diff --git a/.gitignore b/.gitignore index 710c559e72..901dd24cda 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,26 @@ *.o *.ko +*.ko.unsigned *.cmd *.symvers *.order *.mod *.mod.c -lua*.dll -lua*.exp -lua*.lib -lua*.exe -dependencies/ +*.o.d +*.o.rc +*.o.ur-safe build/ +driver/Makefile +driver/driver_config.h +*.pyc +*.config +*.creator +*.creator.user* +*.files +*.includes +*.pro.user +*.ll +.vscode +.cache.mk +build-* + diff --git a/.travis-scripts/linux/before_install.sh b/.travis-scripts/linux/before_install.sh new file mode 100755 index 0000000000..cef5041fcf --- /dev/null +++ b/.travis-scripts/linux/before_install.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test +sudo apt-get update diff --git a/.travis-scripts/linux/build.sh b/.travis-scripts/linux/build.sh new file mode 100755 index 0000000000..7210b11049 --- /dev/null +++ b/.travis-scripts/linux/build.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +set -e + +arch="$(uname -i)" + +if [[ "$arch" == "s390x" ]] || [[ "$arch" == "ppc64le" ]]; then + export CC="gcc-7" + export CXX="g++-7" +else + export CC="gcc-4.8" + export CXX="g++-4.8" +fi + +# this is a workaround to fix the build on ppc64le due to the change in the host kernel on the ppc64le travis backend. +if [[ "$arch" == "ppc64le" ]]; then + sudo apt-get install linux-headers-generic libelf-dev rpm + export KERNELDIR=/lib/modules/$(ls /lib/modules/|sort|head -1)/build +fi + +wget https://github.com/Kitware/CMake/releases/download/v3.16.4/cmake-3.16.4.tar.gz +tar -xzf cmake-3.16.4.tar.gz +cd cmake-3.16.4 +./bootstrap --prefix=/usr +make +sudo make install +cd .. +mkdir build +cd build +cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE +make VERBOSE=1 +make package +make run-unit-tests +cd .. +test/sysdig_trace_regression.sh build/userspace/sysdig/sysdig build/userspace/sysdig/chisels $TRAVIS_BRANCH diff --git a/.travis-scripts/linux/install.sh b/.travis-scripts/linux/install.sh new file mode 100755 index 0000000000..02dcf70933 --- /dev/null +++ b/.travis-scripts/linux/install.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +arch="$(uname -i)" + +if [[ "$arch" != "s390x" ]] && [[ "$arch" != "ppc64le" ]]; then + sudo apt-get --force-yes install g++-4.8 +fi +sudo apt-get install rpm linux-headers-$(uname -r) libelf-dev +sudo apt-get purge cmake diff --git a/.travis-scripts/osx/before_install.sh b/.travis-scripts/osx/before_install.sh new file mode 100755 index 0000000000..8913a1dbf3 --- /dev/null +++ b/.travis-scripts/osx/before_install.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +brew update diff --git a/.travis-scripts/osx/build.sh b/.travis-scripts/osx/build.sh new file mode 100755 index 0000000000..54bea19ead --- /dev/null +++ b/.travis-scripts/osx/build.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +set -e +mkdir build +cd build +cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DUSE_BUNDLED_LUAJIT=OFF -DUSE_BUNDLED_ZLIB=OFF +make install +make run-unit-tests +../test/sysdig_trace_regression.sh $(which sysdig) ./userspace/sysdig/chisels $TRAVIS_BRANCH diff --git a/.travis-scripts/osx/install.sh b/.travis-scripts/osx/install.sh new file mode 100755 index 0000000000..e3697e0666 --- /dev/null +++ b/.travis-scripts/osx/install.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +function install_if_not_present(){ + brew ls | grep ${1} + if [[ ${?} -ne 0 ]]; then + brew install ${1} + else + echo "dependency ${1} already installed" + fi +} +install_if_not_present "cmake" +install_if_not_present "luajit" +install_if_not_present "coreutils" diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..99f1b0a095 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,66 @@ +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +language: c + +matrix: + include: + - env: BUILD_TYPE=Debug + os: linux + - env: BUILD_TYPE=Release + os: linux + - env: BUILD_TYPE=Debug + os: osx + - env: BUILD_TYPE=Release + os: osx + - env: BUILD_TYPE=Debug + os: linux + arch: ppc64le + dist: bionic + - env: BUILD_TYPE=Release + os: linux + arch: ppc64le + dist: bionic + - env: BUILD_TYPE=Debug + os: linux + arch: s390x + dist: bionic + - env: BUILD_TYPE=Release + os: linux + arch: s390x + dist: bionic + - env: BUILD_TYPE=Debug MINIMAL_BUILD=On + os: linux + +sudo: required +services: + - docker +before_install: + - .travis-scripts/${TRAVIS_OS_NAME}/before_install.sh +install: + - .travis-scripts/${TRAVIS_OS_NAME}/install.sh +before_script: + - export KERNELDIR=/lib/modules/$(uname -r)/build +script: + - .travis-scripts/${TRAVIS_OS_NAME}/build.sh +notifications: + webhooks: + urls: + - https://webhooks.gitter.im/e/fdbc2356fb0ea2f15033 + on_success: change + on_failure: always + on_start: never diff --git a/CMakeCPackOptions.cmake b/CMakeCPackOptions.cmake new file mode 100644 index 0000000000..706c128c03 --- /dev/null +++ b/CMakeCPackOptions.cmake @@ -0,0 +1,21 @@ +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +if(CPACK_GENERATOR MATCHES "TGZ") + set(CPACK_SET_DESTDIR "ON") + set(CPACK_STRIP_FILES "OFF") +endif() diff --git a/CMakeLists.txt b/CMakeLists.txt index e49a6ae59e..1f34f1d6c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,89 @@ -cmake_minimum_required(VERSION 2.8) +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +# Prior to doing anything, we make sure that we aren't trying to +# run cmake in-tree. (see Issue 71: https://github.com/draios/sysdig/issues/71) +if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/CMakeLists.txt) + message(FATAL_ERROR + "Looks like you are trying to run cmake from the base sysdig source directory.\n" + "** RUNNING CMAKE FROM THE BASE SYSDIG DIRECTORY WILL NOT WORK **\n" + "To Fix:\n" + " 1. Remove the CMakeCache.txt file in this directory. ex: rm CMakeCache.txt\n" + " 2. Create a build directory from here. ex: mkdir build\n" + " 3. cd into that directory. ex: cd build\n" + " 4. Run cmake from the build directory. ex: cmake ..\n" + " 5. Run make from the build directory. ex: make\n" + "Full paste-able example:\n" + "( rm -f CMakeCache.txt; mkdir build; cd build; cmake ..; make )\n" + "The following wiki page has more information on manually building sysdig: http://bit.ly/1oJ84UI") +endif() + +cmake_minimum_required(VERSION 2.8.2) + +project(sysdig) + +option(MINIMAL_BUILD "Produce a minimal sysdig binary with only the essential features (no eBPF probe driver, no kubernetes, no mesos, no marathon and no container metadata)" OFF) +option(MUSL_OPTIMIZED_BUILD "Enable if you want a musl optimized build" OFF) + +# Add path for custom CMake modules. +list(APPEND CMAKE_MODULE_PATH + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") + +if(NOT DEFINED SYSDIG_VERSION) + set(SYSDIG_VERSION "0.1.1dev") +endif() + +if(NOT DEFINED DIR_ETC) + set(DIR_ETC "${CMAKE_INSTALL_PREFIX}/etc") +endif() + +if(NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE Release) +endif() + +set(PACKAGE_NAME "sysdig") + +add_definitions(-DPLATFORM_NAME="${CMAKE_SYSTEM_NAME}") +add_definitions(-DK8S_DISABLE_THREAD) + +option(BUILD_WARNINGS_AS_ERRORS "Enable building with -Wextra -Werror flags") + +if(MINIMAL_BUILD) + set(MINIMAL_BUILD_FLAGS "-DMINIMAL_BUILD") +endif() + +if(MUSL_OPTIMIZED_BUILD) + set(SYSDIG_MUSL_FLAGS "-static -Os") +endif() -project(sysdig) if(NOT WIN32) set(SYSDIG_DEBUG_FLAGS "-D_DEBUG") + set(CMAKE_COMMON_FLAGS "-Wall -ggdb ${MINIMAL_BUILD_FLAGS} ${SYSDIG_MUSL_FLAGS}") + + if(BUILD_WARNINGS_AS_ERRORS) + set(CMAKE_SUPPRESSED_WARNINGS "-Wno-unused-parameter -Wno-missing-field-initializers -Wno-sign-compare -Wno-type-limits -Wno-implicit-fallthrough -Wno-format-truncation") + set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wextra -Werror ${CMAKE_SUPPRESSED_WARNINGS}") + endif() - set(CMAKE_C_FLAGS "-Wall -ggdb") - set(CMAKE_CXX_FLAGS "-Wall -ggdb --std=c++0x") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_COMMON_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_COMMON_FLAGS} -std=c++0x") set(CMAKE_C_FLAGS_DEBUG "${SYSDIG_DEBUG_FLAGS}") set(CMAKE_CXX_FLAGS_DEBUG "${SYSDIG_DEBUG_FLAGS}") @@ -16,21 +92,39 @@ if(NOT WIN32) set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-strict-aliasing -DNDEBUG") if(CMAKE_SYSTEM_NAME MATCHES "Linux") + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(KBUILD_FLAGS "${SYSDIG_DEBUG_FLAGS} ${SYSDIG_FEATURE_FLAGS}") + else() + set(KBUILD_FLAGS "${SYSDIG_FEATURE_FLAGS}") + endif() + + if(NOT DEFINED PROBE_VERSION) + set(PROBE_VERSION "${SYSDIG_VERSION}") + endif() + if(NOT DEFINED PROBE_NAME) + set(PROBE_NAME "sysdig-probe") + endif() + + if(NOT DEFINED PROBE_DEVICE_NAME) + set(PROBE_DEVICE_NAME "sysdig") + endif() + add_subdirectory(driver) + add_definitions(-DHAS_CAPTURE) endif() - include(ExternalProject) - ExternalProject_Add(luajit - SOURCE_DIR ${PROJECT_SOURCE_DIR}/third-party/LuaJIT-2.0.2 - CONFIGURE_COMMAND "" - BUILD_COMMAND make - BUILD_IN_SOURCE 1 - INSTALL_COMMAND "") + add_subdirectory(scripts) + + if(CMAKE_SYSTEM_NAME MATCHES "SunOS") + set(CMD_MAKE gmake) + else() + set(CMD_MAKE make) + endif() else() - set(SYSDIG_FLAGS_WIN "-D_CRT_SECURE_NO_WARNINGS -DWIN32 /EHsc /W3") - set(SYSDIG_FLAGS_WIN_DEBUG "/MTd") + set(SYSDIG_FLAGS_WIN "-D_CRT_SECURE_NO_WARNINGS -DWIN32 /EHsc /W3 /Zi") + set(SYSDIG_FLAGS_WIN_DEBUG "/MTd /Od") set(SYSDIG_FLAGS_WIN_RELEASE "/MT") set(CMAKE_C_FLAGS "${SYSDIG_FLAGS_WIN}") @@ -42,49 +136,536 @@ else() set(CMAKE_C_FLAGS_RELEASE "${SYSDIG_FLAGS_WIN_RELEASE}") set(CMAKE_CXX_FLAGS_RELEASE "${SYSDIG_FLAGS_WIN_RELEASE}") - include(ExternalProject) - ExternalProject_Add(luajit - SOURCE_DIR ${PROJECT_SOURCE_DIR}/third-party/LuaJIT-2.0.2 - CONFIGURE_COMMAND "" - BUILD_COMMAND msvcbuild.bat - BINARY_DIR ${PROJECT_SOURCE_DIR}/third-party/LuaJIT-2.0.2/src - INSTALL_COMMAND "") - endif() if(APPLE) set(CMAKE_EXE_LINKER_FLAGS "-pagezero_size 10000 -image_base 100000000") endif() +include(ExternalProject) + +option(USE_BUNDLED_DEPS "Enable bundled dependencies instead of using the system ones" ON) + +# +# LuaJIT +# +option(USE_BUNDLED_LUAJIT "Enable building of the bundled LuaJIT" ${USE_BUNDLED_DEPS}) + +if(NOT USE_BUNDLED_LUAJIT) + find_path(LUAJIT_INCLUDE luajit.h PATH_SUFFIXES luajit-2.0 luajit) + find_library(LUAJIT_LIB NAMES luajit luajit-5.1) + if(LUAJIT_INCLUDE AND LUAJIT_LIB) + message(STATUS "Found LuaJIT: include: ${LUAJIT_INCLUDE}, lib: ${LUAJIT_LIB}") + else() + # alternatively try stock Lua + find_package(Lua51) + set(LUAJIT_LIB ${LUA_LIBRARY}) + set(LUAJIT_INCLUDE ${LUA_INCLUDE_DIR}) + + if(NOT ${LUA51_FOUND}) + message(FATAL_ERROR "Couldn't find system LuaJIT or Lua") + endif() + endif() +else() + set(LUAJIT_SRC "${PROJECT_BINARY_DIR}/luajit-prefix/src/luajit/src") + message(STATUS "Using bundled LuaJIT in '${LUAJIT_SRC}'") + set(LUAJIT_INCLUDE "${LUAJIT_SRC}") + if(NOT WIN32) + set(LUAJIT_LIB "${LUAJIT_SRC}/libluajit.a") + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") + ExternalProject_Add(luajit + GIT_REPOSITORY "https://github.com/moonjit/moonjit" + GIT_TAG "2.1.2" + PATCH_COMMAND sed -i "s/luaL_reg/luaL_Reg/g" ${PROJECT_SOURCE_DIR}/userspace/libsinsp/chisel.cpp && sed -i "s/luaL_reg/luaL_Reg/g" ${PROJECT_SOURCE_DIR}/userspace/libsinsp/lua_parser.cpp && sed -i "s/luaL_getn/lua_objlen /g" ${PROJECT_SOURCE_DIR}/userspace/libsinsp/lua_parser_api.cpp + CONFIGURE_COMMAND "" + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${LUAJIT_LIB} + INSTALL_COMMAND "") + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x") + ExternalProject_Add(luajit + GIT_REPOSITORY "https://github.com/linux-on-ibm-z/LuaJIT.git" + GIT_TAG "v2.1" + PATCH_COMMAND sed -i "s/luaL_reg/luaL_Reg/g" ${PROJECT_SOURCE_DIR}/userspace/libsinsp/chisel.cpp && sed -i "s/luaL_reg/luaL_Reg/g" ${PROJECT_SOURCE_DIR}/userspace/libsinsp/lua_parser.cpp && sed -i "s/luaL_getn/lua_objlen /g" ${PROJECT_SOURCE_DIR}/userspace/libsinsp/lua_parser_api.cpp + CONFIGURE_COMMAND "" + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${LUAJIT_LIB} + INSTALL_COMMAND "") + else() + ExternalProject_Add(luajit + URL "http://download.draios.com/dependencies/LuaJIT-2.0.3.tar.gz" + URL_MD5 "f14e9104be513913810cd59c8c658dc0" + CONFIGURE_COMMAND "" + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${LUAJIT_LIB} + INSTALL_COMMAND "") + endif() + else() + set(LUAJIT_LIB "${LUAJIT_SRC}/lua51.lib") + ExternalProject_Add(luajit + URL "http://download.draios.com/dependencies/LuaJIT-2.0.3.tar.gz" + URL_MD5 "f14e9104be513913810cd59c8c658dc0" + CONFIGURE_COMMAND "" + BUILD_COMMAND msvcbuild.bat + BUILD_BYPRODUCTS ${LUAJIT_LIB} + BINARY_DIR "${LUAJIT_SRC}" + INSTALL_COMMAND "") + endif() +endif() + +# +# JsonCpp +# +option(USE_BUNDLED_JSONCPP "Enable building of the bundled jsoncpp" ${USE_BUNDLED_DEPS}) + +set(JSONCPP_SRC "${PROJECT_SOURCE_DIR}/userspace/libsinsp/third-party/jsoncpp") + +if(NOT USE_BUNDLED_JSONCPP) + find_path(JSONCPP_INCLUDE json/json.h PATH_SUFFIXES jsoncpp) + find_library(JSONCPP_LIB NAMES jsoncpp) + if(JSONCPP_INCLUDE AND JSONCPP_LIB) + message(STATUS "Found jsoncpp: include: ${JSONCPP_INCLUDE}, lib: ${JSONCPP_LIB}") + else() + message(FATAL_ERROR "Couldn't find system jsoncpp") + endif() +else() + set(JSONCPP_INCLUDE "${JSONCPP_SRC}") + set(JSONCPP_LIB_SRC "${JSONCPP_SRC}/jsoncpp.cpp") + message(STATUS "Using bundled jsoncpp in '${JSONCPP_SRC}'") +endif() + +# +# zlib +# +option(USE_BUNDLED_ZLIB "Enable building of the bundled zlib" ${USE_BUNDLED_DEPS}) + +if(NOT MINIMAL_BUILD) + if(NOT USE_BUNDLED_ZLIB) + find_path(ZLIB_INCLUDE zlib.h PATH_SUFFIXES zlib) + find_library(ZLIB_LIB NAMES z) + if(ZLIB_INCLUDE AND ZLIB_LIB) + message(STATUS "Found zlib: include: ${ZLIB_INCLUDE}, lib: ${ZLIB_LIB}") + else() + message(FATAL_ERROR "Couldn't find system zlib") + endif() + else() + set(ZLIB_SRC "${PROJECT_BINARY_DIR}/zlib-prefix/src/zlib") + message(STATUS "Using bundled zlib in '${ZLIB_SRC}'") + set(ZLIB_INCLUDE "${ZLIB_SRC}") + if(NOT WIN32) + set(ZLIB_LIB "${ZLIB_SRC}/libz.a") + ExternalProject_Add(zlib + URL "http://download.draios.com/dependencies/zlib-1.2.11.tar.gz" + URL_MD5 "1c9f62f0778697a09d36121ead88e08e" + CONFIGURE_COMMAND "./configure" + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${ZLIB_LIB} + INSTALL_COMMAND "") + else() + set(ZLIB_LIB "${ZLIB_SRC}/zdll.lib") + ExternalProject_Add(zlib + URL "http://download.draios.com/dependencies/zlib-1.2.11.tar.gz" + URL_MD5 "1c9f62f0778697a09d36121ead88e08e" + CONFIGURE_COMMAND "" + BUILD_COMMAND nmake -f win32/Makefile.msc + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${ZLIB_LIB} + INSTALL_COMMAND "") + endif() + endif() +endif() # MINIMAL_BUILD + +# +# Intel tbb +# +if(NOT WIN32) + option(USE_BUNDLED_TBB "Enable building of the bundled tbb" ${USE_BUNDLED_DEPS}) + if(NOT USE_BUNDLED_TBB) + find_path(TBB_INCLUDE_DIR tbb.h PATH_SUFFIXES tbb) + find_library(TBB_LIB NAMES tbb) + if(TBB_INCLUDE_DIR AND TBB_LIB) + message(STATUS "Found tbb: include: ${TBB_INCLUDE_DIR}, lib: ${TBB_LIB}") + else() + message(FATAL_ERROR "Couldn't find system tbb") + endif() + else() + set(TBB_SRC "${PROJECT_BINARY_DIR}/tbb-prefix/src/tbb") + + message(STATUS "Using bundled tbb in '${TBB_SRC}'") + + set(TBB_INCLUDE_DIR "${TBB_SRC}/include/") + set(TBB_LIB "${TBB_SRC}/build/lib_release/libtbb.a") + ExternalProject_Add(tbb + URL "http://s3.amazonaws.com/download.draios.com/dependencies/tbb-2018_U5.tar.gz" + URL_MD5 "ff3ae09f8c23892fbc3008c39f78288f" + CONFIGURE_COMMAND "" + BUILD_COMMAND ${CMD_MAKE} tbb_build_dir=${TBB_SRC}/build tbb_build_prefix=lib extra_inc=big_iron.inc + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${TBB_LIB} + INSTALL_COMMAND "") + endif() +endif() + +# +# jq +# +if(NOT WIN32 AND NOT APPLE) + option(USE_BUNDLED_JQ "Enable building of the bundled jq" ${USE_BUNDLED_DEPS}) + + if(NOT USE_BUNDLED_JQ) + find_path(JQ_INCLUDE jq.h PATH_SUFFIXES jq) + find_library(JQ_LIB NAMES jq) + if(JQ_INCLUDE AND JQ_LIB) + message(STATUS "Found jq: include: ${JQ_INCLUDE}, lib: ${JQ_LIB}") + else() + message(FATAL_ERROR "Couldn't find system jq") + endif() + else() + set(JQ_SRC "${PROJECT_BINARY_DIR}/jq-prefix/src/jq") + message(STATUS "Using bundled jq in '${JQ_SRC}'") + set(JQ_INCLUDE "${JQ_SRC}/target/include") + set(JQ_INSTALL_DIR "${JQ_SRC}/target") + set(JQ_LIB "${JQ_INSTALL_DIR}/lib/libjq.a") + set(ONIGURUMA_LIB "${JQ_INSTALL_DIR}/lib/libonig.a") + message(STATUS "Bundled jq: include: ${JQ_INCLUDE}, lib: ${JQ_LIB}") + + ExternalProject_Add( + jq + URL "http://download.draios.com/dependencies/jq-1.6.tar.gz" + URL_HASH "SHA256=787518068c35e244334cc79b8e56b60dbab352dff175b7f04a94f662b540bfd9" + CONFIGURE_COMMAND ./configure --disable-maintainer-mode --enable-all-static --disable-dependency-tracking --with-oniguruma=builtin --prefix=${JQ_INSTALL_DIR} + BUILD_COMMAND ${CMD_MAKE} LDFLAGS=-all-static + BUILD_IN_SOURCE 1 + INSTALL_COMMAND ${CMD_MAKE} install) + endif() +endif() + +# +# ncurses, keep it simple for the moment +# +if(NOT WIN32) + option(USE_BUNDLED_NCURSES "Enable building of the bundled ncurses" ${USE_BUNDLED_DEPS}) + + if(NOT USE_BUNDLED_NCURSES) + set(CURSES_NEED_NCURSES TRUE) + find_package(Curses REQUIRED) + message(STATUS "Found ncurses: include: ${CURSES_INCLUDE_DIR}, lib: ${CURSES_LIBRARIES}") + else() + set(CURSES_BUNDLE_DIR "${PROJECT_BINARY_DIR}/ncurses-prefix/src/ncurses") + set(CURSES_INCLUDE_DIR "${CURSES_BUNDLE_DIR}/include/") + set(CURSES_LIBRARIES "${CURSES_BUNDLE_DIR}/lib/libncurses.a") + + message(STATUS "Using bundled ncurses in '${CURSES_BUNDLE_DIR}'") + + ExternalProject_Add(ncurses + URL "http://download.draios.com/dependencies/ncurses-6.0-20150725.tgz" + URL_MD5 "32b8913312e738d707ae68da439ca1f4" + CONFIGURE_COMMAND ./configure --without-cxx --without-cxx-binding --without-ada --without-manpages --without-progs --without-tests --with-terminfo-dirs=/etc/terminfo:/lib/terminfo:/usr/share/terminfo + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${CURSES_LIBRARIES} + INSTALL_COMMAND "") + endif() +endif() + +if(NOT WIN32 AND NOT APPLE) + # + # libb64 + # + option(USE_BUNDLED_B64 "Enable building of the bundled b64" ${USE_BUNDLED_DEPS}) + + if(NOT USE_BUNDLED_B64) + find_path(B64_INCLUDE NAMES b64/encode.h) + find_library(B64_LIB NAMES b64) + if(B64_INCLUDE AND B64_LIB) + message(STATUS "Found b64: include: ${B64_INCLUDE}, lib: ${B64_LIB}") + else() + message(FATAL_ERROR "Couldn't find system b64") + endif() + else() + set(B64_SRC "${PROJECT_BINARY_DIR}/b64-prefix/src/b64") + message(STATUS "Using bundled b64 in '${B64_SRC}'") + set(B64_INCLUDE "${B64_SRC}/include") + set(B64_LIB "${B64_SRC}/src/libb64.a") + ExternalProject_Add(b64 + URL "http://download.draios.com/dependencies/libb64-1.2.src.zip" + URL_MD5 "a609809408327117e2c643bed91b76c5" + CONFIGURE_COMMAND "" + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${B64_LIB} + INSTALL_COMMAND "") + endif() +endif() + +if(NOT WIN32 AND NOT MINIMAL_BUILD) + # + # OpenSSL + # + option(USE_BUNDLED_OPENSSL "Enable building of the bundled OpenSSL" ${USE_BUNDLED_DEPS}) + + if(NOT USE_BUNDLED_OPENSSL) + find_package(OpenSSL REQUIRED) + message(STATUS "Found OpenSSL: include: ${OPENSSL_INCLUDE_DIR}, lib: ${OPENSSL_LIBRARIES}") + else() + set(OPENSSL_BUNDLE_DIR "${PROJECT_BINARY_DIR}/openssl-prefix/src/openssl") + set(OPENSSL_INSTALL_DIR "${OPENSSL_BUNDLE_DIR}/target") + set(OPENSSL_INCLUDE_DIR "${PROJECT_BINARY_DIR}/openssl-prefix/src/openssl/include") + set(OPENSSL_LIBRARY_SSL "${OPENSSL_INSTALL_DIR}/lib/libssl.a") + set(OPENSSL_LIBRARY_CRYPTO "${OPENSSL_INSTALL_DIR}/lib/libcrypto.a") + + message(STATUS "Using bundled openssl in '${OPENSSL_BUNDLE_DIR}'") + + ExternalProject_Add(openssl + URL "http://download.draios.com/dependencies/openssl-1.0.2n.tar.gz" + URL_MD5 "13bdc1b1d1ff39b6fd42a255e74676a4" + CONFIGURE_COMMAND ./config shared --prefix=${OPENSSL_INSTALL_DIR} + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${OPENSSL_LIBRARY_SSL} ${OPENSSL_LIBRARY_CRYPTO} + INSTALL_COMMAND ${CMD_MAKE} install) + endif() + + # + # libcurl + # + option(USE_BUNDLED_CURL "Enable building of the bundled curl" ${USE_BUNDLED_DEPS}) + + if(NOT USE_BUNDLED_CURL) + find_package(CURL REQUIRED) + message(STATUS "Found CURL: include: ${CURL_INCLUDE_DIR}, lib: ${CURL_LIBRARIES}") + else() + set(CURL_BUNDLE_DIR "${PROJECT_BINARY_DIR}/curl-prefix/src/curl") + set(CURL_INCLUDE_DIR "${CURL_BUNDLE_DIR}/include/") + set(CURL_LIBRARIES "${CURL_BUNDLE_DIR}/lib/.libs/libcurl.a") + + if(NOT USE_BUNDLED_OPENSSL) + set(CURL_SSL_OPTION "--with-ssl") + else() + set(CURL_SSL_OPTION "--with-ssl=${OPENSSL_INSTALL_DIR}") + message(STATUS "Using bundled curl in '${CURL_BUNDLE_DIR}'") + message(STATUS "Using SSL for curl in '${CURL_SSL_OPTION}'") + endif() + + + ExternalProject_Add(curl + DEPENDS openssl + URL "http://download.draios.com/dependencies/curl-7.61.0.tar.bz2" + URL_MD5 "31d0a9f48dc796a7db351898a1e5058a" + CONFIGURE_COMMAND ./configure ${CURL_SSL_OPTION} --disable-threaded-resolver --disable-shared --enable-optimize --disable-curldebug --disable-rt --enable-http --disable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smb --disable-smtp --disable-gopher --disable-sspi --disable-ntlm-wb --disable-tls-srp --without-winssl --without-darwinssl --without-polarssl --without-cyassl --without-nss --without-axtls --without-ca-path --without-ca-bundle --without-libmetalink --without-librtmp --without-winidn --without-libidn --without-libidn2 --without-nghttp2 --without-libssh2 --without-libpsl + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${CURL_LIBRARIES} + INSTALL_COMMAND "") + endif() +endif() # NOT WIN32 AND NOT MINIMAL_BUILD + +if(NOT MINIMAL_BUILD) + option(USE_BUNDLED_CARES "Enable building of the bundled c-ares" ${USE_BUNDLED_DEPS}) + if(NOT USE_BUNDLED_CARES) + find_path(CARES_INCLUDE NAMES cares/ares.h ares.h) + find_library(CARES_LIB NAMES cares) + if(CARES_INCLUDE AND CARES_LIB) + message(STATUS "Found c-ares: include: ${CARES_INCLUDE}, lib: ${CARES_LIB}") + else() + message(FATAL_ERROR "Couldn't find system c-ares") + endif() + else() + set(CARES_SRC "${PROJECT_BINARY_DIR}/c-ares-prefix/src/c-ares") + message(STATUS "Using bundled c-ares in '${CARES_SRC}'") + set(CARES_INCLUDE "${CARES_SRC}/target/include") + set(CARES_LIB "${CARES_SRC}/target/lib/libcares.a") + ExternalProject_Add(c-ares + URL "http://download.sysdig.com/dependencies/c-ares-1.13.0.tar.gz" + URL_MD5 "d2e010b43537794d8bedfb562ae6bba2" + CONFIGURE_COMMAND ./configure --prefix=${CARES_SRC}/target + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${CARES_INCLUDE} ${CARES_LIB} + INSTALL_COMMAND ${CMD_MAKE} install) + endif() +endif() # NOT MINIMAL_BUILD + +if(NOT WIN32 AND NOT APPLE) + if(NOT MINIMAL_BUILD) + option(USE_BUNDLED_PROTOBUF "Enable building of the bundled protobuf" ${USE_BUNDLED_DEPS}) + if(NOT USE_BUNDLED_PROTOBUF) + find_program(PROTOC NAMES protoc) + find_path(PROTOBUF_INCLUDE NAMES google/protobuf/message.h) + find_library(PROTOBUF_LIB NAMES protobuf) + if(PROTOC AND PROTOBUF_INCLUDE AND PROTOBUF_LIB) + message(STATUS "Found protobuf: compiler: ${PROTOC}, include: ${PROTOBUF_INCLUDE}, lib: ${PROTOBUF_LIB}") + else() + message(FATAL_ERROR "Couldn't find system protobuf") + endif() + else() + set(PROTOBUF_SRC "${PROJECT_BINARY_DIR}/protobuf-prefix/src/protobuf") + message(STATUS "Using bundled protobuf in '${PROTOBUF_SRC}'") + set(PROTOC "${PROTOBUF_SRC}/target/bin/protoc") + set(PROTOBUF_INCLUDE "${PROTOBUF_SRC}/target/include") + set(PROTOBUF_LIB "${PROTOBUF_SRC}/target/lib/libprotobuf.a") + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x") + ExternalProject_Add(protobuf + DEPENDS openssl zlib + URL "http://download.sysdig.com/dependencies/protobuf-cpp-3.5.0.tar.gz" + URL_MD5 "e4ba8284a407712168593e79e6555eb2" + PATCH_COMMAND wget http://download.sysdig.com/dependencies/protobuf-3.5.0-s390x.patch && patch -p1 -i protobuf-3.5.0-s390x.patch + # TODO what if using system zlib? + CONFIGURE_COMMAND /usr/bin/env CPPFLAGS=-I${ZLIB_INCLUDE} LDFLAGS=-L${ZLIB_SRC} ./configure --with-zlib --prefix=${PROTOBUF_SRC}/target + COMMAND aclocal && automake + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${PROTOC} ${PROTOBUF_INCLUDE} ${PROTOBUF_LIB} + INSTALL_COMMAND make install) + else() + ExternalProject_Add(protobuf + DEPENDS openssl zlib + URL "http://download.sysdig.com/dependencies/protobuf-cpp-3.5.0.tar.gz" + URL_MD5 "e4ba8284a407712168593e79e6555eb2" + # TODO what if using system zlib? + CONFIGURE_COMMAND /usr/bin/env CPPFLAGS=-I${ZLIB_INCLUDE} LDFLAGS=-L${ZLIB_SRC} ./configure --with-zlib --prefix=${PROTOBUF_SRC}/target + BUILD_COMMAND ${CMD_MAKE} + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${PROTOC} ${PROTOBUF_INCLUDE} ${PROTOBUF_LIB} + INSTALL_COMMAND make install) + endif() + endif() + + + option(USE_BUNDLED_GRPC "Enable building of the bundled grpc" ${USE_BUNDLED_DEPS}) + if(NOT USE_BUNDLED_GRPC) + find_path(GRPCXX_INCLUDE NAMES grpc++/grpc++.h) + if(GRPCXX_INCLUDE) + set(GRPC_INCLUDE ${GRPCXX_INCLUDE}) + else() + find_path(GRPCPP_INCLUDE NAMES grpcpp/grpcpp.h) + set(GRPC_INCLUDE ${GRPCPP_INCLUDE}) + add_definitions(-DGRPC_INCLUDE_IS_GRPCPP=1) + endif() + find_library(GRPC_LIB NAMES grpc_unsecure) + find_library(GRPCPP_LIB NAMES grpc++_unsecure) + if(GRPC_INCLUDE AND GRPC_LIB AND GRPCPP_LIB) + message(STATUS "Found grpc: include: ${GRPC_INCLUDE}, C lib: ${GRPC_LIB}, C++ lib: ${GRPCPP_LIB}") + else() + message(FATAL_ERROR "Couldn't find system grpc") + endif() + find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin) + if(NOT GRPC_CPP_PLUGIN) + message(FATAL_ERROR "System grpc_cpp_plugin not found") + endif() + else() + find_package(PkgConfig) + if(NOT PKG_CONFIG_FOUND) + message(FATAL_ERROR "pkg-config binary not found") + endif() + message(STATUS "Found pkg-config executable: ${PKG_CONFIG_EXECUTABLE}") + set(GRPC_SRC "${PROJECT_BINARY_DIR}/grpc-prefix/src/grpc") + message(STATUS "Using bundled grpc in '${GRPC_SRC}'") + set(GRPC_INCLUDE "${GRPC_SRC}/include") + set(GRPC_LIB "${GRPC_SRC}/libs/opt/libgrpc_unsecure.a") + set(GRPCPP_LIB "${GRPC_SRC}/libs/opt/libgrpc++_unsecure.a") + set(GRPC_CPP_PLUGIN "${GRPC_SRC}/bins/opt/grpc_cpp_plugin") + + get_filename_component(PROTOC_DIR ${PROTOC} PATH) + + ExternalProject_Add(grpc + DEPENDS protobuf zlib c-ares + URL "http://download.draios.com/dependencies/grpc-1.8.1.tar.gz" + URL_MD5 "2fc42c182a0ed1b48ad77397f76bb3bc" + CONFIGURE_COMMAND "" + # TODO what if using system openssl, protobuf or cares? + BUILD_COMMAND CFLAGS=-Wno-implicit-fallthrough HAS_SYSTEM_ZLIB=false LDFLAGS=-static PATH=${PROTOC_DIR}:$ENV{PATH} PKG_CONFIG_PATH=${OPENSSL_BUNDLE_DIR}:${PROTOBUF_SRC}:${CARES_SRC} PKG_CONFIG=${PKG_CONFIG_EXECUTABLE} make grpc_cpp_plugin static_cxx static_c + BUILD_IN_SOURCE 1 + BUILD_BYPRODUCTS ${GRPC_LIB} ${GRPCPP_LIB} + # TODO s390x support + # TODO what if using system zlib + PATCH_COMMAND rm -rf third_party/zlib && ln -s ${ZLIB_SRC} third_party/zlib && wget https://download.sysdig.com/dependencies/grpc-1.8.1-Makefile.patch && patch < grpc-1.8.1-Makefile.patch + INSTALL_COMMAND "") + endif() + endif() # NOT WIN32 AND NOT APPLE +endif() # MINIMAL_BUILD + +option(CREATE_TEST_TARGETS "Enable make-targets for unit testing" ON) +if(CREATE_TEST_TARGETS AND NOT WIN32) + option(USE_BUNDLED_GTEST "Enable building of the bundled gtest" ${USE_BUNDLED_DEPS}) + if(NOT USE_BUNDLED_GTEST) + find_path(GTEST_INCLUDE_DIR PATH_SUFFIXES gtest NAMES gtest.h) + find_library(GTEST_LIB NAMES gtest) + find_library(GTEST_MAIN_LIB NAMES gtest_main) + if(GTEST_INCLUDE_DIR AND GTEST_LIB AND GTEST_MAIN_LIB) + message(STATUS "Found gtest: include: ${GTEST_INCLUDE_DIR}, lib: ${GTEST_LIB}, main lib: ${GTEST_MAIN_LIB}") + else() + message(FATAL_ERROR "Couldn't find system gtest") + endif() + else() + # https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project + # Download and unpack googletest at configure time + configure_file(CMakeListsGtestInclude.cmake googletest-download/CMakeLists.txt) + execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") + endif() + execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") + endif() + + # Add googletest directly to our build. This defines + # the gtest and gtest_main targets. + add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src + ${CMAKE_CURRENT_BINARY_DIR}/googletest-build + EXCLUDE_FROM_ALL) + + set(GTEST_INCLUDE_DIR "${gtest_SOURCE_DIR}/include/gtest") + set(GTEST_MAIN_LIB "gtest_main") + endif() +endif() # NOT WIN32 + add_subdirectory(userspace/sysdig) add_subdirectory(userspace/libscap) -if(CMAKE_SYSTEM_NAME MATCHES "Linux") - add_subdirectory(userspace/libscap/examples/01-open) - add_subdirectory(userspace/libscap/examples/02-validatebuffer) -endif() add_subdirectory(userspace/libsinsp) -set(CPACK_PACKAGE_NAME "sysdig") -set(CPACK_PACKAGE_VENDOR "Draios Inc.") -set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A system exploration and troubleshooting tool") +if(CREATE_TEST_TARGETS AND NOT WIN32) + # Add unit test directories + add_subdirectory(userspace/libsinsp/test) + + # Add command to run all unit tests at once via the make system. + # This is preferred vs using ctest's add_test because it will build + # the code and output to stdout. + add_custom_target(run-unit-tests + COMMAND ${CMAKE_MAKE_PROGRAM} run-unit-test-libsinsp + ) +endif() + +set(CPACK_PACKAGE_NAME "${PACKAGE_NAME}") +set(CPACK_PACKAGE_VENDOR "Sysdig Inc.") +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "sysdig, a system-level exploration and troubleshooting tool") set(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/scripts/description.txt") set(CPACK_PACKAGE_VERSION "${SYSDIG_VERSION}") set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_SYSTEM_PROCESSOR}") +set(CPACK_PROJECT_CONFIG_FILE "${PROJECT_SOURCE_DIR}/CMakeCPackOptions.cmake") set(CPACK_STRIP_FILES "ON") -set(CPACK_GENERATOR DEB RPM) +set(CPACK_GENERATOR DEB RPM TGZ) -set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Draios Inc. ") +set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Sysdig ") set(CPACK_DEBIAN_PACKAGE_SECTION "utils") -set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "http://www.draios.com") +set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "http://www.sysdig.com") set(CPACK_DEBIAN_PACKAGE_DEPENDS "dkms (>= 2.1.0.0)") -set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_SOURCE_DIR}/scripts/debian/postinst;${PROJECT_SOURCE_DIR}/scripts/debian/prerm") +set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_BINARY_DIR}/scripts/debian/postinst;${CMAKE_BINARY_DIR}/scripts/debian/prerm") -set(CPACK_RPM_PACKAGE_LICENSE "GPLv2") -set(CPACK_RPM_PACKAGE_URL "http://www.draios.com") +set(CPACK_RPM_PACKAGE_LICENSE "Apache v2.0") +set(CPACK_RPM_PACKAGE_URL "http://www.sysdig.com") set(CPACK_RPM_PACKAGE_REQUIRES "dkms, gcc, make, kernel-devel, perl") set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${PROJECT_SOURCE_DIR}/scripts/rpm/postinstall") set(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${PROJECT_SOURCE_DIR}/scripts/rpm/preuninstall") -set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION /usr/src) +set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION /usr/src /usr/share/man /usr/share/man/man8) include(CPack) diff --git a/CMakeListsGtestInclude.cmake b/CMakeListsGtestInclude.cmake new file mode 100644 index 0000000000..45ff47badf --- /dev/null +++ b/CMakeListsGtestInclude.cmake @@ -0,0 +1,33 @@ +# +# Copyright (C) 2019 Sysdig Inc. +# +# This file is part of sysdig . +# +# 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. +# + +cmake_minimum_required(VERSION 2.8.2) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG master + SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/COPYING b/COPYING index d159169d10..9f7ded3381 100644 --- a/COPYING +++ b/COPYING @@ -1,339 +1,203 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. +The contents of the driver/ subdirectory are licensed separately--see COPYING.driver. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/NOTICES b/NOTICES new file mode 100644 index 0000000000..2cfa2fa24f --- /dev/null +++ b/NOTICES @@ -0,0 +1,28 @@ +Copyright (C) 2013-2018 Draios Inc. dba Sysdig + +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. + +SYSDIG SUBCOMPONENTS: + +-The files in and its subdirectories are used to compile the + kernel module and may be injected into eBPF; these are dual licensed + under the MIT license or the GNU General Public License 2. Copies of + both licenses are available in the subdirectory. + +-The following files are under Apache 2.0: + + userspace/sysdig/chisels/fileslower.lua, Copyright (C) 2014 Brendan Gregg + userspace/sysdig/chisels/memcachelog.lua, Copyright (C) 2015 Donatas Abraitis + userspace/sysdig/chisels/subsecoffset.lua, Copyright (C) 2013-2014 Draios Inc. dba Sysdig, Copyright (C) 2015 Brendan Gregg + userspace/sysdig/chisels/v_backlog.lua, Copyright (C) Donatas Abraitis + diff --git a/README.md b/README.md index 728c862ec3..25734ca43c 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,93 @@ sysdig ====== -Welcome to **sysdig** - an open source system-level exploration and troubleshooting tool. +[![Build Status](https://travis-ci.com/draios/sysdig.png?branch=master)](https://travis-ci.com/draios/sysdig) -Where to start? ---- -If this is your first time hearing about sysdig, we recommend you start with the website: -www.sysdig.org +# Welcome to **sysdig**! +**Sysdig** is a universal system visibility tool with native support for containers: +`~$ sysdig` +**Csysdig** is a simple, intuitive, and fully customizable curses UI for sysdig: +`~$ csysdig` + What does sysdig do and why should I use it? --- -Sysdig captures system calls and other system level events using a linux kernel facility called tracepoints, which means much less overhead than strace. +**Sysdig is a simple tool for deep system visibility, with native support for containers.** + +We built sysdig to give you _easy access_ to the actual behavior of your Linux systems and containers. Honestly, the best way to understand sysdig is to [try it](https://github.com/draios/sysdig/wiki/How-to-Install-Sysdig-for-Linux) - its super easy! Or here's a quick video introduction to csysdig, the simple, intuitive, and fully customizable curses-based UI for sysdig: https://www.youtube.com/watch?v=UJ4wVrbP-Q8 + +Far too often, system-level monitoring and troubleshooting still involves logging into a machine with SSH and using a plethora of dated tools with very inconsistent interfaces. And many of these classic Linux tools breakdown completely in containerized environments. Sysdig unites your Linux toolkit into a single, consistent, easy-to-use interface. And sysdig's unique architecture allows deep inspection into containers, right out of the box, without having to instrument the containers themselves in any way. + +Sysdig instruments your physical and virtual machines at the OS level by installing into the Linux kernel and capturing system calls and other OS events. Sysdig also makes it possible to create trace files for system activity, similarly to what you can do for networks with tools like tcpdump and Wireshark. This way, problems can be analyzed at a later time, without losing important information. Rich system state is stored in the trace files, so that the captured activity can be put into full context. + +Think about sysdig as strace + tcpdump + htop + iftop + lsof + ...awesome sauce. + +Documentation / Support +--- +[Visit the wiki](https://github.com/draios/sysdig/wiki) for full documentation on sysdig and its APIs. + +For support using sysdig, please contact [the official mailing list](https://groups.google.com/forum/#!forum/sysdig). + +Join the Community +--- +* Contact the [official mailing list](https://groups.google.com/forum/#!forum/sysdig) for support and to talk with other users +* Follow us on [Twitter](https://twitter.com/sysdig) +* This is our [blog](https://sysdig.com/blog/). There are many like it, but this one is ours. +* Join our [Public Slack](https://slack.sysdig.com) channel for sysdig announcements and discussions. + +License Terms +--- +The sysdig userspace programs and supporting code are licensed to you under the [Apache 2.0](./COPYING) open source license. + +The sysdig kernel module, which is in the `driver` subdirectory, is licensed to you under both the [MIT](https://github.com/draios/sysdig/blob/dev/driver/MIT.txt) and [GPLv2](https://github.com/draios/sysdig/blob/dev/driver/GPL2.txt) open source licenses. + +Contributor License Agreements +--- +### Background +As sysdig matures and gains wider acceptance, we are formalizing the way that we accept contributions of code from the contributing community. We must now ask that contributions to sysdig be provided subject to the terms and conditions of a [Contributor License Agreement (CLA)](https://github.com/draios/sysdig/tree/dev/cla). The CLA comes in two forms, applicable to contributions by individuals, or by legal entities such as corporations and their employees. We recognize that entering into a CLA with us involves real consideration on your part, and we’ve tried to make this process as clear and simple as possible. + +We’ve modeled our CLA off of industry standards, such as [the CLA used by Kubernetes](https://github.com/kubernetes/kubernetes/blob/master/CONTRIBUTING.md). Note that this agreement is not a transfer of copyright ownership, this simply is a license agreement for contributions, intended to clarify the intellectual property license granted with contributions from any person or entity. It is for your protection as a contributor as well as the protection of sysdig; it does not change your rights to use your own contributions for any other purpose. + +For some background on why contributor license agreements are necessary, you can read FAQs from many other open source projects: +- [Django’s excellent CLA FAQ](https://www.djangoproject.com/foundation/cla/faq/) +- [A well-written chapter from Karl Fogel’s Producing Open Source Software on CLAs](http://producingoss.com/en/copyright-assignment.html) +- [The Wikipedia article on CLAs](http://en.wikipedia.org/wiki/Contributor_license_agreement) + +As always, we are grateful for your past and present contributions to sysdig. -It then "packetizes" this information, so that you can save it into trace files and filter it, a bit like you would do with tcpdump. This makes it very flexible to explore what processes are doing. +### What do I need to do in order to contribute code? +**Individual contributions**: Individuals who wish to make contributions must review the [Individual Contributor License Agreement](https://github.com/draios/sysdig/blob/dev/cla/sysdig_contributor_agreement.txt) and indicate agreement by adding the following line to every GIT commit message: + +``` +sysdig-CLA-1.0-signed-off-by: Joe Smith +``` -Sysdig is also packed with a set of scripts that make it easier to extract useful information and do troubleshooting. +Use your real name; pseudonyms or anonymous contributions are not allowed. -Documentation +**Corporate contributions**: Employees of corporations, members of LLCs or LLPs, or others acting on behalf of a contributing entity, must review the [Corporate Contributor License Agreement](https://github.com/draios/sysdig/blob/dev/cla/sysdig_corp_contributor_agreement.txt), must be an authorized representative of the contributing entity, and indicate agreement to it on behalf of the contributing entity by adding the following lines to every GIT commit message: + +``` +sysdig-CLA-1.0-contributing-entity: Full Legal Name of Entity +sysdig-CLA-1.0-signed-off-by: Joe Smith +``` + +Use a real name of a natural person who is an authorized representative of the contributing entity; pseudonyms or anonymous contributions are not allowed. + +**Government contributions**: Employees or officers of the United States Government, must review the [Corporate Contributor License Agreement](https://github.com/draios/sysdig/blob/dev/cla/sysdig_corp_contributor_agreement.txt), must be an authorized representative of the contributing entity, and indicate agreement to it on behalf of the contributing entity by adding the following lines to every GIT commit message: + +``` +sysdig-CLA-1.0-contributing-govt-entity: Full Legal Name of Entity +sysdig-CLA-1.0-signed-off-by: Joe Smith +This file is a work of authorship of an employee or officer of the United States Government and is not subject to copyright in the United States under 17 USC 105. +``` + +Use a real name of a natural person who is an authorized representative of the contributing entity; pseudonyms or anonymous contributions are not allowed. + +Commercial Support --- -Visit the wiki for full documentation on sysdig and its APIs: -https://github.com/draios/sysdig/wiki +Interested in a fully supported, fully distributed version of sysdig? Check out [Sysdig Monitor](https://sysdig.com/products/monitor/)! + +Open source sysdig is proudly supported by [Sysdig Inc](https://sysdig.com/company/). -Sysdig is developed by Draios, Inc. We'd love to hear from you! -www.draios.com / +Interested in what we're doing? [Sysdig is hiring](https://sysdig.com/jobs/). diff --git a/cla/sysdig_contributor_agreement.txt b/cla/sysdig_contributor_agreement.txt new file mode 100644 index 0000000000..430b806309 --- /dev/null +++ b/cla/sysdig_contributor_agreement.txt @@ -0,0 +1,30 @@ +DRAIOS, INC. – OPEN SOURCE CONTRIBUTION LICENSE AGREEMENT (“Agreement”) + +Draios, Inc. dba Sysdig (“Draios” or “Sysdig”) welcomes you to work on our open source software projects. In order to clarify the intellectual property license granted with Contributions from any person or entity, you must agree to the license terms below in order to contribute code back to our repositories. This license is for your protection as a Contributor as well as the protection of Sysdig; it does not change your rights to use your own Contributions for any other purpose. To indicate your Agreement, follow the procedure set forth below under TO AGREE, after reading this Agreement. + +You accept and agree to the following terms and conditions for Your present and future Contributions submitted to Draios/Sysdig. Except for the license granted herein to Draios/Sysdig and recipients of software distributed by Draios/Sysdig, You reserve all right, title, and interest in and to Your Contributions. + +1. Definitions. "You" (or "Your") shall mean the individual natural person and copyright owner who is making this Agreement with Draios/Sysdig. “You” excludes legal entities such as corporations, and Draios/Sysdig provides a separate CLA for corporations or other entities. "Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Draios/Sysdig for inclusion in, or documentation of, any of the products owned or managed by Draios/Sysdig (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Draios/Sysdig or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Draios/Sysdig for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution." + +2. Grant of Copyright License. Subject to the terms and conditions of this Agreement, You hereby grant to Draios/Sysdig and to recipients of software distributed by Draios/Sysdig a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works. + +3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Draios/Sysdig and to recipients of software distributed by Draios/Sysdig a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims that You have the right to license and that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity other than Draios/Sysdig institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed. + +4. You represent to Draios/Sysdig that You are legally entitled to grant the licenses set forth above. + +5. You represent that each of Your Contributions is Your original creation unless you act according to section 7 below. You represent that Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which You are personally aware and which are associated with any part of Your Contributions. You represent that Your sign-off indicating assent to this Agreement includes your real name and not a pseudonym, and that you shall not attempt or make an anonymous Contribution. + +6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions to Draios/Sysdig on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON- INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + +7. If You wish to submit work that is not Your original creation, You may submit it to Draios/Sysdig separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which you are personally aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]". + +8. You agree to notify Draios/Sysdig of any facts or circumstances of which you become aware that would make these representations inaccurate in any respect. + +9. You understand and agree that this project and Your Contribution are public and that a record of the contribution, including all personal information that I submit with it, including my sign-off, may be stored by Draios/Sysdig indefinitely and may be redistributed to others. You understand and agree that Draios/Sysdig has no obligation to use any Contribution in any Draios/Sysdig project or product, and Draios/Sysdig may decline to accept Your Contributions or Draios/Sysdig may remove Your Contributions from Draios/Sysdig projects or products at any time without notice. You understand and agree that Draios/Sysdig is not and will not pay you any form of compensation, in currency, equity or otherwise, in exchange for Your Contributions or for Your assent to this Agreement. You understand and agree that you are independent of Draios/Sysdig and you are not, by entering into this Agreement or providing Your Contributions, becoming employed, hired as an independent contractor, or forming any other relationship with Draios/Sysdig relating to employment, compensation or ownership or involving any fiduciary obligation. + +TO AGREE: +Add the following line to every GIT commit message: + +sysdig-CLA-1.0-signed-off-by: Joe Smith + +Use your real name; pseudonyms or anonymous contributions are not allowed. \ No newline at end of file diff --git a/cla/sysdig_corp_contributor_agreement.txt b/cla/sysdig_corp_contributor_agreement.txt new file mode 100644 index 0000000000..dc9fc6d551 --- /dev/null +++ b/cla/sysdig_corp_contributor_agreement.txt @@ -0,0 +1,33 @@ +DRAIOS, INC. – OPEN SOURCE CONTRIBUTION LICENSE AGREEMENT FOR CONTRIBUTING ENTITIES (SUCH AS CORPORATIONS) (“Agreement”) + +Draios, Inc. dba Sysdig (“Draios” or “Sysdig”) welcomes you to work on our open source software projects. In order to clarify the intellectual property license granted with Contributions from any person or entity, you must agree to the license terms below in order to contribute code back to our repositories. This license is for your protection as a Contributor as well as the protection of Sysdig; it does not change your rights to use your own Contributions for any other purpose. To indicate your Agreement, follow the procedure set forth below under TO AGREE, after reading this Agreement. + +A “contributing entity” means a corporation, limited liability company, partnership, or other entity that is organized and recognized under the laws of a state of the United States or another country (a “contributing entity”). We provide a separate CLA for individual contributors. + +You accept and agree to the following terms and conditions for Your present and future Contributions that are submitted to Draios/Sysdig. Except for the license granted herein to Draios/Sysdig and recipients of software distributed by Draios/Sysdig, You reserve all right, title, and interest in and to Your Contributions. + +1. Definitions. "You" (or "Your") shall mean the contributing entity that owns for copyright purposes or otherwise has the right to contribute the Contribution, and that is making this Agreement with Draios/Sysdig, and all other entities that control, are controlled by, or are under common control with the contributing entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Draios/Sysdig for inclusion in, or documentation of, any of the products owned or managed by Draios/Sysdig (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Draios/Sysdig or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Draios/Sysdig for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution." + +2. Grant of Copyright License. Subject to the terms and conditions of this Agreement, You hereby grant to Draios/Sysdig and to recipients of software distributed by Draios/Sysdig a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works. + +3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Draios/Sysdig and to recipients of software distributed by Draios/Sysdig a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims that You have the right to license and that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity other than Draios/Sysdig institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed. + +4. You represent to Draios/Sysdig that You own or have the right to contribute Your Contributions to Draios/Sysdig, and that You are legally entitled to grant the licenses set forth above. + +5. You represent that each of Your Contributions is Your original creation (see section 7 for submissions on behalf of others). You represent that Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which You are personally aware and which are associated with any part of Your Contributions. You represent that Your sign-off indicating assent to this Agreement includes the real name of a natural person who is an authorized representative of You, and not a pseudonym, and that You are not attempting or making an anonymous Contribution. + +6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions to Draios/Sysdig on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON- INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + +7. If You wish to submit work that is not Your original creation, You may submit it to Draios/Sysdig separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which You are aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]". + +8. You agree to notify Draios/Sysdig of any facts or circumstances of which you become aware that would make these representations inaccurate in any respect. + +9. You understand and agree that this project and Your Contribution are public and that a record of the contribution, including all personal information that You submit with it, including the sign-off of Your authorized representative, may be stored by Draios/Sysdig indefinitely and may be redistributed to others. You understand and agree that Draios/Sysdig has no obligation to use any Contribution in any Draios/Sysdig project or product, and Draios/Sysdig may decline to accept Your Contributions or Draios/Sysdig may remove Your Contributions from Draios/Sysdig projects or products at any time without notice. You understand and agree that Draios/Sysdig is not and will not pay You any form of compensation, in currency, equity or otherwise, in exchange for Your Contributions or for Your assent to this Agreement. You understand and agree that You are independent of Draios/Sysdig and You are not, by entering into this Agreement or providing Your Contributions, becoming employed, hired as an independent contractor, or forming any other relationship with Draios/Sysdig relating to employment, compensation or ownership or involving any fiduciary obligation. + +TO AGREE: +Add the following lines to every GIT commit message: + +sysdig-CLA-1.0-contributing-entity: Full Legal Name of Entity +sysdig-CLA-1.0-signed-off-by: Joe Smith + +Use a real name of a natural person who is an authorized representative of the contributing entity; pseudonyms or anonymous contributions are not allowed. diff --git a/cla/sysdig_govt_contributor_agreement.txt b/cla/sysdig_govt_contributor_agreement.txt new file mode 100644 index 0000000000..1bc557a70c --- /dev/null +++ b/cla/sysdig_govt_contributor_agreement.txt @@ -0,0 +1,33 @@ +DRAIOS, INC. � OPEN SOURCE CONTRIBUTION AGREEMENT FOR UNITED STATES GOVERNMENT CONTRIBUTING ENTITIES (�Agreement�) + +Draios, Inc. (�Draios� or �Sysdig�) welcomes the work of others on our open source software projects. To contribute code back to our repositories, we require a contributing entity that is a United States Government agency to complete, and agree to, the Government Contributor Agreement (GCA) set forth here, by and through a designated authorized representative. This agreement clarifies the ability for us to use and incorporate the contributions of a government contributing entity in our projects and products. After agreeing to these terms, a contributing entity may contribute to our projects. To indicate the agreement of the contributing entity, an authorized representative shall follow the procedure set forth below under TO AGREE, after reading this Agreement. A �contributing entity� means any agency or unit of the United States government. We provide a separate CLA for individual contributors. + +You accept and agree to the following terms and conditions for Your present and future Contributions that are submitted to Draios/Sysdig. + +1. Definitions. "You" (or "Your") shall mean the contributing entity that has authored or otherwise has the right to contribute the Contribution, and that is making this Agreement with Draios/Sysdig. "Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Draios/Sysdig for inclusion in, or documentation of, any of the products owned or managed by Draios/Sysdig (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Draios/Sysdig or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Draios/Sysdig for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution." + +2. Contributions Not Subject to Copyright. Each Contribution is a work authored by the United States Government or an employee or officer thereof and is not subject to copyright under 17 U.S.C. 105. + +3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Draios/Sysdig and to recipients of software distributed by Draios/Sysdig a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims that You have the right to license and that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity other than Draios/Sysdig institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed. + +4. You represent to Draios/Sysdig that You own or have the right to contribute Your Contributions to Draios/Sysdig, and that You are legally entitled to grant the license set forth above. + +5. You represent that each of Your Contributions is Your original creation (see section 7 for submissions on behalf of others). You represent that Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which You are personally aware and which are associated with any part of Your Contributions. You represent that Your sign-off indicating assent to this Agreement includes the real name of a natural person who is an authorized representative of You, and not a pseudonym, and that You are not attempting or making an anonymous Contribution. + +6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions to Draios/Sysdig on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON- INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. + +7. If You wish to submit work that is not Your original creation, You may submit it to Draios/Sysdig separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which You are aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]". + +8. You agree to notify Draios/Sysdig of any facts or circumstances of which you become aware that would make these representations inaccurate in any respect. + +9. You understand and agree that this project and Your Contribution are public and that a record of the contribution, including all personal information that You submit with it, including the sign-off of Your authorized representative, may be stored by Draios/Sysdig indefinitely and may be redistributed to others. You understand and agree that Draios/Sysdig has no obligation to use any Contribution in any Draios/Sysdig project or product, and Draios/Sysdig may decline to accept Your Contributions or Draios/Sysdig may remove Your Contributions from Draios/Sysdig projects or products at any time without notice. You understand and agree that Draios/Sysdig is not and will not pay You any form of compensation, in currency, equity or otherwise, in exchange for Your Contributions or for Your assent to this Agreement. You understand and agree that You are independent of Draios/Sysdig and You are not, by entering into this Agreement or providing Your Contributions, becoming employed, hired as an independent contractor, or forming any other relationship with Draios/Sysdig relating to employment, compensation or ownership or involving any fiduciary obligation. + +TO AGREE: +Add the following lines to every GIT commit message: + +sysdig-CLA-1.0-contributing-govt-entity: Full Legal Name of Entity +sysdig-CLA-1.0-signed-off-by: Joe Smith joe.smith@email.com +This file is a work of authorship of an employee or officer of the United States Government and is not subject to copyright in the United States under 17 USC 105. + +Use a real name of a natural person who is an authorized representative of the contributing entity; pseudonyms or anonymous contributions are not allowed. + diff --git a/cmake/modules/FindMakedev.cmake b/cmake/modules/FindMakedev.cmake new file mode 100644 index 0000000000..c2ccb5f3fe --- /dev/null +++ b/cmake/modules/FindMakedev.cmake @@ -0,0 +1,45 @@ +# +# Copyright (C) 2013-2019 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +# This module is used to understand where the makedev function +# is defined in the glibc in use. +# see 'man 3 makedev' +# Usage: +# In your CMakeLists.txt +# include(FindMakedev) +# +# In your source code: +# +# #if HAVE_SYS_MKDEV_H +# #include +# #endif +# #ifdef HAVE_SYS_SYSMACROS_H +# #include +# #endif +# +include(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake) + +check_include_file("sys/mkdev.h" HAVE_SYS_MKDEV_H) +check_include_file("sys/sysmacros.h" HAVE_SYS_SYSMACROS_H) + +if (HAVE_SYS_MKDEV_H) + add_definitions(-DHAVE_SYS_MKDEV_H) +endif() +if (HAVE_SYS_SYSMACROS_H) + add_definitions(-DHAVE_SYS_SYSMACROS_H) +endif() diff --git a/coding_conventions.md b/coding_conventions.md new file mode 100644 index 0000000000..eb068d177d --- /dev/null +++ b/coding_conventions.md @@ -0,0 +1,278 @@ +0 Introduction +------ + +Sysdig strives for a consistent high quality code base and uses the conventions +below. If you are going to commit code that doesn't follow them, then you put the +work on us. :-(. + +If you use vim or emacs, you can put a custom configuration file in the base +directory of sysdig in order to follow the conventions. + +Also, note that the conventions in this file apply **strictly to the userspace** part +of sysdig. For the kernel code, you should refer to +https://www.kernel.org/doc/html/latest/process/coding-style.html +and always run checkpatch.pl from the kernel tree before submitting pull requests. + +Thanks for your attention and time. + +1 Curly Braces +------ + +Every curly brace ("{" and "}") should go on its own line. + +Example: + + if(a == 0) + { + b = 1; + } + +2 If and for statements +------ + +Every `if` and `for` statement should have the curly braces. + +Example: + + if(a == 0) + { + b = 1; + } + +and not + + if(a == 0) + b = 1; + +3 Whitespace usage +------ + +Spaces are used in the following way: + + int32_t foo(int32_t a, int32_t b) + { + for(j = 0; j < 10; j++) + { + foo(a, b); + } + } + +Note that: + + * in a function declaration, there is no space between the function name and the "(". + * in a function declaration, there is no space between the "(" and the first parameter. + * in a statement (e.g `for`, `while`...), there is no space between the "for" and the "(". + * in a statement (e.g `for`), there is no space between the "(" and the variable name. + * in a function call, there is no space between the function name and the "(". + * in a function call, there is no space between the "(" and the first parameter. + * "," and ";" work like in English: there should be a space after them. + +4 Primitive types +------ + +For portability reasons, please use the standard C99 types instead of the native C types +like `int` and `long`. C99 types types will be available in all the user level sysdig +source files: + +Example: + + int32_t foo; + +5 Commenting Style +------ + +Comments should be in the C++ style so we can use `/* */` to quickly remove +portions of code during development. + +Example: + + // this is a comment + +6 Commenting Content +------ + +Code comments work in the following 2-level way: + + * A three-line comment should document what the code does and give higher level explanations. + * A one line comment should detail single code lines or explain specific actions. + +Example: + + // + // Swap two variables + // + int a = 1, b = 2, t; + + // make a copy of a + t = a; + + // perform the swap + a = b; + b = t; + +7 Class variables +------ + +In order to know whether a variable belongs to a `class` or a `function` we start member variables with "`m_`". + +Example: + + public int32_t m_counter; + +8 Global variables +------ + +Similarly, in order to know whether the variable is global or not, we start +globals with "`g_`". + +Example: + + int g_nplugins; + +9 Capitalization +------ + +The naming convention is camel-cased "Unix" style, i.e. always lower case. Words are separated by underscores. + +Example: + + int32_t g_global_bean_counter; + + int32_t count_beans(); + +and not, + + int32_t GlobalBeanCounter; + +10 Packed Structures +------- +Packed structures should use the GCC and MSVC-style supported `pragma`: + + #pragma pack(push,1) + struct frame_control + { + struct fields.... + }; + #pragma pack(pop) + +11 OS-specific macros +------- + +There's an online wiki which enumerates the different macros for compilers, operating systems, and architectures. +It's available at [http://sourceforge.net/p/predef/wiki/Home/](http://sourceforge.net/p/predef/wiki/Home/). Generally speaking we use the operating system page: [http://sourceforge.net/p/predef/wiki/OperatingSystems/](http://sourceforge.net/p/predef/wiki/OperatingSystems/). + +12 64-bit constants +------- + +Put an "LL" at the end of your 64 bit constants. Without the LL, on some platforms the compiler tries to interpret the constant on the right hand side +as a long integer instead of a long long and in some platform this generate an error at building time. + +Example: + + x=0X00FF00000000000LL + +13 Class Declaration +------- + +Class declarations follow the following sequence + + 1. constructors and destructor + 1. public functions + 1. public data + 1. private functions + 1. private data + 1. friend declarations + +Example: + + class foo + { + public: + foo(); + ~foo(); + + int32_t lonli(); + int32_t m_val; + + private: + int32_t temustra(); + int32_t m_val2; + }; + +14 Struct guidelines +------- + +We think hiding the presence of a pointer makes the code unnecessarily +ambiguous and more difficult. + +Seeing a * in a variable declaration immediately identifies a pointer, which +is easier to mentally keep track of! + +Also we think that defining the struct as a typedef makes forward declarations +clunky and find using the C++ style when declaring our structs makes our +lives easier. + + // + // Us human parsers find this confusing. + // + typedef struct _my_struct + { + u_int16 m_field; + } my_struct, + *p_my_struct; + + // + // This is easier! + // + struct my_struct { + u_int16 m_field; + }; + + +15 Temporary variables +------- + +Since "j" is used less frequently in english prose than "a" or "i", we find +that these variables (in hierarchical order) are great for counters: j, k, l, +m, n. + +Example: + + int32_t j,k; + for(j = 0; j < 10; j++) + { + for(k = 0; k < 10; k++) + { + int32_t foo = j + k; + } + } + +as opposed to: + + int32_t i,counter; + for(i = 0; i < 10; i++) + { + for(counter = 0; counter < 10; counter++) + { + int32_t foo = i + counter; + } + } + +16 Error management +------- + +Error management inside libscap is done through return values, since the scap +library is written in C. +Error management in the rest of the sysdig user level code base is done through +exceptions. We know there's a lot of debate between return values and +exceptions. We decided to pick the latter, so please stick with that. + +## You Made It! + +Phew! That's it. Thanks! + +If we've left anything in the open, feel free to contact us and we'll be happy +to get back to you. Also, you can look at the existing code and see how it's +done. + +Have a good one! diff --git a/conf/dev.conf b/conf/dev.conf new file mode 100644 index 0000000000..0e9503aed0 --- /dev/null +++ b/conf/dev.conf @@ -0,0 +1 @@ +export SYSDIG_VERSION=0.99.$BUILD_NUMBER diff --git a/docker/dev/Dockerfile b/docker/dev/Dockerfile new file mode 100644 index 0000000000..a22290d3df --- /dev/null +++ b/docker/dev/Dockerfile @@ -0,0 +1,106 @@ +FROM debian:stable + +MAINTAINER Sysdig + +ENV SYSDIG_REPOSITORY dev + +LABEL RUN="docker run -i -t -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --name NAME IMAGE" + +ENV SYSDIG_HOST_ROOT /host + +ENV HOME /root + +RUN cp /etc/skel/.bashrc /root && cp /etc/skel/.profile /root + +ADD http://download.draios.com/apt-draios-priority /etc/apt/preferences.d/ + +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get install -y --no-install-recommends \ + bash-completion \ + bc \ + clang-7 \ + curl \ + dkms \ + gnupg2 \ + ca-certificates \ + gcc \ + libc6-dev \ + libelf-dev \ + libelf1 \ + less \ + llvm-7 \ + procps \ + xz-utils \ + libmpx2 \ + && rm -rf /var/lib/apt/lists/* + +# gcc 6 is no longer included in debian unstable, but we need it to +# build kernel modules on the default debian-based ami used by +# kops. So grab copies we've saved from debian snapshots with the +# prefix https://snapshot.debian.org/archive/debian/20170517T033514Z +# or so. + +RUN curl -o cpp-6_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/cpp-6_6.3.0-18_amd64.deb \ + && curl -o gcc-6-base_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/gcc-6-base_6.3.0-18_amd64.deb \ + && curl -o gcc-6_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/gcc-6_6.3.0-18_amd64.deb \ + && curl -o libasan3_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libasan3_6.3.0-18_amd64.deb \ + && curl -o libcilkrts5_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libcilkrts5_6.3.0-18_amd64.deb \ + && curl -o libgcc-6-dev_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libgcc-6-dev_6.3.0-18_amd64.deb \ + && curl -o libubsan0_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libubsan0_6.3.0-18_amd64.deb \ + && curl -o libmpfr4_3.1.3-2_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libmpfr4_3.1.3-2_amd64.deb \ + && curl -o libisl15_0.18-1_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libisl15_0.18-1_amd64.deb \ + && dpkg -i cpp-6_6.3.0-18_amd64.deb gcc-6-base_6.3.0-18_amd64.deb gcc-6_6.3.0-18_amd64.deb libasan3_6.3.0-18_amd64.deb libcilkrts5_6.3.0-18_amd64.deb libgcc-6-dev_6.3.0-18_amd64.deb libubsan0_6.3.0-18_amd64.deb libmpfr4_3.1.3-2_amd64.deb libisl15_0.18-1_amd64.deb \ + && rm -f cpp-6_6.3.0-18_amd64.deb gcc-6-base_6.3.0-18_amd64.deb gcc-6_6.3.0-18_amd64.deb libasan3_6.3.0-18_amd64.deb libcilkrts5_6.3.0-18_amd64.deb libgcc-6-dev_6.3.0-18_amd64.deb libubsan0_6.3.0-18_amd64.deb libmpfr4_3.1.3-2_amd64.deb libisl15_0.18-1_amd64.deb + +# gcc 5 is no longer included in debian unstable, but we need it to +# build centos kernels, which are 3.x based and explicitly want a gcc +# version 3, 4, or 5 compiler. So grab copies we've saved from debian +# snapshots with the prefix https://snapshot.debian.org/archive/debian/20190122T000000Z. + +RUN curl -o cpp-5_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/cpp-5_5.5.0-12_amd64.deb \ + && curl -o gcc-5-base_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-5-base_5.5.0-12_amd64.deb \ + && curl -o gcc-5_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-5_5.5.0-12_amd64.deb \ + && curl -o libasan2_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libasan2_5.5.0-12_amd64.deb \ + && curl -o libgcc-5-dev_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libgcc-5-dev_5.5.0-12_amd64.deb \ + && curl -o libisl15_0.18-4_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libisl15_0.18-4_amd64.deb \ + && curl -o libmpx0_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libmpx0_5.5.0-12_amd64.deb \ + && dpkg -i cpp-5_5.5.0-12_amd64.deb gcc-5-base_5.5.0-12_amd64.deb gcc-5_5.5.0-12_amd64.deb libasan2_5.5.0-12_amd64.deb libgcc-5-dev_5.5.0-12_amd64.deb libisl15_0.18-4_amd64.deb libmpx0_5.5.0-12_amd64.deb \ + && rm -f cpp-5_5.5.0-12_amd64.deb gcc-5-base_5.5.0-12_amd64.deb gcc-5_5.5.0-12_amd64.deb libasan2_5.5.0-12_amd64.deb libgcc-5-dev_5.5.0-12_amd64.deb libisl15_0.18-4_amd64.deb libmpx0_5.5.0-12_amd64.deb + +# Since our base Debian image ships with GCC 7 which breaks older kernels, revert the +# default to gcc-5. +RUN rm -rf /usr/bin/gcc && ln -s /usr/bin/gcc-5 /usr/bin/gcc + +RUN rm -rf /usr/bin/clang \ + && rm -rf /usr/bin/llc \ + && ln -s /usr/bin/clang-7 /usr/bin/clang \ + && ln -s /usr/bin/llc-7 /usr/bin/llc + +RUN curl -s https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public | apt-key add - \ + && curl -s -o /etc/apt/sources.list.d/draios.list http://download.draios.com/$SYSDIG_REPOSITORY/deb/draios.list \ + && apt-get update \ + && apt-get install -y --no-install-recommends sysdig \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# Some base images have an empty /lib/modules by default +# If it's not empty, docker build will fail instead of +# silently overwriting the existing directory +RUN rm -df /lib/modules \ + && ln -s $SYSDIG_HOST_ROOT/lib/modules /lib/modules + +# debian:unstable head contains binutils 2.31, which generates +# binaries that are incompatible with kernels < 4.16. So manually +# forcibly install binutils 2.30-22 instead. +RUN curl -s -o binutils_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils_2.30-22_amd64.deb \ + && curl -s -o libbinutils_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/libbinutils_2.30-22_amd64.deb \ + && curl -s -o binutils-x86-64-linux-gnu_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils-x86-64-linux-gnu_2.30-22_amd64.deb \ + && curl -s -o binutils-common_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils-common_2.30-22_amd64.deb \ + && dpkg -i *binutils*.deb + +COPY ./docker-entrypoint.sh / + +ENTRYPOINT ["/docker-entrypoint.sh"] + +CMD ["bash"] diff --git a/docker/dev/docker-entrypoint.sh b/docker/dev/docker-entrypoint.sh new file mode 100755 index 0000000000..c176e0a9c0 --- /dev/null +++ b/docker/dev/docker-entrypoint.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +#set -e + +echo "* Setting up /usr/src links from host" + +for i in $(ls $SYSDIG_HOST_ROOT/usr/src) +do + ln -s $SYSDIG_HOST_ROOT/usr/src/$i /usr/src/$i +done + +/usr/bin/sysdig-probe-loader + +exec "$@" diff --git a/docker/ebpf-probe-builder/Dockerfile b/docker/ebpf-probe-builder/Dockerfile new file mode 100644 index 0000000000..1c84a670e0 --- /dev/null +++ b/docker/ebpf-probe-builder/Dockerfile @@ -0,0 +1,20 @@ +FROM debian:unstable + +MAINTAINER Sysdig + +# Based on the sysdig container, used for building eBPF probe + +RUN apt-get update \ + && apt-get dist-upgrade -y \ + && apt-get install -y --no-install-recommends \ + clang \ + gcc \ + libelf-dev \ + libelf1 \ + llvm \ + make \ + && rm -rf /var/lib/apt/lists/* + +COPY ./probe-builder-entrypoint.sh / + +ENTRYPOINT ["/probe-builder-entrypoint.sh"] diff --git a/docker/ebpf-probe-builder/build_bpf_probe.sh b/docker/ebpf-probe-builder/build_bpf_probe.sh new file mode 100755 index 0000000000..dfec5f39e9 --- /dev/null +++ b/docker/ebpf-probe-builder/build_bpf_probe.sh @@ -0,0 +1,54 @@ +#!/bin/sh + +set -eu + +# Defaults +DRIVER_DIR=/opt/draios/src/draios-agent-0.1.1dev +KERNEL_DIR=/lib/modules/$(uname -r)/build +OUT_DIR=${HOME}/.sysdig + +usage() +{ + echo "build_bpf_probe [-d ] [-k ] [-o ]" +} + +# Options parsing +while [ -n "${1-}" ]; do + case $1 in + -d | --driver ) shift + DRIVER_DIR=$1 + ;; + -k | --kernel ) shift + KERNEL_DIR=$1 + ;; + -o | --output ) shift + OUT_DIR=$1 + ;; + -h | --help ) usage + exit + ;; + * ) usage + exit 1 + ;; + esac + shift +done + +mkdir -p ${HOME}/.sysdig + +# +# Mapped volumes: +# - ${OUT_DIR}: The directory that the probe gets put in. Defaults to ~/.sysdig +# - ${DRIVER_DIR}: The prepared bpf driver code that gets written by the installer +# - ${KERNEL_DIR}: The kmod build directory for the target kernel. +# - /lib/modules: Unfortunately, on some distros (Debian / Ubuntu), there are +# additional support directories (such as a -common counterpart to -amd64) which +# need to be accessible for the makefile +# - /usr: As with the above, on Debian based systems the /lib/modules tree will have +# symlinks into /usr/lib/linux-kbuild* and these directories need to be present. + +docker build -t ebpf-probe-builder:latest --pull . +docker images -q -f 'dangling=true' | xargs --no-run-if-empty docker rmi -f +docker run --rm -i -v ${OUT_DIR}:/out -v ${DRIVER_DIR}:/driver -v ${KERNEL_DIR}:/kernel -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro -e BPF_PROBE_FILENAME=bpf_probe.o ebpf-probe-builder:latest + +echo "Probe is in ${OUT_DIR}/" diff --git a/docker/ebpf-probe-builder/probe-builder-entrypoint.sh b/docker/ebpf-probe-builder/probe-builder-entrypoint.sh new file mode 100755 index 0000000000..b360b2c96f --- /dev/null +++ b/docker/ebpf-probe-builder/probe-builder-entrypoint.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# +# Copyright (C) 2013-2019 Draios Inc dba Sysdig. +# +# This file is part of sysdig. +# +# 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. +# +# +# Simple script to build the BPF probe. Assumes that all the dependencies +# and requirements are already satisfied (as they are in the accompanying +# docker container) +# + +set -exu + +echo "* Building probe ${BPF_PROBE_FILENAME}" + +# On some distros, the modules dir links into /usr/src, so we need to make sure +# we have that sorted so we can build properly +for i in $(ls /host/usr/src); do + ln -s /host/usr/src/$i /usr/src/$i +done + +# Again, on some distros, we need to populate the /lib/modules directory +# because the kernel header info is split among several subdirs + +mkdir -p /lib/modules + +for i in $(ls /host/lib/modules); do + ln -s /host/lib/modules/$i /lib/modules/$i +done + +cd /driver/bpf +echo "Building bpf" +KERNELDIR=/kernel make + +echo "** Done building probe" +cp probe.o /out/${BPF_PROBE_FILENAME} diff --git a/docker/local/Dockerfile b/docker/local/Dockerfile new file mode 100644 index 0000000000..e79f09f3bd --- /dev/null +++ b/docker/local/Dockerfile @@ -0,0 +1,103 @@ +FROM debian:stable + +MAINTAINER Sysdig + +ENV SYSDIG_VERSION 0.1.1dev + +LABEL RUN="docker run -i -t -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --name NAME IMAGE" + +ENV SYSDIG_HOST_ROOT /host + +ENV HOME /root + +RUN cp /etc/skel/.bashrc /root && cp /etc/skel/.profile /root + +ADD http://download.draios.com/apt-draios-priority /etc/apt/preferences.d/ + +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get install -y --no-install-recommends \ + bash-completion \ + bc \ + clang-7 \ + curl \ + dkms \ + gnupg2 \ + ca-certificates \ + gcc \ + libc6-dev \ + libelf-dev \ + libelf1 \ + less \ + llvm-7 \ + procps \ + xz-utils \ + libmpx2 \ + && rm -rf /var/lib/apt/lists/* + +# gcc 6 is no longer included in debian unstable, but we need it to +# build kernel modules on the default debian-based ami used by +# kops. So grab copies we've saved from debian snapshots with the +# prefix https://snapshot.debian.org/archive/debian/20170517T033514Z +# or so. + +RUN curl -o cpp-6_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/cpp-6_6.3.0-18_amd64.deb \ + && curl -o gcc-6-base_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/gcc-6-base_6.3.0-18_amd64.deb \ + && curl -o gcc-6_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/gcc-6_6.3.0-18_amd64.deb \ + && curl -o libasan3_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libasan3_6.3.0-18_amd64.deb \ + && curl -o libcilkrts5_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libcilkrts5_6.3.0-18_amd64.deb \ + && curl -o libgcc-6-dev_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libgcc-6-dev_6.3.0-18_amd64.deb \ + && curl -o libubsan0_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libubsan0_6.3.0-18_amd64.deb \ + && curl -o libmpfr4_3.1.3-2_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libmpfr4_3.1.3-2_amd64.deb \ + && curl -o libisl15_0.18-1_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libisl15_0.18-1_amd64.deb \ + && dpkg -i cpp-6_6.3.0-18_amd64.deb gcc-6-base_6.3.0-18_amd64.deb gcc-6_6.3.0-18_amd64.deb libasan3_6.3.0-18_amd64.deb libcilkrts5_6.3.0-18_amd64.deb libgcc-6-dev_6.3.0-18_amd64.deb libubsan0_6.3.0-18_amd64.deb libmpfr4_3.1.3-2_amd64.deb libisl15_0.18-1_amd64.deb \ + && rm -f cpp-6_6.3.0-18_amd64.deb gcc-6-base_6.3.0-18_amd64.deb gcc-6_6.3.0-18_amd64.deb libasan3_6.3.0-18_amd64.deb libcilkrts5_6.3.0-18_amd64.deb libgcc-6-dev_6.3.0-18_amd64.deb libubsan0_6.3.0-18_amd64.deb libmpfr4_3.1.3-2_amd64.deb libisl15_0.18-1_amd64.deb + +# gcc 5 is no longer included in debian unstable, but we need it to +# build centos kernels, which are 3.x based and explicitly want a gcc +# version 3, 4, or 5 compiler. So grab copies we've saved from debian +# snapshots with the prefix https://snapshot.debian.org/archive/debian/20190122T000000Z. + +RUN curl -o cpp-5_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/cpp-5_5.5.0-12_amd64.deb \ + && curl -o gcc-5-base_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-5-base_5.5.0-12_amd64.deb \ + && curl -o gcc-5_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-5_5.5.0-12_amd64.deb \ + && curl -o libasan2_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libasan2_5.5.0-12_amd64.deb \ + && curl -o libgcc-5-dev_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libgcc-5-dev_5.5.0-12_amd64.deb \ + && curl -o libisl15_0.18-4_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libisl15_0.18-4_amd64.deb \ + && curl -o libmpx0_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libmpx0_5.5.0-12_amd64.deb \ + && dpkg -i cpp-5_5.5.0-12_amd64.deb gcc-5-base_5.5.0-12_amd64.deb gcc-5_5.5.0-12_amd64.deb libasan2_5.5.0-12_amd64.deb libgcc-5-dev_5.5.0-12_amd64.deb libisl15_0.18-4_amd64.deb libmpx0_5.5.0-12_amd64.deb \ + && rm -f cpp-5_5.5.0-12_amd64.deb gcc-5-base_5.5.0-12_amd64.deb gcc-5_5.5.0-12_amd64.deb libasan2_5.5.0-12_amd64.deb libgcc-5-dev_5.5.0-12_amd64.deb libisl15_0.18-4_amd64.deb libmpx0_5.5.0-12_amd64.deb + + +# Since our base Debian image ships with GCC 7 which breaks older kernels, revert the +# default to gcc-5. +RUN rm -rf /usr/bin/gcc && ln -s /usr/bin/gcc-5 /usr/bin/gcc + +RUN rm -rf /usr/bin/clang \ + && rm -rf /usr/bin/llc \ + && ln -s /usr/bin/clang-7 /usr/bin/clang \ + && ln -s /usr/bin/llc-7 /usr/bin/llc + +# Some base images have an empty /lib/modules by default +# If it's not empty, docker build will fail instead of +# silently overwriting the existing directory +RUN rm -df /lib/modules \ + && ln -s $SYSDIG_HOST_ROOT/lib/modules /lib/modules + +ADD sysdig-${SYSDIG_VERSION}-x86_64.deb / +RUN dpkg -i /sysdig-${SYSDIG_VERSION}-x86_64.deb + +# debian:unstable head contains binutils 2.31, which generates +# binaries that are incompatible with kernels < 4.16. So manually +# forcibly install binutils 2.30-22 instead. +RUN curl -s -o binutils_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils_2.30-22_amd64.deb \ + && curl -s -o libbinutils_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/libbinutils_2.30-22_amd64.deb \ + && curl -s -o binutils-x86-64-linux-gnu_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils-x86-64-linux-gnu_2.30-22_amd64.deb \ + && curl -s -o binutils-common_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils-common_2.30-22_amd64.deb \ + && dpkg -i *binutils*.deb + +COPY ./docker-entrypoint.sh / + +ENTRYPOINT ["/docker-entrypoint.sh"] + +CMD ["bash"] diff --git a/docker/local/docker-entrypoint.sh b/docker/local/docker-entrypoint.sh new file mode 100755 index 0000000000..7b0ea5a428 --- /dev/null +++ b/docker/local/docker-entrypoint.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +#set -e + +echo "* Setting up /usr/src links from host" + +for i in $(ls $SYSDIG_HOST_ROOT/usr/src) +do + ln -s $SYSDIG_HOST_ROOT/usr/src/$i /usr/src/$i +done + +/usr/bin/sysdig-probe-loader + +exec "$@" diff --git a/docker/stable/Dockerfile b/docker/stable/Dockerfile new file mode 100644 index 0000000000..177831e5f3 --- /dev/null +++ b/docker/stable/Dockerfile @@ -0,0 +1,107 @@ +FROM debian:stable + +MAINTAINER Sysdig + +ENV SYSDIG_REPOSITORY stable + +LABEL RUN="docker run -i -t -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --name NAME IMAGE" + +ENV SYSDIG_HOST_ROOT /host + +ENV HOME /root + +RUN cp /etc/skel/.bashrc /root && cp /etc/skel/.profile /root + +ADD http://download.draios.com/apt-draios-priority /etc/apt/preferences.d/ + +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get install -y --no-install-recommends \ + bash-completion \ + bc \ + clang-7 \ + curl \ + dkms \ + gnupg2 \ + ca-certificates \ + gcc \ + libc6-dev \ + libelf-dev \ + libelf1 \ + less \ + llvm-7 \ + procps \ + xz-utils \ + libmpx2 \ + && rm -rf /var/lib/apt/lists/* + +# gcc 6 is no longer included in debian unstable, but we need it to +# build kernel modules on the default debian-based ami used by +# kops. So grab copies we've saved from debian snapshots with the +# prefix https://snapshot.debian.org/archive/debian/20170517T033514Z +# or so. + +RUN curl -o cpp-6_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/cpp-6_6.3.0-18_amd64.deb \ + && curl -o gcc-6-base_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/gcc-6-base_6.3.0-18_amd64.deb \ + && curl -o gcc-6_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/gcc-6_6.3.0-18_amd64.deb \ + && curl -o libasan3_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libasan3_6.3.0-18_amd64.deb \ + && curl -o libcilkrts5_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libcilkrts5_6.3.0-18_amd64.deb \ + && curl -o libgcc-6-dev_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libgcc-6-dev_6.3.0-18_amd64.deb \ + && curl -o libubsan0_6.3.0-18_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libubsan0_6.3.0-18_amd64.deb \ + && curl -o libmpfr4_3.1.3-2_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libmpfr4_3.1.3-2_amd64.deb \ + && curl -o libisl15_0.18-1_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-6-debs/libisl15_0.18-1_amd64.deb \ + && dpkg -i cpp-6_6.3.0-18_amd64.deb gcc-6-base_6.3.0-18_amd64.deb gcc-6_6.3.0-18_amd64.deb libasan3_6.3.0-18_amd64.deb libcilkrts5_6.3.0-18_amd64.deb libgcc-6-dev_6.3.0-18_amd64.deb libubsan0_6.3.0-18_amd64.deb libmpfr4_3.1.3-2_amd64.deb libisl15_0.18-1_amd64.deb \ + && rm -f cpp-6_6.3.0-18_amd64.deb gcc-6-base_6.3.0-18_amd64.deb gcc-6_6.3.0-18_amd64.deb libasan3_6.3.0-18_amd64.deb libcilkrts5_6.3.0-18_amd64.deb libgcc-6-dev_6.3.0-18_amd64.deb libubsan0_6.3.0-18_amd64.deb libmpfr4_3.1.3-2_amd64.deb libisl15_0.18-1_amd64.deb + +# gcc 5 is no longer included in debian unstable, but we need it to +# build centos kernels, which are 3.x based and explicitly want a gcc +# version 3, 4, or 5 compiler. So grab copies we've saved from debian +# snapshots with the prefix https://snapshot.debian.org/archive/debian/20190122T000000Z. + +RUN curl -o cpp-5_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/cpp-5_5.5.0-12_amd64.deb \ + && curl -o gcc-5-base_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-5-base_5.5.0-12_amd64.deb \ + && curl -o gcc-5_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/gcc-5_5.5.0-12_amd64.deb \ + && curl -o libasan2_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libasan2_5.5.0-12_amd64.deb \ + && curl -o libgcc-5-dev_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libgcc-5-dev_5.5.0-12_amd64.deb \ + && curl -o libisl15_0.18-4_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libisl15_0.18-4_amd64.deb \ + && curl -o libmpx0_5.5.0-12_amd64.deb https://s3.amazonaws.com/download.draios.com/dependencies/libmpx0_5.5.0-12_amd64.deb \ + && dpkg -i cpp-5_5.5.0-12_amd64.deb gcc-5-base_5.5.0-12_amd64.deb gcc-5_5.5.0-12_amd64.deb libasan2_5.5.0-12_amd64.deb libgcc-5-dev_5.5.0-12_amd64.deb libisl15_0.18-4_amd64.deb libmpx0_5.5.0-12_amd64.deb \ + && rm -f cpp-5_5.5.0-12_amd64.deb gcc-5-base_5.5.0-12_amd64.deb gcc-5_5.5.0-12_amd64.deb libasan2_5.5.0-12_amd64.deb libgcc-5-dev_5.5.0-12_amd64.deb libisl15_0.18-4_amd64.deb libmpx0_5.5.0-12_amd64.deb + + +# Since our base Debian image ships with GCC 7 which breaks older kernels, revert the +# default to gcc-5. +RUN rm -rf /usr/bin/gcc && ln -s /usr/bin/gcc-5 /usr/bin/gcc + +RUN rm -rf /usr/bin/clang \ + && rm -rf /usr/bin/llc \ + && ln -s /usr/bin/clang-7 /usr/bin/clang \ + && ln -s /usr/bin/llc-7 /usr/bin/llc + +RUN curl -s https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public | apt-key add - \ + && curl -s -o /etc/apt/sources.list.d/draios.list http://download.draios.com/$SYSDIG_REPOSITORY/deb/draios.list \ + && apt-get update \ + && apt-get install -y --no-install-recommends sysdig \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# Some base images have an empty /lib/modules by default +# If it's not empty, docker build will fail instead of +# silently overwriting the existing directory +RUN rm -df /lib/modules \ + && ln -s $SYSDIG_HOST_ROOT/lib/modules /lib/modules + +# debian:unstable head contains binutils 2.31, which generates +# binaries that are incompatible with kernels < 4.16. So manually +# forcibly install binutils 2.30-22 instead. +RUN curl -s -o binutils_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils_2.30-22_amd64.deb \ + && curl -s -o libbinutils_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/libbinutils_2.30-22_amd64.deb \ + && curl -s -o binutils-x86-64-linux-gnu_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils-x86-64-linux-gnu_2.30-22_amd64.deb \ + && curl -s -o binutils-common_2.30-22_amd64.deb http://snapshot.debian.org/archive/debian/20180622T211149Z/pool/main/b/binutils/binutils-common_2.30-22_amd64.deb \ + && dpkg -i *binutils*.deb + +COPY ./docker-entrypoint.sh / + +ENTRYPOINT ["/docker-entrypoint.sh"] + +CMD ["bash"] diff --git a/docker/stable/docker-entrypoint.sh b/docker/stable/docker-entrypoint.sh new file mode 100755 index 0000000000..a93bae09e0 --- /dev/null +++ b/docker/stable/docker-entrypoint.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +#set -e + +echo "* Setting up /usr/src links from host" + +for i in $(ls $SYSDIG_HOST_ROOT/usr/src) +do + ln -s $SYSDIG_HOST_ROOT/usr/src/$i /usr/src/$i +done + +/usr/bin/sysdig-probe-loader + +exec "$@" diff --git a/driver/CMakeLists.txt b/driver/CMakeLists.txt index f0c32abc57..3761f1d572 100644 --- a/driver/CMakeLists.txt +++ b/driver/CMakeLists.txt @@ -1,29 +1,110 @@ -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(KBUILD_FLAGS "${SYSDIG_DEBUG_FLAGS} ${SYSDIG_FEATURE_FLAGS}") -else() - set(KBUILD_FLAGS "${SYSDIG_FEATURE_FLAGS}") -endif() +# +# Copyright (c) 2013-2018 Draios Inc. dba Sysdig. +# +# This file is dual licensed under either the MIT or GPL 2. See +# MIT.txt or GPL.txt for full copies of the license. +# + +option(BUILD_DRIVER "Build the driver on Linux" ON) +option(ENABLE_DKMS "Enable DKMS on Linux" ON) + +# The driver build process is somewhat involved because we use the same +# sources for building the driver locally and for shipping as a DKMS module. +# +# We need a single directory with the following files inside: +# - all the driver *.c/*.h sources +# - Makefile generated from Makefile.in +# - driver_config.h generated from driver_config.h.in +# +# The Makefile _must_ be called just Makefile (and not e.g. Makefile.dkms) +# because of the module build process, which looks like this: +# 1. The user (or some script) runs make in our driver directory +# 2. Our Makefile runs the Makefile from kernel sources/headers +# 3. The kernel Makefile calls our original Makefile again, with options that +# trigger the actual build. This step cannot know that our Makefile has +# a different name. +# +# (DKMS needs a Makefile called Makefile as well). +# +# The files need to be in a single directory because we cannot know where +# the sources will be built (especially by DKMS) so we cannot put _any_ paths +# in the Makefile. +# +# The chosen directory must not be ${CMAKE_CURRENT_BINARY_DIR} because CMake +# puts its own generated Makefile in there, so we (arbitrarily) choose +# ${CMAKE_CURRENT_BINARY_DIR}/src. To maintain compatibility with older versions, +# after the build we copy the compiled module one directory up, +# to ${CMAKE_CURRENT_BINARY_DIR}. + +configure_file(dkms.conf.in src/dkms.conf) +configure_file(Makefile.in src/Makefile) +configure_file(driver_config.h.in src/driver_config.h) -add_custom_target(driver ALL - COMMAND sed -i 's/^PACKAGE_VERSION=.*/PACKAGE_VERSION="${SYSDIG_VERSION}"/g' dkms.conf - COMMAND sed -i 's/^DKMS_VERSION=.*/DKMS_VERSION="${SYSDIG_VERSION}"/g' ../scripts/debian/postinst - COMMAND sed -i 's/^DKMS_VERSION=.*/DKMS_VERSION="${SYSDIG_VERSION}"/g' ../scripts/debian/prerm - COMMAND sed -i 's/^ccflags-y := .*/ccflags-y := ${KBUILD_FLAGS}/g' Makefile - COMMAND make - COMMAND cp -f sysdig-probe.ko ${CMAKE_CURRENT_BINARY_DIR} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -install(FILES - Makefile - dkms.conf +set(DRIVER_SOURCES + dynamic_params_table.c event_table.c + fillers_table.c flags_table.c + kernel_hacks.h main.c ppm.h ppm_events.c ppm_events.h ppm_events_public.h ppm_fillers.c + ppm_fillers.h + ppm_flag_helpers.h ppm_ringbuffer.h + ppm_syscall.h syscall_table.c - DESTINATION "src/sysdig-${SYSDIG_VERSION}") + ppm_cputime.c + ppm_compat_unistd_32.h + ppm_version.h +) + +foreach(FILENAME IN LISTS DRIVER_SOURCES) + configure_file(${FILENAME} src/${FILENAME} COPYONLY) +endforeach() + +# make can be self-referenced as $(MAKE) only from Makefiles but this +# triggers syntax errors with other generators such as Ninja +if(${CMAKE_GENERATOR} STREQUAL "Unix Makefiles") + set(MAKE_COMMAND "$(MAKE)") +else() + set(MAKE_COMMAND "make") +endif() + +# This if/else is needed because you currently cannot manipulate dependencies +# of built-in targets like "all" in CMake: +# http://public.kitware.com/Bug/view.php?id=8438 +if(BUILD_DRIVER) + add_custom_target(driver ALL + COMMAND ${MAKE_COMMAND} + COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${PROBE_NAME}.ko "${CMAKE_CURRENT_BINARY_DIR}" + WORKING_DIRECTORY src + VERBATIM) +else() + add_custom_target(driver + COMMAND ${MAKE_COMMAND} + COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${PROBE_NAME}.ko "${CMAKE_CURRENT_BINARY_DIR}" + WORKING_DIRECTORY src + VERBATIM) +endif() + +add_custom_target(install_driver + COMMAND ${MAKE_COMMAND} install + DEPENDS driver + WORKING_DIRECTORY src + VERBATIM) + +if(ENABLE_DKMS) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/src/Makefile + ${CMAKE_CURRENT_BINARY_DIR}/src/dkms.conf + ${CMAKE_CURRENT_BINARY_DIR}/src/driver_config.h + ${DRIVER_SOURCES} + DESTINATION "src/${PACKAGE_NAME}-${PROBE_VERSION}" + COMPONENT agent-kmodule) + +endif() + +add_subdirectory(bpf) diff --git a/driver/GPL2.txt b/driver/GPL2.txt new file mode 100644 index 0000000000..d159169d10 --- /dev/null +++ b/driver/GPL2.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/driver/MIT.txt b/driver/MIT.txt new file mode 100644 index 0000000000..b5438fe822 --- /dev/null +++ b/driver/MIT.txt @@ -0,0 +1,20 @@ +Copyright (c) 2013-2018 Draios Inc. dba Sysdig + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/driver/Makefile b/driver/Makefile deleted file mode 100644 index f70884f3cf..0000000000 --- a/driver/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -sysdig-probe-y += main.o flags_table.o ppm_events.o ppm_fillers.o event_table.o syscall_table.o -obj-m += sysdig-probe.o -ccflags-y := - -KERNELDIR ?= /lib/modules/$(shell uname -r)/build - -TOP := $(shell pwd) -all: - make -C $(KERNELDIR) M=$(TOP) modules - -clean: - make -C $(KERNELDIR) M=$(TOP) clean - -install: all - make -C $(KERNELDIR) M=$(TOP) modules_install diff --git a/driver/Makefile.in b/driver/Makefile.in new file mode 100644 index 0000000000..42f004766e --- /dev/null +++ b/driver/Makefile.in @@ -0,0 +1,22 @@ +# +# Copyright (c) 2013-2018 Draios Inc. dba Sysdig. +# +# This file is dual licensed under either the MIT or GPL 2. See +# MIT.txt or GPL.txt for full copies of the license. +# + +@PROBE_NAME@-y += main.o dynamic_params_table.o fillers_table.o flags_table.o ppm_events.o ppm_fillers.o event_table.o syscall_table.o ppm_cputime.o +obj-m += @PROBE_NAME@.o +ccflags-y := @KBUILD_FLAGS@ + +KERNELDIR ?= /lib/modules/$(shell uname -r)/build + +TOP := $(shell pwd) +all: + $(MAKE) -C $(KERNELDIR) M=$(TOP) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(TOP) clean + +install: all + $(MAKE) -C $(KERNELDIR) M=$(TOP) modules_install diff --git a/driver/bpf/.gitignore b/driver/bpf/.gitignore new file mode 100644 index 0000000000..b03c23b294 --- /dev/null +++ b/driver/bpf/.gitignore @@ -0,0 +1 @@ +built-in.a diff --git a/driver/bpf/CMakeLists.txt b/driver/bpf/CMakeLists.txt new file mode 100644 index 0000000000..9c85ed2826 --- /dev/null +++ b/driver/bpf/CMakeLists.txt @@ -0,0 +1,32 @@ +# +# Copyright (c) 2013-2018 Draios Inc. dba Sysdig. +# +# This file is dual licensed under either the MIT or GPL 2. See +# MIT.txt or GPL.txt for full copies of the license. +# + +configure_file(../driver_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/../driver_config.h) + +option(BUILD_BPF "Build the BPF driver on Linux" OFF) + +if(BUILD_BPF) + add_custom_target(bpf ALL + COMMAND make + COMMAND "${CMAKE_COMMAND}" -E copy_if_different probe.o "${CMAKE_CURRENT_BINARY_DIR}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + VERBATIM) +endif() + +install(FILES + bpf_helpers.h + filler_helpers.h + fillers.h + Makefile + maps.h + plumbing_helpers.h + probe.c + quirks.h + ring_helpers.h + types.h + DESTINATION "src/${PACKAGE_NAME}-${PROBE_VERSION}/bpf" + COMPONENT agent-kmodule) diff --git a/driver/bpf/Makefile b/driver/bpf/Makefile new file mode 100644 index 0000000000..f87886b89c --- /dev/null +++ b/driver/bpf/Makefile @@ -0,0 +1,45 @@ +# +# Copyright (c) 2013-2018 Draios Inc. dba Sysdig. +# +# This file is dual licensed under either the MIT or GPL 2. See +# MIT.txt or GPL.txt for full copies of the license. +# + +always += probe.o + +LLC ?= llc +CLANG ?= clang + +KERNELDIR ?= /lib/modules/$(shell uname -r)/build + +# DEBUG = -DBPF_DEBUG + +all: + $(MAKE) -C $(KERNELDIR) M=$$PWD + +clean: + $(MAKE) -C $(KERNELDIR) M=$$PWD clean + @rm -f *~ + +$(obj)/probe.o: $(src)/probe.c \ + $(src)/bpf_helpers.h \ + $(src)/filler_helpers.h \ + $(src)/fillers.h \ + $(src)/maps.h \ + $(src)/plumbing_helpers.h \ + $(src)/quirks.h \ + $(src)/ring_helpers.h \ + $(src)/types.h + $(CLANG) $(LINUXINCLUDE) \ + $(KBUILD_CPPFLAGS) \ + $(KBUILD_EXTRA_CPPFLAGS) \ + $(DEBUG) \ + -D__KERNEL__ \ + -D__BPF_TRACING__ \ + -Wno-gnu-variable-sized-type-not-at-end \ + -Wno-address-of-packed-member \ + -fno-jump-tables \ + -fno-stack-protector \ + -Wno-tautological-compare \ + -O2 -g -emit-llvm -c $< -o $(patsubst %.o,%.ll,$@) + $(LLC) -march=bpf -filetype=obj -o $@ $(patsubst %.o,%.ll,$@) diff --git a/driver/bpf/bpf_helpers.h b/driver/bpf/bpf_helpers.h new file mode 100644 index 0000000000..a9078d3960 --- /dev/null +++ b/driver/bpf/bpf_helpers.h @@ -0,0 +1,80 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __BPF_HELPERS_H +#define __BPF_HELPERS_H + +static void *(*bpf_map_lookup_elem)(void *map, void *key) = + (void *)BPF_FUNC_map_lookup_elem; +static int (*bpf_map_update_elem)(void *map, void *key, void *value, + unsigned long long flags) = + (void *)BPF_FUNC_map_update_elem; +static int (*bpf_map_delete_elem)(void *map, void *key) = + (void *)BPF_FUNC_map_delete_elem; +static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) = + (void *)BPF_FUNC_probe_read; +static unsigned long long (*bpf_ktime_get_ns)(void) = + (void *)BPF_FUNC_ktime_get_ns; +static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = + (void *)BPF_FUNC_trace_printk; +static void (*bpf_tail_call)(void *ctx, void *map, int index) = + (void *)BPF_FUNC_tail_call; +static unsigned long long (*bpf_get_smp_processor_id)(void) = + (void *)BPF_FUNC_get_smp_processor_id; +static unsigned long long (*bpf_get_current_pid_tgid)(void) = + (void *)BPF_FUNC_get_current_pid_tgid; +static unsigned long long (*bpf_get_current_uid_gid)(void) = + (void *)BPF_FUNC_get_current_uid_gid; +static int (*bpf_get_current_comm)(void *buf, int buf_size) = + (void *)BPF_FUNC_get_current_comm; +static int (*bpf_perf_event_read)(void *map, int index) = + (void *)BPF_FUNC_perf_event_read; +static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) = + (void *)BPF_FUNC_clone_redirect; +static int (*bpf_redirect)(int ifindex, int flags) = + (void *)BPF_FUNC_redirect; +static int (*bpf_perf_event_output)(void *ctx, void *map, + unsigned long long flags, void *data, + int size) = + (void *)BPF_FUNC_perf_event_output; +static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = + (void *)BPF_FUNC_get_stackid; +static int (*bpf_probe_write_user)(void *dst, void *src, int size) = + (void *)BPF_FUNC_probe_write_user; +static int (*bpf_current_task_under_cgroup)(void *map, int index) = + (void *)BPF_FUNC_current_task_under_cgroup; +static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) = + (void *)BPF_FUNC_skb_get_tunnel_key; +static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) = + (void *)BPF_FUNC_skb_set_tunnel_key; +static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) = + (void *)BPF_FUNC_skb_get_tunnel_opt; +static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) = + (void *)BPF_FUNC_skb_set_tunnel_opt; +static unsigned long long (*bpf_get_prandom_u32)(void) = + (void *)BPF_FUNC_get_prandom_u32; +static int (*bpf_xdp_adjust_head)(void *ctx, int offset) = + (void *)BPF_FUNC_xdp_adjust_head; +static int (*bpf_probe_read_str)(void *dst, u64 size, const void *unsafe_ptr) = + (void *)BPF_FUNC_probe_read_str; +static u64 (*bpf_get_current_task)(void) = + (void *)BPF_FUNC_get_current_task; +static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) = + (void *)BPF_FUNC_skb_load_bytes; +static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = + (void *)BPF_FUNC_skb_store_bytes; +static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) = + (void *)BPF_FUNC_l3_csum_replace; +static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) = + (void *)BPF_FUNC_l4_csum_replace; +static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) = + (void *)BPF_FUNC_skb_under_cgroup; +static int (*bpf_skb_change_head)(void *, int len, int flags) = + (void *)BPF_FUNC_skb_change_head; + +#endif diff --git a/driver/bpf/filler_helpers.h b/driver/bpf/filler_helpers.h new file mode 100644 index 0000000000..bdcf0652ad --- /dev/null +++ b/driver/bpf/filler_helpers.h @@ -0,0 +1,957 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __SYSDIGBPF_HELPERS_H +#define __SYSDIGBPF_HELPERS_H + +#include +#include +#include +#include +#include +#include +#include + +#include "../ppm_flag_helpers.h" + +static __always_inline bool in_port_range(uint16_t port, uint16_t min, uint16_t max) +{ + return port >= min && port <= max; +} + +static __always_inline struct file *bpf_fget(int fd) +{ + struct task_struct *task; + struct files_struct *files; + struct fdtable *fdt; + int max_fds; + struct file **fds; + struct file *fil; + + task = (struct task_struct *)bpf_get_current_task(); + if (!task) + return NULL; + + files = _READ(task->files); + if (!files) + return NULL; + + fdt = _READ(files->fdt); + if (!fdt) + return NULL; + + max_fds = _READ(fdt->max_fds); + if (fd >= max_fds) + return NULL; + + fds = _READ(fdt->fd); + fil = _READ(fds[fd]); + + return fil; +} + +static __always_inline struct socket *bpf_sockfd_lookup(struct filler_data *data, + int fd) +{ + struct file *file; + const struct file_operations *fop; + struct socket *sock; + + if (!data->settings->socket_file_ops) + return NULL; + + file = bpf_fget(fd); + if (!file) + return NULL; + + fop = _READ(file->f_op); + if (fop != data->settings->socket_file_ops) + return NULL; + + sock = _READ(file->private_data); + return sock; +} + +static __always_inline unsigned long bpf_encode_dev(dev_t dev) +{ + unsigned int major = MAJOR(dev); + unsigned int minor = MINOR(dev); + + return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12); +} + +static __always_inline bool bpf_get_fd_dev_ino(int fd, unsigned long *dev, unsigned long *ino) +{ + struct super_block *sb; + struct inode *inode; + struct file *file; + dev_t kdev; + + file = bpf_fget(fd); + if (!file) + return false; + + inode = _READ(file->f_inode); + if (!inode) + return false; + + sb = _READ(inode->i_sb); + if (!sb) + return false; + + kdev = _READ(sb->s_dev); + *dev = bpf_encode_dev(kdev); + + *ino = _READ(inode->i_ino); + + return true; +} + +static __always_inline bool bpf_ipv6_addr_any(const struct in6_addr *a) +{ + const unsigned long *ul = (const unsigned long *)a; + + return (ul[0] | ul[1]) == 0UL; +} + +static __always_inline bool bpf_getsockname(struct socket *sock, + struct sockaddr_storage *addr, + int peer) +{ + struct sock *sk; + sa_family_t family; + + sk = _READ(sock->sk); + if (!sk) + return false; + + family = _READ(sk->sk_family); + + switch (family) { + case AF_INET: + { + struct inet_sock *inet = (struct inet_sock *)sk; + struct sockaddr_in *sin = (struct sockaddr_in *)addr; + + sin->sin_family = AF_INET; + if (peer) { + sin->sin_port = _READ(inet->inet_dport); + sin->sin_addr.s_addr = _READ(inet->inet_daddr); + } else { + u32 addr = _READ(inet->inet_rcv_saddr); + + if (!addr) + addr = _READ(inet->inet_saddr); + sin->sin_port = _READ(inet->inet_sport); + sin->sin_addr.s_addr = addr; + } + + break; + } + case AF_INET6: + { + struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr; + struct inet_sock *inet = (struct inet_sock *)sk; + struct ipv6_pinfo { + struct in6_addr saddr; + }; + struct ipv6_pinfo *np = (struct ipv6_pinfo *)_READ(inet->pinet6); + + sin->sin6_family = AF_INET6; + if (peer) { + sin->sin6_port = _READ(inet->inet_dport); + sin->sin6_addr = _READ(sk->sk_v6_daddr); + } else { + sin->sin6_addr = _READ(sk->sk_v6_rcv_saddr); + if (bpf_ipv6_addr_any(&sin->sin6_addr)) + sin->sin6_addr = _READ(np->saddr); + sin->sin6_port = _READ(inet->inet_sport); + } + + break; + } + case AF_UNIX: + { + struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr; + struct unix_sock *u; + struct unix_address *addr; + + if (peer) + sk = _READ(((struct unix_sock *)sk)->peer); + + u = (struct unix_sock *)sk; + addr = _READ(u->addr); + if (!addr) { + sunaddr->sun_family = AF_UNIX; + sunaddr->sun_path[0] = 0; + } else { + unsigned int len = _READ(addr->len); + + if (len > sizeof(struct sockaddr_storage)) + len = sizeof(struct sockaddr_storage); + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (len > 0) + bpf_probe_read(sunaddr, ((len - 1) & 0xff) + 1, addr->name); +#else + bpf_probe_read(sunaddr, len, addr->name); +#endif + } + + break; + } + default: + return false; + } + + return true; +} + +static __always_inline int bpf_addr_to_kernel(void *uaddr, int ulen, + struct sockaddr *kaddr) +{ + if (ulen < 0 || ulen > sizeof(struct sockaddr_storage)) + return -EINVAL; + + if (ulen == 0) + return 0; + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (bpf_probe_read(kaddr, ((ulen - 1) & 0xff) + 1, uaddr)) +#else + if (bpf_probe_read(kaddr, ulen & 0xff, uaddr)) +#endif + return -EFAULT; + + return 0; +} + +#define get_buf(x) data->buf[(data->state->tail_ctx.curoff + (x)) & SCRATCH_SIZE_HALF] + +static __always_inline u32 bpf_compute_snaplen(struct filler_data *data, + u32 lookahead_size) +{ + struct sockaddr_storage *sock_address; + struct sockaddr_storage *peer_address; + u32 res = data->settings->snaplen; + struct socket *sock; + struct sock *sk; + u16 sport; + u16 dport; + + if (data->settings->tracers_enabled && + data->state->tail_ctx.evt_type == PPME_SYSCALL_WRITE_X) { + struct file *fil; + struct inode *f_inode; + dev_t i_rdev; + + fil = bpf_fget(data->fd); + if (!fil) + return res; + + f_inode = _READ(fil->f_inode); + if (!f_inode) + return res; + + i_rdev = _READ(f_inode->i_rdev); + if (i_rdev == PPM_NULL_RDEV) + return RW_SNAPLEN_EVENT; + } + + if (!data->settings->do_dynamic_snaplen) + return res; + + if (data->fd == -1) + return res; + + sock = bpf_sockfd_lookup(data, data->fd); + if (!sock) + return res; + + sock_address = (struct sockaddr_storage *)data->tmp_scratch; + peer_address = (struct sockaddr_storage *)data->tmp_scratch + 1; + + if (!bpf_getsockname(sock, sock_address, 0)) + return res; + + if (data->state->tail_ctx.evt_type == PPME_SOCKET_SENDTO_X) { + unsigned long val; + struct sockaddr *usrsockaddr; + + usrsockaddr = (struct sockaddr *)bpf_syscall_get_argument(data, 4); + + if (!usrsockaddr) { + if (!bpf_getsockname(sock, peer_address, 1)) + return res; + } else { + int addrlen = bpf_syscall_get_argument(data, 5); + + if (addrlen != 0) { + if (bpf_addr_to_kernel(usrsockaddr, addrlen, (struct sockaddr *)peer_address)) + return res; + } else if (!bpf_getsockname(sock, peer_address, 1)) { + return res; + } + } + } else if (data->state->tail_ctx.evt_type == PPME_SOCKET_SENDMSG_X) { + struct sockaddr *usrsockaddr; + struct user_msghdr mh; + unsigned long val; + int addrlen; + + val = bpf_syscall_get_argument(data, 1); + if (bpf_probe_read(&mh, sizeof(mh), (void *)val)) { + usrsockaddr = NULL; + addrlen = 0; + } else { + usrsockaddr = (struct sockaddr *)mh.msg_name; + addrlen = mh.msg_namelen; + } + + if (usrsockaddr && addrlen != 0) { + if (bpf_addr_to_kernel(usrsockaddr, addrlen, (struct sockaddr *)peer_address)) + return res; + } else if (!bpf_getsockname(sock, peer_address, 1)) { + return res; + } + } else if (!bpf_getsockname(sock, peer_address, 1)) { + return res; + } + + sk = _READ(sock->sk); + if (!sk) + return res; + + sa_family_t family = _READ(sk->sk_family); + + if (family == AF_INET) { + sport = ntohs(((struct sockaddr_in *)sock_address)->sin_port); + dport = ntohs(((struct sockaddr_in *)peer_address)->sin_port); + } else if (family == AF_INET6) { + sport = ntohs(((struct sockaddr_in6 *)sock_address)->sin6_port); + dport = ntohs(((struct sockaddr_in6 *)peer_address)->sin6_port); + } else { + sport = 0; + dport = 0; + } + + uint16_t min_port = data->settings->fullcapture_port_range_start; + uint16_t max_port = data->settings->fullcapture_port_range_end; + + if (max_port > 0 && + (in_port_range(sport, min_port, max_port) || + in_port_range(dport, min_port, max_port))) { + /* + * Before checking the well-known ports, see if the user has requested + * an increased snaplen for the port in question. + */ + return RW_MAX_FULLCAPTURE_PORT_SNAPLEN; + } else if (sport == PPM_PORT_MYSQL || dport == PPM_PORT_MYSQL) { + if (lookahead_size >= 5) { + if (get_buf(0) == 3 || + get_buf(1) == 3 || + get_buf(2) == 3 || + get_buf(3) == 3 || + get_buf(4) == 3) { + return 2000; + } else if (get_buf(2) == 0 && get_buf(3) == 0) { + return 2000; + } + } + } else if (sport == PPM_PORT_POSTGRES || dport == PPM_PORT_POSTGRES) { + if (lookahead_size >= 2) { + if ((get_buf(0) == 'Q' && get_buf(1) == 0) || /* SimpleQuery command */ + (get_buf(0) == 'P' && get_buf(1) == 0) || /* Prepare statement command */ + (get_buf(4) == 0 && get_buf(5) == 3 && get_buf(6) == 0) || /* startup command */ + (get_buf(0) == 'E' && get_buf(1) == 0) /* error or execute command */ + ) { + return 2000; + } + } + } else if ((lookahead_size >= 4 && get_buf(1) == 0 && get_buf(2) == 0 && get_buf(2) == 0) || /* matches command */ + (lookahead_size >= 16 && (*(s32 *)&get_buf(12) == 1 || /* matches header */ + *(s32 *)&get_buf(12) == 2001 || + *(s32 *)&get_buf(12) == 2002 || + *(s32 *)&get_buf(12) == 2003 || + *(s32 *)&get_buf(12) == 2004 || + *(s32 *)&get_buf(12) == 2005 || + *(s32 *)&get_buf(12) == 2006 || + *(s32 *)&get_buf(12) == 2007))) { + return 2000; + } else if (dport == data->settings->statsd_port) { + return 2000; + } else { + if (lookahead_size >= 5) { + u32 buf = *(u32 *)&get_buf(0); + + if (buf == 0x20544547 || // "GET " + buf == 0x54534F50 || // "POST" + buf == 0x20545550 || // "PUT " + buf == 0x454C4544 || // "DELE" + buf == 0x43415254 || // "TRAC" + buf == 0x4E4E4F43 || // "CONN" + buf == 0x4954504F || // "OPTI" + (buf == 0x50545448 && data->buf[(data->state->tail_ctx.curoff + 4) & SCRATCH_SIZE_HALF] == '/')) { // "HTTP/" + return 2000; + } + } + } + + return res; +} + +static __always_inline u16 bpf_pack_addr(struct filler_data *data, + struct sockaddr *usrsockaddr, + int ulen) +{ + u32 ip; + u16 port; + sa_family_t family = usrsockaddr->sa_family; + struct sockaddr_in *usrsockaddr_in; + struct sockaddr_in6 *usrsockaddr_in6; + struct sockaddr_un *usrsockaddr_un; + u16 size; + char *dest; + int res; + + switch (family) { + case AF_INET: + /* + * Map the user-provided address to a sockaddr_in + */ + usrsockaddr_in = (struct sockaddr_in *)usrsockaddr; + + /* + * Retrieve the src address + */ + ip = usrsockaddr_in->sin_addr.s_addr; + port = ntohs(usrsockaddr_in->sin_port); + + /* + * Pack the tuple info in the temporary buffer + */ + size = 1 + 4 + 2; /* family + ip + port */ + + data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF] = socket_family_to_scap(family); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], &ip, 4); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 5) & SCRATCH_SIZE_HALF], &port, 2); + + break; + case AF_INET6: + /* + * Map the user-provided address to a sockaddr_in + */ + usrsockaddr_in6 = (struct sockaddr_in6 *)usrsockaddr; + + /* + * Retrieve the src address + */ + port = ntohs(usrsockaddr_in6->sin6_port); + + /* + * Pack the tuple info in the temporary buffer + */ + size = 1 + 16 + 2; /* family + ip + port */ + + data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF] = socket_family_to_scap(family); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], + usrsockaddr_in6->sin6_addr.s6_addr, 16); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 17) & SCRATCH_SIZE_HALF], &port, 2); + + break; + case AF_UNIX: + /* + * Map the user-provided address to a sockaddr_in + */ + usrsockaddr_un = (struct sockaddr_un *)usrsockaddr; + + /* + * Put a 0 at the end of struct sockaddr_un because + * the user might not have considered it in the length + */ + if (ulen == sizeof(struct sockaddr_storage)) + ((char *)usrsockaddr_un)[(ulen - 1) & SCRATCH_SIZE_MAX] = 0; + else + ((char *)usrsockaddr_un)[ulen & SCRATCH_SIZE_MAX] = 0; + + /* + * Pack the data into the target buffer + */ + size = 1; + + data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF] = socket_family_to_scap(family); + + res = bpf_probe_read_str(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], + UNIX_PATH_MAX, + usrsockaddr_un->sun_path); + + size += res; + + break; + default: + size = 0; + break; + } + + return size; +} + +static __always_inline long bpf_fd_to_socktuple(struct filler_data *data, + int fd, + struct sockaddr *usrsockaddr, + int ulen, + bool use_userdata, + bool is_inbound, + char *tmp_area) +{ + struct sockaddr_storage *sock_address; + struct sockaddr_storage *peer_address; + unsigned short family; + struct socket *sock; + struct sock *sk; + long size = 0; + + sock = bpf_sockfd_lookup(data, fd); + if (!sock) + return 0; + + sock_address = (struct sockaddr_storage *)tmp_area; + peer_address = (struct sockaddr_storage *)tmp_area + 1; + + if (!bpf_getsockname(sock, sock_address, 0)) + return 0; + + sk = _READ(sock->sk); + if (!sk) + return 0; + + family = _READ(sk->sk_family); + + switch (family) { + case AF_INET: + { + u32 sip; + u32 dip; + u16 sport; + u16 dport; + + if (!use_userdata) { + if (bpf_getsockname(sock, peer_address, 1)) { + if (is_inbound) { + sip = ((struct sockaddr_in *)peer_address)->sin_addr.s_addr; + sport = ntohs(((struct sockaddr_in *)peer_address)->sin_port); + dip = ((struct sockaddr_in *)sock_address)->sin_addr.s_addr; + dport = ntohs(((struct sockaddr_in *)sock_address)->sin_port); + } else { + sip = ((struct sockaddr_in *)sock_address)->sin_addr.s_addr; + sport = ntohs(((struct sockaddr_in *)sock_address)->sin_port); + dip = ((struct sockaddr_in *)peer_address)->sin_addr.s_addr; + dport = ntohs(((struct sockaddr_in *)peer_address)->sin_port); + } + } else { + sip = 0; + sport = 0; + dip = 0; + dport = 0; + } + } else { + struct sockaddr_in *usrsockaddr_in = (struct sockaddr_in *)usrsockaddr; + + if (is_inbound) { + sip = usrsockaddr_in->sin_addr.s_addr; + sport = ntohs(usrsockaddr_in->sin_port); + dip = ((struct sockaddr_in *)sock_address)->sin_addr.s_addr; + dport = ntohs(((struct sockaddr_in *)sock_address)->sin_port); + } else { + sip = ((struct sockaddr_in *)sock_address)->sin_addr.s_addr; + sport = ntohs(((struct sockaddr_in *)sock_address)->sin_port); + dip = usrsockaddr_in->sin_addr.s_addr; + dport = ntohs(usrsockaddr_in->sin_port); + } + } + + size = 1 + 4 + 4 + 2 + 2; + + data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF] = socket_family_to_scap(family); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], &sip, 4); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 5) & SCRATCH_SIZE_HALF], &sport, 2); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 7) & SCRATCH_SIZE_HALF], &dip, 4); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 11) & SCRATCH_SIZE_HALF], &dport, 2); + + break; + } + case AF_INET6: + { + u8 *sip6; + u8 *dip6; + u16 sport; + u16 dport; + + if (!use_userdata) { + if (bpf_getsockname(sock, peer_address, 1)) { + if (is_inbound) { + sip6 = ((struct sockaddr_in6 *)peer_address)->sin6_addr.s6_addr; + sport = ntohs(((struct sockaddr_in6 *)peer_address)->sin6_port); + dip6 = ((struct sockaddr_in6 *)sock_address)->sin6_addr.s6_addr; + dport = ntohs(((struct sockaddr_in6 *)sock_address)->sin6_port); + } else { + sip6 = ((struct sockaddr_in6 *)sock_address)->sin6_addr.s6_addr; + sport = ntohs(((struct sockaddr_in6 *)sock_address)->sin6_port); + dip6 = ((struct sockaddr_in6 *)peer_address)->sin6_addr.s6_addr; + dport = ntohs(((struct sockaddr_in6 *)peer_address)->sin6_port); + } + } else { + memset(peer_address, 0, 16); + sip6 = (u8 *)peer_address; + dip6 = (u8 *)peer_address; + sport = 0; + dport = 0; + } + } else { + /* + * Map the user-provided address to a sockaddr_in6 + */ + struct sockaddr_in6 *usrsockaddr_in6 = (struct sockaddr_in6 *)usrsockaddr; + + if (is_inbound) { + sip6 = usrsockaddr_in6->sin6_addr.s6_addr; + sport = ntohs(usrsockaddr_in6->sin6_port); + dip6 = ((struct sockaddr_in6 *)sock_address)->sin6_addr.s6_addr; + dport = ntohs(((struct sockaddr_in6 *)sock_address)->sin6_port); + } else { + sip6 = ((struct sockaddr_in6 *)sock_address)->sin6_addr.s6_addr; + sport = ntohs(((struct sockaddr_in6 *)sock_address)->sin6_port); + dip6 = usrsockaddr_in6->sin6_addr.s6_addr; + dport = ntohs(usrsockaddr_in6->sin6_port); + } + } + + /* + * Pack the tuple info in the temporary buffer + */ + size = 1 + 16 + 16 + 2 + 2; /* family + sip + dip + sport + dport */ + + data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF] = socket_family_to_scap(family); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], sip6, 16); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 17) & SCRATCH_SIZE_HALF], &sport, 2); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 19) & SCRATCH_SIZE_HALF], dip6, 16); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 35) & SCRATCH_SIZE_HALF], &dport, 2); + + break; + } + case AF_UNIX: + { + /* + * Retrieve the addresses + */ + struct unix_sock *us = (struct unix_sock *)sk; + struct sock *speer = _READ(us->peer); + char *us_name; + + data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF] = socket_family_to_scap(family); + + if (is_inbound) { + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], &us, 8); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1 + 8) & SCRATCH_SIZE_HALF], &speer, 8); + } else { + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], &speer, 8); + memcpy(&data->buf[(data->state->tail_ctx.curoff + 1 + 8) & SCRATCH_SIZE_HALF], &us, 8); + } + + /* + * Pack the data into the target buffer + */ + size = 1 + 8 + 8; + + if (!use_userdata) { + if (is_inbound) { + us_name = ((struct sockaddr_un *)sock_address)->sun_path; + } else { + bpf_getsockname(sock, peer_address, 1); + us_name = ((struct sockaddr_un *)peer_address)->sun_path; + } + } else { + /* + * Map the user-provided address to a sockaddr_in + */ + struct sockaddr_un *usrsockaddr_un = (struct sockaddr_un *)usrsockaddr; + + /* + * Put a 0 at the end of struct sockaddr_un because + * the user might not have considered it in the length + */ + if (ulen == sizeof(struct sockaddr_storage)) + ((char *)usrsockaddr_un)[(ulen - 1) & SCRATCH_SIZE_MAX] = 0; + else + ((char *)usrsockaddr_un)[ulen & SCRATCH_SIZE_MAX] = 0; + + if (is_inbound) + us_name = ((struct sockaddr_un *)sock_address)->sun_path; + else + us_name = usrsockaddr_un->sun_path; + } + + int res = bpf_probe_read_str(&data->buf[(data->state->tail_ctx.curoff + 1 + 8 + 8) & SCRATCH_SIZE_HALF], + UNIX_PATH_MAX, + us_name); + + size += res; + + break; + } + } + + return size; +} + +static __always_inline int __bpf_val_to_ring(struct filler_data *data, + unsigned long val, + unsigned long val_len, + enum ppm_param_type type, + u8 dyn_idx, + bool enforce_snaplen) +{ + unsigned int len_dyn = 0; + unsigned int len; + + if (data->state->tail_ctx.curoff > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + if (dyn_idx != (u8)-1) { + *((u8 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = dyn_idx; + len_dyn = sizeof(u8); + data->state->tail_ctx.curoff += len_dyn; + data->state->tail_ctx.len += len_dyn; + } + + if (data->state->tail_ctx.curoff > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + switch (type) { + case PT_CHARBUF: + case PT_FSPATH: + case PT_FSRELPATH: { + if (!data->curarg_already_on_frame) { + int res; + + res = bpf_probe_read_str(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + PPM_MAX_ARG_SIZE, + (const void *)val); + if (res == -EFAULT) + return PPM_FAILURE_INVALID_USER_MEMORY; + len = res; + } else { + len = val_len; + } + break; + } + case PT_BYTEBUF: { + if (val_len) { + len = val_len; + + if (enforce_snaplen) { + u32 dpi_lookahead_size = DPI_LOOKAHEAD_SIZE; + unsigned int sl; + + if (dpi_lookahead_size > len) + dpi_lookahead_size = len; + + if (!data->curarg_already_on_frame) { + volatile unsigned long read_size = dpi_lookahead_size; + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (read_size) + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + ((read_size - 1) & SCRATCH_SIZE_HALF) + 1, + (void *)val)) +#else + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + read_size & SCRATCH_SIZE_HALF, + (void *)val)) +#endif + return PPM_FAILURE_INVALID_USER_MEMORY; + } + + sl = bpf_compute_snaplen(data, dpi_lookahead_size); + + if (len > sl) + len = sl; + } + + if (len > PPM_MAX_ARG_SIZE) + len = PPM_MAX_ARG_SIZE; + + if (!data->curarg_already_on_frame) { + volatile unsigned long read_size = len; + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (read_size) + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + ((read_size - 1) & SCRATCH_SIZE_HALF) + 1, + (void *)val)) +#else + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + read_size & SCRATCH_SIZE_HALF, + (void *)val)) +#endif + return PPM_FAILURE_INVALID_USER_MEMORY; + } + } else { + len = 0; + } + break; + } + case PT_SOCKADDR: + case PT_SOCKTUPLE: + case PT_FDLIST: + if (!data->curarg_already_on_frame) { + bpf_printk("expected arg already on frame: evt_type %d, curarg %d, type %d\n", + data->state->tail_ctx.evt_type, + data->state->tail_ctx.curarg, type); + return PPM_FAILURE_BUG; + } + + len = val_len; + break; + case PT_FLAGS8: + case PT_UINT8: + case PT_SIGTYPE: + *((u8 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(u8); + break; + case PT_FLAGS16: + case PT_UINT16: + case PT_SYSCALLID: + *((u16 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(u16); + break; + case PT_FLAGS32: + case PT_MODE: + case PT_UINT32: + case PT_UID: + case PT_GID: + case PT_SIGSET: + *((u32 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(u32); + break; + case PT_RELTIME: + case PT_ABSTIME: + case PT_UINT64: + *((u64 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(u64); + break; + case PT_INT8: + *((s8 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(s8); + break; + case PT_INT16: + *((s16 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(s16); + break; + case PT_INT32: + *((s32 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(s32); + break; + case PT_INT64: + case PT_ERRNO: + case PT_FD: + case PT_PID: + *((s64 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val; + len = sizeof(s64); + break; + default: { + bpf_printk("unhandled type in bpf_val_to_ring: evt_type %d, curarg %d, type %d\n", + data->state->tail_ctx.evt_type, + data->state->tail_ctx.curarg, type); + return PPM_FAILURE_BUG; + } + } + + if (len_dyn + len > PPM_MAX_ARG_SIZE) + return PPM_FAILURE_BUFFER_FULL; + + fixup_evt_arg_len(data->buf, data->state->tail_ctx.curarg, len_dyn + len); + data->state->tail_ctx.curoff += len; + data->state->tail_ctx.len += len; + data->curarg_already_on_frame = false; + ++data->state->tail_ctx.curarg; + + return PPM_SUCCESS; +} + +static __always_inline int bpf_val_to_ring(struct filler_data *data, + unsigned long val) +{ + const struct ppm_param_info *param_info; + + if (data->state->tail_ctx.curarg >= PPM_MAX_EVENT_PARAMS) { + bpf_printk("invalid curarg: %d\n", data->state->tail_ctx.curarg); + return PPM_FAILURE_BUG; + } + + param_info = &data->evt->params[data->state->tail_ctx.curarg & (PPM_MAX_EVENT_PARAMS - 1)]; + + return __bpf_val_to_ring(data, val, 0, param_info->type, -1, false); +} + +static __always_inline int bpf_val_to_ring_len(struct filler_data *data, + unsigned long val, + unsigned long val_len) +{ + const struct ppm_param_info *param_info; + + if (data->state->tail_ctx.curarg >= PPM_MAX_EVENT_PARAMS) { + bpf_printk("invalid curarg: %d\n", data->state->tail_ctx.curarg); + return PPM_FAILURE_BUG; + } + + param_info = &data->evt->params[data->state->tail_ctx.curarg & (PPM_MAX_EVENT_PARAMS - 1)]; + + return __bpf_val_to_ring(data, val, val_len, param_info->type, -1, false); +} + +static __always_inline int bpf_val_to_ring_dyn(struct filler_data *data, + unsigned long val, + enum ppm_param_type type, + u8 dyn_idx) +{ + return __bpf_val_to_ring(data, val, 0, type, dyn_idx, false); +} + +static __always_inline int bpf_val_to_ring_type(struct filler_data *data, + unsigned long val, + enum ppm_param_type type) +{ + return __bpf_val_to_ring(data, val, 0, type, -1, false); +} + +static __always_inline bool bpf_in_ia32_syscall() +{ +#ifdef __ppc64__ + return 0; +#else + struct task_struct *task; + u32 status; + + task = (struct task_struct *)bpf_get_current_task(); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 18) + status = _READ(task->thread.status); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0) + status = _READ(task->thread_info.status); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 2) + status = _READ(task->thread.status); +#else + status = _READ(task->thread_info.status); +#endif + + return status & TS_COMPAT; +#endif // #ifdef __ppc64__ +} + +#endif diff --git a/driver/bpf/fillers.h b/driver/bpf/fillers.h new file mode 100644 index 0000000000..7a483bef76 --- /dev/null +++ b/driver/bpf/fillers.h @@ -0,0 +1,4380 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __FILLERS_H +#define __FILLERS_H + +/* + * https://chromium.googlesource.com/chromiumos/third_party/kernel/+/096925a44076ba5c52faa84d255a847130ff341e%5E%21/#F2 + * This commit diverged the ChromiumOS kernel from stock in the area of audit + * information, which this probe accesses. + * + * If running on a patched version of COS, enable this #define to get the + * probe to build. + */ +//#define COS_73_WORKAROUND + +#include "../ppm_flag_helpers.h" +#include "../ppm_version.h" + +#include +#include + + +/* + * Linux 5.6 kernels no longer include the old 32-bit timeval + * structures. But the syscalls (might) still use them. + */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0) +#include +struct compat_timespec { + int32_t tv_sec; + int32_t tv_nsec; +}; + +struct timespec { + int32_t tv_sec; + int32_t tv_nsec; +}; + +struct timeval { + int32_t tv_sec; + int32_t tv_usec; +}; +#else +#define timeval64 timeval +#endif + +#define FILLER_RAW(x) \ +static __always_inline int __bpf_##x(struct filler_data *data); \ + \ +__bpf_section(TP_NAME "filler/" #x) \ +static __always_inline int bpf_##x(void *ctx) \ + +#define FILLER(x, is_syscall) \ +static __always_inline int __bpf_##x(struct filler_data *data); \ + \ +__bpf_section(TP_NAME "filler/" #x) \ +static __always_inline int bpf_##x(void *ctx) \ +{ \ + struct filler_data data; \ + int res; \ + \ + res = init_filler_data(ctx, &data, is_syscall); \ + if (res == PPM_SUCCESS) { \ + if (!data.state->tail_ctx.len) \ + write_evt_hdr(&data); \ + res = __bpf_##x(&data); \ + } \ + \ + if (res == PPM_SUCCESS) \ + res = push_evt_frame(ctx, &data); \ + \ + if (data.state) \ + data.state->tail_ctx.prev_res = res; \ + \ + bpf_tail_call(ctx, &tail_map, PPM_FILLER_terminate_filler); \ + bpf_printk("Can't tail call terminate filler\n"); \ + return 0; \ +} \ + \ +static __always_inline int __bpf_##x(struct filler_data *data) \ + +FILLER_RAW(terminate_filler) +{ + struct sysdig_bpf_per_cpu_state *state; + + state = get_local_state(bpf_get_smp_processor_id()); + if (!state) + return 0; + + switch (state->tail_ctx.prev_res) { + case PPM_SUCCESS: + break; + case PPM_FAILURE_BUFFER_FULL: + bpf_printk("PPM_FAILURE_BUFFER_FULL event=%d curarg=%d\n", + state->tail_ctx.evt_type, + state->tail_ctx.curarg); + ++state->n_drops_buffer; + break; + case PPM_FAILURE_INVALID_USER_MEMORY: + bpf_printk("PPM_FAILURE_INVALID_USER_MEMORY event=%d curarg=%d\n", + state->tail_ctx.evt_type, + state->tail_ctx.curarg); + ++state->n_drops_pf; + break; + case PPM_FAILURE_BUG: + bpf_printk("PPM_FAILURE_BUG event=%d curarg=%d\n", + state->tail_ctx.evt_type, + state->tail_ctx.curarg); + ++state->n_drops_bug; + break; + case PPM_SKIP_EVENT: + break; + default: + bpf_printk("Unknown filler res=%d event=%d curarg=%d\n", + state->tail_ctx.prev_res, + state->tail_ctx.evt_type, + state->tail_ctx.curarg); + break; + } + + release_local_state(state); + return 0; +} + +FILLER(sys_empty, true) +{ + return PPM_SUCCESS; +} + +FILLER(sys_single, true) +{ + unsigned long val; + int res; + + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_single_x, true) +{ + int res; + long retval; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + + return res; +} + +FILLER(sys_open_x, true) +{ + unsigned int flags; + unsigned int mode; + unsigned long val; + unsigned long dev; + unsigned long ino; + long retval; + int res; + + /* + * fd + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * Name + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * Flags + */ + val = bpf_syscall_get_argument(data, 1); + flags = open_flags_to_scap(val); + res = bpf_val_to_ring(data, flags); + if (res != PPM_SUCCESS) + return res; + + /* + * Mode + */ + mode = bpf_syscall_get_argument(data, 2); + mode = open_modes_to_scap(val, mode); + res = bpf_val_to_ring(data, mode); + if (res != PPM_SUCCESS) + return res; + + /* + * Device + */ + if (retval < 0 || !bpf_get_fd_dev_ino(retval, &dev, &ino)) + dev = 0; + + res = bpf_val_to_ring(data, dev); + return res; +} + +FILLER(sys_read_x, true) +{ + unsigned long bufsize; + unsigned long val; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + if (retval < 0) { + val = 0; + bufsize = 0; + } else { + val = bpf_syscall_get_argument(data, 1); + bufsize = retval; + } + + /* + * data + */ + data->fd = bpf_syscall_get_argument(data, 0); + res = __bpf_val_to_ring(data, val, bufsize, PT_BYTEBUF, -1, true); + + return res; +} + +FILLER(sys_write_x, true) +{ + unsigned long bufsize; + unsigned long val; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * data + */ + data->fd = bpf_syscall_get_argument(data, 0); + + val = bpf_syscall_get_argument(data, 1); + bufsize = bpf_syscall_get_argument(data, 2); + + res = __bpf_val_to_ring(data, val, bufsize, PT_BYTEBUF, -1, true); + + return res; +} + +#define POLL_MAXFDS 16 + +static __always_inline int bpf_poll_parse_fds(struct filler_data *data, + bool enter_event) +{ + unsigned int read_size; + unsigned int fds_count; + int res = PPM_SUCCESS; + unsigned long nfds; + struct pollfd *fds; + unsigned long val; + unsigned long off; + int j; + + nfds = bpf_syscall_get_argument(data, 1); + fds = (struct pollfd *)data->tmp_scratch; + read_size = nfds * sizeof(struct pollfd); + if (read_size > SCRATCH_SIZE_MAX) + return PPM_FAILURE_BUFFER_FULL; + + val = bpf_syscall_get_argument(data, 0); +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (read_size) + if (bpf_probe_read(fds, + ((read_size - 1) & SCRATCH_SIZE_MAX) + 1, + (void *)val)) +#else + if (bpf_probe_read(fds, read_size & SCRATCH_SIZE_MAX, (void *)val)) +#endif + return PPM_FAILURE_INVALID_USER_MEMORY; + + off = data->state->tail_ctx.curoff + sizeof(u16); + fds_count = 0; + + #pragma unroll + for (j = 0; j < POLL_MAXFDS; ++j) { + u16 flags; + + if (j == nfds) + break; + + if (enter_event) { + flags = poll_events_to_scap(fds[j].events); + } else { + if (!fds[j].revents) + continue; + + flags = poll_events_to_scap(fds[j].revents); + } + + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + *(s64 *)&data->buf[off & SCRATCH_SIZE_HALF] = fds[j].fd; + off += sizeof(s64); + + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + *(s16 *)&data->buf[off & SCRATCH_SIZE_HALF] = flags; + off += sizeof(s16); + ++fds_count; + } + + *((u16 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = fds_count; + data->curarg_already_on_frame = true; + return __bpf_val_to_ring(data, 0, off - data->state->tail_ctx.curoff, PT_FDLIST, -1, false); +} + +FILLER(sys_poll_e, true) +{ + unsigned long val; + int res; + + /* + * fds + */ + res = bpf_poll_parse_fds(data, true); + if (res != PPM_SUCCESS) + return res; + + /* + * timeout + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_poll_x, true) +{ + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * fds + */ + res = bpf_poll_parse_fds(data, false); + + return res; +} + +#define MAX_IOVCNT 32 + +static __always_inline int bpf_parse_readv_writev_bufs(struct filler_data *data, + const struct iovec __user *iovsrc, + unsigned long iovcnt, + long retval, + int flags) +{ + const struct iovec *iov; + int res = PPM_SUCCESS; + unsigned int copylen; + long size = 0; + int j; + + copylen = iovcnt * sizeof(struct iovec); + iov = (const struct iovec *)data->tmp_scratch; + + if (copylen > SCRATCH_SIZE_MAX) + return PPM_FAILURE_BUFFER_FULL; + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (copylen) + if (bpf_probe_read((void *)iov, + ((copylen - 1) & SCRATCH_SIZE_MAX) + 1, + (void *)iovsrc)) +#else + if (bpf_probe_read((void *)iov, + copylen & SCRATCH_SIZE_MAX, + (void *)iovsrc)) +#endif + return PPM_FAILURE_INVALID_USER_MEMORY; + + #pragma unroll + for (j = 0; j < MAX_IOVCNT; ++j) { + if (j == iovcnt) + break; + + size += iov[j].iov_len; + } + + if ((flags & PRB_FLAG_IS_WRITE) == 0) + if (size > retval) + size = retval; + + if (flags & PRB_FLAG_PUSH_SIZE) { + res = bpf_val_to_ring_type(data, size, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + } + + if (flags & PRB_FLAG_PUSH_DATA) { + if (size > 0) { + unsigned long off = data->state->tail_ctx.curoff; + unsigned long remaining = size; + int j; + + #pragma unroll + for (j = 0; j < MAX_IOVCNT; ++j) { + volatile unsigned int to_read; + + if (j == iovcnt) + break; + + if (off > SCRATCH_SIZE_HALF) + break; + + if (iov[j].iov_len <= remaining) + to_read = iov[j].iov_len; + else + to_read = remaining; + + if (to_read > SCRATCH_SIZE_HALF) + to_read = SCRATCH_SIZE_HALF; + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (to_read) + if (bpf_probe_read(&data->buf[off & SCRATCH_SIZE_HALF], + ((to_read - 1) & SCRATCH_SIZE_HALF) + 1, + iov[j].iov_base)) +#else + if (bpf_probe_read(&data->buf[off & SCRATCH_SIZE_HALF], + to_read & SCRATCH_SIZE_HALF, + iov[j].iov_base)) +#endif + return PPM_FAILURE_INVALID_USER_MEMORY; + + remaining -= to_read; + off += to_read; + } + } else { + size = 0; + } + + data->fd = bpf_syscall_get_argument(data, 0); + data->curarg_already_on_frame = true; + return __bpf_val_to_ring(data, 0, size, PT_BYTEBUF, -1, true); + } + + return res; +} + +FILLER(sys_readv_preadv_x, true) +{ + const struct iovec __user *iov; + unsigned long iovcnt; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + iov = (const struct iovec __user *)bpf_syscall_get_argument(data, 1); + iovcnt = bpf_syscall_get_argument(data, 2); + + res = bpf_parse_readv_writev_bufs(data, + iov, + iovcnt, + retval, + PRB_FLAG_PUSH_ALL); + + return res; +} + +FILLER(sys_writev_e, true) +{ + unsigned long iovcnt; + unsigned long val; + int res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + val = bpf_syscall_get_argument(data, 1); + iovcnt = bpf_syscall_get_argument(data, 2); + res = bpf_parse_readv_writev_bufs(data, + (const struct iovec __user *)val, + iovcnt, + 0, + PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); + + return res; +} + +FILLER(sys_writev_pwritev_x, true) +{ + unsigned long iovcnt; + unsigned long val; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * data and size + */ + val = bpf_syscall_get_argument(data, 1); + iovcnt = bpf_syscall_get_argument(data, 2); + res = bpf_parse_readv_writev_bufs(data, + (const struct iovec __user *)val, + iovcnt, + 0, + PRB_FLAG_PUSH_DATA | PRB_FLAG_IS_WRITE); + + return res; +} + +static __always_inline int timespec_parse(struct filler_data *data, + unsigned long val) +{ + u64 longtime; + struct timespec ts; + + if (bpf_probe_read(&ts, sizeof(ts), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + longtime = ((u64)ts.tv_sec) * 1000000000 + ts.tv_nsec; + + return bpf_val_to_ring_type(data, longtime, PT_RELTIME); +} + +FILLER(sys_nanosleep_e, true) +{ + unsigned long val; + int res; + + val = bpf_syscall_get_argument(data, 0); + res = timespec_parse(data, val); + + return res; +} + +FILLER(sys_futex_e, true) +{ + unsigned long val; + int res; + + /* + * addr + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * op + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, futex_op_to_scap(val)); + if (res != PPM_SUCCESS) + return res; + + /* + * val + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, val); + + return res; +} + +static __always_inline unsigned long bpf_get_mm_counter(struct mm_struct *mm, + int member) +{ + long val; + + bpf_probe_read(&val, sizeof(val), &mm->rss_stat.count[member]); + if (val < 0) + val = 0; + + return (unsigned long)val; +} + +static __always_inline unsigned long bpf_get_mm_rss(struct mm_struct *mm) +{ + return bpf_get_mm_counter(mm, MM_FILEPAGES) + + bpf_get_mm_counter(mm, MM_ANONPAGES) + + bpf_get_mm_counter(mm, MM_SHMEMPAGES); +} + +static __always_inline unsigned long bpf_get_mm_swap(struct mm_struct *mm) +{ + return bpf_get_mm_counter(mm, MM_SWAPENTS); +} + +FILLER(sys_brk_munmap_mmap_x, true) +{ + struct task_struct *task; + unsigned long total_vm = 0; + struct mm_struct *mm; + long total_rss = 0; + long swap = 0; + long retval; + int res; + + task = (struct task_struct *)bpf_get_current_task(); + mm = NULL; + bpf_probe_read(&mm, sizeof(mm), &task->mm); + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + if (mm) { + total_vm = _READ(mm->total_vm); + total_vm <<= (PAGE_SHIFT - 10); + total_rss = bpf_get_mm_rss(mm) << (PAGE_SHIFT - 10); + swap = bpf_get_mm_swap(mm) << (PAGE_SHIFT - 10); + } + + /* + * vm_size + */ + res = bpf_val_to_ring_type(data, total_vm, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * vm_rss + */ + res = bpf_val_to_ring_type(data, total_rss, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * vm_swap + */ + res = bpf_val_to_ring_type(data, swap, PT_UINT32); + + return res; +} + +FILLER(sys_mmap_e, true) +{ + unsigned long val; + int res; + + /* + * addr + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * length + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * prot + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, prot_flags_to_scap(val)); + if (res != PPM_SUCCESS) + return res; + + /* + * flags + */ + val = bpf_syscall_get_argument(data, 3); + res = bpf_val_to_ring(data, mmap_flags_to_scap(val)); + if (res != PPM_SUCCESS) + return res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 4); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * offset/pgoffset + */ + val = bpf_syscall_get_argument(data, 5); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_fcntl_e, true) +{ + unsigned long val; + long cmd; + int res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring_type(data, val, PT_FD); + if (res != PPM_SUCCESS) + return res; + + /* + * cmd + */ + val = bpf_syscall_get_argument(data, 1); + cmd = fcntl_cmd_to_scap(val); + res = bpf_val_to_ring_type(data, cmd, PT_FLAGS8); + + return res; +} + +FILLER(sys_access_e, true) +{ + unsigned long val; + int res; + + /* + * mode + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, access_flags_to_scap(val)); + + return res; +} + +FILLER(sys_getrlimit_setrlimit_e, true) +{ + unsigned long val; + int res; + + /* + * resource + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring_type(data, rlimit_resource_to_scap(val), PT_FLAGS8); + + return res; +} + +FILLER(sys_getrlimit_setrlrimit_x, true) +{ + unsigned long val; + long retval; + s64 cur; + s64 max; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * Copy the user structure and extract cur and max + */ + if (retval >= 0 || + data->state->tail_ctx.evt_type == PPME_SYSCALL_SETRLIMIT_X) { + struct rlimit rl; + + val = bpf_syscall_get_argument(data, 1); + if (bpf_probe_read(&rl, sizeof(rl), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + cur = rl.rlim_cur; + max = rl.rlim_max; + } else { + cur = -1; + max = -1; + } + + /* + * cur + */ + res = bpf_val_to_ring(data, cur); + if (res != PPM_SUCCESS) + return res; + + /* + * max + */ + res = bpf_val_to_ring(data, max); + + return res; +} + +FILLER(sys_connect_x, true) +{ + struct sockaddr *usrsockaddr; + unsigned long val; + long size = 0; + long retval; + int err; + int res; + int fd; + + /* + * Push the result + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * Retrieve the fd and push it to the ring. + * Note that, even if we are in the exit callback, the arguments are still + * in the stack, and therefore we can consume them. + */ + fd = bpf_syscall_get_argument(data, 0); + if (fd >= 0) { + usrsockaddr = (struct sockaddr *)bpf_syscall_get_argument(data, 1); + val = bpf_syscall_get_argument(data, 2); + + if (usrsockaddr && val != 0) { + /* + * Copy the address + */ + err = bpf_addr_to_kernel(usrsockaddr, val, + (struct sockaddr *)data->tmp_scratch); + if (err >= 0) { + /* + * Convert the fd into socket endpoint information + */ + size = bpf_fd_to_socktuple(data, + fd, + (struct sockaddr *)data->tmp_scratch, + val, + true, + false, + data->tmp_scratch + sizeof(struct sockaddr_storage)); + } + } + } + + /* + * Copy the endpoint info into the ring + */ + data->curarg_already_on_frame = true; + res = bpf_val_to_ring_len(data, 0, size); + + return res; +} + +FILLER(sys_socketpair_x, true) +{ + struct unix_sock *us = NULL; + struct sock *speer = NULL; + int fds[2] = { 0 }; + unsigned long val; + long retval; + int res; + + /* ret */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + if (retval >= 0) { + val = bpf_syscall_get_argument(data, 3); + if (bpf_probe_read(fds, 2 * sizeof(int), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + struct socket *sock = bpf_sockfd_lookup(data, fds[0]); + + if (sock) { + us = (struct unix_sock *)_READ(sock->sk); + speer = _READ(us->peer); + } + } + /* fd1 */ + res = bpf_val_to_ring_type(data, fds[0], PT_FD); + if (res != PPM_SUCCESS) + return res; + /* fd2 */ + res = bpf_val_to_ring_type(data, fds[1], PT_FD); + if (res != PPM_SUCCESS) + return res; + /* source */ + res = bpf_val_to_ring_type(data, (unsigned long)us, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + /* peer */ + res = bpf_val_to_ring_type(data, (unsigned long)speer, PT_UINT64); + + return res; +} + +static int __always_inline parse_sockopt(struct filler_data *data, int level, int optname, void *optval, int optlen) +{ + union { + uint32_t val32; + uint64_t val64; + struct timeval tv; + } u; + + if (level == SOL_SOCKET) { + switch (optname) { +#ifdef SO_ERROR + case SO_ERROR: + if (bpf_probe_read(&u.val32, sizeof(u.val32), optval)) + return PPM_FAILURE_INVALID_USER_MEMORY; + return bpf_val_to_ring_dyn(data, -u.val32, PT_ERRNO, PPM_SOCKOPT_IDX_ERRNO); +#endif + +#ifdef SO_RCVTIMEO + case SO_RCVTIMEO: +#endif +#ifdef SO_SNDTIMEO + case SO_SNDTIMEO: +#endif + if (bpf_probe_read(&u.tv, sizeof(u.tv), optval)) + return PPM_FAILURE_INVALID_USER_MEMORY; + return bpf_val_to_ring_dyn(data, u.tv.tv_sec * 1000000000 + u.tv.tv_usec * 1000, PT_RELTIME, PPM_SOCKOPT_IDX_TIMEVAL); + +#ifdef SO_COOKIE + case SO_COOKIE: + if (bpf_probe_read(&u.val64, sizeof(u.val64), optval)) + return PPM_FAILURE_INVALID_USER_MEMORY; + return bpf_val_to_ring_dyn(data, u.val64, PT_UINT64, PPM_SOCKOPT_IDX_UINT64); +#endif + +#ifdef SO_DEBUG + case SO_DEBUG: +#endif +#ifdef SO_REUSEADDR + case SO_REUSEADDR: +#endif +#ifdef SO_TYPE + case SO_TYPE: +#endif +#ifdef SO_DONTROUTE + case SO_DONTROUTE: +#endif +#ifdef SO_BROADCAST + case SO_BROADCAST: +#endif +#ifdef SO_SNDBUF + case SO_SNDBUF: +#endif +#ifdef SO_RCVBUF + case SO_RCVBUF: +#endif +#ifdef SO_SNDBUFFORCE + case SO_SNDBUFFORCE: +#endif +#ifdef SO_RCVBUFFORCE + case SO_RCVBUFFORCE: +#endif +#ifdef SO_KEEPALIVE + case SO_KEEPALIVE: +#endif +#ifdef SO_OOBINLINE + case SO_OOBINLINE: +#endif +#ifdef SO_NO_CHECK + case SO_NO_CHECK: +#endif +#ifdef SO_PRIORITY + case SO_PRIORITY: +#endif +#ifdef SO_BSDCOMPAT + case SO_BSDCOMPAT: +#endif +#ifdef SO_REUSEPORT + case SO_REUSEPORT: +#endif +#ifdef SO_PASSCRED + case SO_PASSCRED: +#endif +#ifdef SO_RCVLOWAT + case SO_RCVLOWAT: +#endif +#ifdef SO_SNDLOWAT + case SO_SNDLOWAT: +#endif +#ifdef SO_SECURITY_AUTHENTICATION + case SO_SECURITY_AUTHENTICATION: +#endif +#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT + case SO_SECURITY_ENCRYPTION_TRANSPORT: +#endif +#ifdef SO_SECURITY_ENCRYPTION_NETWORK + case SO_SECURITY_ENCRYPTION_NETWORK: +#endif +#ifdef SO_BINDTODEVICE + case SO_BINDTODEVICE: +#endif +#ifdef SO_DETACH_FILTER + case SO_DETACH_FILTER: +#endif +#ifdef SO_TIMESTAMP + case SO_TIMESTAMP: +#endif +#ifdef SO_ACCEPTCONN + case SO_ACCEPTCONN: +#endif +#ifdef SO_PEERSEC + case SO_PEERSEC: +#endif +#ifdef SO_PASSSEC + case SO_PASSSEC: +#endif +#ifdef SO_TIMESTAMPNS + case SO_TIMESTAMPNS: +#endif +#ifdef SO_MARK + case SO_MARK: +#endif +#ifdef SO_TIMESTAMPING + case SO_TIMESTAMPING: +#endif +#ifdef SO_PROTOCOL + case SO_PROTOCOL: +#endif +#ifdef SO_DOMAIN + case SO_DOMAIN: +#endif +#ifdef SO_RXQ_OVFL + case SO_RXQ_OVFL: +#endif +#ifdef SO_WIFI_STATUS + case SO_WIFI_STATUS: +#endif +#ifdef SO_PEEK_OFF + case SO_PEEK_OFF: +#endif +#ifdef SO_NOFCS + case SO_NOFCS: +#endif +#ifdef SO_LOCK_FILTER + case SO_LOCK_FILTER: +#endif +#ifdef SO_SELECT_ERR_QUEUE + case SO_SELECT_ERR_QUEUE: +#endif +#ifdef SO_BUSY_POLL + case SO_BUSY_POLL: +#endif +#ifdef SO_MAX_PACING_RATE + case SO_MAX_PACING_RATE: +#endif +#ifdef SO_BPF_EXTENSIONS + case SO_BPF_EXTENSIONS: +#endif +#ifdef SO_INCOMING_CPU + case SO_INCOMING_CPU: +#endif + if (bpf_probe_read(&u.val32, sizeof(u.val32), optval)) + return PPM_FAILURE_INVALID_USER_MEMORY; + return bpf_val_to_ring_dyn(data, u.val32, PT_UINT32, PPM_SOCKOPT_IDX_UINT32); + + default: + return __bpf_val_to_ring(data, (unsigned long)optval, optlen, PT_BYTEBUF, PPM_SOCKOPT_IDX_UNKNOWN, false); + } + } else { + return __bpf_val_to_ring(data, (unsigned long)optval, optlen, PT_BYTEBUF, PPM_SOCKOPT_IDX_UNKNOWN, false); + } +} + +FILLER(sys_setsockopt_x, true) +{ + int res; + unsigned long retval, fd, level, optname, optval, optlen; + + retval = bpf_syscall_get_retval(data->ctx); + + /* retval */ + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* fd */ + fd = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring_type(data, fd, PT_FD); + if (res != PPM_SUCCESS) + return res; + + /* level */ + level = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring_type(data, sockopt_level_to_scap(level), PT_FLAGS8); + if (res != PPM_SUCCESS) + return res; + + /* optname */ + optname = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring_type(data, sockopt_optname_to_scap(level, optname), PT_FLAGS8); + if (res != PPM_SUCCESS) + return res; + + /* optval */ + optval = bpf_syscall_get_argument(data, 3); + optlen = bpf_syscall_get_argument(data, 4); + res = parse_sockopt(data, level, optname, (void*)optval, optlen); + if (res != PPM_SUCCESS) + return res; + + /* optlen */ + res = bpf_val_to_ring_type(data, optlen, PT_UINT32); + return res; +} + +FILLER(sys_getsockopt_x, true) +{ + int res; + unsigned long retval, fd, level, optname, optval, optlen_p, optlen; + + retval = bpf_syscall_get_retval(data->ctx); + + /* retval */ + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* fd */ + fd = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring_type(data, fd, PT_FD); + if (res != PPM_SUCCESS) + return res; + + /* level */ + level = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring_type(data, sockopt_level_to_scap(level), PT_FLAGS8); + if (res != PPM_SUCCESS) + return res; + + /* optname */ + optname = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring_type(data, sockopt_optname_to_scap(level, optname), PT_FLAGS8); + if (res != PPM_SUCCESS) + return res; + + /* optval */ + optval = bpf_syscall_get_argument(data, 3); + optlen_p = bpf_syscall_get_argument(data, 4); + if (bpf_probe_read(&optlen, sizeof(optlen), (void*)optlen_p)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = parse_sockopt(data, level, optname, (void*)optval, optlen); + if (res != PPM_SUCCESS) + return res; + + /* optlen */ + res = bpf_val_to_ring_type(data, optlen, PT_UINT32); + return res; +} + +static __always_inline int f_sys_send_e_common(struct filler_data *data, int fd) +{ + unsigned long val; + int res; + + /* + * fd + */ + res = bpf_val_to_ring(data, fd); + if (res != PPM_SUCCESS) + return res; + + /* + * size + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_send_e, true) +{ + int res; + int fd; + + /* + * Push the common params to the ring + */ + fd = bpf_syscall_get_argument(data, 0); + res = f_sys_send_e_common(data, fd); + + return res; +} + +FILLER(sys_sendto_e, true) +{ + struct sockaddr __user *usrsockaddr; + unsigned long val; + long size = 0; + int err = 0; + int res; + int fd; + + /* + * Push the common params to the ring + */ + fd = bpf_syscall_get_argument(data, 0); + res = f_sys_send_e_common(data, fd); + if (res != PPM_SUCCESS) + return res; + + /* + * Get the address + */ + val = bpf_syscall_get_argument(data, 4); + usrsockaddr = (struct sockaddr __user *)val; + + /* + * Get the address len + */ + val = bpf_syscall_get_argument(data, 5); + + if (usrsockaddr && val != 0) { + /* + * Copy the address + */ + err = bpf_addr_to_kernel(usrsockaddr, val, + (struct sockaddr *)data->tmp_scratch); + if (err >= 0) { + /* + * Convert the fd into socket endpoint information + */ + size = bpf_fd_to_socktuple(data, + fd, + (struct sockaddr *)data->tmp_scratch, + val, + true, + false, + data->tmp_scratch + sizeof(struct sockaddr_storage)); + } + } + + /* + * Copy the endpoint info into the ring + */ + data->curarg_already_on_frame = true; + res = bpf_val_to_ring_len(data, 0, size); + + return res; +} + +FILLER(sys_send_x, true) +{ + unsigned long bufsize; + unsigned long val; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * data + */ + if (retval < 0) { + /* + * The operation failed, return an empty buffer + */ + val = 0; + bufsize = 0; + } else { + val = bpf_syscall_get_argument(data, 1); + + /* + * The return value can be lower than the value provided by the user, + * and we take that into account. + */ + bufsize = retval; + } + + data->fd = bpf_syscall_get_argument(data, 0); + res = __bpf_val_to_ring(data, val, bufsize, PT_BYTEBUF, -1, true); + + return res; +} + +FILLER(sys_execve_e, true) +{ + unsigned long val; + int res; + + /* + * filename + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res == PPM_FAILURE_INVALID_USER_MEMORY) { + char na[] = ""; + + res = bpf_val_to_ring(data, (unsigned long)na); + } + + return res; +} + +static __always_inline int bpf_ppm_get_tty(struct task_struct *task) +{ + struct signal_struct *sig; + struct tty_struct *tty; + struct tty_driver *driver; + int major; + int minor_start; + int index; + int tty_nr = 0; + + sig = _READ(task->signal); + if (!sig) + return 0; + + tty = _READ(sig->tty); + if (!tty) + return 0; + + index = _READ(tty->index); + + driver = _READ(tty->driver); + if (!driver) + return 0; + + major = _READ(driver->major); + minor_start = _READ(driver->minor_start); + + tty_nr = new_encode_dev(MKDEV(major, minor_start) + index); + + return tty_nr; +} + +static __always_inline struct pid *bpf_task_pid(struct task_struct *task) +{ +#if (PPM_RHEL_RELEASE_CODE > 0 && PPM_RHEL_RELEASE_CODE >= PPM_RHEL_RELEASE_VERSION(8, 0)) + return _READ(task->thread_pid); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0) + return _READ(task->pids[PIDTYPE_PID].pid); +#else + return _READ(task->thread_pid); +#endif +} + +static __always_inline struct pid_namespace *bpf_ns_of_pid(struct pid *pid) +{ + struct pid_namespace *ns = NULL; + + if (pid) + ns = _READ(pid->numbers[_READ(pid->level)].ns); + return ns; +} + +static __always_inline struct pid_namespace *bpf_task_active_pid_ns(struct task_struct *tsk) +{ + return bpf_ns_of_pid(bpf_task_pid(tsk)); +} + +static __always_inline pid_t bpf_pid_nr_ns(struct pid *pid, + struct pid_namespace *ns) +{ + unsigned int ns_level; + struct upid *upid; + pid_t nr = 0; + + ns_level = _READ(ns->level); + if (pid && ns_level <= _READ(pid->level)) { + upid = &pid->numbers[ns_level]; + if (_READ(upid->ns) == ns) + nr = _READ(upid->nr); + } + return nr; +} + +#if ((PPM_RHEL_RELEASE_CODE > 0 && PPM_RHEL_RELEASE_CODE >= PPM_RHEL_RELEASE_VERSION(8, 0))) || LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) +static __always_inline struct pid **bpf_task_pid_ptr(struct task_struct *task, + enum pid_type type) +{ + return (type == PIDTYPE_PID) ? + &task->thread_pid : + &_READ(task->signal)->pids[type]; +} +#endif + +static __always_inline pid_t bpf_task_pid_nr_ns(struct task_struct *task, + enum pid_type type, + struct pid_namespace *ns) +{ + pid_t nr = 0; + + if (!ns) + ns = bpf_task_active_pid_ns(task); + +#if (PPM_RHEL_RELEASE_CODE > 0 && PPM_RHEL_RELEASE_CODE >= PPM_RHEL_RELEASE_VERSION(8, 0)) + nr = bpf_pid_nr_ns(_READ(*bpf_task_pid_ptr(task, type)), ns); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0) + if (type != PIDTYPE_PID) { + if (type == __PIDTYPE_TGID) + type = PIDTYPE_PID; + + task = _READ(task->group_leader); + } + + nr = bpf_pid_nr_ns(_READ(task->pids[type].pid), ns); +#else + nr = bpf_pid_nr_ns(_READ(*bpf_task_pid_ptr(task, type)), ns); +#endif + + return nr; +} + +static __always_inline pid_t bpf_task_pid_vnr(struct task_struct *task) +{ + return bpf_task_pid_nr_ns(task, PIDTYPE_PID, NULL); +} + +static __always_inline pid_t bpf_task_tgid_vnr(struct task_struct *task) +{ +#if (PPM_RHEL_RELEASE_CODE > 0 && PPM_RHEL_RELEASE_CODE >= PPM_RHEL_RELEASE_VERSION(8, 0)) + return bpf_task_pid_nr_ns(task, PIDTYPE_TGID, NULL); +#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0) + return bpf_task_pid_nr_ns(task, __PIDTYPE_TGID, NULL); +#else + return bpf_task_pid_nr_ns(task, PIDTYPE_TGID, NULL); +#endif +} + +static __always_inline pid_t bpf_task_pgrp_vnr(struct task_struct *task) +{ + return bpf_task_pid_nr_ns(task, PIDTYPE_PGID, NULL); +} + +#define MAX_CGROUP_PATHS 6 + +static __always_inline int __bpf_append_cgroup(struct css_set *cgroups, + int subsys_id, + char *buf, + int *len) +{ + struct cgroup_subsys_state *css = _READ(cgroups->subsys[subsys_id]); + struct cgroup_subsys *ss = _READ(css->ss); + char *subsys_name = (char *)_READ(ss->name); + struct cgroup *cgroup = _READ(css->cgroup); + struct kernfs_node *kn = _READ(cgroup->kn); + char *cgroup_path[MAX_CGROUP_PATHS]; + bool prev_empty = false; + int off = *len; + + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + int res = bpf_probe_read_str(&buf[off & SCRATCH_SIZE_HALF], + SCRATCH_SIZE_HALF, + subsys_name); + if (res == -EFAULT) + return PPM_FAILURE_INVALID_USER_MEMORY; + + off += res - 1; + + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + buf[off & SCRATCH_SIZE_HALF] = '='; + ++off; + + #pragma unroll MAX_CGROUP_PATHS + for (int k = 0; k < MAX_CGROUP_PATHS; ++k) { + if (kn) { + cgroup_path[k] = (char *)_READ(kn->name); + kn = _READ(kn->parent); + } else { + cgroup_path[k] = NULL; + } + } + + #pragma unroll MAX_CGROUP_PATHS + for (int k = MAX_CGROUP_PATHS - 1; k >= 0 ; --k) { + if (cgroup_path[k]) { + if (!prev_empty) { + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + buf[off & SCRATCH_SIZE_HALF] = '/'; + ++off; + } + + prev_empty = false; + + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + res = bpf_probe_read_str(&buf[off & SCRATCH_SIZE_HALF], + SCRATCH_SIZE_HALF, + cgroup_path[k]); + if (res > 1) + off += res - 1; + else if (res == 1) + prev_empty = true; + else + return PPM_FAILURE_INVALID_USER_MEMORY; + } + } + + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + buf[off & SCRATCH_SIZE_HALF] = 0; + ++off; + *len = off; + + return PPM_SUCCESS; +} + +static __always_inline int bpf_append_cgroup(struct task_struct *task, + char *buf, + int *len) +{ + struct css_set *cgroups = _READ(task->cgroups); + int res; + +#if IS_ENABLED(CONFIG_CPUSETS) + res = __bpf_append_cgroup(cgroups, cpuset_cgrp_id, buf, len); + if (res != PPM_SUCCESS) + return res; +#endif + +#if IS_ENABLED(CONFIG_CGROUP_SCHED) + res = __bpf_append_cgroup(cgroups, cpu_cgrp_id, buf, len); + if (res != PPM_SUCCESS) + return res; +#endif + +#if IS_ENABLED(CONFIG_CGROUP_CPUACCT) + res = __bpf_append_cgroup(cgroups, cpuacct_cgrp_id, buf, len); + if (res != PPM_SUCCESS) + return res; +#endif + +#if IS_ENABLED(CONFIG_BLK_CGROUP) + res = __bpf_append_cgroup(cgroups, io_cgrp_id, buf, len); + if (res != PPM_SUCCESS) + return res; +#endif + +#if IS_ENABLED(CONFIG_MEMCG) + res = __bpf_append_cgroup(cgroups, memory_cgrp_id, buf, len); + if (res != PPM_SUCCESS) + return res; +#endif + + return PPM_SUCCESS; +} + +#define ARGS_ENV_SIZE_MAX 4096 +#define FAILED_ARGS_ENV_ITEMS_MAX 16 + +static __always_inline int bpf_accumulate_argv_or_env(struct filler_data *data, + char **argv, + long *args_len) +{ + char *arg; + int off; + int len; + int j; + + *args_len = 0; + off = data->state->tail_ctx.curoff; + + #pragma unroll + for (j = 0; j < FAILED_ARGS_ENV_ITEMS_MAX; ++j) { + arg = _READ(argv[j]); + if (!arg) + break; + + if (off > SCRATCH_SIZE_HALF) + return PPM_FAILURE_BUFFER_FULL; + + len = bpf_probe_read_str(&data->buf[off & SCRATCH_SIZE_HALF], SCRATCH_SIZE_HALF, arg); + if (len == -EFAULT) + return PPM_FAILURE_INVALID_USER_MEMORY; + + *args_len += len; + off += len; + + if (*args_len > ARGS_ENV_SIZE_MAX) { + *args_len = ARGS_ENV_SIZE_MAX; + data->buf[(data->state->tail_ctx.curoff + *args_len - 1) & SCRATCH_SIZE_MAX] = 0; + break; + } + } + + return PPM_SUCCESS; +} + +FILLER(proc_startupdate, true) +{ + struct task_struct *real_parent; + struct signal_struct *signal; + struct task_struct *task; + unsigned long total_vm; + unsigned long min_flt; + unsigned long maj_flt; + unsigned long fdlimit; + struct mm_struct *mm; + long total_rss; + char empty = 0; + long args_len; + long retval; + pid_t tgid; + long swap; + pid_t pid; + int res; + + /* + * Make sure the operation was successful + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + task = (struct task_struct *)bpf_get_current_task(); + mm = _READ(task->mm); + if (!mm) + return PPM_FAILURE_BUG; + + if (retval >= 0) { + /* + * The call succeeded. Get exe, args from the current + * process; put one \0-separated exe-args string into + * str_storage + */ + unsigned long arg_start; + unsigned long arg_end; + + arg_end = _READ(mm->arg_end); + if (!arg_end) + return PPM_FAILURE_BUG; + + arg_start = _READ(mm->arg_start); + args_len = arg_end - arg_start; + + if (args_len) { + if (args_len > ARGS_ENV_SIZE_MAX) + args_len = ARGS_ENV_SIZE_MAX; + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + ((args_len - 1) & SCRATCH_SIZE_HALF) + 1, + (void *)arg_start)) +#else + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + args_len & SCRATCH_SIZE_HALF, + (void *)arg_start)) +#endif + args_len = 0; + else + data->buf[(data->state->tail_ctx.curoff + args_len - 1) & SCRATCH_SIZE_MAX] = 0; + } + } else if (data->state->tail_ctx.evt_type == PPME_SYSCALL_EXECVE_19_X) { + unsigned long val; + char **argv; + + val = bpf_syscall_get_argument(data, 1); + argv = (char **)val; + + res = bpf_accumulate_argv_or_env(data, argv, &args_len); + if (res != PPM_SUCCESS) + args_len = 0; + } else { + args_len = 0; + } + + if (args_len) { + int exe_len; + + exe_len = bpf_probe_read_str(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + SCRATCH_SIZE_HALF, + &data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]); + + if (exe_len == -EFAULT) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * exe + */ + data->curarg_already_on_frame = true; + res = __bpf_val_to_ring(data, 0, exe_len, PT_CHARBUF, -1, false); + if (res != PPM_SUCCESS) + return res; + + /* + * Args + */ + data->curarg_already_on_frame = true; + res = __bpf_val_to_ring(data, 0, args_len - exe_len, PT_BYTEBUF, -1, false); + if (res != PPM_SUCCESS) + return res; + } else { + /* + * exe + */ + res = bpf_val_to_ring_type(data, (unsigned long)&empty, PT_CHARBUF); + if (res != PPM_SUCCESS) + return res; + + /* + * Args + */ + res = bpf_val_to_ring_type(data, 0, PT_BYTEBUF); + if (res != PPM_SUCCESS) + return res; + } + + /* + * tid + */ + pid = _READ(task->pid); + + res = bpf_val_to_ring_type(data, pid, PT_PID); + if (res != PPM_SUCCESS) + return res; + + /* + * pid + */ + tgid = _READ(task->tgid); + + res = bpf_val_to_ring_type(data, tgid, PT_PID); + if (res != PPM_SUCCESS) + return res; + + /* + * ptid + */ + real_parent = _READ(task->real_parent); + pid_t ptid = _READ(real_parent->pid); + + res = bpf_val_to_ring_type(data, ptid, PT_PID); + if (res != PPM_SUCCESS) + return res; + + /* + * cwd, pushed empty to avoid breaking compatibility + * with the older event format + */ + res = bpf_val_to_ring_type(data, (unsigned long)&empty, PT_CHARBUF); + if (res != PPM_SUCCESS) + return res; + + /* + * fdlimit + */ + signal = _READ(task->signal); + fdlimit = _READ(signal->rlim[RLIMIT_NOFILE].rlim_cur); + + res = bpf_val_to_ring_type(data, fdlimit, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + /* + * pgft_maj + */ + maj_flt = _READ(task->maj_flt); + + res = bpf_val_to_ring_type(data, maj_flt, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + /* + * pgft_min + */ + min_flt = _READ(task->min_flt); + + res = bpf_val_to_ring_type(data, min_flt, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + total_vm = 0; + total_rss = 0; + swap = 0; + + if (mm) { + total_vm = _READ(mm->total_vm); + total_vm <<= (PAGE_SHIFT - 10); + total_rss = bpf_get_mm_rss(mm) << (PAGE_SHIFT - 10); + swap = bpf_get_mm_swap(mm) << (PAGE_SHIFT - 10); + } + + /* + * vm_size + */ + res = bpf_val_to_ring_type(data, total_vm, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * vm_rss + */ + res = bpf_val_to_ring_type(data, total_rss, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * vm_swap + */ + res = bpf_val_to_ring_type(data, swap, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * comm + */ + res = bpf_val_to_ring_type(data, (unsigned long)task->comm, PT_CHARBUF); + if (res != PPM_SUCCESS) + return res; + + bpf_tail_call(data->ctx, &tail_map, PPM_FILLER_proc_startupdate_2); + bpf_printk("Can't tail call f_proc_startupdate_2 filler\n"); + return PPM_FAILURE_BUG; +} + +FILLER(proc_startupdate_2, true) +{ + struct task_struct *task; + int cgroups_len = 0; + int res; + + task = (struct task_struct *)bpf_get_current_task(); + + /* + * cgroups + */ + res = bpf_append_cgroup(task, data->tmp_scratch, &cgroups_len); + if (res != PPM_SUCCESS) + return res; + + res = __bpf_val_to_ring(data, (unsigned long)data->tmp_scratch, cgroups_len, PT_BYTEBUF, -1, false); + if (res != PPM_SUCCESS) + return res; + + bpf_tail_call(data->ctx, &tail_map, PPM_FILLER_proc_startupdate_3); + bpf_printk("Can't tail call f_proc_startupdate_3 filler\n"); + return PPM_FAILURE_BUG; +} + +FILLER(proc_startupdate_3, true) +{ + struct task_struct *task; + struct mm_struct *mm; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + + task = (struct task_struct *)bpf_get_current_task(); + mm = _READ(task->mm); + if (!mm) + return PPM_FAILURE_BUG; + + if (data->state->tail_ctx.evt_type == PPME_SYSCALL_CLONE_20_X || + data->state->tail_ctx.evt_type == PPME_SYSCALL_FORK_20_X || + data->state->tail_ctx.evt_type == PPME_SYSCALL_VFORK_20_X) { + /* + * clone-only parameters + */ + unsigned long flags; + struct cred *cred; + kuid_t euid; + kgid_t egid; + pid_t vtid; + pid_t vpid; + struct pid_namespace *pidns = bpf_task_active_pid_ns(task); + int pidns_level = _READ(pidns->level); + + /* + * flags + */ + if (data->state->tail_ctx.evt_type == PPME_SYSCALL_CLONE_20_X) + flags = bpf_syscall_get_argument(data, 0); + else + flags = 0; + + flags = clone_flags_to_scap(flags); + + if(pidns_level != 0) { + flags |= PPM_CL_CHILD_IN_PIDNS; + } else { + struct nsproxy *nsproxy = _READ(task->nsproxy); + if(nsproxy) { + struct pid_namespace *pid_ns_for_children = _READ(nsproxy->pid_ns_for_children); + if(pid_ns_for_children != pidns) { + flags |= PPM_CL_CHILD_IN_PIDNS; + } + } + } + + res = bpf_val_to_ring_type(data, flags, PT_FLAGS32); + if (res != PPM_SUCCESS) + return res; + + /* + * This logic is wrong and doesn't account for user + * namespaces. + * Fix this at some point, maybe with a custom BPF + * helper. + */ + cred = (struct cred *)_READ(task->cred); + + euid = _READ(cred->euid); + + /* + * uid + */ + res = bpf_val_to_ring_type(data, euid.val, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + egid = _READ(cred->egid); + + /* + * gid + */ + res = bpf_val_to_ring_type(data, egid.val, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * vtid + */ + vtid = bpf_task_pid_vnr(task); + res = bpf_val_to_ring_type(data, vtid, PT_PID); + if (res != PPM_SUCCESS) + return res; + + /* + * vpid + */ + vpid = bpf_task_tgid_vnr(task); + res = bpf_val_to_ring_type(data, vpid, PT_PID); + + } else if (data->state->tail_ctx.evt_type == PPME_SYSCALL_EXECVE_19_X) { + /* + * execve-only parameters + */ + long env_len = 0; + kuid_t loginuid; + int tty; + + /* + * environ + */ + if (retval >= 0) { + /* + * Already checked for mm validity + */ + unsigned long env_end = _READ(mm->env_end); + unsigned long env_start = _READ(mm->env_start); + + env_len = env_end - env_start; + + if (env_len) { + if (env_len > ARGS_ENV_SIZE_MAX) + env_len = ARGS_ENV_SIZE_MAX; + +#ifdef BPF_FORBIDS_ZERO_ACCESS + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + ((env_len - 1) & SCRATCH_SIZE_HALF) + 1, + (void *)env_start)) +#else + if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF], + env_len & SCRATCH_SIZE_HALF, + (void *)env_start)) +#endif + env_len = 0; + else + data->buf[(data->state->tail_ctx.curoff + env_len - 1) & SCRATCH_SIZE_MAX] = 0; + } + } else { + unsigned long val; + char **envp; + + val = bpf_syscall_get_argument(data, 2); + envp = (char **)val; + + res = bpf_accumulate_argv_or_env(data, envp, &env_len); + if (res != PPM_SUCCESS) + env_len = 0; + } + + data->curarg_already_on_frame = true; + res = __bpf_val_to_ring(data, 0, env_len, PT_BYTEBUF, -1, false); + if (res != PPM_SUCCESS) + return res; + + /* + * tty + */ + tty = bpf_ppm_get_tty(task); + + res = bpf_val_to_ring_type(data, tty, PT_INT32); + if (res != PPM_SUCCESS) + return res; + + /* + * pgid + */ + res = bpf_val_to_ring_type(data, bpf_task_pgrp_vnr(task), PT_PID); + if (res != PPM_SUCCESS) + return res; + + /* + * loginuid + */ + /* TODO: implement user namespace support */ +#ifdef COS_73_WORKAROUND + { + struct audit_task_info* audit = _READ(task->audit); + if (audit) { + loginuid = _READ(audit->loginuid); + } else { + loginuid = INVALID_UID; + } + } +#else + loginuid = _READ(task->loginuid); +#endif + + res = bpf_val_to_ring_type(data, loginuid.val, PT_INT32); + if (res != PPM_SUCCESS) + return res; + } + + return res; +} + +FILLER(sys_accept4_e, true) +{ + int res; + + /* + * push the flags into the ring. + * XXX we don't support flags yet and so we just return zero + */ + res = bpf_val_to_ring(data, 0); + + return res; +} + +FILLER(sys_accept_x, true) +{ + unsigned long max_ack_backlog = 0; + unsigned long ack_backlog = 0; + unsigned long queuepct = 0; + struct socket *sock; + long size = 0; + int res; + int fd; + + /* + * Retrieve the fd and push it to the ring. + * Note that, even if we are in the exit callback, the arguments are still + * in the stack, and therefore we can consume them. + */ + fd = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, fd, PT_FD); + if (res != PPM_SUCCESS) + return res; + + /* + * Convert the fd into socket endpoint information + */ + size = bpf_fd_to_socktuple(data, fd, NULL, 0, false, true, + data->tmp_scratch); + + /* + * Copy the endpoint info into the ring + */ + data->curarg_already_on_frame = true; + res = __bpf_val_to_ring(data, 0, size, PT_SOCKTUPLE, -1, false); + if (res != PPM_SUCCESS) + return res; + + sock = bpf_sockfd_lookup(data, fd); + if (sock) { + struct sock *sk = _READ(sock->sk); + + if (sk) { + ack_backlog = _READ(sk->sk_ack_backlog); + max_ack_backlog = _READ(sk->sk_max_ack_backlog); + + if (max_ack_backlog) + queuepct = (unsigned long)ack_backlog * 100 / max_ack_backlog; + } + } + + /* queuepct */ + res = bpf_val_to_ring_type(data, queuepct, PT_UINT8); + if (res != PPM_SUCCESS) + return res; + + /* queuelen */ + res = bpf_val_to_ring_type(data, ack_backlog, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* queuemax */ + res = bpf_val_to_ring_type(data, max_ack_backlog, PT_UINT32); + + return res; +} + +FILLER(sys_setns_e, true) +{ + unsigned long val; + u32 flags; + int res; + + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + val = bpf_syscall_get_argument(data, 1); + flags = clone_flags_to_scap(val); + res = bpf_val_to_ring(data, flags); + + return res; +} + +FILLER(sys_unshare_e, true) +{ + unsigned long val; + u32 flags; + int res; + + val = bpf_syscall_get_argument(data, 0); + flags = clone_flags_to_scap(val); + res = bpf_val_to_ring(data, flags); + + return res; +} + +FILLER(sys_generic, true) +{ + long *sysdig_id; + int native_id; + int res; + + native_id = bpf_syscall_get_nr(data->ctx); + sysdig_id = bpf_map_lookup_elem(&syscall_code_routing_table, &native_id); + if (!sysdig_id) { + bpf_printk("no routing for syscall %d\n", native_id); + return PPM_FAILURE_BUG; + } + + if (*sysdig_id == PPM_SC_UNKNOWN) + bpf_printk("no syscall for id %d\n", native_id); + + /* + * id + */ + res = bpf_val_to_ring(data, *sysdig_id); + if (res != PPM_SUCCESS) + return res; + + if (data->state->tail_ctx.evt_type == PPME_GENERIC_E) { + /* + * native id + */ + res = bpf_val_to_ring(data, native_id); + } + + return res; +} + +FILLER(sys_openat_x, true) +{ + unsigned long dev; + unsigned long ino; + unsigned long flags; + unsigned long val; + unsigned long mode; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * dirfd + */ + val = bpf_syscall_get_argument(data, 0); + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * name + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * Flags + * Note that we convert them into the ppm portable representation before pushing them to the ring + */ + val = bpf_syscall_get_argument(data, 2); + flags = open_flags_to_scap(val); + res = bpf_val_to_ring(data, flags); + if (res != PPM_SUCCESS) + return res; + + /* + * mode + */ + mode = bpf_syscall_get_argument(data, 3); + mode = open_modes_to_scap(val, mode); + res = bpf_val_to_ring(data, mode); + if (res != PPM_SUCCESS) + return res; + + /* + * Device + */ + if (retval < 0 || !bpf_get_fd_dev_ino(retval, &dev, &ino)) + dev = 0; + + res = bpf_val_to_ring(data, dev); + return res; +} + +FILLER(sys_sendfile_e, true) +{ + unsigned long val; + off_t *offp; + off_t off; + int res; + + /* + * out_fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * in_fd + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * offset + */ + offp = (off_t *)bpf_syscall_get_argument(data, 2); + off = _READ(*offp); + res = bpf_val_to_ring(data, off); + if (res != PPM_SUCCESS) + return res; + + /* + * size + */ + val = bpf_syscall_get_argument(data, 3); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_sendfile_x, true) +{ + long retval; + off_t *offp; + off_t off; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * offset + */ + offp = (off_t *)bpf_syscall_get_argument(data, 2); + off = _READ(*offp); + res = bpf_val_to_ring(data, off); + + return res; +} + +FILLER(sys_prlimit_e, true) +{ + unsigned long val; + u8 ppm_resource; + int res; + + /* + * pid + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * resource + */ + val = bpf_syscall_get_argument(data, 1); + ppm_resource = rlimit_resource_to_scap(val); + res = bpf_val_to_ring(data, ppm_resource); + + return res; +} + +FILLER(sys_prlimit_x, true) +{ + unsigned long val; + struct rlimit rl; + long retval; + s64 newcur; + s64 newmax; + s64 oldcur; + s64 oldmax; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * Copy the user structure and extract cur and max + */ + if (retval >= 0) { + val = bpf_syscall_get_argument(data, 2); + if (bpf_probe_read(&rl, sizeof(rl), (void *)val)) { + newcur = -1; + newmax = -1; + } else { + newcur = rl.rlim_cur; + newmax = rl.rlim_max; + } + } else { + newcur = -1; + newmax = -1; + } + + val = bpf_syscall_get_argument(data, 3); + if (bpf_probe_read(&rl, sizeof(rl), (void *)val)) { + oldcur = -1; + oldmax = -1; + } else { + oldcur = rl.rlim_cur; + oldmax = rl.rlim_max; + } + + /* + * newcur + */ + res = bpf_val_to_ring_type(data, newcur, PT_INT64); + if (res != PPM_SUCCESS) + return res; + + /* + * newmax + */ + res = bpf_val_to_ring_type(data, newmax, PT_INT64); + if (res != PPM_SUCCESS) + return res; + + /* + * oldcur + */ + res = bpf_val_to_ring_type(data, oldcur, PT_INT64); + if (res != PPM_SUCCESS) + return res; + + /* + * oldmax + */ + res = bpf_val_to_ring_type(data, oldmax, PT_INT64); + + return res; +} + +FILLER(sys_pwritev_e, true) +{ + const struct iovec __user *iov; + unsigned long iovcnt; + unsigned long val; + int res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + iov = (const struct iovec __user *)bpf_syscall_get_argument(data, 1); + iovcnt = bpf_syscall_get_argument(data, 2); + + res = bpf_parse_readv_writev_bufs(data, + iov, + iovcnt, + 0, + PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); + if (res != PPM_SUCCESS) + return res; + + val = bpf_syscall_get_argument(data, 3); + res = bpf_val_to_ring_type(data, val, PT_UINT64); + + return res; +} + +FILLER(sys_getresuid_and_gid_x, true) +{ + long retval; + u32 *idp; + int res; + u32 id; + + /* + * return value + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * ruid + */ + idp = (u32 *)bpf_syscall_get_argument(data, 0); + id = _READ(*idp); + + res = bpf_val_to_ring(data, id); + if (res != PPM_SUCCESS) + return res; + + /* + * euid + */ + idp = (u32 *)bpf_syscall_get_argument(data, 1); + id = _READ(*idp); + + res = bpf_val_to_ring(data, id); + if (res != PPM_SUCCESS) + return res; + + /* + * suid + */ + idp = (u32 *)bpf_syscall_get_argument(data, 2); + id = _READ(*idp); + + res = bpf_val_to_ring(data, id); + + return res; +} + +FILLER(sys_socket_bind_x, true) +{ + struct sockaddr *usrsockaddr; + unsigned long val; + u16 size = 0; + int err = 0; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * addr + */ + usrsockaddr = (struct sockaddr __user *)bpf_syscall_get_argument(data, 1); + val = bpf_syscall_get_argument(data, 2); + + if (usrsockaddr && val != 0) { + /* + * Copy the address + */ + err = bpf_addr_to_kernel(usrsockaddr, val, + (struct sockaddr *)data->tmp_scratch); + if (err >= 0) { + /* + * Convert the fd into socket endpoint information + */ + size = bpf_pack_addr(data, + (struct sockaddr *)data->tmp_scratch, + val); + } + } + + /* + * Copy the endpoint info into the ring + */ + data->curarg_already_on_frame = true; + res = bpf_val_to_ring_len(data, 0, size); + + return res; +} + +static __always_inline int f_sys_recv_x_common(struct filler_data *data, long retval) +{ + unsigned long bufsize; + unsigned long val; + int res; + + /* + * res + */ + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * data + */ + if (retval < 0) { + /* + * The operation failed, return an empty buffer + */ + val = 0; + bufsize = 0; + } else { + val = bpf_syscall_get_argument(data, 1); + + /* + * The return value can be lower than the value provided by the user, + * and we take that into account. + */ + bufsize = retval; + } + + data->fd = bpf_syscall_get_argument(data, 0); + res = __bpf_val_to_ring(data, val, bufsize, PT_BYTEBUF, -1, true); + + return res; +} + +FILLER(sys_recv_x, true) +{ + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = f_sys_recv_x_common(data, retval); + + return res; +} + +FILLER(sys_recvfrom_x, true) +{ + struct sockaddr *usrsockaddr; + unsigned long val; + u16 size = 0; + long retval; + int addrlen; + int err = 0; + int res; + int fd; + + /* + * Push the common params to the ring + */ + retval = bpf_syscall_get_retval(data->ctx); + res = f_sys_recv_x_common(data, retval); + if (res != PPM_SUCCESS) + return res; + + if (retval >= 0) { + /* + * Get the address + */ + usrsockaddr = (struct sockaddr *)bpf_syscall_get_argument(data, 4); + + /* + * Get the address len + */ + val = bpf_syscall_get_argument(data, 5); + + if (usrsockaddr && val != 0) { + if (bpf_probe_read(&addrlen, sizeof(addrlen), + (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * Copy the address + */ + err = bpf_addr_to_kernel(usrsockaddr, addrlen, + (struct sockaddr *)data->tmp_scratch); + if (err >= 0) { + fd = bpf_syscall_get_argument(data, 0); + + /* + * Convert the fd into socket endpoint information + */ + size = bpf_fd_to_socktuple(data, + fd, + (struct sockaddr *)data->tmp_scratch, + addrlen, + true, + true, + data->tmp_scratch + sizeof(struct sockaddr_storage)); + } + } + } + + /* + * Copy the endpoint info into the ring + */ + data->curarg_already_on_frame = true; + res = __bpf_val_to_ring(data, 0, size, PT_SOCKTUPLE, -1, false); + + return res; +} + +FILLER(sys_shutdown_e, true) +{ + unsigned int flags; + unsigned long val; + int res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * how + */ + val = bpf_syscall_get_argument(data, 1); + flags = shutdown_how_to_scap(val); + res = bpf_val_to_ring(data, flags); + + return res; +} + +FILLER(sys_recvmsg_x, true) +{ + const struct iovec *iov; + struct user_msghdr mh; + unsigned long iovcnt; + unsigned long val; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * Retrieve the message header + */ + val = bpf_syscall_get_argument(data, 1); + if (bpf_probe_read(&mh, sizeof(mh), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * data and size + */ + iov = (const struct iovec *)mh.msg_iov; + iovcnt = mh.msg_iovlen; + + res = bpf_parse_readv_writev_bufs(data, iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); + if (res != PPM_SUCCESS) + return res; + + bpf_tail_call(data->ctx, &tail_map, PPM_FILLER_sys_recvmsg_x_2); + bpf_printk("Can't tail call f_sys_recvmsg_x_2 filler\n"); + return PPM_FAILURE_BUG; +} + +FILLER(sys_recvmsg_x_2, true) +{ + struct sockaddr *usrsockaddr; + struct user_msghdr mh; + unsigned long val; + u16 size = 0; + long retval; + int addrlen; + int res; + int fd; + + retval = bpf_syscall_get_retval(data->ctx); + + /* + * tuple + */ + if (retval >= 0) { + /* + * Retrieve the message header + */ + val = bpf_syscall_get_argument(data, 1); + if (bpf_probe_read(&mh, sizeof(mh), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * Get the address + */ + usrsockaddr = (struct sockaddr *)mh.msg_name; + addrlen = mh.msg_namelen; + + if (usrsockaddr && addrlen != 0) { + /* + * Copy the address + */ + res = bpf_addr_to_kernel(usrsockaddr, + addrlen, + (struct sockaddr *)data->tmp_scratch); + + if (res >= 0) { + fd = bpf_syscall_get_argument(data, 0); + + /* + * Convert the fd into socket endpoint information + */ + size = bpf_fd_to_socktuple(data, + fd, + (struct sockaddr *)data->tmp_scratch, + addrlen, + true, + true, + data->tmp_scratch + sizeof(struct sockaddr_storage)); + } + } + } + + data->curarg_already_on_frame = true; + res = __bpf_val_to_ring(data, 0, size, PT_SOCKTUPLE, -1, false); + + return res; +} + +FILLER(sys_sendmsg_e, true) +{ + struct sockaddr *usrsockaddr; + const struct iovec *iov; + struct user_msghdr mh; + unsigned long iovcnt; + unsigned long val; + u16 size = 0; + int addrlen; + int err = 0; + int res; + int fd; + + /* + * fd + */ + fd = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring_type(data, fd, PT_FD); + if (res != PPM_SUCCESS) + return res; + + /* + * Retrieve the message header + */ + val = bpf_syscall_get_argument(data, 1); + if (bpf_probe_read(&mh, sizeof(mh), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * size + */ + iov = (const struct iovec *)mh.msg_iov; + iovcnt = mh.msg_iovlen; + + res = bpf_parse_readv_writev_bufs(data, iov, iovcnt, 0, + PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); + if (res != PPM_SUCCESS) + return res; + + /* + * tuple + */ + usrsockaddr = (struct sockaddr *)mh.msg_name; + addrlen = mh.msg_namelen; + + if (usrsockaddr && addrlen != 0) { + /* + * Copy the address + */ + err = bpf_addr_to_kernel(usrsockaddr, + addrlen, + (struct sockaddr *)data->tmp_scratch); + + if (err >= 0) { + /* + * Convert the fd into socket endpoint information + */ + size = bpf_fd_to_socktuple(data, + fd, + (struct sockaddr *)data->tmp_scratch, + addrlen, + true, + false, + data->tmp_scratch + sizeof(struct sockaddr_storage)); + } + } + + data->curarg_already_on_frame = true; + res = __bpf_val_to_ring(data, 0, size, PT_SOCKTUPLE, -1, false); + + return res; +} + +FILLER(sys_sendmsg_x, true) +{ + const struct iovec *iov; + struct user_msghdr mh; + unsigned long iovcnt; + unsigned long val; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * data + */ + val = bpf_syscall_get_argument(data, 1); + if (bpf_probe_read(&mh, sizeof(mh), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + iov = (const struct iovec *)mh.msg_iov; + iovcnt = mh.msg_iovlen; + + res = bpf_parse_readv_writev_bufs(data, iov, iovcnt, retval, + PRB_FLAG_PUSH_DATA | PRB_FLAG_IS_WRITE); + + return res; +} + +FILLER(sys_creat_x, true) +{ + unsigned long dev; + unsigned long ino; + unsigned long val; + unsigned long mode; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * name + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * mode + */ + mode = bpf_syscall_get_argument(data, 1); + mode = open_modes_to_scap(O_CREAT, mode); + res = bpf_val_to_ring(data, mode); + if (res != PPM_SUCCESS) + return res; + + /* + * Device + */ + if (retval < 0 || !bpf_get_fd_dev_ino(retval, &dev, &ino)) + dev = 0; + + res = bpf_val_to_ring(data, dev); + return res; +} + +FILLER(sys_pipe_x, true) +{ + unsigned long ino; + unsigned long dev; + unsigned long val; + long retval; + int fds[2]; + int res; + + /* + * retval + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * fds + */ + val = bpf_syscall_get_argument(data, 0); + if (bpf_probe_read(fds, sizeof(fds), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = bpf_val_to_ring(data, fds[0]); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring(data, fds[1]); + if (res != PPM_SUCCESS) + return res; + + if (!bpf_get_fd_dev_ino(fds[0], &dev, &ino)) + ino = 0; + + res = bpf_val_to_ring(data, ino); + + return res; +} + +FILLER(sys_lseek_e, true) +{ + unsigned long flags; + unsigned long val; + int res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * offset + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * whence + */ + val = bpf_syscall_get_argument(data, 2); + flags = lseek_whence_to_scap(val); + res = bpf_val_to_ring(data, flags); + + return res; +} + +FILLER(sys_llseek_e, true) +{ + unsigned long flags; + unsigned long val; + unsigned long oh; + unsigned long ol; + u64 offset; + int res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * offset + * We build it by combining the offset_high and offset_low + * system call arguments + */ + oh = bpf_syscall_get_argument(data, 1); + ol = bpf_syscall_get_argument(data, 2); + offset = (((u64)oh) << 32) + ((u64)ol); + res = bpf_val_to_ring(data, offset); + if (res != PPM_SUCCESS) + return res; + + /* + * whence + */ + val = bpf_syscall_get_argument(data, 4); + flags = lseek_whence_to_scap(val); + res = bpf_val_to_ring(data, flags); + + return res; +} + +FILLER(sys_eventfd_e, true) +{ + unsigned long val; + int res; + + /* + * initval + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * flags + * XXX not implemented yet + */ + res = bpf_val_to_ring(data, 0); + + return res; +} + +FILLER(sys_mount_e, true) +{ + unsigned long val; + int res; + + /* + * Fix mount flags in arg 3. + * See http://lxr.free-electrons.com/source/fs/namespace.c?v=4.2#L2650 + */ + val = bpf_syscall_get_argument(data, 3); + if ((val & PPM_MS_MGC_MSK) == PPM_MS_MGC_VAL) + val &= ~PPM_MS_MGC_MSK; + + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_ppoll_e, true) +{ + unsigned long val; + int res; + + res = bpf_poll_parse_fds(data, true); + if (res != PPM_SUCCESS) + return res; + + /* + * timeout + */ + val = bpf_syscall_get_argument(data, 2); + + /* NULL timeout specified as 0xFFFFFF.... */ + if (val == (unsigned long)NULL) { + res = bpf_val_to_ring_type(data, (u64)(-1), PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } else { + res = timespec_parse(data, val); + if (res != PPM_SUCCESS) + return res; + } + + /* + * sigmask + */ + val = bpf_syscall_get_argument(data, 3); + if (val != (unsigned long)NULL) + if (bpf_probe_read(&val, sizeof(val), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = bpf_val_to_ring_type(data, val, PT_SIGSET); + + return res; +} + +FILLER(sys_semop_x, true) +{ + unsigned long nsops; + struct sembuf *ptr; + long retval; + int res; + + /* + * return value + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * nsops + * actually this could be read in the enter function but + * we also need to know the value to access the sembuf structs + */ + nsops = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring_type(data, nsops, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * sembuf + */ + ptr = (struct sembuf *)bpf_syscall_get_argument(data, 1); + + if (nsops && ptr) { + int j; + + #pragma unroll 2 + for (j = 0; j < 2; j++) { + struct sembuf sops = {0, 0, 0}; + + if (nsops--) + if (bpf_probe_read(&sops, sizeof(sops), + (void *)&ptr[j])) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = bpf_val_to_ring_type(data, sops.sem_num, PT_UINT16); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring_type(data, sops.sem_op, PT_INT16); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring_type(data, semop_flags_to_scap(sops.sem_flg), PT_FLAGS16); + if (res != PPM_SUCCESS) + return res; + } + } + + return res; +} + +FILLER(sys_socket_x, true) +{ + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + if (retval > 0 && + !data->settings->socket_file_ops) { + struct file *file = bpf_fget(retval); + + if (file) { + const struct file_operations *f_op = _READ(file->f_op); + + data->settings->socket_file_ops = (void *)f_op; + } + } + + return res; +} + +FILLER(sys_flock_e, true) +{ + unsigned int flags; + unsigned long val; + int res; + + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + val = bpf_syscall_get_argument(data, 1); + flags = flock_flags_to_scap(val); + res = bpf_val_to_ring(data, flags); + + return res; +} + +FILLER(sys_pread64_e, true) +{ +#ifndef _64BIT_ARGS_SINGLE_REGISTER +#error Implement this +#endif + return PPM_FAILURE_BUG; +} + +FILLER(sys_preadv64_e, true) +{ +#ifndef _64BIT_ARGS_SINGLE_REGISTER +#error Implement this +#endif + return PPM_FAILURE_BUG; +} + +FILLER(sys_pwrite64_e, true) +{ +#ifndef _64BIT_ARGS_SINGLE_REGISTER +#error Implement this +#endif + return PPM_FAILURE_BUG; +} + +FILLER(sys_renameat_x, true) +{ + unsigned long val; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * olddirfd + */ + val = bpf_syscall_get_argument(data, 0); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * oldpath + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * newdirfd + */ + val = bpf_syscall_get_argument(data, 2); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * newpath + */ + val = bpf_syscall_get_argument(data, 3); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_renameat2_x, true) +{ + unsigned long val; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * olddirfd + */ + val = bpf_syscall_get_argument(data, 0); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * oldpath + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * newdirfd + */ + val = bpf_syscall_get_argument(data, 2); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * newpath + */ + val = bpf_syscall_get_argument(data, 3); + res = bpf_val_to_ring(data, val); + + /* + * flags + */ + val = bpf_syscall_get_argument(data, 4); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_symlinkat_x, true) +{ + unsigned long val; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * oldpath + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring_type(data, val, PT_CHARBUF); + if (res != PPM_SUCCESS) + return res; + + /* + * newdirfd + */ + val = bpf_syscall_get_argument(data, 1); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring_type(data, val, PT_FD); + if (res != PPM_SUCCESS) + return res; + + /* + * newpath + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring_type(data, val, PT_CHARBUF); + + return res; +} + +FILLER(sys_sysdigevent_e, false) +{ + bpf_printk("f_sys_sysdigevent_e should never be called\n"); + return PPM_FAILURE_BUG; +} + +FILLER(cpu_hotplug_e, false) +{ + int res; + + res = bpf_val_to_ring(data, data->state->hotplug_cpu); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring(data, 0); + if (res != PPM_SUCCESS) + return res; + + data->state->hotplug_cpu = 0; + + return res; +} + +FILLER(sched_drop, false) +{ + int res; + + /* + * ratio + */ + res = bpf_val_to_ring(data, data->settings->sampling_ratio); + + return res; +} + +FILLER(sys_procexit_e, false) +{ + struct task_struct *task; + unsigned int flags; + int exit_code; + int res; + + task = (struct task_struct *)bpf_get_current_task(); + + exit_code = _READ(task->exit_code); + + res = bpf_val_to_ring(data, exit_code); + if (res != PPM_SUCCESS) + return res; + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS + delete_args(); +#endif + return res; +} + +FILLER(sched_switch_e, false) +{ + struct sched_switch_args *ctx; + struct task_struct *task; + unsigned long total_vm; + unsigned long maj_flt; + unsigned long min_flt; + struct mm_struct *mm; + pid_t next_pid; + long total_rss; + long swap; + int res; + + ctx = (struct sched_switch_args *)data->ctx; +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS + struct task_struct *next_task = (struct task_struct *)ctx->next; + + next_pid = _READ(next_task->pid); +#else + next_pid = ctx->next_pid; +#endif + + /* + * next + */ + res = bpf_val_to_ring_type(data, next_pid, PT_PID); + if (res != PPM_SUCCESS) + return res; + + task = (struct task_struct *)bpf_get_current_task(); + + /* + * pgft_maj + */ + maj_flt = _READ(task->maj_flt); + res = bpf_val_to_ring_type(data, maj_flt, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + /* + * pgft_min + */ + min_flt = _READ(task->min_flt); + res = bpf_val_to_ring_type(data, min_flt, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + total_vm = 0; + total_rss = 0; + swap = 0; + + mm = _READ(task->mm); + if (mm) { + total_vm = _READ(mm->total_vm); + total_vm <<= (PAGE_SHIFT - 10); + total_rss = bpf_get_mm_rss(mm) << (PAGE_SHIFT - 10); + swap = bpf_get_mm_swap(mm) << (PAGE_SHIFT - 10); + } + + /* + * vm_size + */ + res = bpf_val_to_ring_type(data, total_vm, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * vm_rss + */ + res = bpf_val_to_ring_type(data, total_rss, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * vm_swap + */ + res = bpf_val_to_ring_type(data, swap, PT_UINT32); + + return res; +} + +FILLER(sys_pagefault_e, false) +{ + struct page_fault_args *ctx; + unsigned long error_code; + unsigned long address; + unsigned long ip; + u32 flags; + int res; + + ctx = (struct page_fault_args *)data->ctx; +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS + struct pt_regs *regs = (struct pt_regs *)ctx->regs; + + address = ctx->address; + ip = _READ(regs->ip); + error_code = ctx->error_code; +#else + address = ctx->address; + ip = ctx->ip; + error_code = ctx->error_code; +#endif + + res = bpf_val_to_ring(data, address); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring(data, ip); + if (res != PPM_SUCCESS) + return res; + + flags = pf_flags_to_scap(error_code); + res = bpf_val_to_ring(data, flags); + + return res; +} + +static __always_inline int siginfo_not_a_pointer(struct siginfo* info) +{ +#ifdef SEND_SIG_FORCED + return info == SEND_SIG_NOINFO || info == SEND_SIG_PRIV || SEND_SIG_FORCED; +#else + return info == (struct siginfo*)SEND_SIG_NOINFO || info == (struct siginfo*)SEND_SIG_PRIV; +#endif +} + +FILLER(sys_signaldeliver_e, false) +{ + struct signal_deliver_args *ctx; + pid_t spid = 0; + int sig; + int res; + + ctx = (struct signal_deliver_args *)data->ctx; +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS + struct siginfo *info = (struct siginfo *)ctx->info; + sig = ctx->sig; + + if (siginfo_not_a_pointer(info)) { + info = NULL; + spid = 0; + } else if (sig == SIGKILL) { + spid = _READ(info->_sifields._kill._pid); + } else if (sig == SIGTERM || sig == SIGHUP || sig == SIGINT || + sig == SIGTSTP || sig == SIGQUIT) { + int si_code = _READ(info->si_code); + + if (si_code == SI_USER || + si_code == SI_QUEUE || + si_code <= 0) { + spid = _READ(info->si_pid); + } + } else if (sig == SIGCHLD) { + spid = _READ(info->_sifields._sigchld._pid); + } else if (sig >= SIGRTMIN && sig <= SIGRTMAX) { + spid = _READ(info->_sifields._rt._pid); + } +#else + sig = ctx->sig; +#endif + + /* + * source pid + */ + res = bpf_val_to_ring(data, spid); + if (res != PPM_SUCCESS) + return res; + + /* + * destination pid + */ + res = bpf_val_to_ring(data, bpf_get_current_pid_tgid() & 0xffffffff); + if (res != PPM_SUCCESS) + return res; + + /* + * signal number + */ + res = bpf_val_to_ring(data, sig); + + return res; +} + +FILLER(sys_quotactl_e, true) +{ + unsigned long val; + int res; + + u32 id; + u8 quota_fmt; + u16 cmd; + + /* + * extract cmd + */ + val = bpf_syscall_get_argument(data, 0); + cmd = quotactl_cmd_to_scap(val); + res = bpf_val_to_ring_type(data, cmd, PT_FLAGS16); + if (res != PPM_SUCCESS) + return res; + + /* + * extract type + */ + res = bpf_val_to_ring_type(data, quotactl_type_to_scap(val), PT_FLAGS8); + if (res != PPM_SUCCESS) + return res; + + /* + * extract id + */ + id = 0; + val = bpf_syscall_get_argument(data, 2); + if (cmd == PPM_Q_GETQUOTA || + cmd == PPM_Q_SETQUOTA || + cmd == PPM_Q_XGETQUOTA || + cmd == PPM_Q_XSETQLIM) { + /* + * in this case id represent an userid or groupid so add it + */ + id = val; + } + res = bpf_val_to_ring_type(data, id, PT_UINT32); + if (res != PPM_SUCCESS) + return res; + + /* + * extract quota_fmt from id + */ + quota_fmt = PPM_QFMT_NOT_USED; + if (cmd == PPM_Q_QUOTAON) + quota_fmt = quotactl_fmt_to_scap(val); + + res = bpf_val_to_ring_type(data, quota_fmt, PT_FLAGS8); + + return res; +} + +FILLER(sys_quotactl_x, true) +{ + struct if_dqinfo dqinfo = {0}; + struct if_dqblk dqblk = {0}; + const char empty[] = ""; + u32 quota_fmt_out; + unsigned long val; + long retval; + int res; + u16 cmd; + + /* + * extract cmd + */ + val = bpf_syscall_get_argument(data, 0); + cmd = quotactl_cmd_to_scap(val); + + /* + * return value + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + /* + * Add special + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring_type(data, val, PT_CHARBUF); + if (res != PPM_SUCCESS) + return res; + + /* + * get addr + */ + val = bpf_syscall_get_argument(data, 3); + + /* + * get quotafilepath only for QUOTAON + */ + if (cmd == PPM_Q_QUOTAON) { + res = bpf_val_to_ring_type(data, val, PT_CHARBUF); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, (unsigned long)empty, PT_CHARBUF); + if (res != PPM_SUCCESS) + return res; + } + + /* + * dqblk fields if present + */ + if (cmd == PPM_Q_GETQUOTA || cmd == PPM_Q_SETQUOTA) { + if (bpf_probe_read(&dqblk, sizeof(dqblk), + (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + } + if (dqblk.dqb_valid & QIF_BLIMITS) { + res = bpf_val_to_ring_type(data, dqblk.dqb_bhardlimit, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring_type(data, dqblk.dqb_bsoftlimit, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring_type(data, 0, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + } + + if (dqblk.dqb_valid & QIF_SPACE) { + res = bpf_val_to_ring_type(data, dqblk.dqb_curspace, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + } + + if (dqblk.dqb_valid & QIF_ILIMITS) { + res = bpf_val_to_ring_type(data, dqblk.dqb_ihardlimit, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + res = bpf_val_to_ring_type(data, dqblk.dqb_isoftlimit, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + res = bpf_val_to_ring_type(data, 0, PT_UINT64); + if (res != PPM_SUCCESS) + return res; + } + + if (dqblk.dqb_valid & QIF_BTIME) { + res = bpf_val_to_ring_type(data, dqblk.dqb_btime, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } + + if (dqblk.dqb_valid & QIF_ITIME) { + res = bpf_val_to_ring_type(data, dqblk.dqb_itime, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } + + /* + * dqinfo fields if present + */ + if (cmd == PPM_Q_GETINFO || cmd == PPM_Q_SETINFO) { + if (bpf_probe_read(&dqinfo, sizeof(dqinfo), + (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + } + + if (dqinfo.dqi_valid & IIF_BGRACE) { + res = bpf_val_to_ring_type(data, dqinfo.dqi_bgrace, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } + + if (dqinfo.dqi_valid & IIF_IGRACE) { + res = bpf_val_to_ring_type(data, dqinfo.dqi_igrace, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_RELTIME); + if (res != PPM_SUCCESS) + return res; + } + + if (dqinfo.dqi_valid & IIF_FLAGS) { + res = bpf_val_to_ring_type(data, dqinfo.dqi_flags, PT_FLAGS8); + if (res != PPM_SUCCESS) + return res; + } else { + res = bpf_val_to_ring_type(data, 0, PT_FLAGS8); + if (res != PPM_SUCCESS) + return res; + } + + quota_fmt_out = PPM_QFMT_NOT_USED; + if (cmd == PPM_Q_GETFMT) { + u32 tmp; + + if (bpf_probe_read(&tmp, sizeof(tmp), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + quota_fmt_out = quotactl_fmt_to_scap(tmp); + } + + res = bpf_val_to_ring_type(data, quota_fmt_out, PT_FLAGS8); + + return res; +} + +FILLER(sys_semget_e, true) +{ + unsigned long val; + int res; + + /* + * key + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * nsems + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * semflg + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, semget_flags_to_scap(val)); + + return res; +} + +FILLER(sys_semctl_e, true) +{ + unsigned long val; + int res; + + /* + * semid + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * semnum + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * cmd + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, semctl_cmd_to_scap(val)); + if (res != PPM_SUCCESS) + return res; + + /* + * optional argument semun/val + */ + if (val == SETVAL) + val = bpf_syscall_get_argument(data, 3); + else + val = 0; + + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_ptrace_e, true) +{ + unsigned long val; + int res; + + /* + * request + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, ptrace_requests_to_scap(val)); + if (res != PPM_SUCCESS) + return res; + + /* + * pid + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + + return res; +} + +static __always_inline int bpf_parse_ptrace_addr(struct filler_data *data, u16 request) +{ + enum ppm_param_type type; + unsigned long val; + u8 idx; + + val = bpf_syscall_get_argument(data, 2); + switch (request) { + default: + idx = PPM_PTRACE_IDX_UINT64; + type = PT_UINT64; + } + + return bpf_val_to_ring_dyn(data, val, type, idx); +} + +static __always_inline int bpf_parse_ptrace_data(struct filler_data *data, u16 request) +{ + enum ppm_param_type type; + unsigned long val; + u64 dst; + u8 idx; + + val = bpf_syscall_get_argument(data, 3); + switch (request) { + case PPM_PTRACE_PEEKTEXT: + case PPM_PTRACE_PEEKDATA: + case PPM_PTRACE_PEEKUSR: + idx = PPM_PTRACE_IDX_UINT64; + type = PT_UINT64; + if (bpf_probe_read(&dst, sizeof(long), (void *)val)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + break; + case PPM_PTRACE_CONT: + case PPM_PTRACE_SINGLESTEP: + case PPM_PTRACE_DETACH: + case PPM_PTRACE_SYSCALL: + idx = PPM_PTRACE_IDX_SIGTYPE; + type = PT_SIGTYPE; + dst = (u64)val; + break; + case PPM_PTRACE_ATTACH: + case PPM_PTRACE_TRACEME: + case PPM_PTRACE_POKETEXT: + case PPM_PTRACE_POKEDATA: + case PPM_PTRACE_POKEUSR: + default: + idx = PPM_PTRACE_IDX_UINT64; + type = PT_UINT64; + dst = (u64)val; + break; + } + + return bpf_val_to_ring_dyn(data, dst, type, idx); +} + +FILLER(sys_ptrace_x, true) +{ + unsigned long val; + u16 request; + long retval; + int res; + + /* + * res + */ + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring_type(data, retval, PT_ERRNO); + if (res != PPM_SUCCESS) + return res; + + if (retval < 0) { + res = bpf_val_to_ring_dyn(data, 0, PT_UINT64, 0); + if (res != PPM_SUCCESS) + return res; + + res = bpf_val_to_ring_dyn(data, 0, PT_UINT64, 0); + + return res; + } + + val = bpf_syscall_get_argument(data, 0); + request = ptrace_requests_to_scap(val); + + res = bpf_parse_ptrace_addr(data, request); + if (res != PPM_SUCCESS) + return res; + + res = bpf_parse_ptrace_data(data, request); + + return res; +} + +FILLER(sys_bpf_x, true) +{ + unsigned long cmd; + s64 retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + cmd = bpf_syscall_get_argument(data, 0); + + /* + * fd, depending on cmd + */ + if (retval >= 0 && (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD)) + res = bpf_val_to_ring_dyn(data, retval, PT_FD, PPM_BPF_IDX_FD); + else + res = bpf_val_to_ring_dyn(data, retval, PT_ERRNO, PPM_BPF_IDX_RES); + + return res; +} + +FILLER(sys_unlinkat_x, true) +{ + unsigned long val; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * dirfd + */ + val = bpf_syscall_get_argument(data, 0); + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * name + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * flags + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, unlinkat_flags_to_scap(val)); + + return res; +} + +FILLER(sys_mkdirat_x, true) +{ + unsigned long val; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * dirfd + */ + val = bpf_syscall_get_argument(data, 0); + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * path + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * mode + */ + val = bpf_syscall_get_argument(data, 2); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_linkat_x, true) +{ + unsigned long val; + long retval; + int res; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * olddir + */ + val = bpf_syscall_get_argument(data, 0); + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * oldpath + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * newdir + */ + val = bpf_syscall_get_argument(data, 2); + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * newpath + */ + val = bpf_syscall_get_argument(data, 3); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * flags + */ + val = bpf_syscall_get_argument(data, 4); + res = bpf_val_to_ring(data, linkat_flags_to_scap(val)); + + return res; +} + +FILLER(sys_autofill, true) +{ + const struct ppm_event_entry *evinfo; + int res; + int j; + + evinfo = data->filler_info; + + #pragma unroll + for (j = 0; j < PPM_MAX_AUTOFILL_ARGS; j++) { + struct ppm_autofill_arg arg = evinfo->autofill_args[j]; + unsigned long val; + + if (j == evinfo->n_autofill_args) + break; + + if (arg.id >= 0) + val = bpf_syscall_get_argument(data, arg.id); + else if (arg.id == AF_ID_RETVAL) + val = bpf_syscall_get_retval(data->ctx); + else if (arg.id == AF_ID_USEDEFAULT) + val = arg.default_val; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + } + + return res; +} + +FILLER(sys_fchmodat_x, true) +{ + unsigned long val; + int res; + long retval; + unsigned int mode; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * dirfd + */ + val = bpf_syscall_get_argument(data, 0); + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * filename + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * mode + */ + mode = bpf_syscall_get_argument(data, 2); + mode = chmod_mode_to_scap(mode); + res = bpf_val_to_ring(data, mode); + + return res; +} + +FILLER(sys_chmod_x, true) +{ + unsigned long val; + int res; + long retval; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * filename + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * mode + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + + return res; +} + +FILLER(sys_fchmod_x, true) +{ + unsigned long val; + int res; + long retval; + + retval = bpf_syscall_get_retval(data->ctx); + res = bpf_val_to_ring(data, retval); + if (res != PPM_SUCCESS) + return res; + + /* + * fd + */ + val = bpf_syscall_get_argument(data, 0); + res = bpf_val_to_ring(data, val); + if (res != PPM_SUCCESS) + return res; + + /* + * mode + */ + val = bpf_syscall_get_argument(data, 1); + res = bpf_val_to_ring(data, val); + + return res; +} + +#endif diff --git a/driver/bpf/maps.h b/driver/bpf/maps.h new file mode 100644 index 0000000000..160a2ae90a --- /dev/null +++ b/driver/bpf/maps.h @@ -0,0 +1,93 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __MAPS_H +#define __MAPS_H + +#include "types.h" + +struct bpf_map_def __bpf_section("maps") perf_map = { + .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(u32), + .max_entries = 0, +}; + +struct bpf_map_def __bpf_section("maps") tail_map = { + .type = BPF_MAP_TYPE_PROG_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(u32), + .max_entries = PPM_FILLER_MAX, +}; + +struct bpf_map_def __bpf_section("maps") syscall_code_routing_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(u64), + .max_entries = SYSCALL_TABLE_SIZE, +}; + +struct bpf_map_def __bpf_section("maps") syscall_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(struct syscall_evt_pair), + .max_entries = SYSCALL_TABLE_SIZE, +}; + +struct bpf_map_def __bpf_section("maps") event_info_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(struct ppm_event_info), + .max_entries = PPM_EVENT_MAX, +}; + +struct bpf_map_def __bpf_section("maps") fillers_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(struct ppm_event_entry), + .max_entries = PPM_EVENT_MAX, +}; + +struct bpf_map_def __bpf_section("maps") frame_scratch_map = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = SCRATCH_SIZE, + .max_entries = 0, +}; + +struct bpf_map_def __bpf_section("maps") tmp_scratch_map = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = SCRATCH_SIZE, + .max_entries = 0, +}; + +struct bpf_map_def __bpf_section("maps") settings_map = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(struct sysdig_bpf_settings), + .max_entries = 1, +}; + +struct bpf_map_def __bpf_section("maps") local_state_map = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(struct sysdig_bpf_per_cpu_state), + .max_entries = 0, +}; + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS +struct bpf_map_def __bpf_section("maps") stash_map = { + .type = BPF_MAP_TYPE_HASH, + .key_size = sizeof(u64), + .value_size = sizeof(struct sys_stash_args), + .max_entries = 65535, +}; +#endif + +#endif diff --git a/driver/bpf/plumbing_helpers.h b/driver/bpf/plumbing_helpers.h new file mode 100644 index 0000000000..5a4f7bb2fb --- /dev/null +++ b/driver/bpf/plumbing_helpers.h @@ -0,0 +1,484 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __PLUMBING_HELPERS_H +#define __PLUMBING_HELPERS_H + +#include +#include +#include + +#include "types.h" + +#define _READ(P) ({ typeof(P) _val; \ + memset(&_val, 0, sizeof(_val)); \ + bpf_probe_read(&_val, sizeof(_val), &P); \ + _val; \ + }) + +#ifdef BPF_DEBUG +#define bpf_printk(fmt, ...) \ + do { \ + char s[] = fmt; \ + bpf_trace_printk(s, sizeof(s), ##__VA_ARGS__); \ + } while (0) +#else +#define bpf_printk(fmt, ...) +#endif + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS +static __always_inline int __stash_args(unsigned long long id, + unsigned long *args) +{ + int ret = bpf_map_update_elem(&stash_map, &id, args, BPF_ANY); + + if (ret) + bpf_printk("error stashing arguments for %d:%d\n", id, ret); + + return ret; +} + +static __always_inline int stash_args(unsigned long *args) +{ + unsigned long long id = bpf_get_current_pid_tgid() & 0xffffffff; + + return __stash_args(id, args); +} + +static __always_inline unsigned long *__unstash_args(unsigned long long id) +{ + struct sys_stash_args *args; + + args = bpf_map_lookup_elem(&stash_map, &id); + if (!args) + return NULL; + + return args->args; +} + +static __always_inline unsigned long *unstash_args(void) +{ + unsigned long long id = bpf_get_current_pid_tgid() & 0xffffffff; + + return __unstash_args(id); +} + +static __always_inline void delete_args(void) +{ + unsigned long long id = bpf_get_current_pid_tgid() & 0xffffffff; + + bpf_map_delete_elem(&stash_map, &id); +} +#endif + +/* Can be called just from an exit event + */ +static __always_inline long bpf_syscall_get_retval(void *ctx) +{ + struct sys_exit_args *args = (struct sys_exit_args *)ctx; + + return args->ret; +} + +/* Can be called from both enter and exit event, id is at the same + * offset in both struct sys_enter_args and struct sys_exit_args + */ +static __always_inline long bpf_syscall_get_nr(void *ctx) +{ + struct sys_enter_args *args = (struct sys_enter_args *)ctx; + long id; + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS + struct pt_regs *regs = (struct pt_regs *)args->regs; + + id = _READ(regs->orig_ax); +#else + id = args->id; +#endif + + return id; +} + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS +static __always_inline unsigned long bpf_syscall_get_argument_from_args(unsigned long *args, + int idx) +{ + unsigned long arg; + + if (idx <= 5) + arg = args[idx]; + else + arg = 0; + + return arg; +} +#endif + +static __always_inline unsigned long bpf_syscall_get_argument_from_ctx(void *ctx, + int idx) +{ + unsigned long arg; + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS + struct sys_enter_args *args = (struct sys_enter_args *)ctx; + struct pt_regs *regs = (struct pt_regs *)args->regs; + + switch (idx) { + case 0: + arg = _READ(regs->di); + break; + case 1: + arg = _READ(regs->si); + break; + case 2: + arg = _READ(regs->dx); + break; + case 3: + arg = _READ(regs->r10); + break; + case 4: + arg = _READ(regs->r8); + break; + case 5: + arg = _READ(regs->r9); + break; + default: + arg = 0; + } +#else + unsigned long *args = unstash_args(); + + if (args) + arg = bpf_syscall_get_argument_from_args(args, idx); + else + arg = 0; +#endif + + return arg; +} + +static __always_inline unsigned long bpf_syscall_get_argument(struct filler_data *data, + int idx) +{ +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS + return bpf_syscall_get_argument_from_ctx(data->ctx, idx); +#else + return bpf_syscall_get_argument_from_args(data->args, idx); +#endif +} + +static __always_inline char *get_frame_scratch_area(unsigned int cpu) +{ + char *scratchp; + + scratchp = bpf_map_lookup_elem(&frame_scratch_map, &cpu); + if (!scratchp) + bpf_printk("frame scratch NULL\n"); + + return scratchp; +} + +static __always_inline char *get_tmp_scratch_area(unsigned int cpu) +{ + char *scratchp; + + scratchp = bpf_map_lookup_elem(&tmp_scratch_map, &cpu); + if (!scratchp) + bpf_printk("tmp scratch NULL\n"); + + return scratchp; +} + +static __always_inline const struct syscall_evt_pair *get_syscall_info(int id) +{ + const struct syscall_evt_pair *p = + bpf_map_lookup_elem(&syscall_table, &id); + + if (!p) + bpf_printk("no syscall_info for %d\n", id); + + return p; +} + +static __always_inline const struct ppm_event_info *get_event_info(enum ppm_event_type event_type) +{ + const struct ppm_event_info *e = + bpf_map_lookup_elem(&event_info_table, &event_type); + + if (!e) + bpf_printk("no event info for %d\n", event_type); + + return e; +} + +static __always_inline const struct ppm_event_entry *get_event_filler_info(enum ppm_event_type event_type) +{ + const struct ppm_event_entry *e; + + e = bpf_map_lookup_elem(&fillers_table, &event_type); + if (!e) + bpf_printk("no filler info for %d\n", event_type); + + return e; +} + +static __always_inline struct sysdig_bpf_settings *get_bpf_settings(void) +{ + struct sysdig_bpf_settings *settings; + int id = 0; + + settings = bpf_map_lookup_elem(&settings_map, &id); + if (!settings) + bpf_printk("settings NULL\n"); + + return settings; +} + +static __always_inline struct sysdig_bpf_per_cpu_state *get_local_state(unsigned int cpu) +{ + struct sysdig_bpf_per_cpu_state *state; + + state = bpf_map_lookup_elem(&local_state_map, &cpu); + if (!state) + bpf_printk("state NULL\n"); + + return state; +} + +static __always_inline bool acquire_local_state(struct sysdig_bpf_per_cpu_state *state) +{ + if (state->in_use) { + bpf_printk("acquire_local_state: already in use\n"); + return false; + } + + state->in_use = true; + return true; +} + +static __always_inline bool release_local_state(struct sysdig_bpf_per_cpu_state *state) +{ + if (!state->in_use) { + bpf_printk("release_local_state: already not in use\n"); + return false; + } + + state->in_use = false; + return true; +} + +static __always_inline int init_filler_data(void *ctx, + struct filler_data *data, + bool is_syscall) +{ + unsigned int cpu; + + data->ctx = ctx; + + data->settings = get_bpf_settings(); + if (!data->settings) + return PPM_FAILURE_BUG; + + cpu = bpf_get_smp_processor_id(); + + data->buf = get_frame_scratch_area(cpu); + if (!data->buf) + return PPM_FAILURE_BUG; + + data->state = get_local_state(cpu); + if (!data->state) + return PPM_FAILURE_BUG; + + data->tmp_scratch = get_tmp_scratch_area(cpu); + if (!data->tmp_scratch) + return PPM_FAILURE_BUG; + + data->evt = get_event_info(data->state->tail_ctx.evt_type); + if (!data->evt) + return PPM_FAILURE_BUG; + + data->filler_info = get_event_filler_info(data->state->tail_ctx.evt_type); + if (!data->filler_info) + return PPM_FAILURE_BUG; + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS + if (is_syscall) { + data->args = unstash_args(); + if (!data->args) + return PPM_SKIP_EVENT; + } +#endif + + data->curarg_already_on_frame = false; + data->fd = -1; + + return PPM_SUCCESS; +} + +static __always_inline int bpf_test_bit(int nr, unsigned long *addr) +{ + return 1UL & (_READ(addr[BIT_WORD(nr)]) >> (nr & (BITS_PER_LONG - 1))); +} + +static __always_inline bool drop_event(void *ctx, + struct sysdig_bpf_per_cpu_state *state, + enum ppm_event_type evt_type, + struct sysdig_bpf_settings *settings, + enum syscall_flags drop_flags) +{ + if (!settings->dropping_mode) + return false; + + switch (evt_type) { + case PPME_SYSCALL_CLOSE_X: + case PPME_SOCKET_BIND_X: { + long ret = bpf_syscall_get_retval(ctx); + + if (ret < 0) + return true; + + break; + } + case PPME_SYSCALL_CLOSE_E: { + struct sys_enter_args *args; + struct files_struct *files; + struct task_struct *task; + unsigned long *open_fds; + struct fdtable *fdt; + int close_fd; + int max_fds; + + close_fd = bpf_syscall_get_argument_from_ctx(ctx, 0); + if (close_fd < 0) + return true; + + task = (struct task_struct *)bpf_get_current_task(); + if (!task) + break; + + files = _READ(task->files); + if (!files) + break; + + fdt = _READ(files->fdt); + if (!fdt) + break; + + max_fds = _READ(fdt->max_fds); + if (close_fd >= max_fds) + return true; + + open_fds = _READ(fdt->open_fds); + if (!open_fds) + break; + + if (!bpf_test_bit(close_fd, open_fds)) + return true; + + break; + } + case PPME_SYSCALL_FCNTL_E: + case PPME_SYSCALL_FCNTL_X: { + long cmd = bpf_syscall_get_argument_from_ctx(ctx, 1); + + if (cmd != F_DUPFD && cmd != F_DUPFD_CLOEXEC) + return true; + + break; + } + default: + break; + } + + if (drop_flags & UF_NEVER_DROP) + return false; + + if (drop_flags & UF_ALWAYS_DROP) + return true; + + if (state->tail_ctx.ts % 1000000000 >= 1000000000 / + settings->sampling_ratio) { + if (!settings->is_dropping) { + settings->is_dropping = true; + state->tail_ctx.evt_type = PPME_DROP_E; + return false; + } + + return true; + } + + if (settings->is_dropping) { + settings->is_dropping = false; + state->tail_ctx.evt_type = PPME_DROP_X; + return false; + } + + return false; +} + +static __always_inline void reset_tail_ctx(struct sysdig_bpf_per_cpu_state *state, + enum ppm_event_type evt_type, + unsigned long long ts) +{ + state->tail_ctx.evt_type = evt_type; + state->tail_ctx.ts = ts; + state->tail_ctx.curarg = 0; + state->tail_ctx.curoff = 0; + state->tail_ctx.len = 0; + state->tail_ctx.prev_res = 0; +} + +static __always_inline void call_filler(void *ctx, + void *stack_ctx, + enum ppm_event_type evt_type, + struct sysdig_bpf_settings *settings, + enum syscall_flags drop_flags) +{ + const struct ppm_event_entry *filler_info; + struct sysdig_bpf_per_cpu_state *state; + unsigned long long pid; + unsigned long long ts; + unsigned int cpu; + + cpu = bpf_get_smp_processor_id(); + + state = get_local_state(cpu); + if (!state) + return; + + if (!acquire_local_state(state)) + return; + + if (cpu == 0 && state->hotplug_cpu != 0) { + evt_type = PPME_CPU_HOTPLUG_E; + drop_flags = UF_NEVER_DROP; + } + + ts = settings->boot_time + bpf_ktime_get_ns(); + reset_tail_ctx(state, evt_type, ts); + + /* drop_event can change state->tail_ctx.evt_type */ + if (drop_event(stack_ctx, state, evt_type, settings, drop_flags)) + goto cleanup; + + ++state->n_evts; + + filler_info = get_event_filler_info(state->tail_ctx.evt_type); + if (!filler_info) + goto cleanup; + + bpf_tail_call(ctx, &tail_map, filler_info->filler_id); + bpf_printk("Can't tail call filler evt=%d, filler=%d\n", + state->tail_ctx.evt_type, + filler_info->filler_id); + +cleanup: + release_local_state(state); +} + +#endif diff --git a/driver/bpf/probe.c b/driver/bpf/probe.c new file mode 100644 index 0000000000..a9f97295b5 --- /dev/null +++ b/driver/bpf/probe.c @@ -0,0 +1,245 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#include "quirks.h" + +#include +#include +#include + +#include "../driver_config.h" +#include "../ppm_events_public.h" +#include "bpf_helpers.h" +#include "types.h" +#include "maps.h" +#include "plumbing_helpers.h" +#include "ring_helpers.h" +#include "filler_helpers.h" +#include "fillers.h" + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +#define BPF_PROBE(prefix, event, type) \ +__bpf_section(TP_NAME #event) \ +int bpf_##event(struct type *ctx) +#else +#define BPF_PROBE(prefix, event, type) \ +__bpf_section(TP_NAME prefix #event) \ +int bpf_##event(struct type *ctx) +#endif + +BPF_PROBE("raw_syscalls/", sys_enter, sys_enter_args) +{ + const struct syscall_evt_pair *sc_evt; + struct sysdig_bpf_settings *settings; + enum ppm_event_type evt_type; + int drop_flags; + long id; + + if (bpf_in_ia32_syscall()) + return 0; + + id = bpf_syscall_get_nr(ctx); + if (id < 0 || id >= SYSCALL_TABLE_SIZE) + return 0; + + settings = get_bpf_settings(); + if (!settings) + return 0; + + if (!settings->capture_enabled) + return 0; + + sc_evt = get_syscall_info(id); + if (!sc_evt) + return 0; + + if (sc_evt->flags & UF_USED) { + evt_type = sc_evt->enter_event_type; + drop_flags = sc_evt->flags; + } else { + evt_type = PPME_GENERIC_E; + drop_flags = UF_ALWAYS_DROP; + } + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS + call_filler(ctx, ctx, evt_type, settings, drop_flags); +#else + /* Duplicated here to avoid verifier madness */ + struct sys_enter_args stack_ctx; + + memcpy(stack_ctx.args, ctx->args, sizeof(ctx->args)); + if (stash_args(stack_ctx.args)) + return 0; + + call_filler(ctx, &stack_ctx, evt_type, settings, drop_flags); +#endif + return 0; +} + +BPF_PROBE("raw_syscalls/", sys_exit, sys_exit_args) +{ + const struct syscall_evt_pair *sc_evt; + struct sysdig_bpf_settings *settings; + enum ppm_event_type evt_type; + int drop_flags; + long id; + + if (bpf_in_ia32_syscall()) + return 0; + + id = bpf_syscall_get_nr(ctx); + if (id < 0 || id >= SYSCALL_TABLE_SIZE) + return 0; + + settings = get_bpf_settings(); + if (!settings) + return 0; + + if (!settings->capture_enabled) + return 0; + + sc_evt = get_syscall_info(id); + if (!sc_evt) + return 0; + + if (sc_evt->flags & UF_USED) { + evt_type = sc_evt->exit_event_type; + drop_flags = sc_evt->flags; + } else { + evt_type = PPME_GENERIC_X; + drop_flags = UF_ALWAYS_DROP; + } + + call_filler(ctx, ctx, evt_type, settings, drop_flags); + return 0; +} + +BPF_PROBE("sched/", sched_process_exit, sched_process_exit_args) +{ + struct sysdig_bpf_settings *settings; + enum ppm_event_type evt_type; + struct task_struct *task; + unsigned int flags; + + task = (struct task_struct *)bpf_get_current_task(); + + flags = _READ(task->flags); + if (flags & PF_KTHREAD) + return 0; + + settings = get_bpf_settings(); + if (!settings) + return 0; + + if (!settings->capture_enabled) + return 0; + + evt_type = PPME_PROCEXIT_1_E; + + call_filler(ctx, ctx, evt_type, settings, UF_NEVER_DROP); + return 0; +} + +BPF_PROBE("sched/", sched_switch, sched_switch_args) +{ + struct sysdig_bpf_settings *settings; + enum ppm_event_type evt_type; + + settings = get_bpf_settings(); + if (!settings) + return 0; + + if (!settings->capture_enabled) + return 0; + + evt_type = PPME_SCHEDSWITCH_6_E; + + call_filler(ctx, ctx, evt_type, settings, 0); + return 0; +} + +static __always_inline int bpf_page_fault(struct page_fault_args *ctx) +{ + struct sysdig_bpf_settings *settings; + enum ppm_event_type evt_type; + + settings = get_bpf_settings(); + if (!settings) + return 0; + + if (!settings->page_faults) + return 0; + + if (!settings->capture_enabled) + return 0; + + evt_type = PPME_PAGE_FAULT_E; + + call_filler(ctx, ctx, evt_type, settings, UF_ALWAYS_DROP); + return 0; +} + +BPF_PROBE("exceptions/", page_fault_user, page_fault_args) +{ + return bpf_page_fault(ctx); +} + +BPF_PROBE("exceptions/", page_fault_kernel, page_fault_args) +{ + return bpf_page_fault(ctx); +} + +BPF_PROBE("signal/", signal_deliver, signal_deliver_args) +{ + struct sysdig_bpf_settings *settings; + enum ppm_event_type evt_type; + + settings = get_bpf_settings(); + if (!settings) + return 0; + + if (!settings->capture_enabled) + return 0; + + evt_type = PPME_SIGNALDELIVER_E; + + call_filler(ctx, ctx, evt_type, settings, UF_ALWAYS_DROP); + return 0; +} + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS +__bpf_section(TP_NAME "sched/sched_process_fork") +int bpf_sched_process_fork(struct sched_process_fork_args *ctx) +{ + struct sysdig_bpf_settings *settings; + enum ppm_event_type evt_type; + struct sys_stash_args args; + unsigned long *argsp; + + settings = get_bpf_settings(); + if (!settings) + return 0; + + if (!settings->capture_enabled) + return 0; + + argsp = __unstash_args(ctx->parent_pid); + if (!argsp) + return 0; + + memcpy(&args, argsp, sizeof(args)); + + __stash_args(ctx->child_pid, args.args); + + return 0; +} +#endif + +char kernel_ver[] __bpf_section("kernel_version") = UTS_RELEASE; + +char probe_ver[] __bpf_section("probe_version") = PROBE_VERSION; diff --git a/driver/bpf/quirks.h b/driver/bpf/quirks.h new file mode 100644 index 0000000000..e8daceb8bd --- /dev/null +++ b/driver/bpf/quirks.h @@ -0,0 +1,47 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __QUIRKS_H +#define __QUIRKS_H + +#include + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0) +#error Kernel version must be >= 4.14 with eBPF enabled +#endif + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 4) +#define randomized_struct_fields_start struct { +#define randomized_struct_fields_end }; +#endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0) +#define BPF_FORBIDS_ZERO_ACCESS +#endif + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 17, 0) +#define BPF_SUPPORTS_RAW_TRACEPOINTS +#endif + +/* Redefine asm_volatile_goto to work around clang not supporting it + */ +#include + +#ifdef asm_volatile_goto +#undef asm_volatile_goto +#define asm_volatile_goto(...) asm volatile("invalid use of asm_volatile_goto") +#endif + +/* Ditto for asm_inline (new in Linux 5.4) + */ +#ifdef asm_inline +#undef asm_inline +#define asm_inline asm +#endif + +#endif diff --git a/driver/bpf/ring_helpers.h b/driver/bpf/ring_helpers.h new file mode 100644 index 0000000000..a296af7d9e --- /dev/null +++ b/driver/bpf/ring_helpers.h @@ -0,0 +1,95 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __RING_HELPERS_H +#define __RING_HELPERS_H + +static __always_inline void write_evt_hdr(struct filler_data *data) +{ + struct ppm_evt_hdr *evt_hdr = (struct ppm_evt_hdr *)data->buf; + + evt_hdr->ts = data->state->tail_ctx.ts; + evt_hdr->tid = bpf_get_current_pid_tgid() & 0xffffffff; + evt_hdr->type = data->state->tail_ctx.evt_type; + evt_hdr->nparams = data->evt->nparams; + + data->state->tail_ctx.curoff = sizeof(struct ppm_evt_hdr) + + sizeof(u16) * data->evt->nparams; + data->state->tail_ctx.len = data->state->tail_ctx.curoff; +} + +static __always_inline void fixup_evt_len(char *p, unsigned long len) +{ + struct ppm_evt_hdr *evt_hdr = (struct ppm_evt_hdr *)p; + + evt_hdr->len = len; +} + +static __always_inline void fixup_evt_arg_len(char *p, + unsigned int argnum, + unsigned int arglen) +{ + volatile unsigned int argnumv = argnum; + *((u16 *)&p[sizeof(struct ppm_evt_hdr)] + (argnumv & (PPM_MAX_EVENT_PARAMS - 1))) = arglen; +} + +static __always_inline int push_evt_frame(void *ctx, + struct filler_data *data) +{ + if (data->state->tail_ctx.curarg != data->evt->nparams) { + bpf_printk("corrupted filler for event type %d (added %u args, should have added %u)\n", + data->state->tail_ctx.evt_type, + data->state->tail_ctx.curarg, + data->evt->nparams); + return PPM_FAILURE_BUG; + } + + if (data->state->tail_ctx.len > PERF_EVENT_MAX_SIZE) + return PPM_FAILURE_BUFFER_FULL; + + fixup_evt_len(data->buf, data->state->tail_ctx.len); + +#ifdef BPF_FORBIDS_ZERO_ACCESS + int res = bpf_perf_event_output(ctx, + &perf_map, + BPF_F_CURRENT_CPU, + data->buf, + ((data->state->tail_ctx.len - 1) & SCRATCH_SIZE_MAX) + 1); +#else + int res = bpf_perf_event_output(ctx, + &perf_map, + BPF_F_CURRENT_CPU, + data->buf, + data->state->tail_ctx.len & SCRATCH_SIZE_MAX); +#endif + if (res == -ENOENT || res == -EOPNOTSUPP) { + /* + * ENOENT = likely a new CPU is online that wasn't + * opened in userspace + * + * EOPNOTSUPP = likely a perf channel has been closed + * because a CPU went offline + * + * Schedule a hotplug event on CPU 0 + */ + struct sysdig_bpf_per_cpu_state *state = get_local_state(0); + + if (!state) + return PPM_FAILURE_BUG; + + state->hotplug_cpu = bpf_get_smp_processor_id(); + bpf_printk("detected hotplug event, cpu=%d\n", state->hotplug_cpu); + } else if (res) { + bpf_printk("bpf_perf_event_output failed, res=%d\n", res); + return PPM_FAILURE_BUG; + } + + return PPM_SUCCESS; +} + +#endif diff --git a/driver/bpf/types.h b/driver/bpf/types.h new file mode 100644 index 0000000000..101e216e44 --- /dev/null +++ b/driver/bpf/types.h @@ -0,0 +1,235 @@ +/* + +Copyright (C) 2020 Sysdig Inc. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#ifndef __TYPES_H +#define __TYPES_H + +#ifdef __KERNEL__ + +#define __bpf_section(NAME) __attribute__((section(NAME), used)) + +#ifndef __always_inline +#define __always_inline inline __attribute__((always_inline)) +#endif + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +#define TP_NAME "raw_tracepoint/" +#else +#define TP_NAME "tracepoint/" +#endif + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +struct sys_enter_args { + unsigned long regs; + unsigned long id; +}; +#else +struct sys_enter_args { + __u64 pad; + long id; + unsigned long args[6]; +}; +#endif + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +struct sys_exit_args { + unsigned long regs; + unsigned long ret; +}; +#else +struct sys_exit_args { + __u64 pad; + long id; + long ret; +}; +#endif + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +struct sched_process_exit_args { + unsigned long p; +}; +#else +struct sched_process_exit_args { + __u64 pad; + char comm[16]; + pid_t pid; + int prio; +}; +#endif + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +struct sched_switch_args { + unsigned long preempt; + unsigned long prev; + unsigned long next; +}; +#else +struct sched_switch_args { + __u64 pad; + char prev_comm[TASK_COMM_LEN]; + pid_t prev_pid; + int prev_prio; + long prev_state; + char next_comm[TASK_COMM_LEN]; + pid_t next_pid; + int next_prio; +}; +#endif + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS +struct sched_process_fork_args { + __u64 pad; + char parent_comm[TASK_COMM_LEN]; + pid_t parent_pid; + char child_comm[TASK_COMM_LEN]; + pid_t child_pid; +}; +#endif + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +struct page_fault_args { + unsigned long address; + unsigned long regs; + unsigned long error_code; +}; +#else +struct page_fault_args { + __u64 pad; + unsigned long address; + unsigned long ip; + unsigned long error_code; +}; +#endif + +#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS +struct signal_deliver_args { + unsigned long sig; + unsigned long info; + unsigned long ka; +}; +#else +struct signal_deliver_args { + __u64 pad; + int sig; + int errno; + int code; + unsigned long sa_handler; + unsigned long sa_flags; +}; +#endif + +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS +struct sys_stash_args { + unsigned long args[6]; +}; +#endif + +struct filler_data { + void *ctx; + struct sysdig_bpf_settings *settings; + struct sysdig_bpf_per_cpu_state *state; + char *tmp_scratch; + const struct ppm_event_info *evt; + const struct ppm_event_entry *filler_info; + bool curarg_already_on_frame; + char *buf; +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS + unsigned long *args; +#endif + int fd; +}; + +struct perf_event_header { + __u32 type; + __u16 misc; + __u16 size; +}; + +struct perf_event_sample { + struct perf_event_header header; + __u32 size; + char data[]; +}; + +/* + * Unfortunately the entire perf event length must fit in u16 + */ +#define PERF_EVENT_MAX_SIZE (0xffff - sizeof(struct perf_event_sample)) + +/* + * Due to the way the verifier works with accessing variable memory, + * the scratch size needs to be at least 2^N > PERF_EVENT_MAX_SIZE * 2 + */ +#define SCRATCH_SIZE (1 << 18) +#define SCRATCH_SIZE_MAX (SCRATCH_SIZE - 1) +#define SCRATCH_SIZE_HALF (SCRATCH_SIZE_MAX >> 1) + +#endif /* __KERNEL__ */ + +struct bpf_map_def { + unsigned int type; + unsigned int key_size; + unsigned int value_size; + unsigned int max_entries; + unsigned int map_flags; + unsigned int inner_map_idx; + unsigned int numa_node; +}; + +enum sysdig_map_types { + SYSDIG_PERF_MAP = 0, + SYSDIG_TAIL_MAP = 1, + SYSDIG_SYSCALL_CODE_ROUTING_TABLE = 2, + SYSDIG_SYSCALL_TABLE = 3, + SYSDIG_EVENT_INFO_TABLE = 4, + SYSDIG_FILLERS_TABLE = 5, + SYSDIG_FRAME_SCRATCH_MAP = 6, + SYSDIG_TMP_SCRATCH_MAP = 7, + SYSDIG_SETTINGS_MAP = 8, + SYSDIG_LOCAL_STATE_MAP = 9, +#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS + SYSDIG_STASH_MAP = 10, +#endif +}; + +struct sysdig_bpf_settings { + uint64_t boot_time; + void *socket_file_ops; + uint32_t snaplen; + uint32_t sampling_ratio; + bool capture_enabled; + bool do_dynamic_snaplen; + bool page_faults; + bool dropping_mode; + bool is_dropping; + bool tracers_enabled; + uint16_t fullcapture_port_range_start; + uint16_t fullcapture_port_range_end; + uint16_t statsd_port; +} __attribute__((packed)); + +struct tail_context { + enum ppm_event_type evt_type; + unsigned long long ts; + unsigned long curarg; + unsigned long curoff; + unsigned long len; + int prev_res; +} __attribute__((packed)); + +struct sysdig_bpf_per_cpu_state { + struct tail_context tail_ctx; + unsigned long long n_evts; + unsigned long long n_drops_buffer; + unsigned long long n_drops_pf; + unsigned long long n_drops_bug; + unsigned int hotplug_cpu; + bool in_use; +} __attribute__((packed)); + +#endif diff --git a/driver/dkms.conf b/driver/dkms.conf deleted file mode 100644 index 91725ded9a..0000000000 --- a/driver/dkms.conf +++ /dev/null @@ -1,5 +0,0 @@ -PACKAGE_NAME="sysdig" -PACKAGE_VERSION="" -BUILT_MODULE_NAME[0]="sysdig-probe" -DEST_MODULE_LOCATION[0]="/kernel/extra" -AUTOINSTALL="yes" diff --git a/driver/dkms.conf.in b/driver/dkms.conf.in new file mode 100644 index 0000000000..afa8ee748b --- /dev/null +++ b/driver/dkms.conf.in @@ -0,0 +1,5 @@ +PACKAGE_NAME="@PACKAGE_NAME@" +PACKAGE_VERSION="@PROBE_VERSION@" +BUILT_MODULE_NAME[0]="@PROBE_NAME@" +DEST_MODULE_LOCATION[0]="/kernel/extra" +AUTOINSTALL="yes" diff --git a/driver/driver_config.h.in b/driver/driver_config.h.in new file mode 100644 index 0000000000..560a0cb23d --- /dev/null +++ b/driver/driver_config.h.in @@ -0,0 +1,19 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#pragma once + +#define PROBE_VERSION "${PROBE_VERSION}" + +#define PROBE_NAME "${PROBE_NAME}" + +#define PROBE_DEVICE_NAME "${PROBE_DEVICE_NAME}" + +#ifndef KBUILD_MODNAME +#define KBUILD_MODNAME PROBE_NAME +#endif diff --git a/driver/dynamic_params_table.c b/driver/dynamic_params_table.c new file mode 100644 index 0000000000..c3f6cb7217 --- /dev/null +++ b/driver/dynamic_params_table.c @@ -0,0 +1,28 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ + +#include "ppm_events_public.h" + +const struct ppm_param_info sockopt_dynamic_param[PPM_SOCKOPT_IDX_MAX] = { + [PPM_SOCKOPT_IDX_UNKNOWN] = {{0}, PT_BYTEBUF, PF_HEX}, + [PPM_SOCKOPT_IDX_ERRNO] = {{0}, PT_ERRNO, PF_DEC}, + [PPM_SOCKOPT_IDX_UINT32] = {{0}, PT_UINT32, PF_DEC}, + [PPM_SOCKOPT_IDX_UINT64] = {{0}, PT_UINT64, PF_DEC}, + [PPM_SOCKOPT_IDX_TIMEVAL] = {{0}, PT_RELTIME, PF_DEC}, +}; + +const struct ppm_param_info ptrace_dynamic_param[PPM_PTRACE_IDX_MAX] = { + [PPM_PTRACE_IDX_UINT64] = {{0}, PT_UINT64, PF_HEX}, + [PPM_PTRACE_IDX_SIGTYPE] = {{0}, PT_SIGTYPE, PF_DEC}, +}; + +const struct ppm_param_info bpf_dynamic_param[PPM_BPF_IDX_MAX] = { + [PPM_BPF_IDX_FD] = {{0}, PT_FD, PF_DEC}, + [PPM_BPF_IDX_RES] = {{0}, PT_ERRNO, PF_DEC}, +}; diff --git a/driver/event_table.c b/driver/event_table.c index 58c42de928..18dc594913 100644 --- a/driver/event_table.c +++ b/driver/event_table.c @@ -1,179 +1,339 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "ppm_events_public.h" -#include "ppm.h" const struct ppm_event_info g_event_info[PPM_EVENT_MAX] = { /* PPME_GENERIC_E */{"syscall", EC_OTHER, EF_NONE, 2, {{"ID", PT_SYSCALLID, PF_DEC}, {"nativeID", PT_UINT16, PF_DEC} } }, /* PPME_GENERIC_X */{"syscall", EC_OTHER, EF_NONE, 1, {{"ID", PT_SYSCALLID, PF_DEC} } }, - /* PPME_SYSCALL_OPEN_E */{"open", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SYSCALL_OPEN_X */{"open", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 4, {{"fd", PT_FD, PF_DEC}, {"name", PT_FSPATH, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_HEX} } }, - /* PPME_SYSCALL_CLOSE_E */{"close", EC_IO_OTHER, (enum ppm_event_flags)(EF_DESTROYS_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_CLOSE_X */{"close", EC_IO_OTHER, (enum ppm_event_flags)(EF_DESTROYS_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_READ_E */{"read", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_READ_X */{"read", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_WRITE_E */{"write", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_WRITE_X */{"write", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_BRK_E */{"brk", EC_MEMORY, EF_NONE, 1, {{"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_BRK_X */{"brk", EC_MEMORY, EF_NONE, 1, {{"res", PT_UINT64, PF_HEX} } }, - /* PPME_SYSCALL_EXECVE_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 0}, - /* PPME_SYSCALL_EXECVE_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 8, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC} } }, - /* PPME_CLONE_E */{"clone", EC_PROCESS, EF_MODIFIES_STATE, 0}, - /* PPME_CLONE_X */{"clone", EC_PROCESS, EF_MODIFIES_STATE, 11, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, - /* PPME_PROCEXIT_E */{"procexit", EC_PROCESS, EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_OPEN_E */{"open", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_OPEN_X */{"open", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE, 5, {{"fd", PT_FD, PF_DEC}, {"name", PT_FSPATH, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_OCT}, {"dev", PT_UINT32, PF_HEX} } }, + /* PPME_SYSCALL_CLOSE_E */{"close", EC_IO_OTHER, EF_DESTROYS_FD | EF_USES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_CLOSE_X */{"close", EC_IO_OTHER, EF_DESTROYS_FD | EF_USES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_READ_E */{"read", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_READ_X */{"read", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_WRITE_E */{"write", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_WRITE_X */{"write", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_BRK_1_E */{"brk", EC_MEMORY, EF_OLD_VERSION, 1, {{"size", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_BRK_1_X */{"brk", EC_MEMORY, EF_OLD_VERSION, 1, {{"res", PT_UINT64, PF_HEX} } }, + /* PPME_SYSCALL_EXECVE_8_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_EXECVE_8_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 8, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC} } }, + /* PPME_CLONE_11_E */{"clone", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_CLONE_11_X */{"clone", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 11, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, + /* PPME_PROCEXIT_E */{"procexit", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, /* PPME_NA1 */{"NA1", EC_PROCESS, EF_UNUSED, 0}, - /* PPME_SOCKET_SOCKET_E */{"socket", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"domain", PT_FLAGS32, PF_DEC, socket_families}, {"type", PT_UINT32, PF_DEC}, {"proto", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_SOCKET_X */{"socket", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_BIND_E */{"bind", EC_NET, EF_USES_FD, 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_BIND_X */{"bind", EC_NET, EF_USES_FD, 2, {{"res", PT_ERRNO, PF_DEC}, {"addr", PT_SOCKADDR, PF_NA} } }, - /* PPME_SOCKET_CONNECT_E */{"connect", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_CONNECT_X */{"connect", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 2, {{"res", PT_ERRNO, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, + /* PPME_SOCKET_SOCKET_E */{"socket", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE, 3, {{"domain", PT_FLAGS32, PF_DEC, socket_families}, {"type", PT_UINT32, PF_DEC}, {"proto", PT_UINT32, PF_DEC} } }, + /* PPME_SOCKET_SOCKET_X */{"socket", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SOCKET_BIND_E */{"bind", EC_NET, EF_USES_FD | EF_MODIFIES_STATE, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SOCKET_BIND_X */{"bind", EC_NET, EF_USES_FD | EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"addr", PT_SOCKADDR, PF_NA} } }, + /* PPME_SOCKET_CONNECT_E */{"connect", EC_NET, EF_USES_FD | EF_MODIFIES_STATE, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SOCKET_CONNECT_X */{"connect", EC_NET, EF_USES_FD | EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, /* PPME_SOCKET_LISTEN_E */{"listen", EC_NET, EF_USES_FD, 2, {{"fd", PT_FD, PF_DEC}, {"backlog", PT_UINT32, PF_DEC} } }, /* PPME_SOCKET_LISTEN_X */{"listen", EC_NET, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SOCKET_ACCEPT_E */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SOCKET_ACCEPT_X */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC} } }, - /* PPME_SYSCALL_SEND_E */{"send", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_SEND_X */{"send", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_SENDTO_E */{"sendto", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_SENDTO_X */{"sendto", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_RECV_E */{"recv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_RECV_X */{"recv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_RECVFROM_E */{"recvfrom", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_RECVFROM_X */{"recvfrom", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_SHUTDOWN_E */{"shutdown", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 2, {{"fd", PT_FD, PF_DEC}, {"how", PT_FLAGS8, PF_HEX, shutdown_how} } }, - /* PPME_SOCKET_SHUTDOWN_X */{"shutdown", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SOCKET_GETSOCKNAME_E */{"getsockname", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETSOCKNAME_X */{"getsockname", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETPEERNAME_E */{"getpeername", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETPEERNAME_X */{"getpeername", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_SOCKETPAIR_E */{"socketpair", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"domain", PT_FLAGS32, PF_DEC, socket_families}, {"type", PT_UINT32, PF_DEC}, {"proto", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_SOECKETPAIR_X */{"socketpair", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 5, {{"res", PT_ERRNO, PF_DEC}, {"fd1", PT_FD, PF_DEC}, {"fd2", PT_FD, PF_DEC}, {"source", PT_UINT64, PF_HEX}, {"peer", PT_UINT64, PF_HEX} } }, - /* PPME_SOCKET_SETSOCKOPT_E */{"setsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_SETSOCKOPT_X */{"setsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETSOCKOPT_E */{"getsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETSOCKOPT_X */{"getsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_SENDMSG_E */{"sendmsg", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_SENDMSG_X */{"sendmsg", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_SENDMMSG_E */{"sendmmsg", EC_IO_WRITE, EF_NONE, 0}, - /* PPME_SOCKET_SENDMMSG_X */{"sendmmsg", EC_IO_WRITE, EF_NONE, 0}, - /* PPME_SOCKET_RECVMSG_E */{"recvmsg", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_RECVMSG_X */{"recvmsg", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 4, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_RECVMMSG_E */{"recvmmsg", EC_IO_READ, EF_NONE, 0}, - /* PPME_SOCKET_RECVMMSG_X */{"recvmmsg", EC_IO_READ, EF_NONE, 0}, - /* PPME_SOCKET_ACCEPT4_E */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"flags", PT_INT32, PF_HEX} } }, - /* PPME_SOCKET_ACCEPT4_X */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC} } }, - /* PPME_SYSCALL_CREAT_E */{"creat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SYSCALL_CREAT_X */{"creat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"name", PT_FSPATH, PF_NA}, {"mode", PT_UINT32, PF_HEX} } }, - /* PPME_SOCKET_PIPE_E */{"pipe", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SOCKET_PIPE_X */{"pipe", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 4, {{"res", PT_ERRNO, PF_DEC}, {"fd1", PT_FD, PF_DEC}, {"fd2", PT_FD, PF_DEC}, {"ino", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_EVENTFD_E */{"eventfd", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 2, {{"initval", PT_UINT64, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX} } }, - /* PPME_SYSCALL_EVENTFD_X */{"eventfd", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_FUTEX_E */{"futex", EC_IPC, EF_NONE, 3, {{"addr", PT_UINT64, PF_HEX}, {"op", PT_FLAGS16, PF_HEX, futex_operations}, {"val", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_FUTEX_X */{"futex", EC_IPC, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_STAT_E */{"stat", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_STAT_X */{"stat", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_LSTAT_E */{"lstat", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_LSTAT_X */{"lstat", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_FSTAT_E */{"fstat", EC_FILE, EF_USES_FD, 1, {{"fd", PT_FD, PF_NA} } }, - /* PPME_SYSCALL_FSTAT_X */{"fstat", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_STAT64_E */{"stat64", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_STAT64_X */{"stat64", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_LSTAT64_E */{"lstat64", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_LSTAT64_X */{"lstat64", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_FSTAT64_E */{"fstat64", EC_FILE, EF_USES_FD, 1, {{"fd", PT_FD, PF_NA} } }, - /* PPME_SYSCALL_FSTAT64_X */{"fstat64", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_EPOLLWAIT_E */{"epoll_wait", EC_WAIT, EF_WAITS, 1, {{"maxevents", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_EPOLLWAIT_X */{"epoll_wait", EC_WAIT, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_POLL_E */{"poll", EC_WAIT, EF_WAITS, 2, {{"fds", PT_FDLIST, PF_DEC}, {"timeout", PT_INT64, PF_DEC} } }, - /* PPME_SYSCALL_POLL_X */{"poll", EC_WAIT, EF_WAITS, 2, {{"res", PT_ERRNO, PF_DEC}, {"fds", PT_FDLIST, PF_DEC} } }, - /* PPME_SYSCALL_SELECT_E */{"select", EC_WAIT, EF_WAITS, 0}, - /* PPME_SYSCALL_SELECT_X */{"select", EC_WAIT, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_NEWSELECT_E */{"select", EC_WAIT, EF_WAITS, 0}, - /* PPME_SYSCALL_NEWSELECT_X */{"select", EC_WAIT, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_LSEEK_E */{"lseek", EC_FILE, EF_USES_FD, 3, {{"fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC}, {"whence", PT_FLAGS8, PF_DEC, lseek_whence} } }, - /* PPME_SYSCALL_LSEEK_X */{"lseek", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_LLSEEK_E */{"llseek", EC_FILE, EF_USES_FD, 3, {{"fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC}, {"whence", PT_FLAGS8, PF_DEC, lseek_whence} } }, - /* PPME_SYSCALL_LLSEEK_X */{"llseek", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_IOCTL_E */{"ioctl", EC_IO_OTHER, EF_USES_FD, 2, {{"fd", PT_FD, PF_DEC}, {"request", PT_UINT64, PF_HEX} } }, - /* PPME_SYSCALL_IOCTL_X */{"ioctl", EC_IO_OTHER, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_GETCWD_E */{"getcwd", EC_FILE, EF_NONE, 0}, - /* Note: path is PT_CHARBUF and not PT_FSPATH because we assume it's abosulte and will never need resolution */ - /* PPME_SYSCALL_GETCWD_X */{"getcwd", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_CHARBUF, PF_NA} } }, + /* PPME_SOCKET_ACCEPT_E */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SOCKET_ACCEPT_X */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE | EF_OLD_VERSION, 3, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC} } }, + /* PPME_SYSCALL_SEND_E */{"send", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_SEND_X */{"send", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SOCKET_SENDTO_E */{"sendto", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_MODIFIES_STATE, 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, + /* PPME_SOCKET_SENDTO_X */{"sendto", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SOCKET_RECV_E */{"recv", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, + /* PPME_SOCKET_RECV_X */{"recv", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SOCKET_RECVFROM_E */{"recvfrom", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_MODIFIES_STATE, 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, + /* PPME_SOCKET_RECVFROM_X */{"recvfrom", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_MODIFIES_STATE, 3, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, + /* PPME_SOCKET_SHUTDOWN_E */{"shutdown", EC_NET, EF_USES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"how", PT_FLAGS8, PF_HEX, shutdown_how} } }, + /* PPME_SOCKET_SHUTDOWN_X */{"shutdown", EC_NET, EF_USES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SOCKET_GETSOCKNAME_E */{"getsockname", EC_NET, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_GETSOCKNAME_X */{"getsockname", EC_NET, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_GETPEERNAME_E */{"getpeername", EC_NET, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_GETPEERNAME_X */{"getpeername", EC_NET, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_SOCKETPAIR_E */{"socketpair", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE, 3, {{"domain", PT_FLAGS32, PF_DEC, socket_families}, {"type", PT_UINT32, PF_DEC}, {"proto", PT_UINT32, PF_DEC} } }, + /* PPME_SOCKET_SOCKETPAIR_X */{"socketpair", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE, 5, {{"res", PT_ERRNO, PF_DEC}, {"fd1", PT_FD, PF_DEC}, {"fd2", PT_FD, PF_DEC}, {"source", PT_UINT64, PF_HEX}, {"peer", PT_UINT64, PF_HEX} } }, + /* PPME_SOCKET_SETSOCKOPT_E */{"setsockopt", EC_NET, EF_NONE, 0 }, + /* PPME_SOCKET_SETSOCKOPT_X */{"setsockopt", EC_NET, EF_USES_FD, 6, {{"res", PT_ERRNO, PF_DEC}, {"fd", PT_FD, PF_DEC}, {"level", PT_FLAGS8, PF_DEC, sockopt_levels}, {"optname", PT_FLAGS8, PF_DEC, sockopt_options}, {"val", PT_DYN, PF_DEC, sockopt_dynamic_param, PPM_SOCKOPT_IDX_MAX}, {"optlen", PT_UINT32, PF_DEC}}}, + /* PPME_SOCKET_GETSOCKOPT_E */{"getsockopt", EC_NET, EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 0 }, + /* PPME_SOCKET_GETSOCKOPT_X */{"getsockopt", EC_NET, EF_USES_FD | EF_MODIFIES_STATE| EF_DROP_SIMPLE_CONS, 6, {{"res", PT_ERRNO, PF_DEC}, {"fd", PT_FD, PF_DEC}, {"level", PT_FLAGS8, PF_DEC, sockopt_levels}, {"optname", PT_FLAGS8, PF_DEC, sockopt_options}, {"val", PT_DYN, PF_DEC, sockopt_dynamic_param, PPM_SOCKOPT_IDX_MAX}, {"optlen", PT_UINT32, PF_DEC}}}, + /* PPME_SOCKET_SENDMSG_E */{"sendmsg", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_MODIFIES_STATE, 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, + /* PPME_SOCKET_SENDMSG_X */{"sendmsg", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SOCKET_SENDMMSG_E */{"sendmmsg", EC_IO_WRITE, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_SENDMMSG_X */{"sendmmsg", EC_IO_WRITE, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_RECVMSG_E */{"recvmsg", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_MODIFIES_STATE, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SOCKET_RECVMSG_X */{"recvmsg", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_MODIFIES_STATE, 4, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, + /* PPME_SOCKET_RECVMMSG_E */{"recvmmsg", EC_IO_READ, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_RECVMMSG_X */{"recvmmsg", EC_IO_READ, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SOCKET_ACCEPT4_E */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE | EF_OLD_VERSION, 1, {{"flags", PT_INT32, PF_HEX} } }, + /* PPME_SOCKET_ACCEPT4_X */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE | EF_OLD_VERSION, 3, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC} } }, + /* PPME_SYSCALL_CREAT_E */{"creat", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_CREAT_X */{"creat", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE, 4, {{"fd", PT_FD, PF_DEC}, {"name", PT_FSPATH, PF_NA}, {"mode", PT_UINT32, PF_OCT}, {"dev", PT_UINT32, PF_HEX} } }, + /* PPME_SOCKET_PIPE_E */{"pipe", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE, 0}, + /* PPME_SOCKET_PIPE_X */{"pipe", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE, 4, {{"res", PT_ERRNO, PF_DEC}, {"fd1", PT_FD, PF_DEC}, {"fd2", PT_FD, PF_DEC}, {"ino", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_EVENTFD_E */{"eventfd", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 2, {{"initval", PT_UINT64, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX} } }, + /* PPME_SYSCALL_EVENTFD_X */{"eventfd", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_FUTEX_E */{"futex", EC_IPC, EF_DROP_SIMPLE_CONS, 3, {{"addr", PT_UINT64, PF_HEX}, {"op", PT_FLAGS16, PF_HEX, futex_operations}, {"val", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_FUTEX_X */{"futex", EC_IPC, EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_STAT_E */{"stat", EC_FILE, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_STAT_X */{"stat", EC_FILE, EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_LSTAT_E */{"lstat", EC_FILE, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_LSTAT_X */{"lstat", EC_FILE, EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_FSTAT_E */{"fstat", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"fd", PT_FD, PF_NA} } }, + /* PPME_SYSCALL_FSTAT_X */{"fstat", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_STAT64_E */{"stat64", EC_FILE, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_STAT64_X */{"stat64", EC_FILE, EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_LSTAT64_E */{"lstat64", EC_FILE, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_LSTAT64_X */{"lstat64", EC_FILE, EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_FSTAT64_E */{"fstat64", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"fd", PT_FD, PF_NA} } }, + /* PPME_SYSCALL_FSTAT64_X */{"fstat64", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_EPOLLWAIT_E */{"epoll_wait", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 1, {{"maxevents", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_EPOLLWAIT_X */{"epoll_wait", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_POLL_E */{"poll", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 2, {{"fds", PT_FDLIST, PF_DEC}, {"timeout", PT_INT64, PF_DEC} } }, + /* PPME_SYSCALL_POLL_X */{"poll", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"fds", PT_FDLIST, PF_DEC} } }, + /* PPME_SYSCALL_SELECT_E */{"select", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_SELECT_X */{"select", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_NEWSELECT_E */{"select", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_NEWSELECT_X */{"select", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_LSEEK_E */{"lseek", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 3, {{"fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC}, {"whence", PT_FLAGS8, PF_DEC, lseek_whence} } }, + /* PPME_SYSCALL_LSEEK_X */{"lseek", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_LLSEEK_E */{"llseek", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 3, {{"fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC}, {"whence", PT_FLAGS8, PF_DEC, lseek_whence} } }, + /* PPME_SYSCALL_LLSEEK_X */{"llseek", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_IOCTL_2_E */{"ioctl", EC_IO_OTHER, EF_USES_FD | EF_OLD_VERSION, 2, {{"fd", PT_FD, PF_DEC}, {"request", PT_UINT64, PF_HEX} } }, + /* PPME_SYSCALL_IOCTL_2_X */{"ioctl", EC_IO_OTHER, EF_USES_FD | EF_OLD_VERSION, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_GETCWD_E */{"getcwd", EC_FILE, EF_DROP_SIMPLE_CONS, 0}, + /* Note: path is PT_CHARBUF and not PT_FSPATH because we assume it's absolute and will never need resolution */ + /* PPME_SYSCALL_GETCWD_X */{"getcwd", EC_FILE, EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_CHARBUF, PF_NA} } }, /* Note: path is PT_CHARBUF and not PT_FSPATH because we don't want it to be resolved, since the event handler already changes it */ /* PPME_SYSCALL_CHDIR_E */{"chdir", EC_FILE, EF_MODIFIES_STATE, 0}, /* PPME_SYSCALL_CHDIR_X */{"chdir", EC_FILE, EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_CHARBUF, PF_NA} } }, - /* PPME_SYSCALL_FCHDIR_E */{"fchdir", EC_FILE, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_NA} } }, - /* PPME_SYSCALL_FCHDIR_X */{"fchdir", EC_FILE, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_FCHDIR_E */{"fchdir", EC_FILE, EF_USES_FD | EF_MODIFIES_STATE, 1, {{"fd", PT_FD, PF_NA} } }, + /* PPME_SYSCALL_FCHDIR_X */{"fchdir", EC_FILE, EF_USES_FD | EF_MODIFIES_STATE, 1, {{"res", PT_ERRNO, PF_DEC} } }, /* PPME_SYSCALL_MKDIR_E */{"mkdir", EC_FILE, EF_NONE, 2, {{"path", PT_FSPATH, PF_NA}, {"mode", PT_UINT32, PF_HEX} } }, /* PPME_SYSCALL_MKDIR_X */{"mkdir", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, /* PPME_SYSCALL_RMDIR_E */{"rmdir", EC_FILE, EF_NONE, 1, {{"path", PT_FSPATH, PF_NA} } }, /* PPME_SYSCALL_RMDIR_X */{"rmdir", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_OPENAT_E */{"openat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 4, {{"dirfd", PT_FD, PF_DEC}, {"name", PT_CHARBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, openat_flags}, {"mode", PT_UINT32, PF_HEX} } }, - /* PPME_SYSCALL_OPENAT_X */{"openat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_LINK_E */{"link", EC_FILE, EF_NONE, 2, {{"oldpath", PT_FSPATH, PF_NA}, {"newpath", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_LINK_X */{"link", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_LINKAT_E */{"linkat", EC_FILE, EF_NONE, 4, {{"olddir", PT_FD, PF_DEC}, {"oldpath", PT_CHARBUF, PF_NA}, {"newdir", PT_FD, PF_DEC}, {"newpath", PT_CHARBUF, PF_NA} } }, - /* PPME_SYSCALL_LINKAT_X */{"linkat", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_UNLINK_E */{"unlink", EC_FILE, EF_NONE, 1, {{"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_UNLINK_X */{"unlink", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_UNLINKAT_E */{"unlinkat", EC_FILE, EF_NONE, 2, {{"dirfd", PT_FD, PF_DEC}, {"name", PT_CHARBUF, PF_NA} } }, - /* PPME_SYSCALL_UNLINKAT_X */{"unlinkat", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_PREAD_E */{"pread", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PREAD_X */{"pread", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_PWRITE_E */{"pwrite", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PWRITE_X */{"pwrite", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_READV_E */{"readv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_READV_X */{"readv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_WRITEV_E */{"writev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_WRITEV_X */{"writev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_PREADV_E */{"preadv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PREADV_X */{"preadv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_PWRITEV_E */{"pwritev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PWRITEV_X */{"pwritev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_DUP_E */{"dup", EC_IO_OTHER, (enum ppm_event_flags)(EF_CREATES_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_DUP_X */{"dup", EC_IO_OTHER, (enum ppm_event_flags)(EF_CREATES_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_SIGNALFD_E */{"signalfd", EC_SIGNAL, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"mask", PT_UINT32, PF_HEX}, {"flags", PT_FLAGS8, PF_HEX} } }, - /* PPME_SYSCALL_SIGNALFD_X */{"signalfd", EC_SIGNAL, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_OPENAT_E */{"openat", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE | EF_OLD_VERSION, 4, {{"dirfd", PT_FD, PF_DEC}, {"name", PT_CHARBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_OCT} } }, + /* PPME_SYSCALL_OPENAT_X */{"openat", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE | EF_OLD_VERSION, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_LINK_E */{"link", EC_FILE, EF_OLD_VERSION, 2, {{"oldpath", PT_FSPATH, PF_NA}, {"newpath", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_LINK_X */{"link", EC_FILE, EF_OLD_VERSION, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_LINKAT_E */{"linkat", EC_FILE, EF_OLD_VERSION, 4, {{"olddir", PT_FD, PF_DEC}, {"oldpath", PT_CHARBUF, PF_NA}, {"newdir", PT_FD, PF_DEC}, {"newpath", PT_CHARBUF, PF_NA} } }, + /* PPME_SYSCALL_LINKAT_X */{"linkat", EC_FILE, EF_OLD_VERSION, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_UNLINK_E */{"unlink", EC_FILE, EF_OLD_VERSION, 1, {{"path", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_UNLINK_X */{"unlink", EC_FILE, EF_OLD_VERSION, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_UNLINKAT_E */{"unlinkat", EC_FILE, EF_OLD_VERSION, 2, {{"dirfd", PT_FD, PF_DEC}, {"name", PT_CHARBUF, PF_NA} } }, + /* PPME_SYSCALL_UNLINKAT_X */{"unlinkat", EC_FILE, EF_OLD_VERSION, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_PREAD_E */{"pread", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_PREAD_X */{"pread", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_PWRITE_E */{"pwrite", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_PWRITE_X */{"pwrite", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_READV_E */{"readv", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_READV_X */{"readv", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 3, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_WRITEV_E */{"writev", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_WRITEV_X */{"writev", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_PREADV_E */{"preadv", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_PREADV_X */{"preadv", EC_IO_READ, EF_USES_FD | EF_READS_FROM_FD | EF_DROP_SIMPLE_CONS, 3, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_PWRITEV_E */{"pwritev", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_PWRITEV_X */{"pwritev", EC_IO_WRITE, EF_USES_FD | EF_WRITES_TO_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_DUP_E */{"dup", EC_IO_OTHER, EF_CREATES_FD | EF_USES_FD | EF_MODIFIES_STATE, 1, {{"fd", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_DUP_X */{"dup", EC_IO_OTHER, EF_CREATES_FD | EF_USES_FD | EF_MODIFIES_STATE, 1, {{"res", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_SIGNALFD_E */{"signalfd", EC_SIGNAL, EF_CREATES_FD | EF_MODIFIES_STATE, 3, {{"fd", PT_FD, PF_DEC}, {"mask", PT_UINT32, PF_HEX}, {"flags", PT_FLAGS8, PF_HEX} } }, + /* PPME_SYSCALL_SIGNALFD_X */{"signalfd", EC_SIGNAL, EF_CREATES_FD | EF_MODIFIES_STATE, 1, {{"res", PT_FD, PF_DEC} } }, /* PPME_SYSCALL_KILL_E */{"kill", EC_SIGNAL, EF_NONE, 2, {{"pid", PT_PID, PF_DEC}, {"sig", PT_SIGTYPE, PF_DEC} } }, /* PPME_SYSCALL_KILL_X */{"kill", EC_SIGNAL, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, /* PPME_SYSCALL_TKILL_E */{"tkill", EC_SIGNAL, EF_NONE, 2, {{"tid", PT_PID, PF_DEC}, {"sig", PT_SIGTYPE, PF_DEC} } }, /* PPME_SYSCALL_TKILL_X */{"tkill", EC_SIGNAL, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, /* PPME_SYSCALL_TGKILL_E */{"tgkill", EC_SIGNAL, EF_NONE, 3, {{"pid", PT_PID, PF_DEC}, {"tid", PT_PID, PF_DEC}, {"sig", PT_SIGTYPE, PF_DEC} } }, /* PPME_SYSCALL_TGKILL_X */{"tgkill", EC_SIGNAL, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_NANOSLEEP_E */{"nanosleep", EC_SLEEP, EF_WAITS, 1, {{"interval", PT_RELTIME, PF_DEC} } }, - /* PPME_SYSCALL_NANOSLEEP_X */{"nanosleep", EC_SLEEP, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_TIMERFD_CREATE_E */{"timerfd_create", EC_TIME, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 2, {{"clockid", PT_UINT8, PF_DEC}, {"flags", PT_FLAGS8, PF_HEX} } }, - /* PPME_SYSCALL_TIMERFD_CREATE_X */{"timerfd_create", EC_TIME, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_INOTIFY_INIT_E */{"inotify_init", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"flags", PT_FLAGS8, PF_HEX} } }, - /* PPME_SYSCALL_INOTIFY_INIT_X */{"inotify_init", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_GETRLIMIT_E */{"getrlimit", EC_PROCESS, EF_NONE, 1, {{"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, - /* PPME_SYSCALL_GETRLIMIT_X */{"getrlimit", EC_PROCESS, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"cur", PT_INT64, PF_DEC}, {"max", PT_INT64, PF_DEC} } }, - /* PPME_SYSCALL_SETRLIMIT_E */{"setrlimit", EC_PROCESS, EF_NONE, 1, {{"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, - /* PPME_SYSCALL_SETRLIMIT_X */{"setrlimit", EC_PROCESS, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"cur", PT_INT64, PF_DEC}, {"max", PT_INT64, PF_DEC} } }, + /* PPME_SYSCALL_NANOSLEEP_E */{"nanosleep", EC_SLEEP, EF_WAITS | EF_DROP_SIMPLE_CONS, 1, {{"interval", PT_RELTIME, PF_DEC} } }, + /* PPME_SYSCALL_NANOSLEEP_X */{"nanosleep", EC_SLEEP, EF_WAITS | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_TIMERFD_CREATE_E */{"timerfd_create", EC_TIME, EF_CREATES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 2, {{"clockid", PT_UINT8, PF_DEC}, {"flags", PT_FLAGS8, PF_HEX} } }, + /* PPME_SYSCALL_TIMERFD_CREATE_X */{"timerfd_create", EC_TIME, EF_CREATES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_INOTIFY_INIT_E */{"inotify_init", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE, 1, {{"flags", PT_FLAGS8, PF_HEX} } }, + /* PPME_SYSCALL_INOTIFY_INIT_X */{"inotify_init", EC_IPC, EF_CREATES_FD | EF_MODIFIES_STATE, 1, {{"res", PT_FD, PF_DEC} } }, + /* PPME_SYSCALL_GETRLIMIT_E */{"getrlimit", EC_PROCESS, EF_DROP_SIMPLE_CONS, 1, {{"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, + /* PPME_SYSCALL_GETRLIMIT_X */{"getrlimit", EC_PROCESS, EF_DROP_SIMPLE_CONS, 3, {{"res", PT_ERRNO, PF_DEC}, {"cur", PT_INT64, PF_DEC}, {"max", PT_INT64, PF_DEC} } }, + /* PPME_SYSCALL_SETRLIMIT_E */{"setrlimit", EC_PROCESS, EF_DROP_SIMPLE_CONS, 1, {{"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, + /* PPME_SYSCALL_SETRLIMIT_X */{"setrlimit", EC_PROCESS, EF_DROP_SIMPLE_CONS, 3, {{"res", PT_ERRNO, PF_DEC}, {"cur", PT_INT64, PF_DEC}, {"max", PT_INT64, PF_DEC} } }, /* PPME_SYSCALL_PRLIMIT_E */{"prlimit", EC_PROCESS, EF_NONE, 2, {{"pid", PT_PID, PF_DEC}, {"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, /* PPME_SYSCALL_PRLIMIT_X */{"prlimit", EC_PROCESS, EF_NONE, 5, {{"res", PT_ERRNO, PF_DEC}, {"newcur", PT_INT64, PF_DEC}, {"newmax", PT_INT64, PF_DEC}, {"oldcur", PT_INT64, PF_DEC}, {"oldmax", PT_INT64, PF_DEC} } }, - /* PPME_SCHEDSWITCH_E */{"switch", EC_SCHEDULER, EF_NONE, 1, {{"next", PT_PID, PF_DEC} } }, - /* PPME_SCHEDSWITCH_X */{"NA2", EC_SCHEDULER, EF_UNUSED, 0}, - /* PPME_DROP_E */{"drop", EC_INTERNAL, EF_NONE, 1, {{"ratio", PT_UINT32, PF_DEC} } }, - /* PPME_DROP_X */{"drop", EC_INTERNAL, EF_NONE, 1, {{"ratio", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_FCNTL_E */{"fcntl", EC_IO_OTHER, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 2, {{"fd", PT_FD, PF_DEC}, {"cmd", PT_FLAGS8, PF_DEC, fcntl_commands} } }, - /* PPME_SYSCALL_FCNTL_X */{"fcntl", EC_IO_OTHER, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SCHEDSWITCHEX_E */{"switch", EC_SCHEDULER, EF_NONE, 5, {{"next", PT_PID, PF_DEC}, {"pgft_maj", PT_UINT32, PF_DEC}, {"pgft_min", PT_UINT32, PF_DEC}, {"next_pgft_maj", PT_UINT32, PF_DEC}, {"next_pgft_min", PT_UINT32, PF_DEC} } }, - /* PPME_SCHEDSWITCHEX_X */{"NA2", EC_SCHEDULER, EF_UNUSED, 0}, + /* PPME_SCHEDSWITCH_1_E */{"switch", EC_SCHEDULER, EF_SKIPPARSERESET | EF_OLD_VERSION | EF_DROP_SIMPLE_CONS, 1, {{"next", PT_PID, PF_DEC} } }, + /* PPME_SCHEDSWITCH_1_X */{"NA2", EC_SCHEDULER, EF_SKIPPARSERESET | EF_UNUSED | EF_OLD_VERSION, 0}, + /* PPME_DROP_E */{"drop", EC_INTERNAL, EF_SKIPPARSERESET, 1, {{"ratio", PT_UINT32, PF_DEC} } }, + /* PPME_DROP_X */{"drop", EC_INTERNAL, EF_SKIPPARSERESET, 1, {{"ratio", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_FCNTL_E */{"fcntl", EC_IO_OTHER, EF_USES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 2, {{"fd", PT_FD, PF_DEC}, {"cmd", PT_FLAGS8, PF_DEC, fcntl_commands} } }, + /* PPME_SYSCALL_FCNTL_X */{"fcntl", EC_IO_OTHER, EF_USES_FD | EF_MODIFIES_STATE | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_FD, PF_DEC} } }, + /* PPME_SCHEDSWITCH_6_E */{"switch", EC_SCHEDULER, EF_DROP_SIMPLE_CONS, 6, {{"next", PT_PID, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC} } }, + /* PPME_SCHEDSWITCH_6_X */{"NA2", EC_SCHEDULER, EF_UNUSED, 0}, + /* PPME_SYSCALL_EXECVE_13_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_EXECVE_13_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 13, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC} } }, + /* PPME_CLONE_16_E */{"clone", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_CLONE_16_X */{"clone", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 16, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_BRK_4_E */{"brk", EC_MEMORY, EF_DROP_SIMPLE_CONS, 1, {{"addr", PT_UINT64, PF_HEX} } }, + /* PPME_SYSCALL_BRK_4_X */{"brk", EC_MEMORY, EF_DROP_SIMPLE_CONS, 4, {{"res", PT_UINT64, PF_HEX}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_MMAP_E */{"mmap", EC_MEMORY, EF_DROP_SIMPLE_CONS, 6, {{"addr", PT_UINT64, PF_HEX}, {"length", PT_UINT64, PF_DEC}, {"prot", PT_FLAGS32, PF_HEX, prot_flags}, {"flags", PT_FLAGS32, PF_HEX, mmap_flags}, {"fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_MMAP_X */{"mmap", EC_MEMORY, EF_DROP_SIMPLE_CONS, 4, {{"res", PT_UINT64, PF_HEX}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_MMAP2_E */{"mmap2", EC_MEMORY, EF_DROP_SIMPLE_CONS, 6, {{"addr", PT_UINT64, PF_HEX}, {"length", PT_UINT64, PF_DEC}, {"prot", PT_FLAGS32, PF_HEX, prot_flags}, {"flags", PT_FLAGS32, PF_HEX, mmap_flags}, {"fd", PT_FD, PF_DEC}, {"pgoffset", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_MMAP2_X */{"mmap2", EC_MEMORY, EF_DROP_SIMPLE_CONS, 4, {{"res", PT_UINT64, PF_HEX}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_MUNMAP_E */{"munmap", EC_MEMORY, EF_DROP_SIMPLE_CONS, 2, {{"addr", PT_UINT64, PF_HEX}, {"length", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_MUNMAP_X */{"munmap", EC_MEMORY, EF_DROP_SIMPLE_CONS, 4, {{"res", PT_ERRNO, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_SPLICE_E */{"splice", EC_IO_OTHER, EF_USES_FD | EF_DROP_SIMPLE_CONS, 4, {{"fd_in", PT_FD, PF_DEC}, {"fd_out", PT_FD, PF_DEC}, {"size", PT_UINT64, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, splice_flags} } }, + /* PPME_SYSCALL_SPLICE_X */{"splice", EC_IO_OTHER, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_PTRACE_E */{"ptrace", EC_PROCESS, EF_NONE, 2, {{"request", PT_FLAGS16, PF_DEC, ptrace_requests}, {"pid", PT_PID, PF_DEC} } }, + /* PPME_SYSCALL_PTRACE_X */{"ptrace", EC_PROCESS, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"addr", PT_DYN, PF_HEX, ptrace_dynamic_param, PPM_PTRACE_IDX_MAX}, {"data", PT_DYN, PF_HEX, ptrace_dynamic_param, PPM_PTRACE_IDX_MAX} } }, + /* PPME_SYSCALL_IOCTL_3_E */{"ioctl", EC_IO_OTHER, EF_USES_FD, 3, {{"fd", PT_FD, PF_DEC}, {"request", PT_UINT64, PF_HEX}, {"argument", PT_UINT64, PF_HEX} } }, + /* PPME_SYSCALL_IOCTL_3_X */{"ioctl", EC_IO_OTHER, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_EXECVE_14_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_EXECVE_14_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 14, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"env", PT_BYTEBUF, PF_NA} } }, + /* PPME_SYSCALL_RENAME_E */{"rename", EC_FILE, EF_NONE, 0 }, + /* PPME_SYSCALL_RENAME_X */{"rename", EC_FILE, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"oldpath", PT_FSPATH, PF_NA}, {"newpath", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_RENAMEAT_E */{"renameat", EC_FILE, EF_NONE, 0 }, + /* PPME_SYSCALL_RENAMEAT_X */{"renameat", EC_FILE, EF_NONE, 5, {{"res", PT_ERRNO, PF_DEC}, {"olddirfd", PT_FD, PF_DEC}, {"oldpath", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"newdirfd", PT_FD, PF_DEC}, {"newpath", PT_FSRELPATH, PF_NA, DIRFD_PARAM(3)} } }, + /* PPME_SYSCALL_SYMLINK_E */{"symlink", EC_FILE, EF_NONE, 0 }, + /* PPME_SYSCALL_SYMLINK_X */{"symlink", EC_FILE, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"target", PT_CHARBUF, PF_NA}, {"linkpath", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_SYMLINKAT_E */{"symlinkat", EC_FILE, EF_NONE, 0 }, + /* PPME_SYSCALL_SYMLINKAT_X */{"symlinkat", EC_FILE, EF_NONE, 4, {{"res", PT_ERRNO, PF_DEC}, {"target", PT_CHARBUF, PF_NA}, {"linkdirfd", PT_FD, PF_DEC}, {"linkpath", PT_FSRELPATH, PF_NA, DIRFD_PARAM(2)} } }, + /* PPME_SYSCALL_FORK_E */{"fork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_FORK_X */{"fork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 16, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_VFORK_E */{"vfork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_VFORK_X */{"vfork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 16, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, + /* PPME_PROCEXIT_1_E */{"procexit", EC_PROCESS, EF_MODIFIES_STATE, 1, {{"status", PT_ERRNO, PF_DEC} } }, + /* PPME_NA1 */{"NA1", EC_PROCESS, EF_UNUSED, 0}, + /* PPME_SYSCALL_SENDFILE_E */{"sendfile", EC_IO_WRITE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 4, {{"out_fd", PT_FD, PF_DEC}, {"in_fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC}, {"size", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_SENDFILE_X */{"sendfile", EC_IO_WRITE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"offset", PT_UINT64, PF_DEC} } }, + /* PPME_SYSCALL_QUOTACTL_E */{"quotactl", EC_USER, EF_NONE, 4, {{"cmd", PT_FLAGS16, PF_DEC, quotactl_cmds }, {"type", PT_FLAGS8, PF_DEC, quotactl_types}, {"id", PT_UINT32, PF_DEC}, {"quota_fmt", PT_FLAGS8, PF_DEC, quotactl_quota_fmts } } }, + /* PPME_SYSCALL_QUOTACTL_X */{"quotactl", EC_USER, EF_NONE, 14, {{"res", PT_ERRNO, PF_DEC}, {"special", PT_CHARBUF, PF_NA }, {"quotafilepath", PT_CHARBUF, PF_NA}, {"dqb_bhardlimit", PT_UINT64, PF_DEC }, {"dqb_bsoftlimit", PT_UINT64, PF_DEC }, {"dqb_curspace", PT_UINT64, PF_DEC }, {"dqb_ihardlimit", PT_UINT64, PF_DEC }, {"dqb_isoftlimit", PT_UINT64, PF_DEC }, {"dqb_btime", PT_RELTIME, PF_DEC }, {"dqb_itime", PT_RELTIME, PF_DEC }, {"dqi_bgrace", PT_RELTIME, PF_DEC }, {"dqi_igrace", PT_RELTIME, PF_DEC }, {"dqi_flags", PT_FLAGS8, PF_DEC, quotactl_dqi_flags }, {"quota_fmt_out", PT_FLAGS8, PF_DEC, quotactl_quota_fmts } } }, + /* PPME_SYSCALL_SETRESUID_E */ {"setresuid", EC_USER, EF_MODIFIES_STATE, 3, {{"ruid", PT_UID, PF_DEC }, {"euid", PT_UID, PF_DEC }, {"suid", PT_UID, PF_DEC } } }, + /* PPME_SYSCALL_SETRESUID_X */ {"setresuid", EC_USER, EF_MODIFIES_STATE, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_SETRESGID_E */ {"setresgid", EC_USER, EF_MODIFIES_STATE, 3, {{"rgid", PT_GID, PF_DEC }, {"egid", PT_GID, PF_DEC }, {"sgid", PT_GID, PF_DEC } } }, + /* PPME_SYSCALL_SETRESGID_X */ {"setresgid", EC_USER, EF_MODIFIES_STATE, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSDIGEVENT_E */{"sysdigevent", EC_INTERNAL, EF_SKIPPARSERESET, 2, {{"event_type", PT_UINT32, PF_DEC}, {"event_data", PT_UINT64, PF_DEC} } }, + /* PPME_NA1 */{"sysdigevent", EC_INTERNAL, EF_UNUSED, 0}, + /* PPME_SYSCALL_SETUID_E */ {"setuid", EC_USER, EF_MODIFIES_STATE, 1, {{"uid", PT_UID, PF_DEC} } }, + /* PPME_SYSCALL_SETUID_X */ {"setuid", EC_USER, EF_MODIFIES_STATE, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_SETGID_E */ {"setgid", EC_USER, EF_MODIFIES_STATE, 1, {{"gid", PT_GID, PF_DEC} } }, + /* PPME_SYSCALL_SETGID_X */ {"setgid", EC_USER, EF_MODIFIES_STATE, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_GETUID_E */ {"getuid", EC_USER, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_GETUID_X */ {"getuid", EC_USER, EF_DROP_SIMPLE_CONS, 1, {{"uid", PT_UID, PF_DEC} } }, + /* PPME_SYSCALL_GETEUID_E */ {"geteuid", EC_USER, EF_DROP_SIMPLE_CONS, 0 }, + /* PPME_SYSCALL_GETEUID_X */ {"geteuid", EC_USER, EF_DROP_SIMPLE_CONS, 1, {{"euid", PT_UID, PF_DEC} } }, + /* PPME_SYSCALL_GETGID_E */ {"getgid", EC_USER, EF_DROP_SIMPLE_CONS, 0}, + /* PPME_SYSCALL_GETGID_X */ {"getgid", EC_USER, EF_DROP_SIMPLE_CONS, 1, {{"gid", PT_GID, PF_DEC} } }, + /* PPME_SYSCALL_GETEGID_E */ {"getegid", EC_USER, EF_DROP_SIMPLE_CONS, 0 }, + /* PPME_SYSCALL_GETEGID_X */ {"getegid", EC_USER, EF_DROP_SIMPLE_CONS, 1, {{"egid", PT_GID, PF_DEC} } }, + /* PPME_SYSCALL_GETRESUID_E */ {"getresuid", EC_USER, EF_DROP_SIMPLE_CONS, 0 }, + /* PPME_SYSCALL_GETRESUID_X */ {"getresuid", EC_USER, EF_DROP_SIMPLE_CONS, 4, {{"res", PT_ERRNO, PF_DEC}, {"ruid", PT_UID, PF_DEC }, {"euid", PT_UID, PF_DEC }, {"suid", PT_UID, PF_DEC } } }, + /* PPME_SYSCALL_GETRESGID_E */ {"getresgid", EC_USER, EF_DROP_SIMPLE_CONS, 0 }, + /* PPME_SYSCALL_GETRESGID_X */ {"getresgid", EC_USER, EF_DROP_SIMPLE_CONS, 4, {{"res", PT_ERRNO, PF_DEC}, {"rgid", PT_GID, PF_DEC }, {"egid", PT_GID, PF_DEC }, {"sgid", PT_GID, PF_DEC } } }, + /* PPME_SYSCALL_EXECVE_15_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_EXECVE_15_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 15, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"env", PT_BYTEBUF, PF_NA} } }, + /* PPME_CLONE_17_E */{"clone", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_CLONE_17_X */{"clone", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 17, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_FORK_17_E */{"fork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_FORK_17_X */{"fork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 17, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_VFORK_17_E */{"vfork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_VFORK_17_X */{"vfork", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 17, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, + /* PPME_CLONE_20_E */{"clone", EC_PROCESS, EF_MODIFIES_STATE, 0}, + /* PPME_CLONE_20_X */{"clone", EC_PROCESS, EF_MODIFIES_STATE, 20, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC}, {"vtid", PT_PID, PF_DEC}, {"vpid", PT_PID, PF_DEC} } }, + /* PPME_SYSCALL_FORK_20_E */{"fork", EC_PROCESS, EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_FORK_20_X */{"fork", EC_PROCESS, EF_MODIFIES_STATE, 20, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC}, {"vtid", PT_PID, PF_DEC}, {"vpid", PT_PID, PF_DEC} } }, + /* PPME_SYSCALL_VFORK_20_E */{"vfork", EC_PROCESS, EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_VFORK_20_X */{"vfork", EC_PROCESS, EF_MODIFIES_STATE, 20, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC}, {"vtid", PT_PID, PF_DEC}, {"vpid", PT_PID, PF_DEC} } }, + /* PPME_CONTAINER_E */{"container", EC_INTERNAL, EF_SKIPPARSERESET | EF_MODIFIES_STATE, 4, {{"id", PT_CHARBUF, PF_NA}, {"type", PT_UINT32, PF_DEC}, {"name", PT_CHARBUF, PF_NA}, {"image", PT_CHARBUF, PF_NA} } }, + /* PPME_CONTAINER_X */{"container", EC_INTERNAL, EF_UNUSED, 0}, + /* PPME_SYSCALL_EXECVE_16_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_EXECVE_16_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 16, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"env", PT_BYTEBUF, PF_NA} } }, + /* PPME_SIGNALDELIVER_E */ {"signaldeliver", EC_SIGNAL, EF_DROP_SIMPLE_CONS, 3, {{"spid", PT_PID, PF_DEC}, {"dpid", PT_PID, PF_DEC}, {"sig", PT_SIGTYPE, PF_DEC} } }, + /* PPME_SIGNALDELIVER_X */ {"signaldeliver", EC_SIGNAL, EF_UNUSED, 0 }, + /* PPME_PROCINFO_E */{"procinfo", EC_INTERNAL, EF_SKIPPARSERESET | EF_DROP_SIMPLE_CONS, 2, {{"cpu_usr", PT_UINT64, PF_DEC}, {"cpu_sys", PT_UINT64, PF_DEC} } }, + /* PPME_PROCINFO_X */{"NA2", EC_INTERNAL, EF_UNUSED, 0}, + /* PPME_SYSCALL_GETDENTS_E */{"getdents", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"fd", PT_FD, PF_NA} } }, + /* PPME_SYSCALL_GETDENTS_X */{"getdents", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_GETDENTS64_E */{"getdents64", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"fd", PT_FD, PF_NA} } }, + /* PPME_SYSCALL_GETDENTS64_X */{"getdents64", EC_FILE, EF_USES_FD | EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_SETNS_E */ {"setns", EC_PROCESS, EF_USES_FD, 2, {{"fd", PT_FD, PF_NA}, {"nstype", PT_FLAGS32, PF_HEX, clone_flags} } }, + /* PPME_SYSCALL_SETNS_X */ {"setns", EC_PROCESS, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_FLOCK_E */ {"flock", EC_FILE, EF_USES_FD, 2, {{"fd", PT_FD, PF_NA}, {"operation", PT_FLAGS32, PF_HEX, flock_flags} } }, + /* PPME_SYSCALL_FLOCK_X */ {"flock", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_CPU_HOTPLUG_E */ {"cpu_hotplug", EC_SYSTEM, EF_SKIPPARSERESET | EF_MODIFIES_STATE, 2, {{"cpu", PT_UINT32, PF_DEC}, {"action", PT_UINT32, PF_DEC} } }, + /* PPME_CPU_HOTPLUG_X */{"NA2", EC_SYSTEM, EF_UNUSED, 0}, + /* PPME_SOCKET_ACCEPT_5_E */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE, 0}, + /* PPME_SOCKET_ACCEPT_5_X */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE, 5, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC}, {"queuelen", PT_UINT32, PF_DEC}, {"queuemax", PT_UINT32, PF_DEC} } }, + /* PPME_SOCKET_ACCEPT4_5_E */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE, 1, {{"flags", PT_INT32, PF_HEX} } }, + /* PPME_SOCKET_ACCEPT4_5_X */{"accept", EC_NET, EF_CREATES_FD | EF_MODIFIES_STATE, 5, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC}, {"queuelen", PT_UINT32, PF_DEC}, {"queuemax", PT_UINT32, PF_DEC} } }, + /* PPME_SYSCALL_SEMOP_E */ {"semop", EC_PROCESS, EF_DROP_SIMPLE_CONS, 1, {{"semid", PT_INT32, PF_DEC} } }, + /* PPME_SYSCALL_SEMOP_X */ {"semop", EC_PROCESS, EF_DROP_SIMPLE_CONS, 8, {{"res", PT_ERRNO, PF_DEC}, {"nsops", PT_UINT32, PF_DEC}, {"sem_num_0", PT_UINT16, PF_DEC}, {"sem_op_0", PT_INT16, PF_DEC}, {"sem_flg_0", PT_FLAGS16, PF_HEX, semop_flags}, {"sem_num_1", PT_UINT16, PF_DEC}, {"sem_op_1", PT_INT16, PF_DEC}, {"sem_flg_1", PT_FLAGS16, PF_HEX, semop_flags} } }, + /* PPME_SYSCALL_SEMCTL_E */{"semctl", EC_PROCESS, EF_DROP_SIMPLE_CONS, 4, {{"semid", PT_INT32, PF_DEC}, {"semnum", PT_INT32, PF_DEC}, {"cmd", PT_FLAGS16, PF_HEX, semctl_commands}, {"val", PT_INT32, PF_DEC} } }, + /* PPME_SYSCALL_SEMCTL_X */{"semctl", EC_PROCESS, EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_PPOLL_E */{"ppoll", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 3, {{"fds", PT_FDLIST, PF_DEC}, {"timeout", PT_RELTIME, PF_DEC}, {"sigmask", PT_SIGSET, PF_DEC} } }, + /* PPME_SYSCALL_PPOLL_X */{"ppoll", EC_WAIT, EF_WAITS | EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"fds", PT_FDLIST, PF_DEC} } }, + /* PPME_SYSCALL_MOUNT_E */{"mount", EC_FILE, EF_MODIFIES_STATE, 1, {{"flags", PT_FLAGS32, PF_HEX, mount_flags} } }, + /* PPME_SYSCALL_MOUNT_X */{"mount", EC_FILE, EF_MODIFIES_STATE, 4, {{"res", PT_ERRNO, PF_DEC}, {"dev", PT_CHARBUF, PF_NA}, {"dir", PT_FSPATH, PF_NA}, {"type", PT_CHARBUF, PF_NA} } }, + /* PPME_SYSCALL_UMOUNT_E */{"umount", EC_FILE, EF_MODIFIES_STATE, 1, {{"flags", PT_FLAGS32, PF_HEX, umount_flags} } }, + /* PPME_SYSCALL_UMOUNT_X */{"umount", EC_FILE, EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"name", PT_FSPATH, PF_NA} } }, + /* PPME_K8S_E */{"k8s", EC_INTERNAL, EF_SKIPPARSERESET | EF_MODIFIES_STATE, 1, {{"json", PT_CHARBUF, PF_NA} } }, + /* PPME_K8S_X */{"NA3", EC_SYSTEM, EF_UNUSED, 0}, + /* PPME_SYSCALL_SEMGET_E */{"semget", EC_PROCESS, EF_DROP_SIMPLE_CONS, 3, {{"key", PT_INT32, PF_HEX}, {"nsems", PT_INT32, PF_DEC}, {"semflg", PT_FLAGS32, PF_HEX, semget_flags} } }, + /* PPME_SYSCALL_SEMGET_X */{"semget", EC_PROCESS, EF_DROP_SIMPLE_CONS, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_ACCESS_E */{"access", EC_FILE, EF_DROP_SIMPLE_CONS, 1, {{"mode", PT_FLAGS32, PF_HEX, access_flags} } }, + /* PPME_SYSCALL_ACCESS_X */{"access", EC_FILE, EF_DROP_SIMPLE_CONS, 2, {{"res", PT_ERRNO, PF_DEC}, {"name", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_CHROOT_E */{"chroot", EC_PROCESS, EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_CHROOT_X */{"chroot", EC_PROCESS, EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_TRACER_E */{"tracer", EC_OTHER, EF_NONE, 3, {{"id", PT_INT64, PF_DEC}, {"tags", PT_CHARBUFARRAY, PF_NA}, {"args", PT_CHARBUF_PAIR_ARRAY, PF_NA} } }, + /* PPME_TRACER_X */{ "tracer", EC_OTHER, EF_NONE, 3, { { "id", PT_INT64, PF_DEC }, { "tags", PT_CHARBUFARRAY, PF_NA }, { "args", PT_CHARBUF_PAIR_ARRAY, PF_NA } } }, + /* PPME_MESOS_E */{"mesos", EC_INTERNAL, EF_SKIPPARSERESET | EF_MODIFIES_STATE, 1, {{"json", PT_CHARBUF, PF_NA} } }, + /* PPME_MESOS_X */{"NA4", EC_SYSTEM, EF_UNUSED, 0}, + /* PPME_CONTAINER_JSON_E */{"container", EC_PROCESS, EF_MODIFIES_STATE, 1, {{"json", PT_CHARBUF, PF_NA} } }, + /* PPME_CONTAINER_JSON_X */{"container", EC_PROCESS, EF_UNUSED, 0}, + /* PPME_SYSCALL_SETSID_E */{"setsid", EC_PROCESS, EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_SETSID_X */{"setsid", EC_PROCESS, EF_MODIFIES_STATE, 1, {{"res", PT_PID, PF_DEC} } }, + /* PPME_SYSCALL_MKDIR_2_E */{"mkdir", EC_FILE, EF_NONE, 1, {{"mode", PT_UINT32, PF_HEX} } }, + /* PPME_SYSCALL_MKDIR_2_X */{"mkdir", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_RMDIR_2_E */{"rmdir", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_RMDIR_2_X */{"rmdir", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_NOTIFICATION_E */{"notification", EC_OTHER, EF_SKIPPARSERESET, 2, {{"id", PT_CHARBUF, PF_DEC}, {"desc", PT_CHARBUF, PF_NA}, } }, + /* PPME_NOTIFICATION_X */{"NA4", EC_SYSTEM, EF_UNUSED, 0}, + /* PPME_SYSCALL_EXECVE_17_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 0}, + /* PPME_SYSCALL_EXECVE_17_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 17, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"env", PT_BYTEBUF, PF_NA}, {"tty", PT_INT32, PF_DEC} } }, + /* PPME_SYSCALL_UNSHARE_E */ {"unshare", EC_PROCESS, EF_NONE, 1, {{"flags", PT_FLAGS32, PF_HEX, clone_flags} } }, + /* PPME_SYSCALL_UNSHARE_X */ {"unshare", EC_PROCESS, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_INFRASTRUCTURE_EVENT_E */{"infra", EC_INTERNAL, EF_SKIPPARSERESET, 4, {{"source", PT_CHARBUF, PF_DEC}, {"name", PT_CHARBUF, PF_NA}, {"description", PT_CHARBUF, PF_NA}, {"scope", PT_CHARBUF, PF_NA} } }, + /* PPME_INFRASTRUCTURE_EVENT_X */{"NA4", EC_SYSTEM, EF_UNUSED, 0}, + /* PPME_SYSCALL_EXECVE_18_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 1, {{"filename", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_EXECVE_18_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE | EF_OLD_VERSION, 17, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"env", PT_BYTEBUF, PF_NA}, {"tty", PT_INT32, PF_DEC} } }, + /* PPME_PAGE_FAULT_E */ {"page_fault", EC_OTHER, EF_SKIPPARSERESET | EF_DROP_SIMPLE_CONS, 3, {{"addr", PT_UINT64, PF_HEX}, {"ip", PT_UINT64, PF_HEX}, {"error", PT_FLAGS32, PF_HEX, pf_flags} } }, + /* PPME_PAGE_FAULT_X */ {"NA5", EC_OTHER, EF_UNUSED, 0}, + /* PPME_SYSCALL_EXECVE_19_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 1, {{"filename", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_EXECVE_19_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 19, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"env", PT_BYTEBUF, PF_NA}, {"tty", PT_INT32, PF_DEC}, {"pgid", PT_PID, PF_DEC}, {"loginuid", PT_INT32, PF_DEC} } }, + /* PPME_SYSCALL_SETPGID_E */{"setpgid", EC_PROCESS, EF_MODIFIES_STATE, 2, {{"pid", PT_PID, PF_DEC}, {"pgid", PT_PID, PF_DEC} } }, + /* PPME_SYSCALL_SETPGID_X */{"setpgid", EC_PROCESS, EF_MODIFIES_STATE, 1, {{"res", PT_PID, PF_DEC} } }, + /* PPME_SYSCALL_BPF_E */{"bpf", EC_OTHER, EF_CREATES_FD, 1, {{"cmd", PT_INT64, PF_DEC} } }, + /* PPME_SYSCALL_BPF_X */{"bpf", EC_OTHER, EF_CREATES_FD, 1, {{"res_or_fd", PT_DYN, PF_DEC, bpf_dynamic_param, PPM_BPF_IDX_MAX} } }, + /* PPME_SYSCALL_SECCOMP_E */{"seccomp", EC_OTHER, EF_NONE, 1, {{"op", PT_UINT64, PF_DEC}, {"flags", PT_UINT64, PF_HEX} } }, + /* PPME_SYSCALL_SECCOMP_X */{"seccomp", EC_OTHER, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, + /* PPME_SYSCALL_UNLINK_2_E */{"unlink", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_UNLINK_2_X */{"unlink", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_UNLINKAT_2_E */{"unlinkat", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_UNLINKAT_2_X */{"unlinkat", EC_FILE, EF_NONE, 4, {{"res", PT_ERRNO, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"name", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"flags", PT_FLAGS32, PF_HEX, unlinkat_flags} } }, + /* PPME_SYSCALL_MKDIRAT_E */{"mkdirat", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_MKDIRAT_X */{"mkdirat", EC_FILE, EF_NONE, 4, {{"res", PT_ERRNO, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"path", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"mode", PT_UINT32, PF_HEX} } }, + /* PPME_SYSCALL_OPENAT_2_E */{"openat", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE, 0}, + /* PPME_SYSCALL_OPENAT_2_X */{"openat", EC_FILE, EF_CREATES_FD | EF_MODIFIES_STATE, 6, {{"fd", PT_FD, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"name", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_OCT}, {"dev", PT_UINT32, PF_HEX} } }, + /* PPME_SYSCALL_LINK_2_E */{"link", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_LINK_2_X */{"link", EC_FILE, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"oldpath", PT_FSPATH, PF_NA}, {"newpath", PT_FSPATH, PF_NA} } }, + /* PPME_SYSCALL_LINKAT_2_E */{"linkat", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_LINKAT_2_X */{"linkat", EC_FILE, EF_NONE, 6, {{"res", PT_ERRNO, PF_DEC}, {"olddir", PT_FD, PF_DEC}, {"oldpath", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"newdir", PT_FD, PF_DEC}, {"newpath", PT_FSRELPATH, PF_NA, DIRFD_PARAM(3)}, {"flags", PT_FLAGS32, PF_HEX, linkat_flags} } }, + /* PPME_SYSCALL_FCHMODAT_E */{"fchmodat", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_FCHMODAT_X */{"fchmodat", EC_FILE, EF_NONE, 4, {{"res", PT_ERRNO, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"filename", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"mode", PT_MODE, PF_OCT, chmod_mode} } }, + /* PPME_SYSCALL_CHMOD_E */{"chmod", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_CHMOD_X */{"chmod", EC_FILE, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"filename", PT_FSPATH, PF_NA}, {"mode", PT_MODE, PF_OCT, chmod_mode} } }, + /* PPME_SYSCALL_FCHMOD_E */{"fchmod", EC_FILE, EF_NONE, 0}, + /* PPME_SYSCALL_FCHMOD_X */{"fchmod", EC_FILE, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"fd", PT_FD, PF_DEC}, {"mode", PT_MODE, PF_OCT, chmod_mode} } }, + /* PPME_SYSCALL_RENAMEAT2_E */{"renameat2", EC_FILE, EF_NONE, 0 }, + /* PPME_SYSCALL_RENAMEAT2_X */{"renameat2", EC_FILE, EF_NONE, 6, {{"res", PT_ERRNO, PF_DEC}, {"olddirfd", PT_FD, PF_DEC}, {"oldpath", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"newdirfd", PT_FD, PF_DEC}, {"newpath", PT_FSRELPATH, PF_NA, DIRFD_PARAM(3)}, {"flags", PT_FLAGS32, PF_HEX, renameat2_flags} } } + /* NB: Starting from scap version 1.2, event types will no longer be changed when an event is modified, and the only kind of change permitted for pre-existent events is adding parameters. + * New event types are allowed only for new syscalls or new internal events. + * The number of parameters can be used to differentiate between event versions. + */ }; diff --git a/driver/fillers_table.c b/driver/fillers_table.c new file mode 100644 index 0000000000..d3bd7abf41 --- /dev/null +++ b/driver/fillers_table.c @@ -0,0 +1,298 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#include "ppm_events_public.h" + +#ifdef __KERNEL__ +#include "ppm.h" +#else +#ifndef UDIG +#define CAPTURE_CONTEXT_SWITCHES +#define CAPTURE_SIGNAL_DELIVERIES +#define CAPTURE_PAGE_FAULTS +#endif /* UDIG */ +#endif /* __KERNEL__ */ + +#if defined(__KERNEL__) || defined(UDIG) +#define FILLER_REF(x) f_##x, PPM_FILLER_##x +#else +#define FILLER_REF(x) 0, PPM_FILLER_##x +#endif /* __KERNEL__ */ + +#define f_sys_socket_x f_sys_single_x + +const struct ppm_event_entry g_ppm_events[PPM_EVENT_MAX] = { + [PPME_GENERIC_E] = {FILLER_REF(sys_generic)}, + [PPME_GENERIC_X] = {FILLER_REF(sys_generic)}, + [PPME_SYSCALL_OPEN_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_OPEN_X] = {FILLER_REF(sys_open_x)}, + [PPME_SYSCALL_CLOSE_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_CLOSE_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_READ_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {2} } }, + [PPME_SYSCALL_READ_X] = {FILLER_REF(sys_read_x)}, + [PPME_SYSCALL_WRITE_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {2} } }, + [PPME_SYSCALL_WRITE_X] = {FILLER_REF(sys_write_x)}, + [PPME_PROCEXIT_1_E] = {FILLER_REF(sys_procexit_e)}, + [PPME_SOCKET_SOCKET_E] = {FILLER_REF(sys_autofill), 3, APT_SOCK, {{0}, {1}, {2} } }, + [PPME_SOCKET_SOCKET_X] = {FILLER_REF(sys_socket_x)}, + [PPME_SOCKET_BIND_E] = {FILLER_REF(sys_autofill), 1, APT_SOCK, {{0} } }, + [PPME_SOCKET_BIND_X] = {FILLER_REF(sys_socket_bind_x)}, + [PPME_SOCKET_CONNECT_E] = {FILLER_REF(sys_autofill), 1, APT_SOCK, {{0} } }, + [PPME_SOCKET_CONNECT_X] = {FILLER_REF(sys_connect_x)}, + [PPME_SOCKET_LISTEN_E] = {FILLER_REF(sys_autofill), 2, APT_SOCK, {{0}, {1} } }, + [PPME_SOCKET_LISTEN_X] = {FILLER_REF(sys_single_x)}, + [PPME_SOCKET_SEND_E] = {FILLER_REF(sys_send_e)}, + [PPME_SOCKET_SEND_X] = {FILLER_REF(sys_send_x)}, + [PPME_SOCKET_SENDTO_E] = {FILLER_REF(sys_sendto_e)}, + [PPME_SOCKET_SENDTO_X] = {FILLER_REF(sys_send_x)}, + [PPME_SOCKET_RECV_E] = {FILLER_REF(sys_autofill), 2, APT_SOCK, {{0}, {2} } }, + [PPME_SOCKET_RECV_X] = {FILLER_REF(sys_recv_x)}, + [PPME_SOCKET_RECVFROM_E] = {FILLER_REF(sys_autofill), 2, APT_SOCK, {{0}, {2} } }, + [PPME_SOCKET_RECVFROM_X] = {FILLER_REF(sys_recvfrom_x)}, + [PPME_SOCKET_SHUTDOWN_E] = {FILLER_REF(sys_shutdown_e)}, + [PPME_SOCKET_SHUTDOWN_X] = {FILLER_REF(sys_single_x)}, + [PPME_SOCKET_GETSOCKNAME_E] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_GETSOCKNAME_X] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_GETPEERNAME_E] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_GETPEERNAME_X] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_SOCKETPAIR_E] = {FILLER_REF(sys_autofill), 3, APT_SOCK, {{0}, {1}, {2} } }, + [PPME_SOCKET_SOCKETPAIR_X] = {FILLER_REF(sys_socketpair_x)}, + [PPME_SOCKET_SETSOCKOPT_E] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_SETSOCKOPT_X] = {FILLER_REF(sys_setsockopt_x)}, + [PPME_SOCKET_GETSOCKOPT_E] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_GETSOCKOPT_X] = {FILLER_REF(sys_getsockopt_x)}, + [PPME_SOCKET_SENDMSG_E] = {FILLER_REF(sys_sendmsg_e)}, + [PPME_SOCKET_SENDMSG_X] = {FILLER_REF(sys_sendmsg_x)}, + [PPME_SOCKET_SENDMMSG_E] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_SENDMMSG_X] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_RECVMSG_E] = {FILLER_REF(sys_autofill), 1, APT_SOCK, {{0} } }, + [PPME_SOCKET_RECVMSG_X] = {FILLER_REF(sys_recvmsg_x)}, + [PPME_SOCKET_RECVMMSG_E] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_RECVMMSG_X] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_CREAT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_CREAT_X] = {FILLER_REF(sys_creat_x)}, + [PPME_SYSCALL_PIPE_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_PIPE_X] = {FILLER_REF(sys_pipe_x)}, + [PPME_SYSCALL_EVENTFD_E] = {FILLER_REF(sys_eventfd_e)}, + [PPME_SYSCALL_EVENTFD_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_FUTEX_E] = {FILLER_REF(sys_futex_e)}, + [PPME_SYSCALL_FUTEX_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_STAT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_STAT_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_LSTAT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_LSTAT_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_FSTAT_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_FSTAT_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_STAT64_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_STAT64_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_LSTAT64_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_LSTAT64_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_FSTAT64_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_FSTAT64_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_EPOLLWAIT_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{2} } }, + [PPME_SYSCALL_EPOLLWAIT_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_POLL_E] = {FILLER_REF(sys_poll_e)}, + [PPME_SYSCALL_POLL_X] = {FILLER_REF(sys_poll_x)}, + [PPME_SYSCALL_SELECT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_SELECT_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_NEWSELECT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_NEWSELECT_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_LSEEK_E] = {FILLER_REF(sys_lseek_e)}, + [PPME_SYSCALL_LSEEK_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_LLSEEK_E] = {FILLER_REF(sys_llseek_e)}, + [PPME_SYSCALL_LLSEEK_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_GETCWD_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_GETCWD_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_CHDIR_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_CHDIR_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_FCHDIR_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_FCHDIR_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_UNLINK_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_UNLINK_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_UNLINKAT_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {1} } }, + [PPME_SYSCALL_UNLINKAT_X] = {FILLER_REF(sys_single_x)}, +#ifdef _64BIT_ARGS_SINGLE_REGISTER + [PPME_SYSCALL_PREAD_E] = {FILLER_REF(sys_autofill), 3, APT_REG, {{0}, {2}, {3} } }, +#else + [PPME_SYSCALL_PREAD_E] = {FILLER_REF(sys_pread64_e)}, +#endif + [PPME_SYSCALL_PREAD_X] = {FILLER_REF(sys_read_x)}, +#ifdef _64BIT_ARGS_SINGLE_REGISTER + [PPME_SYSCALL_PWRITE_E] = {FILLER_REF(sys_autofill), 3, APT_REG, {{0}, {2}, {3} } }, +#else + [PPME_SYSCALL_PWRITE_E] = {FILLER_REF(sys_pwrite64_e)}, + #endif + [PPME_SYSCALL_PWRITE_X] = {FILLER_REF(sys_write_x)}, + [PPME_SYSCALL_READV_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_READV_X] = {FILLER_REF(sys_readv_preadv_x)}, + [PPME_SYSCALL_WRITEV_E] = {FILLER_REF(sys_writev_e)}, + [PPME_SYSCALL_WRITEV_X] = {FILLER_REF(sys_writev_pwritev_x)}, +#ifdef _64BIT_ARGS_SINGLE_REGISTER + [PPME_SYSCALL_PREADV_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {3} } }, +#else + [PPME_SYSCALL_PREADV_E] = {FILLER_REF(sys_preadv64_e)}, +#endif + [PPME_SYSCALL_PREADV_X] = {FILLER_REF(sys_readv_preadv_x)}, + [PPME_SYSCALL_PWRITEV_E] = {FILLER_REF(sys_pwritev_e)}, + [PPME_SYSCALL_PWRITEV_X] = {FILLER_REF(sys_writev_pwritev_x)}, + [PPME_SYSCALL_DUP_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{0} } }, + [PPME_SYSCALL_DUP_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_SIGNALFD_E] = {FILLER_REF(sys_autofill), 3, APT_REG, {{0}, {AF_ID_USEDEFAULT, 0}, {AF_ID_USEDEFAULT, 0} } }, + [PPME_SYSCALL_SIGNALFD_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_KILL_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {1} } }, + [PPME_SYSCALL_KILL_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_TKILL_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {1} } }, + [PPME_SYSCALL_TKILL_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_TGKILL_E] = {FILLER_REF(sys_autofill), 3, APT_REG, {{0}, {1}, {2} } }, + [PPME_SYSCALL_TGKILL_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_NANOSLEEP_E] = {FILLER_REF(sys_nanosleep_e)}, + [PPME_SYSCALL_NANOSLEEP_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_TIMERFD_CREATE_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_USEDEFAULT, 0}, {AF_ID_USEDEFAULT, 0} } }, + [PPME_SYSCALL_TIMERFD_CREATE_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_INOTIFY_INIT_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_USEDEFAULT, 0} } }, + [PPME_SYSCALL_INOTIFY_INIT_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_GETRLIMIT_E] = {FILLER_REF(sys_getrlimit_setrlimit_e)}, + [PPME_SYSCALL_GETRLIMIT_X] = {FILLER_REF(sys_getrlimit_setrlrimit_x)}, + [PPME_SYSCALL_SETRLIMIT_E] = {FILLER_REF(sys_getrlimit_setrlimit_e)}, + [PPME_SYSCALL_SETRLIMIT_X] = {FILLER_REF(sys_getrlimit_setrlrimit_x)}, + [PPME_SYSCALL_PRLIMIT_E] = {FILLER_REF(sys_prlimit_e)}, + [PPME_SYSCALL_PRLIMIT_X] = {FILLER_REF(sys_prlimit_x)}, + [PPME_DROP_E] = {FILLER_REF(sched_drop)}, + [PPME_DROP_X] = {FILLER_REF(sched_drop)}, + [PPME_SYSCALL_FCNTL_E] = {FILLER_REF(sys_fcntl_e)}, + [PPME_SYSCALL_FCNTL_X] = {FILLER_REF(sys_single_x)}, +#ifdef CAPTURE_CONTEXT_SWITCHES + [PPME_SCHEDSWITCH_6_E] = {FILLER_REF(sched_switch_e)}, +#endif + [PPME_SYSCALL_BRK_4_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{0} } }, + [PPME_SYSCALL_BRK_4_X] = {FILLER_REF(sys_brk_munmap_mmap_x)}, + [PPME_SYSCALL_MMAP_E] = {FILLER_REF(sys_mmap_e)}, + [PPME_SYSCALL_MMAP_X] = {FILLER_REF(sys_brk_munmap_mmap_x)}, + [PPME_SYSCALL_MMAP2_E] = {FILLER_REF(sys_mmap_e)}, + [PPME_SYSCALL_MMAP2_X] = {FILLER_REF(sys_brk_munmap_mmap_x)}, + [PPME_SYSCALL_MUNMAP_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {1} } }, + [PPME_SYSCALL_MUNMAP_X] = {FILLER_REF(sys_brk_munmap_mmap_x)}, + [PPME_SYSCALL_SPLICE_E] = {FILLER_REF(sys_autofill), 4, APT_REG, {{0}, {2}, {4}, {5} } }, + [PPME_SYSCALL_SPLICE_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_PTRACE_E] = {FILLER_REF(sys_ptrace_e)}, + [PPME_SYSCALL_PTRACE_X] = {FILLER_REF(sys_ptrace_x)}, + [PPME_SYSCALL_IOCTL_3_E] = {FILLER_REF(sys_autofill), 3, APT_REG, {{0}, {1}, {2} } }, + [PPME_SYSCALL_IOCTL_3_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_RENAME_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_RENAME_X] = {FILLER_REF(sys_autofill), 3, APT_REG, {{AF_ID_RETVAL}, {0}, {1} } }, + [PPME_SYSCALL_RENAMEAT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_RENAMEAT_X] = {FILLER_REF(sys_renameat_x)}, + [PPME_SYSCALL_SYMLINK_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_SYMLINK_X] = {FILLER_REF(sys_autofill), 3, APT_REG, {{AF_ID_RETVAL}, {0}, {1} } }, + [PPME_SYSCALL_SYMLINKAT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_SYMLINKAT_X] = {FILLER_REF(sys_symlinkat_x)}, + [PPME_SYSCALL_SENDFILE_E] = {FILLER_REF(sys_sendfile_e)}, + [PPME_SYSCALL_SENDFILE_X] = {FILLER_REF(sys_sendfile_x)}, + [PPME_SYSCALL_QUOTACTL_E] = {FILLER_REF(sys_quotactl_e)}, + [PPME_SYSCALL_QUOTACTL_X] = {FILLER_REF(sys_quotactl_x)}, + [PPME_SYSCALL_SETRESUID_E] = {FILLER_REF(sys_autofill), 3, APT_REG, {{0}, {1}, {2} } }, + [PPME_SYSCALL_SETRESUID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_SETRESGID_E] = {FILLER_REF(sys_autofill), 3, APT_REG, {{0}, {1}, {2} } }, + [PPME_SYSCALL_SETRESGID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSDIGEVENT_E] = {FILLER_REF(sys_sysdigevent_e)}, + [PPME_SYSCALL_SETUID_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{0} } }, + [PPME_SYSCALL_SETUID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_SETGID_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{0} } }, + [PPME_SYSCALL_SETGID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_GETUID_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_GETUID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_GETEUID_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_GETEUID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_GETGID_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_GETGID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_GETEGID_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_GETEGID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_GETRESUID_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_GETRESUID_X] = {FILLER_REF(sys_getresuid_and_gid_x)}, + [PPME_SYSCALL_GETRESGID_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_GETRESGID_X] = {FILLER_REF(sys_getresuid_and_gid_x)}, + [PPME_SYSCALL_CLONE_20_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_CLONE_20_X] = {FILLER_REF(proc_startupdate)}, + [PPME_SYSCALL_FORK_20_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_FORK_20_X] = {FILLER_REF(proc_startupdate)}, + [PPME_SYSCALL_VFORK_20_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_VFORK_20_X] = {FILLER_REF(proc_startupdate)}, + #ifdef CAPTURE_SIGNAL_DELIVERIES + [PPME_SIGNALDELIVER_E] = {FILLER_REF(sys_signaldeliver_e)}, + [PPME_SIGNALDELIVER_X] = {FILLER_REF(sys_empty)}, + #endif + [PPME_SYSCALL_GETDENTS_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_GETDENTS_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_GETDENTS64_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_GETDENTS64_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_SETNS_E] = {FILLER_REF(sys_setns_e)}, + [PPME_SYSCALL_SETNS_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_FLOCK_E] = {FILLER_REF(sys_flock_e)}, + [PPME_SYSCALL_FLOCK_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_CPU_HOTPLUG_E] = {FILLER_REF(cpu_hotplug_e)}, + [PPME_SOCKET_ACCEPT_5_E] = {FILLER_REF(sys_empty)}, + [PPME_SOCKET_ACCEPT_5_X] = {FILLER_REF(sys_accept_x)}, + [PPME_SOCKET_ACCEPT4_5_E] = {FILLER_REF(sys_accept4_e)}, + [PPME_SOCKET_ACCEPT4_5_X] = {FILLER_REF(sys_accept_x)}, + [PPME_SYSCALL_SEMOP_E] = {FILLER_REF(sys_single)}, + [PPME_SYSCALL_SEMOP_X] = {FILLER_REF(sys_semop_x)}, + [PPME_SYSCALL_SEMCTL_E] = {FILLER_REF(sys_semctl_e)}, + [PPME_SYSCALL_SEMCTL_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_PPOLL_E] = {FILLER_REF(sys_ppoll_e)}, + [PPME_SYSCALL_PPOLL_X] = {FILLER_REF(sys_poll_x)}, /* exit same for poll() and ppoll() */ + [PPME_SYSCALL_MOUNT_E] = {FILLER_REF(sys_mount_e)}, + [PPME_SYSCALL_MOUNT_X] = {FILLER_REF(sys_autofill), 4, APT_REG, {{AF_ID_RETVAL}, {0}, {1}, {2} } }, + [PPME_SYSCALL_UMOUNT_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{1} } }, + [PPME_SYSCALL_UMOUNT_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_SEMGET_E] = {FILLER_REF(sys_semget_e)}, + [PPME_SYSCALL_SEMGET_X] = {FILLER_REF(sys_single_x)}, + [PPME_SYSCALL_ACCESS_E] = {FILLER_REF(sys_access_e)}, + [PPME_SYSCALL_ACCESS_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_CHROOT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_CHROOT_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_SETSID_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_SETSID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_SETPGID_E] = {FILLER_REF(sys_autofill), 2, APT_REG, {{0}, {1} } }, + [PPME_SYSCALL_SETPGID_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_MKDIR_2_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_USEDEFAULT, 0} } }, + [PPME_SYSCALL_MKDIR_2_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_RMDIR_2_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_RMDIR_2_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_UNSHARE_E] = {FILLER_REF(sys_unshare_e)}, + [PPME_SYSCALL_UNSHARE_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_EXECVE_19_E] = {FILLER_REF(sys_execve_e)}, + [PPME_SYSCALL_EXECVE_19_X] = {FILLER_REF(proc_startupdate)}, + #ifdef CAPTURE_PAGE_FAULTS + [PPME_PAGE_FAULT_E] = {FILLER_REF(sys_pagefault_e)}, + [PPME_PAGE_FAULT_X] = {FILLER_REF(sys_empty)}, + #endif + [PPME_SYSCALL_BPF_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{0} } }, + [PPME_SYSCALL_BPF_X] = {FILLER_REF(sys_bpf_x)}, + [PPME_SYSCALL_SECCOMP_E] = {FILLER_REF(sys_autofill), 1, APT_REG, {{0}, {1} } }, + [PPME_SYSCALL_SECCOMP_X] = {FILLER_REF(sys_autofill), 1, APT_REG, {{AF_ID_RETVAL} } }, + [PPME_SYSCALL_UNLINK_2_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_UNLINK_2_X] = {FILLER_REF(sys_autofill), 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, + [PPME_SYSCALL_UNLINKAT_2_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_UNLINKAT_2_X] = {FILLER_REF(sys_unlinkat_x)}, + [PPME_SYSCALL_MKDIRAT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_MKDIRAT_X] = {FILLER_REF(sys_mkdirat_x)}, + [PPME_SYSCALL_OPENAT_2_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_OPENAT_2_X] = {FILLER_REF(sys_openat_x)}, + [PPME_SYSCALL_LINK_2_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_LINK_2_X] = {FILLER_REF(sys_autofill), 3, APT_REG, {{AF_ID_RETVAL}, {0}, {1} } }, + [PPME_SYSCALL_LINKAT_2_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_LINKAT_2_X] = {FILLER_REF(sys_linkat_x)}, + [PPME_SYSCALL_FCHMODAT_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_FCHMODAT_X] = {FILLER_REF(sys_fchmodat_x)}, + [PPME_SYSCALL_CHMOD_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_CHMOD_X] = {FILLER_REF(sys_chmod_x)}, + [PPME_SYSCALL_FCHMOD_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_FCHMOD_X] = {FILLER_REF(sys_fchmod_x)}, + [PPME_SYSCALL_RENAMEAT2_E] = {FILLER_REF(sys_empty)}, + [PPME_SYSCALL_RENAMEAT2_X] = {FILLER_REF(sys_renameat2_x)} +}; diff --git a/driver/flags_table.c b/driver/flags_table.c index 0c55ac648e..ab311f14eb 100644 --- a/driver/flags_table.c +++ b/driver/flags_table.c @@ -1,207 +1,527 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "ppm_events_public.h" const struct ppm_name_value socket_families[] = { - {"AF_NFC", 39}, - {"AF_ALG", 38}, - {"AF_CAIF", 37}, - {"AF_IEEE802154", 36}, - {"AF_PHONET", 35}, - {"AF_ISDN", 34}, - {"AF_RXRPC", 33}, - {"AF_IUCV", 32}, - {"AF_BLUETOOTH", 31}, - {"AF_TIPC", 30}, - {"AF_CAN", 29}, - {"AF_LLC", 26}, - {"AF_WANPIPE", 25}, - {"AF_PPPOX", 24}, - {"AF_IRDA", 23}, - {"AF_SNA", 22}, - {"AF_RDS", 21}, - {"AF_ATMSVC", 20}, - {"AF_ECONET", 19}, - {"AF_ASH", 18}, - {"AF_PACKET", 17}, - {"AF_ROUTE", PPM_AF_NETLINK}, - {"AF_NETLINK", 16}, - {"AF_KEY", 15}, - {"AF_SECURITY", 14}, - {"AF_NETBEUI", 13}, - {"AF_DECnet", 12}, - {"AF_ROSE", 11}, - {"AF_INET6", 10}, - {"AF_X25", 9}, - {"AF_ATMPVC", 8}, - {"AF_BRIDGE", 7}, - {"AF_NETROM", 6}, - {"AF_APPLETALK", 5}, - {"AF_IPX", 4}, - {"AF_AX25", 3}, - {"AF_INET", 2}, - {"AF_LOCAL", 1}, - {"AF_UNIX", 1}, - {"AF_UNSPEC", 0}, - { }, + {"AF_NFC", PPM_AF_NFC}, + {"AF_ALG", PPM_AF_ALG}, + {"AF_CAIF", PPM_AF_CAIF}, + {"AF_IEEE802154", PPM_AF_IEEE802154}, + {"AF_PHONET", PPM_AF_PHONET}, + {"AF_ISDN", PPM_AF_ISDN}, + {"AF_RXRPC", PPM_AF_RXRPC}, + {"AF_IUCV", PPM_AF_IUCV}, + {"AF_BLUETOOTH", PPM_AF_BLUETOOTH}, + {"AF_TIPC", PPM_AF_TIPC}, + {"AF_CAN", PPM_AF_CAN}, + {"AF_LLC", PPM_AF_LLC}, + {"AF_WANPIPE", PPM_AF_WANPIPE}, + {"AF_PPPOX", PPM_AF_PPPOX}, + {"AF_IRDA", PPM_AF_IRDA}, + {"AF_SNA", PPM_AF_SNA}, + {"AF_RDS", PPM_AF_RDS}, + {"AF_ATMSVC", PPM_AF_ATMSVC}, + {"AF_ECONET", PPM_AF_ECONET}, + {"AF_ASH", PPM_AF_ASH}, + {"AF_PACKET", PPM_AF_PACKET}, + {"AF_ROUTE", PPM_AF_ROUTE}, + {"AF_NETLINK", PPM_AF_NETLINK}, + {"AF_KEY", PPM_AF_KEY}, + {"AF_SECURITY", PPM_AF_SECURITY}, + {"AF_NETBEUI", PPM_AF_NETBEUI}, + {"AF_DECnet", PPM_AF_DECnet}, + {"AF_ROSE", PPM_AF_ROSE}, + {"AF_INET6", PPM_AF_INET6}, + {"AF_X25", PPM_AF_X25}, + {"AF_ATMPVC", PPM_AF_ATMPVC}, + {"AF_BRIDGE", PPM_AF_BRIDGE}, + {"AF_NETROM", PPM_AF_NETROM}, + {"AF_APPLETALK", PPM_AF_APPLETALK}, + {"AF_IPX", PPM_AF_IPX}, + {"AF_AX25", PPM_AF_AX25}, + {"AF_INET", PPM_AF_INET}, + {"AF_LOCAL", PPM_AF_LOCAL}, + {"AF_UNIX", PPM_AF_UNIX}, + {"AF_UNSPEC", PPM_AF_UNSPEC}, + {0, 0}, }; const struct ppm_name_value file_flags[] = { - {"O_LARGEFILE", (1 << 11)}, - {"O_DIRECTORY", (1 << 10)}, - {"O_DIRECT", (1 << 9)}, - {"O_TRUNC", (1 << 8)}, - {"O_SYNC", (1 << 7)}, - {"O_NONBLOCK", (1 << 6)}, - {"O_EXCL", (1 << 5)}, - {"O_DSYNC", (1 << 4)}, - {"O_APPEND", (1 << 3)}, - {"O_CREAT", (1 << 2)}, - {"O_RDWR", (PPM_O_RDONLY | PPM_O_WRONLY)}, - {"O_WRONLY", (1 << 1)}, - {"O_RDONLY", (1 << 0)}, - {"O_NONE", 0}, - { }, + {"O_LARGEFILE", PPM_O_LARGEFILE}, + {"O_DIRECTORY", PPM_O_DIRECTORY}, + {"O_DIRECT", PPM_O_DIRECT}, + {"O_TRUNC", PPM_O_TRUNC}, + {"O_SYNC", PPM_O_SYNC}, + {"O_NONBLOCK", PPM_O_NONBLOCK}, + {"O_EXCL", PPM_O_EXCL}, + {"O_DSYNC", PPM_O_DSYNC}, + {"O_APPEND", PPM_O_APPEND}, + {"O_CREAT", PPM_O_CREAT}, + {"O_RDWR", PPM_O_RDWR}, + {"O_WRONLY", PPM_O_WRONLY}, + {"O_RDONLY", PPM_O_RDONLY}, + {"O_CLOEXEC", PPM_O_CLOEXEC}, + {"O_NONE", PPM_O_NONE}, + {"O_TMPFILE", PPM_O_TMPFILE}, + {0, 0}, +}; + +const struct ppm_name_value flock_flags[] = { + {"LOCK_SH", PPM_LOCK_SH}, + {"LOCK_EX", PPM_LOCK_EX}, + {"LOCK_NB", PPM_LOCK_NB}, + {"LOCK_UN", PPM_LOCK_UN}, + {"LOCK_NONE", PPM_LOCK_NONE}, + {0, 0}, }; const struct ppm_name_value clone_flags[] = { - {"CLONE_FILES", (1 << 0)}, - {"CLONE_FS", (1 << 1)}, - {"CLONE_IO", (1 << 2)}, - {"CLONE_NEWIPC", (1 << 3)}, - {"CLONE_NEWNET", (1 << 4)}, - {"CLONE_NEWNS", (1 << 5)}, - {"CLONE_NEWPID", (1 << 6)}, - {"CLONE_NEWUTS", (1 << 7)}, - {"CLONE_PARENT", (1 << 8)}, - {"CLONE_PARENT_SETTID", (1 << 9)}, - {"CLONE_PTRACE", (1 << 10)}, - {"CLONE_SIGHAND", (1 << 11)}, - {"CLONE_SYSVSEM", (1 << 12)}, - {"CLONE_THREAD", (1 << 13)}, - {"CLONE_UNTRACED", (1 << 14)}, - {"CLONE_VM", (1 << 15)}, - {"CLONE_INVERTED", (1 << 16)}, - {"NAME_CHANGED", (1 << 17)}, - {"CLOSED", (1 << 18)}, - { }, + {"CLONE_FILES", PPM_CL_CLONE_FILES}, + {"CLONE_FS", PPM_CL_CLONE_FS}, + {"CLONE_IO", PPM_CL_CLONE_IO}, + {"CLONE_NEWIPC", PPM_CL_CLONE_NEWIPC}, + {"CLONE_NEWNET", PPM_CL_CLONE_NEWNET}, + {"CLONE_NEWNS", PPM_CL_CLONE_NEWNS}, + {"CLONE_NEWPID", PPM_CL_CLONE_NEWPID}, + {"CLONE_NEWUTS", PPM_CL_CLONE_NEWUTS}, + {"CLONE_PARENT", PPM_CL_CLONE_PARENT}, + {"CLONE_PARENT_SETTID", PPM_CL_CLONE_PARENT_SETTID}, + {"CLONE_PTRACE", PPM_CL_CLONE_PTRACE}, + {"CLONE_SIGHAND", PPM_CL_CLONE_SIGHAND}, + {"CLONE_SYSVSEM", PPM_CL_CLONE_SYSVSEM}, + {"CLONE_THREAD", PPM_CL_CLONE_THREAD}, + {"CLONE_UNTRACED", PPM_CL_CLONE_UNTRACED}, + {"CLONE_VM", PPM_CL_CLONE_VM}, + {"CLONE_INVERTED", PPM_CL_CLONE_INVERTED}, + {"NAME_CHANGED", PPM_CL_NAME_CHANGED}, + {"CLOSED", PPM_CL_CLOSED}, + {"CLONE_NEWUSER", PPM_CL_CLONE_NEWUSER}, + {"CLONE_CHILD_CLEARTID", PPM_CL_CLONE_CHILD_CLEARTID}, + {"CLONE_CHILD_SETTID", PPM_CL_CLONE_CHILD_SETTID}, + {"CLONE_SETTLS", PPM_CL_CLONE_SETTLS}, + {"CLONE_STOPPED", PPM_CL_CLONE_STOPPED}, + {"CLONE_VFORK", PPM_CL_CLONE_VFORK}, + {"CLONE_NEWCGROUP", PPM_CL_CLONE_NEWCGROUP}, + {0, 0}, }; const struct ppm_name_value futex_operations[] = { - {"FUTEX_CLOCK_REALTIME", 256}, - {"FUTEX_PRIVATE_FLAG", 128}, - {"FUTEX_CMP_REQUEUE_PI", 12}, - {"FUTEX_WAIT_REQUEUE_PI", 11}, - {"FUTEX_WAKE_BITSET", 10}, - {"FUTEX_WAIT_BITSET", 9}, - {"FUTEX_TRYLOCK_PI", 8}, - {"FUTEX_UNLOCK_PI", 7}, - {"FUTEX_LOCK_PI", 6}, - {"FUTEX_WAKE_OP", 5}, - {"FUTEX_CMP_REQUEUE", 4}, - {"FUTEX_REQUEUE", 3}, - {"FUTEX_FD", 2}, - {"FUTEX_WAKE", 1}, - {"FUTEX_WAIT", 0}, - { }, + {"FUTEX_CLOCK_REALTIME", PPM_FU_FUTEX_CLOCK_REALTIME}, + {"FUTEX_PRIVATE_FLAG", PPM_FU_FUTEX_PRIVATE_FLAG}, + {"FUTEX_CMP_REQUEUE_PI", PPM_FU_FUTEX_CMP_REQUEUE_PI}, + {"FUTEX_WAIT_REQUEUE_PI", PPM_FU_FUTEX_WAIT_REQUEUE_PI}, + {"FUTEX_WAKE_BITSET", PPM_FU_FUTEX_WAKE_BITSET}, + {"FUTEX_WAIT_BITSET", PPM_FU_FUTEX_WAIT_BITSET}, + {"FUTEX_TRYLOCK_PI", PPM_FU_FUTEX_TRYLOCK_PI}, + {"FUTEX_UNLOCK_PI", PPM_FU_FUTEX_UNLOCK_PI}, + {"FUTEX_LOCK_PI", PPM_FU_FUTEX_LOCK_PI}, + {"FUTEX_WAKE_OP", PPM_FU_FUTEX_WAKE_OP}, + {"FUTEX_CMP_REQUEUE", PPM_FU_FUTEX_CMP_REQUEUE}, + {"FUTEX_REQUEUE", PPM_FU_FUTEX_REQUEUE}, + {"FUTEX_FD", PPM_FU_FUTEX_FD}, + {"FUTEX_WAKE", PPM_FU_FUTEX_WAKE}, + {"FUTEX_WAIT", PPM_FU_FUTEX_WAIT}, + {0, 0}, }; const struct ppm_name_value poll_flags[] = { - {"POLLIN", (1 << 0)}, - {"POLLPRI", (1 << 1)}, - {"POLLOUT", (1 << 2)}, - {"POLLRDHUP", (1 << 3)}, - {"POLLERR", (1 << 4)}, - {"POLLHUP", (1 << 5)}, - {"POLLNVAL", (1 << 6)}, - {"POLLRDNORM", (1 << 7)}, - {"POLLRDBAND", (1 << 8)}, - {"POLLWRNORM", (1 << 9)}, - {"POLLWRBAND", (1 << 10)}, - { }, + {"POLLIN", PPM_POLLIN}, + {"POLLPRI", PPM_POLLPRI}, + {"POLLOUT", PPM_POLLOUT}, + {"POLLRDHUP", PPM_POLLRDHUP}, + {"POLLERR", PPM_POLLERR}, + {"POLLHUP", PPM_POLLHUP}, + {"POLLNVAL", PPM_POLLNVAL}, + {"POLLRDNORM", PPM_POLLRDNORM}, + {"POLLRDBAND", PPM_POLLRDBAND}, + {"POLLWRNORM", PPM_POLLWRNORM}, + {"POLLWRBAND", PPM_POLLWRBAND}, + {0, 0}, }; -const struct ppm_name_value lseek_whence[] = { - {"SEEK_END", 2}, - {"SEEK_CUR", 1}, - {"SEEK_SET", 0}, - { }, +/* http://lxr.free-electrons.com/source/include/uapi/linux/fs.h?v=4.2#L65 */ +const struct ppm_name_value mount_flags[] = { + {"RDONLY", PPM_MS_RDONLY}, + {"NOSUID", PPM_MS_NOSUID}, + {"NODEV", PPM_MS_NODEV}, + {"NOEXEC", PPM_MS_NOEXEC}, + {"SYNCHRONOUS", PPM_MS_SYNCHRONOUS}, + {"REMOUNT", PPM_MS_REMOUNT}, + {"MANDLOCK", PPM_MS_MANDLOCK}, + {"DIRSYNC", PPM_MS_DIRSYNC}, + {"NOATIME", PPM_MS_NOATIME}, + {"NODIRATIME", PPM_MS_NODIRATIME}, + {"BIND", PPM_MS_BIND}, + {"MOVE", PPM_MS_MOVE}, + {"REC", PPM_MS_REC}, + {"SILENT", PPM_MS_SILENT}, + {"POSIXACL", PPM_MS_POSIXACL}, + {"UNBINDABLE", PPM_MS_UNBINDABLE}, + {"PRIVATE", PPM_MS_PRIVATE}, + {"SLAVE", PPM_MS_SLAVE}, + {"SHARED", PPM_MS_SHARED}, + {"RELATIME", PPM_MS_RELATIME}, + {"KERNMOUNT", PPM_MS_KERNMOUNT}, + {"I_VERSION", PPM_MS_I_VERSION}, + {"STRICTATIME", PPM_MS_STRICTATIME}, + {"LAZYTIME", PPM_MS_LAZYTIME}, + {"NOSEC", PPM_MS_NOSEC}, + {"BORN", PPM_MS_BORN}, + {"ACTIVE", PPM_MS_ACTIVE}, + {"NOUSER", PPM_MS_NOUSER}, + {0, 0}, }; -const struct ppm_name_value shutdown_how[] = { - {"SHUT_RDWR", 2}, - {"SHUT_WR", 1}, - {"SHUT_RD", 0}, - { }, +/* http://lxr.free-electrons.com/source/include/linux/fs.h?v=4.2#L1251 */ +const struct ppm_name_value umount_flags[] = { + {"FORCE", PPM_MNT_FORCE}, + {"DETACH", PPM_MNT_DETACH}, + {"EXPIRE", PPM_MNT_EXPIRE}, + {"NOFOLLOW", PPM_UMOUNT_NOFOLLOW}, + {0, 0}, +}; + +const struct ppm_name_value lseek_whence[] = { + {"SEEK_END", PPM_SEEK_END}, + {"SEEK_CUR", PPM_SEEK_CUR}, + {"SEEK_SET", PPM_SEEK_SET}, + {0, 0}, }; -const struct ppm_name_value openat_flags[] = { - {"AT_FDCWD", -100}, - { }, +const struct ppm_name_value shutdown_how[] = { + {"SHUT_RDWR", PPM_SHUT_RDWR}, + {"SHUT_WR", PPM_SHUT_WR}, + {"SHUT_RD", PPM_SHUT_RD}, + {0, 0}, }; const struct ppm_name_value rlimit_resources[] = { - {"RLIMIT_UNKNOWN", 255}, - {"RLIMIT_RTTIME", 15}, - {"RLIMIT_RTPRIO", 14}, - {"RLIMIT_NICE", 13}, - {"RLIMIT_MSGQUEUE", 12}, - {"RLIMIT_SIGPENDING", 11}, - {"RLIMIT_LOCKS", 10}, - {"RLIMIT_AS", 9}, - {"RLIMIT_MEMLOCK", 8}, - {"RLIMIT_NOFILE", 7}, - {"RLIMIT_NPROC", 6}, - {"RLIMIT_RSS", 5}, - {"RLIMIT_CORE", 4}, - {"RLIMIT_STACK", 3}, - {"RLIMIT_DATA", 2}, - {"RLIMIT_FSIZE", 1}, - {"RLIMIT_CPU", 0}, - { }, + {"RLIMIT_UNKNOWN", PPM_RLIMIT_UNKNOWN}, + {"RLIMIT_RTTIME", PPM_RLIMIT_RTTIME}, + {"RLIMIT_RTPRIO", PPM_RLIMIT_RTPRIO}, + {"RLIMIT_NICE", PPM_RLIMIT_NICE}, + {"RLIMIT_MSGQUEUE", PPM_RLIMIT_MSGQUEUE}, + {"RLIMIT_SIGPENDING", PPM_RLIMIT_SIGPENDING}, + {"RLIMIT_LOCKS", PPM_RLIMIT_LOCKS}, + {"RLIMIT_AS", PPM_RLIMIT_AS}, + {"RLIMIT_MEMLOCK", PPM_RLIMIT_MEMLOCK}, + {"RLIMIT_NOFILE", PPM_RLIMIT_NOFILE}, + {"RLIMIT_NPROC", PPM_RLIMIT_NPROC}, + {"RLIMIT_RSS", PPM_RLIMIT_RSS}, + {"RLIMIT_CORE", PPM_RLIMIT_CORE}, + {"RLIMIT_STACK", PPM_RLIMIT_STACK}, + {"RLIMIT_DATA", PPM_RLIMIT_DATA}, + {"RLIMIT_FSIZE", PPM_RLIMIT_FSIZE}, + {"RLIMIT_CPU", PPM_RLIMIT_CPU}, + {0, 0}, }; const struct ppm_name_value fcntl_commands[] = { - {"F_GETPIPE_SZ", 29}, - {"F_SETPIPE_SZ", 28}, - {"F_NOTIFY", 27}, - {"F_DUPFD_CLOEXEC", 26}, - {"F_CANCELLK", 25}, - {"F_GETLEASE", 24}, - {"F_SETLEASE", 23}, - {"F_GETOWN_EX", 22}, - {"F_SETOWN_EX", 21}, - {"F_SETLKW64", 19}, - {"F_SETLK64", 18}, - {"F_GETLK64", 17}, - {"F_GETSIG", 15}, - {"F_SETSIG", 13}, - {"F_GETOWN", 12}, - {"F_SETOWN", 10}, - {"F_SETLKW", 9}, - {"F_SETLK", 8}, - {"F_GETLK", 6}, - {"F_SETFL", 5}, - {"F_GETFL", 4}, - {"F_SETFD", 3}, - {"F_GETFD", 2}, - {"F_DUPFD", 1}, - {"UNKNOWN", 0}, - { }, + {"F_GETPIPE_SZ", PPM_FCNTL_F_GETPIPE_SZ}, + {"F_SETPIPE_SZ", PPM_FCNTL_F_SETPIPE_SZ}, + {"F_NOTIFY", PPM_FCNTL_F_NOTIFY}, + {"F_DUPFD_CLOEXEC", PPM_FCNTL_F_DUPFD_CLOEXEC}, + {"F_CANCELLK", PPM_FCNTL_F_CANCELLK}, + {"F_GETLEASE", PPM_FCNTL_F_GETLEASE}, + {"F_SETLEASE", PPM_FCNTL_F_SETLEASE}, + {"F_GETOWN_EX", PPM_FCNTL_F_GETOWN_EX}, + {"F_SETOWN_EX", PPM_FCNTL_F_SETOWN_EX}, +#ifndef CONFIG_64BIT + {"F_SETLKW64", PPM_FCNTL_F_SETLKW64}, + {"F_SETLK64", PPM_FCNTL_F_SETLK64}, + {"F_GETLK64", PPM_FCNTL_F_GETLK64}, +#endif + {"F_GETSIG", PPM_FCNTL_F_GETSIG}, + {"F_SETSIG", PPM_FCNTL_F_SETSIG}, + {"F_GETOWN", PPM_FCNTL_F_GETOWN}, + {"F_SETOWN", PPM_FCNTL_F_SETOWN}, + {"F_SETLKW", PPM_FCNTL_F_SETLKW}, + {"F_SETLK", PPM_FCNTL_F_SETLK}, + {"F_GETLK", PPM_FCNTL_F_GETLK}, + {"F_SETFL", PPM_FCNTL_F_SETFL}, + {"F_GETFL", PPM_FCNTL_F_GETFL}, + {"F_SETFD", PPM_FCNTL_F_SETFD}, + {"F_GETFD", PPM_FCNTL_F_GETFD}, + {"F_DUPFD", PPM_FCNTL_F_DUPFD}, + {"F_OFD_GETLK", PPM_FCNTL_F_OFD_GETLK}, + {"F_OFD_SETLK", PPM_FCNTL_F_OFD_SETLK}, + {"F_OFD_SETLKW", PPM_FCNTL_F_OFD_SETLKW}, + {"UNKNOWN", PPM_FCNTL_UNKNOWN}, + {0, 0}, +}; + +const struct ppm_name_value sockopt_levels[] = { + {"SOL_SOCKET", PPM_SOCKOPT_LEVEL_SOL_SOCKET}, + {"SOL_TCP", PPM_SOCKOPT_LEVEL_SOL_TCP}, + {"UNKNOWN", PPM_SOCKOPT_LEVEL_UNKNOWN}, + {0, 0}, +}; + +const struct ppm_name_value sockopt_options[] = { + {"SO_COOKIE", PPM_SOCKOPT_SO_COOKIE}, + {"SO_MEMINFO", PPM_SOCKOPT_SO_MEMINFO}, + {"SO_PEERGROUPS", PPM_SOCKOPT_SO_PEERGROUPS}, + {"SO_ATTACH_BPF", PPM_SOCKOPT_SO_ATTACH_BPF}, + {"SO_INCOMING_CPU", PPM_SOCKOPT_SO_INCOMING_CPU}, + {"SO_BPF_EXTENSIONS", PPM_SOCKOPT_SO_BPF_EXTENSIONS}, + {"SO_MAX_PACING_RATE", PPM_SOCKOPT_SO_MAX_PACING_RATE}, + {"SO_BUSY_POLL", PPM_SOCKOPT_SO_BUSY_POLL}, + {"SO_SELECT_ERR_QUEUE", PPM_SOCKOPT_SO_SELECT_ERR_QUEUE}, + {"SO_LOCK_FILTER", PPM_SOCKOPT_SO_LOCK_FILTER}, + {"SO_NOFCS", PPM_SOCKOPT_SO_NOFCS}, + {"SO_PEEK_OFF", PPM_SOCKOPT_SO_PEEK_OFF}, + {"SO_WIFI_STATUS", PPM_SOCKOPT_SO_WIFI_STATUS}, + {"SO_RXQ_OVFL", PPM_SOCKOPT_SO_RXQ_OVFL}, + {"SO_DOMAIN", PPM_SOCKOPT_SO_DOMAIN}, + {"SO_PROTOCOL", PPM_SOCKOPT_SO_PROTOCOL}, + {"SO_TIMESTAMPING", PPM_SOCKOPT_SO_TIMESTAMPING}, + {"SO_MARK", PPM_SOCKOPT_SO_MARK}, + {"SO_TIMESTAMPNS", PPM_SOCKOPT_SO_TIMESTAMPNS}, + {"SO_PASSSEC", PPM_SOCKOPT_SO_PASSSEC}, + {"SO_PEERSEC", PPM_SOCKOPT_SO_PEERSEC}, + {"SO_ACCEPTCONN", PPM_SOCKOPT_SO_ACCEPTCONN}, + {"SO_TIMESTAMP", PPM_SOCKOPT_SO_TIMESTAMP}, + {"SO_PEERNAME", PPM_SOCKOPT_SO_PEERNAME}, + {"SO_DETACH_FILTER", PPM_SOCKOPT_SO_DETACH_FILTER}, + {"SO_ATTACH_FILTER", PPM_SOCKOPT_SO_ATTACH_FILTER}, + {"SO_BINDTODEVICE", PPM_SOCKOPT_SO_BINDTODEVICE}, + {"SO_SECURITY_ENCRYPTION_NETWORK", PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_NETWORK}, + {"SO_SECURITY_ENCRYPTION_TRANSPORT", PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_TRANSPORT}, + {"SO_SECURITY_AUTHENTICATION", PPM_SOCKOPT_SO_SECURITY_AUTHENTICATION}, + {"SO_SNDTIMEO", PPM_SOCKOPT_SO_SNDTIMEO}, + {"SO_RCVTIMEO", PPM_SOCKOPT_SO_RCVTIMEO}, + {"SO_SNDLOWAT", PPM_SOCKOPT_SO_SNDLOWAT}, + {"SO_RCVLOWAT", PPM_SOCKOPT_SO_RCVLOWAT}, + {"SO_PEERCRED", PPM_SOCKOPT_SO_PEERCRED}, + {"SO_PASSCRED", PPM_SOCKOPT_SO_PASSCRED}, + {"SO_REUSEPORT", PPM_SOCKOPT_SO_REUSEPORT}, + {"SO_BSDCOMPAT", PPM_SOCKOPT_SO_BSDCOMPAT}, + {"SO_LINGER", PPM_SOCKOPT_SO_LINGER}, + {"SO_PRIORITY", PPM_SOCKOPT_SO_PRIORITY}, + {"SO_NO_CHECK", PPM_SOCKOPT_SO_NO_CHECK}, + {"SO_OOBINLINE", PPM_SOCKOPT_SO_OOBINLINE}, + {"SO_KEEPALIVE", PPM_SOCKOPT_SO_KEEPALIVE}, + {"SO_RCVBUFFORCE", PPM_SOCKOPT_SO_RCVBUFFORCE}, + {"SO_SNDBUFFORCE", PPM_SOCKOPT_SO_SNDBUFFORCE}, + {"SO_RCVBUF", PPM_SOCKOPT_SO_RCVBUF}, + {"SO_SNDBUF", PPM_SOCKOPT_SO_SNDBUF}, + {"SO_BROADCAST", PPM_SOCKOPT_SO_BROADCAST}, + {"SO_DONTROUTE", PPM_SOCKOPT_SO_DONTROUTE}, + {"SO_ERROR", PPM_SOCKOPT_SO_ERROR}, + {"SO_TYPE", PPM_SOCKOPT_SO_TYPE}, + {"SO_REUSEADDR", PPM_SOCKOPT_SO_REUSEADDR}, + {"SO_DEBUG", PPM_SOCKOPT_SO_DEBUG}, + {"UNKNOWN", PPM_SOCKOPT_UNKNOWN}, + {0, 0}, +}; + +const struct ppm_name_value ptrace_requests[] = { + {"PTRACE_SINGLEBLOCK", PPM_PTRACE_SINGLEBLOCK}, + {"PTRACE_SYSEMU_SINGLESTEP", PPM_PTRACE_SYSEMU_SINGLESTEP}, + {"PTRACE_SYSEMU", PPM_PTRACE_SYSEMU}, + {"PTRACE_ARCH_PRCTL", PPM_PTRACE_ARCH_PRCTL}, + {"PTRACE_SET_THREAD_AREA", PPM_PTRACE_SET_THREAD_AREA}, + {"PTRACE_GET_THREAD_AREA", PPM_PTRACE_GET_THREAD_AREA}, + {"PTRACE_OLDSETOPTIONS", PPM_PTRACE_OLDSETOPTIONS}, + {"PTRACE_SETFPXREGS", PPM_PTRACE_SETFPXREGS}, + {"PTRACE_GETFPXREGS", PPM_PTRACE_GETFPXREGS}, + {"PTRACE_SETFPREGS", PPM_PTRACE_SETFPREGS}, + {"PTRACE_GETFPREGS", PPM_PTRACE_GETFPREGS}, + {"PTRACE_SETREGS", PPM_PTRACE_SETREGS}, + {"PTRACE_GETREGS", PPM_PTRACE_GETREGS}, + {"PTRACE_SETSIGMASK", PPM_PTRACE_SETSIGMASK}, + {"PTRACE_GETSIGMASK", PPM_PTRACE_GETSIGMASK}, + {"PTRACE_PEEKSIGINFO", PPM_PTRACE_PEEKSIGINFO}, + {"PTRACE_LISTEN", PPM_PTRACE_LISTEN}, + {"PTRACE_INTERRUPT", PPM_PTRACE_INTERRUPT}, + {"PTRACE_SEIZE", PPM_PTRACE_SEIZE}, + {"PTRACE_SETREGSET", PPM_PTRACE_SETREGSET}, + {"PTRACE_GETREGSET", PPM_PTRACE_GETREGSET}, + {"PTRACE_SETSIGINFO", PPM_PTRACE_SETSIGINFO}, + {"PTRACE_GETSIGINFO", PPM_PTRACE_GETSIGINFO}, + {"PTRACE_GETEVENTMSG", PPM_PTRACE_GETEVENTMSG}, + {"PTRACE_SETOPTIONS", PPM_PTRACE_SETOPTIONS}, + {"PTRACE_SYSCALL", PPM_PTRACE_SYSCALL}, + {"PTRACE_DETACH", PPM_PTRACE_DETACH}, + {"PTRACE_ATTACH", PPM_PTRACE_ATTACH}, + {"PTRACE_SINGLESTEP", PPM_PTRACE_SINGLESTEP}, + {"PTRACE_KILL", PPM_PTRACE_KILL}, + {"PTRACE_CONT", PPM_PTRACE_CONT}, + {"PTRACE_POKEUSR", PPM_PTRACE_POKEUSR}, + {"PTRACE_POKEDATA", PPM_PTRACE_POKEDATA}, + {"PTRACE_POKETEXT", PPM_PTRACE_POKETEXT}, + {"PTRACE_PEEKUSR", PPM_PTRACE_PEEKUSR}, + {"PTRACE_PEEKDATA", PPM_PTRACE_PEEKDATA}, + {"PTRACE_PEEKTEXT", PPM_PTRACE_PEEKTEXT}, + {"PTRACE_TRACEME", PPM_PTRACE_TRACEME}, + {"PTRACE_UNKNOWN", PPM_PTRACE_UNKNOWN}, + {0, 0}, +}; + +const struct ppm_name_value prot_flags[] = { + {"PROT_READ", PPM_PROT_READ}, + {"PROT_WRITE", PPM_PROT_WRITE}, + {"PROT_EXEC", PPM_PROT_EXEC}, + {"PROT_SEM", PPM_PROT_SEM}, + {"PROT_GROWSDOWN", PPM_PROT_GROWSDOWN}, + {"PROT_GROWSUP", PPM_PROT_GROWSUP}, + {"PROT_SAO", PPM_PROT_SAO}, + {"PROT_NONE", PPM_PROT_NONE}, + {0, 0}, +}; + +const struct ppm_name_value mmap_flags[] = { + {"MAP_SHARED", PPM_MAP_SHARED}, + {"MAP_PRIVATE", PPM_MAP_PRIVATE}, + {"MAP_FIXED", PPM_MAP_FIXED}, + {"MAP_ANONYMOUS", PPM_MAP_ANONYMOUS}, + {"MAP_32BIT", PPM_MAP_32BIT}, + {"MAP_RENAME", PPM_MAP_RENAME}, + {"MAP_NORESERVE", PPM_MAP_NORESERVE}, + {"MAP_POPULATE", PPM_MAP_POPULATE}, + {"MAP_NONBLOCK", PPM_MAP_NONBLOCK}, + {"MAP_GROWSDOWN", PPM_MAP_GROWSDOWN}, + {"MAP_DENYWRITE", PPM_MAP_DENYWRITE}, + {"MAP_EXECUTABLE", PPM_MAP_EXECUTABLE}, + {"MAP_INHERIT", PPM_MAP_INHERIT}, + {"MAP_FILE", PPM_MAP_FILE}, + {"MAP_LOCKED", PPM_MAP_LOCKED}, + {0, 0}, +}; + +const struct ppm_name_value splice_flags[] = { + {"SPLICE_F_MOVE", PPM_SPLICE_F_MOVE}, + {"SPLICE_F_NONBLOCK", PPM_SPLICE_F_NONBLOCK}, + {"SPLICE_F_MORE", PPM_SPLICE_F_MORE}, + {"SPLICE_F_GIFT", PPM_SPLICE_F_GIFT}, + {0, 0}, +}; + +const struct ppm_name_value quotactl_dqi_flags[] = { + {"DQF_NONE", PPM_DQF_NONE}, + {"V1_DQF_RSQUASH", PPM_V1_DQF_RSQUASH}, + {0, 0}, +}; + +const struct ppm_name_value quotactl_cmds[] = { + {"Q_QUOTAON", PPM_Q_QUOTAON}, + {"Q_QUOTAOFF", PPM_Q_QUOTAOFF}, + {"Q_GETFMT", PPM_Q_GETFMT}, + {"Q_GETINFO", PPM_Q_GETINFO}, + {"Q_SETINFO", PPM_Q_SETINFO}, + {"Q_GETQUOTA", PPM_Q_GETQUOTA}, + {"Q_SETQUOTA", PPM_Q_SETQUOTA}, + {"Q_SYNC", PPM_Q_SYNC}, + {"Q_XQUOTAON", PPM_Q_XQUOTAON}, + {"Q_XQUOTAOFF", PPM_Q_XQUOTAOFF}, + {"Q_XGETQUOTA", PPM_Q_XGETQUOTA}, + {"Q_XSETQLIM", PPM_Q_XSETQLIM}, + {"Q_XGETQSTAT", PPM_Q_XGETQSTAT}, + {"Q_XQUOTARM", PPM_Q_XQUOTARM}, + {"Q_XQUOTASYNC", PPM_Q_XQUOTASYNC}, + {0, 0}, +}; + +const struct ppm_name_value quotactl_types[] = { + {"USRQUOTA", PPM_USRQUOTA}, + {"GRPQUOTA", PPM_GRPQUOTA}, + {0, 0}, +}; + +const struct ppm_name_value quotactl_quota_fmts[] = { + {"QFMT_NOT_USED", PPM_QFMT_NOT_USED}, + {"QFMT_VFS_OLD", PPM_QFMT_VFS_OLD}, + {"QFMT_VFS_V0", PPM_QFMT_VFS_V0}, + {"QFMT_VFS_V1", PPM_QFMT_VFS_V1}, + {0, 0}, +}; + +const struct ppm_name_value semop_flags[] = { + {"IPC_NOWAIT", PPM_IPC_NOWAIT}, + {"SEM_UNDO", PPM_SEM_UNDO}, + {0, 0}, +}; + +const struct ppm_name_value semget_flags[] = { + {"IPC_EXCL", PPM_IPC_EXCL}, + {"IPC_CREAT", PPM_IPC_CREAT}, + {0, 0}, +}; + +const struct ppm_name_value semctl_commands[] = { + {"IPC_STAT", PPM_IPC_STAT}, + {"IPC_SET", PPM_IPC_SET}, + {"IPC_RMID", PPM_IPC_RMID}, + {"IPC_INFO", PPM_IPC_INFO}, + {"SEM_INFO", PPM_SEM_INFO}, + {"SEM_STAT", PPM_SEM_STAT}, + {"GETALL", PPM_GETALL}, + {"GETNCNT", PPM_GETNCNT}, + {"GETPID", PPM_GETPID}, + {"GETVAL", PPM_GETVAL}, + {"GETZCNT", PPM_GETZCNT}, + {"SETALL", PPM_SETALL}, + {"SETVAL", PPM_SETVAL}, + {0, 0}, +}; + +const struct ppm_name_value access_flags[] = { + {"F_OK", PPM_F_OK}, + {"R_OK", PPM_R_OK}, + {"W_OK", PPM_W_OK}, + {"X_OK", PPM_X_OK}, + {0, 0}, +}; + +const struct ppm_name_value pf_flags[] = { + {"PROTECTION_VIOLATION", PPM_PF_PROTECTION_VIOLATION}, + {"PAGE_NOT_PRESENT", PPM_PF_PAGE_NOT_PRESENT}, + {"WRITE_ACCESS", PPM_PF_WRITE_ACCESS}, + {"READ_ACCESS", PPM_PF_READ_ACCESS}, + {"USER_FAULT", PPM_PF_USER_FAULT}, + {"SUPERVISOR_FAULT", PPM_PF_SUPERVISOR_FAULT}, + {"RESERVED_PAGE", PPM_PF_RESERVED_PAGE}, + {"INSTRUCTION_FETCH", PPM_PF_INSTRUCTION_FETCH}, + {0, 0}, +}; + +const struct ppm_name_value unlinkat_flags[] = { + {"AT_REMOVEDIR", PPM_AT_REMOVEDIR}, + {0, 0}, +}; + +const struct ppm_name_value linkat_flags[] = { + {"AT_SYMLINK_FOLLOW", PPM_AT_SYMLINK_FOLLOW}, + {"AT_EMPTY_PATH", PPM_AT_EMPTY_PATH}, + {0, 0}, +}; + +const struct ppm_name_value chmod_mode[] = { + {"S_IXOTH", PPM_S_IXOTH}, + {"S_IWOTH", PPM_S_IWOTH}, + {"S_IROTH", PPM_S_IROTH}, + {"S_IXGRP", PPM_S_IXGRP}, + {"S_IWGRP", PPM_S_IWGRP}, + {"S_IRGRP", PPM_S_IRGRP}, + {"S_IXUSR", PPM_S_IXUSR}, + {"S_IWUSR", PPM_S_IWUSR}, + {"S_IRUSR", PPM_S_IRUSR}, + {"S_ISVTX", PPM_S_ISVTX}, + {"S_ISGID", PPM_S_ISGID}, + {"S_ISUID", PPM_S_ISUID}, + {0, 0}, +}; + +const struct ppm_name_value renameat2_flags[] = { + {"RENAME_NOREPLACE", PPM_RENAME_NOREPLACE}, + {"RENAME_EXCHANGE", PPM_RENAME_EXCHANGE}, + {"RENAME_WHITEOUT", PPM_RENAME_WHITEOUT}, + {0, 0}, }; diff --git a/driver/kernel_hacks.h b/driver/kernel_hacks.h new file mode 100644 index 0000000000..af6379d27a --- /dev/null +++ b/driver/kernel_hacks.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2020 Draios Inc. dba Sysdig. + * + * This file is dual licensed under either the MIT or GPL 2. See MIT.txt + * or GPL2.txt for full copies of the license. + */ + +/** + * @file kernel_hacks.h + * + * This file contains kernel-version-dependent preprocessor instructions to + * help the driver compile on as many kernel versions as possible. + */ + +#include + +/* probe_kernel_read() only added in kernel 2.6.26, name changed in 5.8.0 */ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26) +static inline long copy_from_kernel_nofault(void *dst, const void *src, size_t size) +{ + long ret; + mm_segment_t old_fs = get_fs(); + + set_fs(KERNEL_DS); + pagefault_disable(); + ret = __copy_from_user_inatomic(dst, (__force const void __user *)src, size); + pagefault_enable(); + set_fs(old_fs); + + return ret ? -EFAULT : 0; +} +#elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 8, 0) +#define copy_from_kernel_nofault probe_kernel_read +#endif + + +/* + * Linux 5.6 kernels no longer include the old 32-bit timeval + * structures. But the syscalls (might) still use them. + */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0) +#include +struct compat_timespec { + int32_t tv_sec; + int32_t tv_nsec; +}; + +struct timespec { + int32_t tv_sec; + int32_t tv_nsec; +}; + +struct timeval { + int32_t tv_sec; + int32_t tv_usec; +}; +#else +#define timeval64 timeval +#endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) +static inline struct inode *file_inode(struct file *f) +{ + return f->f_path.dentry->d_inode; +} +#endif + +/* + * Linux 5.1 kernels modify the syscall_get_arguments function to always + * return all arguments rather than allowing the caller to select which + * arguments are desired. This wrapper replicates the original + * functionality. + */ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0)) +#define syscall_get_arguments_deprecated syscall_get_arguments +#else +#define syscall_get_arguments_deprecated(_task, _reg, _start, _len, _args) \ + do { \ + unsigned long _sga_args[6] = {}; \ + syscall_get_arguments(_task, _reg, _sga_args); \ + memcpy(_args, &_sga_args[_start], _len * sizeof(unsigned long)); \ + } while(0) +#endif diff --git a/driver/main.c b/driver/main.c index 881aa63d12..fab205eff3 100644 --- a/driver/main.c +++ b/driver/main.c @@ -1,50 +1,66 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 20) +#include +#include +#include "ppm_syscall.h" +#include +#else +#include +#endif +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37)) #include +#else +#include +#endif #include #include #include #include #include #include +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)) #include -#include +#else +#include +#include +#endif +#include #include -#include -#include -#if defined(__x86_64__) -#include +#include +#include +#include +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)) +#include #else -#include +#include #endif +#include +#include +#include "driver_config.h" #include "ppm_ringbuffer.h" #include "ppm_events_public.h" #include "ppm_events.h" #include "ppm.h" +#if defined(CONFIG_IA32_EMULATION) && !defined(__NR_ia32_socketcall) +#include "ppm_compat_unistd_32.h" +#endif MODULE_LICENSE("GPL"); -MODULE_AUTHOR("Draios"); - -#define PPM_DEVICE_NAME "sysdig" +MODULE_AUTHOR("sysdig inc"); -#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)) #define TRACEPOINT_PROBE_REGISTER(p1, p2) tracepoint_probe_register(p1, p2) #define TRACEPOINT_PROBE_UNREGISTER(p1, p2) tracepoint_probe_unregister(p1, p2) #define TRACEPOINT_PROBE(probe, args...) static void probe(args) @@ -54,24 +70,41 @@ MODULE_AUTHOR("Draios"); #define TRACEPOINT_PROBE(probe, args...) static void probe(void *__data, args) #endif +#ifndef pgprot_encrypted +#define pgprot_encrypted(x) (x) +#endif + struct ppm_device { dev_t dev; struct cdev cdev; wait_queue_head_t read_queue; }; -/* - * The ring descriptor. - * We have one of these for each CPU. - */ -struct ppm_ring_buffer_context { - atomic_t state; - struct ppm_ring_buffer_info *info; - char *buffer; - struct timespec last_print_time; - uint32_t nevents; - atomic_t preempt_count; - char *str_storage; /* String storage. Size is one page. */ +struct event_data_t { + enum ppm_capture_category category; + int socketcall_syscall; + bool compat; + + union { + struct { + struct pt_regs *regs; + long id; + const enum ppm_syscall_code *cur_g_syscall_code_routing_table; + } syscall_data; + + struct { + struct task_struct *sched_prev; + struct task_struct *sched_next; + } context_data; + + struct { + int sig; + struct siginfo *info; + struct k_sigaction *ka; + } signal_data; + + struct fault_data_t fault_data; + } event_info; }; /* @@ -81,12 +114,24 @@ static int ppm_open(struct inode *inode, struct file *filp); static int ppm_release(struct inode *inode, struct file *filp); static long ppm_ioctl(struct file *f, unsigned int cmd, unsigned long arg); static int ppm_mmap(struct file *filp, struct vm_area_struct *vma); -static void record_event(enum ppm_event_type event_type, - struct pt_regs *regs, - long id, - int never_drop, - struct task_struct *sched_prev, - struct task_struct *sched_next); +static int record_event_consumer(struct ppm_consumer_t *consumer, + enum ppm_event_type event_type, + enum syscall_flags drop_flags, + nanoseconds ns, + struct event_data_t *event_datap); +static void record_event_all_consumers(enum ppm_event_type event_type, + enum syscall_flags drop_flags, + struct event_data_t *event_datap); +static int init_ring_buffer(struct ppm_ring_buffer_context *ring); +static void free_ring_buffer(struct ppm_ring_buffer_context *ring); +static void reset_ring_buffer(struct ppm_ring_buffer_context *ring); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) +void ppm_task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st); +#endif + +#ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS + #error The kernel must have HAVE_SYSCALL_TRACEPOINTS in order for sysdig to be useful +#endif TRACEPOINT_PROBE(syscall_enter_probe, struct pt_regs *regs, long id); TRACEPOINT_PROBE(syscall_exit_probe, struct pt_regs *regs, long ret); @@ -94,15 +139,29 @@ TRACEPOINT_PROBE(syscall_procexit_probe, struct task_struct *p); #ifdef CAPTURE_CONTEXT_SWITCHES #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)) TRACEPOINT_PROBE(sched_switch_probe, struct rq *rq, struct task_struct *prev, struct task_struct *next); -#else +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) TRACEPOINT_PROBE(sched_switch_probe, struct task_struct *prev, struct task_struct *next); +#else +TRACEPOINT_PROBE(sched_switch_probe, bool preempt, struct task_struct *prev, struct task_struct *next); #endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)) */ #endif /* CAPTURE_CONTEXT_SWITCHES */ +#ifdef CAPTURE_SIGNAL_DELIVERIES +TRACEPOINT_PROBE(signal_deliver_probe, int sig, struct siginfo *info, struct k_sigaction *ka); +#endif + +#ifdef CAPTURE_PAGE_FAULTS +TRACEPOINT_PROBE(page_fault_probe, unsigned long address, struct pt_regs *regs, unsigned long error_code); +#endif + +DECLARE_BITMAP(g_events_mask, PPM_EVENT_MAX); static struct ppm_device *g_ppm_devs; static struct class *g_ppm_class; static unsigned int g_ppm_numdevs; static int g_ppm_major; +bool g_tracers_enabled = false; +bool g_simple_mode_enabled = false; +static DEFINE_PER_CPU(long, g_n_tracepoint_hit); static const struct file_operations g_ppm_fops = { .open = ppm_open, .release = ppm_release, @@ -114,14 +173,135 @@ static const struct file_operations g_ppm_fops = { /* * GLOBALS */ +LIST_HEAD(g_consumer_list); +static DEFINE_MUTEX(g_consumer_mutex); +static bool g_tracepoint_registered; + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) +static struct tracepoint *tp_sys_enter; +static struct tracepoint *tp_sys_exit; +#endif + +static struct tracepoint *tp_sched_process_exit; +#ifdef CAPTURE_CONTEXT_SWITCHES +static struct tracepoint *tp_sched_switch; +#endif +#ifdef CAPTURE_SIGNAL_DELIVERIES +static struct tracepoint *tp_signal_deliver; +#endif +#ifdef CAPTURE_PAGE_FAULTS +// Even in kernels that can support page fault tracepoints, tracepoints may be +// disabled so check if g_fault_tracepoint_disabled is set. +static struct tracepoint *tp_page_fault_user; +static struct tracepoint *tp_page_fault_kernel; +static bool g_fault_tracepoint_registered; +static bool g_fault_tracepoint_disabled; +#endif + +#ifdef _DEBUG +static bool verbose = 1; +#else +static bool verbose = 0; +#endif + +static unsigned int max_consumers = 5; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)) +static enum cpuhp_state hp_state = 0; +#endif + +#define vpr_info(fmt, ...) \ +do { \ + if (verbose) \ + pr_info(fmt, ##__VA_ARGS__); \ +} while (0) + +static inline nanoseconds ppm_nsecs(void) +{ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)) + return ktime_get_real_ns(); +#else + /* Don't have ktime_get_real functions */ + struct timespec ts; + getnstimeofday(&ts); + return SECOND_IN_NS * ts.tv_sec + ts.tv_nsec; +#endif +} + +inline void ppm_syscall_get_arguments(struct task_struct *task, struct pt_regs *regs, unsigned long *args) +{ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0)) + syscall_get_arguments(task, regs, 0, 6, args); +#else + syscall_get_arguments(task, regs, args); +#endif +} + +/* compat tracepoint functions */ +static int compat_register_trace(void *func, const char *probename, struct tracepoint *tp) +{ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)) + return TRACEPOINT_PROBE_REGISTER(probename, func); +#else + return tracepoint_probe_register(tp, func, NULL); +#endif +} + +static void compat_unregister_trace(void *func, const char *probename, struct tracepoint *tp) +{ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)) + TRACEPOINT_PROBE_UNREGISTER(probename, func); +#else + tracepoint_probe_unregister(tp, func, NULL); +#endif +} + +static struct ppm_consumer_t *ppm_find_consumer(struct task_struct *consumer_id) +{ + struct ppm_consumer_t *el = NULL; + + rcu_read_lock(); + list_for_each_entry_rcu(el, &g_consumer_list, node) { + if (el->consumer_id == consumer_id) { + rcu_read_unlock(); + return el; + } + } + rcu_read_unlock(); + + return NULL; +} + +static void check_remove_consumer(struct ppm_consumer_t *consumer, int remove_from_list) +{ + int cpu; + int open_rings = 0; + + for_each_possible_cpu(cpu) { + struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, cpu); + + if (ring && ring->open) + ++open_rings; + } + + if (open_rings == 0) { + pr_info("deallocating consumer %p\n", consumer->consumer_id); -static DEFINE_PER_CPU(struct ppm_ring_buffer_context*, g_ring_buffers); -static atomic_t g_open_count; -uint32_t g_snaplen = RW_SNAPLEN; -uint32_t g_sampling_ratio = 1; -static uint32_t g_sampling_interval; -static int g_is_dropping; -static int g_dropping_mode; + if (remove_from_list) { + list_del_rcu(&consumer->node); + synchronize_rcu(); + } + + for_each_possible_cpu(cpu) { + struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, cpu); + free_ring_buffer(ring); + } + + free_percpu(consumer->ring_buffers); + + vfree(consumer); + } +} /* * user I/O functions @@ -129,188 +309,567 @@ static int g_dropping_mode; static int ppm_open(struct inode *inode, struct file *filp) { int ret; - struct ppm_ring_buffer_context *ring; + int in_list = false; +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + int ring_no = iminor(filp->f_path.dentry->d_inode); +#else int ring_no = iminor(filp->f_dentry->d_inode); +#endif + struct task_struct *consumer_id = current; + struct ppm_consumer_t *consumer = NULL; + struct ppm_ring_buffer_context *ring = NULL; - trace_enter(); + /* + * Tricky: to identify a consumer, attach the thread id + * to the newly open file descriptor + */ + filp->private_data = consumer_id; + + mutex_lock(&g_consumer_mutex); - ring = per_cpu(g_ring_buffers, ring_no); + consumer = ppm_find_consumer(consumer_id); + if (!consumer) { + unsigned int cpu; + unsigned int num_consumers = 0; + struct ppm_consumer_t *el = NULL; - if (atomic_cmpxchg(&ring->state, CS_STOPPED, CS_STARTED) != CS_STOPPED) { - pr_info("sysdig-probe: invalid operation: attempting to open device %d multiple times\n", ring_no); - return -EBUSY; + rcu_read_lock(); + list_for_each_entry_rcu(el, &g_consumer_list, node) { + ++num_consumers; + } + rcu_read_unlock(); + + if (num_consumers >= max_consumers) { + pr_err("maximum number of consumers reached\n"); + ret = -EBUSY; + goto cleanup_open; + } + + pr_info("adding new consumer %p\n", consumer_id); + + consumer = vmalloc(sizeof(struct ppm_consumer_t)); + if (!consumer) { + pr_err("can't allocate consumer\n"); + ret = -ENOMEM; + goto cleanup_open; + } + + consumer->consumer_id = consumer_id; + + /* + * Initialize the ring buffers array + */ + consumer->ring_buffers = alloc_percpu(struct ppm_ring_buffer_context); + if (consumer->ring_buffers == NULL) { + pr_err("can't allocate the ring buffer array\n"); + + vfree(consumer); + + ret = -ENOMEM; + goto cleanup_open; + } + + /* + * Note, we have two loops here because the first one makes sure that ALL of the + * rings are properly initialized to null, since the second one could be interrupted + * and cause issues in the cleanup phase. + * This might not be necessary, because alloc_percpu memsets the allocated entries to + * 0, but better be extra safe. + */ + for_each_possible_cpu(cpu) { + ring = per_cpu_ptr(consumer->ring_buffers, cpu); + + ring->cpu_online = false; + ring->str_storage = NULL; + ring->buffer = NULL; + ring->info = NULL; + } + + /* + * If a cpu is offline when the consumer is first created, we + * will never get events for that cpu even if it later comes + * online via hotplug. We could allocate these rings on-demand + * later in this function if needed for hotplug, but that + * requires the consumer to know to call open again, and sysdig + * doesn't support that. + */ + for_each_online_cpu(cpu) { + ring = per_cpu_ptr(consumer->ring_buffers, cpu); + + pr_info("initializing ring buffer for CPU %u\n", cpu); + + if (!init_ring_buffer(ring)) { + pr_err("can't initialize the ring buffer for CPU %u\n", cpu); + ret = -ENOMEM; + goto err_init_ring_buffer; + } + + ring->cpu_online = true; + } + + list_add_rcu(&consumer->node, &g_consumer_list); + in_list = true; + } else { + vpr_info("found already existent consumer %p\n", consumer_id); } - g_dropping_mode = 0; - g_snaplen = RW_SNAPLEN; - g_sampling_ratio = 1; - g_sampling_interval = 0; - g_is_dropping = 0; - ring->info->head = 0; - ring->info->tail = 0; - ring->nevents = 0; - ring->info->n_evts = 0; - ring->info->n_drops_buffer = 0; - ring->info->n_drops_pf = 0; - ring->info->n_preemptions = 0; - ring->info->n_context_switches = 0; - atomic_set(&ring->preempt_count, 0); - getnstimeofday(&ring->last_print_time); + ring = per_cpu_ptr(consumer->ring_buffers, ring_no); /* - * The last open device starts the collection + * Check if the CPU pointed by this device is online. If it isn't stop here and + * return ENODEV. The cpu could be online while buffer is NULL if there's a cpu + * online hotplug callback between the first open on this consumer and the open + * for this particular device. */ - if (atomic_inc_return(&g_open_count) == g_ppm_numdevs) { - /* struct task_struct *task; */ + if (ring->cpu_online == false || ring->buffer == NULL) { + ret = -ENODEV; + goto cleanup_open; + } - pr_info("sysdig-probe: starting capture\n"); + if (ring->open) { + pr_err("invalid operation: attempting to open device %d multiple times for consumer %p\n", ring_no, consumer->consumer_id); + ret = -EBUSY; + goto cleanup_open; + } - /* - // - // Before enabling the tracepoints, we add the current list of running processes as events in buffer 0 - // - for_each_process(task) { - printk("%s [%d]\n",task->comm , task->pid); - } - */ + vpr_info("opening ring %d, consumer %p\n", ring_no, consumer->consumer_id); + /* + * ring->preempt_count is not reset to 0 on purpose, to prevent a race condition: + * if the same device is quickly closed and then reopened, record_event() might still be executing + * (with ring->preempt_count to 1) while ppm_open() resets ring->preempt_count to 0. + * When record_event() will exit, it will decrease + * ring->preempt_count which will become < 0, leading to the complete loss of all the events for that CPU. + */ + consumer->dropping_mode = 0; + consumer->snaplen = RW_SNAPLEN; + consumer->sampling_ratio = 1; + consumer->sampling_interval = 0; + consumer->is_dropping = 0; + consumer->do_dynamic_snaplen = false; + consumer->need_to_insert_drop_e = 0; + consumer->need_to_insert_drop_x = 0; + consumer->fullcapture_port_range_start = 0; + consumer->fullcapture_port_range_end = 0; + consumer->statsd_port = PPM_PORT_STATSD; + bitmap_fill(g_events_mask, PPM_EVENT_MAX); /* Enable all syscall to be passed to userspace */ + reset_ring_buffer(ring); + ring->open = true; + + if (!g_tracepoint_registered) { + pr_info("starting capture\n"); /* * Enable the tracepoints */ - ret = TRACEPOINT_PROBE_REGISTER("sys_exit", (void *) syscall_exit_probe); + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + ret = compat_register_trace(syscall_exit_probe, "sys_exit", tp_sys_exit); +#else + ret = register_trace_syscall_exit(syscall_exit_probe); +#endif if (ret) { - pr_err("sysdig-probe: can't create the sys_exit tracepoint\n"); - return ret; + pr_err("can't create the sys_exit tracepoint\n"); + goto err_sys_exit; } - ret = TRACEPOINT_PROBE_REGISTER("sys_enter", (void *) syscall_enter_probe); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + ret = compat_register_trace(syscall_enter_probe, "sys_enter", tp_sys_enter); +#else + ret = register_trace_syscall_enter(syscall_enter_probe); +#endif if (ret) { - TRACEPOINT_PROBE_UNREGISTER("sys_exit", - (void *) syscall_exit_probe); - - pr_err("sysdig-probe: can't create the sys_enter tracepoint\n"); - - return ret; + pr_err("can't create the sys_enter tracepoint\n"); + goto err_sys_enter; } - ret = TRACEPOINT_PROBE_REGISTER("sched_process_exit", (void *) syscall_procexit_probe); + ret = compat_register_trace(syscall_procexit_probe, "sched_process_exit", tp_sched_process_exit); if (ret) { - TRACEPOINT_PROBE_UNREGISTER("sys_exit", - (void *) syscall_exit_probe); - TRACEPOINT_PROBE_UNREGISTER("sys_enter", - (void *) syscall_enter_probe); - - pr_err("sysdig-probe: can't create the sched_process_exit tracepoint\n"); - - return ret; + pr_err("can't create the sched_process_exit tracepoint\n"); + goto err_sched_procexit; } #ifdef CAPTURE_CONTEXT_SWITCHES - ret = TRACEPOINT_PROBE_REGISTER("sched_switch", (void *) sched_switch_probe); + ret = compat_register_trace(sched_switch_probe, "sched_switch", tp_sched_switch); if (ret) { - TRACEPOINT_PROBE_UNREGISTER("sys_exit", - (void *) syscall_exit_probe); - TRACEPOINT_PROBE_UNREGISTER("sys_enter", - (void *) syscall_enter_probe); - TRACEPOINT_PROBE_UNREGISTER("sched_process_exit", - (void *) syscall_procexit_probe); - - pr_err("sysdig-probe: can't create the sched_switch tracepoint\n"); + pr_err("can't create the sched_switch tracepoint\n"); + goto err_sched_switch; + } +#endif - return ret; +#ifdef CAPTURE_SIGNAL_DELIVERIES + ret = compat_register_trace(signal_deliver_probe, "signal_deliver", tp_signal_deliver); + if (ret) { + pr_err("can't create the signal_deliver tracepoint\n"); + goto err_signal_deliver; } #endif + g_tracepoint_registered = true; } - return 0; + ret = 0; + + goto cleanup_open; + +#ifdef CAPTURE_SIGNAL_DELIVERIES +err_signal_deliver: + compat_unregister_trace(sched_switch_probe, "sched_switch", tp_sched_switch); +#endif +err_sched_switch: + compat_unregister_trace(syscall_procexit_probe, "sched_process_exit", tp_sched_process_exit); +err_sched_procexit: +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + compat_unregister_trace(syscall_enter_probe, "sys_enter", tp_sys_enter); +#else + unregister_trace_syscall_enter(syscall_enter_probe); +#endif +err_sys_enter: +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + compat_unregister_trace(syscall_exit_probe, "sys_exit", tp_sys_exit); +#else + unregister_trace_syscall_exit(syscall_exit_probe); +#endif +err_sys_exit: + ring->open = false; +err_init_ring_buffer: + check_remove_consumer(consumer, in_list); +cleanup_open: + mutex_unlock(&g_consumer_mutex); + + return ret; } static int ppm_release(struct inode *inode, struct file *filp) { + int cpu; + int ret; struct ppm_ring_buffer_context *ring; +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + int ring_no = iminor(filp->f_path.dentry->d_inode); +#else int ring_no = iminor(filp->f_dentry->d_inode); +#endif + struct task_struct *consumer_id = filp->private_data; + struct ppm_consumer_t *consumer = NULL; - trace_enter(); + mutex_lock(&g_consumer_mutex); - ring = per_cpu(g_ring_buffers, ring_no); + consumer = ppm_find_consumer(consumer_id); + if (!consumer) { + pr_err("release: unknown consumer %p\n", consumer_id); + ret = -EBUSY; + goto cleanup_release; + } + + ring = per_cpu_ptr(consumer->ring_buffers, ring_no); + if (!ring) { + ASSERT(false); + ret = -ENODEV; + goto cleanup_release; + } - if (atomic_xchg(&ring->state, CS_STOPPED) == CS_STOPPED) { - pr_info("sysdig-probe: attempting to close unopened device %d\n", ring_no); - return -EBUSY; + if (!ring->open) { + pr_err("attempting to close unopened device %d for consumer %p\n", ring_no, consumer_id); + ret = -EBUSY; + goto cleanup_release; } - pr_info("sysdig-probe: closing ring %d, evt:%llu, dr_buf:%llu, dr_pf:%llu, pr:%llu, cs:%llu\n", + ring->capture_enabled = false; + + vpr_info("closing ring %d, consumer:%p evt:%llu, dr_buf:%llu, dr_pf:%llu, pr:%llu, cs:%llu\n", ring_no, + consumer_id, ring->info->n_evts, ring->info->n_drops_buffer, ring->info->n_drops_pf, ring->info->n_preemptions, ring->info->n_context_switches); + ring->open = false; + + check_remove_consumer(consumer, true); + /* * The last closed device stops event collection */ - if (atomic_dec_return(&g_open_count) == 0) { - pr_info("sysdig-probe: stopping capture\n"); + if (list_empty(&g_consumer_list)) { + if (g_tracepoint_registered) { + pr_info("no more consumers, stopping capture\n"); - TRACEPOINT_PROBE_UNREGISTER("sys_exit", - (void *) syscall_exit_probe); - - TRACEPOINT_PROBE_UNREGISTER("sys_enter", - (void *) syscall_enter_probe); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + compat_unregister_trace(syscall_exit_probe, "sys_exit", tp_sys_exit); + compat_unregister_trace(syscall_enter_probe, "sys_enter", tp_sys_enter); +#else + unregister_trace_syscall_exit(syscall_exit_probe); + unregister_trace_syscall_enter(syscall_enter_probe); +#endif + compat_unregister_trace(syscall_procexit_probe, "sched_process_exit", tp_sched_process_exit); - TRACEPOINT_PROBE_UNREGISTER("sched_process_exit", - (void *) syscall_procexit_probe); #ifdef CAPTURE_CONTEXT_SWITCHES - TRACEPOINT_PROBE_UNREGISTER("sched_switch", - (void *) sched_switch_probe); + compat_unregister_trace(sched_switch_probe, "sched_switch", tp_sched_switch); #endif +#ifdef CAPTURE_SIGNAL_DELIVERIES + compat_unregister_trace(signal_deliver_probe, "signal_deliver", tp_signal_deliver); +#endif +#ifdef CAPTURE_PAGE_FAULTS + if (g_fault_tracepoint_registered) { + compat_unregister_trace(page_fault_probe, "page_fault_user", tp_page_fault_user); + compat_unregister_trace(page_fault_probe, "page_fault_kernel", tp_page_fault_kernel); + + g_fault_tracepoint_registered = false; + } +#endif +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + tracepoint_synchronize_unregister(); +#endif + g_tracepoint_registered = false; + + /* + * While we're here, disable simple mode if it's active + */ + g_simple_mode_enabled = false; + + /* + * Reset tracepoint counter + */ + for_each_possible_cpu(cpu) { + per_cpu(g_n_tracepoint_hit, cpu) = 0; + } + } else { + ASSERT(false); + } } - return 0; + ret = 0; + +cleanup_release: + mutex_unlock(&g_consumer_mutex); + + return ret; } static long ppm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { + int cpu; + int ret; + struct task_struct *consumer_id = filp->private_data; + struct ppm_consumer_t *consumer = NULL; + + if (cmd == PPM_IOCTL_GET_PROCLIST) { + struct ppm_proclist_info *proclist_info = NULL; + struct task_struct *p, *t; + u64 nentries = 0; + struct ppm_proclist_info pli; + u32 memsize; + + if (copy_from_user(&pli, (void *)arg, sizeof(pli))) { + ret = -EINVAL; + goto cleanup_ioctl_nolock; + } + + if(pli.max_entries < 0 || pli.max_entries > 1000000) + { + vpr_info("PPM_IOCTL_GET_PROCLIST: invalid max_entries %llu\n", pli.max_entries); + ret = -EINVAL; + goto cleanup_ioctl_procinfo; + } + + vpr_info("PPM_IOCTL_GET_PROCLIST, size=%d\n", (int)pli.max_entries); + + memsize = sizeof(struct ppm_proclist_info) + sizeof(struct ppm_proc_info) * pli.max_entries; + proclist_info = vmalloc(memsize); + if (!proclist_info) { + ret = -EINVAL; + goto cleanup_ioctl_nolock; + } + + proclist_info->max_entries = pli.max_entries; + + rcu_read_lock(); + +#ifdef for_each_process_thread + for_each_process_thread(p, t) { +#else +#ifdef for_each_process_all + for_each_process_all(p) { +#else + for_each_process(p) { +#endif + t = p; + do { + task_lock(p); +#endif + if (nentries < pli.max_entries) { +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)) + cputime_t utime, stime; +#else + u64 utime, stime; +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) + task_cputime_adjusted(t, &utime, &stime); +#else + ppm_task_cputime_adjusted(t, &utime, &stime); +#endif + proclist_info->entries[nentries].pid = t->pid; +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)) + proclist_info->entries[nentries].utime = cputime_to_clock_t(utime); + proclist_info->entries[nentries].stime = cputime_to_clock_t(stime); +#else + proclist_info->entries[nentries].utime = nsec_to_clock_t(utime); + proclist_info->entries[nentries].stime = nsec_to_clock_t(stime); +#endif + } + + nentries++; +#ifdef for_each_process_thread + } +#else + task_unlock(p); +#ifdef while_each_thread_all + } while_each_thread_all(p, t); + } +#else + } while_each_thread(p, t); + } +#endif +#endif + + rcu_read_unlock(); + + proclist_info->n_entries = nentries; + + if (nentries >= pli.max_entries) { + vpr_info("PPM_IOCTL_GET_PROCLIST: not enough space (%d avail, %d required)\n", + (int)pli.max_entries, + (int)nentries); + + if (copy_to_user((void *)arg, proclist_info, sizeof(struct ppm_proclist_info))) { + ret = -EINVAL; + goto cleanup_ioctl_procinfo; + } + + ret = -ENOSPC; + goto cleanup_ioctl_procinfo; + } else { + memsize = sizeof(struct ppm_proclist_info) + sizeof(struct ppm_proc_info) * nentries; + + if (copy_to_user((void *)arg, proclist_info, memsize)) { + ret = -EINVAL; + goto cleanup_ioctl_procinfo; + } + } + + ret = 0; +cleanup_ioctl_procinfo: + vfree((void *)proclist_info); + goto cleanup_ioctl_nolock; + } + + if (cmd == PPM_IOCTL_GET_N_TRACEPOINT_HIT) { + long __user *counters = (long __user *) arg; + + for_each_possible_cpu(cpu) { + if (put_user(per_cpu(g_n_tracepoint_hit, cpu), &counters[cpu])) { + ret = -EINVAL; + goto cleanup_ioctl_nolock; + } + } + ret = 0; + goto cleanup_ioctl_nolock; + } else if (cmd == PPM_IOCTL_GET_PROBE_VERSION) { + if (copy_to_user((void *)arg, PROBE_VERSION, sizeof(PROBE_VERSION))) { + ret = -EINVAL; + goto cleanup_ioctl_nolock; + } + ret = 0; + goto cleanup_ioctl_nolock; + } + + mutex_lock(&g_consumer_mutex); + + consumer = ppm_find_consumer(consumer_id); + if (!consumer) { + pr_err("ioctl: unknown consumer %p\n", consumer_id); + ret = -EBUSY; + goto cleanup_ioctl; + } + switch (cmd) { case PPM_IOCTL_DISABLE_CAPTURE: { +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + int ring_no = iminor(filp->f_path.dentry->d_inode); +#else int ring_no = iminor(filp->f_dentry->d_inode); - struct ppm_ring_buffer_context *ring = per_cpu(g_ring_buffers, ring_no); +#endif + struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, ring_no); - atomic_set(&(ring->state), CS_INACTIVE); + if (!ring) { + ASSERT(false); + ret = -ENODEV; + goto cleanup_ioctl; + } - pr_info("sysdig-probe: PPM_IOCTL_DISABLE_CAPTURE for ring %d\n", ring_no); + ring->capture_enabled = false; - return 0; + vpr_info("PPM_IOCTL_DISABLE_CAPTURE for ring %d, consumer %p\n", ring_no, consumer_id); + + ret = 0; + goto cleanup_ioctl; } case PPM_IOCTL_ENABLE_CAPTURE: { +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + int ring_no = iminor(filp->f_path.dentry->d_inode); +#else int ring_no = iminor(filp->f_dentry->d_inode); - struct ppm_ring_buffer_context *ring = per_cpu(g_ring_buffers, ring_no); +#endif + struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, ring_no); - atomic_set(&ring->state, CS_STARTED); + if (!ring) { + ASSERT(false); + ret = -ENODEV; + goto cleanup_ioctl; + } - pr_info("sysdig-probe: PPM_IOCTL_ENABLE_CAPTURE for ring %d\n", ring_no); + ring->capture_enabled = true; - return 0; + vpr_info("PPM_IOCTL_ENABLE_CAPTURE for ring %d, consumer %p\n", ring_no, consumer_id); + + ret = 0; + goto cleanup_ioctl; } case PPM_IOCTL_DISABLE_DROPPING_MODE: { - g_dropping_mode = 0; - pr_info("sysdig-probe: PPM_IOCTL_DISABLE_DROPPING_MODE\n"); - g_sampling_interval = 1000000000; - g_sampling_ratio = 1; - return 0; + struct event_data_t event_data; + + vpr_info("PPM_IOCTL_DISABLE_DROPPING_MODE, consumer %p\n", consumer_id); + + consumer->dropping_mode = 0; + consumer->sampling_interval = 1000000000; + consumer->sampling_ratio = 1; + + /* + * Push an event into the ring buffer so that the user can know that dropping + * mode has been disabled + */ + event_data.category = PPMC_CONTEXT_SWITCH; + event_data.event_info.context_data.sched_prev = (void *)DEI_DISABLE_DROPPING; + event_data.event_info.context_data.sched_next = (void *)0; + + record_event_consumer(consumer, PPME_SYSDIGEVENT_E, UF_NEVER_DROP, ppm_nsecs(), &event_data); + + ret = 0; + goto cleanup_ioctl; } case PPM_IOCTL_ENABLE_DROPPING_MODE: { - uint32_t new_sampling_ratio; + u32 new_sampling_ratio; - g_dropping_mode = 1; - pr_info("sysdig-probe: PPM_IOCTL_ENABLE_DROPPING_MODE\n"); + consumer->dropping_mode = 1; + vpr_info("PPM_IOCTL_ENABLE_DROPPING_MODE, consumer %p\n", consumer_id); - new_sampling_ratio = (uint32_t)arg; + new_sampling_ratio = (u32)arg; if (new_sampling_ratio != 1 && new_sampling_ratio != 2 && @@ -320,53 +879,289 @@ static long ppm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) new_sampling_ratio != 32 && new_sampling_ratio != 64 && new_sampling_ratio != 128) { - pr_info("sysdig-probe: invalid sampling ratio %u\n", new_sampling_ratio); - return -EINVAL; + pr_err("invalid sampling ratio %u\n", new_sampling_ratio); + ret = -EINVAL; + goto cleanup_ioctl; } - g_sampling_interval = 1000000000 / new_sampling_ratio; - g_sampling_ratio = new_sampling_ratio; + consumer->sampling_interval = 1000000000 / new_sampling_ratio; + consumer->sampling_ratio = new_sampling_ratio; - pr_info("sysdig-probe: new sampling ratio: %d\n", new_sampling_ratio); - return 0; + vpr_info("new sampling ratio: %d\n", new_sampling_ratio); + + ret = 0; + goto cleanup_ioctl; } case PPM_IOCTL_SET_SNAPLEN: { - uint32_t new_snaplen; + u32 new_snaplen; - pr_info("sysdig-probe: PPM_IOCTL_SET_SNAPLEN\n"); - new_snaplen = (uint32_t)arg; + vpr_info("PPM_IOCTL_SET_SNAPLEN, consumer %p\n", consumer_id); + new_snaplen = (u32)arg; if (new_snaplen > RW_MAX_SNAPLEN) { - pr_info("sysdig-probe: invalid snaplen %u\n", new_snaplen); - return -EINVAL; + pr_err("invalid snaplen %u\n", new_snaplen); + ret = -EINVAL; + goto cleanup_ioctl; } - g_snaplen = new_snaplen; + consumer->snaplen = new_snaplen; - pr_info("sysdig-probe: new snaplen: %d\n", g_snaplen); - return 0; + vpr_info("new snaplen: %d\n", consumer->snaplen); + + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE: + { + u32 encoded_port_range; + + vpr_info("PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE, consumer %p\n", consumer_id); + encoded_port_range = (u32)arg; + + consumer->fullcapture_port_range_start = encoded_port_range & 0xFFFF; + consumer->fullcapture_port_range_end = encoded_port_range >> 16; + + pr_info("new fullcapture_port_range_start: %d\n", (int)consumer->fullcapture_port_range_start); + pr_info("new fullcapture_port_range_end: %d\n", (int)consumer->fullcapture_port_range_end); + + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_SET_STATSD_PORT: + { + consumer->statsd_port = (u16)arg; + + pr_info("new statsd_port: %d\n", (int)consumer->statsd_port); + + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_MASK_ZERO_EVENTS: + { + vpr_info("PPM_IOCTL_MASK_ZERO_EVENTS, consumer %p\n", consumer_id); + + bitmap_zero(g_events_mask, PPM_EVENT_MAX); + + /* Used for dropping events so they must stay on */ + set_bit(PPME_DROP_E, g_events_mask); + set_bit(PPME_DROP_X, g_events_mask); + + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_MASK_SET_EVENT: + { + u32 syscall_to_set = (u32)arg; + + vpr_info("PPM_IOCTL_MASK_SET_EVENT (%u), consumer %p\n", syscall_to_set, consumer_id); + + if (syscall_to_set >= PPM_EVENT_MAX) { + pr_err("invalid syscall %u\n", syscall_to_set); + ret = -EINVAL; + goto cleanup_ioctl; + } + + set_bit(syscall_to_set, g_events_mask); + + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_MASK_UNSET_EVENT: + { + u32 syscall_to_unset = (u32)arg; + + vpr_info("PPM_IOCTL_MASK_UNSET_EVENT (%u), consumer %p\n", syscall_to_unset, consumer_id); + + if (syscall_to_unset >= PPM_EVENT_MAX) { + pr_err("invalid syscall %u\n", syscall_to_unset); + ret = -EINVAL; + goto cleanup_ioctl; + } + + clear_bit(syscall_to_unset, g_events_mask); + + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_DISABLE_DYNAMIC_SNAPLEN: + { + consumer->do_dynamic_snaplen = false; + + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_ENABLE_DYNAMIC_SNAPLEN: + { + consumer->do_dynamic_snaplen = true; + + ret = 0; + goto cleanup_ioctl; + } +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + case PPM_IOCTL_GET_VTID: + case PPM_IOCTL_GET_VPID: + { + pid_t vid; + struct pid *pid; + struct task_struct *task; + struct pid_namespace *ns; + + rcu_read_lock(); + pid = find_pid_ns(arg, &init_pid_ns); + if (!pid) { + rcu_read_unlock(); + ret = -EINVAL; + goto cleanup_ioctl; + } + + task = pid_task(pid, PIDTYPE_PID); + if (!task) { + rcu_read_unlock(); + ret = -EINVAL; + goto cleanup_ioctl; + } + + ns = ns_of_pid(pid); + if (!pid) { + rcu_read_unlock(); + ret = -EINVAL; + goto cleanup_ioctl; + } + + if (cmd == PPM_IOCTL_GET_VTID) + vid = task_pid_nr_ns(task, ns); + else + vid = task_tgid_nr_ns(task, ns); + + rcu_read_unlock(); + ret = vid; + goto cleanup_ioctl; + } +#endif +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + case PPM_IOCTL_GET_CURRENT_TID: + ret = task_pid_nr(current); + goto cleanup_ioctl; + case PPM_IOCTL_GET_CURRENT_PID: + ret = task_tgid_nr(current); + goto cleanup_ioctl; +#endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) */ +#ifdef CAPTURE_SIGNAL_DELIVERIES + case PPM_IOCTL_DISABLE_SIGNAL_DELIVER: + { + vpr_info("PPM_IOCTL_DISABLE_SIGNAL_DELIVER\n"); + if (g_tracepoint_registered) + compat_unregister_trace(signal_deliver_probe, "signal_deliver", tp_signal_deliver); + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_ENABLE_SIGNAL_DELIVER: + { + vpr_info("PPM_IOCTL_ENABLE_SIGNAL_DELIVER\n"); + if (g_tracepoint_registered) + compat_register_trace(signal_deliver_probe, "signal_deliver", tp_signal_deliver); + ret = 0; + goto cleanup_ioctl; + } +#endif + case PPM_IOCTL_SET_TRACERS_CAPTURE: + { + vpr_info("PPM_IOCTL_SET_TRACERS_CAPTURE, consumer %p\n", consumer_id); + g_tracers_enabled = true; + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_SET_SIMPLE_MODE: + { + vpr_info("PPM_IOCTL_SET_SIMPLE_MODE, consumer %p\n", consumer_id); + g_simple_mode_enabled = true; + ret = 0; + goto cleanup_ioctl; + } + case PPM_IOCTL_ENABLE_PAGE_FAULTS: + { + vpr_info("PPM_IOCTL_ENABLE_PAGE_FAULTS\n"); +#ifdef CAPTURE_PAGE_FAULTS + ASSERT(g_tracepoint_registered); + + if (g_fault_tracepoint_disabled) { + pr_err("kernel page fault tracepoints are disabled\n"); + ret = -EPERM; + goto cleanup_ioctl; + } + + if (!g_fault_tracepoint_registered) { + ret = compat_register_trace(page_fault_probe, "page_fault_user", tp_page_fault_user); + if (ret) { + pr_err("can't create the page_fault_user tracepoint\n"); + ret = -EINVAL; + goto cleanup_ioctl; + } + + ret = compat_register_trace(page_fault_probe, "page_fault_kernel", tp_page_fault_kernel); + if (ret) { + pr_err("can't create the page_fault_kernel tracepoint\n"); + ret = -EINVAL; + goto err_page_fault_kernel; + } + + g_fault_tracepoint_registered = true; + } + + ret = 0; + goto cleanup_ioctl; +#else + pr_err("kernel doesn't support page fault tracepoints\n"); + ret = -EINVAL; + goto cleanup_ioctl; +#endif } default: - return -ENOTTY; + ret = -ENOTTY; + goto cleanup_ioctl; } + +#ifdef CAPTURE_PAGE_FAULTS +err_page_fault_kernel: + compat_unregister_trace(page_fault_probe, "page_fault_user", tp_page_fault_user); +#endif +cleanup_ioctl: + mutex_unlock(&g_consumer_mutex); +cleanup_ioctl_nolock: + return ret; } static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) { - trace_enter(); + int ret; + struct task_struct *consumer_id = filp->private_data; + struct ppm_consumer_t *consumer = NULL; + + mutex_lock(&g_consumer_mutex); + + consumer = ppm_find_consumer(consumer_id); + if (!consumer) { + pr_err("mmap: unknown consumer %p\n", consumer_id); + ret = -EIO; + goto cleanup_mmap; + } if (vma->vm_pgoff == 0) { - int ret; long length = vma->vm_end - vma->vm_start; unsigned long useraddr = vma->vm_start; unsigned long pfn; char *vmalloc_area_ptr; char *orig_vmalloc_area_ptr; +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + int ring_no = iminor(filp->f_path.dentry->d_inode); +#else int ring_no = iminor(filp->f_dentry->d_inode); +#endif struct ppm_ring_buffer_context *ring; - pr_info("sysdig-probe: mmap for CPU %d, start=%lu len=%ld page_size=%lu\n", + vpr_info("mmap for consumer %p, CPU %d, start=%lu len=%ld page_size=%lu\n", + consumer_id, ring_no, useraddr, length, @@ -376,28 +1171,35 @@ static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) * Enforce ring buffer size */ if (RING_BUF_SIZE < 2 * PAGE_SIZE) { - pr_info("sysdig-probe: Ring buffer size too small (%ld bytes, must be at least %ld bytes\n", + pr_err("Ring buffer size too small (%ld bytes, must be at least %ld bytes\n", (long)RING_BUF_SIZE, (long)PAGE_SIZE); - return -EIO; + ret = -EIO; + goto cleanup_mmap; } if (RING_BUF_SIZE / PAGE_SIZE * PAGE_SIZE != RING_BUF_SIZE) { - pr_info("sysdig-probe: Ring buffer size is not a multiple of the page size\n"); - return -EIO; + pr_err("Ring buffer size is not a multiple of the page size\n"); + ret = -EIO; + goto cleanup_mmap; } /* * Retrieve the ring structure for this CPU */ - ring = per_cpu(g_ring_buffers, ring_no); + ring = per_cpu_ptr(consumer->ring_buffers, ring_no); + if (!ring) { + ASSERT(false); + ret = -ENODEV; + goto cleanup_mmap; + } if (length <= PAGE_SIZE) { /* * When the size requested by the user is smaller than a page, we assume * she's mapping the ring info structure */ - pr_info("sysdig-probe: mapping the ring info\n"); + vpr_info("mapping the ring info\n"); vmalloc_area_ptr = (char *)ring->info; orig_vmalloc_area_ptr = vmalloc_area_ptr; @@ -405,13 +1207,14 @@ static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) pfn = vmalloc_to_pfn(vmalloc_area_ptr); ret = remap_pfn_range(vma, useraddr, pfn, - PAGE_SIZE, PAGE_SHARED); + PAGE_SIZE, pgprot_encrypted(PAGE_SHARED)); if (ret < 0) { - pr_info("sysdig-probe: remap_pfn_range failed (1)\n"); - return ret; + pr_err("remap_pfn_range failed (1)\n"); + goto cleanup_mmap; } - return 0; + ret = 0; + goto cleanup_mmap; } else if (length == RING_BUF_SIZE * 2) { long mlength; @@ -419,7 +1222,7 @@ static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) * When the size requested by the user equals the ring buffer size, we map the full * buffer */ - pr_info("sysdig-probe: mapping the data buffer\n"); + vpr_info("mapping the data buffer\n"); vmalloc_area_ptr = (char *)ring->buffer; orig_vmalloc_area_ptr = vmalloc_area_ptr; @@ -427,9 +1230,10 @@ static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) /* * Validate that the buffer access is read only */ - if (vma->vm_flags & (VM_WRITE | VM_EXEC)) { - pr_info("sysdig-probe: invalid mmap flags 0x%lx\n", vma->vm_flags); - return -EIO; + if (vma->vm_flags & VM_WRITE) { + pr_err("invalid mmap flags 0x%lx\n", vma->vm_flags); + ret = -EIO; + goto cleanup_mmap; } /* @@ -441,10 +1245,10 @@ static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) pfn = vmalloc_to_pfn(vmalloc_area_ptr); ret = remap_pfn_range(vma, useraddr, pfn, - PAGE_SIZE, PAGE_SHARED); + PAGE_SIZE, pgprot_encrypted(PAGE_SHARED)); if (ret < 0) { - pr_info("sysdig-probe: remap_pfn_range failed (1)\n"); - return ret; + pr_err("remap_pfn_range failed (1)\n"); + goto cleanup_mmap; } useraddr += PAGE_SIZE; @@ -463,10 +1267,10 @@ static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) pfn = vmalloc_to_pfn(vmalloc_area_ptr); ret = remap_pfn_range(vma, useraddr, pfn, - PAGE_SIZE, PAGE_SHARED); + PAGE_SIZE, pgprot_encrypted(PAGE_SHARED)); if (ret < 0) { - pr_info("sysdig-probe: remap_pfn_range failed (1)\n"); - return ret; + pr_err("remap_pfn_range failed (1)\n"); + goto cleanup_mmap; } useraddr += PAGE_SIZE; @@ -474,15 +1278,22 @@ static int ppm_mmap(struct file *filp, struct vm_area_struct *vma) mlength -= PAGE_SIZE; } - return 0; - } else { - pr_info("sysdig-probe: Invalid mmap size %ld\n", length); - return -EIO; + ret = 0; + goto cleanup_mmap; } + + pr_err("Invalid mmap size %ld\n", length); + ret = -EIO; + goto cleanup_mmap; } - pr_info("sysdig-probe: invalid pgoff %lu, must be 0\n", vma->vm_pgoff); - return -EIO; + pr_err("invalid pgoff %lu, must be 0\n", vma->vm_pgoff); + ret = -EIO; + +cleanup_mmap: + mutex_unlock(&g_consumer_mutex); + + return ret; } /* Argument list sizes for sys_socketcall */ @@ -494,15 +1305,25 @@ static const unsigned char nas[21] = { AL(4), AL(5), AL(4) }; #undef AL +#ifdef CONFIG_COMPAT +#define AL(x) ((x) * sizeof(compat_ulong_t)) +static const unsigned char compat_nas[21] = { + AL(0), AL(3), AL(3), AL(3), AL(2), AL(3), + AL(3), AL(3), AL(4), AL(4), AL(4), AL(6), + AL(6), AL(2), AL(5), AL(5), AL(3), AL(3), + AL(4), AL(5), AL(4) +}; +#undef AL +#endif + -#ifndef __x86_64__ +#ifdef _HAS_SOCKETCALL static enum ppm_event_type parse_socketcall(struct event_filler_arguments *filler_args, struct pt_regs *regs) { - unsigned long __user args[2]; + unsigned long __user args[6] = {}; unsigned long __user *scargs; int socketcall_id; - - syscall_get_arguments(current, regs, 0, 2, args); + ppm_syscall_get_arguments(current, regs, args); socketcall_id = args[0]; scargs = (unsigned long __user *)args[1]; @@ -516,8 +1337,22 @@ static enum ppm_event_type parse_socketcall(struct event_filler_arguments *fille #endif return PPME_GENERIC_E; - if (unlikely(ppm_copy_from_user(filler_args->socketcall_args, scargs, nas[socketcall_id]))) - return PPME_GENERIC_E; +#ifdef CONFIG_COMPAT + if (unlikely(filler_args->compat)) { + compat_ulong_t socketcall_args32[6]; + int j; + + if (unlikely(ppm_copy_from_user(socketcall_args32, compat_ptr(args[1]), compat_nas[socketcall_id]))) + return PPME_GENERIC_E; + for (j = 0; j < 6; ++j) + filler_args->socketcall_args[j] = (unsigned long)socketcall_args32[j]; + } else { +#endif + if (unlikely(ppm_copy_from_user(filler_args->socketcall_args, scargs, nas[socketcall_id]))) + return PPME_GENERIC_E; +#ifdef CONFIG_COMPAT + } +#endif switch (socketcall_id) { case SYS_SOCKET: @@ -529,7 +1364,7 @@ static enum ppm_event_type parse_socketcall(struct event_filler_arguments *fille case SYS_LISTEN: return PPME_SOCKET_LISTEN_E; case SYS_ACCEPT: - return PPME_SOCKET_ACCEPT_E; + return PPME_SOCKET_ACCEPT_5_E; case SYS_GETSOCKNAME: return PPME_SOCKET_GETSOCKNAME_E; case SYS_GETPEERNAME: @@ -563,94 +1398,255 @@ static enum ppm_event_type parse_socketcall(struct event_filler_arguments *fille return PPME_SOCKET_RECVMMSG_E; #endif case SYS_ACCEPT4: - return PPME_SOCKET_ACCEPT4_E; + return PPME_SOCKET_ACCEPT4_5_E; default: ASSERT(false); return PPME_GENERIC_E; } } -#endif /* __x86_64__ */ +#endif /* _HAS_SOCKETCALL */ -static inline int drop_event(enum ppm_event_type event_type, int never_drop, struct timespec *ts) +static inline void record_drop_e(struct ppm_consumer_t *consumer, + nanoseconds ns, + enum syscall_flags drop_flags) { - if (never_drop) + struct event_data_t event_data = {0}; + + if (record_event_consumer(consumer, PPME_DROP_E, UF_NEVER_DROP, ns, &event_data) == 0) { + consumer->need_to_insert_drop_e = 1; + } else { + if (consumer->need_to_insert_drop_e == 1 && !(drop_flags & UF_ATOMIC)) { + pr_err("drop enter event delayed insert\n"); + } + + consumer->need_to_insert_drop_e = 0; + } +} + +static inline void record_drop_x(struct ppm_consumer_t *consumer, + nanoseconds ns, + enum syscall_flags drop_flags) +{ + struct event_data_t event_data = {0}; + + if (record_event_consumer(consumer, PPME_DROP_X, UF_NEVER_DROP, ns, &event_data) == 0) { + consumer->need_to_insert_drop_x = 1; + } else { + if (consumer->need_to_insert_drop_x == 1 && !(drop_flags & UF_ATOMIC)) { + pr_err("drop exit event delayed insert\n"); + } + + consumer->need_to_insert_drop_x = 0; + } +} + +// Return 1 if the event should be dropped, else 0 +static inline int drop_nostate_event(enum ppm_event_type event_type, + struct pt_regs *regs) +{ + unsigned long args[6] = {}; + unsigned long arg = 0; + int close_fd = -1; + struct files_struct *files; + struct fdtable *fdt; + bool drop = false; + + switch (event_type) { + case PPME_SYSCALL_CLOSE_X: + case PPME_SOCKET_BIND_X: + if (syscall_get_return_value(current, regs) < 0) + drop = true; + break; + case PPME_SYSCALL_CLOSE_E: + /* + * It's annoying but valid for a program to make a large number of + * close() calls on nonexistent fds. That can cause driver cpu usage + * to spike dramatically, so drop close events if the fd is not valid. + * + * The invalid fd events don't matter to userspace in dropping mode, + * so we do this before the UF_NEVER_DROP check + */ + ppm_syscall_get_arguments(current, regs, args); + arg = args[0]; + close_fd = (int)arg; + + files = current->files; + spin_lock(&files->file_lock); + fdt = files_fdtable(files); + if (close_fd < 0 || close_fd >= fdt->max_fds || +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)) + !FD_ISSET(close_fd, fdt->open_fds) +#else + !fd_is_open(close_fd, fdt) +#endif + ) { + drop = true; + } + spin_unlock(&files->file_lock); + break; + case PPME_SYSCALL_FCNTL_E: + case PPME_SYSCALL_FCNTL_X: + // cmd arg + ppm_syscall_get_arguments(current, regs, args); + arg = args[1]; + if (arg != F_DUPFD && arg != F_DUPFD_CLOEXEC) + drop = true; + break; + default: + break; + } + + if (drop) + return 1; + else return 0; +} - if (g_dropping_mode) { - if (ts->tv_nsec >= g_sampling_interval) { - if (g_is_dropping == 0) { - g_is_dropping = 1; - record_event(PPME_DROP_E, NULL, -1, 1, NULL, NULL); - } +// Return 1 if the event should be dropped, else 0 +static inline int drop_event(struct ppm_consumer_t *consumer, + enum ppm_event_type event_type, + enum syscall_flags drop_flags, + nanoseconds ns, + struct pt_regs *regs) +{ + int maybe_ret = 0; + + if (consumer->dropping_mode) { + maybe_ret = drop_nostate_event(event_type, regs); + if (maybe_ret > 0) + return maybe_ret; + } + + if (drop_flags & UF_NEVER_DROP) { + ASSERT((drop_flags & UF_ALWAYS_DROP) == 0); + return 0; + } + if (consumer->dropping_mode) { + if (drop_flags & UF_ALWAYS_DROP) { + ASSERT((drop_flags & UF_NEVER_DROP) == 0); return 1; - } else { - if (g_is_dropping == 1) { - g_is_dropping = 0; - record_event(PPME_DROP_X, NULL, -1, 1, NULL, NULL); + } + + if (consumer->sampling_interval < SECOND_IN_NS && + (ns % SECOND_IN_NS) >= consumer->sampling_interval) { + if (consumer->is_dropping == 0) { + consumer->is_dropping = 1; + record_drop_e(consumer, ns, drop_flags); } + + return 1; + } + + if (consumer->is_dropping == 1) { + consumer->is_dropping = 0; + record_drop_x(consumer, ns, drop_flags); } } return 0; } -static void record_event(enum ppm_event_type event_type, - struct pt_regs *regs, - long id, - int never_drop, - struct task_struct *sched_prev, - struct task_struct *sched_next) +static void record_event_all_consumers(enum ppm_event_type event_type, + enum syscall_flags drop_flags, + struct event_data_t *event_datap) +{ + struct ppm_consumer_t *consumer; + nanoseconds ns = ppm_nsecs(); + + rcu_read_lock(); + list_for_each_entry_rcu(consumer, &g_consumer_list, node) { + record_event_consumer(consumer, event_type, drop_flags, ns, event_datap); + } + rcu_read_unlock(); +} + +/* + * Returns 0 if the event is dropped + */ +static int record_event_consumer(struct ppm_consumer_t *consumer, + enum ppm_event_type event_type, + enum syscall_flags drop_flags, + nanoseconds ns, + struct event_data_t *event_datap) { - size_t event_size; + int res = 0; + size_t event_size = 0; int next; - uint32_t freespace; - uint32_t usedspace; + u32 freespace; + u32 usedspace; + u32 delta_from_end; struct event_filler_arguments args; - uint32_t ttail; - uint32_t head; + u32 ttail; + u32 head; struct ppm_ring_buffer_context *ring; struct ppm_ring_buffer_info *ring_info; int drop = 1; int32_t cbres = PPM_SUCCESS; - struct timespec ts; - - trace_enter(); - - getnstimeofday(&ts); - - if (drop_event(event_type, never_drop, &ts)) - return; + int cpu; - ring = get_cpu_var(g_ring_buffers); - ring_info = ring->info; + if (!test_bit(event_type, g_events_mask)) + return res; + + if (event_type != PPME_DROP_E && event_type != PPME_DROP_X) { + if (consumer->need_to_insert_drop_e == 1) + record_drop_e(consumer, ns, drop_flags); + else if (consumer->need_to_insert_drop_x == 1) + record_drop_x(consumer, ns, drop_flags); + + if (drop_event(consumer, + event_type, + drop_flags, + ns, + event_datap->event_info.syscall_data.regs)) + return res; + } /* * FROM THIS MOMENT ON, WE HAVE TO BE SUPER FAST */ + cpu = get_cpu(); + ring = per_cpu_ptr(consumer->ring_buffers, cpu); + ASSERT(ring); + + ring_info = ring->info; + + if (!ring->capture_enabled) { + put_cpu(); + return res; + } + ring_info->n_evts++; - if (sched_prev != NULL) { - ASSERT(sched_prev != NULL); - ASSERT(sched_next != NULL); - ASSERT(regs == NULL); - ring_info->n_context_switches++; + if (event_datap->category == PPMC_CONTEXT_SWITCH && event_datap->event_info.context_data.sched_prev != NULL) { + if (event_type != PPME_SYSDIGEVENT_E && event_type != PPME_CPU_HOTPLUG_E) { + ASSERT(event_datap->event_info.context_data.sched_prev != NULL); + ASSERT(event_datap->event_info.context_data.sched_next != NULL); + ring_info->n_context_switches++; + } } /* * Preemption gate */ if (unlikely(atomic_inc_return(&ring->preempt_count) != 1)) { + /* When this driver executing a filler calls ppm_copy_from_user(), + * even if the page fault is disabled, the page fault tracepoint gets + * called very early in the page fault handler, way before the kernel + * terminates it, so this is legit. Still not sure how to solve this, + * so for the moment handle this case by not complaining and ignoring + * the false alarm if the preemption exception is generated by + * page_fault_kernel. The alternative would be to disable the kernel + * tracepoint completely, but there is value in seeing page faults + * generated on this side, so let's see if someone complains. + * This means that effectively those events would be lost. + */ + if (event_type != PPME_PAGE_FAULT_E) { + ring_info->n_preemptions++; + ASSERT(false); + } atomic_dec(&ring->preempt_count); - put_cpu_var(g_ring_buffers); - ring_info->n_preemptions++; - ASSERT(false); - return; - } - - if (unlikely(atomic_read(&ring->state) == CS_INACTIVE)) { - atomic_dec(&ring->preempt_count); - put_cpu_var(g_ring_buffers); - return; + put_cpu(); + return res; } /* @@ -665,13 +1661,15 @@ static void record_event(enum ppm_event_type event_type, freespace = RING_BUF_SIZE + ttail - head - 1; usedspace = RING_BUF_SIZE - freespace - 1; + delta_from_end = RING_BUF_SIZE + (2 * PAGE_SIZE) - head - 1; ASSERT(freespace <= RING_BUF_SIZE); ASSERT(usedspace <= RING_BUF_SIZE); ASSERT(ttail <= RING_BUF_SIZE); ASSERT(head <= RING_BUF_SIZE); - -#ifndef __x86_64__ + ASSERT(delta_from_end < RING_BUF_SIZE + (2 * PAGE_SIZE)); + ASSERT(delta_from_end > (2 * PAGE_SIZE) - 1); +#ifdef _HAS_SOCKETCALL /* * If this is a socketcall system call, determine the correct event type * by parsing the arguments and patch event_type accordingly @@ -682,15 +1680,24 @@ static void record_event(enum ppm_event_type event_type, * second argument contains a pointer to the arguments of the original * call. I guess this was done to reduce the number of syscalls... */ - if (regs && id == __NR_socketcall) { + if (event_datap->category == PPMC_SYSCALL && event_datap->event_info.syscall_data.regs && event_datap->event_info.syscall_data.id == event_datap->socketcall_syscall) { enum ppm_event_type tet; - tet = parse_socketcall(&args, regs); + + args.is_socketcall = true; + args.compat = event_datap->compat; + tet = parse_socketcall(&args, event_datap->event_info.syscall_data.regs); if (event_type == PPME_GENERIC_E) event_type = tet; else event_type = tet + 1; + + } else { + args.is_socketcall = false; + args.compat = false; } + + args.socketcall_syscall = event_datap->socketcall_syscall; #endif ASSERT(event_type < PPM_EVENT_MAX); @@ -714,41 +1721,85 @@ static void record_event(enum ppm_event_type event_type, #ifdef PPM_ENABLE_SENTINEL hdr->sentinel_begin = ring->nevents; #endif - hdr->ts = timespec_to_ns(&ts); + hdr->ts = ns; hdr->tid = current->pid; hdr->type = event_type; + hdr->nparams = args.nargs; /* * Populate the parameters for the filler callback */ + args.consumer = consumer; args.buffer = ring->buffer + head + sizeof(struct ppm_evt_hdr); #ifdef PPM_ENABLE_SENTINEL args.sentinel = ring->nevents; #endif - args.buffer_size = min(freespace, (uint32_t)(2 * PAGE_SIZE)) - sizeof(struct ppm_evt_hdr); /* freespace is guaranteed to be bigger than sizeof(struct ppm_evt_hdr) */ + args.buffer_size = min(freespace, delta_from_end) - sizeof(struct ppm_evt_hdr); /* freespace is guaranteed to be bigger than sizeof(struct ppm_evt_hdr) */ args.event_type = event_type; - args.regs = regs; - args.sched_prev = sched_prev; - args.sched_next = sched_next; - args.syscall_id = id; + + if (event_datap->category == PPMC_SYSCALL) { + args.regs = event_datap->event_info.syscall_data.regs; + args.syscall_id = event_datap->event_info.syscall_data.id; + args.cur_g_syscall_code_routing_table = event_datap->event_info.syscall_data.cur_g_syscall_code_routing_table; + args.compat = event_datap->compat; + } else { + args.regs = NULL; + args.syscall_id = -1; + args.cur_g_syscall_code_routing_table = NULL; + args.compat = false; + } + + if (event_datap->category == PPMC_CONTEXT_SWITCH) { + args.sched_prev = event_datap->event_info.context_data.sched_prev; + args.sched_next = event_datap->event_info.context_data.sched_next; + } else { + args.sched_prev = NULL; + args.sched_next = NULL; + } + + if (event_datap->category == PPMC_SIGNAL) { + args.signo = event_datap->event_info.signal_data.sig; + if (event_datap->event_info.signal_data.info == NULL) { + args.spid = (__kernel_pid_t) 0; + } else if (args.signo == SIGKILL) { + args.spid = event_datap->event_info.signal_data.info->_sifields._kill._pid; + } else if (args.signo == SIGTERM || args.signo == SIGHUP || args.signo == SIGINT || + args.signo == SIGTSTP || args.signo == SIGQUIT) { + if (event_datap->event_info.signal_data.info->si_code == SI_USER || + event_datap->event_info.signal_data.info->si_code == SI_QUEUE || + event_datap->event_info.signal_data.info->si_code <= 0) { + args.spid = event_datap->event_info.signal_data.info->si_pid; + } + } else if (args.signo == SIGCHLD) { + args.spid = event_datap->event_info.signal_data.info->_sifields._sigchld._pid; + } else if (args.signo >= SIGRTMIN && args.signo <= SIGRTMAX) { + args.spid = event_datap->event_info.signal_data.info->_sifields._rt._pid; + } else { + args.spid = (__kernel_pid_t) 0; + } + } else { + args.signo = 0; + args.spid = (__kernel_pid_t) 0; + } + args.dpid = current->pid; + + if (event_datap->category == PPMC_PAGE_FAULT) + args.fault_data = event_datap->event_info.fault_data; + args.curarg = 0; args.arg_data_size = args.buffer_size - args.arg_data_offset; args.nevents = ring->nevents; args.str_storage = ring->str_storage; + args.enforce_snaplen = false; /* * Fire the filler callback */ - if (g_ppm_events[event_type].filler_callback == PPM_AUTOFILL) { - /* - * This event is automatically filled. Hand it to f_sys_autofill. - */ - cbres = f_sys_autofill(&args, &g_ppm_events[event_type]); - } else { - /* - * There's a callback function for this event - */ + if (likely(g_ppm_events[event_type].filler_callback)) { cbres = g_ppm_events[event_type].filler_callback(&args); + } else { + pr_err("corrupted filler for event type %d: NULL callback\n", event_type); + ASSERT(0); } if (likely(cbres == PPM_SUCCESS)) { @@ -756,11 +1807,14 @@ static void record_event(enum ppm_event_type event_type, * Validate that the filler added the right number of parameters */ if (likely(args.curarg == args.nargs)) { + /* + * The event was successfully inserted in the buffer + */ event_size = sizeof(struct ppm_evt_hdr) + args.arg_data_offset; hdr->len = event_size; drop = 0; } else { - pr_info("sysdig-probe: corrupted filler for event type %d (added %u args, should have added %u)\n", + pr_err("corrupted filler for event type %d (added %u args, should have added %u)\n", event_type, args.curarg, args.nargs); @@ -770,6 +1824,8 @@ static void record_event(enum ppm_event_type event_type, } if (likely(!drop)) { + res = 1; + next = head + event_size; if (unlikely(next >= RING_BUF_SIZE)) { @@ -804,7 +1860,7 @@ static void record_event(enum ppm_event_type event_type, ring_info->n_drops_buffer++; } else if (cbres == PPM_FAILURE_INVALID_USER_MEMORY) { #ifdef _DEBUG - pr_info("sysdig-probe: Invalid read from user for event %d\n", event_type); + pr_err("Invalid read from user for event %d\n", event_type); #endif ring_info->n_drops_pf++; } else if (cbres == PPM_FAILURE_BUFFER_FULL) { @@ -814,9 +1870,9 @@ static void record_event(enum ppm_event_type event_type, } } -#ifdef _DEBUG - if (ts.tv_sec > ring->last_print_time.tv_sec + 1) { - pr_info("sysdig-probe: CPU%d, use:%d%%, ev:%llu, dr_buf:%llu, dr_pf:%llu, pr:%llu, cs:%llu\n", + if (MORE_THAN_ONE_SECOND_AHEAD(ns, ring->last_print_time + 1) && !(drop_flags & UF_ATOMIC)) { + vpr_info("consumer:%p CPU:%d, use:%d%%, ev:%llu, dr_buf:%llu, dr_pf:%llu, pr:%llu, cs:%llu\n", + consumer->consumer_id, smp_processor_id(), (usedspace * 100) / RING_BUF_SIZE, ring_info->n_evts, @@ -825,81 +1881,207 @@ static void record_event(enum ppm_event_type event_type, ring_info->n_preemptions, ring->info->n_context_switches); - ring->last_print_time = ts; + ring->last_print_time = ns; } -#endif atomic_dec(&ring->preempt_count); - put_cpu_var(g_ring_buffers); + put_cpu(); - return; + return res; +} + +static inline void g_n_tracepoint_hit_inc(void) +{ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) + this_cpu_inc(g_n_tracepoint_hit); +#elif defined(this_cpu_inc) + /* this_cpu_inc has been added with 2.6.33 but backported by RHEL/CentOS to 2.6.32 + * so just checking the existence of the symbol rather than matching the kernel version + * https://github.com/torvalds/linux/commit/7340a0b15280c9d902c7dd0608b8e751b5a7c403 + * + * per_cpu_var removed with: + * https://github.com/torvalds/linux/commit/dd17c8f72993f9461e9c19250e3f155d6d99df22 + */ + this_cpu_inc(per_cpu_var(g_n_tracepoint_hit)); +#endif } TRACEPOINT_PROBE(syscall_enter_probe, struct pt_regs *regs, long id) { - trace_enter(); + long table_index; + const struct syscall_evt_pair *cur_g_syscall_table = g_syscall_table; + const enum ppm_syscall_code *cur_g_syscall_code_routing_table = g_syscall_code_routing_table; + bool compat = false; +#ifdef __NR_socketcall + int socketcall_syscall = __NR_socketcall; +#else + int socketcall_syscall = -1; +#endif +#if defined(CONFIG_X86_64) && defined(CONFIG_IA32_EMULATION) /* * If this is a 32bit process running on a 64bit kernel (see the CONFIG_IA32_EMULATION - * kernel flag), we skip its events. - * XXX Decide what to do about this. + * kernel flag), we switch to the ia32 syscall table. */ - if (unlikely(test_tsk_thread_flag(current, TIF_IA32))) - return; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0) + if (in_ia32_syscall()) { +#else + if (unlikely(task_thread_info(current)->status & TS_COMPAT)) { +#endif + cur_g_syscall_table = g_syscall_ia32_table; + cur_g_syscall_code_routing_table = g_syscall_ia32_code_routing_table; + socketcall_syscall = __NR_ia32_socketcall; + compat = true; + } +#endif + + g_n_tracepoint_hit_inc(); + + table_index = id - SYSCALL_TABLE_ID0; + if (likely(table_index >= 0 && table_index < SYSCALL_TABLE_SIZE)) { + struct event_data_t event_data; + int used = cur_g_syscall_table[table_index].flags & UF_USED; + enum syscall_flags drop_flags = cur_g_syscall_table[table_index].flags; + enum ppm_event_type type; + + /* + * Simple mode event filtering + */ + if (g_simple_mode_enabled) { + if ((drop_flags & UF_SIMPLEDRIVER_KEEP) == 0) { + return; + } + } + +#ifdef _HAS_SOCKETCALL + if (id == socketcall_syscall) { + used = true; + drop_flags = UF_NEVER_DROP; + type = PPME_GENERIC_E; + } else + type = cur_g_syscall_table[table_index].enter_event_type; +#else + type = cur_g_syscall_table[table_index].enter_event_type; +#endif - if (likely(id >= 0 && id < SYSCALL_TABLE_SIZE)) { - int used = g_syscall_table[id].flags & UF_USED; - int never_drop = g_syscall_table[id].flags & UF_NEVER_DROP; + event_data.category = PPMC_SYSCALL; + event_data.event_info.syscall_data.regs = regs; + event_data.event_info.syscall_data.id = id; + event_data.event_info.syscall_data.cur_g_syscall_code_routing_table = cur_g_syscall_code_routing_table; + event_data.socketcall_syscall = socketcall_syscall; + event_data.compat = compat; if (used) - record_event(g_syscall_table[id].enter_event_type, regs, id, never_drop, NULL, NULL); + record_event_all_consumers(type, drop_flags, &event_data); else - record_event(PPME_GENERIC_E, regs, id, false, NULL, NULL); + record_event_all_consumers(PPME_GENERIC_E, UF_ALWAYS_DROP, &event_data); } } TRACEPOINT_PROBE(syscall_exit_probe, struct pt_regs *regs, long ret) { int id; + long table_index; + const struct syscall_evt_pair *cur_g_syscall_table = g_syscall_table; + const enum ppm_syscall_code *cur_g_syscall_code_routing_table = g_syscall_code_routing_table; + bool compat = false; +#ifdef __NR_socketcall + int socketcall_syscall = __NR_socketcall; +#else + int socketcall_syscall = -1; +#endif - trace_enter(); + id = syscall_get_nr(current, regs); +#if defined(CONFIG_X86_64) && defined(CONFIG_IA32_EMULATION) /* - * If this is a 32bit process running on a 64bit kernel (see the CONFIG_IA32_EMULATION - * kernel flag), we skip its events. - * XXX Decide what to do about this. + * When a process does execve from 64bit to 32bit, TS_COMPAT is marked true + * but the id of the syscall is __NR_execve, so to correctly parse it we need to + * use 64bit syscall table. On 32bit __NR_execve is equal to __NR_ia32_oldolduname + * which is a very old syscall, not used anymore by most applications */ - if (unlikely(test_tsk_thread_flag(current, TIF_IA32))) - return; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0) + if (in_ia32_syscall() && id != __NR_execve) { +#else + if (unlikely((task_thread_info(current)->status & TS_COMPAT) && id != __NR_execve)) { +#endif + cur_g_syscall_table = g_syscall_ia32_table; + cur_g_syscall_code_routing_table = g_syscall_ia32_code_routing_table; + socketcall_syscall = __NR_ia32_socketcall; + compat = true; + } +#endif - id = syscall_get_nr(current, regs); + g_n_tracepoint_hit_inc(); + + table_index = id - SYSCALL_TABLE_ID0; + if (likely(table_index >= 0 && table_index < SYSCALL_TABLE_SIZE)) { + struct event_data_t event_data; + int used = cur_g_syscall_table[table_index].flags & UF_USED; + enum syscall_flags drop_flags = cur_g_syscall_table[table_index].flags; + enum ppm_event_type type; + + /* + * Simple mode event filtering + */ + if (g_simple_mode_enabled) { + if ((drop_flags & UF_SIMPLEDRIVER_KEEP) == 0) { + return; + } + } + +#ifdef _HAS_SOCKETCALL + if (id == socketcall_syscall) { + used = true; + drop_flags = UF_NEVER_DROP; + type = PPME_GENERIC_X; + } else + type = cur_g_syscall_table[table_index].exit_event_type; +#else + type = cur_g_syscall_table[table_index].exit_event_type; +#endif - if (likely(id >= 0 && id < SYSCALL_TABLE_SIZE)) { - int used = g_syscall_table[id].flags & UF_USED; - int never_drop = g_syscall_table[id].flags & UF_NEVER_DROP; + event_data.category = PPMC_SYSCALL; + event_data.event_info.syscall_data.regs = regs; + event_data.event_info.syscall_data.id = id; + event_data.event_info.syscall_data.cur_g_syscall_code_routing_table = cur_g_syscall_code_routing_table; + event_data.socketcall_syscall = socketcall_syscall; + event_data.compat = compat; if (used) - record_event(g_syscall_table[id].exit_event_type, regs, id, never_drop, NULL, NULL); + record_event_all_consumers(type, drop_flags, &event_data); else - record_event(PPME_GENERIC_X, regs, id, false, NULL, NULL); + record_event_all_consumers(PPME_GENERIC_X, UF_ALWAYS_DROP, &event_data); } } +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 1) int __access_remote_vm(struct task_struct *t, struct mm_struct *mm, unsigned long addr, void *buf, int len, int write); +#endif TRACEPOINT_PROBE(syscall_procexit_probe, struct task_struct *p) { - trace_enter(); + struct event_data_t event_data; + + g_n_tracepoint_hit_inc(); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) if (unlikely(current->flags & PF_KTHREAD)) { +#else + if (unlikely(current->flags & PF_BORROWED_MM)) { +#endif /* * We are not interested in kernel threads */ return; } - record_event(PPME_PROCEXIT_E, NULL, -1, 1, NULL, NULL); + event_data.category = PPMC_CONTEXT_SWITCH; + event_data.event_info.context_data.sched_prev = p; + event_data.event_info.context_data.sched_next = p; + + record_event_all_consumers(PPME_PROCEXIT_1_E, UF_NEVER_DROP, &event_data); } #include @@ -909,41 +2091,94 @@ TRACEPOINT_PROBE(syscall_procexit_probe, struct task_struct *p) #ifdef CAPTURE_CONTEXT_SWITCHES #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)) TRACEPOINT_PROBE(sched_switch_probe, struct rq *rq, struct task_struct *prev, struct task_struct *next) -#else +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) TRACEPOINT_PROBE(sched_switch_probe, struct task_struct *prev, struct task_struct *next) +#else +TRACEPOINT_PROBE(sched_switch_probe, bool preempt, struct task_struct *prev, struct task_struct *next) #endif { - record_event(PPME_SCHEDSWITCH_E, - NULL, - -1, - 0, - prev, - next); + struct event_data_t event_data; + + g_n_tracepoint_hit_inc(); + + event_data.category = PPMC_CONTEXT_SWITCH; + event_data.event_info.context_data.sched_prev = prev; + event_data.event_info.context_data.sched_next = next; + + /* + * Need to indicate ATOMIC (i.e. interrupt) context to avoid the event + * handler calling printk() and potentially deadlocking the system. + */ + record_event_all_consumers(PPME_SCHEDSWITCH_6_E, UF_USED | UF_ATOMIC, &event_data); } #endif -static struct ppm_ring_buffer_context *alloc_ring_buffer(struct ppm_ring_buffer_context **ring) +#ifdef CAPTURE_SIGNAL_DELIVERIES + +static __always_inline int siginfo_not_a_pointer(struct siginfo* info) { - unsigned int j; - trace_enter(); +#ifdef SEND_SIG_FORCED + return info == SEND_SIG_NOINFO || info == SEND_SIG_PRIV || SEND_SIG_FORCED; +#else + return info == (struct siginfo*)SEND_SIG_NOINFO || info == (struct siginfo*)SEND_SIG_PRIV; +#endif +} - /* - * Allocate the ring descriptor +TRACEPOINT_PROBE(signal_deliver_probe, int sig, struct siginfo *info, struct k_sigaction *ka) +{ + struct event_data_t event_data; + + g_n_tracepoint_hit_inc(); + + event_data.category = PPMC_SIGNAL; + event_data.event_info.signal_data.sig = sig; + if (siginfo_not_a_pointer(info)) + event_data.event_info.signal_data.info = NULL; + else + event_data.event_info.signal_data.info = info; + event_data.event_info.signal_data.ka = ka; + + record_event_all_consumers(PPME_SIGNALDELIVER_E, UF_USED | UF_ALWAYS_DROP, &event_data); +} +#endif + +#ifdef CAPTURE_PAGE_FAULTS +TRACEPOINT_PROBE(page_fault_probe, unsigned long address, struct pt_regs *regs, unsigned long error_code) +{ + struct event_data_t event_data; + + /* We register both tracepoints under the same probe and + * sysdig event since there's little reason to expose this + * complexity to the sysdig user. The distinction can still be made + * in the output by looking for the USER_FAULT/SUPERVISOR_FAULT + * flags */ - *ring = vmalloc(sizeof(struct ppm_ring_buffer_context)); - if (*ring == NULL) { - pr_err("sysdig-probe: Error allocating ring memory\n"); - return NULL; - } + g_n_tracepoint_hit_inc(); + + /* I still haven't decided if I'm interested in kernel threads or not. + * For the moment, I assume yes since I can see some value for it. + */ + + event_data.category = PPMC_PAGE_FAULT; + event_data.event_info.fault_data.address = address; + event_data.event_info.fault_data.regs = regs; + event_data.event_info.fault_data.error_code = error_code; + + record_event_all_consumers(PPME_PAGE_FAULT_E, UF_ALWAYS_DROP, &event_data); +} +#endif + +static int init_ring_buffer(struct ppm_ring_buffer_context *ring) +{ + unsigned int j; /* * Allocate the string storage in the ring descriptor */ - (*ring)->str_storage = (char *)__get_free_page(GFP_USER); - if (!(*ring)->str_storage) { - pr_err("sysdig-probe: Error allocating the string storage\n"); - vfree(*ring); - return NULL; + ring->str_storage = (char *)__get_free_page(GFP_USER); + if (!ring->str_storage) { + pr_err("Error allocating the string storage\n"); + goto init_ring_err; } /* @@ -951,144 +2186,315 @@ static struct ppm_ring_buffer_context *alloc_ring_buffer(struct ppm_ring_buffer_ * Note how we allocate 2 additional pages: they are used as additional overflow space for * the event data generation functions, so that they always operate on a contiguous buffer. */ - (*ring)->buffer = vmalloc(RING_BUF_SIZE + 2 * PAGE_SIZE); - if ((*ring)->buffer == NULL) { - pr_err("sysdig-probe: Error allocating ring memory\n"); - free_page((unsigned long)(*ring)->str_storage); - vfree(*ring); - return NULL; + ring->buffer = vmalloc(RING_BUF_SIZE + 2 * PAGE_SIZE); + if (ring->buffer == NULL) { + pr_err("Error allocating ring memory\n"); + goto init_ring_err; } for (j = 0; j < RING_BUF_SIZE + 2 * PAGE_SIZE; j++) - (*ring)->buffer[j] = 0; + ring->buffer[j] = 0; /* * Allocate the buffer info structure */ - (*ring)->info = vmalloc(sizeof(struct ppm_ring_buffer_info)); - if ((*ring)->info == NULL) { - pr_err("sysdig-probe: Error allocating ring memory\n"); - vfree((void *)(*ring)->buffer); - free_page((unsigned long)(*ring)->str_storage); - vfree(*ring); - return NULL; + ring->info = vmalloc(sizeof(struct ppm_ring_buffer_info)); + if (ring->info == NULL) { + pr_err("Error allocating ring memory\n"); + goto init_ring_err; } -/* for(j = 0; j < (RING_BUF_SIZE / PAGE_SIZE + 1); j += PAGE_SIZE) */ -/* { */ -/* SetPageReserved(vmalloc_to_page(ring) + j); */ -/* } */ - /* * Initialize the buffer info structure */ - atomic_set(&(*ring)->state, CS_STOPPED); - (*ring)->info->head = 0; - (*ring)->info->tail = 0; - (*ring)->nevents = 0; - (*ring)->info->n_evts = 0; - (*ring)->info->n_drops_buffer = 0; - (*ring)->info->n_drops_pf = 0; - (*ring)->info->n_preemptions = 0; - (*ring)->info->n_context_switches = 0; - atomic_set(&(*ring)->preempt_count, 0); - getnstimeofday(&(*ring)->last_print_time); - - pr_info("sysdig-probe: CPU buffer initialized, size=%d\n", RING_BUF_SIZE); - - return *ring; + reset_ring_buffer(ring); + atomic_set(&ring->preempt_count, 0); + + pr_info("CPU buffer initialized, size=%d\n", RING_BUF_SIZE); + + return 1; + +init_ring_err: + free_ring_buffer(ring); + return 0; } static void free_ring_buffer(struct ppm_ring_buffer_context *ring) { - trace_enter(); + if (ring->info) { + vfree(ring->info); + ring->info = NULL; + } + + if (ring->buffer) { + vfree((void *)ring->buffer); + ring->buffer = NULL; + } + + if (ring->str_storage) { + free_page((unsigned long)ring->str_storage); + ring->str_storage = NULL; + } +} + +static void reset_ring_buffer(struct ppm_ring_buffer_context *ring) +{ + /* + * ring->preempt_count is not reset to 0 on purpose, to prevent a race condition + * see ppm_open + */ + ring->open = false; + ring->capture_enabled = false; + ring->info->head = 0; + ring->info->tail = 0; + ring->nevents = 0; + ring->info->n_evts = 0; + ring->info->n_drops_buffer = 0; + ring->info->n_drops_pf = 0; + ring->info->n_preemptions = 0; + ring->info->n_context_switches = 0; + ring->last_print_time = ppm_nsecs(); +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)) +static void visit_tracepoint(struct tracepoint *tp, void *priv) +{ + if (!strcmp(tp->name, "sys_enter")) + tp_sys_enter = tp; + else if (!strcmp(tp->name, "sys_exit")) + tp_sys_exit = tp; + else if (!strcmp(tp->name, "sched_process_exit")) + tp_sched_process_exit = tp; +#ifdef CAPTURE_CONTEXT_SWITCHES + else if (!strcmp(tp->name, "sched_switch")) + tp_sched_switch = tp; +#endif +#ifdef CAPTURE_SIGNAL_DELIVERIES + else if (!strcmp(tp->name, "signal_deliver")) + tp_signal_deliver = tp; +#endif +#ifdef CAPTURE_PAGE_FAULTS + else if (!strcmp(tp->name, "page_fault_user")) + tp_page_fault_user = tp; + else if (!strcmp(tp->name, "page_fault_kernel")) + tp_page_fault_kernel = tp; +#endif +} + +static int get_tracepoint_handles(void) +{ + for_each_kernel_tracepoint(visit_tracepoint, NULL); + + if (!tp_sys_enter) { + pr_err("failed to find sys_enter tracepoint\n"); + return -ENOENT; + } + if (!tp_sys_exit) { + pr_err("failed to find sys_exit tracepoint\n"); + return -ENOENT; + } + if (!tp_sched_process_exit) { + pr_err("failed to find sched_process_exit tracepoint\n"); + return -ENOENT; + } +#ifdef CAPTURE_CONTEXT_SWITCHES + if (!tp_sched_switch) { + pr_err("failed to find sched_switch tracepoint\n"); + return -ENOENT; + } +#endif +#ifdef CAPTURE_SIGNAL_DELIVERIES + if (!tp_signal_deliver) { + pr_err("failed to find signal_deliver tracepoint\n"); + return -ENOENT; + } +#endif +#ifdef CAPTURE_PAGE_FAULTS + if (!tp_page_fault_user) { + pr_notice("failed to find page_fault_user tracepoint, disabling page-faults\n"); + g_fault_tracepoint_disabled = true; + } + if (!tp_page_fault_kernel) { + pr_notice("failed to find page_fault_kernel tracepoint, disabling page-faults\n"); + g_fault_tracepoint_disabled = true; + } +#endif + + return 0; +} +#else +static int get_tracepoint_handles(void) +{ + return 0; +} +#endif + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0) +static char *ppm_devnode(struct device *dev, umode_t *mode) +#else +static char *ppm_devnode(struct device *dev, mode_t *mode) +#endif +{ + if (mode) { + *mode = 0400; + + if (dev) + if (MINOR(dev->devt) == g_ppm_numdevs) + *mode = 0222; + } + + return NULL; +} +#endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) */ + +static int do_cpu_callback(unsigned long cpu, long sd_action) +{ + struct ppm_ring_buffer_context *ring; + struct ppm_consumer_t *consumer; + struct event_data_t event_data; + + if (sd_action != 0) { + rcu_read_lock(); + + list_for_each_entry_rcu(consumer, &g_consumer_list, node) { + ring = per_cpu_ptr(consumer->ring_buffers, cpu); + if (sd_action == 1) { + /* + * If the cpu was offline when the consumer was created, + * this won't do anything because we never created a ring + * buffer. We can't safely create one here because we're + * in atomic context, and the consumer needs to call open + * on this device anyways, so do it in ppm_open. + */ + ring->cpu_online = true; + } else if (sd_action == 2) { + ring->cpu_online = false; + } + } + + rcu_read_unlock(); + + event_data.category = PPMC_CONTEXT_SWITCH; + event_data.event_info.context_data.sched_prev = (void *)cpu; + event_data.event_info.context_data.sched_next = (void *)sd_action; + record_event_all_consumers(PPME_CPU_HOTPLUG_E, UF_NEVER_DROP, &event_data); + } + return 0; +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)) +static int sysdig_cpu_online(unsigned int cpu) +{ + vpr_info("sysdig_cpu_online on cpu %d\n", cpu); + return do_cpu_callback(cpu, 1); +} + +static int sysdig_cpu_offline(unsigned int cpu) +{ + vpr_info("sysdig_cpu_offline on cpu %d\n", cpu); + return do_cpu_callback(cpu, 2); +} +#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)) */ +/* + * This gets called every time a CPU is added or removed + */ +static int cpu_callback(struct notifier_block *self, unsigned long action, + void *hcpu) +{ + unsigned long cpu = (unsigned long)hcpu; + long sd_action = 0; + + switch (action) { + case CPU_UP_PREPARE: +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + case CPU_UP_PREPARE_FROZEN: +#endif + sd_action = 1; + break; + case CPU_DOWN_PREPARE: +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + case CPU_DOWN_PREPARE_FROZEN: +#endif + sd_action = 2; + break; + default: + break; + } - vfree(ring->info); - vfree((void *)ring->buffer); - free_page((unsigned long)ring->str_storage); - vfree(ring); + if (do_cpu_callback(cpu, sd_action) < 0) + return NOTIFY_BAD; + else + return NOTIFY_OK; } -/* static int ppm_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data) */ -/* { */ -/* int len = 0; */ -/* int j; */ - -/* trace_enter(); */ - -/* for(j = 0; j < NR_syscalls; ++j) */ -/* { */ -/* #if defined(__x86_64__) */ -/* len += snprintf(page + len, count - len, "%ld\t%ld\n", */ -/* atomic64_read(&g_syscall_count[j].count), */ -/* atomic64_read(&g_syscall_count[j].count) ? (atomic64_read(&g_syscall_count[j].tot_time_ns) / atomic64_read(&g_syscall_count[j].count)) : 0); */ -/* #else */ -/* len += snprintf(page + len, count - len, "%lld\n", */ -/* atomic64_read(&g_syscall_count[j].count)); */ -/* #endif */ -/* } */ - -/* *eof = 1; */ -/* return len; */ -/* } */ - -int init_module(void) +static struct notifier_block cpu_notifier = { + .notifier_call = &cpu_callback, + .next = NULL, +}; +#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) */ + +int sysdig_init(void) { dev_t dev; unsigned int cpu; unsigned int num_cpus; int ret; int acrret = 0; +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)) + int hp_ret; +#endif int j; int n_created_devices = 0; +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) struct device *device = NULL; +#else + struct class_device *device = NULL; +#endif + pr_info("driver loading, " PROBE_NAME " " PROBE_VERSION "\n"); - pr_info("sysdig-probe: driver loading\n"); + ret = get_tracepoint_handles(); + if (ret < 0) + goto init_module_err; - /* - * Initialize the ring buffers array - */ num_cpus = 0; - for_each_online_cpu(cpu) { - per_cpu(g_ring_buffers, cpu) = NULL; + for_each_possible_cpu(cpu) { ++num_cpus; } - for_each_online_cpu(cpu) { - pr_info("sysdig-probe: initializing ring buffer for CPU %u\n", cpu); - - alloc_ring_buffer(&per_cpu(g_ring_buffers, cpu)); - if (per_cpu(g_ring_buffers, cpu) == NULL) { - pr_err("sysdig-probe: can't initialize the ring buffer for CPU %u\n", cpu); - ret = -ENOMEM; - goto init_module_err; - } - } - /* * Initialize the user I/O + * ( + 1 for sysdig-events) */ - acrret = alloc_chrdev_region(&dev, 0, num_cpus, PPM_DEVICE_NAME); + acrret = alloc_chrdev_region(&dev, 0, num_cpus + 1, PROBE_DEVICE_NAME); if (acrret < 0) { - pr_err("sysdig-probe: could not allocate major number for %s\n", PPM_DEVICE_NAME); + pr_err("could not allocate major number for %s\n", PROBE_DEVICE_NAME); ret = -ENOMEM; goto init_module_err; } - g_ppm_class = class_create(THIS_MODULE, PPM_DEVICE_NAME); + g_ppm_class = class_create(THIS_MODULE, PROBE_DEVICE_NAME); if (IS_ERR(g_ppm_class)) { - pr_err("sysdig-probe: can't allocate device class\n"); + pr_err("can't allocate device class\n"); ret = -EFAULT; goto init_module_err; } +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + g_ppm_class->devnode = ppm_devnode; +#endif + g_ppm_major = MAJOR(dev); g_ppm_numdevs = num_cpus; +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0) g_ppm_devs = kmalloc(g_ppm_numdevs * sizeof(struct ppm_device), GFP_KERNEL); +#else + g_ppm_devs = kmalloc_array(g_ppm_numdevs, sizeof(struct ppm_device), GFP_KERNEL); +#endif if (!g_ppm_devs) { + pr_err("can't allocate devices\n"); ret = -ENOMEM; goto init_module_err; - pr_err("sysdig-probe: can't allocate devices\n"); } /* @@ -1099,19 +2505,24 @@ int init_module(void) g_ppm_devs[j].dev = MKDEV(g_ppm_major, j); if (cdev_add(&g_ppm_devs[j].cdev, g_ppm_devs[j].dev, 1) < 0) { - pr_err("sysdig-probe: could not allocate chrdev for %s\n", PPM_DEVICE_NAME); + pr_err("could not allocate chrdev for %s\n", PROBE_DEVICE_NAME); ret = -EFAULT; goto init_module_err; } - device = device_create(g_ppm_class, NULL, /* no parent device */ - g_ppm_devs[j].dev, - NULL, /* no additional data */ - PPM_DEVICE_NAME "%d", - j); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + device = device_create( +#else + device = class_device_create( +#endif + g_ppm_class, NULL, /* no parent device */ + g_ppm_devs[j].dev, + NULL, /* no additional data */ + PROBE_DEVICE_NAME "%d", + j); if (IS_ERR(device)) { - pr_err("sysdig-probe: error creating the device for %s\n", PPM_DEVICE_NAME); + pr_err("error creating the device for %s\n", PROBE_DEVICE_NAME); cdev_del(&g_ppm_devs[j].cdev); ret = -EFAULT; goto init_module_err; @@ -1124,22 +2535,49 @@ int init_module(void) /* create_proc_read_entry(PPM_DEVICE_NAME, 0, NULL, ppm_read_proc, NULL); */ /* - * All ok. Final initalizations. + * Snaplen lookahead initialization */ - atomic_set(&g_open_count, 0); - g_dropping_mode = 0; + if (dpi_lookahead_init() != PPM_SUCCESS) { + pr_err("initializing lookahead-based snaplen failed\n"); + ret = -EFAULT; + goto init_module_err; + } - return 0; + /* + * Set up our callback in case we get a hotplug even while we are + * initializing the cpu structures + */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)) + hp_ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, + "sysdig/probe:online", + sysdig_cpu_online, + sysdig_cpu_offline); + if (hp_ret <= 0) { + pr_err("error registering cpu hotplug callback\n"); + ret = hp_ret; + goto init_module_err; + } + hp_state = hp_ret; +#else + register_cpu_notifier(&cpu_notifier); +#endif -init_module_err: - for_each_online_cpu(cpu) - if (per_cpu(g_ring_buffers, cpu) != NULL) - free_ring_buffer(per_cpu(g_ring_buffers, cpu)); + /* + * All ok. Final initializations. + */ + g_tracepoint_registered = false; - /* remove_proc_entry(PPM_DEVICE_NAME, NULL); */ + return 0; +init_module_err: for (j = 0; j < n_created_devices; ++j) { - device_destroy(g_ppm_class, g_ppm_devs[j].dev); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + device_destroy( +#else + class_device_destroy( +#endif + g_ppm_class, g_ppm_devs[j].dev); + cdev_del(&g_ppm_devs[j].cdev); } @@ -1149,33 +2587,52 @@ int init_module(void) if (acrret == 0) unregister_chrdev_region(dev, g_ppm_numdevs); - if (g_ppm_devs) - kfree(g_ppm_devs); + kfree(g_ppm_devs); return ret; } -void cleanup_module(void) +void sysdig_exit(void) { int j; - int cpu; - - pr_info("sysdig-probe: driver unloading\n"); - /* remove_proc_entry(PPM_DEVICE_NAME, NULL); */ - - for_each_online_cpu(cpu) - free_ring_buffer(per_cpu(g_ring_buffers, cpu)); + pr_info("driver unloading\n"); for (j = 0; j < g_ppm_numdevs; ++j) { - device_destroy(g_ppm_class, g_ppm_devs[j].dev); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + device_destroy( +#else + class_device_destroy( +#endif + g_ppm_class, g_ppm_devs[j].dev); cdev_del(&g_ppm_devs[j].cdev); } if (g_ppm_class) class_destroy(g_ppm_class); - unregister_chrdev_region(MKDEV(g_ppm_major, 0), g_ppm_numdevs); + /* + 1 for sysdig-events */ + unregister_chrdev_region(MKDEV(g_ppm_major, 0), g_ppm_numdevs + 1); kfree(g_ppm_devs); + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + tracepoint_synchronize_unregister(); +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)) + if (hp_state > 0) + cpuhp_remove_state_nocalls(hp_state); +#else + unregister_cpu_notifier(&cpu_notifier); +#endif } + +module_init(sysdig_init); +module_exit(sysdig_exit); +module_param(max_consumers, uint, 0444); +MODULE_PARM_DESC(max_consumers, "Maximum number of consumers that can simultaneously open the devices"); +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) +module_param(verbose, bool, 0444); +#endif +MODULE_PARM_DESC(verbose, "Enable verbose logging"); diff --git a/driver/ppm.h b/driver/ppm.h index c07f414393..309d43c770 100644 --- a/driver/ppm.h +++ b/driver/ppm.h @@ -1,104 +1,138 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ -/* - * Driver output definitions - */ +#ifndef PPM_H_ +#define PPM_H_ -/* - * Driver Chattiness - */ -#define OUTPUT_VERBOSE 4 -#define OUTPUT_INFO 2 -#define OUTPUT_ERRORS 1 -#define OUTPUT_NONE 0 - -#define OUTPUT_LEVEL OUTPUT_INFO +#ifndef UDIG +#include +#endif /* * Our Own ASSERT implementation, so we can easily switch among BUG_ON, WARN_ON and nothing */ +#ifndef UDIG #ifdef _DEBUG #define ASSERT(expr) WARN_ON(!(expr)) #else #define ASSERT(expr) #endif -/* - * Tracing and debug printing - */ -#if (OUTPUT_LEVEL >= OUTPUT_VERBOSE) -#define dbgprint(a) pr_info(a "\n") -#define trace_enter() pr_info("> %s\n", __func__) -#define trace_exit() pr_info("< %s\n", __func__) -#else -#define dbgprint(a) -#define trace_exit() -#define trace_enter() -#endif +#include /* * Global defines */ #define CAPTURE_CONTEXT_SWITCHES -#define RW_SNAPLEN 80 -#define RW_MAX_SNAPLEN (256 * 1024 * 1024) -/* Make sure to use a power of two constant for this */ -extern uint32_t g_snaplen; +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)) +#define CAPTURE_SIGNAL_DELIVERIES +#endif +#if (LINUX_VERSION_CODE > KERNEL_VERSION(3, 12, 0)) && defined(CONFIG_X86) +#define CAPTURE_PAGE_FAULTS +#endif +#endif // UDIG +#define RW_SNAPLEN_EVENT 4096 +#define DPI_LOOKAHEAD_SIZE 16 +#define PPM_NULL_RDEV MKDEV(1, 3) +#define PPM_PORT_MYSQL 3306 +#define PPM_PORT_POSTGRES 5432 +#define PPM_PORT_STATSD 8125 +#define PPM_PORT_MONGODB 27017 + +typedef u64 nanoseconds; /* - * Global enums + * The ring descriptor. + * We have one of these for each CPU. */ -enum ppm_capture_state { - CS_STOPPED = 0, /* Not capturing. Either uninitialized or closed. */ - CS_STARTED = 1, /* Capturing. */ - CS_INACTIVE = 2, /* Not Capturing but active, returning the packets in the buffer to the user. */ +struct ppm_ring_buffer_context { + bool cpu_online; + bool open; + bool capture_enabled; + struct ppm_ring_buffer_info *info; + char *buffer; + nanoseconds last_print_time; + u32 nevents; +#ifndef UDIG + atomic_t preempt_count; +#endif + char *str_storage; /* String storage. Size is one page. */ }; -enum syscall_flags { - UF_NONE = 0, - UF_USED = (1 << 0), - UF_NEVER_DROP = (1 << 1), -}; - -/* - * Global structs - */ -struct syscall_evt_pair { - int flags; - enum ppm_event_type enter_event_type; - enum ppm_event_type exit_event_type; +#ifndef UDIG +struct ppm_consumer_t { + struct task_struct *consumer_id; +#ifdef __percpu + struct ppm_ring_buffer_context __percpu *ring_buffers; +#else + struct ppm_ring_buffer_context *ring_buffers; +#endif + u32 snaplen; + u32 sampling_ratio; + bool do_dynamic_snaplen; + u32 sampling_interval; + int is_dropping; + int dropping_mode; + volatile int need_to_insert_drop_e; + volatile int need_to_insert_drop_x; + struct list_head node; + uint16_t fullcapture_port_range_start; + uint16_t fullcapture_port_range_end; + uint16_t statsd_port; }; +#endif // UDIG #define STR_STORAGE_SIZE PAGE_SIZE /* * Global functions + * + * These are analogous to get_user(), copy_from_user() and strncpy_from_user(), + * but they can't sleep, barf on page fault or be preempted */ +#define ppm_get_user(x, ptr) ({ ppm_copy_from_user(&x, ptr, sizeof(x)) ? -EFAULT : 0; }) +#ifndef UDIG unsigned long ppm_copy_from_user(void *to, const void __user *from, unsigned long n); long ppm_strncpy_from_user(char *to, const char __user *from, unsigned long n); +#endif // UDIG /* * Global tables */ -#define SYSCALL_TABLE_SIZE 512 + +#ifdef CONFIG_MIPS + #define SYSCALL_TABLE_ID0 __NR_Linux +#elif defined CONFIG_ARM + #define SYSCALL_TABLE_ID0 __NR_SYSCALL_BASE +#elif defined CONFIG_X86 || defined CONFIG_SUPERH + #define SYSCALL_TABLE_ID0 0 +#elif defined CONFIG_PPC64 + #define SYSCALL_TABLE_ID0 0 +#elif defined CONFIG_S390 + #define SYSCALL_TABLE_ID0 0 +#endif extern const struct syscall_evt_pair g_syscall_table[]; extern const struct ppm_event_info g_event_info[]; extern const enum ppm_syscall_code g_syscall_code_routing_table[]; -extern uint32_t g_sampling_ratio; + +#if defined(CONFIG_X86_64) && defined(CONFIG_IA32_EMULATION) +extern const struct syscall_evt_pair g_syscall_ia32_table[]; +extern const enum ppm_syscall_code g_syscall_ia32_code_routing_table[]; +#endif + +#ifndef UDIG +extern void ppm_syscall_get_arguments(struct task_struct *task, struct pt_regs *regs, unsigned long *args); +#endif + +#define NS_TO_SEC(_ns) ((_ns) / 1000000000) +#define MORE_THAN_ONE_SECOND_AHEAD(_ns1, _ns2) ((_ns1) - (_ns2) > 1000000000) +#define SECOND_IN_NS 1000000000 + +#endif /* PPM_H_ */ diff --git a/driver/ppm_compat_unistd_32.h b/driver/ppm_compat_unistd_32.h new file mode 100644 index 0000000000..2deff7360e --- /dev/null +++ b/driver/ppm_compat_unistd_32.h @@ -0,0 +1,402 @@ +#ifndef _ASM_X86_UNISTD_32_H +#define _ASM_X86_UNISTD_32_H + +/* + * This file contains the system call numbers. + */ + +#define __NR_ia32_restart_syscall 0 +#define __NR_ia32_exit 1 +#define __NR_ia32_fork 2 +#define __NR_ia32_read 3 +#define __NR_ia32_write 4 +#define __NR_ia32_open 5 +#define __NR_ia32_close 6 +#define __NR_ia32_waitpid 7 +#define __NR_ia32_creat 8 +#define __NR_ia32_link 9 +#define __NR_ia32_unlink 10 +#define __NR_ia32_execve 11 +#define __NR_ia32_chdir 12 +#define __NR_ia32_time 13 +#define __NR_ia32_mknod 14 +#define __NR_ia32_chmod 15 +#define __NR_ia32_lchown 16 +#define __NR_ia32_break 17 +#define __NR_ia32_oldstat 18 +#define __NR_ia32_lseek 19 +#define __NR_ia32_getpid 20 +#define __NR_ia32_mount 21 +#define __NR_ia32_umount 22 +#define __NR_ia32_setuid 23 +#define __NR_ia32_getuid 24 +#define __NR_ia32_stime 25 +#define __NR_ia32_ptrace 26 +#define __NR_ia32_alarm 27 +#define __NR_ia32_oldfstat 28 +#define __NR_ia32_pause 29 +#define __NR_ia32_utime 30 +#define __NR_ia32_stty 31 +#define __NR_ia32_gtty 32 +#define __NR_ia32_access 33 +#define __NR_ia32_nice 34 +#define __NR_ia32_ftime 35 +#define __NR_ia32_sync 36 +#define __NR_ia32_kill 37 +#define __NR_ia32_rename 38 +#define __NR_ia32_mkdir 39 +#define __NR_ia32_rmdir 40 +#define __NR_ia32_dup 41 +#define __NR_ia32_pipe 42 +#define __NR_ia32_times 43 +#define __NR_ia32_prof 44 +#define __NR_ia32_brk 45 +#define __NR_ia32_setgid 46 +#define __NR_ia32_getgid 47 +#define __NR_ia32_signal 48 +#define __NR_ia32_geteuid 49 +#define __NR_ia32_getegid 50 +#define __NR_ia32_acct 51 +#define __NR_ia32_umount2 52 +#define __NR_ia32_lock 53 +#define __NR_ia32_ioctl 54 +#define __NR_ia32_fcntl 55 +#define __NR_ia32_mpx 56 +#define __NR_ia32_setpgid 57 +#define __NR_ia32_ulimit 58 +#define __NR_ia32_oldolduname 59 +#define __NR_ia32_umask 60 +#define __NR_ia32_chroot 61 +#define __NR_ia32_ustat 62 +#define __NR_ia32_dup2 63 +#define __NR_ia32_getppid 64 +#define __NR_ia32_getpgrp 65 +#define __NR_ia32_setsid 66 +#define __NR_ia32_sigaction 67 +#define __NR_ia32_sgetmask 68 +#define __NR_ia32_ssetmask 69 +#define __NR_ia32_setreuid 70 +#define __NR_ia32_setregid 71 +#define __NR_ia32_sigsuspend 72 +#define __NR_ia32_sigpending 73 +#define __NR_ia32_sethostname 74 +#define __NR_ia32_setrlimit 75 +#define __NR_ia32_getrlimit 76 /* Back compatible 2Gig limited rlimit */ +#define __NR_ia32_getrusage 77 +#define __NR_ia32_gettimeofday 78 +#define __NR_ia32_settimeofday 79 +#define __NR_ia32_getgroups 80 +#define __NR_ia32_setgroups 81 +#define __NR_ia32_select 82 +#define __NR_ia32_symlink 83 +#define __NR_ia32_oldlstat 84 +#define __NR_ia32_readlink 85 +#define __NR_ia32_uselib 86 +#define __NR_ia32_swapon 87 +#define __NR_ia32_reboot 88 +#define __NR_ia32_readdir 89 +#define __NR_ia32_mmap 90 +#define __NR_ia32_munmap 91 +#define __NR_ia32_truncate 92 +#define __NR_ia32_ftruncate 93 +#define __NR_ia32_fchmod 94 +#define __NR_ia32_fchown 95 +#define __NR_ia32_getpriority 96 +#define __NR_ia32_setpriority 97 +#define __NR_ia32_profil 98 +#define __NR_ia32_statfs 99 +#define __NR_ia32_fstatfs 100 +#define __NR_ia32_ioperm 101 +#define __NR_ia32_socketcall 102 +#define __NR_ia32_syslog 103 +#define __NR_ia32_setitimer 104 +#define __NR_ia32_getitimer 105 +#define __NR_ia32_stat 106 +#define __NR_ia32_lstat 107 +#define __NR_ia32_fstat 108 +#define __NR_ia32_olduname 109 +#define __NR_ia32_iopl 110 +#define __NR_ia32_vhangup 111 +#define __NR_ia32_idle 112 +#define __NR_ia32_vm86old 113 +#define __NR_ia32_wait4 114 +#define __NR_ia32_swapoff 115 +#define __NR_ia32_sysinfo 116 +#define __NR_ia32_ipc 117 +#define __NR_ia32_fsync 118 +#define __NR_ia32_sigreturn 119 +#define __NR_ia32_clone 120 +#define __NR_ia32_setdomainname 121 +#define __NR_ia32_uname 122 +#define __NR_ia32_modify_ldt 123 +#define __NR_ia32_adjtimex 124 +#define __NR_ia32_mprotect 125 +#define __NR_ia32_sigprocmask 126 +#define __NR_ia32_create_module 127 +#define __NR_ia32_init_module 128 +#define __NR_ia32_delete_module 129 +#define __NR_ia32_get_kernel_syms 130 +#define __NR_ia32_quotactl 131 +#define __NR_ia32_getpgid 132 +#define __NR_ia32_fchdir 133 +#define __NR_ia32_bdflush 134 +#define __NR_ia32_sysfs 135 +#define __NR_ia32_personality 136 +#define __NR_ia32_afs_syscall 137 /* Syscall for Andrew File System */ +#define __NR_ia32_setfsuid 138 +#define __NR_ia32_setfsgid 139 +#define __NR_ia32__llseek 140 +#define __NR_ia32_getdents 141 +#define __NR_ia32__newselect 142 +#define __NR_ia32_flock 143 +#define __NR_ia32_msync 144 +#define __NR_ia32_readv 145 +#define __NR_ia32_writev 146 +#define __NR_ia32_getsid 147 +#define __NR_ia32_fdatasync 148 +#define __NR_ia32__sysctl 149 +#define __NR_ia32_mlock 150 +#define __NR_ia32_munlock 151 +#define __NR_ia32_mlockall 152 +#define __NR_ia32_munlockall 153 +#define __NR_ia32_sched_setparam 154 +#define __NR_ia32_sched_getparam 155 +#define __NR_ia32_sched_setscheduler 156 +#define __NR_ia32_sched_getscheduler 157 +#define __NR_ia32_sched_yield 158 +#define __NR_ia32_sched_get_priority_max 159 +#define __NR_ia32_sched_get_priority_min 160 +#define __NR_ia32_sched_rr_get_interval 161 +#define __NR_ia32_nanosleep 162 +#define __NR_ia32_mremap 163 +#define __NR_ia32_setresuid 164 +#define __NR_ia32_getresuid 165 +#define __NR_ia32_vm86 166 +#define __NR_ia32_query_module 167 +#define __NR_ia32_poll 168 +#define __NR_ia32_nfsservctl 169 +#define __NR_ia32_setresgid 170 +#define __NR_ia32_getresgid 171 +#define __NR_ia32_prctl 172 +#define __NR_ia32_rt_sigreturn 173 +#define __NR_ia32_rt_sigaction 174 +#define __NR_ia32_rt_sigprocmask 175 +#define __NR_ia32_rt_sigpending 176 +#define __NR_ia32_rt_sigtimedwait 177 +#define __NR_ia32_rt_sigqueueinfo 178 +#define __NR_ia32_rt_sigsuspend 179 +#define __NR_ia32_pread64 180 +#define __NR_ia32_pwrite64 181 +#define __NR_ia32_chown 182 +#define __NR_ia32_getcwd 183 +#define __NR_ia32_capget 184 +#define __NR_ia32_capset 185 +#define __NR_ia32_sigaltstack 186 +#define __NR_ia32_sendfile 187 +#define __NR_ia32_getpmsg 188 /* some people actually want streams */ +#define __NR_ia32_putpmsg 189 /* some people actually want streams */ +#define __NR_ia32_vfork 190 +#define __NR_ia32_ugetrlimit 191 /* SuS compliant getrlimit */ +#define __NR_ia32_mmap2 192 +#define __NR_ia32_truncate64 193 +#define __NR_ia32_ftruncate64 194 +#define __NR_ia32_stat64 195 +#define __NR_ia32_lstat64 196 +#define __NR_ia32_fstat64 197 +#define __NR_ia32_lchown32 198 +#define __NR_ia32_getuid32 199 +#define __NR_ia32_getgid32 200 +#define __NR_ia32_geteuid32 201 +#define __NR_ia32_getegid32 202 +#define __NR_ia32_setreuid32 203 +#define __NR_ia32_setregid32 204 +#define __NR_ia32_getgroups32 205 +#define __NR_ia32_setgroups32 206 +#define __NR_ia32_fchown32 207 +#define __NR_ia32_setresuid32 208 +#define __NR_ia32_getresuid32 209 +#define __NR_ia32_setresgid32 210 +#define __NR_ia32_getresgid32 211 +#define __NR_ia32_chown32 212 +#define __NR_ia32_setuid32 213 +#define __NR_ia32_setgid32 214 +#define __NR_ia32_setfsuid32 215 +#define __NR_ia32_setfsgid32 216 +#define __NR_ia32_pivot_root 217 +#define __NR_ia32_mincore 218 +#define __NR_ia32_madvise 219 +#define __NR_ia32_madvise1 219 /* delete when C lib stub is removed */ +#define __NR_ia32_getdents64 220 +#define __NR_ia32_fcntl64 221 +/* 223 is unused */ +#define __NR_ia32_gettid 224 +#define __NR_ia32_readahead 225 +#define __NR_ia32_setxattr 226 +#define __NR_ia32_lsetxattr 227 +#define __NR_ia32_fsetxattr 228 +#define __NR_ia32_getxattr 229 +#define __NR_ia32_lgetxattr 230 +#define __NR_ia32_fgetxattr 231 +#define __NR_ia32_listxattr 232 +#define __NR_ia32_llistxattr 233 +#define __NR_ia32_flistxattr 234 +#define __NR_ia32_removexattr 235 +#define __NR_ia32_lremovexattr 236 +#define __NR_ia32_fremovexattr 237 +#define __NR_ia32_tkill 238 +#define __NR_ia32_sendfile64 239 +#define __NR_ia32_futex 240 +#define __NR_ia32_sched_setaffinity 241 +#define __NR_ia32_sched_getaffinity 242 +#define __NR_ia32_set_thread_area 243 +#define __NR_ia32_get_thread_area 244 +#define __NR_ia32_io_setup 245 +#define __NR_ia32_io_destroy 246 +#define __NR_ia32_io_getevents 247 +#define __NR_ia32_io_submit 248 +#define __NR_ia32_io_cancel 249 +#define __NR_ia32_fadvise64 250 +/* 251 is available for reuse (was briefly sys_set_zone_reclaim) */ +#define __NR_ia32_exit_group 252 +#define __NR_ia32_lookup_dcookie 253 +#define __NR_ia32_epoll_create 254 +#define __NR_ia32_epoll_ctl 255 +#define __NR_ia32_epoll_wait 256 +#define __NR_ia32_remap_file_pages 257 +#define __NR_ia32_set_tid_address 258 +#define __NR_ia32_timer_create 259 +#define __NR_ia32_timer_settime (__NR_timer_create+1) +#define __NR_ia32_timer_gettime (__NR_timer_create+2) +#define __NR_ia32_timer_getoverrun (__NR_timer_create+3) +#define __NR_ia32_timer_delete (__NR_timer_create+4) +#define __NR_ia32_clock_settime (__NR_timer_create+5) +#define __NR_ia32_clock_gettime (__NR_timer_create+6) +#define __NR_ia32_clock_getres (__NR_timer_create+7) +#define __NR_ia32_clock_nanosleep (__NR_timer_create+8) +#define __NR_ia32_statfs64 268 +#define __NR_ia32_fstatfs64 269 +#define __NR_ia32_tgkill 270 +#define __NR_ia32_utimes 271 +#define __NR_ia32_fadvise64_64 272 +#define __NR_ia32_vserver 273 +#define __NR_ia32_mbind 274 +#define __NR_ia32_get_mempolicy 275 +#define __NR_ia32_set_mempolicy 276 +#define __NR_ia32_mq_open 277 +#define __NR_ia32_mq_unlink (__NR_mq_open+1) +#define __NR_ia32_mq_timedsend (__NR_mq_open+2) +#define __NR_ia32_mq_timedreceive (__NR_mq_open+3) +#define __NR_ia32_mq_notify (__NR_mq_open+4) +#define __NR_ia32_mq_getsetattr (__NR_mq_open+5) +#define __NR_ia32_kexec_load 283 +#define __NR_ia32_waitid 284 +/* #define __NR_ia32_sys_setaltroot 285 */ +#define __NR_ia32_add_key 286 +#define __NR_ia32_request_key 287 +#define __NR_ia32_keyctl 288 +#define __NR_ia32_ioprio_set 289 +#define __NR_ia32_ioprio_get 290 +#define __NR_ia32_inotify_init 291 +#define __NR_ia32_inotify_add_watch 292 +#define __NR_ia32_inotify_rm_watch 293 +#define __NR_ia32_migrate_pages 294 +#define __NR_ia32_openat 295 +#define __NR_ia32_mkdirat 296 +#define __NR_ia32_mknodat 297 +#define __NR_ia32_fchownat 298 +#define __NR_ia32_futimesat 299 +#define __NR_ia32_fstatat64 300 +#define __NR_ia32_unlinkat 301 +#define __NR_ia32_renameat 302 +#define __NR_ia32_linkat 303 +#define __NR_ia32_symlinkat 304 +#define __NR_ia32_readlinkat 305 +#define __NR_ia32_fchmodat 306 +#define __NR_ia32_faccessat 307 +#define __NR_ia32_pselect6 308 +#define __NR_ia32_ppoll 309 +#define __NR_ia32_unshare 310 +#define __NR_ia32_set_robust_list 311 +#define __NR_ia32_get_robust_list 312 +#define __NR_ia32_splice 313 +#define __NR_ia32_sync_file_range 314 +#define __NR_ia32_tee 315 +#define __NR_ia32_vmsplice 316 +#define __NR_ia32_move_pages 317 +#define __NR_ia32_getcpu 318 +#define __NR_ia32_epoll_pwait 319 +#define __NR_ia32_utimensat 320 +#define __NR_ia32_signalfd 321 +#define __NR_ia32_timerfd_create 322 +#define __NR_ia32_eventfd 323 +#define __NR_ia32_fallocate 324 +#define __NR_ia32_timerfd_settime 325 +#define __NR_ia32_timerfd_gettime 326 +#define __NR_ia32_signalfd4 327 +#define __NR_ia32_eventfd2 328 +#define __NR_ia32_epoll_create1 329 +#define __NR_ia32_dup3 330 +#define __NR_ia32_pipe2 331 +#define __NR_ia32_inotify_init1 332 +#define __NR_ia32_preadv 333 +#define __NR_ia32_pwritev 334 +#define __NR_ia32_rt_tgsigqueueinfo 335 +#define __NR_ia32_perf_event_open 336 +#define __NR_ia32_recvmmsg 337 +#define __NR_ia32_fanotify_init 338 +#define __NR_ia32_fanotify_mark 339 +#define __NR_ia32_prlimit64 340 +#define __NR_ia32_name_to_handle_at 341 +#define __NR_ia32_open_by_handle_at 342 +#define __NR_ia32_clock_adjtime 343 +#define __NR_ia32_syncfs 344 +#define __NR_ia32_sendmmsg 345 +#define __NR_ia32_setns 346 +#define __NR_ia32_process_vm_readv 347 +#define __NR_ia32_process_vm_writev 348 +#define __NR_ia32_renameat2 349 + +#ifdef __KERNEL__ + +#define NR_ia32_syscalls 350 + +#define __ARCH_WANT_IPC_PARSE_VERSION +#define __ARCH_WANT_OLD_READDIR +#define __ARCH_WANT_OLD_STAT +#define __ARCH_WANT_STAT64 +#define __ARCH_WANT_SYS_ALARM +#define __ARCH_WANT_SYS_GETHOSTNAME +#define __ARCH_WANT_SYS_IPC +#define __ARCH_WANT_SYS_PAUSE +#define __ARCH_WANT_SYS_SGETMASK +#define __ARCH_WANT_SYS_SIGNAL +#define __ARCH_WANT_SYS_TIME +#define __ARCH_WANT_SYS_UTIME +#define __ARCH_WANT_SYS_WAITPID +#define __ARCH_WANT_SYS_SOCKETCALL +#define __ARCH_WANT_SYS_FADVISE64 +#define __ARCH_WANT_SYS_GETPGRP +#define __ARCH_WANT_SYS_LLSEEK +#define __ARCH_WANT_SYS_NICE +#define __ARCH_WANT_SYS_OLD_GETRLIMIT +#define __ARCH_WANT_SYS_OLD_UNAME +#define __ARCH_WANT_SYS_OLD_MMAP +#define __ARCH_WANT_SYS_OLD_SELECT +#define __ARCH_WANT_SYS_OLDUMOUNT +#define __ARCH_WANT_SYS_SIGPENDING +#define __ARCH_WANT_SYS_SIGPROCMASK +#define __ARCH_WANT_SYS_RT_SIGACTION +#define __ARCH_WANT_SYS_RT_SIGSUSPEND + +/* + * "Conditional" syscalls + * + * What we want is __attribute__((weak,alias("sys_ni_syscall"))), + * but it doesn't work on all toolchains, so we just do it by hand + */ +#ifndef cond_syscall +#define cond_syscall(x) asm(".weak\t" #x "\n\t.set\t" #x ",sys_ni_syscall") +#endif + +#endif /* __KERNEL__ */ +#endif /* _ASM_X86_UNISTD_32_H */ diff --git a/driver/ppm_cputime.c b/driver/ppm_cputime.c new file mode 100644 index 0000000000..5e7c7e6dce --- /dev/null +++ b/driver/ppm_cputime.c @@ -0,0 +1,367 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ +#include + +// These function are taken from the linux kernel and are used only +// on versions that don't export task_cputime_adjusted() +#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) + +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37)) +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "ppm_ringbuffer.h" +#include "ppm_events_public.h" +#include "ppm_events.h" +#include "ppm.h" +#include "ppm_version.h" + +#if (defined CONFIG_VIRT_CPU_ACCOUNTING_NATIVE) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)) +void ppm_task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) +{ + *ut = p->utime; + *st = p->stime; +} +#else + +#ifndef cmpxchg_cputime +#define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new) +#endif + +#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0)) || (PPM_RHEL_RELEASE_CODE > 0 && PPM_RHEL_RELEASE_CODE >= PPM_RHEL_RELEASE_VERSION(7, 7)) +#define ppm_vtime_starttime(tsk) ((tsk)->vtime.starttime) +#define ppm_vtime_seqlock(tsk) (&(tsk)->vtime.seqlock) +#define ppm_vtime_state(tsk) ((tsk)->vtime.state) +#else +#define ppm_vtime_starttime(tsk) ((tsk)->vtime_snap) +#define ppm_vtime_seqlock(tsk) (&(tsk)->vtime_seqlock) +#define ppm_vtime_state(tsk) ((tsk)->vtime_snap_whence) +#endif + +static unsigned long long vtime_delta(struct task_struct *tsk) +{ + unsigned long long clock; + + clock = local_clock(); + if (clock < ppm_vtime_starttime(tsk)) + return 0; + + return clock - ppm_vtime_starttime(tsk); +} + +static void +fetch_task_cputime(struct task_struct *t, + cputime_t *u_dst, cputime_t *s_dst, + cputime_t *u_src, cputime_t *s_src, + cputime_t *udelta, cputime_t *sdelta) +{ + unsigned int seq; + unsigned long long delta; + + do { + *udelta = 0; + *sdelta = 0; + + seq = read_seqbegin(ppm_vtime_seqlock(t)); + + if (u_dst) + *u_dst = *u_src; + if (s_dst) + *s_dst = *s_src; + + /* Task is sleeping, nothing to add */ + if (ppm_vtime_state(t) == VTIME_SLEEPING || + is_idle_task(t)) + continue; + + delta = vtime_delta(t); + + /* + * Task runs either in user or kernel space, add pending nohz time to + * the right place. + */ + if (ppm_vtime_state(t) == VTIME_USER || t->flags & PF_VCPU) { + *udelta = delta; + } else { + if (ppm_vtime_state(t) == VTIME_SYS) + *sdelta = delta; + } + } while (read_seqretry(ppm_vtime_seqlock(t), seq)); +} + +void task_cputime(struct task_struct *t, cputime_t *utime, cputime_t *stime) +{ + cputime_t udelta, sdelta; + + fetch_task_cputime(t, utime, stime, &t->utime, + &t->stime, &udelta, &sdelta); + if (utime) + *utime += udelta; + if (stime) + *stime += sdelta; +} +#elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) +static inline void task_cputime(struct task_struct *t, + cputime_t *utime, cputime_t *stime) +{ + if (utime) + *utime = t->utime; + if (stime) + *stime = t->stime; +} +#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ + +u64 nsecs_to_jiffies64(u64 n) +{ +#if (NSEC_PER_SEC % HZ) == 0 + /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */ + return div_u64(n, NSEC_PER_SEC / HZ); +#elif (HZ % 512) == 0 + /* overflow after 292 years if HZ = 1024 */ + return div_u64(n * HZ / 512, NSEC_PER_SEC / 512); +#else + /* + * Generic case - optimized for cases where HZ is a multiple of 3. + * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc. + */ + return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ); +#endif +} + +unsigned long nsecs_to_jiffies(u64 n) +{ + return (unsigned long)nsecs_to_jiffies64(n); +} + +#ifndef nsecs_to_cputime +#ifdef msecs_to_cputime +#define nsecs_to_cputime(__nsecs) \ + msecs_to_cputime(div_u64((__nsecs), NSEC_PER_MSEC)) +#else +#define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs) +#endif +#endif + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) +/* + * Perform (stime * rtime) / total, but avoid multiplication overflow by + * loosing precision when the numbers are big. + */ +static cputime_t scale_stime(u64 stime, u64 rtime, u64 total) +{ + u64 scaled; + + for (;;) { + /* Make sure "rtime" is the bigger of stime/rtime */ + if (stime > rtime) + swap(rtime, stime); + + /* Make sure 'total' fits in 32 bits */ + if (total >> 32) + goto drop_precision; + + /* Does rtime (and thus stime) fit in 32 bits? */ + if (!(rtime >> 32)) + break; + + /* Can we just balance rtime/stime rather than dropping bits? */ + if (stime >> 31) + goto drop_precision; + + /* We can grow stime and shrink rtime and try to make them both fit */ + stime <<= 1; + rtime >>= 1; + continue; + +drop_precision: + /* We drop from rtime, it has more bits than stime */ + rtime >>= 1; + total >>= 1; + } + + /* + * Make sure gcc understands that this is a 32x32->64 multiply, + * followed by a 64/32->64 divide. + */ + scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total); + return (__force cputime_t) scaled; +} + +/* + * Atomically advance counter to the new value. Interrupts, vcpu + * scheduling, and scaling inaccuracies can cause cputime_advance + * to be occasionally called with a new value smaller than counter. + * Let's enforce atomicity. + * + * Normally a caller will only go through this loop once, or not + * at all in case a previous caller updated counter the same jiffy. + */ +static void cputime_advance(cputime_t *counter, cputime_t new) +{ + cputime_t old; + + while (new > (old = ACCESS_ONCE(*counter))) + cmpxchg_cputime(counter, old, new); +} + +/* + * Adjust tick based cputime random precision against scheduler + * runtime accounting. + */ +static void cputime_adjust(struct task_cputime *curr, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)) + struct prev_cputime *prev, +#else + struct cputime *prev, +#endif + cputime_t *ut, cputime_t *st) +{ + cputime_t rtime, stime, utime; + + /* + * Tick based cputime accounting depend on random scheduling + * timeslices of a task to be interrupted or not by the timer. + * Depending on these circumstances, the number of these interrupts + * may be over or under-optimistic, matching the real user and system + * cputime with a variable precision. + * + * Fix this by scaling these tick based values against the total + * runtime accounted by the CFS scheduler. + */ + rtime = nsecs_to_cputime(curr->sum_exec_runtime); + + /* + * Update userspace visible utime/stime values only if actual execution + * time is bigger than already exported. Note that can happen, that we + * provided bigger values due to scaling inaccuracy on big numbers. + */ + if (prev->stime + prev->utime >= rtime) + goto out; + + stime = curr->stime; + utime = curr->utime; + + if (utime == 0) { + stime = rtime; + } else if (stime == 0) { + utime = rtime; + } else { + cputime_t total = stime + utime; + + stime = scale_stime((__force u64)stime, + (__force u64)rtime, (__force u64)total); + utime = rtime - stime; + } + + cputime_advance(&prev->stime, stime); + cputime_advance(&prev->utime, utime); + +out: + *ut = prev->utime; + *st = prev->stime; +} + +void ppm_task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) +{ + struct task_cputime cputime = { +#ifdef CONFIG_SCHED_BFS + .sum_exec_runtime = tsk_seruntime(p), +#else + .sum_exec_runtime = p->se.sum_exec_runtime, +#endif + }; + + task_cputime(p, &cputime.utime, &cputime.stime); + cputime_adjust(&cputime, &p->prev_cputime, ut, st); +} + +#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0) */ + +static cputime_t scale_utime(cputime_t utime, cputime_t rtime, cputime_t total) +{ + u64 temp = (__force u64) rtime; + + temp *= (__force u64) utime; + + if (sizeof(cputime_t) == 4) + temp = div_u64(temp, (__force u32) total); + else + temp = div64_u64(temp, (__force u64) total); + + return (__force cputime_t) temp; +} + +// Taken from task_times(struct task_struct *p, cputime_t *ut, cputime_t *st) +void ppm_task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) +{ + cputime_t rtime, utime = p->utime, total = utime + p->stime; + + /* + * Use CFS's precise accounting: + */ + rtime = nsecs_to_cputime(p->se.sum_exec_runtime); + + if (total) + utime = scale_utime(utime, rtime, total); + else + utime = rtime; + + /* + * Compare with previous values, to keep monotonicity: + */ + p->prev_utime = max(p->prev_utime, utime); + p->prev_stime = max(p->prev_stime, rtime - p->prev_utime); + + *ut = p->prev_utime; + *st = p->prev_stime; +} + +#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)) */ +#endif /* (defined CONFIG_VIRT_CPU_ACCOUNTING_NATIVE) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)) */ +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)) */ + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)) +#include +#include + +/* + * Implementation copied from kernel/time/time.c in 4.11.0 + */ +u64 nsec_to_clock_t(u64 x) +{ +#if (NSEC_PER_SEC % USER_HZ) == 0 + return div_u64(x, NSEC_PER_SEC / USER_HZ); +#elif (USER_HZ % 512) == 0 + return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512); +#else + /* + * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, + * overflow after 64.99 years + * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... + */ + return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ); +#endif +} +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)) */ diff --git a/driver/ppm_events.c b/driver/ppm_events.c index f923393fa6..d17793a5e9 100644 --- a/driver/ppm_events.c +++ b/driver/ppm_events.c @@ -1,48 +1,102 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#ifndef UDIG #include +#include #include -#include #include #include +#include #include #include #include #include -#include #include #include #include +#include +#include +#include +#include +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 20) +#include +#include "ppm_syscall.h" +#else +#include +#endif +#else // UDIG +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "udig_capture.h" +#include "ppm_ringbuffer.h" +#include "ppm_events_public.h" +#include "ppm_events.h" +#include "ppm.h" + +#include "udig_inf.h" +#endif // UDIG #include "ppm_ringbuffer.h" #include "ppm_events_public.h" #include "ppm_events.h" #include "ppm.h" +#include "ppm_flag_helpers.h" +#include "ppm_version.h" /* - * do-nothing implementation of compat_ptr for systems that are not compiled - * with CONFIG_COMPAT. + * The kernel patched with grsecurity makes the default access_ok trigger a + * might_sleep(), so if present we use the one defined by them */ -#ifndef CONFIG_COMPAT -#define compat_ptr(X) X +#ifndef UDIG +#ifdef access_ok_noprefault +#define ppm_access_ok access_ok_noprefault +#else +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)) || (PPM_RHEL_RELEASE_CODE > 0 && PPM_RHEL_RELEASE_CODE >= PPM_RHEL_RELEASE_VERSION(8, 1)) +#define ppm_access_ok(type, addr, size) access_ok(addr, size) +#else +#define ppm_access_ok(type, addr, size) access_ok(type, addr, size) +#endif #endif +extern bool g_tracers_enabled; + static void memory_dump(char *p, size_t size) { unsigned int j; @@ -50,7 +104,27 @@ static void memory_dump(char *p, size_t size) for (j = 0; j < size; j += 8) pr_info("%*ph\n", 8, &p[j]); } +#endif // UDIG + +static inline bool in_port_range(uint16_t port, uint16_t min, uint16_t max) +{ + return port >= min && port <= max; +} +/* + * Globals + */ +u32 g_http_options_intval; +u32 g_http_get_intval; +u32 g_http_head_intval; +u32 g_http_post_intval; +u32 g_http_put_intval; +u32 g_http_delete_intval; +u32 g_http_trace_intval; +u32 g_http_connect_intval; +u32 g_http_resp_intval; + +#ifndef UDIG /* * What this function does is basically a special memcpy * so that, if the page fault handler detects the address is invalid, @@ -65,9 +139,8 @@ unsigned long ppm_copy_from_user(void *to, const void __user *from, unsigned lon pagefault_disable(); - if (likely(access_ok(VERIFY_READ, from, n))) { + if (likely(ppm_access_ok(VERIFY_READ, from, n))) res = __copy_from_user_inatomic(to, from, n); - } pagefault_enable(); @@ -94,11 +167,10 @@ long ppm_strncpy_from_user(char *to, const char __user *from, unsigned long n) * Read bytes_to_read bytes at a time, and look for the terminator. Should be fast * since the copy_from_user is optimized for the processor */ - if (n < bytes_to_read) { + if (n < bytes_to_read) bytes_to_read = n; - } - if (!access_ok(VERIFY_READ, from, n)) { + if (!ppm_access_ok(VERIFY_READ, from, bytes_to_read)) { res = -1; goto strncpy_end; } @@ -130,48 +202,420 @@ long ppm_strncpy_from_user(char *to, const char __user *from, unsigned long n) pagefault_enable(); return res; } +#endif + +int32_t dpi_lookahead_init(void) +{ + g_http_options_intval = (*(u32 *)HTTP_OPTIONS_STR); + g_http_get_intval = (*(u32 *)HTTP_GET_STR); + g_http_head_intval = (*(u32 *)HTTP_HEAD_STR); + g_http_post_intval = (*(u32 *)HTTP_POST_STR); + g_http_put_intval = (*(u32 *)HTTP_PUT_STR); + g_http_delete_intval = (*(u32 *)HTTP_DELETE_STR); + g_http_trace_intval = (*(u32 *)HTTP_TRACE_STR); + g_http_connect_intval = (*(u32 *)HTTP_CONNECT_STR); + g_http_resp_intval = (*(u32 *)HTTP_RESP_STR); + + return PPM_SUCCESS; +} + +#ifndef UDIG +inline int sock_getname(struct socket* sock, struct sockaddr* sock_address, int peer) +{ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 17, 0) + int ret = sock->ops->getname(sock, sock_address, peer); + if (ret >= 0) + ret = 0; + return ret; +#else + int sockaddr_len; + return sock->ops->getname(sock, sock_address, &sockaddr_len, peer); +#endif +} + +/** + * Compute the snaplen for the arguments. + * + * The snaplen is the amount of argument data returned along with the event. + * Normally, the driver performs a dynamic calculation to figure out snaplen + * per-event. However, if this calculation is disabled + * (i.e. args->consumer->do_dynamic_snaplen == false), the snaplen will always + * be args->consumer->snaplen. + * + * If dynamic snaplen is enabled, here's how the calculation works: + * + * 1. If the event is a write to /dev/null, it gets a special snaplen because + * writes to /dev/null is a backdoor method for inserting special events + * into the event stream. + * 2. If the event is NOT a socket operation, return args->consumer->snaplen. + * 3. If the sending port OR destination port falls within the fullcapture port + * range specified by the user, return 16000. + * 4. Protocol detection. A number of applications are detected heuristically + * and given a longer snaplen (2000). These applications are MYSQL, Postgres, + * HTTP, mongodb, and statsd. + * 5. If none of the above apply, return args->consumer->snaplen. + */ +inline u32 compute_snaplen(struct event_filler_arguments *args, char *buf, u32 lookahead_size) +{ + u32 res = args->consumer->snaplen; + int err; + struct socket *sock; + sa_family_t family; + struct sockaddr_storage sock_address; + struct sockaddr_storage peer_address; + u16 sport, dport; + u16 min_port = 0, max_port = 0; + u32 dynamic_snaplen = 2000; + + if (args->consumer->snaplen > dynamic_snaplen) { + /* + * If the user requested a default snaplen greater than the custom + * snaplen given to certain applications, just use the greater value. + */ + dynamic_snaplen = args->consumer->snaplen; + } + + /* Increase snaplen on writes to /dev/null */ + if (g_tracers_enabled && args->event_type == PPME_SYSCALL_WRITE_X) { +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0) + struct fd f = fdget(args->fd); + + if (f.file && f.file->f_inode) { + if (f.file->f_inode->i_rdev == PPM_NULL_RDEV) { + res = RW_SNAPLEN_EVENT; + fdput(f); + return res; + } + + fdput(f); + } +#else + struct file* file = fget(args->fd); + /* Use cached f_inode only on kernel versions that have it + * https://github.com/torvalds/linux/commit/dd37978c50bc8b354e5c4633f69387f16572fdac + */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0) + if (file && file->f_inode) { + if (file->f_inode->i_rdev == PPM_NULL_RDEV) { + // Use f_dentry for older kernel versions +#elif LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,20) + if (file && file->f_dentry && file->f_dentry->d_inode) { + if (file->f_dentry->d_inode->i_rdev == PPM_NULL_RDEV) { +#else + if (file && file->f_path.dentry && file->f_path.dentry->d_inode) { + if (file->f_path.dentry->d_inode->i_rdev == PPM_NULL_RDEV) { +#endif + res = RW_SNAPLEN_EVENT; + fput(file); + return res; + } + + fput(file); + } +#endif + } + + if (!args->consumer->do_dynamic_snaplen) + return res; + + sock = sockfd_lookup(args->fd, &err); + + if (!sock) { + return res; + } + + if (!sock->sk) { + goto done; + } + + err = sock_getname(sock, (struct sockaddr *)&sock_address, 0); + + if (err != 0) { + goto done; + } + + /* Try to get the source and destination port */ + if (args->event_type == PPME_SOCKET_SENDTO_X) { + unsigned long syscall_args[6] = {}; + unsigned long val; + struct sockaddr __user * usrsockaddr; + /* + * Get the address + */ + if (!args->is_socketcall) { + ppm_syscall_get_arguments(current, args->regs, syscall_args); + val = syscall_args[4]; + } else { + val = args->socketcall_args[4]; + } + + usrsockaddr = (struct sockaddr __user *)val; + + if(usrsockaddr == NULL) { + /* + * Suppose is a connected socket, fall back to fd + */ + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); + } else { + /* + * Get the address len + */ + if (!args->is_socketcall) { + ppm_syscall_get_arguments(current, args->regs, syscall_args); + val = syscall_args[5]; + } else { + val = args->socketcall_args[5]; + } + + if (val != 0) { + /* + * Copy the address + */ + err = addr_to_kernel(usrsockaddr, val, (struct sockaddr *)&peer_address); + } else { + /* + * This case should be very rare, fallback again to sock + */ + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); + } + } + } else if (args->event_type == PPME_SOCKET_SENDMSG_X) { + unsigned long syscall_args[6] = {}; + unsigned long val; + struct sockaddr __user * usrsockaddr; + int addrlen; +#ifdef CONFIG_COMPAT + struct compat_msghdr compat_mh; +#endif +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) + struct user_msghdr mh; +#else + struct msghdr mh; +#endif + + if (!args->is_socketcall) { + ppm_syscall_get_arguments(current, args->regs, syscall_args); + val = syscall_args[1]; + } else { + val = args->socketcall_args[1]; + } + +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&mh, (const void __user *)val, sizeof(mh)))) { + usrsockaddr = NULL; + addrlen = 0; + } else { + usrsockaddr = (struct sockaddr __user *)mh.msg_name; + addrlen = mh.msg_namelen; + } +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(&compat_mh, (const void __user *)compat_ptr(val), sizeof(compat_mh)))) { + usrsockaddr = NULL; + addrlen = 0; + } else { + usrsockaddr = (struct sockaddr __user *)compat_ptr(compat_mh.msg_name); + addrlen = compat_mh.msg_namelen; + } + } +#endif + + if (usrsockaddr != NULL && addrlen != 0) { + /* + * Copy the address + */ + err = addr_to_kernel(usrsockaddr, addrlen, (struct sockaddr *)&peer_address); + } else { + /* + * Suppose it is a connected socket, fall back to fd + */ + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); + } + } else { + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); + } + + if (err != 0) { + goto done; + } + + /* + * If there's a valid source / dest port, use it to run heuristics + * for determining snaplen. + */ + min_port = args->consumer->fullcapture_port_range_start; + max_port = args->consumer->fullcapture_port_range_end; + family = sock->sk->sk_family; + + if (family == AF_INET) { + sport = ntohs(((struct sockaddr_in *) &sock_address)->sin_port); + dport = ntohs(((struct sockaddr_in *) &peer_address)->sin_port); + } else if (family == AF_INET6) { + sport = ntohs(((struct sockaddr_in6 *) &sock_address)->sin6_port); + dport = ntohs(((struct sockaddr_in6 *) &peer_address)->sin6_port); + } else { + sport = 0; + dport = 0; + } + + if (max_port > 0 && + (in_port_range(sport, min_port, max_port) || + in_port_range(dport, min_port, max_port))) { + /* + * Before checking the well-known ports, see if the user has requested + * an increased snaplen for the port in question. + */ + sockfd_put(sock); + return RW_MAX_FULLCAPTURE_PORT_SNAPLEN; + } else if (sport == PPM_PORT_MYSQL || dport == PPM_PORT_MYSQL) { + if (lookahead_size >= 5) { + if (buf[0] == 3 || buf[1] == 3 || buf[2] == 3 || buf[3] == 3 || buf[4] == 3) { + res = dynamic_snaplen; + goto done; + } else if (buf[2] == 0 && buf[3] == 0) { + res = dynamic_snaplen; + goto done; + } + } + } else if (sport == PPM_PORT_POSTGRES || dport == PPM_PORT_POSTGRES) { + if (lookahead_size >= 2) { + if ((buf[0] == 'Q' && buf[1] == 0) || /* SimpleQuery command */ + (buf[0] == 'P' && buf[1] == 0) || /* Prepare statement command */ + (buf[0] == 'E' && buf[1] == 0) /* error or execute command */ + ) { + res = dynamic_snaplen; + goto done; + } + } + if (lookahead_size >= 7 && + (buf[4] == 0 && buf[5] == 3 && buf[6] == 0)) { /* startup command */ + res = dynamic_snaplen; + goto done; + } + } else if ((sport == PPM_PORT_MONGODB || dport == PPM_PORT_MONGODB) || + (lookahead_size >= 16 && + (*(int32_t *)(buf+12) == 1 || /* matches header */ + *(int32_t *)(buf+12) == 2001 || + *(int32_t *)(buf+12) == 2002 || + *(int32_t *)(buf+12) == 2003 || + *(int32_t *)(buf+12) == 2004 || + *(int32_t *)(buf+12) == 2005 || + *(int32_t *)(buf+12) == 2006 || + *(int32_t *)(buf+12) == 2007) + ) + ) { + res = dynamic_snaplen; + goto done; + } else if (dport == args->consumer->statsd_port) { + res = dynamic_snaplen; + goto done; + } else { + if (lookahead_size >= 5) { + if (*(u32 *)buf == g_http_get_intval || + *(u32 *)buf == g_http_post_intval || + *(u32 *)buf == g_http_put_intval || + *(u32 *)buf == g_http_delete_intval || + *(u32 *)buf == g_http_trace_intval || + *(u32 *)buf == g_http_connect_intval || + *(u32 *)buf == g_http_options_intval || + ((*(u32 *)buf == g_http_resp_intval) && (buf[4] == '/')) + ) { + res = dynamic_snaplen; + goto done; + } + } + } + +done: + sockfd_put(sock); + return res; +} +#endif // UDIG /* * NOTES: * - val_len is ignored for everything other than PT_BYTEBUF. * - fromuser is ignored for numeric types + * - dyn_idx is ignored for everything other than PT_DYN */ -inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u16 val_len, bool fromuser) +int val_to_ring(struct event_filler_arguments *args, uint64_t val, u32 val_len, bool fromuser, u8 dyn_idx) { - int32_t len = -1; + const struct ppm_param_info *param_info; + int len = -1; u16 *psize = (u16 *)(args->buffer + args->curarg * sizeof(u16)); + u32 max_arg_size = args->arg_data_size; if (unlikely(args->curarg >= args->nargs)) { - pr_info("sysdig-probe: %u)val_to_ring: too many arguments for event #%u, type=%u, curarg=%u, nargs=%u tid:%u\n", - smp_processor_id(), - args->nevents, - (uint32_t)args->event_type, - args->curarg, - args->nargs, - current->pid); +#ifndef UDIG + pr_err("(%u)val_to_ring: too many arguments for event #%u, type=%u, curarg=%u, nargs=%u tid:%u\n", + smp_processor_id(), + args->nevents, + (u32)args->event_type, + args->curarg, + args->nargs, + current->pid); memory_dump(args->buffer - sizeof(struct ppm_evt_hdr), 32); +#endif ASSERT(0); return PPM_FAILURE_BUG; } - switch (g_event_info[args->event_type].params[args->curarg].type) { + if (unlikely(args->arg_data_size == 0)) + return PPM_FAILURE_BUFFER_FULL; + + if (max_arg_size > PPM_MAX_ARG_SIZE) + max_arg_size = PPM_MAX_ARG_SIZE; + + param_info = &(g_event_info[args->event_type].params[args->curarg]); + if (param_info->type == PT_DYN && param_info->info != NULL) { + const struct ppm_param_info *dyn_params; + + if (unlikely(dyn_idx >= param_info->ninfo)) { + ASSERT(0); + return PPM_FAILURE_BUG; + } + +#ifdef UDIG + dyn_params = (const struct ppm_param_info *)patch_pointer((uint8_t*)param_info->info); +#else + dyn_params = (const struct ppm_param_info *)param_info->info; +#endif + + param_info = &dyn_params[dyn_idx]; + if (likely(max_arg_size >= sizeof(u8))) { + *(u8 *)(args->buffer + args->arg_data_offset) = dyn_idx; + len = sizeof(u8); + } else { + return PPM_FAILURE_BUFFER_FULL; + } + args->arg_data_offset += len; + args->arg_data_size -= len; + max_arg_size -= len; + *psize = (u16)len; + } else { + *psize = 0; + } + + switch (param_info->type) { case PT_CHARBUF: case PT_FSPATH: + case PT_FSRELPATH: if (likely(val != 0)) { if (fromuser) { len = ppm_strncpy_from_user(args->buffer + args->arg_data_offset, - (const char __user *)(unsigned long)val, args->arg_data_size); + (const char __user *)(unsigned long)val, max_arg_size); - if (unlikely(len < 0)) { + if (unlikely(len < 0)) return PPM_FAILURE_INVALID_USER_MEMORY; - } } else { - char *dest = strncpy(args->buffer + args->arg_data_offset, + len = strlcpy(args->buffer + args->arg_data_offset, (const char *)(unsigned long)val, - args->arg_data_size); + max_arg_size); - dest[args->arg_data_size - 1] = 0; - len = strlen(dest) + 1; + if (++len > max_arg_size) + len = max_arg_size; } /* @@ -182,39 +626,116 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 /* * Handle NULL pointers */ - char *dest = strncpy(args->buffer + args->arg_data_offset, - "(NULL)", - args->arg_data_size); + len = strlcpy(args->buffer + args->arg_data_offset, + "(NULL)", + max_arg_size); - dest[args->arg_data_size - 1] = 0; - len = strlen(dest) + 1; + if (++len > max_arg_size) + len = max_arg_size; } break; case PT_BYTEBUF: + if (likely(val != 0)) { + if (fromuser) { + /* + * Copy the lookahead portion of the buffer that we will use DPI-based + * snaplen calculation + */ + u32 dpi_lookahead_size = DPI_LOOKAHEAD_SIZE; + + if (dpi_lookahead_size > val_len) + dpi_lookahead_size = val_len; + + if (unlikely(dpi_lookahead_size >= max_arg_size)) + return PPM_FAILURE_BUFFER_FULL; + + len = (int)ppm_copy_from_user(args->buffer + args->arg_data_offset, + (const void __user *)(unsigned long)val, + dpi_lookahead_size); + + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * Check if there's more to copy + */ + if (likely((dpi_lookahead_size != val_len))) { + /* + * Calculate the snaplen + */ + if (likely(args->enforce_snaplen)) { + u32 sl = args->consumer->snaplen; + +#ifndef UDIG + sl = compute_snaplen(args, args->buffer + args->arg_data_offset, dpi_lookahead_size); +#endif + if (val_len > sl) + val_len = sl; + } + + if (unlikely((val_len) >= max_arg_size)) + val_len = max_arg_size; + + if (val_len > dpi_lookahead_size) { + len = (int)ppm_copy_from_user(args->buffer + args->arg_data_offset + dpi_lookahead_size, + (const void __user *)(unsigned long)val + dpi_lookahead_size, + val_len - dpi_lookahead_size); + + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + } + } + + len = val_len; + } else { + if (likely(args->enforce_snaplen)) { +#ifdef UDIG + u32 sl = args->consumer->snaplen; +#else + u32 sl = compute_snaplen(args, (char *)(unsigned long)val, val_len); +#endif + if (val_len > sl) + val_len = sl; + } + + if (unlikely(val_len >= max_arg_size)) + return PPM_FAILURE_BUFFER_FULL; + + memcpy(args->buffer + args->arg_data_offset, + (void *)(unsigned long)val, val_len); + + len = val_len; + } + } else { + /* + * Handle NULL pointers + */ + len = 0; + } + + break; case PT_SOCKADDR: case PT_SOCKTUPLE: case PT_FDLIST: if (likely(val != 0)) { - if (unlikely(val_len >= args->arg_data_size)) { + if (unlikely(val_len >= max_arg_size)) return PPM_FAILURE_BUFFER_FULL; - } else { - if (fromuser) { - len = (int32_t)ppm_copy_from_user(args->buffer + args->arg_data_offset, - (const void __user *)(unsigned long)val, - val_len); - if (unlikely(len != 0)) { - return PPM_FAILURE_INVALID_USER_MEMORY; - } + if (fromuser) { + len = (int)ppm_copy_from_user(args->buffer + args->arg_data_offset, + (const void __user *)(unsigned long)val, + val_len); - len = val_len; - } else { - memcpy(args->buffer + args->arg_data_offset, - (void *)(unsigned long)val, val_len); + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; - len = val_len; - } + len = val_len; + } else { + memcpy(args->buffer + args->arg_data_offset, + (void *)(unsigned long)val, val_len); + + len = val_len; } } else { /* @@ -227,7 +748,7 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 case PT_FLAGS8: case PT_UINT8: case PT_SIGTYPE: - if (likely(args->arg_data_size >= sizeof(u8))) { + if (likely(max_arg_size >= sizeof(u8))) { *(u8 *)(args->buffer + args->arg_data_offset) = (u8)val; len = sizeof(u8); } else { @@ -238,7 +759,7 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 case PT_FLAGS16: case PT_UINT16: case PT_SYSCALLID: - if (likely(args->arg_data_size >= sizeof(u16))) { + if (likely(max_arg_size >= sizeof(u16))) { *(u16 *)(args->buffer + args->arg_data_offset) = (u16)val; len = sizeof(u16); } else { @@ -248,9 +769,13 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 break; case PT_FLAGS32: case PT_UINT32: - if (likely(args->arg_data_size >= sizeof(uint32_t))) { - *(uint32_t *)(args->buffer + args->arg_data_offset) = (uint32_t)val; - len = sizeof(uint32_t); + case PT_MODE: + case PT_UID: + case PT_GID: + case PT_SIGSET: + if (likely(max_arg_size >= sizeof(u32))) { + *(u32 *)(args->buffer + args->arg_data_offset) = (u32)val; + len = sizeof(u32); } else { return PPM_FAILURE_BUFFER_FULL; } @@ -259,36 +784,36 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 case PT_RELTIME: case PT_ABSTIME: case PT_UINT64: - if (likely(args->arg_data_size >= sizeof(uint64_t))) { - *(uint64_t *)(args->buffer + args->arg_data_offset) = (uint64_t)val; - len = sizeof(uint64_t); + if (likely(max_arg_size >= sizeof(u64))) { + *(u64 *)(args->buffer + args->arg_data_offset) = (u64)val; + len = sizeof(u64); } else { return PPM_FAILURE_BUFFER_FULL; } break; case PT_INT8: - if (likely(args->arg_data_size >= sizeof(int8_t))) { - *(int8_t *)(args->buffer + args->arg_data_offset) = (int8_t)(long)val; - len = sizeof(int8_t); + if (likely(max_arg_size >= sizeof(s8))) { + *(s8 *)(args->buffer + args->arg_data_offset) = (s8)(long)val; + len = sizeof(s8); } else { return PPM_FAILURE_BUFFER_FULL; } break; case PT_INT16: - if (likely(args->arg_data_size >= sizeof(int16_t))) { - *(int16_t *)(args->buffer + args->arg_data_offset) = (int16_t)(long)val; - len = sizeof(int16_t); + if (likely(max_arg_size >= sizeof(s16))) { + *(s16 *)(args->buffer + args->arg_data_offset) = (s16)(long)val; + len = sizeof(s16); } else { return PPM_FAILURE_BUFFER_FULL; } break; case PT_INT32: - if (likely(args->arg_data_size >= sizeof(int32_t))) { - *(int32_t *)(args->buffer + args->arg_data_offset) = (int32_t)(long)val; - len = sizeof(int32_t); + if (likely(max_arg_size >= sizeof(s32))) { + *(s32 *)(args->buffer + args->arg_data_offset) = (s32)(long)val; + len = sizeof(s32); } else { return PPM_FAILURE_BUFFER_FULL; } @@ -298,9 +823,9 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 case PT_ERRNO: case PT_FD: case PT_PID: - if (likely(args->arg_data_size >= sizeof(int64_t))) { - *(int64_t *)(args->buffer + args->arg_data_offset) = (int64_t)(long)val; - len = sizeof(int64_t); + if (likely(max_arg_size >= sizeof(s64))) { + *(s64 *)(args->buffer + args->arg_data_offset) = (s64)(long)val; + len = sizeof(s64); } else { return PPM_FAILURE_BUFFER_FULL; } @@ -308,17 +833,19 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 break; default: ASSERT(0); - pr_info("sysdig-probe: val_to_ring: invalid argument type %d. Event %u (%s) might have less parameters than what has been declared in nparams\n", - (int)g_event_info[args->event_type].params[args->curarg].type, - (uint32_t)args->event_type, - g_event_info[args->event_type].name); +#ifndef UDIG + pr_err("val_to_ring: invalid argument type %d. Event %u (%s) might have less parameters than what has been declared in nparams\n", + (int)g_event_info[args->event_type].params[args->curarg].type, + (u32)args->event_type, + g_event_info[args->event_type].name); +#endif return PPM_FAILURE_BUG; } - ASSERT(len <= 65535); - ASSERT(len <= args->arg_data_size); + ASSERT(len <= PPM_MAX_ARG_SIZE); + ASSERT(len <= max_arg_size); - *psize = (u16)len; + *psize += (u16)len; args->curarg++; args->arg_data_offset += len; args->arg_data_size -= len; @@ -326,130 +853,6 @@ inline int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u1 return PPM_SUCCESS; } -/* - * Get the current working directory for the current process. - * Returns the pointer to the string, which is NOT going to be at the beginning - * of buf. - * Buf must be at least 1 page in size. - */ -char *npm_getcwd(char *buf, unsigned long bufsize) -{ - struct path pwd; - char *res; - - ASSERT(bufsize >= PAGE_SIZE - 1); - -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) - get_fs_pwd(current->fs, &pwd); -#else - read_lock(¤t->fs->lock); - pwd = current->fs->pwd; - path_get(&pwd); - read_unlock(¤t->fs->lock); -#endif - - res = d_path(&pwd, buf, bufsize); - path_put(&pwd); - - return res; -} - -static inline u8 socket_family_to_scap(u8 family) -{ - if (family == AF_INET) { - return PPM_AF_INET; - } else if (family == AF_INET6) { - return PPM_AF_INET6; - } else if (family == AF_UNIX) { - return PPM_AF_UNIX; - } else if (family == AF_NETLINK) { - return PPM_AF_NETLINK; - } else if (family == AF_PACKET) { - return PPM_AF_PACKET; - } else if (family == AF_UNSPEC) { - return PPM_AF_UNSPEC; - } else if (family == AF_AX25) { - return PPM_AF_AX25; - } else if (family == AF_IPX) { - return PPM_AF_IPX; - } else if (family == AF_APPLETALK) { - return PPM_AF_APPLETALK; - } else if (family == AF_NETROM) { - return PPM_AF_NETROM; - } else if (family == AF_BRIDGE) { - return PPM_AF_BRIDGE; - } else if (family == AF_ATMPVC) { - return PPM_AF_ATMPVC; - } else if (family == AF_X25) { - return PPM_AF_X25; - } else if (family == AF_ROSE) { - return PPM_AF_ROSE; - } else if (family == AF_DECnet) { - return PPM_AF_DECnet; - } else if (family == AF_NETBEUI) { - return PPM_AF_NETBEUI; - } else if (family == AF_SECURITY) { - return PPM_AF_SECURITY; - } else if (family == AF_KEY) { - return PPM_AF_KEY; - } else if (family == AF_ROUTE) { - return PPM_AF_ROUTE; - } else if (family == AF_ASH) { - return PPM_AF_ASH; - } else if (family == AF_ECONET) { - return PPM_AF_ECONET; - } else if (family == AF_ATMSVC) { - return PPM_AF_ATMSVC; - } else if (family == AF_RDS) { - return PPM_AF_RDS; - } else if (family == AF_SNA) { - return PPM_AF_SNA; - } else if (family == AF_IRDA) { - return PPM_AF_IRDA; - } else if (family == AF_PPPOX) { - return PPM_AF_PPPOX; - } else if (family == AF_WANPIPE) { - return PPM_AF_WANPIPE; - } else if (family == AF_LLC) { - return PPM_AF_LLC; - } else if (family == AF_CAN) { - return PPM_AF_CAN; - } else if (family == AF_TIPC) { - return PPM_AF_TIPC; - } else if (family == AF_BLUETOOTH) { - return PPM_AF_BLUETOOTH; - } else if (family == AF_IUCV) { - return PPM_AF_IUCV; - } else if (family == AF_RXRPC) { - return PPM_AF_RXRPC; - } else if (family == AF_ISDN) { - return PPM_AF_ISDN; - } else if (family == AF_PHONET) { - return PPM_AF_PHONET; - } else if (family == AF_IEEE802154) { - return PPM_AF_IEEE802154; - } -#ifdef AF_CAIF - else if (family == AF_CAIF) { - return PPM_AF_CAIF; - } -#endif -#ifdef AF_ALG - else if (family == AF_ALG) { - return PPM_AF_ALG; - } -#endif -#ifdef AF_NFC - else if (family == AF_NFC) { - return PPM_AF_NFC; - } -#endif - else { - ASSERT(false); - return PPM_AF_UNSPEC; - } -} - /* static struct socket *ppm_sockfd_lookup_light(int fd, int *err, int *fput_needed) { @@ -477,7 +880,7 @@ u16 pack_addr(struct sockaddr *usrsockaddr, char *targetbuf, u16 targetbufsize) { - uint32_t ip; + u32 ip; u16 port; sa_family_t family = usrsockaddr->sa_family; struct sockaddr_in *usrsockaddr_in; @@ -505,7 +908,7 @@ u16 pack_addr(struct sockaddr *usrsockaddr, size = 1 + 4 + 2; /* family + ip + port */ *targetbuf = socket_family_to_scap(family); - *(uint32_t *)(targetbuf + 1) = ip; + *(u32 *)(targetbuf + 1) = ip; *(u16 *)(targetbuf + 5) = port; break; @@ -542,11 +945,10 @@ u16 pack_addr(struct sockaddr *usrsockaddr, * Put a 0 at the end of struct sockaddr_un because * the user might not have considered it in the length */ - if (ulen == sizeof(struct sockaddr_storage)) { + if (ulen == sizeof(struct sockaddr_storage)) *(((char *)usrsockaddr_un) + ulen - 1) = 0; - } else { + else *(((char *)usrsockaddr_un) + ulen) = 0; - } /* * Pack the data into the target buffer @@ -588,8 +990,8 @@ u16 fd_to_socktuple(int fd, struct unix_sock *us; char *us_name; struct sock *speer; - uint32_t sip; - uint32_t dip; + u32 sip; + u32 dip; u8 *sip6; u8 *dip6; u16 sport; @@ -601,9 +1003,8 @@ u16 fd_to_socktuple(int fd, char *dest; struct sockaddr_storage sock_address; struct sockaddr_storage peer_address; - int sock_address_len; - int peer_address_len; +#ifndef UDIG /* * Get the socket from the fd * NOTE: sockfd_lookup() locks the socket, so we don't need to worry when we dig in it @@ -615,16 +1016,27 @@ u16 fd_to_socktuple(int fd, * This usually happens if the call failed without being able to establish a connection, * i.e. if it didn't return something like SE_EINPROGRESS. */ - if (sock) { + if (sock) sockfd_put(sock); - } return 0; } +#endif - err = sock->ops->getname(sock, (struct sockaddr *)&sock_address, &sock_address_len, 0); +#ifdef UDIG + socklen_t alen = sizeof(struct sockaddr_storage); + err = udig_getsockname(fd, (struct sockaddr *)&sock_address, &alen); + if(err < 0) + { + return 0; + } + + family = sock_address.ss_family; +#else + err = sock_getname(sock, (struct sockaddr *)&sock_address, 0); ASSERT(err == 0); family = sock->sk->sk_family; +#endif /* * Extract and pack the info, based on the family @@ -632,19 +1044,29 @@ u16 fd_to_socktuple(int fd, switch (family) { case AF_INET: if (!use_userdata) { - err = sock->ops->getname(sock, (struct sockaddr *)&peer_address, &peer_address_len, 1); - ASSERT(err == 0); - - if (is_inbound) { - sip = ((struct sockaddr_in *) &peer_address)->sin_addr.s_addr; - sport = ntohs(((struct sockaddr_in *) &peer_address)->sin_port); - dip = ((struct sockaddr_in *) &sock_address)->sin_addr.s_addr; - dport = ntohs(((struct sockaddr_in *) &sock_address)->sin_port); +#ifdef UDIG + socklen_t palen = sizeof(struct sockaddr_storage); + err = udig_getpeername(fd, (struct sockaddr *)&peer_address, &palen); +#else + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); +#endif + if (err == 0) { + if (is_inbound) { + sip = ((struct sockaddr_in *) &peer_address)->sin_addr.s_addr; + sport = ntohs(((struct sockaddr_in *) &peer_address)->sin_port); + dip = ((struct sockaddr_in *) &sock_address)->sin_addr.s_addr; + dport = ntohs(((struct sockaddr_in *) &sock_address)->sin_port); + } else { + sip = ((struct sockaddr_in *) &sock_address)->sin_addr.s_addr; + sport = ntohs(((struct sockaddr_in *) &sock_address)->sin_port); + dip = ((struct sockaddr_in *) &peer_address)->sin_addr.s_addr; + dport = ntohs(((struct sockaddr_in *) &peer_address)->sin_port); + } } else { - sip = ((struct sockaddr_in *) &sock_address)->sin_addr.s_addr; - sport = ntohs(((struct sockaddr_in *) &sock_address)->sin_port); - dip = ((struct sockaddr_in *) &peer_address)->sin_addr.s_addr; - dport = ntohs(((struct sockaddr_in *) &peer_address)->sin_port); + sip = 0; + sport = 0; + dip = 0; + dport = 0; } } else { /* @@ -671,15 +1093,20 @@ u16 fd_to_socktuple(int fd, size = 1 + 4 + 4 + 2 + 2; /* family + sip + dip + sport + dport */ *targetbuf = socket_family_to_scap(family); - *(uint32_t *)(targetbuf + 1) = sip; + *(u32 *)(targetbuf + 1) = sip; *(u16 *)(targetbuf + 5) = sport; - *(uint32_t *)(targetbuf + 7) = dip; + *(u32 *)(targetbuf + 7) = dip; *(u16 *)(targetbuf + 11) = dport; break; case AF_INET6: if (!use_userdata) { - err = sock->ops->getname(sock, (struct sockaddr *)&peer_address, &peer_address_len, 1); +#ifdef UDIG + socklen_t palen = sizeof(struct sockaddr_storage); + err = udig_getpeername(fd, (struct sockaddr *)&peer_address, &palen); +#else + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); +#endif ASSERT(err == 0); if (is_inbound) { @@ -729,6 +1156,9 @@ u16 fd_to_socktuple(int fd, break; case AF_UNIX: +#ifdef UDIG + size = 0; +#else /* * Retrieve the addresses */ @@ -754,7 +1184,7 @@ u16 fd_to_socktuple(int fd, if (is_inbound) { us_name = ((struct sockaddr_un *) &sock_address)->sun_path; } else { - err = sock->ops->getname(sock, (struct sockaddr *)&peer_address, &peer_address_len, 1); + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); ASSERT(err == 0); us_name = ((struct sockaddr_un *) &peer_address)->sun_path; @@ -769,17 +1199,15 @@ u16 fd_to_socktuple(int fd, * Put a 0 at the end of struct sockaddr_un because * the user might not have considered it in the length */ - if (ulen == sizeof(struct sockaddr_storage)) { + if (ulen == sizeof(struct sockaddr_storage)) *(((char *)usrsockaddr_un) + ulen - 1) = 0; - } else { + else *(((char *)usrsockaddr_un) + ulen) = 0; - } - if (is_inbound) { + if (is_inbound) us_name = ((struct sockaddr_un *) &sock_address)->sun_path; - } else { + else us_name = usrsockaddr_un->sun_path; - } } ASSERT(us_name); @@ -789,33 +1217,33 @@ u16 fd_to_socktuple(int fd, dest[UNIX_PATH_MAX - 1] = 0; size += strlen(dest) + 1; + #endif /* UDIG */ break; default: size = 0; break; } +#ifndef UDIG /* * Digging finished. We can release the fd. */ sockfd_put(sock); +#endif return size; } int addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr) { - if (unlikely(ulen < 0 || ulen > sizeof(struct sockaddr_storage))) { + if (unlikely(ulen < 0 || ulen > sizeof(struct sockaddr_storage))) return -EINVAL; - } - if (unlikely(ulen == 0)) { + if (unlikely(ulen == 0)) return 0; - } - if (unlikely(ppm_copy_from_user(kaddr, uaddr, ulen))) { + if (unlikely(ppm_copy_from_user(kaddr, uaddr, ulen))) return -EFAULT; - } return 0; } @@ -828,65 +1256,275 @@ int32_t parse_readv_writev_bufs(struct event_filler_arguments *args, const struc { int32_t res; const struct iovec *iov; - uint32_t copylen; - uint32_t j; - uint64_t size = 0; + u64 copylen; + u32 j; + u64 size = 0; unsigned long bufsize; char *targetbuf = args->str_storage; + u32 targetbuflen = STR_STORAGE_SIZE; + unsigned long syscall_args[6] = {}; + unsigned long val; + u32 notcopied_len; + size_t tocopy_len; copylen = iovcnt * sizeof(struct iovec); - if (unlikely(copylen >= STR_STORAGE_SIZE)) { + if (unlikely(iovcnt >= 0xffffffff)) return PPM_FAILURE_BUFFER_FULL; - } - if (unlikely(ppm_copy_from_user(targetbuf, iovsrc, copylen))) { + if (unlikely(copylen >= STR_STORAGE_SIZE)) + return PPM_FAILURE_BUFFER_FULL; + + if (unlikely(ppm_copy_from_user(args->str_storage, iovsrc, copylen))) return PPM_FAILURE_INVALID_USER_MEMORY; - } - iov = (const struct iovec *)targetbuf; + iov = (const struct iovec *)(args->str_storage); + + targetbuf += copylen; + targetbuflen -= copylen; /* * Size */ if (flags & PRB_FLAG_PUSH_SIZE) { - for (j = 0; j < iovcnt; j++) { + for (j = 0; j < iovcnt; j++) size += iov[j].iov_len; - } - res = val_to_ring(args, size, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + /* + * Size is the total size of the buffers provided by the user. The number of + * received bytes can be smaller + */ + if ((flags & PRB_FLAG_IS_WRITE) == 0) + if (size > retval) + size = retval; + + res = val_to_ring(args, size, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } } /* * data - * NOTE: for the moment, we limit our data copy to the first buffer. - * We assume that in the vast majority of the cases g_snaplen is much smaller - * than iov[0].iov_len, and therefore we don't bother complicvating the code. */ if (flags & PRB_FLAG_PUSH_DATA) { if (retval > 0 && iovcnt > 0) { - bufsize = min_t(int64_t, retval, (int64_t)iov[0].iov_len); + /* + * Retrieve the FD. It will be used for dynamic snaplen calculation. + */ + if (!args->is_socketcall) { + ppm_syscall_get_arguments(current, args->regs, syscall_args); + val = syscall_args[0]; + } +#ifndef UDIG + else + val = args->socketcall_args[0]; +#endif + args->fd = (int)val; + + /* + * Merge the buffers + */ + bufsize = 0; + + for (j = 0; j < iovcnt; j++) { + if ((flags & PRB_FLAG_IS_WRITE) == 0) { + if (bufsize >= retval) { + ASSERT(bufsize >= retval); + + /* + * Copied all the data even if we haven't reached the + * end of the buffer. + * Copy must stop here. + */ + break; + } + + tocopy_len = min(iov[j].iov_len, (size_t)retval - bufsize); + tocopy_len = min(tocopy_len, (size_t)targetbuflen - bufsize - 1); + } else { + tocopy_len = min(iov[j].iov_len, targetbuflen - bufsize - 1); + } + + notcopied_len = (int)ppm_copy_from_user(targetbuf + bufsize, + iov[j].iov_base, + tocopy_len); + + if (unlikely(notcopied_len != 0)) { + /* + * This means we had a page fault. Skip this event. + */ + return PPM_FAILURE_INVALID_USER_MEMORY; + } + + bufsize += tocopy_len; + + if (tocopy_len != iov[j].iov_len) { + /* + * No space left in the args->str_storage buffer. + * Copy must stop here. + */ + break; + } + } + + args->enforce_snaplen = true; res = val_to_ring(args, - (unsigned long)iov[0].iov_base, - min(bufsize, (unsigned long)g_snaplen), - true); - if (unlikely(res != PPM_SUCCESS)) { + (unsigned long)targetbuf, + bufsize, + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } } else { - res = val_to_ring(args, 0, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; + } + } + + return PPM_SUCCESS; +} + +#ifndef UDIG + +#ifdef CONFIG_COMPAT +/* + * Parses the list of buffers of a xreadv or xwritev call, and pushes the size + * (and optionally the data) to the ring. + */ +int32_t compat_parse_readv_writev_bufs(struct event_filler_arguments *args, const struct compat_iovec __user *iovsrc, unsigned long iovcnt, int64_t retval, int flags) +{ + int32_t res; + const struct compat_iovec *iov; + u64 copylen; + u32 j; + u64 size = 0; + unsigned long bufsize; + char *targetbuf = args->str_storage; + u32 targetbuflen = STR_STORAGE_SIZE; + unsigned long syscall_args[6] = {}; + unsigned long val; + u32 notcopied_len; + compat_size_t tocopy_len; + + copylen = iovcnt * sizeof(struct compat_iovec); + + if (unlikely(iovcnt >= 0xffffffff)) + return PPM_FAILURE_BUFFER_FULL; + + if (unlikely(copylen >= STR_STORAGE_SIZE)) + return PPM_FAILURE_BUFFER_FULL; + + if (unlikely(ppm_copy_from_user(args->str_storage, iovsrc, copylen))) + return PPM_FAILURE_INVALID_USER_MEMORY; + + iov = (const struct compat_iovec *)(args->str_storage); + + targetbuf += copylen; + targetbuflen -= copylen; + + /* + * Size + */ + if (flags & PRB_FLAG_PUSH_SIZE) { + for (j = 0; j < iovcnt; j++) + size += iov[j].iov_len; + + /* + * Size is the total size of the buffers provided by the user. The number of + * received bytes can be smaller + */ + if ((flags & PRB_FLAG_IS_WRITE) == 0) + if (size > retval) + size = retval; + + res = val_to_ring(args, size, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + /* + * data + */ + if (flags & PRB_FLAG_PUSH_DATA) { + if (retval > 0 && iovcnt > 0) { + /* + * Retrieve the FD. It will be used for dynamic snaplen calculation. + */ + if (!args->is_socketcall) { + ppm_syscall_get_arguments(current, args->regs, syscall_args); + val = syscall_args[0]; + } else + val = args->socketcall_args[0]; + args->fd = (int)val; + + /* + * Merge the buffers + */ + bufsize = 0; + + for (j = 0; j < iovcnt; j++) { + if ((flags & PRB_FLAG_IS_WRITE) == 0) { + if (bufsize >= retval) { + ASSERT(bufsize >= retval); + + /* + * Copied all the data even if we haven't reached the + * end of the buffer. + * Copy must stop here. + */ + break; + } + + tocopy_len = min(iov[j].iov_len, (compat_size_t)((size_t)retval - bufsize)); + tocopy_len = min(tocopy_len, (compat_size_t)(targetbuflen - bufsize - 1)); + } else { + tocopy_len = min(iov[j].iov_len, (compat_size_t)(targetbuflen - bufsize - 1)); + } + + notcopied_len = (int)ppm_copy_from_user(targetbuf + bufsize, + compat_ptr(iov[j].iov_base), + tocopy_len); + + if (unlikely(notcopied_len != 0)) { + /* + * This means we had a page fault. Skip this event. + */ + return PPM_FAILURE_INVALID_USER_MEMORY; + } + + bufsize += tocopy_len; + + if (tocopy_len != iov[j].iov_len) { + /* + * No space left in the args->str_storage buffer. + * Copy must stop here. + */ + break; + } } + + args->enforce_snaplen = true; + + res = val_to_ring(args, + (unsigned long)targetbuf, + bufsize, + false, + 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; } } return PPM_SUCCESS; } +#endif /* CONFIG_COMPAT */ +#endif /* UDIG */ /* * STANDARD FILLERS @@ -899,62 +1537,50 @@ int32_t parse_readv_writev_bufs(struct event_filler_arguments *args, const struc * filler function. * The arguments to extract are be specified in g_ppm_events. */ -int32_t f_sys_autofill(struct event_filler_arguments *args, const struct ppm_event_entry *evinfo) +int f_sys_autofill(struct event_filler_arguments *args) { - int32_t res; + int res; + unsigned long syscall_args[6] = {}; unsigned long val; - uint32_t j; + u32 j; int64_t retval; + const struct ppm_event_entry *evinfo = &g_ppm_events[args->event_type]; ASSERT(evinfo->n_autofill_args <= PPM_MAX_AUTOFILL_ARGS); for (j = 0; j < evinfo->n_autofill_args; j++) { if (evinfo->autofill_args[j].id >= 0) { -#ifdef __x86_64__ - /* - * Regular argument - */ - syscall_get_arguments(current, - args->regs, - evinfo->autofill_args[j].id, - 1, - &val); -#else - if (evinfo->paramtype == APT_SOCK) { +#ifdef _HAS_SOCKETCALL + if (args->is_socketcall && evinfo->paramtype == APT_SOCK) { val = args->socketcall_args[evinfo->autofill_args[j].id]; - } else { + } else +#endif + { /* * Regular argument */ - syscall_get_arguments(current, - args->regs, - evinfo->autofill_args[j].id, - 1, - &val); + ppm_syscall_get_arguments(current, args->regs, syscall_args); + val = syscall_args[evinfo->autofill_args[j].id]; } -#endif - res = val_to_ring(args, val, 0, true); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } } else if (evinfo->autofill_args[j].id == AF_ID_RETVAL) { /* * Return value */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } } else if (evinfo->autofill_args[j].id == AF_ID_USEDEFAULT) { /* * Default Value */ - res = val_to_ring(args, evinfo->autofill_args[j].default_val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, evinfo->autofill_args[j].default_val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } } else { ASSERT(false); } diff --git a/driver/ppm_events.h b/driver/ppm_events.h index 9952736ae1..471d9c0961 100644 --- a/driver/ppm_events.h +++ b/driver/ppm_events.h @@ -1,115 +1,125 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #ifndef EVENTS_H_ #define EVENTS_H_ +/* To know about __NR_socketcall */ +#ifndef UDIG +#include +#endif +#ifdef CONFIG_COMPAT +#include +#endif + +#ifdef __NR_socketcall + #define _HAS_SOCKETCALL +#endif +#if defined(CONFIG_X86_64) && defined(CONFIG_IA32_EMULATION) + #define _HAS_SOCKETCALL +#endif + +#include "ppm_events_public.h" + /* * Various crap that a callback might need */ +struct fault_data_t { + unsigned long address; + struct pt_regs *regs; + unsigned long error_code; +}; + struct event_filler_arguments { - char *buffer; /* the buffer that will be filled with the data */ - uint32_t buffer_size; /* the space in the ring buffer available for this event */ - uint32_t syscall_id; /* the system call ID */ + ppm_consumer_t *consumer; + char *buffer; /* the buffer that will be filled with the data */ + u32 buffer_size; /* the space in the ring buffer available for this event */ + u32 syscall_id; /* the system call ID */ + const enum ppm_syscall_code *cur_g_syscall_code_routing_table; #ifdef PPM_ENABLE_SENTINEL - uint32_t sentinel; + u32 sentinel; #endif - uint32_t nevents; - uint32_t curarg; - uint32_t nargs; - uint32_t arg_data_offset; - uint32_t arg_data_size; + u32 nevents; + u32 curarg; + u32 nargs; + u32 arg_data_offset; + u32 arg_data_size; enum ppm_event_type event_type; /* the event type */ + /* Eventually convert this to an event_info union and move all the + * below per-event params in this union, it's not good to waste kernel + * stack since all this stuff is always exclusive + */ +#ifdef UDIG + u64 *regs; /* the registers containing the call arguments */ +#else struct pt_regs *regs; /* the registers containing the call arguments */ - struct task_struct *sched_prev; /* for context switch events, the task that is being schduled out */ - struct task_struct *sched_next; /* for context switch events, the task that is being schduled in */ +#endif + struct task_struct *sched_prev; /* for context switch events, the task that is being scheduled out */ + struct task_struct *sched_next; /* for context switch events, the task that is being scheduled in */ char *str_storage; /* String storage. Size is one page. */ -#ifndef __x86_64__ +#ifndef UDIG unsigned long socketcall_args[6]; #endif -}; - -/* - * Filler table-related definitions - */ -#define PPM_AUTOFILL NULL -#define PPM_MAX_AUTOFILL_ARGS 4 - -/* - * Return codes - */ -#define PPM_SUCCESS 0 -#define PPM_FAILURE_BUFFER_FULL -1 -#define PPM_FAILURE_INVALID_USER_MEMORY -2 -#define PPM_FAILURE_BUG -3 - -typedef int32_t (*filler_callback) (struct event_filler_arguments *args); - -struct ppm_autofill_arg { -#define AF_ID_RETVAL -1 -#define AF_ID_USEDEFAULT -2 - int16_t id; - long default_val; -}; - -enum autofill_paramtype { - APT_REG, - APT_SOCK, -}; - -struct ppm_event_entry { - filler_callback filler_callback; - u16 n_autofill_args; - enum autofill_paramtype paramtype; - struct ppm_autofill_arg autofill_args[PPM_MAX_AUTOFILL_ARGS]; + bool is_socketcall; +#ifndef UDIG + int socketcall_syscall; + bool compat; +#endif + int fd; /* Passed by some of the fillers to val_to_ring to compute the snaplen dynamically */ + bool enforce_snaplen; +#ifndef UDIG + int signo; /* Signal number */ + __kernel_pid_t spid; /* PID of source process */ + __kernel_pid_t dpid; /* PID of destination process */ + struct fault_data_t fault_data; /* For page faults */ +#endif }; extern const struct ppm_event_entry g_ppm_events[]; /* - * parse_readv_writev_bufs flags + * HTTP markers */ -#define PRB_FLAG_PUSH_SIZE 1 -#define PRB_FLAG_PUSH_DATA 2 -#define PRB_FLAG_PUSH_ALL (PRB_FLAG_PUSH_SIZE | PRB_FLAG_PUSH_DATA) +#define HTTP_GET_STR "GET " +#define HTTP_OPTIONS_STR "OPTI" +#define HTTP_HEAD_STR "HEAD" +#define HTTP_POST_STR "POST" +#define HTTP_PUT_STR "PUT " +#define HTTP_DELETE_STR "DELE" +#define HTTP_TRACE_STR "TRAC" +#define HTTP_CONNECT_STR "CONN" +#define HTTP_RESP_STR "HTTP" /* * Functions */ -int32_t f_sys_autofill(struct event_filler_arguments *args, const struct ppm_event_entry *evinfo); -int32_t val_to_ring(struct event_filler_arguments *args, uint64_t val, u16 val_len, bool fromuser); -char *npm_getcwd(char *buf, unsigned long bufsize); +int32_t dpi_lookahead_init(void); +int32_t val_to_ring(struct event_filler_arguments *args, u64 val, u32 val_len, bool fromuser, u8 dyn_idx); u16 pack_addr(struct sockaddr *usrsockaddr, int ulen, char *targetbuf, u16 targetbufsize); u16 fd_to_socktuple(int fd, struct sockaddr *usrsockaddr, int ulen, bool use_userdata, bool is_inbound, char *targetbuf, u16 targetbufsize); int addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr); int32_t parse_readv_writev_bufs(struct event_filler_arguments *args, const struct iovec __user *iovsrc, unsigned long iovcnt, int64_t retval, int flags); -static inline int32_t add_sentinel(struct event_filler_arguments *args) +#ifdef CONFIG_COMPAT +int32_t compat_parse_readv_writev_bufs(struct event_filler_arguments *args, const struct compat_iovec __user *iovsrc, unsigned long iovcnt, int64_t retval, int flags); +#endif + +static inline int add_sentinel(struct event_filler_arguments *args) { #ifdef PPM_ENABLE_SENTINEL - if (likely(args->arg_data_size >= sizeof(uint32_t))) { - *(uint32_t *)(args->buffer + args->arg_data_offset) = args->sentinel; + if (likely(args->arg_data_size >= sizeof(u32))) { + *(u32 *)(args->buffer + args->arg_data_offset) = args->sentinel; args->arg_data_offset += 4; args->arg_data_size -= 4; return PPM_SUCCESS; - } else { - return PPM_FAILURE_BUFFER_FULL; } + return PPM_FAILURE_BUFFER_FULL; #else return PPM_SUCCESS; #endif diff --git a/driver/ppm_events_public.h b/driver/ppm_events_public.h index 7470e5749c..22ff221996 100644 --- a/driver/ppm_events_public.h +++ b/driver/ppm_events_public.h @@ -1,32 +1,38 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #ifndef EVENTS_PUBLIC_H_ #define EVENTS_PUBLIC_H_ +#if defined(__sun) +#include +#endif + #ifdef __KERNEL__ #include +#else +#include "../userspace/common/sysdig_types.h" +#endif + +/* + * Macros for packing in different build environments + */ +#if !defined(CYGWING_AGENT) && (defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)) +#define _packed __pragma(pack(push, 1)); __pragma(pack(pop)) +#else +#define _packed __attribute__((packed)) #endif /* * Limits */ -#define PPM_MAX_EVENT_PARAMS 16 /* Max number of parameters an event can have */ +#define PPM_MAX_EVENT_PARAMS (1 << 5) /* Max number of parameters an event can have */ #define PPM_MAX_PATH_SIZE 256 /* Max size that an event parameter can have in the circular buffer, in bytes */ #define PPM_MAX_NAME_LEN 32 @@ -82,15 +88,43 @@ along with sysdig. If not, see . #define PPM_O_WRONLY (1 << 1) /* Open for writing only */ #define PPM_O_RDWR (PPM_O_RDONLY | PPM_O_WRONLY) /* Open for reading and writing */ #define PPM_O_CREAT (1 << 2) /* Create a new file if it doesn't exist. */ -#define PPM_O_APPEND (1 << 3) /* If set, the file offset shall be set to the end of the file prior to each write. */ +#define PPM_O_APPEND (1 << 3) /* If set, the file offset shall be set to the end of the file prior to each write. */ #define PPM_O_DSYNC (1 << 4) #define PPM_O_EXCL (1 << 5) #define PPM_O_NONBLOCK (1 << 6) #define PPM_O_SYNC (1 << 7) #define PPM_O_TRUNC (1 << 8) -#define PPM_O_DIRECT (1 << 9) +#define PPM_O_DIRECT (1 << 9) #define PPM_O_DIRECTORY (1 << 10) #define PPM_O_LARGEFILE (1 << 11) +#define PPM_O_CLOEXEC (1 << 12) +#define PPM_O_TMPFILE (1 << 13) + +/* + * File modes + */ +#define PPM_S_NONE 0 +#define PPM_S_IXOTH (1 << 0) +#define PPM_S_IWOTH (1 << 1) +#define PPM_S_IROTH (1 << 2) +#define PPM_S_IXGRP (1 << 3) +#define PPM_S_IWGRP (1 << 4) +#define PPM_S_IRGRP (1 << 5) +#define PPM_S_IXUSR (1 << 6) +#define PPM_S_IWUSR (1 << 7) +#define PPM_S_IRUSR (1 << 8) +#define PPM_S_ISVTX (1 << 9) +#define PPM_S_ISGID (1 << 10) +#define PPM_S_ISUID (1 << 11) + +/* + * flock() flags + */ +#define PPM_LOCK_NONE 0 +#define PPM_LOCK_SH (1 << 0) +#define PPM_LOCK_EX (1 << 1) +#define PPM_LOCK_NB (1 << 2) +#define PPM_LOCK_UN (1 << 3) /* * Clone flags @@ -117,6 +151,21 @@ along with sysdig. If not, see . #define PPM_CL_NAME_CHANGED (1 << 17) /* libsinsp-specific flag. Set when the thread name changes */ /* (for example because execve was called) */ #define PPM_CL_CLOSED (1 << 18) /* thread has been closed. */ +#define PPM_CL_ACTIVE (1 << 19) /* libsinsp-specific flag. Set in the first non-clone event for + this thread. */ +#define PPM_CL_CLONE_NEWUSER (1 << 20) +#define PPM_CL_PIPE_SRC (1 << 21) /* libsinsp-specific flag. Set if this thread has been + detected to be the source in a shell pipe. */ +#define PPM_CL_PIPE_DST (1 << 22) /* libsinsp-specific flag. Set if this thread has been + detected to be the destination in a shell pipe. */ +#define PPM_CL_CLONE_CHILD_CLEARTID (1 << 23) +#define PPM_CL_CLONE_CHILD_SETTID (1 << 24) +#define PPM_CL_CLONE_SETTLS (1 << 25) +#define PPM_CL_CLONE_STOPPED (1 << 26) +#define PPM_CL_CLONE_VFORK (1 << 27) +#define PPM_CL_CLONE_NEWCGROUP (1 << 28) +#define PPM_CL_CHILD_IN_PIDNS (1<<29) /* true if the thread created by clone() is *not* + in the init pid namespace */ /* * Futex Operations @@ -159,6 +208,48 @@ along with sysdig. If not, see . #define PPM_POLLWRNORM (1 << 9) #define PPM_POLLWRBAND (1 << 10) +/* + * mount() flags + */ +#define PPM_MS_RDONLY (1<<0) +#define PPM_MS_NOSUID (1<<1) +#define PPM_MS_NODEV (1<<2) +#define PPM_MS_NOEXEC (1<<3) +#define PPM_MS_SYNCHRONOUS (1<<4) +#define PPM_MS_REMOUNT (1<<5) +#define PPM_MS_MANDLOCK (1<<6) +#define PPM_MS_DIRSYNC (1<<7) + +#define PPM_MS_NOATIME (1<<10) +#define PPM_MS_NODIRATIME (1<<11) +#define PPM_MS_BIND (1<<12) +#define PPM_MS_MOVE (1<<13) +#define PPM_MS_REC (1<<14) +#define PPM_MS_SILENT (1<<15) +#define PPM_MS_POSIXACL (1<<16) +#define PPM_MS_UNBINDABLE (1<<17) +#define PPM_MS_PRIVATE (1<<18) +#define PPM_MS_SLAVE (1<<19) +#define PPM_MS_SHARED (1<<20) +#define PPM_MS_RELATIME (1<<21) +#define PPM_MS_KERNMOUNT (1<<22) +#define PPM_MS_I_VERSION (1<<23) +#define PPM_MS_STRICTATIME (1<<24) +#define PPM_MS_LAZYTIME (1<<25) + +#define PPM_MS_NOSEC (1<<28) +#define PPM_MS_BORN (1<<29) +#define PPM_MS_ACTIVE (1<<30) +#define PPM_MS_NOUSER (1<<31) + +/* + * umount() flags + */ +#define PPM_MNT_FORCE 1 +#define PPM_MNT_DETACH 2 +#define PPM_MNT_EXPIRE 4 +#define PPM_UMOUNT_NOFOLLOW 8 + /* * shutdown() how */ @@ -167,10 +258,21 @@ along with sysdig. If not, see . #define PPM_SHUT_RDWR 2 /* - * openat() flags + * fs *at() flags */ #define PPM_AT_FDCWD -100 +/* + * unlinkat() flags + */ +#define PPM_AT_REMOVEDIR 0x200 + +/* + * linkat() flags + */ +#define PPM_AT_SYMLINK_FOLLOW 0x400 +#define PPM_AT_EMPTY_PATH 0x1000 + /* * rlimit resources */ @@ -208,9 +310,11 @@ along with sysdig. If not, see . #define PPM_FCNTL_F_GETOWN 12 #define PPM_FCNTL_F_SETSIG 13 #define PPM_FCNTL_F_GETSIG 15 +#ifndef CONFIG_64BIT #define PPM_FCNTL_F_GETLK64 17 #define PPM_FCNTL_F_SETLK64 18 #define PPM_FCNTL_F_SETLKW64 19 +#endif #define PPM_FCNTL_F_SETOWN_EX 21 #define PPM_FCNTL_F_GETOWN_EX 22 #define PPM_FCNTL_F_SETLEASE 23 @@ -220,6 +324,274 @@ along with sysdig. If not, see . #define PPM_FCNTL_F_NOTIFY 27 #define PPM_FCNTL_F_SETPIPE_SZ 28 #define PPM_FCNTL_F_GETPIPE_SZ 29 +#define PPM_FCNTL_F_OFD_GETLK 30 +#define PPM_FCNTL_F_OFD_SETLK 31 +#define PPM_FCNTL_F_OFD_SETLKW 32 + +/* + * getsockopt/setsockopt levels + */ +#define PPM_SOCKOPT_LEVEL_UNKNOWN 0 +#define PPM_SOCKOPT_LEVEL_SOL_SOCKET 1 +#define PPM_SOCKOPT_LEVEL_SOL_TCP 2 + +/* + * getsockopt/setsockopt options + * SOL_SOCKET only currently + */ +#define PPM_SOCKOPT_UNKNOWN 0 +#define PPM_SOCKOPT_SO_DEBUG 1 +#define PPM_SOCKOPT_SO_REUSEADDR 2 +#define PPM_SOCKOPT_SO_TYPE 3 +#define PPM_SOCKOPT_SO_ERROR 4 +#define PPM_SOCKOPT_SO_DONTROUTE 5 +#define PPM_SOCKOPT_SO_BROADCAST 6 +#define PPM_SOCKOPT_SO_SNDBUF 7 +#define PPM_SOCKOPT_SO_RCVBUF 8 +#define PPM_SOCKOPT_SO_SNDBUFFORCE 32 +#define PPM_SOCKOPT_SO_RCVBUFFORCE 33 +#define PPM_SOCKOPT_SO_KEEPALIVE 9 +#define PPM_SOCKOPT_SO_OOBINLINE 10 +#define PPM_SOCKOPT_SO_NO_CHECK 11 +#define PPM_SOCKOPT_SO_PRIORITY 12 +#define PPM_SOCKOPT_SO_LINGER 13 +#define PPM_SOCKOPT_SO_BSDCOMPAT 14 +#define PPM_SOCKOPT_SO_REUSEPORT 15 +#define PPM_SOCKOPT_SO_PASSCRED 16 +#define PPM_SOCKOPT_SO_PEERCRED 17 +#define PPM_SOCKOPT_SO_RCVLOWAT 18 +#define PPM_SOCKOPT_SO_SNDLOWAT 19 +#define PPM_SOCKOPT_SO_RCVTIMEO 20 +#define PPM_SOCKOPT_SO_SNDTIMEO 21 +#define PPM_SOCKOPT_SO_SECURITY_AUTHENTICATION 22 +#define PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_TRANSPORT 23 +#define PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_NETWORK 24 +#define PPM_SOCKOPT_SO_BINDTODEVICE 25 +#define PPM_SOCKOPT_SO_ATTACH_FILTER 26 +#define PPM_SOCKOPT_SO_DETACH_FILTER 27 +#define PPM_SOCKOPT_SO_PEERNAME 28 +#define PPM_SOCKOPT_SO_TIMESTAMP 29 +#define PPM_SOCKOPT_SO_ACCEPTCONN 30 +#define PPM_SOCKOPT_SO_PEERSEC 31 +#define PPM_SOCKOPT_SO_PASSSEC 34 +#define PPM_SOCKOPT_SO_TIMESTAMPNS 35 +#define PPM_SOCKOPT_SO_MARK 36 +#define PPM_SOCKOPT_SO_TIMESTAMPING 37 +#define PPM_SOCKOPT_SO_PROTOCOL 38 +#define PPM_SOCKOPT_SO_DOMAIN 39 +#define PPM_SOCKOPT_SO_RXQ_OVFL 40 +#define PPM_SOCKOPT_SO_WIFI_STATUS 41 +#define PPM_SOCKOPT_SO_PEEK_OFF 42 +#define PPM_SOCKOPT_SO_NOFCS 43 +#define PPM_SOCKOPT_SO_LOCK_FILTER 44 +#define PPM_SOCKOPT_SO_SELECT_ERR_QUEUE 45 +#define PPM_SOCKOPT_SO_BUSY_POLL 46 +#define PPM_SOCKOPT_SO_MAX_PACING_RATE 47 +#define PPM_SOCKOPT_SO_BPF_EXTENSIONS 48 +#define PPM_SOCKOPT_SO_INCOMING_CPU 49 +#define PPM_SOCKOPT_SO_ATTACH_BPF 50 +#define PPM_SOCKOPT_SO_PEERGROUPS 51 +#define PPM_SOCKOPT_SO_MEMINFO 52 +#define PPM_SOCKOPT_SO_COOKIE 53 + +/* + * getsockopt/setsockopt dynamic params + */ +#define PPM_SOCKOPT_IDX_UNKNOWN 0 +#define PPM_SOCKOPT_IDX_ERRNO 1 +#define PPM_SOCKOPT_IDX_UINT32 2 +#define PPM_SOCKOPT_IDX_UINT64 3 +#define PPM_SOCKOPT_IDX_TIMEVAL 4 +#define PPM_SOCKOPT_IDX_MAX 5 + + /* + * ptrace requests + */ +#define PPM_PTRACE_UNKNOWN 0 +#define PPM_PTRACE_TRACEME 1 +#define PPM_PTRACE_PEEKTEXT 2 +#define PPM_PTRACE_PEEKDATA 3 +#define PPM_PTRACE_PEEKUSR 4 +#define PPM_PTRACE_POKETEXT 5 +#define PPM_PTRACE_POKEDATA 6 +#define PPM_PTRACE_POKEUSR 7 +#define PPM_PTRACE_CONT 8 +#define PPM_PTRACE_KILL 9 +#define PPM_PTRACE_SINGLESTEP 10 +#define PPM_PTRACE_ATTACH 11 +#define PPM_PTRACE_DETACH 12 +#define PPM_PTRACE_SYSCALL 13 +#define PPM_PTRACE_SETOPTIONS 14 +#define PPM_PTRACE_GETEVENTMSG 15 +#define PPM_PTRACE_GETSIGINFO 16 +#define PPM_PTRACE_SETSIGINFO 17 +#define PPM_PTRACE_GETREGSET 18 +#define PPM_PTRACE_SETREGSET 19 +#define PPM_PTRACE_SEIZE 20 +#define PPM_PTRACE_INTERRUPT 21 +#define PPM_PTRACE_LISTEN 22 +#define PPM_PTRACE_PEEKSIGINFO 23 +#define PPM_PTRACE_GETSIGMASK 24 +#define PPM_PTRACE_SETSIGMASK 25 +#define PPM_PTRACE_GETREGS 26 +#define PPM_PTRACE_SETREGS 27 +#define PPM_PTRACE_GETFPREGS 28 +#define PPM_PTRACE_SETFPREGS 29 +#define PPM_PTRACE_GETFPXREGS 30 +#define PPM_PTRACE_SETFPXREGS 31 +#define PPM_PTRACE_OLDSETOPTIONS 32 +#define PPM_PTRACE_GET_THREAD_AREA 33 +#define PPM_PTRACE_SET_THREAD_AREA 34 +#define PPM_PTRACE_ARCH_PRCTL 35 +#define PPM_PTRACE_SYSEMU 36 +#define PPM_PTRACE_SYSEMU_SINGLESTEP 37 +#define PPM_PTRACE_SINGLEBLOCK 38 + +/* + * ptrace dynamic table indexes + */ +#define PPM_PTRACE_IDX_UINT64 0 +#define PPM_PTRACE_IDX_SIGTYPE 1 + +#define PPM_PTRACE_IDX_MAX 2 + +#define PPM_BPF_IDX_FD 0 +#define PPM_BPF_IDX_RES 1 + +#define PPM_BPF_IDX_MAX 2 + +/* + * memory protection flags + */ +#define PPM_PROT_NONE 0 +#define PPM_PROT_READ (1 << 0) +#define PPM_PROT_WRITE (1 << 1) +#define PPM_PROT_EXEC (1 << 2) +#define PPM_PROT_SEM (1 << 3) +#define PPM_PROT_GROWSDOWN (1 << 4) +#define PPM_PROT_GROWSUP (1 << 5) +#define PPM_PROT_SAO (1 << 6) + +/* + * mmap flags + */ +#define PPM_MAP_SHARED (1 << 0) +#define PPM_MAP_PRIVATE (1 << 1) +#define PPM_MAP_FIXED (1 << 2) +#define PPM_MAP_ANONYMOUS (1 << 3) +#define PPM_MAP_32BIT (1 << 4) +#define PPM_MAP_RENAME (1 << 5) +#define PPM_MAP_NORESERVE (1 << 6) +#define PPM_MAP_POPULATE (1 << 7) +#define PPM_MAP_NONBLOCK (1 << 8) +#define PPM_MAP_GROWSDOWN (1 << 9) +#define PPM_MAP_DENYWRITE (1 << 10) +#define PPM_MAP_EXECUTABLE (1 << 11) +#define PPM_MAP_INHERIT (1 << 12) +#define PPM_MAP_FILE (1 << 13) +#define PPM_MAP_LOCKED (1 << 14) + +/* + * splice flags + */ +#define PPM_SPLICE_F_MOVE (1 << 0) +#define PPM_SPLICE_F_NONBLOCK (1 << 1) +#define PPM_SPLICE_F_MORE (1 << 2) +#define PPM_SPLICE_F_GIFT (1 << 3) + +/* + * quotactl cmds + */ +#define PPM_Q_QUOTAON (1 << 0) +#define PPM_Q_QUOTAOFF (1 << 1) +#define PPM_Q_GETFMT (1 << 2) +#define PPM_Q_GETINFO (1 << 3) +#define PPM_Q_SETINFO (1 << 4) +#define PPM_Q_GETQUOTA (1 << 5) +#define PPM_Q_SETQUOTA (1 << 6) +#define PPM_Q_SYNC (1 << 7) +#define PPM_Q_XQUOTAON (1 << 8) +#define PPM_Q_XQUOTAOFF (1 << 9) +#define PPM_Q_XGETQUOTA (1 << 10) +#define PPM_Q_XSETQLIM (1 << 11) +#define PPM_Q_XGETQSTAT (1 << 12) +#define PPM_Q_XQUOTARM (1 << 13) +#define PPM_Q_XQUOTASYNC (1 << 14) +#define PPM_Q_XGETQSTATV (1 << 15) + +/* + * quotactl types + */ +#define PPM_USRQUOTA (1 << 0) +#define PPM_GRPQUOTA (1 << 1) + +/* + * quotactl dqi_flags + */ +#define PPM_DQF_NONE (1 << 0) +#define PPM_V1_DQF_RSQUASH (1 << 1) + +/* + * quotactl quotafmts + */ +#define PPM_QFMT_NOT_USED (1 << 0) +#define PPM_QFMT_VFS_OLD (1 << 1) +#define PPM_QFMT_VFS_V0 (1 << 2) +#define PPM_QFMT_VFS_V1 (1 << 3) + +/* + * Semop flags + */ +#define PPM_IPC_NOWAIT (1 << 0) +#define PPM_SEM_UNDO (1 << 1) + +/* + * Semget flags + */ +#define PPM_IPC_CREAT (1 << 13) +#define PPM_IPC_EXCL (1 << 14) + +#define PPM_IPC_STAT (1 << 0) +#define PPM_IPC_SET (1 << 1) +#define PPM_IPC_RMID (1 << 2) +#define PPM_IPC_INFO (1 << 3) +#define PPM_SEM_INFO (1 << 4) +#define PPM_SEM_STAT (1 << 5) +#define PPM_GETALL (1 << 6) +#define PPM_GETNCNT (1 << 7) +#define PPM_GETPID (1 << 8) +#define PPM_GETVAL (1 << 9) +#define PPM_GETZCNT (1 << 10) +#define PPM_SETALL (1 << 11) +#define PPM_SETVAL (1 << 12) + +/* + * Access flags + */ +#define PPM_F_OK (0) +#define PPM_X_OK (1 << 0) +#define PPM_W_OK (1 << 1) +#define PPM_R_OK (1 << 2) + +/* + * Page fault flags + */ +#define PPM_PF_PROTECTION_VIOLATION (1 << 0) +#define PPM_PF_PAGE_NOT_PRESENT (1 << 1) +#define PPM_PF_WRITE_ACCESS (1 << 2) +#define PPM_PF_READ_ACCESS (1 << 3) +#define PPM_PF_USER_FAULT (1 << 4) +#define PPM_PF_SUPERVISOR_FAULT (1 << 5) +#define PPM_PF_RESERVED_PAGE (1 << 6) +#define PPM_PF_INSTRUCTION_FETCH (1 << 7) + + +/* + * Rename flags + */ +#define PPM_RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */ +#define PPM_RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */ +#define PPM_RENAME_WHITEOUT (1 << 2) /* Whiteout source */ /* * SuS says limits have to be unsigned. @@ -238,7 +610,6 @@ along with sysdig. If not, see . # define _STK_LIM_MAX RLIM_INFINITY #endif - /* * The list of event types * Enter events have even numbers while exit events have odd numbers. @@ -250,6 +621,17 @@ along with sysdig. If not, see . #define PPME_IS_EXIT(x) (x & PPME_DIRECTION_FLAG) #define PPME_MAKE_ENTER(x) (x & (~1)) +/* + * Event category to classify events in generic categories + */ +enum ppm_capture_category { + PPMC_NONE = 0, + PPMC_SYSCALL = 1, + PPMC_CONTEXT_SWITCH = 2, + PPMC_SIGNAL = 3, + PPMC_PAGE_FAULT = 4, +}; + /** @defgroup etypes Event Types * @{ */ @@ -264,12 +646,12 @@ enum ppm_event_type { PPME_SYSCALL_READ_X = 7, PPME_SYSCALL_WRITE_E = 8, PPME_SYSCALL_WRITE_X = 9, - PPME_SYSCALL_BRK_E = 10, - PPME_SYSCALL_BRK_X = 11, - PPME_SYSCALL_EXECVE_E = 12, - PPME_SYSCALL_EXECVE_X = 13, - PPME_CLONE_E = 14, - PPME_CLONE_X = 15, + PPME_SYSCALL_BRK_1_E = 10, + PPME_SYSCALL_BRK_1_X = 11, + PPME_SYSCALL_EXECVE_8_E = 12, + PPME_SYSCALL_EXECVE_8_X = 13, + PPME_SYSCALL_CLONE_11_E = 14, + PPME_SYSCALL_CLONE_11_X = 15, PPME_PROCEXIT_E = 16, PPME_PROCEXIT_X = 17, /* This should never be called */ PPME_SOCKET_SOCKET_E = 18, @@ -344,14 +726,15 @@ enum ppm_event_type { PPME_SYSCALL_LSEEK_X = 87, PPME_SYSCALL_LLSEEK_E = 88, PPME_SYSCALL_LLSEEK_X = 89, - PPME_SYSCALL_IOCTL_E = 90, - PPME_SYSCALL_IOCTL_X = 91, + PPME_SYSCALL_IOCTL_2_E = 90, + PPME_SYSCALL_IOCTL_2_X = 91, PPME_SYSCALL_GETCWD_E = 92, PPME_SYSCALL_GETCWD_X = 93, PPME_SYSCALL_CHDIR_E = 94, PPME_SYSCALL_CHDIR_X = 95, PPME_SYSCALL_FCHDIR_E = 96, PPME_SYSCALL_FCHDIR_X = 97, + /* mkdir/rmdir events are not emitted anymore */ PPME_SYSCALL_MKDIR_E = 98, PPME_SYSCALL_MKDIR_X = 99, PPME_SYSCALL_RMDIR_E = 100, @@ -400,15 +783,181 @@ enum ppm_event_type { PPME_SYSCALL_SETRLIMIT_X = 143, PPME_SYSCALL_PRLIMIT_E = 144, PPME_SYSCALL_PRLIMIT_X = 145, - PPME_SCHEDSWITCH_E = 146, - PPME_SCHEDSWITCH_X = 147, /* This should never be called */ + PPME_SCHEDSWITCH_1_E = 146, + PPME_SCHEDSWITCH_1_X = 147, /* This should never be called */ PPME_DROP_E = 148, /* For internal use */ PPME_DROP_X = 149, /* For internal use */ PPME_SYSCALL_FCNTL_E = 150, /* For internal use */ PPME_SYSCALL_FCNTL_X = 151, /* For internal use */ - PPME_SCHEDSWITCHEX_E = 152, - PPME_SCHEDSWITCHEX_X = 153, /* This should never be called */ - PPM_EVENT_MAX = 154, + PPME_SCHEDSWITCH_6_E = 152, + PPME_SCHEDSWITCH_6_X = 153, /* This should never be called */ + PPME_SYSCALL_EXECVE_13_E = 154, + PPME_SYSCALL_EXECVE_13_X = 155, + PPME_SYSCALL_CLONE_16_E = 156, + PPME_SYSCALL_CLONE_16_X = 157, + PPME_SYSCALL_BRK_4_E = 158, + PPME_SYSCALL_BRK_4_X = 159, + PPME_SYSCALL_MMAP_E = 160, + PPME_SYSCALL_MMAP_X = 161, + PPME_SYSCALL_MMAP2_E = 162, + PPME_SYSCALL_MMAP2_X = 163, + PPME_SYSCALL_MUNMAP_E = 164, + PPME_SYSCALL_MUNMAP_X = 165, + PPME_SYSCALL_SPLICE_E = 166, + PPME_SYSCALL_SPLICE_X = 167, + PPME_SYSCALL_PTRACE_E = 168, + PPME_SYSCALL_PTRACE_X = 169, + PPME_SYSCALL_IOCTL_3_E = 170, + PPME_SYSCALL_IOCTL_3_X = 171, + PPME_SYSCALL_EXECVE_14_E = 172, + PPME_SYSCALL_EXECVE_14_X = 173, + PPME_SYSCALL_RENAME_E = 174, + PPME_SYSCALL_RENAME_X = 175, + PPME_SYSCALL_RENAMEAT_E = 176, + PPME_SYSCALL_RENAMEAT_X = 177, + PPME_SYSCALL_SYMLINK_E = 178, + PPME_SYSCALL_SYMLINK_X = 179, + PPME_SYSCALL_SYMLINKAT_E = 180, + PPME_SYSCALL_SYMLINKAT_X = 181, + PPME_SYSCALL_FORK_E = 182, + PPME_SYSCALL_FORK_X = 183, + PPME_SYSCALL_VFORK_E = 184, + PPME_SYSCALL_VFORK_X = 185, + PPME_PROCEXIT_1_E = 186, + PPME_PROCEXIT_1_X = 187, /* This should never be called */ + PPME_SYSCALL_SENDFILE_E = 188, + PPME_SYSCALL_SENDFILE_X = 189, /* This should never be called */ + PPME_SYSCALL_QUOTACTL_E = 190, + PPME_SYSCALL_QUOTACTL_X = 191, + PPME_SYSCALL_SETRESUID_E = 192, + PPME_SYSCALL_SETRESUID_X = 193, + PPME_SYSCALL_SETRESGID_E = 194, + PPME_SYSCALL_SETRESGID_X = 195, + PPME_SYSDIGEVENT_E = 196, + PPME_SYSDIGEVENT_X = 197, /* This should never be called */ + PPME_SYSCALL_SETUID_E = 198, + PPME_SYSCALL_SETUID_X = 199, + PPME_SYSCALL_SETGID_E = 200, + PPME_SYSCALL_SETGID_X = 201, + PPME_SYSCALL_GETUID_E = 202, + PPME_SYSCALL_GETUID_X = 203, + PPME_SYSCALL_GETEUID_E = 204, + PPME_SYSCALL_GETEUID_X = 205, + PPME_SYSCALL_GETGID_E = 206, + PPME_SYSCALL_GETGID_X = 207, + PPME_SYSCALL_GETEGID_E = 208, + PPME_SYSCALL_GETEGID_X = 209, + PPME_SYSCALL_GETRESUID_E = 210, + PPME_SYSCALL_GETRESUID_X = 211, + PPME_SYSCALL_GETRESGID_E = 212, + PPME_SYSCALL_GETRESGID_X = 213, + PPME_SYSCALL_EXECVE_15_E = 214, + PPME_SYSCALL_EXECVE_15_X = 215, + PPME_SYSCALL_CLONE_17_E = 216, + PPME_SYSCALL_CLONE_17_X = 217, + PPME_SYSCALL_FORK_17_E = 218, + PPME_SYSCALL_FORK_17_X = 219, + PPME_SYSCALL_VFORK_17_E = 220, + PPME_SYSCALL_VFORK_17_X = 221, + PPME_SYSCALL_CLONE_20_E = 222, + PPME_SYSCALL_CLONE_20_X = 223, + PPME_SYSCALL_FORK_20_E = 224, + PPME_SYSCALL_FORK_20_X = 225, + PPME_SYSCALL_VFORK_20_E = 226, + PPME_SYSCALL_VFORK_20_X = 227, + PPME_CONTAINER_E = 228, + PPME_CONTAINER_X = 229, + PPME_SYSCALL_EXECVE_16_E = 230, + PPME_SYSCALL_EXECVE_16_X = 231, + PPME_SIGNALDELIVER_E = 232, + PPME_SIGNALDELIVER_X = 233, /* This should never be called */ + PPME_PROCINFO_E = 234, + PPME_PROCINFO_X = 235, /* This should never be called */ + PPME_SYSCALL_GETDENTS_E = 236, + PPME_SYSCALL_GETDENTS_X = 237, + PPME_SYSCALL_GETDENTS64_E = 238, + PPME_SYSCALL_GETDENTS64_X = 239, + PPME_SYSCALL_SETNS_E = 240, + PPME_SYSCALL_SETNS_X = 241, + PPME_SYSCALL_FLOCK_E = 242, + PPME_SYSCALL_FLOCK_X = 243, + PPME_CPU_HOTPLUG_E = 244, + PPME_CPU_HOTPLUG_X = 245, /* This should never be called */ + PPME_SOCKET_ACCEPT_5_E = 246, + PPME_SOCKET_ACCEPT_5_X = 247, + PPME_SOCKET_ACCEPT4_5_E = 248, + PPME_SOCKET_ACCEPT4_5_X = 249, + PPME_SYSCALL_SEMOP_E = 250, + PPME_SYSCALL_SEMOP_X = 251, + PPME_SYSCALL_SEMCTL_E = 252, + PPME_SYSCALL_SEMCTL_X = 253, + PPME_SYSCALL_PPOLL_E = 254, + PPME_SYSCALL_PPOLL_X = 255, + PPME_SYSCALL_MOUNT_E = 256, + PPME_SYSCALL_MOUNT_X = 257, + PPME_SYSCALL_UMOUNT_E = 258, + PPME_SYSCALL_UMOUNT_X = 259, + PPME_K8S_E = 260, + PPME_K8S_X = 261, + PPME_SYSCALL_SEMGET_E = 262, + PPME_SYSCALL_SEMGET_X = 263, + PPME_SYSCALL_ACCESS_E = 264, + PPME_SYSCALL_ACCESS_X = 265, + PPME_SYSCALL_CHROOT_E = 266, + PPME_SYSCALL_CHROOT_X = 267, + PPME_TRACER_E = 268, + PPME_TRACER_X = 269, + PPME_MESOS_E = 270, + PPME_MESOS_X = 271, + PPME_CONTAINER_JSON_E = 272, + PPME_CONTAINER_JSON_X = 273, + PPME_SYSCALL_SETSID_E = 274, + PPME_SYSCALL_SETSID_X = 275, + PPME_SYSCALL_MKDIR_2_E = 276, + PPME_SYSCALL_MKDIR_2_X = 277, + PPME_SYSCALL_RMDIR_2_E = 278, + PPME_SYSCALL_RMDIR_2_X = 279, + PPME_NOTIFICATION_E = 280, + PPME_NOTIFICATION_X = 281, + PPME_SYSCALL_EXECVE_17_E = 282, + PPME_SYSCALL_EXECVE_17_X = 283, + PPME_SYSCALL_UNSHARE_E = 284, + PPME_SYSCALL_UNSHARE_X = 285, + PPME_INFRASTRUCTURE_EVENT_E = 286, + PPME_INFRASTRUCTURE_EVENT_X = 287, + PPME_SYSCALL_EXECVE_18_E = 288, + PPME_SYSCALL_EXECVE_18_X = 289, + PPME_PAGE_FAULT_E = 290, + PPME_PAGE_FAULT_X = 291, + PPME_SYSCALL_EXECVE_19_E = 292, + PPME_SYSCALL_EXECVE_19_X = 293, + PPME_SYSCALL_SETPGID_E = 294, + PPME_SYSCALL_SETPGID_X = 295, + PPME_SYSCALL_BPF_E = 296, + PPME_SYSCALL_BPF_X = 297, + PPME_SYSCALL_SECCOMP_E = 298, + PPME_SYSCALL_SECCOMP_X = 299, + PPME_SYSCALL_UNLINK_2_E = 300, + PPME_SYSCALL_UNLINK_2_X = 301, + PPME_SYSCALL_UNLINKAT_2_E = 302, + PPME_SYSCALL_UNLINKAT_2_X = 303, + PPME_SYSCALL_MKDIRAT_E = 304, + PPME_SYSCALL_MKDIRAT_X = 305, + PPME_SYSCALL_OPENAT_2_E = 306, + PPME_SYSCALL_OPENAT_2_X = 307, + PPME_SYSCALL_LINK_2_E = 308, + PPME_SYSCALL_LINK_2_X = 309, + PPME_SYSCALL_LINKAT_2_E = 310, + PPME_SYSCALL_LINKAT_2_X = 311, + PPME_SYSCALL_FCHMODAT_E = 312, + PPME_SYSCALL_FCHMODAT_X = 313, + PPME_SYSCALL_CHMOD_E = 314, + PPME_SYSCALL_CHMOD_X = 315, + PPME_SYSCALL_FCHMOD_E = 316, + PPME_SYSCALL_FCHMOD_X = 317, + PPME_SYSCALL_RENAMEAT2_E = 318, + PPME_SYSCALL_RENAMEAT2_X = 319, + PPM_EVENT_MAX = 320 }; /*@}*/ @@ -598,7 +1147,7 @@ enum ppm_syscall_code { PPM_SC_TGKILL = 179, PPM_SC_UTIMES = 180, PPM_SC_MQ_OPEN = 181, - PPM_SC_MQ_UNLINK = 18, + PPM_SC_MQ_UNLINK = 182, PPM_SC_MQ_TIMEDSEND = 183, PPM_SC_MQ_TIMEDRECEIVE = 184, PPM_SC_MQ_NOTIFY = 185, @@ -712,7 +1261,32 @@ enum ppm_syscall_code { PPM_SC_WAITPID = 293, PPM_SC_PREAD64 = 294, PPM_SC_PWRITE64 = 295, - PPM_SC_MAX = 296, + PPM_SC_ARCH_PRCTL = 296, + PPM_SC_SHMAT = 297, + PPM_SC_SIGRETURN = 298, + PPM_SC_FALLOCATE = 299, + PPM_SC_NEWFSSTAT = 300, + PPM_SC_PROCESS_VM_READV = 301, + PPM_SC_PROCESS_VM_WRITEV = 302, + PPM_SC_FORK = 303, + PPM_SC_VFORK = 304, + PPM_SC_SETUID32 = 305, + PPM_SC_GETUID32 = 306, + PPM_SC_SETGID32 = 307, + PPM_SC_GETEUID32 = 308, + PPM_SC_GETGID32 = 309, + PPM_SC_SETRESUID32 = 310, + PPM_SC_SETRESGID32 = 311, + PPM_SC_GETRESUID32 = 312, + PPM_SC_GETRESGID32 = 313, + PPM_SC_FINIT_MODULE = 314, + PPM_SC_BPF = 315, + PPM_SC_SECCOMP = 316, + PPM_SC_SIGALTSTACK = 317, + PPM_SC_GETRANDOM = 318, + PPM_SC_FADVISE64 = 319, + PPM_SC_RENAMEAT2 = 320, + PPM_SC_MAX = 321, }; /* @@ -737,7 +1311,7 @@ enum ppm_event_category { EC_IO_WRITE = 33,/* General I/O write (can be file, socket, IPC...) */ EC_IO_OTHER = 34,/* General I/O that is neither read not write (can be file, socket, IPC...) */ EC_WAIT = 64, /* General wait (can be file, socket, IPC...) */ - EC_SCHEDULER = 128, /* General wait (can be file, socket, IPC...) */ + EC_SCHEDULER = 128, /* Scheduler event (e.g. context switch) */ EC_INTERNAL = 256, /* Internal event that shouldn't be shown to the user */ }; @@ -749,22 +1323,11 @@ enum ppm_event_flags { EF_READS_FROM_FD = (1 << 3), /* This event reads data from an FD. */ EF_WRITES_TO_FD = (1 << 4), /* This event writes data to an FD. */ EF_MODIFIES_STATE = (1 << 5), /* This event causes the machine state to change and should not be dropped by the filtering engine. */ - EF_UNUSED = (1 << 6), /* This event is no */ + EF_UNUSED = (1 << 6), /* This event is not used */ EF_WAITS = (1 << 7), /* This event reads data from an FD. */ -}; - -/* - * Operators to compare events - */ -enum ppm_cmp_operator { - CO_NONE = 0, - CO_EQ = 1, - CO_NE = 2, - CO_LT = 3, - CO_LE = 4, - CO_GT = 5, - CO_GE = 6, - CO_CONTAINS = 7, + EF_SKIPPARSERESET = (1 << 8), /* This event shouldn't pollute the parser lastevent state tracker. */ + EF_OLD_VERSION = (1 << 9), /* This event is kept for backward compatibility */ + EF_DROP_SIMPLE_CONS = (1 << 10) /* This event can be skipped by consumers that privilege low overhead to full event capture */ }; /* @@ -802,13 +1365,30 @@ enum ppm_param_type { PT_FLAGS8 = 28, /* this is an UINT8, but will be interpreted as 8 bit flags. */ PT_FLAGS16 = 29, /* this is an UINT16, but will be interpreted as 16 bit flags. */ PT_FLAGS32 = 30, /* this is an UINT32, but will be interpreted as 32 bit flags. */ + PT_UID = 31, /* this is an UINT32, MAX_UINT32 will be interpreted as no value. */ + PT_GID = 32, /* this is an UINT32, MAX_UINT32 will be interpreted as no value. */ + PT_DOUBLE = 33, /* this is a double precision floating point number. */ + PT_SIGSET = 34, /* sigset_t. I only store the lower UINT32 of it */ + PT_CHARBUFARRAY = 35, /* Pointer to an array of strings, exported by the user events decoder. 64bit. For internal use only. */ + PT_CHARBUF_PAIR_ARRAY = 36, /* Pointer to an array of string pairs, exported by the user events decoder. 64bit. For internal use only. */ + PT_IPV4NET = 37, /* An IPv4 network. */ + PT_IPV6ADDR = 38, /* A 16 byte raw IPv6 address. */ + PT_IPV6NET = 39, /* An IPv6 network. */ + PT_IPADDR = 40, /* Either an IPv4 or IPv6 address. The length indicates which one it is. */ + PT_IPNET = 41, /* Either an IPv4 or IPv6 network. The length indicates which one it is. */ + PT_MODE = 42, /* a 32 bit bitmask to represent file modes. */ + PT_FSRELPATH = 43, /* A path relative to a dirfd. */ + PT_MAX = 44 /* array size */ }; enum ppm_print_format { PF_NA = 0, PF_DEC = 1, /* decimal */ - PF_HEX = 2, /* hexadecima */ + PF_HEX = 2, /* hexadecimal */ PF_10_PADDED_DEC = 3, /* decimal padded to 10 digits, useful to print the fractional part of a ns timestamp */ + PF_ID = 4, + PF_DIR = 5, + PF_OCT = 6, /* octal */ }; /*! @@ -819,15 +1399,20 @@ struct ppm_name_value { uint32_t value; }; +#define DIRFD_PARAM(_param_num) ((void*)_param_num) + /*! \brief Event parameter information. */ struct ppm_param_info { - char name[PPM_MAX_NAME_LEN]; /**< Paramter name, e.g. 'size'. */ - enum ppm_param_type type; /**< Paramter type, e.g. 'uint16', 'string'... */ + char name[PPM_MAX_NAME_LEN]; /**< Parameter name, e.g. 'size'. */ + enum ppm_param_type type; /**< Parameter type, e.g. 'uint16', 'string'... */ enum ppm_print_format fmt; /**< If this is a numeric parameter, this flag specifies if it should be rendered as decimal or hex. */ - const struct ppm_name_value *symbols; /**< If this is a flags parameter, it points to an array of ppm_name_value, terminated with {0, 0} */ -}; + const void *info; /**< If this is a flags parameter, it points to an array of ppm_name_value, + if this is a FSRELPATH parameter, it references the related dirfd, + else if this is a dynamic parameter it points to an array of ppm_param_info */ + uint8_t ninfo; /**< Number of entry in the info array. */ +} _packed; /*! \brief Event information. @@ -839,13 +1424,14 @@ struct ppm_event_info { enum ppm_event_category category; /**< Event category, e.g. 'file', 'net', etc. */ enum ppm_event_flags flags; /**< flags for this event. */ uint32_t nparams; /**< Number of parameter in the params array. */ - /* XXX this 16 limit comes out of my ass. Determine something that makes sense or use a dynamic array. */ struct ppm_param_info params[PPM_MAX_EVENT_PARAMS]; /**< parameters descriptions. */ -}; +} _packed; #if defined _MSC_VER #pragma pack(push) #pragma pack(1) +#elif defined __sun +#pragma pack(1) #else #pragma pack(push, 1) #endif @@ -857,38 +1443,205 @@ struct ppm_evt_hdr { uint64_t tid; /* the tid of the thread that generated this event */ uint32_t len; /* the event len, including the header */ uint16_t type; /* the event type */ -/* uint16_t cpuid; the cpu that generated the event */ + uint32_t nparams; /* the number of parameters of the event */ }; +#if defined __sun +#pragma pack() +#else #pragma pack(pop) +#endif /* * IOCTL codes */ +#ifndef CYGWING_AGENT #define PPM_IOCTL_MAGIC 's' #define PPM_IOCTL_DISABLE_CAPTURE _IO(PPM_IOCTL_MAGIC, 0) #define PPM_IOCTL_ENABLE_CAPTURE _IO(PPM_IOCTL_MAGIC, 1) #define PPM_IOCTL_DISABLE_DROPPING_MODE _IO(PPM_IOCTL_MAGIC, 2) #define PPM_IOCTL_ENABLE_DROPPING_MODE _IO(PPM_IOCTL_MAGIC, 3) #define PPM_IOCTL_SET_SNAPLEN _IO(PPM_IOCTL_MAGIC, 4) - - -/*! - \brief System call description struct. -*/ -struct ppm_syscall_desc { - enum ppm_event_category category; /**< System call category. */ - char *name; /**< System call name, e.g. 'open'. */ -}; +#define PPM_IOCTL_MASK_ZERO_EVENTS _IO(PPM_IOCTL_MAGIC, 5) +#define PPM_IOCTL_MASK_SET_EVENT _IO(PPM_IOCTL_MAGIC, 6) +#define PPM_IOCTL_MASK_UNSET_EVENT _IO(PPM_IOCTL_MAGIC, 7) +#define PPM_IOCTL_DISABLE_DYNAMIC_SNAPLEN _IO(PPM_IOCTL_MAGIC, 8) +#define PPM_IOCTL_ENABLE_DYNAMIC_SNAPLEN _IO(PPM_IOCTL_MAGIC, 9) +#define PPM_IOCTL_GET_VTID _IO(PPM_IOCTL_MAGIC, 10) +#define PPM_IOCTL_GET_VPID _IO(PPM_IOCTL_MAGIC, 11) +#define PPM_IOCTL_GET_CURRENT_TID _IO(PPM_IOCTL_MAGIC, 12) +#define PPM_IOCTL_GET_CURRENT_PID _IO(PPM_IOCTL_MAGIC, 13) +#define PPM_IOCTL_DISABLE_SIGNAL_DELIVER _IO(PPM_IOCTL_MAGIC, 14) +#define PPM_IOCTL_ENABLE_SIGNAL_DELIVER _IO(PPM_IOCTL_MAGIC, 15) +#define PPM_IOCTL_GET_PROCLIST _IO(PPM_IOCTL_MAGIC, 16) +#define PPM_IOCTL_SET_TRACERS_CAPTURE _IO(PPM_IOCTL_MAGIC, 17) +#define PPM_IOCTL_SET_SIMPLE_MODE _IO(PPM_IOCTL_MAGIC, 18) +#define PPM_IOCTL_ENABLE_PAGE_FAULTS _IO(PPM_IOCTL_MAGIC, 19) +#define PPM_IOCTL_GET_N_TRACEPOINT_HIT _IO(PPM_IOCTL_MAGIC, 20) +#define PPM_IOCTL_GET_PROBE_VERSION _IO(PPM_IOCTL_MAGIC, 21) +#define PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE _IO(PPM_IOCTL_MAGIC, 22) +#define PPM_IOCTL_SET_STATSD_PORT _IO(PPM_IOCTL_MAGIC, 23) +#endif // CYGWING_AGENT extern const struct ppm_name_value socket_families[]; extern const struct ppm_name_value file_flags[]; +extern const struct ppm_name_value flock_flags[]; extern const struct ppm_name_value clone_flags[]; extern const struct ppm_name_value futex_operations[]; extern const struct ppm_name_value lseek_whence[]; extern const struct ppm_name_value poll_flags[]; +extern const struct ppm_name_value mount_flags[]; +extern const struct ppm_name_value umount_flags[]; extern const struct ppm_name_value shutdown_how[]; -extern const struct ppm_name_value openat_flags[]; extern const struct ppm_name_value rlimit_resources[]; extern const struct ppm_name_value fcntl_commands[]; +extern const struct ppm_name_value sockopt_levels[]; +extern const struct ppm_name_value sockopt_options[]; +extern const struct ppm_name_value ptrace_requests[]; +extern const struct ppm_name_value prot_flags[]; +extern const struct ppm_name_value mmap_flags[]; +extern const struct ppm_name_value splice_flags[]; +extern const struct ppm_name_value quotactl_cmds[]; +extern const struct ppm_name_value quotactl_types[]; +extern const struct ppm_name_value quotactl_dqi_flags[]; +extern const struct ppm_name_value quotactl_quota_fmts[]; +extern const struct ppm_name_value semop_flags[]; +extern const struct ppm_name_value semget_flags[]; +extern const struct ppm_name_value semctl_commands[]; +extern const struct ppm_name_value access_flags[]; +extern const struct ppm_name_value pf_flags[]; +extern const struct ppm_name_value unlinkat_flags[]; +extern const struct ppm_name_value linkat_flags[]; +extern const struct ppm_name_value chmod_mode[]; +extern const struct ppm_name_value renameat2_flags[]; + +extern const struct ppm_param_info sockopt_dynamic_param[]; +extern const struct ppm_param_info ptrace_dynamic_param[]; +extern const struct ppm_param_info bpf_dynamic_param[]; + +/* + * Driver event notification ID + */ +enum ppm_driver_event_id { + DEI_NONE = 0, + DEI_DISABLE_DROPPING = 1, + DEI_ENABLE_DROPPING = 2, +}; + +/*! + \brief Process information as returned by the PPM_IOCTL_GET_PROCLIST IOCTL. +*/ +struct ppm_proc_info { + uint64_t pid; + uint64_t utime; + uint64_t stime; +}; + +struct ppm_proclist_info { + int64_t n_entries; + int64_t max_entries; + struct ppm_proc_info entries[0]; +}; + +enum syscall_flags { + UF_NONE = 0, + UF_USED = (1 << 0), + UF_NEVER_DROP = (1 << 1), + UF_ALWAYS_DROP = (1 << 2), + UF_SIMPLEDRIVER_KEEP = (1 << 3), + UF_ATOMIC = (1 << 4), ///< The handler should not block (interrupt context) +}; + +struct syscall_evt_pair { + int flags; + enum ppm_event_type enter_event_type; + enum ppm_event_type exit_event_type; +} _packed; + +#define SYSCALL_TABLE_SIZE 512 + +/* + * Filler table-related definitions + */ + +#define PPM_MAX_AUTOFILL_ARGS (1 << 2) + +/* + * Max size of a parameter in the kernel module is u16, so no point + * in going beyond 0xffff. However, in BPF the limit is more stringent + * because the entire perf event must fit in u16, so make this + * a more conservative 65k so we have some room for the other + * parameters in the event. It shouldn't cause issues since typically + * snaplen is much lower than this. + */ +#define PPM_MAX_ARG_SIZE 65000 + +struct event_filler_arguments; + +#include "ppm_fillers.h" + +struct ppm_autofill_arg { +#define AF_ID_RETVAL -1 +#define AF_ID_USEDEFAULT -2 + int16_t id; + long default_val; +} _packed; + +enum autofill_paramtype { + APT_REG, + APT_SOCK, +}; + +typedef int (*filler_callback) (struct event_filler_arguments *args); + +struct ppm_event_entry { + filler_callback filler_callback; + enum ppm_filler_id filler_id; + uint16_t n_autofill_args; + enum autofill_paramtype paramtype; + struct ppm_autofill_arg autofill_args[PPM_MAX_AUTOFILL_ARGS]; +} _packed; + +/* + * parse_readv_writev_bufs flags + */ +#define PRB_FLAG_PUSH_SIZE 1 +#define PRB_FLAG_PUSH_DATA 2 +#define PRB_FLAG_PUSH_ALL (PRB_FLAG_PUSH_SIZE | PRB_FLAG_PUSH_DATA) +#define PRB_FLAG_IS_WRITE 4 + +/* + * Return codes + */ +#define PPM_SUCCESS 0 +#define PPM_FAILURE_BUFFER_FULL -1 +#define PPM_FAILURE_INVALID_USER_MEMORY -2 +#define PPM_FAILURE_BUG -3 +#define PPM_SKIP_EVENT -4 + +#define RW_SNAPLEN 80 +#define RW_MAX_SNAPLEN PPM_MAX_ARG_SIZE +#define RW_MAX_FULLCAPTURE_PORT_SNAPLEN 16000 + +/* + * Udig stuff + */ +struct udig_consumer_t { + uint32_t snaplen; + uint32_t sampling_ratio; + bool do_dynamic_snaplen; + uint32_t sampling_interval; + int is_dropping; + int dropping_mode; + volatile int need_to_insert_drop_e; + volatile int need_to_insert_drop_x; + uint16_t fullcapture_port_range_start; + uint16_t fullcapture_port_range_end; + uint16_t statsd_port; +}; +#ifdef UDIG +typedef struct udig_consumer_t ppm_consumer_t; +#else +typedef struct ppm_consumer_t ppm_consumer_t; +#endif /* UDIG */ #endif /* EVENTS_PUBLIC_H_ */ diff --git a/driver/ppm_fillers.c b/driver/ppm_fillers.c index 87177863dd..25e81af01c 100644 --- a/driver/ppm_fillers.c +++ b/driver/ppm_fillers.c @@ -1,467 +1,300 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#ifndef UDIG #include #include -#include #include #include #include +#include #include #include #include +#include #include -#include #include +#include +#include #include - +#include +#include +#include +#include +#include +#ifdef CONFIG_CGROUPS +#include +#endif +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 20) +#include "ppm_syscall.h" +#else +#include +#endif +#else /* UDIG */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "udig_capture.h" #include "ppm_ringbuffer.h" #include "ppm_events_public.h" #include "ppm_events.h" #include "ppm.h" -static int32_t f_sys_generic(struct event_filler_arguments *args); /* generic syscall event filler that includes the system call number */ -static int32_t f_sys_empty(struct event_filler_arguments *args); /* empty filler */ -static int32_t f_sys_single(struct event_filler_arguments *args); /* generic enter filler that copies a single argument syscall into a single parameter event */ -static int32_t f_sys_single_x(struct event_filler_arguments *args); /* generic exit filler that captures an integer */ -static int32_t f_sys_open_x(struct event_filler_arguments *args); -static int32_t f_sys_read_x(struct event_filler_arguments *args); -static int32_t f_sys_write_x(struct event_filler_arguments *args); -static int32_t f_proc_startupdate(struct event_filler_arguments *args); -static int32_t f_sys_socketpair_x(struct event_filler_arguments *args); -static int32_t f_sys_connect_x(struct event_filler_arguments *args); -static int32_t f_sys_accept4_e(struct event_filler_arguments *args); -static int32_t f_sys_accept_x(struct event_filler_arguments *args); -static int32_t f_sys_send_e(struct event_filler_arguments *args); -static int32_t f_sys_send_x(struct event_filler_arguments *args); -static int32_t f_sys_sendto_e(struct event_filler_arguments *args); -static int32_t f_sys_sendmsg_e(struct event_filler_arguments *args); -static int32_t f_sys_sendmsg_x(struct event_filler_arguments *args); -static int32_t f_sys_recv_e(struct event_filler_arguments *args); -static int32_t f_sys_recv_x(struct event_filler_arguments *args); -static int32_t f_sys_recvfrom_e(struct event_filler_arguments *args); -static int32_t f_sys_recvfrom_x(struct event_filler_arguments *args); -static int32_t f_sys_recvmsg_e(struct event_filler_arguments *args); -static int32_t f_sys_recvmsg_x(struct event_filler_arguments *args); -static int32_t f_sys_shutdown_e(struct event_filler_arguments *args); -static int32_t f_sys_pipe_x(struct event_filler_arguments *args); -static int32_t f_sys_eventfd_e(struct event_filler_arguments *args); -static int32_t f_sys_futex_e(struct event_filler_arguments *args); -static int32_t f_sys_lseek_e(struct event_filler_arguments *args); -static int32_t f_sys_llseek_e(struct event_filler_arguments *args); -static int32_t f_sys_socket_bind_x(struct event_filler_arguments *args); -static int32_t f_sys_poll_e(struct event_filler_arguments *args); -static int32_t f_sys_poll_x(struct event_filler_arguments *args); -static int32_t f_sys_openat_e(struct event_filler_arguments *args); -#ifndef __x86_64__ -static int32_t f_sys_pread64_e(struct event_filler_arguments *args); -static int32_t f_sys_preadv_e(struct event_filler_arguments *args); -#endif -static int32_t f_sys_writev_e(struct event_filler_arguments *args); -static int32_t f_sys_pwrite64_e(struct event_filler_arguments *args); -static int32_t f_sys_readv_x(struct event_filler_arguments *args); -static int32_t f_sys_writev_e(struct event_filler_arguments *args); -static int32_t f_sys_writev_pwritev_x(struct event_filler_arguments *args); -static int32_t f_sys_preadv_x(struct event_filler_arguments *args); -static int32_t f_sys_pwritev_e(struct event_filler_arguments *args); -static int32_t f_sys_nanosleep_e(struct event_filler_arguments *args); -static int32_t f_sys_getrlimit_setrlimit_e(struct event_filler_arguments *args); -static int32_t f_sys_getrlimit_setrlrimit_x(struct event_filler_arguments *args); -static int32_t f_sys_prlimit_e(struct event_filler_arguments *args); -static int32_t f_sys_prlimit_x(struct event_filler_arguments *args); -#ifdef CAPTURE_CONTEXT_SWITCHES -static int32_t f_sched_switch_e(struct event_filler_arguments *args); -#endif -static int32_t f_sched_drop(struct event_filler_arguments *args); -static int32_t f_sched_fcntl_e(struct event_filler_arguments *args); +#include "udig_inf.h" +#endif /* UDIG */ -/* - * Note, this is not part of g_event_info because we want to share g_event_info with userland. - * However, separating this information in a different struct is not ideal and we should find a better way. - */ -const struct ppm_event_entry g_ppm_events[PPM_EVENT_MAX] = { - [PPME_GENERIC_E] = {f_sys_generic}, - [PPME_GENERIC_X] = {f_sys_generic}, - [PPME_SYSCALL_OPEN_E] = {f_sys_empty}, - [PPME_SYSCALL_OPEN_X] = {f_sys_open_x}, - [PPME_SYSCALL_CREAT_E] = {f_sys_empty}, - [PPME_SYSCALL_CREAT_X] = {PPM_AUTOFILL, 3, APT_REG, {{AF_ID_RETVAL}, {0}, {AF_ID_USEDEFAULT, 0} } }, - [PPME_SYSCALL_CLOSE_E] = {f_sys_single}, - [PPME_SYSCALL_CLOSE_X] = {f_sys_single_x}, - [PPME_SYSCALL_READ_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {2} } }, - [PPME_SYSCALL_READ_X] = {f_sys_read_x}, - [PPME_SYSCALL_WRITE_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {2} } }, - [PPME_SYSCALL_WRITE_X] = {f_sys_write_x}, - [PPME_SYSCALL_BRK_E] = {PPM_AUTOFILL, 1, APT_REG, {{0} } }, - [PPME_SYSCALL_BRK_X] = {f_sys_single_x}, - [PPME_SYSCALL_EXECVE_E] = {f_sys_empty}, - [PPME_SYSCALL_EXECVE_X] = {f_proc_startupdate}, - [PPME_CLONE_E] = {f_sys_empty}, - [PPME_CLONE_X] = {f_proc_startupdate}, - [PPME_PROCEXIT_E] = {f_sys_empty}, - [PPME_SOCKET_SOCKET_E] = {PPM_AUTOFILL, 3, APT_SOCK, {{0}, {1}, {2} } }, - [PPME_SOCKET_SOCKET_X] = {f_sys_single_x}, - [PPME_SOCKET_SOCKETPAIR_E] = {PPM_AUTOFILL, 3, APT_SOCK, {{0}, {1}, {2} } }, - [PPME_SOCKET_SOCKETPAIR_X] = {f_sys_socketpair_x}, - [PPME_SOCKET_BIND_E] = {PPM_AUTOFILL, 1, APT_SOCK, {{0} } }, - [PPME_SOCKET_BIND_X] = {f_sys_socket_bind_x}, - [PPME_SOCKET_CONNECT_E] = {PPM_AUTOFILL, 1, APT_SOCK, {{0} } }, - [PPME_SOCKET_CONNECT_X] = {f_sys_connect_x}, - [PPME_SOCKET_LISTEN_E] = {PPM_AUTOFILL, 2, APT_SOCK, {{0}, {1} } }, - [PPME_SOCKET_LISTEN_X] = {f_sys_single_x}, - [PPME_SOCKET_ACCEPT_E] = {f_sys_empty}, - [PPME_SOCKET_ACCEPT_X] = {f_sys_accept_x}, - [PPME_SOCKET_ACCEPT4_E] = {f_sys_accept4_e}, - [PPME_SOCKET_ACCEPT4_X] = {f_sys_accept_x}, - [PPME_SOCKET_SEND_E] = {f_sys_send_e}, - [PPME_SOCKET_SEND_X] = {f_sys_send_x}, - [PPME_SOCKET_SENDTO_E] = {f_sys_sendto_e}, - [PPME_SOCKET_SENDTO_X] = {f_sys_send_x}, - [PPME_SOCKET_SENDMSG_E] = {f_sys_sendmsg_e}, - [PPME_SOCKET_SENDMSG_X] = {f_sys_sendmsg_x}, - [PPME_SOCKET_RECV_E] = {f_sys_recv_e}, - [PPME_SOCKET_RECV_X] = {f_sys_recv_x}, - [PPME_SOCKET_RECVFROM_E] = {f_sys_recvfrom_e}, - [PPME_SOCKET_RECVFROM_X] = {f_sys_recvfrom_x}, - [PPME_SOCKET_RECVMSG_E] = {f_sys_recvmsg_e}, - [PPME_SOCKET_RECVMSG_X] = {f_sys_recvmsg_x}, - [PPME_SOCKET_SHUTDOWN_E] = {f_sys_shutdown_e}, - [PPME_SOCKET_SHUTDOWN_X] = {f_sys_single_x}, - [PPME_SYSCALL_PIPE_E] = {f_sys_empty}, - [PPME_SYSCALL_PIPE_X] = {f_sys_pipe_x}, - [PPME_SYSCALL_EVENTFD_E] = {f_sys_eventfd_e}, - [PPME_SYSCALL_EVENTFD_X] = {f_sys_single_x}, - [PPME_SYSCALL_FUTEX_E] = {f_sys_futex_e}, - [PPME_SYSCALL_FUTEX_X] = {f_sys_single_x}, - [PPME_SYSCALL_STAT_E] = {f_sys_empty}, - [PPME_SYSCALL_STAT_X] = {PPM_AUTOFILL, 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, - [PPME_SYSCALL_LSTAT_E] = {f_sys_empty}, - [PPME_SYSCALL_LSTAT_X] = {PPM_AUTOFILL, 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, - [PPME_SYSCALL_FSTAT_E] = {f_sys_single}, - [PPME_SYSCALL_FSTAT_X] = {f_sys_single_x}, - [PPME_SYSCALL_STAT64_E] = {f_sys_empty}, - [PPME_SYSCALL_STAT64_X] = {PPM_AUTOFILL, 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, - [PPME_SYSCALL_LSTAT64_E] = {f_sys_empty}, - [PPME_SYSCALL_LSTAT64_X] = {PPM_AUTOFILL, 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, - [PPME_SYSCALL_FSTAT64_E] = {f_sys_single}, - [PPME_SYSCALL_FSTAT64_X] = {f_sys_single_x}, - [PPME_SYSCALL_EPOLLWAIT_E] = {PPM_AUTOFILL, 1, APT_REG, {{2} } }, - [PPME_SYSCALL_EPOLLWAIT_X] = {f_sys_single_x}, - [PPME_SYSCALL_POLL_E] = {f_sys_poll_e}, - [PPME_SYSCALL_POLL_X] = {f_sys_poll_x}, - [PPME_SYSCALL_SELECT_E] = {f_sys_empty}, - [PPME_SYSCALL_SELECT_X] = {f_sys_single_x}, - [PPME_SYSCALL_NEWSELECT_E] = {f_sys_empty}, - [PPME_SYSCALL_NEWSELECT_X] = {f_sys_single_x}, - [PPME_SYSCALL_LSEEK_E] = {f_sys_lseek_e}, - [PPME_SYSCALL_LSEEK_X] = {f_sys_single_x}, - [PPME_SYSCALL_LLSEEK_E] = {f_sys_llseek_e}, - [PPME_SYSCALL_LLSEEK_X] = {f_sys_single_x}, - [PPME_SYSCALL_IOCTL_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {1} } }, - [PPME_SYSCALL_IOCTL_X] = {f_sys_single_x}, - [PPME_SYSCALL_GETCWD_E] = {f_sys_empty}, - [PPME_SYSCALL_GETCWD_X] = {PPM_AUTOFILL, 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, - [PPME_SYSCALL_CHDIR_E] = {f_sys_empty}, - [PPME_SYSCALL_CHDIR_X] = {PPM_AUTOFILL, 2, APT_REG, {{AF_ID_RETVAL}, {0} } }, - [PPME_SYSCALL_FCHDIR_E] = {f_sys_single}, - [PPME_SYSCALL_FCHDIR_X] = {f_sys_single_x}, - [PPME_SYSCALL_MKDIR_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {AF_ID_USEDEFAULT, 0} } }, - [PPME_SYSCALL_MKDIR_X] = {f_sys_single_x}, - [PPME_SYSCALL_RMDIR_E] = {f_sys_single}, - [PPME_SYSCALL_RMDIR_X] = {f_sys_single_x}, - [PPME_SYSCALL_OPENAT_E] = {f_sys_openat_e}, - [PPME_SYSCALL_OPENAT_X] = {f_sys_single_x}, - [PPME_SYSCALL_LINK_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {1} } }, - [PPME_SYSCALL_LINK_X] = {f_sys_single_x}, - [PPME_SYSCALL_LINKAT_E] = {PPM_AUTOFILL, 4, APT_REG, {{0}, {1}, {2}, {3} } }, - [PPME_SYSCALL_LINKAT_X] = {f_sys_single_x}, - [PPME_SYSCALL_UNLINK_E] = {f_sys_single}, - [PPME_SYSCALL_UNLINK_X] = {f_sys_single_x}, - [PPME_SYSCALL_UNLINKAT_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {1} } }, - [PPME_SYSCALL_UNLINKAT_X] = {f_sys_single_x}, -#ifdef __x86_64__ - [PPME_SYSCALL_PREAD_E] = {PPM_AUTOFILL, 3, APT_REG, {{0}, {2}, {3} } }, -#else - [PPME_SYSCALL_PREAD_E] = {f_sys_pread64_e}, -#endif - [PPME_SYSCALL_PREAD_X] = {f_sys_read_x}, - [PPME_SYSCALL_PWRITE_E] = {f_sys_pwrite64_e}, - [PPME_SYSCALL_PWRITE_X] = {f_sys_write_x}, - [PPME_SYSCALL_READV_E] = {f_sys_single}, - [PPME_SYSCALL_READV_X] = {f_sys_readv_x}, - [PPME_SYSCALL_WRITEV_E] = {f_sys_writev_e}, - [PPME_SYSCALL_WRITEV_X] = {f_sys_writev_pwritev_x}, -#ifdef __x86_64__ - [PPME_SYSCALL_PREADV_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {3} } }, -#else - [PPME_SYSCALL_PREADV_E] = {f_sys_preadv_e}, -#endif - [PPME_SYSCALL_PREADV_X] = {f_sys_preadv_x}, - [PPME_SYSCALL_PWRITEV_E] = {f_sys_pwritev_e}, - [PPME_SYSCALL_PWRITEV_X] = {f_sys_writev_pwritev_x}, - [PPME_SYSCALL_DUP_E] = {PPM_AUTOFILL, 1, APT_REG, {{0} } }, - [PPME_SYSCALL_DUP_X] = {f_sys_single_x}, - /* Mask and Flags not implemented yet */ - [PPME_SYSCALL_SIGNALFD_E] = {PPM_AUTOFILL, 3, APT_REG, {{0}, {AF_ID_USEDEFAULT, 0}, {AF_ID_USEDEFAULT, 0} } }, - [PPME_SYSCALL_SIGNALFD_X] = {f_sys_single_x}, - [PPME_SYSCALL_KILL_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {1} } }, - [PPME_SYSCALL_KILL_X] = {f_sys_single_x}, - [PPME_SYSCALL_TKILL_E] = {PPM_AUTOFILL, 2, APT_REG, {{0}, {1} } }, - [PPME_SYSCALL_TKILL_X] = {f_sys_single_x}, - [PPME_SYSCALL_TGKILL_E] = {PPM_AUTOFILL, 3, APT_REG, {{0}, {1}, {2} } }, - [PPME_SYSCALL_TGKILL_X] = {f_sys_single_x}, - [PPME_SYSCALL_NANOSLEEP_E] = {f_sys_nanosleep_e}, - [PPME_SYSCALL_NANOSLEEP_X] = {f_sys_single_x}, - [PPME_SYSCALL_TIMERFD_CREATE_E] = {PPM_AUTOFILL, 2, APT_REG, {{AF_ID_USEDEFAULT, 0}, {AF_ID_USEDEFAULT, 0} } }, - [PPME_SYSCALL_TIMERFD_CREATE_X] = {f_sys_single_x}, - [PPME_SYSCALL_INOTIFY_INIT_E] = {PPM_AUTOFILL, 1, APT_REG, {{AF_ID_USEDEFAULT, 0} } }, - [PPME_SYSCALL_INOTIFY_INIT_X] = {f_sys_single_x}, - [PPME_SYSCALL_GETRLIMIT_E] = {f_sys_getrlimit_setrlimit_e}, - [PPME_SYSCALL_GETRLIMIT_X] = {f_sys_getrlimit_setrlrimit_x}, - [PPME_SYSCALL_SETRLIMIT_E] = {f_sys_getrlimit_setrlimit_e}, - [PPME_SYSCALL_SETRLIMIT_X] = {f_sys_getrlimit_setrlrimit_x}, - [PPME_SYSCALL_PRLIMIT_E] = {f_sys_prlimit_e}, - [PPME_SYSCALL_PRLIMIT_X] = {f_sys_prlimit_x}, -#ifdef CAPTURE_CONTEXT_SWITCHES - [PPME_SCHEDSWITCH_E] = {f_sched_switch_e}, +#include "ppm_ringbuffer.h" +#include "ppm_events_public.h" +#include "ppm_events.h" +#include "ppm.h" +#include "ppm_flag_helpers.h" +#ifndef UDIG +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) +#include #endif - [PPME_DROP_E] = {f_sched_drop}, - [PPME_DROP_X] = {f_sched_drop}, - [PPME_SYSCALL_FCNTL_E] = {f_sched_fcntl_e}, - [PPME_SYSCALL_FCNTL_X] = {f_sys_single_x}, -}; -/* - * do-nothing implementation of compat_ptr for systems that are not compiled - * with CONFIG_COMPAT. - */ -#ifndef CONFIG_COMPAT -#define compat_ptr(X) X -#endif +#include "kernel_hacks.h" +#endif /* UDIG */ + +#define merge_64(hi, lo) ((((unsigned long long)(hi)) << 32) + ((lo) & 0xffffffffUL)) -#define merge_64(hi, lo) ((((unsigned long long)(hi)) << 32) + ((lo) & 0xffffffffUL)); +#ifndef UDIG +static inline struct pid_namespace *pid_ns_for_children(struct task_struct *task) +{ +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0)) + return task->nsproxy->pid_ns; +#else + return task->nsproxy->pid_ns_for_children; +#endif +} +#endif /* UDIG */ -static int32_t f_sys_generic(struct event_filler_arguments *args) +int f_sys_generic(struct event_filler_arguments *args) { - int32_t res; + int res; + long table_index = args->syscall_id - SYSCALL_TABLE_ID0; + const enum ppm_syscall_code *cur_g_syscall_code_routing_table = args->cur_g_syscall_code_routing_table; -#ifndef __x86_64__ - if (unlikely(args->syscall_id == __NR_socketcall)) { +#ifdef _HAS_SOCKETCALL + if (unlikely(args->syscall_id == args->socketcall_syscall)) { /* * All the socket calls should be implemented */ ASSERT(false); return PPM_FAILURE_BUG; - } else { -#endif /* __x86_64__ */ + } +#endif + + /* + * name + */ + + if (likely(table_index >= 0 && + table_index < SYSCALL_TABLE_SIZE)) { + enum ppm_syscall_code sc_code = cur_g_syscall_code_routing_table[table_index]; + /* - * name + * ID */ - if (likely(args->syscall_id < SYSCALL_TABLE_SIZE)) { - enum ppm_syscall_code sc_code = g_syscall_code_routing_table[args->syscall_id]; + res = val_to_ring(args, sc_code, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + if (args->event_type == PPME_GENERIC_E) { /* - * ID + * nativeID */ - res = val_to_ring(args, sc_code, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - - if (args->event_type == PPME_GENERIC_E) { - /* - * nativeID - */ - res = val_to_ring(args, args->syscall_id, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - } - } else { - ASSERT(false); - res = val_to_ring(args, (unsigned long)"", 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, args->syscall_id, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } } -#ifndef __x86_64__ + } else { + ASSERT(false); + res = val_to_ring(args, (unsigned long)"", 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; } -#endif return add_sentinel(args); } -static int32_t f_sys_empty(struct event_filler_arguments *args) +int f_sys_empty(struct event_filler_arguments *args) { return add_sentinel(args); } -static int32_t f_sys_single(struct event_filler_arguments *args) +int f_sys_single(struct event_filler_arguments *args) { - int32_t res; + int res; unsigned long val; - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, true); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_single_x(struct event_filler_arguments *args) +int f_sys_single_x(struct event_filler_arguments *args) { - int32_t res; + int res; int64_t retval; retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static inline uint32_t open_flags_to_scap(unsigned long flags) +static inline uint32_t get_fd_dev(int64_t fd) { - uint32_t res = 0; - - switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) { - case O_WRONLY: - res |= PPM_O_WRONLY; - break; - case O_RDWR: - res |= PPM_O_RDWR; - break; - default: - res |= PPM_O_RDONLY; - break; - } - - if (flags & O_CREAT) { - res |= PPM_O_CREAT; - } - - if (flags & O_APPEND) { - res |= PPM_O_APPEND; - } - -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) - if (flags & O_DSYNC) { - res |= PPM_O_DSYNC; - } -#endif +#ifdef UDIG + return 0; +#else + struct files_struct *files; + struct fdtable *fdt; + struct file *file; + struct inode *inode; + struct super_block *sb; + uint32_t dev = 0; - if (flags & O_EXCL) { - res |= PPM_O_EXCL; - } + if (fd < 0) + return dev; - if (flags & O_NONBLOCK) { - res |= PPM_O_NONBLOCK; - } + files = current->files; + if (unlikely(!files)) + return dev; - if (flags & O_SYNC) { - res |= PPM_O_SYNC; - } + spin_lock(&files->file_lock); + fdt = files_fdtable(files); + if (unlikely(fd > fdt->max_fds)) + goto out_unlock; - if (flags & O_TRUNC) { - res |= PPM_O_TRUNC; - } + file = fdt->fd[fd]; + if (unlikely(!file)) + goto out_unlock; - if (flags & O_DIRECT) { - res |= PPM_O_DIRECT; - } + inode = file_inode(file); + if (unlikely(!inode)) + goto out_unlock; - if (flags & O_DIRECTORY) { - res |= PPM_O_DIRECTORY; - } + sb = inode->i_sb; + if (unlikely(!sb)) + goto out_unlock; - if (flags & O_LARGEFILE) { - res |= PPM_O_LARGEFILE; - } + dev = new_encode_dev(sb->s_dev); - return res; +out_unlock: + spin_unlock(&files->file_lock); + return dev; +#endif /* UDIG */ } -static int32_t f_sys_open_x(struct event_filler_arguments *args) +int f_sys_open_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + unsigned long flags; + unsigned long modes; + int res; int64_t retval; + /* + * fd + */ retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * name */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, true); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * Flags * Note that we convert them into the ppm portable representation before pushing them to the ring */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - res = val_to_ring(args, open_flags_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &flags); + res = val_to_ring(args, open_flags_to_scap(flags), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* - * Mode - * XXX: at this time, mode decoding is not supported. We nonetheless return a value (zero) - * so the format of the event is ready for when we'll export the mode in the future. - * - * syscall_get_arguments(current, args->regs, 2, 1, &val); + * mode */ - res = val_to_ring(args, 0, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &modes); + res = val_to_ring(args, open_modes_to_scap(flags, modes), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * dev + */ + res = val_to_ring(args, get_fd_dev(retval), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_read_x(struct event_filler_arguments *args) +int f_sys_read_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; int64_t retval; unsigned long bufsize; - unsigned int snaplen; + + /* + * Retrieve the FD. It will be used for dynamic snaplen calculation. + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + args->fd = (int)val; /* * res */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * data @@ -473,7 +306,7 @@ static int32_t f_sys_read_x(struct event_filler_arguments *args) val = 0; bufsize = 0; } else { - syscall_get_arguments(current, args->regs, 1, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); /* * The return value can be lower than the value provided by the user, @@ -482,345 +315,818 @@ static int32_t f_sys_read_x(struct event_filler_arguments *args) bufsize = retval; } - /* - * Determine the snaplen by checking the fd type. - * (note: not implemeted yet) - */ - snaplen = g_snaplen; -#if 0 - { - int fd; - int err, fput_needed; - struct socket *sock; - - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; - - sock = ppm_sockfd_lookup_light(fd, &err, &fput_needed); - if (sock) { - snaplen = g_snaplen; - fput_light(sock->file, fput_needed); - } else { - snaplen = RW_SNAPLEN; - } - } -#endif - /* * Copy the buffer */ - res = val_to_ring(args, val, min_t(unsigned long, bufsize, (unsigned long)snaplen), true); - if (unlikely(res != PPM_SUCCESS)) { + args->enforce_snaplen = true; + res = val_to_ring(args, val, bufsize, true, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_write_x(struct event_filler_arguments *args) +int f_sys_write_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; int64_t retval; unsigned long bufsize; - unsigned int snaplen; + + /* + * Retrieve the FD. It will be used for dynamic snaplen calculation. + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + args->fd = (int)val; /* * res */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * data */ - syscall_get_arguments(current, args->regs, 2, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); bufsize = val; /* - * Determine the snaplen by checking the fd type. - * (note: not implemeted yet) + * Copy the buffer */ - snaplen = g_snaplen; -#if 0 - { - int fd; - int err, fput_needed; - struct socket *sock; + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + args->enforce_snaplen = true; + res = val_to_ring(args, val, bufsize, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; + return add_sentinel(args); +} - sock = ppm_sockfd_lookup_light(fd, &err, &fput_needed); - if (sock) { - snaplen = g_snaplen; - fput_light(sock->file, fput_needed); - } else { - snaplen = RW_SNAPLEN; - } - } +#ifndef UDIG + +/* + * get_mm_counter was not inline and exported between 3.0 and 3.4 + * https://github.com/torvalds/linux/commit/69c978232aaa99476f9bd002c2a29a84fa3779b5 + * Hence the crap in these two functions + */ +unsigned long ppm_get_mm_counter(struct mm_struct *mm, int member) +{ + long val = 0; + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0) + val = get_mm_counter(mm, member); +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) + val = atomic_long_read(&mm->rss_stat.count[member]); + + if (val < 0) + val = 0; #endif - /* - * Copy the buffer - */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - res = val_to_ring(args, val, min_t(unsigned long, bufsize, (unsigned long)snaplen), true); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + return val; +} - return add_sentinel(args); +static unsigned long ppm_get_mm_swap(struct mm_struct *mm) +{ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) + return ppm_get_mm_counter(mm, MM_SWAPENTS); +#endif + return 0; } -static inline uint32_t clone_flags_to_scap(unsigned long flags) +static unsigned long ppm_get_mm_rss(struct mm_struct *mm) { - uint32_t res = 0; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0) + return get_mm_rss(mm); +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) + return ppm_get_mm_counter(mm, MM_FILEPAGES) + + ppm_get_mm_counter(mm, MM_ANONPAGES); +#else + return get_mm_rss(mm); +#endif + return 0; +} - if (flags & CLONE_FILES) { - res |= PPM_CL_CLONE_FILES; - } +#ifdef CONFIG_CGROUPS +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34) +static int ppm_cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) +{ + char *start; + struct dentry *dentry = rcu_dereference(cgrp->dentry); - if (flags & CLONE_FS) { - res |= PPM_CL_CLONE_FS; - } + if (!dentry) { + /* + * Inactive subsystems have no dentry for their root + * cgroup + */ + strcpy(buf, "/"); + return 0; + } + + start = buf + buflen; + + *--start = '\0'; + for (;;) { + int len = dentry->d_name.len; + + start -= len; + if (start < buf) + return -ENAMETOOLONG; + memcpy(start, cgrp->dentry->d_name.name, len); + cgrp = cgrp->parent; + if (!cgrp) + break; + dentry = rcu_dereference(cgrp->dentry); + if (!cgrp->parent) + continue; + if (--start < buf) + return -ENAMETOOLONG; + *start = '/'; + } + memmove(buf, start, buf + buflen - start); + return 0; +} +#endif - if (flags & CLONE_IO) { - res |= PPM_CL_CLONE_IO; - } +static int append_cgroup(const char *subsys_name, int subsys_id, char *buf, int *available) +{ + int pathlen; + int subsys_len; + char *path; - if (flags & CLONE_NEWIPC) { - res |= PPM_CL_CLONE_NEWIPC; - } +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) || LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0) + int res; +#endif - if (flags & CLONE_NEWNET) { - res |= PPM_CL_CLONE_NEWNET; - } +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0) + struct cgroup_subsys_state *css = task_css(current, subsys_id); +#else + struct cgroup_subsys_state *css = task_subsys_state(current, subsys_id); +#endif - if (flags & CLONE_NEWNS) { - res |= PPM_CL_CLONE_NEWNS; + if (!css) { + ASSERT(false); + return 1; } - if (flags & CLONE_NEWPID) { - res |= PPM_CL_CLONE_NEWPID; + if (!css->cgroup) { + ASSERT(false); + return 1; } - if (flags & CLONE_NEWUTS) { - res |= PPM_CL_CLONE_NEWUTS; +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0) + // According to https://github.com/torvalds/linux/commit/4c737b41de7f4eef2a593803bad1b918dd718b10 + // cgroup_path now returns an int again + res = cgroup_path(css->cgroup, buf, *available); + if (res < 0) { + ASSERT(false); + path = "NA"; + } else { + path = buf; } - - if (flags & CLONE_PARENT_SETTID) { - res |= PPM_CL_CLONE_PARENT_SETTID; +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0) + path = cgroup_path(css->cgroup, buf, *available); + if (!path) { + ASSERT(false); + path = "NA"; } - - if (flags & CLONE_PARENT) { - res |= PPM_CL_CLONE_PARENT; +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34) + res = cgroup_path(css->cgroup, buf, *available); + if (res < 0) { + ASSERT(false); + path = "NA"; + } else { + path = buf; } - - if (flags & CLONE_PTRACE) { - res |= PPM_CL_CLONE_PTRACE; +#else + res = ppm_cgroup_path(css->cgroup, buf, *available); + if (res < 0) { + ASSERT(false); + path = "NA"; + } else { + path = buf; } +#endif - if (flags & CLONE_SIGHAND) { - res |= PPM_CL_CLONE_SIGHAND; - } + pathlen = strlen(path); + subsys_len = strlen(subsys_name); + if (subsys_len + 1 + pathlen + 1 > *available) + return 1; + + memmove(buf + subsys_len + 1, path, pathlen); + memcpy(buf, subsys_name, subsys_len); + buf += subsys_len; + *buf++ = '='; + buf += pathlen; + *buf++ = 0; + *available -= (subsys_len + 1 + pathlen + 1); + return 0; +} - if (flags & CLONE_SYSVSEM) { - res |= PPM_CL_CLONE_SYSVSEM; - } +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0) +#define SUBSYS(_x) \ +if (append_cgroup(#_x, _x ## _cgrp_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \ + goto cgroups_error; +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0) +#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) +#define SUBSYS(_x) \ +if (append_cgroup(#_x, _x ## _subsys_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \ + goto cgroups_error; +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0) +#define IS_SUBSYS_ENABLED(option) IS_ENABLED(option) +#define SUBSYS(_x) \ +if (append_cgroup(#_x, _x ## _subsys_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \ + goto cgroups_error; +#else +#define SUBSYS(_x) \ +if (append_cgroup(#_x, _x ## _subsys_id, args->str_storage + STR_STORAGE_SIZE - available, &available)) \ + goto cgroups_error; +#endif - if (flags & CLONE_THREAD) { - res |= PPM_CL_CLONE_THREAD; - } +#endif +#endif /* UDIG */ + +/* Takes in a NULL-terminated array of pointers to strings in userspace, and + * concatenates them to a single \0-separated string. Return the length of this + * string, or <0 on error */ +int accumulate_argv_or_env(const char __user * __user *argv, + char *str_storage, + int available) +{ + int len = 0; + int n_bytes_copied; - if (flags & CLONE_UNTRACED) { - res |= PPM_CL_CLONE_UNTRACED; - } + if (argv == NULL) + return len; - if (flags & CLONE_VM) { - res |= PPM_CL_CLONE_VM; + for (;;) { + const char __user *p; + + if (unlikely(ppm_get_user(p, argv))) + return PPM_FAILURE_INVALID_USER_MEMORY; + + if (p == NULL) + break; + + /* need at least enough space for a \0 */ + if (available < 1) + return PPM_FAILURE_BUFFER_FULL; + + n_bytes_copied = ppm_strncpy_from_user(&str_storage[len], p, + available); + + /* ppm_strncpy_from_user includes the trailing \0 in its return + * count. I want to pretend it was strncpy_from_user() so I + * subtract off the 1 */ + n_bytes_copied--; + + if (n_bytes_copied < 0) + return PPM_FAILURE_INVALID_USER_MEMORY; + + if (n_bytes_copied >= available) + return PPM_FAILURE_BUFFER_FULL; + + /* update buffer. I want to keep the trailing \0, so I +1 */ + available -= n_bytes_copied+1; + len += n_bytes_copied+1; + + argv++; } - return res; + return len; } -static int32_t f_proc_startupdate(struct event_filler_arguments *args) +#ifndef UDIG +#ifdef CONFIG_COMPAT +/* compat version that deals correctly with 32bits pointers of argv */ +static int compat_accumulate_argv_or_env(compat_uptr_t argv, + char *str_storage, + int available) { - unsigned long val; - int res = 0; - unsigned int exe_len = 0; - unsigned int args_len = 0; - struct mm_struct *mm = current->mm; - int64_t retval; - const char *argstr; - int ptid; - char *spwd; + int len = 0; + int n_bytes_copied; - trace_enter(); + if (compat_ptr(argv) == NULL) + return len; - /* - * Make sure the operation was successful - */ - retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + for (;;) { + compat_uptr_t compat_p; + const char __user *p; - if (likely(retval >= 0)) { - if (unlikely(!mm)) { - args->str_storage[0] = 0; - pr_info("sysdig-probe: f_proc_startupdate drop, mm=NULL\n"); - return PPM_FAILURE_BUG; - } + if (unlikely(ppm_get_user(compat_p, compat_ptr(argv)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + p = compat_ptr(compat_p); - if (unlikely(!mm->arg_end)) { - args->str_storage[0] = 0; - pr_info("sysdig-probe: f_proc_startupdate drop, mm->arg_end=NULL\n"); - return PPM_FAILURE_BUG; - } + if (p == NULL) + break; - args_len = mm->arg_end - mm->arg_start; + /* need at least enough space for a \0 */ + if (available < 1) + return PPM_FAILURE_BUFFER_FULL; - if (args_len > PAGE_SIZE) { - args_len = PAGE_SIZE; - } + n_bytes_copied = ppm_strncpy_from_user(&str_storage[len], p, + available); - if (unlikely(ppm_copy_from_user(args->str_storage, (const void __user *)mm->arg_start, args_len))) { + /* ppm_strncpy_from_user includes the trailing \0 in its return + * count. I want to pretend it was strncpy_from_user() so I + * subtract off the 1 */ + n_bytes_copied--; + + if (n_bytes_copied < 0) { return PPM_FAILURE_INVALID_USER_MEMORY; } + if (n_bytes_copied >= available) + return PPM_FAILURE_BUFFER_FULL; - args->str_storage[args_len - 1] = 0; + /* update buffer. I want to keep the trailing \0, so I +1 */ + available -= n_bytes_copied+1; + len += n_bytes_copied+1; - exe_len = strnlen(args->str_storage, args_len); - if (exe_len < args_len) { - ++exe_len; - } - } else { - /* - * The call failed. Return empty strings for exe and args - */ - *args->str_storage = 0; - argstr = ""; + argv += sizeof(compat_uptr_t); } - /* - * exe - */ - res = val_to_ring(args, (uint64_t)(long)args->str_storage, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + return len; +} - /* - * Args - */ - res = val_to_ring(args, (int64_t)(long)args->str_storage + exe_len, args_len - exe_len, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } +#endif - /* - * tid +static int ppm_get_tty(void) +{ + /* Locking of the signal structures seems too complicated across + * multiple kernel versions to get it right, so simply do protected + * memory accesses, and in the worst case we get some garbage, + * which is not the end of the world. In the vast majority of accesses, + * we'll be just fine. */ - res = val_to_ring(args, (int64_t)current->pid, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + struct signal_struct *sig; + struct tty_struct *tty; + struct tty_driver *driver; + int major; + int minor_start; + int index; + int tty_nr = 0; - /* - * pid - */ - res = val_to_ring(args, (int64_t)current->tgid, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + sig = current->signal; + if (!sig) + return 0; - /* - * ptid - */ - if (current->real_parent) { - ptid = current->parent->pid; - } else { - ptid = 0; - } + if (unlikely(copy_from_kernel_nofault(&tty, &sig->tty, sizeof(tty)))) + return 0; - res = val_to_ring(args, (int64_t)ptid, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + if (!tty) + return 0; - /* - * cwd - */ - spwd = npm_getcwd(args->str_storage, STR_STORAGE_SIZE - 1); - if (spwd == NULL) { - spwd = ""; - } + if (unlikely(copy_from_kernel_nofault(&index, &tty->index, sizeof(index)))) + return 0; - res = val_to_ring(args, (uint64_t)(long)spwd, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + if (unlikely(copy_from_kernel_nofault(&driver, &tty->driver, sizeof(driver)))) + return 0; - /* - * fdlimit - */ - res = val_to_ring(args, (int64_t)rlimit(RLIMIT_NOFILE), 0, false); - if (res != PPM_SUCCESS) { - return res; - } + if (!driver) + return 0; - /* - * clone-only parameters - */ - if (args->event_type == PPME_CLONE_X) { + if (unlikely(copy_from_kernel_nofault(&major, &driver->major, sizeof(major)))) + return 0; -#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) + if (unlikely(copy_from_kernel_nofault(&minor_start, &driver->minor_start, sizeof(minor_start)))) + return 0; + + tty_nr = new_encode_dev(MKDEV(major, minor_start) + index); + + return tty_nr; +} + +#endif /* UDIG */ + +int f_proc_startupdate(struct event_filler_arguments *args) +{ +#ifdef UDIG + return udig_proc_startupdate(args); +#else /* UDIG */ + unsigned long val; + int res = 0; + unsigned int exe_len = 0; /* the length of the executable string */ + int args_len = 0; /*the combined length of the arguments string + executable string */ + struct mm_struct *mm = current->mm; + int64_t retval; + int ptid; + char *spwd = ""; + long total_vm = 0; + long total_rss = 0; + long swap = 0; + int available = STR_STORAGE_SIZE; + + /* + * Make sure the operation was successful + */ + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + if (unlikely(retval < 0 && + args->event_type != PPME_SYSCALL_EXECVE_19_X)) { + + /* The call failed, but this syscall has no exe, args + * anyway, so I report empty ones */ + *args->str_storage = 0; + + /* + * exe + */ + res = val_to_ring(args, (uint64_t)(long)args->str_storage, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * Args + */ + res = val_to_ring(args, (int64_t)(long)args->str_storage, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + + if (likely(retval >= 0)) { + /* + * The call succeeded. Get exe, args from the current + * process; put one \0-separated exe-args string into + * str_storage + */ + + if (unlikely(!mm)) { + args->str_storage[0] = 0; + pr_info("f_proc_startupdate drop, mm=NULL\n"); + return PPM_FAILURE_BUG; + } + + if (unlikely(!mm->arg_end)) { + args->str_storage[0] = 0; + pr_info("f_proc_startupdate drop, mm->arg_end=NULL\n"); + return PPM_FAILURE_BUG; + } + + args_len = mm->arg_end - mm->arg_start; + + if (args_len) { + if (args_len > PAGE_SIZE) + args_len = PAGE_SIZE; + + if (unlikely(ppm_copy_from_user(args->str_storage, (const void __user *)mm->arg_start, args_len))) + args_len = 0; + else + args->str_storage[args_len - 1] = 0; + } + } else { + + /* + * The execve call failed. I get exe, args from the + * input args; put one \0-separated exe-args string into + * str_storage + */ + args->str_storage[0] = 0; + + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifdef CONFIG_COMPAT + if (unlikely(args->compat)) + args_len = compat_accumulate_argv_or_env((compat_uptr_t)val, + args->str_storage, available); + else +#endif + args_len = accumulate_argv_or_env((const char __user * __user *)val, + args->str_storage, available); + + if (unlikely(args_len < 0)) + args_len = 0; + } + + if (args_len == 0) + *args->str_storage = 0; + + exe_len = strnlen(args->str_storage, args_len); + if (exe_len < args_len) + ++exe_len; + + /* + * exe + */ + res = val_to_ring(args, (uint64_t)(long)args->str_storage, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * Args + */ + res = val_to_ring(args, (int64_t)(long)args->str_storage + exe_len, args_len - exe_len, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + + /* + * tid + */ + res = val_to_ring(args, (int64_t)current->pid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * pid + */ + res = val_to_ring(args, (int64_t)current->tgid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * ptid + */ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + if (current->real_parent) + ptid = current->real_parent->pid; +#else + if (current->parent) + ptid = current->parent->pid; +#endif + else + ptid = 0; + + res = val_to_ring(args, (int64_t)ptid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * cwd, pushed empty to avoid breaking compatibility + * with the older event format + */ + res = val_to_ring(args, (uint64_t)(long)spwd, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * fdlimit + */ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + res = val_to_ring(args, (int64_t)rlimit(RLIMIT_NOFILE), 0, false, 0); +#else + res = val_to_ring(args, (int64_t)0, 0, false, 0); +#endif + if (res != PPM_SUCCESS) + return res; + + /* + * pgft_maj + */ + res = val_to_ring(args, current->maj_flt, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * pgft_min + */ + res = val_to_ring(args, current->min_flt, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + if (mm) { + total_vm = mm->total_vm << (PAGE_SHIFT-10); + total_rss = ppm_get_mm_rss(mm) << (PAGE_SHIFT-10); + swap = ppm_get_mm_swap(mm) << (PAGE_SHIFT-10); + } + + /* + * vm_size + */ + res = val_to_ring(args, total_vm, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vm_rss + */ + res = val_to_ring(args, total_rss, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vm_swap + */ + res = val_to_ring(args, swap, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * comm + */ + res = val_to_ring(args, (uint64_t)current->comm, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * cgroups + */ + args->str_storage[0] = 0; +#ifdef CONFIG_CGROUPS + rcu_read_lock(); +#include +cgroups_error: + rcu_read_unlock(); +#endif + + res = val_to_ring(args, (int64_t)(long)args->str_storage, STR_STORAGE_SIZE - available, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + if (args->event_type == PPME_SYSCALL_CLONE_20_X || + args->event_type == PPME_SYSCALL_FORK_20_X || + args->event_type == PPME_SYSCALL_VFORK_20_X) { + /* + * clone-only parameters + */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) uint64_t euid = from_kuid_munged(current_user_ns(), current_euid()); uint64_t egid = from_kgid_munged(current_user_ns(), current_egid()); -#else +#elif LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) uint64_t euid = current_euid(); uint64_t egid = current_egid(); +#else + uint64_t euid = current->euid; + uint64_t egid = current->egid; +#endif + int64_t in_pidns = 0; +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + struct pid_namespace *pidns = task_active_pid_ns(current); #endif /* * flags */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, (uint64_t)clone_flags_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { + if (args->event_type == PPME_SYSCALL_CLONE_20_X) { +#ifdef CONFIG_S390 + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#else + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); +#endif + } else + val = 0; + +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + if(pidns != &init_pid_ns || pid_ns_for_children(current) != pidns) + in_pidns = PPM_CL_CHILD_IN_PIDNS; +#endif + res = val_to_ring(args, (uint64_t)clone_flags_to_scap(val) | in_pidns, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * uid */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, euid, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, euid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * gid */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, egid, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, egid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vtid + */ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + res = val_to_ring(args, task_pid_vnr(current), 0, false, 0); +#else + /* Not relevant in old kernels */ + res = val_to_ring(args, 0, 0, false, 0); +#endif + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vpid + */ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + res = val_to_ring(args, task_tgid_vnr(current), 0, false, 0); +#else + /* Not relevant in old kernels */ + res = val_to_ring(args, 0, 0, false, 0); +#endif + if (unlikely(res != PPM_SUCCESS)) return res; + + } else if (args->event_type == PPME_SYSCALL_EXECVE_19_X) { + /* + * execve-only parameters + */ + long env_len = 0; + int tty_nr = 0; + + if (likely(retval >= 0)) { + /* + * Already checked for mm validity + */ + env_len = mm->env_end - mm->env_start; + + if (env_len) { + if (env_len > PAGE_SIZE) + env_len = PAGE_SIZE; + + if (unlikely(ppm_copy_from_user(args->str_storage, (const void __user *)mm->env_start, env_len))) + env_len = 0; + else + args->str_storage[env_len - 1] = 0; + } + } else { + /* + * The call failed, so get the env from the arguments + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); +#ifdef CONFIG_COMPAT + if (unlikely(args->compat)) + env_len = compat_accumulate_argv_or_env((compat_uptr_t)val, + args->str_storage, available); + else +#endif + env_len = accumulate_argv_or_env((const char __user * __user *)val, + args->str_storage, available); + + if (unlikely(env_len < 0)) + env_len = 0; } + + if (env_len == 0) + *args->str_storage = 0; + + /* + * environ + */ + res = val_to_ring(args, (int64_t)(long)args->str_storage, env_len, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * tty + */ + tty_nr = ppm_get_tty(); + res = val_to_ring(args, tty_nr, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * pgid + */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) + res = val_to_ring(args, (int64_t)task_pgrp_nr_ns(current, task_active_pid_ns(current)), 0, false, 0); +#else + res = val_to_ring(args, (int64_t)process_group(current), 0, false, 0); +#endif + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * loginuid + */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) + val = from_kuid(current_user_ns(), audit_get_loginuid(current)); +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) + val = audit_get_loginuid(current); +#else + val = audit_get_loginuid(current->audit_context); +#endif + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; } + return add_sentinel(args); +#endif /* UDIG */ +} + +int f_sys_execve_e(struct event_filler_arguments *args) +{ + int res; + unsigned long val; + + /* + * filename + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (res == PPM_FAILURE_INVALID_USER_MEMORY) + res = val_to_ring(args, (unsigned long)"", 0, false, 0); + + if (unlikely(res != PPM_SUCCESS)) + return res; + return add_sentinel(args); } -static int32_t f_sys_socket_bind_x(struct event_filler_arguments *args) +int f_sys_socket_bind_x(struct event_filler_arguments *args) { - int32_t res; + int res; int64_t retval; int err = 0; u16 size = 0; @@ -833,25 +1139,28 @@ static int32_t f_sys_socket_bind_x(struct event_filler_arguments *args) * res */ retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); + res = val_to_ring(args, retval, 0, false, 0); /* * addr */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif + usrsockaddr = (struct sockaddr __user *)val; /* * Get the address len */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 2, 1, &val); -#else - val = args->socketcall_args[2]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[2]; #endif if (usrsockaddr != NULL && val != 0) { @@ -876,17 +1185,17 @@ static int32_t f_sys_socket_bind_x(struct event_filler_arguments *args) res = val_to_ring(args, (uint64_t)(unsigned long)targetbuf, size, - false); - if (unlikely(res != PPM_SUCCESS)) { + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_connect_x(struct event_filler_arguments *args) +int f_sys_connect_x(struct event_filler_arguments *args) { - int32_t res; + int res; int64_t retval; int err = 0; int fd; @@ -900,38 +1209,43 @@ static int32_t f_sys_connect_x(struct event_filler_arguments *args) * Push the result */ retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); + res = val_to_ring(args, retval, 0, false, 0); /* * Retrieve the fd and push it to the ring. * Note that, even if we are in the exit callback, the arguments are still * in the stack, and therefore we can consume them. */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; -#else - fd = (int)args->socketcall_args[0]; + if (!args->is_socketcall) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + fd = (int)val; + } +#ifndef UDIG + else + fd = (int)args->socketcall_args[0]; #endif if (fd >= 0) { /* * Get the address */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif + usrsockaddr = (struct sockaddr __user *)val; /* * Get the address len */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 2, 1, &val); -#else - val = args->socketcall_args[2]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[2]; #endif if (usrsockaddr != NULL && val != 0) { @@ -960,17 +1274,17 @@ static int32_t f_sys_connect_x(struct event_filler_arguments *args) res = val_to_ring(args, (uint64_t)(unsigned long)targetbuf, size, - false); - if (unlikely(res != PPM_SUCCESS)) { + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_socketpair_x(struct event_filler_arguments *args) +int f_sys_socketpair_x(struct event_filler_arguments *args) { - int32_t res; + int res; int64_t retval; unsigned long val; int fds[2]; @@ -983,49 +1297,54 @@ static int32_t f_sys_socketpair_x(struct event_filler_arguments *args) * retval */ retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* - * If the call was succesful, copy the FDs + * If the call was successful, copy the FDs */ +#ifndef UDIG if (likely(retval >= 0)) { /* * fds */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 3, 1, &val); -#else - val = args->socketcall_args[3]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + else + val = args->socketcall_args[3]; +#ifdef CONFIG_COMPAT + if (!args->compat) { #endif - if (unlikely(ppm_copy_from_user(fds, (const void __user *)val, sizeof(fds)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; + if (unlikely(ppm_copy_from_user(fds, (const void __user *)val, sizeof(fds)))) + return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(fds, (const void __user *)compat_ptr(val), sizeof(fds)))) + return PPM_FAILURE_INVALID_USER_MEMORY; } +#endif - res = val_to_ring(args, fds[0], 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, fds[0], 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - res = val_to_ring(args, fds[1], 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, fds[1], 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* get socket source and peer address */ sock = sockfd_lookup(fds[0], &err); if (likely(sock != NULL)) { us = unix_sk(sock->sk); speer = us->peer; - res = val_to_ring(args, (unsigned long)us, 0, false); + res = val_to_ring(args, (unsigned long)us, 0, false, 0); if (unlikely(res != PPM_SUCCESS)) { sockfd_put(sock); return res; } - res = val_to_ring(args, (unsigned long)speer, 0, false); + res = val_to_ring(args, (unsigned long)speer, 0, false, 0); if (unlikely(res != PPM_SUCCESS)) { sockfd_put(sock); return res; @@ -1036,59 +1355,332 @@ static int32_t f_sys_socketpair_x(struct event_filler_arguments *args) return err; } } else { - res = val_to_ring(args, 0, 0, false); - if (unlikely(res != PPM_SUCCESS)) { +#endif /* UDIG */ + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - res = val_to_ring(args, 0, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - } - - return add_sentinel(args); -} -static int32_t f_sys_accept4_e(struct event_filler_arguments *args) -{ - int32_t res; + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; - /* - * push the flags into the ring. - * XXX we don't support flags yet and so we just return zero - */ - /* res = val_to_ring(args, args->socketcall_args[3]); */ - res = val_to_ring(args, 0, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; +#ifndef UDIG } +#endif return add_sentinel(args); } -static int32_t f_sys_accept_x(struct event_filler_arguments *args) +static int parse_sockopt(struct event_filler_arguments *args, int level, int optname, const void __user *optval, int optlen) { - int32_t res; - int fd; - char *targetbuf = args->str_storage; - u16 size = 0; - unsigned long val; - unsigned long srvskfd; - int err = 0; - struct socket *sock; - - /* - * Push the fd - */ - fd = syscall_get_return_value(current, args->regs); - res = val_to_ring(args, (int64_t)fd, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + union { + uint32_t val32; + uint64_t val64; + struct timeval tv; + } u; + nanoseconds ns = 0; + + if (level == SOL_SOCKET) { + switch (optname) { +#ifdef SO_ERROR + case SO_ERROR: + if (unlikely(ppm_copy_from_user(&u.val32, optval, sizeof(u.val32)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + return val_to_ring(args, -(int)u.val32, 0, false, PPM_SOCKOPT_IDX_ERRNO); +#endif - /* - * Convert the fd into socket endpoint information +#ifdef SO_RCVTIMEO + case SO_RCVTIMEO: +#endif +#ifdef SO_SNDTIMEO + case SO_SNDTIMEO: +#endif + if (unlikely(ppm_copy_from_user(&u.tv, optval, sizeof(u.tv)))) { + return PPM_FAILURE_INVALID_USER_MEMORY; + } + ns = u.tv.tv_sec * SECOND_IN_NS + u.tv.tv_usec * 1000; + return val_to_ring(args, ns, 0, false, PPM_SOCKOPT_IDX_TIMEVAL); + +#ifdef SO_COOKIE + case SO_COOKIE: + if (unlikely(ppm_copy_from_user(&u.val64, optval, sizeof(u.val64)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + return val_to_ring(args, u.val64, 0, false, PPM_SOCKOPT_IDX_UINT64); +#endif + +#ifdef SO_DEBUG + case SO_DEBUG: +#endif +#ifdef SO_REUSEADDR + case SO_REUSEADDR: +#endif +#ifdef SO_TYPE + case SO_TYPE: +#endif +#ifdef SO_DONTROUTE + case SO_DONTROUTE: +#endif +#ifdef SO_BROADCAST + case SO_BROADCAST: +#endif +#ifdef SO_SNDBUF + case SO_SNDBUF: +#endif +#ifdef SO_RCVBUF + case SO_RCVBUF: +#endif +#ifdef SO_SNDBUFFORCE + case SO_SNDBUFFORCE: +#endif +#ifdef SO_RCVBUFFORCE + case SO_RCVBUFFORCE: +#endif +#ifdef SO_KEEPALIVE + case SO_KEEPALIVE: +#endif +#ifdef SO_OOBINLINE + case SO_OOBINLINE: +#endif +#ifdef SO_NO_CHECK + case SO_NO_CHECK: +#endif +#ifdef SO_PRIORITY + case SO_PRIORITY: +#endif +#ifdef SO_BSDCOMPAT + case SO_BSDCOMPAT: +#endif +#ifdef SO_REUSEPORT + case SO_REUSEPORT: +#endif +#ifdef SO_PASSCRED + case SO_PASSCRED: +#endif +#ifdef SO_RCVLOWAT + case SO_RCVLOWAT: +#endif +#ifdef SO_SNDLOWAT + case SO_SNDLOWAT: +#endif +#ifdef SO_SECURITY_AUTHENTICATION + case SO_SECURITY_AUTHENTICATION: +#endif +#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT + case SO_SECURITY_ENCRYPTION_TRANSPORT: +#endif +#ifdef SO_SECURITY_ENCRYPTION_NETWORK + case SO_SECURITY_ENCRYPTION_NETWORK: +#endif +#ifdef SO_BINDTODEVICE + case SO_BINDTODEVICE: +#endif +#ifdef SO_DETACH_FILTER + case SO_DETACH_FILTER: +#endif +#ifdef SO_TIMESTAMP + case SO_TIMESTAMP: +#endif +#ifdef SO_ACCEPTCONN + case SO_ACCEPTCONN: +#endif +#ifdef SO_PEERSEC + case SO_PEERSEC: +#endif +#ifdef SO_PASSSEC + case SO_PASSSEC: +#endif +#ifdef SO_TIMESTAMPNS + case SO_TIMESTAMPNS: +#endif +#ifdef SO_MARK + case SO_MARK: +#endif +#ifdef SO_TIMESTAMPING + case SO_TIMESTAMPING: +#endif +#ifdef SO_PROTOCOL + case SO_PROTOCOL: +#endif +#ifdef SO_DOMAIN + case SO_DOMAIN: +#endif +#ifdef SO_RXQ_OVFL + case SO_RXQ_OVFL: +#endif +#ifdef SO_WIFI_STATUS + case SO_WIFI_STATUS: +#endif +#ifdef SO_PEEK_OFF + case SO_PEEK_OFF: +#endif +#ifdef SO_NOFCS + case SO_NOFCS: +#endif +#ifdef SO_LOCK_FILTER + case SO_LOCK_FILTER: +#endif +#ifdef SO_SELECT_ERR_QUEUE + case SO_SELECT_ERR_QUEUE: +#endif +#ifdef SO_BUSY_POLL + case SO_BUSY_POLL: +#endif +#ifdef SO_MAX_PACING_RATE + case SO_MAX_PACING_RATE: +#endif +#ifdef SO_BPF_EXTENSIONS + case SO_BPF_EXTENSIONS: +#endif +#ifdef SO_INCOMING_CPU + case SO_INCOMING_CPU: +#endif + if (unlikely(ppm_copy_from_user(&u.val32, optval, sizeof(u.val32)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + return val_to_ring(args, u.val32, 0, false, PPM_SOCKOPT_IDX_UINT32); + + default: + return val_to_ring(args, (unsigned long)optval, optlen, true, PPM_SOCKOPT_IDX_UNKNOWN); + } + } else { + return val_to_ring(args, (unsigned long)optval, optlen, true, PPM_SOCKOPT_IDX_UNKNOWN); + } +} + +int f_sys_setsockopt_x(struct event_filler_arguments *args) +{ + int res; + int64_t retval; + unsigned long val[5] = {}; + + syscall_get_arguments_deprecated(current, args->regs, 0, 5, val); + retval = (int64_t)(long)syscall_get_return_value(current, args->regs); + + /* retval */ + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* fd */ + res = val_to_ring(args, val[0], 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* level */ + res = val_to_ring(args, sockopt_level_to_scap(val[1]), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* optname */ + res = val_to_ring(args, sockopt_optname_to_scap(val[1], val[2]), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* optval */ + res = parse_sockopt(args, val[1], val[2], (const void __user*)val[3], val[4]); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* optlen */ + res = val_to_ring(args, val[4], 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); + +} + +int f_sys_getsockopt_x(struct event_filler_arguments *args) +{ + int res; + int64_t retval; + uint32_t optlen; + unsigned long val[5] = {}; + + syscall_get_arguments_deprecated(current, args->regs, 0, 5, val); + retval = (int64_t)(long)syscall_get_return_value(current, args->regs); + + /* retval */ + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* fd */ + res = val_to_ring(args, val[0], 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* level */ + res = val_to_ring(args, sockopt_level_to_scap(val[1]), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* optname */ + res = val_to_ring(args, sockopt_optname_to_scap(val[1], val[2]), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + if (unlikely(ppm_copy_from_user(&optlen, (const void __user*)val[4], sizeof(optlen)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* optval */ + res = parse_sockopt(args, val[1], val[2], (const void __user*)val[3], optlen); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* optlen */ + res = val_to_ring(args, optlen, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_accept4_e(struct event_filler_arguments *args) +{ + int res; + + /* + * push the flags into the ring. + * XXX we don't support flags yet and so we just return zero + */ + /* res = val_to_ring(args, args->socketcall_args[3]); */ + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_accept_x(struct event_filler_arguments *args) +{ + int res; + int fd; + char *targetbuf = args->str_storage; + u16 size = 0; + unsigned long queuepct = 0; + unsigned long ack_backlog = 0; + unsigned long max_ack_backlog = 0; + unsigned long srvskfd; + int err = 0; + struct socket *sock; + + /* + * Push the fd + */ + fd = syscall_get_return_value(current, args->regs); + res = val_to_ring(args, (int64_t)fd, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * Convert the fd into socket endpoint information */ size = fd_to_socktuple(fd, NULL, @@ -1104,99 +1696,106 @@ static int32_t f_sys_accept_x(struct event_filler_arguments *args) res = val_to_ring(args, (uint64_t)(unsigned long)targetbuf, size, - false); - if (unlikely(res != PPM_SUCCESS)) { + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * queuepct */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &srvskfd); -#else - srvskfd = args->socketcall_args[0]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &srvskfd); +#ifndef UDIG + else + srvskfd = args->socketcall_args[0]; #endif + +#ifndef UDIG sock = sockfd_lookup(srvskfd, &err); - if (unlikely(!sock || !(sock->sk))) { - val = 0; + if (sock && sock->sk) { + ack_backlog = sock->sk->sk_ack_backlog; + max_ack_backlog = sock->sk->sk_max_ack_backlog; + } - if (sock) { - sockfd_put(sock); - } - } else { - if (sock->sk->sk_max_ack_backlog == 0) { - val = 0; - } else { - val = (unsigned long)sock->sk->sk_ack_backlog * 100 / sock->sk->sk_max_ack_backlog; - } + if (sock) sockfd_put(sock); - } - res = val_to_ring(args, val, 0, false); - if (res != PPM_SUCCESS) { + if (max_ack_backlog) + queuepct = (unsigned long)ack_backlog * 100 / max_ack_backlog; +#endif /* UDIG */ + + res = val_to_ring(args, queuepct, 0, false, 0); + if (res != PPM_SUCCESS) + return res; + + res = val_to_ring(args, ack_backlog, 0, false, 0); + if (res != PPM_SUCCESS) + return res; + + res = val_to_ring(args, max_ack_backlog, 0, false, 0); + if (res != PPM_SUCCESS) return res; - } return add_sentinel(args); } -static int32_t f_sys_send_e_common(struct event_filler_arguments *args, int *fd) +int f_sys_send_e_common(struct event_filler_arguments *args, int *fd) { - int32_t res; + int res; unsigned long size; unsigned long val; /* * fd */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); -#else - val = args->socketcall_args[0]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[0]; #endif - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } *fd = val; /* * size */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 2, 1, &size); -#else - size = args->socketcall_args[2]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &size); +#ifndef UDIG + else + size = args->socketcall_args[2]; #endif - res = val_to_ring(args, size, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + + res = val_to_ring(args, size, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return PPM_SUCCESS; } -static int32_t f_sys_send_e(struct event_filler_arguments *args) +int f_sys_send_e(struct event_filler_arguments *args) { - int32_t res; + int res; int fd; res = f_sys_send_e_common(args, &fd); - if (likely(res == PPM_SUCCESS)) { + if (likely(res == PPM_SUCCESS)) return add_sentinel(args); - } else { - return res; - } + return res; } -static int32_t f_sys_sendto_e(struct event_filler_arguments *args) +int f_sys_sendto_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; u16 size = 0; char *targetbuf = args->str_storage; int fd; @@ -1210,27 +1809,29 @@ static int32_t f_sys_sendto_e(struct event_filler_arguments *args) * Push the common params to the ring */ res = f_sys_send_e_common(args, &fd); - if (unlikely(res != PPM_SUCCESS)) { + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * Get the address */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 4, 1, &val); -#else - val = args->socketcall_args[4]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[4]; #endif + usrsockaddr = (struct sockaddr __user *)val; /* * Get the address len */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 5, 1, &val); -#else - val = args->socketcall_args[5]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 5, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[5]; #endif if (usrsockaddr != NULL && val != 0) { @@ -1258,29 +1859,40 @@ static int32_t f_sys_sendto_e(struct event_filler_arguments *args) res = val_to_ring(args, (uint64_t)(unsigned long)targetbuf, size, - false); - if (unlikely(res != PPM_SUCCESS)) { + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_send_x(struct event_filler_arguments *args) +int f_sys_send_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; int64_t retval; unsigned long bufsize; + /* + * Retrieve the FD. It will be used for dynamic snaplen calculation. + */ + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[0]; +#endif + + args->fd = (int)val; + /* * res */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * data @@ -1292,10 +1904,11 @@ static int32_t f_sys_send_x(struct event_filler_arguments *args) val = 0; bufsize = 0; } else { -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif /* @@ -1305,87 +1918,39 @@ static int32_t f_sys_send_x(struct event_filler_arguments *args) bufsize = retval; } - res = val_to_ring(args, val, min_t(unsigned long, bufsize, (unsigned long)g_snaplen), true); - if (unlikely(res != PPM_SUCCESS)) { + args->enforce_snaplen = true; + res = val_to_ring(args, val, bufsize, true, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_recv_e_common(struct event_filler_arguments *args) +int f_sys_recv_x_common(struct event_filler_arguments *args, int64_t *retval) { - int32_t res; + int res; unsigned long val; + unsigned long bufsize; /* - * fd - */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); -#else - val = args->socketcall_args[0]; -#endif - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - - /* - * size + * Retrieve the FD. It will be used for dynamic snaplen calculation. */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 2, 1, &val); -#else - val = args->socketcall_args[2]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - - return PPM_SUCCESS; -} - -static int32_t f_sys_recv_e(struct event_filler_arguments *args) -{ - int32_t res; - - res = f_sys_recv_e_common(args); - - if (likely(res == PPM_SUCCESS)) { - return add_sentinel(args); - } else { - return res; - } -} - -static int32_t f_sys_recvfrom_e(struct event_filler_arguments *args) -{ - int32_t res; - - res = f_sys_recv_e_common(args); - if (likely(res == PPM_SUCCESS)) { - return add_sentinel(args); - } else { - return res; - } -} -static int32_t f_sys_recv_x_common(struct event_filler_arguments *args, int64_t *retval) -{ - int32_t res; - unsigned long val; - unsigned long bufsize; + args->fd = (int)val; /* * res */ *retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, *retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, *retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * data @@ -1397,10 +1962,11 @@ static int32_t f_sys_recv_x_common(struct event_filler_arguments *args, int64_t val = 0; bufsize = 0; } else { -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif /* @@ -1410,32 +1976,30 @@ static int32_t f_sys_recv_x_common(struct event_filler_arguments *args, int64_t bufsize = *retval; } - res = val_to_ring(args, val, min_t(unsigned long, bufsize, (unsigned long)g_snaplen), true); - if (unlikely(res != PPM_SUCCESS)) { + args->enforce_snaplen = true; + res = val_to_ring(args, val, bufsize, true, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return PPM_SUCCESS; } -static int32_t f_sys_recv_x(struct event_filler_arguments *args) +int f_sys_recv_x(struct event_filler_arguments *args) { - int32_t res; + int res; int64_t retval; res = f_sys_recv_x_common(args, &retval); - if (likely(res == PPM_SUCCESS)) { + if (likely(res == PPM_SUCCESS)) return add_sentinel(args); - } else { - return res; - } + return res; } -static int32_t f_sys_recvfrom_x(struct event_filler_arguments *args) +int f_sys_recvfrom_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; u16 size = 0; int64_t retval; char *targetbuf = args->str_storage; @@ -1449,43 +2013,54 @@ static int32_t f_sys_recvfrom_x(struct event_filler_arguments *args) * Push the common params to the ring */ res = f_sys_recv_x_common(args, &retval); - if (unlikely(res != PPM_SUCCESS)) { + if (unlikely(res != PPM_SUCCESS)) return res; - } if (retval >= 0) { /* * Get the fd */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; -#else - fd = (int)args->socketcall_args[0]; + if (!args->is_socketcall) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + fd = (int)val; + } +#ifndef UDIG + else + fd = (int)args->socketcall_args[0]; #endif /* * Get the address */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 4, 1, &val); -#else - val = args->socketcall_args[4]; -#endif + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[4]; +#endif usrsockaddr = (struct sockaddr __user *)val; /* * Get the address len */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 5, 1, &val); -#else - val = args->socketcall_args[5]; -#endif + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 5, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[5]; +#endif if (usrsockaddr != NULL && val != 0) { - if (unlikely(ppm_copy_from_user(&addrlen, (const void __user *)val, sizeof(addrlen)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&addrlen, (const void __user *)val, sizeof(addrlen)))) + return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(&addrlen, (const void __user *)compat_ptr(val), sizeof(addrlen)))) + return PPM_FAILURE_INVALID_USER_MEMORY; } +#endif /* * Copy the address @@ -1512,21 +2087,33 @@ static int32_t f_sys_recvfrom_x(struct event_filler_arguments *args) res = val_to_ring(args, (uint64_t)(unsigned long)targetbuf, size, - false); - if (unlikely(res != PPM_SUCCESS)) { + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_sendmsg_e(struct event_filler_arguments *args) +int f_sys_sendmsg_e(struct event_filler_arguments *args) { - int32_t res; + int res; unsigned long val; +#ifndef UDIG +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) + struct user_msghdr mh; +#else struct msghdr mh; +#endif +#else /* UDIG */ + struct msghdr mh; +#endif /* UDIG */ char *targetbuf = args->str_storage; const struct iovec __user *iov; +#ifdef CONFIG_COMPAT + const struct compat_iovec __user *compat_iov; + struct compat_msghdr compat_mh; +#endif unsigned long iovcnt; int fd; u16 size = 0; @@ -1538,53 +2125,82 @@ static int32_t f_sys_sendmsg_e(struct event_filler_arguments *args) /* * fd */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); -#else - val = args->socketcall_args[0]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[0]; #endif + fd = val; - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * Retrieve the message header */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif - if (unlikely(ppm_copy_from_user(&mh, (const void __user *)val, sizeof(struct msghdr)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; - } +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&mh, (const void __user *)val, sizeof(mh)))) + return PPM_FAILURE_INVALID_USER_MEMORY; - /* - * size - */ - iov = (const struct iovec __user *)mh.msg_iov; - iovcnt = mh.msg_iovlen; + /* + * size + */ + iov = (const struct iovec __user *)mh.msg_iov; + iovcnt = mh.msg_iovlen; - res = parse_readv_writev_bufs(args, iov, iovcnt, g_snaplen, PRB_FLAG_PUSH_SIZE); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + res = parse_readv_writev_bufs(args, iov, iovcnt, args->consumer->snaplen, PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); - /* - * tuple - */ - usrsockaddr = (struct sockaddr __user *)mh.msg_name; - addrlen = mh.msg_namelen; - if (usrsockaddr != NULL && addrlen != 0) { + if (unlikely(res != PPM_SUCCESS)) + return res; + /* - * Copy the address + * tuple */ - err = addr_to_kernel(usrsockaddr, addrlen, (struct sockaddr *)&address); - if (likely(err >= 0)) { + usrsockaddr = (struct sockaddr __user *)mh.msg_name; + addrlen = mh.msg_namelen; +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(&compat_mh, (const void __user *)compat_ptr(val), sizeof(compat_mh)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * size + */ + compat_iov = (const struct compat_iovec __user *)compat_ptr(compat_mh.msg_iov); + iovcnt = compat_mh.msg_iovlen; + + res = compat_parse_readv_writev_bufs(args, compat_iov, iovcnt, args->consumer->snaplen, PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); + + + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * tuple + */ + usrsockaddr = (struct sockaddr __user *)compat_ptr(compat_mh.msg_name); + addrlen = compat_mh.msg_namelen; + } +#endif + + if (usrsockaddr != NULL && addrlen != 0) { + /* + * Copy the address + */ + err = addr_to_kernel(usrsockaddr, addrlen, (struct sockaddr *)&address); + if (likely(err >= 0)) { /* * Convert the fd into socket endpoint information */ @@ -1602,88 +2218,107 @@ static int32_t f_sys_sendmsg_e(struct event_filler_arguments *args) res = val_to_ring(args, (uint64_t)(unsigned long)targetbuf, size, - false); - if (unlikely(res != PPM_SUCCESS)) { + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_sendmsg_x(struct event_filler_arguments *args) +int f_sys_sendmsg_x(struct event_filler_arguments *args) { - int32_t res; + int res; unsigned long val; int64_t retval; const struct iovec __user *iov; +#ifdef CONFIG_COMPAT + const struct compat_iovec __user *compat_iov; + struct compat_msghdr compat_mh; +#endif unsigned long iovcnt; + +#ifndef UDIG +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) + struct user_msghdr mh; +#else + struct msghdr mh; +#endif +#else /* UDIG */ struct msghdr mh; +#endif /* UDIG */ /* * res */ retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * Retrieve the message header */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif - if (unlikely(ppm_copy_from_user(&mh, (const void __user *)val, sizeof(struct msghdr)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; - } - /* * data */ - iov = (const struct iovec __user *)mh.msg_iov; - iovcnt = mh.msg_iovlen; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&mh, (const void __user *)val, sizeof(mh)))) + return PPM_FAILURE_INVALID_USER_MEMORY; - res = parse_readv_writev_bufs(args, iov, iovcnt, g_snaplen, PRB_FLAG_PUSH_DATA); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - return add_sentinel(args); -} + iov = (const struct iovec __user *)mh.msg_iov; + iovcnt = mh.msg_iovlen; -static int32_t f_sys_recvmsg_e(struct event_filler_arguments *args) -{ - int32_t res; - unsigned long val; + res = parse_readv_writev_bufs(args, iov, iovcnt, args->consumer->snaplen, PRB_FLAG_PUSH_DATA | PRB_FLAG_IS_WRITE); + if (unlikely(res != PPM_SUCCESS)) + return res; +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(&compat_mh, (const void __user *)compat_ptr(val), sizeof(compat_mh)))) + return PPM_FAILURE_INVALID_USER_MEMORY; - /* - * fd - */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); -#else - val = args->socketcall_args[0]; -#endif - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; + compat_iov = (const struct compat_iovec __user *)compat_ptr(compat_mh.msg_iov); + iovcnt = compat_mh.msg_iovlen; + + res = compat_parse_readv_writev_bufs(args, compat_iov, iovcnt, args->consumer->snaplen, PRB_FLAG_PUSH_DATA | PRB_FLAG_IS_WRITE); + if (unlikely(res != PPM_SUCCESS)) + return res; } +#endif return add_sentinel(args); } -static int32_t f_sys_recvmsg_x(struct event_filler_arguments *args) +int f_sys_recvmsg_x(struct event_filler_arguments *args) { - int32_t res; + int res; unsigned long val; int64_t retval; const struct iovec __user *iov; +#ifdef CONFIG_COMPAT + const struct compat_iovec __user *compat_iov; + struct compat_msghdr compat_mh; +#endif unsigned long iovcnt; +#ifndef UDIG +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) + struct user_msghdr mh; +#else + struct msghdr mh; +#endif +#else /* UDIG */ struct msghdr mh; +#endif /* UDIG */ char *targetbuf = args->str_storage; int fd; struct sockaddr __user *usrsockaddr; @@ -1696,34 +2331,50 @@ static int32_t f_sys_recvmsg_x(struct event_filler_arguments *args) * res */ retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * Retrieve the message header */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif - if (unlikely(ppm_copy_from_user(&mh, (const void __user *)val, sizeof(struct msghdr)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; - } +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&mh, (const void __user *)val, sizeof(mh)))) + return PPM_FAILURE_INVALID_USER_MEMORY; - /* - * data and size - */ - iov = (const struct iovec __user *)mh.msg_iov; - iovcnt = mh.msg_iovlen; + /* + * data and size + */ + iov = (const struct iovec __user *)mh.msg_iov; + iovcnt = mh.msg_iovlen; - res = parse_readv_writev_bufs(args, iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); - if (unlikely(res != PPM_SUCCESS)) { - return res; + res = parse_readv_writev_bufs(args, iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(&compat_mh, (const void __user *)compat_ptr(val), sizeof(compat_mh)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + + /* + * data and size + */ + compat_iov = (const struct compat_iovec __user *)compat_ptr(compat_mh.msg_iov); + iovcnt = compat_mh.msg_iovlen; + + res = compat_parse_readv_writev_bufs(args, compat_iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); } +#endif + + if (unlikely(res != PPM_SUCCESS)) + return res; /* * tuple @@ -1732,11 +2383,13 @@ static int32_t f_sys_recvmsg_x(struct event_filler_arguments *args) /* * Get the fd */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; -#else - fd = (int)args->socketcall_args[0]; + if (!args->is_socketcall) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + fd = (int)val; + } +#ifndef UDIG + else + fd = (int)args->socketcall_args[0]; #endif /* @@ -1769,18 +2422,58 @@ static int32_t f_sys_recvmsg_x(struct event_filler_arguments *args) res = val_to_ring(args, (uint64_t)(unsigned long)targetbuf, size, - false); - if (unlikely(res != PPM_SUCCESS)) { + false, + 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } +int f_sys_creat_x(struct event_filler_arguments *args) +{ + unsigned long val; + unsigned long modes; + int res; + int64_t retval; + + /* + * fd + */ + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * name + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * mode + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &modes); + res = val_to_ring(args, open_modes_to_scap(O_CREAT, modes), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * dev + */ + res = val_to_ring(args, get_fd_dev(retval), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} -static int32_t f_sys_pipe_x(struct event_filler_arguments *args) +int f_sys_pipe_x(struct event_filler_arguments *args) { - int32_t res; + int res; int64_t retval; unsigned long val; int fds[2]; @@ -1790,254 +2483,185 @@ static int32_t f_sys_pipe_x(struct event_filler_arguments *args) * retval */ retval = (int64_t)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * fds */ - syscall_get_arguments(current, args->regs, 0, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); - if (unlikely(ppm_copy_from_user(fds, (const void __user *)val, sizeof(fds)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(fds, (const void __user *)val, sizeof(fds)))) + return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(fds, (const void __user *)compat_ptr(val), sizeof(fds)))) + return PPM_FAILURE_INVALID_USER_MEMORY; } +#endif - res = val_to_ring(args, fds[0], 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, fds[0], 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - res = val_to_ring(args, fds[1], 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, fds[1], 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - file = fget(fds[0]); val = 0; +#ifndef UDIG + file = fget(fds[0]); if (likely(file != NULL)) { +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) + val = file->f_path.dentry->d_inode->i_ino; +#else val = file->f_dentry->d_inode->i_ino; +#endif fput(file); } +#endif - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_eventfd_e(struct event_filler_arguments *args) +int f_sys_eventfd_e(struct event_filler_arguments *args) { - int32_t res; + int res; unsigned long val; /* * initval */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * flags * XXX not implemented yet */ - /* syscall_get_arguments(current, args->regs, 1, 1, &val); */ + /* syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); */ val = 0; - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static inline u16 shutdown_how_to_scap(unsigned long how) -{ - if (how == SHUT_RD) { - return PPM_SHUT_RD; - } else if (how == SHUT_WR) { - return SHUT_WR; - } else if (how == SHUT_RDWR) { - return SHUT_RDWR; - } else { - ASSERT(false); - return (u16)how; - } -} - -static int32_t f_sys_shutdown_e(struct event_filler_arguments *args) +int f_sys_shutdown_e(struct event_filler_arguments *args) { - int32_t res; + int res; unsigned long val; /* * fd */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 0, 1, &val); -#else - val = args->socketcall_args[0]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[0]; #endif - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * how */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 1, 1, &val); -#else - val = args->socketcall_args[1]; + if (!args->is_socketcall) + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifndef UDIG + else + val = args->socketcall_args[1]; #endif - res = val_to_ring(args, (unsigned long)shutdown_how_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { + + res = val_to_ring(args, (unsigned long)shutdown_how_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static inline u16 futex_op_to_scap(unsigned long op) -{ - u16 res = 0; - unsigned long flt_op = op & 127; - - if (flt_op == FUTEX_WAIT) { - res = PPM_FU_FUTEX_WAIT; - } else if (flt_op == FUTEX_WAKE) { - res = PPM_FU_FUTEX_WAKE; - } else if (flt_op == FUTEX_FD) { - res = PPM_FU_FUTEX_FD; - } else if (flt_op == FUTEX_REQUEUE) { - res = PPM_FU_FUTEX_REQUEUE; - } else if (flt_op == FUTEX_CMP_REQUEUE) { - res = PPM_FU_FUTEX_CMP_REQUEUE; - } else if (flt_op == FUTEX_WAKE_OP) { - res = PPM_FU_FUTEX_WAKE_OP; - } else if (flt_op == FUTEX_LOCK_PI) { - res = PPM_FU_FUTEX_LOCK_PI; - } else if (flt_op == FUTEX_UNLOCK_PI) { - res = PPM_FU_FUTEX_UNLOCK_PI; - } else if (flt_op == FUTEX_TRYLOCK_PI) { - res = PPM_FU_FUTEX_TRYLOCK_PI; - } else if (flt_op == FUTEX_WAIT_BITSET) { - res = PPM_FU_FUTEX_WAIT_BITSET; - } else if (flt_op == FUTEX_WAKE_BITSET) { - res = PPM_FU_FUTEX_WAKE_BITSET; - } else if (flt_op == FUTEX_WAIT_REQUEUE_PI) { - res = PPM_FU_FUTEX_WAIT_REQUEUE_PI; - } else if (flt_op == FUTEX_CMP_REQUEUE_PI) { - res = PPM_FU_FUTEX_CMP_REQUEUE_PI; - } - - if (op & FUTEX_PRIVATE_FLAG) { - res |= PPM_FU_FUTEX_PRIVATE_FLAG; - } - - if (op & FUTEX_CLOCK_REALTIME) { - res |= PPM_FU_FUTEX_CLOCK_REALTIME; - } - - return res; -} - -static int32_t f_sys_futex_e(struct event_filler_arguments *args) +int f_sys_futex_e(struct event_filler_arguments *args) { - int32_t res; + int res; unsigned long val; /* * addr */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * op */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - res = val_to_ring(args, (unsigned long)futex_op_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, (unsigned long)futex_op_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * val */ - syscall_get_arguments(current, args->regs, 2, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static inline uint64_t lseek_whence_to_scap(unsigned long whence) -{ - uint64_t res = 0; - - if (whence == SEEK_SET) { - res = PPM_SEEK_SET; - } else if (whence == SEEK_CUR) { - res = PPM_SEEK_CUR; - } else if (whence == SEEK_END) { - res = PPM_SEEK_END; - } - - return res; -} - -static int32_t f_sys_lseek_e(struct event_filler_arguments *args) +int f_sys_lseek_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; /* * fd */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * offset */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * whence */ - syscall_get_arguments(current, args->regs, 2, 1, &val); - res = val_to_ring(args, lseek_whence_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, lseek_whence_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_llseek_e(struct event_filler_arguments *args) +int f_sys_llseek_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; unsigned long oh; unsigned long ol; uint64_t offset; @@ -2045,97 +2669,42 @@ static int32_t f_sys_llseek_e(struct event_filler_arguments *args) /* * fd */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * offset * We build it by combining the offset_high and offset_low system call arguments */ - syscall_get_arguments(current, args->regs, 1, 1, &oh); - syscall_get_arguments(current, args->regs, 2, 1, &ol); + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &oh); + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &ol); offset = (((uint64_t)oh) << 32) + ((uint64_t)ol); - res = val_to_ring(args, offset, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, offset, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * whence */ - syscall_get_arguments(current, args->regs, 4, 1, &val); - res = val_to_ring(args, lseek_whence_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &val); + res = val_to_ring(args, lseek_whence_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -/* XXX this is very basic for the moment, we'll need to improve it */ -static inline u16 poll_events_to_scap(short revents) -{ - u16 res = 0; - - if (revents & POLLIN) { - res |= PPM_POLLIN; - } - - if (revents & PPM_POLLPRI) { - res |= PPM_POLLPRI; - } - - if (revents & POLLOUT) { - res |= PPM_POLLOUT; - } - - if (revents & POLLRDHUP) { - res |= PPM_POLLRDHUP; - } - - if (revents & POLLERR) { - res |= PPM_POLLERR; - } - - if (revents & POLLHUP) { - res |= PPM_POLLHUP; - } - - if (revents & POLLNVAL) { - res |= PPM_POLLNVAL; - } - - if (revents & POLLRDNORM) { - res |= PPM_POLLRDNORM; - } - - if (revents & POLLRDBAND) { - res |= PPM_POLLRDBAND; - } - - if (revents & POLLWRNORM) { - res |= PPM_POLLWRNORM; - } - - if (revents & POLLWRBAND) { - res |= PPM_POLLWRBAND; - } - - return res; -} - -static int32_t poll_parse_fds(struct event_filler_arguments *args, bool enter_event) +static int poll_parse_fds(struct event_filler_arguments *args, bool enter_event) { struct pollfd *fds; char *targetbuf; unsigned long val; unsigned long nfds; unsigned long fds_count; - uint32_t j; - uint32_t pos; + u32 j; + u32 pos; u16 flags; /* @@ -2143,23 +2712,30 @@ static int32_t poll_parse_fds(struct event_filler_arguments *args, bool enter_ev * * Get the number of fds */ - syscall_get_arguments(current, args->regs, 1, 1, &nfds); + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &nfds); /* * Check if we have enough space to store both the fd list * from user space and the temporary buffer to serialize to the ring */ - if (unlikely(sizeof(struct pollfd) * nfds + 2 + 10 * nfds > STR_STORAGE_SIZE)) { + if (unlikely(sizeof(struct pollfd) * nfds + 2 + 10 * nfds > STR_STORAGE_SIZE)) return PPM_FAILURE_BUFFER_FULL; - } /* Get the fds pointer */ - syscall_get_arguments(current, args->regs, 0, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); fds = (struct pollfd *)args->str_storage; - if (unlikely(ppm_copy_from_user(fds, (const void __user *)val, nfds * sizeof(struct pollfd)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(fds, (const void __user *)val, nfds * sizeof(struct pollfd)))) + return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(fds, (const void __user *)compat_ptr(val), nfds * sizeof(struct pollfd)))) + return PPM_FAILURE_INVALID_USER_MEMORY; } +#endif pos = 2; targetbuf = args->str_storage + nfds * sizeof(struct pollfd) + pos; @@ -2174,9 +2750,8 @@ static int32_t poll_parse_fds(struct event_filler_arguments *args, bool enter_ev * If it's an exit event, we copy only the fds that * returned something */ - if (!fds[j].revents) { + if (!fds[j].revents) continue; - } flags = poll_events_to_scap(fds[j].revents); } @@ -2189,157 +2764,363 @@ static int32_t poll_parse_fds(struct event_filler_arguments *args, bool enter_ev *(u16 *)(targetbuf) = (u16)fds_count; - return val_to_ring(args, (uint64_t)(unsigned long)targetbuf, pos, false); + return val_to_ring(args, (uint64_t)(unsigned long)targetbuf, pos, false, 0); } -static int32_t f_sys_poll_e(struct event_filler_arguments *args) +int f_sys_poll_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; res = poll_parse_fds(args, true); - if (unlikely(res != PPM_SUCCESS)) { + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * timeout */ - syscall_get_arguments(current, args->regs, 2, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_poll_x(struct event_filler_arguments *args) +static int timespec_parse(struct event_filler_arguments *args, unsigned long val) { - int64_t retval; - int32_t res; + uint64_t longtime; + char *targetbuf = args->str_storage; + struct timespec *tts = (struct timespec *)targetbuf; +#ifdef CONFIG_COMPAT + struct compat_timespec *compat_tts = (struct compat_timespec *)targetbuf; +#endif + int cfulen; /* - * res + * interval + * We copy the timespec structure and then convert it to a 64bit relative time */ - retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + cfulen = (int)ppm_copy_from_user(targetbuf, (void __user *)val, sizeof(*tts)); + if (unlikely(cfulen != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; - res = poll_parse_fds(args, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; + longtime = ((uint64_t)tts->tv_sec) * 1000000000 + tts->tv_nsec; +#ifdef CONFIG_COMPAT + } else { + cfulen = (int)ppm_copy_from_user(targetbuf, (void __user *)compat_ptr(val), sizeof(struct compat_timespec)); + if (unlikely(cfulen != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + longtime = ((uint64_t)compat_tts->tv_sec) * 1000000000 + compat_tts->tv_nsec; } +#endif - return add_sentinel(args); + return val_to_ring(args, longtime, 0, false, 0); } -static int32_t f_sys_openat_e(struct event_filler_arguments *args) +int f_sys_ppoll_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; - /* - * dirfd - */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - - if (val == AT_FDCWD) { - val = PPM_AT_FDCWD; - } - - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = poll_parse_fds(args, true); + if (res != PPM_SUCCESS) return res; - } /* - * name + * timeout */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - res = val_to_ring(args, val, 0, true); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + /* NULL timeout specified as 0xFFFFFF.... */ + if (val == (unsigned long)NULL) + res = val_to_ring(args, (uint64_t)(-1), 0, false, 0); + else + res = timespec_parse(args, val); + if (res != PPM_SUCCESS) return res; - } /* - * Flags - * Note that we convert them into the ppm portable representation before pushing them to the ring + * sigmask */ - syscall_get_arguments(current, args->regs, 2, 1, &val); - res = val_to_ring(args, open_flags_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + if (val != (unsigned long)NULL) + if (0 != ppm_copy_from_user(&val, (void __user *)val, sizeof(val))) + return PPM_FAILURE_INVALID_USER_MEMORY; - /* - * Mode - * XXX: at this time, mode decoding is not supported. We nonetheless return a value (zero) - * so the format of the event is ready for when we'll export the mode in the future. - * - * syscall_get_arguments(current, args->regs, 3, 1, &val); - */ - res = val_to_ring(args, 0, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, val, 0, false, 0); + if (res != PPM_SUCCESS) return res; - } return add_sentinel(args); } -#ifndef __x86_64__ -static int32_t f_sys_pread64_e(struct event_filler_arguments *args) +/* This is the same for poll() and ppoll() */ +int f_sys_poll_x(struct event_filler_arguments *args) { - unsigned long val; - unsigned long size; - int32_t res; - unsigned long pos0; - unsigned long pos1; - uint64_t pos64; + int64_t retval; + int res; /* - * fd + * res */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + retval = (int64_t)(long)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - /* + res = poll_parse_fds(args, false); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_mount_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + + /* + * Fix mount flags in arg 3. + * See http://lxr.free-electrons.com/source/fs/namespace.c?v=4.2#L2650 + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + if ((val & PPM_MS_MGC_MSK) == PPM_MS_MGC_VAL) + val &= ~PPM_MS_MGC_MSK; + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_openat_x(struct event_filler_arguments *args) +{ + unsigned long val; + unsigned long flags; + unsigned long modes; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * dirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * name + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * Flags + * Note that we convert them into the ppm portable representation before pushing them to the ring + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &flags); + res = val_to_ring(args, open_flags_to_scap(flags), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * mode + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &modes); + res = val_to_ring(args, open_modes_to_scap(flags, modes), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * dev + */ + res = val_to_ring(args, get_fd_dev(retval), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_unlinkat_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * dirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * name + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * flags + * Note that we convert them into the ppm portable representation before pushing them to the ring + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, unlinkat_flags_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_linkat_x(struct event_filler_arguments *args) +{ + unsigned long val; + unsigned long flags; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * olddir + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * oldpath + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newdir + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newpath + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * Flags + * Note that we convert them into the ppm portable representation before pushing them to the ring + */ + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &flags); + res = val_to_ring(args, linkat_flags_to_scap(flags), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +#ifndef _64BIT_ARGS_SINGLE_REGISTER +int f_sys_pread64_e(struct event_filler_arguments *args) +{ + unsigned long val; + unsigned long size; + int res; + unsigned long pos0; + unsigned long pos1; + uint64_t pos64; + + /* + * fd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* * size */ - syscall_get_arguments(current, args->regs, 2, 1, &size); - res = val_to_ring(args, size, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &size); + res = val_to_ring(args, size, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * pos */ - syscall_get_arguments(current, args->regs, 3, 1, &pos0); - syscall_get_arguments(current, args->regs, 4, 1, &pos1); +#if defined CONFIG_X86 + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &pos0); + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &pos1); +#elif defined CONFIG_ARM && CONFIG_AEABI + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &pos0); + syscall_get_arguments_deprecated(current, args->regs, 5, 1, &pos1); +#else + #error This architecture/abi not yet supported +#endif pos64 = merge_64(pos1, pos0); - res = val_to_ring(args, pos64, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, pos64, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -#endif /* __x86_64__ */ +#endif /* _64BIT_ARGS_SINGLE_REGISTER */ -static int32_t f_sys_pwrite64_e(struct event_filler_arguments *args) +#ifndef _64BIT_ARGS_SINGLE_REGISTER +int f_sys_pwrite64_e(struct event_filler_arguments *args) { unsigned long val; unsigned long size; - int32_t res; -#ifndef __x86_64__ + int res; +#ifndef _64BIT_ARGS_SINGLE_REGISTER unsigned long pos0; unsigned long pos1; uint64_t pos64; @@ -2348,51 +3129,59 @@ static int32_t f_sys_pwrite64_e(struct event_filler_arguments *args) /* * fd */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * size */ - syscall_get_arguments(current, args->regs, 2, 1, &size); - res = val_to_ring(args, size, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &size); + res = val_to_ring(args, size, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * pos * NOTE: this is a 64bit value, which means that on 32bit systems it uses two * separate registers that we need to merge. */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 3, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { +#ifdef _64BIT_ARGS_SINGLE_REGISTER + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } #else - syscall_get_arguments(current, args->regs, 3, 1, &pos0); - syscall_get_arguments(current, args->regs, 4, 1, &pos1); + #if defined CONFIG_X86 + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &pos0); + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &pos1); + #elif defined CONFIG_ARM && CONFIG_AEABI + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &pos0); + syscall_get_arguments_deprecated(current, args->regs, 5, 1, &pos1); + #else + #error This architecture/abi not yet supported + #endif + pos64 = merge_64(pos1, pos0); - res = val_to_ring(args, pos64, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, pos64, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } #endif return add_sentinel(args); } +#endif -static int32_t f_sys_readv_x(struct event_filler_arguments *args) +int f_sys_readv_preadv_x(struct event_filler_arguments *args) { unsigned long val; int64_t retval; - int32_t res; + int res; +#ifdef CONFIG_COMPAT + const struct compat_iovec __user *compat_iov; +#endif const struct iovec __user *iov; unsigned long iovcnt; @@ -2400,150 +3189,129 @@ static int32_t f_sys_readv_x(struct event_filler_arguments *args) * res */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * data and size */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - iov = (const struct iovec __user *)val; - syscall_get_arguments(current, args->regs, 2, 1, &iovcnt); - - res = parse_readv_writev_bufs(args, iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); - if (unlikely(res != PPM_SUCCESS)) { - return res; + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &iovcnt); + +#ifdef CONFIG_COMPAT + if (unlikely(args->compat)) { + compat_iov = (const struct compat_iovec __user *)compat_ptr(val); + res = compat_parse_readv_writev_bufs(args, compat_iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); + } else +#endif + { + iov = (const struct iovec __user *)val; + res = parse_readv_writev_bufs(args, iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); } + if (unlikely(res != PPM_SUCCESS)) + return res; return add_sentinel(args); } -static int32_t f_sys_writev_e(struct event_filler_arguments *args) +int f_sys_writev_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; +#ifdef CONFIG_COMPAT + const struct compat_iovec __user *compat_iov; +#endif const struct iovec __user *iov; unsigned long iovcnt; - unsigned int snaplen; /* * fd */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * size */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - iov = (const struct iovec __user *)val; - syscall_get_arguments(current, args->regs, 2, 1, &iovcnt); + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &iovcnt); /* - * Determine the snaplen by checking the fd type. - * (note: not implemeted yet) + * Copy the buffer */ - snaplen = g_snaplen; -#if 0 + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifdef CONFIG_COMPAT + if (unlikely(args->compat)) { + compat_iov = (const struct compat_iovec __user *)compat_ptr(val); + res = compat_parse_readv_writev_bufs(args, compat_iov, iovcnt, + args->consumer->snaplen, + PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); + } else +#endif { - int fd; - int err, fput_needed; - struct socket *sock; - - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; - - sock = ppm_sockfd_lookup_light(fd, &err, &fput_needed); - if (sock) { - snaplen = g_snaplen; - fput_light(sock->file, fput_needed); - } else { - snaplen = RW_SNAPLEN; - } + iov = (const struct iovec __user *)val; + res = parse_readv_writev_bufs(args, iov, iovcnt, args->consumer->snaplen, + PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); } -#endif - /* - * Copy the buffer - */ - res = parse_readv_writev_bufs(args, iov, iovcnt, snaplen, PRB_FLAG_PUSH_SIZE); - if (unlikely(res != PPM_SUCCESS)) { + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_writev_pwritev_x(struct event_filler_arguments *args) +int f_sys_writev_pwritev_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; int64_t retval; +#ifdef CONFIG_COMPAT + const struct compat_iovec __user *compat_iov; +#endif const struct iovec __user *iov; unsigned long iovcnt; - unsigned int snaplen; /* * res */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * data and size */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - iov = (const struct iovec __user *)val; - syscall_get_arguments(current, args->regs, 2, 1, &iovcnt); - - /* - * Determine the snaplen by checking the fd type. - * (note: not implemeted yet) - */ - snaplen = g_snaplen; -#if 0 - { - int fd; - int err, fput_needed; - struct socket *sock; + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &iovcnt); - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; - - sock = ppm_sockfd_lookup_light(fd, &err, &fput_needed); - if (sock) { - snaplen = g_snaplen; - fput_light(sock->file, fput_needed); - } else { - snaplen = RW_SNAPLEN; - } - } -#endif /* * Copy the buffer */ - res = parse_readv_writev_bufs(args, iov, iovcnt, snaplen, PRB_FLAG_PUSH_DATA); - if (unlikely(res != PPM_SUCCESS)) { - return res; + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifdef CONFIG_COMPAT + if (unlikely(args->compat)) { + compat_iov = (const struct compat_iovec __user *)compat_ptr(val); + res = compat_parse_readv_writev_bufs(args, compat_iov, iovcnt, args->consumer->snaplen, PRB_FLAG_PUSH_DATA | PRB_FLAG_IS_WRITE); + } else +#endif + { + iov = (const struct iovec __user *)val; + res = parse_readv_writev_bufs(args, iov, iovcnt, args->consumer->snaplen, PRB_FLAG_PUSH_DATA | PRB_FLAG_IS_WRITE); } + if (unlikely(res != PPM_SUCCESS)) + return res; return add_sentinel(args); } -#ifndef __x86_64__ -static int32_t f_sys_preadv_e(struct event_filler_arguments *args) +#ifndef _64BIT_ARGS_SINGLE_REGISTER +int f_sys_preadv64_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; unsigned long pos0; unsigned long pos1; uint64_t pos64; @@ -2551,246 +3319,154 @@ static int32_t f_sys_preadv_e(struct event_filler_arguments *args) /* * fd */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * pos */ - syscall_get_arguments(current, args->regs, 3, 1, &pos0); - syscall_get_arguments(current, args->regs, 4, 1, &pos1); - - pos64 = merge_64(pos1, pos0); - - res = val_to_ring(args, pos64, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - - return add_sentinel(args); -} -#endif /* __x86_64__ */ - -static int32_t f_sys_preadv_x(struct event_filler_arguments *args) -{ - unsigned long val; - int64_t retval; - int32_t res; - const struct iovec __user *iov; - unsigned long iovcnt; /* - * res + * Note that in preadv and pwritev have NO 64-bit arguments in the + * syscall (despite having one in the userspace API), so no alignment + * requirements apply here. For an overly-detailed discussion about + * this, see https://lwn.net/Articles/311630/ */ - retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &pos0); + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &pos1); - /* - * data and size - */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - iov = (const struct iovec __user *)val; - syscall_get_arguments(current, args->regs, 2, 1, &iovcnt); + pos64 = merge_64(pos1, pos0); - res = parse_readv_writev_bufs(args, iov, iovcnt, retval, PRB_FLAG_PUSH_ALL); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, pos64, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } +#endif /* _64BIT_ARGS_SINGLE_REGISTER */ -static int32_t f_sys_pwritev_e(struct event_filler_arguments *args) +int f_sys_pwritev_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; -#ifndef __x86_64__ + int res; +#ifndef _64BIT_ARGS_SINGLE_REGISTER unsigned long pos0; unsigned long pos1; uint64_t pos64; +#endif +#ifdef CONFIG_COMPAT + const struct compat_iovec __user *compat_iov; #endif const struct iovec __user *iov; unsigned long iovcnt; - unsigned int snaplen; /* * fd */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * size */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - iov = (const struct iovec __user *)val; - syscall_get_arguments(current, args->regs, 2, 1, &iovcnt); + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &iovcnt); /* - * Determine the snaplen by checking the fd type. - * (note: not implemeted yet) + * Copy the buffer */ - snaplen = g_snaplen; -#if 0 + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); +#ifdef CONFIG_COMPAT + if (unlikely(args->compat)) { + compat_iov = (const struct compat_iovec __user *)compat_ptr(val); + res = compat_parse_readv_writev_bufs(args, compat_iov, iovcnt, + args->consumer->snaplen, + PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); + } else +#endif { - int fd; - int err, fput_needed; - struct socket *sock; - - syscall_get_arguments(current, args->regs, 0, 1, &val); - fd = (int)val; - - sock = ppm_sockfd_lookup_light(fd, &err, &fput_needed); - if (sock) { - snaplen = g_snaplen; - fput_light(sock->file, fput_needed); - } else { - snaplen = RW_SNAPLEN; - } + iov = (const struct iovec __user *)val; + res = parse_readv_writev_bufs(args, iov, iovcnt, args->consumer->snaplen, + PRB_FLAG_PUSH_SIZE | PRB_FLAG_IS_WRITE); } -#endif - - /* - * Copy the buffer - */ - res = parse_readv_writev_bufs(args, iov, iovcnt, snaplen, PRB_FLAG_PUSH_SIZE); - if (unlikely(res != PPM_SUCCESS)) { + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * pos * NOTE: this is a 64bit value, which means that on 32bit systems it uses two * separate registers that we need to merge. */ -#ifdef __x86_64__ - syscall_get_arguments(current, args->regs, 3, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { +#ifdef _64BIT_ARGS_SINGLE_REGISTER + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } #else - syscall_get_arguments(current, args->regs, 3, 1, &pos0); - syscall_get_arguments(current, args->regs, 4, 1, &pos1); + /* + * Note that in preadv and pwritev have NO 64-bit arguments in the + * syscall (despite having one in the userspace API), so no alignment + * requirements apply here. For an overly-detailed discussion about + * this, see https://lwn.net/Articles/311630/ + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &pos0); + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &pos1); + pos64 = merge_64(pos1, pos0); - res = val_to_ring(args, pos64, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, pos64, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } #endif return add_sentinel(args); } -static int32_t f_sys_nanosleep_e(struct event_filler_arguments *args) +int f_sys_nanosleep_e(struct event_filler_arguments *args) { - int32_t res; - uint64_t longtime; unsigned long val; - char *targetbuf = args->str_storage; - struct timespec *tts = (struct timespec *)targetbuf; - int32_t cfulen; - - /* - * interval - * We copy the timespec structure and then convert it to a 64bit relative time - */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - - cfulen = (int32_t)ppm_copy_from_user(targetbuf, (void __user *)val, sizeof(struct timespec)); + int res; - if (unlikely(cfulen != 0)) { - return PPM_FAILURE_INVALID_USER_MEMORY; - } + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = timespec_parse(args, val); + if (unlikely(res != PPM_SUCCESS)) + return res; - longtime = ((uint64_t)tts->tv_sec) * 1000000000 + tts->tv_nsec; - - res = val_to_ring(args, longtime, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - - return add_sentinel(args); -} - -static inline u8 rlimit_resource_to_scap(unsigned long rresource) -{ - switch (rresource) { - case RLIMIT_CPU: - return PPM_RLIMIT_CPU; - case RLIMIT_FSIZE: - return PPM_RLIMIT_FSIZE; - case RLIMIT_DATA: - return PPM_RLIMIT_DATA; - case RLIMIT_STACK: - return PPM_RLIMIT_STACK; - case RLIMIT_CORE: - return PPM_RLIMIT_CORE; - case RLIMIT_RSS: - return PPM_RLIMIT_RSS; - case RLIMIT_NPROC: - return PPM_RLIMIT_NPROC; - case RLIMIT_NOFILE: - return PPM_RLIMIT_NOFILE; - case RLIMIT_MEMLOCK: - return PPM_RLIMIT_MEMLOCK; - case RLIMIT_AS: - return PPM_RLIMIT_AS; - case RLIMIT_LOCKS: - return PPM_RLIMIT_LOCKS; - case RLIMIT_SIGPENDING: - return PPM_RLIMIT_SIGPENDING; - case RLIMIT_MSGQUEUE: - return PPM_RLIMIT_MSGQUEUE; - case RLIMIT_NICE: - return PPM_RLIMIT_NICE; - case RLIMIT_RTPRIO: - return PPM_RLIMIT_RTPRIO; - case RLIMIT_RTTIME: - return PPM_RLIMIT_RTTIME; - default: - ASSERT(false); - return PPM_RLIMIT_UNKNOWN; - } + return add_sentinel(args); } -static int32_t f_sys_getrlimit_setrlimit_e(struct event_filler_arguments *args) +int f_sys_getrlimit_setrlimit_e(struct event_filler_arguments *args) { u8 ppm_resource; unsigned long val; - int32_t res; + int res; /* * resource */ - syscall_get_arguments(current, args->regs, 0, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); ppm_resource = rlimit_resource_to_scap(val); - res = val_to_ring(args, (uint64_t)ppm_resource, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, (uint64_t)ppm_resource, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_getrlimit_setrlrimit_x(struct event_filler_arguments *args) +int f_sys_getrlimit_setrlrimit_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; int64_t retval; struct rlimit rl; +#ifdef CONFIG_COMPAT + struct compat_rlimit compat_rl; +#endif int64_t cur; int64_t max; @@ -2798,23 +3474,31 @@ static int32_t f_sys_getrlimit_setrlrimit_x(struct event_filler_arguments *args) * res */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * Copy the user structure and extract cur and max */ if (retval >= 0 || args->event_type == PPME_SYSCALL_SETRLIMIT_X) { - syscall_get_arguments(current, args->regs, 1, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); - if (unlikely(ppm_copy_from_user(&rl, (const void __user *)val, sizeof(struct rlimit)))) { - return PPM_FAILURE_INVALID_USER_MEMORY; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&rl, (const void __user *)val, sizeof(struct rlimit)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + cur = rl.rlim_cur; + max = rl.rlim_max; +#ifdef CONFIG_COMPAT + } else { + if (unlikely(ppm_copy_from_user(&compat_rl, (const void __user *)compat_ptr(val), sizeof(struct compat_rlimit)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + cur = compat_rl.rlim_cur; + max = compat_rl.rlim_max; } - - cur = rl.rlim_cur; - max = rl.rlim_max; +#endif } else { cur = -1; max = -1; @@ -2823,59 +3507,58 @@ static int32_t f_sys_getrlimit_setrlrimit_x(struct event_filler_arguments *args) /* * cur */ - res = val_to_ring(args, cur, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, cur, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * max */ - res = val_to_ring(args, max, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, max, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_prlimit_e(struct event_filler_arguments *args) +int f_sys_prlimit_e(struct event_filler_arguments *args) { u8 ppm_resource; unsigned long val; - int32_t res; + int res; /* * pid */ - syscall_get_arguments(current, args->regs, 0, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * resource */ - syscall_get_arguments(current, args->regs, 1, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); ppm_resource = rlimit_resource_to_scap(val); - res = val_to_ring(args, (uint64_t)ppm_resource, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, (uint64_t)ppm_resource, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -static int32_t f_sys_prlimit_x(struct event_filler_arguments *args) +int f_sys_prlimit_x(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; int64_t retval; struct rlimit rl; +#ifdef CONFIG_COMPAT + struct compat_rlimit compat_rl; +#endif int64_t newcur; int64_t newmax; int64_t oldcur; @@ -2885,81 +3568,106 @@ static int32_t f_sys_prlimit_x(struct event_filler_arguments *args) * res */ retval = (int64_t)(long)syscall_get_return_value(current, args->regs); - res = val_to_ring(args, retval, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * Copy the user structure and extract cur and max */ if (retval >= 0) { - syscall_get_arguments(current, args->regs, 2, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); - if (unlikely(ppm_copy_from_user(&rl, (const void __user *)val, sizeof(struct rlimit)))) { - newcur = -1; - newmax = -1; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&rl, (const void __user *)val, sizeof(struct rlimit)))) { + newcur = -1; + newmax = -1; + } else { + newcur = rl.rlim_cur; + newmax = rl.rlim_max; + } +#ifdef CONFIG_COMPAT } else { - newcur = rl.rlim_cur; - newmax = rl.rlim_max; + if (unlikely(ppm_copy_from_user(&compat_rl, (const void __user *)val, sizeof(struct compat_rlimit)))) { + newcur = -1; + newmax = -1; + } else { + newcur = compat_rl.rlim_cur; + newmax = compat_rl.rlim_max; + } } +#endif } else { newcur = -1; newmax = -1; } - syscall_get_arguments(current, args->regs, 3, 1, &val); + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); - if (unlikely(ppm_copy_from_user(&rl, (const void __user *)val, sizeof(struct rlimit)))) { - oldcur = -1; - oldmax = -1; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + if (unlikely(ppm_copy_from_user(&rl, (const void __user *)val, sizeof(struct rlimit)))) { + oldcur = -1; + oldmax = -1; + } else { + oldcur = rl.rlim_cur; + oldmax = rl.rlim_max; + } +#ifdef CONFIG_COMPAT } else { - oldcur = rl.rlim_cur; - oldmax = rl.rlim_max; + if (unlikely(ppm_copy_from_user(&compat_rl, (const void __user *)val, sizeof(struct compat_rlimit)))) { + oldcur = -1; + oldmax = -1; + } else { + oldcur = compat_rl.rlim_cur; + oldmax = compat_rl.rlim_max; + } } +#endif /* * newcur */ - res = val_to_ring(args, newcur, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, newcur, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * newmax */ - res = val_to_ring(args, newmax, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, newmax, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * oldcur */ - res = val_to_ring(args, oldcur, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, oldcur, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* * oldmax */ - res = val_to_ring(args, oldmax, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, oldmax, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } #ifdef CAPTURE_CONTEXT_SWITCHES -#include -static int32_t f_sched_switch_e(struct event_filler_arguments *args) +int f_sched_switch_e(struct event_filler_arguments *args) { - int32_t res; -/* uint64_t steal; */ + int res; + long total_vm = 0; + long total_rss = 0; + long swap = 0; + struct mm_struct *mm = NULL; if (args->sched_prev == NULL || args->sched_next == NULL) { ASSERT(false); @@ -2969,178 +3677,1409 @@ static int32_t f_sched_switch_e(struct event_filler_arguments *args) /* * next */ - res = val_to_ring(args, args->sched_next->pid, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + res = val_to_ring(args, args->sched_next->pid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * pgft_maj + */ + res = val_to_ring(args, args->sched_prev->maj_flt, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; + + /* + * pgft_min + */ + res = val_to_ring(args, args->sched_prev->min_flt, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + mm = args->sched_prev->mm; + if (mm) { + total_vm = mm->total_vm << (PAGE_SHIFT-10); + total_rss = ppm_get_mm_rss(mm) << (PAGE_SHIFT-10); + swap = ppm_get_mm_swap(mm) << (PAGE_SHIFT-10); } -/* - // - // steal - // + /* + * vm_size + */ + res = val_to_ring(args, total_vm, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vm_rss + */ + res = val_to_ring(args, total_rss, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vm_swap + */ + res = val_to_ring(args, swap, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + +#if 0 + /* + * steal + */ steal = cputime64_to_clock_t(kcpustat_this_cpu->cpustat[CPUTIME_STEAL]); res = val_to_ring(args, steal, 0, false); - if(unlikely(res != PPM_SUCCESS)) - { + if (unlikely(res != PPM_SUCCESS)) return res; - } -*/ +#endif + return add_sentinel(args); } +#endif /* CAPTURE_CONTEXT_SWITCHES */ -#if 0 -static int32_t f_sched_switchex_e(struct event_filler_arguments *args) +int f_sched_drop(struct event_filler_arguments *args) { - int32_t res; + int res; - if (args->sched_prev == NULL || args->sched_next == NULL) { - ASSERT(false); - return -1; - } + /* + * ratio + */ + res = val_to_ring(args, args->consumer->sampling_ratio, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_fcntl_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; - /* */ - /* next */ - /* */ - res = val_to_ring(args, args->sched_next->pid, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + /* + * fd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - /* */ - /* pgft_maj */ - /* */ - res = val_to_ring(args, args->sched_prev->maj_flt, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + /* + * cmd + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, fcntl_cmd_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; + + return add_sentinel(args); +} + +static inline int parse_ptrace_addr(struct event_filler_arguments *args, u16 request) +{ + unsigned long val; + uint64_t dst; + u8 idx; + + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + switch (request) { + default: + idx = PPM_PTRACE_IDX_UINT64; + dst = (uint64_t)val; } - /* */ - /* pgft_min */ - /* */ - res = val_to_ring(args, args->sched_prev->min_flt, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; + return val_to_ring(args, dst, 0, false, idx); +} + +static inline int parse_ptrace_data(struct event_filler_arguments *args, u16 request) +{ + unsigned long val; + unsigned long len; + uint64_t dst; + u8 idx; + + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + switch (request) { + case PPM_PTRACE_PEEKTEXT: + case PPM_PTRACE_PEEKDATA: + case PPM_PTRACE_PEEKUSR: + idx = PPM_PTRACE_IDX_UINT64; +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + len = ppm_copy_from_user(&dst, (const void __user *)val, sizeof(long)); +#ifdef CONFIG_COMPAT + } else { + len = ppm_copy_from_user(&dst, (const void __user *)compat_ptr(val), sizeof(compat_long_t)); + } +#endif + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + break; + case PPM_PTRACE_CONT: + case PPM_PTRACE_SINGLESTEP: + case PPM_PTRACE_DETACH: + case PPM_PTRACE_SYSCALL: + idx = PPM_PTRACE_IDX_SIGTYPE; + dst = (uint64_t)val; + break; + case PPM_PTRACE_ATTACH: + case PPM_PTRACE_TRACEME: + case PPM_PTRACE_POKETEXT: + case PPM_PTRACE_POKEDATA: + case PPM_PTRACE_POKEUSR: + default: + idx = PPM_PTRACE_IDX_UINT64; + dst = (uint64_t)val; + break; } - /* */ - /* next_pgft_maj */ - /* */ - res = val_to_ring(args, args->sched_next->maj_flt, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + return val_to_ring(args, dst, 0, false, idx); +} + +int f_sys_ptrace_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + + /* + * request + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, ptrace_requests_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } - /* */ - /* next_pgft_min */ - /* */ - res = val_to_ring(args, args->sched_next->min_flt, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + /* + * pid + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } -#endif /* 0 */ -#endif /* CAPTURE_CONTEXT_SWITCHES */ -static int32_t f_sched_drop(struct event_filler_arguments *args) +int f_sys_ptrace_x(struct event_filler_arguments *args) { - int32_t res; + unsigned long val; + int64_t retval; + u16 request; + int res; /* - * next + * res */ - res = val_to_ring(args, g_sampling_ratio, 0, false); - if (unlikely(res != PPM_SUCCESS)) { - return res; - } - - return add_sentinel(args); -} - -static inline u8 fcntl_cmd_to_scap(unsigned long cmd) -{ - switch (cmd) { - case F_DUPFD: - return PPM_FCNTL_F_DUPFD; - case F_GETFD: - return PPM_FCNTL_F_GETFD; - case F_SETFD: - return PPM_FCNTL_F_SETFD; - case F_GETFL: - return PPM_FCNTL_F_GETFL; - case F_SETFL: - return PPM_FCNTL_F_SETFL; - case F_GETLK: - return PPM_FCNTL_F_GETLK; - case F_SETLK: - return PPM_FCNTL_F_SETLK; - case F_SETLKW: - return PPM_FCNTL_F_SETLKW; - case F_SETOWN: - return PPM_FCNTL_F_SETOWN; - case F_GETOWN: - return PPM_FCNTL_F_GETOWN; - case F_SETSIG: - return PPM_FCNTL_F_SETSIG; - case F_GETSIG: - return PPM_FCNTL_F_GETSIG; - case F_GETLK64: - return PPM_FCNTL_F_GETLK64; - case F_SETLK64: - return PPM_FCNTL_F_SETLK64; - case F_SETLKW64: - return PPM_FCNTL_F_SETLKW64; - case F_SETOWN_EX: - return PPM_FCNTL_F_SETOWN_EX; - case F_GETOWN_EX: - return PPM_FCNTL_F_GETOWN_EX; - case F_SETLEASE: - return PPM_FCNTL_F_SETLEASE; - case F_GETLEASE: - return PPM_FCNTL_F_GETLEASE; - case F_CANCELLK: - return PPM_FCNTL_F_CANCELLK; - case F_DUPFD_CLOEXEC: - return PPM_FCNTL_F_DUPFD_CLOEXEC; - case F_NOTIFY: - return PPM_FCNTL_F_NOTIFY; -#ifdef F_SETPIPE_SZ - case F_SETPIPE_SZ: - return PPM_FCNTL_F_SETPIPE_SZ; -#endif -#ifdef F_GETPIPE_SZ - case F_GETPIPE_SZ: - return PPM_FCNTL_F_GETPIPE_SZ; + retval = (int64_t)(long)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + if (retval < 0) { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); + } + + /* + * request + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + request = ptrace_requests_to_scap(val); + + res = parse_ptrace_addr(args, request); + if (unlikely(res != PPM_SUCCESS)) + return res; + + res = parse_ptrace_data(args, request); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_brk_munmap_mmap_x(struct event_filler_arguments *args) +{ + int64_t retval; + int res = 0; +#ifndef UDIG + struct mm_struct *mm = current->mm; #endif - default: - ASSERT(false); - return PPM_FCNTL_UNKNOWN; + long total_vm = 0; + long total_rss = 0; + long swap = 0; + + retval = (int64_t)(long)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + +#ifndef UDIG + if (mm) { + total_vm = mm->total_vm << (PAGE_SHIFT-10); + total_rss = ppm_get_mm_rss(mm) << (PAGE_SHIFT-10); + swap = ppm_get_mm_swap(mm) << (PAGE_SHIFT-10); } +#endif + + /* + * vm_size + */ + res = val_to_ring(args, total_vm, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vm_rss + */ + res = val_to_ring(args, total_rss, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * vm_swap + */ + res = val_to_ring(args, swap, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); } -static int32_t f_sched_fcntl_e(struct event_filler_arguments *args) +int f_sys_mmap_e(struct event_filler_arguments *args) { unsigned long val; - int32_t res; + int res; + + /* + * addr + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * length + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * prot + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, prot_flags_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * flags + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + res = val_to_ring(args, mmap_flags_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; /* * fd */ - syscall_get_arguments(current, args->regs, 0, 1, &val); - res = val_to_ring(args, val, 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } /* - * cmd + * offset/pgoffset */ - syscall_get_arguments(current, args->regs, 1, 1, &val); - res = val_to_ring(args, fcntl_cmd_to_scap(val), 0, false); - if (unlikely(res != PPM_SUCCESS)) { + syscall_get_arguments_deprecated(current, args->regs, 5, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_renameat_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * olddirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * oldpath + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newdirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newpath + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_renameat2_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * olddirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * oldpath + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newdirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newpath + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + + /* + * flags + */ + syscall_get_arguments_deprecated(current, args->regs, 4, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_symlinkat_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * oldpath + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newdirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * newpath + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_procexit_e(struct event_filler_arguments *args) +{ + int res; + +#ifndef UDIG + if (args->sched_prev == NULL) { + ASSERT(false); + return -1; + } +#endif + + /* + * status + */ +#ifndef UDIG + res = val_to_ring(args, args->sched_prev->exit_code, 0, false, 0); +#else + res = val_to_ring(args, 0, 0, false, 0); +#endif + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_sendfile_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + off_t offset; + + /* + * out_fd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * in_fd + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * offset + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + + if (val != 0) { +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + res = ppm_copy_from_user(&offset, (void *)val, sizeof(off_t)); +#ifdef CONFIG_COMPAT + } else { + res = ppm_copy_from_user(&offset, (void *)compat_ptr(val), sizeof(compat_off_t)); + } +#endif + if (unlikely(res)) + val = 0; + else + val = offset; + } + + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * size + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_sendfile_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + off_t offset; + + /* + * res + */ + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * offset + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + + if (val != 0) { +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + res = ppm_copy_from_user(&offset, (void *)val, sizeof(off_t)); +#ifdef CONFIG_COMPAT + } else { + res = ppm_copy_from_user(&offset, (void *)compat_ptr(val), sizeof(compat_off_t)); + } +#endif + if (unlikely(res)) + val = 0; + else + val = offset; + } + + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_quotactl_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + uint32_t id; + uint8_t quota_fmt; + uint16_t cmd; + + /* + * extract cmd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + cmd = quotactl_cmd_to_scap(val); + res = val_to_ring(args, cmd, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * extract type + */ + res = val_to_ring(args, quotactl_type_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * extract id + */ + id = 0; + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + if ((cmd == PPM_Q_GETQUOTA) || + (cmd == PPM_Q_SETQUOTA) || + (cmd == PPM_Q_XGETQUOTA) || + (cmd == PPM_Q_XSETQLIM)) { + /* + * in this case id represent an userid or groupid so add it + */ + id = val; + } + res = val_to_ring(args, id, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * extract quota_fmt from id + */ + quota_fmt = PPM_QFMT_NOT_USED; + if (cmd == PPM_Q_QUOTAON) + quota_fmt = quotactl_fmt_to_scap(val); + + res = val_to_ring(args, quota_fmt, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_quotactl_x(struct event_filler_arguments *args) +{ + unsigned long val, len; + int res; + int64_t retval; + uint16_t cmd; + struct if_dqblk dqblk; + struct if_dqinfo dqinfo; + uint32_t quota_fmt_out; + + const char empty_string[] = ""; + + /* + * extract cmd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + cmd = quotactl_cmd_to_scap(val); + + /* + * return value + */ + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * Add special + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * get addr + */ + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + + /* + * get quotafilepath only for QUOTAON + */ + if (cmd == PPM_Q_QUOTAON) + res = val_to_ring(args, val, 0, true, 0); + else + res = val_to_ring(args, (unsigned long)empty_string, 0, false, 0); + + if (unlikely(res != PPM_SUCCESS)) + return res; + + + /* + * dqblk fields if present + */ + dqblk.dqb_valid = 0; + if ((cmd == PPM_Q_GETQUOTA) || (cmd == PPM_Q_SETQUOTA)) { + len = ppm_copy_from_user(&dqblk, (void *)val, sizeof(struct if_dqblk)); + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + } + if (dqblk.dqb_valid & QIF_BLIMITS) { + res = val_to_ring(args, dqblk.dqb_bhardlimit, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + res = val_to_ring(args, dqblk.dqb_bsoftlimit, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + if (dqblk.dqb_valid & QIF_SPACE) { + res = val_to_ring(args, dqblk.dqb_curspace, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + if (dqblk.dqb_valid & QIF_ILIMITS) { + res = val_to_ring(args, dqblk.dqb_ihardlimit, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + res = val_to_ring(args, dqblk.dqb_isoftlimit, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + if (dqblk.dqb_valid & QIF_BTIME) { + res = val_to_ring(args, dqblk.dqb_btime, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + if (dqblk.dqb_valid & QIF_ITIME) { + res = val_to_ring(args, dqblk.dqb_itime, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + /* + * dqinfo fields if present + */ + dqinfo.dqi_valid = 0; + if ((cmd == PPM_Q_GETINFO) || (cmd == PPM_Q_SETINFO)) { + len = ppm_copy_from_user(&dqinfo, (void *)val, sizeof(struct if_dqinfo)); + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + } + + if (dqinfo.dqi_valid & IIF_BGRACE) { + res = val_to_ring(args, dqinfo.dqi_bgrace, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + if (dqinfo.dqi_valid & IIF_IGRACE) { + res = val_to_ring(args, dqinfo.dqi_igrace, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + if (dqinfo.dqi_valid & IIF_FLAGS) { + res = val_to_ring(args, dqinfo.dqi_flags, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } else { + res = val_to_ring(args, 0, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + + quota_fmt_out = PPM_QFMT_NOT_USED; + if (cmd == PPM_Q_GETFMT) { + len = ppm_copy_from_user("a_fmt_out, (void *)val, sizeof(uint32_t)); + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + quota_fmt_out = quotactl_fmt_to_scap(quota_fmt_out); + } + res = val_to_ring(args, quota_fmt_out, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_sysdigevent_e(struct event_filler_arguments *args) +{ + int res; + + /* + * event_type + */ + res = val_to_ring(args, (unsigned long)args->sched_prev, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * event_data + */ + res = val_to_ring(args, (unsigned long)args->sched_next, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_getresuid_and_gid_x(struct event_filler_arguments *args) +{ + int res; + unsigned long val, len; + uint32_t uid; + int16_t retval; + + /* + * return value + */ + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * ruid + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); +#ifdef CONFIG_COMPAT + if (!args->compat) { +#endif + len = ppm_copy_from_user(&uid, (void *)val, sizeof(uint32_t)); +#ifdef CONFIG_COMPAT + } else { + len = ppm_copy_from_user(&uid, (void *)compat_ptr(val), sizeof(uint32_t)); + } +#endif + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = val_to_ring(args, uid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * euid + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + len = ppm_copy_from_user(&uid, (void *)val, sizeof(uint32_t)); + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = val_to_ring(args, uid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * suid + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + len = ppm_copy_from_user(&uid, (void *)val, sizeof(uint32_t)); + if (unlikely(len != 0)) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = val_to_ring(args, uid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_flock_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + u32 flags; + + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + flags = flock_flags_to_scap(val); + res = val_to_ring(args, flags, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_setns_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + u32 flags; + + /* + * parse fd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * get type, parse as clone flags as it's a subset of it + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + flags = clone_flags_to_scap(val); + res = val_to_ring(args, flags, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_unshare_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + u32 flags; + + /* + * get type, parse as clone flags as it's a subset of it + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + flags = clone_flags_to_scap(val); + res = val_to_ring(args, flags, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +#ifdef CAPTURE_SIGNAL_DELIVERIES +int f_sys_signaldeliver_e(struct event_filler_arguments *args) +{ + int res; + + /* + * source pid + */ + res = val_to_ring(args, args->spid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * destination pid + */ + res = val_to_ring(args, args->dpid, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * signal number + */ + res = val_to_ring(args, args->signo, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} +#endif + +#ifdef CAPTURE_PAGE_FAULTS +int f_sys_pagefault_e(struct event_filler_arguments *args) +{ + int res; + + res = val_to_ring(args, args->fault_data.address, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + res = val_to_ring(args, args->fault_data.regs->ip, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + res = val_to_ring(args, pf_flags_to_scap(args->fault_data.error_code), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} +#endif + +int f_cpu_hotplug_e(struct event_filler_arguments *args) +{ + int res; + + /* + * cpu + */ + res = val_to_ring(args, (uint64_t)args->sched_prev, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * action + */ + res = val_to_ring(args, (uint64_t)args->sched_next, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_semop_x(struct event_filler_arguments *args) +{ + unsigned long nsops; + int res; + int64_t retval; + struct sembuf *ptr; + + /* + * return value + */ + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * nsops + * actually this could be read in the enter function but + * we also need to know the value to access the sembuf structs + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &nsops); + res = val_to_ring(args, nsops, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * sembuf + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, (unsigned long *) &ptr); + + if (nsops && ptr) { + /* max length of sembuf array in g_event_info = 2 */ + const unsigned max_nsops = 2; + unsigned j; + + for (j = 0; j < max_nsops; j++) { + struct sembuf sops = {0, 0, 0}; + + if (nsops--) + if (unlikely(ppm_copy_from_user(&sops, (void *)&ptr[j], sizeof(struct sembuf)))) + return PPM_FAILURE_INVALID_USER_MEMORY; + + res = val_to_ring(args, sops.sem_num, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + res = val_to_ring(args, sops.sem_op, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + res = val_to_ring(args, semop_flags_to_scap(sops.sem_flg), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + } + } + + return add_sentinel(args); +} + +int f_sys_semget_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + + /* + * key + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * nsems + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * semflg + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, semget_flags_to_scap(val), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_semctl_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + + /* + * semid + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * semnum + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * cmd + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, semctl_cmd_to_scap(val), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * optional argument semun/val + */ + if (val == SETVAL) + syscall_get_arguments_deprecated(current, args->regs, 3, 1, &val); + else + val = 0; + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_access_e(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + + /* + * mode + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, access_flags_to_scap(val), 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_bpf_x(struct event_filler_arguments *args) +{ + int64_t retval; + unsigned long cmd; + int res; + + /* + * res, if failure or depending on cmd + */ + retval = (int64_t)(long)syscall_get_return_value(current, args->regs); + if (retval < 0) { + res = val_to_ring(args, retval, 0, false, PPM_BPF_IDX_RES); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); + } + /* + * fd, depending on cmd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &cmd); +#ifdef UDIG + if(0) +#else /* UDIG */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) + if(cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD) +#else + if(0) +#endif +#endif /* UDIG */ + { + res = val_to_ring(args, retval, 0, false, PPM_BPF_IDX_FD); + } + else + { + res = val_to_ring(args, retval, 0, false, PPM_BPF_IDX_RES); + } + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_mkdirat_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * dirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * path + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * mode + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_fchmodat_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * dirfd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + if ((int)val == AT_FDCWD) + val = PPM_AT_FDCWD; + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * filename + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * mode + */ + syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val); + res = val_to_ring(args, chmod_mode_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_chmod_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * filename + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + res = val_to_ring(args, val, 0, true, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * mode + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, chmod_mode_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + return add_sentinel(args); +} + +int f_sys_fchmod_x(struct event_filler_arguments *args) +{ + unsigned long val; + int res; + int64_t retval; + + retval = (int64_t)syscall_get_return_value(current, args->regs); + res = val_to_ring(args, retval, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * fd + */ + syscall_get_arguments_deprecated(current, args->regs, 0, 1, &val); + + res = val_to_ring(args, val, 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) + return res; + + /* + * mode + */ + syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val); + res = val_to_ring(args, chmod_mode_to_scap(val), 0, false, 0); + if (unlikely(res != PPM_SUCCESS)) return res; - } return add_sentinel(args); } diff --git a/driver/ppm_fillers.h b/driver/ppm_fillers.h new file mode 100644 index 0000000000..5876ac54d2 --- /dev/null +++ b/driver/ppm_fillers.h @@ -0,0 +1,131 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ + +#ifndef PPM_FILLERS_H_ +#define PPM_FILLERS_H_ + +/* This is described in syscall(2). Some syscalls take 64-bit arguments. On + * arches that have 64-bit registers, these arguments are shipped in a register. + * On 32-bit arches, however, these are split between two consecutive registers, + * with some alignment requirements. Some require an odd/even pair while some + * others require even/odd. For now I assume they all do what x86_32 does, and + * we can handle the rest when we port those. + */ +#ifdef __KERNEL__ +#ifdef CONFIG_64BIT +#define _64BIT_ARGS_SINGLE_REGISTER +#endif /* CONFIG_64BIT */ +#else +#ifdef __x86_64__ +#define _64BIT_ARGS_SINGLE_REGISTER +#endif /* __x86_64__ */ +#endif /* __KERNEL__ */ + +#define FILLER_LIST_MAPPER(FN) \ + FN(sys_autofill) \ + FN(sys_generic) \ + FN(sys_empty) \ + FN(sys_single) \ + FN(sys_single_x) \ + FN(sys_open_x) \ + FN(sys_read_x) \ + FN(sys_write_x) \ + FN(sys_execve_e) \ + FN(proc_startupdate) \ + FN(proc_startupdate_2) \ + FN(proc_startupdate_3) \ + FN(sys_socketpair_x) \ + FN(sys_setsockopt_x) \ + FN(sys_getsockopt_x) \ + FN(sys_connect_x) \ + FN(sys_accept4_e) \ + FN(sys_accept_x) \ + FN(sys_send_e) \ + FN(sys_send_x) \ + FN(sys_sendto_e) \ + FN(sys_sendmsg_e) \ + FN(sys_sendmsg_x) \ + FN(sys_recv_x) \ + FN(sys_recvfrom_x) \ + FN(sys_recvmsg_x) \ + FN(sys_recvmsg_x_2) \ + FN(sys_shutdown_e) \ + FN(sys_creat_x) \ + FN(sys_pipe_x) \ + FN(sys_eventfd_e) \ + FN(sys_futex_e) \ + FN(sys_lseek_e) \ + FN(sys_llseek_e) \ + FN(sys_socket_bind_x) \ + FN(sys_poll_e) \ + FN(sys_poll_x) \ + FN(sys_pread64_e) \ + FN(sys_preadv64_e) \ + FN(sys_writev_e) \ + FN(sys_pwrite64_e) \ + FN(sys_readv_preadv_x) \ + FN(sys_writev_pwritev_x) \ + FN(sys_pwritev_e) \ + FN(sys_nanosleep_e) \ + FN(sys_getrlimit_setrlimit_e) \ + FN(sys_getrlimit_setrlrimit_x) \ + FN(sys_prlimit_e) \ + FN(sys_prlimit_x) \ + FN(sched_switch_e) \ + FN(sched_drop) \ + FN(sys_fcntl_e) \ + FN(sys_ptrace_e) \ + FN(sys_ptrace_x) \ + FN(sys_mmap_e) \ + FN(sys_brk_munmap_mmap_x) \ + FN(sys_renameat_x) \ + FN(sys_renameat2_x) \ + FN(sys_symlinkat_x) \ + FN(sys_procexit_e) \ + FN(sys_sendfile_e) \ + FN(sys_sendfile_x) \ + FN(sys_quotactl_e) \ + FN(sys_quotactl_x) \ + FN(sys_sysdigevent_e) \ + FN(sys_getresuid_and_gid_x) \ + FN(sys_signaldeliver_e) \ + FN(sys_pagefault_e) \ + FN(sys_setns_e) \ + FN(sys_unshare_e) \ + FN(sys_flock_e) \ + FN(cpu_hotplug_e) \ + FN(sys_semop_x) \ + FN(sys_semget_e) \ + FN(sys_semctl_e) \ + FN(sys_ppoll_e) \ + FN(sys_mount_e) \ + FN(sys_access_e) \ + FN(sys_socket_x) \ + FN(sys_bpf_x) \ + FN(sys_unlinkat_x) \ + FN(sys_fchmodat_x) \ + FN(sys_chmod_x) \ + FN(sys_fchmod_x) \ + FN(sys_mkdirat_x) \ + FN(sys_openat_x) \ + FN(sys_linkat_x) \ + FN(terminate_filler) + +#define FILLER_ENUM_FN(x) PPM_FILLER_##x, +enum ppm_filler_id { + FILLER_LIST_MAPPER(FILLER_ENUM_FN) + PPM_FILLER_MAX +}; +#undef FILLER_ENUM_FN + +#define FILLER_PROTOTYPE_FN(x) int f_##x(struct event_filler_arguments *args); +FILLER_LIST_MAPPER(FILLER_PROTOTYPE_FN) +#undef FILLER_PROTOTYPE_FN + +#endif /* PPM_FILLERS_H_ */ diff --git a/driver/ppm_flag_helpers.h b/driver/ppm_flag_helpers.h new file mode 100644 index 0000000000..4b48c858fe --- /dev/null +++ b/driver/ppm_flag_helpers.h @@ -0,0 +1,1354 @@ +/* + +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. + +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. + +*/ + +#ifndef PPM_FLAG_HELPERS_H_ +#define PPM_FLAG_HELPERS_H_ +#ifndef UDIG +#include +#include +#include +#include "ppm.h" +#endif + +#define PPM_MS_MGC_MSK 0xffff0000 +#define PPM_MS_MGC_VAL 0xC0ED0000 + +static __always_inline uint32_t open_flags_to_scap(unsigned long flags) +{ + uint32_t res = 0; + + switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) { + case O_WRONLY: + res |= PPM_O_WRONLY; + break; + case O_RDWR: + res |= PPM_O_RDWR; + break; + default: + res |= PPM_O_RDONLY; + break; + } + + if (flags & O_CREAT) + res |= PPM_O_CREAT; +#ifdef O_TMPFILE + if (flags & O_TMPFILE) + res |= PPM_O_TMPFILE; +#endif + + if (flags & O_APPEND) + res |= PPM_O_APPEND; + +#ifdef O_DSYNC + if (flags & O_DSYNC) + res |= PPM_O_DSYNC; +#endif + + if (flags & O_EXCL) + res |= PPM_O_EXCL; + + if (flags & O_NONBLOCK) + res |= PPM_O_NONBLOCK; + + if (flags & O_SYNC) + res |= PPM_O_SYNC; + + if (flags & O_TRUNC) + res |= PPM_O_TRUNC; + +#ifdef O_DIRECT + if (flags & O_DIRECT) + res |= PPM_O_DIRECT; +#endif + +#ifdef O_DIRECTORY + if (flags & O_DIRECTORY) + res |= PPM_O_DIRECTORY; +#endif + +#ifdef O_LARGEFILE + if (flags & O_LARGEFILE) + res |= PPM_O_LARGEFILE; +#endif + +#ifdef O_CLOEXEC + if (flags & O_CLOEXEC) + res |= PPM_O_CLOEXEC; +#endif + + return res; +} + +static __always_inline u32 open_modes_to_scap(unsigned long flags, + unsigned long modes) +{ +#ifdef UDIG + unsigned long flags_mask = O_CREAT | O_TMPFILE; +#else +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0) + unsigned long flags_mask = O_CREAT | O_TMPFILE; +#else + unsigned long flags_mask = O_CREAT; +#endif +#endif /* UDIG */ + u32 res = 0; + + if ((flags & flags_mask) == 0) + return res; + + if (modes & S_IRUSR) + res |= PPM_S_IRUSR; + + if (modes & S_IWUSR) + res |= PPM_S_IWUSR; + + if (modes & S_IXUSR) + res |= PPM_S_IXUSR; + + /* + * PPM_S_IRWXU == S_IRUSR | S_IWUSR | S_IXUSR + */ + + if (modes & S_IRGRP) + res |= PPM_S_IRGRP; + + if (modes & S_IWGRP) + res |= PPM_S_IWGRP; + + if (modes & S_IXGRP) + res |= PPM_S_IXGRP; + + /* + * PPM_S_IRWXG == S_IRGRP | S_IWGRP | S_IXGRP + */ + + if (modes & S_IROTH) + res |= PPM_S_IROTH; + + if (modes & S_IWOTH) + res |= PPM_S_IWOTH; + + if (modes & S_IXOTH) + res |= PPM_S_IXOTH; + + /* + * PPM_S_IRWXO == S_IROTH | S_IWOTH | S_IXOTH + */ + + if (modes & S_ISUID) + res |= PPM_S_ISUID; + + if (modes & S_ISGID) + res |= PPM_S_ISGID; + + if (modes & S_ISVTX) + res |= PPM_S_ISVTX; + + return res; +} + +static __always_inline u32 clone_flags_to_scap(unsigned long flags) +{ + u32 res = 0; + + if (flags & CLONE_FILES) + res |= PPM_CL_CLONE_FILES; + + if (flags & CLONE_FS) + res |= PPM_CL_CLONE_FS; + +#ifdef CLONE_IO + if (flags & CLONE_IO) + res |= PPM_CL_CLONE_IO; +#endif + +#ifdef CLONE_NEWIPC + if (flags & CLONE_NEWIPC) + res |= PPM_CL_CLONE_NEWIPC; +#endif + +#ifdef CLONE_NEWNET + if (flags & CLONE_NEWNET) + res |= PPM_CL_CLONE_NEWNET; +#endif + +#ifdef CLONE_NEWNS + if (flags & CLONE_NEWNS) + res |= PPM_CL_CLONE_NEWNS; +#endif + +#ifdef CLONE_NEWPID + if (flags & CLONE_NEWPID) + res |= PPM_CL_CLONE_NEWPID; +#endif + +#ifdef CLONE_NEWUTS + if (flags & CLONE_NEWUTS) + res |= PPM_CL_CLONE_NEWUTS; +#endif + + if (flags & CLONE_PARENT_SETTID) + res |= PPM_CL_CLONE_PARENT_SETTID; + + if (flags & CLONE_PARENT) + res |= PPM_CL_CLONE_PARENT; + + if (flags & CLONE_PTRACE) + res |= PPM_CL_CLONE_PTRACE; + + if (flags & CLONE_SIGHAND) + res |= PPM_CL_CLONE_SIGHAND; + + if (flags & CLONE_SYSVSEM) + res |= PPM_CL_CLONE_SYSVSEM; + + if (flags & CLONE_THREAD) + res |= PPM_CL_CLONE_THREAD; + + if (flags & CLONE_UNTRACED) + res |= PPM_CL_CLONE_UNTRACED; + + if (flags & CLONE_VM) + res |= PPM_CL_CLONE_VM; + +#ifdef CLONE_NEWUSER + if (flags & CLONE_NEWUSER) + res |= PPM_CL_CLONE_NEWUSER; +#endif + + if (flags & CLONE_CHILD_CLEARTID) + res |= PPM_CL_CLONE_CHILD_CLEARTID; + + if (flags & CLONE_CHILD_SETTID) + res |= PPM_CL_CLONE_CHILD_SETTID; + + if (flags & CLONE_SETTLS) + res |= PPM_CL_CLONE_SETTLS; + +#ifdef CLONE_STOPPED + if (flags & CLONE_STOPPED) + res |= PPM_CL_CLONE_STOPPED; +#endif + + if (flags & CLONE_VFORK) + res |= PPM_CL_CLONE_VFORK; + +#ifdef CLONE_NEWCGROUP + if (flags & CLONE_NEWCGROUP) + res |= PPM_CL_CLONE_NEWCGROUP; +#endif + + return res; +} + +static __always_inline u8 socket_family_to_scap(u8 family) +{ + if (family == AF_INET) + return PPM_AF_INET; + else if (family == AF_INET6) + return PPM_AF_INET6; + else if (family == AF_UNIX) + return PPM_AF_UNIX; + else if (family == AF_NETLINK) + return PPM_AF_NETLINK; + else if (family == AF_PACKET) + return PPM_AF_PACKET; + else if (family == AF_UNSPEC) + return PPM_AF_UNSPEC; + else if (family == AF_AX25) + return PPM_AF_AX25; + else if (family == AF_IPX) + return PPM_AF_IPX; + else if (family == AF_APPLETALK) + return PPM_AF_APPLETALK; + else if (family == AF_NETROM) + return PPM_AF_NETROM; + else if (family == AF_BRIDGE) + return PPM_AF_BRIDGE; + else if (family == AF_ATMPVC) + return PPM_AF_ATMPVC; + else if (family == AF_X25) + return PPM_AF_X25; + else if (family == AF_ROSE) + return PPM_AF_ROSE; + else if (family == AF_DECnet) + return PPM_AF_DECnet; + else if (family == AF_NETBEUI) + return PPM_AF_NETBEUI; + else if (family == AF_SECURITY) + return PPM_AF_SECURITY; + else if (family == AF_KEY) + return PPM_AF_KEY; + else if (family == AF_ROUTE) + return PPM_AF_ROUTE; + else if (family == AF_ASH) + return PPM_AF_ASH; + else if (family == AF_ECONET) + return PPM_AF_ECONET; + else if (family == AF_ATMSVC) + return PPM_AF_ATMSVC; +#ifdef AF_RDS + else if (family == AF_RDS) + return PPM_AF_RDS; +#endif + else if (family == AF_SNA) + return PPM_AF_SNA; + else if (family == AF_IRDA) + return PPM_AF_IRDA; + else if (family == AF_PPPOX) + return PPM_AF_PPPOX; + else if (family == AF_WANPIPE) + return PPM_AF_WANPIPE; + else if (family == AF_LLC) + return PPM_AF_LLC; +#ifdef AF_CAN + else if (family == AF_CAN) + return PPM_AF_CAN; +#endif + else if (family == AF_TIPC) + return PPM_AF_TIPC; + else if (family == AF_BLUETOOTH) + return PPM_AF_BLUETOOTH; + else if (family == AF_IUCV) + return PPM_AF_IUCV; +#ifdef AF_RXRPC + else if (family == AF_RXRPC) + return PPM_AF_RXRPC; +#endif +#ifdef AF_ISDN + else if (family == AF_ISDN) + return PPM_AF_ISDN; +#endif +#ifdef AF_PHONET + else if (family == AF_PHONET) + return PPM_AF_PHONET; +#endif +#ifdef AF_IEEE802154 + else if (family == AF_IEEE802154) + return PPM_AF_IEEE802154; +#endif +#ifdef AF_CAIF + else if (family == AF_CAIF) + return PPM_AF_CAIF; +#endif +#ifdef AF_ALG + else if (family == AF_ALG) + return PPM_AF_ALG; +#endif +#ifdef AF_NFC + else if (family == AF_NFC) + return PPM_AF_NFC; +#endif + else { + ASSERT(false); + return PPM_AF_UNSPEC; + } +} + +static __always_inline u32 prot_flags_to_scap(int prot) +{ + u32 res = 0; + + if (prot & PROT_READ) + res |= PPM_PROT_READ; + + if (prot & PROT_WRITE) + res |= PPM_PROT_WRITE; + + if (prot & PROT_EXEC) + res |= PPM_PROT_EXEC; + +#ifdef PROT_SEM + if (prot & PROT_SEM) + res |= PPM_PROT_SEM; +#endif + + if (prot & PROT_GROWSDOWN) + res |= PPM_PROT_GROWSDOWN; + + if (prot & PROT_GROWSUP) + res |= PPM_PROT_GROWSUP; + +#ifdef PROT_SAO + if (prot & PROT_SAO) + res |= PPM_PROT_SAO; +#endif + + return res; +} + +static __always_inline u32 mmap_flags_to_scap(int flags) +{ + u32 res = 0; + + if (flags & MAP_SHARED) + res |= PPM_MAP_SHARED; + + if (flags & MAP_PRIVATE) + res |= PPM_MAP_PRIVATE; + + if (flags & MAP_FIXED) + res |= PPM_MAP_FIXED; + + if (flags & MAP_ANONYMOUS) + res |= PPM_MAP_ANONYMOUS; + +#ifdef MAP_32BIT + if (flags & MAP_32BIT) + res |= PPM_MAP_32BIT; +#endif + +#ifdef MAP_RENAME + if (flags & MAP_RENAME) + res |= PPM_MAP_RENAME; +#endif + + if (flags & MAP_NORESERVE) + res |= PPM_MAP_NORESERVE; + + if (flags & MAP_POPULATE) + res |= PPM_MAP_POPULATE; + + if (flags & MAP_NONBLOCK) + res |= PPM_MAP_NONBLOCK; + + if (flags & MAP_GROWSDOWN) + res |= PPM_MAP_GROWSDOWN; + + if (flags & MAP_DENYWRITE) + res |= PPM_MAP_DENYWRITE; + + if (flags & MAP_EXECUTABLE) + res |= PPM_MAP_EXECUTABLE; + +#ifdef MAP_INHERIT + if (flags & MAP_INHERIT) + res |= PPM_MAP_INHERIT; +#endif + + if (flags & MAP_FILE) + res |= PPM_MAP_FILE; + + if (flags & MAP_LOCKED) + res |= PPM_MAP_LOCKED; + + return res; +} + +static __always_inline u8 fcntl_cmd_to_scap(unsigned long cmd) +{ + switch (cmd) { + case F_DUPFD: + return PPM_FCNTL_F_DUPFD; + case F_GETFD: + return PPM_FCNTL_F_GETFD; + case F_SETFD: + return PPM_FCNTL_F_SETFD; + case F_GETFL: + return PPM_FCNTL_F_GETFL; + case F_SETFL: + return PPM_FCNTL_F_SETFL; + case F_GETLK: + return PPM_FCNTL_F_GETLK; + case F_SETLK: + return PPM_FCNTL_F_SETLK; + case F_SETLKW: + return PPM_FCNTL_F_SETLKW; + case F_SETOWN: + return PPM_FCNTL_F_SETOWN; + case F_GETOWN: + return PPM_FCNTL_F_GETOWN; + case F_SETSIG: + return PPM_FCNTL_F_SETSIG; + case F_GETSIG: + return PPM_FCNTL_F_GETSIG; +#ifndef UDIG +#ifndef CONFIG_64BIT + case F_GETLK64: + return PPM_FCNTL_F_GETLK64; + case F_SETLK64: + return PPM_FCNTL_F_SETLK64; + case F_SETLKW64: + return PPM_FCNTL_F_SETLKW64; +#endif +#endif /* UDIG */ +#ifdef F_SETOWN_EX + case F_SETOWN_EX: + return PPM_FCNTL_F_SETOWN_EX; +#endif +#ifdef F_GETOWN_EX + case F_GETOWN_EX: + return PPM_FCNTL_F_GETOWN_EX; +#endif + case F_SETLEASE: + return PPM_FCNTL_F_SETLEASE; + case F_GETLEASE: + return PPM_FCNTL_F_GETLEASE; + case F_CANCELLK: + return PPM_FCNTL_F_CANCELLK; +#ifdef F_DUPFD_CLOEXEC + case F_DUPFD_CLOEXEC: + return PPM_FCNTL_F_DUPFD_CLOEXEC; +#endif + case F_NOTIFY: + return PPM_FCNTL_F_NOTIFY; +#ifdef F_SETPIPE_SZ + case F_SETPIPE_SZ: + return PPM_FCNTL_F_SETPIPE_SZ; +#endif +#ifdef F_GETPIPE_SZ + case F_GETPIPE_SZ: + return PPM_FCNTL_F_GETPIPE_SZ; +#endif +#ifdef F_OFD_GETLK + case F_OFD_GETLK: + return PPM_FCNTL_F_OFD_GETLK; +#endif +#ifdef F_OFD_SETLK + case F_OFD_SETLK: + return PPM_FCNTL_F_OFD_SETLK; +#endif +#ifdef F_OFD_SETLKW + case F_OFD_SETLKW: + return PPM_FCNTL_F_OFD_SETLKW; +#endif + default: + ASSERT(false); + return PPM_FCNTL_UNKNOWN; + } +} + +static __always_inline u8 sockopt_level_to_scap(int level) +{ + switch (level) { + case SOL_SOCKET: + return PPM_SOCKOPT_LEVEL_SOL_SOCKET; + case SOL_TCP: + return PPM_SOCKOPT_LEVEL_SOL_TCP; + default: + /* no ASSERT as there are legitimate other levels we don't just support yet */ + return PPM_SOCKOPT_LEVEL_UNKNOWN; + } +} + +static __always_inline u8 sockopt_optname_to_scap(int level, int optname) +{ + if (level != SOL_SOCKET) + { + /* no ASSERT as there are legitimate other levels we don't just support yet */ + return PPM_SOCKOPT_LEVEL_UNKNOWN; + } + switch (optname) { +#ifdef SO_DEBUG + case SO_DEBUG: + return PPM_SOCKOPT_SO_DEBUG; +#endif +#ifdef SO_REUSEADDR + case SO_REUSEADDR: + return PPM_SOCKOPT_SO_REUSEADDR; +#endif +#ifdef SO_TYPE + case SO_TYPE: + return PPM_SOCKOPT_SO_TYPE; +#endif +#ifdef SO_ERROR + case SO_ERROR: + return PPM_SOCKOPT_SO_ERROR; +#endif +#ifdef SO_DONTROUTE + case SO_DONTROUTE: + return PPM_SOCKOPT_SO_DONTROUTE; +#endif +#ifdef SO_BROADCAST + case SO_BROADCAST: + return PPM_SOCKOPT_SO_BROADCAST; +#endif +#ifdef SO_SNDBUF + case SO_SNDBUF: + return PPM_SOCKOPT_SO_SNDBUF; +#endif +#ifdef SO_RCVBUF + case SO_RCVBUF: + return PPM_SOCKOPT_SO_RCVBUF; +#endif +#ifdef SO_SNDBUFFORCE + case SO_SNDBUFFORCE: + return PPM_SOCKOPT_SO_SNDBUFFORCE; +#endif +#ifdef SO_RCVBUFFORCE + case SO_RCVBUFFORCE: + return PPM_SOCKOPT_SO_RCVBUFFORCE; +#endif +#ifdef SO_KEEPALIVE + case SO_KEEPALIVE: + return PPM_SOCKOPT_SO_KEEPALIVE; +#endif +#ifdef SO_OOBINLINE + case SO_OOBINLINE: + return PPM_SOCKOPT_SO_OOBINLINE; +#endif +#ifdef SO_NO_CHECK + case SO_NO_CHECK: + return PPM_SOCKOPT_SO_NO_CHECK; +#endif +#ifdef SO_PRIORITY + case SO_PRIORITY: + return PPM_SOCKOPT_SO_PRIORITY; +#endif +#ifdef SO_LINGER + case SO_LINGER: + return PPM_SOCKOPT_SO_LINGER; +#endif +#ifdef SO_BSDCOMPAT + case SO_BSDCOMPAT: + return PPM_SOCKOPT_SO_BSDCOMPAT; +#endif +#ifdef SO_REUSEPORT + case SO_REUSEPORT: + return PPM_SOCKOPT_SO_REUSEPORT; +#endif +#ifdef SO_PASSCRED + case SO_PASSCRED: + return PPM_SOCKOPT_SO_PASSCRED; +#endif +#ifdef SO_PEERCRED + case SO_PEERCRED: + return PPM_SOCKOPT_SO_PEERCRED; +#endif +#ifdef SO_RCVLOWAT + case SO_RCVLOWAT: + return PPM_SOCKOPT_SO_RCVLOWAT; +#endif +#ifdef SO_SNDLOWAT + case SO_SNDLOWAT: + return PPM_SOCKOPT_SO_SNDLOWAT; +#endif +#ifdef SO_RCVTIMEO + case SO_RCVTIMEO: + return PPM_SOCKOPT_SO_RCVTIMEO; +#endif +#ifdef SO_SNDTIMEO + case SO_SNDTIMEO: + return PPM_SOCKOPT_SO_SNDTIMEO; +#endif +#ifdef SO_SECURITY_AUTHENTICATION + case SO_SECURITY_AUTHENTICATION: + return PPM_SOCKOPT_SO_SECURITY_AUTHENTICATION; +#endif +#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT + case SO_SECURITY_ENCRYPTION_TRANSPORT: + return PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_TRANSPORT; +#endif +#ifdef SO_SECURITY_ENCRYPTION_NETWORK + case SO_SECURITY_ENCRYPTION_NETWORK: + return PPM_SOCKOPT_SO_SECURITY_ENCRYPTION_NETWORK; +#endif +#ifdef SO_BINDTODEVICE + case SO_BINDTODEVICE: + return PPM_SOCKOPT_SO_BINDTODEVICE; +#endif +#ifdef SO_ATTACH_FILTER + case SO_ATTACH_FILTER: + return PPM_SOCKOPT_SO_ATTACH_FILTER; +#endif +#ifdef SO_DETACH_FILTER + case SO_DETACH_FILTER: + return PPM_SOCKOPT_SO_DETACH_FILTER; +#endif +#ifdef SO_PEERNAME + case SO_PEERNAME: + return PPM_SOCKOPT_SO_PEERNAME; +#endif +#ifdef SO_TIMESTAMP + case SO_TIMESTAMP: + return PPM_SOCKOPT_SO_TIMESTAMP; +#endif +#ifdef SO_ACCEPTCONN + case SO_ACCEPTCONN: + return PPM_SOCKOPT_SO_ACCEPTCONN; +#endif +#ifdef SO_PEERSEC + case SO_PEERSEC: + return PPM_SOCKOPT_SO_PEERSEC; +#endif +#ifdef SO_PASSSEC + case SO_PASSSEC: + return PPM_SOCKOPT_SO_PASSSEC; +#endif +#ifdef SO_TIMESTAMPNS + case SO_TIMESTAMPNS: + return PPM_SOCKOPT_SO_TIMESTAMPNS; +#endif +#ifdef SO_MARK + case SO_MARK: + return PPM_SOCKOPT_SO_MARK; +#endif +#ifdef SO_TIMESTAMPING + case SO_TIMESTAMPING: + return PPM_SOCKOPT_SO_TIMESTAMPING; +#endif +#ifdef SO_PROTOCOL + case SO_PROTOCOL: + return PPM_SOCKOPT_SO_PROTOCOL; +#endif +#ifdef SO_DOMAIN + case SO_DOMAIN: + return PPM_SOCKOPT_SO_DOMAIN; +#endif +#ifdef SO_RXQ_OVFL + case SO_RXQ_OVFL: + return PPM_SOCKOPT_SO_RXQ_OVFL; +#endif +#ifdef SO_WIFI_STATUS + case SO_WIFI_STATUS: + return PPM_SOCKOPT_SO_WIFI_STATUS; +#endif +#ifdef SO_PEEK_OFF + case SO_PEEK_OFF: + return PPM_SOCKOPT_SO_PEEK_OFF; +#endif +#ifdef SO_NOFCS + case SO_NOFCS: + return PPM_SOCKOPT_SO_NOFCS; +#endif +#ifdef SO_LOCK_FILTER + case SO_LOCK_FILTER: + return PPM_SOCKOPT_SO_LOCK_FILTER; +#endif +#ifdef SO_SELECT_ERR_QUEUE + case SO_SELECT_ERR_QUEUE: + return PPM_SOCKOPT_SO_SELECT_ERR_QUEUE; +#endif +#ifdef SO_BUSY_POLL + case SO_BUSY_POLL: + return PPM_SOCKOPT_SO_BUSY_POLL; +#endif +#ifdef SO_MAX_PACING_RATE + case SO_MAX_PACING_RATE: + return PPM_SOCKOPT_SO_MAX_PACING_RATE; +#endif +#ifdef SO_BPF_EXTENSIONS + case SO_BPF_EXTENSIONS: + return PPM_SOCKOPT_SO_BPF_EXTENSIONS; +#endif +#ifdef SO_INCOMING_CPU + case SO_INCOMING_CPU: + return PPM_SOCKOPT_SO_INCOMING_CPU; +#endif +#ifdef SO_ATTACH_BPF + case SO_ATTACH_BPF: + return PPM_SOCKOPT_SO_ATTACH_BPF; +#endif +#ifdef SO_PEERGROUPS + case SO_PEERGROUPS: + return PPM_SOCKOPT_SO_PEERGROUPS; +#endif +#ifdef SO_MEMINFO + case SO_MEMINFO: + return PPM_SOCKOPT_SO_MEMINFO; +#endif +#ifdef SO_COOKIE + case SO_COOKIE: + return PPM_SOCKOPT_SO_COOKIE; +#endif + default: + ASSERT(false); + return PPM_SOCKOPT_UNKNOWN; + } +} + +/* XXX this is very basic for the moment, we'll need to improve it */ +static __always_inline u16 poll_events_to_scap(short revents) +{ + u16 res = 0; + + if (revents & POLLIN) + res |= PPM_POLLIN; + + if (revents & PPM_POLLPRI) + res |= PPM_POLLPRI; + + if (revents & POLLOUT) + res |= PPM_POLLOUT; + + if (revents & POLLRDHUP) + res |= PPM_POLLRDHUP; + + if (revents & POLLERR) + res |= PPM_POLLERR; + + if (revents & POLLHUP) + res |= PPM_POLLHUP; + + if (revents & POLLNVAL) + res |= PPM_POLLNVAL; + + if (revents & POLLRDNORM) + res |= PPM_POLLRDNORM; + + if (revents & POLLRDBAND) + res |= PPM_POLLRDBAND; + + if (revents & POLLWRNORM) + res |= PPM_POLLWRNORM; + + if (revents & POLLWRBAND) + res |= PPM_POLLWRBAND; + + return res; +} + +static __always_inline u16 futex_op_to_scap(unsigned long op) +{ + u16 res = 0; +#ifndef UDIG + unsigned long flt_op = op & 127; + + if (flt_op == FUTEX_WAIT) + res = PPM_FU_FUTEX_WAIT; + else if (flt_op == FUTEX_WAKE) + res = PPM_FU_FUTEX_WAKE; + else if (flt_op == FUTEX_FD) + res = PPM_FU_FUTEX_FD; + else if (flt_op == FUTEX_REQUEUE) + res = PPM_FU_FUTEX_REQUEUE; + else if (flt_op == FUTEX_CMP_REQUEUE) + res = PPM_FU_FUTEX_CMP_REQUEUE; + else if (flt_op == FUTEX_WAKE_OP) + res = PPM_FU_FUTEX_WAKE_OP; + else if (flt_op == FUTEX_LOCK_PI) + res = PPM_FU_FUTEX_LOCK_PI; + else if (flt_op == FUTEX_UNLOCK_PI) + res = PPM_FU_FUTEX_UNLOCK_PI; + else if (flt_op == FUTEX_TRYLOCK_PI) + res = PPM_FU_FUTEX_TRYLOCK_PI; +#ifdef FUTEX_WAIT_BITSET + else if (flt_op == FUTEX_WAIT_BITSET) + res = PPM_FU_FUTEX_WAIT_BITSET; +#endif +#ifdef FUTEX_WAKE_BITSET + else if (flt_op == FUTEX_WAKE_BITSET) + res = PPM_FU_FUTEX_WAKE_BITSET; +#endif +#ifdef FUTEX_WAIT_REQUEUE_PI + else if (flt_op == FUTEX_WAIT_REQUEUE_PI) + res = PPM_FU_FUTEX_WAIT_REQUEUE_PI; +#endif +#ifdef FUTEX_CMP_REQUEUE_PI + else if (flt_op == FUTEX_CMP_REQUEUE_PI) + res = PPM_FU_FUTEX_CMP_REQUEUE_PI; +#endif + + if (op & FUTEX_PRIVATE_FLAG) + res |= PPM_FU_FUTEX_PRIVATE_FLAG; + +#ifdef FUTEX_CLOCK_REALTIME + if (op & FUTEX_CLOCK_REALTIME) + res |= PPM_FU_FUTEX_CLOCK_REALTIME; +#endif +#endif /* UDIG */ + return res; +} + +static __always_inline u32 access_flags_to_scap(unsigned flags) +{ + u32 res = 0; + + if (flags == 0/*F_OK*/) { + res = PPM_F_OK; + } else { +#ifdef UDIG + if (flags & X_OK) + res |= PPM_X_OK; + if (flags & R_OK) + res |= PPM_R_OK; + if (flags & W_OK) + res |= PPM_W_OK; +#else + if (flags & MAY_EXEC) + res |= PPM_X_OK; + if (flags & MAY_READ) + res |= PPM_R_OK; + if (flags & MAY_WRITE) + res |= PPM_W_OK; +#endif + } + + return res; +} + +static __always_inline u8 rlimit_resource_to_scap(unsigned long rresource) +{ + switch (rresource) { + case RLIMIT_CPU: + return PPM_RLIMIT_CPU; + case RLIMIT_FSIZE: + return PPM_RLIMIT_FSIZE; + case RLIMIT_DATA: + return PPM_RLIMIT_DATA; + case RLIMIT_STACK: + return PPM_RLIMIT_STACK; + case RLIMIT_CORE: + return PPM_RLIMIT_CORE; + case RLIMIT_RSS: + return PPM_RLIMIT_RSS; + case RLIMIT_NPROC: + return PPM_RLIMIT_NPROC; + case RLIMIT_NOFILE: + return PPM_RLIMIT_NOFILE; + case RLIMIT_MEMLOCK: + return PPM_RLIMIT_MEMLOCK; + case RLIMIT_AS: + return PPM_RLIMIT_AS; + case RLIMIT_LOCKS: + return PPM_RLIMIT_LOCKS; + case RLIMIT_SIGPENDING: + return PPM_RLIMIT_SIGPENDING; + case RLIMIT_MSGQUEUE: + return PPM_RLIMIT_MSGQUEUE; + case RLIMIT_NICE: + return PPM_RLIMIT_NICE; + case RLIMIT_RTPRIO: + return PPM_RLIMIT_RTPRIO; +#ifdef RLIMIT_RTTIME + case RLIMIT_RTTIME: + return PPM_RLIMIT_RTTIME; +#endif + default: + return PPM_RLIMIT_UNKNOWN; + } +} + +static __always_inline u16 shutdown_how_to_scap(unsigned long how) +{ +#ifdef SHUT_RD + if (how == SHUT_RD) + return PPM_SHUT_RD; + else if (how == SHUT_WR) + return SHUT_WR; + else if (how == SHUT_RDWR) + return SHUT_RDWR; + + ASSERT(false); +#endif + return (u16)how; +} + +static __always_inline uint64_t lseek_whence_to_scap(unsigned long whence) +{ + uint64_t res = 0; + + if (whence == SEEK_SET) + res = PPM_SEEK_SET; + else if (whence == SEEK_CUR) + res = PPM_SEEK_CUR; + else if (whence == SEEK_END) + res = PPM_SEEK_END; + + return res; +} + +static __always_inline u16 semop_flags_to_scap(short flags) +{ + u16 res = 0; + + if (flags & IPC_NOWAIT) + res |= PPM_IPC_NOWAIT; + + if (flags & SEM_UNDO) + res |= PPM_SEM_UNDO; + + return res; +} + +static __always_inline u32 pf_flags_to_scap(unsigned long flags) +{ + u32 res = 0; + + /* Page fault error codes don't seem to be clearly defined in header + * files throughout the kernel except in some emulation modes (e.g. kvm) + * which we can't assume to exist, so I just took the definitions from + * the x86 manual. If we end up supporting another arch for page faults, + * refactor this. + */ + if (flags & 0x1) + res |= PPM_PF_PROTECTION_VIOLATION; + else + res |= PPM_PF_PAGE_NOT_PRESENT; + + if (flags & 0x2) + res |= PPM_PF_WRITE_ACCESS; + else + res |= PPM_PF_READ_ACCESS; + + if (flags & 0x4) + res |= PPM_PF_USER_FAULT; + else + res |= PPM_PF_SUPERVISOR_FAULT; + + if (flags & 0x8) + res |= PPM_PF_RESERVED_PAGE; + + if (flags & 0x10) + res |= PPM_PF_INSTRUCTION_FETCH; + + return res; +} + +static __always_inline u32 flock_flags_to_scap(unsigned long flags) +{ + u32 res = 0; + + if (flags & LOCK_EX) + res |= PPM_LOCK_EX; + + if (flags & LOCK_SH) + res |= PPM_LOCK_SH; + + if (flags & LOCK_UN) + res |= PPM_LOCK_UN; + + if (flags & LOCK_NB) + res |= PPM_LOCK_NB; + + return res; +} + +static __always_inline uint8_t quotactl_type_to_scap(unsigned long cmd) +{ + switch (cmd & SUBCMDMASK) { + case USRQUOTA: + return PPM_USRQUOTA; + case GRPQUOTA: + return PPM_GRPQUOTA; + } + return 0; +} + +static __always_inline uint16_t quotactl_cmd_to_scap(unsigned long cmd) +{ + uint16_t res; + + switch (cmd >> SUBCMDSHIFT) { + case Q_SYNC: + res = PPM_Q_SYNC; + break; + case Q_QUOTAON: + res = PPM_Q_QUOTAON; + break; + case Q_QUOTAOFF: + res = PPM_Q_QUOTAOFF; + break; + case Q_GETFMT: + res = PPM_Q_GETFMT; + break; + case Q_GETINFO: + res = PPM_Q_GETINFO; + break; + case Q_SETINFO: + res = PPM_Q_SETINFO; + break; + case Q_GETQUOTA: + res = PPM_Q_GETQUOTA; + break; + case Q_SETQUOTA: + res = PPM_Q_SETQUOTA; + break; + /* + * XFS specific + */ +#ifndef UDIG + case Q_XQUOTAON: + res = PPM_Q_XQUOTAON; + break; + case Q_XQUOTAOFF: + res = PPM_Q_XQUOTAOFF; + break; + case Q_XGETQUOTA: + res = PPM_Q_XGETQUOTA; + break; + case Q_XSETQLIM: + res = PPM_Q_XSETQLIM; + break; + case Q_XGETQSTAT: + res = PPM_Q_XGETQSTAT; + break; + case Q_XQUOTARM: + res = PPM_Q_XQUOTARM; + break; + case Q_XQUOTASYNC: + res = PPM_Q_XQUOTASYNC; + break; +#endif + default: + res = 0; + } + return res; +} + +static __always_inline uint8_t quotactl_fmt_to_scap(unsigned long fmt) +{ + switch (fmt) { + case QFMT_VFS_OLD: + return PPM_QFMT_VFS_OLD; + case QFMT_VFS_V0: + return PPM_QFMT_VFS_V0; +#ifdef QFMT_VFS_V1 + case QFMT_VFS_V1: + return PPM_QFMT_VFS_V1; +#endif + default: + return PPM_QFMT_NOT_USED; + } +} + +static __always_inline u32 semget_flags_to_scap(unsigned flags) +{ + u32 res = 0; + + if (flags & IPC_CREAT) + res |= PPM_IPC_CREAT; + + if (flags & IPC_EXCL) + res |= PPM_IPC_EXCL; + + return res; +} + +static __always_inline u32 semctl_cmd_to_scap(unsigned cmd) +{ + switch (cmd) { + case IPC_STAT: return PPM_IPC_STAT; + case IPC_SET: return PPM_IPC_SET; + case IPC_RMID: return PPM_IPC_RMID; + case IPC_INFO: return PPM_IPC_INFO; + case SEM_INFO: return PPM_SEM_INFO; + case SEM_STAT: return PPM_SEM_STAT; + case GETALL: return PPM_GETALL; + case GETNCNT: return PPM_GETNCNT; + case GETPID: return PPM_GETPID; + case GETVAL: return PPM_GETVAL; + case GETZCNT: return PPM_GETZCNT; + case SETALL: return PPM_SETALL; + case SETVAL: return PPM_SETVAL; + } + return 0; +} + +static __always_inline u16 ptrace_requests_to_scap(unsigned long req) +{ + switch (req) { +#ifdef PTRACE_SINGLEBLOCK + case PTRACE_SINGLEBLOCK: + return PPM_PTRACE_SINGLEBLOCK; +#endif +#ifdef PTRACE_SYSEMU_SINGLESTEP + case PTRACE_SYSEMU_SINGLESTEP: + return PPM_PTRACE_SYSEMU_SINGLESTEP; +#endif + +#ifdef PTRACE_SYSEMU + case PTRACE_SYSEMU: + return PPM_PTRACE_SYSEMU; +#endif +#ifdef PTRACE_ARCH_PRCTL + case PTRACE_ARCH_PRCTL: + return PPM_PTRACE_ARCH_PRCTL; +#endif +#ifdef PTRACE_SET_THREAD_AREA + case PTRACE_SET_THREAD_AREA: + return PPM_PTRACE_SET_THREAD_AREA; +#endif +#ifdef PTRACE_GET_THREAD_AREA + case PTRACE_GET_THREAD_AREA: + return PPM_PTRACE_GET_THREAD_AREA; +#endif +#ifdef PTRACE_OLDSETOPTIONS + case PTRACE_OLDSETOPTIONS: + return PPM_PTRACE_OLDSETOPTIONS; +#endif +#ifdef PTRACE_SETFPXREGS + case PTRACE_SETFPXREGS: + return PPM_PTRACE_SETFPXREGS; +#endif +#ifdef PTRACE_GETFPXREGS + case PTRACE_GETFPXREGS: + return PPM_PTRACE_GETFPXREGS; +#endif +#ifdef PTRACE_SETFPREGS + case PTRACE_SETFPREGS: + return PPM_PTRACE_SETFPREGS; +#endif +#ifdef PTRACE_GETFPREGS + case PTRACE_GETFPREGS: + return PPM_PTRACE_GETFPREGS; +#endif +#ifdef PTRACE_SETREGS + case PTRACE_SETREGS: + return PPM_PTRACE_SETREGS; +#endif +#ifdef PTRACE_GETREGS + case PTRACE_GETREGS: + return PPM_PTRACE_GETREGS; +#endif +#ifdef PTRACE_SETSIGMASK + case PTRACE_SETSIGMASK: + return PPM_PTRACE_SETSIGMASK; +#endif +#ifdef PTRACE_GETSIGMASK + case PTRACE_GETSIGMASK: + return PPM_PTRACE_GETSIGMASK; +#endif +#ifdef PTRACE_PEEKSIGINFO + case PTRACE_PEEKSIGINFO: + return PPM_PTRACE_PEEKSIGINFO; +#endif +#ifdef PTRACE_LISTEN + case PTRACE_LISTEN: + return PPM_PTRACE_LISTEN; +#endif +#ifdef PTRACE_INTERRUPT + case PTRACE_INTERRUPT: + return PPM_PTRACE_INTERRUPT; +#endif +#ifdef PTRACE_SEIZE + case PTRACE_SEIZE: + return PPM_PTRACE_SEIZE; +#endif +#ifdef PTRACE_SETREGSET + case PTRACE_SETREGSET: + return PPM_PTRACE_SETREGSET; +#endif +#ifdef PTRACE_GETREGSET + case PTRACE_GETREGSET: + return PPM_PTRACE_GETREGSET; +#endif + case PTRACE_SETSIGINFO: + return PPM_PTRACE_SETSIGINFO; + case PTRACE_GETSIGINFO: + return PPM_PTRACE_GETSIGINFO; + case PTRACE_GETEVENTMSG: + return PPM_PTRACE_GETEVENTMSG; + case PTRACE_SETOPTIONS: + return PPM_PTRACE_SETOPTIONS; + case PTRACE_SYSCALL: + return PPM_PTRACE_SYSCALL; + case PTRACE_DETACH: + return PPM_PTRACE_DETACH; + case PTRACE_ATTACH: + return PPM_PTRACE_ATTACH; + case PTRACE_SINGLESTEP: + return PPM_PTRACE_SINGLESTEP; + case PTRACE_KILL: + return PPM_PTRACE_KILL; + case PTRACE_CONT: + return PPM_PTRACE_CONT; +#ifdef PTRACE_POKEUSR + case PTRACE_POKEUSR: + return PPM_PTRACE_POKEUSR; +#endif + case PTRACE_POKEDATA: + return PPM_PTRACE_POKEDATA; + case PTRACE_POKETEXT: + return PPM_PTRACE_POKETEXT; +#ifdef PTRACE_PEEKUSR + case PTRACE_PEEKUSR: + return PPM_PTRACE_PEEKUSR; +#endif + case PTRACE_PEEKDATA: + return PPM_PTRACE_PEEKDATA; + case PTRACE_PEEKTEXT: + return PPM_PTRACE_PEEKTEXT; + case PTRACE_TRACEME: + return PPM_PTRACE_TRACEME; + default: + return PPM_PTRACE_UNKNOWN; + } +} + +static __always_inline u32 unlinkat_flags_to_scap(unsigned long flags) +{ + u32 res = 0; + + if (flags & AT_REMOVEDIR) + res |= PPM_AT_REMOVEDIR; + + return res; +} + +static __always_inline u32 linkat_flags_to_scap(unsigned long flags) +{ + u32 res = 0; + + if (flags & AT_SYMLINK_FOLLOW) + res |= PPM_AT_SYMLINK_FOLLOW; + +#ifdef AT_EMPTY_PATH + if (flags & AT_EMPTY_PATH) + res |= PPM_AT_EMPTY_PATH; +#endif + + return res; +} + +static __always_inline u32 chmod_mode_to_scap(unsigned long modes) +{ + u32 res = 0; + if (modes & S_IRUSR) + res |= PPM_S_IRUSR; + + if (modes & S_IWUSR) + res |= PPM_S_IWUSR; + + if (modes & S_IXUSR) + res |= PPM_S_IXUSR; + + /* + * PPM_S_IRWXU == S_IRUSR | S_IWUSR | S_IXUSR + */ + + if (modes & S_IRGRP) + res |= PPM_S_IRGRP; + + if (modes & S_IWGRP) + res |= PPM_S_IWGRP; + + if (modes & S_IXGRP) + res |= PPM_S_IXGRP; + + /* + * PPM_S_IRWXG == S_IRGRP | S_IWGRP | S_IXGRP + */ + + if (modes & S_IROTH) + res |= PPM_S_IROTH; + + if (modes & S_IWOTH) + res |= PPM_S_IWOTH; + + if (modes & S_IXOTH) + res |= PPM_S_IXOTH; + + /* + * PPM_S_IRWXO == S_IROTH | S_IWOTH | S_IXOTH + */ + + if (modes & S_ISUID) + res |= PPM_S_ISUID; + + if (modes & S_ISGID) + res |= PPM_S_ISGID; + + if (modes & S_ISVTX) + res |= PPM_S_ISVTX; + + return res; +} + +#endif /* PPM_FLAG_HELPERS_H_ */ diff --git a/driver/ppm_ringbuffer.h b/driver/ppm_ringbuffer.h index 524ce1596e..b6761b2718 100644 --- a/driver/ppm_ringbuffer.h +++ b/driver/ppm_ringbuffer.h @@ -1,42 +1,33 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2013-2018 Draios Inc. dba Sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ -#ifndef PPM_H_ -#define PPM_H_ +#ifndef PPM_RINGBUFFER_H_ +#define PPM_RINGBUFFER_H_ #ifdef __KERNEL__ #include #endif -static const uint32_t RING_BUF_SIZE = 1024 * 1024; -static const uint32_t MIN_USERSPACE_READ_SIZE = 128 * 1024; +static const __u32 RING_BUF_SIZE = 8 * 1024 * 1024; +static const __u32 MIN_USERSPACE_READ_SIZE = 128 * 1024; /* * This gets mapped to user level, so we want to keep it as clean as possible */ struct ppm_ring_buffer_info { - volatile uint32_t head; - volatile uint32_t tail; - volatile uint64_t n_evts; /* Total number of events that were received by the driver. */ - volatile uint64_t n_drops_buffer; /* Number of dropped events (buffer full). */ - volatile uint64_t n_drops_pf; /* Number of dropped events (page faults). */ - volatile uint64_t n_preemptions; /* Number of preemptions. */ - volatile uint64_t n_context_switches; /* Number of received context switch events. */ + volatile __u32 head; + volatile __u32 tail; + volatile __u64 n_evts; /* Total number of events that were received by the driver. */ + volatile __u64 n_drops_buffer; /* Number of dropped events (buffer full). */ + volatile __u64 n_drops_pf; /* Number of dropped events (page faults). */ + volatile __u64 n_preemptions; /* Number of preemptions. */ + volatile __u64 n_context_switches; /* Number of received context switch events. */ }; -#endif /* PPM_H_ */ +#endif /* PPM_RINGBUFFER_H_ */ diff --git a/driver/ppm_syscall.h b/driver/ppm_syscall.h new file mode 100644 index 0000000000..deace07f71 --- /dev/null +++ b/driver/ppm_syscall.h @@ -0,0 +1,229 @@ +/* + * Access to user system call parameters and results + * + * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU General Public License v.2. + * + * See asm-generic/syscall.h for descriptions of what we must do here. + */ + +#ifndef _ASM_X86_SYSCALL_H +#define _ASM_X86_SYSCALL_H + +//#include +#include +#include +#include /* For NR_syscalls */ +#include /* for TS_COMPAT */ +#include + +#ifndef NS_syscalls +#define NR_syscalls (__NR_syscall_max + 1) +#endif + +typedef void (*sys_call_ptr_t)(void); +extern const sys_call_ptr_t sys_call_table[]; + +/* + * Only the low 32 bits of orig_ax are meaningful, so we return int. + * This importantly ignores the high bits on 64-bit, so comparisons + * sign-extend the low 32 bits. + */ +static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs) +{ + return regs->orig_rax; +} + +static inline void syscall_rollback(struct task_struct *task, + struct pt_regs *regs) +{ + regs->rax = regs->orig_rax; +} + +static inline long syscall_get_error(struct task_struct *task, + struct pt_regs *regs) +{ + unsigned long error = regs->rax; +#ifdef CONFIG_IA32_EMULATION + /* + * TS_COMPAT is set for 32-bit syscall entries and then + * remains set until we return to user mode. + */ + if (task_thread_info(task)->status & TS_COMPAT) + /* + * Sign-extend the value so (int)-EFOO becomes (long)-EFOO + * and will match correctly in comparisons. + */ + error = (long) (int) error; +#endif + return IS_ERR_VALUE(error) ? error : 0; +} + +static inline long syscall_get_return_value(struct task_struct *task, + struct pt_regs *regs) +{ + return regs->rax; +} + +static inline void syscall_set_return_value(struct task_struct *task, + struct pt_regs *regs, + int error, long val) +{ + regs->rax = (long) error ?: val; +} + +#ifdef CONFIG_X86_32 + +static inline void syscall_get_arguments(struct task_struct *task, + struct pt_regs *regs, + unsigned int i, unsigned int n, + unsigned long *args) +{ + BUG_ON(i + n > 6); + memcpy(args, ®s->rbx + i, n * sizeof(args[0])); +} + +static inline void syscall_set_arguments(struct task_struct *task, + struct pt_regs *regs, + unsigned int i, unsigned int n, + const unsigned long *args) +{ + BUG_ON(i + n > 6); + memcpy(®s->rbx + i, args, n * sizeof(args[0])); +} + +static inline int syscall_get_arch(void) +{ + return AUDIT_ARCH_I386; +} + +#else /* CONFIG_X86_64 */ + +static inline void syscall_get_arguments(struct task_struct *task, + struct pt_regs *regs, + unsigned int i, unsigned int n, + unsigned long *args) +{ +# ifdef CONFIG_IA32_EMULATION + if (task_thread_info(task)->status & TS_COMPAT) + switch (i) { + case 0: + if (!n--) break; + *args++ = regs->rbx; + case 1: + if (!n--) break; + *args++ = regs->rcx; + case 2: + if (!n--) break; + *args++ = regs->rdx; + case 3: + if (!n--) break; + *args++ = regs->rsi; + case 4: + if (!n--) break; + *args++ = regs->rdi; + case 5: + if (!n--) break; + *args++ = regs->rbp; + case 6: + if (!n--) break; + default: + BUG(); + break; + } + else +# endif + switch (i) { + case 0: + if (!n--) break; + *args++ = regs->rdi; + case 1: + if (!n--) break; + *args++ = regs->rsi; + case 2: + if (!n--) break; + *args++ = regs->rdx; + case 3: + if (!n--) break; + *args++ = regs->r10; + case 4: + if (!n--) break; + *args++ = regs->r8; + case 5: + if (!n--) break; + *args++ = regs->r9; + case 6: + if (!n--) break; + default: + BUG(); + break; + } +} + +static inline void syscall_set_arguments(struct task_struct *task, + struct pt_regs *regs, + unsigned int i, unsigned int n, + const unsigned long *args) +{ +# ifdef CONFIG_IA32_EMULATION + if (task_thread_info(task)->status & TS_COMPAT) + switch (i) { + case 0: + if (!n--) break; + regs->rbx = *args++; + case 1: + if (!n--) break; + regs->rcx = *args++; + case 2: + if (!n--) break; + regs->rdx = *args++; + case 3: + if (!n--) break; + regs->rsi = *args++; + case 4: + if (!n--) break; + regs->rdi = *args++; + case 5: + if (!n--) break; + regs->rbp = *args++; + case 6: + if (!n--) break; + default: + BUG(); + break; + } + else +# endif + switch (i) { + case 0: + if (!n--) break; + regs->rdi = *args++; + case 1: + if (!n--) break; + regs->rsi = *args++; + case 2: + if (!n--) break; + regs->rdx = *args++; + case 3: + if (!n--) break; + regs->r10 = *args++; + case 4: + if (!n--) break; + regs->r8 = *args++; + case 5: + if (!n--) break; + regs->r9 = *args++; + case 6: + if (!n--) break; + default: + BUG(); + break; + } +} + +#endif /* CONFIG_X86_32 */ + +#endif /* _ASM_X86_SYSCALL_H */ diff --git a/driver/ppm_version.h b/driver/ppm_version.h new file mode 100644 index 0000000000..00e5197feb --- /dev/null +++ b/driver/ppm_version.h @@ -0,0 +1,20 @@ +#ifndef UDIG +#include +#endif + +/** + * for RHEL kernels, export the release code (which is equal to e.g. + * RHEL_RELEASE_CODE(8, 1)) under our own name. + * For other kernels, just use zeros. + * + * We need macros that are always defined to use in preprocessor directives + * to express the required kernel version in a single expression, without + * a multiline #ifdef soup. + */ +#ifdef RHEL_RELEASE_CODE +#define PPM_RHEL_RELEASE_CODE RHEL_RELEASE_CODE +#define PPM_RHEL_RELEASE_VERSION(x,y) RHEL_RELEASE_VERSION(x,y) +#else +#define PPM_RHEL_RELEASE_CODE 0 +#define PPM_RHEL_RELEASE_VERSION(x,y) 0 +#endif diff --git a/driver/syscall_table.c b/driver/syscall_table.c index 1a7a1adbc2..529c09417e 100644 --- a/driver/syscall_table.c +++ b/driver/syscall_table.c @@ -1,21 +1,14 @@ /* -Copyright (C) 2013-2014 Draios inc. -This file is part of sysdig. +Copyright (c) 2020 Sysdig Inc -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +This file is dual licensed under either the MIT or GPL 2. See MIT.txt +or GPL2.txt for full copies of the license. -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#ifdef __KERNEL__ +#include #include #include #include @@ -25,487 +18,1634 @@ along with sysdig. If not, see . #include #include #include -#include #include #include +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 20) +#include "ppm_syscall.h" +#else +#include +#endif +#else /* __KERNEL__ */ +#ifdef UDIG +#include +#else /* UDIG */ +#include +#endif /* UDIG */ +#ifdef __mips__ +#define SYSCALL_TABLE_ID0 __NR_Linux +#else /* __mips__ */ +#define SYSCALL_TABLE_ID0 0 +#endif /* __mips__ */ +#endif /* __KERNEL__ */ + -#include "ppm_ringbuffer.h" #include "ppm_events_public.h" -#include "ppm_events.h" +#ifdef __KERNEL__ #include "ppm.h" +#if defined(CONFIG_IA32_EMULATION) && !defined(__NR_ia32_socketcall) +#include "ppm_compat_unistd_32.h" +#endif +#endif /* __KERNEL__ */ /* * SYSCALL TABLE */ const struct syscall_evt_pair g_syscall_table[SYSCALL_TABLE_SIZE] = { - [__NR_open] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_OPEN_E, PPME_SYSCALL_OPEN_X}, - [__NR_creat] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_CREAT_E, PPME_SYSCALL_CREAT_X}, - [__NR_close] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_CLOSE_E, PPME_SYSCALL_CLOSE_X}, - [__NR_brk] = {UF_USED, PPME_SYSCALL_BRK_E, PPME_SYSCALL_BRK_X}, - [__NR_read] = {UF_USED, PPME_SYSCALL_READ_E, PPME_SYSCALL_READ_X}, - [__NR_write] = {UF_USED, PPME_SYSCALL_WRITE_E, PPME_SYSCALL_WRITE_X}, - [__NR_execve] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_EXECVE_E, PPME_SYSCALL_EXECVE_X}, - [__NR_clone] = {UF_USED | UF_NEVER_DROP, PPME_CLONE_E, PPME_CLONE_X}, - [__NR_pipe] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PIPE_E, PPME_SYSCALL_PIPE_X}, - [__NR_pipe2] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PIPE_E, PPME_SYSCALL_PIPE_X}, - [__NR_eventfd] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_EVENTFD_E, PPME_SYSCALL_EVENTFD_X}, - [__NR_eventfd2] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_EVENTFD_E, PPME_SYSCALL_EVENTFD_X}, - [__NR_futex] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_FUTEX_E, PPME_SYSCALL_FUTEX_X}, - [__NR_stat] = {UF_USED, PPME_SYSCALL_STAT_E, PPME_SYSCALL_STAT_X}, - [__NR_lstat] = {UF_USED, PPME_SYSCALL_LSTAT_E, PPME_SYSCALL_LSTAT_X}, - [__NR_fstat] = {UF_USED, PPME_SYSCALL_FSTAT_E, PPME_SYSCALL_FSTAT_X}, - [__NR_epoll_wait] = {UF_USED, PPME_SYSCALL_EPOLLWAIT_E, PPME_SYSCALL_EPOLLWAIT_X}, - [__NR_poll] = {UF_USED, PPME_SYSCALL_POLL_E, PPME_SYSCALL_POLL_X}, - [__NR_select] = {UF_USED, PPME_SYSCALL_SELECT_E, PPME_SYSCALL_SELECT_X}, - [__NR_lseek] = {UF_USED, PPME_SYSCALL_LSEEK_E, PPME_SYSCALL_LSEEK_X}, - [__NR_ioctl] = {UF_USED, PPME_SYSCALL_IOCTL_E, PPME_SYSCALL_IOCTL_X}, - [__NR_getcwd] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_GETCWD_E, PPME_SYSCALL_GETCWD_X}, - [__NR_chdir] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_CHDIR_E, PPME_SYSCALL_CHDIR_X}, - [__NR_fchdir] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_FCHDIR_E, PPME_SYSCALL_FCHDIR_X}, - [__NR_mkdir] = {UF_USED, PPME_SYSCALL_MKDIR_E, PPME_SYSCALL_MKDIR_X}, - [__NR_rmdir] = {UF_USED, PPME_SYSCALL_RMDIR_E, PPME_SYSCALL_RMDIR_X}, - [__NR_openat] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_OPENAT_E, PPME_SYSCALL_OPENAT_X}, - [__NR_link] = {UF_USED, PPME_SYSCALL_LINK_E, PPME_SYSCALL_LINK_X}, - [__NR_linkat] = {UF_USED, PPME_SYSCALL_LINKAT_E, PPME_SYSCALL_LINKAT_X}, - [__NR_unlink] = {UF_USED, PPME_SYSCALL_UNLINK_E, PPME_SYSCALL_UNLINK_X}, - [__NR_unlinkat] = {UF_USED, PPME_SYSCALL_UNLINKAT_E, PPME_SYSCALL_UNLINKAT_X}, - [__NR_pread64] = {UF_USED, PPME_SYSCALL_PREAD_E, PPME_SYSCALL_PREAD_X}, - [__NR_pwrite64] = {UF_USED, PPME_SYSCALL_PWRITE_E, PPME_SYSCALL_PWRITE_X}, - [__NR_readv] = {UF_USED, PPME_SYSCALL_READV_E, PPME_SYSCALL_READV_X}, - [__NR_writev] = {UF_USED, PPME_SYSCALL_WRITEV_E, PPME_SYSCALL_WRITEV_X}, - [__NR_preadv] = {UF_USED, PPME_SYSCALL_PREADV_E, PPME_SYSCALL_PREADV_X}, - [__NR_pwritev] = {UF_USED, PPME_SYSCALL_PWRITEV_E, PPME_SYSCALL_PWRITEV_X}, - [__NR_dup] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, - [__NR_dup2] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, - [__NR_dup3] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, - [__NR_signalfd] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SIGNALFD_E, PPME_SYSCALL_SIGNALFD_X}, - [__NR_signalfd4] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SIGNALFD_E, PPME_SYSCALL_SIGNALFD_X}, - [__NR_kill] = {UF_USED, PPME_SYSCALL_KILL_E, PPME_SYSCALL_KILL_X}, - [__NR_tkill] = {UF_USED, PPME_SYSCALL_TKILL_E, PPME_SYSCALL_TKILL_X}, - [__NR_tgkill] = {UF_USED, PPME_SYSCALL_TGKILL_E, PPME_SYSCALL_TGKILL_X}, - [__NR_nanosleep] = {UF_USED, PPME_SYSCALL_NANOSLEEP_E, PPME_SYSCALL_NANOSLEEP_X}, - [__NR_timerfd_create] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_TIMERFD_CREATE_E, PPME_SYSCALL_TIMERFD_CREATE_X}, - [__NR_inotify_init] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_INOTIFY_INIT_E, PPME_SYSCALL_INOTIFY_INIT_X}, - [__NR_inotify_init1] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_INOTIFY_INIT_E, PPME_SYSCALL_INOTIFY_INIT_X}, - [__NR_getrlimit] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_GETRLIMIT_E, PPME_SYSCALL_GETRLIMIT_X}, - [__NR_setrlimit] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SETRLIMIT_E, PPME_SYSCALL_SETRLIMIT_X}, + [__NR_open - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_OPEN_E, PPME_SYSCALL_OPEN_X}, + [__NR_creat - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_CREAT_E, PPME_SYSCALL_CREAT_X}, + [__NR_close - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_CLOSE_E, PPME_SYSCALL_CLOSE_X}, + [__NR_brk - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_BRK_4_E, PPME_SYSCALL_BRK_4_X}, + [__NR_read - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_READ_E, PPME_SYSCALL_READ_X}, + [__NR_write - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_WRITE_E, PPME_SYSCALL_WRITE_X}, + [__NR_execve - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_EXECVE_19_E, PPME_SYSCALL_EXECVE_19_X}, + [__NR_clone - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_CLONE_20_E, PPME_SYSCALL_CLONE_20_X}, + [__NR_fork - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_FORK_20_E, PPME_SYSCALL_FORK_20_X}, + [__NR_vfork - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_VFORK_20_E, PPME_SYSCALL_VFORK_20_X}, + [__NR_pipe - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PIPE_E, PPME_SYSCALL_PIPE_X}, + [__NR_pipe2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PIPE_E, PPME_SYSCALL_PIPE_X}, + [__NR_eventfd - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_EVENTFD_E, PPME_SYSCALL_EVENTFD_X}, + [__NR_eventfd2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_EVENTFD_E, PPME_SYSCALL_EVENTFD_X}, + [__NR_futex - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_FUTEX_E, PPME_SYSCALL_FUTEX_X}, + [__NR_stat - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_STAT_E, PPME_SYSCALL_STAT_X}, + [__NR_lstat - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_LSTAT_E, PPME_SYSCALL_LSTAT_X}, + [__NR_fstat - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_FSTAT_E, PPME_SYSCALL_FSTAT_X}, + [__NR_epoll_wait - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_EPOLLWAIT_E, PPME_SYSCALL_EPOLLWAIT_X}, + [__NR_poll - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_POLL_E, PPME_SYSCALL_POLL_X}, +#ifdef __NR_select + [__NR_select - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SELECT_E, PPME_SYSCALL_SELECT_X}, +#endif + [__NR_lseek - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_LSEEK_E, PPME_SYSCALL_LSEEK_X}, + [__NR_ioctl - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_IOCTL_3_E, PPME_SYSCALL_IOCTL_3_X}, + [__NR_getcwd - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_GETCWD_E, PPME_SYSCALL_GETCWD_X}, + [__NR_chdir - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_CHDIR_E, PPME_SYSCALL_CHDIR_X}, + [__NR_fchdir - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_FCHDIR_E, PPME_SYSCALL_FCHDIR_X}, + [__NR_mkdir - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_MKDIR_2_E, PPME_SYSCALL_MKDIR_2_X}, + [__NR_rmdir - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RMDIR_2_E, PPME_SYSCALL_RMDIR_2_X}, + [__NR_openat - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_OPENAT_2_E, PPME_SYSCALL_OPENAT_2_X}, + [__NR_mkdirat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_MKDIRAT_E, PPME_SYSCALL_MKDIRAT_X}, + [__NR_link - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_LINK_2_E, PPME_SYSCALL_LINK_2_X}, + [__NR_linkat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_LINKAT_2_E, PPME_SYSCALL_LINKAT_2_X}, + [__NR_unlink - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_UNLINK_2_E, PPME_SYSCALL_UNLINK_2_X}, + [__NR_unlinkat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_UNLINKAT_2_E, PPME_SYSCALL_UNLINKAT_2_X}, + [__NR_pread64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PREAD_E, PPME_SYSCALL_PREAD_X}, + [__NR_pwrite64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PWRITE_E, PPME_SYSCALL_PWRITE_X}, + [__NR_readv - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_READV_E, PPME_SYSCALL_READV_X}, + [__NR_writev - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_WRITEV_E, PPME_SYSCALL_WRITEV_X}, + [__NR_preadv - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PREADV_E, PPME_SYSCALL_PREADV_X}, + [__NR_pwritev - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PWRITEV_E, PPME_SYSCALL_PWRITEV_X}, + [__NR_dup - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, + [__NR_dup2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, + [__NR_dup3 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, + [__NR_signalfd - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SIGNALFD_E, PPME_SYSCALL_SIGNALFD_X}, + [__NR_signalfd4 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SIGNALFD_E, PPME_SYSCALL_SIGNALFD_X}, + [__NR_kill - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_KILL_E, PPME_SYSCALL_KILL_X}, + [__NR_tkill - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_TKILL_E, PPME_SYSCALL_TKILL_X}, + [__NR_tgkill - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_TGKILL_E, PPME_SYSCALL_TGKILL_X}, + [__NR_nanosleep - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_NANOSLEEP_E, PPME_SYSCALL_NANOSLEEP_X}, + [__NR_timerfd_create - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_TIMERFD_CREATE_E, PPME_SYSCALL_TIMERFD_CREATE_X}, + [__NR_inotify_init - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_INOTIFY_INIT_E, PPME_SYSCALL_INOTIFY_INIT_X}, + [__NR_inotify_init1 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_INOTIFY_INIT_E, PPME_SYSCALL_INOTIFY_INIT_X}, + [__NR_fchmodat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCHMODAT_E, PPME_SYSCALL_FCHMODAT_X}, + [__NR_fchmod - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCHMOD_E, PPME_SYSCALL_FCHMOD_X}, +#ifdef __NR_getrlimit + [__NR_getrlimit - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_GETRLIMIT_E, PPME_SYSCALL_GETRLIMIT_X}, +#endif + [__NR_setrlimit - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SETRLIMIT_E, PPME_SYSCALL_SETRLIMIT_X}, #ifdef __NR_prlimit64 - [__NR_prlimit64] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PRLIMIT_E, PPME_SYSCALL_PRLIMIT_X}, + [__NR_prlimit64 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PRLIMIT_E, PPME_SYSCALL_PRLIMIT_X}, #endif #ifdef __NR_ugetrlimit - [__NR_ugetrlimit] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_GETRLIMIT_E, PPME_SYSCALL_GETRLIMIT_X}, + [__NR_ugetrlimit - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_GETRLIMIT_E, PPME_SYSCALL_GETRLIMIT_X}, #endif - [__NR_fcntl] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_FCNTL_E, PPME_SYSCALL_FCNTL_X}, + [__NR_fcntl - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCNTL_E, PPME_SYSCALL_FCNTL_X}, #ifdef __NR_fcntl64 - [__NR_fcntl64] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_FCNTL_E, PPME_SYSCALL_FCNTL_X}, -#endif -/* [__NR_ppoll] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, */ -/* [__NR_old_select] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, */ - [__NR_pselect6] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_epoll_create] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_epoll_ctl] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_uselib] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_sched_setparam] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_sched_getparam] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_fork] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_syslog] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_chmod] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_lchown] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_utime] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_mount] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_umount2] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_setuid] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_getuid] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_ptrace] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_alarm] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, - [__NR_pause] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, -#ifdef __x86_64__ - [__NR_socket] = {UF_USED, PPME_SOCKET_SOCKET_E, PPME_SOCKET_SOCKET_X}, - [__NR_bind] = {UF_USED, PPME_SOCKET_BIND_E, PPME_SOCKET_BIND_X}, - [__NR_connect] = {UF_USED, PPME_SOCKET_CONNECT_E, PPME_SOCKET_CONNECT_X}, - [__NR_listen] = {UF_USED, PPME_SOCKET_LISTEN_E, PPME_SOCKET_LISTEN_X}, - [__NR_accept] = {UF_USED, PPME_SOCKET_ACCEPT_E, PPME_SOCKET_ACCEPT_X}, - [__NR_getsockname] = {UF_USED, PPME_SOCKET_GETSOCKNAME_E, PPME_SOCKET_GETSOCKNAME_X}, - [__NR_getpeername] = {UF_USED, PPME_SOCKET_GETPEERNAME_E, PPME_SOCKET_GETPEERNAME_X}, - [__NR_socketpair] = {UF_USED | UF_NEVER_DROP, PPME_SOCKET_SOCKETPAIR_E, PPME_SOCKET_SOCKETPAIR_X}, - [__NR_sendto] = {UF_USED, PPME_SOCKET_SENDTO_E, PPME_SOCKET_SENDTO_X}, - [__NR_recvfrom] = {UF_USED, PPME_SOCKET_RECVFROM_E, PPME_SOCKET_RECVFROM_X}, - [__NR_shutdown] = {UF_USED, PPME_SOCKET_SHUTDOWN_E, PPME_SOCKET_SHUTDOWN_X}, - [__NR_setsockopt] = {UF_USED, PPME_SOCKET_SETSOCKOPT_E, PPME_SOCKET_SETSOCKOPT_X}, - [__NR_getsockopt] = {UF_USED, PPME_SOCKET_GETSOCKOPT_E, PPME_SOCKET_GETSOCKOPT_X}, - [__NR_sendmsg] = {UF_USED, PPME_SOCKET_SENDMSG_E, PPME_SOCKET_SENDMSG_X}, + [__NR_fcntl64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCNTL_E, PPME_SYSCALL_FCNTL_X}, +#endif +/* [__NR_old_select - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, */ + [__NR_pselect6 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_epoll_create - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_epoll_ctl - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_uselib - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_sched_setparam - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_sched_getparam - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_syslog - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_chmod - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_CHMOD_E, PPME_SYSCALL_CHMOD_X}, + [__NR_lchown - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, +#ifdef __NR_utime + [__NR_utime - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, +#endif + [__NR_mount - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_MOUNT_E, PPME_SYSCALL_MOUNT_X}, + [__NR_umount2 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_UMOUNT_E, PPME_SYSCALL_UMOUNT_X}, + [__NR_ptrace - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PTRACE_E, PPME_SYSCALL_PTRACE_X}, +#ifdef __NR_alarm + [__NR_alarm - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, +#endif + [__NR_pause - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + +#ifndef __NR_socketcall + [__NR_socket - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_SOCKET_E, PPME_SOCKET_SOCKET_X}, + [__NR_bind - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SOCKET_BIND_E, PPME_SOCKET_BIND_X}, + [__NR_connect - SYSCALL_TABLE_ID0] = {UF_USED | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_CONNECT_E, PPME_SOCKET_CONNECT_X}, + [__NR_listen - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_LISTEN_E, PPME_SOCKET_LISTEN_X}, + [__NR_accept - SYSCALL_TABLE_ID0] = {UF_USED | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_ACCEPT_5_E, PPME_SOCKET_ACCEPT_5_X}, + [__NR_getsockname - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SOCKET_GETSOCKNAME_E, PPME_SOCKET_GETSOCKNAME_X}, + [__NR_getpeername - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SOCKET_GETPEERNAME_E, PPME_SOCKET_GETPEERNAME_X}, + [__NR_socketpair - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SOCKET_SOCKETPAIR_E, PPME_SOCKET_SOCKETPAIR_X}, + [__NR_sendto - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SENDTO_E, PPME_SOCKET_SENDTO_X}, + [__NR_recvfrom - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_RECVFROM_E, PPME_SOCKET_RECVFROM_X}, + [__NR_shutdown - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SHUTDOWN_E, PPME_SOCKET_SHUTDOWN_X}, + [__NR_setsockopt - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SOCKET_SETSOCKOPT_E, PPME_SOCKET_SETSOCKOPT_X}, + [__NR_getsockopt - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_GETSOCKOPT_E, PPME_SOCKET_GETSOCKOPT_X}, + [__NR_sendmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SENDMSG_E, PPME_SOCKET_SENDMSG_X}, + [__NR_accept4 - SYSCALL_TABLE_ID0] = {UF_USED | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_ACCEPT4_5_E, PPME_SOCKET_ACCEPT4_5_X}, +#endif + #ifdef __NR_sendmmsg - [__NR_sendmmsg] = {UF_USED, PPME_SOCKET_SENDMMSG_E, PPME_SOCKET_SENDMMSG_X}, + [__NR_sendmmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SENDMMSG_E, PPME_SOCKET_SENDMMSG_X}, +#endif +#ifdef __NR_recvmsg + [__NR_recvmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_RECVMSG_E, PPME_SOCKET_RECVMSG_X}, #endif - [__NR_recvmsg] = {UF_USED, PPME_SOCKET_RECVMSG_E, PPME_SOCKET_RECVMSG_X}, #ifdef __NR_recvmmsg - [__NR_recvmmsg] = {UF_USED, PPME_SOCKET_RECVMMSG_E, PPME_SOCKET_RECVMMSG_X}, -#endif - [__NR_accept4] = {UF_USED, PPME_SOCKET_ACCEPT4_E, PPME_SOCKET_ACCEPT4_X}, -#else /* __x86_64__ */ - [__NR_stat64] = {UF_USED, PPME_SYSCALL_STAT64_E, PPME_SYSCALL_STAT64_X}, - [__NR_fstat64] = {UF_USED, PPME_SYSCALL_FSTAT64_E, PPME_SYSCALL_FSTAT64_X}, - [__NR__llseek] = {UF_USED, PPME_SYSCALL_LLSEEK_E, PPME_SYSCALL_LLSEEK_X} -#endif /* __x86_64__ */ + [__NR_recvmmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_RECVMMSG_E, PPME_SOCKET_RECVMMSG_X}, +#endif +#ifdef __NR_stat64 + [__NR_stat64 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_STAT64_E, PPME_SYSCALL_STAT64_X}, +#endif +#ifdef __NR_fstat64 + [__NR_fstat64 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_FSTAT64_E, PPME_SYSCALL_FSTAT64_X}, +#endif +#ifdef __NR__llseek + [__NR__llseek - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_LLSEEK_E, PPME_SYSCALL_LLSEEK_X}, +#endif +#ifdef __NR_mmap + [__NR_mmap - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_MMAP_E, PPME_SYSCALL_MMAP_X}, +#endif +#ifdef __NR_mmap2 + [__NR_mmap2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_MMAP2_E, PPME_SYSCALL_MMAP2_X}, +#endif + [__NR_munmap - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_MUNMAP_E, PPME_SYSCALL_MUNMAP_X}, + [__NR_splice - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SPLICE_E, PPME_SYSCALL_SPLICE_X}, +#ifdef __NR_process_vm_readv + [__NR_process_vm_readv - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, +#endif +#ifdef __NR_process_vm_writev + [__NR_process_vm_writev - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, +#endif + + [__NR_rename - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RENAME_E, PPME_SYSCALL_RENAME_X}, + [__NR_renameat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RENAMEAT_E, PPME_SYSCALL_RENAMEAT_X}, + [__NR_symlink - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SYMLINK_E, PPME_SYSCALL_SYMLINK_X}, + [__NR_symlinkat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SYMLINKAT_E, PPME_SYSCALL_SYMLINKAT_X}, + [__NR_sendfile - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SENDFILE_E, PPME_SYSCALL_SENDFILE_X}, +#ifdef __NR_sendfile64 + [__NR_sendfile64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SENDFILE_E, PPME_SYSCALL_SENDFILE_X}, +#endif +#ifdef __NR_quotactl + [__NR_quotactl - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_QUOTACTL_E, PPME_SYSCALL_QUOTACTL_X}, +#endif +#ifdef __NR_setresuid + [__NR_setresuid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESUID_E, PPME_SYSCALL_SETRESUID_X }, +#endif +#ifdef __NR_setresuid32 + [__NR_setresuid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESUID_E, PPME_SYSCALL_SETRESUID_X }, +#endif +#ifdef __NR_setresgid + [__NR_setresgid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESGID_E, PPME_SYSCALL_SETRESGID_X }, +#endif +#ifdef __NR_setresgid32 + [__NR_setresgid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESGID_E, PPME_SYSCALL_SETRESGID_X }, +#endif +#ifdef __NR_setuid + [__NR_setuid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETUID_E, PPME_SYSCALL_SETUID_X }, +#endif +#ifdef __NR_setuid32 + [__NR_setuid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETUID_E, PPME_SYSCALL_SETUID_X }, +#endif +#ifdef __NR_setgid + [__NR_setgid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETGID_E, PPME_SYSCALL_SETGID_X }, +#endif +#ifdef __NR_setgid32 + [__NR_setgid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETGID_E, PPME_SYSCALL_SETGID_X }, +#endif +#ifdef __NR_getuid + [__NR_getuid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETUID_E, PPME_SYSCALL_GETUID_X }, +#endif +#ifdef __NR_getuid32 + [__NR_getuid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETUID_E, PPME_SYSCALL_GETUID_X }, +#endif +#ifdef __NR_geteuid + [__NR_geteuid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETEUID_E, PPME_SYSCALL_GETEUID_X }, +#endif +#ifdef __NR_geteuid32 + [__NR_geteuid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETEUID_E, PPME_SYSCALL_GETEUID_X }, +#endif +#ifdef __NR_getgid + [__NR_getgid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETGID_E, PPME_SYSCALL_GETGID_X }, +#endif +#ifdef __NR_getgid32 + [__NR_getgid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETGID_E, PPME_SYSCALL_GETGID_X }, +#endif +#ifdef __NR_getegid + [__NR_getegid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETEGID_E, PPME_SYSCALL_GETEGID_X }, +#endif +#ifdef __NR_getegid32 + [__NR_getegid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETEGID_E, PPME_SYSCALL_GETEGID_X }, +#endif +#ifdef __NR_getresuid + [__NR_getresuid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETRESUID_E, PPME_SYSCALL_GETRESUID_X }, +#endif +#ifdef __NR_getresuid32 + [__NR_getresuid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETRESUID_E, PPME_SYSCALL_GETRESUID_X }, +#endif +#ifdef __NR_getresgid + [__NR_getresgid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETRESGID_E, PPME_SYSCALL_GETRESGID_X }, +#endif +#ifdef __NR_getresgid32 + [__NR_getresgid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_GETRESGID_E, PPME_SYSCALL_GETRESGID_X }, +#endif + [__NR_getdents - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_GETDENTS_E, PPME_SYSCALL_GETDENTS_X}, + [__NR_getdents64 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_GETDENTS64_E, PPME_SYSCALL_GETDENTS64_X}, +#ifdef __NR_setns + [__NR_setns - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETNS_E, PPME_SYSCALL_SETNS_X}, +#endif +#ifdef __NR_unshare + [__NR_unshare - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_UNSHARE_E, PPME_SYSCALL_UNSHARE_X}, +#endif + [__NR_flock - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_FLOCK_E, PPME_SYSCALL_FLOCK_X}, +#ifdef __NR_semop + [__NR_semop - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SEMOP_E, PPME_SYSCALL_SEMOP_X}, +#endif +#ifdef __NR_semget + [__NR_semget - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SEMGET_E, PPME_SYSCALL_SEMGET_X}, +#endif +#ifdef __NR_semctl + [__NR_semctl - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SEMCTL_E, PPME_SYSCALL_SEMCTL_X}, +#endif + [__NR_ppoll - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_PPOLL_E, PPME_SYSCALL_PPOLL_X}, +#ifdef __NR_access + [__NR_access - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_ACCESS_E, PPME_SYSCALL_ACCESS_X}, +#endif +#ifdef __NR_chroot + [__NR_chroot - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_CHROOT_E, PPME_SYSCALL_CHROOT_X}, +#endif + [__NR_setsid - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SETSID_E, PPME_SYSCALL_SETSID_X}, + [__NR_setpgid - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SETPGID_E, PPME_SYSCALL_SETPGID_X}, +#ifdef __NR_bpf + [__NR_bpf - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_BPF_E, PPME_SYSCALL_BPF_X}, +#endif +#ifdef __NR_seccomp + [__NR_seccomp - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SECCOMP_E, PPME_SYSCALL_SECCOMP_X}, +#endif +#ifdef __NR_renameat2 + [__NR_renameat2 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RENAMEAT2_E, PPME_SYSCALL_RENAMEAT2_X}, +#endif }; /* * SYSCALL ROUTING TABLE */ const enum ppm_syscall_code g_syscall_code_routing_table[SYSCALL_TABLE_SIZE] = { - [__NR_restart_syscall] = PPM_SC_RESTART_SYSCALL, - [__NR_exit] = PPM_SC_EXIT, - [__NR_read] = PPM_SC_READ, - [__NR_write] = PPM_SC_WRITE, - [__NR_open] = PPM_SC_OPEN, - [__NR_close] = PPM_SC_CLOSE, - [__NR_creat] = PPM_SC_CREAT, - [__NR_link] = PPM_SC_LINK, - [__NR_unlink] = PPM_SC_UNLINK, - [__NR_chdir] = PPM_SC_CHDIR, - [__NR_time] = PPM_SC_TIME, - [__NR_mknod] = PPM_SC_MKNOD, - [__NR_chmod] = PPM_SC_CHMOD, -/* [__NR_lchown16] = PPM_SC_NR_LCHOWN16, */ - [__NR_stat] = PPM_SC_STAT, - [__NR_lseek] = PPM_SC_LSEEK, - [__NR_getpid] = PPM_SC_GETPID, - [__NR_mount] = PPM_SC_MOUNT, -/* [__NR_oldumount] = PPM_SC_NR_OLDUMOUNT, */ -/* [__NR_setuid16] = PPM_SC_NR_SETUID16, */ -/* [__NR_getuid16] = PPM_SC_NR_GETUID16, */ - [__NR_ptrace] = PPM_SC_PTRACE, - [__NR_alarm] = PPM_SC_ALARM, - [__NR_fstat] = PPM_SC_FSTAT, - [__NR_pause] = PPM_SC_PAUSE, - [__NR_utime] = PPM_SC_UTIME, - [__NR_access] = PPM_SC_ACCESS, - [__NR_sync] = PPM_SC_SYNC, - [__NR_kill] = PPM_SC_KILL, - [__NR_rename] = PPM_SC_RENAME, - [__NR_mkdir] = PPM_SC_MKDIR, - [__NR_rmdir] = PPM_SC_RMDIR, - [__NR_dup] = PPM_SC_DUP, - [__NR_pipe] = PPM_SC_PIPE, - [__NR_times] = PPM_SC_TIMES, - [__NR_brk] = PPM_SC_BRK, -/* [__NR_setgid16] = PPM_SC_NR_SETGID16, */ -/* [__NR_getgid16] = PPM_SC_NR_GETGID16, */ -/* [__NR_geteuid16] = PPM_SC_NR_GETEUID16, */ -/* [__NR_getegid16] = PPM_SC_NR_GETEGID16, */ - [__NR_acct] = PPM_SC_ACCT, - [__NR_ioctl] = PPM_SC_IOCTL, - [__NR_fcntl] = PPM_SC_FCNTL, - [__NR_setpgid] = PPM_SC_SETPGID, - [__NR_umask] = PPM_SC_UMASK, - [__NR_chroot] = PPM_SC_CHROOT, - [__NR_ustat] = PPM_SC_USTAT, - [__NR_dup2] = PPM_SC_DUP2, - [__NR_getppid] = PPM_SC_GETPPID, - [__NR_getpgrp] = PPM_SC_GETPGRP, - [__NR_setsid] = PPM_SC_SETSID, - [__NR_sethostname] = PPM_SC_SETHOSTNAME, - [__NR_setrlimit] = PPM_SC_SETRLIMIT, -/* [__NR_old_getrlimit] = PPM_SC_NR_OLD_GETRLIMIT, */ - [__NR_getrusage] = PPM_SC_GETRUSAGE, - [__NR_gettimeofday] = PPM_SC_GETTIMEOFDAY, - [__NR_settimeofday] = PPM_SC_SETTIMEOFDAY, -/* [__NR_getgroups16] = PPM_SC_NR_GETGROUPS16, */ -/* [__NR_setgroups16] = PPM_SC_NR_SETGROUPS16, */ -/* [__NR_old_select] = PPM_SC_NR_OLD_SELECT, */ - [__NR_symlink] = PPM_SC_SYMLINK, - [__NR_lstat] = PPM_SC_LSTAT, - [__NR_readlink] = PPM_SC_READLINK, - [__NR_uselib] = PPM_SC_USELIB, - [__NR_swapon] = PPM_SC_SWAPON, - [__NR_reboot] = PPM_SC_REBOOT, -/* [__NR_old_readdir] = PPM_SC_NR_OLD_READDIR, */ -/* [__NR_old_mmap] = PPM_SC_NR_OLD_MMAP, */ - [__NR_mmap] = PPM_SC_MMAP, - [__NR_munmap] = PPM_SC_MUNMAP, - [__NR_truncate] = PPM_SC_TRUNCATE, - [__NR_ftruncate] = PPM_SC_FTRUNCATE, - [__NR_fchmod] = PPM_SC_FCHMOD, -/* [__NR_fchown16] = PPM_SC_NR_FCHOWN16, */ - [__NR_getpriority] = PPM_SC_GETPRIORITY, - [__NR_setpriority] = PPM_SC_SETPRIORITY, - [__NR_statfs] = PPM_SC_STATFS, - [__NR_fstatfs] = PPM_SC_FSTATFS, - [__NR_syslog] = PPM_SC_SYSLOG, - [__NR_setitimer] = PPM_SC_SETITIMER, - [__NR_getitimer] = PPM_SC_GETITIMER, -/* [__NR_newstat] = PPM_SC_NR_NEWSTAT, */ -/* [__NR_newlstat] = PPM_SC_NR_NEWLSTAT, */ -/* [__NR_newfstat] = PPM_SC_NR_NEWFSTAT, */ - [__NR_uname] = PPM_SC_UNAME, - [__NR_vhangup] = PPM_SC_VHANGUP, - [__NR_wait4] = PPM_SC_WAIT4, - [__NR_swapoff] = PPM_SC_SWAPOFF, - [__NR_sysinfo] = PPM_SC_SYSINFO, - [__NR_fsync] = PPM_SC_FSYNC, - [__NR_setdomainname] = PPM_SC_SETDOMAINNAME, -/* [__NR_newuname] = PPM_SC_NR_NEWUNAME, */ - [__NR_adjtimex] = PPM_SC_ADJTIMEX, - [__NR_mprotect] = PPM_SC_MPROTECT, - [__NR_init_module] = PPM_SC_INIT_MODULE, - [__NR_delete_module] = PPM_SC_DELETE_MODULE, - [__NR_quotactl] = PPM_SC_QUOTACTL, - [__NR_getpgid] = PPM_SC_GETPGID, - [__NR_fchdir] = PPM_SC_FCHDIR, - [__NR_sysfs] = PPM_SC_SYSFS, - [__NR_personality] = PPM_SC_PERSONALITY, -/* [__NR_setfsuid16] = PPM_SC_NR_SETFSUID16, */ -/* [__NR_setfsgid16] = PPM_SC_NR_SETFSGID16, */ -/* [__NR_llseek] = PPM_SC_NR_LLSEEK, */ - [__NR_getdents] = PPM_SC_GETDENTS, - [__NR_select] = PPM_SC_SELECT, - [__NR_flock] = PPM_SC_FLOCK, - [__NR_msync] = PPM_SC_MSYNC, - [__NR_readv] = PPM_SC_READV, - [__NR_writev] = PPM_SC_WRITEV, - [__NR_getsid] = PPM_SC_GETSID, - [__NR_fdatasync] = PPM_SC_FDATASYNC, -/* [__NR_sysctl] = PPM_SC_NR_SYSCTL, */ - [__NR_mlock] = PPM_SC_MLOCK, - [__NR_munlock] = PPM_SC_MUNLOCK, - [__NR_mlockall] = PPM_SC_MLOCKALL, - [__NR_munlockall] = PPM_SC_MUNLOCKALL, - [__NR_sched_setparam] = PPM_SC_SCHED_SETPARAM, - [__NR_sched_getparam] = PPM_SC_SCHED_GETPARAM, - [__NR_sched_setscheduler] = PPM_SC_SCHED_SETSCHEDULER, - [__NR_sched_getscheduler] = PPM_SC_SCHED_GETSCHEDULER, - [__NR_sched_yield] = PPM_SC_SCHED_YIELD, - [__NR_sched_get_priority_max] = PPM_SC_SCHED_GET_PRIORITY_MAX, - [__NR_sched_get_priority_min] = PPM_SC_SCHED_GET_PRIORITY_MIN, - [__NR_sched_rr_get_interval] = PPM_SC_SCHED_RR_GET_INTERVAL, - [__NR_nanosleep] = PPM_SC_NANOSLEEP, - [__NR_mremap] = PPM_SC_MREMAP, -/* [__NR_setresuid16] = PPM_SC_NR_SETRESUID16, */ -/* [__NR_getresuid16] = PPM_SC_NR_GETRESUID16, */ - [__NR_poll] = PPM_SC_POLL, -/* [__NR_setresgid16] = PPM_SC_NR_SETRESGID16, */ -/* [__NR_getresgid16] = PPM_SC_NR_GETRESGID16, */ - [__NR_prctl] = PPM_SC_PRCTL, - [__NR_rt_sigaction] = PPM_SC_RT_SIGACTION, - [__NR_rt_sigprocmask] = PPM_SC_RT_SIGPROCMASK, - [__NR_rt_sigpending] = PPM_SC_RT_SIGPENDING, - [__NR_rt_sigtimedwait] = PPM_SC_RT_SIGTIMEDWAIT, - [__NR_rt_sigqueueinfo] = PPM_SC_RT_SIGQUEUEINFO, - [__NR_rt_sigsuspend] = PPM_SC_RT_SIGSUSPEND, -/* [__NR_chown16] = PPM_SC_NR_CHOWN16, */ - [__NR_getcwd] = PPM_SC_GETCWD, - [__NR_capget] = PPM_SC_CAPGET, - [__NR_capset] = PPM_SC_CAPSET, - [__NR_sendfile] = PPM_SC_SENDFILE, - [__NR_getrlimit] = PPM_SC_GETRLIMIT, -/* [__NR_mmap_pgoff] = PPM_SC_NR_MMAP_PGOFF, */ - [__NR_lchown] = PPM_SC_LCHOWN, - [__NR_getuid] = PPM_SC_GETUID, - [__NR_getgid] = PPM_SC_GETGID, - [__NR_geteuid] = PPM_SC_GETEUID, - [__NR_getegid] = PPM_SC_GETEGID, - [__NR_setreuid] = PPM_SC_SETREUID, - [__NR_setregid] = PPM_SC_SETREGID, - [__NR_getgroups] = PPM_SC_GETGROUPS, - [__NR_setgroups] = PPM_SC_SETGROUPS, - [__NR_fchown] = PPM_SC_FCHOWN, - [__NR_setresuid] = PPM_SC_SETRESUID, - [__NR_getresuid] = PPM_SC_GETRESUID, - [__NR_setresgid] = PPM_SC_SETRESGID, - [__NR_getresgid] = PPM_SC_GETRESGID, - [__NR_chown] = PPM_SC_CHOWN, - [__NR_setuid] = PPM_SC_SETUID, - [__NR_setgid] = PPM_SC_SETGID, - [__NR_setfsuid] = PPM_SC_SETFSUID, - [__NR_setfsgid] = PPM_SC_SETFSGID, - [__NR_pivot_root] = PPM_SC_PIVOT_ROOT, - [__NR_mincore] = PPM_SC_MINCORE, - [__NR_madvise] = PPM_SC_MADVISE, - [__NR_gettid] = PPM_SC_GETTID, - [__NR_setxattr] = PPM_SC_SETXATTR, - [__NR_lsetxattr] = PPM_SC_LSETXATTR, - [__NR_fsetxattr] = PPM_SC_FSETXATTR, - [__NR_getxattr] = PPM_SC_GETXATTR, - [__NR_lgetxattr] = PPM_SC_LGETXATTR, - [__NR_fgetxattr] = PPM_SC_FGETXATTR, - [__NR_listxattr] = PPM_SC_LISTXATTR, - [__NR_llistxattr] = PPM_SC_LLISTXATTR, - [__NR_flistxattr] = PPM_SC_FLISTXATTR, - [__NR_removexattr] = PPM_SC_REMOVEXATTR, - [__NR_lremovexattr] = PPM_SC_LREMOVEXATTR, - [__NR_fremovexattr] = PPM_SC_FREMOVEXATTR, - [__NR_tkill] = PPM_SC_TKILL, - [__NR_futex] = PPM_SC_FUTEX, - [__NR_sched_setaffinity] = PPM_SC_SCHED_SETAFFINITY, - [__NR_sched_getaffinity] = PPM_SC_SCHED_GETAFFINITY, - [__NR_set_thread_area] = PPM_SC_SET_THREAD_AREA, - [__NR_get_thread_area] = PPM_SC_GET_THREAD_AREA, - [__NR_io_setup] = PPM_SC_IO_SETUP, - [__NR_io_destroy] = PPM_SC_IO_DESTROY, - [__NR_io_getevents] = PPM_SC_IO_GETEVENTS, - [__NR_io_submit] = PPM_SC_IO_SUBMIT, - [__NR_io_cancel] = PPM_SC_IO_CANCEL, - [__NR_exit_group] = PPM_SC_EXIT_GROUP, - [__NR_epoll_create] = PPM_SC_EPOLL_CREATE, - [__NR_epoll_ctl] = PPM_SC_EPOLL_CTL, - [__NR_epoll_wait] = PPM_SC_EPOLL_WAIT, - [__NR_remap_file_pages] = PPM_SC_REMAP_FILE_PAGES, - [__NR_set_tid_address] = PPM_SC_SET_TID_ADDRESS, - [__NR_timer_create] = PPM_SC_TIMER_CREATE, - [__NR_timer_settime] = PPM_SC_TIMER_SETTIME, - [__NR_timer_gettime] = PPM_SC_TIMER_GETTIME, - [__NR_timer_getoverrun] = PPM_SC_TIMER_GETOVERRUN, - [__NR_timer_delete] = PPM_SC_TIMER_DELETE, - [__NR_clock_settime] = PPM_SC_CLOCK_SETTIME, - [__NR_clock_gettime] = PPM_SC_CLOCK_GETTIME, - [__NR_clock_getres] = PPM_SC_CLOCK_GETRES, - [__NR_clock_nanosleep] = PPM_SC_CLOCK_NANOSLEEP, - [__NR_tgkill] = PPM_SC_TGKILL, - [__NR_utimes] = PPM_SC_UTIMES, - [__NR_mq_open] = PPM_SC_MQ_OPEN, - [__NR_mq_unlink] = PPM_SC_MQ_UNLINK, - [__NR_mq_timedsend] = PPM_SC_MQ_TIMEDSEND, - [__NR_mq_timedreceive] = PPM_SC_MQ_TIMEDRECEIVE, - [__NR_mq_notify] = PPM_SC_MQ_NOTIFY, - [__NR_mq_getsetattr] = PPM_SC_MQ_GETSETATTR, - [__NR_kexec_load] = PPM_SC_KEXEC_LOAD, - [__NR_waitid] = PPM_SC_WAITID, - [__NR_add_key] = PPM_SC_ADD_KEY, - [__NR_request_key] = PPM_SC_REQUEST_KEY, - [__NR_keyctl] = PPM_SC_KEYCTL, - [__NR_ioprio_set] = PPM_SC_IOPRIO_SET, - [__NR_ioprio_get] = PPM_SC_IOPRIO_GET, - [__NR_inotify_init] = PPM_SC_INOTIFY_INIT, - [__NR_inotify_add_watch] = PPM_SC_INOTIFY_ADD_WATCH, - [__NR_inotify_rm_watch] = PPM_SC_INOTIFY_RM_WATCH, - [__NR_openat] = PPM_SC_OPENAT, - [__NR_mkdirat] = PPM_SC_MKDIRAT, - [__NR_mknodat] = PPM_SC_MKNODAT, - [__NR_fchownat] = PPM_SC_FCHOWNAT, - [__NR_futimesat] = PPM_SC_FUTIMESAT, - [__NR_unlinkat] = PPM_SC_UNLINKAT, - [__NR_renameat] = PPM_SC_RENAMEAT, - [__NR_linkat] = PPM_SC_LINKAT, - [__NR_symlinkat] = PPM_SC_SYMLINKAT, - [__NR_readlinkat] = PPM_SC_READLINKAT, - [__NR_fchmodat] = PPM_SC_FCHMODAT, - [__NR_faccessat] = PPM_SC_FACCESSAT, - [__NR_pselect6] = PPM_SC_PSELECT6, - [__NR_ppoll] = PPM_SC_PPOLL, - [__NR_unshare] = PPM_SC_UNSHARE, - [__NR_set_robust_list] = PPM_SC_SET_ROBUST_LIST, - [__NR_get_robust_list] = PPM_SC_GET_ROBUST_LIST, - [__NR_splice] = PPM_SC_SPLICE, - [__NR_tee] = PPM_SC_TEE, - [__NR_vmsplice] = PPM_SC_VMSPLICE, + [__NR_restart_syscall - SYSCALL_TABLE_ID0] = PPM_SC_RESTART_SYSCALL, + [__NR_exit - SYSCALL_TABLE_ID0] = PPM_SC_EXIT, + [__NR_read - SYSCALL_TABLE_ID0] = PPM_SC_READ, + [__NR_write - SYSCALL_TABLE_ID0] = PPM_SC_WRITE, + [__NR_open - SYSCALL_TABLE_ID0] = PPM_SC_OPEN, + [__NR_close - SYSCALL_TABLE_ID0] = PPM_SC_CLOSE, + [__NR_creat - SYSCALL_TABLE_ID0] = PPM_SC_CREAT, + [__NR_link - SYSCALL_TABLE_ID0] = PPM_SC_LINK, + [__NR_unlink - SYSCALL_TABLE_ID0] = PPM_SC_UNLINK, + [__NR_chdir - SYSCALL_TABLE_ID0] = PPM_SC_CHDIR, +#ifdef __NR_time + [__NR_time - SYSCALL_TABLE_ID0] = PPM_SC_TIME, +#endif + [__NR_mknod - SYSCALL_TABLE_ID0] = PPM_SC_MKNOD, + [__NR_chmod - SYSCALL_TABLE_ID0] = PPM_SC_CHMOD, +/* [__NR_lchown16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_LCHOWN16, */ + [__NR_stat - SYSCALL_TABLE_ID0] = PPM_SC_STAT, + [__NR_lseek - SYSCALL_TABLE_ID0] = PPM_SC_LSEEK, + [__NR_getpid - SYSCALL_TABLE_ID0] = PPM_SC_GETPID, + [__NR_mount - SYSCALL_TABLE_ID0] = PPM_SC_MOUNT, +/* [__NR_oldumount - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLDUMOUNT, */ +/* [__NR_setuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETUID16, */ +/* [__NR_getuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETUID16, */ + [__NR_ptrace - SYSCALL_TABLE_ID0] = PPM_SC_PTRACE, +#ifdef __NR_alarm + [__NR_alarm - SYSCALL_TABLE_ID0] = PPM_SC_ALARM, +#endif + [__NR_fstat - SYSCALL_TABLE_ID0] = PPM_SC_FSTAT, + [__NR_pause - SYSCALL_TABLE_ID0] = PPM_SC_PAUSE, +#ifdef __NR_utime + [__NR_utime - SYSCALL_TABLE_ID0] = PPM_SC_UTIME, +#endif + [__NR_sync - SYSCALL_TABLE_ID0] = PPM_SC_SYNC, + [__NR_kill - SYSCALL_TABLE_ID0] = PPM_SC_KILL, + [__NR_rename - SYSCALL_TABLE_ID0] = PPM_SC_RENAME, + [__NR_mkdir - SYSCALL_TABLE_ID0] = PPM_SC_MKDIR, + [__NR_rmdir - SYSCALL_TABLE_ID0] = PPM_SC_RMDIR, + [__NR_dup - SYSCALL_TABLE_ID0] = PPM_SC_DUP, + [__NR_pipe - SYSCALL_TABLE_ID0] = PPM_SC_PIPE, + [__NR_times - SYSCALL_TABLE_ID0] = PPM_SC_TIMES, + [__NR_brk - SYSCALL_TABLE_ID0] = PPM_SC_BRK, +/* [__NR_setgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETGID16, */ +/* [__NR_getgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETGID16, */ +/* [__NR_geteuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETEUID16, */ +/* [__NR_getegid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETEGID16, */ + [__NR_acct - SYSCALL_TABLE_ID0] = PPM_SC_ACCT, + [__NR_ioctl - SYSCALL_TABLE_ID0] = PPM_SC_IOCTL, + [__NR_fcntl - SYSCALL_TABLE_ID0] = PPM_SC_FCNTL, + [__NR_setpgid - SYSCALL_TABLE_ID0] = PPM_SC_SETPGID, + [__NR_umask - SYSCALL_TABLE_ID0] = PPM_SC_UMASK, + [__NR_chroot - SYSCALL_TABLE_ID0] = PPM_SC_CHROOT, + [__NR_ustat - SYSCALL_TABLE_ID0] = PPM_SC_USTAT, + [__NR_dup2 - SYSCALL_TABLE_ID0] = PPM_SC_DUP2, + [__NR_getppid - SYSCALL_TABLE_ID0] = PPM_SC_GETPPID, + [__NR_getpgrp - SYSCALL_TABLE_ID0] = PPM_SC_GETPGRP, + [__NR_setsid - SYSCALL_TABLE_ID0] = PPM_SC_SETSID, + [__NR_sethostname - SYSCALL_TABLE_ID0] = PPM_SC_SETHOSTNAME, + [__NR_setrlimit - SYSCALL_TABLE_ID0] = PPM_SC_SETRLIMIT, +/* [__NR_old_getrlimit - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_GETRLIMIT, */ + [__NR_getrusage - SYSCALL_TABLE_ID0] = PPM_SC_GETRUSAGE, + [__NR_gettimeofday - SYSCALL_TABLE_ID0] = PPM_SC_GETTIMEOFDAY, + [__NR_settimeofday - SYSCALL_TABLE_ID0] = PPM_SC_SETTIMEOFDAY, +/* [__NR_getgroups16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETGROUPS16, */ +/* [__NR_setgroups16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETGROUPS16, */ +/* [__NR_old_select - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_SELECT, */ + [__NR_symlink - SYSCALL_TABLE_ID0] = PPM_SC_SYMLINK, + [__NR_lstat - SYSCALL_TABLE_ID0] = PPM_SC_LSTAT, + [__NR_readlink - SYSCALL_TABLE_ID0] = PPM_SC_READLINK, + [__NR_uselib - SYSCALL_TABLE_ID0] = PPM_SC_USELIB, + [__NR_swapon - SYSCALL_TABLE_ID0] = PPM_SC_SWAPON, + [__NR_reboot - SYSCALL_TABLE_ID0] = PPM_SC_REBOOT, +/* [__NR_old_readdir - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_READDIR, */ +/* [__NR_old_mmap - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_MMAP, */ +#ifdef __NR_mmap + [__NR_mmap - SYSCALL_TABLE_ID0] = PPM_SC_MMAP, +#endif + [__NR_munmap - SYSCALL_TABLE_ID0] = PPM_SC_MUNMAP, + [__NR_truncate - SYSCALL_TABLE_ID0] = PPM_SC_TRUNCATE, + [__NR_ftruncate - SYSCALL_TABLE_ID0] = PPM_SC_FTRUNCATE, + [__NR_fchmod - SYSCALL_TABLE_ID0] = PPM_SC_FCHMOD, +/* [__NR_fchown16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_FCHOWN16, */ + [__NR_getpriority - SYSCALL_TABLE_ID0] = PPM_SC_GETPRIORITY, + [__NR_setpriority - SYSCALL_TABLE_ID0] = PPM_SC_SETPRIORITY, + [__NR_statfs - SYSCALL_TABLE_ID0] = PPM_SC_STATFS, + [__NR_fstatfs - SYSCALL_TABLE_ID0] = PPM_SC_FSTATFS, + [__NR_syslog - SYSCALL_TABLE_ID0] = PPM_SC_SYSLOG, + [__NR_setitimer - SYSCALL_TABLE_ID0] = PPM_SC_SETITIMER, + [__NR_getitimer - SYSCALL_TABLE_ID0] = PPM_SC_GETITIMER, +/* [__NR_newstat - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWSTAT, */ +/* [__NR_newlstat - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWLSTAT, */ +/* [__NR_newfstat - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWFSTAT, */ + [__NR_uname - SYSCALL_TABLE_ID0] = PPM_SC_UNAME, + [__NR_vhangup - SYSCALL_TABLE_ID0] = PPM_SC_VHANGUP, + [__NR_wait4 - SYSCALL_TABLE_ID0] = PPM_SC_WAIT4, + [__NR_swapoff - SYSCALL_TABLE_ID0] = PPM_SC_SWAPOFF, + [__NR_sysinfo - SYSCALL_TABLE_ID0] = PPM_SC_SYSINFO, + [__NR_fsync - SYSCALL_TABLE_ID0] = PPM_SC_FSYNC, + [__NR_setdomainname - SYSCALL_TABLE_ID0] = PPM_SC_SETDOMAINNAME, +/* [__NR_newuname - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWUNAME, */ + [__NR_adjtimex - SYSCALL_TABLE_ID0] = PPM_SC_ADJTIMEX, + [__NR_mprotect - SYSCALL_TABLE_ID0] = PPM_SC_MPROTECT, + [__NR_init_module - SYSCALL_TABLE_ID0] = PPM_SC_INIT_MODULE, + [__NR_delete_module - SYSCALL_TABLE_ID0] = PPM_SC_DELETE_MODULE, + [__NR_getpgid - SYSCALL_TABLE_ID0] = PPM_SC_GETPGID, + [__NR_fchdir - SYSCALL_TABLE_ID0] = PPM_SC_FCHDIR, + [__NR_sysfs - SYSCALL_TABLE_ID0] = PPM_SC_SYSFS, + [__NR_personality - SYSCALL_TABLE_ID0] = PPM_SC_PERSONALITY, +/* [__NR_setfsuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETFSUID16, */ +/* [__NR_setfsgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETFSGID16, */ +/* [__NR_llseek - SYSCALL_TABLE_ID0] = PPM_SC_NR_LLSEEK, */ + [__NR_getdents - SYSCALL_TABLE_ID0] = PPM_SC_GETDENTS, +#ifdef __NR_select + [__NR_select - SYSCALL_TABLE_ID0] = PPM_SC_SELECT, +#endif + [__NR_flock - SYSCALL_TABLE_ID0] = PPM_SC_FLOCK, + [__NR_msync - SYSCALL_TABLE_ID0] = PPM_SC_MSYNC, + [__NR_readv - SYSCALL_TABLE_ID0] = PPM_SC_READV, + [__NR_writev - SYSCALL_TABLE_ID0] = PPM_SC_WRITEV, + [__NR_getsid - SYSCALL_TABLE_ID0] = PPM_SC_GETSID, + [__NR_fdatasync - SYSCALL_TABLE_ID0] = PPM_SC_FDATASYNC, +/* [__NR_sysctl - SYSCALL_TABLE_ID0] = PPM_SC_NR_SYSCTL, */ + [__NR_mlock - SYSCALL_TABLE_ID0] = PPM_SC_MLOCK, + [__NR_munlock - SYSCALL_TABLE_ID0] = PPM_SC_MUNLOCK, + [__NR_mlockall - SYSCALL_TABLE_ID0] = PPM_SC_MLOCKALL, + [__NR_munlockall - SYSCALL_TABLE_ID0] = PPM_SC_MUNLOCKALL, + [__NR_sched_setparam - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_SETPARAM, + [__NR_sched_getparam - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GETPARAM, + [__NR_sched_setscheduler - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_SETSCHEDULER, + [__NR_sched_getscheduler - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GETSCHEDULER, + [__NR_sched_yield - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_YIELD, + [__NR_sched_get_priority_max - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GET_PRIORITY_MAX, + [__NR_sched_get_priority_min - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GET_PRIORITY_MIN, + [__NR_sched_rr_get_interval - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_RR_GET_INTERVAL, + [__NR_nanosleep - SYSCALL_TABLE_ID0] = PPM_SC_NANOSLEEP, + [__NR_mremap - SYSCALL_TABLE_ID0] = PPM_SC_MREMAP, +/* [__NR_setresuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETRESUID16, */ +/* [__NR_getresuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETRESUID16, */ + [__NR_poll - SYSCALL_TABLE_ID0] = PPM_SC_POLL, +/* [__NR_setresgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETRESGID16, */ +/* [__NR_getresgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETRESGID16, */ + [__NR_prctl - SYSCALL_TABLE_ID0] = PPM_SC_PRCTL, +#ifdef __NR_arch_prctl + [__NR_arch_prctl - SYSCALL_TABLE_ID0] = PPM_SC_ARCH_PRCTL, +#endif + [__NR_rt_sigaction - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGACTION, + [__NR_rt_sigprocmask - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGPROCMASK, + [__NR_rt_sigpending - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGPENDING, + [__NR_rt_sigtimedwait - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGTIMEDWAIT, + [__NR_rt_sigqueueinfo - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGQUEUEINFO, + [__NR_rt_sigsuspend - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGSUSPEND, +/* [__NR_chown16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_CHOWN16, */ + [__NR_getcwd - SYSCALL_TABLE_ID0] = PPM_SC_GETCWD, + [__NR_capget - SYSCALL_TABLE_ID0] = PPM_SC_CAPGET, + [__NR_capset - SYSCALL_TABLE_ID0] = PPM_SC_CAPSET, + [__NR_sendfile - SYSCALL_TABLE_ID0] = PPM_SC_SENDFILE, +#ifdef __NR_getrlimit + [__NR_getrlimit - SYSCALL_TABLE_ID0] = PPM_SC_GETRLIMIT, +#endif +/* [__NR_mmap_pgoff - SYSCALL_TABLE_ID0] = PPM_SC_NR_MMAP_PGOFF, */ + [__NR_lchown - SYSCALL_TABLE_ID0] = PPM_SC_LCHOWN, + [__NR_setreuid - SYSCALL_TABLE_ID0] = PPM_SC_SETREUID, + [__NR_setregid - SYSCALL_TABLE_ID0] = PPM_SC_SETREGID, + [__NR_getgroups - SYSCALL_TABLE_ID0] = PPM_SC_GETGROUPS, + [__NR_setgroups - SYSCALL_TABLE_ID0] = PPM_SC_SETGROUPS, + [__NR_fchown - SYSCALL_TABLE_ID0] = PPM_SC_FCHOWN, + [__NR_chown - SYSCALL_TABLE_ID0] = PPM_SC_CHOWN, + [__NR_setfsuid - SYSCALL_TABLE_ID0] = PPM_SC_SETFSUID, + [__NR_setfsgid - SYSCALL_TABLE_ID0] = PPM_SC_SETFSGID, + [__NR_pivot_root - SYSCALL_TABLE_ID0] = PPM_SC_PIVOT_ROOT, + [__NR_mincore - SYSCALL_TABLE_ID0] = PPM_SC_MINCORE, + [__NR_madvise - SYSCALL_TABLE_ID0] = PPM_SC_MADVISE, + [__NR_gettid - SYSCALL_TABLE_ID0] = PPM_SC_GETTID, + [__NR_setxattr - SYSCALL_TABLE_ID0] = PPM_SC_SETXATTR, + [__NR_lsetxattr - SYSCALL_TABLE_ID0] = PPM_SC_LSETXATTR, + [__NR_fsetxattr - SYSCALL_TABLE_ID0] = PPM_SC_FSETXATTR, + [__NR_getxattr - SYSCALL_TABLE_ID0] = PPM_SC_GETXATTR, + [__NR_lgetxattr - SYSCALL_TABLE_ID0] = PPM_SC_LGETXATTR, + [__NR_fgetxattr - SYSCALL_TABLE_ID0] = PPM_SC_FGETXATTR, + [__NR_listxattr - SYSCALL_TABLE_ID0] = PPM_SC_LISTXATTR, + [__NR_llistxattr - SYSCALL_TABLE_ID0] = PPM_SC_LLISTXATTR, + [__NR_flistxattr - SYSCALL_TABLE_ID0] = PPM_SC_FLISTXATTR, + [__NR_removexattr - SYSCALL_TABLE_ID0] = PPM_SC_REMOVEXATTR, + [__NR_lremovexattr - SYSCALL_TABLE_ID0] = PPM_SC_LREMOVEXATTR, + [__NR_fremovexattr - SYSCALL_TABLE_ID0] = PPM_SC_FREMOVEXATTR, + [__NR_tkill - SYSCALL_TABLE_ID0] = PPM_SC_TKILL, + [__NR_futex - SYSCALL_TABLE_ID0] = PPM_SC_FUTEX, + [__NR_sched_setaffinity - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_SETAFFINITY, + [__NR_sched_getaffinity - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GETAFFINITY, +#ifdef __NR_set_thread_area + [__NR_set_thread_area - SYSCALL_TABLE_ID0] = PPM_SC_SET_THREAD_AREA, +#endif +#ifdef __NR_get_thread_area + [__NR_get_thread_area - SYSCALL_TABLE_ID0] = PPM_SC_GET_THREAD_AREA, +#endif + [__NR_io_setup - SYSCALL_TABLE_ID0] = PPM_SC_IO_SETUP, + [__NR_io_destroy - SYSCALL_TABLE_ID0] = PPM_SC_IO_DESTROY, + [__NR_io_getevents - SYSCALL_TABLE_ID0] = PPM_SC_IO_GETEVENTS, + [__NR_io_submit - SYSCALL_TABLE_ID0] = PPM_SC_IO_SUBMIT, + [__NR_io_cancel - SYSCALL_TABLE_ID0] = PPM_SC_IO_CANCEL, + [__NR_exit_group - SYSCALL_TABLE_ID0] = PPM_SC_EXIT_GROUP, + [__NR_epoll_create - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_CREATE, + [__NR_epoll_ctl - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_CTL, + [__NR_epoll_wait - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_WAIT, + [__NR_remap_file_pages - SYSCALL_TABLE_ID0] = PPM_SC_REMAP_FILE_PAGES, + [__NR_set_tid_address - SYSCALL_TABLE_ID0] = PPM_SC_SET_TID_ADDRESS, + [__NR_timer_create - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_CREATE, + [__NR_timer_settime - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_SETTIME, + [__NR_timer_gettime - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_GETTIME, + [__NR_timer_getoverrun - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_GETOVERRUN, + [__NR_timer_delete - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_DELETE, + [__NR_clock_settime - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_SETTIME, + [__NR_clock_gettime - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_GETTIME, + [__NR_clock_getres - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_GETRES, + [__NR_clock_nanosleep - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_NANOSLEEP, + [__NR_tgkill - SYSCALL_TABLE_ID0] = PPM_SC_TGKILL, + [__NR_utimes - SYSCALL_TABLE_ID0] = PPM_SC_UTIMES, + [__NR_mq_open - SYSCALL_TABLE_ID0] = PPM_SC_MQ_OPEN, + [__NR_mq_unlink - SYSCALL_TABLE_ID0] = PPM_SC_MQ_UNLINK, + [__NR_mq_timedsend - SYSCALL_TABLE_ID0] = PPM_SC_MQ_TIMEDSEND, + [__NR_mq_timedreceive - SYSCALL_TABLE_ID0] = PPM_SC_MQ_TIMEDRECEIVE, + [__NR_mq_notify - SYSCALL_TABLE_ID0] = PPM_SC_MQ_NOTIFY, + [__NR_mq_getsetattr - SYSCALL_TABLE_ID0] = PPM_SC_MQ_GETSETATTR, + [__NR_kexec_load - SYSCALL_TABLE_ID0] = PPM_SC_KEXEC_LOAD, + [__NR_waitid - SYSCALL_TABLE_ID0] = PPM_SC_WAITID, + [__NR_add_key - SYSCALL_TABLE_ID0] = PPM_SC_ADD_KEY, + [__NR_request_key - SYSCALL_TABLE_ID0] = PPM_SC_REQUEST_KEY, + [__NR_keyctl - SYSCALL_TABLE_ID0] = PPM_SC_KEYCTL, + [__NR_ioprio_set - SYSCALL_TABLE_ID0] = PPM_SC_IOPRIO_SET, + [__NR_ioprio_get - SYSCALL_TABLE_ID0] = PPM_SC_IOPRIO_GET, + [__NR_inotify_init - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_INIT, + [__NR_inotify_add_watch - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_ADD_WATCH, + [__NR_inotify_rm_watch - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_RM_WATCH, + [__NR_openat - SYSCALL_TABLE_ID0] = PPM_SC_OPENAT, + [__NR_mkdirat - SYSCALL_TABLE_ID0] = PPM_SC_MKDIRAT, + [__NR_mknodat - SYSCALL_TABLE_ID0] = PPM_SC_MKNODAT, + [__NR_fchownat - SYSCALL_TABLE_ID0] = PPM_SC_FCHOWNAT, + [__NR_futimesat - SYSCALL_TABLE_ID0] = PPM_SC_FUTIMESAT, + [__NR_unlinkat - SYSCALL_TABLE_ID0] = PPM_SC_UNLINKAT, + [__NR_renameat - SYSCALL_TABLE_ID0] = PPM_SC_RENAMEAT, + [__NR_linkat - SYSCALL_TABLE_ID0] = PPM_SC_LINKAT, + [__NR_symlinkat - SYSCALL_TABLE_ID0] = PPM_SC_SYMLINKAT, + [__NR_readlinkat - SYSCALL_TABLE_ID0] = PPM_SC_READLINKAT, + [__NR_fchmodat - SYSCALL_TABLE_ID0] = PPM_SC_FCHMODAT, + [__NR_faccessat - SYSCALL_TABLE_ID0] = PPM_SC_FACCESSAT, + [__NR_pselect6 - SYSCALL_TABLE_ID0] = PPM_SC_PSELECT6, + [__NR_ppoll - SYSCALL_TABLE_ID0] = PPM_SC_PPOLL, + [__NR_unshare - SYSCALL_TABLE_ID0] = PPM_SC_UNSHARE, + [__NR_set_robust_list - SYSCALL_TABLE_ID0] = PPM_SC_SET_ROBUST_LIST, + [__NR_get_robust_list - SYSCALL_TABLE_ID0] = PPM_SC_GET_ROBUST_LIST, + [__NR_splice - SYSCALL_TABLE_ID0] = PPM_SC_SPLICE, + [__NR_tee - SYSCALL_TABLE_ID0] = PPM_SC_TEE, + [__NR_vmsplice - SYSCALL_TABLE_ID0] = PPM_SC_VMSPLICE, #ifdef __NR_getcpu - [__NR_getcpu] = PPM_SC_GETCPU, -#endif - [__NR_epoll_pwait] = PPM_SC_EPOLL_PWAIT, - [__NR_utimensat] = PPM_SC_UTIMENSAT, - [__NR_signalfd] = PPM_SC_SIGNALFD, - [__NR_timerfd_create] = PPM_SC_TIMERFD_CREATE, - [__NR_eventfd] = PPM_SC_EVENTFD, - [__NR_timerfd_settime] = PPM_SC_TIMERFD_SETTIME, - [__NR_timerfd_gettime] = PPM_SC_TIMERFD_GETTIME, - [__NR_signalfd4] = PPM_SC_SIGNALFD4, - [__NR_eventfd2] = PPM_SC_EVENTFD2, - [__NR_epoll_create1] = PPM_SC_EPOLL_CREATE1, - [__NR_dup3] = PPM_SC_DUP3, - [__NR_pipe2] = PPM_SC_PIPE2, - [__NR_inotify_init1] = PPM_SC_INOTIFY_INIT1, - [__NR_preadv] = PPM_SC_PREADV, - [__NR_pwritev] = PPM_SC_PWRITEV, - [__NR_rt_tgsigqueueinfo] = PPM_SC_RT_TGSIGQUEUEINFO, - [__NR_perf_event_open] = PPM_SC_PERF_EVENT_OPEN, + [__NR_getcpu - SYSCALL_TABLE_ID0] = PPM_SC_GETCPU, +#endif + [__NR_epoll_pwait - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_PWAIT, + [__NR_utimensat - SYSCALL_TABLE_ID0] = PPM_SC_UTIMENSAT, + [__NR_signalfd - SYSCALL_TABLE_ID0] = PPM_SC_SIGNALFD, + [__NR_timerfd_create - SYSCALL_TABLE_ID0] = PPM_SC_TIMERFD_CREATE, + [__NR_eventfd - SYSCALL_TABLE_ID0] = PPM_SC_EVENTFD, + [__NR_timerfd_settime - SYSCALL_TABLE_ID0] = PPM_SC_TIMERFD_SETTIME, + [__NR_timerfd_gettime - SYSCALL_TABLE_ID0] = PPM_SC_TIMERFD_GETTIME, + [__NR_signalfd4 - SYSCALL_TABLE_ID0] = PPM_SC_SIGNALFD4, + [__NR_eventfd2 - SYSCALL_TABLE_ID0] = PPM_SC_EVENTFD2, + [__NR_epoll_create1 - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_CREATE1, + [__NR_dup3 - SYSCALL_TABLE_ID0] = PPM_SC_DUP3, + [__NR_pipe2 - SYSCALL_TABLE_ID0] = PPM_SC_PIPE2, + [__NR_inotify_init1 - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_INIT1, + [__NR_preadv - SYSCALL_TABLE_ID0] = PPM_SC_PREADV, + [__NR_pwritev - SYSCALL_TABLE_ID0] = PPM_SC_PWRITEV, + [__NR_rt_tgsigqueueinfo - SYSCALL_TABLE_ID0] = PPM_SC_RT_TGSIGQUEUEINFO, + [__NR_perf_event_open - SYSCALL_TABLE_ID0] = PPM_SC_PERF_EVENT_OPEN, #ifdef __NR_fanotify_init - [__NR_fanotify_init] = PPM_SC_FANOTIFY_INIT, + [__NR_fanotify_init - SYSCALL_TABLE_ID0] = PPM_SC_FANOTIFY_INIT, #endif #ifdef __NR_prlimit64 - [__NR_prlimit64] = PPM_SC_PRLIMIT64, + [__NR_prlimit64 - SYSCALL_TABLE_ID0] = PPM_SC_PRLIMIT64, #endif #ifdef __NR_clock_adjtime - [__NR_clock_adjtime] = PPM_SC_CLOCK_ADJTIME, + [__NR_clock_adjtime - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_ADJTIME, #endif #ifdef __NR_syncfs - [__NR_syncfs] = PPM_SC_SYNCFS, -#endif -#ifdef __NR_setns - [__NR_setns] = PPM_SC_SETNS, + [__NR_syncfs - SYSCALL_TABLE_ID0] = PPM_SC_SYNCFS, #endif - [__NR_getdents64] = PPM_SC_GETDENTS64, -#ifdef __x86_64__ + [__NR_getdents64 - SYSCALL_TABLE_ID0] = PPM_SC_GETDENTS64, +#ifndef __NR_socketcall /* * Non-multiplexed socket family */ - [__NR_socket] = PPM_SC_SOCKET, - [__NR_bind] = PPM_SC_BIND, - [__NR_connect] = PPM_SC_CONNECT, - [__NR_listen] = PPM_SC_LISTEN, - [__NR_accept] = PPM_SC_ACCEPT, - [__NR_getsockname] = PPM_SC_GETSOCKNAME, - [__NR_getpeername] = PPM_SC_GETPEERNAME, - [__NR_socketpair] = PPM_SC_SOCKETPAIR, -/* [__NR_send] = PPM_SC_NR_SEND, */ - [__NR_sendto] = PPM_SC_SENDTO, -/* [__NR_recv] = PPM_SC_NR_RECV, */ - [__NR_recvfrom] = PPM_SC_RECVFROM, - [__NR_shutdown] = PPM_SC_SHUTDOWN, - [__NR_setsockopt] = PPM_SC_SETSOCKOPT, - [__NR_getsockopt] = PPM_SC_GETSOCKOPT, - [__NR_sendmsg] = PPM_SC_SENDMSG, + [__NR_socket - SYSCALL_TABLE_ID0] = PPM_SC_SOCKET, + [__NR_bind - SYSCALL_TABLE_ID0] = PPM_SC_BIND, + [__NR_connect - SYSCALL_TABLE_ID0] = PPM_SC_CONNECT, + [__NR_listen - SYSCALL_TABLE_ID0] = PPM_SC_LISTEN, + [__NR_accept - SYSCALL_TABLE_ID0] = PPM_SC_ACCEPT, + [__NR_getsockname - SYSCALL_TABLE_ID0] = PPM_SC_GETSOCKNAME, + [__NR_getpeername - SYSCALL_TABLE_ID0] = PPM_SC_GETPEERNAME, + [__NR_socketpair - SYSCALL_TABLE_ID0] = PPM_SC_SOCKETPAIR, +/* [__NR_send - SYSCALL_TABLE_ID0] = PPM_SC_NR_SEND, */ + [__NR_sendto - SYSCALL_TABLE_ID0] = PPM_SC_SENDTO, +/* [__NR_recv - SYSCALL_TABLE_ID0] = PPM_SC_NR_RECV, */ + [__NR_recvfrom - SYSCALL_TABLE_ID0] = PPM_SC_RECVFROM, + [__NR_shutdown - SYSCALL_TABLE_ID0] = PPM_SC_SHUTDOWN, + [__NR_setsockopt - SYSCALL_TABLE_ID0] = PPM_SC_SETSOCKOPT, + [__NR_getsockopt - SYSCALL_TABLE_ID0] = PPM_SC_GETSOCKOPT, + [__NR_sendmsg - SYSCALL_TABLE_ID0] = PPM_SC_SENDMSG, + [__NR_recvmsg - SYSCALL_TABLE_ID0] = PPM_SC_RECVMSG, + [__NR_accept4 - SYSCALL_TABLE_ID0] = PPM_SC_ACCEPT4, +#else + [__NR_socketcall - SYSCALL_TABLE_ID0] = PPM_SC_SOCKETCALL, +#endif + + #ifdef __NR_sendmmsg - [__NR_sendmmsg] = PPM_SC_SENDMMSG, + [__NR_sendmmsg - SYSCALL_TABLE_ID0] = PPM_SC_SENDMMSG, #endif - [__NR_recvmsg] = PPM_SC_RECVMSG, #ifdef __NR_recvmmsg - [__NR_recvmmsg] = PPM_SC_RECVMMSG, + [__NR_recvmmsg - SYSCALL_TABLE_ID0] = PPM_SC_RECVMMSG, #endif - [__NR_accept4] = PPM_SC_ACCEPT4, /* * Non-multiplexed IPC family */ - [__NR_semop] = PPM_SC_SEMOP, - [__NR_semget] = PPM_SC_SEMGET, - [__NR_semctl] = PPM_SC_SEMCTL, - [__NR_msgsnd] = PPM_SC_MSGSND, - [__NR_msgrcv] = PPM_SC_MSGRCV, - [__NR_msgget] = PPM_SC_MSGGET, - [__NR_msgctl] = PPM_SC_MSGCTL, -/* [__NR_shmatcall] = PPM_SC_NR_SHMATCALL, */ - [__NR_shmdt] = PPM_SC_SHMDT, - [__NR_shmget] = PPM_SC_SHMGET, - [__NR_shmctl] = PPM_SC_SHMCTL, -/* [__NR_fcntl64] = PPM_SC_NR_FCNTL64, */ +#ifdef __NR_semop + [__NR_semop - SYSCALL_TABLE_ID0] = PPM_SC_SEMOP, +#endif +#ifdef __NR_semget + [__NR_semget - SYSCALL_TABLE_ID0] = PPM_SC_SEMGET, +#endif +#ifdef __NR_semctl + [__NR_semctl - SYSCALL_TABLE_ID0] = PPM_SC_SEMCTL, +#endif +#ifdef __NR_msgsnd + [__NR_msgsnd - SYSCALL_TABLE_ID0] = PPM_SC_MSGSND, +#endif +#ifdef __NR_msgrcv + [__NR_msgrcv - SYSCALL_TABLE_ID0] = PPM_SC_MSGRCV, +#endif +#ifdef __NR_msgget + [__NR_msgget - SYSCALL_TABLE_ID0] = PPM_SC_MSGGET, +#endif +#ifdef __NR_msgctl + [__NR_msgctl - SYSCALL_TABLE_ID0] = PPM_SC_MSGCTL, +#endif +/* [__NR_shmatcall - SYSCALL_TABLE_ID0] = PPM_SC_NR_SHMATCALL, */ +#ifdef __NR_shmdt + [__NR_shmdt - SYSCALL_TABLE_ID0] = PPM_SC_SHMDT, +#endif +#ifdef __NR_shmget + [__NR_shmget - SYSCALL_TABLE_ID0] = PPM_SC_SHMGET, +#endif +#ifdef __NR_shmctl + [__NR_shmctl - SYSCALL_TABLE_ID0] = PPM_SC_SHMCTL, +#endif +/* [__NR_fcntl64 - SYSCALL_TABLE_ID0] = PPM_SC_NR_FCNTL64, */ +#ifdef __NR_statfs64 + [__NR_statfs64 - SYSCALL_TABLE_ID0] = PPM_SC_STATFS64, +#endif +#ifdef __NR_fstatfs64 + [__NR_fstatfs64 - SYSCALL_TABLE_ID0] = PPM_SC_FSTATFS64, +#endif +#ifdef __NR_fstatat64 + [__NR_fstatat64 - SYSCALL_TABLE_ID0] = PPM_SC_FSTATAT64, +#endif +#ifdef __NR_sendfile64 + [__NR_sendfile64 - SYSCALL_TABLE_ID0] = PPM_SC_SENDFILE64, +#endif +#ifdef __NR_ugetrlimit + [__NR_ugetrlimit - SYSCALL_TABLE_ID0] = PPM_SC_UGETRLIMIT, +#endif +#ifdef __NR_bdflush + [__NR_bdflush - SYSCALL_TABLE_ID0] = PPM_SC_BDFLUSH, +#endif +#ifdef __NR_sigprocmask + [__NR_sigprocmask - SYSCALL_TABLE_ID0] = PPM_SC_SIGPROCMASK, +#endif +#ifdef __NR_ipc + [__NR_ipc - SYSCALL_TABLE_ID0] = PPM_SC_IPC, +#endif +#ifdef __NR_stat64 + [__NR_stat64 - SYSCALL_TABLE_ID0] = PPM_SC_STAT64, +#endif +#ifdef __NR_lstat64 + [__NR_lstat64 - SYSCALL_TABLE_ID0] = PPM_SC_LSTAT64, +#endif +#ifdef __NR_fstat64 + [__NR_fstat64 - SYSCALL_TABLE_ID0] = PPM_SC_FSTAT64, +#endif +#ifdef __NR_fcntl64 + [__NR_fcntl64 - SYSCALL_TABLE_ID0] = PPM_SC_FCNTL64, +#endif +#ifdef __NR_mmap2 + [__NR_mmap2 - SYSCALL_TABLE_ID0] = PPM_SC_MMAP2, +#endif +#ifdef __NR__newselect + [__NR__newselect - SYSCALL_TABLE_ID0] = PPM_SC__NEWSELECT, +#endif +#ifdef __NR_sgetmask + [__NR_sgetmask - SYSCALL_TABLE_ID0] = PPM_SC_SGETMASK, +#endif +#ifdef __NR_ssetmask + [__NR_ssetmask - SYSCALL_TABLE_ID0] = PPM_SC_SSETMASK, +#endif + +/* [__NR_setreuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETREUID16, */ +/* [__NR_setregid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETREGID16, */ +#ifdef __NR_sigpending + [__NR_sigpending - SYSCALL_TABLE_ID0] = PPM_SC_SIGPENDING, +#endif +#ifdef __NR_olduname + [__NR_olduname - SYSCALL_TABLE_ID0] = PPM_SC_OLDUNAME, +#endif +#ifdef __NR_umount + [__NR_umount - SYSCALL_TABLE_ID0] = PPM_SC_UMOUNT, +#endif +#ifdef __NR_signal + [__NR_signal - SYSCALL_TABLE_ID0] = PPM_SC_SIGNAL, +#endif +#ifdef __NR_nice + [__NR_nice - SYSCALL_TABLE_ID0] = PPM_SC_NICE, +#endif +#ifdef __NR_stime + [__NR_stime - SYSCALL_TABLE_ID0] = PPM_SC_STIME, +#endif +#ifdef __NR__llseek + [__NR__llseek - SYSCALL_TABLE_ID0] = PPM_SC__LLSEEK, +#endif +#ifdef __NR_waitpid + [__NR_waitpid - SYSCALL_TABLE_ID0] = PPM_SC_WAITPID, +#endif +#ifdef __NR_pread64 + [__NR_pread64 - SYSCALL_TABLE_ID0] = PPM_SC_PREAD64, +#endif +#ifdef __NR_pwrite64 + [__NR_pwrite64 - SYSCALL_TABLE_ID0] = PPM_SC_PWRITE64, +#endif +#ifdef __NR_shmat + [__NR_shmat - SYSCALL_TABLE_ID0] = PPM_SC_SHMAT, +#endif +#ifdef __NR_rt_sigreturn + [__NR_rt_sigreturn - SYSCALL_TABLE_ID0] = PPM_SC_SIGRETURN, +#endif +#ifdef __NR_fallocate + [__NR_fallocate - SYSCALL_TABLE_ID0] = PPM_SC_FALLOCATE, +#endif +#ifdef __NR_newfstatat + [__NR_newfstatat - SYSCALL_TABLE_ID0] = PPM_SC_NEWFSSTAT, +#endif +#ifdef __NR_process_vm_readv + [__NR_process_vm_readv - SYSCALL_TABLE_ID0] = PPM_SC_PROCESS_VM_READV, +#endif +#ifdef __NR_process_vm_writev + [__NR_process_vm_writev - SYSCALL_TABLE_ID0] = PPM_SC_PROCESS_VM_WRITEV, +#endif +#ifdef __NR_fork + [__NR_fork - SYSCALL_TABLE_ID0] = PPM_SC_FORK, +#endif +#ifdef __NR_vfork + [__NR_vfork - SYSCALL_TABLE_ID0] = PPM_SC_VFORK, +#endif +#ifdef __NR_quotactl + [__NR_quotactl - SYSCALL_TABLE_ID0] = PPM_SC_QUOTACTL, +#endif +#ifdef __NR_setresuid + [__NR_setresuid - SYSCALL_TABLE_ID0] = PPM_SC_SETRESUID, +#endif +#ifdef __NR_setresuid32 + [__NR_setresuid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETRESUID, +#endif +#ifdef __NR_setresgid + [__NR_setresgid - SYSCALL_TABLE_ID0] = PPM_SC_SETRESGID, +#endif +#ifdef __NR_setresgid32 + [__NR_setresgid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETRESGID, +#endif +#ifdef __NR_setuid + [__NR_setuid - SYSCALL_TABLE_ID0] = PPM_SC_SETUID, +#endif +#ifdef __NR_setuid32 + [__NR_setuid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETUID32, +#endif +#ifdef __NR_setgid + [__NR_setgid - SYSCALL_TABLE_ID0] = PPM_SC_SETGID, +#endif +#ifdef __NR_setgid32 + [__NR_setgid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETGID32, +#endif +#ifdef __NR_getuid + [__NR_getuid - SYSCALL_TABLE_ID0] = PPM_SC_GETUID, +#endif +#ifdef __NR_getuid32 + [__NR_getuid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETUID32, +#endif +#ifdef __NR_geteuid + [__NR_geteuid - SYSCALL_TABLE_ID0] = PPM_SC_GETEUID, +#endif +#ifdef __NR_geteuid32 + [__NR_geteuid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETEUID, +#endif +#ifdef __NR_getgid + [__NR_getgid - SYSCALL_TABLE_ID0] = PPM_SC_GETGID, +#endif +#ifdef __NR_getgid32 + [__NR_getgid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETGID, +#endif +#ifdef __NR_getegid + [__NR_getegid - SYSCALL_TABLE_ID0] = PPM_SC_GETEGID, +#endif +#ifdef __NR_getegid32 + [__NR_getegid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETEGID, +#endif +#ifdef __NR_getresuid + [__NR_getresuid - SYSCALL_TABLE_ID0] = PPM_SC_GETRESUID, +#endif +#ifdef __NR_getresuid32 + [__NR_getresuid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETRESUID32, +#endif +#ifdef __NR_getresgid + [__NR_getresgid - SYSCALL_TABLE_ID0] = PPM_SC_GETRESGID, +#endif +#ifdef __NR_getresgid32 + [__NR_getresgid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETRESGID32, +#endif +#ifdef __NR_setns + [__NR_setns - SYSCALL_TABLE_ID0] = PPM_SC_SETNS, +#endif +#ifdef __NR_access + [__NR_access - SYSCALL_TABLE_ID0] = PPM_SC_ACCESS, +#endif +#ifdef __NR_finit_module + [__NR_finit_module - SYSCALL_TABLE_ID0] = PPM_SC_FINIT_MODULE, +#endif +#ifdef __NR_bpf + [__NR_bpf - SYSCALL_TABLE_ID0] = PPM_SC_BPF, +#endif +#ifdef __NR_seccomp + [__NR_seccomp - SYSCALL_TABLE_ID0] = PPM_SC_SECCOMP, +#endif +#ifdef __NR_sigaltstack + [__NR_sigaltstack - SYSCALL_TABLE_ID0] = PPM_SC_SIGALTSTACK, +#endif +#ifdef __NR_getrandom + [__NR_getrandom - SYSCALL_TABLE_ID0] = PPM_SC_GETRANDOM, +#endif +#ifdef __NR_fadvise64 + [__NR_fadvise64 - SYSCALL_TABLE_ID0] = PPM_SC_FADVISE64, +#endif +#ifdef __NR_renameat2 + [__NR_renameat2 - SYSCALL_TABLE_ID0] = PPM_SC_RENAMEAT2, +#endif +}; + +#ifdef CONFIG_IA32_EMULATION +const struct syscall_evt_pair g_syscall_ia32_table[SYSCALL_TABLE_SIZE] = { + [__NR_ia32_open - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_OPEN_E, PPME_SYSCALL_OPEN_X}, + [__NR_ia32_creat - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_CREAT_E, PPME_SYSCALL_CREAT_X}, + [__NR_ia32_close - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_CLOSE_E, PPME_SYSCALL_CLOSE_X}, + [__NR_ia32_brk - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_BRK_4_E, PPME_SYSCALL_BRK_4_X}, + [__NR_ia32_read - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_READ_E, PPME_SYSCALL_READ_X}, + [__NR_ia32_write - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_WRITE_E, PPME_SYSCALL_WRITE_X}, + [__NR_ia32_execve - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_EXECVE_19_E, PPME_SYSCALL_EXECVE_19_X}, + [__NR_ia32_clone - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_CLONE_20_E, PPME_SYSCALL_CLONE_20_X}, + [__NR_ia32_fork - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_FORK_20_E, PPME_SYSCALL_FORK_20_X}, + [__NR_ia32_vfork - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_VFORK_20_E, PPME_SYSCALL_VFORK_20_X}, + [__NR_ia32_pipe - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PIPE_E, PPME_SYSCALL_PIPE_X}, + [__NR_ia32_pipe2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PIPE_E, PPME_SYSCALL_PIPE_X}, + [__NR_ia32_eventfd - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_EVENTFD_E, PPME_SYSCALL_EVENTFD_X}, + [__NR_ia32_eventfd2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_EVENTFD_E, PPME_SYSCALL_EVENTFD_X}, + [__NR_ia32_futex - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_FUTEX_E, PPME_SYSCALL_FUTEX_X}, + [__NR_ia32_stat - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_STAT_E, PPME_SYSCALL_STAT_X}, + [__NR_ia32_lstat - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_LSTAT_E, PPME_SYSCALL_LSTAT_X}, + [__NR_ia32_fstat - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_FSTAT_E, PPME_SYSCALL_FSTAT_X}, + [__NR_ia32_epoll_wait - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_EPOLLWAIT_E, PPME_SYSCALL_EPOLLWAIT_X}, + [__NR_ia32_poll - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_POLL_E, PPME_SYSCALL_POLL_X}, +#ifdef __NR_ia32_select + [__NR_ia32_select - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SELECT_E, PPME_SYSCALL_SELECT_X}, +#endif + [__NR_ia32_lseek - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_LSEEK_E, PPME_SYSCALL_LSEEK_X}, + [__NR_ia32_ioctl - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_IOCTL_3_E, PPME_SYSCALL_IOCTL_3_X}, + [__NR_ia32_getcwd - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_GETCWD_E, PPME_SYSCALL_GETCWD_X}, + [__NR_ia32_chdir - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_CHDIR_E, PPME_SYSCALL_CHDIR_X}, + [__NR_ia32_fchdir - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_FCHDIR_E, PPME_SYSCALL_FCHDIR_X}, + [__NR_ia32_mkdir - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_MKDIR_2_E, PPME_SYSCALL_MKDIR_2_X}, + [__NR_ia32_rmdir - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RMDIR_2_E, PPME_SYSCALL_RMDIR_2_X}, + [__NR_ia32_openat - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_OPENAT_2_E, PPME_SYSCALL_OPENAT_2_X}, + [__NR_ia32_mkdirat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_MKDIRAT_E, PPME_SYSCALL_MKDIRAT_X}, + [__NR_ia32_link - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_LINK_2_E, PPME_SYSCALL_LINK_2_X}, + [__NR_ia32_linkat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_LINKAT_2_E, PPME_SYSCALL_LINKAT_2_X}, + [__NR_ia32_unlink - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_UNLINK_2_E, PPME_SYSCALL_UNLINK_2_X}, + [__NR_ia32_unlinkat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_UNLINKAT_2_E, PPME_SYSCALL_UNLINKAT_2_X}, + [__NR_ia32_pread64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PREAD_E, PPME_SYSCALL_PREAD_X}, + [__NR_ia32_pwrite64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PWRITE_E, PPME_SYSCALL_PWRITE_X}, + [__NR_ia32_readv - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_READV_E, PPME_SYSCALL_READV_X}, + [__NR_ia32_writev - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_WRITEV_E, PPME_SYSCALL_WRITEV_X}, + [__NR_ia32_preadv - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PREADV_E, PPME_SYSCALL_PREADV_X}, + [__NR_ia32_pwritev - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PWRITEV_E, PPME_SYSCALL_PWRITEV_X}, + [__NR_ia32_dup - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, + [__NR_ia32_dup2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, + [__NR_ia32_dup3 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP | UF_SIMPLEDRIVER_KEEP, PPME_SYSCALL_DUP_E, PPME_SYSCALL_DUP_X}, + [__NR_ia32_signalfd - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SIGNALFD_E, PPME_SYSCALL_SIGNALFD_X}, + [__NR_ia32_signalfd4 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SIGNALFD_E, PPME_SYSCALL_SIGNALFD_X}, + [__NR_ia32_kill - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_KILL_E, PPME_SYSCALL_KILL_X}, + [__NR_ia32_tkill - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_TKILL_E, PPME_SYSCALL_TKILL_X}, + [__NR_ia32_tgkill - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_TGKILL_E, PPME_SYSCALL_TGKILL_X}, + [__NR_ia32_nanosleep - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_NANOSLEEP_E, PPME_SYSCALL_NANOSLEEP_X}, + [__NR_ia32_timerfd_create - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_TIMERFD_CREATE_E, PPME_SYSCALL_TIMERFD_CREATE_X}, + [__NR_ia32_inotify_init - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_INOTIFY_INIT_E, PPME_SYSCALL_INOTIFY_INIT_X}, + [__NR_ia32_inotify_init1 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_INOTIFY_INIT_E, PPME_SYSCALL_INOTIFY_INIT_X}, + [__NR_ia32_getrlimit - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_GETRLIMIT_E, PPME_SYSCALL_GETRLIMIT_X}, + [__NR_ia32_setrlimit - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_SETRLIMIT_E, PPME_SYSCALL_SETRLIMIT_X}, + [__NR_ia32_fchmodat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCHMODAT_E, PPME_SYSCALL_FCHMODAT_X}, + [__NR_ia32_fchmod - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCHMOD_E, PPME_SYSCALL_FCHMOD_X}, +#ifdef __NR_ia32_prlimit64 + [__NR_ia32_prlimit64 - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_PRLIMIT_E, PPME_SYSCALL_PRLIMIT_X}, +#endif +#ifdef __NR_ia32_ugetrlimit + [__NR_ia32_ugetrlimit - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_GETRLIMIT_E, PPME_SYSCALL_GETRLIMIT_X}, +#endif + [__NR_ia32_fcntl - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCNTL_E, PPME_SYSCALL_FCNTL_X}, +#ifdef __NR_ia32_fcntl64 + [__NR_ia32_fcntl64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_FCNTL_E, PPME_SYSCALL_FCNTL_X}, +#endif + [__NR_ia32_ppoll - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_PPOLL_E, PPME_SYSCALL_PPOLL_X}, +/* [__NR_ia32_old_select - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, */ + [__NR_ia32_pselect6 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_epoll_create - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_epoll_ctl - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_uselib - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_sched_setparam - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_sched_getparam - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_syslog - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_chmod - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_CHMOD_E, PPME_SYSCALL_CHMOD_X}, + [__NR_ia32_lchown - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_utime - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_mount - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_umount2 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_ptrace - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_PTRACE_E, PPME_SYSCALL_PTRACE_X}, + [__NR_ia32_alarm - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + [__NR_ia32_pause - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_GENERIC_E, PPME_GENERIC_X}, + +#ifndef __NR_ia32_socketcall + [__NR_ia32_socket - SYSCALL_TABLE_ID0] = {UF_USED | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_SOCKET_E, PPME_SOCKET_SOCKET_X}, + [__NR_ia32_bind - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_BIND_E, PPME_SOCKET_BIND_X}, + [__NR_ia32_connect - SYSCALL_TABLE_ID0] = {UF_USED | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_CONNECT_E, PPME_SOCKET_CONNECT_X}, + [__NR_ia32_listen - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_LISTEN_E, PPME_SOCKET_LISTEN_X}, + [__NR_ia32_accept - SYSCALL_TABLE_ID0] = {UF_USED | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_ACCEPT_E, PPME_SOCKET_ACCEPT_X}, + [__NR_ia32_getsockname - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SOCKET_GETSOCKNAME_E, PPME_SOCKET_GETSOCKNAME_X}, + [__NR_ia32_getpeername - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SOCKET_GETPEERNAME_E, PPME_SOCKET_GETPEERNAME_X}, + [__NR_ia32_socketpair - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SOCKET_SOCKETPAIR_E, PPME_SOCKET_SOCKETPAIR_X}, + [__NR_ia32_sendto - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SENDTO_E, PPME_SOCKET_SENDTO_X}, + [__NR_ia32_recvfrom - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_RECVFROM_E, PPME_SOCKET_RECVFROM_X}, + [__NR_ia32_shutdown - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SHUTDOWN_E, PPME_SOCKET_SHUTDOWN_X}, + [__NR_ia32_setsockopt - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SOCKET_SETSOCKOPT_E, PPME_SOCKET_SETSOCKOPT_X}, + [__NR_ia32_getsockopt - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_GETSOCKOPT_E, PPME_SOCKET_GETSOCKOPT_X}, + [__NR_ia32_sendmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SENDMSG_E, PPME_SOCKET_SENDMSG_X}, + [__NR_ia32_accept4 - SYSCALL_TABLE_ID0] = {UF_USED | UF_SIMPLEDRIVER_KEEP, PPME_SOCKET_ACCEPT4_E, PPME_SOCKET_ACCEPT4_X}, +#endif + +#ifdef __NR_ia32_sendmmsg + [__NR_ia32_sendmmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_SENDMMSG_E, PPME_SOCKET_SENDMMSG_X}, +#endif +#ifdef __NR_ia32_recvmsg + [__NR_ia32_recvmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_RECVMSG_E, PPME_SOCKET_RECVMSG_X}, +#endif +#ifdef __NR_ia32_recvmmsg + [__NR_ia32_recvmmsg - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SOCKET_RECVMMSG_E, PPME_SOCKET_RECVMMSG_X}, +#endif +#ifdef __NR_ia32_stat64 + [__NR_ia32_stat64 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_STAT64_E, PPME_SYSCALL_STAT64_X}, +#endif +#ifdef __NR_ia32_fstat64 + [__NR_ia32_fstat64 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_FSTAT64_E, PPME_SYSCALL_FSTAT64_X}, +#endif +#ifdef __NR_ia32__llseek + [__NR_ia32__llseek - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_LLSEEK_E, PPME_SYSCALL_LLSEEK_X}, +#endif + [__NR_ia32_mmap - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_MMAP_E, PPME_SYSCALL_MMAP_X}, +#ifdef __NR_ia32_mmap2 + [__NR_ia32_mmap2 - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_MMAP2_E, PPME_SYSCALL_MMAP2_X}, +#endif + [__NR_ia32_munmap - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_MUNMAP_E, PPME_SYSCALL_MUNMAP_X}, + [__NR_ia32_splice - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SPLICE_E, PPME_SYSCALL_SPLICE_X}, +#ifdef __NR_ia32_process_vm_readv + [__NR_ia32_process_vm_readv - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, +#endif +#ifdef __NR_ia32_process_vm_writev + [__NR_ia32_process_vm_writev - SYSCALL_TABLE_ID0] = {UF_USED, PPME_GENERIC_E, PPME_GENERIC_X}, +#endif + + [__NR_ia32_rename - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RENAME_E, PPME_SYSCALL_RENAME_X}, + [__NR_ia32_renameat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RENAMEAT_E, PPME_SYSCALL_RENAMEAT_X}, + [__NR_ia32_symlink - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SYMLINK_E, PPME_SYSCALL_SYMLINK_X}, + [__NR_ia32_symlinkat - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SYMLINKAT_E, PPME_SYSCALL_SYMLINKAT_X}, + [__NR_ia32_sendfile - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SENDFILE_E, PPME_SYSCALL_SENDFILE_X}, +#ifdef __NR_ia32_sendfile64 + [__NR_ia32_sendfile64 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SENDFILE_E, PPME_SYSCALL_SENDFILE_X}, +#endif +#ifdef __NR_ia32_quotactl + [__NR_ia32_quotactl - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_QUOTACTL_E, PPME_SYSCALL_QUOTACTL_X}, +#endif +#ifdef __NR_ia32_setresuid + [__NR_ia32_setresuid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESUID_E, PPME_SYSCALL_SETRESUID_X }, +#endif +#ifdef __NR_ia32_setresuid32 + [__NR_ia32_setresuid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESUID_E, PPME_SYSCALL_SETRESUID_X }, +#endif +#ifdef __NR_ia32_setresgid + [__NR_ia32_setresgid - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESGID_E, PPME_SYSCALL_SETRESGID_X }, +#endif +#ifdef __NR_ia32_setresgid32 + [__NR_ia32_setresgid32 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SETRESGID_E, PPME_SYSCALL_SETRESGID_X }, +#endif +#ifdef __NR_ia32_setuid + [__NR_ia32_setuid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_SETUID_E, PPME_SYSCALL_SETUID_X }, +#endif +#ifdef __NR_ia32_setuid32 + [__NR_ia32_setuid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_SETUID_E, PPME_SYSCALL_SETUID_X }, +#endif +#ifdef __NR_ia32_setgid + [__NR_ia32_setgid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_SETGID_E, PPME_SYSCALL_SETGID_X }, +#endif +#ifdef __NR_ia32_setgid32 + [__NR_ia32_setgid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_SETGID_E, PPME_SYSCALL_SETGID_X }, +#endif +#ifdef __NR_ia32_getuid + [__NR_ia32_getuid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETUID_E, PPME_SYSCALL_GETUID_X }, +#endif +#ifdef __NR_ia32_getuid32 + [__NR_ia32_getuid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETUID_E, PPME_SYSCALL_GETUID_X }, +#endif +#ifdef __NR_ia32_geteuid + [__NR_ia32_geteuid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETEUID_E, PPME_SYSCALL_GETEUID_X }, +#endif +#ifdef __NR_ia32_geteuid32 + [__NR_ia32_geteuid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETEUID_E, PPME_SYSCALL_GETEUID_X }, +#endif +#ifdef __NR_ia32_getgid + [__NR_ia32_getgid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETGID_E, PPME_SYSCALL_GETGID_X }, +#endif +#ifdef __NR_ia32_getgid32 + [__NR_ia32_getgid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETGID_E, PPME_SYSCALL_GETGID_X }, +#endif +#ifdef __NR_ia32_getegid + [__NR_ia32_getegid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETEGID_E, PPME_SYSCALL_GETEGID_X }, +#endif +#ifdef __NR_ia32_getegid32 + [__NR_ia32_getegid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETEGID_E, PPME_SYSCALL_GETEGID_X }, +#endif +#ifdef __NR_ia32_getresuid + [__NR_ia32_getresuid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETRESUID_E, PPME_SYSCALL_GETRESUID_X }, +#endif +#ifdef __NR_ia32_getresuid32 + [__NR_ia32_getresuid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETRESUID_E, PPME_SYSCALL_GETRESUID_X }, +#endif +#ifdef __NR_ia32_getresgid + [__NR_ia32_getresgid - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETRESGID_E, PPME_SYSCALL_GETRESGID_X }, +#endif +#ifdef __NR_ia32_getresgid32 + [__NR_ia32_getresgid32 - SYSCALL_TABLE_ID0] = { UF_USED, PPME_SYSCALL_GETRESGID_E, PPME_SYSCALL_GETRESGID_X }, +#endif +#ifdef __NR_ia32_semop + [__NR_ia32_semop - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SEMOP_E, PPME_SYSCALL_SEMOP_X}, +#endif +#ifdef __NR_ia32_semget + [__NR_ia32_semget - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SEMGET_E, PPME_SYSCALL_SEMGET_X}, +#endif +#ifdef __NR_ia32_semctl + [__NR_ia32_semctl - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SEMCTL_E, PPME_SYSCALL_SEMCTL_X}, +#endif +#ifdef __NR_ia32_access + [__NR_ia32_access - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_ACCESS_E, PPME_SYSCALL_ACCESS_X}, +#endif +#ifdef __NR_ia32_chroot + [__NR_ia32_chroot - SYSCALL_TABLE_ID0] = {UF_USED | UF_NEVER_DROP, PPME_SYSCALL_CHROOT_E, PPME_SYSCALL_CHROOT_X}, +#endif + [__NR_ia32_setsid - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SETSID_E, PPME_SYSCALL_SETSID_X}, + [__NR_ia32_setpgid - SYSCALL_TABLE_ID0] = {UF_USED | UF_ALWAYS_DROP, PPME_SYSCALL_SETPGID_E, PPME_SYSCALL_SETPGID_X}, +#ifdef __NR_ia32_bpf + [__NR_ia32_bpf - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_BPF_E, PPME_SYSCALL_BPF_X}, +#endif +#ifdef __NR_ia32_seccomp + [__NR_ia32_seccomp - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_SECCOMP_E, PPME_SYSCALL_SECCOMP_X}, +#endif +#ifdef __NR_ia32_renameat2 + [__NR_ia32_renameat2 - SYSCALL_TABLE_ID0] = {UF_USED, PPME_SYSCALL_RENAMEAT2_E, PPME_SYSCALL_RENAMEAT2_X}, +#endif +}; + +/* + * SYSCALL ROUTING TABLE + */ +const enum ppm_syscall_code g_syscall_ia32_code_routing_table[SYSCALL_TABLE_SIZE] = { + [__NR_ia32_restart_syscall - SYSCALL_TABLE_ID0] = PPM_SC_RESTART_SYSCALL, + [__NR_ia32_exit - SYSCALL_TABLE_ID0] = PPM_SC_EXIT, + [__NR_ia32_read - SYSCALL_TABLE_ID0] = PPM_SC_READ, + [__NR_ia32_write - SYSCALL_TABLE_ID0] = PPM_SC_WRITE, + [__NR_ia32_open - SYSCALL_TABLE_ID0] = PPM_SC_OPEN, + [__NR_ia32_close - SYSCALL_TABLE_ID0] = PPM_SC_CLOSE, + [__NR_ia32_creat - SYSCALL_TABLE_ID0] = PPM_SC_CREAT, + [__NR_ia32_link - SYSCALL_TABLE_ID0] = PPM_SC_LINK, + [__NR_ia32_unlink - SYSCALL_TABLE_ID0] = PPM_SC_UNLINK, + [__NR_ia32_chdir - SYSCALL_TABLE_ID0] = PPM_SC_CHDIR, + [__NR_ia32_time - SYSCALL_TABLE_ID0] = PPM_SC_TIME, + [__NR_ia32_mknod - SYSCALL_TABLE_ID0] = PPM_SC_MKNOD, + [__NR_ia32_chmod - SYSCALL_TABLE_ID0] = PPM_SC_CHMOD, +/* [__NR_ia32_lchown16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_LCHOWN16, */ + [__NR_ia32_stat - SYSCALL_TABLE_ID0] = PPM_SC_STAT, + [__NR_ia32_lseek - SYSCALL_TABLE_ID0] = PPM_SC_LSEEK, + [__NR_ia32_getpid - SYSCALL_TABLE_ID0] = PPM_SC_GETPID, + [__NR_ia32_mount - SYSCALL_TABLE_ID0] = PPM_SC_MOUNT, +/* [__NR_ia32_oldumount - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLDUMOUNT, */ +/* [__NR_ia32_setuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETUID16, */ +/* [__NR_ia32_getuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETUID16, */ + [__NR_ia32_ptrace - SYSCALL_TABLE_ID0] = PPM_SC_PTRACE, + [__NR_ia32_alarm - SYSCALL_TABLE_ID0] = PPM_SC_ALARM, + [__NR_ia32_fstat - SYSCALL_TABLE_ID0] = PPM_SC_FSTAT, + [__NR_ia32_pause - SYSCALL_TABLE_ID0] = PPM_SC_PAUSE, + [__NR_ia32_utime - SYSCALL_TABLE_ID0] = PPM_SC_UTIME, + [__NR_ia32_access - SYSCALL_TABLE_ID0] = PPM_SC_ACCESS, + [__NR_ia32_sync - SYSCALL_TABLE_ID0] = PPM_SC_SYNC, + [__NR_ia32_kill - SYSCALL_TABLE_ID0] = PPM_SC_KILL, + [__NR_ia32_rename - SYSCALL_TABLE_ID0] = PPM_SC_RENAME, + [__NR_ia32_mkdir - SYSCALL_TABLE_ID0] = PPM_SC_MKDIR, + [__NR_ia32_rmdir - SYSCALL_TABLE_ID0] = PPM_SC_RMDIR, + [__NR_ia32_dup - SYSCALL_TABLE_ID0] = PPM_SC_DUP, + [__NR_ia32_pipe - SYSCALL_TABLE_ID0] = PPM_SC_PIPE, + [__NR_ia32_times - SYSCALL_TABLE_ID0] = PPM_SC_TIMES, + [__NR_ia32_brk - SYSCALL_TABLE_ID0] = PPM_SC_BRK, +/* [__NR_ia32_setgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETGID16, */ +/* [__NR_ia32_getgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETGID16, */ +/* [__NR_ia32_geteuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETEUID16, */ +/* [__NR_ia32_getegid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETEGID16, */ + [__NR_ia32_acct - SYSCALL_TABLE_ID0] = PPM_SC_ACCT, + [__NR_ia32_ioctl - SYSCALL_TABLE_ID0] = PPM_SC_IOCTL, + [__NR_ia32_fcntl - SYSCALL_TABLE_ID0] = PPM_SC_FCNTL, + [__NR_ia32_setpgid - SYSCALL_TABLE_ID0] = PPM_SC_SETPGID, + [__NR_ia32_umask - SYSCALL_TABLE_ID0] = PPM_SC_UMASK, + [__NR_ia32_chroot - SYSCALL_TABLE_ID0] = PPM_SC_CHROOT, + [__NR_ia32_ustat - SYSCALL_TABLE_ID0] = PPM_SC_USTAT, + [__NR_ia32_dup2 - SYSCALL_TABLE_ID0] = PPM_SC_DUP2, + [__NR_ia32_getppid - SYSCALL_TABLE_ID0] = PPM_SC_GETPPID, + [__NR_ia32_getpgrp - SYSCALL_TABLE_ID0] = PPM_SC_GETPGRP, + [__NR_ia32_setsid - SYSCALL_TABLE_ID0] = PPM_SC_SETSID, + [__NR_ia32_sethostname - SYSCALL_TABLE_ID0] = PPM_SC_SETHOSTNAME, + [__NR_ia32_setrlimit - SYSCALL_TABLE_ID0] = PPM_SC_SETRLIMIT, +/* [__NR_ia32_old_getrlimit - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_GETRLIMIT, */ + [__NR_ia32_getrusage - SYSCALL_TABLE_ID0] = PPM_SC_GETRUSAGE, + [__NR_ia32_gettimeofday - SYSCALL_TABLE_ID0] = PPM_SC_GETTIMEOFDAY, + [__NR_ia32_settimeofday - SYSCALL_TABLE_ID0] = PPM_SC_SETTIMEOFDAY, +/* [__NR_ia32_getgroups16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETGROUPS16, */ +/* [__NR_ia32_setgroups16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETGROUPS16, */ +/* [__NR_ia32_old_select - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_SELECT, */ + [__NR_ia32_symlink - SYSCALL_TABLE_ID0] = PPM_SC_SYMLINK, + [__NR_ia32_lstat - SYSCALL_TABLE_ID0] = PPM_SC_LSTAT, + [__NR_ia32_readlink - SYSCALL_TABLE_ID0] = PPM_SC_READLINK, + [__NR_ia32_uselib - SYSCALL_TABLE_ID0] = PPM_SC_USELIB, + [__NR_ia32_swapon - SYSCALL_TABLE_ID0] = PPM_SC_SWAPON, + [__NR_ia32_reboot - SYSCALL_TABLE_ID0] = PPM_SC_REBOOT, +/* [__NR_ia32_old_readdir - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_READDIR, */ +/* [__NR_ia32_old_mmap - SYSCALL_TABLE_ID0] = PPM_SC_NR_OLD_MMAP, */ + [__NR_ia32_mmap - SYSCALL_TABLE_ID0] = PPM_SC_MMAP, + [__NR_ia32_munmap - SYSCALL_TABLE_ID0] = PPM_SC_MUNMAP, + [__NR_ia32_truncate - SYSCALL_TABLE_ID0] = PPM_SC_TRUNCATE, + [__NR_ia32_ftruncate - SYSCALL_TABLE_ID0] = PPM_SC_FTRUNCATE, + [__NR_ia32_fchmod - SYSCALL_TABLE_ID0] = PPM_SC_FCHMOD, +/* [__NR_ia32_fchown16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_FCHOWN16, */ + [__NR_ia32_getpriority - SYSCALL_TABLE_ID0] = PPM_SC_GETPRIORITY, + [__NR_ia32_setpriority - SYSCALL_TABLE_ID0] = PPM_SC_SETPRIORITY, + [__NR_ia32_statfs - SYSCALL_TABLE_ID0] = PPM_SC_STATFS, + [__NR_ia32_fstatfs - SYSCALL_TABLE_ID0] = PPM_SC_FSTATFS, + [__NR_ia32_syslog - SYSCALL_TABLE_ID0] = PPM_SC_SYSLOG, + [__NR_ia32_setitimer - SYSCALL_TABLE_ID0] = PPM_SC_SETITIMER, + [__NR_ia32_getitimer - SYSCALL_TABLE_ID0] = PPM_SC_GETITIMER, +/* [__NR_ia32_newstat - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWSTAT, */ +/* [__NR_ia32_newlstat - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWLSTAT, */ +/* [__NR_ia32_newfstat - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWFSTAT, */ + [__NR_ia32_uname - SYSCALL_TABLE_ID0] = PPM_SC_UNAME, + [__NR_ia32_vhangup - SYSCALL_TABLE_ID0] = PPM_SC_VHANGUP, + [__NR_ia32_wait4 - SYSCALL_TABLE_ID0] = PPM_SC_WAIT4, + [__NR_ia32_swapoff - SYSCALL_TABLE_ID0] = PPM_SC_SWAPOFF, + [__NR_ia32_sysinfo - SYSCALL_TABLE_ID0] = PPM_SC_SYSINFO, + [__NR_ia32_fsync - SYSCALL_TABLE_ID0] = PPM_SC_FSYNC, + [__NR_ia32_setdomainname - SYSCALL_TABLE_ID0] = PPM_SC_SETDOMAINNAME, +/* [__NR_ia32_newuname - SYSCALL_TABLE_ID0] = PPM_SC_NR_NEWUNAME, */ + [__NR_ia32_adjtimex - SYSCALL_TABLE_ID0] = PPM_SC_ADJTIMEX, + [__NR_ia32_mprotect - SYSCALL_TABLE_ID0] = PPM_SC_MPROTECT, + [__NR_ia32_init_module - SYSCALL_TABLE_ID0] = PPM_SC_INIT_MODULE, + [__NR_ia32_delete_module - SYSCALL_TABLE_ID0] = PPM_SC_DELETE_MODULE, + [__NR_ia32_quotactl - SYSCALL_TABLE_ID0] = PPM_SC_QUOTACTL, + [__NR_ia32_getpgid - SYSCALL_TABLE_ID0] = PPM_SC_GETPGID, + [__NR_ia32_fchdir - SYSCALL_TABLE_ID0] = PPM_SC_FCHDIR, + [__NR_ia32_sysfs - SYSCALL_TABLE_ID0] = PPM_SC_SYSFS, + [__NR_ia32_personality - SYSCALL_TABLE_ID0] = PPM_SC_PERSONALITY, +/* [__NR_ia32_setfsuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETFSUID16, */ +/* [__NR_ia32_setfsgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETFSGID16, */ +/* [__NR_ia32_llseek - SYSCALL_TABLE_ID0] = PPM_SC_NR_LLSEEK, */ + [__NR_ia32_getdents - SYSCALL_TABLE_ID0] = PPM_SC_GETDENTS, +#ifdef __NR_ia32_select + [__NR_ia32_select - SYSCALL_TABLE_ID0] = PPM_SC_SELECT, +#endif + [__NR_ia32_flock - SYSCALL_TABLE_ID0] = PPM_SC_FLOCK, + [__NR_ia32_msync - SYSCALL_TABLE_ID0] = PPM_SC_MSYNC, + [__NR_ia32_readv - SYSCALL_TABLE_ID0] = PPM_SC_READV, + [__NR_ia32_writev - SYSCALL_TABLE_ID0] = PPM_SC_WRITEV, + [__NR_ia32_getsid - SYSCALL_TABLE_ID0] = PPM_SC_GETSID, + [__NR_ia32_fdatasync - SYSCALL_TABLE_ID0] = PPM_SC_FDATASYNC, +/* [__NR_ia32_sysctl - SYSCALL_TABLE_ID0] = PPM_SC_NR_SYSCTL, */ + [__NR_ia32_mlock - SYSCALL_TABLE_ID0] = PPM_SC_MLOCK, + [__NR_ia32_munlock - SYSCALL_TABLE_ID0] = PPM_SC_MUNLOCK, + [__NR_ia32_mlockall - SYSCALL_TABLE_ID0] = PPM_SC_MLOCKALL, + [__NR_ia32_munlockall - SYSCALL_TABLE_ID0] = PPM_SC_MUNLOCKALL, + [__NR_ia32_sched_setparam - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_SETPARAM, + [__NR_ia32_sched_getparam - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GETPARAM, + [__NR_ia32_sched_setscheduler - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_SETSCHEDULER, + [__NR_ia32_sched_getscheduler - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GETSCHEDULER, + [__NR_ia32_sched_yield - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_YIELD, + [__NR_ia32_sched_get_priority_max - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GET_PRIORITY_MAX, + [__NR_ia32_sched_get_priority_min - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GET_PRIORITY_MIN, + [__NR_ia32_sched_rr_get_interval - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_RR_GET_INTERVAL, + [__NR_ia32_nanosleep - SYSCALL_TABLE_ID0] = PPM_SC_NANOSLEEP, + [__NR_ia32_mremap - SYSCALL_TABLE_ID0] = PPM_SC_MREMAP, +/* [__NR_ia32_setresuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETRESUID16, */ +/* [__NR_ia32_getresuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETRESUID16, */ + [__NR_ia32_poll - SYSCALL_TABLE_ID0] = PPM_SC_POLL, +/* [__NR_ia32_setresgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETRESGID16, */ +/* [__NR_ia32_getresgid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_GETRESGID16, */ + [__NR_ia32_prctl - SYSCALL_TABLE_ID0] = PPM_SC_PRCTL, +#ifdef __NR_ia32_arch_prctl + [__NR_ia32_arch_prctl - SYSCALL_TABLE_ID0] = PPM_SC_ARCH_PRCTL, +#endif + [__NR_ia32_rt_sigaction - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGACTION, + [__NR_ia32_rt_sigprocmask - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGPROCMASK, + [__NR_ia32_rt_sigpending - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGPENDING, + [__NR_ia32_rt_sigtimedwait - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGTIMEDWAIT, + [__NR_ia32_rt_sigqueueinfo - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGQUEUEINFO, + [__NR_ia32_rt_sigsuspend - SYSCALL_TABLE_ID0] = PPM_SC_RT_SIGSUSPEND, +/* [__NR_ia32_chown16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_CHOWN16, */ + [__NR_ia32_getcwd - SYSCALL_TABLE_ID0] = PPM_SC_GETCWD, + [__NR_ia32_capget - SYSCALL_TABLE_ID0] = PPM_SC_CAPGET, + [__NR_ia32_capset - SYSCALL_TABLE_ID0] = PPM_SC_CAPSET, + [__NR_ia32_sendfile - SYSCALL_TABLE_ID0] = PPM_SC_SENDFILE, + [__NR_ia32_getrlimit - SYSCALL_TABLE_ID0] = PPM_SC_GETRLIMIT, +/* [__NR_ia32_mmap_pgoff - SYSCALL_TABLE_ID0] = PPM_SC_NR_MMAP_PGOFF, */ + [__NR_ia32_lchown - SYSCALL_TABLE_ID0] = PPM_SC_LCHOWN, + [__NR_ia32_getuid - SYSCALL_TABLE_ID0] = PPM_SC_GETUID, + [__NR_ia32_getgid - SYSCALL_TABLE_ID0] = PPM_SC_GETGID, + [__NR_ia32_geteuid - SYSCALL_TABLE_ID0] = PPM_SC_GETEUID, + [__NR_ia32_getegid - SYSCALL_TABLE_ID0] = PPM_SC_GETEGID, + [__NR_ia32_setreuid - SYSCALL_TABLE_ID0] = PPM_SC_SETREUID, + [__NR_ia32_setregid - SYSCALL_TABLE_ID0] = PPM_SC_SETREGID, + [__NR_ia32_getgroups - SYSCALL_TABLE_ID0] = PPM_SC_GETGROUPS, + [__NR_ia32_setgroups - SYSCALL_TABLE_ID0] = PPM_SC_SETGROUPS, + [__NR_ia32_fchown - SYSCALL_TABLE_ID0] = PPM_SC_FCHOWN, + [__NR_ia32_setresuid - SYSCALL_TABLE_ID0] = PPM_SC_SETRESUID, + [__NR_ia32_getresuid - SYSCALL_TABLE_ID0] = PPM_SC_GETRESUID, + [__NR_ia32_setresgid - SYSCALL_TABLE_ID0] = PPM_SC_SETRESGID, + [__NR_ia32_getresgid - SYSCALL_TABLE_ID0] = PPM_SC_GETRESGID, + [__NR_ia32_chown - SYSCALL_TABLE_ID0] = PPM_SC_CHOWN, + [__NR_ia32_setuid - SYSCALL_TABLE_ID0] = PPM_SC_SETUID, + [__NR_ia32_setgid - SYSCALL_TABLE_ID0] = PPM_SC_SETGID, + [__NR_ia32_setfsuid - SYSCALL_TABLE_ID0] = PPM_SC_SETFSUID, + [__NR_ia32_setfsgid - SYSCALL_TABLE_ID0] = PPM_SC_SETFSGID, + [__NR_ia32_pivot_root - SYSCALL_TABLE_ID0] = PPM_SC_PIVOT_ROOT, + [__NR_ia32_mincore - SYSCALL_TABLE_ID0] = PPM_SC_MINCORE, + [__NR_ia32_madvise - SYSCALL_TABLE_ID0] = PPM_SC_MADVISE, + [__NR_ia32_gettid - SYSCALL_TABLE_ID0] = PPM_SC_GETTID, + [__NR_ia32_setxattr - SYSCALL_TABLE_ID0] = PPM_SC_SETXATTR, + [__NR_ia32_lsetxattr - SYSCALL_TABLE_ID0] = PPM_SC_LSETXATTR, + [__NR_ia32_fsetxattr - SYSCALL_TABLE_ID0] = PPM_SC_FSETXATTR, + [__NR_ia32_getxattr - SYSCALL_TABLE_ID0] = PPM_SC_GETXATTR, + [__NR_ia32_lgetxattr - SYSCALL_TABLE_ID0] = PPM_SC_LGETXATTR, + [__NR_ia32_fgetxattr - SYSCALL_TABLE_ID0] = PPM_SC_FGETXATTR, + [__NR_ia32_listxattr - SYSCALL_TABLE_ID0] = PPM_SC_LISTXATTR, + [__NR_ia32_llistxattr - SYSCALL_TABLE_ID0] = PPM_SC_LLISTXATTR, + [__NR_ia32_flistxattr - SYSCALL_TABLE_ID0] = PPM_SC_FLISTXATTR, + [__NR_ia32_removexattr - SYSCALL_TABLE_ID0] = PPM_SC_REMOVEXATTR, + [__NR_ia32_lremovexattr - SYSCALL_TABLE_ID0] = PPM_SC_LREMOVEXATTR, + [__NR_ia32_fremovexattr - SYSCALL_TABLE_ID0] = PPM_SC_FREMOVEXATTR, + [__NR_ia32_tkill - SYSCALL_TABLE_ID0] = PPM_SC_TKILL, + [__NR_ia32_futex - SYSCALL_TABLE_ID0] = PPM_SC_FUTEX, + [__NR_ia32_sched_setaffinity - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_SETAFFINITY, + [__NR_ia32_sched_getaffinity - SYSCALL_TABLE_ID0] = PPM_SC_SCHED_GETAFFINITY, +#ifdef __NR_ia32_set_thread_area + [__NR_ia32_set_thread_area - SYSCALL_TABLE_ID0] = PPM_SC_SET_THREAD_AREA, +#endif +#ifdef __NR_ia32_get_thread_area + [__NR_ia32_get_thread_area - SYSCALL_TABLE_ID0] = PPM_SC_GET_THREAD_AREA, +#endif + [__NR_ia32_io_setup - SYSCALL_TABLE_ID0] = PPM_SC_IO_SETUP, + [__NR_ia32_io_destroy - SYSCALL_TABLE_ID0] = PPM_SC_IO_DESTROY, + [__NR_ia32_io_getevents - SYSCALL_TABLE_ID0] = PPM_SC_IO_GETEVENTS, + [__NR_ia32_io_submit - SYSCALL_TABLE_ID0] = PPM_SC_IO_SUBMIT, + [__NR_ia32_io_cancel - SYSCALL_TABLE_ID0] = PPM_SC_IO_CANCEL, + [__NR_ia32_exit_group - SYSCALL_TABLE_ID0] = PPM_SC_EXIT_GROUP, + [__NR_ia32_epoll_create - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_CREATE, + [__NR_ia32_epoll_ctl - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_CTL, + [__NR_ia32_epoll_wait - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_WAIT, + [__NR_ia32_remap_file_pages - SYSCALL_TABLE_ID0] = PPM_SC_REMAP_FILE_PAGES, + [__NR_ia32_set_tid_address - SYSCALL_TABLE_ID0] = PPM_SC_SET_TID_ADDRESS, + [__NR_ia32_timer_create - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_CREATE, + [__NR_ia32_timer_settime - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_SETTIME, + [__NR_ia32_timer_gettime - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_GETTIME, + [__NR_ia32_timer_getoverrun - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_GETOVERRUN, + [__NR_ia32_timer_delete - SYSCALL_TABLE_ID0] = PPM_SC_TIMER_DELETE, + [__NR_ia32_clock_settime - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_SETTIME, + [__NR_ia32_clock_gettime - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_GETTIME, + [__NR_ia32_clock_getres - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_GETRES, + [__NR_ia32_clock_nanosleep - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_NANOSLEEP, + [__NR_ia32_tgkill - SYSCALL_TABLE_ID0] = PPM_SC_TGKILL, + [__NR_ia32_utimes - SYSCALL_TABLE_ID0] = PPM_SC_UTIMES, + [__NR_ia32_mq_open - SYSCALL_TABLE_ID0] = PPM_SC_MQ_OPEN, + [__NR_ia32_mq_unlink - SYSCALL_TABLE_ID0] = PPM_SC_MQ_UNLINK, + [__NR_ia32_mq_timedsend - SYSCALL_TABLE_ID0] = PPM_SC_MQ_TIMEDSEND, + [__NR_ia32_mq_timedreceive - SYSCALL_TABLE_ID0] = PPM_SC_MQ_TIMEDRECEIVE, + [__NR_ia32_mq_notify - SYSCALL_TABLE_ID0] = PPM_SC_MQ_NOTIFY, + [__NR_ia32_mq_getsetattr - SYSCALL_TABLE_ID0] = PPM_SC_MQ_GETSETATTR, + [__NR_ia32_kexec_load - SYSCALL_TABLE_ID0] = PPM_SC_KEXEC_LOAD, + [__NR_ia32_waitid - SYSCALL_TABLE_ID0] = PPM_SC_WAITID, + [__NR_ia32_add_key - SYSCALL_TABLE_ID0] = PPM_SC_ADD_KEY, + [__NR_ia32_request_key - SYSCALL_TABLE_ID0] = PPM_SC_REQUEST_KEY, + [__NR_ia32_keyctl - SYSCALL_TABLE_ID0] = PPM_SC_KEYCTL, + [__NR_ia32_ioprio_set - SYSCALL_TABLE_ID0] = PPM_SC_IOPRIO_SET, + [__NR_ia32_ioprio_get - SYSCALL_TABLE_ID0] = PPM_SC_IOPRIO_GET, + [__NR_ia32_inotify_init - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_INIT, + [__NR_ia32_inotify_add_watch - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_ADD_WATCH, + [__NR_ia32_inotify_rm_watch - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_RM_WATCH, + [__NR_ia32_openat - SYSCALL_TABLE_ID0] = PPM_SC_OPENAT, + [__NR_ia32_mkdirat - SYSCALL_TABLE_ID0] = PPM_SC_MKDIRAT, + [__NR_ia32_mknodat - SYSCALL_TABLE_ID0] = PPM_SC_MKNODAT, + [__NR_ia32_fchownat - SYSCALL_TABLE_ID0] = PPM_SC_FCHOWNAT, + [__NR_ia32_futimesat - SYSCALL_TABLE_ID0] = PPM_SC_FUTIMESAT, + [__NR_ia32_unlinkat - SYSCALL_TABLE_ID0] = PPM_SC_UNLINKAT, + [__NR_ia32_renameat - SYSCALL_TABLE_ID0] = PPM_SC_RENAMEAT, + [__NR_ia32_linkat - SYSCALL_TABLE_ID0] = PPM_SC_LINKAT, + [__NR_ia32_symlinkat - SYSCALL_TABLE_ID0] = PPM_SC_SYMLINKAT, + [__NR_ia32_readlinkat - SYSCALL_TABLE_ID0] = PPM_SC_READLINKAT, + [__NR_ia32_fchmodat - SYSCALL_TABLE_ID0] = PPM_SC_FCHMODAT, + [__NR_ia32_faccessat - SYSCALL_TABLE_ID0] = PPM_SC_FACCESSAT, + [__NR_ia32_pselect6 - SYSCALL_TABLE_ID0] = PPM_SC_PSELECT6, + [__NR_ia32_ppoll - SYSCALL_TABLE_ID0] = PPM_SC_PPOLL, + [__NR_ia32_unshare - SYSCALL_TABLE_ID0] = PPM_SC_UNSHARE, + [__NR_ia32_set_robust_list - SYSCALL_TABLE_ID0] = PPM_SC_SET_ROBUST_LIST, + [__NR_ia32_get_robust_list - SYSCALL_TABLE_ID0] = PPM_SC_GET_ROBUST_LIST, + [__NR_ia32_splice - SYSCALL_TABLE_ID0] = PPM_SC_SPLICE, + [__NR_ia32_tee - SYSCALL_TABLE_ID0] = PPM_SC_TEE, + [__NR_ia32_vmsplice - SYSCALL_TABLE_ID0] = PPM_SC_VMSPLICE, +#ifdef __NR_ia32_getcpu + [__NR_ia32_getcpu - SYSCALL_TABLE_ID0] = PPM_SC_GETCPU, +#endif + [__NR_ia32_epoll_pwait - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_PWAIT, + [__NR_ia32_utimensat - SYSCALL_TABLE_ID0] = PPM_SC_UTIMENSAT, + [__NR_ia32_signalfd - SYSCALL_TABLE_ID0] = PPM_SC_SIGNALFD, + [__NR_ia32_timerfd_create - SYSCALL_TABLE_ID0] = PPM_SC_TIMERFD_CREATE, + [__NR_ia32_eventfd - SYSCALL_TABLE_ID0] = PPM_SC_EVENTFD, + [__NR_ia32_timerfd_settime - SYSCALL_TABLE_ID0] = PPM_SC_TIMERFD_SETTIME, + [__NR_ia32_timerfd_gettime - SYSCALL_TABLE_ID0] = PPM_SC_TIMERFD_GETTIME, + [__NR_ia32_signalfd4 - SYSCALL_TABLE_ID0] = PPM_SC_SIGNALFD4, + [__NR_ia32_eventfd2 - SYSCALL_TABLE_ID0] = PPM_SC_EVENTFD2, + [__NR_ia32_epoll_create1 - SYSCALL_TABLE_ID0] = PPM_SC_EPOLL_CREATE1, + [__NR_ia32_dup3 - SYSCALL_TABLE_ID0] = PPM_SC_DUP3, + [__NR_ia32_pipe2 - SYSCALL_TABLE_ID0] = PPM_SC_PIPE2, + [__NR_ia32_inotify_init1 - SYSCALL_TABLE_ID0] = PPM_SC_INOTIFY_INIT1, + [__NR_ia32_preadv - SYSCALL_TABLE_ID0] = PPM_SC_PREADV, + [__NR_ia32_pwritev - SYSCALL_TABLE_ID0] = PPM_SC_PWRITEV, + [__NR_ia32_rt_tgsigqueueinfo - SYSCALL_TABLE_ID0] = PPM_SC_RT_TGSIGQUEUEINFO, + [__NR_ia32_perf_event_open - SYSCALL_TABLE_ID0] = PPM_SC_PERF_EVENT_OPEN, +#ifdef __NR_ia32_fanotify_init + [__NR_ia32_fanotify_init - SYSCALL_TABLE_ID0] = PPM_SC_FANOTIFY_INIT, +#endif +#ifdef __NR_ia32_prlimit64 + [__NR_ia32_prlimit64 - SYSCALL_TABLE_ID0] = PPM_SC_PRLIMIT64, +#endif +#ifdef __NR_ia32_clock_adjtime + [__NR_ia32_clock_adjtime - SYSCALL_TABLE_ID0] = PPM_SC_CLOCK_ADJTIME, +#endif +#ifdef __NR_ia32_syncfs + [__NR_ia32_syncfs - SYSCALL_TABLE_ID0] = PPM_SC_SYNCFS, +#endif +#ifdef __NR_ia32_setns + [__NR_ia32_setns - SYSCALL_TABLE_ID0] = PPM_SC_SETNS, +#endif + [__NR_ia32_getdents64 - SYSCALL_TABLE_ID0] = PPM_SC_GETDENTS64, +#ifndef __NR_ia32_socketcall + /* + * Non-multiplexed socket family + */ + [__NR_ia32_socket - SYSCALL_TABLE_ID0] = PPM_SC_SOCKET, + [__NR_ia32_bind - SYSCALL_TABLE_ID0] = PPM_SC_BIND, + [__NR_ia32_connect - SYSCALL_TABLE_ID0] = PPM_SC_CONNECT, + [__NR_ia32_listen - SYSCALL_TABLE_ID0] = PPM_SC_LISTEN, + [__NR_ia32_accept - SYSCALL_TABLE_ID0] = PPM_SC_ACCEPT, + [__NR_ia32_getsockname - SYSCALL_TABLE_ID0] = PPM_SC_GETSOCKNAME, + [__NR_ia32_getpeername - SYSCALL_TABLE_ID0] = PPM_SC_GETPEERNAME, + [__NR_ia32_socketpair - SYSCALL_TABLE_ID0] = PPM_SC_SOCKETPAIR, +/* [__NR_ia32_send - SYSCALL_TABLE_ID0] = PPM_SC_NR_SEND, */ + [__NR_ia32_sendto - SYSCALL_TABLE_ID0] = PPM_SC_SENDTO, +/* [__NR_ia32_recv - SYSCALL_TABLE_ID0] = PPM_SC_NR_RECV, */ + [__NR_ia32_recvfrom - SYSCALL_TABLE_ID0] = PPM_SC_RECVFROM, + [__NR_ia32_shutdown - SYSCALL_TABLE_ID0] = PPM_SC_SHUTDOWN, + [__NR_ia32_setsockopt - SYSCALL_TABLE_ID0] = PPM_SC_SETSOCKOPT, + [__NR_ia32_getsockopt - SYSCALL_TABLE_ID0] = PPM_SC_GETSOCKOPT, + [__NR_ia32_sendmsg - SYSCALL_TABLE_ID0] = PPM_SC_SENDMSG, + [__NR_ia32_recvmsg - SYSCALL_TABLE_ID0] = PPM_SC_RECVMSG, + [__NR_ia32_accept4 - SYSCALL_TABLE_ID0] = PPM_SC_ACCEPT4, #else - [__NR_statfs64] = PPM_SC_STATFS64, - [__NR_fstatfs64] = PPM_SC_FSTATFS64, - [__NR_fstatat64] = PPM_SC_FSTATAT64, - [__NR_sendfile64] = PPM_SC_SENDFILE64, - [__NR_ugetrlimit] = PPM_SC_UGETRLIMIT, - [__NR_bdflush] = PPM_SC_BDFLUSH, - [__NR_sigprocmask] = PPM_SC_SIGPROCMASK, - [__NR_ipc] = PPM_SC_IPC, - [__NR_socketcall] = PPM_SC_SOCKETCALL, - [__NR_stat64] = PPM_SC_STAT64, - [__NR_lstat64] = PPM_SC_LSTAT64, - [__NR_fstat64] = PPM_SC_FSTAT64, - [__NR_fcntl64] = PPM_SC_FCNTL64, - [__NR_mmap2] = PPM_SC_MMAP2, - [__NR__newselect] = PPM_SC__NEWSELECT, - [__NR_sgetmask] = PPM_SC_SGETMASK, - [__NR_ssetmask] = PPM_SC_SSETMASK, -/* [__NR_setreuid16] = PPM_SC_NR_SETREUID16, */ -/* [__NR_setregid16] = PPM_SC_NR_SETREGID16, */ - [__NR_sigpending] = PPM_SC_SIGPENDING, - [__NR_olduname] = PPM_SC_OLDUNAME, - [__NR_umount] = PPM_SC_UMOUNT, - [__NR_signal] = PPM_SC_SIGNAL, - [__NR_nice] = PPM_SC_NICE, - [__NR_stime] = PPM_SC_STIME, - [__NR__llseek] = PPM_SC__LLSEEK, - [__NR_waitpid] = PPM_SC_WAITPID, - [__NR_pread64] = PPM_SC_PREAD64, - [__NR_pwrite64] = PPM_SC_PWRITE64, -#endif /* __x86_64__ */ + [__NR_ia32_socketcall - SYSCALL_TABLE_ID0] = PPM_SC_SOCKETCALL, +#endif + + +#ifdef __NR_ia32_sendmmsg + [__NR_ia32_sendmmsg - SYSCALL_TABLE_ID0] = PPM_SC_SENDMMSG, +#endif +#ifdef __NR_ia32_recvmmsg + [__NR_ia32_recvmmsg - SYSCALL_TABLE_ID0] = PPM_SC_RECVMMSG, +#endif + /* + * Non-multiplexed IPC family + */ +#ifdef __NR_ia32_semop + [__NR_ia32_semop - SYSCALL_TABLE_ID0] = PPM_SC_SEMOP, +#endif +#ifdef __NR_ia32_semget + [__NR_ia32_semget - SYSCALL_TABLE_ID0] = PPM_SC_SEMGET, +#endif +#ifdef __NR_ia32_semctl + [__NR_ia32_semctl - SYSCALL_TABLE_ID0] = PPM_SC_SEMCTL, +#endif +#ifdef __NR_ia32_semget + [__NR_ia32_semget - SYSCALL_TABLE_ID0] = PPM_SC_SEMGET, +#endif +#ifdef __NR_ia32_msgsnd + [__NR_ia32_msgsnd - SYSCALL_TABLE_ID0] = PPM_SC_MSGSND, +#endif +#ifdef __NR_ia32_msgrcv + [__NR_ia32_msgrcv - SYSCALL_TABLE_ID0] = PPM_SC_MSGRCV, +#endif +#ifdef __NR_ia32_msgget + [__NR_ia32_msgget - SYSCALL_TABLE_ID0] = PPM_SC_MSGGET, +#endif +#ifdef __NR_ia32_msgctl + [__NR_ia32_msgctl - SYSCALL_TABLE_ID0] = PPM_SC_MSGCTL, +#endif +/* [__NR_ia32_shmatcall - SYSCALL_TABLE_ID0] = PPM_SC_NR_SHMATCALL, */ +#ifdef __NR_ia32_shmdt + [__NR_ia32_shmdt - SYSCALL_TABLE_ID0] = PPM_SC_SHMDT, +#endif +#ifdef __NR_ia32_shmget + [__NR_ia32_shmget - SYSCALL_TABLE_ID0] = PPM_SC_SHMGET, +#endif +#ifdef __NR_ia32_shmctl + [__NR_ia32_shmctl - SYSCALL_TABLE_ID0] = PPM_SC_SHMCTL, +#endif +/* [__NR_ia32_fcntl64 - SYSCALL_TABLE_ID0] = PPM_SC_NR_FCNTL64, */ +#ifdef __NR_ia32_statfs64 + [__NR_ia32_statfs64 - SYSCALL_TABLE_ID0] = PPM_SC_STATFS64, +#endif +#ifdef __NR_ia32_fstatfs64 + [__NR_ia32_fstatfs64 - SYSCALL_TABLE_ID0] = PPM_SC_FSTATFS64, +#endif +#ifdef __NR_ia32_fstatat64 + [__NR_ia32_fstatat64 - SYSCALL_TABLE_ID0] = PPM_SC_FSTATAT64, +#endif +#ifdef __NR_ia32_sendfile64 + [__NR_ia32_sendfile64 - SYSCALL_TABLE_ID0] = PPM_SC_SENDFILE64, +#endif +#ifdef __NR_ia32_ugetrlimit + [__NR_ia32_ugetrlimit - SYSCALL_TABLE_ID0] = PPM_SC_UGETRLIMIT, +#endif +#ifdef __NR_ia32_bdflush + [__NR_ia32_bdflush - SYSCALL_TABLE_ID0] = PPM_SC_BDFLUSH, +#endif +#ifdef __NR_ia32_sigprocmask + [__NR_ia32_sigprocmask - SYSCALL_TABLE_ID0] = PPM_SC_SIGPROCMASK, +#endif +#ifdef __NR_ia32_ipc + [__NR_ia32_ipc - SYSCALL_TABLE_ID0] = PPM_SC_IPC, +#endif +#ifdef __NR_ia32_stat64 + [__NR_ia32_stat64 - SYSCALL_TABLE_ID0] = PPM_SC_STAT64, +#endif +#ifdef __NR_ia32_lstat64 + [__NR_ia32_lstat64 - SYSCALL_TABLE_ID0] = PPM_SC_LSTAT64, +#endif +#ifdef __NR_ia32_fstat64 + [__NR_ia32_fstat64 - SYSCALL_TABLE_ID0] = PPM_SC_FSTAT64, +#endif +#ifdef __NR_ia32_fcntl64 + [__NR_ia32_fcntl64 - SYSCALL_TABLE_ID0] = PPM_SC_FCNTL64, +#endif +#ifdef __NR_ia32_mmap2 + [__NR_ia32_mmap2 - SYSCALL_TABLE_ID0] = PPM_SC_MMAP2, +#endif +#ifdef __NR_ia32__newselect + [__NR_ia32__newselect - SYSCALL_TABLE_ID0] = PPM_SC__NEWSELECT, +#endif +#ifdef __NR_ia32_sgetmask + [__NR_ia32_sgetmask - SYSCALL_TABLE_ID0] = PPM_SC_SGETMASK, +#endif +#ifdef __NR_ia32_ssetmask + [__NR_ia32_ssetmask - SYSCALL_TABLE_ID0] = PPM_SC_SSETMASK, +#endif + +/* [__NR_ia32_setreuid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETREUID16, */ +/* [__NR_ia32_setregid16 - SYSCALL_TABLE_ID0] = PPM_SC_NR_SETREGID16, */ +#ifdef __NR_ia32_sigpending + [__NR_ia32_sigpending - SYSCALL_TABLE_ID0] = PPM_SC_SIGPENDING, +#endif +#ifdef __NR_ia32_olduname + [__NR_ia32_olduname - SYSCALL_TABLE_ID0] = PPM_SC_OLDUNAME, +#endif +#ifdef __NR_ia32_umount + [__NR_ia32_umount - SYSCALL_TABLE_ID0] = PPM_SC_UMOUNT, +#endif +#ifdef __NR_ia32_signal + [__NR_ia32_signal - SYSCALL_TABLE_ID0] = PPM_SC_SIGNAL, +#endif +#ifdef __NR_ia32_nice + [__NR_ia32_nice - SYSCALL_TABLE_ID0] = PPM_SC_NICE, +#endif +#ifdef __NR_ia32_stime + [__NR_ia32_stime - SYSCALL_TABLE_ID0] = PPM_SC_STIME, +#endif +#ifdef __NR_ia32__llseek + [__NR_ia32__llseek - SYSCALL_TABLE_ID0] = PPM_SC__LLSEEK, +#endif +#ifdef __NR_ia32_waitpid + [__NR_ia32_waitpid - SYSCALL_TABLE_ID0] = PPM_SC_WAITPID, +#endif +#ifdef __NR_ia32_pread64 + [__NR_ia32_pread64 - SYSCALL_TABLE_ID0] = PPM_SC_PREAD64, +#endif +#ifdef __NR_ia32_pwrite64 + [__NR_ia32_pwrite64 - SYSCALL_TABLE_ID0] = PPM_SC_PWRITE64, +#endif +#ifdef __NR_ia32_shmat + [__NR_ia32_shmat - SYSCALL_TABLE_ID0] = PPM_SC_SHMAT, +#endif +#ifdef __NR_ia32_rt_sigreturn + [__NR_ia32_rt_sigreturn - SYSCALL_TABLE_ID0] = PPM_SC_SIGRETURN, +#endif +#ifdef __NR_ia32_fallocate + [__NR_ia32_fallocate - SYSCALL_TABLE_ID0] = PPM_SC_FALLOCATE, +#endif +#ifdef __NR_ia32_newfstatat + [__NR_ia32_newfstatat - SYSCALL_TABLE_ID0] = PPM_SC_NEWFSSTAT, +#endif +#ifdef __NR_ia32_process_vm_readv + [__NR_ia32_process_vm_readv - SYSCALL_TABLE_ID0] = PPM_SC_PROCESS_VM_READV, +#endif +#ifdef __NR_ia32_process_vm_writev + [__NR_ia32_process_vm_writev - SYSCALL_TABLE_ID0] = PPM_SC_PROCESS_VM_WRITEV, +#endif +#ifdef __NR_ia32_fork + [__NR_ia32_fork - SYSCALL_TABLE_ID0] = PPM_SC_FORK, +#endif +#ifdef __NR_ia32_vfork + [__NR_ia32_vfork - SYSCALL_TABLE_ID0] = PPM_SC_VFORK, +#endif +#ifdef __NR_ia32_quotactl + [__NR_ia32_quotactl - SYSCALL_TABLE_ID0] = PPM_SC_QUOTACTL, +#endif +#ifdef __NR_ia32_setresuid + [__NR_ia32_setresuid - SYSCALL_TABLE_ID0] = PPM_SC_SETRESUID, +#endif +#ifdef __NR_ia32_setresuid32 + [__NR_ia32_setresuid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETRESUID, +#endif +#ifdef __NR_ia32_setresgid + [__NR_ia32_setresgid - SYSCALL_TABLE_ID0] = PPM_SC_SETRESGID, +#endif +#ifdef __NR_ia32_setresgid32 + [__NR_ia32_setresgid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETRESGID, +#endif +#ifdef __NR_ia32_setuid + [__NR_ia32_setuid - SYSCALL_TABLE_ID0] = PPM_SC_SETUID, +#endif +#ifdef __NR_ia32_setuid32 + [__NR_ia32_setuid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETUID32, +#endif +#ifdef __NR_ia32_setgid + [__NR_ia32_setgid - SYSCALL_TABLE_ID0] = PPM_SC_SETGID, +#endif +#ifdef __NR_ia32_setgid32 + [__NR_ia32_setgid32 - SYSCALL_TABLE_ID0] = PPM_SC_SETGID32, +#endif +#ifdef __NR_ia32_getuid + [__NR_ia32_getuid - SYSCALL_TABLE_ID0] = PPM_SC_GETUID, +#endif +#ifdef __NR_ia32_getuid32 + [__NR_ia32_getuid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETUID32, +#endif +#ifdef __NR_ia32_geteuid + [__NR_ia32_geteuid - SYSCALL_TABLE_ID0] = PPM_SC_GETEUID, +#endif +#ifdef __NR_ia32_geteuid32 + [__NR_ia32_geteuid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETEUID, +#endif +#ifdef __NR_ia32_getgid + [__NR_ia32_getgid - SYSCALL_TABLE_ID0] = PPM_SC_GETGID, +#endif +#ifdef __NR_ia32_getgid32 + [__NR_ia32_getgid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETGID, +#endif +#ifdef __NR_ia32_getegid + [__NR_ia32_getegid - SYSCALL_TABLE_ID0] = PPM_SC_GETEGID, +#endif +#ifdef __NR_ia32_getegid32 + [__NR_ia32_getegid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETEGID, +#endif +#ifdef __NR_ia32_getresuid + [__NR_ia32_getresuid - SYSCALL_TABLE_ID0] = PPM_SC_GETRESUID, +#endif +#ifdef __NR_ia32_getresuid32 + [__NR_ia32_getresuid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETRESUID32, +#endif +#ifdef __NR_ia32_getresgid + [__NR_ia32_getresgid - SYSCALL_TABLE_ID0] = PPM_SC_GETRESGID, +#endif +#ifdef __NR_ia32_getresgid32 + [__NR_ia32_getresgid32 - SYSCALL_TABLE_ID0] = PPM_SC_GETRESGID32, +#endif +#ifdef __NR_ia32_finit_module + [__NR_ia32_finit_module - SYSCALL_TABLE_ID0] = PPM_SC_FINIT_MODULE, +#endif +#ifdef __NR_ia32_bpf + [__NR_ia32_bpf - SYSCALL_TABLE_ID0] = PPM_SC_BPF, +#endif +#ifdef __NR_ia32_seccomp + [__NR_ia32_seccomp - SYSCALL_TABLE_ID0] = PPM_SC_SECCOMP, +#endif +#ifdef __NR_ia32_sigaltstack + [__NR_ia32_sigaltstack - SYSCALL_TABLE_ID0] = PPM_SC_SIGALTSTACK, +#endif +#ifdef __NR_ia32_getrandom + [__NR_ia32_getrandom - SYSCALL_TABLE_ID0] = PPM_SC_GETRANDOM, +#endif +#ifdef __NR_ia32_fadvise64 + [__NR_ia32_fadvise64 - SYSCALL_TABLE_ID0] = PPM_SC_FADVISE64, +#endif +#ifdef __NR_ia32_renameat2 + [__NR_ia32_renameat2 - SYSCALL_TABLE_ID0] = PPM_SC_RENAMEAT2, +#endif }; + +#endif /* CONFIG_IA32_EMULATION */ diff --git a/probe-builder/Dockerfile b/probe-builder/Dockerfile new file mode 100644 index 0000000000..b7df339723 --- /dev/null +++ b/probe-builder/Dockerfile @@ -0,0 +1,19 @@ +FROM alpine + +RUN apk add \ + bash \ + gawk \ + grep \ + curl \ + dpkg \ + rpm2cpio \ + git \ + jq \ + multipath-tools \ + py3-lxml \ + wget \ + docker + +ADD . /builder +WORKDIR /builder +ENTRYPOINT [ "/builder/main-builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.centos-gcc4.4 b/probe-builder/Dockerfile.centos-gcc4.4 new file mode 100644 index 0000000000..d468fe80ba --- /dev/null +++ b/probe-builder/Dockerfile.centos-gcc4.4 @@ -0,0 +1,18 @@ +FROM centos:6 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + bison \ + flex \ + make \ + cmake \ + elfutils-devel \ + python-lxml && yum clean all + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.centos-gcc4.8 b/probe-builder/Dockerfile.centos-gcc4.8 new file mode 100644 index 0000000000..3ae750b912 --- /dev/null +++ b/probe-builder/Dockerfile.centos-gcc4.8 @@ -0,0 +1,18 @@ +FROM centos:7 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + bison \ + flex \ + make \ + cmake \ + elfutils-devel \ + python-lxml && yum clean all + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.centos-gcc7.3 b/probe-builder/Dockerfile.centos-gcc7.3 new file mode 100644 index 0000000000..927bfcf733 --- /dev/null +++ b/probe-builder/Dockerfile.centos-gcc7.3 @@ -0,0 +1,20 @@ +FROM fedora:27 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + bison \ + flex \ + make \ + cmake \ + elfutils-devel \ + findutils \ + kmod \ + python-lxml && yum clean all + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.centos-gcc8.3 b/probe-builder/Dockerfile.centos-gcc8.3 new file mode 100644 index 0000000000..4a5b4863f3 --- /dev/null +++ b/probe-builder/Dockerfile.centos-gcc8.3 @@ -0,0 +1,20 @@ +FROM fedora:29 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + bison \ + flex \ + make \ + cmake \ + elfutils-devel \ + findutils \ + kmod \ + python-lxml && yum clean all + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.centos-gcc9.2 b/probe-builder/Dockerfile.centos-gcc9.2 new file mode 100644 index 0000000000..e03b4d2d61 --- /dev/null +++ b/probe-builder/Dockerfile.centos-gcc9.2 @@ -0,0 +1,20 @@ +FROM fedora:31 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + bison \ + flex \ + make \ + cmake \ + elfutils-devel \ + findutils \ + kmod \ + python-lxml && yum clean all + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.coreos-old b/probe-builder/Dockerfile.coreos-old new file mode 100644 index 0000000000..8b2fb3938d --- /dev/null +++ b/probe-builder/Dockerfile.coreos-old @@ -0,0 +1,20 @@ +FROM centos:7 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + bison \ + flex \ + make \ + cmake \ + elfutils-devel \ + bc \ + openssl-devel \ + python-lxml && yum clean all + +ADD builder-entrypoint-coreos.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint-coreos.sh" ] diff --git a/probe-builder/Dockerfile.debian-gcc4.9 b/probe-builder/Dockerfile.debian-gcc4.9 new file mode 100644 index 0000000000..0b4fcf48a8 --- /dev/null +++ b/probe-builder/Dockerfile.debian-gcc4.9 @@ -0,0 +1,16 @@ +FROM debian:jessie + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + kmod \ + libc6-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] + diff --git a/probe-builder/Dockerfile.debian-gcc6.3 b/probe-builder/Dockerfile.debian-gcc6.3 new file mode 100644 index 0000000000..639fdcca17 --- /dev/null +++ b/probe-builder/Dockerfile.debian-gcc6.3 @@ -0,0 +1,16 @@ +FROM debian:stretch + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + kmod \ + libc6-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] + diff --git a/probe-builder/Dockerfile.debian-gcc8.3 b/probe-builder/Dockerfile.debian-gcc8.3 new file mode 100644 index 0000000000..9b08543792 --- /dev/null +++ b/probe-builder/Dockerfile.debian-gcc8.3 @@ -0,0 +1,16 @@ +FROM debian:buster + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + kmod \ + libc6-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] + diff --git a/probe-builder/Dockerfile.ol6 b/probe-builder/Dockerfile.ol6 new file mode 100644 index 0000000000..e5e8b5bf34 --- /dev/null +++ b/probe-builder/Dockerfile.ol6 @@ -0,0 +1,17 @@ +FROM oraclelinux:6 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + make \ + cmake \ + libdtrace-ctf \ + elfutils-devel \ + python-lxml && yum clean all + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.ol7 b/probe-builder/Dockerfile.ol7 new file mode 100644 index 0000000000..cd3a2ad61b --- /dev/null +++ b/probe-builder/Dockerfile.ol7 @@ -0,0 +1,18 @@ +FROM oraclelinux:7 + +RUN yum -y install \ + wget \ + git \ + gcc \ + gcc-c++ \ + autoconf \ + make \ + cmake \ + libdtrace-ctf \ + elfutils-libelf-devel \ + file \ + python-lxml && yum clean all + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.toolkit b/probe-builder/Dockerfile.toolkit new file mode 100644 index 0000000000..14089abc23 --- /dev/null +++ b/probe-builder/Dockerfile.toolkit @@ -0,0 +1,5 @@ +FROM alpine + +RUN apk add rpm2cpio multipath-tools +ADD toolkit-entrypoint.sh /toolkit-entrypoint.sh +ENTRYPOINT ["/toolkit-entrypoint.sh"] diff --git a/probe-builder/Dockerfile.ubuntu-gcc4.8 b/probe-builder/Dockerfile.ubuntu-gcc4.8 new file mode 100644 index 0000000000..afa62d459b --- /dev/null +++ b/probe-builder/Dockerfile.ubuntu-gcc4.8 @@ -0,0 +1,15 @@ +FROM ubuntu:14.04 + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + libc6-dev \ + libelf-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/Dockerfile.ubuntu-gcc5.4 b/probe-builder/Dockerfile.ubuntu-gcc5.4 new file mode 100644 index 0000000000..8aaccaa8b6 --- /dev/null +++ b/probe-builder/Dockerfile.ubuntu-gcc5.4 @@ -0,0 +1,17 @@ +FROM ubuntu:16.04 + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + kmod \ + libc6-dev \ + libelf-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] + diff --git a/probe-builder/Dockerfile.ubuntu-gcc7.4 b/probe-builder/Dockerfile.ubuntu-gcc7.4 new file mode 100644 index 0000000000..72d060dd98 --- /dev/null +++ b/probe-builder/Dockerfile.ubuntu-gcc7.4 @@ -0,0 +1,17 @@ +FROM ubuntu:18.04 + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + kmod \ + libc6-dev \ + libelf-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] + diff --git a/probe-builder/Dockerfile.ubuntu-gcc8.3 b/probe-builder/Dockerfile.ubuntu-gcc8.3 new file mode 100644 index 0000000000..16b217ebf1 --- /dev/null +++ b/probe-builder/Dockerfile.ubuntu-gcc8.3 @@ -0,0 +1,17 @@ +FROM ubuntu:19.04 + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + kmod \ + libc6-dev \ + libelf-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] + diff --git a/probe-builder/Dockerfile.ubuntu-gcc9.2 b/probe-builder/Dockerfile.ubuntu-gcc9.2 new file mode 100644 index 0000000000..d92431184e --- /dev/null +++ b/probe-builder/Dockerfile.ubuntu-gcc9.2 @@ -0,0 +1,16 @@ +FROM ubuntu:19.10 + +RUN apt-get update && apt-get -y --no-install-recommends install \ + cmake \ + g++ \ + git \ + kmod \ + libc6-dev \ + libelf-dev \ + make \ + pkg-config \ + && apt-get clean + +ADD builder-entrypoint.sh / +WORKDIR /build/probe +ENTRYPOINT [ "/builder-entrypoint.sh" ] diff --git a/probe-builder/build-probe-binaries b/probe-builder/build-probe-binaries new file mode 100755 index 0000000000..c10998b8c4 --- /dev/null +++ b/probe-builder/build-probe-binaries @@ -0,0 +1,1031 @@ +#!/bin/bash +# +# Copyright (C) 2013-2020 Sysdig, Inc. +# +# This file is part of sysdig . +# +# 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. +# + +# +# This script builds a precompiled version of sysdig-probe for a bunch of kernels +# The precompiled binary is then obtained at runtime by sysdig-probe-loader +# Ideally, the community should expand this stuff with better support +# +set -euo pipefail + +ARTIFACTORY_SERVER= +ARTIFACTORY_KEY= +BUILDER_IMAGE_PREFIX= +DOWNLOAD_CONCURRENCY=$(nproc) +KERNEL_TYPE= +PROBE_NAME=sysdig-probe +RETRIES=10 +DOWNLOAD_TIMEOUT=300 +PROBE_VERSION= +RUNNING_IN_DOCKER= +FAILED= +FAIL_LOG=$(mktemp /tmp/fail.log.XXXXXX) + +usage() { + cat >&2 <sysdig-probe-builder:, where: + - is the value of this option, + - is chosen for every build. See the Dockerfiles shipped + with this script. + + -B BASEDIR + Pass BASEDIR as the base directory to the builder containers + This is probably only useful when running this script in a container + + -d DOWNLOAD_CONCURRENCY + The number of parallel kernel package downloads. Set to 0 to disable + downloads completely. Defaults to the number of CPU cores. + + Note: CoreOS images are downloaded serially, regardless of the value + of this setting (0 is still honored). + + -k KERNEL_TYPE + Build only for specified kernel types. The supported types are: + AmazonLinux + AmazonLinux2 + CentOS + CoreOS + Debian + Fedora + Fedora-Atomic + OracleOL6 + OracleOL7 + OracleRHCK + RHEL [requires Artifactory authentication] + Ubuntu + + If not specified, builds all the above types (RHEL kernels will be + skipped if no Artifactory credentials are provided). + + Additionally, you can skip the downloading of distribution kernels + and provide your own DEBs/RPMs for the Custom* kernel types: + + $0 (...) -k CustomCentOS path/*.rpm + $0 (...) -k CustomCoreOS path/*.bin.bz2 + $0 (...) -k CustomDebian path/*.deb + $0 (...) -k CustomUbuntu path/*.deb + + The CustomCentOS type should also work for other RPM-based distributions, + like Fedora, RHEL or AmazonLinux + + For CoreOS, the downloaded images must be named + coreos_developer_container.VERSION.bin.bz2, where VERSION is + the CoreOS release (e.g. 2345.0.0). + + -p PROBE_NAME + The resulting module name (e.g. sysdig-probe for OSS sysdig). + + -r RETRIES + Number of retries for kernel package downloads. + + -s SYSDIG_DIR + The directory containing an existing Sysdig checkout in the required + version. Specify this option to prevent this script from cloning + the Sysdig repository. + + Note: you still need to pass -v with the right version, otherwise + the version number and the actual code will diverge, leading to + unpredictable behavior, including kernel crashes. + + -t DOWNLOAD_TIMEOUT + Timeout for kernel package downloads, in seconds. + + -v VERSION + The resulting module version. If not disabled (see -s), this script + will clone the Sysdig repository again and check out the right + version. + +Examples: + - build the probe for all distro kernels we know of: + $0 -v 0.26.5 + + - build the probe for all CentOS kernels + $0 -v 0.26.5 -k CentOS + + - build the probe for a custom set of Ubuntu kernels + $0 -v 0.26.5 -k CustomUbuntu pool/l/linux-{image,modules,headers}*.deb + + - build the probe for a specific CoreOS release + wget http://alpha.release.core-os.net/amd64-usr/2345.0.0/coreos_developer_container.bin.bz2 -O coreos_developer_container.2345.0.0.bin.bz2 + $0 -v 0.26.5 -k CustomCoreOS coreos_developer_container.2345.0.0.bin.bz2 +EOF + exit 1 +} + +BASEDIR=$(pwd) +DOCKER_BASEDIR=$BASEDIR +while getopts ":a:A:b:B:d:k:p:r:s:t:v:" opt +do + case "$opt" in + a) ARTIFACTORY_SERVER=$OPTARG;; + A) ARTIFACTORY_KEY=$OPTARG;; + b) BUILDER_IMAGE_PREFIX=$OPTARG;; + B) DOCKER_BASEDIR=$OPTARG + RUNNING_IN_DOCKER=1;; + d) DOWNLOAD_CONCURRENCY=$OPTARG;; + k) KERNEL_TYPE=$OPTARG;; + p) PROBE_NAME=$OPTARG;; + r) RETRIES=$OPTARG;; + s) SYSDIG_DIR=$OPTARG;; + t) DOWNLOAD_TIMEOUT=$OPTARG;; + v) PROBE_VERSION=$OPTARG;; + \?) + echo "Invalid option $OPTARG" >&2 + usage + ;; + :) + echo "Option $OPTARG requires an argument" >&2 + usage + ;; + esac +done + +shift $((OPTIND - 1)) + +if [ -z "$PROBE_VERSION" ] +then + echo "-v VERSION is mandatory" >&2 + usage +fi + +if [ -n "${SYSDIG_DIR:-}" ] +then + # assume sysdig in the right version is already checked out in the directory + HAVE_SYSDIG_CHECKOUT=1 +else + SYSDIG_DIR=sysdig + HAVE_SYSDIG_CHECKOUT= +fi + +if [[ "$SYSDIG_DIR" != "/"* ]] +then + SYSDIG_DIR=$(readlink -f $SYSDIG_DIR) +fi +ARCH=$(uname -m) +BUILDER_SOURCE=$(dirname $(readlink -f $0)) + +if [ ! -d $BASEDIR/output ]; then + mkdir $BASEDIR/output +fi + +PROBE_DEVICE_NAME=$(echo $PROBE_NAME | cut -f1 -d-) + +if [ $PROBE_NAME = "sysdigcloud-probe" ]; then + SYSDIG_TAG="agent/${PROBE_VERSION}" +elif [ $PROBE_NAME = "sysdig-probe" ]; then + SYSDIG_TAG="${PROBE_VERSION}" +else + SYSDIG_TAG="${PROBE_DEVICE_NAME}/${PROBE_VERSION}" +fi + +# The previously released versions of sysdig, falco, and the agent +# don't have one of the necessary changes that this script requires +# (namely, the ability to run cmake from the sysdig directory but +# specify PROBE_NAME, PROBE_VERSION, and PROBE_DEVICE_NAME). Rather +# than change what was in the tag we made modified tags x.y.z-cfgprobe +# that include the sysdig version x.y.z but also one additional commit +# that allows making the probe name/version/etc configurable. + +if [ $SYSDIG_TAG = "0.24.1" ] || [ $SYSDIG_TAG = "agent/0.85.1" ] || [ $SYSDIG_TAG = "falco/0.12.1" ]; then + SYSDIG_TAG="${SYSDIG_TAG}-cfgprobe" +fi + +function checkout_sysdig { + if [ -z "$HAVE_SYSDIG_CHECKOUT" ] + then + rm -rf $SYSDIG_DIR + mkdir -p $SYSDIG_DIR + git clone git@github.com:draios/sysdig.git $SYSDIG_DIR + (cd $SYSDIG_DIR && git checkout $SYSDIG_TAG) + fi + HAVE_SYSDIG_CHECKOUT=1 +} + +function build_toolkit { + if [ -z "${HAVE_BUILDER_TOOLKIT:-}" ] + then + IMAGE_NAME="${BUILDER_IMAGE_PREFIX:-}sysdig-probe-builder:toolkit" + docker build -t "$IMAGE_NAME" -f $BUILDER_SOURCE/Dockerfile.toolkit --pull $BUILDER_SOURCE + HAVE_BUILDER_TOOLKIT=1 + fi +} + +function run_toolkit { + if [ -z "$RUNNING_IN_DOCKER" ] + then + build_toolkit + fi + declare -a DOCKER_OPTS + declare -a TOOLKIT_OPTS + + IN_TOOLKIT_OPTS= + for i in "$@" + do + if [ -z "$IN_TOOLKIT_OPTS" ] + then + if [ "$i" == "--" ] + then + IN_TOOLKIT_OPTS=1 + else + DOCKER_OPTS+=("$i") + fi + else + TOOLKIT_OPTS+=("$i") + fi + done + + if [ -z "$RUNNING_IN_DOCKER" ] + then + # running on the host, use toolkit container + docker run --rm --privileged ${DOCKER_OPTS[@]} "${BUILDER_IMAGE_PREFIX:-}sysdig-probe-builder:toolkit" ${TOOLKIT_OPTS[@]} + else + # already in a container, call the toolkit directly + /builder/toolkit-entrypoint.sh ${TOOLKIT_OPTS[@]} + fi +} + +declare -A builders +function build_probe_impl { + PROBE_ROOT=${PROBE_ROOT:-/build/probe} + if [ -n "${DOCKER_BUILDER:-}" ] + then + DOCKERFILE=$BUILDER_SOURCE/Dockerfile.$DOCKER_BUILDER + DOCKERFILE_TAG=$DOCKER_BUILDER + + echo "Forced use of builder $DOCKERFILE_TAG" + elif [ -n "${BUILDER_DISTRO:-}" ] + then + # Try to find the gcc version used to build this particular kernel + # Check CONFIG_GCC_VERSION=90201 in the kernel config first + # as 5.8.0 seems to have a different format for the LINUX_COMPILER string + if [ -e $KERNELDIR/include/generated/autoconf.h ] + then + BUILDER_GCC_VERSION=$(grep -Po '(?<=^#define CONFIG_GCC_VERSION ).*' $KERNELDIR/include/generated/autoconf.h | sed '-res@(.*)(..)(..)$@\1\.\2\.\3@' '-es@\.0@\.@g') + else + BUILDER_GCC_VERSION= + fi + + if [ -z "$BUILDER_GCC_VERSION" ] + then + if [ -e $KERNELDIR/include/generated/compile.h ] + then + BUILDER_GCC_VERSION=$(grep -Po '(?<=^#define LINUX_COMPILER "gcc version )[0-9.]+' $KERNELDIR/include/generated/compile.h || true) + elif [ -e $KERNELDIR/include/linux/compile.h ] + then + # RHEL6 + BUILDER_GCC_VERSION=$(grep -Po '(?<=^#define LINUX_COMPILER "gcc version )[0-9.]+' $KERNELDIR/include/linux/compile.h || true) + else + # ancient Ubuntu gets and ancient compiler + BUILDER_GCC_VERSION=4.8.0 + fi + fi + + if [ -z "$BUILDER_GCC_VERSION" ] + then + echo "Failed to find gcc version for $KERNELDIR" + return 1 + fi + + # We don't really care about the compiler patch levels, only the major/minor version + BUILDER_GCC_MINOR_VERSION="${BUILDER_GCC_VERSION%.*}" + + # Choose the right gcc version from the ones we have available (as Docker images) + # - if we have the exact minor version, use it + # - if not, and there's a newer compiler version, use that + # (as close to the requested version as possible) + # - if there are no newer compilers, use the newest one we have + # it will be older than the requested one but hopefully + # not by much + # + # This means we don't have to exactly follow all distro gcc versions + # (indeed, we don't e.g. for AmazonLinux) but only need to add a new + # Dockerfile when the latest kernel fails to build with our newest + # gcc for that distro + + # The dockerfiles in question all look like .../Dockerfile.centos-gcc4.4 + # or similar. We want to pick the one that's closest to $BUILDER_GCC_MINOR_VERSION + # (exact match, slightly newer, slightly older, in that order of preference). + # To do that, we iterate over the list of all available dockerfiles (i.e. gcc + # versions) for a particular distribution in ascending version order (oldest->newest). + # Sadly, sort -V falls back to ASCII order with non-numeric prefixes, i.e. + # these are sorted according to sort -V: + # Dockerfile.centos-gcc10.0 + # Dockerfile.centos-gcc4.4 + # Dockerfile.centos-gcc9.2 + # So, to get actual sorting by version numbers, we strip the common prefix first + # and add it back after finding the best available version. What we're sorting is: + # 4.4 + # 9.2 + # 10.0 + # and now we properly realize that gcc 10 is newer than 9.2, not older than 4.4 + DOCKERFILE_PREFIX=$BUILDER_SOURCE/Dockerfile.${BUILDER_DISTRO}-gcc + CHOSEN_GCC_VERSION=$(ls $DOCKERFILE_PREFIX* | sed "s@$DOCKERFILE_PREFIX@@" | sort -V | awk \ + -v target=${BUILDER_GCC_MINOR_VERSION} \ + ' + $1 < target { older = $1 } + $1 == target { exact = $1 } + $1 > target && !newer { newer = $1 } + + END { + if (exact) { print exact } + else if (newer) { print newer } + else { print older } + } + ') + DOCKERFILE=${DOCKERFILE_PREFIX}${CHOSEN_GCC_VERSION} + DOCKERFILE_TAG=${DOCKERFILE#$BUILDER_SOURCE/Dockerfile.} + echo "Selected builder $DOCKERFILE_TAG for $BUILDER_DISTRO, gcc $BUILDER_GCC_VERSION" + fi + + if [ ! -e $DOCKERFILE ] + then + echo "Cannot find $DOCKERFILE" + return 1 + fi + + IMAGE_NAME="sysdig-probe-builder:${DOCKERFILE_TAG}" + CONTAINER_NAME="sysdig-probe-builder-${DOCKERFILE_TAG}" + + docker ps -q -f "name=$CONTAINER_NAME" | xargs --no-run-if-empty docker rm || return 1 + if [ -n "$BUILDER_IMAGE_PREFIX" ] + then + IMAGE_NAME="${BUILDER_IMAGE_PREFIX}${IMAGE_NAME}" + else + if [ -z "${builders[$IMAGE_NAME]:-}" ] + then + docker build -t "$IMAGE_NAME" -f $DOCKERFILE --pull $BUILDER_SOURCE || return 1 + docker images -q -f 'dangling=true' | xargs --no-run-if-empty docker rmi || true + builders[$IMAGE_NAME]=1 + fi + fi + + docker run --rm -v $DOCKER_BASEDIR:$PROBE_ROOT -v $SYSDIG_DIR:$PROBE_ROOT/sysdig \ + -e OUTPUT=$PROBE_ROOT/output \ + -e PROBE_NAME=$PROBE_NAME \ + -e PROBE_VERSION=$PROBE_VERSION \ + -e PROBE_DEVICE_NAME=$PROBE_DEVICE_NAME \ + -e KERNELDIR=$PROBE_ROOT/${KERNELDIR#$BASEDIR} \ + -e KERNEL_RELEASE=$KERNEL_RELEASE \ + -e HASH=$HASH \ + -e HASH_ORIG=$HASH_ORIG \ + --name $CONTAINER_NAME \ + $IMAGE_NAME || return 1 +} + +function build_probe { + PROBE_ID=$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko + + # Skip Kernel 4.15.0-29 because probe does not build against it + if [ $KERNEL_RELEASE-$HASH = "4.15.0-29-generic-ea0aa038a6b9bdc4bb42152682bba6ce" ]; then + echo "Temporarily skipping $PROBE_ID" + return + fi + + if [ -f $BASEDIR/output/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko ] && + [ -f $BASEDIR/output/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH_ORIG.ko ]; then + echo "Skipping $PROBE_ID (already built)" + return + fi + + LOG=$(mktemp /tmp/log.XXXXXX) + if ! build_probe_impl &> $LOG + then + (echo "Build for $PROBE_ID failed"; cat $LOG) | tee -a $FAIL_LOG + FAILED=1 + else + echo "Build for $PROBE_ID successful" + cat $LOG + fi + rm -f $LOG +} + +function coreos_build { + BUILDER_DISTRO=${BUILDER_DISTRO:-centos} + for IMG in "$@" + do + VERSION=${IMG##*/coreos_developer_container.} + VERSION=${VERSION%.bin.bz2} + VERSION_DIR=build/$DISTRO/$VERSION + if [ ! -e $VERSION_DIR/config_orig ] + then + mkdir -p $VERSION_DIR + echo "Unpacking $(basename $IMG)" + FULL_IMG=$(readlink -f "$IMG") + FULL_VERSION_DIR=$(readlink -f "$VERSION_DIR") + run_toolkit -v "$FULL_IMG:$FULL_IMG:ro" -v "$FULL_VERSION_DIR:$FULL_VERSION_DIR" -- coreos "$FULL_IMG" "$FULL_VERSION_DIR" + + KERNEL_RELEASE=$(cd $VERSION_DIR && ls config-* | sed s/config-//) + export COREOS_BUILD=${VERSION%%.*} + if [ $COREOS_BUILD -gt 890 ] + then + # https://groups.google.com/forum/#!topic/coreos-dev/Z8Q7sIy6YwE + sed -i 's/CONFIG_INITRAMFS_SOURCE=""/CONFIG_INITRAMFS_SOURCE="bootengine.cpio"\nCONFIG_INITRAMFS_ROOT_UID=0\nCONFIG_INITRAMFS_ROOT_GID=0/' $VERSION_DIR/config-* + sed -i 'N;s/\(CONFIG_RD_LZ4=.\)\n\(CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=\)/\1\nCONFIG_INITRAMFS_COMPRESSION=".gz"\n\2/' $VERSION_DIR/config-* + else + mkdir -p build/$DISTRO/vanilla + VANILLA=$(echo $KERNEL_RELEASE | sed s/[-+].*// | sed s/\.0$//) + MAJOR=$(echo $KERNEL_RELEASE | head -c1) + EXTRAVERSION=$(echo $KERNEL_RELEASE | sed s/[^-+]*//) + TGZ_NAME=linux-${VANILLA}.tar.xz + DIR_NAME=linux-${VANILLA} + KERNEL_URL=https://www.kernel.org/pub/linux/kernel/v${MAJOR}.x/$TGZ_NAME + wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} $KERNEL_URL -O build/$DISTRO/vanilla/$TGZ_NAME + if [ ! -e build/$DISTRO/vanilla/$DIR_NAME ] + then + (cd build/$DISTRO/vanilla && tar xf $TGZ_NAME) + fi + fi + fi + + KERNEL_RELEASE=$(cd $VERSION_DIR && ls config-* | sed s/config-//) + COREOS_BUILD=${VERSION%%.*} + HASH_ORIG=$(md5sum $VERSION_DIR/config_orig | cut -d' ' -f1) + HASH=$(md5sum $VERSION_DIR/config-* | cut -d' ' -f1) + echo "Building probe for CoreOS kernel $KERNEL_RELEASE (CoreOS build $COREOS_BUILD)" + if [ -f $BASEDIR/output/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko ] && + [ -f $BASEDIR/output/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH_ORIG.ko ]; then + echo "Skipping $PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko (already built)" + continue + fi + + if [ $COREOS_BUILD -gt 890 ] + then + unset DOCKER_BUILDER + export KERNELDIR=$BASEDIR/$VERSION_DIR/modules/$KERNEL_RELEASE/build + else + VANILLA=$(echo $KERNEL_RELEASE | sed s/[-+].*// | sed s/\.0$//) + EXTRAVERSION=$(echo $KERNEL_RELEASE | sed s/[^-+]*//) + export KERNELDIR=$BASEDIR/build/$DISTRO/vanilla/linux-$VANILLA + make distclean -C $KERNELDIR + sed -i "s/^EXTRAVERSION.*/EXTRAVERSION = $EXTRAVERSION/" $KERNELDIR/Makefile + cp $VERSION_DIR/config_orig $KERNELDIR/.config + DOCKER_BUILDER=coreos-old + fi + build_probe + + done +} + +function ubuntu_build { + BUILDER_DISTRO=${BUILDER_DISTRO:-ubuntu} + for DEB in "$@" + do + KERNEL_RELEASE_FULL=$(echo $DEB | grep -E -o "[0-9]{1}\.[0-9]+\.[0-9]+-[0-9]+\.[0-9][^_]*") # ex. 3.13.0-24.47 + KERNEL_RELEASE=$(echo "$KERNEL_RELEASE_FULL" | grep -Eo '[^-]+-[0-9]+') # ex. 3.13.0-24 + KERNEL_UPDATE=${KERNEL_RELEASE_FULL#$KERNEL_RELEASE.} # ex. 47 + + TARGET=build/$BUILDER_DISTRO/$KERNEL_RELEASE/$KERNEL_UPDATE + MARKER=$TARGET/.$(basename $DEB) + if [ -e $MARKER ] + then + echo "$DEB already unpacked in $TARGET" + else + echo "Unpacking $DEB to $TARGET" + mkdir -p $TARGET + dpkg -x $DEB $TARGET + touch $MARKER + fi + done + + for DEB in "$@" + do + if [[ $(basename $DEB) != "linux-image-"*".deb" ]] + then + continue + fi + KERNEL_RELEASE_FULL=$(echo $DEB | grep -Eo "[0-9]{1}\.[0-9]+\.[0-9]+-[0-9]+\.[0-9][^_]*") # ex. 3.13.0-24.47 + KERNEL_RELEASE=$(echo "$KERNEL_RELEASE_FULL" | grep -Eo '[^-]+-[0-9]+') # ex. 3.13.0-24 + KERNEL_UPDATE=${KERNEL_RELEASE_FULL#$KERNEL_RELEASE.} # ex. 47 + + KERNEL_VERSION=$(echo $DEB | grep -Eo "[0-9]{1}\.[0-9]+\.[0-9]+-[0-9]+-[a-z]+") # ex. 3.13.0-24-generic + + TARGET=build/$BUILDER_DISTRO/$KERNEL_RELEASE/$KERNEL_UPDATE + HASH=$(md5sum $TARGET/boot/config-$KERNEL_VERSION | cut -d' ' -f1) + HASH_ORIG=$HASH + + export KERNELDIR=$BASEDIR/$TARGET/usr/src/linux-headers-$KERNEL_VERSION + + # impedance mismatch + KERNEL_RELEASE=$KERNEL_VERSION + echo "Building probe for Ubuntu kernel $KERNEL_VERSION ($KERNEL_RELEASE_FULL)" + build_probe + done +} + +function rhel_build { + BUILDER_DISTRO=${BUILDER_DISTRO:-centos} + for RPM in "$@" + do + RPM_FILE=$(basename $RPM) + KERNEL_RELEASE=$(echo $RPM_FILE | gawk 'match($0, /[^kernel\-(uek\-)?(core\-|devel\-)?].*[^(\.rpm)]/){ print substr($0, RSTART, RLENGTH) }') + + TARGET=build/$DISTRO/$KERNEL_RELEASE + MARKER=$TARGET/.$RPM_FILE + if [ -e $MARKER ] + then + echo "$RPM already unpacked in $TARGET" + else + echo "Unpacking $RPM to $TARGET" + mkdir -p $TARGET + FULL_RPM=$(readlink -f "$RPM") + FULL_TARGET=$(readlink -f "$TARGET") + run_toolkit -v "$FULL_RPM:$FULL_RPM:ro" -v "$FULL_TARGET:$FULL_TARGET" -- rpm "$FULL_RPM" "$FULL_TARGET" + touch $MARKER + fi + done + + for RPM in "$@" + do + RPM_FILE=$(basename $RPM) + KERNEL_RELEASE=$(echo $RPM_FILE | gawk 'match($0, /[^kernel\-(uek\-)?(core\-|devel\-)?].*[^(\.rpm)]/){ print substr($0, RSTART, RLENGTH) }') + TARGET=build/$DISTRO/$KERNEL_RELEASE + + if [ -f $TARGET/boot/config-$KERNEL_RELEASE ]; then + HASH=$(md5sum $TARGET/boot/config-$KERNEL_RELEASE | cut -d' ' -f1) + else + HASH=$(md5sum $TARGET/lib/modules/$KERNEL_RELEASE/config | cut -d' ' -f1) + fi + HASH_ORIG=$HASH + export KERNELDIR=$BASEDIR/$TARGET/usr/src/kernels/$KERNEL_RELEASE + echo "Building probe for CentOS kernel $KERNEL_RELEASE" + build_probe + done +} + +function debian_build { + BUILDER_DISTRO=${BUILDER_DISTRO:-debian} + for DEB in "$@" + do + DEB_FILE=$(basename $DEB) + if [[ "$DEB_FILE" = *"kbuild"* ]] + then + continue + fi + KERNEL_RELEASE=$(echo ${DEB_FILE} | grep -E -o "[0-9]{1}\.[0-9]+\.[0-9]+(-[0-9]+)?"| head -1) + KERNEL_MAJOR=$(echo ${KERNEL_RELEASE} | grep -E -o "[0-9]{1}\.[0-9]+") + PACKAGE=$(echo ${DEB_FILE} | grep -E -o "(common_[0-9]{1}\.[0-9]+.*(amd64|all)|amd64_[0-9]{1}\.[0-9]+.*amd64)" | sed -E 's/(common_|amd64_|_amd64|all_|_all)//g') + + TARGET=build/$BUILDER_DISTRO/$KERNEL_RELEASE + MARKER=$TARGET/.$(basename $DEB) + if [ -e $MARKER ] + then + echo "$DEB already unpacked in $TARGET" + else + echo "Unpacking $DEB to $TARGET" + mkdir -p $TARGET + dpkg -x $DEB $TARGET + touch $MARKER + fi + set +e + KBUILD_PACKAGE=$(ls $DISTRO/linux-kbuild-${KERNEL_MAJOR}_*.deb 2>/dev/null | tail -1) + set -e + if [ -n "$KBUILD_PACKAGE" ] + then + MARKER=$TARGET/.$(basename $KBUILD_PACKAGE) + if [ -e $MARKER ] + then + echo "$KBUILD_PACKAGE already unpacked in $TARGET" + else + echo "Unpacking $KBUILD_PACKAGE to $TARGET" + mkdir -p $TARGET + dpkg -x $KBUILD_PACKAGE $TARGET + touch $MARKER + fi + fi + done + + for DEB in "$@" + do + DEB_FILE=$(basename $DEB) + if [[ "$DEB_FILE" != "linux-image-"*".deb" ]] + then + continue + fi + KERNEL_RELEASE=$(echo ${DEB_FILE} | grep -E -o "[0-9]{1}\.[0-9]+\.[0-9]+(-[0-9]+)?"| head -1) + KERNEL_ARCH_RELEASE=$(echo ${DEB_FILE} | grep -E -o "[0-9]{1}\.[0-9]+\.[0-9]+(-[0-9]+)?-amd64"| head -1) + KERNEL_COMMON_RELEASE=${KERNEL_ARCH_RELEASE/amd64/common} + + TARGET=build/$BUILDER_DISTRO/$KERNEL_RELEASE + HASH=$(md5sum $TARGET/boot/config-$KERNEL_ARCH_RELEASE | cut -d' ' -f1) + HASH_ORIG=$HASH + + export KERNELDIR=$BASEDIR/$TARGET/usr/src/linux-headers-$KERNEL_ARCH_RELEASE + export KERNELDIR_COMMON=$BASEDIR/$TARGET/usr/src/linux-headers-$KERNEL_COMMON_RELEASE + + CONTAINER_KERNELDIR=/build/probe/$TARGET/usr/src/linux-headers-$KERNEL_ARCH_RELEASE + CONTAINER_KERNELDIR_COMMON=/build/probe/$TARGET/usr/src/linux-headers-$KERNEL_COMMON_RELEASE + + BUILD_LINK=$(readlink $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/build) + case $BUILD_LINK in + /*) + BUILD_LINK=../../../${BUILD_LINK#/} + ls -l $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/build + ln -sf $BUILD_LINK $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/build + ;; + esac + ls -l $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/build + + SOURCE_LINK=$(readlink $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/source) + case $SOURCE_LINK in + /*) + SOURCE_LINK=../../../${SOURCE_LINK#/} + ls -l $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/source + ln -sf $SOURCE_LINK $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/source + ;; + esac + ls -l $BASEDIR/$TARGET/lib/modules/$KERNEL_ARCH_RELEASE/source + + MAKEFILE=$BASEDIR/$TARGET/usr/src/linux-headers-$KERNEL_ARCH_RELEASE/Makefile + + if [ ! -e $MAKEFILE.sysdig-orig ] + then + cp $MAKEFILE $MAKEFILE.sysdig-orig + cat $MAKEFILE.sysdig-orig | \ + sed '0,/MAKEARGS.*$/s||MAKEARGS := -C '"${CONTAINER_KERNELDIR_COMMON}"' O='"${CONTAINER_KERNELDIR}"'|' | \ + sed 's/@://' | \ + sed 's|$(cmd) %.*$|$(cmd) : all|' > $MAKEFILE + fi + + # impedance mismatch + KERNEL_RELEASE=$KERNEL_ARCH_RELEASE + echo "Building probe for Debian kernel $KERNEL_ARCH_RELEASE" + build_probe + done +} + +function build_group_ubuntu { + # + # Ubuntu build + # + + DISTRO=ubuntu + BUILDER_DISTRO=ubuntu + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading Ubuntu kernels + mkdir -p $DISTRO + $BUILDER_SOURCE/kernel-crawler.py Ubuntu | xargs -n 1 -P $DOWNLOAD_CONCURRENCY wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + + echo "Building Ubuntu" + ubuntu_build $DISTRO/*.deb +} + +function build_group_centos { + # + # RHEL build through CentOS + # + + DISTRO=centos + BUILDER_DISTRO=centos + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading CentOS kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/kernel-crawler.py CentOS | xargs -n 1 -P $DOWNLOAD_CONCURRENCY wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + + echo Building CentOS + rhel_build $DISTRO/*.rpm +} + +function build_group_fedora { + # + # Fedora build + # + + DISTRO=fedora + BUILDER_DISTRO=centos + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading Fedora kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/kernel-crawler.py Fedora | xargs -n 1 -P $DOWNLOAD_CONCURRENCY wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + + echo Building Fedora + rhel_build $DISTRO/*.rpm +} + +function build_group_fedora_atomic { + # + # Fedora Atomic build + # + + DISTRO=fedora-atomic + BUILDER_DISTRO=centos + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading Fedora Atomic kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/kernel-crawler.py Fedora-Atomic | xargs -n 1 -P $DOWNLOAD_CONCURRENCY wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + + echo Building Fedora Atomic + rhel_build $DISTRO/*.rpm +} + +function build_group_coreos { + # + # CoreOS build + # + + DISTRO=coreos + BUILDER_DISTRO=centos + + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading CoreOS kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/kernel-crawler.py CoreOS | while read URL + do + COREOS_VERSION=$(curl -fsSL ${URL}version.txt | grep -Po '(?<=^COREOS_VERSION=).*') + wget -O $DISTRO/coreos_developer_container.$COREOS_VERSION.bin.bz2 -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} ${URL}coreos_developer_container.bin.bz2 + done + fi + + echo Building CoreOS + coreos_build $DISTRO/*.bin.bz2 +} + +function build_group_debian { + # + # Debian build + # + DISTRO=debian + BUILDER_DISTRO=debian + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading Debian kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/kernel-crawler.py Debian | xargs -n 1 -P $DOWNLOAD_CONCURRENCY wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + + echo "Building Debian" + debian_build $DISTRO/*.deb +} + +function build_group_oracle_rhck { + # + # Oracle RHCK build + # + DISTRO=oracle-rhck + BUILDER_DISTRO=centos + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading Oracle RHCK kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/oracle-kernel-crawler.py Oracle-RHCK | xargs -n 1 wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + echo "Building Oracle RHCK" + rhel_build $DISTRO/*.rpm +} + +function build_group_oracle_ol6_uek { + # + # Oracle Linux 6 UEK build + # + DISTRO=oracle-uek6 + BUILDER_DISTRO=oracle-uek6 + DOCKER_BUILDER=ol6 + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading Oracle Linux 6 UEK kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/oracle-kernel-crawler.py OL6-UEK | xargs -n 1 wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + echo "Building Oracle Linux 6 UEK" + rhel_build $DISTRO/*.rpm + unset DOCKER_BUILDER +} + +function build_group_oracle_ol7_uek { + # + # Oracle Linux 7 UEK build + # + DISTRO=oracle-uek7 + BUILDER_DISTRO=oracle-uek7 + DOCKER_BUILDER=ol7 + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading Oracle Linux 7 UEK kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/oracle-kernel-crawler.py OL7-UEK | xargs -n 1 wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + echo "Building Oracle Linux 7 UEK" + rhel_build $DISTRO/*.rpm + unset DOCKER_BUILDER +} + +function build_group_amazonlinux { + # + # AmazonLinux build + # + DISTRO=amazonlinux + BUILDER_DISTRO=centos + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading AmazonLinux kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/kernel-crawler.py AmazonLinux | xargs -n 1 -P $DOWNLOAD_CONCURRENCY wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + echo "Building AmazonLinux" + rhel_build $DISTRO/*.rpm +} + +function build_group_amazonlinux2 { + # + # AmazonLinux build + # + DISTRO=amazonlinux2 + BUILDER_DISTRO=centos + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading AmazonLinux2 kernels + DIR=$(dirname $(readlink -f $0)) + mkdir -p $DISTRO + $DIR/kernel-crawler.py AmazonLinux2 | xargs -n 1 -P $DOWNLOAD_CONCURRENCY wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO + fi + echo "Building AmazonLinux2" + rhel_build $DISTRO/*.rpm +} + +function build_group_rhel { + if [ -z "$ARTIFACTORY_KEY" -o -z "$ARTIFACTORY_SERVER" ] + then + echo "Artifactory credentials missing, skipping RHEL build" + return + fi + + # + # RHEL build from Stored Sources + # + DISTRO=rhel + BUILDER_DISTRO=centos + if [ "$DOWNLOAD_CONCURRENCY" != "0" ] + then + echo Downloading RHEL kernels + mkdir -p $DISTRO + + # We're using aql to query for all of the objects in the repo + # and jq to parse the json to get the rpms + curl -s -H 'Content-Type: text/plain' -H "X-JFrog-Art-Api: $ARTIFACTORY_KEY" -X POST \ + -d 'items.find({"repo":"redhat-sources"})' https://$ARTIFACTORY_SERVER/artifactory/api/search/aql | \ + jq -r '.results[].name as $o | $o | select(endswith(".rpm")) | $o' | \ + xargs -n 1 -P $DOWNLOAD_CONCURRENCY -iRPM wget -c -nv --timeout=${DOWNLOAD_TIMEOUT} --tries=${RETRIES} -P $DISTRO https://$ARTIFACTORY_SERVER/artifactory/redhat-sources/RPM --header "X-JFrog-Art-Api: $ARTIFACTORY_KEY" + fi + + echo Building RHEL from Stored Sources + rhel_build $DISTRO/*.rpm +} + +function build_everything { + build_group_rhel + build_group_ubuntu + build_group_centos + build_group_fedora + build_group_fedora_atomic + build_group_coreos + build_group_debian + build_group_oracle_rhck + build_group_oracle_ol6_uek + build_group_oracle_ol7_uek + build_group_amazonlinux + build_group_amazonlinux2 +} + +case "$KERNEL_TYPE" in + "") + checkout_sysdig + build_everything + ;; + Ubuntu) + checkout_sysdig + build_group_ubuntu + ;; + CentOS) + checkout_sysdig + build_group_centos + ;; + Fedora) + checkout_sysdig + build_group_fedora + ;; + Fedora-Atomic) + checkout_sysdig + build_group_fedora_atomic + ;; + CoreOS) + checkout_sysdig + build_group_coreos + ;; + Debian) + checkout_sysdig + build_group_debian + ;; + OracleRHCK) + checkout_sysdig + build_group_oracle_rhck + ;; + OracleOL6) + checkout_sysdig + build_group_oracle_ol6_uek + ;; + OracleOL7) + checkout_sysdig + build_group_oracle_ol7_uek + ;; + AmazonLinux) + checkout_sysdig + build_group_amazonlinux + ;; + AmazonLinux2) + checkout_sysdig + build_group_amazonlinux2 + ;; + RHEL) + checkout_sysdig + build_group_rhel + ;; + CustomCentOS) + checkout_sysdig + DISTRO=custom-centos + rhel_build "$@" + ;; + CustomCoreOS) + checkout_sysdig + DISTRO=custom-coreos + coreos_build "$@" + ;; + CustomDebian) + checkout_sysdig + DISTRO=custom-debian + debian_build "$@" + ;; + CustomUbuntu) + checkout_sysdig + DISTRO=custom-ubuntu + ubuntu_build "$@" + ;; + *) +# Note: if you add a new KERNEL_TYPE here, please remember to update +# the usage message near the top of this file + exit 1 + ;; +esac + +if [ -s "$FAIL_LOG" ] +then + echo "Failed builds:" + echo "------------------------------" + cat $FAIL_LOG +fi +rm -f $FAIL_LOG +if [ -n "$FAILED" ] +then + echo "Build failed." + exit 1 +else + echo "Success." +fi + +# vim: :set tabstop=8 shiftwidth=8 noexpandtab: diff --git a/probe-builder/builder-entrypoint-coreos.sh b/probe-builder/builder-entrypoint-coreos.sh new file mode 100755 index 0000000000..578320e8f1 --- /dev/null +++ b/probe-builder/builder-entrypoint-coreos.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# required env vars: +# HASH +# HASH_ORIG +# KERNELDIR +# KERNEL_RELEASE +# OUTPUT +# PROBE_DEVICE_NAME +# PROBE_NAME +# PROBE_VERSION + +set -euo pipefail + +ARCH=$(uname -m) + +if [[ -f "${KERNELDIR}/scripts/gcc-plugins/stackleak_plugin.so" ]]; then + echo "Rebuilding gcc plugins for ${KERNELDIR}" + (cd "${KERNELDIR}" && make gcc-plugins) +fi + +(cd $KERNELDIR && make modules_prepare) + +echo Building $PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko + +mkdir -p /build/sysdig +cd /build/sysdig + +cmake -DCMAKE_BUILD_TYPE=Release -DPROBE_NAME=$PROBE_NAME -DPROBE_VERSION=$PROBE_VERSION -DPROBE_DEVICE_NAME=$PROBE_DEVICE_NAME -DCREATE_TEST_TARGETS=OFF /build/probe/sysdig +make driver +strip -g driver/$PROBE_NAME.ko + +KO_VERSION=$(/sbin/modinfo driver/$PROBE_NAME.ko | grep vermagic | tr -s " " | cut -d " " -f 2) +if [ "$KO_VERSION" != "$KERNEL_RELEASE" ]; then + echo "Corrupted probe, KO_VERSION " $KO_VERSION ", KERNEL_RELEASE " $KERNEL_RELEASE + exit 1 +fi + +cp driver/$PROBE_NAME.ko $OUTPUT/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko +cp driver/$PROBE_NAME.ko $OUTPUT/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH_ORIG.ko + diff --git a/probe-builder/builder-entrypoint.sh b/probe-builder/builder-entrypoint.sh new file mode 100755 index 0000000000..3638fff1be --- /dev/null +++ b/probe-builder/builder-entrypoint.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# required env vars: +# HASH +# HASH_ORIG +# KERNELDIR +# KERNEL_RELEASE +# OUTPUT +# PROBE_DEVICE_NAME +# PROBE_NAME +# PROBE_VERSION + +set -euo pipefail + +ARCH=$(uname -m) + +if [[ -f "${KERNELDIR}/scripts/gcc-plugins/stackleak_plugin.so" ]]; then + echo "Rebuilding gcc plugins for ${KERNELDIR}" + (cd "${KERNELDIR}" && make gcc-plugins) +fi + +echo Building $PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko + +mkdir -p /build/sysdig +cd /build/sysdig + +cmake -DCMAKE_BUILD_TYPE=Release -DPROBE_NAME=$PROBE_NAME -DPROBE_VERSION=$PROBE_VERSION -DPROBE_DEVICE_NAME=$PROBE_DEVICE_NAME -DCREATE_TEST_TARGETS=OFF /build/probe/sysdig +make driver +strip -g driver/$PROBE_NAME.ko + +KO_VERSION=$(/sbin/modinfo driver/$PROBE_NAME.ko | grep vermagic | tr -s " " | cut -d " " -f 2) +if [ "$KO_VERSION" != "$KERNEL_RELEASE" ]; then + echo "Corrupted probe, KO_VERSION " $KO_VERSION ", KERNEL_RELEASE " $KERNEL_RELEASE + exit 1 +fi + +cp driver/$PROBE_NAME.ko $OUTPUT/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH.ko +cp driver/$PROBE_NAME.ko $OUTPUT/$PROBE_NAME-$PROBE_VERSION-$ARCH-$KERNEL_RELEASE-$HASH_ORIG.ko diff --git a/probe-builder/kernel-crawler.py b/probe-builder/kernel-crawler.py new file mode 100755 index 0000000000..1b7ad2fecf --- /dev/null +++ b/probe-builder/kernel-crawler.py @@ -0,0 +1,455 @@ +#!/usr/bin/env python +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +# Author: Samuele Pilleri +# Date: August 17th, 2015 + +from __future__ import unicode_literals + +import bz2 +import re +import sqlite3 +import sys +import tempfile +import time +import zlib + +try: + from urllib2 import urlopen, unquote + + # python 2 + def sqlite_column(row, col): + return row[bytes(col)] + +except ImportError: + from urllib.request import urlopen + from urllib.parse import unquote + + # python 3 + def sqlite_column(row, col): + return row[col] + +from lxml import html + +# +# This is the main configuration tree for easily analyze Linux repositories +# hunting packages. When adding repos or so be sure to respect the same data +# structure +# +repos = { + "CentOS" : [ + { + # This is the root path of the repository in which the script will + # look for distros (HTML page) + "root" : "http://mirrors.edge.kernel.org/centos/", + + # This is the XPath + Regex (optional) for analyzing the `root` + # page and discover possible distro versions. Use the regex if you + # want to limit the version release + "discovery_pattern" : "/html/body//pre/a[regex:test(@href, '^6|^7.*$')]/@href", + + # Once we have found every version available, we need to know were + # to go inside the tree to find packages we need (HTML pages) + "subdirs" : [ + "os/x86_64/Packages/", + "updates/x86_64/Packages/" + ], + + # Finally, we need to inspect every page for packages we need. + # Again, this is a XPath + Regex query so use the regex if you want + # to limit the number of packages reported. + "page_pattern" : "/html/body//a[regex:test(@href, '^kernel-(devel-)?[0-9].*\.rpm$')]/@href" + }, + + { + "root" : "http://vault.centos.org/", + "discovery_pattern" : "//body//table/tr/td/a[regex:test(@href, '^6|^7.*$')]/@href", + "subdirs" : [ + "os/x86_64/Packages/", + "updates/x86_64/Packages/" + ], + "page_pattern" : "//body//table/tr/td/a[regex:test(@href, '^kernel-(devel-)?[0-9].*\.rpm$')]/@href" + } + ], + + "Ubuntu" : [ + { + # Had to split the URL because, unlikely other repos for which the + # script was first created, Ubuntu puts everything into a single + # folder. The real URL is be: + # http://mirrors.us.kernel.org/ubuntu/pool/main/l/linux/ + "root" : "https://mirrors.edge.kernel.org/ubuntu/pool/main/l/", + "discovery_pattern" : "/html/body//a[@href = 'linux/']/@href", + "subdirs" : [""], + "page_pattern" : "/html/body//a[regex:test(@href, '^linux-(image|headers)-(unsigned-)*[3-9].*-generic.*amd64.deb$')]/@href" + }, + + { + "root" : "https://mirrors.edge.kernel.org/ubuntu/pool/main/l/", + "discovery_pattern" : "/html/body//a[@href = 'linux/']/@href", + "subdirs" : [""], + "page_pattern" : "/html/body//a[regex:test(@href, '^linux-headers-[3-9].*_all.deb$')]/@href" + }, + + { + "root" : "http://security.ubuntu.com/ubuntu/pool/main/l/", + "discovery_pattern" : "/html/body//a[@href = 'linux/']/@href", + "subdirs" : [""], + "page_pattern" : "/html/body//a[regex:test(@href, '^linux-(image|headers)-(unsigned-)*[3-9].*-generic.*amd64.deb$')]/@href" + }, + + { + "root" : "http://security.ubuntu.com/ubuntu/pool/main/l/", + "discovery_pattern" : "/html/body//a[@href = 'linux/']/@href", + "subdirs" : [""], + "page_pattern" : "/html/body//a[regex:test(@href, '^linux-headers-[3-9].*_all.deb$')]/@href" + }, + + { + "root" : "http://security.ubuntu.com/ubuntu/pool/main/l/", + "discovery_pattern" : "/html/body//a[@href = 'linux/']/@href", + "subdirs" : [""], + "page_pattern" : "/html/body//a[regex:test(@href, '^linux-modules-[3-9].*-generic.*amd64.deb$')]/@href" + }, + + ### Ubuntu AWS kernels + { + "root" : "https://mirrors.edge.kernel.org/ubuntu/pool/main/l/", + "discovery_pattern" : "/html/body//a[regex:test(@href, 'linux-aws.*/')]/@href", + "subdirs" : [""], + "page_pattern" : "/html/body//a[regex:test(@href, '^linux-(image|(aws-.*)?headers|modules)-[3-9].*(all|amd64).deb$')]/@href" + }, + + { + "root" : "http://security.ubuntu.com/ubuntu/pool/main/l/", + "discovery_pattern" : "/html/body//a[regex:test(@href, 'linux-aws.*/')]/@href", + "subdirs" : [""], + "page_pattern" : "/html/body//a[regex:test(@href, '^linux-(image|(aws-.*)?headers|modules)-[3-9].*(all|amd64).deb$')]/@href" + }, + ], + + "Fedora" : [ + { + "root" : "https://mirrors.edge.kernel.org/fedora/releases/", + "discovery_pattern": "/html/body//a[regex:test(@href, '^[3-9][0-9]/$')]/@href", + "subdirs" : [ + "Everything/x86_64/os/Packages/k/" + ], + "page_pattern" : "/html/body//a[regex:test(@href, '^kernel-(core|devel)-[0-9].*\.rpm$')]/@href" + }, + + { + "root" : "https://mirrors.edge.kernel.org/fedora/updates/", + "discovery_pattern": "/html/body//a[regex:test(@href, '^[3-9][0-9]/$')]/@href", + "subdirs" : [ + "x86_64/Packages/k/" + ], + "page_pattern" : "/html/body//a[regex:test(@href, '^kernel-(core|devel)-[0-9].*\.rpm$')]/@href" + }, + + { + "root" : "https://mirrors.edge.kernel.org/fedora/updates/", + "discovery_pattern": "/html/body//a[regex:test(@href, '^[3-9][0-9]/$')]/@href", + "subdirs" : [ + "Everything/x86_64/Packages/k/" + ], + "page_pattern" : "/html/body//a[regex:test(@href, '^kernel-(core|devel)-[0-9].*\.rpm$')]/@href" + }, + + # { + # "root" : "https://mirrors.edge.kernel.org/fedora/development/", + # "discovery_pattern": "/html/body//a[regex:test(@href, '^2[2-9]/$')]/@href", + # "subdirs" : [ + # "x86_64/os/Packages/k/" + # ], + # "page_pattern" : "/html/body//a[regex:test(@href, '^kernel-(core|devel)-[0-9].*\.rpm$')]/@href" + # } + ], + + # + # Fedora Atomic repo is hard-coded to get the 4.17.x and 4.18.x (excluding rc) for now. + # + "Fedora-Atomic" : [ + { + "root" : "https://kojipkgs.fedoraproject.org/packages/kernel/", + "version_discovery_pattern": "/html/body//a[regex:test(@href, '^4\.1[78].*/$')]/@href", + "build_discovery_pattern": "/html/body//a[regex:test(@href, '^[0-9]+\.[^r].*/$')]/@href", + "subdirs" : [ + "x86_64/" + ], + "page_pattern" : "/html/body//a[regex:test(@href, '^kernel-(core|devel)-[0-9].*\.rpm$')]/@href", + }, + ], + + "CoreOS" : [ + { + "root" : "http://alpha.release.core-os.net/", + "discovery_pattern": "/html/body//a[regex:test(@href, 'amd64-usr')]/@href", + "subdirs" : [ + "" + ], + "page_pattern" : "/html/body//a[regex:test(@href, '^[5-9][0-9][0-9]|current|[1][0-9]{3}')]/@href", + "exclude_patterns": ["^15\d\d\."] + }, + + { + "root" : "http://beta.release.core-os.net/", + "discovery_pattern": "/html/body//a[regex:test(@href, 'amd64-usr')]/@href", + "subdirs" : [ + "" + ], + "page_pattern" : "/html/body//a[regex:test(@href, '^[5-9][0-9][0-9]|current|[1][0-9]{3}')]/@href" + }, + + { + "root" : "http://stable.release.core-os.net/", + "discovery_pattern": "/html/body//a[regex:test(@href, 'amd64-usr')]/@href", + "subdirs" : [ + "" + ], + "page_pattern" : "/html/body//a[regex:test(@href, '^[4-9][0-9][0-9]|current|[1][0-9]{3}')]/@href" + } + ], + + "Debian": [ + { + "root": "https://mirrors.edge.kernel.org/debian/pool/main/l/", + "discovery_pattern": "/html/body/pre/a[@href = 'linux/']/@href", + "subdirs": [""], + "page_pattern": "/html/body//a[regex:test(@href, '^linux-(image|headers)-[3-9]\.[0-9]+\.[0-9]+.*amd64.deb$')]/@href", + "exclude_patterns": ["-rt", "dbg", "trunk", "all", "exp", "unsigned", "cloud-amd64"] + }, + { + "root": "http://security.debian.org/pool/updates/main/l/", + "discovery_pattern": "/html/body/table//tr/td/a[@href = 'linux/']/@href", + "subdirs": [""], + "page_pattern": "/html/body//a[regex:test(@href, '^linux-(image|headers)-[3-9]\.[0-9]+\.[0-9]+.*amd64.deb$')]/@href", + "exclude_patterns": ["-rt", "dbg", "trunk", "all", "exp", "unsigned", "cloud-amd64"] + }, + { + "root": "https://mirrors.edge.kernel.org/debian/pool/main/l/", + "discovery_pattern": "/html/body/pre/a[@href = 'linux/']/@href", + "subdirs": [""], + "page_pattern": "/html/body//a[regex:test(@href, '^linux-headers-[3-9]\.[0-9]+\.[0-9]+.*-common_.*.all\.deb$')]/@href", + "exclude_patterns": ["-rt", "dbg", "trunk", "exp", "unsigned", "cloud-amd64"] + }, + { + "root": "http://security.debian.org/pool/updates/main/l/", + "discovery_pattern": "/html/body/table//tr/td/a[@href = 'linux/']/@href", + "subdirs": [""], + "page_pattern": "/html/body//a[regex:test(@href, '^linux-headers-[3-9]\.[0-9]+\.[0-9]+.*-common_.*.all\.deb$')]/@href", + "exclude_patterns": ["-rt", "dbg", "trunk", "exp", "unsigned", "cloud-amd64"] + }, + { + "root": "http://mirrors.edge.kernel.org/debian/pool/main/l/", + "discovery_pattern": "/html/body/pre/a[@href = 'linux/']/@href", + "subdirs": [""], + "page_pattern": "/html/body//a[regex:test(@href, '^linux-kbuild-.*amd64.deb$')]/@href", + "exclude_patterns": ["-rt", "dbg", "trunk", "all", "exp", "unsigned", "cloud-amd64"] + }, + { + "root": "http://mirrors.edge.kernel.org/debian/pool/main/l/", + "discovery_pattern": "/html/body/pre/a[@href = 'linux-tools/']/@href", + "subdirs": [""], + "page_pattern": "/html/body//a[regex:test(@href, '^linux-kbuild-.*amd64.deb$')]/@href", + "exclude_patterns": ["-rt", "dbg", "trunk", "all", "exp", "unsigned", "cloud-amd64"] + } + ] +} + +# Build static list, check here for the last Amazon Linux AMI release: https://aws.amazon.com/amazon-linux-2/faqs/ +amazon_linux_builder = [('latest', 'updates'), ('latest', 'main'), ('2017.03', 'updates'), ('2017.03', 'main'), ('2017.09', 'updates'), ('2017.09', 'main'), ('2018.03', 'updates'), ('2018.03', 'main')] +amazon_repos = [] +for repo_release, release_type in amazon_linux_builder: + amazon_repos.append({ + "root": "http://repo.us-east-1.amazonaws.com/" + repo_release + "/" + release_type + "/mirror.list", + "discovery_pattern": "SELECT * FROM packages WHERE name LIKE 'kernel%'", + "subdirs": [""], + "page_pattern": "", + "exclude_patterns": ["doc", "tools", "headers"] + }) +repos['AmazonLinux'] = amazon_repos + +amazon_linux_2 = ['2.0', 'latest'] +amazon_linux2 = [] +for amzn_repos in amazon_linux_2: + amazon_linux2.append({ + "root": "http://amazonlinux.us-east-1.amazonaws.com/2/core/" + amzn_repos + "/x86_64/mirror.list", + "discovery_pattern": "SELECT * FROM packages WHERE name LIKE 'kernel%' AND name NOT LIKE 'kernel-livepatch%'", + "subdirs": [""], + "page_pattern": "", + "exclude_patterns": ["doc", "tools", "headers"] + }) + +repos['AmazonLinux2'] = amazon_linux2 + +def progress(distro, current, total, package): + sys.stderr.write('\r{} {}/{} {} '.format(distro, current, total, package)) + +def exclude_patterns(repo, packages, base_url, urls): + for rpm in packages: + if "exclude_patterns" in repo and any(re.search(x, rpm) for x in repo["exclude_patterns"]): + continue + else: + urls.add(base_url + str(unquote(rpm))) + +def process_al_distro(al_distro_name, current_repo): + get_url = urlopen(current_repo["root"]).readline().decode('ascii') + if get_url: + if al_distro_name == "AmazonLinux": + base_mirror_url = get_url.replace('$basearch','x86_64').replace('\n','') + '/' + db_path = "repodata/primary.sqlite.bz2" + elif al_distro_name == "AmazonLinux2": + base_mirror_url = get_url.replace('\n','') + '/' + db_path = "repodata/primary.sqlite.gz" + + progress(current_repo["root"], 1, 3, 'downloading ' + db_path) + response = urlopen(base_mirror_url + db_path) + body = response.read() + + progress(current_repo["root"], 2, 3, 'decompressing') + if al_distro_name == "AmazonLinux": + decompressed_data = bz2.decompress(body) + elif al_distro_name == "AmazonLinux2": + decompressed_data = zlib.decompress(body, 16+zlib.MAX_WBITS) + + progress(current_repo["root"], 3, 3, 'querying') + db_file = tempfile.NamedTemporaryFile() + db_file.write(decompressed_data) + conn = sqlite3.connect(db_file.name) + conn.row_factory = sqlite3.Row + c = conn.cursor() + al_rpms = [sqlite_column(r, "location_href") for r in c.execute(current_repo["discovery_pattern"])] + exclude_patterns(current_repo, al_rpms, base_mirror_url, urls) + conn.close() + db_file.close() + + sys.stderr.write('\n') + return True + + else: + return False + +# +# Fedora Atomic needs 2 levels of discovery(for version, and build id, respectively) +# +def process_atomic_distro(current_repos): + for repo in current_repos["Fedora-Atomic"]: + try: + root = urlopen(repo["root"],timeout=URL_TIMEOUT).read() + except: + continue + versions = html.fromstring(root).xpath(repo["version_discovery_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + vid = 0 + for version in versions: + vid += 1 + version_url=repo["root"] + version + try: + progress(repo["root"], vid, len(versions), version) + version_page=urlopen(version_url,timeout=URL_TIMEOUT).read() + except: + continue + builds = html.fromstring(version_page).xpath(repo["build_discovery_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + for build in builds: + for subdir in repo["subdirs"]: + source = version_url + build + subdir + try: + page = urlopen(source,timeout=URL_TIMEOUT).read() + except: + continue + rpms = html.fromstring(page).xpath(repo["page_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + exclude_patterns(repo, rpms, source, urls) + sys.stderr.write('\n') + + +# +# In our design you are not supposed to modify the code. The whole script is +# created so that you just have to add entry to the `repos` array and new +# links will be found automagically without needing to write any single line of +# code. +# +urls = set() +URL_TIMEOUT=30 + +if len(sys.argv) < 2 or not sys.argv[1] in repos: + sys.stderr.write("Usage: " + sys.argv[0] + " \n") + sys.stderr.write("Available distros:\n") + for d in sorted(repos): + sys.stderr.write(" - {}\n".format(d)) + sys.exit(1) + +distro = sys.argv[1] + +# +# Navigate the `repos` tree and look for packages we need that match the +# patterns given. Save the result in `packages`. +# + +al2_repo_count = 0 + +for repo in repos[distro]: + if distro == 'AmazonLinux': + try: + process_al_distro(distro, repo) + except: + continue + elif distro == 'AmazonLinux2': + try: + # Brute force finding the repositories and only grab the most recent two, then skip the rest. + if al2_repo_count < 2: + if process_al_distro(distro, repo): + al2_repo_count += 1 + except: + continue + elif distro == "Fedora-Atomic": + try: + process_atomic_distro(repos) + except: + continue + else: + try: + root = urlopen(repo["root"],timeout=URL_TIMEOUT).read() + except: + continue + + versions = html.fromstring(root).xpath(repo["discovery_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + current = 1 + total = len(versions) * len(repo["subdirs"]) + for version in versions: + for subdir in repo["subdirs"]: + # The try - except block is used because 404 errors and similar + # might happen (and actually happen because not all repos have + # packages we need) + try: + source = repo["root"] + version + subdir + progress(repo["root"], current, total, version + subdir) + current += 1 + page = urlopen(source,timeout=URL_TIMEOUT).read() + rpms = html.fromstring(page).xpath(repo["page_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + exclude_patterns(repo, rpms, source, urls) + except: + continue + sys.stderr.write('\n') + + +# +# Print URLs to stdout +# +for url in urls: + print(url) diff --git a/probe-builder/main-builder-entrypoint.sh b/probe-builder/main-builder-entrypoint.sh new file mode 100755 index 0000000000..fbd1b6874f --- /dev/null +++ b/probe-builder/main-builder-entrypoint.sh @@ -0,0 +1,120 @@ +#!/bin/bash + +set -euo pipefail + +usage() +{ + cat >&2 <&2 + echo >&2 + usage + fi + echo "$SOURCE" +} + +build_probes() +{ + WORKSPACE_SRC=$(get_host_mount /workspace) + SYSDIG_SRC=$(get_host_mount /sysdig) + KERNELS_SRC=$(get_host_mount /kernels) + + cd /workspace + /builder/build-probe-binaries -B $WORKSPACE_SRC -s $SYSDIG_SRC -b "$BUILDER_IMAGE_PREFIX" "$@" /kernels/* +} + +prepare_builders() +{ + for i in Dockerfile.* ; do docker build -t ${BUILDER_IMAGE_PREFIX}sysdig-probe-builder:${i#Dockerfile.} -f $i . ; done +} + +if ! docker info &>/dev/null +then + echo "Docker socket not available" >&2 + echo >&2 + usage +fi + +BUILDER_IMAGE_PREFIX= +while getopts ":b:BP" opt +do + case "$opt" in + b) + BUILDER_IMAGE_PREFIX=$OPTARG + ;; + B) + OP=build + ;; + P) + OP=prepare + ;; + \?) + echo "Invalid option $OPTARG" >&2 + echo "Did you mean to pass it to the probe builder? Add -- before" >&2 + echo >&2 + usage + ;; + :) + echo "Option $OPTARG requires an argument" >&2 + echo >&2 + usage + ;; + esac +done + +shift $((OPTIND - 1)) + +case "${OP:-}" in + build) + build_probes "$@" + ;; + prepare) + prepare_builders + ;; + *) + usage + ;; +esac diff --git a/probe-builder/oracle-kernel-crawler.py b/probe-builder/oracle-kernel-crawler.py new file mode 100755 index 0000000000..17edb49207 --- /dev/null +++ b/probe-builder/oracle-kernel-crawler.py @@ -0,0 +1,121 @@ +#!/usr/bin/python +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +import sys + +try: + from urllib2 import urlopen, unquote +except ImportError: + from urllib.request import urlopen + from urllib.parse import unquote + +from lxml import html + +# +# Copied from kernel-crawler.py and hacked up for oracle linux +# because they don't use a normal directory structure. +# +repos = { + # Oracle only puts full isos with unhelpful names on mirrors.kernel.org, so skip it + "OL6-UEK": [ + { + # yum.oracle.com has a bad cert, so use http instead of https + "root": "http://yum.oracle.com/", + "discovery_pattern": "/html/body//h3/a[regex:test(@href, 'oracle-linux-6\.html')]/@href", + "sub_discovery_pattern": "/html/body//h3[regex:test(., '^UEK Release [3-9]:')]/a[regex:test(@href, 'x86_64/index.html')]/@href", + "page_pattern": "/html/body//a[regex:test(@href, '^getPackage/kernel-uek-(devel-)?[0-9].*\.rpm$')]/@href", + } + ], + + "OL7-UEK": [ + { + "root": "http://yum.oracle.com/", + "discovery_pattern": "/html/body//h3/a[regex:test(@href, 'oracle-linux-7\.html')]/@href", + "sub_discovery_pattern": "/html/body//h3[regex:test(., '^UEK Release [3-9]:')]/a[regex:test(@href, 'x86_64/index.html')]/@href", + "page_pattern": "/html/body//a[regex:test(@href, '^getPackage/kernel-uek-(devel-)?[0-9].*\.rpm$')]/@href", + } + ], + + "Oracle-RHCK": [ + { + "root": "http://yum.oracle.com/", + "discovery_pattern": "/html/body//h3/a[regex:test(@href, 'oracle-linux-[6-7]+\.html')]/@href", + "sub_discovery_pattern": "/html/body//h3[regex:test(., '^Latest:')]/a[regex:test(@href, 'x86_64/index.html')]/@href", + "page_pattern": "/html/body//a[regex:test(@href, '^getPackage/kernel-(devel-)?[0-9].*\.rpm$')]/@href", + } + ] +} + +def progress(distro, current, total, package): + sys.stderr.write('\r{} {}/{} {} '.format(distro, current, total, package)) + +# +# In our design you are not supposed to modify the code. The whole script is +# created so that you just have to add entry to the `repos` array and new +# links will be found automagically without needing to write any single line of +# code. +# +urls = set() +URL_TIMEOUT=30 + +if len(sys.argv) < 2 or not sys.argv[1] in repos: + sys.stderr.write("Usage: " + sys.argv[0] + " \n") + sys.exit(1) + +# +# Navigate the `repos` tree and look for packages we need that match the +# patterns given. Save the result in `packages`. +# +for repo in repos[sys.argv[1]]: + try: + root = urlopen(repo["root"],timeout=URL_TIMEOUT).read() + except: + continue + versions = html.fromstring(root).xpath(repo["discovery_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + vid = 0 + for version in versions: + vid += 1 + ver_url = repo["root"] + version + progress(repo["root"], vid, len(versions), version) + try: + subroot = urlopen(ver_url,timeout=URL_TIMEOUT).read() + except: + continue + sub_vers = html.fromstring(subroot).xpath(repo["sub_discovery_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + for sub_ver in sub_vers: + sub_ver = sub_ver.lstrip('/') + # The try - except block is used because 404 errors and similar + # might happen (and actually happen because not all repos have + # packages we need) + try: + source = repo["root"] + sub_ver + page = urlopen(source,timeout=URL_TIMEOUT).read() + rpms = html.fromstring(page).xpath(repo["page_pattern"], namespaces = {"regex": "http://exslt.org/regular-expressions"}) + + source = source.replace("index.html", "") + for rpm in rpms: + urls.add(source + str(unquote(rpm))) + except: + continue + +# +# Print URLs to stdout +# +for url in urls: + print(url) diff --git a/probe-builder/toolkit-entrypoint.sh b/probe-builder/toolkit-entrypoint.sh new file mode 100755 index 0000000000..b192f282fa --- /dev/null +++ b/probe-builder/toolkit-entrypoint.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +set -euo pipefail + +usage() { + cat >&2 < /tmp/container.img + + # mount developer container is a very stateful part of this script + # the section between mount/unmounting should be kept very small + # otherwise if something fails there are many inconsistencies that can happen + LOOPDEV=$(kpartx -asv /tmp/container.img | cut -d\ -f 3) + mount /dev/mapper/$LOOPDEV /mnt + # Copy kernel headers + cp -r /mnt/lib/modules "$OUTPUT_DIR" + + # Copy kernel config + rm -f $OUTPUT_DIR/config-* + cp /mnt/usr/boot/config-* $OUTPUT_DIR/ + cp $OUTPUT_DIR/config-* $OUTPUT_DIR/config_orig + # umount and remove the developer container + umount /mnt + kpartx -dv /tmp/container.img +} + +unpack_rpm() +{ + KERNEL_PACKAGE="$1" + OUTPUT_DIR="$2" + + rpm2cpio "$KERNEL_PACKAGE" | (cd "$OUTPUT_DIR" && cpio -idm) +} + +case "$1" in + coreos) + unpack_coreos_kernel "$2" "$3" + ;; + rpm) + unpack_rpm "$2" "$3" + ;; + *) + echo "Unsupported distribution $1" + exit 1 + ;; +esac diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt new file mode 100644 index 0000000000..e30089c9d6 --- /dev/null +++ b/scripts/CMakeLists.txt @@ -0,0 +1,31 @@ +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +configure_file(debian/postinst.in debian/postinst) +configure_file(debian/prerm.in debian/prerm) + +install(FILES completions/bash/sysdig + DESTINATION "${DIR_ETC}/bash_completion.d") + +install(FILES completions/zsh/_sysdig + DESTINATION share/zsh/vendor-completions) + +install(FILES completions/zsh/_sysdig + DESTINATION share/zsh/site-functions) + +install(PROGRAMS sysdig-probe-loader + DESTINATION bin) diff --git a/scripts/boot2docker-kernel-crawler.py b/scripts/boot2docker-kernel-crawler.py new file mode 100755 index 0000000000..b5fbdd3919 --- /dev/null +++ b/scripts/boot2docker-kernel-crawler.py @@ -0,0 +1,47 @@ +#!/usr/bin/python +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + + +# Author: Ethan Sutin + +from lxml import etree +import urllib2,re +from distutils.version import LooseVersion + +response = urllib2.urlopen('https://github.com/boot2docker/boot2docker/tags.atom') +ns_map = {'ns': 'http://www.w3.org/2005/Atom'} +data = etree.fromstring(response.read()) +release_nodes = data.xpath('//ns:feed/ns:entry/ns:title', namespaces=ns_map) + +for release in release_nodes: + version = release.text + if ':' in version: + version = version[:version.index(':')] + # tracepoints only enabled >= 1.7.0 + if LooseVersion(version[1:]) >= LooseVersion('1.7'): + dockerFile = urllib2.urlopen('https://raw.githubusercontent.com/boot2docker/boot2docker/%s/Dockerfile' % (version)).read() + for line in dockerFile.split('\n'): + if re.search('ENV KERNEL_VERSION', line): + kernel_version = line.split()[-1] + if re.search('ENV AUFS_BRANCH', line): + aufs_branch = line.split()[-1] + if re.search('ENV AUFS_COMMIT', line): + aufs_commit = line.split()[-1] + print 'boot2docker-%s %s-boot2docker https://www.kernel.org/pub/linux/kernel/v4.x/linux-%s.tar.xz https://raw.githubusercontent.com/boot2docker/boot2docker/%s/kernel_config https://github.com/sfjro/aufs4-standalone %s %s' % \ + (version[1:],kernel_version,kernel_version,version,aufs_branch,aufs_commit) diff --git a/scripts/build-probe-binaries b/scripts/build-probe-binaries new file mode 100755 index 0000000000..1338c9c528 --- /dev/null +++ b/scripts/build-probe-binaries @@ -0,0 +1,21 @@ +#!/bin/bash + +# compatibility wrapper for the probe builder +# see probe-builder/build-probe-binaries for the actual script + +BASEDIR=$(dirname $(readlink -f $0)) +NEW_BUILDER=$(dirname $BASEDIR)/probe-builder/build-probe-binaries + +echo "Using $0 is deprecated, please switch to $NEW_BUILDER soon" >&2 + +set -e + +if [ $# -ge 4 ]; then + echo "Running $NEW_BUILDER -p $1 -v $2 -k $4 -a $6 -A $5" + $NEW_BUILDER -p $1 -v $2 -k $4 -a "$6" -A "$5" +else + echo "Running $NEW_BUILDER -p $1 -v $2" + $NEW_BUILDER -p $1 -v $2 +fi + +echo "Using $0 is deprecated, please switch to $NEW_BUILDER soon" >&2 diff --git a/scripts/compile-linux-tree.sh b/scripts/compile-linux-tree.sh new file mode 100755 index 0000000000..0d6b430180 --- /dev/null +++ b/scripts/compile-linux-tree.sh @@ -0,0 +1,102 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +# +# This script tries to compile the sysdig probes against all the stable kernel +# releases that match a specific pattern. +# +# Usage: +# +# compile-linux-tree.sh SYSDIG_SOURCE_DIRECTORY LINUX_TREE PATTERN1 PATTERN2 ... +# +# Example: +# +# compile-linux-tree.sh ~/sysdig_src ~/linux_tree 'v4.1[4-9]*' +# +set -uo pipefail + +SYSDIG_SRC_DIR=$1 +LINUX_TREE=$2 +shift 2 +PATTERNS=$@ + +export KERNELDIR=$LINUX_TREE + +cd "$LINUX_TREE" + +TAGS=$(git tag -l $PATTERNS | sort -V) + +echo "Processing the following versions: $TAGS" + +for tag in $TAGS +do + cd "$LINUX_TREE" + git checkout "$tag" &> /dev/null + if [ $? -ne 0 ]; then + echo "$tag -> failure (git)" + continue + fi + + make distclean &> /dev/null + if [ $? -ne 0 ]; then + echo "$tag -> failure (make distclean)" + continue + fi + + make defconfig &> /dev/null + if [ $? -ne 0 ]; then + echo "$tag -> failure (make defconfig)" + continue + fi + + make modules_prepare &> /dev/null + if [ $? -ne 0 ]; then + echo "$tag -> failure (make modules_prepare)" + continue + fi + + cd "$SYSDIG_SRC_DIR" + + make -C driver/bpf clean &> /dev/null + rm -rf build + + mkdir build + cd build + cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_BPF=ON .. > /dev/null + if [ $? -ne 0 ]; then + echo "$tag -> failure (cmake)" + continue + fi + + make driver VERBOSE=1 > /dev/null + if [ $? -ne 0 ]; then + echo "$tag -> failure (driver)" + continue + fi + + make bpf VERBOSE=1 > /dev/null + if [ $? -ne 0 ]; then + echo "$tag -> failure (bpf)" + continue + fi + + echo "$tag -> success" +done + +echo "All done" diff --git a/scripts/completions/bash/sysdig b/scripts/completions/bash/sysdig new file mode 100644 index 0000000000..d0d414df32 --- /dev/null +++ b/scripts/completions/bash/sysdig @@ -0,0 +1,120 @@ +_sysdig_complete() +{ + local opts=' \ + -A \ + --print-ascii \ + -b \ + --print-base64 \ + -C \ + --file-size \ + -cl \ + --list-chisels \ + -d \ + --displayflt \ + -D \ + --debug \ + -e \ + --events \ + -E \ + --exclude-users \ + -F \ + --fatfile \ + -G \ + --seconds \ + -h \ + --help \ + -j \ + --json \ + -k \ + --k8s-api \ + -L \ + --list-events \ + -l \ + --list \ + -lv \ + --page-faults \ + -P \ + --progress \ + -q \ + --quiet \ + -R \ + --resolve-ports \ + -S \ + --summary \ + -v \ + --verbose \ + -x \ + --print-hex \ + -X \ + --print-hex-ascii \ + -z \ + --compress \ + -n \ + --numevents \ + -p \ + --print \ + -r \ + --read \ + -w \ + --write \ + -W \ + --limit \ + -s \ + --snaplen \ + -t \ + --timetype \ + -c \ + --chisel \ + -i \ + --chisel-info' + + local cur=${COMP_WORDS[COMP_CWORD]} + local prev=${COMP_WORDS[COMP_CWORD-1]} + + case "$prev" in + -c|--chisel|-i|--chisel-info) + local chisels="" + local detail="Use the -i flag to get detailed information about a specific chisel" + while IFS= read -r line + do + if [[ $line =~ "---" ]]; then + # skip lines such as + # ----------------- + continue; + elif [[ -z "$line" ]]; then + # empty lines reset the category + continue; + elif [[ $line =~ "Category" ]]; then + # category + continue; + elif [[ $line =~ $detail ]]; then + # detail instructions + continue; + fi + + local chisel=${line%% *} + + if [[ -z "$chisel" ]]; then + # empty lines from description + continue; + fi + + chisels="$chisels $chisel" + + done < <(sysdig -cl) + COMPREPLY=( $( compgen -W "$chisels" -- $cur ) ) + return 0 + ;; + + esac + + # completing an option + if [[ "$cur" == -* ]]; then + COMPREPLY=( $( compgen -W "$opts" -- $cur ) ) + fi +} +complete -o default -F _sysdig_complete sysdig + +# Local Variables: +# mode:sh +# End: diff --git a/scripts/completions/zsh/_sysdig b/scripts/completions/zsh/_sysdig new file mode 100644 index 0000000000..0270dd95ae --- /dev/null +++ b/scripts/completions/zsh/_sysdig @@ -0,0 +1,274 @@ +#compdef sysdig + +# This completion code is based on the cmdline interface as it exists in sysdig +# 0.1.101 + +# this must match fields_info.cpp +local DESCRIPTION_TEXT_START=16 + +# handles completion of filter fields +function _filter () { + # I run 'sysdig -l' to get a list of available filter types to generate the + # user choice + + local in_field=0 fields + typeset -a fields + + _call_program chisel_info sysdig -l 2>/dev/null | + while IFS='' read -r line; do + if [[ $line =~ "^([a-zA-Z0-9_]+\.[a-zA-Z0-9_\.]+) +(.*?)$" ]]; then + # starting a new field + in_field=1 + fields[$#fields+1]="$match[1]:$match[2]" + elif (($in_field)) && [[ $line =~ "^ {$DESCRIPTION_TEXT_START}(.*)" ]]; then + # field continuation + fields[$#fields]+=$match[1] + else + in_field=0 + fi + done + + + # Here I use _describe -S '' to omit the closing quote when completing. + # Otherwise if you have + # + # $ sysdig -p '%fd.si + # + # and you press TAB, you'll get + # + # $ sysdig -p '%fd.sip' + # + # Note the trailing ', which I don't want + if [[ -z $1 ]]; then + # full filter expansion + local ops + ops=("=" "!=" "<" "<=" ">" ">=" "contains" "and" "or" "not" "(" ")") + + # Remove the FILTER-ONLY string, since we don't need to indicate that to + # the user here + fields=(${fields/"(FILTER ONLY) "/}) # + _describe -t fields 'Fields' fields + _describe -t operators 'Operators' ops + elif [[ $1 == "format" ]]; then + # expanding format specifiers + local allfields_withpercent; + + # add a leading '%', and remove all FILTER-ONLY fields + allfields_withpercent=("%"${^fields:#*FILTER ONLY*}) + + # skip all leading characters that can't be in a field name. This lets + # me specify multiple fields in the string or do things like + # "%fd.cip,%fd.cport" + compset -P "*[^a-zA-Z0-9_%\.]" + + _describe -t format_fields 'Format fields' allfields_withpercent -S '' + fi +} + +# handles completion for chisel names +function _chisels () { + # I run 'sysdig -cl' to get a list of available chisels, and build an + # _alternative command to generate the user choice + + local incategory=0 alts_types alts_chisels + typeset -a alts_types alts_chisels + + function closetype() { + if [[ -n "$alts_types[$#alts_types]" ]] && (($#alts_chisels)); then + + # append the trailing " + local descriptions_for_type + typeset -a descriptions_for_type + descriptions_for_type=(${^alts_chisels}\") + + alts_types[$#alts_types]+=$descriptions_for_type[*]'))' + fi + alts_chisels=() + incategory=0 + } + + _call_program chisels sysdig -cl 2>/dev/null | + while IFS='' read -r line; do + if [[ $line =~ "^-+$" ]]; then + # skip lines such as + # ----------------- + continue; + elif [[ -z "$line" ]]; then + # empty lines reset the category + closetype + elif [[ $line =~ "^Category: (.*)" ]]; then + # got a new category + closetype + incategory=1 + local category=$match[1] + local index=$(($#alts_types+1)) + alts_types[$index]="category_$index:$category:((" + elif (($incategory)) && [[ $line =~ "^([a-zA-Z0-9_-]+) *(.+?)" ]]; then + # got a chisel + local name=$match[1]; + local descr=$match[2]; + local index=$(($#alts_chisels+1)) + alts_chisels[$index]+="${name}\\:\"$descr" # leave out closing " to + # allow line + # continuations + elif (($incategory)) && [[ $line =~ "^ {$DESCRIPTION_TEXT_START}(.*)" ]]; then + # field continuation + alts_chisels[$#alts_chisels]+=$match[1] + fi + done + closetype + + _alternative $alts_types[*] +} + +# returns (in $$1) a description of a given argument of a given chisel +function _get_chisel_arg () { + # I run 'sysdig -i ' to get a description of this chisel. I parse + # the argument list at the end of this description. This list looks like + # this + # + # Args: + # [int] arg1 - blah blah blah blah blah + # [int] arg2 - something blah blah blah blah blah blah blah b + # lah blah blah blah blah + # + # The odd line wrapping is a result of a sysdig bug (patch in review), so + # here I assume that this has been fixed + + local outputvar=$1 chisel=$2 nth_want=$3 + local in_arglist=0 in_argwant=0 nth_have=0 + + _call_program chisel_info sysdig -i $chisel 2>/dev/null | + while IFS='' read -r line; do + if (($in_arglist)); then + + # I'm in the argument list + + if (($in_argwant)); then + if [[ $line =~ "^ {$DESCRIPTION_TEXT_START}(.*?)$" ]]; then + + # I'm in a continuation line for my argument I strip the + # leading whitespace and append + eval "$outputvar+=\$match[1]" + else + # Done reading my argument + return + fi + elif [[ $line =~ "^\[" ]] && ((++nth_have == $nth_want)); then + + # I'm in the argument I want! + ((++in_argwant)) + eval "$outputvar=\$line"; + fi + elif [[ $line =~ "^Args:" ]]; then + in_arglist=1 + continue + fi + done +} + +# If the cursor is in a chisel argument, returns (in $$1) the description of this +# argument. Otherwise returns '' +function _in_chiselarg () { + local outputvar=$1 + local index=$words[(i)-c] + local chisel_index=$words[(i)--chisel] + + if [[ $index -le $CURRENT ]]; then + # the -c index is valid + ; + elif [[ $chisel_index -le $CURRENT ]]; then + # the --chisel index is valid + index=$chisel_index; + else + # neither index is valid, so return false + return + fi + + # There is a chisel argument somewhere on the commandline. I look to see if + # I'm currently typing into it, and return the description if so + local chisel=$words[$index+1] + local nth=$(($CURRENT-$index-1)) + + _get_chisel_arg $outputvar $chisel $nth +} + +function _chiselarg () { + + # the index of the -c/--chisel argument + local description=$1 + _alternative "chiselarg:$description:" +} + + + + +local context state state_descr line +typeset -A opt_args + +_arguments \ + '(-A --print-ascii)'{-A,--print-ascii}'[Only print the text portion of data buffers]' \ + '(-b --print-base64)'{-b,--print-base64}'[Print data buffers in base64]' \ + '(-C --file-size)'{-C,--file-size=-}'[Chunk captures to files of given size]:Maximum chunk file size:' \ + '(-cl --list-chisels)'{-cl,--list-chisels}'[lists the available chisels]' \ + '(-d --displayflt)'{-d,--displayflt}'[Make the given filter a display one]' \ + '(-D --debug)'{-D,--debug}'[Capture events about sysdig itself]' \ + '(-E --exclude-users)'{-E,--exclude-users}'[Don'\''t create the user/group tables when starting]' \ + '(-e --events)'{-e,--events}'[Rotate the capture file every events]:Maximum event number:' \ + '(-F --fatfile)'{-F,--fatfile}'[Enable fatfile mode]' \ + '(-G --seconds)'{-G,--seconds=-}'[Rotate the capture file every seconds]:Rotation period (seconds):' \ + '(-h --help)'{-h,--help}'[Print this help]' \ + '(-j --json)'{-j,--json}'[Emit output as JSON]' \ + '(-k --k8s-api)'{-k,--k8s-api}'[Kubernetes API server]' \ + '(-L --list-events)'{-L,--list-events}'[List the events that the engine supports]' \ + '(-l -lv --list)'{-l,--list}'[List the fields that can be used for filtering]' \ + '(-l -lv --list)-lv[Verbosely list the fields that can be used for filtering]' \ + '(-n --numevents)'{-n,--numevents=-}'[Stop capturing after events]:Max events:' \ + '--page-faults[Capture user/kernel major/minor page faults]' \ + '(-P --progress)'{-P,--progress}'[Print progress on stderr while processing trace files]' \ + '(-p --print)'{-p,--print=-}'[Specify the event format (default reported with "sysdig -pp")]:Event output format:->format' \ + '(-q --quiet)'{-q,--quiet}'[Do not print events on the screen]' \ + '(-r --read)'{-r,--read=-}'[Read events from ]:Input file:_files -g "*.scap"' \ + '(-R --resolve-ports)'{-R,--resolve-ports}'[Resolve port numbers to names.]' \ + '(-S --summary)'{-S,--summary}'[Print the event summary when the capture ends]' \ + '(-s --snaplen)'{-s,--snaplen=-}'[Capture the first bytes of each I/O buffer]:Buffer length (bytes):' \ + '(-t --timetype)'{-t,--timetype=-}'[Change the way event time is displayed]:Time-reporting type:(( \ + h\:"human-readable string" \ + a\:"absolute timestamp from epoch" \ + r\:"relative time from capture start" \ + d\:"delta between enter/exit" \ + D\:"delta from previous event"))' \ + '(-v --verbose)'{-v,--verbose}'[Verbose output]' \ + '--unbuffered[Disable buffering of output]' \ + '(-w --write)'{-w,--write=-}'[Write events to ]:Output file:_files -g "*.scap"' \ + '(-W --limit)'{-W,--limit}'[Limit split captures (-C, -G or -e) to a given number of files]:Max # of files' \ + '(-x --print-hex)'{-x,--print-hex}'[Print data buffers in hex]' \ + '(-X --print-hex-ascii)'{-X,--print-hex-ascii}'[Print data buffers in hex and ASCII]' \ + '(-z --compress)'{-z,--compress}'[Enables compression for stored tracefiles]' \ + '-pp[Report the default printing format used if -p is omitted]' \ + '(-c --chisel)'{-c,--chisel}'[Run a given chisel]:Chisel:->chisel' \ + '(-i --chisel-info)'{-i,--chisel-info}'[Get a detailed chisel description]:Chisel:->chisel' \ + '*:Filter or chisel argument:->filter_or_chiselarg' && return 0 + + +case $state in + (chisel) + _chisels + ;; + (format) + _filter format + ;; + (filter_or_chiselarg) + local chiselarg + _in_chiselarg chiselarg + if [[ -n "$chiselarg" ]]; then + _chiselarg $chiselarg + else + _filter + fi + ;; +esac + +# Local Variables: +# mode:sh +# End: diff --git a/scripts/debian/postinst b/scripts/debian/postinst deleted file mode 100644 index 9ae575d351..0000000000 --- a/scripts/debian/postinst +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh -set -e - -DKMS_PACKAGE_NAME="sysdig" -DKMS_VERSION="" - -postinst_found=0 - -case "$1" in - configure) - for DKMS_POSTINST in /usr/lib/dkms/common.postinst /usr/share/$DKMS_PACKAGE_NAME/postinst; do - if [ -f $DKMS_POSTINST ]; then - $DKMS_POSTINST $DKMS_PACKAGE_NAME $DKMS_VERSION /usr/share/$DKMS_PACKAGE_NAME "" $2 - postinst_found=1 - break - fi - done - if [ "$postinst_found" -eq 0 ]; then - echo "ERROR: DKMS version is too old and $DKMS_PACKAGE_NAME was not" - echo "built with legacy DKMS support." - echo "You must either rebuild $DKMS_PACKAGE_NAME with legacy postinst" - echo "support or upgrade DKMS to a more current version." - exit 1 - fi - ;; -esac diff --git a/scripts/debian/postinst.in b/scripts/debian/postinst.in new file mode 100755 index 0000000000..c5f59b0d7d --- /dev/null +++ b/scripts/debian/postinst.in @@ -0,0 +1,44 @@ +#!/bin/sh +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +set -e + +DKMS_PACKAGE_NAME="@PACKAGE_NAME@" +DKMS_VERSION="@PROBE_VERSION@" + +postinst_found=0 + +case "$1" in + configure) + for DKMS_POSTINST in /usr/lib/dkms/common.postinst /usr/share/$DKMS_PACKAGE_NAME/postinst; do + if [ -f $DKMS_POSTINST ]; then + $DKMS_POSTINST $DKMS_PACKAGE_NAME $DKMS_VERSION /usr/share/$DKMS_PACKAGE_NAME "" $2 + postinst_found=1 + break + fi + done + if [ "$postinst_found" -eq 0 ]; then + echo "ERROR: DKMS version is too old and $DKMS_PACKAGE_NAME was not" + echo "built with legacy DKMS support." + echo "You must either rebuild $DKMS_PACKAGE_NAME with legacy postinst" + echo "support or upgrade DKMS to a more current version." + exit 1 + fi + ;; +esac diff --git a/scripts/debian/prerm b/scripts/debian/prerm deleted file mode 100644 index ef233e9018..0000000000 --- a/scripts/debian/prerm +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh -set -e - -DKMS_PACKAGE_NAME="sysdig" -DKMS_VERSION="" - -case "$1" in - remove|upgrade|deconfigure) - if [ "$(dkms status -m $DKMS_PACKAGE_NAME -v $DKMS_VERSION)" ]; then - dkms remove -m $DKMS_PACKAGE_NAME -v $DKMS_VERSION --all - fi - ;; -esac diff --git a/scripts/debian/prerm.in b/scripts/debian/prerm.in new file mode 100755 index 0000000000..d0796193cb --- /dev/null +++ b/scripts/debian/prerm.in @@ -0,0 +1,30 @@ +#!/bin/sh +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +set -e + +DKMS_PACKAGE_NAME="@PACKAGE_NAME@" +DKMS_VERSION="@PROBE_VERSION@" + +case "$1" in + remove|upgrade|deconfigure) + if [ "$(dkms status -m $DKMS_PACKAGE_NAME -v $DKMS_VERSION)" ]; then + dkms remove -m $DKMS_PACKAGE_NAME -v $DKMS_VERSION --all + fi + ;; +esac diff --git a/scripts/install-sysdig b/scripts/install-sysdig deleted file mode 100755 index 9102bf2180..0000000000 --- a/scripts/install-sysdig +++ /dev/null @@ -1,140 +0,0 @@ -#!/bin/bash -# -# Copyright (C) 2013-2014 Draios inc. -# -# This file is part of sysdig. -# -# sysdig is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 as -# published by the Free Software Foundation. -# -# sysdig is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with sysdig. If not, see . -# -set -e - -function install_rpm { - if ! hash curl > /dev/null 2>&1; then - echo "* Installing curl" - yum -q -y install curl - fi - - if ! yum -q list dkms > /dev/null 2>&1; then - echo "* Installing EPEL repository (for DKMS)" - rpm --quiet -i http://mirror.us.leaseweb.net/epel/6/i386/epel-release-6-8.noarch.rpm - fi - - echo "* Installing Draios public key" - rpm --quiet --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public - echo "* Installing Draios repository" - curl -s -o /etc/yum.repos.d/draios.repo http://download.draios.com/_REPOSITORY_NAME_/rpm/draios.repo - echo "* Installing kernel headers" - KERNEL_VERSION=$(uname -r) - if [[ $KERNEL_VERSION == *PAE* ]]; then - yum -q -y install kernel-PAE-devel-${KERNEL_VERSION%.PAE} || kernel_warning - else - yum -q -y install kernel-devel-$KERNEL_VERSION || kernel_warning - fi - echo "* Installing Sysdig" - yum -q -y install sysdig -} - -function install_deb { - export DEBIAN_FRONTEND=noninteractive - - if ! hash curl > /dev/null 2>&1; then - echo "* Installing curl" - apt-get -qq -y install curl - fi - - echo "* Installing Draios public key" - curl -s https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public | apt-key add - - echo "* Installing Draios repository" - curl -s -o /etc/apt/sources.list.d/draios.list http://download.draios.com/_REPOSITORY_NAME_/deb/draios.list - apt-get -qq update - echo "* Installing kernel headers" - apt-get -qq -y install linux-headers-$(uname -r) || kernel_warning - echo "* Installing Sysdig" - apt-get -qq -y install sysdig -} - -function unsupported { - echo "Unsupported operating system. Please consider contacting support@draios.com or trying the manual installation." - exit 1 -} - -function kernel_warning { - echo "Unable to find kernel development files for the current kernel version" $(uname -r) - echo "This usually means that your system is not up-to-date or you installed a custom kernel version." - echo "The installation will continue but you'll need to install these yourself in order to use sysdig." - echo "Contact support@draios.com if you need further assistance." -} - -if [ $(id -u) != 0 ]; then - echo "Installer must be run as root (or with sudo)." - exit 1 -fi - -echo "* Detecting operating system" - -ARCH=$(uname -m) -if [[ ! $ARCH = *86 ]] && [ ! $ARCH = "x86_64" ]; then - unsupported -fi - -if [ -f /etc/debian_version ]; then - if [ -f /etc/lsb-release ]; then - . /etc/lsb-release - DISTRO=$DISTRIB_ID - VERSION=$DISTRIB_RELEASE - else - DISTRO="Debian" - VERSION=$(cat /etc/debian_version | cut -d'.' -f1) - fi - - if [ $DISTRO = "Ubuntu" ]; then - VERSION=$(echo $VERSION | cut -d'.' -f1) - if [ $VERSION -ge 10 ]; then - install_deb - else - unsupported - fi - elif [ $DISTRO = "Debian" ]; then - if [ $VERSION -ge 6 ]; then - install_deb - else - unsupported - fi - else - unsupported - fi - -elif [ -f /etc/system-release-cpe ]; then - DISTRO=$(cat /etc/system-release-cpe | cut -d':' -f3) - VERSION=$(cat /etc/system-release-cpe | cut -d':' -f5) - - if [ $DISTRO = "oracle" ] || [ $DISTRO = "centos" ] || [ $DISTRO = "redhat" ]; then - if echo $VERSION | grep -q ^6; then - install_rpm - else - unsupported - fi - elif [ $DISTRO = "amazon" ]; then - install_rpm - elif [ $DISTRO = "fedoraproject" ]; then - if [ $VERSION -ge 13 ]; then - install_rpm - else - unsupported - fi - else - unsupported - fi -else - unsupported -fi diff --git a/scripts/install-sysdig.in b/scripts/install-sysdig.in new file mode 100644 index 0000000000..058ee341af --- /dev/null +++ b/scripts/install-sysdig.in @@ -0,0 +1,200 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of _COMPONENT_ . +# +# 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. +# +set -e + +function install_rpm { + if ! hash curl > /dev/null 2>&1; then + echo "* Installing curl" + yum -q -y install curl + fi + + if ! yum -q list dkms > /dev/null 2>&1; then + echo "* Installing EPEL repository (for DKMS)" + if [ $VERSION -eq 8 ]; then + rpm --quiet -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm + elif [ $VERSION -eq 7 ]; then + rpm --quiet -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm + else + rpm --quiet -i https://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm + fi + fi + + echo "* Installing _COMPONENT_ public key" + rpm --quiet --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public + echo "* Installing _COMPONENT_ repository" + curl -s -o /etc/yum.repos.d/draios.repo https://s3.amazonaws.com/download.draios.com/_REPOSITORY_NAME_/rpm/draios.repo + echo "* Installing kernel headers" + KERNEL_VERSION=$(uname -r) + if [[ $KERNEL_VERSION == *PAE* ]]; then + yum -q -y install kernel-PAE-devel-${KERNEL_VERSION%.PAE} || kernel_warning + elif [[ $KERNEL_VERSION == *stab* ]]; then + # It's OpenVZ kernel and we should install another package + yum -q -y install vzkernel-devel-$KERNEL_VERSION || kernel_warning + elif [[ $KERNEL_VERSION == *uek* ]]; then + yum -q -y install kernel-uek-devel-$KERNEL_VERSION || kernel_warning + else + yum -q -y install kernel-devel-$KERNEL_VERSION || kernel_warning + fi + echo "* Installing _COMPONENT_" + yum -q -y install _COMPONENT_ +} + +function install_deb { + export DEBIAN_FRONTEND=noninteractive + + if ! hash curl > /dev/null 2>&1; then + echo "* Installing curl" + apt-get -qq -y install curl < /dev/null + fi + + echo "* Installing Sysdig public key" + curl -s https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public | apt-key add - + echo "* Installing _COMPONENT_ repository" + curl -s -o /etc/apt/sources.list.d/draios.list https://s3.amazonaws.com/download.draios.com/_REPOSITORY_NAME_/deb/draios.list + apt-get -qq update < /dev/null + echo "* Installing kernel headers" + apt-get -qq -y install linux-headers-$(uname -r) < /dev/null || kernel_warning + echo "* Installing _COMPONENT_" + apt-get -qq -y install _COMPONENT_ < /dev/null +} + +function unsupported { + echo 'Unsupported operating system. Please consider writing to the mailing list at' + echo 'https://groups.google.com/forum/#!forum/sysdig or trying the manual' + echo 'installation.' + exit 1 +} + +function kernel_warning { + echo "Unable to find kernel development files for the current kernel version" $(uname -r) + echo "This usually means that your system is not up-to-date or you installed a custom kernel version." + echo "The installation will continue but you'll need to install these yourself in order to use _COMPONENT_." + echo 'Please write to the mailing list at https://groups.google.com/forum/#!forum/sysdig' + echo "if you need further assistance." +} + +if [ $(id -u) != 0 ]; then + echo "Installer must be run as root (or with sudo)." + exit 1 +fi + +echo "* Detecting operating system" + +ARCH=$(uname -m) +if [[ ! $ARCH = *86 ]] && [ ! $ARCH = "x86_64" ] && [ ! $ARCH = "s390x" ]; then + unsupported +fi + +if [ $ARCH = "s390x" ]; then + echo "------------" + echo "WARNING: A Docker container is the only officially supported platform on s390x" + echo "------------" +fi + +if [ -f /etc/debian_version ]; then + if [ -f /etc/lsb-release ]; then + . /etc/lsb-release + DISTRO=$DISTRIB_ID + VERSION=${DISTRIB_RELEASE%%.*} + else + DISTRO="Debian" + VERSION=$(cat /etc/debian_version | cut -d'.' -f1) + fi + + case "$DISTRO" in + + "Ubuntu") + if [ $VERSION -ge 10 ]; then + install_deb + else + unsupported + fi + ;; + + "LinuxMint") + if [ $VERSION -ge 9 ]; then + install_deb + else + unsupported + fi + ;; + + "Debian") + if [ $VERSION -ge 6 ]; then + install_deb + elif [[ $VERSION == *sid* ]]; then + install_deb + else + unsupported + fi + ;; + + *) + unsupported + ;; + + esac + +elif [ -f /etc/system-release-cpe ]; then + DISTRO=$(cat /etc/system-release-cpe | cut -d':' -f3) + + # New Amazon Linux 2 distro + if [[ -f /etc/image-id ]]; then + AMZ_AMI_VERSION=$(cat /etc/image-id | grep 'image_name' | cut -d"=" -f2 | tr -d "\"") + fi + + if [[ "${DISTRO}" == "o" ]] && [[ ${AMZ_AMI_VERSION} = *"amzn2"* ]]; then + DISTRO=$(cat /etc/system-release-cpe | cut -d':' -f4) + fi + + VERSION=$(cat /etc/system-release-cpe | cut -d':' -f5 | cut -d'.' -f1 | sed 's/[^0-9]*//g') + + case "$DISTRO" in + + "oracle" | "centos" | "redhat") + if [ $VERSION -ge 6 ]; then + install_rpm + else + unsupported + fi + ;; + + "amazon") + install_rpm + ;; + + "fedoraproject") + if [ $VERSION -ge 13 ]; then + install_rpm + else + unsupported + fi + ;; + + *) + unsupported + ;; + + esac + +else + unsupported +fi + +modprobe -r _COMPONENT__probe diff --git a/scripts/rpm/postinstall b/scripts/rpm/postinstall old mode 100644 new mode 100755 index 72674f6671..27db41826c --- a/scripts/rpm/postinstall +++ b/scripts/rpm/postinstall @@ -1,7 +1,24 @@ +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# dkms add -m sysdig -v %{version} --rpm_safe_upgrade if [ `uname -r | grep -c "BOOT"` -eq 0 ] && [ -e /lib/modules/`uname -r`/build/include ]; then dkms build -m sysdig -v %{version} - dkms install -m sysdig -v %{version} + dkms install --force -m sysdig -v %{version} elif [ `uname -r | grep -c "BOOT"` -gt 0 ]; then echo -e "" echo -e "Module build for the currently running kernel was skipped since you" diff --git a/scripts/rpm/preuninstall b/scripts/rpm/preuninstall old mode 100644 new mode 100755 index 8fc95ce33d..7b0137329c --- a/scripts/rpm/preuninstall +++ b/scripts/rpm/preuninstall @@ -1 +1,18 @@ +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# dkms remove -m sysdig -v %{version} --all --rpm_safe_upgrade diff --git a/scripts/sysdig-probe-loader b/scripts/sysdig-probe-loader new file mode 100755 index 0000000000..313d1d9d4e --- /dev/null +++ b/scripts/sysdig-probe-loader @@ -0,0 +1,432 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +# +# Simple script that desperately tries to load sysdig-probe looking +# for it in a bunch of ways. Convenient when running sysdig inside +# a container or in other weird environments. +# + +# +# Returns 1 if $cos_ver > $base_ver, 0 otherwise +# +cos_version_greater() +{ + if [[ $cos_ver == $base_ver ]]; then + return 0 + fi + + # + # COS build numbers are in the format x.y.z + # + a=`echo $cos_ver | cut -d. -f1` + b=`echo $cos_ver | cut -d. -f2` + c=`echo $cos_ver | cut -d. -f3` + + d=`echo $base_ver | cut -d. -f1` + e=`echo $base_ver | cut -d. -f2` + f=`echo $base_ver | cut -d. -f3` + + # Test the first component + if [[ $a -gt $d ]]; then + return 1 + elif [[ $d -gt $a ]]; then + return 0 + fi + + # Test the second component + if [[ $b -gt $e ]]; then + return 1 + elif [[ $e -gt $b ]]; then + return 0 + fi + + # Test the third component + if [[ $c -gt $f ]]; then + return 1 + elif [[ $f -gt $c ]]; then + return 0 + fi + + # If we get here, probably malformed version string? + + return 0 +} + + +get_kernel_config() { + if [ -f /proc/config.gz ]; then + echo "Found kernel config at /proc/config.gz" + KERNEL_CONFIG_PATH=/proc/config.gz + elif [ -f "/boot/config-${KERNEL_RELEASE}" ]; then + echo "Found kernel config at /boot/config-${KERNEL_RELEASE}" + KERNEL_CONFIG_PATH=/boot/config-${KERNEL_RELEASE} + elif [ ! -z "${SYSDIG_HOST_ROOT}" ] && [ -f "${SYSDIG_HOST_ROOT}/boot/config-${KERNEL_RELEASE}" ]; then + echo "Found kernel config at ${SYSDIG_HOST_ROOT}/boot/config-${KERNEL_RELEASE}" + KERNEL_CONFIG_PATH="${SYSDIG_HOST_ROOT}/boot/config-${KERNEL_RELEASE}" + elif [ -f "/usr/lib/ostree-boot/config-${KERNEL_RELEASE}" ]; then + echo "Found kernel config at /usr/lib/ostree-boot/config-${KERNEL_RELEASE}" + KERNEL_CONFIG_PATH="/usr/lib/ostree-boot/config-${KERNEL_RELEASE}" + elif [ ! -z "${SYSDIG_HOST_ROOT}" ] && [ -f "${SYSDIG_HOST_ROOT}/usr/lib/ostree-boot/config-${KERNEL_RELEASE}" ]; then + echo "Found kernel config at ${SYSDIG_HOST_ROOT}/usr/lib/ostree-boot/config-${KERNEL_RELEASE}" + KERNEL_CONFIG_PATH="${SYSDIG_HOST_ROOT}/usr/lib/ostree-boot/config-${KERNEL_RELEASE}" + elif [ -f /lib/modules/${KERNEL_RELEASE}/config ]; then + # this code works both for native host and agent container assuming that + # Dockerfile sets up the desired symlink /lib/modules -> $SYSDIG_HOST_ROOT/lib/modules + echo "Found kernel config at /lib/modules/${KERNEL_RELEASE}/config" + KERNEL_CONFIG_PATH="/lib/modules/${KERNEL_RELEASE}/config" + fi + + if [ -z "${KERNEL_CONFIG_PATH}" ]; then + echo "Cannot find kernel config" + exit 1 + fi + + if [[ "${KERNEL_CONFIG_PATH}" == *.gz ]]; then + HASH=$(zcat "${KERNEL_CONFIG_PATH}" | md5sum - | cut -d' ' -f1) + else + HASH=$(md5sum "${KERNEL_CONFIG_PATH}" | cut -d' ' -f1) + fi +} + +load_kernel_probe() { + if ! hash lsmod > /dev/null 2>&1; then + echo "This program requires lsmod" + exit 1 + fi + + if ! hash modprobe > /dev/null 2>&1; then + echo "This program requires modprobe" + exit 1 + fi + + if ! hash rmmod > /dev/null 2>&1; then + echo "This program requires rmmod" + exit 1 + fi + + echo "* Unloading ${PROBE_NAME}, if present" + rmmod "${PROBE_NAME}" 2>/dev/null + WAIT_TIME=0 + KMOD_NAME=$(echo "${PROBE_NAME}" | tr "-" "_") + while lsmod | grep "${KMOD_NAME}" > /dev/null 2>&1 && [ $WAIT_TIME -lt $MAX_RMMOD_WAIT ]; do + if rmmod "${PROBE_NAME}" 2>/dev/null; then + echo "* Unloading ${PROBE_NAME} succeeded after ${WAIT_TIME}s" + break + fi + ((++WAIT_TIME)) + if (( $WAIT_TIME % 5 == 0 )); then + echo "* ${PROBE_NAME} still loaded, waited ${WAIT_TIME}s (max wait ${MAX_RMMOD_WAIT}s)" + fi + sleep 1 + done + + if lsmod | grep "${KMOD_NAME}" > /dev/null 2>&1; then + echo "* ${PROBE_NAME} seems to still be loaded, hoping the best" + exit 0 + fi + + # skip dkms on UEK hosts because it will always fail + if [[ $(uname -r) == *uek* ]]; then + echo "* Skipping dkms install for UEK host" + else + echo "* Running dkms install for ${PACKAGE_NAME}" + if dkms install -m "${PACKAGE_NAME}" -v "${SYSDIG_VERSION}" -k "${KERNEL_RELEASE}"; then + echo "* Trying to load a dkms ${PROBE_NAME}, if present" + + if insmod "/var/lib/dkms/${PACKAGE_NAME}/${SYSDIG_VERSION}/${KERNEL_RELEASE}/${ARCH}/module/${PROBE_NAME}.ko" > /dev/null 2>&1; then + echo "${PROBE_NAME} found and loaded in dkms" + exit 0 + elif insmod "/var/lib/dkms/${PACKAGE_NAME}/${SYSDIG_VERSION}/${KERNEL_RELEASE}/${ARCH}/module/${PROBE_NAME}.ko.xz" > /dev/null 2>&1; then + echo "${PROBE_NAME} found and loaded in dkms (xz)" + exit 0 + else + echo "* Unable to insmod" + fi + else + DKMS_LOG="/var/lib/dkms/${PACKAGE_NAME}/${SYSDIG_VERSION}/build/make.log" + if [ -f "${DKMS_LOG}" ]; then + echo "* Running dkms build failed, dumping ${DKMS_LOG}" + cat "${DKMS_LOG}" + else + echo "* Running dkms build failed, couldn't find ${DKMS_LOG}" + fi + fi + fi + + echo "* Trying to load a system ${PROBE_NAME}, if present" + + if modprobe "${PROBE_NAME}" > /dev/null 2>&1; then + echo "${PROBE_NAME} found and loaded with modprobe" + exit 0 + fi + + echo "* Trying to find precompiled ${PROBE_NAME} for ${KERNEL_RELEASE}" + + get_kernel_config + + local SYSDIG_PROBE_FILENAME="${PROBE_NAME}-${SYSDIG_VERSION}-${ARCH}-${KERNEL_RELEASE}-${HASH}.ko" + + if [ -f "${HOME}/.sysdig/${SYSDIG_PROBE_FILENAME}" ]; then + echo "Found precompiled module at ~/.sysdig/${SYSDIG_PROBE_FILENAME}, loading module" + insmod "${HOME}/.sysdig/${SYSDIG_PROBE_FILENAME}" + exit $? + fi + + local URL + URL=$(echo "${SYSDIG_PROBE_URL}/${SYSDIG_REPOSITORY}/sysdig-probe-binaries/${SYSDIG_PROBE_FILENAME}" | sed s/+/%2B/g) + + echo "* Trying to download precompiled module from ${URL}" + + curl_out=$(curl --create-dirs "${SYSDIG_PROBE_CURL_OPTIONS}" -o "${HOME}/.sysdig/${SYSDIG_PROBE_FILENAME}" "${URL}" 2>&1) + if [ "$?" = "0" ]; then + echo "Download succeeded, loading module" + insmod "${HOME}/.sysdig/${SYSDIG_PROBE_FILENAME}" + exit $? + else + if [[ "$curl_out" =~ "404 Not Found" ]]; then + # The curl output throws a "curl: (22) The requested URL returned error: 404 Not Found" - but that is because the probe doesn't exist in the repo. + echo "Download of ${PROBE_NAME} for version ${SYSDIG_VERSION} failed. This is because the probe for this particular version does not exist in the repo." + if [ ! -z "${KERNEL_ERR_MESSAGE}" ]; then + echo "${KERNEL_ERR_MESSAGE}" + else + echo "Consider compiling your own ${PROBE_NAME} and loading it or getting in touch with the sysdig community" + fi + else + echo "$curl_out" + fi + exit 1 + fi +} + +load_bpf_probe() { + echo "* Mounting debugfs" + + if [ ! -d /sys/kernel/debug/tracing ]; then + mount -t debugfs nodev /sys/kernel/debug + fi + + get_kernel_config + + if [ ! -z "${SYSDIG_HOST_ROOT}" ] && [ -f "${SYSDIG_HOST_ROOT}/etc/os-release" ]; then + . "${SYSDIG_HOST_ROOT}/etc/os-release" + + if [ "${ID}" == "cos" ]; then + COS=1 + fi + fi + + if [ ! -z "${SYSDIG_HOST_ROOT}" ] && [ -f "${SYSDIG_HOST_ROOT}/etc/VERSION" ]; then + MINIKUBE=1 + MINIKUBE_VERSION="$(cat ${SYSDIG_HOST_ROOT}/etc/VERSION)" + fi + + local BPF_PROBE_FILENAME="${BPF_PROBE_NAME}-${SYSDIG_VERSION}-${ARCH}-${KERNEL_RELEASE}-${HASH}.o" + + if [ ! -f "${HOME}/.sysdig/${BPF_PROBE_FILENAME}" ]; then + + local BPF_KERNEL_SOURCES_URL="" + local STRIP_COMPONENTS=1 + + customize_kernel_build() { + if [ -n "${KERNEL_EXTRA_VERSION}" ]; then + sed -i "s/LOCALVERSION=\"\"/LOCALVERSION=\"${KERNEL_EXTRA_VERSION}\"/" .config + fi + make olddefconfig > /dev/null + make modules_prepare > /dev/null + } + + if [ -n "${COS}" ]; then + echo "* COS detected (build ${BUILD_ID}), using cos kernel headers..." + + BPF_KERNEL_SOURCES_URL="https://storage.googleapis.com/cos-tools/${BUILD_ID}/kernel-headers.tgz" + KERNEL_EXTRA_VERSION="+" + STRIP_COMPONENTS=0 + + customize_kernel_build() { + pushd usr/src/* > /dev/null + + # Note: this overrides the KERNELDIR set while untarring the tarball + export KERNELDIR=`pwd` + + sed -i '/^#define randomized_struct_fields_start struct {$/d' include/linux/compiler-clang.h + sed -i '/^#define randomized_struct_fields_end };$/d' include/linux/compiler-clang.h + + popd > /dev/null + + # Might need to configure our own sources depending on COS version + cos_ver=${BUILD_ID} + base_ver=11553.0.0 + + cos_version_greater + greater_ret=$? + + if [[ greater_ret -eq 1 ]]; then + export KBUILD_EXTRA_CPPFLAGS=-DCOS_73_WORKAROUND + fi + } + fi + + if [ -n "${MINIKUBE}" ]; then + echo "* Minikube detected (${MINIKUBE_VERSION}), using linux kernel sources for minikube kernel" + local kernel_version=$(uname -r) + local -r kernel_version_major=$(echo ${kernel_version} | cut -d. -f1) + local -r kernel_version_minor=$(echo ${kernel_version} | cut -d. -f2) + local -r kernel_version_patch=$(echo ${kernel_version} | cut -d. -f3) + + if [ "${kernel_version_patch}" == "0" ]; then + kernel_version="${kernel_version_major}.${kernel_version_minor}" + fi + + BPF_KERNEL_SOURCES_URL="http://mirrors.edge.kernel.org/pub/linux/kernel/v${kernel_version_major}.x/linux-${kernel_version}.tar.gz" + fi + + if [ -n "${SYSDIG_BPF_USE_LOCAL_KERNEL_SOURCES}" ]; then + local -r kernel_version_major=$(uname -r | cut -d. -f1) + local -r kernel_version=$(uname -r | cut -d- -f1) + KERNEL_EXTRA_VERSION="-$(uname -r | cut -d- -f2)" + + echo "* Using downloaded kernel sources for kernel version ${kernel_version}..." + + BPF_KERNEL_SOURCES_URL="http://mirrors.edge.kernel.org/pub/linux/kernel/v${kernel_version_major}.x/linux-${kernel_version}.tar.gz" + fi + + if [ -n "${BPF_KERNEL_SOURCES_URL}" ]; then + echo "* Downloading ${BPF_KERNEL_SOURCES_URL}" + + mkdir -p /tmp/kernel + cd /tmp/kernel + cd `mktemp -d -p /tmp/kernel` + if ! curl -o kernel-sources.tgz --create-dirs "${SYSDIG_PROBE_CURL_OPTIONS}" "${BPF_KERNEL_SOURCES_URL}"; then + exit 1; + fi + + echo "* Extracting kernel sources" + + mkdir kernel-sources && tar xf kernel-sources.tgz -C kernel-sources --strip-components "${STRIP_COMPONENTS}" + + cd kernel-sources + export KERNELDIR=`pwd` + + if [[ "${KERNEL_CONFIG_PATH}" == *.gz ]]; then + zcat "${KERNEL_CONFIG_PATH}" > .config + else + cat "${KERNEL_CONFIG_PATH}" > .config + fi + + echo "* Configuring kernel" + customize_kernel_build + fi + + echo "* Trying to compile BPF probe ${BPF_PROBE_NAME} (${BPF_PROBE_FILENAME})" + + make -C "/usr/src/${PACKAGE_NAME}-${SYSDIG_VERSION}/bpf" > /dev/null + + mkdir -p ~/.sysdig + mv "/usr/src/${PACKAGE_NAME}-${SYSDIG_VERSION}/bpf/probe.o" "${HOME}/.sysdig/${BPF_PROBE_FILENAME}" + + if [ -n "${BPF_KERNEL_SOURCES_URL}" ]; then + rm -r /tmp/kernel + fi + fi + + if [ ! -f "${HOME}/.sysdig/${BPF_PROBE_FILENAME}" ]; then + local URL + URL=$(echo "${SYSDIG_PROBE_URL}/${SYSDIG_REPOSITORY}/sysdig-probe-binaries/${BPF_PROBE_FILENAME}" | sed s/+/%2B/g) + + echo "* Trying to download precompiled BPF probe from ${URL}" + + curl --create-dirs "${SYSDIG_PROBE_CURL_OPTIONS}" -o "${HOME}/.sysdig/${BPF_PROBE_FILENAME}" "${URL}" + fi + + if [ -f "${HOME}/.sysdig/${BPF_PROBE_FILENAME}" ]; then + if [ ! -f /proc/sys/net/core/bpf_jit_enable ]; then + echo "**********************************************************" + echo "** BPF doesn't have JIT enabled, performance might be **" + echo "** degraded. Please ensure to run on a kernel with **" + echo "** CONFIG_BPF_JIT enabled and/or use --net=host if **" + echo "** running inside a container. **" + echo "**********************************************************" + fi + + echo "* BPF probe located, it's now possible to start sysdig" + + ln -sf "${HOME}/.sysdig/${BPF_PROBE_FILENAME}" "${HOME}/.sysdig/${BPF_PROBE_NAME}.o" + exit $? + else + echo "* Failure to find a BPF probe" + exit 1 + fi +} + +ARCH=$(uname -m) +KERNEL_RELEASE=$(uname -r) +SCRIPT_NAME=$(basename "${0}") +SYSDIG_PROBE_URL=${SYSDIG_PROBE_URL:-https://download.sysdig.com} +if [ -n "$SYSDIG_PROBE_INSECURE_DOWNLOAD" ] +then + SYSDIG_PROBE_CURL_OPTIONS=-fsSk +else + SYSDIG_PROBE_CURL_OPTIONS=-fsS +fi + +MAX_RMMOD_WAIT=60 +KERNEL_ERR_MESSAGE="" +if [[ $# -ge 1 ]]; then + KERNEL_ERR_MESSAGE="$1" +fi + +if [ -z "${SYSDIG_REPOSITORY}" ]; then + SYSDIG_REPOSITORY="stable" +fi + +if [ "${SCRIPT_NAME}" = "sysdig-probe-loader" ]; then + if [ -z "$SYSDIG_VERSION" ]; then + SYSDIG_VERSION=$(sysdig --version | cut -d' ' -f3) + fi + PROBE_NAME="sysdig-probe" + BPF_PROBE_NAME="sysdig-probe-bpf" + PACKAGE_NAME="sysdig" +elif [ "${SCRIPT_NAME}" = "sysdigcloud-probe-loader" ]; then + EXEPATH=$(dirname "$(readlink -f "${0}")") + if [ -z "$SYSDIG_VERSION" ]; then + SYSDIG_VERSION=$("${EXEPATH}"/dragent --version) + fi + PROBE_NAME="sysdigcloud-probe" + BPF_PROBE_NAME="sysdigcloud-probe-bpf" + PACKAGE_NAME="draios-agent" +else + echo "This script must be called as sysdig-probe-loader or sysdigcloud-probe-loader" + exit 1 +fi + +if [ "$(id -u)" != 0 ]; then + echo "Installer must be run as root (or with sudo)." + exit 1 +fi + +if ! hash curl > /dev/null 2>&1; then + echo "This program requires curl" + exit 1 +fi + +if [ -v SYSDIG_BPF_PROBE ] || [ "${1}" = "bpf" ]; then + load_bpf_probe +else + load_kernel_probe +fi diff --git a/test/csysdig_trace_regression.sh b/test/csysdig_trace_regression.sh new file mode 100644 index 0000000000..0284e3f502 --- /dev/null +++ b/test/csysdig_trace_regression.sh @@ -0,0 +1,77 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +#set -eu + +BASEDIR=. + +SYSDIG=$1 +CHISELS=$2 +#TMPBASE=${4:-$(mktemp -d --tmpdir sysdig.XXXXXXXXXX)} +TMPBASE=. +TRACEDIR="${TMPBASE}/traces" +RESULTDIR="${TMPBASE}/results" +BASELINEDIR="${TMPBASE}/baseline" +BRANCH=$3 + +if [ ! -d "$TRACEDIR" ]; then + mkdir -p $TRACEDIR + cd $TRACEDIR + wget https://s3.amazonaws.com/download.draios.com/sysdig-tests/traces.zip + unzip traces.zip + rm -rf traces.zip + cd - +fi + +if [ ! -d "$BASELINEDIR" ]; then + mkdir -p $BASELINEDIR + cd $BASELINEDIR + wget -O baseline.zip https://s3.amazonaws.com/download.draios.com/sysdig-tests/baseline-$BRANCH.zip || wget -O baseline.zip https://s3.amazonaws.com/download.draios.com/sysdig-tests/baseline-dev.zip + unzip baseline.zip + rm -rf baseline.zip + cd - +fi + +echo "Executing sysdig tests in ${TMPBASE}" + +ret=0 + +# Views to run +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vprocs" $TRACEDIR $RESULTDIR/procs $BASELINEDIR/procs || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vfiles" $TRACEDIR $RESULTDIR/files $BASELINEDIR/files || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vconnections" $TRACEDIR $RESULTDIR/connections $BASELINEDIR/connections || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vcontainer_errors" $TRACEDIR $RESULTDIR/container_errors $BASELINEDIR/container_errors || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vcontainers" $TRACEDIR $RESULTDIR/containers $BASELINEDIR/containers || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vdirectories" $TRACEDIR $RESULTDIR/directories $BASELINEDIR/directories || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -verrors" $TRACEDIR $RESULTDIR/errors $BASELINEDIR/errors || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vfile_opens" $TRACEDIR $RESULTDIR/file_opens $BASELINEDIR/file_opens || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vio_by_type" $TRACEDIR $RESULTDIR/io_by_type $BASELINEDIR/io_by_type || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vincoming_connections" $TRACEDIR $RESULTDIR/incoming_connections $BASELINEDIR/incoming_connections || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vincoming_connections" $TRACEDIR $RESULTDIR/incoming_connections $BASELINEDIR/incoming_connections || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vpage_faults" $TRACEDIR $RESULTDIR/page_faults $BASELINEDIR/page_faults || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vprocs_cpu" $TRACEDIR $RESULTDIR/procs_cpu $BASELINEDIR/procs_cpu || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vprocs_errors" $TRACEDIR $RESULTDIR/procs_errors $BASELINEDIR/procs_errors || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vprocs_fd_usage" $TRACEDIR $RESULTDIR/procs_fd_usage $BASELINEDIR/procs_fd_usage || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vsports" $TRACEDIR $RESULTDIR/sports $BASELINEDIR/sports || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vspy_syslog" $TRACEDIR $RESULTDIR/spy_syslog $BASELINEDIR/spy_syslog || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vspy_users" $TRACEDIR $RESULTDIR/spy_users $BASELINEDIR/spy_users || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "--raw -vsyscalls" $TRACEDIR $RESULTDIR/syscalls $BASELINEDIR/syscalls || ret=1 + +#rm -rf "${TMPBASE}" +exit $ret diff --git a/test/sysdig_batch_parser.sh b/test/sysdig_batch_parser.sh new file mode 100755 index 0000000000..dc7bea2893 --- /dev/null +++ b/test/sysdig_batch_parser.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +# +# This script runs sysdig on all the trace files (i.e. all the files with scap +# extension) in the current directory, and compares the result with the one of +# a previous run. +# +# Arguments: +# - sysdig path +# - sysdig chisels directory +# - sysdig command line +# - traces directory +# - prefix of the result directory (it will be completed with the current +# date/time) +# - directory to use as a reference +# +# Examples: +# ./sysdig_batch_parser.sh "-p%thread.exectime" exetime exetime_2014-04-28_10-18-30 +# ./sysdig_batch_parser.sh "-ctopconns" topconns topconns_2014-04-28_02-51-34 +# +# Note: +# if the comparison succeeds, the result directory is deleted. Otherwise, it's +# kept there for reference/analysis. +# +set -eu + +SYSDIG=$1 +SYSDIG_CHISEL_DIR=$2 +ARGS=$3 +TRACESDIR=$4 +DIRNAME=$5 +REFERENCEDIR=$6 + +export SYSDIG_CHISEL_DIR + +unamestr=`uname` +if [[ "$unamestr" == 'Linux' ]]; then + TIMEOUT_BIN="timeout" +elif [[ "$unamestr" == 'Darwin' ]]; then + TIMEOUT_BIN="gtimeout" +fi + +rm -rf $DIRNAME || true +mkdir -p $DIRNAME + +if [ ! -e $REFERENCEDIR ]; then + echo "Reference directory $REFERENCEDIR does not exist--skipping directory entirely" + exit 0 +fi + +for f in $TRACESDIR/* +do + ref=$REFERENCEDIR/$(basename $f).output; + if [ ! -e $ref ]; then + echo "Corresponding reference file $ref does not exist--skipping" + else + echo "Processing $f" + TZ=UTC eval ${TIMEOUT_BIN} 60 $SYSDIG -r $f $ARGS > $DIRNAME/$(basename $f).output + fi +done + +echo Data saved in $DIRNAME + +diff -r $DIRNAME $REFERENCEDIR +rm -rf $DIRNAME diff --git a/test/sysdig_trace_regression.sh b/test/sysdig_trace_regression.sh new file mode 100755 index 0000000000..3bde4f8f39 --- /dev/null +++ b/test/sysdig_trace_regression.sh @@ -0,0 +1,141 @@ +#!/bin/bash +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# + +set -exu + +unamestr=`uname` +if [[ "$unamestr" == 'Linux' ]]; then + SCRIPT=$(readlink -f $0) + TMPBASE=${4:-$(mktemp -d --tmpdir sysdig.XXXXXXXXXX)} +elif [[ "$unamestr" == 'Darwin' ]]; then + SCRIPT=$(greadlink -f $0) + unset TMPDIR #make shure that mktemp on mac will generate the folder under /tmp + TMPBASE=${4:-$(mktemp -d -t sysdig)} +fi + +BASEDIR=$(dirname $SCRIPT) +SYSDIG=$1 +CHISELS=$2 +TRACEDIR="${TMPBASE}/traces" +RESULTDIR="${TMPBASE}/results" +BASELINEDIR="${TMPBASE}/baseline" +BRANCH=$3 + +if [ ! -d "$TRACEDIR" ]; then + mkdir -p $TRACEDIR + cd $TRACEDIR + wget -O traces.zip https://s3.amazonaws.com/download.draios.com/sysdig-tests/traces-$BRANCH.zip || wget -O traces.zip https://s3.amazonaws.com/download.draios.com/sysdig-tests/traces.zip + unzip traces.zip + rm -rf traces.zip + cd - +fi + +if [ ! -d "$BASELINEDIR" ]; then + mkdir -p $BASELINEDIR + cd $BASELINEDIR + wget -O baseline.zip https://s3.amazonaws.com/download.draios.com/sysdig-tests/baseline-$BRANCH.zip || wget -O baseline.zip https://s3.amazonaws.com/download.draios.com/sysdig-tests/baseline-dev.zip + unzip baseline.zip + rm -rf baseline.zip + cd - +fi + +echo "Executing sysdig tests in ${TMPBASE}" + +ret=0 + +# Fields +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-n 1000" $TRACEDIR $RESULTDIR/default $BASELINEDIR/default || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-n 1000 -pc" $TRACEDIR $RESULTDIR/containers $BASELINEDIR/containers || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-n 1000 -t h" $TRACEDIR $RESULTDIR/time_human $BASELINEDIR/time_human || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-n 1000 -t a" $TRACEDIR $RESULTDIR/time_absolute $BASELINEDIR/time_absolute || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-n 1000 -t r" $TRACEDIR $RESULTDIR/time_relative $BASELINEDIR/time_relative || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-n 1000 -t d" $TRACEDIR $RESULTDIR/delta_enter $BASELINEDIR/delta_enter || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-n 1000 -t D" $TRACEDIR $RESULTDIR/delta_previous $BASELINEDIR/delta_previous || ret=1 +# Category: CPU Usage +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopcontainers_cpu" $TRACEDIR $RESULTDIR/topcontainers_cpu $BASELINEDIR/topcontainers_cpu || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopprocs_cpu" $TRACEDIR $RESULTDIR/topprocs_cpu $BASELINEDIR/topprocs_cpu || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopprocs_cpu" $TRACEDIR $RESULTDIR/topprocs_cpu_container $BASELINEDIR/topprocs_cpu_container || ret=1 +# Category: Errors +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopcontainers_error" $TRACEDIR $RESULTDIR/topcontainers_error $BASELINEDIR/topcontainers_error || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopfiles_errors" $TRACEDIR $RESULTDIR/topfiles_errors $BASELINEDIR/topfiles_errors || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopfiles_errors" $TRACEDIR $RESULTDIR/topfiles_errors_container $BASELINEDIR/topfiles_errors_container || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopprocs_errors" $TRACEDIR $RESULTDIR/topprocs_errors $BASELINEDIR/topprocs_errors || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopprocs_errors" $TRACEDIR $RESULTDIR/topprocs_errors_container $BASELINEDIR/topprocs_errors_container || ret=1 +# Category: I/O +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cecho_fds" $TRACEDIR $RESULTDIR/echo_fds $BASELINEDIR/echo_fds || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -cecho_fds" $TRACEDIR $RESULTDIR/echo_fds_container $BASELINEDIR/echo_fds_container || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cfdbytes_by fd.name" $TRACEDIR $RESULTDIR/fdbytes_by $BASELINEDIR/fdbytes_by || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cfdcount_by fd.name" $TRACEDIR $RESULTDIR/fdcount_by $BASELINEDIR/fdcount_by || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ciobytes" $TRACEDIR $RESULTDIR/iobytes $BASELINEDIR/iobytes || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ciobytes_file" $TRACEDIR $RESULTDIR/iobytes_file $BASELINEDIR/iobytes_file || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cspy_file" $TRACEDIR $RESULTDIR/spy_file $BASELINEDIR/spy_file || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cstderr" $TRACEDIR $RESULTDIR/stderr $BASELINEDIR/stderr || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -cstderr" $TRACEDIR $RESULTDIR/stderr_container $BASELINEDIR/stderr_container || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cstdin" $TRACEDIR $RESULTDIR/stdin $BASELINEDIR/stdin || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cstdout" $TRACEDIR $RESULTDIR/stdout $BASELINEDIR/stdout || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopcontainers_file" $TRACEDIR $RESULTDIR/topcontainers_file $BASELINEDIR/topcontainers_file || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopfiles_bytes" $TRACEDIR $RESULTDIR/topfiles_bytes $BASELINEDIR/topfiles_bytes || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopfiles_bytes" $TRACEDIR $RESULTDIR/topfiles_bytes_container $BASELINEDIR/topfiles_bytes_container || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopfiles_time" $TRACEDIR $RESULTDIR/topfiles_time $BASELINEDIR/topfiles_time || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopfiles_time" $TRACEDIR $RESULTDIR/topfiles_time_container $BASELINEDIR/topfiles_time_container || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopprocs_file" $TRACEDIR $RESULTDIR/topprocs_file $BASELINEDIR/topprocs_file || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopprocs_file" $TRACEDIR $RESULTDIR/topprocs_file_container $BASELINEDIR/topprocs_file_container || ret=1 +# Category: Logs +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cspy_logs" $TRACEDIR $RESULTDIR/spy_logs $BASELINEDIR/spy_logs || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -cspy_logs" $TRACEDIR $RESULTDIR/spy_logs_container $BASELINEDIR/spy_logs_container || ret=1 +# Category: Net +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ciobytes_net" $TRACEDIR $RESULTDIR/iobytes_net $BASELINEDIR/iobytes_net || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cspy_ip 127.0.0.1" $TRACEDIR $RESULTDIR/spy_ip $BASELINEDIR/spy_ip || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cspy_port 80" $TRACEDIR $RESULTDIR/spy_port $BASELINEDIR/spy_port || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopconns" $TRACEDIR $RESULTDIR/topconns $BASELINEDIR/topconns || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -ctopconns" $TRACEDIR $RESULTDIR/topconns_container $BASELINEDIR/topconns_container || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopcontainers_net" $TRACEDIR $RESULTDIR/topcontainers_net $BASELINEDIR/topcontainers_net || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopports_server" $TRACEDIR $RESULTDIR/topports_server $BASELINEDIR/topports_server || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopprocs_net" $TRACEDIR $RESULTDIR/topprocs_net $BASELINEDIR/topprocs_net || ret=1 +# Category: Performance +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cbottlenecks" $TRACEDIR $RESULTDIR/bottlenecks $BASELINEDIR/bottlenecks || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cfileslower 1000" $TRACEDIR $RESULTDIR/fileslower $BASELINEDIR/fileslower || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cnetlower 10" $TRACEDIR $RESULTDIR/netlower $BASELINEDIR/netlower || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cproc_exec_time" $TRACEDIR $RESULTDIR/proc_exec_time $BASELINEDIR/proc_exec_time || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cscallslower 1000" $TRACEDIR $RESULTDIR/scallslower $BASELINEDIR/scallslower || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopscalls" $TRACEDIR $RESULTDIR/topscalls $BASELINEDIR/topscalls || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-ctopscalls_time" $TRACEDIR $RESULTDIR/topscalls_time $BASELINEDIR/topscalls_time || ret=1 +# Category: Security +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-clist_login_shells" $TRACEDIR $RESULTDIR/list_login_shells $BASELINEDIR/list_login_shells || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cspy_users" $TRACEDIR $RESULTDIR/spy_users $BASELINEDIR/spy_users || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -cspy_users" $TRACEDIR $RESULTDIR/spy_users_container $BASELINEDIR/spy_users_container || ret=1 +# Category: System State +# $BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-clscontainers" $TRACEDIR $RESULTDIR/lscontainers $BASELINEDIR/lscontainers || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-clsof" $TRACEDIR $RESULTDIR/lsof $BASELINEDIR/lsof || ret=1 +# $BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cnetstat" $TRACEDIR $RESULTDIR/netstat $BASELINEDIR/netstat || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-cps" $TRACEDIR $RESULTDIR/ps $BASELINEDIR/ps || ret=1 +# JSON +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-j -n 10000" $TRACEDIR $RESULTDIR/fd_fields_json $BASELINEDIR/fd_fields_json || ret=1 +# Sessions +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-p '*%evt.num %evt.outputtime %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.info sid=%proc.sid sname=%proc.sname'" $TRACEDIR $RESULTDIR/sessions $BASELINEDIR/sessions || ret=1 +# Cwd +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-pc -p\"*%evt.num %evt.outputtime %evt.cpu %container.name (%container.id) %proc.name (%thread.tid:%thread.vtid) %evt.dir %evt.type %evt.info %proc.cwd\"" $TRACEDIR $RESULTDIR/cwd $BASELINEDIR/cwd || ret=1 + +# Testing filters/outputs that can traverse parent thread state +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "-p\"*%evt.num %evt.outputtime %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.info LS=%proc.loginshellid\"" $TRACEDIR $RESULTDIR/loginshell-parent-loop $BASELINEDIR/loginshell-parent-loop || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "proc.apid=10 or proc.apid=26890" $TRACEDIR $RESULTDIR/apid-parent-loop $BASELINEDIR/apid-parent-loop || ret=1 +$BASEDIR/sysdig_batch_parser.sh $SYSDIG $CHISELS "proc.aname=foo or proc.aname=sh" $TRACEDIR $RESULTDIR/aname-parent-loop $BASELINEDIR/aname-parent-loop || ret=1 + +rm -rf "${TMPBASE}" +exit $ret diff --git a/third-party/LuaJIT-2.0.2/COPYRIGHT b/third-party/LuaJIT-2.0.2/COPYRIGHT deleted file mode 100644 index 83ce94daee..0000000000 --- a/third-party/LuaJIT-2.0.2/COPYRIGHT +++ /dev/null @@ -1,56 +0,0 @@ -=============================================================================== -LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/ - -Copyright (C) 2005-2013 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -[ MIT license: http://www.opensource.org/licenses/mit-license.php ] - -=============================================================================== -[ LuaJIT includes code from Lua 5.1/5.2, which has this license statement: ] - -Copyright (C) 1994-2012 Lua.org, PUC-Rio. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -=============================================================================== -[ LuaJIT includes code from dlmalloc, which has this license statement: ] - -This is a version (aka dlmalloc) of malloc/free/realloc written by -Doug Lea and released to the public domain, as explained at -http://creativecommons.org/licenses/publicdomain - -=============================================================================== diff --git a/third-party/LuaJIT-2.0.2/Makefile b/third-party/LuaJIT-2.0.2/Makefile deleted file mode 100644 index 8883503f66..0000000000 --- a/third-party/LuaJIT-2.0.2/Makefile +++ /dev/null @@ -1,149 +0,0 @@ -############################################################################## -# LuaJIT top level Makefile for installation. Requires GNU Make. -# -# Please read doc/install.html before changing any variables! -# -# Suitable for POSIX platforms (Linux, *BSD, OSX etc.). -# Note: src/Makefile has many more configurable options. -# -# ##### This Makefile is NOT useful for Windows! ##### -# For MSVC, please follow the instructions given in src/msvcbuild.bat. -# For MinGW and Cygwin, cd to src and run make with the Makefile there. -# -# Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -############################################################################## - -MAJVER= 2 -MINVER= 0 -RELVER= 2 -VERSION= $(MAJVER).$(MINVER).$(RELVER) -ABIVER= 5.1 - -############################################################################## -# -# Change the installation path as needed. This automatically adjusts -# the paths in src/luaconf.h, too. Note: PREFIX must be an absolute path! -# -export PREFIX= /usr/local -############################################################################## - -DPREFIX= $(DESTDIR)$(PREFIX) -INSTALL_BIN= $(DPREFIX)/bin -INSTALL_LIB= $(DPREFIX)/lib -INSTALL_SHARE= $(DPREFIX)/share -INSTALL_INC= $(DPREFIX)/include/luajit-$(MAJVER).$(MINVER) - -INSTALL_LJLIBD= $(INSTALL_SHARE)/luajit-$(VERSION) -INSTALL_JITLIB= $(INSTALL_LJLIBD)/jit -INSTALL_LMODD= $(INSTALL_SHARE)/lua -INSTALL_LMOD= $(INSTALL_LMODD)/$(ABIVER) -INSTALL_CMODD= $(INSTALL_LIB)/lua -INSTALL_CMOD= $(INSTALL_CMODD)/$(ABIVER) -INSTALL_MAN= $(INSTALL_SHARE)/man/man1 -INSTALL_PKGCONFIG= $(INSTALL_LIB)/pkgconfig - -INSTALL_TNAME= luajit-$(VERSION) -INSTALL_TSYMNAME= luajit -INSTALL_ANAME= libluajit-$(ABIVER).a -INSTALL_SONAME= libluajit-$(ABIVER).so.$(MAJVER).$(MINVER).$(RELVER) -INSTALL_SOSHORT= libluajit-$(ABIVER).so -INSTALL_DYLIBNAME= libluajit-$(ABIVER).$(MAJVER).$(MINVER).$(RELVER).dylib -INSTALL_DYLIBSHORT1= libluajit-$(ABIVER).dylib -INSTALL_DYLIBSHORT2= libluajit-$(ABIVER).$(MAJVER).dylib -INSTALL_PCNAME= luajit.pc - -INSTALL_STATIC= $(INSTALL_LIB)/$(INSTALL_ANAME) -INSTALL_DYN= $(INSTALL_LIB)/$(INSTALL_SONAME) -INSTALL_SHORT1= $(INSTALL_LIB)/$(INSTALL_SOSHORT) -INSTALL_SHORT2= $(INSTALL_LIB)/$(INSTALL_SOSHORT) -INSTALL_T= $(INSTALL_BIN)/$(INSTALL_TNAME) -INSTALL_TSYM= $(INSTALL_BIN)/$(INSTALL_TSYMNAME) -INSTALL_PC= $(INSTALL_PKGCONFIG)/$(INSTALL_PCNAME) - -INSTALL_DIRS= $(INSTALL_BIN) $(INSTALL_LIB) $(INSTALL_INC) $(INSTALL_MAN) \ - $(INSTALL_PKGCONFIG) $(INSTALL_JITLIB) $(INSTALL_LMOD) $(INSTALL_CMOD) -UNINSTALL_DIRS= $(INSTALL_JITLIB) $(INSTALL_LJLIBD) $(INSTALL_INC) \ - $(INSTALL_LMOD) $(INSTALL_LMODD) $(INSTALL_CMOD) $(INSTALL_CMODD) - -RM= rm -f -MKDIR= mkdir -p -RMDIR= rmdir 2>/dev/null -SYMLINK= ln -sf -INSTALL_X= install -m 0755 -INSTALL_F= install -m 0644 -UNINSTALL= $(RM) -LDCONFIG= ldconfig -n -SED_PC= sed -e "s|^prefix=.*|prefix=$(PREFIX)|" - -FILE_T= luajit -FILE_A= libluajit.a -FILE_SO= libluajit.so -FILE_MAN= luajit.1 -FILE_PC= luajit.pc -FILES_INC= lua.h lualib.h lauxlib.h luaconf.h lua.hpp luajit.h -FILES_JITLIB= bc.lua v.lua dump.lua dis_x86.lua dis_x64.lua dis_arm.lua \ - dis_ppc.lua dis_mips.lua dis_mipsel.lua bcsave.lua vmdef.lua - -ifeq (,$(findstring Windows,$(OS))) - ifeq (Darwin,$(shell uname -s)) - INSTALL_SONAME= $(INSTALL_DYLIBNAME) - INSTALL_SHORT1= $(INSTALL_LIB)/$(INSTALL_DYLIBSHORT1) - INSTALL_SHORT2= $(INSTALL_LIB)/$(INSTALL_DYLIBSHORT2) - LDCONFIG= : - endif -endif - -############################################################################## - -INSTALL_DEP= src/luajit - -default all $(INSTALL_DEP): - @echo "==== Building LuaJIT $(VERSION) ====" - $(MAKE) -C src - @echo "==== Successfully built LuaJIT $(VERSION) ====" - -install: $(INSTALL_DEP) - @echo "==== Installing LuaJIT $(VERSION) to $(PREFIX) ====" - $(MKDIR) $(INSTALL_DIRS) - cd src && $(INSTALL_X) $(FILE_T) $(INSTALL_T) - cd src && test -f $(FILE_A) && $(INSTALL_F) $(FILE_A) $(INSTALL_STATIC) || : - $(RM) $(INSTALL_TSYM) $(INSTALL_DYN) $(INSTALL_SHORT1) $(INSTALL_SHORT2) - cd src && test -f $(FILE_SO) && \ - $(INSTALL_X) $(FILE_SO) $(INSTALL_DYN) && \ - $(LDCONFIG) $(INSTALL_LIB) && \ - $(SYMLINK) $(INSTALL_SONAME) $(INSTALL_SHORT1) && \ - $(SYMLINK) $(INSTALL_SONAME) $(INSTALL_SHORT2) || : - cd etc && $(INSTALL_F) $(FILE_MAN) $(INSTALL_MAN) - cd etc && $(SED_PC) $(FILE_PC) > $(FILE_PC).tmp && \ - $(INSTALL_F) $(FILE_PC).tmp $(INSTALL_PC) && \ - $(RM) $(FILE_PC).tmp - cd src && $(INSTALL_F) $(FILES_INC) $(INSTALL_INC) - cd src/jit && $(INSTALL_F) $(FILES_JITLIB) $(INSTALL_JITLIB) - $(SYMLINK) $(INSTALL_TNAME) $(INSTALL_TSYM) - @echo "==== Successfully installed LuaJIT $(VERSION) to $(PREFIX) ====" - -uninstall: - @echo "==== Uninstalling LuaJIT $(VERSION) from $(PREFIX) ====" - $(UNINSTALL) $(INSTALL_TSYM) $(INSTALL_T) $(INSTALL_STATIC) $(INSTALL_DYN) $(INSTALL_SHORT1) $(INSTALL_SHORT2) $(INSTALL_MAN)/$(FILE_MAN) $(INSTALL_PC) - for file in $(FILES_JITLIB); do \ - $(UNINSTALL) $(INSTALL_JITLIB)/$$file; \ - done - for file in $(FILES_INC); do \ - $(UNINSTALL) $(INSTALL_INC)/$$file; \ - done - $(LDCONFIG) $(INSTALL_LIB) - $(RMDIR) $(UNINSTALL_DIRS) || : - @echo "==== Successfully uninstalled LuaJIT $(VERSION) from $(PREFIX) ====" - -############################################################################## - -amalg: - @echo "Building LuaJIT $(VERSION)" - $(MAKE) -C src amalg - -clean: - $(MAKE) -C src clean - -.PHONY: all install amalg clean - -############################################################################## diff --git a/third-party/LuaJIT-2.0.2/README b/third-party/LuaJIT-2.0.2/README deleted file mode 100644 index d837fd2ff6..0000000000 --- a/third-party/LuaJIT-2.0.2/README +++ /dev/null @@ -1,16 +0,0 @@ -README for LuaJIT 2.0.2 ------------------------ - -LuaJIT is a Just-In-Time (JIT) compiler for the Lua programming language. - -Project Homepage: http://luajit.org/ - -LuaJIT is Copyright (C) 2005-2013 Mike Pall. -LuaJIT is free software, released under the MIT license. -See full Copyright Notice in the COPYRIGHT file or in luajit.h. - -Documentation for LuaJIT is available in HTML format. -Please point your favorite browser to: - - doc/luajit.html - diff --git a/third-party/LuaJIT-2.0.2/doc/bluequad-print.css b/third-party/LuaJIT-2.0.2/doc/bluequad-print.css deleted file mode 100644 index 41ae7575b6..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/bluequad-print.css +++ /dev/null @@ -1,166 +0,0 @@ -/* Copyright (C) 2004-2013 Mike Pall. - * - * You are welcome to use the general ideas of this design for your own sites. - * But please do not steal the stylesheet, the layout or the color scheme. - */ -body { - font-family: serif; - font-size: 11pt; - margin: 0 3em; - padding: 0; - border: none; -} -a:link, a:visited, a:hover, a:active { - text-decoration: none; - background: transparent; - color: #0000ff; -} -h1, h2, h3 { - font-family: sans-serif; - font-weight: bold; - text-align: left; - margin: 0.5em 0; - padding: 0; -} -h1 { - font-size: 200%; -} -h2 { - font-size: 150%; -} -h3 { - font-size: 125%; -} -p { - margin: 0 0 0.5em 0; - padding: 0; -} -ul, ol { - margin: 0.5em 0; - padding: 0 0 0 2em; -} -ul { - list-style: outside square; -} -ol { - list-style: outside decimal; -} -li { - margin: 0; - padding: 0; -} -dl { - margin: 1em 0; - padding: 1em; - border: 1px solid black; -} -dt { - font-weight: bold; - margin: 0; - padding: 0; -} -dt sup { - float: right; - margin-left: 1em; -} -dd { - margin: 0.5em 0 0 2em; - padding: 0; -} -table { - table-layout: fixed; - width: 100%; - margin: 1em 0; - padding: 0; - border: 1px solid black; - border-spacing: 0; - border-collapse: collapse; -} -tr { - margin: 0; - padding: 0; - border: none; -} -td { - text-align: left; - margin: 0; - padding: 0.2em 0.5em; - border-top: 1px solid black; - border-bottom: 1px solid black; -} -tr.separate td { - border-top: double; -} -tt, pre, code, kbd, samp { - font-family: monospace; - font-size: 75%; -} -kbd { - font-weight: bolder; -} -blockquote, pre { - margin: 1em 2em; - padding: 0; -} -img { - border: none; - vertical-align: baseline; - margin: 0; - padding: 0; -} -img.left { - float: left; - margin: 0.5em 1em 0.5em 0; -} -img.right { - float: right; - margin: 0.5em 0 0.5em 1em; -} -.flush { - clear: both; - visibility: hidden; -} -.hide, .noprint, #nav { - display: none !important; -} -.pagebreak { - page-break-before: always; -} -#site { - text-align: right; - font-family: sans-serif; - font-weight: bold; - margin: 0 1em; - border-bottom: 1pt solid black; -} -#site a { - font-size: 1.2em; -} -#site a:link, #site a:visited { - text-decoration: none; - font-weight: bold; - background: transparent; - color: #ffffff; -} -#logo { - color: #ff8000; -} -#head { - clear: both; - margin: 0 1em; -} -#main { - line-height: 1.3; - text-align: justify; - margin: 1em; -} -#foot { - clear: both; - font-size: 80%; - text-align: center; - margin: 0 1.25em; - padding: 0.5em 0 0 0; - border-top: 1pt solid black; - page-break-before: avoid; - page-break-after: avoid; -} diff --git a/third-party/LuaJIT-2.0.2/doc/bluequad.css b/third-party/LuaJIT-2.0.2/doc/bluequad.css deleted file mode 100644 index 5e8d5ce772..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/bluequad.css +++ /dev/null @@ -1,325 +0,0 @@ -/* Copyright (C) 2004-2013 Mike Pall. - * - * You are welcome to use the general ideas of this design for your own sites. - * But please do not steal the stylesheet, the layout or the color scheme. - */ -/* colorscheme: - * - * site | head #4162bf/white | #6078bf/#e6ecff - * ------+------ ----------------+------------------- - * nav | main #bfcfff | #e6ecff/black - * - * nav: hiback loback #c5d5ff #b9c9f9 - * hiborder loborder #e6ecff #97a7d7 - * link hover #2142bf #ff0000 - * - * link: link visited hover #2142bf #8122bf #ff0000 - * - * main: boxback boxborder #f0f4ff #bfcfff - */ -body { - font-family: Verdana, Arial, Helvetica, sans-serif; - font-size: 10pt; - margin: 0; - padding: 0; - border: none; - background: #e0e0e0; - color: #000000; -} -a:link { - text-decoration: none; - background: transparent; - color: #2142bf; -} -a:visited { - text-decoration: none; - background: transparent; - color: #8122bf; -} -a:hover, a:active { - text-decoration: underline; - background: transparent; - color: #ff0000; -} -h1, h2, h3 { - font-weight: bold; - text-align: left; - margin: 0.5em 0; - padding: 0; - background: transparent; -} -h1 { - font-size: 200%; - line-height: 3em; /* really 6em relative to body, match #site span */ - margin: 0; -} -h2 { - font-size: 150%; - color: #606060; -} -h3 { - font-size: 125%; - color: #404040; -} -p { - max-width: 600px; - margin: 0 0 0.5em 0; - padding: 0; -} -b { - color: #404040; -} -ul, ol { - max-width: 600px; - margin: 0.5em 0; - padding: 0 0 0 2em; -} -ul { - list-style: outside square; -} -ol { - list-style: outside decimal; -} -li { - margin: 0; - padding: 0; -} -dl { - max-width: 600px; - margin: 1em 0; - padding: 1em; - border: 1px solid #bfcfff; - background: #f0f4ff; -} -dt { - font-weight: bold; - margin: 0; - padding: 0; -} -dt sup { - float: right; - margin-left: 1em; - color: #808080; -} -dt a:visited { - text-decoration: none; - color: #2142bf; -} -dt a:hover, dt a:active { - text-decoration: none; - color: #ff0000; -} -dd { - margin: 0.5em 0 0 2em; - padding: 0; -} -div.tablewrap { /* for IE *sigh* */ - max-width: 600px; -} -table { - table-layout: fixed; - border-spacing: 0; - border-collapse: collapse; - max-width: 600px; - width: 100%; - margin: 1em 0; - padding: 0; - border: 1px solid #bfcfff; -} -tr { - margin: 0; - padding: 0; - border: none; -} -tr.odd { - background: #f0f4ff; -} -tr.separate td { - border-top: 1px solid #bfcfff; -} -td { - text-align: left; - margin: 0; - padding: 0.2em 0.5em; - border: none; -} -tt, code, kbd, samp { - font-family: Courier New, Courier, monospace; - line-height: 1.2; - font-size: 110%; -} -kbd { - font-weight: bolder; -} -blockquote, pre { - max-width: 600px; - margin: 1em 2em; - padding: 0; -} -pre { - line-height: 1.1; -} -pre.code { - line-height: 1.4; - margin: 0.5em 0 1em 0.5em; - padding: 0.5em 1em; - border: 1px solid #bfcfff; - background: #f0f4ff; -} -pre.mark { - padding-left: 2em; -} -span.codemark { - position:absolute; - left: 16em; - color: #4040c0; -} -span.mark { - color: #4040c0; - font-family: Courier New, Courier, monospace; - line-height: 1.1; -} -img { - border: none; - vertical-align: baseline; - margin: 0; - padding: 0; -} -img.left { - float: left; - margin: 0.5em 1em 0.5em 0; -} -img.right { - float: right; - margin: 0.5em 0 0.5em 1em; -} -.indent { - padding-left: 1em; -} -.flush { - clear: both; - visibility: hidden; -} -.hide, .noscreen { - display: none !important; -} -.ext { - color: #ff8000; -} -.new { - font-size: 6pt; - vertical-align: middle; - background: #ff8000; - color: #ffffff; -} -#site { - clear: both; - float: left; - width: 13em; - text-align: center; - font-weight: bold; - margin: 0; - padding: 0; - background: transparent; - color: #ffffff; -} -#site a { - font-size: 200%; -} -#site a:link, #site a:visited { - text-decoration: none; - font-weight: bold; - background: transparent; - color: #ffffff; -} -#site span { - line-height: 3em; /* really 6em relative to body, match h1 */ -} -#logo { - color: #ffb380; -} -#head { - margin: 0; - padding: 0 0 0 2em; - border-left: solid 13em #4162bf; - border-right: solid 3em #6078bf; - background: #6078bf; - color: #e6ecff; -} -#nav { - clear: both; - float: left; - overflow: hidden; - text-align: left; - line-height: 1.5; - width: 13em; - padding-top: 1em; - background: transparent; -} -#nav ul { - list-style: none outside; - margin: 0; - padding: 0; -} -#nav li { - margin: 0; - padding: 0; -} -#nav a { - display: block; - text-decoration: none; - font-weight: bold; - margin: 0; - padding: 2px 1em; - border-top: 1px solid transparent; - border-bottom: 1px solid transparent; - background: transparent; - color: #2142bf; -} -#nav a:hover, #nav a:active { - text-decoration: none; - border-top: 1px solid #97a7d7; - border-bottom: 1px solid #e6ecff; - background: #b9c9f9; - color: #ff0000; -} -#nav a.current, #nav a.current:hover, #nav a.current:active { - border-top: 1px solid #e6ecff; - border-bottom: 1px solid #97a7d7; - background: #c5d5ff; - color: #2142bf; -} -#nav ul ul a { - padding: 0 1em 0 1.7em; -} -#nav ul ul ul a { - padding: 0 0.5em 0 2.4em; -} -#main { - line-height: 1.5; - text-align: left; - margin: 0; - padding: 1em 2em; - border-left: solid 13em #bfcfff; - border-right: solid 3em #e6ecff; - background: #e6ecff; -} -#foot { - clear: both; - font-size: 80%; - text-align: center; - margin: 0; - padding: 0.5em; - background: #6078bf; - color: #ffffff; -} -#foot a:link, #foot a:visited { - text-decoration: underline; - background: transparent; - color: #ffffff; -} -#foot a:hover, #foot a:active { - text-decoration: underline; - background: transparent; - color: #bfcfff; -} diff --git a/third-party/LuaJIT-2.0.2/doc/changes.html b/third-party/LuaJIT-2.0.2/doc/changes.html deleted file mode 100644 index b3deeaf2e3..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/changes.html +++ /dev/null @@ -1,892 +0,0 @@ - - - -LuaJIT Change History - - - - - - - - - -
-Lua -
- - -
-

-This is a list of changes between the released versions of LuaJIT.
-The current stable version is LuaJIT 2.0.2.
-

-

-Please check the -» Online Change History -to see whether newer versions are available. -

- -
-

LuaJIT 2.0.2 — 2013-06-03

-
    -
  • Fix memory access check for fast string interning.
  • -
  • Fix MSVC intrinsics for older versions.
  • -
  • Add missing GC steps for io.* functions.
  • -
  • Fix spurious red zone overflows in machine code generation.
  • -
  • Fix jump-range constrained mcode allocation.
  • -
  • Inhibit DSE for implicit loads via calls.
  • -
  • Fix builtin string to number conversion for overflow digits.
  • -
  • Fix optional argument handling while recording builtins.
  • -
  • Fix optional argument handling in table.concat().
  • -
  • Add partial support for building with MingW64 GCC 4.8-SEH.
  • -
  • Add missing PHI barrier to string.sub(str, a, b) == kstr FOLD rule.
  • -
  • Fix compatibility issues with Illumos.
  • -
  • ARM: Fix cache flush/sync for exit stubs of JIT-compiled code.
  • -
  • MIPS: Fix cache flush/sync for JIT-compiled code jump area.
  • -
  • PPC: Add plt suffix for external calls from assembler code.
  • -
  • FFI: Fix snapshot substitution in SPLIT pass.
  • -
  • FFI/x86: Fix register allocation for 64 bit comparisons.
  • -
  • FFI: Fix tailcall in lowest frame to C function with bool result.
  • -
  • FFI: Ignore long type specifier in ffi.istype().
  • -
  • FFI: Fix calling conventions for 32 bit OSX and iOS simulator (struct returns).
  • -
  • FFI: Fix calling conventions for ARM hard-float EABI (nested structs).
  • -
  • FFI: Improve error messages for arithmetic and comparison operators.
  • -
  • FFI: Insert no-op type conversion for pointer to integer cast.
  • -
  • FFI: Fix unroll limit for ffi.fill().
  • -
  • FFI: Must sink XBAR together with XSTOREs.
  • -
  • FFI: Preserve intermediate string for const char * conversion.
  • -
- -

LuaJIT 2.0.1 — 2013-02-19

-
    -
  • Don't clear frame for out-of-memory error.
  • -
  • Leave hook when resume catches error thrown from hook.
  • -
  • Add missing GC steps for template table creation.
  • -
  • Fix discharge order of comparisons in Lua parser.
  • -
  • Improve buffer handling for io.read().
  • -
  • OSX: Add support for Mach-O object files to -b option.
  • -
  • Fix PS3 port.
  • -
  • Fix/enable Xbox 360 port.
  • -
  • x86/x64: Always mark ref for shift count as non-weak.
  • -
  • x64: Don't fuse implicitly 32-to-64 extended operands.
  • -
  • ARM: Fix armhf call argument handling.
  • -
  • ARM: Fix code generation for integer math.min/math.max.
  • -
  • PPC/e500: Fix lj_vm_floor() for Inf/NaN.
  • -
  • FFI: Change priority of table initializer variants for structs.
  • -
  • FFI: Fix code generation for bool call result check on x86/x64.
  • -
  • FFI: Load FFI library on-demand for bytecode with cdata literals.
  • -
  • FFI: Fix handling of qualified transparent structs/unions.
  • -
- -

LuaJIT 2.0.0 — 2012-11-08

-
    -
  • Correctness and completeness: -
      -
    • Fix Android/x86 build.
    • -
    • Fix recording of equality comparisons with __eq metamethods.
    • -
    • Fix detection of immutable upvalues.
    • -
    • Replace error with PANIC for callbacks from JIT-compiled code.
    • -
    • Fix builtin string to number conversion for INT_MIN.
    • -
    • Don't create unneeded array part for template tables.
    • -
    • Fix CONV.num.int sinking.
    • -
    • Don't propagate implicitly widened number to index metamethods.
    • -
    • ARM: Fix ordered comparisons of number vs. non-number.
    • -
    • FFI: Fix code generation for replay of sunk float fields.
    • -
    • FFI: Fix signedness of bool.
    • -
    • FFI: Fix recording of bool call result check on x86/x64.
    • -
    • FFI: Fix stack-adjustment for __thiscall callbacks.
    • -
  • -
- -

LuaJIT 2.0.0-beta11 — 2012-10-16

-
    -
  • New features: -
      -
    • Use ARM VFP instructions, if available (build-time detection).
    • -
    • Add support for ARM hard-float EABI (armhf).
    • -
    • Add PS3 port.
    • -
    • Add many features from Lua 5.2, e.g. goto/labels. - Refer to this list.
    • -
    • FFI: Add parameterized C types.
    • -
    • FFI: Add support for copy constructors.
    • -
    • FFI: Equality comparisons never raise an error (treat as unequal instead).
    • -
    • FFI: Box all accessed or returned enums.
    • -
    • FFI: Check for __new metamethod when calling a constructor.
    • -
    • FFI: Handle __pairs/__ipairs metamethods for cdata objects.
    • -
    • FFI: Convert io.* file handle to FILE * pointer (but as a void *).
    • -
    • FFI: Detect and support type punning through unions.
    • -
    • FFI: Improve various error messages.
    • -
  • -
  • Build-system reorganization: -
      -
    • Reorganize directory layout:
      - lib/*src/jit/*
      - src/buildvm_*.dascsrc/vm_*.dasc
      - src/buildvm_*.h → removed
      - src/buildvm*src/host/*
    • -
    • Add minified Lua interpreter plus Lua BitOp (minilua) to run DynASM.
    • -
    • Change DynASM bit operations to use Lua BitOp
    • -
    • Translate only vm_*.dasc for detected target architecture.
    • -
    • Improve target detection for msvcbuild.bat.
    • -
    • Fix build issues on Cygwin and MinGW with optional MSys.
    • -
    • Handle cross-compiles with FPU/no-FPU or hard-fp/soft-fp ABI mismatch.
    • -
    • Remove some library functions for no-JIT/no-FFI builds.
    • -
    • Add uninstall target to top-level Makefile.
    • -
  • -
  • Correctness and completeness: -
      -
    • Preserve snapshot #0 PC for all traces.
    • -
    • Fix argument checks for coroutine.create().
    • -
    • Command line prints version and JIT status to stdout, not stderr.
    • -
    • Fix userdata __gc separations at Lua state close.
    • -
    • Fix TDUP to HLOAD forwarding for LJ_DUALNUM builds.
    • -
    • Fix buffer check in bytecode writer.
    • -
    • Make os.date() thread-safe.
    • -
    • Add missing declarations for MSVC intrinsics.
    • -
    • Fix dispatch table modifications for return hooks.
    • -
    • Workaround for MSVC conversion bug (doubleuint32_tint32_t).
    • -
    • Fix FOLD rule (i-j)-i => 0-j.
    • -
    • Never use DWARF unwinder on Windows.
    • -
    • Fix shrinking of direct mapped blocks in builtin allocator.
    • -
    • Limit recursion depth in string.match() et al.
    • -
    • Fix late despecialization of ITERN after loop has been entered.
    • -
    • Fix 'f' and 'L' options for debug.getinfo() and lua_getinfo().
    • -
    • Fix package.searchpath().
    • -
    • OSX: Change dylib names to be consistent with other platforms.
    • -
    • Android: Workaround for broken sprintf("%g", -0.0).
    • -
    • x86: Remove support for ancient CPUs without CMOV (before Pentium Pro).
    • -
    • x86: Fix register allocation for calls returning register pair.
    • -
    • x86/x64: Fix fusion of unsigned byte comparisons with swapped operands.
    • -
    • ARM: Fix tonumber() argument check.
    • -
    • ARM: Fix modulo operator and math.floor()/math.ceil() for inf/nan.
    • -
    • ARM: Invoke SPLIT pass for leftover IR_TOBIT.
    • -
    • ARM: Fix BASE register coalescing.
    • -
    • PPC: Fix interpreter state setup in callbacks.
    • -
    • PPC: Fix string.sub() range check.
    • -
    • MIPS: Support generation of MIPS/MIPSEL bytecode object files.
    • -
    • MIPS: Fix calls to floor()/ceil()/trunc().
    • -
    • ARM/PPC: Detect more target architecture variants.
    • -
    • ARM/PPC/e500/MIPS: Fix tailcalls from fast functions, esp. tostring().
    • -
    • ARM/PPC/MIPS: Fix rematerialization of FP constants.
    • -
    • FFI: Don't call FreeLibrary() on our own EXE/DLL.
    • -
    • FFI: Resolve metamethods for constructors, too.
    • -
    • FFI: Properly disable callbacks on iOS (would require executable memory).
    • -
    • FFI: Fix cdecl string parsing during recording.
    • -
    • FFI: Show address pointed to for tostring(ref), too.
    • -
    • FFI: Fix alignment of C call argument/return structure.
    • -
    • FFI: Initialize all fields of standard types.
    • -
    • FFI: Fix callback handling when new C types are declared in callback.
    • -
    • FFI: Fix recording of constructors for pointers.
    • -
    • FFI: Always resolve metamethods for pointers to structs.
    • -
    • FFI: Correctly propagate alignment when interning nested types.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • Add allocation sinking and store sinking optimization.
    • -
    • Constify immutable upvalues.
    • -
    • Add builtin string to integer or FP number conversion. Improves cross-platform consistency and correctness.
    • -
    • Create string hash slots in template tables for non-const values, too. Avoids later table resizes.
    • -
    • Eliminate HREFK guard for template table references.
    • -
    • Add various new FOLD rules.
    • -
    • Don't use stack unwinding for lua_yield() (slow on x64).
    • -
    • ARM, PPC, MIPS: Improve XLOAD operand fusion and register hinting.
    • -
    • PPC, MIPS: Compile math.sqrt() to sqrt instruction, if available.
    • -
    • FFI: Fold KPTR + constant offset in SPLIT pass.
    • -
    • FFI: Optimize/inline ffi.copy() and ffi.fill().
    • -
    • FFI: Compile and optimize array/struct copies.
    • -
    • FFI: Compile ffi.typeof(cdata|ctype), ffi.sizeof(), ffi.alignof(), ffi.offsetof() and ffi.gc().
    • -
  • -
- -

LuaJIT 2.0.0-beta10 — 2012-05-09

-
    -
  • New features: -
      -
    • The MIPS of LuaJIT is complete. It requires a CPU conforming to the -MIPS32 R1 architecture with hardware FPU. O32 hard-fp ABI, -little-endian or big-endian.
    • -
    • Auto-detect target arch via cross-compiler. No need for -TARGET=arch anymore.
    • -
    • Make DynASM compatible with Lua 5.2.
    • -
    • From Lua 5.2: Try __tostring metamethod on non-string error -messages..
    • -
  • -
  • Correctness and completeness: -
      -
    • Fix parsing of hex literals with exponents.
    • -
    • Fix bytecode dump for certain number constants.
    • -
    • Fix argument type in error message for relative arguments.
    • -
    • Fix argument error handling on Lua stacks without a frame.
    • -
    • Add missing mcode limit check in assembler backend.
    • -
    • Fix compilation on OpenBSD.
    • -
    • Avoid recursive GC steps after GC-triggered trace exit.
    • -
    • Replace <unwind.h> definitions with our own.
    • -
    • Fix OSX build issues. Bump minimum required OSX version to 10.4.
    • -
    • Fix discharge order of comparisons in Lua parser.
    • -
    • Ensure running __gc of userdata created in __gc -at state close.
    • -
    • Limit number of userdata __gc separations at state close.
    • -
    • Fix bytecode JMP slot range when optimizing -and/or with constant LHS.
    • -
    • Fix DSE of USTORE.
    • -
    • Make lua_concat() work from C hook with partial frame.
    • -
    • Add required PHIs for implicit conversions, e.g. via XREF -forwarding.
    • -
    • Add more comparison variants to Valgrind suppressions file.
    • -
    • Disable loading bytecode with an extra header (BOM or #!).
    • -
    • Fix PHI stack slot syncing.
    • -
    • ARM: Reorder type/value tests to silence Valgrind.
    • -
    • ARM: Fix register allocation for ldrd-optimized -HREFK.
    • -
    • ARM: Fix conditional branch fixup for OBAR.
    • -
    • ARM: Invoke SPLIT pass for double args in FFI call.
    • -
    • ARM: Handle all CALL* ops with double results in -SPLIT pass.
    • -
    • ARM: Fix rejoin of POW in SPLIT pass.
    • -
    • ARM: Fix compilation of math.sinh, math.cosh, -math.tanh.
    • -
    • ARM, PPC: Avoid pointless arg clearing in BC_IFUNCF.
    • -
    • PPC: Fix resume after yield from hook.
    • -
    • PPC: Fix argument checking for rawget().
    • -
    • PPC: Fix fusion of floating-point XLOAD/XSTORE.
    • -
    • PPC: Fix HREFK code generation for huge tables.
    • -
    • PPC: Use builtin D-Cache/I-Cache sync code.
    • -
  • -
  • FFI library: -
      -
    • Ignore empty statements in ffi.cdef().
    • -
    • Ignore number parsing errors while skipping definitions.
    • -
    • Don't touch frame in callbacks with tailcalls to fast functions.
    • -
    • Fix library unloading on POSIX systems.
    • -
    • Finalize cdata before userdata when closing the state.
    • -
    • Change ffi.load() library name resolution for Cygwin.
    • -
    • Fix resolving of function name redirects on Windows/x86.
    • -
    • Fix symbol resolving error messages on Windows.
    • -
    • Fix blacklisting of C functions calling callbacks.
    • -
    • Fix result type of pointer difference.
    • -
    • Use correct PC in FFI metamethod error message.
    • -
    • Allow 'typedef _Bool int BOOL;' for the Windows API.
    • -
    • Don't record test for bool result of call, if ignored.
    • -
  • -
- -

LuaJIT 2.0.0-beta9 — 2011-12-14

-
    -
  • New features: -
      -
    • PPC port of LuaJIT is complete. Default is the dual-number port -(usually faster). Single-number port selectable via src/Makefile -at build time.
    • -
    • Add FFI callback support.
    • -
    • Extend -b to generate .c, .h or .obj/.o -files with embedded bytecode.
    • -
    • Allow loading embedded bytecode with require().
    • -
    • From Lua 5.2: Change to '\z' escape. Reject undefined escape -sequences.
    • -
  • -
  • Correctness and completeness: -
      -
    • Fix OSX 10.7 build. Fix install_name and versioning on OSX.
    • -
    • Fix iOS build.
    • -
    • Install dis_arm.lua, too.
    • -
    • Mark installed shared library as executable.
    • -
    • Add debug option to msvcbuild.bat and improve error handling.
    • -
    • Fix data-flow analysis for iterators.
    • -
    • Fix forced unwinding triggered by external unwinder.
    • -
    • Record missing for loop slot loads (return to lower frame).
    • -
    • Always use ANSI variants of Windows system functions.
    • -
    • Fix GC barrier for multi-result table constructor (TSETM).
    • -
    • Fix/add various FOLD rules.
    • -
    • Add potential PHI for number conversions due to type instability.
    • -
    • Do not eliminate PHIs only referenced from other PHIs.
    • -
    • Correctly anchor implicit number to string conversions in Lua/C API.
    • -
    • Fix various stack limit checks.
    • -
    • x64: Use thread-safe exceptions for external unwinding (GCC platforms).
    • -
    • x64: Fix result type of cdata index conversions.
    • -
    • x64: Fix math.random() and bit.bswap() code generation.
    • -
    • x64: Fix lightuserdata comparisons.
    • -
    • x64: Always extend stack-passed arguments to pointer size.
    • -
    • ARM: Many fixes to code generation backend.
    • -
    • PPC/e500: Fix dispatch for binop metamethods.
    • -
    • PPC/e500: Save/restore condition registers when entering/leaving the VM.
    • -
    • PPC/e500: Fix write barrier in stores of strings to upvalues.
    • -
  • -
  • FFI library: -
      -
    • Fix C comment parsing.
    • -
    • Fix snapshot optimization for cdata comparisons.
    • -
    • Fix recording of const/enum lookups in namespaces.
    • -
    • Fix call argument and return handling for I8/U8/I16/U16 types.
    • -
    • Fix unfused loads of float fields.
    • -
    • Fix ffi.string() recording.
    • -
    • Save GetLastError() around ffi.load() and symbol -resolving, too.
    • -
    • Improve ld script detection in ffi.load().
    • -
    • Record loads/stores to external variables in namespaces.
    • -
    • Compile calls to stdcall, fastcall and vararg functions.
    • -
    • Treat function ctypes like pointers in comparisons.
    • -
    • Resolve __call metamethod for pointers, too.
    • -
    • Record C function calls with bool return values.
    • -
    • Record ffi.errno().
    • -
    • x86: Fix number to uint32_t conversion rounding.
    • -
    • x86: Fix 64 bit arithmetic in assembler backend.
    • -
    • x64: Fix struct-by-value calling conventions.
    • -
    • ARM: Ensure invocation of SPLIT pass for float conversions.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • Display trace types with -jv and -jdump.
    • -
    • Record isolated calls. But prefer recording loops over calls.
    • -
    • Specialize to prototype for non-monomorphic functions. Solves the -trace-explosion problem for closure-heavy programming styles.
    • -
    • Always generate a portable vmdef.lua. Easier for distros.
    • -
  • -
- -

LuaJIT 2.0.0-beta8 — 2011-06-23

-
    -
  • New features: -
      -
    • Soft-float ARM port of LuaJIT is complete.
    • -
    • Add support for bytecode loading/saving and -b command line -option.
    • -
    • From Lua 5.2: __len metamethod for tables -(disabled by default).
    • -
  • -
  • Correctness and completeness: -
      -
    • ARM: Misc. fixes for interpreter.
    • -
    • x86/x64: Fix bit.* argument checking in interpreter.
    • -
    • Catch early out-of-memory in memory allocator initialization.
    • -
    • Fix data-flow analysis for paths leading to an upvalue close.
    • -
    • Fix check for missing arguments in string.format().
    • -
    • Fix Solaris/x86 build (note: not a supported target).
    • -
    • Fix recording of loops with instable directions in side traces.
    • -
    • x86/x64: Fix fusion of comparisons with u8/u16 -XLOAD.
    • -
    • x86/x64: Fix register allocation for variable shifts.
    • -
  • -
  • FFI library: -
      -
    • Add ffi.errno(). Save errno/GetLastError() -around allocations etc.
    • -
    • Fix __gc for VLA/VLS cdata objects.
    • -
    • Fix recording of casts from 32 bit cdata pointers to integers.
    • -
    • tonumber(cdata) returns nil for non-numbers.
    • -
    • Show address pointed to for tostring(pointer).
    • -
    • Print NULL pointers as "cdata<... *>: NULL".
    • -
    • Support __tostring metamethod for pointers to structs, too.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • More tuning for loop unrolling heuristics.
    • -
    • Flatten and compress in-memory debug info (saves ~70%).
    • -
  • -
- -

LuaJIT 2.0.0-beta7 — 2011-05-05

-
    -
  • New features: -
      -
    • ARM port of the LuaJIT interpreter is complete.
    • -
    • FFI library: Add ffi.gc(), ffi.metatype(), -ffi.istype().
    • -
    • FFI library: Resolve ld script redirection in ffi.load().
    • -
    • From Lua 5.2: package.searchpath(), fp:read("*L"), -load(string).
    • -
    • From Lua 5.2, disabled by default: empty statement, -table.unpack(), modified coroutine.running().
    • -
  • -
  • Correctness and completeness: -
      -
    • FFI library: numerous fixes.
    • -
    • Fix type mismatches in store-to-load forwarding.
    • -
    • Fix error handling within metamethods.
    • -
    • Fix table.maxn().
    • -
    • Improve accuracy of x^-k on x64.
    • -
    • Fix code generation for Intel Atom in x64 mode.
    • -
    • Fix narrowing of POW.
    • -
    • Fix recording of retried fast functions.
    • -
    • Fix code generation for bit.bnot() and multiplies.
    • -
    • Fix error location within cpcall frames.
    • -
    • Add workaround for old libgcc unwind bug.
    • -
    • Fix lua_yield() and getmetatable(lightuserdata) on x64.
    • -
    • Misc. fixes for PPC/e500 interpreter.
    • -
    • Fix stack slot updates for down-recursion.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • Add dual-number mode (int/double) for the VM. Enabled for ARM.
    • -
    • Improve narrowing of arithmetic operators and for loops.
    • -
    • Tune loop unrolling heuristics and increase trace recorder limits.
    • -
    • Eliminate dead slots in snapshots using bytecode data-flow analysis.
    • -
    • Avoid phantom stores to proxy tables.
    • -
    • Optimize lookups in empty proxy tables.
    • -
    • Improve bytecode optimization of and/or operators.
    • -
  • -
- -

LuaJIT 2.0.0-beta6 — 2011-02-11

-
    -
  • New features: -
      -
    • PowerPC/e500v2 port of the LuaJIT interpreter is complete.
    • -
    • Various minor features from Lua 5.2: Hex escapes in literals, -'\*' escape, reversible string.format("%q",s), -"%g" pattern, table.sort checks callbacks, -os.exit(status|true|false[,close]).
    • -
    • Lua 5.2 __pairs and __ipairs metamethods -(disabled by default).
    • -
    • Initial release of the FFI library.
    • -
  • -
  • Correctness and completeness: -
      -
    • Fix string.format() for non-finite numbers.
    • -
    • Fix memory leak when compiled to use the built-in allocator.
    • -
    • x86/x64: Fix unnecessary resize in TSETM bytecode.
    • -
    • Fix various GC issues with traces and jit.flush().
    • -
    • x64: Fix fusion of indexes for array references.
    • -
    • x86/x64: Fix stack overflow handling for coroutine results.
    • -
    • Enable low-2GB memory allocation on FreeBSD/x64.
    • -
    • Fix collectgarbage("count") result if more than 2GB is in use.
    • -
    • Fix parsing of hex floats.
    • -
    • x86/x64: Fix loop branch inversion with trailing -HREF+NE/EQ.
    • -
    • Add jit.os string.
    • -
    • coroutine.create() permits running C functions, too.
    • -
    • Fix OSX build to work with newer ld64 versions.
    • -
    • Fix bytecode optimization of and/or operators.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • Emit specialized bytecode for pairs()/next().
    • -
    • Improve bytecode coalescing of nil constants.
    • -
    • Compile calls to vararg functions.
    • -
    • Compile select().
    • -
    • Improve alias analysis, esp. for loads from allocations.
    • -
    • Tuning of various compiler heuristics.
    • -
    • Refactor and extend IR conversion instructions.
    • -
    • x86/x64: Various backend enhancements related to the FFI.
    • -
    • Add SPLIT pass to split 64 bit IR instructions for 32 bit CPUs.
    • -
  • -
- -

LuaJIT 2.0.0-beta5 — 2010-08-24

-
    -
  • Correctness and completeness: -
      -
    • Fix trace exit dispatch to function headers.
    • -
    • Fix Windows and OSX builds with LUAJIT_DISABLE_JIT.
    • -
    • Reorganize and fix placement of generated machine code on x64.
    • -
    • Fix TNEW in x64 interpreter.
    • -
    • Do not eliminate PHIs for values only referenced from side exits.
    • -
    • OS-independent canonicalization of strings for non-finite numbers.
    • -
    • Fix string.char() range check on x64.
    • -
    • Fix tostring() resolving within print().
    • -
    • Fix error handling for next().
    • -
    • Fix passing of constant arguments to external calls on x64.
    • -
    • Fix interpreter argument check for two-argument SSE math functions.
    • -
    • Fix C frame chain corruption caused by lua_cpcall().
    • -
    • Fix return from pcall() within active hook.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • Replace on-trace GC frame syncing with interpreter exit.
    • -
    • Improve hash lookup specialization by not removing dead keys during GC.
    • -
    • Turn traces into true GC objects.
    • -
    • Avoid starting a GC cycle immediately after library init.
    • -
    • Add weak guards to improve dead-code elimination.
    • -
    • Speed up string interning.
    • -
  • -
- -

LuaJIT 2.0.0-beta4 — 2010-03-28

-
    -
  • Correctness and completeness: -
      -
    • Fix precondition for on-trace creation of table keys.
    • -
    • Fix {f()} on x64 when table is resized.
    • -
    • Fix folding of ordered comparisons with same references.
    • -
    • Fix snapshot restores for multi-result bytecodes.
    • -
    • Fix potential hang when recording bytecode with nested closures.
    • -
    • Fix recording of getmetatable(), tonumber() and bad argument types.
    • -
    • Fix SLOAD fusion across returns to lower frames.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • Add array bounds check elimination. -Oabc is enabled by default.
    • -
    • More tuning for x64, e.g. smaller table objects.
    • -
  • -
- -

LuaJIT 2.0.0-beta3 — 2010-03-07

-
    -
  • LuaJIT x64 port: -
      -
    • Port integrated memory allocator to Linux/x64, Windows/x64 and OSX/x64.
    • -
    • Port interpreter and JIT compiler to x64.
    • -
    • Port DynASM to x64.
    • -
    • Many 32/64 bit cleanups in the VM.
    • -
    • Allow building the interpreter with either x87 or SSE2 arithmetics.
    • -
    • Add external unwinding and C++ exception interop (default on x64).
    • -
  • -
  • Correctness and completeness: -
      -
    • Fix constructor bytecode generation for certain conditional values.
    • -
    • Fix some cases of ordered string comparisons.
    • -
    • Fix lua_tocfunction().
    • -
    • Fix cutoff register in JMP bytecode for some conditional expressions.
    • -
    • Fix PHI marking algorithm for references from variant slots.
    • -
    • Fix package.cpath for non-default PREFIX.
    • -
    • Fix DWARF2 frame unwind information for interpreter on OSX.
    • -
    • Drive the GC forward on string allocations in the parser.
    • -
    • Implement call/return hooks (zero-cost if disabled).
    • -
    • Implement yield from C hooks.
    • -
    • Disable JIT compiler on older non-SSE2 CPUs instead of aborting.
    • -
  • -
  • Structural and performance enhancements: -
      -
    • Compile recursive code (tail-, up- and down-recursion).
    • -
    • Improve heuristics for bytecode penalties and blacklisting.
    • -
    • Split CALL/FUNC recording and clean up fast function call semantics.
    • -
    • Major redesign of internal function call handling.
    • -
    • Improve FOR loop const specialization and integerness checks.
    • -
    • Switch to pre-initialized stacks. Avoid frame-clearing.
    • -
    • Colocation of prototypes and related data: bytecode, constants, debug info.
    • -
    • Cleanup parser and streamline bytecode generation.
    • -
    • Add support for weak IR references to register allocator.
    • -
    • Switch to compressed, extensible snapshots.
    • -
    • Compile returns to frames below the start frame.
    • -
    • Improve alias analysis of upvalues using a disambiguation hash value.
    • -
    • Compile floor/ceil/trunc to SSE2 helper calls or SSE4.1 instructions.
    • -
    • Add generic C call handling to IR and backend.
    • -
    • Improve KNUM fuse vs. load heuristics.
    • -
    • Compile various io.*() functions.
    • -
    • Compile math.sinh(), math.cosh(), math.tanh() -and math.random().
    • -
  • -
- -

LuaJIT 2.0.0-beta2 — 2009-11-09

-
    -
  • Reorganize build system. Build static+shared library on POSIX.
  • -
  • Allow C++ exception conversion on all platforms -using a wrapper function.
  • -
  • Automatically catch C++ exceptions and rethrow Lua error -(DWARF2 only).
  • -
  • Check for the correct x87 FPU precision at strategic points.
  • -
  • Always use wrappers for libm functions.
  • -
  • Resurrect metamethod name strings before copying them.
  • -
  • Mark current trace, even if compiler is idle.
  • -
  • Ensure FILE metatable is created only once.
  • -
  • Fix type comparisons when different integer types are involved.
  • -
  • Fix getmetatable() recording.
  • -
  • Fix TDUP with dead keys in template table.
  • -
  • jit.flush(tr) returns status. -Prevent manual flush of a trace that's still linked.
  • -
  • Improve register allocation heuristics for invariant references.
  • -
  • Compile the push/pop variants of table.insert() and -table.remove().
  • -
  • Compatibility with MSVC link /debug.
  • -
  • Fix lua_iscfunction().
  • -
  • Fix math.random() when compiled with -fpic (OSX).
  • -
  • Fix table.maxn().
  • -
  • Bump MACOSX_DEPLOYMENT_TARGET to 10.4
  • -
  • luaL_check*() and luaL_opt*() now support -negative arguments, too.
    -This matches the behavior of Lua 5.1, but not the specification.
  • -
- -

LuaJIT 2.0.0-beta1 — 2009-10-31

-
    -
  • This is the first public release of LuaJIT 2.0.
  • -
  • The whole VM has been rewritten from the ground up, so there's -no point in listing differences over earlier versions.
  • -
-
- -
-

LuaJIT 1.1.8 — 2012-04-16

- - -

LuaJIT 1.1.7 — 2011-05-05

- - -

LuaJIT 1.1.6 — 2010-03-28

-
    -
  • Added fixes for the -» currently known bugs in Lua 5.1.4.
  • -
  • Removed wrong GC check in jit_createstate(). -Thanks to Tim Mensch.
  • -
  • Fixed bad assertions while compiling table.insert() and -table.remove().
  • -
- -

LuaJIT 1.1.5 — 2008-10-25

- - -

LuaJIT 1.1.4 — 2008-02-05

-
    -
  • Merged with Lua 5.1.3. Fixes all -» known bugs in Lua 5.1.2.
  • -
  • Fixed possible (but unlikely) stack corruption while compiling -k^x expressions.
  • -
  • Fixed DynASM template for cmpss instruction.
  • -
- -

LuaJIT 1.1.3 — 2007-05-24

-
    -
  • Merged with Lua 5.1.2. Fixes all -» known bugs in Lua 5.1.1.
  • -
  • Merged pending Lua 5.1.x fixes: "return -nil" bug, spurious count hook call.
  • -
  • Remove a (sometimes) wrong assertion in luaJIT_findpc().
  • -
  • DynASM now allows labels for displacements and .aword.
  • -
  • Fix some compiler warnings for DynASM glue (internal API change).
  • -
  • Correct naming for SSSE3 (temporarily known as SSE4) in DynASM and x86 disassembler.
  • -
  • The loadable debug modules now handle redirection to stdout -(e.g. -j trace=-).
  • -
- -

LuaJIT 1.1.2 — 2006-06-24

-
    -
  • Fix MSVC inline assembly: use only local variables with -lua_number2int().
  • -
  • Fix "attempt to call a thread value" bug on Mac OS X: -make values of consts used as lightuserdata keys unique -to avoid joining by the compiler/linker.
  • -
- -

LuaJIT 1.1.1 — 2006-06-20

-
    -
  • Merged with Lua 5.1.1. Fixes all -» known bugs in Lua 5.1.
  • -
  • Enforce (dynamic) linker error for EXE/DLL version mismatches.
  • -
  • Minor changes to DynASM: faster pre-processing, smaller encoding -for some immediates.
  • -
-

-This release is in sync with Coco 1.1.1 (see the -» Coco Change History). -

- -

LuaJIT 1.1.0 — 2006-03-13

-
    -
  • Merged with Lua 5.1 (final).
  • - -
  • New JIT call frame setup: -
      -
    • The C stack is kept 16 byte aligned (faster). -Mandatory for Mac OS X on Intel, too.
    • -
    • Faster calling conventions for internal C helper functions.
    • -
    • Better instruction scheduling for function prologue, OP_CALL and -OP_RETURN.
    • -
  • - -
  • Miscellaneous optimizations: -
      -
    • Faster loads of FP constants. Remove narrow-to-wide store-to-load -forwarding stalls.
    • -
    • Use (scalar) SSE2 ops (if the CPU supports it) to speed up slot moves -and FP to integer conversions.
    • -
    • Optimized the two-argument form of OP_CONCAT (a..b).
    • -
    • Inlined OP_MOD (a%b). -With better accuracy than the C variant, too.
    • -
    • Inlined OP_POW (a^b). Unroll x^k or -use k^x = 2^(log2(k)*x) or call pow().
    • -
  • - -
  • Changes in the optimizer: -
      -
    • Improved hinting for table keys derived from table values -(t1[t2[x]]).
    • -
    • Lookup hinting now works with arbitrary object types and -supports index chains, too.
    • -
    • Generate type hints for arithmetic and comparison operators, -OP_LEN, OP_CONCAT and OP_FORPREP.
    • -
    • Remove several hint definitions in favour of a generic COMBINE hint.
    • -
    • Complete rewrite of jit.opt_inline module -(ex jit.opt_lib).
    • -
  • - -
  • Use adaptive deoptimization: -
      -
    • If runtime verification of a contract fails, the affected -instruction is recompiled and patched on-the-fly. -Regular programs will trigger deoptimization only occasionally.
    • -
    • This avoids generating code for uncommon fallback cases -most of the time. Generated code is up to 30% smaller compared to -LuaJIT 1.0.3.
    • -
    • Deoptimization is used for many opcodes and contracts: -
        -
      • OP_CALL, OP_TAILCALL: type mismatch for callable.
      • -
      • Inlined calls: closure mismatch, parameter number and type mismatches.
      • -
      • OP_GETTABLE, OP_SETTABLE: table or key type and range mismatches.
      • -
      • All arithmetic and comparison operators, OP_LEN, OP_CONCAT, -OP_FORPREP: operand type and range mismatches.
      • -
    • -
    • Complete redesign of the debug and traceback info -(bytecode ↔ mcode) to support deoptimization. -Much more flexible and needs only 50% of the space.
    • -
    • The modules jit.trace, jit.dumphints and -jit.dump handle deoptimization.
    • -
  • - -
  • Inlined many popular library functions -(for commonly used arguments only): -
      -
    • Most math.* functions (the 18 most used ones) -[2x-10x faster].
    • -
    • string.len, string.sub and string.char -[2x-10x faster].
    • -
    • table.insert, table.remove and table.getn -[3x-5x faster].
    • -
    • coroutine.yield and coroutine.resume -[3x-5x faster].
    • -
    • pairs, ipairs and the corresponding iterators -[8x-15x faster].
    • -
  • - -
  • Changes in the core and loadable modules and the stand-alone executable: -
      -
    • Added jit.version, jit.version_num -and jit.arch.
    • -
    • Reorganized some internal API functions (jit.util.*mcode*).
    • -
    • The -j dump output now shows JSUB names, too.
    • -
    • New x86 disassembler module written in pure Lua. No dependency -on ndisasm anymore. Flexible API, very compact (500 lines) -and complete (x87, MMX, SSE, SSE2, SSE3, SSSE3, privileged instructions).
    • -
    • luajit -v prints the LuaJIT version and copyright -on a separate line.
    • -
  • - -
  • Added SSE, SSE2, SSE3 and SSSE3 support to DynASM.
  • -
  • Miscellaneous doc changes. Added a section about -embedding LuaJIT.
  • -
-

-This release is in sync with Coco 1.1.0 (see the -» Coco Change History). -

-
- -
-

LuaJIT 1.0.3 — 2005-09-08

-
    -
  • Even more docs.
  • -
  • Unified closure checks in jit.*.
  • -
  • Fixed some range checks in jit.util.*.
  • -
  • Fixed __newindex call originating from jit_settable_str().
  • -
  • Merged with Lua 5.1 alpha (including early bug fixes).
  • -
-

-This is the first public release of LuaJIT. -

- -

LuaJIT 1.0.2 — 2005-09-02

-
    -
  • Add support for flushing the Valgrind translation cache
    -(MYCFLAGS= -DUSE_VALGRIND).
  • -
  • Add support for freeing executable mcode memory to the mmap()-based -variant for POSIX systems.
  • -
  • Reorganized the C function signature handling in -jit.opt_lib.
  • -
  • Changed to index-based hints for inlining C functions. -Still no support in the backend for inlining.
  • -
  • Hardcode HEAP_CREATE_ENABLE_EXECUTE value if undefined.
  • -
  • Misc. changes to the jit.* modules.
  • -
  • Misc. changes to the Makefiles.
  • -
  • Lots of new docs.
  • -
  • Complete doc reorg.
  • -
-

-Not released because Lua 5.1 alpha came out today. -

- -

LuaJIT 1.0.1 — 2005-08-31

-
    -
  • Missing GC step in OP_CONCAT.
  • -
  • Fix result handling for C –> JIT calls.
  • -
  • Detect CPU feature bits.
  • -
  • Encode conditional moves (fucomip) only when supported.
  • -
  • Add fallback instructions for FP compares.
  • -
  • Add support for LUA_COMPAT_VARARG. Still disabled by default.
  • -
  • MSVC needs a specific place for the CALLBACK attribute -(David Burgess).
  • -
  • Misc. doc updates.
  • -
-

-Interim non-public release. -Special thanks to Adam D. Moss for reporting most of the bugs. -

- -

LuaJIT 1.0.0 — 2005-08-29

-

-This is the initial non-public release of LuaJIT. -

-
-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/contact.html b/third-party/LuaJIT-2.0.2/doc/contact.html deleted file mode 100644 index 4735faf440..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/contact.html +++ /dev/null @@ -1,102 +0,0 @@ - - - -Contact - - - - - - - - -
-Lua -
- - -
-

-Please send general questions to the -» LuaJIT mailing list. -You can also send any questions you have directly to me: -

- - - - - -

Copyright

-

-All documentation is -Copyright © 2005-2013 Mike Pall. -

- - -
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/ext_c_api.html b/third-party/LuaJIT-2.0.2/doc/ext_c_api.html deleted file mode 100644 index c6feb8e16c..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/ext_c_api.html +++ /dev/null @@ -1,187 +0,0 @@ - - - -Lua/C API Extensions - - - - - - - - -
-Lua -
- - -
-

-LuaJIT adds some extensions to the standard Lua/C API. The LuaJIT include -directory must be in the compiler search path (-Ipath) -to be able to include the required header for C code: -

-
-#include "luajit.h"
-
-

-Or for C++ code: -

-
-#include "lua.hpp"
-
- -

luaJIT_setmode(L, idx, mode) -— Control VM

-

-This is a C API extension to allow control of the VM from C code. The -full prototype of LuaJIT_setmode is: -

-
-LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
-
-

-The returned status is either success (1) or failure (0). -The second argument is either 0 or a stack index (similar to the -other Lua/C API functions). -

-

-The third argument specifies the mode, which is 'or'ed with a flag. -The flag can be LUAJIT_MODE_OFF to turn a feature on, -LUAJIT_MODE_ON to turn a feature off, or -LUAJIT_MODE_FLUSH to flush cached code. -

-

-The following modes are defined: -

- -

luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)

-

-Turn the whole JIT compiler on or off or flush the whole cache of compiled code. -

- -

luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)
-luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)
-luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)

-

-This sets the mode for the function at the stack index idx or -the parent of the calling function (idx = 0). It either -enables JIT compilation for a function, disables it and flushes any -already compiled code or only flushes already compiled code. This -applies recursively to all sub-functions of the function with -LUAJIT_MODE_ALLFUNC or only to the sub-functions with -LUAJIT_MODE_ALLSUBFUNC. -

- -

luaJIT_setmode(L, trace,
-  LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)

-

-Flushes the specified root trace and all of its side traces from the cache. -The code for the trace will be retained as long as there are any other -traces which link to it. -

- -

luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)

-

-This mode defines a wrapper function for calls to C functions. If -called with LUAJIT_MODE_ON, the stack index at idx -must be a lightuserdata object holding a pointer to the wrapper -function. From now on all C functions are called through the wrapper -function. If called with LUAJIT_MODE_OFF this mode is turned -off and all C functions are directly called. -

-

-The wrapper function can be used for debugging purposes or to catch -and convert foreign exceptions. But please read the section on -C++ exception interoperability -first. Recommended usage can be seen in this C++ code excerpt: -

-
-#include <exception>
-#include "lua.hpp"
-
-// Catch C++ exceptions and convert them to Lua error messages.
-// Customize as needed for your own exception classes.
-static int wrap_exceptions(lua_State *L, lua_CFunction f)
-{
-  try {
-    return f(L);  // Call wrapped function and return result.
-  } catch (const char *s) {  // Catch and convert exceptions.
-    lua_pushstring(L, s);
-  } catch (std::exception& e) {
-    lua_pushstring(L, e.what());
-  } catch (...) {
-    lua_pushliteral(L, "caught (...)");
-  }
-  return lua_error(L);  // Rethrow as a Lua error.
-}
-
-static int myinit(lua_State *L)
-{
-  ...
-  // Define wrapper function and enable it.
-  lua_pushlightuserdata(L, (void *)wrap_exceptions);
-  luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
-  lua_pop(L, 1);
-  ...
-}
-
-

-Note that you can only define a single global wrapper function, -so be careful when using this mechanism from multiple C++ modules. -Also note that this mechanism is not without overhead. -

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/ext_ffi.html b/third-party/LuaJIT-2.0.2/doc/ext_ffi.html deleted file mode 100644 index a146b055d8..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/ext_ffi.html +++ /dev/null @@ -1,330 +0,0 @@ - - - -FFI Library - - - - - - - - -
-Lua -
- - -
-

- -The FFI library allows calling external C functions and -using C data structures from pure Lua code. - -

-

- -The FFI library largely obviates the need to write tedious manual -Lua/C bindings in C. No need to learn a separate binding language -— it parses plain C declarations! These can be -cut-n-pasted from C header files or reference manuals. It's up to -the task of binding large libraries without the need for dealing with -fragile binding generators. - -

-

-The FFI library is tightly integrated into LuaJIT (it's not available -as a separate module). The code generated by the JIT-compiler for -accesses to C data structures from Lua code is on par with the -code a C compiler would generate. Calls to C functions can -be inlined in JIT-compiled code, unlike calls to functions bound via -the classic Lua/C API. -

-

-This page gives a short introduction to the usage of the FFI library. -Please use the FFI sub-topics in the navigation bar to learn more. -

- -

Motivating Example: Calling External C Functions

-

-It's really easy to call an external C library function: -

-
-①
-②
-
-
-③local ffi = require("ffi")
-ffi.cdef[[
-int printf(const char *fmt, ...);
-]]
-ffi.C.printf("Hello %s!", "world")
-
-

-So, let's pick that apart: -

-

- Load the FFI library. -

-

- Add a C declaration -for the function. The part inside the double-brackets (in green) is -just standard C syntax. -

-

- Call the named -C function — Yes, it's that simple! -

-

-Actually, what goes on behind the scenes is far from simple: makes use of the standard -C library namespace ffi.C. Indexing this namespace with -a symbol name ("printf") automatically binds it to the -standard C library. The result is a special kind of object which, -when called, runs the printf function. The arguments passed -to this function are automatically converted from Lua objects to the -corresponding C types. -

-

-Ok, so maybe the use of printf() wasn't such a spectacular -example. You could have done that with io.write() and -string.format(), too. But you get the idea ... -

-

-So here's something to pop up a message box on Windows: -

-
-local ffi = require("ffi")
-ffi.cdef[[
-int MessageBoxA(void *w, const char *txt, const char *cap, int type);
-]]
-ffi.C.MessageBoxA(nil, "Hello world!", "Test", 0)
-
-

-Bing! Again, that was far too easy, no? -

-

-Compare this with the effort required to bind that function using the -classic Lua/C API: create an extra C file, add a C function -that retrieves and checks the argument types passed from Lua and calls -the actual C function, add a list of module functions and their -names, add a luaopen_* function and register all module -functions, compile and link it into a shared library (DLL), move it to -the proper path, add Lua code that loads the module aaaand ... finally -call the binding function. Phew! -

- -

Motivating Example: Using C Data Structures

-

-The FFI library allows you to create and access C data -structures. Of course the main use for this is for interfacing with -C functions. But they can be used stand-alone, too. -

-

-Lua is built upon high-level data types. They are flexible, extensible -and dynamic. That's why we all love Lua so much. Alas, this can be -inefficient for certain tasks, where you'd really want a low-level -data type. E.g. a large array of a fixed structure needs to be -implemented with a big table holding lots of tiny tables. This imposes -both a substantial memory overhead as well as a performance overhead. -

-

-Here's a sketch of a library that operates on color images plus a -simple benchmark. First, the plain Lua version: -

-
-local floor = math.floor
-
-local function image_ramp_green(n)
-  local img = {}
-  local f = 255/(n-1)
-  for i=1,n do
-    img[i] = { red = 0, green = floor((i-1)*f), blue = 0, alpha = 255 }
-  end
-  return img
-end
-
-local function image_to_grey(img, n)
-  for i=1,n do
-    local y = floor(0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue)
-    img[i].red = y; img[i].green = y; img[i].blue = y
-  end
-end
-
-local N = 400*400
-local img = image_ramp_green(N)
-for i=1,1000 do
-  image_to_grey(img, N)
-end
-
-

-This creates a table with 160.000 pixels, each of which is a table -holding four number values in the range of 0-255. First an image with -a green ramp is created (1D for simplicity), then the image is -converted to greyscale 1000 times. Yes, that's silly, but I was in -need of a simple example ... -

-

-And here's the FFI version. The modified parts have been marked in -bold: -

-
-①
-
-
-
-
-
-②
-
-③
-④
-
-
-
-
-
-
-③
-⑤local ffi = require("ffi")
-ffi.cdef[[
-typedef struct { uint8_t red, green, blue, alpha; } rgba_pixel;
-]]
-
-local function image_ramp_green(n)
-  local img = ffi.new("rgba_pixel[?]", n)
-  local f = 255/(n-1)
-  for i=0,n-1 do
-    img[i].green = i*f
-    img[i].alpha = 255
-  end
-  return img
-end
-
-local function image_to_grey(img, n)
-  for i=0,n-1 do
-    local y = 0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue
-    img[i].red = y; img[i].green = y; img[i].blue = y
-  end
-end
-
-local N = 400*400
-local img = image_ramp_green(N)
-for i=1,1000 do
-  image_to_grey(img, N)
-end
-
-

-Ok, so that wasn't too difficult: -

-

- First, load the FFI -library and declare the low-level data type. Here we choose a -struct which holds four byte fields, one for each component -of a 4x8 bit RGBA pixel. -

-

- Creating the data -structure with ffi.new() is straightforward — the -'?' is a placeholder for the number of elements of a -variable-length array. -

-

- C arrays are -zero-based, so the indexes have to run from 0 to -n-1. One might want to allocate one more element instead to -simplify converting legacy code. -

-

- Since ffi.new() -zero-fills the array by default, we only need to set the green and the -alpha fields. -

-

- The calls to -math.floor() can be omitted here, because floating-point -numbers are already truncated towards zero when converting them to an -integer. This happens implicitly when the number is stored in the -fields of each pixel. -

-

-Now let's have a look at the impact of the changes: first, memory -consumption for the image is down from 22 Megabytes to -640 Kilobytes (400*400*4 bytes). That's a factor of 35x less! So, -yes, tables do have a noticeable overhead. BTW: The original program -would consume 40 Megabytes in plain Lua (on x64). -

-

-Next, performance: the pure Lua version runs in 9.57 seconds (52.9 -seconds with the Lua interpreter) and the FFI version runs in 0.48 -seconds on my machine (YMMV). That's a factor of 20x faster (110x -faster than the Lua interpreter). -

-

-The avid reader may notice that converting the pure Lua version over -to use array indexes for the colors ([1] instead of -.red, [2] instead of .green etc.) ought to -be more compact and faster. This is certainly true (by a factor of -~1.7x). Switching to a struct-of-arrays would help, too. -

-

-However the resulting code would be less idiomatic and rather -error-prone. And it still doesn't get even close to the performance of -the FFI version of the code. Also, high-level data structures cannot -be easily passed to other C functions, especially I/O functions, -without undue conversion penalties. -

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/ext_ffi_api.html b/third-party/LuaJIT-2.0.2/doc/ext_ffi_api.html deleted file mode 100644 index 8b2555b568..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/ext_ffi_api.html +++ /dev/null @@ -1,566 +0,0 @@ - - - -ffi.* API Functions - - - - - - - - - -
-Lua -
- - -
-

-This page describes the API functions provided by the FFI library in -detail. It's recommended to read through the -introduction and the -FFI tutorial first. -

- -

Glossary

-
    -
  • cdecl — An abstract C type declaration (a Lua -string).
  • -
  • ctype — A C type object. This is a special kind of -cdata returned by ffi.typeof(). It serves as a -cdata constructor when called.
  • -
  • cdata — A C data object. It holds a value of the -corresponding ctype.
  • -
  • ct — A C type specification which can be used for -most of the API functions. Either a cdecl, a ctype or a -cdata serving as a template type.
  • -
  • cb — A callback object. This is a C data object -holding a special function pointer. Calling this function from -C code runs an associated Lua function.
  • -
  • VLA — A variable-length array is declared with a -? instead of the number of elements, e.g. "int[?]". -The number of elements (nelem) must be given when it's -created.
  • -
  • VLS — A variable-length struct is a struct C -type where the last element is a VLA. The same rules for -declaration and creation apply.
  • -
- -

Declaring and Accessing External Symbols

-

-External symbols must be declared first and can then be accessed by -indexing a C library -namespace, which automatically binds the symbol to a specific -library. -

- -

ffi.cdef(def)

-

-Adds multiple C declarations for types or external symbols (named -variables or functions). def must be a Lua string. It's -recommended to use the syntactic sugar for string arguments as -follows: -

-
-ffi.cdef[[
-typedef struct foo { int a, b; } foo_t;  // Declare a struct and typedef.
-int dofoo(foo_t *f, int n);  /* Declare an external C function. */
-]]
-
-

-The contents of the string (the part in green above) must be a -sequence of -C declarations, -separated by semicolons. The trailing semicolon for a single -declaration may be omitted. -

-

-Please note that external symbols are only declared, but they -are not bound to any specific address, yet. Binding is -achieved with C library namespaces (see below). -

-

-C declarations are not passed through a C pre-processor, -yet. No pre-processor tokens are allowed, except for -#pragma pack. Replace #define in existing -C header files with enum, static const -or typedef and/or pass the files through an external -C pre-processor (once). Be careful not to include unneeded or -redundant declarations from unrelated header files. -

- -

ffi.C

-

-This is the default C library namespace — note the -uppercase 'C'. It binds to the default set of symbols or -libraries on the target system. These are more or less the same as a -C compiler would offer by default, without specifying extra link -libraries. -

-

-On POSIX systems, this binds to symbols in the default or global -namespace. This includes all exported symbols from the executable and -any libraries loaded into the global namespace. This includes at least -libc, libm, libdl (on Linux), -libgcc (if compiled with GCC), as well as any exported -symbols from the Lua/C API provided by LuaJIT itself. -

-

-On Windows systems, this binds to symbols exported from the -*.exe, the lua51.dll (i.e. the Lua/C API -provided by LuaJIT itself), the C runtime library LuaJIT was linked -with (msvcrt*.dll), kernel32.dll, -user32.dll and gdi32.dll. -

- -

clib = ffi.load(name [,global])

-

-This loads the dynamic library given by name and returns -a new C library namespace which binds to its symbols. On POSIX -systems, if global is true, the library symbols are -loaded into the global namespace, too. -

-

-If name is a path, the library is loaded from this path. -Otherwise name is canonicalized in a system-dependent way and -searched in the default search path for dynamic libraries: -

-

-On POSIX systems, if the name contains no dot, the extension -.so is appended. Also, the lib prefix is prepended -if necessary. So ffi.load("z") looks for "libz.so" -in the default shared library search path. -

-

-On Windows systems, if the name contains no dot, the extension -.dll is appended. So ffi.load("ws2_32") looks for -"ws2_32.dll" in the default DLL search path. -

- -

Creating cdata Objects

-

-The following API functions create cdata objects (type() -returns "cdata"). All created cdata objects are -garbage collected. -

- -

cdata = ffi.new(ct [,nelem] [,init...])
-cdata = ctype([nelem,] [init...])

-

-Creates a cdata object for the given ct. VLA/VLS types -require the nelem argument. The second syntax uses a ctype as -a constructor and is otherwise fully equivalent. -

-

-The cdata object is initialized according to the -rules for initializers, -using the optional init arguments. Excess initializers cause -an error. -

-

-Performance notice: if you want to create many objects of one kind, -parse the cdecl only once and get its ctype with -ffi.typeof(). Then use the ctype as a constructor repeatedly. -

-

-Please note that an anonymous struct declaration implicitly -creates a new and distinguished ctype every time you use it for -ffi.new(). This is probably not what you want, -especially if you create more than one cdata object. Different anonymous -structs are not considered assignment-compatible by the -C standard, even though they may have the same fields! Also, they -are considered different types by the JIT-compiler, which may cause an -excessive number of traces. It's strongly suggested to either declare -a named struct or typedef with ffi.cdef() -or to create a single ctype object for an anonymous struct -with ffi.typeof(). -

- -

ctype = ffi.typeof(ct)

-

-Creates a ctype object for the given ct. -

-

-This function is especially useful to parse a cdecl only once and then -use the resulting ctype object as a constructor. -

- -

cdata = ffi.cast(ct, init)

-

-Creates a scalar cdata object for the given ct. The cdata -object is initialized with init using the "cast" variant of -the C type conversion -rules. -

-

-This functions is mainly useful to override the pointer compatibility -checks or to convert pointers to addresses or vice versa. -

- -

ctype = ffi.metatype(ct, metatable)

-

-Creates a ctype object for the given ct and associates it with -a metatable. Only struct/union types, complex numbers -and vectors are allowed. Other types may be wrapped in a -struct, if needed. -

-

-The association with a metatable is permanent and cannot be changed -afterwards. Neither the contents of the metatable nor the -contents of an __index table (if any) may be modified -afterwards. The associated metatable automatically applies to all uses -of this type, no matter how the objects are created or where they -originate from. Note that pre-defined operations on types have -precedence (e.g. declared field names cannot be overriden). -

-

-All standard Lua metamethods are implemented. These are called directly, -without shortcuts and on any mix of types. For binary operations, the -left operand is checked first for a valid ctype metamethod. The -__gc metamethod only applies to struct/union -types and performs an implicit ffi.gc() -call during creation of an instance. -

- -

cdata = ffi.gc(cdata, finalizer)

-

-Associates a finalizer with a pointer or aggregate cdata object. The -cdata object is returned unchanged. -

-

-This function allows safe integration of unmanaged resources into the -automatic memory management of the LuaJIT garbage collector. Typical -usage: -

-
-local p = ffi.gc(ffi.C.malloc(n), ffi.C.free)
-...
-p = nil -- Last reference to p is gone.
--- GC will eventually run finalizer: ffi.C.free(p)
-
-

-A cdata finalizer works like the __gc metamethod for userdata -objects: when the last reference to a cdata object is gone, the -associated finalizer is called with the cdata object as an argument. The -finalizer can be a Lua function or a cdata function or cdata function -pointer. An existing finalizer can be removed by setting a nil -finalizer, e.g. right before explicitly deleting a resource: -

-
-ffi.C.free(ffi.gc(p, nil)) -- Manually free the memory.
-
- -

C Type Information

-

-The following API functions return information about C types. -They are most useful for inspecting cdata objects. -

- -

size = ffi.sizeof(ct [,nelem])

-

-Returns the size of ct in bytes. Returns nil if -the size is not known (e.g. for "void" or function types). -Requires nelem for VLA/VLS types, except for cdata objects. -

- -

align = ffi.alignof(ct)

-

-Returns the minimum required alignment for ct in bytes. -

- -

ofs [,bpos,bsize] = ffi.offsetof(ct, field)

-

-Returns the offset (in bytes) of field relative to the start -of ct, which must be a struct. Additionally returns -the position and the field size (in bits) for bit fields. -

- -

status = ffi.istype(ct, obj)

-

-Returns true if obj has the C type given by -ct. Returns false otherwise. -

-

-C type qualifiers (const etc.) are ignored. Pointers are -checked with the standard pointer compatibility rules, but without any -special treatment for void *. If ct specifies a -struct/union, then a pointer to this type is accepted, -too. Otherwise the types must match exactly. -

-

-Note: this function accepts all kinds of Lua objects for the -obj argument, but always returns false for non-cdata -objects. -

- -

Utility Functions

- -

err = ffi.errno([newerr])

-

-Returns the error number set by the last C function call which -indicated an error condition. If the optional newerr argument -is present, the error number is set to the new value and the previous -value is returned. -

-

-This function offers a portable and OS-independent way to get and set the -error number. Note that only some C functions set the error -number. And it's only significant if the function actually indicated an -error condition (e.g. with a return value of -1 or -NULL). Otherwise, it may or may not contain any previously set -value. -

-

-You're advised to call this function only when needed and as close as -possible after the return of the related C function. The -errno value is preserved across hooks, memory allocations, -invocations of the JIT compiler and other internal VM activity. The same -applies to the value returned by GetLastError() on Windows, but -you need to declare and call it yourself. -

- -

str = ffi.string(ptr [,len])

-

-Creates an interned Lua string from the data pointed to by -ptr. -

-

-If the optional argument len is missing, ptr is -converted to a "char *" and the data is assumed to be -zero-terminated. The length of the string is computed with -strlen(). -

-

-Otherwise ptr is converted to a "void *" and -len gives the length of the data. The data may contain -embedded zeros and need not be byte-oriented (though this may cause -endianess issues). -

-

-This function is mainly useful to convert (temporary) -"const char *" pointers returned by -C functions to Lua strings and store them or pass them to other -functions expecting a Lua string. The Lua string is an (interned) copy -of the data and bears no relation to the original data area anymore. -Lua strings are 8 bit clean and may be used to hold arbitrary, -non-character data. -

-

-Performance notice: it's faster to pass the length of the string, if -it's known. E.g. when the length is returned by a C call like -sprintf(). -

- -

ffi.copy(dst, src, len)
-ffi.copy(dst, str)

-

-Copies the data pointed to by src to dst. -dst is converted to a "void *" and src -is converted to a "const void *". -

-

-In the first syntax, len gives the number of bytes to copy. -Caveat: if src is a Lua string, then len must not -exceed #src+1. -

-

-In the second syntax, the source of the copy must be a Lua string. All -bytes of the string plus a zero-terminator are copied to -dst (i.e. #src+1 bytes). -

-

-Performance notice: ffi.copy() may be used as a faster -(inlinable) replacement for the C library functions -memcpy(), strcpy() and strncpy(). -

- -

ffi.fill(dst, len [,c])

-

-Fills the data pointed to by dst with len constant -bytes, given by c. If c is omitted, the data is -zero-filled. -

-

-Performance notice: ffi.fill() may be used as a faster -(inlinable) replacement for the C library function -memset(dst, c, len). Please note the different -order of arguments! -

- -

Target-specific Information

- -

status = ffi.abi(param)

-

-Returns true if param (a Lua string) applies for the -target ABI (Application Binary Interface). Returns false -otherwise. The following parameters are currently defined: -

- - - - - - - - - - - - - - - - - - - - - - - -
ParameterDescription
32bit32 bit architecture
64bit64 bit architecture
leLittle-endian architecture
beBig-endian architecture
fpuTarget has a hardware FPU
softfpsoftfp calling conventions
hardfphardfp calling conventions
eabiEABI variant of the standard ABI
winWindows variant of the standard ABI
- -

ffi.os

-

-Contains the target OS name. Same contents as -jit.os. -

- -

ffi.arch

-

-Contains the target architecture name. Same contents as -jit.arch. -

- -

Methods for Callbacks

-

-The C types for callbacks -have some extra methods: -

- -

cb:free()

-

-Free the resources associated with a callback. The associated Lua -function is unanchored and may be garbage collected. The callback -function pointer is no longer valid and must not be called anymore -(it may be reused by a subsequently created callback). -

- -

cb:set(func)

-

-Associate a new Lua function with a callback. The C type of the -callback and the callback function pointer are unchanged. -

-

-This method is useful to dynamically switch the receiver of callbacks -without creating a new callback each time and registering it again (e.g. -with a GUI library). -

- -

Extended Standard Library Functions

-

-The following standard library functions have been extended to work -with cdata objects: -

- -

n = tonumber(cdata)

-

-Converts a number cdata object to a double and returns it as -a Lua number. This is particularly useful for boxed 64 bit -integer values. Caveat: this conversion may incur a precision loss. -

- -

s = tostring(cdata)

-

-Returns a string representation of the value of 64 bit integers -("nnnLL" or "nnnULL") or -complex numbers ("re±imi"). Otherwise -returns a string representation of the C type of a ctype object -("ctype<type>") or a cdata object -("cdata<type>: address"), unless you -override it with a __tostring metamethod (see -ffi.metatype()). -

- -

iter, obj, start = pairs(cdata)
-iter, obj, start = ipairs(cdata)

-

-Calls the __pairs or __ipairs metamethod of the -corresponding ctype. -

- -

Extensions to the Lua Parser

-

-The parser for Lua source code treats numeric literals with the -suffixes LL or ULL as signed or unsigned 64 bit -integers. Case doesn't matter, but uppercase is recommended for -readability. It handles both decimal (42LL) and hexadecimal -(0x2aLL) literals. -

-

-The imaginary part of complex numbers can be specified by suffixing -number literals with i or I, e.g. 12.5i. -Caveat: you'll need to use 1i to get an imaginary part with -the value one, since i itself still refers to a variable -named i. -

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/ext_ffi_semantics.html b/third-party/LuaJIT-2.0.2/doc/ext_ffi_semantics.html deleted file mode 100644 index 03229012ed..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/ext_ffi_semantics.html +++ /dev/null @@ -1,1243 +0,0 @@ - - - -FFI Semantics - - - - - - - - - -
-Lua -
- - -
-

-This page describes the detailed semantics underlying the FFI library -and its interaction with both Lua and C code. -

-

-Given that the FFI library is designed to interface with C code -and that declarations can be written in plain C syntax, it -closely follows the C language semantics, wherever possible. -Some minor concessions are needed for smoother interoperation with Lua -language semantics. -

-

-Please don't be overwhelmed by the contents of this page — this -is a reference and you may need to consult it, if in doubt. It doesn't -hurt to skim this page, but most of the semantics "just work" as you'd -expect them to work. It should be straightforward to write -applications using the LuaJIT FFI for developers with a C or C++ -background. -

- -

C Language Support

-

-The FFI library has a built-in C parser with a minimal memory -footprint. It's used by the ffi.* library -functions to declare C types or external symbols. -

-

-It's only purpose is to parse C declarations, as found e.g. in -C header files. Although it does evaluate constant expressions, -it's not a C compiler. The body of inline -C function definitions is simply ignored. -

-

-Also, this is not a validating C parser. It expects and -accepts correctly formed C declarations, but it may choose to -ignore bad declarations or show rather generic error messages. If in -doubt, please check the input against your favorite C compiler. -

-

-The C parser complies to the C99 language standard plus -the following extensions: -

-
    - -
  • The '\e' escape in character and string literals.
  • - -
  • The C99/C++ boolean type, declared with the keywords bool -or _Bool.
  • - -
  • Complex numbers, declared with the keywords complex or -_Complex.
  • - -
  • Two complex number types: complex (aka -complex double) and complex float.
  • - -
  • Vector types, declared with the GCC mode or -vector_size attribute.
  • - -
  • Unnamed ('transparent') struct/union fields -inside a struct/union.
  • - -
  • Incomplete enum declarations, handled like incomplete -struct declarations.
  • - -
  • Unnamed enum fields inside a -struct/union. This is similar to a scoped C++ -enum, except that declared constants are visible in the -global namespace, too.
  • - -
  • Scoped static const declarations inside a -struct/union (from C++).
  • - -
  • Zero-length arrays ([0]), empty -struct/union, variable-length arrays (VLA, -[?]) and variable-length structs (VLS, with a trailing -VLA).
  • - -
  • C++ reference types (int &x).
  • - -
  • Alternate GCC keywords with '__', e.g. -__const__.
  • - -
  • GCC __attribute__ with the following attributes: -aligned, packed, mode, -vector_size, cdecl, fastcall, -stdcall, thiscall.
  • - -
  • The GCC __extension__ keyword and the GCC -__alignof__ operator.
  • - -
  • GCC __asm__("symname") symbol name redirection for -function declarations.
  • - -
  • MSVC keywords for fixed-length types: __int8, -__int16, __int32 and __int64.
  • - -
  • MSVC __cdecl, __fastcall, __stdcall, -__thiscall, __ptr32, __ptr64, -__declspec(align(n)) and #pragma pack.
  • - -
  • All other GCC/MSVC-specific attributes are ignored.
  • - -
-

-The following C types are pre-defined by the C parser (like -a typedef, except re-declarations will be ignored): -

-
    - -
  • Vararg handling: va_list, __builtin_va_list, -__gnuc_va_list.
  • - -
  • From <stddef.h>: ptrdiff_t, -size_t, wchar_t.
  • - -
  • From <stdint.h>: int8_t, int16_t, -int32_t, int64_t, uint8_t, -uint16_t, uint32_t, uint64_t, -intptr_t, uintptr_t.
  • - -
-

-You're encouraged to use these types in preference to -compiler-specific extensions or target-dependent standard types. -E.g. char differs in signedness and long differs in -size, depending on the target architecture and platform ABI. -

-

-The following C features are not supported: -

-
    - -
  • A declaration must always have a type specifier; it doesn't -default to an int type.
  • - -
  • Old-style empty function declarations (K&R) are not allowed. -All C functions must have a proper prototype declaration. A -function declared without parameters (int foo();) is -treated as a function taking zero arguments, like in C++.
  • - -
  • The long double C type is parsed correctly, but -there's no support for the related conversions, accesses or arithmetic -operations.
  • - -
  • Wide character strings and character literals are not -supported.
  • - -
  • See below for features that are currently -not implemented.
  • - -
- -

C Type Conversion Rules

- -

Conversions from C types to Lua objects

-

-These conversion rules apply for read accesses to -C types: indexing pointers, arrays or -struct/union types; reading external variables or -constant values; retrieving return values from C calls: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
int8_t, int16_tsign-ext int32_tdoublenumber
uint8_t, uint16_tzero-ext int32_tdoublenumber
int32_t, uint32_tdoublenumber
int64_t, uint64_tboxed value64 bit int cdata
double, floatdoublenumber
bool0 → false, otherwise trueboolean
enumboxed valueenum cdata
Complex numberboxed valuecomplex cdata
Vectorboxed valuevector cdata
Pointerboxed valuepointer cdata
Arrayboxed referencereference cdata
struct/unionboxed referencereference cdata
-

-Bitfields are treated like their underlying type. -

-

-Reference types are dereferenced before a conversion can take -place — the conversion is applied to the C type pointed to -by the reference. -

- -

Conversions from Lua objects to C types

-

-These conversion rules apply for write accesses to -C types: indexing pointers, arrays or -struct/union types; initializing cdata objects; -casts to C types; writing to external variables; passing -arguments to C calls: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
numberdouble
booleanfalse → 0, true → 1bool
nilNULL(void *)
lightuserdatalightuserdata address →(void *)
userdatauserdata payload →(void *)
io.* fileget FILE * handle →(void *)
stringmatch against enum constantenum
stringcopy string data + zero-byteint8_t[], uint8_t[]
stringstring data →const char[]
functioncreate callbackC function type
tabletable initializerArray
tabletable initializerstruct/union
cdatacdata payload →C type
-

-If the result type of this conversion doesn't match the -C type of the destination, the -conversion rules between C types -are applied. -

-

-Reference types are immutable after initialization ("no re-seating of -references"). For initialization purposes or when passing values to -reference parameters, they are treated like pointers. Note that unlike -in C++, there's no way to implement automatic reference generation of -variables under the Lua language semantics. If you want to call a -function with a reference parameter, you need to explicitly pass a -one-element array. -

- -

Conversions between C types

-

-These conversion rules are more or less the same as the standard -C conversion rules. Some rules only apply to casts, or require -pointer or type compatibility: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
Signed integernarrow or sign-extendInteger
Unsigned integernarrow or zero-extendInteger
Integerrounddouble, float
double, floattrunc int32_tnarrow(u)int8_t, (u)int16_t
double, floattrunc(u)int32_t, (u)int64_t
double, floatroundfloat, double
Numbern == 0 → 0, otherwise 1bool
boolfalse → 0, true → 1Number
Complex numberconvert real partNumber
Numberconvert real part, imag = 0Complex number
Complex numberconvert real and imag partComplex number
Numberconvert scalar and replicateVector
Vectorcopy (same size)Vector
struct/uniontake base address (compat)Pointer
Arraytake base address (compat)Pointer
Functiontake function addressFunction pointer
Numberconvert via uintptr_t (cast)Pointer
Pointerconvert address (compat/cast)Pointer
Pointerconvert address (cast)Integer
Arrayconvert base address (cast)Integer
Arraycopy (compat)Array
struct/unioncopy (identical type)struct/union
-

-Bitfields or enum types are treated like their underlying -type. -

-

-Conversions not listed above will raise an error. E.g. it's not -possible to convert a pointer to a complex number or vice versa. -

- -

Conversions for vararg C function arguments

-

-The following default conversion rules apply when passing Lua objects -to the variable argument part of vararg C functions: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
numberdouble
booleanfalse → 0, true → 1bool
nilNULL(void *)
userdatauserdata payload →(void *)
lightuserdatalightuserdata address →(void *)
stringstring data →const char *
float cdatadouble
Array cdatatake base addressElement pointer
struct/union cdatatake base addressstruct/union pointer
Function cdatatake function addressFunction pointer
Any other cdatano conversionC type
-

-To pass a Lua object, other than a cdata object, as a specific type, -you need to override the conversion rules: create a temporary cdata -object with a constructor or a cast and initialize it with the value -to pass: -

-

-Assuming x is a Lua number, here's how to pass it as an -integer to a vararg function: -

-
-ffi.cdef[[
-int printf(const char *fmt, ...);
-]]
-ffi.C.printf("integer value: %d\n", ffi.new("int", x))
-
-

-If you don't do this, the default Lua number → double -conversion rule applies. A vararg C function expecting an integer -will see a garbled or uninitialized value. -

- -

Initializers

-

-Creating a cdata object with -ffi.new() or the -equivalent constructor syntax always initializes its contents, too. -Different rules apply, depending on the number of optional -initializers and the C types involved: -

-
    -
  • If no initializers are given, the object is filled with zero bytes.
  • - -
  • Scalar types (numbers and pointers) accept a single initializer. -The Lua object is converted to the scalar -C type.
  • - -
  • Valarrays (complex numbers and vectors) are treated like scalars -when a single initializer is given. Otherwise they are treated like -regular arrays.
  • - -
  • Aggregate types (arrays and structs) accept either a single cdata -initializer of the same type (copy constructor), a single -table initializer, or a flat list of -initializers.
  • - -
  • The elements of an array are initialized, starting at index zero. -If a single initializer is given for an array, it's repeated for all -remaining elements. This doesn't happen if two or more initializers -are given: all remaining uninitialized elements are filled with zero -bytes.
  • - -
  • Byte arrays may also be initialized with a Lua string. This copies -the whole string plus a terminating zero-byte. The copy stops early only -if the array has a known, fixed size.
  • - -
  • The fields of a struct are initialized in the order of -their declaration. Uninitialized fields are filled with zero -bytes.
  • - -
  • Only the first field of a union can be initialized with a -flat initializer.
  • - -
  • Elements or fields which are aggregates themselves are initialized -with a single initializer, but this may be a table -initializer or a compatible aggregate.
  • - -
  • Excess initializers cause an error.
  • - -
- -

Table Initializers

-

-The following rules apply if a Lua table is used to initialize an -Array or a struct/union: -

-
    - -
  • If the table index [0] is non-nil, then the -table is assumed to be zero-based. Otherwise it's assumed to be -one-based.
  • - -
  • Array elements, starting at index zero, are initialized one-by-one -with the consecutive table elements, starting at either index -[0] or [1]. This process stops at the first -nil table element.
  • - -
  • If exactly one array element was initialized, it's repeated for -all the remaining elements. Otherwise all remaining uninitialized -elements are filled with zero bytes.
  • - -
  • The above logic only applies to arrays with a known fixed size. -A VLA is only initialized with the element(s) given in the table. -Depending on the use case, you may need to explicitly add a -NULL or 0 terminator to a VLA.
  • - -
  • A struct/union can be initialized in the -order of the declaration of its fields. Each field is initialized with -consecutive table elements, starting at either index [0] -or [1]. This process stops at the first nil table -element.
  • - -
  • Otherwise, if neither index [0] nor [1] is present, -a struct/union is initialized by looking up each field -name (as a string key) in the table. Each non-nil value is -used to initialize the corresponding field.
  • - -
  • Uninitialized fields of a struct are filled with zero -bytes, except for the trailing VLA of a VLS.
  • - -
  • Initialization of a union stops after one field has been -initialized. If no field has been initialized, the union is -filled with zero bytes.
  • - -
  • Elements or fields which are aggregates themselves are initialized -with a single initializer, but this may be a nested table -initializer (or a compatible aggregate).
  • - -
  • Excess initializers for an array cause an error. Excess -initializers for a struct/union are ignored. -Unrelated table entries are ignored, too.
  • - -
-

-Example: -

-
-local ffi = require("ffi")
-
-ffi.cdef[[
-struct foo { int a, b; };
-union bar { int i; double d; };
-struct nested { int x; struct foo y; };
-]]
-
-ffi.new("int[3]", {})            --> 0, 0, 0
-ffi.new("int[3]", {1})           --> 1, 1, 1
-ffi.new("int[3]", {1,2})         --> 1, 2, 0
-ffi.new("int[3]", {1,2,3})       --> 1, 2, 3
-ffi.new("int[3]", {[0]=1})       --> 1, 1, 1
-ffi.new("int[3]", {[0]=1,2})     --> 1, 2, 0
-ffi.new("int[3]", {[0]=1,2,3})   --> 1, 2, 3
-ffi.new("int[3]", {[0]=1,2,3,4}) --> error: too many initializers
-
-ffi.new("struct foo", {})            --> a = 0, b = 0
-ffi.new("struct foo", {1})           --> a = 1, b = 0
-ffi.new("struct foo", {1,2})         --> a = 1, b = 2
-ffi.new("struct foo", {[0]=1,2})     --> a = 1, b = 2
-ffi.new("struct foo", {b=2})         --> a = 0, b = 2
-ffi.new("struct foo", {a=1,b=2,c=3}) --> a = 1, b = 2  'c' is ignored
-
-ffi.new("union bar", {})        --> i = 0, d = 0.0
-ffi.new("union bar", {1})       --> i = 1, d = ?
-ffi.new("union bar", {[0]=1,2}) --> i = 1, d = ?    '2' is ignored
-ffi.new("union bar", {d=2})     --> i = ?, d = 2.0
-
-ffi.new("struct nested", {1,{2,3}})     --> x = 1, y.a = 2, y.b = 3
-ffi.new("struct nested", {x=1,y={2,3}}) --> x = 1, y.a = 2, y.b = 3
-
- -

Operations on cdata Objects

-

-All of the standard Lua operators can be applied to cdata objects or a -mix of a cdata object and another Lua object. The following list shows -the pre-defined operations. -

-

-Reference types are dereferenced before performing each of -the operations below — the operation is applied to the -C type pointed to by the reference. -

-

-The pre-defined operations are always tried first before deferring to a -metamethod or index table (if any) for the corresponding ctype (except -for __new). An error is raised if the metamethod lookup or -index table lookup fails. -

- -

Indexing a cdata object

-
    - -
  • Indexing a pointer/array: a cdata pointer/array can be -indexed by a cdata number or a Lua number. The element address is -computed as the base address plus the number value multiplied by the -element size in bytes. A read access loads the element value and -converts it to a Lua object. A write -access converts a Lua object to the element -type and stores the converted value to the element. An error is -raised if the element size is undefined or a write access to a -constant element is attempted.
  • - -
  • Dereferencing a struct/union field: a -cdata struct/union or a pointer to a -struct/union can be dereferenced by a string key, -giving the field name. The field address is computed as the base -address plus the relative offset of the field. A read access loads the -field value and converts it to a Lua -object. A write access converts a Lua -object to the field type and stores the converted value to the -field. An error is raised if a write access to a constant -struct/union or a constant field is attempted. -Scoped enum constants or static constants are treated like a constant -field.
  • - -
  • Indexing a complex number: a complex number can be indexed -either by a cdata number or a Lua number with the values 0 or 1, or by -the strings "re" or "im". A read access loads the -real part ([0], .re) or the imaginary part -([1], .im) part of a complex number and -converts it to a Lua number. The -sub-parts of a complex number are immutable — assigning to an -index of a complex number raises an error. Accessing out-of-bound -indexes returns unspecified results, but is guaranteed not to trigger -memory access violations.
  • - -
  • Indexing a vector: a vector is treated like an array for -indexing purposes, except the vector elements are immutable — -assigning to an index of a vector raises an error.
  • - -
-

-A ctype object can be indexed with a string key, too. The only -pre-defined operation is reading scoped constants of -struct/union types. All other accesses defer -to the corresponding metamethods or index tables (if any). -

-

-Note: since there's (deliberately) no address-of operator, a cdata -object holding a value type is effectively immutable after -initialization. The JIT compiler benefits from this fact when applying -certain optimizations. -

-

-As a consequence, the elements of complex numbers and -vectors are immutable. But the elements of an aggregate holding these -types may be modified of course. I.e. you cannot assign to -foo.c.im, but you can assign a (newly created) complex number -to foo.c. -

-

-The JIT compiler implements strict aliasing rules: accesses to different -types do not alias, except for differences in signedness (this -applies even to char pointers, unlike C99). Type punning -through unions is explicitly detected and allowed. -

- -

Calling a cdata object

-
    - -
  • Constructor: a ctype object can be called and used as a -constructor. This is equivalent -to ffi.new(ct, ...), unless a __new metamethod is -defined. The __new metamethod is called with the ctype object -plus any other arguments passed to the contructor. Note that you have to -use ffi.new inside of it, since calling ct(...) would -cause infinite recursion.
  • - -
  • C function call: a cdata function or cdata function -pointer can be called. The passed arguments are -converted to the C types of the -parameters given by the function declaration. Arguments passed to the -variable argument part of vararg C function use -special conversion rules. This -C function is called and the return value (if any) is -converted to a Lua object.
    -On Windows/x86 systems, __stdcall functions are automatically -detected and a function declared as __cdecl (the default) is -silently fixed up after the first call.
  • - -
- -

Arithmetic on cdata objects

-
    - -
  • Pointer arithmetic: a cdata pointer/array and a cdata -number or a Lua number can be added or subtracted. The number must be -on the right hand side for a subtraction. The result is a pointer of -the same type with an address plus or minus the number value -multiplied by the element size in bytes. An error is raised if the -element size is undefined.
  • - -
  • Pointer difference: two compatible cdata pointers/arrays -can be subtracted. The result is the difference between their -addresses, divided by the element size in bytes. An error is raised if -the element size is undefined or zero.
  • - -
  • 64 bit integer arithmetic: the standard arithmetic -operators (+ - * / % ^ and unary -minus) can be applied to two cdata numbers, or a cdata number and a -Lua number. If one of them is an uint64_t, the other side is -converted to an uint64_t and an unsigned arithmetic operation -is performed. Otherwise both sides are converted to an -int64_t and a signed arithmetic operation is performed. The -result is a boxed 64 bit cdata object.
    - -If one of the operands is an enum and the other operand is a -string, the string is converted to the value of a matching enum -constant before the above conversion.
    - -These rules ensure that 64 bit integers are "sticky". Any -expression involving at least one 64 bit integer operand results -in another one. The undefined cases for the division, modulo and power -operators return 2LL ^ 63 or -2ULL ^ 63.
    - -You'll have to explicitly convert a 64 bit integer to a Lua -number (e.g. for regular floating-point calculations) with -tonumber(). But note this may incur a precision loss.
  • - -
- -

Comparisons of cdata objects

-
    - -
  • Pointer comparison: two compatible cdata pointers/arrays -can be compared. The result is the same as an unsigned comparison of -their addresses. nil is treated like a NULL pointer, -which is compatible with any other pointer type.
  • - -
  • 64 bit integer comparison: two cdata numbers, or a -cdata number and a Lua number can be compared with each other. If one -of them is an uint64_t, the other side is converted to an -uint64_t and an unsigned comparison is performed. Otherwise -both sides are converted to an int64_t and a signed -comparison is performed.
    - -If one of the operands is an enum and the other operand is a -string, the string is converted to the value of a matching enum -constant before the above conversion.
    - -
  • Comparisons for equality/inequality never raise an error. -Even incompatible pointers can be compared for equality by address. Any -other incompatible comparison (also with non-cdata objects) treats the -two sides as unequal.
  • - -
- -

cdata objects as table keys

-

-Lua tables may be indexed by cdata objects, but this doesn't provide -any useful semantics — cdata objects are unsuitable as table -keys! -

-

-A cdata object is treated like any other garbage-collected object and -is hashed and compared by its address for table indexing. Since -there's no interning for cdata value types, the same value may be -boxed in different cdata objects with different addresses. Thus -t[1LL+1LL] and t[2LL] usually do not point to -the same hash slot and they certainly do not point to the same -hash slot as t[2]. -

-

-It would seriously drive up implementation complexity and slow down -the common case, if one were to add extra handling for by-value -hashing and comparisons to Lua tables. Given the ubiquity of their use -inside the VM, this is not acceptable. -

-

-There are three viable alternatives, if you really need to use cdata -objects as keys: -

-
    - -
  • If you can get by with the precision of Lua numbers -(52 bits), then use tonumber() on a cdata number or -combine multiple fields of a cdata aggregate to a Lua number. Then use -the resulting Lua number as a key when indexing tables.
    -One obvious benefit: t[tonumber(2LL)] does point to -the same slot as t[2].
  • - -
  • Otherwise use either tostring() on 64 bit integers -or complex numbers or combine multiple fields of a cdata aggregate to -a Lua string (e.g. with -ffi.string()). Then -use the resulting Lua string as a key when indexing tables.
  • - -
  • Create your own specialized hash table implementation using the -C types provided by the FFI library, just like you would in -C code. Ultimately this may give much better performance than the -other alternatives or what a generic by-value hash table could -possibly provide.
  • - -
- -

Parameterized Types

-

-To facilitate some abstractions, the two functions -ffi.typeof and -ffi.cdef support -parameterized types in C declarations. Note: none of the other API -functions taking a cdecl allow this. -

-

-Any place you can write a typedef name, an -identifier or a number in a declaration, you can write -$ (the dollar sign) instead. These placeholders are replaced in -order of appearance with the arguments following the cdecl string: -

-
--- Declare a struct with a parameterized field type and name:
-ffi.cdef([[
-typedef struct { $ $; } foo_t;
-]], type1, name1)
-
--- Anonymous struct with dynamic names:
-local bar_t = ffi.typeof("struct { int $, $; }", name1, name2)
--- Derived pointer type:
-local bar_ptr_t = ffi.typeof("$ *", bar_t)
-
--- Parameterized dimensions work even where a VLA won't work:
-local matrix_t = ffi.typeof("uint8_t[$][$]", width, height)
-
-

-Caveat: this is not simple text substitution! A passed ctype or -cdata object is treated like the underlying type, a passed string is -considered an identifier and a number is considered a number. You must -not mix this up: e.g. passing "int" as a string doesn't work in -place of a type, you'd need to use ffi.typeof("int") instead. -

-

-The main use for parameterized types are libraries implementing abstract -data types -(» example), -similar to what can be achieved with C++ template metaprogramming. -Another use case are derived types of anonymous structs, which avoids -pollution of the global struct namespace. -

-

-Please note that parameterized types are a nice tool and indispensable -for certain use cases. But you'll want to use them sparingly in regular -code, e.g. when all types are actually fixed. -

- -

Garbage Collection of cdata Objects

-

-All explicitly (ffi.new(), ffi.cast() etc.) or -implicitly (accessors) created cdata objects are garbage collected. -You need to ensure to retain valid references to cdata objects -somewhere on a Lua stack, an upvalue or in a Lua table while they are -still in use. Once the last reference to a cdata object is gone, the -garbage collector will automatically free the memory used by it (at -the end of the next GC cycle). -

-

-Please note that pointers themselves are cdata objects, however they -are not followed by the garbage collector. So e.g. if you -assign a cdata array to a pointer, you must keep the cdata object -holding the array alive as long as the pointer is still in use: -

-
-ffi.cdef[[
-typedef struct { int *a; } foo_t;
-]]
-
-local s = ffi.new("foo_t", ffi.new("int[10]")) -- WRONG!
-
-local a = ffi.new("int[10]") -- OK
-local s = ffi.new("foo_t", a)
--- Now do something with 's', but keep 'a' alive until you're done.
-
-

-Similar rules apply for Lua strings which are implicitly converted to -"const char *": the string object itself must be -referenced somewhere or it'll be garbage collected eventually. The -pointer will then point to stale data, which may have already been -overwritten. Note that string literals are automatically kept -alive as long as the function containing it (actually its prototype) -is not garbage collected. -

-

-Objects which are passed as an argument to an external C function -are kept alive until the call returns. So it's generally safe to -create temporary cdata objects in argument lists. This is a common -idiom for passing specific C types to -vararg functions. -

-

-Memory areas returned by C functions (e.g. from malloc()) -must be manually managed, of course (or use -ffi.gc()). Pointers to -cdata objects are indistinguishable from pointers returned by C -functions (which is one of the reasons why the GC cannot follow them). -

- -

Callbacks

-

-The LuaJIT FFI automatically generates special callback functions -whenever a Lua function is converted to a C function pointer. This -associates the generated callback function pointer with the C type -of the function pointer and the Lua function object (closure). -

-

-This can happen implicitly due to the usual conversions, e.g. when -passing a Lua function to a function pointer argument. Or you can use -ffi.cast() to explicitly cast a Lua function to a -C function pointer. -

-

-Currently only certain C function types can be used as callback -functions. Neither C vararg functions nor functions with -pass-by-value aggregate argument or result types are supported. There -are no restrictions for the kind of Lua functions that can be called -from the callback — no checks for the proper number of arguments -are made. The return value of the Lua function will be converted to the -result type and an error will be thrown for invalid conversions. -

-

-It's allowed to throw errors across a callback invocation, but it's not -advisable in general. Do this only if you know the C function, that -called the callback, copes with the forced stack unwinding and doesn't -leak resources. -

-

-One thing that's not allowed, is to let an FFI call into a C function -get JIT-compiled, which in turn calls a callback, calling into Lua again. -Usually this attempt is caught by the interpreter first and the -C function is blacklisted for compilation. -

-

-However, this heuristic may fail under specific circumstances: e.g. a -message polling function might not run Lua callbacks right away and the call -gets JIT-compiled. If it later happens to call back into Lua (e.g. a rarely -invoked error callback), you'll get a VM PANIC with the message -"bad callback". Then you'll need to manually turn off -JIT-compilation with -jit.off() for the -surrounding Lua function that invokes such a message polling function (or -similar). -

- -

Callback resource handling

-

-Callbacks take up resources — you can only have a limited number -of them at the same time (500 - 1000, depending on the -architecture). The associated Lua functions are anchored to prevent -garbage collection, too. -

-

-Callbacks due to implicit conversions are permanent! There is no -way to guess their lifetime, since the C side might store the -function pointer for later use (typical for GUI toolkits). The associated -resources cannot be reclaimed until termination: -

-
-ffi.cdef[[
-typedef int (__stdcall *WNDENUMPROC)(void *hwnd, intptr_t l);
-int EnumWindows(WNDENUMPROC func, intptr_t l);
-]]
-
--- Implicit conversion to a callback via function pointer argument.
-local count = 0
-ffi.C.EnumWindows(function(hwnd, l)
-  count = count + 1
-  return true
-end, 0)
--- The callback is permanent and its resources cannot be reclaimed!
--- Ok, so this may not be a problem, if you do this only once.
-
-

-Note: this example shows that you must properly declare -__stdcall callbacks on Windows/x86 systems. The calling -convention cannot be automatically detected, unlike for -__stdcall calls to Windows functions. -

-

-For some use cases it's necessary to free up the resources or to -dynamically redirect callbacks. Use an explicit cast to a -C function pointer and keep the resulting cdata object. Then use -the cb:free() -or cb:set() methods -on the cdata object: -

-
--- Explicitly convert to a callback via cast.
-local count = 0
-local cb = ffi.cast("WNDENUMPROC", function(hwnd, l)
-  count = count + 1
-  return true
-end)
-
--- Pass it to a C function.
-ffi.C.EnumWindows(cb, 0)
--- EnumWindows doesn't need the callback after it returns, so free it.
-
-cb:free()
--- The callback function pointer is no longer valid and its resources
--- will be reclaimed. The created Lua closure will be garbage collected.
-
- -

Callback performance

-

-Callbacks are slow! First, the C to Lua transition itself -has an unavoidable cost, similar to a lua_call() or -lua_pcall(). Argument and result marshalling add to that cost. -And finally, neither the C compiler nor LuaJIT can inline or -optimize across the language barrier and hoist repeated computations out -of a callback function. -

-

-Do not use callbacks for performance-sensitive work: e.g. consider a -numerical integration routine which takes a user-defined function to -integrate over. It's a bad idea to call a user-defined Lua function from -C code millions of times. The callback overhead will be absolutely -detrimental for performance. -

-

-It's considerably faster to write the numerical integration routine -itself in Lua — the JIT compiler will be able to inline the -user-defined function and optimize it together with its calling context, -with very competitive performance. -

-

-As a general guideline: use callbacks only when you must, because -of existing C APIs. E.g. callback performance is irrelevant for a -GUI application, which waits for user input most of the time, anyway. -

-

-For new designs avoid push-style APIs: a C function repeatedly -calling a callback for each result. Instead use pull-style APIs: -call a C function repeatedly to get a new result. Calls from Lua -to C via the FFI are much faster than the other way round. Most well-designed -libraries already use pull-style APIs (read/write, get/put). -

- -

C Library Namespaces

-

-A C library namespace is a special kind of object which allows -access to the symbols contained in shared libraries or the default -symbol namespace. The default -ffi.C namespace is -automatically created when the FFI library is loaded. C library -namespaces for specific shared libraries may be created with the -ffi.load() API -function. -

-

-Indexing a C library namespace object with a symbol name (a Lua -string) automatically binds it to the library. First the symbol type -is resolved — it must have been declared with -ffi.cdef. Then the -symbol address is resolved by searching for the symbol name in the -associated shared libraries or the default symbol namespace. Finally, -the resulting binding between the symbol name, the symbol type and its -address is cached. Missing symbol declarations or nonexistent symbol -names cause an error. -

-

-This is what happens on a read access for the different kinds of -symbols: -

-
    - -
  • External functions: a cdata object with the type of the function -and its address is returned.
  • - -
  • External variables: the symbol address is dereferenced and the -loaded value is converted to a Lua object -and returned.
  • - -
  • Constant values (static const or enum -constants): the constant is converted to a -Lua object and returned.
  • - -
-

-This is what happens on a write access: -

-
    - -
  • External variables: the value to be written is -converted to the C type of the -variable and then stored at the symbol address.
  • - -
  • Writing to constant variables or to any other symbol type causes -an error, like any other attempted write to a constant location.
  • - -
-

-C library namespaces themselves are garbage collected objects. If -the last reference to the namespace object is gone, the garbage -collector will eventually release the shared library reference and -remove all memory associated with the namespace. Since this may -trigger the removal of the shared library from the memory of the -running process, it's generally not safe to use function -cdata objects obtained from a library if the namespace object may be -unreferenced. -

-

-Performance notice: the JIT compiler specializes to the identity of -namespace objects and to the strings used to index it. This -effectively turns function cdata objects into constants. It's not -useful and actually counter-productive to explicitly cache these -function objects, e.g. local strlen = ffi.C.strlen. OTOH it -is useful to cache the namespace itself, e.g. local C = -ffi.C. -

- -

No Hand-holding!

-

-The FFI library has been designed as a low-level library. The -goal is to interface with C code and C data types with a -minimum of overhead. This means you can do anything you can do -from C: access all memory, overwrite anything in memory, call -machine code at any memory address and so on. -

-

-The FFI library provides no memory safety, unlike regular Lua -code. It will happily allow you to dereference a NULL -pointer, to access arrays out of bounds or to misdeclare -C functions. If you make a mistake, your application might crash, -just like equivalent C code would. -

-

-This behavior is inevitable, since the goal is to provide full -interoperability with C code. Adding extra safety measures, like -bounds checks, would be futile. There's no way to detect -misdeclarations of C functions, since shared libraries only -provide symbol names, but no type information. Likewise there's no way -to infer the valid range of indexes for a returned pointer. -

-

-Again: the FFI library is a low-level library. This implies it needs -to be used with care, but it's flexibility and performance often -outweigh this concern. If you're a C or C++ developer, it'll be easy -to apply your existing knowledge. OTOH writing code for the FFI -library is not for the faint of heart and probably shouldn't be the -first exercise for someone with little experience in Lua, C or C++. -

-

-As a corollary of the above, the FFI library is not safe for use by -untrusted Lua code. If you're sandboxing untrusted Lua code, you -definitely don't want to give this code access to the FFI library or -to any cdata object (except 64 bit integers or complex -numbers). Any properly engineered Lua sandbox needs to provide safety -wrappers for many of the standard Lua library functions — -similar wrappers need to be written for high-level operations on FFI -data types, too. -

- -

Current Status

-

-The initial release of the FFI library has some limitations and is -missing some features. Most of these will be fixed in future releases. -

-

-C language support is -currently incomplete: -

-
    -
  • C declarations are not passed through a C pre-processor, -yet.
  • -
  • The C parser is able to evaluate most constant expressions -commonly found in C header files. However it doesn't handle the -full range of C expression semantics and may fail for some -obscure constructs.
  • -
  • static const declarations only work for integer types -up to 32 bits. Neither declaring string constants nor -floating-point constants is supported.
  • -
  • Packed struct bitfields that cross container boundaries -are not implemented.
  • -
  • Native vector types may be defined with the GCC mode or -vector_size attribute. But no operations other than loading, -storing and initializing them are supported, yet.
  • -
  • The volatile type qualifier is currently ignored by -compiled code.
  • -
  • ffi.cdef silently -ignores all re-declarations.
  • -
-

-The JIT compiler already handles a large subset of all FFI operations. -It automatically falls back to the interpreter for unimplemented -operations (you can check for this with the --jv command line option). -The following operations are currently not compiled and may exhibit -suboptimal performance, especially when used in inner loops: -

-
    -
  • Bitfield accesses and initializations.
  • -
  • Vector operations.
  • -
  • Table initializers.
  • -
  • Initialization of nested struct/union types.
  • -
  • Allocations of variable-length arrays or structs.
  • -
  • Allocations of C types with a size > 128 bytes or an -alignment > 8 bytes.
  • -
  • Conversions from lightuserdata to void *.
  • -
  • Pointer differences for element sizes that are not a power of -two.
  • -
  • Calls to C functions with aggregates passed or returned by -value.
  • -
  • Calls to ctype metamethods which are not plain functions.
  • -
  • ctype __newindex tables and non-string lookups in ctype -__index tables.
  • -
  • tostring() for cdata types.
  • -
  • Calls to ffi.cdef(), ffi.load() and -ffi.metatype().
  • -
-

-Other missing features: -

-
    -
  • Bit operations for 64 bit types.
  • -
  • Arithmetic for complex numbers.
  • -
  • Passing structs by value to vararg C functions.
  • -
  • C++ exception interoperability -does not extend to C functions called via the FFI, if the call is -compiled.
  • -
-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/ext_ffi_tutorial.html b/third-party/LuaJIT-2.0.2/doc/ext_ffi_tutorial.html deleted file mode 100644 index 30213b31b0..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/ext_ffi_tutorial.html +++ /dev/null @@ -1,601 +0,0 @@ - - - -FFI Tutorial - - - - - - - - - -
-Lua -
- - -
-

-This page is intended to give you an overview of the features of the FFI -library by presenting a few use cases and guidelines. -

-

-This page makes no attempt to explain all of the FFI library, though. -You'll want to have a look at the ffi.* API -function reference and the FFI -semantics to learn more. -

- -

Loading the FFI Library

-

-The FFI library is built into LuaJIT by default, but it's not loaded -and initialized by default. The suggested way to use the FFI library -is to add the following to the start of every Lua file that needs one -of its functions: -

-
-local ffi = require("ffi")
-
-

-Please note this doesn't define an ffi variable in the table -of globals — you really need to use the local variable. The -require function ensures the library is only loaded once. -

-

-Note: If you want to experiment with the FFI from the interactive prompt -of the command line executable, omit the local, as it doesn't -preserve local variables across lines. -

- -

Accessing Standard System Functions

-

-The following code explains how to access standard system functions. -We slowly print two lines of dots by sleeping for 10 milliseconds -after each dot: -

-
- 
-①
-
-
-
-
-
-②
-③
-④
-
-
-
-⑤
-
-
-
-
-
-⑥local ffi = require("ffi")
-ffi.cdef[[
-void Sleep(int ms);
-int poll(struct pollfd *fds, unsigned long nfds, int timeout);
-]]
-
-local sleep
-if ffi.os == "Windows" then
-  function sleep(s)
-    ffi.C.Sleep(s*1000)
-  end
-else
-  function sleep(s)
-    ffi.C.poll(nil, 0, s*1000)
-  end
-end
-
-for i=1,160 do
-  io.write("."); io.flush()
-  sleep(0.01)
-end
-io.write("\n")
-
-

-Here's the step-by-step explanation: -

-

- This defines the -C library functions we're going to use. The part inside the -double-brackets (in green) is just standard C syntax. You can -usually get this info from the C header files or the -documentation provided by each C library or C compiler. -

-

- The difficulty we're -facing here, is that there are different standards to choose from. -Windows has a simple Sleep() function. On other systems there -are a variety of functions available to achieve sub-second sleeps, but -with no clear consensus. Thankfully poll() can be used for -this task, too, and it's present on most non-Windows systems. The -check for ffi.os makes sure we use the Windows-specific -function only on Windows systems. -

-

- Here we're wrapping the -call to the C function in a Lua function. This isn't strictly -necessary, but it's helpful to deal with system-specific issues only -in one part of the code. The way we're wrapping it ensures the check -for the OS is only done during initialization and not for every call. -

-

- A more subtle point is -that we defined our sleep() function (for the sake of this -example) as taking the number of seconds, but accepting fractional -seconds. Multiplying this by 1000 gets us milliseconds, but that still -leaves it a Lua number, which is a floating-point value. Alas, the -Sleep() function only accepts an integer value. Luckily for -us, the FFI library automatically performs the conversion when calling -the function (truncating the FP value towards zero, like in C). -

-

-Some readers will notice that Sleep() is part of -KERNEL32.DLL and is also a stdcall function. So how -can this possibly work? The FFI library provides the ffi.C -default C library namespace, which allows calling functions from -the default set of libraries, like a C compiler would. Also, the -FFI library automatically detects stdcall functions, so you -don't need to declare them as such. -

-

- The poll() -function takes a couple more arguments we're not going to use. You can -simply use nil to pass a NULL pointer and 0 -for the nfds parameter. Please note that the -number 0 does not convert to a pointer value, -unlike in C++. You really have to pass pointers to pointer arguments -and numbers to number arguments. -

-

-The page on FFI semantics has all -of the gory details about -conversions between Lua -objects and C types. For the most part you don't have to deal -with this, as it's performed automatically and it's carefully designed -to bridge the semantic differences between Lua and C. -

-

- Now that we have defined -our own sleep() function, we can just call it from plain Lua -code. That wasn't so bad, huh? Turning these boring animated dots into -a fascinating best-selling game is left as an exercise for the reader. -:-) -

- -

Accessing the zlib Compression Library

-

-The following code shows how to access the zlib compression library from Lua code. -We'll define two convenience wrapper functions that take a string and -compress or uncompress it to another string: -

-
- 
-①
-
-
-
-
-
-
-②
-
-
-③
-
-④
-
-
-⑤
-
-
-⑥
-
-
-
-
-
-
-
-⑦local ffi = require("ffi")
-ffi.cdef[[
-unsigned long compressBound(unsigned long sourceLen);
-int compress2(uint8_t *dest, unsigned long *destLen,
-	      const uint8_t *source, unsigned long sourceLen, int level);
-int uncompress(uint8_t *dest, unsigned long *destLen,
-	       const uint8_t *source, unsigned long sourceLen);
-]]
-local zlib = ffi.load(ffi.os == "Windows" and "zlib1" or "z")
-
-local function compress(txt)
-  local n = zlib.compressBound(#txt)
-  local buf = ffi.new("uint8_t[?]", n)
-  local buflen = ffi.new("unsigned long[1]", n)
-  local res = zlib.compress2(buf, buflen, txt, #txt, 9)
-  assert(res == 0)
-  return ffi.string(buf, buflen[0])
-end
-
-local function uncompress(comp, n)
-  local buf = ffi.new("uint8_t[?]", n)
-  local buflen = ffi.new("unsigned long[1]", n)
-  local res = zlib.uncompress(buf, buflen, comp, #comp)
-  assert(res == 0)
-  return ffi.string(buf, buflen[0])
-end
-
--- Simple test code.
-local txt = string.rep("abcd", 1000)
-print("Uncompressed size: ", #txt)
-local c = compress(txt)
-print("Compressed size: ", #c)
-local txt2 = uncompress(c, #txt)
-assert(txt2 == txt)
-
-

-Here's the step-by-step explanation: -

-

- This defines some of the -C functions provided by zlib. For the sake of this example, some -type indirections have been reduced and it uses the pre-defined -fixed-size integer types, while still adhering to the zlib API/ABI. -

-

- This loads the zlib shared -library. On POSIX systems it's named libz.so and usually -comes pre-installed. Since ffi.load() automatically adds any -missing standard prefixes/suffixes, we can simply load the -"z" library. On Windows it's named zlib1.dll and -you'll have to download it first from the -» zlib site. The check for -ffi.os makes sure we pass the right name to -ffi.load(). -

-

- First, the maximum size of -the compression buffer is obtained by calling the -zlib.compressBound function with the length of the -uncompressed string. The next line allocates a byte buffer of this -size. The [?] in the type specification indicates a -variable-length array (VLA). The actual number of elements of this -array is given as the 2nd argument to ffi.new(). -

-

- This may look strange at -first, but have a look at the declaration of the compress2 -function from zlib: the destination length is defined as a pointer! -This is because you pass in the maximum buffer size and get back the -actual length that was used. -

-

-In C you'd pass in the address of a local variable -(&buflen). But since there's no address-of operator in -Lua, we'll just pass in a one-element array. Conveniently it can be -initialized with the maximum buffer size in one step. Calling the -actual zlib.compress2 function is then straightforward. -

-

- We want to return the -compressed data as a Lua string, so we'll use ffi.string(). -It needs a pointer to the start of the data and the actual length. The -length has been returned in the buflen array, so we'll just -get it from there. -

-

-Note that since the function returns now, the buf and -buflen variables will eventually be garbage collected. This -is fine, because ffi.string() has copied the contents to a -newly created (interned) Lua string. If you plan to call this function -lots of times, consider reusing the buffers and/or handing back the -results in buffers instead of strings. This will reduce the overhead -for garbage collection and string interning. -

-

- The uncompress -functions does the exact opposite of the compress function. -The compressed data doesn't include the size of the original string, -so this needs to be passed in. Otherwise no surprises here. -

-

- The code, that makes use -of the functions we just defined, is just plain Lua code. It doesn't -need to know anything about the LuaJIT FFI — the convenience -wrapper functions completely hide it. -

-

-One major advantage of the LuaJIT FFI is that you are now able to -write those wrappers in Lua. And at a fraction of the time it -would cost you to create an extra C module using the Lua/C API. -Many of the simpler C functions can probably be used directly -from your Lua code, without any wrappers. -

-

-Side note: the zlib API uses the long type for passing -lengths and sizes around. But all those zlib functions actually only -deal with 32 bit values. This is an unfortunate choice for a -public API, but may be explained by zlib's history — we'll just -have to deal with it. -

-

-First, you should know that a long is a 64 bit type e.g. -on POSIX/x64 systems, but a 32 bit type on Windows/x64 and on -32 bit systems. Thus a long result can be either a plain -Lua number or a boxed 64 bit integer cdata object, depending on -the target system. -

-

-Ok, so the ffi.* functions generally accept cdata objects -wherever you'd want to use a number. That's why we get a away with -passing n to ffi.string() above. But other Lua -library functions or modules don't know how to deal with this. So for -maximum portability one needs to use tonumber() on returned -long results before passing them on. Otherwise the -application might work on some systems, but would fail in a POSIX/x64 -environment. -

- -

Defining Metamethods for a C Type

-

-The following code explains how to define metamethods for a C type. -We define a simple point type and add some operations to it: -

-
- 
-①
-
-
-
-②
-
-③
-
-④
-
-
-
-⑤
-
-⑥local ffi = require("ffi")
-ffi.cdef[[
-typedef struct { double x, y; } point_t;
-]]
-
-local point
-local mt = {
-  __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
-  __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
-  __index = {
-    area = function(a) return a.x*a.x + a.y*a.y end,
-  },
-}
-point = ffi.metatype("point_t", mt)
-
-local a = point(3, 4)
-print(a.x, a.y)  --> 3  4
-print(#a)        --> 5
-print(a:area())  --> 25
-local b = a + point(0.5, 8)
-print(#b)        --> 12.5
-
-

-Here's the step-by-step explanation: -

-

- This defines the C type for a -two-dimensional point object. -

-

- We have to declare the variable -holding the point constructor first, because it's used inside of a -metamethod. -

-

- Let's define an __add -metamethod which adds the coordinates of two points and creates a new -point object. For simplicity, this function assumes that both arguments -are points. But it could be any mix of objects, if at least one operand -is of the required type (e.g. adding a point plus a number or vice -versa). Our __len metamethod returns the distance of a point to -the origin. -

-

- If we run out of operators, we can -define named methods, too. Here the __index table defines an -area function. For custom indexing needs, one might want to -define __index and __newindex functions instead. -

-

- This associates the metamethods with -our C type. This only needs to be done once. For convenience, a -constructor is returned by -ffi.metatype(). -We're not required to use it, though. The original C type can still -be used e.g. to create an array of points. The metamethods automatically -apply to any and all uses of this type. -

-

-Please note that the association with a metatable is permanent and -the metatable must not be modified afterwards! Ditto for the -__index table. -

-

- Here are some simple usage examples -for the point type and their expected results. The pre-defined -operations (such as a.x) can be freely mixed with the newly -defined metamethods. Note that area is a method and must be -called with the Lua syntax for methods: a:area(), not -a.area(). -

-

-The C type metamethod mechanism is most useful when used in -conjunction with C libraries that are written in an object-oriented -style. Creators return a pointer to a new instance and methods take an -instance pointer as the first argument. Sometimes you can just point -__index to the library namespace and __gc to the -destructor and you're done. But often enough you'll want to add -convenience wrappers, e.g. to return actual Lua strings or when -returning multiple values. -

-

-Some C libraries only declare instance pointers as an opaque -void * type. In this case you can use a fake type for all -declarations, e.g. a pointer to a named (incomplete) struct will do: -typedef struct foo_type *foo_handle. The C side doesn't -know what you declare with the LuaJIT FFI, but as long as the underlying -types are compatible, everything still works. -

- -

Translating C Idioms

-

-Here's a list of common C idioms and their translation to the -LuaJIT FFI: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IdiomC codeLua code
Pointer dereference
int *p;
x = *p;
*p = y;
x = p[0]
p[0] = y
Pointer indexing
int i, *p;
x = p[i];
p[i+1] = y;
x = p[i]
p[i+1] = y
Array indexing
int i, a[];
x = a[i];
a[i+1] = y;
x = a[i]
a[i+1] = y
struct/union dereference
struct foo s;
x = s.field;
s.field = y;
x = s.field
s.field = y
struct/union pointer deref.
struct foo *sp;
x = sp->field;
sp->field = y;
x = s.field
s.field = y
Pointer arithmetic
int i, *p;
x = p + i;
y = p - i;
x = p + i
y = p - i
Pointer difference
int *p1, *p2;
x = p1 - p2;x = p1 - p2
Array element pointer
int i, a[];
x = &a[i];x = a+i
Cast pointer to address
int *p;
x = (intptr_t)p;x = tonumber(
 ffi.cast("intptr_t",
          p))
Functions with outargs
void foo(int *inoutlen);
int len = x;
foo(&len);
y = len;
local len =
  ffi.new("int[1]", x)
foo(len)
y = len[0]
Vararg conversions
int printf(char *fmt, ...);
printf("%g", 1.0);
printf("%d", 1);
 
printf("%g", 1);
printf("%d",
  ffi.new("int", 1))
- -

To Cache or Not to Cache

-

-It's a common Lua idiom to cache library functions in local variables -or upvalues, e.g.: -

-
-local byte, char = string.byte, string.char
-local function foo(x)
-  return char(byte(x)+1)
-end
-
-

-This replaces several hash-table lookups with a (faster) direct use of -a local or an upvalue. This is less important with LuaJIT, since the -JIT compiler optimizes hash-table lookups a lot and is even able to -hoist most of them out of the inner loops. It can't eliminate -all of them, though, and it saves some typing for often-used -functions. So there's still a place for this, even with LuaJIT. -

-

-The situation is a bit different with C function calls via the -FFI library. The JIT compiler has special logic to eliminate all -of the lookup overhead for functions resolved from a -C library namespace! -Thus it's not helpful and actually counter-productive to cache -individual C functions like this: -

-
-local funca, funcb = ffi.C.funcb, ffi.C.funcb -- Not helpful!
-local function foo(x, n)
-  for i=1,n do funcb(funca(x, i), 1) end
-end
-
-

-This turns them into indirect calls and generates bigger and slower -machine code. Instead you'll want to cache the namespace itself and -rely on the JIT compiler to eliminate the lookups: -

-
-local C = ffi.C          -- Instead use this!
-local function foo(x, n)
-  for i=1,n do C.funcb(C.funca(x, i), 1) end
-end
-
-

-This generates both shorter and faster code. So don't cache -C functions, but do cache namespaces! Most often the -namespace is already in a local variable at an outer scope, e.g. from -local lib = ffi.load(...). Note that copying -it to a local variable in the function scope is unnecessary. -

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/ext_jit.html b/third-party/LuaJIT-2.0.2/doc/ext_jit.html deleted file mode 100644 index cc00e72b1b..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/ext_jit.html +++ /dev/null @@ -1,199 +0,0 @@ - - - -jit.* Library - - - - - - - - -
-Lua -
- - -
-

-The functions in this built-in module control the behavior of the JIT -compiler engine. Note that JIT-compilation is fully automatic — -you probably won't need to use any of the following functions unless -you have special needs. -

- -

jit.on()
-jit.off()

-

-Turns the whole JIT compiler on (default) or off. -

-

-These functions are typically used with the command line options --j on or -j off. -

- -

jit.flush()

-

-Flushes the whole cache of compiled code. -

- -

jit.on(func|true [,true|false])
-jit.off(func|true [,true|false])
-jit.flush(func|true [,true|false])

-

-jit.on enables JIT compilation for a Lua function (this is -the default). -

-

-jit.off disables JIT compilation for a Lua function and -flushes any already compiled code from the code cache. -

-

-jit.flush flushes the code, but doesn't affect the -enable/disable status. -

-

-The current function, i.e. the Lua function calling this library -function, can also be specified by passing true as the first -argument. -

-

-If the second argument is true, JIT compilation is also -enabled, disabled or flushed recursively for all sub-functions of a -function. With false only the sub-functions are affected. -

-

-The jit.on and jit.off functions only set a flag -which is checked when the function is about to be compiled. They do -not trigger immediate compilation. -

-

-Typical usage is jit.off(true, true) in the main chunk -of a module to turn off JIT compilation for the whole module for -debugging purposes. -

- -

jit.flush(tr)

-

-Flushes the root trace, specified by its number, and all of its side -traces from the cache. The code for the trace will be retained as long -as there are any other traces which link to it. -

- -

status, ... = jit.status()

-

-Returns the current status of the JIT compiler. The first result is -either true or false if the JIT compiler is turned -on or off. The remaining results are strings for CPU-specific features -and enabled optimizations. -

- -

jit.version

-

-Contains the LuaJIT version string. -

- -

jit.version_num

-

-Contains the version number of the LuaJIT core. Version xx.yy.zz -is represented by the decimal number xxyyzz. -

- -

jit.os

-

-Contains the target OS name: -"Windows", "Linux", "OSX", "BSD", "POSIX" or "Other". -

- -

jit.arch

-

-Contains the target architecture name: -"x86", "x64" or "ppcspe". -

- -

jit.opt.* — JIT compiler optimization control

-

-This sub-module provides the backend for the -O command line -option. -

-

-You can also use it programmatically, e.g.: -

-
-jit.opt.start(2) -- same as -O2
-jit.opt.start("-dce")
-jit.opt.start("hotloop=10", "hotexit=2")
-
-

-Unlike in LuaJIT 1.x, the module is built-in and -optimization is turned on by default! -It's no longer necessary to run require("jit.opt").start(), -which was one of the ways to enable optimization. -

- -

jit.util.* — JIT compiler introspection

-

-This sub-module holds functions to introspect the bytecode, generated -traces, the IR and the generated machine code. The functionality -provided by this module is still in flux and therefore undocumented. -

-

-The debug modules -jbc, -jv and -jdump make -extensive use of these functions. Please check out their source code, -if you want to know more. -

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/extensions.html b/third-party/LuaJIT-2.0.2/doc/extensions.html deleted file mode 100644 index 8684dc32a0..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/extensions.html +++ /dev/null @@ -1,408 +0,0 @@ - - - -Extensions - - - - - - - - - -
-Lua -
- - -
-

-LuaJIT is fully upwards-compatible with Lua 5.1. It supports all -» standard Lua -library functions and the full set of -» Lua/C API -functions. -

-

-LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic -loader level. This means you can compile a C module against the -standard Lua headers and load the same shared library from either Lua -or LuaJIT. -

-

-LuaJIT extends the standard Lua VM with new functionality and adds -several extension modules. Please note this page is only about -functional enhancements and not about performance enhancements, -such as the optimized VM, the faster interpreter or the JIT compiler. -

- -

Extensions Modules

-

-LuaJIT comes with several built-in extension modules: -

- -

bit.* — Bitwise operations

-

-LuaJIT supports all bitwise operations as defined by -» Lua BitOp: -

-
-bit.tobit  bit.tohex  bit.bnot    bit.band bit.bor  bit.bxor
-bit.lshift bit.rshift bit.arshift bit.rol  bit.ror  bit.bswap
-
-

-This module is a LuaJIT built-in — you don't need to download or -install Lua BitOp. The Lua BitOp site has full documentation for all -» Lua BitOp API functions. -

-

-Please make sure to require the module before using any of -its functions: -

-
-local bit = require("bit")
-
-

-An already installed Lua BitOp module is ignored by LuaJIT. -This way you can use bit operations from both Lua and LuaJIT on a -shared installation. -

- -

ffi.* — FFI library

-

-The FFI library allows calling external -C functions and the use of C data structures from pure Lua -code. -

- -

jit.* — JIT compiler control

-

-The functions in this module -control the behavior of the JIT compiler engine. -

- -

C API extensions

-

-LuaJIT adds some -extra functions to the Lua/C API. -

- -

Enhanced Standard Library Functions

- -

xpcall(f, err [,args...]) passes arguments

-

-Unlike the standard implementation in Lua 5.1, xpcall() -passes any arguments after the error function to the function -which is called in a protected context. -

- -

loadfile() etc. handle UTF-8 source code

-

-Non-ASCII characters are handled transparently by the Lua source code parser. -This allows the use of UTF-8 characters in identifiers and strings. -A UTF-8 BOM is skipped at the start of the source code. -

- -

tostring() etc. canonicalize NaN and ±Inf

-

-All number-to-string conversions consistently convert non-finite numbers -to the same strings on all platforms. NaN results in "nan", -positive infinity results in "inf" and negative infinity results -in "-inf". -

- -

tonumber() etc. use builtin string to number conversion

-

-All string-to-number conversions consistently convert integer and -floating-point inputs in decimal and hexadecimal on all platforms. -strtod() is not used anymore, which avoids numerous -problems with poor C library implementations. The builtin conversion -function provides full precision according to the IEEE-754 standard, it -works independently of the current locale and it supports hex floating-point -numbers (e.g. 0x1.5p-3). -

- -

string.dump(f [,strip]) generates portable bytecode

-

-An extra argument has been added to string.dump(). If set to -true, 'stripped' bytecode without debug information is -generated. This speeds up later bytecode loading and reduces memory -usage. See also the --b command line option. -

-

-The generated bytecode is portable and can be loaded on any architecture -that LuaJIT supports, independent of word size or endianess. However the -bytecode compatibility versions must match. Bytecode stays compatible -for dot releases (x.y.0 → x.y.1), but may change with major or -minor releases (2.0 → 2.1) or between any beta release. Foreign -bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded. -

- -

Enhanced PRNG for math.random()

-

-LuaJIT uses a Tausworthe PRNG with period 2^223 to implement -math.random() and math.randomseed(). The quality of -the PRNG results is much superior compared to the standard Lua -implementation which uses the platform-specific ANSI rand(). -

-

-The PRNG generates the same sequences from the same seeds on all -platforms and makes use of all bits in the seed argument. -math.random() without arguments generates 52 pseudo-random bits -for every call. The result is uniformly distributed between 0.0 and 1.0. -It's correctly scaled up and rounded for math.random(n [,m]) to -preserve uniformity. -

- -

io.* functions handle 64 bit file offsets

-

-The file I/O functions in the standard io.* library handle -64 bit file offsets. In particular this means it's possible -to open files larger than 2 Gigabytes and to reposition or obtain -the current file position for offsets beyond 2 GB -(fp:seek() method). -

- -

debug.* functions identify metamethods

-

-debug.getinfo() and lua_getinfo() also return information -about invoked metamethods. The namewhat field is set to -"metamethod" and the name field has the name of -the corresponding metamethod (e.g. "__index"). -

- -

Fully Resumable VM

-

-The LuaJIT VM is fully resumable. This means you can yield from a -coroutine even across contexts, where this would not possible with -the standard Lua 5.1 VM: e.g. you can yield across pcall() -and xpcall(), across iterators and across metamethods. -

- -

Extensions from Lua 5.2

-

-LuaJIT supports some language and library extensions from Lua 5.2. -Features that are unlikely to break existing code are unconditionally -enabled: -

-
    -
  • goto and ::labels::.
  • -
  • Hex escapes '\x3F' and '\*' escape in strings.
  • -
  • load(string|reader [, chunkname [,mode [,env]]]).
  • -
  • loadstring() is an alias for load().
  • -
  • loadfile(filename [,mode [,env]]).
  • -
  • math.log(x [,base]). -
  • string.rep(s, n [,sep]). -
  • string.format(): %q reversible. -%s checks __tostring. -%a and "%A added.
  • -
  • String matching pattern %g added.
  • -
  • io.read("*L").
  • -
  • io.lines() and file:lines() process -io.read() options.
  • -
  • os.exit(status|true|false [,close]).
  • -
  • package.searchpath(name, path [, sep [, rep]]).
  • -
  • package.loadlib(name, "*").
  • -
  • debug.getinfo() returns nparams and isvararg -for option "u".
  • -
  • debug.getlocal() accepts function instead of level.
  • -
  • debug.getlocal() and debug.setlocal() accept negative -indexes for varargs.
  • -
  • debug.getupvalue() and debug.setupvalue() handle -C functions.
  • -
  • debug.upvalueid() and debug.upvaluejoin().
  • -
  • Command line option -E.
  • -
  • Command line checks __tostring for errors.
  • -
-

-Other features are only enabled, if LuaJIT is built with --DLUAJIT_ENABLE_LUA52COMPAT: -

-
    -
  • goto is a keyword and not a valid variable name anymore.
  • -
  • break can be placed anywhere. Empty statements (;;) -are allowed.
  • -
  • __lt, __le are invoked for mixed types.
  • -
  • __len for tables. rawlen() library function.
  • -
  • pairs() and ipairs() check for __pairs and -__ipairs.
  • -
  • coroutine.running() returns two results.
  • -
  • table.pack() and table.unpack() -(same as unpack()).
  • -
  • io.write() and file:write() return file handle -instead of true.
  • -
  • os.execute() and pipe:close() return detailed -exit status.
  • -
  • debug.setmetatable() returns object.
  • -
  • debug.getuservalue() and debug.setuservalue().
  • -
  • Remove math.mod(), string.gfind(). -
-

-Note: this provides only partial compatibility with Lua 5.2 at the -language and Lua library level. LuaJIT is API+ABI-compatible with -Lua 5.1, which prevents implementing features that would otherwise -break the Lua/C API and ABI (e.g. _ENV). -

- -

C++ Exception Interoperability

-

-LuaJIT has built-in support for interoperating with C++ exceptions. -The available range of features depends on the target platform and -the toolchain used to compile LuaJIT: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PlatformCompilerInteroperability
POSIX/x64, DWARF2 unwindingGCC 4.3+Full
Other platforms, DWARF2 unwindingGCCLimited
Windows/x64MSVC or WinSDKFull
Windows/x86AnyNo
Other platformsOther compilersNo
-

-Full interoperability means: -

-
    -
  • C++ exceptions can be caught on the Lua side with pcall(), -lua_pcall() etc.
  • -
  • C++ exceptions will be converted to the generic Lua error -"C++ exception", unless you use the -C call wrapper feature.
  • -
  • It's safe to throw C++ exceptions across non-protected Lua frames -on the C stack. The contents of the C++ exception object -pass through unmodified.
  • -
  • Lua errors can be caught on the C++ side with catch(...). -The corresponding Lua error message can be retrieved from the Lua stack.
  • -
  • Throwing Lua errors across C++ frames is safe. C++ destructors -will be called.
  • -
-

-Limited interoperability means: -

-
    -
  • C++ exceptions can be caught on the Lua side with pcall(), -lua_pcall() etc.
  • -
  • C++ exceptions will be converted to the generic Lua error -"C++ exception", unless you use the -C call wrapper feature.
  • -
  • C++ exceptions will be caught by non-protected Lua frames and -are rethrown as a generic Lua error. The C++ exception object will -be destroyed.
  • -
  • Lua errors cannot be caught on the C++ side.
  • -
  • Throwing Lua errors across C++ frames will not call -C++ destructors.
  • -
- -

-No interoperability means: -

-
    -
  • It's not safe to throw C++ exceptions across Lua frames.
  • -
  • C++ exceptions cannot be caught on the Lua side.
  • -
  • Lua errors cannot be caught on the C++ side.
  • -
  • Throwing Lua errors across C++ frames will not call -C++ destructors.
  • -
  • Additionally, on Windows/x86 with SEH-based C++ exceptions: -it's not safe to throw a Lua error across any frames containing -a C++ function with any try/catch construct or using variables with -(implicit) destructors. This also applies to any functions which may be -inlined in such a function. It doesn't matter whether lua_error() -is called inside or outside of a try/catch or whether any object actually -needs to be destroyed: the SEH chain is corrupted and this will eventually -lead to the termination of the process.
  • -
-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/faq.html b/third-party/LuaJIT-2.0.2/doc/faq.html deleted file mode 100644 index c61b8dcf1f..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/faq.html +++ /dev/null @@ -1,184 +0,0 @@ - - - -Frequently Asked Questions (FAQ) - - - - - - - - - -
-Lua -
- - -
-
-
Q: Where can I learn more about LuaJIT and Lua?
-
- -
- -
-
Q: Where can I learn more about the compiler technology used by LuaJIT?
-
-I'm planning to write more documentation about the internals of LuaJIT. -In the meantime, please use the following Google Scholar searches -to find relevant papers:
-Search for: » Trace Compiler
-Search for: » JIT Compiler
-Search for: » Dynamic Language Optimizations
-Search for: » SSA Form
-Search for: » Linear Scan Register Allocation
-Here is a list of the » innovative features in LuaJIT.
-And, you know, reading the source is of course the only way to enlightenment. :-) -
-
- -
-
Q: Why do I get this error: "attempt to index global 'arg' (a nil value)"?
-Q: My vararg functions fail after switching to LuaJIT!
-
LuaJIT is compatible to the Lua 5.1 language standard. It doesn't -support the implicit arg parameter for old-style vararg -functions from Lua 5.0.
Please convert your code to the -» Lua 5.1 -vararg syntax.
-
- -
-
Q: Why do I get this error: "bad FPU precision"?
-
Q: I get weird behavior after initializing Direct3D.
-
Q: Some FPU operations crash after I load a Delphi DLL.
-
-
- -DirectX/Direct3D (up to version 9) sets the x87 FPU to single-precision -mode by default. This violates the Windows ABI and interferes with the -operation of many programs — LuaJIT is affected, too. Please make -sure you always use the D3DCREATE_FPU_PRESERVE flag when -initializing Direct3D.
- -Direct3D version 10 or higher do not show this behavior anymore. -Consider testing your application with older versions, too.
- -Similarly, the Borland/Delphi runtime modifies the FPU control word and -enables FP exceptions. Of course this violates the Windows ABI, too. -Please check the Delphi docs for the Set8087CW method. - -
- -
-
Q: Sometimes Ctrl-C fails to stop my Lua program. Why?
-
The interrupt signal handler sets a Lua debug hook. But this is -currently ignored by compiled code (this will eventually be fixed). If -your program is running in a tight loop and never falls back to the -interpreter, the debug hook never runs and can't throw the -"interrupted!" error.
In the meantime you have to press Ctrl-C -twice to get stop your program. That's similar to when it's stuck -running inside a C function under the Lua interpreter.
-
- -
-
Q: Why doesn't my favorite power-patch for Lua apply against LuaJIT?
-
Because it's a completely redesigned VM and has very little code -in common with Lua anymore. Also, if the patch introduces changes to -the Lua semantics, these would need to be reflected everywhere in the -VM, from the interpreter up to all stages of the compiler.
Please -use only standard Lua language constructs. For many common needs you -can use source transformations or use wrapper or proxy functions. -The compiler will happily optimize away such indirections.
-
- -
-
Q: Lua runs everywhere. Why doesn't LuaJIT support my CPU?
-
Because it's a compiler — it needs to generate native -machine code. This means the code generator must be ported to each -architecture. And the fast interpreter is written in assembler and -must be ported, too. This is quite an undertaking.
-The install documentation shows the supported -architectures. Other architectures will follow based on sufficient user -demand and/or sponsoring.
-
- -
-
Q: When will feature X be added? When will the next version be released?
-
When it's ready.
-C'mon, it's open source — I'm doing it on my own time and you're -getting it for free. You can either contribute a patch or sponsor -the development of certain features, if they are important to you. -
-
-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/img/contact.png b/third-party/LuaJIT-2.0.2/doc/img/contact.png deleted file mode 100644 index 9c73dc594e..0000000000 Binary files a/third-party/LuaJIT-2.0.2/doc/img/contact.png and /dev/null differ diff --git a/third-party/LuaJIT-2.0.2/doc/install.html b/third-party/LuaJIT-2.0.2/doc/install.html deleted file mode 100644 index faf19c431b..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/install.html +++ /dev/null @@ -1,613 +0,0 @@ - - - -Installation - - - - - - - - - -
-Lua -
- - -
-

-LuaJIT is only distributed as a source package. This page explains -how to build and install LuaJIT with different operating systems -and C compilers. -

-

-For the impatient (on POSIX systems): -

-
-make && sudo make install
-
-

-LuaJIT currently builds out-of-the box on most systems. -Here's the compatibility matrix for the supported combinations of -operating systems, CPUs and compilers: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CPU / OSLinux or
Android
*BSD, OtherOSX 10.4+ or
iOS 3.0+
Windows
XP/Vista/7
x86 (32 bit)GCC 4.x
GCC 3.4
GCC 4.x
GCC 3.4
GCC 4.x
GCC 3.4
MSVC, MSVC/EE
WinSDK
MinGW, Cygwin
x64 (64 bit)GCC 4.x GCC 4.xMSVC + SDK v7.0
WinSDK v7.0
ARMv5+
ARM9E+
GCC 4.2+GCC 4.2+GCC 4.2+ 
PPCGCC 4.3+GCC 4.3+
GCC 4.1 (PS3)
 XEDK (Xbox 360)
PPC/e500v2GCC 4.3+GCC 4.3+  
MIPSGCC 4.3+GCC 4.3+  
- -

Configuring LuaJIT

-

-The standard configuration should work fine for most installations. -Usually there is no need to tweak the settings. The following files -hold all user-configurable settings: -

-
    -
  • src/luaconf.h sets some configuration variables.
  • -
  • Makefile has settings for installing LuaJIT (POSIX -only).
  • -
  • src/Makefile has settings for compiling LuaJIT -under POSIX, MinGW or Cygwin.
  • -
  • src/msvcbuild.bat has settings for compiling LuaJIT with -MSVC or WinSDK.
  • -
-

-Please read the instructions given in these files, before changing -any settings. -

- -

POSIX Systems (Linux, OSX, *BSD etc.)

-

Prerequisites

-

-Depending on your distribution, you may need to install a package for -GCC, the development headers and/or a complete SDK. E.g. on a current -Debian/Ubuntu, install libc6-dev with the package manager. -

-

-Download the current source package of LuaJIT (pick the .tar.gz), -if you haven't already done so. Move it to a directory of your choice, -open a terminal window and change to this directory. Now unpack the archive -and change to the newly created directory: -

-
-tar zxf LuaJIT-2.0.2.tar.gz
-cd LuaJIT-2.0.2
-

Building LuaJIT

-

-The supplied Makefiles try to auto-detect the settings needed for your -operating system and your compiler. They need to be run with GNU Make, -which is probably the default on your system, anyway. Simply run: -

-
-make
-
-

-This always builds a native x86, x64 or PPC binary, depending on the host OS -you're running this command on. Check the section on -cross-compilation for more options. -

-

-By default, modules are only searched under the prefix /usr/local. -You can add an extra prefix to the search paths by appending the -PREFIX option, e.g.: -

-
-make PREFIX=/home/myself/lj2
-
-

-Note for OSX: if the MACOSX_DEPLOYMENT_TARGET environment -variable is not set, then it's forced to 10.4. -

-

Installing LuaJIT

-

-The top-level Makefile installs LuaJIT by default under -/usr/local, i.e. the executable ends up in -/usr/local/bin and so on. You need root privileges -to write to this path. So, assuming sudo is installed on your system, -run the following command and enter your sudo password: -

-
-sudo make install
-
-

-Otherwise specify the directory prefix as an absolute path, e.g.: -

-
-make install PREFIX=/home/myself/lj2
-
-

-Obviously the prefixes given during build and installation need to be the same. -

- -

Windows Systems

-

Prerequisites

-

-Either install one of the open source SDKs -(» MinGW or -» Cygwin), which come with a modified -GCC plus the required development headers. -

-

-Or install Microsoft's Visual C++ (MSVC). The freely downloadable -» Express Edition -works just fine, but only contains an x86 compiler. -

-

-The freely downloadable -» Windows SDK -only comes with command line tools, but this is all you need to build LuaJIT. -It contains x86 and x64 compilers. -

-

-Next, download the source package and unpack it using an archive manager -(e.g. the Windows Explorer) to a directory of your choice. -

-

Building with MSVC

-

-Open a "Visual Studio .NET Command Prompt", cd to the -directory where you've unpacked the sources and run these commands: -

-
-cd src
-msvcbuild
-
-

-Then follow the installation instructions below. -

-

Building with the Windows SDK

-

-Open a "Windows SDK Command Shell" and select the x86 compiler: -

-
-setenv /release /x86
-
-

-Or select the x64 compiler: -

-
-setenv /release /x64
-
-

-Then cd to the directory where you've unpacked the sources -and run these commands: -

-
-cd src
-msvcbuild
-
-

-Then follow the installation instructions below. -

-

Building with MinGW or Cygwin

-

-Open a command prompt window and make sure the MinGW or Cygwin programs -are in your path. Then cd to the directory where -you've unpacked the sources and run this command for MinGW: -

-
-mingw32-make
-
-

-Or this command for Cygwin: -

-
-make
-
-

-Then follow the installation instructions below. -

-

Installing LuaJIT

-

-Copy luajit.exe and lua51.dll (built in the src -directory) to a newly created directory (any location is ok). -Add lua and lua\jit directories below it and copy -all Lua files from the src\jit directory of the distribution -to the latter directory. -

-

-There are no hardcoded -absolute path names — all modules are loaded relative to the -directory where luajit.exe is installed -(see src/luaconf.h). -

- -

Cross-compiling LuaJIT

-

-The GNU Makefile-based build system allows cross-compiling on any host -for any supported target, as long as both architectures have the same -pointer size. If you want to cross-compile to any 32 bit target on an -x64 OS, you need to install the multilib development package (e.g. -libc6-dev-i386 on Debian/Ubuntu) and build a 32 bit host part -(HOST_CC="gcc -m32"). -

-

-You need to specify TARGET_SYS whenever the host OS and the -target OS differ, or you'll get assembler or linker errors. E.g. if -you're compiling on a Windows or OSX host for embedded Linux or Android, -you need to add TARGET_SYS=Linux to the examples below. For a -minimal target OS, you may need to disable the built-in allocator in -src/Makefile and use TARGET_SYS=Other. The examples -below only show some popular targets — please check the comments -in src/Makefile for more details. -

-
-# Cross-compile to a 32 bit binary on a multilib x64 OS
-make CC="gcc -m32"
-
-# Cross-compile on Debian/Ubuntu for Windows (mingw32 package)
-make HOST_CC="gcc -m32" CROSS=i586-mingw32msvc- TARGET_SYS=Windows
-
-

-The CROSS prefix allows specifying a standard GNU cross-compile -toolchain (Binutils, GCC and a matching libc). The prefix may vary -depending on the --target the toolchain was built for (note the -CROSS prefix has a trailing "-"). The examples below -use the canonical toolchain triplets for Linux. -

-

-Since there's often no easy way to detect CPU features at runtime, it's -important to compile with the proper CPU or architecture settings. You -can specify these when building the toolchain yourself. Or add --mcpu=... or -march=... to TARGET_CFLAGS. For -ARM it's important to have the correct -mfloat-abi=... setting, -too. Otherwise LuaJIT may not run at the full performance of your target -CPU. -

-
-# ARM soft-float
-make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
-     TARGET_CFLAGS="-mfloat-abi=soft"
-
-# ARM soft-float ABI with VFP (example for Cortex-A8)
-make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
-     TARGET_CFLAGS="-mcpu=cortex-a8 -mfloat-abi=softfp"
-
-# ARM hard-float ABI with VFP (armhf, requires recent toolchain)
-make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabihf-
-
-# PPC
-make HOST_CC="gcc -m32" CROSS=powerpc-linux-gnu-
-# PPC/e500v2 (fast interpreter only)
-make HOST_CC="gcc -m32" CROSS=powerpc-e500v2-linux-gnuspe-
-
-# MIPS big-endian
-make HOST_CC="gcc -m32" CROSS=mips-linux-
-# MIPS little-endian
-make HOST_CC="gcc -m32" CROSS=mipsel-linux-
-
-

-You can cross-compile for Android using the » Android NDK. -The environment variables need to match the install locations and the -desired target platform. E.g. Android 4.0 corresponds to ABI level 14. -For details check the folder docs in the NDK directory. -

-

-Only a few common variations for the different CPUs, ABIs and platforms -are listed. Please use your own judgement for which combination you want -to build/deploy or which lowest common denominator you want to pick: -

-
-# Android/ARM, armeabi (ARMv5TE soft-float), Android 2.2+ (Froyo)
-NDK=/opt/android/ndk
-NDKABI=8
-NDKVER=$NDK/toolchains/arm-linux-androideabi-4.6
-NDKP=$NDKVER/prebuilt/linux-x86/bin/arm-linux-androideabi-
-NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
-make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF"
-
-# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS)
-NDK=/opt/android/ndk
-NDKABI=14
-NDKVER=$NDK/toolchains/arm-linux-androideabi-4.6
-NDKP=$NDKVER/prebuilt/linux-x86/bin/arm-linux-androideabi-
-NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
-NDKARCH="-march=armv7-a -mfloat-abi=softfp -Wl,--fix-cortex-a8"
-make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF $NDKARCH"
-
-# Android/MIPS, mips (MIPS32R1 hard-float), Android 4.0+ (ICS)
-NDK=/opt/android/ndk
-NDKABI=14
-NDKVER=$NDK/toolchains/mipsel-linux-android-4.6
-NDKP=$NDKVER/prebuilt/linux-x86/bin/mipsel-linux-android-
-NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-mips"
-make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF"
-
-# Android/x86, x86 (i686 SSE3), Android 4.0+ (ICS)
-NDK=/opt/android/ndk
-NDKABI=14
-NDKVER=$NDK/toolchains/x86-4.6
-NDKP=$NDKVER/prebuilt/linux-x86/bin/i686-linux-android-
-NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-x86"
-make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF"
-
-

-You can cross-compile for iOS 3.0+ (iPhone/iPad) using the » iOS SDK. -The environment variables need to match the iOS SDK version: -

-

-Note: the JIT compiler is disabled for iOS, because regular iOS Apps -are not allowed to generate code at runtime. You'll only get the performance -of the LuaJIT interpreter on iOS. This is still faster than plain Lua, but -much slower than the JIT compiler. Please complain to Apple, not me. -Or use Android. :-p -

-
-IXCODE=`xcode-select -print-path`
-ISDK=$IXCODE/Platforms/iPhoneOS.platform/Developer
-ISDKVER=iPhoneOS6.0.sdk
-ISDKP=$ISDK/usr/bin/
-ISDKF="-arch armv7 -isysroot $ISDK/SDKs/$ISDKVER"
-make HOST_CC="gcc -m32 -arch i386" CROSS=$ISDKP TARGET_FLAGS="$ISDKF" \
-     TARGET_SYS=iOS
-
-

-You can cross-compile for PS3 using the PS3 SDK from -a Linux host or a Windows host (requires 32 bit MinGW (GCC) on the host, -too). Due to restrictions on consoles, the JIT compiler is disabled and -only the fast interpreter is built: -

-
-make HOST_CC="gcc -m32" CROSS=ppu-lv2-
-
-

-You can cross-compile for Xbox 360 using the -Xbox 360 SDK (MSVC + XEDK). Due to restrictions on consoles, the -JIT compiler is disabled and only the fast interpreter is built. -

-

-Open a "Visual Studio .NET Command Prompt" (32 bit host compiler), -cd to the directory where you've unpacked the sources and run -the following commands. This builds a static library luajit20.lib, -which can be linked against your game, just like the Lua library. -

-
-cd src
-xedkbuild
-
- -

Embedding LuaJIT

-

-LuaJIT is API-compatible with Lua 5.1. If you've already embedded Lua -into your application, you probably don't need to do anything to switch -to LuaJIT, except link with a different library: -

-
    -
  • It's strongly suggested to build LuaJIT separately using the supplied -build system. Please do not attempt to integrate the individual -source files into your build tree. You'll most likely get the internal build -dependencies wrong or mess up the compiler flags. Treat LuaJIT like any -other external library and link your application with either the dynamic -or static library, depending on your needs.
  • -
  • If you want to load C modules compiled for plain Lua -with require(), you need to make sure the public symbols -(e.g. lua_pushnumber) are exported, too: -
    • On POSIX systems you can either link to the shared library -or link the static library into your application. In the latter case -you'll need to export all public symbols from your main executable -(e.g. -Wl,-E on Linux) and add the external dependencies -(e.g. -lm -ldl on Linux).
    • -
    • Since Windows symbols are bound to a specific DLL name, you need to -link to the lua51.dll created by the LuaJIT build (do not rename -the DLL). You may link LuaJIT statically on Windows only if you don't -intend to load Lua/C modules at runtime. -
    -
  • -
  • -If you're building a 64 bit application on OSX which links directly or -indirectly against LuaJIT, you need to link your main executable -with these flags: -
    --pagezero_size 10000 -image_base 100000000
    -
    -Also, it's recommended to rebase all (self-compiled) shared libraries -which are loaded at runtime on OSX/x64 (e.g. C extension modules for Lua). -See: man rebase -
  • -
-

Additional hints for initializing LuaJIT using the C API functions:

-
    -
  • Here's a -» simple example -for embedding Lua or LuaJIT into your application.
  • -
  • Make sure you use luaL_newstate. Avoid using -lua_newstate, since this uses the (slower) default memory -allocator from your system (no support for this on x64).
  • -
  • Make sure you use luaL_openlibs and not the old Lua 5.0 style -of calling luaopen_base etc. directly.
  • -
  • To change or extend the list of standard libraries to load, copy -src/lib_init.c to your project and modify it accordingly. -Make sure the jit library is loaded or the JIT compiler -will not be activated.
  • -
  • The bit.* module for bitwise operations -is already built-in. There's no need to statically link -» Lua BitOp to your application.
  • -
- -

Hints for Distribution Maintainers

-

-The LuaJIT build system has extra provisions for the needs of most -POSIX-based distributions. If you're a package maintainer for -a distribution, please make use of these features and -avoid patching, subverting, autotoolizing or messing up the build system -in unspeakable ways. -

-

-There should be absolutely no need to patch luaconf.h or any -of the Makefiles. And please do not hand-pick files for your packages — -simply use whatever make install creates. There's a reason -for all of the files and directories it creates. -

-

-The build system uses GNU make and auto-detects most settings based on -the host you're building it on. This should work fine for native builds, -even when sandboxed. You may need to pass some of the following flags to -both the make and the make install command lines -for a regular distribution build: -

-
    -
  • PREFIX overrides the installation path and should usually -be set to /usr. Setting this also changes the module paths and -the -rpath of the shared library.
  • -
  • DESTDIR is an absolute path which allows you to install -to a shadow tree instead of the root tree of the build system.
  • -
  • Have a look at the top-level Makefile and src/Makefile -for additional variables to tweak. The following variables may be -overridden, but it's not recommended, except for special needs -like cross-builds: -BUILDMODE, CC, HOST_CC, STATIC_CC, DYNAMIC_CC, CFLAGS, HOST_CFLAGS, -TARGET_CFLAGS, LDFLAGS, HOST_LDFLAGS, TARGET_LDFLAGS, TARGET_SHLDFLAGS, -TARGET_FLAGS, LIBS, HOST_LIBS, TARGET_LIBS, CROSS, HOST_SYS, TARGET_SYS -
  • -
-

-The build system has a special target for an amalgamated build, i.e. -make amalg. This compiles the LuaJIT core as one huge C file -and allows GCC to generate faster and shorter code. Alas, this requires -lots of memory during the build. This may be a problem for some users, -that's why it's not enabled by default. But it shouldn't be a problem for -most build farms. It's recommended that binary distributions use this -target for their LuaJIT builds. -

-

-The tl;dr version of the above: -

-
-make amalg PREFIX=/usr && \
-make install PREFIX=/usr DESTDIR=/tmp/buildroot
-
-

-Finally, if you encounter any difficulties, please -contact me first, instead of releasing a broken -package onto unsuspecting users. Because they'll usually gonna complain -to me (the upstream) and not you (the package maintainer), anyway. -

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/luajit.html b/third-party/LuaJIT-2.0.2/doc/luajit.html deleted file mode 100644 index e8581d3a03..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/luajit.html +++ /dev/null @@ -1,228 +0,0 @@ - - - -LuaJIT - - - - - - - - - - -
-Lua -
- - -
-

-LuaJIT is a Just-In-Time Compiler (JIT) for the -» Lua programming language. -Lua is a powerful, dynamic and light-weight programming language. -It may be embedded or used as a general-purpose, stand-alone language. -

-

-LuaJIT is Copyright © 2005-2013 Mike Pall, released under the -» MIT open source license. -

-

-

- -

Compatibility

- - -
WindowsLinuxBSDOSXPOSIX
- - -
EmbeddedAndroidiOSPS3Xbox 360
- - -
GCCCLANG
LLVM
MSVC
- - -
x86x64ARMPPCe500MIPS
- - -
Lua 5.1
API+ABI
+ JIT+ BitOp+ FFIDrop-in
DLL/.so
- -

Overview

- - - - - - - - - -
3x
-  100x
115 KB
VM
90 KB
JIT
63 KLOC
C
24 KLOC
ASM
11 KLOC
Lua
-

-LuaJIT has been successfully used as a scripting middleware in -games, appliances, network and graphics apps, numerical simulations, -trading platforms and many other specialty applications. It scales from -embedded devices, smartphones, desktops up to server farms. It combines -high flexibility with » high performance -and an unmatched low memory footprint. -

-

-LuaJIT has been in continuous development since 2005. It's widely -considered to be one of the fastest dynamic language -implementations. It has outperformed other dynamic languages on many -cross-language benchmarks since its first release — often by a -substantial margin. -

-

-For LuaJIT 2.0, the whole VM has been rewritten from the ground up -and relentlessly optimized for performance. It combines a high-speed -interpreter, written in assembler, with a state-of-the-art JIT -compiler. -

-

-An innovative trace compiler is integrated with advanced, -SSA-based optimizations and highly tuned code generation backends. -A substantial reduction of the overhead associated with dynamic languages -allows it to break into the performance range traditionally reserved for -offline, static language compilers. -

- -

More ...

-

-Please select a sub-topic in the navigation bar to learn more about LuaJIT. -

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/running.html b/third-party/LuaJIT-2.0.2/doc/running.html deleted file mode 100644 index 3149b3818c..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/running.html +++ /dev/null @@ -1,306 +0,0 @@ - - - -Running LuaJIT - - - - - - - - - -
-Lua -
- - -
-

-LuaJIT has only a single stand-alone executable, called luajit on -POSIX systems or luajit.exe on Windows. It can be used to run simple -Lua statements or whole Lua applications from the command line. It has an -interactive mode, too. -

- -

Command Line Options

-

-The luajit stand-alone executable is just a slightly modified -version of the regular lua stand-alone executable. -It supports the same basic options, too. luajit -h -prints a short list of the available options. Please have a look at the -» Lua manual -for details. -

-

-LuaJIT has some additional options: -

- -

-b[options] input output

-

-This option saves or lists bytecode. The following additional options -are accepted: -

-
    -
  • -l — Only list bytecode.
  • -
  • -s — Strip debug info (this is the default).
  • -
  • -g — Keep debug info.
  • -
  • -n name — Set module name (default: auto-detect from input name)
  • -
  • -t type — Set output file type (default: auto-detect from output name).
  • -
  • -a arch — Override architecture for object files (default: native).
  • -
  • -o os — Override OS for object files (default: native).
  • -
  • -e chunk — Use chunk string as input.
  • -
  • - (a single minus sign) — Use stdin as input and/or stdout as output.
  • -
-

-The output file type is auto-detected from the extension of the output -file name: -

-
    -
  • c — C source file, exported bytecode data.
  • -
  • h — C header file, static bytecode data.
  • -
  • obj or o — Object file, exported bytecode data -(OS- and architecture-specific).
  • -
  • raw or any other extension — Raw bytecode file (portable). -
-

-Notes: -

-
    -
  • See also string.dump() -for information on bytecode portability and compatibility.
  • -
  • A file in raw bytecode format is auto-detected and can be loaded like -any Lua source file. E.g. directly from the command line or with -loadfile(), dofile() etc.
  • -
  • To statically embed the bytecode of a module in your application, -generate an object file and just link it with your application.
  • -
  • On most ELF-based systems (e.g. Linux) you need to explicitly export the -global symbols when linking your application, e.g. with: -Wl,-E
  • -
  • require() tries to load embedded bytecode data from exported -symbols (in *.exe or lua51.dll on Windows) and from -shared libraries in package.cpath.
  • -
-

-Typical usage examples: -

-
-luajit -b test.lua test.out                 # Save bytecode to test.out
-luajit -bg test.lua test.out                # Keep debug info
-luajit -be "print('hello world')" test.out  # Save cmdline script
-
-luajit -bl test.lua                         # List to stdout
-luajit -bl test.lua test.txt                # List to test.txt
-luajit -ble "print('hello world')"          # List cmdline script
-
-luajit -b test.lua test.obj                 # Generate object file
-# Link test.obj with your application and load it with require("test")
-
- -

-j cmd[=arg[,arg...]]

-

-This option performs a LuaJIT control command or activates one of the -loadable extension modules. The command is first looked up in the -jit.* library. If no matching function is found, a module -named jit.<cmd> is loaded and the start() -function of the module is called with the specified arguments (if -any). The space between -j and cmd is optional. -

-

-Here are the available LuaJIT control commands: -

-
    -
  • -jon — Turns the JIT compiler on (default).
  • -
  • -joff — Turns the JIT compiler off (only use the interpreter).
  • -
  • -jflush — Flushes the whole cache of compiled code.
  • -
  • -jv — Shows verbose information about the progress of the JIT compiler.
  • -
  • -jdump — Dumps the code and structures used in various compiler stages.
  • -
-

-The -jv and -jdump commands are extension modules -written in Lua. They are mainly used for debugging the JIT compiler -itself. For a description of their options and output format, please -read the comment block at the start of their source. -They can be found in the lib directory of the source -distribution or installed under the jit directory. By default -this is /usr/local/share/luajit-2.0.2/jit on POSIX -systems. -

- -

-O[level]
--O[+]flag   -O-flag
--Oparam=value

-

-This options allows fine-tuned control of the optimizations used by -the JIT compiler. This is mainly intended for debugging LuaJIT itself. -Please note that the JIT compiler is extremely fast (we are talking -about the microsecond to millisecond range). Disabling optimizations -doesn't have any visible impact on its overhead, but usually generates -code that runs slower. -

-

-The first form sets an optimization level — this enables a -specific mix of optimization flags. -O0 turns off all -optimizations and higher numbers enable more optimizations. Omitting -the level (i.e. just -O) sets the default optimization level, -which is -O3 in the current version. -

-

-The second form adds or removes individual optimization flags. -The third form sets a parameter for the VM or the JIT compiler -to a specific value. -

-

-You can either use this option multiple times (like -Ocse --O-dce -Ohotloop=10) or separate several settings with a comma -(like -O+cse,-dce,hotloop=10). The settings are applied from -left to right and later settings override earlier ones. You can freely -mix the three forms, but note that setting an optimization level -overrides all earlier flags. -

-

-Here are the available flags and at what optimization levels they -are enabled: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Flag-O1-O2-O3 
foldConstant Folding, Simplifications and Reassociation
cseCommon-Subexpression Elimination
dceDead-Code Elimination
narrow Narrowing of numbers to integers
loop Loop Optimizations (code hoisting)
fwd  Load Forwarding (L2L) and Store Forwarding (S2L)
dse  Dead-Store Elimination
abc  Array Bounds Check Elimination
sink  Allocation/Store Sinking
fuse  Fusion of operands into instructions
-

-Here are the parameters and their default settings: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ParameterDefault 
maxtrace1000Max. number of traces in the cache
maxrecord4000Max. number of recorded IR instructions
maxirconst500Max. number of IR constants of a trace
maxside100Max. number of side traces of a root trace
maxsnap500Max. number of snapshots for a trace
hotloop56Number of iterations to detect a hot loop or hot call
hotexit10Number of taken exits to start a side trace
tryside4Number of attempts to compile a side trace
instunroll4Max. unroll factor for instable loops
loopunroll15Max. unroll factor for loop ops in side traces
callunroll3Max. unroll factor for pseudo-recursive calls
recunroll2Min. unroll factor for true recursion
sizemcode32Size of each machine code area in KBytes (Windows: 64K)
maxmcode512Max. total size of all machine code areas in KBytes
-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/doc/status.html b/third-party/LuaJIT-2.0.2/doc/status.html deleted file mode 100644 index 3d148b0a86..0000000000 --- a/third-party/LuaJIT-2.0.2/doc/status.html +++ /dev/null @@ -1,125 +0,0 @@ - - - -Status & Roadmap - - - - - - - - - -
-Lua -
- - -
-

-LuaJIT 2.0 is the current -stable branch. This branch is in -feature-freeze — new features will only be added to LuaJIT 2.1. -

- -

Current Status

-

-LuaJIT ought to run all Lua 5.1-compatible source code just fine. -It's considered a serious bug if the VM crashes or produces unexpected -results — please report this. -

-

-Known incompatibilities and issues in LuaJIT 2.0: -

-
    -
  • -There are some differences in implementation-defined behavior. -These either have a good reason, are arbitrary design choices -or are due to quirks in the VM. The latter cases may get fixed if a -demonstrable need is shown. -
  • -
  • -The Lua debug API is missing a couple of features (return -hooks for non-Lua functions) and shows slightly different behavior -in LuaJIT (no per-coroutine hooks, no tail call counting). -
  • -
  • -Some checks are missing in the JIT-compiled code for obscure situations -with open upvalues aliasing one of the SSA slots later on (or -vice versa). Bonus points, if you can find a real world test case for -this. -
  • -
  • -Currently some out-of-memory errors from on-trace code are not -handled correctly. The error may fall through an on-trace -pcall or it may be passed on to the function set with -lua_atpanic on x64. This issue will be fixed with the new -garbage collector. -
  • -
- -

Roadmap

-

-Please refer to the -» LuaJIT Roadmap 2012/2013 and an -» update on release planning for details. -

-

-

-
-
- - - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_arm.h b/third-party/LuaJIT-2.0.2/dynasm/dasm_arm.h deleted file mode 100644 index d83cbbb339..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_arm.h +++ /dev/null @@ -1,456 +0,0 @@ -/* -** DynASM ARM encoding engine. -** Copyright (C) 2005-2013 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "arm" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, - DASM_IMM, DASM_IMM12, DASM_IMM16, DASM_IMML8, DASM_IMML12, DASM_IMMV8, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -static int dasm_imm12(unsigned int n) -{ - int i; - for (i = 0; i < 16; i++, n = (n << 2) | (n >> 30)) - if (n <= 255) return (int)(n + (i << 8)); - return -1; -} - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: - case DASM_IMM16: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); - if ((ins & 0x8000)) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - case DASM_IMMV8: - CK((n & 3) == 0, RANGE_I); - n >>= 2; - case DASM_IMML8: - case DASM_IMML12: - CK(n >= 0 ? ((n>>((ins>>5)&31)) == 0) : - (((-n)>>((ins>>5)&31)) == 0), RANGE_I); - b[pos++] = n; - break; - case DASM_IMM12: - CK(dasm_imm12((unsigned int)n) != -1, RANGE_I); - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: case DASM_IMM12: case DASM_IMM16: - case DASM_IMML8: case DASM_IMML12: case DASM_IMMV8: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048)); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) - 4; - patchrel: - if ((ins & 0x800) == 0) { - CK((n & 3) == 0 && ((n+0x02000000) >> 26) == 0, RANGE_REL); - cp[-1] |= ((n >> 2) & 0x00ffffff); - } else if ((ins & 0x1000)) { - CK((n & 3) == 0 && -256 <= n && n <= 256, RANGE_REL); - goto patchimml8; - } else if ((ins & 0x2000) == 0) { - CK((n & 3) == 0 && -4096 <= n && n <= 4096, RANGE_REL); - goto patchimml; - } else { - CK((n & 3) == 0 && -1020 <= n && n <= 1020, RANGE_REL); - n >>= 2; - goto patchimml; - } - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - case DASM_IMM12: - cp[-1] |= dasm_imm12((unsigned int)n); - break; - case DASM_IMM16: - cp[-1] |= ((n & 0xf000) << 4) | (n & 0x0fff); - break; - case DASM_IMML8: patchimml8: - cp[-1] |= n >= 0 ? (0x00800000 | (n & 0x0f) | ((n & 0xf0) << 4)) : - ((-n & 0x0f) | ((-n & 0xf0) << 4)); - break; - case DASM_IMML12: case DASM_IMMV8: patchimml: - cp[-1] |= n >= 0 ? (0x00800000 | n) : (-n); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_arm.lua b/third-party/LuaJIT-2.0.2/dynasm/dasm_arm.lua deleted file mode 100644 index 9720e57cd3..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_arm.lua +++ /dev/null @@ -1,1122 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM ARM module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "arm", - description = "DynASM ARM module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable, rawget = assert, setmetatable, rawget -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub -local concat, sort, insert = table.concat, table.sort, table.insert -local bit = bit or require("bit") -local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift -local ror, tohex = bit.ror, bit.tohex - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM16", "IMML8", "IMML12", "IMMV8", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0x000fffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - if n <= 0x000fffff then - insert(actlist, pos+1, n) - n = map_action.ESC * 0x10000 - end - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. - --- Ext. register name -> int. name. -local map_archdef = { sp = "r13", lr = "r14", pc = "r15", } - --- Int. register name -> ext. name. -local map_reg_rev = { r13 = "sp", r14 = "lr", r15 = "pc", } - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - return map_reg_rev[s] or s -end - -local map_shift = { lsl = 0, lsr = 1, asr = 2, ror = 3, } - -local map_cond = { - eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7, - hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14, - hs = 2, lo = 3, -} - ------------------------------------------------------------------------------- - --- Template strings for ARM instructions. -local map_op = { - -- Basic data processing instructions. - and_3 = "e0000000DNPs", - eor_3 = "e0200000DNPs", - sub_3 = "e0400000DNPs", - rsb_3 = "e0600000DNPs", - add_3 = "e0800000DNPs", - adc_3 = "e0a00000DNPs", - sbc_3 = "e0c00000DNPs", - rsc_3 = "e0e00000DNPs", - tst_2 = "e1100000NP", - teq_2 = "e1300000NP", - cmp_2 = "e1500000NP", - cmn_2 = "e1700000NP", - orr_3 = "e1800000DNPs", - mov_2 = "e1a00000DPs", - bic_3 = "e1c00000DNPs", - mvn_2 = "e1e00000DPs", - - and_4 = "e0000000DNMps", - eor_4 = "e0200000DNMps", - sub_4 = "e0400000DNMps", - rsb_4 = "e0600000DNMps", - add_4 = "e0800000DNMps", - adc_4 = "e0a00000DNMps", - sbc_4 = "e0c00000DNMps", - rsc_4 = "e0e00000DNMps", - tst_3 = "e1100000NMp", - teq_3 = "e1300000NMp", - cmp_3 = "e1500000NMp", - cmn_3 = "e1700000NMp", - orr_4 = "e1800000DNMps", - mov_3 = "e1a00000DMps", - bic_4 = "e1c00000DNMps", - mvn_3 = "e1e00000DMps", - - lsl_3 = "e1a00000DMws", - lsr_3 = "e1a00020DMws", - asr_3 = "e1a00040DMws", - ror_3 = "e1a00060DMws", - rrx_2 = "e1a00060DMs", - - -- Multiply and multiply-accumulate. - mul_3 = "e0000090NMSs", - mla_4 = "e0200090NMSDs", - umaal_4 = "e0400090DNMSs", -- v6 - mls_4 = "e0600090DNMSs", -- v6T2 - umull_4 = "e0800090DNMSs", - umlal_4 = "e0a00090DNMSs", - smull_4 = "e0c00090DNMSs", - smlal_4 = "e0e00090DNMSs", - - -- Halfword multiply and multiply-accumulate. - smlabb_4 = "e1000080NMSD", -- v5TE - smlatb_4 = "e10000a0NMSD", -- v5TE - smlabt_4 = "e10000c0NMSD", -- v5TE - smlatt_4 = "e10000e0NMSD", -- v5TE - smlawb_4 = "e1200080NMSD", -- v5TE - smulwb_3 = "e12000a0NMS", -- v5TE - smlawt_4 = "e12000c0NMSD", -- v5TE - smulwt_3 = "e12000e0NMS", -- v5TE - smlalbb_4 = "e1400080NMSD", -- v5TE - smlaltb_4 = "e14000a0NMSD", -- v5TE - smlalbt_4 = "e14000c0NMSD", -- v5TE - smlaltt_4 = "e14000e0NMSD", -- v5TE - smulbb_3 = "e1600080NMS", -- v5TE - smultb_3 = "e16000a0NMS", -- v5TE - smulbt_3 = "e16000c0NMS", -- v5TE - smultt_3 = "e16000e0NMS", -- v5TE - - -- Miscellaneous data processing instructions. - clz_2 = "e16f0f10DM", -- v5T - rev_2 = "e6bf0f30DM", -- v6 - rev16_2 = "e6bf0fb0DM", -- v6 - revsh_2 = "e6ff0fb0DM", -- v6 - sel_3 = "e6800fb0DNM", -- v6 - usad8_3 = "e780f010NMS", -- v6 - usada8_4 = "e7800010NMSD", -- v6 - rbit_2 = "e6ff0f30DM", -- v6T2 - movw_2 = "e3000000DW", -- v6T2 - movt_2 = "e3400000DW", -- v6T2 - -- Note: the X encodes width-1, not width. - sbfx_4 = "e7a00050DMvX", -- v6T2 - ubfx_4 = "e7e00050DMvX", -- v6T2 - -- Note: the X encodes the msb field, not the width. - bfc_3 = "e7c0001fDvX", -- v6T2 - bfi_4 = "e7c00010DMvX", -- v6T2 - - -- Packing and unpacking instructions. - pkhbt_3 = "e6800010DNM", pkhbt_4 = "e6800010DNMv", -- v6 - pkhtb_3 = "e6800050DNM", pkhtb_4 = "e6800050DNMv", -- v6 - sxtab_3 = "e6a00070DNM", sxtab_4 = "e6a00070DNMv", -- v6 - sxtab16_3 = "e6800070DNM", sxtab16_4 = "e6800070DNMv", -- v6 - sxtah_3 = "e6b00070DNM", sxtah_4 = "e6b00070DNMv", -- v6 - sxtb_2 = "e6af0070DM", sxtb_3 = "e6af0070DMv", -- v6 - sxtb16_2 = "e68f0070DM", sxtb16_3 = "e68f0070DMv", -- v6 - sxth_2 = "e6bf0070DM", sxth_3 = "e6bf0070DMv", -- v6 - uxtab_3 = "e6e00070DNM", uxtab_4 = "e6e00070DNMv", -- v6 - uxtab16_3 = "e6c00070DNM", uxtab16_4 = "e6c00070DNMv", -- v6 - uxtah_3 = "e6f00070DNM", uxtah_4 = "e6f00070DNMv", -- v6 - uxtb_2 = "e6ef0070DM", uxtb_3 = "e6ef0070DMv", -- v6 - uxtb16_2 = "e6cf0070DM", uxtb16_3 = "e6cf0070DMv", -- v6 - uxth_2 = "e6ff0070DM", uxth_3 = "e6ff0070DMv", -- v6 - - -- Saturating instructions. - qadd_3 = "e1000050DMN", -- v5TE - qsub_3 = "e1200050DMN", -- v5TE - qdadd_3 = "e1400050DMN", -- v5TE - qdsub_3 = "e1600050DMN", -- v5TE - -- Note: the X for ssat* encodes sat_imm-1, not sat_imm. - ssat_3 = "e6a00010DXM", ssat_4 = "e6a00010DXMp", -- v6 - usat_3 = "e6e00010DXM", usat_4 = "e6e00010DXMp", -- v6 - ssat16_3 = "e6a00f30DXM", -- v6 - usat16_3 = "e6e00f30DXM", -- v6 - - -- Parallel addition and subtraction. - sadd16_3 = "e6100f10DNM", -- v6 - sasx_3 = "e6100f30DNM", -- v6 - ssax_3 = "e6100f50DNM", -- v6 - ssub16_3 = "e6100f70DNM", -- v6 - sadd8_3 = "e6100f90DNM", -- v6 - ssub8_3 = "e6100ff0DNM", -- v6 - qadd16_3 = "e6200f10DNM", -- v6 - qasx_3 = "e6200f30DNM", -- v6 - qsax_3 = "e6200f50DNM", -- v6 - qsub16_3 = "e6200f70DNM", -- v6 - qadd8_3 = "e6200f90DNM", -- v6 - qsub8_3 = "e6200ff0DNM", -- v6 - shadd16_3 = "e6300f10DNM", -- v6 - shasx_3 = "e6300f30DNM", -- v6 - shsax_3 = "e6300f50DNM", -- v6 - shsub16_3 = "e6300f70DNM", -- v6 - shadd8_3 = "e6300f90DNM", -- v6 - shsub8_3 = "e6300ff0DNM", -- v6 - uadd16_3 = "e6500f10DNM", -- v6 - uasx_3 = "e6500f30DNM", -- v6 - usax_3 = "e6500f50DNM", -- v6 - usub16_3 = "e6500f70DNM", -- v6 - uadd8_3 = "e6500f90DNM", -- v6 - usub8_3 = "e6500ff0DNM", -- v6 - uqadd16_3 = "e6600f10DNM", -- v6 - uqasx_3 = "e6600f30DNM", -- v6 - uqsax_3 = "e6600f50DNM", -- v6 - uqsub16_3 = "e6600f70DNM", -- v6 - uqadd8_3 = "e6600f90DNM", -- v6 - uqsub8_3 = "e6600ff0DNM", -- v6 - uhadd16_3 = "e6700f10DNM", -- v6 - uhasx_3 = "e6700f30DNM", -- v6 - uhsax_3 = "e6700f50DNM", -- v6 - uhsub16_3 = "e6700f70DNM", -- v6 - uhadd8_3 = "e6700f90DNM", -- v6 - uhsub8_3 = "e6700ff0DNM", -- v6 - - -- Load/store instructions. - str_2 = "e4000000DL", str_3 = "e4000000DL", str_4 = "e4000000DL", - strb_2 = "e4400000DL", strb_3 = "e4400000DL", strb_4 = "e4400000DL", - ldr_2 = "e4100000DL", ldr_3 = "e4100000DL", ldr_4 = "e4100000DL", - ldrb_2 = "e4500000DL", ldrb_3 = "e4500000DL", ldrb_4 = "e4500000DL", - strh_2 = "e00000b0DL", strh_3 = "e00000b0DL", - ldrh_2 = "e01000b0DL", ldrh_3 = "e01000b0DL", - ldrd_2 = "e00000d0DL", ldrd_3 = "e00000d0DL", -- v5TE - ldrsb_2 = "e01000d0DL", ldrsb_3 = "e01000d0DL", - strd_2 = "e00000f0DL", strd_3 = "e00000f0DL", -- v5TE - ldrsh_2 = "e01000f0DL", ldrsh_3 = "e01000f0DL", - - ldm_2 = "e8900000oR", ldmia_2 = "e8900000oR", ldmfd_2 = "e8900000oR", - ldmda_2 = "e8100000oR", ldmfa_2 = "e8100000oR", - ldmdb_2 = "e9100000oR", ldmea_2 = "e9100000oR", - ldmib_2 = "e9900000oR", ldmed_2 = "e9900000oR", - stm_2 = "e8800000oR", stmia_2 = "e8800000oR", stmfd_2 = "e8800000oR", - stmda_2 = "e8000000oR", stmfa_2 = "e8000000oR", - stmdb_2 = "e9000000oR", stmea_2 = "e9000000oR", - stmib_2 = "e9800000oR", stmed_2 = "e9800000oR", - pop_1 = "e8bd0000R", push_1 = "e92d0000R", - - -- Branch instructions. - b_1 = "ea000000B", - bl_1 = "eb000000B", - blx_1 = "e12fff30C", - bx_1 = "e12fff10M", - - -- Miscellaneous instructions. - nop_0 = "e1a00000", - mrs_1 = "e10f0000D", - bkpt_1 = "e1200070K", -- v5T - svc_1 = "ef000000T", swi_1 = "ef000000T", - ud_0 = "e7f001f0", - - -- VFP instructions. - ["vadd.f32_3"] = "ee300a00dnm", - ["vadd.f64_3"] = "ee300b00Gdnm", - ["vsub.f32_3"] = "ee300a40dnm", - ["vsub.f64_3"] = "ee300b40Gdnm", - ["vmul.f32_3"] = "ee200a00dnm", - ["vmul.f64_3"] = "ee200b00Gdnm", - ["vnmul.f32_3"] = "ee200a40dnm", - ["vnmul.f64_3"] = "ee200b40Gdnm", - ["vmla.f32_3"] = "ee000a00dnm", - ["vmla.f64_3"] = "ee000b00Gdnm", - ["vmls.f32_3"] = "ee000a40dnm", - ["vmls.f64_3"] = "ee000b40Gdnm", - ["vnmla.f32_3"] = "ee100a40dnm", - ["vnmla.f64_3"] = "ee100b40Gdnm", - ["vnmls.f32_3"] = "ee100a00dnm", - ["vnmls.f64_3"] = "ee100b00Gdnm", - ["vdiv.f32_3"] = "ee800a00dnm", - ["vdiv.f64_3"] = "ee800b00Gdnm", - - ["vabs.f32_2"] = "eeb00ac0dm", - ["vabs.f64_2"] = "eeb00bc0Gdm", - ["vneg.f32_2"] = "eeb10a40dm", - ["vneg.f64_2"] = "eeb10b40Gdm", - ["vsqrt.f32_2"] = "eeb10ac0dm", - ["vsqrt.f64_2"] = "eeb10bc0Gdm", - ["vcmp.f32_2"] = "eeb40a40dm", - ["vcmp.f64_2"] = "eeb40b40Gdm", - ["vcmpe.f32_2"] = "eeb40ac0dm", - ["vcmpe.f64_2"] = "eeb40bc0Gdm", - ["vcmpz.f32_1"] = "eeb50a40d", - ["vcmpz.f64_1"] = "eeb50b40Gd", - ["vcmpze.f32_1"] = "eeb50ac0d", - ["vcmpze.f64_1"] = "eeb50bc0Gd", - - vldr_2 = "ed100a00dl|ed100b00Gdl", - vstr_2 = "ed000a00dl|ed000b00Gdl", - vldm_2 = "ec900a00or", - vldmia_2 = "ec900a00or", - vldmdb_2 = "ed100a00or", - vpop_1 = "ecbd0a00r", - vstm_2 = "ec800a00or", - vstmia_2 = "ec800a00or", - vstmdb_2 = "ed000a00or", - vpush_1 = "ed2d0a00r", - - ["vmov.f32_2"] = "eeb00a40dm|eeb00a00dY", -- #imm is VFPv3 only - ["vmov.f64_2"] = "eeb00b40Gdm|eeb00b00GdY", -- #imm is VFPv3 only - vmov_2 = "ee100a10Dn|ee000a10nD", - vmov_3 = "ec500a10DNm|ec400a10mDN|ec500b10GDNm|ec400b10GmDN", - - vmrs_0 = "eef1fa10", - vmrs_1 = "eef10a10D", - vmsr_1 = "eee10a10D", - - ["vcvt.s32.f32_2"] = "eebd0ac0dm", - ["vcvt.s32.f64_2"] = "eebd0bc0dGm", - ["vcvt.u32.f32_2"] = "eebc0ac0dm", - ["vcvt.u32.f64_2"] = "eebc0bc0dGm", - ["vcvtr.s32.f32_2"] = "eebd0a40dm", - ["vcvtr.s32.f64_2"] = "eebd0b40dGm", - ["vcvtr.u32.f32_2"] = "eebc0a40dm", - ["vcvtr.u32.f64_2"] = "eebc0b40dGm", - ["vcvt.f32.s32_2"] = "eeb80ac0dm", - ["vcvt.f64.s32_2"] = "eeb80bc0GdFm", - ["vcvt.f32.u32_2"] = "eeb80a40dm", - ["vcvt.f64.u32_2"] = "eeb80b40GdFm", - ["vcvt.f32.f64_2"] = "eeb70bc0dGm", - ["vcvt.f64.f32_2"] = "eeb70ac0GdFm", - - -- VFPv4 only: - ["vfma.f32_3"] = "eea00a00dnm", - ["vfma.f64_3"] = "eea00b00Gdnm", - ["vfms.f32_3"] = "eea00a40dnm", - ["vfms.f64_3"] = "eea00b40Gdnm", - ["vfnma.f32_3"] = "ee900a40dnm", - ["vfnma.f64_3"] = "ee900b40Gdnm", - ["vfnms.f32_3"] = "ee900a00dnm", - ["vfnms.f64_3"] = "ee900b00Gdnm", - - -- NYI: Advanced SIMD instructions. - - -- NYI: I have no need for these instructions right now: - -- swp, swpb, strex, ldrex, strexd, ldrexd, strexb, ldrexb, strexh, ldrexh - -- msr, nopv6, yield, wfe, wfi, sev, dbg, bxj, smc, srs, rfe - -- cps, setend, pli, pld, pldw, clrex, dsb, dmb, isb - -- stc, ldc, mcr, mcr2, mrc, mrc2, mcrr, mcrr2, mrrc, mrrc2, cdp, cdp2 -} - --- Add mnemonics for "s" variants. -do - local t = {} - for k,v in pairs(map_op) do - if sub(v, -1) == "s" then - local v2 = sub(v, 1, 2)..char(byte(v, 3)+1)..sub(v, 4, -2) - t[sub(k, 1, -3).."s"..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r1?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r(1?[0-9])$") - if r then - r = tonumber(r) - if r <= 15 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_gpr_pm(expr) - local pm, expr2 = match(expr, "^([+-]?)(.*)$") - return parse_gpr(expr2), (pm == "-") -end - -local function parse_vr(expr, tp) - local t, r = match(expr, "^([sd])([0-9]+)$") - if t == tp then - r = tonumber(r) - if r <= 31 then - if t == "s" then return shr(r, 1), band(r, 1) end - return band(r, 15), shr(r, 4) - end - end - werror("bad register name `"..expr.."'") -end - -local function parse_reglist(reglist) - reglist = match(reglist, "^{%s*([^}]*)}$") - if not reglist then werror("register list expected") end - local rr = 0 - for p in gmatch(reglist..",", "%s*([^,]*),") do - local rbit = shl(1, parse_gpr(gsub(p, "%s+$", ""))) - if band(rr, rbit) ~= 0 then - werror("duplicate register `"..p.."'") - end - rr = rr + rbit - end - return rr -end - -local function parse_vrlist(reglist) - local ta, ra, tb, rb = match(reglist, - "^{%s*([sd])([0-9]+)%s*%-%s*([sd])([0-9]+)%s*}$") - ra, rb = tonumber(ra), tonumber(rb) - if ta and ta == tb and ra and rb and ra <= 31 and rb <= 31 and ra <= rb then - local nr = rb+1 - ra - if ta == "s" then - return shl(shr(ra,1),12)+shl(band(ra,1),22) + nr - else - return shl(band(ra,15),12)+shl(shr(ra,4),22) + nr*2 + 0x100 - end - end - werror("register list expected") -end - -local function parse_imm(imm, bits, shift, scale, signed) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n then - if signed then - local s = sar(m, bits-1) - if s == 0 then return shl(m, shift) - elseif s == -1 then return shl(m + shl(1, bits), shift) end - else - if sar(m, bits) == 0 then return shl(m, shift) end - end - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_imm12(imm) - local n = tonumber(imm) - if n then - local m = band(n) - for i=0,-15,-1 do - if shr(m, 8) == 0 then return m + shl(band(i, 15), 8) end - m = ror(m, 2) - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM12", 0, imm) - return 0 - end -end - -local function parse_imm16(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - if shr(n, 16) == 0 then return band(n, 0x0fff) + shl(band(n, 0xf000), 4) end - werror("out of range immediate `"..imm.."'") - else - waction("IMM16", 32*16, imm) - return 0 - end -end - -local function parse_imm_load(imm, ext) - local n = tonumber(imm) - if n then - if ext then - if n >= -255 and n <= 255 then - local up = 0x00800000 - if n < 0 then n = -n; up = 0 end - return shl(band(n, 0xf0), 4) + band(n, 0x0f) + up - end - else - if n >= -4095 and n <= 4095 then - if n >= 0 then return n+0x00800000 end - return -n - end - end - werror("out of range immediate `"..imm.."'") - else - waction(ext and "IMML8" or "IMML12", 32768 + shl(ext and 8 or 12, 5), imm) - return 0 - end -end - -local function parse_shift(shift, gprok) - if shift == "rrx" then - return 3 * 32 - else - local s, s2 = match(shift, "^(%S+)%s*(.*)$") - s = map_shift[s] - if not s then werror("expected shift operand") end - if sub(s2, 1, 1) == "#" then - return parse_imm(s2, 5, 7, 0, false) + shl(s, 5) - else - if not gprok then werror("expected immediate shift operand") end - return shl(parse_gpr(s2), 8) + shl(s, 5) + 16 - end - end -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - -local function parse_load(params, nparams, n, op) - local oplo = band(op, 255) - local ext, ldrd = (oplo ~= 0), (oplo == 208) - local d - if (ldrd or oplo == 240) then - d = band(shr(op, 12), 15) - if band(d, 1) ~= 0 then werror("odd destination register") end - end - local pn = params[n] - local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") - local p2 = params[n+1] - if not p1 then - if not p2 then - if match(pn, "^[<>=%-]") or match(pn, "^extern%s+") then - local mode, n, s = parse_label(pn, false) - waction("REL_"..mode, n + (ext and 0x1800 or 0x0800), s, 1) - return op + 15 * 65536 + 0x01000000 + (ext and 0x00400000 or 0) - end - local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local d, tp = parse_gpr(reg) - if tp then - waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), - format(tp.ctypefmt, tailr)) - return op + shl(d, 16) + 0x01000000 + (ext and 0x00400000 or 0) - end - end - end - werror("expected address operand") - end - if wb == "!" then op = op + 0x00200000 end - if p2 then - if wb == "!" then werror("bad use of '!'") end - local p3 = params[n+2] - op = op + shl(parse_gpr(p1), 16) - local imm = match(p2, "^#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - if p3 then werror("too many parameters") end - op = op + m + (ext and 0x00400000 or 0) - else - local m, neg = parse_gpr_pm(p2) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 then op = op + parse_shift(p3) end - end - else - local p1a, p2 = match(p1, "^([^,%s]*)%s*(.*)$") - op = op + shl(parse_gpr(p1a), 16) + 0x01000000 - if p2 ~= "" then - local imm = match(p2, "^,%s*#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - op = op + m + (ext and 0x00400000 or 0) - else - local p2a, p3 = match(p2, "^,%s*([^,%s]*)%s*,?%s*(.*)$") - local m, neg = parse_gpr_pm(p2a) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 ~= "" then - if ext then werror("too many parameters") end - op = op + parse_shift(p3) - end - end - else - if wb == "!" then werror("bad use of '!'") end - op = op + (ext and 0x00c00000 or 0x00800000) - end - end - return op -end - -local function parse_vload(q) - local reg, imm = match(q, "^%[%s*([^,%s]*)%s*(.*)%]$") - if reg then - local d = shl(parse_gpr(reg), 16) - if imm == "" then return d end - imm = match(imm, "^,%s*#(.*)$") - if imm then - local n = tonumber(imm) - if n then - if n >= -1020 and n <= 1020 and n%4 == 0 then - return d + (n >= 0 and n/4+0x00800000 or -n/4) - end - werror("out of range immediate `"..imm.."'") - else - waction("IMMV8", 32768 + 32*8, imm) - return d - end - end - else - if match(q, "^[<>=%-]") or match(q, "^extern%s+") then - local mode, n, s = parse_label(q, false) - waction("REL_"..mode, n + 0x2800, s, 1) - return 15 * 65536 - end - local reg, tailr = match(q, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local d, tp = parse_gpr(reg) - if tp then - waction("IMMV8", 32768 + 32*8, format(tp.ctypefmt, tailr)) - return shl(d, 16) - end - end - end - werror("expected address operand") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -local function parse_template(params, template, nparams, pos) - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - local vr = "s" - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - local q = params[n] - if p == "D" then - op = op + shl(parse_gpr(q), 12); n = n + 1 - elseif p == "N" then - op = op + shl(parse_gpr(q), 16); n = n + 1 - elseif p == "S" then - op = op + shl(parse_gpr(q), 8); n = n + 1 - elseif p == "M" then - op = op + parse_gpr(q); n = n + 1 - elseif p == "d" then - local r,h = parse_vr(q, vr); op = op+shl(r,12)+shl(h,22); n = n + 1 - elseif p == "n" then - local r,h = parse_vr(q, vr); op = op+shl(r,16)+shl(h,7); n = n + 1 - elseif p == "m" then - local r,h = parse_vr(q, vr); op = op+r+shl(h,5); n = n + 1 - elseif p == "P" then - local imm = match(q, "^#(.*)$") - if imm then - op = op + parse_imm12(imm) + 0x02000000 - else - op = op + parse_gpr(q) - end - n = n + 1 - elseif p == "p" then - op = op + parse_shift(q, true); n = n + 1 - elseif p == "L" then - op = parse_load(params, nparams, n, op) - elseif p == "l" then - op = op + parse_vload(q) - elseif p == "B" then - local mode, n, s = parse_label(q, false) - waction("REL_"..mode, n, s, 1) - elseif p == "C" then -- blx gpr vs. blx label. - if match(q, "^([%w_]+):(r1?[0-9])$") or match(q, "^r(1?[0-9])$") then - op = op + parse_gpr(q) - else - if op < 0xe0000000 then werror("unconditional instruction") end - local mode, n, s = parse_label(q, false) - waction("REL_"..mode, n, s, 1) - op = 0xfa000000 - end - elseif p == "F" then - vr = "s" - elseif p == "G" then - vr = "d" - elseif p == "o" then - local r, wb = match(q, "^([^!]*)(!?)$") - op = op + shl(parse_gpr(r), 16) + (wb == "!" and 0x00200000 or 0) - n = n + 1 - elseif p == "R" then - op = op + parse_reglist(q); n = n + 1 - elseif p == "r" then - op = op + parse_vrlist(q); n = n + 1 - elseif p == "W" then - op = op + parse_imm16(q); n = n + 1 - elseif p == "v" then - op = op + parse_imm(q, 5, 7, 0, false); n = n + 1 - elseif p == "w" then - local imm = match(q, "^#(.*)$") - if imm then - op = op + parse_imm(q, 5, 7, 0, false); n = n + 1 - else - op = op + shl(parse_gpr(q), 8) + 16 - end - elseif p == "X" then - op = op + parse_imm(q, 5, 16, 0, false); n = n + 1 - elseif p == "Y" then - local imm = tonumber(match(q, "^#(.*)$")); n = n + 1 - if not imm or shr(imm, 8) ~= 0 then - werror("bad immediate operand") - end - op = op + shl(band(imm, 0xf0), 12) + band(imm, 0x0f) - elseif p == "K" then - local imm = tonumber(match(q, "^#(.*)$")); n = n + 1 - if not imm or shr(imm, 16) ~= 0 then - werror("bad immediate operand") - end - op = op + shl(band(imm, 0xfff0), 4) + band(imm, 0x000f) - elseif p == "T" then - op = op + parse_imm(q, 24, 0, 0, false); n = n + 1 - elseif p == "s" then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions. - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - local apos, spos = #actargs, secpos - - local ok, err - for t in gmatch(template, "[^|]+") do - ok, err = pcall(parse_template, params, t, nparams, pos) - if ok then return end - secpos = spos - actargs[apos+1] = nil - actargs[apos+2] = nil - actargs[apos+3] = nil - end - error(err, 0) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = function(t, k) - local v = map_coreop[k] - if v then return v end - local k1, cc, k2 = match(k, "^(.-)(..)([._].*)$") - local cv = map_cond[cc] - if cv then - local v = rawget(t, k1..k2) - if type(v) == "string" then - local scv = format("%x", cv) - return gsub(scv..sub(v, 2), "|e", "|"..scv) - end - end - end }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_mips.h b/third-party/LuaJIT-2.0.2/dynasm/dasm_mips.h deleted file mode 100644 index 0866beb8bf..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_mips.h +++ /dev/null @@ -1,416 +0,0 @@ -/* -** DynASM MIPS encoding engine. -** Copyright (C) 2005-2013 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "mips" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n); - if (ins & 2048) - n = n - (int)((char *)cp - base); - else - n = (n + (int)base) & 0x0fffffff; - patchrel: - CK((n & 3) == 0 && - ((n + ((ins & 2048) ? 0x00020000 : 0)) >> - ((ins & 2048) ? 18 : 28)) == 0, RANGE_REL); - cp[-1] |= ((n>>2) & ((ins & 2048) ? 0x0000ffff: 0x03ffffff)); - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_mips.lua b/third-party/LuaJIT-2.0.2/dynasm/dasm_mips.lua deleted file mode 100644 index 02ab9d5fe7..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_mips.lua +++ /dev/null @@ -1,953 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM MIPS module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "mips", - description = "DynASM MIPS module", - version = "1.3.0", - vernum = 10300, - release = "2012-01-23", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort -local bit = bit or require("bit") -local band, shl, sar, tohex = bit.band, bit.lshift, bit.arshift, bit.tohex - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(0xff000000 + w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n >= 0xff000000 then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp="r29", ra="r31" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r29" then return "sp" - elseif s == "r31" then return "ra" end - return s -end - ------------------------------------------------------------------------------- - --- Template strings for MIPS instructions. -local map_op = { - -- First-level opcodes. - j_1 = "08000000J", - jal_1 = "0c000000J", - b_1 = "10000000B", - beqz_2 = "10000000SB", - beq_3 = "10000000STB", - bnez_2 = "14000000SB", - bne_3 = "14000000STB", - blez_2 = "18000000SB", - bgtz_2 = "1c000000SB", - addi_3 = "20000000TSI", - li_2 = "24000000TI", - addiu_3 = "24000000TSI", - slti_3 = "28000000TSI", - sltiu_3 = "2c000000TSI", - andi_3 = "30000000TSU", - lu_2 = "34000000TU", - ori_3 = "34000000TSU", - xori_3 = "38000000TSU", - lui_2 = "3c000000TU", - beqzl_2 = "50000000SB", - beql_3 = "50000000STB", - bnezl_2 = "54000000SB", - bnel_3 = "54000000STB", - blezl_2 = "58000000SB", - bgtzl_2 = "5c000000SB", - lb_2 = "80000000TO", - lh_2 = "84000000TO", - lwl_2 = "88000000TO", - lw_2 = "8c000000TO", - lbu_2 = "90000000TO", - lhu_2 = "94000000TO", - lwr_2 = "98000000TO", - sb_2 = "a0000000TO", - sh_2 = "a4000000TO", - swl_2 = "a8000000TO", - sw_2 = "ac000000TO", - swr_2 = "b8000000TO", - cache_2 = "bc000000NO", - ll_2 = "c0000000TO", - lwc1_2 = "c4000000HO", - pref_2 = "cc000000NO", - ldc1_2 = "d4000000HO", - sc_2 = "e0000000TO", - swc1_2 = "e4000000HO", - sdc1_2 = "f4000000HO", - - -- Opcode SPECIAL. - nop_0 = "00000000", - sll_3 = "00000000DTA", - movf_2 = "00000001DS", - movf_3 = "00000001DSC", - movt_2 = "00010001DS", - movt_3 = "00010001DSC", - srl_3 = "00000002DTA", - rotr_3 = "00200002DTA", - sra_3 = "00000003DTA", - sllv_3 = "00000004DTS", - srlv_3 = "00000006DTS", - rotrv_3 = "00000046DTS", - srav_3 = "00000007DTS", - jr_1 = "00000008S", - jalr_1 = "0000f809S", - jalr_2 = "00000009DS", - movz_3 = "0000000aDST", - movn_3 = "0000000bDST", - syscall_0 = "0000000c", - syscall_1 = "0000000cY", - break_0 = "0000000d", - break_1 = "0000000dY", - sync_0 = "0000000f", - mfhi_1 = "00000010D", - mthi_1 = "00000011S", - mflo_1 = "00000012D", - mtlo_1 = "00000013S", - mult_2 = "00000018ST", - multu_2 = "00000019ST", - div_2 = "0000001aST", - divu_2 = "0000001bST", - add_3 = "00000020DST", - move_2 = "00000021DS", - addu_3 = "00000021DST", - sub_3 = "00000022DST", - negu_2 = "00000023DT", - subu_3 = "00000023DST", - and_3 = "00000024DST", - or_3 = "00000025DST", - xor_3 = "00000026DST", - not_2 = "00000027DS", - nor_3 = "00000027DST", - slt_3 = "0000002aDST", - sltu_3 = "0000002bDST", - tge_2 = "00000030ST", - tge_3 = "00000030STZ", - tgeu_2 = "00000031ST", - tgeu_3 = "00000031STZ", - tlt_2 = "00000032ST", - tlt_3 = "00000032STZ", - tltu_2 = "00000033ST", - tltu_3 = "00000033STZ", - teq_2 = "00000034ST", - teq_3 = "00000034STZ", - tne_2 = "00000036ST", - tne_3 = "00000036STZ", - - -- Opcode REGIMM. - bltz_2 = "04000000SB", - bgez_2 = "04010000SB", - bltzl_2 = "04020000SB", - bgezl_2 = "04030000SB", - tgei_2 = "04080000SI", - tgeiu_2 = "04090000SI", - tlti_2 = "040a0000SI", - tltiu_2 = "040b0000SI", - teqi_2 = "040c0000SI", - tnei_2 = "040e0000SI", - bltzal_2 = "04100000SB", - bal_1 = "04110000B", - bgezal_2 = "04110000SB", - bltzall_2 = "04120000SB", - bgezall_2 = "04130000SB", - synci_1 = "041f0000O", - - -- Opcode SPECIAL2. - madd_2 = "70000000ST", - maddu_2 = "70000001ST", - mul_3 = "70000002DST", - msub_2 = "70000004ST", - msubu_2 = "70000005ST", - clz_2 = "70000020DS=", - clo_2 = "70000021DS=", - sdbbp_0 = "7000003f", - sdbbp_1 = "7000003fY", - - -- Opcode SPECIAL3. - ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1 - ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1 - wsbh_2 = "7c0000a0DT", - seb_2 = "7c000420DT", - seh_2 = "7c000620DT", - rdhwr_2 = "7c00003bTD", - - -- Opcode COP0. - mfc0_2 = "40000000TD", - mfc0_3 = "40000000TDW", - mtc0_2 = "40800000TD", - mtc0_3 = "40800000TDW", - rdpgpr_2 = "41400000DT", - di_0 = "41606000", - di_1 = "41606000T", - ei_0 = "41606020", - ei_1 = "41606020T", - wrpgpr_2 = "41c00000DT", - tlbr_0 = "42000001", - tlbwi_0 = "42000002", - tlbwr_0 = "42000006", - tlbp_0 = "42000008", - eret_0 = "42000018", - deret_0 = "4200001f", - wait_0 = "42000020", - - -- Opcode COP1. - mfc1_2 = "44000000TG", - cfc1_2 = "44400000TG", - mfhc1_2 = "44600000TG", - mtc1_2 = "44800000TG", - ctc1_2 = "44c00000TG", - mthc1_2 = "44e00000TG", - - bc1f_1 = "45000000B", - bc1f_2 = "45000000CB", - bc1t_1 = "45010000B", - bc1t_2 = "45010000CB", - bc1fl_1 = "45020000B", - bc1fl_2 = "45020000CB", - bc1tl_1 = "45030000B", - bc1tl_2 = "45030000CB", - - ["add.s_3"] = "46000000FGH", - ["sub.s_3"] = "46000001FGH", - ["mul.s_3"] = "46000002FGH", - ["div.s_3"] = "46000003FGH", - ["sqrt.s_2"] = "46000004FG", - ["abs.s_2"] = "46000005FG", - ["mov.s_2"] = "46000006FG", - ["neg.s_2"] = "46000007FG", - ["round.l.s_2"] = "46000008FG", - ["trunc.l.s_2"] = "46000009FG", - ["ceil.l.s_2"] = "4600000aFG", - ["floor.l.s_2"] = "4600000bFG", - ["round.w.s_2"] = "4600000cFG", - ["trunc.w.s_2"] = "4600000dFG", - ["ceil.w.s_2"] = "4600000eFG", - ["floor.w.s_2"] = "4600000fFG", - ["movf.s_2"] = "46000011FG", - ["movf.s_3"] = "46000011FGC", - ["movt.s_2"] = "46010011FG", - ["movt.s_3"] = "46010011FGC", - ["movz.s_3"] = "46000012FGT", - ["movn.s_3"] = "46000013FGT", - ["recip.s_2"] = "46000015FG", - ["rsqrt.s_2"] = "46000016FG", - ["cvt.d.s_2"] = "46000021FG", - ["cvt.w.s_2"] = "46000024FG", - ["cvt.l.s_2"] = "46000025FG", - ["cvt.ps.s_3"] = "46000026FGH", - ["c.f.s_2"] = "46000030GH", - ["c.f.s_3"] = "46000030VGH", - ["c.un.s_2"] = "46000031GH", - ["c.un.s_3"] = "46000031VGH", - ["c.eq.s_2"] = "46000032GH", - ["c.eq.s_3"] = "46000032VGH", - ["c.ueq.s_2"] = "46000033GH", - ["c.ueq.s_3"] = "46000033VGH", - ["c.olt.s_2"] = "46000034GH", - ["c.olt.s_3"] = "46000034VGH", - ["c.ult.s_2"] = "46000035GH", - ["c.ult.s_3"] = "46000035VGH", - ["c.ole.s_2"] = "46000036GH", - ["c.ole.s_3"] = "46000036VGH", - ["c.ule.s_2"] = "46000037GH", - ["c.ule.s_3"] = "46000037VGH", - ["c.sf.s_2"] = "46000038GH", - ["c.sf.s_3"] = "46000038VGH", - ["c.ngle.s_2"] = "46000039GH", - ["c.ngle.s_3"] = "46000039VGH", - ["c.seq.s_2"] = "4600003aGH", - ["c.seq.s_3"] = "4600003aVGH", - ["c.ngl.s_2"] = "4600003bGH", - ["c.ngl.s_3"] = "4600003bVGH", - ["c.lt.s_2"] = "4600003cGH", - ["c.lt.s_3"] = "4600003cVGH", - ["c.nge.s_2"] = "4600003dGH", - ["c.nge.s_3"] = "4600003dVGH", - ["c.le.s_2"] = "4600003eGH", - ["c.le.s_3"] = "4600003eVGH", - ["c.ngt.s_2"] = "4600003fGH", - ["c.ngt.s_3"] = "4600003fVGH", - - ["add.d_3"] = "46200000FGH", - ["sub.d_3"] = "46200001FGH", - ["mul.d_3"] = "46200002FGH", - ["div.d_3"] = "46200003FGH", - ["sqrt.d_2"] = "46200004FG", - ["abs.d_2"] = "46200005FG", - ["mov.d_2"] = "46200006FG", - ["neg.d_2"] = "46200007FG", - ["round.l.d_2"] = "46200008FG", - ["trunc.l.d_2"] = "46200009FG", - ["ceil.l.d_2"] = "4620000aFG", - ["floor.l.d_2"] = "4620000bFG", - ["round.w.d_2"] = "4620000cFG", - ["trunc.w.d_2"] = "4620000dFG", - ["ceil.w.d_2"] = "4620000eFG", - ["floor.w.d_2"] = "4620000fFG", - ["movf.d_2"] = "46200011FG", - ["movf.d_3"] = "46200011FGC", - ["movt.d_2"] = "46210011FG", - ["movt.d_3"] = "46210011FGC", - ["movz.d_3"] = "46200012FGT", - ["movn.d_3"] = "46200013FGT", - ["recip.d_2"] = "46200015FG", - ["rsqrt.d_2"] = "46200016FG", - ["cvt.s.d_2"] = "46200020FG", - ["cvt.w.d_2"] = "46200024FG", - ["cvt.l.d_2"] = "46200025FG", - ["c.f.d_2"] = "46200030GH", - ["c.f.d_3"] = "46200030VGH", - ["c.un.d_2"] = "46200031GH", - ["c.un.d_3"] = "46200031VGH", - ["c.eq.d_2"] = "46200032GH", - ["c.eq.d_3"] = "46200032VGH", - ["c.ueq.d_2"] = "46200033GH", - ["c.ueq.d_3"] = "46200033VGH", - ["c.olt.d_2"] = "46200034GH", - ["c.olt.d_3"] = "46200034VGH", - ["c.ult.d_2"] = "46200035GH", - ["c.ult.d_3"] = "46200035VGH", - ["c.ole.d_2"] = "46200036GH", - ["c.ole.d_3"] = "46200036VGH", - ["c.ule.d_2"] = "46200037GH", - ["c.ule.d_3"] = "46200037VGH", - ["c.sf.d_2"] = "46200038GH", - ["c.sf.d_3"] = "46200038VGH", - ["c.ngle.d_2"] = "46200039GH", - ["c.ngle.d_3"] = "46200039VGH", - ["c.seq.d_2"] = "4620003aGH", - ["c.seq.d_3"] = "4620003aVGH", - ["c.ngl.d_2"] = "4620003bGH", - ["c.ngl.d_3"] = "4620003bVGH", - ["c.lt.d_2"] = "4620003cGH", - ["c.lt.d_3"] = "4620003cVGH", - ["c.nge.d_2"] = "4620003dGH", - ["c.nge.d_3"] = "4620003dVGH", - ["c.le.d_2"] = "4620003eGH", - ["c.le.d_3"] = "4620003eVGH", - ["c.ngt.d_2"] = "4620003fGH", - ["c.ngt.d_3"] = "4620003fVGH", - - ["add.ps_3"] = "46c00000FGH", - ["sub.ps_3"] = "46c00001FGH", - ["mul.ps_3"] = "46c00002FGH", - ["abs.ps_2"] = "46c00005FG", - ["mov.ps_2"] = "46c00006FG", - ["neg.ps_2"] = "46c00007FG", - ["movf.ps_2"] = "46c00011FG", - ["movf.ps_3"] = "46c00011FGC", - ["movt.ps_2"] = "46c10011FG", - ["movt.ps_3"] = "46c10011FGC", - ["movz.ps_3"] = "46c00012FGT", - ["movn.ps_3"] = "46c00013FGT", - ["cvt.s.pu_2"] = "46c00020FG", - ["cvt.s.pl_2"] = "46c00028FG", - ["pll.ps_3"] = "46c0002cFGH", - ["plu.ps_3"] = "46c0002dFGH", - ["pul.ps_3"] = "46c0002eFGH", - ["puu.ps_3"] = "46c0002fFGH", - ["c.f.ps_2"] = "46c00030GH", - ["c.f.ps_3"] = "46c00030VGH", - ["c.un.ps_2"] = "46c00031GH", - ["c.un.ps_3"] = "46c00031VGH", - ["c.eq.ps_2"] = "46c00032GH", - ["c.eq.ps_3"] = "46c00032VGH", - ["c.ueq.ps_2"] = "46c00033GH", - ["c.ueq.ps_3"] = "46c00033VGH", - ["c.olt.ps_2"] = "46c00034GH", - ["c.olt.ps_3"] = "46c00034VGH", - ["c.ult.ps_2"] = "46c00035GH", - ["c.ult.ps_3"] = "46c00035VGH", - ["c.ole.ps_2"] = "46c00036GH", - ["c.ole.ps_3"] = "46c00036VGH", - ["c.ule.ps_2"] = "46c00037GH", - ["c.ule.ps_3"] = "46c00037VGH", - ["c.sf.ps_2"] = "46c00038GH", - ["c.sf.ps_3"] = "46c00038VGH", - ["c.ngle.ps_2"] = "46c00039GH", - ["c.ngle.ps_3"] = "46c00039VGH", - ["c.seq.ps_2"] = "46c0003aGH", - ["c.seq.ps_3"] = "46c0003aVGH", - ["c.ngl.ps_2"] = "46c0003bGH", - ["c.ngl.ps_3"] = "46c0003bVGH", - ["c.lt.ps_2"] = "46c0003cGH", - ["c.lt.ps_3"] = "46c0003cVGH", - ["c.nge.ps_2"] = "46c0003dGH", - ["c.nge.ps_3"] = "46c0003dVGH", - ["c.le.ps_2"] = "46c0003eGH", - ["c.le.ps_3"] = "46c0003eVGH", - ["c.ngt.ps_2"] = "46c0003fGH", - ["c.ngt.ps_3"] = "46c0003fVGH", - - ["cvt.s.w_2"] = "46800020FG", - ["cvt.d.w_2"] = "46800021FG", - - ["cvt.s.l_2"] = "46a00020FG", - ["cvt.d.l_2"] = "46a00021FG", - - -- Opcode COP1X. - lwxc1_2 = "4c000000FX", - ldxc1_2 = "4c000001FX", - luxc1_2 = "4c000005FX", - swxc1_2 = "4c000008FX", - sdxc1_2 = "4c000009FX", - suxc1_2 = "4c00000dFX", - prefx_2 = "4c00000fMX", - ["alnv.ps_4"] = "4c00001eFGHS", - ["madd.s_4"] = "4c000020FRGH", - ["madd.d_4"] = "4c000021FRGH", - ["madd.ps_4"] = "4c000026FRGH", - ["msub.s_4"] = "4c000028FRGH", - ["msub.d_4"] = "4c000029FRGH", - ["msub.ps_4"] = "4c00002eFRGH", - ["nmadd.s_4"] = "4c000030FRGH", - ["nmadd.d_4"] = "4c000031FRGH", - ["nmadd.ps_4"] = "4c000036FRGH", - ["nmsub.s_4"] = "4c000038FRGH", - ["nmsub.d_4"] = "4c000039FRGH", - ["nmsub.ps_4"] = "4c00003eFRGH", -} - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_imm(imm, bits, shift, scale, signed) - local n = tonumber(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n then - if signed then - local s = sar(m, bits-1) - if s == 0 then return shl(m, shift) - elseif s == -1 then return shl(m + shl(1, bits), shift) end - else - if sar(m, bits) == 0 then return shl(m, shift) end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^[rf]([1-3]?[0-9])$") or - match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = shl(parse_gpr(reg), 21) - local extname = match(imm, "^extern%s+(%S+)$") - if extname then - waction("REL_EXT", map_extern[extname], nil, 1) - return r - else - return r + parse_imm(imm, 16, 0, 0, true) - end - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return shl(r, 21) - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_index(idx) - local rt, rs = match(idx, "^(.*)%(([%w_:]+)%)$") - if rt then - rt = parse_gpr(rt) - rs = parse_gpr(rs) - return shl(rt, 16) + shl(rs, 21) - end - werror("bad index `"..idx.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 2 positions (ins/ext). - if secpos+2 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "D" then - op = op + shl(parse_gpr(params[n]), 11); n = n + 1 - elseif p == "T" then - op = op + shl(parse_gpr(params[n]), 16); n = n + 1 - elseif p == "S" then - op = op + shl(parse_gpr(params[n]), 21); n = n + 1 - elseif p == "F" then - op = op + shl(parse_fpr(params[n]), 6); n = n + 1 - elseif p == "G" then - op = op + shl(parse_fpr(params[n]), 11); n = n + 1 - elseif p == "H" then - op = op + shl(parse_fpr(params[n]), 16); n = n + 1 - elseif p == "R" then - op = op + shl(parse_fpr(params[n]), 21); n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "O" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "X" then - op = op + parse_index(params[n]); n = n + 1 - elseif p == "B" or p == "J" then - local mode, n, s = parse_label(params[n], false) - if p == "B" then n = n + 2048 end - waction("REL_"..mode, n, s, 1) - n = n + 1 - elseif p == "A" then - op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1 - elseif p == "M" then - op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1 - elseif p == "N" then - op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1 - elseif p == "C" then - op = op + parse_imm(params[n], 3, 18, 0, false); n = n + 1 - elseif p == "V" then - op = op + parse_imm(params[n], 3, 8, 0, false); n = n + 1 - elseif p == "W" then - op = op + parse_imm(params[n], 3, 0, 0, false); n = n + 1 - elseif p == "Y" then - op = op + parse_imm(params[n], 20, 6, 0, false); n = n + 1 - elseif p == "Z" then - op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1 - elseif p == "=" then - op = op + shl(band(op, 0xf800), 5) -- Copy D to T for clz, clo. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_ppc.h b/third-party/LuaJIT-2.0.2/dynasm/dasm_ppc.h deleted file mode 100644 index 13c446110c..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_ppc.h +++ /dev/null @@ -1,412 +0,0 @@ -/* -** DynASM PPC encoding engine. -** Copyright (C) 2005-2013 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "ppc" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1) - 4; - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base); - patchrel: - CK((n & 3) == 0 && - (((n+4) + ((ins & 2048) ? 0x00008000 : 0x02000000)) >> - ((ins & 2048) ? 16 : 26)) == 0, RANGE_REL); - cp[-1] |= ((n+4) & ((ins & 2048) ? 0x0000fffc: 0x03fffffc)); - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_ppc.lua b/third-party/LuaJIT-2.0.2/dynasm/dasm_ppc.lua deleted file mode 100644 index 65da317b8b..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_ppc.lua +++ /dev/null @@ -1,1249 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM PPC module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "ppc", - description = "DynASM PPC module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort -local bit = bit or require("bit") -local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift -local tohex = bit.tohex - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0xffffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp = "r1" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r1" then return "sp" end - return s -end - -local map_cond = { - lt = 0, gt = 1, eq = 2, so = 3, - ge = 4, le = 5, ne = 6, ns = 7, -} - ------------------------------------------------------------------------------- - --- Template strings for PPC instructions. -local map_op = { - tdi_3 = "08000000ARI", - twi_3 = "0c000000ARI", - mulli_3 = "1c000000RRI", - subfic_3 = "20000000RRI", - cmplwi_3 = "28000000XRU", - cmplwi_2 = "28000000-RU", - cmpldi_3 = "28200000XRU", - cmpldi_2 = "28200000-RU", - cmpwi_3 = "2c000000XRI", - cmpwi_2 = "2c000000-RI", - cmpdi_3 = "2c200000XRI", - cmpdi_2 = "2c200000-RI", - addic_3 = "30000000RRI", - ["addic._3"] = "34000000RRI", - addi_3 = "38000000RR0I", - li_2 = "38000000RI", - la_2 = "38000000RD", - addis_3 = "3c000000RR0I", - lis_2 = "3c000000RI", - lus_2 = "3c000000RU", - bc_3 = "40000000AAK", - bcl_3 = "40000001AAK", - bdnz_1 = "42000000K", - bdz_1 = "42400000K", - sc_0 = "44000000", - b_1 = "48000000J", - bl_1 = "48000001J", - rlwimi_5 = "50000000RR~AAA.", - rlwinm_5 = "54000000RR~AAA.", - rlwnm_5 = "5c000000RR~RAA.", - ori_3 = "60000000RR~U", - nop_0 = "60000000", - oris_3 = "64000000RR~U", - xori_3 = "68000000RR~U", - xoris_3 = "6c000000RR~U", - ["andi._3"] = "70000000RR~U", - ["andis._3"] = "74000000RR~U", - lwz_2 = "80000000RD", - lwzu_2 = "84000000RD", - lbz_2 = "88000000RD", - lbzu_2 = "8c000000RD", - stw_2 = "90000000RD", - stwu_2 = "94000000RD", - stb_2 = "98000000RD", - stbu_2 = "9c000000RD", - lhz_2 = "a0000000RD", - lhzu_2 = "a4000000RD", - lha_2 = "a8000000RD", - lhau_2 = "ac000000RD", - sth_2 = "b0000000RD", - sthu_2 = "b4000000RD", - lmw_2 = "b8000000RD", - stmw_2 = "bc000000RD", - lfs_2 = "c0000000FD", - lfsu_2 = "c4000000FD", - lfd_2 = "c8000000FD", - lfdu_2 = "cc000000FD", - stfs_2 = "d0000000FD", - stfsu_2 = "d4000000FD", - stfd_2 = "d8000000FD", - stfdu_2 = "dc000000FD", - ld_2 = "e8000000RD", -- NYI: displacement must be divisible by 4. - ldu_2 = "e8000001RD", - lwa_2 = "e8000002RD", - std_2 = "f8000000RD", - stdu_2 = "f8000001RD", - - -- Primary opcode 19: - mcrf_2 = "4c000000XX", - isync_0 = "4c00012c", - crnor_3 = "4c000042CCC", - crnot_2 = "4c000042CC=", - crandc_3 = "4c000102CCC", - crxor_3 = "4c000182CCC", - crclr_1 = "4c000182C==", - crnand_3 = "4c0001c2CCC", - crand_3 = "4c000202CCC", - creqv_3 = "4c000242CCC", - crset_1 = "4c000242C==", - crorc_3 = "4c000342CCC", - cror_3 = "4c000382CCC", - crmove_2 = "4c000382CC=", - bclr_2 = "4c000020AA", - bclrl_2 = "4c000021AA", - bcctr_2 = "4c000420AA", - bcctrl_2 = "4c000421AA", - blr_0 = "4e800020", - blrl_0 = "4e800021", - bctr_0 = "4e800420", - bctrl_0 = "4e800421", - - -- Primary opcode 31: - cmpw_3 = "7c000000XRR", - cmpw_2 = "7c000000-RR", - cmpd_3 = "7c200000XRR", - cmpd_2 = "7c200000-RR", - tw_3 = "7c000008ARR", - subfc_3 = "7c000010RRR.", - subc_3 = "7c000010RRR~.", - mulhdu_3 = "7c000012RRR.", - addc_3 = "7c000014RRR.", - mulhwu_3 = "7c000016RRR.", - isel_4 = "7c00001eRRRC", - isellt_3 = "7c00001eRRR", - iselgt_3 = "7c00005eRRR", - iseleq_3 = "7c00009eRRR", - mfcr_1 = "7c000026R", - mfocrf_2 = "7c100026RG", - mtcrf_2 = "7c000120GR", - mtocrf_2 = "7c100120GR", - lwarx_3 = "7c000028RR0R", - ldx_3 = "7c00002aRR0R", - lwzx_3 = "7c00002eRR0R", - slw_3 = "7c000030RR~R.", - cntlzw_2 = "7c000034RR~", - sld_3 = "7c000036RR~R.", - and_3 = "7c000038RR~R.", - cmplw_3 = "7c000040XRR", - cmplw_2 = "7c000040-RR", - cmpld_3 = "7c200040XRR", - cmpld_2 = "7c200040-RR", - subf_3 = "7c000050RRR.", - sub_3 = "7c000050RRR~.", - ldux_3 = "7c00006aRR0R", - dcbst_2 = "7c00006c-RR", - lwzux_3 = "7c00006eRR0R", - cntlzd_2 = "7c000074RR~", - andc_3 = "7c000078RR~R.", - td_3 = "7c000088ARR", - mulhd_3 = "7c000092RRR.", - mulhw_3 = "7c000096RRR.", - ldarx_3 = "7c0000a8RR0R", - dcbf_2 = "7c0000ac-RR", - lbzx_3 = "7c0000aeRR0R", - neg_2 = "7c0000d0RR.", - lbzux_3 = "7c0000eeRR0R", - popcntb_2 = "7c0000f4RR~", - not_2 = "7c0000f8RR~%.", - nor_3 = "7c0000f8RR~R.", - subfe_3 = "7c000110RRR.", - sube_3 = "7c000110RRR~.", - adde_3 = "7c000114RRR.", - stdx_3 = "7c00012aRR0R", - stwcx_3 = "7c00012cRR0R.", - stwx_3 = "7c00012eRR0R", - prtyw_2 = "7c000134RR~", - stdux_3 = "7c00016aRR0R", - stwux_3 = "7c00016eRR0R", - prtyd_2 = "7c000174RR~", - subfze_2 = "7c000190RR.", - addze_2 = "7c000194RR.", - stdcx_3 = "7c0001acRR0R.", - stbx_3 = "7c0001aeRR0R", - subfme_2 = "7c0001d0RR.", - mulld_3 = "7c0001d2RRR.", - addme_2 = "7c0001d4RR.", - mullw_3 = "7c0001d6RRR.", - dcbtst_2 = "7c0001ec-RR", - stbux_3 = "7c0001eeRR0R", - add_3 = "7c000214RRR.", - dcbt_2 = "7c00022c-RR", - lhzx_3 = "7c00022eRR0R", - eqv_3 = "7c000238RR~R.", - eciwx_3 = "7c00026cRR0R", - lhzux_3 = "7c00026eRR0R", - xor_3 = "7c000278RR~R.", - mfspefscr_1 = "7c0082a6R", - mfxer_1 = "7c0102a6R", - mflr_1 = "7c0802a6R", - mfctr_1 = "7c0902a6R", - lwax_3 = "7c0002aaRR0R", - lhax_3 = "7c0002aeRR0R", - mftb_1 = "7c0c42e6R", - mftbu_1 = "7c0d42e6R", - lwaux_3 = "7c0002eaRR0R", - lhaux_3 = "7c0002eeRR0R", - sthx_3 = "7c00032eRR0R", - orc_3 = "7c000338RR~R.", - ecowx_3 = "7c00036cRR0R", - sthux_3 = "7c00036eRR0R", - or_3 = "7c000378RR~R.", - mr_2 = "7c000378RR~%.", - divdu_3 = "7c000392RRR.", - divwu_3 = "7c000396RRR.", - mtspefscr_1 = "7c0083a6R", - mtxer_1 = "7c0103a6R", - mtlr_1 = "7c0803a6R", - mtctr_1 = "7c0903a6R", - dcbi_2 = "7c0003ac-RR", - nand_3 = "7c0003b8RR~R.", - divd_3 = "7c0003d2RRR.", - divw_3 = "7c0003d6RRR.", - cmpb_3 = "7c0003f8RR~R.", - mcrxr_1 = "7c000400X", - subfco_3 = "7c000410RRR.", - subco_3 = "7c000410RRR~.", - addco_3 = "7c000414RRR.", - ldbrx_3 = "7c000428RR0R", - lswx_3 = "7c00042aRR0R", - lwbrx_3 = "7c00042cRR0R", - lfsx_3 = "7c00042eFR0R", - srw_3 = "7c000430RR~R.", - srd_3 = "7c000436RR~R.", - subfo_3 = "7c000450RRR.", - subo_3 = "7c000450RRR~.", - lfsux_3 = "7c00046eFR0R", - lswi_3 = "7c0004aaRR0A", - sync_0 = "7c0004ac", - lwsync_0 = "7c2004ac", - ptesync_0 = "7c4004ac", - lfdx_3 = "7c0004aeFR0R", - nego_2 = "7c0004d0RR.", - lfdux_3 = "7c0004eeFR0R", - subfeo_3 = "7c000510RRR.", - subeo_3 = "7c000510RRR~.", - addeo_3 = "7c000514RRR.", - stdbrx_3 = "7c000528RR0R", - stswx_3 = "7c00052aRR0R", - stwbrx_3 = "7c00052cRR0R", - stfsx_3 = "7c00052eFR0R", - stfsux_3 = "7c00056eFR0R", - subfzeo_2 = "7c000590RR.", - addzeo_2 = "7c000594RR.", - stswi_3 = "7c0005aaRR0A", - stfdx_3 = "7c0005aeFR0R", - subfmeo_2 = "7c0005d0RR.", - mulldo_3 = "7c0005d2RRR.", - addmeo_2 = "7c0005d4RR.", - mullwo_3 = "7c0005d6RRR.", - dcba_2 = "7c0005ec-RR", - stfdux_3 = "7c0005eeFR0R", - addo_3 = "7c000614RRR.", - lhbrx_3 = "7c00062cRR0R", - sraw_3 = "7c000630RR~R.", - srad_3 = "7c000634RR~R.", - srawi_3 = "7c000670RR~A.", - sradi_3 = "7c000674RR~H.", - eieio_0 = "7c0006ac", - lfiwax_3 = "7c0006aeFR0R", - sthbrx_3 = "7c00072cRR0R", - extsh_2 = "7c000734RR~.", - extsb_2 = "7c000774RR~.", - divduo_3 = "7c000792RRR.", - divwou_3 = "7c000796RRR.", - icbi_2 = "7c0007ac-RR", - stfiwx_3 = "7c0007aeFR0R", - extsw_2 = "7c0007b4RR~.", - divdo_3 = "7c0007d2RRR.", - divwo_3 = "7c0007d6RRR.", - dcbz_2 = "7c0007ec-RR", - - -- Primary opcode 30: - rldicl_4 = "78000000RR~HM.", - rldicr_4 = "78000004RR~HM.", - rldic_4 = "78000008RR~HM.", - rldimi_4 = "7800000cRR~HM.", - rldcl_4 = "78000010RR~RM.", - rldcr_4 = "78000012RR~RM.", - - -- Primary opcode 59: - fdivs_3 = "ec000024FFF.", - fsubs_3 = "ec000028FFF.", - fadds_3 = "ec00002aFFF.", - fsqrts_2 = "ec00002cF-F.", - fres_2 = "ec000030F-F.", - fmuls_3 = "ec000032FF-F.", - frsqrtes_2 = "ec000034F-F.", - fmsubs_4 = "ec000038FFFF~.", - fmadds_4 = "ec00003aFFFF~.", - fnmsubs_4 = "ec00003cFFFF~.", - fnmadds_4 = "ec00003eFFFF~.", - - -- Primary opcode 63: - fdiv_3 = "fc000024FFF.", - fsub_3 = "fc000028FFF.", - fadd_3 = "fc00002aFFF.", - fsqrt_2 = "fc00002cF-F.", - fsel_4 = "fc00002eFFFF~.", - fre_2 = "fc000030F-F.", - fmul_3 = "fc000032FF-F.", - frsqrte_2 = "fc000034F-F.", - fmsub_4 = "fc000038FFFF~.", - fmadd_4 = "fc00003aFFFF~.", - fnmsub_4 = "fc00003cFFFF~.", - fnmadd_4 = "fc00003eFFFF~.", - fcmpu_3 = "fc000000XFF", - fcpsgn_3 = "fc000010FFF.", - fcmpo_3 = "fc000040XFF", - mtfsb1_1 = "fc00004cA", - fneg_2 = "fc000050F-F.", - mcrfs_2 = "fc000080XX", - mtfsb0_1 = "fc00008cA", - fmr_2 = "fc000090F-F.", - frsp_2 = "fc000018F-F.", - fctiw_2 = "fc00001cF-F.", - fctiwz_2 = "fc00001eF-F.", - mtfsfi_2 = "fc00010cAA", -- NYI: upshift. - fnabs_2 = "fc000110F-F.", - fabs_2 = "fc000210F-F.", - frin_2 = "fc000310F-F.", - friz_2 = "fc000350F-F.", - frip_2 = "fc000390F-F.", - frim_2 = "fc0003d0F-F.", - mffs_1 = "fc00048eF.", - -- NYI: mtfsf, mtfsb0, mtfsb1. - fctid_2 = "fc00065cF-F.", - fctidz_2 = "fc00065eF-F.", - fcfid_2 = "fc00069cF-F.", - - -- Primary opcode 4, SPE APU extension: - evaddw_3 = "10000200RRR", - evaddiw_3 = "10000202RAR~", - evsubw_3 = "10000204RRR~", - evsubiw_3 = "10000206RAR~", - evabs_2 = "10000208RR", - evneg_2 = "10000209RR", - evextsb_2 = "1000020aRR", - evextsh_2 = "1000020bRR", - evrndw_2 = "1000020cRR", - evcntlzw_2 = "1000020dRR", - evcntlsw_2 = "1000020eRR", - brinc_3 = "1000020fRRR", - evand_3 = "10000211RRR", - evandc_3 = "10000212RRR", - evxor_3 = "10000216RRR", - evor_3 = "10000217RRR", - evmr_2 = "10000217RR=", - evnor_3 = "10000218RRR", - evnot_2 = "10000218RR=", - eveqv_3 = "10000219RRR", - evorc_3 = "1000021bRRR", - evnand_3 = "1000021eRRR", - evsrwu_3 = "10000220RRR", - evsrws_3 = "10000221RRR", - evsrwiu_3 = "10000222RRA", - evsrwis_3 = "10000223RRA", - evslw_3 = "10000224RRR", - evslwi_3 = "10000226RRA", - evrlw_3 = "10000228RRR", - evsplati_2 = "10000229RS", - evrlwi_3 = "1000022aRRA", - evsplatfi_2 = "1000022bRS", - evmergehi_3 = "1000022cRRR", - evmergelo_3 = "1000022dRRR", - evcmpgtu_3 = "10000230XRR", - evcmpgtu_2 = "10000230-RR", - evcmpgts_3 = "10000231XRR", - evcmpgts_2 = "10000231-RR", - evcmpltu_3 = "10000232XRR", - evcmpltu_2 = "10000232-RR", - evcmplts_3 = "10000233XRR", - evcmplts_2 = "10000233-RR", - evcmpeq_3 = "10000234XRR", - evcmpeq_2 = "10000234-RR", - evsel_4 = "10000278RRRW", - evsel_3 = "10000278RRR", - evfsadd_3 = "10000280RRR", - evfssub_3 = "10000281RRR", - evfsabs_2 = "10000284RR", - evfsnabs_2 = "10000285RR", - evfsneg_2 = "10000286RR", - evfsmul_3 = "10000288RRR", - evfsdiv_3 = "10000289RRR", - evfscmpgt_3 = "1000028cXRR", - evfscmpgt_2 = "1000028c-RR", - evfscmplt_3 = "1000028dXRR", - evfscmplt_2 = "1000028d-RR", - evfscmpeq_3 = "1000028eXRR", - evfscmpeq_2 = "1000028e-RR", - evfscfui_2 = "10000290R-R", - evfscfsi_2 = "10000291R-R", - evfscfuf_2 = "10000292R-R", - evfscfsf_2 = "10000293R-R", - evfsctui_2 = "10000294R-R", - evfsctsi_2 = "10000295R-R", - evfsctuf_2 = "10000296R-R", - evfsctsf_2 = "10000297R-R", - evfsctuiz_2 = "10000298R-R", - evfsctsiz_2 = "1000029aR-R", - evfststgt_3 = "1000029cXRR", - evfststgt_2 = "1000029c-RR", - evfststlt_3 = "1000029dXRR", - evfststlt_2 = "1000029d-RR", - evfststeq_3 = "1000029eXRR", - evfststeq_2 = "1000029e-RR", - efsadd_3 = "100002c0RRR", - efssub_3 = "100002c1RRR", - efsabs_2 = "100002c4RR", - efsnabs_2 = "100002c5RR", - efsneg_2 = "100002c6RR", - efsmul_3 = "100002c8RRR", - efsdiv_3 = "100002c9RRR", - efscmpgt_3 = "100002ccXRR", - efscmpgt_2 = "100002cc-RR", - efscmplt_3 = "100002cdXRR", - efscmplt_2 = "100002cd-RR", - efscmpeq_3 = "100002ceXRR", - efscmpeq_2 = "100002ce-RR", - efscfd_2 = "100002cfR-R", - efscfui_2 = "100002d0R-R", - efscfsi_2 = "100002d1R-R", - efscfuf_2 = "100002d2R-R", - efscfsf_2 = "100002d3R-R", - efsctui_2 = "100002d4R-R", - efsctsi_2 = "100002d5R-R", - efsctuf_2 = "100002d6R-R", - efsctsf_2 = "100002d7R-R", - efsctuiz_2 = "100002d8R-R", - efsctsiz_2 = "100002daR-R", - efststgt_3 = "100002dcXRR", - efststgt_2 = "100002dc-RR", - efststlt_3 = "100002ddXRR", - efststlt_2 = "100002dd-RR", - efststeq_3 = "100002deXRR", - efststeq_2 = "100002de-RR", - efdadd_3 = "100002e0RRR", - efdsub_3 = "100002e1RRR", - efdcfuid_2 = "100002e2R-R", - efdcfsid_2 = "100002e3R-R", - efdabs_2 = "100002e4RR", - efdnabs_2 = "100002e5RR", - efdneg_2 = "100002e6RR", - efdmul_3 = "100002e8RRR", - efddiv_3 = "100002e9RRR", - efdctuidz_2 = "100002eaR-R", - efdctsidz_2 = "100002ebR-R", - efdcmpgt_3 = "100002ecXRR", - efdcmpgt_2 = "100002ec-RR", - efdcmplt_3 = "100002edXRR", - efdcmplt_2 = "100002ed-RR", - efdcmpeq_3 = "100002eeXRR", - efdcmpeq_2 = "100002ee-RR", - efdcfs_2 = "100002efR-R", - efdcfui_2 = "100002f0R-R", - efdcfsi_2 = "100002f1R-R", - efdcfuf_2 = "100002f2R-R", - efdcfsf_2 = "100002f3R-R", - efdctui_2 = "100002f4R-R", - efdctsi_2 = "100002f5R-R", - efdctuf_2 = "100002f6R-R", - efdctsf_2 = "100002f7R-R", - efdctuiz_2 = "100002f8R-R", - efdctsiz_2 = "100002faR-R", - efdtstgt_3 = "100002fcXRR", - efdtstgt_2 = "100002fc-RR", - efdtstlt_3 = "100002fdXRR", - efdtstlt_2 = "100002fd-RR", - efdtsteq_3 = "100002feXRR", - efdtsteq_2 = "100002fe-RR", - evlddx_3 = "10000300RR0R", - evldd_2 = "10000301R8", - evldwx_3 = "10000302RR0R", - evldw_2 = "10000303R8", - evldhx_3 = "10000304RR0R", - evldh_2 = "10000305R8", - evlwhex_3 = "10000310RR0R", - evlwhe_2 = "10000311R4", - evlwhoux_3 = "10000314RR0R", - evlwhou_2 = "10000315R4", - evlwhosx_3 = "10000316RR0R", - evlwhos_2 = "10000317R4", - evstddx_3 = "10000320RR0R", - evstdd_2 = "10000321R8", - evstdwx_3 = "10000322RR0R", - evstdw_2 = "10000323R8", - evstdhx_3 = "10000324RR0R", - evstdh_2 = "10000325R8", - evstwhex_3 = "10000330RR0R", - evstwhe_2 = "10000331R4", - evstwhox_3 = "10000334RR0R", - evstwho_2 = "10000335R4", - evstwwex_3 = "10000338RR0R", - evstwwe_2 = "10000339R4", - evstwwox_3 = "1000033cRR0R", - evstwwo_2 = "1000033dR4", - evmhessf_3 = "10000403RRR", - evmhossf_3 = "10000407RRR", - evmheumi_3 = "10000408RRR", - evmhesmi_3 = "10000409RRR", - evmhesmf_3 = "1000040bRRR", - evmhoumi_3 = "1000040cRRR", - evmhosmi_3 = "1000040dRRR", - evmhosmf_3 = "1000040fRRR", - evmhessfa_3 = "10000423RRR", - evmhossfa_3 = "10000427RRR", - evmheumia_3 = "10000428RRR", - evmhesmia_3 = "10000429RRR", - evmhesmfa_3 = "1000042bRRR", - evmhoumia_3 = "1000042cRRR", - evmhosmia_3 = "1000042dRRR", - evmhosmfa_3 = "1000042fRRR", - evmwhssf_3 = "10000447RRR", - evmwlumi_3 = "10000448RRR", - evmwhumi_3 = "1000044cRRR", - evmwhsmi_3 = "1000044dRRR", - evmwhsmf_3 = "1000044fRRR", - evmwssf_3 = "10000453RRR", - evmwumi_3 = "10000458RRR", - evmwsmi_3 = "10000459RRR", - evmwsmf_3 = "1000045bRRR", - evmwhssfa_3 = "10000467RRR", - evmwlumia_3 = "10000468RRR", - evmwhumia_3 = "1000046cRRR", - evmwhsmia_3 = "1000046dRRR", - evmwhsmfa_3 = "1000046fRRR", - evmwssfa_3 = "10000473RRR", - evmwumia_3 = "10000478RRR", - evmwsmia_3 = "10000479RRR", - evmwsmfa_3 = "1000047bRRR", - evmra_2 = "100004c4RR", - evdivws_3 = "100004c6RRR", - evdivwu_3 = "100004c7RRR", - evmwssfaa_3 = "10000553RRR", - evmwumiaa_3 = "10000558RRR", - evmwsmiaa_3 = "10000559RRR", - evmwsmfaa_3 = "1000055bRRR", - evmwssfan_3 = "100005d3RRR", - evmwumian_3 = "100005d8RRR", - evmwsmian_3 = "100005d9RRR", - evmwsmfan_3 = "100005dbRRR", - evmergehilo_3 = "1000022eRRR", - evmergelohi_3 = "1000022fRRR", - evlhhesplatx_3 = "10000308RR0R", - evlhhesplat_2 = "10000309R2", - evlhhousplatx_3 = "1000030cRR0R", - evlhhousplat_2 = "1000030dR2", - evlhhossplatx_3 = "1000030eRR0R", - evlhhossplat_2 = "1000030fR2", - evlwwsplatx_3 = "10000318RR0R", - evlwwsplat_2 = "10000319R4", - evlwhsplatx_3 = "1000031cRR0R", - evlwhsplat_2 = "1000031dR4", - evaddusiaaw_2 = "100004c0RR", - evaddssiaaw_2 = "100004c1RR", - evsubfusiaaw_2 = "100004c2RR", - evsubfssiaaw_2 = "100004c3RR", - evaddumiaaw_2 = "100004c8RR", - evaddsmiaaw_2 = "100004c9RR", - evsubfumiaaw_2 = "100004caRR", - evsubfsmiaaw_2 = "100004cbRR", - evmheusiaaw_3 = "10000500RRR", - evmhessiaaw_3 = "10000501RRR", - evmhessfaaw_3 = "10000503RRR", - evmhousiaaw_3 = "10000504RRR", - evmhossiaaw_3 = "10000505RRR", - evmhossfaaw_3 = "10000507RRR", - evmheumiaaw_3 = "10000508RRR", - evmhesmiaaw_3 = "10000509RRR", - evmhesmfaaw_3 = "1000050bRRR", - evmhoumiaaw_3 = "1000050cRRR", - evmhosmiaaw_3 = "1000050dRRR", - evmhosmfaaw_3 = "1000050fRRR", - evmhegumiaa_3 = "10000528RRR", - evmhegsmiaa_3 = "10000529RRR", - evmhegsmfaa_3 = "1000052bRRR", - evmhogumiaa_3 = "1000052cRRR", - evmhogsmiaa_3 = "1000052dRRR", - evmhogsmfaa_3 = "1000052fRRR", - evmwlusiaaw_3 = "10000540RRR", - evmwlssiaaw_3 = "10000541RRR", - evmwlumiaaw_3 = "10000548RRR", - evmwlsmiaaw_3 = "10000549RRR", - evmheusianw_3 = "10000580RRR", - evmhessianw_3 = "10000581RRR", - evmhessfanw_3 = "10000583RRR", - evmhousianw_3 = "10000584RRR", - evmhossianw_3 = "10000585RRR", - evmhossfanw_3 = "10000587RRR", - evmheumianw_3 = "10000588RRR", - evmhesmianw_3 = "10000589RRR", - evmhesmfanw_3 = "1000058bRRR", - evmhoumianw_3 = "1000058cRRR", - evmhosmianw_3 = "1000058dRRR", - evmhosmfanw_3 = "1000058fRRR", - evmhegumian_3 = "100005a8RRR", - evmhegsmian_3 = "100005a9RRR", - evmhegsmfan_3 = "100005abRRR", - evmhogumian_3 = "100005acRRR", - evmhogsmian_3 = "100005adRRR", - evmhogsmfan_3 = "100005afRRR", - evmwlusianw_3 = "100005c0RRR", - evmwlssianw_3 = "100005c1RRR", - evmwlumianw_3 = "100005c8RRR", - evmwlsmianw_3 = "100005c9RRR", - - -- NYI: Book E instructions. -} - --- Add mnemonics for "." variants. -do - local t = {} - for k,v in pairs(map_op) do - if sub(v, -1) == "." then - local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2) - t[sub(k, 1, -3).."."..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - --- Add more branch mnemonics. -for cond,c in pairs(map_cond) do - local b1 = "b"..cond - local c1 = shl(band(c, 3), 16) + (c < 4 and 0x01000000 or 0) - -- bX[l] - map_op[b1.."_1"] = tohex(0x40800000 + c1).."K" - map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K" - map_op[b1.."l_1"] = tohex(0x40800001 + c1).."K" - map_op[b1.."_2"] = tohex(0x40800000 + c1).."-XK" - map_op[b1.."y_2"] = tohex(0x40a00000 + c1).."-XK" - map_op[b1.."l_2"] = tohex(0x40800001 + c1).."-XK" - -- bXlr[l] - map_op[b1.."lr_0"] = tohex(0x4c800020 + c1) - map_op[b1.."lrl_0"] = tohex(0x4c800021 + c1) - map_op[b1.."ctr_0"] = tohex(0x4c800420 + c1) - map_op[b1.."ctrl_0"] = tohex(0x4c800421 + c1) - -- bXctr[l] - map_op[b1.."lr_1"] = tohex(0x4c800020 + c1).."-X" - map_op[b1.."lrl_1"] = tohex(0x4c800021 + c1).."-X" - map_op[b1.."ctr_1"] = tohex(0x4c800420 + c1).."-X" - map_op[b1.."ctrl_1"] = tohex(0x4c800421 + c1).."-X" -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_cr(expr) - local r = match(expr, "^cr([0-7])$") - if r then return tonumber(r) end - werror("bad condition register name `"..expr.."'") -end - -local function parse_cond(expr) - local r, cond = match(expr, "^4%*cr([0-7])%+(%w%w)$") - if r then - r = tonumber(r) - local c = map_cond[cond] - if c and c < 4 then return r*4+c end - end - werror("bad condition bit name `"..expr.."'") -end - -local function parse_imm(imm, bits, shift, scale, signed) - local n = tonumber(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n then - if signed then - local s = sar(m, bits-1) - if s == 0 then return shl(m, shift) - elseif s == -1 then return shl(m + shl(1, bits), shift) end - else - if sar(m, bits) == 0 then return shl(m, shift) end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^r([1-3]?[0-9])$") or - match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_shiftmask(imm, isshift) - local n = tonumber(imm) - if n then - if shr(n, 6) == 0 then - local lsb = band(imm, 31) - local msb = imm - lsb - return isshift and (shl(lsb, 11)+shr(msb, 4)) or (shl(lsb, 6)+msb) - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^r([1-3]?[0-9])$") or - match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - werror("NYI: parameterized 64 bit shift/mask") - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return shl(r, 16) + parse_imm(imm, 16, 0, 0, true) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return shl(r, 16) - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_u5disp(disp, scale) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return shl(r, 16) + parse_imm(imm, 5, 11, scale, false) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr)) - return shl(r, 16) - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n, rs = 1, 26 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions (rlwinm). - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "R" then - rs = rs - 5; op = op + shl(parse_gpr(params[n]), rs); n = n + 1 - elseif p == "F" then - rs = rs - 5; op = op + shl(parse_fpr(params[n]), rs); n = n + 1 - elseif p == "A" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 - elseif p == "S" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, true); n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "D" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "2" then - op = op + parse_u5disp(params[n], 1); n = n + 1 - elseif p == "4" then - op = op + parse_u5disp(params[n], 2); n = n + 1 - elseif p == "8" then - op = op + parse_u5disp(params[n], 3); n = n + 1 - elseif p == "C" then - rs = rs - 5; op = op + shl(parse_cond(params[n]), rs); n = n + 1 - elseif p == "X" then - rs = rs - 5; op = op + shl(parse_cr(params[n]), rs+2); n = n + 1 - elseif p == "W" then - op = op + parse_cr(params[n]); n = n + 1 - elseif p == "G" then - op = op + parse_imm(params[n], 8, 12, 0, false); n = n + 1 - elseif p == "H" then - op = op + parse_shiftmask(params[n], true); n = n + 1 - elseif p == "M" then - op = op + parse_shiftmask(params[n], false); n = n + 1 - elseif p == "J" or p == "K" then - local mode, n, s = parse_label(params[n], false) - if p == "K" then n = n + 2048 end - waction("REL_"..mode, n, s, 1) - n = n + 1 - elseif p == "0" then - if band(shr(op, rs), 31) == 0 then werror("cannot use r0") end - elseif p == "=" or p == "%" then - local t = band(shr(op, p == "%" and rs+5 or rs), 31) - rs = rs - 5 - op = op + shl(t, rs) - elseif p == "~" then - local mm = shl(31, rs) - local lo = band(op, mm) - local hi = band(op, shl(mm, 5)) - op = op - lo - hi + shl(lo, 5) + shr(hi, 5) - elseif p == "-" then - rs = rs - 5 - elseif p == "." then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_proto.h b/third-party/LuaJIT-2.0.2/dynasm/dasm_proto.h deleted file mode 100644 index 960ddb72ec..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_proto.h +++ /dev/null @@ -1,83 +0,0 @@ -/* -** DynASM encoding engine prototypes. -** Copyright (C) 2005-2013 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#ifndef _DASM_PROTO_H -#define _DASM_PROTO_H - -#include -#include - -#define DASM_IDENT "DynASM 1.3.0" -#define DASM_VERSION 10300 /* 1.3.0 */ - -#ifndef Dst_DECL -#define Dst_DECL dasm_State **Dst -#endif - -#ifndef Dst_REF -#define Dst_REF (*Dst) -#endif - -#ifndef DASM_FDEF -#define DASM_FDEF extern -#endif - -#ifndef DASM_M_GROW -#define DASM_M_GROW(ctx, t, p, sz, need) \ - do { \ - size_t _sz = (sz), _need = (need); \ - if (_sz < _need) { \ - if (_sz < 16) _sz = 16; \ - while (_sz < _need) _sz += _sz; \ - (p) = (t *)realloc((p), _sz); \ - if ((p) == NULL) exit(1); \ - (sz) = _sz; \ - } \ - } while(0) -#endif - -#ifndef DASM_M_FREE -#define DASM_M_FREE(ctx, p, sz) free(p) -#endif - -/* Internal DynASM encoder state. */ -typedef struct dasm_State dasm_State; - - -/* Initialize and free DynASM state. */ -DASM_FDEF void dasm_init(Dst_DECL, int maxsection); -DASM_FDEF void dasm_free(Dst_DECL); - -/* Setup global array. Must be called before dasm_setup(). */ -DASM_FDEF void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl); - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -DASM_FDEF void dasm_growpc(Dst_DECL, unsigned int maxpc); - -/* Setup encoder. */ -DASM_FDEF void dasm_setup(Dst_DECL, const void *actionlist); - -/* Feed encoder with actions. Calls are generated by pre-processor. */ -DASM_FDEF void dasm_put(Dst_DECL, int start, ...); - -/* Link sections and return the resulting size. */ -DASM_FDEF int dasm_link(Dst_DECL, size_t *szp); - -/* Encode sections into buffer. */ -DASM_FDEF int dasm_encode(Dst_DECL, void *buffer); - -/* Get PC label offset. */ -DASM_FDEF int dasm_getpclabel(Dst_DECL, unsigned int pc); - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -DASM_FDEF int dasm_checkstep(Dst_DECL, int secmatch); -#else -#define dasm_checkstep(a, b) 0 -#endif - - -#endif /* _DASM_PROTO_H */ diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_x64.lua b/third-party/LuaJIT-2.0.2/dynasm/dasm_x64.lua deleted file mode 100644 index ca25823c0d..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_x64.lua +++ /dev/null @@ -1,12 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x64 module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- --- This module just sets 64 bit mode for the combined x86/x64 module. --- All the interesting stuff is there. ------------------------------------------------------------------------------- - -x64 = true -- Using a global is an ugly, but effective solution. -return require("dasm_x86") diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_x86.h b/third-party/LuaJIT-2.0.2/dynasm/dasm_x86.h deleted file mode 100644 index c91bfdd53f..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_x86.h +++ /dev/null @@ -1,471 +0,0 @@ -/* -** DynASM x86 encoding engine. -** Copyright (C) 2005-2013 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "x86" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. DASM_STOP must be 255. */ -enum { - DASM_DISP = 233, - DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB, - DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC, - DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN, - DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_VREG 0x15000000 -#define DASM_S_UNDEF_L 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned char *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs, mrm = 4; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - int action = *p++; - if (action < DASM_DISP) { - ofs++; - } else if (action <= DASM_REL_A) { - int n = va_arg(ap, int); - b[pos++] = n; - switch (action) { - case DASM_DISP: - if (n == 0) { if ((mrm&7) == 4) mrm = p[-2]; if ((mrm&7) != 5) break; } - case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; - case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */ - case DASM_IMM_D: ofs += 4; break; - case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob; - case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; - case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break; - case DASM_SPACE: p++; ofs += n; break; - case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */ - case DASM_VREG: CK((n&-8) == 0 && (n != 4 || (*p&1) == 0), RANGE_VREG); - if (*p++ == 1 && *p == DASM_DISP) mrm = n; continue; - } - mrm = 4; - } else { - int *pl, n; - switch (action) { - case DASM_REL_LG: - case DASM_IMM_LG: - n = *p++; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n <= 246) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl -= 246; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - ofs += 4; /* Maximum offset needed. */ - if (action == DASM_REL_LG || action == DASM_REL_PC) - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_ALIGN: - ofs += *p++; /* Maximum alignment needed (arg is 2**n-1). */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_EXTERN: p += 2; ofs += 4; break; - case DASM_ESC: p++; ofs++; break; - case DASM_MARK: mrm = p[-2]; break; - case DASM_SECTION: - n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n]; - case DASM_STOP: goto stop; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - int op, action = *p++; - switch (action) { - case DASM_REL_LG: p++; op = p[-3]; goto rel_pc; - case DASM_REL_PC: op = p[-2]; rel_pc: { - int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0); - if (shrink) { /* Shrinkable branch opcode? */ - int lofs, lpos = b[pos]; - if (lpos < 0) goto noshrink; /* Ext global? */ - lofs = *DASM_POS2PTR(D, lpos); - if (lpos > pos) { /* Fwd label: add cumulative section offsets. */ - int i; - for (i = secnum; i < DASM_POS2SEC(lpos); i++) - lofs += D->sections[i].ofs; - } else { - lofs -= ofs; /* Bkwd label: unfix offset. */ - } - lofs -= b[pos+1]; /* Short branch ok? */ - if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink; /* Yes. */ - else { noshrink: shrink = 0; } /* No, cannot shrink op. */ - } - b[pos+1] = shrink; - pos += 2; - break; - } - case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++; - case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W: - case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB: - case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break; - case DASM_LABEL_LG: p++; - case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */ - case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */ - case DASM_EXTERN: p += 2; break; - case DASM_ESC: p++; break; - case DASM_MARK: break; - case DASM_SECTION: case DASM_STOP: goto stop; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#define dasmb(x) *cp++ = (unsigned char)(x) -#ifndef DASM_ALIGNED_WRITES -#define dasmw(x) \ - do { *((unsigned short *)cp) = (unsigned short)(x); cp+=2; } while (0) -#define dasmd(x) \ - do { *((unsigned int *)cp) = (unsigned int)(x); cp+=4; } while (0) -#else -#define dasmw(x) do { dasmb(x); dasmb((x)>>8); } while (0) -#define dasmd(x) do { dasmw(x); dasmw((x)>>16); } while (0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - unsigned char *base = (unsigned char *)buffer; - unsigned char *cp = base; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - unsigned char *mark = NULL; - while (1) { - int action = *p++; - int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0; - switch (action) { - case DASM_DISP: if (!mark) mark = cp; { - unsigned char *mm = mark; - if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL; - if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7; - if (mrm != 5) { mm[-1] -= 0x80; break; } } - if (((n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40; - } - case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break; - case DASM_IMM_DB: if (((n+128)&-256) == 0) { - db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb; - } else mark = NULL; - case DASM_IMM_D: wd: dasmd(n); break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL; - case DASM_IMM_W: dasmw(n); break; - case DASM_VREG: { int t = *p++; if (t >= 2) n<<=3; cp[-1] |= n; break; } - case DASM_REL_LG: p++; if (n >= 0) goto rel_pc; - b++; n = (int)(ptrdiff_t)D->globals[-n]; - case DASM_REL_A: rel_a: n -= (int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */ - case DASM_REL_PC: rel_pc: { - int shrink = *b++; - int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; } - n = *pb - ((int)(cp-base) + 4-shrink); - if (shrink == 0) goto wd; - if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb; - goto wb; - } - case DASM_IMM_LG: - p++; if (n < 0) { n = (int)(ptrdiff_t)D->globals[-n]; goto wd; } - case DASM_IMM_PC: { - int *pb = DASM_POS2PTR(D, n); - n = *pb < 0 ? pb[1] : (*pb + (int)(ptrdiff_t)base); - goto wd; - } - case DASM_LABEL_LG: { - int idx = *p++; - if (idx >= 10) - D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n)); - break; - } - case DASM_LABEL_PC: case DASM_SETLABEL: break; - case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; } - case DASM_ALIGN: - n = *p++; - while (((cp-base) & n)) *cp++ = 0x90; /* nop */ - break; - case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd; - case DASM_MARK: mark = cp; break; - case DASM_ESC: action = *p++; - default: *cp++ = action; break; - case DASM_SECTION: case DASM_STOP: goto stop; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dasm_x86.lua b/third-party/LuaJIT-2.0.2/dynasm/dasm_x86.lua deleted file mode 100644 index 1f0981f326..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dasm_x86.lua +++ /dev/null @@ -1,1931 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x86/x64 module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - -local x64 = x64 - --- Module information: -local _info = { - arch = x64 and "x64" or "x86", - description = "DynASM x86/x64 module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, unpack, setmetatable = assert, unpack or table.unpack, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub -local concat, sort = table.concat, table.sort -local bit = bit or require("bit") -local band, shl, shr = bit.band, bit.lshift, bit.rshift - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - -- int arg, 1 buffer pos: - "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB", - -- action arg (1 byte), int arg, 1 buffer pos (reg/num): - "VREG", "SPACE", -- !x64: VREG support NYI. - -- ptrdiff_t arg, 1 buffer pos (address): !x64 - "SETLABEL", "REL_A", - -- action arg (1 byte) or int arg, 2 buffer pos (link, offset): - "REL_LG", "REL_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (link): - "IMM_LG", "IMM_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (offset): - "LABEL_LG", "LABEL_PC", - -- action arg (1 byte), 1 buffer pos (offset): - "ALIGN", - -- action args (2 bytes), no buffer pos. - "EXTERN", - -- action arg (1 byte), no buffer pos. - "ESC", - -- no action arg, no buffer pos. - "MARK", - -- action arg (1 byte), no buffer pos, terminal action: - "SECTION", - -- no args, no buffer pos, terminal action: - "STOP" -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number (dynamically generated below). -local map_action = {} --- First action number. Everything below does not need to be escaped. -local actfirst = 256-#action_names - --- Action list buffer and string (only used to remove dupes). -local actlist = {} -local actstr = "" - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Compute action numbers for action names. -for n,name in ipairs(action_names) do - local num = actfirst + n - 1 - map_action[name] = num -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - local last = actlist[nn] or 255 - actlist[nn] = nil -- Remove last byte. - if nn == 0 then nn = 1 end - out:write("static const unsigned char ", name, "[", nn, "] = {\n") - local s = " " - for n,b in ipairs(actlist) do - s = s..b.."," - if #s >= 75 then - assert(out:write(s, "\n")) - s = " " - end - end - out:write(s, last, "\n};\n\n") -- Add last byte back. -end - ------------------------------------------------------------------------------- - --- Add byte to action list. -local function wputxb(n) - assert(n >= 0 and n <= 255 and n % 1 == 0, "byte out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, a, num) - wputxb(assert(map_action[action], "bad action name `"..action.."'")) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Add call to embedded DynASM C code. -local function wcall(func, args) - wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true) -end - --- Delete duplicate action list chunks. A tad slow, but so what. -local function dedupechunk(offset) - local al, as = actlist, actstr - local chunk = char(unpack(al, offset+1, #al)) - local orig = find(as, chunk, 1, true) - if orig then - actargs[1] = orig-1 -- Replace with original offset. - for i=offset+1,#al do al[i] = nil end -- Kill dupe. - else - actstr = as..chunk - end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - local offset = actargs[1] - if #actlist == offset then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - dedupechunk(offset) - wcall("put", actargs) -- Add call to dasm_put(). - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped byte. -local function wputb(n) - if n >= actfirst then waction("ESC") end -- Need to escape byte. - wputxb(n) -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 10 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_@]*$") then werror("bad global label") end - local n = next_global - if n > 246 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=10,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=10,next_global-1 do - out:write(" ", prefix, gsub(t[i], "@.*", ""), ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=10,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = -1 -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n < -256 then werror("too many extern labels") end - next_extern = n - 1 - t[name] = n - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("Extern labels:\n") - for i=1,-next_extern-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=1,-next_extern-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = {} -- Ext. register name -> int. name. -local map_reg_rev = {} -- Int. register name -> ext. name. -local map_reg_num = {} -- Int. register name -> register number. -local map_reg_opsize = {} -- Int. register name -> operand size. -local map_reg_valid_base = {} -- Int. register name -> valid base register? -local map_reg_valid_index = {} -- Int. register name -> valid index register? -local map_reg_needrex = {} -- Int. register name -> need rex vs. no rex. -local reg_list = {} -- Canonical list of int. register names. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for _PTx macros). - -local addrsize = x64 and "q" or "d" -- Size for address operands. - --- Helper functions to fill register maps. -local function mkrmap(sz, cl, names) - local cname = format("@%s", sz) - reg_list[#reg_list+1] = cname - map_archdef[cl] = cname - map_reg_rev[cname] = cl - map_reg_num[cname] = -1 - map_reg_opsize[cname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[cname] = true - map_reg_valid_index[cname] = true - end - if names then - for n,name in ipairs(names) do - local iname = format("@%s%x", sz, n-1) - reg_list[#reg_list+1] = iname - map_archdef[name] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = n-1 - map_reg_opsize[iname] = sz - if sz == "b" and n > 4 then map_reg_needrex[iname] = false end - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - for i=0,(x64 and sz ~= "f") and 15 or 7 do - local needrex = sz == "b" and i > 3 - local iname = format("@%s%x%s", sz, i, needrex and "R" or "") - if needrex then map_reg_needrex[iname] = true end - local name - if sz == "o" then name = format("xmm%d", i) - elseif sz == "f" then name = format("st%d", i) - else name = format("r%d%s", i, sz == addrsize and "" or sz) end - map_archdef[name] = iname - if not map_reg_rev[iname] then - reg_list[#reg_list+1] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = i - map_reg_opsize[iname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - reg_list[#reg_list+1] = "" -end - --- Integer registers (qword, dword, word and byte sized). -if x64 then - mkrmap("q", "Rq", {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi"}) -end -mkrmap("d", "Rd", {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}) -mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"}) -mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"}) -map_reg_valid_index[map_archdef.esp] = false -if x64 then map_reg_valid_index[map_archdef.rsp] = false end -map_archdef["Ra"] = "@"..addrsize - --- FP registers (internally tword sized, but use "f" as operand size). -mkrmap("f", "Rf") - --- SSE registers (oword sized, but qword and dword accessible). -mkrmap("o", "xmm") - --- Operand size prefixes to codes. -local map_opsize = { - byte = "b", word = "w", dword = "d", qword = "q", oword = "o", tword = "t", - aword = addrsize, -} - --- Operand size code to number. -local map_opsizenum = { - b = 1, w = 2, d = 4, q = 8, o = 16, t = 10, -} - --- Operand size code to name. -local map_opsizename = { - b = "byte", w = "word", d = "dword", q = "qword", o = "oword", t = "tword", - f = "fpword", -} - --- Valid index register scale factors. -local map_xsc = { - ["1"] = 0, ["2"] = 1, ["4"] = 2, ["8"] = 3, -} - --- Condition codes. -local map_cc = { - o = 0, no = 1, b = 2, nb = 3, e = 4, ne = 5, be = 6, nbe = 7, - s = 8, ns = 9, p = 10, np = 11, l = 12, nl = 13, le = 14, nle = 15, - c = 2, nae = 2, nc = 3, ae = 3, z = 4, nz = 5, na = 6, a = 7, - pe = 10, po = 11, nge = 12, ge = 13, ng = 14, g = 15, -} - - --- Reverse defines for registers. -function _M.revdef(s) - return gsub(s, "@%w+", map_reg_rev) -end - --- Dump register names and numbers -local function dumpregs(out) - out:write("Register names, sizes and internal numbers:\n") - for _,reg in ipairs(reg_list) do - if reg == "" then - out:write("\n") - else - local name = map_reg_rev[reg] - local num = map_reg_num[reg] - local opsize = map_opsizename[map_reg_opsize[reg]] - out:write(format(" %-5s %-8s %s\n", name, opsize, - num < 0 and "(variable)" or num)) - end - end -end - ------------------------------------------------------------------------------- - --- Put action for label arg (IMM_LG, IMM_PC, REL_LG, REL_PC). -local function wputlabel(aprefix, imm, num) - if type(imm) == "number" then - if imm < 0 then - waction("EXTERN") - wputxb(aprefix == "IMM_" and 0 or 1) - imm = -imm-1 - else - waction(aprefix.."LG", nil, num); - end - wputxb(imm) - else - waction(aprefix.."PC", imm, num) - end -end - --- Put signed byte or arg. -local function wputsbarg(n) - if type(n) == "number" then - if n < -128 or n > 127 then - werror("signed immediate byte out of range") - end - if n < 0 then n = n + 256 end - wputb(n) - else waction("IMM_S", n) end -end - --- Put unsigned byte or arg. -local function wputbarg(n) - if type(n) == "number" then - if n < 0 or n > 255 then - werror("unsigned immediate byte out of range") - end - wputb(n) - else waction("IMM_B", n) end -end - --- Put unsigned word or arg. -local function wputwarg(n) - if type(n) == "number" then - if shr(n, 16) ~= 0 then - werror("unsigned immediate word out of range") - end - wputb(band(n, 255)); wputb(shr(n, 8)); - else waction("IMM_W", n) end -end - --- Put signed or unsigned dword or arg. -local function wputdarg(n) - local tn = type(n) - if tn == "number" then - wputb(band(n, 255)) - wputb(band(shr(n, 8), 255)) - wputb(band(shr(n, 16), 255)) - wputb(shr(n, 24)) - elseif tn == "table" then - wputlabel("IMM_", n[1], 1) - else - waction("IMM_D", n) - end -end - --- Put operand-size dependent number or arg (defaults to dword). -local function wputszarg(sz, n) - if not sz or sz == "d" or sz == "q" then wputdarg(n) - elseif sz == "w" then wputwarg(n) - elseif sz == "b" then wputbarg(n) - elseif sz == "s" then wputsbarg(n) - else werror("bad operand size") end -end - --- Put multi-byte opcode with operand-size dependent modifications. -local function wputop(sz, op, rex) - local r - if rex ~= 0 and not x64 then werror("bad operand size") end - if sz == "w" then wputb(102) end - -- Needs >32 bit numbers, but only for crc32 eax, word [ebx] - if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end - if op >= 16777216 then wputb(shr(op, 24)); op = band(op, 0xffffff) end - if op >= 65536 then - if rex ~= 0 then - local opc3 = band(op, 0xffff00) - if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then - wputb(64 + band(rex, 15)); rex = 0 - end - end - wputb(shr(op, 16)); op = band(op, 0xffff) - end - if op >= 256 then - local b = shr(op, 8) - if b == 15 and rex ~= 0 then wputb(64 + band(rex, 15)); rex = 0 end - wputb(b) - op = band(op, 255) - end - if rex ~= 0 then wputb(64 + band(rex, 15)) end - if sz == "b" then op = op - 1 end - wputb(op) -end - --- Put ModRM or SIB formatted byte. -local function wputmodrm(m, s, rm, vs, vrm) - assert(m < 4 and s < 16 and rm < 16, "bad modrm operands") - wputb(shl(m, 6) + shl(band(s, 7), 3) + band(rm, 7)) -end - --- Put ModRM/SIB plus optional displacement. -local function wputmrmsib(t, imark, s, vsreg) - local vreg, vxreg - local reg, xreg = t.reg, t.xreg - if reg and reg < 0 then reg = 0; vreg = t.vreg end - if xreg and xreg < 0 then xreg = 0; vxreg = t.vxreg end - if s < 0 then s = 0 end - - -- Register mode. - if sub(t.mode, 1, 1) == "r" then - wputmodrm(3, s, reg) - if vsreg then waction("VREG", vsreg); wputxb(2) end - if vreg then waction("VREG", vreg); wputxb(0) end - return - end - - local disp = t.disp - local tdisp = type(disp) - -- No base register? - if not reg then - local riprel = false - if xreg then - -- Indexed mode with index register only. - -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp) - wputmodrm(0, s, 4) - if imark == "I" then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - wputmodrm(t.xsc, xreg, 5) - if vxreg then waction("VREG", vxreg); wputxb(3) end - else - -- Pure 32 bit displacement. - if x64 and tdisp ~= "table" then - wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp) - if imark == "I" then waction("MARK") end - wputmodrm(0, 4, 5) - else - riprel = x64 - wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp) - if imark == "I" then waction("MARK") end - end - if vsreg then waction("VREG", vsreg); wputxb(2) end - end - if riprel then -- Emit rip-relative displacement. - if match("UWSiI", imark) then - werror("NYI: rip-relative displacement followed by immediate") - end - -- The previous byte in the action buffer cannot be 0xe9 or 0x80-0x8f. - wputlabel("REL_", disp[1], 2) - else - wputdarg(disp) - end - return - end - - local m - if tdisp == "number" then -- Check displacement size at assembly time. - if disp == 0 and band(reg, 7) ~= 5 then -- [ebp] -> [ebp+0] (in SIB, too) - if not vreg then m = 0 end -- Force DISP to allow [Rd(5)] -> [ebp+0] - elseif disp >= -128 and disp <= 127 then m = 1 - else m = 2 end - elseif tdisp == "table" then - m = 2 - end - - -- Index register present or esp as base register: need SIB encoding. - if xreg or band(reg, 7) == 4 then - wputmodrm(m or 2, s, 4) -- ModRM. - if m == nil or imark == "I" then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB. - if vxreg then waction("VREG", vxreg); wputxb(3) end - if vreg then waction("VREG", vreg); wputxb(1) end - else - wputmodrm(m or 2, s, reg) -- ModRM. - if (imark == "I" and (m == 1 or m == 2)) or - (m == nil and (vsreg or vreg)) then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - if vreg then waction("VREG", vreg); wputxb(1) end - end - - -- Put displacement. - if m == 1 then wputsbarg(disp) - elseif m == 2 then wputdarg(disp) - elseif m == nil then waction("DISP", disp) end -end - ------------------------------------------------------------------------------- - --- Return human-readable operand mode string. -local function opmodestr(op, args) - local m = {} - for i=1,#args do - local a = args[i] - m[#m+1] = sub(a.mode, 1, 1)..(a.opsize or "?") - end - return op.." "..concat(m, ",") -end - --- Convert number to valid integer or nil. -local function toint(expr) - local n = tonumber(expr) - if n then - if n % 1 ~= 0 or n < -2147483648 or n > 4294967295 then - werror("bad integer number `"..expr.."'") - end - return n - end -end - --- Parse immediate expression. -local function immexpr(expr) - -- &expr (pointer) - if sub(expr, 1, 1) == "&" then - return "iPJ", format("(ptrdiff_t)(%s)", sub(expr,2)) - end - - local prefix = sub(expr, 1, 2) - -- =>expr (pc label reference) - if prefix == "=>" then - return "iJ", sub(expr, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "iJ", map_global[sub(expr, 3)] - end - - -- [<>][1-9] (local label reference) - local dir, lnum = match(expr, "^([<>])([1-9])$") - if dir then -- Fwd: 247-255, Bkwd: 1-9. - return "iJ", lnum + (dir == ">" and 246 or 0) - end - - local extname = match(expr, "^extern%s+(%S+)$") - if extname then - return "iJ", map_extern[extname] - end - - -- expr (interpreted as immediate) - return "iI", expr -end - --- Parse displacement expression: +-num, +-expr, +-opsize*num -local function dispexpr(expr) - local disp = expr == "" and 0 or toint(expr) - if disp then return disp end - local c, dispt = match(expr, "^([+-])%s*(.+)$") - if c == "+" then - expr = dispt - elseif not c then - werror("bad displacement expression `"..expr.."'") - end - local opsize, tailops = match(dispt, "^(%w+)%s*%*%s*(.+)$") - local ops, imm = map_opsize[opsize], toint(tailops) - if ops and imm then - if c == "-" then imm = -imm end - return imm*map_opsizenum[ops] - end - local mode, iexpr = immexpr(dispt) - if mode == "iJ" then - if c == "-" then werror("cannot invert label reference") end - return { iexpr } - end - return expr -- Need to return original signed expression. -end - --- Parse register or type expression. -local function rtexpr(expr) - if not expr then return end - local tname, ovreg = match(expr, "^([%w_]+):(@[%w_]+)$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - local rnum = map_reg_num[reg] - if not rnum then - werror("type `"..(tname or expr).."' needs a register override") - end - if not map_reg_valid_base[reg] then - werror("bad base register override `"..(map_reg_rev[reg] or reg).."'") - end - return reg, rnum, tp - end - return expr, map_reg_num[expr] -end - --- Parse operand and return { mode, opsize, reg, xreg, xsc, disp, imm }. -local function parseoperand(param) - local t = {} - - local expr = param - local opsize, tailops = match(param, "^(%w+)%s*(.+)$") - if opsize then - t.opsize = map_opsize[opsize] - if t.opsize then expr = tailops end - end - - local br = match(expr, "^%[%s*(.-)%s*%]$") - repeat - if br then - t.mode = "xm" - - -- [disp] - t.disp = toint(br) - if t.disp then - t.mode = x64 and "xm" or "xmO" - break - end - - -- [reg...] - local tp - local reg, tailr = match(br, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if not t.reg then - -- [expr] - t.mode = x64 and "xm" or "xmO" - t.disp = dispexpr("+"..br) - break - end - - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - - -- [xreg*xsc] or [xreg*xsc+-disp] or [xreg*xsc+-expr] - local xsc, tailsc = match(tailr, "^%*%s*([1248])%s*(.*)$") - if xsc then - if not map_reg_valid_index[reg] then - werror("bad index register `"..map_reg_rev[reg].."'") - end - t.xsc = map_xsc[xsc] - t.xreg = t.reg - t.vxreg = t.vreg - t.reg = nil - t.vreg = nil - t.disp = dispexpr(tailsc) - break - end - if not map_reg_valid_base[reg] then - werror("bad base register `"..map_reg_rev[reg].."'") - end - - -- [reg] or [reg+-disp] - t.disp = toint(tailr) or (tailr == "" and 0) - if t.disp then break end - - -- [reg+xreg...] - local xreg, tailx = match(tailr, "^+%s*([@%w_:]+)%s*(.*)$") - xreg, t.xreg, tp = rtexpr(xreg) - if not t.xreg then - -- [reg+-expr] - t.disp = dispexpr(tailr) - break - end - if not map_reg_valid_index[xreg] then - werror("bad index register `"..map_reg_rev[xreg].."'") - end - - if t.xreg == -1 then - t.vxreg, tailx = match(tailx, "^(%b())(.*)$") - if not t.vxreg then werror("bad variable register expression") end - end - - -- [reg+xreg*xsc...] - local xsc, tailsc = match(tailx, "^%*%s*([1248])%s*(.*)$") - if xsc then - t.xsc = map_xsc[xsc] - tailx = tailsc - end - - -- [...] or [...+-disp] or [...+-expr] - t.disp = dispexpr(tailx) - else - -- imm or opsize*imm - local imm = toint(expr) - if not imm and sub(expr, 1, 1) == "*" and t.opsize then - imm = toint(sub(expr, 2)) - if imm then - imm = imm * map_opsizenum[t.opsize] - t.opsize = nil - end - end - if imm then - if t.opsize then werror("bad operand size override") end - local m = "i" - if imm == 1 then m = m.."1" end - if imm >= 4294967168 and imm <= 4294967295 then imm = imm-4294967296 end - if imm >= -128 and imm <= 127 then m = m.."S" end - t.imm = imm - t.mode = m - break - end - - local tp - local reg, tailr = match(expr, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if t.reg then - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - -- reg - if tailr == "" then - if t.opsize then werror("bad operand size override") end - t.opsize = map_reg_opsize[reg] - if t.opsize == "f" then - t.mode = t.reg == 0 and "fF" or "f" - else - if reg == "@w4" or (x64 and reg == "@d4") then - wwarn("bad idea, try again with `"..(x64 and "rsp'" or "esp'")) - end - t.mode = t.reg == 0 and "rmR" or (reg == "@b1" and "rmC" or "rm") - end - t.needrex = map_reg_needrex[reg] - break - end - - -- type[idx], type[idx].field, type->field -> [reg+offset_expr] - if not tp then werror("bad operand `"..param.."'") end - t.mode = "xm" - t.disp = format(tp.ctypefmt, tailr) - else - t.mode, t.imm = immexpr(expr) - if sub(t.mode, -1) == "J" then - if t.opsize and t.opsize ~= addrsize then - werror("bad operand size override") - end - t.opsize = addrsize - end - end - end - until true - return t -end - ------------------------------------------------------------------------------- --- x86 Template String Description --- =============================== --- --- Each template string is a list of [match:]pattern pairs, --- separated by "|". The first match wins. No match means a --- bad or unsupported combination of operand modes or sizes. --- --- The match part and the ":" is omitted if the operation has --- no operands. Otherwise the first N characters are matched --- against the mode strings of each of the N operands. --- --- The mode string for each operand type is (see parseoperand()): --- Integer register: "rm", +"R" for eax, ax, al, +"C" for cl --- FP register: "f", +"F" for st0 --- Index operand: "xm", +"O" for [disp] (pure offset) --- Immediate: "i", +"S" for signed 8 bit, +"1" for 1, --- +"I" for arg, +"P" for pointer --- Any: +"J" for valid jump targets --- --- So a match character "m" (mixed) matches both an integer register --- and an index operand (to be encoded with the ModRM/SIB scheme). --- But "r" matches only a register and "x" only an index operand --- (e.g. for FP memory access operations). --- --- The operand size match string starts right after the mode match --- characters and ends before the ":". "dwb" or "qdwb" is assumed, if empty. --- The effective data size of the operation is matched against this list. --- --- If only the regular "b", "w", "d", "q", "t" operand sizes are --- present, then all operands must be the same size. Unspecified sizes --- are ignored, but at least one operand must have a size or the pattern --- won't match (use the "byte", "word", "dword", "qword", "tword" --- operand size overrides. E.g.: mov dword [eax], 1). --- --- If the list has a "1" or "2" prefix, the operand size is taken --- from the respective operand and any other operand sizes are ignored. --- If the list contains only ".", all operand sizes are ignored. --- If the list has a "/" prefix, the concatenated (mixed) operand sizes --- are compared to the match. --- --- E.g. "rrdw" matches for either two dword registers or two word --- registers. "Fx2dq" matches an st0 operand plus an index operand --- pointing to a dword (float) or qword (double). --- --- Every character after the ":" is part of the pattern string: --- Hex chars are accumulated to form the opcode (left to right). --- "n" disables the standard opcode mods --- (otherwise: -1 for "b", o16 prefix for "w", rex.w for "q") --- "X" Force REX.W. --- "r"/"R" adds the reg. number from the 1st/2nd operand to the opcode. --- "m"/"M" generates ModRM/SIB from the 1st/2nd operand. --- The spare 3 bits are either filled with the last hex digit or --- the result from a previous "r"/"R". The opcode is restored. --- --- All of the following characters force a flush of the opcode: --- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand. --- "S" stores a signed 8 bit immediate from the last operand. --- "U" stores an unsigned 8 bit immediate from the last operand. --- "W" stores an unsigned 16 bit immediate from the last operand. --- "i" stores an operand sized immediate from the last operand. --- "I" dito, but generates an action code to optionally modify --- the opcode (+2) for a signed 8 bit immediate. --- "J" generates one of the REL action codes from the last operand. --- ------------------------------------------------------------------------------- - --- Template strings for x86 instructions. Ordered by first opcode byte. --- Unimplemented opcodes (deliberate omissions) are marked with *. -local map_op = { - -- 00-05: add... - -- 06: *push es - -- 07: *pop es - -- 08-0D: or... - -- 0E: *push cs - -- 0F: two byte opcode prefix - -- 10-15: adc... - -- 16: *push ss - -- 17: *pop ss - -- 18-1D: sbb... - -- 1E: *push ds - -- 1F: *pop ds - -- 20-25: and... - es_0 = "26", - -- 27: *daa - -- 28-2D: sub... - cs_0 = "2E", - -- 2F: *das - -- 30-35: xor... - ss_0 = "36", - -- 37: *aaa - -- 38-3D: cmp... - ds_0 = "3E", - -- 3F: *aas - inc_1 = x64 and "m:FF0m" or "rdw:40r|m:FF0m", - dec_1 = x64 and "m:FF1m" or "rdw:48r|m:FF1m", - push_1 = (x64 and "rq:n50r|rw:50r|mq:nFF6m|mw:FF6m" or - "rdw:50r|mdw:FF6m").."|S.:6AS|ib:n6Ai|i.:68i", - pop_1 = x64 and "rq:n58r|rw:58r|mq:n8F0m|mw:8F0m" or "rdw:58r|mdw:8F0m", - -- 60: *pusha, *pushad, *pushaw - -- 61: *popa, *popad, *popaw - -- 62: *bound rdw,x - -- 63: x86: *arpl mw,rw - movsxd_2 = x64 and "rm/qd:63rM", - fs_0 = "64", - gs_0 = "65", - o16_0 = "66", - a16_0 = not x64 and "67" or nil, - a32_0 = x64 and "67", - -- 68: push idw - -- 69: imul rdw,mdw,idw - -- 6A: push ib - -- 6B: imul rdw,mdw,S - -- 6C: *insb - -- 6D: *insd, *insw - -- 6E: *outsb - -- 6F: *outsd, *outsw - -- 70-7F: jcc lb - -- 80: add... mb,i - -- 81: add... mdw,i - -- 82: *undefined - -- 83: add... mdw,S - test_2 = "mr:85Rm|rm:85rM|Ri:A9ri|mi:F70mi", - -- 86: xchg rb,mb - -- 87: xchg rdw,mdw - -- 88: mov mb,r - -- 89: mov mdw,r - -- 8A: mov r,mb - -- 8B: mov r,mdw - -- 8C: *mov mdw,seg - lea_2 = "rx1dq:8DrM", - -- 8E: *mov seg,mdw - -- 8F: pop mdw - nop_0 = "90", - xchg_2 = "Rrqdw:90R|rRqdw:90r|rm:87rM|mr:87Rm", - cbw_0 = "6698", - cwde_0 = "98", - cdqe_0 = "4898", - cwd_0 = "6699", - cdq_0 = "99", - cqo_0 = "4899", - -- 9A: *call iw:idw - wait_0 = "9B", - fwait_0 = "9B", - pushf_0 = "9C", - pushfd_0 = not x64 and "9C", - pushfq_0 = x64 and "9C", - popf_0 = "9D", - popfd_0 = not x64 and "9D", - popfq_0 = x64 and "9D", - sahf_0 = "9E", - lahf_0 = "9F", - mov_2 = "OR:A3o|RO:A1O|mr:89Rm|rm:8BrM|rib:nB0ri|ridw:B8ri|mi:C70mi", - movsb_0 = "A4", - movsw_0 = "66A5", - movsd_0 = "A5", - cmpsb_0 = "A6", - cmpsw_0 = "66A7", - cmpsd_0 = "A7", - -- A8: test Rb,i - -- A9: test Rdw,i - stosb_0 = "AA", - stosw_0 = "66AB", - stosd_0 = "AB", - lodsb_0 = "AC", - lodsw_0 = "66AD", - lodsd_0 = "AD", - scasb_0 = "AE", - scasw_0 = "66AF", - scasd_0 = "AF", - -- B0-B7: mov rb,i - -- B8-BF: mov rdw,i - -- C0: rol... mb,i - -- C1: rol... mdw,i - ret_1 = "i.:nC2W", - ret_0 = "C3", - -- C4: *les rdw,mq - -- C5: *lds rdw,mq - -- C6: mov mb,i - -- C7: mov mdw,i - -- C8: *enter iw,ib - leave_0 = "C9", - -- CA: *retf iw - -- CB: *retf - int3_0 = "CC", - int_1 = "i.:nCDU", - into_0 = "CE", - -- CF: *iret - -- D0: rol... mb,1 - -- D1: rol... mdw,1 - -- D2: rol... mb,cl - -- D3: rol... mb,cl - -- D4: *aam ib - -- D5: *aad ib - -- D6: *salc - -- D7: *xlat - -- D8-DF: floating point ops - -- E0: *loopne - -- E1: *loope - -- E2: *loop - -- E3: *jcxz, *jecxz - -- E4: *in Rb,ib - -- E5: *in Rdw,ib - -- E6: *out ib,Rb - -- E7: *out ib,Rdw - call_1 = x64 and "mq:nFF2m|J.:E8nJ" or "md:FF2m|J.:E8J", - jmp_1 = x64 and "mq:nFF4m|J.:E9nJ" or "md:FF4m|J.:E9J", -- short: EB - -- EA: *jmp iw:idw - -- EB: jmp ib - -- EC: *in Rb,dx - -- ED: *in Rdw,dx - -- EE: *out dx,Rb - -- EF: *out dx,Rdw - -- F0: *lock - int1_0 = "F1", - repne_0 = "F2", - repnz_0 = "F2", - rep_0 = "F3", - repe_0 = "F3", - repz_0 = "F3", - -- F4: *hlt - cmc_0 = "F5", - -- F6: test... mb,i; div... mb - -- F7: test... mdw,i; div... mdw - clc_0 = "F8", - stc_0 = "F9", - -- FA: *cli - cld_0 = "FC", - std_0 = "FD", - -- FE: inc... mb - -- FF: inc... mdw - - -- misc ops - not_1 = "m:F72m", - neg_1 = "m:F73m", - mul_1 = "m:F74m", - imul_1 = "m:F75m", - div_1 = "m:F76m", - idiv_1 = "m:F77m", - - imul_2 = "rmqdw:0FAFrM|rIqdw:69rmI|rSqdw:6BrmS|riqdw:69rmi", - imul_3 = "rmIqdw:69rMI|rmSqdw:6BrMS|rmiqdw:69rMi", - - movzx_2 = "rm/db:0FB6rM|rm/qb:|rm/wb:0FB6rM|rm/dw:0FB7rM|rm/qw:", - movsx_2 = "rm/db:0FBErM|rm/qb:|rm/wb:0FBErM|rm/dw:0FBFrM|rm/qw:", - - bswap_1 = "rqd:0FC8r", - bsf_2 = "rmqdw:0FBCrM", - bsr_2 = "rmqdw:0FBDrM", - bt_2 = "mrqdw:0FA3Rm|miqdw:0FBA4mU", - btc_2 = "mrqdw:0FBBRm|miqdw:0FBA7mU", - btr_2 = "mrqdw:0FB3Rm|miqdw:0FBA6mU", - bts_2 = "mrqdw:0FABRm|miqdw:0FBA5mU", - - rdtsc_0 = "0F31", -- P1+ - cpuid_0 = "0FA2", -- P1+ - - -- floating point ops - fst_1 = "ff:DDD0r|xd:D92m|xq:nDD2m", - fstp_1 = "ff:DDD8r|xd:D93m|xq:nDD3m|xt:DB7m", - fld_1 = "ff:D9C0r|xd:D90m|xq:nDD0m|xt:DB5m", - - fpop_0 = "DDD8", -- Alias for fstp st0. - - fist_1 = "xw:nDF2m|xd:DB2m", - fistp_1 = "xw:nDF3m|xd:DB3m|xq:nDF7m", - fild_1 = "xw:nDF0m|xd:DB0m|xq:nDF5m", - - fxch_0 = "D9C9", - fxch_1 = "ff:D9C8r", - fxch_2 = "fFf:D9C8r|Fff:D9C8R", - - fucom_1 = "ff:DDE0r", - fucom_2 = "Fff:DDE0R", - fucomp_1 = "ff:DDE8r", - fucomp_2 = "Fff:DDE8R", - fucomi_1 = "ff:DBE8r", -- P6+ - fucomi_2 = "Fff:DBE8R", -- P6+ - fucomip_1 = "ff:DFE8r", -- P6+ - fucomip_2 = "Fff:DFE8R", -- P6+ - fcomi_1 = "ff:DBF0r", -- P6+ - fcomi_2 = "Fff:DBF0R", -- P6+ - fcomip_1 = "ff:DFF0r", -- P6+ - fcomip_2 = "Fff:DFF0R", -- P6+ - fucompp_0 = "DAE9", - fcompp_0 = "DED9", - - fldcw_1 = "xw:nD95m", - fstcw_1 = "xw:n9BD97m", - fnstcw_1 = "xw:nD97m", - fstsw_1 = "Rw:n9BDFE0|xw:n9BDD7m", - fnstsw_1 = "Rw:nDFE0|xw:nDD7m", - fclex_0 = "9BDBE2", - fnclex_0 = "DBE2", - - fnop_0 = "D9D0", - -- D9D1-D9DF: unassigned - - fchs_0 = "D9E0", - fabs_0 = "D9E1", - -- D9E2: unassigned - -- D9E3: unassigned - ftst_0 = "D9E4", - fxam_0 = "D9E5", - -- D9E6: unassigned - -- D9E7: unassigned - fld1_0 = "D9E8", - fldl2t_0 = "D9E9", - fldl2e_0 = "D9EA", - fldpi_0 = "D9EB", - fldlg2_0 = "D9EC", - fldln2_0 = "D9ED", - fldz_0 = "D9EE", - -- D9EF: unassigned - - f2xm1_0 = "D9F0", - fyl2x_0 = "D9F1", - fptan_0 = "D9F2", - fpatan_0 = "D9F3", - fxtract_0 = "D9F4", - fprem1_0 = "D9F5", - fdecstp_0 = "D9F6", - fincstp_0 = "D9F7", - fprem_0 = "D9F8", - fyl2xp1_0 = "D9F9", - fsqrt_0 = "D9FA", - fsincos_0 = "D9FB", - frndint_0 = "D9FC", - fscale_0 = "D9FD", - fsin_0 = "D9FE", - fcos_0 = "D9FF", - - -- SSE, SSE2 - andnpd_2 = "rmo:660F55rM", - andnps_2 = "rmo:0F55rM", - andpd_2 = "rmo:660F54rM", - andps_2 = "rmo:0F54rM", - clflush_1 = "x.:0FAE7m", - cmppd_3 = "rmio:660FC2rMU", - cmpps_3 = "rmio:0FC2rMU", - cmpsd_3 = "rrio:F20FC2rMU|rxi/oq:", - cmpss_3 = "rrio:F30FC2rMU|rxi/od:", - comisd_2 = "rro:660F2FrM|rx/oq:", - comiss_2 = "rro:0F2FrM|rx/od:", - cvtdq2pd_2 = "rro:F30FE6rM|rx/oq:", - cvtdq2ps_2 = "rmo:0F5BrM", - cvtpd2dq_2 = "rmo:F20FE6rM", - cvtpd2ps_2 = "rmo:660F5ArM", - cvtpi2pd_2 = "rx/oq:660F2ArM", - cvtpi2ps_2 = "rx/oq:0F2ArM", - cvtps2dq_2 = "rmo:660F5BrM", - cvtps2pd_2 = "rro:0F5ArM|rx/oq:", - cvtsd2si_2 = "rr/do:F20F2DrM|rr/qo:|rx/dq:|rxq:", - cvtsd2ss_2 = "rro:F20F5ArM|rx/oq:", - cvtsi2sd_2 = "rm/od:F20F2ArM|rm/oq:F20F2ArXM", - cvtsi2ss_2 = "rm/od:F30F2ArM|rm/oq:F30F2ArXM", - cvtss2sd_2 = "rro:F30F5ArM|rx/od:", - cvtss2si_2 = "rr/do:F20F2CrM|rr/qo:|rxd:|rx/qd:", - cvttpd2dq_2 = "rmo:660FE6rM", - cvttps2dq_2 = "rmo:F30F5BrM", - cvttsd2si_2 = "rr/do:F20F2CrM|rr/qo:|rx/dq:|rxq:", - cvttss2si_2 = "rr/do:F30F2CrM|rr/qo:|rxd:|rx/qd:", - ldmxcsr_1 = "xd:0FAE2m", - lfence_0 = "0FAEE8", - maskmovdqu_2 = "rro:660FF7rM", - mfence_0 = "0FAEF0", - movapd_2 = "rmo:660F28rM|mro:660F29Rm", - movaps_2 = "rmo:0F28rM|mro:0F29Rm", - movd_2 = "rm/od:660F6ErM|rm/oq:660F6ErXM|mr/do:660F7ERm|mr/qo:", - movdqa_2 = "rmo:660F6FrM|mro:660F7FRm", - movdqu_2 = "rmo:F30F6FrM|mro:F30F7FRm", - movhlps_2 = "rro:0F12rM", - movhpd_2 = "rx/oq:660F16rM|xr/qo:n660F17Rm", - movhps_2 = "rx/oq:0F16rM|xr/qo:n0F17Rm", - movlhps_2 = "rro:0F16rM", - movlpd_2 = "rx/oq:660F12rM|xr/qo:n660F13Rm", - movlps_2 = "rx/oq:0F12rM|xr/qo:n0F13Rm", - movmskpd_2 = "rr/do:660F50rM", - movmskps_2 = "rr/do:0F50rM", - movntdq_2 = "xro:660FE7Rm", - movnti_2 = "xrqd:0FC3Rm", - movntpd_2 = "xro:660F2BRm", - movntps_2 = "xro:0F2BRm", - movq_2 = "rro:F30F7ErM|rx/oq:|xr/qo:n660FD6Rm", - movsd_2 = "rro:F20F10rM|rx/oq:|xr/qo:nF20F11Rm", - movss_2 = "rro:F30F10rM|rx/od:|xr/do:F30F11Rm", - movupd_2 = "rmo:660F10rM|mro:660F11Rm", - movups_2 = "rmo:0F10rM|mro:0F11Rm", - orpd_2 = "rmo:660F56rM", - orps_2 = "rmo:0F56rM", - packssdw_2 = "rmo:660F6BrM", - packsswb_2 = "rmo:660F63rM", - packuswb_2 = "rmo:660F67rM", - paddb_2 = "rmo:660FFCrM", - paddd_2 = "rmo:660FFErM", - paddq_2 = "rmo:660FD4rM", - paddsb_2 = "rmo:660FECrM", - paddsw_2 = "rmo:660FEDrM", - paddusb_2 = "rmo:660FDCrM", - paddusw_2 = "rmo:660FDDrM", - paddw_2 = "rmo:660FFDrM", - pand_2 = "rmo:660FDBrM", - pandn_2 = "rmo:660FDFrM", - pause_0 = "F390", - pavgb_2 = "rmo:660FE0rM", - pavgw_2 = "rmo:660FE3rM", - pcmpeqb_2 = "rmo:660F74rM", - pcmpeqd_2 = "rmo:660F76rM", - pcmpeqw_2 = "rmo:660F75rM", - pcmpgtb_2 = "rmo:660F64rM", - pcmpgtd_2 = "rmo:660F66rM", - pcmpgtw_2 = "rmo:660F65rM", - pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nrMU", -- Mem op: SSE4.1 only. - pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:", - pmaddwd_2 = "rmo:660FF5rM", - pmaxsw_2 = "rmo:660FEErM", - pmaxub_2 = "rmo:660FDErM", - pminsw_2 = "rmo:660FEArM", - pminub_2 = "rmo:660FDArM", - pmovmskb_2 = "rr/do:660FD7rM", - pmulhuw_2 = "rmo:660FE4rM", - pmulhw_2 = "rmo:660FE5rM", - pmullw_2 = "rmo:660FD5rM", - pmuludq_2 = "rmo:660FF4rM", - por_2 = "rmo:660FEBrM", - prefetchnta_1 = "xb:n0F180m", - prefetcht0_1 = "xb:n0F181m", - prefetcht1_1 = "xb:n0F182m", - prefetcht2_1 = "xb:n0F183m", - psadbw_2 = "rmo:660FF6rM", - pshufd_3 = "rmio:660F70rMU", - pshufhw_3 = "rmio:F30F70rMU", - pshuflw_3 = "rmio:F20F70rMU", - pslld_2 = "rmo:660FF2rM|rio:660F726mU", - pslldq_2 = "rio:660F737mU", - psllq_2 = "rmo:660FF3rM|rio:660F736mU", - psllw_2 = "rmo:660FF1rM|rio:660F716mU", - psrad_2 = "rmo:660FE2rM|rio:660F724mU", - psraw_2 = "rmo:660FE1rM|rio:660F714mU", - psrld_2 = "rmo:660FD2rM|rio:660F722mU", - psrldq_2 = "rio:660F733mU", - psrlq_2 = "rmo:660FD3rM|rio:660F732mU", - psrlw_2 = "rmo:660FD1rM|rio:660F712mU", - psubb_2 = "rmo:660FF8rM", - psubd_2 = "rmo:660FFArM", - psubq_2 = "rmo:660FFBrM", - psubsb_2 = "rmo:660FE8rM", - psubsw_2 = "rmo:660FE9rM", - psubusb_2 = "rmo:660FD8rM", - psubusw_2 = "rmo:660FD9rM", - psubw_2 = "rmo:660FF9rM", - punpckhbw_2 = "rmo:660F68rM", - punpckhdq_2 = "rmo:660F6ArM", - punpckhqdq_2 = "rmo:660F6DrM", - punpckhwd_2 = "rmo:660F69rM", - punpcklbw_2 = "rmo:660F60rM", - punpckldq_2 = "rmo:660F62rM", - punpcklqdq_2 = "rmo:660F6CrM", - punpcklwd_2 = "rmo:660F61rM", - pxor_2 = "rmo:660FEFrM", - rcpps_2 = "rmo:0F53rM", - rcpss_2 = "rro:F30F53rM|rx/od:", - rsqrtps_2 = "rmo:0F52rM", - rsqrtss_2 = "rmo:F30F52rM", - sfence_0 = "0FAEF8", - shufpd_3 = "rmio:660FC6rMU", - shufps_3 = "rmio:0FC6rMU", - stmxcsr_1 = "xd:0FAE3m", - ucomisd_2 = "rro:660F2ErM|rx/oq:", - ucomiss_2 = "rro:0F2ErM|rx/od:", - unpckhpd_2 = "rmo:660F15rM", - unpckhps_2 = "rmo:0F15rM", - unpcklpd_2 = "rmo:660F14rM", - unpcklps_2 = "rmo:0F14rM", - xorpd_2 = "rmo:660F57rM", - xorps_2 = "rmo:0F57rM", - - -- SSE3 ops - fisttp_1 = "xw:nDF1m|xd:DB1m|xq:nDD1m", - addsubpd_2 = "rmo:660FD0rM", - addsubps_2 = "rmo:F20FD0rM", - haddpd_2 = "rmo:660F7CrM", - haddps_2 = "rmo:F20F7CrM", - hsubpd_2 = "rmo:660F7DrM", - hsubps_2 = "rmo:F20F7DrM", - lddqu_2 = "rxo:F20FF0rM", - movddup_2 = "rmo:F20F12rM", - movshdup_2 = "rmo:F30F16rM", - movsldup_2 = "rmo:F30F12rM", - - -- SSSE3 ops - pabsb_2 = "rmo:660F381CrM", - pabsd_2 = "rmo:660F381ErM", - pabsw_2 = "rmo:660F381DrM", - palignr_3 = "rmio:660F3A0FrMU", - phaddd_2 = "rmo:660F3802rM", - phaddsw_2 = "rmo:660F3803rM", - phaddw_2 = "rmo:660F3801rM", - phsubd_2 = "rmo:660F3806rM", - phsubsw_2 = "rmo:660F3807rM", - phsubw_2 = "rmo:660F3805rM", - pmaddubsw_2 = "rmo:660F3804rM", - pmulhrsw_2 = "rmo:660F380BrM", - pshufb_2 = "rmo:660F3800rM", - psignb_2 = "rmo:660F3808rM", - psignd_2 = "rmo:660F380ArM", - psignw_2 = "rmo:660F3809rM", - - -- SSE4.1 ops - blendpd_3 = "rmio:660F3A0DrMU", - blendps_3 = "rmio:660F3A0CrMU", - blendvpd_3 = "rmRo:660F3815rM", - blendvps_3 = "rmRo:660F3814rM", - dppd_3 = "rmio:660F3A41rMU", - dpps_3 = "rmio:660F3A40rMU", - extractps_3 = "mri/do:660F3A17RmU|rri/qo:660F3A17RXmU", - insertps_3 = "rrio:660F3A41rMU|rxi/od:", - movntdqa_2 = "rmo:660F382ArM", - mpsadbw_3 = "rmio:660F3A42rMU", - packusdw_2 = "rmo:660F382BrM", - pblendvb_3 = "rmRo:660F3810rM", - pblendw_3 = "rmio:660F3A0ErMU", - pcmpeqq_2 = "rmo:660F3829rM", - pextrb_3 = "rri/do:660F3A14nRmU|rri/qo:|xri/bo:", - pextrd_3 = "mri/do:660F3A16RmU", - pextrq_3 = "mri/qo:660F3A16RmU", - -- pextrw is SSE2, mem operand is SSE4.1 only - phminposuw_2 = "rmo:660F3841rM", - pinsrb_3 = "rri/od:660F3A20nrMU|rxi/ob:", - pinsrd_3 = "rmi/od:660F3A22rMU", - pinsrq_3 = "rmi/oq:660F3A22rXMU", - pmaxsb_2 = "rmo:660F383CrM", - pmaxsd_2 = "rmo:660F383DrM", - pmaxud_2 = "rmo:660F383FrM", - pmaxuw_2 = "rmo:660F383ErM", - pminsb_2 = "rmo:660F3838rM", - pminsd_2 = "rmo:660F3839rM", - pminud_2 = "rmo:660F383BrM", - pminuw_2 = "rmo:660F383ArM", - pmovsxbd_2 = "rro:660F3821rM|rx/od:", - pmovsxbq_2 = "rro:660F3822rM|rx/ow:", - pmovsxbw_2 = "rro:660F3820rM|rx/oq:", - pmovsxdq_2 = "rro:660F3825rM|rx/oq:", - pmovsxwd_2 = "rro:660F3823rM|rx/oq:", - pmovsxwq_2 = "rro:660F3824rM|rx/od:", - pmovzxbd_2 = "rro:660F3831rM|rx/od:", - pmovzxbq_2 = "rro:660F3832rM|rx/ow:", - pmovzxbw_2 = "rro:660F3830rM|rx/oq:", - pmovzxdq_2 = "rro:660F3835rM|rx/oq:", - pmovzxwd_2 = "rro:660F3833rM|rx/oq:", - pmovzxwq_2 = "rro:660F3834rM|rx/od:", - pmuldq_2 = "rmo:660F3828rM", - pmulld_2 = "rmo:660F3840rM", - ptest_2 = "rmo:660F3817rM", - roundpd_3 = "rmio:660F3A09rMU", - roundps_3 = "rmio:660F3A08rMU", - roundsd_3 = "rrio:660F3A0BrMU|rxi/oq:", - roundss_3 = "rrio:660F3A0ArMU|rxi/od:", - - -- SSE4.2 ops - crc32_2 = "rmqd:F20F38F1rM|rm/dw:66F20F38F1rM|rm/db:F20F38F0rM|rm/qb:", - pcmpestri_3 = "rmio:660F3A61rMU", - pcmpestrm_3 = "rmio:660F3A60rMU", - pcmpgtq_2 = "rmo:660F3837rM", - pcmpistri_3 = "rmio:660F3A63rMU", - pcmpistrm_3 = "rmio:660F3A62rMU", - popcnt_2 = "rmqdw:F30FB8rM", - - -- SSE4a - extrq_2 = "rro:660F79rM", - extrq_3 = "riio:660F780mUU", - insertq_2 = "rro:F20F79rM", - insertq_4 = "rriio:F20F78rMUU", - lzcnt_2 = "rmqdw:F30FBDrM", - movntsd_2 = "xr/qo:nF20F2BRm", - movntss_2 = "xr/do:F30F2BRm", - -- popcnt is also in SSE4.2 -} - ------------------------------------------------------------------------------- - --- Arithmetic ops. -for name,n in pairs{ add = 0, ["or"] = 1, adc = 2, sbb = 3, - ["and"] = 4, sub = 5, xor = 6, cmp = 7 } do - local n8 = shl(n, 3) - map_op[name.."_2"] = format( - "mr:%02XRm|rm:%02XrM|mI1qdw:81%XmI|mS1qdw:83%XmS|Ri1qdwb:%02Xri|mi1qdwb:81%Xmi", - 1+n8, 3+n8, n, n, 5+n8, n) -end - --- Shift ops. -for name,n in pairs{ rol = 0, ror = 1, rcl = 2, rcr = 3, - shl = 4, shr = 5, sar = 7, sal = 4 } do - map_op[name.."_2"] = format("m1:D1%Xm|mC1qdwb:D3%Xm|mi:C1%XmU", n, n, n) -end - --- Conditional ops. -for cc,n in pairs(map_cc) do - map_op["j"..cc.."_1"] = format("J.:n0F8%XJ", n) -- short: 7%X - map_op["set"..cc.."_1"] = format("mb:n0F9%X2m", n) - map_op["cmov"..cc.."_2"] = format("rmqdw:0F4%XrM", n) -- P6+ -end - --- FP arithmetic ops. -for name,n in pairs{ add = 0, mul = 1, com = 2, comp = 3, - sub = 4, subr = 5, div = 6, divr = 7 } do - local nc = 0xc0 + shl(n, 3) - local nr = nc + (n < 4 and 0 or (n % 2 == 0 and 8 or -8)) - local fn = "f"..name - map_op[fn.."_1"] = format("ff:D8%02Xr|xd:D8%Xm|xq:nDC%Xm", nc, n, n) - if n == 2 or n == 3 then - map_op[fn.."_2"] = format("Fff:D8%02XR|Fx2d:D8%XM|Fx2q:nDC%XM", nc, n, n) - else - map_op[fn.."_2"] = format("Fff:D8%02XR|fFf:DC%02Xr|Fx2d:D8%XM|Fx2q:nDC%XM", nc, nr, n, n) - map_op[fn.."p_1"] = format("ff:DE%02Xr", nr) - map_op[fn.."p_2"] = format("fFf:DE%02Xr", nr) - end - map_op["fi"..name.."_1"] = format("xd:DA%Xm|xw:nDE%Xm", n, n) -end - --- FP conditional moves. -for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do - local nc = 0xdac0 + shl(band(n, 3), 3) + shl(band(n, 4), 6) - map_op["fcmov"..cc.."_1"] = format("ff:%04Xr", nc) -- P6+ - map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+ -end - --- SSE FP arithmetic ops. -for name,n in pairs{ sqrt = 1, add = 8, mul = 9, - sub = 12, min = 13, div = 14, max = 15 } do - map_op[name.."ps_2"] = format("rmo:0F5%XrM", n) - map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n) - map_op[name.."pd_2"] = format("rmo:660F5%XrM", n) - map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n) -end - ------------------------------------------------------------------------------- - --- Process pattern string. -local function dopattern(pat, args, sz, op, needrex) - local digit, addin - local opcode = 0 - local szov = sz - local narg = 1 - local rex = 0 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 5 positions. - if secpos+5 > maxsecpos then wflush() end - - -- Process each character. - for c in gmatch(pat.."|", ".") do - if match(c, "%x") then -- Hex digit. - digit = byte(c) - 48 - if digit > 48 then digit = digit - 39 - elseif digit > 16 then digit = digit - 7 end - opcode = opcode*16 + digit - addin = nil - elseif c == "n" then -- Disable operand size mods for opcode. - szov = nil - elseif c == "X" then -- Force REX.W. - rex = 8 - elseif c == "r" then -- Merge 1st operand regno. into opcode. - addin = args[1]; opcode = opcode + (addin.reg % 8) - if narg < 2 then narg = 2 end - elseif c == "R" then -- Merge 2nd operand regno. into opcode. - addin = args[2]; opcode = opcode + (addin.reg % 8) - narg = 3 - elseif c == "m" or c == "M" then -- Encode ModRM/SIB. - local s - if addin then - s = addin.reg - opcode = opcode - band(s, 7) -- Undo regno opcode merge. - else - s = band(opcode, 15) -- Undo last digit. - opcode = shr(opcode, 4) - end - local nn = c == "m" and 1 or 2 - local t = args[nn] - if narg <= nn then narg = nn + 1 end - if szov == "q" and rex == 0 then rex = rex + 8 end - if t.reg and t.reg > 7 then rex = rex + 1 end - if t.xreg and t.xreg > 7 then rex = rex + 2 end - if s > 7 then rex = rex + 4 end - if needrex then rex = rex + 16 end - wputop(szov, opcode, rex); opcode = nil - local imark = sub(pat, -1) -- Force a mark (ugly). - -- Put ModRM/SIB with regno/last digit as spare. - wputmrmsib(t, imark, s, addin and addin.vreg) - addin = nil - else - if opcode then -- Flush opcode. - if szov == "q" and rex == 0 then rex = rex + 8 end - if needrex then rex = rex + 16 end - if addin and addin.reg == -1 then - wputop(szov, opcode - 7, rex) - waction("VREG", addin.vreg); wputxb(0) - else - if addin and addin.reg > 7 then rex = rex + 1 end - wputop(szov, opcode, rex) - end - opcode = nil - end - if c == "|" then break end - if c == "o" then -- Offset (pure 32 bit displacement). - wputdarg(args[1].disp); if narg < 2 then narg = 2 end - elseif c == "O" then - wputdarg(args[2].disp); narg = 3 - else - -- Anything else is an immediate operand. - local a = args[narg] - narg = narg + 1 - local mode, imm = a.mode, a.imm - if mode == "iJ" and not match("iIJ", c) then - werror("bad operand size for label") - end - if c == "S" then - wputsbarg(imm) - elseif c == "U" then - wputbarg(imm) - elseif c == "W" then - wputwarg(imm) - elseif c == "i" or c == "I" then - if mode == "iJ" then - wputlabel("IMM_", imm, 1) - elseif mode == "iI" and c == "I" then - waction(sz == "w" and "IMM_WB" or "IMM_DB", imm) - else - wputszarg(sz, imm) - end - elseif c == "J" then - if mode == "iPJ" then - waction("REL_A", imm) -- !x64 (secpos) - else - wputlabel("REL_", imm, 2) - end - else - werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'") - end - end - end - end -end - ------------------------------------------------------------------------------- - --- Mapping of operand modes to short names. Suppress output with '#'. -local map_modename = { - r = "reg", R = "eax", C = "cl", x = "mem", m = "mrm", i = "imm", - f = "stx", F = "st0", J = "lbl", ["1"] = "1", - I = "#", S = "#", O = "#", -} - --- Return a table/string showing all possible operand modes. -local function templatehelp(template, nparams) - if nparams == 0 then return "" end - local t = {} - for tm in gmatch(template, "[^%|]+") do - local s = map_modename[sub(tm, 1, 1)] - s = s..gsub(sub(tm, 2, nparams), ".", function(c) - return ", "..map_modename[c] - end) - if not match(s, "#") then t[#t+1] = s end - end - return t -end - --- Match operand modes against mode match part of template. -local function matchtm(tm, args) - for i=1,#args do - if not match(args[i].mode, sub(tm, i, i)) then return end - end - return true -end - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return templatehelp(template, nparams) end - local args = {} - - -- Zero-operand opcodes have no match part. - if #params == 0 then - dopattern(template, args, "d", params.op, nil) - return - end - - -- Determine common operand size (coerce undefined size) or flag as mixed. - local sz, szmix, needrex - for i,p in ipairs(params) do - args[i] = parseoperand(p) - local nsz = args[i].opsize - if nsz then - if sz and sz ~= nsz then szmix = true else sz = nsz end - end - local nrex = args[i].needrex - if nrex ~= nil then - if needrex == nil then - needrex = nrex - elseif needrex ~= nrex then - werror("bad mix of byte-addressable registers") - end - end - end - - -- Try all match:pattern pairs (separated by '|'). - local gotmatch, lastpat - for tm in gmatch(template, "[^%|]+") do - -- Split off size match (starts after mode match) and pattern string. - local szm, pat = match(tm, "^(.-):(.*)$", #args+1) - if pat == "" then pat = lastpat else lastpat = pat end - if matchtm(tm, args) then - local prefix = sub(szm, 1, 1) - if prefix == "/" then -- Match both operand sizes. - if args[1].opsize == sub(szm, 2, 2) and - args[2].opsize == sub(szm, 3, 3) then - dopattern(pat, args, sz, params.op, needrex) -- Process pattern. - return - end - else -- Match common operand size. - local szp = sz - if szm == "" then szm = x64 and "qdwb" or "dwb" end -- Default sizes. - if prefix == "1" then szp = args[1].opsize; szmix = nil - elseif prefix == "2" then szp = args[2].opsize; szmix = nil end - if not szmix and (prefix == "." or match(szm, szp or "#")) then - dopattern(pat, args, szp, params.op, needrex) -- Process pattern. - return - end - end - gotmatch = true - end - end - - local msg = "bad operand mode" - if gotmatch then - if szmix then - msg = "mixed operand size" - else - msg = sz and "bad operand size" or "missing operand size" - end - end - - werror(msg.." in `"..opmodestr(params.op, args).."'") -end - ------------------------------------------------------------------------------- - --- x64-specific opcode for 64 bit immediates and displacements. -if x64 then - function map_op.mov64_2(params) - if not params then return { "reg, imm", "reg, [disp]", "[disp], reg" } end - if secpos+2 > maxsecpos then wflush() end - local opcode, op64, sz, rex - local op64 = match(params[1], "^%[%s*(.-)%s*%]$") - if op64 then - local a = parseoperand(params[2]) - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa3 - else - op64 = match(params[2], "^%[%s*(.-)%s*%]$") - local a = parseoperand(params[1]) - if op64 then - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa1 - else - if sub(a.mode, 1, 1) ~= "r" or a.opsize ~= "q" then - werror("bad operand mode") - end - op64 = params[2] - opcode = 0xb8 + band(a.reg, 7) -- !x64: no VREG support. - rex = a.reg > 7 and 9 or 8 - end - end - wputop(sz, opcode, rex) - waction("IMM_D", format("(unsigned int)(%s)", op64)) - waction("IMM_D", format("(unsigned int)((%s)>>32)", op64)) - end -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -local function op_data(params) - if not params then return "imm..." end - local sz = sub(params.op, 2, 2) - if sz == "a" then sz = addrsize end - for _,p in ipairs(params) do - local a = parseoperand(p) - if sub(a.mode, 1, 1) ~= "i" or (a.opsize and a.opsize ~= sz) then - werror("bad mode or size in `"..p.."'") - end - if a.mode == "iJ" then - wputlabel("IMM_", a.imm, 1) - else - wputszarg(sz, a.imm) - end - if secpos+2 > maxsecpos then wflush() end - end -end - -map_op[".byte_*"] = op_data -map_op[".sbyte_*"] = op_data -map_op[".word_*"] = op_data -map_op[".dword_*"] = op_data -map_op[".aword_*"] = op_data - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_2"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr [, addr]" end - if secpos+2 > maxsecpos then wflush() end - local a = parseoperand(params[1]) - local mode, imm = a.mode, a.imm - if type(imm) == "number" and (mode == "iJ" or (imm >= 1 and imm <= 9)) then - -- Local label (1: ... 9:) or global label (->global:). - waction("LABEL_LG", nil, 1) - wputxb(imm) - elseif mode == "iJ" then - -- PC label (=>pcexpr:). - waction("LABEL_PC", imm) - else - werror("bad label definition") - end - -- SETLABEL must immediately follow LABEL_LG/LABEL_PC. - local addr = params[2] - if addr then - local a = parseoperand(addr) - if a.mode == "iPJ" then - waction("SETLABEL", a.imm) - else - werror("bad label assignment") - end - end -end -map_op[".label_1"] = map_op[".label_2"] - ------------------------------------------------------------------------------- - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) or map_opsizenum[map_opsize[params[1]]] - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", nil, 1) - wputxb(align-1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - --- Spacing pseudo-opcode. -map_op[".space_2"] = function(params) - if not params then return "num [, filler]" end - if secpos+1 > maxsecpos then wflush() end - waction("SPACE", params[1]) - local fill = params[2] - if fill then - fill = tonumber(fill) - if not fill or fill < 0 or fill > 255 then werror("bad filler") end - end - wputxb(fill or 0) -end -map_op[".space_1"] = map_op[".space_2"] - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - if reg and not map_reg_valid_base[reg] then - werror("bad base register `"..(map_reg_rev[reg] or reg).."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg and map_reg_rev[tp.reg] or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION") - wputxb(num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpregs(out) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third-party/LuaJIT-2.0.2/dynasm/dynasm.lua b/third-party/LuaJIT-2.0.2/dynasm/dynasm.lua deleted file mode 100644 index d670f8f8b3..0000000000 --- a/third-party/LuaJIT-2.0.2/dynasm/dynasm.lua +++ /dev/null @@ -1,1095 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM. A dynamic assembler for code generation engines. --- Originally designed and implemented for LuaJIT. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- See below for full copyright notice. ------------------------------------------------------------------------------- - --- Application information. -local _info = { - name = "DynASM", - description = "A dynamic assembler for code generation engines", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - url = "http://luajit.org/dynasm.html", - license = "MIT", - copyright = [[ -Copyright (C) 2005-2013 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -[ MIT license: http://www.opensource.org/licenses/mit-license.php ] -]], -} - --- Cache library functions. -local type, pairs, ipairs = type, pairs, ipairs -local pcall, error, assert = pcall, error, assert -local _s = string -local sub, match, gmatch, gsub = _s.sub, _s.match, _s.gmatch, _s.gsub -local format, rep, upper = _s.format, _s.rep, _s.upper -local _t = table -local insert, remove, concat, sort = _t.insert, _t.remove, _t.concat, _t.sort -local exit = os.exit -local io = io -local stdin, stdout, stderr = io.stdin, io.stdout, io.stderr - ------------------------------------------------------------------------------- - --- Program options. -local g_opt = {} - --- Global state for current file. -local g_fname, g_curline, g_indent, g_lineno, g_synclineno, g_arch -local g_errcount = 0 - --- Write buffer for output file. -local g_wbuffer, g_capbuffer - ------------------------------------------------------------------------------- - --- Write an output line (or callback function) to the buffer. -local function wline(line, needindent) - local buf = g_capbuffer or g_wbuffer - buf[#buf+1] = needindent and g_indent..line or line - g_synclineno = g_synclineno + 1 -end - --- Write assembler line as a comment, if requestd. -local function wcomment(aline) - if g_opt.comment then - wline(g_opt.comment..aline..g_opt.endcomment, true) - end -end - --- Resync CPP line numbers. -local function wsync() - if g_synclineno ~= g_lineno and g_opt.cpp then - wline("# "..g_lineno..' "'..g_fname..'"') - g_synclineno = g_lineno - end -end - --- Dummy action flush function. Replaced with arch-specific function later. -local function wflush(term) -end - --- Dump all buffered output lines. -local function wdumplines(out, buf) - for _,line in ipairs(buf) do - if type(line) == "string" then - assert(out:write(line, "\n")) - else - -- Special callback to dynamically insert lines after end of processing. - line(out) - end - end -end - ------------------------------------------------------------------------------- - --- Emit an error. Processing continues with next statement. -local function werror(msg) - error(format("%s:%s: error: %s:\n%s", g_fname, g_lineno, msg, g_curline), 0) -end - --- Emit a fatal error. Processing stops. -local function wfatal(msg) - g_errcount = "fatal" - werror(msg) -end - --- Print a warning. Processing continues. -local function wwarn(msg) - stderr:write(format("%s:%s: warning: %s:\n%s\n", - g_fname, g_lineno, msg, g_curline)) -end - --- Print caught error message. But suppress excessive errors. -local function wprinterr(...) - if type(g_errcount) == "number" then - -- Regular error. - g_errcount = g_errcount + 1 - if g_errcount < 21 then -- Seems to be a reasonable limit. - stderr:write(...) - elseif g_errcount == 21 then - stderr:write(g_fname, - ":*: warning: too many errors (suppressed further messages).\n") - end - else - -- Fatal error. - stderr:write(...) - return true -- Stop processing. - end -end - ------------------------------------------------------------------------------- - --- Map holding all option handlers. -local opt_map = {} -local opt_current - --- Print error and exit with error status. -local function opterror(...) - stderr:write("dynasm.lua: ERROR: ", ...) - stderr:write("\n") - exit(1) -end - --- Get option parameter. -local function optparam(args) - local argn = args.argn - local p = args[argn] - if not p then - opterror("missing parameter for option `", opt_current, "'.") - end - args.argn = argn + 1 - return p -end - ------------------------------------------------------------------------------- - --- Core pseudo-opcodes. -local map_coreop = {} --- Dummy opcode map. Replaced by arch-specific map. -local map_op = {} - --- Forward declarations. -local dostmt -local readfile - ------------------------------------------------------------------------------- - --- Map for defines (initially empty, chains to arch-specific map). -local map_def = {} - --- Pseudo-opcode to define a substitution. -map_coreop[".define_2"] = function(params, nparams) - if not params then return nparams == 1 and "name" or "name, subst" end - local name, def = params[1], params[2] or "1" - if not match(name, "^[%a_][%w_]*$") then werror("bad or duplicate define") end - map_def[name] = def -end -map_coreop[".define_1"] = map_coreop[".define_2"] - --- Define a substitution on the command line. -function opt_map.D(args) - local namesubst = optparam(args) - local name, subst = match(namesubst, "^([%a_][%w_]*)=(.*)$") - if name then - map_def[name] = subst - elseif match(namesubst, "^[%a_][%w_]*$") then - map_def[namesubst] = "1" - else - opterror("bad define") - end -end - --- Undefine a substitution on the command line. -function opt_map.U(args) - local name = optparam(args) - if match(name, "^[%a_][%w_]*$") then - map_def[name] = nil - else - opterror("bad define") - end -end - --- Helper for definesubst. -local gotsubst - -local function definesubst_one(word) - local subst = map_def[word] - if subst then gotsubst = word; return subst else return word end -end - --- Iteratively substitute defines. -local function definesubst(stmt) - -- Limit number of iterations. - for i=1,100 do - gotsubst = false - stmt = gsub(stmt, "#?[%w_]+", definesubst_one) - if not gotsubst then break end - end - if gotsubst then wfatal("recursive define involving `"..gotsubst.."'") end - return stmt -end - --- Dump all defines. -local function dumpdefines(out, lvl) - local t = {} - for name in pairs(map_def) do - t[#t+1] = name - end - sort(t) - out:write("Defines:\n") - for _,name in ipairs(t) do - local subst = map_def[name] - if g_arch then subst = g_arch.revdef(subst) end - out:write(format(" %-20s %s\n", name, subst)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Support variables for conditional assembly. -local condlevel = 0 -local condstack = {} - --- Evaluate condition with a Lua expression. Substitutions already performed. -local function cond_eval(cond) - local func, err - if setfenv then - func, err = loadstring("return "..cond, "=expr") - else - -- No globals. All unknown identifiers evaluate to nil. - func, err = load("return "..cond, "=expr", "t", {}) - end - if func then - if setfenv then - setfenv(func, {}) -- No globals. All unknown identifiers evaluate to nil. - end - local ok, res = pcall(func) - if ok then - if res == 0 then return false end -- Oh well. - return not not res - end - err = res - end - wfatal("bad condition: "..err) -end - --- Skip statements until next conditional pseudo-opcode at the same level. -local function stmtskip() - local dostmt_save = dostmt - local lvl = 0 - dostmt = function(stmt) - local op = match(stmt, "^%s*(%S+)") - if op == ".if" then - lvl = lvl + 1 - elseif lvl ~= 0 then - if op == ".endif" then lvl = lvl - 1 end - elseif op == ".elif" or op == ".else" or op == ".endif" then - dostmt = dostmt_save - dostmt(stmt) - end - end -end - --- Pseudo-opcodes for conditional assembly. -map_coreop[".if_1"] = function(params) - if not params then return "condition" end - local lvl = condlevel + 1 - local res = cond_eval(params[1]) - condlevel = lvl - condstack[lvl] = res - if not res then stmtskip() end -end - -map_coreop[".elif_1"] = function(params) - if not params then return "condition" end - if condlevel == 0 then wfatal(".elif without .if") end - local lvl = condlevel - local res = condstack[lvl] - if res then - if res == "else" then wfatal(".elif after .else") end - else - res = cond_eval(params[1]) - if res then - condstack[lvl] = res - return - end - end - stmtskip() -end - -map_coreop[".else_0"] = function(params) - if condlevel == 0 then wfatal(".else without .if") end - local lvl = condlevel - local res = condstack[lvl] - condstack[lvl] = "else" - if res then - if res == "else" then wfatal(".else after .else") end - stmtskip() - end -end - -map_coreop[".endif_0"] = function(params) - local lvl = condlevel - if lvl == 0 then wfatal(".endif without .if") end - condlevel = lvl - 1 -end - --- Check for unfinished conditionals. -local function checkconds() - if g_errcount ~= "fatal" and condlevel ~= 0 then - wprinterr(g_fname, ":*: error: unbalanced conditional\n") - end -end - ------------------------------------------------------------------------------- - --- Search for a file in the given path and open it for reading. -local function pathopen(path, name) - local dirsep = package and match(package.path, "\\") and "\\" or "/" - for _,p in ipairs(path) do - local fullname = p == "" and name or p..dirsep..name - local fin = io.open(fullname, "r") - if fin then - g_fname = fullname - return fin - end - end -end - --- Include a file. -map_coreop[".include_1"] = function(params) - if not params then return "filename" end - local name = params[1] - -- Save state. Ugly, I know. but upvalues are fast. - local gf, gl, gcl, gi = g_fname, g_lineno, g_curline, g_indent - -- Read the included file. - local fatal = readfile(pathopen(g_opt.include, name) or - wfatal("include file `"..name.."' not found")) - -- Restore state. - g_synclineno = -1 - g_fname, g_lineno, g_curline, g_indent = gf, gl, gcl, gi - if fatal then wfatal("in include file") end -end - --- Make .include and conditionals initially available, too. -map_op[".include_1"] = map_coreop[".include_1"] -map_op[".if_1"] = map_coreop[".if_1"] -map_op[".elif_1"] = map_coreop[".elif_1"] -map_op[".else_0"] = map_coreop[".else_0"] -map_op[".endif_0"] = map_coreop[".endif_0"] - ------------------------------------------------------------------------------- - --- Support variables for macros. -local mac_capture, mac_lineno, mac_name -local mac_active = {} -local mac_list = {} - --- Pseudo-opcode to define a macro. -map_coreop[".macro_*"] = function(mparams) - if not mparams then return "name [, params...]" end - -- Split off and validate macro name. - local name = remove(mparams, 1) - if not name then werror("missing macro name") end - if not (match(name, "^[%a_][%w_%.]*$") or match(name, "^%.[%w_%.]*$")) then - wfatal("bad macro name `"..name.."'") - end - -- Validate macro parameter names. - local mdup = {} - for _,mp in ipairs(mparams) do - if not match(mp, "^[%a_][%w_]*$") then - wfatal("bad macro parameter name `"..mp.."'") - end - if mdup[mp] then wfatal("duplicate macro parameter name `"..mp.."'") end - mdup[mp] = true - end - -- Check for duplicate or recursive macro definitions. - local opname = name.."_"..#mparams - if map_op[opname] or map_op[name.."_*"] then - wfatal("duplicate macro `"..name.."' ("..#mparams.." parameters)") - end - if mac_capture then wfatal("recursive macro definition") end - - -- Enable statement capture. - local lines = {} - mac_lineno = g_lineno - mac_name = name - mac_capture = function(stmt) -- Statement capture function. - -- Stop macro definition with .endmacro pseudo-opcode. - if not match(stmt, "^%s*.endmacro%s*$") then - lines[#lines+1] = stmt - return - end - mac_capture = nil - mac_lineno = nil - mac_name = nil - mac_list[#mac_list+1] = opname - -- Add macro-op definition. - map_op[opname] = function(params) - if not params then return mparams, lines end - -- Protect against recursive macro invocation. - if mac_active[opname] then wfatal("recursive macro invocation") end - mac_active[opname] = true - -- Setup substitution map. - local subst = {} - for i,mp in ipairs(mparams) do subst[mp] = params[i] end - local mcom - if g_opt.maccomment and g_opt.comment then - mcom = " MACRO "..name.." ("..#mparams..")" - wcomment("{"..mcom) - end - -- Loop through all captured statements - for _,stmt in ipairs(lines) do - -- Substitute macro parameters. - local st = gsub(stmt, "[%w_]+", subst) - st = definesubst(st) - st = gsub(st, "%s*%.%.%s*", "") -- Token paste a..b. - if mcom and sub(st, 1, 1) ~= "|" then wcomment(st) end - -- Emit statement. Use a protected call for better diagnostics. - local ok, err = pcall(dostmt, st) - if not ok then - -- Add the captured statement to the error. - wprinterr(err, "\n", g_indent, "| ", stmt, - "\t[MACRO ", name, " (", #mparams, ")]\n") - end - end - if mcom then wcomment("}"..mcom) end - mac_active[opname] = nil - end - end -end - --- An .endmacro pseudo-opcode outside of a macro definition is an error. -map_coreop[".endmacro_0"] = function(params) - wfatal(".endmacro without .macro") -end - --- Dump all macros and their contents (with -PP only). -local function dumpmacros(out, lvl) - sort(mac_list) - out:write("Macros:\n") - for _,opname in ipairs(mac_list) do - local name = sub(opname, 1, -3) - local params, lines = map_op[opname]() - out:write(format(" %-20s %s\n", name, concat(params, ", "))) - if lvl > 1 then - for _,line in ipairs(lines) do - out:write(" |", line, "\n") - end - out:write("\n") - end - end - out:write("\n") -end - --- Check for unfinished macro definitions. -local function checkmacros() - if mac_capture then - wprinterr(g_fname, ":", mac_lineno, - ": error: unfinished .macro `", mac_name ,"'\n") - end -end - ------------------------------------------------------------------------------- - --- Support variables for captures. -local cap_lineno, cap_name -local cap_buffers = {} -local cap_used = {} - --- Start a capture. -map_coreop[".capture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - if cap_name then - wfatal("already capturing to `"..cap_name.."' since line "..cap_lineno) - end - cap_name = name - cap_lineno = g_lineno - -- Create or continue a capture buffer and start the output line capture. - local buf = cap_buffers[name] - if not buf then buf = {}; cap_buffers[name] = buf end - g_capbuffer = buf - g_synclineno = 0 -end - --- Stop a capture. -map_coreop[".endcapture_0"] = function(params) - wflush() - if not cap_name then wfatal(".endcapture without a valid .capture") end - cap_name = nil - cap_lineno = nil - g_capbuffer = nil - g_synclineno = 0 -end - --- Dump a capture buffer. -map_coreop[".dumpcapture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - cap_used[name] = true - wline(function(out) - local buf = cap_buffers[name] - if buf then wdumplines(out, buf) end - end) - g_synclineno = 0 -end - --- Dump all captures and their buffers (with -PP only). -local function dumpcaptures(out, lvl) - out:write("Captures:\n") - for name,buf in pairs(cap_buffers) do - out:write(format(" %-20s %4s)\n", name, "("..#buf)) - if lvl > 1 then - local bar = rep("=", 76) - out:write(" ", bar, "\n") - for _,line in ipairs(buf) do - out:write(" ", line, "\n") - end - out:write(" ", bar, "\n\n") - end - end - out:write("\n") -end - --- Check for unfinished or unused captures. -local function checkcaptures() - if cap_name then - wprinterr(g_fname, ":", cap_lineno, - ": error: unfinished .capture `", cap_name,"'\n") - return - end - for name in pairs(cap_buffers) do - if not cap_used[name] then - wprinterr(g_fname, ":*: error: missing .dumpcapture ", name ,"\n") - end - end -end - ------------------------------------------------------------------------------- - --- Sections names. -local map_sections = {} - --- Pseudo-opcode to define code sections. --- TODO: Data sections, BSS sections. Needs extra C code and API. -map_coreop[".section_*"] = function(params) - if not params then return "name..." end - if #map_sections > 0 then werror("duplicate section definition") end - wflush() - for sn,name in ipairs(params) do - local opname = "."..name.."_0" - if not match(name, "^[%a][%w_]*$") or - map_op[opname] or map_op["."..name.."_*"] then - werror("bad section name `"..name.."'") - end - map_sections[#map_sections+1] = name - wline(format("#define DASM_SECTION_%s\t%d", upper(name), sn-1)) - map_op[opname] = function(params) g_arch.section(sn-1) end - end - wline(format("#define DASM_MAXSECTION\t\t%d", #map_sections)) -end - --- Dump all sections. -local function dumpsections(out, lvl) - out:write("Sections:\n") - for _,name in ipairs(map_sections) do - out:write(format(" %s\n", name)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Replacement for customized Lua, which lacks the package library. -local prefix = "" -if not require then - function require(name) - local fp = assert(io.open(prefix..name..".lua")) - local s = fp:read("*a") - assert(fp:close()) - return assert(loadstring(s, "@"..name..".lua"))() - end -end - --- Load architecture-specific module. -local function loadarch(arch) - if not match(arch, "^[%w_]+$") then return "bad arch name" end - local ok, m_arch = pcall(require, "dasm_"..arch) - if not ok then return "cannot load module: "..m_arch end - g_arch = m_arch - wflush = m_arch.passcb(wline, werror, wfatal, wwarn) - m_arch.setup(arch, g_opt) - map_op, map_def = m_arch.mergemaps(map_coreop, map_def) -end - --- Dump architecture description. -function opt_map.dumparch(args) - local name = optparam(args) - if not g_arch then - local err = loadarch(name) - if err then opterror(err) end - end - - local t = {} - for name in pairs(map_coreop) do t[#t+1] = name end - for name in pairs(map_op) do t[#t+1] = name end - sort(t) - - local out = stdout - local _arch = g_arch._info - out:write(format("%s version %s, released %s, %s\n", - _info.name, _info.version, _info.release, _info.url)) - g_arch.dumparch(out) - - local pseudo = true - out:write("Pseudo-Opcodes:\n") - for _,sname in ipairs(t) do - local name, nparam = match(sname, "^(.+)_([0-9%*])$") - if name then - if pseudo and sub(name, 1, 1) ~= "." then - out:write("\nOpcodes:\n") - pseudo = false - end - local f = map_op[sname] - local s - if nparam ~= "*" then nparam = nparam + 0 end - if nparam == 0 then - s = "" - elseif type(f) == "string" then - s = map_op[".template__"](nil, f, nparam) - else - s = f(nil, nparam) - end - if type(s) == "table" then - for _,s2 in ipairs(s) do - out:write(format(" %-12s %s\n", name, s2)) - end - else - out:write(format(" %-12s %s\n", name, s)) - end - end - end - out:write("\n") - exit(0) -end - --- Pseudo-opcode to set the architecture. --- Only initially available (map_op is replaced when called). -map_op[".arch_1"] = function(params) - if not params then return "name" end - local err = loadarch(params[1]) - if err then wfatal(err) end -end - --- Dummy .arch pseudo-opcode to improve the error report. -map_coreop[".arch_1"] = function(params) - if not params then return "name" end - wfatal("duplicate .arch statement") -end - ------------------------------------------------------------------------------- - --- Dummy pseudo-opcode. Don't confuse '.nop' with 'nop'. -map_coreop[".nop_*"] = function(params) - if not params then return "[ignored...]" end -end - --- Pseudo-opcodes to raise errors. -map_coreop[".error_1"] = function(params) - if not params then return "message" end - werror(params[1]) -end - -map_coreop[".fatal_1"] = function(params) - if not params then return "message" end - wfatal(params[1]) -end - --- Dump all user defined elements. -local function dumpdef(out) - local lvl = g_opt.dumpdef - if lvl == 0 then return end - dumpsections(out, lvl) - dumpdefines(out, lvl) - if g_arch then g_arch.dumpdef(out, lvl) end - dumpmacros(out, lvl) - dumpcaptures(out, lvl) -end - ------------------------------------------------------------------------------- - --- Helper for splitstmt. -local splitlvl - -local function splitstmt_one(c) - if c == "(" then - splitlvl = ")"..splitlvl - elseif c == "[" then - splitlvl = "]"..splitlvl - elseif c == "{" then - splitlvl = "}"..splitlvl - elseif c == ")" or c == "]" or c == "}" then - if sub(splitlvl, 1, 1) ~= c then werror("unbalanced (), [] or {}") end - splitlvl = sub(splitlvl, 2) - elseif splitlvl == "" then - return " \0 " - end - return c -end - --- Split statement into (pseudo-)opcode and params. -local function splitstmt(stmt) - -- Convert label with trailing-colon into .label statement. - local label = match(stmt, "^%s*(.+):%s*$") - if label then return ".label", {label} end - - -- Split at commas and equal signs, but obey parentheses and brackets. - splitlvl = "" - stmt = gsub(stmt, "[,%(%)%[%]{}]", splitstmt_one) - if splitlvl ~= "" then werror("unbalanced () or []") end - - -- Split off opcode. - local op, other = match(stmt, "^%s*([^%s%z]+)%s*(.*)$") - if not op then werror("bad statement syntax") end - - -- Split parameters. - local params = {} - for p in gmatch(other, "%s*(%Z+)%z?") do - params[#params+1] = gsub(p, "%s+$", "") - end - if #params > 16 then werror("too many parameters") end - - params.op = op - return op, params -end - --- Process a single statement. -dostmt = function(stmt) - -- Ignore empty statements. - if match(stmt, "^%s*$") then return end - - -- Capture macro defs before substitution. - if mac_capture then return mac_capture(stmt) end - stmt = definesubst(stmt) - - -- Emit C code without parsing the line. - if sub(stmt, 1, 1) == "|" then - local tail = sub(stmt, 2) - wflush() - if sub(tail, 1, 2) == "//" then wcomment(tail) else wline(tail, true) end - return - end - - -- Split into (pseudo-)opcode and params. - local op, params = splitstmt(stmt) - - -- Get opcode handler (matching # of parameters or generic handler). - local f = map_op[op.."_"..#params] or map_op[op.."_*"] - if not f then - if not g_arch then wfatal("first statement must be .arch") end - -- Improve error report. - for i=0,9 do - if map_op[op.."_"..i] then - werror("wrong number of parameters for `"..op.."'") - end - end - werror("unknown statement `"..op.."'") - end - - -- Call opcode handler or special handler for template strings. - if type(f) == "string" then - map_op[".template__"](params, f) - else - f(params) - end -end - --- Process a single line. -local function doline(line) - if g_opt.flushline then wflush() end - - -- Assembler line? - local indent, aline = match(line, "^(%s*)%|(.*)$") - if not aline then - -- No, plain C code line, need to flush first. - wflush() - wsync() - wline(line, false) - return - end - - g_indent = indent -- Remember current line indentation. - - -- Emit C code (even from macros). Avoids echo and line parsing. - if sub(aline, 1, 1) == "|" then - if not mac_capture then - wsync() - elseif g_opt.comment then - wsync() - wcomment(aline) - end - dostmt(aline) - return - end - - -- Echo assembler line as a comment. - if g_opt.comment then - wsync() - wcomment(aline) - end - - -- Strip assembler comments. - aline = gsub(aline, "//.*$", "") - - -- Split line into statements at semicolons. - if match(aline, ";") then - for stmt in gmatch(aline, "[^;]+") do dostmt(stmt) end - else - dostmt(aline) - end -end - ------------------------------------------------------------------------------- - --- Write DynASM header. -local function dasmhead(out) - out:write(format([[ -/* -** This file has been pre-processed with DynASM. -** %s -** DynASM version %s, DynASM %s version %s -** DO NOT EDIT! The original file is in "%s". -*/ - -#if DASM_VERSION != %d -#error "Version mismatch between DynASM and included encoding engine" -#endif - -]], _info.url, - _info.version, g_arch._info.arch, g_arch._info.version, - g_fname, _info.vernum)) -end - --- Read input file. -readfile = function(fin) - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Process all lines. - for line in fin:lines() do - g_lineno = g_lineno + 1 - g_curline = line - local ok, err = pcall(doline, line) - if not ok and wprinterr(err, "\n") then return true end - end - wflush() - - -- Close input file. - assert(fin == stdin or fin:close()) -end - --- Write output file. -local function writefile(outfile) - local fout - - -- Open output file. - if outfile == nil or outfile == "-" then - fout = stdout - else - fout = assert(io.open(outfile, "w")) - end - - -- Write all buffered lines - wdumplines(fout, g_wbuffer) - - -- Close output file. - assert(fout == stdout or fout:close()) - - -- Optionally dump definitions. - dumpdef(fout == stdout and stderr or stdout) -end - --- Translate an input file to an output file. -local function translate(infile, outfile) - g_wbuffer = {} - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Put header. - wline(dasmhead) - - -- Read input file. - local fin - if infile == "-" then - g_fname = "(stdin)" - fin = stdin - else - g_fname = infile - fin = assert(io.open(infile, "r")) - end - readfile(fin) - - -- Check for errors. - if not g_arch then - wprinterr(g_fname, ":*: error: missing .arch directive\n") - end - checkconds() - checkmacros() - checkcaptures() - - if g_errcount ~= 0 then - stderr:write(g_fname, ":*: info: ", g_errcount, " error", - (type(g_errcount) == "number" and g_errcount > 1) and "s" or "", - " in input file -- no output file generated.\n") - dumpdef(stderr) - exit(1) - end - - -- Write output file. - writefile(outfile) -end - ------------------------------------------------------------------------------- - --- Print help text. -function opt_map.help() - stdout:write("DynASM -- ", _info.description, ".\n") - stdout:write("DynASM ", _info.version, " ", _info.release, " ", _info.url, "\n") - stdout:write[[ - -Usage: dynasm [OPTION]... INFILE.dasc|- - - -h, --help Display this help text. - -V, --version Display version and copyright information. - - -o, --outfile FILE Output file name (default is stdout). - -I, --include DIR Add directory to the include search path. - - -c, --ccomment Use /* */ comments for assembler lines. - -C, --cppcomment Use // comments for assembler lines (default). - -N, --nocomment Suppress assembler lines in output. - -M, --maccomment Show macro expansions as comments (default off). - - -L, --nolineno Suppress CPP line number information in output. - -F, --flushline Flush action list for every line. - - -D NAME[=SUBST] Define a substitution. - -U NAME Undefine a substitution. - - -P, --dumpdef Dump defines, macros, etc. Repeat for more output. - -A, --dumparch ARCH Load architecture ARCH and dump description. -]] - exit(0) -end - --- Print version information. -function opt_map.version() - stdout:write(format("%s version %s, released %s\n%s\n\n%s", - _info.name, _info.version, _info.release, _info.url, _info.copyright)) - exit(0) -end - --- Misc. options. -function opt_map.outfile(args) g_opt.outfile = optparam(args) end -function opt_map.include(args) insert(g_opt.include, 1, optparam(args)) end -function opt_map.ccomment() g_opt.comment = "/*|"; g_opt.endcomment = " */" end -function opt_map.cppcomment() g_opt.comment = "//|"; g_opt.endcomment = "" end -function opt_map.nocomment() g_opt.comment = false end -function opt_map.maccomment() g_opt.maccomment = true end -function opt_map.nolineno() g_opt.cpp = false end -function opt_map.flushline() g_opt.flushline = true end -function opt_map.dumpdef() g_opt.dumpdef = g_opt.dumpdef + 1 end - ------------------------------------------------------------------------------- - --- Short aliases for long options. -local opt_alias = { - h = "help", ["?"] = "help", V = "version", - o = "outfile", I = "include", - c = "ccomment", C = "cppcomment", N = "nocomment", M = "maccomment", - L = "nolineno", F = "flushline", - P = "dumpdef", A = "dumparch", -} - --- Parse single option. -local function parseopt(opt, args) - opt_current = #opt == 1 and "-"..opt or "--"..opt - local f = opt_map[opt] or opt_map[opt_alias[opt]] - if not f then - opterror("unrecognized option `", opt_current, "'. Try `--help'.\n") - end - f(args) -end - --- Parse arguments. -local function parseargs(args) - -- Default options. - g_opt.comment = "//|" - g_opt.endcomment = "" - g_opt.cpp = true - g_opt.dumpdef = 0 - g_opt.include = { "" } - - -- Process all option arguments. - args.argn = 1 - repeat - local a = args[args.argn] - if not a then break end - local lopt, opt = match(a, "^%-(%-?)(.+)") - if not opt then break end - args.argn = args.argn + 1 - if lopt == "" then - -- Loop through short options. - for o in gmatch(opt, ".") do parseopt(o, args) end - else - -- Long option. - parseopt(opt, args) - end - until false - - -- Check for proper number of arguments. - local nargs = #args - args.argn + 1 - if nargs ~= 1 then - if nargs == 0 then - if g_opt.dumpdef > 0 then return dumpdef(stdout) end - end - opt_map.help() - end - - -- Translate a single input file to a single output file - -- TODO: Handle multiple files? - translate(args[args.argn], g_opt.outfile) -end - ------------------------------------------------------------------------------- - --- Add the directory dynasm.lua resides in to the Lua module search path. -local arg = arg -if arg and arg[0] then - prefix = match(arg[0], "^(.*[/\\])") - if package and prefix then package.path = prefix.."?.lua;"..package.path end -end - --- Start DynASM. -parseargs{...} - ------------------------------------------------------------------------------- - diff --git a/third-party/LuaJIT-2.0.2/etc/luajit.1 b/third-party/LuaJIT-2.0.2/etc/luajit.1 deleted file mode 100644 index 6489bd3b53..0000000000 --- a/third-party/LuaJIT-2.0.2/etc/luajit.1 +++ /dev/null @@ -1,88 +0,0 @@ -.TH luajit 1 "" "" "LuaJIT documentation" -.SH NAME -luajit \- Just-In-Time Compiler for the Lua Language -\fB -.SH SYNOPSIS -.B luajit -[\fIoptions\fR]... [\fIscript\fR [\fIargs\fR]...] -.SH "WEB SITE" -.IR http://luajit.org -.SH DESCRIPTION -.PP -This is the command-line program to run Lua programs with \fBLuaJIT\fR. -.PP -\fBLuaJIT\fR is a just-in-time (JIT) compiler for the Lua language. -The virtual machine (VM) is based on a fast interpreter combined with -a trace compiler. It can significantly improve the performance of Lua programs. -.PP -\fBLuaJIT\fR is API\- and ABI-compatible with the VM of the standard -Lua\ 5.1 interpreter. When embedding the VM into an application, -the built library can be used as a drop-in replacement. -.SH OPTIONS -.TP -.BI "\-e " chunk -Run the given chunk of Lua code. -.TP -.BI "\-l " library -Load the named library, just like \fBrequire("\fR\fIlibrary\fR\fB")\fR. -.TP -.BI "\-b " ... -Save or list bytecode. Run without arguments to get help on options. -.TP -.BI "\-j " command -Perform LuaJIT control command (optional space after \fB\-j\fR). -.TP -.BI "\-O" [opt] -Control LuaJIT optimizations. -.TP -.B "\-i" -Run in interactive mode. -.TP -.B "\-v" -Show \fBLuaJIT\fR version. -.TP -.B "\-E" -Ignore environment variables. -.TP -.B "\-\-" -Stop processing options. -.TP -.B "\-" -Read script from stdin instead. -.PP -After all options are processed, the given \fIscript\fR is run. -The arguments are passed in the global \fIarg\fR table. -.PP -Interactive mode is only entered, if no \fIscript\fR and no \fB\-e\fR -option is given. Interactive mode can be left with EOF (\fICtrl\-Z\fB). -.SH EXAMPLES -.TP -luajit hello.lua world - -Prints "Hello world", assuming \fIhello.lua\fR contains: -.br - print("Hello", arg[1]) -.TP -luajit \-e "local x=0; for i=1,1e9 do x=x+i end; print(x)" - -Calculates the sum of the numbers from 1 to 1000000000. -.br -And finishes in a reasonable amount of time, too. -.TP -luajit \-jv \-e "for i=1,10 do for j=1,10 do for k=1,100 do end end end" - -Runs some nested loops and shows the resulting traces. -.SH COPYRIGHT -.PP -\fBLuaJIT\fR is Copyright \(co 2005-2013 Mike Pall. -.br -\fBLuaJIT\fR is open source software, released under the MIT license. -.SH SEE ALSO -.PP -More details in the provided HTML docs or at: -.IR http://luajit.org -.br -More about the Lua language can be found at: -.IR http://lua.org/docs.html -.PP -lua(1) diff --git a/third-party/LuaJIT-2.0.2/etc/luajit.pc b/third-party/LuaJIT-2.0.2/etc/luajit.pc deleted file mode 100644 index 5a982a627d..0000000000 --- a/third-party/LuaJIT-2.0.2/etc/luajit.pc +++ /dev/null @@ -1,24 +0,0 @@ -# Package information for LuaJIT to be used by pkg-config. -majver=2 -minver=0 -relver=2 -version=${majver}.${minver}.${relver} -abiver=5.1 - -prefix=/usr/local -exec_prefix=${prefix} -libdir=${exec_prefix}/lib -libname=luajit-${abiver} -includedir=${prefix}/include/luajit-${majver}.${minver} - -INSTALL_LMOD=${prefix}/share/lua/${abiver} -INSTALL_CMOD=${prefix}/lib/lua/${abiver} - -Name: LuaJIT -Description: Just-in-time compiler for Lua -URL: http://luajit.org -Version: ${version} -Requires: -Libs: -L${libdir} -l${libname} -Libs.private: -Wl,-E -lm -ldl -Cflags: -I${includedir} diff --git a/third-party/LuaJIT-2.0.2/src/.gitignore b/third-party/LuaJIT-2.0.2/src/.gitignore deleted file mode 100644 index dceeb6542a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -/libluajit.a -/libluajit.so -/lj_bcdef.h -/lj_ffdef.h -/lj_folddef.h -/lj_libdef.h -/lj_recdef.h -/lj_vm.s -/luajit diff --git a/third-party/LuaJIT-2.0.2/src/Makefile b/third-party/LuaJIT-2.0.2/src/Makefile deleted file mode 100644 index 999e2808dc..0000000000 --- a/third-party/LuaJIT-2.0.2/src/Makefile +++ /dev/null @@ -1,677 +0,0 @@ -############################################################################## -# LuaJIT Makefile. Requires GNU Make. -# -# Please read doc/install.html before changing any variables! -# -# Suitable for POSIX platforms (Linux, *BSD, OSX etc.). -# Also works with MinGW and Cygwin on Windows. -# Please check msvcbuild.bat for building with MSVC on Windows. -# -# Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h -############################################################################## - -MAJVER= 2 -MINVER= 0 -RELVER= 2 -ABIVER= 5.1 -NODOTABIVER= 51 - -############################################################################## -############################# COMPILER OPTIONS ############################# -############################################################################## -# These options mainly affect the speed of the JIT compiler itself, not the -# speed of the JIT-compiled code. Turn any of the optional settings on by -# removing the '#' in front of them. Make sure you force a full recompile -# with "make clean", followed by "make" if you change any options. -# -# LuaJIT builds as a native 32 or 64 bit binary by default. -CC= gcc -# -# Use this if you want to force a 32 bit build on a 64 bit multilib OS. -#CC= gcc -m32 -# -# Since the assembler part does NOT maintain a frame pointer, it's pointless -# to slow down the C part by not omitting it. Debugging, tracebacks and -# unwinding are not affected -- the assembler part has frame unwind -# information and GCC emits it where needed (x64) or with -g (see CCDEBUG). -CCOPT= -O2 -fomit-frame-pointer -# Use this if you want to generate a smaller binary (but it's slower): -#CCOPT= -Os -fomit-frame-pointer -# Note: it's no longer recommended to use -O3 with GCC 4.x. -# The I-Cache bloat usually outweighs the benefits from aggressive inlining. -# -# Target-specific compiler options: -# -# x86 only: it's recommended to compile at least for i686. Better yet, -# compile for an architecture that has SSE2, too (-msse -msse2). -# -# x86/x64 only: For GCC 4.2 or higher and if you don't intend to distribute -# the binaries to a different machine you could also use: -march=native -# -CCOPT_x86= -march=i686 -CCOPT_x64= -CCOPT_arm= -CCOPT_ppc= -CCOPT_ppcspe= -CCOPT_mips= -# -CCDEBUG= -# Uncomment the next line to generate debug information: -#CCDEBUG= -g -# -CCWARN= -Wall -# Uncomment the next line to enable more warnings: -#CCWARN+= -Wextra -Wdeclaration-after-statement -Wredundant-decls -Wshadow -Wpointer-arith -# -############################################################################## - -############################################################################## -################################ BUILD MODE ################################ -############################################################################## -# The default build mode is mixed mode on POSIX. On Windows this is the same -# as dynamic mode. -# -# Mixed mode creates a static + dynamic library and a statically linked luajit. -BUILDMODE= mixed -# -# Static mode creates a static library and a statically linked luajit. -#BUILDMODE= static -# -# Dynamic mode creates a dynamic library and a dynamically linked luajit. -# Note: this executable will only run when the library is installed! -#BUILDMODE= dynamic -# -############################################################################## - -############################################################################## -################################# FEATURES ################################# -############################################################################## -# Enable/disable these features as needed, but make sure you force a full -# recompile with "make clean", followed by "make". -XCFLAGS= -# -# Permanently disable the FFI extension to reduce the size of the LuaJIT -# executable. But please consider that the FFI library is compiled-in, -# but NOT loaded by default. It only allocates any memory, if you actually -# make use of it. -#XCFLAGS+= -DLUAJIT_DISABLE_FFI -# -# Features from Lua 5.2 that are unlikely to break existing code are -# enabled by default. Some other features that *might* break some existing -# code (e.g. __pairs or os.execute() return values) can be enabled here. -# Note: this does not provide full compatibility with Lua 5.2 at this time. -#XCFLAGS+= -DLUAJIT_ENABLE_LUA52COMPAT -# -# Disable the JIT compiler, i.e. turn LuaJIT into a pure interpreter. -#XCFLAGS+= -DLUAJIT_DISABLE_JIT -# -# Some architectures (e.g. PPC) can use either single-number (1) or -# dual-number (2) mode. Uncomment one of these lines to override the -# default mode. Please see LJ_ARCH_NUMMODE in lj_arch.h for details. -#XCFLAGS+= -DLUAJIT_NUMMODE=1 -#XCFLAGS+= -DLUAJIT_NUMMODE=2 -# -############################################################################## - -############################################################################## -############################ DEBUGGING SUPPORT ############################# -############################################################################## -# Enable these options as needed, but make sure you force a full recompile -# with "make clean", followed by "make". -# Note that most of these are NOT suitable for benchmarking or release mode! -# -# Use the system provided memory allocator (realloc) instead of the -# bundled memory allocator. This is slower, but sometimes helpful for -# debugging. It's helpful for Valgrind's memcheck tool, too. This option -# cannot be enabled on x64, since the built-in allocator is mandatory. -#XCFLAGS+= -DLUAJIT_USE_SYSMALLOC -# -# This define is required to run LuaJIT under Valgrind. The Valgrind -# header files must be installed. You should enable debug information, too. -# Use --suppressions=lj.supp to avoid some false positives. -#XCFLAGS+= -DLUAJIT_USE_VALGRIND -# -# This is the client for the GDB JIT API. GDB 7.0 or higher is required -# to make use of it. See lj_gdbjit.c for details. Enabling this causes -# a non-negligible overhead, even when not running under GDB. -#XCFLAGS+= -DLUAJIT_USE_GDBJIT -# -# Turn on assertions for the Lua/C API to debug problems with lua_* calls. -# This is rather slow -- use only while developing C libraries/embeddings. -#XCFLAGS+= -DLUA_USE_APICHECK -# -# Turn on assertions for the whole LuaJIT VM. This significantly slows down -# everything. Use only if you suspect a problem with LuaJIT itself. -#XCFLAGS+= -DLUA_USE_ASSERT -# -############################################################################## -# You probably don't need to change anything below this line! -############################################################################## - -############################################################################## -# Flags and options for host and target. -############################################################################## - -# You can override the following variables at the make command line: -# CC HOST_CC STATIC_CC DYNAMIC_CC -# CFLAGS HOST_CFLAGS TARGET_CFLAGS -# LDFLAGS HOST_LDFLAGS TARGET_LDFLAGS TARGET_SHLDFLAGS -# LIBS HOST_LIBS TARGET_LIBS -# CROSS HOST_SYS TARGET_SYS TARGET_FLAGS -# -# Cross-compilation examples: -# make HOST_CC="gcc -m32" CROSS=i586-mingw32msvc- TARGET_SYS=Windows -# make HOST_CC="gcc -m32" CROSS=powerpc-linux-gnu- - -CCOPTIONS= $(CCDEBUG) $(CCOPT) $(CCWARN) $(XCFLAGS) $(CFLAGS) -LDOPTIONS= $(CCDEBUG) $(LDFLAGS) - -HOST_CC= $(CC) -HOST_RM= rm -f -# If left blank, minilua is built and used. You can supply an installed -# copy of (plain) Lua 5.1 or 5.2, plus Lua BitOp. E.g. with: HOST_LUA=lua -HOST_LUA= - -HOST_XCFLAGS= -I. -HOST_XLDFLAGS= -HOST_XLIBS= -HOST_ACFLAGS= $(CCOPTIONS) $(HOST_XCFLAGS) $(TARGET_ARCH) $(HOST_CFLAGS) -HOST_ALDFLAGS= $(LDOPTIONS) $(HOST_XLDFLAGS) $(HOST_LDFLAGS) -HOST_ALIBS= $(HOST_XLIBS) $(LIBS) $(HOST_LIBS) - -STATIC_CC = $(CROSS)$(CC) -DYNAMIC_CC = $(CROSS)$(CC) -fPIC -TARGET_CC= $(STATIC_CC) -TARGET_STCC= $(STATIC_CC) -TARGET_DYNCC= $(DYNAMIC_CC) -TARGET_LD= $(CROSS)$(CC) -TARGET_AR= $(CROSS)ar rcus -TARGET_STRIP= $(CROSS)strip - -TARGET_SONAME= libluajit-$(ABIVER).so.$(MAJVER) -TARGET_DYLIBNAME= libluajit-$(ABIVER).$(MAJVER).dylib -TARGET_DYLIBPATH= $(or $(PREFIX),/usr/local)/lib/$(TARGET_DYLIBNAME) -TARGET_DLLNAME= lua$(NODOTABIVER).dll -TARGET_XSHLDFLAGS= -shared -fPIC -Wl,-soname,$(TARGET_SONAME) -TARGET_DYNXLDOPTS= - -TARGET_LFSFLAGS= -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -TARGET_XCFLAGS= $(TARGET_LFSFLAGS) -U_FORTIFY_SOURCE -TARGET_XLDFLAGS= -TARGET_XLIBS= -lm -TARGET_TCFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS) -TARGET_ACFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS) -TARGET_ALDFLAGS= $(LDOPTIONS) $(TARGET_XLDFLAGS) $(TARGET_FLAGS) $(TARGET_LDFLAGS) -TARGET_ASHLDFLAGS= $(LDOPTIONS) $(TARGET_XSHLDFLAGS) $(TARGET_FLAGS) $(TARGET_SHLDFLAGS) -TARGET_ALIBS= $(TARGET_XLIBS) $(LIBS) $(TARGET_LIBS) - -TARGET_TESTARCH=$(shell $(TARGET_CC) $(TARGET_TCFLAGS) -E lj_arch.h -dM) -ifneq (,$(findstring LJ_TARGET_X64 ,$(TARGET_TESTARCH))) - TARGET_LJARCH= x64 -else -ifneq (,$(findstring LJ_TARGET_X86 ,$(TARGET_TESTARCH))) - TARGET_LJARCH= x86 -else -ifneq (,$(findstring LJ_TARGET_ARM ,$(TARGET_TESTARCH))) - TARGET_LJARCH= arm -else -ifneq (,$(findstring LJ_TARGET_PPC ,$(TARGET_TESTARCH))) - TARGET_LJARCH= ppc -else -ifneq (,$(findstring LJ_TARGET_PPCSPE ,$(TARGET_TESTARCH))) - TARGET_LJARCH= ppcspe -else -ifneq (,$(findstring LJ_TARGET_MIPS ,$(TARGET_TESTARCH))) - ifneq (,$(findstring MIPSEL ,$(TARGET_TESTARCH))) - TARGET_ARCH= -D__MIPSEL__=1 - endif - TARGET_LJARCH= mips -else - $(error Unsupported target architecture) -endif -endif -endif -endif -endif -endif - -ifneq (,$(findstring LJ_TARGET_PS3 1,$(TARGET_TESTARCH))) - TARGET_SYS= PS3 - TARGET_ARCH+= -D__CELLOS_LV2__ - TARGET_XCFLAGS+= -DLUAJIT_USE_SYSMALLOC -endif -ifneq (,$(findstring LJ_NO_UNWIND 1,$(TARGET_TESTARCH))) - TARGET_ARCH+= -DLUAJIT_NO_UNWIND -endif - -TARGET_XCFLAGS+= $(CCOPT_$(TARGET_LJARCH)) -TARGET_ARCH+= $(patsubst %,-DLUAJIT_TARGET=LUAJIT_ARCH_%,$(TARGET_LJARCH)) - -ifneq (,$(PREFIX)) -ifneq (/usr/local,$(PREFIX)) - TARGET_XCFLAGS+= -DLUA_XROOT=\"$(PREFIX)/\" - ifneq (/usr,$(PREFIX)) - TARGET_DYNXLDOPTS= -Wl,-rpath,$(PREFIX)/lib - endif -endif -endif - -############################################################################## -# System detection. -############################################################################## - -ifeq (Windows,$(findstring Windows,$(OS))$(MSYSTEM)$(TERM)) - HOST_SYS= Windows - HOST_RM= del -else - HOST_SYS:= $(shell uname -s) - ifneq (,$(findstring MINGW,$(HOST_SYS))) - HOST_SYS= Windows - HOST_MSYS= mingw - endif - ifneq (,$(findstring CYGWIN,$(HOST_SYS))) - HOST_SYS= Windows - HOST_MSYS= cygwin - endif -endif - -TARGET_SYS?= $(HOST_SYS) -ifeq (Windows,$(TARGET_SYS)) - TARGET_STRIP+= --strip-unneeded - TARGET_XSHLDFLAGS= -shared - TARGET_DYNXLDOPTS= -else -ifeq (Darwin,$(TARGET_SYS)) - ifeq (,$(MACOSX_DEPLOYMENT_TARGET)) - export MACOSX_DEPLOYMENT_TARGET=10.4 - endif - TARGET_STRIP+= -x - TARGET_AR+= 2>/dev/null - TARGET_XCFLAGS+= -fno-stack-protector - TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup -fPIC - TARGET_DYNXLDOPTS= - TARGET_XSHLDFLAGS+= -install_name $(TARGET_DYLIBPATH) -compatibility_version $(MAJVER).$(MINVER) -current_version $(MAJVER).$(MINVER).$(RELVER) - ifeq (x64,$(TARGET_LJARCH)) - TARGET_XLDFLAGS+= -pagezero_size 10000 -image_base 100000000 - TARGET_XSHLDFLAGS+= -image_base 7fff04c4a000 - endif -else -ifeq (iOS,$(TARGET_SYS)) - TARGET_STRIP+= -x - TARGET_AR+= 2>/dev/null - TARGET_XCFLAGS+= -fno-stack-protector - TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup -fPIC - TARGET_DYNXLDOPTS= - TARGET_XSHLDFLAGS+= -install_name $(TARGET_DYLIBPATH) -compatibility_version $(MAJVER).$(MINVER) -current_version $(MAJVER).$(MINVER).$(RELVER) -else - ifneq (,$(findstring stack-protector,$(shell $(TARGET_CC) -dumpspecs))) - TARGET_XCFLAGS+= -fno-stack-protector - endif - ifneq (SunOS,$(TARGET_SYS)) - ifneq (PS3,$(TARGET_SYS)) - TARGET_XLDFLAGS+= -Wl,-E - endif - endif - ifeq (Linux,$(TARGET_SYS)) - TARGET_XLIBS+= -ldl - endif - ifeq (GNU/kFreeBSD,$(TARGET_SYS)) - TARGET_XLIBS+= -ldl - endif -endif -endif -endif - -ifneq ($(HOST_SYS),$(TARGET_SYS)) - ifeq (Windows,$(TARGET_SYS)) - HOST_XCFLAGS+= -malign-double -DLUAJIT_OS=LUAJIT_OS_WINDOWS - else - ifeq (Linux,$(TARGET_SYS)) - HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_LINUX - else - ifeq (Darwin,$(TARGET_SYS)) - HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OSX - else - ifeq (iOS,$(TARGET_SYS)) - HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OSX - else - HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OTHER - endif - endif - endif - endif -endif - -ifneq (,$(CCDEBUG)) - TARGET_STRIP= @: -endif - -############################################################################## -# Files and pathnames. -############################################################################## - -MINILUA_O= host/minilua.o -MINILUA_LIBS= -lm -MINILUA_T= host/minilua -MINILUA_X= $(MINILUA_T) - -ifeq (,$(HOST_LUA)) - HOST_LUA= $(MINILUA_X) - DASM_DEP= $(MINILUA_T) -endif - -DASM_DIR= ../dynasm -DASM= $(HOST_LUA) $(DASM_DIR)/dynasm.lua -DASM_XFLAGS= -DASM_AFLAGS= -DASM_ARCH= $(TARGET_LJARCH) - -ifneq (,$(findstring LJ_ARCH_BITS 64,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D P64 -endif -ifneq (,$(findstring LJ_HASJIT 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D JIT -endif -ifneq (,$(findstring LJ_HASFFI 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D FFI -endif -ifneq (,$(findstring LJ_DUALNUM 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D DUALNUM -endif -ifneq (,$(findstring LJ_ARCH_HASFPU 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D FPU - TARGET_ARCH+= -DLJ_ARCH_HASFPU=1 -else - TARGET_ARCH+= -DLJ_ARCH_HASFPU=0 -endif -ifeq (,$(findstring LJ_ABI_SOFTFP 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D HFABI - TARGET_ARCH+= -DLJ_ABI_SOFTFP=0 -else - TARGET_ARCH+= -DLJ_ABI_SOFTFP=1 -endif -DASM_AFLAGS+= -D VER=$(subst LJ_ARCH_VERSION_,,$(filter LJ_ARCH_VERSION_%,$(subst LJ_ARCH_VERSION ,LJ_ARCH_VERSION_,$(TARGET_TESTARCH)))) -ifeq (Windows,$(TARGET_SYS)) - DASM_AFLAGS+= -D WIN -endif -ifeq (x86,$(TARGET_LJARCH)) - ifneq (,$(findstring __SSE2__ 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D SSE - endif -else -ifeq (x64,$(TARGET_LJARCH)) - DASM_ARCH= x86 -else -ifeq (arm,$(TARGET_LJARCH)) - ifeq (iOS,$(TARGET_SYS)) - DASM_AFLAGS+= -D IOS - endif -else -ifeq (ppc,$(TARGET_LJARCH)) - ifneq (,$(findstring LJ_ARCH_SQRT 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D SQRT - endif - ifneq (,$(findstring LJ_ARCH_ROUND 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D ROUND - endif - ifneq (,$(findstring LJ_ARCH_PPC64 1,$(TARGET_TESTARCH))) - DASM_AFLAGS+= -D GPR64 - endif - ifeq (PS3,$(TARGET_SYS)) - DASM_AFLAGS+= -D PPE -D TOC - endif -endif -endif -endif -endif - -DASM_FLAGS= $(DASM_XFLAGS) $(DASM_AFLAGS) -DASM_DASC= vm_$(DASM_ARCH).dasc - -BUILDVM_O= host/buildvm.o host/buildvm_asm.o host/buildvm_peobj.o \ - host/buildvm_lib.o host/buildvm_fold.o -BUILDVM_T= host/buildvm -BUILDVM_X= $(BUILDVM_T) - -HOST_O= $(MINILUA_O) $(BUILDVM_O) -HOST_T= $(MINILUA_T) $(BUILDVM_T) - -LJVM_S= lj_vm.s -LJVM_O= lj_vm.o -LJVM_BOUT= $(LJVM_S) -LJVM_MODE= elfasm - -LJLIB_O= lib_base.o lib_math.o lib_bit.o lib_string.o lib_table.o \ - lib_io.o lib_os.o lib_package.o lib_debug.o lib_jit.o lib_ffi.o -LJLIB_C= $(LJLIB_O:.o=.c) - -LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \ - lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \ - lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \ - lj_api.o lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \ - lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ - lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \ - lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \ - lj_asm.o lj_trace.o lj_gdbjit.o \ - lj_ctype.o lj_cdata.o lj_cconv.o lj_ccall.o lj_ccallback.o \ - lj_carith.o lj_clib.o lj_cparse.o \ - lj_lib.o lj_alloc.o lib_aux.o \ - $(LJLIB_O) lib_init.o - -LJVMCORE_O= $(LJVM_O) $(LJCORE_O) -LJVMCORE_DYNO= $(LJVMCORE_O:.o=_dyn.o) - -LIB_VMDEF= jit/vmdef.lua -LIB_VMDEFP= $(LIB_VMDEF) - -LUAJIT_O= luajit.o -LUAJIT_A= libluajit.a -LUAJIT_SO= libluajit.so -LUAJIT_T= luajit - -ALL_T= $(LUAJIT_T) $(LUAJIT_A) $(LUAJIT_SO) $(HOST_T) -ALL_HDRGEN= lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h \ - host/buildvm_arch.h -ALL_GEN= $(LJVM_S) $(ALL_HDRGEN) $(LIB_VMDEFP) -WIN_RM= *.obj *.lib *.exp *.dll *.exe *.manifest *.pdb *.ilk -ALL_RM= $(ALL_T) $(ALL_GEN) *.o host/*.o $(WIN_RM) - -############################################################################## -# Build mode handling. -############################################################################## - -# Mixed mode defaults. -TARGET_O= $(LUAJIT_A) -TARGET_T= $(LUAJIT_T) $(LUAJIT_SO) -TARGET_DEP= $(LIB_VMDEF) $(LUAJIT_SO) - -ifeq (Windows,$(TARGET_SYS)) - TARGET_DYNCC= $(STATIC_CC) - LJVM_MODE= peobj - LJVM_BOUT= $(LJVM_O) - LUAJIT_T= luajit.exe - ifeq (cygwin,$(HOST_MSYS)) - LUAJIT_SO= cyg$(TARGET_DLLNAME) - else - LUAJIT_SO= $(TARGET_DLLNAME) - endif - # Mixed mode is not supported on Windows. And static mode doesn't work well. - # C modules cannot be loaded, because they bind to lua51.dll. - ifneq (static,$(BUILDMODE)) - BUILDMODE= dynamic - TARGET_XCFLAGS+= -DLUA_BUILD_AS_DLL - endif -endif -ifeq (Darwin,$(TARGET_SYS)) - LJVM_MODE= machasm -endif -ifeq (iOS,$(TARGET_SYS)) - LJVM_MODE= machasm -endif -ifeq (SunOS,$(TARGET_SYS)) - BUILDMODE= static -endif -ifeq (PS3,$(TARGET_SYS)) - BUILDMODE= static -endif - -ifeq (Windows,$(HOST_SYS)) - MINILUA_T= host/minilua.exe - BUILDVM_T= host/buildvm.exe - ifeq (,$(HOST_MSYS)) - MINILUA_X= host\minilua - BUILDVM_X= host\buildvm - ALL_RM:= $(subst /,\,$(ALL_RM)) - endif -endif - -ifeq (static,$(BUILDMODE)) - TARGET_DYNCC= @: - TARGET_T= $(LUAJIT_T) - TARGET_DEP= $(LIB_VMDEF) -else -ifeq (dynamic,$(BUILDMODE)) - ifneq (Windows,$(TARGET_SYS)) - TARGET_CC= $(DYNAMIC_CC) - endif - TARGET_DYNCC= @: - LJVMCORE_DYNO= $(LJVMCORE_O) - TARGET_O= $(LUAJIT_SO) - TARGET_XLDFLAGS+= $(TARGET_DYNXLDOPTS) -else -ifeq (Darwin,$(TARGET_SYS)) - TARGET_DYNCC= @: - LJVMCORE_DYNO= $(LJVMCORE_O) -endif -ifeq (iOS,$(TARGET_SYS)) - TARGET_DYNCC= @: - LJVMCORE_DYNO= $(LJVMCORE_O) -endif -endif -endif - -Q= @ -E= @echo -#Q= -#E= @: - -############################################################################## -# Make targets. -############################################################################## - -default all: $(TARGET_T) - -amalg: - @grep "^[+|]" ljamalg.c - $(MAKE) all "LJCORE_O=ljamalg.o" - -clean: - $(HOST_RM) $(ALL_RM) - -depend: - @for file in $(ALL_HDRGEN); do \ - test -f $$file || touch $$file; \ - done - @$(HOST_CC) $(HOST_ACFLAGS) -MM *.c host/*.c | \ - sed -e "s| [^ ]*/dasm_\S*\.h||g" \ - -e "s|^\([^l ]\)|host/\1|" \ - -e "s| lj_target_\S*\.h| lj_target_*.h|g" \ - -e "s| lj_emit_\S*\.h| lj_emit_*.h|g" \ - -e "s| lj_asm_\S*\.h| lj_asm_*.h|g" >Makefile.dep - @for file in $(ALL_HDRGEN); do \ - test -s $$file || $(HOST_RM) $$file; \ - done - -.PHONY: default all amalg clean depend - -############################################################################## -# Rules for generated files. -############################################################################## - -$(MINILUA_T): $(MINILUA_O) - $(E) "HOSTLINK $@" - $(Q)$(HOST_CC) $(HOST_ALDFLAGS) -o $@ $(MINILUA_O) $(MINILUA_LIBS) $(HOST_ALIBS) - -host/buildvm_arch.h: $(DASM_DASC) $(DASM_DEP) - $(E) "DYNASM $@" - $(Q)$(DASM) $(DASM_FLAGS) -o $@ $(DASM_DASC) - -host/buildvm.o: $(DASM_DIR)/dasm_*.h - -$(BUILDVM_T): $(BUILDVM_O) - $(E) "HOSTLINK $@" - $(Q)$(HOST_CC) $(HOST_ALDFLAGS) -o $@ $(BUILDVM_O) $(HOST_ALIBS) - -$(LJVM_BOUT): $(BUILDVM_T) - $(E) "BUILDVM $@" - $(Q)$(BUILDVM_X) -m $(LJVM_MODE) -o $@ - -lj_bcdef.h: $(BUILDVM_T) $(LJLIB_C) - $(E) "BUILDVM $@" - $(Q)$(BUILDVM_X) -m bcdef -o $@ $(LJLIB_C) - -lj_ffdef.h: $(BUILDVM_T) $(LJLIB_C) - $(E) "BUILDVM $@" - $(Q)$(BUILDVM_X) -m ffdef -o $@ $(LJLIB_C) - -lj_libdef.h: $(BUILDVM_T) $(LJLIB_C) - $(E) "BUILDVM $@" - $(Q)$(BUILDVM_X) -m libdef -o $@ $(LJLIB_C) - -lj_recdef.h: $(BUILDVM_T) $(LJLIB_C) - $(E) "BUILDVM $@" - $(Q)$(BUILDVM_X) -m recdef -o $@ $(LJLIB_C) - -$(LIB_VMDEF): $(BUILDVM_T) $(LJLIB_C) - $(E) "BUILDVM $@" - $(Q)$(BUILDVM_X) -m vmdef -o $(LIB_VMDEFP) $(LJLIB_C) - -lj_folddef.h: $(BUILDVM_T) lj_opt_fold.c - $(E) "BUILDVM $@" - $(Q)$(BUILDVM_X) -m folddef -o $@ lj_opt_fold.c - -############################################################################## -# Object file rules. -############################################################################## - -%.o: %.c - $(E) "CC $@" - $(Q)$(TARGET_DYNCC) $(TARGET_ACFLAGS) -c -o $(@:.o=_dyn.o) $< - $(Q)$(TARGET_CC) $(TARGET_ACFLAGS) -c -o $@ $< - -%.o: %.s - $(E) "ASM $@" - $(Q)$(TARGET_DYNCC) $(TARGET_ACFLAGS) -c -o $(@:.o=_dyn.o) $< - $(Q)$(TARGET_CC) $(TARGET_ACFLAGS) -c -o $@ $< - -$(LUAJIT_O): - $(E) "CC $@" - $(Q)$(TARGET_STCC) $(TARGET_ACFLAGS) -c -o $@ $< - -$(HOST_O): %.o: %.c - $(E) "HOSTCC $@" - $(Q)$(HOST_CC) $(HOST_ACFLAGS) -c -o $@ $< - -include Makefile.dep - -############################################################################## -# Target file rules. -############################################################################## - -$(LUAJIT_A): $(LJVMCORE_O) - $(E) "AR $@" - $(Q)$(TARGET_AR) $@ $(LJVMCORE_O) - -# The dependency on _O, but linking with _DYNO is intentional. -$(LUAJIT_SO): $(LJVMCORE_O) - $(E) "DYNLINK $@" - $(Q)$(TARGET_LD) $(TARGET_ASHLDFLAGS) -o $@ $(LJVMCORE_DYNO) $(TARGET_ALIBS) - $(Q)$(TARGET_STRIP) $@ - -$(LUAJIT_T): $(TARGET_O) $(LUAJIT_O) $(TARGET_DEP) - $(E) "LINK $@" - $(Q)$(TARGET_LD) $(TARGET_ALDFLAGS) -o $@ $(LUAJIT_O) $(TARGET_O) $(TARGET_ALIBS) - $(Q)$(TARGET_STRIP) $@ - $(E) "OK Successfully built LuaJIT" - -############################################################################## diff --git a/third-party/LuaJIT-2.0.2/src/Makefile.dep b/third-party/LuaJIT-2.0.2/src/Makefile.dep deleted file mode 100644 index 5d91723a76..0000000000 --- a/third-party/LuaJIT-2.0.2/src/Makefile.dep +++ /dev/null @@ -1,226 +0,0 @@ -lib_aux.o: lib_aux.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_state.h lj_trace.h lj_jit.h lj_ir.h \ - lj_dispatch.h lj_bc.h lj_traceerr.h lj_lib.h lj_alloc.h -lib_base.o: lib_base.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h \ - lj_tab.h lj_meta.h lj_state.h lj_ctype.h lj_cconv.h lj_bc.h lj_ff.h \ - lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h lj_strscan.h \ - lj_lib.h lj_libdef.h -lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_lib.h lj_libdef.h -lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \ - lj_libdef.h -lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h \ - lj_ctype.h lj_cparse.h lj_cdata.h lj_cconv.h lj_carith.h lj_ccall.h \ - lj_ccallback.h lj_clib.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h -lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h -lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_ff.h lj_ffdef.h \ - lj_lib.h lj_libdef.h -lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h \ - lj_obj.h lj_def.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h \ - lj_bc.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_target.h \ - lj_target_*.h lj_dispatch.h lj_vm.h lj_vmevent.h lj_lib.h luajit.h \ - lj_libdef.h -lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h -lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_libdef.h -lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h -lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h \ - lj_meta.h lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h lj_char.h \ - lj_lib.h lj_libdef.h -lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_lib.h \ - lj_libdef.h -lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h -lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \ - lj_meta.h lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h \ - lj_dispatch.h lj_traceerr.h lj_vm.h lj_strscan.h -lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h \ - lj_ircall.h lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h \ - lj_snap.h lj_asm.h lj_vm.h lj_target.h lj_target_*.h lj_emit_*.h \ - lj_asm_*.h -lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \ - lj_bcdef.h -lj_bcread.o: lj_bcread.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_bc.h lj_ctype.h \ - lj_cdata.h lualib.h lj_lex.h lj_bcdump.h lj_state.h -lj_bcwrite.o: lj_bcwrite.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h lj_ir.h \ - lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h -lj_carith.o: lj_carith.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_meta.h lj_ctype.h lj_cconv.h \ - lj_cdata.h lj_carith.h -lj_ccall.o: lj_ccall.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cconv.h \ - lj_cdata.h lj_ccall.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ - lj_traceerr.h -lj_ccallback.o: lj_ccallback.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_state.h lj_frame.h \ - lj_bc.h lj_ctype.h lj_cconv.h lj_ccall.h lj_ccallback.h lj_target.h \ - lj_target_*.h lj_mcode.h lj_jit.h lj_ir.h lj_trace.h lj_dispatch.h \ - lj_traceerr.h lj_vm.h -lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_gc.h lj_cdata.h lj_cconv.h \ - lj_ccallback.h -lj_cdata.o: lj_cdata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cconv.h \ - lj_cdata.h -lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h -lj_clib.o: lj_clib.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_tab.h lj_str.h lj_udata.h lj_ctype.h lj_cconv.h \ - lj_cdata.h lj_clib.h -lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \ - lj_bc.h lj_vm.h lj_char.h lj_strscan.h -lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h \ - lj_gc.h lj_cdata.h lj_cparse.h lj_cconv.h lj_clib.h lj_ccall.h lj_ff.h \ - lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ - lj_dispatch.h lj_traceerr.h lj_record.h lj_ffrecord.h lj_snap.h \ - lj_crecord.h -lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_ccallback.h -lj_debug.o: lj_debug.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_state.h lj_frame.h \ - lj_bc.h lj_jit.h lj_ir.h -lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_func.h lj_str.h lj_tab.h lj_meta.h lj_debug.h \ - lj_state.h lj_frame.h lj_bc.h lj_ff.h lj_ffdef.h lj_jit.h lj_ir.h \ - lj_ccallback.h lj_ctype.h lj_gc.h lj_trace.h lj_dispatch.h lj_traceerr.h \ - lj_vm.h luajit.h -lj_err.o: lj_err.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_err.h \ - lj_errmsg.h lj_debug.h lj_str.h lj_func.h lj_state.h lj_frame.h lj_bc.h \ - lj_ff.h lj_ffdef.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \ - lj_traceerr.h lj_vm.h -lj_ffrecord.o: lj_ffrecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ff.h \ - lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ - lj_dispatch.h lj_traceerr.h lj_record.h lj_ffrecord.h lj_crecord.h \ - lj_vm.h lj_strscan.h lj_recdef.h -lj_func.o: lj_func.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ - lj_traceerr.h lj_vm.h -lj_gc.o: lj_gc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_udata.h lj_meta.h \ - lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h lj_trace.h lj_jit.h \ - lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h -lj_gdbjit.o: lj_gdbjit.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_frame.h lj_bc.h lj_jit.h \ - lj_ir.h lj_dispatch.h -lj_ir.o: lj_ir.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ - lj_dispatch.h lj_bc.h lj_traceerr.h lj_ctype.h lj_cdata.h lj_carith.h \ - lj_vm.h lj_strscan.h lj_lib.h -lj_lex.o: lj_lex.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cdata.h lualib.h \ - lj_state.h lj_lex.h lj_parse.h lj_char.h lj_strscan.h -lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \ - lj_dispatch.h lj_jit.h lj_ir.h lj_vm.h lj_strscan.h lj_lib.h -lj_load.o: lj_load.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_func.h lj_frame.h \ - lj_bc.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h -lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h lj_dispatch.h lj_bc.h \ - lj_traceerr.h lj_vm.h -lj_meta.o: lj_meta.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ - lj_vm.h lj_strscan.h -lj_obj.o: lj_obj.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h -lj_opt_dce.o: lj_opt_dce.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_ir.h lj_jit.h lj_iropt.h -lj_opt_fold.o: lj_opt_fold.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ - lj_bc.h lj_traceerr.h lj_ctype.h lj_gc.h lj_carith.h lj_vm.h \ - lj_strscan.h lj_folddef.h -lj_opt_loop.o: lj_opt_loop.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h \ - lj_dispatch.h lj_bc.h lj_traceerr.h lj_snap.h lj_vm.h -lj_opt_mem.o: lj_opt_mem.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_tab.h lj_ir.h lj_jit.h lj_iropt.h -lj_opt_narrow.o: lj_opt_narrow.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ - lj_traceerr.h lj_vm.h lj_strscan.h -lj_opt_sink.o: lj_opt_sink.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_ir.h lj_jit.h lj_iropt.h lj_target.h lj_target_*.h -lj_opt_split.o: lj_opt_split.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_ircall.h \ - lj_iropt.h lj_vm.h -lj_parse.o: lj_parse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h \ - lj_state.h lj_bc.h lj_ctype.h lj_lex.h lj_parse.h lj_vm.h lj_vmevent.h -lj_record.o: lj_record.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ - lj_ctype.h lj_gc.h lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h \ - lj_iropt.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_record.h \ - lj_ffrecord.h lj_snap.h lj_vm.h -lj_snap.o: lj_snap.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_tab.h lj_state.h lj_frame.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h \ - lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h \ - lj_target_*.h lj_ctype.h lj_cdata.h -lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_meta.h \ - lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_trace.h lj_jit.h lj_ir.h \ - lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_alloc.h -lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_char.h -lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_char.h lj_strscan.h -lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_tab.h -lj_trace.o: lj_trace.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_frame.h lj_bc.h \ - lj_state.h lj_ir.h lj_jit.h lj_iropt.h lj_mcode.h lj_trace.h \ - lj_dispatch.h lj_traceerr.h lj_snap.h lj_gdbjit.h lj_record.h lj_asm.h \ - lj_vm.h lj_vmevent.h lj_target.h lj_target_*.h -lj_udata.o: lj_udata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_udata.h -lj_vmevent.o: lj_vmevent.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_str.h lj_tab.h lj_state.h lj_dispatch.h lj_bc.h lj_jit.h lj_ir.h \ - lj_vm.h lj_vmevent.h -lj_vmmath.o: lj_vmmath.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_ir.h lj_vm.h -ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h \ - lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h \ - lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_err.c \ - lj_debug.h lj_ff.h lj_ffdef.h lj_char.c lj_char.h lj_bc.c lj_bcdef.h \ - lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c lj_meta.c lj_strscan.h \ - lj_debug.c lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c lj_ccallback.h \ - luajit.h lj_vmevent.c lj_vmevent.h lj_vmmath.c lj_strscan.c lj_api.c \ - lj_lex.c lualib.h lj_parse.h lj_parse.c lj_bcread.c lj_bcdump.h \ - lj_bcwrite.c lj_load.c lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c \ - lj_ccall.c lj_ccall.h lj_ccallback.c lj_target.h lj_target_*.h \ - lj_mcode.h lj_carith.c lj_carith.h lj_clib.c lj_clib.h lj_cparse.c \ - lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_ircall.h lj_iropt.h \ - lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \ - lj_opt_loop.c lj_snap.h lj_opt_split.c lj_opt_sink.c lj_mcode.c \ - lj_snap.c lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c \ - lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h \ - lj_asm_*.h lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c \ - lib_base.c lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c \ - lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c \ - lib_init.c -luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h -host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \ - lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \ - lj_ircall.h lj_ir.h lj_jit.h lj_frame.h lj_bc.h lj_dispatch.h lj_ctype.h \ - lj_gc.h lj_ccall.h lj_ctype.h luajit.h \ - host/buildvm_arch.h lj_traceerr.h -host/buildvm_asm.o: host/buildvm_asm.c host/buildvm.h lj_def.h lua.h luaconf.h \ - lj_arch.h lj_bc.h lj_def.h lj_arch.h -host/buildvm_fold.o: host/buildvm_fold.c host/buildvm.h lj_def.h lua.h \ - luaconf.h lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_ir.h lj_obj.h -host/buildvm_lib.o: host/buildvm_lib.c host/buildvm.h lj_def.h lua.h luaconf.h \ - lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_lib.h lj_obj.h -host/buildvm_peobj.o: host/buildvm_peobj.c host/buildvm.h lj_def.h lua.h \ - luaconf.h lj_arch.h lj_bc.h lj_def.h lj_arch.h -host/minilua.o: host/minilua.c diff --git a/third-party/LuaJIT-2.0.2/src/host/.gitignore b/third-party/LuaJIT-2.0.2/src/host/.gitignore deleted file mode 100644 index 87dab34c34..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/buildvm -/buildvm_arch.h -/minilua diff --git a/third-party/LuaJIT-2.0.2/src/host/README b/third-party/LuaJIT-2.0.2/src/host/README deleted file mode 100644 index abfcdaa76e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/README +++ /dev/null @@ -1,4 +0,0 @@ -The files in this directory are only used during the build process of LuaJIT. -For cross-compilation, they must be executed on the host, not on the target. - -These files should NOT be installed! diff --git a/third-party/LuaJIT-2.0.2/src/host/buildvm.c b/third-party/LuaJIT-2.0.2/src/host/buildvm.c deleted file mode 100644 index 2ce3b63803..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/buildvm.c +++ /dev/null @@ -1,516 +0,0 @@ -/* -** LuaJIT VM builder. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** This is a tool to build the hand-tuned assembler code required for -** LuaJIT's bytecode interpreter. It supports a variety of output formats -** to feed different toolchains (see usage() below). -** -** This tool is not particularly optimized because it's only used while -** _building_ LuaJIT. There's no point in distributing or installing it. -** Only the object code generated by this tool is linked into LuaJIT. -** -** Caveat: some memory is not free'd, error handling is lazy. -** It's a one-shot tool -- any effort fixing this would be wasted. -*/ - -#include "buildvm.h" -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_bc.h" -#include "lj_ir.h" -#include "lj_ircall.h" -#include "lj_frame.h" -#include "lj_dispatch.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_ccall.h" -#endif -#include "luajit.h" - -#if defined(_WIN32) -#include -#include -#endif - -/* ------------------------------------------------------------------------ */ - -/* DynASM glue definitions. */ -#define Dst ctx -#define Dst_DECL BuildCtx *ctx -#define Dst_REF (ctx->D) -#define DASM_CHECKS 1 - -#include "../dynasm/dasm_proto.h" - -/* Glue macros for DynASM. */ -static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type); - -#define DASM_EXTERN(ctx, addr, idx, type) \ - collect_reloc(ctx, addr, idx, type) - -/* ------------------------------------------------------------------------ */ - -/* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */ -#define DASM_ALIGNED_WRITES 1 - -/* Embed architecture-specific DynASM encoder. */ -#if LJ_TARGET_X86ORX64 -#include "../dynasm/dasm_x86.h" -#elif LJ_TARGET_ARM -#include "../dynasm/dasm_arm.h" -#elif LJ_TARGET_PPC -#include "../dynasm/dasm_ppc.h" -#elif LJ_TARGET_PPCSPE -#include "../dynasm/dasm_ppc.h" -#elif LJ_TARGET_MIPS -#include "../dynasm/dasm_mips.h" -#else -#error "No support for this architecture (yet)" -#endif - -/* Embed generated architecture-specific backend. */ -#include "buildvm_arch.h" - -/* ------------------------------------------------------------------------ */ - -void owrite(BuildCtx *ctx, const void *ptr, size_t sz) -{ - if (fwrite(ptr, 1, sz, ctx->fp) != sz) { - fprintf(stderr, "Error: cannot write to output file: %s\n", - strerror(errno)); - exit(1); - } -} - -/* ------------------------------------------------------------------------ */ - -/* Emit code as raw bytes. Only used for DynASM debugging. */ -static void emit_raw(BuildCtx *ctx) -{ - owrite(ctx, ctx->code, ctx->codesz); -} - -/* -- Build machine code -------------------------------------------------- */ - -static const char *sym_decorate(BuildCtx *ctx, - const char *prefix, const char *suffix) -{ - char name[256]; - char *p; -#if LJ_64 - const char *symprefix = ctx->mode == BUILD_machasm ? "_" : ""; -#elif LJ_TARGET_XBOX360 - const char *symprefix = ""; -#else - const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : ""; -#endif - sprintf(name, "%s%s%s", symprefix, prefix, suffix); - p = strchr(name, '@'); - if (p) { -#if LJ_TARGET_X86ORX64 - if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj)) - name[0] = '@'; - else - *p = '\0'; -#elif (LJ_TARGET_PPC || LJ_TARGET_PPCSPE) && !LJ_TARGET_CONSOLE - /* Keep @plt. */ -#else - *p = '\0'; -#endif - } - p = (char *)malloc(strlen(name)+1); /* MSVC doesn't like strdup. */ - strcpy(p, name); - return p; -} - -#define NRELOCSYM (sizeof(extnames)/sizeof(extnames[0])-1) - -static int relocmap[NRELOCSYM]; - -/* Collect external relocations. */ -static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type) -{ - if (ctx->nreloc >= BUILD_MAX_RELOC) { - fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n"); - exit(1); - } - if (relocmap[idx] < 0) { - relocmap[idx] = ctx->nrelocsym; - ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]); - ctx->nrelocsym++; - } - ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code); - ctx->reloc[ctx->nreloc].sym = relocmap[idx]; - ctx->reloc[ctx->nreloc].type = type; - ctx->nreloc++; -#if LJ_TARGET_XBOX360 - return (int)(ctx->code - addr) + 4; /* Encode symbol offset of .text. */ -#else - return 0; /* Encode symbol offset of 0. */ -#endif -} - -/* Naive insertion sort. Performance doesn't matter here. */ -static void sym_insert(BuildCtx *ctx, int32_t ofs, - const char *prefix, const char *suffix) -{ - ptrdiff_t i = ctx->nsym++; - while (i > 0) { - if (ctx->sym[i-1].ofs <= ofs) - break; - ctx->sym[i] = ctx->sym[i-1]; - i--; - } - ctx->sym[i].ofs = ofs; - ctx->sym[i].name = sym_decorate(ctx, prefix, suffix); -} - -/* Build the machine code. */ -static int build_code(BuildCtx *ctx) -{ - int status; - int i; - - /* Initialize DynASM structures. */ - ctx->nglob = GLOB__MAX; - ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *)); - memset(ctx->glob, 0, ctx->nglob*sizeof(void *)); - ctx->nreloc = 0; - - ctx->globnames = globnames; - ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *)); - ctx->nrelocsym = 0; - for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1; - - ctx->dasm_ident = DASM_IDENT; - ctx->dasm_arch = DASM_ARCH; - - dasm_init(Dst, DASM_MAXSECTION); - dasm_setupglobal(Dst, ctx->glob, ctx->nglob); - dasm_setup(Dst, build_actionlist); - - /* Call arch-specific backend to emit the code. */ - ctx->npc = build_backend(ctx); - - /* Finalize the code. */ - (void)dasm_checkstep(Dst, -1); - if ((status = dasm_link(Dst, &ctx->codesz))) return status; - ctx->code = (uint8_t *)malloc(ctx->codesz); - if ((status = dasm_encode(Dst, (void *)ctx->code))) return status; - - /* Allocate symbol table and bytecode offsets. */ - ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin"); - ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym)); - ctx->nsym = 0; - ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t)); - - /* Collect the opcodes (PC labels). */ - for (i = 0; i < ctx->npc; i++) { - int32_t ofs = dasm_getpclabel(Dst, i); - if (ofs < 0) return 0x22000000|i; - ctx->bc_ofs[i] = ofs; - if ((LJ_HASJIT || - !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP || - i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) && - (LJ_HASFFI || i != BC_KCDATA)) - sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]); - } - - /* Collect the globals (named labels). */ - for (i = 0; i < ctx->nglob; i++) { - const char *gl = globnames[i]; - int len = (int)strlen(gl); - if (!ctx->glob[i]) { - fprintf(stderr, "Error: undefined global %s\n", gl); - exit(2); - } - /* Skip the _Z symbols. */ - if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z')) - sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code), - LABEL_PREFIX, globnames[i]); - } - - /* Close the address range. */ - sym_insert(ctx, (int32_t)ctx->codesz, "", ""); - ctx->nsym--; - - dasm_free(Dst); - - return 0; -} - -/* -- Generate VM enums --------------------------------------------------- */ - -const char *const bc_names[] = { -#define BCNAME(name, ma, mb, mc, mt) #name, -BCDEF(BCNAME) -#undef BCNAME - NULL -}; - -const char *const ir_names[] = { -#define IRNAME(name, m, m1, m2) #name, -IRDEF(IRNAME) -#undef IRNAME - NULL -}; - -const char *const irt_names[] = { -#define IRTNAME(name, size) #name, -IRTDEF(IRTNAME) -#undef IRTNAME - NULL -}; - -const char *const irfpm_names[] = { -#define FPMNAME(name) #name, -IRFPMDEF(FPMNAME) -#undef FPMNAME - NULL -}; - -const char *const irfield_names[] = { -#define FLNAME(name, ofs) #name, -IRFLDEF(FLNAME) -#undef FLNAME - NULL -}; - -const char *const ircall_names[] = { -#define IRCALLNAME(cond, name, nargs, kind, type, flags) #name, -IRCALLDEF(IRCALLNAME) -#undef IRCALLNAME - NULL -}; - -static const char *const trace_errors[] = { -#define TREDEF(name, msg) msg, -#include "lj_traceerr.h" - NULL -}; - -static const char *lower(char *buf, const char *s) -{ - char *p = buf; - while (*s) { - *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s; - s++; - } - *p = '\0'; - return buf; -} - -/* Emit C source code for bytecode-related definitions. */ -static void emit_bcdef(BuildCtx *ctx) -{ - int i; - fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); - fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n"); - for (i = 0; i < ctx->npc; i++) { - if (i != 0) - fprintf(ctx->fp, ",\n"); - fprintf(ctx->fp, "%d", ctx->bc_ofs[i]); - } -} - -/* Emit VM definitions as Lua code for debug modules. */ -static void emit_vmdef(BuildCtx *ctx) -{ - char buf[80]; - int i; - fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n"); - fprintf(ctx->fp, "module(...)\n\n"); - - fprintf(ctx->fp, "bcnames = \""); - for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]); - fprintf(ctx->fp, "\"\n\n"); - - fprintf(ctx->fp, "irnames = \""); - for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]); - fprintf(ctx->fp, "\"\n\n"); - - fprintf(ctx->fp, "irfpm = { [0]="); - for (i = 0; irfpm_names[i]; i++) - fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i])); - fprintf(ctx->fp, "}\n\n"); - - fprintf(ctx->fp, "irfield = { [0]="); - for (i = 0; irfield_names[i]; i++) { - char *p; - lower(buf, irfield_names[i]); - p = strchr(buf, '_'); - if (p) *p = '.'; - fprintf(ctx->fp, "\"%s\", ", buf); - } - fprintf(ctx->fp, "}\n\n"); - - fprintf(ctx->fp, "ircall = {\n[0]="); - for (i = 0; ircall_names[i]; i++) - fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]); - fprintf(ctx->fp, "}\n\n"); - - fprintf(ctx->fp, "traceerr = {\n[0]="); - for (i = 0; trace_errors[i]; i++) - fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]); - fprintf(ctx->fp, "}\n\n"); -} - -/* -- Argument parsing ---------------------------------------------------- */ - -/* Build mode names. */ -static const char *const modenames[] = { -#define BUILDNAME(name) #name, -BUILDDEF(BUILDNAME) -#undef BUILDNAME - NULL -}; - -/* Print usage information and exit. */ -static void usage(void) -{ - int i; - fprintf(stderr, LUAJIT_VERSION " VM builder.\n"); - fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n"); - fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n"); - fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n"); - fprintf(stderr, "Available modes:\n"); - for (i = 0; i < BUILD__MAX; i++) - fprintf(stderr, " %s\n", modenames[i]); - exit(1); -} - -/* Parse the output mode name. */ -static BuildMode parsemode(const char *mode) -{ - int i; - for (i = 0; modenames[i]; i++) - if (!strcmp(mode, modenames[i])) - return (BuildMode)i; - usage(); - return (BuildMode)-1; -} - -/* Parse arguments. */ -static void parseargs(BuildCtx *ctx, char **argv) -{ - const char *a; - int i; - ctx->mode = (BuildMode)-1; - ctx->outname = "-"; - for (i = 1; (a = argv[i]) != NULL; i++) { - if (a[0] != '-') - break; - switch (a[1]) { - case '-': - if (a[2]) goto err; - i++; - goto ok; - case '\0': - goto ok; - case 'm': - i++; - if (a[2] || argv[i] == NULL) goto err; - ctx->mode = parsemode(argv[i]); - break; - case 'o': - i++; - if (a[2] || argv[i] == NULL) goto err; - ctx->outname = argv[i]; - break; - default: err: - usage(); - break; - } - } -ok: - ctx->args = argv+i; - if (ctx->mode == (BuildMode)-1) goto err; -} - -int main(int argc, char **argv) -{ - BuildCtx ctx_; - BuildCtx *ctx = &ctx_; - int status, binmode; - - if (sizeof(void *) != 4*LJ_32+8*LJ_64) { - fprintf(stderr,"Error: pointer size mismatch in cross-build.\n"); - fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=...\n\n"); - return 1; - } - - UNUSED(argc); - parseargs(ctx, argv); - - if ((status = build_code(ctx))) { - fprintf(stderr,"Error: DASM error %08x\n", status); - return 1; - } - - switch (ctx->mode) { - case BUILD_peobj: - case BUILD_raw: - binmode = 1; - break; - default: - binmode = 0; - break; - } - - if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') { - ctx->fp = stdout; -#if defined(_WIN32) - if (binmode) - _setmode(_fileno(stdout), _O_BINARY); /* Yuck. */ -#endif - } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) { - fprintf(stderr, "Error: cannot open output file '%s': %s\n", - ctx->outname, strerror(errno)); - exit(1); - } - - switch (ctx->mode) { - case BUILD_elfasm: - case BUILD_coffasm: - case BUILD_machasm: - emit_asm(ctx); - emit_asm_debug(ctx); - break; - case BUILD_peobj: - emit_peobj(ctx); - break; - case BUILD_raw: - emit_raw(ctx); - break; - case BUILD_bcdef: - emit_bcdef(ctx); - emit_lib(ctx); - break; - case BUILD_vmdef: - emit_vmdef(ctx); - emit_lib(ctx); - break; - case BUILD_ffdef: - case BUILD_libdef: - case BUILD_recdef: - emit_lib(ctx); - break; - case BUILD_folddef: - emit_fold(ctx); - break; - default: - break; - } - - fflush(ctx->fp); - if (ferror(ctx->fp)) { - fprintf(stderr, "Error: cannot write to output file: %s\n", - strerror(errno)); - exit(1); - } - fclose(ctx->fp); - - return 0; -} - diff --git a/third-party/LuaJIT-2.0.2/src/host/buildvm.h b/third-party/LuaJIT-2.0.2/src/host/buildvm.h deleted file mode 100644 index 1a037e168f..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/buildvm.h +++ /dev/null @@ -1,104 +0,0 @@ -/* -** LuaJIT VM builder. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _BUILDVM_H -#define _BUILDVM_H - -#include -#include -#include -#include -#include - -#include "lj_def.h" -#include "lj_arch.h" - -/* Hardcoded limits. Increase as needed. */ -#define BUILD_MAX_RELOC 200 /* Max. number of relocations. */ -#define BUILD_MAX_FOLD 4096 /* Max. number of fold rules. */ - -/* Prefix for scanned library definitions. */ -#define LIBDEF_PREFIX "LJLIB_" - -/* Prefix for scanned fold definitions. */ -#define FOLDDEF_PREFIX "LJFOLD" - -/* Prefixes for generated labels. */ -#define LABEL_PREFIX "lj_" -#define LABEL_PREFIX_BC LABEL_PREFIX "BC_" -#define LABEL_PREFIX_FF LABEL_PREFIX "ff_" -#define LABEL_PREFIX_CF LABEL_PREFIX "cf_" -#define LABEL_PREFIX_FFH LABEL_PREFIX "ffh_" -#define LABEL_PREFIX_LIBCF LABEL_PREFIX "lib_cf_" -#define LABEL_PREFIX_LIBINIT LABEL_PREFIX "lib_init_" - -/* Forward declaration. */ -struct dasm_State; - -/* Build modes. */ -#define BUILDDEF(_) \ - _(elfasm) _(coffasm) _(machasm) _(peobj) _(raw) \ - _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \ - _(folddef) - -typedef enum { -#define BUILDENUM(name) BUILD_##name, -BUILDDEF(BUILDENUM) -#undef BUILDENUM - BUILD__MAX -} BuildMode; - -/* Code relocation. */ -typedef struct BuildReloc { - int32_t ofs; - int sym; - int type; -} BuildReloc; - -typedef struct BuildSym { - const char *name; - int32_t ofs; -} BuildSym; - -/* Build context structure. */ -typedef struct BuildCtx { - /* DynASM state pointer. Should be first member. */ - struct dasm_State *D; - /* Parsed command line. */ - BuildMode mode; - FILE *fp; - const char *outname; - char **args; - /* Code and symbols generated by DynASM. */ - uint8_t *code; - size_t codesz; - int npc, nglob, nsym, nreloc, nrelocsym; - void **glob; - BuildSym *sym; - const char **relocsym; - int32_t *bc_ofs; - const char *beginsym; - /* Strings generated by DynASM. */ - const char *const *globnames; - const char *dasm_ident; - const char *dasm_arch; - /* Relocations. */ - BuildReloc reloc[BUILD_MAX_RELOC]; -} BuildCtx; - -extern void owrite(BuildCtx *ctx, const void *ptr, size_t sz); -extern void emit_asm(BuildCtx *ctx); -extern void emit_peobj(BuildCtx *ctx); -extern void emit_lib(BuildCtx *ctx); -extern void emit_fold(BuildCtx *ctx); - -extern const char *const bc_names[]; -extern const char *const ir_names[]; -extern const char *const irt_names[]; -extern const char *const irfpm_names[]; -extern const char *const irfield_names[]; -extern const char *const ircall_names[]; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/host/buildvm_asm.c b/third-party/LuaJIT-2.0.2/src/host/buildvm_asm.c deleted file mode 100644 index f18d1493fb..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/buildvm_asm.c +++ /dev/null @@ -1,313 +0,0 @@ -/* -** LuaJIT VM builder: Assembler source code emitter. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "buildvm.h" -#include "lj_bc.h" - -/* ------------------------------------------------------------------------ */ - -#if LJ_TARGET_X86ORX64 -/* Emit bytes piecewise as assembler text. */ -static void emit_asm_bytes(BuildCtx *ctx, uint8_t *p, int n) -{ - int i; - for (i = 0; i < n; i++) { - if ((i & 15) == 0) - fprintf(ctx->fp, "\t.byte %d", p[i]); - else - fprintf(ctx->fp, ",%d", p[i]); - if ((i & 15) == 15) putc('\n', ctx->fp); - } - if ((n & 15) != 0) putc('\n', ctx->fp); -} - -/* Emit relocation */ -static void emit_asm_reloc(BuildCtx *ctx, int type, const char *sym) -{ - switch (ctx->mode) { - case BUILD_elfasm: - if (type) - fprintf(ctx->fp, "\t.long %s-.-4\n", sym); - else - fprintf(ctx->fp, "\t.long %s\n", sym); - break; - case BUILD_coffasm: - fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", sym); - if (type) - fprintf(ctx->fp, "\t.long %s-.-4\n", sym); - else - fprintf(ctx->fp, "\t.long %s\n", sym); - break; - default: /* BUILD_machasm for relative relocations handled below. */ - fprintf(ctx->fp, "\t.long %s\n", sym); - break; - } -} - -static const char *const jccnames[] = { - "jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "ja", - "js", "jns", "jpe", "jpo", "jl", "jge", "jle", "jg" -}; - -/* Emit relocation for the incredibly stupid OSX assembler. */ -static void emit_asm_reloc_mach(BuildCtx *ctx, uint8_t *cp, int n, - const char *sym) -{ - const char *opname = NULL; - if (--n < 0) goto err; - if (cp[n] == 0xe8) { - opname = "call"; - } else if (cp[n] == 0xe9) { - opname = "jmp"; - } else if (cp[n] >= 0x80 && cp[n] <= 0x8f && n > 0 && cp[n-1] == 0x0f) { - opname = jccnames[cp[n]-0x80]; - n--; - } else { -err: - fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n", - sym); - exit(1); - } - emit_asm_bytes(ctx, cp, n); - fprintf(ctx->fp, "\t%s %s\n", opname, sym); -} -#else -/* Emit words piecewise as assembler text. */ -static void emit_asm_words(BuildCtx *ctx, uint8_t *p, int n) -{ - int i; - for (i = 0; i < n; i += 4) { - if ((i & 15) == 0) - fprintf(ctx->fp, "\t.long 0x%08x", *(uint32_t *)(p+i)); - else - fprintf(ctx->fp, ",0x%08x", *(uint32_t *)(p+i)); - if ((i & 15) == 12) putc('\n', ctx->fp); - } - if ((n & 15) != 0) putc('\n', ctx->fp); -} - -/* Emit relocation as part of an instruction. */ -static void emit_asm_wordreloc(BuildCtx *ctx, uint8_t *p, int n, - const char *sym) -{ - uint32_t ins; - emit_asm_words(ctx, p, n-4); - ins = *(uint32_t *)(p+n-4); -#if LJ_TARGET_ARM - if ((ins & 0xff000000u) == 0xfa000000u) { - fprintf(ctx->fp, "\tblx %s\n", sym); - } else if ((ins & 0x0e000000u) == 0x0a000000u) { - fprintf(ctx->fp, "\t%s%.2s %s\n", (ins & 0x01000000u) ? "bl" : "b", - "eqnecsccmiplvsvchilsgeltgtle" + 2*(ins >> 28), sym); - } else { - fprintf(stderr, - "Error: unsupported opcode %08x for %s symbol relocation.\n", - ins, sym); - exit(1); - } -#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE -#if LJ_TARGET_PS3 -#define TOCPREFIX "." -#else -#define TOCPREFIX "" -#endif - if ((ins >> 26) == 16) { - fprintf(ctx->fp, "\t%s %d, %d, " TOCPREFIX "%s\n", - (ins & 1) ? "bcl" : "bc", (ins >> 21) & 31, (ins >> 16) & 31, sym); - } else if ((ins >> 26) == 18) { - fprintf(ctx->fp, "\t%s " TOCPREFIX "%s\n", (ins & 1) ? "bl" : "b", sym); - } else { - fprintf(stderr, - "Error: unsupported opcode %08x for %s symbol relocation.\n", - ins, sym); - exit(1); - } -#elif LJ_TARGET_MIPS - fprintf(stderr, - "Error: unsupported opcode %08x for %s symbol relocation.\n", - ins, sym); - exit(1); -#else -#error "missing relocation support for this architecture" -#endif -} -#endif - -#if LJ_TARGET_ARM -#define ELFASM_PX "%%" -#else -#define ELFASM_PX "@" -#endif - -/* Emit an assembler label. */ -static void emit_asm_label(BuildCtx *ctx, const char *name, int size, int isfunc) -{ - switch (ctx->mode) { - case BUILD_elfasm: -#if LJ_TARGET_PS3 - if (!strncmp(name, "lj_vm_", 6) && - strcmp(name, ctx->beginsym) && - !strstr(name, "hook")) { - fprintf(ctx->fp, - "\n\t.globl %s\n" - "\t.section \".opd\",\"aw\"\n" - "%s:\n" - "\t.long .%s,.TOC.@tocbase32\n" - "\t.size %s,8\n" - "\t.previous\n" - "\t.globl .%s\n" - "\t.hidden .%s\n" - "\t.type .%s, " ELFASM_PX "function\n" - "\t.size .%s, %d\n" - ".%s:\n", - name, name, name, name, name, name, name, name, size, name); - break; - } -#endif - fprintf(ctx->fp, - "\n\t.globl %s\n" - "\t.hidden %s\n" - "\t.type %s, " ELFASM_PX "%s\n" - "\t.size %s, %d\n" - "%s:\n", - name, name, name, isfunc ? "function" : "object", name, size, name); - break; - case BUILD_coffasm: - fprintf(ctx->fp, "\n\t.globl %s\n", name); - if (isfunc) - fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", name); - fprintf(ctx->fp, "%s:\n", name); - break; - case BUILD_machasm: - fprintf(ctx->fp, - "\n\t.private_extern %s\n" - "%s:\n", name, name); - break; - default: - break; - } -} - -/* Emit alignment. */ -static void emit_asm_align(BuildCtx *ctx, int bits) -{ - switch (ctx->mode) { - case BUILD_elfasm: - case BUILD_coffasm: - fprintf(ctx->fp, "\t.p2align %d\n", bits); - break; - case BUILD_machasm: - fprintf(ctx->fp, "\t.align %d\n", bits); - break; - default: - break; - } -} - -/* ------------------------------------------------------------------------ */ - -/* Emit assembler source code. */ -void emit_asm(BuildCtx *ctx) -{ - int i, rel; - - fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch); - fprintf(ctx->fp, "\t.text\n"); - emit_asm_align(ctx, 4); - -#if LJ_TARGET_PS3 - emit_asm_label(ctx, ctx->beginsym, ctx->codesz, 0); -#else - emit_asm_label(ctx, ctx->beginsym, 0, 0); -#endif - if (ctx->mode != BUILD_machasm) - fprintf(ctx->fp, ".Lbegin:\n"); - -#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND - /* This should really be moved into buildvm_arm.dasc. */ - fprintf(ctx->fp, - ".fnstart\n" - ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n" - ".pad #28\n"); -#endif -#if LJ_TARGET_MIPS - fprintf(ctx->fp, ".set nomips16\n.abicalls\n.set noreorder\n.set nomacro\n"); -#endif - - for (i = rel = 0; i < ctx->nsym; i++) { - int32_t ofs = ctx->sym[i].ofs; - int32_t next = ctx->sym[i+1].ofs; -#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND && LJ_HASFFI - if (!strcmp(ctx->sym[i].name, "lj_vm_ffi_call")) - fprintf(ctx->fp, - ".globl lj_err_unwind_arm\n" - ".personality lj_err_unwind_arm\n" - ".fnend\n" - ".fnstart\n" - ".save {r4, r5, r11, lr}\n" - ".setfp r11, sp\n"); -#endif - emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1); - while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) { - BuildReloc *r = &ctx->reloc[rel]; - int n = r->ofs - ofs; -#if LJ_TARGET_X86ORX64 - if (ctx->mode == BUILD_machasm && r->type != 0) { - emit_asm_reloc_mach(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]); - } else { - emit_asm_bytes(ctx, ctx->code+ofs, n); - emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]); - } - ofs += n+4; -#else - emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]); - ofs += n; -#endif - rel++; - } -#if LJ_TARGET_X86ORX64 - emit_asm_bytes(ctx, ctx->code+ofs, next-ofs); -#else - emit_asm_words(ctx, ctx->code+ofs, next-ofs); -#endif - } - -#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND - fprintf(ctx->fp, -#if !LJ_HASFFI - ".globl lj_err_unwind_arm\n" - ".personality lj_err_unwind_arm\n" -#endif - ".fnend\n"); -#endif - - fprintf(ctx->fp, "\n"); - switch (ctx->mode) { - case BUILD_elfasm: -#if !LJ_TARGET_PS3 - fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n"); -#endif -#if LJ_TARGET_PPCSPE - /* Soft-float ABI + SPE. */ - fprintf(ctx->fp, "\t.gnu_attribute 4, 2\n\t.gnu_attribute 8, 3\n"); -#elif LJ_TARGET_PPC && !LJ_TARGET_PS3 - /* Hard-float ABI. */ - fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n"); -#endif - /* fallthrough */ - case BUILD_coffasm: - fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident); - break; - case BUILD_machasm: - fprintf(ctx->fp, - "\t.cstring\n" - "\t.ascii \"%s\\0\"\n", ctx->dasm_ident); - break; - default: - break; - } - fprintf(ctx->fp, "\n"); -} - diff --git a/third-party/LuaJIT-2.0.2/src/host/buildvm_fold.c b/third-party/LuaJIT-2.0.2/src/host/buildvm_fold.c deleted file mode 100644 index 085a4c345c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/buildvm_fold.c +++ /dev/null @@ -1,229 +0,0 @@ -/* -** LuaJIT VM builder: IR folding hash table generator. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "buildvm.h" -#include "lj_obj.h" -#include "lj_ir.h" - -/* Context for the folding hash table generator. */ -static int lineno; -static int funcidx; -static uint32_t foldkeys[BUILD_MAX_FOLD]; -static uint32_t nkeys; - -/* Try to fill the hash table with keys using the hash parameters. */ -static int tryhash(uint32_t *htab, uint32_t sz, uint32_t r, int dorol) -{ - uint32_t i; - if (dorol && ((r & 31) == 0 || (r>>5) == 0)) - return 0; /* Avoid zero rotates. */ - memset(htab, 0xff, (sz+1)*sizeof(uint32_t)); - for (i = 0; i < nkeys; i++) { - uint32_t key = foldkeys[i]; - uint32_t k = key & 0xffffff; - uint32_t h = (dorol ? lj_rol(lj_rol(k, r>>5) - k, r&31) : - (((k << (r>>5)) - k) << (r&31))) % sz; - if (htab[h] != 0xffffffff) { /* Collision on primary slot. */ - if (htab[h+1] != 0xffffffff) { /* Collision on secondary slot. */ - /* Try to move the colliding key, if possible. */ - if (h < sz-1 && htab[h+2] == 0xffffffff) { - uint32_t k2 = htab[h+1] & 0xffffff; - uint32_t h2 = (dorol ? lj_rol(lj_rol(k2, r>>5) - k2, r&31) : - (((k2 << (r>>5)) - k2) << (r&31))) % sz; - if (h2 != h+1) return 0; /* Cannot resolve collision. */ - htab[h+2] = htab[h+1]; /* Move colliding key to secondary slot. */ - } else { - return 0; /* Collision. */ - } - } - htab[h+1] = key; - } else { - htab[h] = key; - } - } - return 1; /* Success, all keys could be stored. */ -} - -/* Print the generated hash table. */ -static void printhash(BuildCtx *ctx, uint32_t *htab, uint32_t sz) -{ - uint32_t i; - fprintf(ctx->fp, "static const uint32_t fold_hash[%d] = {\n0x%08x", - sz+1, htab[0]); - for (i = 1; i < sz+1; i++) - fprintf(ctx->fp, ",\n0x%08x", htab[i]); - fprintf(ctx->fp, "\n};\n\n"); -} - -/* Exhaustive search for the shortest semi-perfect hash table. */ -static void makehash(BuildCtx *ctx) -{ - uint32_t htab[BUILD_MAX_FOLD*2+1]; - uint32_t sz, r; - /* Search for the smallest hash table with an odd size. */ - for (sz = (nkeys|1); sz < BUILD_MAX_FOLD*2; sz += 2) { - /* First try all shift hash combinations. */ - for (r = 0; r < 32*32; r++) { - if (tryhash(htab, sz, r, 0)) { - printhash(ctx, htab, sz); - fprintf(ctx->fp, - "#define fold_hashkey(k)\t(((((k)<<%u)-(k))<<%u)%%%u)\n\n", - r>>5, r&31, sz); - return; - } - } - /* Then try all rotate hash combinations. */ - for (r = 0; r < 32*32; r++) { - if (tryhash(htab, sz, r, 1)) { - printhash(ctx, htab, sz); - fprintf(ctx->fp, - "#define fold_hashkey(k)\t(lj_rol(lj_rol((k),%u)-(k),%u)%%%u)\n\n", - r>>5, r&31, sz); - return; - } - } - } - fprintf(stderr, "Error: search for perfect hash failed\n"); - exit(1); -} - -/* Parse one token of a fold rule. */ -static uint32_t nexttoken(char **pp, int allowlit, int allowany) -{ - char *p = *pp; - if (p) { - uint32_t i; - char *q = strchr(p, ' '); - if (q) *q++ = '\0'; - *pp = q; - if (allowlit && !strncmp(p, "IRFPM_", 6)) { - for (i = 0; irfpm_names[i]; i++) - if (!strcmp(irfpm_names[i], p+6)) - return i; - } else if (allowlit && !strncmp(p, "IRFL_", 5)) { - for (i = 0; irfield_names[i]; i++) - if (!strcmp(irfield_names[i], p+5)) - return i; - } else if (allowlit && !strncmp(p, "IRCALL_", 7)) { - for (i = 0; ircall_names[i]; i++) - if (!strcmp(ircall_names[i], p+7)) - return i; - } else if (allowlit && !strncmp(p, "IRCONV_", 7)) { - for (i = 0; irt_names[i]; i++) { - const char *r = strchr(p+7, '_'); - if (r && !strncmp(irt_names[i], p+7, r-(p+7))) { - uint32_t j; - for (j = 0; irt_names[j]; j++) - if (!strcmp(irt_names[j], r+1)) - return (i << 5) + j; - } - } - } else if (allowlit && *p >= '0' && *p <= '9') { - for (i = 0; *p >= '0' && *p <= '9'; p++) - i = i*10 + (*p - '0'); - if (*p == '\0') - return i; - } else if (allowany && !strcmp("any", p)) { - return allowany; - } else { - for (i = 0; ir_names[i]; i++) - if (!strcmp(ir_names[i], p)) - return i; - } - fprintf(stderr, "Error: bad fold definition token \"%s\" at line %d\n", p, lineno); - exit(1); - } - return 0; -} - -/* Parse a fold rule. */ -static void foldrule(char *p) -{ - uint32_t op = nexttoken(&p, 0, 0); - uint32_t left = nexttoken(&p, 0, 0x7f); - uint32_t right = nexttoken(&p, 1, 0x3ff); - uint32_t key = (funcidx << 24) | (op << 17) | (left << 10) | right; - uint32_t i; - if (nkeys >= BUILD_MAX_FOLD) { - fprintf(stderr, "Error: too many fold rules, increase BUILD_MAX_FOLD.\n"); - exit(1); - } - /* Simple insertion sort to detect duplicates. */ - for (i = nkeys; i > 0; i--) { - if ((foldkeys[i-1]&0xffffff) < (key & 0xffffff)) - break; - if ((foldkeys[i-1]&0xffffff) == (key & 0xffffff)) { - fprintf(stderr, "Error: duplicate fold definition at line %d\n", lineno); - exit(1); - } - foldkeys[i] = foldkeys[i-1]; - } - foldkeys[i] = key; - nkeys++; -} - -/* Emit C source code for IR folding hash table. */ -void emit_fold(BuildCtx *ctx) -{ - char buf[256]; /* We don't care about analyzing lines longer than that. */ - const char *fname = ctx->args[0]; - FILE *fp; - - if (fname == NULL) { - fprintf(stderr, "Error: missing input filename\n"); - exit(1); - } - - if (fname[0] == '-' && fname[1] == '\0') { - fp = stdin; - } else { - fp = fopen(fname, "r"); - if (!fp) { - fprintf(stderr, "Error: cannot open input file '%s': %s\n", - fname, strerror(errno)); - exit(1); - } - } - - fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); - fprintf(ctx->fp, "static const FoldFunc fold_func[] = {\n"); - - lineno = 0; - funcidx = 0; - nkeys = 0; - while (fgets(buf, sizeof(buf), fp) != NULL) { - lineno++; - /* The prefix must be at the start of a line, otherwise it's ignored. */ - if (!strncmp(buf, FOLDDEF_PREFIX, sizeof(FOLDDEF_PREFIX)-1)) { - char *p = buf+sizeof(FOLDDEF_PREFIX)-1; - char *q = strchr(p, ')'); - if (p[0] == '(' && q) { - p++; - *q = '\0'; - foldrule(p); - } else if ((p[0] == 'F' || p[0] == 'X') && p[1] == '(' && q) { - p += 2; - *q = '\0'; - if (funcidx) - fprintf(ctx->fp, ",\n"); - if (p[-2] == 'X') - fprintf(ctx->fp, " %s", p); - else - fprintf(ctx->fp, " fold_%s", p); - funcidx++; - } else { - buf[strlen(buf)-1] = '\0'; - fprintf(stderr, "Error: unknown fold definition tag %s%s at line %d\n", - FOLDDEF_PREFIX, p, lineno); - exit(1); - } - } - } - fclose(fp); - fprintf(ctx->fp, "\n};\n\n"); - - makehash(ctx); -} - diff --git a/third-party/LuaJIT-2.0.2/src/host/buildvm_lib.c b/third-party/LuaJIT-2.0.2/src/host/buildvm_lib.c deleted file mode 100644 index 40141dfbc6..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/buildvm_lib.c +++ /dev/null @@ -1,398 +0,0 @@ -/* -** LuaJIT VM builder: library definition compiler. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "buildvm.h" -#include "lj_obj.h" -#include "lj_lib.h" - -/* Context for library definitions. */ -static uint8_t obuf[8192]; -static uint8_t *optr; -static char modname[80]; -static size_t modnamelen; -static char funcname[80]; -static int modstate, regfunc; -static int ffid, recffid, ffasmfunc; - -enum { - REGFUNC_OK, - REGFUNC_NOREG, - REGFUNC_NOREGUV -}; - -static void libdef_name(const char *p, int kind) -{ - size_t n = strlen(p); - if (kind != LIBINIT_STRING) { - if (n > modnamelen && p[modnamelen] == '_' && - !strncmp(p, modname, modnamelen)) { - p += modnamelen+1; - n -= modnamelen+1; - } - } - if (n > LIBINIT_MAXSTR) { - fprintf(stderr, "Error: string too long: '%s'\n", p); - exit(1); - } - if (optr+1+n+2 > obuf+sizeof(obuf)) { /* +2 for caller. */ - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = (uint8_t)(n | kind); - memcpy(optr, p, n); - optr += n; -} - -static void libdef_endmodule(BuildCtx *ctx) -{ - if (modstate != 0) { - char line[80]; - const uint8_t *p; - int n; - if (modstate == 1) - fprintf(ctx->fp, " (lua_CFunction)0"); - fprintf(ctx->fp, "\n};\n"); - fprintf(ctx->fp, "static const uint8_t %s%s[] = {\n", - LABEL_PREFIX_LIBINIT, modname); - line[0] = '\0'; - for (n = 0, p = obuf; p < optr; p++) { - n += sprintf(line+n, "%d,", *p); - if (n >= 75) { - fprintf(ctx->fp, "%s\n", line); - n = 0; - line[0] = '\0'; - } - } - fprintf(ctx->fp, "%s%d\n};\n#endif\n\n", line, LIBINIT_END); - } -} - -static void libdef_module(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_libdef) { - libdef_endmodule(ctx); - optr = obuf; - *optr++ = (uint8_t)ffid; - *optr++ = (uint8_t)ffasmfunc; - *optr++ = 0; /* Hash table size. */ - modstate = 1; - fprintf(ctx->fp, "#ifdef %sMODULE_%s\n", LIBDEF_PREFIX, p); - fprintf(ctx->fp, "#undef %sMODULE_%s\n", LIBDEF_PREFIX, p); - fprintf(ctx->fp, "static const lua_CFunction %s%s[] = {\n", - LABEL_PREFIX_LIBCF, p); - } - modnamelen = strlen(p); - if (modnamelen > sizeof(modname)-1) { - fprintf(stderr, "Error: module name too long: '%s'\n", p); - exit(1); - } - strcpy(modname, p); -} - -static int find_ffofs(BuildCtx *ctx, const char *name) -{ - int i; - for (i = 0; i < ctx->nglob; i++) { - const char *gl = ctx->globnames[i]; - if (gl[0] == 'f' && gl[1] == 'f' && gl[2] == '_' && !strcmp(gl+3, name)) { - return (int)((uint8_t *)ctx->glob[i] - ctx->code); - } - } - fprintf(stderr, "Error: undefined fast function %s%s\n", - LABEL_PREFIX_FF, name); - exit(1); -} - -static void libdef_func(BuildCtx *ctx, char *p, int arg) -{ - if (arg != LIBINIT_CF) - ffasmfunc++; - if (ctx->mode == BUILD_libdef) { - if (modstate == 0) { - fprintf(stderr, "Error: no module for function definition %s\n", p); - exit(1); - } - if (regfunc == REGFUNC_NOREG) { - if (optr+1 > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_FFID; - } else { - if (arg != LIBINIT_ASM_) { - if (modstate != 1) fprintf(ctx->fp, ",\n"); - modstate = 2; - fprintf(ctx->fp, " %s%s", arg ? LABEL_PREFIX_FFH : LABEL_PREFIX_CF, p); - } - if (regfunc != REGFUNC_NOREGUV) obuf[2]++; /* Bump hash table size. */ - libdef_name(regfunc == REGFUNC_NOREGUV ? "" : p, arg); - } - } else if (ctx->mode == BUILD_ffdef) { - fprintf(ctx->fp, "FFDEF(%s)\n", p); - } else if (ctx->mode == BUILD_recdef) { - if (strlen(p) > sizeof(funcname)-1) { - fprintf(stderr, "Error: function name too long: '%s'\n", p); - exit(1); - } - strcpy(funcname, p); - } else if (ctx->mode == BUILD_vmdef) { - int i; - for (i = 1; p[i] && modname[i-1]; i++) - if (p[i] == '_') p[i] = '.'; - fprintf(ctx->fp, "\"%s\",\n", p); - } else if (ctx->mode == BUILD_bcdef) { - if (arg != LIBINIT_CF) - fprintf(ctx->fp, ",\n%d", find_ffofs(ctx, p)); - } - ffid++; - regfunc = REGFUNC_OK; -} - -static uint32_t find_rec(char *name) -{ - char *p = (char *)obuf; - uint32_t n; - for (n = 2; *p; n++) { - if (strcmp(p, name) == 0) - return n; - p += strlen(p)+1; - } - if (p+strlen(name)+1 >= (char *)obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - strcpy(p, name); - return n; -} - -static void libdef_rec(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_recdef) { - char *q; - uint32_t n; - for (; recffid+1 < ffid; recffid++) - fprintf(ctx->fp, ",\n0"); - recffid = ffid; - if (*p == '.') p = funcname; - q = strchr(p, ' '); - if (q) *q++ = '\0'; - n = find_rec(p); - if (q) - fprintf(ctx->fp, ",\n0x%02x00+(%s)", n, q); - else - fprintf(ctx->fp, ",\n0x%02x00", n); - } -} - -static void memcpy_endian(void *dst, void *src, size_t n) -{ - union { uint8_t b; uint32_t u; } host_endian; - host_endian.u = 1; - if (host_endian.b == LJ_ENDIAN_SELECT(1, 0)) { - memcpy(dst, src, n); - } else { - size_t i; - for (i = 0; i < n; i++) - ((uint8_t *)dst)[i] = ((uint8_t *)src)[n-i-1]; - } -} - -static void libdef_push(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_libdef) { - int len = (int)strlen(p); - if (*p == '"') { - if (len > 1 && p[len-1] == '"') { - p[len-1] = '\0'; - libdef_name(p+1, LIBINIT_STRING); - return; - } - } else if (*p >= '0' && *p <= '9') { - char *ep; - double d = strtod(p, &ep); - if (*ep == '\0') { - if (optr+1+sizeof(double) > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_NUMBER; - memcpy_endian(optr, &d, sizeof(double)); - optr += sizeof(double); - return; - } - } else if (!strcmp(p, "lastcl")) { - if (optr+1 > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_LASTCL; - return; - } else if (len > 4 && !strncmp(p, "top-", 4)) { - if (optr+2 > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_COPY; - *optr++ = (uint8_t)atoi(p+4); - return; - } - fprintf(stderr, "Error: bad value for %sPUSH(%s)\n", LIBDEF_PREFIX, p); - exit(1); - } -} - -static void libdef_set(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_libdef) { - if (p[0] == '!' && p[1] == '\0') p[0] = '\0'; /* Set env. */ - libdef_name(p, LIBINIT_STRING); - *optr++ = LIBINIT_SET; - obuf[2]++; /* Bump hash table size. */ - } -} - -static void libdef_regfunc(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(ctx); UNUSED(p); - regfunc = arg; -} - -typedef void (*LibDefFunc)(BuildCtx *ctx, char *p, int arg); - -typedef struct LibDefHandler { - const char *suffix; - const char *stop; - const LibDefFunc func; - const int arg; -} LibDefHandler; - -static const LibDefHandler libdef_handlers[] = { - { "MODULE_", " \t\r\n", libdef_module, 0 }, - { "CF(", ")", libdef_func, LIBINIT_CF }, - { "ASM(", ")", libdef_func, LIBINIT_ASM }, - { "ASM_(", ")", libdef_func, LIBINIT_ASM_ }, - { "REC(", ")", libdef_rec, 0 }, - { "PUSH(", ")", libdef_push, 0 }, - { "SET(", ")", libdef_set, 0 }, - { "NOREGUV", NULL, libdef_regfunc, REGFUNC_NOREGUV }, - { "NOREG", NULL, libdef_regfunc, REGFUNC_NOREG }, - { NULL, NULL, (LibDefFunc)0, 0 } -}; - -/* Emit C source code for library function definitions. */ -void emit_lib(BuildCtx *ctx) -{ - const char *fname; - - if (ctx->mode == BUILD_ffdef || ctx->mode == BUILD_libdef || - ctx->mode == BUILD_recdef) - fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); - else if (ctx->mode == BUILD_vmdef) - fprintf(ctx->fp, "ffnames = {\n[0]=\"Lua\",\n\"C\",\n"); - if (ctx->mode == BUILD_recdef) - fprintf(ctx->fp, "static const uint16_t recff_idmap[] = {\n0,\n0x0100"); - recffid = ffid = FF_C+1; - ffasmfunc = 0; - - while ((fname = *ctx->args++)) { - char buf[256]; /* We don't care about analyzing lines longer than that. */ - FILE *fp; - if (fname[0] == '-' && fname[1] == '\0') { - fp = stdin; - } else { - fp = fopen(fname, "r"); - if (!fp) { - fprintf(stderr, "Error: cannot open input file '%s': %s\n", - fname, strerror(errno)); - exit(1); - } - } - modstate = 0; - regfunc = REGFUNC_OK; - while (fgets(buf, sizeof(buf), fp) != NULL) { - char *p; - /* Simplistic pre-processor. Only handles top-level #if/#endif. */ - if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') { - int ok = 1; - if (!strcmp(buf, "#if LJ_52\n")) - ok = LJ_52; - else if (!strcmp(buf, "#if LJ_HASJIT\n")) - ok = LJ_HASJIT; - else if (!strcmp(buf, "#if LJ_HASFFI\n")) - ok = LJ_HASFFI; - if (!ok) { - int lvl = 1; - while (fgets(buf, sizeof(buf), fp) != NULL) { - if (buf[0] == '#' && buf[1] == 'e' && buf[2] == 'n') { - if (--lvl == 0) break; - } else if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') { - lvl++; - } - } - continue; - } - } - for (p = buf; (p = strstr(p, LIBDEF_PREFIX)) != NULL; ) { - const LibDefHandler *ldh; - p += sizeof(LIBDEF_PREFIX)-1; - for (ldh = libdef_handlers; ldh->suffix != NULL; ldh++) { - size_t n, len = strlen(ldh->suffix); - if (!strncmp(p, ldh->suffix, len)) { - p += len; - n = ldh->stop ? strcspn(p, ldh->stop) : 0; - if (!p[n]) break; - p[n] = '\0'; - ldh->func(ctx, p, ldh->arg); - p += n+1; - break; - } - } - if (ldh->suffix == NULL) { - buf[strlen(buf)-1] = '\0'; - fprintf(stderr, "Error: unknown library definition tag %s%s\n", - LIBDEF_PREFIX, p); - exit(1); - } - } - } - fclose(fp); - if (ctx->mode == BUILD_libdef) { - libdef_endmodule(ctx); - } - } - - if (ctx->mode == BUILD_ffdef) { - fprintf(ctx->fp, "\n#undef FFDEF\n\n"); - fprintf(ctx->fp, - "#ifndef FF_NUM_ASMFUNC\n#define FF_NUM_ASMFUNC %d\n#endif\n\n", - ffasmfunc); - } else if (ctx->mode == BUILD_vmdef) { - fprintf(ctx->fp, "}\n\n"); - } else if (ctx->mode == BUILD_bcdef) { - int i; - fprintf(ctx->fp, "\n};\n\n"); - fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_mode[] = {\n"); - fprintf(ctx->fp, "BCDEF(BCMODE)\n"); - for (i = ffasmfunc-1; i > 0; i--) - fprintf(ctx->fp, "BCMODE_FF,\n"); - fprintf(ctx->fp, "BCMODE_FF\n};\n\n"); - } else if (ctx->mode == BUILD_recdef) { - char *p = (char *)obuf; - fprintf(ctx->fp, "\n};\n\n"); - fprintf(ctx->fp, "static const RecordFunc recff_func[] = {\n" - "recff_nyi,\n" - "recff_c"); - while (*p) { - fprintf(ctx->fp, ",\nrecff_%s", p); - p += strlen(p)+1; - } - fprintf(ctx->fp, "\n};\n\n"); - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/host/buildvm_peobj.c b/third-party/LuaJIT-2.0.2/src/host/buildvm_peobj.c deleted file mode 100644 index 1249445b02..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/buildvm_peobj.c +++ /dev/null @@ -1,368 +0,0 @@ -/* -** LuaJIT VM builder: PE object emitter. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Only used for building on Windows, since we cannot assume the presence -** of a suitable assembler. The host and target byte order must match. -*/ - -#include "buildvm.h" -#include "lj_bc.h" - -#if LJ_TARGET_X86ORX64 || LJ_TARGET_PPC - -/* Context for PE object emitter. */ -static char *strtab; -static size_t strtabofs; - -/* -- PE object definitions ----------------------------------------------- */ - -/* PE header. */ -typedef struct PEheader { - uint16_t arch; - uint16_t nsects; - uint32_t time; - uint32_t symtabofs; - uint32_t nsyms; - uint16_t opthdrsz; - uint16_t flags; -} PEheader; - -/* PE section. */ -typedef struct PEsection { - char name[8]; - uint32_t vsize; - uint32_t vaddr; - uint32_t size; - uint32_t ofs; - uint32_t relocofs; - uint32_t lineofs; - uint16_t nreloc; - uint16_t nline; - uint32_t flags; -} PEsection; - -/* PE relocation. */ -typedef struct PEreloc { - uint32_t vaddr; - uint32_t symidx; - uint16_t type; -} PEreloc; - -/* Cannot use sizeof, because it pads up to the max. alignment. */ -#define PEOBJ_RELOC_SIZE (4+4+2) - -/* PE symbol table entry. */ -typedef struct PEsym { - union { - char name[8]; - uint32_t nameref[2]; - } n; - uint32_t value; - int16_t sect; - uint16_t type; - uint8_t scl; - uint8_t naux; -} PEsym; - -/* PE symbol table auxiliary entry for a section. */ -typedef struct PEsymaux { - uint32_t size; - uint16_t nreloc; - uint16_t nline; - uint32_t cksum; - uint16_t assoc; - uint8_t comdatsel; - uint8_t unused[3]; -} PEsymaux; - -/* Cannot use sizeof, because it pads up to the max. alignment. */ -#define PEOBJ_SYM_SIZE (8+4+2+2+1+1) - -/* PE object CPU specific defines. */ -#if LJ_TARGET_X86 -#define PEOBJ_ARCH_TARGET 0x014c -#define PEOBJ_RELOC_REL32 0x14 /* MS: REL32, GNU: DISP32. */ -#define PEOBJ_RELOC_DIR32 0x06 -#define PEOBJ_RELOC_OFS 0 -#define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */ -#elif LJ_TARGET_X64 -#define PEOBJ_ARCH_TARGET 0x8664 -#define PEOBJ_RELOC_REL32 0x04 /* MS: REL32, GNU: DISP32. */ -#define PEOBJ_RELOC_DIR32 0x02 -#define PEOBJ_RELOC_ADDR32NB 0x03 -#define PEOBJ_RELOC_OFS 0 -#define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */ -#elif LJ_TARGET_PPC -#define PEOBJ_ARCH_TARGET 0x01f2 -#define PEOBJ_RELOC_REL32 0x06 -#define PEOBJ_RELOC_DIR32 0x02 -#define PEOBJ_RELOC_OFS (-4) -#define PEOBJ_TEXT_FLAGS 0x60400020 /* 60=r+x, 40=align8, 20=code. */ -#endif - -/* Section numbers (0-based). */ -enum { - PEOBJ_SECT_ABS = -2, - PEOBJ_SECT_UNDEF = -1, - PEOBJ_SECT_TEXT, -#if LJ_TARGET_X64 - PEOBJ_SECT_PDATA, - PEOBJ_SECT_XDATA, -#endif - PEOBJ_SECT_RDATA_Z, - PEOBJ_NSECTIONS -}; - -/* Symbol types. */ -#define PEOBJ_TYPE_NULL 0 -#define PEOBJ_TYPE_FUNC 0x20 - -/* Symbol storage class. */ -#define PEOBJ_SCL_EXTERN 2 -#define PEOBJ_SCL_STATIC 3 - -/* -- PE object emitter --------------------------------------------------- */ - -/* Emit PE object symbol. */ -static void emit_peobj_sym(BuildCtx *ctx, const char *name, uint32_t value, - int sect, int type, int scl) -{ - PEsym sym; - size_t len = strlen(name); - if (!strtab) { /* Pass 1: only calculate string table length. */ - if (len > 8) strtabofs += len+1; - return; - } - if (len <= 8) { - memcpy(sym.n.name, name, len); - memset(sym.n.name+len, 0, 8-len); - } else { - sym.n.nameref[0] = 0; - sym.n.nameref[1] = (uint32_t)strtabofs; - memcpy(strtab + strtabofs, name, len); - strtab[strtabofs+len] = 0; - strtabofs += len+1; - } - sym.value = value; - sym.sect = (int16_t)(sect+1); /* 1-based section number. */ - sym.type = (uint16_t)type; - sym.scl = (uint8_t)scl; - sym.naux = 0; - owrite(ctx, &sym, PEOBJ_SYM_SIZE); -} - -/* Emit PE object section symbol. */ -static void emit_peobj_sym_sect(BuildCtx *ctx, PEsection *pesect, int sect) -{ - PEsym sym; - PEsymaux aux; - if (!strtab) return; /* Pass 1: no output. */ - memcpy(sym.n.name, pesect[sect].name, 8); - sym.value = 0; - sym.sect = (int16_t)(sect+1); /* 1-based section number. */ - sym.type = PEOBJ_TYPE_NULL; - sym.scl = PEOBJ_SCL_STATIC; - sym.naux = 1; - owrite(ctx, &sym, PEOBJ_SYM_SIZE); - memset(&aux, 0, sizeof(PEsymaux)); - aux.size = pesect[sect].size; - aux.nreloc = pesect[sect].nreloc; - owrite(ctx, &aux, PEOBJ_SYM_SIZE); -} - -/* Emit Windows PE object file. */ -void emit_peobj(BuildCtx *ctx) -{ - PEheader pehdr; - PEsection pesect[PEOBJ_NSECTIONS]; - uint32_t sofs; - int i, nrsym; - union { uint8_t b; uint32_t u; } host_endian; - - sofs = sizeof(PEheader) + PEOBJ_NSECTIONS*sizeof(PEsection); - - /* Fill in PE sections. */ - memset(&pesect, 0, PEOBJ_NSECTIONS*sizeof(PEsection)); - memcpy(pesect[PEOBJ_SECT_TEXT].name, ".text", sizeof(".text")-1); - pesect[PEOBJ_SECT_TEXT].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_TEXT].size = (uint32_t)ctx->codesz); - pesect[PEOBJ_SECT_TEXT].relocofs = sofs; - sofs += (pesect[PEOBJ_SECT_TEXT].nreloc = (uint16_t)ctx->nreloc) * PEOBJ_RELOC_SIZE; - /* Flags: 60 = read+execute, 50 = align16, 20 = code. */ - pesect[PEOBJ_SECT_TEXT].flags = PEOBJ_TEXT_FLAGS; - -#if LJ_TARGET_X64 - memcpy(pesect[PEOBJ_SECT_PDATA].name, ".pdata", sizeof(".pdata")-1); - pesect[PEOBJ_SECT_PDATA].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_PDATA].size = 6*4); - pesect[PEOBJ_SECT_PDATA].relocofs = sofs; - sofs += (pesect[PEOBJ_SECT_PDATA].nreloc = 6) * PEOBJ_RELOC_SIZE; - /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ - pesect[PEOBJ_SECT_PDATA].flags = 0x40300040; - - memcpy(pesect[PEOBJ_SECT_XDATA].name, ".xdata", sizeof(".xdata")-1); - pesect[PEOBJ_SECT_XDATA].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_XDATA].size = 8*2+4+6*2); /* See below. */ - pesect[PEOBJ_SECT_XDATA].relocofs = sofs; - sofs += (pesect[PEOBJ_SECT_XDATA].nreloc = 1) * PEOBJ_RELOC_SIZE; - /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ - pesect[PEOBJ_SECT_XDATA].flags = 0x40300040; -#endif - - memcpy(pesect[PEOBJ_SECT_RDATA_Z].name, ".rdata$Z", sizeof(".rdata$Z")-1); - pesect[PEOBJ_SECT_RDATA_Z].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_RDATA_Z].size = (uint32_t)strlen(ctx->dasm_ident)+1); - /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ - pesect[PEOBJ_SECT_RDATA_Z].flags = 0x40300040; - - /* Fill in PE header. */ - pehdr.arch = PEOBJ_ARCH_TARGET; - pehdr.nsects = PEOBJ_NSECTIONS; - pehdr.time = 0; /* Timestamp is optional. */ - pehdr.symtabofs = sofs; - pehdr.opthdrsz = 0; - pehdr.flags = 0; - - /* Compute the size of the symbol table: - ** @feat.00 + nsections*2 - ** + asm_start + nsym - ** + nrsym - */ - nrsym = ctx->nrelocsym; - pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+ctx->nsym + nrsym; -#if LJ_TARGET_X64 - pehdr.nsyms += 1; /* Symbol for lj_err_unwind_win64. */ -#endif - - /* Write PE object header and all sections. */ - owrite(ctx, &pehdr, sizeof(PEheader)); - owrite(ctx, &pesect, sizeof(PEsection)*PEOBJ_NSECTIONS); - - /* Write .text section. */ - host_endian.u = 1; - if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) { -#if LJ_TARGET_PPC - uint32_t *p = (uint32_t *)ctx->code; - int n = (int)(ctx->codesz >> 2); - for (i = 0; i < n; i++, p++) - *p = lj_bswap(*p); /* Byteswap .text section. */ -#else - fprintf(stderr, "Error: different byte order for host and target\n"); - exit(1); -#endif - } - owrite(ctx, ctx->code, ctx->codesz); - for (i = 0; i < ctx->nreloc; i++) { - PEreloc reloc; - reloc.vaddr = (uint32_t)ctx->reloc[i].ofs + PEOBJ_RELOC_OFS; - reloc.symidx = 1+2+ctx->reloc[i].sym; /* Reloc syms are after .text sym. */ - reloc.type = ctx->reloc[i].type ? PEOBJ_RELOC_REL32 : PEOBJ_RELOC_DIR32; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - } - -#if LJ_TARGET_X64 - { /* Write .pdata section. */ - uint32_t fcofs = (uint32_t)ctx->sym[ctx->nsym-1].ofs; - uint32_t pdata[3]; /* Start of .text, end of .text and .xdata. */ - PEreloc reloc; - pdata[0] = 0; pdata[1] = fcofs; pdata[2] = 0; - owrite(ctx, &pdata, sizeof(pdata)); - pdata[0] = fcofs; pdata[1] = (uint32_t)ctx->codesz; pdata[2] = 20; - owrite(ctx, &pdata, sizeof(pdata)); - reloc.vaddr = 0; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 4; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 8; reloc.symidx = 1+2+nrsym+2; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 12; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 16; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 20; reloc.symidx = 1+2+nrsym+2; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - } - { /* Write .xdata section. */ - uint16_t xdata[8+2+6]; - PEreloc reloc; - xdata[0] = 0x01|0x08|0x10; /* Ver. 1, uhandler/ehandler, prolog size 0. */ - xdata[1] = 0x0005; /* Number of unwind codes, no frame pointer. */ - xdata[2] = 0x4200; /* Stack offset 4*8+8 = aword*5. */ - xdata[3] = 0x3000; /* Push rbx. */ - xdata[4] = 0x6000; /* Push rsi. */ - xdata[5] = 0x7000; /* Push rdi. */ - xdata[6] = 0x5000; /* Push rbp. */ - xdata[7] = 0; /* Alignment. */ - xdata[8] = xdata[9] = 0; /* Relocated address of exception handler. */ - xdata[10] = 0x01; /* Ver. 1, no handler, prolog size 0. */ - xdata[11] = 0x1504; /* Number of unwind codes, fp = rbp, fpofs = 16. */ - xdata[12] = 0x0300; /* set_fpreg. */ - xdata[13] = 0x0200; /* stack offset 0*8+8 = aword*1. */ - xdata[14] = 0x3000; /* Push rbx. */ - xdata[15] = 0x5000; /* Push rbp. */ - owrite(ctx, &xdata, sizeof(xdata)); - reloc.vaddr = 2*8; reloc.symidx = 1+2+nrsym+2+2; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - } -#endif - - /* Write .rdata$Z section. */ - owrite(ctx, ctx->dasm_ident, strlen(ctx->dasm_ident)+1); - - /* Write symbol table. */ - strtab = NULL; /* 1st pass: collect string sizes. */ - for (;;) { - strtabofs = 4; - /* Mark as SafeSEH compliant. */ - emit_peobj_sym(ctx, "@feat.00", 1, - PEOBJ_SECT_ABS, PEOBJ_TYPE_NULL, PEOBJ_SCL_STATIC); - - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_TEXT); - for (i = 0; i < nrsym; i++) - emit_peobj_sym(ctx, ctx->relocsym[i], 0, - PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); - -#if LJ_TARGET_X64 - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_PDATA); - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_XDATA); - emit_peobj_sym(ctx, "lj_err_unwind_win64", 0, - PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); -#endif - - emit_peobj_sym(ctx, ctx->beginsym, 0, - PEOBJ_SECT_TEXT, PEOBJ_TYPE_NULL, PEOBJ_SCL_EXTERN); - for (i = 0; i < ctx->nsym; i++) - emit_peobj_sym(ctx, ctx->sym[i].name, (uint32_t)ctx->sym[i].ofs, - PEOBJ_SECT_TEXT, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); - - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA_Z); - - if (strtab) - break; - /* 2nd pass: alloc strtab, write syms and copy strings. */ - strtab = (char *)malloc(strtabofs); - *(uint32_t *)strtab = (uint32_t)strtabofs; - } - - /* Write string table. */ - owrite(ctx, strtab, strtabofs); -} - -#else - -void emit_peobj(BuildCtx *ctx) -{ - UNUSED(ctx); - fprintf(stderr, "Error: no PE object support for this target\n"); - exit(1); -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/host/genminilua.lua b/third-party/LuaJIT-2.0.2/src/host/genminilua.lua deleted file mode 100644 index e666f088a8..0000000000 --- a/third-party/LuaJIT-2.0.2/src/host/genminilua.lua +++ /dev/null @@ -1,427 +0,0 @@ ----------------------------------------------------------------------------- --- Lua script to generate a customized, minified version of Lua. --- The resulting 'minilua' is used for the build process of LuaJIT. ----------------------------------------------------------------------------- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- - -local sub, match, gsub = string.sub, string.match, string.gsub - -local LUA_VERSION = "5.1.5" -local LUA_SOURCE - -local function usage() - io.stderr:write("Usage: ", arg and arg[0] or "genminilua", - " lua-", LUA_VERSION, "-source-dir\n") - os.exit(1) -end - -local function find_sources() - LUA_SOURCE = arg and arg[1] - if not LUA_SOURCE then usage() end - if sub(LUA_SOURCE, -1) ~= "/" then LUA_SOURCE = LUA_SOURCE.."/" end - local fp = io.open(LUA_SOURCE .. "lua.h") - if not fp then - LUA_SOURCE = LUA_SOURCE.."src/" - fp = io.open(LUA_SOURCE .. "lua.h") - if not fp then usage() end - end - local all = fp:read("*a") - fp:close() - if not match(all, 'LUA_RELEASE%s*"Lua '..LUA_VERSION..'"') then - io.stderr:write("Error: version mismatch\n") - usage() - end -end - -local LUA_FILES = { -"lmem.c", "lobject.c", "ltm.c", "lfunc.c", "ldo.c", "lstring.c", "ltable.c", -"lgc.c", "lstate.c", "ldebug.c", "lzio.c", "lopcodes.c", -"llex.c", "lcode.c", "lparser.c", "lvm.c", "lapi.c", "lauxlib.c", -"lbaselib.c", "ltablib.c", "liolib.c", "loslib.c", "lstrlib.c", "linit.c", -} - -local REMOVE_LIB = {} -gsub([[ -collectgarbage dofile gcinfo getfenv getmetatable load print rawequal rawset -select tostring xpcall -foreach foreachi getn maxn setn -popen tmpfile seek setvbuf __tostring -clock date difftime execute getenv rename setlocale time tmpname -dump gfind len reverse -LUA_LOADLIBNAME LUA_MATHLIBNAME LUA_DBLIBNAME -]], "%S+", function(name) - REMOVE_LIB[name] = true -end) - -local REMOVE_EXTINC = { [""] = true, [""] = true, } - -local CUSTOM_MAIN = [[ -typedef unsigned int UB; -static UB barg(lua_State *L,int idx){ -union{lua_Number n;U64 b;}bn; -bn.n=lua_tonumber(L,idx)+6755399441055744.0; -if (bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); -return(UB)bn.b; -} -#define BRET(b) lua_pushnumber(L,(lua_Number)(int)(b));return 1; -static int tobit(lua_State *L){ -BRET(barg(L,1))} -static int bnot(lua_State *L){ -BRET(~barg(L,1))} -static int band(lua_State *L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} -static int bor(lua_State *L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} -static int bxor(lua_State *L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} -static int lshift(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET(b<>n)} -static int arshift(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} -static int rol(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b<>(32-n)))} -static int ror(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} -static int bswap(lua_State *L){ -UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} -static int tohex(lua_State *L){ -UB b=barg(L,1); -int n=lua_isnone(L,2)?8:(int)barg(L,2); -const char *hexdigits="0123456789abcdef"; -char buf[8]; -int i; -if(n<0){n=-n;hexdigits="0123456789ABCDEF";} -if(n>8)n=8; -for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} -lua_pushlstring(L,buf,(size_t)n); -return 1; -} -static const struct luaL_Reg bitlib[] = { -{"tobit",tobit}, -{"bnot",bnot}, -{"band",band}, -{"bor",bor}, -{"bxor",bxor}, -{"lshift",lshift}, -{"rshift",rshift}, -{"arshift",arshift}, -{"rol",rol}, -{"ror",ror}, -{"bswap",bswap}, -{"tohex",tohex}, -{NULL,NULL} -}; -int main(int argc, char **argv){ - lua_State *L = luaL_newstate(); - int i; - luaL_openlibs(L); - luaL_register(L, "bit", bitlib); - if (argc < 2) return sizeof(void *); - lua_createtable(L, 0, 1); - lua_pushstring(L, argv[1]); - lua_rawseti(L, -2, 0); - lua_setglobal(L, "arg"); - if (luaL_loadfile(L, argv[1])) - goto err; - for (i = 2; i < argc; i++) - lua_pushstring(L, argv[i]); - if (lua_pcall(L, argc - 2, 0, 0)) { - err: - fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); - return 1; - } - lua_close(L); - return 0; -} -]] - -local function read_sources() - local t = {} - for i, name in ipairs(LUA_FILES) do - local fp = assert(io.open(LUA_SOURCE..name, "r")) - t[i] = fp:read("*a") - assert(fp:close()) - end - t[#t+1] = CUSTOM_MAIN - return table.concat(t) -end - -local includes = {} - -local function merge_includes(src) - return gsub(src, '#include%s*"([^"]*)"%s*\n', function(name) - if includes[name] then return "" end - includes[name] = true - local fp = assert(io.open(LUA_SOURCE..name, "r")) - local src = fp:read("*a") - assert(fp:close()) - src = gsub(src, "#ifndef%s+%w+_h\n#define%s+%w+_h\n", "") - src = gsub(src, "#endif%s*$", "") - return merge_includes(src) - end) -end - -local function get_license(src) - return match(src, "/%*+\n%* Copyright %(.-%*/\n") -end - -local function fold_lines(src) - return gsub(src, "\\\n", " ") -end - -local strings = {} - -local function save_str(str) - local n = #strings+1 - strings[n] = str - return "\1"..n.."\2" -end - -local function save_strings(src) - src = gsub(src, '"[^"\n]*"', save_str) - return gsub(src, "'[^'\n]*'", save_str) -end - -local function restore_strings(src) - return gsub(src, "\1(%d+)\2", function(numstr) - return strings[tonumber(numstr)] - end) -end - -local function def_istrue(def) - return def == "INT_MAX > 2147483640L" or - def == "LUAI_BITSINT >= 32" or - def == "SIZE_Bx < LUAI_BITSINT-1" or - def == "cast" or - def == "defined(LUA_CORE)" or - def == "MINSTRTABSIZE" or - def == "LUA_MINBUFFER" or - def == "HARDSTACKTESTS" or - def == "UNUSED" -end - -local head, defs = {[[ -#ifdef _MSC_VER -typedef unsigned __int64 U64; -#else -typedef unsigned long long U64; -#endif -]]}, {} - -local function preprocess(src) - local t = { match(src, "^(.-)#") } - local lvl, on, oldon = 0, true, {} - for pp, def, txt in string.gmatch(src, "#(%w+) *([^\n]*)\n([^#]*)") do - if pp == "if" or pp == "ifdef" or pp == "ifndef" then - lvl = lvl + 1 - oldon[lvl] = on - on = def_istrue(def) - elseif pp == "else" then - if oldon[lvl] then - if on == false then on = true else on = false end - end - elseif pp == "elif" then - if oldon[lvl] then - on = def_istrue(def) - end - elseif pp == "endif" then - on = oldon[lvl] - lvl = lvl - 1 - elseif on then - if pp == "include" then - if not head[def] and not REMOVE_EXTINC[def] then - head[def] = true - head[#head+1] = "#include "..def.."\n" - end - elseif pp == "define" then - local k, sp, v = match(def, "([%w_]+)(%s*)(.*)") - if k and not (sp == "" and sub(v, 1, 1) == "(") then - defs[k] = gsub(v, "%a[%w_]*", function(tok) - return defs[tok] or tok - end) - else - t[#t+1] = "#define "..def.."\n" - end - elseif pp ~= "undef" then - error("unexpected directive: "..pp.." "..def) - end - end - if on then t[#t+1] = txt end - end - return gsub(table.concat(t), "%a[%w_]*", function(tok) - return defs[tok] or tok - end) -end - -local function merge_header(src, license) - local hdr = string.format([[ -/* This is a heavily customized and minimized copy of Lua %s. */ -/* It's only used to build LuaJIT. It does NOT have all standard functions! */ -]], LUA_VERSION) - return hdr..license..table.concat(head)..src -end - -local function strip_unused1(src) - return gsub(src, '( {"?([%w_]+)"?,%s+%a[%w_]*},\n)', function(line, func) - return REMOVE_LIB[func] and "" or line - end) -end - -local function strip_unused2(src) - return gsub(src, "Symbolic Execution.-}=", "") -end - -local function strip_unused3(src) - src = gsub(src, "extern", "static") - src = gsub(src, "\nstatic([^\n]-)%(([^)]*)%)%(", "\nstatic%1 %2(") - src = gsub(src, "#define lua_assert[^\n]*\n", "") - src = gsub(src, "lua_assert%b();?", "") - src = gsub(src, "default:\n}", "default:;\n}") - src = gsub(src, "lua_lock%b();", "") - src = gsub(src, "lua_unlock%b();", "") - src = gsub(src, "luai_threadyield%b();", "") - src = gsub(src, "luai_userstateopen%b();", "{}") - src = gsub(src, "luai_userstate%w+%b();", "") - src = gsub(src, "%(%(c==.*luaY_parser%)", "luaY_parser") - src = gsub(src, "trydecpoint%(ls,seminfo%)", - "luaX_lexerror(ls,\"malformed number\",TK_NUMBER)") - src = gsub(src, "int c=luaZ_lookahead%b();", "") - src = gsub(src, "luaL_register%(L,[^,]*,co_funcs%);\nreturn 2;", - "return 1;") - src = gsub(src, "getfuncname%b():", "NULL:") - src = gsub(src, "getobjname%b():", "NULL:") - src = gsub(src, "if%([^\n]*hookmask[^\n]*%)\n[^\n]*\n", "") - src = gsub(src, "if%([^\n]*hookmask[^\n]*%)%b{}\n", "") - src = gsub(src, "if%([^\n]*hookmask[^\n]*&&\n[^\n]*%b{}\n", "") - src = gsub(src, "(twoto%b()%()", "%1(size_t)") - src = gsub(src, "i -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -typedef enum{ -TM_INDEX, -TM_NEWINDEX, -TM_GC, -TM_MODE, -TM_EQ, -TM_ADD, -TM_SUB, -TM_MUL, -TM_DIV, -TM_MOD, -TM_POW, -TM_UNM, -TM_LEN, -TM_LT, -TM_LE, -TM_CONCAT, -TM_CALL, -TM_N -}TMS; -enum OpMode{iABC,iABx,iAsBx}; -typedef enum{ -OP_MOVE, -OP_LOADK, -OP_LOADBOOL, -OP_LOADNIL, -OP_GETUPVAL, -OP_GETGLOBAL, -OP_GETTABLE, -OP_SETGLOBAL, -OP_SETUPVAL, -OP_SETTABLE, -OP_NEWTABLE, -OP_SELF, -OP_ADD, -OP_SUB, -OP_MUL, -OP_DIV, -OP_MOD, -OP_POW, -OP_UNM, -OP_NOT, -OP_LEN, -OP_CONCAT, -OP_JMP, -OP_EQ, -OP_LT, -OP_LE, -OP_TEST, -OP_TESTSET, -OP_CALL, -OP_TAILCALL, -OP_RETURN, -OP_FORLOOP, -OP_FORPREP, -OP_TFORLOOP, -OP_SETLIST, -OP_CLOSE, -OP_CLOSURE, -OP_VARARG -}OpCode; -enum OpArgMask{ -OpArgN, -OpArgU, -OpArgR, -OpArgK -}; -typedef enum{ -VVOID, -VNIL, -VTRUE, -VFALSE, -VK, -VKNUM, -VLOCAL, -VUPVAL, -VGLOBAL, -VINDEXED, -VJMP, -VRELOCABLE, -VNONRELOC, -VCALL, -VVARARG -}expkind; -enum RESERVED{ -TK_AND=257,TK_BREAK, -TK_DO,TK_ELSE,TK_ELSEIF,TK_END,TK_FALSE,TK_FOR,TK_FUNCTION, -TK_IF,TK_IN,TK_LOCAL,TK_NIL,TK_NOT,TK_OR,TK_REPEAT, -TK_RETURN,TK_THEN,TK_TRUE,TK_UNTIL,TK_WHILE, -TK_CONCAT,TK_DOTS,TK_EQ,TK_GE,TK_LE,TK_NE,TK_NUMBER, -TK_NAME,TK_STRING,TK_EOS -}; -typedef enum BinOpr{ -OPR_ADD,OPR_SUB,OPR_MUL,OPR_DIV,OPR_MOD,OPR_POW, -OPR_CONCAT, -OPR_NE,OPR_EQ, -OPR_LT,OPR_LE,OPR_GT,OPR_GE, -OPR_AND,OPR_OR, -OPR_NOBINOPR -}BinOpr; -typedef enum UnOpr{OPR_MINUS,OPR_NOT,OPR_LEN,OPR_NOUNOPR}UnOpr; -#define LUA_QL(x)"'"x"'" -#define luai_apicheck(L,o){(void)L;} -#define lua_number2str(s,n)sprintf((s),"%.14g",(n)) -#define lua_str2number(s,p)strtod((s),(p)) -#define luai_numadd(a,b)((a)+(b)) -#define luai_numsub(a,b)((a)-(b)) -#define luai_nummul(a,b)((a)*(b)) -#define luai_numdiv(a,b)((a)/(b)) -#define luai_nummod(a,b)((a)-floor((a)/(b))*(b)) -#define luai_numpow(a,b)(pow(a,b)) -#define luai_numunm(a)(-(a)) -#define luai_numeq(a,b)((a)==(b)) -#define luai_numlt(a,b)((a)<(b)) -#define luai_numle(a,b)((a)<=(b)) -#define luai_numisnan(a)(!luai_numeq((a),(a))) -#define lua_number2int(i,d)((i)=(int)(d)) -#define lua_number2integer(i,d)((i)=(lua_Integer)(d)) -#define LUAI_THROW(L,c)longjmp((c)->b,1) -#define LUAI_TRY(L,c,a)if(setjmp((c)->b)==0){a} -#define lua_pclose(L,file)((void)((void)L,file),0) -#define lua_upvalueindex(i)((-10002)-(i)) -typedef struct lua_State lua_State; -typedef int(*lua_CFunction)(lua_State*L); -typedef const char*(*lua_Reader)(lua_State*L,void*ud,size_t*sz); -typedef void*(*lua_Alloc)(void*ud,void*ptr,size_t osize,size_t nsize); -typedef double lua_Number; -typedef ptrdiff_t lua_Integer; -static void lua_settop(lua_State*L,int idx); -static int lua_type(lua_State*L,int idx); -static const char* lua_tolstring(lua_State*L,int idx,size_t*len); -static size_t lua_objlen(lua_State*L,int idx); -static void lua_pushlstring(lua_State*L,const char*s,size_t l); -static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n); -static void lua_createtable(lua_State*L,int narr,int nrec); -static void lua_setfield(lua_State*L,int idx,const char*k); -#define lua_pop(L,n)lua_settop(L,-(n)-1) -#define lua_newtable(L)lua_createtable(L,0,0) -#define lua_pushcfunction(L,f)lua_pushcclosure(L,(f),0) -#define lua_strlen(L,i)lua_objlen(L,(i)) -#define lua_isfunction(L,n)(lua_type(L,(n))==6) -#define lua_istable(L,n)(lua_type(L,(n))==5) -#define lua_isnil(L,n)(lua_type(L,(n))==0) -#define lua_isboolean(L,n)(lua_type(L,(n))==1) -#define lua_isnone(L,n)(lua_type(L,(n))==(-1)) -#define lua_isnoneornil(L,n)(lua_type(L,(n))<=0) -#define lua_pushliteral(L,s)lua_pushlstring(L,""s,(sizeof(s)/sizeof(char))-1) -#define lua_setglobal(L,s)lua_setfield(L,(-10002),(s)) -#define lua_tostring(L,i)lua_tolstring(L,(i),NULL) -typedef struct lua_Debug lua_Debug; -typedef void(*lua_Hook)(lua_State*L,lua_Debug*ar); -struct lua_Debug{ -int event; -const char*name; -const char*namewhat; -const char*what; -const char*source; -int currentline; -int nups; -int linedefined; -int lastlinedefined; -char short_src[60]; -int i_ci; -}; -typedef unsigned int lu_int32; -typedef size_t lu_mem; -typedef ptrdiff_t l_mem; -typedef unsigned char lu_byte; -#define IntPoint(p)((unsigned int)(lu_mem)(p)) -typedef union{double u;void*s;long l;}L_Umaxalign; -typedef double l_uacNumber; -#define check_exp(c,e)(e) -#define UNUSED(x)((void)(x)) -#define cast(t,exp)((t)(exp)) -#define cast_byte(i)cast(lu_byte,(i)) -#define cast_num(i)cast(lua_Number,(i)) -#define cast_int(i)cast(int,(i)) -typedef lu_int32 Instruction; -#define condhardstacktests(x)((void)0) -typedef union GCObject GCObject; -typedef struct GCheader{ -GCObject*next;lu_byte tt;lu_byte marked; -}GCheader; -typedef union{ -GCObject*gc; -void*p; -lua_Number n; -int b; -}Value; -typedef struct lua_TValue{ -Value value;int tt; -}TValue; -#define ttisnil(o)(ttype(o)==0) -#define ttisnumber(o)(ttype(o)==3) -#define ttisstring(o)(ttype(o)==4) -#define ttistable(o)(ttype(o)==5) -#define ttisfunction(o)(ttype(o)==6) -#define ttisboolean(o)(ttype(o)==1) -#define ttisuserdata(o)(ttype(o)==7) -#define ttisthread(o)(ttype(o)==8) -#define ttislightuserdata(o)(ttype(o)==2) -#define ttype(o)((o)->tt) -#define gcvalue(o)check_exp(iscollectable(o),(o)->value.gc) -#define pvalue(o)check_exp(ttislightuserdata(o),(o)->value.p) -#define nvalue(o)check_exp(ttisnumber(o),(o)->value.n) -#define rawtsvalue(o)check_exp(ttisstring(o),&(o)->value.gc->ts) -#define tsvalue(o)(&rawtsvalue(o)->tsv) -#define rawuvalue(o)check_exp(ttisuserdata(o),&(o)->value.gc->u) -#define uvalue(o)(&rawuvalue(o)->uv) -#define clvalue(o)check_exp(ttisfunction(o),&(o)->value.gc->cl) -#define hvalue(o)check_exp(ttistable(o),&(o)->value.gc->h) -#define bvalue(o)check_exp(ttisboolean(o),(o)->value.b) -#define thvalue(o)check_exp(ttisthread(o),&(o)->value.gc->th) -#define l_isfalse(o)(ttisnil(o)||(ttisboolean(o)&&bvalue(o)==0)) -#define checkconsistency(obj) -#define checkliveness(g,obj) -#define setnilvalue(obj)((obj)->tt=0) -#define setnvalue(obj,x){TValue*i_o=(obj);i_o->value.n=(x);i_o->tt=3;} -#define setbvalue(obj,x){TValue*i_o=(obj);i_o->value.b=(x);i_o->tt=1;} -#define setsvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=4;checkliveness(G(L),i_o);} -#define setuvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=7;checkliveness(G(L),i_o);} -#define setthvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=8;checkliveness(G(L),i_o);} -#define setclvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=6;checkliveness(G(L),i_o);} -#define sethvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=5;checkliveness(G(L),i_o);} -#define setptvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=(8+1);checkliveness(G(L),i_o);} -#define setobj(L,obj1,obj2){const TValue*o2=(obj2);TValue*o1=(obj1);o1->value=o2->value;o1->tt=o2->tt;checkliveness(G(L),o1);} -#define setttype(obj,tt)(ttype(obj)=(tt)) -#define iscollectable(o)(ttype(o)>=4) -typedef TValue*StkId; -typedef union TString{ -L_Umaxalign dummy; -struct{ -GCObject*next;lu_byte tt;lu_byte marked; -lu_byte reserved; -unsigned int hash; -size_t len; -}tsv; -}TString; -#define getstr(ts)cast(const char*,(ts)+1) -#define svalue(o)getstr(rawtsvalue(o)) -typedef union Udata{ -L_Umaxalign dummy; -struct{ -GCObject*next;lu_byte tt;lu_byte marked; -struct Table*metatable; -struct Table*env; -size_t len; -}uv; -}Udata; -typedef struct Proto{ -GCObject*next;lu_byte tt;lu_byte marked; -TValue*k; -Instruction*code; -struct Proto**p; -int*lineinfo; -struct LocVar*locvars; -TString**upvalues; -TString*source; -int sizeupvalues; -int sizek; -int sizecode; -int sizelineinfo; -int sizep; -int sizelocvars; -int linedefined; -int lastlinedefined; -GCObject*gclist; -lu_byte nups; -lu_byte numparams; -lu_byte is_vararg; -lu_byte maxstacksize; -}Proto; -typedef struct LocVar{ -TString*varname; -int startpc; -int endpc; -}LocVar; -typedef struct UpVal{ -GCObject*next;lu_byte tt;lu_byte marked; -TValue*v; -union{ -TValue value; -struct{ -struct UpVal*prev; -struct UpVal*next; -}l; -}u; -}UpVal; -typedef struct CClosure{ -GCObject*next;lu_byte tt;lu_byte marked;lu_byte isC;lu_byte nupvalues;GCObject*gclist;struct Table*env; -lua_CFunction f; -TValue upvalue[1]; -}CClosure; -typedef struct LClosure{ -GCObject*next;lu_byte tt;lu_byte marked;lu_byte isC;lu_byte nupvalues;GCObject*gclist;struct Table*env; -struct Proto*p; -UpVal*upvals[1]; -}LClosure; -typedef union Closure{ -CClosure c; -LClosure l; -}Closure; -#define iscfunction(o)(ttype(o)==6&&clvalue(o)->c.isC) -typedef union TKey{ -struct{ -Value value;int tt; -struct Node*next; -}nk; -TValue tvk; -}TKey; -typedef struct Node{ -TValue i_val; -TKey i_key; -}Node; -typedef struct Table{ -GCObject*next;lu_byte tt;lu_byte marked; -lu_byte flags; -lu_byte lsizenode; -struct Table*metatable; -TValue*array; -Node*node; -Node*lastfree; -GCObject*gclist; -int sizearray; -}Table; -#define lmod(s,size)(check_exp((size&(size-1))==0,(cast(int,(s)&((size)-1))))) -#define twoto(x)((size_t)1<<(x)) -#define sizenode(t)(twoto((t)->lsizenode)) -static const TValue luaO_nilobject_; -#define ceillog2(x)(luaO_log2((x)-1)+1) -static int luaO_log2(unsigned int x); -#define gfasttm(g,et,e)((et)==NULL?NULL:((et)->flags&(1u<<(e)))?NULL:luaT_gettm(et,e,(g)->tmname[e])) -#define fasttm(l,et,e)gfasttm(G(l),et,e) -static const TValue*luaT_gettm(Table*events,TMS event,TString*ename); -#define luaM_reallocv(L,b,on,n,e)((cast(size_t,(n)+1)<=((size_t)(~(size_t)0)-2)/(e))?luaM_realloc_(L,(b),(on)*(e),(n)*(e)):luaM_toobig(L)) -#define luaM_freemem(L,b,s)luaM_realloc_(L,(b),(s),0) -#define luaM_free(L,b)luaM_realloc_(L,(b),sizeof(*(b)),0) -#define luaM_freearray(L,b,n,t)luaM_reallocv(L,(b),n,0,sizeof(t)) -#define luaM_malloc(L,t)luaM_realloc_(L,NULL,0,(t)) -#define luaM_new(L,t)cast(t*,luaM_malloc(L,sizeof(t))) -#define luaM_newvector(L,n,t)cast(t*,luaM_reallocv(L,NULL,0,n,sizeof(t))) -#define luaM_growvector(L,v,nelems,size,t,limit,e)if((nelems)+1>(size))((v)=cast(t*,luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) -#define luaM_reallocvector(L,v,oldn,n,t)((v)=cast(t*,luaM_reallocv(L,v,oldn,n,sizeof(t)))) -static void*luaM_realloc_(lua_State*L,void*block,size_t oldsize, -size_t size); -static void*luaM_toobig(lua_State*L); -static void*luaM_growaux_(lua_State*L,void*block,int*size, -size_t size_elem,int limit, -const char*errormsg); -typedef struct Zio ZIO; -#define char2int(c)cast(int,cast(unsigned char,(c))) -#define zgetc(z)(((z)->n--)>0?char2int(*(z)->p++):luaZ_fill(z)) -typedef struct Mbuffer{ -char*buffer; -size_t n; -size_t buffsize; -}Mbuffer; -#define luaZ_initbuffer(L,buff)((buff)->buffer=NULL,(buff)->buffsize=0) -#define luaZ_buffer(buff)((buff)->buffer) -#define luaZ_sizebuffer(buff)((buff)->buffsize) -#define luaZ_bufflen(buff)((buff)->n) -#define luaZ_resetbuffer(buff)((buff)->n=0) -#define luaZ_resizebuffer(L,buff,size)(luaM_reallocvector(L,(buff)->buffer,(buff)->buffsize,size,char),(buff)->buffsize=size) -#define luaZ_freebuffer(L,buff)luaZ_resizebuffer(L,buff,0) -struct Zio{ -size_t n; -const char*p; -lua_Reader reader; -void*data; -lua_State*L; -}; -static int luaZ_fill(ZIO*z); -struct lua_longjmp; -#define gt(L)(&L->l_gt) -#define registry(L)(&G(L)->l_registry) -typedef struct stringtable{ -GCObject**hash; -lu_int32 nuse; -int size; -}stringtable; -typedef struct CallInfo{ -StkId base; -StkId func; -StkId top; -const Instruction*savedpc; -int nresults; -int tailcalls; -}CallInfo; -#define curr_func(L)(clvalue(L->ci->func)) -#define ci_func(ci)(clvalue((ci)->func)) -#define f_isLua(ci)(!ci_func(ci)->c.isC) -#define isLua(ci)(ttisfunction((ci)->func)&&f_isLua(ci)) -typedef struct global_State{ -stringtable strt; -lua_Alloc frealloc; -void*ud; -lu_byte currentwhite; -lu_byte gcstate; -int sweepstrgc; -GCObject*rootgc; -GCObject**sweepgc; -GCObject*gray; -GCObject*grayagain; -GCObject*weak; -GCObject*tmudata; -Mbuffer buff; -lu_mem GCthreshold; -lu_mem totalbytes; -lu_mem estimate; -lu_mem gcdept; -int gcpause; -int gcstepmul; -lua_CFunction panic; -TValue l_registry; -struct lua_State*mainthread; -UpVal uvhead; -struct Table*mt[(8+1)]; -TString*tmname[TM_N]; -}global_State; -struct lua_State{ -GCObject*next;lu_byte tt;lu_byte marked; -lu_byte status; -StkId top; -StkId base; -global_State*l_G; -CallInfo*ci; -const Instruction*savedpc; -StkId stack_last; -StkId stack; -CallInfo*end_ci; -CallInfo*base_ci; -int stacksize; -int size_ci; -unsigned short nCcalls; -unsigned short baseCcalls; -lu_byte hookmask; -lu_byte allowhook; -int basehookcount; -int hookcount; -lua_Hook hook; -TValue l_gt; -TValue env; -GCObject*openupval; -GCObject*gclist; -struct lua_longjmp*errorJmp; -ptrdiff_t errfunc; -}; -#define G(L)(L->l_G) -union GCObject{ -GCheader gch; -union TString ts; -union Udata u; -union Closure cl; -struct Table h; -struct Proto p; -struct UpVal uv; -struct lua_State th; -}; -#define rawgco2ts(o)check_exp((o)->gch.tt==4,&((o)->ts)) -#define gco2ts(o)(&rawgco2ts(o)->tsv) -#define rawgco2u(o)check_exp((o)->gch.tt==7,&((o)->u)) -#define gco2u(o)(&rawgco2u(o)->uv) -#define gco2cl(o)check_exp((o)->gch.tt==6,&((o)->cl)) -#define gco2h(o)check_exp((o)->gch.tt==5,&((o)->h)) -#define gco2p(o)check_exp((o)->gch.tt==(8+1),&((o)->p)) -#define gco2uv(o)check_exp((o)->gch.tt==(8+2),&((o)->uv)) -#define ngcotouv(o)check_exp((o)==NULL||(o)->gch.tt==(8+2),&((o)->uv)) -#define gco2th(o)check_exp((o)->gch.tt==8,&((o)->th)) -#define obj2gco(v)(cast(GCObject*,(v))) -static void luaE_freethread(lua_State*L,lua_State*L1); -#define pcRel(pc,p)(cast(int,(pc)-(p)->code)-1) -#define getline_(f,pc)(((f)->lineinfo)?(f)->lineinfo[pc]:0) -#define resethookcount(L)(L->hookcount=L->basehookcount) -static void luaG_typeerror(lua_State*L,const TValue*o, -const char*opname); -static void luaG_runerror(lua_State*L,const char*fmt,...); -#define luaD_checkstack(L,n)if((char*)L->stack_last-(char*)L->top<=(n)*(int)sizeof(TValue))luaD_growstack(L,n);else condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1)); -#define incr_top(L){luaD_checkstack(L,1);L->top++;} -#define savestack(L,p)((char*)(p)-(char*)L->stack) -#define restorestack(L,n)((TValue*)((char*)L->stack+(n))) -#define saveci(L,p)((char*)(p)-(char*)L->base_ci) -#define restoreci(L,n)((CallInfo*)((char*)L->base_ci+(n))) -typedef void(*Pfunc)(lua_State*L,void*ud); -static int luaD_poscall(lua_State*L,StkId firstResult); -static void luaD_reallocCI(lua_State*L,int newsize); -static void luaD_reallocstack(lua_State*L,int newsize); -static void luaD_growstack(lua_State*L,int n); -static void luaD_throw(lua_State*L,int errcode); -static void*luaM_growaux_(lua_State*L,void*block,int*size,size_t size_elems, -int limit,const char*errormsg){ -void*newblock; -int newsize; -if(*size>=limit/2){ -if(*size>=limit) -luaG_runerror(L,errormsg); -newsize=limit; -} -else{ -newsize=(*size)*2; -if(newsize<4) -newsize=4; -} -newblock=luaM_reallocv(L,block,*size,newsize,size_elems); -*size=newsize; -return newblock; -} -static void*luaM_toobig(lua_State*L){ -luaG_runerror(L,"memory allocation error: block too big"); -return NULL; -} -static void*luaM_realloc_(lua_State*L,void*block,size_t osize,size_t nsize){ -global_State*g=G(L); -block=(*g->frealloc)(g->ud,block,osize,nsize); -if(block==NULL&&nsize>0) -luaD_throw(L,4); -g->totalbytes=(g->totalbytes-osize)+nsize; -return block; -} -#define resetbits(x,m)((x)&=cast(lu_byte,~(m))) -#define setbits(x,m)((x)|=(m)) -#define testbits(x,m)((x)&(m)) -#define bitmask(b)(1<<(b)) -#define bit2mask(b1,b2)(bitmask(b1)|bitmask(b2)) -#define l_setbit(x,b)setbits(x,bitmask(b)) -#define resetbit(x,b)resetbits(x,bitmask(b)) -#define testbit(x,b)testbits(x,bitmask(b)) -#define set2bits(x,b1,b2)setbits(x,(bit2mask(b1,b2))) -#define reset2bits(x,b1,b2)resetbits(x,(bit2mask(b1,b2))) -#define test2bits(x,b1,b2)testbits(x,(bit2mask(b1,b2))) -#define iswhite(x)test2bits((x)->gch.marked,0,1) -#define isblack(x)testbit((x)->gch.marked,2) -#define isgray(x)(!isblack(x)&&!iswhite(x)) -#define otherwhite(g)(g->currentwhite^bit2mask(0,1)) -#define isdead(g,v)((v)->gch.marked&otherwhite(g)&bit2mask(0,1)) -#define changewhite(x)((x)->gch.marked^=bit2mask(0,1)) -#define gray2black(x)l_setbit((x)->gch.marked,2) -#define valiswhite(x)(iscollectable(x)&&iswhite(gcvalue(x))) -#define luaC_white(g)cast(lu_byte,(g)->currentwhite&bit2mask(0,1)) -#define luaC_checkGC(L){condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));if(G(L)->totalbytes>=G(L)->GCthreshold)luaC_step(L);} -#define luaC_barrier(L,p,v){if(valiswhite(v)&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),gcvalue(v));} -#define luaC_barriert(L,t,v){if(valiswhite(v)&&isblack(obj2gco(t)))luaC_barrierback(L,t);} -#define luaC_objbarrier(L,p,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),obj2gco(o));} -#define luaC_objbarriert(L,t,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(t)))luaC_barrierback(L,t);} -static void luaC_step(lua_State*L); -static void luaC_link(lua_State*L,GCObject*o,lu_byte tt); -static void luaC_linkupval(lua_State*L,UpVal*uv); -static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v); -static void luaC_barrierback(lua_State*L,Table*t); -#define sizestring(s)(sizeof(union TString)+((s)->len+1)*sizeof(char)) -#define sizeudata(u)(sizeof(union Udata)+(u)->len) -#define luaS_new(L,s)(luaS_newlstr(L,s,strlen(s))) -#define luaS_newliteral(L,s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1)) -#define luaS_fix(s)l_setbit((s)->tsv.marked,5) -static TString*luaS_newlstr(lua_State*L,const char*str,size_t l); -#define tostring(L,o)((ttype(o)==4)||(luaV_tostring(L,o))) -#define tonumber(o,n)(ttype(o)==3||(((o)=luaV_tonumber(o,n))!=NULL)) -#define equalobj(L,o1,o2)(ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2)) -static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2); -static const TValue*luaV_tonumber(const TValue*obj,TValue*n); -static int luaV_tostring(lua_State*L,StkId obj); -static void luaV_execute(lua_State*L,int nexeccalls); -static void luaV_concat(lua_State*L,int total,int last); -static const TValue luaO_nilobject_={{NULL},0}; -static int luaO_int2fb(unsigned int x){ -int e=0; -while(x>=16){ -x=(x+1)>>1; -e++; -} -if(x<8)return x; -else return((e+1)<<3)|(cast_int(x)-8); -} -static int luaO_fb2int(int x){ -int e=(x>>3)&31; -if(e==0)return x; -else return((x&7)+8)<<(e-1); -} -static int luaO_log2(unsigned int x){ -static const lu_byte log_2[256]={ -0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, -6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, -7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, -7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 -}; -int l=-1; -while(x>=256){l+=8;x>>=8;} -return l+log_2[x]; -} -static int luaO_rawequalObj(const TValue*t1,const TValue*t2){ -if(ttype(t1)!=ttype(t2))return 0; -else switch(ttype(t1)){ -case 0: -return 1; -case 3: -return luai_numeq(nvalue(t1),nvalue(t2)); -case 1: -return bvalue(t1)==bvalue(t2); -case 2: -return pvalue(t1)==pvalue(t2); -default: -return gcvalue(t1)==gcvalue(t2); -} -} -static int luaO_str2d(const char*s,lua_Number*result){ -char*endptr; -*result=lua_str2number(s,&endptr); -if(endptr==s)return 0; -if(*endptr=='x'||*endptr=='X') -*result=cast_num(strtoul(s,&endptr,16)); -if(*endptr=='\0')return 1; -while(isspace(cast(unsigned char,*endptr)))endptr++; -if(*endptr!='\0')return 0; -return 1; -} -static void pushstr(lua_State*L,const char*str){ -setsvalue(L,L->top,luaS_new(L,str)); -incr_top(L); -} -static const char*luaO_pushvfstring(lua_State*L,const char*fmt,va_list argp){ -int n=1; -pushstr(L,""); -for(;;){ -const char*e=strchr(fmt,'%'); -if(e==NULL)break; -setsvalue(L,L->top,luaS_newlstr(L,fmt,e-fmt)); -incr_top(L); -switch(*(e+1)){ -case's':{ -const char*s=va_arg(argp,char*); -if(s==NULL)s="(null)"; -pushstr(L,s); -break; -} -case'c':{ -char buff[2]; -buff[0]=cast(char,va_arg(argp,int)); -buff[1]='\0'; -pushstr(L,buff); -break; -} -case'd':{ -setnvalue(L->top,cast_num(va_arg(argp,int))); -incr_top(L); -break; -} -case'f':{ -setnvalue(L->top,cast_num(va_arg(argp,l_uacNumber))); -incr_top(L); -break; -} -case'p':{ -char buff[4*sizeof(void*)+8]; -sprintf(buff,"%p",va_arg(argp,void*)); -pushstr(L,buff); -break; -} -case'%':{ -pushstr(L,"%"); -break; -} -default:{ -char buff[3]; -buff[0]='%'; -buff[1]=*(e+1); -buff[2]='\0'; -pushstr(L,buff); -break; -} -} -n+=2; -fmt=e+2; -} -pushstr(L,fmt); -luaV_concat(L,n+1,cast_int(L->top-L->base)-1); -L->top-=n; -return svalue(L->top-1); -} -static const char*luaO_pushfstring(lua_State*L,const char*fmt,...){ -const char*msg; -va_list argp; -va_start(argp,fmt); -msg=luaO_pushvfstring(L,fmt,argp); -va_end(argp); -return msg; -} -static void luaO_chunkid(char*out,const char*source,size_t bufflen){ -if(*source=='='){ -strncpy(out,source+1,bufflen); -out[bufflen-1]='\0'; -} -else{ -if(*source=='@'){ -size_t l; -source++; -bufflen-=sizeof(" '...' "); -l=strlen(source); -strcpy(out,""); -if(l>bufflen){ -source+=(l-bufflen); -strcat(out,"..."); -} -strcat(out,source); -} -else{ -size_t len=strcspn(source,"\n\r"); -bufflen-=sizeof(" [string \"...\"] "); -if(len>bufflen)len=bufflen; -strcpy(out,"[string \""); -if(source[len]!='\0'){ -strncat(out,source,len); -strcat(out,"..."); -} -else -strcat(out,source); -strcat(out,"\"]"); -} -} -} -#define gnode(t,i)(&(t)->node[i]) -#define gkey(n)(&(n)->i_key.nk) -#define gval(n)(&(n)->i_val) -#define gnext(n)((n)->i_key.nk.next) -#define key2tval(n)(&(n)->i_key.tvk) -static TValue*luaH_setnum(lua_State*L,Table*t,int key); -static const TValue*luaH_getstr(Table*t,TString*key); -static TValue*luaH_set(lua_State*L,Table*t,const TValue*key); -static const char*const luaT_typenames[]={ -"nil","boolean","userdata","number", -"string","table","function","userdata","thread", -"proto","upval" -}; -static void luaT_init(lua_State*L){ -static const char*const luaT_eventname[]={ -"__index","__newindex", -"__gc","__mode","__eq", -"__add","__sub","__mul","__div","__mod", -"__pow","__unm","__len","__lt","__le", -"__concat","__call" -}; -int i; -for(i=0;itmname[i]=luaS_new(L,luaT_eventname[i]); -luaS_fix(G(L)->tmname[i]); -} -} -static const TValue*luaT_gettm(Table*events,TMS event,TString*ename){ -const TValue*tm=luaH_getstr(events,ename); -if(ttisnil(tm)){ -events->flags|=cast_byte(1u<metatable; -break; -case 7: -mt=uvalue(o)->metatable; -break; -default: -mt=G(L)->mt[ttype(o)]; -} -return(mt?luaH_getstr(mt,G(L)->tmname[event]):(&luaO_nilobject_)); -} -#define sizeCclosure(n)(cast(int,sizeof(CClosure))+cast(int,sizeof(TValue)*((n)-1))) -#define sizeLclosure(n)(cast(int,sizeof(LClosure))+cast(int,sizeof(TValue*)*((n)-1))) -static Closure*luaF_newCclosure(lua_State*L,int nelems,Table*e){ -Closure*c=cast(Closure*,luaM_malloc(L,sizeCclosure(nelems))); -luaC_link(L,obj2gco(c),6); -c->c.isC=1; -c->c.env=e; -c->c.nupvalues=cast_byte(nelems); -return c; -} -static Closure*luaF_newLclosure(lua_State*L,int nelems,Table*e){ -Closure*c=cast(Closure*,luaM_malloc(L,sizeLclosure(nelems))); -luaC_link(L,obj2gco(c),6); -c->l.isC=0; -c->l.env=e; -c->l.nupvalues=cast_byte(nelems); -while(nelems--)c->l.upvals[nelems]=NULL; -return c; -} -static UpVal*luaF_newupval(lua_State*L){ -UpVal*uv=luaM_new(L,UpVal); -luaC_link(L,obj2gco(uv),(8+2)); -uv->v=&uv->u.value; -setnilvalue(uv->v); -return uv; -} -static UpVal*luaF_findupval(lua_State*L,StkId level){ -global_State*g=G(L); -GCObject**pp=&L->openupval; -UpVal*p; -UpVal*uv; -while(*pp!=NULL&&(p=ngcotouv(*pp))->v>=level){ -if(p->v==level){ -if(isdead(g,obj2gco(p))) -changewhite(obj2gco(p)); -return p; -} -pp=&p->next; -} -uv=luaM_new(L,UpVal); -uv->tt=(8+2); -uv->marked=luaC_white(g); -uv->v=level; -uv->next=*pp; -*pp=obj2gco(uv); -uv->u.l.prev=&g->uvhead; -uv->u.l.next=g->uvhead.u.l.next; -uv->u.l.next->u.l.prev=uv; -g->uvhead.u.l.next=uv; -return uv; -} -static void unlinkupval(UpVal*uv){ -uv->u.l.next->u.l.prev=uv->u.l.prev; -uv->u.l.prev->u.l.next=uv->u.l.next; -} -static void luaF_freeupval(lua_State*L,UpVal*uv){ -if(uv->v!=&uv->u.value) -unlinkupval(uv); -luaM_free(L,uv); -} -static void luaF_close(lua_State*L,StkId level){ -UpVal*uv; -global_State*g=G(L); -while(L->openupval!=NULL&&(uv=ngcotouv(L->openupval))->v>=level){ -GCObject*o=obj2gco(uv); -L->openupval=uv->next; -if(isdead(g,o)) -luaF_freeupval(L,uv); -else{ -unlinkupval(uv); -setobj(L,&uv->u.value,uv->v); -uv->v=&uv->u.value; -luaC_linkupval(L,uv); -} -} -} -static Proto*luaF_newproto(lua_State*L){ -Proto*f=luaM_new(L,Proto); -luaC_link(L,obj2gco(f),(8+1)); -f->k=NULL; -f->sizek=0; -f->p=NULL; -f->sizep=0; -f->code=NULL; -f->sizecode=0; -f->sizelineinfo=0; -f->sizeupvalues=0; -f->nups=0; -f->upvalues=NULL; -f->numparams=0; -f->is_vararg=0; -f->maxstacksize=0; -f->lineinfo=NULL; -f->sizelocvars=0; -f->locvars=NULL; -f->linedefined=0; -f->lastlinedefined=0; -f->source=NULL; -return f; -} -static void luaF_freeproto(lua_State*L,Proto*f){ -luaM_freearray(L,f->code,f->sizecode,Instruction); -luaM_freearray(L,f->p,f->sizep,Proto*); -luaM_freearray(L,f->k,f->sizek,TValue); -luaM_freearray(L,f->lineinfo,f->sizelineinfo,int); -luaM_freearray(L,f->locvars,f->sizelocvars,struct LocVar); -luaM_freearray(L,f->upvalues,f->sizeupvalues,TString*); -luaM_free(L,f); -} -static void luaF_freeclosure(lua_State*L,Closure*c){ -int size=(c->c.isC)?sizeCclosure(c->c.nupvalues): -sizeLclosure(c->l.nupvalues); -luaM_freemem(L,c,size); -} -#define MASK1(n,p)((~((~(Instruction)0)<>0)&MASK1(6,0))) -#define SET_OPCODE(i,o)((i)=(((i)&MASK0(6,0))|((cast(Instruction,o)<<0)&MASK1(6,0)))) -#define GETARG_A(i)(cast(int,((i)>>(0+6))&MASK1(8,0))) -#define SETARG_A(i,u)((i)=(((i)&MASK0(8,(0+6)))|((cast(Instruction,u)<<(0+6))&MASK1(8,(0+6))))) -#define GETARG_B(i)(cast(int,((i)>>(((0+6)+8)+9))&MASK1(9,0))) -#define SETARG_B(i,b)((i)=(((i)&MASK0(9,(((0+6)+8)+9)))|((cast(Instruction,b)<<(((0+6)+8)+9))&MASK1(9,(((0+6)+8)+9))))) -#define GETARG_C(i)(cast(int,((i)>>((0+6)+8))&MASK1(9,0))) -#define SETARG_C(i,b)((i)=(((i)&MASK0(9,((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1(9,((0+6)+8))))) -#define GETARG_Bx(i)(cast(int,((i)>>((0+6)+8))&MASK1((9+9),0))) -#define SETARG_Bx(i,b)((i)=(((i)&MASK0((9+9),((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1((9+9),((0+6)+8))))) -#define GETARG_sBx(i)(GETARG_Bx(i)-(((1<<(9+9))-1)>>1)) -#define SETARG_sBx(i,b)SETARG_Bx((i),cast(unsigned int,(b)+(((1<<(9+9))-1)>>1))) -#define CREATE_ABC(o,a,b,c)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,b)<<(((0+6)+8)+9))|(cast(Instruction,c)<<((0+6)+8))) -#define CREATE_ABx(o,a,bc)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,bc)<<((0+6)+8))) -#define ISK(x)((x)&(1<<(9-1))) -#define INDEXK(r)((int)(r)&~(1<<(9-1))) -#define RKASK(x)((x)|(1<<(9-1))) -static const lu_byte luaP_opmodes[(cast(int,OP_VARARG)+1)]; -#define getBMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>4)&3)) -#define getCMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>2)&3)) -#define testTMode(m)(luaP_opmodes[m]&(1<<7)) -typedef struct expdesc{ -expkind k; -union{ -struct{int info,aux;}s; -lua_Number nval; -}u; -int t; -int f; -}expdesc; -typedef struct upvaldesc{ -lu_byte k; -lu_byte info; -}upvaldesc; -struct BlockCnt; -typedef struct FuncState{ -Proto*f; -Table*h; -struct FuncState*prev; -struct LexState*ls; -struct lua_State*L; -struct BlockCnt*bl; -int pc; -int lasttarget; -int jpc; -int freereg; -int nk; -int np; -short nlocvars; -lu_byte nactvar; -upvaldesc upvalues[60]; -unsigned short actvar[200]; -}FuncState; -static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff, -const char*name); -struct lua_longjmp{ -struct lua_longjmp*previous; -jmp_buf b; -volatile int status; -}; -static void luaD_seterrorobj(lua_State*L,int errcode,StkId oldtop){ -switch(errcode){ -case 4:{ -setsvalue(L,oldtop,luaS_newliteral(L,"not enough memory")); -break; -} -case 5:{ -setsvalue(L,oldtop,luaS_newliteral(L,"error in error handling")); -break; -} -case 3: -case 2:{ -setobj(L,oldtop,L->top-1); -break; -} -} -L->top=oldtop+1; -} -static void restore_stack_limit(lua_State*L){ -if(L->size_ci>20000){ -int inuse=cast_int(L->ci-L->base_ci); -if(inuse+1<20000) -luaD_reallocCI(L,20000); -} -} -static void resetstack(lua_State*L,int status){ -L->ci=L->base_ci; -L->base=L->ci->base; -luaF_close(L,L->base); -luaD_seterrorobj(L,status,L->base); -L->nCcalls=L->baseCcalls; -L->allowhook=1; -restore_stack_limit(L); -L->errfunc=0; -L->errorJmp=NULL; -} -static void luaD_throw(lua_State*L,int errcode){ -if(L->errorJmp){ -L->errorJmp->status=errcode; -LUAI_THROW(L,L->errorJmp); -} -else{ -L->status=cast_byte(errcode); -if(G(L)->panic){ -resetstack(L,errcode); -G(L)->panic(L); -} -exit(EXIT_FAILURE); -} -} -static int luaD_rawrunprotected(lua_State*L,Pfunc f,void*ud){ -struct lua_longjmp lj; -lj.status=0; -lj.previous=L->errorJmp; -L->errorJmp=&lj; -LUAI_TRY(L,&lj, -(*f)(L,ud); -); -L->errorJmp=lj.previous; -return lj.status; -} -static void correctstack(lua_State*L,TValue*oldstack){ -CallInfo*ci; -GCObject*up; -L->top=(L->top-oldstack)+L->stack; -for(up=L->openupval;up!=NULL;up=up->gch.next) -gco2uv(up)->v=(gco2uv(up)->v-oldstack)+L->stack; -for(ci=L->base_ci;ci<=L->ci;ci++){ -ci->top=(ci->top-oldstack)+L->stack; -ci->base=(ci->base-oldstack)+L->stack; -ci->func=(ci->func-oldstack)+L->stack; -} -L->base=(L->base-oldstack)+L->stack; -} -static void luaD_reallocstack(lua_State*L,int newsize){ -TValue*oldstack=L->stack; -int realsize=newsize+1+5; -luaM_reallocvector(L,L->stack,L->stacksize,realsize,TValue); -L->stacksize=realsize; -L->stack_last=L->stack+newsize; -correctstack(L,oldstack); -} -static void luaD_reallocCI(lua_State*L,int newsize){ -CallInfo*oldci=L->base_ci; -luaM_reallocvector(L,L->base_ci,L->size_ci,newsize,CallInfo); -L->size_ci=newsize; -L->ci=(L->ci-oldci)+L->base_ci; -L->end_ci=L->base_ci+L->size_ci-1; -} -static void luaD_growstack(lua_State*L,int n){ -if(n<=L->stacksize) -luaD_reallocstack(L,2*L->stacksize); -else -luaD_reallocstack(L,L->stacksize+n); -} -static CallInfo*growCI(lua_State*L){ -if(L->size_ci>20000) -luaD_throw(L,5); -else{ -luaD_reallocCI(L,2*L->size_ci); -if(L->size_ci>20000) -luaG_runerror(L,"stack overflow"); -} -return++L->ci; -} -static StkId adjust_varargs(lua_State*L,Proto*p,int actual){ -int i; -int nfixargs=p->numparams; -Table*htab=NULL; -StkId base,fixed; -for(;actualtop++); -fixed=L->top-actual; -base=L->top; -for(i=0;itop++,fixed+i); -setnilvalue(fixed+i); -} -if(htab){ -sethvalue(L,L->top++,htab); -} -return base; -} -static StkId tryfuncTM(lua_State*L,StkId func){ -const TValue*tm=luaT_gettmbyobj(L,func,TM_CALL); -StkId p; -ptrdiff_t funcr=savestack(L,func); -if(!ttisfunction(tm)) -luaG_typeerror(L,func,"call"); -for(p=L->top;p>func;p--)setobj(L,p,p-1); -incr_top(L); -func=restorestack(L,funcr); -setobj(L,func,tm); -return func; -} -#define inc_ci(L)((L->ci==L->end_ci)?growCI(L):(condhardstacktests(luaD_reallocCI(L,L->size_ci)),++L->ci)) -static int luaD_precall(lua_State*L,StkId func,int nresults){ -LClosure*cl; -ptrdiff_t funcr; -if(!ttisfunction(func)) -func=tryfuncTM(L,func); -funcr=savestack(L,func); -cl=&clvalue(func)->l; -L->ci->savedpc=L->savedpc; -if(!cl->isC){ -CallInfo*ci; -StkId st,base; -Proto*p=cl->p; -luaD_checkstack(L,p->maxstacksize); -func=restorestack(L,funcr); -if(!p->is_vararg){ -base=func+1; -if(L->top>base+p->numparams) -L->top=base+p->numparams; -} -else{ -int nargs=cast_int(L->top-func)-1; -base=adjust_varargs(L,p,nargs); -func=restorestack(L,funcr); -} -ci=inc_ci(L); -ci->func=func; -L->base=ci->base=base; -ci->top=L->base+p->maxstacksize; -L->savedpc=p->code; -ci->tailcalls=0; -ci->nresults=nresults; -for(st=L->top;sttop;st++) -setnilvalue(st); -L->top=ci->top; -return 0; -} -else{ -CallInfo*ci; -int n; -luaD_checkstack(L,20); -ci=inc_ci(L); -ci->func=restorestack(L,funcr); -L->base=ci->base=ci->func+1; -ci->top=L->top+20; -ci->nresults=nresults; -n=(*curr_func(L)->c.f)(L); -if(n<0) -return 2; -else{ -luaD_poscall(L,L->top-n); -return 1; -} -} -} -static int luaD_poscall(lua_State*L,StkId firstResult){ -StkId res; -int wanted,i; -CallInfo*ci; -ci=L->ci--; -res=ci->func; -wanted=ci->nresults; -L->base=(ci-1)->base; -L->savedpc=(ci-1)->savedpc; -for(i=wanted;i!=0&&firstResulttop;i--) -setobj(L,res++,firstResult++); -while(i-->0) -setnilvalue(res++); -L->top=res; -return(wanted-(-1)); -} -static void luaD_call(lua_State*L,StkId func,int nResults){ -if(++L->nCcalls>=200){ -if(L->nCcalls==200) -luaG_runerror(L,"C stack overflow"); -else if(L->nCcalls>=(200+(200>>3))) -luaD_throw(L,5); -} -if(luaD_precall(L,func,nResults)==0) -luaV_execute(L,1); -L->nCcalls--; -luaC_checkGC(L); -} -static int luaD_pcall(lua_State*L,Pfunc func,void*u, -ptrdiff_t old_top,ptrdiff_t ef){ -int status; -unsigned short oldnCcalls=L->nCcalls; -ptrdiff_t old_ci=saveci(L,L->ci); -lu_byte old_allowhooks=L->allowhook; -ptrdiff_t old_errfunc=L->errfunc; -L->errfunc=ef; -status=luaD_rawrunprotected(L,func,u); -if(status!=0){ -StkId oldtop=restorestack(L,old_top); -luaF_close(L,oldtop); -luaD_seterrorobj(L,status,oldtop); -L->nCcalls=oldnCcalls; -L->ci=restoreci(L,old_ci); -L->base=L->ci->base; -L->savedpc=L->ci->savedpc; -L->allowhook=old_allowhooks; -restore_stack_limit(L); -} -L->errfunc=old_errfunc; -return status; -} -struct SParser{ -ZIO*z; -Mbuffer buff; -const char*name; -}; -static void f_parser(lua_State*L,void*ud){ -int i; -Proto*tf; -Closure*cl; -struct SParser*p=cast(struct SParser*,ud); -luaC_checkGC(L); -tf=luaY_parser(L,p->z, -&p->buff,p->name); -cl=luaF_newLclosure(L,tf->nups,hvalue(gt(L))); -cl->l.p=tf; -for(i=0;inups;i++) -cl->l.upvals[i]=luaF_newupval(L); -setclvalue(L,L->top,cl); -incr_top(L); -} -static int luaD_protectedparser(lua_State*L,ZIO*z,const char*name){ -struct SParser p; -int status; -p.z=z;p.name=name; -luaZ_initbuffer(L,&p.buff); -status=luaD_pcall(L,f_parser,&p,savestack(L,L->top),L->errfunc); -luaZ_freebuffer(L,&p.buff); -return status; -} -static void luaS_resize(lua_State*L,int newsize){ -GCObject**newhash; -stringtable*tb; -int i; -if(G(L)->gcstate==2) -return; -newhash=luaM_newvector(L,newsize,GCObject*); -tb=&G(L)->strt; -for(i=0;isize;i++){ -GCObject*p=tb->hash[i]; -while(p){ -GCObject*next=p->gch.next; -unsigned int h=gco2ts(p)->hash; -int h1=lmod(h,newsize); -p->gch.next=newhash[h1]; -newhash[h1]=p; -p=next; -} -} -luaM_freearray(L,tb->hash,tb->size,TString*); -tb->size=newsize; -tb->hash=newhash; -} -static TString*newlstr(lua_State*L,const char*str,size_t l, -unsigned int h){ -TString*ts; -stringtable*tb; -if(l+1>(((size_t)(~(size_t)0)-2)-sizeof(TString))/sizeof(char)) -luaM_toobig(L); -ts=cast(TString*,luaM_malloc(L,(l+1)*sizeof(char)+sizeof(TString))); -ts->tsv.len=l; -ts->tsv.hash=h; -ts->tsv.marked=luaC_white(G(L)); -ts->tsv.tt=4; -ts->tsv.reserved=0; -memcpy(ts+1,str,l*sizeof(char)); -((char*)(ts+1))[l]='\0'; -tb=&G(L)->strt; -h=lmod(h,tb->size); -ts->tsv.next=tb->hash[h]; -tb->hash[h]=obj2gco(ts); -tb->nuse++; -if(tb->nuse>cast(lu_int32,tb->size)&&tb->size<=(INT_MAX-2)/2) -luaS_resize(L,tb->size*2); -return ts; -} -static TString*luaS_newlstr(lua_State*L,const char*str,size_t l){ -GCObject*o; -unsigned int h=cast(unsigned int,l); -size_t step=(l>>5)+1; -size_t l1; -for(l1=l;l1>=step;l1-=step) -h=h^((h<<5)+(h>>2)+cast(unsigned char,str[l1-1])); -for(o=G(L)->strt.hash[lmod(h,G(L)->strt.size)]; -o!=NULL; -o=o->gch.next){ -TString*ts=rawgco2ts(o); -if(ts->tsv.len==l&&(memcmp(str,getstr(ts),l)==0)){ -if(isdead(G(L),o))changewhite(o); -return ts; -} -} -return newlstr(L,str,l,h); -} -static Udata*luaS_newudata(lua_State*L,size_t s,Table*e){ -Udata*u; -if(s>((size_t)(~(size_t)0)-2)-sizeof(Udata)) -luaM_toobig(L); -u=cast(Udata*,luaM_malloc(L,s+sizeof(Udata))); -u->uv.marked=luaC_white(G(L)); -u->uv.tt=7; -u->uv.len=s; -u->uv.metatable=NULL; -u->uv.env=e; -u->uv.next=G(L)->mainthread->next; -G(L)->mainthread->next=obj2gco(u); -return u; -} -#define hashpow2(t,n)(gnode(t,lmod((n),sizenode(t)))) -#define hashstr(t,str)hashpow2(t,(str)->tsv.hash) -#define hashboolean(t,p)hashpow2(t,p) -#define hashmod(t,n)(gnode(t,((n)%((sizenode(t)-1)|1)))) -#define hashpointer(t,p)hashmod(t,IntPoint(p)) -static const Node dummynode_={ -{{NULL},0}, -{{{NULL},0,NULL}} -}; -static Node*hashnum(const Table*t,lua_Number n){ -unsigned int a[cast_int(sizeof(lua_Number)/sizeof(int))]; -int i; -if(luai_numeq(n,0)) -return gnode(t,0); -memcpy(a,&n,sizeof(a)); -for(i=1;isizearray) -return i-1; -else{ -Node*n=mainposition(t,key); -do{ -if(luaO_rawequalObj(key2tval(n),key)|| -(ttype(gkey(n))==(8+3)&&iscollectable(key)&& -gcvalue(gkey(n))==gcvalue(key))){ -i=cast_int(n-gnode(t,0)); -return i+t->sizearray; -} -else n=gnext(n); -}while(n); -luaG_runerror(L,"invalid key to "LUA_QL("next")); -return 0; -} -} -static int luaH_next(lua_State*L,Table*t,StkId key){ -int i=findindex(L,t,key); -for(i++;isizearray;i++){ -if(!ttisnil(&t->array[i])){ -setnvalue(key,cast_num(i+1)); -setobj(L,key+1,&t->array[i]); -return 1; -} -} -for(i-=t->sizearray;i<(int)sizenode(t);i++){ -if(!ttisnil(gval(gnode(t,i)))){ -setobj(L,key,key2tval(gnode(t,i))); -setobj(L,key+1,gval(gnode(t,i))); -return 1; -} -} -return 0; -} -static int computesizes(int nums[],int*narray){ -int i; -int twotoi; -int a=0; -int na=0; -int n=0; -for(i=0,twotoi=1;twotoi/2<*narray;i++,twotoi*=2){ -if(nums[i]>0){ -a+=nums[i]; -if(a>twotoi/2){ -n=twotoi; -na=a; -} -} -if(a==*narray)break; -} -*narray=n; -return na; -} -static int countint(const TValue*key,int*nums){ -int k=arrayindex(key); -if(0t->sizearray){ -lim=t->sizearray; -if(i>lim) -break; -} -for(;i<=lim;i++){ -if(!ttisnil(&t->array[i-1])) -lc++; -} -nums[lg]+=lc; -ause+=lc; -} -return ause; -} -static int numusehash(const Table*t,int*nums,int*pnasize){ -int totaluse=0; -int ause=0; -int i=sizenode(t); -while(i--){ -Node*n=&t->node[i]; -if(!ttisnil(gval(n))){ -ause+=countint(key2tval(n),nums); -totaluse++; -} -} -*pnasize+=ause; -return totaluse; -} -static void setarrayvector(lua_State*L,Table*t,int size){ -int i; -luaM_reallocvector(L,t->array,t->sizearray,size,TValue); -for(i=t->sizearray;iarray[i]); -t->sizearray=size; -} -static void setnodevector(lua_State*L,Table*t,int size){ -int lsize; -if(size==0){ -t->node=cast(Node*,(&dummynode_)); -lsize=0; -} -else{ -int i; -lsize=ceillog2(size); -if(lsize>(32-2)) -luaG_runerror(L,"table overflow"); -size=twoto(lsize); -t->node=luaM_newvector(L,size,Node); -for(i=0;ilsizenode=cast_byte(lsize); -t->lastfree=gnode(t,size); -} -static void resize(lua_State*L,Table*t,int nasize,int nhsize){ -int i; -int oldasize=t->sizearray; -int oldhsize=t->lsizenode; -Node*nold=t->node; -if(nasize>oldasize) -setarrayvector(L,t,nasize); -setnodevector(L,t,nhsize); -if(nasizesizearray=nasize; -for(i=nasize;iarray[i])) -setobj(L,luaH_setnum(L,t,i+1),&t->array[i]); -} -luaM_reallocvector(L,t->array,oldasize,nasize,TValue); -} -for(i=twoto(oldhsize)-1;i>=0;i--){ -Node*old=nold+i; -if(!ttisnil(gval(old))) -setobj(L,luaH_set(L,t,key2tval(old)),gval(old)); -} -if(nold!=(&dummynode_)) -luaM_freearray(L,nold,twoto(oldhsize),Node); -} -static void luaH_resizearray(lua_State*L,Table*t,int nasize){ -int nsize=(t->node==(&dummynode_))?0:sizenode(t); -resize(L,t,nasize,nsize); -} -static void rehash(lua_State*L,Table*t,const TValue*ek){ -int nasize,na; -int nums[(32-2)+1]; -int i; -int totaluse; -for(i=0;i<=(32-2);i++)nums[i]=0; -nasize=numusearray(t,nums); -totaluse=nasize; -totaluse+=numusehash(t,nums,&nasize); -nasize+=countint(ek,nums); -totaluse++; -na=computesizes(nums,&nasize); -resize(L,t,nasize,totaluse-na); -} -static Table*luaH_new(lua_State*L,int narray,int nhash){ -Table*t=luaM_new(L,Table); -luaC_link(L,obj2gco(t),5); -t->metatable=NULL; -t->flags=cast_byte(~0); -t->array=NULL; -t->sizearray=0; -t->lsizenode=0; -t->node=cast(Node*,(&dummynode_)); -setarrayvector(L,t,narray); -setnodevector(L,t,nhash); -return t; -} -static void luaH_free(lua_State*L,Table*t){ -if(t->node!=(&dummynode_)) -luaM_freearray(L,t->node,sizenode(t),Node); -luaM_freearray(L,t->array,t->sizearray,TValue); -luaM_free(L,t); -} -static Node*getfreepos(Table*t){ -while(t->lastfree-->t->node){ -if(ttisnil(gkey(t->lastfree))) -return t->lastfree; -} -return NULL; -} -static TValue*newkey(lua_State*L,Table*t,const TValue*key){ -Node*mp=mainposition(t,key); -if(!ttisnil(gval(mp))||mp==(&dummynode_)){ -Node*othern; -Node*n=getfreepos(t); -if(n==NULL){ -rehash(L,t,key); -return luaH_set(L,t,key); -} -othern=mainposition(t,key2tval(mp)); -if(othern!=mp){ -while(gnext(othern)!=mp)othern=gnext(othern); -gnext(othern)=n; -*n=*mp; -gnext(mp)=NULL; -setnilvalue(gval(mp)); -} -else{ -gnext(n)=gnext(mp); -gnext(mp)=n; -mp=n; -} -} -gkey(mp)->value=key->value;gkey(mp)->tt=key->tt; -luaC_barriert(L,t,key); -return gval(mp); -} -static const TValue*luaH_getnum(Table*t,int key){ -if(cast(unsigned int,key-1)sizearray)) -return&t->array[key-1]; -else{ -lua_Number nk=cast_num(key); -Node*n=hashnum(t,nk); -do{ -if(ttisnumber(gkey(n))&&luai_numeq(nvalue(gkey(n)),nk)) -return gval(n); -else n=gnext(n); -}while(n); -return(&luaO_nilobject_); -} -} -static const TValue*luaH_getstr(Table*t,TString*key){ -Node*n=hashstr(t,key); -do{ -if(ttisstring(gkey(n))&&rawtsvalue(gkey(n))==key) -return gval(n); -else n=gnext(n); -}while(n); -return(&luaO_nilobject_); -} -static const TValue*luaH_get(Table*t,const TValue*key){ -switch(ttype(key)){ -case 0:return(&luaO_nilobject_); -case 4:return luaH_getstr(t,rawtsvalue(key)); -case 3:{ -int k; -lua_Number n=nvalue(key); -lua_number2int(k,n); -if(luai_numeq(cast_num(k),nvalue(key))) -return luaH_getnum(t,k); -} -default:{ -Node*n=mainposition(t,key); -do{ -if(luaO_rawequalObj(key2tval(n),key)) -return gval(n); -else n=gnext(n); -}while(n); -return(&luaO_nilobject_); -} -} -} -static TValue*luaH_set(lua_State*L,Table*t,const TValue*key){ -const TValue*p=luaH_get(t,key); -t->flags=0; -if(p!=(&luaO_nilobject_)) -return cast(TValue*,p); -else{ -if(ttisnil(key))luaG_runerror(L,"table index is nil"); -else if(ttisnumber(key)&&luai_numisnan(nvalue(key))) -luaG_runerror(L,"table index is NaN"); -return newkey(L,t,key); -} -} -static TValue*luaH_setnum(lua_State*L,Table*t,int key){ -const TValue*p=luaH_getnum(t,key); -if(p!=(&luaO_nilobject_)) -return cast(TValue*,p); -else{ -TValue k; -setnvalue(&k,cast_num(key)); -return newkey(L,t,&k); -} -} -static TValue*luaH_setstr(lua_State*L,Table*t,TString*key){ -const TValue*p=luaH_getstr(t,key); -if(p!=(&luaO_nilobject_)) -return cast(TValue*,p); -else{ -TValue k; -setsvalue(L,&k,key); -return newkey(L,t,&k); -} -} -static int unbound_search(Table*t,unsigned int j){ -unsigned int i=j; -j++; -while(!ttisnil(luaH_getnum(t,j))){ -i=j; -j*=2; -if(j>cast(unsigned int,(INT_MAX-2))){ -i=1; -while(!ttisnil(luaH_getnum(t,i)))i++; -return i-1; -} -} -while(j-i>1){ -unsigned int m=(i+j)/2; -if(ttisnil(luaH_getnum(t,m)))j=m; -else i=m; -} -return i; -} -static int luaH_getn(Table*t){ -unsigned int j=t->sizearray; -if(j>0&&ttisnil(&t->array[j-1])){ -unsigned int i=0; -while(j-i>1){ -unsigned int m=(i+j)/2; -if(ttisnil(&t->array[m-1]))j=m; -else i=m; -} -return i; -} -else if(t->node==(&dummynode_)) -return j; -else return unbound_search(t,j); -} -#define makewhite(g,x)((x)->gch.marked=cast_byte(((x)->gch.marked&cast_byte(~(bitmask(2)|bit2mask(0,1))))|luaC_white(g))) -#define white2gray(x)reset2bits((x)->gch.marked,0,1) -#define black2gray(x)resetbit((x)->gch.marked,2) -#define stringmark(s)reset2bits((s)->tsv.marked,0,1) -#define isfinalized(u)testbit((u)->marked,3) -#define markfinalized(u)l_setbit((u)->marked,3) -#define markvalue(g,o){checkconsistency(o);if(iscollectable(o)&&iswhite(gcvalue(o)))reallymarkobject(g,gcvalue(o));} -#define markobject(g,t){if(iswhite(obj2gco(t)))reallymarkobject(g,obj2gco(t));} -#define setthreshold(g)(g->GCthreshold=(g->estimate/100)*g->gcpause) -static void removeentry(Node*n){ -if(iscollectable(gkey(n))) -setttype(gkey(n),(8+3)); -} -static void reallymarkobject(global_State*g,GCObject*o){ -white2gray(o); -switch(o->gch.tt){ -case 4:{ -return; -} -case 7:{ -Table*mt=gco2u(o)->metatable; -gray2black(o); -if(mt)markobject(g,mt); -markobject(g,gco2u(o)->env); -return; -} -case(8+2):{ -UpVal*uv=gco2uv(o); -markvalue(g,uv->v); -if(uv->v==&uv->u.value) -gray2black(o); -return; -} -case 6:{ -gco2cl(o)->c.gclist=g->gray; -g->gray=o; -break; -} -case 5:{ -gco2h(o)->gclist=g->gray; -g->gray=o; -break; -} -case 8:{ -gco2th(o)->gclist=g->gray; -g->gray=o; -break; -} -case(8+1):{ -gco2p(o)->gclist=g->gray; -g->gray=o; -break; -} -default:; -} -} -static void marktmu(global_State*g){ -GCObject*u=g->tmudata; -if(u){ -do{ -u=u->gch.next; -makewhite(g,u); -reallymarkobject(g,u); -}while(u!=g->tmudata); -} -} -static size_t luaC_separateudata(lua_State*L,int all){ -global_State*g=G(L); -size_t deadmem=0; -GCObject**p=&g->mainthread->next; -GCObject*curr; -while((curr=*p)!=NULL){ -if(!(iswhite(curr)||all)||isfinalized(gco2u(curr))) -p=&curr->gch.next; -else if(fasttm(L,gco2u(curr)->metatable,TM_GC)==NULL){ -markfinalized(gco2u(curr)); -p=&curr->gch.next; -} -else{ -deadmem+=sizeudata(gco2u(curr)); -markfinalized(gco2u(curr)); -*p=curr->gch.next; -if(g->tmudata==NULL) -g->tmudata=curr->gch.next=curr; -else{ -curr->gch.next=g->tmudata->gch.next; -g->tmudata->gch.next=curr; -g->tmudata=curr; -} -} -} -return deadmem; -} -static int traversetable(global_State*g,Table*h){ -int i; -int weakkey=0; -int weakvalue=0; -const TValue*mode; -if(h->metatable) -markobject(g,h->metatable); -mode=gfasttm(g,h->metatable,TM_MODE); -if(mode&&ttisstring(mode)){ -weakkey=(strchr(svalue(mode),'k')!=NULL); -weakvalue=(strchr(svalue(mode),'v')!=NULL); -if(weakkey||weakvalue){ -h->marked&=~(bitmask(3)|bitmask(4)); -h->marked|=cast_byte((weakkey<<3)| -(weakvalue<<4)); -h->gclist=g->weak; -g->weak=obj2gco(h); -} -} -if(weakkey&&weakvalue)return 1; -if(!weakvalue){ -i=h->sizearray; -while(i--) -markvalue(g,&h->array[i]); -} -i=sizenode(h); -while(i--){ -Node*n=gnode(h,i); -if(ttisnil(gval(n))) -removeentry(n); -else{ -if(!weakkey)markvalue(g,gkey(n)); -if(!weakvalue)markvalue(g,gval(n)); -} -} -return weakkey||weakvalue; -} -static void traverseproto(global_State*g,Proto*f){ -int i; -if(f->source)stringmark(f->source); -for(i=0;isizek;i++) -markvalue(g,&f->k[i]); -for(i=0;isizeupvalues;i++){ -if(f->upvalues[i]) -stringmark(f->upvalues[i]); -} -for(i=0;isizep;i++){ -if(f->p[i]) -markobject(g,f->p[i]); -} -for(i=0;isizelocvars;i++){ -if(f->locvars[i].varname) -stringmark(f->locvars[i].varname); -} -} -static void traverseclosure(global_State*g,Closure*cl){ -markobject(g,cl->c.env); -if(cl->c.isC){ -int i; -for(i=0;ic.nupvalues;i++) -markvalue(g,&cl->c.upvalue[i]); -} -else{ -int i; -markobject(g,cl->l.p); -for(i=0;il.nupvalues;i++) -markobject(g,cl->l.upvals[i]); -} -} -static void checkstacksizes(lua_State*L,StkId max){ -int ci_used=cast_int(L->ci-L->base_ci); -int s_used=cast_int(max-L->stack); -if(L->size_ci>20000) -return; -if(4*ci_usedsize_ci&&2*8size_ci) -luaD_reallocCI(L,L->size_ci/2); -condhardstacktests(luaD_reallocCI(L,ci_used+1)); -if(4*s_usedstacksize&& -2*((2*20)+5)stacksize) -luaD_reallocstack(L,L->stacksize/2); -condhardstacktests(luaD_reallocstack(L,s_used)); -} -static void traversestack(global_State*g,lua_State*l){ -StkId o,lim; -CallInfo*ci; -markvalue(g,gt(l)); -lim=l->top; -for(ci=l->base_ci;ci<=l->ci;ci++){ -if(limtop)lim=ci->top; -} -for(o=l->stack;otop;o++) -markvalue(g,o); -for(;o<=lim;o++) -setnilvalue(o); -checkstacksizes(l,lim); -} -static l_mem propagatemark(global_State*g){ -GCObject*o=g->gray; -gray2black(o); -switch(o->gch.tt){ -case 5:{ -Table*h=gco2h(o); -g->gray=h->gclist; -if(traversetable(g,h)) -black2gray(o); -return sizeof(Table)+sizeof(TValue)*h->sizearray+ -sizeof(Node)*sizenode(h); -} -case 6:{ -Closure*cl=gco2cl(o); -g->gray=cl->c.gclist; -traverseclosure(g,cl); -return(cl->c.isC)?sizeCclosure(cl->c.nupvalues): -sizeLclosure(cl->l.nupvalues); -} -case 8:{ -lua_State*th=gco2th(o); -g->gray=th->gclist; -th->gclist=g->grayagain; -g->grayagain=o; -black2gray(o); -traversestack(g,th); -return sizeof(lua_State)+sizeof(TValue)*th->stacksize+ -sizeof(CallInfo)*th->size_ci; -} -case(8+1):{ -Proto*p=gco2p(o); -g->gray=p->gclist; -traverseproto(g,p); -return sizeof(Proto)+sizeof(Instruction)*p->sizecode+ -sizeof(Proto*)*p->sizep+ -sizeof(TValue)*p->sizek+ -sizeof(int)*p->sizelineinfo+ -sizeof(LocVar)*p->sizelocvars+ -sizeof(TString*)*p->sizeupvalues; -} -default:return 0; -} -} -static size_t propagateall(global_State*g){ -size_t m=0; -while(g->gray)m+=propagatemark(g); -return m; -} -static int iscleared(const TValue*o,int iskey){ -if(!iscollectable(o))return 0; -if(ttisstring(o)){ -stringmark(rawtsvalue(o)); -return 0; -} -return iswhite(gcvalue(o))|| -(ttisuserdata(o)&&(!iskey&&isfinalized(uvalue(o)))); -} -static void cleartable(GCObject*l){ -while(l){ -Table*h=gco2h(l); -int i=h->sizearray; -if(testbit(h->marked,4)){ -while(i--){ -TValue*o=&h->array[i]; -if(iscleared(o,0)) -setnilvalue(o); -} -} -i=sizenode(h); -while(i--){ -Node*n=gnode(h,i); -if(!ttisnil(gval(n))&& -(iscleared(key2tval(n),1)||iscleared(gval(n),0))){ -setnilvalue(gval(n)); -removeentry(n); -} -} -l=h->gclist; -} -} -static void freeobj(lua_State*L,GCObject*o){ -switch(o->gch.tt){ -case(8+1):luaF_freeproto(L,gco2p(o));break; -case 6:luaF_freeclosure(L,gco2cl(o));break; -case(8+2):luaF_freeupval(L,gco2uv(o));break; -case 5:luaH_free(L,gco2h(o));break; -case 8:{ -luaE_freethread(L,gco2th(o)); -break; -} -case 4:{ -G(L)->strt.nuse--; -luaM_freemem(L,o,sizestring(gco2ts(o))); -break; -} -case 7:{ -luaM_freemem(L,o,sizeudata(gco2u(o))); -break; -} -default:; -} -} -#define sweepwholelist(L,p)sweeplist(L,p,((lu_mem)(~(lu_mem)0)-2)) -static GCObject**sweeplist(lua_State*L,GCObject**p,lu_mem count){ -GCObject*curr; -global_State*g=G(L); -int deadmask=otherwhite(g); -while((curr=*p)!=NULL&&count-->0){ -if(curr->gch.tt==8) -sweepwholelist(L,&gco2th(curr)->openupval); -if((curr->gch.marked^bit2mask(0,1))&deadmask){ -makewhite(g,curr); -p=&curr->gch.next; -} -else{ -*p=curr->gch.next; -if(curr==g->rootgc) -g->rootgc=curr->gch.next; -freeobj(L,curr); -} -} -return p; -} -static void checkSizes(lua_State*L){ -global_State*g=G(L); -if(g->strt.nusestrt.size/4)&& -g->strt.size>32*2) -luaS_resize(L,g->strt.size/2); -if(luaZ_sizebuffer(&g->buff)>32*2){ -size_t newsize=luaZ_sizebuffer(&g->buff)/2; -luaZ_resizebuffer(L,&g->buff,newsize); -} -} -static void GCTM(lua_State*L){ -global_State*g=G(L); -GCObject*o=g->tmudata->gch.next; -Udata*udata=rawgco2u(o); -const TValue*tm; -if(o==g->tmudata) -g->tmudata=NULL; -else -g->tmudata->gch.next=udata->uv.next; -udata->uv.next=g->mainthread->next; -g->mainthread->next=o; -makewhite(g,o); -tm=fasttm(L,udata->uv.metatable,TM_GC); -if(tm!=NULL){ -lu_byte oldah=L->allowhook; -lu_mem oldt=g->GCthreshold; -L->allowhook=0; -g->GCthreshold=2*g->totalbytes; -setobj(L,L->top,tm); -setuvalue(L,L->top+1,udata); -L->top+=2; -luaD_call(L,L->top-2,0); -L->allowhook=oldah; -g->GCthreshold=oldt; -} -} -static void luaC_callGCTM(lua_State*L){ -while(G(L)->tmudata) -GCTM(L); -} -static void luaC_freeall(lua_State*L){ -global_State*g=G(L); -int i; -g->currentwhite=bit2mask(0,1)|bitmask(6); -sweepwholelist(L,&g->rootgc); -for(i=0;istrt.size;i++) -sweepwholelist(L,&g->strt.hash[i]); -} -static void markmt(global_State*g){ -int i; -for(i=0;i<(8+1);i++) -if(g->mt[i])markobject(g,g->mt[i]); -} -static void markroot(lua_State*L){ -global_State*g=G(L); -g->gray=NULL; -g->grayagain=NULL; -g->weak=NULL; -markobject(g,g->mainthread); -markvalue(g,gt(g->mainthread)); -markvalue(g,registry(L)); -markmt(g); -g->gcstate=1; -} -static void remarkupvals(global_State*g){ -UpVal*uv; -for(uv=g->uvhead.u.l.next;uv!=&g->uvhead;uv=uv->u.l.next){ -if(isgray(obj2gco(uv))) -markvalue(g,uv->v); -} -} -static void atomic(lua_State*L){ -global_State*g=G(L); -size_t udsize; -remarkupvals(g); -propagateall(g); -g->gray=g->weak; -g->weak=NULL; -markobject(g,L); -markmt(g); -propagateall(g); -g->gray=g->grayagain; -g->grayagain=NULL; -propagateall(g); -udsize=luaC_separateudata(L,0); -marktmu(g); -udsize+=propagateall(g); -cleartable(g->weak); -g->currentwhite=cast_byte(otherwhite(g)); -g->sweepstrgc=0; -g->sweepgc=&g->rootgc; -g->gcstate=2; -g->estimate=g->totalbytes-udsize; -} -static l_mem singlestep(lua_State*L){ -global_State*g=G(L); -switch(g->gcstate){ -case 0:{ -markroot(L); -return 0; -} -case 1:{ -if(g->gray) -return propagatemark(g); -else{ -atomic(L); -return 0; -} -} -case 2:{ -lu_mem old=g->totalbytes; -sweepwholelist(L,&g->strt.hash[g->sweepstrgc++]); -if(g->sweepstrgc>=g->strt.size) -g->gcstate=3; -g->estimate-=old-g->totalbytes; -return 10; -} -case 3:{ -lu_mem old=g->totalbytes; -g->sweepgc=sweeplist(L,g->sweepgc,40); -if(*g->sweepgc==NULL){ -checkSizes(L); -g->gcstate=4; -} -g->estimate-=old-g->totalbytes; -return 40*10; -} -case 4:{ -if(g->tmudata){ -GCTM(L); -if(g->estimate>100) -g->estimate-=100; -return 100; -} -else{ -g->gcstate=0; -g->gcdept=0; -return 0; -} -} -default:return 0; -} -} -static void luaC_step(lua_State*L){ -global_State*g=G(L); -l_mem lim=(1024u/100)*g->gcstepmul; -if(lim==0) -lim=(((lu_mem)(~(lu_mem)0)-2)-1)/2; -g->gcdept+=g->totalbytes-g->GCthreshold; -do{ -lim-=singlestep(L); -if(g->gcstate==0) -break; -}while(lim>0); -if(g->gcstate!=0){ -if(g->gcdept<1024u) -g->GCthreshold=g->totalbytes+1024u; -else{ -g->gcdept-=1024u; -g->GCthreshold=g->totalbytes; -} -} -else{ -setthreshold(g); -} -} -static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v){ -global_State*g=G(L); -if(g->gcstate==1) -reallymarkobject(g,v); -else -makewhite(g,o); -} -static void luaC_barrierback(lua_State*L,Table*t){ -global_State*g=G(L); -GCObject*o=obj2gco(t); -black2gray(o); -t->gclist=g->grayagain; -g->grayagain=o; -} -static void luaC_link(lua_State*L,GCObject*o,lu_byte tt){ -global_State*g=G(L); -o->gch.next=g->rootgc; -g->rootgc=o; -o->gch.marked=luaC_white(g); -o->gch.tt=tt; -} -static void luaC_linkupval(lua_State*L,UpVal*uv){ -global_State*g=G(L); -GCObject*o=obj2gco(uv); -o->gch.next=g->rootgc; -g->rootgc=o; -if(isgray(o)){ -if(g->gcstate==1){ -gray2black(o); -luaC_barrier(L,uv,uv->v); -} -else{ -makewhite(g,o); -} -} -} -typedef union{ -lua_Number r; -TString*ts; -}SemInfo; -typedef struct Token{ -int token; -SemInfo seminfo; -}Token; -typedef struct LexState{ -int current; -int linenumber; -int lastline; -Token t; -Token lookahead; -struct FuncState*fs; -struct lua_State*L; -ZIO*z; -Mbuffer*buff; -TString*source; -char decpoint; -}LexState; -static void luaX_init(lua_State*L); -static void luaX_lexerror(LexState*ls,const char*msg,int token); -#define state_size(x)(sizeof(x)+0) -#define fromstate(l)(cast(lu_byte*,(l))-0) -#define tostate(l)(cast(lua_State*,cast(lu_byte*,l)+0)) -typedef struct LG{ -lua_State l; -global_State g; -}LG; -static void stack_init(lua_State*L1,lua_State*L){ -L1->base_ci=luaM_newvector(L,8,CallInfo); -L1->ci=L1->base_ci; -L1->size_ci=8; -L1->end_ci=L1->base_ci+L1->size_ci-1; -L1->stack=luaM_newvector(L,(2*20)+5,TValue); -L1->stacksize=(2*20)+5; -L1->top=L1->stack; -L1->stack_last=L1->stack+(L1->stacksize-5)-1; -L1->ci->func=L1->top; -setnilvalue(L1->top++); -L1->base=L1->ci->base=L1->top; -L1->ci->top=L1->top+20; -} -static void freestack(lua_State*L,lua_State*L1){ -luaM_freearray(L,L1->base_ci,L1->size_ci,CallInfo); -luaM_freearray(L,L1->stack,L1->stacksize,TValue); -} -static void f_luaopen(lua_State*L,void*ud){ -global_State*g=G(L); -UNUSED(ud); -stack_init(L,L); -sethvalue(L,gt(L),luaH_new(L,0,2)); -sethvalue(L,registry(L),luaH_new(L,0,2)); -luaS_resize(L,32); -luaT_init(L); -luaX_init(L); -luaS_fix(luaS_newliteral(L,"not enough memory")); -g->GCthreshold=4*g->totalbytes; -} -static void preinit_state(lua_State*L,global_State*g){ -G(L)=g; -L->stack=NULL; -L->stacksize=0; -L->errorJmp=NULL; -L->hook=NULL; -L->hookmask=0; -L->basehookcount=0; -L->allowhook=1; -resethookcount(L); -L->openupval=NULL; -L->size_ci=0; -L->nCcalls=L->baseCcalls=0; -L->status=0; -L->base_ci=L->ci=NULL; -L->savedpc=NULL; -L->errfunc=0; -setnilvalue(gt(L)); -} -static void close_state(lua_State*L){ -global_State*g=G(L); -luaF_close(L,L->stack); -luaC_freeall(L); -luaM_freearray(L,G(L)->strt.hash,G(L)->strt.size,TString*); -luaZ_freebuffer(L,&g->buff); -freestack(L,L); -(*g->frealloc)(g->ud,fromstate(L),state_size(LG),0); -} -static void luaE_freethread(lua_State*L,lua_State*L1){ -luaF_close(L1,L1->stack); -freestack(L,L1); -luaM_freemem(L,fromstate(L1),state_size(lua_State)); -} -static lua_State*lua_newstate(lua_Alloc f,void*ud){ -int i; -lua_State*L; -global_State*g; -void*l=(*f)(ud,NULL,0,state_size(LG)); -if(l==NULL)return NULL; -L=tostate(l); -g=&((LG*)L)->g; -L->next=NULL; -L->tt=8; -g->currentwhite=bit2mask(0,5); -L->marked=luaC_white(g); -set2bits(L->marked,5,6); -preinit_state(L,g); -g->frealloc=f; -g->ud=ud; -g->mainthread=L; -g->uvhead.u.l.prev=&g->uvhead; -g->uvhead.u.l.next=&g->uvhead; -g->GCthreshold=0; -g->strt.size=0; -g->strt.nuse=0; -g->strt.hash=NULL; -setnilvalue(registry(L)); -luaZ_initbuffer(L,&g->buff); -g->panic=NULL; -g->gcstate=0; -g->rootgc=obj2gco(L); -g->sweepstrgc=0; -g->sweepgc=&g->rootgc; -g->gray=NULL; -g->grayagain=NULL; -g->weak=NULL; -g->tmudata=NULL; -g->totalbytes=sizeof(LG); -g->gcpause=200; -g->gcstepmul=200; -g->gcdept=0; -for(i=0;i<(8+1);i++)g->mt[i]=NULL; -if(luaD_rawrunprotected(L,f_luaopen,NULL)!=0){ -close_state(L); -L=NULL; -} -else -{} -return L; -} -static void callallgcTM(lua_State*L,void*ud){ -UNUSED(ud); -luaC_callGCTM(L); -} -static void lua_close(lua_State*L){ -L=G(L)->mainthread; -luaF_close(L,L->stack); -luaC_separateudata(L,1); -L->errfunc=0; -do{ -L->ci=L->base_ci; -L->base=L->top=L->ci->base; -L->nCcalls=L->baseCcalls=0; -}while(luaD_rawrunprotected(L,callallgcTM,NULL)!=0); -close_state(L); -} -#define getcode(fs,e)((fs)->f->code[(e)->u.s.info]) -#define luaK_codeAsBx(fs,o,A,sBx)luaK_codeABx(fs,o,A,(sBx)+(((1<<(9+9))-1)>>1)) -#define luaK_setmultret(fs,e)luaK_setreturns(fs,e,(-1)) -static int luaK_codeABx(FuncState*fs,OpCode o,int A,unsigned int Bx); -static int luaK_codeABC(FuncState*fs,OpCode o,int A,int B,int C); -static void luaK_setreturns(FuncState*fs,expdesc*e,int nresults); -static void luaK_patchtohere(FuncState*fs,int list); -static void luaK_concat(FuncState*fs,int*l1,int l2); -static int currentpc(lua_State*L,CallInfo*ci){ -if(!isLua(ci))return-1; -if(ci==L->ci) -ci->savedpc=L->savedpc; -return pcRel(ci->savedpc,ci_func(ci)->l.p); -} -static int currentline(lua_State*L,CallInfo*ci){ -int pc=currentpc(L,ci); -if(pc<0) -return-1; -else -return getline_(ci_func(ci)->l.p,pc); -} -static int lua_getstack(lua_State*L,int level,lua_Debug*ar){ -int status; -CallInfo*ci; -for(ci=L->ci;level>0&&ci>L->base_ci;ci--){ -level--; -if(f_isLua(ci)) -level-=ci->tailcalls; -} -if(level==0&&ci>L->base_ci){ -status=1; -ar->i_ci=cast_int(ci-L->base_ci); -} -else if(level<0){ -status=1; -ar->i_ci=0; -} -else status=0; -return status; -} -static Proto*getluaproto(CallInfo*ci){ -return(isLua(ci)?ci_func(ci)->l.p:NULL); -} -static void funcinfo(lua_Debug*ar,Closure*cl){ -if(cl->c.isC){ -ar->source="=[C]"; -ar->linedefined=-1; -ar->lastlinedefined=-1; -ar->what="C"; -} -else{ -ar->source=getstr(cl->l.p->source); -ar->linedefined=cl->l.p->linedefined; -ar->lastlinedefined=cl->l.p->lastlinedefined; -ar->what=(ar->linedefined==0)?"main":"Lua"; -} -luaO_chunkid(ar->short_src,ar->source,60); -} -static void info_tailcall(lua_Debug*ar){ -ar->name=ar->namewhat=""; -ar->what="tail"; -ar->lastlinedefined=ar->linedefined=ar->currentline=-1; -ar->source="=(tail call)"; -luaO_chunkid(ar->short_src,ar->source,60); -ar->nups=0; -} -static void collectvalidlines(lua_State*L,Closure*f){ -if(f==NULL||f->c.isC){ -setnilvalue(L->top); -} -else{ -Table*t=luaH_new(L,0,0); -int*lineinfo=f->l.p->lineinfo; -int i; -for(i=0;il.p->sizelineinfo;i++) -setbvalue(luaH_setnum(L,t,lineinfo[i]),1); -sethvalue(L,L->top,t); -} -incr_top(L); -} -static int auxgetinfo(lua_State*L,const char*what,lua_Debug*ar, -Closure*f,CallInfo*ci){ -int status=1; -if(f==NULL){ -info_tailcall(ar); -return status; -} -for(;*what;what++){ -switch(*what){ -case'S':{ -funcinfo(ar,f); -break; -} -case'l':{ -ar->currentline=(ci)?currentline(L,ci):-1; -break; -} -case'u':{ -ar->nups=f->c.nupvalues; -break; -} -case'n':{ -ar->namewhat=(ci)?NULL:NULL; -if(ar->namewhat==NULL){ -ar->namewhat=""; -ar->name=NULL; -} -break; -} -case'L': -case'f': -break; -default:status=0; -} -} -return status; -} -static int lua_getinfo(lua_State*L,const char*what,lua_Debug*ar){ -int status; -Closure*f=NULL; -CallInfo*ci=NULL; -if(*what=='>'){ -StkId func=L->top-1; -luai_apicheck(L,ttisfunction(func)); -what++; -f=clvalue(func); -L->top--; -} -else if(ar->i_ci!=0){ -ci=L->base_ci+ar->i_ci; -f=clvalue(ci->func); -} -status=auxgetinfo(L,what,ar,f,ci); -if(strchr(what,'f')){ -if(f==NULL)setnilvalue(L->top); -else setclvalue(L,L->top,f); -incr_top(L); -} -if(strchr(what,'L')) -collectvalidlines(L,f); -return status; -} -static int isinstack(CallInfo*ci,const TValue*o){ -StkId p; -for(p=ci->base;ptop;p++) -if(o==p)return 1; -return 0; -} -static void luaG_typeerror(lua_State*L,const TValue*o,const char*op){ -const char*name=NULL; -const char*t=luaT_typenames[ttype(o)]; -const char*kind=(isinstack(L->ci,o))? -NULL: -NULL; -if(kind) -luaG_runerror(L,"attempt to %s %s "LUA_QL("%s")" (a %s value)", -op,kind,name,t); -else -luaG_runerror(L,"attempt to %s a %s value",op,t); -} -static void luaG_concaterror(lua_State*L,StkId p1,StkId p2){ -if(ttisstring(p1)||ttisnumber(p1))p1=p2; -luaG_typeerror(L,p1,"concatenate"); -} -static void luaG_aritherror(lua_State*L,const TValue*p1,const TValue*p2){ -TValue temp; -if(luaV_tonumber(p1,&temp)==NULL) -p2=p1; -luaG_typeerror(L,p2,"perform arithmetic on"); -} -static int luaG_ordererror(lua_State*L,const TValue*p1,const TValue*p2){ -const char*t1=luaT_typenames[ttype(p1)]; -const char*t2=luaT_typenames[ttype(p2)]; -if(t1[2]==t2[2]) -luaG_runerror(L,"attempt to compare two %s values",t1); -else -luaG_runerror(L,"attempt to compare %s with %s",t1,t2); -return 0; -} -static void addinfo(lua_State*L,const char*msg){ -CallInfo*ci=L->ci; -if(isLua(ci)){ -char buff[60]; -int line=currentline(L,ci); -luaO_chunkid(buff,getstr(getluaproto(ci)->source),60); -luaO_pushfstring(L,"%s:%d: %s",buff,line,msg); -} -} -static void luaG_errormsg(lua_State*L){ -if(L->errfunc!=0){ -StkId errfunc=restorestack(L,L->errfunc); -if(!ttisfunction(errfunc))luaD_throw(L,5); -setobj(L,L->top,L->top-1); -setobj(L,L->top-1,errfunc); -incr_top(L); -luaD_call(L,L->top-2,1); -} -luaD_throw(L,2); -} -static void luaG_runerror(lua_State*L,const char*fmt,...){ -va_list argp; -va_start(argp,fmt); -addinfo(L,luaO_pushvfstring(L,fmt,argp)); -va_end(argp); -luaG_errormsg(L); -} -static int luaZ_fill(ZIO*z){ -size_t size; -lua_State*L=z->L; -const char*buff; -buff=z->reader(L,z->data,&size); -if(buff==NULL||size==0)return(-1); -z->n=size-1; -z->p=buff; -return char2int(*(z->p++)); -} -static void luaZ_init(lua_State*L,ZIO*z,lua_Reader reader,void*data){ -z->L=L; -z->reader=reader; -z->data=data; -z->n=0; -z->p=NULL; -} -static char*luaZ_openspace(lua_State*L,Mbuffer*buff,size_t n){ -if(n>buff->buffsize){ -if(n<32)n=32; -luaZ_resizebuffer(L,buff,n); -} -return buff->buffer; -} -#define opmode(t,a,b,c,m)(((t)<<7)|((a)<<6)|((b)<<4)|((c)<<2)|(m)) -static const lu_byte luaP_opmodes[(cast(int,OP_VARARG)+1)]={ -opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgK,OpArgN,iABx) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgU,OpArgN,iABC) -,opmode(0,1,OpArgK,OpArgN,iABx) -,opmode(0,1,OpArgR,OpArgK,iABC) -,opmode(0,0,OpArgK,OpArgN,iABx) -,opmode(0,0,OpArgU,OpArgN,iABC) -,opmode(0,0,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,1,OpArgR,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgR,iABC) -,opmode(0,0,OpArgR,OpArgN,iAsBx) -,opmode(1,0,OpArgK,OpArgK,iABC) -,opmode(1,0,OpArgK,OpArgK,iABC) -,opmode(1,0,OpArgK,OpArgK,iABC) -,opmode(1,1,OpArgR,OpArgU,iABC) -,opmode(1,1,OpArgR,OpArgU,iABC) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,0,OpArgU,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgN,iAsBx) -,opmode(0,1,OpArgR,OpArgN,iAsBx) -,opmode(1,0,OpArgN,OpArgU,iABC) -,opmode(0,0,OpArgU,OpArgU,iABC) -,opmode(0,0,OpArgN,OpArgN,iABC) -,opmode(0,1,OpArgU,OpArgN,iABx) -,opmode(0,1,OpArgU,OpArgN,iABC) -}; -#define next(ls)(ls->current=zgetc(ls->z)) -#define currIsNewline(ls)(ls->current=='\n'||ls->current=='\r') -static const char*const luaX_tokens[]={ -"and","break","do","else","elseif", -"end","false","for","function","if", -"in","local","nil","not","or","repeat", -"return","then","true","until","while", -"..","...","==",">=","<=","~=", -"","","","", -NULL -}; -#define save_and_next(ls)(save(ls,ls->current),next(ls)) -static void save(LexState*ls,int c){ -Mbuffer*b=ls->buff; -if(b->n+1>b->buffsize){ -size_t newsize; -if(b->buffsize>=((size_t)(~(size_t)0)-2)/2) -luaX_lexerror(ls,"lexical element too long",0); -newsize=b->buffsize*2; -luaZ_resizebuffer(ls->L,b,newsize); -} -b->buffer[b->n++]=cast(char,c); -} -static void luaX_init(lua_State*L){ -int i; -for(i=0;i<(cast(int,TK_WHILE-257+1));i++){ -TString*ts=luaS_new(L,luaX_tokens[i]); -luaS_fix(ts); -ts->tsv.reserved=cast_byte(i+1); -} -} -static const char*luaX_token2str(LexState*ls,int token){ -if(token<257){ -return(iscntrl(token))?luaO_pushfstring(ls->L,"char(%d)",token): -luaO_pushfstring(ls->L,"%c",token); -} -else -return luaX_tokens[token-257]; -} -static const char*txtToken(LexState*ls,int token){ -switch(token){ -case TK_NAME: -case TK_STRING: -case TK_NUMBER: -save(ls,'\0'); -return luaZ_buffer(ls->buff); -default: -return luaX_token2str(ls,token); -} -} -static void luaX_lexerror(LexState*ls,const char*msg,int token){ -char buff[80]; -luaO_chunkid(buff,getstr(ls->source),80); -msg=luaO_pushfstring(ls->L,"%s:%d: %s",buff,ls->linenumber,msg); -if(token) -luaO_pushfstring(ls->L,"%s near "LUA_QL("%s"),msg,txtToken(ls,token)); -luaD_throw(ls->L,3); -} -static void luaX_syntaxerror(LexState*ls,const char*msg){ -luaX_lexerror(ls,msg,ls->t.token); -} -static TString*luaX_newstring(LexState*ls,const char*str,size_t l){ -lua_State*L=ls->L; -TString*ts=luaS_newlstr(L,str,l); -TValue*o=luaH_setstr(L,ls->fs->h,ts); -if(ttisnil(o)){ -setbvalue(o,1); -luaC_checkGC(L); -} -return ts; -} -static void inclinenumber(LexState*ls){ -int old=ls->current; -next(ls); -if(currIsNewline(ls)&&ls->current!=old) -next(ls); -if(++ls->linenumber>=(INT_MAX-2)) -luaX_syntaxerror(ls,"chunk has too many lines"); -} -static void luaX_setinput(lua_State*L,LexState*ls,ZIO*z,TString*source){ -ls->decpoint='.'; -ls->L=L; -ls->lookahead.token=TK_EOS; -ls->z=z; -ls->fs=NULL; -ls->linenumber=1; -ls->lastline=1; -ls->source=source; -luaZ_resizebuffer(ls->L,ls->buff,32); -next(ls); -} -static int check_next(LexState*ls,const char*set){ -if(!strchr(set,ls->current)) -return 0; -save_and_next(ls); -return 1; -} -static void buffreplace(LexState*ls,char from,char to){ -size_t n=luaZ_bufflen(ls->buff); -char*p=luaZ_buffer(ls->buff); -while(n--) -if(p[n]==from)p[n]=to; -} -static void read_numeral(LexState*ls,SemInfo*seminfo){ -do{ -save_and_next(ls); -}while(isdigit(ls->current)||ls->current=='.'); -if(check_next(ls,"Ee")) -check_next(ls,"+-"); -while(isalnum(ls->current)||ls->current=='_') -save_and_next(ls); -save(ls,'\0'); -buffreplace(ls,'.',ls->decpoint); -if(!luaO_str2d(luaZ_buffer(ls->buff),&seminfo->r)) -luaX_lexerror(ls,"malformed number",TK_NUMBER); -} -static int skip_sep(LexState*ls){ -int count=0; -int s=ls->current; -save_and_next(ls); -while(ls->current=='='){ -save_and_next(ls); -count++; -} -return(ls->current==s)?count:(-count)-1; -} -static void read_long_string(LexState*ls,SemInfo*seminfo,int sep){ -int cont=0; -(void)(cont); -save_and_next(ls); -if(currIsNewline(ls)) -inclinenumber(ls); -for(;;){ -switch(ls->current){ -case(-1): -luaX_lexerror(ls,(seminfo)?"unfinished long string": -"unfinished long comment",TK_EOS); -break; -case']':{ -if(skip_sep(ls)==sep){ -save_and_next(ls); -goto endloop; -} -break; -} -case'\n': -case'\r':{ -save(ls,'\n'); -inclinenumber(ls); -if(!seminfo)luaZ_resetbuffer(ls->buff); -break; -} -default:{ -if(seminfo)save_and_next(ls); -else next(ls); -} -} -}endloop: -if(seminfo) -seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+(2+sep), -luaZ_bufflen(ls->buff)-2*(2+sep)); -} -static void read_string(LexState*ls,int del,SemInfo*seminfo){ -save_and_next(ls); -while(ls->current!=del){ -switch(ls->current){ -case(-1): -luaX_lexerror(ls,"unfinished string",TK_EOS); -continue; -case'\n': -case'\r': -luaX_lexerror(ls,"unfinished string",TK_STRING); -continue; -case'\\':{ -int c; -next(ls); -switch(ls->current){ -case'a':c='\a';break; -case'b':c='\b';break; -case'f':c='\f';break; -case'n':c='\n';break; -case'r':c='\r';break; -case't':c='\t';break; -case'v':c='\v';break; -case'\n': -case'\r':save(ls,'\n');inclinenumber(ls);continue; -case(-1):continue; -default:{ -if(!isdigit(ls->current)) -save_and_next(ls); -else{ -int i=0; -c=0; -do{ -c=10*c+(ls->current-'0'); -next(ls); -}while(++i<3&&isdigit(ls->current)); -if(c>UCHAR_MAX) -luaX_lexerror(ls,"escape sequence too large",TK_STRING); -save(ls,c); -} -continue; -} -} -save(ls,c); -next(ls); -continue; -} -default: -save_and_next(ls); -} -} -save_and_next(ls); -seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+1, -luaZ_bufflen(ls->buff)-2); -} -static int llex(LexState*ls,SemInfo*seminfo){ -luaZ_resetbuffer(ls->buff); -for(;;){ -switch(ls->current){ -case'\n': -case'\r':{ -inclinenumber(ls); -continue; -} -case'-':{ -next(ls); -if(ls->current!='-')return'-'; -next(ls); -if(ls->current=='['){ -int sep=skip_sep(ls); -luaZ_resetbuffer(ls->buff); -if(sep>=0){ -read_long_string(ls,NULL,sep); -luaZ_resetbuffer(ls->buff); -continue; -} -} -while(!currIsNewline(ls)&&ls->current!=(-1)) -next(ls); -continue; -} -case'[':{ -int sep=skip_sep(ls); -if(sep>=0){ -read_long_string(ls,seminfo,sep); -return TK_STRING; -} -else if(sep==-1)return'['; -else luaX_lexerror(ls,"invalid long string delimiter",TK_STRING); -} -case'=':{ -next(ls); -if(ls->current!='=')return'='; -else{next(ls);return TK_EQ;} -} -case'<':{ -next(ls); -if(ls->current!='=')return'<'; -else{next(ls);return TK_LE;} -} -case'>':{ -next(ls); -if(ls->current!='=')return'>'; -else{next(ls);return TK_GE;} -} -case'~':{ -next(ls); -if(ls->current!='=')return'~'; -else{next(ls);return TK_NE;} -} -case'"': -case'\'':{ -read_string(ls,ls->current,seminfo); -return TK_STRING; -} -case'.':{ -save_and_next(ls); -if(check_next(ls,".")){ -if(check_next(ls,".")) -return TK_DOTS; -else return TK_CONCAT; -} -else if(!isdigit(ls->current))return'.'; -else{ -read_numeral(ls,seminfo); -return TK_NUMBER; -} -} -case(-1):{ -return TK_EOS; -} -default:{ -if(isspace(ls->current)){ -next(ls); -continue; -} -else if(isdigit(ls->current)){ -read_numeral(ls,seminfo); -return TK_NUMBER; -} -else if(isalpha(ls->current)||ls->current=='_'){ -TString*ts; -do{ -save_and_next(ls); -}while(isalnum(ls->current)||ls->current=='_'); -ts=luaX_newstring(ls,luaZ_buffer(ls->buff), -luaZ_bufflen(ls->buff)); -if(ts->tsv.reserved>0) -return ts->tsv.reserved-1+257; -else{ -seminfo->ts=ts; -return TK_NAME; -} -} -else{ -int c=ls->current; -next(ls); -return c; -} -} -} -} -} -static void luaX_next(LexState*ls){ -ls->lastline=ls->linenumber; -if(ls->lookahead.token!=TK_EOS){ -ls->t=ls->lookahead; -ls->lookahead.token=TK_EOS; -} -else -ls->t.token=llex(ls,&ls->t.seminfo); -} -static void luaX_lookahead(LexState*ls){ -ls->lookahead.token=llex(ls,&ls->lookahead.seminfo); -} -#define hasjumps(e)((e)->t!=(e)->f) -static int isnumeral(expdesc*e){ -return(e->k==VKNUM&&e->t==(-1)&&e->f==(-1)); -} -static void luaK_nil(FuncState*fs,int from,int n){ -Instruction*previous; -if(fs->pc>fs->lasttarget){ -if(fs->pc==0){ -if(from>=fs->nactvar) -return; -} -else{ -previous=&fs->f->code[fs->pc-1]; -if(GET_OPCODE(*previous)==OP_LOADNIL){ -int pfrom=GETARG_A(*previous); -int pto=GETARG_B(*previous); -if(pfrom<=from&&from<=pto+1){ -if(from+n-1>pto) -SETARG_B(*previous,from+n-1); -return; -} -} -} -} -luaK_codeABC(fs,OP_LOADNIL,from,from+n-1,0); -} -static int luaK_jump(FuncState*fs){ -int jpc=fs->jpc; -int j; -fs->jpc=(-1); -j=luaK_codeAsBx(fs,OP_JMP,0,(-1)); -luaK_concat(fs,&j,jpc); -return j; -} -static void luaK_ret(FuncState*fs,int first,int nret){ -luaK_codeABC(fs,OP_RETURN,first,nret+1,0); -} -static int condjump(FuncState*fs,OpCode op,int A,int B,int C){ -luaK_codeABC(fs,op,A,B,C); -return luaK_jump(fs); -} -static void fixjump(FuncState*fs,int pc,int dest){ -Instruction*jmp=&fs->f->code[pc]; -int offset=dest-(pc+1); -if(abs(offset)>(((1<<(9+9))-1)>>1)) -luaX_syntaxerror(fs->ls,"control structure too long"); -SETARG_sBx(*jmp,offset); -} -static int luaK_getlabel(FuncState*fs){ -fs->lasttarget=fs->pc; -return fs->pc; -} -static int getjump(FuncState*fs,int pc){ -int offset=GETARG_sBx(fs->f->code[pc]); -if(offset==(-1)) -return(-1); -else -return(pc+1)+offset; -} -static Instruction*getjumpcontrol(FuncState*fs,int pc){ -Instruction*pi=&fs->f->code[pc]; -if(pc>=1&&testTMode(GET_OPCODE(*(pi-1)))) -return pi-1; -else -return pi; -} -static int need_value(FuncState*fs,int list){ -for(;list!=(-1);list=getjump(fs,list)){ -Instruction i=*getjumpcontrol(fs,list); -if(GET_OPCODE(i)!=OP_TESTSET)return 1; -} -return 0; -} -static int patchtestreg(FuncState*fs,int node,int reg){ -Instruction*i=getjumpcontrol(fs,node); -if(GET_OPCODE(*i)!=OP_TESTSET) -return 0; -if(reg!=((1<<8)-1)&®!=GETARG_B(*i)) -SETARG_A(*i,reg); -else -*i=CREATE_ABC(OP_TEST,GETARG_B(*i),0,GETARG_C(*i)); -return 1; -} -static void removevalues(FuncState*fs,int list){ -for(;list!=(-1);list=getjump(fs,list)) -patchtestreg(fs,list,((1<<8)-1)); -} -static void patchlistaux(FuncState*fs,int list,int vtarget,int reg, -int dtarget){ -while(list!=(-1)){ -int next=getjump(fs,list); -if(patchtestreg(fs,list,reg)) -fixjump(fs,list,vtarget); -else -fixjump(fs,list,dtarget); -list=next; -} -} -static void dischargejpc(FuncState*fs){ -patchlistaux(fs,fs->jpc,fs->pc,((1<<8)-1),fs->pc); -fs->jpc=(-1); -} -static void luaK_patchlist(FuncState*fs,int list,int target){ -if(target==fs->pc) -luaK_patchtohere(fs,list); -else{ -patchlistaux(fs,list,target,((1<<8)-1),target); -} -} -static void luaK_patchtohere(FuncState*fs,int list){ -luaK_getlabel(fs); -luaK_concat(fs,&fs->jpc,list); -} -static void luaK_concat(FuncState*fs,int*l1,int l2){ -if(l2==(-1))return; -else if(*l1==(-1)) -*l1=l2; -else{ -int list=*l1; -int next; -while((next=getjump(fs,list))!=(-1)) -list=next; -fixjump(fs,list,l2); -} -} -static void luaK_checkstack(FuncState*fs,int n){ -int newstack=fs->freereg+n; -if(newstack>fs->f->maxstacksize){ -if(newstack>=250) -luaX_syntaxerror(fs->ls,"function or expression too complex"); -fs->f->maxstacksize=cast_byte(newstack); -} -} -static void luaK_reserveregs(FuncState*fs,int n){ -luaK_checkstack(fs,n); -fs->freereg+=n; -} -static void freereg(FuncState*fs,int reg){ -if(!ISK(reg)&®>=fs->nactvar){ -fs->freereg--; -} -} -static void freeexp(FuncState*fs,expdesc*e){ -if(e->k==VNONRELOC) -freereg(fs,e->u.s.info); -} -static int addk(FuncState*fs,TValue*k,TValue*v){ -lua_State*L=fs->L; -TValue*idx=luaH_set(L,fs->h,k); -Proto*f=fs->f; -int oldsize=f->sizek; -if(ttisnumber(idx)){ -return cast_int(nvalue(idx)); -} -else{ -setnvalue(idx,cast_num(fs->nk)); -luaM_growvector(L,f->k,fs->nk,f->sizek,TValue, -((1<<(9+9))-1),"constant table overflow"); -while(oldsizesizek)setnilvalue(&f->k[oldsize++]); -setobj(L,&f->k[fs->nk],v); -luaC_barrier(L,f,v); -return fs->nk++; -} -} -static int luaK_stringK(FuncState*fs,TString*s){ -TValue o; -setsvalue(fs->L,&o,s); -return addk(fs,&o,&o); -} -static int luaK_numberK(FuncState*fs,lua_Number r){ -TValue o; -setnvalue(&o,r); -return addk(fs,&o,&o); -} -static int boolK(FuncState*fs,int b){ -TValue o; -setbvalue(&o,b); -return addk(fs,&o,&o); -} -static int nilK(FuncState*fs){ -TValue k,v; -setnilvalue(&v); -sethvalue(fs->L,&k,fs->h); -return addk(fs,&k,&v); -} -static void luaK_setreturns(FuncState*fs,expdesc*e,int nresults){ -if(e->k==VCALL){ -SETARG_C(getcode(fs,e),nresults+1); -} -else if(e->k==VVARARG){ -SETARG_B(getcode(fs,e),nresults+1); -SETARG_A(getcode(fs,e),fs->freereg); -luaK_reserveregs(fs,1); -} -} -static void luaK_setoneret(FuncState*fs,expdesc*e){ -if(e->k==VCALL){ -e->k=VNONRELOC; -e->u.s.info=GETARG_A(getcode(fs,e)); -} -else if(e->k==VVARARG){ -SETARG_B(getcode(fs,e),2); -e->k=VRELOCABLE; -} -} -static void luaK_dischargevars(FuncState*fs,expdesc*e){ -switch(e->k){ -case VLOCAL:{ -e->k=VNONRELOC; -break; -} -case VUPVAL:{ -e->u.s.info=luaK_codeABC(fs,OP_GETUPVAL,0,e->u.s.info,0); -e->k=VRELOCABLE; -break; -} -case VGLOBAL:{ -e->u.s.info=luaK_codeABx(fs,OP_GETGLOBAL,0,e->u.s.info); -e->k=VRELOCABLE; -break; -} -case VINDEXED:{ -freereg(fs,e->u.s.aux); -freereg(fs,e->u.s.info); -e->u.s.info=luaK_codeABC(fs,OP_GETTABLE,0,e->u.s.info,e->u.s.aux); -e->k=VRELOCABLE; -break; -} -case VVARARG: -case VCALL:{ -luaK_setoneret(fs,e); -break; -} -default:break; -} -} -static int code_label(FuncState*fs,int A,int b,int jump){ -luaK_getlabel(fs); -return luaK_codeABC(fs,OP_LOADBOOL,A,b,jump); -} -static void discharge2reg(FuncState*fs,expdesc*e,int reg){ -luaK_dischargevars(fs,e); -switch(e->k){ -case VNIL:{ -luaK_nil(fs,reg,1); -break; -} -case VFALSE:case VTRUE:{ -luaK_codeABC(fs,OP_LOADBOOL,reg,e->k==VTRUE,0); -break; -} -case VK:{ -luaK_codeABx(fs,OP_LOADK,reg,e->u.s.info); -break; -} -case VKNUM:{ -luaK_codeABx(fs,OP_LOADK,reg,luaK_numberK(fs,e->u.nval)); -break; -} -case VRELOCABLE:{ -Instruction*pc=&getcode(fs,e); -SETARG_A(*pc,reg); -break; -} -case VNONRELOC:{ -if(reg!=e->u.s.info) -luaK_codeABC(fs,OP_MOVE,reg,e->u.s.info,0); -break; -} -default:{ -return; -} -} -e->u.s.info=reg; -e->k=VNONRELOC; -} -static void discharge2anyreg(FuncState*fs,expdesc*e){ -if(e->k!=VNONRELOC){ -luaK_reserveregs(fs,1); -discharge2reg(fs,e,fs->freereg-1); -} -} -static void exp2reg(FuncState*fs,expdesc*e,int reg){ -discharge2reg(fs,e,reg); -if(e->k==VJMP) -luaK_concat(fs,&e->t,e->u.s.info); -if(hasjumps(e)){ -int final; -int p_f=(-1); -int p_t=(-1); -if(need_value(fs,e->t)||need_value(fs,e->f)){ -int fj=(e->k==VJMP)?(-1):luaK_jump(fs); -p_f=code_label(fs,reg,0,1); -p_t=code_label(fs,reg,1,0); -luaK_patchtohere(fs,fj); -} -final=luaK_getlabel(fs); -patchlistaux(fs,e->f,final,reg,p_f); -patchlistaux(fs,e->t,final,reg,p_t); -} -e->f=e->t=(-1); -e->u.s.info=reg; -e->k=VNONRELOC; -} -static void luaK_exp2nextreg(FuncState*fs,expdesc*e){ -luaK_dischargevars(fs,e); -freeexp(fs,e); -luaK_reserveregs(fs,1); -exp2reg(fs,e,fs->freereg-1); -} -static int luaK_exp2anyreg(FuncState*fs,expdesc*e){ -luaK_dischargevars(fs,e); -if(e->k==VNONRELOC){ -if(!hasjumps(e))return e->u.s.info; -if(e->u.s.info>=fs->nactvar){ -exp2reg(fs,e,e->u.s.info); -return e->u.s.info; -} -} -luaK_exp2nextreg(fs,e); -return e->u.s.info; -} -static void luaK_exp2val(FuncState*fs,expdesc*e){ -if(hasjumps(e)) -luaK_exp2anyreg(fs,e); -else -luaK_dischargevars(fs,e); -} -static int luaK_exp2RK(FuncState*fs,expdesc*e){ -luaK_exp2val(fs,e); -switch(e->k){ -case VKNUM: -case VTRUE: -case VFALSE: -case VNIL:{ -if(fs->nk<=((1<<(9-1))-1)){ -e->u.s.info=(e->k==VNIL)?nilK(fs): -(e->k==VKNUM)?luaK_numberK(fs,e->u.nval): -boolK(fs,(e->k==VTRUE)); -e->k=VK; -return RKASK(e->u.s.info); -} -else break; -} -case VK:{ -if(e->u.s.info<=((1<<(9-1))-1)) -return RKASK(e->u.s.info); -else break; -} -default:break; -} -return luaK_exp2anyreg(fs,e); -} -static void luaK_storevar(FuncState*fs,expdesc*var,expdesc*ex){ -switch(var->k){ -case VLOCAL:{ -freeexp(fs,ex); -exp2reg(fs,ex,var->u.s.info); -return; -} -case VUPVAL:{ -int e=luaK_exp2anyreg(fs,ex); -luaK_codeABC(fs,OP_SETUPVAL,e,var->u.s.info,0); -break; -} -case VGLOBAL:{ -int e=luaK_exp2anyreg(fs,ex); -luaK_codeABx(fs,OP_SETGLOBAL,e,var->u.s.info); -break; -} -case VINDEXED:{ -int e=luaK_exp2RK(fs,ex); -luaK_codeABC(fs,OP_SETTABLE,var->u.s.info,var->u.s.aux,e); -break; -} -default:{ -break; -} -} -freeexp(fs,ex); -} -static void luaK_self(FuncState*fs,expdesc*e,expdesc*key){ -int func; -luaK_exp2anyreg(fs,e); -freeexp(fs,e); -func=fs->freereg; -luaK_reserveregs(fs,2); -luaK_codeABC(fs,OP_SELF,func,e->u.s.info,luaK_exp2RK(fs,key)); -freeexp(fs,key); -e->u.s.info=func; -e->k=VNONRELOC; -} -static void invertjump(FuncState*fs,expdesc*e){ -Instruction*pc=getjumpcontrol(fs,e->u.s.info); -SETARG_A(*pc,!(GETARG_A(*pc))); -} -static int jumponcond(FuncState*fs,expdesc*e,int cond){ -if(e->k==VRELOCABLE){ -Instruction ie=getcode(fs,e); -if(GET_OPCODE(ie)==OP_NOT){ -fs->pc--; -return condjump(fs,OP_TEST,GETARG_B(ie),0,!cond); -} -} -discharge2anyreg(fs,e); -freeexp(fs,e); -return condjump(fs,OP_TESTSET,((1<<8)-1),e->u.s.info,cond); -} -static void luaK_goiftrue(FuncState*fs,expdesc*e){ -int pc; -luaK_dischargevars(fs,e); -switch(e->k){ -case VK:case VKNUM:case VTRUE:{ -pc=(-1); -break; -} -case VJMP:{ -invertjump(fs,e); -pc=e->u.s.info; -break; -} -default:{ -pc=jumponcond(fs,e,0); -break; -} -} -luaK_concat(fs,&e->f,pc); -luaK_patchtohere(fs,e->t); -e->t=(-1); -} -static void luaK_goiffalse(FuncState*fs,expdesc*e){ -int pc; -luaK_dischargevars(fs,e); -switch(e->k){ -case VNIL:case VFALSE:{ -pc=(-1); -break; -} -case VJMP:{ -pc=e->u.s.info; -break; -} -default:{ -pc=jumponcond(fs,e,1); -break; -} -} -luaK_concat(fs,&e->t,pc); -luaK_patchtohere(fs,e->f); -e->f=(-1); -} -static void codenot(FuncState*fs,expdesc*e){ -luaK_dischargevars(fs,e); -switch(e->k){ -case VNIL:case VFALSE:{ -e->k=VTRUE; -break; -} -case VK:case VKNUM:case VTRUE:{ -e->k=VFALSE; -break; -} -case VJMP:{ -invertjump(fs,e); -break; -} -case VRELOCABLE: -case VNONRELOC:{ -discharge2anyreg(fs,e); -freeexp(fs,e); -e->u.s.info=luaK_codeABC(fs,OP_NOT,0,e->u.s.info,0); -e->k=VRELOCABLE; -break; -} -default:{ -break; -} -} -{int temp=e->f;e->f=e->t;e->t=temp;} -removevalues(fs,e->f); -removevalues(fs,e->t); -} -static void luaK_indexed(FuncState*fs,expdesc*t,expdesc*k){ -t->u.s.aux=luaK_exp2RK(fs,k); -t->k=VINDEXED; -} -static int constfolding(OpCode op,expdesc*e1,expdesc*e2){ -lua_Number v1,v2,r; -if(!isnumeral(e1)||!isnumeral(e2))return 0; -v1=e1->u.nval; -v2=e2->u.nval; -switch(op){ -case OP_ADD:r=luai_numadd(v1,v2);break; -case OP_SUB:r=luai_numsub(v1,v2);break; -case OP_MUL:r=luai_nummul(v1,v2);break; -case OP_DIV: -if(v2==0)return 0; -r=luai_numdiv(v1,v2);break; -case OP_MOD: -if(v2==0)return 0; -r=luai_nummod(v1,v2);break; -case OP_POW:r=luai_numpow(v1,v2);break; -case OP_UNM:r=luai_numunm(v1);break; -case OP_LEN:return 0; -default:r=0;break; -} -if(luai_numisnan(r))return 0; -e1->u.nval=r; -return 1; -} -static void codearith(FuncState*fs,OpCode op,expdesc*e1,expdesc*e2){ -if(constfolding(op,e1,e2)) -return; -else{ -int o2=(op!=OP_UNM&&op!=OP_LEN)?luaK_exp2RK(fs,e2):0; -int o1=luaK_exp2RK(fs,e1); -if(o1>o2){ -freeexp(fs,e1); -freeexp(fs,e2); -} -else{ -freeexp(fs,e2); -freeexp(fs,e1); -} -e1->u.s.info=luaK_codeABC(fs,op,0,o1,o2); -e1->k=VRELOCABLE; -} -} -static void codecomp(FuncState*fs,OpCode op,int cond,expdesc*e1, -expdesc*e2){ -int o1=luaK_exp2RK(fs,e1); -int o2=luaK_exp2RK(fs,e2); -freeexp(fs,e2); -freeexp(fs,e1); -if(cond==0&&op!=OP_EQ){ -int temp; -temp=o1;o1=o2;o2=temp; -cond=1; -} -e1->u.s.info=condjump(fs,op,cond,o1,o2); -e1->k=VJMP; -} -static void luaK_prefix(FuncState*fs,UnOpr op,expdesc*e){ -expdesc e2; -e2.t=e2.f=(-1);e2.k=VKNUM;e2.u.nval=0; -switch(op){ -case OPR_MINUS:{ -if(!isnumeral(e)) -luaK_exp2anyreg(fs,e); -codearith(fs,OP_UNM,e,&e2); -break; -} -case OPR_NOT:codenot(fs,e);break; -case OPR_LEN:{ -luaK_exp2anyreg(fs,e); -codearith(fs,OP_LEN,e,&e2); -break; -} -default:; -} -} -static void luaK_infix(FuncState*fs,BinOpr op,expdesc*v){ -switch(op){ -case OPR_AND:{ -luaK_goiftrue(fs,v); -break; -} -case OPR_OR:{ -luaK_goiffalse(fs,v); -break; -} -case OPR_CONCAT:{ -luaK_exp2nextreg(fs,v); -break; -} -case OPR_ADD:case OPR_SUB:case OPR_MUL:case OPR_DIV: -case OPR_MOD:case OPR_POW:{ -if(!isnumeral(v))luaK_exp2RK(fs,v); -break; -} -default:{ -luaK_exp2RK(fs,v); -break; -} -} -} -static void luaK_posfix(FuncState*fs,BinOpr op,expdesc*e1,expdesc*e2){ -switch(op){ -case OPR_AND:{ -luaK_dischargevars(fs,e2); -luaK_concat(fs,&e2->f,e1->f); -*e1=*e2; -break; -} -case OPR_OR:{ -luaK_dischargevars(fs,e2); -luaK_concat(fs,&e2->t,e1->t); -*e1=*e2; -break; -} -case OPR_CONCAT:{ -luaK_exp2val(fs,e2); -if(e2->k==VRELOCABLE&&GET_OPCODE(getcode(fs,e2))==OP_CONCAT){ -freeexp(fs,e1); -SETARG_B(getcode(fs,e2),e1->u.s.info); -e1->k=VRELOCABLE;e1->u.s.info=e2->u.s.info; -} -else{ -luaK_exp2nextreg(fs,e2); -codearith(fs,OP_CONCAT,e1,e2); -} -break; -} -case OPR_ADD:codearith(fs,OP_ADD,e1,e2);break; -case OPR_SUB:codearith(fs,OP_SUB,e1,e2);break; -case OPR_MUL:codearith(fs,OP_MUL,e1,e2);break; -case OPR_DIV:codearith(fs,OP_DIV,e1,e2);break; -case OPR_MOD:codearith(fs,OP_MOD,e1,e2);break; -case OPR_POW:codearith(fs,OP_POW,e1,e2);break; -case OPR_EQ:codecomp(fs,OP_EQ,1,e1,e2);break; -case OPR_NE:codecomp(fs,OP_EQ,0,e1,e2);break; -case OPR_LT:codecomp(fs,OP_LT,1,e1,e2);break; -case OPR_LE:codecomp(fs,OP_LE,1,e1,e2);break; -case OPR_GT:codecomp(fs,OP_LT,0,e1,e2);break; -case OPR_GE:codecomp(fs,OP_LE,0,e1,e2);break; -default:; -} -} -static void luaK_fixline(FuncState*fs,int line){ -fs->f->lineinfo[fs->pc-1]=line; -} -static int luaK_code(FuncState*fs,Instruction i,int line){ -Proto*f=fs->f; -dischargejpc(fs); -luaM_growvector(fs->L,f->code,fs->pc,f->sizecode,Instruction, -(INT_MAX-2),"code size overflow"); -f->code[fs->pc]=i; -luaM_growvector(fs->L,f->lineinfo,fs->pc,f->sizelineinfo,int, -(INT_MAX-2),"code size overflow"); -f->lineinfo[fs->pc]=line; -return fs->pc++; -} -static int luaK_codeABC(FuncState*fs,OpCode o,int a,int b,int c){ -return luaK_code(fs,CREATE_ABC(o,a,b,c),fs->ls->lastline); -} -static int luaK_codeABx(FuncState*fs,OpCode o,int a,unsigned int bc){ -return luaK_code(fs,CREATE_ABx(o,a,bc),fs->ls->lastline); -} -static void luaK_setlist(FuncState*fs,int base,int nelems,int tostore){ -int c=(nelems-1)/50+1; -int b=(tostore==(-1))?0:tostore; -if(c<=((1<<9)-1)) -luaK_codeABC(fs,OP_SETLIST,base,b,c); -else{ -luaK_codeABC(fs,OP_SETLIST,base,b,0); -luaK_code(fs,cast(Instruction,c),fs->ls->lastline); -} -fs->freereg=base+1; -} -#define hasmultret(k)((k)==VCALL||(k)==VVARARG) -#define getlocvar(fs,i)((fs)->f->locvars[(fs)->actvar[i]]) -#define luaY_checklimit(fs,v,l,m)if((v)>(l))errorlimit(fs,l,m) -typedef struct BlockCnt{ -struct BlockCnt*previous; -int breaklist; -lu_byte nactvar; -lu_byte upval; -lu_byte isbreakable; -}BlockCnt; -static void chunk(LexState*ls); -static void expr(LexState*ls,expdesc*v); -static void anchor_token(LexState*ls){ -if(ls->t.token==TK_NAME||ls->t.token==TK_STRING){ -TString*ts=ls->t.seminfo.ts; -luaX_newstring(ls,getstr(ts),ts->tsv.len); -} -} -static void error_expected(LexState*ls,int token){ -luaX_syntaxerror(ls, -luaO_pushfstring(ls->L,LUA_QL("%s")" expected",luaX_token2str(ls,token))); -} -static void errorlimit(FuncState*fs,int limit,const char*what){ -const char*msg=(fs->f->linedefined==0)? -luaO_pushfstring(fs->L,"main function has more than %d %s",limit,what): -luaO_pushfstring(fs->L,"function at line %d has more than %d %s", -fs->f->linedefined,limit,what); -luaX_lexerror(fs->ls,msg,0); -} -static int testnext(LexState*ls,int c){ -if(ls->t.token==c){ -luaX_next(ls); -return 1; -} -else return 0; -} -static void check(LexState*ls,int c){ -if(ls->t.token!=c) -error_expected(ls,c); -} -static void checknext(LexState*ls,int c){ -check(ls,c); -luaX_next(ls); -} -#define check_condition(ls,c,msg){if(!(c))luaX_syntaxerror(ls,msg);} -static void check_match(LexState*ls,int what,int who,int where){ -if(!testnext(ls,what)){ -if(where==ls->linenumber) -error_expected(ls,what); -else{ -luaX_syntaxerror(ls,luaO_pushfstring(ls->L, -LUA_QL("%s")" expected (to close "LUA_QL("%s")" at line %d)", -luaX_token2str(ls,what),luaX_token2str(ls,who),where)); -} -} -} -static TString*str_checkname(LexState*ls){ -TString*ts; -check(ls,TK_NAME); -ts=ls->t.seminfo.ts; -luaX_next(ls); -return ts; -} -static void init_exp(expdesc*e,expkind k,int i){ -e->f=e->t=(-1); -e->k=k; -e->u.s.info=i; -} -static void codestring(LexState*ls,expdesc*e,TString*s){ -init_exp(e,VK,luaK_stringK(ls->fs,s)); -} -static void checkname(LexState*ls,expdesc*e){ -codestring(ls,e,str_checkname(ls)); -} -static int registerlocalvar(LexState*ls,TString*varname){ -FuncState*fs=ls->fs; -Proto*f=fs->f; -int oldsize=f->sizelocvars; -luaM_growvector(ls->L,f->locvars,fs->nlocvars,f->sizelocvars, -LocVar,SHRT_MAX,"too many local variables"); -while(oldsizesizelocvars)f->locvars[oldsize++].varname=NULL; -f->locvars[fs->nlocvars].varname=varname; -luaC_objbarrier(ls->L,f,varname); -return fs->nlocvars++; -} -#define new_localvarliteral(ls,v,n)new_localvar(ls,luaX_newstring(ls,""v,(sizeof(v)/sizeof(char))-1),n) -static void new_localvar(LexState*ls,TString*name,int n){ -FuncState*fs=ls->fs; -luaY_checklimit(fs,fs->nactvar+n+1,200,"local variables"); -fs->actvar[fs->nactvar+n]=cast(unsigned short,registerlocalvar(ls,name)); -} -static void adjustlocalvars(LexState*ls,int nvars){ -FuncState*fs=ls->fs; -fs->nactvar=cast_byte(fs->nactvar+nvars); -for(;nvars;nvars--){ -getlocvar(fs,fs->nactvar-nvars).startpc=fs->pc; -} -} -static void removevars(LexState*ls,int tolevel){ -FuncState*fs=ls->fs; -while(fs->nactvar>tolevel) -getlocvar(fs,--fs->nactvar).endpc=fs->pc; -} -static int indexupvalue(FuncState*fs,TString*name,expdesc*v){ -int i; -Proto*f=fs->f; -int oldsize=f->sizeupvalues; -for(i=0;inups;i++){ -if(fs->upvalues[i].k==v->k&&fs->upvalues[i].info==v->u.s.info){ -return i; -} -} -luaY_checklimit(fs,f->nups+1,60,"upvalues"); -luaM_growvector(fs->L,f->upvalues,f->nups,f->sizeupvalues, -TString*,(INT_MAX-2),""); -while(oldsizesizeupvalues)f->upvalues[oldsize++]=NULL; -f->upvalues[f->nups]=name; -luaC_objbarrier(fs->L,f,name); -fs->upvalues[f->nups].k=cast_byte(v->k); -fs->upvalues[f->nups].info=cast_byte(v->u.s.info); -return f->nups++; -} -static int searchvar(FuncState*fs,TString*n){ -int i; -for(i=fs->nactvar-1;i>=0;i--){ -if(n==getlocvar(fs,i).varname) -return i; -} -return-1; -} -static void markupval(FuncState*fs,int level){ -BlockCnt*bl=fs->bl; -while(bl&&bl->nactvar>level)bl=bl->previous; -if(bl)bl->upval=1; -} -static int singlevaraux(FuncState*fs,TString*n,expdesc*var,int base){ -if(fs==NULL){ -init_exp(var,VGLOBAL,((1<<8)-1)); -return VGLOBAL; -} -else{ -int v=searchvar(fs,n); -if(v>=0){ -init_exp(var,VLOCAL,v); -if(!base) -markupval(fs,v); -return VLOCAL; -} -else{ -if(singlevaraux(fs->prev,n,var,0)==VGLOBAL) -return VGLOBAL; -var->u.s.info=indexupvalue(fs,n,var); -var->k=VUPVAL; -return VUPVAL; -} -} -} -static void singlevar(LexState*ls,expdesc*var){ -TString*varname=str_checkname(ls); -FuncState*fs=ls->fs; -if(singlevaraux(fs,varname,var,1)==VGLOBAL) -var->u.s.info=luaK_stringK(fs,varname); -} -static void adjust_assign(LexState*ls,int nvars,int nexps,expdesc*e){ -FuncState*fs=ls->fs; -int extra=nvars-nexps; -if(hasmultret(e->k)){ -extra++; -if(extra<0)extra=0; -luaK_setreturns(fs,e,extra); -if(extra>1)luaK_reserveregs(fs,extra-1); -} -else{ -if(e->k!=VVOID)luaK_exp2nextreg(fs,e); -if(extra>0){ -int reg=fs->freereg; -luaK_reserveregs(fs,extra); -luaK_nil(fs,reg,extra); -} -} -} -static void enterlevel(LexState*ls){ -if(++ls->L->nCcalls>200) -luaX_lexerror(ls,"chunk has too many syntax levels",0); -} -#define leavelevel(ls)((ls)->L->nCcalls--) -static void enterblock(FuncState*fs,BlockCnt*bl,lu_byte isbreakable){ -bl->breaklist=(-1); -bl->isbreakable=isbreakable; -bl->nactvar=fs->nactvar; -bl->upval=0; -bl->previous=fs->bl; -fs->bl=bl; -} -static void leaveblock(FuncState*fs){ -BlockCnt*bl=fs->bl; -fs->bl=bl->previous; -removevars(fs->ls,bl->nactvar); -if(bl->upval) -luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0); -fs->freereg=fs->nactvar; -luaK_patchtohere(fs,bl->breaklist); -} -static void pushclosure(LexState*ls,FuncState*func,expdesc*v){ -FuncState*fs=ls->fs; -Proto*f=fs->f; -int oldsize=f->sizep; -int i; -luaM_growvector(ls->L,f->p,fs->np,f->sizep,Proto*, -((1<<(9+9))-1),"constant table overflow"); -while(oldsizesizep)f->p[oldsize++]=NULL; -f->p[fs->np++]=func->f; -luaC_objbarrier(ls->L,f,func->f); -init_exp(v,VRELOCABLE,luaK_codeABx(fs,OP_CLOSURE,0,fs->np-1)); -for(i=0;if->nups;i++){ -OpCode o=(func->upvalues[i].k==VLOCAL)?OP_MOVE:OP_GETUPVAL; -luaK_codeABC(fs,o,0,func->upvalues[i].info,0); -} -} -static void open_func(LexState*ls,FuncState*fs){ -lua_State*L=ls->L; -Proto*f=luaF_newproto(L); -fs->f=f; -fs->prev=ls->fs; -fs->ls=ls; -fs->L=L; -ls->fs=fs; -fs->pc=0; -fs->lasttarget=-1; -fs->jpc=(-1); -fs->freereg=0; -fs->nk=0; -fs->np=0; -fs->nlocvars=0; -fs->nactvar=0; -fs->bl=NULL; -f->source=ls->source; -f->maxstacksize=2; -fs->h=luaH_new(L,0,0); -sethvalue(L,L->top,fs->h); -incr_top(L); -setptvalue(L,L->top,f); -incr_top(L); -} -static void close_func(LexState*ls){ -lua_State*L=ls->L; -FuncState*fs=ls->fs; -Proto*f=fs->f; -removevars(ls,0); -luaK_ret(fs,0,0); -luaM_reallocvector(L,f->code,f->sizecode,fs->pc,Instruction); -f->sizecode=fs->pc; -luaM_reallocvector(L,f->lineinfo,f->sizelineinfo,fs->pc,int); -f->sizelineinfo=fs->pc; -luaM_reallocvector(L,f->k,f->sizek,fs->nk,TValue); -f->sizek=fs->nk; -luaM_reallocvector(L,f->p,f->sizep,fs->np,Proto*); -f->sizep=fs->np; -luaM_reallocvector(L,f->locvars,f->sizelocvars,fs->nlocvars,LocVar); -f->sizelocvars=fs->nlocvars; -luaM_reallocvector(L,f->upvalues,f->sizeupvalues,f->nups,TString*); -f->sizeupvalues=f->nups; -ls->fs=fs->prev; -if(fs)anchor_token(ls); -L->top-=2; -} -static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff,const char*name){ -struct LexState lexstate; -struct FuncState funcstate; -lexstate.buff=buff; -luaX_setinput(L,&lexstate,z,luaS_new(L,name)); -open_func(&lexstate,&funcstate); -funcstate.f->is_vararg=2; -luaX_next(&lexstate); -chunk(&lexstate); -check(&lexstate,TK_EOS); -close_func(&lexstate); -return funcstate.f; -} -static void field(LexState*ls,expdesc*v){ -FuncState*fs=ls->fs; -expdesc key; -luaK_exp2anyreg(fs,v); -luaX_next(ls); -checkname(ls,&key); -luaK_indexed(fs,v,&key); -} -static void yindex(LexState*ls,expdesc*v){ -luaX_next(ls); -expr(ls,v); -luaK_exp2val(ls->fs,v); -checknext(ls,']'); -} -struct ConsControl{ -expdesc v; -expdesc*t; -int nh; -int na; -int tostore; -}; -static void recfield(LexState*ls,struct ConsControl*cc){ -FuncState*fs=ls->fs; -int reg=ls->fs->freereg; -expdesc key,val; -int rkkey; -if(ls->t.token==TK_NAME){ -luaY_checklimit(fs,cc->nh,(INT_MAX-2),"items in a constructor"); -checkname(ls,&key); -} -else -yindex(ls,&key); -cc->nh++; -checknext(ls,'='); -rkkey=luaK_exp2RK(fs,&key); -expr(ls,&val); -luaK_codeABC(fs,OP_SETTABLE,cc->t->u.s.info,rkkey,luaK_exp2RK(fs,&val)); -fs->freereg=reg; -} -static void closelistfield(FuncState*fs,struct ConsControl*cc){ -if(cc->v.k==VVOID)return; -luaK_exp2nextreg(fs,&cc->v); -cc->v.k=VVOID; -if(cc->tostore==50){ -luaK_setlist(fs,cc->t->u.s.info,cc->na,cc->tostore); -cc->tostore=0; -} -} -static void lastlistfield(FuncState*fs,struct ConsControl*cc){ -if(cc->tostore==0)return; -if(hasmultret(cc->v.k)){ -luaK_setmultret(fs,&cc->v); -luaK_setlist(fs,cc->t->u.s.info,cc->na,(-1)); -cc->na--; -} -else{ -if(cc->v.k!=VVOID) -luaK_exp2nextreg(fs,&cc->v); -luaK_setlist(fs,cc->t->u.s.info,cc->na,cc->tostore); -} -} -static void listfield(LexState*ls,struct ConsControl*cc){ -expr(ls,&cc->v); -luaY_checklimit(ls->fs,cc->na,(INT_MAX-2),"items in a constructor"); -cc->na++; -cc->tostore++; -} -static void constructor(LexState*ls,expdesc*t){ -FuncState*fs=ls->fs; -int line=ls->linenumber; -int pc=luaK_codeABC(fs,OP_NEWTABLE,0,0,0); -struct ConsControl cc; -cc.na=cc.nh=cc.tostore=0; -cc.t=t; -init_exp(t,VRELOCABLE,pc); -init_exp(&cc.v,VVOID,0); -luaK_exp2nextreg(ls->fs,t); -checknext(ls,'{'); -do{ -if(ls->t.token=='}')break; -closelistfield(fs,&cc); -switch(ls->t.token){ -case TK_NAME:{ -luaX_lookahead(ls); -if(ls->lookahead.token!='=') -listfield(ls,&cc); -else -recfield(ls,&cc); -break; -} -case'[':{ -recfield(ls,&cc); -break; -} -default:{ -listfield(ls,&cc); -break; -} -} -}while(testnext(ls,',')||testnext(ls,';')); -check_match(ls,'}','{',line); -lastlistfield(fs,&cc); -SETARG_B(fs->f->code[pc],luaO_int2fb(cc.na)); -SETARG_C(fs->f->code[pc],luaO_int2fb(cc.nh)); -} -static void parlist(LexState*ls){ -FuncState*fs=ls->fs; -Proto*f=fs->f; -int nparams=0; -f->is_vararg=0; -if(ls->t.token!=')'){ -do{ -switch(ls->t.token){ -case TK_NAME:{ -new_localvar(ls,str_checkname(ls),nparams++); -break; -} -case TK_DOTS:{ -luaX_next(ls); -f->is_vararg|=2; -break; -} -default:luaX_syntaxerror(ls," or "LUA_QL("...")" expected"); -} -}while(!f->is_vararg&&testnext(ls,',')); -} -adjustlocalvars(ls,nparams); -f->numparams=cast_byte(fs->nactvar-(f->is_vararg&1)); -luaK_reserveregs(fs,fs->nactvar); -} -static void body(LexState*ls,expdesc*e,int needself,int line){ -FuncState new_fs; -open_func(ls,&new_fs); -new_fs.f->linedefined=line; -checknext(ls,'('); -if(needself){ -new_localvarliteral(ls,"self",0); -adjustlocalvars(ls,1); -} -parlist(ls); -checknext(ls,')'); -chunk(ls); -new_fs.f->lastlinedefined=ls->linenumber; -check_match(ls,TK_END,TK_FUNCTION,line); -close_func(ls); -pushclosure(ls,&new_fs,e); -} -static int explist1(LexState*ls,expdesc*v){ -int n=1; -expr(ls,v); -while(testnext(ls,',')){ -luaK_exp2nextreg(ls->fs,v); -expr(ls,v); -n++; -} -return n; -} -static void funcargs(LexState*ls,expdesc*f){ -FuncState*fs=ls->fs; -expdesc args; -int base,nparams; -int line=ls->linenumber; -switch(ls->t.token){ -case'(':{ -if(line!=ls->lastline) -luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); -luaX_next(ls); -if(ls->t.token==')') -args.k=VVOID; -else{ -explist1(ls,&args); -luaK_setmultret(fs,&args); -} -check_match(ls,')','(',line); -break; -} -case'{':{ -constructor(ls,&args); -break; -} -case TK_STRING:{ -codestring(ls,&args,ls->t.seminfo.ts); -luaX_next(ls); -break; -} -default:{ -luaX_syntaxerror(ls,"function arguments expected"); -return; -} -} -base=f->u.s.info; -if(hasmultret(args.k)) -nparams=(-1); -else{ -if(args.k!=VVOID) -luaK_exp2nextreg(fs,&args); -nparams=fs->freereg-(base+1); -} -init_exp(f,VCALL,luaK_codeABC(fs,OP_CALL,base,nparams+1,2)); -luaK_fixline(fs,line); -fs->freereg=base+1; -} -static void prefixexp(LexState*ls,expdesc*v){ -switch(ls->t.token){ -case'(':{ -int line=ls->linenumber; -luaX_next(ls); -expr(ls,v); -check_match(ls,')','(',line); -luaK_dischargevars(ls->fs,v); -return; -} -case TK_NAME:{ -singlevar(ls,v); -return; -} -default:{ -luaX_syntaxerror(ls,"unexpected symbol"); -return; -} -} -} -static void primaryexp(LexState*ls,expdesc*v){ -FuncState*fs=ls->fs; -prefixexp(ls,v); -for(;;){ -switch(ls->t.token){ -case'.':{ -field(ls,v); -break; -} -case'[':{ -expdesc key; -luaK_exp2anyreg(fs,v); -yindex(ls,&key); -luaK_indexed(fs,v,&key); -break; -} -case':':{ -expdesc key; -luaX_next(ls); -checkname(ls,&key); -luaK_self(fs,v,&key); -funcargs(ls,v); -break; -} -case'(':case TK_STRING:case'{':{ -luaK_exp2nextreg(fs,v); -funcargs(ls,v); -break; -} -default:return; -} -} -} -static void simpleexp(LexState*ls,expdesc*v){ -switch(ls->t.token){ -case TK_NUMBER:{ -init_exp(v,VKNUM,0); -v->u.nval=ls->t.seminfo.r; -break; -} -case TK_STRING:{ -codestring(ls,v,ls->t.seminfo.ts); -break; -} -case TK_NIL:{ -init_exp(v,VNIL,0); -break; -} -case TK_TRUE:{ -init_exp(v,VTRUE,0); -break; -} -case TK_FALSE:{ -init_exp(v,VFALSE,0); -break; -} -case TK_DOTS:{ -FuncState*fs=ls->fs; -check_condition(ls,fs->f->is_vararg, -"cannot use "LUA_QL("...")" outside a vararg function"); -fs->f->is_vararg&=~4; -init_exp(v,VVARARG,luaK_codeABC(fs,OP_VARARG,0,1,0)); -break; -} -case'{':{ -constructor(ls,v); -return; -} -case TK_FUNCTION:{ -luaX_next(ls); -body(ls,v,0,ls->linenumber); -return; -} -default:{ -primaryexp(ls,v); -return; -} -} -luaX_next(ls); -} -static UnOpr getunopr(int op){ -switch(op){ -case TK_NOT:return OPR_NOT; -case'-':return OPR_MINUS; -case'#':return OPR_LEN; -default:return OPR_NOUNOPR; -} -} -static BinOpr getbinopr(int op){ -switch(op){ -case'+':return OPR_ADD; -case'-':return OPR_SUB; -case'*':return OPR_MUL; -case'/':return OPR_DIV; -case'%':return OPR_MOD; -case'^':return OPR_POW; -case TK_CONCAT:return OPR_CONCAT; -case TK_NE:return OPR_NE; -case TK_EQ:return OPR_EQ; -case'<':return OPR_LT; -case TK_LE:return OPR_LE; -case'>':return OPR_GT; -case TK_GE:return OPR_GE; -case TK_AND:return OPR_AND; -case TK_OR:return OPR_OR; -default:return OPR_NOBINOPR; -} -} -static const struct{ -lu_byte left; -lu_byte right; -}priority[]={ -{6,6},{6,6},{7,7},{7,7},{7,7}, -{10,9},{5,4}, -{3,3},{3,3}, -{3,3},{3,3},{3,3},{3,3}, -{2,2},{1,1} -}; -static BinOpr subexpr(LexState*ls,expdesc*v,unsigned int limit){ -BinOpr op; -UnOpr uop; -enterlevel(ls); -uop=getunopr(ls->t.token); -if(uop!=OPR_NOUNOPR){ -luaX_next(ls); -subexpr(ls,v,8); -luaK_prefix(ls->fs,uop,v); -} -else simpleexp(ls,v); -op=getbinopr(ls->t.token); -while(op!=OPR_NOBINOPR&&priority[op].left>limit){ -expdesc v2; -BinOpr nextop; -luaX_next(ls); -luaK_infix(ls->fs,op,v); -nextop=subexpr(ls,&v2,priority[op].right); -luaK_posfix(ls->fs,op,v,&v2); -op=nextop; -} -leavelevel(ls); -return op; -} -static void expr(LexState*ls,expdesc*v){ -subexpr(ls,v,0); -} -static int block_follow(int token){ -switch(token){ -case TK_ELSE:case TK_ELSEIF:case TK_END: -case TK_UNTIL:case TK_EOS: -return 1; -default:return 0; -} -} -static void block(LexState*ls){ -FuncState*fs=ls->fs; -BlockCnt bl; -enterblock(fs,&bl,0); -chunk(ls); -leaveblock(fs); -} -struct LHS_assign{ -struct LHS_assign*prev; -expdesc v; -}; -static void check_conflict(LexState*ls,struct LHS_assign*lh,expdesc*v){ -FuncState*fs=ls->fs; -int extra=fs->freereg; -int conflict=0; -for(;lh;lh=lh->prev){ -if(lh->v.k==VINDEXED){ -if(lh->v.u.s.info==v->u.s.info){ -conflict=1; -lh->v.u.s.info=extra; -} -if(lh->v.u.s.aux==v->u.s.info){ -conflict=1; -lh->v.u.s.aux=extra; -} -} -} -if(conflict){ -luaK_codeABC(fs,OP_MOVE,fs->freereg,v->u.s.info,0); -luaK_reserveregs(fs,1); -} -} -static void assignment(LexState*ls,struct LHS_assign*lh,int nvars){ -expdesc e; -check_condition(ls,VLOCAL<=lh->v.k&&lh->v.k<=VINDEXED, -"syntax error"); -if(testnext(ls,',')){ -struct LHS_assign nv; -nv.prev=lh; -primaryexp(ls,&nv.v); -if(nv.v.k==VLOCAL) -check_conflict(ls,lh,&nv.v); -luaY_checklimit(ls->fs,nvars,200-ls->L->nCcalls, -"variables in assignment"); -assignment(ls,&nv,nvars+1); -} -else{ -int nexps; -checknext(ls,'='); -nexps=explist1(ls,&e); -if(nexps!=nvars){ -adjust_assign(ls,nvars,nexps,&e); -if(nexps>nvars) -ls->fs->freereg-=nexps-nvars; -} -else{ -luaK_setoneret(ls->fs,&e); -luaK_storevar(ls->fs,&lh->v,&e); -return; -} -} -init_exp(&e,VNONRELOC,ls->fs->freereg-1); -luaK_storevar(ls->fs,&lh->v,&e); -} -static int cond(LexState*ls){ -expdesc v; -expr(ls,&v); -if(v.k==VNIL)v.k=VFALSE; -luaK_goiftrue(ls->fs,&v); -return v.f; -} -static void breakstat(LexState*ls){ -FuncState*fs=ls->fs; -BlockCnt*bl=fs->bl; -int upval=0; -while(bl&&!bl->isbreakable){ -upval|=bl->upval; -bl=bl->previous; -} -if(!bl) -luaX_syntaxerror(ls,"no loop to break"); -if(upval) -luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0); -luaK_concat(fs,&bl->breaklist,luaK_jump(fs)); -} -static void whilestat(LexState*ls,int line){ -FuncState*fs=ls->fs; -int whileinit; -int condexit; -BlockCnt bl; -luaX_next(ls); -whileinit=luaK_getlabel(fs); -condexit=cond(ls); -enterblock(fs,&bl,1); -checknext(ls,TK_DO); -block(ls); -luaK_patchlist(fs,luaK_jump(fs),whileinit); -check_match(ls,TK_END,TK_WHILE,line); -leaveblock(fs); -luaK_patchtohere(fs,condexit); -} -static void repeatstat(LexState*ls,int line){ -int condexit; -FuncState*fs=ls->fs; -int repeat_init=luaK_getlabel(fs); -BlockCnt bl1,bl2; -enterblock(fs,&bl1,1); -enterblock(fs,&bl2,0); -luaX_next(ls); -chunk(ls); -check_match(ls,TK_UNTIL,TK_REPEAT,line); -condexit=cond(ls); -if(!bl2.upval){ -leaveblock(fs); -luaK_patchlist(ls->fs,condexit,repeat_init); -} -else{ -breakstat(ls); -luaK_patchtohere(ls->fs,condexit); -leaveblock(fs); -luaK_patchlist(ls->fs,luaK_jump(fs),repeat_init); -} -leaveblock(fs); -} -static int exp1(LexState*ls){ -expdesc e; -int k; -expr(ls,&e); -k=e.k; -luaK_exp2nextreg(ls->fs,&e); -return k; -} -static void forbody(LexState*ls,int base,int line,int nvars,int isnum){ -BlockCnt bl; -FuncState*fs=ls->fs; -int prep,endfor; -adjustlocalvars(ls,3); -checknext(ls,TK_DO); -prep=isnum?luaK_codeAsBx(fs,OP_FORPREP,base,(-1)):luaK_jump(fs); -enterblock(fs,&bl,0); -adjustlocalvars(ls,nvars); -luaK_reserveregs(fs,nvars); -block(ls); -leaveblock(fs); -luaK_patchtohere(fs,prep); -endfor=(isnum)?luaK_codeAsBx(fs,OP_FORLOOP,base,(-1)): -luaK_codeABC(fs,OP_TFORLOOP,base,0,nvars); -luaK_fixline(fs,line); -luaK_patchlist(fs,(isnum?endfor:luaK_jump(fs)),prep+1); -} -static void fornum(LexState*ls,TString*varname,int line){ -FuncState*fs=ls->fs; -int base=fs->freereg; -new_localvarliteral(ls,"(for index)",0); -new_localvarliteral(ls,"(for limit)",1); -new_localvarliteral(ls,"(for step)",2); -new_localvar(ls,varname,3); -checknext(ls,'='); -exp1(ls); -checknext(ls,','); -exp1(ls); -if(testnext(ls,',')) -exp1(ls); -else{ -luaK_codeABx(fs,OP_LOADK,fs->freereg,luaK_numberK(fs,1)); -luaK_reserveregs(fs,1); -} -forbody(ls,base,line,1,1); -} -static void forlist(LexState*ls,TString*indexname){ -FuncState*fs=ls->fs; -expdesc e; -int nvars=0; -int line; -int base=fs->freereg; -new_localvarliteral(ls,"(for generator)",nvars++); -new_localvarliteral(ls,"(for state)",nvars++); -new_localvarliteral(ls,"(for control)",nvars++); -new_localvar(ls,indexname,nvars++); -while(testnext(ls,',')) -new_localvar(ls,str_checkname(ls),nvars++); -checknext(ls,TK_IN); -line=ls->linenumber; -adjust_assign(ls,3,explist1(ls,&e),&e); -luaK_checkstack(fs,3); -forbody(ls,base,line,nvars-3,0); -} -static void forstat(LexState*ls,int line){ -FuncState*fs=ls->fs; -TString*varname; -BlockCnt bl; -enterblock(fs,&bl,1); -luaX_next(ls); -varname=str_checkname(ls); -switch(ls->t.token){ -case'=':fornum(ls,varname,line);break; -case',':case TK_IN:forlist(ls,varname);break; -default:luaX_syntaxerror(ls,LUA_QL("=")" or "LUA_QL("in")" expected"); -} -check_match(ls,TK_END,TK_FOR,line); -leaveblock(fs); -} -static int test_then_block(LexState*ls){ -int condexit; -luaX_next(ls); -condexit=cond(ls); -checknext(ls,TK_THEN); -block(ls); -return condexit; -} -static void ifstat(LexState*ls,int line){ -FuncState*fs=ls->fs; -int flist; -int escapelist=(-1); -flist=test_then_block(ls); -while(ls->t.token==TK_ELSEIF){ -luaK_concat(fs,&escapelist,luaK_jump(fs)); -luaK_patchtohere(fs,flist); -flist=test_then_block(ls); -} -if(ls->t.token==TK_ELSE){ -luaK_concat(fs,&escapelist,luaK_jump(fs)); -luaK_patchtohere(fs,flist); -luaX_next(ls); -block(ls); -} -else -luaK_concat(fs,&escapelist,flist); -luaK_patchtohere(fs,escapelist); -check_match(ls,TK_END,TK_IF,line); -} -static void localfunc(LexState*ls){ -expdesc v,b; -FuncState*fs=ls->fs; -new_localvar(ls,str_checkname(ls),0); -init_exp(&v,VLOCAL,fs->freereg); -luaK_reserveregs(fs,1); -adjustlocalvars(ls,1); -body(ls,&b,0,ls->linenumber); -luaK_storevar(fs,&v,&b); -getlocvar(fs,fs->nactvar-1).startpc=fs->pc; -} -static void localstat(LexState*ls){ -int nvars=0; -int nexps; -expdesc e; -do{ -new_localvar(ls,str_checkname(ls),nvars++); -}while(testnext(ls,',')); -if(testnext(ls,'=')) -nexps=explist1(ls,&e); -else{ -e.k=VVOID; -nexps=0; -} -adjust_assign(ls,nvars,nexps,&e); -adjustlocalvars(ls,nvars); -} -static int funcname(LexState*ls,expdesc*v){ -int needself=0; -singlevar(ls,v); -while(ls->t.token=='.') -field(ls,v); -if(ls->t.token==':'){ -needself=1; -field(ls,v); -} -return needself; -} -static void funcstat(LexState*ls,int line){ -int needself; -expdesc v,b; -luaX_next(ls); -needself=funcname(ls,&v); -body(ls,&b,needself,line); -luaK_storevar(ls->fs,&v,&b); -luaK_fixline(ls->fs,line); -} -static void exprstat(LexState*ls){ -FuncState*fs=ls->fs; -struct LHS_assign v; -primaryexp(ls,&v.v); -if(v.v.k==VCALL) -SETARG_C(getcode(fs,&v.v),1); -else{ -v.prev=NULL; -assignment(ls,&v,1); -} -} -static void retstat(LexState*ls){ -FuncState*fs=ls->fs; -expdesc e; -int first,nret; -luaX_next(ls); -if(block_follow(ls->t.token)||ls->t.token==';') -first=nret=0; -else{ -nret=explist1(ls,&e); -if(hasmultret(e.k)){ -luaK_setmultret(fs,&e); -if(e.k==VCALL&&nret==1){ -SET_OPCODE(getcode(fs,&e),OP_TAILCALL); -} -first=fs->nactvar; -nret=(-1); -} -else{ -if(nret==1) -first=luaK_exp2anyreg(fs,&e); -else{ -luaK_exp2nextreg(fs,&e); -first=fs->nactvar; -} -} -} -luaK_ret(fs,first,nret); -} -static int statement(LexState*ls){ -int line=ls->linenumber; -switch(ls->t.token){ -case TK_IF:{ -ifstat(ls,line); -return 0; -} -case TK_WHILE:{ -whilestat(ls,line); -return 0; -} -case TK_DO:{ -luaX_next(ls); -block(ls); -check_match(ls,TK_END,TK_DO,line); -return 0; -} -case TK_FOR:{ -forstat(ls,line); -return 0; -} -case TK_REPEAT:{ -repeatstat(ls,line); -return 0; -} -case TK_FUNCTION:{ -funcstat(ls,line); -return 0; -} -case TK_LOCAL:{ -luaX_next(ls); -if(testnext(ls,TK_FUNCTION)) -localfunc(ls); -else -localstat(ls); -return 0; -} -case TK_RETURN:{ -retstat(ls); -return 1; -} -case TK_BREAK:{ -luaX_next(ls); -breakstat(ls); -return 1; -} -default:{ -exprstat(ls); -return 0; -} -} -} -static void chunk(LexState*ls){ -int islast=0; -enterlevel(ls); -while(!islast&&!block_follow(ls->t.token)){ -islast=statement(ls); -testnext(ls,';'); -ls->fs->freereg=ls->fs->nactvar; -} -leavelevel(ls); -} -static const TValue*luaV_tonumber(const TValue*obj,TValue*n){ -lua_Number num; -if(ttisnumber(obj))return obj; -if(ttisstring(obj)&&luaO_str2d(svalue(obj),&num)){ -setnvalue(n,num); -return n; -} -else -return NULL; -} -static int luaV_tostring(lua_State*L,StkId obj){ -if(!ttisnumber(obj)) -return 0; -else{ -char s[32]; -lua_Number n=nvalue(obj); -lua_number2str(s,n); -setsvalue(L,obj,luaS_new(L,s)); -return 1; -} -} -static void callTMres(lua_State*L,StkId res,const TValue*f, -const TValue*p1,const TValue*p2){ -ptrdiff_t result=savestack(L,res); -setobj(L,L->top,f); -setobj(L,L->top+1,p1); -setobj(L,L->top+2,p2); -luaD_checkstack(L,3); -L->top+=3; -luaD_call(L,L->top-3,1); -res=restorestack(L,result); -L->top--; -setobj(L,res,L->top); -} -static void callTM(lua_State*L,const TValue*f,const TValue*p1, -const TValue*p2,const TValue*p3){ -setobj(L,L->top,f); -setobj(L,L->top+1,p1); -setobj(L,L->top+2,p2); -setobj(L,L->top+3,p3); -luaD_checkstack(L,4); -L->top+=4; -luaD_call(L,L->top-4,0); -} -static void luaV_gettable(lua_State*L,const TValue*t,TValue*key,StkId val){ -int loop; -for(loop=0;loop<100;loop++){ -const TValue*tm; -if(ttistable(t)){ -Table*h=hvalue(t); -const TValue*res=luaH_get(h,key); -if(!ttisnil(res)|| -(tm=fasttm(L,h->metatable,TM_INDEX))==NULL){ -setobj(L,val,res); -return; -} -} -else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_INDEX))) -luaG_typeerror(L,t,"index"); -if(ttisfunction(tm)){ -callTMres(L,val,tm,t,key); -return; -} -t=tm; -} -luaG_runerror(L,"loop in gettable"); -} -static void luaV_settable(lua_State*L,const TValue*t,TValue*key,StkId val){ -int loop; -TValue temp; -for(loop=0;loop<100;loop++){ -const TValue*tm; -if(ttistable(t)){ -Table*h=hvalue(t); -TValue*oldval=luaH_set(L,h,key); -if(!ttisnil(oldval)|| -(tm=fasttm(L,h->metatable,TM_NEWINDEX))==NULL){ -setobj(L,oldval,val); -h->flags=0; -luaC_barriert(L,h,val); -return; -} -} -else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_NEWINDEX))) -luaG_typeerror(L,t,"index"); -if(ttisfunction(tm)){ -callTM(L,tm,t,key,val); -return; -} -setobj(L,&temp,tm); -t=&temp; -} -luaG_runerror(L,"loop in settable"); -} -static int call_binTM(lua_State*L,const TValue*p1,const TValue*p2, -StkId res,TMS event){ -const TValue*tm=luaT_gettmbyobj(L,p1,event); -if(ttisnil(tm)) -tm=luaT_gettmbyobj(L,p2,event); -if(ttisnil(tm))return 0; -callTMres(L,res,tm,p1,p2); -return 1; -} -static const TValue*get_compTM(lua_State*L,Table*mt1,Table*mt2, -TMS event){ -const TValue*tm1=fasttm(L,mt1,event); -const TValue*tm2; -if(tm1==NULL)return NULL; -if(mt1==mt2)return tm1; -tm2=fasttm(L,mt2,event); -if(tm2==NULL)return NULL; -if(luaO_rawequalObj(tm1,tm2)) -return tm1; -return NULL; -} -static int call_orderTM(lua_State*L,const TValue*p1,const TValue*p2, -TMS event){ -const TValue*tm1=luaT_gettmbyobj(L,p1,event); -const TValue*tm2; -if(ttisnil(tm1))return-1; -tm2=luaT_gettmbyobj(L,p2,event); -if(!luaO_rawequalObj(tm1,tm2)) -return-1; -callTMres(L,L->top,tm1,p1,p2); -return!l_isfalse(L->top); -} -static int l_strcmp(const TString*ls,const TString*rs){ -const char*l=getstr(ls); -size_t ll=ls->tsv.len; -const char*r=getstr(rs); -size_t lr=rs->tsv.len; -for(;;){ -int temp=strcoll(l,r); -if(temp!=0)return temp; -else{ -size_t len=strlen(l); -if(len==lr) -return(len==ll)?0:1; -else if(len==ll) -return-1; -len++; -l+=len;ll-=len;r+=len;lr-=len; -} -} -} -static int luaV_lessthan(lua_State*L,const TValue*l,const TValue*r){ -int res; -if(ttype(l)!=ttype(r)) -return luaG_ordererror(L,l,r); -else if(ttisnumber(l)) -return luai_numlt(nvalue(l),nvalue(r)); -else if(ttisstring(l)) -return l_strcmp(rawtsvalue(l),rawtsvalue(r))<0; -else if((res=call_orderTM(L,l,r,TM_LT))!=-1) -return res; -return luaG_ordererror(L,l,r); -} -static int lessequal(lua_State*L,const TValue*l,const TValue*r){ -int res; -if(ttype(l)!=ttype(r)) -return luaG_ordererror(L,l,r); -else if(ttisnumber(l)) -return luai_numle(nvalue(l),nvalue(r)); -else if(ttisstring(l)) -return l_strcmp(rawtsvalue(l),rawtsvalue(r))<=0; -else if((res=call_orderTM(L,l,r,TM_LE))!=-1) -return res; -else if((res=call_orderTM(L,r,l,TM_LT))!=-1) -return!res; -return luaG_ordererror(L,l,r); -} -static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2){ -const TValue*tm; -switch(ttype(t1)){ -case 0:return 1; -case 3:return luai_numeq(nvalue(t1),nvalue(t2)); -case 1:return bvalue(t1)==bvalue(t2); -case 2:return pvalue(t1)==pvalue(t2); -case 7:{ -if(uvalue(t1)==uvalue(t2))return 1; -tm=get_compTM(L,uvalue(t1)->metatable,uvalue(t2)->metatable, -TM_EQ); -break; -} -case 5:{ -if(hvalue(t1)==hvalue(t2))return 1; -tm=get_compTM(L,hvalue(t1)->metatable,hvalue(t2)->metatable,TM_EQ); -break; -} -default:return gcvalue(t1)==gcvalue(t2); -} -if(tm==NULL)return 0; -callTMres(L,L->top,tm,t1,t2); -return!l_isfalse(L->top); -} -static void luaV_concat(lua_State*L,int total,int last){ -do{ -StkId top=L->base+last+1; -int n=2; -if(!(ttisstring(top-2)||ttisnumber(top-2))||!tostring(L,top-1)){ -if(!call_binTM(L,top-2,top-1,top-2,TM_CONCAT)) -luaG_concaterror(L,top-2,top-1); -}else if(tsvalue(top-1)->len==0) -(void)tostring(L,top-2); -else{ -size_t tl=tsvalue(top-1)->len; -char*buffer; -int i; -for(n=1;nlen; -if(l>=((size_t)(~(size_t)0)-2)-tl)luaG_runerror(L,"string length overflow"); -tl+=l; -} -buffer=luaZ_openspace(L,&G(L)->buff,tl); -tl=0; -for(i=n;i>0;i--){ -size_t l=tsvalue(top-i)->len; -memcpy(buffer+tl,svalue(top-i),l); -tl+=l; -} -setsvalue(L,top-n,luaS_newlstr(L,buffer,tl)); -} -total-=n-1; -last-=n-1; -}while(total>1); -} -static void Arith(lua_State*L,StkId ra,const TValue*rb, -const TValue*rc,TMS op){ -TValue tempb,tempc; -const TValue*b,*c; -if((b=luaV_tonumber(rb,&tempb))!=NULL&& -(c=luaV_tonumber(rc,&tempc))!=NULL){ -lua_Number nb=nvalue(b),nc=nvalue(c); -switch(op){ -case TM_ADD:setnvalue(ra,luai_numadd(nb,nc));break; -case TM_SUB:setnvalue(ra,luai_numsub(nb,nc));break; -case TM_MUL:setnvalue(ra,luai_nummul(nb,nc));break; -case TM_DIV:setnvalue(ra,luai_numdiv(nb,nc));break; -case TM_MOD:setnvalue(ra,luai_nummod(nb,nc));break; -case TM_POW:setnvalue(ra,luai_numpow(nb,nc));break; -case TM_UNM:setnvalue(ra,luai_numunm(nb));break; -default:break; -} -} -else if(!call_binTM(L,rb,rc,ra,op)) -luaG_aritherror(L,rb,rc); -} -#define runtime_check(L,c){if(!(c))break;} -#define RA(i)(base+GETARG_A(i)) -#define RB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgR,base+GETARG_B(i)) -#define RKB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_B(i))?k+INDEXK(GETARG_B(i)):base+GETARG_B(i)) -#define RKC(i)check_exp(getCMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_C(i))?k+INDEXK(GETARG_C(i)):base+GETARG_C(i)) -#define KBx(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,k+GETARG_Bx(i)) -#define dojump(L,pc,i){(pc)+=(i);} -#define Protect(x){L->savedpc=pc;{x;};base=L->base;} -#define arith_op(op,tm){TValue*rb=RKB(i);TValue*rc=RKC(i);if(ttisnumber(rb)&&ttisnumber(rc)){lua_Number nb=nvalue(rb),nc=nvalue(rc);setnvalue(ra,op(nb,nc));}else Protect(Arith(L,ra,rb,rc,tm));} -static void luaV_execute(lua_State*L,int nexeccalls){ -LClosure*cl; -StkId base; -TValue*k; -const Instruction*pc; -reentry: -pc=L->savedpc; -cl=&clvalue(L->ci->func)->l; -base=L->base; -k=cl->p->k; -for(;;){ -const Instruction i=*pc++; -StkId ra; -ra=RA(i); -switch(GET_OPCODE(i)){ -case OP_MOVE:{ -setobj(L,ra,RB(i)); -continue; -} -case OP_LOADK:{ -setobj(L,ra,KBx(i)); -continue; -} -case OP_LOADBOOL:{ -setbvalue(ra,GETARG_B(i)); -if(GETARG_C(i))pc++; -continue; -} -case OP_LOADNIL:{ -TValue*rb=RB(i); -do{ -setnilvalue(rb--); -}while(rb>=ra); -continue; -} -case OP_GETUPVAL:{ -int b=GETARG_B(i); -setobj(L,ra,cl->upvals[b]->v); -continue; -} -case OP_GETGLOBAL:{ -TValue g; -TValue*rb=KBx(i); -sethvalue(L,&g,cl->env); -Protect(luaV_gettable(L,&g,rb,ra)); -continue; -} -case OP_GETTABLE:{ -Protect(luaV_gettable(L,RB(i),RKC(i),ra)); -continue; -} -case OP_SETGLOBAL:{ -TValue g; -sethvalue(L,&g,cl->env); -Protect(luaV_settable(L,&g,KBx(i),ra)); -continue; -} -case OP_SETUPVAL:{ -UpVal*uv=cl->upvals[GETARG_B(i)]; -setobj(L,uv->v,ra); -luaC_barrier(L,uv,ra); -continue; -} -case OP_SETTABLE:{ -Protect(luaV_settable(L,ra,RKB(i),RKC(i))); -continue; -} -case OP_NEWTABLE:{ -int b=GETARG_B(i); -int c=GETARG_C(i); -sethvalue(L,ra,luaH_new(L,luaO_fb2int(b),luaO_fb2int(c))); -Protect(luaC_checkGC(L)); -continue; -} -case OP_SELF:{ -StkId rb=RB(i); -setobj(L,ra+1,rb); -Protect(luaV_gettable(L,rb,RKC(i),ra)); -continue; -} -case OP_ADD:{ -arith_op(luai_numadd,TM_ADD); -continue; -} -case OP_SUB:{ -arith_op(luai_numsub,TM_SUB); -continue; -} -case OP_MUL:{ -arith_op(luai_nummul,TM_MUL); -continue; -} -case OP_DIV:{ -arith_op(luai_numdiv,TM_DIV); -continue; -} -case OP_MOD:{ -arith_op(luai_nummod,TM_MOD); -continue; -} -case OP_POW:{ -arith_op(luai_numpow,TM_POW); -continue; -} -case OP_UNM:{ -TValue*rb=RB(i); -if(ttisnumber(rb)){ -lua_Number nb=nvalue(rb); -setnvalue(ra,luai_numunm(nb)); -} -else{ -Protect(Arith(L,ra,rb,rb,TM_UNM)); -} -continue; -} -case OP_NOT:{ -int res=l_isfalse(RB(i)); -setbvalue(ra,res); -continue; -} -case OP_LEN:{ -const TValue*rb=RB(i); -switch(ttype(rb)){ -case 5:{ -setnvalue(ra,cast_num(luaH_getn(hvalue(rb)))); -break; -} -case 4:{ -setnvalue(ra,cast_num(tsvalue(rb)->len)); -break; -} -default:{ -Protect( -if(!call_binTM(L,rb,(&luaO_nilobject_),ra,TM_LEN)) -luaG_typeerror(L,rb,"get length of"); -) -} -} -continue; -} -case OP_CONCAT:{ -int b=GETARG_B(i); -int c=GETARG_C(i); -Protect(luaV_concat(L,c-b+1,c);luaC_checkGC(L)); -setobj(L,RA(i),base+b); -continue; -} -case OP_JMP:{ -dojump(L,pc,GETARG_sBx(i)); -continue; -} -case OP_EQ:{ -TValue*rb=RKB(i); -TValue*rc=RKC(i); -Protect( -if(equalobj(L,rb,rc)==GETARG_A(i)) -dojump(L,pc,GETARG_sBx(*pc)); -) -pc++; -continue; -} -case OP_LT:{ -Protect( -if(luaV_lessthan(L,RKB(i),RKC(i))==GETARG_A(i)) -dojump(L,pc,GETARG_sBx(*pc)); -) -pc++; -continue; -} -case OP_LE:{ -Protect( -if(lessequal(L,RKB(i),RKC(i))==GETARG_A(i)) -dojump(L,pc,GETARG_sBx(*pc)); -) -pc++; -continue; -} -case OP_TEST:{ -if(l_isfalse(ra)!=GETARG_C(i)) -dojump(L,pc,GETARG_sBx(*pc)); -pc++; -continue; -} -case OP_TESTSET:{ -TValue*rb=RB(i); -if(l_isfalse(rb)!=GETARG_C(i)){ -setobj(L,ra,rb); -dojump(L,pc,GETARG_sBx(*pc)); -} -pc++; -continue; -} -case OP_CALL:{ -int b=GETARG_B(i); -int nresults=GETARG_C(i)-1; -if(b!=0)L->top=ra+b; -L->savedpc=pc; -switch(luaD_precall(L,ra,nresults)){ -case 0:{ -nexeccalls++; -goto reentry; -} -case 1:{ -if(nresults>=0)L->top=L->ci->top; -base=L->base; -continue; -} -default:{ -return; -} -} -} -case OP_TAILCALL:{ -int b=GETARG_B(i); -if(b!=0)L->top=ra+b; -L->savedpc=pc; -switch(luaD_precall(L,ra,(-1))){ -case 0:{ -CallInfo*ci=L->ci-1; -int aux; -StkId func=ci->func; -StkId pfunc=(ci+1)->func; -if(L->openupval)luaF_close(L,ci->base); -L->base=ci->base=ci->func+((ci+1)->base-pfunc); -for(aux=0;pfunc+auxtop;aux++) -setobj(L,func+aux,pfunc+aux); -ci->top=L->top=func+aux; -ci->savedpc=L->savedpc; -ci->tailcalls++; -L->ci--; -goto reentry; -} -case 1:{ -base=L->base; -continue; -} -default:{ -return; -} -} -} -case OP_RETURN:{ -int b=GETARG_B(i); -if(b!=0)L->top=ra+b-1; -if(L->openupval)luaF_close(L,base); -L->savedpc=pc; -b=luaD_poscall(L,ra); -if(--nexeccalls==0) -return; -else{ -if(b)L->top=L->ci->top; -goto reentry; -} -} -case OP_FORLOOP:{ -lua_Number step=nvalue(ra+2); -lua_Number idx=luai_numadd(nvalue(ra),step); -lua_Number limit=nvalue(ra+1); -if(luai_numlt(0,step)?luai_numle(idx,limit) -:luai_numle(limit,idx)){ -dojump(L,pc,GETARG_sBx(i)); -setnvalue(ra,idx); -setnvalue(ra+3,idx); -} -continue; -} -case OP_FORPREP:{ -const TValue*init=ra; -const TValue*plimit=ra+1; -const TValue*pstep=ra+2; -L->savedpc=pc; -if(!tonumber(init,ra)) -luaG_runerror(L,LUA_QL("for")" initial value must be a number"); -else if(!tonumber(plimit,ra+1)) -luaG_runerror(L,LUA_QL("for")" limit must be a number"); -else if(!tonumber(pstep,ra+2)) -luaG_runerror(L,LUA_QL("for")" step must be a number"); -setnvalue(ra,luai_numsub(nvalue(ra),nvalue(pstep))); -dojump(L,pc,GETARG_sBx(i)); -continue; -} -case OP_TFORLOOP:{ -StkId cb=ra+3; -setobj(L,cb+2,ra+2); -setobj(L,cb+1,ra+1); -setobj(L,cb,ra); -L->top=cb+3; -Protect(luaD_call(L,cb,GETARG_C(i))); -L->top=L->ci->top; -cb=RA(i)+3; -if(!ttisnil(cb)){ -setobj(L,cb-1,cb); -dojump(L,pc,GETARG_sBx(*pc)); -} -pc++; -continue; -} -case OP_SETLIST:{ -int n=GETARG_B(i); -int c=GETARG_C(i); -int last; -Table*h; -if(n==0){ -n=cast_int(L->top-ra)-1; -L->top=L->ci->top; -} -if(c==0)c=cast_int(*pc++); -runtime_check(L,ttistable(ra)); -h=hvalue(ra); -last=((c-1)*50)+n; -if(last>h->sizearray) -luaH_resizearray(L,h,last); -for(;n>0;n--){ -TValue*val=ra+n; -setobj(L,luaH_setnum(L,h,last--),val); -luaC_barriert(L,h,val); -} -continue; -} -case OP_CLOSE:{ -luaF_close(L,ra); -continue; -} -case OP_CLOSURE:{ -Proto*p; -Closure*ncl; -int nup,j; -p=cl->p->p[GETARG_Bx(i)]; -nup=p->nups; -ncl=luaF_newLclosure(L,nup,cl->env); -ncl->l.p=p; -for(j=0;jl.upvals[j]=cl->upvals[GETARG_B(*pc)]; -else{ -ncl->l.upvals[j]=luaF_findupval(L,base+GETARG_B(*pc)); -} -} -setclvalue(L,ra,ncl); -Protect(luaC_checkGC(L)); -continue; -} -case OP_VARARG:{ -int b=GETARG_B(i)-1; -int j; -CallInfo*ci=L->ci; -int n=cast_int(ci->base-ci->func)-cl->p->numparams-1; -if(b==(-1)){ -Protect(luaD_checkstack(L,n)); -ra=RA(i); -b=n; -L->top=ra+n; -} -for(j=0;jbase-n+j); -} -else{ -setnilvalue(ra+j); -} -} -continue; -} -} -} -} -#define api_checknelems(L,n)luai_apicheck(L,(n)<=(L->top-L->base)) -#define api_checkvalidindex(L,i)luai_apicheck(L,(i)!=(&luaO_nilobject_)) -#define api_incr_top(L){luai_apicheck(L,L->topci->top);L->top++;} -static TValue*index2adr(lua_State*L,int idx){ -if(idx>0){ -TValue*o=L->base+(idx-1); -luai_apicheck(L,idx<=L->ci->top-L->base); -if(o>=L->top)return cast(TValue*,(&luaO_nilobject_)); -else return o; -} -else if(idx>(-10000)){ -luai_apicheck(L,idx!=0&&-idx<=L->top-L->base); -return L->top+idx; -} -else switch(idx){ -case(-10000):return registry(L); -case(-10001):{ -Closure*func=curr_func(L); -sethvalue(L,&L->env,func->c.env); -return&L->env; -} -case(-10002):return gt(L); -default:{ -Closure*func=curr_func(L); -idx=(-10002)-idx; -return(idx<=func->c.nupvalues) -?&func->c.upvalue[idx-1] -:cast(TValue*,(&luaO_nilobject_)); -} -} -} -static Table*getcurrenv(lua_State*L){ -if(L->ci==L->base_ci) -return hvalue(gt(L)); -else{ -Closure*func=curr_func(L); -return func->c.env; -} -} -static int lua_checkstack(lua_State*L,int size){ -int res=1; -if(size>8000||(L->top-L->base+size)>8000) -res=0; -else if(size>0){ -luaD_checkstack(L,size); -if(L->ci->toptop+size) -L->ci->top=L->top+size; -} -return res; -} -static lua_CFunction lua_atpanic(lua_State*L,lua_CFunction panicf){ -lua_CFunction old; -old=G(L)->panic; -G(L)->panic=panicf; -return old; -} -static int lua_gettop(lua_State*L){ -return cast_int(L->top-L->base); -} -static void lua_settop(lua_State*L,int idx){ -if(idx>=0){ -luai_apicheck(L,idx<=L->stack_last-L->base); -while(L->topbase+idx) -setnilvalue(L->top++); -L->top=L->base+idx; -} -else{ -luai_apicheck(L,-(idx+1)<=(L->top-L->base)); -L->top+=idx+1; -} -} -static void lua_remove(lua_State*L,int idx){ -StkId p; -p=index2adr(L,idx); -api_checkvalidindex(L,p); -while(++ptop)setobj(L,p-1,p); -L->top--; -} -static void lua_insert(lua_State*L,int idx){ -StkId p; -StkId q; -p=index2adr(L,idx); -api_checkvalidindex(L,p); -for(q=L->top;q>p;q--)setobj(L,q,q-1); -setobj(L,p,L->top); -} -static void lua_replace(lua_State*L,int idx){ -StkId o; -if(idx==(-10001)&&L->ci==L->base_ci) -luaG_runerror(L,"no calling environment"); -api_checknelems(L,1); -o=index2adr(L,idx); -api_checkvalidindex(L,o); -if(idx==(-10001)){ -Closure*func=curr_func(L); -luai_apicheck(L,ttistable(L->top-1)); -func->c.env=hvalue(L->top-1); -luaC_barrier(L,func,L->top-1); -} -else{ -setobj(L,o,L->top-1); -if(idx<(-10002)) -luaC_barrier(L,curr_func(L),L->top-1); -} -L->top--; -} -static void lua_pushvalue(lua_State*L,int idx){ -setobj(L,L->top,index2adr(L,idx)); -api_incr_top(L); -} -static int lua_type(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -return(o==(&luaO_nilobject_))?(-1):ttype(o); -} -static const char*lua_typename(lua_State*L,int t){ -UNUSED(L); -return(t==(-1))?"no value":luaT_typenames[t]; -} -static int lua_iscfunction(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -return iscfunction(o); -} -static int lua_isnumber(lua_State*L,int idx){ -TValue n; -const TValue*o=index2adr(L,idx); -return tonumber(o,&n); -} -static int lua_isstring(lua_State*L,int idx){ -int t=lua_type(L,idx); -return(t==4||t==3); -} -static int lua_rawequal(lua_State*L,int index1,int index2){ -StkId o1=index2adr(L,index1); -StkId o2=index2adr(L,index2); -return(o1==(&luaO_nilobject_)||o2==(&luaO_nilobject_))?0 -:luaO_rawequalObj(o1,o2); -} -static int lua_lessthan(lua_State*L,int index1,int index2){ -StkId o1,o2; -int i; -o1=index2adr(L,index1); -o2=index2adr(L,index2); -i=(o1==(&luaO_nilobject_)||o2==(&luaO_nilobject_))?0 -:luaV_lessthan(L,o1,o2); -return i; -} -static lua_Number lua_tonumber(lua_State*L,int idx){ -TValue n; -const TValue*o=index2adr(L,idx); -if(tonumber(o,&n)) -return nvalue(o); -else -return 0; -} -static lua_Integer lua_tointeger(lua_State*L,int idx){ -TValue n; -const TValue*o=index2adr(L,idx); -if(tonumber(o,&n)){ -lua_Integer res; -lua_Number num=nvalue(o); -lua_number2integer(res,num); -return res; -} -else -return 0; -} -static int lua_toboolean(lua_State*L,int idx){ -const TValue*o=index2adr(L,idx); -return!l_isfalse(o); -} -static const char*lua_tolstring(lua_State*L,int idx,size_t*len){ -StkId o=index2adr(L,idx); -if(!ttisstring(o)){ -if(!luaV_tostring(L,o)){ -if(len!=NULL)*len=0; -return NULL; -} -luaC_checkGC(L); -o=index2adr(L,idx); -} -if(len!=NULL)*len=tsvalue(o)->len; -return svalue(o); -} -static size_t lua_objlen(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -switch(ttype(o)){ -case 4:return tsvalue(o)->len; -case 7:return uvalue(o)->len; -case 5:return luaH_getn(hvalue(o)); -case 3:{ -size_t l; -l=(luaV_tostring(L,o)?tsvalue(o)->len:0); -return l; -} -default:return 0; -} -} -static lua_CFunction lua_tocfunction(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -return(!iscfunction(o))?NULL:clvalue(o)->c.f; -} -static void*lua_touserdata(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -switch(ttype(o)){ -case 7:return(rawuvalue(o)+1); -case 2:return pvalue(o); -default:return NULL; -} -} -static void lua_pushnil(lua_State*L){ -setnilvalue(L->top); -api_incr_top(L); -} -static void lua_pushnumber(lua_State*L,lua_Number n){ -setnvalue(L->top,n); -api_incr_top(L); -} -static void lua_pushinteger(lua_State*L,lua_Integer n){ -setnvalue(L->top,cast_num(n)); -api_incr_top(L); -} -static void lua_pushlstring(lua_State*L,const char*s,size_t len){ -luaC_checkGC(L); -setsvalue(L,L->top,luaS_newlstr(L,s,len)); -api_incr_top(L); -} -static void lua_pushstring(lua_State*L,const char*s){ -if(s==NULL) -lua_pushnil(L); -else -lua_pushlstring(L,s,strlen(s)); -} -static const char*lua_pushvfstring(lua_State*L,const char*fmt, -va_list argp){ -const char*ret; -luaC_checkGC(L); -ret=luaO_pushvfstring(L,fmt,argp); -return ret; -} -static const char*lua_pushfstring(lua_State*L,const char*fmt,...){ -const char*ret; -va_list argp; -luaC_checkGC(L); -va_start(argp,fmt); -ret=luaO_pushvfstring(L,fmt,argp); -va_end(argp); -return ret; -} -static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n){ -Closure*cl; -luaC_checkGC(L); -api_checknelems(L,n); -cl=luaF_newCclosure(L,n,getcurrenv(L)); -cl->c.f=fn; -L->top-=n; -while(n--) -setobj(L,&cl->c.upvalue[n],L->top+n); -setclvalue(L,L->top,cl); -api_incr_top(L); -} -static void lua_pushboolean(lua_State*L,int b){ -setbvalue(L->top,(b!=0)); -api_incr_top(L); -} -static int lua_pushthread(lua_State*L){ -setthvalue(L,L->top,L); -api_incr_top(L); -return(G(L)->mainthread==L); -} -static void lua_gettable(lua_State*L,int idx){ -StkId t; -t=index2adr(L,idx); -api_checkvalidindex(L,t); -luaV_gettable(L,t,L->top-1,L->top-1); -} -static void lua_getfield(lua_State*L,int idx,const char*k){ -StkId t; -TValue key; -t=index2adr(L,idx); -api_checkvalidindex(L,t); -setsvalue(L,&key,luaS_new(L,k)); -luaV_gettable(L,t,&key,L->top); -api_incr_top(L); -} -static void lua_rawget(lua_State*L,int idx){ -StkId t; -t=index2adr(L,idx); -luai_apicheck(L,ttistable(t)); -setobj(L,L->top-1,luaH_get(hvalue(t),L->top-1)); -} -static void lua_rawgeti(lua_State*L,int idx,int n){ -StkId o; -o=index2adr(L,idx); -luai_apicheck(L,ttistable(o)); -setobj(L,L->top,luaH_getnum(hvalue(o),n)); -api_incr_top(L); -} -static void lua_createtable(lua_State*L,int narray,int nrec){ -luaC_checkGC(L); -sethvalue(L,L->top,luaH_new(L,narray,nrec)); -api_incr_top(L); -} -static int lua_getmetatable(lua_State*L,int objindex){ -const TValue*obj; -Table*mt=NULL; -int res; -obj=index2adr(L,objindex); -switch(ttype(obj)){ -case 5: -mt=hvalue(obj)->metatable; -break; -case 7: -mt=uvalue(obj)->metatable; -break; -default: -mt=G(L)->mt[ttype(obj)]; -break; -} -if(mt==NULL) -res=0; -else{ -sethvalue(L,L->top,mt); -api_incr_top(L); -res=1; -} -return res; -} -static void lua_getfenv(lua_State*L,int idx){ -StkId o; -o=index2adr(L,idx); -api_checkvalidindex(L,o); -switch(ttype(o)){ -case 6: -sethvalue(L,L->top,clvalue(o)->c.env); -break; -case 7: -sethvalue(L,L->top,uvalue(o)->env); -break; -case 8: -setobj(L,L->top,gt(thvalue(o))); -break; -default: -setnilvalue(L->top); -break; -} -api_incr_top(L); -} -static void lua_settable(lua_State*L,int idx){ -StkId t; -api_checknelems(L,2); -t=index2adr(L,idx); -api_checkvalidindex(L,t); -luaV_settable(L,t,L->top-2,L->top-1); -L->top-=2; -} -static void lua_setfield(lua_State*L,int idx,const char*k){ -StkId t; -TValue key; -api_checknelems(L,1); -t=index2adr(L,idx); -api_checkvalidindex(L,t); -setsvalue(L,&key,luaS_new(L,k)); -luaV_settable(L,t,&key,L->top-1); -L->top--; -} -static void lua_rawset(lua_State*L,int idx){ -StkId t; -api_checknelems(L,2); -t=index2adr(L,idx); -luai_apicheck(L,ttistable(t)); -setobj(L,luaH_set(L,hvalue(t),L->top-2),L->top-1); -luaC_barriert(L,hvalue(t),L->top-1); -L->top-=2; -} -static void lua_rawseti(lua_State*L,int idx,int n){ -StkId o; -api_checknelems(L,1); -o=index2adr(L,idx); -luai_apicheck(L,ttistable(o)); -setobj(L,luaH_setnum(L,hvalue(o),n),L->top-1); -luaC_barriert(L,hvalue(o),L->top-1); -L->top--; -} -static int lua_setmetatable(lua_State*L,int objindex){ -TValue*obj; -Table*mt; -api_checknelems(L,1); -obj=index2adr(L,objindex); -api_checkvalidindex(L,obj); -if(ttisnil(L->top-1)) -mt=NULL; -else{ -luai_apicheck(L,ttistable(L->top-1)); -mt=hvalue(L->top-1); -} -switch(ttype(obj)){ -case 5:{ -hvalue(obj)->metatable=mt; -if(mt) -luaC_objbarriert(L,hvalue(obj),mt); -break; -} -case 7:{ -uvalue(obj)->metatable=mt; -if(mt) -luaC_objbarrier(L,rawuvalue(obj),mt); -break; -} -default:{ -G(L)->mt[ttype(obj)]=mt; -break; -} -} -L->top--; -return 1; -} -static int lua_setfenv(lua_State*L,int idx){ -StkId o; -int res=1; -api_checknelems(L,1); -o=index2adr(L,idx); -api_checkvalidindex(L,o); -luai_apicheck(L,ttistable(L->top-1)); -switch(ttype(o)){ -case 6: -clvalue(o)->c.env=hvalue(L->top-1); -break; -case 7: -uvalue(o)->env=hvalue(L->top-1); -break; -case 8: -sethvalue(L,gt(thvalue(o)),hvalue(L->top-1)); -break; -default: -res=0; -break; -} -if(res)luaC_objbarrier(L,gcvalue(o),hvalue(L->top-1)); -L->top--; -return res; -} -#define adjustresults(L,nres){if(nres==(-1)&&L->top>=L->ci->top)L->ci->top=L->top;} -#define checkresults(L,na,nr)luai_apicheck(L,(nr)==(-1)||(L->ci->top-L->top>=(nr)-(na))) -static void lua_call(lua_State*L,int nargs,int nresults){ -StkId func; -api_checknelems(L,nargs+1); -checkresults(L,nargs,nresults); -func=L->top-(nargs+1); -luaD_call(L,func,nresults); -adjustresults(L,nresults); -} -struct CallS{ -StkId func; -int nresults; -}; -static void f_call(lua_State*L,void*ud){ -struct CallS*c=cast(struct CallS*,ud); -luaD_call(L,c->func,c->nresults); -} -static int lua_pcall(lua_State*L,int nargs,int nresults,int errfunc){ -struct CallS c; -int status; -ptrdiff_t func; -api_checknelems(L,nargs+1); -checkresults(L,nargs,nresults); -if(errfunc==0) -func=0; -else{ -StkId o=index2adr(L,errfunc); -api_checkvalidindex(L,o); -func=savestack(L,o); -} -c.func=L->top-(nargs+1); -c.nresults=nresults; -status=luaD_pcall(L,f_call,&c,savestack(L,c.func),func); -adjustresults(L,nresults); -return status; -} -static int lua_load(lua_State*L,lua_Reader reader,void*data, -const char*chunkname){ -ZIO z; -int status; -if(!chunkname)chunkname="?"; -luaZ_init(L,&z,reader,data); -status=luaD_protectedparser(L,&z,chunkname); -return status; -} -static int lua_error(lua_State*L){ -api_checknelems(L,1); -luaG_errormsg(L); -return 0; -} -static int lua_next(lua_State*L,int idx){ -StkId t; -int more; -t=index2adr(L,idx); -luai_apicheck(L,ttistable(t)); -more=luaH_next(L,hvalue(t),L->top-1); -if(more){ -api_incr_top(L); -} -else -L->top-=1; -return more; -} -static void lua_concat(lua_State*L,int n){ -api_checknelems(L,n); -if(n>=2){ -luaC_checkGC(L); -luaV_concat(L,n,cast_int(L->top-L->base)-1); -L->top-=(n-1); -} -else if(n==0){ -setsvalue(L,L->top,luaS_newlstr(L,"",0)); -api_incr_top(L); -} -} -static void*lua_newuserdata(lua_State*L,size_t size){ -Udata*u; -luaC_checkGC(L); -u=luaS_newudata(L,size,getcurrenv(L)); -setuvalue(L,L->top,u); -api_incr_top(L); -return u+1; -} -#define luaL_getn(L,i)((int)lua_objlen(L,i)) -#define luaL_setn(L,i,j)((void)0) -typedef struct luaL_Reg{ -const char*name; -lua_CFunction func; -}luaL_Reg; -static void luaI_openlib(lua_State*L,const char*libname, -const luaL_Reg*l,int nup); -static int luaL_argerror(lua_State*L,int numarg,const char*extramsg); -static const char* luaL_checklstring(lua_State*L,int numArg, -size_t*l); -static const char* luaL_optlstring(lua_State*L,int numArg, -const char*def,size_t*l); -static lua_Integer luaL_checkinteger(lua_State*L,int numArg); -static lua_Integer luaL_optinteger(lua_State*L,int nArg, -lua_Integer def); -static int luaL_error(lua_State*L,const char*fmt,...); -static const char* luaL_findtable(lua_State*L,int idx, -const char*fname,int szhint); -#define luaL_argcheck(L,cond,numarg,extramsg)((void)((cond)||luaL_argerror(L,(numarg),(extramsg)))) -#define luaL_checkstring(L,n)(luaL_checklstring(L,(n),NULL)) -#define luaL_optstring(L,n,d)(luaL_optlstring(L,(n),(d),NULL)) -#define luaL_checkint(L,n)((int)luaL_checkinteger(L,(n))) -#define luaL_optint(L,n,d)((int)luaL_optinteger(L,(n),(d))) -#define luaL_typename(L,i)lua_typename(L,lua_type(L,(i))) -#define luaL_getmetatable(L,n)(lua_getfield(L,(-10000),(n))) -#define luaL_opt(L,f,n,d)(lua_isnoneornil(L,(n))?(d):f(L,(n))) -typedef struct luaL_Buffer{ -char*p; -int lvl; -lua_State*L; -char buffer[BUFSIZ]; -}luaL_Buffer; -#define luaL_addchar(B,c)((void)((B)->p<((B)->buffer+BUFSIZ)||luaL_prepbuffer(B)),(*(B)->p++=(char)(c))) -#define luaL_addsize(B,n)((B)->p+=(n)) -static char* luaL_prepbuffer(luaL_Buffer*B); -static int luaL_argerror(lua_State*L,int narg,const char*extramsg){ -lua_Debug ar; -if(!lua_getstack(L,0,&ar)) -return luaL_error(L,"bad argument #%d (%s)",narg,extramsg); -lua_getinfo(L,"n",&ar); -if(strcmp(ar.namewhat,"method")==0){ -narg--; -if(narg==0) -return luaL_error(L,"calling "LUA_QL("%s")" on bad self (%s)", -ar.name,extramsg); -} -if(ar.name==NULL) -ar.name="?"; -return luaL_error(L,"bad argument #%d to "LUA_QL("%s")" (%s)", -narg,ar.name,extramsg); -} -static int luaL_typerror(lua_State*L,int narg,const char*tname){ -const char*msg=lua_pushfstring(L,"%s expected, got %s", -tname,luaL_typename(L,narg)); -return luaL_argerror(L,narg,msg); -} -static void tag_error(lua_State*L,int narg,int tag){ -luaL_typerror(L,narg,lua_typename(L,tag)); -} -static void luaL_where(lua_State*L,int level){ -lua_Debug ar; -if(lua_getstack(L,level,&ar)){ -lua_getinfo(L,"Sl",&ar); -if(ar.currentline>0){ -lua_pushfstring(L,"%s:%d: ",ar.short_src,ar.currentline); -return; -} -} -lua_pushliteral(L,""); -} -static int luaL_error(lua_State*L,const char*fmt,...){ -va_list argp; -va_start(argp,fmt); -luaL_where(L,1); -lua_pushvfstring(L,fmt,argp); -va_end(argp); -lua_concat(L,2); -return lua_error(L); -} -static int luaL_newmetatable(lua_State*L,const char*tname){ -lua_getfield(L,(-10000),tname); -if(!lua_isnil(L,-1)) -return 0; -lua_pop(L,1); -lua_newtable(L); -lua_pushvalue(L,-1); -lua_setfield(L,(-10000),tname); -return 1; -} -static void*luaL_checkudata(lua_State*L,int ud,const char*tname){ -void*p=lua_touserdata(L,ud); -if(p!=NULL){ -if(lua_getmetatable(L,ud)){ -lua_getfield(L,(-10000),tname); -if(lua_rawequal(L,-1,-2)){ -lua_pop(L,2); -return p; -} -} -} -luaL_typerror(L,ud,tname); -return NULL; -} -static void luaL_checkstack(lua_State*L,int space,const char*mes){ -if(!lua_checkstack(L,space)) -luaL_error(L,"stack overflow (%s)",mes); -} -static void luaL_checktype(lua_State*L,int narg,int t){ -if(lua_type(L,narg)!=t) -tag_error(L,narg,t); -} -static void luaL_checkany(lua_State*L,int narg){ -if(lua_type(L,narg)==(-1)) -luaL_argerror(L,narg,"value expected"); -} -static const char*luaL_checklstring(lua_State*L,int narg,size_t*len){ -const char*s=lua_tolstring(L,narg,len); -if(!s)tag_error(L,narg,4); -return s; -} -static const char*luaL_optlstring(lua_State*L,int narg, -const char*def,size_t*len){ -if(lua_isnoneornil(L,narg)){ -if(len) -*len=(def?strlen(def):0); -return def; -} -else return luaL_checklstring(L,narg,len); -} -static lua_Number luaL_checknumber(lua_State*L,int narg){ -lua_Number d=lua_tonumber(L,narg); -if(d==0&&!lua_isnumber(L,narg)) -tag_error(L,narg,3); -return d; -} -static lua_Integer luaL_checkinteger(lua_State*L,int narg){ -lua_Integer d=lua_tointeger(L,narg); -if(d==0&&!lua_isnumber(L,narg)) -tag_error(L,narg,3); -return d; -} -static lua_Integer luaL_optinteger(lua_State*L,int narg, -lua_Integer def){ -return luaL_opt(L,luaL_checkinteger,narg,def); -} -static int luaL_getmetafield(lua_State*L,int obj,const char*event){ -if(!lua_getmetatable(L,obj)) -return 0; -lua_pushstring(L,event); -lua_rawget(L,-2); -if(lua_isnil(L,-1)){ -lua_pop(L,2); -return 0; -} -else{ -lua_remove(L,-2); -return 1; -} -} -static void luaL_register(lua_State*L,const char*libname, -const luaL_Reg*l){ -luaI_openlib(L,libname,l,0); -} -static int libsize(const luaL_Reg*l){ -int size=0; -for(;l->name;l++)size++; -return size; -} -static void luaI_openlib(lua_State*L,const char*libname, -const luaL_Reg*l,int nup){ -if(libname){ -int size=libsize(l); -luaL_findtable(L,(-10000),"_LOADED",1); -lua_getfield(L,-1,libname); -if(!lua_istable(L,-1)){ -lua_pop(L,1); -if(luaL_findtable(L,(-10002),libname,size)!=NULL) -luaL_error(L,"name conflict for module "LUA_QL("%s"),libname); -lua_pushvalue(L,-1); -lua_setfield(L,-3,libname); -} -lua_remove(L,-2); -lua_insert(L,-(nup+1)); -} -for(;l->name;l++){ -int i; -for(i=0;ifunc,nup); -lua_setfield(L,-(nup+2),l->name); -} -lua_pop(L,nup); -} -static const char*luaL_findtable(lua_State*L,int idx, -const char*fname,int szhint){ -const char*e; -lua_pushvalue(L,idx); -do{ -e=strchr(fname,'.'); -if(e==NULL)e=fname+strlen(fname); -lua_pushlstring(L,fname,e-fname); -lua_rawget(L,-2); -if(lua_isnil(L,-1)){ -lua_pop(L,1); -lua_createtable(L,0,(*e=='.'?1:szhint)); -lua_pushlstring(L,fname,e-fname); -lua_pushvalue(L,-2); -lua_settable(L,-4); -} -else if(!lua_istable(L,-1)){ -lua_pop(L,2); -return fname; -} -lua_remove(L,-2); -fname=e+1; -}while(*e=='.'); -return NULL; -} -#define bufflen(B)((B)->p-(B)->buffer) -#define bufffree(B)((size_t)(BUFSIZ-bufflen(B))) -static int emptybuffer(luaL_Buffer*B){ -size_t l=bufflen(B); -if(l==0)return 0; -else{ -lua_pushlstring(B->L,B->buffer,l); -B->p=B->buffer; -B->lvl++; -return 1; -} -} -static void adjuststack(luaL_Buffer*B){ -if(B->lvl>1){ -lua_State*L=B->L; -int toget=1; -size_t toplen=lua_strlen(L,-1); -do{ -size_t l=lua_strlen(L,-(toget+1)); -if(B->lvl-toget+1>=(20/2)||toplen>l){ -toplen+=l; -toget++; -} -else break; -}while(togetlvl); -lua_concat(L,toget); -B->lvl=B->lvl-toget+1; -} -} -static char*luaL_prepbuffer(luaL_Buffer*B){ -if(emptybuffer(B)) -adjuststack(B); -return B->buffer; -} -static void luaL_addlstring(luaL_Buffer*B,const char*s,size_t l){ -while(l--) -luaL_addchar(B,*s++); -} -static void luaL_pushresult(luaL_Buffer*B){ -emptybuffer(B); -lua_concat(B->L,B->lvl); -B->lvl=1; -} -static void luaL_addvalue(luaL_Buffer*B){ -lua_State*L=B->L; -size_t vl; -const char*s=lua_tolstring(L,-1,&vl); -if(vl<=bufffree(B)){ -memcpy(B->p,s,vl); -B->p+=vl; -lua_pop(L,1); -} -else{ -if(emptybuffer(B)) -lua_insert(L,-2); -B->lvl++; -adjuststack(B); -} -} -static void luaL_buffinit(lua_State*L,luaL_Buffer*B){ -B->L=L; -B->p=B->buffer; -B->lvl=0; -} -typedef struct LoadF{ -int extraline; -FILE*f; -char buff[BUFSIZ]; -}LoadF; -static const char*getF(lua_State*L,void*ud,size_t*size){ -LoadF*lf=(LoadF*)ud; -(void)L; -if(lf->extraline){ -lf->extraline=0; -*size=1; -return"\n"; -} -if(feof(lf->f))return NULL; -*size=fread(lf->buff,1,sizeof(lf->buff),lf->f); -return(*size>0)?lf->buff:NULL; -} -static int errfile(lua_State*L,const char*what,int fnameindex){ -const char*serr=strerror(errno); -const char*filename=lua_tostring(L,fnameindex)+1; -lua_pushfstring(L,"cannot %s %s: %s",what,filename,serr); -lua_remove(L,fnameindex); -return(5+1); -} -static int luaL_loadfile(lua_State*L,const char*filename){ -LoadF lf; -int status,readstatus; -int c; -int fnameindex=lua_gettop(L)+1; -lf.extraline=0; -if(filename==NULL){ -lua_pushliteral(L,"=stdin"); -lf.f=stdin; -} -else{ -lua_pushfstring(L,"@%s",filename); -lf.f=fopen(filename,"r"); -if(lf.f==NULL)return errfile(L,"open",fnameindex); -} -c=getc(lf.f); -if(c=='#'){ -lf.extraline=1; -while((c=getc(lf.f))!=EOF&&c!='\n'); -if(c=='\n')c=getc(lf.f); -} -if(c=="\033Lua"[0]&&filename){ -lf.f=freopen(filename,"rb",lf.f); -if(lf.f==NULL)return errfile(L,"reopen",fnameindex); -while((c=getc(lf.f))!=EOF&&c!="\033Lua"[0]); -lf.extraline=0; -} -ungetc(c,lf.f); -status=lua_load(L,getF,&lf,lua_tostring(L,-1)); -readstatus=ferror(lf.f); -if(filename)fclose(lf.f); -if(readstatus){ -lua_settop(L,fnameindex); -return errfile(L,"read",fnameindex); -} -lua_remove(L,fnameindex); -return status; -} -typedef struct LoadS{ -const char*s; -size_t size; -}LoadS; -static const char*getS(lua_State*L,void*ud,size_t*size){ -LoadS*ls=(LoadS*)ud; -(void)L; -if(ls->size==0)return NULL; -*size=ls->size; -ls->size=0; -return ls->s; -} -static int luaL_loadbuffer(lua_State*L,const char*buff,size_t size, -const char*name){ -LoadS ls; -ls.s=buff; -ls.size=size; -return lua_load(L,getS,&ls,name); -} -static void*l_alloc(void*ud,void*ptr,size_t osize,size_t nsize){ -(void)ud; -(void)osize; -if(nsize==0){ -free(ptr); -return NULL; -} -else -return realloc(ptr,nsize); -} -static int panic(lua_State*L){ -(void)L; -fprintf(stderr,"PANIC: unprotected error in call to Lua API (%s)\n", -lua_tostring(L,-1)); -return 0; -} -static lua_State*luaL_newstate(void){ -lua_State*L=lua_newstate(l_alloc,NULL); -if(L)lua_atpanic(L,&panic); -return L; -} -static int luaB_tonumber(lua_State*L){ -int base=luaL_optint(L,2,10); -if(base==10){ -luaL_checkany(L,1); -if(lua_isnumber(L,1)){ -lua_pushnumber(L,lua_tonumber(L,1)); -return 1; -} -} -else{ -const char*s1=luaL_checkstring(L,1); -char*s2; -unsigned long n; -luaL_argcheck(L,2<=base&&base<=36,2,"base out of range"); -n=strtoul(s1,&s2,base); -if(s1!=s2){ -while(isspace((unsigned char)(*s2)))s2++; -if(*s2=='\0'){ -lua_pushnumber(L,(lua_Number)n); -return 1; -} -} -} -lua_pushnil(L); -return 1; -} -static int luaB_error(lua_State*L){ -int level=luaL_optint(L,2,1); -lua_settop(L,1); -if(lua_isstring(L,1)&&level>0){ -luaL_where(L,level); -lua_pushvalue(L,1); -lua_concat(L,2); -} -return lua_error(L); -} -static int luaB_setmetatable(lua_State*L){ -int t=lua_type(L,2); -luaL_checktype(L,1,5); -luaL_argcheck(L,t==0||t==5,2, -"nil or table expected"); -if(luaL_getmetafield(L,1,"__metatable")) -luaL_error(L,"cannot change a protected metatable"); -lua_settop(L,2); -lua_setmetatable(L,1); -return 1; -} -static void getfunc(lua_State*L,int opt){ -if(lua_isfunction(L,1))lua_pushvalue(L,1); -else{ -lua_Debug ar; -int level=opt?luaL_optint(L,1,1):luaL_checkint(L,1); -luaL_argcheck(L,level>=0,1,"level must be non-negative"); -if(lua_getstack(L,level,&ar)==0) -luaL_argerror(L,1,"invalid level"); -lua_getinfo(L,"f",&ar); -if(lua_isnil(L,-1)) -luaL_error(L,"no function environment for tail call at level %d", -level); -} -} -static int luaB_setfenv(lua_State*L){ -luaL_checktype(L,2,5); -getfunc(L,0); -lua_pushvalue(L,2); -if(lua_isnumber(L,1)&&lua_tonumber(L,1)==0){ -lua_pushthread(L); -lua_insert(L,-2); -lua_setfenv(L,-2); -return 0; -} -else if(lua_iscfunction(L,-2)||lua_setfenv(L,-2)==0) -luaL_error(L, -LUA_QL("setfenv")" cannot change environment of given object"); -return 1; -} -static int luaB_rawget(lua_State*L){ -luaL_checktype(L,1,5); -luaL_checkany(L,2); -lua_settop(L,2); -lua_rawget(L,1); -return 1; -} -static int luaB_type(lua_State*L){ -luaL_checkany(L,1); -lua_pushstring(L,luaL_typename(L,1)); -return 1; -} -static int luaB_next(lua_State*L){ -luaL_checktype(L,1,5); -lua_settop(L,2); -if(lua_next(L,1)) -return 2; -else{ -lua_pushnil(L); -return 1; -} -} -static int luaB_pairs(lua_State*L){ -luaL_checktype(L,1,5); -lua_pushvalue(L,lua_upvalueindex(1)); -lua_pushvalue(L,1); -lua_pushnil(L); -return 3; -} -static int ipairsaux(lua_State*L){ -int i=luaL_checkint(L,2); -luaL_checktype(L,1,5); -i++; -lua_pushinteger(L,i); -lua_rawgeti(L,1,i); -return(lua_isnil(L,-1))?0:2; -} -static int luaB_ipairs(lua_State*L){ -luaL_checktype(L,1,5); -lua_pushvalue(L,lua_upvalueindex(1)); -lua_pushvalue(L,1); -lua_pushinteger(L,0); -return 3; -} -static int load_aux(lua_State*L,int status){ -if(status==0) -return 1; -else{ -lua_pushnil(L); -lua_insert(L,-2); -return 2; -} -} -static int luaB_loadstring(lua_State*L){ -size_t l; -const char*s=luaL_checklstring(L,1,&l); -const char*chunkname=luaL_optstring(L,2,s); -return load_aux(L,luaL_loadbuffer(L,s,l,chunkname)); -} -static int luaB_loadfile(lua_State*L){ -const char*fname=luaL_optstring(L,1,NULL); -return load_aux(L,luaL_loadfile(L,fname)); -} -static int luaB_assert(lua_State*L){ -luaL_checkany(L,1); -if(!lua_toboolean(L,1)) -return luaL_error(L,"%s",luaL_optstring(L,2,"assertion failed!")); -return lua_gettop(L); -} -static int luaB_unpack(lua_State*L){ -int i,e,n; -luaL_checktype(L,1,5); -i=luaL_optint(L,2,1); -e=luaL_opt(L,luaL_checkint,3,luaL_getn(L,1)); -if(i>e)return 0; -n=e-i+1; -if(n<=0||!lua_checkstack(L,n)) -return luaL_error(L,"too many results to unpack"); -lua_rawgeti(L,1,i); -while(i++e)e=pos; -for(i=e;i>pos;i--){ -lua_rawgeti(L,1,i-1); -lua_rawseti(L,1,i); -} -break; -} -default:{ -return luaL_error(L,"wrong number of arguments to "LUA_QL("insert")); -} -} -luaL_setn(L,1,e); -lua_rawseti(L,1,pos); -return 0; -} -static int tremove(lua_State*L){ -int e=aux_getn(L,1); -int pos=luaL_optint(L,2,e); -if(!(1<=pos&&pos<=e)) -return 0; -luaL_setn(L,1,e-1); -lua_rawgeti(L,1,pos); -for(;posu)luaL_error(L,"invalid order function for sorting"); -lua_pop(L,1); -} -while(lua_rawgeti(L,1,--j),sort_comp(L,-3,-1)){ -if(j0); -} -l=strlen(p); -if(l==0||p[l-1]!='\n') -luaL_addsize(&b,l); -else{ -luaL_addsize(&b,l-1); -luaL_pushresult(&b); -return 1; -} -} -} -static int read_chars(lua_State*L,FILE*f,size_t n){ -size_t rlen; -size_t nr; -luaL_Buffer b; -luaL_buffinit(L,&b); -rlen=BUFSIZ; -do{ -char*p=luaL_prepbuffer(&b); -if(rlen>n)rlen=n; -nr=fread(p,sizeof(char),rlen,f); -luaL_addsize(&b,nr); -n-=nr; -}while(n>0&&nr==rlen); -luaL_pushresult(&b); -return(n==0||lua_objlen(L,-1)>0); -} -static int g_read(lua_State*L,FILE*f,int first){ -int nargs=lua_gettop(L)-1; -int success; -int n; -clearerr(f); -if(nargs==0){ -success=read_line(L,f); -n=first+1; -} -else{ -luaL_checkstack(L,nargs+20,"too many arguments"); -success=1; -for(n=first;nargs--&&success;n++){ -if(lua_type(L,n)==3){ -size_t l=(size_t)lua_tointeger(L,n); -success=(l==0)?test_eof(L,f):read_chars(L,f,l); -} -else{ -const char*p=lua_tostring(L,n); -luaL_argcheck(L,p&&p[0]=='*',n,"invalid option"); -switch(p[1]){ -case'n': -success=read_number(L,f); -break; -case'l': -success=read_line(L,f); -break; -case'a': -read_chars(L,f,~((size_t)0)); -success=1; -break; -default: -return luaL_argerror(L,n,"invalid format"); -} -} -} -} -if(ferror(f)) -return pushresult(L,0,NULL); -if(!success){ -lua_pop(L,1); -lua_pushnil(L); -} -return n-first; -} -static int io_read(lua_State*L){ -return g_read(L,getiofile(L,1),1); -} -static int f_read(lua_State*L){ -return g_read(L,tofile(L),2); -} -static int io_readline(lua_State*L){ -FILE*f=*(FILE**)lua_touserdata(L,lua_upvalueindex(1)); -int sucess; -if(f==NULL) -luaL_error(L,"file is already closed"); -sucess=read_line(L,f); -if(ferror(f)) -return luaL_error(L,"%s",strerror(errno)); -if(sucess)return 1; -else{ -if(lua_toboolean(L,lua_upvalueindex(2))){ -lua_settop(L,0); -lua_pushvalue(L,lua_upvalueindex(1)); -aux_close(L); -} -return 0; -} -} -static int g_write(lua_State*L,FILE*f,int arg){ -int nargs=lua_gettop(L)-1; -int status=1; -for(;nargs--;arg++){ -if(lua_type(L,arg)==3){ -status=status&& -fprintf(f,"%.14g",lua_tonumber(L,arg))>0; -} -else{ -size_t l; -const char*s=luaL_checklstring(L,arg,&l); -status=status&&(fwrite(s,sizeof(char),l,f)==l); -} -} -return pushresult(L,status,NULL); -} -static int io_write(lua_State*L){ -return g_write(L,getiofile(L,2),1); -} -static int f_write(lua_State*L){ -return g_write(L,tofile(L),2); -} -static int io_flush(lua_State*L){ -return pushresult(L,fflush(getiofile(L,2))==0,NULL); -} -static int f_flush(lua_State*L){ -return pushresult(L,fflush(tofile(L))==0,NULL); -} -static const luaL_Reg iolib[]={ -{"close",io_close}, -{"flush",io_flush}, -{"input",io_input}, -{"lines",io_lines}, -{"open",io_open}, -{"output",io_output}, -{"read",io_read}, -{"type",io_type}, -{"write",io_write}, -{NULL,NULL} -}; -static const luaL_Reg flib[]={ -{"close",io_close}, -{"flush",f_flush}, -{"lines",f_lines}, -{"read",f_read}, -{"write",f_write}, -{"__gc",io_gc}, -{NULL,NULL} -}; -static void createmeta(lua_State*L){ -luaL_newmetatable(L,"FILE*"); -lua_pushvalue(L,-1); -lua_setfield(L,-2,"__index"); -luaL_register(L,NULL,flib); -} -static void createstdfile(lua_State*L,FILE*f,int k,const char*fname){ -*newfile(L)=f; -if(k>0){ -lua_pushvalue(L,-1); -lua_rawseti(L,(-10001),k); -} -lua_pushvalue(L,-2); -lua_setfenv(L,-2); -lua_setfield(L,-3,fname); -} -static void newfenv(lua_State*L,lua_CFunction cls){ -lua_createtable(L,0,1); -lua_pushcfunction(L,cls); -lua_setfield(L,-2,"__close"); -} -static int luaopen_io(lua_State*L){ -createmeta(L); -newfenv(L,io_fclose); -lua_replace(L,(-10001)); -luaL_register(L,"io",iolib); -newfenv(L,io_noclose); -createstdfile(L,stdin,1,"stdin"); -createstdfile(L,stdout,2,"stdout"); -createstdfile(L,stderr,0,"stderr"); -lua_pop(L,1); -lua_getfield(L,-1,"popen"); -newfenv(L,io_pclose); -lua_setfenv(L,-2); -lua_pop(L,1); -return 1; -} -static int os_pushresult(lua_State*L,int i,const char*filename){ -int en=errno; -if(i){ -lua_pushboolean(L,1); -return 1; -} -else{ -lua_pushnil(L); -lua_pushfstring(L,"%s: %s",filename,strerror(en)); -lua_pushinteger(L,en); -return 3; -} -} -static int os_remove(lua_State*L){ -const char*filename=luaL_checkstring(L,1); -return os_pushresult(L,remove(filename)==0,filename); -} -static int os_exit(lua_State*L){ -exit(luaL_optint(L,1,EXIT_SUCCESS)); -} -static const luaL_Reg syslib[]={ -{"exit",os_exit}, -{"remove",os_remove}, -{NULL,NULL} -}; -static int luaopen_os(lua_State*L){ -luaL_register(L,"os",syslib); -return 1; -} -#define uchar(c)((unsigned char)(c)) -static ptrdiff_t posrelat(ptrdiff_t pos,size_t len){ -if(pos<0)pos+=(ptrdiff_t)len+1; -return(pos>=0)?pos:0; -} -static int str_sub(lua_State*L){ -size_t l; -const char*s=luaL_checklstring(L,1,&l); -ptrdiff_t start=posrelat(luaL_checkinteger(L,2),l); -ptrdiff_t end=posrelat(luaL_optinteger(L,3,-1),l); -if(start<1)start=1; -if(end>(ptrdiff_t)l)end=(ptrdiff_t)l; -if(start<=end) -lua_pushlstring(L,s+start-1,end-start+1); -else lua_pushliteral(L,""); -return 1; -} -static int str_lower(lua_State*L){ -size_t l; -size_t i; -luaL_Buffer b; -const char*s=luaL_checklstring(L,1,&l); -luaL_buffinit(L,&b); -for(i=0;i0) -luaL_addlstring(&b,s,l); -luaL_pushresult(&b); -return 1; -} -static int str_byte(lua_State*L){ -size_t l; -const char*s=luaL_checklstring(L,1,&l); -ptrdiff_t posi=posrelat(luaL_optinteger(L,2,1),l); -ptrdiff_t pose=posrelat(luaL_optinteger(L,3,posi),l); -int n,i; -if(posi<=0)posi=1; -if((size_t)pose>l)pose=l; -if(posi>pose)return 0; -n=(int)(pose-posi+1); -if(posi+n<=pose) -luaL_error(L,"string slice too long"); -luaL_checkstack(L,n,"string slice too long"); -for(i=0;i=ms->level||ms->capture[l].len==(-1)) -return luaL_error(ms->L,"invalid capture index"); -return l; -} -static int capture_to_close(MatchState*ms){ -int level=ms->level; -for(level--;level>=0;level--) -if(ms->capture[level].len==(-1))return level; -return luaL_error(ms->L,"invalid pattern capture"); -} -static const char*classend(MatchState*ms,const char*p){ -switch(*p++){ -case'%':{ -if(*p=='\0') -luaL_error(ms->L,"malformed pattern (ends with "LUA_QL("%%")")"); -return p+1; -} -case'[':{ -if(*p=='^')p++; -do{ -if(*p=='\0') -luaL_error(ms->L,"malformed pattern (missing "LUA_QL("]")")"); -if(*(p++)=='%'&&*p!='\0') -p++; -}while(*p!=']'); -return p+1; -} -default:{ -return p; -} -} -} -static int match_class(int c,int cl){ -int res; -switch(tolower(cl)){ -case'a':res=isalpha(c);break; -case'c':res=iscntrl(c);break; -case'd':res=isdigit(c);break; -case'l':res=islower(c);break; -case'p':res=ispunct(c);break; -case's':res=isspace(c);break; -case'u':res=isupper(c);break; -case'w':res=isalnum(c);break; -case'x':res=isxdigit(c);break; -case'z':res=(c==0);break; -default:return(cl==c); -} -return(islower(cl)?res:!res); -} -static int matchbracketclass(int c,const char*p,const char*ec){ -int sig=1; -if(*(p+1)=='^'){ -sig=0; -p++; -} -while(++pL,"unbalanced pattern"); -if(*s!=*p)return NULL; -else{ -int b=*p; -int e=*(p+1); -int cont=1; -while(++ssrc_end){ -if(*s==e){ -if(--cont==0)return s+1; -} -else if(*s==b)cont++; -} -} -return NULL; -} -static const char*max_expand(MatchState*ms,const char*s, -const char*p,const char*ep){ -ptrdiff_t i=0; -while((s+i)src_end&&singlematch(uchar(*(s+i)),p,ep)) -i++; -while(i>=0){ -const char*res=match(ms,(s+i),ep+1); -if(res)return res; -i--; -} -return NULL; -} -static const char*min_expand(MatchState*ms,const char*s, -const char*p,const char*ep){ -for(;;){ -const char*res=match(ms,s,ep+1); -if(res!=NULL) -return res; -else if(ssrc_end&&singlematch(uchar(*s),p,ep)) -s++; -else return NULL; -} -} -static const char*start_capture(MatchState*ms,const char*s, -const char*p,int what){ -const char*res; -int level=ms->level; -if(level>=32)luaL_error(ms->L,"too many captures"); -ms->capture[level].init=s; -ms->capture[level].len=what; -ms->level=level+1; -if((res=match(ms,s,p))==NULL) -ms->level--; -return res; -} -static const char*end_capture(MatchState*ms,const char*s, -const char*p){ -int l=capture_to_close(ms); -const char*res; -ms->capture[l].len=s-ms->capture[l].init; -if((res=match(ms,s,p))==NULL) -ms->capture[l].len=(-1); -return res; -} -static const char*match_capture(MatchState*ms,const char*s,int l){ -size_t len; -l=check_capture(ms,l); -len=ms->capture[l].len; -if((size_t)(ms->src_end-s)>=len&& -memcmp(ms->capture[l].init,s,len)==0) -return s+len; -else return NULL; -} -static const char*match(MatchState*ms,const char*s,const char*p){ -init: -switch(*p){ -case'(':{ -if(*(p+1)==')') -return start_capture(ms,s,p+2,(-2)); -else -return start_capture(ms,s,p+1,(-1)); -} -case')':{ -return end_capture(ms,s,p+1); -} -case'%':{ -switch(*(p+1)){ -case'b':{ -s=matchbalance(ms,s,p+2); -if(s==NULL)return NULL; -p+=4;goto init; -} -case'f':{ -const char*ep;char previous; -p+=2; -if(*p!='[') -luaL_error(ms->L,"missing "LUA_QL("[")" after " -LUA_QL("%%f")" in pattern"); -ep=classend(ms,p); -previous=(s==ms->src_init)?'\0':*(s-1); -if(matchbracketclass(uchar(previous),p,ep-1)|| -!matchbracketclass(uchar(*s),p,ep-1))return NULL; -p=ep;goto init; -} -default:{ -if(isdigit(uchar(*(p+1)))){ -s=match_capture(ms,s,uchar(*(p+1))); -if(s==NULL)return NULL; -p+=2;goto init; -} -goto dflt; -} -} -} -case'\0':{ -return s; -} -case'$':{ -if(*(p+1)=='\0') -return(s==ms->src_end)?s:NULL; -else goto dflt; -} -default:dflt:{ -const char*ep=classend(ms,p); -int m=ssrc_end&&singlematch(uchar(*s),p,ep); -switch(*ep){ -case'?':{ -const char*res; -if(m&&((res=match(ms,s+1,ep+1))!=NULL)) -return res; -p=ep+1;goto init; -} -case'*':{ -return max_expand(ms,s,p,ep); -} -case'+':{ -return(m?max_expand(ms,s+1,p,ep):NULL); -} -case'-':{ -return min_expand(ms,s,p,ep); -} -default:{ -if(!m)return NULL; -s++;p=ep;goto init; -} -} -} -} -} -static const char*lmemfind(const char*s1,size_t l1, -const char*s2,size_t l2){ -if(l2==0)return s1; -else if(l2>l1)return NULL; -else{ -const char*init; -l2--; -l1=l1-l2; -while(l1>0&&(init=(const char*)memchr(s1,*s2,l1))!=NULL){ -init++; -if(memcmp(init,s2+1,l2)==0) -return init-1; -else{ -l1-=init-s1; -s1=init; -} -} -return NULL; -} -} -static void push_onecapture(MatchState*ms,int i,const char*s, -const char*e){ -if(i>=ms->level){ -if(i==0) -lua_pushlstring(ms->L,s,e-s); -else -luaL_error(ms->L,"invalid capture index"); -} -else{ -ptrdiff_t l=ms->capture[i].len; -if(l==(-1))luaL_error(ms->L,"unfinished capture"); -if(l==(-2)) -lua_pushinteger(ms->L,ms->capture[i].init-ms->src_init+1); -else -lua_pushlstring(ms->L,ms->capture[i].init,l); -} -} -static int push_captures(MatchState*ms,const char*s,const char*e){ -int i; -int nlevels=(ms->level==0&&s)?1:ms->level; -luaL_checkstack(ms->L,nlevels,"too many captures"); -for(i=0;il1)init=(ptrdiff_t)l1; -if(find&&(lua_toboolean(L,4)|| -strpbrk(p,"^$*+?.([%-")==NULL)){ -const char*s2=lmemfind(s+init,l1-init,p,l2); -if(s2){ -lua_pushinteger(L,s2-s+1); -lua_pushinteger(L,s2-s+l2); -return 2; -} -} -else{ -MatchState ms; -int anchor=(*p=='^')?(p++,1):0; -const char*s1=s+init; -ms.L=L; -ms.src_init=s; -ms.src_end=s+l1; -do{ -const char*res; -ms.level=0; -if((res=match(&ms,s1,p))!=NULL){ -if(find){ -lua_pushinteger(L,s1-s+1); -lua_pushinteger(L,res-s); -return push_captures(&ms,NULL,0)+2; -} -else -return push_captures(&ms,s1,res); -} -}while(s1++L,3,&l); -for(i=0;iL; -switch(lua_type(L,3)){ -case 3: -case 4:{ -add_s(ms,b,s,e); -return; -} -case 6:{ -int n; -lua_pushvalue(L,3); -n=push_captures(ms,s,e); -lua_call(L,n,1); -break; -} -case 5:{ -push_onecapture(ms,0,s,e); -lua_gettable(L,3); -break; -} -} -if(!lua_toboolean(L,-1)){ -lua_pop(L,1); -lua_pushlstring(L,s,e-s); -} -else if(!lua_isstring(L,-1)) -luaL_error(L,"invalid replacement value (a %s)",luaL_typename(L,-1)); -luaL_addvalue(b); -} -static int str_gsub(lua_State*L){ -size_t srcl; -const char*src=luaL_checklstring(L,1,&srcl); -const char*p=luaL_checkstring(L,2); -int tr=lua_type(L,3); -int max_s=luaL_optint(L,4,srcl+1); -int anchor=(*p=='^')?(p++,1):0; -int n=0; -MatchState ms; -luaL_Buffer b; -luaL_argcheck(L,tr==3||tr==4|| -tr==6||tr==5,3, -"string/function/table expected"); -luaL_buffinit(L,&b); -ms.L=L; -ms.src_init=src; -ms.src_end=src+srcl; -while(nsrc) -src=e; -else if(src=sizeof("-+ #0")) -luaL_error(L,"invalid format (repeated flags)"); -if(isdigit(uchar(*p)))p++; -if(isdigit(uchar(*p)))p++; -if(*p=='.'){ -p++; -if(isdigit(uchar(*p)))p++; -if(isdigit(uchar(*p)))p++; -} -if(isdigit(uchar(*p))) -luaL_error(L,"invalid format (width or precision too long)"); -*(form++)='%'; -strncpy(form,strfrmt,p-strfrmt+1); -form+=p-strfrmt+1; -*form='\0'; -return p; -} -static void addintlen(char*form){ -size_t l=strlen(form); -char spec=form[l-1]; -strcpy(form+l-1,"l"); -form[l+sizeof("l")-2]=spec; -form[l+sizeof("l")-1]='\0'; -} -static int str_format(lua_State*L){ -int top=lua_gettop(L); -int arg=1; -size_t sfl; -const char*strfrmt=luaL_checklstring(L,arg,&sfl); -const char*strfrmt_end=strfrmt+sfl; -luaL_Buffer b; -luaL_buffinit(L,&b); -while(strfrmttop) -luaL_argerror(L,arg,"no value"); -strfrmt=scanformat(L,strfrmt,form); -switch(*strfrmt++){ -case'c':{ -sprintf(buff,form,(int)luaL_checknumber(L,arg)); -break; -} -case'd':case'i':{ -addintlen(form); -sprintf(buff,form,(long)luaL_checknumber(L,arg)); -break; -} -case'o':case'u':case'x':case'X':{ -addintlen(form); -sprintf(buff,form,(unsigned long)luaL_checknumber(L,arg)); -break; -} -case'e':case'E':case'f': -case'g':case'G':{ -sprintf(buff,form,(double)luaL_checknumber(L,arg)); -break; -} -case'q':{ -addquoted(L,&b,arg); -continue; -} -case's':{ -size_t l; -const char*s=luaL_checklstring(L,arg,&l); -if(!strchr(form,'.')&&l>=100){ -lua_pushvalue(L,arg); -luaL_addvalue(&b); -continue; -} -else{ -sprintf(buff,form,s); -break; -} -} -default:{ -return luaL_error(L,"invalid option "LUA_QL("%%%c")" to " -LUA_QL("format"),*(strfrmt-1)); -} -} -luaL_addlstring(&b,buff,strlen(buff)); -} -} -luaL_pushresult(&b); -return 1; -} -static const luaL_Reg strlib[]={ -{"byte",str_byte}, -{"char",str_char}, -{"find",str_find}, -{"format",str_format}, -{"gmatch",gmatch}, -{"gsub",str_gsub}, -{"lower",str_lower}, -{"match",str_match}, -{"rep",str_rep}, -{"sub",str_sub}, -{"upper",str_upper}, -{NULL,NULL} -}; -static void createmetatable(lua_State*L){ -lua_createtable(L,0,1); -lua_pushliteral(L,""); -lua_pushvalue(L,-2); -lua_setmetatable(L,-2); -lua_pop(L,1); -lua_pushvalue(L,-2); -lua_setfield(L,-2,"__index"); -lua_pop(L,1); -} -static int luaopen_string(lua_State*L){ -luaL_register(L,"string",strlib); -createmetatable(L); -return 1; -} -static const luaL_Reg lualibs[]={ -{"",luaopen_base}, -{"table",luaopen_table}, -{"io",luaopen_io}, -{"os",luaopen_os}, -{"string",luaopen_string}, -{NULL,NULL} -}; -static void luaL_openlibs(lua_State*L){ -const luaL_Reg*lib=lualibs; -for(;lib->func;lib++){ -lua_pushcfunction(L,lib->func); -lua_pushstring(L,lib->name); -lua_call(L,1,0); -} -} -typedef unsigned int UB; -static UB barg(lua_State*L,int idx){ -union{lua_Number n;U64 b;}bn; -bn.n=lua_tonumber(L,idx)+6755399441055744.0; -if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); -return(UB)bn.b; -} -#define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1; -static int tobit(lua_State*L){ -BRET(barg(L,1))} -static int bnot(lua_State*L){ -BRET(~barg(L,1))} -static int band(lua_State*L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} -static int bor(lua_State*L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} -static int bxor(lua_State*L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} -static int lshift(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET(b<>n)} -static int arshift(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} -static int rol(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b<>(32-n)))} -static int ror(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} -static int bswap(lua_State*L){ -UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} -static int tohex(lua_State*L){ -UB b=barg(L,1); -int n=lua_isnone(L,2)?8:(int)barg(L,2); -const char*hexdigits="0123456789abcdef"; -char buf[8]; -int i; -if(n<0){n=-n;hexdigits="0123456789ABCDEF";} -if(n>8)n=8; -for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} -lua_pushlstring(L,buf,(size_t)n); -return 1; -} -static const struct luaL_Reg bitlib[]={ -{"tobit",tobit}, -{"bnot",bnot}, -{"band",band}, -{"bor",bor}, -{"bxor",bxor}, -{"lshift",lshift}, -{"rshift",rshift}, -{"arshift",arshift}, -{"rol",rol}, -{"ror",ror}, -{"bswap",bswap}, -{"tohex",tohex}, -{NULL,NULL} -}; -int main(int argc,char**argv){ -lua_State*L=luaL_newstate(); -int i; -luaL_openlibs(L); -luaL_register(L,"bit",bitlib); -if(argc<2)return sizeof(void*); -lua_createtable(L,0,1); -lua_pushstring(L,argv[1]); -lua_rawseti(L,-2,0); -lua_setglobal(L,"arg"); -if(luaL_loadfile(L,argv[1])) -goto err; -for(i=2;i -- BYTECODE -- [...] --- print(bc.line(foo, 2)) --> 0002 KSTR 1 1 ; "hello" --- --- local out = { --- -- Do something with each line: --- write = function(t, ...) io.write(...) end, --- close = function(t) end, --- flush = function(t) end, --- } --- bc.dump(foo, out) --- ------------------------------------------------------------------------------- - --- Cache some library functions and objects. -local jit = require("jit") -assert(jit.version_num == 20002, "LuaJIT core/library version mismatch") -local jutil = require("jit.util") -local vmdef = require("jit.vmdef") -local bit = require("bit") -local sub, gsub, format = string.sub, string.gsub, string.format -local byte, band, shr = string.byte, bit.band, bit.rshift -local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck -local funcuvname = jutil.funcuvname -local bcnames = vmdef.bcnames -local stdout, stderr = io.stdout, io.stderr - ------------------------------------------------------------------------------- - -local function ctlsub(c) - if c == "\n" then return "\\n" - elseif c == "\r" then return "\\r" - elseif c == "\t" then return "\\t" - else return format("\\%03d", byte(c)) - end -end - --- Return one bytecode line. -local function bcline(func, pc, prefix) - local ins, m = funcbc(func, pc) - if not ins then return end - local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128) - local a = band(shr(ins, 8), 0xff) - local oidx = 6*band(ins, 0xff) - local op = sub(bcnames, oidx+1, oidx+6) - local s = format("%04d %s %-6s %3s ", - pc, prefix or " ", op, ma == 0 and "" or a) - local d = shr(ins, 16) - if mc == 13*128 then -- BCMjump - return format("%s=> %04d\n", s, pc+d-0x7fff) - end - if mb ~= 0 then - d = band(d, 0xff) - elseif mc == 0 then - return s.."\n" - end - local kc - if mc == 10*128 then -- BCMstr - kc = funck(func, -d-1) - kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub)) - elseif mc == 9*128 then -- BCMnum - kc = funck(func, d) - if op == "TSETM " then kc = kc - 2^52 end - elseif mc == 12*128 then -- BCMfunc - local fi = funcinfo(funck(func, -d-1)) - if fi.ffid then - kc = vmdef.ffnames[fi.ffid] - else - kc = fi.loc - end - elseif mc == 5*128 then -- BCMuv - kc = funcuvname(func, d) - end - if ma == 5 then -- BCMuv - local ka = funcuvname(func, a) - if kc then kc = ka.." ; "..kc else kc = ka end - end - if mb ~= 0 then - local b = shr(ins, 24) - if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end - return format("%s%3d %3d\n", s, b, d) - end - if kc then return format("%s%3d ; %s\n", s, d, kc) end - if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits - return format("%s%3d\n", s, d) -end - --- Collect branch targets of a function. -local function bctargets(func) - local target = {} - for pc=1,1000000000 do - local ins, m = funcbc(func, pc) - if not ins then break end - if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end - end - return target -end - --- Dump bytecode instructions of a function. -local function bcdump(func, out, all) - if not out then out = stdout end - local fi = funcinfo(func) - if all and fi.children then - for n=-1,-1000000000,-1 do - local k = funck(func, n) - if not k then break end - if type(k) == "proto" then bcdump(k, out, true) end - end - end - out:write(format("-- BYTECODE -- %s-%d\n", fi.loc, fi.lastlinedefined)) - local target = bctargets(func) - for pc=1,1000000000 do - local s = bcline(func, pc, target[pc] and "=>") - if not s then break end - out:write(s) - end - out:write("\n") - out:flush() -end - ------------------------------------------------------------------------------- - --- Active flag and output file handle. -local active, out - --- List handler. -local function h_list(func) - return bcdump(func, out) -end - --- Detach list handler. -local function bclistoff() - if active then - active = false - jit.attach(h_list) - if out and out ~= stdout and out ~= stderr then out:close() end - out = nil - end -end - --- Open the output file and attach list handler. -local function bcliston(outfile) - if active then bclistoff() end - if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end - if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) - else - out = stderr - end - jit.attach(h_list, "bc") - active = true -end - --- Public module functions. -module(...) - -line = bcline -dump = bcdump -targets = bctargets - -on = bcliston -off = bclistoff -start = bcliston -- For -j command line option. - diff --git a/third-party/LuaJIT-2.0.2/src/jit/bcsave.lua b/third-party/LuaJIT-2.0.2/src/jit/bcsave.lua deleted file mode 100644 index e6d566e534..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/bcsave.lua +++ /dev/null @@ -1,659 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT module to save/list bytecode. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module saves or lists the bytecode for an input file. --- It's run by the -b command line option. --- ------------------------------------------------------------------------------- - -local jit = require("jit") -assert(jit.version_num == 20002, "LuaJIT core/library version mismatch") -local bit = require("bit") - --- Symbol name prefix for LuaJIT bytecode. -local LJBC_PREFIX = "luaJIT_BC_" - ------------------------------------------------------------------------------- - -local function usage() - io.stderr:write[[ -Save LuaJIT bytecode: luajit -b[options] input output - -l Only list bytecode. - -s Strip debug info (default). - -g Keep debug info. - -n name Set module name (default: auto-detect from input name). - -t type Set output file type (default: auto-detect from output name). - -a arch Override architecture for object files (default: native). - -o os Override OS for object files (default: native). - -e chunk Use chunk string as input. - -- Stop handling options. - - Use stdin as input and/or stdout as output. - -File types: c h obj o raw (default) -]] - os.exit(1) -end - -local function check(ok, ...) - if ok then return ok, ... end - io.stderr:write("luajit: ", ...) - io.stderr:write("\n") - os.exit(1) -end - -local function readfile(input) - if type(input) == "function" then return input end - if input == "-" then input = nil end - return check(loadfile(input)) -end - -local function savefile(name, mode) - if name == "-" then return io.stdout end - return check(io.open(name, mode)) -end - ------------------------------------------------------------------------------- - -local map_type = { - raw = "raw", c = "c", h = "h", o = "obj", obj = "obj", -} - -local map_arch = { - x86 = true, x64 = true, arm = true, ppc = true, ppcspe = true, - mips = true, mipsel = true, -} - -local map_os = { - linux = true, windows = true, osx = true, freebsd = true, netbsd = true, - openbsd = true, solaris = true, -} - -local function checkarg(str, map, err) - str = string.lower(str) - local s = check(map[str], "unknown ", err) - return s == true and str or s -end - -local function detecttype(str) - local ext = string.match(string.lower(str), "%.(%a+)$") - return map_type[ext] or "raw" -end - -local function checkmodname(str) - check(string.match(str, "^[%w_.%-]+$"), "bad module name") - return string.gsub(str, "[%.%-]", "_") -end - -local function detectmodname(str) - if type(str) == "string" then - local tail = string.match(str, "[^/\\]+$") - if tail then str = tail end - local head = string.match(str, "^(.*)%.[^.]*$") - if head then str = head end - str = string.match(str, "^[%w_.%-]+") - else - str = nil - end - check(str, "cannot derive module name, use -n name") - return string.gsub(str, "[%.%-]", "_") -end - ------------------------------------------------------------------------------- - -local function bcsave_tail(fp, output, s) - local ok, err = fp:write(s) - if ok and output ~= "-" then ok, err = fp:close() end - check(ok, "cannot write ", output, ": ", err) -end - -local function bcsave_raw(output, s) - local fp = savefile(output, "wb") - bcsave_tail(fp, output, s) -end - -local function bcsave_c(ctx, output, s) - local fp = savefile(output, "w") - if ctx.type == "c" then - fp:write(string.format([[ -#ifdef _cplusplus -extern "C" -#endif -#ifdef _WIN32 -__declspec(dllexport) -#endif -const char %s%s[] = { -]], LJBC_PREFIX, ctx.modname)) - else - fp:write(string.format([[ -#define %s%s_SIZE %d -static const char %s%s[] = { -]], LJBC_PREFIX, ctx.modname, #s, LJBC_PREFIX, ctx.modname)) - end - local t, n, m = {}, 0, 0 - for i=1,#s do - local b = tostring(string.byte(s, i)) - m = m + #b + 1 - if m > 78 then - fp:write(table.concat(t, ",", 1, n), ",\n") - n, m = 0, #b + 1 - end - n = n + 1 - t[n] = b - end - bcsave_tail(fp, output, table.concat(t, ",", 1, n).."\n};\n") -end - -local function bcsave_elfobj(ctx, output, s, ffi) - ffi.cdef[[ -typedef struct { - uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7]; - uint16_t type, machine; - uint32_t version; - uint32_t entry, phofs, shofs; - uint32_t flags; - uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx; -} ELF32header; -typedef struct { - uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7]; - uint16_t type, machine; - uint32_t version; - uint64_t entry, phofs, shofs; - uint32_t flags; - uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx; -} ELF64header; -typedef struct { - uint32_t name, type, flags, addr, ofs, size, link, info, align, entsize; -} ELF32sectheader; -typedef struct { - uint32_t name, type; - uint64_t flags, addr, ofs, size; - uint32_t link, info; - uint64_t align, entsize; -} ELF64sectheader; -typedef struct { - uint32_t name, value, size; - uint8_t info, other; - uint16_t sectidx; -} ELF32symbol; -typedef struct { - uint32_t name; - uint8_t info, other; - uint16_t sectidx; - uint64_t value, size; -} ELF64symbol; -typedef struct { - ELF32header hdr; - ELF32sectheader sect[6]; - ELF32symbol sym[2]; - uint8_t space[4096]; -} ELF32obj; -typedef struct { - ELF64header hdr; - ELF64sectheader sect[6]; - ELF64symbol sym[2]; - uint8_t space[4096]; -} ELF64obj; -]] - local symname = LJBC_PREFIX..ctx.modname - local is64, isbe = false, false - if ctx.arch == "x64" then - is64 = true - elseif ctx.arch == "ppc" or ctx.arch == "ppcspe" or ctx.arch == "mips" then - isbe = true - end - - -- Handle different host/target endianess. - local function f32(x) return x end - local f16, fofs = f32, f32 - if ffi.abi("be") ~= isbe then - f32 = bit.bswap - function f16(x) return bit.rshift(bit.bswap(x), 16) end - if is64 then - local two32 = ffi.cast("int64_t", 2^32) - function fofs(x) return bit.bswap(x)*two32 end - else - fofs = f32 - end - end - - -- Create ELF object and fill in header. - local o = ffi.new(is64 and "ELF64obj" or "ELF32obj") - local hdr = o.hdr - if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi. - local bf = assert(io.open("/bin/ls", "rb")) - local bs = bf:read(9) - bf:close() - ffi.copy(o, bs, 9) - check(hdr.emagic[0] == 127, "no support for writing native object files") - else - hdr.emagic = "\127ELF" - hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0 - end - hdr.eclass = is64 and 2 or 1 - hdr.eendian = isbe and 2 or 1 - hdr.eversion = 1 - hdr.type = f16(1) - hdr.machine = f16(({ x86=3, x64=62, arm=40, ppc=20, ppcspe=20, mips=8, mipsel=8 })[ctx.arch]) - if ctx.arch == "mips" or ctx.arch == "mipsel" then - hdr.flags = 0x50001006 - end - hdr.version = f32(1) - hdr.shofs = fofs(ffi.offsetof(o, "sect")) - hdr.ehsize = f16(ffi.sizeof(hdr)) - hdr.shentsize = f16(ffi.sizeof(o.sect[0])) - hdr.shnum = f16(6) - hdr.shstridx = f16(2) - - -- Fill in sections and symbols. - local sofs, ofs = ffi.offsetof(o, "space"), 1 - for i,name in ipairs{ - ".symtab", ".shstrtab", ".strtab", ".rodata", ".note.GNU-stack", - } do - local sect = o.sect[i] - sect.align = fofs(1) - sect.name = f32(ofs) - ffi.copy(o.space+ofs, name) - ofs = ofs + #name+1 - end - o.sect[1].type = f32(2) -- .symtab - o.sect[1].link = f32(3) - o.sect[1].info = f32(1) - o.sect[1].align = fofs(8) - o.sect[1].ofs = fofs(ffi.offsetof(o, "sym")) - o.sect[1].entsize = fofs(ffi.sizeof(o.sym[0])) - o.sect[1].size = fofs(ffi.sizeof(o.sym)) - o.sym[1].name = f32(1) - o.sym[1].sectidx = f16(4) - o.sym[1].size = fofs(#s) - o.sym[1].info = 17 - o.sect[2].type = f32(3) -- .shstrtab - o.sect[2].ofs = fofs(sofs) - o.sect[2].size = fofs(ofs) - o.sect[3].type = f32(3) -- .strtab - o.sect[3].ofs = fofs(sofs + ofs) - o.sect[3].size = fofs(#symname+1) - ffi.copy(o.space+ofs+1, symname) - ofs = ofs + #symname + 2 - o.sect[4].type = f32(1) -- .rodata - o.sect[4].flags = fofs(2) - o.sect[4].ofs = fofs(sofs + ofs) - o.sect[4].size = fofs(#s) - o.sect[5].type = f32(1) -- .note.GNU-stack - o.sect[5].ofs = fofs(sofs + ofs + #s) - - -- Write ELF object file. - local fp = savefile(output, "wb") - fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs)) - bcsave_tail(fp, output, s) -end - -local function bcsave_peobj(ctx, output, s, ffi) - ffi.cdef[[ -typedef struct { - uint16_t arch, nsects; - uint32_t time, symtabofs, nsyms; - uint16_t opthdrsz, flags; -} PEheader; -typedef struct { - char name[8]; - uint32_t vsize, vaddr, size, ofs, relocofs, lineofs; - uint16_t nreloc, nline; - uint32_t flags; -} PEsection; -typedef struct __attribute((packed)) { - union { - char name[8]; - uint32_t nameref[2]; - }; - uint32_t value; - int16_t sect; - uint16_t type; - uint8_t scl, naux; -} PEsym; -typedef struct __attribute((packed)) { - uint32_t size; - uint16_t nreloc, nline; - uint32_t cksum; - uint16_t assoc; - uint8_t comdatsel, unused[3]; -} PEsymaux; -typedef struct { - PEheader hdr; - PEsection sect[2]; - // Must be an even number of symbol structs. - PEsym sym0; - PEsymaux sym0aux; - PEsym sym1; - PEsymaux sym1aux; - PEsym sym2; - PEsym sym3; - uint32_t strtabsize; - uint8_t space[4096]; -} PEobj; -]] - local symname = LJBC_PREFIX..ctx.modname - local is64 = false - if ctx.arch == "x86" then - symname = "_"..symname - elseif ctx.arch == "x64" then - is64 = true - end - local symexport = " /EXPORT:"..symname..",DATA " - - -- The file format is always little-endian. Swap if the host is big-endian. - local function f32(x) return x end - local f16 = f32 - if ffi.abi("be") then - f32 = bit.bswap - function f16(x) return bit.rshift(bit.bswap(x), 16) end - end - - -- Create PE object and fill in header. - local o = ffi.new("PEobj") - local hdr = o.hdr - hdr.arch = f16(({ x86=0x14c, x64=0x8664, arm=0x1c0, ppc=0x1f2, mips=0x366, mipsel=0x366 })[ctx.arch]) - hdr.nsects = f16(2) - hdr.symtabofs = f32(ffi.offsetof(o, "sym0")) - hdr.nsyms = f32(6) - - -- Fill in sections and symbols. - o.sect[0].name = ".drectve" - o.sect[0].size = f32(#symexport) - o.sect[0].flags = f32(0x00100a00) - o.sym0.sect = f16(1) - o.sym0.scl = 3 - o.sym0.name = ".drectve" - o.sym0.naux = 1 - o.sym0aux.size = f32(#symexport) - o.sect[1].name = ".rdata" - o.sect[1].size = f32(#s) - o.sect[1].flags = f32(0x40300040) - o.sym1.sect = f16(2) - o.sym1.scl = 3 - o.sym1.name = ".rdata" - o.sym1.naux = 1 - o.sym1aux.size = f32(#s) - o.sym2.sect = f16(2) - o.sym2.scl = 2 - o.sym2.nameref[1] = f32(4) - o.sym3.sect = f16(-1) - o.sym3.scl = 2 - o.sym3.value = f32(1) - o.sym3.name = "@feat.00" -- Mark as SafeSEH compliant. - ffi.copy(o.space, symname) - local ofs = #symname + 1 - o.strtabsize = f32(ofs + 4) - o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs) - ffi.copy(o.space + ofs, symexport) - ofs = ofs + #symexport - o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs) - - -- Write PE object file. - local fp = savefile(output, "wb") - fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs)) - bcsave_tail(fp, output, s) -end - -local function bcsave_machobj(ctx, output, s, ffi) - ffi.cdef[[ -typedef struct -{ - uint32_t magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags; -} mach_header; -typedef struct -{ - mach_header; uint32_t reserved; -} mach_header_64; -typedef struct { - uint32_t cmd, cmdsize; - char segname[16]; - uint32_t vmaddr, vmsize, fileoff, filesize; - uint32_t maxprot, initprot, nsects, flags; -} mach_segment_command; -typedef struct { - uint32_t cmd, cmdsize; - char segname[16]; - uint64_t vmaddr, vmsize, fileoff, filesize; - uint32_t maxprot, initprot, nsects, flags; -} mach_segment_command_64; -typedef struct { - char sectname[16], segname[16]; - uint32_t addr, size; - uint32_t offset, align, reloff, nreloc, flags; - uint32_t reserved1, reserved2; -} mach_section; -typedef struct { - char sectname[16], segname[16]; - uint64_t addr, size; - uint32_t offset, align, reloff, nreloc, flags; - uint32_t reserved1, reserved2, reserved3; -} mach_section_64; -typedef struct { - uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize; -} mach_symtab_command; -typedef struct { - int32_t strx; - uint8_t type, sect; - int16_t desc; - uint32_t value; -} mach_nlist; -typedef struct { - uint32_t strx; - uint8_t type, sect; - uint16_t desc; - uint64_t value; -} mach_nlist_64; -typedef struct -{ - uint32_t magic, nfat_arch; -} mach_fat_header; -typedef struct -{ - uint32_t cputype, cpusubtype, offset, size, align; -} mach_fat_arch; -typedef struct { - struct { - mach_header hdr; - mach_segment_command seg; - mach_section sec; - mach_symtab_command sym; - } arch[1]; - mach_nlist sym_entry; - uint8_t space[4096]; -} mach_obj; -typedef struct { - struct { - mach_header_64 hdr; - mach_segment_command_64 seg; - mach_section_64 sec; - mach_symtab_command sym; - } arch[1]; - mach_nlist_64 sym_entry; - uint8_t space[4096]; -} mach_obj_64; -typedef struct { - mach_fat_header fat; - mach_fat_arch fat_arch[4]; - struct { - mach_header hdr; - mach_segment_command seg; - mach_section sec; - mach_symtab_command sym; - } arch[4]; - mach_nlist sym_entry; - uint8_t space[4096]; -} mach_fat_obj; -]] - local symname = '_'..LJBC_PREFIX..ctx.modname - local isfat, is64, align, mobj = false, false, 4, "mach_obj" - if ctx.arch == "x64" then - is64, align, mobj = true, 8, "mach_obj_64" - elseif ctx.arch == "arm" then - isfat, mobj = true, "mach_fat_obj" - else - check(ctx.arch == "x86", "unsupported architecture for OSX") - end - local function aligned(v, a) return bit.band(v+a-1, -a) end - local be32 = bit.bswap -- Mach-O FAT is BE, supported archs are LE. - - -- Create Mach-O object and fill in header. - local o = ffi.new(mobj) - local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, align) - local cputype = ({ x86={7}, x64={0x01000007}, arm={7,12,12,12} })[ctx.arch] - local cpusubtype = ({ x86={3}, x64={3}, arm={3,6,9,11} })[ctx.arch] - if isfat then - o.fat.magic = be32(0xcafebabe) - o.fat.nfat_arch = be32(#cpusubtype) - end - - -- Fill in sections and symbols. - for i=0,#cpusubtype-1 do - local ofs = 0 - if isfat then - local a = o.fat_arch[i] - a.cputype = be32(cputype[i+1]) - a.cpusubtype = be32(cpusubtype[i+1]) - -- Subsequent slices overlap each other to share data. - ofs = ffi.offsetof(o, "arch") + i*ffi.sizeof(o.arch[0]) - a.offset = be32(ofs) - a.size = be32(mach_size-ofs+#s) - end - local a = o.arch[i] - a.hdr.magic = is64 and 0xfeedfacf or 0xfeedface - a.hdr.cputype = cputype[i+1] - a.hdr.cpusubtype = cpusubtype[i+1] - a.hdr.filetype = 1 - a.hdr.ncmds = 2 - a.hdr.sizeofcmds = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)+ffi.sizeof(a.sym) - a.seg.cmd = is64 and 0x19 or 0x1 - a.seg.cmdsize = ffi.sizeof(a.seg)+ffi.sizeof(a.sec) - a.seg.vmsize = #s - a.seg.fileoff = mach_size-ofs - a.seg.filesize = #s - a.seg.maxprot = 1 - a.seg.initprot = 1 - a.seg.nsects = 1 - ffi.copy(a.sec.sectname, "__data") - ffi.copy(a.sec.segname, "__DATA") - a.sec.size = #s - a.sec.offset = mach_size-ofs - a.sym.cmd = 2 - a.sym.cmdsize = ffi.sizeof(a.sym) - a.sym.symoff = ffi.offsetof(o, "sym_entry")-ofs - a.sym.nsyms = 1 - a.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)-ofs - a.sym.strsize = aligned(#symname+2, align) - end - o.sym_entry.type = 0xf - o.sym_entry.sect = 1 - o.sym_entry.strx = 1 - ffi.copy(o.space+1, symname) - - -- Write Macho-O object file. - local fp = savefile(output, "wb") - fp:write(ffi.string(o, mach_size)) - bcsave_tail(fp, output, s) -end - -local function bcsave_obj(ctx, output, s) - local ok, ffi = pcall(require, "ffi") - check(ok, "FFI library required to write this file type") - if ctx.os == "windows" then - return bcsave_peobj(ctx, output, s, ffi) - elseif ctx.os == "osx" then - return bcsave_machobj(ctx, output, s, ffi) - else - return bcsave_elfobj(ctx, output, s, ffi) - end -end - ------------------------------------------------------------------------------- - -local function bclist(input, output) - local f = readfile(input) - require("jit.bc").dump(f, savefile(output, "w"), true) -end - -local function bcsave(ctx, input, output) - local f = readfile(input) - local s = string.dump(f, ctx.strip) - local t = ctx.type - if not t then - t = detecttype(output) - ctx.type = t - end - if t == "raw" then - bcsave_raw(output, s) - else - if not ctx.modname then ctx.modname = detectmodname(input) end - if t == "obj" then - bcsave_obj(ctx, output, s) - else - bcsave_c(ctx, output, s) - end - end -end - -local function docmd(...) - local arg = {...} - local n = 1 - local list = false - local ctx = { - strip = true, arch = jit.arch, os = string.lower(jit.os), - type = false, modname = false, - } - while n <= #arg do - local a = arg[n] - if type(a) == "string" and string.sub(a, 1, 1) == "-" and a ~= "-" then - table.remove(arg, n) - if a == "--" then break end - for m=2,#a do - local opt = string.sub(a, m, m) - if opt == "l" then - list = true - elseif opt == "s" then - ctx.strip = true - elseif opt == "g" then - ctx.strip = false - else - if arg[n] == nil or m ~= #a then usage() end - if opt == "e" then - if n ~= 1 then usage() end - arg[1] = check(loadstring(arg[1])) - elseif opt == "n" then - ctx.modname = checkmodname(table.remove(arg, n)) - elseif opt == "t" then - ctx.type = checkarg(table.remove(arg, n), map_type, "file type") - elseif opt == "a" then - ctx.arch = checkarg(table.remove(arg, n), map_arch, "architecture") - elseif opt == "o" then - ctx.os = checkarg(table.remove(arg, n), map_os, "OS name") - else - usage() - end - end - end - else - n = n + 1 - end - end - if list then - if #arg == 0 or #arg > 2 then usage() end - bclist(arg[1], arg[2] or "-") - else - if #arg ~= 2 then usage() end - bcsave(ctx, arg[1], arg[2]) - end -end - ------------------------------------------------------------------------------- - --- Public module functions. -module(...) - -start = docmd -- Process -b command line option. - diff --git a/third-party/LuaJIT-2.0.2/src/jit/dis_arm.lua b/third-party/LuaJIT-2.0.2/src/jit/dis_arm.lua deleted file mode 100644 index dc7ca71ff4..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/dis_arm.lua +++ /dev/null @@ -1,689 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT ARM disassembler module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- It disassembles most user-mode ARMv7 instructions --- NYI: Advanced SIMD and VFP instructions. ------------------------------------------------------------------------------- - -local type = type -local sub, byte, format = string.sub, string.byte, string.format -local match, gmatch, gsub = string.match, string.gmatch, string.gsub -local concat = table.concat -local bit = require("bit") -local band, bor, ror, tohex = bit.band, bit.bor, bit.ror, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift - ------------------------------------------------------------------------------- --- Opcode maps ------------------------------------------------------------------------------- - -local map_loadc = { - shift = 8, mask = 15, - [10] = { - shift = 20, mask = 1, - [0] = { - shift = 23, mask = 3, - [0] = "vmovFmDN", "vstmFNdr", - _ = { - shift = 21, mask = 1, - [0] = "vstrFdl", - { shift = 16, mask = 15, [13] = "vpushFdr", _ = "vstmdbFNdr", } - }, - }, - { - shift = 23, mask = 3, - [0] = "vmovFDNm", - { shift = 16, mask = 15, [13] = "vpopFdr", _ = "vldmFNdr", }, - _ = { - shift = 21, mask = 1, - [0] = "vldrFdl", "vldmdbFNdr", - }, - }, - }, - [11] = { - shift = 20, mask = 1, - [0] = { - shift = 23, mask = 3, - [0] = "vmovGmDN", "vstmGNdr", - _ = { - shift = 21, mask = 1, - [0] = "vstrGdl", - { shift = 16, mask = 15, [13] = "vpushGdr", _ = "vstmdbGNdr", } - }, - }, - { - shift = 23, mask = 3, - [0] = "vmovGDNm", - { shift = 16, mask = 15, [13] = "vpopGdr", _ = "vldmGNdr", }, - _ = { - shift = 21, mask = 1, - [0] = "vldrGdl", "vldmdbGNdr", - }, - }, - }, - _ = { - shift = 0, mask = 0 -- NYI ldc, mcrr, mrrc. - }, -} - -local map_vfps = { - shift = 6, mask = 0x2c001, - [0] = "vmlaF.dnm", "vmlsF.dnm", - [0x04000] = "vnmlsF.dnm", [0x04001] = "vnmlaF.dnm", - [0x08000] = "vmulF.dnm", [0x08001] = "vnmulF.dnm", - [0x0c000] = "vaddF.dnm", [0x0c001] = "vsubF.dnm", - [0x20000] = "vdivF.dnm", - [0x24000] = "vfnmsF.dnm", [0x24001] = "vfnmaF.dnm", - [0x28000] = "vfmaF.dnm", [0x28001] = "vfmsF.dnm", - [0x2c000] = "vmovF.dY", - [0x2c001] = { - shift = 7, mask = 0x1e01, - [0] = "vmovF.dm", "vabsF.dm", - [0x0200] = "vnegF.dm", [0x0201] = "vsqrtF.dm", - [0x0800] = "vcmpF.dm", [0x0801] = "vcmpeF.dm", - [0x0a00] = "vcmpzF.d", [0x0a01] = "vcmpzeF.d", - [0x0e01] = "vcvtG.dF.m", - [0x1000] = "vcvt.f32.u32Fdm", [0x1001] = "vcvt.f32.s32Fdm", - [0x1800] = "vcvtr.u32F.dm", [0x1801] = "vcvt.u32F.dm", - [0x1a00] = "vcvtr.s32F.dm", [0x1a01] = "vcvt.s32F.dm", - }, -} - -local map_vfpd = { - shift = 6, mask = 0x2c001, - [0] = "vmlaG.dnm", "vmlsG.dnm", - [0x04000] = "vnmlsG.dnm", [0x04001] = "vnmlaG.dnm", - [0x08000] = "vmulG.dnm", [0x08001] = "vnmulG.dnm", - [0x0c000] = "vaddG.dnm", [0x0c001] = "vsubG.dnm", - [0x20000] = "vdivG.dnm", - [0x24000] = "vfnmsG.dnm", [0x24001] = "vfnmaG.dnm", - [0x28000] = "vfmaG.dnm", [0x28001] = "vfmsG.dnm", - [0x2c000] = "vmovG.dY", - [0x2c001] = { - shift = 7, mask = 0x1e01, - [0] = "vmovG.dm", "vabsG.dm", - [0x0200] = "vnegG.dm", [0x0201] = "vsqrtG.dm", - [0x0800] = "vcmpG.dm", [0x0801] = "vcmpeG.dm", - [0x0a00] = "vcmpzG.d", [0x0a01] = "vcmpzeG.d", - [0x0e01] = "vcvtF.dG.m", - [0x1000] = "vcvt.f64.u32GdFm", [0x1001] = "vcvt.f64.s32GdFm", - [0x1800] = "vcvtr.u32FdG.m", [0x1801] = "vcvt.u32FdG.m", - [0x1a00] = "vcvtr.s32FdG.m", [0x1a01] = "vcvt.s32FdG.m", - }, -} - -local map_datac = { - shift = 24, mask = 1, - [0] = { - shift = 4, mask = 1, - [0] = { - shift = 8, mask = 15, - [10] = map_vfps, - [11] = map_vfpd, - -- NYI cdp, mcr, mrc. - }, - { - shift = 8, mask = 15, - [10] = { - shift = 20, mask = 15, - [0] = "vmovFnD", "vmovFDn", - [14] = "vmsrD", - [15] = { shift = 12, mask = 15, [15] = "vmrs", _ = "vmrsD", }, - }, - }, - }, - "svcT", -} - -local map_loadcu = { - shift = 0, mask = 0, -- NYI unconditional CP load/store. -} - -local map_datacu = { - shift = 0, mask = 0, -- NYI unconditional CP data. -} - -local map_simddata = { - shift = 0, mask = 0, -- NYI SIMD data. -} - -local map_simdload = { - shift = 0, mask = 0, -- NYI SIMD load/store, preload. -} - -local map_preload = { - shift = 0, mask = 0, -- NYI preload. -} - -local map_media = { - shift = 20, mask = 31, - [0] = false, - { --01 - shift = 5, mask = 7, - [0] = "sadd16DNM", "sasxDNM", "ssaxDNM", "ssub16DNM", - "sadd8DNM", false, false, "ssub8DNM", - }, - { --02 - shift = 5, mask = 7, - [0] = "qadd16DNM", "qasxDNM", "qsaxDNM", "qsub16DNM", - "qadd8DNM", false, false, "qsub8DNM", - }, - { --03 - shift = 5, mask = 7, - [0] = "shadd16DNM", "shasxDNM", "shsaxDNM", "shsub16DNM", - "shadd8DNM", false, false, "shsub8DNM", - }, - false, - { --05 - shift = 5, mask = 7, - [0] = "uadd16DNM", "uasxDNM", "usaxDNM", "usub16DNM", - "uadd8DNM", false, false, "usub8DNM", - }, - { --06 - shift = 5, mask = 7, - [0] = "uqadd16DNM", "uqasxDNM", "uqsaxDNM", "uqsub16DNM", - "uqadd8DNM", false, false, "uqsub8DNM", - }, - { --07 - shift = 5, mask = 7, - [0] = "uhadd16DNM", "uhasxDNM", "uhsaxDNM", "uhsub16DNM", - "uhadd8DNM", false, false, "uhsub8DNM", - }, - { --08 - shift = 5, mask = 7, - [0] = "pkhbtDNMU", false, "pkhtbDNMU", - { shift = 16, mask = 15, [15] = "sxtb16DMU", _ = "sxtab16DNMU", }, - "pkhbtDNMU", "selDNM", "pkhtbDNMU", - }, - false, - { --0a - shift = 5, mask = 7, - [0] = "ssatDxMu", "ssat16DxM", "ssatDxMu", - { shift = 16, mask = 15, [15] = "sxtbDMU", _ = "sxtabDNMU", }, - "ssatDxMu", false, "ssatDxMu", - }, - { --0b - shift = 5, mask = 7, - [0] = "ssatDxMu", "revDM", "ssatDxMu", - { shift = 16, mask = 15, [15] = "sxthDMU", _ = "sxtahDNMU", }, - "ssatDxMu", "rev16DM", "ssatDxMu", - }, - { --0c - shift = 5, mask = 7, - [3] = { shift = 16, mask = 15, [15] = "uxtb16DMU", _ = "uxtab16DNMU", }, - }, - false, - { --0e - shift = 5, mask = 7, - [0] = "usatDwMu", "usat16DwM", "usatDwMu", - { shift = 16, mask = 15, [15] = "uxtbDMU", _ = "uxtabDNMU", }, - "usatDwMu", false, "usatDwMu", - }, - { --0f - shift = 5, mask = 7, - [0] = "usatDwMu", "rbitDM", "usatDwMu", - { shift = 16, mask = 15, [15] = "uxthDMU", _ = "uxtahDNMU", }, - "usatDwMu", "revshDM", "usatDwMu", - }, - { --10 - shift = 12, mask = 15, - [15] = { - shift = 5, mask = 7, - "smuadNMS", "smuadxNMS", "smusdNMS", "smusdxNMS", - }, - _ = { - shift = 5, mask = 7, - [0] = "smladNMSD", "smladxNMSD", "smlsdNMSD", "smlsdxNMSD", - }, - }, - false, false, false, - { --14 - shift = 5, mask = 7, - [0] = "smlaldDNMS", "smlaldxDNMS", "smlsldDNMS", "smlsldxDNMS", - }, - { --15 - shift = 5, mask = 7, - [0] = { shift = 12, mask = 15, [15] = "smmulNMS", _ = "smmlaNMSD", }, - { shift = 12, mask = 15, [15] = "smmulrNMS", _ = "smmlarNMSD", }, - false, false, false, false, - "smmlsNMSD", "smmlsrNMSD", - }, - false, false, - { --18 - shift = 5, mask = 7, - [0] = { shift = 12, mask = 15, [15] = "usad8NMS", _ = "usada8NMSD", }, - }, - false, - { --1a - shift = 5, mask = 3, [2] = "sbfxDMvw", - }, - { --1b - shift = 5, mask = 3, [2] = "sbfxDMvw", - }, - { --1c - shift = 5, mask = 3, - [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", }, - }, - { --1d - shift = 5, mask = 3, - [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", }, - }, - { --1e - shift = 5, mask = 3, [2] = "ubfxDMvw", - }, - { --1f - shift = 5, mask = 3, [2] = "ubfxDMvw", - }, -} - -local map_load = { - shift = 21, mask = 9, - { - shift = 20, mask = 5, - [0] = "strtDL", "ldrtDL", [4] = "strbtDL", [5] = "ldrbtDL", - }, - _ = { - shift = 20, mask = 5, - [0] = "strDL", "ldrDL", [4] = "strbDL", [5] = "ldrbDL", - } -} - -local map_load1 = { - shift = 4, mask = 1, - [0] = map_load, map_media, -} - -local map_loadm = { - shift = 20, mask = 1, - [0] = { - shift = 23, mask = 3, - [0] = "stmdaNR", "stmNR", - { shift = 16, mask = 63, [45] = "pushR", _ = "stmdbNR", }, "stmibNR", - }, - { - shift = 23, mask = 3, - [0] = "ldmdaNR", { shift = 16, mask = 63, [61] = "popR", _ = "ldmNR", }, - "ldmdbNR", "ldmibNR", - }, -} - -local map_data = { - shift = 21, mask = 15, - [0] = "andDNPs", "eorDNPs", "subDNPs", "rsbDNPs", - "addDNPs", "adcDNPs", "sbcDNPs", "rscDNPs", - "tstNP", "teqNP", "cmpNP", "cmnNP", - "orrDNPs", "movDPs", "bicDNPs", "mvnDPs", -} - -local map_mul = { - shift = 21, mask = 7, - [0] = "mulNMSs", "mlaNMSDs", "umaalDNMS", "mlsDNMS", - "umullDNMSs", "umlalDNMSs", "smullDNMSs", "smlalDNMSs", -} - -local map_sync = { - shift = 20, mask = 15, -- NYI: brackets around N. R(D+1) for ldrexd/strexd. - [0] = "swpDMN", false, false, false, - "swpbDMN", false, false, false, - "strexDMN", "ldrexDN", "strexdDN", "ldrexdDN", - "strexbDMN", "ldrexbDN", "strexhDN", "ldrexhDN", -} - -local map_mulh = { - shift = 21, mask = 3, - [0] = { shift = 5, mask = 3, - [0] = "smlabbNMSD", "smlatbNMSD", "smlabtNMSD", "smlattNMSD", }, - { shift = 5, mask = 3, - [0] = "smlawbNMSD", "smulwbNMS", "smlawtNMSD", "smulwtNMS", }, - { shift = 5, mask = 3, - [0] = "smlalbbDNMS", "smlaltbDNMS", "smlalbtDNMS", "smlalttDNMS", }, - { shift = 5, mask = 3, - [0] = "smulbbNMS", "smultbNMS", "smulbtNMS", "smulttNMS", }, -} - -local map_misc = { - shift = 4, mask = 7, - -- NYI: decode PSR bits of msr. - [0] = { shift = 21, mask = 1, [0] = "mrsD", "msrM", }, - { shift = 21, mask = 3, "bxM", false, "clzDM", }, - { shift = 21, mask = 3, "bxjM", }, - { shift = 21, mask = 3, "blxM", }, - false, - { shift = 21, mask = 3, [0] = "qaddDMN", "qsubDMN", "qdaddDMN", "qdsubDMN", }, - false, - { shift = 21, mask = 3, "bkptK", }, -} - -local map_datar = { - shift = 4, mask = 9, - [9] = { - shift = 5, mask = 3, - [0] = { shift = 24, mask = 1, [0] = map_mul, map_sync, }, - { shift = 20, mask = 1, [0] = "strhDL", "ldrhDL", }, - { shift = 20, mask = 1, [0] = "ldrdDL", "ldrsbDL", }, - { shift = 20, mask = 1, [0] = "strdDL", "ldrshDL", }, - }, - _ = { - shift = 20, mask = 25, - [16] = { shift = 7, mask = 1, [0] = map_misc, map_mulh, }, - _ = { - shift = 0, mask = 0xffffffff, - [bor(0xe1a00000)] = "nop", - _ = map_data, - } - }, -} - -local map_datai = { - shift = 20, mask = 31, -- NYI: decode PSR bits of msr. Decode imm12. - [16] = "movwDW", [20] = "movtDW", - [18] = { shift = 0, mask = 0xf00ff, [0] = "nopv6", _ = "msrNW", }, - [22] = "msrNW", - _ = map_data, -} - -local map_branch = { - shift = 24, mask = 1, - [0] = "bB", "blB" -} - -local map_condins = { - [0] = map_datar, map_datai, map_load, map_load1, - map_loadm, map_branch, map_loadc, map_datac -} - --- NYI: setend. -local map_uncondins = { - [0] = false, map_simddata, map_simdload, map_preload, - false, "blxB", map_loadcu, map_datacu, -} - ------------------------------------------------------------------------------- - -local map_gpr = { - [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", - "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc", -} - -local map_cond = { - [0] = "eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc", - "hi", "ls", "ge", "lt", "gt", "le", "al", -} - -local map_shift = { [0] = "lsl", "lsr", "asr", "ror", } - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local pos = ctx.pos - local extra = "" - if ctx.rel then - local sym = ctx.symtab[ctx.rel] - if sym then - extra = "\t->"..sym - elseif band(ctx.op, 0x0e000000) ~= 0x0a000000 then - extra = "\t; 0x"..tohex(ctx.rel) - end - end - if ctx.hexdump > 0 then - ctx.out(format("%08x %s %-5s %s%s\n", - ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) - else - ctx.out(format("%08x %-5s %s%s\n", - ctx.addr+pos, text, concat(operands, ", "), extra)) - end - ctx.pos = pos + 4 -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) -end - --- Format operand 2 of load/store opcodes. -local function fmtload(ctx, op, pos) - local base = map_gpr[band(rshift(op, 16), 15)] - local x, ofs - local ext = (band(op, 0x04000000) == 0) - if not ext and band(op, 0x02000000) == 0 then - ofs = band(op, 4095) - if band(op, 0x00800000) == 0 then ofs = -ofs end - if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end - ofs = "#"..ofs - elseif ext and band(op, 0x00400000) ~= 0 then - ofs = band(op, 15) + band(rshift(op, 4), 0xf0) - if band(op, 0x00800000) == 0 then ofs = -ofs end - if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end - ofs = "#"..ofs - else - ofs = map_gpr[band(op, 15)] - if ext or band(op, 0xfe0) == 0 then - elseif band(op, 0xfe0) == 0x60 then - ofs = format("%s, rrx", ofs) - else - local sh = band(rshift(op, 7), 31) - if sh == 0 then sh = 32 end - ofs = format("%s, %s #%d", ofs, map_shift[band(rshift(op, 5), 3)], sh) - end - if band(op, 0x00800000) == 0 then ofs = "-"..ofs end - end - if ofs == "#0" then - x = format("[%s]", base) - elseif band(op, 0x01000000) == 0 then - x = format("[%s], %s", base, ofs) - else - x = format("[%s, %s]", base, ofs) - end - if band(op, 0x01200000) == 0x01200000 then x = x.."!" end - return x -end - --- Format operand 2 of vector load/store opcodes. -local function fmtvload(ctx, op, pos) - local base = map_gpr[band(rshift(op, 16), 15)] - local ofs = band(op, 255)*4 - if band(op, 0x00800000) == 0 then ofs = -ofs end - if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end - if ofs == 0 then - return format("[%s]", base) - else - return format("[%s, #%d]", base, ofs) - end -end - -local function fmtvr(op, vr, sh0, sh1) - if vr == "s" then - return format("s%d", 2*band(rshift(op, sh0), 15)+band(rshift(op, sh1), 1)) - else - return format("d%d", band(rshift(op, sh0), 15)+band(rshift(op, sh1-4), 16)) - end -end - --- Disassemble a single instruction. -local function disass_ins(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) - local operands = {} - local suffix = "" - local last, name, pat - local vr - ctx.op = op - ctx.rel = nil - - local cond = rshift(op, 28) - local opat - if cond == 15 then - opat = map_uncondins[band(rshift(op, 25), 7)] - else - if cond ~= 14 then suffix = map_cond[cond] end - opat = map_condins[band(rshift(op, 25), 7)] - end - while type(opat) ~= "string" do - if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ - end - name, pat = match(opat, "^([a-z0-9]*)(.*)") - if sub(pat, 1, 1) == "." then - local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") - suffix = suffix..s2 - pat = p2 - end - - for p in gmatch(pat, ".") do - local x = nil - if p == "D" then - x = map_gpr[band(rshift(op, 12), 15)] - elseif p == "N" then - x = map_gpr[band(rshift(op, 16), 15)] - elseif p == "S" then - x = map_gpr[band(rshift(op, 8), 15)] - elseif p == "M" then - x = map_gpr[band(op, 15)] - elseif p == "d" then - x = fmtvr(op, vr, 12, 22) - elseif p == "n" then - x = fmtvr(op, vr, 16, 7) - elseif p == "m" then - x = fmtvr(op, vr, 0, 5) - elseif p == "P" then - if band(op, 0x02000000) ~= 0 then - x = ror(band(op, 255), 2*band(rshift(op, 8), 15)) - else - x = map_gpr[band(op, 15)] - if band(op, 0xff0) ~= 0 then - operands[#operands+1] = x - local s = map_shift[band(rshift(op, 5), 3)] - local r = nil - if band(op, 0xf90) == 0 then - if s == "ror" then s = "rrx" else r = "#32" end - elseif band(op, 0x10) == 0 then - r = "#"..band(rshift(op, 7), 31) - else - r = map_gpr[band(rshift(op, 8), 15)] - end - if name == "mov" then name = s; x = r - elseif r then x = format("%s %s", s, r) - else x = s end - end - end - elseif p == "L" then - x = fmtload(ctx, op, pos) - elseif p == "l" then - x = fmtvload(ctx, op, pos) - elseif p == "B" then - local addr = ctx.addr + pos + 8 + arshift(lshift(op, 8), 6) - if cond == 15 then addr = addr + band(rshift(op, 23), 2) end - ctx.rel = addr - x = "0x"..tohex(addr) - elseif p == "F" then - vr = "s" - elseif p == "G" then - vr = "d" - elseif p == "." then - suffix = suffix..(vr == "s" and ".f32" or ".f64") - elseif p == "R" then - if band(op, 0x00200000) ~= 0 and #operands == 1 then - operands[1] = operands[1].."!" - end - local t = {} - for i=0,15 do - if band(rshift(op, i), 1) == 1 then t[#t+1] = map_gpr[i] end - end - x = "{"..concat(t, ", ").."}" - elseif p == "r" then - if band(op, 0x00200000) ~= 0 and #operands == 2 then - operands[1] = operands[1].."!" - end - local s = tonumber(sub(last, 2)) - local n = band(op, 255) - if vr == "d" then n = rshift(n, 1) end - operands[#operands] = format("{%s-%s%d}", last, vr, s+n-1) - elseif p == "W" then - x = band(op, 0x0fff) + band(rshift(op, 4), 0xf000) - elseif p == "T" then - x = "#0x"..tohex(band(op, 0x00ffffff), 6) - elseif p == "U" then - x = band(rshift(op, 7), 31) - if x == 0 then x = nil end - elseif p == "u" then - x = band(rshift(op, 7), 31) - if band(op, 0x40) == 0 then - if x == 0 then x = nil else x = "lsl #"..x end - else - if x == 0 then x = "asr #32" else x = "asr #"..x end - end - elseif p == "v" then - x = band(rshift(op, 7), 31) - elseif p == "w" then - x = band(rshift(op, 16), 31) - elseif p == "x" then - x = band(rshift(op, 16), 31) + 1 - elseif p == "X" then - x = band(rshift(op, 16), 31) - last + 1 - elseif p == "Y" then - x = band(rshift(op, 12), 0xf0) + band(op, 0x0f) - elseif p == "K" then - x = "#0x"..tohex(band(rshift(op, 4), 0x0000fff0) + band(op, 15), 4) - elseif p == "s" then - if band(op, 0x00100000) ~= 0 then suffix = "s"..suffix end - else - assert(false) - end - if x then - last = x - if type(x) == "number" then x = "#"..x end - operands[#operands+1] = x - end - end - - return putop(ctx, name..suffix, operands) -end - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - ctx.pos = ofs - ctx.rel = nil - while ctx.pos < stop do disass_ins(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create_(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = addr or 0 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 8 - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass_(code, addr, out) - create_(code, addr, out):disass() -end - --- Return register name for RID. -local function regname_(r) - if r < 16 then return map_gpr[r] end - return "d"..(r-16) -end - --- Public module functions. -module(...) - -create = create_ -disass = disass_ -regname = regname_ - diff --git a/third-party/LuaJIT-2.0.2/src/jit/dis_mips.lua b/third-party/LuaJIT-2.0.2/src/jit/dis_mips.lua deleted file mode 100644 index 830db409b6..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/dis_mips.lua +++ /dev/null @@ -1,428 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPS disassembler module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT/X license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- It disassembles all standard MIPS32R1/R2 instructions. --- Default mode is big-endian, but see: dis_mipsel.lua ------------------------------------------------------------------------------- - -local type = type -local sub, byte, format = string.sub, string.byte, string.format -local match, gmatch, gsub = string.match, string.gmatch, string.gsub -local concat = table.concat -local bit = require("bit") -local band, bor, tohex = bit.band, bit.bor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift - ------------------------------------------------------------------------------- --- Primary and extended opcode maps ------------------------------------------------------------------------------- - -local map_movci = { shift = 16, mask = 1, [0] = "movfDSC", "movtDSC", } -local map_srl = { shift = 21, mask = 1, [0] = "srlDTA", "rotrDTA", } -local map_srlv = { shift = 6, mask = 1, [0] = "srlvDTS", "rotrvDTS", } - -local map_special = { - shift = 0, mask = 63, - [0] = { shift = 0, mask = -1, [0] = "nop", _ = "sllDTA" }, - map_movci, map_srl, "sraDTA", - "sllvDTS", false, map_srlv, "sravDTS", - "jrS", "jalrD1S", "movzDST", "movnDST", - "syscallY", "breakY", false, "sync", - "mfhiD", "mthiS", "mfloD", "mtloS", - false, false, false, false, - "multST", "multuST", "divST", "divuST", - false, false, false, false, - "addDST", "addu|moveDST0", "subDST", "subu|neguDS0T", - "andDST", "orDST", "xorDST", "nor|notDST0", - false, false, "sltDST", "sltuDST", - false, false, false, false, - "tgeSTZ", "tgeuSTZ", "tltSTZ", "tltuSTZ", - "teqSTZ", false, "tneSTZ", -} - -local map_special2 = { - shift = 0, mask = 63, - [0] = "maddST", "madduST", "mulDST", false, - "msubST", "msubuST", - [32] = "clzDS", [33] = "cloDS", - [63] = "sdbbpY", -} - -local map_bshfl = { - shift = 6, mask = 31, - [2] = "wsbhDT", - [16] = "sebDT", - [24] = "sehDT", -} - -local map_special3 = { - shift = 0, mask = 63, - [0] = "extTSAK", [4] = "insTSAL", - [32] = map_bshfl, - [59] = "rdhwrTD", -} - -local map_regimm = { - shift = 16, mask = 31, - [0] = "bltzSB", "bgezSB", "bltzlSB", "bgezlSB", - false, false, false, false, - "tgeiSI", "tgeiuSI", "tltiSI", "tltiuSI", - "teqiSI", false, "tneiSI", false, - "bltzalSB", "bgezalSB", "bltzallSB", "bgezallSB", - false, false, false, false, - false, false, false, false, - false, false, false, "synciSO", -} - -local map_cop0 = { - shift = 25, mask = 1, - [0] = { - shift = 21, mask = 15, - [0] = "mfc0TDW", [4] = "mtc0TDW", - [10] = "rdpgprDT", - [11] = { shift = 5, mask = 1, [0] = "diT0", "eiT0", }, - [14] = "wrpgprDT", - }, { - shift = 0, mask = 63, - [1] = "tlbr", [2] = "tlbwi", [6] = "tlbwr", [8] = "tlbp", - [24] = "eret", [31] = "deret", - [32] = "wait", - }, -} - -local map_cop1s = { - shift = 0, mask = 63, - [0] = "add.sFGH", "sub.sFGH", "mul.sFGH", "div.sFGH", - "sqrt.sFG", "abs.sFG", "mov.sFG", "neg.sFG", - "round.l.sFG", "trunc.l.sFG", "ceil.l.sFG", "floor.l.sFG", - "round.w.sFG", "trunc.w.sFG", "ceil.w.sFG", "floor.w.sFG", - false, - { shift = 16, mask = 1, [0] = "movf.sFGC", "movt.sFGC" }, - "movz.sFGT", "movn.sFGT", - false, "recip.sFG", "rsqrt.sFG", false, - false, false, false, false, - false, false, false, false, - false, "cvt.d.sFG", false, false, - "cvt.w.sFG", "cvt.l.sFG", "cvt.ps.sFGH", false, - false, false, false, false, - false, false, false, false, - "c.f.sVGH", "c.un.sVGH", "c.eq.sVGH", "c.ueq.sVGH", - "c.olt.sVGH", "c.ult.sVGH", "c.ole.sVGH", "c.ule.sVGH", - "c.sf.sVGH", "c.ngle.sVGH", "c.seq.sVGH", "c.ngl.sVGH", - "c.lt.sVGH", "c.nge.sVGH", "c.le.sVGH", "c.ngt.sVGH", -} - -local map_cop1d = { - shift = 0, mask = 63, - [0] = "add.dFGH", "sub.dFGH", "mul.dFGH", "div.dFGH", - "sqrt.dFG", "abs.dFG", "mov.dFG", "neg.dFG", - "round.l.dFG", "trunc.l.dFG", "ceil.l.dFG", "floor.l.dFG", - "round.w.dFG", "trunc.w.dFG", "ceil.w.dFG", "floor.w.dFG", - false, - { shift = 16, mask = 1, [0] = "movf.dFGC", "movt.dFGC" }, - "movz.dFGT", "movn.dFGT", - false, "recip.dFG", "rsqrt.dFG", false, - false, false, false, false, - false, false, false, false, - "cvt.s.dFG", false, false, false, - "cvt.w.dFG", "cvt.l.dFG", false, false, - false, false, false, false, - false, false, false, false, - "c.f.dVGH", "c.un.dVGH", "c.eq.dVGH", "c.ueq.dVGH", - "c.olt.dVGH", "c.ult.dVGH", "c.ole.dVGH", "c.ule.dVGH", - "c.df.dVGH", "c.ngle.dVGH", "c.deq.dVGH", "c.ngl.dVGH", - "c.lt.dVGH", "c.nge.dVGH", "c.le.dVGH", "c.ngt.dVGH", -} - -local map_cop1ps = { - shift = 0, mask = 63, - [0] = "add.psFGH", "sub.psFGH", "mul.psFGH", false, - false, "abs.psFG", "mov.psFG", "neg.psFG", - false, false, false, false, - false, false, false, false, - false, - { shift = 16, mask = 1, [0] = "movf.psFGC", "movt.psFGC" }, - "movz.psFGT", "movn.psFGT", - false, false, false, false, - false, false, false, false, - false, false, false, false, - "cvt.s.puFG", false, false, false, - false, false, false, false, - "cvt.s.plFG", false, false, false, - "pll.psFGH", "plu.psFGH", "pul.psFGH", "puu.psFGH", - "c.f.psVGH", "c.un.psVGH", "c.eq.psVGH", "c.ueq.psVGH", - "c.olt.psVGH", "c.ult.psVGH", "c.ole.psVGH", "c.ule.psVGH", - "c.psf.psVGH", "c.ngle.psVGH", "c.pseq.psVGH", "c.ngl.psVGH", - "c.lt.psVGH", "c.nge.psVGH", "c.le.psVGH", "c.ngt.psVGH", -} - -local map_cop1w = { - shift = 0, mask = 63, - [32] = "cvt.s.wFG", [33] = "cvt.d.wFG", -} - -local map_cop1l = { - shift = 0, mask = 63, - [32] = "cvt.s.lFG", [33] = "cvt.d.lFG", -} - -local map_cop1bc = { - shift = 16, mask = 3, - [0] = "bc1fCB", "bc1tCB", "bc1flCB", "bc1tlCB", -} - -local map_cop1 = { - shift = 21, mask = 31, - [0] = "mfc1TG", false, "cfc1TG", "mfhc1TG", - "mtc1TG", false, "ctc1TG", "mthc1TG", - map_cop1bc, false, false, false, - false, false, false, false, - map_cop1s, map_cop1d, false, false, - map_cop1w, map_cop1l, map_cop1ps, -} - -local map_cop1x = { - shift = 0, mask = 63, - [0] = "lwxc1FSX", "ldxc1FSX", false, false, - false, "luxc1FSX", false, false, - "swxc1FSX", "sdxc1FSX", false, false, - false, "suxc1FSX", false, "prefxMSX", - false, false, false, false, - false, false, false, false, - false, false, false, false, - false, false, "alnv.psFGHS", false, - "madd.sFRGH", "madd.dFRGH", false, false, - false, false, "madd.psFRGH", false, - "msub.sFRGH", "msub.dFRGH", false, false, - false, false, "msub.psFRGH", false, - "nmadd.sFRGH", "nmadd.dFRGH", false, false, - false, false, "nmadd.psFRGH", false, - "nmsub.sFRGH", "nmsub.dFRGH", false, false, - false, false, "nmsub.psFRGH", false, -} - -local map_pri = { - [0] = map_special, map_regimm, "jJ", "jalJ", - "beq|beqz|bST00B", "bne|bnezST0B", "blezSB", "bgtzSB", - "addiTSI", "addiu|liTS0I", "sltiTSI", "sltiuTSI", - "andiTSU", "ori|liTS0U", "xoriTSU", "luiTU", - map_cop0, map_cop1, false, map_cop1x, - "beql|beqzlST0B", "bnel|bnezlST0B", "blezlSB", "bgtzlSB", - false, false, false, false, - map_special2, false, false, map_special3, - "lbTSO", "lhTSO", "lwlTSO", "lwTSO", - "lbuTSO", "lhuTSO", "lwrTSO", false, - "sbTSO", "shTSO", "swlTSO", "swTSO", - false, false, "swrTSO", "cacheNSO", - "llTSO", "lwc1HSO", "lwc2TSO", "prefNSO", - false, "ldc1HSO", "ldc2TSO", false, - "scTSO", "swc1HSO", "swc2TSO", false, - false, "sdc1HSO", "sdc2TSO", false, -} - ------------------------------------------------------------------------------- - -local map_gpr = { - [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", - "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", - "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", - "r24", "r25", "r26", "r27", "r28", "sp", "r30", "ra", -} - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local pos = ctx.pos - local extra = "" - if ctx.rel then - local sym = ctx.symtab[ctx.rel] - if sym then extra = "\t->"..sym end - end - if ctx.hexdump > 0 then - ctx.out(format("%08x %s %-7s %s%s\n", - ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) - else - ctx.out(format("%08x %-7s %s%s\n", - ctx.addr+pos, text, concat(operands, ", "), extra)) - end - ctx.pos = pos + 4 -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) -end - -local function get_be(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - return bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) -end - -local function get_le(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - return bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) -end - --- Disassemble a single instruction. -local function disass_ins(ctx) - local op = ctx:get() - local operands = {} - local last = nil - ctx.op = op - ctx.rel = nil - - local opat = map_pri[rshift(op, 26)] - while type(opat) ~= "string" do - if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ - end - local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") - local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)") - if altname then pat = pat2 end - - for p in gmatch(pat, ".") do - local x = nil - if p == "S" then - x = map_gpr[band(rshift(op, 21), 31)] - elseif p == "T" then - x = map_gpr[band(rshift(op, 16), 31)] - elseif p == "D" then - x = map_gpr[band(rshift(op, 11), 31)] - elseif p == "F" then - x = "f"..band(rshift(op, 6), 31) - elseif p == "G" then - x = "f"..band(rshift(op, 11), 31) - elseif p == "H" then - x = "f"..band(rshift(op, 16), 31) - elseif p == "R" then - x = "f"..band(rshift(op, 21), 31) - elseif p == "A" then - x = band(rshift(op, 6), 31) - elseif p == "M" then - x = band(rshift(op, 11), 31) - elseif p == "N" then - x = band(rshift(op, 16), 31) - elseif p == "C" then - x = band(rshift(op, 18), 7) - if x == 0 then x = nil end - elseif p == "K" then - x = band(rshift(op, 11), 31) + 1 - elseif p == "L" then - x = band(rshift(op, 11), 31) - last + 1 - elseif p == "I" then - x = arshift(lshift(op, 16), 16) - elseif p == "U" then - x = band(op, 0xffff) - elseif p == "O" then - local disp = arshift(lshift(op, 16), 16) - operands[#operands] = format("%d(%s)", disp, last) - elseif p == "X" then - local index = map_gpr[band(rshift(op, 16), 31)] - operands[#operands] = format("%s(%s)", index, last) - elseif p == "B" then - x = ctx.addr + ctx.pos + arshift(lshift(op, 16), 16)*4 + 4 - ctx.rel = x - x = "0x"..tohex(x) - elseif p == "J" then - x = band(ctx.addr + ctx.pos, 0xf0000000) + band(op, 0x03ffffff)*4 - ctx.rel = x - x = "0x"..tohex(x) - elseif p == "V" then - x = band(rshift(op, 8), 7) - if x == 0 then x = nil end - elseif p == "W" then - x = band(op, 7) - if x == 0 then x = nil end - elseif p == "Y" then - x = band(rshift(op, 6), 0x000fffff) - if x == 0 then x = nil end - elseif p == "Z" then - x = band(rshift(op, 6), 1023) - if x == 0 then x = nil end - elseif p == "0" then - if last == "r0" or last == 0 then - local n = #operands - operands[n] = nil - last = operands[n-1] - if altname then - local a1, a2 = match(altname, "([^|]*)|(.*)") - if a1 then name, altname = a1, a2 - else name = altname end - end - end - elseif p == "1" then - if last == "ra" then - operands[#operands] = nil - end - else - assert(false) - end - if x then operands[#operands+1] = x; last = x end - end - - return putop(ctx, name, operands) -end - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - stop = stop - stop % 4 - ctx.pos = ofs - ofs % 4 - ctx.rel = nil - while ctx.pos < stop do disass_ins(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create_(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = addr or 0 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 8 - ctx.get = get_be - return ctx -end - -local function create_el_(code, addr, out) - local ctx = create_(code, addr, out) - ctx.get = get_le - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass_(code, addr, out) - create_(code, addr, out):disass() -end - -local function disass_el_(code, addr, out) - create_el_(code, addr, out):disass() -end - --- Return register name for RID. -local function regname_(r) - if r < 32 then return map_gpr[r] end - return "f"..(r-32) -end - --- Public module functions. -module(...) - -create = create_ -create_el = create_el_ -disass = disass_ -disass_el = disass_el_ -regname = regname_ - diff --git a/third-party/LuaJIT-2.0.2/src/jit/dis_mipsel.lua b/third-party/LuaJIT-2.0.2/src/jit/dis_mipsel.lua deleted file mode 100644 index 8a10c46235..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/dis_mipsel.lua +++ /dev/null @@ -1,20 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPSEL disassembler wrapper module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the little-endian functions from the --- MIPS disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local require = require - -module(...) - -local dis_mips = require(_PACKAGE.."dis_mips") - -create = dis_mips.create_el -disass = dis_mips.disass_el -regname = dis_mips.regname - diff --git a/third-party/LuaJIT-2.0.2/src/jit/dis_ppc.lua b/third-party/LuaJIT-2.0.2/src/jit/dis_ppc.lua deleted file mode 100644 index 169a534c10..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/dis_ppc.lua +++ /dev/null @@ -1,591 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT PPC disassembler module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT/X license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- It disassembles all common, non-privileged 32/64 bit PowerPC instructions --- plus the e500 SPE instructions and some Cell/Xenon extensions. --- --- NYI: VMX, VMX128 ------------------------------------------------------------------------------- - -local type = type -local sub, byte, format = string.sub, string.byte, string.format -local match, gmatch, gsub = string.match, string.gmatch, string.gsub -local concat = table.concat -local bit = require("bit") -local band, bor, tohex = bit.band, bit.bor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift - ------------------------------------------------------------------------------- --- Primary and extended opcode maps ------------------------------------------------------------------------------- - -local map_crops = { - shift = 1, mask = 1023, - [0] = "mcrfXX", - [33] = "crnor|crnotCCC=", [129] = "crandcCCC", - [193] = "crxor|crclrCCC%", [225] = "crnandCCC", - [257] = "crandCCC", [289] = "creqv|crsetCCC%", - [417] = "crorcCCC", [449] = "cror|crmoveCCC=", - [16] = "b_lrKB", [528] = "b_ctrKB", - [150] = "isync", -} - -local map_rlwinm = setmetatable({ - shift = 0, mask = -1, -}, -{ __index = function(t, x) - local rot = band(rshift(x, 11), 31) - local mb = band(rshift(x, 6), 31) - local me = band(rshift(x, 1), 31) - if mb == 0 and me == 31-rot then - return "slwiRR~A." - elseif me == 31 and mb == 32-rot then - return "srwiRR~-A." - else - return "rlwinmRR~AAA." - end - end -}) - -local map_rld = { - shift = 2, mask = 7, - [0] = "rldiclRR~HM.", "rldicrRR~HM.", "rldicRR~HM.", "rldimiRR~HM.", - { - shift = 1, mask = 1, - [0] = "rldclRR~RM.", "rldcrRR~RM.", - }, -} - -local map_ext = setmetatable({ - shift = 1, mask = 1023, - - [0] = "cmp_YLRR", [32] = "cmpl_YLRR", - [4] = "twARR", [68] = "tdARR", - - [8] = "subfcRRR.", [40] = "subfRRR.", - [104] = "negRR.", [136] = "subfeRRR.", - [200] = "subfzeRR.", [232] = "subfmeRR.", - [520] = "subfcoRRR.", [552] = "subfoRRR.", - [616] = "negoRR.", [648] = "subfeoRRR.", - [712] = "subfzeoRR.", [744] = "subfmeoRR.", - - [9] = "mulhduRRR.", [73] = "mulhdRRR.", [233] = "mulldRRR.", - [457] = "divduRRR.", [489] = "divdRRR.", - [745] = "mulldoRRR.", - [969] = "divduoRRR.", [1001] = "divdoRRR.", - - [10] = "addcRRR.", [138] = "addeRRR.", - [202] = "addzeRR.", [234] = "addmeRR.", [266] = "addRRR.", - [522] = "addcoRRR.", [650] = "addeoRRR.", - [714] = "addzeoRR.", [746] = "addmeoRR.", [778] = "addoRRR.", - - [11] = "mulhwuRRR.", [75] = "mulhwRRR.", [235] = "mullwRRR.", - [459] = "divwuRRR.", [491] = "divwRRR.", - [747] = "mullwoRRR.", - [971] = "divwouRRR.", [1003] = "divwoRRR.", - - [15] = "iselltRRR", [47] = "iselgtRRR", [79] = "iseleqRRR", - - [144] = { shift = 20, mask = 1, [0] = "mtcrfRZ~", "mtocrfRZ~", }, - [19] = { shift = 20, mask = 1, [0] = "mfcrR", "mfocrfRZ", }, - [371] = { shift = 11, mask = 1023, [392] = "mftbR", [424] = "mftbuR", }, - [339] = { - shift = 11, mask = 1023, - [32] = "mferR", [256] = "mflrR", [288] = "mfctrR", [16] = "mfspefscrR", - }, - [467] = { - shift = 11, mask = 1023, - [32] = "mtxerR", [256] = "mtlrR", [288] = "mtctrR", [16] = "mtspefscrR", - }, - - [20] = "lwarxRR0R", [84] = "ldarxRR0R", - - [21] = "ldxRR0R", [53] = "lduxRRR", - [149] = "stdxRR0R", [181] = "stduxRRR", - [341] = "lwaxRR0R", [373] = "lwauxRRR", - - [23] = "lwzxRR0R", [55] = "lwzuxRRR", - [87] = "lbzxRR0R", [119] = "lbzuxRRR", - [151] = "stwxRR0R", [183] = "stwuxRRR", - [215] = "stbxRR0R", [247] = "stbuxRRR", - [279] = "lhzxRR0R", [311] = "lhzuxRRR", - [343] = "lhaxRR0R", [375] = "lhauxRRR", - [407] = "sthxRR0R", [439] = "sthuxRRR", - - [54] = "dcbst-R0R", [86] = "dcbf-R0R", - [150] = "stwcxRR0R.", [214] = "stdcxRR0R.", - [246] = "dcbtst-R0R", [278] = "dcbt-R0R", - [310] = "eciwxRR0R", [438] = "ecowxRR0R", - [470] = "dcbi-RR", - - [598] = { - shift = 21, mask = 3, - [0] = "sync", "lwsync", "ptesync", - }, - [758] = "dcba-RR", - [854] = "eieio", [982] = "icbi-R0R", [1014] = "dcbz-R0R", - - [26] = "cntlzwRR~", [58] = "cntlzdRR~", - [122] = "popcntbRR~", - [154] = "prtywRR~", [186] = "prtydRR~", - - [28] = "andRR~R.", [60] = "andcRR~R.", [124] = "nor|notRR~R=.", - [284] = "eqvRR~R.", [316] = "xorRR~R.", - [412] = "orcRR~R.", [444] = "or|mrRR~R=.", [476] = "nandRR~R.", - [508] = "cmpbRR~R", - - [512] = "mcrxrX", - - [532] = "ldbrxRR0R", [660] = "stdbrxRR0R", - - [533] = "lswxRR0R", [597] = "lswiRR0A", - [661] = "stswxRR0R", [725] = "stswiRR0A", - - [534] = "lwbrxRR0R", [662] = "stwbrxRR0R", - [790] = "lhbrxRR0R", [918] = "sthbrxRR0R", - - [535] = "lfsxFR0R", [567] = "lfsuxFRR", - [599] = "lfdxFR0R", [631] = "lfduxFRR", - [663] = "stfsxFR0R", [695] = "stfsuxFRR", - [727] = "stfdxFR0R", [759] = "stfduxFR0R", - [855] = "lfiwaxFR0R", - [983] = "stfiwxFR0R", - - [24] = "slwRR~R.", - - [27] = "sldRR~R.", [536] = "srwRR~R.", - [792] = "srawRR~R.", [824] = "srawiRR~A.", - - [794] = "sradRR~R.", [826] = "sradiRR~H.", [827] = "sradiRR~H.", - [922] = "extshRR~.", [954] = "extsbRR~.", [986] = "extswRR~.", - - [539] = "srdRR~R.", -}, -{ __index = function(t, x) - if band(x, 31) == 15 then return "iselRRRC" end - end -}) - -local map_ld = { - shift = 0, mask = 3, - [0] = "ldRRE", "lduRRE", "lwaRRE", -} - -local map_std = { - shift = 0, mask = 3, - [0] = "stdRRE", "stduRRE", -} - -local map_fps = { - shift = 5, mask = 1, - { - shift = 1, mask = 15, - [0] = false, false, "fdivsFFF.", false, - "fsubsFFF.", "faddsFFF.", "fsqrtsF-F.", false, - "fresF-F.", "fmulsFF-F.", "frsqrtesF-F.", false, - "fmsubsFFFF~.", "fmaddsFFFF~.", "fnmsubsFFFF~.", "fnmaddsFFFF~.", - } -} - -local map_fpd = { - shift = 5, mask = 1, - [0] = { - shift = 1, mask = 1023, - [0] = "fcmpuXFF", [32] = "fcmpoXFF", [64] = "mcrfsXX", - [38] = "mtfsb1A.", [70] = "mtfsb0A.", [134] = "mtfsfiA>>-A>", - [8] = "fcpsgnFFF.", [40] = "fnegF-F.", [72] = "fmrF-F.", - [136] = "fnabsF-F.", [264] = "fabsF-F.", - [12] = "frspF-F.", - [14] = "fctiwF-F.", [15] = "fctiwzF-F.", - [583] = "mffsF.", [711] = "mtfsfZF.", - [392] = "frinF-F.", [424] = "frizF-F.", - [456] = "fripF-F.", [488] = "frimF-F.", - [814] = "fctidF-F.", [815] = "fctidzF-F.", [846] = "fcfidF-F.", - }, - { - shift = 1, mask = 15, - [0] = false, false, "fdivFFF.", false, - "fsubFFF.", "faddFFF.", "fsqrtF-F.", "fselFFFF~.", - "freF-F.", "fmulFF-F.", "frsqrteF-F.", false, - "fmsubFFFF~.", "fmaddFFFF~.", "fnmsubFFFF~.", "fnmaddFFFF~.", - } -} - -local map_spe = { - shift = 0, mask = 2047, - - [512] = "evaddwRRR", [514] = "evaddiwRAR~", - [516] = "evsubwRRR~", [518] = "evsubiwRAR~", - [520] = "evabsRR", [521] = "evnegRR", - [522] = "evextsbRR", [523] = "evextshRR", [524] = "evrndwRR", - [525] = "evcntlzwRR", [526] = "evcntlswRR", - - [527] = "brincRRR", - - [529] = "evandRRR", [530] = "evandcRRR", [534] = "evxorRRR", - [535] = "evor|evmrRRR=", [536] = "evnor|evnotRRR=", - [537] = "eveqvRRR", [539] = "evorcRRR", [542] = "evnandRRR", - - [544] = "evsrwuRRR", [545] = "evsrwsRRR", - [546] = "evsrwiuRRA", [547] = "evsrwisRRA", - [548] = "evslwRRR", [550] = "evslwiRRA", - [552] = "evrlwRRR", [553] = "evsplatiRS", - [554] = "evrlwiRRA", [555] = "evsplatfiRS", - [556] = "evmergehiRRR", [557] = "evmergeloRRR", - [558] = "evmergehiloRRR", [559] = "evmergelohiRRR", - - [560] = "evcmpgtuYRR", [561] = "evcmpgtsYRR", - [562] = "evcmpltuYRR", [563] = "evcmpltsYRR", - [564] = "evcmpeqYRR", - - [632] = "evselRRR", [633] = "evselRRRW", - [634] = "evselRRRW", [635] = "evselRRRW", - [636] = "evselRRRW", [637] = "evselRRRW", - [638] = "evselRRRW", [639] = "evselRRRW", - - [640] = "evfsaddRRR", [641] = "evfssubRRR", - [644] = "evfsabsRR", [645] = "evfsnabsRR", [646] = "evfsnegRR", - [648] = "evfsmulRRR", [649] = "evfsdivRRR", - [652] = "evfscmpgtYRR", [653] = "evfscmpltYRR", [654] = "evfscmpeqYRR", - [656] = "evfscfuiR-R", [657] = "evfscfsiR-R", - [658] = "evfscfufR-R", [659] = "evfscfsfR-R", - [660] = "evfsctuiR-R", [661] = "evfsctsiR-R", - [662] = "evfsctufR-R", [663] = "evfsctsfR-R", - [664] = "evfsctuizR-R", [666] = "evfsctsizR-R", - [668] = "evfststgtYRR", [669] = "evfststltYRR", [670] = "evfststeqYRR", - - [704] = "efsaddRRR", [705] = "efssubRRR", - [708] = "efsabsRR", [709] = "efsnabsRR", [710] = "efsnegRR", - [712] = "efsmulRRR", [713] = "efsdivRRR", - [716] = "efscmpgtYRR", [717] = "efscmpltYRR", [718] = "efscmpeqYRR", - [719] = "efscfdR-R", - [720] = "efscfuiR-R", [721] = "efscfsiR-R", - [722] = "efscfufR-R", [723] = "efscfsfR-R", - [724] = "efsctuiR-R", [725] = "efsctsiR-R", - [726] = "efsctufR-R", [727] = "efsctsfR-R", - [728] = "efsctuizR-R", [730] = "efsctsizR-R", - [732] = "efststgtYRR", [733] = "efststltYRR", [734] = "efststeqYRR", - - [736] = "efdaddRRR", [737] = "efdsubRRR", - [738] = "efdcfuidR-R", [739] = "efdcfsidR-R", - [740] = "efdabsRR", [741] = "efdnabsRR", [742] = "efdnegRR", - [744] = "efdmulRRR", [745] = "efddivRRR", - [746] = "efdctuidzR-R", [747] = "efdctsidzR-R", - [748] = "efdcmpgtYRR", [749] = "efdcmpltYRR", [750] = "efdcmpeqYRR", - [751] = "efdcfsR-R", - [752] = "efdcfuiR-R", [753] = "efdcfsiR-R", - [754] = "efdcfufR-R", [755] = "efdcfsfR-R", - [756] = "efdctuiR-R", [757] = "efdctsiR-R", - [758] = "efdctufR-R", [759] = "efdctsfR-R", - [760] = "efdctuizR-R", [762] = "efdctsizR-R", - [764] = "efdtstgtYRR", [765] = "efdtstltYRR", [766] = "efdtsteqYRR", - - [768] = "evlddxRR0R", [769] = "evlddRR8", - [770] = "evldwxRR0R", [771] = "evldwRR8", - [772] = "evldhxRR0R", [773] = "evldhRR8", - [776] = "evlhhesplatxRR0R", [777] = "evlhhesplatRR2", - [780] = "evlhhousplatxRR0R", [781] = "evlhhousplatRR2", - [782] = "evlhhossplatxRR0R", [783] = "evlhhossplatRR2", - [784] = "evlwhexRR0R", [785] = "evlwheRR4", - [788] = "evlwhouxRR0R", [789] = "evlwhouRR4", - [790] = "evlwhosxRR0R", [791] = "evlwhosRR4", - [792] = "evlwwsplatxRR0R", [793] = "evlwwsplatRR4", - [796] = "evlwhsplatxRR0R", [797] = "evlwhsplatRR4", - - [800] = "evstddxRR0R", [801] = "evstddRR8", - [802] = "evstdwxRR0R", [803] = "evstdwRR8", - [804] = "evstdhxRR0R", [805] = "evstdhRR8", - [816] = "evstwhexRR0R", [817] = "evstwheRR4", - [820] = "evstwhoxRR0R", [821] = "evstwhoRR4", - [824] = "evstwwexRR0R", [825] = "evstwweRR4", - [828] = "evstwwoxRR0R", [829] = "evstwwoRR4", - - [1027] = "evmhessfRRR", [1031] = "evmhossfRRR", [1032] = "evmheumiRRR", - [1033] = "evmhesmiRRR", [1035] = "evmhesmfRRR", [1036] = "evmhoumiRRR", - [1037] = "evmhosmiRRR", [1039] = "evmhosmfRRR", [1059] = "evmhessfaRRR", - [1063] = "evmhossfaRRR", [1064] = "evmheumiaRRR", [1065] = "evmhesmiaRRR", - [1067] = "evmhesmfaRRR", [1068] = "evmhoumiaRRR", [1069] = "evmhosmiaRRR", - [1071] = "evmhosmfaRRR", [1095] = "evmwhssfRRR", [1096] = "evmwlumiRRR", - [1100] = "evmwhumiRRR", [1101] = "evmwhsmiRRR", [1103] = "evmwhsmfRRR", - [1107] = "evmwssfRRR", [1112] = "evmwumiRRR", [1113] = "evmwsmiRRR", - [1115] = "evmwsmfRRR", [1127] = "evmwhssfaRRR", [1128] = "evmwlumiaRRR", - [1132] = "evmwhumiaRRR", [1133] = "evmwhsmiaRRR", [1135] = "evmwhsmfaRRR", - [1139] = "evmwssfaRRR", [1144] = "evmwumiaRRR", [1145] = "evmwsmiaRRR", - [1147] = "evmwsmfaRRR", - - [1216] = "evaddusiaawRR", [1217] = "evaddssiaawRR", - [1218] = "evsubfusiaawRR", [1219] = "evsubfssiaawRR", - [1220] = "evmraRR", - [1222] = "evdivwsRRR", [1223] = "evdivwuRRR", - [1224] = "evaddumiaawRR", [1225] = "evaddsmiaawRR", - [1226] = "evsubfumiaawRR", [1227] = "evsubfsmiaawRR", - - [1280] = "evmheusiaawRRR", [1281] = "evmhessiaawRRR", - [1283] = "evmhessfaawRRR", [1284] = "evmhousiaawRRR", - [1285] = "evmhossiaawRRR", [1287] = "evmhossfaawRRR", - [1288] = "evmheumiaawRRR", [1289] = "evmhesmiaawRRR", - [1291] = "evmhesmfaawRRR", [1292] = "evmhoumiaawRRR", - [1293] = "evmhosmiaawRRR", [1295] = "evmhosmfaawRRR", - [1320] = "evmhegumiaaRRR", [1321] = "evmhegsmiaaRRR", - [1323] = "evmhegsmfaaRRR", [1324] = "evmhogumiaaRRR", - [1325] = "evmhogsmiaaRRR", [1327] = "evmhogsmfaaRRR", - [1344] = "evmwlusiaawRRR", [1345] = "evmwlssiaawRRR", - [1352] = "evmwlumiaawRRR", [1353] = "evmwlsmiaawRRR", - [1363] = "evmwssfaaRRR", [1368] = "evmwumiaaRRR", - [1369] = "evmwsmiaaRRR", [1371] = "evmwsmfaaRRR", - [1408] = "evmheusianwRRR", [1409] = "evmhessianwRRR", - [1411] = "evmhessfanwRRR", [1412] = "evmhousianwRRR", - [1413] = "evmhossianwRRR", [1415] = "evmhossfanwRRR", - [1416] = "evmheumianwRRR", [1417] = "evmhesmianwRRR", - [1419] = "evmhesmfanwRRR", [1420] = "evmhoumianwRRR", - [1421] = "evmhosmianwRRR", [1423] = "evmhosmfanwRRR", - [1448] = "evmhegumianRRR", [1449] = "evmhegsmianRRR", - [1451] = "evmhegsmfanRRR", [1452] = "evmhogumianRRR", - [1453] = "evmhogsmianRRR", [1455] = "evmhogsmfanRRR", - [1472] = "evmwlusianwRRR", [1473] = "evmwlssianwRRR", - [1480] = "evmwlumianwRRR", [1481] = "evmwlsmianwRRR", - [1491] = "evmwssfanRRR", [1496] = "evmwumianRRR", - [1497] = "evmwsmianRRR", [1499] = "evmwsmfanRRR", -} - -local map_pri = { - [0] = false, false, "tdiARI", "twiARI", - map_spe, false, false, "mulliRRI", - "subficRRI", false, "cmpl_iYLRU", "cmp_iYLRI", - "addicRRI", "addic.RRI", "addi|liRR0I", "addis|lisRR0I", - "b_KBJ", "sc", "bKJ", map_crops, - "rlwimiRR~AAA.", map_rlwinm, false, "rlwnmRR~RAA.", - "oriNRR~U", "orisRR~U", "xoriRR~U", "xorisRR~U", - "andi.RR~U", "andis.RR~U", map_rld, map_ext, - "lwzRRD", "lwzuRRD", "lbzRRD", "lbzuRRD", - "stwRRD", "stwuRRD", "stbRRD", "stbuRRD", - "lhzRRD", "lhzuRRD", "lhaRRD", "lhauRRD", - "sthRRD", "sthuRRD", "lmwRRD", "stmwRRD", - "lfsFRD", "lfsuFRD", "lfdFRD", "lfduFRD", - "stfsFRD", "stfsuFRD", "stfdFRD", "stfduFRD", - false, false, map_ld, map_fps, - false, false, map_std, map_fpd, -} - ------------------------------------------------------------------------------- - -local map_gpr = { - [0] = "r0", "sp", "r2", "r3", "r4", "r5", "r6", "r7", - "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", - "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", - "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", -} - -local map_cond = { [0] = "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", } - --- Format a condition bit. -local function condfmt(cond) - if cond <= 3 then - return map_cond[band(cond, 3)] - else - return format("4*cr%d+%s", rshift(cond, 2), map_cond[band(cond, 3)]) - end -end - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local pos = ctx.pos - local extra = "" - if ctx.rel then - local sym = ctx.symtab[ctx.rel] - if sym then extra = "\t->"..sym end - end - if ctx.hexdump > 0 then - ctx.out(format("%08x %s %-7s %s%s\n", - ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) - else - ctx.out(format("%08x %-7s %s%s\n", - ctx.addr+pos, text, concat(operands, ", "), extra)) - end - ctx.pos = pos + 4 -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) -end - --- Disassemble a single instruction. -local function disass_ins(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) - local operands = {} - local last = nil - local rs = 21 - ctx.op = op - ctx.rel = nil - - local opat = map_pri[rshift(b0, 2)] - while type(opat) ~= "string" do - if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] - end - local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") - local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)") - if altname then pat = pat2 end - - for p in gmatch(pat, ".") do - local x = nil - if p == "R" then - x = map_gpr[band(rshift(op, rs), 31)] - rs = rs - 5 - elseif p == "F" then - x = "f"..band(rshift(op, rs), 31) - rs = rs - 5 - elseif p == "A" then - x = band(rshift(op, rs), 31) - rs = rs - 5 - elseif p == "S" then - x = arshift(lshift(op, 27-rs), 27) - rs = rs - 5 - elseif p == "I" then - x = arshift(lshift(op, 16), 16) - elseif p == "U" then - x = band(op, 0xffff) - elseif p == "D" or p == "E" then - local disp = arshift(lshift(op, 16), 16) - if p == "E" then disp = band(disp, -4) end - if last == "r0" then last = "0" end - operands[#operands] = format("%d(%s)", disp, last) - elseif p >= "2" and p <= "8" then - local disp = band(rshift(op, rs), 31) * p - if last == "r0" then last = "0" end - operands[#operands] = format("%d(%s)", disp, last) - elseif p == "H" then - x = band(rshift(op, rs), 31) + lshift(band(op, 2), 4) - rs = rs - 5 - elseif p == "M" then - x = band(rshift(op, rs), 31) + band(op, 0x20) - elseif p == "C" then - x = condfmt(band(rshift(op, rs), 31)) - rs = rs - 5 - elseif p == "B" then - local bo = rshift(op, 21) - local cond = band(rshift(op, 16), 31) - local cn = "" - rs = rs - 10 - if band(bo, 4) == 0 then - cn = band(bo, 2) == 0 and "dnz" or "dz" - if band(bo, 0x10) == 0 then - cn = cn..(band(bo, 8) == 0 and "f" or "t") - end - if band(bo, 0x10) == 0 then x = condfmt(cond) end - name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") - elseif band(bo, 0x10) == 0 then - cn = map_cond[band(cond, 3) + (band(bo, 8) == 0 and 4 or 0)] - if cond > 3 then x = "cr"..rshift(cond, 2) end - name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") - end - name = gsub(name, "_", cn) - elseif p == "J" then - x = arshift(lshift(op, 27-rs), 29-rs)*4 - if band(op, 2) == 0 then x = ctx.addr + pos + x end - ctx.rel = x - x = "0x"..tohex(x) - elseif p == "K" then - if band(op, 1) ~= 0 then name = name.."l" end - if band(op, 2) ~= 0 then name = name.."a" end - elseif p == "X" or p == "Y" then - x = band(rshift(op, rs+2), 7) - if x == 0 and p == "Y" then x = nil else x = "cr"..x end - rs = rs - 5 - elseif p == "W" then - x = "cr"..band(op, 7) - elseif p == "Z" then - x = band(rshift(op, rs-4), 255) - rs = rs - 10 - elseif p == ">" then - operands[#operands] = rshift(operands[#operands], 1) - elseif p == "0" then - if last == "r0" then - operands[#operands] = nil - if altname then name = altname end - end - elseif p == "L" then - name = gsub(name, "_", band(op, 0x00200000) ~= 0 and "d" or "w") - elseif p == "." then - if band(op, 1) == 1 then name = name.."." end - elseif p == "N" then - if op == 0x60000000 then name = "nop"; break end - elseif p == "~" then - local n = #operands - operands[n-1], operands[n] = operands[n], operands[n-1] - elseif p == "=" then - local n = #operands - if last == operands[n-1] then - operands[n] = nil - name = altname - end - elseif p == "%" then - local n = #operands - if last == operands[n-1] and last == operands[n-2] then - operands[n] = nil - operands[n-1] = nil - name = altname - end - elseif p == "-" then - rs = rs - 5 - else - assert(false) - end - if x then operands[#operands+1] = x; last = x end - end - - return putop(ctx, name, operands) -end - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - stop = stop - stop % 4 - ctx.pos = ofs - ofs % 4 - ctx.rel = nil - while ctx.pos < stop do disass_ins(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create_(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = addr or 0 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 8 - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass_(code, addr, out) - create_(code, addr, out):disass() -end - --- Return register name for RID. -local function regname_(r) - if r < 32 then return map_gpr[r] end - return "f"..(r-32) -end - --- Public module functions. -module(...) - -create = create_ -disass = disass_ -regname = regname_ - diff --git a/third-party/LuaJIT-2.0.2/src/jit/dis_x64.lua b/third-party/LuaJIT-2.0.2/src/jit/dis_x64.lua deleted file mode 100644 index 4a1894acea..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/dis_x64.lua +++ /dev/null @@ -1,20 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT x64 disassembler wrapper module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the 64 bit functions from the combined --- x86/x64 disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local require = require - -module(...) - -local dis_x86 = require(_PACKAGE.."dis_x86") - -create = dis_x86.create64 -disass = dis_x86.disass64 -regname = dis_x86.regname64 - diff --git a/third-party/LuaJIT-2.0.2/src/jit/dis_x86.lua b/third-party/LuaJIT-2.0.2/src/jit/dis_x86.lua deleted file mode 100644 index c442a176b1..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/dis_x86.lua +++ /dev/null @@ -1,836 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT x86/x64 disassembler module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- Sending small code snippets to an external disassembler and mixing the --- output with our own stuff was too fragile. So I had to bite the bullet --- and write yet another x86 disassembler. Oh well ... --- --- The output format is very similar to what ndisasm generates. But it has --- been developed independently by looking at the opcode tables from the --- Intel and AMD manuals. The supported instruction set is quite extensive --- and reflects what a current generation Intel or AMD CPU implements in --- 32 bit and 64 bit mode. Yes, this includes MMX, SSE, SSE2, SSE3, SSSE3, --- SSE4.1, SSE4.2, SSE4a and even privileged and hypervisor (VMX/SVM) --- instructions. --- --- Notes: --- * The (useless) a16 prefix, 3DNow and pre-586 opcodes are unsupported. --- * No attempt at optimization has been made -- it's fast enough for my needs. --- * The public API may change when more architectures are added. ------------------------------------------------------------------------------- - -local type = type -local sub, byte, format = string.sub, string.byte, string.format -local match, gmatch, gsub = string.match, string.gmatch, string.gsub -local lower, rep = string.lower, string.rep - --- Map for 1st opcode byte in 32 bit mode. Ugly? Well ... read on. -local map_opc1_32 = { ---0x -[0]="addBmr","addVmr","addBrm","addVrm","addBai","addVai","push es","pop es", -"orBmr","orVmr","orBrm","orVrm","orBai","orVai","push cs","opc2*", ---1x -"adcBmr","adcVmr","adcBrm","adcVrm","adcBai","adcVai","push ss","pop ss", -"sbbBmr","sbbVmr","sbbBrm","sbbVrm","sbbBai","sbbVai","push ds","pop ds", ---2x -"andBmr","andVmr","andBrm","andVrm","andBai","andVai","es:seg","daa", -"subBmr","subVmr","subBrm","subVrm","subBai","subVai","cs:seg","das", ---3x -"xorBmr","xorVmr","xorBrm","xorVrm","xorBai","xorVai","ss:seg","aaa", -"cmpBmr","cmpVmr","cmpBrm","cmpVrm","cmpBai","cmpVai","ds:seg","aas", ---4x -"incVR","incVR","incVR","incVR","incVR","incVR","incVR","incVR", -"decVR","decVR","decVR","decVR","decVR","decVR","decVR","decVR", ---5x -"pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR", -"popUR","popUR","popUR","popUR","popUR","popUR","popUR","popUR", ---6x -"sz*pushaw,pusha","sz*popaw,popa","boundVrm","arplWmr", -"fs:seg","gs:seg","o16:","a16", -"pushUi","imulVrmi","pushBs","imulVrms", -"insb","insVS","outsb","outsVS", ---7x -"joBj","jnoBj","jbBj","jnbBj","jzBj","jnzBj","jbeBj","jaBj", -"jsBj","jnsBj","jpeBj","jpoBj","jlBj","jgeBj","jleBj","jgBj", ---8x -"arith!Bmi","arith!Vmi","arith!Bmi","arith!Vms", -"testBmr","testVmr","xchgBrm","xchgVrm", -"movBmr","movVmr","movBrm","movVrm", -"movVmg","leaVrm","movWgm","popUm", ---9x -"nop*xchgVaR|pause|xchgWaR|repne nop","xchgVaR","xchgVaR","xchgVaR", -"xchgVaR","xchgVaR","xchgVaR","xchgVaR", -"sz*cbw,cwde,cdqe","sz*cwd,cdq,cqo","call farViw","wait", -"sz*pushfw,pushf","sz*popfw,popf","sahf","lahf", ---Ax -"movBao","movVao","movBoa","movVoa", -"movsb","movsVS","cmpsb","cmpsVS", -"testBai","testVai","stosb","stosVS", -"lodsb","lodsVS","scasb","scasVS", ---Bx -"movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi", -"movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI", ---Cx -"shift!Bmu","shift!Vmu","retBw","ret","$lesVrm","$ldsVrm","movBmi","movVmi", -"enterBwu","leave","retfBw","retf","int3","intBu","into","iretVS", ---Dx -"shift!Bm1","shift!Vm1","shift!Bmc","shift!Vmc","aamBu","aadBu","salc","xlatb", -"fp*0","fp*1","fp*2","fp*3","fp*4","fp*5","fp*6","fp*7", ---Ex -"loopneBj","loopeBj","loopBj","sz*jcxzBj,jecxzBj,jrcxzBj", -"inBau","inVau","outBua","outVua", -"callVj","jmpVj","jmp farViw","jmpBj","inBad","inVad","outBda","outVda", ---Fx -"lock:","int1","repne:rep","rep:","hlt","cmc","testb!Bm","testv!Vm", -"clc","stc","cli","sti","cld","std","incb!Bm","incd!Vm", -} -assert(#map_opc1_32 == 255) - --- Map for 1st opcode byte in 64 bit mode (overrides only). -local map_opc1_64 = setmetatable({ - [0x06]=false, [0x07]=false, [0x0e]=false, - [0x16]=false, [0x17]=false, [0x1e]=false, [0x1f]=false, - [0x27]=false, [0x2f]=false, [0x37]=false, [0x3f]=false, - [0x60]=false, [0x61]=false, [0x62]=false, [0x63]="movsxdVrDmt", [0x67]="a32:", - [0x40]="rex*", [0x41]="rex*b", [0x42]="rex*x", [0x43]="rex*xb", - [0x44]="rex*r", [0x45]="rex*rb", [0x46]="rex*rx", [0x47]="rex*rxb", - [0x48]="rex*w", [0x49]="rex*wb", [0x4a]="rex*wx", [0x4b]="rex*wxb", - [0x4c]="rex*wr", [0x4d]="rex*wrb", [0x4e]="rex*wrx", [0x4f]="rex*wrxb", - [0x82]=false, [0x9a]=false, [0xc4]=false, [0xc5]=false, [0xce]=false, - [0xd4]=false, [0xd5]=false, [0xd6]=false, [0xea]=false, -}, { __index = map_opc1_32 }) - --- Map for 2nd opcode byte (0F xx). True CISC hell. Hey, I told you. --- Prefix dependent MMX/SSE opcodes: (none)|rep|o16|repne, -|F3|66|F2 -local map_opc2 = { ---0x -[0]="sldt!Dmp","sgdt!Ump","larVrm","lslVrm",nil,"syscall","clts","sysret", -"invd","wbinvd",nil,"ud1",nil,"$prefetch!Bm","femms","3dnowMrmu", ---1x -"movupsXrm|movssXrm|movupdXrm|movsdXrm", -"movupsXmr|movssXmr|movupdXmr|movsdXmr", -"movhlpsXrm$movlpsXrm|movsldupXrm|movlpdXrm|movddupXrm", -"movlpsXmr||movlpdXmr", -"unpcklpsXrm||unpcklpdXrm", -"unpckhpsXrm||unpckhpdXrm", -"movlhpsXrm$movhpsXrm|movshdupXrm|movhpdXrm", -"movhpsXmr||movhpdXmr", -"$prefetcht!Bm","hintnopVm","hintnopVm","hintnopVm", -"hintnopVm","hintnopVm","hintnopVm","hintnopVm", ---2x -"movUmx$","movUmy$","movUxm$","movUym$","movUmz$",nil,"movUzm$",nil, -"movapsXrm||movapdXrm", -"movapsXmr||movapdXmr", -"cvtpi2psXrMm|cvtsi2ssXrVmt|cvtpi2pdXrMm|cvtsi2sdXrVmt", -"movntpsXmr|movntssXmr|movntpdXmr|movntsdXmr", -"cvttps2piMrXm|cvttss2siVrXm|cvttpd2piMrXm|cvttsd2siVrXm", -"cvtps2piMrXm|cvtss2siVrXm|cvtpd2piMrXm|cvtsd2siVrXm", -"ucomissXrm||ucomisdXrm", -"comissXrm||comisdXrm", ---3x -"wrmsr","rdtsc","rdmsr","rdpmc","sysenter","sysexit",nil,"getsec", -"opc3*38",nil,"opc3*3a",nil,nil,nil,nil,nil, ---4x -"cmovoVrm","cmovnoVrm","cmovbVrm","cmovnbVrm", -"cmovzVrm","cmovnzVrm","cmovbeVrm","cmovaVrm", -"cmovsVrm","cmovnsVrm","cmovpeVrm","cmovpoVrm", -"cmovlVrm","cmovgeVrm","cmovleVrm","cmovgVrm", ---5x -"movmskpsVrXm$||movmskpdVrXm$","sqrtpsXrm|sqrtssXrm|sqrtpdXrm|sqrtsdXrm", -"rsqrtpsXrm|rsqrtssXrm","rcppsXrm|rcpssXrm", -"andpsXrm||andpdXrm","andnpsXrm||andnpdXrm", -"orpsXrm||orpdXrm","xorpsXrm||xorpdXrm", -"addpsXrm|addssXrm|addpdXrm|addsdXrm","mulpsXrm|mulssXrm|mulpdXrm|mulsdXrm", -"cvtps2pdXrm|cvtss2sdXrm|cvtpd2psXrm|cvtsd2ssXrm", -"cvtdq2psXrm|cvttps2dqXrm|cvtps2dqXrm", -"subpsXrm|subssXrm|subpdXrm|subsdXrm","minpsXrm|minssXrm|minpdXrm|minsdXrm", -"divpsXrm|divssXrm|divpdXrm|divsdXrm","maxpsXrm|maxssXrm|maxpdXrm|maxsdXrm", ---6x -"punpcklbwPrm","punpcklwdPrm","punpckldqPrm","packsswbPrm", -"pcmpgtbPrm","pcmpgtwPrm","pcmpgtdPrm","packuswbPrm", -"punpckhbwPrm","punpckhwdPrm","punpckhdqPrm","packssdwPrm", -"||punpcklqdqXrm","||punpckhqdqXrm", -"movPrVSm","movqMrm|movdquXrm|movdqaXrm", ---7x -"pshufwMrmu|pshufhwXrmu|pshufdXrmu|pshuflwXrmu","pshiftw!Pmu", -"pshiftd!Pmu","pshiftq!Mmu||pshiftdq!Xmu", -"pcmpeqbPrm","pcmpeqwPrm","pcmpeqdPrm","emms|", -"vmreadUmr||extrqXmuu$|insertqXrmuu$","vmwriteUrm||extrqXrm$|insertqXrm$", -nil,nil, -"||haddpdXrm|haddpsXrm","||hsubpdXrm|hsubpsXrm", -"movVSmMr|movqXrm|movVSmXr","movqMmr|movdquXmr|movdqaXmr", ---8x -"joVj","jnoVj","jbVj","jnbVj","jzVj","jnzVj","jbeVj","jaVj", -"jsVj","jnsVj","jpeVj","jpoVj","jlVj","jgeVj","jleVj","jgVj", ---9x -"setoBm","setnoBm","setbBm","setnbBm","setzBm","setnzBm","setbeBm","setaBm", -"setsBm","setnsBm","setpeBm","setpoBm","setlBm","setgeBm","setleBm","setgBm", ---Ax -"push fs","pop fs","cpuid","btVmr","shldVmru","shldVmrc",nil,nil, -"push gs","pop gs","rsm","btsVmr","shrdVmru","shrdVmrc","fxsave!Dmp","imulVrm", ---Bx -"cmpxchgBmr","cmpxchgVmr","$lssVrm","btrVmr", -"$lfsVrm","$lgsVrm","movzxVrBmt","movzxVrWmt", -"|popcntVrm","ud2Dp","bt!Vmu","btcVmr", -"bsfVrm","bsrVrm|lzcntVrm|bsrWrm","movsxVrBmt","movsxVrWmt", ---Cx -"xaddBmr","xaddVmr", -"cmppsXrmu|cmpssXrmu|cmppdXrmu|cmpsdXrmu","$movntiVmr|", -"pinsrwPrWmu","pextrwDrPmu", -"shufpsXrmu||shufpdXrmu","$cmpxchg!Qmp", -"bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR", ---Dx -"||addsubpdXrm|addsubpsXrm","psrlwPrm","psrldPrm","psrlqPrm", -"paddqPrm","pmullwPrm", -"|movq2dqXrMm|movqXmr|movdq2qMrXm$","pmovmskbVrMm||pmovmskbVrXm", -"psubusbPrm","psubuswPrm","pminubPrm","pandPrm", -"paddusbPrm","padduswPrm","pmaxubPrm","pandnPrm", ---Ex -"pavgbPrm","psrawPrm","psradPrm","pavgwPrm", -"pmulhuwPrm","pmulhwPrm", -"|cvtdq2pdXrm|cvttpd2dqXrm|cvtpd2dqXrm","$movntqMmr||$movntdqXmr", -"psubsbPrm","psubswPrm","pminswPrm","porPrm", -"paddsbPrm","paddswPrm","pmaxswPrm","pxorPrm", ---Fx -"|||lddquXrm","psllwPrm","pslldPrm","psllqPrm", -"pmuludqPrm","pmaddwdPrm","psadbwPrm","maskmovqMrm||maskmovdquXrm$", -"psubbPrm","psubwPrm","psubdPrm","psubqPrm", -"paddbPrm","paddwPrm","padddPrm","ud", -} -assert(map_opc2[255] == "ud") - --- Map for three-byte opcodes. Can't wait for their next invention. -local map_opc3 = { -["38"] = { -- [66] 0f 38 xx ---0x -[0]="pshufbPrm","phaddwPrm","phadddPrm","phaddswPrm", -"pmaddubswPrm","phsubwPrm","phsubdPrm","phsubswPrm", -"psignbPrm","psignwPrm","psigndPrm","pmulhrswPrm", -nil,nil,nil,nil, ---1x -"||pblendvbXrma",nil,nil,nil, -"||blendvpsXrma","||blendvpdXrma",nil,"||ptestXrm", -nil,nil,nil,nil, -"pabsbPrm","pabswPrm","pabsdPrm",nil, ---2x -"||pmovsxbwXrm","||pmovsxbdXrm","||pmovsxbqXrm","||pmovsxwdXrm", -"||pmovsxwqXrm","||pmovsxdqXrm",nil,nil, -"||pmuldqXrm","||pcmpeqqXrm","||$movntdqaXrm","||packusdwXrm", -nil,nil,nil,nil, ---3x -"||pmovzxbwXrm","||pmovzxbdXrm","||pmovzxbqXrm","||pmovzxwdXrm", -"||pmovzxwqXrm","||pmovzxdqXrm",nil,"||pcmpgtqXrm", -"||pminsbXrm","||pminsdXrm","||pminuwXrm","||pminudXrm", -"||pmaxsbXrm","||pmaxsdXrm","||pmaxuwXrm","||pmaxudXrm", ---4x -"||pmulddXrm","||phminposuwXrm", ---Fx -[0xf0] = "|||crc32TrBmt",[0xf1] = "|||crc32TrVmt", -}, - -["3a"] = { -- [66] 0f 3a xx ---0x -[0x00]=nil,nil,nil,nil,nil,nil,nil,nil, -"||roundpsXrmu","||roundpdXrmu","||roundssXrmu","||roundsdXrmu", -"||blendpsXrmu","||blendpdXrmu","||pblendwXrmu","palignrPrmu", ---1x -nil,nil,nil,nil, -"||pextrbVmXru","||pextrwVmXru","||pextrVmSXru","||extractpsVmXru", -nil,nil,nil,nil,nil,nil,nil,nil, ---2x -"||pinsrbXrVmu","||insertpsXrmu","||pinsrXrVmuS",nil, ---4x -[0x40] = "||dppsXrmu", -[0x41] = "||dppdXrmu", -[0x42] = "||mpsadbwXrmu", ---6x -[0x60] = "||pcmpestrmXrmu",[0x61] = "||pcmpestriXrmu", -[0x62] = "||pcmpistrmXrmu",[0x63] = "||pcmpistriXrmu", -}, -} - --- Map for VMX/SVM opcodes 0F 01 C0-FF (sgdt group with register operands). -local map_opcvm = { -[0xc1]="vmcall",[0xc2]="vmlaunch",[0xc3]="vmresume",[0xc4]="vmxoff", -[0xc8]="monitor",[0xc9]="mwait", -[0xd8]="vmrun",[0xd9]="vmmcall",[0xda]="vmload",[0xdb]="vmsave", -[0xdc]="stgi",[0xdd]="clgi",[0xde]="skinit",[0xdf]="invlpga", -[0xf8]="swapgs",[0xf9]="rdtscp", -} - --- Map for FP opcodes. And you thought stack machines are simple? -local map_opcfp = { --- D8-DF 00-BF: opcodes with a memory operand. --- D8 -[0]="faddFm","fmulFm","fcomFm","fcompFm","fsubFm","fsubrFm","fdivFm","fdivrFm", -"fldFm",nil,"fstFm","fstpFm","fldenvVm","fldcwWm","fnstenvVm","fnstcwWm", --- DA -"fiaddDm","fimulDm","ficomDm","ficompDm", -"fisubDm","fisubrDm","fidivDm","fidivrDm", --- DB -"fildDm","fisttpDm","fistDm","fistpDm",nil,"fld twordFmp",nil,"fstp twordFmp", --- DC -"faddGm","fmulGm","fcomGm","fcompGm","fsubGm","fsubrGm","fdivGm","fdivrGm", --- DD -"fldGm","fisttpQm","fstGm","fstpGm","frstorDmp",nil,"fnsaveDmp","fnstswWm", --- DE -"fiaddWm","fimulWm","ficomWm","ficompWm", -"fisubWm","fisubrWm","fidivWm","fidivrWm", --- DF -"fildWm","fisttpWm","fistWm","fistpWm", -"fbld twordFmp","fildQm","fbstp twordFmp","fistpQm", --- xx C0-FF: opcodes with a pseudo-register operand. --- D8 -"faddFf","fmulFf","fcomFf","fcompFf","fsubFf","fsubrFf","fdivFf","fdivrFf", --- D9 -"fldFf","fxchFf",{"fnop"},nil, -{"fchs","fabs",nil,nil,"ftst","fxam"}, -{"fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz"}, -{"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp"}, -{"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"}, --- DA -"fcmovbFf","fcmoveFf","fcmovbeFf","fcmovuFf",nil,{nil,"fucompp"},nil,nil, --- DB -"fcmovnbFf","fcmovneFf","fcmovnbeFf","fcmovnuFf", -{nil,nil,"fnclex","fninit"},"fucomiFf","fcomiFf",nil, --- DC -"fadd toFf","fmul toFf",nil,nil, -"fsub toFf","fsubr toFf","fdivr toFf","fdiv toFf", --- DD -"ffreeFf",nil,"fstFf","fstpFf","fucomFf","fucompFf",nil,nil, --- DE -"faddpFf","fmulpFf",nil,{nil,"fcompp"}, -"fsubrpFf","fsubpFf","fdivrpFf","fdivpFf", --- DF -nil,nil,nil,nil,{"fnstsw ax"},"fucomipFf","fcomipFf",nil, -} -assert(map_opcfp[126] == "fcomipFf") - --- Map for opcode groups. The subkey is sp from the ModRM byte. -local map_opcgroup = { - arith = { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" }, - shift = { "rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar" }, - testb = { "testBmi", "testBmi", "not", "neg", "mul", "imul", "div", "idiv" }, - testv = { "testVmi", "testVmi", "not", "neg", "mul", "imul", "div", "idiv" }, - incb = { "inc", "dec" }, - incd = { "inc", "dec", "callUmp", "$call farDmp", - "jmpUmp", "$jmp farDmp", "pushUm" }, - sldt = { "sldt", "str", "lldt", "ltr", "verr", "verw" }, - sgdt = { "vm*$sgdt", "vm*$sidt", "$lgdt", "vm*$lidt", - "smsw", nil, "lmsw", "vm*$invlpg" }, - bt = { nil, nil, nil, nil, "bt", "bts", "btr", "btc" }, - cmpxchg = { nil, "sz*,cmpxchg8bQmp,cmpxchg16bXmp", nil, nil, - nil, nil, "vmptrld|vmxon|vmclear", "vmptrst" }, - pshiftw = { nil, nil, "psrlw", nil, "psraw", nil, "psllw" }, - pshiftd = { nil, nil, "psrld", nil, "psrad", nil, "pslld" }, - pshiftq = { nil, nil, "psrlq", nil, nil, nil, "psllq" }, - pshiftdq = { nil, nil, "psrlq", "psrldq", nil, nil, "psllq", "pslldq" }, - fxsave = { "$fxsave", "$fxrstor", "$ldmxcsr", "$stmxcsr", - nil, "lfenceDp$", "mfenceDp$", "sfenceDp$clflush" }, - prefetch = { "prefetch", "prefetchw" }, - prefetcht = { "prefetchnta", "prefetcht0", "prefetcht1", "prefetcht2" }, -} - ------------------------------------------------------------------------------- - --- Maps for register names. -local map_regs = { - B = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh", - "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" }, - B64 = { "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", - "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" }, - W = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", - "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" }, - D = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", - "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" }, - Q = { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", - "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" }, - M = { "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7", - "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7" }, -- No x64 ext! - X = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", - "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15" }, -} -local map_segregs = { "es", "cs", "ss", "ds", "fs", "gs", "segr6", "segr7" } - --- Maps for size names. -local map_sz2n = { - B = 1, W = 2, D = 4, Q = 8, M = 8, X = 16, -} -local map_sz2prefix = { - B = "byte", W = "word", D = "dword", - Q = "qword", - M = "qword", X = "xword", - F = "dword", G = "qword", -- No need for sizes/register names for these two. -} - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local code, pos, hex = ctx.code, ctx.pos, "" - local hmax = ctx.hexdump - if hmax > 0 then - for i=ctx.start,pos-1 do - hex = hex..format("%02X", byte(code, i, i)) - end - if #hex > hmax then hex = sub(hex, 1, hmax)..". " - else hex = hex..rep(" ", hmax-#hex+2) end - end - if operands then text = text.." "..operands end - if ctx.o16 then text = "o16 "..text; ctx.o16 = false end - if ctx.a32 then text = "a32 "..text; ctx.a32 = false end - if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end - if ctx.rex then - local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "").. - (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "") - if t ~= "" then text = "rex."..t.." "..text end - ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false - ctx.rex = false - end - if ctx.seg then - local text2, n = gsub(text, "%[", "["..ctx.seg..":") - if n == 0 then text = ctx.seg.." "..text else text = text2 end - ctx.seg = false - end - if ctx.lock then text = "lock "..text; ctx.lock = false end - local imm = ctx.imm - if imm then - local sym = ctx.symtab[imm] - if sym then text = text.."\t->"..sym end - end - ctx.out(format("%08x %s%s\n", ctx.addr+ctx.start, hex, text)) - ctx.mrm = false - ctx.start = pos - ctx.imm = nil -end - --- Clear all prefix flags. -local function clearprefixes(ctx) - ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false - ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false - ctx.rex = false; ctx.a32 = false -end - --- Fallback for incomplete opcodes at the end. -local function incomplete(ctx) - ctx.pos = ctx.stop+1 - clearprefixes(ctx) - return putop(ctx, "(incomplete)") -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - clearprefixes(ctx) - return putop(ctx, "(unknown)") -end - --- Return an immediate of the specified size. -local function getimm(ctx, pos, n) - if pos+n-1 > ctx.stop then return incomplete(ctx) end - local code = ctx.code - if n == 1 then - local b1 = byte(code, pos, pos) - return b1 - elseif n == 2 then - local b1, b2 = byte(code, pos, pos+1) - return b1+b2*256 - else - local b1, b2, b3, b4 = byte(code, pos, pos+3) - local imm = b1+b2*256+b3*65536+b4*16777216 - ctx.imm = imm - return imm - end -end - --- Process pattern string and generate the operands. -local function putpat(ctx, name, pat) - local operands, regs, sz, mode, sp, rm, sc, rx, sdisp - local code, pos, stop = ctx.code, ctx.pos, ctx.stop - - -- Chars used: 1DFGIMPQRSTUVWXacdfgijmoprstuwxyz - for p in gmatch(pat, ".") do - local x = nil - if p == "V" or p == "U" then - if ctx.rexw then sz = "Q"; ctx.rexw = false - elseif ctx.o16 then sz = "W"; ctx.o16 = false - elseif p == "U" and ctx.x64 then sz = "Q" - else sz = "D" end - regs = map_regs[sz] - elseif p == "T" then - if ctx.rexw then sz = "Q"; ctx.rexw = false else sz = "D" end - regs = map_regs[sz] - elseif p == "B" then - sz = "B" - regs = ctx.rex and map_regs.B64 or map_regs.B - elseif match(p, "[WDQMXFG]") then - sz = p - regs = map_regs[sz] - elseif p == "P" then - sz = ctx.o16 and "X" or "M"; ctx.o16 = false - regs = map_regs[sz] - elseif p == "S" then - name = name..lower(sz) - elseif p == "s" then - local imm = getimm(ctx, pos, 1); if not imm then return end - x = imm <= 127 and format("+0x%02x", imm) - or format("-0x%02x", 256-imm) - pos = pos+1 - elseif p == "u" then - local imm = getimm(ctx, pos, 1); if not imm then return end - x = format("0x%02x", imm) - pos = pos+1 - elseif p == "w" then - local imm = getimm(ctx, pos, 2); if not imm then return end - x = format("0x%x", imm) - pos = pos+2 - elseif p == "o" then -- [offset] - if ctx.x64 then - local imm1 = getimm(ctx, pos, 4); if not imm1 then return end - local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end - x = format("[0x%08x%08x]", imm2, imm1) - pos = pos+8 - else - local imm = getimm(ctx, pos, 4); if not imm then return end - x = format("[0x%08x]", imm) - pos = pos+4 - end - elseif p == "i" or p == "I" then - local n = map_sz2n[sz] - if n == 8 and ctx.x64 and p == "I" then - local imm1 = getimm(ctx, pos, 4); if not imm1 then return end - local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end - x = format("0x%08x%08x", imm2, imm1) - else - if n == 8 then n = 4 end - local imm = getimm(ctx, pos, n); if not imm then return end - if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then - imm = (0xffffffff+1)-imm - x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm) - else - x = format(imm > 65535 and "0x%08x" or "0x%x", imm) - end - end - pos = pos+n - elseif p == "j" then - local n = map_sz2n[sz] - if n == 8 then n = 4 end - local imm = getimm(ctx, pos, n); if not imm then return end - if sz == "B" and imm > 127 then imm = imm-256 - elseif imm > 2147483647 then imm = imm-4294967296 end - pos = pos+n - imm = imm + pos + ctx.addr - if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end - ctx.imm = imm - if sz == "W" then - x = format("word 0x%04x", imm%65536) - elseif ctx.x64 then - local lo = imm % 0x1000000 - x = format("0x%02x%06x", (imm-lo) / 0x1000000, lo) - else - x = format("0x%08x", imm) - end - elseif p == "R" then - local r = byte(code, pos-1, pos-1)%8 - if ctx.rexb then r = r + 8; ctx.rexb = false end - x = regs[r+1] - elseif p == "a" then x = regs[1] - elseif p == "c" then x = "cl" - elseif p == "d" then x = "dx" - elseif p == "1" then x = "1" - else - if not mode then - mode = ctx.mrm - if not mode then - if pos > stop then return incomplete(ctx) end - mode = byte(code, pos, pos) - pos = pos+1 - end - rm = mode%8; mode = (mode-rm)/8 - sp = mode%8; mode = (mode-sp)/8 - sdisp = "" - if mode < 3 then - if rm == 4 then - if pos > stop then return incomplete(ctx) end - sc = byte(code, pos, pos) - pos = pos+1 - rm = sc%8; sc = (sc-rm)/8 - rx = sc%8; sc = (sc-rx)/8 - if ctx.rexx then rx = rx + 8; ctx.rexx = false end - if rx == 4 then rx = nil end - end - if mode > 0 or rm == 5 then - local dsz = mode - if dsz ~= 1 then dsz = 4 end - local disp = getimm(ctx, pos, dsz); if not disp then return end - if mode == 0 then rm = nil end - if rm or rx or (not sc and ctx.x64 and not ctx.a32) then - if dsz == 1 and disp > 127 then - sdisp = format("-0x%x", 256-disp) - elseif disp >= 0 and disp <= 0x7fffffff then - sdisp = format("+0x%x", disp) - else - sdisp = format("-0x%x", (0xffffffff+1)-disp) - end - else - sdisp = format(ctx.x64 and not ctx.a32 and - not (disp >= 0 and disp <= 0x7fffffff) - and "0xffffffff%08x" or "0x%08x", disp) - end - pos = pos+dsz - end - end - if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end - if ctx.rexr then sp = sp + 8; ctx.rexr = false end - end - if p == "m" then - if mode == 3 then x = regs[rm+1] - else - local aregs = ctx.a32 and map_regs.D or ctx.aregs - local srm, srx = "", "" - if rm then srm = aregs[rm+1] - elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end - ctx.a32 = false - if rx then - if rm then srm = srm.."+" end - srx = aregs[rx+1] - if sc > 0 then srx = srx.."*"..(2^sc) end - end - x = format("[%s%s%s]", srm, srx, sdisp) - end - if mode < 3 and - (not match(pat, "[aRrgp]") or match(pat, "t")) then -- Yuck. - x = map_sz2prefix[sz].." "..x - end - elseif p == "r" then x = regs[sp+1] - elseif p == "g" then x = map_segregs[sp+1] - elseif p == "p" then -- Suppress prefix. - elseif p == "f" then x = "st"..rm - elseif p == "x" then - if sp == 0 and ctx.lock and not ctx.x64 then - x = "CR8"; ctx.lock = false - else - x = "CR"..sp - end - elseif p == "y" then x = "DR"..sp - elseif p == "z" then x = "TR"..sp - elseif p == "t" then - else - error("bad pattern `"..pat.."'") - end - end - if x then operands = operands and operands..", "..x or x end - end - ctx.pos = pos - return putop(ctx, name, operands) -end - --- Forward declaration. -local map_act - --- Fetch and cache MRM byte. -local function getmrm(ctx) - local mrm = ctx.mrm - if not mrm then - local pos = ctx.pos - if pos > ctx.stop then return nil end - mrm = byte(ctx.code, pos, pos) - ctx.pos = pos+1 - ctx.mrm = mrm - end - return mrm -end - --- Dispatch to handler depending on pattern. -local function dispatch(ctx, opat, patgrp) - if not opat then return unknown(ctx) end - if match(opat, "%|") then -- MMX/SSE variants depending on prefix. - local p - if ctx.rep then - p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)" - ctx.rep = false - elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false - else p = "^[^%|]*" end - opat = match(opat, p) - if not opat then return unknown(ctx) end --- ctx.rep = false; ctx.o16 = false - --XXX fails for 66 f2 0f 38 f1 06 crc32 eax,WORD PTR [esi] - --XXX remove in branches? - end - if match(opat, "%$") then -- reg$mem variants. - local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)") - if opat == "" then return unknown(ctx) end - end - if opat == "" then return unknown(ctx) end - local name, pat = match(opat, "^([a-z0-9 ]*)(.*)") - if pat == "" and patgrp then pat = patgrp end - return map_act[sub(pat, 1, 1)](ctx, name, pat) -end - --- Get a pattern from an opcode map and dispatch to handler. -local function dispatchmap(ctx, opcmap) - local pos = ctx.pos - local opat = opcmap[byte(ctx.code, pos, pos)] - pos = pos + 1 - ctx.pos = pos - return dispatch(ctx, opat) -end - --- Map for action codes. The key is the first char after the name. -map_act = { - -- Simple opcodes without operands. - [""] = function(ctx, name, pat) - return putop(ctx, name) - end, - - -- Operand size chars fall right through. - B = putpat, W = putpat, D = putpat, Q = putpat, - V = putpat, U = putpat, T = putpat, - M = putpat, X = putpat, P = putpat, - F = putpat, G = putpat, - - -- Collect prefixes. - [":"] = function(ctx, name, pat) - ctx[pat == ":" and name or sub(pat, 2)] = name - if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes. - end, - - -- Chain to special handler specified by name. - ["*"] = function(ctx, name, pat) - return map_act[name](ctx, name, sub(pat, 2)) - end, - - -- Use named subtable for opcode group. - ["!"] = function(ctx, name, pat) - local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2)) - end, - - -- o16,o32[,o64] variants. - sz = function(ctx, name, pat) - if ctx.o16 then ctx.o16 = false - else - pat = match(pat, ",(.*)") - if ctx.rexw then - local p = match(pat, ",(.*)") - if p then pat = p; ctx.rexw = false end - end - end - pat = match(pat, "^[^,]*") - return dispatch(ctx, pat) - end, - - -- Two-byte opcode dispatch. - opc2 = function(ctx, name, pat) - return dispatchmap(ctx, map_opc2) - end, - - -- Three-byte opcode dispatch. - opc3 = function(ctx, name, pat) - return dispatchmap(ctx, map_opc3[pat]) - end, - - -- VMX/SVM dispatch. - vm = function(ctx, name, pat) - return dispatch(ctx, map_opcvm[ctx.mrm]) - end, - - -- Floating point opcode dispatch. - fp = function(ctx, name, pat) - local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - local rm = mrm%8 - local idx = pat*8 + ((mrm-rm)/8)%8 - if mrm >= 192 then idx = idx + 64 end - local opat = map_opcfp[idx] - if type(opat) == "table" then opat = opat[rm+1] end - return dispatch(ctx, opat) - end, - - -- REX prefix. - rex = function(ctx, name, pat) - if ctx.rex then return unknown(ctx) end -- Only 1 REX prefix allowed. - for p in gmatch(pat, ".") do ctx["rex"..p] = true end - ctx.rex = true - end, - - -- Special case for nop with REX prefix. - nop = function(ctx, name, pat) - return dispatch(ctx, ctx.rex and pat or "nop") - end, -} - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - ofs = ofs + 1 - ctx.start = ofs - ctx.pos = ofs - ctx.stop = stop - ctx.imm = nil - ctx.mrm = false - clearprefixes(ctx) - while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end - if ctx.pos ~= ctx.start then incomplete(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create_(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = (addr or 0) - 1 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 16 - ctx.x64 = false - ctx.map1 = map_opc1_32 - ctx.aregs = map_regs.D - return ctx -end - -local function create64_(code, addr, out) - local ctx = create_(code, addr, out) - ctx.x64 = true - ctx.map1 = map_opc1_64 - ctx.aregs = map_regs.Q - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass_(code, addr, out) - create_(code, addr, out):disass() -end - -local function disass64_(code, addr, out) - create64_(code, addr, out):disass() -end - --- Return register name for RID. -local function regname_(r) - if r < 8 then return map_regs.D[r+1] end - return map_regs.X[r-7] -end - -local function regname64_(r) - if r < 16 then return map_regs.Q[r+1] end - return map_regs.X[r-15] -end - --- Public module functions. -module(...) - -create = create_ -create64 = create64_ -disass = disass_ -disass64 = disass64_ -regname = regname_ -regname64 = regname64_ - diff --git a/third-party/LuaJIT-2.0.2/src/jit/dump.lua b/third-party/LuaJIT-2.0.2/src/jit/dump.lua deleted file mode 100644 index 7441b74dbf..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/dump.lua +++ /dev/null @@ -1,700 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT compiler dump module. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module can be used to debug the JIT compiler itself. It dumps the --- code representations and structures used in various compiler stages. --- --- Example usage: --- --- luajit -jdump -e "local x=0; for i=1,1e6 do x=x+i end; print(x)" --- luajit -jdump=im -e "for i=1,1000 do for j=1,1000 do end end" | less -R --- luajit -jdump=is myapp.lua | less -R --- luajit -jdump=-b myapp.lua --- luajit -jdump=+aH,myapp.html myapp.lua --- luajit -jdump=ixT,myapp.dump myapp.lua --- --- The first argument specifies the dump mode. The second argument gives --- the output file name. Default output is to stdout, unless the environment --- variable LUAJIT_DUMPFILE is set. The file is overwritten every time the --- module is started. --- --- Different features can be turned on or off with the dump mode. If the --- mode starts with a '+', the following features are added to the default --- set of features; a '-' removes them. Otherwise the features are replaced. --- --- The following dump features are available (* marks the default): --- --- * t Print a line for each started, ended or aborted trace (see also -jv). --- * b Dump the traced bytecode. --- * i Dump the IR (intermediate representation). --- r Augment the IR with register/stack slots. --- s Dump the snapshot map. --- * m Dump the generated machine code. --- x Print each taken trace exit. --- X Print each taken trace exit and the contents of all registers. --- --- The output format can be set with the following characters: --- --- T Plain text output. --- A ANSI-colored text output --- H Colorized HTML + CSS output. --- --- The default output format is plain text. It's set to ANSI-colored text --- if the COLORTERM variable is set. Note: this is independent of any output --- redirection, which is actually considered a feature. --- --- You probably want to use less -R to enjoy viewing ANSI-colored text from --- a pipe or a file. Add this to your ~/.bashrc: export LESS="-R" --- ------------------------------------------------------------------------------- - --- Cache some library functions and objects. -local jit = require("jit") -assert(jit.version_num == 20002, "LuaJIT core/library version mismatch") -local jutil = require("jit.util") -local vmdef = require("jit.vmdef") -local funcinfo, funcbc = jutil.funcinfo, jutil.funcbc -local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek -local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap -local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr -local bit = require("bit") -local band, shl, shr = bit.band, bit.lshift, bit.rshift -local sub, gsub, format = string.sub, string.gsub, string.format -local byte, char, rep = string.byte, string.char, string.rep -local type, tostring = type, tostring -local stdout, stderr = io.stdout, io.stderr - --- Load other modules on-demand. -local bcline, disass - --- Active flag, output file handle and dump mode. -local active, out, dumpmode - ------------------------------------------------------------------------------- - -local symtabmt = { __index = false } -local symtab = {} -local nexitsym = 0 - --- Fill nested symbol table with per-trace exit stub addresses. -local function fillsymtab_tr(tr, nexit) - local t = {} - symtabmt.__index = t - if jit.arch == "mips" or jit.arch == "mipsel" then - t[traceexitstub(tr, 0)] = "exit" - return - end - for i=0,nexit-1 do - local addr = traceexitstub(tr, i) - t[addr] = tostring(i) - end - local addr = traceexitstub(tr, nexit) - if addr then t[addr] = "stack_check" end -end - --- Fill symbol table with trace exit stub addresses. -local function fillsymtab(tr, nexit) - local t = symtab - if nexitsym == 0 then - local ircall = vmdef.ircall - for i=0,#ircall do - local addr = ircalladdr(i) - if addr ~= 0 then t[addr] = ircall[i] end - end - end - if nexitsym == 1000000 then -- Per-trace exit stubs. - fillsymtab_tr(tr, nexit) - elseif nexit > nexitsym then -- Shared exit stubs. - for i=nexitsym,nexit-1 do - local addr = traceexitstub(i) - if addr == nil then -- Fall back to per-trace exit stubs. - fillsymtab_tr(tr, nexit) - setmetatable(symtab, symtabmt) - nexit = 1000000 - break - end - t[addr] = tostring(i) - end - nexitsym = nexit - end - return t -end - -local function dumpwrite(s) - out:write(s) -end - --- Disassemble machine code. -local function dump_mcode(tr) - local info = traceinfo(tr) - if not info then return end - local mcode, addr, loop = tracemc(tr) - if not mcode then return end - if not disass then disass = require("jit.dis_"..jit.arch) end - out:write("---- TRACE ", tr, " mcode ", #mcode, "\n") - local ctx = disass.create(mcode, addr, dumpwrite) - ctx.hexdump = 0 - ctx.symtab = fillsymtab(tr, info.nexit) - if loop ~= 0 then - symtab[addr+loop] = "LOOP" - ctx:disass(0, loop) - out:write("->LOOP:\n") - ctx:disass(loop, #mcode-loop) - symtab[addr+loop] = nil - else - ctx:disass(0, #mcode) - end -end - ------------------------------------------------------------------------------- - -local irtype_text = { - [0] = "nil", - "fal", - "tru", - "lud", - "str", - "p32", - "thr", - "pro", - "fun", - "p64", - "cdt", - "tab", - "udt", - "flt", - "num", - "i8 ", - "u8 ", - "i16", - "u16", - "int", - "u32", - "i64", - "u64", - "sfp", -} - -local colortype_ansi = { - [0] = "%s", - "%s", - "%s", - "\027[36m%s\027[m", - "\027[32m%s\027[m", - "%s", - "\027[1m%s\027[m", - "%s", - "\027[1m%s\027[m", - "%s", - "\027[33m%s\027[m", - "\027[31m%s\027[m", - "\027[36m%s\027[m", - "\027[34m%s\027[m", - "\027[34m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", -} - -local function colorize_text(s, t) - return s -end - -local function colorize_ansi(s, t) - return format(colortype_ansi[t], s) -end - -local irtype_ansi = setmetatable({}, - { __index = function(tab, t) - local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end }) - -local html_escape = { ["<"] = "<", [">"] = ">", ["&"] = "&", } - -local function colorize_html(s, t) - s = gsub(s, "[<>&]", html_escape) - return format('%s', irtype_text[t], s) -end - -local irtype_html = setmetatable({}, - { __index = function(tab, t) - local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end }) - -local header_html = [[ - -]] - -local colorize, irtype - --- Lookup tables to convert some literals into names. -local litname = { - ["SLOAD "] = setmetatable({}, { __index = function(t, mode) - local s = "" - if band(mode, 1) ~= 0 then s = s.."P" end - if band(mode, 2) ~= 0 then s = s.."F" end - if band(mode, 4) ~= 0 then s = s.."T" end - if band(mode, 8) ~= 0 then s = s.."C" end - if band(mode, 16) ~= 0 then s = s.."R" end - if band(mode, 32) ~= 0 then s = s.."I" end - t[mode] = s - return s - end}), - ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", }, - ["CONV "] = setmetatable({}, { __index = function(t, mode) - local s = irtype[band(mode, 31)] - s = irtype[band(shr(mode, 5), 31)].."."..s - if band(mode, 0x400) ~= 0 then s = s.." trunc" - elseif band(mode, 0x800) ~= 0 then s = s.." sext" end - local c = shr(mode, 14) - if c == 2 then s = s.." index" elseif c == 3 then s = s.." check" end - t[mode] = s - return s - end}), - ["FLOAD "] = vmdef.irfield, - ["FREF "] = vmdef.irfield, - ["FPMATH"] = vmdef.irfpm, -} - -local function ctlsub(c) - if c == "\n" then return "\\n" - elseif c == "\r" then return "\\r" - elseif c == "\t" then return "\\t" - else return format("\\%03d", byte(c)) - end -end - -local function fmtfunc(func, pc) - local fi = funcinfo(func, pc) - if fi.loc then - return fi.loc - elseif fi.ffid then - return vmdef.ffnames[fi.ffid] - elseif fi.addr then - return format("C:%x", fi.addr) - else - return "(?)" - end -end - -local function formatk(tr, idx) - local k, t, slot = tracek(tr, idx) - local tn = type(k) - local s - if tn == "number" then - if k == 2^52+2^51 then - s = "bias" - else - s = format("%+.14g", k) - end - elseif tn == "string" then - s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub)) - elseif tn == "function" then - s = fmtfunc(k) - elseif tn == "table" then - s = format("{%p}", k) - elseif tn == "userdata" then - if t == 12 then - s = format("userdata:%p", k) - else - s = format("[%p]", k) - if s == "[0x00000000]" then s = "NULL" end - end - elseif t == 21 then -- int64_t - s = sub(tostring(k), 1, -3) - if sub(s, 1, 1) ~= "-" then s = "+"..s end - else - s = tostring(k) -- For primitives. - end - s = colorize(format("%-4s", s), t) - if slot then - s = format("%s @%d", s, slot) - end - return s -end - -local function printsnap(tr, snap) - local n = 2 - for s=0,snap[1]-1 do - local sn = snap[n] - if shr(sn, 24) == s then - n = n + 1 - local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS - if ref < 0 then - out:write(formatk(tr, ref)) - elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM - out:write(colorize(format("%04d/%04d", ref, ref+1), 14)) - else - local m, ot, op1, op2 = traceir(tr, ref) - out:write(colorize(format("%04d", ref), band(ot, 31))) - end - out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME - else - out:write("---- ") - end - end - out:write("]\n") -end - --- Dump snapshots (not interleaved with IR). -local function dump_snap(tr) - out:write("---- TRACE ", tr, " snapshots\n") - for i=0,1000000000 do - local snap = tracesnap(tr, i) - if not snap then break end - out:write(format("#%-3d %04d [ ", i, snap[0])) - printsnap(tr, snap) - end -end - --- Return a register name or stack slot for a rid/sp location. -local function ridsp_name(ridsp, ins) - if not disass then disass = require("jit.dis_"..jit.arch) end - local rid, slot = band(ridsp, 0xff), shr(ridsp, 8) - if rid == 253 or rid == 254 then - return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot) - end - if ridsp > 255 then return format("[%x]", slot*4) end - if rid < 128 then return disass.regname(rid) end - return "" -end - --- Dump CALL* function ref and return optional ctype. -local function dumpcallfunc(tr, ins) - local ctype - if ins > 0 then - local m, ot, op1, op2 = traceir(tr, ins) - if band(ot, 31) == 0 then -- nil type means CARG(func, ctype). - ins = op1 - ctype = formatk(tr, op2) - end - end - if ins < 0 then - out:write(format("[0x%x](", tonumber((tracek(tr, ins))))) - else - out:write(format("%04d (", ins)) - end - return ctype -end - --- Recursively gather CALL* args and dump them. -local function dumpcallargs(tr, ins) - if ins < 0 then - out:write(formatk(tr, ins)) - else - local m, ot, op1, op2 = traceir(tr, ins) - local oidx = 6*shr(ot, 8) - local op = sub(vmdef.irnames, oidx+1, oidx+6) - if op == "CARG " then - dumpcallargs(tr, op1) - if op2 < 0 then - out:write(" ", formatk(tr, op2)) - else - out:write(" ", format("%04d", op2)) - end - else - out:write(format("%04d", ins)) - end - end -end - --- Dump IR and interleaved snapshots. -local function dump_ir(tr, dumpsnap, dumpreg) - local info = traceinfo(tr) - if not info then return end - local nins = info.nins - out:write("---- TRACE ", tr, " IR\n") - local irnames = vmdef.irnames - local snapref = 65536 - local snap, snapno - if dumpsnap then - snap = tracesnap(tr, 0) - snapref = snap[0] - snapno = 0 - end - for ins=1,nins do - if ins >= snapref then - if dumpreg then - out:write(format(".... SNAP #%-3d [ ", snapno)) - else - out:write(format(".... SNAP #%-3d [ ", snapno)) - end - printsnap(tr, snap) - snapno = snapno + 1 - snap = tracesnap(tr, snapno) - snapref = snap and snap[0] or 65536 - end - local m, ot, op1, op2, ridsp = traceir(tr, ins) - local oidx, t = 6*shr(ot, 8), band(ot, 31) - local op = sub(irnames, oidx+1, oidx+6) - if op == "LOOP " then - if dumpreg then - out:write(format("%04d ------------ LOOP ------------\n", ins)) - else - out:write(format("%04d ------ LOOP ------------\n", ins)) - end - elseif op ~= "NOP " and op ~= "CARG " and - (dumpreg or op ~= "RENAME") then - local rid = band(ridsp, 255) - if dumpreg then - out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins))) - else - out:write(format("%04d ", ins)) - end - out:write(format("%s%s %s %s ", - (rid == 254 or rid == 253) and "}" or - (band(ot, 128) == 0 and " " or ">"), - band(ot, 64) == 0 and " " or "+", - irtype[t], op)) - local m1, m2 = band(m, 3), band(m, 3*4) - if sub(op, 1, 4) == "CALL" then - local ctype - if m2 == 1*4 then -- op2 == IRMlit - out:write(format("%-10s (", vmdef.ircall[op2])) - else - ctype = dumpcallfunc(tr, op2) - end - if op1 ~= -1 then dumpcallargs(tr, op1) end - out:write(")") - if ctype then out:write(" ctype ", ctype) end - elseif op == "CNEW " and op2 == -1 then - out:write(formatk(tr, op1)) - elseif m1 ~= 3 then -- op1 != IRMnone - if op1 < 0 then - out:write(formatk(tr, op1)) - else - out:write(format(m1 == 0 and "%04d" or "#%-3d", op1)) - end - if m2 ~= 3*4 then -- op2 != IRMnone - if m2 == 1*4 then -- op2 == IRMlit - local litn = litname[op] - if litn and litn[op2] then - out:write(" ", litn[op2]) - elseif op == "UREFO " or op == "UREFC " then - out:write(format(" #%-3d", shr(op2, 8))) - else - out:write(format(" #%-3d", op2)) - end - elseif op2 < 0 then - out:write(" ", formatk(tr, op2)) - else - out:write(format(" %04d", op2)) - end - end - end - out:write("\n") - end - end - if snap then - if dumpreg then - out:write(format(".... SNAP #%-3d [ ", snapno)) - else - out:write(format(".... SNAP #%-3d [ ", snapno)) - end - printsnap(tr, snap) - end -end - ------------------------------------------------------------------------------- - -local recprefix = "" -local recdepth = 0 - --- Format trace error message. -local function fmterr(err, info) - if type(err) == "number" then - if type(info) == "function" then info = fmtfunc(info) end - err = format(vmdef.traceerr[err], info) - end - return err -end - --- Dump trace states. -local function dump_trace(what, tr, func, pc, otr, oex) - if what == "stop" or (what == "abort" and dumpmode.a) then - if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop") - elseif dumpmode.s then dump_snap(tr) end - if dumpmode.m then dump_mcode(tr) end - end - if what == "start" then - if dumpmode.H then out:write('
\n') end
-    out:write("---- TRACE ", tr, " ", what)
-    if otr then out:write(" ", otr, "/", oex) end
-    out:write(" ", fmtfunc(func, pc), "\n")
-    recprefix = ""
-  elseif what == "stop" or what == "abort" then
-    out:write("---- TRACE ", tr, " ", what)
-    recprefix = nil
-    if what == "abort" then
-      out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
-    else
-      local info = traceinfo(tr)
-      local link, ltype = info.link, info.linktype
-      if link == tr or link == 0 then
-	out:write(" -> ", ltype, "\n")
-      elseif ltype == "root" then
-	out:write(" -> ", link, "\n")
-      else
-	out:write(" -> ", link, " ", ltype, "\n")
-      end
-    end
-    if dumpmode.H then out:write("
\n\n") else out:write("\n") end - else - out:write("---- TRACE ", what, "\n\n") - end - out:flush() -end - --- Dump recorded bytecode. -local function dump_record(tr, func, pc, depth, callee) - if depth ~= recdepth then - recdepth = depth - recprefix = rep(" .", depth) - end - local line - if pc >= 0 then - line = bcline(func, pc, recprefix) - if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end - else - line = "0000 "..recprefix.." FUNCC \n" - callee = func - end - if pc <= 0 then - out:write(sub(line, 1, -2), " ; ", fmtfunc(func), "\n") - else - out:write(line) - end - if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC - out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond. - end -end - ------------------------------------------------------------------------------- - --- Dump taken trace exits. -local function dump_texit(tr, ex, ngpr, nfpr, ...) - out:write("---- TRACE ", tr, " exit ", ex, "\n") - if dumpmode.X then - local regs = {...} - if jit.arch == "x64" then - for i=1,ngpr do - out:write(format(" %016x", regs[i])) - if i % 4 == 0 then out:write("\n") end - end - else - for i=1,ngpr do - out:write(format(" %08x", regs[i])) - if i % 8 == 0 then out:write("\n") end - end - end - if jit.arch == "mips" or jit.arch == "mipsel" then - for i=1,nfpr,2 do - out:write(format(" %+17.14g", regs[ngpr+i])) - if i % 8 == 7 then out:write("\n") end - end - else - for i=1,nfpr do - out:write(format(" %+17.14g", regs[ngpr+i])) - if i % 4 == 0 then out:write("\n") end - end - end - end -end - ------------------------------------------------------------------------------- - --- Detach dump handlers. -local function dumpoff() - if active then - active = false - jit.attach(dump_texit) - jit.attach(dump_record) - jit.attach(dump_trace) - if out and out ~= stdout and out ~= stderr then out:close() end - out = nil - end -end - --- Open the output file and attach dump handlers. -local function dumpon(opt, outfile) - if active then dumpoff() end - - local colormode = os.getenv("COLORTERM") and "A" or "T" - if opt then - opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end) - end - - local m = { t=true, b=true, i=true, m=true, } - if opt and opt ~= "" then - local o = sub(opt, 1, 1) - if o ~= "+" and o ~= "-" then m = {} end - for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end - end - dumpmode = m - - if m.t or m.b or m.i or m.s or m.m then - jit.attach(dump_trace, "trace") - end - if m.b then - jit.attach(dump_record, "record") - if not bcline then bcline = require("jit.bc").line end - end - if m.x or m.X then - jit.attach(dump_texit, "texit") - end - - if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end - if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) - else - out = stdout - end - - m[colormode] = true - if colormode == "A" then - colorize = colorize_ansi - irtype = irtype_ansi - elseif colormode == "H" then - colorize = colorize_html - irtype = irtype_html - out:write(header_html) - else - colorize = colorize_text - irtype = irtype_text - end - - active = true -end - --- Public module functions. -module(...) - -on = dumpon -off = dumpoff -start = dumpon -- For -j command line option. - diff --git a/third-party/LuaJIT-2.0.2/src/jit/v.lua b/third-party/LuaJIT-2.0.2/src/jit/v.lua deleted file mode 100644 index d7df71262b..0000000000 --- a/third-party/LuaJIT-2.0.2/src/jit/v.lua +++ /dev/null @@ -1,167 +0,0 @@ ----------------------------------------------------------------------------- --- Verbose mode of the LuaJIT compiler. --- --- Copyright (C) 2005-2013 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module shows verbose information about the progress of the --- JIT compiler. It prints one line for each generated trace. This module --- is useful to see which code has been compiled or where the compiler --- punts and falls back to the interpreter. --- --- Example usage: --- --- luajit -jv -e "for i=1,1000 do for j=1,1000 do end end" --- luajit -jv=myapp.out myapp.lua --- --- Default output is to stderr. To redirect the output to a file, pass a --- filename as an argument (use '-' for stdout) or set the environment --- variable LUAJIT_VERBOSEFILE. The file is overwritten every time the --- module is started. --- --- The output from the first example should look like this: --- --- [TRACE 1 (command line):1 loop] --- [TRACE 2 (1/3) (command line):1 -> 1] --- --- The first number in each line is the internal trace number. Next are --- the file name ('(command line)') and the line number (':1') where the --- trace has started. Side traces also show the parent trace number and --- the exit number where they are attached to in parentheses ('(1/3)'). --- An arrow at the end shows where the trace links to ('-> 1'), unless --- it loops to itself. --- --- In this case the inner loop gets hot and is traced first, generating --- a root trace. Then the last exit from the 1st trace gets hot, too, --- and triggers generation of the 2nd trace. The side trace follows the --- path along the outer loop and *around* the inner loop, back to its --- start, and then links to the 1st trace. Yes, this may seem unusual, --- if you know how traditional compilers work. Trace compilers are full --- of surprises like this -- have fun! :-) --- --- Aborted traces are shown like this: --- --- [TRACE --- foo.lua:44 -- leaving loop in root trace at foo:lua:50] --- --- Don't worry -- trace aborts are quite common, even in programs which --- can be fully compiled. The compiler may retry several times until it --- finds a suitable trace. --- --- Of course this doesn't work with features that are not-yet-implemented --- (NYI error messages). The VM simply falls back to the interpreter. This --- may not matter at all if the particular trace is not very high up in --- the CPU usage profile. Oh, and the interpreter is quite fast, too. --- --- Also check out the -jdump module, which prints all the gory details. --- ------------------------------------------------------------------------------- - --- Cache some library functions and objects. -local jit = require("jit") -assert(jit.version_num == 20002, "LuaJIT core/library version mismatch") -local jutil = require("jit.util") -local vmdef = require("jit.vmdef") -local funcinfo, traceinfo = jutil.funcinfo, jutil.traceinfo -local type, format = type, string.format -local stdout, stderr = io.stdout, io.stderr - --- Active flag and output file handle. -local active, out - ------------------------------------------------------------------------------- - -local startloc, startex - -local function fmtfunc(func, pc) - local fi = funcinfo(func, pc) - if fi.loc then - return fi.loc - elseif fi.ffid then - return vmdef.ffnames[fi.ffid] - elseif fi.addr then - return format("C:%x", fi.addr) - else - return "(?)" - end -end - --- Format trace error message. -local function fmterr(err, info) - if type(err) == "number" then - if type(info) == "function" then info = fmtfunc(info) end - err = format(vmdef.traceerr[err], info) - end - return err -end - --- Dump trace states. -local function dump_trace(what, tr, func, pc, otr, oex) - if what == "start" then - startloc = fmtfunc(func, pc) - startex = otr and "("..otr.."/"..oex..") " or "" - else - if what == "abort" then - local loc = fmtfunc(func, pc) - if loc ~= startloc then - out:write(format("[TRACE --- %s%s -- %s at %s]\n", - startex, startloc, fmterr(otr, oex), loc)) - else - out:write(format("[TRACE --- %s%s -- %s]\n", - startex, startloc, fmterr(otr, oex))) - end - elseif what == "stop" then - local info = traceinfo(tr) - local link, ltype = info.link, info.linktype - if ltype == "interpreter" then - out:write(format("[TRACE %3s %s%s -- fallback to interpreter]\n", - tr, startex, startloc)) - elseif link == tr or link == 0 then - out:write(format("[TRACE %3s %s%s %s]\n", - tr, startex, startloc, ltype)) - elseif ltype == "root" then - out:write(format("[TRACE %3s %s%s -> %d]\n", - tr, startex, startloc, link)) - else - out:write(format("[TRACE %3s %s%s -> %d %s]\n", - tr, startex, startloc, link, ltype)) - end - else - out:write(format("[TRACE %s]\n", what)) - end - out:flush() - end -end - ------------------------------------------------------------------------------- - --- Detach dump handlers. -local function dumpoff() - if active then - active = false - jit.attach(dump_trace) - if out and out ~= stdout and out ~= stderr then out:close() end - out = nil - end -end - --- Open the output file and attach dump handlers. -local function dumpon(outfile) - if active then dumpoff() end - if not outfile then outfile = os.getenv("LUAJIT_VERBOSEFILE") end - if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) - else - out = stderr - end - jit.attach(dump_trace, "trace") - active = true -end - --- Public module functions. -module(...) - -on = dumpon -off = dumpoff -start = dumpon -- For -j command line option. - diff --git a/third-party/LuaJIT-2.0.2/src/lauxlib.h b/third-party/LuaJIT-2.0.2/src/lauxlib.h deleted file mode 100644 index fed1491b89..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lauxlib.h +++ /dev/null @@ -1,167 +0,0 @@ -/* -** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lauxlib_h -#define lauxlib_h - - -#include -#include - -#include "lua.h" - - -#define luaL_getn(L,i) ((int)lua_objlen(L, i)) -#define luaL_setn(L,i,j) ((void)0) /* no op! */ - -/* extra error code for `luaL_load' */ -#define LUA_ERRFILE (LUA_ERRERR+1) - -typedef struct luaL_Reg { - const char *name; - lua_CFunction func; -} luaL_Reg; - -LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, - const luaL_Reg *l, int nup); -LUALIB_API void (luaL_register) (lua_State *L, const char *libname, - const luaL_Reg *l); -LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); -LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, - size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, - const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); - -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, - lua_Integer def); - -LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int narg); - -LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); -LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); - -LUALIB_API void (luaL_where) (lua_State *L, int lvl); -LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); - -LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, - const char *const lst[]); - -LUALIB_API int (luaL_ref) (lua_State *L, int t); -LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); - -LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); -LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, - const char *name); -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); - -LUALIB_API lua_State *(luaL_newstate) (void); - - -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, - const char *r); - -LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, - const char *fname, int szhint); - -/* From Lua 5.2. */ -LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname); -LUALIB_API int luaL_execresult(lua_State *L, int stat); -LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, - const char *mode); -LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, - const char *name, const char *mode); -LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, - int level); - - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - -#define luaL_argcheck(L, cond,numarg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) -#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) -#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) - -#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - -#define luaL_dofile(L, fn) \ - (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_dostring(L, s) \ - (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) - -#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - - - -typedef struct luaL_Buffer { - char *p; /* current position in buffer */ - int lvl; /* number of strings in the stack (level) */ - lua_State *L; - char buffer[LUAL_BUFFERSIZE]; -} luaL_Buffer; - -#define luaL_addchar(B,c) \ - ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ - (*(B)->p++ = (char)(c))) - -/* compatibility only */ -#define luaL_putchar(B,c) luaL_addchar(B,c) - -#define luaL_addsize(B,n) ((B)->p += (n)) - -LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); -LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); -LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); -LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); -LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); - - -/* }====================================================== */ - - -/* compatibility with ref system */ - -/* pre-defined references */ -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - -#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ - (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) - -#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) - -#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) - - -#define luaL_reg luaL_Reg - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lib_aux.c b/third-party/LuaJIT-2.0.2/src/lib_aux.c deleted file mode 100644 index 05fa6b1018..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_aux.c +++ /dev/null @@ -1,356 +0,0 @@ -/* -** Auxiliary library for the Lua/C API. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major parts taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include -#include - -#define lib_aux_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_state.h" -#include "lj_trace.h" -#include "lj_lib.h" - -#if LJ_TARGET_POSIX -#include -#endif - -/* -- I/O error handling -------------------------------------------------- */ - -LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname) -{ - if (stat) { - setboolV(L->top++, 1); - return 1; - } else { - int en = errno; /* Lua API calls may change this value. */ - setnilV(L->top++); - if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); - else - lua_pushfstring(L, "%s", strerror(en)); - setintV(L->top++, en); - lj_trace_abort(G(L)); - return 3; - } -} - -LUALIB_API int luaL_execresult(lua_State *L, int stat) -{ - if (stat != -1) { -#if LJ_TARGET_POSIX - if (WIFSIGNALED(stat)) { - stat = WTERMSIG(stat); - setnilV(L->top++); - lua_pushliteral(L, "signal"); - } else { - if (WIFEXITED(stat)) - stat = WEXITSTATUS(stat); - if (stat == 0) - setboolV(L->top++, 1); - else - setnilV(L->top++); - lua_pushliteral(L, "exit"); - } -#else - if (stat == 0) - setboolV(L->top++, 1); - else - setnilV(L->top++); - lua_pushliteral(L, "exit"); -#endif - setintV(L->top++, stat); - return 3; - } - return luaL_fileresult(L, 0, NULL); -} - -/* -- Module registration ------------------------------------------------- */ - -LUALIB_API const char *luaL_findtable(lua_State *L, int idx, - const char *fname, int szhint) -{ - const char *e; - lua_pushvalue(L, idx); - do { - e = strchr(fname, '.'); - if (e == NULL) e = fname + strlen(fname); - lua_pushlstring(L, fname, (size_t)(e - fname)); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { /* no such field? */ - lua_pop(L, 1); /* remove this nil */ - lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ - lua_pushlstring(L, fname, (size_t)(e - fname)); - lua_pushvalue(L, -2); - lua_settable(L, -4); /* set new table into field */ - } else if (!lua_istable(L, -1)) { /* field has a non-table value? */ - lua_pop(L, 2); /* remove table and value */ - return fname; /* return problematic part of the name */ - } - lua_remove(L, -2); /* remove previous table */ - fname = e + 1; - } while (*e == '.'); - return NULL; -} - -static int libsize(const luaL_Reg *l) -{ - int size = 0; - for (; l->name; l++) size++; - return size; -} - -LUALIB_API void luaL_openlib(lua_State *L, const char *libname, - const luaL_Reg *l, int nup) -{ - lj_lib_checkfpu(L); - if (libname) { - int size = libsize(l); - /* check whether lib already exists */ - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); - lua_getfield(L, -1, libname); /* get _LOADED[libname] */ - if (!lua_istable(L, -1)) { /* not found? */ - lua_pop(L, 1); /* remove previous result */ - /* try global variable (and create one if it does not exist) */ - if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) - lj_err_callerv(L, LJ_ERR_BADMODN, libname); - lua_pushvalue(L, -1); - lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ - } - lua_remove(L, -2); /* remove _LOADED table */ - lua_insert(L, -(nup+1)); /* move library table to below upvalues */ - } - for (; l->name; l++) { - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); - lua_setfield(L, -(nup+2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ -} - -LUALIB_API void luaL_register(lua_State *L, const char *libname, - const luaL_Reg *l) -{ - luaL_openlib(L, libname, l, 0); -} - -LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, - const char *p, const char *r) -{ - const char *wild; - size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); - while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, (size_t)(wild - s)); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after `p' */ - } - luaL_addstring(&b, s); /* push last suffix */ - luaL_pushresult(&b); - return lua_tostring(L, -1); -} - -/* -- Buffer handling ----------------------------------------------------- */ - -#define bufflen(B) ((size_t)((B)->p - (B)->buffer)) -#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) - -static int emptybuffer(luaL_Buffer *B) -{ - size_t l = bufflen(B); - if (l == 0) - return 0; /* put nothing on stack */ - lua_pushlstring(B->L, B->buffer, l); - B->p = B->buffer; - B->lvl++; - return 1; -} - -static void adjuststack(luaL_Buffer *B) -{ - if (B->lvl > 1) { - lua_State *L = B->L; - int toget = 1; /* number of levels to concat */ - size_t toplen = lua_strlen(L, -1); - do { - size_t l = lua_strlen(L, -(toget+1)); - if (!(B->lvl - toget + 1 >= LUA_MINSTACK/2 || toplen > l)) - break; - toplen += l; - toget++; - } while (toget < B->lvl); - lua_concat(L, toget); - B->lvl = B->lvl - toget + 1; - } -} - -LUALIB_API char *luaL_prepbuffer(luaL_Buffer *B) -{ - if (emptybuffer(B)) - adjuststack(B); - return B->buffer; -} - -LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l) -{ - while (l--) - luaL_addchar(B, *s++); -} - -LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s) -{ - luaL_addlstring(B, s, strlen(s)); -} - -LUALIB_API void luaL_pushresult(luaL_Buffer *B) -{ - emptybuffer(B); - lua_concat(B->L, B->lvl); - B->lvl = 1; -} - -LUALIB_API void luaL_addvalue(luaL_Buffer *B) -{ - lua_State *L = B->L; - size_t vl; - const char *s = lua_tolstring(L, -1, &vl); - if (vl <= bufffree(B)) { /* fit into buffer? */ - memcpy(B->p, s, vl); /* put it there */ - B->p += vl; - lua_pop(L, 1); /* remove from stack */ - } else { - if (emptybuffer(B)) - lua_insert(L, -2); /* put buffer before new value */ - B->lvl++; /* add new value into B stack */ - adjuststack(B); - } -} - -LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B) -{ - B->L = L; - B->p = B->buffer; - B->lvl = 0; -} - -/* -- Reference management ------------------------------------------------ */ - -#define FREELIST_REF 0 - -/* Convert a stack index to an absolute index. */ -#define abs_index(L, i) \ - ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1) - -LUALIB_API int luaL_ref(lua_State *L, int t) -{ - int ref; - t = abs_index(L, t); - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* `nil' has a unique fixed reference */ - } - lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ - lua_pop(L, 1); /* remove it from stack */ - if (ref != 0) { /* any free element? */ - lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ - } else { /* no free elements */ - ref = (int)lua_objlen(L, t); - ref++; /* create new reference */ - } - lua_rawseti(L, t, ref); - return ref; -} - -LUALIB_API void luaL_unref(lua_State *L, int t, int ref) -{ - if (ref >= 0) { - t = abs_index(L, t); - lua_rawgeti(L, t, FREELIST_REF); - lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ - lua_pushinteger(L, ref); - lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ - } -} - -/* -- Default allocator and panic function -------------------------------- */ - -static int panic(lua_State *L) -{ - const char *s = lua_tostring(L, -1); - fputs("PANIC: unprotected error in call to Lua API (", stderr); - fputs(s ? s : "?", stderr); - fputc(')', stderr); fputc('\n', stderr); - fflush(stderr); - return 0; -} - -#ifdef LUAJIT_USE_SYSMALLOC - -#if LJ_64 -#error "Must use builtin allocator for 64 bit target" -#endif - -static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize) -{ - (void)ud; - (void)osize; - if (nsize == 0) { - free(ptr); - return NULL; - } else { - return realloc(ptr, nsize); - } -} - -LUALIB_API lua_State *luaL_newstate(void) -{ - lua_State *L = lua_newstate(mem_alloc, NULL); - if (L) G(L)->panic = panic; - return L; -} - -#else - -#include "lj_alloc.h" - -LUALIB_API lua_State *luaL_newstate(void) -{ - lua_State *L; - void *ud = lj_alloc_create(); - if (ud == NULL) return NULL; -#if LJ_64 - L = lj_state_newstate(lj_alloc_f, ud); -#else - L = lua_newstate(lj_alloc_f, ud); -#endif - if (L) G(L)->panic = panic; - return L; -} - -#if LJ_64 -LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) -{ - UNUSED(f); UNUSED(ud); - fputs("Must use luaL_newstate() for 64 bit target\n", stderr); - return NULL; -} -#endif - -#endif - diff --git a/third-party/LuaJIT-2.0.2/src/lib_base.c b/third-party/LuaJIT-2.0.2/src/lib_base.c deleted file mode 100644 index 070970ed71..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_base.c +++ /dev/null @@ -1,683 +0,0 @@ -/* -** Base and coroutine library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include - -#define lib_base_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_state.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cconv.h" -#endif -#include "lj_bc.h" -#include "lj_ff.h" -#include "lj_dispatch.h" -#include "lj_char.h" -#include "lj_strscan.h" -#include "lj_lib.h" - -/* -- Base library: checks ------------------------------------------------ */ - -#define LJLIB_MODULE_base - -LJLIB_ASM(assert) LJLIB_REC(.) -{ - GCstr *s; - lj_lib_checkany(L, 1); - s = lj_lib_optstr(L, 2); - if (s) - lj_err_callermsg(L, strdata(s)); - else - lj_err_caller(L, LJ_ERR_ASSERT); - return FFH_UNREACHABLE; -} - -/* ORDER LJ_T */ -LJLIB_PUSH("nil") -LJLIB_PUSH("boolean") -LJLIB_PUSH(top-1) /* boolean */ -LJLIB_PUSH("userdata") -LJLIB_PUSH("string") -LJLIB_PUSH("upval") -LJLIB_PUSH("thread") -LJLIB_PUSH("proto") -LJLIB_PUSH("function") -LJLIB_PUSH("trace") -LJLIB_PUSH("cdata") -LJLIB_PUSH("table") -LJLIB_PUSH(top-9) /* userdata */ -LJLIB_PUSH("number") -LJLIB_ASM_(type) LJLIB_REC(.) -/* Recycle the lj_lib_checkany(L, 1) from assert. */ - -/* -- Base library: iterators --------------------------------------------- */ - -/* This solves a circular dependency problem -- change FF_next_N as needed. */ -LJ_STATIC_ASSERT((int)FF_next == FF_next_N); - -LJLIB_ASM(next) -{ - lj_lib_checktab(L, 1); - return FFH_UNREACHABLE; -} - -#if LJ_52 || LJ_HASFFI -static int ffh_pairs(lua_State *L, MMS mm) -{ - TValue *o = lj_lib_checkany(L, 1); - cTValue *mo = lj_meta_lookup(L, o, mm); - if ((LJ_52 || tviscdata(o)) && !tvisnil(mo)) { - L->top = o+1; /* Only keep one argument. */ - copyTV(L, L->base-1, mo); /* Replace callable. */ - return FFH_TAILCALL; - } else { - if (!tvistab(o)) lj_err_argt(L, 1, LUA_TTABLE); - setfuncV(L, o-1, funcV(lj_lib_upvalue(L, 1))); - if (mm == MM_pairs) setnilV(o+1); else setintV(o+1, 0); - return FFH_RES(3); - } -} -#else -#define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE) -#endif - -LJLIB_PUSH(lastcl) -LJLIB_ASM(pairs) -{ - return ffh_pairs(L, MM_pairs); -} - -LJLIB_NOREGUV LJLIB_ASM(ipairs_aux) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - lj_lib_checkint(L, 2); - return FFH_UNREACHABLE; -} - -LJLIB_PUSH(lastcl) -LJLIB_ASM(ipairs) LJLIB_REC(.) -{ - return ffh_pairs(L, MM_ipairs); -} - -/* -- Base library: getters and setters ----------------------------------- */ - -LJLIB_ASM_(getmetatable) LJLIB_REC(.) -/* Recycle the lj_lib_checkany(L, 1) from assert. */ - -LJLIB_ASM(setmetatable) LJLIB_REC(.) -{ - GCtab *t = lj_lib_checktab(L, 1); - GCtab *mt = lj_lib_checktabornil(L, 2); - if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable))) - lj_err_caller(L, LJ_ERR_PROTMT); - setgcref(t->metatable, obj2gco(mt)); - if (mt) { lj_gc_objbarriert(L, t, mt); } - settabV(L, L->base-1, t); - return FFH_RES(1); -} - -LJLIB_CF(getfenv) -{ - GCfunc *fn; - cTValue *o = L->base; - if (!(o < L->top && tvisfunc(o))) { - int level = lj_lib_optint(L, 1, 1); - o = lj_debug_frame(L, level, &level); - if (o == NULL) - lj_err_arg(L, 1, LJ_ERR_INVLVL); - } - fn = &gcval(o)->fn; - settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env)); - return 1; -} - -LJLIB_CF(setfenv) -{ - GCfunc *fn; - GCtab *t = lj_lib_checktab(L, 2); - cTValue *o = L->base; - if (!(o < L->top && tvisfunc(o))) { - int level = lj_lib_checkint(L, 1); - if (level == 0) { - /* NOBARRIER: A thread (i.e. L) is never black. */ - setgcref(L->env, obj2gco(t)); - return 0; - } - o = lj_debug_frame(L, level, &level); - if (o == NULL) - lj_err_arg(L, 1, LJ_ERR_INVLVL); - } - fn = &gcval(o)->fn; - if (!isluafunc(fn)) - lj_err_caller(L, LJ_ERR_SETFENV); - setgcref(fn->l.env, obj2gco(t)); - lj_gc_objbarrier(L, obj2gco(fn), t); - setfuncV(L, L->top++, fn); - return 1; -} - -LJLIB_ASM(rawget) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - lj_lib_checkany(L, 2); - return FFH_UNREACHABLE; -} - -LJLIB_CF(rawset) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - lj_lib_checkany(L, 2); - L->top = 1+lj_lib_checkany(L, 3); - lua_rawset(L, 1); - return 1; -} - -LJLIB_CF(rawequal) LJLIB_REC(.) -{ - cTValue *o1 = lj_lib_checkany(L, 1); - cTValue *o2 = lj_lib_checkany(L, 2); - setboolV(L->top-1, lj_obj_equal(o1, o2)); - return 1; -} - -#if LJ_52 -LJLIB_CF(rawlen) LJLIB_REC(.) -{ - cTValue *o = L->base; - int32_t len; - if (L->top > o && tvisstr(o)) - len = (int32_t)strV(o)->len; - else - len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1)); - setintV(L->top-1, len); - return 1; -} -#endif - -LJLIB_CF(unpack) -{ - GCtab *t = lj_lib_checktab(L, 1); - int32_t n, i = lj_lib_optint(L, 2, 1); - int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ? - lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t); - if (i > e) return 0; - n = e - i + 1; - if (n <= 0 || !lua_checkstack(L, n)) - lj_err_caller(L, LJ_ERR_UNPACK); - do { - cTValue *tv = lj_tab_getint(t, i); - if (tv) { - copyTV(L, L->top++, tv); - } else { - setnilV(L->top++); - } - } while (i++ < e); - return n; -} - -LJLIB_CF(select) LJLIB_REC(.) -{ - int32_t n = (int32_t)(L->top - L->base); - if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') { - setintV(L->top-1, n-1); - return 1; - } else { - int32_t i = lj_lib_checkint(L, 1); - if (i < 0) i = n + i; else if (i > n) i = n; - if (i < 1) - lj_err_arg(L, 1, LJ_ERR_IDXRNG); - return n - i; - } -} - -/* -- Base library: conversions ------------------------------------------- */ - -LJLIB_ASM(tonumber) LJLIB_REC(.) -{ - int32_t base = lj_lib_optint(L, 2, 10); - if (base == 10) { - TValue *o = lj_lib_checkany(L, 1); - if (lj_strscan_numberobj(o)) { - copyTV(L, L->base-1, o); - return FFH_RES(1); - } -#if LJ_HASFFI - if (tviscdata(o)) { - CTState *cts = ctype_cts(L); - CType *ct = lj_ctype_rawref(cts, cdataV(o)->ctypeid); - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) { - if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) && - ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) { - int32_t i; - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0); - setintV(L->base-1, i); - return FFH_RES(1); - } - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE), - (uint8_t *)&(L->base-1)->n, o, 0); - return FFH_RES(1); - } - } -#endif - } else { - const char *p = strdata(lj_lib_checkstr(L, 1)); - char *ep; - unsigned long ul; - if (base < 2 || base > 36) - lj_err_arg(L, 2, LJ_ERR_BASERNG); - ul = strtoul(p, &ep, base); - if (p != ep) { - while (lj_char_isspace((unsigned char)(*ep))) ep++; - if (*ep == '\0') { - if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u)) - setintV(L->base-1, (int32_t)ul); - else - setnumV(L->base-1, (lua_Number)ul); - return FFH_RES(1); - } - } - } - setnilV(L->base-1); - return FFH_RES(1); -} - -LJLIB_PUSH("nil") -LJLIB_PUSH("false") -LJLIB_PUSH("true") -LJLIB_ASM(tostring) LJLIB_REC(.) -{ - TValue *o = lj_lib_checkany(L, 1); - cTValue *mo; - L->top = o+1; /* Only keep one argument. */ - if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { - copyTV(L, L->base-1, mo); /* Replace callable. */ - return FFH_TAILCALL; - } else { - GCstr *s; - if (tvisnumber(o)) { - s = lj_str_fromnumber(L, o); - } else if (tvispri(o)) { - s = strV(lj_lib_upvalue(L, -(int32_t)itype(o))); - } else { - if (tvisfunc(o) && isffunc(funcV(o))) - lua_pushfstring(L, "function: builtin#%d", funcV(o)->c.ffid); - else - lua_pushfstring(L, "%s: %p", lj_typename(o), lua_topointer(L, 1)); - /* Note: lua_pushfstring calls the GC which may invalidate o. */ - s = strV(L->top-1); - } - setstrV(L, L->base-1, s); - return FFH_RES(1); - } -} - -/* -- Base library: throw and catch errors -------------------------------- */ - -LJLIB_CF(error) -{ - int32_t level = lj_lib_optint(L, 2, 1); - lua_settop(L, 1); - if (lua_isstring(L, 1) && level > 0) { - luaL_where(L, level); - lua_pushvalue(L, 1); - lua_concat(L, 2); - } - return lua_error(L); -} - -LJLIB_ASM(pcall) LJLIB_REC(.) -{ - lj_lib_checkany(L, 1); - lj_lib_checkfunc(L, 2); /* For xpcall only. */ - return FFH_UNREACHABLE; -} -LJLIB_ASM_(xpcall) LJLIB_REC(.) - -/* -- Base library: load Lua code ----------------------------------------- */ - -static int load_aux(lua_State *L, int status, int envarg) -{ - if (status == 0) { - if (tvistab(L->base+envarg-1)) { - GCfunc *fn = funcV(L->top-1); - GCtab *t = tabV(L->base+envarg-1); - setgcref(fn->c.env, obj2gco(t)); - lj_gc_objbarrier(L, fn, t); - } - return 1; - } else { - setnilV(L->top-2); - return 2; - } -} - -LJLIB_CF(loadfile) -{ - GCstr *fname = lj_lib_optstr(L, 1); - GCstr *mode = lj_lib_optstr(L, 2); - int status; - lua_settop(L, 3); /* Ensure env arg exists. */ - status = luaL_loadfilex(L, fname ? strdata(fname) : NULL, - mode ? strdata(mode) : NULL); - return load_aux(L, status, 3); -} - -static const char *reader_func(lua_State *L, void *ud, size_t *size) -{ - UNUSED(ud); - luaL_checkstack(L, 2, "too many nested functions"); - copyTV(L, L->top++, L->base); - lua_call(L, 0, 1); /* Call user-supplied function. */ - L->top--; - if (tvisnil(L->top)) { - *size = 0; - return NULL; - } else if (tvisstr(L->top) || tvisnumber(L->top)) { - copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */ - return lua_tolstring(L, 5, size); - } else { - lj_err_caller(L, LJ_ERR_RDRSTR); - return NULL; - } -} - -LJLIB_CF(load) -{ - GCstr *name = lj_lib_optstr(L, 2); - GCstr *mode = lj_lib_optstr(L, 3); - int status; - if (L->base < L->top && (tvisstr(L->base) || tvisnumber(L->base))) { - GCstr *s = lj_lib_checkstr(L, 1); - lua_settop(L, 4); /* Ensure env arg exists. */ - status = luaL_loadbufferx(L, strdata(s), s->len, strdata(name ? name : s), - mode ? strdata(mode) : NULL); - } else { - lj_lib_checkfunc(L, 1); - lua_settop(L, 5); /* Reserve a slot for the string from the reader. */ - status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)", - mode ? strdata(mode) : NULL); - } - return load_aux(L, status, 4); -} - -LJLIB_CF(loadstring) -{ - return lj_cf_load(L); -} - -LJLIB_CF(dofile) -{ - GCstr *fname = lj_lib_optstr(L, 1); - setnilV(L->top); - L->top = L->base+1; - if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != 0) - lua_error(L); - lua_call(L, 0, LUA_MULTRET); - return (int)(L->top - L->base) - 1; -} - -/* -- Base library: GC control -------------------------------------------- */ - -LJLIB_CF(gcinfo) -{ - setintV(L->top++, (G(L)->gc.total >> 10)); - return 1; -} - -LJLIB_CF(collectgarbage) -{ - int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */ - "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul"); - int32_t data = lj_lib_optint(L, 2, 0); - if (opt == LUA_GCCOUNT) { - setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0); - } else { - int res = lua_gc(L, opt, data); - if (opt == LUA_GCSTEP) - setboolV(L->top, res); - else - setintV(L->top, res); - } - L->top++; - return 1; -} - -/* -- Base library: miscellaneous functions ------------------------------- */ - -LJLIB_PUSH(top-2) /* Upvalue holds weak table. */ -LJLIB_CF(newproxy) -{ - lua_settop(L, 1); - lua_newuserdata(L, 0); - if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */ - return 1; - } else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */ - lua_newtable(L); - lua_pushvalue(L, -1); - lua_pushboolean(L, 1); - lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */ - } else { /* newproxy(proxy): inherit metatable. */ - int validproxy = 0; - if (lua_getmetatable(L, 1)) { - lua_rawget(L, lua_upvalueindex(1)); - validproxy = lua_toboolean(L, -1); - lua_pop(L, 1); - } - if (!validproxy) - lj_err_arg(L, 1, LJ_ERR_NOPROXY); - lua_getmetatable(L, 1); - } - lua_setmetatable(L, 2); - return 1; -} - -LJLIB_PUSH("tostring") -LJLIB_CF(print) -{ - ptrdiff_t i, nargs = L->top - L->base; - cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1))); - int shortcut; - if (tv && !tvisnil(tv)) { - copyTV(L, L->top++, tv); - } else { - setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1))); - lua_gettable(L, LUA_GLOBALSINDEX); - tv = L->top-1; - } - shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring); - for (i = 0; i < nargs; i++) { - const char *str; - size_t size; - cTValue *o = &L->base[i]; - if (shortcut && tvisstr(o)) { - str = strVdata(o); - size = strV(o)->len; - } else if (shortcut && tvisint(o)) { - char buf[LJ_STR_INTBUF]; - char *p = lj_str_bufint(buf, intV(o)); - size = (size_t)(buf+LJ_STR_INTBUF-p); - str = p; - } else if (shortcut && tvisnum(o)) { - char buf[LJ_STR_NUMBUF]; - size = lj_str_bufnum(buf, o); - str = buf; - } else { - copyTV(L, L->top+1, o); - copyTV(L, L->top, L->top-1); - L->top += 2; - lua_call(L, 1, 1); - str = lua_tolstring(L, -1, &size); - if (!str) - lj_err_caller(L, LJ_ERR_PRTOSTR); - L->top--; - } - if (i) - putchar('\t'); - fwrite(str, 1, size, stdout); - } - putchar('\n'); - return 0; -} - -LJLIB_PUSH(top-3) -LJLIB_SET(_VERSION) - -#include "lj_libdef.h" - -/* -- Coroutine library --------------------------------------------------- */ - -#define LJLIB_MODULE_coroutine - -LJLIB_CF(coroutine_status) -{ - const char *s; - lua_State *co; - if (!(L->top > L->base && tvisthread(L->base))) - lj_err_arg(L, 1, LJ_ERR_NOCORO); - co = threadV(L->base); - if (co == L) s = "running"; - else if (co->status == LUA_YIELD) s = "suspended"; - else if (co->status != 0) s = "dead"; - else if (co->base > tvref(co->stack)+1) s = "normal"; - else if (co->top == co->base) s = "dead"; - else s = "suspended"; - lua_pushstring(L, s); - return 1; -} - -LJLIB_CF(coroutine_running) -{ -#if LJ_52 - int ismain = lua_pushthread(L); - setboolV(L->top++, ismain); - return 2; -#else - if (lua_pushthread(L)) - setnilV(L->top++); - return 1; -#endif -} - -LJLIB_CF(coroutine_create) -{ - lua_State *L1; - if (!(L->base < L->top && tvisfunc(L->base))) - lj_err_argt(L, 1, LUA_TFUNCTION); - L1 = lua_newthread(L); - setfuncV(L, L1->top++, funcV(L->base)); - return 1; -} - -LJLIB_ASM(coroutine_yield) -{ - lj_err_caller(L, LJ_ERR_CYIELD); - return FFH_UNREACHABLE; -} - -static int ffh_resume(lua_State *L, lua_State *co, int wrap) -{ - if (co->cframe != NULL || co->status > LUA_YIELD || - (co->status == 0 && co->top == co->base)) { - ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD; - if (wrap) lj_err_caller(L, em); - setboolV(L->base-1, 0); - setstrV(L, L->base, lj_err_str(L, em)); - return FFH_RES(2); - } - lj_state_growstack(co, (MSize)(L->top - L->base)); - return FFH_RETRY; -} - -LJLIB_ASM(coroutine_resume) -{ - if (!(L->top > L->base && tvisthread(L->base))) - lj_err_arg(L, 1, LJ_ERR_NOCORO); - return ffh_resume(L, threadV(L->base), 0); -} - -LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux) -{ - return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1); -} - -/* Inline declarations. */ -LJ_ASMF void lj_ff_coroutine_wrap_aux(void); -#if !(LJ_TARGET_MIPS && defined(ljamalg_c)) -LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, - lua_State *co); -#endif - -/* Error handler, called from assembler VM. */ -void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co) -{ - co->top--; copyTV(L, L->top, co->top); L->top++; - if (tvisstr(L->top-1)) - lj_err_callermsg(L, strVdata(L->top-1)); - else - lj_err_run(L); -} - -/* Forward declaration. */ -static void setpc_wrap_aux(lua_State *L, GCfunc *fn); - -LJLIB_CF(coroutine_wrap) -{ - lj_cf_coroutine_create(L); - lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1); - setpc_wrap_aux(L, funcV(L->top-1)); - return 1; -} - -#include "lj_libdef.h" - -/* Fix the PC of wrap_aux. Really ugly workaround. */ -static void setpc_wrap_aux(lua_State *L, GCfunc *fn) -{ - setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]); -} - -/* ------------------------------------------------------------------------ */ - -static void newproxy_weaktable(lua_State *L) -{ - /* NOBARRIER: The table is new (marked white). */ - GCtab *t = lj_tab_new(L, 0, 1); - settabV(L, L->top++, t); - setgcref(t->metatable, obj2gco(t)); - setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")), - lj_str_newlit(L, "kv")); - t->nomm = (uint8_t)(~(1u<env); - settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env); - lua_pushliteral(L, LUA_VERSION); /* top-3. */ - newproxy_weaktable(L); /* top-2. */ - LJ_LIB_REG(L, "_G", base); - LJ_LIB_REG(L, LUA_COLIBNAME, coroutine); - return 2; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_bit.c b/third-party/LuaJIT-2.0.2/src/lib_bit.c deleted file mode 100644 index 93fead9232..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_bit.c +++ /dev/null @@ -1,74 +0,0 @@ -/* -** Bit manipulation library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lib_bit_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_bit - -LJLIB_ASM(bit_tobit) LJLIB_REC(bit_unary IR_TOBIT) -{ - lj_lib_checknumber(L, 1); - return FFH_RETRY; -} -LJLIB_ASM_(bit_bnot) LJLIB_REC(bit_unary IR_BNOT) -LJLIB_ASM_(bit_bswap) LJLIB_REC(bit_unary IR_BSWAP) - -LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) -{ - lj_lib_checknumber(L, 1); - lj_lib_checkbit(L, 2); - return FFH_RETRY; -} -LJLIB_ASM_(bit_rshift) LJLIB_REC(bit_shift IR_BSHR) -LJLIB_ASM_(bit_arshift) LJLIB_REC(bit_shift IR_BSAR) -LJLIB_ASM_(bit_rol) LJLIB_REC(bit_shift IR_BROL) -LJLIB_ASM_(bit_ror) LJLIB_REC(bit_shift IR_BROR) - -LJLIB_ASM(bit_band) LJLIB_REC(bit_nary IR_BAND) -{ - int i = 0; - do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); - return FFH_RETRY; -} -LJLIB_ASM_(bit_bor) LJLIB_REC(bit_nary IR_BOR) -LJLIB_ASM_(bit_bxor) LJLIB_REC(bit_nary IR_BXOR) - -/* ------------------------------------------------------------------------ */ - -LJLIB_CF(bit_tohex) -{ - uint32_t b = (uint32_t)lj_lib_checkbit(L, 1); - int32_t i, n = L->base+1 >= L->top ? 8 : lj_lib_checkbit(L, 2); - const char *hexdigits = "0123456789abcdef"; - char buf[8]; - if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } - if (n > 8) n = 8; - for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } - lua_pushlstring(L, buf, (size_t)n); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_bit(lua_State *L) -{ - LJ_LIB_REG(L, LUA_BITLIBNAME, bit); - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_debug.c b/third-party/LuaJIT-2.0.2/src/lib_debug.c deleted file mode 100644 index 38e6054367..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_debug.c +++ /dev/null @@ -1,405 +0,0 @@ -/* -** Debug library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_debug_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_debug - -LJLIB_CF(debug_getregistry) -{ - copyTV(L, L->top++, registry(L)); - return 1; -} - -LJLIB_CF(debug_getmetatable) -{ - lj_lib_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - setnilV(L->top-1); - } - return 1; -} - -LJLIB_CF(debug_setmetatable) -{ - lj_lib_checktabornil(L, 2); - L->top = L->base+2; - lua_setmetatable(L, 1); -#if !LJ_52 - setboolV(L->top-1, 1); -#endif - return 1; -} - -LJLIB_CF(debug_getfenv) -{ - lj_lib_checkany(L, 1); - lua_getfenv(L, 1); - return 1; -} - -LJLIB_CF(debug_setfenv) -{ - lj_lib_checktab(L, 2); - L->top = L->base+2; - if (!lua_setfenv(L, 1)) - lj_err_caller(L, LJ_ERR_SETFENV); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void settabss(lua_State *L, const char *i, const char *v) -{ - lua_pushstring(L, v); - lua_setfield(L, -2, i); -} - -static void settabsi(lua_State *L, const char *i, int v) -{ - lua_pushinteger(L, v); - lua_setfield(L, -2, i); -} - -static void settabsb(lua_State *L, const char *i, int v) -{ - lua_pushboolean(L, v); - lua_setfield(L, -2, i); -} - -static lua_State *getthread(lua_State *L, int *arg) -{ - if (L->base < L->top && tvisthread(L->base)) { - *arg = 1; - return threadV(L->base); - } else { - *arg = 0; - return L; - } -} - -static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) -{ - if (L == L1) { - lua_pushvalue(L, -2); - lua_remove(L, -3); - } - else - lua_xmove(L1, L, 1); - lua_setfield(L, -2, fname); -} - -LJLIB_CF(debug_getinfo) -{ - lj_Debug ar; - int arg, opt_f = 0, opt_L = 0; - lua_State *L1 = getthread(L, &arg); - const char *options = luaL_optstring(L, arg+2, "flnSu"); - if (lua_isnumber(L, arg+1)) { - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), (lua_Debug *)&ar)) { - setnilV(L->top-1); - return 1; - } - } else if (L->base+arg < L->top && tvisfunc(L->base+arg)) { - options = lua_pushfstring(L, ">%s", options); - setfuncV(L1, L1->top++, funcV(L->base+arg)); - } else { - lj_err_arg(L, arg+1, LJ_ERR_NOFUNCL); - } - if (!lj_debug_getinfo(L1, options, &ar, 1)) - lj_err_arg(L, arg+2, LJ_ERR_INVOPT); - lua_createtable(L, 0, 16); /* Create result table. */ - for (; *options; options++) { - switch (*options) { - case 'S': - settabss(L, "source", ar.source); - settabss(L, "short_src", ar.short_src); - settabsi(L, "linedefined", ar.linedefined); - settabsi(L, "lastlinedefined", ar.lastlinedefined); - settabss(L, "what", ar.what); - break; - case 'l': - settabsi(L, "currentline", ar.currentline); - break; - case 'u': - settabsi(L, "nups", ar.nups); - settabsi(L, "nparams", ar.nparams); - settabsb(L, "isvararg", ar.isvararg); - break; - case 'n': - settabss(L, "name", ar.name); - settabss(L, "namewhat", ar.namewhat); - break; - case 'f': opt_f = 1; break; - case 'L': opt_L = 1; break; - default: break; - } - } - if (opt_L) treatstackoption(L, L1, "activelines"); - if (opt_f) treatstackoption(L, L1, "func"); - return 1; /* Return result table. */ -} - -LJLIB_CF(debug_getlocal) -{ - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; - int slot = lj_lib_checkint(L, arg+2); - if (tvisfunc(L->base+arg)) { - L->top = L->base+arg+1; - lua_pushstring(L, lua_getlocal(L, NULL, slot)); - return 1; - } - if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar)) - lj_err_arg(L, arg+1, LJ_ERR_LVLRNG); - name = lua_getlocal(L1, &ar, slot); - if (name) { - lua_xmove(L1, L, 1); - lua_pushstring(L, name); - lua_pushvalue(L, -2); - return 2; - } else { - setnilV(L->top-1); - return 1; - } -} - -LJLIB_CF(debug_setlocal) -{ - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - TValue *tv; - if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar)) - lj_err_arg(L, arg+1, LJ_ERR_LVLRNG); - tv = lj_lib_checkany(L, arg+3); - copyTV(L1, L1->top++, tv); - lua_pushstring(L, lua_setlocal(L1, &ar, lj_lib_checkint(L, arg+2))); - return 1; -} - -static int debug_getupvalue(lua_State *L, int get) -{ - int32_t n = lj_lib_checkint(L, 2); - const char *name; - lj_lib_checkfunc(L, 1); - name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); - if (name) { - lua_pushstring(L, name); - if (!get) return 1; - copyTV(L, L->top, L->top-2); - L->top++; - return 2; - } - return 0; -} - -LJLIB_CF(debug_getupvalue) -{ - return debug_getupvalue(L, 1); -} - -LJLIB_CF(debug_setupvalue) -{ - lj_lib_checkany(L, 3); - return debug_getupvalue(L, 0); -} - -LJLIB_CF(debug_upvalueid) -{ - GCfunc *fn = lj_lib_checkfunc(L, 1); - int32_t n = lj_lib_checkint(L, 2) - 1; - if ((uint32_t)n >= fn->l.nupvalues) - lj_err_arg(L, 2, LJ_ERR_IDXRNG); - setlightudV(L->top-1, isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : - (void *)&fn->c.upvalue[n]); - return 1; -} - -LJLIB_CF(debug_upvaluejoin) -{ - GCfunc *fn[2]; - GCRef *p[2]; - int i; - for (i = 0; i < 2; i++) { - int32_t n; - fn[i] = lj_lib_checkfunc(L, 2*i+1); - if (!isluafunc(fn[i])) - lj_err_arg(L, 2*i+1, LJ_ERR_NOLFUNC); - n = lj_lib_checkint(L, 2*i+2) - 1; - if ((uint32_t)n >= fn[i]->l.nupvalues) - lj_err_arg(L, 2*i+2, LJ_ERR_IDXRNG); - p[i] = &fn[i]->l.uvptr[n]; - } - setgcrefr(*p[0], *p[1]); - lj_gc_objbarrier(L, fn[0], gcref(*p[1])); - return 0; -} - -#if LJ_52 -LJLIB_CF(debug_getuservalue) -{ - TValue *o = L->base; - if (o < L->top && tvisudata(o)) - settabV(L, o, tabref(udataV(o)->env)); - else - setnilV(o); - L->top = o+1; - return 1; -} - -LJLIB_CF(debug_setuservalue) -{ - TValue *o = L->base; - if (!(o < L->top && tvisudata(o))) - lj_err_argt(L, 1, LUA_TUSERDATA); - if (!(o+1 < L->top && tvistab(o+1))) - lj_err_argt(L, 2, LUA_TTABLE); - L->top = o+2; - lua_setfenv(L, 1); - return 1; -} -#endif - -/* ------------------------------------------------------------------------ */ - -static const char KEY_HOOK = 'h'; - -static void hookf(lua_State *L, lua_Debug *ar) -{ - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail return"}; - lua_pushlightuserdata(L, (void *)&KEY_HOOK); - lua_rawget(L, LUA_REGISTRYINDEX); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); - if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); - else lua_pushnil(L); - lua_call(L, 2, 0); - } -} - -static int makemask(const char *smask, int count) -{ - int mask = 0; - if (strchr(smask, 'c')) mask |= LUA_MASKCALL; - if (strchr(smask, 'r')) mask |= LUA_MASKRET; - if (strchr(smask, 'l')) mask |= LUA_MASKLINE; - if (count > 0) mask |= LUA_MASKCOUNT; - return mask; -} - -static char *unmakemask(int mask, char *smask) -{ - int i = 0; - if (mask & LUA_MASKCALL) smask[i++] = 'c'; - if (mask & LUA_MASKRET) smask[i++] = 'r'; - if (mask & LUA_MASKLINE) smask[i++] = 'l'; - smask[i] = '\0'; - return smask; -} - -LJLIB_CF(debug_sethook) -{ - int arg, mask, count; - lua_Hook func; - (void)getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { - lua_settop(L, arg+1); - func = NULL; mask = 0; count = 0; /* turn off hooks */ - } else { - const char *smask = luaL_checkstring(L, arg+2); - luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } - lua_pushlightuserdata(L, (void *)&KEY_HOOK); - lua_pushvalue(L, arg+1); - lua_rawset(L, LUA_REGISTRYINDEX); - lua_sethook(L, func, mask, count); - return 0; -} - -LJLIB_CF(debug_gethook) -{ - char buff[5]; - int mask = lua_gethookmask(L); - lua_Hook hook = lua_gethook(L); - if (hook != NULL && hook != hookf) { /* external hook? */ - lua_pushliteral(L, "external hook"); - } else { - lua_pushlightuserdata(L, (void *)&KEY_HOOK); - lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */ - } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L)); - return 3; -} - -/* ------------------------------------------------------------------------ */ - -LJLIB_CF(debug_debug) -{ - for (;;) { - char buffer[250]; - fputs("lua_debug> ", stderr); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || - strcmp(buffer, "cont\n") == 0) - return 0; - if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || - lua_pcall(L, 0, 0, 0)) { - fputs(lua_tostring(L, -1), stderr); - fputs("\n", stderr); - } - lua_settop(L, 0); /* remove eventual returns */ - } -} - -/* ------------------------------------------------------------------------ */ - -#define LEVELS1 12 /* size of the first part of the stack */ -#define LEVELS2 10 /* size of the second part of the stack */ - -LJLIB_CF(debug_traceback) -{ - int arg; - lua_State *L1 = getthread(L, &arg); - const char *msg = lua_tostring(L, arg+1); - if (msg == NULL && L->top > L->base+arg) - L->top = L->base+arg+1; - else - luaL_traceback(L, L1, msg, lj_lib_optint(L, arg+2, (L == L1))); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_debug(lua_State *L) -{ - LJ_LIB_REG(L, LUA_DBLIBNAME, debug); - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_ffi.c b/third-party/LuaJIT-2.0.2/src/lib_ffi.c deleted file mode 100644 index f61fabc049..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_ffi.c +++ /dev/null @@ -1,850 +0,0 @@ -/* -** FFI library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lib_ffi_c -#define LUA_LIB - -#include - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_ctype.h" -#include "lj_cparse.h" -#include "lj_cdata.h" -#include "lj_cconv.h" -#include "lj_carith.h" -#include "lj_ccall.h" -#include "lj_ccallback.h" -#include "lj_clib.h" -#include "lj_ff.h" -#include "lj_lib.h" - -/* -- C type checks ------------------------------------------------------- */ - -/* Check first argument for a C type and returns its ID. */ -static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param) -{ - TValue *o = L->base; - if (!(o < L->top)) { - err_argtype: - lj_err_argtype(L, 1, "C type"); - } - if (tvisstr(o)) { /* Parse an abstract C type declaration. */ - GCstr *s = strV(o); - CPState cp; - int errcode; - cp.L = L; - cp.cts = cts; - cp.srcname = strdata(s); - cp.p = strdata(s); - cp.param = param; - cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; - errcode = lj_cparse(&cp); - if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ - return cp.val.id; - } else { - GCcdata *cd; - if (!tviscdata(o)) goto err_argtype; - if (param && param < L->top) lj_err_arg(L, 1, LJ_ERR_FFI_NUMPARAM); - cd = cdataV(o); - return cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->ctypeid; - } -} - -/* Check argument for C data and return it. */ -static GCcdata *ffi_checkcdata(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && tviscdata(o))) - lj_err_argt(L, narg, LUA_TCDATA); - return cdataV(o); -} - -/* Convert argument to C pointer. */ -static void *ffi_checkptr(lua_State *L, int narg, CTypeID id) -{ - CTState *cts = ctype_cts(L); - TValue *o = L->base + narg-1; - void *p; - if (o >= L->top) - lj_err_arg(L, narg, LJ_ERR_NOVAL); - lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg)); - return p; -} - -/* Convert argument to int32_t. */ -static int32_t ffi_checkint(lua_State *L, int narg) -{ - CTState *cts = ctype_cts(L); - TValue *o = L->base + narg-1; - int32_t i; - if (o >= L->top) - lj_err_arg(L, narg, LJ_ERR_NOVAL); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, - CCF_ARG(narg)); - return i; -} - -/* -- C type metamethods -------------------------------------------------- */ - -#define LJLIB_MODULE_ffi_meta - -/* Handle ctype __index/__newindex metamethods. */ -static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm) -{ - CTypeID id = ctype_typeid(cts, ct); - cTValue *tv = lj_ctype_meta(cts, id, mm); - TValue *base = L->base; - if (!tv) { - const char *s; - err_index: - s = strdata(lj_ctype_repr(L, id, NULL)); - if (tvisstr(L->base+1)) { - lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1)); - } else { - const char *key = tviscdata(L->base+1) ? - strdata(lj_ctype_repr(L, cdataV(L->base+1)->ctypeid, NULL)) : - lj_typename(L->base+1); - lj_err_callerv(L, LJ_ERR_FFI_BADIDXW, s, key); - } - } - if (!tvisfunc(tv)) { - if (mm == MM_index) { - cTValue *o = lj_meta_tget(L, tv, base+1); - if (o) { - if (tvisnil(o)) goto err_index; - copyTV(L, L->top-1, o); - return 1; - } - } else { - TValue *o = lj_meta_tset(L, tv, base+1); - if (o) { - copyTV(L, o, base+2); - return 0; - } - } - tv = L->top-1; - } - return lj_meta_tailcall(L, tv); -} - -LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0) -{ - CTState *cts = ctype_cts(L); - CTInfo qual = 0; - CType *ct; - uint8_t *p; - TValue *o = L->base; - if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */ - lj_err_argt(L, 1, LUA_TCDATA); - ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual); - if ((qual & 1)) - return ffi_index_meta(L, cts, ct, MM_index); - if (lj_cdata_get(cts, ct, L->top-1, p)) - lj_gc_check(L); - return 1; -} - -LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1) -{ - CTState *cts = ctype_cts(L); - CTInfo qual = 0; - CType *ct; - uint8_t *p; - TValue *o = L->base; - if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */ - lj_err_argt(L, 1, LUA_TCDATA); - ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual); - if ((qual & 1)) { - if ((qual & CTF_CONST)) - lj_err_caller(L, LJ_ERR_FFI_WRCONST); - return ffi_index_meta(L, cts, ct, MM_newindex); - } - lj_cdata_set(cts, ct, p, o+2, qual); - return 0; -} - -/* Common handler for cdata arithmetic. */ -static int ffi_arith(lua_State *L) -{ - MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq); - return lj_carith_op(L, mm); -} - -/* The following functions must be in contiguous ORDER MM. */ -LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat) -{ - return ffi_arith(L); -} - -/* Forward declaration. */ -static int lj_cf_ffi_new(lua_State *L); - -LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call) -{ - CTState *cts = ctype_cts(L); - GCcdata *cd = ffi_checkcdata(L, 1); - CTypeID id = cd->ctypeid; - CType *ct; - cTValue *tv; - MMS mm = MM_call; - if (cd->ctypeid == CTID_CTYPEID) { - id = *(CTypeID *)cdataptr(cd); - mm = MM_new; - } else { - int ret = lj_ccall_func(L, cd); - if (ret >= 0) - return ret; - } - /* Handle ctype __call/__new metamethod. */ - ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - if (tv) - return lj_meta_tailcall(L, tv); - else if (mm == MM_call) - lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL))); - return lj_cf_ffi_new(L); -} - -LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm) -{ - return ffi_arith(L); -} -/* End of contiguous ORDER MM. */ - -LJLIB_CF(ffi_meta___tostring) -{ - GCcdata *cd = ffi_checkcdata(L, 1); - const char *msg = "cdata<%s>: %p"; - CTypeID id = cd->ctypeid; - void *p = cdataptr(cd); - if (id == CTID_CTYPEID) { - msg = "ctype<%s>"; - id = *(CTypeID *)p; - } else { - CTState *cts = ctype_cts(L); - CType *ct = ctype_raw(cts, id); - if (ctype_isref(ct->info)) { - p = *(void **)p; - ct = ctype_rawchild(cts, ct); - } - if (ctype_iscomplex(ct->info)) { - setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size)); - goto checkgc; - } else if (ct->size == 8 && ctype_isinteger(ct->info)) { - setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd), - (ct->info & CTF_UNSIGNED))); - goto checkgc; - } else if (ctype_isfunc(ct->info)) { - p = *(void **)p; - } else if (ctype_isenum(ct->info)) { - msg = "cdata<%s>: %d"; - p = (void *)(uintptr_t)*(uint32_t **)p; - } else { - if (ctype_isptr(ct->info)) { - p = cdata_getptr(p, ct->size); - ct = ctype_rawchild(cts, ct); - } - if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) { - /* Handle ctype __tostring metamethod. */ - cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring); - if (tv) - return lj_meta_tailcall(L, tv); - } - } - } - lj_str_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p); -checkgc: - lj_gc_check(L); - return 1; -} - -static int ffi_pairs(lua_State *L, MMS mm) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkcdata(L, 1)->ctypeid; - CType *ct = ctype_raw(cts, id); - cTValue *tv; - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - if (!tv) - lj_err_callerv(L, LJ_ERR_FFI_BADMM, strdata(lj_ctype_repr(L, id, NULL)), - strdata(mmname_str(G(L), mm))); - return lj_meta_tailcall(L, tv); -} - -LJLIB_CF(ffi_meta___pairs) -{ - return ffi_pairs(L, MM_pairs); -} - -LJLIB_CF(ffi_meta___ipairs) -{ - return ffi_pairs(L, MM_ipairs); -} - -LJLIB_PUSH("ffi") LJLIB_SET(__metatable) - -#include "lj_libdef.h" - -/* -- C library metamethods ----------------------------------------------- */ - -#define LJLIB_MODULE_ffi_clib - -/* Index C library by a name. */ -static TValue *ffi_clib_index(lua_State *L) -{ - TValue *o = L->base; - CLibrary *cl; - if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)) - lj_err_argt(L, 1, LUA_TUSERDATA); - cl = (CLibrary *)uddata(udataV(o)); - if (!(o+1 < L->top && tvisstr(o+1))) - lj_err_argt(L, 2, LUA_TSTRING); - return lj_clib_index(L, cl, strV(o+1)); -} - -LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1) -{ - TValue *tv = ffi_clib_index(L); - if (tviscdata(tv)) { - CTState *cts = ctype_cts(L); - GCcdata *cd = cdataV(tv); - CType *s = ctype_get(cts, cd->ctypeid); - if (ctype_isextern(s->info)) { - CTypeID sid = ctype_cid(s->info); - void *sp = *(void **)cdataptr(cd); - CType *ct = ctype_raw(cts, sid); - if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp)) - lj_gc_check(L); - return 1; - } - } - copyTV(L, L->top-1, tv); - return 1; -} - -LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0) -{ - TValue *tv = ffi_clib_index(L); - TValue *o = L->base+2; - if (o < L->top && tviscdata(tv)) { - CTState *cts = ctype_cts(L); - GCcdata *cd = cdataV(tv); - CType *d = ctype_get(cts, cd->ctypeid); - if (ctype_isextern(d->info)) { - CTInfo qual = 0; - for (;;) { /* Skip attributes and collect qualifiers. */ - d = ctype_child(cts, d); - if (!ctype_isattrib(d->info)) break; - if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size; - } - if (!((d->info|qual) & CTF_CONST)) { - lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0); - return 0; - } - } - } - lj_err_caller(L, LJ_ERR_FFI_WRCONST); - return 0; /* unreachable */ -} - -LJLIB_CF(ffi_clib___gc) -{ - TValue *o = L->base; - if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB) - lj_clib_unload((CLibrary *)uddata(udataV(o))); - return 0; -} - -#include "lj_libdef.h" - -/* -- Callback function metamethods --------------------------------------- */ - -#define LJLIB_MODULE_ffi_callback - -static int ffi_callback_set(lua_State *L, GCfunc *fn) -{ - GCcdata *cd = ffi_checkcdata(L, 1); - CTState *cts = ctype_cts(L); - CType *ct = ctype_raw(cts, cd->ctypeid); - if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) { - MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd)); - if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) { - GCtab *t = cts->miscmap; - TValue *tv = lj_tab_setint(L, t, (int32_t)slot); - if (fn) { - setfuncV(L, tv, fn); - lj_gc_anybarriert(L, t); - } else { - setnilV(tv); - cts->cb.cbid[slot] = 0; - cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid; - } - return 0; - } - } - lj_err_caller(L, LJ_ERR_FFI_BADCBACK); - return 0; -} - -LJLIB_CF(ffi_callback_free) -{ - return ffi_callback_set(L, NULL); -} - -LJLIB_CF(ffi_callback_set) -{ - GCfunc *fn = lj_lib_checkfunc(L, 2); - return ffi_callback_set(L, fn); -} - -LJLIB_PUSH(top-1) LJLIB_SET(__index) - -#include "lj_libdef.h" - -/* -- FFI library functions ----------------------------------------------- */ - -#define LJLIB_MODULE_ffi - -LJLIB_CF(ffi_cdef) -{ - GCstr *s = lj_lib_checkstr(L, 1); - CPState cp; - int errcode; - cp.L = L; - cp.cts = ctype_cts(L); - cp.srcname = strdata(s); - cp.p = strdata(s); - cp.param = L->base+1; - cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT; - errcode = lj_cparse(&cp); - if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ - lj_gc_check(L); - return 0; -} - -LJLIB_CF(ffi_new) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CType *ct = ctype_raw(cts, id); - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - TValue *o = L->base+1; - GCcdata *cd; - if ((info & CTF_VLA)) { - o++; - sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2)); - } - if (sz == CTSIZE_INVALID) - lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE); - if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN) - cd = lj_cdata_new(cts, id, sz); - else - cd = lj_cdata_newv(cts, id, sz, ctype_align(info)); - setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */ - lj_cconv_ct_init(cts, ct, sz, cdataptr(cd), - o, (MSize)(L->top - o)); /* Initialize cdata. */ - if (ctype_isstruct(ct->info)) { - /* Handle ctype __gc metamethod. Use the fast lookup here. */ - cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id); - if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) { - GCtab *t = cts->finalizer; - if (gcref(t->metatable)) { - /* Add to finalizer table, if still enabled. */ - copyTV(L, lj_tab_set(L, t, o-1), tv); - lj_gc_anybarriert(L, t); - cd->marked |= LJ_GC_CDATA_FIN; - } - } - } - L->top = o; /* Only return the cdata itself. */ - lj_gc_check(L); - return 1; -} - -LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CType *d = ctype_raw(cts, id); - TValue *o = lj_lib_checkany(L, 2); - L->top = o+1; /* Make sure this is the last item on the stack. */ - if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info))) - lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); - if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) { - GCcdata *cd = lj_cdata_new(cts, id, d->size); - lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST); - setcdataV(L, o, cd); - lj_gc_check(L); - } - return 1; -} - -LJLIB_CF(ffi_typeof) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, L->base+1); - GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4); - *(CTypeID *)cdataptr(cd) = id; - setcdataV(L, L->top-1, cd); - lj_gc_check(L); - return 1; -} - -LJLIB_CF(ffi_istype) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - CTypeID id1 = ffi_checkctype(L, cts, NULL); - TValue *o = lj_lib_checkany(L, 2); - int b = 0; - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : - cd->ctypeid; - CType *ct1 = lj_ctype_rawref(cts, id1); - CType *ct2 = lj_ctype_rawref(cts, id2); - if (ct1 == ct2) { - b = 1; - } else if (ctype_type(ct1->info) == ctype_type(ct2->info) && - ct1->size == ct2->size) { - if (ctype_ispointer(ct1->info)) - b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL); - else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info)) - b = (((ct1->info ^ ct2->info) & ~(CTF_QUAL|CTF_LONG)) == 0); - } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) && - ct1 == ctype_rawchild(cts, ct2)) { - b = 1; - } - } - setboolV(L->top-1, b); - setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ - return 1; -} - -LJLIB_CF(ffi_sizeof) LJLIB_REC(ffi_xof FF_ffi_sizeof) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CTSize sz; - if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) { - sz = cdatavlen(cdataV(L->base)); - } else { - CType *ct = lj_ctype_rawref(cts, id); - if (ctype_isvltype(ct->info)) - sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2)); - else - sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; - if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) { - setnilV(L->top-1); - return 1; - } - } - setintV(L->top-1, (int32_t)sz); - return 1; -} - -LJLIB_CF(ffi_alignof) LJLIB_REC(ffi_xof FF_ffi_alignof) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CTSize sz = 0; - CTInfo info = lj_ctype_info(cts, id, &sz); - setintV(L->top-1, 1 << ctype_align(info)); - return 1; -} - -LJLIB_CF(ffi_offsetof) LJLIB_REC(ffi_xof FF_ffi_offsetof) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - GCstr *name = lj_lib_checkstr(L, 2); - CType *ct = lj_ctype_rawref(cts, id); - CTSize ofs; - if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) { - CType *fct = lj_ctype_getfield(cts, ct, name, &ofs); - if (fct) { - setintV(L->top-1, ofs); - if (ctype_isfield(fct->info)) { - return 1; - } else if (ctype_isbitfield(fct->info)) { - setintV(L->top++, ctype_bitpos(fct->info)); - setintV(L->top++, ctype_bitbsz(fct->info)); - return 3; - } - } - } - return 0; -} - -LJLIB_CF(ffi_errno) LJLIB_REC(.) -{ - int err = errno; - if (L->top > L->base) - errno = ffi_checkint(L, 1); - setintV(L->top++, err); - return 1; -} - -LJLIB_CF(ffi_string) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - TValue *o = lj_lib_checkany(L, 1); - const char *p; - size_t len; - if (o+1 < L->top) { - len = (size_t)ffi_checkint(L, 2); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o, - CCF_ARG(1)); - } else { - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o, - CCF_ARG(1)); - len = strlen(p); - } - L->top = o+1; /* Make sure this is the last item on the stack. */ - setstrV(L, o, lj_str_new(L, p, len)); - lj_gc_check(L); - return 1; -} - -LJLIB_CF(ffi_copy) LJLIB_REC(.) -{ - void *dp = ffi_checkptr(L, 1, CTID_P_VOID); - void *sp = ffi_checkptr(L, 2, CTID_P_CVOID); - TValue *o = L->base+1; - CTSize len; - if (tvisstr(o) && o+1 >= L->top) - len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */ - else - len = (CTSize)ffi_checkint(L, 3); - memcpy(dp, sp, len); - return 0; -} - -LJLIB_CF(ffi_fill) LJLIB_REC(.) -{ - void *dp = ffi_checkptr(L, 1, CTID_P_VOID); - CTSize len = (CTSize)ffi_checkint(L, 2); - int32_t fill = 0; - if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3); - memset(dp, fill, len); - return 0; -} - -#define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be) - -/* Test ABI string. */ -LJLIB_CF(ffi_abi) LJLIB_REC(.) -{ - GCstr *s = lj_lib_checkstr(L, 1); - int b = 0; - switch (s->hash) { -#if LJ_64 - case H_(849858eb,ad35fd06): b = 1; break; /* 64bit */ -#else - case H_(662d3c79,d0e22477): b = 1; break; /* 32bit */ -#endif -#if LJ_ARCH_HASFPU - case H_(e33ee463,e33ee463): b = 1; break; /* fpu */ -#endif -#if LJ_ABI_SOFTFP - case H_(61211a23,c2e8c81c): b = 1; break; /* softfp */ -#else - case H_(539417a8,8ce0812f): b = 1; break; /* hardfp */ -#endif -#if LJ_ABI_EABI - case H_(2182df8f,f2ed1152): b = 1; break; /* eabi */ -#endif -#if LJ_ABI_WIN - case H_(4ab624a8,4ab624a8): b = 1; break; /* win */ -#endif - case H_(3af93066,1f001464): b = 1; break; /* le/be */ - default: - break; - } - setboolV(L->top-1, b); - setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ - return 1; -} - -#undef H_ - -LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */ - -LJLIB_CF(ffi_metatype) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - GCtab *mt = lj_lib_checktab(L, 2); - GCtab *t = cts->miscmap; - CType *ct = ctype_get(cts, id); /* Only allow raw types. */ - TValue *tv; - GCcdata *cd; - if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) || - ctype_isvector(ct->info))) - lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); - tv = lj_tab_setinth(L, t, -(int32_t)id); - if (!tvisnil(tv)) - lj_err_caller(L, LJ_ERR_PROTMT); - settabV(L, tv, mt); - lj_gc_anybarriert(L, t); - cd = lj_cdata_new(cts, CTID_CTYPEID, 4); - *(CTypeID *)cdataptr(cd) = id; - setcdataV(L, L->top-1, cd); - lj_gc_check(L); - return 1; -} - -LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */ - -LJLIB_CF(ffi_gc) LJLIB_REC(.) -{ - GCcdata *cd = ffi_checkcdata(L, 1); - TValue *fin = lj_lib_checkany(L, 2); - CTState *cts = ctype_cts(L); - GCtab *t = cts->finalizer; - CType *ct = ctype_raw(cts, cd->ctypeid); - if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) || - ctype_isrefarray(ct->info))) - lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); - if (gcref(t->metatable)) { /* Update finalizer table, if still enabled. */ - copyTV(L, lj_tab_set(L, t, L->base), fin); - lj_gc_anybarriert(L, t); - if (!tvisnil(fin)) - cd->marked |= LJ_GC_CDATA_FIN; - else - cd->marked &= ~LJ_GC_CDATA_FIN; - } - L->top = L->base+1; /* Pass through the cdata object. */ - return 1; -} - -LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */ - -LJLIB_CF(ffi_load) -{ - GCstr *name = lj_lib_checkstr(L, 1); - int global = (L->base+1 < L->top && tvistruecond(L->base+1)); - lj_clib_load(L, tabref(curr_func(L)->c.env), name, global); - return 1; -} - -LJLIB_PUSH(top-4) LJLIB_SET(C) -LJLIB_PUSH(top-3) LJLIB_SET(os) -LJLIB_PUSH(top-2) LJLIB_SET(arch) - -#include "lj_libdef.h" - -/* ------------------------------------------------------------------------ */ - -/* Create special weak-keyed finalizer table. */ -static GCtab *ffi_finalizer(lua_State *L) -{ - /* NOBARRIER: The table is new (marked white). */ - GCtab *t = lj_tab_new(L, 0, 1); - settabV(L, L->top++, t); - setgcref(t->metatable, obj2gco(t)); - setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")), - lj_str_newlit(L, "K")); - t->nomm = (uint8_t)(~(1u<top-1); - lj_gc_anybarriert(L, t); - } -} - -LUALIB_API int luaopen_ffi(lua_State *L) -{ - CTState *cts = lj_ctype_init(L); - settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1))); - cts->finalizer = ffi_finalizer(L); - LJ_LIB_REG(L, NULL, ffi_meta); - /* NOBARRIER: basemt is a GC root. */ - setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1))); - LJ_LIB_REG(L, NULL, ffi_clib); - LJ_LIB_REG(L, NULL, ffi_callback); - /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */ - settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1)); - L->top--; - lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */ - lua_pushliteral(L, LJ_OS_NAME); - lua_pushliteral(L, LJ_ARCH_NAME); - LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */ - ffi_register_module(L); - return 1; -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lib_init.c b/third-party/LuaJIT-2.0.2/src/lib_init.c deleted file mode 100644 index 2d6bd591bf..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_init.c +++ /dev/null @@ -1,55 +0,0 @@ -/* -** Library initialization. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major parts taken verbatim from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_init_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_arch.h" - -static const luaL_Reg lj_lib_load[] = { - { "", luaopen_base }, - { LUA_LOADLIBNAME, luaopen_package }, - { LUA_TABLIBNAME, luaopen_table }, - { LUA_IOLIBNAME, luaopen_io }, - { LUA_OSLIBNAME, luaopen_os }, - { LUA_STRLIBNAME, luaopen_string }, - { LUA_MATHLIBNAME, luaopen_math }, - { LUA_DBLIBNAME, luaopen_debug }, - { LUA_BITLIBNAME, luaopen_bit }, - { LUA_JITLIBNAME, luaopen_jit }, - { NULL, NULL } -}; - -static const luaL_Reg lj_lib_preload[] = { -#if LJ_HASFFI - { LUA_FFILIBNAME, luaopen_ffi }, -#endif - { NULL, NULL } -}; - -LUALIB_API void luaL_openlibs(lua_State *L) -{ - const luaL_Reg *lib; - for (lib = lj_lib_load; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); - } - luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", - sizeof(lj_lib_preload)/sizeof(lj_lib_preload[0])-1); - for (lib = lj_lib_preload; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_setfield(L, -2, lib->name); - } - lua_pop(L, 1); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_io.c b/third-party/LuaJIT-2.0.2/src/lib_io.c deleted file mode 100644 index e0c6908fae..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_io.c +++ /dev/null @@ -1,539 +0,0 @@ -/* -** I/O library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include - -#define lib_io_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_state.h" -#include "lj_ff.h" -#include "lj_lib.h" - -/* Userdata payload for I/O file. */ -typedef struct IOFileUD { - FILE *fp; /* File handle. */ - uint32_t type; /* File type. */ -} IOFileUD; - -#define IOFILE_TYPE_FILE 0 /* Regular file. */ -#define IOFILE_TYPE_PIPE 1 /* Pipe. */ -#define IOFILE_TYPE_STDF 2 /* Standard file handle. */ -#define IOFILE_TYPE_MASK 3 - -#define IOFILE_FLAG_CLOSE 4 /* Close after io.lines() iterator. */ - -#define IOSTDF_UD(L, id) (&gcref(G(L)->gcroot[(id)])->ud) -#define IOSTDF_IOF(L, id) ((IOFileUD *)uddata(IOSTDF_UD(L, (id)))) - -/* -- Open/close helpers -------------------------------------------------- */ - -static IOFileUD *io_tofilep(lua_State *L) -{ - if (!(L->base < L->top && tvisudata(L->base) && - udataV(L->base)->udtype == UDTYPE_IO_FILE)) - lj_err_argtype(L, 1, "FILE*"); - return (IOFileUD *)uddata(udataV(L->base)); -} - -static IOFileUD *io_tofile(lua_State *L) -{ - IOFileUD *iof = io_tofilep(L); - if (iof->fp == NULL) - lj_err_caller(L, LJ_ERR_IOCLFL); - return iof; -} - -static FILE *io_stdfile(lua_State *L, ptrdiff_t id) -{ - IOFileUD *iof = IOSTDF_IOF(L, id); - if (iof->fp == NULL) - lj_err_caller(L, LJ_ERR_IOSTDCL); - return iof->fp; -} - -static IOFileUD *io_file_new(lua_State *L) -{ - IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD)); - GCudata *ud = udataV(L->top-1); - ud->udtype = UDTYPE_IO_FILE; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcrefr(ud->metatable, curr_func(L)->c.env); - iof->fp = NULL; - iof->type = IOFILE_TYPE_FILE; - return iof; -} - -static IOFileUD *io_file_open(lua_State *L, const char *mode) -{ - const char *fname = strdata(lj_lib_checkstr(L, 1)); - IOFileUD *iof = io_file_new(L); - iof->fp = fopen(fname, mode); - if (iof->fp == NULL) - luaL_argerror(L, 1, lj_str_pushf(L, "%s: %s", fname, strerror(errno))); - return iof; -} - -static int io_file_close(lua_State *L, IOFileUD *iof) -{ - int ok; - if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_FILE) { - ok = (fclose(iof->fp) == 0); - } else if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_PIPE) { - int stat = -1; -#if LJ_TARGET_POSIX - stat = pclose(iof->fp); -#elif LJ_TARGET_WINDOWS - stat = _pclose(iof->fp); -#else - lua_assert(0); - return 0; -#endif -#if LJ_52 - iof->fp = NULL; - return luaL_execresult(L, stat); -#else - ok = (stat != -1); -#endif - } else { - lua_assert((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_STDF); - setnilV(L->top++); - lua_pushliteral(L, "cannot close standard file"); - return 2; - } - iof->fp = NULL; - return luaL_fileresult(L, ok, NULL); -} - -/* -- Read/write helpers -------------------------------------------------- */ - -static int io_file_readnum(lua_State *L, FILE *fp) -{ - lua_Number d; - if (fscanf(fp, LUA_NUMBER_SCAN, &d) == 1) { - if (LJ_DUALNUM) { - int32_t i = lj_num2int(d); - if (d == (lua_Number)i && !tvismzero((cTValue *)&d)) { - setintV(L->top++, i); - return 1; - } - } - setnumV(L->top++, d); - return 1; - } else { - setnilV(L->top++); - return 0; - } -} - -static int io_file_readline(lua_State *L, FILE *fp, MSize chop) -{ - MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0; - char *buf; - for (;;) { - buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); - if (fgets(buf+n, m-n, fp) == NULL) break; - n += (MSize)strlen(buf+n); - ok |= n; - if (n && buf[n-1] == '\n') { n -= chop; break; } - if (n >= m - 64) m += m; - } - setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); - lj_gc_check(L); - return (int)ok; -} - -static void io_file_readall(lua_State *L, FILE *fp) -{ - MSize m, n; - for (m = LUAL_BUFFERSIZE, n = 0; ; m += m) { - char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); - n += (MSize)fread(buf+n, 1, m-n, fp); - if (n != m) { - setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); - lj_gc_check(L); - return; - } - } -} - -static int io_file_readlen(lua_State *L, FILE *fp, MSize m) -{ - if (m) { - char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); - MSize n = (MSize)fread(buf, 1, m, fp); - setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); - lj_gc_check(L); - return (n > 0 || m == 0); - } else { - int c = getc(fp); - ungetc(c, fp); - setstrV(L, L->top++, &G(L)->strempty); - return (c != EOF); - } -} - -static int io_file_read(lua_State *L, FILE *fp, int start) -{ - int ok, n, nargs = (int)(L->top - L->base) - start; - clearerr(fp); - if (nargs == 0) { - ok = io_file_readline(L, fp, 1); - n = start+1; /* Return 1 result. */ - } else { - /* The results plus the buffers go on top of the args. */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); - ok = 1; - for (n = start; nargs-- && ok; n++) { - if (tvisstr(L->base+n)) { - const char *p = strVdata(L->base+n); - if (p[0] != '*') - lj_err_arg(L, n+1, LJ_ERR_INVOPT); - if (p[1] == 'n') - ok = io_file_readnum(L, fp); - else if ((p[1] & ~0x20) == 'L') - ok = io_file_readline(L, fp, (p[1] == 'l')); - else if (p[1] == 'a') - io_file_readall(L, fp); - else - lj_err_arg(L, n+1, LJ_ERR_INVFMT); - } else if (tvisnumber(L->base+n)) { - ok = io_file_readlen(L, fp, (MSize)lj_lib_checkint(L, n+1)); - } else { - lj_err_arg(L, n+1, LJ_ERR_INVOPT); - } - } - } - if (ferror(fp)) - return luaL_fileresult(L, 0, NULL); - if (!ok) - setnilV(L->top-1); /* Replace last result with nil. */ - return n - start; -} - -static int io_file_write(lua_State *L, FILE *fp, int start) -{ - cTValue *tv; - int status = 1; - for (tv = L->base+start; tv < L->top; tv++) { - if (tvisstr(tv)) { - MSize len = strV(tv)->len; - status = status && (fwrite(strVdata(tv), 1, len, fp) == len); - } else if (tvisint(tv)) { - char buf[LJ_STR_INTBUF]; - char *p = lj_str_bufint(buf, intV(tv)); - size_t len = (size_t)(buf+LJ_STR_INTBUF-p); - status = status && (fwrite(p, 1, len, fp) == len); - } else if (tvisnum(tv)) { - status = status && (fprintf(fp, LUA_NUMBER_FMT, numV(tv)) > 0); - } else { - lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING); - } - } - if (LJ_52 && status) { - L->top = L->base+1; - if (start == 0) - setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_OUTPUT)); - return 1; - } - return luaL_fileresult(L, status, NULL); -} - -static int io_file_iter(lua_State *L) -{ - GCfunc *fn = curr_func(L); - IOFileUD *iof = uddata(udataV(&fn->c.upvalue[0])); - int n = fn->c.nupvalues - 1; - if (iof->fp == NULL) - lj_err_caller(L, LJ_ERR_IOCLFL); - L->top = L->base; - if (n) { /* Copy upvalues with options to stack. */ - if (n > LUAI_MAXCSTACK) - lj_err_caller(L, LJ_ERR_STKOV); - lj_state_checkstack(L, (MSize)n); - memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue)); - L->top += n; - } - n = io_file_read(L, iof->fp, 0); - if (ferror(iof->fp)) - lj_err_callermsg(L, strVdata(L->top-2)); - if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) { - io_file_close(L, iof); /* Return values are ignored. */ - return 0; - } - return n; -} - -/* -- I/O file methods ---------------------------------------------------- */ - -#define LJLIB_MODULE_io_method - -LJLIB_CF(io_method_close) -{ - IOFileUD *iof = L->base < L->top ? io_tofile(L) : - IOSTDF_IOF(L, GCROOT_IO_OUTPUT); - return io_file_close(L, iof); -} - -LJLIB_CF(io_method_read) -{ - return io_file_read(L, io_tofile(L)->fp, 1); -} - -LJLIB_CF(io_method_write) LJLIB_REC(io_write 0) -{ - return io_file_write(L, io_tofile(L)->fp, 1); -} - -LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0) -{ - return luaL_fileresult(L, fflush(io_tofile(L)->fp) == 0, NULL); -} - -LJLIB_CF(io_method_seek) -{ - FILE *fp = io_tofile(L)->fp; - int opt = lj_lib_checkopt(L, 2, 1, "\3set\3cur\3end"); - int64_t ofs = 0; - cTValue *o; - int res; - if (opt == 0) opt = SEEK_SET; - else if (opt == 1) opt = SEEK_CUR; - else if (opt == 2) opt = SEEK_END; - o = L->base+2; - if (o < L->top) { - if (tvisint(o)) - ofs = (int64_t)intV(o); - else if (tvisnum(o)) - ofs = (int64_t)numV(o); - else if (!tvisnil(o)) - lj_err_argt(L, 3, LUA_TNUMBER); - } -#if LJ_TARGET_POSIX - res = fseeko(fp, ofs, opt); -#elif _MSC_VER >= 1400 - res = _fseeki64(fp, ofs, opt); -#elif defined(__MINGW32__) - res = fseeko64(fp, ofs, opt); -#else - res = fseek(fp, (long)ofs, opt); -#endif - if (res) - return luaL_fileresult(L, 0, NULL); -#if LJ_TARGET_POSIX - ofs = ftello(fp); -#elif _MSC_VER >= 1400 - ofs = _ftelli64(fp); -#elif defined(__MINGW32__) - ofs = ftello64(fp); -#else - ofs = (int64_t)ftell(fp); -#endif - setint64V(L->top-1, ofs); - return 1; -} - -LJLIB_CF(io_method_setvbuf) -{ - FILE *fp = io_tofile(L)->fp; - int opt = lj_lib_checkopt(L, 2, -1, "\4full\4line\2no"); - size_t sz = (size_t)lj_lib_optint(L, 3, LUAL_BUFFERSIZE); - if (opt == 0) opt = _IOFBF; - else if (opt == 1) opt = _IOLBF; - else if (opt == 2) opt = _IONBF; - return luaL_fileresult(L, setvbuf(fp, NULL, opt, sz) == 0, NULL); -} - -LJLIB_CF(io_method_lines) -{ - io_tofile(L); - lua_pushcclosure(L, io_file_iter, (int)(L->top - L->base)); - return 1; -} - -LJLIB_CF(io_method___gc) -{ - IOFileUD *iof = io_tofilep(L); - if (iof->fp != NULL && (iof->type & IOFILE_TYPE_MASK) != IOFILE_TYPE_STDF) - io_file_close(L, iof); - return 0; -} - -LJLIB_CF(io_method___tostring) -{ - IOFileUD *iof = io_tofilep(L); - if (iof->fp != NULL) - lua_pushfstring(L, "file (%p)", iof->fp); - else - lua_pushliteral(L, "file (closed)"); - return 1; -} - -LJLIB_PUSH(top-1) LJLIB_SET(__index) - -#include "lj_libdef.h" - -/* -- I/O library functions ----------------------------------------------- */ - -#define LJLIB_MODULE_io - -LJLIB_PUSH(top-2) LJLIB_SET(!) /* Set environment. */ - -LJLIB_CF(io_open) -{ - const char *fname = strdata(lj_lib_checkstr(L, 1)); - GCstr *s = lj_lib_optstr(L, 2); - const char *mode = s ? strdata(s) : "r"; - IOFileUD *iof = io_file_new(L); - iof->fp = fopen(fname, mode); - return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname); -} - -LJLIB_CF(io_popen) -{ -#if LJ_TARGET_POSIX || LJ_TARGET_WINDOWS - const char *fname = strdata(lj_lib_checkstr(L, 1)); - GCstr *s = lj_lib_optstr(L, 2); - const char *mode = s ? strdata(s) : "r"; - IOFileUD *iof = io_file_new(L); - iof->type = IOFILE_TYPE_PIPE; -#if LJ_TARGET_POSIX - fflush(NULL); - iof->fp = popen(fname, mode); -#else - iof->fp = _popen(fname, mode); -#endif - return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname); -#else - return luaL_error(L, LUA_QL("popen") " not supported"); -#endif -} - -LJLIB_CF(io_tmpfile) -{ - IOFileUD *iof = io_file_new(L); -#if LJ_TARGET_PS3 - iof->fp = NULL; errno = ENOSYS; -#else - iof->fp = tmpfile(); -#endif - return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, NULL); -} - -LJLIB_CF(io_close) -{ - return lj_cf_io_method_close(L); -} - -LJLIB_CF(io_read) -{ - return io_file_read(L, io_stdfile(L, GCROOT_IO_INPUT), 0); -} - -LJLIB_CF(io_write) LJLIB_REC(io_write GCROOT_IO_OUTPUT) -{ - return io_file_write(L, io_stdfile(L, GCROOT_IO_OUTPUT), 0); -} - -LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT) -{ - return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)) == 0, NULL); -} - -static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode) -{ - if (L->base < L->top && !tvisnil(L->base)) { - if (tvisudata(L->base)) { - io_tofile(L); - L->top = L->base+1; - } else { - io_file_open(L, mode); - } - /* NOBARRIER: The standard I/O handles are GC roots. */ - setgcref(G(L)->gcroot[id], gcV(L->top-1)); - } else { - setudataV(L, L->top++, IOSTDF_UD(L, id)); - } - return 1; -} - -LJLIB_CF(io_input) -{ - return io_std_getset(L, GCROOT_IO_INPUT, "r"); -} - -LJLIB_CF(io_output) -{ - return io_std_getset(L, GCROOT_IO_OUTPUT, "w"); -} - -LJLIB_CF(io_lines) -{ - if (L->base == L->top) setnilV(L->top++); - if (!tvisnil(L->base)) { /* io.lines(fname) */ - IOFileUD *iof = io_file_open(L, "r"); - iof->type = IOFILE_TYPE_FILE|IOFILE_FLAG_CLOSE; - L->top--; - setudataV(L, L->base, udataV(L->top)); - } else { /* io.lines() iterates over stdin. */ - setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_INPUT)); - } - lua_pushcclosure(L, io_file_iter, (int)(L->top - L->base)); - return 1; -} - -LJLIB_CF(io_type) -{ - cTValue *o = lj_lib_checkany(L, 1); - if (!(tvisudata(o) && udataV(o)->udtype == UDTYPE_IO_FILE)) - setnilV(L->top++); - else if (((IOFileUD *)uddata(udataV(o)))->fp != NULL) - lua_pushliteral(L, "file"); - else - lua_pushliteral(L, "closed file"); - return 1; -} - -#include "lj_libdef.h" - -/* ------------------------------------------------------------------------ */ - -static GCobj *io_std_new(lua_State *L, FILE *fp, const char *name) -{ - IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD)); - GCudata *ud = udataV(L->top-1); - ud->udtype = UDTYPE_IO_FILE; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcref(ud->metatable, gcV(L->top-3)); - iof->fp = fp; - iof->type = IOFILE_TYPE_STDF; - lua_setfield(L, -2, name); - return obj2gco(ud); -} - -LUALIB_API int luaopen_io(lua_State *L) -{ - LJ_LIB_REG(L, NULL, io_method); - copyTV(L, L->top, L->top-1); L->top++; - lua_setfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); - LJ_LIB_REG(L, LUA_IOLIBNAME, io); - setgcref(G(L)->gcroot[GCROOT_IO_INPUT], io_std_new(L, stdin, "stdin")); - setgcref(G(L)->gcroot[GCROOT_IO_OUTPUT], io_std_new(L, stdout, "stdout")); - io_std_new(L, stderr, "stderr"); - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_jit.c b/third-party/LuaJIT-2.0.2/src/lib_jit.c deleted file mode 100644 index 82e68258f3..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_jit.c +++ /dev/null @@ -1,663 +0,0 @@ -/* -** JIT library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lib_jit_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_arch.h" -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_bc.h" -#if LJ_HASJIT -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_target.h" -#endif -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_vmevent.h" -#include "lj_lib.h" - -#include "luajit.h" - -/* -- jit.* functions ----------------------------------------------------- */ - -#define LJLIB_MODULE_jit - -static int setjitmode(lua_State *L, int mode) -{ - int idx = 0; - if (L->base == L->top || tvisnil(L->base)) { /* jit.on/off/flush([nil]) */ - mode |= LUAJIT_MODE_ENGINE; - } else { - /* jit.on/off/flush(func|proto, nil|true|false) */ - if (tvisfunc(L->base) || tvisproto(L->base)) - idx = 1; - else if (!tvistrue(L->base)) /* jit.on/off/flush(true, nil|true|false) */ - goto err; - if (L->base+1 < L->top && tvisbool(L->base+1)) - mode |= boolV(L->base+1) ? LUAJIT_MODE_ALLFUNC : LUAJIT_MODE_ALLSUBFUNC; - else - mode |= LUAJIT_MODE_FUNC; - } - if (luaJIT_setmode(L, idx, mode) != 1) { - if ((mode & LUAJIT_MODE_MASK) == LUAJIT_MODE_ENGINE) - lj_err_caller(L, LJ_ERR_NOJIT); - err: - lj_err_argt(L, 1, LUA_TFUNCTION); - } - return 0; -} - -LJLIB_CF(jit_on) -{ - return setjitmode(L, LUAJIT_MODE_ON); -} - -LJLIB_CF(jit_off) -{ - return setjitmode(L, LUAJIT_MODE_OFF); -} - -LJLIB_CF(jit_flush) -{ -#if LJ_HASJIT - if (L->base < L->top && !tvisnil(L->base)) { - int traceno = lj_lib_checkint(L, 1); - luaJIT_setmode(L, traceno, LUAJIT_MODE_FLUSH|LUAJIT_MODE_TRACE); - return 0; - } -#endif - return setjitmode(L, LUAJIT_MODE_FLUSH); -} - -#if LJ_HASJIT -/* Push a string for every flag bit that is set. */ -static void flagbits_to_strings(lua_State *L, uint32_t flags, uint32_t base, - const char *str) -{ - for (; *str; base <<= 1, str += 1+*str) - if (flags & base) - setstrV(L, L->top++, lj_str_new(L, str+1, *(uint8_t *)str)); -} -#endif - -LJLIB_CF(jit_status) -{ -#if LJ_HASJIT - jit_State *J = L2J(L); - L->top = L->base; - setboolV(L->top++, (J->flags & JIT_F_ON) ? 1 : 0); - flagbits_to_strings(L, J->flags, JIT_F_CPU_FIRST, JIT_F_CPUSTRING); - flagbits_to_strings(L, J->flags, JIT_F_OPT_FIRST, JIT_F_OPTSTRING); - return (int)(L->top - L->base); -#else - setboolV(L->top++, 0); - return 1; -#endif -} - -LJLIB_CF(jit_attach) -{ -#ifdef LUAJIT_DISABLE_VMEVENT - luaL_error(L, "vmevent API disabled"); -#else - GCfunc *fn = lj_lib_checkfunc(L, 1); - GCstr *s = lj_lib_optstr(L, 2); - luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE); - if (s) { /* Attach to given event. */ - const uint8_t *p = (const uint8_t *)strdata(s); - uint32_t h = s->len; - while (*p) h = h ^ (lj_rol(h, 6) + *p++); - lua_pushvalue(L, 1); - lua_rawseti(L, -2, VMEVENT_HASHIDX(h)); - G(L)->vmevmask = VMEVENT_NOCACHE; /* Invalidate cache. */ - } else { /* Detach if no event given. */ - setnilV(L->top++); - while (lua_next(L, -2)) { - L->top--; - if (tvisfunc(L->top) && funcV(L->top) == fn) { - setnilV(lj_tab_set(L, tabV(L->top-2), L->top-1)); - } - } - } -#endif - return 0; -} - -LJLIB_PUSH(top-5) LJLIB_SET(os) -LJLIB_PUSH(top-4) LJLIB_SET(arch) -LJLIB_PUSH(top-3) LJLIB_SET(version_num) -LJLIB_PUSH(top-2) LJLIB_SET(version) - -#include "lj_libdef.h" - -/* -- jit.util.* functions ------------------------------------------------ */ - -#define LJLIB_MODULE_jit_util - -/* -- Reflection API for Lua functions ------------------------------------ */ - -/* Return prototype of first argument (Lua function or prototype object) */ -static GCproto *check_Lproto(lua_State *L, int nolua) -{ - TValue *o = L->base; - if (L->top > o) { - if (tvisproto(o)) { - return protoV(o); - } else if (tvisfunc(o)) { - if (isluafunc(funcV(o))) - return funcproto(funcV(o)); - else if (nolua) - return NULL; - } - } - lj_err_argt(L, 1, LUA_TFUNCTION); - return NULL; /* unreachable */ -} - -static void setintfield(lua_State *L, GCtab *t, const char *name, int32_t val) -{ - setintV(lj_tab_setstr(L, t, lj_str_newz(L, name)), val); -} - -/* local info = jit.util.funcinfo(func [,pc]) */ -LJLIB_CF(jit_util_funcinfo) -{ - GCproto *pt = check_Lproto(L, 1); - if (pt) { - BCPos pc = (BCPos)lj_lib_optint(L, 2, 0); - GCtab *t; - lua_createtable(L, 0, 16); /* Increment hash size if fields are added. */ - t = tabV(L->top-1); - setintfield(L, t, "linedefined", pt->firstline); - setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline); - setintfield(L, t, "stackslots", pt->framesize); - setintfield(L, t, "params", pt->numparams); - setintfield(L, t, "bytecodes", (int32_t)pt->sizebc); - setintfield(L, t, "gcconsts", (int32_t)pt->sizekgc); - setintfield(L, t, "nconsts", (int32_t)pt->sizekn); - setintfield(L, t, "upvalues", (int32_t)pt->sizeuv); - if (pc < pt->sizebc) - setintfield(L, t, "currentline", lj_debug_line(pt, pc)); - lua_pushboolean(L, (pt->flags & PROTO_VARARG)); - lua_setfield(L, -2, "isvararg"); - lua_pushboolean(L, (pt->flags & PROTO_CHILD)); - lua_setfield(L, -2, "children"); - setstrV(L, L->top++, proto_chunkname(pt)); - lua_setfield(L, -2, "source"); - lj_debug_pushloc(L, pt, pc); - lua_setfield(L, -2, "loc"); - } else { - GCfunc *fn = funcV(L->base); - GCtab *t; - lua_createtable(L, 0, 4); /* Increment hash size if fields are added. */ - t = tabV(L->top-1); - if (!iscfunc(fn)) - setintfield(L, t, "ffid", fn->c.ffid); - setintptrV(lj_tab_setstr(L, t, lj_str_newlit(L, "addr")), - (intptr_t)(void *)fn->c.f); - setintfield(L, t, "upvalues", fn->c.nupvalues); - } - return 1; -} - -/* local ins, m = jit.util.funcbc(func, pc) */ -LJLIB_CF(jit_util_funcbc) -{ - GCproto *pt = check_Lproto(L, 0); - BCPos pc = (BCPos)lj_lib_checkint(L, 2); - if (pc < pt->sizebc) { - BCIns ins = proto_bc(pt)[pc]; - BCOp op = bc_op(ins); - lua_assert(op < BC__MAX); - setintV(L->top, ins); - setintV(L->top+1, lj_bc_mode[op]); - L->top += 2; - return 2; - } - return 0; -} - -/* local k = jit.util.funck(func, idx) */ -LJLIB_CF(jit_util_funck) -{ - GCproto *pt = check_Lproto(L, 0); - ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2); - if (idx >= 0) { - if (idx < (ptrdiff_t)pt->sizekn) { - copyTV(L, L->top-1, proto_knumtv(pt, idx)); - return 1; - } - } else { - if (~idx < (ptrdiff_t)pt->sizekgc) { - GCobj *gc = proto_kgc(pt, idx); - setgcV(L, L->top-1, gc, ~gc->gch.gct); - return 1; - } - } - return 0; -} - -/* local name = jit.util.funcuvname(func, idx) */ -LJLIB_CF(jit_util_funcuvname) -{ - GCproto *pt = check_Lproto(L, 0); - uint32_t idx = (uint32_t)lj_lib_checkint(L, 2); - if (idx < pt->sizeuv) { - setstrV(L, L->top-1, lj_str_newz(L, lj_debug_uvname(pt, idx))); - return 1; - } - return 0; -} - -/* -- Reflection API for traces ------------------------------------------- */ - -#if LJ_HASJIT - -/* Check trace argument. Must not throw for non-existent trace numbers. */ -static GCtrace *jit_checktrace(lua_State *L) -{ - TraceNo tr = (TraceNo)lj_lib_checkint(L, 1); - jit_State *J = L2J(L); - if (tr > 0 && tr < J->sizetrace) - return traceref(J, tr); - return NULL; -} - -/* Names of link types. ORDER LJ_TRLINK */ -static const char *const jit_trlinkname[] = { - "none", "root", "loop", "tail-recursion", "up-recursion", "down-recursion", - "interpreter", "return" -}; - -/* local info = jit.util.traceinfo(tr) */ -LJLIB_CF(jit_util_traceinfo) -{ - GCtrace *T = jit_checktrace(L); - if (T) { - GCtab *t; - lua_createtable(L, 0, 8); /* Increment hash size if fields are added. */ - t = tabV(L->top-1); - setintfield(L, t, "nins", (int32_t)T->nins - REF_BIAS - 1); - setintfield(L, t, "nk", REF_BIAS - (int32_t)T->nk); - setintfield(L, t, "link", T->link); - setintfield(L, t, "nexit", T->nsnap); - setstrV(L, L->top++, lj_str_newz(L, jit_trlinkname[T->linktype])); - lua_setfield(L, -2, "linktype"); - /* There are many more fields. Add them only when needed. */ - return 1; - } - return 0; -} - -/* local m, ot, op1, op2, prev = jit.util.traceir(tr, idx) */ -LJLIB_CF(jit_util_traceir) -{ - GCtrace *T = jit_checktrace(L); - IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS; - if (T && ref >= REF_BIAS && ref < T->nins) { - IRIns *ir = &T->ir[ref]; - int32_t m = lj_ir_mode[ir->o]; - setintV(L->top-2, m); - setintV(L->top-1, ir->ot); - setintV(L->top++, (int32_t)ir->op1 - (irm_op1(m)==IRMref ? REF_BIAS : 0)); - setintV(L->top++, (int32_t)ir->op2 - (irm_op2(m)==IRMref ? REF_BIAS : 0)); - setintV(L->top++, ir->prev); - return 5; - } - return 0; -} - -/* local k, t [, slot] = jit.util.tracek(tr, idx) */ -LJLIB_CF(jit_util_tracek) -{ - GCtrace *T = jit_checktrace(L); - IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS; - if (T && ref >= T->nk && ref < REF_BIAS) { - IRIns *ir = &T->ir[ref]; - int32_t slot = -1; - if (ir->o == IR_KSLOT) { - slot = ir->op2; - ir = &T->ir[ir->op1]; - } - lj_ir_kvalue(L, L->top-2, ir); - setintV(L->top-1, (int32_t)irt_type(ir->t)); - if (slot == -1) - return 2; - setintV(L->top++, slot); - return 3; - } - return 0; -} - -/* local snap = jit.util.tracesnap(tr, sn) */ -LJLIB_CF(jit_util_tracesnap) -{ - GCtrace *T = jit_checktrace(L); - SnapNo sn = (SnapNo)lj_lib_checkint(L, 2); - if (T && sn < T->nsnap) { - SnapShot *snap = &T->snap[sn]; - SnapEntry *map = &T->snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - GCtab *t; - lua_createtable(L, nent+2, 0); - t = tabV(L->top-1); - setintV(lj_tab_setint(L, t, 0), (int32_t)snap->ref - REF_BIAS); - setintV(lj_tab_setint(L, t, 1), (int32_t)snap->nslots); - for (n = 0; n < nent; n++) - setintV(lj_tab_setint(L, t, (int32_t)(n+2)), (int32_t)map[n]); - setintV(lj_tab_setint(L, t, (int32_t)(nent+2)), (int32_t)SNAP(255, 0, 0)); - return 1; - } - return 0; -} - -/* local mcode, addr, loop = jit.util.tracemc(tr) */ -LJLIB_CF(jit_util_tracemc) -{ - GCtrace *T = jit_checktrace(L); - if (T && T->mcode != NULL) { - setstrV(L, L->top-1, lj_str_new(L, (const char *)T->mcode, T->szmcode)); - setintptrV(L->top++, (intptr_t)(void *)T->mcode); - setintV(L->top++, T->mcloop); - return 3; - } - return 0; -} - -/* local addr = jit.util.traceexitstub([tr,] exitno) */ -LJLIB_CF(jit_util_traceexitstub) -{ -#ifdef EXITSTUBS_PER_GROUP - ExitNo exitno = (ExitNo)lj_lib_checkint(L, 1); - jit_State *J = L2J(L); - if (exitno < EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) { - setintptrV(L->top-1, (intptr_t)(void *)exitstub_addr(J, exitno)); - return 1; - } -#else - if (L->top > L->base+1) { /* Don't throw for one-argument variant. */ - GCtrace *T = jit_checktrace(L); - ExitNo exitno = (ExitNo)lj_lib_checkint(L, 2); - ExitNo maxexit = T->root ? T->nsnap+1 : T->nsnap; - if (T && T->mcode != NULL && exitno < maxexit) { - setintptrV(L->top-1, (intptr_t)(void *)exitstub_trace_addr(T, exitno)); - return 1; - } - } -#endif - return 0; -} - -/* local addr = jit.util.ircalladdr(idx) */ -LJLIB_CF(jit_util_ircalladdr) -{ - uint32_t idx = (uint32_t)lj_lib_checkint(L, 1); - if (idx < IRCALL__MAX) { - setintptrV(L->top-1, (intptr_t)(void *)lj_ir_callinfo[idx].func); - return 1; - } - return 0; -} - -#endif - -#include "lj_libdef.h" - -/* -- jit.opt module ------------------------------------------------------ */ - -#if LJ_HASJIT - -#define LJLIB_MODULE_jit_opt - -/* Parse optimization level. */ -static int jitopt_level(jit_State *J, const char *str) -{ - if (str[0] >= '0' && str[0] <= '9' && str[1] == '\0') { - uint32_t flags; - if (str[0] == '0') flags = JIT_F_OPT_0; - else if (str[0] == '1') flags = JIT_F_OPT_1; - else if (str[0] == '2') flags = JIT_F_OPT_2; - else flags = JIT_F_OPT_3; - J->flags = (J->flags & ~JIT_F_OPT_MASK) | flags; - return 1; /* Ok. */ - } - return 0; /* No match. */ -} - -/* Parse optimization flag. */ -static int jitopt_flag(jit_State *J, const char *str) -{ - const char *lst = JIT_F_OPTSTRING; - uint32_t opt; - int set = 1; - if (str[0] == '+') { - str++; - } else if (str[0] == '-') { - str++; - set = 0; - } else if (str[0] == 'n' && str[1] == 'o') { - str += str[2] == '-' ? 3 : 2; - set = 0; - } - for (opt = JIT_F_OPT_FIRST; ; opt <<= 1) { - size_t len = *(const uint8_t *)lst; - if (len == 0) - break; - if (strncmp(str, lst+1, len) == 0 && str[len] == '\0') { - if (set) J->flags |= opt; else J->flags &= ~opt; - return 1; /* Ok. */ - } - lst += 1+len; - } - return 0; /* No match. */ -} - -/* Parse optimization parameter. */ -static int jitopt_param(jit_State *J, const char *str) -{ - const char *lst = JIT_P_STRING; - int i; - for (i = 0; i < JIT_P__MAX; i++) { - size_t len = *(const uint8_t *)lst; - lua_assert(len != 0); - if (strncmp(str, lst+1, len) == 0 && str[len] == '=') { - int32_t n = 0; - const char *p = &str[len+1]; - while (*p >= '0' && *p <= '9') - n = n*10 + (*p++ - '0'); - if (*p) return 0; /* Malformed number. */ - J->param[i] = n; - if (i == JIT_P_hotloop) - lj_dispatch_init_hotcount(J2G(J)); - return 1; /* Ok. */ - } - lst += 1+len; - } - return 0; /* No match. */ -} - -/* jit.opt.start(flags...) */ -LJLIB_CF(jit_opt_start) -{ - jit_State *J = L2J(L); - int nargs = (int)(L->top - L->base); - if (nargs == 0) { - J->flags = (J->flags & ~JIT_F_OPT_MASK) | JIT_F_OPT_DEFAULT; - } else { - int i; - for (i = 1; i <= nargs; i++) { - const char *str = strdata(lj_lib_checkstr(L, i)); - if (!jitopt_level(J, str) && - !jitopt_flag(J, str) && - !jitopt_param(J, str)) - lj_err_callerv(L, LJ_ERR_JITOPT, str); - } - } - return 0; -} - -#include "lj_libdef.h" - -#endif - -/* -- JIT compiler initialization ----------------------------------------- */ - -#if LJ_HASJIT -/* Default values for JIT parameters. */ -static const int32_t jit_param_default[JIT_P__MAX+1] = { -#define JIT_PARAMINIT(len, name, value) (value), -JIT_PARAMDEF(JIT_PARAMINIT) -#undef JIT_PARAMINIT - 0 -}; -#endif - -#if LJ_TARGET_ARM && LJ_TARGET_LINUX -#include -#endif - -/* Arch-dependent CPU detection. */ -static uint32_t jit_cpudetect(lua_State *L) -{ - uint32_t flags = 0; -#if LJ_TARGET_X86ORX64 - uint32_t vendor[4]; - uint32_t features[4]; - if (lj_vm_cpuid(0, vendor) && lj_vm_cpuid(1, features)) { -#if !LJ_HASJIT -#define JIT_F_CMOV 1 -#define JIT_F_SSE2 2 -#endif - flags |= ((features[3] >> 15)&1) * JIT_F_CMOV; - flags |= ((features[3] >> 26)&1) * JIT_F_SSE2; -#if LJ_HASJIT - flags |= ((features[2] >> 0)&1) * JIT_F_SSE3; - flags |= ((features[2] >> 19)&1) * JIT_F_SSE4_1; - if (vendor[2] == 0x6c65746e) { /* Intel. */ - if ((features[0] & 0x0ff00f00) == 0x00000f00) /* P4. */ - flags |= JIT_F_P4; /* Currently unused. */ - else if ((features[0] & 0x0fff0ff0) == 0x000106c0) /* Atom. */ - flags |= JIT_F_LEA_AGU; - } else if (vendor[2] == 0x444d4163) { /* AMD. */ - uint32_t fam = (features[0] & 0x0ff00f00); - if (fam == 0x00000f00) /* K8. */ - flags |= JIT_F_SPLIT_XMM; - if (fam >= 0x00000f00) /* K8, K10. */ - flags |= JIT_F_PREFER_IMUL; - } -#endif - } - /* Check for required instruction set support on x86 (unnecessary on x64). */ -#if LJ_TARGET_X86 -#if !defined(LUAJIT_CPU_NOCMOV) - if (!(flags & JIT_F_CMOV)) - luaL_error(L, "CPU not supported"); -#endif -#if defined(LUAJIT_CPU_SSE2) - if (!(flags & JIT_F_SSE2)) - luaL_error(L, "CPU does not support SSE2 (recompile without -DLUAJIT_CPU_SSE2)"); -#endif -#endif -#elif LJ_TARGET_ARM -#if LJ_HASJIT - int ver = LJ_ARCH_VERSION; /* Compile-time ARM CPU detection. */ -#if LJ_TARGET_LINUX - if (ver < 70) { /* Runtime ARM CPU detection. */ - struct utsname ut; - uname(&ut); - if (strncmp(ut.machine, "armv", 4) == 0) { - if (ut.machine[4] >= '7') - ver = 70; - else if (ut.machine[4] == '6') - ver = 60; - } - } -#endif - flags |= ver >= 70 ? JIT_F_ARMV7 : - ver >= 61 ? JIT_F_ARMV6T2_ : - ver >= 60 ? JIT_F_ARMV6_ : 0; - flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2; -#endif -#elif LJ_TARGET_PPC -#if LJ_HASJIT -#if LJ_ARCH_SQRT - flags |= JIT_F_SQRT; -#endif -#if LJ_ARCH_ROUND - flags |= JIT_F_ROUND; -#endif -#endif -#elif LJ_TARGET_PPCSPE - /* Nothing to do. */ -#elif LJ_TARGET_MIPS -#if LJ_HASJIT - /* Compile-time MIPS CPU detection. */ -#if LJ_ARCH_VERSION >= 20 - flags |= JIT_F_MIPS32R2; -#endif - /* Runtime MIPS CPU detection. */ -#if defined(__GNUC__) - if (!(flags & JIT_F_MIPS32R2)) { - int x; - /* On MIPS32R1 rotr is treated as srl. rotr r2,r2,1 -> srl r2,r2,1. */ - __asm__("li $2, 1\n\t.long 0x00221042\n\tmove %0, $2" : "=r"(x) : : "$2"); - if (x) flags |= JIT_F_MIPS32R2; /* Either 0x80000000 (R2) or 0 (R1). */ - } -#endif -#endif -#else -#error "Missing CPU detection for this architecture" -#endif - UNUSED(L); - return flags; -} - -/* Initialize JIT compiler. */ -static void jit_init(lua_State *L) -{ - uint32_t flags = jit_cpudetect(L); -#if LJ_HASJIT - jit_State *J = L2J(L); -#if LJ_TARGET_X86 - /* Silently turn off the JIT compiler on CPUs without SSE2. */ - if ((flags & JIT_F_SSE2)) -#endif - J->flags = flags | JIT_F_ON | JIT_F_OPT_DEFAULT; - memcpy(J->param, jit_param_default, sizeof(J->param)); - lj_dispatch_update(G(L)); -#else - UNUSED(flags); -#endif -} - -LUALIB_API int luaopen_jit(lua_State *L) -{ - lua_pushliteral(L, LJ_OS_NAME); - lua_pushliteral(L, LJ_ARCH_NAME); - lua_pushinteger(L, LUAJIT_VERSION_NUM); - lua_pushliteral(L, LUAJIT_VERSION); - LJ_LIB_REG(L, LUA_JITLIBNAME, jit); -#ifndef LUAJIT_DISABLE_JITUTIL - LJ_LIB_REG(L, "jit.util", jit_util); -#endif -#if LJ_HASJIT - LJ_LIB_REG(L, "jit.opt", jit_opt); -#endif - L->top -= 2; - jit_init(L); - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_math.c b/third-party/LuaJIT-2.0.2/src/lib_math.c deleted file mode 100644 index b23d9a2dc0..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_math.c +++ /dev/null @@ -1,233 +0,0 @@ -/* -** Math library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include - -#define lib_math_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_lib.h" -#include "lj_vm.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_math - -LJLIB_ASM(math_abs) LJLIB_REC(.) -{ - lj_lib_checknumber(L, 1); - return FFH_RETRY; -} -LJLIB_ASM_(math_floor) LJLIB_REC(math_round IRFPM_FLOOR) -LJLIB_ASM_(math_ceil) LJLIB_REC(math_round IRFPM_CEIL) - -LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT) -{ - lj_lib_checknum(L, 1); - return FFH_RETRY; -} -LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10) -LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP) -LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN) -LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS) -LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN) -LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin) -LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos) -LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan) -LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh) -LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh) -LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh) -LJLIB_ASM_(math_frexp) -LJLIB_ASM_(math_modf) LJLIB_REC(.) - -LJLIB_ASM(math_log) LJLIB_REC(math_log) -{ - double x = lj_lib_checknum(L, 1); - if (L->base+1 < L->top) { - double y = lj_lib_checknum(L, 2); -#ifdef LUAJIT_NO_LOG2 - x = log(x); y = 1.0 / log(y); -#else - x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y); -#endif - setnumV(L->base-1, x*y); /* Do NOT join the expression to x / y. */ - return FFH_RES(1); - } - return FFH_RETRY; -} - -LJLIB_PUSH(57.29577951308232) -LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad) - -LJLIB_PUSH(0.017453292519943295) -LJLIB_ASM_(math_rad) LJLIB_REC(math_degrad) - -LJLIB_ASM(math_atan2) LJLIB_REC(.) -{ - lj_lib_checknum(L, 1); - lj_lib_checknum(L, 2); - return FFH_RETRY; -} -LJLIB_ASM_(math_pow) LJLIB_REC(.) -LJLIB_ASM_(math_fmod) - -LJLIB_ASM(math_ldexp) LJLIB_REC(.) -{ - lj_lib_checknum(L, 1); -#if LJ_DUALNUM && !LJ_TARGET_X86ORX64 - lj_lib_checkint(L, 2); -#else - lj_lib_checknum(L, 2); -#endif - return FFH_RETRY; -} - -LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN) -{ - int i = 0; - do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); - return FFH_RETRY; -} -LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX) - -LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi) -LJLIB_PUSH(1e310) LJLIB_SET(huge) - -/* ------------------------------------------------------------------------ */ - -/* This implements a Tausworthe PRNG with period 2^223. Based on: -** Tables of maximally-equidistributed combined LFSR generators, -** Pierre L'Ecuyer, 1991, table 3, 1st entry. -** Full-period ME-CF generator with L=64, J=4, k=223, N1=49. -*/ - -/* PRNG state. */ -struct RandomState { - uint64_t gen[4]; /* State of the 4 LFSR generators. */ - int valid; /* State is valid. */ -}; - -/* Union needed for bit-pattern conversion between uint64_t and double. */ -typedef union { uint64_t u64; double d; } U64double; - -/* Update generator i and compute a running xor of all states. */ -#define TW223_GEN(i, k, q, s) \ - z = rs->gen[i]; \ - z = (((z<> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<gen[i] = z; - -/* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */ -LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs) -{ - uint64_t z, r = 0; - TW223_GEN(0, 63, 31, 18) - TW223_GEN(1, 58, 19, 28) - TW223_GEN(2, 55, 24, 7) - TW223_GEN(3, 47, 21, 8) - return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000); -} - -/* PRNG initialization function. */ -static void random_init(RandomState *rs, double d) -{ - uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */ - int i; - for (i = 0; i < 4; i++) { - U64double u; - uint32_t m = 1u << (r&255); - r >>= 8; - u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354; - if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */ - rs->gen[i] = u.u64; - } - rs->valid = 1; - for (i = 0; i < 10; i++) - lj_math_random_step(rs); -} - -/* PRNG extract function. */ -LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ -LJLIB_CF(math_random) LJLIB_REC(.) -{ - int n = (int)(L->top - L->base); - RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); - U64double u; - double d; - if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0); - u.u64 = lj_math_random_step(rs); - d = u.d - 1.0; - if (n > 0) { -#if LJ_DUALNUM - int isint = 1; - double r1; - lj_lib_checknumber(L, 1); - if (tvisint(L->base)) { - r1 = (lua_Number)intV(L->base); - } else { - isint = 0; - r1 = numV(L->base); - } -#else - double r1 = lj_lib_checknum(L, 1); -#endif - if (n == 1) { - d = lj_vm_floor(d*r1) + 1.0; /* d is an int in range [1, r1] */ - } else { -#if LJ_DUALNUM - double r2; - lj_lib_checknumber(L, 2); - if (tvisint(L->base+1)) { - r2 = (lua_Number)intV(L->base+1); - } else { - isint = 0; - r2 = numV(L->base+1); - } -#else - double r2 = lj_lib_checknum(L, 2); -#endif - d = lj_vm_floor(d*(r2-r1+1.0)) + r1; /* d is an int in range [r1, r2] */ - } -#if LJ_DUALNUM - if (isint) { - setintV(L->top-1, lj_num2int(d)); - return 1; - } -#endif - } /* else: d is a double in range [0, 1] */ - setnumV(L->top++, d); - return 1; -} - -/* PRNG seed function. */ -LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ -LJLIB_CF(math_randomseed) -{ - RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); - random_init(rs, lj_lib_checknum(L, 1)); - return 0; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_math(lua_State *L) -{ - RandomState *rs; - rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState)); - rs->valid = 0; /* Use lazy initialization to save some time on startup. */ - LJ_LIB_REG(L, LUA_MATHLIBNAME, math); -#if defined(LUA_COMPAT_MOD) && !LJ_52 - lua_getfield(L, -1, "fmod"); - lua_setfield(L, -2, "mod"); -#endif - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_os.c b/third-party/LuaJIT-2.0.2/src/lib_os.c deleted file mode 100644 index 0a78412909..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_os.c +++ /dev/null @@ -1,280 +0,0 @@ -/* -** OS library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include -#include - -#define lib_os_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_lib.h" - -#if LJ_TARGET_POSIX -#include -#else -#include -#endif - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_os - -LJLIB_CF(os_execute) -{ -#if LJ_TARGET_CONSOLE -#if LJ_52 - errno = ENOSYS; - return luaL_fileresult(L, 0, NULL); -#else - lua_pushinteger(L, -1); - return 1; -#endif -#else - const char *cmd = luaL_optstring(L, 1, NULL); - int stat = system(cmd); -#if LJ_52 - if (cmd) - return luaL_execresult(L, stat); - setboolV(L->top++, 1); -#else - setintV(L->top++, stat); -#endif - return 1; -#endif -} - -LJLIB_CF(os_remove) -{ - const char *filename = luaL_checkstring(L, 1); - return luaL_fileresult(L, remove(filename) == 0, filename); -} - -LJLIB_CF(os_rename) -{ - const char *fromname = luaL_checkstring(L, 1); - const char *toname = luaL_checkstring(L, 2); - return luaL_fileresult(L, rename(fromname, toname) == 0, fromname); -} - -LJLIB_CF(os_tmpname) -{ -#if LJ_TARGET_PS3 - lj_err_caller(L, LJ_ERR_OSUNIQF); - return 0; -#else -#if LJ_TARGET_POSIX - char buf[15+1]; - int fp; - strcpy(buf, "/tmp/lua_XXXXXX"); - fp = mkstemp(buf); - if (fp != -1) - close(fp); - else - lj_err_caller(L, LJ_ERR_OSUNIQF); -#else - char buf[L_tmpnam]; - if (tmpnam(buf) == NULL) - lj_err_caller(L, LJ_ERR_OSUNIQF); -#endif - lua_pushstring(L, buf); - return 1; -#endif -} - -LJLIB_CF(os_getenv) -{ -#if LJ_TARGET_CONSOLE - lua_pushnil(L); -#else - lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ -#endif - return 1; -} - -LJLIB_CF(os_exit) -{ - int status; - if (L->base < L->top && tvisbool(L->base)) - status = boolV(L->base) ? EXIT_SUCCESS : EXIT_FAILURE; - else - status = lj_lib_optint(L, 1, EXIT_SUCCESS); - if (L->base+1 < L->top && tvistruecond(L->base+1)) - lua_close(L); - exit(status); - return 0; /* Unreachable. */ -} - -LJLIB_CF(os_clock) -{ - setnumV(L->top++, ((lua_Number)clock())*(1.0/(lua_Number)CLOCKS_PER_SEC)); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void setfield(lua_State *L, const char *key, int value) -{ - lua_pushinteger(L, value); - lua_setfield(L, -2, key); -} - -static void setboolfield(lua_State *L, const char *key, int value) -{ - if (value < 0) /* undefined? */ - return; /* does not set field */ - lua_pushboolean(L, value); - lua_setfield(L, -2, key); -} - -static int getboolfield(lua_State *L, const char *key) -{ - int res; - lua_getfield(L, -1, key); - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); - lua_pop(L, 1); - return res; -} - -static int getfield(lua_State *L, const char *key, int d) -{ - int res; - lua_getfield(L, -1, key); - if (lua_isnumber(L, -1)) { - res = (int)lua_tointeger(L, -1); - } else { - if (d < 0) - lj_err_callerv(L, LJ_ERR_OSDATEF, key); - res = d; - } - lua_pop(L, 1); - return res; -} - -LJLIB_CF(os_date) -{ - const char *s = luaL_optstring(L, 1, "%c"); - time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); - struct tm *stm; -#if LJ_TARGET_POSIX - struct tm rtm; -#endif - if (*s == '!') { /* UTC? */ - s++; /* Skip '!' */ -#if LJ_TARGET_POSIX - stm = gmtime_r(&t, &rtm); -#else - stm = gmtime(&t); -#endif - } else { -#if LJ_TARGET_POSIX - stm = localtime_r(&t, &rtm); -#else - stm = localtime(&t); -#endif - } - if (stm == NULL) { /* Invalid date? */ - setnilV(L->top-1); - } else if (strcmp(s, "*t") == 0) { - lua_createtable(L, 0, 9); /* 9 = number of fields */ - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon+1); - setfield(L, "year", stm->tm_year+1900); - setfield(L, "wday", stm->tm_wday+1); - setfield(L, "yday", stm->tm_yday+1); - setboolfield(L, "isdst", stm->tm_isdst); - } else { - char cc[3]; - luaL_Buffer b; - cc[0] = '%'; cc[2] = '\0'; - luaL_buffinit(L, &b); - for (; *s; s++) { - if (*s != '%' || *(s + 1) == '\0') { /* No conversion specifier? */ - luaL_addchar(&b, *s); - } else { - size_t reslen; - char buff[200]; /* Should be big enough for any conversion result. */ - cc[1] = *(++s); - reslen = strftime(buff, sizeof(buff), cc, stm); - luaL_addlstring(&b, buff, reslen); - } - } - luaL_pushresult(&b); - } - return 1; -} - -LJLIB_CF(os_time) -{ - time_t t; - if (lua_isnoneornil(L, 1)) { /* called without args? */ - t = time(NULL); /* get current time */ - } else { - struct tm ts; - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0); - ts.tm_min = getfield(L, "min", 0); - ts.tm_hour = getfield(L, "hour", 12); - ts.tm_mday = getfield(L, "day", -1); - ts.tm_mon = getfield(L, "month", -1) - 1; - ts.tm_year = getfield(L, "year", -1) - 1900; - ts.tm_isdst = getboolfield(L, "isdst"); - t = mktime(&ts); - } - if (t == (time_t)(-1)) - lua_pushnil(L); - else - lua_pushnumber(L, (lua_Number)t); - return 1; -} - -LJLIB_CF(os_difftime) -{ - lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), - (time_t)(luaL_optnumber(L, 2, (lua_Number)0)))); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -LJLIB_CF(os_setlocale) -{ - GCstr *s = lj_lib_optstr(L, 1); - const char *str = s ? strdata(s) : NULL; - int opt = lj_lib_checkopt(L, 2, 6, - "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all"); - if (opt == 0) opt = LC_CTYPE; - else if (opt == 1) opt = LC_NUMERIC; - else if (opt == 2) opt = LC_TIME; - else if (opt == 3) opt = LC_COLLATE; - else if (opt == 4) opt = LC_MONETARY; - else if (opt == 6) opt = LC_ALL; - lua_pushstring(L, setlocale(opt, str)); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_os(lua_State *L) -{ - LJ_LIB_REG(L, LUA_OSLIBNAME, os); - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_package.c b/third-party/LuaJIT-2.0.2/src/lib_package.c deleted file mode 100644 index f0e672d21a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_package.c +++ /dev/null @@ -1,605 +0,0 @@ -/* -** Package library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2012 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_package_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -/* Error codes for ll_loadfunc. */ -#define PACKAGE_ERR_LIB 1 -#define PACKAGE_ERR_FUNC 2 -#define PACKAGE_ERR_LOAD 3 - -/* Redefined in platform specific part. */ -#define PACKAGE_LIB_FAIL "open" -#define setprogdir(L) ((void)0) - -/* Symbol name prefixes. */ -#define SYMPREFIX_CF "luaopen_%s" -#define SYMPREFIX_BC "luaJIT_BC_%s" - -#if LJ_TARGET_DLOPEN - -#include - -static void ll_unloadlib(void *lib) -{ - dlclose(lib); -} - -static void *ll_load(lua_State *L, const char *path, int gl) -{ - void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL)); - if (lib == NULL) lua_pushstring(L, dlerror()); - return lib; -} - -static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) -{ - lua_CFunction f = (lua_CFunction)dlsym(lib, sym); - if (f == NULL) lua_pushstring(L, dlerror()); - return f; -} - -static const char *ll_bcsym(void *lib, const char *sym) -{ -#if defined(RTLD_DEFAULT) - if (lib == NULL) lib = RTLD_DEFAULT; -#elif LJ_TARGET_OSX || LJ_TARGET_BSD - if (lib == NULL) lib = (void *)(intptr_t)-2; -#endif - return (const char *)dlsym(lib, sym); -} - -#elif LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#ifndef WINVER -#define WINVER 0x0500 -#endif -#include - -#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS -#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4 -#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2 -BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*); -#endif - -#undef setprogdir - -static void setprogdir(lua_State *L) -{ - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) { - luaL_error(L, "unable to get ModuleFileName"); - } else { - *lb = '\0'; - luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - -static void pusherror(lua_State *L) -{ - DWORD error = GetLastError(); - char buffer[128]; - if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error, 0, buffer, sizeof(buffer), NULL)) - lua_pushstring(L, buffer); - else - lua_pushfstring(L, "system error %d\n", error); -} - -static void ll_unloadlib(void *lib) -{ - FreeLibrary((HINSTANCE)lib); -} - -static void *ll_load(lua_State *L, const char *path, int gl) -{ - HINSTANCE lib = LoadLibraryA(path); - if (lib == NULL) pusherror(L); - UNUSED(gl); - return lib; -} - -static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) -{ - lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); - if (f == NULL) pusherror(L); - return f; -} - -static const char *ll_bcsym(void *lib, const char *sym) -{ - if (lib) { - return (const char *)GetProcAddress((HINSTANCE)lib, sym); - } else { - HINSTANCE h = GetModuleHandleA(NULL); - const char *p = (const char *)GetProcAddress(h, sym); - if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (const char *)ll_bcsym, &h)) - p = (const char *)GetProcAddress(h, sym); - return p; - } -} - -#else - -#undef PACKAGE_LIB_FAIL -#define PACKAGE_LIB_FAIL "absent" - -#define DLMSG "dynamic libraries not enabled; no support for target OS" - -static void ll_unloadlib(void *lib) -{ - UNUSED(lib); -} - -static void *ll_load(lua_State *L, const char *path, int gl) -{ - UNUSED(path); UNUSED(gl); - lua_pushliteral(L, DLMSG); - return NULL; -} - -static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) -{ - UNUSED(lib); UNUSED(sym); - lua_pushliteral(L, DLMSG); - return NULL; -} - -static const char *ll_bcsym(void *lib, const char *sym) -{ - UNUSED(lib); UNUSED(sym); - return NULL; -} - -#endif - -/* ------------------------------------------------------------------------ */ - -static void **ll_register(lua_State *L, const char *path) -{ - void **plib; - lua_pushfstring(L, "LOADLIB: %s", path); - lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ - if (!lua_isnil(L, -1)) { /* is there an entry? */ - plib = (void **)lua_touserdata(L, -1); - } else { /* no entry yet; create one */ - lua_pop(L, 1); - plib = (void **)lua_newuserdata(L, sizeof(void *)); - *plib = NULL; - luaL_getmetatable(L, "_LOADLIB"); - lua_setmetatable(L, -2); - lua_pushfstring(L, "LOADLIB: %s", path); - lua_pushvalue(L, -2); - lua_settable(L, LUA_REGISTRYINDEX); - } - return plib; -} - -static const char *mksymname(lua_State *L, const char *modname, - const char *prefix) -{ - const char *funcname; - const char *mark = strchr(modname, *LUA_IGMARK); - if (mark) modname = mark + 1; - funcname = luaL_gsub(L, modname, ".", "_"); - funcname = lua_pushfstring(L, prefix, funcname); - lua_remove(L, -2); /* remove 'gsub' result */ - return funcname; -} - -static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r) -{ - void **reg = ll_register(L, path); - if (*reg == NULL) *reg = ll_load(L, path, (*name == '*')); - if (*reg == NULL) { - return PACKAGE_ERR_LIB; /* Unable to load library. */ - } else if (*name == '*') { /* Only load library into global namespace. */ - lua_pushboolean(L, 1); - return 0; - } else { - const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF); - lua_CFunction f = ll_sym(L, *reg, sym); - if (f) { - lua_pushcfunction(L, f); - return 0; - } - if (!r) { - const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC)); - lua_pop(L, 1); - if (bcdata) { - if (luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) - return PACKAGE_ERR_LOAD; - return 0; - } - } - return PACKAGE_ERR_FUNC; /* Unable to find function. */ - } -} - -static int lj_cf_package_loadlib(lua_State *L) -{ - const char *path = luaL_checkstring(L, 1); - const char *init = luaL_checkstring(L, 2); - int st = ll_loadfunc(L, path, init, 1); - if (st == 0) { /* no errors? */ - return 1; /* return the loaded function */ - } else { /* error; error message is on stack top */ - lua_pushnil(L); - lua_insert(L, -2); - lua_pushstring(L, (st == PACKAGE_ERR_LIB) ? PACKAGE_LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ - } -} - -static int lj_cf_package_unloadlib(lua_State *L) -{ - void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); - if (*lib) ll_unloadlib(*lib); - *lib = NULL; /* mark library as closed */ - return 0; -} - -/* ------------------------------------------------------------------------ */ - -static int readable(const char *filename) -{ - FILE *f = fopen(filename, "r"); /* try to open file */ - if (f == NULL) return 0; /* open failed */ - fclose(f); - return 1; -} - -static const char *pushnexttemplate(lua_State *L, const char *path) -{ - const char *l; - while (*path == *LUA_PATHSEP) path++; /* skip separators */ - if (*path == '\0') return NULL; /* no more templates */ - l = strchr(path, *LUA_PATHSEP); /* find next separator */ - if (l == NULL) l = path + strlen(path); - lua_pushlstring(L, path, (size_t)(l - path)); /* template */ - return l; -} - -static const char *searchpath (lua_State *L, const char *name, - const char *path, const char *sep, - const char *dirsep) -{ - luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); - if (*sep != '\0') /* non-empty separator? */ - name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - while ((path = pushnexttemplate(L, path)) != NULL) { - const char *filename = luaL_gsub(L, lua_tostring(L, -1), - LUA_PATH_MARK, name); - lua_remove(L, -2); /* remove path template */ - if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file " LUA_QS, filename); - lua_remove(L, -2); /* remove file name */ - luaL_addvalue(&msg); /* concatenate error msg. entry */ - } - luaL_pushresult(&msg); /* create error message */ - return NULL; /* not found */ -} - -static int lj_cf_package_searchpath(lua_State *L) -{ - const char *f = searchpath(L, luaL_checkstring(L, 1), - luaL_checkstring(L, 2), - luaL_optstring(L, 3, "."), - luaL_optstring(L, 4, LUA_DIRSEP)); - if (f != NULL) { - return 1; - } else { /* error message is on top of the stack */ - lua_pushnil(L); - lua_insert(L, -2); - return 2; /* return nil + error message */ - } -} - -static const char *findfile(lua_State *L, const char *name, - const char *pname) -{ - const char *path; - lua_getfield(L, LUA_ENVIRONINDEX, pname); - path = lua_tostring(L, -1); - if (path == NULL) - luaL_error(L, LUA_QL("package.%s") " must be a string", pname); - return searchpath(L, name, path, ".", LUA_DIRSEP); -} - -static void loaderror(lua_State *L, const char *filename) -{ - luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", - lua_tostring(L, 1), filename, lua_tostring(L, -1)); -} - -static int lj_cf_package_loader_lua(lua_State *L) -{ - const char *filename; - const char *name = luaL_checkstring(L, 1); - filename = findfile(L, name, "path"); - if (filename == NULL) return 1; /* library not found in this path */ - if (luaL_loadfile(L, filename) != 0) - loaderror(L, filename); - return 1; /* library loaded successfully */ -} - -static int lj_cf_package_loader_c(lua_State *L) -{ - const char *name = luaL_checkstring(L, 1); - const char *filename = findfile(L, name, "cpath"); - if (filename == NULL) return 1; /* library not found in this path */ - if (ll_loadfunc(L, filename, name, 0) != 0) - loaderror(L, filename); - return 1; /* library loaded successfully */ -} - -static int lj_cf_package_loader_croot(lua_State *L) -{ - const char *filename; - const char *name = luaL_checkstring(L, 1); - const char *p = strchr(name, '.'); - int st; - if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, (size_t)(p - name)); - filename = findfile(L, lua_tostring(L, -1), "cpath"); - if (filename == NULL) return 1; /* root not found */ - if ((st = ll_loadfunc(L, filename, name, 0)) != 0) { - if (st != PACKAGE_ERR_FUNC) loaderror(L, filename); /* real error */ - lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, - name, filename); - return 1; /* function not found */ - } - return 1; -} - -static int lj_cf_package_loader_preload(lua_State *L) -{ - const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_ENVIRONINDEX, "preload"); - if (!lua_istable(L, -1)) - luaL_error(L, LUA_QL("package.preload") " must be a table"); - lua_getfield(L, -1, name); - if (lua_isnil(L, -1)) { /* Not found? */ - const char *bcname = mksymname(L, name, SYMPREFIX_BC); - const char *bcdata = ll_bcsym(NULL, bcname); - if (bcdata == NULL || luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); - } - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static const int sentinel_ = 0; -#define sentinel ((void *)&sentinel_) - -static int lj_cf_package_require(lua_State *L) -{ - const char *name = luaL_checkstring(L, 1); - int i; - lua_settop(L, 1); /* _LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, 2, name); - if (lua_toboolean(L, -1)) { /* is it there? */ - if (lua_touserdata(L, -1) == sentinel) /* check loops */ - luaL_error(L, "loop or previous error loading module " LUA_QS, name); - return 1; /* package is already loaded */ - } - /* else must load it; iterate over available loaders */ - lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); - if (!lua_istable(L, -1)) - luaL_error(L, LUA_QL("package.loaders") " must be a table"); - lua_pushliteral(L, ""); /* error message accumulator */ - for (i = 1; ; i++) { - lua_rawgeti(L, -2, i); /* get a loader */ - if (lua_isnil(L, -1)) - luaL_error(L, "module " LUA_QS " not found:%s", - name, lua_tostring(L, -2)); - lua_pushstring(L, name); - lua_call(L, 1, 1); /* call it */ - if (lua_isfunction(L, -1)) /* did it find module? */ - break; /* module loaded successfully */ - else if (lua_isstring(L, -1)) /* loader returned error message? */ - lua_concat(L, 2); /* accumulate it */ - else - lua_pop(L, 1); - } - lua_pushlightuserdata(L, sentinel); - lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ - lua_pushstring(L, name); /* pass name as argument to module */ - lua_call(L, 1, 1); /* run loaded module */ - if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ - lua_getfield(L, 2, name); - if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ - lua_pushboolean(L, 1); /* use true as result */ - lua_pushvalue(L, -1); /* extra copy to be returned */ - lua_setfield(L, 2, name); /* _LOADED[name] = true */ - } - lj_lib_checkfpu(L); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void setfenv(lua_State *L) -{ - lua_Debug ar; - if (lua_getstack(L, 1, &ar) == 0 || - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ - lua_iscfunction(L, -1)) - luaL_error(L, LUA_QL("module") " not called from a Lua function"); - lua_pushvalue(L, -2); - lua_setfenv(L, -2); - lua_pop(L, 1); -} - -static void dooptions(lua_State *L, int n) -{ - int i; - for (i = 2; i <= n; i++) { - lua_pushvalue(L, i); /* get option (a function) */ - lua_pushvalue(L, -2); /* module */ - lua_call(L, 1, 0); - } -} - -static void modinit(lua_State *L, const char *modname) -{ - const char *dot; - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_M"); /* module._M = module */ - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, (size_t)(dot - modname)); - lua_setfield(L, -2, "_PACKAGE"); -} - -static int lj_cf_package_module(lua_State *L) -{ - const char *modname = luaL_checkstring(L, 1); - int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ - if (!lua_istable(L, -1)) { /* not found? */ - lua_pop(L, 1); /* remove previous result */ - /* try global variable (and create one if it does not exist) */ - if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) - lj_err_callerv(L, LJ_ERR_BADMODN, modname); - lua_pushvalue(L, -1); - lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ - } - /* check whether table already has a _NAME field */ - lua_getfield(L, -1, "_NAME"); - if (!lua_isnil(L, -1)) { /* is table an initialized module? */ - lua_pop(L, 1); - } else { /* no; initialize it */ - lua_pop(L, 1); - modinit(L, modname); - } - lua_pushvalue(L, -1); - setfenv(L); - dooptions(L, loaded - 1); - return 0; -} - -static int lj_cf_package_seeall(lua_State *L) -{ - luaL_checktype(L, 1, LUA_TTABLE); - if (!lua_getmetatable(L, 1)) { - lua_createtable(L, 0, 1); /* create new metatable */ - lua_pushvalue(L, -1); - lua_setmetatable(L, 1); - } - lua_pushvalue(L, LUA_GLOBALSINDEX); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - return 0; -} - -/* ------------------------------------------------------------------------ */ - -#define AUXMARK "\1" - -static void setpath(lua_State *L, const char *fieldname, const char *envname, - const char *def, int noenv) -{ -#if LJ_TARGET_CONSOLE - const char *path = NULL; - UNUSED(envname); -#else - const char *path = getenv(envname); -#endif - if (path == NULL || noenv) { - lua_pushstring(L, def); - } else { - path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, - LUA_PATHSEP AUXMARK LUA_PATHSEP); - luaL_gsub(L, path, AUXMARK, def); - lua_remove(L, -2); - } - setprogdir(L); - lua_setfield(L, -2, fieldname); -} - -static const luaL_Reg package_lib[] = { - { "loadlib", lj_cf_package_loadlib }, - { "searchpath", lj_cf_package_searchpath }, - { "seeall", lj_cf_package_seeall }, - { NULL, NULL } -}; - -static const luaL_Reg package_global[] = { - { "module", lj_cf_package_module }, - { "require", lj_cf_package_require }, - { NULL, NULL } -}; - -static const lua_CFunction package_loaders[] = -{ - lj_cf_package_loader_preload, - lj_cf_package_loader_lua, - lj_cf_package_loader_c, - lj_cf_package_loader_croot, - NULL -}; - -LUALIB_API int luaopen_package(lua_State *L) -{ - int i; - int noenv; - luaL_newmetatable(L, "_LOADLIB"); - lj_lib_pushcf(L, lj_cf_package_unloadlib, 1); - lua_setfield(L, -2, "__gc"); - luaL_register(L, LUA_LOADLIBNAME, package_lib); - lua_pushvalue(L, -1); - lua_replace(L, LUA_ENVIRONINDEX); - lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0); - for (i = 0; package_loaders[i] != NULL; i++) { - lj_lib_pushcf(L, package_loaders[i], 1); - lua_rawseti(L, -2, i+1); - } - lua_setfield(L, -2, "loaders"); - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - noenv = lua_toboolean(L, -1); - lua_pop(L, 1); - setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv); - setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv); - lua_pushliteral(L, LUA_PATH_CONFIG); - lua_setfield(L, -2, "config"); - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); - lua_setfield(L, -2, "loaded"); - luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4); - lua_setfield(L, -2, "preload"); - lua_pushvalue(L, LUA_GLOBALSINDEX); - luaL_register(L, NULL, package_global); - lua_pop(L, 1); - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lib_string.c b/third-party/LuaJIT-2.0.2/src/lib_string.c deleted file mode 100644 index 9e8ab900fd..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lib_string.c +++ /dev/null @@ -1,940 +0,0 @@ -/* -** String library. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include - -#define lib_string_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_ff.h" -#include "lj_bcdump.h" -#include "lj_char.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_string - -LJLIB_ASM(string_len) LJLIB_REC(.) -{ - lj_lib_checkstr(L, 1); - return FFH_RETRY; -} - -LJLIB_ASM(string_byte) LJLIB_REC(string_range 0) -{ - GCstr *s = lj_lib_checkstr(L, 1); - int32_t len = (int32_t)s->len; - int32_t start = lj_lib_optint(L, 2, 1); - int32_t stop = lj_lib_optint(L, 3, start); - int32_t n, i; - const unsigned char *p; - if (stop < 0) stop += len+1; - if (start < 0) start += len+1; - if (start <= 0) start = 1; - if (stop > len) stop = len; - if (start > stop) return FFH_RES(0); /* Empty interval: return no results. */ - start--; - n = stop - start; - if ((uint32_t)n > LUAI_MAXCSTACK) - lj_err_caller(L, LJ_ERR_STRSLC); - lj_state_checkstack(L, (MSize)n); - p = (const unsigned char *)strdata(s) + start; - for (i = 0; i < n; i++) - setintV(L->base + i-1, p[i]); - return FFH_RES(n); -} - -LJLIB_ASM(string_char) -{ - int i, nargs = (int)(L->top - L->base); - char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, (size_t)nargs); - for (i = 1; i <= nargs; i++) { - int32_t k = lj_lib_checkint(L, i); - if (!checku8(k)) - lj_err_arg(L, i, LJ_ERR_BADVAL); - buf[i-1] = (char)k; - } - setstrV(L, L->base-1, lj_str_new(L, buf, (size_t)nargs)); - return FFH_RES(1); -} - -LJLIB_ASM(string_sub) LJLIB_REC(string_range 1) -{ - lj_lib_checkstr(L, 1); - lj_lib_checkint(L, 2); - setintV(L->base+2, lj_lib_optint(L, 3, -1)); - return FFH_RETRY; -} - -LJLIB_ASM(string_rep) -{ - GCstr *s = lj_lib_checkstr(L, 1); - int32_t k = lj_lib_checkint(L, 2); - GCstr *sep = lj_lib_optstr(L, 3); - int32_t len = (int32_t)s->len; - global_State *g = G(L); - int64_t tlen; - const char *src; - char *buf; - if (k <= 0) { - empty: - setstrV(L, L->base-1, &g->strempty); - return FFH_RES(1); - } - if (sep) { - tlen = (int64_t)len + sep->len; - if (tlen > LJ_MAX_STR) - lj_err_caller(L, LJ_ERR_STROV); - tlen *= k; - if (tlen > LJ_MAX_STR) - lj_err_caller(L, LJ_ERR_STROV); - } else { - tlen = (int64_t)k * len; - if (tlen > LJ_MAX_STR) - lj_err_caller(L, LJ_ERR_STROV); - } - if (tlen == 0) goto empty; - buf = lj_str_needbuf(L, &g->tmpbuf, (MSize)tlen); - src = strdata(s); - if (sep) { - tlen -= sep->len; /* Ignore trailing separator. */ - if (k > 1) { /* Paste one string and one separator. */ - int32_t i; - i = 0; while (i < len) *buf++ = src[i++]; - src = strdata(sep); len = sep->len; - i = 0; while (i < len) *buf++ = src[i++]; - src = g->tmpbuf.buf; len += s->len; k--; /* Now copy that k-1 times. */ - } - } - do { - int32_t i = 0; - do { *buf++ = src[i++]; } while (i < len); - } while (--k > 0); - setstrV(L, L->base-1, lj_str_new(L, g->tmpbuf.buf, (size_t)tlen)); - return FFH_RES(1); -} - -LJLIB_ASM(string_reverse) -{ - GCstr *s = lj_lib_checkstr(L, 1); - lj_str_needbuf(L, &G(L)->tmpbuf, s->len); - return FFH_RETRY; -} -LJLIB_ASM_(string_lower) -LJLIB_ASM_(string_upper) - -/* ------------------------------------------------------------------------ */ - -static int writer_buf(lua_State *L, const void *p, size_t size, void *b) -{ - luaL_addlstring((luaL_Buffer *)b, (const char *)p, size); - UNUSED(L); - return 0; -} - -LJLIB_CF(string_dump) -{ - GCfunc *fn = lj_lib_checkfunc(L, 1); - int strip = L->base+1 < L->top && tvistruecond(L->base+1); - luaL_Buffer b; - L->top = L->base+1; - luaL_buffinit(L, &b); - if (!isluafunc(fn) || lj_bcwrite(L, funcproto(fn), writer_buf, &b, strip)) - lj_err_caller(L, LJ_ERR_STRDUMP); - luaL_pushresult(&b); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -/* macro to `unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - -#define CAP_UNFINISHED (-1) -#define CAP_POSITION (-2) - -typedef struct MatchState { - const char *src_init; /* init of source string */ - const char *src_end; /* end (`\0') of source string */ - lua_State *L; - int level; /* total number of captures (finished or unfinished) */ - int depth; - struct { - const char *init; - ptrdiff_t len; - } capture[LUA_MAXCAPTURES]; -} MatchState; - -#define L_ESC '%' -#define SPECIALS "^$*+?.([%-" - -static int check_capture(MatchState *ms, int l) -{ - l -= '1'; - if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) - lj_err_caller(ms->L, LJ_ERR_STRCAPI); - return l; -} - -static int capture_to_close(MatchState *ms) -{ - int level = ms->level; - for (level--; level>=0; level--) - if (ms->capture[level].len == CAP_UNFINISHED) return level; - lj_err_caller(ms->L, LJ_ERR_STRPATC); - return 0; /* unreachable */ -} - -static const char *classend(MatchState *ms, const char *p) -{ - switch (*p++) { - case L_ESC: - if (*p == '\0') - lj_err_caller(ms->L, LJ_ERR_STRPATE); - return p+1; - case '[': - if (*p == '^') p++; - do { /* look for a `]' */ - if (*p == '\0') - lj_err_caller(ms->L, LJ_ERR_STRPATM); - if (*(p++) == L_ESC && *p != '\0') - p++; /* skip escapes (e.g. `%]') */ - } while (*p != ']'); - return p+1; - default: - return p; - } -} - -static const unsigned char match_class_map[32] = { - 0,LJ_CHAR_ALPHA,0,LJ_CHAR_CNTRL,LJ_CHAR_DIGIT,0,0,LJ_CHAR_GRAPH,0,0,0,0, - LJ_CHAR_LOWER,0,0,0,LJ_CHAR_PUNCT,0,0,LJ_CHAR_SPACE,0, - LJ_CHAR_UPPER,0,LJ_CHAR_ALNUM,LJ_CHAR_XDIGIT,0,0,0,0,0,0,0 -}; - -static int match_class(int c, int cl) -{ - if ((cl & 0xc0) == 0x40) { - int t = match_class_map[(cl&0x1f)]; - if (t) { - t = lj_char_isa(c, t); - return (cl & 0x20) ? t : !t; - } - if (cl == 'z') return c == 0; - if (cl == 'Z') return c != 0; - } - return (cl == c); -} - -static int matchbracketclass(int c, const char *p, const char *ec) -{ - int sig = 1; - if (*(p+1) == '^') { - sig = 0; - p++; /* skip the `^' */ - } - while (++p < ec) { - if (*p == L_ESC) { - p++; - if (match_class(c, uchar(*p))) - return sig; - } - else if ((*(p+1) == '-') && (p+2 < ec)) { - p+=2; - if (uchar(*(p-2)) <= c && c <= uchar(*p)) - return sig; - } - else if (uchar(*p) == c) return sig; - } - return !sig; -} - -static int singlematch(int c, const char *p, const char *ep) -{ - switch (*p) { - case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); - case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); - } -} - -static const char *match(MatchState *ms, const char *s, const char *p); - -static const char *matchbalance(MatchState *ms, const char *s, const char *p) -{ - if (*p == 0 || *(p+1) == 0) - lj_err_caller(ms->L, LJ_ERR_STRPATU); - if (*s != *p) { - return NULL; - } else { - int b = *p; - int e = *(p+1); - int cont = 1; - while (++s < ms->src_end) { - if (*s == e) { - if (--cont == 0) return s+1; - } else if (*s == b) { - cont++; - } - } - } - return NULL; /* string ends out of balance */ -} - -static const char *max_expand(MatchState *ms, const char *s, - const char *p, const char *ep) -{ - ptrdiff_t i = 0; /* counts maximum expand for item */ - while ((s+i)src_end && singlematch(uchar(*(s+i)), p, ep)) - i++; - /* keeps trying to match with the maximum repetitions */ - while (i>=0) { - const char *res = match(ms, (s+i), ep+1); - if (res) return res; - i--; /* else didn't match; reduce 1 repetition to try again */ - } - return NULL; -} - -static const char *min_expand(MatchState *ms, const char *s, - const char *p, const char *ep) -{ - for (;;) { - const char *res = match(ms, s, ep+1); - if (res != NULL) - return res; - else if (ssrc_end && singlematch(uchar(*s), p, ep)) - s++; /* try with one more repetition */ - else - return NULL; - } -} - -static const char *start_capture(MatchState *ms, const char *s, - const char *p, int what) -{ - const char *res; - int level = ms->level; - if (level >= LUA_MAXCAPTURES) lj_err_caller(ms->L, LJ_ERR_STRCAPN); - ms->capture[level].init = s; - ms->capture[level].len = what; - ms->level = level+1; - if ((res=match(ms, s, p)) == NULL) /* match failed? */ - ms->level--; /* undo capture */ - return res; -} - -static const char *end_capture(MatchState *ms, const char *s, - const char *p) -{ - int l = capture_to_close(ms); - const char *res; - ms->capture[l].len = s - ms->capture[l].init; /* close capture */ - if ((res = match(ms, s, p)) == NULL) /* match failed? */ - ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ - return res; -} - -static const char *match_capture(MatchState *ms, const char *s, int l) -{ - size_t len; - l = check_capture(ms, l); - len = (size_t)ms->capture[l].len; - if ((size_t)(ms->src_end-s) >= len && - memcmp(ms->capture[l].init, s, len) == 0) - return s+len; - else - return NULL; -} - -static const char *match(MatchState *ms, const char *s, const char *p) -{ - if (++ms->depth > LJ_MAX_XLEVEL) - lj_err_caller(ms->L, LJ_ERR_STRPATX); - init: /* using goto's to optimize tail recursion */ - switch (*p) { - case '(': /* start capture */ - if (*(p+1) == ')') /* position capture? */ - s = start_capture(ms, s, p+2, CAP_POSITION); - else - s = start_capture(ms, s, p+1, CAP_UNFINISHED); - break; - case ')': /* end capture */ - s = end_capture(ms, s, p+1); - break; - case L_ESC: - switch (*(p+1)) { - case 'b': /* balanced string? */ - s = matchbalance(ms, s, p+2); - if (s == NULL) break; - p+=4; - goto init; /* else s = match(ms, s, p+4); */ - case 'f': { /* frontier? */ - const char *ep; char previous; - p += 2; - if (*p != '[') - lj_err_caller(ms->L, LJ_ERR_STRPATB); - ep = classend(ms, p); /* points to what is next */ - previous = (s == ms->src_init) ? '\0' : *(s-1); - if (matchbracketclass(uchar(previous), p, ep-1) || - !matchbracketclass(uchar(*s), p, ep-1)) { s = NULL; break; } - p=ep; - goto init; /* else s = match(ms, s, ep); */ - } - default: - if (lj_char_isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p+1))); - if (s == NULL) break; - p+=2; - goto init; /* else s = match(ms, s, p+2) */ - } - goto dflt; /* case default */ - } - break; - case '\0': /* end of pattern */ - break; /* match succeeded */ - case '$': - /* is the `$' the last char in pattern? */ - if (*(p+1) != '\0') goto dflt; - if (s != ms->src_end) s = NULL; /* check end of string */ - break; - default: dflt: { /* it is a pattern item */ - const char *ep = classend(ms, p); /* points to what is next */ - int m = ssrc_end && singlematch(uchar(*s), p, ep); - switch (*ep) { - case '?': { /* optional */ - const char *res; - if (m && ((res=match(ms, s+1, ep+1)) != NULL)) { - s = res; - break; - } - p=ep+1; - goto init; /* else s = match(ms, s, ep+1); */ - } - case '*': /* 0 or more repetitions */ - s = max_expand(ms, s, p, ep); - break; - case '+': /* 1 or more repetitions */ - s = (m ? max_expand(ms, s+1, p, ep) : NULL); - break; - case '-': /* 0 or more repetitions (minimum) */ - s = min_expand(ms, s, p, ep); - break; - default: - if (m) { s++; p=ep; goto init; } /* else s = match(ms, s+1, ep); */ - s = NULL; - break; - } - break; - } - } - ms->depth--; - return s; -} - -static const char *lmemfind(const char *s1, size_t l1, - const char *s2, size_t l2) -{ - if (l2 == 0) { - return s1; /* empty strings are everywhere */ - } else if (l2 > l1) { - return NULL; /* avoids a negative `l1' */ - } else { - const char *init; /* to search for a `*s2' inside `s1' */ - l2--; /* 1st char will be checked by `memchr' */ - l1 = l1-l2; /* `s2' cannot be found after that */ - while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { - init++; /* 1st char is already checked */ - if (memcmp(init, s2+1, l2) == 0) { - return init-1; - } else { /* correct `l1' and `s1' to try again */ - l1 -= (size_t)(init-s1); - s1 = init; - } - } - return NULL; /* not found */ - } -} - -static void push_onecapture(MatchState *ms, int i, const char *s, const char *e) -{ - if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, (size_t)(e - s)); /* add whole match */ - else - lj_err_caller(ms->L, LJ_ERR_STRCAPI); - } else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) lj_err_caller(ms->L, LJ_ERR_STRCAPU); - if (l == CAP_POSITION) - lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, (size_t)l); - } -} - -static int push_captures(MatchState *ms, const char *s, const char *e) -{ - int i; - int nlevels = (ms->level == 0 && s) ? 1 : ms->level; - luaL_checkstack(ms->L, nlevels, "too many captures"); - for (i = 0; i < nlevels; i++) - push_onecapture(ms, i, s, e); - return nlevels; /* number of strings pushed */ -} - -static ptrdiff_t posrelat(ptrdiff_t pos, size_t len) -{ - /* relative string position: negative means back from end */ - if (pos < 0) pos += (ptrdiff_t)len + 1; - return (pos >= 0) ? pos : 0; -} - -static int str_find_aux(lua_State *L, int find) -{ - size_t l1, l2; - const char *s = luaL_checklstring(L, 1, &l1); - const char *p = luaL_checklstring(L, 2, &l2); - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; - if (init < 0) { - init = 0; - } else if ((size_t)(init) > l1) { -#if LJ_52 - setnilV(L->top-1); - return 1; -#else - init = (ptrdiff_t)l1; -#endif - } - if (find && (lua_toboolean(L, 4) || /* explicit request? */ - strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ - /* do a plain search */ - const char *s2 = lmemfind(s+init, l1-(size_t)init, p, l2); - if (s2) { - lua_pushinteger(L, s2-s+1); - lua_pushinteger(L, s2-s+(ptrdiff_t)l2); - return 2; - } - } else { - MatchState ms; - int anchor = (*p == '^') ? (p++, 1) : 0; - const char *s1=s+init; - ms.L = L; - ms.src_init = s; - ms.src_end = s+l1; - do { - const char *res; - ms.level = ms.depth = 0; - if ((res=match(&ms, s1, p)) != NULL) { - if (find) { - lua_pushinteger(L, s1-s+1); /* start */ - lua_pushinteger(L, res-s); /* end */ - return push_captures(&ms, NULL, 0) + 2; - } else { - return push_captures(&ms, s1, res); - } - } - } while (s1++ < ms.src_end && !anchor); - } - lua_pushnil(L); /* not found */ - return 1; -} - -LJLIB_CF(string_find) -{ - return str_find_aux(L, 1); -} - -LJLIB_CF(string_match) -{ - return str_find_aux(L, 0); -} - -LJLIB_NOREG LJLIB_CF(string_gmatch_aux) -{ - const char *p = strVdata(lj_lib_upvalue(L, 2)); - GCstr *str = strV(lj_lib_upvalue(L, 1)); - const char *s = strdata(str); - TValue *tvpos = lj_lib_upvalue(L, 3); - const char *src = s + tvpos->u32.lo; - MatchState ms; - ms.L = L; - ms.src_init = s; - ms.src_end = s + str->len; - for (; src <= ms.src_end; src++) { - const char *e; - ms.level = ms.depth = 0; - if ((e = match(&ms, src, p)) != NULL) { - int32_t pos = (int32_t)(e - s); - if (e == src) pos++; /* Ensure progress for empty match. */ - tvpos->u32.lo = (uint32_t)pos; - return push_captures(&ms, src, e); - } - } - return 0; /* not found */ -} - -LJLIB_CF(string_gmatch) -{ - lj_lib_checkstr(L, 1); - lj_lib_checkstr(L, 2); - L->top = L->base+3; - (L->top-1)->u64 = 0; - lj_lib_pushcc(L, lj_cf_string_gmatch_aux, FF_string_gmatch_aux, 3); - return 1; -} - -static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e) -{ - size_t l, i; - const char *news = lua_tolstring(ms->L, 3, &l); - for (i = 0; i < l; i++) { - if (news[i] != L_ESC) { - luaL_addchar(b, news[i]); - } else { - i++; /* skip ESC */ - if (!lj_char_isdigit(uchar(news[i]))) { - luaL_addchar(b, news[i]); - } else if (news[i] == '0') { - luaL_addlstring(b, s, (size_t)(e - s)); - } else { - push_onecapture(ms, news[i] - '1', s, e); - luaL_addvalue(b); /* add capture to accumulated result */ - } - } - } -} - -static void add_value(MatchState *ms, luaL_Buffer *b, - const char *s, const char *e) -{ - lua_State *L = ms->L; - switch (lua_type(L, 3)) { - case LUA_TNUMBER: - case LUA_TSTRING: { - add_s(ms, b, s, e); - return; - } - case LUA_TFUNCTION: { - int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); - break; - } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); - lua_gettable(L, 3); - break; - } - } - if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, (size_t)(e - s)); /* keep original text */ - } else if (!lua_isstring(L, -1)) { - lj_err_callerv(L, LJ_ERR_STRGSRV, luaL_typename(L, -1)); - } - luaL_addvalue(b); /* add result to accumulator */ -} - -LJLIB_CF(string_gsub) -{ - size_t srcl; - const char *src = luaL_checklstring(L, 1, &srcl); - const char *p = luaL_checkstring(L, 2); - int tr = lua_type(L, 3); - int max_s = luaL_optint(L, 4, (int)(srcl+1)); - int anchor = (*p == '^') ? (p++, 1) : 0; - int n = 0; - MatchState ms; - luaL_Buffer b; - if (!(tr == LUA_TNUMBER || tr == LUA_TSTRING || - tr == LUA_TFUNCTION || tr == LUA_TTABLE)) - lj_err_arg(L, 3, LJ_ERR_NOSFT); - luaL_buffinit(L, &b); - ms.L = L; - ms.src_init = src; - ms.src_end = src+srcl; - while (n < max_s) { - const char *e; - ms.level = ms.depth = 0; - e = match(&ms, src, p); - if (e) { - n++; - add_value(&ms, &b, src, e); - } - if (e && e>src) /* non empty match? */ - src = e; /* skip it */ - else if (src < ms.src_end) - luaL_addchar(&b, *src++); - else - break; - if (anchor) - break; - } - luaL_addlstring(&b, src, (size_t)(ms.src_end-src)); - luaL_pushresult(&b); - lua_pushinteger(L, n); /* number of substitutions */ - return 2; -} - -/* ------------------------------------------------------------------------ */ - -/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ -#define MAX_FMTITEM 512 -/* valid flags in a format specification */ -#define FMT_FLAGS "-+ #0" -/* -** maximum size of each format specification (such as '%-099.99d') -** (+10 accounts for %99.99x plus margin of error) -*/ -#define MAX_FMTSPEC (sizeof(FMT_FLAGS) + sizeof(LUA_INTFRMLEN) + 10) - -static void addquoted(lua_State *L, luaL_Buffer *b, int arg) -{ - GCstr *str = lj_lib_checkstr(L, arg); - int32_t len = (int32_t)str->len; - const char *s = strdata(str); - luaL_addchar(b, '"'); - while (len--) { - uint32_t c = uchar(*s); - if (c == '"' || c == '\\' || c == '\n') { - luaL_addchar(b, '\\'); - } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */ - uint32_t d; - luaL_addchar(b, '\\'); - if (c >= 100 || lj_char_isdigit(uchar(s[1]))) { - luaL_addchar(b, '0'+(c >= 100)); if (c >= 100) c -= 100; - goto tens; - } else if (c >= 10) { - tens: - d = (c * 205) >> 11; c -= d * 10; luaL_addchar(b, '0'+d); - } - c += '0'; - } - luaL_addchar(b, c); - s++; - } - luaL_addchar(b, '"'); -} - -static const char *scanformat(lua_State *L, const char *strfrmt, char *form) -{ - const char *p = strfrmt; - while (*p != '\0' && strchr(FMT_FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FMT_FLAGS)) - lj_err_caller(L, LJ_ERR_STRFMTR); - if (lj_char_isdigit(uchar(*p))) p++; /* skip width */ - if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (lj_char_isdigit(uchar(*p))) p++; /* skip precision */ - if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */ - } - if (lj_char_isdigit(uchar(*p))) - lj_err_caller(L, LJ_ERR_STRFMTW); - *(form++) = '%'; - strncpy(form, strfrmt, (size_t)(p - strfrmt + 1)); - form += p - strfrmt + 1; - *form = '\0'; - return p; -} - -static void addintlen(char *form) -{ - size_t l = strlen(form); - char spec = form[l - 1]; - strcpy(form + l - 1, LUA_INTFRMLEN); - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; -} - -static unsigned LUA_INTFRM_T num2intfrm(lua_State *L, int arg) -{ - if (sizeof(LUA_INTFRM_T) == 4) { - return (LUA_INTFRM_T)lj_lib_checkbit(L, arg); - } else { - cTValue *o; - lj_lib_checknumber(L, arg); - o = L->base+arg-1; - if (tvisint(o)) - return (LUA_INTFRM_T)intV(o); - else - return (LUA_INTFRM_T)numV(o); - } -} - -static unsigned LUA_INTFRM_T num2uintfrm(lua_State *L, int arg) -{ - if (sizeof(LUA_INTFRM_T) == 4) { - return (unsigned LUA_INTFRM_T)lj_lib_checkbit(L, arg); - } else { - cTValue *o; - lj_lib_checknumber(L, arg); - o = L->base+arg-1; - if (tvisint(o)) - return (unsigned LUA_INTFRM_T)intV(o); - else if ((int32_t)o->u32.hi < 0) - return (unsigned LUA_INTFRM_T)(LUA_INTFRM_T)numV(o); - else - return (unsigned LUA_INTFRM_T)numV(o); - } -} - -static GCstr *meta_tostring(lua_State *L, int arg) -{ - TValue *o = L->base+arg-1; - cTValue *mo; - lua_assert(o < L->top); /* Caller already checks for existence. */ - if (LJ_LIKELY(tvisstr(o))) - return strV(o); - if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { - copyTV(L, L->top++, mo); - copyTV(L, L->top++, o); - lua_call(L, 1, 1); - L->top--; - if (tvisstr(L->top)) - return strV(L->top); - o = L->base+arg-1; - copyTV(L, o, L->top); - } - if (tvisnumber(o)) { - return lj_str_fromnumber(L, o); - } else if (tvisnil(o)) { - return lj_str_newlit(L, "nil"); - } else if (tvisfalse(o)) { - return lj_str_newlit(L, "false"); - } else if (tvistrue(o)) { - return lj_str_newlit(L, "true"); - } else { - if (tvisfunc(o) && isffunc(funcV(o))) - lj_str_pushf(L, "function: builtin#%d", funcV(o)->c.ffid); - else - lj_str_pushf(L, "%s: %p", lj_typename(o), lua_topointer(L, arg)); - L->top--; - return strV(L->top); - } -} - -LJLIB_CF(string_format) -{ - int arg = 1, top = (int)(L->top - L->base); - GCstr *fmt = lj_lib_checkstr(L, arg); - const char *strfrmt = strdata(fmt); - const char *strfrmt_end = strfrmt + fmt->len; - luaL_Buffer b; - luaL_buffinit(L, &b); - while (strfrmt < strfrmt_end) { - if (*strfrmt != L_ESC) { - luaL_addchar(&b, *strfrmt++); - } else if (*++strfrmt == L_ESC) { - luaL_addchar(&b, *strfrmt++); /* %% */ - } else { /* format item */ - char form[MAX_FMTSPEC]; /* to store the format (`%...') */ - char buff[MAX_FMTITEM]; /* to store the formatted item */ - if (++arg > top) - luaL_argerror(L, arg, lj_obj_typename[0]); - strfrmt = scanformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': - sprintf(buff, form, lj_lib_checkint(L, arg)); - break; - case 'd': case 'i': - addintlen(form); - sprintf(buff, form, num2intfrm(L, arg)); - break; - case 'o': case 'u': case 'x': case 'X': - addintlen(form); - sprintf(buff, form, num2uintfrm(L, arg)); - break; - case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A': { - TValue tv; - tv.n = lj_lib_checknum(L, arg); - if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) { - /* Canonicalize output of non-finite values. */ - char *p, nbuf[LJ_STR_NUMBUF]; - size_t len = lj_str_bufnum(nbuf, &tv); - if (strfrmt[-1] < 'a') { - nbuf[len-3] = nbuf[len-3] - 0x20; - nbuf[len-2] = nbuf[len-2] - 0x20; - nbuf[len-1] = nbuf[len-1] - 0x20; - } - nbuf[len] = '\0'; - for (p = form; *p < 'A' && *p != '.'; p++) ; - *p++ = 's'; *p = '\0'; - sprintf(buff, form, nbuf); - break; - } - sprintf(buff, form, (double)tv.n); - break; - } - case 'q': - addquoted(L, &b, arg); - continue; - case 'p': - lj_str_pushf(L, "%p", lua_topointer(L, arg)); - luaL_addvalue(&b); - continue; - case 's': { - GCstr *str = meta_tostring(L, arg); - if (!strchr(form, '.') && str->len >= 100) { - /* no precision and string is too long to be formatted; - keep original string */ - setstrV(L, L->top++, str); - luaL_addvalue(&b); - continue; - } - sprintf(buff, form, strdata(str)); - break; - } - default: - lj_err_callerv(L, LJ_ERR_STRFMTO, *(strfrmt -1)); - break; - } - luaL_addlstring(&b, buff, strlen(buff)); - } - } - luaL_pushresult(&b); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_string(lua_State *L) -{ - GCtab *mt; - global_State *g; - LJ_LIB_REG(L, LUA_STRLIBNAME, string); -#if defined(LUA_COMPAT_GFIND) && !LJ_52 - lua_getfield(L, -1, "gmatch"); - lua_setfield(L, -2, "gfind"); -#endif - mt = lj_tab_new(L, 0, 1); - /* NOBARRIER: basemt is a GC root. */ - g = G(L); - setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt)); - settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1)); - mt->nomm = (uint8_t)(~(1u<top, func); - setintV(L->top+1, i); - val = lj_tab_getint(t, (int32_t)i); - if (val) { copyTV(L, L->top+2, val); } else { setnilV(L->top+2); } - L->top += 3; - lua_call(L, 2, 1); - if (!tvisnil(L->top-1)) - return 1; - L->top--; - } - return 0; -} - -LJLIB_CF(table_foreach) -{ - GCtab *t = lj_lib_checktab(L, 1); - GCfunc *func = lj_lib_checkfunc(L, 2); - L->top = L->base+3; - setnilV(L->top-1); - while (lj_tab_next(L, t, L->top-1)) { - copyTV(L, L->top+2, L->top); - copyTV(L, L->top+1, L->top-1); - setfuncV(L, L->top, func); - L->top += 3; - lua_call(L, 2, 1); - if (!tvisnil(L->top-1)) - return 1; - L->top--; - } - return 0; -} - -LJLIB_ASM(table_getn) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - return FFH_UNREACHABLE; -} - -LJLIB_CF(table_maxn) -{ - GCtab *t = lj_lib_checktab(L, 1); - TValue *array = tvref(t->array); - Node *node; - lua_Number m = 0; - ptrdiff_t i; - for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--) - if (!tvisnil(&array[i])) { - m = (lua_Number)(int32_t)i; - break; - } - node = noderef(t->node); - for (i = (ptrdiff_t)t->hmask; i >= 0; i--) - if (!tvisnil(&node[i].val) && tvisnumber(&node[i].key)) { - lua_Number n = numberVnum(&node[i].key); - if (n > m) m = n; - } - setnumV(L->top-1, m); - return 1; -} - -LJLIB_CF(table_insert) LJLIB_REC(.) -{ - GCtab *t = lj_lib_checktab(L, 1); - int32_t n, i = (int32_t)lj_tab_len(t) + 1; - int nargs = (int)((char *)L->top - (char *)L->base); - if (nargs != 2*sizeof(TValue)) { - if (nargs != 3*sizeof(TValue)) - lj_err_caller(L, LJ_ERR_TABINS); - /* NOBARRIER: This just moves existing elements around. */ - for (n = lj_lib_checkint(L, 2); i > n; i--) { - /* The set may invalidate the get pointer, so need to do it first! */ - TValue *dst = lj_tab_setint(L, t, i); - cTValue *src = lj_tab_getint(t, i-1); - if (src) { - copyTV(L, dst, src); - } else { - setnilV(dst); - } - } - i = n; - } - { - TValue *dst = lj_tab_setint(L, t, i); - copyTV(L, dst, L->top-1); /* Set new value. */ - lj_gc_barriert(L, t, dst); - } - return 0; -} - -LJLIB_CF(table_remove) LJLIB_REC(.) -{ - GCtab *t = lj_lib_checktab(L, 1); - int32_t e = (int32_t)lj_tab_len(t); - int32_t pos = lj_lib_optint(L, 2, e); - if (!(1 <= pos && pos <= e)) /* Nothing to remove? */ - return 0; - lua_rawgeti(L, 1, pos); /* Get previous value. */ - /* NOBARRIER: This just moves existing elements around. */ - for (; pos < e; pos++) { - cTValue *src = lj_tab_getint(t, pos+1); - TValue *dst = lj_tab_setint(L, t, pos); - if (src) { - copyTV(L, dst, src); - } else { - setnilV(dst); - } - } - setnilV(lj_tab_setint(L, t, e)); /* Remove (last) value. */ - return 1; /* Return previous value. */ -} - -LJLIB_CF(table_concat) -{ - luaL_Buffer b; - GCtab *t = lj_lib_checktab(L, 1); - GCstr *sep = lj_lib_optstr(L, 2); - MSize seplen = sep ? sep->len : 0; - int32_t i = lj_lib_optint(L, 3, 1); - int32_t e = (L->base+3 < L->top && !tvisnil(L->base+3)) ? - lj_lib_checkint(L, 4) : (int32_t)lj_tab_len(t); - luaL_buffinit(L, &b); - if (i <= e) { - for (;;) { - cTValue *o; - lua_rawgeti(L, 1, i); - o = L->top-1; - if (!(tvisstr(o) || tvisnumber(o))) - lj_err_callerv(L, LJ_ERR_TABCAT, lj_typename(o), i); - luaL_addvalue(&b); - if (i++ == e) break; - if (seplen) - luaL_addlstring(&b, strdata(sep), seplen); - } - } - luaL_pushresult(&b); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void set2(lua_State *L, int i, int j) -{ - lua_rawseti(L, 1, i); - lua_rawseti(L, 1, j); -} - -static int sort_comp(lua_State *L, int a, int b) -{ - if (!lua_isnil(L, 2)) { /* function? */ - int res; - lua_pushvalue(L, 2); - lua_pushvalue(L, a-1); /* -1 to compensate function */ - lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ - lua_call(L, 2, 1); - res = lua_toboolean(L, -1); - lua_pop(L, 1); - return res; - } else { /* a < b? */ - return lua_lessthan(L, a, b); - } -} - -static void auxsort(lua_State *L, int l, int u) -{ - while (l < u) { /* for tail recursion */ - int i, j; - /* sort elements a[l], a[(l+u)/2] and a[u] */ - lua_rawgeti(L, 1, l); - lua_rawgeti(L, 1, u); - if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ - set2(L, l, u); /* swap a[l] - a[u] */ - else - lua_pop(L, 2); - if (u-l == 1) break; /* only 2 elements */ - i = (l+u)/2; - lua_rawgeti(L, 1, i); - lua_rawgeti(L, 1, l); - if (sort_comp(L, -2, -1)) { /* a[i]= P */ - while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i>=u) lj_err_caller(L, LJ_ERR_TABSORT); - lua_pop(L, 1); /* remove a[i] */ - } - /* repeat --j until a[j] <= P */ - while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j<=l) lj_err_caller(L, LJ_ERR_TABSORT); - lua_pop(L, 1); /* remove a[j] */ - } - if (jbase+1)) - lj_lib_checkfunc(L, 2); - auxsort(L, 1, n); - return 0; -} - -#if LJ_52 -LJLIB_PUSH("n") -LJLIB_CF(table_pack) -{ - TValue *array, *base = L->base; - MSize i, n = (uint32_t)(L->top - base); - GCtab *t = lj_tab_new(L, n ? n+1 : 0, 1); - /* NOBARRIER: The table is new (marked white). */ - setintV(lj_tab_setstr(L, t, strV(lj_lib_upvalue(L, 1))), (int32_t)n); - for (array = tvref(t->array) + 1, i = 0; i < n; i++) - copyTV(L, &array[i], &base[i]); - settabV(L, base, t); - L->top = base+1; - lj_gc_check(L); - return 1; -} -#endif - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_table(lua_State *L) -{ - LJ_LIB_REG(L, LUA_TABLIBNAME, table); -#if LJ_52 - lua_getglobal(L, "unpack"); - lua_setfield(L, -2, "unpack"); -#endif - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj.supp b/third-party/LuaJIT-2.0.2/src/lj.supp deleted file mode 100644 index 411f261700..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj.supp +++ /dev/null @@ -1,26 +0,0 @@ -# Valgrind suppression file for LuaJIT 2.0. -{ - Optimized string compare - Memcheck:Addr4 - fun:lj_str_cmp -} -{ - Optimized string compare - Memcheck:Addr1 - fun:lj_str_cmp -} -{ - Optimized string compare - Memcheck:Addr4 - fun:lj_str_new -} -{ - Optimized string compare - Memcheck:Addr1 - fun:lj_str_new -} -{ - Optimized string compare - Memcheck:Cond - fun:lj_str_new -} diff --git a/third-party/LuaJIT-2.0.2/src/lj_alloc.c b/third-party/LuaJIT-2.0.2/src/lj_alloc.c deleted file mode 100644 index 8f285d174b..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_alloc.c +++ /dev/null @@ -1,1388 +0,0 @@ -/* -** Bundled memory allocator. -** -** Beware: this is a HEAVILY CUSTOMIZED version of dlmalloc. -** The original bears the following remark: -** -** This is a version (aka dlmalloc) of malloc/free/realloc written by -** Doug Lea and released to the public domain, as explained at -** http://creativecommons.org/licenses/publicdomain. -** -** * Version pre-2.8.4 Wed Mar 29 19:46:29 2006 (dl at gee) -** -** No additional copyright is claimed over the customizations. -** Please do NOT bother the original author about this version here! -** -** If you want to use dlmalloc in another project, you should get -** the original from: ftp://gee.cs.oswego.edu/pub/misc/ -** For thread-safe derivatives, take a look at: -** - ptmalloc: http://www.malloc.de/ -** - nedmalloc: http://www.nedprod.com/programs/portable/nedmalloc/ -*/ - -#define lj_alloc_c -#define LUA_CORE - -/* To get the mremap prototype. Must be defined before any system includes. */ -#if defined(__linux__) && !defined(_GNU_SOURCE) -#define _GNU_SOURCE -#endif - -#include "lj_def.h" -#include "lj_arch.h" -#include "lj_alloc.h" - -#ifndef LUAJIT_USE_SYSMALLOC - -#define MAX_SIZE_T (~(size_t)0) -#define MALLOC_ALIGNMENT ((size_t)8U) - -#define DEFAULT_GRANULARITY ((size_t)128U * (size_t)1024U) -#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U) -#define DEFAULT_MMAP_THRESHOLD ((size_t)128U * (size_t)1024U) -#define MAX_RELEASE_CHECK_RATE 255 - -/* ------------------- size_t and alignment properties -------------------- */ - -/* The byte and bit size of a size_t */ -#define SIZE_T_SIZE (sizeof(size_t)) -#define SIZE_T_BITSIZE (sizeof(size_t) << 3) - -/* Some constants coerced to size_t */ -/* Annoying but necessary to avoid errors on some platforms */ -#define SIZE_T_ZERO ((size_t)0) -#define SIZE_T_ONE ((size_t)1) -#define SIZE_T_TWO ((size_t)2) -#define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) -#define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) -#define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) - -/* The bit mask value corresponding to MALLOC_ALIGNMENT */ -#define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) - -/* the number of bytes to offset an address to align it */ -#define align_offset(A)\ - ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ - ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) - -/* -------------------------- MMAP support ------------------------------- */ - -#define MFAIL ((void *)(MAX_SIZE_T)) -#define CMFAIL ((char *)(MFAIL)) /* defined for convenience */ - -#define IS_DIRECT_BIT (SIZE_T_ONE) - -#if LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#if LJ_64 - -/* Undocumented, but hey, that's what we all love so much about Windows. */ -typedef long (*PNTAVM)(HANDLE handle, void **addr, ULONG zbits, - size_t *size, ULONG alloctype, ULONG prot); -static PNTAVM ntavm; - -/* Number of top bits of the lower 32 bits of an address that must be zero. -** Apparently 0 gives us full 64 bit addresses and 1 gives us the lower 2GB. -*/ -#define NTAVM_ZEROBITS 1 - -static void INIT_MMAP(void) -{ - ntavm = (PNTAVM)GetProcAddress(GetModuleHandleA("ntdll.dll"), - "NtAllocateVirtualMemory"); -} - -/* Win64 32 bit MMAP via NtAllocateVirtualMemory. */ -static LJ_AINLINE void *CALL_MMAP(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = NULL; - long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size, - MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); - SetLastError(olderr); - return st == 0 ? ptr : MFAIL; -} - -/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ -static LJ_AINLINE void *DIRECT_MMAP(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = NULL; - long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size, - MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE); - SetLastError(olderr); - return st == 0 ? ptr : MFAIL; -} - -#else - -#define INIT_MMAP() ((void)0) - -/* Win32 MMAP via VirtualAlloc */ -static LJ_AINLINE void *CALL_MMAP(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); - SetLastError(olderr); - return ptr ? ptr : MFAIL; -} - -/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ -static LJ_AINLINE void *DIRECT_MMAP(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, - PAGE_READWRITE); - SetLastError(olderr); - return ptr ? ptr : MFAIL; -} - -#endif - -/* This function supports releasing coalesed segments */ -static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size) -{ - DWORD olderr = GetLastError(); - MEMORY_BASIC_INFORMATION minfo; - char *cptr = (char *)ptr; - while (size) { - if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0) - return -1; - if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr || - minfo.State != MEM_COMMIT || minfo.RegionSize > size) - return -1; - if (VirtualFree(cptr, 0, MEM_RELEASE) == 0) - return -1; - cptr += minfo.RegionSize; - size -= minfo.RegionSize; - } - SetLastError(olderr); - return 0; -} - -#else - -#include -#include - -#define MMAP_PROT (PROT_READ|PROT_WRITE) -#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) -#define MAP_ANONYMOUS MAP_ANON -#endif -#define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS) - -#if LJ_64 -/* 64 bit mode needs special support for allocating memory in the lower 2GB. */ - -#if LJ_TARGET_LINUX - -/* Actually this only gives us max. 1GB in current Linux kernels. */ -static LJ_AINLINE void *CALL_MMAP(size_t size) -{ - int olderr = errno; - void *ptr = mmap(NULL, size, MMAP_PROT, MAP_32BIT|MMAP_FLAGS, -1, 0); - errno = olderr; - return ptr; -} - -#elif LJ_TARGET_OSX || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__sun__) - -/* OSX and FreeBSD mmap() use a naive first-fit linear search. -** That's perfect for us. Except that -pagezero_size must be set for OSX, -** otherwise the lower 4GB are blocked. And the 32GB RLIMIT_DATA needs -** to be reduced to 250MB on FreeBSD. -*/ -#if LJ_TARGET_OSX -#define MMAP_REGION_START ((uintptr_t)0x10000) -#else -#define MMAP_REGION_START ((uintptr_t)0x10000000) -#endif -#define MMAP_REGION_END ((uintptr_t)0x80000000) - -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -#include -#endif - -static LJ_AINLINE void *CALL_MMAP(size_t size) -{ - int olderr = errno; - /* Hint for next allocation. Doesn't need to be thread-safe. */ - static uintptr_t alloc_hint = MMAP_REGION_START; - int retry = 0; -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - static int rlimit_modified = 0; - if (LJ_UNLIKELY(rlimit_modified == 0)) { - struct rlimit rlim; - rlim.rlim_cur = rlim.rlim_max = MMAP_REGION_START; - setrlimit(RLIMIT_DATA, &rlim); /* Ignore result. May fail below. */ - rlimit_modified = 1; - } -#endif - for (;;) { - void *p = mmap((void *)alloc_hint, size, MMAP_PROT, MMAP_FLAGS, -1, 0); - if ((uintptr_t)p >= MMAP_REGION_START && - (uintptr_t)p + size < MMAP_REGION_END) { - alloc_hint = (uintptr_t)p + size; - errno = olderr; - return p; - } - if (p != CMFAIL) munmap(p, size); -#ifdef __sun__ - alloc_hint += 0x1000000; /* Need near-exhaustive linear scan. */ - if (alloc_hint + size < MMAP_REGION_END) continue; -#endif - if (retry) break; - retry = 1; - alloc_hint = MMAP_REGION_START; - } - errno = olderr; - return CMFAIL; -} - -#else - -#error "NYI: need an equivalent of MAP_32BIT for this 64 bit OS" - -#endif - -#else - -/* 32 bit mode is easy. */ -static LJ_AINLINE void *CALL_MMAP(size_t size) -{ - int olderr = errno; - void *ptr = mmap(NULL, size, MMAP_PROT, MMAP_FLAGS, -1, 0); - errno = olderr; - return ptr; -} - -#endif - -#define INIT_MMAP() ((void)0) -#define DIRECT_MMAP(s) CALL_MMAP(s) - -static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size) -{ - int olderr = errno; - int ret = munmap(ptr, size); - errno = olderr; - return ret; -} - -#if LJ_TARGET_LINUX -/* Need to define _GNU_SOURCE to get the mremap prototype. */ -static LJ_AINLINE void *CALL_MREMAP_(void *ptr, size_t osz, size_t nsz, - int flags) -{ - int olderr = errno; - ptr = mremap(ptr, osz, nsz, flags); - errno = olderr; - return ptr; -} - -#define CALL_MREMAP(addr, osz, nsz, mv) CALL_MREMAP_((addr), (osz), (nsz), (mv)) -#define CALL_MREMAP_NOMOVE 0 -#define CALL_MREMAP_MAYMOVE 1 -#if LJ_64 -#define CALL_MREMAP_MV CALL_MREMAP_NOMOVE -#else -#define CALL_MREMAP_MV CALL_MREMAP_MAYMOVE -#endif -#endif - -#endif - -#ifndef CALL_MREMAP -#define CALL_MREMAP(addr, osz, nsz, mv) ((void)osz, MFAIL) -#endif - -/* ----------------------- Chunk representations ------------------------ */ - -struct malloc_chunk { - size_t prev_foot; /* Size of previous chunk (if free). */ - size_t head; /* Size and inuse bits. */ - struct malloc_chunk *fd; /* double links -- used only if free. */ - struct malloc_chunk *bk; -}; - -typedef struct malloc_chunk mchunk; -typedef struct malloc_chunk *mchunkptr; -typedef struct malloc_chunk *sbinptr; /* The type of bins of chunks */ -typedef size_t bindex_t; /* Described below */ -typedef unsigned int binmap_t; /* Described below */ -typedef unsigned int flag_t; /* The type of various bit flag sets */ - -/* ------------------- Chunks sizes and alignments ----------------------- */ - -#define MCHUNK_SIZE (sizeof(mchunk)) - -#define CHUNK_OVERHEAD (SIZE_T_SIZE) - -/* Direct chunks need a second word of overhead ... */ -#define DIRECT_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) -/* ... and additional padding for fake next-chunk at foot */ -#define DIRECT_FOOT_PAD (FOUR_SIZE_T_SIZES) - -/* The smallest size we can malloc is an aligned minimal chunk */ -#define MIN_CHUNK_SIZE\ - ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) - -/* conversion from malloc headers to user pointers, and back */ -#define chunk2mem(p) ((void *)((char *)(p) + TWO_SIZE_T_SIZES)) -#define mem2chunk(mem) ((mchunkptr)((char *)(mem) - TWO_SIZE_T_SIZES)) -/* chunk associated with aligned address A */ -#define align_as_chunk(A) (mchunkptr)((A) + align_offset(chunk2mem(A))) - -/* Bounds on request (not chunk) sizes. */ -#define MAX_REQUEST ((~MIN_CHUNK_SIZE+1) << 2) -#define MIN_REQUEST (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE) - -/* pad request bytes into a usable size */ -#define pad_request(req) \ - (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) - -/* pad request, checking for minimum (but not maximum) */ -#define request2size(req) \ - (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) - -/* ------------------ Operations on head and foot fields ----------------- */ - -#define PINUSE_BIT (SIZE_T_ONE) -#define CINUSE_BIT (SIZE_T_TWO) -#define INUSE_BITS (PINUSE_BIT|CINUSE_BIT) - -/* Head value for fenceposts */ -#define FENCEPOST_HEAD (INUSE_BITS|SIZE_T_SIZE) - -/* extraction of fields from head words */ -#define cinuse(p) ((p)->head & CINUSE_BIT) -#define pinuse(p) ((p)->head & PINUSE_BIT) -#define chunksize(p) ((p)->head & ~(INUSE_BITS)) - -#define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT) -#define clear_cinuse(p) ((p)->head &= ~CINUSE_BIT) - -/* Treat space at ptr +/- offset as a chunk */ -#define chunk_plus_offset(p, s) ((mchunkptr)(((char *)(p)) + (s))) -#define chunk_minus_offset(p, s) ((mchunkptr)(((char *)(p)) - (s))) - -/* Ptr to next or previous physical malloc_chunk. */ -#define next_chunk(p) ((mchunkptr)(((char *)(p)) + ((p)->head & ~INUSE_BITS))) -#define prev_chunk(p) ((mchunkptr)(((char *)(p)) - ((p)->prev_foot) )) - -/* extract next chunk's pinuse bit */ -#define next_pinuse(p) ((next_chunk(p)->head) & PINUSE_BIT) - -/* Get/set size at footer */ -#define get_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot) -#define set_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot = (s)) - -/* Set size, pinuse bit, and foot */ -#define set_size_and_pinuse_of_free_chunk(p, s)\ - ((p)->head = (s|PINUSE_BIT), set_foot(p, s)) - -/* Set size, pinuse bit, foot, and clear next pinuse */ -#define set_free_with_pinuse(p, s, n)\ - (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) - -#define is_direct(p)\ - (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_DIRECT_BIT)) - -/* Get the internal overhead associated with chunk p */ -#define overhead_for(p)\ - (is_direct(p)? DIRECT_CHUNK_OVERHEAD : CHUNK_OVERHEAD) - -/* ---------------------- Overlaid data structures ----------------------- */ - -struct malloc_tree_chunk { - /* The first four fields must be compatible with malloc_chunk */ - size_t prev_foot; - size_t head; - struct malloc_tree_chunk *fd; - struct malloc_tree_chunk *bk; - - struct malloc_tree_chunk *child[2]; - struct malloc_tree_chunk *parent; - bindex_t index; -}; - -typedef struct malloc_tree_chunk tchunk; -typedef struct malloc_tree_chunk *tchunkptr; -typedef struct malloc_tree_chunk *tbinptr; /* The type of bins of trees */ - -/* A little helper macro for trees */ -#define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1]) - -/* ----------------------------- Segments -------------------------------- */ - -struct malloc_segment { - char *base; /* base address */ - size_t size; /* allocated size */ - struct malloc_segment *next; /* ptr to next segment */ -}; - -typedef struct malloc_segment msegment; -typedef struct malloc_segment *msegmentptr; - -/* ---------------------------- malloc_state ----------------------------- */ - -/* Bin types, widths and sizes */ -#define NSMALLBINS (32U) -#define NTREEBINS (32U) -#define SMALLBIN_SHIFT (3U) -#define SMALLBIN_WIDTH (SIZE_T_ONE << SMALLBIN_SHIFT) -#define TREEBIN_SHIFT (8U) -#define MIN_LARGE_SIZE (SIZE_T_ONE << TREEBIN_SHIFT) -#define MAX_SMALL_SIZE (MIN_LARGE_SIZE - SIZE_T_ONE) -#define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD) - -struct malloc_state { - binmap_t smallmap; - binmap_t treemap; - size_t dvsize; - size_t topsize; - mchunkptr dv; - mchunkptr top; - size_t trim_check; - size_t release_checks; - mchunkptr smallbins[(NSMALLBINS+1)*2]; - tbinptr treebins[NTREEBINS]; - msegment seg; -}; - -typedef struct malloc_state *mstate; - -#define is_initialized(M) ((M)->top != 0) - -/* -------------------------- system alloc setup ------------------------- */ - -/* page-align a size */ -#define page_align(S)\ - (((S) + (LJ_PAGESIZE - SIZE_T_ONE)) & ~(LJ_PAGESIZE - SIZE_T_ONE)) - -/* granularity-align a size */ -#define granularity_align(S)\ - (((S) + (DEFAULT_GRANULARITY - SIZE_T_ONE))\ - & ~(DEFAULT_GRANULARITY - SIZE_T_ONE)) - -#if LJ_TARGET_WINDOWS -#define mmap_align(S) granularity_align(S) -#else -#define mmap_align(S) page_align(S) -#endif - -/* True if segment S holds address A */ -#define segment_holds(S, A)\ - ((char *)(A) >= S->base && (char *)(A) < S->base + S->size) - -/* Return segment holding given address */ -static msegmentptr segment_holding(mstate m, char *addr) -{ - msegmentptr sp = &m->seg; - for (;;) { - if (addr >= sp->base && addr < sp->base + sp->size) - return sp; - if ((sp = sp->next) == 0) - return 0; - } -} - -/* Return true if segment contains a segment link */ -static int has_segment_link(mstate m, msegmentptr ss) -{ - msegmentptr sp = &m->seg; - for (;;) { - if ((char *)sp >= ss->base && (char *)sp < ss->base + ss->size) - return 1; - if ((sp = sp->next) == 0) - return 0; - } -} - -/* - TOP_FOOT_SIZE is padding at the end of a segment, including space - that may be needed to place segment records and fenceposts when new - noncontiguous segments are added. -*/ -#define TOP_FOOT_SIZE\ - (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE) - -/* ---------------------------- Indexing Bins ---------------------------- */ - -#define is_small(s) (((s) >> SMALLBIN_SHIFT) < NSMALLBINS) -#define small_index(s) ((s) >> SMALLBIN_SHIFT) -#define small_index2size(i) ((i) << SMALLBIN_SHIFT) -#define MIN_SMALL_INDEX (small_index(MIN_CHUNK_SIZE)) - -/* addressing by index. See above about smallbin repositioning */ -#define smallbin_at(M, i) ((sbinptr)((char *)&((M)->smallbins[(i)<<1]))) -#define treebin_at(M,i) (&((M)->treebins[i])) - -/* assign tree index for size S to variable I */ -#define compute_tree_index(S, I)\ -{\ - unsigned int X = (unsigned int)(S >> TREEBIN_SHIFT);\ - if (X == 0) {\ - I = 0;\ - } else if (X > 0xFFFF) {\ - I = NTREEBINS-1;\ - } else {\ - unsigned int K = lj_fls(X);\ - I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ - }\ -} - -/* Bit representing maximum resolved size in a treebin at i */ -#define bit_for_tree_index(i) \ - (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2) - -/* Shift placing maximum resolved bit in a treebin at i as sign bit */ -#define leftshift_for_tree_index(i) \ - ((i == NTREEBINS-1)? 0 : \ - ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2))) - -/* The size of the smallest chunk held in bin with index i */ -#define minsize_for_tree_index(i) \ - ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \ - (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1))) - -/* ------------------------ Operations on bin maps ----------------------- */ - -/* bit corresponding to given index */ -#define idx2bit(i) ((binmap_t)(1) << (i)) - -/* Mark/Clear bits with given index */ -#define mark_smallmap(M,i) ((M)->smallmap |= idx2bit(i)) -#define clear_smallmap(M,i) ((M)->smallmap &= ~idx2bit(i)) -#define smallmap_is_marked(M,i) ((M)->smallmap & idx2bit(i)) - -#define mark_treemap(M,i) ((M)->treemap |= idx2bit(i)) -#define clear_treemap(M,i) ((M)->treemap &= ~idx2bit(i)) -#define treemap_is_marked(M,i) ((M)->treemap & idx2bit(i)) - -/* mask with all bits to left of least bit of x on */ -#define left_bits(x) ((x<<1) | (~(x<<1)+1)) - -/* Set cinuse bit and pinuse bit of next chunk */ -#define set_inuse(M,p,s)\ - ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ - ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT) - -/* Set cinuse and pinuse of this chunk and pinuse of next chunk */ -#define set_inuse_and_pinuse(M,p,s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ - ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT) - -/* Set size, cinuse and pinuse bit of this chunk */ -#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT)) - -/* ----------------------- Operations on smallbins ----------------------- */ - -/* Link a free chunk into a smallbin */ -#define insert_small_chunk(M, P, S) {\ - bindex_t I = small_index(S);\ - mchunkptr B = smallbin_at(M, I);\ - mchunkptr F = B;\ - if (!smallmap_is_marked(M, I))\ - mark_smallmap(M, I);\ - else\ - F = B->fd;\ - B->fd = P;\ - F->bk = P;\ - P->fd = F;\ - P->bk = B;\ -} - -/* Unlink a chunk from a smallbin */ -#define unlink_small_chunk(M, P, S) {\ - mchunkptr F = P->fd;\ - mchunkptr B = P->bk;\ - bindex_t I = small_index(S);\ - if (F == B) {\ - clear_smallmap(M, I);\ - } else {\ - F->bk = B;\ - B->fd = F;\ - }\ -} - -/* Unlink the first chunk from a smallbin */ -#define unlink_first_small_chunk(M, B, P, I) {\ - mchunkptr F = P->fd;\ - if (B == F) {\ - clear_smallmap(M, I);\ - } else {\ - B->fd = F;\ - F->bk = B;\ - }\ -} - -/* Replace dv node, binning the old one */ -/* Used only when dvsize known to be small */ -#define replace_dv(M, P, S) {\ - size_t DVS = M->dvsize;\ - if (DVS != 0) {\ - mchunkptr DV = M->dv;\ - insert_small_chunk(M, DV, DVS);\ - }\ - M->dvsize = S;\ - M->dv = P;\ -} - -/* ------------------------- Operations on trees ------------------------- */ - -/* Insert chunk into tree */ -#define insert_large_chunk(M, X, S) {\ - tbinptr *H;\ - bindex_t I;\ - compute_tree_index(S, I);\ - H = treebin_at(M, I);\ - X->index = I;\ - X->child[0] = X->child[1] = 0;\ - if (!treemap_is_marked(M, I)) {\ - mark_treemap(M, I);\ - *H = X;\ - X->parent = (tchunkptr)H;\ - X->fd = X->bk = X;\ - } else {\ - tchunkptr T = *H;\ - size_t K = S << leftshift_for_tree_index(I);\ - for (;;) {\ - if (chunksize(T) != S) {\ - tchunkptr *C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\ - K <<= 1;\ - if (*C != 0) {\ - T = *C;\ - } else {\ - *C = X;\ - X->parent = T;\ - X->fd = X->bk = X;\ - break;\ - }\ - } else {\ - tchunkptr F = T->fd;\ - T->fd = F->bk = X;\ - X->fd = F;\ - X->bk = T;\ - X->parent = 0;\ - break;\ - }\ - }\ - }\ -} - -#define unlink_large_chunk(M, X) {\ - tchunkptr XP = X->parent;\ - tchunkptr R;\ - if (X->bk != X) {\ - tchunkptr F = X->fd;\ - R = X->bk;\ - F->bk = R;\ - R->fd = F;\ - } else {\ - tchunkptr *RP;\ - if (((R = *(RP = &(X->child[1]))) != 0) ||\ - ((R = *(RP = &(X->child[0]))) != 0)) {\ - tchunkptr *CP;\ - while ((*(CP = &(R->child[1])) != 0) ||\ - (*(CP = &(R->child[0])) != 0)) {\ - R = *(RP = CP);\ - }\ - *RP = 0;\ - }\ - }\ - if (XP != 0) {\ - tbinptr *H = treebin_at(M, X->index);\ - if (X == *H) {\ - if ((*H = R) == 0) \ - clear_treemap(M, X->index);\ - } else {\ - if (XP->child[0] == X) \ - XP->child[0] = R;\ - else \ - XP->child[1] = R;\ - }\ - if (R != 0) {\ - tchunkptr C0, C1;\ - R->parent = XP;\ - if ((C0 = X->child[0]) != 0) {\ - R->child[0] = C0;\ - C0->parent = R;\ - }\ - if ((C1 = X->child[1]) != 0) {\ - R->child[1] = C1;\ - C1->parent = R;\ - }\ - }\ - }\ -} - -/* Relays to large vs small bin operations */ - -#define insert_chunk(M, P, S)\ - if (is_small(S)) { insert_small_chunk(M, P, S)\ - } else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } - -#define unlink_chunk(M, P, S)\ - if (is_small(S)) { unlink_small_chunk(M, P, S)\ - } else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); } - -/* ----------------------- Direct-mmapping chunks ----------------------- */ - -static void *direct_alloc(size_t nb) -{ - size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - if (LJ_LIKELY(mmsize > nb)) { /* Check for wrap around 0 */ - char *mm = (char *)(DIRECT_MMAP(mmsize)); - if (mm != CMFAIL) { - size_t offset = align_offset(chunk2mem(mm)); - size_t psize = mmsize - offset - DIRECT_FOOT_PAD; - mchunkptr p = (mchunkptr)(mm + offset); - p->prev_foot = offset | IS_DIRECT_BIT; - p->head = psize|CINUSE_BIT; - chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; - chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; - return chunk2mem(p); - } - } - return NULL; -} - -static mchunkptr direct_resize(mchunkptr oldp, size_t nb) -{ - size_t oldsize = chunksize(oldp); - if (is_small(nb)) /* Can't shrink direct regions below small size */ - return NULL; - /* Keep old chunk if big enough but not too big */ - if (oldsize >= nb + SIZE_T_SIZE && - (oldsize - nb) <= (DEFAULT_GRANULARITY >> 1)) { - return oldp; - } else { - size_t offset = oldp->prev_foot & ~IS_DIRECT_BIT; - size_t oldmmsize = oldsize + offset + DIRECT_FOOT_PAD; - size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - char *cp = (char *)CALL_MREMAP((char *)oldp - offset, - oldmmsize, newmmsize, CALL_MREMAP_MV); - if (cp != CMFAIL) { - mchunkptr newp = (mchunkptr)(cp + offset); - size_t psize = newmmsize - offset - DIRECT_FOOT_PAD; - newp->head = psize|CINUSE_BIT; - chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; - chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; - return newp; - } - } - return NULL; -} - -/* -------------------------- mspace management -------------------------- */ - -/* Initialize top chunk and its size */ -static void init_top(mstate m, mchunkptr p, size_t psize) -{ - /* Ensure alignment */ - size_t offset = align_offset(chunk2mem(p)); - p = (mchunkptr)((char *)p + offset); - psize -= offset; - - m->top = p; - m->topsize = psize; - p->head = psize | PINUSE_BIT; - /* set size of fake trailing chunk holding overhead space only once */ - chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE; - m->trim_check = DEFAULT_TRIM_THRESHOLD; /* reset on each update */ -} - -/* Initialize bins for a new mstate that is otherwise zeroed out */ -static void init_bins(mstate m) -{ - /* Establish circular links for smallbins */ - bindex_t i; - for (i = 0; i < NSMALLBINS; i++) { - sbinptr bin = smallbin_at(m,i); - bin->fd = bin->bk = bin; - } -} - -/* Allocate chunk and prepend remainder with chunk in successor base. */ -static void *prepend_alloc(mstate m, char *newbase, char *oldbase, size_t nb) -{ - mchunkptr p = align_as_chunk(newbase); - mchunkptr oldfirst = align_as_chunk(oldbase); - size_t psize = (size_t)((char *)oldfirst - (char *)p); - mchunkptr q = chunk_plus_offset(p, nb); - size_t qsize = psize - nb; - set_size_and_pinuse_of_inuse_chunk(m, p, nb); - - /* consolidate remainder with first chunk of old base */ - if (oldfirst == m->top) { - size_t tsize = m->topsize += qsize; - m->top = q; - q->head = tsize | PINUSE_BIT; - } else if (oldfirst == m->dv) { - size_t dsize = m->dvsize += qsize; - m->dv = q; - set_size_and_pinuse_of_free_chunk(q, dsize); - } else { - if (!cinuse(oldfirst)) { - size_t nsize = chunksize(oldfirst); - unlink_chunk(m, oldfirst, nsize); - oldfirst = chunk_plus_offset(oldfirst, nsize); - qsize += nsize; - } - set_free_with_pinuse(q, qsize, oldfirst); - insert_chunk(m, q, qsize); - } - - return chunk2mem(p); -} - -/* Add a segment to hold a new noncontiguous region */ -static void add_segment(mstate m, char *tbase, size_t tsize) -{ - /* Determine locations and sizes of segment, fenceposts, old top */ - char *old_top = (char *)m->top; - msegmentptr oldsp = segment_holding(m, old_top); - char *old_end = oldsp->base + oldsp->size; - size_t ssize = pad_request(sizeof(struct malloc_segment)); - char *rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - size_t offset = align_offset(chunk2mem(rawsp)); - char *asp = rawsp + offset; - char *csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp; - mchunkptr sp = (mchunkptr)csp; - msegmentptr ss = (msegmentptr)(chunk2mem(sp)); - mchunkptr tnext = chunk_plus_offset(sp, ssize); - mchunkptr p = tnext; - - /* reset top to new space */ - init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); - - /* Set up segment record */ - set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); - *ss = m->seg; /* Push current record */ - m->seg.base = tbase; - m->seg.size = tsize; - m->seg.next = ss; - - /* Insert trailing fenceposts */ - for (;;) { - mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); - p->head = FENCEPOST_HEAD; - if ((char *)(&(nextp->head)) < old_end) - p = nextp; - else - break; - } - - /* Insert the rest of old top into a bin as an ordinary free chunk */ - if (csp != old_top) { - mchunkptr q = (mchunkptr)old_top; - size_t psize = (size_t)(csp - old_top); - mchunkptr tn = chunk_plus_offset(q, psize); - set_free_with_pinuse(q, psize, tn); - insert_chunk(m, q, psize); - } -} - -/* -------------------------- System allocation -------------------------- */ - -static void *alloc_sys(mstate m, size_t nb) -{ - char *tbase = CMFAIL; - size_t tsize = 0; - - /* Directly map large chunks */ - if (LJ_UNLIKELY(nb >= DEFAULT_MMAP_THRESHOLD)) { - void *mem = direct_alloc(nb); - if (mem != 0) - return mem; - } - - { - size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE; - size_t rsize = granularity_align(req); - if (LJ_LIKELY(rsize > nb)) { /* Fail if wraps around zero */ - char *mp = (char *)(CALL_MMAP(rsize)); - if (mp != CMFAIL) { - tbase = mp; - tsize = rsize; - } - } - } - - if (tbase != CMFAIL) { - msegmentptr sp = &m->seg; - /* Try to merge with an existing segment */ - while (sp != 0 && tbase != sp->base + sp->size) - sp = sp->next; - if (sp != 0 && segment_holds(sp, m->top)) { /* append */ - sp->size += tsize; - init_top(m, m->top, m->topsize + tsize); - } else { - sp = &m->seg; - while (sp != 0 && sp->base != tbase + tsize) - sp = sp->next; - if (sp != 0) { - char *oldbase = sp->base; - sp->base = tbase; - sp->size += tsize; - return prepend_alloc(m, tbase, oldbase, nb); - } else { - add_segment(m, tbase, tsize); - } - } - - if (nb < m->topsize) { /* Allocate from new or extended top space */ - size_t rsize = m->topsize -= nb; - mchunkptr p = m->top; - mchunkptr r = m->top = chunk_plus_offset(p, nb); - r->head = rsize | PINUSE_BIT; - set_size_and_pinuse_of_inuse_chunk(m, p, nb); - return chunk2mem(p); - } - } - - return NULL; -} - -/* ----------------------- system deallocation -------------------------- */ - -/* Unmap and unlink any mmapped segments that don't contain used chunks */ -static size_t release_unused_segments(mstate m) -{ - size_t released = 0; - size_t nsegs = 0; - msegmentptr pred = &m->seg; - msegmentptr sp = pred->next; - while (sp != 0) { - char *base = sp->base; - size_t size = sp->size; - msegmentptr next = sp->next; - nsegs++; - { - mchunkptr p = align_as_chunk(base); - size_t psize = chunksize(p); - /* Can unmap if first chunk holds entire segment and not pinned */ - if (!cinuse(p) && (char *)p + psize >= base + size - TOP_FOOT_SIZE) { - tchunkptr tp = (tchunkptr)p; - if (p == m->dv) { - m->dv = 0; - m->dvsize = 0; - } else { - unlink_large_chunk(m, tp); - } - if (CALL_MUNMAP(base, size) == 0) { - released += size; - /* unlink obsoleted record */ - sp = pred; - sp->next = next; - } else { /* back out if cannot unmap */ - insert_large_chunk(m, tp, psize); - } - } - } - pred = sp; - sp = next; - } - /* Reset check counter */ - m->release_checks = nsegs > MAX_RELEASE_CHECK_RATE ? - nsegs : MAX_RELEASE_CHECK_RATE; - return released; -} - -static int alloc_trim(mstate m, size_t pad) -{ - size_t released = 0; - if (pad < MAX_REQUEST && is_initialized(m)) { - pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ - - if (m->topsize > pad) { - /* Shrink top space in granularity-size units, keeping at least one */ - size_t unit = DEFAULT_GRANULARITY; - size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - - SIZE_T_ONE) * unit; - msegmentptr sp = segment_holding(m, (char *)m->top); - - if (sp->size >= extra && - !has_segment_link(m, sp)) { /* can't shrink if pinned */ - size_t newsize = sp->size - extra; - /* Prefer mremap, fall back to munmap */ - if ((CALL_MREMAP(sp->base, sp->size, newsize, CALL_MREMAP_NOMOVE) != MFAIL) || - (CALL_MUNMAP(sp->base + newsize, extra) == 0)) { - released = extra; - } - } - - if (released != 0) { - sp->size -= released; - init_top(m, m->top, m->topsize - released); - } - } - - /* Unmap any unused mmapped segments */ - released += release_unused_segments(m); - - /* On failure, disable autotrim to avoid repeated failed future calls */ - if (released == 0 && m->topsize > m->trim_check) - m->trim_check = MAX_SIZE_T; - } - - return (released != 0)? 1 : 0; -} - -/* ---------------------------- malloc support --------------------------- */ - -/* allocate a large request from the best fitting chunk in a treebin */ -static void *tmalloc_large(mstate m, size_t nb) -{ - tchunkptr v = 0; - size_t rsize = ~nb+1; /* Unsigned negation */ - tchunkptr t; - bindex_t idx; - compute_tree_index(nb, idx); - - if ((t = *treebin_at(m, idx)) != 0) { - /* Traverse tree for this bin looking for node with size == nb */ - size_t sizebits = nb << leftshift_for_tree_index(idx); - tchunkptr rst = 0; /* The deepest untaken right subtree */ - for (;;) { - tchunkptr rt; - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - v = t; - if ((rsize = trem) == 0) - break; - } - rt = t->child[1]; - t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; - if (rt != 0 && rt != t) - rst = rt; - if (t == 0) { - t = rst; /* set t to least subtree holding sizes > nb */ - break; - } - sizebits <<= 1; - } - } - - if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */ - binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap; - if (leftbits != 0) - t = *treebin_at(m, lj_ffs(leftbits)); - } - - while (t != 0) { /* find smallest of tree or subtree */ - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - rsize = trem; - v = t; - } - t = leftmost_child(t); - } - - /* If dv is a better fit, return NULL so malloc will use it */ - if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { - mchunkptr r = chunk_plus_offset(v, nb); - unlink_large_chunk(m, v); - if (rsize < MIN_CHUNK_SIZE) { - set_inuse_and_pinuse(m, v, (rsize + nb)); - } else { - set_size_and_pinuse_of_inuse_chunk(m, v, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - insert_chunk(m, r, rsize); - } - return chunk2mem(v); - } - return NULL; -} - -/* allocate a small request from the best fitting chunk in a treebin */ -static void *tmalloc_small(mstate m, size_t nb) -{ - tchunkptr t, v; - mchunkptr r; - size_t rsize; - bindex_t i = lj_ffs(m->treemap); - - v = t = *treebin_at(m, i); - rsize = chunksize(t) - nb; - - while ((t = leftmost_child(t)) != 0) { - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - rsize = trem; - v = t; - } - } - - r = chunk_plus_offset(v, nb); - unlink_large_chunk(m, v); - if (rsize < MIN_CHUNK_SIZE) { - set_inuse_and_pinuse(m, v, (rsize + nb)); - } else { - set_size_and_pinuse_of_inuse_chunk(m, v, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - replace_dv(m, r, rsize); - } - return chunk2mem(v); -} - -/* ----------------------------------------------------------------------- */ - -void *lj_alloc_create(void) -{ - size_t tsize = DEFAULT_GRANULARITY; - char *tbase; - INIT_MMAP(); - tbase = (char *)(CALL_MMAP(tsize)); - if (tbase != CMFAIL) { - size_t msize = pad_request(sizeof(struct malloc_state)); - mchunkptr mn; - mchunkptr msp = align_as_chunk(tbase); - mstate m = (mstate)(chunk2mem(msp)); - memset(m, 0, msize); - msp->head = (msize|PINUSE_BIT|CINUSE_BIT); - m->seg.base = tbase; - m->seg.size = tsize; - m->release_checks = MAX_RELEASE_CHECK_RATE; - init_bins(m); - mn = next_chunk(mem2chunk(m)); - init_top(m, mn, (size_t)((tbase + tsize) - (char *)mn) - TOP_FOOT_SIZE); - return m; - } - return NULL; -} - -void lj_alloc_destroy(void *msp) -{ - mstate ms = (mstate)msp; - msegmentptr sp = &ms->seg; - while (sp != 0) { - char *base = sp->base; - size_t size = sp->size; - sp = sp->next; - CALL_MUNMAP(base, size); - } -} - -static LJ_NOINLINE void *lj_alloc_malloc(void *msp, size_t nsize) -{ - mstate ms = (mstate)msp; - void *mem; - size_t nb; - if (nsize <= MAX_SMALL_REQUEST) { - bindex_t idx; - binmap_t smallbits; - nb = (nsize < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(nsize); - idx = small_index(nb); - smallbits = ms->smallmap >> idx; - - if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ - mchunkptr b, p; - idx += ~smallbits & 1; /* Uses next bin if idx empty */ - b = smallbin_at(ms, idx); - p = b->fd; - unlink_first_small_chunk(ms, b, p, idx); - set_inuse_and_pinuse(ms, p, small_index2size(idx)); - mem = chunk2mem(p); - return mem; - } else if (nb > ms->dvsize) { - if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ - mchunkptr b, p, r; - size_t rsize; - binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); - bindex_t i = lj_ffs(leftbits); - b = smallbin_at(ms, i); - p = b->fd; - unlink_first_small_chunk(ms, b, p, i); - rsize = small_index2size(i) - nb; - /* Fit here cannot be remainderless if 4byte sizes */ - if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) { - set_inuse_and_pinuse(ms, p, small_index2size(i)); - } else { - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - r = chunk_plus_offset(p, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - replace_dv(ms, r, rsize); - } - mem = chunk2mem(p); - return mem; - } else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { - return mem; - } - } - } else if (nsize >= MAX_REQUEST) { - nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ - } else { - nb = pad_request(nsize); - if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) { - return mem; - } - } - - if (nb <= ms->dvsize) { - size_t rsize = ms->dvsize - nb; - mchunkptr p = ms->dv; - if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ - mchunkptr r = ms->dv = chunk_plus_offset(p, nb); - ms->dvsize = rsize; - set_size_and_pinuse_of_free_chunk(r, rsize); - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - } else { /* exhaust dv */ - size_t dvs = ms->dvsize; - ms->dvsize = 0; - ms->dv = 0; - set_inuse_and_pinuse(ms, p, dvs); - } - mem = chunk2mem(p); - return mem; - } else if (nb < ms->topsize) { /* Split top */ - size_t rsize = ms->topsize -= nb; - mchunkptr p = ms->top; - mchunkptr r = ms->top = chunk_plus_offset(p, nb); - r->head = rsize | PINUSE_BIT; - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - mem = chunk2mem(p); - return mem; - } - return alloc_sys(ms, nb); -} - -static LJ_NOINLINE void *lj_alloc_free(void *msp, void *ptr) -{ - if (ptr != 0) { - mchunkptr p = mem2chunk(ptr); - mstate fm = (mstate)msp; - size_t psize = chunksize(p); - mchunkptr next = chunk_plus_offset(p, psize); - if (!pinuse(p)) { - size_t prevsize = p->prev_foot; - if ((prevsize & IS_DIRECT_BIT) != 0) { - prevsize &= ~IS_DIRECT_BIT; - psize += prevsize + DIRECT_FOOT_PAD; - CALL_MUNMAP((char *)p - prevsize, psize); - return NULL; - } else { - mchunkptr prev = chunk_minus_offset(p, prevsize); - psize += prevsize; - p = prev; - /* consolidate backward */ - if (p != fm->dv) { - unlink_chunk(fm, p, prevsize); - } else if ((next->head & INUSE_BITS) == INUSE_BITS) { - fm->dvsize = psize; - set_free_with_pinuse(p, psize, next); - return NULL; - } - } - } - if (!cinuse(next)) { /* consolidate forward */ - if (next == fm->top) { - size_t tsize = fm->topsize += psize; - fm->top = p; - p->head = tsize | PINUSE_BIT; - if (p == fm->dv) { - fm->dv = 0; - fm->dvsize = 0; - } - if (tsize > fm->trim_check) - alloc_trim(fm, 0); - return NULL; - } else if (next == fm->dv) { - size_t dsize = fm->dvsize += psize; - fm->dv = p; - set_size_and_pinuse_of_free_chunk(p, dsize); - return NULL; - } else { - size_t nsize = chunksize(next); - psize += nsize; - unlink_chunk(fm, next, nsize); - set_size_and_pinuse_of_free_chunk(p, psize); - if (p == fm->dv) { - fm->dvsize = psize; - return NULL; - } - } - } else { - set_free_with_pinuse(p, psize, next); - } - - if (is_small(psize)) { - insert_small_chunk(fm, p, psize); - } else { - tchunkptr tp = (tchunkptr)p; - insert_large_chunk(fm, tp, psize); - if (--fm->release_checks == 0) - release_unused_segments(fm); - } - } - return NULL; -} - -static LJ_NOINLINE void *lj_alloc_realloc(void *msp, void *ptr, size_t nsize) -{ - if (nsize >= MAX_REQUEST) { - return NULL; - } else { - mstate m = (mstate)msp; - mchunkptr oldp = mem2chunk(ptr); - size_t oldsize = chunksize(oldp); - mchunkptr next = chunk_plus_offset(oldp, oldsize); - mchunkptr newp = 0; - size_t nb = request2size(nsize); - - /* Try to either shrink or extend into top. Else malloc-copy-free */ - if (is_direct(oldp)) { - newp = direct_resize(oldp, nb); /* this may return NULL. */ - } else if (oldsize >= nb) { /* already big enough */ - size_t rsize = oldsize - nb; - newp = oldp; - if (rsize >= MIN_CHUNK_SIZE) { - mchunkptr rem = chunk_plus_offset(newp, nb); - set_inuse(m, newp, nb); - set_inuse(m, rem, rsize); - lj_alloc_free(m, chunk2mem(rem)); - } - } else if (next == m->top && oldsize + m->topsize > nb) { - /* Expand into top */ - size_t newsize = oldsize + m->topsize; - size_t newtopsize = newsize - nb; - mchunkptr newtop = chunk_plus_offset(oldp, nb); - set_inuse(m, oldp, nb); - newtop->head = newtopsize |PINUSE_BIT; - m->top = newtop; - m->topsize = newtopsize; - newp = oldp; - } - - if (newp != 0) { - return chunk2mem(newp); - } else { - void *newmem = lj_alloc_malloc(m, nsize); - if (newmem != 0) { - size_t oc = oldsize - overhead_for(oldp); - memcpy(newmem, ptr, oc < nsize ? oc : nsize); - lj_alloc_free(m, ptr); - } - return newmem; - } - } -} - -void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize) -{ - (void)osize; - if (nsize == 0) { - return lj_alloc_free(msp, ptr); - } else if (ptr == NULL) { - return lj_alloc_malloc(msp, nsize); - } else { - return lj_alloc_realloc(msp, ptr, nsize); - } -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_alloc.h b/third-party/LuaJIT-2.0.2/src/lj_alloc.h deleted file mode 100644 index f87a7cf342..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_alloc.h +++ /dev/null @@ -1,17 +0,0 @@ -/* -** Bundled memory allocator. -** Donated to the public domain. -*/ - -#ifndef _LJ_ALLOC_H -#define _LJ_ALLOC_H - -#include "lj_def.h" - -#ifndef LUAJIT_USE_SYSMALLOC -LJ_FUNC void *lj_alloc_create(void); -LJ_FUNC void lj_alloc_destroy(void *msp); -LJ_FUNC void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_api.c b/third-party/LuaJIT-2.0.2/src/lj_api.c deleted file mode 100644 index edb2d620da..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_api.c +++ /dev/null @@ -1,1200 +0,0 @@ -/* -** Public Lua/C API. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_api_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_udata.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_bc.h" -#include "lj_frame.h" -#include "lj_trace.h" -#include "lj_vm.h" -#include "lj_strscan.h" - -/* -- Common helper functions --------------------------------------------- */ - -#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) -#define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L)) - -static TValue *index2adr(lua_State *L, int idx) -{ - if (idx > 0) { - TValue *o = L->base + (idx - 1); - return o < L->top ? o : niltv(L); - } else if (idx > LUA_REGISTRYINDEX) { - api_check(L, idx != 0 && -idx <= L->top - L->base); - return L->top + idx; - } else if (idx == LUA_GLOBALSINDEX) { - TValue *o = &G(L)->tmptv; - settabV(L, o, tabref(L->env)); - return o; - } else if (idx == LUA_REGISTRYINDEX) { - return registry(L); - } else { - GCfunc *fn = curr_func(L); - api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn)); - if (idx == LUA_ENVIRONINDEX) { - TValue *o = &G(L)->tmptv; - settabV(L, o, tabref(fn->c.env)); - return o; - } else { - idx = LUA_GLOBALSINDEX - idx; - return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L); - } - } -} - -static TValue *stkindex2adr(lua_State *L, int idx) -{ - if (idx > 0) { - TValue *o = L->base + (idx - 1); - return o < L->top ? o : niltv(L); - } else { - api_check(L, idx != 0 && -idx <= L->top - L->base); - return L->top + idx; - } -} - -static GCtab *getcurrenv(lua_State *L) -{ - GCfunc *fn = curr_func(L); - return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env); -} - -/* -- Miscellaneous API functions ----------------------------------------- */ - -LUA_API int lua_status(lua_State *L) -{ - return L->status; -} - -LUA_API int lua_checkstack(lua_State *L, int size) -{ - if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) { - return 0; /* Stack overflow. */ - } else if (size > 0) { - lj_state_checkstack(L, (MSize)size); - } - return 1; -} - -LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg) -{ - if (!lua_checkstack(L, size)) - lj_err_callerv(L, LJ_ERR_STKOVM, msg); -} - -LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) -{ - TValue *f, *t; - if (from == to) return; - api_checknelems(from, n); - api_check(from, G(from) == G(to)); - lj_state_checkstack(to, (MSize)n); - f = from->top; - t = to->top = to->top + n; - while (--n >= 0) copyTV(to, --t, --f); - from->top = f; -} - -/* -- Stack manipulation -------------------------------------------------- */ - -LUA_API int lua_gettop(lua_State *L) -{ - return (int)(L->top - L->base); -} - -LUA_API void lua_settop(lua_State *L, int idx) -{ - if (idx >= 0) { - api_check(L, idx <= tvref(L->maxstack) - L->base); - if (L->base + idx > L->top) { - if (L->base + idx >= tvref(L->maxstack)) - lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base)); - do { setnilV(L->top++); } while (L->top < L->base + idx); - } else { - L->top = L->base + idx; - } - } else { - api_check(L, -(idx+1) <= (L->top - L->base)); - L->top += idx+1; /* Shrinks top (idx < 0). */ - } -} - -LUA_API void lua_remove(lua_State *L, int idx) -{ - TValue *p = stkindex2adr(L, idx); - api_checkvalidindex(L, p); - while (++p < L->top) copyTV(L, p-1, p); - L->top--; -} - -LUA_API void lua_insert(lua_State *L, int idx) -{ - TValue *q, *p = stkindex2adr(L, idx); - api_checkvalidindex(L, p); - for (q = L->top; q > p; q--) copyTV(L, q, q-1); - copyTV(L, p, L->top); -} - -LUA_API void lua_replace(lua_State *L, int idx) -{ - api_checknelems(L, 1); - if (idx == LUA_GLOBALSINDEX) { - api_check(L, tvistab(L->top-1)); - /* NOBARRIER: A thread (i.e. L) is never black. */ - setgcref(L->env, obj2gco(tabV(L->top-1))); - } else if (idx == LUA_ENVIRONINDEX) { - GCfunc *fn = curr_func(L); - if (fn->c.gct != ~LJ_TFUNC) - lj_err_msg(L, LJ_ERR_NOENV); - api_check(L, tvistab(L->top-1)); - setgcref(fn->c.env, obj2gco(tabV(L->top-1))); - lj_gc_barrier(L, fn, L->top-1); - } else { - TValue *o = index2adr(L, idx); - api_checkvalidindex(L, o); - copyTV(L, o, L->top-1); - if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */ - lj_gc_barrier(L, curr_func(L), L->top-1); - } - L->top--; -} - -LUA_API void lua_pushvalue(lua_State *L, int idx) -{ - copyTV(L, L->top, index2adr(L, idx)); - incr_top(L); -} - -/* -- Stack getters ------------------------------------------------------- */ - -LUA_API int lua_type(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - if (tvisnumber(o)) { - return LUA_TNUMBER; -#if LJ_64 - } else if (tvislightud(o)) { - return LUA_TLIGHTUSERDATA; -#endif - } else if (o == niltv(L)) { - return LUA_TNONE; - } else { /* Magic internal/external tag conversion. ORDER LJ_T */ - uint32_t t = ~itype(o); -#if LJ_64 - int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u); -#else - int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u); -#endif - lua_assert(tt != LUA_TNIL || tvisnil(o)); - return tt; - } -} - -LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt) -{ - if (lua_type(L, idx) != tt) - lj_err_argt(L, idx, tt); -} - -LUALIB_API void luaL_checkany(lua_State *L, int idx) -{ - if (index2adr(L, idx) == niltv(L)) - lj_err_arg(L, idx, LJ_ERR_NOVAL); -} - -LUA_API const char *lua_typename(lua_State *L, int t) -{ - UNUSED(L); - return lj_obj_typename[t+1]; -} - -LUA_API int lua_iscfunction(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return tvisfunc(o) && !isluafunc(funcV(o)); -} - -LUA_API int lua_isnumber(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp))); -} - -LUA_API int lua_isstring(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return (tvisstr(o) || tvisnumber(o)); -} - -LUA_API int lua_isuserdata(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return (tvisudata(o) || tvislightud(o)); -} - -LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2) -{ - cTValue *o1 = index2adr(L, idx1); - cTValue *o2 = index2adr(L, idx2); - return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2); -} - -LUA_API int lua_equal(lua_State *L, int idx1, int idx2) -{ - cTValue *o1 = index2adr(L, idx1); - cTValue *o2 = index2adr(L, idx2); - if (tvisint(o1) && tvisint(o2)) { - return intV(o1) == intV(o2); - } else if (tvisnumber(o1) && tvisnumber(o2)) { - return numberVnum(o1) == numberVnum(o2); - } else if (itype(o1) != itype(o2)) { - return 0; - } else if (tvispri(o1)) { - return o1 != niltv(L) && o2 != niltv(L); -#if LJ_64 - } else if (tvislightud(o1)) { - return o1->u64 == o2->u64; -#endif - } else if (gcrefeq(o1->gcr, o2->gcr)) { - return 1; - } else if (!tvistabud(o1)) { - return 0; - } else { - TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0); - if ((uintptr_t)base <= 1) { - return (int)(uintptr_t)base; - } else { - L->top = base+2; - lj_vm_call(L, base, 1+1); - L->top -= 2; - return tvistruecond(L->top+1); - } - } -} - -LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2) -{ - cTValue *o1 = index2adr(L, idx1); - cTValue *o2 = index2adr(L, idx2); - if (o1 == niltv(L) || o2 == niltv(L)) { - return 0; - } else if (tvisint(o1) && tvisint(o2)) { - return intV(o1) < intV(o2); - } else if (tvisnumber(o1) && tvisnumber(o2)) { - return numberVnum(o1) < numberVnum(o2); - } else { - TValue *base = lj_meta_comp(L, o1, o2, 0); - if ((uintptr_t)base <= 1) { - return (int)(uintptr_t)base; - } else { - L->top = base+2; - lj_vm_call(L, base, 1+1); - L->top -= 2; - return tvistruecond(L->top+1); - } - } -} - -LUA_API lua_Number lua_tonumber(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - if (LJ_LIKELY(tvisnumber(o))) - return numberVnum(o); - else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) - return numV(&tmp); - else - return 0; -} - -LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - if (LJ_LIKELY(tvisnumber(o))) - return numberVnum(o); - else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - return numV(&tmp); -} - -LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - if (LJ_LIKELY(tvisnumber(o))) - return numberVnum(o); - else if (tvisnil(o)) - return def; - else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - return numV(&tmp); -} - -LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - lua_Number n; - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else if (LJ_LIKELY(tvisnum(o))) { - n = numV(o); - } else { - if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) - return 0; - if (tvisint(&tmp)) - return (lua_Integer)intV(&tmp); - n = numV(&tmp); - } -#if LJ_64 - return (lua_Integer)n; -#else - return lj_num2int(n); -#endif -} - -LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - lua_Number n; - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else if (LJ_LIKELY(tvisnum(o))) { - n = numV(o); - } else { - if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - if (tvisint(&tmp)) - return (lua_Integer)intV(&tmp); - n = numV(&tmp); - } -#if LJ_64 - return (lua_Integer)n; -#else - return lj_num2int(n); -#endif -} - -LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - lua_Number n; - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else if (LJ_LIKELY(tvisnum(o))) { - n = numV(o); - } else if (tvisnil(o)) { - return def; - } else { - if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - if (tvisint(&tmp)) - return (lua_Integer)intV(&tmp); - n = numV(&tmp); - } -#if LJ_64 - return (lua_Integer)n; -#else - return lj_num2int(n); -#endif -} - -LUA_API int lua_toboolean(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return tvistruecond(o); -} - -LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len) -{ - TValue *o = index2adr(L, idx); - GCstr *s; - if (LJ_LIKELY(tvisstr(o))) { - s = strV(o); - } else if (tvisnumber(o)) { - lj_gc_check(L); - o = index2adr(L, idx); /* GC may move the stack. */ - s = lj_str_fromnumber(L, o); - setstrV(L, o, s); - } else { - if (len != NULL) *len = 0; - return NULL; - } - if (len != NULL) *len = s->len; - return strdata(s); -} - -LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len) -{ - TValue *o = index2adr(L, idx); - GCstr *s; - if (LJ_LIKELY(tvisstr(o))) { - s = strV(o); - } else if (tvisnumber(o)) { - lj_gc_check(L); - o = index2adr(L, idx); /* GC may move the stack. */ - s = lj_str_fromnumber(L, o); - setstrV(L, o, s); - } else { - lj_err_argt(L, idx, LUA_TSTRING); - } - if (len != NULL) *len = s->len; - return strdata(s); -} - -LUALIB_API const char *luaL_optlstring(lua_State *L, int idx, - const char *def, size_t *len) -{ - TValue *o = index2adr(L, idx); - GCstr *s; - if (LJ_LIKELY(tvisstr(o))) { - s = strV(o); - } else if (tvisnil(o)) { - if (len != NULL) *len = def ? strlen(def) : 0; - return def; - } else if (tvisnumber(o)) { - lj_gc_check(L); - o = index2adr(L, idx); /* GC may move the stack. */ - s = lj_str_fromnumber(L, o); - setstrV(L, o, s); - } else { - lj_err_argt(L, idx, LUA_TSTRING); - } - if (len != NULL) *len = s->len; - return strdata(s); -} - -LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def, - const char *const lst[]) -{ - ptrdiff_t i; - const char *s = lua_tolstring(L, idx, NULL); - if (s == NULL && (s = def) == NULL) - lj_err_argt(L, idx, LUA_TSTRING); - for (i = 0; lst[i]; i++) - if (strcmp(lst[i], s) == 0) - return (int)i; - lj_err_argv(L, idx, LJ_ERR_INVOPTM, s); -} - -LUA_API size_t lua_objlen(lua_State *L, int idx) -{ - TValue *o = index2adr(L, idx); - if (tvisstr(o)) { - return strV(o)->len; - } else if (tvistab(o)) { - return (size_t)lj_tab_len(tabV(o)); - } else if (tvisudata(o)) { - return udataV(o)->len; - } else if (tvisnumber(o)) { - GCstr *s = lj_str_fromnumber(L, o); - setstrV(L, o, s); - return s->len; - } else { - return 0; - } -} - -LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - if (tvisfunc(o)) { - BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns)); - if (op == BC_FUNCC || op == BC_FUNCCW) - return funcV(o)->c.f; - } - return NULL; -} - -LUA_API void *lua_touserdata(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - if (tvisudata(o)) - return uddata(udataV(o)); - else if (tvislightud(o)) - return lightudV(o); - else - return NULL; -} - -LUA_API lua_State *lua_tothread(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return (!tvisthread(o)) ? NULL : threadV(o); -} - -LUA_API const void *lua_topointer(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - if (tvisudata(o)) - return uddata(udataV(o)); - else if (tvislightud(o)) - return lightudV(o); - else if (tviscdata(o)) - return cdataptr(cdataV(o)); - else if (tvisgcv(o)) - return gcV(o); - else - return NULL; -} - -/* -- Stack setters (object creation) ------------------------------------- */ - -LUA_API void lua_pushnil(lua_State *L) -{ - setnilV(L->top); - incr_top(L); -} - -LUA_API void lua_pushnumber(lua_State *L, lua_Number n) -{ - setnumV(L->top, n); - if (LJ_UNLIKELY(tvisnan(L->top))) - setnanV(L->top); /* Canonicalize injected NaNs. */ - incr_top(L); -} - -LUA_API void lua_pushinteger(lua_State *L, lua_Integer n) -{ - setintptrV(L->top, n); - incr_top(L); -} - -LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len) -{ - GCstr *s; - lj_gc_check(L); - s = lj_str_new(L, str, len); - setstrV(L, L->top, s); - incr_top(L); -} - -LUA_API void lua_pushstring(lua_State *L, const char *str) -{ - if (str == NULL) { - setnilV(L->top); - } else { - GCstr *s; - lj_gc_check(L); - s = lj_str_newz(L, str); - setstrV(L, L->top, s); - } - incr_top(L); -} - -LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt, - va_list argp) -{ - lj_gc_check(L); - return lj_str_pushvf(L, fmt, argp); -} - -LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...) -{ - const char *ret; - va_list argp; - lj_gc_check(L); - va_start(argp, fmt); - ret = lj_str_pushvf(L, fmt, argp); - va_end(argp); - return ret; -} - -LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n) -{ - GCfunc *fn; - lj_gc_check(L); - api_checknelems(L, n); - fn = lj_func_newC(L, (MSize)n, getcurrenv(L)); - fn->c.f = f; - L->top -= n; - while (n--) - copyTV(L, &fn->c.upvalue[n], L->top+n); - setfuncV(L, L->top, fn); - lua_assert(iswhite(obj2gco(fn))); - incr_top(L); -} - -LUA_API void lua_pushboolean(lua_State *L, int b) -{ - setboolV(L->top, (b != 0)); - incr_top(L); -} - -LUA_API void lua_pushlightuserdata(lua_State *L, void *p) -{ - setlightudV(L->top, checklightudptr(L, p)); - incr_top(L); -} - -LUA_API void lua_createtable(lua_State *L, int narray, int nrec) -{ - GCtab *t; - lj_gc_check(L); - t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec)); - settabV(L, L->top, t); - incr_top(L); -} - -LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname) -{ - GCtab *regt = tabV(registry(L)); - TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname)); - if (tvisnil(tv)) { - GCtab *mt = lj_tab_new(L, 0, 1); - settabV(L, tv, mt); - settabV(L, L->top++, mt); - lj_gc_anybarriert(L, regt); - return 1; - } else { - copyTV(L, L->top++, tv); - return 0; - } -} - -LUA_API int lua_pushthread(lua_State *L) -{ - setthreadV(L, L->top, L); - incr_top(L); - return (mainthread(G(L)) == L); -} - -LUA_API lua_State *lua_newthread(lua_State *L) -{ - lua_State *L1; - lj_gc_check(L); - L1 = lj_state_new(L); - setthreadV(L, L->top, L1); - incr_top(L); - return L1; -} - -LUA_API void *lua_newuserdata(lua_State *L, size_t size) -{ - GCudata *ud; - lj_gc_check(L); - if (size > LJ_MAX_UDATA) - lj_err_msg(L, LJ_ERR_UDATAOV); - ud = lj_udata_new(L, (MSize)size, getcurrenv(L)); - setudataV(L, L->top, ud); - incr_top(L); - return uddata(ud); -} - -LUA_API void lua_concat(lua_State *L, int n) -{ - api_checknelems(L, n); - if (n >= 2) { - n--; - do { - TValue *top = lj_meta_cat(L, L->top-1, -n); - if (top == NULL) { - L->top -= n; - break; - } - n -= (int)(L->top - top); - L->top = top+2; - lj_vm_call(L, top, 1+1); - L->top--; - copyTV(L, L->top-1, L->top); - } while (--n > 0); - } else if (n == 0) { /* Push empty string. */ - setstrV(L, L->top, &G(L)->strempty); - incr_top(L); - } - /* else n == 1: nothing to do. */ -} - -/* -- Object getters ------------------------------------------------------ */ - -LUA_API void lua_gettable(lua_State *L, int idx) -{ - cTValue *v, *t = index2adr(L, idx); - api_checkvalidindex(L, t); - v = lj_meta_tget(L, t, L->top-1); - if (v == NULL) { - L->top += 2; - lj_vm_call(L, L->top-2, 1+1); - L->top -= 2; - v = L->top+1; - } - copyTV(L, L->top-1, v); -} - -LUA_API void lua_getfield(lua_State *L, int idx, const char *k) -{ - cTValue *v, *t = index2adr(L, idx); - TValue key; - api_checkvalidindex(L, t); - setstrV(L, &key, lj_str_newz(L, k)); - v = lj_meta_tget(L, t, &key); - if (v == NULL) { - L->top += 2; - lj_vm_call(L, L->top-2, 1+1); - L->top -= 2; - v = L->top+1; - } - copyTV(L, L->top, v); - incr_top(L); -} - -LUA_API void lua_rawget(lua_State *L, int idx) -{ - cTValue *t = index2adr(L, idx); - api_check(L, tvistab(t)); - copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1)); -} - -LUA_API void lua_rawgeti(lua_State *L, int idx, int n) -{ - cTValue *v, *t = index2adr(L, idx); - api_check(L, tvistab(t)); - v = lj_tab_getint(tabV(t), n); - if (v) { - copyTV(L, L->top, v); - } else { - setnilV(L->top); - } - incr_top(L); -} - -LUA_API int lua_getmetatable(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - GCtab *mt = NULL; - if (tvistab(o)) - mt = tabref(tabV(o)->metatable); - else if (tvisudata(o)) - mt = tabref(udataV(o)->metatable); - else - mt = tabref(basemt_obj(G(L), o)); - if (mt == NULL) - return 0; - settabV(L, L->top, mt); - incr_top(L); - return 1; -} - -LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field) -{ - if (lua_getmetatable(L, idx)) { - cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field)); - if (tv && !tvisnil(tv)) { - copyTV(L, L->top-1, tv); - return 1; - } - L->top--; - } - return 0; -} - -LUA_API void lua_getfenv(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - api_checkvalidindex(L, o); - if (tvisfunc(o)) { - settabV(L, L->top, tabref(funcV(o)->c.env)); - } else if (tvisudata(o)) { - settabV(L, L->top, tabref(udataV(o)->env)); - } else if (tvisthread(o)) { - settabV(L, L->top, tabref(threadV(o)->env)); - } else { - setnilV(L->top); - } - incr_top(L); -} - -LUA_API int lua_next(lua_State *L, int idx) -{ - cTValue *t = index2adr(L, idx); - int more; - api_check(L, tvistab(t)); - more = lj_tab_next(L, tabV(t), L->top-1); - if (more) { - incr_top(L); /* Return new key and value slot. */ - } else { /* End of traversal. */ - L->top--; /* Remove key slot. */ - } - return more; -} - -LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n) -{ - TValue *val; - const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val); - if (name) { - copyTV(L, L->top, val); - incr_top(L); - } - return name; -} - -LUA_API void *lua_upvalueid(lua_State *L, int idx, int n) -{ - GCfunc *fn = funcV(index2adr(L, idx)); - n--; - api_check(L, (uint32_t)n < fn->l.nupvalues); - return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : - (void *)&fn->c.upvalue[n]; -} - -LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2) -{ - GCfunc *fn1 = funcV(index2adr(L, idx1)); - GCfunc *fn2 = funcV(index2adr(L, idx2)); - n1--; n2--; - api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues); - api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues); - setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]); - lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1])); -} - -LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname) -{ - cTValue *o = index2adr(L, idx); - if (tvisudata(o)) { - GCudata *ud = udataV(o); - cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname)); - if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable)) - return uddata(ud); - } - lj_err_argtype(L, idx, tname); - return NULL; /* unreachable */ -} - -/* -- Object setters ------------------------------------------------------ */ - -LUA_API void lua_settable(lua_State *L, int idx) -{ - TValue *o; - cTValue *t = index2adr(L, idx); - api_checknelems(L, 2); - api_checkvalidindex(L, t); - o = lj_meta_tset(L, t, L->top-2); - if (o) { - /* NOBARRIER: lj_meta_tset ensures the table is not black. */ - copyTV(L, o, L->top-1); - L->top -= 2; - } else { - L->top += 3; - copyTV(L, L->top-1, L->top-6); - lj_vm_call(L, L->top-3, 0+1); - L->top -= 3; - } -} - -LUA_API void lua_setfield(lua_State *L, int idx, const char *k) -{ - TValue *o; - TValue key; - cTValue *t = index2adr(L, idx); - api_checknelems(L, 1); - api_checkvalidindex(L, t); - setstrV(L, &key, lj_str_newz(L, k)); - o = lj_meta_tset(L, t, &key); - if (o) { - L->top--; - /* NOBARRIER: lj_meta_tset ensures the table is not black. */ - copyTV(L, o, L->top); - } else { - L->top += 3; - copyTV(L, L->top-1, L->top-6); - lj_vm_call(L, L->top-3, 0+1); - L->top -= 2; - } -} - -LUA_API void lua_rawset(lua_State *L, int idx) -{ - GCtab *t = tabV(index2adr(L, idx)); - TValue *dst, *key; - api_checknelems(L, 2); - key = L->top-2; - dst = lj_tab_set(L, t, key); - copyTV(L, dst, key+1); - lj_gc_anybarriert(L, t); - L->top = key; -} - -LUA_API void lua_rawseti(lua_State *L, int idx, int n) -{ - GCtab *t = tabV(index2adr(L, idx)); - TValue *dst, *src; - api_checknelems(L, 1); - dst = lj_tab_setint(L, t, n); - src = L->top-1; - copyTV(L, dst, src); - lj_gc_barriert(L, t, dst); - L->top = src; -} - -LUA_API int lua_setmetatable(lua_State *L, int idx) -{ - global_State *g; - GCtab *mt; - cTValue *o = index2adr(L, idx); - api_checknelems(L, 1); - api_checkvalidindex(L, o); - if (tvisnil(L->top-1)) { - mt = NULL; - } else { - api_check(L, tvistab(L->top-1)); - mt = tabV(L->top-1); - } - g = G(L); - if (tvistab(o)) { - setgcref(tabV(o)->metatable, obj2gco(mt)); - if (mt) - lj_gc_objbarriert(L, tabV(o), mt); - } else if (tvisudata(o)) { - setgcref(udataV(o)->metatable, obj2gco(mt)); - if (mt) - lj_gc_objbarrier(L, udataV(o), mt); - } else { - /* Flush cache, since traces specialize to basemt. But not during __gc. */ - if (lj_trace_flushall(L)) - lj_err_caller(L, LJ_ERR_NOGCMM); - if (tvisbool(o)) { - /* NOBARRIER: basemt is a GC root. */ - setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt)); - setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt)); - } else { - /* NOBARRIER: basemt is a GC root. */ - setgcref(basemt_obj(g, o), obj2gco(mt)); - } - } - L->top--; - return 1; -} - -LUA_API int lua_setfenv(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - GCtab *t; - api_checknelems(L, 1); - api_checkvalidindex(L, o); - api_check(L, tvistab(L->top-1)); - t = tabV(L->top-1); - if (tvisfunc(o)) { - setgcref(funcV(o)->c.env, obj2gco(t)); - } else if (tvisudata(o)) { - setgcref(udataV(o)->env, obj2gco(t)); - } else if (tvisthread(o)) { - setgcref(threadV(o)->env, obj2gco(t)); - } else { - L->top--; - return 0; - } - lj_gc_objbarrier(L, gcV(o), t); - L->top--; - return 1; -} - -LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n) -{ - cTValue *f = index2adr(L, idx); - TValue *val; - const char *name; - api_checknelems(L, 1); - name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val); - if (name) { - L->top--; - copyTV(L, val, L->top); - lj_gc_barrier(L, funcV(f), L->top); - } - return name; -} - -/* -- Calls --------------------------------------------------------------- */ - -LUA_API void lua_call(lua_State *L, int nargs, int nresults) -{ - api_check(L, L->status == 0 || L->status == LUA_ERRERR); - api_checknelems(L, nargs+1); - lj_vm_call(L, L->top - nargs, nresults+1); -} - -LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc) -{ - global_State *g = G(L); - uint8_t oldh = hook_save(g); - ptrdiff_t ef; - int status; - api_check(L, L->status == 0 || L->status == LUA_ERRERR); - api_checknelems(L, nargs+1); - if (errfunc == 0) { - ef = 0; - } else { - cTValue *o = stkindex2adr(L, errfunc); - api_checkvalidindex(L, o); - ef = savestack(L, o); - } - status = lj_vm_pcall(L, L->top - nargs, nresults+1, ef); - if (status) hook_restore(g, oldh); - return status; -} - -static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud) -{ - GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L)); - fn->c.f = func; - setfuncV(L, L->top, fn); - setlightudV(L->top+1, checklightudptr(L, ud)); - cframe_nres(L->cframe) = 1+0; /* Zero results. */ - L->top += 2; - return L->top-1; /* Now call the newly allocated C function. */ -} - -LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud) -{ - global_State *g = G(L); - uint8_t oldh = hook_save(g); - int status; - api_check(L, L->status == 0 || L->status == LUA_ERRERR); - status = lj_vm_cpcall(L, func, ud, cpcall); - if (status) hook_restore(g, oldh); - return status; -} - -LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field) -{ - if (luaL_getmetafield(L, idx, field)) { - TValue *base = L->top--; - copyTV(L, base, index2adr(L, idx)); - L->top = base+1; - lj_vm_call(L, base, 1+1); - return 1; - } - return 0; -} - -/* -- Coroutine yield and resume ------------------------------------------ */ - -LUA_API int lua_yield(lua_State *L, int nresults) -{ - void *cf = L->cframe; - global_State *g = G(L); - if (cframe_canyield(cf)) { - cf = cframe_raw(cf); - if (!hook_active(g)) { /* Regular yield: move results down if needed. */ - cTValue *f = L->top - nresults; - if (f > L->base) { - TValue *t = L->base; - while (--nresults >= 0) copyTV(L, t++, f++); - L->top = t; - } - L->cframe = NULL; - L->status = LUA_YIELD; - return -1; - } else { /* Yield from hook: add a pseudo-frame. */ - TValue *top = L->top; - hook_leave(g); - top->u64 = cframe_multres(cf); - setcont(top+1, lj_cont_hook); - setframe_pc(top+1, cframe_pc(cf)-1); - setframe_gc(top+2, obj2gco(L)); - setframe_ftsz(top+2, (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT); - L->top = L->base = top+3; -#if LJ_TARGET_X64 - lj_err_throw(L, LUA_YIELD); -#else - L->cframe = NULL; - L->status = LUA_YIELD; - lj_vm_unwind_c(cf, LUA_YIELD); -#endif - } - } - lj_err_msg(L, LJ_ERR_CYIELD); - return 0; /* unreachable */ -} - -LUA_API int lua_resume(lua_State *L, int nargs) -{ - if (L->cframe == NULL && L->status <= LUA_YIELD) - return lj_vm_resume(L, L->top - nargs, 0, 0); - L->top = L->base; - setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP)); - incr_top(L); - return LUA_ERRRUN; -} - -/* -- GC and memory management -------------------------------------------- */ - -LUA_API int lua_gc(lua_State *L, int what, int data) -{ - global_State *g = G(L); - int res = 0; - switch (what) { - case LUA_GCSTOP: - g->gc.threshold = LJ_MAX_MEM; - break; - case LUA_GCRESTART: - g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total; - break; - case LUA_GCCOLLECT: - lj_gc_fullgc(L); - break; - case LUA_GCCOUNT: - res = (int)(g->gc.total >> 10); - break; - case LUA_GCCOUNTB: - res = (int)(g->gc.total & 0x3ff); - break; - case LUA_GCSTEP: { - MSize a = (MSize)data << 10; - g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0; - while (g->gc.total >= g->gc.threshold) - if (lj_gc_step(L)) { - res = 1; - break; - } - break; - } - case LUA_GCSETPAUSE: - res = (int)(g->gc.pause); - g->gc.pause = (MSize)data; - break; - case LUA_GCSETSTEPMUL: - res = (int)(g->gc.stepmul); - g->gc.stepmul = (MSize)data; - break; - default: - res = -1; /* Invalid option. */ - } - return res; -} - -LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud) -{ - global_State *g = G(L); - if (ud) *ud = g->allocd; - return g->allocf; -} - -LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud) -{ - global_State *g = G(L); - g->allocd = ud; - g->allocf = f; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_arch.h b/third-party/LuaJIT-2.0.2/src/lj_arch.h deleted file mode 100644 index 9ea10d0f5e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_arch.h +++ /dev/null @@ -1,419 +0,0 @@ -/* -** Target architecture selection. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_ARCH_H -#define _LJ_ARCH_H - -#include "lua.h" - -/* Target endianess. */ -#define LUAJIT_LE 0 -#define LUAJIT_BE 1 - -/* Target architectures. */ -#define LUAJIT_ARCH_X86 1 -#define LUAJIT_ARCH_x86 1 -#define LUAJIT_ARCH_X64 2 -#define LUAJIT_ARCH_x64 2 -#define LUAJIT_ARCH_ARM 3 -#define LUAJIT_ARCH_arm 3 -#define LUAJIT_ARCH_PPC 4 -#define LUAJIT_ARCH_ppc 4 -#define LUAJIT_ARCH_PPCSPE 5 -#define LUAJIT_ARCH_ppcspe 5 -#define LUAJIT_ARCH_MIPS 6 -#define LUAJIT_ARCH_mips 6 - -/* Target OS. */ -#define LUAJIT_OS_OTHER 0 -#define LUAJIT_OS_WINDOWS 1 -#define LUAJIT_OS_LINUX 2 -#define LUAJIT_OS_OSX 3 -#define LUAJIT_OS_BSD 4 -#define LUAJIT_OS_POSIX 5 - -/* Select native target if no target defined. */ -#ifndef LUAJIT_TARGET - -#if defined(__i386) || defined(__i386__) || defined(_M_IX86) -#define LUAJIT_TARGET LUAJIT_ARCH_X86 -#elif defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) -#define LUAJIT_TARGET LUAJIT_ARCH_X64 -#elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM) -#define LUAJIT_TARGET LUAJIT_ARCH_ARM -#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC) -#ifdef __NO_FPRS__ -#define LUAJIT_TARGET LUAJIT_ARCH_PPCSPE -#else -#define LUAJIT_TARGET LUAJIT_ARCH_PPC -#endif -#elif defined(__mips__) || defined(__mips) || defined(__MIPS__) || defined(__MIPS) -#define LUAJIT_TARGET LUAJIT_ARCH_MIPS -#else -#error "No support for this architecture (yet)" -#endif - -#endif - -/* Select native OS if no target OS defined. */ -#ifndef LUAJIT_OS - -#if defined(_WIN32) && !defined(_XBOX_VER) -#define LUAJIT_OS LUAJIT_OS_WINDOWS -#elif defined(__linux__) -#define LUAJIT_OS LUAJIT_OS_LINUX -#elif defined(__MACH__) && defined(__APPLE__) -#define LUAJIT_OS LUAJIT_OS_OSX -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ - defined(__NetBSD__) || defined(__OpenBSD__) -#define LUAJIT_OS LUAJIT_OS_BSD -#elif (defined(__sun__) && defined(__svr4__)) || defined(__CYGWIN__) -#define LUAJIT_OS LUAJIT_OS_POSIX -#else -#define LUAJIT_OS LUAJIT_OS_OTHER -#endif - -#endif - -/* Set target OS properties. */ -#if LUAJIT_OS == LUAJIT_OS_WINDOWS -#define LJ_OS_NAME "Windows" -#elif LUAJIT_OS == LUAJIT_OS_LINUX -#define LJ_OS_NAME "Linux" -#elif LUAJIT_OS == LUAJIT_OS_OSX -#define LJ_OS_NAME "OSX" -#elif LUAJIT_OS == LUAJIT_OS_BSD -#define LJ_OS_NAME "BSD" -#elif LUAJIT_OS == LUAJIT_OS_POSIX -#define LJ_OS_NAME "POSIX" -#else -#define LJ_OS_NAME "Other" -#endif - -#define LJ_TARGET_WINDOWS (LUAJIT_OS == LUAJIT_OS_WINDOWS) -#define LJ_TARGET_LINUX (LUAJIT_OS == LUAJIT_OS_LINUX) -#define LJ_TARGET_OSX (LUAJIT_OS == LUAJIT_OS_OSX) -#define LJ_TARGET_IOS (LJ_TARGET_OSX && LUAJIT_TARGET == LUAJIT_ARCH_ARM) -#define LJ_TARGET_POSIX (LUAJIT_OS > LUAJIT_OS_WINDOWS) -#define LJ_TARGET_DLOPEN LJ_TARGET_POSIX - -#ifdef __CELLOS_LV2__ -#define LJ_TARGET_PS3 1 -#define LJ_TARGET_CONSOLE 1 -#endif - -#if _XBOX_VER >= 200 -#define LJ_TARGET_XBOX360 1 -#define LJ_TARGET_CONSOLE 1 -#endif - -#define LJ_NUMMODE_SINGLE 0 /* Single-number mode only. */ -#define LJ_NUMMODE_SINGLE_DUAL 1 /* Default to single-number mode. */ -#define LJ_NUMMODE_DUAL 2 /* Dual-number mode only. */ -#define LJ_NUMMODE_DUAL_SINGLE 3 /* Default to dual-number mode. */ - -/* Set target architecture properties. */ -#if LUAJIT_TARGET == LUAJIT_ARCH_X86 - -#define LJ_ARCH_NAME "x86" -#define LJ_ARCH_BITS 32 -#define LJ_ARCH_ENDIAN LUAJIT_LE -#if LJ_TARGET_WINDOWS || __CYGWIN__ -#define LJ_ABI_WIN 1 -#else -#define LJ_ABI_WIN 0 -#endif -#define LJ_TARGET_X86 1 -#define LJ_TARGET_X86ORX64 1 -#define LJ_TARGET_EHRETREG 0 -#define LJ_TARGET_MASKSHIFT 1 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNALIGNED 1 -#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL - -#elif LUAJIT_TARGET == LUAJIT_ARCH_X64 - -#define LJ_ARCH_NAME "x64" -#define LJ_ARCH_BITS 64 -#define LJ_ARCH_ENDIAN LUAJIT_LE -#define LJ_ABI_WIN LJ_TARGET_WINDOWS -#define LJ_TARGET_X64 1 -#define LJ_TARGET_X86ORX64 1 -#define LJ_TARGET_EHRETREG 0 -#define LJ_TARGET_JUMPRANGE 31 /* +-2^31 = +-2GB */ -#define LJ_TARGET_MASKSHIFT 1 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNALIGNED 1 -#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL - -#elif LUAJIT_TARGET == LUAJIT_ARCH_ARM - -#define LJ_ARCH_NAME "arm" -#define LJ_ARCH_BITS 32 -#define LJ_ARCH_ENDIAN LUAJIT_LE -#if !defined(LJ_ARCH_HASFPU) && __SOFTFP__ -#define LJ_ARCH_HASFPU 0 -#endif -#if !defined(LJ_ABI_SOFTFP) && !__ARM_PCS_VFP -#define LJ_ABI_SOFTFP 1 -#endif -#define LJ_ABI_EABI 1 -#define LJ_TARGET_ARM 1 -#define LJ_TARGET_EHRETREG 0 -#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ -#define LJ_TARGET_MASKSHIFT 0 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ -#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL - -#if __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH_7S__ -#define LJ_ARCH_VERSION 70 -#elif __ARM_ARCH_6T2__ -#define LJ_ARCH_VERSION 61 -#elif __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6K__ || __ARM_ARCH_6Z__ || __ARM_ARCH_6ZK__ -#define LJ_ARCH_VERSION 60 -#else -#define LJ_ARCH_VERSION 50 -#endif - -#elif LUAJIT_TARGET == LUAJIT_ARCH_PPC - -#define LJ_ARCH_NAME "ppc" -#if _LP64 -#define LJ_ARCH_BITS 64 -#else -#define LJ_ARCH_BITS 32 -#endif -#define LJ_ARCH_ENDIAN LUAJIT_BE -#define LJ_TARGET_PPC 1 -#define LJ_TARGET_EHRETREG 3 -#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ -#define LJ_TARGET_MASKSHIFT 0 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ -#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL_SINGLE - -#if _ARCH_PWR7 -#define LJ_ARCH_VERSION 70 -#elif _ARCH_PWR6 -#define LJ_ARCH_VERSION 60 -#elif _ARCH_PWR5X -#define LJ_ARCH_VERSION 51 -#elif _ARCH_PWR5 -#define LJ_ARCH_VERSION 50 -#elif _ARCH_PWR4 -#define LJ_ARCH_VERSION 40 -#else -#define LJ_ARCH_VERSION 0 -#endif -#if __PPC64__ || __powerpc64__ || LJ_TARGET_CONSOLE -#define LJ_ARCH_PPC64 1 -#define LJ_ARCH_NOFFI 1 -#endif -#if _ARCH_PPCSQ -#define LJ_ARCH_SQRT 1 -#endif -#if _ARCH_PWR5X -#define LJ_ARCH_ROUND 1 -#endif -#if __PPU__ -#define LJ_ARCH_CELL 1 -#endif -#if LJ_TARGET_XBOX360 -#define LJ_ARCH_XENON 1 -#endif - -#elif LUAJIT_TARGET == LUAJIT_ARCH_PPCSPE - -#define LJ_ARCH_NAME "ppcspe" -#define LJ_ARCH_BITS 32 -#define LJ_ARCH_ENDIAN LUAJIT_BE -#ifndef LJ_ABI_SOFTFP -#define LJ_ABI_SOFTFP 1 -#endif -#define LJ_ABI_EABI 1 -#define LJ_TARGET_PPCSPE 1 -#define LJ_TARGET_EHRETREG 3 -#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ -#define LJ_TARGET_MASKSHIFT 0 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ -#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE -#define LJ_ARCH_NOFFI 1 /* NYI: comparisons, calls. */ -#define LJ_ARCH_NOJIT 1 - -#elif LUAJIT_TARGET == LUAJIT_ARCH_MIPS - -#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) -#define LJ_ARCH_NAME "mipsel" -#define LJ_ARCH_ENDIAN LUAJIT_LE -#else -#define LJ_ARCH_NAME "mips" -#define LJ_ARCH_ENDIAN LUAJIT_BE -#endif -#define LJ_ARCH_BITS 32 -#define LJ_TARGET_MIPS 1 -#define LJ_TARGET_EHRETREG 4 -#define LJ_TARGET_JUMPRANGE 27 /* 2*2^27 = 256MB-aligned region */ -#define LJ_TARGET_MASKSHIFT 1 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ -#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE - -#if _MIPS_ARCH_MIPS32R2 -#define LJ_ARCH_VERSION 20 -#else -#define LJ_ARCH_VERSION 10 -#endif - -#else -#error "No target architecture defined" -#endif - -#ifndef LJ_PAGESIZE -#define LJ_PAGESIZE 4096 -#endif - -/* Check for minimum required compiler versions. */ -#if defined(__GNUC__) -#if LJ_TARGET_X86 -#if (__GNUC__ < 3) || ((__GNUC__ == 3) && __GNUC_MINOR__ < 4) -#error "Need at least GCC 3.4 or newer" -#endif -#elif LJ_TARGET_X64 -#if __GNUC__ < 4 -#error "Need at least GCC 4.0 or newer" -#endif -#elif LJ_TARGET_ARM -#if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 2) -#error "Need at least GCC 4.2 or newer" -#endif -#elif !LJ_TARGET_PS3 -#if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 3) -#error "Need at least GCC 4.3 or newer" -#endif -#endif -#endif - -/* Check target-specific constraints. */ -#ifndef _BUILDVM_H -#if LJ_TARGET_X64 -#if __USING_SJLJ_EXCEPTIONS__ -#error "Need a C compiler with native exception handling on x64" -#endif -#elif LJ_TARGET_ARM -#if defined(__ARMEB__) -#error "No support for big-endian ARM" -#endif -#if __ARM_ARCH_6M__ || __ARM_ARCH_7M__ || __ARM_ARCH_7EM__ -#error "No support for Cortex-M CPUs" -#endif -#if !(__ARM_EABI__ || LJ_TARGET_IOS) -#error "Only ARM EABI or iOS 3.0+ ABI is supported" -#endif -#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE -#if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE) -#error "No support for PowerPC CPUs without double-precision FPU" -#endif -#if defined(_LITTLE_ENDIAN) -#error "No support for little-endian PowerPC" -#endif -#if defined(_LP64) -#error "No support for PowerPC 64 bit mode" -#endif -#elif LJ_TARGET_MIPS -#if defined(__mips_soft_float) -#error "No support for MIPS CPUs without FPU" -#endif -#endif -#endif - -/* Enable or disable the dual-number mode for the VM. */ -#if (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE && LUAJIT_NUMMODE == 2) || \ - (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL && LUAJIT_NUMMODE == 1) -#error "No support for this number mode on this architecture" -#endif -#if LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL || \ - (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL_SINGLE && LUAJIT_NUMMODE != 1) || \ - (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE_DUAL && LUAJIT_NUMMODE == 2) -#define LJ_DUALNUM 1 -#else -#define LJ_DUALNUM 0 -#endif - -#if LJ_TARGET_IOS || LJ_TARGET_CONSOLE -/* Runtime code generation is restricted on iOS. Complain to Apple, not me. */ -/* Ditto for the consoles. Complain to Sony or MS, not me. */ -#ifndef LUAJIT_ENABLE_JIT -#define LJ_OS_NOJIT 1 -#endif -#endif - -/* Disable or enable the JIT compiler. */ -#if defined(LUAJIT_DISABLE_JIT) || defined(LJ_ARCH_NOJIT) || defined(LJ_OS_NOJIT) -#define LJ_HASJIT 0 -#else -#define LJ_HASJIT 1 -#endif - -/* Disable or enable the FFI extension. */ -#if defined(LUAJIT_DISABLE_FFI) || defined(LJ_ARCH_NOFFI) -#define LJ_HASFFI 0 -#else -#define LJ_HASFFI 1 -#endif - -#ifndef LJ_ARCH_HASFPU -#define LJ_ARCH_HASFPU 1 -#endif -#ifndef LJ_ABI_SOFTFP -#define LJ_ABI_SOFTFP 0 -#endif -#define LJ_SOFTFP (!LJ_ARCH_HASFPU) - -#if LJ_ARCH_ENDIAN == LUAJIT_BE -#define LJ_LE 0 -#define LJ_BE 1 -#define LJ_ENDIAN_SELECT(le, be) be -#define LJ_ENDIAN_LOHI(lo, hi) hi lo -#else -#define LJ_LE 1 -#define LJ_BE 0 -#define LJ_ENDIAN_SELECT(le, be) le -#define LJ_ENDIAN_LOHI(lo, hi) lo hi -#endif - -#if LJ_ARCH_BITS == 32 -#define LJ_32 1 -#define LJ_64 0 -#else -#define LJ_32 0 -#define LJ_64 1 -#endif - -#ifndef LJ_TARGET_UNALIGNED -#define LJ_TARGET_UNALIGNED 0 -#endif - -/* Various workarounds for embedded operating systems. */ -#if (defined(__ANDROID__) && !defined(LJ_TARGET_X86ORX64)) || defined(__symbian__) || LJ_TARGET_XBOX360 -#define LUAJIT_NO_LOG2 -#endif -#if defined(__symbian__) -#define LUAJIT_NO_EXP2 -#endif - -#if defined(LUAJIT_NO_UNWIND) || defined(__symbian__) || LJ_TARGET_IOS || LJ_TARGET_PS3 -#define LJ_NO_UNWIND 1 -#endif - -/* Compatibility with Lua 5.1 vs. 5.2. */ -#ifdef LUAJIT_ENABLE_LUA52COMPAT -#define LJ_52 1 -#else -#define LJ_52 0 -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_asm.c b/third-party/LuaJIT-2.0.2/src/lj_asm.c deleted file mode 100644 index 316e81d6a8..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_asm.c +++ /dev/null @@ -1,1912 +0,0 @@ -/* -** IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_asm_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_mcode.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_asm.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_target.h" - -#ifdef LUA_USE_ASSERT -#include -#endif - -/* -- Assembler state and common macros ----------------------------------- */ - -/* Assembler state. */ -typedef struct ASMState { - RegCost cost[RID_MAX]; /* Reference and blended allocation cost for regs. */ - - MCode *mcp; /* Current MCode pointer (grows down). */ - MCode *mclim; /* Lower limit for MCode memory + red zone. */ -#ifdef LUA_USE_ASSERT - MCode *mcp_prev; /* Red zone overflow check. */ -#endif - - IRIns *ir; /* Copy of pointer to IR instructions/constants. */ - jit_State *J; /* JIT compiler state. */ - -#if LJ_TARGET_X86ORX64 - x86ModRM mrm; /* Fused x86 address operand. */ -#endif - - RegSet freeset; /* Set of free registers. */ - RegSet modset; /* Set of registers modified inside the loop. */ - RegSet weakset; /* Set of weakly referenced registers. */ - RegSet phiset; /* Set of PHI registers. */ - - uint32_t flags; /* Copy of JIT compiler flags. */ - int loopinv; /* Loop branch inversion (0:no, 1:yes, 2:yes+CC_P). */ - - int32_t evenspill; /* Next even spill slot. */ - int32_t oddspill; /* Next odd spill slot (or 0). */ - - IRRef curins; /* Reference of current instruction. */ - IRRef stopins; /* Stop assembly before hitting this instruction. */ - IRRef orignins; /* Original T->nins. */ - - IRRef snapref; /* Current snapshot is active after this reference. */ - IRRef snaprename; /* Rename highwater mark for snapshot check. */ - SnapNo snapno; /* Current snapshot number. */ - SnapNo loopsnapno; /* Loop snapshot number. */ - - IRRef fuseref; /* Fusion limit (loopref, 0 or FUSE_DISABLED). */ - IRRef sectref; /* Section base reference (loopref or 0). */ - IRRef loopref; /* Reference of LOOP instruction (or 0). */ - - BCReg topslot; /* Number of slots for stack check (unless 0). */ - int32_t gcsteps; /* Accumulated number of GC steps (per section). */ - - GCtrace *T; /* Trace to assemble. */ - GCtrace *parent; /* Parent trace (or NULL). */ - - MCode *mcbot; /* Bottom of reserved MCode. */ - MCode *mctop; /* Top of generated MCode. */ - MCode *mcloop; /* Pointer to loop MCode (or NULL). */ - MCode *invmcp; /* Points to invertible loop branch (or NULL). */ - MCode *flagmcp; /* Pending opportunity to merge flag setting ins. */ - MCode *realign; /* Realign loop if not NULL. */ - -#ifdef RID_NUM_KREF - int32_t krefk[RID_NUM_KREF]; -#endif - IRRef1 phireg[RID_MAX]; /* PHI register references. */ - uint16_t parentmap[LJ_MAX_JSLOTS]; /* Parent instruction to RegSP map. */ -} ASMState; - -#define IR(ref) (&as->ir[(ref)]) - -#define ASMREF_TMP1 REF_TRUE /* Temp. register. */ -#define ASMREF_TMP2 REF_FALSE /* Temp. register. */ -#define ASMREF_L REF_NIL /* Stores register for L. */ - -/* Check for variant to invariant references. */ -#define iscrossref(as, ref) ((ref) < as->sectref) - -/* Inhibit memory op fusion from variant to invariant references. */ -#define FUSE_DISABLED (~(IRRef)0) -#define mayfuse(as, ref) ((ref) > as->fuseref) -#define neverfuse(as) (as->fuseref == FUSE_DISABLED) -#define canfuse(as, ir) (!neverfuse(as) && !irt_isphi((ir)->t)) -#define opisfusableload(o) \ - ((o) == IR_ALOAD || (o) == IR_HLOAD || (o) == IR_ULOAD || \ - (o) == IR_FLOAD || (o) == IR_XLOAD || (o) == IR_SLOAD || (o) == IR_VLOAD) - -/* Sparse limit checks using a red zone before the actual limit. */ -#define MCLIM_REDZONE 64 - -static LJ_NORET LJ_NOINLINE void asm_mclimit(ASMState *as) -{ - lj_mcode_limiterr(as->J, (size_t)(as->mctop - as->mcp + 4*MCLIM_REDZONE)); -} - -static LJ_AINLINE void checkmclim(ASMState *as) -{ -#ifdef LUA_USE_ASSERT - if (as->mcp + MCLIM_REDZONE < as->mcp_prev) { - IRIns *ir = IR(as->curins+1); - fprintf(stderr, "RED ZONE OVERFLOW: %p IR %04d %02d %04d %04d\n", as->mcp, - as->curins+1-REF_BIAS, ir->o, ir->op1-REF_BIAS, ir->op2-REF_BIAS); - lua_assert(0); - } -#endif - if (LJ_UNLIKELY(as->mcp < as->mclim)) asm_mclimit(as); -#ifdef LUA_USE_ASSERT - as->mcp_prev = as->mcp; -#endif -} - -#ifdef RID_NUM_KREF -#define ra_iskref(ref) ((ref) < RID_NUM_KREF) -#define ra_krefreg(ref) ((Reg)(RID_MIN_KREF + (Reg)(ref))) -#define ra_krefk(as, ref) (as->krefk[(ref)]) - -static LJ_AINLINE void ra_setkref(ASMState *as, Reg r, int32_t k) -{ - IRRef ref = (IRRef)(r - RID_MIN_KREF); - as->krefk[ref] = k; - as->cost[r] = REGCOST(ref, ref); -} - -#else -#define ra_iskref(ref) 0 -#define ra_krefreg(ref) RID_MIN_GPR -#define ra_krefk(as, ref) 0 -#endif - -/* Arch-specific field offsets. */ -static const uint8_t field_ofs[IRFL__MAX+1] = { -#define FLOFS(name, ofs) (uint8_t)(ofs), -IRFLDEF(FLOFS) -#undef FLOFS - 0 -}; - -/* -- Target-specific instruction emitter --------------------------------- */ - -#if LJ_TARGET_X86ORX64 -#include "lj_emit_x86.h" -#elif LJ_TARGET_ARM -#include "lj_emit_arm.h" -#elif LJ_TARGET_PPC -#include "lj_emit_ppc.h" -#elif LJ_TARGET_MIPS -#include "lj_emit_mips.h" -#else -#error "Missing instruction emitter for target CPU" -#endif - -/* -- Register allocator debugging ---------------------------------------- */ - -/* #define LUAJIT_DEBUG_RA */ - -#ifdef LUAJIT_DEBUG_RA - -#include -#include - -#define RIDNAME(name) #name, -static const char *const ra_regname[] = { - GPRDEF(RIDNAME) - FPRDEF(RIDNAME) - VRIDDEF(RIDNAME) - NULL -}; -#undef RIDNAME - -static char ra_dbg_buf[65536]; -static char *ra_dbg_p; -static char *ra_dbg_merge; -static MCode *ra_dbg_mcp; - -static void ra_dstart(void) -{ - ra_dbg_p = ra_dbg_buf; - ra_dbg_merge = NULL; - ra_dbg_mcp = NULL; -} - -static void ra_dflush(void) -{ - fwrite(ra_dbg_buf, 1, (size_t)(ra_dbg_p-ra_dbg_buf), stdout); - ra_dstart(); -} - -static void ra_dprintf(ASMState *as, const char *fmt, ...) -{ - char *p; - va_list argp; - va_start(argp, fmt); - p = ra_dbg_mcp == as->mcp ? ra_dbg_merge : ra_dbg_p; - ra_dbg_mcp = NULL; - p += sprintf(p, "%08x \e[36m%04d ", (uintptr_t)as->mcp, as->curins-REF_BIAS); - for (;;) { - const char *e = strchr(fmt, '$'); - if (e == NULL) break; - memcpy(p, fmt, (size_t)(e-fmt)); - p += e-fmt; - if (e[1] == 'r') { - Reg r = va_arg(argp, Reg) & RID_MASK; - if (r <= RID_MAX) { - const char *q; - for (q = ra_regname[r]; *q; q++) - *p++ = *q >= 'A' && *q <= 'Z' ? *q + 0x20 : *q; - } else { - *p++ = '?'; - lua_assert(0); - } - } else if (e[1] == 'f' || e[1] == 'i') { - IRRef ref; - if (e[1] == 'f') - ref = va_arg(argp, IRRef); - else - ref = va_arg(argp, IRIns *) - as->ir; - if (ref >= REF_BIAS) - p += sprintf(p, "%04d", ref - REF_BIAS); - else - p += sprintf(p, "K%03d", REF_BIAS - ref); - } else if (e[1] == 's') { - uint32_t slot = va_arg(argp, uint32_t); - p += sprintf(p, "[sp+0x%x]", sps_scale(slot)); - } else if (e[1] == 'x') { - p += sprintf(p, "%08x", va_arg(argp, int32_t)); - } else { - lua_assert(0); - } - fmt = e+2; - } - va_end(argp); - while (*fmt) - *p++ = *fmt++; - *p++ = '\e'; *p++ = '['; *p++ = 'm'; *p++ = '\n'; - if (p > ra_dbg_buf+sizeof(ra_dbg_buf)-256) { - fwrite(ra_dbg_buf, 1, (size_t)(p-ra_dbg_buf), stdout); - p = ra_dbg_buf; - } - ra_dbg_p = p; -} - -#define RA_DBG_START() ra_dstart() -#define RA_DBG_FLUSH() ra_dflush() -#define RA_DBG_REF() \ - do { char *_p = ra_dbg_p; ra_dprintf(as, ""); \ - ra_dbg_merge = _p; ra_dbg_mcp = as->mcp; } while (0) -#define RA_DBGX(x) ra_dprintf x - -#else -#define RA_DBG_START() ((void)0) -#define RA_DBG_FLUSH() ((void)0) -#define RA_DBG_REF() ((void)0) -#define RA_DBGX(x) ((void)0) -#endif - -/* -- Register allocator -------------------------------------------------- */ - -#define ra_free(as, r) rset_set(as->freeset, (r)) -#define ra_modified(as, r) rset_set(as->modset, (r)) -#define ra_weak(as, r) rset_set(as->weakset, (r)) -#define ra_noweak(as, r) rset_clear(as->weakset, (r)) - -#define ra_used(ir) (ra_hasreg((ir)->r) || ra_hasspill((ir)->s)) - -/* Setup register allocator. */ -static void ra_setup(ASMState *as) -{ - Reg r; - /* Initially all regs (except the stack pointer) are free for use. */ - as->freeset = RSET_INIT; - as->modset = RSET_EMPTY; - as->weakset = RSET_EMPTY; - as->phiset = RSET_EMPTY; - memset(as->phireg, 0, sizeof(as->phireg)); - for (r = RID_MIN_GPR; r < RID_MAX; r++) - as->cost[r] = REGCOST(~0u, 0u); -} - -/* Rematerialize constants. */ -static Reg ra_rematk(ASMState *as, IRRef ref) -{ - IRIns *ir; - Reg r; - if (ra_iskref(ref)) { - r = ra_krefreg(ref); - lua_assert(!rset_test(as->freeset, r)); - ra_free(as, r); - ra_modified(as, r); - emit_loadi(as, r, ra_krefk(as, ref)); - return r; - } - ir = IR(ref); - r = ir->r; - lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s)); - ra_free(as, r); - ra_modified(as, r); - ir->r = RID_INIT; /* Do not keep any hint. */ - RA_DBGX((as, "remat $i $r", ir, r)); -#if !LJ_SOFTFP - if (ir->o == IR_KNUM) { - emit_loadn(as, r, ir_knum(ir)); - } else -#endif - if (emit_canremat(REF_BASE) && ir->o == IR_BASE) { - ra_sethint(ir->r, RID_BASE); /* Restore BASE register hint. */ - emit_getgl(as, r, jit_base); - } else if (emit_canremat(ASMREF_L) && ir->o == IR_KPRI) { - lua_assert(irt_isnil(ir->t)); /* REF_NIL stores ASMREF_L register. */ - emit_getgl(as, r, jit_L); -#if LJ_64 - } else if (ir->o == IR_KINT64) { - emit_loadu64(as, r, ir_kint64(ir)->u64); -#endif - } else { - lua_assert(ir->o == IR_KINT || ir->o == IR_KGC || - ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL); - emit_loadi(as, r, ir->i); - } - return r; -} - -/* Force a spill. Allocate a new spill slot if needed. */ -static int32_t ra_spill(ASMState *as, IRIns *ir) -{ - int32_t slot = ir->s; - if (!ra_hasspill(slot)) { - if (irt_is64(ir->t)) { - slot = as->evenspill; - as->evenspill += 2; - } else if (as->oddspill) { - slot = as->oddspill; - as->oddspill = 0; - } else { - slot = as->evenspill; - as->oddspill = slot+1; - as->evenspill += 2; - } - if (as->evenspill > 256) - lj_trace_err(as->J, LJ_TRERR_SPILLOV); - ir->s = (uint8_t)slot; - } - return sps_scale(slot); -} - -/* Release the temporarily allocated register in ASMREF_TMP1/ASMREF_TMP2. */ -static Reg ra_releasetmp(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - Reg r = ir->r; - lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s)); - ra_free(as, r); - ra_modified(as, r); - ir->r = RID_INIT; - return r; -} - -/* Restore a register (marked as free). Rematerialize or force a spill. */ -static Reg ra_restore(ASMState *as, IRRef ref) -{ - if (emit_canremat(ref)) { - return ra_rematk(as, ref); - } else { - IRIns *ir = IR(ref); - int32_t ofs = ra_spill(as, ir); /* Force a spill slot. */ - Reg r = ir->r; - lua_assert(ra_hasreg(r)); - ra_sethint(ir->r, r); /* Keep hint. */ - ra_free(as, r); - if (!rset_test(as->weakset, r)) { /* Only restore non-weak references. */ - ra_modified(as, r); - RA_DBGX((as, "restore $i $r", ir, r)); - emit_spload(as, ir, r, ofs); - } - return r; - } -} - -/* Save a register to a spill slot. */ -static void ra_save(ASMState *as, IRIns *ir, Reg r) -{ - RA_DBGX((as, "save $i $r", ir, r)); - emit_spstore(as, ir, r, sps_scale(ir->s)); -} - -#define MINCOST(name) \ - if (rset_test(RSET_ALL, RID_##name) && \ - LJ_LIKELY(allow&RID2RSET(RID_##name)) && as->cost[RID_##name] < cost) \ - cost = as->cost[RID_##name]; - -/* Evict the register with the lowest cost, forcing a restore. */ -static Reg ra_evict(ASMState *as, RegSet allow) -{ - IRRef ref; - RegCost cost = ~(RegCost)0; - lua_assert(allow != RSET_EMPTY); - if (RID_NUM_FPR == 0 || allow < RID2RSET(RID_MAX_GPR)) { - GPRDEF(MINCOST) - } else { - FPRDEF(MINCOST) - } - ref = regcost_ref(cost); - lua_assert(ra_iskref(ref) || (ref >= as->T->nk && ref < as->T->nins)); - /* Preferably pick any weak ref instead of a non-weak, non-const ref. */ - if (!irref_isk(ref) && (as->weakset & allow)) { - IRIns *ir = IR(ref); - if (!rset_test(as->weakset, ir->r)) - ref = regcost_ref(as->cost[rset_pickbot((as->weakset & allow))]); - } - return ra_restore(as, ref); -} - -/* Pick any register (marked as free). Evict on-demand. */ -static Reg ra_pick(ASMState *as, RegSet allow) -{ - RegSet pick = as->freeset & allow; - if (!pick) - return ra_evict(as, allow); - else - return rset_picktop(pick); -} - -/* Get a scratch register (marked as free). */ -static Reg ra_scratch(ASMState *as, RegSet allow) -{ - Reg r = ra_pick(as, allow); - ra_modified(as, r); - RA_DBGX((as, "scratch $r", r)); - return r; -} - -/* Evict all registers from a set (if not free). */ -static void ra_evictset(ASMState *as, RegSet drop) -{ - RegSet work; - as->modset |= drop; -#if !LJ_SOFTFP - work = (drop & ~as->freeset) & RSET_FPR; - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } -#endif - work = (drop & ~as->freeset); - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } -} - -/* Evict (rematerialize) all registers allocated to constants. */ -static void ra_evictk(ASMState *as) -{ - RegSet work; -#if !LJ_SOFTFP - work = ~as->freeset & RSET_FPR; - while (work) { - Reg r = rset_pickbot(work); - IRRef ref = regcost_ref(as->cost[r]); - if (emit_canremat(ref) && irref_isk(ref)) { - ra_rematk(as, ref); - checkmclim(as); - } - rset_clear(work, r); - } -#endif - work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_pickbot(work); - IRRef ref = regcost_ref(as->cost[r]); - if (emit_canremat(ref) && irref_isk(ref)) { - ra_rematk(as, ref); - checkmclim(as); - } - rset_clear(work, r); - } -} - -#ifdef RID_NUM_KREF -/* Allocate a register for a constant. */ -static Reg ra_allock(ASMState *as, int32_t k, RegSet allow) -{ - /* First try to find a register which already holds the same constant. */ - RegSet pick, work = ~as->freeset & RSET_GPR; - Reg r; - while (work) { - IRRef ref; - r = rset_pickbot(work); - ref = regcost_ref(as->cost[r]); - if (ref < ASMREF_L && - k == (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i)) - return r; - rset_clear(work, r); - } - pick = as->freeset & allow; - if (pick) { - /* Constants should preferably get unmodified registers. */ - if ((pick & ~as->modset)) - pick &= ~as->modset; - r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ - } else { - r = ra_evict(as, allow); - } - RA_DBGX((as, "allock $x $r", k, r)); - ra_setkref(as, r, k); - rset_clear(as->freeset, r); - ra_noweak(as, r); - return r; -} - -/* Allocate a specific register for a constant. */ -static void ra_allockreg(ASMState *as, int32_t k, Reg r) -{ - Reg kr = ra_allock(as, k, RID2RSET(r)); - if (kr != r) { - IRIns irdummy; - irdummy.t.irt = IRT_INT; - ra_scratch(as, RID2RSET(r)); - emit_movrr(as, &irdummy, r, kr); - } -} -#else -#define ra_allockreg(as, k, r) emit_loadi(as, (r), (k)) -#endif - -/* Allocate a register for ref from the allowed set of registers. -** Note: this function assumes the ref does NOT have a register yet! -** Picks an optimal register, sets the cost and marks the register as non-free. -*/ -static Reg ra_allocref(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - RegSet pick = as->freeset & allow; - Reg r; - lua_assert(ra_noreg(ir->r)); - if (pick) { - /* First check register hint from propagation or PHI. */ - if (ra_hashint(ir->r)) { - r = ra_gethint(ir->r); - if (rset_test(pick, r)) /* Use hint register if possible. */ - goto found; - /* Rematerialization is cheaper than missing a hint. */ - if (rset_test(allow, r) && emit_canremat(regcost_ref(as->cost[r]))) { - ra_rematk(as, regcost_ref(as->cost[r])); - goto found; - } - RA_DBGX((as, "hintmiss $f $r", ref, r)); - } - /* Invariants should preferably get unmodified registers. */ - if (ref < as->loopref && !irt_isphi(ir->t)) { - if ((pick & ~as->modset)) - pick &= ~as->modset; - r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ - } else { - /* We've got plenty of regs, so get callee-save regs if possible. */ - if (RID_NUM_GPR > 8 && (pick & ~RSET_SCRATCH)) - pick &= ~RSET_SCRATCH; - r = rset_picktop(pick); - } - } else { - r = ra_evict(as, allow); - } -found: - RA_DBGX((as, "alloc $f $r", ref, r)); - ir->r = (uint8_t)r; - rset_clear(as->freeset, r); - ra_noweak(as, r); - as->cost[r] = REGCOST_REF_T(ref, irt_t(ir->t)); - return r; -} - -/* Allocate a register on-demand. */ -static Reg ra_alloc1(ASMState *as, IRRef ref, RegSet allow) -{ - Reg r = IR(ref)->r; - /* Note: allow is ignored if the register is already allocated. */ - if (ra_noreg(r)) r = ra_allocref(as, ref, allow); - ra_noweak(as, r); - return r; -} - -/* Rename register allocation and emit move. */ -static void ra_rename(ASMState *as, Reg down, Reg up) -{ - IRRef ren, ref = regcost_ref(as->cost[up] = as->cost[down]); - IRIns *ir = IR(ref); - ir->r = (uint8_t)up; - as->cost[down] = 0; - lua_assert((down < RID_MAX_GPR) == (up < RID_MAX_GPR)); - lua_assert(!rset_test(as->freeset, down) && rset_test(as->freeset, up)); - ra_free(as, down); /* 'down' is free ... */ - ra_modified(as, down); - rset_clear(as->freeset, up); /* ... and 'up' is now allocated. */ - ra_noweak(as, up); - RA_DBGX((as, "rename $f $r $r", regcost_ref(as->cost[up]), down, up)); - emit_movrr(as, ir, down, up); /* Backwards codegen needs inverse move. */ - if (!ra_hasspill(IR(ref)->s)) { /* Add the rename to the IR. */ - lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), ref, as->snapno); - ren = tref_ref(lj_ir_emit(as->J)); - as->ir = as->T->ir; /* The IR may have been reallocated. */ - IR(ren)->r = (uint8_t)down; - IR(ren)->s = SPS_NONE; - } -} - -/* Pick a destination register (marked as free). -** Caveat: allow is ignored if there's already a destination register. -** Use ra_destreg() to get a specific register. -*/ -static Reg ra_dest(ASMState *as, IRIns *ir, RegSet allow) -{ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - } else { - if (ra_hashint(dest) && rset_test((as->freeset&allow), ra_gethint(dest))) { - dest = ra_gethint(dest); - ra_modified(as, dest); - RA_DBGX((as, "dest $r", dest)); - } else { - dest = ra_scratch(as, allow); - } - ir->r = dest; - } - if (LJ_UNLIKELY(ra_hasspill(ir->s))) ra_save(as, ir, dest); - return dest; -} - -/* Force a specific destination register (marked as free). */ -static void ra_destreg(ASMState *as, IRIns *ir, Reg r) -{ - Reg dest = ra_dest(as, ir, RID2RSET(r)); - if (dest != r) { - lua_assert(rset_test(as->freeset, r)); - ra_modified(as, r); - emit_movrr(as, ir, dest, r); - } -} - -#if LJ_TARGET_X86ORX64 -/* Propagate dest register to left reference. Emit moves as needed. -** This is a required fixup step for all 2-operand machine instructions. -*/ -static void ra_left(ASMState *as, Reg dest, IRRef lref) -{ - IRIns *ir = IR(lref); - Reg left = ir->r; - if (ra_noreg(left)) { - if (irref_isk(lref)) { - if (ir->o == IR_KNUM) { - cTValue *tv = ir_knum(ir); - /* FP remat needs a load except for +0. Still better than eviction. */ - if (tvispzero(tv) || !(as->freeset & RSET_FPR)) { - emit_loadn(as, dest, tv); - return; - } -#if LJ_64 - } else if (ir->o == IR_KINT64) { - emit_loadu64(as, dest, ir_kint64(ir)->u64); - return; -#endif - } else { - lua_assert(ir->o == IR_KINT || ir->o == IR_KGC || - ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL); - emit_loadi(as, dest, ir->i); - return; - } - } - if (!ra_hashint(left) && !iscrossref(as, lref)) - ra_sethint(ir->r, dest); /* Propagate register hint. */ - left = ra_allocref(as, lref, dest < RID_MAX_GPR ? RSET_GPR : RSET_FPR); - } - ra_noweak(as, left); - /* Move needed for true 3-operand instruction: y=a+b ==> y=a; y+=b. */ - if (dest != left) { - /* Use register renaming if dest is the PHI reg. */ - if (irt_isphi(ir->t) && as->phireg[dest] == lref) { - ra_modified(as, left); - ra_rename(as, left, dest); - } else { - emit_movrr(as, ir, dest, left); - } - } -} -#else -/* Similar to ra_left, except we override any hints. */ -static void ra_leftov(ASMState *as, Reg dest, IRRef lref) -{ - IRIns *ir = IR(lref); - Reg left = ir->r; - if (ra_noreg(left)) { - ra_sethint(ir->r, dest); /* Propagate register hint. */ - left = ra_allocref(as, lref, - (LJ_SOFTFP || dest < RID_MAX_GPR) ? RSET_GPR : RSET_FPR); - } - ra_noweak(as, left); - if (dest != left) { - /* Use register renaming if dest is the PHI reg. */ - if (irt_isphi(ir->t) && as->phireg[dest] == lref) { - ra_modified(as, left); - ra_rename(as, left, dest); - } else { - emit_movrr(as, ir, dest, left); - } - } -} -#endif - -#if !LJ_64 -/* Force a RID_RETLO/RID_RETHI destination register pair (marked as free). */ -static void ra_destpair(ASMState *as, IRIns *ir) -{ - Reg destlo = ir->r, desthi = (ir+1)->r; - /* First spill unrelated refs blocking the destination registers. */ - if (!rset_test(as->freeset, RID_RETLO) && - destlo != RID_RETLO && desthi != RID_RETLO) - ra_restore(as, regcost_ref(as->cost[RID_RETLO])); - if (!rset_test(as->freeset, RID_RETHI) && - destlo != RID_RETHI && desthi != RID_RETHI) - ra_restore(as, regcost_ref(as->cost[RID_RETHI])); - /* Next free the destination registers (if any). */ - if (ra_hasreg(destlo)) { - ra_free(as, destlo); - ra_modified(as, destlo); - } else { - destlo = RID_RETLO; - } - if (ra_hasreg(desthi)) { - ra_free(as, desthi); - ra_modified(as, desthi); - } else { - desthi = RID_RETHI; - } - /* Check for conflicts and shuffle the registers as needed. */ - if (destlo == RID_RETHI) { - if (desthi == RID_RETLO) { -#if LJ_TARGET_X86 - *--as->mcp = XI_XCHGa + RID_RETHI; -#else - emit_movrr(as, ir, RID_RETHI, RID_TMP); - emit_movrr(as, ir, RID_RETLO, RID_RETHI); - emit_movrr(as, ir, RID_TMP, RID_RETLO); -#endif - } else { - emit_movrr(as, ir, RID_RETHI, RID_RETLO); - if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI); - } - } else if (desthi == RID_RETLO) { - emit_movrr(as, ir, RID_RETLO, RID_RETHI); - if (destlo != RID_RETLO) emit_movrr(as, ir, destlo, RID_RETLO); - } else { - if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI); - if (destlo != RID_RETLO) emit_movrr(as, ir, destlo, RID_RETLO); - } - /* Restore spill slots (if any). */ - if (ra_hasspill((ir+1)->s)) ra_save(as, ir+1, RID_RETHI); - if (ra_hasspill(ir->s)) ra_save(as, ir, RID_RETLO); -} -#endif - -/* -- Snapshot handling --------- ----------------------------------------- */ - -/* Can we rematerialize a KNUM instead of forcing a spill? */ -static int asm_snap_canremat(ASMState *as) -{ - Reg r; - for (r = RID_MIN_FPR; r < RID_MAX_FPR; r++) - if (irref_isk(regcost_ref(as->cost[r]))) - return 1; - return 0; -} - -/* Check whether a sunk store corresponds to an allocation. */ -static int asm_sunk_store(ASMState *as, IRIns *ira, IRIns *irs) -{ - if (irs->s == 255) { - if (irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE || irs->o == IR_XSTORE) { - IRIns *irk = IR(irs->op1); - if (irk->o == IR_AREF || irk->o == IR_HREFK) - irk = IR(irk->op1); - return (IR(irk->op1) == ira); - } - return 0; - } else { - return (ira + irs->s == irs); /* Quick check. */ - } -} - -/* Allocate register or spill slot for a ref that escapes to a snapshot. */ -static void asm_snap_alloc1(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (!irref_isk(ref) && (!(ra_used(ir) || ir->r == RID_SUNK))) { - if (ir->r == RID_SINK) { - ir->r = RID_SUNK; -#if LJ_HASFFI - if (ir->o == IR_CNEWI) { /* Allocate CNEWI value. */ - asm_snap_alloc1(as, ir->op2); - if (LJ_32 && (ir+1)->o == IR_HIOP) - asm_snap_alloc1(as, (ir+1)->op2); - } else -#endif - { /* Allocate stored values for TNEW, TDUP and CNEW. */ - IRIns *irs; - lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW); - for (irs = IR(as->snapref-1); irs > ir; irs--) - if (irs->r == RID_SINK && asm_sunk_store(as, ir, irs)) { - lua_assert(irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE || irs->o == IR_XSTORE); - asm_snap_alloc1(as, irs->op2); - if (LJ_32 && (irs+1)->o == IR_HIOP) - asm_snap_alloc1(as, (irs+1)->op2); - } - } - } else { - RegSet allow; - if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT) { - IRIns *irc; - for (irc = IR(as->curins); irc > ir; irc--) - if ((irc->op1 == ref || irc->op2 == ref) && - !(irc->r == RID_SINK || irc->r == RID_SUNK)) - goto nosink; /* Don't sink conversion if result is used. */ - asm_snap_alloc1(as, ir->op1); - return; - } - nosink: - allow = (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR; - if ((as->freeset & allow) || - (allow == RSET_FPR && asm_snap_canremat(as))) { - /* Get a weak register if we have a free one or can rematerialize. */ - Reg r = ra_allocref(as, ref, allow); /* Allocate a register. */ - if (!irt_isphi(ir->t)) - ra_weak(as, r); /* But mark it as weakly referenced. */ - checkmclim(as); - RA_DBGX((as, "snapreg $f $r", ref, ir->r)); - } else { - ra_spill(as, ir); /* Otherwise force a spill slot. */ - RA_DBGX((as, "snapspill $f $s", ref, ir->s)); - } - } - } -} - -/* Allocate refs escaping to a snapshot. */ -static void asm_snap_alloc(ASMState *as) -{ - SnapShot *snap = &as->T->snap[as->snapno]; - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - IRRef ref = snap_ref(sn); - if (!irref_isk(ref)) { - asm_snap_alloc1(as, ref); - if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) { - lua_assert(irt_type(IR(ref+1)->t) == IRT_SOFTFP); - asm_snap_alloc1(as, ref+1); - } - } - } -} - -/* All guards for a snapshot use the same exitno. This is currently the -** same as the snapshot number. Since the exact origin of the exit cannot -** be determined, all guards for the same snapshot must exit with the same -** RegSP mapping. -** A renamed ref which has been used in a prior guard for the same snapshot -** would cause an inconsistency. The easy way out is to force a spill slot. -*/ -static int asm_snap_checkrename(ASMState *as, IRRef ren) -{ - SnapShot *snap = &as->T->snap[as->snapno]; - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - IRRef ref = snap_ref(sn); - if (ref == ren || (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && ++ref == ren)) { - IRIns *ir = IR(ref); - ra_spill(as, ir); /* Register renamed, so force a spill slot. */ - RA_DBGX((as, "snaprensp $f $s", ref, ir->s)); - return 1; /* Found. */ - } - } - return 0; /* Not found. */ -} - -/* Prepare snapshot for next guard instruction. */ -static void asm_snap_prep(ASMState *as) -{ - if (as->curins < as->snapref) { - do { - if (as->snapno == 0) return; /* Called by sunk stores before snap #0. */ - as->snapno--; - as->snapref = as->T->snap[as->snapno].ref; - } while (as->curins < as->snapref); - asm_snap_alloc(as); - as->snaprename = as->T->nins; - } else { - /* Process any renames above the highwater mark. */ - for (; as->snaprename < as->T->nins; as->snaprename++) { - IRIns *ir = IR(as->snaprename); - if (asm_snap_checkrename(as, ir->op1)) - ir->op2 = REF_BIAS-1; /* Kill rename. */ - } - } -} - -/* -- Miscellaneous helpers ----------------------------------------------- */ - -/* Collect arguments from CALL* and CARG instructions. */ -static void asm_collectargs(ASMState *as, IRIns *ir, - const CCallInfo *ci, IRRef *args) -{ - uint32_t n = CCI_NARGS(ci); - lua_assert(n <= CCI_NARGS_MAX*2); /* Account for split args. */ - if ((ci->flags & CCI_L)) { *args++ = ASMREF_L; n--; } - while (n-- > 1) { - ir = IR(ir->op1); - lua_assert(ir->o == IR_CARG); - args[n] = ir->op2 == REF_NIL ? 0 : ir->op2; - } - args[0] = ir->op1 == REF_NIL ? 0 : ir->op1; - lua_assert(IR(ir->op1)->o != IR_CARG); -} - -/* Reconstruct CCallInfo flags for CALLX*. */ -static uint32_t asm_callx_flags(ASMState *as, IRIns *ir) -{ - uint32_t nargs = 0; - if (ir->op1 != REF_NIL) { /* Count number of arguments first. */ - IRIns *ira = IR(ir->op1); - nargs++; - while (ira->o == IR_CARG) { nargs++; ira = IR(ira->op1); } - } -#if LJ_HASFFI - if (IR(ir->op2)->o == IR_CARG) { /* Copy calling convention info. */ - CTypeID id = (CTypeID)IR(IR(ir->op2)->op2)->i; - CType *ct = ctype_get(ctype_ctsG(J2G(as->J)), id); - nargs |= ((ct->info & CTF_VARARG) ? CCI_VARARG : 0); -#if LJ_TARGET_X86 - nargs |= (ctype_cconv(ct->info) << CCI_CC_SHIFT); -#endif - } -#endif - return (nargs | (ir->t.irt << CCI_OTSHIFT)); -} - -/* Calculate stack adjustment. */ -static int32_t asm_stack_adjust(ASMState *as) -{ - if (as->evenspill <= SPS_FIXED) - return 0; - return sps_scale(sps_align(as->evenspill)); -} - -/* Must match with hash*() in lj_tab.c. */ -static uint32_t ir_khash(IRIns *ir) -{ - uint32_t lo, hi; - if (irt_isstr(ir->t)) { - return ir_kstr(ir)->hash; - } else if (irt_isnum(ir->t)) { - lo = ir_knum(ir)->u32.lo; - hi = ir_knum(ir)->u32.hi << 1; - } else if (irt_ispri(ir->t)) { - lua_assert(!irt_isnil(ir->t)); - return irt_type(ir->t)-IRT_FALSE; - } else { - lua_assert(irt_isgcv(ir->t)); - lo = u32ptr(ir_kgc(ir)); - hi = lo + HASH_BIAS; - } - return hashrot(lo, hi); -} - -/* -- Allocations --------------------------------------------------------- */ - -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args); -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci); - -static void asm_snew(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_new]; - IRRef args[3]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* const char *str */ - args[2] = ir->op2; /* size_t len */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); -} - -static void asm_tnew(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_new1]; - IRRef args[2]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* uint32_t ahsize */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCtab * */ - asm_gencall(as, ci, args); - ra_allockreg(as, ir->op1 | (ir->op2 << 24), ra_releasetmp(as, ASMREF_TMP1)); -} - -static void asm_tdup(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_dup]; - IRRef args[2]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* const GCtab *kt */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCtab * */ - asm_gencall(as, ci, args); -} - -static void asm_gc_check(ASMState *as); - -/* Explicit GC step. */ -static void asm_gcstep(ASMState *as, IRIns *ir) -{ - IRIns *ira; - for (ira = IR(as->stopins+1); ira < ir; ira++) - if ((ira->o == IR_TNEW || ira->o == IR_TDUP || - (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI))) && - ra_used(ira)) - as->gcsteps++; - if (as->gcsteps) - asm_gc_check(as); - as->gcsteps = 0x80000000; /* Prevent implicit GC check further up. */ -} - -/* -- PHI and loop handling ----------------------------------------------- */ - -/* Break a PHI cycle by renaming to a free register (evict if needed). */ -static void asm_phi_break(ASMState *as, RegSet blocked, RegSet blockedby, - RegSet allow) -{ - RegSet candidates = blocked & allow; - if (candidates) { /* If this register file has candidates. */ - /* Note: the set for ra_pick cannot be empty, since each register file - ** has some registers never allocated to PHIs. - */ - Reg down, up = ra_pick(as, ~blocked & allow); /* Get a free register. */ - if (candidates & ~blockedby) /* Optimize shifts, else it's a cycle. */ - candidates = candidates & ~blockedby; - down = rset_picktop(candidates); /* Pick candidate PHI register. */ - ra_rename(as, down, up); /* And rename it to the free register. */ - } -} - -/* PHI register shuffling. -** -** The allocator tries hard to preserve PHI register assignments across -** the loop body. Most of the time this loop does nothing, since there -** are no register mismatches. -** -** If a register mismatch is detected and ... -** - the register is currently free: rename it. -** - the register is blocked by an invariant: restore/remat and rename it. -** - Otherwise the register is used by another PHI, so mark it as blocked. -** -** The renames are order-sensitive, so just retry the loop if a register -** is marked as blocked, but has been freed in the meantime. A cycle is -** detected if all of the blocked registers are allocated. To break the -** cycle rename one of them to a free register and retry. -** -** Note that PHI spill slots are kept in sync and don't need to be shuffled. -*/ -static void asm_phi_shuffle(ASMState *as) -{ - RegSet work; - - /* Find and resolve PHI register mismatches. */ - for (;;) { - RegSet blocked = RSET_EMPTY; - RegSet blockedby = RSET_EMPTY; - RegSet phiset = as->phiset; - while (phiset) { /* Check all left PHI operand registers. */ - Reg r = rset_pickbot(phiset); - IRIns *irl = IR(as->phireg[r]); - Reg left = irl->r; - if (r != left) { /* Mismatch? */ - if (!rset_test(as->freeset, r)) { /* PHI register blocked? */ - IRRef ref = regcost_ref(as->cost[r]); - /* Blocked by other PHI (w/reg)? */ - if (!ra_iskref(ref) && irt_ismarked(IR(ref)->t)) { - rset_set(blocked, r); - if (ra_hasreg(left)) - rset_set(blockedby, left); - left = RID_NONE; - } else { /* Otherwise grab register from invariant. */ - ra_restore(as, ref); - checkmclim(as); - } - } - if (ra_hasreg(left)) { - ra_rename(as, left, r); - checkmclim(as); - } - } - rset_clear(phiset, r); - } - if (!blocked) break; /* Finished. */ - if (!(as->freeset & blocked)) { /* Break cycles if none are free. */ - asm_phi_break(as, blocked, blockedby, RSET_GPR); - if (!LJ_SOFTFP) asm_phi_break(as, blocked, blockedby, RSET_FPR); - checkmclim(as); - } /* Else retry some more renames. */ - } - - /* Restore/remat invariants whose registers are modified inside the loop. */ -#if !LJ_SOFTFP - work = as->modset & ~(as->freeset | as->phiset) & RSET_FPR; - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } -#endif - work = as->modset & ~(as->freeset | as->phiset); - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } - - /* Allocate and save all unsaved PHI regs and clear marks. */ - work = as->phiset; - while (work) { - Reg r = rset_picktop(work); - IRRef lref = as->phireg[r]; - IRIns *ir = IR(lref); - if (ra_hasspill(ir->s)) { /* Left PHI gained a spill slot? */ - irt_clearmark(ir->t); /* Handled here, so clear marker now. */ - ra_alloc1(as, lref, RID2RSET(r)); - ra_save(as, ir, r); /* Save to spill slot inside the loop. */ - checkmclim(as); - } - rset_clear(work, r); - } -} - -/* Copy unsynced left/right PHI spill slots. Rarely needed. */ -static void asm_phi_copyspill(ASMState *as) -{ - int need = 0; - IRIns *ir; - for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) - if (ra_hasspill(ir->s) && ra_hasspill(IR(ir->op1)->s)) - need |= irt_isfp(ir->t) ? 2 : 1; /* Unsynced spill slot? */ - if ((need & 1)) { /* Copy integer spill slots. */ -#if !LJ_TARGET_X86ORX64 - Reg r = RID_TMP; -#else - Reg r = RID_RET; - if ((as->freeset & RSET_GPR)) - r = rset_pickbot((as->freeset & RSET_GPR)); - else - emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); -#endif - for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { - if (ra_hasspill(ir->s)) { - IRIns *irl = IR(ir->op1); - if (ra_hasspill(irl->s) && !irt_isfp(ir->t)) { - emit_spstore(as, irl, r, sps_scale(irl->s)); - emit_spload(as, ir, r, sps_scale(ir->s)); - checkmclim(as); - } - } - } -#if LJ_TARGET_X86ORX64 - if (!rset_test(as->freeset, r)) - emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); -#endif - } -#if !LJ_SOFTFP - if ((need & 2)) { /* Copy FP spill slots. */ -#if LJ_TARGET_X86 - Reg r = RID_XMM0; -#else - Reg r = RID_FPRET; -#endif - if ((as->freeset & RSET_FPR)) - r = rset_pickbot((as->freeset & RSET_FPR)); - if (!rset_test(as->freeset, r)) - emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); - for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { - if (ra_hasspill(ir->s)) { - IRIns *irl = IR(ir->op1); - if (ra_hasspill(irl->s) && irt_isfp(ir->t)) { - emit_spstore(as, irl, r, sps_scale(irl->s)); - emit_spload(as, ir, r, sps_scale(ir->s)); - checkmclim(as); - } - } - } - if (!rset_test(as->freeset, r)) - emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); - } -#endif -} - -/* Emit renames for left PHIs which are only spilled outside the loop. */ -static void asm_phi_fixup(ASMState *as) -{ - RegSet work = as->phiset; - while (work) { - Reg r = rset_picktop(work); - IRRef lref = as->phireg[r]; - IRIns *ir = IR(lref); - /* Left PHI gained a spill slot before the loop? */ - if (irt_ismarked(ir->t) && ra_hasspill(ir->s)) { - IRRef ren; - lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), lref, as->loopsnapno); - ren = tref_ref(lj_ir_emit(as->J)); - as->ir = as->T->ir; /* The IR may have been reallocated. */ - IR(ren)->r = (uint8_t)r; - IR(ren)->s = SPS_NONE; - } - irt_clearmark(ir->t); /* Always clear marker. */ - rset_clear(work, r); - } -} - -/* Setup right PHI reference. */ -static void asm_phi(ASMState *as, IRIns *ir) -{ - RegSet allow = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & - ~as->phiset; - RegSet afree = (as->freeset & allow); - IRIns *irl = IR(ir->op1); - IRIns *irr = IR(ir->op2); - if (ir->r == RID_SINK) /* Sink PHI. */ - return; - /* Spill slot shuffling is not implemented yet (but rarely needed). */ - if (ra_hasspill(irl->s) || ra_hasspill(irr->s)) - lj_trace_err(as->J, LJ_TRERR_NYIPHI); - /* Leave at least one register free for non-PHIs (and PHI cycle breaking). */ - if ((afree & (afree-1))) { /* Two or more free registers? */ - Reg r; - if (ra_noreg(irr->r)) { /* Get a register for the right PHI. */ - r = ra_allocref(as, ir->op2, allow); - } else { /* Duplicate right PHI, need a copy (rare). */ - r = ra_scratch(as, allow); - emit_movrr(as, irr, r, irr->r); - } - ir->r = (uint8_t)r; - rset_set(as->phiset, r); - as->phireg[r] = (IRRef1)ir->op1; - irt_setmark(irl->t); /* Marks left PHIs _with_ register. */ - if (ra_noreg(irl->r)) - ra_sethint(irl->r, r); /* Set register hint for left PHI. */ - } else { /* Otherwise allocate a spill slot. */ - /* This is overly restrictive, but it triggers only on synthetic code. */ - if (ra_hasreg(irl->r) || ra_hasreg(irr->r)) - lj_trace_err(as->J, LJ_TRERR_NYIPHI); - ra_spill(as, ir); - irr->s = ir->s; /* Set right PHI spill slot. Sync left slot later. */ - } -} - -static void asm_loop_fixup(ASMState *as); - -/* Middle part of a loop. */ -static void asm_loop(ASMState *as) -{ - MCode *mcspill; - /* LOOP is a guard, so the snapno is up to date. */ - as->loopsnapno = as->snapno; - if (as->gcsteps) - asm_gc_check(as); - /* LOOP marks the transition from the variant to the invariant part. */ - as->flagmcp = as->invmcp = NULL; - as->sectref = 0; - if (!neverfuse(as)) as->fuseref = 0; - asm_phi_shuffle(as); - mcspill = as->mcp; - asm_phi_copyspill(as); - asm_loop_fixup(as); - as->mcloop = as->mcp; - RA_DBGX((as, "===== LOOP =====")); - if (!as->realign) RA_DBG_FLUSH(); - if (as->mcp != mcspill) - emit_jmp(as, mcspill); -} - -/* -- Target-specific assembler ------------------------------------------- */ - -#if LJ_TARGET_X86ORX64 -#include "lj_asm_x86.h" -#elif LJ_TARGET_ARM -#include "lj_asm_arm.h" -#elif LJ_TARGET_PPC -#include "lj_asm_ppc.h" -#elif LJ_TARGET_MIPS -#include "lj_asm_mips.h" -#else -#error "Missing assembler for target CPU" -#endif - -/* -- Head of trace ------------------------------------------------------- */ - -/* Head of a root trace. */ -static void asm_head_root(ASMState *as) -{ - int32_t spadj; - asm_head_root_base(as); - emit_setvmstate(as, (int32_t)as->T->traceno); - spadj = asm_stack_adjust(as); - as->T->spadjust = (uint16_t)spadj; - emit_spsub(as, spadj); - /* Root traces assume a checked stack for the starting proto. */ - as->T->topslot = gcref(as->T->startpt)->pt.framesize; -} - -/* Head of a side trace. -** -** The current simplistic algorithm requires that all slots inherited -** from the parent are live in a register between pass 2 and pass 3. This -** avoids the complexity of stack slot shuffling. But of course this may -** overflow the register set in some cases and cause the dreaded error: -** "NYI: register coalescing too complex". A refined algorithm is needed. -*/ -static void asm_head_side(ASMState *as) -{ - IRRef1 sloadins[RID_MAX]; - RegSet allow = RSET_ALL; /* Inverse of all coalesced registers. */ - RegSet live = RSET_EMPTY; /* Live parent registers. */ - IRIns *irp = &as->parent->ir[REF_BASE]; /* Parent base. */ - int32_t spadj, spdelta; - int pass2 = 0; - int pass3 = 0; - IRRef i; - - allow = asm_head_side_base(as, irp, allow); - - /* Scan all parent SLOADs and collect register dependencies. */ - for (i = as->stopins; i > REF_BASE; i--) { - IRIns *ir = IR(i); - RegSP rs; - lua_assert((ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_PARENT)) || - (LJ_SOFTFP && ir->o == IR_HIOP) || ir->o == IR_PVAL); - rs = as->parentmap[i - REF_FIRST]; - if (ra_hasreg(ir->r)) { - rset_clear(allow, ir->r); - if (ra_hasspill(ir->s)) { - ra_save(as, ir, ir->r); - checkmclim(as); - } - } else if (ra_hasspill(ir->s)) { - irt_setmark(ir->t); - pass2 = 1; - } - if (ir->r == rs) { /* Coalesce matching registers right now. */ - ra_free(as, ir->r); - } else if (ra_hasspill(regsp_spill(rs))) { - if (ra_hasreg(ir->r)) - pass3 = 1; - } else if (ra_used(ir)) { - sloadins[rs] = (IRRef1)i; - rset_set(live, rs); /* Block live parent register. */ - } - } - - /* Calculate stack frame adjustment. */ - spadj = asm_stack_adjust(as); - spdelta = spadj - (int32_t)as->parent->spadjust; - if (spdelta < 0) { /* Don't shrink the stack frame. */ - spadj = (int32_t)as->parent->spadjust; - spdelta = 0; - } - as->T->spadjust = (uint16_t)spadj; - - /* Reload spilled target registers. */ - if (pass2) { - for (i = as->stopins; i > REF_BASE; i--) { - IRIns *ir = IR(i); - if (irt_ismarked(ir->t)) { - RegSet mask; - Reg r; - RegSP rs; - irt_clearmark(ir->t); - rs = as->parentmap[i - REF_FIRST]; - if (!ra_hasspill(regsp_spill(rs))) - ra_sethint(ir->r, rs); /* Hint may be gone, set it again. */ - else if (sps_scale(regsp_spill(rs))+spdelta == sps_scale(ir->s)) - continue; /* Same spill slot, do nothing. */ - mask = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & allow; - if (mask == RSET_EMPTY) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - r = ra_allocref(as, i, mask); - ra_save(as, ir, r); - rset_clear(allow, r); - if (r == rs) { /* Coalesce matching registers right now. */ - ra_free(as, r); - rset_clear(live, r); - } else if (ra_hasspill(regsp_spill(rs))) { - pass3 = 1; - } - checkmclim(as); - } - } - } - - /* Store trace number and adjust stack frame relative to the parent. */ - emit_setvmstate(as, (int32_t)as->T->traceno); - emit_spsub(as, spdelta); - -#if !LJ_TARGET_X86ORX64 - /* Restore BASE register from parent spill slot. */ - if (ra_hasspill(irp->s)) - emit_spload(as, IR(REF_BASE), IR(REF_BASE)->r, sps_scale(irp->s)); -#endif - - /* Restore target registers from parent spill slots. */ - if (pass3) { - RegSet work = ~as->freeset & RSET_ALL; - while (work) { - Reg r = rset_pickbot(work); - IRRef ref = regcost_ref(as->cost[r]); - RegSP rs = as->parentmap[ref - REF_FIRST]; - rset_clear(work, r); - if (ra_hasspill(regsp_spill(rs))) { - int32_t ofs = sps_scale(regsp_spill(rs)); - ra_free(as, r); - emit_spload(as, IR(ref), r, ofs); - checkmclim(as); - } - } - } - - /* Shuffle registers to match up target regs with parent regs. */ - for (;;) { - RegSet work; - - /* Repeatedly coalesce free live registers by moving to their target. */ - while ((work = as->freeset & live) != RSET_EMPTY) { - Reg rp = rset_pickbot(work); - IRIns *ir = IR(sloadins[rp]); - rset_clear(live, rp); - rset_clear(allow, rp); - ra_free(as, ir->r); - emit_movrr(as, ir, ir->r, rp); - checkmclim(as); - } - - /* We're done if no live registers remain. */ - if (live == RSET_EMPTY) - break; - - /* Break cycles by renaming one target to a temp. register. */ - if (live & RSET_GPR) { - RegSet tmpset = as->freeset & ~live & allow & RSET_GPR; - if (tmpset == RSET_EMPTY) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - ra_rename(as, rset_pickbot(live & RSET_GPR), rset_pickbot(tmpset)); - } - if (!LJ_SOFTFP && (live & RSET_FPR)) { - RegSet tmpset = as->freeset & ~live & allow & RSET_FPR; - if (tmpset == RSET_EMPTY) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - ra_rename(as, rset_pickbot(live & RSET_FPR), rset_pickbot(tmpset)); - } - checkmclim(as); - /* Continue with coalescing to fix up the broken cycle(s). */ - } - - /* Inherit top stack slot already checked by parent trace. */ - as->T->topslot = as->parent->topslot; - if (as->topslot > as->T->topslot) { /* Need to check for higher slot? */ -#ifdef EXITSTATE_CHECKEXIT - /* Highest exit + 1 indicates stack check. */ - ExitNo exitno = as->T->nsnap; -#else - /* Reuse the parent exit in the context of the parent trace. */ - ExitNo exitno = as->J->exitno; -#endif - as->T->topslot = (uint8_t)as->topslot; /* Remember for child traces. */ - asm_stack_check(as, as->topslot, irp, allow & RSET_GPR, exitno); - } -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Get base slot for a snapshot. */ -static BCReg asm_baseslot(ASMState *as, SnapShot *snap, int *gotframe) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - MSize n; - for (n = snap->nent; n > 0; n--) { - SnapEntry sn = map[n-1]; - if ((sn & SNAP_FRAME)) { - *gotframe = 1; - return snap_slot(sn); - } - } - return 0; -} - -/* Link to another trace. */ -static void asm_tail_link(ASMState *as) -{ - SnapNo snapno = as->T->nsnap-1; /* Last snapshot. */ - SnapShot *snap = &as->T->snap[snapno]; - int gotframe = 0; - BCReg baseslot = asm_baseslot(as, snap, &gotframe); - - as->topslot = snap->topslot; - checkmclim(as); - ra_allocref(as, REF_BASE, RID2RSET(RID_BASE)); - - if (as->T->link == 0) { - /* Setup fixed registers for exit to interpreter. */ - const BCIns *pc = snap_pc(as->T->snapmap[snap->mapofs + snap->nent]); - int32_t mres; - if (bc_op(*pc) == BC_JLOOP) { /* NYI: find a better way to do this. */ - BCIns *retpc = &traceref(as->J, bc_d(*pc))->startins; - if (bc_isret(bc_op(*retpc))) - pc = retpc; - } - ra_allockreg(as, i32ptr(J2GG(as->J)->dispatch), RID_DISPATCH); - ra_allockreg(as, i32ptr(pc), RID_LPC); - mres = (int32_t)(snap->nslots - baseslot); - switch (bc_op(*pc)) { - case BC_CALLM: case BC_CALLMT: - mres -= (int32_t)(1 + bc_a(*pc) + bc_c(*pc)); break; - case BC_RETM: mres -= (int32_t)(bc_a(*pc) + bc_d(*pc)); break; - case BC_TSETM: mres -= (int32_t)bc_a(*pc); break; - default: if (bc_op(*pc) < BC_FUNCF) mres = 0; break; - } - ra_allockreg(as, mres, RID_RET); /* Return MULTRES or 0. */ - } else if (baseslot) { - /* Save modified BASE for linking to trace with higher start frame. */ - emit_setgl(as, RID_BASE, jit_base); - } - emit_addptr(as, RID_BASE, 8*(int32_t)baseslot); - - /* Sync the interpreter state with the on-trace state. */ - asm_stack_restore(as, snap); - - /* Root traces that add frames need to check the stack at the end. */ - if (!as->parent && gotframe) - asm_stack_check(as, as->topslot, NULL, as->freeset & RSET_GPR, snapno); -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Clear reg/sp for all instructions and add register hints. */ -static void asm_setup_regsp(ASMState *as) -{ - GCtrace *T = as->T; - int sink = T->sinktags; - IRRef nins = T->nins; - IRIns *ir, *lastir; - int inloop; -#if LJ_TARGET_ARM - uint32_t rload = 0xa6402a64; -#endif - - ra_setup(as); - - /* Clear reg/sp for constants. */ - for (ir = IR(T->nk), lastir = IR(REF_BASE); ir < lastir; ir++) - ir->prev = REGSP_INIT; - - /* REF_BASE is used for implicit references to the BASE register. */ - lastir->prev = REGSP_HINT(RID_BASE); - - ir = IR(nins-1); - if (ir->o == IR_RENAME) { - do { ir--; nins--; } while (ir->o == IR_RENAME); - T->nins = nins; /* Remove any renames left over from ASM restart. */ - } - as->snaprename = nins; - as->snapref = nins; - as->snapno = T->nsnap; - - as->stopins = REF_BASE; - as->orignins = nins; - as->curins = nins; - - /* Setup register hints for parent link instructions. */ - ir = IR(REF_FIRST); - if (as->parent) { - uint16_t *p; - lastir = lj_snap_regspmap(as->parent, as->J->exitno, ir); - if (lastir - ir > LJ_MAX_JSLOTS) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - as->stopins = (IRRef)((lastir-1) - as->ir); - for (p = as->parentmap; ir < lastir; ir++) { - RegSP rs = ir->prev; - *p++ = (uint16_t)rs; /* Copy original parent RegSP to parentmap. */ - if (!ra_hasspill(regsp_spill(rs))) - ir->prev = (uint16_t)REGSP_HINT(regsp_reg(rs)); - else - ir->prev = REGSP_INIT; - } - } - - inloop = 0; - as->evenspill = SPS_FIRST; - for (lastir = IR(nins); ir < lastir; ir++) { - if (sink) { - if (ir->r == RID_SINK) - continue; - if (ir->r == RID_SUNK) { /* Revert after ASM restart. */ - ir->r = RID_SINK; - continue; - } - } - switch (ir->o) { - case IR_LOOP: - inloop = 1; - break; -#if LJ_TARGET_ARM - case IR_SLOAD: - if (!((ir->op2 & IRSLOAD_TYPECHECK) || (ir+1)->o == IR_HIOP)) - break; - /* fallthrough */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - if (!LJ_SOFTFP && irt_isnum(ir->t)) break; - ir->prev = (uint16_t)REGSP_HINT((rload & 15)); - rload = lj_ror(rload, 4); - continue; -#endif - case IR_CALLXS: { - CCallInfo ci; - ci.flags = asm_callx_flags(as, ir); - ir->prev = asm_setup_call_slots(as, ir, &ci); - if (inloop) - as->modset |= RSET_SCRATCH; - continue; - } - case IR_CALLN: case IR_CALLL: case IR_CALLS: { - const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; - ir->prev = asm_setup_call_slots(as, ir, ci); - if (inloop) - as->modset |= (ci->flags & CCI_NOFPRCLOBBER) ? - (RSET_SCRATCH & ~RSET_FPR) : RSET_SCRATCH; - continue; - } -#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) - case IR_HIOP: - switch ((ir-1)->o) { -#if LJ_SOFTFP && LJ_TARGET_ARM - case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - if (ra_hashint((ir-1)->r)) { - ir->prev = (ir-1)->prev + 1; - continue; - } - break; -#endif -#if !LJ_SOFTFP && LJ_NEED_FP64 - case IR_CONV: - if (irt_isfp((ir-1)->t)) { - ir->prev = REGSP_HINT(RID_FPRET); - continue; - } - /* fallthrough */ -#endif - case IR_CALLN: case IR_CALLXS: -#if LJ_SOFTFP - case IR_MIN: case IR_MAX: -#endif - (ir-1)->prev = REGSP_HINT(RID_RETLO); - ir->prev = REGSP_HINT(RID_RETHI); - continue; - default: - break; - } - break; -#endif -#if LJ_SOFTFP - case IR_MIN: case IR_MAX: - if ((ir+1)->o != IR_HIOP) break; - /* fallthrough */ -#endif - /* C calls evict all scratch regs and return results in RID_RET. */ - case IR_SNEW: case IR_XSNEW: case IR_NEWREF: - if (REGARG_NUMGPR < 3 && as->evenspill < 3) - as->evenspill = 3; /* lj_str_new and lj_tab_newkey need 3 args. */ - case IR_TNEW: case IR_TDUP: case IR_CNEW: case IR_CNEWI: case IR_TOSTR: - ir->prev = REGSP_HINT(RID_RET); - if (inloop) - as->modset = RSET_SCRATCH; - continue; - case IR_STRTO: case IR_OBAR: - if (inloop) - as->modset = RSET_SCRATCH; - break; -#if !LJ_TARGET_X86ORX64 && !LJ_SOFTFP - case IR_ATAN2: case IR_LDEXP: -#endif - case IR_POW: - if (!LJ_SOFTFP && irt_isnum(ir->t)) { -#if LJ_TARGET_X86ORX64 - ir->prev = REGSP_HINT(RID_XMM0); - if (inloop) - as->modset |= RSET_RANGE(RID_XMM0, RID_XMM1+1)|RID2RSET(RID_EAX); -#else - ir->prev = REGSP_HINT(RID_FPRET); - if (inloop) - as->modset |= RSET_SCRATCH; -#endif - continue; - } - /* fallthrough for integer POW */ - case IR_DIV: case IR_MOD: - if (!irt_isnum(ir->t)) { - ir->prev = REGSP_HINT(RID_RET); - if (inloop) - as->modset |= (RSET_SCRATCH & RSET_GPR); - continue; - } - break; - case IR_FPMATH: -#if LJ_TARGET_X86ORX64 - if (ir->op2 == IRFPM_EXP2) { /* May be joined to lj_vm_pow_sse. */ - ir->prev = REGSP_HINT(RID_XMM0); -#if !LJ_64 - if (as->evenspill < 4) /* Leave room for 16 byte scratch area. */ - as->evenspill = 4; -#endif - if (inloop) - as->modset |= RSET_RANGE(RID_XMM0, RID_XMM2+1)|RID2RSET(RID_EAX); - continue; - } else if (ir->op2 <= IRFPM_TRUNC && !(as->flags & JIT_F_SSE4_1)) { - ir->prev = REGSP_HINT(RID_XMM0); - if (inloop) - as->modset |= RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); - continue; - } - break; -#else - ir->prev = REGSP_HINT(RID_FPRET); - if (inloop) - as->modset |= RSET_SCRATCH; - continue; -#endif -#if LJ_TARGET_X86ORX64 - /* Non-constant shift counts need to be in RID_ECX on x86/x64. */ - case IR_BSHL: case IR_BSHR: case IR_BSAR: case IR_BROL: case IR_BROR: - if (!irref_isk(ir->op2) && !ra_hashint(IR(ir->op2)->r)) { - IR(ir->op2)->r = REGSP_HINT(RID_ECX); - if (inloop) - rset_set(as->modset, RID_ECX); - } - break; -#endif - /* Do not propagate hints across type conversions or loads. */ - case IR_TOBIT: - case IR_XLOAD: -#if !LJ_TARGET_ARM - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: -#endif - break; - case IR_CONV: - if (irt_isfp(ir->t) || (ir->op2 & IRCONV_SRCMASK) == IRT_NUM || - (ir->op2 & IRCONV_SRCMASK) == IRT_FLOAT) - break; - /* fallthrough */ - default: - /* Propagate hints across likely 'op reg, imm' or 'op reg'. */ - if (irref_isk(ir->op2) && !irref_isk(ir->op1) && - ra_hashint(regsp_reg(IR(ir->op1)->prev))) { - ir->prev = IR(ir->op1)->prev; - continue; - } - break; - } - ir->prev = REGSP_INIT; - } - if ((as->evenspill & 1)) - as->oddspill = as->evenspill++; - else - as->oddspill = 0; -} - -/* -- Assembler core ------------------------------------------------------ */ - -/* Assemble a trace. */ -void lj_asm_trace(jit_State *J, GCtrace *T) -{ - ASMState as_; - ASMState *as = &as_; - MCode *origtop; - - /* Ensure an initialized instruction beyond the last one for HIOP checks. */ - J->cur.nins = lj_ir_nextins(J); - J->cur.ir[J->cur.nins].o = IR_NOP; - - /* Setup initial state. Copy some fields to reduce indirections. */ - as->J = J; - as->T = T; - as->ir = T->ir; - as->flags = J->flags; - as->loopref = J->loopref; - as->realign = NULL; - as->loopinv = 0; - as->parent = J->parent ? traceref(J, J->parent) : NULL; - - /* Reserve MCode memory. */ - as->mctop = origtop = lj_mcode_reserve(J, &as->mcbot); - as->mcp = as->mctop; - as->mclim = as->mcbot + MCLIM_REDZONE; - asm_setup_target(as); - - do { - as->mcp = as->mctop; -#ifdef LUA_USE_ASSERT - as->mcp_prev = as->mcp; -#endif - as->curins = T->nins; - RA_DBG_START(); - RA_DBGX((as, "===== STOP =====")); - - /* General trace setup. Emit tail of trace. */ - asm_tail_prep(as); - as->mcloop = NULL; - as->flagmcp = NULL; - as->topslot = 0; - as->gcsteps = 0; - as->sectref = as->loopref; - as->fuseref = (as->flags & JIT_F_OPT_FUSE) ? as->loopref : FUSE_DISABLED; - asm_setup_regsp(as); - if (!as->loopref) - asm_tail_link(as); - - /* Assemble a trace in linear backwards order. */ - for (as->curins--; as->curins > as->stopins; as->curins--) { - IRIns *ir = IR(as->curins); - lua_assert(!(LJ_32 && irt_isint64(ir->t))); /* Handled by SPLIT. */ - if (!ra_used(ir) && !ir_sideeff(ir) && (as->flags & JIT_F_OPT_DCE)) - continue; /* Dead-code elimination can be soooo easy. */ - if (irt_isguard(ir->t)) - asm_snap_prep(as); - RA_DBG_REF(); - checkmclim(as); - asm_ir(as, ir); - } - } while (as->realign); /* Retry in case the MCode needs to be realigned. */ - - /* Emit head of trace. */ - RA_DBG_REF(); - checkmclim(as); - if (as->gcsteps > 0) { - as->curins = as->T->snap[0].ref; - asm_snap_prep(as); /* The GC check is a guard. */ - asm_gc_check(as); - } - ra_evictk(as); - if (as->parent) - asm_head_side(as); - else - asm_head_root(as); - asm_phi_fixup(as); - - RA_DBGX((as, "===== START ====")); - RA_DBG_FLUSH(); - if (as->freeset != RSET_ALL) - lj_trace_err(as->J, LJ_TRERR_BADRA); /* Ouch! Should never happen. */ - - /* Set trace entry point before fixing up tail to allow link to self. */ - T->mcode = as->mcp; - T->mcloop = as->mcloop ? (MSize)((char *)as->mcloop - (char *)as->mcp) : 0; - if (!as->loopref) - asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */ - T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp); - lj_mcode_sync(T->mcode, origtop); -} - -#undef IR - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_asm.h b/third-party/LuaJIT-2.0.2/src/lj_asm.h deleted file mode 100644 index a88e7da3c6..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_asm.h +++ /dev/null @@ -1,17 +0,0 @@ -/* -** IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_ASM_H -#define _LJ_ASM_H - -#include "lj_jit.h" - -#if LJ_HASJIT -LJ_FUNC void lj_asm_trace(jit_State *J, GCtrace *T); -LJ_FUNC void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, - MCode *target); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_asm_arm.h b/third-party/LuaJIT-2.0.2/src/lj_asm_arm.h deleted file mode 100644 index a66573c0a3..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_asm_arm.h +++ /dev/null @@ -1,2358 +0,0 @@ -/* -** ARM IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Register allocator extensions --------------------------------------- */ - -/* Allocate a register with a hint. */ -static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!ra_hashint(r) && !iscrossref(as, ref)) - ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ - r = ra_allocref(as, ref, allow); - } - ra_noweak(as, r); - return r; -} - -/* Allocate a scratch register pair. */ -static Reg ra_scratchpair(ASMState *as, RegSet allow) -{ - RegSet pick1 = as->freeset & allow; - RegSet pick2 = pick1 & (pick1 >> 1) & RSET_GPREVEN; - Reg r; - if (pick2) { - r = rset_picktop(pick2); - } else { - RegSet pick = pick1 & (allow >> 1) & RSET_GPREVEN; - if (pick) { - r = rset_picktop(pick); - ra_restore(as, regcost_ref(as->cost[r+1])); - } else { - pick = pick1 & (allow << 1) & RSET_GPRODD; - if (pick) { - r = ra_restore(as, regcost_ref(as->cost[rset_picktop(pick)-1])); - } else { - r = ra_evict(as, allow & (allow >> 1) & RSET_GPREVEN); - ra_restore(as, regcost_ref(as->cost[r+1])); - } - } - } - lua_assert(rset_test(RSET_GPREVEN, r)); - ra_modified(as, r); - ra_modified(as, r+1); - RA_DBGX((as, "scratchpair $r $r", r, r+1)); - return r; -} - -#if !LJ_SOFTFP -/* Allocate two source registers for three-operand instructions. */ -static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - Reg left = irl->r, right = irr->r; - if (ra_hasreg(left)) { - ra_noweak(as, left); - if (ra_noreg(right)) - right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); - else - ra_noweak(as, right); - } else if (ra_hasreg(right)) { - ra_noweak(as, right); - left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); - } else if (ra_hashint(right)) { - right = ra_allocref(as, ir->op2, allow); - left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); - } else { - left = ra_allocref(as, ir->op1, allow); - right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); - } - return left | (right << 8); -} -#endif - -/* -- Guard handling ------------------------------------------------------ */ - -/* Generate an exit stub group at the bottom of the reserved MCode memory. */ -static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) -{ - MCode *mxp = as->mcbot; - int i; - if (mxp + 4*4+4*EXITSTUBS_PER_GROUP >= as->mctop) - asm_mclimit(as); - /* str lr, [sp]; bl ->vm_exit_handler; .long DISPATCH_address, group. */ - *mxp++ = ARMI_STR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_LR)|ARMF_N(RID_SP); - *mxp = ARMI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)-2)&0x00ffffffu); - mxp++; - *mxp++ = (MCode)i32ptr(J2GG(as->J)->dispatch); /* DISPATCH address */ - *mxp++ = group*EXITSTUBS_PER_GROUP; - for (i = 0; i < EXITSTUBS_PER_GROUP; i++) - *mxp++ = ARMI_B|((-6-i)&0x00ffffffu); - lj_mcode_sync(as->mcbot, mxp); - lj_mcode_commitbot(as->J, mxp); - as->mcbot = mxp; - as->mclim = as->mcbot + MCLIM_REDZONE; - return mxp - EXITSTUBS_PER_GROUP; -} - -/* Setup all needed exit stubs. */ -static void asm_exitstub_setup(ASMState *as, ExitNo nexits) -{ - ExitNo i; - if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) - lj_trace_err(as->J, LJ_TRERR_SNAPOV); - for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++) - if (as->J->exitstubgroup[i] == NULL) - as->J->exitstubgroup[i] = asm_exitstub_gen(as, i); -} - -/* Emit conditional branch to exit for guard. */ -static void asm_guardcc(ASMState *as, ARMCC cc) -{ - MCode *target = exitstub_addr(as->J, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *p = ARMI_BL | ((target-p-2) & 0x00ffffffu); - emit_branch(as, ARMF_CC(ARMI_B, cc^1), p+1); - return; - } - emit_branch(as, ARMF_CC(ARMI_BL, cc), target); -} - -/* -- Operand fusion ------------------------------------------------------ */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if there's no conflicting instruction between curins and ref. */ -static int noconflict(ASMState *as, IRRef ref, IROp conflict) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - return 1; /* Ok, no conflict. */ -} - -/* Fuse the array base of colocated arrays. */ -static int32_t asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) - return (int32_t)sizeof(GCtab); - return 0; -} - -/* Fuse array/hash/upvalue reference into register+offset operand. */ -static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow, - int lim) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - if (ir->o == IR_AREF) { - if (mayfuse(as, ref)) { - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (ofs > -lim && ofs < lim) { - *ofsp = ofs; - return ra_alloc1(as, refa, allow); - } - } - } - } else if (ir->o == IR_HREFK) { - if (mayfuse(as, ref)) { - int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - if (ofs < lim) { - *ofsp = ofs; - return ra_alloc1(as, ir->op1, allow); - } - } - } else if (ir->o == IR_UREFC) { - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); - *ofsp = (ofs & 255); /* Mask out less bits to allow LDRD. */ - return ra_allock(as, (ofs & ~255), allow); - } - } - } - *ofsp = 0; - return ra_alloc1(as, ref, allow); -} - -/* Fuse m operand into arithmetic/logic instructions. */ -static uint32_t asm_fuseopm(ASMState *as, ARMIns ai, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_hasreg(ir->r)) { - ra_noweak(as, ir->r); - return ARMF_M(ir->r); - } else if (irref_isk(ref)) { - uint32_t k = emit_isk12(ai, ir->i); - if (k) - return k; - } else if (mayfuse(as, ref)) { - if (ir->o >= IR_BSHL && ir->o <= IR_BROR) { - Reg m = ra_alloc1(as, ir->op1, allow); - ARMShift sh = ir->o == IR_BSHL ? ARMSH_LSL : - ir->o == IR_BSHR ? ARMSH_LSR : - ir->o == IR_BSAR ? ARMSH_ASR : ARMSH_ROR; - if (irref_isk(ir->op2)) { - return m | ARMF_SH(sh, (IR(ir->op2)->i & 31)); - } else { - Reg s = ra_alloc1(as, ir->op2, rset_exclude(allow, m)); - return m | ARMF_RSH(sh, s); - } - } else if (ir->o == IR_ADD && ir->op1 == ir->op2) { - Reg m = ra_alloc1(as, ir->op1, allow); - return m | ARMF_SH(ARMSH_LSL, 1); - } - } - return ra_allocref(as, ref, allow); -} - -/* Fuse shifts into loads/stores. Only bother with BSHL 2 => lsl #2. */ -static IRRef asm_fuselsl2(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r) && mayfuse(as, ref) && ir->o == IR_BSHL && - irref_isk(ir->op2) && IR(ir->op2)->i == 2) - return ir->op1; - return 0; /* No fusion. */ -} - -/* Fuse XLOAD/XSTORE reference into load/store operand. */ -static void asm_fusexref(ASMState *as, ARMIns ai, Reg rd, IRRef ref, - RegSet allow, int32_t ofs) -{ - IRIns *ir = IR(ref); - Reg base; - if (ra_noreg(ir->r) && canfuse(as, ir)) { - int32_t lim = (!LJ_SOFTFP && (ai & 0x08000000)) ? 1024 : - (ai & 0x04000000) ? 4096 : 256; - if (ir->o == IR_ADD) { - int32_t ofs2; - if (irref_isk(ir->op2) && - (ofs2 = ofs + IR(ir->op2)->i) > -lim && ofs2 < lim && - (!(!LJ_SOFTFP && (ai & 0x08000000)) || !(ofs2 & 3))) { - ofs = ofs2; - ref = ir->op1; - } else if (ofs == 0 && !(!LJ_SOFTFP && (ai & 0x08000000))) { - IRRef lref = ir->op1, rref = ir->op2; - Reg rn, rm; - if ((ai & 0x04000000)) { - IRRef sref = asm_fuselsl2(as, rref); - if (sref) { - rref = sref; - ai |= ARMF_SH(ARMSH_LSL, 2); - } else if ((sref = asm_fuselsl2(as, lref)) != 0) { - lref = rref; - rref = sref; - ai |= ARMF_SH(ARMSH_LSL, 2); - } - } - rn = ra_alloc1(as, lref, allow); - rm = ra_alloc1(as, rref, rset_exclude(allow, rn)); - if ((ai & 0x04000000)) ai |= ARMI_LS_R; - emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm); - return; - } - } else if (ir->o == IR_STRREF && !(!LJ_SOFTFP && (ai & 0x08000000))) { - lua_assert(ofs == 0); - ofs = (int32_t)sizeof(GCstr); - if (irref_isk(ir->op2)) { - ofs += IR(ir->op2)->i; - ref = ir->op1; - } else if (irref_isk(ir->op1)) { - ofs += IR(ir->op1)->i; - ref = ir->op2; - } else { - /* NYI: Fuse ADD with constant. */ - Reg rn = ra_alloc1(as, ir->op1, allow); - uint32_t m = asm_fuseopm(as, 0, ir->op2, rset_exclude(allow, rn)); - if ((ai & 0x04000000)) - emit_lso(as, ai, rd, rd, ofs); - else - emit_lsox(as, ai, rd, rd, ofs); - emit_dn(as, ARMI_ADD^m, rd, rn); - return; - } - if (ofs <= -lim || ofs >= lim) { - Reg rn = ra_alloc1(as, ref, allow); - Reg rm = ra_allock(as, ofs, rset_exclude(allow, rn)); - if ((ai & 0x04000000)) ai |= ARMI_LS_R; - emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm); - return; - } - } - } - base = ra_alloc1(as, ref, allow); -#if !LJ_SOFTFP - if ((ai & 0x08000000)) - emit_vlso(as, ai, rd, base, ofs); - else -#endif - if ((ai & 0x04000000)) - emit_lso(as, ai, rd, base, ofs); - else - emit_lsox(as, ai, rd, base, ofs); -} - -#if !LJ_SOFTFP -/* Fuse to multiply-add/sub instruction. */ -static int asm_fusemadd(ASMState *as, IRIns *ir, ARMIns ai, ARMIns air) -{ - IRRef lref = ir->op1, rref = ir->op2; - IRIns *irm; - if (lref != rref && - ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && - ra_noreg(irm->r)) || - (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && - (rref = lref, ai = air, ra_noreg(irm->r))))) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg add = ra_hintalloc(as, rref, dest, RSET_FPR); - Reg right, left = ra_alloc2(as, irm, - rset_exclude(rset_exclude(RSET_FPR, dest), add)); - right = (left >> 8); left &= 255; - emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15)); - if (dest != add) emit_dm(as, ARMI_VMOV_D, (dest & 15), (add & 15)); - return 1; - } - return 0; -} -#endif - -/* -- Calls --------------------------------------------------------------- */ - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_NARGS(ci); - int32_t ofs = 0; -#if LJ_SOFTFP - Reg gpr = REGARG_FIRSTGPR; -#else - Reg gpr, fpr = REGARG_FIRSTFPR, fprodd = 0; -#endif - if ((void *)ci->func) - emit_call(as, (void *)ci->func); -#if !LJ_SOFTFP - for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) - as->cost[gpr] = REGCOST(~0u, ASMREF_L); - gpr = REGARG_FIRSTGPR; -#endif - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - IRIns *ir = IR(ref); -#if !LJ_SOFTFP - if (ref && irt_isfp(ir->t)) { - RegSet of = as->freeset; - Reg src; - if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) { - if (irt_isnum(ir->t)) { - if (fpr <= REGARG_LASTFPR) { - ra_leftov(as, fpr, ref); - fpr++; - continue; - } - } else if (fprodd) { /* Ick. */ - src = ra_alloc1(as, ref, RSET_FPR); - emit_dm(as, ARMI_VMOV_S, (fprodd & 15), (src & 15) | 0x00400000); - fprodd = 0; - continue; - } else if (fpr <= REGARG_LASTFPR) { - ra_leftov(as, fpr, ref); - fprodd = fpr++; - continue; - } - /* Workaround to protect argument GPRs from being used for remat. */ - as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); - src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */ - as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); - fprodd = 0; - goto stackfp; - } - /* Workaround to protect argument GPRs from being used for remat. */ - as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); - src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */ - as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); - if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1u; - if (gpr <= REGARG_LASTGPR) { - lua_assert(rset_test(as->freeset, gpr)); /* Must have been evicted. */ - if (irt_isnum(ir->t)) { - lua_assert(rset_test(as->freeset, gpr+1)); /* Ditto. */ - emit_dnm(as, ARMI_VMOV_RR_D, gpr, gpr+1, (src & 15)); - gpr += 2; - } else { - emit_dn(as, ARMI_VMOV_R_S, gpr, (src & 15)); - gpr++; - } - } else { - stackfp: - if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; - emit_spstore(as, ir, src, ofs); - ofs += irt_isnum(ir->t) ? 8 : 4; - } - } else -#endif - { - if (gpr <= REGARG_LASTGPR) { - lua_assert(rset_test(as->freeset, gpr)); /* Must have been evicted. */ - if (ref) ra_leftov(as, gpr, ref); - gpr++; - } else { - if (ref) { - Reg r = ra_alloc1(as, ref, RSET_GPR); - emit_spstore(as, ir, r, ofs); - } - ofs += 4; - } - } - } -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP); - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - lua_assert(!irt_ispri(ir->t)); - if (!LJ_SOFTFP && irt_isfp(ir->t)) { - if (LJ_ABI_SOFTFP || (ci->flags & (CCI_CASTU64|CCI_VARARG))) { - Reg dest = (ra_dest(as, ir, RSET_FPR) & 15); - if (irt_isnum(ir->t)) - emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, dest); - else - emit_dn(as, ARMI_VMOV_S_R, RID_RET, dest); - } else { - ra_destreg(as, ir, RID_FPRET); - } - } else if (hiop) { - ra_destpair(as, ir); - } else { - ra_destreg(as, ir, RID_RET); - } - } - UNUSED(ci); -} - -static void asm_call(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX]; - const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; - asm_collectargs(as, ir, ci, args); - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - if (irref_isk(func)) { /* Call to constant address. */ - ci.func = (ASMFunction)(void *)(irf->i); - } else { /* Need a non-argument register for indirect calls. */ - Reg freg = ra_alloc1(as, func, RSET_RANGE(RID_R4, RID_R12+1)); - emit_m(as, ARMI_BLXr, freg); - ci.func = (ASMFunction)(void *)0; - } - asm_gencall(as, &ci, args); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - /* Need to force a spill on REF_BASE now to update the stack slot. */ - emit_lso(as, ARMI_STR, base, RID_SP, ra_spill(as, IR(REF_BASE))); - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guardcc(as, CC_NE); - emit_nm(as, ARMI_CMP, RID_TMP, - ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); - emit_lso(as, ARMI_LDR, RID_TMP, base, -4); -} - -/* -- Type conversions ---------------------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_guardcc(as, CC_NE); - emit_d(as, ARMI_VMRS, 0); - emit_dm(as, ARMI_VCMP_D, (tmp & 15), (left & 15)); - emit_dm(as, ARMI_VCVT_F64_S32, (tmp & 15), (tmp & 15)); - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (left & 15)); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_FPR; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, allow); - Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); - Reg tmp = ra_scratch(as, rset_clear(allow, right)); - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - emit_dnm(as, ARMI_VADD_D, (tmp & 15), (left & 15), (right & 15)); -} -#endif - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if !LJ_SOFTFP - int stfp = (st == IRT_NUM || st == IRT_FLOAT); -#endif - IRRef lref = ir->op1; - /* 64 bit integer conversions are handled by SPLIT. */ - lua_assert(!irt_isint64(ir->t) && !(st == IRT_I64 || st == IRT_U64)); -#if LJ_SOFTFP - /* FP conversions are handled by SPLIT. */ - lua_assert(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT)); - /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */ -#else - lua_assert(irt_type(ir->t) != st); - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - emit_dm(as, st == IRT_NUM ? ARMI_VCVT_F32_F64 : ARMI_VCVT_F64_F32, - (dest & 15), (ra_alloc1(as, lref, RSET_FPR) & 15)); - } else { /* Integer to FP conversion. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - ARMIns ai = irt_isfloat(ir->t) ? - (st == IRT_INT ? ARMI_VCVT_F32_S32 : ARMI_VCVT_F32_U32) : - (st == IRT_INT ? ARMI_VCVT_F64_S32 : ARMI_VCVT_F64_U32); - emit_dm(as, ai, (dest & 15), (dest & 15)); - emit_dn(as, ARMI_VMOV_S_R, left, (dest & 15)); - } - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lua_assert(irt_isint(ir->t) && st == IRT_NUM); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, lref, RSET_FPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - ARMIns ai; - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - ai = irt_isint(ir->t) ? - (st == IRT_NUM ? ARMI_VCVT_S32_F64 : ARMI_VCVT_S32_F32) : - (st == IRT_NUM ? ARMI_VCVT_U32_F64 : ARMI_VCVT_U32_F32); - emit_dm(as, ai, (tmp & 15), (left & 15)); - } - } else -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); - if ((as->flags & JIT_F_ARMV6)) { - ARMIns ai = st == IRT_I8 ? ARMI_SXTB : - st == IRT_U8 ? ARMI_UXTB : - st == IRT_I16 ? ARMI_SXTH : ARMI_UXTH; - emit_dm(as, ai, dest, left); - } else if (st == IRT_U8) { - emit_dn(as, ARMI_AND|ARMI_K12|255, dest, left); - } else { - uint32_t shift = st == IRT_I8 ? 24 : 16; - ARMShift sh = st == IRT_U16 ? ARMSH_LSR : ARMSH_ASR; - emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, RID_TMP); - emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_LSL, shift), RID_TMP, left); - } - } else { /* Handle 32/32 bit no-op (cast). */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } -} - -#if !LJ_SOFTFP && LJ_HASFFI -static void asm_conv64(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); - IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); - IRCallID id; - CCallInfo ci; - IRRef args[2]; - args[0] = (ir-1)->op1; - args[1] = ir->op1; - if (st == IRT_NUM || st == IRT_FLOAT) { - id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); - ir--; - } else { - id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); - } - ci = lj_ir_callinfo[id]; -#if !LJ_ABI_SOFTFP - ci.flags |= CCI_VARARG; /* These calls don't use the hard-float ABI! */ -#endif - asm_setupresult(as, ir, &ci); - asm_gencall(as, &ci, args); -} -#endif - -static void asm_strto(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - Reg rlo = 0, rhi = 0, tmp; - int destused = ra_used(ir); - int32_t ofs = 0; - ra_evictset(as, RSET_SCRATCH); -#if LJ_SOFTFP - if (destused) { - if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) && - (ir->s & 1) == 0 && ir->s + 1 == (ir+1)->s) { - int i; - for (i = 0; i < 2; i++) { - Reg r = (ir+i)->r; - if (ra_hasreg(r)) { - ra_free(as, r); - ra_modified(as, r); - emit_spload(as, ir+i, r, sps_scale((ir+i)->s)); - } - } - ofs = sps_scale(ir->s); - destused = 0; - } else { - rhi = ra_dest(as, ir+1, RSET_GPR); - rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi)); - } - } - asm_guardcc(as, CC_EQ); - if (destused) { - emit_lso(as, ARMI_LDR, rhi, RID_SP, 4); - emit_lso(as, ARMI_LDR, rlo, RID_SP, 0); - } -#else - UNUSED(rhi); - if (destused) { - if (ra_hasspill(ir->s)) { - ofs = sps_scale(ir->s); - destused = 0; - if (ra_hasreg(ir->r)) { - ra_free(as, ir->r); - ra_modified(as, ir->r); - emit_spload(as, ir, ir->r, ofs); - } - } else { - rlo = ra_dest(as, ir, RSET_FPR); - } - } - asm_guardcc(as, CC_EQ); - if (destused) - emit_vlso(as, ARMI_VLDR_D, rlo, RID_SP, 0); -#endif - emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - tmp = ra_releasetmp(as, ASMREF_TMP1); - if (ofs == 0) - emit_dm(as, ARMI_MOV, tmp, RID_SP); - else - emit_opk(as, ARMI_ADD, tmp, RID_SP, ofs, RSET_GPR); -} - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) -{ - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if (irref_isk(ref)) { - /* Use the number constant itself as a TValue. */ - ra_allockreg(as, i32ptr(ir_knum(ir)), dest); - } else { -#if LJ_SOFTFP - lua_assert(0); -#else - /* Otherwise force a spill and use the spill slot. */ - emit_opk(as, ARMI_ADD, dest, RID_SP, ra_spill(as, ir), RSET_GPR); -#endif - } - } else { - /* Otherwise use [sp] and [sp+4] to hold the TValue. */ - RegSet allow = rset_exclude(RSET_GPR, dest); - Reg type; - emit_dm(as, ARMI_MOV, dest, RID_SP); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, allow); - emit_lso(as, ARMI_STR, src, RID_SP, 0); - } - if ((ir+1)->o == IR_HIOP) - type = ra_alloc1(as, ref+1, allow); - else - type = ra_allock(as, irt_toitype(ir->t), allow); - emit_lso(as, ARMI_STR, type, RID_SP, 4); - } -} - -static void asm_tostr(ASMState *as, IRIns *ir) -{ - IRRef args[2]; - args[0] = ASMREF_L; - as->gcsteps++; - if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; - args[1] = ASMREF_TMP1; /* const lua_Number * */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); - } else { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; - args[1] = ir->op1; /* int32_t k */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - } -} - -/* -- Memory references --------------------------------------------------- */ - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx, base; - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - uint32_t k = emit_isk12(ARMI_ADD, ofs + 8*IR(ir->op2)->i); - if (k) { - base = ra_alloc1(as, refa, RSET_GPR); - emit_dn(as, ARMI_ADD^k, dest, base); - return; - } - } - base = ra_alloc1(as, ir->op1, RSET_GPR); - idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, base, idx); -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir, IROp merge) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = 0, keyhi = 0, keynumhi = RID_NONE, tmp = RID_TMP; - IRRef refkey = ir->op2; - IRIns *irkey = IR(refkey); - IRType1 kt = irkey->t; - int32_t k = 0, khi = emit_isk12(ARMI_CMP, irt_toitype(kt)); - uint32_t khash; - MCLabel l_end, l_loop; - rset_clear(allow, tab); - if (!irref_isk(refkey) || irt_isstr(kt)) { -#if LJ_SOFTFP - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - if (irkey[1].o == IR_HIOP) { - if (ra_hasreg((irkey+1)->r)) { - keynumhi = (irkey+1)->r; - keyhi = RID_TMP; - ra_noweak(as, keynumhi); - } else { - keyhi = keynumhi = ra_allocref(as, refkey+1, allow); - } - rset_clear(allow, keynumhi); - khi = 0; - } -#else - if (irt_isnum(kt)) { - key = ra_scratch(as, allow); - rset_clear(allow, key); - keyhi = keynumhi = ra_scratch(as, allow); - rset_clear(allow, keyhi); - khi = 0; - } else { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } -#endif - } else if (irt_isnum(kt)) { - int32_t val = (int32_t)ir_knum(irkey)->u32.lo; - k = emit_isk12(ARMI_CMP, val); - if (!k) { - key = ra_allock(as, val, allow); - rset_clear(allow, key); - } - val = (int32_t)ir_knum(irkey)->u32.hi; - khi = emit_isk12(ARMI_CMP, val); - if (!khi) { - keyhi = ra_allock(as, val, allow); - rset_clear(allow, keyhi); - } - } else if (!irt_ispri(kt)) { - k = emit_isk12(ARMI_CMP, irkey->i); - if (!k) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } - } - if (!irt_ispri(kt)) - tmp = ra_scratchpair(as, allow); - - /* Key not found in chain: jump to exit (if merged) or load niltv. */ - l_end = emit_label(as); - as->invmcp = NULL; - if (merge == IR_NE) - asm_guardcc(as, CC_AL); - else if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - - /* Follow hash chain until the end. */ - l_loop = --as->mcp; - emit_n(as, ARMI_CMP|ARMI_K12|0, dest); - emit_lso(as, ARMI_LDR, dest, dest, (int32_t)offsetof(Node, next)); - - /* Type and value comparison. */ - if (merge == IR_EQ) - asm_guardcc(as, CC_EQ); - else - emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); - if (!irt_ispri(kt)) { - emit_nm(as, ARMF_CC(ARMI_CMP, CC_EQ)^k, tmp, key); - emit_nm(as, ARMI_CMP^khi, tmp+1, keyhi); - emit_lsox(as, ARMI_LDRD, tmp, dest, (int32_t)offsetof(Node, key)); - } else { - emit_n(as, ARMI_CMP^khi, tmp); - emit_lso(as, ARMI_LDR, tmp, dest, (int32_t)offsetof(Node, key.it)); - } - *l_loop = ARMF_CC(ARMI_B, CC_NE) | ((as->mcp-l_loop-2) & 0x00ffffffu); - - /* Load main position relative to tab->node into dest. */ - khash = irref_isk(refkey) ? ir_khash(irkey) : 1; - if (khash == 0) { - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - } else { - emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, dest, tmp); - emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 1), tmp, tmp, tmp); - if (irt_isstr(kt)) { /* Fetch of str->hash is cheaper than ra_allock. */ - emit_dnm(as, ARMI_AND, tmp, tmp+1, RID_TMP); - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_lso(as, ARMI_LDR, tmp+1, key, (int32_t)offsetof(GCstr, hash)); - emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); - } else if (irref_isk(refkey)) { - emit_opk(as, ARMI_AND, tmp, RID_TMP, (int32_t)khash, - rset_exclude(rset_exclude(RSET_GPR, tab), dest)); - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); - } else { /* Must match with hash*() in lj_tab.c. */ - if (ra_hasreg(keynumhi)) { /* Canonicalize +-0.0 to 0.0. */ - if (keyhi == RID_TMP) - emit_dm(as, ARMF_CC(ARMI_MOV, CC_NE), keyhi, keynumhi); - emit_d(as, ARMF_CC(ARMI_MOV, CC_EQ)|ARMI_K12|0, keyhi); - } - emit_dnm(as, ARMI_AND, tmp, tmp, RID_TMP); - emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT3), tmp, tmp, tmp+1); - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 32-((HASH_ROT2+HASH_ROT1)&31)), - tmp, tmp+1, tmp); - emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); - emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT1), tmp+1, tmp+1, tmp); - if (ra_hasreg(keynumhi)) { - emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); - emit_dnm(as, ARMI_ORR|ARMI_S, RID_TMP, tmp, key); /* Test for +-0.0. */ - emit_dnm(as, ARMI_ADD, tmp, keynumhi, keynumhi); -#if !LJ_SOFTFP - emit_dnm(as, ARMI_VMOV_RR_D, key, keynumhi, - (ra_alloc1(as, refkey, RSET_FPR) & 15)); -#endif - } else { - emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); - emit_opk(as, ARMI_ADD, tmp, key, (int32_t)HASH_BIAS, - rset_exclude(rset_exclude(RSET_GPR, tab), key)); - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - int32_t kofs = ofs + (int32_t)offsetof(Node, key); - Reg dest = (ra_used(ir) || ofs > 4095) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); - Reg key = RID_NONE, type = RID_TMP, idx = node; - RegSet allow = rset_exclude(RSET_GPR, node); - lua_assert(ofs % sizeof(Node) == 0); - if (ofs > 4095) { - idx = dest; - rset_clear(allow, dest); - kofs = (int32_t)offsetof(Node, key); - } else if (ra_hasreg(dest)) { - emit_opk(as, ARMI_ADD, dest, node, ofs, allow); - } - asm_guardcc(as, CC_NE); - if (!irt_ispri(irkey->t)) { - RegSet even = (as->freeset & allow); - even = even & (even >> 1) & RSET_GPREVEN; - if (even) { - key = ra_scratch(as, even); - if (rset_test(as->freeset, key+1)) { - type = key+1; - ra_modified(as, type); - } - } else { - key = ra_scratch(as, allow); - } - rset_clear(allow, key); - } - rset_clear(allow, type); - if (irt_isnum(irkey->t)) { - emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, type, - (int32_t)ir_knum(irkey)->u32.hi, allow); - emit_opk(as, ARMI_CMP, 0, key, - (int32_t)ir_knum(irkey)->u32.lo, allow); - } else { - if (ra_hasreg(key)) - emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, key, irkey->i, allow); - emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype(irkey->t), type); - } - emit_lso(as, ARMI_LDR, type, idx, kofs+4); - if (ra_hasreg(key)) emit_lso(as, ARMI_LDR, key, idx, kofs); - if (ofs > 4095) - emit_opk(as, ARMI_ADD, dest, node, ofs, RSET_GPR); -} - -static void asm_newref(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; - IRRef args[3]; - if (ir->r == RID_SINK) - return; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* GCtab *t */ - args[2] = ASMREF_TMP1; /* cTValue *key */ - asm_setupresult(as, ir, ci); /* TValue * */ - asm_gencall(as, ci, args); - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - /* NYI: Check that UREFO is still open and not aliasing a slot. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_lsptr(as, ARMI_LDR, dest, v); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - asm_guardcc(as, CC_NE); - emit_n(as, ARMI_CMP|ARMI_K12|1, RID_TMP); - emit_opk(as, ARMI_ADD, dest, uv, - (int32_t)offsetof(GCupval, tv), RSET_GPR); - emit_lso(as, ARMI_LDRB, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); - } else { - emit_lso(as, ARMI_LDR, dest, uv, (int32_t)offsetof(GCupval, v)); - } - emit_lso(as, ARMI_LDR, uv, func, - (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - UNUSED(as); UNUSED(ir); - lua_assert(!ra_used(ir)); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - IRRef ref = ir->op2, refk = ir->op1; - Reg r; - if (irref_isk(ref)) { - IRRef tmp = refk; refk = ref; ref = tmp; - } else if (!irref_isk(refk)) { - uint32_t k, m = ARMI_K12|sizeof(GCstr); - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - IRIns *irr = IR(ir->op2); - if (ra_hasreg(irr->r)) { - ra_noweak(as, irr->r); - right = irr->r; - } else if (mayfuse(as, irr->op2) && - irr->o == IR_ADD && irref_isk(irr->op2) && - (k = emit_isk12(ARMI_ADD, - (int32_t)sizeof(GCstr) + IR(irr->op2)->i))) { - m = k; - right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); - } else { - right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_dn(as, ARMI_ADD^m, dest, dest); - emit_dnm(as, ARMI_ADD, dest, left, right); - return; - } - r = ra_alloc1(as, ref, RSET_GPR); - emit_opk(as, ARMI_ADD, dest, r, - sizeof(GCstr) + IR(refk)->i, rset_exclude(RSET_GPR, r)); -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static ARMIns asm_fxloadins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: return ARMI_LDRSB; - case IRT_U8: return ARMI_LDRB; - case IRT_I16: return ARMI_LDRSH; - case IRT_U16: return ARMI_LDRH; - case IRT_NUM: lua_assert(!LJ_SOFTFP); return ARMI_VLDR_D; - case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VLDR_S; - default: return ARMI_LDR; - } -} - -static ARMIns asm_fxstoreins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: return ARMI_STRB; - case IRT_I16: case IRT_U16: return ARMI_STRH; - case IRT_NUM: lua_assert(!LJ_SOFTFP); return ARMI_VSTR_D; - case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VSTR_S; - default: return ARMI_STR; - } -} - -static void asm_fload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx = ra_alloc1(as, ir->op1, RSET_GPR); - ARMIns ai = asm_fxloadins(ir); - int32_t ofs; - if (ir->op2 == IRFL_TAB_ARRAY) { - ofs = asm_fuseabase(as, ir->op1); - if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ - emit_dn(as, ARMI_ADD|ARMI_K12|ofs, dest, idx); - return; - } - } - ofs = field_ofs[ir->op2]; - if ((ai & 0x04000000)) - emit_lso(as, ai, dest, idx, ofs); - else - emit_lsox(as, ai, dest, idx, ofs); -} - -static void asm_fstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, RSET_GPR); - IRIns *irf = IR(ir->op1); - Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); - int32_t ofs = field_ofs[irf->op2]; - ARMIns ai = asm_fxstoreins(ir); - if ((ai & 0x04000000)) - emit_lso(as, ai, src, idx, ofs); - else - emit_lsox(as, ai, src, idx, ofs); - } -} - -static void asm_xload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); - asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0); -} - -static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, - rset_exclude(RSET_GPR, src), ofs); - } -} - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); - IRType t = hiop ? IRT_NUM : irt_type(ir->t); - Reg dest = RID_NONE, type = RID_NONE, idx; - RegSet allow = RSET_GPR; - int32_t ofs = 0; - if (hiop && ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } - if (ra_used(ir)) { - lua_assert((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) || - irt_isint(ir->t) || irt_isaddr(ir->t)); - dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow); - rset_clear(allow, dest); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow, - (!LJ_SOFTFP && t == IRT_NUM) ? 1024 : 4096); - if (!hiop || type == RID_NONE) { - rset_clear(allow, idx); - if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && - rset_test((as->freeset & allow), dest+1)) { - type = dest+1; - ra_modified(as, type); - } else { - type = RID_TMP; - } - } - asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); - emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); - if (ra_hasreg(dest)) { -#if !LJ_SOFTFP - if (t == IRT_NUM) - emit_vlso(as, ARMI_VLDR_D, dest, idx, ofs); - else -#endif - emit_lso(as, ARMI_LDR, dest, idx, ofs); - } - emit_lso(as, ARMI_LDR, type, idx, ofs+4); -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - RegSet allow = RSET_GPR; - Reg idx, src = RID_NONE, type = RID_NONE; - int32_t ofs = 0; -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - src = ra_alloc1(as, ir->op2, RSET_FPR); - idx = asm_fuseahuref(as, ir->op1, &ofs, allow, 1024); - emit_vlso(as, ARMI_VSTR_D, src, idx, ofs); - } else -#endif - { - int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); - if (!irt_ispri(ir->t)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - } - if (hiop) - type = ra_alloc1(as, (ir+1)->op2, allow); - else - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type), 4096); - if (ra_hasreg(src)) emit_lso(as, ARMI_STR, src, idx, ofs); - emit_lso(as, ARMI_STR, type, idx, ofs+4); - } - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); - int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); - IRType t = hiop ? IRT_NUM : irt_type(ir->t); - Reg dest = RID_NONE, type = RID_NONE, base; - RegSet allow = RSET_GPR; - lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ - lua_assert(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK)); -#if LJ_SOFTFP - lua_assert(!(ir->op2 & IRSLOAD_CONVERT)); /* Handled by LJ_SOFTFP SPLIT. */ - if (hiop && ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } -#else - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(ir->t) && t == IRT_INT) { - dest = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, dest); - t = IRT_NUM; /* Continue with a regular number type check. */ - } else -#endif - if (ra_used(ir)) { - lua_assert((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) || - irt_isint(ir->t) || irt_isaddr(ir->t)); - dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow); - rset_clear(allow, dest); - base = ra_alloc1(as, REF_BASE, allow); - if ((ir->op2 & IRSLOAD_CONVERT)) { - if (t == IRT_INT) { - Reg tmp = ra_scratch(as, RSET_FPR); - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (tmp & 15)); - dest = tmp; - t = IRT_NUM; /* Check for original type. */ - } else { - Reg tmp = ra_scratch(as, RSET_GPR); - emit_dm(as, ARMI_VCVT_F64_S32, (dest & 15), (dest & 15)); - emit_dn(as, ARMI_VMOV_S_R, tmp, (dest & 15)); - dest = tmp; - t = IRT_INT; /* Check for original type. */ - } - } - goto dotypecheck; - } - base = ra_alloc1(as, REF_BASE, allow); -dotypecheck: - rset_clear(allow, base); - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - if (ra_noreg(type)) { - if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && - rset_test((as->freeset & allow), dest+1)) { - type = dest+1; - ra_modified(as, type); - } else { - type = RID_TMP; - } - } - asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); - emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); - } - if (ra_hasreg(dest)) { -#if !LJ_SOFTFP - if (t == IRT_NUM) { - if (ofs < 1024) { - emit_vlso(as, ARMI_VLDR_D, dest, base, ofs); - } else { - if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4); - emit_vlso(as, ARMI_VLDR_D, dest, RID_TMP, 0); - emit_opk(as, ARMI_ADD, RID_TMP, base, ofs, allow); - return; - } - } else -#endif - emit_lso(as, ARMI_LDR, dest, base, ofs); - } - if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4); -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; - CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? - lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[2]; - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - RegSet drop = RSET_SCRATCH; - lua_assert(sz != CTSIZE_INVALID); - - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - as->gcsteps++; - - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - if (ra_used(ir)) - ra_destreg(as, ir, RID_RET); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - int32_t ofs = sizeof(GCcdata); - lua_assert(sz == 4 || sz == 8); - if (sz == 8) { - ofs += 4; ir++; - lua_assert(ir->o == IR_HIOP); - } - for (;;) { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_lso(as, ARMI_STR, r, RID_RET, ofs); - rset_clear(allow, r); - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; ir--; - } - } - /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ - { - uint32_t k = emit_isk12(ARMI_MOV, ctypeid); - Reg r = k ? RID_R1 : ra_allock(as, ctypeid, allow); - emit_lso(as, ARMI_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct)); - emit_lsox(as, ARMI_STRH, r, RID_RET, offsetof(GCcdata, ctypeid)); - emit_d(as, ARMI_MOV|ARMI_K12|~LJ_TCDATA, RID_TMP); - if (k) emit_d(as, ARMI_MOV^k, RID_R1); - } - asm_gencall(as, ci, args); - ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), - ra_releasetmp(as, ASMREF_TMP1)); -} -#else -#define asm_cnew(as, ir) ((void)0) -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - Reg gr = ra_allock(as, i32ptr(J2G(as->J)), - rset_exclude(rset_exclude(RSET_GPR, tab), link)); - Reg mark = RID_TMP; - MCLabel l_end = emit_label(as); - emit_lso(as, ARMI_STR, link, tab, (int32_t)offsetof(GCtab, gclist)); - emit_lso(as, ARMI_STRB, mark, tab, (int32_t)offsetof(GCtab, marked)); - emit_lso(as, ARMI_STR, tab, gr, - (int32_t)offsetof(global_State, gc.grayagain)); - emit_dn(as, ARMI_BIC|ARMI_K12|LJ_GC_BLACK, mark, mark); - emit_lso(as, ARMI_LDR, link, gr, - (int32_t)offsetof(global_State, gc.grayagain)); - emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); - emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_BLACK, mark); - emit_lso(as, ARMI_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj, val, tmp; - /* No need for other object barriers (yet). */ - lua_assert(IR(ir->op1)->o == IR_UREFC); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - if ((l_end[-1] >> 28) == CC_AL) - l_end[-1] = ARMF_CC(l_end[-1], CC_NE); - else - emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); - ra_allockreg(as, i32ptr(J2G(as->J)), ra_releasetmp(as, ASMREF_TMP1)); - obj = IR(ir->op1)->r; - tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); - emit_n(as, ARMF_CC(ARMI_TST, CC_NE)|ARMI_K12|LJ_GC_BLACK, tmp); - emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_WHITES, RID_TMP); - val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); - emit_lso(as, ARMI_LDRB, tmp, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); - emit_lso(as, ARMI_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked)); -} - -/* -- Arithmetic and logic operations ------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_fparith(ASMState *as, IRIns *ir, ARMIns ai) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15)); -} - -static void asm_fpunary(ASMState *as, IRIns *ir, ARMIns ai) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); - emit_dm(as, ai, (dest & 15), (left & 15)); -} - -static int asm_fpjoin_pow(ASMState *as, IRIns *ir) -{ - IRIns *irp = IR(ir->op1); - if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { - IRIns *irpp = IR(irp->op1); - if (irpp == ir-2 && irpp->o == IR_FPMATH && - irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; - IRRef args[2]; - args[0] = irpp->op1; - args[1] = irp->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); - return 1; - } - } - return 0; -} -#endif - -static int asm_swapops(ASMState *as, IRRef lref, IRRef rref) -{ - IRIns *ir; - if (irref_isk(rref)) - return 0; /* Don't swap constants to the left. */ - if (irref_isk(lref)) - return 1; /* But swap constants to the right. */ - ir = IR(rref); - if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || - (ir->o == IR_ADD && ir->op1 == ir->op2)) - return 0; /* Don't swap fusable operands to the left. */ - ir = IR(lref); - if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || - (ir->o == IR_ADD && ir->op1 == ir->op2)) - return 1; /* But swap fusable operands to the right. */ - return 0; /* Otherwise don't swap. */ -} - -static void asm_intop(ASMState *as, IRIns *ir, ARMIns ai) -{ - IRRef lref = ir->op1, rref = ir->op2; - Reg left, dest = ra_dest(as, ir, RSET_GPR); - uint32_t m; - if (asm_swapops(as, lref, rref)) { - IRRef tmp = lref; lref = rref; rref = tmp; - if ((ai & ~ARMI_S) == ARMI_SUB || (ai & ~ARMI_S) == ARMI_SBC) - ai ^= (ARMI_SUB^ARMI_RSB); - } - left = ra_hintalloc(as, lref, dest, RSET_GPR); - m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left)); - if (irt_isguard(ir->t)) { /* For IR_ADDOV etc. */ - asm_guardcc(as, CC_VS); - ai |= ARMI_S; - } - emit_dn(as, ai^m, dest, left); -} - -static void asm_intop_s(ASMState *as, IRIns *ir, ARMIns ai) -{ - if (as->flagmcp == as->mcp) { /* Drop cmp r, #0. */ - as->flagmcp = NULL; - as->mcp++; - ai |= ARMI_S; - } - asm_intop(as, ir, ai); -} - -static void asm_bitop(ASMState *as, IRIns *ir, ARMIns ai) -{ - if (as->flagmcp == as->mcp) { /* Try to drop cmp r, #0. */ - uint32_t cc = (as->mcp[1] >> 28); - as->flagmcp = NULL; - if (cc <= CC_NE) { - as->mcp++; - ai |= ARMI_S; - } else if (cc == CC_GE) { - *++as->mcp ^= ((CC_GE^CC_PL) << 28); - ai |= ARMI_S; - } else if (cc == CC_LT) { - *++as->mcp ^= ((CC_LT^CC_MI) << 28); - ai |= ARMI_S; - } /* else: other conds don't work with bit ops. */ - } - if (ir->op2 == 0) { - Reg dest = ra_dest(as, ir, RSET_GPR); - uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR); - emit_d(as, ai^m, dest); - } else { - /* NYI: Turn BAND !k12 into uxtb, uxth or bfc or shl+shr. */ - asm_intop(as, ir, ai); - } -} - -static void asm_intneg(ASMState *as, IRIns *ir, ARMIns ai) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_dn(as, ai|ARMI_K12|0, dest, left); -} - -/* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */ -static void asm_intmul(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, dest)); - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - Reg tmp = RID_NONE; - /* ARMv5 restriction: dest != left and dest_hi != left. */ - if (dest == left && left != right) { left = right; right = dest; } - if (irt_isguard(ir->t)) { /* IR_MULOV */ - if (!(as->flags & JIT_F_ARMV6) && dest == left) - tmp = left = ra_scratch(as, rset_exclude(RSET_FPR, left)); - asm_guardcc(as, CC_NE); - emit_nm(as, ARMI_TEQ|ARMF_SH(ARMSH_ASR, 31), RID_TMP, dest); - emit_dnm(as, ARMI_SMULL|ARMF_S(right), dest, RID_TMP, left); - } else { - if (!(as->flags & JIT_F_ARMV6) && dest == left) tmp = left = RID_TMP; - emit_nm(as, ARMI_MUL|ARMF_S(right), dest, left); - } - /* Only need this for the dest == left == right case. */ - if (ra_hasreg(tmp)) emit_dm(as, ARMI_MOV, tmp, right); -} - -static void asm_add(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, ARMI_VMLA_D, ARMI_VMLA_D)) - asm_fparith(as, ir, ARMI_VADD_D); - return; - } -#endif - asm_intop_s(as, ir, ARMI_ADD); -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, ARMI_VNMLS_D, ARMI_VMLS_D)) - asm_fparith(as, ir, ARMI_VSUB_D); - return; - } -#endif - asm_intop_s(as, ir, ARMI_SUB); -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, ARMI_VMUL_D); - return; - } -#endif - asm_intmul(as, ir); -} - -static void asm_neg(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - asm_fpunary(as, ir, ARMI_VNEG_D); - return; - } -#endif - asm_intneg(as, ir, ARMI_RSB); -} - -static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) -{ - const CCallInfo *ci = &lj_ir_callinfo[id]; - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -#if !LJ_SOFTFP -static void asm_callround(ASMState *as, IRIns *ir, int id) -{ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RID2RSET(RID_R0)|RID2RSET(RID_R1)|RID2RSET(RID_R2)| - RID2RSET(RID_R3)|RID2RSET(RID_R12); - RegSet of; - Reg dest, src; - ra_evictset(as, drop); - dest = ra_dest(as, ir, RSET_FPR); - emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, (dest & 15)); - emit_call(as, id == IRFPM_FLOOR ? (void *)lj_vm_floor_sf : - id == IRFPM_CEIL ? (void *)lj_vm_ceil_sf : - (void *)lj_vm_trunc_sf); - /* Workaround to protect argument GPRs from being used for remat. */ - of = as->freeset; - as->freeset &= ~RSET_RANGE(RID_R0, RID_R1+1); - as->cost[RID_R0] = as->cost[RID_R1] = REGCOST(~0u, ASMREF_L); - src = ra_alloc1(as, ir->op1, RSET_FPR); /* May alloc GPR to remat FPR. */ - as->freeset |= (of & RSET_RANGE(RID_R0, RID_R1+1)); - emit_dnm(as, ARMI_VMOV_RR_D, RID_R0, RID_R1, (src & 15)); -} -#endif - -static void asm_bitswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - if ((as->flags & JIT_F_ARMV6)) { - emit_dm(as, ARMI_REV, dest, left); - } else { - Reg tmp2 = dest; - if (tmp2 == left) - tmp2 = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, dest), left)); - emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_LSR, 8), dest, tmp2, RID_TMP); - emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_ROR, 8), tmp2, left); - emit_dn(as, ARMI_BIC|ARMI_K12|256*8|255, RID_TMP, RID_TMP); - emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 16), RID_TMP, left, left); - } -} - -static void asm_bitshift(ASMState *as, IRIns *ir, ARMShift sh) -{ - if (irref_isk(ir->op2)) { /* Constant shifts. */ - /* NYI: Turn SHL+SHR or BAND+SHR into uxtb, uxth or ubfx. */ - /* NYI: Turn SHL+ASR into sxtb, sxth or sbfx. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - int32_t shift = (IR(ir->op2)->i & 31); - emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, left); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dm(as, ARMI_MOV|ARMF_RSH(sh, right), dest, left); - } -} - -static void asm_intmin_max(ASMState *as, IRIns *ir, int cc) -{ - uint32_t kcmp = 0, kmov = 0; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - Reg right = 0; - if (irref_isk(ir->op2)) { - kcmp = emit_isk12(ARMI_CMP, IR(ir->op2)->i); - if (kcmp) kmov = emit_isk12(ARMI_MOV, IR(ir->op2)->i); - } - if (!kmov) { - kcmp = 0; - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - if (kmov || dest != right) { - emit_dm(as, ARMF_CC(ARMI_MOV, cc)^kmov, dest, right); - cc ^= 1; /* Must use opposite conditions for paired moves. */ - } else { - cc ^= (CC_LT^CC_GT); /* Otherwise may swap CC_LT <-> CC_GT. */ - } - if (dest != left) emit_dm(as, ARMF_CC(ARMI_MOV, cc), dest, left); - emit_nm(as, ARMI_CMP^kcmp, left, right); -} - -#if LJ_SOFTFP -static void asm_sfpmin_max(ASMState *as, IRIns *ir, int cc) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; - RegSet drop = RSET_SCRATCH; - Reg r; - IRRef args[4]; - args[0] = ir->op1; args[1] = (ir+1)->op1; - args[2] = ir->op2; args[3] = (ir+1)->op2; - /* __aeabi_cdcmple preserves r0-r3. */ - if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); - if (ra_hasreg((ir+1)->r)) rset_clear(drop, (ir+1)->r); - if (!rset_test(as->freeset, RID_R2) && - regcost_ref(as->cost[RID_R2]) == args[2]) rset_clear(drop, RID_R2); - if (!rset_test(as->freeset, RID_R3) && - regcost_ref(as->cost[RID_R3]) == args[3]) rset_clear(drop, RID_R3); - ra_evictset(as, drop); - ra_destpair(as, ir); - emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETHI, RID_R3); - emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETLO, RID_R2); - emit_call(as, (void *)ci->func); - for (r = RID_R0; r <= RID_R3; r++) - ra_leftov(as, r, args[r-RID_R0]); -} -#else -static void asm_fpmin_max(ASMState *as, IRIns *ir, int cc) -{ - Reg dest = (ra_dest(as, ir, RSET_FPR) & 15); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = ((left >> 8) & 15); left &= 15; - if (dest != left) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc^1), dest, left); - if (dest != right) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc), dest, right); - emit_d(as, ARMI_VMRS, 0); - emit_dm(as, ARMI_VCMP_D, left, right); -} -#endif - -static void asm_min_max(ASMState *as, IRIns *ir, int cc, int fcc) -{ -#if LJ_SOFTFP - UNUSED(fcc); -#else - if (irt_isnum(ir->t)) - asm_fpmin_max(as, ir, fcc); - else -#endif - asm_intmin_max(as, ir, cc); -} - -/* -- Comparisons --------------------------------------------------------- */ - -/* Map of comparisons to flags. ORDER IR. */ -static const uint8_t asm_compmap[IR_ABC+1] = { - /* op FP swp int cc FP cc */ - /* LT */ CC_GE + (CC_HS << 4), - /* GE x */ CC_LT + (CC_HI << 4), - /* LE */ CC_GT + (CC_HI << 4), - /* GT x */ CC_LE + (CC_HS << 4), - /* ULT x */ CC_HS + (CC_LS << 4), - /* UGE */ CC_LO + (CC_LO << 4), - /* ULE x */ CC_HI + (CC_LO << 4), - /* UGT */ CC_LS + (CC_LS << 4), - /* EQ */ CC_NE + (CC_NE << 4), - /* NE */ CC_EQ + (CC_EQ << 4), - /* ABC */ CC_LS + (CC_LS << 4) /* Same as UGT. */ -}; - -#if LJ_SOFTFP -/* FP comparisons. */ -static void asm_sfpcomp(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; - RegSet drop = RSET_SCRATCH; - Reg r; - IRRef args[4]; - int swp = (((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1) << 1); - args[swp^0] = ir->op1; args[swp^1] = (ir+1)->op1; - args[swp^2] = ir->op2; args[swp^3] = (ir+1)->op2; - /* __aeabi_cdcmple preserves r0-r3. This helps to reduce spills. */ - for (r = RID_R0; r <= RID_R3; r++) - if (!rset_test(as->freeset, r) && - regcost_ref(as->cost[r]) == args[r-RID_R0]) rset_clear(drop, r); - ra_evictset(as, drop); - asm_guardcc(as, (asm_compmap[ir->o] >> 4)); - emit_call(as, (void *)ci->func); - for (r = RID_R0; r <= RID_R3; r++) - ra_leftov(as, r, args[r-RID_R0]); -} -#else -/* FP comparisons. */ -static void asm_fpcomp(ASMState *as, IRIns *ir) -{ - Reg left, right; - ARMIns ai; - int swp = ((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1); - if (!swp && irref_isk(ir->op2) && ir_knum(IR(ir->op2))->u64 == 0) { - left = (ra_alloc1(as, ir->op1, RSET_FPR) & 15); - right = 0; - ai = ARMI_VCMPZ_D; - } else { - left = ra_alloc2(as, ir, RSET_FPR); - if (swp) { - right = (left & 15); left = ((left >> 8) & 15); - } else { - right = ((left >> 8) & 15); left &= 15; - } - ai = ARMI_VCMP_D; - } - asm_guardcc(as, (asm_compmap[ir->o] >> 4)); - emit_d(as, ARMI_VMRS, 0); - emit_dm(as, ai, left, right); -} -#endif - -/* Integer comparisons. */ -static void asm_intcomp(ASMState *as, IRIns *ir) -{ - ARMCC cc = (asm_compmap[ir->o] & 15); - IRRef lref = ir->op1, rref = ir->op2; - Reg left; - uint32_t m; - int cmpprev0 = 0; - lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)); - if (asm_swapops(as, lref, rref)) { - Reg tmp = lref; lref = rref; rref = tmp; - if (cc >= CC_GE) cc ^= 7; /* LT <-> GT, LE <-> GE */ - else if (cc > CC_NE) cc ^= 11; /* LO <-> HI, LS <-> HS */ - } - if (irref_isk(rref) && IR(rref)->i == 0) { - IRIns *irl = IR(lref); - cmpprev0 = (irl+1 == ir); - /* Combine comp(BAND(left, right), 0) into tst left, right. */ - if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) { - IRRef blref = irl->op1, brref = irl->op2; - uint32_t m2 = 0; - Reg bleft; - if (asm_swapops(as, blref, brref)) { - Reg tmp = blref; blref = brref; brref = tmp; - } - if (irref_isk(brref)) { - m2 = emit_isk12(ARMI_AND, IR(brref)->i); - if ((m2 & (ARMI_AND^ARMI_BIC))) - goto notst; /* Not beneficial if we miss a constant operand. */ - } - if (cc == CC_GE) cc = CC_PL; - else if (cc == CC_LT) cc = CC_MI; - else if (cc > CC_NE) goto notst; /* Other conds don't work with tst. */ - bleft = ra_alloc1(as, blref, RSET_GPR); - if (!m2) m2 = asm_fuseopm(as, 0, brref, rset_exclude(RSET_GPR, bleft)); - asm_guardcc(as, cc); - emit_n(as, ARMI_TST^m2, bleft); - return; - } - } -notst: - left = ra_alloc1(as, lref, RSET_GPR); - m = asm_fuseopm(as, ARMI_CMP, rref, rset_exclude(RSET_GPR, left)); - asm_guardcc(as, cc); - emit_n(as, ARMI_CMP^m, left); - /* Signed comparison with zero and referencing previous ins? */ - if (cmpprev0 && (cc <= CC_NE || cc >= CC_GE)) - as->flagmcp = as->mcp; /* Allow elimination of the compare. */ -} - -#if LJ_HASFFI -/* 64 bit integer comparisons. */ -static void asm_int64comp(ASMState *as, IRIns *ir) -{ - int signedcomp = (ir->o <= IR_GT); - ARMCC cclo, cchi; - Reg leftlo, lefthi; - uint32_t mlo, mhi; - RegSet allow = RSET_GPR, oldfree; - - /* Always use unsigned comparison for loword. */ - cclo = asm_compmap[ir->o + (signedcomp ? 4 : 0)] & 15; - leftlo = ra_alloc1(as, ir->op1, allow); - oldfree = as->freeset; - mlo = asm_fuseopm(as, ARMI_CMP, ir->op2, rset_clear(allow, leftlo)); - allow &= ~(oldfree & ~as->freeset); /* Update for allocs of asm_fuseopm. */ - - /* Use signed or unsigned comparison for hiword. */ - cchi = asm_compmap[ir->o] & 15; - lefthi = ra_alloc1(as, (ir+1)->op1, allow); - mhi = asm_fuseopm(as, ARMI_CMP, (ir+1)->op2, rset_clear(allow, lefthi)); - - /* All register allocations must be performed _before_ this point. */ - if (signedcomp) { - MCLabel l_around = emit_label(as); - asm_guardcc(as, cclo); - emit_n(as, ARMI_CMP^mlo, leftlo); - emit_branch(as, ARMF_CC(ARMI_B, CC_NE), l_around); - if (cchi == CC_GE || cchi == CC_LE) cchi ^= 6; /* GE -> GT, LE -> LT */ - asm_guardcc(as, cchi); - } else { - asm_guardcc(as, cclo); - emit_n(as, ARMF_CC(ARMI_CMP, CC_EQ)^mlo, leftlo); - } - emit_n(as, ARMI_CMP^mhi, lefthi); -} -#endif - -/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ - -/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ -#if LJ_HASFFI || LJ_SOFTFP - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; - if ((ir-1)->o <= IR_NE) { /* 64 bit integer or FP comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ -#if LJ_SOFTFP - if (!irt_isint(ir->t)) { - asm_sfpcomp(as, ir-1); - return; - } -#endif -#if LJ_HASFFI - asm_int64comp(as, ir-1); -#endif - return; -#if LJ_SOFTFP - } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) { - as->curins--; /* Always skip the loword min/max. */ - if (uselo || usehi) - asm_sfpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_HI : CC_LO); - return; -#elif LJ_HASFFI - } else if ((ir-1)->o == IR_CONV) { - as->curins--; /* Always skip the CONV. */ - if (usehi || uselo) - asm_conv64(as, ir); - return; -#endif - } else if ((ir-1)->o == IR_XSTORE) { - if ((ir-1)->r != RID_SINK) - asm_xstore(as, ir, 4); - return; - } - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { -#if LJ_HASFFI - case IR_ADD: - as->curins--; - asm_intop(as, ir, ARMI_ADC); - asm_intop(as, ir-1, ARMI_ADD|ARMI_S); - break; - case IR_SUB: - as->curins--; - asm_intop(as, ir, ARMI_SBC); - asm_intop(as, ir-1, ARMI_SUB|ARMI_S); - break; - case IR_NEG: - as->curins--; - asm_intneg(as, ir, ARMI_RSC); - asm_intneg(as, ir-1, ARMI_RSB|ARMI_S); - break; -#endif -#if LJ_SOFTFP - case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - case IR_STRTO: - if (!uselo) - ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */ - break; -#endif - case IR_CALLN: - case IR_CALLS: - case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; -#if LJ_SOFTFP - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR: -#endif - case IR_CNEWI: - /* Nothing to do here. Handled by lo op itself. */ - break; - default: lua_assert(0); break; - } -#else - UNUSED(as); UNUSED(ir); lua_assert(0); -#endif -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - Reg pbase; - uint32_t k; - if (irp) { - if (!ra_hasspill(irp->s)) { - pbase = irp->r; - lua_assert(ra_hasreg(pbase)); - } else if (allow) { - pbase = rset_pickbot(allow); - } else { - pbase = RID_RET; - emit_lso(as, ARMI_LDR, RID_RET, RID_SP, 0); /* Restore temp. register. */ - } - } else { - pbase = RID_BASE; - } - emit_branch(as, ARMF_CC(ARMI_BL, CC_LS), exitstub_addr(as->J, exitno)); - k = emit_isk12(0, (int32_t)(8*topslot)); - lua_assert(k); - emit_n(as, ARMI_CMP^k, RID_TMP); - emit_dnm(as, ARMI_SUB, RID_TMP, RID_TMP, pbase); - emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, - (int32_t)offsetof(lua_State, maxstack)); - if (irp) { /* Must not spill arbitrary registers in head of side trace. */ - int32_t i = i32ptr(&J2G(as->J)->jit_L); - if (ra_hasspill(irp->s)) - emit_lso(as, ARMI_LDR, pbase, RID_SP, sps_scale(irp->s)); - emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, (i & 4095)); - if (ra_hasspill(irp->s) && !allow) - emit_lso(as, ARMI_STR, RID_RET, RID_SP, 0); /* Save temp. register. */ - emit_loadi(as, RID_TMP, (i & ~4095)); - } else { - emit_getgl(as, RID_TMP, jit_L); - } -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if (irt_isnum(ir->t)) { -#if LJ_SOFTFP - RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); - Reg tmp; - lua_assert(irref_isk(ref)); /* LJ_SOFTFP: must be a number constant. */ - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, - rset_exclude(RSET_GPREVEN, RID_BASE)); - emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs); - if (rset_test(as->freeset, tmp+1)) odd = RID2RSET(tmp+1); - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, odd); - emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs+4); -#else - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_vlso(as, ARMI_VSTR_D, src, RID_BASE, ofs); -#endif - } else { - RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); - Reg type; - lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPREVEN, RID_BASE)); - emit_lso(as, ARMI_STR, src, RID_BASE, ofs); - if (rset_test(as->freeset, src+1)) odd = RID2RSET(src+1); - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { - if (s == 0) continue; /* Do not overwrite link to previous frame. */ - type = ra_allock(as, (int32_t)(*flinks--), odd); -#if LJ_SOFTFP - } else if ((sn & SNAP_SOFTFPNUM)) { - type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPRODD, RID_BASE)); -#endif - } else { - type = ra_allock(as, (int32_t)irt_toitype(ir->t), odd); - } - emit_lso(as, ARMI_STR, type, RID_BASE, ofs+4); - } - checkmclim(as); - } - lua_assert(map + nent == flinks); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp1, tmp2; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ - emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - tmp1 = ra_releasetmp(as, ASMREF_TMP1); - tmp2 = ra_releasetmp(as, ASMREF_TMP2); - emit_loadi(as, tmp2, as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_branch(as, ARMF_CC(ARMI_B, CC_LS), l_end); - emit_nm(as, ARMI_CMP, RID_TMP, tmp2); - emit_lso(as, ARMI_LDR, tmp2, tmp1, - (int32_t)offsetof(global_State, gc.threshold)); - emit_lso(as, ARMI_LDR, RID_TMP, tmp1, - (int32_t)offsetof(global_State, gc.total)); - ra_allockreg(as, i32ptr(J2G(as->J)), tmp1); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guardcc already inverted the bcc and patched the final bl. */ - p[-2] |= ((uint32_t)(target-p) & 0x00ffffffu); - } else { - p[-1] = ARMI_B | ((uint32_t)((target-p)-1) & 0x00ffffffu); - } -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Reload L register from g->jit_L. */ -static void asm_head_lreg(ASMState *as) -{ - IRIns *ir = IR(ASMREF_L); - if (ra_used(ir)) { - Reg r = ra_dest(as, ir, RSET_GPR); - emit_getgl(as, r, jit_L); - ra_evictk(as); - } -} - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir; - asm_head_lreg(as); - ir = IR(REF_BASE); - if (ra_hasreg(ir->r) && rset_test(as->modset, ir->r)) ra_spill(as, ir); - ra_destreg(as, ir, RID_BASE); -} - -/* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir; - asm_head_lreg(as); - ir = IR(REF_BASE); - if (ra_hasreg(ir->r) && rset_test(as->modset, ir->r)) ra_spill(as, ir); - if (ra_hasspill(irp->s)) { - rset_clear(allow, ra_dest(as, ir, allow)); - } else { - Reg r = irp->r; - lua_assert(ra_hasreg(r)); - rset_clear(allow, r); - if (r != ir->r && !rset_test(as->freeset, r)) - ra_restore(as, regcost_ref(as->cost[r])); - ra_destreg(as, ir, r); - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - MCode *p = as->mctop; - MCode *target; - int32_t spadj = as->T->spadjust; - if (spadj == 0) { - as->mctop = --p; - } else { - /* Patch stack adjustment. */ - uint32_t k = emit_isk12(ARMI_ADD, spadj); - lua_assert(k); - p[-2] = (ARMI_ADD^k) | ARMF_D(RID_SP) | ARMF_N(RID_SP); - } - /* Patch exit branch. */ - target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; - p[-1] = ARMI_B|(((target-p)-1)&0x00ffffffu); -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - MCode *p = as->mctop - 1; /* Leave room for exit branch. */ - if (as->loopref) { - as->invmcp = as->mcp = p; - } else { - as->mcp = p-1; /* Leave room for stack pointer adjustment. */ - as->invmcp = NULL; - } - *p = 0; /* Prevent load/store merging. */ -} - -/* -- Instruction dispatch ------------------------------------------------ */ - -/* Assemble a single instruction. */ -static void asm_ir(ASMState *as, IRIns *ir) -{ - switch ((IROp)ir->o) { - /* Miscellaneous ops. */ - case IR_LOOP: asm_loop(as); break; - case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; - case IR_USE: - ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; - case IR_PHI: asm_phi(as, ir); break; - case IR_HIOP: asm_hiop(as, ir); break; - case IR_GCSTEP: asm_gcstep(as, ir); break; - - /* Guarded assertions. */ - case IR_EQ: case IR_NE: - if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { - as->curins--; - asm_href(as, ir-1, (IROp)ir->o); - break; - } - /* fallthrough */ - case IR_LT: case IR_GE: case IR_LE: case IR_GT: - case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: - case IR_ABC: -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { asm_fpcomp(as, ir); break; } -#endif - asm_intcomp(as, ir); - break; - - case IR_RETF: asm_retf(as, ir); break; - - /* Bit ops. */ - case IR_BNOT: asm_bitop(as, ir, ARMI_MVN); break; - case IR_BSWAP: asm_bitswap(as, ir); break; - - case IR_BAND: asm_bitop(as, ir, ARMI_AND); break; - case IR_BOR: asm_bitop(as, ir, ARMI_ORR); break; - case IR_BXOR: asm_bitop(as, ir, ARMI_EOR); break; - - case IR_BSHL: asm_bitshift(as, ir, ARMSH_LSL); break; - case IR_BSHR: asm_bitshift(as, ir, ARMSH_LSR); break; - case IR_BSAR: asm_bitshift(as, ir, ARMSH_ASR); break; - case IR_BROR: asm_bitshift(as, ir, ARMSH_ROR); break; - case IR_BROL: lua_assert(0); break; - - /* Arithmetic ops. */ - case IR_ADD: case IR_ADDOV: asm_add(as, ir); break; - case IR_SUB: case IR_SUBOV: asm_sub(as, ir); break; - case IR_MUL: case IR_MULOV: asm_mul(as, ir); break; - case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break; - case IR_NEG: asm_neg(as, ir); break; - -#if LJ_SOFTFP - case IR_DIV: case IR_POW: case IR_ABS: - case IR_ATAN2: case IR_LDEXP: case IR_FPMATH: case IR_TOBIT: - lua_assert(0); /* Unused for LJ_SOFTFP. */ - break; -#else - case IR_DIV: asm_fparith(as, ir, ARMI_VDIV_D); break; - case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break; - case IR_ABS: asm_fpunary(as, ir, ARMI_VABS_D); break; - case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break; - case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break; - case IR_FPMATH: - if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) - break; - if (ir->op2 <= IRFPM_TRUNC) - asm_callround(as, ir, ir->op2); - else if (ir->op2 == IRFPM_SQRT) - asm_fpunary(as, ir, ARMI_VSQRT_D); - else - asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); - break; - case IR_TOBIT: asm_tobit(as, ir); break; -#endif - - case IR_MIN: asm_min_max(as, ir, CC_GT, CC_HI); break; - case IR_MAX: asm_min_max(as, ir, CC_LT, CC_LO); break; - - /* Memory references. */ - case IR_AREF: asm_aref(as, ir); break; - case IR_HREF: asm_href(as, ir, 0); break; - case IR_HREFK: asm_hrefk(as, ir); break; - case IR_NEWREF: asm_newref(as, ir); break; - case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; - case IR_FREF: asm_fref(as, ir); break; - case IR_STRREF: asm_strref(as, ir); break; - - /* Loads and stores. */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - asm_ahuvload(as, ir); - break; - case IR_FLOAD: asm_fload(as, ir); break; - case IR_XLOAD: asm_xload(as, ir); break; - case IR_SLOAD: asm_sload(as, ir); break; - - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; - case IR_FSTORE: asm_fstore(as, ir); break; - case IR_XSTORE: asm_xstore(as, ir, 0); break; - - /* Allocations. */ - case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; - case IR_TNEW: asm_tnew(as, ir); break; - case IR_TDUP: asm_tdup(as, ir); break; - case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; - - /* Write barriers. */ - case IR_TBAR: asm_tbar(as, ir); break; - case IR_OBAR: asm_obar(as, ir); break; - - /* Type conversions. */ - case IR_CONV: asm_conv(as, ir); break; - case IR_TOSTR: asm_tostr(as, ir); break; - case IR_STRTO: asm_strto(as, ir); break; - - /* Calls. */ - case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; - case IR_CALLXS: asm_callx(as, ir); break; - case IR_CARG: break; - - default: - setintV(&as->J->errinfo, ir->o); - lj_trace_err_info(as->J, LJ_TRERR_NYIIR); - break; - } -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - uint32_t i, nargs = (int)CCI_NARGS(ci); - int nslots = 0, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR, fprodd = 0; - asm_collectargs(as, ir, ci, args); - for (i = 0; i < nargs; i++) { - if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t)) { - if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) { - if (irt_isnum(IR(args[i])->t)) { - if (nfpr > 0) nfpr--; - else fprodd = 0, nslots = (nslots + 3) & ~1; - } else { - if (fprodd) fprodd--; - else if (nfpr > 0) fprodd = 1, nfpr--; - else nslots++; - } - } else if (irt_isnum(IR(args[i])->t)) { - ngpr &= ~1; - if (ngpr > 0) ngpr -= 2; else nslots += 2; - } else { - if (ngpr > 0) ngpr--; else nslots++; - } - } else { - if (ngpr > 0) ngpr--; else nslots++; - } - } - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; - return REGSP_HINT(RID_RET); -} - -static void asm_setup_target(ASMState *as) -{ - /* May need extra exit for asm_stack_check on side traces. */ - asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); -} - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *pe = (MCode *)((char *)p + T->szmcode); - MCode *cstart = NULL, *cend = p; - MCode *mcarea = lj_mcode_patch(J, p, 0); - MCode *px = exitstub_addr(J, exitno) - 2; - for (; p < pe; p++) { - /* Look for bl_cc exitstub, replace with b_cc target. */ - uint32_t ins = *p; - if ((ins & 0x0f000000u) == 0x0b000000u && ins < 0xf0000000u && - ((ins ^ (px-p)) & 0x00ffffffu) == 0) { - *p = (ins & 0xfe000000u) | (((target-p)-2) & 0x00ffffffu); - cend = p+1; - if (!cstart) cstart = p; - } - } - lua_assert(cstart != NULL); - lj_mcode_sync(cstart, cend); - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_asm_mips.h b/third-party/LuaJIT-2.0.2/src/lj_asm_mips.h deleted file mode 100644 index 9fe7c9c334..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_asm_mips.h +++ /dev/null @@ -1,1976 +0,0 @@ -/* -** MIPS IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Register allocator extensions --------------------------------------- */ - -/* Allocate a register with a hint. */ -static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!ra_hashint(r) && !iscrossref(as, ref)) - ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ - r = ra_allocref(as, ref, allow); - } - ra_noweak(as, r); - return r; -} - -/* Allocate a register or RID_ZERO. */ -static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!(allow & RSET_FPR) && irref_isk(ref) && IR(ref)->i == 0) - return RID_ZERO; - r = ra_allocref(as, ref, allow); - } else { - ra_noweak(as, r); - } - return r; -} - -/* Allocate two source registers for three-operand instructions. */ -static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - Reg left = irl->r, right = irr->r; - if (ra_hasreg(left)) { - ra_noweak(as, left); - if (ra_noreg(right)) - right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left)); - else - ra_noweak(as, right); - } else if (ra_hasreg(right)) { - ra_noweak(as, right); - left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right)); - } else if (ra_hashint(right)) { - right = ra_alloc1z(as, ir->op2, allow); - left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right)); - } else { - left = ra_alloc1z(as, ir->op1, allow); - right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left)); - } - return left | (right << 8); -} - -/* -- Guard handling ------------------------------------------------------ */ - -/* Need some spare long-range jump slots, for out-of-range branches. */ -#define MIPS_SPAREJUMP 4 - -/* Setup spare long-range jump slots per mcarea. */ -static void asm_sparejump_setup(ASMState *as) -{ - MCode *mxp = as->mcbot; - /* Assumes sizeof(MCLink) == 8. */ - if (((uintptr_t)mxp & (LJ_PAGESIZE-1)) == 8) { - lua_assert(MIPSI_NOP == 0); - memset(mxp+2, 0, MIPS_SPAREJUMP*8); - mxp += MIPS_SPAREJUMP*2; - lua_assert(mxp < as->mctop); - lj_mcode_sync(as->mcbot, mxp); - lj_mcode_commitbot(as->J, mxp); - as->mcbot = mxp; - as->mclim = as->mcbot + MCLIM_REDZONE; - } -} - -/* Setup exit stub after the end of each trace. */ -static void asm_exitstub_setup(ASMState *as) -{ - MCode *mxp = as->mctop; - /* sw TMP, 0(sp); j ->vm_exit_handler; li TMP, traceno */ - *--mxp = MIPSI_LI|MIPSF_T(RID_TMP)|as->T->traceno; - *--mxp = MIPSI_J|((((uintptr_t)(void *)lj_vm_exit_handler)>>2)&0x03ffffffu); - lua_assert(((uintptr_t)mxp ^ (uintptr_t)(void *)lj_vm_exit_handler)>>28 == 0); - *--mxp = MIPSI_SW|MIPSF_T(RID_TMP)|MIPSF_S(RID_SP)|0; - as->mctop = mxp; -} - -/* Keep this in-sync with exitstub_trace_addr(). */ -#define asm_exitstub_addr(as) ((as)->mctop) - -/* Emit conditional branch to exit for guard. */ -static void asm_guard(ASMState *as, MIPSIns mi, Reg rs, Reg rt) -{ - MCode *target = asm_exitstub_addr(as); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->invmcp = NULL; - as->loopinv = 1; - as->mcp = p+1; - mi = mi ^ ((mi>>28) == 1 ? 0x04000000u : 0x00010000u); /* Invert cond. */ - target = p; /* Patch target later in asm_loop_fixup. */ - } - emit_ti(as, MIPSI_LI, RID_TMP, as->snapno); - emit_branch(as, mi, rs, rt, target); -} - -/* -- Operand fusion ------------------------------------------------------ */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if there's no conflicting instruction between curins and ref. */ -static int noconflict(ASMState *as, IRRef ref, IROp conflict) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - return 1; /* Ok, no conflict. */ -} - -/* Fuse the array base of colocated arrays. */ -static int32_t asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) - return (int32_t)sizeof(GCtab); - return 0; -} - -/* Fuse array/hash/upvalue reference into register+offset operand. */ -static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - if (ir->o == IR_AREF) { - if (mayfuse(as, ref)) { - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, refa, allow); - } - } - } - } else if (ir->o == IR_HREFK) { - if (mayfuse(as, ref)) { - int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, ir->op1, allow); - } - } - } else if (ir->o == IR_UREFC) { - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); - int32_t jgl = (intptr_t)J2G(as->J); - if ((uint32_t)(ofs-jgl) < 65536) { - *ofsp = ofs-jgl-32768; - return RID_JGL; - } else { - *ofsp = (int16_t)ofs; - return ra_allock(as, ofs-(int16_t)ofs, allow); - } - } - } - } - *ofsp = 0; - return ra_alloc1(as, ref, allow); -} - -/* Fuse XLOAD/XSTORE reference into load/store operand. */ -static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref, - RegSet allow, int32_t ofs) -{ - IRIns *ir = IR(ref); - Reg base; - if (ra_noreg(ir->r) && canfuse(as, ir)) { - if (ir->o == IR_ADD) { - int32_t ofs2; - if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) { - ref = ir->op1; - ofs = ofs2; - } - } else if (ir->o == IR_STRREF) { - int32_t ofs2 = 65536; - lua_assert(ofs == 0); - ofs = (int32_t)sizeof(GCstr); - if (irref_isk(ir->op2)) { - ofs2 = ofs + IR(ir->op2)->i; - ref = ir->op1; - } else if (irref_isk(ir->op1)) { - ofs2 = ofs + IR(ir->op1)->i; - ref = ir->op2; - } - if (!checki16(ofs2)) { - /* NYI: Fuse ADD with constant. */ - Reg right, left = ra_alloc2(as, ir, allow); - right = (left >> 8); left &= 255; - emit_hsi(as, mi, rt, RID_TMP, ofs); - emit_dst(as, MIPSI_ADDU, RID_TMP, left, right); - return; - } - ofs = ofs2; - } - } - base = ra_alloc1(as, ref, allow); - emit_hsi(as, mi, rt, base, ofs); -} - -/* -- Calls --------------------------------------------------------------- */ - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_NARGS(ci); - int32_t ofs = 16; - Reg gpr, fpr = REGARG_FIRSTFPR; - if ((void *)ci->func) - emit_call(as, (void *)ci->func); - for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) - as->cost[gpr] = REGCOST(~0u, ASMREF_L); - gpr = REGARG_FIRSTGPR; - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - if (ref) { - IRIns *ir = IR(ref); - if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR && - !(ci->flags & CCI_VARARG)) { - lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */ - ra_leftov(as, fpr, ref); - fpr += 2; - gpr += irt_isnum(ir->t) ? 2 : 1; - } else { - fpr = REGARG_LASTFPR+1; - if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1; - if (gpr <= REGARG_LASTGPR) { - lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */ - if (irt_isfp(ir->t)) { - RegSet of = as->freeset; - Reg r; - /* Workaround to protect argument GPRs from being used for remat. */ - as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); - r = ra_alloc1(as, ref, RSET_FPR); - as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); - if (irt_isnum(ir->t)) { - emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1); - emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r); - lua_assert(rset_test(as->freeset, gpr+1)); /* Already evicted. */ - gpr += 2; - } else if (irt_isfloat(ir->t)) { - emit_tg(as, MIPSI_MFC1, gpr, r); - gpr++; - } - } else { - ra_leftov(as, gpr, ref); - gpr++; - } - } else { - Reg r = ra_alloc1z(as, ref, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; - emit_spstore(as, ir, r, ofs); - ofs += irt_isnum(ir->t) ? 8 : 4; - } - } - } else { - fpr = REGARG_LASTFPR+1; - if (gpr <= REGARG_LASTGPR) - gpr++; - else - ofs += 4; - } - checkmclim(as); - } -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP); - if ((ci->flags & CCI_NOFPRCLOBBER)) - drop &= ~RSET_FPR; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - lua_assert(!irt_ispri(ir->t)); - if (irt_isfp(ir->t)) { - if ((ci->flags & CCI_CASTU64)) { - int32_t ofs = sps_scale(ir->s); - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1); - emit_tg(as, MIPSI_MTC1, RID_RETLO, dest); - } - if (ofs) { - emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0)); - emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4)); - } - } else { - ra_destreg(as, ir, RID_FPRET); - } - } else if (hiop) { - ra_destpair(as, ir); - } else { - ra_destreg(as, ir, RID_RET); - } - } -} - -static void asm_call(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX]; - const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; - asm_collectargs(as, ir, ci, args); - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - if (irref_isk(func)) { /* Call to constant address. */ - ci.func = (ASMFunction)(void *)(irf->i); - } else { /* Need specific register for indirect calls. */ - Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR)); - MCode *p = as->mcp; - if (r == RID_CFUNCADDR) - *--p = MIPSI_NOP; - else - *--p = MIPSI_MOVE | MIPSF_D(RID_CFUNCADDR) | MIPSF_S(r); - *--p = MIPSI_JALR | MIPSF_S(r); - as->mcp = p; - ci.func = (ASMFunction)(void *)0; - } - asm_gencall(as, &ci, args); -} - -static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) -{ - const CCallInfo *ci = &lj_ir_callinfo[id]; - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -static void asm_callround(ASMState *as, IRIns *ir, IRCallID id) -{ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RID2RSET(RID_R1)|RID2RSET(RID_R12)|RID2RSET(RID_FPRET)| - RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(REGARG_FIRSTFPR); - if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); - ra_evictset(as, drop); - ra_destreg(as, ir, RID_FPRET); - emit_call(as, (void *)lj_ir_callinfo[id].func); - ra_leftov(as, REGARG_FIRSTFPR, ir->op1); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guard(as, MIPSI_BNE, RID_TMP, - ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); - emit_tsi(as, MIPSI_LW, RID_TMP, base, -8); -} - -/* -- Type conversions ---------------------------------------------------- */ - -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_guard(as, MIPSI_BC1F, 0, 0); - emit_fgh(as, MIPSI_C_EQ_D, 0, tmp, left); - emit_fg(as, MIPSI_CVT_D_W, tmp, tmp); - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, MIPSI_CVT_W_D, tmp, left); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_FPR; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, allow); - Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); - Reg tmp = ra_scratch(as, rset_clear(allow, right)); - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fgh(as, MIPSI_ADD_D, tmp, left, right); -} - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); - int stfp = (st == IRT_NUM || st == IRT_FLOAT); - IRRef lref = ir->op1; - lua_assert(irt_type(ir->t) != st); - lua_assert(!(irt_isint64(ir->t) || - (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */ - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - emit_fg(as, st == IRT_NUM ? MIPSI_CVT_S_D : MIPSI_CVT_D_S, - dest, ra_alloc1(as, lref, RSET_FPR)); - } else if (st == IRT_U32) { /* U32 to FP conversion. */ - /* y = (x ^ 0x8000000) + 2147483648.0 */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - emit_fgh(as, irt_isfloat(ir->t) ? MIPSI_ADD_S : MIPSI_ADD_D, - dest, dest, tmp); - emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W, - dest, dest); - if (irt_isfloat(ir->t)) - emit_lsptr(as, MIPSI_LWC1, (tmp & 31), - (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)), - RSET_GPR); - else - emit_lsptr(as, MIPSI_LDC1, (tmp & 31), - (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)), - RSET_GPR); - emit_tg(as, MIPSI_MTC1, RID_TMP, dest); - emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left); - emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); - } else { /* Integer to FP conversion. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W, - dest, dest); - emit_tg(as, MIPSI_MTC1, left, dest); - } - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lua_assert(irt_isint(ir->t) && st == IRT_NUM); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, lref, RSET_FPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - if (irt_isu32(ir->t)) { - /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */ - emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP); - emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, st == IRT_FLOAT ? MIPSI_FLOOR_W_S : MIPSI_FLOOR_W_D, - tmp, tmp); - emit_fgh(as, st == IRT_FLOAT ? MIPSI_SUB_S : MIPSI_SUB_D, - tmp, left, tmp); - if (st == IRT_FLOAT) - emit_lsptr(as, MIPSI_LWC1, (tmp & 31), - (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)), - RSET_GPR); - else - emit_lsptr(as, MIPSI_LDC1, (tmp & 31), - (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)), - RSET_GPR); - } else { - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D, - tmp, left); - } - } - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); - if ((ir->op2 & IRCONV_SEXT)) { - if ((as->flags & JIT_F_MIPS32R2)) { - emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left); - } else { - uint32_t shift = st == IRT_I8 ? 24 : 16; - emit_dta(as, MIPSI_SRA, dest, dest, shift); - emit_dta(as, MIPSI_SLL, dest, left, shift); - } - } else { - emit_tsi(as, MIPSI_ANDI, dest, left, - (int32_t)(st == IRT_U8 ? 0xff : 0xffff)); - } - } else { /* 32/64 bit integer conversions. */ - /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } -} - -#if LJ_HASFFI -static void asm_conv64(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); - IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); - IRCallID id; - const CCallInfo *ci; - IRRef args[2]; - args[LJ_BE?0:1] = ir->op1; - args[LJ_BE?1:0] = (ir-1)->op1; - if (st == IRT_NUM || st == IRT_FLOAT) { - id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); - ir--; - } else { - id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); - } - ci = &lj_ir_callinfo[id]; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} -#endif - -static void asm_strto(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - RegSet drop = RSET_SCRATCH; - if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ - ra_evictset(as, drop); - asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - /* Store the result to the spill slot or temp slots. */ - emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), - RID_SP, sps_scale(ir->s)); -} - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) -{ - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if (irref_isk(ref)) /* Use the number constant itself as a TValue. */ - ra_allockreg(as, i32ptr(ir_knum(ir)), dest); - else /* Otherwise force a spill and use the spill slot. */ - emit_tsi(as, MIPSI_ADDIU, dest, RID_SP, ra_spill(as, ir)); - } else { - /* Otherwise use g->tmptv to hold the TValue. */ - RegSet allow = rset_exclude(RSET_GPR, dest); - Reg type; - emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, offsetof(global_State, tmptv)-32768); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, allow); - emit_setgl(as, src, tmptv.gcr); - } - type = ra_allock(as, irt_toitype(ir->t), allow); - emit_setgl(as, type, tmptv.it); - } -} - -static void asm_tostr(ASMState *as, IRIns *ir) -{ - IRRef args[2]; - args[0] = ASMREF_L; - as->gcsteps++; - if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; - args[1] = ASMREF_TMP1; /* const lua_Number * */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); - } else { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; - args[1] = ir->op1; /* int32_t k */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - } -} - -/* -- Memory references --------------------------------------------------- */ - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx, base; - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - base = ra_alloc1(as, refa, RSET_GPR); - emit_tsi(as, MIPSI_ADDIU, dest, base, ofs); - return; - } - } - base = ra_alloc1(as, ir->op1, RSET_GPR); - idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - emit_dst(as, MIPSI_ADDU, dest, RID_TMP, base); - emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3); -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2; - IRRef refkey = ir->op2; - IRIns *irkey = IR(refkey); - IRType1 kt = irkey->t; - uint32_t khash; - MCLabel l_end, l_loop, l_next; - - rset_clear(allow, tab); - if (irt_isnum(kt)) { - key = ra_alloc1(as, refkey, RSET_FPR); - tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); - } else if (!irt_ispri(kt)) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - type = ra_allock(as, irt_toitype(irkey->t), allow); - rset_clear(allow, type); - } - tmp2 = ra_scratch(as, allow); - rset_clear(allow, tmp2); - - /* Key not found in chain: load niltv. */ - l_end = emit_label(as); - if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - else - *--as->mcp = MIPSI_NOP; - /* Follow hash chain until the end. */ - emit_move(as, dest, tmp1); - l_loop = --as->mcp; - emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, next)); - l_next = emit_label(as); - - /* Type and value comparison. */ - if (irt_isnum(kt)) { - emit_branch(as, MIPSI_BC1T, 0, 0, l_end); - emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key); - emit_tg(as, MIPSI_MFC1, tmp1, key+1); - emit_branch(as, MIPSI_BEQ, tmp1, RID_ZERO, l_next); - emit_tsi(as, MIPSI_SLTIU, tmp1, tmp1, (int32_t)LJ_TISNUM); - emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n)); - } else { - if (irt_ispri(kt)) { - emit_branch(as, MIPSI_BEQ, tmp1, type, l_end); - } else { - emit_branch(as, MIPSI_BEQ, tmp2, key, l_end); - emit_tsi(as, MIPSI_LW, tmp2, dest, (int32_t)offsetof(Node, key.gcr)); - emit_branch(as, MIPSI_BNE, tmp1, type, l_next); - } - } - emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.it)); - *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu); - - /* Load main position relative to tab->node into dest. */ - khash = irref_isk(refkey) ? ir_khash(irkey) : 1; - if (khash == 0) { - emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node)); - } else { - Reg tmphash = tmp1; - if (irref_isk(refkey)) - tmphash = ra_allock(as, khash, allow); - emit_dst(as, MIPSI_ADDU, dest, dest, tmp1); - lua_assert(sizeof(Node) == 24); - emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1); - emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3); - emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5); - emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash); - emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); - if (irref_isk(refkey)) { - /* Nothing to do. */ - } else if (irt_isstr(kt)) { - emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, hash)); - } else { /* Must match with hash*() in lj_tab.c. */ - emit_dst(as, MIPSI_SUBU, tmp1, tmp1, tmp2); - emit_rotr(as, tmp2, tmp2, dest, (-HASH_ROT3)&31); - emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2); - emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31); - emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest); - if (irt_isnum(kt)) { - emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1); - if ((as->flags & JIT_F_MIPS32R2)) { - emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31); - } else { - emit_dst(as, MIPSI_OR, dest, dest, tmp1); - emit_dta(as, MIPSI_SLL, tmp1, tmp1, HASH_ROT1); - emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31); - } - emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1); - emit_tg(as, MIPSI_MFC1, tmp2, key); - emit_tg(as, MIPSI_MFC1, tmp1, key+1); - } else { - emit_dst(as, MIPSI_XOR, tmp2, key, tmp1); - emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31); - emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow)); - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - int32_t kofs = ofs + (int32_t)offsetof(Node, key); - Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); - Reg key = RID_NONE, type = RID_TMP, idx = node; - RegSet allow = rset_exclude(RSET_GPR, node); - int32_t lo, hi; - lua_assert(ofs % sizeof(Node) == 0); - if (ofs > 32736) { - idx = dest; - rset_clear(allow, dest); - kofs = (int32_t)offsetof(Node, key); - } else if (ra_hasreg(dest)) { - emit_tsi(as, MIPSI_ADDIU, dest, node, ofs); - } - if (!irt_ispri(irkey->t)) { - key = ra_scratch(as, allow); - rset_clear(allow, key); - } - if (irt_isnum(irkey->t)) { - lo = (int32_t)ir_knum(irkey)->u32.lo; - hi = (int32_t)ir_knum(irkey)->u32.hi; - } else { - lo = irkey->i; - hi = irt_toitype(irkey->t); - if (!ra_hasreg(key)) - goto nolo; - } - asm_guard(as, MIPSI_BNE, key, lo ? ra_allock(as, lo, allow) : RID_ZERO); -nolo: - asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO); - if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0)); - emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4)); - if (ofs > 32736) - emit_tsi(as, MIPSI_ADDU, dest, node, ra_allock(as, ofs, allow)); -} - -static void asm_newref(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; - IRRef args[3]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* GCtab *t */ - args[2] = ASMREF_TMP1; /* cTValue *key */ - asm_setupresult(as, ir, ci); /* TValue * */ - asm_gencall(as, ci, args); - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); - } -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - /* NYI: Check that UREFO is still open and not aliasing a slot. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_lsptr(as, MIPSI_LW, dest, v, RSET_GPR); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, MIPSI_ADDIU, dest, uv, (int32_t)offsetof(GCupval, tv)); - emit_tsi(as, MIPSI_LBU, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); - } else { - emit_tsi(as, MIPSI_LW, dest, uv, (int32_t)offsetof(GCupval, v)); - } - emit_tsi(as, MIPSI_LW, uv, func, - (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - UNUSED(as); UNUSED(ir); - lua_assert(!ra_used(ir)); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - IRRef ref = ir->op2, refk = ir->op1; - int32_t ofs = (int32_t)sizeof(GCstr); - Reg r; - if (irref_isk(ref)) { - IRRef tmp = refk; refk = ref; ref = tmp; - } else if (!irref_isk(refk)) { - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - IRIns *irr = IR(ir->op2); - if (ra_hasreg(irr->r)) { - ra_noweak(as, irr->r); - right = irr->r; - } else if (mayfuse(as, irr->op2) && - irr->o == IR_ADD && irref_isk(irr->op2) && - checki16(ofs + IR(irr->op2)->i)) { - ofs += IR(irr->op2)->i; - right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); - } else { - right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_tsi(as, MIPSI_ADDIU, dest, dest, ofs); - emit_dst(as, MIPSI_ADDU, dest, left, right); - return; - } - r = ra_alloc1(as, ref, RSET_GPR); - ofs += IR(refk)->i; - if (checki16(ofs)) - emit_tsi(as, MIPSI_ADDIU, dest, r, ofs); - else - emit_dst(as, MIPSI_ADDU, dest, r, - ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static MIPSIns asm_fxloadins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: return MIPSI_LB; - case IRT_U8: return MIPSI_LBU; - case IRT_I16: return MIPSI_LH; - case IRT_U16: return MIPSI_LHU; - case IRT_NUM: return MIPSI_LDC1; - case IRT_FLOAT: return MIPSI_LWC1; - default: return MIPSI_LW; - } -} - -static MIPSIns asm_fxstoreins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: return MIPSI_SB; - case IRT_I16: case IRT_U16: return MIPSI_SH; - case IRT_NUM: return MIPSI_SDC1; - case IRT_FLOAT: return MIPSI_SWC1; - default: return MIPSI_SW; - } -} - -static void asm_fload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx = ra_alloc1(as, ir->op1, RSET_GPR); - MIPSIns mi = asm_fxloadins(ir); - int32_t ofs; - if (ir->op2 == IRFL_TAB_ARRAY) { - ofs = asm_fuseabase(as, ir->op1); - if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ - emit_tsi(as, MIPSI_ADDIU, dest, idx, ofs); - return; - } - } - ofs = field_ofs[ir->op2]; - lua_assert(!irt_isfp(ir->t)); - emit_tsi(as, mi, dest, idx, ofs); -} - -static void asm_fstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1z(as, ir->op2, RSET_GPR); - IRIns *irf = IR(ir->op1); - Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); - int32_t ofs = field_ofs[irf->op2]; - MIPSIns mi = asm_fxstoreins(ir); - lua_assert(!irt_isfp(ir->t)); - emit_tsi(as, mi, src, idx, ofs); - } -} - -static void asm_xload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); - asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0); -} - -static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1z(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, - rset_exclude(RSET_GPR, src), ofs); - } -} - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - IRType1 t = ir->t; - Reg dest = RID_NONE, type = RID_TMP, idx; - RegSet allow = RSET_GPR; - int32_t ofs = 0; - if (ra_used(ir)) { - lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); - dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); - rset_clear(allow, dest); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - rset_clear(allow, idx); - if (irt_isnum(t)) { - asm_guard(as, MIPSI_BEQ, type, RID_ZERO); - emit_tsi(as, MIPSI_SLTIU, type, type, (int32_t)LJ_TISNUM); - if (ra_hasreg(dest)) - emit_hsi(as, MIPSI_LDC1, dest, idx, ofs); - } else { - asm_guard(as, MIPSI_BNE, type, ra_allock(as, irt_toitype(t), allow)); - if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0)); - } - emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4)); -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - Reg idx, src = RID_NONE, type = RID_NONE; - int32_t ofs = 0; - if (ir->r == RID_SINK) - return; - if (irt_isnum(ir->t)) { - src = ra_alloc1(as, ir->op2, RSET_FPR); - } else { - if (!irt_ispri(ir->t)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - } - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - rset_clear(allow, type); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - if (irt_isnum(ir->t)) { - emit_hsi(as, MIPSI_SDC1, src, idx, ofs); - } else { - if (ra_hasreg(src)) - emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0)); - emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4)); - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); - IRType1 t = ir->t; - Reg dest = RID_NONE, type = RID_NONE, base; - RegSet allow = RSET_GPR; - lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ - lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); - lua_assert(!irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { - dest = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, dest); - t.irt = IRT_NUM; /* Continue with a regular number type check. */ - } else if (ra_used(ir)) { - lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); - dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); - rset_clear(allow, dest); - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); - if ((ir->op2 & IRSLOAD_CONVERT)) { - if (irt_isint(t)) { - Reg tmp = ra_scratch(as, RSET_FPR); - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, MIPSI_CVT_W_D, tmp, tmp); - dest = tmp; - t.irt = IRT_NUM; /* Check for original type. */ - } else { - Reg tmp = ra_scratch(as, RSET_GPR); - emit_fg(as, MIPSI_CVT_D_W, dest, dest); - emit_tg(as, MIPSI_MTC1, tmp, dest); - dest = tmp; - t.irt = IRT_INT; /* Check for original type. */ - } - } - goto dotypecheck; - } - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); -dotypecheck: - if (irt_isnum(t)) { - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM); - type = RID_TMP; - } - if (ra_hasreg(dest)) emit_hsi(as, MIPSI_LDC1, dest, base, ofs); - } else { - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - Reg ktype = ra_allock(as, irt_toitype(t), allow); - asm_guard(as, MIPSI_BNE, RID_TMP, ktype); - type = RID_TMP; - } - if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0)); - } - if (ra_hasreg(type)) emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4)); -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; - CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? - lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[2]; - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - RegSet drop = RSET_SCRATCH; - lua_assert(sz != CTSIZE_INVALID); - - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - as->gcsteps++; - - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - if (ra_used(ir)) - ra_destreg(as, ir, RID_RET); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - int32_t ofs = sizeof(GCcdata); - lua_assert(sz == 4 || sz == 8); - if (sz == 8) { - ofs += 4; - lua_assert((ir+1)->o == IR_HIOP); - if (LJ_LE) ir++; - } - for (;;) { - Reg r = ra_alloc1z(as, ir->op2, allow); - emit_tsi(as, MIPSI_SW, r, RID_RET, ofs); - rset_clear(allow, r); - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; if (LJ_BE) ir++; else ir--; - } - } - /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ - emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); - emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid)); - emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA); - emit_ti(as, MIPSI_LI, RID_TMP, ctypeid); /* Lower 16 bit used. Sign-ext ok. */ - asm_gencall(as, ci, args); - ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), - ra_releasetmp(as, ASMREF_TMP1)); -} -#else -#define asm_cnew(as, ir) ((void)0) -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - Reg link = RID_TMP; - MCLabel l_end = emit_label(as); - emit_tsi(as, MIPSI_SW, link, tab, (int32_t)offsetof(GCtab, gclist)); - emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked)); - emit_setgl(as, tab, gc.grayagain); - emit_getgl(as, link, gc.grayagain); - emit_dst(as, MIPSI_XOR, mark, mark, RID_TMP); /* Clear black bit. */ - emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); - emit_tsi(as, MIPSI_ANDI, RID_TMP, mark, LJ_GC_BLACK); - emit_tsi(as, MIPSI_LBU, mark, tab, (int32_t)offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj, val, tmp; - /* No need for other object barriers (yet). */ - lua_assert(IR(ir->op1)->o == IR_UREFC); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - obj = IR(ir->op1)->r; - tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); - emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); - emit_tsi(as, MIPSI_ANDI, tmp, tmp, LJ_GC_BLACK); - emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); - emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, LJ_GC_WHITES); - val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); - emit_tsi(as, MIPSI_LBU, tmp, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); - emit_tsi(as, MIPSI_LBU, RID_TMP, val, (int32_t)offsetof(GChead, marked)); -} - -/* -- Arithmetic and logic operations ------------------------------------- */ - -static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - emit_fgh(as, mi, dest, left, right); -} - -static void asm_fpunary(ASMState *as, IRIns *ir, MIPSIns mi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); - emit_fg(as, mi, dest, left); -} - -static int asm_fpjoin_pow(ASMState *as, IRIns *ir) -{ - IRIns *irp = IR(ir->op1); - if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { - IRIns *irpp = IR(irp->op1); - if (irpp == ir-2 && irpp->o == IR_FPMATH && - irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; - IRRef args[2]; - args[0] = irpp->op1; - args[1] = irp->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); - return 1; - } - } - return 0; -} - -static void asm_add(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, MIPSI_ADD_D); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checki16(k)) { - emit_tsi(as, MIPSI_ADDIU, dest, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dst(as, MIPSI_ADDU, dest, left, right); - } -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, MIPSI_SUB_D); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_SUBU, dest, left, right); - } -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, MIPSI_MUL_D); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_MUL, dest, left, right); - } -} - -static void asm_neg(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fpunary(as, ir, MIPSI_NEG_D); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); - } -} - -static void asm_arithov(ASMState *as, IRIns *ir) -{ - Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op2)) { - int k = IR(ir->op2)->i; - if (ir->o == IR_SUBOV) k = -k; - if (checki16(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */ - left = ra_alloc1(as, ir->op1, RSET_GPR); - asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_dst(as, MIPSI_SLT, RID_TMP, dest, dest == left ? RID_TMP : left); - emit_tsi(as, MIPSI_ADDIU, dest, left, k); - if (dest == left) emit_move(as, RID_TMP, left); - return; - } - } - left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left), - right), dest)); - asm_guard(as, MIPSI_BLTZ, RID_TMP, 0); - emit_dst(as, MIPSI_AND, RID_TMP, RID_TMP, tmp); - if (ir->o == IR_ADDOV) { /* ((dest^left) & (dest^right)) < 0 */ - emit_dst(as, MIPSI_XOR, RID_TMP, dest, dest == right ? RID_TMP : right); - } else { /* ((dest^left) & (dest^~right)) < 0 */ - emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, dest); - emit_dst(as, MIPSI_NOR, RID_TMP, dest == right ? RID_TMP : right, RID_ZERO); - } - emit_dst(as, MIPSI_XOR, tmp, dest, dest == left ? RID_TMP : left); - emit_dst(as, ir->o == IR_ADDOV ? MIPSI_ADDU : MIPSI_SUBU, dest, left, right); - if (dest == left || dest == right) - emit_move(as, RID_TMP, dest == left ? left : right); -} - -static void asm_mulov(ASMState *as, IRIns *ir) -{ -#if LJ_DUALNUM -#error "NYI: MULOV" -#else - UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused in single-number mode. */ -#endif -} - -#if LJ_HASFFI -static void asm_add64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k == 0) { - emit_dst(as, MIPSI_ADDU, dest, left, RID_TMP); - goto loarith; - } else if (checki16(k)) { - emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP); - emit_tsi(as, MIPSI_ADDIU, dest, left, k); - goto loarith; - } - } - emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP); - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dst(as, MIPSI_ADDU, dest, left, right); -loarith: - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k == 0) { - if (dest != left) - emit_move(as, dest, left); - return; - } else if (checki16(k)) { - if (dest == left) { - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, left)); - emit_move(as, dest, tmp); - dest = tmp; - } - emit_dst(as, MIPSI_SLTU, RID_TMP, dest, left); - emit_tsi(as, MIPSI_ADDIU, dest, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - if (dest == left && dest == right) { - Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); - emit_move(as, dest, tmp); - dest = tmp; - } - emit_dst(as, MIPSI_SLTU, RID_TMP, dest, dest == left ? right : left); - emit_dst(as, MIPSI_ADDU, dest, left, right); -} - -static void asm_sub64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP); - emit_dst(as, MIPSI_SUBU, dest, left, right); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (dest == left) { - Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); - emit_move(as, dest, tmp); - dest = tmp; - } - emit_dst(as, MIPSI_SLTU, RID_TMP, left, dest); - emit_dst(as, MIPSI_SUBU, dest, left, right); -} - -static void asm_neg64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP); - emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_dst(as, MIPSI_SLTU, RID_TMP, RID_ZERO, dest); - emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); -} -#endif - -static void asm_bitnot(ASMState *as, IRIns *ir) -{ - Reg left, right, dest = ra_dest(as, ir, RSET_GPR); - IRIns *irl = IR(ir->op1); - if (mayfuse(as, ir->op1) && irl->o == IR_BOR) { - left = ra_alloc2(as, irl, RSET_GPR); - right = (left >> 8); left &= 255; - } else { - left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - right = RID_ZERO; - } - emit_dst(as, MIPSI_NOR, dest, left, right); -} - -static void asm_bitswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - if ((as->flags & JIT_F_MIPS32R2)) { - emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16); - emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left); - } else { - Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), dest)); - emit_dst(as, MIPSI_OR, dest, dest, tmp); - emit_dst(as, MIPSI_OR, dest, dest, RID_TMP); - emit_tsi(as, MIPSI_ANDI, dest, dest, 0xff00); - emit_dta(as, MIPSI_SLL, RID_TMP, RID_TMP, 8); - emit_dta(as, MIPSI_SRL, dest, left, 8); - emit_tsi(as, MIPSI_ANDI, RID_TMP, left, 0xff00); - emit_dst(as, MIPSI_OR, tmp, tmp, RID_TMP); - emit_dta(as, MIPSI_SRL, tmp, left, 24); - emit_dta(as, MIPSI_SLL, RID_TMP, left, 24); - } -} - -static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checku16(k)) { - emit_tsi(as, mik, dest, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dst(as, mi, dest, left, right); -} - -static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op2)) { /* Constant shifts. */ - uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31); - emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR), shift); - } else { - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, mi, dest, right, left); /* Shift amount is in rs. */ - } -} - -static void asm_bitror(ASMState *as, IRIns *ir) -{ - if ((as->flags & JIT_F_MIPS32R2)) { - asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op2)) { /* Constant shifts. */ - uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_rotr(as, dest, left, RID_TMP, shift); - } else { - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_OR, dest, dest, RID_TMP); - emit_dst(as, MIPSI_SRLV, dest, right, left); - emit_dst(as, MIPSI_SLLV, RID_TMP, RID_TMP, left); - emit_dst(as, MIPSI_SUBU, RID_TMP, ra_allock(as, 32, RSET_GPR), right); - } - } -} - -static void asm_min_max(ASMState *as, IRIns *ir, int ismax) -{ - if (irt_isnum(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - if (dest == left) { - emit_fg(as, MIPSI_MOVT_D, dest, right); - } else { - emit_fg(as, MIPSI_MOVF_D, dest, left); - if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right); - } - emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? left : right, ismax ? right : left); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (dest == left) { - emit_dst(as, MIPSI_MOVN, dest, right, RID_TMP); - } else { - emit_dst(as, MIPSI_MOVZ, dest, left, RID_TMP); - if (dest != right) emit_move(as, dest, right); - } - emit_dst(as, MIPSI_SLT, RID_TMP, - ismax ? left : right, ismax ? right : left); - } -} - -/* -- Comparisons --------------------------------------------------------- */ - -static void asm_comp(ASMState *as, IRIns *ir) -{ - /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */ - IROp op = ir->o; - if (irt_isnum(ir->t)) { - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); - emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right); - } else { - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - if (op == IR_ABC) op = IR_UGT; - if ((op&4) == 0 && irref_isk(ir->op2) && IR(ir->op2)->i == 0) { - MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) : - ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ); - asm_guard(as, mi, left, 0); - } else { - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if ((op&2)) k++; - if (checki16(k)) { - asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, (op&4) ? MIPSI_SLTIU : MIPSI_SLTI, - RID_TMP, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, - RID_TMP, (op&2) ? right : left, (op&2) ? left : right); - } - } -} - -static void asm_compeq(ASMState *as, IRIns *ir) -{ - Reg right, left = ra_alloc2(as, ir, irt_isnum(ir->t) ? RSET_FPR : RSET_GPR); - right = (left >> 8); left &= 255; - if (irt_isnum(ir->t)) { - asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); - emit_fgh(as, MIPSI_C_EQ_D, 0, left, right); - } else { - asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right); - } -} - -#if LJ_HASFFI -/* 64 bit integer comparisons. */ -static void asm_comp64(ASMState *as, IRIns *ir) -{ - /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */ - IROp op = (ir-1)->o; - MCLabel l_end; - Reg rightlo, leftlo, righthi, lefthi = ra_alloc2(as, ir, RSET_GPR); - righthi = (lefthi >> 8); lefthi &= 255; - leftlo = ra_alloc2(as, ir-1, - rset_exclude(rset_exclude(RSET_GPR, lefthi), righthi)); - rightlo = (leftlo >> 8); leftlo &= 255; - asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - l_end = emit_label(as); - if (lefthi != righthi) - emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, RID_TMP, - (op&2) ? righthi : lefthi, (op&2) ? lefthi : righthi); - emit_dst(as, MIPSI_SLTU, RID_TMP, - (op&2) ? rightlo : leftlo, (op&2) ? leftlo : rightlo); - if (lefthi != righthi) - emit_branch(as, MIPSI_BEQ, lefthi, righthi, l_end); -} - -static void asm_comp64eq(ASMState *as, IRIns *ir) -{ - Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - asm_guard(as, ((ir-1)->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_TMP, RID_ZERO); - tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); - emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp); - emit_dst(as, MIPSI_XOR, tmp, left, right); - left = ra_alloc2(as, ir-1, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_XOR, RID_TMP, left, right); -} -#endif - -/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ - -/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ -#if LJ_HASFFI - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; - if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ - as->curins--; /* Always skip the CONV. */ - if (usehi || uselo) - asm_conv64(as, ir); - return; - } else if ((ir-1)->o < IR_EQ) { /* 64 bit integer comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ - asm_comp64(as, ir); - return; - } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ - asm_comp64eq(as, ir); - return; - } else if ((ir-1)->o == IR_XSTORE) { - as->curins--; /* Handle both stores here. */ - if ((ir-1)->r != RID_SINK) { - asm_xstore(as, ir, LJ_LE ? 4 : 0); - asm_xstore(as, ir-1, LJ_LE ? 0 : 4); - } - return; - } - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { - case IR_ADD: as->curins--; asm_add64(as, ir); break; - case IR_SUB: as->curins--; asm_sub64(as, ir); break; - case IR_NEG: as->curins--; asm_neg64(as, ir); break; - case IR_CALLN: - case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - case IR_CNEWI: - /* Nothing to do here. Handled by lo op itself. */ - break; - default: lua_assert(0); break; - } -#else - UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */ -#endif -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */ - Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; - ExitNo oldsnap = as->snapno; - rset_clear(allow, pbase); - tmp = allow ? rset_pickbot(allow) : - (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); - as->snapno = exitno; - asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO); - as->snapno = oldsnap; - if (allow == RSET_EMPTY) /* Restore temp. register. */ - emit_tsi(as, MIPSI_LW, tmp, RID_SP, 0); - else - ra_modified(as, tmp); - emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot)); - emit_dst(as, MIPSI_SUBU, RID_TMP, tmp, pbase); - emit_tsi(as, MIPSI_LW, tmp, tmp, offsetof(lua_State, maxstack)); - if (pbase == RID_TMP) - emit_getgl(as, RID_TMP, jit_base); - emit_getgl(as, tmp, jit_L); - if (allow == RSET_EMPTY) /* Spill temp. register. */ - emit_tsi(as, MIPSI_SW, tmp, RID_SP, 0); -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if (irt_isnum(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs); - } else { - Reg type; - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, allow); - rset_clear(allow, src); - emit_tsi(as, MIPSI_SW, src, RID_BASE, ofs+(LJ_BE?4:0)); - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { - if (s == 0) continue; /* Do not overwrite link to previous frame. */ - type = ra_allock(as, (int32_t)(*flinks--), allow); - } else { - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - } - emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4)); - } - checkmclim(as); - } - lua_assert(map + nent == flinks); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - /* Assumes asm_snap_prep() already done. */ - asm_guard(as, MIPSI_BNE, RID_RET, RID_ZERO); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - tmp = ra_releasetmp(as, ASMREF_TMP2); - emit_loadi(as, tmp, as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_branch(as, MIPSI_BNE, RID_TMP, RID_ZERO, l_end); - emit_dst(as, MIPSI_SLTU, RID_TMP, RID_TMP, tmp); - emit_getgl(as, tmp, gc.threshold); - emit_getgl(as, RID_TMP, gc.total); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - p[-1] = MIPSI_NOP; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guard already inverted the cond branch. Only patch the target. */ - p[-3] |= ((target-p+2) & 0x0000ffffu); - } else { - p[-2] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); - } -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (as->loopinv) as->mctop--; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (r != RID_BASE) - emit_move(as, r, RID_BASE); - } -} - -/* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (as->loopinv) as->mctop--; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ - } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - rset_clear(allow, irp->r); - emit_move(as, r, irp->r); /* Move from coalesced parent reg. */ - } else { - emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ - } - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp; - int32_t spadj = as->T->spadjust; - MCode *p = as->mctop-1; - *p = spadj ? (MIPSI_ADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP; - p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - as->mcp = as->mctop-2; /* Leave room for branch plus nop or stack adj. */ - as->invmcp = as->loopref ? as->mcp : NULL; -} - -/* -- Instruction dispatch ------------------------------------------------ */ - -/* Assemble a single instruction. */ -static void asm_ir(ASMState *as, IRIns *ir) -{ - switch ((IROp)ir->o) { - /* Miscellaneous ops. */ - case IR_LOOP: asm_loop(as); break; - case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; - case IR_USE: - ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; - case IR_PHI: asm_phi(as, ir); break; - case IR_HIOP: asm_hiop(as, ir); break; - case IR_GCSTEP: asm_gcstep(as, ir); break; - - /* Guarded assertions. */ - case IR_EQ: case IR_NE: asm_compeq(as, ir); break; - case IR_LT: case IR_GE: case IR_LE: case IR_GT: - case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: - case IR_ABC: - asm_comp(as, ir); - break; - - case IR_RETF: asm_retf(as, ir); break; - - /* Bit ops. */ - case IR_BNOT: asm_bitnot(as, ir); break; - case IR_BSWAP: asm_bitswap(as, ir); break; - - case IR_BAND: asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI); break; - case IR_BOR: asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI); break; - case IR_BXOR: asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI); break; - - case IR_BSHL: asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL); break; - case IR_BSHR: asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL); break; - case IR_BSAR: asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA); break; - case IR_BROL: lua_assert(0); break; - case IR_BROR: asm_bitror(as, ir); break; - - /* Arithmetic ops. */ - case IR_ADD: asm_add(as, ir); break; - case IR_SUB: asm_sub(as, ir); break; - case IR_MUL: asm_mul(as, ir); break; - case IR_DIV: asm_fparith(as, ir, MIPSI_DIV_D); break; - case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break; - case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break; - case IR_NEG: asm_neg(as, ir); break; - - case IR_ABS: asm_fpunary(as, ir, MIPSI_ABS_D); break; - case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break; - case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break; - case IR_MIN: asm_min_max(as, ir, 0); break; - case IR_MAX: asm_min_max(as, ir, 1); break; - case IR_FPMATH: - if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) - break; - if (ir->op2 <= IRFPM_TRUNC) - asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2); - else if (ir->op2 == IRFPM_SQRT) - asm_fpunary(as, ir, MIPSI_SQRT_D); - else - asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); - break; - - /* Overflow-checking arithmetic ops. */ - case IR_ADDOV: asm_arithov(as, ir); break; - case IR_SUBOV: asm_arithov(as, ir); break; - case IR_MULOV: asm_mulov(as, ir); break; - - /* Memory references. */ - case IR_AREF: asm_aref(as, ir); break; - case IR_HREF: asm_href(as, ir); break; - case IR_HREFK: asm_hrefk(as, ir); break; - case IR_NEWREF: asm_newref(as, ir); break; - case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; - case IR_FREF: asm_fref(as, ir); break; - case IR_STRREF: asm_strref(as, ir); break; - - /* Loads and stores. */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - asm_ahuvload(as, ir); - break; - case IR_FLOAD: asm_fload(as, ir); break; - case IR_XLOAD: asm_xload(as, ir); break; - case IR_SLOAD: asm_sload(as, ir); break; - - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; - case IR_FSTORE: asm_fstore(as, ir); break; - case IR_XSTORE: asm_xstore(as, ir, 0); break; - - /* Allocations. */ - case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; - case IR_TNEW: asm_tnew(as, ir); break; - case IR_TDUP: asm_tdup(as, ir); break; - case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; - - /* Write barriers. */ - case IR_TBAR: asm_tbar(as, ir); break; - case IR_OBAR: asm_obar(as, ir); break; - - /* Type conversions. */ - case IR_CONV: asm_conv(as, ir); break; - case IR_TOBIT: asm_tobit(as, ir); break; - case IR_TOSTR: asm_tostr(as, ir); break; - case IR_STRTO: asm_strto(as, ir); break; - - /* Calls. */ - case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; - case IR_CALLXS: asm_callx(as, ir); break; - case IR_CARG: break; - - default: - setintV(&as->J->errinfo, ir->o); - lj_trace_err_info(as->J, LJ_TRERR_NYIIR); - break; - } -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - uint32_t i, nargs = (int)CCI_NARGS(ci); - int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; - asm_collectargs(as, ir, ci, args); - for (i = 0; i < nargs; i++) { - if (args[i] && irt_isfp(IR(args[i])->t) && - nfpr > 0 && !(ci->flags & CCI_VARARG)) { - nfpr--; - ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1; - } else if (args[i] && irt_isnum(IR(args[i])->t)) { - nfpr = 0; - ngpr = ngpr & ~1; - if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1; - } else { - nfpr = 0; - if (ngpr > 0) ngpr--; else nslots++; - } - } - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; - return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); -} - -static void asm_setup_target(ASMState *as) -{ - asm_sparejump_setup(as); - asm_exitstub_setup(as); -} - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *pe = (MCode *)((char *)p + T->szmcode); - MCode *px = exitstub_trace_addr(T, exitno); - MCode *cstart = NULL, *cstop = NULL; - MCode *mcarea = lj_mcode_patch(J, p, 0); - MCode exitload = MIPSI_LI | MIPSF_T(RID_TMP) | exitno; - MCode tjump = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); - for (p++; p < pe; p++) { - if (*p == exitload) { /* Look for load of exit number. */ - if (((p[-1] ^ (px-p)) & 0xffffu) == 0) { /* Look for exitstub branch. */ - ptrdiff_t delta = target - p; - if (((delta + 0x8000) >> 16) == 0) { /* Patch in-range branch. */ - patchbranch: - p[-1] = (p[-1] & 0xffff0000u) | (delta & 0xffffu); - *p = MIPSI_NOP; /* Replace the load of the exit number. */ - cstop = p; - if (!cstart) cstart = p-1; - } else { /* Branch out of range. Use spare jump slot in mcarea. */ - int i; - for (i = 2; i < 2+MIPS_SPAREJUMP*2; i += 2) { - if (mcarea[i] == tjump) { - delta = mcarea+i - p; - goto patchbranch; - } else if (mcarea[i] == MIPSI_NOP) { - mcarea[i] = tjump; - cstart = mcarea+i; - delta = mcarea+i - p; - goto patchbranch; - } - } - /* Ignore jump slot overflow. Child trace is simply not attached. */ - } - } else if (p+1 == pe) { - /* Patch NOP after code for inverted loop branch. Use of J is ok. */ - lua_assert(p[1] == MIPSI_NOP); - p[1] = tjump; - *p = MIPSI_NOP; /* Replace the load of the exit number. */ - cstop = p+2; - if (!cstart) cstart = p+1; - } - } - } - if (cstart) lj_mcode_sync(cstart, cstop); - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_asm_ppc.h b/third-party/LuaJIT-2.0.2/src/lj_asm_ppc.h deleted file mode 100644 index 651fa3187a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_asm_ppc.h +++ /dev/null @@ -1,2166 +0,0 @@ -/* -** PPC IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Register allocator extensions --------------------------------------- */ - -/* Allocate a register with a hint. */ -static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!ra_hashint(r) && !iscrossref(as, ref)) - ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ - r = ra_allocref(as, ref, allow); - } - ra_noweak(as, r); - return r; -} - -/* Allocate two source registers for three-operand instructions. */ -static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - Reg left = irl->r, right = irr->r; - if (ra_hasreg(left)) { - ra_noweak(as, left); - if (ra_noreg(right)) - right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); - else - ra_noweak(as, right); - } else if (ra_hasreg(right)) { - ra_noweak(as, right); - left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); - } else if (ra_hashint(right)) { - right = ra_allocref(as, ir->op2, allow); - left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); - } else { - left = ra_allocref(as, ir->op1, allow); - right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); - } - return left | (right << 8); -} - -/* -- Guard handling ------------------------------------------------------ */ - -/* Setup exit stubs after the end of each trace. */ -static void asm_exitstub_setup(ASMState *as, ExitNo nexits) -{ - ExitNo i; - MCode *mxp = as->mctop; - /* 1: mflr r0; bl ->vm_exit_handler; li r0, traceno; bl <1; bl <1; ... */ - for (i = nexits-1; (int32_t)i >= 0; i--) - *--mxp = PPCI_BL|(((-3-i)&0x00ffffffu)<<2); - *--mxp = PPCI_LI|PPCF_T(RID_TMP)|as->T->traceno; /* Read by exit handler. */ - mxp--; - *mxp = PPCI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)&0x00ffffffu)<<2); - *--mxp = PPCI_MFLR|PPCF_T(RID_TMP); - as->mctop = mxp; -} - -static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno) -{ - /* Keep this in-sync with exitstub_trace_addr(). */ - return as->mctop + exitno + 3; -} - -/* Emit conditional branch to exit for guard. */ -static void asm_guardcc(ASMState *as, PPCCC cc) -{ - MCode *target = asm_exitstub_addr(as, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *p = PPCI_B | (((target-p) & 0x00ffffffu) << 2); - emit_condbranch(as, PPCI_BC, cc^4, p); - return; - } - emit_condbranch(as, PPCI_BC, cc, target); -} - -/* -- Operand fusion ------------------------------------------------------ */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if there's no conflicting instruction between curins and ref. */ -static int noconflict(ASMState *as, IRRef ref, IROp conflict) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - return 1; /* Ok, no conflict. */ -} - -/* Fuse the array base of colocated arrays. */ -static int32_t asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) - return (int32_t)sizeof(GCtab); - return 0; -} - -/* Indicates load/store indexed is ok. */ -#define AHUREF_LSX ((int32_t)0x80000000) - -/* Fuse array/hash/upvalue reference into register+offset operand. */ -static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - if (ir->o == IR_AREF) { - if (mayfuse(as, ref)) { - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, refa, allow); - } - } - if (*ofsp == AHUREF_LSX) { - Reg base = ra_alloc1(as, ir->op1, allow); - Reg idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - return base | (idx << 8); - } - } - } else if (ir->o == IR_HREFK) { - if (mayfuse(as, ref)) { - int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, ir->op1, allow); - } - } - } else if (ir->o == IR_UREFC) { - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); - int32_t jgl = (intptr_t)J2G(as->J); - if ((uint32_t)(ofs-jgl) < 65536) { - *ofsp = ofs-jgl-32768; - return RID_JGL; - } else { - *ofsp = (int16_t)ofs; - return ra_allock(as, ofs-(int16_t)ofs, allow); - } - } - } - } - *ofsp = 0; - return ra_alloc1(as, ref, allow); -} - -/* Fuse XLOAD/XSTORE reference into load/store operand. */ -static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref, - RegSet allow, int32_t ofs) -{ - IRIns *ir = IR(ref); - Reg base; - if (ra_noreg(ir->r) && canfuse(as, ir)) { - if (ir->o == IR_ADD) { - int32_t ofs2; - if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) { - ofs = ofs2; - ref = ir->op1; - } else if (ofs == 0) { - Reg right, left = ra_alloc2(as, ir, allow); - right = (left >> 8); left &= 255; - emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); - return; - } - } else if (ir->o == IR_STRREF) { - lua_assert(ofs == 0); - ofs = (int32_t)sizeof(GCstr); - if (irref_isk(ir->op2)) { - ofs += IR(ir->op2)->i; - ref = ir->op1; - } else if (irref_isk(ir->op1)) { - ofs += IR(ir->op1)->i; - ref = ir->op2; - } else { - /* NYI: Fuse ADD with constant. */ - Reg tmp, right, left = ra_alloc2(as, ir, allow); - right = (left >> 8); left &= 255; - tmp = ra_scratch(as, rset_exclude(rset_exclude(allow, left), right)); - emit_fai(as, pi, rt, tmp, ofs); - emit_tab(as, PPCI_ADD, tmp, left, right); - return; - } - if (!checki16(ofs)) { - Reg left = ra_alloc1(as, ref, allow); - Reg right = ra_allock(as, ofs, rset_exclude(allow, left)); - emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); - return; - } - } - } - base = ra_alloc1(as, ref, allow); - emit_fai(as, pi, rt, base, ofs); -} - -/* Fuse XLOAD/XSTORE reference into indexed-only load/store operand. */ -static void asm_fusexrefx(ASMState *as, PPCIns pi, Reg rt, IRRef ref, - RegSet allow) -{ - IRIns *ira = IR(ref); - Reg right, left; - if (canfuse(as, ira) && ira->o == IR_ADD && ra_noreg(ira->r)) { - left = ra_alloc2(as, ira, allow); - right = (left >> 8); left &= 255; - } else { - right = ra_alloc1(as, ref, allow); - left = RID_R0; - } - emit_tab(as, pi, rt, left, right); -} - -/* Fuse to multiply-add/sub instruction. */ -static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir) -{ - IRRef lref = ir->op1, rref = ir->op2; - IRIns *irm; - if (lref != rref && - ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && - ra_noreg(irm->r)) || - (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && - (rref = lref, pi = pir, ra_noreg(irm->r))))) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg add = ra_alloc1(as, rref, RSET_FPR); - Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add)); - right = (left >> 8); left &= 255; - emit_facb(as, pi, dest, left, right, add); - return 1; - } - return 0; -} - -/* -- Calls --------------------------------------------------------------- */ - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_NARGS(ci); - int32_t ofs = 8; - Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR; - if ((void *)ci->func) - emit_call(as, (void *)ci->func); - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - if (ref) { - IRIns *ir = IR(ref); - if (irt_isfp(ir->t)) { - if (fpr <= REGARG_LASTFPR) { - lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */ - ra_leftov(as, fpr, ref); - fpr++; - } else { - Reg r = ra_alloc1(as, ref, RSET_FPR); - if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; - emit_spstore(as, ir, r, ofs); - ofs += irt_isnum(ir->t) ? 8 : 4; - } - } else { - if (gpr <= REGARG_LASTGPR) { - lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */ - ra_leftov(as, gpr, ref); - gpr++; - } else { - Reg r = ra_alloc1(as, ref, RSET_GPR); - emit_spstore(as, ir, r, ofs); - ofs += 4; - } - } - } else { - if (gpr <= REGARG_LASTGPR) - gpr++; - else - ofs += 4; - } - checkmclim(as); - } - if ((ci->flags & CCI_VARARG)) /* Vararg calls need to know about FPR use. */ - emit_tab(as, fpr == REGARG_FIRSTFPR ? PPCI_CRXOR : PPCI_CREQV, 6, 6, 6); -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP); - if ((ci->flags & CCI_NOFPRCLOBBER)) - drop &= ~RSET_FPR; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - lua_assert(!irt_ispri(ir->t)); - if (irt_isfp(ir->t)) { - if ((ci->flags & CCI_CASTU64)) { - /* Use spill slot or temp slots. */ - int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP; - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_fai(as, PPCI_LFD, dest, RID_SP, ofs); - } - emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs); - emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4); - } else { - ra_destreg(as, ir, RID_FPRET); - } - } else if (hiop) { - ra_destpair(as, ir); - } else { - ra_destreg(as, ir, RID_RET); - } - } -} - -static void asm_call(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX]; - const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; - asm_collectargs(as, ir, ci, args); - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - if (irref_isk(func)) { /* Call to constant address. */ - ci.func = (ASMFunction)(void *)(irf->i); - } else { /* Need a non-argument register for indirect calls. */ - RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); - Reg freg = ra_alloc1(as, func, allow); - *--as->mcp = PPCI_BCTRL; - *--as->mcp = PPCI_MTCTR | PPCF_T(freg); - ci.func = (ASMFunction)(void *)0; - } - asm_gencall(as, &ci, args); -} - -static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) -{ - const CCallInfo *ci = &lj_ir_callinfo[id]; - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guardcc(as, CC_NE); - emit_ab(as, PPCI_CMPW, RID_TMP, - ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); - emit_tai(as, PPCI_LWZ, RID_TMP, base, -8); -} - -/* -- Type conversions ---------------------------------------------------- */ - -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - RegSet allow = RSET_FPR; - Reg tmp = ra_scratch(as, rset_clear(allow, left)); - Reg fbias = ra_scratch(as, rset_clear(allow, tmp)); - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest)); - asm_guardcc(as, CC_NE); - emit_fab(as, PPCI_FCMPU, 0, tmp, left); - emit_fab(as, PPCI_FSUB, tmp, tmp, fbias); - emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP); - emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO); - emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); - emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000); - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - emit_lsptr(as, PPCI_LFS, (fbias & 31), - (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)), - RSET_GPR); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, tmp, left); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_FPR; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, allow); - Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); - Reg tmp = ra_scratch(as, rset_clear(allow, right)); - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fab(as, PPCI_FADD, tmp, left, right); -} - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); - int stfp = (st == IRT_NUM || st == IRT_FLOAT); - IRRef lref = ir->op1; - lua_assert(irt_type(ir->t) != st); - lua_assert(!(irt_isint64(ir->t) || - (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */ - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - if (st == IRT_NUM) /* double -> float conversion. */ - emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR)); - else /* float -> double conversion is a no-op on PPC. */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } else { /* Integer to FP conversion. */ - /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */ - /* IRT_U32: Bias with 2^52, subtract 2^52. */ - RegSet allow = RSET_GPR; - Reg left = ra_alloc1(as, lref, allow); - Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left)); - Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - const float *kbias; - if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest); - emit_fab(as, PPCI_FSUB, dest, dest, fbias); - emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); - kbias = (const float *)lj_ir_k64_find(as->J, U64x(59800004,59800000)); - if (st == IRT_U32) kbias++; - emit_lsptr(as, PPCI_LFS, (fbias & 31), (void *)kbias, - rset_clear(allow, hibias)); - emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP, - RID_SP, SPOFS_TMPLO); - emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); - if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000); - } - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lua_assert(irt_isint(ir->t) && st == IRT_NUM); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, lref, RSET_FPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - if (irt_isu32(ir->t)) { - /* Convert both x and x-2^31 to int and merge results. */ - Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest)); - emit_asb(as, PPCI_OR, dest, dest, tmpi); /* Select with mask idiom. */ - emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP); - emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP); - emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO); /* tmp = (int)(x) */ - emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000); /* dest += 2^31 */ - emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31); /* mask = -(dest < 0) */ - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_tai(as, PPCI_LWZ, dest, - RID_SP, SPOFS_TMPLO); /* dest = (int)(x-2^31) */ - emit_fb(as, PPCI_FCTIWZ, tmp, left); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, tmp, tmp); - emit_fab(as, PPCI_FSUB, tmp, left, tmp); - emit_lsptr(as, PPCI_LFS, (tmp & 31), - (void *)lj_ir_k64_find(as->J, U64x(4f000000,00000000)), - RSET_GPR); - } else { - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, tmp, left); - } - } - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); - if ((ir->op2 & IRCONV_SEXT)) - emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left); - else - emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31); - } else { /* 32/64 bit integer conversions. */ - /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } -} - -#if LJ_HASFFI -static void asm_conv64(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); - IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); - IRCallID id; - const CCallInfo *ci; - IRRef args[2]; - args[0] = ir->op1; - args[1] = (ir-1)->op1; - if (st == IRT_NUM || st == IRT_FLOAT) { - id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); - ir--; - } else { - id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); - } - ci = &lj_ir_callinfo[id]; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} -#endif - -static void asm_strto(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - int32_t ofs; - RegSet drop = RSET_SCRATCH; - if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ - ra_evictset(as, drop); - asm_guardcc(as, CC_EQ); - emit_ai(as, PPCI_CMPWI, RID_RET, 0); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - /* Store the result to the spill slot or temp slots. */ - ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP; - emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs); -} - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) -{ - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if (irref_isk(ref)) /* Use the number constant itself as a TValue. */ - ra_allockreg(as, i32ptr(ir_knum(ir)), dest); - else /* Otherwise force a spill and use the spill slot. */ - emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir)); - } else { - /* Otherwise use g->tmptv to hold the TValue. */ - RegSet allow = rset_exclude(RSET_GPR, dest); - Reg type; - emit_tai(as, PPCI_ADDI, dest, RID_JGL, offsetof(global_State, tmptv)-32768); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, allow); - emit_setgl(as, src, tmptv.gcr); - } - type = ra_allock(as, irt_toitype(ir->t), allow); - emit_setgl(as, type, tmptv.it); - } -} - -static void asm_tostr(ASMState *as, IRIns *ir) -{ - IRRef args[2]; - args[0] = ASMREF_L; - as->gcsteps++; - if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; - args[1] = ASMREF_TMP1; /* const lua_Number * */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); - } else { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; - args[1] = ir->op1; /* int32_t k */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - } -} - -/* -- Memory references --------------------------------------------------- */ - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx, base; - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - base = ra_alloc1(as, refa, RSET_GPR); - emit_tai(as, PPCI_ADDI, dest, base, ofs); - return; - } - } - base = ra_alloc1(as, ir->op1, RSET_GPR); - idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - emit_tab(as, PPCI_ADD, dest, RID_TMP, base); - emit_slwi(as, RID_TMP, idx, 3); -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir, IROp merge) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = RID_NONE, tmp1 = RID_TMP, tmp2; - Reg tisnum = RID_NONE, tmpnum = RID_NONE; - IRRef refkey = ir->op2; - IRIns *irkey = IR(refkey); - IRType1 kt = irkey->t; - uint32_t khash; - MCLabel l_end, l_loop, l_next; - - rset_clear(allow, tab); - if (irt_isnum(kt)) { - key = ra_alloc1(as, refkey, RSET_FPR); - tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); - tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); - rset_clear(allow, tisnum); - } else if (!irt_ispri(kt)) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } - tmp2 = ra_scratch(as, allow); - rset_clear(allow, tmp2); - - /* Key not found in chain: jump to exit (if merged) or load niltv. */ - l_end = emit_label(as); - as->invmcp = NULL; - if (merge == IR_NE) - asm_guardcc(as, CC_EQ); - else if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - - /* Follow hash chain until the end. */ - l_loop = --as->mcp; - emit_ai(as, PPCI_CMPWI, dest, 0); - emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next)); - l_next = emit_label(as); - - /* Type and value comparison. */ - if (merge == IR_EQ) - asm_guardcc(as, CC_EQ); - else - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); - if (irt_isnum(kt)) { - emit_fab(as, PPCI_FCMPU, 0, tmpnum, key); - emit_condbranch(as, PPCI_BC, CC_GE, l_next); - emit_ab(as, PPCI_CMPLW, tmp1, tisnum); - emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n)); - } else { - if (!irt_ispri(kt)) { - emit_ab(as, PPCI_CMPW, tmp2, key); - emit_condbranch(as, PPCI_BC, CC_NE, l_next); - } - emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t)); - if (!irt_ispri(kt)) - emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr)); - } - emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it)); - *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) | - (((char *)as->mcp-(char *)l_loop) & 0xffffu); - - /* Load main position relative to tab->node into dest. */ - khash = irref_isk(refkey) ? ir_khash(irkey) : 1; - if (khash == 0) { - emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); - } else { - Reg tmphash = tmp1; - if (irref_isk(refkey)) - tmphash = ra_allock(as, khash, allow); - emit_tab(as, PPCI_ADD, dest, dest, tmp1); - emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node)); - emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash); - emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); - if (irref_isk(refkey)) { - /* Nothing to do. */ - } else if (irt_isstr(kt)) { - emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, hash)); - } else { /* Must match with hash*() in lj_tab.c. */ - emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1); - emit_rotlwi(as, tmp2, tmp2, HASH_ROT3); - emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2); - emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31); - emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2); - if (irt_isnum(kt)) { - int32_t ofs = ra_spill(as, irkey); - emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1); - emit_rotlwi(as, dest, tmp1, HASH_ROT1); - emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1); - emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4); - emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs); - } else { - emit_asb(as, PPCI_XOR, tmp2, key, tmp1); - emit_rotlwi(as, dest, tmp1, HASH_ROT1); - emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS); - emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16); - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - int32_t kofs = ofs + (int32_t)offsetof(Node, key); - Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); - Reg key = RID_NONE, type = RID_TMP, idx = node; - RegSet allow = rset_exclude(RSET_GPR, node); - lua_assert(ofs % sizeof(Node) == 0); - if (ofs > 32736) { - idx = dest; - rset_clear(allow, dest); - kofs = (int32_t)offsetof(Node, key); - } else if (ra_hasreg(dest)) { - emit_tai(as, PPCI_ADDI, dest, node, ofs); - } - asm_guardcc(as, CC_NE); - if (!irt_ispri(irkey->t)) { - key = ra_scratch(as, allow); - rset_clear(allow, key); - } - rset_clear(allow, type); - if (irt_isnum(irkey->t)) { - emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo); - asm_guardcc(as, CC_NE); - emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi); - } else { - if (ra_hasreg(key)) { - emit_cmpi(as, key, irkey->i); /* May use RID_TMP, i.e. type. */ - asm_guardcc(as, CC_NE); - } - emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t)); - } - if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4); - emit_tai(as, PPCI_LWZ, type, idx, kofs); - if (ofs > 32736) { - emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16); - emit_tai(as, PPCI_ADDI, dest, node, ofs); - } -} - -static void asm_newref(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; - IRRef args[3]; - if (ir->r == RID_SINK) - return; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* GCtab *t */ - args[2] = ASMREF_TMP1; /* cTValue *key */ - asm_setupresult(as, ir, ci); /* TValue * */ - asm_gencall(as, ci, args); - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - /* NYI: Check that UREFO is still open and not aliasing a slot. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - asm_guardcc(as, CC_NE); - emit_ai(as, PPCI_CMPWI, RID_TMP, 1); - emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv)); - emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); - } else { - emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v)); - } - emit_tai(as, PPCI_LWZ, uv, func, - (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - UNUSED(as); UNUSED(ir); - lua_assert(!ra_used(ir)); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - IRRef ref = ir->op2, refk = ir->op1; - int32_t ofs = (int32_t)sizeof(GCstr); - Reg r; - if (irref_isk(ref)) { - IRRef tmp = refk; refk = ref; ref = tmp; - } else if (!irref_isk(refk)) { - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - IRIns *irr = IR(ir->op2); - if (ra_hasreg(irr->r)) { - ra_noweak(as, irr->r); - right = irr->r; - } else if (mayfuse(as, irr->op2) && - irr->o == IR_ADD && irref_isk(irr->op2) && - checki16(ofs + IR(irr->op2)->i)) { - ofs += IR(irr->op2)->i; - right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); - } else { - right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_tai(as, PPCI_ADDI, dest, dest, ofs); - emit_tab(as, PPCI_ADD, dest, left, right); - return; - } - r = ra_alloc1(as, ref, RSET_GPR); - ofs += IR(refk)->i; - if (checki16(ofs)) - emit_tai(as, PPCI_ADDI, dest, r, ofs); - else - emit_tab(as, PPCI_ADD, dest, r, - ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static PPCIns asm_fxloadins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: return PPCI_LBZ; /* Needs sign-extension. */ - case IRT_U8: return PPCI_LBZ; - case IRT_I16: return PPCI_LHA; - case IRT_U16: return PPCI_LHZ; - case IRT_NUM: return PPCI_LFD; - case IRT_FLOAT: return PPCI_LFS; - default: return PPCI_LWZ; - } -} - -static PPCIns asm_fxstoreins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: return PPCI_STB; - case IRT_I16: case IRT_U16: return PPCI_STH; - case IRT_NUM: return PPCI_STFD; - case IRT_FLOAT: return PPCI_STFS; - default: return PPCI_STW; - } -} - -static void asm_fload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx = ra_alloc1(as, ir->op1, RSET_GPR); - PPCIns pi = asm_fxloadins(ir); - int32_t ofs; - if (ir->op2 == IRFL_TAB_ARRAY) { - ofs = asm_fuseabase(as, ir->op1); - if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ - emit_tai(as, PPCI_ADDI, dest, idx, ofs); - return; - } - } - ofs = field_ofs[ir->op2]; - lua_assert(!irt_isi8(ir->t)); - emit_tai(as, pi, dest, idx, ofs); -} - -static void asm_fstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, RSET_GPR); - IRIns *irf = IR(ir->op1); - Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); - int32_t ofs = field_ofs[irf->op2]; - PPCIns pi = asm_fxstoreins(ir); - emit_tai(as, pi, src, idx, ofs); - } -} - -static void asm_xload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); - if (irt_isi8(ir->t)) - emit_as(as, PPCI_EXTSB, dest, dest); - asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0); -} - -static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs) -{ - IRIns *irb; - if (ir->r == RID_SINK) - return; - if (ofs == 0 && mayfuse(as, ir->op2) && (irb = IR(ir->op2))->o == IR_BSWAP && - ra_noreg(irb->r) && (irt_isint(ir->t) || irt_isu32(ir->t))) { - /* Fuse BSWAP with XSTORE to stwbrx. */ - Reg src = ra_alloc1(as, irb->op1, RSET_GPR); - asm_fusexrefx(as, PPCI_STWBRX, src, ir->op1, rset_exclude(RSET_GPR, src)); - } else { - Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, - rset_exclude(RSET_GPR, src), ofs); - } -} - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - IRType1 t = ir->t; - Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx; - RegSet allow = RSET_GPR; - int32_t ofs = AHUREF_LSX; - if (ra_used(ir)) { - lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); - if (!irt_isnum(t)) ofs = 0; - dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); - rset_clear(allow, dest); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - if (irt_isnum(t)) { - Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx)); - asm_guardcc(as, CC_GE); - emit_ab(as, PPCI_CMPLW, type, tisnum); - if (ra_hasreg(dest)) { - if (ofs == AHUREF_LSX) { - tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, - (idx&255)), (idx>>8))); - emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp); - } else { - emit_fai(as, PPCI_LFD, dest, idx, ofs); - } - } - } else { - asm_guardcc(as, CC_NE); - emit_ai(as, PPCI_CMPWI, type, irt_toitype(t)); - if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4); - } - if (ofs == AHUREF_LSX) { - emit_tab(as, PPCI_LWZX, type, (idx&255), tmp); - emit_slwi(as, tmp, (idx>>8), 3); - } else { - emit_tai(as, PPCI_LWZ, type, idx, ofs); - } -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - Reg idx, src = RID_NONE, type = RID_NONE; - int32_t ofs = AHUREF_LSX; - if (ir->r == RID_SINK) - return; - if (irt_isnum(ir->t)) { - src = ra_alloc1(as, ir->op2, RSET_FPR); - } else { - if (!irt_ispri(ir->t)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - ofs = 0; - } - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - rset_clear(allow, type); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - if (irt_isnum(ir->t)) { - if (ofs == AHUREF_LSX) { - emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP); - emit_slwi(as, RID_TMP, (idx>>8), 3); - } else { - emit_fai(as, PPCI_STFD, src, idx, ofs); - } - } else { - if (ra_hasreg(src)) - emit_tai(as, PPCI_STW, src, idx, ofs+4); - if (ofs == AHUREF_LSX) { - emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP); - emit_slwi(as, RID_TMP, (idx>>8), 3); - } else { - emit_tai(as, PPCI_STW, type, idx, ofs); - } - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4); - IRType1 t = ir->t; - Reg dest = RID_NONE, type = RID_NONE, base; - RegSet allow = RSET_GPR; - lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ - lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); - lua_assert(LJ_DUALNUM || - !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { - dest = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, dest); - t.irt = IRT_NUM; /* Continue with a regular number type check. */ - } else if (ra_used(ir)) { - lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); - dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); - rset_clear(allow, dest); - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); - if ((ir->op2 & IRSLOAD_CONVERT)) { - if (irt_isint(t)) { - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - dest = ra_scratch(as, RSET_FPR); - emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, dest, dest); - t.irt = IRT_NUM; /* Check for original type. */ - } else { - Reg tmp = ra_scratch(as, allow); - Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp)); - Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - emit_fab(as, PPCI_FSUB, dest, dest, fbias); - emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); - emit_lsptr(as, PPCI_LFS, (fbias & 31), - (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)), - rset_clear(allow, hibias)); - emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO); - emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); - emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000); - dest = tmp; - t.irt = IRT_INT; /* Check for original type. */ - } - } - goto dotypecheck; - } - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); -dotypecheck: - if (irt_isnum(t)) { - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); - asm_guardcc(as, CC_GE); - emit_ab(as, PPCI_CMPLW, RID_TMP, tisnum); - type = RID_TMP; - } - if (ra_hasreg(dest)) emit_fai(as, PPCI_LFD, dest, base, ofs-4); - } else { - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - asm_guardcc(as, CC_NE); - emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t)); - type = RID_TMP; - } - if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs); - } - if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4); -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; - CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? - lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[2]; - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - RegSet drop = RSET_SCRATCH; - lua_assert(sz != CTSIZE_INVALID); - - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - as->gcsteps++; - - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - if (ra_used(ir)) - ra_destreg(as, ir, RID_RET); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - int32_t ofs = sizeof(GCcdata); - lua_assert(sz == 4 || sz == 8); - if (sz == 8) { - ofs += 4; - lua_assert((ir+1)->o == IR_HIOP); - } - for (;;) { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_tai(as, PPCI_STW, r, RID_RET, ofs); - rset_clear(allow, r); - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; ir++; - } - } - /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ - emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); - emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid)); - emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA); - emit_ti(as, PPCI_LI, RID_TMP, ctypeid); /* Lower 16 bit used. Sign-ext ok. */ - asm_gencall(as, ci, args); - ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), - ra_releasetmp(as, ASMREF_TMP1)); -} -#else -#define asm_cnew(as, ir) ((void)0) -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - Reg link = RID_TMP; - MCLabel l_end = emit_label(as); - emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist)); - emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked)); - emit_setgl(as, tab, gc.grayagain); - lua_assert(LJ_GC_BLACK == 0x04); - emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28); /* Clear black bit. */ - emit_getgl(as, link, gc.grayagain); - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); - emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK); - emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj, val, tmp; - /* No need for other object barriers (yet). */ - lua_assert(IR(ir->op1)->o == IR_UREFC); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - obj = IR(ir->op1)->r; - tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); - emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK); - emit_condbranch(as, PPCI_BC, CC_EQ, l_end); - emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES); - val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); - emit_tai(as, PPCI_LBZ, tmp, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); - emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked)); -} - -/* -- Arithmetic and logic operations ------------------------------------- */ - -static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - if (pi == PPCI_FMUL) - emit_fac(as, pi, dest, left, right); - else - emit_fab(as, pi, dest, left, right); -} - -static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); - emit_fb(as, pi, dest, left); -} - -static int asm_fpjoin_pow(ASMState *as, IRIns *ir) -{ - IRIns *irp = IR(ir->op1); - if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { - IRIns *irpp = IR(irp->op1); - if (irpp == ir-2 && irpp->o == IR_FPMATH && - irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; - IRRef args[2]; - args[0] = irpp->op1; - args[1] = irp->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); - return 1; - } - } - return 0; -} - -static void asm_add(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD)) - asm_fparith(as, ir, PPCI_FADD); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - PPCIns pi; - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checki16(k)) { - pi = PPCI_ADDI; - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi = PPCI_ADDICDOT; - } - emit_tai(as, pi, dest, left, k); - return; - } else if ((k & 0xffff) == 0) { - emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16)); - return; - } else if (!as->sectref) { - emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16); - emit_tai(as, PPCI_ADDI, dest, left, k); - return; - } - } - pi = PPCI_ADD; - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, pi, dest, left, right); - } -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB)) - asm_fparith(as, ir, PPCI_FSUB); - } else { - PPCIns pi = PPCI_SUBF; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left, right; - if (irref_isk(ir->op1)) { - int32_t k = IR(ir->op1)->i; - if (checki16(k)) { - right = ra_alloc1(as, ir->op2, RSET_GPR); - emit_tai(as, PPCI_SUBFIC, dest, right, k); - return; - } - } - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ - } -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, PPCI_FMUL); - } else { - PPCIns pi = PPCI_MULLW; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checki16(k)) { - emit_tai(as, PPCI_MULLI, dest, left, k); - return; - } - } - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, pi, dest, left, right); - } -} - -static void asm_neg(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fpunary(as, ir, PPCI_FNEG); - } else { - Reg dest, left; - PPCIns pi = PPCI_NEG; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_tab(as, pi, dest, left, 0); - } -} - -static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi) -{ - Reg dest, left, right; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - } - asm_guardcc(as, CC_SO); - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; } - emit_tab(as, pi|PPCF_DOT, dest, left, right); -} - -#if LJ_HASFFI -static void asm_add64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - PPCIns pi = PPCI_ADDE; - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k == 0) - pi = PPCI_ADDZE; - else if (k == -1) - pi = PPCI_ADDME; - else - goto needright; - right = 0; - } else { - needright: - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_tab(as, pi, dest, left, right); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checki16(k)) { - emit_tai(as, PPCI_ADDIC, dest, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, PPCI_ADDC, dest, left, right); -} - -static void asm_sub64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR); - PPCIns pi = PPCI_SUBFE; - if (irref_isk(ir->op1)) { - int32_t k = IR(ir->op1)->i; - if (k == 0) - pi = PPCI_SUBFZE; - else if (k == -1) - pi = PPCI_SUBFME; - else - goto needleft; - left = 0; - } else { - needleft: - left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); - } - emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ - ir--; - dest = ra_dest(as, ir, RSET_GPR); - right = ra_alloc1(as, ir->op2, RSET_GPR); - if (irref_isk(ir->op1)) { - int32_t k = IR(ir->op1)->i; - if (checki16(k)) { - emit_tai(as, PPCI_SUBFIC, dest, right, k); - return; - } - } - left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); - emit_tab(as, PPCI_SUBFC, dest, right, left); -} - -static void asm_neg64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_tab(as, PPCI_SUBFZE, dest, left, 0); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_tai(as, PPCI_SUBFIC, dest, left, 0); -} -#endif - -static void asm_bitnot(ASMState *as, IRIns *ir) -{ - Reg dest, left, right; - PPCIns pi = PPCI_NOR; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - if (mayfuse(as, ir->op1)) { - IRIns *irl = IR(ir->op1); - if (irl->o == IR_BAND) - pi ^= (PPCI_NOR ^ PPCI_NAND); - else if (irl->o == IR_BXOR) - pi ^= (PPCI_NOR ^ PPCI_EQV); - else if (irl->o != IR_BOR) - goto nofuse; - left = ra_hintalloc(as, irl->op1, dest, RSET_GPR); - right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left)); - } else { -nofuse: - left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - } - emit_asb(as, pi, dest, left, right); -} - -static void asm_bitswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - IRIns *irx; - if (mayfuse(as, ir->op1) && (irx = IR(ir->op1))->o == IR_XLOAD && - ra_noreg(irx->r) && (irt_isint(irx->t) || irt_isu32(irx->t))) { - /* Fuse BSWAP with XLOAD to lwbrx. */ - asm_fusexrefx(as, PPCI_LWBRX, dest, irx->op1, RSET_GPR); - } else { - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - Reg tmp = dest; - if (tmp == left) { - tmp = RID_TMP; - emit_mr(as, dest, RID_TMP); - } - emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23); - emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7); - emit_rotlwi(as, tmp, left, 8); - } -} - -static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - Reg tmp = left; - if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) { - if (!checku16(k)) { - emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16)); - if ((k & 0xffff) == 0) return; - } - emit_asi(as, pik, dest, left, k); - return; - } - } - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_asb(as, pi, dest, left, right); -} - -/* Fuse BAND with contiguous bitmask and a shift to rlwinm. */ -static void asm_fuseandsh(ASMState *as, PPCIns pi, int32_t mask, IRRef ref) -{ - IRIns *ir; - Reg left; - if (mayfuse(as, ref) && (ir = IR(ref), ra_noreg(ir->r)) && - irref_isk(ir->op2) && ir->o >= IR_BSHL && ir->o <= IR_BROR) { - int32_t sh = (IR(ir->op2)->i & 31); - switch (ir->o) { - case IR_BSHL: - if ((mask & ((1u<>sh))) goto nofuse; - sh = ((32-sh)&31); - break; - case IR_BROL: - break; - default: - goto nofuse; - } - left = ra_alloc1(as, ir->op1, RSET_GPR); - *--as->mcp = pi | PPCF_T(left) | PPCF_B(sh); - return; - } -nofuse: - left = ra_alloc1(as, ref, RSET_GPR); - *--as->mcp = pi | PPCF_T(left); -} - -static void asm_bitand(ASMState *as, IRIns *ir) -{ - Reg dest, left, right; - IRRef lref = ir->op1; - PPCIns dot = 0; - IRRef op2; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - dot = PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k) { - /* First check for a contiguous bitmask as used by rlwinm. */ - uint32_t s1 = lj_ffs((uint32_t)k); - uint32_t k1 = ((uint32_t)k >> s1); - if ((k1 & (k1+1)) == 0) { - asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) | - PPCF_MB(31-lj_fls((uint32_t)k)) | PPCF_ME(31-s1), - k, lref); - return; - } - if (~(uint32_t)k) { - uint32_t s2 = lj_ffs(~(uint32_t)k); - uint32_t k2 = (~(uint32_t)k >> s2); - if ((k2 & (k2+1)) == 0) { - asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) | - PPCF_MB(32-s2) | PPCF_ME(30-lj_fls(~(uint32_t)k)), - k, lref); - return; - } - } - } - if (checku16(k)) { - left = ra_alloc1(as, lref, RSET_GPR); - emit_asi(as, PPCI_ANDIDOT, dest, left, k); - return; - } else if ((k & 0xffff) == 0) { - left = ra_alloc1(as, lref, RSET_GPR); - emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16)); - return; - } - } - op2 = ir->op2; - if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT && ra_noreg(IR(op2)->r)) { - dot ^= (PPCI_AND ^ PPCI_ANDC); - op2 = IR(op2)->op1; - } - left = ra_hintalloc(as, lref, dest, RSET_GPR); - right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left)); - emit_asb(as, PPCI_AND ^ dot, dest, left, right); -} - -static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) -{ - Reg dest, left; - Reg dot = 0; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - dot = PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { /* Constant shifts. */ - int32_t shift = (IR(ir->op2)->i & 31); - if (pik == 0) /* SLWI */ - emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift); - else if (pik == 1) /* SRWI */ - emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31); - else - emit_asb(as, pik|dot, dest, left, shift); - } else { - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_asb(as, pi|dot, dest, left, right); - } -} - -static void asm_min_max(ASMState *as, IRIns *ir, int ismax) -{ - if (irt_isnum(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg tmp = dest; - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - if (tmp == left || tmp == right) - tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR, - dest), left), right)); - emit_facb(as, PPCI_FSEL, dest, tmp, - ismax ? left : right, ismax ? right : left); - emit_fab(as, PPCI_FSUB, tmp, left, right); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg tmp1 = RID_TMP, tmp2 = dest; - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (tmp2 == left || tmp2 == right) - tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, - dest), left), right)); - emit_tab(as, PPCI_ADD, dest, tmp2, right); - emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1); - emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1); - emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1); - emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000); - emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000); - } -} - -/* -- Comparisons --------------------------------------------------------- */ - -#define CC_UNSIGNED 0x08 /* Unsigned integer comparison. */ -#define CC_TWO 0x80 /* Check two flags for FP comparison. */ - -/* Map of comparisons to flags. ORDER IR. */ -static const uint8_t asm_compmap[IR_ABC+1] = { - /* op int cc FP cc */ - /* LT */ CC_GE + (CC_GE<<4), - /* GE */ CC_LT + (CC_LE<<4) + CC_TWO, - /* LE */ CC_GT + (CC_GE<<4) + CC_TWO, - /* GT */ CC_LE + (CC_LE<<4), - /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO, - /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4), - /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4), - /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO, - /* EQ */ CC_NE + (CC_NE<<4), - /* NE */ CC_EQ + (CC_EQ<<4), - /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO /* Same as UGT. */ -}; - -static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc) -{ - Reg right, left = ra_alloc1(as, lref, RSET_GPR); - if (irref_isk(rref)) { - int32_t k = IR(rref)->i; - if ((cc & CC_UNSIGNED) == 0) { /* Signed comparison with constant. */ - if (checki16(k)) { - emit_tai(as, PPCI_CMPWI, cr, left, k); - /* Signed comparison with zero and referencing previous ins? */ - if (k == 0 && lref == as->curins-1) - as->flagmcp = as->mcp; /* Allow elimination of the compare. */ - return; - } else if ((cc & 3) == (CC_EQ & 3)) { /* Use CMPLWI for EQ or NE. */ - if (checku16(k)) { - emit_tai(as, PPCI_CMPLWI, cr, left, k); - return; - } else if (!as->sectref && ra_noreg(IR(rref)->r)) { - emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k); - emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16)); - return; - } - } - } else { /* Unsigned comparison with constant. */ - if (checku16(k)) { - emit_tai(as, PPCI_CMPLWI, cr, left, k); - return; - } - } - } - right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left)); - emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right); -} - -static void asm_comp(ASMState *as, IRIns *ir) -{ - PPCCC cc = asm_compmap[ir->o]; - if (irt_isnum(ir->t)) { - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - asm_guardcc(as, (cc >> 4)); - if ((cc & CC_TWO)) - emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3)); - emit_fab(as, PPCI_FCMPU, 0, left, right); - } else { - IRRef lref = ir->op1, rref = ir->op2; - if (irref_isk(lref) && !irref_isk(rref)) { - /* Swap constants to the right (only for ABC). */ - IRRef tmp = lref; lref = rref; rref = tmp; - if ((cc & 2) == 0) cc ^= 1; /* LT <-> GT, LE <-> GE */ - } - asm_guardcc(as, cc); - asm_intcomp_(as, lref, rref, 0, cc); - } -} - -#if LJ_HASFFI -/* 64 bit integer comparisons. */ -static void asm_comp64(ASMState *as, IRIns *ir) -{ - PPCCC cc = asm_compmap[(ir-1)->o]; - if ((cc&3) == (CC_EQ&3)) { - asm_guardcc(as, cc); - emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR, - (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3)); - } else { - asm_guardcc(as, CC_EQ); - emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1)); - emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC, - (CC_EQ&3), (CC_EQ&3), 4+(cc&3)); - } - /* Loword comparison sets cr1 and is unsigned, except for equality. */ - asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4, - cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED)); - /* Hiword comparison sets cr0. */ - asm_intcomp_(as, ir->op1, ir->op2, 0, cc); - as->flagmcp = NULL; /* Doesn't work here. */ -} -#endif - -/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ - -/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ -#if LJ_HASFFI - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; - if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ - as->curins--; /* Always skip the CONV. */ - if (usehi || uselo) - asm_conv64(as, ir); - return; - } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ - asm_comp64(as, ir); - return; - } else if ((ir-1)->o == IR_XSTORE) { - as->curins--; /* Handle both stores here. */ - if ((ir-1)->r != RID_SINK) { - asm_xstore(as, ir, 0); - asm_xstore(as, ir-1, 4); - } - return; - } - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { - case IR_ADD: as->curins--; asm_add64(as, ir); break; - case IR_SUB: as->curins--; asm_sub64(as, ir); break; - case IR_NEG: as->curins--; asm_neg64(as, ir); break; - case IR_CALLN: - case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - case IR_CNEWI: - /* Nothing to do here. Handled by lo op itself. */ - break; - default: lua_assert(0); break; - } -#else - UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */ -#endif -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */ - Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; - rset_clear(allow, pbase); - tmp = allow ? rset_pickbot(allow) : - (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); - emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno)); - if (allow == RSET_EMPTY) /* Restore temp. register. */ - emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW); - else - ra_modified(as, tmp); - emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot)); - emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp); - emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack)); - if (pbase == RID_TMP) - emit_getgl(as, RID_TMP, jit_base); - emit_getgl(as, tmp, jit_L); - if (allow == RSET_EMPTY) /* Spill temp. register. */ - emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW); -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if (irt_isnum(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_fai(as, PPCI_STFD, src, RID_BASE, ofs); - } else { - Reg type; - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, allow); - rset_clear(allow, src); - emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4); - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { - if (s == 0) continue; /* Do not overwrite link to previous frame. */ - type = ra_allock(as, (int32_t)(*flinks--), allow); - } else { - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - } - emit_tai(as, PPCI_STW, type, RID_BASE, ofs); - } - checkmclim(as); - } - lua_assert(map + nent == flinks); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ - emit_ai(as, PPCI_CMPWI, RID_RET, 0); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - tmp = ra_releasetmp(as, ASMREF_TMP2); - emit_loadi(as, tmp, as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end); - emit_ab(as, PPCI_CMPLW, RID_TMP, tmp); - emit_getgl(as, tmp, gc.threshold); - emit_getgl(as, RID_TMP, gc.total); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guardcc already inverted the cond branch and patched the final b. */ - p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2); - } else { - p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); - } -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (r != RID_BASE) - emit_mr(as, r, RID_BASE); - } -} - -/* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ - } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - rset_clear(allow, irp->r); - emit_mr(as, r, irp->r); /* Move from coalesced parent reg. */ - } else { - emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ - } - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - MCode *p = as->mctop; - MCode *target; - int32_t spadj = as->T->spadjust; - if (spadj == 0) { - *--p = PPCI_NOP; - *--p = PPCI_NOP; - as->mctop = p; - } else { - /* Patch stack adjustment. */ - lua_assert(checki16(CFRAME_SIZE+spadj)); - p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj); - p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj; - } - /* Patch exit branch. */ - target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; - p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - MCode *p = as->mctop - 1; /* Leave room for exit branch. */ - if (as->loopref) { - as->invmcp = as->mcp = p; - } else { - as->mcp = p-2; /* Leave room for stack pointer adjustment. */ - as->invmcp = NULL; - } -} - -/* -- Instruction dispatch ------------------------------------------------ */ - -/* Assemble a single instruction. */ -static void asm_ir(ASMState *as, IRIns *ir) -{ - switch ((IROp)ir->o) { - /* Miscellaneous ops. */ - case IR_LOOP: asm_loop(as); break; - case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; - case IR_USE: - ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; - case IR_PHI: asm_phi(as, ir); break; - case IR_HIOP: asm_hiop(as, ir); break; - case IR_GCSTEP: asm_gcstep(as, ir); break; - - /* Guarded assertions. */ - case IR_EQ: case IR_NE: - if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { - as->curins--; - asm_href(as, ir-1, (IROp)ir->o); - break; - } - /* fallthrough */ - case IR_LT: case IR_GE: case IR_LE: case IR_GT: - case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: - case IR_ABC: - asm_comp(as, ir); - break; - - case IR_RETF: asm_retf(as, ir); break; - - /* Bit ops. */ - case IR_BNOT: asm_bitnot(as, ir); break; - case IR_BSWAP: asm_bitswap(as, ir); break; - - case IR_BAND: asm_bitand(as, ir); break; - case IR_BOR: asm_bitop(as, ir, PPCI_OR, PPCI_ORI); break; - case IR_BXOR: asm_bitop(as, ir, PPCI_XOR, PPCI_XORI); break; - - case IR_BSHL: asm_bitshift(as, ir, PPCI_SLW, 0); break; - case IR_BSHR: asm_bitshift(as, ir, PPCI_SRW, 1); break; - case IR_BSAR: asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI); break; - case IR_BROL: asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31), - PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31)); break; - case IR_BROR: lua_assert(0); break; - - /* Arithmetic ops. */ - case IR_ADD: asm_add(as, ir); break; - case IR_SUB: asm_sub(as, ir); break; - case IR_MUL: asm_mul(as, ir); break; - case IR_DIV: asm_fparith(as, ir, PPCI_FDIV); break; - case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break; - case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break; - case IR_NEG: asm_neg(as, ir); break; - - case IR_ABS: asm_fpunary(as, ir, PPCI_FABS); break; - case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break; - case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break; - case IR_MIN: asm_min_max(as, ir, 0); break; - case IR_MAX: asm_min_max(as, ir, 1); break; - case IR_FPMATH: - if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) - break; - if (ir->op2 == IRFPM_SQRT && (as->flags & JIT_F_SQRT)) - asm_fpunary(as, ir, PPCI_FSQRT); - else - asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); - break; - - /* Overflow-checking arithmetic ops. */ - case IR_ADDOV: asm_arithov(as, ir, PPCI_ADDO); break; - case IR_SUBOV: asm_arithov(as, ir, PPCI_SUBFO); break; - case IR_MULOV: asm_arithov(as, ir, PPCI_MULLWO); break; - - /* Memory references. */ - case IR_AREF: asm_aref(as, ir); break; - case IR_HREF: asm_href(as, ir, 0); break; - case IR_HREFK: asm_hrefk(as, ir); break; - case IR_NEWREF: asm_newref(as, ir); break; - case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; - case IR_FREF: asm_fref(as, ir); break; - case IR_STRREF: asm_strref(as, ir); break; - - /* Loads and stores. */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - asm_ahuvload(as, ir); - break; - case IR_FLOAD: asm_fload(as, ir); break; - case IR_XLOAD: asm_xload(as, ir); break; - case IR_SLOAD: asm_sload(as, ir); break; - - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; - case IR_FSTORE: asm_fstore(as, ir); break; - case IR_XSTORE: asm_xstore(as, ir, 0); break; - - /* Allocations. */ - case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; - case IR_TNEW: asm_tnew(as, ir); break; - case IR_TDUP: asm_tdup(as, ir); break; - case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; - - /* Write barriers. */ - case IR_TBAR: asm_tbar(as, ir); break; - case IR_OBAR: asm_obar(as, ir); break; - - /* Type conversions. */ - case IR_CONV: asm_conv(as, ir); break; - case IR_TOBIT: asm_tobit(as, ir); break; - case IR_TOSTR: asm_tostr(as, ir); break; - case IR_STRTO: asm_strto(as, ir); break; - - /* Calls. */ - case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; - case IR_CALLXS: asm_callx(as, ir); break; - case IR_CARG: break; - - default: - setintV(&as->J->errinfo, ir->o); - lj_trace_err_info(as->J, LJ_TRERR_NYIIR); - break; - } -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - uint32_t i, nargs = (int)CCI_NARGS(ci); - int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; - asm_collectargs(as, ir, ci, args); - for (i = 0; i < nargs; i++) - if (args[i] && irt_isfp(IR(args[i])->t)) { - if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1; - } else { - if (ngpr > 0) ngpr--; else nslots++; - } - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; - return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); -} - -static void asm_setup_target(ASMState *as) -{ - asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); -} - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *pe = (MCode *)((char *)p + T->szmcode); - MCode *px = exitstub_trace_addr(T, exitno); - MCode *cstart = NULL; - MCode *mcarea = lj_mcode_patch(J, p, 0); - int clearso = 0; - for (; p < pe; p++) { - /* Look for exitstub branch, try to replace with branch to target. */ - uint32_t ins = *p; - if ((ins & 0xfc000000u) == 0x40000000u && - ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) { - ptrdiff_t delta = (char *)target - (char *)p; - if (((ins >> 16) & 3) == (CC_SO&3)) { - clearso = sizeof(MCode); - delta -= sizeof(MCode); - } - /* Many, but not all short-range branches can be patched directly. */ - if (((delta + 0x8000) >> 16) == 0) { - *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) | - ((delta & 0x8000) * (PPCF_Y/0x8000)); - if (!cstart) cstart = p; - } - } else if ((ins & 0xfc000000u) == PPCI_B && - ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) { - ptrdiff_t delta = (char *)target - (char *)p; - lua_assert(((delta + 0x02000000) >> 26) == 0); - *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu); - if (!cstart) cstart = p; - } - } - { /* Always patch long-range branch in exit stub itself. */ - ptrdiff_t delta = (char *)target - (char *)px - clearso; - lua_assert(((delta + 0x02000000) >> 26) == 0); - *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu); - } - if (!cstart) cstart = px; - lj_mcode_sync(cstart, px+1); - if (clearso) { /* Extend the current trace. Ugly workaround. */ - MCode *pp = J->cur.mcode; - J->cur.szmcode += sizeof(MCode); - *--pp = PPCI_MCRXR; /* Clear SO flag. */ - J->cur.mcode = pp; - lj_mcode_sync(pp, pp+1); - } - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_asm_x86.h b/third-party/LuaJIT-2.0.2/src/lj_asm_x86.h deleted file mode 100644 index 64441ccb7a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_asm_x86.h +++ /dev/null @@ -1,2793 +0,0 @@ -/* -** x86/x64 IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Guard handling ------------------------------------------------------ */ - -/* Generate an exit stub group at the bottom of the reserved MCode memory. */ -static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) -{ - ExitNo i, groupofs = (group*EXITSTUBS_PER_GROUP) & 0xff; - MCode *mxp = as->mcbot; - MCode *mxpstart = mxp; - if (mxp + (2+2)*EXITSTUBS_PER_GROUP+8+5 >= as->mctop) - asm_mclimit(as); - /* Push low byte of exitno for each exit stub. */ - *mxp++ = XI_PUSHi8; *mxp++ = (MCode)groupofs; - for (i = 1; i < EXITSTUBS_PER_GROUP; i++) { - *mxp++ = XI_JMPs; *mxp++ = (MCode)((2+2)*(EXITSTUBS_PER_GROUP - i) - 2); - *mxp++ = XI_PUSHi8; *mxp++ = (MCode)(groupofs + i); - } - /* Push the high byte of the exitno for each exit stub group. */ - *mxp++ = XI_PUSHi8; *mxp++ = (MCode)((group*EXITSTUBS_PER_GROUP)>>8); - /* Store DISPATCH at original stack slot 0. Account for the two push ops. */ - *mxp++ = XI_MOVmi; - *mxp++ = MODRM(XM_OFS8, 0, RID_ESP); - *mxp++ = MODRM(XM_SCALE1, RID_ESP, RID_ESP); - *mxp++ = 2*sizeof(void *); - *(int32_t *)mxp = ptr2addr(J2GG(as->J)->dispatch); mxp += 4; - /* Jump to exit handler which fills in the ExitState. */ - *mxp++ = XI_JMP; mxp += 4; - *((int32_t *)(mxp-4)) = jmprel(mxp, (MCode *)(void *)lj_vm_exit_handler); - /* Commit the code for this group (even if assembly fails later on). */ - lj_mcode_commitbot(as->J, mxp); - as->mcbot = mxp; - as->mclim = as->mcbot + MCLIM_REDZONE; - return mxpstart; -} - -/* Setup all needed exit stubs. */ -static void asm_exitstub_setup(ASMState *as, ExitNo nexits) -{ - ExitNo i; - if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) - lj_trace_err(as->J, LJ_TRERR_SNAPOV); - for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++) - if (as->J->exitstubgroup[i] == NULL) - as->J->exitstubgroup[i] = asm_exitstub_gen(as, i); -} - -/* Emit conditional branch to exit for guard. -** It's important to emit this *after* all registers have been allocated, -** because rematerializations may invalidate the flags. -*/ -static void asm_guardcc(ASMState *as, int cc) -{ - MCode *target = exitstub_addr(as->J, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *(int32_t *)(p+1) = jmprel(p+5, target); - target = p; - cc ^= 1; - if (as->realign) { - emit_sjcc(as, cc, target); - return; - } - } - emit_jcc(as, cc, target); -} - -/* -- Memory operand fusion ----------------------------------------------- */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if a reference is a signed 32 bit constant. */ -static int asm_isk32(ASMState *as, IRRef ref, int32_t *k) -{ - if (irref_isk(ref)) { - IRIns *ir = IR(ref); - if (ir->o != IR_KINT64) { - *k = ir->i; - return 1; - } else if (checki32((int64_t)ir_kint64(ir)->u64)) { - *k = (int32_t)ir_kint64(ir)->u64; - return 1; - } - } - return 0; -} - -/* Check if there's no conflicting instruction between curins and ref. -** Also avoid fusing loads if there are multiple references. -*/ -static int noconflict(ASMState *as, IRRef ref, IROp conflict, int noload) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) { - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - else if (!noload && (ir[i].op1 == ref || ir[i].op2 == ref)) - return 0; - } - return 1; /* Ok, no conflict. */ -} - -/* Fuse array base into memory operand. */ -static IRRef asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *irb = IR(ref); - as->mrm.ofs = 0; - if (irb->o == IR_FLOAD) { - IRIns *ira = IR(irb->op1); - lua_assert(irb->op2 == IRFL_TAB_ARRAY); - /* We can avoid the FLOAD of t->array for colocated arrays. */ - if (ira->o == IR_TNEW && ira->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, irb->op1, IR_NEWREF, 1)) { - as->mrm.ofs = (int32_t)sizeof(GCtab); /* Ofs to colocated array. */ - return irb->op1; /* Table obj. */ - } - } else if (irb->o == IR_ADD && irref_isk(irb->op2)) { - /* Fuse base offset (vararg load). */ - as->mrm.ofs = IR(irb->op2)->i; - return irb->op1; - } - return ref; /* Otherwise use the given array base. */ -} - -/* Fuse array reference into memory operand. */ -static void asm_fusearef(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irx; - lua_assert(ir->o == IR_AREF); - as->mrm.base = (uint8_t)ra_alloc1(as, asm_fuseabase(as, ir->op1), allow); - irx = IR(ir->op2); - if (irref_isk(ir->op2)) { - as->mrm.ofs += 8*irx->i; - as->mrm.idx = RID_NONE; - } else { - rset_clear(allow, as->mrm.base); - as->mrm.scale = XM_SCALE8; - /* Fuse a constant ADD (e.g. t[i+1]) into the offset. - ** Doesn't help much without ABCelim, but reduces register pressure. - */ - if (!LJ_64 && /* Has bad effects with negative index on x64. */ - mayfuse(as, ir->op2) && ra_noreg(irx->r) && - irx->o == IR_ADD && irref_isk(irx->op2)) { - as->mrm.ofs += 8*IR(irx->op2)->i; - as->mrm.idx = (uint8_t)ra_alloc1(as, irx->op1, allow); - } else { - as->mrm.idx = (uint8_t)ra_alloc1(as, ir->op2, allow); - } - } -} - -/* Fuse array/hash/upvalue reference into memory operand. -** Caveat: this may allocate GPRs for the base/idx registers. Be sure to -** pass the final allow mask, excluding any GPRs used for other inputs. -** In particular: 2-operand GPR instructions need to call ra_dest() first! -*/ -static void asm_fuseahuref(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - switch ((IROp)ir->o) { - case IR_AREF: - if (mayfuse(as, ref)) { - asm_fusearef(as, ir, allow); - return; - } - break; - case IR_HREFK: - if (mayfuse(as, ref)) { - as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow); - as->mrm.ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - as->mrm.idx = RID_NONE; - return; - } - break; - case IR_UREFC: - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv; - as->mrm.ofs = ptr2addr(&uv->tv); - as->mrm.base = as->mrm.idx = RID_NONE; - return; - } - break; - default: - lua_assert(ir->o == IR_HREF || ir->o == IR_NEWREF || ir->o == IR_UREFO || - ir->o == IR_KKPTR); - break; - } - } - as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow); - as->mrm.ofs = 0; - as->mrm.idx = RID_NONE; -} - -/* Fuse FLOAD/FREF reference into memory operand. */ -static void asm_fusefref(ASMState *as, IRIns *ir, RegSet allow) -{ - lua_assert(ir->o == IR_FLOAD || ir->o == IR_FREF); - as->mrm.ofs = field_ofs[ir->op2]; - as->mrm.idx = RID_NONE; - if (irref_isk(ir->op1)) { - as->mrm.ofs += IR(ir->op1)->i; - as->mrm.base = RID_NONE; - } else { - as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow); - } -} - -/* Fuse string reference into memory operand. */ -static void asm_fusestrref(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irr; - lua_assert(ir->o == IR_STRREF); - as->mrm.base = as->mrm.idx = RID_NONE; - as->mrm.scale = XM_SCALE1; - as->mrm.ofs = sizeof(GCstr); - if (irref_isk(ir->op1)) { - as->mrm.ofs += IR(ir->op1)->i; - } else { - Reg r = ra_alloc1(as, ir->op1, allow); - rset_clear(allow, r); - as->mrm.base = (uint8_t)r; - } - irr = IR(ir->op2); - if (irref_isk(ir->op2)) { - as->mrm.ofs += irr->i; - } else { - Reg r; - /* Fuse a constant add into the offset, e.g. string.sub(s, i+10). */ - if (!LJ_64 && /* Has bad effects with negative index on x64. */ - mayfuse(as, ir->op2) && irr->o == IR_ADD && irref_isk(irr->op2)) { - as->mrm.ofs += IR(irr->op2)->i; - r = ra_alloc1(as, irr->op1, allow); - } else { - r = ra_alloc1(as, ir->op2, allow); - } - if (as->mrm.base == RID_NONE) - as->mrm.base = (uint8_t)r; - else - as->mrm.idx = (uint8_t)r; - } -} - -static void asm_fusexref(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - as->mrm.idx = RID_NONE; - if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { - as->mrm.ofs = ir->i; - as->mrm.base = RID_NONE; - } else if (ir->o == IR_STRREF) { - asm_fusestrref(as, ir, allow); - } else { - as->mrm.ofs = 0; - if (canfuse(as, ir) && ir->o == IR_ADD && ra_noreg(ir->r)) { - /* Gather (base+idx*sz)+ofs as emitted by cdata ptr/array indexing. */ - IRIns *irx; - IRRef idx; - Reg r; - if (asm_isk32(as, ir->op2, &as->mrm.ofs)) { /* Recognize x+ofs. */ - ref = ir->op1; - ir = IR(ref); - if (!(ir->o == IR_ADD && canfuse(as, ir) && ra_noreg(ir->r))) - goto noadd; - } - as->mrm.scale = XM_SCALE1; - idx = ir->op1; - ref = ir->op2; - irx = IR(idx); - if (!(irx->o == IR_BSHL || irx->o == IR_ADD)) { /* Try other operand. */ - idx = ir->op2; - ref = ir->op1; - irx = IR(idx); - } - if (canfuse(as, irx) && ra_noreg(irx->r)) { - if (irx->o == IR_BSHL && irref_isk(irx->op2) && IR(irx->op2)->i <= 3) { - /* Recognize idx<op1; - as->mrm.scale = (uint8_t)(IR(irx->op2)->i << 6); - } else if (irx->o == IR_ADD && irx->op1 == irx->op2) { - /* FOLD does idx*2 ==> idx<<1 ==> idx+idx. */ - idx = irx->op1; - as->mrm.scale = XM_SCALE2; - } - } - r = ra_alloc1(as, idx, allow); - rset_clear(allow, r); - as->mrm.idx = (uint8_t)r; - } - noadd: - as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow); - } -} - -/* Fuse load into memory operand. */ -static Reg asm_fuseload(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_hasreg(ir->r)) { - if (allow != RSET_EMPTY) { /* Fast path. */ - ra_noweak(as, ir->r); - return ir->r; - } - fusespill: - /* Force a spill if only memory operands are allowed (asm_x87load). */ - as->mrm.base = RID_ESP; - as->mrm.ofs = ra_spill(as, ir); - as->mrm.idx = RID_NONE; - return RID_MRM; - } - if (ir->o == IR_KNUM) { - RegSet avail = as->freeset & ~as->modset & RSET_FPR; - lua_assert(allow != RSET_EMPTY); - if (!(avail & (avail-1))) { /* Fuse if less than two regs available. */ - as->mrm.ofs = ptr2addr(ir_knum(ir)); - as->mrm.base = as->mrm.idx = RID_NONE; - return RID_MRM; - } - } else if (mayfuse(as, ref)) { - RegSet xallow = (allow & RSET_GPR) ? allow : RSET_GPR; - if (ir->o == IR_SLOAD) { - if (!(ir->op2 & (IRSLOAD_PARENT|IRSLOAD_CONVERT)) && - noconflict(as, ref, IR_RETF, 0)) { - as->mrm.base = (uint8_t)ra_alloc1(as, REF_BASE, xallow); - as->mrm.ofs = 8*((int32_t)ir->op1-1) + ((ir->op2&IRSLOAD_FRAME)?4:0); - as->mrm.idx = RID_NONE; - return RID_MRM; - } - } else if (ir->o == IR_FLOAD) { - /* Generic fusion is only ok for 32 bit operand (but see asm_comp). */ - if ((irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)) && - noconflict(as, ref, IR_FSTORE, 0)) { - asm_fusefref(as, ir, xallow); - return RID_MRM; - } - } else if (ir->o == IR_ALOAD || ir->o == IR_HLOAD || ir->o == IR_ULOAD) { - if (noconflict(as, ref, ir->o + IRDELTA_L2S, 0)) { - asm_fuseahuref(as, ir->op1, xallow); - return RID_MRM; - } - } else if (ir->o == IR_XLOAD) { - /* Generic fusion is not ok for 8/16 bit operands (but see asm_comp). - ** Fusing unaligned memory operands is ok on x86 (except for SIMD types). - */ - if ((!irt_typerange(ir->t, IRT_I8, IRT_U16)) && - noconflict(as, ref, IR_XSTORE, 0)) { - asm_fusexref(as, ir->op1, xallow); - return RID_MRM; - } - } else if (ir->o == IR_VLOAD) { - asm_fuseahuref(as, ir->op1, xallow); - return RID_MRM; - } - } - if (!(as->freeset & allow) && - (allow == RSET_EMPTY || ra_hasspill(ir->s) || iscrossref(as, ref))) - goto fusespill; - return ra_allocref(as, ref, allow); -} - -#if LJ_64 -/* Don't fuse a 32 bit load into a 64 bit operation. */ -static Reg asm_fuseloadm(ASMState *as, IRRef ref, RegSet allow, int is64) -{ - if (is64 && !irt_is64(IR(ref)->t)) - return ra_alloc1(as, ref, allow); - return asm_fuseload(as, ref, allow); -} -#else -#define asm_fuseloadm(as, ref, allow, is64) asm_fuseload(as, (ref), (allow)) -#endif - -/* -- Calls --------------------------------------------------------------- */ - -/* Count the required number of stack slots for a call. */ -static int asm_count_call_slots(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t i, nargs = CCI_NARGS(ci); - int nslots = 0; -#if LJ_64 - if (LJ_ABI_WIN) { - nslots = (int)(nargs*2); /* Only matters for more than four args. */ - } else { - int ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; - for (i = 0; i < nargs; i++) - if (args[i] && irt_isfp(IR(args[i])->t)) { - if (nfpr > 0) nfpr--; else nslots += 2; - } else { - if (ngpr > 0) ngpr--; else nslots += 2; - } - } -#else - int ngpr = 0; - if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL) - ngpr = 2; - else if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL) - ngpr = 1; - for (i = 0; i < nargs; i++) - if (args[i] && irt_isfp(IR(args[i])->t)) { - nslots += irt_isnum(IR(args[i])->t) ? 2 : 1; - } else { - if (ngpr > 0) ngpr--; else nslots++; - } -#endif - return nslots; -} - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_NARGS(ci); - int32_t ofs = STACKARG_OFS; -#if LJ_64 - uint32_t gprs = REGARG_GPRS; - Reg fpr = REGARG_FIRSTFPR; -#if !LJ_ABI_WIN - MCode *patchnfpr = NULL; -#endif -#else - uint32_t gprs = 0; - if ((ci->flags & CCI_CC_MASK) != CCI_CC_CDECL) { - if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL) - gprs = (REGARG_GPRS & 31); - else if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL) - gprs = REGARG_GPRS; - } -#endif - if ((void *)ci->func) - emit_call(as, ci->func); -#if LJ_64 - if ((ci->flags & CCI_VARARG)) { /* Special handling for vararg calls. */ -#if LJ_ABI_WIN - for (n = 0; n < 4 && n < nargs; n++) { - IRIns *ir = IR(args[n]); - if (irt_isfp(ir->t)) /* Duplicate FPRs in GPRs. */ - emit_rr(as, XO_MOVDto, (irt_isnum(ir->t) ? REX_64 : 0) | (fpr+n), - ((gprs >> (n*5)) & 31)); /* Either MOVD or MOVQ. */ - } -#else - patchnfpr = --as->mcp; /* Indicate number of used FPRs in register al. */ - *--as->mcp = XI_MOVrib | RID_EAX; -#endif - } -#endif - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - IRIns *ir = IR(ref); - Reg r; -#if LJ_64 && LJ_ABI_WIN - /* Windows/x64 argument registers are strictly positional. */ - r = irt_isfp(ir->t) ? (fpr <= REGARG_LASTFPR ? fpr : 0) : (gprs & 31); - fpr++; gprs >>= 5; -#elif LJ_64 - /* POSIX/x64 argument registers are used in order of appearance. */ - if (irt_isfp(ir->t)) { - r = fpr <= REGARG_LASTFPR ? fpr++ : 0; - } else { - r = gprs & 31; gprs >>= 5; - } -#else - if (ref && irt_isfp(ir->t)) { - r = 0; - } else { - r = gprs & 31; gprs >>= 5; - if (!ref) continue; - } -#endif - if (r) { /* Argument is in a register. */ - if (r < RID_MAX_GPR && ref < ASMREF_TMP1) { -#if LJ_64 - if (ir->o == IR_KINT64) - emit_loadu64(as, r, ir_kint64(ir)->u64); - else -#endif - emit_loadi(as, r, ir->i); - } else { - lua_assert(rset_test(as->freeset, r)); /* Must have been evicted. */ - if (ra_hasreg(ir->r)) { - ra_noweak(as, ir->r); - emit_movrr(as, ir, r, ir->r); - } else { - ra_allocref(as, ref, RID2RSET(r)); - } - } - } else if (irt_isfp(ir->t)) { /* FP argument is on stack. */ - lua_assert(!(irt_isfloat(ir->t) && irref_isk(ref))); /* No float k. */ - if (LJ_32 && (ofs & 4) && irref_isk(ref)) { - /* Split stores for unaligned FP consts. */ - emit_movmroi(as, RID_ESP, ofs, (int32_t)ir_knum(ir)->u32.lo); - emit_movmroi(as, RID_ESP, ofs+4, (int32_t)ir_knum(ir)->u32.hi); - } else { - r = ra_alloc1(as, ref, RSET_FPR); - emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, - r, RID_ESP, ofs); - } - ofs += (LJ_32 && irt_isfloat(ir->t)) ? 4 : 8; - } else { /* Non-FP argument is on stack. */ - if (LJ_32 && ref < ASMREF_TMP1) { - emit_movmroi(as, RID_ESP, ofs, ir->i); - } else { - r = ra_alloc1(as, ref, RSET_GPR); - emit_movtomro(as, REX_64 + r, RID_ESP, ofs); - } - ofs += sizeof(intptr_t); - } - checkmclim(as); - } -#if LJ_64 && !LJ_ABI_WIN - if (patchnfpr) *patchnfpr = fpr - REGARG_FIRSTFPR; -#endif -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = (LJ_32 && (ir+1)->o == IR_HIOP); - if ((ci->flags & CCI_NOFPRCLOBBER)) - drop &= ~RSET_FPR; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - if (irt_isfp(ir->t)) { - int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ -#if LJ_64 - if ((ci->flags & CCI_CASTU64)) { - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rr(as, XO_MOVD, dest|REX_64, RID_RET); /* Really MOVQ. */ - } - if (ofs) emit_movtomro(as, RID_RET|REX_64, RID_ESP, ofs); - } else { - ra_destreg(as, ir, RID_FPRET); - } -#else - /* Number result is in x87 st0 for x86 calling convention. */ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rmro(as, irt_isnum(ir->t) ? XMM_MOVRM(as) : XO_MOVSS, - dest, RID_ESP, ofs); - } - if ((ci->flags & CCI_CASTU64)) { - emit_movtomro(as, RID_RETLO, RID_ESP, ofs); - emit_movtomro(as, RID_RETHI, RID_ESP, ofs+4); - } else { - emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd, - irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs); - } -#endif -#if LJ_32 - } else if (hiop) { - ra_destpair(as, ir); -#endif - } else { - lua_assert(!irt_ispri(ir->t)); - ra_destreg(as, ir, RID_RET); - } - } else if (LJ_32 && irt_isfp(ir->t)) { - emit_x87op(as, XI_FPOP); /* Pop unused result from x87 st0. */ - } -} - -static void asm_call(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX]; - const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; - asm_collectargs(as, ir, ci, args); - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -/* Return a constant function pointer or NULL for indirect calls. */ -static void *asm_callx_func(ASMState *as, IRIns *irf, IRRef func) -{ -#if LJ_32 - UNUSED(as); - if (irref_isk(func)) - return (void *)irf->i; -#else - if (irref_isk(func)) { - MCode *p; - if (irf->o == IR_KINT64) - p = (MCode *)(void *)ir_k64(irf)->u64; - else - p = (MCode *)(void *)(uintptr_t)(uint32_t)irf->i; - if (p - as->mcp == (int32_t)(p - as->mcp)) - return p; /* Call target is still in +-2GB range. */ - /* Avoid the indirect case of emit_call(). Try to hoist func addr. */ - } -#endif - return NULL; -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - int32_t spadj = 0; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); -#if LJ_32 - /* Have to readjust stack after non-cdecl calls due to callee cleanup. */ - if ((ci.flags & CCI_CC_MASK) != CCI_CC_CDECL) - spadj = 4 * asm_count_call_slots(as, &ci, args); -#endif - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - ci.func = (ASMFunction)asm_callx_func(as, irf, func); - if (!(void *)ci.func) { - /* Use a (hoistable) non-scratch register for indirect calls. */ - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - Reg r = ra_alloc1(as, func, allow); - if (LJ_32) emit_spsub(as, spadj); /* Above code may cause restores! */ - emit_rr(as, XO_GROUP5, XOg_CALL, r); - } else if (LJ_32) { - emit_spsub(as, spadj); - } - asm_gencall(as, &ci, args); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guardcc(as, CC_NE); - emit_gmroi(as, XG_ARITHi(XOg_CMP), base, -4, ptr2addr(pc)); -} - -/* -- Type conversions ---------------------------------------------------- */ - -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_guardcc(as, CC_P); - asm_guardcc(as, CC_NE); - emit_rr(as, XO_UCOMISD, left, tmp); - emit_rr(as, XO_CVTSI2SD, tmp, dest); - if (!(as->flags & JIT_F_SPLIT_XMM)) - emit_rr(as, XO_XORPS, tmp, tmp); /* Avoid partial register stall. */ - emit_rr(as, XO_CVTTSD2SI, dest, left); - /* Can't fuse since left is needed twice. */ -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg tmp = ra_noreg(IR(ir->op1)->r) ? - ra_alloc1(as, ir->op1, RSET_FPR) : - ra_scratch(as, RSET_FPR); - Reg right = asm_fuseload(as, ir->op2, rset_exclude(RSET_FPR, tmp)); - emit_rr(as, XO_MOVDto, tmp, dest); - emit_mrm(as, XO_ADDSD, tmp, right); - ra_left(as, tmp, ir->op1); -} - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); - int st64 = (st == IRT_I64 || st == IRT_U64 || (LJ_64 && st == IRT_P64)); - int stfp = (st == IRT_NUM || st == IRT_FLOAT); - IRRef lref = ir->op1; - lua_assert(irt_type(ir->t) != st); - lua_assert(!(LJ_32 && (irt_isint64(ir->t) || st64))); /* Handled by SPLIT. */ - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - Reg left = asm_fuseload(as, lref, RSET_FPR); - emit_mrm(as, st == IRT_NUM ? XO_CVTSD2SS : XO_CVTSS2SD, dest, left); - if (left == dest) return; /* Avoid the XO_XORPS. */ - } else if (LJ_32 && st == IRT_U32) { /* U32 to FP conversion on x86. */ - /* number = (2^52+2^51 .. u32) - (2^52+2^51) */ - cTValue *k = lj_ir_k64_find(as->J, U64x(43380000,00000000)); - Reg bias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - if (irt_isfloat(ir->t)) - emit_rr(as, XO_CVTSD2SS, dest, dest); - emit_rr(as, XO_SUBSD, dest, bias); /* Subtract 2^52+2^51 bias. */ - emit_rr(as, XO_XORPS, dest, bias); /* Merge bias and integer. */ - emit_loadn(as, bias, k); - emit_mrm(as, XO_MOVD, dest, asm_fuseload(as, lref, RSET_GPR)); - return; - } else { /* Integer to FP conversion. */ - Reg left = (LJ_64 && (st == IRT_U32 || st == IRT_U64)) ? - ra_alloc1(as, lref, RSET_GPR) : - asm_fuseloadm(as, lref, RSET_GPR, st64); - if (LJ_64 && st == IRT_U64) { - MCLabel l_end = emit_label(as); - const void *k = lj_ir_k64_find(as->J, U64x(43f00000,00000000)); - emit_rma(as, XO_ADDSD, dest, k); /* Add 2^64 to compensate. */ - emit_sjcc(as, CC_NS, l_end); - emit_rr(as, XO_TEST, left|REX_64, left); /* Check if u64 >= 2^63. */ - } - emit_mrm(as, irt_isnum(ir->t) ? XO_CVTSI2SD : XO_CVTSI2SS, - dest|((LJ_64 && (st64 || st == IRT_U32)) ? REX_64 : 0), left); - } - if (!(as->flags & JIT_F_SPLIT_XMM)) - emit_rr(as, XO_XORPS, dest, dest); /* Avoid partial register stall. */ - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lua_assert(irt_isint(ir->t) && st == IRT_NUM); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - x86Op op = st == IRT_NUM ? - ((ir->op2 & IRCONV_TRUNC) ? XO_CVTTSD2SI : XO_CVTSD2SI) : - ((ir->op2 & IRCONV_TRUNC) ? XO_CVTTSS2SI : XO_CVTSS2SI); - if (LJ_64 ? irt_isu64(ir->t) : irt_isu32(ir->t)) { - /* LJ_64: For inputs >= 2^63 add -2^64, convert again. */ - /* LJ_32: For inputs >= 2^31 add -2^31, convert again and add 2^31. */ - Reg tmp = ra_noreg(IR(lref)->r) ? ra_alloc1(as, lref, RSET_FPR) : - ra_scratch(as, RSET_FPR); - MCLabel l_end = emit_label(as); - if (LJ_32) - emit_gri(as, XG_ARITHi(XOg_ADD), dest, (int32_t)0x80000000); - emit_rr(as, op, dest|REX_64, tmp); - if (st == IRT_NUM) - emit_rma(as, XO_ADDSD, tmp, lj_ir_k64_find(as->J, - LJ_64 ? U64x(c3f00000,00000000) : U64x(c1e00000,00000000))); - else - emit_rma(as, XO_ADDSS, tmp, lj_ir_k64_find(as->J, - LJ_64 ? U64x(00000000,df800000) : U64x(00000000,cf000000))); - emit_sjcc(as, CC_NS, l_end); - emit_rr(as, XO_TEST, dest|REX_64, dest); /* Check if dest negative. */ - emit_rr(as, op, dest|REX_64, tmp); - ra_left(as, tmp, lref); - } else { - Reg left = asm_fuseload(as, lref, RSET_FPR); - if (LJ_64 && irt_isu32(ir->t)) - emit_rr(as, XO_MOV, dest, dest); /* Zero hiword. */ - emit_mrm(as, op, - dest|((LJ_64 && - (irt_is64(ir->t) || irt_isu32(ir->t))) ? REX_64 : 0), - left); - } - } - } else if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left, dest = ra_dest(as, ir, RSET_GPR); - RegSet allow = RSET_GPR; - x86Op op; - lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); - if (st == IRT_I8) { - op = XO_MOVSXb; allow = RSET_GPR8; dest |= FORCE_REX; - } else if (st == IRT_U8) { - op = XO_MOVZXb; allow = RSET_GPR8; dest |= FORCE_REX; - } else if (st == IRT_I16) { - op = XO_MOVSXw; - } else { - op = XO_MOVZXw; - } - left = asm_fuseload(as, lref, allow); - /* Add extra MOV if source is already in wrong register. */ - if (!LJ_64 && left != RID_MRM && !rset_test(allow, left)) { - Reg tmp = ra_scratch(as, allow); - emit_rr(as, op, dest, tmp); - emit_rr(as, XO_MOV, tmp, left); - } else { - emit_mrm(as, op, dest, left); - } - } else { /* 32/64 bit integer conversions. */ - if (LJ_32) { /* Only need to handle 32/32 bit no-op (cast) on x86. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ - } else if (irt_is64(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st64 || !(ir->op2 & IRCONV_SEXT)) { - /* 64/64 bit no-op (cast) or 32 to 64 bit zero extension. */ - ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ - } else { /* 32 to 64 bit sign extension. */ - Reg left = asm_fuseload(as, lref, RSET_GPR); - emit_mrm(as, XO_MOVSXd, dest|REX_64, left); - } - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st64) { - Reg left = asm_fuseload(as, lref, RSET_GPR); - /* This is either a 32 bit reg/reg mov which zeroes the hiword - ** or a load of the loword from a 64 bit address. - */ - emit_mrm(as, XO_MOV, dest, left); - } else { /* 32/32 bit no-op (cast). */ - ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } - } -} - -#if LJ_32 && LJ_HASFFI -/* No SSE conversions to/from 64 bit on x86, so resort to ugly x87 code. */ - -/* 64 bit integer to FP conversion in 32 bit mode. */ -static void asm_conv_fp_int64(ASMState *as, IRIns *ir) -{ - Reg hi = ra_alloc1(as, ir->op1, RSET_GPR); - Reg lo = ra_alloc1(as, (ir-1)->op1, rset_exclude(RSET_GPR, hi)); - int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rmro(as, irt_isnum(ir->t) ? XMM_MOVRM(as) : XO_MOVSS, - dest, RID_ESP, ofs); - } - emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd, - irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs); - if (((ir-1)->op2 & IRCONV_SRCMASK) == IRT_U64) { - /* For inputs in [2^63,2^64-1] add 2^64 to compensate. */ - MCLabel l_end = emit_label(as); - emit_rma(as, XO_FADDq, XOg_FADDq, - lj_ir_k64_find(as->J, U64x(43f00000,00000000))); - emit_sjcc(as, CC_NS, l_end); - emit_rr(as, XO_TEST, hi, hi); /* Check if u64 >= 2^63. */ - } else { - lua_assert(((ir-1)->op2 & IRCONV_SRCMASK) == IRT_I64); - } - emit_rmro(as, XO_FILDq, XOg_FILDq, RID_ESP, 0); - /* NYI: Avoid narrow-to-wide store-to-load forwarding stall. */ - emit_rmro(as, XO_MOVto, hi, RID_ESP, 4); - emit_rmro(as, XO_MOVto, lo, RID_ESP, 0); -} - -/* FP to 64 bit integer conversion in 32 bit mode. */ -static void asm_conv_int64_fp(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); - IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); - Reg lo, hi; - lua_assert(st == IRT_NUM || st == IRT_FLOAT); - lua_assert(dt == IRT_I64 || dt == IRT_U64); - lua_assert(((ir-1)->op2 & IRCONV_TRUNC)); - hi = ra_dest(as, ir, RSET_GPR); - lo = ra_dest(as, ir-1, rset_exclude(RSET_GPR, hi)); - if (ra_used(ir-1)) emit_rmro(as, XO_MOV, lo, RID_ESP, 0); - /* NYI: Avoid wide-to-narrow store-to-load forwarding stall. */ - if (!(as->flags & JIT_F_SSE3)) { /* Set FPU rounding mode to default. */ - emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 4); - emit_rmro(as, XO_MOVto, lo, RID_ESP, 4); - emit_gri(as, XG_ARITHi(XOg_AND), lo, 0xf3ff); - } - if (dt == IRT_U64) { - /* For inputs in [2^63,2^64-1] add -2^64 and convert again. */ - MCLabel l_pop, l_end = emit_label(as); - emit_x87op(as, XI_FPOP); - l_pop = emit_label(as); - emit_sjmp(as, l_end); - emit_rmro(as, XO_MOV, hi, RID_ESP, 4); - if ((as->flags & JIT_F_SSE3)) - emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0); - else - emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0); - emit_rma(as, XO_FADDq, XOg_FADDq, - lj_ir_k64_find(as->J, U64x(c3f00000,00000000))); - emit_sjcc(as, CC_NS, l_pop); - emit_rr(as, XO_TEST, hi, hi); /* Check if out-of-range (2^63). */ - } - emit_rmro(as, XO_MOV, hi, RID_ESP, 4); - if ((as->flags & JIT_F_SSE3)) { /* Truncation is easy with SSE3. */ - emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0); - } else { /* Otherwise set FPU rounding mode to truncate before the store. */ - emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0); - emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 0); - emit_rmro(as, XO_MOVtow, lo, RID_ESP, 0); - emit_rmro(as, XO_ARITHw(XOg_OR), lo, RID_ESP, 0); - emit_loadi(as, lo, 0xc00); - emit_rmro(as, XO_FNSTCW, XOg_FNSTCW, RID_ESP, 0); - } - if (dt == IRT_U64) - emit_x87op(as, XI_FDUP); - emit_mrm(as, st == IRT_NUM ? XO_FLDq : XO_FLDd, - st == IRT_NUM ? XOg_FLDq: XOg_FLDd, - asm_fuseload(as, ir->op1, RSET_EMPTY)); -} -#endif - -static void asm_strto(ASMState *as, IRIns *ir) -{ - /* Force a spill slot for the destination register (if any). */ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - RegSet drop = RSET_SCRATCH; - if ((drop & RSET_FPR) != RSET_FPR && ra_hasreg(ir->r)) - rset_set(drop, ir->r); /* WIN64 doesn't spill all FPRs. */ - ra_evictset(as, drop); - asm_guardcc(as, CC_E); - emit_rr(as, XO_TEST, RID_RET, RID_RET); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - /* Store the result to the spill slot or temp slots. */ - emit_rmro(as, XO_LEA, ra_releasetmp(as, ASMREF_TMP1)|REX_64, - RID_ESP, sps_scale(ir->s)); -} - -static void asm_tostr(ASMState *as, IRIns *ir) -{ - IRIns *irl = IR(ir->op1); - IRRef args[2]; - args[0] = ASMREF_L; - as->gcsteps++; - if (irt_isnum(irl->t)) { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; - args[1] = ASMREF_TMP1; /* const lua_Number * */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - emit_rmro(as, XO_LEA, ra_releasetmp(as, ASMREF_TMP1)|REX_64, - RID_ESP, ra_spill(as, irl)); - } else { - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; - args[1] = ir->op1; /* int32_t k */ - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - } -} - -/* -- Memory references --------------------------------------------------- */ - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_fusearef(as, ir, RSET_GPR); - if (!(as->mrm.idx == RID_NONE && as->mrm.ofs == 0)) - emit_mrm(as, XO_LEA, dest, RID_MRM); - else if (as->mrm.base != dest) - emit_rr(as, XO_MOV, dest, as->mrm.base); -} - -/* Merge NE(HREF, niltv) check. */ -static MCode *merge_href_niltv(ASMState *as, IRIns *ir) -{ - /* Assumes nothing else generates NE of HREF. */ - if ((ir[1].o == IR_NE || ir[1].o == IR_EQ) && ir[1].op1 == as->curins && - ra_hasreg(ir->r)) { - MCode *p = as->mcp; - p += (LJ_64 && *p != XI_ARITHi) ? 7+6 : 6+6; - /* Ensure no loop branch inversion happened. */ - if (p[-6] == 0x0f && p[-5] == XI_JCCn+(CC_NE^(ir[1].o & 1))) { - as->mcp = p; /* Kill cmp reg, imm32 + jz exit. */ - return p + *(int32_t *)(p-4); /* Return exit address. */ - } - } - return NULL; -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir) -{ - MCode *nilexit = merge_href_niltv(as, ir); /* Do this before any restores. */ - RegSet allow = RSET_GPR; - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = RID_NONE, tmp = RID_NONE; - IRIns *irkey = IR(ir->op2); - int isk = irref_isk(ir->op2); - IRType1 kt = irkey->t; - uint32_t khash; - MCLabel l_end, l_loop, l_next; - - if (!isk) { - rset_clear(allow, tab); - key = ra_alloc1(as, ir->op2, irt_isnum(kt) ? RSET_FPR : allow); - if (!irt_isstr(kt)) - tmp = ra_scratch(as, rset_exclude(allow, key)); - } - - /* Key not found in chain: jump to exit (if merged with NE) or load niltv. */ - l_end = emit_label(as); - if (nilexit && ir[1].o == IR_NE) { - emit_jcc(as, CC_E, nilexit); /* XI_JMP is not found by lj_asm_patchexit. */ - nilexit = NULL; - } else { - emit_loada(as, dest, niltvg(J2G(as->J))); - } - - /* Follow hash chain until the end. */ - l_loop = emit_sjcc_label(as, CC_NZ); - emit_rr(as, XO_TEST, dest, dest); - emit_rmro(as, XO_MOV, dest, dest, offsetof(Node, next)); - l_next = emit_label(as); - - /* Type and value comparison. */ - if (nilexit) - emit_jcc(as, CC_E, nilexit); - else - emit_sjcc(as, CC_E, l_end); - if (irt_isnum(kt)) { - if (isk) { - /* Assumes -0.0 is already canonicalized to +0.0. */ - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.lo), - (int32_t)ir_knum(irkey)->u32.lo); - emit_sjcc(as, CC_NE, l_next); - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.hi), - (int32_t)ir_knum(irkey)->u32.hi); - } else { - emit_sjcc(as, CC_P, l_next); - emit_rmro(as, XO_UCOMISD, key, dest, offsetof(Node, key.n)); - emit_sjcc(as, CC_AE, l_next); - /* The type check avoids NaN penalties and complaints from Valgrind. */ -#if LJ_64 - emit_u32(as, LJ_TISNUM); - emit_rmro(as, XO_ARITHi, XOg_CMP, dest, offsetof(Node, key.it)); -#else - emit_i8(as, LJ_TISNUM); - emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it)); -#endif - } -#if LJ_64 - } else if (irt_islightud(kt)) { - emit_rmro(as, XO_CMP, key|REX_64, dest, offsetof(Node, key.u64)); -#endif - } else { - if (!irt_ispri(kt)) { - lua_assert(irt_isaddr(kt)); - if (isk) - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.gcr), - ptr2addr(ir_kgc(irkey))); - else - emit_rmro(as, XO_CMP, key, dest, offsetof(Node, key.gcr)); - emit_sjcc(as, CC_NE, l_next); - } - lua_assert(!irt_isnil(kt)); - emit_i8(as, irt_toitype(kt)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it)); - } - emit_sfixup(as, l_loop); - checkmclim(as); - - /* Load main position relative to tab->node into dest. */ - khash = isk ? ir_khash(irkey) : 1; - if (khash == 0) { - emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, node)); - } else { - emit_rmro(as, XO_ARITH(XOg_ADD), dest, tab, offsetof(GCtab, node)); - if ((as->flags & JIT_F_PREFER_IMUL)) { - emit_i8(as, sizeof(Node)); - emit_rr(as, XO_IMULi8, dest, dest); - } else { - emit_shifti(as, XOg_SHL, dest, 3); - emit_rmrxo(as, XO_LEA, dest, dest, dest, XM_SCALE2, 0); - } - if (isk) { - emit_gri(as, XG_ARITHi(XOg_AND), dest, (int32_t)khash); - emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask)); - } else if (irt_isstr(kt)) { - emit_rmro(as, XO_ARITH(XOg_AND), dest, key, offsetof(GCstr, hash)); - emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask)); - } else { /* Must match with hashrot() in lj_tab.c. */ - emit_rmro(as, XO_ARITH(XOg_AND), dest, tab, offsetof(GCtab, hmask)); - emit_rr(as, XO_ARITH(XOg_SUB), dest, tmp); - emit_shifti(as, XOg_ROL, tmp, HASH_ROT3); - emit_rr(as, XO_ARITH(XOg_XOR), dest, tmp); - emit_shifti(as, XOg_ROL, dest, HASH_ROT2); - emit_rr(as, XO_ARITH(XOg_SUB), tmp, dest); - emit_shifti(as, XOg_ROL, dest, HASH_ROT1); - emit_rr(as, XO_ARITH(XOg_XOR), tmp, dest); - if (irt_isnum(kt)) { - emit_rr(as, XO_ARITH(XOg_ADD), dest, dest); -#if LJ_64 - emit_shifti(as, XOg_SHR|REX_64, dest, 32); - emit_rr(as, XO_MOV, tmp, dest); - emit_rr(as, XO_MOVDto, key|REX_64, dest); -#else - emit_rmro(as, XO_MOV, dest, RID_ESP, ra_spill(as, irkey)+4); - emit_rr(as, XO_MOVDto, key, tmp); -#endif - } else { - emit_rr(as, XO_MOV, tmp, key); - emit_rmro(as, XO_LEA, dest, key, HASH_BIAS); - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - Reg dest = ra_used(ir) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); -#if !LJ_64 - MCLabel l_exit; -#endif - lua_assert(ofs % sizeof(Node) == 0); - if (ra_hasreg(dest)) { - if (ofs != 0) { - if (dest == node && !(as->flags & JIT_F_LEA_AGU)) - emit_gri(as, XG_ARITHi(XOg_ADD), dest, ofs); - else - emit_rmro(as, XO_LEA, dest, node, ofs); - } else if (dest != node) { - emit_rr(as, XO_MOV, dest, node); - } - } - asm_guardcc(as, CC_NE); -#if LJ_64 - if (!irt_ispri(irkey->t)) { - Reg key = ra_scratch(as, rset_exclude(RSET_GPR, node)); - emit_rmro(as, XO_CMP, key|REX_64, node, - ofs + (int32_t)offsetof(Node, key.u64)); - lua_assert(irt_isnum(irkey->t) || irt_isgcv(irkey->t)); - /* Assumes -0.0 is already canonicalized to +0.0. */ - emit_loadu64(as, key, irt_isnum(irkey->t) ? ir_knum(irkey)->u64 : - ((uint64_t)irt_toitype(irkey->t) << 32) | - (uint64_t)(uint32_t)ptr2addr(ir_kgc(irkey))); - } else { - lua_assert(!irt_isnil(irkey->t)); - emit_i8(as, irt_toitype(irkey->t)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, node, - ofs + (int32_t)offsetof(Node, key.it)); - } -#else - l_exit = emit_label(as); - if (irt_isnum(irkey->t)) { - /* Assumes -0.0 is already canonicalized to +0.0. */ - emit_gmroi(as, XG_ARITHi(XOg_CMP), node, - ofs + (int32_t)offsetof(Node, key.u32.lo), - (int32_t)ir_knum(irkey)->u32.lo); - emit_sjcc(as, CC_NE, l_exit); - emit_gmroi(as, XG_ARITHi(XOg_CMP), node, - ofs + (int32_t)offsetof(Node, key.u32.hi), - (int32_t)ir_knum(irkey)->u32.hi); - } else { - if (!irt_ispri(irkey->t)) { - lua_assert(irt_isgcv(irkey->t)); - emit_gmroi(as, XG_ARITHi(XOg_CMP), node, - ofs + (int32_t)offsetof(Node, key.gcr), - ptr2addr(ir_kgc(irkey))); - emit_sjcc(as, CC_NE, l_exit); - } - lua_assert(!irt_isnil(irkey->t)); - emit_i8(as, irt_toitype(irkey->t)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, node, - ofs + (int32_t)offsetof(Node, key.it)); - } -#endif -} - -static void asm_newref(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; - IRRef args[3]; - IRIns *irkey; - Reg tmp; - if (ir->r == RID_SINK) - return; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* GCtab *t */ - args[2] = ASMREF_TMP1; /* cTValue *key */ - asm_setupresult(as, ir, ci); /* TValue * */ - asm_gencall(as, ci, args); - tmp = ra_releasetmp(as, ASMREF_TMP1); - irkey = IR(ir->op2); - if (irt_isnum(irkey->t)) { - /* For numbers use the constant itself or a spill slot as a TValue. */ - if (irref_isk(ir->op2)) - emit_loada(as, tmp, ir_knum(irkey)); - else - emit_rmro(as, XO_LEA, tmp|REX_64, RID_ESP, ra_spill(as, irkey)); - } else { - /* Otherwise use g->tmptv to hold the TValue. */ - if (!irref_isk(ir->op2)) { - Reg src = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, tmp)); - emit_movtomro(as, REX_64IR(irkey, src), tmp, 0); - } else if (!irt_ispri(irkey->t)) { - emit_movmroi(as, tmp, 0, irkey->i); - } - if (!(LJ_64 && irt_islightud(irkey->t))) - emit_movmroi(as, tmp, 4, irt_toitype(irkey->t)); - emit_loada(as, tmp, &J2G(as->J)->tmptv); - } -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - /* NYI: Check that UREFO is still open and not aliasing a slot. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_rma(as, XO_MOV, dest, v); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - emit_rmro(as, XO_LEA, dest, uv, offsetof(GCupval, tv)); - asm_guardcc(as, CC_NE); - emit_i8(as, 1); - emit_rmro(as, XO_ARITHib, XOg_CMP, uv, offsetof(GCupval, closed)); - } else { - emit_rmro(as, XO_MOV, dest, uv, offsetof(GCupval, v)); - } - emit_rmro(as, XO_MOV, uv, func, - (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_fusefref(as, ir, RSET_GPR); - emit_mrm(as, XO_LEA, dest, RID_MRM); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_fusestrref(as, ir, RSET_GPR); - if (as->mrm.base == RID_NONE) - emit_loadi(as, dest, as->mrm.ofs); - else if (as->mrm.base == dest && as->mrm.idx == RID_NONE) - emit_gri(as, XG_ARITHi(XOg_ADD), dest, as->mrm.ofs); - else - emit_mrm(as, XO_LEA, dest, RID_MRM); -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static void asm_fxload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - x86Op xo; - if (ir->o == IR_FLOAD) - asm_fusefref(as, ir, RSET_GPR); - else - asm_fusexref(as, ir->op1, RSET_GPR); - /* ir->op2 is ignored -- unaligned loads are ok on x86. */ - switch (irt_type(ir->t)) { - case IRT_I8: xo = XO_MOVSXb; break; - case IRT_U8: xo = XO_MOVZXb; break; - case IRT_I16: xo = XO_MOVSXw; break; - case IRT_U16: xo = XO_MOVZXw; break; - case IRT_NUM: xo = XMM_MOVRM(as); break; - case IRT_FLOAT: xo = XO_MOVSS; break; - default: - if (LJ_64 && irt_is64(ir->t)) - dest |= REX_64; - else - lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)); - xo = XO_MOV; - break; - } - emit_mrm(as, xo, dest, RID_MRM); -} - -static void asm_fxstore(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - Reg src = RID_NONE, osrc = RID_NONE; - int32_t k = 0; - if (ir->r == RID_SINK) - return; - /* The IRT_I16/IRT_U16 stores should never be simplified for constant - ** values since mov word [mem], imm16 has a length-changing prefix. - */ - if (irt_isi16(ir->t) || irt_isu16(ir->t) || irt_isfp(ir->t) || - !asm_isk32(as, ir->op2, &k)) { - RegSet allow8 = irt_isfp(ir->t) ? RSET_FPR : - (irt_isi8(ir->t) || irt_isu8(ir->t)) ? RSET_GPR8 : RSET_GPR; - src = osrc = ra_alloc1(as, ir->op2, allow8); - if (!LJ_64 && !rset_test(allow8, src)) { /* Already in wrong register. */ - rset_clear(allow, osrc); - src = ra_scratch(as, allow8); - } - rset_clear(allow, src); - } - if (ir->o == IR_FSTORE) { - asm_fusefref(as, IR(ir->op1), allow); - } else { - asm_fusexref(as, ir->op1, allow); - if (LJ_32 && ir->o == IR_HIOP) as->mrm.ofs += 4; - } - if (ra_hasreg(src)) { - x86Op xo; - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: xo = XO_MOVtob; src |= FORCE_REX; break; - case IRT_I16: case IRT_U16: xo = XO_MOVtow; break; - case IRT_NUM: xo = XO_MOVSDto; break; - case IRT_FLOAT: xo = XO_MOVSSto; break; -#if LJ_64 - case IRT_LIGHTUD: lua_assert(0); /* NYI: mask 64 bit lightuserdata. */ -#endif - default: - if (LJ_64 && irt_is64(ir->t)) - src |= REX_64; - else - lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)); - xo = XO_MOVto; - break; - } - emit_mrm(as, xo, src, RID_MRM); - if (!LJ_64 && src != osrc) { - ra_noweak(as, osrc); - emit_rr(as, XO_MOV, src, osrc); - } - } else { - if (irt_isi8(ir->t) || irt_isu8(ir->t)) { - emit_i8(as, k); - emit_mrm(as, XO_MOVmib, 0, RID_MRM); - } else { - lua_assert(irt_is64(ir->t) || irt_isint(ir->t) || irt_isu32(ir->t) || - irt_isaddr(ir->t)); - emit_i32(as, k); - emit_mrm(as, XO_MOVmi, REX_64IR(ir, 0), RID_MRM); - } - } -} - -#if LJ_64 -static Reg asm_load_lightud64(ASMState *as, IRIns *ir, int typecheck) -{ - if (ra_used(ir) || typecheck) { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (typecheck) { - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, dest)); - asm_guardcc(as, CC_NE); - emit_i8(as, -2); - emit_rr(as, XO_ARITHi8, XOg_CMP, tmp); - emit_shifti(as, XOg_SAR|REX_64, tmp, 47); - emit_rr(as, XO_MOV, tmp|REX_64, dest); - } - return dest; - } else { - return RID_NONE; - } -} -#endif - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - lua_assert(irt_isnum(ir->t) || irt_ispri(ir->t) || irt_isaddr(ir->t) || - (LJ_DUALNUM && irt_isint(ir->t))); -#if LJ_64 - if (irt_islightud(ir->t)) { - Reg dest = asm_load_lightud64(as, ir, 1); - if (ra_hasreg(dest)) { - asm_fuseahuref(as, ir->op1, RSET_GPR); - emit_mrm(as, XO_MOV, dest|REX_64, RID_MRM); - } - return; - } else -#endif - if (ra_used(ir)) { - RegSet allow = irt_isnum(ir->t) ? RSET_FPR : RSET_GPR; - Reg dest = ra_dest(as, ir, allow); - asm_fuseahuref(as, ir->op1, RSET_GPR); - emit_mrm(as, dest < RID_MAX_GPR ? XO_MOV : XMM_MOVRM(as), dest, RID_MRM); - } else { - asm_fuseahuref(as, ir->op1, RSET_GPR); - } - /* Always do the type check, even if the load result is unused. */ - as->mrm.ofs += 4; - asm_guardcc(as, irt_isnum(ir->t) ? CC_AE : CC_NE); - if (LJ_64 && irt_type(ir->t) >= IRT_NUM) { - lua_assert(irt_isinteger(ir->t) || irt_isnum(ir->t)); - emit_u32(as, LJ_TISNUM); - emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM); - } else { - emit_i8(as, irt_toitype(ir->t)); - emit_mrm(as, XO_ARITHi8, XOg_CMP, RID_MRM); - } -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - if (ir->r == RID_SINK) - return; - if (irt_isnum(ir->t)) { - Reg src = ra_alloc1(as, ir->op2, RSET_FPR); - asm_fuseahuref(as, ir->op1, RSET_GPR); - emit_mrm(as, XO_MOVSDto, src, RID_MRM); -#if LJ_64 - } else if (irt_islightud(ir->t)) { - Reg src = ra_alloc1(as, ir->op2, RSET_GPR); - asm_fuseahuref(as, ir->op1, rset_exclude(RSET_GPR, src)); - emit_mrm(as, XO_MOVto, src|REX_64, RID_MRM); -#endif - } else { - IRIns *irr = IR(ir->op2); - RegSet allow = RSET_GPR; - Reg src = RID_NONE; - if (!irref_isk(ir->op2)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - } - asm_fuseahuref(as, ir->op1, allow); - if (ra_hasreg(src)) { - emit_mrm(as, XO_MOVto, src, RID_MRM); - } else if (!irt_ispri(irr->t)) { - lua_assert(irt_isaddr(ir->t) || (LJ_DUALNUM && irt_isinteger(ir->t))); - emit_i32(as, irr->i); - emit_mrm(as, XO_MOVmi, 0, RID_MRM); - } - as->mrm.ofs += 4; - emit_i32(as, (int32_t)irt_toitype(ir->t)); - emit_mrm(as, XO_MOVmi, 0, RID_MRM); - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); - IRType1 t = ir->t; - Reg base; - lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ - lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); - lua_assert(LJ_DUALNUM || - !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { - Reg left = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, left); /* Frees dest reg. Do this before base alloc. */ - base = ra_alloc1(as, REF_BASE, RSET_GPR); - emit_rmro(as, XMM_MOVRM(as), left, base, ofs); - t.irt = IRT_NUM; /* Continue with a regular number type check. */ -#if LJ_64 - } else if (irt_islightud(t)) { - Reg dest = asm_load_lightud64(as, ir, (ir->op2 & IRSLOAD_TYPECHECK)); - if (ra_hasreg(dest)) { - base = ra_alloc1(as, REF_BASE, RSET_GPR); - emit_rmro(as, XO_MOV, dest|REX_64, base, ofs); - } - return; -#endif - } else if (ra_used(ir)) { - RegSet allow = irt_isnum(t) ? RSET_FPR : RSET_GPR; - Reg dest = ra_dest(as, ir, allow); - base = ra_alloc1(as, REF_BASE, RSET_GPR); - lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); - if ((ir->op2 & IRSLOAD_CONVERT)) { - t.irt = irt_isint(t) ? IRT_NUM : IRT_INT; /* Check for original type. */ - emit_rmro(as, irt_isint(t) ? XO_CVTSI2SD : XO_CVTSD2SI, dest, base, ofs); - } else if (irt_isnum(t)) { - emit_rmro(as, XMM_MOVRM(as), dest, base, ofs); - } else { - emit_rmro(as, XO_MOV, dest, base, ofs); - } - } else { - if (!(ir->op2 & IRSLOAD_TYPECHECK)) - return; /* No type check: avoid base alloc. */ - base = ra_alloc1(as, REF_BASE, RSET_GPR); - } - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - /* Need type check, even if the load result is unused. */ - asm_guardcc(as, irt_isnum(t) ? CC_AE : CC_NE); - if (LJ_64 && irt_type(t) >= IRT_NUM) { - lua_assert(irt_isinteger(t) || irt_isnum(t)); - emit_u32(as, LJ_TISNUM); - emit_rmro(as, XO_ARITHi, XOg_CMP, base, ofs+4); - } else { - emit_i8(as, irt_toitype(t)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, base, ofs+4); - } - } -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; - CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? - lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[2]; - lua_assert(sz != CTSIZE_INVALID); - - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); -#if LJ_64 - Reg r64 = sz == 8 ? REX_64 : 0; - if (irref_isk(ir->op2)) { - IRIns *irk = IR(ir->op2); - uint64_t k = irk->o == IR_KINT64 ? ir_k64(irk)->u64 : - (uint64_t)(uint32_t)irk->i; - if (sz == 4 || checki32((int64_t)k)) { - emit_i32(as, (int32_t)k); - emit_rmro(as, XO_MOVmi, r64, RID_RET, sizeof(GCcdata)); - } else { - emit_movtomro(as, RID_ECX + r64, RID_RET, sizeof(GCcdata)); - emit_loadu64(as, RID_ECX, k); - } - } else { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_movtomro(as, r + r64, RID_RET, sizeof(GCcdata)); - } -#else - int32_t ofs = sizeof(GCcdata); - if (sz == 8) { - ofs += 4; ir++; - lua_assert(ir->o == IR_HIOP); - } - do { - if (irref_isk(ir->op2)) { - emit_movmroi(as, RID_RET, ofs, IR(ir->op2)->i); - } else { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_movtomro(as, r, RID_RET, ofs); - rset_clear(allow, r); - } - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; ir--; - } while (1); -#endif - lua_assert(sz == 4 || sz == 8); - } - - /* Combine initialization of marked, gct and ctypeid. */ - emit_movtomro(as, RID_ECX, RID_RET, offsetof(GCcdata, marked)); - emit_gri(as, XG_ARITHi(XOg_OR), RID_ECX, - (int32_t)((~LJ_TCDATA<<8)+(ctypeid<<16))); - emit_gri(as, XG_ARITHi(XOg_AND), RID_ECX, LJ_GC_WHITES); - emit_opgl(as, XO_MOVZXb, RID_ECX, gc.currentwhite); - - asm_gencall(as, ci, args); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)(sz+sizeof(GCcdata))); -} -#else -#define asm_cnew(as, ir) ((void)0) -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - MCLabel l_end = emit_label(as); - emit_movtomro(as, tmp, tab, offsetof(GCtab, gclist)); - emit_setgl(as, tab, gc.grayagain); - emit_getgl(as, tmp, gc.grayagain); - emit_i8(as, ~LJ_GC_BLACK); - emit_rmro(as, XO_ARITHib, XOg_AND, tab, offsetof(GCtab, marked)); - emit_sjcc(as, CC_Z, l_end); - emit_i8(as, LJ_GC_BLACK); - emit_rmro(as, XO_GROUP3b, XOg_TEST, tab, offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj; - /* No need for other object barriers (yet). */ - lua_assert(IR(ir->op1)->o == IR_UREFC); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - emit_loada(as, ra_releasetmp(as, ASMREF_TMP1), J2G(as->J)); - obj = IR(ir->op1)->r; - emit_sjcc(as, CC_Z, l_end); - emit_i8(as, LJ_GC_WHITES); - if (irref_isk(ir->op2)) { - GCobj *vp = ir_kgc(IR(ir->op2)); - emit_rma(as, XO_GROUP3b, XOg_TEST, &vp->gch.marked); - } else { - Reg val = ra_alloc1(as, ir->op2, rset_exclude(RSET_SCRATCH&RSET_GPR, obj)); - emit_rmro(as, XO_GROUP3b, XOg_TEST, val, (int32_t)offsetof(GChead, marked)); - } - emit_sjcc(as, CC_Z, l_end); - emit_i8(as, LJ_GC_BLACK); - emit_rmro(as, XO_GROUP3b, XOg_TEST, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); -} - -/* -- FP/int arithmetic and logic operations ------------------------------ */ - -/* Load reference onto x87 stack. Force a spill to memory if needed. */ -static void asm_x87load(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_KNUM) { - cTValue *tv = ir_knum(ir); - if (tvispzero(tv)) /* Use fldz only for +0. */ - emit_x87op(as, XI_FLDZ); - else if (tvispone(tv)) - emit_x87op(as, XI_FLD1); - else - emit_rma(as, XO_FLDq, XOg_FLDq, tv); - } else if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && !ra_used(ir) && - !irref_isk(ir->op1) && mayfuse(as, ir->op1)) { - IRIns *iri = IR(ir->op1); - emit_rmro(as, XO_FILDd, XOg_FILDd, RID_ESP, ra_spill(as, iri)); - } else { - emit_mrm(as, XO_FLDq, XOg_FLDq, asm_fuseload(as, ref, RSET_EMPTY)); - } -} - -/* Try to rejoin pow from EXP2, MUL and LOG2 (if still unsplit). */ -static int fpmjoin_pow(ASMState *as, IRIns *ir) -{ - IRIns *irp = IR(ir->op1); - if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { - IRIns *irpp = IR(irp->op1); - if (irpp == ir-2 && irpp->o == IR_FPMATH && - irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM2+1)|RID2RSET(RID_EAX); - IRIns *irx; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - ra_destreg(as, ir, RID_XMM0); - emit_call(as, lj_vm_pow_sse); - irx = IR(irpp->op1); - if (ra_noreg(irx->r) && ra_gethint(irx->r) == RID_XMM1) - irx->r = RID_INIT; /* Avoid allocating xmm1 for x. */ - ra_left(as, RID_XMM0, irpp->op1); - ra_left(as, RID_XMM1, irp->op2); - return 1; - } - } - return 0; -} - -static void asm_fpmath(ASMState *as, IRIns *ir) -{ - IRFPMathOp fpm = ir->o == IR_FPMATH ? (IRFPMathOp)ir->op2 : IRFPM_OTHER; - if (fpm == IRFPM_SQRT) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = asm_fuseload(as, ir->op1, RSET_FPR); - emit_mrm(as, XO_SQRTSD, dest, left); - } else if (fpm <= IRFPM_TRUNC) { - if (as->flags & JIT_F_SSE4_1) { /* SSE4.1 has a rounding instruction. */ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = asm_fuseload(as, ir->op1, RSET_FPR); - /* ROUNDSD has a 4-byte opcode which doesn't fit in x86Op. - ** Let's pretend it's a 3-byte opcode, and compensate afterwards. - ** This is atrocious, but the alternatives are much worse. - */ - /* Round down/up/trunc == 1001/1010/1011. */ - emit_i8(as, 0x09 + fpm); - emit_mrm(as, XO_ROUNDSD, dest, left); - if (LJ_64 && as->mcp[1] != (MCode)(XO_ROUNDSD >> 16)) { - as->mcp[0] = as->mcp[1]; as->mcp[1] = 0x0f; /* Swap 0F and REX. */ - } - *--as->mcp = 0x66; /* 1st byte of ROUNDSD opcode. */ - } else { /* Call helper functions for SSE2 variant. */ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - ra_destreg(as, ir, RID_XMM0); - emit_call(as, fpm == IRFPM_FLOOR ? lj_vm_floor_sse : - fpm == IRFPM_CEIL ? lj_vm_ceil_sse : lj_vm_trunc_sse); - ra_left(as, RID_XMM0, ir->op1); - } - } else if (fpm == IRFPM_EXP2 && fpmjoin_pow(as, ir)) { - /* Rejoined to pow(). */ - } else { /* Handle x87 ops. */ - int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rmro(as, XMM_MOVRM(as), dest, RID_ESP, ofs); - } - emit_rmro(as, XO_FSTPq, XOg_FSTPq, RID_ESP, ofs); - switch (fpm) { /* st0 = lj_vm_*(st0) */ - case IRFPM_EXP: emit_call(as, lj_vm_exp_x87); break; - case IRFPM_EXP2: emit_call(as, lj_vm_exp2_x87); break; - case IRFPM_SIN: emit_x87op(as, XI_FSIN); break; - case IRFPM_COS: emit_x87op(as, XI_FCOS); break; - case IRFPM_TAN: emit_x87op(as, XI_FPOP); emit_x87op(as, XI_FPTAN); break; - case IRFPM_LOG: case IRFPM_LOG2: case IRFPM_LOG10: - /* Note: the use of fyl2xp1 would be pointless here. When computing - ** log(1.0+eps) the precision is already lost after 1.0 is added. - ** Subtracting 1.0 won't recover it. OTOH math.log1p would make sense. - */ - emit_x87op(as, XI_FYL2X); break; - case IRFPM_OTHER: - switch (ir->o) { - case IR_ATAN2: - emit_x87op(as, XI_FPATAN); asm_x87load(as, ir->op2); break; - case IR_LDEXP: - emit_x87op(as, XI_FPOP1); emit_x87op(as, XI_FSCALE); break; - default: lua_assert(0); break; - } - break; - default: lua_assert(0); break; - } - asm_x87load(as, ir->op1); - switch (fpm) { - case IRFPM_LOG: emit_x87op(as, XI_FLDLN2); break; - case IRFPM_LOG2: emit_x87op(as, XI_FLD1); break; - case IRFPM_LOG10: emit_x87op(as, XI_FLDLG2); break; - case IRFPM_OTHER: - if (ir->o == IR_LDEXP) asm_x87load(as, ir->op2); - break; - default: break; - } - } -} - -static void asm_fppowi(ASMState *as, IRIns *ir) -{ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM1+1)|RID2RSET(RID_EAX); - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - ra_destreg(as, ir, RID_XMM0); - emit_call(as, lj_vm_powi_sse); - ra_left(as, RID_XMM0, ir->op1); - ra_left(as, RID_EAX, ir->op2); -} - -#if LJ_64 && LJ_HASFFI -static void asm_arith64(ASMState *as, IRIns *ir, IRCallID id) -{ - const CCallInfo *ci = &lj_ir_callinfo[id]; - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} -#endif - -static void asm_intmod(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_vm_modi]; - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -static int asm_swapops(ASMState *as, IRIns *ir) -{ - IRIns *irl = IR(ir->op1); - IRIns *irr = IR(ir->op2); - lua_assert(ra_noreg(irr->r)); - if (!irm_iscomm(lj_ir_mode[ir->o])) - return 0; /* Can't swap non-commutative operations. */ - if (irref_isk(ir->op2)) - return 0; /* Don't swap constants to the left. */ - if (ra_hasreg(irl->r)) - return 1; /* Swap if left already has a register. */ - if (ra_samehint(ir->r, irr->r)) - return 1; /* Swap if dest and right have matching hints. */ - if (as->curins > as->loopref) { /* In variant part? */ - if (ir->op2 < as->loopref && !irt_isphi(irr->t)) - return 0; /* Keep invariants on the right. */ - if (ir->op1 < as->loopref && !irt_isphi(irl->t)) - return 1; /* Swap invariants to the right. */ - } - if (opisfusableload(irl->o)) - return 1; /* Swap fusable loads to the right. */ - return 0; /* Otherwise don't swap. */ -} - -static void asm_fparith(ASMState *as, IRIns *ir, x86Op xo) -{ - IRRef lref = ir->op1; - IRRef rref = ir->op2; - RegSet allow = RSET_FPR; - Reg dest; - Reg right = IR(rref)->r; - if (ra_hasreg(right)) { - rset_clear(allow, right); - ra_noweak(as, right); - } - dest = ra_dest(as, ir, allow); - if (lref == rref) { - right = dest; - } else if (ra_noreg(right)) { - if (asm_swapops(as, ir)) { - IRRef tmp = lref; lref = rref; rref = tmp; - } - right = asm_fuseload(as, rref, rset_clear(allow, dest)); - } - emit_mrm(as, xo, dest, right); - ra_left(as, dest, lref); -} - -static void asm_intarith(ASMState *as, IRIns *ir, x86Arith xa) -{ - IRRef lref = ir->op1; - IRRef rref = ir->op2; - RegSet allow = RSET_GPR; - Reg dest, right; - int32_t k = 0; - if (as->flagmcp == as->mcp) { /* Drop test r,r instruction. */ - as->flagmcp = NULL; - as->mcp += (LJ_64 && *as->mcp < XI_TESTb) ? 3 : 2; - } - right = IR(rref)->r; - if (ra_hasreg(right)) { - rset_clear(allow, right); - ra_noweak(as, right); - } - dest = ra_dest(as, ir, allow); - if (lref == rref) { - right = dest; - } else if (ra_noreg(right) && !asm_isk32(as, rref, &k)) { - if (asm_swapops(as, ir)) { - IRRef tmp = lref; lref = rref; rref = tmp; - } - right = asm_fuseloadm(as, rref, rset_clear(allow, dest), irt_is64(ir->t)); - } - if (irt_isguard(ir->t)) /* For IR_ADDOV etc. */ - asm_guardcc(as, CC_O); - if (xa != XOg_X_IMUL) { - if (ra_hasreg(right)) - emit_mrm(as, XO_ARITH(xa), REX_64IR(ir, dest), right); - else - emit_gri(as, XG_ARITHi(xa), REX_64IR(ir, dest), k); - } else if (ra_hasreg(right)) { /* IMUL r, mrm. */ - emit_mrm(as, XO_IMUL, REX_64IR(ir, dest), right); - } else { /* IMUL r, r, k. */ - /* NYI: use lea/shl/add/sub (FOLD only does 2^k) depending on CPU. */ - Reg left = asm_fuseloadm(as, lref, RSET_GPR, irt_is64(ir->t)); - x86Op xo; - if (checki8(k)) { emit_i8(as, k); xo = XO_IMULi8; - } else { emit_i32(as, k); xo = XO_IMULi; } - emit_mrm(as, xo, REX_64IR(ir, dest), left); - return; - } - ra_left(as, dest, lref); -} - -/* LEA is really a 4-operand ADD with an independent destination register, -** up to two source registers and an immediate. One register can be scaled -** by 1, 2, 4 or 8. This can be used to avoid moves or to fuse several -** instructions. -** -** Currently only a few common cases are supported: -** - 3-operand ADD: y = a+b; y = a+k with a and b already allocated -** - Left ADD fusion: y = (a+b)+k; y = (a+k)+b -** - Right ADD fusion: y = a+(b+k) -** The ommited variants have already been reduced by FOLD. -** -** There are more fusion opportunities, like gathering shifts or joining -** common references. But these are probably not worth the trouble, since -** array indexing is not decomposed and already makes use of all fields -** of the ModRM operand. -*/ -static int asm_lea(ASMState *as, IRIns *ir) -{ - IRIns *irl = IR(ir->op1); - IRIns *irr = IR(ir->op2); - RegSet allow = RSET_GPR; - Reg dest; - as->mrm.base = as->mrm.idx = RID_NONE; - as->mrm.scale = XM_SCALE1; - as->mrm.ofs = 0; - if (ra_hasreg(irl->r)) { - rset_clear(allow, irl->r); - ra_noweak(as, irl->r); - as->mrm.base = irl->r; - if (irref_isk(ir->op2) || ra_hasreg(irr->r)) { - /* The PHI renaming logic does a better job in some cases. */ - if (ra_hasreg(ir->r) && - ((irt_isphi(irl->t) && as->phireg[ir->r] == ir->op1) || - (irt_isphi(irr->t) && as->phireg[ir->r] == ir->op2))) - return 0; - if (irref_isk(ir->op2)) { - as->mrm.ofs = irr->i; - } else { - rset_clear(allow, irr->r); - ra_noweak(as, irr->r); - as->mrm.idx = irr->r; - } - } else if (irr->o == IR_ADD && mayfuse(as, ir->op2) && - irref_isk(irr->op2)) { - Reg idx = ra_alloc1(as, irr->op1, allow); - rset_clear(allow, idx); - as->mrm.idx = (uint8_t)idx; - as->mrm.ofs = IR(irr->op2)->i; - } else { - return 0; - } - } else if (ir->op1 != ir->op2 && irl->o == IR_ADD && mayfuse(as, ir->op1) && - (irref_isk(ir->op2) || irref_isk(irl->op2))) { - Reg idx, base = ra_alloc1(as, irl->op1, allow); - rset_clear(allow, base); - as->mrm.base = (uint8_t)base; - if (irref_isk(ir->op2)) { - as->mrm.ofs = irr->i; - idx = ra_alloc1(as, irl->op2, allow); - } else { - as->mrm.ofs = IR(irl->op2)->i; - idx = ra_alloc1(as, ir->op2, allow); - } - rset_clear(allow, idx); - as->mrm.idx = (uint8_t)idx; - } else { - return 0; - } - dest = ra_dest(as, ir, allow); - emit_mrm(as, XO_LEA, dest, RID_MRM); - return 1; /* Success. */ -} - -static void asm_add(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_ADDSD); - else if ((as->flags & JIT_F_LEA_AGU) || as->flagmcp == as->mcp || - irt_is64(ir->t) || !asm_lea(as, ir)) - asm_intarith(as, ir, XOg_ADD); -} - -static void asm_neg_not(ASMState *as, IRIns *ir, x86Group3 xg) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - emit_rr(as, XO_GROUP3, REX_64IR(ir, xg), dest); - ra_left(as, dest, ir->op1); -} - -static void asm_min_max(ASMState *as, IRIns *ir, int cc) -{ - Reg right, dest = ra_dest(as, ir, RSET_GPR); - IRRef lref = ir->op1, rref = ir->op2; - if (irref_isk(rref)) { lref = rref; rref = ir->op1; } - right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, dest)); - emit_rr(as, XO_CMOV + (cc<<24), REX_64IR(ir, dest), right); - emit_rr(as, XO_CMP, REX_64IR(ir, dest), right); - ra_left(as, dest, lref); -} - -static void asm_bitswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - as->mcp = emit_op(XO_BSWAP + ((dest&7) << 24), - REX_64IR(ir, 0), dest, 0, as->mcp, 1); - ra_left(as, dest, ir->op1); -} - -static void asm_bitshift(ASMState *as, IRIns *ir, x86Shift xs) -{ - IRRef rref = ir->op2; - IRIns *irr = IR(rref); - Reg dest; - if (irref_isk(rref)) { /* Constant shifts. */ - int shift; - dest = ra_dest(as, ir, RSET_GPR); - shift = irr->i & (irt_is64(ir->t) ? 63 : 31); - switch (shift) { - case 0: break; - case 1: emit_rr(as, XO_SHIFT1, REX_64IR(ir, xs), dest); break; - default: emit_shifti(as, REX_64IR(ir, xs), dest, shift); break; - } - } else { /* Variable shifts implicitly use register cl (i.e. ecx). */ - Reg right; - dest = ra_dest(as, ir, rset_exclude(RSET_GPR, RID_ECX)); - if (dest == RID_ECX) { - dest = ra_scratch(as, rset_exclude(RSET_GPR, RID_ECX)); - emit_rr(as, XO_MOV, RID_ECX, dest); - } - right = irr->r; - if (ra_noreg(right)) - right = ra_allocref(as, rref, RID2RSET(RID_ECX)); - else if (right != RID_ECX) - ra_scratch(as, RID2RSET(RID_ECX)); - emit_rr(as, XO_SHIFTcl, REX_64IR(ir, xs), dest); - ra_noweak(as, right); - if (right != RID_ECX) - emit_rr(as, XO_MOV, RID_ECX, right); - } - ra_left(as, dest, ir->op1); - /* - ** Note: avoid using the flags resulting from a shift or rotate! - ** All of them cause a partial flag stall, except for r,1 shifts - ** (but not rotates). And a shift count of 0 leaves the flags unmodified. - */ -} - -/* -- Comparisons --------------------------------------------------------- */ - -/* Virtual flags for unordered FP comparisons. */ -#define VCC_U 0x1000 /* Unordered. */ -#define VCC_P 0x2000 /* Needs extra CC_P branch. */ -#define VCC_S 0x4000 /* Swap avoids CC_P branch. */ -#define VCC_PS (VCC_P|VCC_S) - -/* Map of comparisons to flags. ORDER IR. */ -#define COMPFLAGS(ci, cin, cu, cf) ((ci)+((cu)<<4)+((cin)<<8)+(cf)) -static const uint16_t asm_compmap[IR_ABC+1] = { - /* signed non-eq unsigned flags */ - /* LT */ COMPFLAGS(CC_GE, CC_G, CC_AE, VCC_PS), - /* GE */ COMPFLAGS(CC_L, CC_L, CC_B, 0), - /* LE */ COMPFLAGS(CC_G, CC_G, CC_A, VCC_PS), - /* GT */ COMPFLAGS(CC_LE, CC_L, CC_BE, 0), - /* ULT */ COMPFLAGS(CC_AE, CC_A, CC_AE, VCC_U), - /* UGE */ COMPFLAGS(CC_B, CC_B, CC_B, VCC_U|VCC_PS), - /* ULE */ COMPFLAGS(CC_A, CC_A, CC_A, VCC_U), - /* UGT */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS), - /* EQ */ COMPFLAGS(CC_NE, CC_NE, CC_NE, VCC_P), - /* NE */ COMPFLAGS(CC_E, CC_E, CC_E, VCC_U|VCC_P), - /* ABC */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS) /* Same as UGT. */ -}; - -/* FP and integer comparisons. */ -static void asm_comp(ASMState *as, IRIns *ir, uint32_t cc) -{ - if (irt_isnum(ir->t)) { - IRRef lref = ir->op1; - IRRef rref = ir->op2; - Reg left, right; - MCLabel l_around; - /* - ** An extra CC_P branch is required to preserve ordered/unordered - ** semantics for FP comparisons. This can be avoided by swapping - ** the operands and inverting the condition (except for EQ and UNE). - ** So always try to swap if possible. - ** - ** Another option would be to swap operands to achieve better memory - ** operand fusion. But it's unlikely that this outweighs the cost - ** of the extra branches. - */ - if (cc & VCC_S) { /* Swap? */ - IRRef tmp = lref; lref = rref; rref = tmp; - cc ^= (VCC_PS|(5<<4)); /* A <-> B, AE <-> BE, PS <-> none */ - } - left = ra_alloc1(as, lref, RSET_FPR); - right = asm_fuseload(as, rref, rset_exclude(RSET_FPR, left)); - l_around = emit_label(as); - asm_guardcc(as, cc >> 4); - if (cc & VCC_P) { /* Extra CC_P branch required? */ - if (!(cc & VCC_U)) { - asm_guardcc(as, CC_P); /* Branch to exit for ordered comparisons. */ - } else if (l_around != as->invmcp) { - emit_sjcc(as, CC_P, l_around); /* Branch around for unordered. */ - } else { - /* Patched to mcloop by asm_loop_fixup. */ - as->loopinv = 2; - if (as->realign) - emit_sjcc(as, CC_P, as->mcp); - else - emit_jcc(as, CC_P, as->mcp); - } - } - emit_mrm(as, XO_UCOMISD, left, right); - } else { - IRRef lref = ir->op1, rref = ir->op2; - IROp leftop = (IROp)(IR(lref)->o); - Reg r64 = REX_64IR(ir, 0); - int32_t imm = 0; - lua_assert(irt_is64(ir->t) || irt_isint(ir->t) || - irt_isu32(ir->t) || irt_isaddr(ir->t) || irt_isu8(ir->t)); - /* Swap constants (only for ABC) and fusable loads to the right. */ - if (irref_isk(lref) || (!irref_isk(rref) && opisfusableload(leftop))) { - if ((cc & 0xc) == 0xc) cc ^= 0x53; /* L <-> G, LE <-> GE */ - else if ((cc & 0xa) == 0x2) cc ^= 0x55; /* A <-> B, AE <-> BE */ - lref = ir->op2; rref = ir->op1; - } - if (asm_isk32(as, rref, &imm)) { - IRIns *irl = IR(lref); - /* Check wether we can use test ins. Not for unsigned, since CF=0. */ - int usetest = (imm == 0 && (cc & 0xa) != 0x2); - if (usetest && irl->o == IR_BAND && irl+1 == ir && !ra_used(irl)) { - /* Combine comp(BAND(ref, r/imm), 0) into test mrm, r/imm. */ - Reg right, left = RID_NONE; - RegSet allow = RSET_GPR; - if (!asm_isk32(as, irl->op2, &imm)) { - left = ra_alloc1(as, irl->op2, allow); - rset_clear(allow, left); - } else { /* Try to Fuse IRT_I8/IRT_U8 loads, too. See below. */ - IRIns *irll = IR(irl->op1); - if (opisfusableload((IROp)irll->o) && - (irt_isi8(irll->t) || irt_isu8(irll->t))) { - IRType1 origt = irll->t; /* Temporarily flip types. */ - irll->t.irt = (irll->t.irt & ~IRT_TYPE) | IRT_INT; - as->curins--; /* Skip to BAND to avoid failing in noconflict(). */ - right = asm_fuseload(as, irl->op1, RSET_GPR); - as->curins++; - irll->t = origt; - if (right != RID_MRM) goto test_nofuse; - /* Fusion succeeded, emit test byte mrm, imm8. */ - asm_guardcc(as, cc); - emit_i8(as, (imm & 0xff)); - emit_mrm(as, XO_GROUP3b, XOg_TEST, RID_MRM); - return; - } - } - as->curins--; /* Skip to BAND to avoid failing in noconflict(). */ - right = asm_fuseloadm(as, irl->op1, allow, r64); - as->curins++; /* Undo the above. */ - test_nofuse: - asm_guardcc(as, cc); - if (ra_noreg(left)) { - emit_i32(as, imm); - emit_mrm(as, XO_GROUP3, r64 + XOg_TEST, right); - } else { - emit_mrm(as, XO_TEST, r64 + left, right); - } - } else { - Reg left; - if (opisfusableload((IROp)irl->o) && - ((irt_isu8(irl->t) && checku8(imm)) || - ((irt_isi8(irl->t) || irt_isi16(irl->t)) && checki8(imm)) || - (irt_isu16(irl->t) && checku16(imm) && checki8((int16_t)imm)))) { - /* Only the IRT_INT case is fused by asm_fuseload. - ** The IRT_I8/IRT_U8 loads and some IRT_I16/IRT_U16 loads - ** are handled here. - ** Note that cmp word [mem], imm16 should not be generated, - ** since it has a length-changing prefix. Compares of a word - ** against a sign-extended imm8 are ok, however. - */ - IRType1 origt = irl->t; /* Temporarily flip types. */ - irl->t.irt = (irl->t.irt & ~IRT_TYPE) | IRT_INT; - left = asm_fuseload(as, lref, RSET_GPR); - irl->t = origt; - if (left == RID_MRM) { /* Fusion succeeded? */ - if (irt_isu8(irl->t) || irt_isu16(irl->t)) - cc >>= 4; /* Need unsigned compare. */ - asm_guardcc(as, cc); - emit_i8(as, imm); - emit_mrm(as, (irt_isi8(origt) || irt_isu8(origt)) ? - XO_ARITHib : XO_ARITHiw8, r64 + XOg_CMP, RID_MRM); - return; - } /* Otherwise handle register case as usual. */ - } else { - left = asm_fuseloadm(as, lref, - irt_isu8(ir->t) ? RSET_GPR8 : RSET_GPR, r64); - } - asm_guardcc(as, cc); - if (usetest && left != RID_MRM) { - /* Use test r,r instead of cmp r,0. */ - x86Op xo = XO_TEST; - if (irt_isu8(ir->t)) { - lua_assert(ir->o == IR_EQ || ir->o == IR_NE); - xo = XO_TESTb; - if (!rset_test(RSET_RANGE(RID_EAX, RID_EBX+1), left)) { - if (LJ_64) { - left |= FORCE_REX; - } else { - emit_i32(as, 0xff); - emit_mrm(as, XO_GROUP3, XOg_TEST, left); - return; - } - } - } - emit_rr(as, xo, r64 + left, left); - if (irl+1 == ir) /* Referencing previous ins? */ - as->flagmcp = as->mcp; /* Set flag to drop test r,r if possible. */ - } else { - emit_gmrmi(as, XG_ARITHi(XOg_CMP), r64 + left, imm); - } - } - } else { - Reg left = ra_alloc1(as, lref, RSET_GPR); - Reg right = asm_fuseloadm(as, rref, rset_exclude(RSET_GPR, left), r64); - asm_guardcc(as, cc); - emit_mrm(as, XO_CMP, r64 + left, right); - } - } -} - -#if LJ_32 && LJ_HASFFI -/* 64 bit integer comparisons in 32 bit mode. */ -static void asm_comp_int64(ASMState *as, IRIns *ir) -{ - uint32_t cc = asm_compmap[(ir-1)->o]; - RegSet allow = RSET_GPR; - Reg lefthi = RID_NONE, leftlo = RID_NONE; - Reg righthi = RID_NONE, rightlo = RID_NONE; - MCLabel l_around; - x86ModRM mrm; - - as->curins--; /* Skip loword ins. Avoids failing in noconflict(), too. */ - - /* Allocate/fuse hiword operands. */ - if (irref_isk(ir->op2)) { - lefthi = asm_fuseload(as, ir->op1, allow); - } else { - lefthi = ra_alloc1(as, ir->op1, allow); - rset_clear(allow, lefthi); - righthi = asm_fuseload(as, ir->op2, allow); - if (righthi == RID_MRM) { - if (as->mrm.base != RID_NONE) rset_clear(allow, as->mrm.base); - if (as->mrm.idx != RID_NONE) rset_clear(allow, as->mrm.idx); - } else { - rset_clear(allow, righthi); - } - } - mrm = as->mrm; /* Save state for hiword instruction. */ - - /* Allocate/fuse loword operands. */ - if (irref_isk((ir-1)->op2)) { - leftlo = asm_fuseload(as, (ir-1)->op1, allow); - } else { - leftlo = ra_alloc1(as, (ir-1)->op1, allow); - rset_clear(allow, leftlo); - rightlo = asm_fuseload(as, (ir-1)->op2, allow); - } - - /* All register allocations must be performed _before_ this point. */ - l_around = emit_label(as); - as->invmcp = as->flagmcp = NULL; /* Cannot use these optimizations. */ - - /* Loword comparison and branch. */ - asm_guardcc(as, cc >> 4); /* Always use unsigned compare for loword. */ - if (ra_noreg(rightlo)) { - int32_t imm = IR((ir-1)->op2)->i; - if (imm == 0 && ((cc >> 4) & 0xa) != 0x2 && leftlo != RID_MRM) - emit_rr(as, XO_TEST, leftlo, leftlo); - else - emit_gmrmi(as, XG_ARITHi(XOg_CMP), leftlo, imm); - } else { - emit_mrm(as, XO_CMP, leftlo, rightlo); - } - - /* Hiword comparison and branches. */ - if ((cc & 15) != CC_NE) - emit_sjcc(as, CC_NE, l_around); /* Hiword unequal: skip loword compare. */ - if ((cc & 15) != CC_E) - asm_guardcc(as, cc >> 8); /* Hiword compare without equality check. */ - as->mrm = mrm; /* Restore state. */ - if (ra_noreg(righthi)) { - int32_t imm = IR(ir->op2)->i; - if (imm == 0 && (cc & 0xa) != 0x2 && lefthi != RID_MRM) - emit_rr(as, XO_TEST, lefthi, lefthi); - else - emit_gmrmi(as, XG_ARITHi(XOg_CMP), lefthi, imm); - } else { - emit_mrm(as, XO_CMP, lefthi, righthi); - } -} -#endif - -/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ - -/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ -#if LJ_32 && LJ_HASFFI - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; - if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ - if (usehi || uselo) { - if (irt_isfp(ir->t)) - asm_conv_fp_int64(as, ir); - else - asm_conv_int64_fp(as, ir); - } - as->curins--; /* Always skip the CONV. */ - return; - } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ - asm_comp_int64(as, ir); - return; - } else if ((ir-1)->o == IR_XSTORE) { - if ((ir-1)->r != RID_SINK) - asm_fxstore(as, ir); - return; - } - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { - case IR_ADD: - as->flagmcp = NULL; - as->curins--; - asm_intarith(as, ir, XOg_ADC); - asm_intarith(as, ir-1, XOg_ADD); - break; - case IR_SUB: - as->flagmcp = NULL; - as->curins--; - asm_intarith(as, ir, XOg_SBB); - asm_intarith(as, ir-1, XOg_SUB); - break; - case IR_NEG: { - Reg dest = ra_dest(as, ir, RSET_GPR); - emit_rr(as, XO_GROUP3, XOg_NEG, dest); - emit_i8(as, 0); - emit_rr(as, XO_ARITHi8, XOg_ADC, dest); - ra_left(as, dest, ir->op1); - as->curins--; - asm_neg_not(as, ir-1, XOg_NEG); - break; - } - case IR_CALLN: - case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - case IR_CNEWI: - /* Nothing to do here. Handled by CNEWI itself. */ - break; - default: lua_assert(0); break; - } -#else - UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused on x64 or without FFI. */ -#endif -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - /* Try to get an unused temp. register, otherwise spill/restore eax. */ - Reg pbase = irp ? irp->r : RID_BASE; - Reg r = allow ? rset_pickbot(allow) : RID_EAX; - emit_jcc(as, CC_B, exitstub_addr(as->J, exitno)); - if (allow == RSET_EMPTY) /* Restore temp. register. */ - emit_rmro(as, XO_MOV, r|REX_64, RID_ESP, 0); - else - ra_modified(as, r); - emit_gri(as, XG_ARITHi(XOg_CMP), r, (int32_t)(8*topslot)); - if (ra_hasreg(pbase) && pbase != r) - emit_rr(as, XO_ARITH(XOg_SUB), r, pbase); - else - emit_rmro(as, XO_ARITH(XOg_SUB), r, RID_NONE, - ptr2addr(&J2G(as->J)->jit_base)); - emit_rmro(as, XO_MOV, r, r, offsetof(lua_State, maxstack)); - emit_getgl(as, r, jit_L); - if (allow == RSET_EMPTY) /* Spill temp. register. */ - emit_rmro(as, XO_MOVto, r|REX_64, RID_ESP, 0); -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if (irt_isnum(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_rmro(as, XO_MOVSDto, src, RID_BASE, ofs); - } else { - lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || - (LJ_DUALNUM && irt_isinteger(ir->t))); - if (!irref_isk(ref)) { - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE)); - emit_movtomro(as, REX_64IR(ir, src), RID_BASE, ofs); - } else if (!irt_ispri(ir->t)) { - emit_movmroi(as, RID_BASE, ofs, ir->i); - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { - if (s != 0) /* Do not overwrite link to previous frame. */ - emit_movmroi(as, RID_BASE, ofs+4, (int32_t)(*flinks--)); - } else { - if (!(LJ_64 && irt_islightud(ir->t))) - emit_movmroi(as, RID_BASE, ofs+4, irt_toitype(ir->t)); - } - } - checkmclim(as); - } - lua_assert(map + nent == flinks); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ - emit_rr(as, XO_TEST, RID_RET, RID_RET); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - tmp = ra_releasetmp(as, ASMREF_TMP1); - emit_loada(as, tmp, J2G(as->J)); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP2), as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_sjcc(as, CC_B, l_end); - emit_opgl(as, XO_ARITH(XOg_CMP), tmp, gc.threshold); - emit_getgl(as, tmp, gc.total); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - if (as->realign) { /* Realigned loops use short jumps. */ - as->realign = NULL; /* Stop another retry. */ - lua_assert(((intptr_t)target & 15) == 0); - if (as->loopinv) { /* Inverted loop branch? */ - p -= 5; - p[0] = XI_JMP; - lua_assert(target - p >= -128); - p[-1] = (MCode)(target - p); /* Patch sjcc. */ - if (as->loopinv == 2) - p[-3] = (MCode)(target - p + 2); /* Patch opt. short jp. */ - } else { - lua_assert(target - p >= -128); - p[-1] = (MCode)(int8_t)(target - p); /* Patch short jmp. */ - p[-2] = XI_JMPs; - } - } else { - MCode *newloop; - p[-5] = XI_JMP; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guardcc already inverted the jcc and patched the jmp. */ - p -= 5; - newloop = target+4; - *(int32_t *)(p-4) = (int32_t)(target - p); /* Patch jcc. */ - if (as->loopinv == 2) { - *(int32_t *)(p-10) = (int32_t)(target - p + 6); /* Patch opt. jp. */ - newloop = target+8; - } - } else { /* Otherwise just patch jmp. */ - *(int32_t *)(p-4) = (int32_t)(target - p); - newloop = target+3; - } - /* Realign small loops and shorten the loop branch. */ - if (newloop >= p - 128) { - as->realign = newloop; /* Force a retry and remember alignment. */ - as->curins = as->stopins; /* Abort asm_trace now. */ - as->T->nins = as->orignins; /* Remove any added renames. */ - } - } -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (r != RID_BASE) - emit_rr(as, XO_MOV, r, RID_BASE); - } -} - -/* Coalesce or reload BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ - } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - rset_clear(allow, irp->r); - emit_rr(as, XO_MOV, r, irp->r); /* Move from coalesced parent reg. */ - } else { - emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ - } - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - /* Note: don't use as->mcp swap + emit_*: emit_op overwrites more bytes. */ - MCode *p = as->mctop; - MCode *target, *q; - int32_t spadj = as->T->spadjust; - if (spadj == 0) { - p -= ((as->flags & JIT_F_LEA_AGU) ? 7 : 6) + (LJ_64 ? 1 : 0); - } else { - MCode *p1; - /* Patch stack adjustment. */ - if (checki8(spadj)) { - p -= 3; - p1 = p-6; - *p1 = (MCode)spadj; - } else { - p1 = p-9; - *(int32_t *)p1 = spadj; - } - if ((as->flags & JIT_F_LEA_AGU)) { -#if LJ_64 - p1[-4] = 0x48; -#endif - p1[-3] = (MCode)XI_LEA; - p1[-2] = MODRM(checki8(spadj) ? XM_OFS8 : XM_OFS32, RID_ESP, RID_ESP); - p1[-1] = MODRM(XM_SCALE1, RID_ESP, RID_ESP); - } else { -#if LJ_64 - p1[-3] = 0x48; -#endif - p1[-2] = (MCode)(checki8(spadj) ? XI_ARITHi8 : XI_ARITHi); - p1[-1] = MODRM(XM_REG, XOg_ADD, RID_ESP); - } - } - /* Patch exit branch. */ - target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; - *(int32_t *)(p-4) = jmprel(p, target); - p[-5] = XI_JMP; - /* Drop unused mcode tail. Fill with NOPs to make the prefetcher happy. */ - for (q = as->mctop-1; q >= p; q--) - *q = XI_NOP; - as->mctop = p; -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - MCode *p = as->mctop; - /* Realign and leave room for backwards loop branch or exit branch. */ - if (as->realign) { - int i = ((int)(intptr_t)as->realign) & 15; - /* Fill unused mcode tail with NOPs to make the prefetcher happy. */ - while (i-- > 0) - *--p = XI_NOP; - as->mctop = p; - p -= (as->loopinv ? 5 : 2); /* Space for short/near jmp. */ - } else { - p -= 5; /* Space for exit branch (near jmp). */ - } - if (as->loopref) { - as->invmcp = as->mcp = p; - } else { - /* Leave room for ESP adjustment: add esp, imm or lea esp, [esp+imm] */ - as->mcp = p - (((as->flags & JIT_F_LEA_AGU) ? 7 : 6) + (LJ_64 ? 1 : 0)); - as->invmcp = NULL; - } -} - -/* -- Instruction dispatch ------------------------------------------------ */ - -/* Assemble a single instruction. */ -static void asm_ir(ASMState *as, IRIns *ir) -{ - switch ((IROp)ir->o) { - /* Miscellaneous ops. */ - case IR_LOOP: asm_loop(as); break; - case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; - case IR_USE: - ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; - case IR_PHI: asm_phi(as, ir); break; - case IR_HIOP: asm_hiop(as, ir); break; - case IR_GCSTEP: asm_gcstep(as, ir); break; - - /* Guarded assertions. */ - case IR_LT: case IR_GE: case IR_LE: case IR_GT: - case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: - case IR_EQ: case IR_NE: case IR_ABC: - asm_comp(as, ir, asm_compmap[ir->o]); - break; - - case IR_RETF: asm_retf(as, ir); break; - - /* Bit ops. */ - case IR_BNOT: asm_neg_not(as, ir, XOg_NOT); break; - case IR_BSWAP: asm_bitswap(as, ir); break; - - case IR_BAND: asm_intarith(as, ir, XOg_AND); break; - case IR_BOR: asm_intarith(as, ir, XOg_OR); break; - case IR_BXOR: asm_intarith(as, ir, XOg_XOR); break; - - case IR_BSHL: asm_bitshift(as, ir, XOg_SHL); break; - case IR_BSHR: asm_bitshift(as, ir, XOg_SHR); break; - case IR_BSAR: asm_bitshift(as, ir, XOg_SAR); break; - case IR_BROL: asm_bitshift(as, ir, XOg_ROL); break; - case IR_BROR: asm_bitshift(as, ir, XOg_ROR); break; - - /* Arithmetic ops. */ - case IR_ADD: asm_add(as, ir); break; - case IR_SUB: - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_SUBSD); - else /* Note: no need for LEA trick here. i-k is encoded as i+(-k). */ - asm_intarith(as, ir, XOg_SUB); - break; - case IR_MUL: - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_MULSD); - else - asm_intarith(as, ir, XOg_X_IMUL); - break; - case IR_DIV: -#if LJ_64 && LJ_HASFFI - if (!irt_isnum(ir->t)) - asm_arith64(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : - IRCALL_lj_carith_divu64); - else -#endif - asm_fparith(as, ir, XO_DIVSD); - break; - case IR_MOD: -#if LJ_64 && LJ_HASFFI - if (!irt_isint(ir->t)) - asm_arith64(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : - IRCALL_lj_carith_modu64); - else -#endif - asm_intmod(as, ir); - break; - - case IR_NEG: - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_XORPS); - else - asm_neg_not(as, ir, XOg_NEG); - break; - case IR_ABS: asm_fparith(as, ir, XO_ANDPS); break; - - case IR_MIN: - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_MINSD); - else - asm_min_max(as, ir, CC_G); - break; - case IR_MAX: - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_MAXSD); - else - asm_min_max(as, ir, CC_L); - break; - - case IR_FPMATH: case IR_ATAN2: case IR_LDEXP: - asm_fpmath(as, ir); - break; - case IR_POW: -#if LJ_64 && LJ_HASFFI - if (!irt_isnum(ir->t)) - asm_arith64(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : - IRCALL_lj_carith_powu64); - else -#endif - asm_fppowi(as, ir); - break; - - /* Overflow-checking arithmetic ops. Note: don't use LEA here! */ - case IR_ADDOV: asm_intarith(as, ir, XOg_ADD); break; - case IR_SUBOV: asm_intarith(as, ir, XOg_SUB); break; - case IR_MULOV: asm_intarith(as, ir, XOg_X_IMUL); break; - - /* Memory references. */ - case IR_AREF: asm_aref(as, ir); break; - case IR_HREF: asm_href(as, ir); break; - case IR_HREFK: asm_hrefk(as, ir); break; - case IR_NEWREF: asm_newref(as, ir); break; - case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; - case IR_FREF: asm_fref(as, ir); break; - case IR_STRREF: asm_strref(as, ir); break; - - /* Loads and stores. */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - asm_ahuvload(as, ir); - break; - case IR_FLOAD: case IR_XLOAD: asm_fxload(as, ir); break; - case IR_SLOAD: asm_sload(as, ir); break; - - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; - case IR_FSTORE: case IR_XSTORE: asm_fxstore(as, ir); break; - - /* Allocations. */ - case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; - case IR_TNEW: asm_tnew(as, ir); break; - case IR_TDUP: asm_tdup(as, ir); break; - case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; - - /* Write barriers. */ - case IR_TBAR: asm_tbar(as, ir); break; - case IR_OBAR: asm_obar(as, ir); break; - - /* Type conversions. */ - case IR_TOBIT: asm_tobit(as, ir); break; - case IR_CONV: asm_conv(as, ir); break; - case IR_TOSTR: asm_tostr(as, ir); break; - case IR_STRTO: asm_strto(as, ir); break; - - /* Calls. */ - case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; - case IR_CALLXS: asm_callx(as, ir); break; - case IR_CARG: break; - - default: - setintV(&as->J->errinfo, ir->o); - lj_trace_err_info(as->J, LJ_TRERR_NYIIR); - break; - } -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - int nslots; - asm_collectargs(as, ir, ci, args); - nslots = asm_count_call_slots(as, ci, args); - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; -#if LJ_64 - return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); -#else - return irt_isfp(ir->t) ? REGSP_INIT : REGSP_HINT(RID_RET); -#endif -} - -/* Target-specific setup. */ -static void asm_setup_target(ASMState *as) -{ - asm_exitstub_setup(as, as->T->nsnap); -} - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *mcarea = lj_mcode_patch(J, p, 0); - MSize len = T->szmcode; - MCode *px = exitstub_addr(J, exitno) - 6; - MCode *pe = p+len-6; - uint32_t stateaddr = u32ptr(&J2G(J)->vmstate); - if (len > 5 && p[len-5] == XI_JMP && p+len-6 + *(int32_t *)(p+len-4) == px) - *(int32_t *)(p+len-4) = jmprel(p+len, target); - /* Do not patch parent exit for a stack check. Skip beyond vmstate update. */ - for (; p < pe; p++) - if (*(uint32_t *)(p+(LJ_64 ? 3 : 2)) == stateaddr && p[0] == XI_MOVmi) { - p += LJ_64 ? 11 : 10; - break; - } - lua_assert(p < pe); - for (; p < pe; p++) { - if ((*(uint16_t *)p & 0xf0ff) == 0x800f && p + *(int32_t *)(p+2) == px) { - *(int32_t *)(p+2) = jmprel(p+6, target); - p += 5; - } - } - lj_mcode_sync(T->mcode, T->mcode + T->szmcode); - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_bc.c b/third-party/LuaJIT-2.0.2/src/lj_bc.c deleted file mode 100644 index 1e5869e6e1..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_bc.c +++ /dev/null @@ -1,14 +0,0 @@ -/* -** Bytecode instruction modes. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_bc_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_bc.h" - -/* Bytecode offsets and bytecode instruction modes. */ -#include "lj_bcdef.h" - diff --git a/third-party/LuaJIT-2.0.2/src/lj_bc.h b/third-party/LuaJIT-2.0.2/src/lj_bc.h deleted file mode 100644 index 56e71dd9dc..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_bc.h +++ /dev/null @@ -1,261 +0,0 @@ -/* -** Bytecode instruction format. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_BC_H -#define _LJ_BC_H - -#include "lj_def.h" -#include "lj_arch.h" - -/* Bytecode instruction format, 32 bit wide, fields of 8 or 16 bit: -** -** +----+----+----+----+ -** | B | C | A | OP | Format ABC -** +----+----+----+----+ -** | D | A | OP | Format AD -** +-------------------- -** MSB LSB -** -** In-memory instructions are always stored in host byte order. -*/ - -/* Operand ranges and related constants. */ -#define BCMAX_A 0xff -#define BCMAX_B 0xff -#define BCMAX_C 0xff -#define BCMAX_D 0xffff -#define BCBIAS_J 0x8000 -#define NO_REG BCMAX_A -#define NO_JMP (~(BCPos)0) - -/* Macros to get instruction fields. */ -#define bc_op(i) ((BCOp)((i)&0xff)) -#define bc_a(i) ((BCReg)(((i)>>8)&0xff)) -#define bc_b(i) ((BCReg)((i)>>24)) -#define bc_c(i) ((BCReg)(((i)>>16)&0xff)) -#define bc_d(i) ((BCReg)((i)>>16)) -#define bc_j(i) ((ptrdiff_t)bc_d(i)-BCBIAS_J) - -/* Macros to set instruction fields. */ -#define setbc_byte(p, x, ofs) \ - ((uint8_t *)(p))[LJ_ENDIAN_SELECT(ofs, 3-ofs)] = (uint8_t)(x) -#define setbc_op(p, x) setbc_byte(p, (x), 0) -#define setbc_a(p, x) setbc_byte(p, (x), 1) -#define setbc_b(p, x) setbc_byte(p, (x), 3) -#define setbc_c(p, x) setbc_byte(p, (x), 2) -#define setbc_d(p, x) \ - ((uint16_t *)(p))[LJ_ENDIAN_SELECT(1, 0)] = (uint16_t)(x) -#define setbc_j(p, x) setbc_d(p, (BCPos)((int32_t)(x)+BCBIAS_J)) - -/* Macros to compose instructions. */ -#define BCINS_ABC(o, a, b, c) \ - (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(b)<<24)|((BCIns)(c)<<16)) -#define BCINS_AD(o, a, d) \ - (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(d)<<16)) -#define BCINS_AJ(o, a, j) BCINS_AD(o, a, (BCPos)((int32_t)(j)+BCBIAS_J)) - -/* Bytecode instruction definition. Order matters, see below. -** -** (name, filler, Amode, Bmode, Cmode or Dmode, metamethod) -** -** The opcode name suffixes specify the type for RB/RC or RD: -** V = variable slot -** S = string const -** N = number const -** P = primitive type (~itype) -** B = unsigned byte literal -** M = multiple args/results -*/ -#define BCDEF(_) \ - /* Comparison ops. ORDER OPR. */ \ - _(ISLT, var, ___, var, lt) \ - _(ISGE, var, ___, var, lt) \ - _(ISLE, var, ___, var, le) \ - _(ISGT, var, ___, var, le) \ - \ - _(ISEQV, var, ___, var, eq) \ - _(ISNEV, var, ___, var, eq) \ - _(ISEQS, var, ___, str, eq) \ - _(ISNES, var, ___, str, eq) \ - _(ISEQN, var, ___, num, eq) \ - _(ISNEN, var, ___, num, eq) \ - _(ISEQP, var, ___, pri, eq) \ - _(ISNEP, var, ___, pri, eq) \ - \ - /* Unary test and copy ops. */ \ - _(ISTC, dst, ___, var, ___) \ - _(ISFC, dst, ___, var, ___) \ - _(IST, ___, ___, var, ___) \ - _(ISF, ___, ___, var, ___) \ - \ - /* Unary ops. */ \ - _(MOV, dst, ___, var, ___) \ - _(NOT, dst, ___, var, ___) \ - _(UNM, dst, ___, var, unm) \ - _(LEN, dst, ___, var, len) \ - \ - /* Binary ops. ORDER OPR. VV last, POW must be next. */ \ - _(ADDVN, dst, var, num, add) \ - _(SUBVN, dst, var, num, sub) \ - _(MULVN, dst, var, num, mul) \ - _(DIVVN, dst, var, num, div) \ - _(MODVN, dst, var, num, mod) \ - \ - _(ADDNV, dst, var, num, add) \ - _(SUBNV, dst, var, num, sub) \ - _(MULNV, dst, var, num, mul) \ - _(DIVNV, dst, var, num, div) \ - _(MODNV, dst, var, num, mod) \ - \ - _(ADDVV, dst, var, var, add) \ - _(SUBVV, dst, var, var, sub) \ - _(MULVV, dst, var, var, mul) \ - _(DIVVV, dst, var, var, div) \ - _(MODVV, dst, var, var, mod) \ - \ - _(POW, dst, var, var, pow) \ - _(CAT, dst, rbase, rbase, concat) \ - \ - /* Constant ops. */ \ - _(KSTR, dst, ___, str, ___) \ - _(KCDATA, dst, ___, cdata, ___) \ - _(KSHORT, dst, ___, lits, ___) \ - _(KNUM, dst, ___, num, ___) \ - _(KPRI, dst, ___, pri, ___) \ - _(KNIL, base, ___, base, ___) \ - \ - /* Upvalue and function ops. */ \ - _(UGET, dst, ___, uv, ___) \ - _(USETV, uv, ___, var, ___) \ - _(USETS, uv, ___, str, ___) \ - _(USETN, uv, ___, num, ___) \ - _(USETP, uv, ___, pri, ___) \ - _(UCLO, rbase, ___, jump, ___) \ - _(FNEW, dst, ___, func, gc) \ - \ - /* Table ops. */ \ - _(TNEW, dst, ___, lit, gc) \ - _(TDUP, dst, ___, tab, gc) \ - _(GGET, dst, ___, str, index) \ - _(GSET, var, ___, str, newindex) \ - _(TGETV, dst, var, var, index) \ - _(TGETS, dst, var, str, index) \ - _(TGETB, dst, var, lit, index) \ - _(TSETV, var, var, var, newindex) \ - _(TSETS, var, var, str, newindex) \ - _(TSETB, var, var, lit, newindex) \ - _(TSETM, base, ___, num, newindex) \ - \ - /* Calls and vararg handling. T = tail call. */ \ - _(CALLM, base, lit, lit, call) \ - _(CALL, base, lit, lit, call) \ - _(CALLMT, base, ___, lit, call) \ - _(CALLT, base, ___, lit, call) \ - _(ITERC, base, lit, lit, call) \ - _(ITERN, base, lit, lit, call) \ - _(VARG, base, lit, lit, ___) \ - _(ISNEXT, base, ___, jump, ___) \ - \ - /* Returns. */ \ - _(RETM, base, ___, lit, ___) \ - _(RET, rbase, ___, lit, ___) \ - _(RET0, rbase, ___, lit, ___) \ - _(RET1, rbase, ___, lit, ___) \ - \ - /* Loops and branches. I/J = interp/JIT, I/C/L = init/call/loop. */ \ - _(FORI, base, ___, jump, ___) \ - _(JFORI, base, ___, jump, ___) \ - \ - _(FORL, base, ___, jump, ___) \ - _(IFORL, base, ___, jump, ___) \ - _(JFORL, base, ___, lit, ___) \ - \ - _(ITERL, base, ___, jump, ___) \ - _(IITERL, base, ___, jump, ___) \ - _(JITERL, base, ___, lit, ___) \ - \ - _(LOOP, rbase, ___, jump, ___) \ - _(ILOOP, rbase, ___, jump, ___) \ - _(JLOOP, rbase, ___, lit, ___) \ - \ - _(JMP, rbase, ___, jump, ___) \ - \ - /* Function headers. I/J = interp/JIT, F/V/C = fixarg/vararg/C func. */ \ - _(FUNCF, rbase, ___, ___, ___) \ - _(IFUNCF, rbase, ___, ___, ___) \ - _(JFUNCF, rbase, ___, lit, ___) \ - _(FUNCV, rbase, ___, ___, ___) \ - _(IFUNCV, rbase, ___, ___, ___) \ - _(JFUNCV, rbase, ___, lit, ___) \ - _(FUNCC, rbase, ___, ___, ___) \ - _(FUNCCW, rbase, ___, ___, ___) - -/* Bytecode opcode numbers. */ -typedef enum { -#define BCENUM(name, ma, mb, mc, mt) BC_##name, -BCDEF(BCENUM) -#undef BCENUM - BC__MAX -} BCOp; - -LJ_STATIC_ASSERT((int)BC_ISEQV+1 == (int)BC_ISNEV); -LJ_STATIC_ASSERT(((int)BC_ISEQV^1) == (int)BC_ISNEV); -LJ_STATIC_ASSERT(((int)BC_ISEQS^1) == (int)BC_ISNES); -LJ_STATIC_ASSERT(((int)BC_ISEQN^1) == (int)BC_ISNEN); -LJ_STATIC_ASSERT(((int)BC_ISEQP^1) == (int)BC_ISNEP); -LJ_STATIC_ASSERT(((int)BC_ISLT^1) == (int)BC_ISGE); -LJ_STATIC_ASSERT(((int)BC_ISLE^1) == (int)BC_ISGT); -LJ_STATIC_ASSERT(((int)BC_ISLT^3) == (int)BC_ISGT); -LJ_STATIC_ASSERT((int)BC_IST-(int)BC_ISTC == (int)BC_ISF-(int)BC_ISFC); -LJ_STATIC_ASSERT((int)BC_CALLT-(int)BC_CALL == (int)BC_CALLMT-(int)BC_CALLM); -LJ_STATIC_ASSERT((int)BC_CALLMT + 1 == (int)BC_CALLT); -LJ_STATIC_ASSERT((int)BC_RETM + 1 == (int)BC_RET); -LJ_STATIC_ASSERT((int)BC_FORL + 1 == (int)BC_IFORL); -LJ_STATIC_ASSERT((int)BC_FORL + 2 == (int)BC_JFORL); -LJ_STATIC_ASSERT((int)BC_ITERL + 1 == (int)BC_IITERL); -LJ_STATIC_ASSERT((int)BC_ITERL + 2 == (int)BC_JITERL); -LJ_STATIC_ASSERT((int)BC_LOOP + 1 == (int)BC_ILOOP); -LJ_STATIC_ASSERT((int)BC_LOOP + 2 == (int)BC_JLOOP); -LJ_STATIC_ASSERT((int)BC_FUNCF + 1 == (int)BC_IFUNCF); -LJ_STATIC_ASSERT((int)BC_FUNCF + 2 == (int)BC_JFUNCF); -LJ_STATIC_ASSERT((int)BC_FUNCV + 1 == (int)BC_IFUNCV); -LJ_STATIC_ASSERT((int)BC_FUNCV + 2 == (int)BC_JFUNCV); - -/* This solves a circular dependency problem, change as needed. */ -#define FF_next_N 4 - -/* Stack slots used by FORI/FORL, relative to operand A. */ -enum { - FORL_IDX, FORL_STOP, FORL_STEP, FORL_EXT -}; - -/* Bytecode operand modes. ORDER BCMode */ -typedef enum { - BCMnone, BCMdst, BCMbase, BCMvar, BCMrbase, BCMuv, /* Mode A must be <= 7 */ - BCMlit, BCMlits, BCMpri, BCMnum, BCMstr, BCMtab, BCMfunc, BCMjump, BCMcdata, - BCM_max -} BCMode; -#define BCM___ BCMnone - -#define bcmode_a(op) ((BCMode)(lj_bc_mode[op] & 7)) -#define bcmode_b(op) ((BCMode)((lj_bc_mode[op]>>3) & 15)) -#define bcmode_c(op) ((BCMode)((lj_bc_mode[op]>>7) & 15)) -#define bcmode_d(op) bcmode_c(op) -#define bcmode_hasd(op) ((lj_bc_mode[op] & (15<<3)) == (BCMnone<<3)) -#define bcmode_mm(op) ((MMS)(lj_bc_mode[op]>>11)) - -#define BCMODE(name, ma, mb, mc, mm) \ - (BCM##ma|(BCM##mb<<3)|(BCM##mc<<7)|(MM_##mm<<11)), -#define BCMODE_FF 0 - -static LJ_AINLINE int bc_isret(BCOp op) -{ - return (op == BC_RETM || op == BC_RET || op == BC_RET0 || op == BC_RET1); -} - -LJ_DATA const uint16_t lj_bc_mode[]; -LJ_DATA const uint16_t lj_bc_ofs[]; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_bcdump.h b/third-party/LuaJIT-2.0.2/src/lj_bcdump.h deleted file mode 100644 index e660156d68..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_bcdump.h +++ /dev/null @@ -1,66 +0,0 @@ -/* -** Bytecode dump definitions. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_BCDUMP_H -#define _LJ_BCDUMP_H - -#include "lj_obj.h" -#include "lj_lex.h" - -/* -- Bytecode dump format ------------------------------------------------ */ - -/* -** dump = header proto+ 0U -** header = ESC 'L' 'J' versionB flagsU [namelenU nameB*] -** proto = lengthU pdata -** pdata = phead bcinsW* uvdataH* kgc* knum* [debugB*] -** phead = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU -** [debuglenU [firstlineU numlineU]] -** kgc = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* } -** knum = intU0 | (loU1 hiU) -** ktab = narrayU nhashU karray* khash* -** karray = ktabk -** khash = ktabk ktabk -** ktabk = ktabtypeU { intU | (loU hiU) | strB* } -** -** B = 8 bit, H = 16 bit, W = 32 bit, U = ULEB128 of W, U0/U1 = ULEB128 of W+1 -*/ - -/* Bytecode dump header. */ -#define BCDUMP_HEAD1 0x1b -#define BCDUMP_HEAD2 0x4c -#define BCDUMP_HEAD3 0x4a - -/* If you perform *any* kind of private modifications to the bytecode itself -** or to the dump format, you *must* set BCDUMP_VERSION to 0x80 or higher. -*/ -#define BCDUMP_VERSION 1 - -/* Compatibility flags. */ -#define BCDUMP_F_BE 0x01 -#define BCDUMP_F_STRIP 0x02 -#define BCDUMP_F_FFI 0x04 - -#define BCDUMP_F_KNOWN (BCDUMP_F_FFI*2-1) - -/* Type codes for the GC constants of a prototype. Plus length for strings. */ -enum { - BCDUMP_KGC_CHILD, BCDUMP_KGC_TAB, BCDUMP_KGC_I64, BCDUMP_KGC_U64, - BCDUMP_KGC_COMPLEX, BCDUMP_KGC_STR -}; - -/* Type codes for the keys/values of a constant table. */ -enum { - BCDUMP_KTAB_NIL, BCDUMP_KTAB_FALSE, BCDUMP_KTAB_TRUE, - BCDUMP_KTAB_INT, BCDUMP_KTAB_NUM, BCDUMP_KTAB_STR -}; - -/* -- Bytecode reader/writer ---------------------------------------------- */ - -LJ_FUNC int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, - void *data, int strip); -LJ_FUNC GCproto *lj_bcread(LexState *ls); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_bcread.c b/third-party/LuaJIT-2.0.2/src/lj_bcread.c deleted file mode 100644 index 2b5ba85533..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_bcread.c +++ /dev/null @@ -1,476 +0,0 @@ -/* -** Bytecode reader. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_bcread_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_bc.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lualib.h" -#endif -#include "lj_lex.h" -#include "lj_bcdump.h" -#include "lj_state.h" - -/* Reuse some lexer fields for our own purposes. */ -#define bcread_flags(ls) ls->level -#define bcread_swap(ls) \ - ((bcread_flags(ls) & BCDUMP_F_BE) != LJ_BE*BCDUMP_F_BE) -#define bcread_oldtop(L, ls) restorestack(L, ls->lastline) -#define bcread_savetop(L, ls, top) \ - ls->lastline = (BCLine)savestack(L, (top)) - -/* -- Input buffer handling ----------------------------------------------- */ - -/* Throw reader error. */ -static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em) -{ - lua_State *L = ls->L; - const char *name = ls->chunkarg; - if (*name == BCDUMP_HEAD1) name = "(binary)"; - else if (*name == '@' || *name == '=') name++; - lj_str_pushf(L, "%s: %s", name, err2msg(em)); - lj_err_throw(L, LUA_ERRSYNTAX); -} - -/* Resize input buffer. */ -static void bcread_resize(LexState *ls, MSize len) -{ - if (ls->sb.sz < len) { - MSize sz = ls->sb.sz * 2; - while (len > sz) sz = sz * 2; - lj_str_resizebuf(ls->L, &ls->sb, sz); - /* Caveat: this may change ls->sb.buf which may affect ls->p. */ - } -} - -/* Refill buffer if needed. */ -static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need) -{ - lua_assert(len != 0); - if (len > LJ_MAX_MEM || ls->current < 0) - bcread_error(ls, LJ_ERR_BCBAD); - do { - const char *buf; - size_t size; - if (ls->n) { /* Copy remainder to buffer. */ - if (ls->sb.n) { /* Move down in buffer. */ - lua_assert(ls->p + ls->n == ls->sb.buf + ls->sb.n); - if (ls->n != ls->sb.n) - memmove(ls->sb.buf, ls->p, ls->n); - } else { /* Copy from buffer provided by reader. */ - bcread_resize(ls, len); - memcpy(ls->sb.buf, ls->p, ls->n); - } - ls->p = ls->sb.buf; - } - ls->sb.n = ls->n; - buf = ls->rfunc(ls->L, ls->rdata, &size); /* Get more data from reader. */ - if (buf == NULL || size == 0) { /* EOF? */ - if (need) bcread_error(ls, LJ_ERR_BCBAD); - ls->current = -1; /* Only bad if we get called again. */ - break; - } - if (ls->sb.n) { /* Append to buffer. */ - MSize n = ls->sb.n + (MSize)size; - bcread_resize(ls, n < len ? len : n); - memcpy(ls->sb.buf + ls->sb.n, buf, size); - ls->n = ls->sb.n = n; - ls->p = ls->sb.buf; - } else { /* Return buffer provided by reader. */ - ls->n = (MSize)size; - ls->p = buf; - } - } while (ls->n < len); -} - -/* Need a certain number of bytes. */ -static LJ_AINLINE void bcread_need(LexState *ls, MSize len) -{ - if (LJ_UNLIKELY(ls->n < len)) - bcread_fill(ls, len, 1); -} - -/* Want to read up to a certain number of bytes, but may need less. */ -static LJ_AINLINE void bcread_want(LexState *ls, MSize len) -{ - if (LJ_UNLIKELY(ls->n < len)) - bcread_fill(ls, len, 0); -} - -#define bcread_dec(ls) check_exp(ls->n > 0, ls->n--) -#define bcread_consume(ls, len) check_exp(ls->n >= (len), ls->n -= (len)) - -/* Return memory block from buffer. */ -static uint8_t *bcread_mem(LexState *ls, MSize len) -{ - uint8_t *p = (uint8_t *)ls->p; - bcread_consume(ls, len); - ls->p = (char *)p + len; - return p; -} - -/* Copy memory block from buffer. */ -static void bcread_block(LexState *ls, void *q, MSize len) -{ - memcpy(q, bcread_mem(ls, len), len); -} - -/* Read byte from buffer. */ -static LJ_AINLINE uint32_t bcread_byte(LexState *ls) -{ - bcread_dec(ls); - return (uint32_t)(uint8_t)*ls->p++; -} - -/* Read ULEB128 value from buffer. */ -static uint32_t bcread_uleb128(LexState *ls) -{ - const uint8_t *p = (const uint8_t *)ls->p; - uint32_t v = *p++; - if (LJ_UNLIKELY(v >= 0x80)) { - int sh = 0; - v &= 0x7f; - do { - v |= ((*p & 0x7f) << (sh += 7)); - bcread_dec(ls); - } while (*p++ >= 0x80); - } - bcread_dec(ls); - ls->p = (char *)p; - return v; -} - -/* Read top 32 bits of 33 bit ULEB128 value from buffer. */ -static uint32_t bcread_uleb128_33(LexState *ls) -{ - const uint8_t *p = (const uint8_t *)ls->p; - uint32_t v = (*p++ >> 1); - if (LJ_UNLIKELY(v >= 0x40)) { - int sh = -1; - v &= 0x3f; - do { - v |= ((*p & 0x7f) << (sh += 7)); - bcread_dec(ls); - } while (*p++ >= 0x80); - } - bcread_dec(ls); - ls->p = (char *)p; - return v; -} - -/* -- Bytecode reader ----------------------------------------------------- */ - -/* Read debug info of a prototype. */ -static void bcread_dbg(LexState *ls, GCproto *pt, MSize sizedbg) -{ - void *lineinfo = (void *)proto_lineinfo(pt); - bcread_block(ls, lineinfo, sizedbg); - /* Swap lineinfo if the endianess differs. */ - if (bcread_swap(ls) && pt->numline >= 256) { - MSize i, n = pt->sizebc-1; - if (pt->numline < 65536) { - uint16_t *p = (uint16_t *)lineinfo; - for (i = 0; i < n; i++) p[i] = (uint16_t)((p[i] >> 8)|(p[i] << 8)); - } else { - uint32_t *p = (uint32_t *)lineinfo; - for (i = 0; i < n; i++) p[i] = lj_bswap(p[i]); - } - } -} - -/* Find pointer to varinfo. */ -static const void *bcread_varinfo(GCproto *pt) -{ - const uint8_t *p = proto_uvinfo(pt); - MSize n = pt->sizeuv; - if (n) while (*p++ || --n) ; - return p; -} - -/* Read a single constant key/value of a template table. */ -static void bcread_ktabk(LexState *ls, TValue *o) -{ - MSize tp = bcread_uleb128(ls); - if (tp >= BCDUMP_KTAB_STR) { - MSize len = tp - BCDUMP_KTAB_STR; - const char *p = (const char *)bcread_mem(ls, len); - setstrV(ls->L, o, lj_str_new(ls->L, p, len)); - } else if (tp == BCDUMP_KTAB_INT) { - setintV(o, (int32_t)bcread_uleb128(ls)); - } else if (tp == BCDUMP_KTAB_NUM) { - o->u32.lo = bcread_uleb128(ls); - o->u32.hi = bcread_uleb128(ls); - } else { - lua_assert(tp <= BCDUMP_KTAB_TRUE); - setitype(o, ~tp); - } -} - -/* Read a template table. */ -static GCtab *bcread_ktab(LexState *ls) -{ - MSize narray = bcread_uleb128(ls); - MSize nhash = bcread_uleb128(ls); - GCtab *t = lj_tab_new(ls->L, narray, hsize2hbits(nhash)); - if (narray) { /* Read array entries. */ - MSize i; - TValue *o = tvref(t->array); - for (i = 0; i < narray; i++, o++) - bcread_ktabk(ls, o); - } - if (nhash) { /* Read hash entries. */ - MSize i; - for (i = 0; i < nhash; i++) { - TValue key; - bcread_ktabk(ls, &key); - lua_assert(!tvisnil(&key)); - bcread_ktabk(ls, lj_tab_set(ls->L, t, &key)); - } - } - return t; -} - -/* Read GC constants of a prototype. */ -static void bcread_kgc(LexState *ls, GCproto *pt, MSize sizekgc) -{ - MSize i; - GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc; - for (i = 0; i < sizekgc; i++, kr++) { - MSize tp = bcread_uleb128(ls); - if (tp >= BCDUMP_KGC_STR) { - MSize len = tp - BCDUMP_KGC_STR; - const char *p = (const char *)bcread_mem(ls, len); - setgcref(*kr, obj2gco(lj_str_new(ls->L, p, len))); - } else if (tp == BCDUMP_KGC_TAB) { - setgcref(*kr, obj2gco(bcread_ktab(ls))); -#if LJ_HASFFI - } else if (tp != BCDUMP_KGC_CHILD) { - CTypeID id = tp == BCDUMP_KGC_COMPLEX ? CTID_COMPLEX_DOUBLE : - tp == BCDUMP_KGC_I64 ? CTID_INT64 : CTID_UINT64; - CTSize sz = tp == BCDUMP_KGC_COMPLEX ? 16 : 8; - GCcdata *cd = lj_cdata_new_(ls->L, id, sz); - TValue *p = (TValue *)cdataptr(cd); - setgcref(*kr, obj2gco(cd)); - p[0].u32.lo = bcread_uleb128(ls); - p[0].u32.hi = bcread_uleb128(ls); - if (tp == BCDUMP_KGC_COMPLEX) { - p[1].u32.lo = bcread_uleb128(ls); - p[1].u32.hi = bcread_uleb128(ls); - } -#endif - } else { - lua_State *L = ls->L; - lua_assert(tp == BCDUMP_KGC_CHILD); - if (L->top <= bcread_oldtop(L, ls)) /* Stack underflow? */ - bcread_error(ls, LJ_ERR_BCBAD); - L->top--; - setgcref(*kr, obj2gco(protoV(L->top))); - } - } -} - -/* Read number constants of a prototype. */ -static void bcread_knum(LexState *ls, GCproto *pt, MSize sizekn) -{ - MSize i; - TValue *o = mref(pt->k, TValue); - for (i = 0; i < sizekn; i++, o++) { - int isnum = (ls->p[0] & 1); - uint32_t lo = bcread_uleb128_33(ls); - if (isnum) { - o->u32.lo = lo; - o->u32.hi = bcread_uleb128(ls); - } else { - setintV(o, lo); - } - } -} - -/* Read bytecode instructions. */ -static void bcread_bytecode(LexState *ls, GCproto *pt, MSize sizebc) -{ - BCIns *bc = proto_bc(pt); - bc[0] = BCINS_AD((pt->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF, - pt->framesize, 0); - bcread_block(ls, bc+1, (sizebc-1)*(MSize)sizeof(BCIns)); - /* Swap bytecode instructions if the endianess differs. */ - if (bcread_swap(ls)) { - MSize i; - for (i = 1; i < sizebc; i++) bc[i] = lj_bswap(bc[i]); - } -} - -/* Read upvalue refs. */ -static void bcread_uv(LexState *ls, GCproto *pt, MSize sizeuv) -{ - if (sizeuv) { - uint16_t *uv = proto_uv(pt); - bcread_block(ls, uv, sizeuv*2); - /* Swap upvalue refs if the endianess differs. */ - if (bcread_swap(ls)) { - MSize i; - for (i = 0; i < sizeuv; i++) - uv[i] = (uint16_t)((uv[i] >> 8)|(uv[i] << 8)); - } - } -} - -/* Read a prototype. */ -static GCproto *bcread_proto(LexState *ls) -{ - GCproto *pt; - MSize framesize, numparams, flags, sizeuv, sizekgc, sizekn, sizebc, sizept; - MSize ofsk, ofsuv, ofsdbg; - MSize sizedbg = 0; - BCLine firstline = 0, numline = 0; - MSize len, startn; - - /* Read length. */ - if (ls->n > 0 && ls->p[0] == 0) { /* Shortcut EOF. */ - ls->n--; ls->p++; - return NULL; - } - bcread_want(ls, 5); - len = bcread_uleb128(ls); - if (!len) return NULL; /* EOF */ - bcread_need(ls, len); - startn = ls->n; - - /* Read prototype header. */ - flags = bcread_byte(ls); - numparams = bcread_byte(ls); - framesize = bcread_byte(ls); - sizeuv = bcread_byte(ls); - sizekgc = bcread_uleb128(ls); - sizekn = bcread_uleb128(ls); - sizebc = bcread_uleb128(ls) + 1; - if (!(bcread_flags(ls) & BCDUMP_F_STRIP)) { - sizedbg = bcread_uleb128(ls); - if (sizedbg) { - firstline = bcread_uleb128(ls); - numline = bcread_uleb128(ls); - } - } - - /* Calculate total size of prototype including all colocated arrays. */ - sizept = (MSize)sizeof(GCproto) + - sizebc*(MSize)sizeof(BCIns) + - sizekgc*(MSize)sizeof(GCRef); - sizept = (sizept + (MSize)sizeof(TValue)-1) & ~((MSize)sizeof(TValue)-1); - ofsk = sizept; sizept += sizekn*(MSize)sizeof(TValue); - ofsuv = sizept; sizept += ((sizeuv+1)&~1)*2; - ofsdbg = sizept; sizept += sizedbg; - - /* Allocate prototype object and initialize its fields. */ - pt = (GCproto *)lj_mem_newgco(ls->L, (MSize)sizept); - pt->gct = ~LJ_TPROTO; - pt->numparams = (uint8_t)numparams; - pt->framesize = (uint8_t)framesize; - pt->sizebc = sizebc; - setmref(pt->k, (char *)pt + ofsk); - setmref(pt->uv, (char *)pt + ofsuv); - pt->sizekgc = 0; /* Set to zero until fully initialized. */ - pt->sizekn = sizekn; - pt->sizept = sizept; - pt->sizeuv = (uint8_t)sizeuv; - pt->flags = (uint8_t)flags; - pt->trace = 0; - setgcref(pt->chunkname, obj2gco(ls->chunkname)); - - /* Close potentially uninitialized gap between bc and kgc. */ - *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(sizekgc+1)) = 0; - - /* Read bytecode instructions and upvalue refs. */ - bcread_bytecode(ls, pt, sizebc); - bcread_uv(ls, pt, sizeuv); - - /* Read constants. */ - bcread_kgc(ls, pt, sizekgc); - pt->sizekgc = sizekgc; - bcread_knum(ls, pt, sizekn); - - /* Read and initialize debug info. */ - pt->firstline = firstline; - pt->numline = numline; - if (sizedbg) { - MSize sizeli = (sizebc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2); - setmref(pt->lineinfo, (char *)pt + ofsdbg); - setmref(pt->uvinfo, (char *)pt + ofsdbg + sizeli); - bcread_dbg(ls, pt, sizedbg); - setmref(pt->varinfo, bcread_varinfo(pt)); - } else { - setmref(pt->lineinfo, NULL); - setmref(pt->uvinfo, NULL); - setmref(pt->varinfo, NULL); - } - - if (len != startn - ls->n) - bcread_error(ls, LJ_ERR_BCBAD); - return pt; -} - -/* Read and check header of bytecode dump. */ -static int bcread_header(LexState *ls) -{ - uint32_t flags; - bcread_want(ls, 3+5+5); - if (bcread_byte(ls) != BCDUMP_HEAD2 || - bcread_byte(ls) != BCDUMP_HEAD3 || - bcread_byte(ls) != BCDUMP_VERSION) return 0; - bcread_flags(ls) = flags = bcread_uleb128(ls); - if ((flags & ~(BCDUMP_F_KNOWN)) != 0) return 0; - if ((flags & BCDUMP_F_FFI)) { -#if LJ_HASFFI - lua_State *L = ls->L; - if (!ctype_ctsG(G(L))) { - ptrdiff_t oldtop = savestack(L, L->top); - luaopen_ffi(L); /* Load FFI library on-demand. */ - L->top = restorestack(L, oldtop); - } -#else - return 0; -#endif - } - if ((flags & BCDUMP_F_STRIP)) { - ls->chunkname = lj_str_newz(ls->L, ls->chunkarg); - } else { - MSize len = bcread_uleb128(ls); - bcread_need(ls, len); - ls->chunkname = lj_str_new(ls->L, (const char *)bcread_mem(ls, len), len); - } - return 1; /* Ok. */ -} - -/* Read a bytecode dump. */ -GCproto *lj_bcread(LexState *ls) -{ - lua_State *L = ls->L; - lua_assert(ls->current == BCDUMP_HEAD1); - bcread_savetop(L, ls, L->top); - lj_str_resetbuf(&ls->sb); - /* Check for a valid bytecode dump header. */ - if (!bcread_header(ls)) - bcread_error(ls, LJ_ERR_BCFMT); - for (;;) { /* Process all prototypes in the bytecode dump. */ - GCproto *pt = bcread_proto(ls); - if (!pt) break; - setprotoV(L, L->top, pt); - incr_top(L); - } - if ((int32_t)ls->n > 0 || L->top-1 != bcread_oldtop(L, ls)) - bcread_error(ls, LJ_ERR_BCBAD); - /* Pop off last prototype. */ - L->top--; - return protoV(L->top); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_bcwrite.c b/third-party/LuaJIT-2.0.2/src/lj_bcwrite.c deleted file mode 100644 index 4805d515f3..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_bcwrite.c +++ /dev/null @@ -1,396 +0,0 @@ -/* -** Bytecode writer. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_bcwrite_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_str.h" -#include "lj_bc.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#if LJ_HASJIT -#include "lj_dispatch.h" -#include "lj_jit.h" -#endif -#include "lj_bcdump.h" -#include "lj_vm.h" - -/* Context for bytecode writer. */ -typedef struct BCWriteCtx { - SBuf sb; /* Output buffer. */ - lua_State *L; /* Lua state. */ - GCproto *pt; /* Root prototype. */ - lua_Writer wfunc; /* Writer callback. */ - void *wdata; /* Writer callback data. */ - int strip; /* Strip debug info. */ - int status; /* Status from writer callback. */ -} BCWriteCtx; - -/* -- Output buffer handling ---------------------------------------------- */ - -/* Resize buffer if needed. */ -static LJ_NOINLINE void bcwrite_resize(BCWriteCtx *ctx, MSize len) -{ - MSize sz = ctx->sb.sz * 2; - while (ctx->sb.n + len > sz) sz = sz * 2; - lj_str_resizebuf(ctx->L, &ctx->sb, sz); -} - -/* Need a certain amount of buffer space. */ -static LJ_AINLINE void bcwrite_need(BCWriteCtx *ctx, MSize len) -{ - if (LJ_UNLIKELY(ctx->sb.n + len > ctx->sb.sz)) - bcwrite_resize(ctx, len); -} - -/* Add memory block to buffer. */ -static void bcwrite_block(BCWriteCtx *ctx, const void *p, MSize len) -{ - uint8_t *q = (uint8_t *)(ctx->sb.buf + ctx->sb.n); - MSize i; - ctx->sb.n += len; - for (i = 0; i < len; i++) q[i] = ((uint8_t *)p)[i]; -} - -/* Add byte to buffer. */ -static LJ_AINLINE void bcwrite_byte(BCWriteCtx *ctx, uint8_t b) -{ - ctx->sb.buf[ctx->sb.n++] = b; -} - -/* Add ULEB128 value to buffer. */ -static void bcwrite_uleb128(BCWriteCtx *ctx, uint32_t v) -{ - MSize n = ctx->sb.n; - uint8_t *p = (uint8_t *)ctx->sb.buf; - for (; v >= 0x80; v >>= 7) - p[n++] = (uint8_t)((v & 0x7f) | 0x80); - p[n++] = (uint8_t)v; - ctx->sb.n = n; -} - -/* -- Bytecode writer ----------------------------------------------------- */ - -/* Write a single constant key/value of a template table. */ -static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow) -{ - bcwrite_need(ctx, 1+10); - if (tvisstr(o)) { - const GCstr *str = strV(o); - MSize len = str->len; - bcwrite_need(ctx, 5+len); - bcwrite_uleb128(ctx, BCDUMP_KTAB_STR+len); - bcwrite_block(ctx, strdata(str), len); - } else if (tvisint(o)) { - bcwrite_byte(ctx, BCDUMP_KTAB_INT); - bcwrite_uleb128(ctx, intV(o)); - } else if (tvisnum(o)) { - if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */ - lua_Number num = numV(o); - int32_t k = lj_num2int(num); - if (num == (lua_Number)k) { /* -0 is never a constant. */ - bcwrite_byte(ctx, BCDUMP_KTAB_INT); - bcwrite_uleb128(ctx, k); - return; - } - } - bcwrite_byte(ctx, BCDUMP_KTAB_NUM); - bcwrite_uleb128(ctx, o->u32.lo); - bcwrite_uleb128(ctx, o->u32.hi); - } else { - lua_assert(tvispri(o)); - bcwrite_byte(ctx, BCDUMP_KTAB_NIL+~itype(o)); - } -} - -/* Write a template table. */ -static void bcwrite_ktab(BCWriteCtx *ctx, const GCtab *t) -{ - MSize narray = 0, nhash = 0; - if (t->asize > 0) { /* Determine max. length of array part. */ - ptrdiff_t i; - TValue *array = tvref(t->array); - for (i = (ptrdiff_t)t->asize-1; i >= 0; i--) - if (!tvisnil(&array[i])) - break; - narray = (MSize)(i+1); - } - if (t->hmask > 0) { /* Count number of used hash slots. */ - MSize i, hmask = t->hmask; - Node *node = noderef(t->node); - for (i = 0; i <= hmask; i++) - nhash += !tvisnil(&node[i].val); - } - /* Write number of array slots and hash slots. */ - bcwrite_uleb128(ctx, narray); - bcwrite_uleb128(ctx, nhash); - if (narray) { /* Write array entries (may contain nil). */ - MSize i; - TValue *o = tvref(t->array); - for (i = 0; i < narray; i++, o++) - bcwrite_ktabk(ctx, o, 1); - } - if (nhash) { /* Write hash entries. */ - MSize i = nhash; - Node *node = noderef(t->node) + t->hmask; - for (;; node--) - if (!tvisnil(&node->val)) { - bcwrite_ktabk(ctx, &node->key, 0); - bcwrite_ktabk(ctx, &node->val, 1); - if (--i == 0) break; - } - } -} - -/* Write GC constants of a prototype. */ -static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt) -{ - MSize i, sizekgc = pt->sizekgc; - GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc; - for (i = 0; i < sizekgc; i++, kr++) { - GCobj *o = gcref(*kr); - MSize tp, need = 1; - /* Determine constant type and needed size. */ - if (o->gch.gct == ~LJ_TSTR) { - tp = BCDUMP_KGC_STR + gco2str(o)->len; - need = 5+gco2str(o)->len; - } else if (o->gch.gct == ~LJ_TPROTO) { - lua_assert((pt->flags & PROTO_CHILD)); - tp = BCDUMP_KGC_CHILD; -#if LJ_HASFFI - } else if (o->gch.gct == ~LJ_TCDATA) { - CTypeID id = gco2cd(o)->ctypeid; - need = 1+4*5; - if (id == CTID_INT64) { - tp = BCDUMP_KGC_I64; - } else if (id == CTID_UINT64) { - tp = BCDUMP_KGC_U64; - } else { - lua_assert(id == CTID_COMPLEX_DOUBLE); - tp = BCDUMP_KGC_COMPLEX; - } -#endif - } else { - lua_assert(o->gch.gct == ~LJ_TTAB); - tp = BCDUMP_KGC_TAB; - need = 1+2*5; - } - /* Write constant type. */ - bcwrite_need(ctx, need); - bcwrite_uleb128(ctx, tp); - /* Write constant data (if any). */ - if (tp >= BCDUMP_KGC_STR) { - bcwrite_block(ctx, strdata(gco2str(o)), gco2str(o)->len); - } else if (tp == BCDUMP_KGC_TAB) { - bcwrite_ktab(ctx, gco2tab(o)); -#if LJ_HASFFI - } else if (tp != BCDUMP_KGC_CHILD) { - cTValue *p = (TValue *)cdataptr(gco2cd(o)); - bcwrite_uleb128(ctx, p[0].u32.lo); - bcwrite_uleb128(ctx, p[0].u32.hi); - if (tp == BCDUMP_KGC_COMPLEX) { - bcwrite_uleb128(ctx, p[1].u32.lo); - bcwrite_uleb128(ctx, p[1].u32.hi); - } -#endif - } - } -} - -/* Write number constants of a prototype. */ -static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt) -{ - MSize i, sizekn = pt->sizekn; - cTValue *o = mref(pt->k, TValue); - bcwrite_need(ctx, 10*sizekn); - for (i = 0; i < sizekn; i++, o++) { - int32_t k; - if (tvisint(o)) { - k = intV(o); - goto save_int; - } else { - /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */ - if (!LJ_DUALNUM) { /* Narrow number constants to integers. */ - lua_Number num = numV(o); - k = lj_num2int(num); - if (num == (lua_Number)k) { /* -0 is never a constant. */ - save_int: - bcwrite_uleb128(ctx, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u)); - if (k < 0) { - char *p = &ctx->sb.buf[ctx->sb.n-1]; - *p = (*p & 7) | ((k>>27) & 0x18); - } - continue; - } - } - bcwrite_uleb128(ctx, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u))); - if (o->u32.lo >= 0x80000000u) { - char *p = &ctx->sb.buf[ctx->sb.n-1]; - *p = (*p & 7) | ((o->u32.lo>>27) & 0x18); - } - bcwrite_uleb128(ctx, o->u32.hi); - } - } -} - -/* Write bytecode instructions. */ -static void bcwrite_bytecode(BCWriteCtx *ctx, GCproto *pt) -{ - MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */ -#if LJ_HASJIT - uint8_t *p = (uint8_t *)&ctx->sb.buf[ctx->sb.n]; -#endif - bcwrite_block(ctx, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns)); -#if LJ_HASJIT - /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */ - if ((pt->flags & PROTO_ILOOP) || pt->trace) { - jit_State *J = L2J(ctx->L); - MSize i; - for (i = 0; i < nbc; i++, p += sizeof(BCIns)) { - BCOp op = (BCOp)p[LJ_ENDIAN_SELECT(0, 3)]; - if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP || - op == BC_JFORI) { - p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL); - } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { - BCReg rd = p[LJ_ENDIAN_SELECT(2, 1)] + (p[LJ_ENDIAN_SELECT(3, 0)] << 8); - BCIns ins = traceref(J, rd)->startins; - p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL); - p[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins); - p[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins); - } - } - } -#endif -} - -/* Write prototype. */ -static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) -{ - MSize sizedbg = 0; - - /* Recursively write children of prototype. */ - if ((pt->flags & PROTO_CHILD)) { - ptrdiff_t i, n = pt->sizekgc; - GCRef *kr = mref(pt->k, GCRef) - 1; - for (i = 0; i < n; i++, kr--) { - GCobj *o = gcref(*kr); - if (o->gch.gct == ~LJ_TPROTO) - bcwrite_proto(ctx, gco2pt(o)); - } - } - - /* Start writing the prototype info to a buffer. */ - lj_str_resetbuf(&ctx->sb); - ctx->sb.n = 5; /* Leave room for final size. */ - bcwrite_need(ctx, 4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2); - - /* Write prototype header. */ - bcwrite_byte(ctx, (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI))); - bcwrite_byte(ctx, pt->numparams); - bcwrite_byte(ctx, pt->framesize); - bcwrite_byte(ctx, pt->sizeuv); - bcwrite_uleb128(ctx, pt->sizekgc); - bcwrite_uleb128(ctx, pt->sizekn); - bcwrite_uleb128(ctx, pt->sizebc-1); - if (!ctx->strip) { - if (proto_lineinfo(pt)) - sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt); - bcwrite_uleb128(ctx, sizedbg); - if (sizedbg) { - bcwrite_uleb128(ctx, pt->firstline); - bcwrite_uleb128(ctx, pt->numline); - } - } - - /* Write bytecode instructions and upvalue refs. */ - bcwrite_bytecode(ctx, pt); - bcwrite_block(ctx, proto_uv(pt), pt->sizeuv*2); - - /* Write constants. */ - bcwrite_kgc(ctx, pt); - bcwrite_knum(ctx, pt); - - /* Write debug info, if not stripped. */ - if (sizedbg) { - bcwrite_need(ctx, sizedbg); - bcwrite_block(ctx, proto_lineinfo(pt), sizedbg); - } - - /* Pass buffer to writer function. */ - if (ctx->status == 0) { - MSize n = ctx->sb.n - 5; - MSize nn = (lj_fls(n)+8)*9 >> 6; - ctx->sb.n = 5 - nn; - bcwrite_uleb128(ctx, n); /* Fill in final size. */ - lua_assert(ctx->sb.n == 5); - ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf+5-nn, nn+n, ctx->wdata); - } -} - -/* Write header of bytecode dump. */ -static void bcwrite_header(BCWriteCtx *ctx) -{ - GCstr *chunkname = proto_chunkname(ctx->pt); - const char *name = strdata(chunkname); - MSize len = chunkname->len; - lj_str_resetbuf(&ctx->sb); - bcwrite_need(ctx, 5+5+len); - bcwrite_byte(ctx, BCDUMP_HEAD1); - bcwrite_byte(ctx, BCDUMP_HEAD2); - bcwrite_byte(ctx, BCDUMP_HEAD3); - bcwrite_byte(ctx, BCDUMP_VERSION); - bcwrite_byte(ctx, (ctx->strip ? BCDUMP_F_STRIP : 0) + - (LJ_BE ? BCDUMP_F_BE : 0) + - ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0)); - if (!ctx->strip) { - bcwrite_uleb128(ctx, len); - bcwrite_block(ctx, name, len); - } - ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf, ctx->sb.n, ctx->wdata); -} - -/* Write footer of bytecode dump. */ -static void bcwrite_footer(BCWriteCtx *ctx) -{ - if (ctx->status == 0) { - uint8_t zero = 0; - ctx->status = ctx->wfunc(ctx->L, &zero, 1, ctx->wdata); - } -} - -/* Protected callback for bytecode writer. */ -static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud) -{ - BCWriteCtx *ctx = (BCWriteCtx *)ud; - UNUSED(dummy); - lj_str_resizebuf(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */ - bcwrite_header(ctx); - bcwrite_proto(ctx, ctx->pt); - bcwrite_footer(ctx); - return NULL; -} - -/* Write bytecode for a prototype. */ -int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data, - int strip) -{ - BCWriteCtx ctx; - int status; - ctx.L = L; - ctx.pt = pt; - ctx.wfunc = writer; - ctx.wdata = data; - ctx.strip = strip; - ctx.status = 0; - lj_str_initbuf(&ctx.sb); - status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); - if (status == 0) status = ctx.status; - lj_str_freebuf(G(ctx.L), &ctx.sb); - return status; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_carith.c b/third-party/LuaJIT-2.0.2/src/lj_carith.c deleted file mode 100644 index afe7e6821d..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_carith.c +++ /dev/null @@ -1,351 +0,0 @@ -/* -** C data arithmetic. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" -#include "lj_carith.h" - -/* -- C data arithmetic --------------------------------------------------- */ - -/* Binary operands of an operator converted to ctypes. */ -typedef struct CDArith { - uint8_t *p[2]; - CType *ct[2]; -} CDArith; - -/* Check arguments for arithmetic metamethods. */ -static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca) -{ - TValue *o = L->base; - int ok = 1; - MSize i; - if (o+1 >= L->top) - lj_err_argt(L, 1, LUA_TCDATA); - for (i = 0; i < 2; i++, o++) { - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - CTypeID id = (CTypeID)cd->ctypeid; - CType *ct = ctype_raw(cts, id); - uint8_t *p = (uint8_t *)cdataptr(cd); - if (ctype_isptr(ct->info)) { - p = (uint8_t *)cdata_getptr(p, ct->size); - if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct); - } else if (ctype_isfunc(ct->info)) { - p = (uint8_t *)*(void **)p; - ct = ctype_get(cts, - lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR)); - } - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - ca->ct[i] = ct; - ca->p[i] = p; - } else if (tvisint(o)) { - ca->ct[i] = ctype_get(cts, CTID_INT32); - ca->p[i] = (uint8_t *)&o->i; - } else if (tvisnum(o)) { - ca->ct[i] = ctype_get(cts, CTID_DOUBLE); - ca->p[i] = (uint8_t *)&o->n; - } else if (tvisnil(o)) { - ca->ct[i] = ctype_get(cts, CTID_P_VOID); - ca->p[i] = (uint8_t *)0; - } else if (tvisstr(o)) { - TValue *o2 = i == 0 ? o+1 : o-1; - CType *ct = ctype_raw(cts, cdataV(o2)->ctypeid); - ca->ct[i] = NULL; - ca->p[i] = NULL; - ok = 0; - if (ctype_isenum(ct->info)) { - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, ct, strV(o), &ofs); - if (cct && ctype_isconstval(cct->info)) { - ca->ct[i] = ctype_child(cts, cct); - ca->p[i] = (uint8_t *)&cct->size; /* Assumes ct does not grow. */ - ok = 1; - } else { - ca->ct[1-i] = ct; /* Use enum to improve error message. */ - ca->p[1-i] = NULL; - break; - } - } - } else { - ca->ct[i] = NULL; - ca->p[i] = NULL; - ok = 0; - } - } - return ok; -} - -/* Pointer arithmetic. */ -static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm) -{ - CType *ctp = ca->ct[0]; - uint8_t *pp = ca->p[0]; - ptrdiff_t idx; - CTSize sz; - CTypeID id; - GCcdata *cd; - if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) { - if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) && - (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) { - uint8_t *pp2 = ca->p[1]; - if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */ - setboolV(L->top-1, (pp == pp2)); - return 1; - } - if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL)) - return 0; - if (mm == MM_sub) { /* Pointer difference. */ - intptr_t diff; - sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ - if (sz == 0 || sz == CTSIZE_INVALID) - return 0; - diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz; - /* All valid pointer differences on x64 are in (-2^47, +2^47), - ** which fits into a double without loss of precision. - */ - setintptrV(L->top-1, (int32_t)diff); - return 1; - } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */ - setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2)); - return 1; - } else { - lua_assert(mm == MM_le); - setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2)); - return 1; - } - } - if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info))) - return 0; - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1], - (uint8_t *)&idx, ca->p[1], 0); - if (mm == MM_sub) idx = -idx; - } else if (mm == MM_add && ctype_isnum(ctp->info) && - (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) { - /* Swap pointer and index. */ - ctp = ca->ct[1]; pp = ca->p[1]; - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0], - (uint8_t *)&idx, ca->p[0], 0); - } else { - return 0; - } - sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ - if (sz == CTSIZE_INVALID) - return 0; - pp += idx*(int32_t)sz; /* Compute pointer + index. */ - id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), - CTSIZE_PTR); - cd = lj_cdata_new(cts, id, CTSIZE_PTR); - *(uint8_t **)cdataptr(cd) = pp; - setcdataV(L, L->top-1, cd); - lj_gc_check(L); - return 1; -} - -/* 64 bit integer arithmetic. */ -static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm) -{ - if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 && - ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) { - CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) || - ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ? - CTID_UINT64 : CTID_INT64; - CType *ct = ctype_get(cts, id); - GCcdata *cd; - uint64_t u0, u1, *up; - lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0); - if (mm != MM_unm) - lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0); - switch (mm) { - case MM_eq: - setboolV(L->top-1, (u0 == u1)); - return 1; - case MM_lt: - setboolV(L->top-1, - id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1)); - return 1; - case MM_le: - setboolV(L->top-1, - id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1)); - return 1; - default: break; - } - cd = lj_cdata_new(cts, id, 8); - up = (uint64_t *)cdataptr(cd); - setcdataV(L, L->top-1, cd); - switch (mm) { - case MM_add: *up = u0 + u1; break; - case MM_sub: *up = u0 - u1; break; - case MM_mul: *up = u0 * u1; break; - case MM_div: - if (id == CTID_INT64) - *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1); - else - *up = lj_carith_divu64(u0, u1); - break; - case MM_mod: - if (id == CTID_INT64) - *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1); - else - *up = lj_carith_modu64(u0, u1); - break; - case MM_pow: - if (id == CTID_INT64) - *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1); - else - *up = lj_carith_powu64(u0, u1); - break; - case MM_unm: *up = (uint64_t)-(int64_t)u0; break; - default: lua_assert(0); break; - } - lj_gc_check(L); - return 1; - } - return 0; -} - -/* Handle ctype arithmetic metamethods. */ -static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm) -{ - cTValue *tv = NULL; - if (tviscdata(L->base)) { - CTypeID id = cdataV(L->base)->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - } - if (!tv && L->base+1 < L->top && tviscdata(L->base+1)) { - CTypeID id = cdataV(L->base+1)->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - } - if (!tv) { - const char *repr[2]; - int i, isenum = -1, isstr = -1; - if (mm == MM_eq) { /* Equality checks never raise an error. */ - setboolV(L->top-1, 0); - return 1; - } - for (i = 0; i < 2; i++) { - if (ca->ct[i] && tviscdata(L->base+i)) { - if (ctype_isenum(ca->ct[i]->info)) isenum = i; - repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL)); - } else { - if (tvisstr(&L->base[i])) isstr = i; - repr[i] = lj_typename(&L->base[i]); - } - } - if ((isenum ^ isstr) == 1) - lj_err_callerv(L, LJ_ERR_FFI_BADCONV, repr[isstr], repr[isenum]); - lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN : - mm == MM_concat ? LJ_ERR_FFI_BADCONCAT : - mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH, - repr[0], repr[1]); - } - return lj_meta_tailcall(L, tv); -} - -/* Arithmetic operators for cdata. */ -int lj_carith_op(lua_State *L, MMS mm) -{ - CTState *cts = ctype_cts(L); - CDArith ca; - if (carith_checkarg(L, cts, &ca)) { - if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) { - copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */ - return 1; - } - } - return lj_carith_meta(L, cts, &ca, mm); -} - -/* -- 64 bit integer arithmetic helpers ----------------------------------- */ - -#if LJ_32 && LJ_HASJIT -/* Signed/unsigned 64 bit multiplication. */ -int64_t lj_carith_mul64(int64_t a, int64_t b) -{ - return a * b; -} -#endif - -/* Unsigned 64 bit division. */ -uint64_t lj_carith_divu64(uint64_t a, uint64_t b) -{ - if (b == 0) return U64x(80000000,00000000); - return a / b; -} - -/* Signed 64 bit division. */ -int64_t lj_carith_divi64(int64_t a, int64_t b) -{ - if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1)) - return U64x(80000000,00000000); - return a / b; -} - -/* Unsigned 64 bit modulo. */ -uint64_t lj_carith_modu64(uint64_t a, uint64_t b) -{ - if (b == 0) return U64x(80000000,00000000); - return a % b; -} - -/* Signed 64 bit modulo. */ -int64_t lj_carith_modi64(int64_t a, int64_t b) -{ - if (b == 0) return U64x(80000000,00000000); - if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0; - return a % b; -} - -/* Unsigned 64 bit x^k. */ -uint64_t lj_carith_powu64(uint64_t x, uint64_t k) -{ - uint64_t y; - if (k == 0) - return 1; - for (; (k & 1) == 0; k >>= 1) x *= x; - y = x; - if ((k >>= 1) != 0) { - for (;;) { - x *= x; - if (k == 1) break; - if (k & 1) y *= x; - k >>= 1; - } - y *= x; - } - return y; -} - -/* Signed 64 bit x^k. */ -int64_t lj_carith_powi64(int64_t x, int64_t k) -{ - if (k == 0) - return 1; - if (k < 0) { - if (x == 0) - return U64x(7fffffff,ffffffff); - else if (x == 1) - return 1; - else if (x == -1) - return (k & 1) ? -1 : 1; - else - return 0; - } - return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k); -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_carith.h b/third-party/LuaJIT-2.0.2/src/lj_carith.h deleted file mode 100644 index ae17df0068..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_carith.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -** C data arithmetic. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CARITH_H -#define _LJ_CARITH_H - -#include "lj_obj.h" - -#if LJ_HASFFI - -LJ_FUNC int lj_carith_op(lua_State *L, MMS mm); - -#if LJ_32 && LJ_HASJIT -LJ_FUNC int64_t lj_carith_mul64(int64_t x, int64_t k); -#endif -LJ_FUNC uint64_t lj_carith_divu64(uint64_t a, uint64_t b); -LJ_FUNC int64_t lj_carith_divi64(int64_t a, int64_t b); -LJ_FUNC uint64_t lj_carith_modu64(uint64_t a, uint64_t b); -LJ_FUNC int64_t lj_carith_modi64(int64_t a, int64_t b); -LJ_FUNC uint64_t lj_carith_powu64(uint64_t x, uint64_t k); -LJ_FUNC int64_t lj_carith_powi64(int64_t x, int64_t k); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ccall.c b/third-party/LuaJIT-2.0.2/src/lj_ccall.c deleted file mode 100644 index eb73604f6e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ccall.c +++ /dev/null @@ -1,899 +0,0 @@ -/* -** FFI C call handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" -#include "lj_ccall.h" -#include "lj_trace.h" - -/* Target-specific handling of register arguments. */ -#if LJ_TARGET_X86 -/* -- x86 calling conventions --------------------------------------------- */ - -#if LJ_ABI_WIN - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs bigger than 8 by reference (on stack only). */ \ - cc->retref = (sz > 8); \ - if (cc->retref) cc->stack[nsp++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET - -#else - -#if LJ_TARGET_OSX - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs of size 1, 2, 4 or 8 in registers. */ \ - cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \ - if (cc->retref) { \ - if (ngpr < maxgpr) \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - else \ - cc->stack[nsp++] = (GPRArg)dp; \ - } else { /* Struct with single FP field ends up in FPR. */ \ - cc->resx87 = ccall_classify_struct(cts, ctr); \ - } - -#define CCALL_HANDLE_STRUCTRET2 \ - if (cc->resx87) sp = (uint8_t *)&cc->fpr[0]; \ - memcpy(dp, sp, ctr->size); - -#else - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = 1; /* Return all structs by reference (in reg or on stack). */ \ - if (ngpr < maxgpr) \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - else \ - cc->stack[nsp++] = (GPRArg)dp; - -#endif - -#define CCALL_HANDLE_COMPLEXRET \ - /* Return complex float in GPRs and complex double by reference. */ \ - cc->retref = (sz > 8); \ - if (cc->retref) { \ - if (ngpr < maxgpr) \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - else \ - cc->stack[nsp++] = (GPRArg)dp; \ - } - -#endif - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (!cc->retref) \ - *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */ - -#define CCALL_HANDLE_STRUCTARG \ - ngpr = maxgpr; /* Pass all structs by value on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - isfp = 1; /* Pass complex by value on stack. */ - -#define CCALL_HANDLE_REGARG \ - if (!isfp) { /* Only non-FP values may be passed in registers. */ \ - if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \ - if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \ - } else if (ngpr + 1 <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_X64 && LJ_ABI_WIN -/* -- Windows/x64 calling conventions ------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs of size 1, 2, 4 or 8 in a GPR. */ \ - cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \ - if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (!cc->retref) \ - *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */ - -#define CCALL_HANDLE_STRUCTARG \ - /* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */ \ - if (!(sz == 1 || sz == 2 || sz == 4 || sz == 8)) { \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; /* Pass all other structs by reference. */ \ - } - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex float in a GPR and complex double by reference. */ \ - if (sz != 2*sizeof(float)) { \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; \ - } - -/* Windows/x64 argument registers are strictly positional (use ngpr). */ -#define CCALL_HANDLE_REGARG \ - if (isfp) { \ - if (ngpr < maxgpr) { dp = &cc->fpr[ngpr++]; nfpr = ngpr; goto done; } \ - } else { \ - if (ngpr < maxgpr) { dp = &cc->gpr[ngpr++]; goto done; } \ - } - -#elif LJ_TARGET_X64 -/* -- POSIX/x64 calling conventions --------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - int rcl[2]; rcl[0] = rcl[1] = 0; \ - if (ccall_classify_struct(cts, ctr, rcl, 0)) { \ - cc->retref = 1; /* Return struct by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - } else { \ - cc->retref = 0; /* Return small structs in registers. */ \ - } - -#define CCALL_HANDLE_STRUCTRET2 \ - int rcl[2]; rcl[0] = rcl[1] = 0; \ - ccall_classify_struct(cts, ctr, rcl, 0); \ - ccall_struct_ret(cc, rcl, dp, ctr->size); - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in one or two FPRs. */ \ - cc->retref = 0; - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPR. */ \ - *(int64_t *)dp = cc->fpr[0].l[0]; \ - } else { /* Copy non-contiguous complex double from FPRs. */ \ - ((int64_t *)dp)[0] = cc->fpr[0].l[0]; \ - ((int64_t *)dp)[1] = cc->fpr[1].l[0]; \ - } - -#define CCALL_HANDLE_STRUCTARG \ - int rcl[2]; rcl[0] = rcl[1] = 0; \ - if (!ccall_classify_struct(cts, d, rcl, 0)) { \ - cc->nsp = nsp; cc->ngpr = ngpr; cc->nfpr = nfpr; \ - if (ccall_struct_arg(cc, cts, d, rcl, o, narg)) goto err_nyi; \ - nsp = cc->nsp; ngpr = cc->ngpr; nfpr = cc->nfpr; \ - continue; \ - } /* Pass all other structs by value on stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - isfp = 2; /* Pass complex in FPRs or on stack. Needs postprocessing. */ - -#define CCALL_HANDLE_REGARG \ - if (isfp) { /* Try to pass argument in FPRs. */ \ - if (nfpr + n <= CCALL_NARG_FPR) { \ - dp = &cc->fpr[nfpr]; \ - nfpr += n; \ - goto done; \ - } \ - } else { /* Try to pass argument in GPRs. */ \ - /* Note that reordering is explicitly allowed in the x64 ABI. */ \ - if (n <= 2 && ngpr + n <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_ARM -/* -- ARM calling conventions --------------------------------------------- */ - -#if LJ_ABI_SOFTFP - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs of size <= 4 in a GPR. */ \ - cc->retref = !(sz <= 4); \ - if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET \ - cc->retref = 1; /* Return all complex values by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET2 \ - UNUSED(dp); /* Nothing to do. */ - -#define CCALL_HANDLE_STRUCTARG \ - /* Pass all structs by value in registers and/or on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -#define CCALL_HANDLE_REGARG_FP1 -#define CCALL_HANDLE_REGARG_FP2 - -#else - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = !ccall_classify_struct(cts, ctr, ct); \ - if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_STRUCTRET2 \ - if (ccall_classify_struct(cts, ctr, ct) > 1) sp = (uint8_t *)&cc->fpr[0]; \ - memcpy(dp, sp, ctr->size); - -#define CCALL_HANDLE_COMPLEXRET \ - if (!(ct->info & CTF_VARARG)) cc->retref = 0; /* Return complex in FPRs. */ - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (!(ct->info & CTF_VARARG)) memcpy(dp, &cc->fpr[0], ctr->size); - -#define CCALL_HANDLE_STRUCTARG \ - isfp = (ccall_classify_struct(cts, d, ct) > 1); - /* Pass all structs by value in registers and/or on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - isfp = 1; /* Pass complex by value in FPRs or on stack. */ - -#define CCALL_HANDLE_REGARG_FP1 \ - if (isfp && !(ct->info & CTF_VARARG)) { \ - if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \ - if (nfpr + (n >> 1) <= CCALL_NARG_FPR) { \ - dp = &cc->fpr[nfpr]; \ - nfpr += (n >> 1); \ - goto done; \ - } \ - } else { \ - if (sz > 1 && fprodd != nfpr) fprodd = 0; \ - if (fprodd) { \ - if (2*nfpr+n <= 2*CCALL_NARG_FPR+1) { \ - dp = (void *)&cc->fpr[fprodd-1].f[1]; \ - nfpr += (n >> 1); \ - if ((n & 1)) fprodd = 0; else fprodd = nfpr-1; \ - goto done; \ - } \ - } else { \ - if (2*nfpr+n <= 2*CCALL_NARG_FPR) { \ - dp = (void *)&cc->fpr[nfpr]; \ - nfpr += (n >> 1); \ - if ((n & 1)) fprodd = ++nfpr; else fprodd = 0; \ - goto done; \ - } \ - } \ - } \ - fprodd = 0; /* No reordering after the first FP value is on stack. */ \ - } else { - -#define CCALL_HANDLE_REGARG_FP2 } - -#endif - -#define CCALL_HANDLE_REGARG \ - CCALL_HANDLE_REGARG_FP1 \ - if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \ - if (ngpr < maxgpr) \ - ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - } \ - if (ngpr < maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - if (ngpr + n > maxgpr) { \ - nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \ - if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \ - ngpr = maxgpr; \ - } else { \ - ngpr += n; \ - } \ - goto done; \ - } CCALL_HANDLE_REGARG_FP2 - -#define CCALL_HANDLE_RET \ - if ((ct->info & CTF_VARARG)) sp = (uint8_t *)&cc->gpr[0]; - -#elif LJ_TARGET_PPC -/* -- PPC calling conventions --------------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = 1; /* Return all structs by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in 2 or 4 GPRs. */ \ - cc->retref = 0; - -#define CCALL_HANDLE_COMPLEXRET2 \ - memcpy(dp, sp, ctr->size); /* Copy complex from GPRs. */ - -#define CCALL_HANDLE_STRUCTARG \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; /* Pass all structs by reference. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -#define CCALL_HANDLE_REGARG \ - if (isfp) { /* Try to pass argument in FPRs. */ \ - if (nfpr + 1 <= CCALL_NARG_FPR) { \ - dp = &cc->fpr[nfpr]; \ - nfpr += 1; \ - d = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \ - goto done; \ - } \ - } else { /* Try to pass argument in GPRs. */ \ - if (n > 1) { \ - lua_assert(n == 2 || n == 4); /* int64_t or complex (float). */ \ - if (ctype_isinteger(d->info)) \ - ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ - else if (ngpr + n > maxgpr) \ - ngpr = maxgpr; /* Prevent reordering. */ \ - } \ - if (ngpr + n <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#define CCALL_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - ctr = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ - -#elif LJ_TARGET_PPCSPE -/* -- PPC/SPE calling conventions ----------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = 1; /* Return all structs by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in 2 or 4 GPRs. */ \ - cc->retref = 0; - -#define CCALL_HANDLE_COMPLEXRET2 \ - memcpy(dp, sp, ctr->size); /* Copy complex from GPRs. */ - -#define CCALL_HANDLE_STRUCTARG \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; /* Pass all structs by reference. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -/* PPC/SPE has a softfp ABI. */ -#define CCALL_HANDLE_REGARG \ - if (n > 1) { /* Doesn't fit in a single GPR? */ \ - lua_assert(n == 2 || n == 4); /* int64_t, double or complex (float). */ \ - if (n == 2) \ - ngpr = (ngpr + 1u) & ~1u; /* Only align 64 bit value to regpair. */ \ - else if (ngpr + n > maxgpr) \ - ngpr = maxgpr; /* Prevent reordering. */ \ - } \ - if (ngpr + n <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } - -#elif LJ_TARGET_MIPS -/* -- MIPS calling conventions -------------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = 1; /* Return all structs by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in 1 or 2 FPRs. */ \ - cc->retref = 0; - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPRs. */ \ - ((float *)dp)[0] = cc->fpr[0].f; \ - ((float *)dp)[1] = cc->fpr[1].f; \ - } else { /* Copy complex double from FPRs. */ \ - ((double *)dp)[0] = cc->fpr[0].d; \ - ((double *)dp)[1] = cc->fpr[1].d; \ - } - -#define CCALL_HANDLE_STRUCTARG \ - /* Pass all structs by value in registers and/or on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -#define CCALL_HANDLE_REGARG \ - if (isfp && nfpr < CCALL_NARG_FPR && !(ct->info & CTF_VARARG)) { \ - /* Try to pass argument in FPRs. */ \ - dp = n == 1 ? (void *)&cc->fpr[nfpr].f : (void *)&cc->fpr[nfpr].d; \ - nfpr++; ngpr += n; \ - goto done; \ - } else { /* Try to pass argument in GPRs. */ \ - nfpr = CCALL_NARG_FPR; \ - if ((d->info & CTF_ALIGN) > CTALIGN_PTR) \ - ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr < maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - if (ngpr + n > maxgpr) { \ - nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \ - if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \ - ngpr = maxgpr; \ - } else { \ - ngpr += n; \ - } \ - goto done; \ - } \ - } - -#define CCALL_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - sp = (uint8_t *)&cc->fpr[0].f; - -#else -#error "Missing calling convention definitions for this architecture" -#endif - -#ifndef CCALL_HANDLE_STRUCTRET2 -#define CCALL_HANDLE_STRUCTRET2 \ - memcpy(dp, sp, ctr->size); /* Copy struct return value from GPRs. */ -#endif - -/* -- x86 OSX ABI struct classification ----------------------------------- */ - -#if LJ_TARGET_X86 && LJ_TARGET_OSX - -/* Check for struct with single FP field. */ -static int ccall_classify_struct(CTState *cts, CType *ct) -{ - CTSize sz = ct->size; - if (!(sz == sizeof(float) || sz == sizeof(double))) return 0; - if ((ct->info & CTF_UNION)) return 0; - while (ct->sib) { - ct = ctype_get(cts, ct->sib); - if (ctype_isfield(ct->info)) { - CType *sct = ctype_rawchild(cts, ct); - if (ctype_isfp(sct->info)) { - if (sct->size == sz) - return (sz >> 2); /* Return 1 for float or 2 for double. */ - } else if (ctype_isstruct(sct->info)) { - if (sct->size) - return ccall_classify_struct(cts, sct); - } else { - break; - } - } else if (ctype_isbitfield(ct->info)) { - break; - } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - CType *sct = ctype_rawchild(cts, ct); - if (sct->size) - return ccall_classify_struct(cts, sct); - } - } - return 0; -} - -#endif - -/* -- x64 struct classification ------------------------------------------- */ - -#if LJ_TARGET_X64 && !LJ_ABI_WIN - -/* Register classes for x64 struct classification. */ -#define CCALL_RCL_INT 1 -#define CCALL_RCL_SSE 2 -#define CCALL_RCL_MEM 4 -/* NYI: classify vectors. */ - -static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs); - -/* Classify a C type. */ -static void ccall_classify_ct(CTState *cts, CType *ct, int *rcl, CTSize ofs) -{ - if (ctype_isarray(ct->info)) { - CType *cct = ctype_rawchild(cts, ct); - CTSize eofs, esz = cct->size, asz = ct->size; - for (eofs = 0; eofs < asz; eofs += esz) - ccall_classify_ct(cts, cct, rcl, ofs+eofs); - } else if (ctype_isstruct(ct->info)) { - ccall_classify_struct(cts, ct, rcl, ofs); - } else { - int cl = ctype_isfp(ct->info) ? CCALL_RCL_SSE : CCALL_RCL_INT; - lua_assert(ctype_hassize(ct->info)); - if ((ofs & (ct->size-1))) cl = CCALL_RCL_MEM; /* Unaligned. */ - rcl[(ofs >= 8)] |= cl; - } -} - -/* Recursively classify a struct based on its fields. */ -static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs) -{ - if (ct->size > 16) return CCALL_RCL_MEM; /* Too big, gets memory class. */ - while (ct->sib) { - CTSize fofs; - ct = ctype_get(cts, ct->sib); - fofs = ofs+ct->size; - if (ctype_isfield(ct->info)) - ccall_classify_ct(cts, ctype_rawchild(cts, ct), rcl, fofs); - else if (ctype_isbitfield(ct->info)) - rcl[(fofs >= 8)] |= CCALL_RCL_INT; /* NYI: unaligned bitfields? */ - else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) - ccall_classify_struct(cts, ctype_rawchild(cts, ct), rcl, fofs); - } - return ((rcl[0]|rcl[1]) & CCALL_RCL_MEM); /* Memory class? */ -} - -/* Try to split up a small struct into registers. */ -static int ccall_struct_reg(CCallState *cc, GPRArg *dp, int *rcl) -{ - MSize ngpr = cc->ngpr, nfpr = cc->nfpr; - uint32_t i; - for (i = 0; i < 2; i++) { - lua_assert(!(rcl[i] & CCALL_RCL_MEM)); - if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */ - if (ngpr >= CCALL_NARG_GPR) return 1; /* Register overflow. */ - cc->gpr[ngpr++] = dp[i]; - } else if ((rcl[i] & CCALL_RCL_SSE)) { - if (nfpr >= CCALL_NARG_FPR) return 1; /* Register overflow. */ - cc->fpr[nfpr++].l[0] = dp[i]; - } - } - cc->ngpr = ngpr; cc->nfpr = nfpr; - return 0; /* Ok. */ -} - -/* Pass a small struct argument. */ -static int ccall_struct_arg(CCallState *cc, CTState *cts, CType *d, int *rcl, - TValue *o, int narg) -{ - GPRArg dp[2]; - dp[0] = dp[1] = 0; - /* Convert to temp. struct. */ - lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg)); - if (ccall_struct_reg(cc, dp, rcl)) { /* Register overflow? Pass on stack. */ - MSize nsp = cc->nsp, n = rcl[1] ? 2 : 1; - if (nsp + n > CCALL_MAXSTACK) return 1; /* Too many arguments. */ - cc->nsp = nsp + n; - memcpy(&cc->stack[nsp], dp, n*CTSIZE_PTR); - } - return 0; /* Ok. */ -} - -/* Combine returned small struct. */ -static void ccall_struct_ret(CCallState *cc, int *rcl, uint8_t *dp, CTSize sz) -{ - GPRArg sp[2]; - MSize ngpr = 0, nfpr = 0; - uint32_t i; - for (i = 0; i < 2; i++) { - if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */ - sp[i] = cc->gpr[ngpr++]; - } else if ((rcl[i] & CCALL_RCL_SSE)) { - sp[i] = cc->fpr[nfpr++].l[0]; - } - } - memcpy(dp, sp, sz); -} -#endif - -/* -- ARM hard-float ABI struct classification ---------------------------- */ - -#if LJ_TARGET_ARM && !LJ_ABI_SOFTFP - -/* Classify a struct based on its fields. */ -static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf) -{ - CTSize sz = ct->size; - unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION); - if ((ctf->info & CTF_VARARG)) goto noth; - while (ct->sib) { - CType *sct; - ct = ctype_get(cts, ct->sib); - if (ctype_isfield(ct->info)) { - sct = ctype_rawchild(cts, ct); - if (ctype_isfp(sct->info)) { - r |= sct->size; - if (!isu) n++; else if (n == 0) n = 1; - } else if (ctype_iscomplex(sct->info)) { - r |= (sct->size >> 1); - if (!isu) n += 2; else if (n < 2) n = 2; - } else if (ctype_isstruct(sct->info)) { - goto substruct; - } else { - goto noth; - } - } else if (ctype_isbitfield(ct->info)) { - goto noth; - } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - sct = ctype_rawchild(cts, ct); - substruct: - if (sct->size > 0) { - unsigned int s = ccall_classify_struct(cts, sct, ctf); - if (s <= 1) goto noth; - r |= (s & 255); - if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8); - } - } - } - if ((r == 4 || r == 8) && n <= 4) - return r + (n << 8); -noth: /* Not a homogeneous float/double aggregate. */ - return (sz <= 4); /* Return structs of size <= 4 in a GPR. */ -} - -#endif - -/* -- Common C call handling ---------------------------------------------- */ - -/* Infer the destination CTypeID for a vararg argument. */ -CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o) -{ - if (tvisnumber(o)) { - return CTID_DOUBLE; - } else if (tviscdata(o)) { - CTypeID id = cdataV(o)->ctypeid; - CType *s = ctype_get(cts, id); - if (ctype_isrefarray(s->info)) { - return lj_ctype_intern(cts, - CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR); - } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) { - /* NYI: how to pass a struct by value in a vararg argument? */ - return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR); - } else if (ctype_isfp(s->info) && s->size == sizeof(float)) { - return CTID_DOUBLE; - } else { - return id; - } - } else if (tvisstr(o)) { - return CTID_P_CCHAR; - } else if (tvisbool(o)) { - return CTID_BOOL; - } else { - return CTID_P_VOID; - } -} - -/* Setup arguments for C call. */ -static int ccall_set_args(lua_State *L, CTState *cts, CType *ct, - CCallState *cc) -{ - int gcsteps = 0; - TValue *o, *top = L->top; - CTypeID fid; - CType *ctr; - MSize maxgpr, ngpr = 0, nsp = 0, narg; -#if CCALL_NARG_FPR - MSize nfpr = 0; -#if LJ_TARGET_ARM - MSize fprodd = 0; -#endif -#endif - - /* Clear unused regs to get some determinism in case of misdeclaration. */ - memset(cc->gpr, 0, sizeof(cc->gpr)); -#if CCALL_NUM_FPR - memset(cc->fpr, 0, sizeof(cc->fpr)); -#endif - -#if LJ_TARGET_X86 - /* x86 has several different calling conventions. */ - cc->resx87 = 0; - switch (ctype_cconv(ct->info)) { - case CTCC_FASTCALL: maxgpr = 2; break; - case CTCC_THISCALL: maxgpr = 1; break; - default: maxgpr = 0; break; - } -#else - maxgpr = CCALL_NARG_GPR; -#endif - - /* Perform required setup for some result types. */ - ctr = ctype_rawchild(cts, ct); - if (ctype_isvector(ctr->info)) { - if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16))) - goto err_nyi; - } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) { - /* Preallocate cdata object and anchor it after arguments. */ - CTSize sz = ctr->size; - GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz); - void *dp = cdataptr(cd); - setcdataV(L, L->top++, cd); - if (ctype_isstruct(ctr->info)) { - CCALL_HANDLE_STRUCTRET - } else { - CCALL_HANDLE_COMPLEXRET - } -#if LJ_TARGET_X86 - } else if (ctype_isfp(ctr->info)) { - cc->resx87 = ctr->size == sizeof(float) ? 1 : 2; -#endif - } - - /* Skip initial attributes. */ - fid = ct->sib; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) break; - fid = ctf->sib; - } - - /* Walk through all passed arguments. */ - for (o = L->base+1, narg = 1; o < top; o++, narg++) { - CTypeID did; - CType *d; - CTSize sz; - MSize n, isfp = 0, isva = 0; - void *dp, *rp = NULL; - - if (fid) { /* Get argument type from field. */ - CType *ctf = ctype_get(cts, fid); - fid = ctf->sib; - lua_assert(ctype_isfield(ctf->info)); - did = ctype_cid(ctf->info); - } else { - if (!(ct->info & CTF_VARARG)) - lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too many arguments. */ - did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */ - isva = 1; - } - d = ctype_raw(cts, did); - sz = d->size; - - /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */ - if (ctype_isnum(d->info)) { - if (sz > 8) goto err_nyi; - if ((d->info & CTF_FP)) - isfp = 1; - } else if (ctype_isvector(d->info)) { - if (CCALL_VECTOR_REG && (sz == 8 || sz == 16)) - isfp = 1; - else - goto err_nyi; - } else if (ctype_isstruct(d->info)) { - CCALL_HANDLE_STRUCTARG - } else if (ctype_iscomplex(d->info)) { - CCALL_HANDLE_COMPLEXARG - } else { - sz = CTSIZE_PTR; - } - sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); - n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */ - - CCALL_HANDLE_REGARG /* Handle register arguments. */ - - /* Otherwise pass argument on stack. */ - if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) { - MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1; - nsp = (nsp + align) & ~align; /* Align argument on stack. */ - } - if (nsp + n > CCALL_MAXSTACK) { /* Too many arguments. */ - err_nyi: - lj_err_caller(L, LJ_ERR_FFI_NYICALL); - } - dp = &cc->stack[nsp]; - nsp += n; - isva = 0; - - done: - if (rp) { /* Pass by reference. */ - gcsteps++; - *(void **)dp = rp; - dp = rp; - } - lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg)); - /* Extend passed integers to 32 bits at least. */ - if (ctype_isinteger_or_bool(d->info) && d->size < 4) { - if (d->info & CTF_UNSIGNED) - *(uint32_t *)dp = d->size == 1 ? (uint32_t)*(uint8_t *)dp : - (uint32_t)*(uint16_t *)dp; - else - *(int32_t *)dp = d->size == 1 ? (int32_t)*(int8_t *)dp : - (int32_t)*(int16_t *)dp; - } -#if LJ_TARGET_X64 && LJ_ABI_WIN - if (isva) { /* Windows/x64 mirrors varargs in both register sets. */ - if (nfpr == ngpr) - cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0]; - else - cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1]; - } -#else - UNUSED(isva); -#endif -#if LJ_TARGET_X64 && !LJ_ABI_WIN - if (isfp == 2 && n == 2 && (uint8_t *)dp == (uint8_t *)&cc->fpr[nfpr-2]) { - cc->fpr[nfpr-1].d[0] = cc->fpr[nfpr-2].d[1]; /* Split complex double. */ - cc->fpr[nfpr-2].d[1] = 0; - } -#else - UNUSED(isfp); -#endif - } - if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too few arguments. */ - -#if LJ_TARGET_X64 || LJ_TARGET_PPC - cc->nfpr = nfpr; /* Required for vararg functions. */ -#endif - cc->nsp = nsp; - cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR; - if (nsp > CCALL_SPS_FREE) - cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u); - return gcsteps; -} - -/* Get results from C call. */ -static int ccall_get_results(lua_State *L, CTState *cts, CType *ct, - CCallState *cc, int *ret) -{ - CType *ctr = ctype_rawchild(cts, ct); - uint8_t *sp = (uint8_t *)&cc->gpr[0]; - if (ctype_isvoid(ctr->info)) { - *ret = 0; /* Zero results. */ - return 0; /* No additional GC step. */ - } - *ret = 1; /* One result. */ - if (ctype_isstruct(ctr->info)) { - /* Return cdata object which is already on top of stack. */ - if (!cc->retref) { - void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */ - CCALL_HANDLE_STRUCTRET2 - } - return 1; /* One GC step. */ - } - if (ctype_iscomplex(ctr->info)) { - /* Return cdata object which is already on top of stack. */ - void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */ - CCALL_HANDLE_COMPLEXRET2 - return 1; /* One GC step. */ - } - if (LJ_BE && ctype_isinteger_or_bool(ctr->info) && ctr->size < CTSIZE_PTR) - sp += (CTSIZE_PTR - ctr->size); -#if CCALL_NUM_FPR - if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info)) - sp = (uint8_t *)&cc->fpr[0]; -#endif -#ifdef CCALL_HANDLE_RET - CCALL_HANDLE_RET -#endif - /* No reference types end up here, so there's no need for the CTypeID. */ - lua_assert(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info))); - return lj_cconv_tv_ct(cts, ctr, 0, L->top-1, sp); -} - -/* Call C function. */ -int lj_ccall_func(lua_State *L, GCcdata *cd) -{ - CTState *cts = ctype_cts(L); - CType *ct = ctype_raw(cts, cd->ctypeid); - CTSize sz = CTSIZE_PTR; - if (ctype_isptr(ct->info)) { - sz = ct->size; - ct = ctype_rawchild(cts, ct); - } - if (ctype_isfunc(ct->info)) { - CCallState cc; - int gcsteps, ret; - cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz); - gcsteps = ccall_set_args(L, cts, ct, &cc); - ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab); - cts->cb.slot = ~0u; - lj_vm_ffi_call(&cc); - if (cts->cb.slot != ~0u) { /* Blacklist function that called a callback. */ - TValue tv; - setlightudV(&tv, (void *)cc.func); - setboolV(lj_tab_set(L, cts->miscmap, &tv), 1); - } - ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab); /* May be reallocated. */ - gcsteps += ccall_get_results(L, cts, ct, &cc, &ret); -#if LJ_TARGET_X86 && LJ_ABI_WIN - /* Automatically detect __stdcall and fix up C function declaration. */ - if (cc.spadj && ctype_cconv(ct->info) == CTCC_CDECL) { - CTF_INSERT(ct->info, CCONV, CTCC_STDCALL); - lj_trace_abort(G(L)); - } -#endif - while (gcsteps-- > 0) - lj_gc_check(L); - return ret; - } - return -1; /* Not a function. */ -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ccall.h b/third-party/LuaJIT-2.0.2/src/lj_ccall.h deleted file mode 100644 index 1afeed75b2..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ccall.h +++ /dev/null @@ -1,171 +0,0 @@ -/* -** FFI C call handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CCALL_H -#define _LJ_CCALL_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* -- C calling conventions ----------------------------------------------- */ - -#if LJ_TARGET_X86ORX64 - -#if LJ_TARGET_X86 -#define CCALL_NARG_GPR 2 /* For fastcall arguments. */ -#define CCALL_NARG_FPR 0 -#define CCALL_NRET_GPR 2 -#define CCALL_NRET_FPR 1 /* For FP results on x87 stack. */ -#define CCALL_ALIGN_STACKARG 0 /* Don't align argument on stack. */ -#elif LJ_ABI_WIN -#define CCALL_NARG_GPR 4 -#define CCALL_NARG_FPR 4 -#define CCALL_NRET_GPR 1 -#define CCALL_NRET_FPR 1 -#define CCALL_SPS_EXTRA 4 -#else -#define CCALL_NARG_GPR 6 -#define CCALL_NARG_FPR 8 -#define CCALL_NRET_GPR 2 -#define CCALL_NRET_FPR 2 -#define CCALL_VECTOR_REG 1 /* Pass vectors in registers. */ -#endif - -#define CCALL_SPS_FREE 1 -#define CCALL_ALIGN_CALLSTATE 16 - -typedef LJ_ALIGN(16) union FPRArg { - double d[2]; - float f[4]; - uint8_t b[16]; - uint16_t s[8]; - int i[4]; - int64_t l[2]; -} FPRArg; - -typedef intptr_t GPRArg; - -#elif LJ_TARGET_ARM - -#define CCALL_NARG_GPR 4 -#define CCALL_NRET_GPR 2 /* For softfp double. */ -#if LJ_ABI_SOFTFP -#define CCALL_NARG_FPR 0 -#define CCALL_NRET_FPR 0 -#else -#define CCALL_NARG_FPR 8 -#define CCALL_NRET_FPR 4 -#endif -#define CCALL_SPS_FREE 0 - -typedef intptr_t GPRArg; -typedef union FPRArg { - double d; - float f[2]; -} FPRArg; - -#elif LJ_TARGET_PPC - -#define CCALL_NARG_GPR 8 -#define CCALL_NARG_FPR 8 -#define CCALL_NRET_GPR 4 /* For complex double. */ -#define CCALL_NRET_FPR 1 -#define CCALL_SPS_EXTRA 4 -#define CCALL_SPS_FREE 0 - -typedef intptr_t GPRArg; -typedef double FPRArg; - -#elif LJ_TARGET_PPCSPE - -#define CCALL_NARG_GPR 8 -#define CCALL_NARG_FPR 0 -#define CCALL_NRET_GPR 4 /* For softfp complex double. */ -#define CCALL_NRET_FPR 0 -#define CCALL_SPS_FREE 0 /* NYI */ - -typedef intptr_t GPRArg; - -#elif LJ_TARGET_MIPS - -#define CCALL_NARG_GPR 4 -#define CCALL_NARG_FPR 2 -#define CCALL_NRET_GPR 2 -#define CCALL_NRET_FPR 2 -#define CCALL_SPS_EXTRA 7 -#define CCALL_SPS_FREE 1 - -typedef intptr_t GPRArg; -typedef union FPRArg { - double d; - struct { LJ_ENDIAN_LOHI(float f; , float g;) }; -} FPRArg; - -#else -#error "Missing calling convention definitions for this architecture" -#endif - -#ifndef CCALL_SPS_EXTRA -#define CCALL_SPS_EXTRA 0 -#endif -#ifndef CCALL_VECTOR_REG -#define CCALL_VECTOR_REG 0 -#endif -#ifndef CCALL_ALIGN_STACKARG -#define CCALL_ALIGN_STACKARG 1 -#endif -#ifndef CCALL_ALIGN_CALLSTATE -#define CCALL_ALIGN_CALLSTATE 8 -#endif - -#define CCALL_NUM_GPR \ - (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR) -#define CCALL_NUM_FPR \ - (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR) - -/* Check against constants in lj_ctype.h. */ -LJ_STATIC_ASSERT(CCALL_NUM_GPR <= CCALL_MAX_GPR); -LJ_STATIC_ASSERT(CCALL_NUM_FPR <= CCALL_MAX_FPR); - -#define CCALL_MAXSTACK 32 - -/* -- C call state -------------------------------------------------------- */ - -typedef LJ_ALIGN(CCALL_ALIGN_CALLSTATE) struct CCallState { - void (*func)(void); /* Pointer to called function. */ - uint32_t spadj; /* Stack pointer adjustment. */ - uint8_t nsp; /* Number of stack slots. */ - uint8_t retref; /* Return value by reference. */ -#if LJ_TARGET_X64 - uint8_t ngpr; /* Number of arguments in GPRs. */ - uint8_t nfpr; /* Number of arguments in FPRs. */ -#elif LJ_TARGET_X86 - uint8_t resx87; /* Result on x87 stack: 1:float, 2:double. */ -#elif LJ_TARGET_PPC - uint8_t nfpr; /* Number of arguments in FPRs. */ -#endif -#if LJ_32 - int32_t align1; -#endif -#if CCALL_NUM_FPR - FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */ -#endif - GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */ - GPRArg stack[CCALL_MAXSTACK]; /* Stack slots. */ -} CCallState; - -/* -- C call handling ----------------------------------------------------- */ - -/* Really belongs to lj_vm.h. */ -LJ_ASMF void LJ_FASTCALL lj_vm_ffi_call(CCallState *cc); - -LJ_FUNC CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o); -LJ_FUNC int lj_ccall_func(lua_State *L, GCcdata *cd); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ccallback.c b/third-party/LuaJIT-2.0.2/src/lj_ccallback.c deleted file mode 100644 index 0010992323..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ccallback.c +++ /dev/null @@ -1,641 +0,0 @@ -/* -** FFI C callback handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_ccall.h" -#include "lj_ccallback.h" -#include "lj_target.h" -#include "lj_mcode.h" -#include "lj_trace.h" -#include "lj_vm.h" - -/* -- Target-specific handling of callback slots -------------------------- */ - -#define CALLBACK_MCODE_SIZE (LJ_PAGESIZE * LJ_NUM_CBPAGE) - -#if LJ_OS_NOJIT - -/* Disabled callback support. */ -#define CALLBACK_SLOT2OFS(slot) (0*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (0*(ofs)) -#define CALLBACK_MAX_SLOT 0 - -#elif LJ_TARGET_X86ORX64 - -#define CALLBACK_MCODE_HEAD (LJ_64 ? 8 : 0) -#define CALLBACK_MCODE_GROUP (-2+1+2+5+(LJ_64 ? 6 : 5)) - -#define CALLBACK_SLOT2OFS(slot) \ - (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot)) - -static MSize CALLBACK_OFS2SLOT(MSize ofs) -{ - MSize group; - ofs -= CALLBACK_MCODE_HEAD; - group = ofs / (32*4 + CALLBACK_MCODE_GROUP); - return (ofs % (32*4 + CALLBACK_MCODE_GROUP))/4 + group*32; -} - -#define CALLBACK_MAX_SLOT \ - (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32) - -#elif LJ_TARGET_ARM - -#define CALLBACK_MCODE_HEAD 32 -#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8) -#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE)) - -#elif LJ_TARGET_PPC - -#define CALLBACK_MCODE_HEAD 24 -#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8) -#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE)) - -#elif LJ_TARGET_MIPS - -#define CALLBACK_MCODE_HEAD 24 -#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8) -#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE)) - -#else - -/* Missing support for this architecture. */ -#define CALLBACK_SLOT2OFS(slot) (0*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (0*(ofs)) -#define CALLBACK_MAX_SLOT 0 - -#endif - -/* Convert callback slot number to callback function pointer. */ -static void *callback_slot2ptr(CTState *cts, MSize slot) -{ - return (uint8_t *)cts->cb.mcode + CALLBACK_SLOT2OFS(slot); -} - -/* Convert callback function pointer to slot number. */ -MSize lj_ccallback_ptr2slot(CTState *cts, void *p) -{ - uintptr_t ofs = (uintptr_t)((uint8_t *)p -(uint8_t *)cts->cb.mcode); - if (ofs < CALLBACK_MCODE_SIZE) { - MSize slot = CALLBACK_OFS2SLOT((MSize)ofs); - if (CALLBACK_SLOT2OFS(slot) == (MSize)ofs) - return slot; - } - return ~0u; /* Not a known callback function pointer. */ -} - -/* Initialize machine code for callback function pointers. */ -#if LJ_OS_NOJIT -/* Disabled callback support. */ -#define callback_mcode_init(g, p) UNUSED(p) -#elif LJ_TARGET_X86ORX64 -static void callback_mcode_init(global_State *g, uint8_t *page) -{ - uint8_t *p = page; - uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback; - MSize slot; -#if LJ_64 - *(void **)p = target; p += 8; -#endif - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - /* mov al, slot; jmp group */ - *p++ = XI_MOVrib | RID_EAX; *p++ = (uint8_t)slot; - if ((slot & 31) == 31 || slot == CALLBACK_MAX_SLOT-1) { - /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */ - *p++ = XI_PUSH + RID_EBP; - *p++ = XI_MOVrib | (RID_EAX+4); *p++ = (uint8_t)(slot >> 8); - *p++ = XI_MOVri | RID_EBP; - *(int32_t *)p = i32ptr(g); p += 4; -#if LJ_64 - /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */ - *p++ = XI_GROUP5; *p++ = XM_OFS0 + (XOg_JMP<<3) + RID_EBP; - *(int32_t *)p = (int32_t)(page-(p+4)); p += 4; -#else - /* jmp lj_vm_ffi_callback. */ - *p++ = XI_JMP; *(int32_t *)p = target-(p+4); p += 4; -#endif - } else { - *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2); - } - } - lua_assert(p - page <= CALLBACK_MCODE_SIZE); -} -#elif LJ_TARGET_ARM -static void callback_mcode_init(global_State *g, uint32_t *page) -{ - uint32_t *p = page; - void *target = (void *)lj_vm_ffi_callback; - MSize slot; - /* This must match with the saveregs macro in buildvm_arm.dasc. */ - *p++ = ARMI_SUB|ARMF_D(RID_R12)|ARMF_N(RID_R12)|ARMF_M(RID_PC); - *p++ = ARMI_PUSH|ARMF_N(RID_SP)|RSET_RANGE(RID_R4,RID_R11+1)|RID2RSET(RID_LR); - *p++ = ARMI_SUB|ARMI_K12|ARMF_D(RID_R12)|ARMF_N(RID_R12)|CALLBACK_MCODE_HEAD; - *p++ = ARMI_STR|ARMI_LS_P|ARMI_LS_W|ARMF_D(RID_R12)|ARMF_N(RID_SP)|(CFRAME_SIZE-4*9); - *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_R12)|ARMF_N(RID_PC); - *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_PC)|ARMF_N(RID_PC); - *p++ = u32ptr(g); - *p++ = u32ptr(target); - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - *p++ = ARMI_MOV|ARMF_D(RID_R12)|ARMF_M(RID_PC); - *p = ARMI_B | ((page-p-2) & 0x00ffffffu); - p++; - } - lua_assert(p - page <= CALLBACK_MCODE_SIZE); -} -#elif LJ_TARGET_PPC -static void callback_mcode_init(global_State *g, uint32_t *page) -{ - uint32_t *p = page; - void *target = (void *)lj_vm_ffi_callback; - MSize slot; - *p++ = PPCI_LIS | PPCF_T(RID_TMP) | (u32ptr(target) >> 16); - *p++ = PPCI_LIS | PPCF_T(RID_R12) | (u32ptr(g) >> 16); - *p++ = PPCI_ORI | PPCF_A(RID_TMP)|PPCF_T(RID_TMP) | (u32ptr(target) & 0xffff); - *p++ = PPCI_ORI | PPCF_A(RID_R12)|PPCF_T(RID_R12) | (u32ptr(g) & 0xffff); - *p++ = PPCI_MTCTR | PPCF_T(RID_TMP); - *p++ = PPCI_BCTR; - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - *p++ = PPCI_LI | PPCF_T(RID_R11) | slot; - *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2); - p++; - } - lua_assert(p - page <= CALLBACK_MCODE_SIZE); -} -#elif LJ_TARGET_MIPS -static void callback_mcode_init(global_State *g, uint32_t *page) -{ - uint32_t *p = page; - void *target = (void *)lj_vm_ffi_callback; - MSize slot; - *p++ = MIPSI_SW | MIPSF_T(RID_R1)|MIPSF_S(RID_SP) | 0; - *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (u32ptr(target) >> 16); - *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (u32ptr(g) >> 16); - *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) |(u32ptr(target)&0xffff); - *p++ = MIPSI_JR | MIPSF_S(RID_R3); - *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | (u32ptr(g)&0xffff); - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - *p = MIPSI_B | ((page-p-1) & 0x0000ffffu); - p++; - *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot; - } - lua_assert(p - page <= CALLBACK_MCODE_SIZE); -} -#else -/* Missing support for this architecture. */ -#define callback_mcode_init(g, p) UNUSED(p) -#endif - -/* -- Machine code management --------------------------------------------- */ - -#if LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#elif LJ_TARGET_POSIX - -#include -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif - -#endif - -/* Allocate and initialize area for callback function pointers. */ -static void callback_mcode_new(CTState *cts) -{ - size_t sz = (size_t)CALLBACK_MCODE_SIZE; - void *p; - if (CALLBACK_MAX_SLOT == 0) - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); -#if LJ_TARGET_WINDOWS - p = VirtualAlloc(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); - if (!p) - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); -#elif LJ_TARGET_POSIX - p = mmap(NULL, sz, (PROT_READ|PROT_WRITE), MAP_PRIVATE|MAP_ANONYMOUS, - -1, 0); - if (p == MAP_FAILED) - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); -#else - /* Fallback allocator. Fails if memory is not executable by default. */ - p = lj_mem_new(cts->L, sz); -#endif - cts->cb.mcode = p; - callback_mcode_init(cts->g, p); - lj_mcode_sync(p, (char *)p + sz); -#if LJ_TARGET_WINDOWS - { - DWORD oprot; - VirtualProtect(p, sz, PAGE_EXECUTE_READ, &oprot); - } -#elif LJ_TARGET_POSIX - mprotect(p, sz, (PROT_READ|PROT_EXEC)); -#endif -} - -/* Free area for callback function pointers. */ -void lj_ccallback_mcode_free(CTState *cts) -{ - size_t sz = (size_t)CALLBACK_MCODE_SIZE; - void *p = cts->cb.mcode; - if (p == NULL) return; -#if LJ_TARGET_WINDOWS - VirtualFree(p, 0, MEM_RELEASE); - UNUSED(sz); -#elif LJ_TARGET_POSIX - munmap(p, sz); -#else - lj_mem_free(cts->g, p, sz); -#endif -} - -/* -- C callback entry ---------------------------------------------------- */ - -/* Target-specific handling of register arguments. Similar to lj_ccall.c. */ -#if LJ_TARGET_X86 - -#define CALLBACK_HANDLE_REGARG \ - if (!isfp) { /* Only non-FP values may be passed in registers. */ \ - if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \ - if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \ - } else if (ngpr + 1 <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_X64 && LJ_ABI_WIN - -/* Windows/x64 argument registers are strictly positional (use ngpr). */ -#define CALLBACK_HANDLE_REGARG \ - if (isfp) { \ - if (ngpr < maxgpr) { sp = &cts->cb.fpr[ngpr++]; UNUSED(nfpr); goto done; } \ - } else { \ - if (ngpr < maxgpr) { sp = &cts->cb.gpr[ngpr++]; goto done; } \ - } - -#elif LJ_TARGET_X64 - -#define CALLBACK_HANDLE_REGARG \ - if (isfp) { \ - if (nfpr + n <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr]; \ - nfpr += n; \ - goto done; \ - } \ - } else { \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_ARM - -#if LJ_ABI_SOFTFP - -#define CALLBACK_HANDLE_REGARG_FP1 UNUSED(isfp); -#define CALLBACK_HANDLE_REGARG_FP2 - -#else - -#define CALLBACK_HANDLE_REGARG_FP1 \ - if (isfp) { \ - if (n == 1) { \ - if (fprodd) { \ - sp = &cts->cb.fpr[fprodd-1]; \ - fprodd = 0; \ - goto done; \ - } else if (nfpr + 1 <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr++]; \ - fprodd = nfpr; \ - goto done; \ - } \ - } else { \ - if (nfpr + 1 <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr++]; \ - goto done; \ - } \ - } \ - fprodd = 0; /* No reordering after the first FP value is on stack. */ \ - } else { - -#define CALLBACK_HANDLE_REGARG_FP2 } - -#endif - -#define CALLBACK_HANDLE_REGARG \ - CALLBACK_HANDLE_REGARG_FP1 \ - if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } CALLBACK_HANDLE_REGARG_FP2 - -#elif LJ_TARGET_PPC - -#define CALLBACK_HANDLE_REGARG \ - if (isfp) { \ - if (nfpr + 1 <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr++]; \ - cta = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \ - goto done; \ - } \ - } else { /* Try to pass argument in GPRs. */ \ - if (n > 1) { \ - lua_assert(ctype_isinteger(cta->info) && n == 2); /* int64_t. */ \ - ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ - } \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#define CALLBACK_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - *(double *)dp = *(float *)dp; /* FPRs always hold doubles. */ - -#elif LJ_TARGET_MIPS - -#define CALLBACK_HANDLE_REGARG \ - if (isfp && nfpr < CCALL_NARG_FPR) { /* Try to pass argument in FPRs. */ \ - sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \ - nfpr++; ngpr += n; \ - goto done; \ - } else { /* Try to pass argument in GPRs. */ \ - nfpr = CCALL_NARG_FPR; \ - if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#define CALLBACK_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - ((float *)dp)[1] = *(float *)dp; - -#else -#error "Missing calling convention definitions for this architecture" -#endif - -/* Convert and push callback arguments to Lua stack. */ -static void callback_conv_args(CTState *cts, lua_State *L) -{ - TValue *o = L->top; - intptr_t *stack = cts->cb.stack; - MSize slot = cts->cb.slot; - CTypeID id = 0, rid, fid; - CType *ct; - GCfunc *fn; - MSize ngpr = 0, nsp = 0, maxgpr = CCALL_NARG_GPR; -#if CCALL_NARG_FPR - MSize nfpr = 0; -#if LJ_TARGET_ARM - MSize fprodd = 0; -#endif -#endif - - if (slot < cts->cb.sizeid && (id = cts->cb.cbid[slot]) != 0) { - ct = ctype_get(cts, id); - rid = ctype_cid(ct->info); - fn = funcV(lj_tab_getint(cts->miscmap, (int32_t)slot)); - } else { /* Must set up frame first, before throwing the error. */ - ct = NULL; - rid = 0; - fn = (GCfunc *)L; - } - o->u32.lo = LJ_CONT_FFI_CALLBACK; /* Continuation returns from callback. */ - o->u32.hi = rid; /* Return type. x86: +(spadj<<16). */ - o++; - setframe_gc(o, obj2gco(fn)); - setframe_ftsz(o, (int)((char *)(o+1) - (char *)L->base) + FRAME_CONT); - L->top = L->base = ++o; - if (!ct) - lj_err_caller(cts->L, LJ_ERR_FFI_BADCBACK); - if (isluafunc(fn)) - setcframe_pc(L->cframe, proto_bc(funcproto(fn))+1); - lj_state_checkstack(L, LUA_MINSTACK); /* May throw. */ - o = L->base; /* Might have been reallocated. */ - -#if LJ_TARGET_X86 - /* x86 has several different calling conventions. */ - switch (ctype_cconv(ct->info)) { - case CTCC_FASTCALL: maxgpr = 2; break; - case CTCC_THISCALL: maxgpr = 1; break; - default: maxgpr = 0; break; - } -#endif - - fid = ct->sib; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) { - CType *cta; - void *sp; - CTSize sz; - int isfp; - MSize n; - lua_assert(ctype_isfield(ctf->info)); - cta = ctype_rawchild(cts, ctf); - isfp = ctype_isfp(cta->info); - sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); - n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */ - - CALLBACK_HANDLE_REGARG /* Handle register arguments. */ - - /* Otherwise pass argument on stack. */ - if (CCALL_ALIGN_STACKARG && LJ_32 && sz == 8) - nsp = (nsp + 1) & ~1u; /* Align 64 bit argument on stack. */ - sp = &stack[nsp]; - nsp += n; - - done: - if (LJ_BE && cta->size < CTSIZE_PTR) - sp = (void *)((uint8_t *)sp + CTSIZE_PTR-cta->size); - lj_cconv_tv_ct(cts, cta, 0, o++, sp); - } - fid = ctf->sib; - } - L->top = o; -#if LJ_TARGET_X86 - /* Store stack adjustment for returns from non-cdecl callbacks. */ - if (ctype_cconv(ct->info) != CTCC_CDECL) - (L->base-2)->u32.hi |= (nsp << (16+2)); -#endif -} - -/* Convert Lua object to callback result. */ -static void callback_conv_result(CTState *cts, lua_State *L, TValue *o) -{ - CType *ctr = ctype_raw(cts, (uint16_t)(L->base-2)->u32.hi); -#if LJ_TARGET_X86 - cts->cb.gpr[2] = 0; -#endif - if (!ctype_isvoid(ctr->info)) { - uint8_t *dp = (uint8_t *)&cts->cb.gpr[0]; -#if CCALL_NUM_FPR - if (ctype_isfp(ctr->info)) - dp = (uint8_t *)&cts->cb.fpr[0]; -#endif - lj_cconv_ct_tv(cts, ctr, dp, o, 0); -#ifdef CALLBACK_HANDLE_RET - CALLBACK_HANDLE_RET -#endif - /* Extend returned integers to (at least) 32 bits. */ - if (ctype_isinteger_or_bool(ctr->info) && ctr->size < 4) { - if (ctr->info & CTF_UNSIGNED) - *(uint32_t *)dp = ctr->size == 1 ? (uint32_t)*(uint8_t *)dp : - (uint32_t)*(uint16_t *)dp; - else - *(int32_t *)dp = ctr->size == 1 ? (int32_t)*(int8_t *)dp : - (int32_t)*(int16_t *)dp; - } -#if LJ_TARGET_X86 - if (ctype_isfp(ctr->info)) - cts->cb.gpr[2] = ctr->size == sizeof(float) ? 1 : 2; -#endif - } -} - -/* Enter callback. */ -lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf) -{ - lua_State *L = cts->L; - global_State *g = cts->g; - lua_assert(L != NULL); - if (gcref(g->jit_L)) { - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK)); - if (g->panic) g->panic(L); - exit(EXIT_FAILURE); - } - lj_trace_abort(g); /* Never record across callback. */ - /* Setup C frame. */ - cframe_prev(cf) = L->cframe; - setcframe_L(cf, L); - cframe_errfunc(cf) = -1; - cframe_nres(cf) = 0; - L->cframe = cf; - callback_conv_args(cts, L); - return L; /* Now call the function on this stack. */ -} - -/* Leave callback. */ -void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o) -{ - lua_State *L = cts->L; - GCfunc *fn; - TValue *obase = L->base; - L->base = L->top; /* Keep continuation frame for throwing errors. */ - if (o >= L->base) { - /* PC of RET* is lost. Point to last line for result conv. errors. */ - fn = curr_func(L); - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - setcframe_pc(L->cframe, proto_bc(pt)+pt->sizebc+1); - } - } - callback_conv_result(cts, L, o); - /* Finally drop C frame and continuation frame. */ - L->cframe = cframe_prev(L->cframe); - L->top -= 2; - L->base = obase; - cts->cb.slot = 0; /* Blacklist C function that called the callback. */ -} - -/* -- C callback management ----------------------------------------------- */ - -/* Get an unused slot in the callback slot table. */ -static MSize callback_slot_new(CTState *cts, CType *ct) -{ - CTypeID id = ctype_typeid(cts, ct); - CTypeID1 *cbid = cts->cb.cbid; - MSize top; - for (top = cts->cb.topid; top < cts->cb.sizeid; top++) - if (LJ_LIKELY(cbid[top] == 0)) - goto found; -#if CALLBACK_MAX_SLOT - if (top >= CALLBACK_MAX_SLOT) -#endif - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); - if (!cts->cb.mcode) - callback_mcode_new(cts); - lj_mem_growvec(cts->L, cbid, cts->cb.sizeid, CALLBACK_MAX_SLOT, CTypeID1); - cts->cb.cbid = cbid; - memset(cbid+top, 0, (cts->cb.sizeid-top)*sizeof(CTypeID1)); -found: - cbid[top] = id; - cts->cb.topid = top+1; - return top; -} - -/* Check for function pointer and supported argument/result types. */ -static CType *callback_checkfunc(CTState *cts, CType *ct) -{ - int narg = 0; - if (!ctype_isptr(ct->info) || (LJ_64 && ct->size != CTSIZE_PTR)) - return NULL; - ct = ctype_rawchild(cts, ct); - if (ctype_isfunc(ct->info)) { - CType *ctr = ctype_rawchild(cts, ct); - CTypeID fid = ct->sib; - if (!(ctype_isvoid(ctr->info) || ctype_isenum(ctr->info) || - ctype_isptr(ctr->info) || (ctype_isnum(ctr->info) && ctr->size <= 8))) - return NULL; - if ((ct->info & CTF_VARARG)) - return NULL; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) { - CType *cta; - lua_assert(ctype_isfield(ctf->info)); - cta = ctype_rawchild(cts, ctf); - if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) || - (ctype_isnum(cta->info) && cta->size <= 8)) || - ++narg >= LUA_MINSTACK-3) - return NULL; - } - fid = ctf->sib; - } - return ct; - } - return NULL; -} - -/* Create a new callback and return the callback function pointer. */ -void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn) -{ - ct = callback_checkfunc(cts, ct); - if (ct) { - MSize slot = callback_slot_new(cts, ct); - GCtab *t = cts->miscmap; - setfuncV(cts->L, lj_tab_setint(cts->L, t, (int32_t)slot), fn); - lj_gc_anybarriert(cts->L, t); - return callback_slot2ptr(cts, slot); - } - return NULL; /* Bad conversion. */ -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ccallback.h b/third-party/LuaJIT-2.0.2/src/lj_ccallback.h deleted file mode 100644 index ac11d7b66d..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ccallback.h +++ /dev/null @@ -1,25 +0,0 @@ -/* -** FFI C callback handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CCALLBACK_H -#define _LJ_CCALLBACK_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* Really belongs to lj_vm.h. */ -LJ_ASMF void lj_vm_ffi_callback(void); - -LJ_FUNC MSize lj_ccallback_ptr2slot(CTState *cts, void *p); -LJ_FUNCA lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf); -LJ_FUNCA void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o); -LJ_FUNC void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn); -LJ_FUNC void lj_ccallback_mcode_free(CTState *cts); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_cconv.c b/third-party/LuaJIT-2.0.2/src/lj_cconv.c deleted file mode 100644 index 337382f5f2..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_cconv.c +++ /dev/null @@ -1,751 +0,0 @@ -/* -** C type conversions. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_cconv.h" -#include "lj_ccallback.h" - -/* -- Conversion errors --------------------------------------------------- */ - -/* Bad conversion. */ -LJ_NORET static void cconv_err_conv(CTState *cts, CType *d, CType *s, - CTInfo flags) -{ - const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); - const char *src; - if ((flags & CCF_FROMTV)) - src = lj_obj_typename[1+(ctype_isnum(s->info) ? LUA_TNUMBER : - ctype_isarray(s->info) ? LUA_TSTRING : LUA_TNIL)]; - else - src = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, s), NULL)); - if (CCF_GETARG(flags)) - lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst); - else - lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst); -} - -/* Bad conversion from TValue. */ -LJ_NORET static void cconv_err_convtv(CTState *cts, CType *d, TValue *o, - CTInfo flags) -{ - const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); - const char *src = lj_typename(o); - if (CCF_GETARG(flags)) - lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst); - else - lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst); -} - -/* Initializer overflow. */ -LJ_NORET static void cconv_err_initov(CTState *cts, CType *d) -{ - const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); - lj_err_callerv(cts->L, LJ_ERR_FFI_INITOV, dst); -} - -/* -- C type compatibility checks ----------------------------------------- */ - -/* Get raw type and qualifiers for a child type. Resolves enums, too. */ -static CType *cconv_childqual(CTState *cts, CType *ct, CTInfo *qual) -{ - ct = ctype_child(cts, ct); - for (;;) { - if (ctype_isattrib(ct->info)) { - if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size; - } else if (!ctype_isenum(ct->info)) { - break; - } - ct = ctype_child(cts, ct); - } - *qual |= (ct->info & CTF_QUAL); - return ct; -} - -/* Check for compatible types when converting to a pointer. -** Note: these checks are more relaxed than what C99 mandates. -*/ -int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags) -{ - if (!((flags & CCF_CAST) || d == s)) { - CTInfo dqual = 0, squal = 0; - d = cconv_childqual(cts, d, &dqual); - if (!ctype_isstruct(s->info)) - s = cconv_childqual(cts, s, &squal); - if ((flags & CCF_SAME)) { - if (dqual != squal) - return 0; /* Different qualifiers. */ - } else if (!(flags & CCF_IGNQUAL)) { - if ((dqual & squal) != squal) - return 0; /* Discarded qualifiers. */ - if (ctype_isvoid(d->info) || ctype_isvoid(s->info)) - return 1; /* Converting to/from void * is always ok. */ - } - if (ctype_type(d->info) != ctype_type(s->info) || - d->size != s->size) - return 0; /* Different type or different size. */ - if (ctype_isnum(d->info)) { - if (((d->info ^ s->info) & (CTF_BOOL|CTF_FP))) - return 0; /* Different numeric types. */ - } else if (ctype_ispointer(d->info)) { - /* Check child types for compatibility. */ - return lj_cconv_compatptr(cts, d, s, flags|CCF_SAME); - } else if (ctype_isstruct(d->info)) { - if (d != s) - return 0; /* Must be exact same type for struct/union. */ - } else if (ctype_isfunc(d->info)) { - /* NYI: structural equality of functions. */ - } - } - return 1; /* Types are compatible. */ -} - -/* -- C type to C type conversion ----------------------------------------- */ - -/* Convert C type to C type. Caveat: expects to get the raw CType! -** -** Note: This is only used by the interpreter and not optimized at all. -** The JIT compiler will do a much better job specializing for each case. -*/ -void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s, - uint8_t *dp, uint8_t *sp, CTInfo flags) -{ - CTSize dsize = d->size, ssize = s->size; - CTInfo dinfo = d->info, sinfo = s->info; - void *tmpptr; - - lua_assert(!ctype_isenum(dinfo) && !ctype_isenum(sinfo)); - lua_assert(!ctype_isattrib(dinfo) && !ctype_isattrib(sinfo)); - - if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT) - goto err_conv; - - /* Some basic sanity checks. */ - lua_assert(!ctype_isnum(dinfo) || dsize > 0); - lua_assert(!ctype_isnum(sinfo) || ssize > 0); - lua_assert(!ctype_isbool(dinfo) || dsize == 1 || dsize == 4); - lua_assert(!ctype_isbool(sinfo) || ssize == 1 || ssize == 4); - lua_assert(!ctype_isinteger(dinfo) || (1u< ssize) { /* Zero-extend or sign-extend LSB. */ -#if LJ_LE - uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[ssize-1]&0x80)) ? 0xff : 0; - memcpy(dp, sp, ssize); - memset(dp + ssize, fill, dsize-ssize); -#else - uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[0]&0x80)) ? 0xff : 0; - memset(dp, fill, dsize-ssize); - memcpy(dp + (dsize-ssize), sp, ssize); -#endif - } else { /* Copy LSB. */ -#if LJ_LE - memcpy(dp, sp, dsize); -#else - memcpy(dp, sp + (ssize-dsize), dsize); -#endif - } - break; - case CCX(I, F): { - double n; /* Always convert via double. */ - conv_I_F: - /* Convert source to double. */ - if (ssize == sizeof(double)) n = *(double *)sp; - else if (ssize == sizeof(float)) n = (double)*(float *)sp; - else goto err_conv; /* NYI: long double. */ - /* Then convert double to integer. */ - /* The conversion must exactly match the semantics of JIT-compiled code! */ - if (dsize < 4 || (dsize == 4 && !(dinfo & CTF_UNSIGNED))) { - int32_t i = (int32_t)n; - if (dsize == 4) *(int32_t *)dp = i; - else if (dsize == 2) *(int16_t *)dp = (int16_t)i; - else *(int8_t *)dp = (int8_t)i; - } else if (dsize == 4) { - *(uint32_t *)dp = (uint32_t)n; - } else if (dsize == 8) { - if (!(dinfo & CTF_UNSIGNED)) - *(int64_t *)dp = (int64_t)n; - else - *(uint64_t *)dp = lj_num2u64(n); - } else { - goto err_conv; /* NYI: conversion to >64 bit integers. */ - } - break; - } - case CCX(I, C): - s = ctype_child(cts, s); - sinfo = s->info; - ssize = s->size; - goto conv_I_F; /* Just convert re. */ - case CCX(I, P): - if (!(flags & CCF_CAST)) goto err_conv; - sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - goto conv_I_I; - case CCX(I, A): - if (!(flags & CCF_CAST)) goto err_conv; - sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - ssize = CTSIZE_PTR; - tmpptr = sp; - sp = (uint8_t *)&tmpptr; - goto conv_I_I; - - /* Destination is a floating-point number. */ - case CCX(F, B): - case CCX(F, I): { - double n; /* Always convert via double. */ - conv_F_I: - /* First convert source to double. */ - /* The conversion must exactly match the semantics of JIT-compiled code! */ - if (ssize < 4 || (ssize == 4 && !(sinfo & CTF_UNSIGNED))) { - int32_t i; - if (ssize == 4) { - i = *(int32_t *)sp; - } else if (!(sinfo & CTF_UNSIGNED)) { - if (ssize == 2) i = *(int16_t *)sp; - else i = *(int8_t *)sp; - } else { - if (ssize == 2) i = *(uint16_t *)sp; - else i = *(uint8_t *)sp; - } - n = (double)i; - } else if (ssize == 4) { - n = (double)*(uint32_t *)sp; - } else if (ssize == 8) { - if (!(sinfo & CTF_UNSIGNED)) n = (double)*(int64_t *)sp; - else n = (double)*(uint64_t *)sp; - } else { - goto err_conv; /* NYI: conversion from >64 bit integers. */ - } - /* Convert double to destination. */ - if (dsize == sizeof(double)) *(double *)dp = n; - else if (dsize == sizeof(float)) *(float *)dp = (float)n; - else goto err_conv; /* NYI: long double. */ - break; - } - case CCX(F, F): { - double n; /* Always convert via double. */ - conv_F_F: - if (ssize == dsize) goto copyval; - /* Convert source to double. */ - if (ssize == sizeof(double)) n = *(double *)sp; - else if (ssize == sizeof(float)) n = (double)*(float *)sp; - else goto err_conv; /* NYI: long double. */ - /* Convert double to destination. */ - if (dsize == sizeof(double)) *(double *)dp = n; - else if (dsize == sizeof(float)) *(float *)dp = (float)n; - else goto err_conv; /* NYI: long double. */ - break; - } - case CCX(F, C): - s = ctype_child(cts, s); - sinfo = s->info; - ssize = s->size; - goto conv_F_F; /* Ignore im, and convert from re. */ - - /* Destination is a complex number. */ - case CCX(C, I): - d = ctype_child(cts, d); - dinfo = d->info; - dsize = d->size; - memset(dp + dsize, 0, dsize); /* Clear im. */ - goto conv_F_I; /* Convert to re. */ - case CCX(C, F): - d = ctype_child(cts, d); - dinfo = d->info; - dsize = d->size; - memset(dp + dsize, 0, dsize); /* Clear im. */ - goto conv_F_F; /* Convert to re. */ - - case CCX(C, C): - if (dsize != ssize) { /* Different types: convert re/im separately. */ - CType *dc = ctype_child(cts, d); - CType *sc = ctype_child(cts, s); - lj_cconv_ct_ct(cts, dc, sc, dp, sp, flags); - lj_cconv_ct_ct(cts, dc, sc, dp + dc->size, sp + sc->size, flags); - return; - } - goto copyval; /* Otherwise this is easy. */ - - /* Destination is a vector. */ - case CCX(V, I): - case CCX(V, F): - case CCX(V, C): { - CType *dc = ctype_child(cts, d); - CTSize esize; - /* First convert the scalar to the first element. */ - lj_cconv_ct_ct(cts, dc, s, dp, sp, flags); - /* Then replicate it to the other elements (splat). */ - for (sp = dp, esize = dc->size; dsize > esize; dsize -= esize) { - dp += esize; - memcpy(dp, sp, esize); - } - break; - } - - case CCX(V, V): - /* Copy same-sized vectors, even for different lengths/element-types. */ - if (dsize != ssize) goto err_conv; - goto copyval; - - /* Destination is a pointer. */ - case CCX(P, I): - if (!(flags & CCF_CAST)) goto err_conv; - dinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - goto conv_I_I; - - case CCX(P, F): - if (!(flags & CCF_CAST) || !(flags & CCF_FROMTV)) goto err_conv; - /* The signed conversion is cheaper. x64 really has 47 bit pointers. */ - dinfo = CTINFO(CT_NUM, (LJ_64 && dsize == 8) ? 0 : CTF_UNSIGNED); - goto conv_I_F; - - case CCX(P, P): - if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv; - cdata_setptr(dp, dsize, cdata_getptr(sp, ssize)); - break; - - case CCX(P, A): - case CCX(P, S): - if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv; - cdata_setptr(dp, dsize, sp); - break; - - /* Destination is an array. */ - case CCX(A, A): - if ((flags & CCF_CAST) || (d->info & CTF_VLA) || dsize != ssize || - d->size == CTSIZE_INVALID || !lj_cconv_compatptr(cts, d, s, flags)) - goto err_conv; - goto copyval; - - /* Destination is a struct/union. */ - case CCX(S, S): - if ((flags & CCF_CAST) || (d->info & CTF_VLA) || d != s) - goto err_conv; /* Must be exact same type. */ -copyval: /* Copy value. */ - lua_assert(dsize == ssize); - memcpy(dp, sp, dsize); - break; - - default: - err_conv: - cconv_err_conv(cts, d, s, flags); - } -} - -/* -- C type to TValue conversion ----------------------------------------- */ - -/* Convert C type to TValue. Caveat: expects to get the raw CType! */ -int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid, - TValue *o, uint8_t *sp) -{ - CTInfo sinfo = s->info; - if (ctype_isnum(sinfo)) { - if (!ctype_isbool(sinfo)) { - if (ctype_isinteger(sinfo) && s->size > 4) goto copyval; - if (LJ_DUALNUM && ctype_isinteger(sinfo)) { - int32_t i; - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT32), s, - (uint8_t *)&i, sp, 0); - if ((sinfo & CTF_UNSIGNED) && i < 0) - setnumV(o, (lua_Number)(uint32_t)i); - else - setintV(o, i); - } else { - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_DOUBLE), s, - (uint8_t *)&o->n, sp, 0); - /* Numbers are NOT canonicalized here! Beware of uninitialized data. */ - lua_assert(tvisnum(o)); - } - } else { - uint32_t b = s->size == 1 ? (*sp != 0) : (*(int *)sp != 0); - setboolV(o, b); - setboolV(&cts->g->tmptv2, b); /* Remember for trace recorder. */ - } - return 0; - } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) { - /* Create reference. */ - setcdataV(cts->L, o, lj_cdata_newref(cts, sp, sid)); - return 1; /* Need GC step. */ - } else { - GCcdata *cd; - CTSize sz; - copyval: /* Copy value. */ - sz = s->size; - lua_assert(sz != CTSIZE_INVALID); - /* Attributes are stripped, qualifiers are kept (but mostly ignored). */ - cd = lj_cdata_new(cts, ctype_typeid(cts, s), sz); - setcdataV(cts->L, o, cd); - memcpy(cdataptr(cd), sp, sz); - return 1; /* Need GC step. */ - } -} - -/* Convert bitfield to TValue. */ -int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp) -{ - CTInfo info = s->info; - CTSize pos, bsz; - uint32_t val; - lua_assert(ctype_isbitfield(info)); - /* NYI: packed bitfields may cause misaligned reads. */ - switch (ctype_bitcsz(info)) { - case 4: val = *(uint32_t *)sp; break; - case 2: val = *(uint16_t *)sp; break; - case 1: val = *(uint8_t *)sp; break; - default: lua_assert(0); val = 0; break; - } - /* Check if a packed bitfield crosses a container boundary. */ - pos = ctype_bitpos(info); - bsz = ctype_bitbsz(info); - lua_assert(pos < 8*ctype_bitcsz(info)); - lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info)); - if (pos + bsz > 8*ctype_bitcsz(info)) - lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT); - if (!(info & CTF_BOOL)) { - CTSize shift = 32 - bsz; - if (!(info & CTF_UNSIGNED)) { - setintV(o, (int32_t)(val << (shift-pos)) >> shift); - } else { - val = (val << (shift-pos)) >> shift; - if (!LJ_DUALNUM || (int32_t)val < 0) - setnumV(o, (lua_Number)(uint32_t)val); - else - setintV(o, (int32_t)val); - } - } else { - lua_assert(bsz == 1); - setboolV(o, (val >> pos) & 1); - } - return 0; /* No GC step needed. */ -} - -/* -- TValue to C type conversion ----------------------------------------- */ - -/* Convert table to array. */ -static void cconv_array_tab(CTState *cts, CType *d, - uint8_t *dp, GCtab *t, CTInfo flags) -{ - int32_t i; - CType *dc = ctype_rawchild(cts, d); /* Array element type. */ - CTSize size = d->size, esize = dc->size, ofs = 0; - for (i = 0; ; i++) { - TValue *tv = (TValue *)lj_tab_getint(t, i); - if (!tv || tvisnil(tv)) { - if (i == 0) continue; /* Try again for 1-based tables. */ - break; /* Stop at first nil. */ - } - if (ofs >= size) - cconv_err_initov(cts, d); - lj_cconv_ct_tv(cts, dc, dp + ofs, tv, flags); - ofs += esize; - } - if (size != CTSIZE_INVALID) { /* Only fill up arrays with known size. */ - if (ofs == esize) { /* Replicate a single element. */ - for (; ofs < size; ofs += esize) memcpy(dp + ofs, dp, esize); - } else { /* Otherwise fill the remainder with zero. */ - memset(dp + ofs, 0, size - ofs); - } - } -} - -/* Convert table to sub-struct/union. */ -static void cconv_substruct_tab(CTState *cts, CType *d, uint8_t *dp, - GCtab *t, int32_t *ip, CTInfo flags) -{ - CTypeID id = d->sib; - while (id) { - CType *df = ctype_get(cts, id); - id = df->sib; - if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) { - TValue *tv; - int32_t i = *ip, iz = i; - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - if (i >= 0) { - retry: - tv = (TValue *)lj_tab_getint(t, i); - if (!tv || tvisnil(tv)) { - if (i == 0) { i = 1; goto retry; } /* 1-based tables. */ - if (iz == 0) { *ip = i = -1; goto tryname; } /* Init named fields. */ - break; /* Stop at first nil. */ - } - *ip = i + 1; - } else { - tryname: - tv = (TValue *)lj_tab_getstr(t, gco2str(gcref(df->name))); - if (!tv || tvisnil(tv)) continue; - } - if (ctype_isfield(df->info)) - lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, tv, flags); - else - lj_cconv_bf_tv(cts, df, dp+df->size, tv); - if ((d->info & CTF_UNION)) break; - } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) { - cconv_substruct_tab(cts, ctype_rawchild(cts, df), - dp+df->size, t, ip, flags); - } /* Ignore all other entries in the chain. */ - } -} - -/* Convert table to struct/union. */ -static void cconv_struct_tab(CTState *cts, CType *d, - uint8_t *dp, GCtab *t, CTInfo flags) -{ - int32_t i = 0; - memset(dp, 0, d->size); /* Much simpler to clear the struct first. */ - cconv_substruct_tab(cts, d, dp, t, &i, flags); -} - -/* Convert TValue to C type. Caveat: expects to get the raw CType! */ -void lj_cconv_ct_tv(CTState *cts, CType *d, - uint8_t *dp, TValue *o, CTInfo flags) -{ - CTypeID sid = CTID_P_VOID; - CType *s; - void *tmpptr; - uint8_t tmpbool, *sp = (uint8_t *)&tmpptr; - if (LJ_LIKELY(tvisint(o))) { - sp = (uint8_t *)&o->i; - sid = CTID_INT32; - flags |= CCF_FROMTV; - } else if (LJ_LIKELY(tvisnum(o))) { - sp = (uint8_t *)&o->n; - sid = CTID_DOUBLE; - flags |= CCF_FROMTV; - } else if (tviscdata(o)) { - sp = cdataptr(cdataV(o)); - sid = cdataV(o)->ctypeid; - s = ctype_get(cts, sid); - if (ctype_isref(s->info)) { /* Resolve reference for value. */ - lua_assert(s->size == CTSIZE_PTR); - sp = *(void **)sp; - sid = ctype_cid(s->info); - } - s = ctype_raw(cts, sid); - if (ctype_isfunc(s->info)) { - sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR); - } else { - if (ctype_isenum(s->info)) s = ctype_child(cts, s); - goto doconv; - } - } else if (tvisstr(o)) { - GCstr *str = strV(o); - if (ctype_isenum(d->info)) { /* Match string against enum constant. */ - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, d, str, &ofs); - if (!cct || !ctype_isconstval(cct->info)) - goto err_conv; - lua_assert(d->size == 4); - sp = (uint8_t *)&cct->size; - sid = ctype_cid(cct->info); - } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */ - CType *dc = ctype_rawchild(cts, d); - CTSize sz = str->len+1; - if (!ctype_isinteger(dc->info) || dc->size != 1) - goto err_conv; - if (d->size != 0 && d->size < sz) - sz = d->size; - memcpy(dp, strdata(str), sz); - return; - } else { /* Otherwise pass it as a const char[]. */ - sp = (uint8_t *)strdata(str); - sid = CTID_A_CCHAR; - flags |= CCF_FROMTV; - } - } else if (tvistab(o)) { - if (ctype_isarray(d->info)) { - cconv_array_tab(cts, d, dp, tabV(o), flags); - return; - } else if (ctype_isstruct(d->info)) { - cconv_struct_tab(cts, d, dp, tabV(o), flags); - return; - } else { - goto err_conv; - } - } else if (tvisbool(o)) { - tmpbool = boolV(o); - sp = &tmpbool; - sid = CTID_BOOL; - } else if (tvisnil(o)) { - tmpptr = (void *)0; - flags |= CCF_FROMTV; - } else if (tvisudata(o)) { - GCudata *ud = udataV(o); - tmpptr = uddata(ud); - if (ud->udtype == UDTYPE_IO_FILE) - tmpptr = *(void **)tmpptr; - } else if (tvislightud(o)) { - tmpptr = lightudV(o); - } else if (tvisfunc(o)) { - void *p = lj_ccallback_new(cts, d, funcV(o)); - if (p) { - *(void **)dp = p; - return; - } - goto err_conv; - } else { - err_conv: - cconv_err_convtv(cts, d, o, flags); - } - s = ctype_get(cts, sid); -doconv: - if (ctype_isenum(d->info)) d = ctype_child(cts, d); - lj_cconv_ct_ct(cts, d, s, dp, sp, flags); -} - -/* Convert TValue to bitfield. */ -void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o) -{ - CTInfo info = d->info; - CTSize pos, bsz; - uint32_t val, mask; - lua_assert(ctype_isbitfield(info)); - if ((info & CTF_BOOL)) { - uint8_t tmpbool; - lua_assert(ctype_bitbsz(info) == 1); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_BOOL), &tmpbool, o, 0); - val = tmpbool; - } else { - CTypeID did = (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32; - lj_cconv_ct_tv(cts, ctype_get(cts, did), (uint8_t *)&val, o, 0); - } - pos = ctype_bitpos(info); - bsz = ctype_bitbsz(info); - lua_assert(pos < 8*ctype_bitcsz(info)); - lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info)); - /* Check if a packed bitfield crosses a container boundary. */ - if (pos + bsz > 8*ctype_bitcsz(info)) - lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT); - mask = ((1u << bsz) - 1u) << pos; - val = (val << pos) & mask; - /* NYI: packed bitfields may cause misaligned reads/writes. */ - switch (ctype_bitcsz(info)) { - case 4: *(uint32_t *)dp = (*(uint32_t *)dp & ~mask) | (uint32_t)val; break; - case 2: *(uint16_t *)dp = (*(uint16_t *)dp & ~mask) | (uint16_t)val; break; - case 1: *(uint8_t *)dp = (*(uint8_t *)dp & ~mask) | (uint8_t)val; break; - default: lua_assert(0); break; - } -} - -/* -- Initialize C type with TValues -------------------------------------- */ - -/* Initialize an array with TValues. */ -static void cconv_array_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp, - TValue *o, MSize len) -{ - CType *dc = ctype_rawchild(cts, d); /* Array element type. */ - CTSize ofs, esize = dc->size; - MSize i; - if (len*esize > sz) - cconv_err_initov(cts, d); - for (i = 0, ofs = 0; i < len; i++, ofs += esize) - lj_cconv_ct_tv(cts, dc, dp + ofs, o + i, 0); - if (ofs == esize) { /* Replicate a single element. */ - for (; ofs < sz; ofs += esize) memcpy(dp + ofs, dp, esize); - } else { /* Otherwise fill the remainder with zero. */ - memset(dp + ofs, 0, sz - ofs); - } -} - -/* Initialize a sub-struct/union with TValues. */ -static void cconv_substruct_init(CTState *cts, CType *d, uint8_t *dp, - TValue *o, MSize len, MSize *ip) -{ - CTypeID id = d->sib; - while (id) { - CType *df = ctype_get(cts, id); - id = df->sib; - if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) { - MSize i = *ip; - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - if (i >= len) break; - *ip = i + 1; - if (ctype_isfield(df->info)) - lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, o + i, 0); - else - lj_cconv_bf_tv(cts, df, dp+df->size, o + i); - if ((d->info & CTF_UNION)) break; - } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) { - cconv_substruct_init(cts, ctype_rawchild(cts, df), - dp+df->size, o, len, ip); - } /* Ignore all other entries in the chain. */ - } -} - -/* Initialize a struct/union with TValues. */ -static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp, - TValue *o, MSize len) -{ - MSize i = 0; - memset(dp, 0, sz); /* Much simpler to clear the struct first. */ - cconv_substruct_init(cts, d, dp, o, len, &i); - if (i < len) - cconv_err_initov(cts, d); -} - -/* Check whether to use a multi-value initializer. -** This is true if an aggregate is to be initialized with a value. -** Valarrays are treated as values here so ct_tv handles (V|C, I|F). -*/ -int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o) -{ - if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info))) - return 0; /* Destination is not an aggregate. */ - if (tvistab(o) || (tvisstr(o) && !ctype_isstruct(d->info))) - return 0; /* Initializer is not a value. */ - if (tviscdata(o) && lj_ctype_rawref(cts, cdataV(o)->ctypeid) == d) - return 0; /* Source and destination are identical aggregates. */ - return 1; /* Otherwise the initializer is a value. */ -} - -/* Initialize C type with TValues. Caveat: expects to get the raw CType! */ -void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, - uint8_t *dp, TValue *o, MSize len) -{ - if (len == 0) - memset(dp, 0, sz); - else if (len == 1 && !lj_cconv_multi_init(cts, d, o)) - lj_cconv_ct_tv(cts, d, dp, o, 0); - else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */ - cconv_array_init(cts, d, sz, dp, o, len); - else if (ctype_isstruct(d->info)) - cconv_struct_init(cts, d, sz, dp, o, len); - else - cconv_err_initov(cts, d); -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_cconv.h b/third-party/LuaJIT-2.0.2/src/lj_cconv.h deleted file mode 100644 index 5e95d2e930..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_cconv.h +++ /dev/null @@ -1,70 +0,0 @@ -/* -** C type conversions. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CCONV_H -#define _LJ_CCONV_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* Compressed C type index. ORDER CCX. */ -enum { - CCX_B, /* Bool. */ - CCX_I, /* Integer. */ - CCX_F, /* Floating-point number. */ - CCX_C, /* Complex. */ - CCX_V, /* Vector. */ - CCX_P, /* Pointer. */ - CCX_A, /* Refarray. */ - CCX_S /* Struct/union. */ -}; - -/* Convert C type info to compressed C type index. ORDER CT. ORDER CCX. */ -static LJ_AINLINE uint32_t cconv_idx(CTInfo info) -{ - uint32_t idx = ((info >> 26) & 15u); /* Dispatch bits. */ - lua_assert(ctype_type(info) <= CT_MAYCONVERT); -#if LJ_64 - idx = ((U64x(f436fff5,fff7f021) >> 4*idx) & 15u); -#else - idx = (((idx < 8 ? 0xfff7f021u : 0xf436fff5) >> 4*(idx & 7u)) & 15u); -#endif - lua_assert(idx < 8); - return idx; -} - -#define cconv_idx2(dinfo, sinfo) \ - ((cconv_idx((dinfo)) << 3) + cconv_idx((sinfo))) - -#define CCX(dst, src) ((CCX_##dst << 3) + CCX_##src) - -/* Conversion flags. */ -#define CCF_CAST 0x00000001u -#define CCF_FROMTV 0x00000002u -#define CCF_SAME 0x00000004u -#define CCF_IGNQUAL 0x00000008u - -#define CCF_ARG_SHIFT 8 -#define CCF_ARG(n) ((n) << CCF_ARG_SHIFT) -#define CCF_GETARG(f) ((f) >> CCF_ARG_SHIFT) - -LJ_FUNC int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags); -LJ_FUNC void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s, - uint8_t *dp, uint8_t *sp, CTInfo flags); -LJ_FUNC int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid, - TValue *o, uint8_t *sp); -LJ_FUNC int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp); -LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d, - uint8_t *dp, TValue *o, CTInfo flags); -LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o); -LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o); -LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, - uint8_t *dp, TValue *o, MSize len); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_cdata.c b/third-party/LuaJIT-2.0.2/src/lj_cdata.c deleted file mode 100644 index 10f4809c11..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_cdata.c +++ /dev/null @@ -1,285 +0,0 @@ -/* -** C data management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" - -/* -- C data allocation --------------------------------------------------- */ - -/* Allocate a new C data object holding a reference to another object. */ -GCcdata *lj_cdata_newref(CTState *cts, const void *p, CTypeID id) -{ - CTypeID refid = lj_ctype_intern(cts, CTINFO_REF(id), CTSIZE_PTR); - GCcdata *cd = lj_cdata_new(cts, refid, CTSIZE_PTR); - *(const void **)cdataptr(cd) = p; - return cd; -} - -/* Allocate variable-sized or specially aligned C data object. */ -GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz, CTSize align) -{ - global_State *g; - MSize extra = sizeof(GCcdataVar) + sizeof(GCcdata) + - (align > CT_MEMALIGN ? (1u<L, extra + sz, char); - uintptr_t adata = (uintptr_t)p + sizeof(GCcdataVar) + sizeof(GCcdata); - uintptr_t almask = (1u << align) - 1u; - GCcdata *cd = (GCcdata *)(((adata + almask) & ~almask) - sizeof(GCcdata)); - lua_assert((char *)cd - p < 65536); - cdatav(cd)->offset = (uint16_t)((char *)cd - p); - cdatav(cd)->extra = extra; - cdatav(cd)->len = sz; - g = cts->g; - setgcrefr(cd->nextgc, g->gc.root); - setgcref(g->gc.root, obj2gco(cd)); - newwhite(g, obj2gco(cd)); - cd->marked |= 0x80; - cd->gct = ~LJ_TCDATA; - cd->ctypeid = id; - return cd; -} - -/* Free a C data object. */ -void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd) -{ - if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) { - GCobj *root; - makewhite(g, obj2gco(cd)); - markfinalized(obj2gco(cd)); - if ((root = gcref(g->gc.mmudata)) != NULL) { - setgcrefr(cd->nextgc, root->gch.nextgc); - setgcref(root->gch.nextgc, obj2gco(cd)); - setgcref(g->gc.mmudata, obj2gco(cd)); - } else { - setgcref(cd->nextgc, obj2gco(cd)); - setgcref(g->gc.mmudata, obj2gco(cd)); - } - } else if (LJ_LIKELY(!cdataisv(cd))) { - CType *ct = ctype_raw(ctype_ctsG(g), cd->ctypeid); - CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR; - lua_assert(ctype_hassize(ct->info) || ctype_isfunc(ct->info) || - ctype_isextern(ct->info)); - lj_mem_free(g, cd, sizeof(GCcdata) + sz); - } else { - lj_mem_free(g, memcdatav(cd), sizecdatav(cd)); - } -} - -TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd) -{ - global_State *g = G(L); - GCtab *t = ctype_ctsG(g)->finalizer; - if (gcref(t->metatable)) { - /* Add cdata to finalizer table, if still enabled. */ - TValue *tv, tmp; - setcdataV(L, &tmp, cd); - lj_gc_anybarriert(L, t); - tv = lj_tab_set(L, t, &tmp); - cd->marked |= LJ_GC_CDATA_FIN; - return tv; - } else { - /* Otherwise return dummy TValue. */ - return &g->tmptv; - } -} - -/* -- C data indexing ----------------------------------------------------- */ - -/* Index C data by a TValue. Return CType and pointer. */ -CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp, - CTInfo *qual) -{ - uint8_t *p = (uint8_t *)cdataptr(cd); - CType *ct = ctype_get(cts, cd->ctypeid); - ptrdiff_t idx; - - /* Resolve reference for cdata object. */ - if (ctype_isref(ct->info)) { - lua_assert(ct->size == CTSIZE_PTR); - p = *(uint8_t **)p; - ct = ctype_child(cts, ct); - } - -collect_attrib: - /* Skip attributes and collect qualifiers. */ - while (ctype_isattrib(ct->info)) { - if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size; - ct = ctype_child(cts, ct); - } - lua_assert(!ctype_isref(ct->info)); /* Interning rejects refs to refs. */ - - if (tvisint(key)) { - idx = (ptrdiff_t)intV(key); - goto integer_key; - } else if (tvisnum(key)) { /* Numeric key. */ - idx = LJ_64 ? (ptrdiff_t)numV(key) : (ptrdiff_t)lj_num2int(numV(key)); - integer_key: - if (ctype_ispointer(ct->info)) { - CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */ - if (sz != CTSIZE_INVALID) { - if (ctype_isptr(ct->info)) { - p = (uint8_t *)cdata_getptr(p, ct->size); - } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) { - if ((ct->info & CTF_COMPLEX)) idx &= 1; - *qual |= CTF_CONST; /* Valarray elements are constant. */ - } - *pp = p + idx*(int32_t)sz; - return ct; - } - } - } else if (tviscdata(key)) { /* Integer cdata key. */ - GCcdata *cdk = cdataV(key); - CType *ctk = ctype_raw(cts, cdk->ctypeid); - if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk); - if (ctype_isinteger(ctk->info)) { - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk, - (uint8_t *)&idx, cdataptr(cdk), 0); - goto integer_key; - } - } else if (tvisstr(key)) { /* String key. */ - GCstr *name = strV(key); - if (ctype_isstruct(ct->info)) { - CTSize ofs; - CType *fct = lj_ctype_getfieldq(cts, ct, name, &ofs, qual); - if (fct) { - *pp = p + ofs; - return fct; - } - } else if (ctype_iscomplex(ct->info)) { - if (name->len == 2) { - *qual |= CTF_CONST; /* Complex fields are constant. */ - if (strdata(name)[0] == 'r' && strdata(name)[1] == 'e') { - *pp = p; - return ct; - } else if (strdata(name)[0] == 'i' && strdata(name)[1] == 'm') { - *pp = p + (ct->size >> 1); - return ct; - } - } - } else if (cd->ctypeid == CTID_CTYPEID) { - /* Allow indexing a (pointer to) struct constructor to get constants. */ - CType *sct = ctype_raw(cts, *(CTypeID *)p); - if (ctype_isptr(sct->info)) - sct = ctype_rawchild(cts, sct); - if (ctype_isstruct(sct->info)) { - CTSize ofs; - CType *fct = lj_ctype_getfield(cts, sct, name, &ofs); - if (fct && ctype_isconstval(fct->info)) - return fct; - } - ct = sct; /* Allow resolving metamethods for constructors, too. */ - } - } - if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */ - if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) { - p = (uint8_t *)cdata_getptr(p, ct->size); - ct = ctype_child(cts, ct); - goto collect_attrib; - } - } - *qual |= 1; /* Lookup failed. */ - return ct; /* But return the resolved raw type. */ -} - -/* -- C data getters ------------------------------------------------------ */ - -/* Get constant value and convert to TValue. */ -static void cdata_getconst(CTState *cts, TValue *o, CType *ct) -{ - CType *ctt = ctype_child(cts, ct); - lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4); - /* Constants are already zero-extended/sign-extended to 32 bits. */ - if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0) - setnumV(o, (lua_Number)(uint32_t)ct->size); - else - setintV(o, (int32_t)ct->size); -} - -/* Get C data value and convert to TValue. */ -int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp) -{ - CTypeID sid; - - if (ctype_isconstval(s->info)) { - cdata_getconst(cts, o, s); - return 0; /* No GC step needed. */ - } else if (ctype_isbitfield(s->info)) { - return lj_cconv_tv_bf(cts, s, o, sp); - } - - /* Get child type of pointer/array/field. */ - lua_assert(ctype_ispointer(s->info) || ctype_isfield(s->info)); - sid = ctype_cid(s->info); - s = ctype_get(cts, sid); - - /* Resolve reference for field. */ - if (ctype_isref(s->info)) { - lua_assert(s->size == CTSIZE_PTR); - sp = *(uint8_t **)sp; - sid = ctype_cid(s->info); - s = ctype_get(cts, sid); - } - - /* Skip attributes. */ - while (ctype_isattrib(s->info)) - s = ctype_child(cts, s); - - return lj_cconv_tv_ct(cts, s, sid, o, sp); -} - -/* -- C data setters ------------------------------------------------------ */ - -/* Convert TValue and set C data value. */ -void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual) -{ - if (ctype_isconstval(d->info)) { - goto err_const; - } else if (ctype_isbitfield(d->info)) { - if (((d->info|qual) & CTF_CONST)) goto err_const; - lj_cconv_bf_tv(cts, d, dp, o); - return; - } - - /* Get child type of pointer/array/field. */ - lua_assert(ctype_ispointer(d->info) || ctype_isfield(d->info)); - d = ctype_child(cts, d); - - /* Resolve reference for field. */ - if (ctype_isref(d->info)) { - lua_assert(d->size == CTSIZE_PTR); - dp = *(uint8_t **)dp; - d = ctype_child(cts, d); - } - - /* Skip attributes and collect qualifiers. */ - for (;;) { - if (ctype_isattrib(d->info)) { - if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size; - } else { - break; - } - d = ctype_child(cts, d); - } - - lua_assert(ctype_hassize(d->info) && !ctype_isvoid(d->info)); - - if (((d->info|qual) & CTF_CONST)) { - err_const: - lj_err_caller(cts->L, LJ_ERR_FFI_WRCONST); - } - - lj_cconv_ct_tv(cts, d, dp, o, 0); -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_cdata.h b/third-party/LuaJIT-2.0.2/src/lj_cdata.h deleted file mode 100644 index 0c81b02b7f..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_cdata.h +++ /dev/null @@ -1,75 +0,0 @@ -/* -** C data management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CDATA_H -#define _LJ_CDATA_H - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* Get C data pointer. */ -static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz) -{ - if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ - return ((void *)(uintptr_t)*(uint32_t *)p); - } else { - lua_assert(sz == CTSIZE_PTR); - return *(void **)p; - } -} - -/* Set C data pointer. */ -static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v) -{ - if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ - *(uint32_t *)p = (uint32_t)(uintptr_t)v; - } else { - lua_assert(sz == CTSIZE_PTR); - *(void **)p = (void *)v; - } -} - -/* Allocate fixed-size C data object. */ -static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz) -{ - GCcdata *cd; -#ifdef LUA_USE_ASSERT - CType *ct = ctype_raw(cts, id); - lua_assert((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz); -#endif - cd = (GCcdata *)lj_mem_newgco(cts->L, sizeof(GCcdata) + sz); - cd->gct = ~LJ_TCDATA; - cd->ctypeid = ctype_check(cts, id); - return cd; -} - -/* Variant which works without a valid CTState. */ -static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz) -{ - GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz); - cd->gct = ~LJ_TCDATA; - cd->ctypeid = id; - return cd; -} - -LJ_FUNC GCcdata *lj_cdata_newref(CTState *cts, const void *pp, CTypeID id); -LJ_FUNC GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz, - CTSize align); - -LJ_FUNC void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd); -LJ_FUNCA TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd); - -LJ_FUNC CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, - uint8_t **pp, CTInfo *qual); -LJ_FUNC int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp); -LJ_FUNC void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, - CTInfo qual); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_char.c b/third-party/LuaJIT-2.0.2/src/lj_char.c deleted file mode 100644 index 11f23efe44..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_char.c +++ /dev/null @@ -1,43 +0,0 @@ -/* -** Character types. -** Donated to the public domain. -** -** This is intended to replace the problematic libc single-byte NLS functions. -** These just don't make sense anymore with UTF-8 locales becoming the norm -** on POSIX systems. It never worked too well on Windows systems since hardly -** anyone bothered to call setlocale(). -** -** This table is hardcoded for ASCII. Identifiers include the characters -** 128-255, too. This allows for the use of all non-ASCII chars as identifiers -** in the lexer. This is a broad definition, but works well in practice -** for both UTF-8 locales and most single-byte locales (such as ISO-8859-*). -** -** If you really need proper character types for UTF-8 strings, please use -** an add-on library such as slnunicode: http://luaforge.net/projects/sln/ -*/ - -#define lj_char_c -#define LUA_CORE - -#include "lj_char.h" - -LJ_DATADEF const uint8_t lj_char_bits[257] = { - 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 152,152,152,152,152,152,152,152,152,152, 4, 4, 4, 4, 4, 4, - 4,176,176,176,176,176,176,160,160,160,160,160,160,160,160,160, - 160,160,160,160,160,160,160,160,160,160,160, 4, 4, 4, 4,132, - 4,208,208,208,208,208,208,192,192,192,192,192,192,192,192,192, - 192,192,192,192,192,192,192,192,192,192,192, 4, 4, 4, 4, 1, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128 -}; - diff --git a/third-party/LuaJIT-2.0.2/src/lj_char.h b/third-party/LuaJIT-2.0.2/src/lj_char.h deleted file mode 100644 index c3c86d34b1..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_char.h +++ /dev/null @@ -1,42 +0,0 @@ -/* -** Character types. -** Donated to the public domain. -*/ - -#ifndef _LJ_CHAR_H -#define _LJ_CHAR_H - -#include "lj_def.h" - -#define LJ_CHAR_CNTRL 0x01 -#define LJ_CHAR_SPACE 0x02 -#define LJ_CHAR_PUNCT 0x04 -#define LJ_CHAR_DIGIT 0x08 -#define LJ_CHAR_XDIGIT 0x10 -#define LJ_CHAR_UPPER 0x20 -#define LJ_CHAR_LOWER 0x40 -#define LJ_CHAR_IDENT 0x80 -#define LJ_CHAR_ALPHA (LJ_CHAR_LOWER|LJ_CHAR_UPPER) -#define LJ_CHAR_ALNUM (LJ_CHAR_ALPHA|LJ_CHAR_DIGIT) -#define LJ_CHAR_GRAPH (LJ_CHAR_ALNUM|LJ_CHAR_PUNCT) - -/* Only pass -1 or 0..255 to these macros. Never pass a signed char! */ -#define lj_char_isa(c, t) ((lj_char_bits+1)[(c)] & t) -#define lj_char_iscntrl(c) lj_char_isa((c), LJ_CHAR_CNTRL) -#define lj_char_isspace(c) lj_char_isa((c), LJ_CHAR_SPACE) -#define lj_char_ispunct(c) lj_char_isa((c), LJ_CHAR_PUNCT) -#define lj_char_isdigit(c) lj_char_isa((c), LJ_CHAR_DIGIT) -#define lj_char_isxdigit(c) lj_char_isa((c), LJ_CHAR_XDIGIT) -#define lj_char_isupper(c) lj_char_isa((c), LJ_CHAR_UPPER) -#define lj_char_islower(c) lj_char_isa((c), LJ_CHAR_LOWER) -#define lj_char_isident(c) lj_char_isa((c), LJ_CHAR_IDENT) -#define lj_char_isalpha(c) lj_char_isa((c), LJ_CHAR_ALPHA) -#define lj_char_isalnum(c) lj_char_isa((c), LJ_CHAR_ALNUM) -#define lj_char_isgraph(c) lj_char_isa((c), LJ_CHAR_GRAPH) - -#define lj_char_toupper(c) ((c) - (lj_char_islower(c) >> 1)) -#define lj_char_tolower(c) ((c) + lj_char_isupper(c)) - -LJ_DATA const uint8_t lj_char_bits[257]; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_clib.c b/third-party/LuaJIT-2.0.2/src/lj_clib.c deleted file mode 100644 index 23d1f18224..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_clib.c +++ /dev/null @@ -1,412 +0,0 @@ -/* -** FFI C library loader. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_str.h" -#include "lj_udata.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" -#include "lj_clib.h" - -/* -- OS-specific functions ----------------------------------------------- */ - -#if LJ_TARGET_DLOPEN - -#include -#include - -#if defined(RTLD_DEFAULT) -#define CLIB_DEFHANDLE RTLD_DEFAULT -#elif LJ_TARGET_OSX || LJ_TARGET_BSD -#define CLIB_DEFHANDLE ((void *)(intptr_t)-2) -#else -#define CLIB_DEFHANDLE NULL -#endif - -LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L) -{ - lj_err_callermsg(L, dlerror()); -} - -#define clib_error(L, fmt, name) clib_error_(L) - -#if defined(__CYGWIN__) -#define CLIB_SOPREFIX "cyg" -#else -#define CLIB_SOPREFIX "lib" -#endif - -#if LJ_TARGET_OSX -#define CLIB_SOEXT "%s.dylib" -#elif defined(__CYGWIN__) -#define CLIB_SOEXT "%s.dll" -#else -#define CLIB_SOEXT "%s.so" -#endif - -static const char *clib_extname(lua_State *L, const char *name) -{ - if (!strchr(name, '/') -#ifdef __CYGWIN__ - && !strchr(name, '\\') -#endif - ) { - if (!strchr(name, '.')) { - name = lj_str_pushf(L, CLIB_SOEXT, name); - L->top--; -#ifdef __CYGWIN__ - } else { - return name; -#endif - } - if (!(name[0] == CLIB_SOPREFIX[0] && name[1] == CLIB_SOPREFIX[1] && - name[2] == CLIB_SOPREFIX[2])) { - name = lj_str_pushf(L, CLIB_SOPREFIX "%s", name); - L->top--; - } - } - return name; -} - -/* Check for a recognized ld script line. */ -static const char *clib_check_lds(lua_State *L, const char *buf) -{ - char *p, *e; - if ((!strncmp(buf, "GROUP", 5) || !strncmp(buf, "INPUT", 5)) && - (p = strchr(buf, '('))) { - while (*++p == ' ') ; - for (e = p; *e && *e != ' ' && *e != ')'; e++) ; - return strdata(lj_str_new(L, p, e-p)); - } - return NULL; -} - -/* Quick and dirty solution to resolve shared library name from ld script. */ -static const char *clib_resolve_lds(lua_State *L, const char *name) -{ - FILE *fp = fopen(name, "r"); - const char *p = NULL; - if (fp) { - char buf[256]; - if (fgets(buf, sizeof(buf), fp)) { - if (!strncmp(buf, "/* GNU ld script", 16)) { /* ld script magic? */ - while (fgets(buf, sizeof(buf), fp)) { /* Check all lines. */ - p = clib_check_lds(L, buf); - if (p) break; - } - } else { /* Otherwise check only the first line. */ - p = clib_check_lds(L, buf); - } - } - fclose(fp); - } - return p; -} - -static void *clib_loadlib(lua_State *L, const char *name, int global) -{ - void *h = dlopen(clib_extname(L, name), - RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL)); - if (!h) { - const char *e, *err = dlerror(); - if (*err == '/' && (e = strchr(err, ':')) && - (name = clib_resolve_lds(L, strdata(lj_str_new(L, err, e-err))))) { - h = dlopen(name, RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL)); - if (h) return h; - err = dlerror(); - } - lj_err_callermsg(L, err); - } - return h; -} - -static void clib_unloadlib(CLibrary *cl) -{ - if (cl->handle && cl->handle != CLIB_DEFHANDLE) - dlclose(cl->handle); -} - -static void *clib_getsym(CLibrary *cl, const char *name) -{ - void *p = dlsym(cl->handle, name); - return p; -} - -#elif LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#ifndef WINVER -#define WINVER 0x0500 -#endif -#include - -#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS -#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4 -#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2 -BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*); -#endif - -#define CLIB_DEFHANDLE ((void *)-1) - -/* Default libraries. */ -enum { - CLIB_HANDLE_EXE, - CLIB_HANDLE_DLL, - CLIB_HANDLE_CRT, - CLIB_HANDLE_KERNEL32, - CLIB_HANDLE_USER32, - CLIB_HANDLE_GDI32, - CLIB_HANDLE_MAX -}; - -static void *clib_def_handle[CLIB_HANDLE_MAX]; - -LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt, - const char *name) -{ - DWORD err = GetLastError(); - char buf[128]; - if (!FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, - NULL, err, 0, buf, sizeof(buf), NULL)) - buf[0] = '\0'; - lj_err_callermsg(L, lj_str_pushf(L, fmt, name, buf)); -} - -static int clib_needext(const char *s) -{ - while (*s) { - if (*s == '/' || *s == '\\' || *s == '.') return 0; - s++; - } - return 1; -} - -static const char *clib_extname(lua_State *L, const char *name) -{ - if (clib_needext(name)) { - name = lj_str_pushf(L, "%s.dll", name); - L->top--; - } - return name; -} - -static void *clib_loadlib(lua_State *L, const char *name, int global) -{ - DWORD oldwerr = GetLastError(); - void *h = (void *)LoadLibraryA(clib_extname(L, name)); - if (!h) clib_error(L, "cannot load module " LUA_QS ": %s", name); - SetLastError(oldwerr); - UNUSED(global); - return h; -} - -static void clib_unloadlib(CLibrary *cl) -{ - if (cl->handle == CLIB_DEFHANDLE) { - MSize i; - for (i = CLIB_HANDLE_KERNEL32; i < CLIB_HANDLE_MAX; i++) { - void *h = clib_def_handle[i]; - if (h) { - clib_def_handle[i] = NULL; - FreeLibrary((HINSTANCE)h); - } - } - } else if (!cl->handle) { - FreeLibrary((HINSTANCE)cl->handle); - } -} - -static void *clib_getsym(CLibrary *cl, const char *name) -{ - void *p = NULL; - if (cl->handle == CLIB_DEFHANDLE) { /* Search default libraries. */ - MSize i; - for (i = 0; i < CLIB_HANDLE_MAX; i++) { - HINSTANCE h = (HINSTANCE)clib_def_handle[i]; - if (!(void *)h) { /* Resolve default library handles (once). */ - switch (i) { - case CLIB_HANDLE_EXE: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &h); break; - case CLIB_HANDLE_DLL: - GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (const char *)clib_def_handle, &h); - break; - case CLIB_HANDLE_CRT: - GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (const char *)&_fmode, &h); - break; - case CLIB_HANDLE_KERNEL32: h = LoadLibraryA("kernel32.dll"); break; - case CLIB_HANDLE_USER32: h = LoadLibraryA("user32.dll"); break; - case CLIB_HANDLE_GDI32: h = LoadLibraryA("gdi32.dll"); break; - } - if (!h) continue; - clib_def_handle[i] = (void *)h; - } - p = (void *)GetProcAddress(h, name); - if (p) break; - } - } else { - p = (void *)GetProcAddress((HINSTANCE)cl->handle, name); - } - return p; -} - -#else - -#define CLIB_DEFHANDLE NULL - -LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt, - const char *name) -{ - lj_err_callermsg(L, lj_str_pushf(L, fmt, name, "no support for this OS")); -} - -static void *clib_loadlib(lua_State *L, const char *name, int global) -{ - lj_err_callermsg(L, "no support for loading dynamic libraries for this OS"); - UNUSED(name); UNUSED(global); - return NULL; -} - -static void clib_unloadlib(CLibrary *cl) -{ - UNUSED(cl); -} - -static void *clib_getsym(CLibrary *cl, const char *name) -{ - UNUSED(cl); UNUSED(name); - return NULL; -} - -#endif - -/* -- C library indexing -------------------------------------------------- */ - -#if LJ_TARGET_X86 && LJ_ABI_WIN -/* Compute argument size for fastcall/stdcall functions. */ -static CTSize clib_func_argsize(CTState *cts, CType *ct) -{ - CTSize n = 0; - while (ct->sib) { - CType *d; - ct = ctype_get(cts, ct->sib); - if (ctype_isfield(ct->info)) { - d = ctype_rawchild(cts, ct); - n += ((d->size + 3) & ~3); - } - } - return n; -} -#endif - -/* Get redirected or mangled external symbol. */ -static const char *clib_extsym(CTState *cts, CType *ct, GCstr *name) -{ - if (ct->sib) { - CType *ctf = ctype_get(cts, ct->sib); - if (ctype_isxattrib(ctf->info, CTA_REDIR)) - return strdata(gco2str(gcref(ctf->name))); - } - return strdata(name); -} - -/* Index a C library by name. */ -TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name) -{ - TValue *tv = lj_tab_setstr(L, cl->cache, name); - if (LJ_UNLIKELY(tvisnil(tv))) { - CTState *cts = ctype_cts(L); - CType *ct; - CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX); - if (!id) - lj_err_callerv(L, LJ_ERR_FFI_NODECL, strdata(name)); - if (ctype_isconstval(ct->info)) { - CType *ctt = ctype_child(cts, ct); - lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4); - if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0) - setnumV(tv, (lua_Number)(uint32_t)ct->size); - else - setintV(tv, (int32_t)ct->size); - } else { - const char *sym = clib_extsym(cts, ct, name); -#if LJ_TARGET_WINDOWS - DWORD oldwerr = GetLastError(); -#endif - void *p = clib_getsym(cl, sym); - GCcdata *cd; - lua_assert(ctype_isfunc(ct->info) || ctype_isextern(ct->info)); -#if LJ_TARGET_X86 && LJ_ABI_WIN - /* Retry with decorated name for fastcall/stdcall functions. */ - if (!p && ctype_isfunc(ct->info)) { - CTInfo cconv = ctype_cconv(ct->info); - if (cconv == CTCC_FASTCALL || cconv == CTCC_STDCALL) { - CTSize sz = clib_func_argsize(cts, ct); - const char *symd = lj_str_pushf(L, - cconv == CTCC_FASTCALL ? "@%s@%d" : "_%s@%d", - sym, sz); - L->top--; - p = clib_getsym(cl, symd); - } - } -#endif - if (!p) - clib_error(L, "cannot resolve symbol " LUA_QS ": %s", sym); -#if LJ_TARGET_WINDOWS - SetLastError(oldwerr); -#endif - cd = lj_cdata_new(cts, id, CTSIZE_PTR); - *(void **)cdataptr(cd) = p; - setcdataV(L, tv, cd); - } - } - return tv; -} - -/* -- C library management ------------------------------------------------ */ - -/* Create a new CLibrary object and push it on the stack. */ -static CLibrary *clib_new(lua_State *L, GCtab *mt) -{ - GCtab *t = lj_tab_new(L, 0, 0); - GCudata *ud = lj_udata_new(L, sizeof(CLibrary), t); - CLibrary *cl = (CLibrary *)uddata(ud); - cl->cache = t; - ud->udtype = UDTYPE_FFI_CLIB; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcref(ud->metatable, obj2gco(mt)); - setudataV(L, L->top++, ud); - return cl; -} - -/* Load a C library. */ -void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global) -{ - void *handle = clib_loadlib(L, strdata(name), global); - CLibrary *cl = clib_new(L, mt); - cl->handle = handle; -} - -/* Unload a C library. */ -void lj_clib_unload(CLibrary *cl) -{ - clib_unloadlib(cl); - cl->handle = NULL; -} - -/* Create the default C library object. */ -void lj_clib_default(lua_State *L, GCtab *mt) -{ - CLibrary *cl = clib_new(L, mt); - cl->handle = CLIB_DEFHANDLE; -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_clib.h b/third-party/LuaJIT-2.0.2/src/lj_clib.h deleted file mode 100644 index 421388adeb..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_clib.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -** FFI C library loader. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CLIB_H -#define _LJ_CLIB_H - -#include "lj_obj.h" - -#if LJ_HASFFI - -/* Namespace for C library indexing. */ -#define CLNS_INDEX ((1u<env. */ -} CLibrary; - -LJ_FUNC TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name); -LJ_FUNC void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global); -LJ_FUNC void lj_clib_unload(CLibrary *cl); -LJ_FUNC void lj_clib_default(lua_State *L, GCtab *mt); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_cparse.c b/third-party/LuaJIT-2.0.2/src/lj_cparse.c deleted file mode 100644 index 107c0381d4..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_cparse.c +++ /dev/null @@ -1,1872 +0,0 @@ -/* -** C declaration parser. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_ctype.h" -#include "lj_cparse.h" -#include "lj_frame.h" -#include "lj_vm.h" -#include "lj_char.h" -#include "lj_strscan.h" - -/* -** Important note: this is NOT a validating C parser! This is a minimal -** C declaration parser, solely for use by the LuaJIT FFI. -** -** It ought to return correct results for properly formed C declarations, -** but it may accept some invalid declarations, too (and return nonsense). -** Also, it shows rather generic error messages to avoid unnecessary bloat. -** If in doubt, please check the input against your favorite C compiler. -*/ - -/* -- C lexer ------------------------------------------------------------- */ - -/* C lexer token names. */ -static const char *const ctoknames[] = { -#define CTOKSTR(name, str) str, -CTOKDEF(CTOKSTR) -#undef CTOKSTR - NULL -}; - -/* Forward declaration. */ -LJ_NORET static void cp_err(CPState *cp, ErrMsg em); - -static const char *cp_tok2str(CPState *cp, CPToken tok) -{ - lua_assert(tok < CTOK_FIRSTDECL); - if (tok > CTOK_OFS) - return ctoknames[tok-CTOK_OFS-1]; - else if (!lj_char_iscntrl(tok)) - return lj_str_pushf(cp->L, "%c", tok); - else - return lj_str_pushf(cp->L, "char(%d)", tok); -} - -/* End-of-line? */ -static LJ_AINLINE int cp_iseol(CPChar c) -{ - return (c == '\n' || c == '\r'); -} - -static LJ_AINLINE CPChar cp_get(CPState *cp); - -/* Peek next raw character. */ -static LJ_AINLINE CPChar cp_rawpeek(CPState *cp) -{ - return (CPChar)(uint8_t)(*cp->p); -} - -/* Transparently skip backslash-escaped line breaks. */ -static LJ_NOINLINE CPChar cp_get_bs(CPState *cp) -{ - CPChar c2, c = cp_rawpeek(cp); - if (!cp_iseol(c)) return cp->c; - cp->p++; - c2 = cp_rawpeek(cp); - if (cp_iseol(c2) && c2 != c) cp->p++; - cp->linenumber++; - return cp_get(cp); -} - -/* Get next character. */ -static LJ_AINLINE CPChar cp_get(CPState *cp) -{ - cp->c = (CPChar)(uint8_t)(*cp->p++); - if (LJ_LIKELY(cp->c != '\\')) return cp->c; - return cp_get_bs(cp); -} - -/* Grow save buffer. */ -static LJ_NOINLINE void cp_save_grow(CPState *cp, CPChar c) -{ - MSize newsize; - if (cp->sb.sz >= CPARSE_MAX_BUF/2) - cp_err(cp, LJ_ERR_XELEM); - newsize = cp->sb.sz * 2; - lj_str_resizebuf(cp->L, &cp->sb, newsize); - cp->sb.buf[cp->sb.n++] = (char)c; -} - -/* Save character in buffer. */ -static LJ_AINLINE void cp_save(CPState *cp, CPChar c) -{ - if (LJ_UNLIKELY(cp->sb.n + 1 > cp->sb.sz)) - cp_save_grow(cp, c); - else - cp->sb.buf[cp->sb.n++] = (char)c; -} - -/* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ -static void cp_newline(CPState *cp) -{ - CPChar c = cp_rawpeek(cp); - if (cp_iseol(c) && c != cp->c) cp->p++; - cp->linenumber++; -} - -LJ_NORET static void cp_errmsg(CPState *cp, CPToken tok, ErrMsg em, ...) -{ - const char *msg, *tokstr; - lua_State *L; - va_list argp; - if (tok == 0) { - tokstr = NULL; - } else if (tok == CTOK_IDENT || tok == CTOK_INTEGER || tok == CTOK_STRING || - tok >= CTOK_FIRSTDECL) { - if (cp->sb.n == 0) cp_save(cp, '$'); - cp_save(cp, '\0'); - tokstr = cp->sb.buf; - } else { - tokstr = cp_tok2str(cp, tok); - } - L = cp->L; - va_start(argp, em); - msg = lj_str_pushvf(L, err2msg(em), argp); - va_end(argp); - if (tokstr) - msg = lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tokstr); - if (cp->linenumber > 1) - msg = lj_str_pushf(L, "%s at line %d", msg, cp->linenumber); - lj_err_callermsg(L, msg); -} - -LJ_NORET LJ_NOINLINE static void cp_err_token(CPState *cp, CPToken tok) -{ - cp_errmsg(cp, cp->tok, LJ_ERR_XTOKEN, cp_tok2str(cp, tok)); -} - -LJ_NORET LJ_NOINLINE static void cp_err_badidx(CPState *cp, CType *ct) -{ - GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); - cp_errmsg(cp, 0, LJ_ERR_FFI_BADIDX, strdata(s)); -} - -LJ_NORET LJ_NOINLINE static void cp_err(CPState *cp, ErrMsg em) -{ - cp_errmsg(cp, 0, em); -} - -/* -- Main lexical scanner ------------------------------------------------ */ - -/* Parse number literal. Only handles int32_t/uint32_t right now. */ -static CPToken cp_number(CPState *cp) -{ - StrScanFmt fmt; - TValue o; - do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp))); - cp_save(cp, '\0'); - fmt = lj_strscan_scan((const uint8_t *)cp->sb.buf, &o, STRSCAN_OPT_C); - if (fmt == STRSCAN_INT) cp->val.id = CTID_INT32; - else if (fmt == STRSCAN_U32) cp->val.id = CTID_UINT32; - else if (!(cp->mode & CPARSE_MODE_SKIP)) - cp_errmsg(cp, CTOK_INTEGER, LJ_ERR_XNUMBER); - cp->val.u32 = (uint32_t)o.i; - return CTOK_INTEGER; -} - -/* Parse identifier or keyword. */ -static CPToken cp_ident(CPState *cp) -{ - do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp))); - cp->str = lj_str_new(cp->L, cp->sb.buf, cp->sb.n); - cp->val.id = lj_ctype_getname(cp->cts, &cp->ct, cp->str, cp->tmask); - if (ctype_type(cp->ct->info) == CT_KW) - return ctype_cid(cp->ct->info); - return CTOK_IDENT; -} - -/* Parse parameter. */ -static CPToken cp_param(CPState *cp) -{ - CPChar c = cp_get(cp); - TValue *o = cp->param; - if (lj_char_isident(c) || c == '$') /* Reserve $xyz for future extensions. */ - cp_errmsg(cp, c, LJ_ERR_XSYNTAX); - if (!o || o >= cp->L->top) - cp_err(cp, LJ_ERR_FFI_NUMPARAM); - cp->param = o+1; - if (tvisstr(o)) { - cp->str = strV(o); - cp->val.id = 0; - cp->ct = &cp->cts->tab[0]; - return CTOK_IDENT; - } else if (tvisnumber(o)) { - cp->val.i32 = numberVint(o); - cp->val.id = CTID_INT32; - return CTOK_INTEGER; - } else { - GCcdata *cd; - if (!tviscdata(o)) - lj_err_argtype(cp->L, (int)(o-cp->L->base)+1, "type parameter"); - cd = cdataV(o); - if (cd->ctypeid == CTID_CTYPEID) - cp->val.id = *(CTypeID *)cdataptr(cd); - else - cp->val.id = cd->ctypeid; - return '$'; - } -} - -/* Parse string or character constant. */ -static CPToken cp_string(CPState *cp) -{ - CPChar delim = cp->c; - cp_get(cp); - while (cp->c != delim) { - CPChar c = cp->c; - if (c == '\0') cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); - if (c == '\\') { - c = cp_get(cp); - switch (c) { - case '\0': cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); break; - case 'a': c = '\a'; break; - case 'b': c = '\b'; break; - case 'f': c = '\f'; break; - case 'n': c = '\n'; break; - case 'r': c = '\r'; break; - case 't': c = '\t'; break; - case 'v': c = '\v'; break; - case 'e': c = 27; break; - case 'x': - c = 0; - while (lj_char_isxdigit(cp_get(cp))) - c = (c<<4) + (lj_char_isdigit(cp->c) ? cp->c-'0' : (cp->c&15)+9); - cp_save(cp, (c & 0xff)); - continue; - default: - if (lj_char_isdigit(c)) { - c -= '0'; - if (lj_char_isdigit(cp_get(cp))) { - c = c*8 + (cp->c - '0'); - if (lj_char_isdigit(cp_get(cp))) { - c = c*8 + (cp->c - '0'); - cp_get(cp); - } - } - cp_save(cp, (c & 0xff)); - continue; - } - break; - } - } - cp_save(cp, c); - cp_get(cp); - } - cp_get(cp); - if (delim == '"') { - cp->str = lj_str_new(cp->L, cp->sb.buf, cp->sb.n); - return CTOK_STRING; - } else { - if (cp->sb.n != 1) cp_err_token(cp, '\''); - cp->val.i32 = (int32_t)(char)cp->sb.buf[0]; - cp->val.id = CTID_INT32; - return CTOK_INTEGER; - } -} - -/* Skip C comment. */ -static void cp_comment_c(CPState *cp) -{ - do { - if (cp_get(cp) == '*') { - do { - if (cp_get(cp) == '/') { cp_get(cp); return; } - } while (cp->c == '*'); - } - if (cp_iseol(cp->c)) cp_newline(cp); - } while (cp->c != '\0'); -} - -/* Skip C++ comment. */ -static void cp_comment_cpp(CPState *cp) -{ - while (!cp_iseol(cp_get(cp)) && cp->c != '\0') - ; -} - -/* Lexical scanner for C. Only a minimal subset is implemented. */ -static CPToken cp_next_(CPState *cp) -{ - lj_str_resetbuf(&cp->sb); - for (;;) { - if (lj_char_isident(cp->c)) - return lj_char_isdigit(cp->c) ? cp_number(cp) : cp_ident(cp); - switch (cp->c) { - case '\n': case '\r': cp_newline(cp); /* fallthrough. */ - case ' ': case '\t': case '\v': case '\f': cp_get(cp); break; - case '"': case '\'': return cp_string(cp); - case '/': - if (cp_get(cp) == '*') cp_comment_c(cp); - else if (cp->c == '/') cp_comment_cpp(cp); - else return '/'; - break; - case '|': - if (cp_get(cp) != '|') return '|'; cp_get(cp); return CTOK_OROR; - case '&': - if (cp_get(cp) != '&') return '&'; cp_get(cp); return CTOK_ANDAND; - case '=': - if (cp_get(cp) != '=') return '='; cp_get(cp); return CTOK_EQ; - case '!': - if (cp_get(cp) != '=') return '!'; cp_get(cp); return CTOK_NE; - case '<': - if (cp_get(cp) == '=') { cp_get(cp); return CTOK_LE; } - else if (cp->c == '<') { cp_get(cp); return CTOK_SHL; } - return '<'; - case '>': - if (cp_get(cp) == '=') { cp_get(cp); return CTOK_GE; } - else if (cp->c == '>') { cp_get(cp); return CTOK_SHR; } - return '>'; - case '-': - if (cp_get(cp) != '>') return '-'; cp_get(cp); return CTOK_DEREF; - case '$': - return cp_param(cp); - case '\0': return CTOK_EOF; - default: { CPToken c = cp->c; cp_get(cp); return c; } - } - } -} - -static LJ_NOINLINE CPToken cp_next(CPState *cp) -{ - return (cp->tok = cp_next_(cp)); -} - -/* -- C parser ------------------------------------------------------------ */ - -/* Namespaces for resolving identifiers. */ -#define CPNS_DEFAULT \ - ((1u<linenumber = 1; - cp->depth = 0; - cp->curpack = 0; - cp->packstack[0] = 255; - lj_str_initbuf(&cp->sb); - lj_str_resizebuf(cp->L, &cp->sb, LJ_MIN_SBUF); - lua_assert(cp->p != NULL); - cp_get(cp); /* Read-ahead first char. */ - cp->tok = 0; - cp->tmask = CPNS_DEFAULT; - cp_next(cp); /* Read-ahead first token. */ -} - -/* Cleanup C parser state. */ -static void cp_cleanup(CPState *cp) -{ - global_State *g = G(cp->L); - lj_str_freebuf(g, &cp->sb); -} - -/* Check and consume optional token. */ -static int cp_opt(CPState *cp, CPToken tok) -{ - if (cp->tok == tok) { cp_next(cp); return 1; } - return 0; -} - -/* Check and consume token. */ -static void cp_check(CPState *cp, CPToken tok) -{ - if (cp->tok != tok) cp_err_token(cp, tok); - cp_next(cp); -} - -/* Check if the next token may start a type declaration. */ -static int cp_istypedecl(CPState *cp) -{ - if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECL) return 1; - if (cp->tok == CTOK_IDENT && ctype_istypedef(cp->ct->info)) return 1; - if (cp->tok == '$') return 1; - return 0; -} - -/* -- Constant expression evaluator --------------------------------------- */ - -/* Forward declarations. */ -static void cp_expr_unary(CPState *cp, CPValue *k); -static void cp_expr_sub(CPState *cp, CPValue *k, int pri); - -/* Please note that type handling is very weak here. Most ops simply -** assume integer operands. Accessors are only needed to compute types and -** return synthetic values. The only purpose of the expression evaluator -** is to compute the values of constant expressions one would typically -** find in C header files. And again: this is NOT a validating C parser! -*/ - -/* Parse comma separated expression and return last result. */ -static void cp_expr_comma(CPState *cp, CPValue *k) -{ - do { cp_expr_sub(cp, k, 0); } while (cp_opt(cp, ',')); -} - -/* Parse sizeof/alignof operator. */ -static void cp_expr_sizeof(CPState *cp, CPValue *k, int wantsz) -{ - CTSize sz; - CTInfo info; - if (cp_opt(cp, '(')) { - if (cp_istypedecl(cp)) - k->id = cp_decl_abstract(cp); - else - cp_expr_comma(cp, k); - cp_check(cp, ')'); - } else { - cp_expr_unary(cp, k); - } - info = lj_ctype_info(cp->cts, k->id, &sz); - if (wantsz) { - if (sz != CTSIZE_INVALID) - k->u32 = sz; - else if (k->id != CTID_A_CCHAR) /* Special case for sizeof("string"). */ - cp_err(cp, LJ_ERR_FFI_INVSIZE); - } else { - k->u32 = 1u << ctype_align(info); - } - k->id = CTID_UINT32; /* Really size_t. */ -} - -/* Parse prefix operators. */ -static void cp_expr_prefix(CPState *cp, CPValue *k) -{ - if (cp->tok == CTOK_INTEGER) { - *k = cp->val; cp_next(cp); - } else if (cp_opt(cp, '+')) { - cp_expr_unary(cp, k); /* Nothing to do (well, integer promotion). */ - } else if (cp_opt(cp, '-')) { - cp_expr_unary(cp, k); k->i32 = -k->i32; - } else if (cp_opt(cp, '~')) { - cp_expr_unary(cp, k); k->i32 = ~k->i32; - } else if (cp_opt(cp, '!')) { - cp_expr_unary(cp, k); k->i32 = !k->i32; k->id = CTID_INT32; - } else if (cp_opt(cp, '(')) { - if (cp_istypedecl(cp)) { /* Cast operator. */ - CTypeID id = cp_decl_abstract(cp); - cp_check(cp, ')'); - cp_expr_unary(cp, k); - k->id = id; /* No conversion performed. */ - } else { /* Sub-expression. */ - cp_expr_comma(cp, k); - cp_check(cp, ')'); - } - } else if (cp_opt(cp, '*')) { /* Indirection. */ - CType *ct; - cp_expr_unary(cp, k); - ct = lj_ctype_rawref(cp->cts, k->id); - if (!ctype_ispointer(ct->info)) - cp_err_badidx(cp, ct); - k->u32 = 0; k->id = ctype_cid(ct->info); - } else if (cp_opt(cp, '&')) { /* Address operator. */ - cp_expr_unary(cp, k); - k->id = lj_ctype_intern(cp->cts, CTINFO(CT_PTR, CTALIGN_PTR+k->id), - CTSIZE_PTR); - } else if (cp_opt(cp, CTOK_SIZEOF)) { - cp_expr_sizeof(cp, k, 1); - } else if (cp_opt(cp, CTOK_ALIGNOF)) { - cp_expr_sizeof(cp, k, 0); - } else if (cp->tok == CTOK_IDENT) { - if (ctype_type(cp->ct->info) == CT_CONSTVAL) { - k->u32 = cp->ct->size; k->id = ctype_cid(cp->ct->info); - } else if (ctype_type(cp->ct->info) == CT_EXTERN) { - k->u32 = cp->val.id; k->id = ctype_cid(cp->ct->info); - } else if (ctype_type(cp->ct->info) == CT_FUNC) { - k->u32 = cp->val.id; k->id = cp->val.id; - } else { - goto err_expr; - } - cp_next(cp); - } else if (cp->tok == CTOK_STRING) { - CTSize sz = cp->str->len; - while (cp_next(cp) == CTOK_STRING) - sz += cp->str->len; - k->u32 = sz + 1; - k->id = CTID_A_CCHAR; - } else { - err_expr: - cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); - } -} - -/* Parse postfix operators. */ -static void cp_expr_postfix(CPState *cp, CPValue *k) -{ - for (;;) { - CType *ct; - if (cp_opt(cp, '[')) { /* Array/pointer index. */ - CPValue k2; - cp_expr_comma(cp, &k2); - ct = lj_ctype_rawref(cp->cts, k->id); - if (!ctype_ispointer(ct->info)) { - ct = lj_ctype_rawref(cp->cts, k2.id); - if (!ctype_ispointer(ct->info)) - cp_err_badidx(cp, ct); - } - cp_check(cp, ']'); - k->u32 = 0; - } else if (cp->tok == '.' || cp->tok == CTOK_DEREF) { /* Struct deref. */ - CTSize ofs; - CType *fct; - ct = lj_ctype_rawref(cp->cts, k->id); - if (cp->tok == CTOK_DEREF) { - if (!ctype_ispointer(ct->info)) - cp_err_badidx(cp, ct); - ct = lj_ctype_rawref(cp->cts, ctype_cid(ct->info)); - } - cp_next(cp); - if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); - if (!ctype_isstruct(ct->info) || ct->size == CTSIZE_INVALID || - !(fct = lj_ctype_getfield(cp->cts, ct, cp->str, &ofs)) || - ctype_isbitfield(fct->info)) { - GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); - cp_errmsg(cp, 0, LJ_ERR_FFI_BADMEMBER, strdata(s), strdata(cp->str)); - } - ct = fct; - k->u32 = ctype_isconstval(ct->info) ? ct->size : 0; - cp_next(cp); - } else { - return; - } - k->id = ctype_cid(ct->info); - } -} - -/* Parse infix operators. */ -static void cp_expr_infix(CPState *cp, CPValue *k, int pri) -{ - CPValue k2; - k2.u32 = 0; k2.id = 0; /* Silence the compiler. */ - for (;;) { - switch (pri) { - case 0: - if (cp_opt(cp, '?')) { - CPValue k3; - cp_expr_comma(cp, &k2); /* Right-associative. */ - cp_check(cp, ':'); - cp_expr_sub(cp, &k3, 0); - k->u32 = k->u32 ? k2.u32 : k3.u32; - k->id = k2.id > k3.id ? k2.id : k3.id; - continue; - } - case 1: - if (cp_opt(cp, CTOK_OROR)) { - cp_expr_sub(cp, &k2, 2); k->i32 = k->u32 || k2.u32; k->id = CTID_INT32; - continue; - } - case 2: - if (cp_opt(cp, CTOK_ANDAND)) { - cp_expr_sub(cp, &k2, 3); k->i32 = k->u32 && k2.u32; k->id = CTID_INT32; - continue; - } - case 3: - if (cp_opt(cp, '|')) { - cp_expr_sub(cp, &k2, 4); k->u32 = k->u32 | k2.u32; goto arith_result; - } - case 4: - if (cp_opt(cp, '^')) { - cp_expr_sub(cp, &k2, 5); k->u32 = k->u32 ^ k2.u32; goto arith_result; - } - case 5: - if (cp_opt(cp, '&')) { - cp_expr_sub(cp, &k2, 6); k->u32 = k->u32 & k2.u32; goto arith_result; - } - case 6: - if (cp_opt(cp, CTOK_EQ)) { - cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 == k2.u32; k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, CTOK_NE)) { - cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 != k2.u32; k->id = CTID_INT32; - continue; - } - case 7: - if (cp_opt(cp, '<')) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 < k2.i32; - else - k->i32 = k->u32 < k2.u32; - k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, '>')) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 > k2.i32; - else - k->i32 = k->u32 > k2.u32; - k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, CTOK_LE)) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 <= k2.i32; - else - k->i32 = k->u32 <= k2.u32; - k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, CTOK_GE)) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 >= k2.i32; - else - k->i32 = k->u32 >= k2.u32; - k->id = CTID_INT32; - continue; - } - case 8: - if (cp_opt(cp, CTOK_SHL)) { - cp_expr_sub(cp, &k2, 9); k->u32 = k->u32 << k2.u32; - continue; - } else if (cp_opt(cp, CTOK_SHR)) { - cp_expr_sub(cp, &k2, 9); - if (k->id == CTID_INT32) - k->i32 = k->i32 >> k2.i32; - else - k->u32 = k->u32 >> k2.u32; - continue; - } - case 9: - if (cp_opt(cp, '+')) { - cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 + k2.u32; - arith_result: - if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ - continue; - } else if (cp_opt(cp, '-')) { - cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 - k2.u32; goto arith_result; - } - case 10: - if (cp_opt(cp, '*')) { - cp_expr_unary(cp, &k2); k->u32 = k->u32 * k2.u32; goto arith_result; - } else if (cp_opt(cp, '/')) { - cp_expr_unary(cp, &k2); - if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ - if (k2.u32 == 0 || - (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) - cp_err(cp, LJ_ERR_BADVAL); - if (k->id == CTID_INT32) - k->i32 = k->i32 / k2.i32; - else - k->u32 = k->u32 / k2.u32; - continue; - } else if (cp_opt(cp, '%')) { - cp_expr_unary(cp, &k2); - if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ - if (k2.u32 == 0 || - (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) - cp_err(cp, LJ_ERR_BADVAL); - if (k->id == CTID_INT32) - k->i32 = k->i32 % k2.i32; - else - k->u32 = k->u32 % k2.u32; - continue; - } - default: - return; - } - } -} - -/* Parse and evaluate unary expression. */ -static void cp_expr_unary(CPState *cp, CPValue *k) -{ - if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); - cp_expr_prefix(cp, k); - cp_expr_postfix(cp, k); - cp->depth--; -} - -/* Parse and evaluate sub-expression. */ -static void cp_expr_sub(CPState *cp, CPValue *k, int pri) -{ - cp_expr_unary(cp, k); - cp_expr_infix(cp, k, pri); -} - -/* Parse constant integer expression. */ -static void cp_expr_kint(CPState *cp, CPValue *k) -{ - CType *ct; - cp_expr_sub(cp, k, 0); - ct = ctype_raw(cp->cts, k->id); - if (!ctype_isinteger(ct->info)) cp_err(cp, LJ_ERR_BADVAL); -} - -/* Parse (non-negative) size expression. */ -static CTSize cp_expr_ksize(CPState *cp) -{ - CPValue k; - cp_expr_kint(cp, &k); - if (k.u32 >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); - return k.u32; -} - -/* -- Type declaration stack management ----------------------------------- */ - -/* Add declaration element behind the insertion position. */ -static CPDeclIdx cp_add(CPDecl *decl, CTInfo info, CTSize size) -{ - CPDeclIdx top = decl->top; - if (top >= CPARSE_MAX_DECLSTACK) cp_err(decl->cp, LJ_ERR_XLEVELS); - decl->stack[top].info = info; - decl->stack[top].size = size; - decl->stack[top].sib = 0; - setgcrefnull(decl->stack[top].name); - decl->stack[top].next = decl->stack[decl->pos].next; - decl->stack[decl->pos].next = (CTypeID1)top; - decl->top = top+1; - return top; -} - -/* Push declaration element before the insertion position. */ -static CPDeclIdx cp_push(CPDecl *decl, CTInfo info, CTSize size) -{ - return (decl->pos = cp_add(decl, info, size)); -} - -/* Push or merge attributes. */ -static void cp_push_attributes(CPDecl *decl) -{ - CType *ct = &decl->stack[decl->pos]; - if (ctype_isfunc(ct->info)) { /* Ok to modify in-place. */ -#if LJ_TARGET_X86 - if ((decl->fattr & CTFP_CCONV)) - ct->info = (ct->info & (CTMASK_NUM|CTF_VARARG|CTMASK_CID)) + - (decl->fattr & ~CTMASK_CID); -#endif - } else { - if ((decl->attr & CTFP_ALIGNED) && !(decl->mode & CPARSE_MODE_FIELD)) - cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_ALIGN)), - ctype_align(decl->attr)); - } -} - -/* Push unrolled type to declaration stack and merge qualifiers. */ -static void cp_push_type(CPDecl *decl, CTypeID id) -{ - CType *ct = ctype_get(decl->cp->cts, id); - CTInfo info = ct->info; - CTSize size = ct->size; - switch (ctype_type(info)) { - case CT_STRUCT: case CT_ENUM: - cp_push(decl, CTINFO(CT_TYPEDEF, id), 0); /* Don't copy unique types. */ - if ((decl->attr & CTF_QUAL)) { /* Push unmerged qualifiers. */ - cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_QUAL)), - (decl->attr & CTF_QUAL)); - decl->attr &= ~CTF_QUAL; - } - break; - case CT_ATTRIB: - if (ctype_isxattrib(info, CTA_QUAL)) - decl->attr &= ~size; /* Remove redundant qualifiers. */ - cp_push_type(decl, ctype_cid(info)); /* Unroll. */ - cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ - break; - case CT_ARRAY: - cp_push_type(decl, ctype_cid(info)); /* Unroll. */ - cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ - decl->stack[decl->pos].sib = 1; /* Mark as already checked and sized. */ - /* Note: this is not copied to the ct->sib in the C type table. */ - break; - case CT_FUNC: - /* Copy type, link parameters (shared). */ - decl->stack[cp_push(decl, info, size)].sib = ct->sib; - break; - default: - /* Copy type, merge common qualifiers. */ - cp_push(decl, info|(decl->attr & CTF_QUAL), size); - decl->attr &= ~CTF_QUAL; - break; - } -} - -/* Consume the declaration element chain and intern the C type. */ -static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl) -{ - CTypeID id = 0; - CPDeclIdx idx = 0; - CTSize csize = CTSIZE_INVALID; - CTSize cinfo = 0; - do { - CType *ct = &decl->stack[idx]; - CTInfo info = ct->info; - CTInfo size = ct->size; - /* The cid is already part of info for copies of pointers/functions. */ - idx = ct->next; - if (ctype_istypedef(info)) { - lua_assert(id == 0); - id = ctype_cid(info); - /* Always refetch info/size, since struct/enum may have been completed. */ - cinfo = ctype_get(cp->cts, id)->info; - csize = ctype_get(cp->cts, id)->size; - lua_assert(ctype_isstruct(cinfo) || ctype_isenum(cinfo)); - } else if (ctype_isfunc(info)) { /* Intern function. */ - CType *fct; - CTypeID fid; - CTypeID sib; - if (id) { - CType *refct = ctype_raw(cp->cts, id); - /* Reject function or refarray return types. */ - if (ctype_isfunc(refct->info) || ctype_isrefarray(refct->info)) - cp_err(cp, LJ_ERR_FFI_INVTYPE); - } - /* No intervening attributes allowed, skip forward. */ - while (idx) { - CType *ctn = &decl->stack[idx]; - if (!ctype_isattrib(ctn->info)) break; - idx = ctn->next; /* Skip attribute. */ - } - sib = ct->sib; /* Next line may reallocate the C type table. */ - fid = lj_ctype_new(cp->cts, &fct); - csize = CTSIZE_INVALID; - fct->info = cinfo = info + id; - fct->size = size; - fct->sib = sib; - id = fid; - } else if (ctype_isattrib(info)) { - if (ctype_isxattrib(info, CTA_QUAL)) - cinfo |= size; - else if (ctype_isxattrib(info, CTA_ALIGN)) - CTF_INSERT(cinfo, ALIGN, size); - id = lj_ctype_intern(cp->cts, info+id, size); - /* Inherit csize/cinfo from original type. */ - } else { - if (ctype_isnum(info)) { /* Handle mode/vector-size attributes. */ - lua_assert(id == 0); - if (!(info & CTF_BOOL)) { - CTSize msize = ctype_msizeP(decl->attr); - CTSize vsize = ctype_vsizeP(decl->attr); - if (msize && (!(info & CTF_FP) || (msize == 4 || msize == 8))) { - CTSize malign = lj_fls(msize); - if (malign > 4) malign = 4; /* Limit alignment. */ - CTF_INSERT(info, ALIGN, malign); - size = msize; /* Override size via mode. */ - } - if (vsize) { /* Vector size set? */ - CTSize esize = lj_fls(size); - if (vsize >= esize) { - /* Intern the element type first. */ - id = lj_ctype_intern(cp->cts, info, size); - /* Then create a vector (array) with vsize alignment. */ - size = (1u << vsize); - if (vsize > 4) vsize = 4; /* Limit alignment. */ - if (ctype_align(info) > vsize) vsize = ctype_align(info); - info = CTINFO(CT_ARRAY, (info & CTF_QUAL) + CTF_VECTOR + - CTALIGN(vsize)); - } - } - } - } else if (ctype_isptr(info)) { - /* Reject pointer/ref to ref. */ - if (id && ctype_isref(ctype_raw(cp->cts, id)->info)) - cp_err(cp, LJ_ERR_FFI_INVTYPE); - if (ctype_isref(info)) { - info &= ~CTF_VOLATILE; /* Refs are always const, never volatile. */ - /* No intervening attributes allowed, skip forward. */ - while (idx) { - CType *ctn = &decl->stack[idx]; - if (!ctype_isattrib(ctn->info)) break; - idx = ctn->next; /* Skip attribute. */ - } - } - } else if (ctype_isarray(info)) { /* Check for valid array size etc. */ - if (ct->sib == 0) { /* Only check/size arrays not copied by unroll. */ - if (ctype_isref(cinfo)) /* Reject arrays of refs. */ - cp_err(cp, LJ_ERR_FFI_INVTYPE); - /* Reject VLS or unknown-sized types. */ - if (ctype_isvltype(cinfo) || csize == CTSIZE_INVALID) - cp_err(cp, LJ_ERR_FFI_INVSIZE); - /* a[] and a[?] keep their invalid size. */ - if (size != CTSIZE_INVALID) { - uint64_t xsz = (uint64_t)size * csize; - if (xsz >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); - size = (CTSize)xsz; - } - } - if ((cinfo & CTF_ALIGN) > (info & CTF_ALIGN)) /* Find max. align. */ - info = (info & ~CTF_ALIGN) | (cinfo & CTF_ALIGN); - info |= (cinfo & CTF_QUAL); /* Inherit qual. */ - } else { - lua_assert(ctype_isvoid(info)); - } - csize = size; - cinfo = info+id; - id = lj_ctype_intern(cp->cts, info+id, size); - } - } while (idx); - return id; -} - -/* -- C declaration parser ------------------------------------------------ */ - -#define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be) - -/* Reset declaration state to declaration specifier. */ -static void cp_decl_reset(CPDecl *decl) -{ - decl->pos = decl->specpos; - decl->top = decl->specpos+1; - decl->stack[decl->specpos].next = 0; - decl->attr = decl->specattr; - decl->fattr = decl->specfattr; - decl->name = NULL; - decl->redir = NULL; -} - -/* Parse constant initializer. */ -/* NYI: FP constants and strings as initializers. */ -static CTypeID cp_decl_constinit(CPState *cp, CType **ctp, CTypeID ctypeid) -{ - CType *ctt = ctype_get(cp->cts, ctypeid); - CTInfo info; - CTSize size; - CPValue k; - CTypeID constid; - while (ctype_isattrib(ctt->info)) { /* Skip attributes. */ - ctypeid = ctype_cid(ctt->info); /* Update ID, too. */ - ctt = ctype_get(cp->cts, ctypeid); - } - info = ctt->info; - size = ctt->size; - if (!ctype_isinteger(info) || !(info & CTF_CONST) || size > 4) - cp_err(cp, LJ_ERR_FFI_INVTYPE); - cp_check(cp, '='); - cp_expr_sub(cp, &k, 0); - constid = lj_ctype_new(cp->cts, ctp); - (*ctp)->info = CTINFO(CT_CONSTVAL, CTF_CONST|ctypeid); - k.u32 <<= 8*(4-size); - if ((info & CTF_UNSIGNED)) - k.u32 >>= 8*(4-size); - else - k.u32 = (uint32_t)((int32_t)k.u32 >> 8*(4-size)); - (*ctp)->size = k.u32; - return constid; -} - -/* Parse size in parentheses as part of attribute. */ -static CTSize cp_decl_sizeattr(CPState *cp) -{ - CTSize sz; - uint32_t oldtmask = cp->tmask; - cp->tmask = CPNS_DEFAULT; /* Required for expression evaluator. */ - cp_check(cp, '('); - sz = cp_expr_ksize(cp); - cp->tmask = oldtmask; - cp_check(cp, ')'); - return sz; -} - -/* Parse alignment attribute. */ -static void cp_decl_align(CPState *cp, CPDecl *decl) -{ - CTSize al = 4; /* Unspecified alignment is 16 bytes. */ - if (cp->tok == '(') { - al = cp_decl_sizeattr(cp); - al = al ? lj_fls(al) : 0; - } - CTF_INSERT(decl->attr, ALIGN, al); - decl->attr |= CTFP_ALIGNED; -} - -/* Parse GCC asm("name") redirect. */ -static void cp_decl_asm(CPState *cp, CPDecl *decl) -{ - UNUSED(decl); - cp_next(cp); - cp_check(cp, '('); - if (cp->tok == CTOK_STRING) { - GCstr *str = cp->str; - while (cp_next(cp) == CTOK_STRING) { - lj_str_pushf(cp->L, "%s%s", strdata(str), strdata(cp->str)); - cp->L->top--; - str = strV(cp->L->top); - } - decl->redir = str; - } - cp_check(cp, ')'); -} - -/* Parse GCC __attribute__((mode(...))). */ -static void cp_decl_mode(CPState *cp, CPDecl *decl) -{ - cp_check(cp, '('); - if (cp->tok == CTOK_IDENT) { - const char *s = strdata(cp->str); - CTSize sz = 0, vlen = 0; - if (s[0] == '_' && s[1] == '_') s += 2; - if (*s == 'V') { - s++; - vlen = *s++ - '0'; - if (*s >= '0' && *s <= '9') - vlen = vlen*10 + (*s++ - '0'); - } - switch (*s++) { - case 'Q': sz = 1; break; - case 'H': sz = 2; break; - case 'S': sz = 4; break; - case 'D': sz = 8; break; - case 'T': sz = 16; break; - case 'O': sz = 32; break; - default: goto bad_size; - } - if (*s == 'I' || *s == 'F') { - CTF_INSERT(decl->attr, MSIZEP, sz); - if (vlen) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vlen*sz)); - } - bad_size: - cp_next(cp); - } - cp_check(cp, ')'); -} - -/* Parse GCC __attribute__((...)). */ -static void cp_decl_gccattribute(CPState *cp, CPDecl *decl) -{ - cp_next(cp); - cp_check(cp, '('); - cp_check(cp, '('); - while (cp->tok != ')') { - if (cp->tok == CTOK_IDENT) { - GCstr *attrstr = cp->str; - cp_next(cp); - switch (attrstr->hash) { - case H_(64a9208e,8ce14319): case H_(8e6331b2,95a282af): /* aligned */ - cp_decl_align(cp, decl); - break; - case H_(42eb47de,f0ede26c): case H_(29f48a09,cf383e0c): /* packed */ - decl->attr |= CTFP_PACKED; - break; - case H_(0a84eef6,8dfab04c): case H_(995cf92c,d5696591): /* mode */ - cp_decl_mode(cp, decl); - break; - case H_(0ab31997,2d5213fa): case H_(bf875611,200e9990): /* vector_size */ - { - CTSize vsize = cp_decl_sizeattr(cp); - if (vsize) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vsize)); - } - break; -#if LJ_TARGET_X86 - case H_(5ad22db8,c689b848): case H_(439150fa,65ea78cb): /* regparm */ - CTF_INSERT(decl->fattr, REGPARM, cp_decl_sizeattr(cp)); - decl->fattr |= CTFP_CCONV; - break; - case H_(18fc0b98,7ff4c074): case H_(4e62abed,0a747424): /* cdecl */ - CTF_INSERT(decl->fattr, CCONV, CTCC_CDECL); - decl->fattr |= CTFP_CCONV; - break; - case H_(72b2e41b,494c5a44): case H_(f2356d59,f25fc9bd): /* thiscall */ - CTF_INSERT(decl->fattr, CCONV, CTCC_THISCALL); - decl->fattr |= CTFP_CCONV; - break; - case H_(0d0ffc42,ab746f88): case H_(21c54ba1,7f0ca7e3): /* fastcall */ - CTF_INSERT(decl->fattr, CCONV, CTCC_FASTCALL); - decl->fattr |= CTFP_CCONV; - break; - case H_(ef76b040,9412e06a): case H_(de56697b,c750e6e1): /* stdcall */ - CTF_INSERT(decl->fattr, CCONV, CTCC_STDCALL); - decl->fattr |= CTFP_CCONV; - break; - case H_(ea78b622,f234bd8e): case H_(252ffb06,8d50f34b): /* sseregparm */ - decl->fattr |= CTF_SSEREGPARM; - decl->fattr |= CTFP_CCONV; - break; -#endif - default: /* Skip all other attributes. */ - goto skip_attr; - } - } else if (cp->tok >= CTOK_FIRSTDECL) { /* For __attribute((const)) etc. */ - cp_next(cp); - skip_attr: - if (cp_opt(cp, '(')) { - while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); - cp_check(cp, ')'); - } - } else { - break; - } - if (!cp_opt(cp, ',')) break; - } - cp_check(cp, ')'); - cp_check(cp, ')'); -} - -/* Parse MSVC __declspec(...). */ -static void cp_decl_msvcattribute(CPState *cp, CPDecl *decl) -{ - cp_next(cp); - cp_check(cp, '('); - while (cp->tok == CTOK_IDENT) { - GCstr *attrstr = cp->str; - cp_next(cp); - switch (attrstr->hash) { - case H_(bc2395fa,98f267f8): /* align */ - cp_decl_align(cp, decl); - break; - default: /* Ignore all other attributes. */ - if (cp_opt(cp, '(')) { - while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); - cp_check(cp, ')'); - } - break; - } - } - cp_check(cp, ')'); -} - -/* Parse declaration attributes (and common qualifiers). */ -static void cp_decl_attributes(CPState *cp, CPDecl *decl) -{ - for (;;) { - switch (cp->tok) { - case CTOK_CONST: decl->attr |= CTF_CONST; break; - case CTOK_VOLATILE: decl->attr |= CTF_VOLATILE; break; - case CTOK_RESTRICT: break; /* Ignore. */ - case CTOK_EXTENSION: break; /* Ignore. */ - case CTOK_ATTRIBUTE: cp_decl_gccattribute(cp, decl); continue; - case CTOK_ASM: cp_decl_asm(cp, decl); continue; - case CTOK_DECLSPEC: cp_decl_msvcattribute(cp, decl); continue; - case CTOK_CCDECL: -#if LJ_TARGET_X86 - CTF_INSERT(decl->fattr, CCONV, cp->ct->size); - decl->fattr |= CTFP_CCONV; -#endif - break; - case CTOK_PTRSZ: -#if LJ_64 - CTF_INSERT(decl->attr, MSIZEP, cp->ct->size); -#endif - break; - default: return; - } - cp_next(cp); - } -} - -/* Parse struct/union/enum name. */ -static CTypeID cp_struct_name(CPState *cp, CPDecl *sdecl, CTInfo info) -{ - CTypeID sid; - CType *ct; - cp->tmask = CPNS_STRUCT; - cp_next(cp); - cp_decl_attributes(cp, sdecl); - cp->tmask = CPNS_DEFAULT; - if (cp->tok != '{') { - if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); - if (cp->val.id) { /* Name of existing struct/union/enum. */ - sid = cp->val.id; - ct = cp->ct; - if ((ct->info ^ info) & (CTMASK_NUM|CTF_UNION)) /* Wrong type. */ - cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); - } else { /* Create named, incomplete struct/union/enum. */ - if ((cp->mode & CPARSE_MODE_NOIMPLICIT)) - cp_errmsg(cp, 0, LJ_ERR_FFI_BADTAG, strdata(cp->str)); - sid = lj_ctype_new(cp->cts, &ct); - ct->info = info; - ct->size = CTSIZE_INVALID; - ctype_setname(ct, cp->str); - lj_ctype_addname(cp->cts, ct, sid); - } - cp_next(cp); - } else { /* Create anonymous, incomplete struct/union/enum. */ - sid = lj_ctype_new(cp->cts, &ct); - ct->info = info; - ct->size = CTSIZE_INVALID; - } - if (cp->tok == '{') { - if (ct->size != CTSIZE_INVALID || ct->sib) - cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); - ct->sib = 1; /* Indicate the type is currently being defined. */ - } - return sid; -} - -/* Determine field alignment. */ -static CTSize cp_field_align(CPState *cp, CType *ct, CTInfo info) -{ - CTSize align = ctype_align(info); - UNUSED(cp); UNUSED(ct); -#if (LJ_TARGET_X86 && !LJ_ABI_WIN) || (LJ_TARGET_ARM && __APPLE__) - /* The SYSV i386 and iOS ABIs limit alignment of non-vector fields to 2^2. */ - if (align > 2 && !(info & CTFP_ALIGNED)) { - if (ctype_isarray(info) && !(info & CTF_VECTOR)) { - do { - ct = ctype_rawchild(cp->cts, ct); - info = ct->info; - } while (ctype_isarray(info) && !(info & CTF_VECTOR)); - } - if (ctype_isnum(info) || ctype_isenum(info)) - align = 2; - } -#endif - return align; -} - -/* Layout struct/union fields. */ -static void cp_struct_layout(CPState *cp, CTypeID sid, CTInfo sattr) -{ - CTSize bofs = 0, bmaxofs = 0; /* Bit offset and max. bit offset. */ - CTSize maxalign = ctype_align(sattr); - CType *sct = ctype_get(cp->cts, sid); - CTInfo sinfo = sct->info; - CTypeID fieldid = sct->sib; - while (fieldid) { - CType *ct = ctype_get(cp->cts, fieldid); - CTInfo attr = ct->size; /* Field declaration attributes (temp.). */ - - if (ctype_isfield(ct->info) || - (ctype_isxattrib(ct->info, CTA_SUBTYPE) && attr)) { - CTSize align, amask; /* Alignment (pow2) and alignment mask (bits). */ - CTSize sz; - CTInfo info = lj_ctype_info(cp->cts, ctype_cid(ct->info), &sz); - CTSize bsz, csz = 8*sz; /* Field size and container size (in bits). */ - sinfo |= (info & (CTF_QUAL|CTF_VLA)); /* Merge pseudo-qualifiers. */ - - /* Check for size overflow and determine alignment. */ - if (sz >= 0x20000000u || bofs + csz < bofs) { - if (!(sz == CTSIZE_INVALID && ctype_isarray(info) && - !(sinfo & CTF_UNION))) - cp_err(cp, LJ_ERR_FFI_INVSIZE); - csz = sz = 0; /* Treat a[] and a[?] as zero-sized. */ - } - align = cp_field_align(cp, ct, info); - if (((attr|sattr) & CTFP_PACKED) || - ((attr & CTFP_ALIGNED) && ctype_align(attr) > align)) - align = ctype_align(attr); - if (cp->packstack[cp->curpack] < align) - align = cp->packstack[cp->curpack]; - if (align > maxalign) maxalign = align; - amask = (8u << align) - 1; - - bsz = ctype_bitcsz(ct->info); /* Bitfield size (temp.). */ - if (bsz == CTBSZ_FIELD || !ctype_isfield(ct->info)) { - bsz = csz; /* Regular fields or subtypes always fill the container. */ - bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ - ct->size = (bofs >> 3); /* Store field offset. */ - } else { /* Bitfield. */ - if (bsz == 0 || (attr & CTFP_ALIGNED) || - (!((attr|sattr) & CTFP_PACKED) && (bofs & amask) + bsz > csz)) - bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ - - /* Prefer regular field over bitfield. */ - if (bsz == csz && (bofs & amask) == 0) { - ct->info = CTINFO(CT_FIELD, ctype_cid(ct->info)); - ct->size = (bofs >> 3); /* Store field offset. */ - } else { - ct->info = CTINFO(CT_BITFIELD, - (info & (CTF_QUAL|CTF_UNSIGNED|CTF_BOOL)) + - (csz << (CTSHIFT_BITCSZ-3)) + (bsz << CTSHIFT_BITBSZ)); -#if LJ_BE - ct->info += ((csz - (bofs & (csz-1)) - bsz) << CTSHIFT_BITPOS); -#else - ct->info += ((bofs & (csz-1)) << CTSHIFT_BITPOS); -#endif - ct->size = ((bofs & ~(csz-1)) >> 3); /* Store container offset. */ - } - } - - /* Determine next offset or max. offset. */ - if ((sinfo & CTF_UNION)) { - if (bsz > bmaxofs) bmaxofs = bsz; - } else { - bofs += bsz; - } - } /* All other fields in the chain are already set up. */ - - fieldid = ct->sib; - } - - /* Complete struct/union. */ - sct->info = sinfo + CTALIGN(maxalign); - bofs = (sinfo & CTF_UNION) ? bmaxofs : bofs; - maxalign = (8u << maxalign) - 1; - sct->size = (((bofs + maxalign) & ~maxalign) >> 3); -} - -/* Parse struct/union declaration. */ -static CTypeID cp_decl_struct(CPState *cp, CPDecl *sdecl, CTInfo sinfo) -{ - CTypeID sid = cp_struct_name(cp, sdecl, sinfo); - if (cp_opt(cp, '{')) { /* Struct/union definition. */ - CTypeID lastid = sid; - int lastdecl = 0; - while (cp->tok != '}') { - CPDecl decl; - CPscl scl = cp_decl_spec(cp, &decl, CDF_STATIC); - decl.mode = scl ? CPARSE_MODE_DIRECT : - CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT|CPARSE_MODE_FIELD; - - for (;;) { - CTypeID ctypeid; - - if (lastdecl) cp_err_token(cp, '}'); - - /* Parse field declarator. */ - decl.bits = CTSIZE_INVALID; - cp_declarator(cp, &decl); - ctypeid = cp_decl_intern(cp, &decl); - - if ((scl & CDF_STATIC)) { /* Static constant in struct namespace. */ - CType *ct; - CTypeID fieldid = cp_decl_constinit(cp, &ct, ctypeid); - ctype_get(cp->cts, lastid)->sib = fieldid; - lastid = fieldid; - ctype_setname(ct, decl.name); - } else { - CTSize bsz = CTBSZ_FIELD; /* Temp. for layout phase. */ - CType *ct; - CTypeID fieldid = lj_ctype_new(cp->cts, &ct); /* Do this first. */ - CType *tct = ctype_raw(cp->cts, ctypeid); - - if (decl.bits == CTSIZE_INVALID) { /* Regular field. */ - if (ctype_isarray(tct->info) && tct->size == CTSIZE_INVALID) - lastdecl = 1; /* a[] or a[?] must be the last declared field. */ - - /* Accept transparent struct/union/enum. */ - if (!decl.name) { - if (!((ctype_isstruct(tct->info) && !(tct->info & CTF_VLA)) || - ctype_isenum(tct->info))) - cp_err_token(cp, CTOK_IDENT); - ct->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_SUBTYPE) + ctypeid); - ct->size = ctype_isstruct(tct->info) ? - (decl.attr|0x80000000u) : 0; /* For layout phase. */ - goto add_field; - } - } else { /* Bitfield. */ - bsz = decl.bits; - if (!ctype_isinteger_or_bool(tct->info) || - (bsz == 0 && decl.name) || 8*tct->size > CTBSZ_MAX || - bsz > ((tct->info & CTF_BOOL) ? 1 : 8*tct->size)) - cp_errmsg(cp, ':', LJ_ERR_BADVAL); - } - - /* Create temporary field for layout phase. */ - ct->info = CTINFO(CT_FIELD, ctypeid + (bsz << CTSHIFT_BITCSZ)); - ct->size = decl.attr; - if (decl.name) ctype_setname(ct, decl.name); - - add_field: - ctype_get(cp->cts, lastid)->sib = fieldid; - lastid = fieldid; - } - if (!cp_opt(cp, ',')) break; - cp_decl_reset(&decl); - } - cp_check(cp, ';'); - } - cp_check(cp, '}'); - ctype_get(cp->cts, lastid)->sib = 0; /* Drop sib = 1 for empty structs. */ - cp_decl_attributes(cp, sdecl); /* Layout phase needs postfix attributes. */ - cp_struct_layout(cp, sid, sdecl->attr); - } - return sid; -} - -/* Parse enum declaration. */ -static CTypeID cp_decl_enum(CPState *cp, CPDecl *sdecl) -{ - CTypeID eid = cp_struct_name(cp, sdecl, CTINFO(CT_ENUM, CTID_VOID)); - CTInfo einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_UINT32); - CTSize esize = 4; /* Only 32 bit enums are supported. */ - if (cp_opt(cp, '{')) { /* Enum definition. */ - CPValue k; - CTypeID lastid = eid; - k.u32 = 0; - k.id = CTID_INT32; - do { - GCstr *name = cp->str; - if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); - if (cp->val.id) cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(name)); - cp_next(cp); - if (cp_opt(cp, '=')) { - cp_expr_kint(cp, &k); - if (k.id == CTID_UINT32) { - /* C99 says that enum constants are always (signed) integers. - ** But since unsigned constants like 0x80000000 are quite common, - ** those are left as uint32_t. - */ - if (k.i32 >= 0) k.id = CTID_INT32; - } else { - /* OTOH it's common practice and even mandated by some ABIs - ** that the enum type itself is unsigned, unless there are any - ** negative constants. - */ - k.id = CTID_INT32; - if (k.i32 < 0) einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_INT32); - } - } - /* Add named enum constant. */ - { - CType *ct; - CTypeID constid = lj_ctype_new(cp->cts, &ct); - ctype_get(cp->cts, lastid)->sib = constid; - lastid = constid; - ctype_setname(ct, name); - ct->info = CTINFO(CT_CONSTVAL, CTF_CONST|k.id); - ct->size = k.u32++; - if (k.u32 == 0x80000000u) k.id = CTID_UINT32; - lj_ctype_addname(cp->cts, ct, constid); - } - if (!cp_opt(cp, ',')) break; - } while (cp->tok != '}'); /* Trailing ',' is ok. */ - cp_check(cp, '}'); - /* Complete enum. */ - ctype_get(cp->cts, eid)->info = einfo; - ctype_get(cp->cts, eid)->size = esize; - } - return eid; -} - -/* Parse declaration specifiers. */ -static CPscl cp_decl_spec(CPState *cp, CPDecl *decl, CPscl scl) -{ - uint32_t cds = 0, sz = 0; - CTypeID tdef = 0; - - decl->cp = cp; - decl->mode = cp->mode; - decl->name = NULL; - decl->redir = NULL; - decl->attr = 0; - decl->fattr = 0; - decl->pos = decl->top = 0; - decl->stack[0].next = 0; - - for (;;) { /* Parse basic types. */ - cp_decl_attributes(cp, decl); - if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECLFLAG) { - uint32_t cbit; - if (cp->ct->size) { - if (sz) goto end_decl; - sz = cp->ct->size; - } - cbit = (1u << (cp->tok - CTOK_FIRSTDECL)); - cds = cds | cbit | ((cbit & cds & CDF_LONG) << 1); - if (cp->tok >= CTOK_FIRSTSCL) { - if (!(scl & cbit)) cp_errmsg(cp, cp->tok, LJ_ERR_FFI_BADSCL); - } else if (tdef) { - goto end_decl; - } - cp_next(cp); - continue; - } - if (sz || tdef || - (cds & (CDF_SHORT|CDF_LONG|CDF_SIGNED|CDF_UNSIGNED|CDF_COMPLEX))) - break; - switch (cp->tok) { - case CTOK_STRUCT: - tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, 0)); - continue; - case CTOK_UNION: - tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, CTF_UNION)); - continue; - case CTOK_ENUM: - tdef = cp_decl_enum(cp, decl); - continue; - case CTOK_IDENT: - if (ctype_istypedef(cp->ct->info)) { - tdef = ctype_cid(cp->ct->info); /* Get typedef. */ - cp_next(cp); - continue; - } - break; - case '$': - tdef = cp->val.id; - cp_next(cp); - continue; - default: - break; - } - break; - } -end_decl: - - if ((cds & CDF_COMPLEX)) /* Use predefined complex types. */ - tdef = sz == 4 ? CTID_COMPLEX_FLOAT : CTID_COMPLEX_DOUBLE; - - if (tdef) { - cp_push_type(decl, tdef); - } else if ((cds & CDF_VOID)) { - cp_push(decl, CTINFO(CT_VOID, (decl->attr & CTF_QUAL)), CTSIZE_INVALID); - decl->attr &= ~CTF_QUAL; - } else { - /* Determine type info and size. */ - CTInfo info = CTINFO(CT_NUM, (cds & CDF_UNSIGNED) ? CTF_UNSIGNED : 0); - if ((cds & CDF_BOOL)) { - if ((cds & ~(CDF_SCL|CDF_BOOL|CDF_INT|CDF_SIGNED|CDF_UNSIGNED))) - cp_errmsg(cp, 0, LJ_ERR_FFI_INVTYPE); - info |= CTF_BOOL; - if (!(cds & CDF_SIGNED)) info |= CTF_UNSIGNED; - if (!sz) { - sz = 1; - } - } else if ((cds & CDF_FP)) { - info = CTINFO(CT_NUM, CTF_FP); - if ((cds & CDF_LONG)) sz = sizeof(long double); - } else if ((cds & CDF_CHAR)) { - if ((cds & (CDF_CHAR|CDF_SIGNED|CDF_UNSIGNED)) == CDF_CHAR) - info |= CTF_UCHAR; /* Handle platforms where char is unsigned. */ - } else if ((cds & CDF_SHORT)) { - sz = sizeof(short); - } else if ((cds & CDF_LONGLONG)) { - sz = 8; - } else if ((cds & CDF_LONG)) { - info |= CTF_LONG; - sz = sizeof(long); - } else if (!sz) { - if (!(cds & (CDF_SIGNED|CDF_UNSIGNED))) - cp_errmsg(cp, cp->tok, LJ_ERR_FFI_DECLSPEC); - sz = sizeof(int); - } - lua_assert(sz != 0); - info += CTALIGN(lj_fls(sz)); /* Use natural alignment. */ - info += (decl->attr & CTF_QUAL); /* Merge qualifiers. */ - cp_push(decl, info, sz); - decl->attr &= ~CTF_QUAL; - } - decl->specpos = decl->pos; - decl->specattr = decl->attr; - decl->specfattr = decl->fattr; - return (cds & CDF_SCL); /* Return storage class. */ -} - -/* Parse array declaration. */ -static void cp_decl_array(CPState *cp, CPDecl *decl) -{ - CTInfo info = CTINFO(CT_ARRAY, 0); - CTSize nelem = CTSIZE_INVALID; /* Default size for a[] or a[?]. */ - cp_decl_attributes(cp, decl); - if (cp_opt(cp, '?')) - info |= CTF_VLA; /* Create variable-length array a[?]. */ - else if (cp->tok != ']') - nelem = cp_expr_ksize(cp); - cp_check(cp, ']'); - cp_add(decl, info, nelem); -} - -/* Parse function declaration. */ -static void cp_decl_func(CPState *cp, CPDecl *fdecl) -{ - CTSize nargs = 0; - CTInfo info = CTINFO(CT_FUNC, 0); - CTypeID lastid = 0, anchor = 0; - if (cp->tok != ')') { - do { - CPDecl decl; - CTypeID ctypeid, fieldid; - CType *ct; - if (cp_opt(cp, '.')) { /* Vararg function. */ - cp_check(cp, '.'); /* Workaround for the minimalistic lexer. */ - cp_check(cp, '.'); - info |= CTF_VARARG; - break; - } - cp_decl_spec(cp, &decl, CDF_REGISTER); - decl.mode = CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT; - cp_declarator(cp, &decl); - ctypeid = cp_decl_intern(cp, &decl); - ct = ctype_raw(cp->cts, ctypeid); - if (ctype_isvoid(ct->info)) - break; - else if (ctype_isrefarray(ct->info)) - ctypeid = lj_ctype_intern(cp->cts, - CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ct->info)), CTSIZE_PTR); - else if (ctype_isfunc(ct->info)) - ctypeid = lj_ctype_intern(cp->cts, - CTINFO(CT_PTR, CTALIGN_PTR|ctypeid), CTSIZE_PTR); - /* Add new parameter. */ - fieldid = lj_ctype_new(cp->cts, &ct); - if (anchor) - ctype_get(cp->cts, lastid)->sib = fieldid; - else - anchor = fieldid; - lastid = fieldid; - if (decl.name) ctype_setname(ct, decl.name); - ct->info = CTINFO(CT_FIELD, ctypeid); - ct->size = nargs++; - } while (cp_opt(cp, ',')); - } - cp_check(cp, ')'); - if (cp_opt(cp, '{')) { /* Skip function definition. */ - int level = 1; - cp->mode |= CPARSE_MODE_SKIP; - for (;;) { - if (cp->tok == '{') level++; - else if (cp->tok == '}' && --level == 0) break; - else if (cp->tok == CTOK_EOF) cp_err_token(cp, '}'); - cp_next(cp); - } - cp->mode &= ~CPARSE_MODE_SKIP; - cp->tok = ';'; /* Ok for cp_decl_multi(), error in cp_decl_single(). */ - } - info |= (fdecl->fattr & ~CTMASK_CID); - fdecl->fattr = 0; - fdecl->stack[cp_add(fdecl, info, nargs)].sib = anchor; -} - -/* Parse declarator. */ -static void cp_declarator(CPState *cp, CPDecl *decl) -{ - if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); - - for (;;) { /* Head of declarator. */ - if (cp_opt(cp, '*')) { /* Pointer. */ - CTSize sz; - CTInfo info; - cp_decl_attributes(cp, decl); - sz = CTSIZE_PTR; - info = CTINFO(CT_PTR, CTALIGN_PTR); -#if LJ_64 - if (ctype_msizeP(decl->attr) == 4) { - sz = 4; - info = CTINFO(CT_PTR, CTALIGN(2)); - } -#endif - info += (decl->attr & (CTF_QUAL|CTF_REF)); - decl->attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<mode & CPARSE_MODE_ABSTRACT) && - (cp->tok == ')' || cp_istypedecl(cp))) goto func_decl; - pos = decl->pos; - cp_declarator(cp, decl); - cp_check(cp, ')'); - decl->pos = pos; - } else if (cp->tok == CTOK_IDENT) { /* Direct declarator. */ - if (!(decl->mode & CPARSE_MODE_DIRECT)) cp_err_token(cp, CTOK_EOF); - decl->name = cp->str; - decl->nameid = cp->val.id; - cp_next(cp); - } else { /* Abstract declarator. */ - if (!(decl->mode & CPARSE_MODE_ABSTRACT)) cp_err_token(cp, CTOK_IDENT); - } - - for (;;) { /* Tail of declarator. */ - if (cp_opt(cp, '[')) { /* Array. */ - cp_decl_array(cp, decl); - } else if (cp_opt(cp, '(')) { /* Function. */ - func_decl: - cp_decl_func(cp, decl); - } else { - break; - } - } - - if ((decl->mode & CPARSE_MODE_FIELD) && cp_opt(cp, ':')) /* Field width. */ - decl->bits = cp_expr_ksize(cp); - - /* Process postfix attributes. */ - cp_decl_attributes(cp, decl); - cp_push_attributes(decl); - - cp->depth--; -} - -/* Parse an abstract type declaration and return it's C type ID. */ -static CTypeID cp_decl_abstract(CPState *cp) -{ - CPDecl decl; - cp_decl_spec(cp, &decl, 0); - decl.mode = CPARSE_MODE_ABSTRACT; - cp_declarator(cp, &decl); - return cp_decl_intern(cp, &decl); -} - -/* Handle pragmas. */ -static void cp_pragma(CPState *cp, BCLine pragmaline) -{ - cp_next(cp); - if (cp->tok == CTOK_IDENT && - cp->str->hash == H_(e79b999f,42ca3e85)) { /* pack */ - cp_next(cp); - cp_check(cp, '('); - if (cp->tok == CTOK_IDENT) { - if (cp->str->hash == H_(738e923c,a1b65954)) { /* push */ - if (cp->curpack < CPARSE_MAX_PACKSTACK) { - cp->packstack[cp->curpack+1] = cp->packstack[cp->curpack]; - cp->curpack++; - } - } else if (cp->str->hash == H_(6c71cf27,6c71cf27)) { /* pop */ - if (cp->curpack > 0) cp->curpack--; - } else { - cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); - } - cp_next(cp); - if (!cp_opt(cp, ',')) goto end_pack; - } - if (cp->tok == CTOK_INTEGER) { - cp->packstack[cp->curpack] = cp->val.u32 ? lj_fls(cp->val.u32) : 0; - cp_next(cp); - } else { - cp->packstack[cp->curpack] = 255; - } - end_pack: - cp_check(cp, ')'); - } else { /* Ignore all other pragmas. */ - while (cp->tok != CTOK_EOF && cp->linenumber == pragmaline) - cp_next(cp); - } -} - -/* Parse multiple C declarations of types or extern identifiers. */ -static void cp_decl_multi(CPState *cp) -{ - int first = 1; - while (cp->tok != CTOK_EOF) { - CPDecl decl; - CPscl scl; - if (cp_opt(cp, ';')) { /* Skip empty statements. */ - first = 0; - continue; - } - if (cp->tok == '#') { /* Workaround, since we have no preprocessor, yet. */ - BCLine pragmaline = cp->linenumber; - if (!(cp_next(cp) == CTOK_IDENT && - cp->str->hash == H_(f5e6b4f8,1d509107))) /* pragma */ - cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); - cp_pragma(cp, pragmaline); - continue; - } - scl = cp_decl_spec(cp, &decl, CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC); - if ((cp->tok == ';' || cp->tok == CTOK_EOF) && - ctype_istypedef(decl.stack[0].info)) { - CTInfo info = ctype_rawchild(cp->cts, &decl.stack[0])->info; - if (ctype_isstruct(info) || ctype_isenum(info)) - goto decl_end; /* Accept empty declaration of struct/union/enum. */ - } - for (;;) { - CTypeID ctypeid; - cp_declarator(cp, &decl); - ctypeid = cp_decl_intern(cp, &decl); - if (decl.name && !decl.nameid) { /* NYI: redeclarations are ignored. */ - CType *ct; - CTypeID id; - if ((scl & CDF_TYPEDEF)) { /* Create new typedef. */ - id = lj_ctype_new(cp->cts, &ct); - ct->info = CTINFO(CT_TYPEDEF, ctypeid); - goto noredir; - } else if (ctype_isfunc(ctype_get(cp->cts, ctypeid)->info)) { - /* Treat both static and extern function declarations as extern. */ - ct = ctype_get(cp->cts, ctypeid); - /* We always get new anonymous functions (typedefs are copied). */ - lua_assert(gcref(ct->name) == NULL); - id = ctypeid; /* Just name it. */ - } else if ((scl & CDF_STATIC)) { /* Accept static constants. */ - id = cp_decl_constinit(cp, &ct, ctypeid); - goto noredir; - } else { /* External references have extern or no storage class. */ - id = lj_ctype_new(cp->cts, &ct); - ct->info = CTINFO(CT_EXTERN, ctypeid); - } - if (decl.redir) { /* Add attribute for redirected symbol name. */ - CType *cta; - CTypeID aid = lj_ctype_new(cp->cts, &cta); - ct = ctype_get(cp->cts, id); /* Table may have been reallocated. */ - cta->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_REDIR)); - cta->sib = ct->sib; - ct->sib = aid; - ctype_setname(cta, decl.redir); - } - noredir: - ctype_setname(ct, decl.name); - lj_ctype_addname(cp->cts, ct, id); - } - if (!cp_opt(cp, ',')) break; - cp_decl_reset(&decl); - } - decl_end: - if (cp->tok == CTOK_EOF && first) break; /* May omit ';' for 1 decl. */ - first = 0; - cp_check(cp, ';'); - } -} - -/* Parse a single C type declaration. */ -static void cp_decl_single(CPState *cp) -{ - CPDecl decl; - cp_decl_spec(cp, &decl, 0); - cp_declarator(cp, &decl); - cp->val.id = cp_decl_intern(cp, &decl); - if (cp->tok != CTOK_EOF) cp_err_token(cp, CTOK_EOF); -} - -#undef H_ - -/* ------------------------------------------------------------------------ */ - -/* Protected callback for C parser. */ -static TValue *cpcparser(lua_State *L, lua_CFunction dummy, void *ud) -{ - CPState *cp = (CPState *)ud; - UNUSED(dummy); - cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ - cp_init(cp); - if ((cp->mode & CPARSE_MODE_MULTI)) - cp_decl_multi(cp); - else - cp_decl_single(cp); - if (cp->param && cp->param != cp->L->top) - cp_err(cp, LJ_ERR_FFI_NUMPARAM); - lua_assert(cp->depth == 0); - return NULL; -} - -/* C parser. */ -int lj_cparse(CPState *cp) -{ - LJ_CTYPE_SAVE(cp->cts); - int errcode = lj_vm_cpcall(cp->L, NULL, cp, cpcparser); - if (errcode) - LJ_CTYPE_RESTORE(cp->cts); - cp_cleanup(cp); - return errcode; -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_cparse.h b/third-party/LuaJIT-2.0.2/src/lj_cparse.h deleted file mode 100644 index c097b14a6e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_cparse.h +++ /dev/null @@ -1,65 +0,0 @@ -/* -** C declaration parser. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CPARSE_H -#define _LJ_CPARSE_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* C parser limits. */ -#define CPARSE_MAX_BUF 32768 /* Max. token buffer size. */ -#define CPARSE_MAX_DECLSTACK 100 /* Max. declaration stack depth. */ -#define CPARSE_MAX_DECLDEPTH 20 /* Max. recursive declaration depth. */ -#define CPARSE_MAX_PACKSTACK 7 /* Max. pack pragma stack depth. */ - -/* Flags for C parser mode. */ -#define CPARSE_MODE_MULTI 1 /* Process multiple declarations. */ -#define CPARSE_MODE_ABSTRACT 2 /* Accept abstract declarators. */ -#define CPARSE_MODE_DIRECT 4 /* Accept direct declarators. */ -#define CPARSE_MODE_FIELD 8 /* Accept field width in bits, too. */ -#define CPARSE_MODE_NOIMPLICIT 16 /* Reject implicit declarations. */ -#define CPARSE_MODE_SKIP 32 /* Skip definitions, ignore errors. */ - -typedef int CPChar; /* C parser character. Unsigned ext. from char. */ -typedef int CPToken; /* C parser token. */ - -/* C parser internal value representation. */ -typedef struct CPValue { - union { - int32_t i32; /* Value for CTID_INT32. */ - uint32_t u32; /* Value for CTID_UINT32. */ - }; - CTypeID id; /* C Type ID of the value. */ -} CPValue; - -/* C parser state. */ -typedef struct CPState { - CPChar c; /* Current character. */ - CPToken tok; /* Current token. */ - CPValue val; /* Token value. */ - GCstr *str; /* Interned string of identifier/keyword. */ - CType *ct; /* C type table entry. */ - const char *p; /* Current position in input buffer. */ - SBuf sb; /* String buffer for tokens. */ - lua_State *L; /* Lua state. */ - CTState *cts; /* C type state. */ - TValue *param; /* C type parameters. */ - const char *srcname; /* Current source name. */ - BCLine linenumber; /* Input line counter. */ - int depth; /* Recursive declaration depth. */ - uint32_t tmask; /* Type mask for next identifier. */ - uint32_t mode; /* C parser mode. */ - uint8_t packstack[CPARSE_MAX_PACKSTACK]; /* Stack for pack pragmas. */ - uint8_t curpack; /* Current position in pack pragma stack. */ -} CPState; - -LJ_FUNC int lj_cparse(CPState *cp); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_crecord.c b/third-party/LuaJIT-2.0.2/src/lj_crecord.c deleted file mode 100644 index df98e6ec90..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_crecord.c +++ /dev/null @@ -1,1653 +0,0 @@ -/* -** Trace recorder for C data operations. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_ffrecord_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT && LJ_HASFFI - -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_frame.h" -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_cparse.h" -#include "lj_cconv.h" -#include "lj_clib.h" -#include "lj_ccall.h" -#include "lj_ff.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_record.h" -#include "lj_ffrecord.h" -#include "lj_snap.h" -#include "lj_crecord.h" -#include "lj_dispatch.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -#define emitconv(a, dt, st, flags) \ - emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags)) - -/* -- C type checks ------------------------------------------------------- */ - -static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o) -{ - GCcdata *cd; - TRef trtypeid; - if (!tref_iscdata(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - cd = cdataV(o); - /* Specialize to the CTypeID. */ - trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID); - emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid)); - return cd; -} - -/* Specialize to the CTypeID held by a cdata constructor. */ -static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr) -{ - CTypeID id; - lua_assert(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID); - id = *(CTypeID *)cdataptr(cd); - tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT); - emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id)); - return id; -} - -static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o) -{ - if (tref_isstr(tr)) { - GCstr *s = strV(o); - CPState cp; - CTypeID oldtop; - /* Specialize to the string containing the C type declaration. */ - emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s)); - cp.L = J->L; - cp.cts = ctype_ctsG(J2G(J)); - oldtop = cp.cts->top; - cp.srcname = strdata(s); - cp.p = strdata(s); - cp.param = NULL; - cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; - if (lj_cparse(&cp) || cp.cts->top > oldtop) /* Avoid new struct defs. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); - return cp.val.id; - } else { - GCcdata *cd = argv2cdata(J, tr, o); - return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) : - cd->ctypeid; - } -} - -/* Convert CType to IRType (if possible). */ -static IRType crec_ct2irt(CTState *cts, CType *ct) -{ - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (LJ_LIKELY(ctype_isnum(ct->info))) { - if ((ct->info & CTF_FP)) { - if (ct->size == sizeof(double)) - return IRT_NUM; - else if (ct->size == sizeof(float)) - return IRT_FLOAT; - } else { - uint32_t b = lj_fls(ct->size); - if (b <= 3) - return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0); - } - } else if (ctype_isptr(ct->info)) { - return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; - } else if (ctype_iscomplex(ct->info)) { - if (ct->size == 2*sizeof(double)) - return IRT_NUM; - else if (ct->size == 2*sizeof(float)) - return IRT_FLOAT; - } - return IRT_CDATA; -} - -/* -- Optimized memory fill and copy -------------------------------------- */ - -/* Maximum length and unroll of inlined copy/fill. */ -#define CREC_COPY_MAXUNROLL 16 -#define CREC_COPY_MAXLEN 128 - -#define CREC_FILL_MAXUNROLL 16 - -/* Number of windowed registers used for optimized memory copy. */ -#if LJ_TARGET_X86 -#define CREC_COPY_REGWIN 2 -#elif LJ_TARGET_PPC || LJ_TARGET_MIPS -#define CREC_COPY_REGWIN 8 -#else -#define CREC_COPY_REGWIN 4 -#endif - -/* List of memory offsets for copy/fill. */ -typedef struct CRecMemList { - CTSize ofs; /* Offset in bytes. */ - IRType tp; /* Type of load/store. */ - TRef trofs; /* TRef of interned offset. */ - TRef trval; /* TRef of load value. */ -} CRecMemList; - -/* Generate copy list for element-wise struct copy. */ -static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct) -{ - CTypeID fid = ct->sib; - MSize mlp = 0; - while (fid) { - CType *df = ctype_get(cts, fid); - fid = df->sib; - if (ctype_isfield(df->info)) { - CType *cct; - IRType tp; - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - cct = ctype_rawchild(cts, df); /* Field type. */ - tp = crec_ct2irt(cts, cct); - if (tp == IRT_CDATA) return 0; /* NYI: aggregates. */ - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = df->size; - ml[mlp].tp = tp; - mlp++; - if (ctype_iscomplex(cct->info)) { - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = df->size + (cct->size >> 1); - ml[mlp].tp = tp; - mlp++; - } - } else if (!ctype_isconstval(df->info)) { - /* NYI: bitfields and sub-structures. */ - return 0; - } - } - return mlp; -} - -/* Generate unrolled copy list, from highest to lowest step size/alignment. */ -static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step, - IRType tp) -{ - CTSize ofs = 0; - MSize mlp = 0; - if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step); - do { - while (ofs + step <= len) { - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = ofs; - ml[mlp].tp = tp; - mlp++; - ofs += step; - } - step >>= 1; - tp -= 2; - } while (ofs < len); - return mlp; -} - -/* -** Emit copy list with windowed loads/stores. -** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such). -*/ -static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp, - TRef trdst, TRef trsrc) -{ - MSize i, j, rwin = 0; - for (i = 0, j = 0; i < mlp; ) { - TRef trofs = lj_ir_kintp(J, ml[i].ofs); - TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs); - ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0); - ml[i].trofs = trofs; - i++; - rwin += (LJ_SOFTFP && ml[i].tp == IRT_NUM) ? 2 : 1; - if (rwin >= CREC_COPY_REGWIN || i >= mlp) { /* Flush buffered stores. */ - rwin = 0; - for ( ; j < i; j++) { - TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs); - emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval); - } - } - } -} - -/* Optimized memory copy. */ -static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen, - CType *ct) -{ - if (tref_isk(trlen)) { /* Length must be constant. */ - CRecMemList ml[CREC_COPY_MAXUNROLL]; - MSize mlp = 0; - CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i; - IRType tp = IRT_CDATA; - int needxbar = 0; - if (len == 0) return; /* Shortcut. */ - if (len > CREC_COPY_MAXLEN) goto fallback; - if (ct) { - CTState *cts = ctype_ctsG(J2G(J)); - lua_assert(ctype_isarray(ct->info) || ctype_isstruct(ct->info)); - if (ctype_isarray(ct->info)) { - CType *cct = ctype_rawchild(cts, ct); - tp = crec_ct2irt(cts, cct); - if (tp == IRT_CDATA) goto rawcopy; - step = lj_ir_type_size[tp]; - lua_assert((len & (step-1)) == 0); - } else if ((ct->info & CTF_UNION)) { - step = (1u << ctype_align(ct->info)); - goto rawcopy; - } else { - mlp = crec_copy_struct(ml, cts, ct); - goto emitcopy; - } - } else { - rawcopy: - needxbar = 1; - if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR) - step = CTSIZE_PTR; - } - mlp = crec_copy_unroll(ml, len, step, tp); - emitcopy: - if (mlp) { - crec_copy_emit(J, ml, mlp, trdst, trsrc); - if (needxbar) - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); - return; - } - } -fallback: - /* Call memcpy. Always needs a barrier to disable alias analysis. */ - lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen); - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); -} - -/* Generate unrolled fill list, from highest to lowest step size/alignment. */ -static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step) -{ - CTSize ofs = 0; - MSize mlp = 0; - IRType tp = IRT_U8 + 2*lj_fls(step); - do { - while (ofs + step <= len) { - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = ofs; - ml[mlp].tp = tp; - mlp++; - ofs += step; - } - step >>= 1; - tp -= 2; - } while (ofs < len); - return mlp; -} - -/* -** Emit stores for fill list. -** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such). -*/ -static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp, - TRef trdst, TRef trfill) -{ - MSize i; - for (i = 0; i < mlp; i++) { - TRef trofs = lj_ir_kintp(J, ml[i].ofs); - TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs); - emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill); - } -} - -/* Optimized memory fill. */ -static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill, - CTSize step) -{ - if (tref_isk(trlen)) { /* Length must be constant. */ - CRecMemList ml[CREC_FILL_MAXUNROLL]; - MSize mlp; - CTSize len = (CTSize)IR(tref_ref(trlen))->i; - if (len == 0) return; /* Shortcut. */ - if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR) - step = CTSIZE_PTR; - if (step * CREC_FILL_MAXUNROLL < len) goto fallback; - mlp = crec_fill_unroll(ml, len, step); - if (!mlp) goto fallback; - if (tref_isk(trfill) || ml[0].tp != IRT_U8) - trfill = emitconv(trfill, IRT_INT, IRT_U8, 0); - if (ml[0].tp != IRT_U8) { /* Scatter U8 to U16/U32/U64. */ - if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) { - if (tref_isk(trfill)) /* Pointless on x64 with zero-extended regs. */ - trfill = emitconv(trfill, IRT_U64, IRT_U32, 0); - trfill = emitir(IRT(IR_MUL, IRT_U64), trfill, - lj_ir_kint64(J, U64x(01010101,01010101))); - } else { - trfill = emitir(IRTI(IR_MUL), trfill, - lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101)); - } - } - crec_fill_emit(J, ml, mlp, trdst, trfill); - } else { -fallback: - /* Call memset. Always needs a barrier to disable alias analysis. */ - lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen); /* Note: arg order! */ - } - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); -} - -/* -- Convert C type to C type -------------------------------------------- */ - -/* -** This code mirrors the code in lj_cconv.c. It performs the same steps -** for the trace recorder that lj_cconv.c does for the interpreter. -** -** One major difference is that we can get away with much fewer checks -** here. E.g. checks for casts, constness or correct types can often be -** omitted, even if they might fail. The interpreter subsequently throws -** an error, which aborts the trace. -** -** All operations are specialized to their C types, so the on-trace -** outcome must be the same as the outcome in the interpreter. If the -** interpreter doesn't throw an error, then the trace is correct, too. -** Care must be taken not to generate invalid (temporary) IR or to -** trigger asserts. -*/ - -/* Determine whether a passed number or cdata number is non-zero. */ -static int crec_isnonzero(CType *s, void *p) -{ - if (p == (void *)0) - return 0; - if (p == (void *)1) - return 1; - if ((s->info & CTF_FP)) { - if (s->size == sizeof(float)) - return (*(float *)p != 0); - else - return (*(double *)p != 0); - } else { - if (s->size == 1) - return (*(uint8_t *)p != 0); - else if (s->size == 2) - return (*(uint16_t *)p != 0); - else if (s->size == 4) - return (*(uint32_t *)p != 0); - else - return (*(uint64_t *)p != 0); - } -} - -static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp, - void *svisnz) -{ - IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d); - IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s); - CTSize dsize = d->size, ssize = s->size; - CTInfo dinfo = d->info, sinfo = s->info; - - if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT) - goto err_conv; - - /* - ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and - ** numbers up to 8 bytes. Otherwise sp holds a pointer. - */ - - switch (cconv_idx2(dinfo, sinfo)) { - /* Destination is a bool. */ - case CCX(B, B): - goto xstore; /* Source operand is already normalized. */ - case CCX(B, I): - case CCX(B, F): - if (st != IRT_CDATA) { - /* Specialize to the result of a comparison against 0. */ - TRef zero = (st == IRT_NUM || st == IRT_FLOAT) ? lj_ir_knum(J, 0) : - (st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) : - lj_ir_kint(J, 0); - int isnz = crec_isnonzero(s, svisnz); - emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero); - sp = lj_ir_kint(J, isnz); - goto xstore; - } - goto err_nyi; - - /* Destination is an integer. */ - case CCX(I, B): - case CCX(I, I): - conv_I_I: - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - /* Extend 32 to 64 bit integer. */ - if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED))) - sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, - (sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT); - else if (dsize < 8 && ssize == 8) /* Truncate from 64 bit integer. */ - sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0); - else if (st == IRT_INT) - sp = lj_opt_narrow_toint(J, sp); - xstore: - if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J); - if (dp == 0) return sp; - emitir(IRT(IR_XSTORE, dt), dp, sp); - break; - case CCX(I, C): - sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */ - /* fallthrough */ - case CCX(I, F): - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_TRUNC|IRCONV_ANY); - goto xstore; - case CCX(I, P): - case CCX(I, A): - sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - ssize = CTSIZE_PTR; - st = IRT_UINTP; - if (((dsize ^ ssize) & 8) == 0) { /* Must insert no-op type conversion. */ - sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, IRT_PTR, 0); - goto xstore; - } - goto conv_I_I; - - /* Destination is a floating-point number. */ - case CCX(F, B): - case CCX(F, I): - conv_F_I: - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0); - goto xstore; - case CCX(F, C): - sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */ - /* fallthrough */ - case CCX(F, F): - conv_F_F: - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - if (dt != st) sp = emitconv(sp, dt, st, 0); - goto xstore; - - /* Destination is a complex number. */ - case CCX(C, I): - case CCX(C, F): - { /* Clear im. */ - TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1))); - emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0)); - } - /* Convert to re. */ - if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I; - - case CCX(C, C): - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - { - TRef re, im, ptr; - re = emitir(IRT(IR_XLOAD, st), sp, 0); - ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1))); - im = emitir(IRT(IR_XLOAD, st), ptr, 0); - if (dt != st) { - re = emitconv(re, dt, st, 0); - im = emitconv(im, dt, st, 0); - } - emitir(IRT(IR_XSTORE, dt), dp, re); - ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1))); - emitir(IRT(IR_XSTORE, dt), ptr, im); - } - break; - - /* Destination is a vector. */ - case CCX(V, I): - case CCX(V, F): - case CCX(V, C): - case CCX(V, V): - goto err_nyi; - - /* Destination is a pointer. */ - case CCX(P, P): - case CCX(P, A): - case CCX(P, S): - /* There are only 32 bit pointers/addresses on 32 bit machines. - ** Also ok on x64, since all 32 bit ops clear the upper part of the reg. - */ - goto xstore; - case CCX(P, I): - if (st == IRT_CDATA) goto err_nyi; - if (!LJ_64 && ssize == 8) /* Truncate from 64 bit integer. */ - sp = emitconv(sp, IRT_U32, st, 0); - goto xstore; - case CCX(P, F): - if (st == IRT_CDATA) goto err_nyi; - /* The signed conversion is cheaper. x64 really has 47 bit pointers. */ - sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32, - st, IRCONV_TRUNC|IRCONV_ANY); - goto xstore; - - /* Destination is an array. */ - case CCX(A, A): - /* Destination is a struct/union. */ - case CCX(S, S): - if (dp == 0) goto err_conv; - crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d); - break; - - default: - err_conv: - err_nyi: - lj_trace_err(J, LJ_TRERR_NYICONV); - break; - } - return 0; -} - -/* -- Convert C type to TValue (load) ------------------------------------- */ - -static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp) -{ - CTState *cts = ctype_ctsG(J2G(J)); - IRType t = crec_ct2irt(cts, s); - CTInfo sinfo = s->info; - if (ctype_isnum(sinfo)) { - TRef tr; - if (t == IRT_CDATA) - goto err_nyi; /* NYI: copyval of >64 bit integers. */ - tr = emitir(IRT(IR_XLOAD, t), sp, 0); - if (t == IRT_FLOAT || t == IRT_U32) { /* Keep uint32_t/float as numbers. */ - return emitconv(tr, IRT_NUM, t, 0); - } else if (t == IRT_I64 || t == IRT_U64) { /* Box 64 bit integer. */ - sp = tr; - lj_needsplit(J); - } else if ((sinfo & CTF_BOOL)) { - /* Assume not equal to zero. Fixup and emit pending guard later. */ - lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0)); - J->postproc = LJ_POST_FIXGUARD; - return TREF_TRUE; - } else { - return tr; - } - } else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) { - sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Box pointers and enums. */ - } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) { - cts->L = J->L; - sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR); /* Create ref. */ - } else if (ctype_iscomplex(sinfo)) { /* Unbox/box complex. */ - ptrdiff_t esz = (ptrdiff_t)(s->size >> 1); - TRef ptr, tr1, tr2, dp; - dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL); - tr1 = emitir(IRT(IR_XLOAD, t), sp, 0); - ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz)); - tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0); - ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata))); - emitir(IRT(IR_XSTORE, t), ptr, tr1); - ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz)); - emitir(IRT(IR_XSTORE, t), ptr, tr2); - return dp; - } else { - /* NYI: copyval of vectors. */ - err_nyi: - lj_trace_err(J, LJ_TRERR_NYICONV); - } - /* Box pointer, ref, enum or 64 bit integer. */ - return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp); -} - -/* -- Convert TValue to C type (store) ------------------------------------ */ - -static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTypeID sid = CTID_P_VOID; - void *svisnz = 0; - CType *s; - if (LJ_LIKELY(tref_isinteger(sp))) { - sid = CTID_INT32; - svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval)); - } else if (tref_isnum(sp)) { - sid = CTID_DOUBLE; - svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval)); - } else if (tref_isbool(sp)) { - sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0); - sid = CTID_BOOL; - } else if (tref_isnil(sp)) { - sp = lj_ir_kptr(J, NULL); - } else if (tref_isudata(sp)) { - GCudata *ud = udataV(sval); - if (ud->udtype == UDTYPE_IO_FILE) { - TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE)); - sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_UDATA_FILE); - } else { - sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCudata))); - } - } else if (tref_isstr(sp)) { - if (ctype_isenum(d->info)) { /* Match string against enum constant. */ - GCstr *str = strV(sval); - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, d, str, &ofs); - /* Specialize to the name of the enum constant. */ - emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str)); - if (cct && ctype_isconstval(cct->info)) { - lua_assert(ctype_child(cts, cct)->size == 4); - svisnz = (void *)(intptr_t)(ofs != 0); - sp = lj_ir_kint(J, (int32_t)ofs); - sid = ctype_cid(cct->info); - } /* else: interpreter will throw. */ - } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); /* NYI */ - } else { /* Otherwise pass the string data as a const char[]. */ - /* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */ - sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCstr))); - sid = CTID_A_CCHAR; - } - } else { /* NYI: tref_istab(sp), tref_islightud(sp). */ - IRType t; - sid = argv2cdata(J, sp, sval)->ctypeid; - s = ctype_raw(cts, sid); - svisnz = cdataptr(cdataV(sval)); - t = crec_ct2irt(cts, s); - if (ctype_isptr(s->info)) { - sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR); - if (ctype_isref(s->info)) { - svisnz = *(void **)svisnz; - s = ctype_rawchild(cts, s); - if (ctype_isenum(s->info)) s = ctype_child(cts, s); - t = crec_ct2irt(cts, s); - } else { - goto doconv; - } - } else if (t == IRT_I64 || t == IRT_U64) { - sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64); - lj_needsplit(J); - goto doconv; - } else if (t == IRT_INT || t == IRT_U32) { - if (ctype_isenum(s->info)) s = ctype_child(cts, s); - sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT); - goto doconv; - } else { - sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata))); - } - if (ctype_isnum(s->info) && t != IRT_CDATA) - sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Load number value. */ - goto doconv; - } - s = ctype_get(cts, sid); -doconv: - if (ctype_isenum(d->info)) d = ctype_child(cts, d); - return crec_ct_ct(J, d, s, dp, sp, svisnz); -} - -/* -- C data metamethods -------------------------------------------------- */ - -/* This would be rather difficult in FOLD, so do it here: -** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k) -** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz) -*/ -static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz) -{ - IRIns *ir = IR(tref_ref(tr)); - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) && - (ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) { - IRIns *irk = IR(ir->op2); - ptrdiff_t k; - if (LJ_64 && irk->o == IR_KINT64) - k = (ptrdiff_t)ir_kint64(irk)->u64 * sz; - else - k = (ptrdiff_t)irk->i * sz; - if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k; - tr = ir->op1; /* Not a TRef, but the caller doesn't care. */ - } - return tr; -} - -/* Record ctype __index/__newindex metamethods. */ -static void crec_index_meta(jit_State *J, CTState *cts, CType *ct, - RecordFFData *rd) -{ - CTypeID id = ctype_typeid(cts, ct); - cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index); - if (!tv) - lj_trace_err(J, LJ_TRERR_BADTYPE); - if (tvisfunc(tv)) { - J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME; - rd->nres = -1; /* Pending tailcall. */ - } else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) { - /* Specialize to result of __index lookup. */ - cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]); - J->base[0] = lj_record_constify(J, o); - if (!J->base[0]) - lj_trace_err(J, LJ_TRERR_BADTYPE); - /* Always specialize to the key. */ - emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1]))); - } else { - /* NYI: resolving of non-function metamethods. */ - /* NYI: non-string keys for __index table. */ - /* NYI: stores to __newindex table. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); - } -} - -void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd) -{ - TRef idx, ptr = J->base[0]; - ptrdiff_t ofs = sizeof(GCcdata); - GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]); - CTState *cts = ctype_ctsG(J2G(J)); - CType *ct = ctype_raw(cts, cd->ctypeid); - CTypeID sid = 0; - - /* Resolve pointer or reference for cdata object. */ - if (ctype_isptr(ct->info)) { - IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; - if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct); - ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR); - ofs = 0; - ptr = crec_reassoc_ofs(J, ptr, &ofs, 1); - } - -again: - idx = J->base[1]; - if (tref_isnumber(idx)) { - idx = lj_opt_narrow_cindex(J, idx); - if (ctype_ispointer(ct->info)) { - CTSize sz; - integer_key: - if ((ct->info & CTF_COMPLEX)) - idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1)); - sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info))); - idx = crec_reassoc_ofs(J, idx, &ofs, sz); -#if LJ_TARGET_ARM || LJ_TARGET_PPC - /* Hoist base add to allow fusion of index/shift into operands. */ - if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs -#if LJ_TARGET_ARM - && (sz == 1 || sz == 4) -#endif - ) { - ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); - ofs = 0; - } -#endif - idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz)); - ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr); - } - } else if (tref_iscdata(idx)) { - GCcdata *cdk = cdataV(&rd->argv[1]); - CType *ctk = ctype_raw(cts, cdk->ctypeid); - IRType t = crec_ct2irt(cts, ctk); - if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) { - if (ctk->size == 8) { - idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64); - } else if (ctk->size == 4) { - idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT); - } else { - idx = emitir(IRT(IR_ADD, IRT_PTR), idx, - lj_ir_kintp(J, sizeof(GCcdata))); - idx = emitir(IRT(IR_XLOAD, t), idx, 0); - } - if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED)) - idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT); - if (!LJ_64 && ctk->size > sizeof(intptr_t)) { - idx = emitconv(idx, IRT_INTP, t, 0); - lj_needsplit(J); - } - goto integer_key; - } - } else if (tref_isstr(idx)) { - GCstr *name = strV(&rd->argv[1]); - if (cd->ctypeid == CTID_CTYPEID) - ct = ctype_raw(cts, crec_constructor(J, cd, ptr)); - if (ctype_isstruct(ct->info)) { - CTSize fofs; - CType *fct; - fct = lj_ctype_getfield(cts, ct, name, &fofs); - if (fct) { - /* Always specialize to the field name. */ - emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name)); - if (ctype_isconstval(fct->info)) { - if (fct->size >= 0x80000000u && - (ctype_child(cts, fct)->info & CTF_UNSIGNED)) { - J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size); - return; - } - J->base[0] = lj_ir_kint(J, (int32_t)fct->size); - return; /* Interpreter will throw for newindex. */ - } else if (ctype_isbitfield(fct->info)) { - lj_trace_err(J, LJ_TRERR_NYICONV); - } else { - lua_assert(ctype_isfield(fct->info)); - sid = ctype_cid(fct->info); - } - ofs += (ptrdiff_t)fofs; - } - } else if (ctype_iscomplex(ct->info)) { - if (name->len == 2 && - ((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') || - (strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) { - /* Always specialize to the field name. */ - emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name)); - if (strdata(name)[0] == 'i') ofs += (ct->size >> 1); - sid = ctype_cid(ct->info); - } - } - } - if (!sid) { - if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */ - CType *cct = ctype_rawchild(cts, ct); - if (ctype_isstruct(cct->info)) { - ct = cct; - if (tref_isstr(idx)) goto again; - } - } - crec_index_meta(J, cts, ct, rd); - return; - } - - if (ofs) - ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); - - /* Resolve reference for field. */ - ct = ctype_get(cts, sid); - if (ctype_isref(ct->info)) - ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0); - - while (ctype_isattrib(ct->info)) - ct = ctype_child(cts, ct); /* Skip attributes. */ - - if (rd->data == 0) { /* __index metamethod. */ - J->base[0] = crec_tv_ct(J, ct, sid, ptr); - } else { /* __newindex metamethod. */ - rd->nres = 0; - J->needsnap = 1; - crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]); - } -} - -/* Record setting a finalizer. */ -static void crec_finalizer(jit_State *J, TRef trcd, cTValue *fin) -{ - TRef trlo = lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd); - TRef trhi = emitir(IRT(IR_ADD, IRT_P32), trlo, lj_ir_kint(J, 4)); - if (LJ_BE) { TRef tmp = trlo; trlo = trhi; trhi = tmp; } - if (tvisfunc(fin)) { - emitir(IRT(IR_XSTORE, IRT_P32), trlo, lj_ir_kfunc(J, funcV(fin))); - emitir(IRTI(IR_XSTORE), trhi, lj_ir_kint(J, LJ_TFUNC)); - } else if (tviscdata(fin)) { - emitir(IRT(IR_XSTORE, IRT_P32), trlo, - lj_ir_kgc(J, obj2gco(cdataV(fin)), IRT_CDATA)); - emitir(IRTI(IR_XSTORE), trhi, lj_ir_kint(J, LJ_TCDATA)); - } else { - lj_trace_err(J, LJ_TRERR_BADTYPE); - } - J->needsnap = 1; -} - -/* Record cdata allocation. */ -static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - CType *d = ctype_raw(cts, id); - TRef trid; - if (!sz || sz > 128 || (info & CTF_VLA) || ctype_align(info) > CT_MEMALIGN) - lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: large/special allocations. */ - trid = lj_ir_kint(J, id); - /* Use special instruction to box pointer or 32/64 bit integer. */ - if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) { - TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) : - ctype_isptr(info) ? lj_ir_kptr(J, NULL) : - sz == 4 ? lj_ir_kint(J, 0) : - (lj_needsplit(J), lj_ir_kint64(J, 0)); - J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp); - } else { - TRef trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, TREF_NIL); - cTValue *fin; - J->base[0] = trcd; - if (J->base[1] && !J->base[2] && - !lj_cconv_multi_init(cts, d, &rd->argv[1])) { - goto single_init; - } else if (ctype_isarray(d->info)) { - CType *dc = ctype_rawchild(cts, d); /* Array element type. */ - CTSize ofs, esize = dc->size; - TRef sp = 0; - TValue tv; - TValue *sval = &tv; - MSize i; - tv.u64 = 0; - if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info))) - lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init array of aggregates. */ - for (i = 1, ofs = 0; ofs < sz; ofs += esize) { - TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, - lj_ir_kintp(J, ofs + sizeof(GCcdata))); - if (J->base[i]) { - sp = J->base[i]; - sval = &rd->argv[i]; - i++; - } else if (i != 2) { - sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL; - } - crec_ct_tv(J, dc, dp, sp, sval); - } - } else if (ctype_isstruct(d->info)) { - CTypeID fid = d->sib; - MSize i = 1; - while (fid) { - CType *df = ctype_get(cts, fid); - fid = df->sib; - if (ctype_isfield(df->info)) { - CType *dc; - TRef sp, dp; - TValue tv; - TValue *sval = &tv; - setintV(&tv, 0); - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - dc = ctype_rawchild(cts, df); /* Field type. */ - if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) || - ctype_isenum(dc->info))) - lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init aggregates. */ - if (J->base[i]) { - sp = J->base[i]; - sval = &rd->argv[i]; - i++; - } else { - sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0); - } - dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, - lj_ir_kintp(J, df->size + sizeof(GCcdata))); - crec_ct_tv(J, dc, dp, sp, sval); - } else if (!ctype_isconstval(df->info)) { - /* NYI: init bitfields and sub-structures. */ - lj_trace_err(J, LJ_TRERR_NYICONV); - } - } - } else { - TRef dp; - single_init: - dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata))); - if (J->base[1]) { - crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]); - } else { - TValue tv; - tv.u64 = 0; - crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv); - } - } - /* Handle __gc metamethod. */ - fin = lj_ctype_meta(cts, id, MM_gc); - if (fin) - crec_finalizer(J, trcd, fin); - } -} - -/* Record argument conversions. */ -static TRef crec_call_args(jit_State *J, RecordFFData *rd, - CTState *cts, CType *ct) -{ - TRef args[CCI_NARGS_MAX]; - CTypeID fid; - MSize i, n; - TRef tr, *base; - cTValue *o; -#if LJ_TARGET_X86 -#if LJ_ABI_WIN - TRef *arg0 = NULL, *arg1 = NULL; -#endif - int ngpr = 0; - if (ctype_cconv(ct->info) == CTCC_THISCALL) - ngpr = 1; - else if (ctype_cconv(ct->info) == CTCC_FASTCALL) - ngpr = 2; -#endif - - /* Skip initial attributes. */ - fid = ct->sib; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) break; - fid = ctf->sib; - } - args[0] = TREF_NIL; - for (n = 0, base = J->base+1, o = rd->argv+1; *base; n++, base++, o++) { - CTypeID did; - CType *d; - - if (n >= CCI_NARGS_MAX) - lj_trace_err(J, LJ_TRERR_NYICALL); - - if (fid) { /* Get argument type from field. */ - CType *ctf = ctype_get(cts, fid); - fid = ctf->sib; - lua_assert(ctype_isfield(ctf->info)); - did = ctype_cid(ctf->info); - } else { - if (!(ct->info & CTF_VARARG)) - lj_trace_err(J, LJ_TRERR_NYICALL); /* Too many arguments. */ - did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */ - } - d = ctype_raw(cts, did); - if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || - ctype_isenum(d->info))) - lj_trace_err(J, LJ_TRERR_NYICALL); - tr = crec_ct_tv(J, d, 0, *base, o); - if (ctype_isinteger_or_bool(d->info)) { - if (d->size < 4) { - if ((d->info & CTF_UNSIGNED)) - tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_U8 : IRT_U16, 0); - else - tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_I8 : IRT_I16,IRCONV_SEXT); - } - } else if (LJ_SOFTFP && ctype_isfp(d->info) && d->size > 4) { - lj_needsplit(J); - } -#if LJ_TARGET_X86 - /* 64 bit args must not end up in registers for fastcall/thiscall. */ -#if LJ_ABI_WIN - if (!ctype_isfp(d->info)) { - /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */ - if (tref_typerange(tr, IRT_I64, IRT_U64)) { - if (ngpr) { - arg0 = &args[n]; args[n++] = TREF_NIL; ngpr--; - if (ngpr) { - arg1 = &args[n]; args[n++] = TREF_NIL; ngpr--; - } - } - } else { - if (arg0) { *arg0 = tr; arg0 = NULL; n--; continue; } - if (arg1) { *arg1 = tr; arg1 = NULL; n--; continue; } - if (ngpr) ngpr--; - } - } -#else - if (!ctype_isfp(d->info) && ngpr) { - if (tref_typerange(tr, IRT_I64, IRT_U64)) { - /* No reordering for other x86 ABIs. Simply add alignment args. */ - do { args[n++] = TREF_NIL; } while (--ngpr); - } else { - ngpr--; - } - } -#endif -#endif - args[n] = tr; - } - tr = args[0]; - for (i = 1; i < n; i++) - tr = emitir(IRT(IR_CARG, IRT_NIL), tr, args[i]); - return tr; -} - -/* Create a snapshot for the caller, simulating a 'false' return value. */ -static void crec_snap_caller(jit_State *J) -{ - lua_State *L = J->L; - TValue *base = L->base, *top = L->top; - const BCIns *pc = J->pc; - TRef ftr = J->base[-1]; - ptrdiff_t delta; - if (!frame_islua(base-1) || J->framedepth <= 0) - lj_trace_err(J, LJ_TRERR_NYICALL); - J->pc = frame_pc(base-1); delta = 1+bc_a(J->pc[-1]); - L->top = base; L->base = base - delta; - J->base[-1] = TREF_FALSE; - J->base -= delta; J->baseslot -= (BCReg)delta; - J->maxslot = (BCReg)delta; J->framedepth--; - lj_snap_add(J); - L->base = base; L->top = top; - J->framedepth++; J->maxslot = 1; - J->base += delta; J->baseslot += (BCReg)delta; - J->base[-1] = ftr; J->pc = pc; -} - -/* Record function call. */ -static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CType *ct = ctype_raw(cts, cd->ctypeid); - IRType tp = IRT_PTR; - if (ctype_isptr(ct->info)) { - tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; - ct = ctype_rawchild(cts, ct); - } - if (ctype_isfunc(ct->info)) { - TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR); - CType *ctr = ctype_rawchild(cts, ct); - IRType t = crec_ct2irt(cts, ctr); - TRef tr; - TValue tv; - /* Check for blacklisted C functions that might call a callback. */ - setlightudV(&tv, - cdata_getptr(cdataptr(cd), (LJ_64 && tp == IRT_P64) ? 8 : 4)); - if (tvistrue(lj_tab_get(J->L, cts->miscmap, &tv))) - lj_trace_err(J, LJ_TRERR_BLACKL); - if (ctype_isvoid(ctr->info)) { - t = IRT_NIL; - rd->nres = 0; - } else if (!(ctype_isnum(ctr->info) || ctype_isptr(ctr->info) || - ctype_isenum(ctr->info)) || t == IRT_CDATA) { - lj_trace_err(J, LJ_TRERR_NYICALL); - } - if ((ct->info & CTF_VARARG) -#if LJ_TARGET_X86 - || ctype_cconv(ct->info) != CTCC_CDECL -#endif - ) - func = emitir(IRT(IR_CARG, IRT_NIL), func, - lj_ir_kint(J, ctype_typeid(cts, ct))); - tr = emitir(IRT(IR_CALLXS, t), crec_call_args(J, rd, cts, ct), func); - if (ctype_isbool(ctr->info)) { - if (frame_islua(J->L->base-1) && bc_b(frame_pc(J->L->base-1)[-1]) == 1) { - /* Don't check result if ignored. */ - tr = TREF_NIL; - } else { - crec_snap_caller(J); -#if LJ_TARGET_X86ORX64 - /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */ - lj_ir_set(J, IRTG(IR_NE, IRT_U8), tr, lj_ir_kint(J, 0)); -#else - lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0)); -#endif - J->postproc = LJ_POST_FIXGUARDSNAP; - tr = TREF_TRUE; - } - } else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) || - t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) { - TRef trid = lj_ir_kint(J, ctype_cid(ct->info)); - tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr); - if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J); - } else if (t == IRT_FLOAT || t == IRT_U32) { - tr = emitconv(tr, IRT_NUM, t, 0); - } else if (t == IRT_I8 || t == IRT_I16) { - tr = emitconv(tr, IRT_INT, t, IRCONV_SEXT); - } else if (t == IRT_U8 || t == IRT_U16) { - tr = emitconv(tr, IRT_INT, t, 0); - } - J->base[0] = tr; - J->needsnap = 1; - return 1; - } - return 0; -} - -void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]); - CTypeID id = cd->ctypeid; - CType *ct; - cTValue *tv; - MMS mm = MM_call; - if (id == CTID_CTYPEID) { - id = crec_constructor(J, cd, J->base[0]); - mm = MM_new; - } else if (crec_call(J, rd, cd)) { - return; - } - /* Record ctype __call/__new metamethod. */ - ct = ctype_raw(cts, id); - tv = lj_ctype_meta(cts, ctype_isptr(ct->info) ? ctype_cid(ct->info) : id, mm); - if (tv) { - if (tvisfunc(tv)) { - J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME; - rd->nres = -1; /* Pending tailcall. */ - return; - } - } else if (mm == MM_new) { - crec_alloc(J, rd, id); - return; - } - /* No metamethod or NYI: non-function metamethods. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); -} - -static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm) -{ - if (ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) { - IRType dt; - CTypeID id; - TRef tr; - MSize i; - IROp op; - lj_needsplit(J); - if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) || - ((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) { - dt = IRT_U64; id = CTID_UINT64; - } else { - dt = IRT_I64; id = CTID_INT64; - if (mm < MM_add && - !((s[0]->info | s[1]->info) & CTF_FP) && - s[0]->size == 4 && s[1]->size == 4) { /* Try to narrow comparison. */ - if (!((s[0]->info ^ s[1]->info) & CTF_UNSIGNED) || - (tref_isk(sp[1]) && IR(tref_ref(sp[1]))->i >= 0)) { - dt = (s[0]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT; - goto comp; - } else if (tref_isk(sp[0]) && IR(tref_ref(sp[0]))->i >= 0) { - dt = (s[1]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT; - goto comp; - } - } - } - for (i = 0; i < 2; i++) { - IRType st = tref_type(sp[i]); - if (st == IRT_NUM || st == IRT_FLOAT) - sp[i] = emitconv(sp[i], dt, st, IRCONV_TRUNC|IRCONV_ANY); - else if (!(st == IRT_I64 || st == IRT_U64)) - sp[i] = emitconv(sp[i], dt, IRT_INT, - (s[i]->info & CTF_UNSIGNED) ? 0 : IRCONV_SEXT); - } - if (mm < MM_add) { - comp: - /* Assume true comparison. Fixup and emit pending guard later. */ - if (mm == MM_eq) { - op = IR_EQ; - } else { - op = mm == MM_lt ? IR_LT : IR_LE; - if (dt == IRT_U32 || dt == IRT_U64) - op += (IR_ULT-IR_LT); - } - lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]); - J->postproc = LJ_POST_FIXGUARD; - return TREF_TRUE; - } else { - tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]); - } - return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); - } - return 0; -} - -static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CType *ctp = s[0]; - if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) { - if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) && - (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) { - if (mm == MM_sub) { /* Pointer difference. */ - TRef tr; - CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info)); - if (sz == 0 || (sz & (sz-1)) != 0) - return 0; /* NYI: integer division. */ - tr = emitir(IRT(IR_SUB, IRT_INTP), sp[0], sp[1]); - tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz))); -#if LJ_64 - tr = emitconv(tr, IRT_NUM, IRT_INTP, 0); -#endif - return tr; - } else { /* Pointer comparison (unsigned). */ - /* Assume true comparison. Fixup and emit pending guard later. */ - IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE; - lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]); - J->postproc = LJ_POST_FIXGUARD; - return TREF_TRUE; - } - } - if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info))) - return 0; - } else if (mm == MM_add && ctype_isnum(ctp->info) && - (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) { - TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr; /* Swap pointer and index. */ - ctp = s[1]; - } else { - return 0; - } - { - TRef tr = sp[1]; - IRType t = tref_type(tr); - CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info)); - CTypeID id; -#if LJ_64 - if (t == IRT_NUM || t == IRT_FLOAT) - tr = emitconv(tr, IRT_INTP, t, IRCONV_TRUNC|IRCONV_ANY); - else if (!(t == IRT_I64 || t == IRT_U64)) - tr = emitconv(tr, IRT_INTP, IRT_INT, - ((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT); -#else - if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) { - tr = emitconv(tr, IRT_INTP, t, - (t == IRT_NUM || t == IRT_FLOAT) ? - IRCONV_TRUNC|IRCONV_ANY : 0); - } -#endif - tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz)); - tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, IRT_PTR), sp[0], tr); - id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), - CTSIZE_PTR); - return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); - } -} - -/* Record ctype arithmetic metamethods. */ -static void crec_arith_meta(jit_State *J, CTState *cts, RecordFFData *rd) -{ - cTValue *tv = NULL; - if (J->base[0]) { - if (tviscdata(&rd->argv[0])) { - CTypeID id = argv2cdata(J, J->base[0], &rd->argv[0])->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, (MMS)rd->data); - } - if (!tv && J->base[1] && tviscdata(&rd->argv[1])) { - CTypeID id = argv2cdata(J, J->base[1], &rd->argv[1])->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, (MMS)rd->data); - } - } - if (tv) { - if (tvisfunc(tv)) { - J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME; - rd->nres = -1; /* Pending tailcall. */ - return; - } /* NYI: non-function metamethods. */ - } else if ((MMS)rd->data == MM_eq) { - J->base[0] = TREF_FALSE; - return; - } - lj_trace_err(J, LJ_TRERR_BADTYPE); -} - -void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef sp[2]; - CType *s[2]; - MSize i; - for (i = 0; i < 2; i++) { - TRef tr = J->base[i]; - CType *ct = ctype_get(cts, CTID_DOUBLE); - if (!tr) { - goto trymeta; - } else if (tref_iscdata(tr)) { - CTypeID id = argv2cdata(J, tr, &rd->argv[i])->ctypeid; - IRType t; - ct = ctype_raw(cts, id); - t = crec_ct2irt(cts, ct); - if (ctype_isptr(ct->info)) { /* Resolve pointer or reference. */ - tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR); - if (ctype_isref(ct->info)) { - ct = ctype_rawchild(cts, ct); - t = crec_ct2irt(cts, ct); - } - } else if (t == IRT_I64 || t == IRT_U64) { - tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64); - lj_needsplit(J); - goto ok; - } else if (t == IRT_INT || t == IRT_U32) { - tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT); - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - goto ok; - } else if (ctype_isfunc(ct->info)) { - tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR); - ct = ctype_get(cts, - lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR)); - goto ok; - } else { - tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata))); - } - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (ctype_isnum(ct->info)) { - if (t == IRT_CDATA) goto trymeta; - if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J); - tr = emitir(IRT(IR_XLOAD, t), tr, 0); - } else if (!(ctype_isptr(ct->info) || ctype_isrefarray(ct->info))) { - goto trymeta; - } - } else if (tref_isnil(tr)) { - tr = lj_ir_kptr(J, NULL); - ct = ctype_get(cts, CTID_P_VOID); - } else if (tref_isinteger(tr)) { - ct = ctype_get(cts, CTID_INT32); - } else if (tref_isstr(tr)) { - TRef tr2 = J->base[1-i]; - CTypeID id = argv2cdata(J, tr2, &rd->argv[1-i])->ctypeid; - ct = ctype_raw(cts, id); - if (ctype_isenum(ct->info)) { /* Match string against enum constant. */ - GCstr *str = strV(&rd->argv[i]); - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, ct, str, &ofs); - if (cct && ctype_isconstval(cct->info)) { - /* Specialize to the name of the enum constant. */ - emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, str)); - ct = ctype_child(cts, cct); - tr = lj_ir_kint(J, (int32_t)ofs); - } /* else: interpreter will throw. */ - } /* else: interpreter will throw. */ - } else if (!tref_isnum(tr)) { - goto trymeta; - } - ok: - s[i] = ct; - sp[i] = tr; - } - { - TRef tr; - if ((tr = crec_arith_int64(J, sp, s, (MMS)rd->data)) || - (tr = crec_arith_ptr(J, sp, s, (MMS)rd->data))) { - J->base[0] = tr; - /* Fixup cdata comparisons, too. Avoids some cdata escapes. */ - if (J->postproc == LJ_POST_FIXGUARD && frame_iscont(J->L->base-1) && - !irt_isguard(J->guardemit)) { - const BCIns *pc = frame_contpc(J->L->base-1) - 1; - if (bc_op(*pc) <= BC_ISNEP) { - setframe_pc(&J2G(J)->tmptv, pc); - J2G(J)->tmptv.u32.lo = ((tref_istrue(tr) ^ bc_op(*pc)) & 1); - J->postproc = LJ_POST_FIXCOMP; - } - } - } else { - trymeta: - crec_arith_meta(J, cts, rd); - } - } -} - -/* -- C library namespace metamethods ------------------------------------- */ - -void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - if (tref_isudata(J->base[0]) && tref_isstr(J->base[1]) && - udataV(&rd->argv[0])->udtype == UDTYPE_FFI_CLIB) { - CLibrary *cl = (CLibrary *)uddata(udataV(&rd->argv[0])); - GCstr *name = strV(&rd->argv[1]); - CType *ct; - CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX); - cTValue *tv = lj_tab_getstr(cl->cache, name); - rd->nres = rd->data; - if (id && tv && !tvisnil(tv)) { - /* Specialize to the symbol name and make the result a constant. */ - emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, name)); - if (ctype_isconstval(ct->info)) { - if (ct->size >= 0x80000000u && - (ctype_child(cts, ct)->info & CTF_UNSIGNED)) - J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)ct->size); - else - J->base[0] = lj_ir_kint(J, (int32_t)ct->size); - } else if (ctype_isextern(ct->info)) { - CTypeID sid = ctype_cid(ct->info); - void *sp = *(void **)cdataptr(cdataV(tv)); - TRef ptr; - ct = ctype_raw(cts, sid); - if (LJ_64 && !checkptr32(sp)) - ptr = lj_ir_kintp(J, (uintptr_t)sp); - else - ptr = lj_ir_kptr(J, sp); - if (rd->data) { - J->base[0] = crec_tv_ct(J, ct, sid, ptr); - } else { - J->needsnap = 1; - crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]); - } - } else { - J->base[0] = lj_ir_kgc(J, obj2gco(cdataV(tv)), IRT_CDATA); - } - } else { - lj_trace_err(J, LJ_TRERR_NOCACHE); - } - } /* else: interpreter will throw. */ -} - -/* -- FFI library functions ----------------------------------------------- */ - -static TRef crec_toint(jit_State *J, CTState *cts, TRef sp, TValue *sval) -{ - return crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, sp, sval); -} - -void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd) -{ - crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0])); -} - -void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd) -{ - UNUSED(rd); - if (J->base[0]) - lj_trace_err(J, LJ_TRERR_NYICALL); - J->base[0] = lj_ir_call(J, IRCALL_lj_vm_errno); -} - -void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef tr = J->base[0]; - if (tr) { - TRef trlen = J->base[1]; - if (trlen) { - trlen = crec_toint(J, cts, trlen, &rd->argv[1]); - tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]); - } else { - tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]); - trlen = lj_ir_call(J, IRCALL_strlen, tr); - } - J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), tr, trlen); - } /* else: interpreter will throw. */ -} - -void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef trdst = J->base[0], trsrc = J->base[1], trlen = J->base[2]; - if (trdst && trsrc && (trlen || tref_isstr(trsrc))) { - trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]); - trsrc = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, trsrc, &rd->argv[1]); - if (trlen) { - trlen = crec_toint(J, cts, trlen, &rd->argv[2]); - } else { - trlen = emitir(IRTI(IR_FLOAD), J->base[1], IRFL_STR_LEN); - trlen = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); - } - rd->nres = 0; - crec_copy(J, trdst, trsrc, trlen, NULL); - } /* else: interpreter will throw. */ -} - -void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef trdst = J->base[0], trlen = J->base[1], trfill = J->base[2]; - if (trdst && trlen) { - CTSize step = 1; - if (tviscdata(&rd->argv[0])) { /* Get alignment of original destination. */ - CTSize sz; - CType *ct = ctype_raw(cts, cdataV(&rd->argv[0])->ctypeid); - if (ctype_isptr(ct->info)) - ct = ctype_rawchild(cts, ct); - step = (1u<argv[0]); - trlen = crec_toint(J, cts, trlen, &rd->argv[1]); - if (trfill) - trfill = crec_toint(J, cts, trfill, &rd->argv[2]); - else - trfill = lj_ir_kint(J, 0); - rd->nres = 0; - crec_fill(J, trdst, trlen, trfill, step); - } /* else: interpreter will throw. */ -} - -void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd) -{ - if (tref_iscdata(J->base[0])) { - TRef trid = lj_ir_kint(J, argv2ctype(J, J->base[0], &rd->argv[0])); - J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), - lj_ir_kint(J, CTID_CTYPEID), trid); - } else { - setfuncV(J->L, &J->errinfo, J->fn); - lj_trace_err_info(J, LJ_TRERR_NYIFFU); - } -} - -void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd) -{ - argv2ctype(J, J->base[0], &rd->argv[0]); - if (tref_iscdata(J->base[1])) { - argv2ctype(J, J->base[1], &rd->argv[1]); - J->postproc = LJ_POST_FIXBOOL; - J->base[0] = TREF_TRUE; - } else { - J->base[0] = TREF_FALSE; - } -} - -void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd) -{ - if (tref_isstr(J->base[0])) { - /* Specialize to the ABI string to make the boolean result a constant. */ - emitir(IRTG(IR_EQ, IRT_STR), J->base[0], lj_ir_kstr(J, strV(&rd->argv[0]))); - J->postproc = LJ_POST_FIXBOOL; - J->base[0] = TREF_TRUE; - } else { - lj_trace_err(J, LJ_TRERR_BADTYPE); - } -} - -/* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */ -void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd) -{ - CTypeID id = argv2ctype(J, J->base[0], &rd->argv[0]); - if (rd->data == FF_ffi_sizeof) { - CType *ct = lj_ctype_rawref(ctype_ctsG(J2G(J)), id); - if (ctype_isvltype(ct->info)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - } else if (rd->data == FF_ffi_offsetof) { /* Specialize to the field name. */ - if (!tref_isstr(J->base[1])) - lj_trace_err(J, LJ_TRERR_BADTYPE); - emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1]))); - rd->nres = 3; /* Just in case. */ - } - J->postproc = LJ_POST_FIXCONST; - J->base[0] = J->base[1] = J->base[2] = TREF_NIL; -} - -void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd) -{ - argv2cdata(J, J->base[0], &rd->argv[0]); - crec_finalizer(J, J->base[0], &rd->argv[1]); -} - -/* -- Miscellaneous library functions ------------------------------------- */ - -void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->ctypeid); - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) { - if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 && - !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) - d = ctype_get(cts, CTID_INT32); - else - d = ctype_get(cts, CTID_DOUBLE); - J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]); - } else { - J->base[0] = TREF_NIL; - } -} - -#undef IR -#undef emitir -#undef emitconv - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_crecord.h b/third-party/LuaJIT-2.0.2/src/lj_crecord.h deleted file mode 100644 index dea05f78ab..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_crecord.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -** Trace recorder for C data operations. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CRECORD_H -#define _LJ_CRECORD_H - -#include "lj_obj.h" -#include "lj_jit.h" -#include "lj_ffrecord.h" - -#if LJ_HASJIT && LJ_HASFFI -LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ctype.c b/third-party/LuaJIT-2.0.2/src/lj_ctype.c deleted file mode 100644 index 57a0d7cc81..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ctype.c +++ /dev/null @@ -1,634 +0,0 @@ -/* -** C type management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_ccallback.h" - -/* -- C type definitions -------------------------------------------------- */ - -/* Predefined typedefs. */ -#define CTTDDEF(_) \ - /* Vararg handling. */ \ - _("va_list", P_VOID) \ - _("__builtin_va_list", P_VOID) \ - _("__gnuc_va_list", P_VOID) \ - /* From stddef.h. */ \ - _("ptrdiff_t", INT_PSZ) \ - _("size_t", UINT_PSZ) \ - _("wchar_t", WCHAR) \ - /* Subset of stdint.h. */ \ - _("int8_t", INT8) \ - _("int16_t", INT16) \ - _("int32_t", INT32) \ - _("int64_t", INT64) \ - _("uint8_t", UINT8) \ - _("uint16_t", UINT16) \ - _("uint32_t", UINT32) \ - _("uint64_t", UINT64) \ - _("intptr_t", INT_PSZ) \ - _("uintptr_t", UINT_PSZ) \ - /* End of typedef list. */ - -/* Keywords (only the ones we actually care for). */ -#define CTKWDEF(_) \ - /* Type specifiers. */ \ - _("void", -1, CTOK_VOID) \ - _("_Bool", 0, CTOK_BOOL) \ - _("bool", 1, CTOK_BOOL) \ - _("char", 1, CTOK_CHAR) \ - _("int", 4, CTOK_INT) \ - _("__int8", 1, CTOK_INT) \ - _("__int16", 2, CTOK_INT) \ - _("__int32", 4, CTOK_INT) \ - _("__int64", 8, CTOK_INT) \ - _("float", 4, CTOK_FP) \ - _("double", 8, CTOK_FP) \ - _("long", 0, CTOK_LONG) \ - _("short", 0, CTOK_SHORT) \ - _("_Complex", 0, CTOK_COMPLEX) \ - _("complex", 0, CTOK_COMPLEX) \ - _("__complex", 0, CTOK_COMPLEX) \ - _("__complex__", 0, CTOK_COMPLEX) \ - _("signed", 0, CTOK_SIGNED) \ - _("__signed", 0, CTOK_SIGNED) \ - _("__signed__", 0, CTOK_SIGNED) \ - _("unsigned", 0, CTOK_UNSIGNED) \ - /* Type qualifiers. */ \ - _("const", 0, CTOK_CONST) \ - _("__const", 0, CTOK_CONST) \ - _("__const__", 0, CTOK_CONST) \ - _("volatile", 0, CTOK_VOLATILE) \ - _("__volatile", 0, CTOK_VOLATILE) \ - _("__volatile__", 0, CTOK_VOLATILE) \ - _("restrict", 0, CTOK_RESTRICT) \ - _("__restrict", 0, CTOK_RESTRICT) \ - _("__restrict__", 0, CTOK_RESTRICT) \ - _("inline", 0, CTOK_INLINE) \ - _("__inline", 0, CTOK_INLINE) \ - _("__inline__", 0, CTOK_INLINE) \ - /* Storage class specifiers. */ \ - _("typedef", 0, CTOK_TYPEDEF) \ - _("extern", 0, CTOK_EXTERN) \ - _("static", 0, CTOK_STATIC) \ - _("auto", 0, CTOK_AUTO) \ - _("register", 0, CTOK_REGISTER) \ - /* GCC Attributes. */ \ - _("__extension__", 0, CTOK_EXTENSION) \ - _("__attribute", 0, CTOK_ATTRIBUTE) \ - _("__attribute__", 0, CTOK_ATTRIBUTE) \ - _("asm", 0, CTOK_ASM) \ - _("__asm", 0, CTOK_ASM) \ - _("__asm__", 0, CTOK_ASM) \ - /* MSVC Attributes. */ \ - _("__declspec", 0, CTOK_DECLSPEC) \ - _("__cdecl", CTCC_CDECL, CTOK_CCDECL) \ - _("__thiscall", CTCC_THISCALL, CTOK_CCDECL) \ - _("__fastcall", CTCC_FASTCALL, CTOK_CCDECL) \ - _("__stdcall", CTCC_STDCALL, CTOK_CCDECL) \ - _("__ptr32", 4, CTOK_PTRSZ) \ - _("__ptr64", 8, CTOK_PTRSZ) \ - /* Other type specifiers. */ \ - _("struct", 0, CTOK_STRUCT) \ - _("union", 0, CTOK_UNION) \ - _("enum", 0, CTOK_ENUM) \ - /* Operators. */ \ - _("sizeof", 0, CTOK_SIZEOF) \ - _("__alignof", 0, CTOK_ALIGNOF) \ - _("__alignof__", 0, CTOK_ALIGNOF) \ - /* End of keyword list. */ - -/* Type info for predefined types. Size merged in. */ -static CTInfo lj_ctype_typeinfo[] = { -#define CTTYINFODEF(id, sz, ct, info) CTINFO((ct),(((sz)&0x3fu)<<10)+(info)), -#define CTTDINFODEF(name, id) CTINFO(CT_TYPEDEF, CTID_##id), -#define CTKWINFODEF(name, sz, kw) CTINFO(CT_KW,(((sz)&0x3fu)<<10)+(kw)), -CTTYDEF(CTTYINFODEF) -CTTDDEF(CTTDINFODEF) -CTKWDEF(CTKWINFODEF) -#undef CTTYINFODEF -#undef CTTDINFODEF -#undef CTKWINFODEF - 0 -}; - -/* Predefined type names collected in a single string. */ -static const char * const lj_ctype_typenames = -#define CTTDNAMEDEF(name, id) name "\0" -#define CTKWNAMEDEF(name, sz, cds) name "\0" -CTTDDEF(CTTDNAMEDEF) -CTKWDEF(CTKWNAMEDEF) -#undef CTTDNAMEDEF -#undef CTKWNAMEDEF -; - -#define CTTYPEINFO_NUM (sizeof(lj_ctype_typeinfo)/sizeof(CTInfo)-1) -#ifdef LUAJIT_CTYPE_CHECK_ANCHOR -#define CTTYPETAB_MIN CTTYPEINFO_NUM -#else -#define CTTYPETAB_MIN 128 -#endif - -/* -- C type interning ---------------------------------------------------- */ - -#define ct_hashtype(info, size) (hashrot(info, size) & CTHASH_MASK) -#define ct_hashname(name) \ - (hashrot(u32ptr(name), u32ptr(name) + HASH_BIAS) & CTHASH_MASK) - -/* Create new type element. */ -CTypeID lj_ctype_new(CTState *cts, CType **ctp) -{ - CTypeID id = cts->top; - CType *ct; - lua_assert(cts->L); - if (LJ_UNLIKELY(id >= cts->sizetab)) { - if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); -#ifdef LUAJIT_CTYPE_CHECK_ANCHOR - ct = lj_mem_newvec(cts->L, id+1, CType); - memcpy(ct, cts->tab, id*sizeof(CType)); - memset(cts->tab, 0, id*sizeof(CType)); - lj_mem_freevec(cts->g, cts->tab, cts->sizetab, CType); - cts->tab = ct; - cts->sizetab = id+1; -#else - lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); -#endif - } - cts->top = id+1; - *ctp = ct = &cts->tab[id]; - ct->info = 0; - ct->size = 0; - ct->sib = 0; - ct->next = 0; - setgcrefnull(ct->name); - return id; -} - -/* Intern a type element. */ -CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size) -{ - uint32_t h = ct_hashtype(info, size); - CTypeID id = cts->hash[h]; - lua_assert(cts->L); - while (id) { - CType *ct = ctype_get(cts, id); - if (ct->info == info && ct->size == size) - return id; - id = ct->next; - } - id = cts->top; - if (LJ_UNLIKELY(id >= cts->sizetab)) { - if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); - lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); - } - cts->top = id+1; - cts->tab[id].info = info; - cts->tab[id].size = size; - cts->tab[id].sib = 0; - cts->tab[id].next = cts->hash[h]; - setgcrefnull(cts->tab[id].name); - cts->hash[h] = (CTypeID1)id; - return id; -} - -/* Add type element to hash table. */ -static void ctype_addtype(CTState *cts, CType *ct, CTypeID id) -{ - uint32_t h = ct_hashtype(ct->info, ct->size); - ct->next = cts->hash[h]; - cts->hash[h] = (CTypeID1)id; -} - -/* Add named element to hash table. */ -void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id) -{ - uint32_t h = ct_hashname(gcref(ct->name)); - ct->next = cts->hash[h]; - cts->hash[h] = (CTypeID1)id; -} - -/* Get a C type by name, matching the type mask. */ -CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, uint32_t tmask) -{ - CTypeID id = cts->hash[ct_hashname(name)]; - while (id) { - CType *ct = ctype_get(cts, id); - if (gcref(ct->name) == obj2gco(name) && - ((tmask >> ctype_type(ct->info)) & 1)) { - *ctp = ct; - return id; - } - id = ct->next; - } - *ctp = &cts->tab[0]; /* Simplify caller logic. ctype_get() would assert. */ - return 0; -} - -/* Get a struct/union/enum/function field by name. */ -CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, CTSize *ofs, - CTInfo *qual) -{ - while (ct->sib) { - ct = ctype_get(cts, ct->sib); - if (gcref(ct->name) == obj2gco(name)) { - *ofs = ct->size; - return ct; - } - if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - CType *fct, *cct = ctype_child(cts, ct); - CTInfo q = 0; - while (ctype_isattrib(cct->info)) { - if (ctype_attrib(cct->info) == CTA_QUAL) q |= cct->size; - cct = ctype_child(cts, cct); - } - fct = lj_ctype_getfieldq(cts, cct, name, ofs, qual); - if (fct) { - if (qual) *qual |= q; - *ofs += ct->size; - return fct; - } - } - } - return NULL; /* Not found. */ -} - -/* -- C type information -------------------------------------------------- */ - -/* Follow references and get raw type for a C type ID. */ -CType *lj_ctype_rawref(CTState *cts, CTypeID id) -{ - CType *ct = ctype_get(cts, id); - while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) - ct = ctype_child(cts, ct); - return ct; -} - -/* Get size for a C type ID. Does NOT support VLA/VLS. */ -CTSize lj_ctype_size(CTState *cts, CTypeID id) -{ - CType *ct = ctype_raw(cts, id); - return ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; -} - -/* Get size for a variable-length C type. Does NOT support other C types. */ -CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem) -{ - uint64_t xsz = 0; - if (ctype_isstruct(ct->info)) { - CTypeID arrid = 0, fid = ct->sib; - xsz = ct->size; /* Add the struct size. */ - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (ctype_type(ctf->info) == CT_FIELD) - arrid = ctype_cid(ctf->info); /* Remember last field of VLS. */ - fid = ctf->sib; - } - ct = ctype_raw(cts, arrid); - } - lua_assert(ctype_isvlarray(ct->info)); /* Must be a VLA. */ - ct = ctype_rawchild(cts, ct); /* Get array element. */ - lua_assert(ctype_hassize(ct->info)); - /* Calculate actual size of VLA and check for overflow. */ - xsz += (uint64_t)ct->size * nelem; - return xsz < 0x80000000u ? (CTSize)xsz : CTSIZE_INVALID; -} - -/* Get type, qualifiers, size and alignment for a C type ID. */ -CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp) -{ - CTInfo qual = 0; - CType *ct = ctype_get(cts, id); - for (;;) { - CTInfo info = ct->info; - if (ctype_isenum(info)) { - /* Follow child. Need to look at its attributes, too. */ - } else if (ctype_isattrib(info)) { - if (ctype_isxattrib(info, CTA_QUAL)) - qual |= ct->size; - else if (ctype_isxattrib(info, CTA_ALIGN) && !(qual & CTFP_ALIGNED)) - qual |= CTFP_ALIGNED + CTALIGN(ct->size); - } else { - if (!(qual & CTFP_ALIGNED)) qual |= (info & CTF_ALIGN); - qual |= (info & ~(CTF_ALIGN|CTMASK_CID)); - lua_assert(ctype_hassize(info) || ctype_isfunc(info)); - *szp = ctype_isfunc(info) ? CTSIZE_INVALID : ct->size; - break; - } - ct = ctype_get(cts, ctype_cid(info)); - } - return qual; -} - -/* Get ctype metamethod. */ -cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm) -{ - CType *ct = ctype_get(cts, id); - cTValue *tv; - while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) { - id = ctype_cid(ct->info); - ct = ctype_get(cts, id); - } - if (ctype_isptr(ct->info) && - ctype_isfunc(ctype_get(cts, ctype_cid(ct->info))->info)) - tv = lj_tab_getstr(cts->miscmap, &cts->g->strempty); - else - tv = lj_tab_getinth(cts->miscmap, -(int32_t)id); - if (tv && tvistab(tv) && - (tv = lj_tab_getstr(tabV(tv), mmname_str(cts->g, mm))) && !tvisnil(tv)) - return tv; - return NULL; -} - -/* -- C type representation ----------------------------------------------- */ - -/* Fixed max. length of a C type representation. */ -#define CTREPR_MAX 512 - -typedef struct CTRepr { - char *pb, *pe; - CTState *cts; - lua_State *L; - int needsp; - int ok; - char buf[CTREPR_MAX]; -} CTRepr; - -/* Prepend string. */ -static void ctype_prepstr(CTRepr *ctr, const char *str, MSize len) -{ - char *p = ctr->pb; - if (ctr->buf + len+1 > p) { ctr->ok = 0; return; } - if (ctr->needsp) *--p = ' '; - ctr->needsp = 1; - p -= len; - while (len-- > 0) p[len] = str[len]; - ctr->pb = p; -} - -#define ctype_preplit(ctr, str) ctype_prepstr((ctr), "" str, sizeof(str)-1) - -/* Prepend char. */ -static void ctype_prepc(CTRepr *ctr, int c) -{ - if (ctr->buf >= ctr->pb) { ctr->ok = 0; return; } - *--ctr->pb = c; -} - -/* Prepend number. */ -static void ctype_prepnum(CTRepr *ctr, uint32_t n) -{ - char *p = ctr->pb; - if (ctr->buf + 10+1 > p) { ctr->ok = 0; return; } - do { *--p = (char)('0' + n % 10); } while (n /= 10); - ctr->pb = p; - ctr->needsp = 0; -} - -/* Append char. */ -static void ctype_appc(CTRepr *ctr, int c) -{ - if (ctr->pe >= ctr->buf + CTREPR_MAX) { ctr->ok = 0; return; } - *ctr->pe++ = c; -} - -/* Append number. */ -static void ctype_appnum(CTRepr *ctr, uint32_t n) -{ - char buf[10]; - char *p = buf+sizeof(buf); - char *q = ctr->pe; - if (q > ctr->buf + CTREPR_MAX - 10) { ctr->ok = 0; return; } - do { *--p = (char)('0' + n % 10); } while (n /= 10); - do { *q++ = *p++; } while (p < buf+sizeof(buf)); - ctr->pe = q; -} - -/* Prepend qualifiers. */ -static void ctype_prepqual(CTRepr *ctr, CTInfo info) -{ - if ((info & CTF_VOLATILE)) ctype_preplit(ctr, "volatile"); - if ((info & CTF_CONST)) ctype_preplit(ctr, "const"); -} - -/* Prepend named type. */ -static void ctype_preptype(CTRepr *ctr, CType *ct, CTInfo qual, const char *t) -{ - if (gcref(ct->name)) { - GCstr *str = gco2str(gcref(ct->name)); - ctype_prepstr(ctr, strdata(str), str->len); - } else { - if (ctr->needsp) ctype_prepc(ctr, ' '); - ctype_prepnum(ctr, ctype_typeid(ctr->cts, ct)); - ctr->needsp = 1; - } - ctype_prepstr(ctr, t, (MSize)strlen(t)); - ctype_prepqual(ctr, qual); -} - -static void ctype_repr(CTRepr *ctr, CTypeID id) -{ - CType *ct = ctype_get(ctr->cts, id); - CTInfo qual = 0; - int ptrto = 0; - for (;;) { - CTInfo info = ct->info; - CTSize size = ct->size; - switch (ctype_type(info)) { - case CT_NUM: - if ((info & CTF_BOOL)) { - ctype_preplit(ctr, "bool"); - } else if ((info & CTF_FP)) { - if (size == sizeof(double)) ctype_preplit(ctr, "double"); - else if (size == sizeof(float)) ctype_preplit(ctr, "float"); - else ctype_preplit(ctr, "long double"); - } else if (size == 1) { - if (!((info ^ CTF_UCHAR) & CTF_UNSIGNED)) ctype_preplit(ctr, "char"); - else if (CTF_UCHAR) ctype_preplit(ctr, "signed char"); - else ctype_preplit(ctr, "unsigned char"); - } else if (size < 8) { - if (size == 4) ctype_preplit(ctr, "int"); - else ctype_preplit(ctr, "short"); - if ((info & CTF_UNSIGNED)) ctype_preplit(ctr, "unsigned"); - } else { - ctype_preplit(ctr, "_t"); - ctype_prepnum(ctr, size*8); - ctype_preplit(ctr, "int"); - if ((info & CTF_UNSIGNED)) ctype_prepc(ctr, 'u'); - } - ctype_prepqual(ctr, (qual|info)); - return; - case CT_VOID: - ctype_preplit(ctr, "void"); - ctype_prepqual(ctr, (qual|info)); - return; - case CT_STRUCT: - ctype_preptype(ctr, ct, qual, (info & CTF_UNION) ? "union" : "struct"); - return; - case CT_ENUM: - if (id == CTID_CTYPEID) { - ctype_preplit(ctr, "ctype"); - return; - } - ctype_preptype(ctr, ct, qual, "enum"); - return; - case CT_ATTRIB: - if (ctype_attrib(info) == CTA_QUAL) qual |= size; - break; - case CT_PTR: - if ((info & CTF_REF)) { - ctype_prepc(ctr, '&'); - } else { - ctype_prepqual(ctr, (qual|info)); - if (LJ_64 && size == 4) ctype_preplit(ctr, "__ptr32"); - ctype_prepc(ctr, '*'); - } - qual = 0; - ptrto = 1; - ctr->needsp = 1; - break; - case CT_ARRAY: - if (ctype_isrefarray(info)) { - ctr->needsp = 1; - if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } - ctype_appc(ctr, '['); - if (size != CTSIZE_INVALID) { - CTSize csize = ctype_child(ctr->cts, ct)->size; - ctype_appnum(ctr, csize ? size/csize : 0); - } else if ((info & CTF_VLA)) { - ctype_appc(ctr, '?'); - } - ctype_appc(ctr, ']'); - } else if ((info & CTF_COMPLEX)) { - if (size == 2*sizeof(float)) ctype_preplit(ctr, "float"); - ctype_preplit(ctr, "complex"); - return; - } else { - ctype_preplit(ctr, ")))"); - ctype_prepnum(ctr, size); - ctype_preplit(ctr, "__attribute__((vector_size("); - } - break; - case CT_FUNC: - ctr->needsp = 1; - if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } - ctype_appc(ctr, '('); - ctype_appc(ctr, ')'); - break; - default: - lua_assert(0); - break; - } - ct = ctype_get(ctr->cts, ctype_cid(info)); - } -} - -/* Return a printable representation of a C type. */ -GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name) -{ - global_State *g = G(L); - CTRepr ctr; - ctr.pb = ctr.pe = &ctr.buf[CTREPR_MAX/2]; - ctr.cts = ctype_ctsG(g); - ctr.L = L; - ctr.ok = 1; - ctr.needsp = 0; - if (name) ctype_prepstr(&ctr, strdata(name), name->len); - ctype_repr(&ctr, id); - if (LJ_UNLIKELY(!ctr.ok)) return lj_str_newlit(L, "?"); - return lj_str_new(L, ctr.pb, ctr.pe - ctr.pb); -} - -/* Convert int64_t/uint64_t to string with 'LL' or 'ULL' suffix. */ -GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned) -{ - char buf[1+20+3]; - char *p = buf+sizeof(buf); - int sign = 0; - *--p = 'L'; *--p = 'L'; - if (isunsigned) { - *--p = 'U'; - } else if ((int64_t)n < 0) { - n = (uint64_t)-(int64_t)n; - sign = 1; - } - do { *--p = (char)('0' + n % 10); } while (n /= 10); - if (sign) *--p = '-'; - return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p)); -} - -/* Convert complex to string with 'i' or 'I' suffix. */ -GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size) -{ - char buf[2*LJ_STR_NUMBUF+2+1]; - TValue re, im; - size_t len; - if (size == 2*sizeof(double)) { - re.n = *(double *)sp; im.n = ((double *)sp)[1]; - } else { - re.n = (double)*(float *)sp; im.n = (double)((float *)sp)[1]; - } - len = lj_str_bufnum(buf, &re); - if (!(im.u32.hi & 0x80000000u) || im.n != im.n) buf[len++] = '+'; - len += lj_str_bufnum(buf+len, &im); - buf[len] = buf[len-1] >= 'a' ? 'I' : 'i'; - return lj_str_new(L, buf, len+1); -} - -/* -- C type state -------------------------------------------------------- */ - -/* Initialize C type table and state. */ -CTState *lj_ctype_init(lua_State *L) -{ - CTState *cts = lj_mem_newt(L, sizeof(CTState), CTState); - CType *ct = lj_mem_newvec(L, CTTYPETAB_MIN, CType); - const char *name = lj_ctype_typenames; - CTypeID id; - memset(cts, 0, sizeof(CTState)); - cts->tab = ct; - cts->sizetab = CTTYPETAB_MIN; - cts->top = CTTYPEINFO_NUM; - cts->L = NULL; - cts->g = G(L); - for (id = 0; id < CTTYPEINFO_NUM; id++, ct++) { - CTInfo info = lj_ctype_typeinfo[id]; - ct->size = (CTSize)((int32_t)(info << 16) >> 26); - ct->info = info & 0xffff03ffu; - ct->sib = 0; - if (ctype_type(info) == CT_KW || ctype_istypedef(info)) { - size_t len = strlen(name); - GCstr *str = lj_str_new(L, name, len); - ctype_setname(ct, str); - name += len+1; - lj_ctype_addname(cts, ct, id); - } else { - setgcrefnull(ct->name); - ct->next = 0; - if (!ctype_isenum(info)) ctype_addtype(cts, ct, id); - } - } - setmref(G(L)->ctype_state, cts); - return cts; -} - -/* Free C type table and state. */ -void lj_ctype_freestate(global_State *g) -{ - CTState *cts = ctype_ctsG(g); - if (cts) { - lj_ccallback_mcode_free(cts); - lj_mem_freevec(g, cts->tab, cts->sizetab, CType); - lj_mem_freevec(g, cts->cb.cbid, cts->cb.sizeid, CTypeID1); - lj_mem_freet(g, cts); - } -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ctype.h b/third-party/LuaJIT-2.0.2/src/lj_ctype.h deleted file mode 100644 index c43a2ba875..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ctype.h +++ /dev/null @@ -1,461 +0,0 @@ -/* -** C type management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CTYPE_H -#define _LJ_CTYPE_H - -#include "lj_obj.h" -#include "lj_gc.h" - -#if LJ_HASFFI - -/* -- C type definitions -------------------------------------------------- */ - -/* C type numbers. Highest 4 bits of C type info. ORDER CT. */ -enum { - /* Externally visible types. */ - CT_NUM, /* Integer or floating-point numbers. */ - CT_STRUCT, /* Struct or union. */ - CT_PTR, /* Pointer or reference. */ - CT_ARRAY, /* Array or complex type. */ - CT_MAYCONVERT = CT_ARRAY, - CT_VOID, /* Void type. */ - CT_ENUM, /* Enumeration. */ - CT_HASSIZE = CT_ENUM, /* Last type where ct->size holds the actual size. */ - CT_FUNC, /* Function. */ - CT_TYPEDEF, /* Typedef. */ - CT_ATTRIB, /* Miscellaneous attributes. */ - /* Internal element types. */ - CT_FIELD, /* Struct/union field or function parameter. */ - CT_BITFIELD, /* Struct/union bitfield. */ - CT_CONSTVAL, /* Constant value. */ - CT_EXTERN, /* External reference. */ - CT_KW /* Keyword. */ -}; - -LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR); -LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT); - -/* -** ---------- info ------------ -** |type flags... A cid | size | sib | next | name | -** +----------------------------+--------+-------+-------+-------+-- -** |NUM BFvcUL.. A | size | | type | | -** |STRUCT ..vcU..V A | size | field | name? | name? | -** |PTR ..vcR... A cid | size | | type | | -** |ARRAY VCvc...V A cid | size | | type | | -** |VOID ..vc.... A | size | | type | | -** |ENUM A cid | size | const | name? | name? | -** |FUNC ....VS.. cc cid | nargs | field | name? | name? | -** |TYPEDEF cid | | | name | name | -** |ATTRIB attrnum cid | attr | sib? | type? | | -** |FIELD cid | offset | field | | name? | -** |BITFIELD B.vcU csz bsz pos | offset | field | | name? | -** |CONSTVAL c cid | value | const | name | name | -** |EXTERN cid | | sib? | name | name | -** |KW tok | size | | name | name | -** +----------------------------+--------+-------+-------+-------+-- -** ^^ ^^--- bits used for C type conversion dispatch -*/ - -/* C type info flags. TFFArrrr */ -#define CTF_BOOL 0x08000000u /* Boolean: NUM, BITFIELD. */ -#define CTF_FP 0x04000000u /* Floating-point: NUM. */ -#define CTF_CONST 0x02000000u /* Const qualifier. */ -#define CTF_VOLATILE 0x01000000u /* Volatile qualifier. */ -#define CTF_UNSIGNED 0x00800000u /* Unsigned: NUM, BITFIELD. */ -#define CTF_LONG 0x00400000u /* Long: NUM. */ -#define CTF_VLA 0x00100000u /* Variable-length: ARRAY, STRUCT. */ -#define CTF_REF 0x00800000u /* Reference: PTR. */ -#define CTF_VECTOR 0x08000000u /* Vector: ARRAY. */ -#define CTF_COMPLEX 0x04000000u /* Complex: ARRAY. */ -#define CTF_UNION 0x00800000u /* Union: STRUCT. */ -#define CTF_VARARG 0x00800000u /* Vararg: FUNC. */ -#define CTF_SSEREGPARM 0x00400000u /* SSE register parameters: FUNC. */ - -#define CTF_QUAL (CTF_CONST|CTF_VOLATILE) -#define CTF_ALIGN (CTMASK_ALIGN< 0 ? CTF_UNSIGNED : 0) - -/* Flags used in parser. .F.Ammvf cp->attr */ -#define CTFP_ALIGNED 0x00000001u /* cp->attr + ALIGN */ -#define CTFP_PACKED 0x00000002u /* cp->attr */ -/* ...C...f cp->fattr */ -#define CTFP_CCONV 0x00000001u /* cp->fattr + CCONV/[SSE]REGPARM */ - -/* C type info bitfields. */ -#define CTMASK_CID 0x0000ffffu /* Max. 65536 type IDs. */ -#define CTMASK_NUM 0xf0000000u /* Max. 16 type numbers. */ -#define CTSHIFT_NUM 28 -#define CTMASK_ALIGN 15 /* Max. alignment is 2^15. */ -#define CTSHIFT_ALIGN 16 -#define CTMASK_ATTRIB 255 /* Max. 256 attributes. */ -#define CTSHIFT_ATTRIB 16 -#define CTMASK_CCONV 3 /* Max. 4 calling conventions. */ -#define CTSHIFT_CCONV 16 -#define CTMASK_REGPARM 3 /* Max. 0-3 regparms. */ -#define CTSHIFT_REGPARM 18 -/* Bitfields only used in parser. */ -#define CTMASK_VSIZEP 15 /* Max. vector size is 2^15. */ -#define CTSHIFT_VSIZEP 4 -#define CTMASK_MSIZEP 255 /* Max. type size (via mode) is 128. */ -#define CTSHIFT_MSIZEP 8 - -/* Info bits for BITFIELD. Max. size of bitfield is 64 bits. */ -#define CTBSZ_MAX 32 /* Max. size of bitfield is 32 bit. */ -#define CTBSZ_FIELD 127 /* Temp. marker for regular field. */ -#define CTMASK_BITPOS 127 -#define CTMASK_BITBSZ 127 -#define CTMASK_BITCSZ 127 -#define CTSHIFT_BITPOS 0 -#define CTSHIFT_BITBSZ 8 -#define CTSHIFT_BITCSZ 16 - -#define CTF_INSERT(info, field, val) \ - info = (info & ~(CTMASK_##field<> CTSHIFT_NUM) -#define ctype_cid(info) ((CTypeID)((info) & CTMASK_CID)) -#define ctype_align(info) (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN) -#define ctype_attrib(info) (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB) -#define ctype_bitpos(info) (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS) -#define ctype_bitbsz(info) (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ) -#define ctype_bitcsz(info) (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ) -#define ctype_vsizeP(info) (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP) -#define ctype_msizeP(info) (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP) -#define ctype_cconv(info) (((info) >> CTSHIFT_CCONV) & CTMASK_CCONV) - -/* Simple type checks. */ -#define ctype_isnum(info) (ctype_type((info)) == CT_NUM) -#define ctype_isvoid(info) (ctype_type((info)) == CT_VOID) -#define ctype_isptr(info) (ctype_type((info)) == CT_PTR) -#define ctype_isarray(info) (ctype_type((info)) == CT_ARRAY) -#define ctype_isstruct(info) (ctype_type((info)) == CT_STRUCT) -#define ctype_isfunc(info) (ctype_type((info)) == CT_FUNC) -#define ctype_isenum(info) (ctype_type((info)) == CT_ENUM) -#define ctype_istypedef(info) (ctype_type((info)) == CT_TYPEDEF) -#define ctype_isattrib(info) (ctype_type((info)) == CT_ATTRIB) -#define ctype_isfield(info) (ctype_type((info)) == CT_FIELD) -#define ctype_isbitfield(info) (ctype_type((info)) == CT_BITFIELD) -#define ctype_isconstval(info) (ctype_type((info)) == CT_CONSTVAL) -#define ctype_isextern(info) (ctype_type((info)) == CT_EXTERN) -#define ctype_hassize(info) (ctype_type((info)) <= CT_HASSIZE) - -/* Combined type and flag checks. */ -#define ctype_isinteger(info) \ - (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0)) -#define ctype_isinteger_or_bool(info) \ - (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0)) -#define ctype_isbool(info) \ - (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL)) -#define ctype_isfp(info) \ - (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP)) - -#define ctype_ispointer(info) \ - ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */ -#define ctype_isref(info) \ - (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF)) - -#define ctype_isrefarray(info) \ - (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0)) -#define ctype_isvector(info) \ - (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR)) -#define ctype_iscomplex(info) \ - (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX)) - -#define ctype_isvltype(info) \ - (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<") _(STRING, "") \ - _(INTEGER, "") _(EOF, "") \ - _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \ - _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->") - -/* Simple declaration specifiers. */ -#define CDSDEF(_) \ - _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \ - _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \ - _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \ - _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER) - -/* C keywords. */ -#define CKWDEF(_) \ - CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \ - _(DECLSPEC) _(CCDECL) _(PTRSZ) \ - _(STRUCT) _(UNION) _(ENUM) \ - _(SIZEOF) _(ALIGNOF) - -/* C token numbers. */ -enum { - CTOK_OFS = 255, -#define CTOKNUM(name, sym) CTOK_##name, -#define CKWNUM(name) CTOK_##name, -CTOKDEF(CTOKNUM) -CKWDEF(CKWNUM) -#undef CTOKNUM -#undef CKWNUM - CTOK_FIRSTDECL = CTOK_VOID, - CTOK_FIRSTSCL = CTOK_TYPEDEF, - CTOK_LASTDECLFLAG = CTOK_REGISTER, - CTOK_LASTDECL = CTOK_ENUM -}; - -/* Declaration specifier flags. */ -enum { -#define CDSFLAG(name) CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)), -CDSDEF(CDSFLAG) -#undef CDSFLAG - CDF__END -}; - -#define CDF_SCL (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER) - -/* -- C type management --------------------------------------------------- */ - -#define ctype_ctsG(g) (mref((g)->ctype_state, CTState)) - -/* Get C type state. */ -static LJ_AINLINE CTState *ctype_cts(lua_State *L) -{ - CTState *cts = ctype_ctsG(G(L)); - cts->L = L; /* Save L for errors and allocations. */ - return cts; -} - -/* Save and restore state of C type table. */ -#define LJ_CTYPE_SAVE(cts) CTState savects_ = *(cts) -#define LJ_CTYPE_RESTORE(cts) \ - ((cts)->top = savects_.top, \ - memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash))) - -/* Check C type ID for validity when assertions are enabled. */ -static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id) -{ - lua_assert(id > 0 && id < cts->top); UNUSED(cts); - return id; -} - -/* Get C type for C type ID. */ -static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id) -{ - return &cts->tab[ctype_check(cts, id)]; -} - -/* Get C type ID for a C type. */ -#define ctype_typeid(cts, ct) ((CTypeID)((ct) - (cts)->tab)) - -/* Get child C type. */ -static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct) -{ - lua_assert(!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) || - ctype_isbitfield(ct->info))); /* These don't have children. */ - return ctype_get(cts, ctype_cid(ct->info)); -} - -/* Get raw type for a C type ID. */ -static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id) -{ - CType *ct = ctype_get(cts, id); - while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct); - return ct; -} - -/* Get raw type of the child of a C type. */ -static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct) -{ - do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info)); - return ct; -} - -/* Set the name of a C type table element. */ -static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s) -{ - /* NOBARRIER: mark string as fixed -- the C type table is never collected. */ - fixstring(s); - setgcref(ct->name, obj2gco(s)); -} - -LJ_FUNC CTypeID lj_ctype_new(CTState *cts, CType **ctp); -LJ_FUNC CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size); -LJ_FUNC void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id); -LJ_FUNC CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, - uint32_t tmask); -LJ_FUNC CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, - CTSize *ofs, CTInfo *qual); -#define lj_ctype_getfield(cts, ct, name, ofs) \ - lj_ctype_getfieldq((cts), (ct), (name), (ofs), NULL) -LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id); -LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id); -LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem); -LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp); -LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm); -LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name); -LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned); -LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size); -LJ_FUNC CTState *lj_ctype_init(lua_State *L); -LJ_FUNC void lj_ctype_freestate(global_State *g); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_debug.c b/third-party/LuaJIT-2.0.2/src/lj_debug.c deleted file mode 100644 index be7fb2b1f0..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_debug.c +++ /dev/null @@ -1,596 +0,0 @@ -/* -** Debugging and introspection. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_debug_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_bc.h" -#if LJ_HASJIT -#include "lj_jit.h" -#endif - -/* -- Frames -------------------------------------------------------------- */ - -/* Get frame corresponding to a level. */ -cTValue *lj_debug_frame(lua_State *L, int level, int *size) -{ - cTValue *frame, *nextframe, *bot = tvref(L->stack); - /* Traverse frames backwards. */ - for (nextframe = frame = L->base-1; frame > bot; ) { - if (frame_gc(frame) == obj2gco(L)) - level++; /* Skip dummy frames. See lj_meta_call(). */ - if (level-- == 0) { - *size = (int)(nextframe - frame); - return frame; /* Level found. */ - } - nextframe = frame; - if (frame_islua(frame)) { - frame = frame_prevl(frame); - } else { - if (frame_isvarg(frame)) - level++; /* Skip vararg pseudo-frame. */ - frame = frame_prevd(frame); - } - } - *size = level; - return NULL; /* Level not found. */ -} - -/* Invalid bytecode position. */ -#define NO_BCPOS (~(BCPos)0) - -/* Return bytecode position for function/frame or NO_BCPOS. */ -static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe) -{ - const BCIns *ins; - GCproto *pt; - BCPos pos; - lua_assert(fn->c.gct == ~LJ_TFUNC || fn->c.gct == ~LJ_TTHREAD); - if (!isluafunc(fn)) { /* Cannot derive a PC for non-Lua functions. */ - return NO_BCPOS; - } else if (nextframe == NULL) { /* Lua function on top. */ - void *cf = cframe_raw(L->cframe); - if (cf == NULL || (char *)cframe_pc(cf) == (char *)cframe_L(cf)) - return NO_BCPOS; - ins = cframe_pc(cf); /* Only happens during error/hook handling. */ - } else { - if (frame_islua(nextframe)) { - ins = frame_pc(nextframe); - } else if (frame_iscont(nextframe)) { - ins = frame_contpc(nextframe); - } else { - /* Lua function below errfunc/gc/hook: find cframe to get the PC. */ - void *cf = cframe_raw(L->cframe); - TValue *f = L->base-1; - if (cf == NULL) - return NO_BCPOS; - while (f > nextframe) { - if (frame_islua(f)) { - f = frame_prevl(f); - } else { - if (frame_isc(f)) - cf = cframe_raw(cframe_prev(cf)); - f = frame_prevd(f); - } - } - if (cframe_prev(cf)) - cf = cframe_raw(cframe_prev(cf)); - ins = cframe_pc(cf); - } - } - pt = funcproto(fn); - pos = proto_bcpos(pt, ins) - 1; -#if LJ_HASJIT - if (pos > pt->sizebc) { /* Undo the effects of lj_trace_exit for JLOOP. */ - GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins)); - lua_assert(bc_isret(bc_op(ins[-1]))); - pos = proto_bcpos(pt, mref(T->startpc, const BCIns)); - } -#endif - return pos; -} - -/* -- Line numbers -------------------------------------------------------- */ - -/* Get line number for a bytecode position. */ -BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc) -{ - const void *lineinfo = proto_lineinfo(pt); - if (pc <= pt->sizebc && lineinfo) { - BCLine first = pt->firstline; - if (pc == pt->sizebc) return first + pt->numline; - if (pc-- == 0) return first; - if (pt->numline < 256) - return first + (BCLine)((const uint8_t *)lineinfo)[pc]; - else if (pt->numline < 65536) - return first + (BCLine)((const uint16_t *)lineinfo)[pc]; - else - return first + (BCLine)((const uint32_t *)lineinfo)[pc]; - } - return 0; -} - -/* Get line number for function/frame. */ -static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe) -{ - BCPos pc = debug_framepc(L, fn, nextframe); - if (pc != NO_BCPOS) { - GCproto *pt = funcproto(fn); - lua_assert(pc <= pt->sizebc); - return lj_debug_line(pt, pc); - } - return -1; -} - -/* -- Variable names ------------------------------------------------------ */ - -/* Read ULEB128 value. */ -static uint32_t debug_read_uleb128(const uint8_t **pp) -{ - const uint8_t *p = *pp; - uint32_t v = *p++; - if (LJ_UNLIKELY(v >= 0x80)) { - int sh = 0; - v &= 0x7f; - do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80); - } - *pp = p; - return v; -} - -/* Get name of a local variable from slot number and PC. */ -static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) -{ - const uint8_t *p = proto_varinfo(pt); - if (p) { - BCPos lastpc = 0; - for (;;) { - const char *name = (const char *)p; - uint32_t vn = *p++; - BCPos startpc, endpc; - if (vn < VARNAME__MAX) { - if (vn == VARNAME_END) break; /* End of varinfo. */ - } else { - while (*p++) ; /* Skip over variable name string. */ - } - lastpc = startpc = lastpc + debug_read_uleb128(&p); - if (startpc > pc) break; - endpc = startpc + debug_read_uleb128(&p); - if (pc < endpc && slot-- == 0) { - if (vn < VARNAME__MAX) { -#define VARNAMESTR(name, str) str "\0" - name = VARNAMEDEF(VARNAMESTR); -#undef VARNAMESTR - if (--vn) while (*name++ || --vn) ; - } - return name; - } - } - } - return NULL; -} - -/* Get name of local variable from 1-based slot number and function/frame. */ -static TValue *debug_localname(lua_State *L, const lua_Debug *ar, - const char **name, BCReg slot1) -{ - uint32_t offset = (uint32_t)ar->i_ci & 0xffff; - uint32_t size = (uint32_t)ar->i_ci >> 16; - TValue *frame = tvref(L->stack) + offset; - TValue *nextframe = size ? frame + size : NULL; - GCfunc *fn = frame_func(frame); - BCPos pc = debug_framepc(L, fn, nextframe); - if (!nextframe) nextframe = L->top; - if ((int)slot1 < 0) { /* Negative slot number is for varargs. */ - if (pc != NO_BCPOS) { - GCproto *pt = funcproto(fn); - if ((pt->flags & PROTO_VARARG)) { - slot1 = pt->numparams + (BCReg)(-(int)slot1); - if (frame_isvarg(frame)) { /* Vararg frame has been set up? (pc!=0) */ - nextframe = frame; - frame = frame_prevd(frame); - } - if (frame + slot1 < nextframe) { - *name = "(*vararg)"; - return frame+slot1; - } - } - } - return NULL; - } - if (pc != NO_BCPOS && - (*name = debug_varname(funcproto(fn), pc, slot1-1)) != NULL) - ; - else if (slot1 > 0 && frame + slot1 < nextframe) - *name = "(*temporary)"; - return frame+slot1; -} - -/* Get name of upvalue. */ -const char *lj_debug_uvname(GCproto *pt, uint32_t idx) -{ - const uint8_t *p = proto_uvinfo(pt); - lua_assert(idx < pt->sizeuv); - if (!p) return ""; - if (idx) while (*p++ || --idx) ; - return (const char *)p; -} - -/* Get name and value of upvalue. */ -const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp) -{ - if (tvisfunc(o)) { - GCfunc *fn = funcV(o); - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - if (idx < pt->sizeuv) { - *tvp = uvval(&gcref(fn->l.uvptr[idx])->uv); - return lj_debug_uvname(pt, idx); - } - } else { - if (idx < fn->c.nupvalues) { - *tvp = &fn->c.upvalue[idx]; - return ""; - } - } - } - return NULL; -} - -/* Deduce name of an object from slot number and PC. */ -const char *lj_debug_slotname(GCproto *pt, const BCIns *ip, BCReg slot, - const char **name) -{ - const char *lname; -restart: - lname = debug_varname(pt, proto_bcpos(pt, ip), slot); - if (lname != NULL) { *name = lname; return "local"; } - while (--ip > proto_bc(pt)) { - BCIns ins = *ip; - BCOp op = bc_op(ins); - BCReg ra = bc_a(ins); - if (bcmode_a(op) == BCMbase) { - if (slot >= ra && (op != BC_KNIL || slot <= bc_d(ins))) - return NULL; - } else if (bcmode_a(op) == BCMdst && ra == slot) { - switch (bc_op(ins)) { - case BC_MOV: - if (ra == slot) { slot = bc_d(ins); goto restart; } - break; - case BC_GGET: - *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins)))); - return "global"; - case BC_TGETS: - *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins)))); - if (ip > proto_bc(pt)) { - BCIns insp = ip[-1]; - if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1 && - bc_d(insp) == bc_b(ins)) - return "method"; - } - return "field"; - case BC_UGET: - *name = lj_debug_uvname(pt, bc_d(ins)); - return "upvalue"; - default: - return NULL; - } - } - } - return NULL; -} - -/* Deduce function name from caller of a frame. */ -const char *lj_debug_funcname(lua_State *L, TValue *frame, const char **name) -{ - TValue *pframe; - GCfunc *fn; - BCPos pc; - if (frame <= tvref(L->stack)) - return NULL; - if (frame_isvarg(frame)) - frame = frame_prevd(frame); - pframe = frame_prev(frame); - fn = frame_func(pframe); - pc = debug_framepc(L, fn, frame); - if (pc != NO_BCPOS) { - GCproto *pt = funcproto(fn); - const BCIns *ip = &proto_bc(pt)[check_exp(pc < pt->sizebc, pc)]; - MMS mm = bcmode_mm(bc_op(*ip)); - if (mm == MM_call) { - BCReg slot = bc_a(*ip); - if (bc_op(*ip) == BC_ITERC) slot -= 3; - return lj_debug_slotname(pt, ip, slot, name); - } else if (mm != MM__MAX) { - *name = strdata(mmname_str(G(L), mm)); - return "metamethod"; - } - } - return NULL; -} - -/* -- Source code locations ----------------------------------------------- */ - -/* Generate shortened source name. */ -void lj_debug_shortname(char *out, GCstr *str) -{ - const char *src = strdata(str); - if (*src == '=') { - strncpy(out, src+1, LUA_IDSIZE); /* Remove first char. */ - out[LUA_IDSIZE-1] = '\0'; /* Ensures null termination. */ - } else if (*src == '@') { /* Output "source", or "...source". */ - size_t len = str->len-1; - src++; /* Skip the `@' */ - if (len >= LUA_IDSIZE) { - src += len-(LUA_IDSIZE-4); /* Get last part of file name. */ - *out++ = '.'; *out++ = '.'; *out++ = '.'; - } - strcpy(out, src); - } else { /* Output [string "string"]. */ - size_t len; /* Length, up to first control char. */ - for (len = 0; len < LUA_IDSIZE-12; len++) - if (((const unsigned char *)src)[len] < ' ') break; - strcpy(out, "[string \""); out += 9; - if (src[len] != '\0') { /* Must truncate? */ - if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15; - strncpy(out, src, len); out += len; - strcpy(out, "..."); out += 3; - } else { - strcpy(out, src); out += len; - } - strcpy(out, "\"]"); - } -} - -/* Add current location of a frame to error message. */ -void lj_debug_addloc(lua_State *L, const char *msg, - cTValue *frame, cTValue *nextframe) -{ - if (frame) { - GCfunc *fn = frame_func(frame); - if (isluafunc(fn)) { - BCLine line = debug_frameline(L, fn, nextframe); - if (line >= 0) { - char buf[LUA_IDSIZE]; - lj_debug_shortname(buf, proto_chunkname(funcproto(fn))); - lj_str_pushf(L, "%s:%d: %s", buf, line, msg); - return; - } - } - } - lj_str_pushf(L, "%s", msg); -} - -/* Push location string for a bytecode position to Lua stack. */ -void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc) -{ - GCstr *name = proto_chunkname(pt); - const char *s = strdata(name); - MSize i, len = name->len; - BCLine line = lj_debug_line(pt, pc); - if (*s == '@') { - s++; len--; - for (i = len; i > 0; i--) - if (s[i] == '/' || s[i] == '\\') { - s += i+1; - break; - } - lj_str_pushf(L, "%s:%d", s, line); - } else if (len > 40) { - lj_str_pushf(L, "%p:%d", pt, line); - } else if (*s == '=') { - lj_str_pushf(L, "%s:%d", s+1, line); - } else { - lj_str_pushf(L, "\"%s\":%d", s, line); - } -} - -/* -- Public debug API ---------------------------------------------------- */ - -/* lua_getupvalue() and lua_setupvalue() are in lj_api.c. */ - -LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) -{ - const char *name = NULL; - if (ar) { - TValue *o = debug_localname(L, ar, &name, (BCReg)n); - if (name) { - copyTV(L, L->top, o); - incr_top(L); - } - } else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) { - name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1); - } - return name; -} - -LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n) -{ - const char *name = NULL; - TValue *o = debug_localname(L, ar, &name, (BCReg)n); - if (name) - copyTV(L, o, L->top-1); - L->top--; - return name; -} - -int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext) -{ - int opt_f = 0, opt_L = 0; - TValue *frame = NULL; - TValue *nextframe = NULL; - GCfunc *fn; - if (*what == '>') { - TValue *func = L->top - 1; - api_check(L, tvisfunc(func)); - fn = funcV(func); - L->top--; - what++; - } else { - uint32_t offset = (uint32_t)ar->i_ci & 0xffff; - uint32_t size = (uint32_t)ar->i_ci >> 16; - lua_assert(offset != 0); - frame = tvref(L->stack) + offset; - if (size) nextframe = frame + size; - lua_assert(frame <= tvref(L->maxstack) && - (!nextframe || nextframe <= tvref(L->maxstack))); - fn = frame_func(frame); - lua_assert(fn->c.gct == ~LJ_TFUNC); - } - for (; *what; what++) { - if (*what == 'S') { - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - BCLine firstline = pt->firstline; - GCstr *name = proto_chunkname(pt); - ar->source = strdata(name); - lj_debug_shortname(ar->short_src, name); - ar->linedefined = (int)firstline; - ar->lastlinedefined = (int)(firstline + pt->numline); - ar->what = firstline ? "Lua" : "main"; - } else { - ar->source = "=[C]"; - ar->short_src[0] = '['; - ar->short_src[1] = 'C'; - ar->short_src[2] = ']'; - ar->short_src[3] = '\0'; - ar->linedefined = -1; - ar->lastlinedefined = -1; - ar->what = "C"; - } - } else if (*what == 'l') { - ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1; - } else if (*what == 'u') { - ar->nups = fn->c.nupvalues; - if (ext) { - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - ar->nparams = pt->numparams; - ar->isvararg = !!(pt->flags & PROTO_VARARG); - } else { - ar->nparams = 0; - ar->isvararg = 1; - } - } - } else if (*what == 'n') { - ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL; - if (ar->namewhat == NULL) { - ar->namewhat = ""; - ar->name = NULL; - } - } else if (*what == 'f') { - opt_f = 1; - } else if (*what == 'L') { - opt_L = 1; - } else { - return 0; /* Bad option. */ - } - } - if (opt_f) { - setfuncV(L, L->top, fn); - incr_top(L); - } - if (opt_L) { - if (isluafunc(fn)) { - GCtab *t = lj_tab_new(L, 0, 0); - GCproto *pt = funcproto(fn); - const void *lineinfo = proto_lineinfo(pt); - if (lineinfo) { - BCLine first = pt->firstline; - int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4; - MSize i, szl = pt->sizebc-1; - for (i = 0; i < szl; i++) { - BCLine line = first + - (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] : - sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] : - (BCLine)((const uint32_t *)lineinfo)[i]); - setboolV(lj_tab_setint(L, t, line), 1); - } - } - settabV(L, L->top, t); - } else { - setnilV(L->top); - } - incr_top(L); - } - return 1; /* Ok. */ -} - -LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) -{ - return lj_debug_getinfo(L, what, (lj_Debug *)ar, 0); -} - -LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) -{ - int size; - cTValue *frame = lj_debug_frame(L, level, &size); - if (frame) { - ar->i_ci = (size << 16) + (int)(frame - tvref(L->stack)); - return 1; - } else { - ar->i_ci = level - size; - return 0; - } -} - -/* Number of frames for the leading and trailing part of a traceback. */ -#define TRACEBACK_LEVELS1 12 -#define TRACEBACK_LEVELS2 10 - -LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, - int level) -{ - int top = (int)(L->top - L->base); - int lim = TRACEBACK_LEVELS1; - lua_Debug ar; - if (msg) lua_pushfstring(L, "%s\n", msg); - lua_pushliteral(L, "stack traceback:"); - while (lua_getstack(L1, level++, &ar)) { - GCfunc *fn; - if (level > lim) { - if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) { - level--; - } else { - lua_pushliteral(L, "\n\t..."); - lua_getstack(L1, -10, &ar); - level = ar.i_ci - TRACEBACK_LEVELS2; - } - lim = 2147483647; - continue; - } - lua_getinfo(L1, "Snlf", &ar); - fn = funcV(L1->top-1); L1->top--; - if (isffunc(fn) && !*ar.namewhat) - lua_pushfstring(L, "\n\t[builtin#%d]:", fn->c.ffid); - else - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - if (*ar.namewhat) { - lua_pushfstring(L, " in function " LUA_QS, ar.name); - } else { - if (*ar.what == 'm') { - lua_pushliteral(L, " in main chunk"); - } else if (*ar.what == 'C') { - lua_pushfstring(L, " at %p", fn->c.f); - } else { - lua_pushfstring(L, " in function <%s:%d>", - ar.short_src, ar.linedefined); - } - } - if ((int)(L->top - L->base) - top >= 15) - lua_concat(L, (int)(L->top - L->base) - top); - } - lua_concat(L, (int)(L->top - L->base) - top); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_debug.h b/third-party/LuaJIT-2.0.2/src/lj_debug.h deleted file mode 100644 index 7cf57de778..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_debug.h +++ /dev/null @@ -1,61 +0,0 @@ -/* -** Debugging and introspection. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_DEBUG_H -#define _LJ_DEBUG_H - -#include "lj_obj.h" - -typedef struct lj_Debug { - /* Common fields. Must be in the same order as in lua.h. */ - int event; - const char *name; - const char *namewhat; - const char *what; - const char *source; - int currentline; - int nups; - int linedefined; - int lastlinedefined; - char short_src[LUA_IDSIZE]; - int i_ci; - /* Extended fields. Only valid if lj_debug_getinfo() is called with ext = 1.*/ - int nparams; - int isvararg; -} lj_Debug; - -LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size); -LJ_FUNC BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc); -LJ_FUNC const char *lj_debug_uvname(GCproto *pt, uint32_t idx); -LJ_FUNC const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp); -LJ_FUNC const char *lj_debug_slotname(GCproto *pt, const BCIns *pc, - BCReg slot, const char **name); -LJ_FUNC const char *lj_debug_funcname(lua_State *L, TValue *frame, - const char **name); -LJ_FUNC void lj_debug_shortname(char *out, GCstr *str); -LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg, - cTValue *frame, cTValue *nextframe); -LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc); -LJ_FUNC int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, - int ext); - -/* Fixed internal variable names. */ -#define VARNAMEDEF(_) \ - _(FOR_IDX, "(for index)") \ - _(FOR_STOP, "(for limit)") \ - _(FOR_STEP, "(for step)") \ - _(FOR_GEN, "(for generator)") \ - _(FOR_STATE, "(for state)") \ - _(FOR_CTL, "(for control)") - -enum { - VARNAME_END, -#define VARNAMEENUM(name, str) VARNAME_##name, - VARNAMEDEF(VARNAMEENUM) -#undef VARNAMEENUM - VARNAME__MAX -}; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_def.h b/third-party/LuaJIT-2.0.2/src/lj_def.h deleted file mode 100644 index 250729f759..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_def.h +++ /dev/null @@ -1,349 +0,0 @@ -/* -** LuaJIT common internal definitions. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_DEF_H -#define _LJ_DEF_H - -#include "lua.h" - -#if defined(_MSC_VER) -/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */ -typedef __int8 int8_t; -typedef __int16 int16_t; -typedef __int32 int32_t; -typedef __int64 int64_t; -typedef unsigned __int8 uint8_t; -typedef unsigned __int16 uint16_t; -typedef unsigned __int32 uint32_t; -typedef unsigned __int64 uint64_t; -#ifdef _WIN64 -typedef __int64 intptr_t; -typedef unsigned __int64 uintptr_t; -#else -typedef __int32 intptr_t; -typedef unsigned __int32 uintptr_t; -#endif -#elif defined(__symbian__) -/* Cough. */ -typedef signed char int8_t; -typedef short int int16_t; -typedef int int32_t; -typedef long long int64_t; -typedef unsigned char uint8_t; -typedef unsigned short int uint16_t; -typedef unsigned int uint32_t; -typedef unsigned long long uint64_t; -typedef int intptr_t; -typedef unsigned int uintptr_t; -#else -#include -#endif - -/* Needed everywhere. */ -#include -#include - -/* Various VM limits. */ -#define LJ_MAX_MEM 0x7fffff00 /* Max. total memory allocation. */ -#define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */ -#define LJ_MAX_STR LJ_MAX_MEM /* Max. string length. */ -#define LJ_MAX_UDATA LJ_MAX_MEM /* Max. userdata length. */ - -#define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */ -#define LJ_MAX_HBITS 26 /* Max. hash bits. */ -#define LJ_MAX_ABITS 28 /* Max. bits of array key. */ -#define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */ -#define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */ - -#define LJ_MAX_LINE LJ_MAX_MEM /* Max. source code line number. */ -#define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */ -#define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */ -#define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */ -#define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */ -#define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */ - -#define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */ -#define LJ_STACK_EXTRA 5 /* Extra stack space (metamethods). */ - -#define LJ_NUM_CBPAGE 1 /* Number of FFI callback pages. */ - -/* Minimum table/buffer sizes. */ -#define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */ -#define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). */ -#define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */ -#define LJ_MIN_SBUF 32 /* Min. string buffer length. */ -#define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */ -#define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */ -#define LJ_MIN_K64SZ 16 /* Min. size for chained K64Array. */ - -/* JIT compiler limits. */ -#define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */ -#define LJ_MAX_PHI 64 /* Max. # of PHIs for a loop. */ -#define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */ - -/* Various macros. */ -#ifndef UNUSED -#define UNUSED(x) ((void)(x)) /* to avoid warnings */ -#endif - -#define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo) -#define i32ptr(p) ((int32_t)(intptr_t)(void *)(p)) -#define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p)) - -#define checki8(x) ((x) == (int32_t)(int8_t)(x)) -#define checku8(x) ((x) == (int32_t)(uint8_t)(x)) -#define checki16(x) ((x) == (int32_t)(int16_t)(x)) -#define checku16(x) ((x) == (int32_t)(uint16_t)(x)) -#define checki32(x) ((x) == (int32_t)(x)) -#define checku32(x) ((x) == (uint32_t)(x)) -#define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x)) - -/* Every half-decent C compiler transforms this into a rotate instruction. */ -#define lj_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1)))) -#define lj_ror(x, n) (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n))) - -/* A really naive Bloom filter. But sufficient for our needs. */ -typedef uintptr_t BloomFilter; -#define BLOOM_MASK (8*sizeof(BloomFilter) - 1) -#define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK)) -#define bloomset(b, x) ((b) |= bloombit((x))) -#define bloomtest(b, x) ((b) & bloombit((x))) - -#if defined(__GNUC__) - -#define LJ_NORET __attribute__((noreturn)) -#define LJ_ALIGN(n) __attribute__((aligned(n))) -#define LJ_INLINE inline -#define LJ_AINLINE inline __attribute__((always_inline)) -#define LJ_NOINLINE __attribute__((noinline)) - -#if defined(__ELF__) || defined(__MACH__) -#if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__)) -#define LJ_NOAPI extern __attribute__((visibility("hidden"))) -#endif -#endif - -/* Note: it's only beneficial to use fastcall on x86 and then only for up to -** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only -** indirect calls and related tail-called C functions are marked as fastcall. -*/ -#if defined(__i386__) -#define LJ_FASTCALL __attribute__((fastcall)) -#endif - -#define LJ_LIKELY(x) __builtin_expect(!!(x), 1) -#define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0) - -#define lj_ffs(x) ((uint32_t)__builtin_ctz(x)) -/* Don't ask ... */ -#if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__)) -static LJ_AINLINE uint32_t lj_fls(uint32_t x) -{ - uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r; -} -#else -#define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31)) -#endif - -#if defined(__arm__) -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ - uint32_t r; -#if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\ - __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ - __asm__("rev %0, %1" : "=r" (r) : "r" (x)); - return r; -#else -#ifdef __thumb__ - r = x ^ lj_ror(x, 16); -#else - __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x)); -#endif - return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8); -#endif -} - -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); -} -#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ - return (uint32_t)__builtin_bswap32((int32_t)x); -} - -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return (uint64_t)__builtin_bswap64((int64_t)x); -} -#elif defined(__i386__) || defined(__x86_64__) -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ - uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; -} - -#if defined(__i386__) -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); -} -#else -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; -} -#endif -#else -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ - return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24); -} - -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return (uint64_t)lj_bswap((uint32_t)(x >> 32)) | - ((uint64_t)lj_bswap((uint32_t)x) << 32); -} -#endif - -typedef union __attribute__((packed)) Unaligned16 { - uint16_t u; - uint8_t b[2]; -} Unaligned16; - -typedef union __attribute__((packed)) Unaligned32 { - uint32_t u; - uint8_t b[4]; -} Unaligned32; - -/* Unaligned load of uint16_t. */ -static LJ_AINLINE uint16_t lj_getu16(const void *p) -{ - return ((const Unaligned16 *)p)->u; -} - -/* Unaligned load of uint32_t. */ -static LJ_AINLINE uint32_t lj_getu32(const void *p) -{ - return ((const Unaligned32 *)p)->u; -} - -#elif defined(_MSC_VER) - -#define LJ_NORET __declspec(noreturn) -#define LJ_ALIGN(n) __declspec(align(n)) -#define LJ_INLINE __inline -#define LJ_AINLINE __forceinline -#define LJ_NOINLINE __declspec(noinline) -#if defined(_M_IX86) -#define LJ_FASTCALL __fastcall -#endif - -#ifdef _M_PPC -unsigned int _CountLeadingZeros(long); -#pragma intrinsic(_CountLeadingZeros) -static LJ_AINLINE uint32_t lj_fls(uint32_t x) -{ - return _CountLeadingZeros(x) ^ 31; -} -#else -unsigned char _BitScanForward(uint32_t *, unsigned long); -unsigned char _BitScanReverse(uint32_t *, unsigned long); -#pragma intrinsic(_BitScanForward) -#pragma intrinsic(_BitScanReverse) - -static LJ_AINLINE uint32_t lj_ffs(uint32_t x) -{ - uint32_t r; _BitScanForward(&r, x); return r; -} - -static LJ_AINLINE uint32_t lj_fls(uint32_t x) -{ - uint32_t r; _BitScanReverse(&r, x); return r; -} -#endif - -unsigned long _byteswap_ulong(unsigned long); -uint64_t _byteswap_uint64(uint64_t); -#define lj_bswap(x) (_byteswap_ulong((x))) -#define lj_bswap64(x) (_byteswap_uint64((x))) - -#if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED) -/* -** Replacement for unaligned loads on Xbox 360. Disabled by default since it's -** usually more costly than the occasional stall when crossing a cache-line. -*/ -static LJ_AINLINE uint16_t lj_getu16(const void *v) -{ - const uint8_t *p = (const uint8_t *)v; - return (uint16_t)((p[0]<<8) | p[1]); -} -static LJ_AINLINE uint32_t lj_getu32(const void *v) -{ - const uint8_t *p = (const uint8_t *)v; - return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]); -} -#else -/* Unaligned loads are generally ok on x86/x64. */ -#define lj_getu16(p) (*(uint16_t *)(p)) -#define lj_getu32(p) (*(uint32_t *)(p)) -#endif - -#else -#error "missing defines for your compiler" -#endif - -/* Optional defines. */ -#ifndef LJ_FASTCALL -#define LJ_FASTCALL -#endif -#ifndef LJ_NORET -#define LJ_NORET -#endif -#ifndef LJ_NOAPI -#define LJ_NOAPI extern -#endif -#ifndef LJ_LIKELY -#define LJ_LIKELY(x) (x) -#define LJ_UNLIKELY(x) (x) -#endif - -/* Attributes for internal functions. */ -#define LJ_DATA LJ_NOAPI -#define LJ_DATADEF -#define LJ_ASMF LJ_NOAPI -#define LJ_FUNCA LJ_NOAPI -#if defined(ljamalg_c) -#define LJ_FUNC static -#else -#define LJ_FUNC LJ_NOAPI -#endif -#define LJ_FUNC_NORET LJ_FUNC LJ_NORET -#define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET -#define LJ_ASMF_NORET LJ_ASMF LJ_NORET - -/* Runtime assertions. */ -#ifdef lua_assert -#define check_exp(c, e) (lua_assert(c), (e)) -#define api_check(l, e) lua_assert(e) -#else -#define lua_assert(c) ((void)0) -#define check_exp(c, e) (e) -#define api_check luai_apicheck -#endif - -/* Static assertions. */ -#define LJ_ASSERT_NAME2(name, line) name ## line -#define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line) -#ifdef __COUNTER__ -#define LJ_STATIC_ASSERT(cond) \ - extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) -#else -#define LJ_STATIC_ASSERT(cond) \ - extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_dispatch.c b/third-party/LuaJIT-2.0.2/src/lj_dispatch.c deleted file mode 100644 index d57f1a6f92..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_dispatch.c +++ /dev/null @@ -1,494 +0,0 @@ -/* -** Instruction dispatch handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_dispatch_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_func.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_debug.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_ff.h" -#if LJ_HASJIT -#include "lj_jit.h" -#endif -#if LJ_HASFFI -#include "lj_ccallback.h" -#endif -#include "lj_trace.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "luajit.h" - -/* Bump GG_NUM_ASMFF in lj_dispatch.h as needed. Ugly. */ -LJ_STATIC_ASSERT(GG_NUM_ASMFF == FF_NUM_ASMFUNC); - -/* -- Dispatch table management ------------------------------------------- */ - -#if LJ_TARGET_MIPS -#include -LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, - lua_State *co); - -#define GOTFUNC(name) (ASMFunction)name, -static const ASMFunction dispatch_got[] = { - GOTDEF(GOTFUNC) -}; -#undef GOTFUNC -#endif - -/* Initialize instruction dispatch table and hot counters. */ -void lj_dispatch_init(GG_State *GG) -{ - uint32_t i; - ASMFunction *disp = GG->dispatch; - for (i = 0; i < GG_LEN_SDISP; i++) - disp[GG_LEN_DDISP+i] = disp[i] = makeasmfunc(lj_bc_ofs[i]); - for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) - disp[i] = makeasmfunc(lj_bc_ofs[i]); - /* The JIT engine is off by default. luaopen_jit() turns it on. */ - disp[BC_FORL] = disp[BC_IFORL]; - disp[BC_ITERL] = disp[BC_IITERL]; - disp[BC_LOOP] = disp[BC_ILOOP]; - disp[BC_FUNCF] = disp[BC_IFUNCF]; - disp[BC_FUNCV] = disp[BC_IFUNCV]; - GG->g.bc_cfunc_ext = GG->g.bc_cfunc_int = BCINS_AD(BC_FUNCC, LUA_MINSTACK, 0); - for (i = 0; i < GG_NUM_ASMFF; i++) - GG->bcff[i] = BCINS_AD(BC__MAX+i, 0, 0); -#if LJ_TARGET_MIPS - memcpy(GG->got, dispatch_got, LJ_GOT__MAX*4); -#endif -} - -#if LJ_HASJIT -/* Initialize hotcount table. */ -void lj_dispatch_init_hotcount(global_State *g) -{ - int32_t hotloop = G2J(g)->param[JIT_P_hotloop]; - HotCount start = (HotCount)(hotloop*HOTCOUNT_LOOP - 1); - HotCount *hotcount = G2GG(g)->hotcount; - uint32_t i; - for (i = 0; i < HOTCOUNT_SIZE; i++) - hotcount[i] = start; -} -#endif - -/* Internal dispatch mode bits. */ -#define DISPMODE_JIT 0x01 /* JIT compiler on. */ -#define DISPMODE_REC 0x02 /* Recording active. */ -#define DISPMODE_INS 0x04 /* Override instruction dispatch. */ -#define DISPMODE_CALL 0x08 /* Override call dispatch. */ -#define DISPMODE_RET 0x10 /* Override return dispatch. */ - -/* Update dispatch table depending on various flags. */ -void lj_dispatch_update(global_State *g) -{ - uint8_t oldmode = g->dispatchmode; - uint8_t mode = 0; -#if LJ_HASJIT - mode |= (G2J(g)->flags & JIT_F_ON) ? DISPMODE_JIT : 0; - mode |= G2J(g)->state != LJ_TRACE_IDLE ? - (DISPMODE_REC|DISPMODE_INS|DISPMODE_CALL) : 0; -#endif - mode |= (g->hookmask & (LUA_MASKLINE|LUA_MASKCOUNT)) ? DISPMODE_INS : 0; - mode |= (g->hookmask & LUA_MASKCALL) ? DISPMODE_CALL : 0; - mode |= (g->hookmask & LUA_MASKRET) ? DISPMODE_RET : 0; - if (oldmode != mode) { /* Mode changed? */ - ASMFunction *disp = G2GG(g)->dispatch; - ASMFunction f_forl, f_iterl, f_loop, f_funcf, f_funcv; - g->dispatchmode = mode; - - /* Hotcount if JIT is on, but not while recording. */ - if ((mode & (DISPMODE_JIT|DISPMODE_REC)) == DISPMODE_JIT) { - f_forl = makeasmfunc(lj_bc_ofs[BC_FORL]); - f_iterl = makeasmfunc(lj_bc_ofs[BC_ITERL]); - f_loop = makeasmfunc(lj_bc_ofs[BC_LOOP]); - f_funcf = makeasmfunc(lj_bc_ofs[BC_FUNCF]); - f_funcv = makeasmfunc(lj_bc_ofs[BC_FUNCV]); - } else { /* Otherwise use the non-hotcounting instructions. */ - f_forl = disp[GG_LEN_DDISP+BC_IFORL]; - f_iterl = disp[GG_LEN_DDISP+BC_IITERL]; - f_loop = disp[GG_LEN_DDISP+BC_ILOOP]; - f_funcf = makeasmfunc(lj_bc_ofs[BC_IFUNCF]); - f_funcv = makeasmfunc(lj_bc_ofs[BC_IFUNCV]); - } - /* Init static counting instruction dispatch first (may be copied below). */ - disp[GG_LEN_DDISP+BC_FORL] = f_forl; - disp[GG_LEN_DDISP+BC_ITERL] = f_iterl; - disp[GG_LEN_DDISP+BC_LOOP] = f_loop; - - /* Set dynamic instruction dispatch. */ - if ((oldmode ^ mode) & (DISPMODE_REC|DISPMODE_INS)) { - /* Need to update the whole table. */ - if (!(mode & (DISPMODE_REC|DISPMODE_INS))) { /* No ins dispatch? */ - /* Copy static dispatch table to dynamic dispatch table. */ - memcpy(&disp[0], &disp[GG_LEN_DDISP], GG_LEN_SDISP*sizeof(ASMFunction)); - /* Overwrite with dynamic return dispatch. */ - if ((mode & DISPMODE_RET)) { - disp[BC_RETM] = lj_vm_rethook; - disp[BC_RET] = lj_vm_rethook; - disp[BC_RET0] = lj_vm_rethook; - disp[BC_RET1] = lj_vm_rethook; - } - } else { - /* The recording dispatch also checks for hooks. */ - ASMFunction f = (mode & DISPMODE_REC) ? lj_vm_record : lj_vm_inshook; - uint32_t i; - for (i = 0; i < GG_LEN_SDISP; i++) - disp[i] = f; - } - } else if (!(mode & (DISPMODE_REC|DISPMODE_INS))) { - /* Otherwise set dynamic counting ins. */ - disp[BC_FORL] = f_forl; - disp[BC_ITERL] = f_iterl; - disp[BC_LOOP] = f_loop; - /* Set dynamic return dispatch. */ - if ((mode & DISPMODE_RET)) { - disp[BC_RETM] = lj_vm_rethook; - disp[BC_RET] = lj_vm_rethook; - disp[BC_RET0] = lj_vm_rethook; - disp[BC_RET1] = lj_vm_rethook; - } else { - disp[BC_RETM] = disp[GG_LEN_DDISP+BC_RETM]; - disp[BC_RET] = disp[GG_LEN_DDISP+BC_RET]; - disp[BC_RET0] = disp[GG_LEN_DDISP+BC_RET0]; - disp[BC_RET1] = disp[GG_LEN_DDISP+BC_RET1]; - } - } - - /* Set dynamic call dispatch. */ - if ((oldmode ^ mode) & DISPMODE_CALL) { /* Update the whole table? */ - uint32_t i; - if ((mode & DISPMODE_CALL) == 0) { /* No call hooks? */ - for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) - disp[i] = makeasmfunc(lj_bc_ofs[i]); - } else { - for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) - disp[i] = lj_vm_callhook; - } - } - if (!(mode & DISPMODE_CALL)) { /* Overwrite dynamic counting ins. */ - disp[BC_FUNCF] = f_funcf; - disp[BC_FUNCV] = f_funcv; - } - -#if LJ_HASJIT - /* Reset hotcounts for JIT off to on transition. */ - if ((mode & DISPMODE_JIT) && !(oldmode & DISPMODE_JIT)) - lj_dispatch_init_hotcount(g); -#endif - } -} - -/* -- JIT mode setting ---------------------------------------------------- */ - -#if LJ_HASJIT -/* Set JIT mode for a single prototype. */ -static void setptmode(global_State *g, GCproto *pt, int mode) -{ - if ((mode & LUAJIT_MODE_ON)) { /* (Re-)enable JIT compilation. */ - pt->flags &= ~PROTO_NOJIT; - lj_trace_reenableproto(pt); /* Unpatch all ILOOP etc. bytecodes. */ - } else { /* Flush and/or disable JIT compilation. */ - if (!(mode & LUAJIT_MODE_FLUSH)) - pt->flags |= PROTO_NOJIT; - lj_trace_flushproto(g, pt); /* Flush all traces of prototype. */ - } -} - -/* Recursively set the JIT mode for all children of a prototype. */ -static void setptmode_all(global_State *g, GCproto *pt, int mode) -{ - ptrdiff_t i; - if (!(pt->flags & PROTO_CHILD)) return; - for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) { - GCobj *o = proto_kgc(pt, i); - if (o->gch.gct == ~LJ_TPROTO) { - setptmode(g, gco2pt(o), mode); - setptmode_all(g, gco2pt(o), mode); - } - } -} -#endif - -/* Public API function: control the JIT engine. */ -int luaJIT_setmode(lua_State *L, int idx, int mode) -{ - global_State *g = G(L); - int mm = mode & LUAJIT_MODE_MASK; - lj_trace_abort(g); /* Abort recording on any state change. */ - /* Avoid pulling the rug from under our own feet. */ - if ((g->hookmask & HOOK_GC)) - lj_err_caller(L, LJ_ERR_NOGCMM); - switch (mm) { -#if LJ_HASJIT - case LUAJIT_MODE_ENGINE: - if ((mode & LUAJIT_MODE_FLUSH)) { - lj_trace_flushall(L); - } else { - if (!(mode & LUAJIT_MODE_ON)) - G2J(g)->flags &= ~(uint32_t)JIT_F_ON; -#if LJ_TARGET_X86ORX64 - else if ((G2J(g)->flags & JIT_F_SSE2)) - G2J(g)->flags |= (uint32_t)JIT_F_ON; - else - return 0; /* Don't turn on JIT compiler without SSE2 support. */ -#else - else - G2J(g)->flags |= (uint32_t)JIT_F_ON; -#endif - lj_dispatch_update(g); - } - break; - case LUAJIT_MODE_FUNC: - case LUAJIT_MODE_ALLFUNC: - case LUAJIT_MODE_ALLSUBFUNC: { - cTValue *tv = idx == 0 ? frame_prev(L->base-1) : - idx > 0 ? L->base + (idx-1) : L->top + idx; - GCproto *pt; - if ((idx == 0 || tvisfunc(tv)) && isluafunc(&gcval(tv)->fn)) - pt = funcproto(&gcval(tv)->fn); /* Cannot use funcV() for frame slot. */ - else if (tvisproto(tv)) - pt = protoV(tv); - else - return 0; /* Failed. */ - if (mm != LUAJIT_MODE_ALLSUBFUNC) - setptmode(g, pt, mode); - if (mm != LUAJIT_MODE_FUNC) - setptmode_all(g, pt, mode); - break; - } - case LUAJIT_MODE_TRACE: - if (!(mode & LUAJIT_MODE_FLUSH)) - return 0; /* Failed. */ - lj_trace_flush(G2J(g), idx); - break; -#else - case LUAJIT_MODE_ENGINE: - case LUAJIT_MODE_FUNC: - case LUAJIT_MODE_ALLFUNC: - case LUAJIT_MODE_ALLSUBFUNC: - UNUSED(idx); - if ((mode & LUAJIT_MODE_ON)) - return 0; /* Failed. */ - break; -#endif - case LUAJIT_MODE_WRAPCFUNC: - if ((mode & LUAJIT_MODE_ON)) { - if (idx != 0) { - cTValue *tv = idx > 0 ? L->base + (idx-1) : L->top + idx; - if (tvislightud(tv)) - g->wrapf = (lua_CFunction)lightudV(tv); - else - return 0; /* Failed. */ - } else { - return 0; /* Failed. */ - } - g->bc_cfunc_ext = BCINS_AD(BC_FUNCCW, 0, 0); - } else { - g->bc_cfunc_ext = BCINS_AD(BC_FUNCC, 0, 0); - } - break; - default: - return 0; /* Failed. */ - } - return 1; /* OK. */ -} - -/* Enforce (dynamic) linker error for version mismatches. See luajit.c. */ -LUA_API void LUAJIT_VERSION_SYM(void) -{ -} - -/* -- Hooks --------------------------------------------------------------- */ - -/* This function can be called asynchronously (e.g. during a signal). */ -LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count) -{ - global_State *g = G(L); - mask &= HOOK_EVENTMASK; - if (func == NULL || mask == 0) { mask = 0; func = NULL; } /* Consistency. */ - g->hookf = func; - g->hookcount = g->hookcstart = (int32_t)count; - g->hookmask = (uint8_t)((g->hookmask & ~HOOK_EVENTMASK) | mask); - lj_trace_abort(g); /* Abort recording on any hook change. */ - lj_dispatch_update(g); - return 1; -} - -LUA_API lua_Hook lua_gethook(lua_State *L) -{ - return G(L)->hookf; -} - -LUA_API int lua_gethookmask(lua_State *L) -{ - return G(L)->hookmask & HOOK_EVENTMASK; -} - -LUA_API int lua_gethookcount(lua_State *L) -{ - return (int)G(L)->hookcstart; -} - -/* Call a hook. */ -static void callhook(lua_State *L, int event, BCLine line) -{ - global_State *g = G(L); - lua_Hook hookf = g->hookf; - if (hookf && !hook_active(g)) { - lua_Debug ar; - lj_trace_abort(g); /* Abort recording on any hook call. */ - ar.event = event; - ar.currentline = line; - /* Top frame, nextframe = NULL. */ - ar.i_ci = (int)((L->base-1) - tvref(L->stack)); - lj_state_checkstack(L, 1+LUA_MINSTACK); - hook_enter(g); - hookf(L, &ar); - lua_assert(hook_active(g)); - hook_leave(g); - } -} - -/* -- Dispatch callbacks -------------------------------------------------- */ - -/* Calculate number of used stack slots in the current frame. */ -static BCReg cur_topslot(GCproto *pt, const BCIns *pc, uint32_t nres) -{ - BCIns ins = pc[-1]; - if (bc_op(ins) == BC_UCLO) - ins = pc[bc_j(ins)]; - switch (bc_op(ins)) { - case BC_CALLM: case BC_CALLMT: return bc_a(ins) + bc_c(ins) + nres-1+1; - case BC_RETM: return bc_a(ins) + bc_d(ins) + nres-1; - case BC_TSETM: return bc_a(ins) + nres-1; - default: return pt->framesize; - } -} - -/* Instruction dispatch. Used by instr/line/return hooks or when recording. */ -void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc) -{ - ERRNO_SAVE - GCfunc *fn = curr_func(L); - GCproto *pt = funcproto(fn); - void *cf = cframe_raw(L->cframe); - const BCIns *oldpc = cframe_pc(cf); - global_State *g = G(L); - BCReg slots; - setcframe_pc(cf, pc); - slots = cur_topslot(pt, pc, cframe_multres_n(cf)); - L->top = L->base + slots; /* Fix top. */ -#if LJ_HASJIT - { - jit_State *J = G2J(g); - if (J->state != LJ_TRACE_IDLE) { -#ifdef LUA_USE_ASSERT - ptrdiff_t delta = L->top - L->base; -#endif - J->L = L; - lj_trace_ins(J, pc-1); /* The interpreter bytecode PC is offset by 1. */ - lua_assert(L->top - L->base == delta); - } - } -#endif - if ((g->hookmask & LUA_MASKCOUNT) && g->hookcount == 0) { - g->hookcount = g->hookcstart; - callhook(L, LUA_HOOKCOUNT, -1); - L->top = L->base + slots; /* Fix top again. */ - } - if ((g->hookmask & LUA_MASKLINE)) { - BCPos npc = proto_bcpos(pt, pc) - 1; - BCPos opc = proto_bcpos(pt, oldpc) - 1; - BCLine line = lj_debug_line(pt, npc); - if (pc <= oldpc || opc >= pt->sizebc || line != lj_debug_line(pt, opc)) { - callhook(L, LUA_HOOKLINE, line); - L->top = L->base + slots; /* Fix top again. */ - } - } - if ((g->hookmask & LUA_MASKRET) && bc_isret(bc_op(pc[-1]))) - callhook(L, LUA_HOOKRET, -1); - ERRNO_RESTORE -} - -/* Initialize call. Ensure stack space and return # of missing parameters. */ -static int call_init(lua_State *L, GCfunc *fn) -{ - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - int numparams = pt->numparams; - int gotparams = (int)(L->top - L->base); - int need = pt->framesize; - if ((pt->flags & PROTO_VARARG)) need += 1+gotparams; - lj_state_checkstack(L, (MSize)need); - numparams -= gotparams; - return numparams >= 0 ? numparams : 0; - } else { - lj_state_checkstack(L, LUA_MINSTACK); - return 0; - } -} - -/* Call dispatch. Used by call hooks, hot calls or when recording. */ -ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns *pc) -{ - ERRNO_SAVE - GCfunc *fn = curr_func(L); - BCOp op; - global_State *g = G(L); -#if LJ_HASJIT - jit_State *J = G2J(g); -#endif - int missing = call_init(L, fn); -#if LJ_HASJIT - J->L = L; - if ((uintptr_t)pc & 1) { /* Marker for hot call. */ -#ifdef LUA_USE_ASSERT - ptrdiff_t delta = L->top - L->base; -#endif - pc = (const BCIns *)((uintptr_t)pc & ~(uintptr_t)1); - lj_trace_hot(J, pc); - lua_assert(L->top - L->base == delta); - goto out; - } else if (J->state != LJ_TRACE_IDLE && - !(g->hookmask & (HOOK_GC|HOOK_VMEVENT))) { -#ifdef LUA_USE_ASSERT - ptrdiff_t delta = L->top - L->base; -#endif - /* Record the FUNC* bytecodes, too. */ - lj_trace_ins(J, pc-1); /* The interpreter bytecode PC is offset by 1. */ - lua_assert(L->top - L->base == delta); - } -#endif - if ((g->hookmask & LUA_MASKCALL)) { - int i; - for (i = 0; i < missing; i++) /* Add missing parameters. */ - setnilV(L->top++); - callhook(L, LUA_HOOKCALL, -1); - /* Preserve modifications of missing parameters by lua_setlocal(). */ - while (missing-- > 0 && tvisnil(L->top - 1)) - L->top--; - } -#if LJ_HASJIT -out: -#endif - op = bc_op(pc[-1]); /* Get FUNC* op. */ -#if LJ_HASJIT - /* Use the non-hotcounting variants if JIT is off or while recording. */ - if ((!(J->flags & JIT_F_ON) || J->state != LJ_TRACE_IDLE) && - (op == BC_FUNCF || op == BC_FUNCV)) - op = (BCOp)((int)op+(int)BC_IFUNCF-(int)BC_FUNCF); -#endif - ERRNO_RESTORE - return makeasmfunc(lj_bc_ofs[op]); /* Return static dispatch target. */ -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_dispatch.h b/third-party/LuaJIT-2.0.2/src/lj_dispatch.h deleted file mode 100644 index a56b626021..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_dispatch.h +++ /dev/null @@ -1,131 +0,0 @@ -/* -** Instruction dispatch handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_DISPATCH_H -#define _LJ_DISPATCH_H - -#include "lj_obj.h" -#include "lj_bc.h" -#if LJ_HASJIT -#include "lj_jit.h" -#endif - -#if LJ_TARGET_MIPS -/* Need our own global offset table for the dreaded MIPS calling conventions. */ -#if LJ_HASJIT -#define JITGOTDEF(_) _(lj_trace_exit) _(lj_trace_hot) -#else -#define JITGOTDEF(_) -#endif -#if LJ_HASFFI -#define FFIGOTDEF(_) \ - _(lj_meta_equal_cd) _(lj_ccallback_enter) _(lj_ccallback_leave) -#else -#define FFIGOTDEF(_) -#endif -#define GOTDEF(_) \ - _(floor) _(ceil) _(trunc) _(log) _(log10) _(exp) _(sin) _(cos) _(tan) \ - _(asin) _(acos) _(atan) _(sinh) _(cosh) _(tanh) _(frexp) _(modf) _(atan2) \ - _(pow) _(fmod) _(ldexp) \ - _(lj_dispatch_call) _(lj_dispatch_ins) _(lj_err_throw) \ - _(lj_ffh_coroutine_wrap_err) _(lj_func_closeuv) _(lj_func_newL_gc) \ - _(lj_gc_barrieruv) _(lj_gc_step) _(lj_gc_step_fixtop) _(lj_meta_arith) \ - _(lj_meta_call) _(lj_meta_cat) _(lj_meta_comp) _(lj_meta_equal) \ - _(lj_meta_for) _(lj_meta_len) _(lj_meta_tget) _(lj_meta_tset) \ - _(lj_state_growstack) _(lj_str_fromnum) _(lj_str_fromnumber) _(lj_str_new) \ - _(lj_tab_dup) _(lj_tab_get) _(lj_tab_getinth) _(lj_tab_len) _(lj_tab_new) \ - _(lj_tab_newkey) _(lj_tab_next) _(lj_tab_reasize) \ - JITGOTDEF(_) FFIGOTDEF(_) - -enum { -#define GOTENUM(name) LJ_GOT_##name, -GOTDEF(GOTENUM) -#undef GOTENUM - LJ_GOT__MAX -}; -#endif - -/* Type of hot counter. Must match the code in the assembler VM. */ -/* 16 bits are sufficient. Only 0.0015% overhead with maximum slot penalty. */ -typedef uint16_t HotCount; - -/* Number of hot counter hash table entries (must be a power of two). */ -#define HOTCOUNT_SIZE 64 -#define HOTCOUNT_PCMASK ((HOTCOUNT_SIZE-1)*sizeof(HotCount)) - -/* Hotcount decrements. */ -#define HOTCOUNT_LOOP 2 -#define HOTCOUNT_CALL 1 - -/* This solves a circular dependency problem -- bump as needed. Sigh. */ -#define GG_NUM_ASMFF 62 - -#define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF) -#define GG_LEN_SDISP BC_FUNCF -#define GG_LEN_DISP (GG_LEN_DDISP + GG_LEN_SDISP) - -/* Global state, main thread and extra fields are allocated together. */ -typedef struct GG_State { - lua_State L; /* Main thread. */ - global_State g; /* Global state. */ -#if LJ_TARGET_MIPS - ASMFunction got[LJ_GOT__MAX]; /* Global offset table. */ -#endif -#if LJ_HASJIT - jit_State J; /* JIT state. */ - HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */ -#endif - ASMFunction dispatch[GG_LEN_DISP]; /* Instruction dispatch tables. */ - BCIns bcff[GG_NUM_ASMFF]; /* Bytecode for ASM fast functions. */ -} GG_State; - -#define GG_OFS(field) ((int)offsetof(GG_State, field)) -#define G2GG(gl) ((GG_State *)((char *)(gl) - GG_OFS(g))) -#define J2GG(j) ((GG_State *)((char *)(j) - GG_OFS(J))) -#define L2GG(L) (G2GG(G(L))) -#define J2G(J) (&J2GG(J)->g) -#define G2J(gl) (&G2GG(gl)->J) -#define L2J(L) (&L2GG(L)->J) -#define GG_G2DISP (GG_OFS(dispatch) - GG_OFS(g)) -#define GG_DISP2G (GG_OFS(g) - GG_OFS(dispatch)) -#define GG_DISP2J (GG_OFS(J) - GG_OFS(dispatch)) -#define GG_DISP2HOT (GG_OFS(hotcount) - GG_OFS(dispatch)) -#define GG_DISP2STATIC (GG_LEN_DDISP*(int)sizeof(ASMFunction)) - -#define hotcount_get(gg, pc) \ - (gg)->hotcount[(u32ptr(pc)>>2) & (HOTCOUNT_SIZE-1)] -#define hotcount_set(gg, pc, val) \ - (hotcount_get((gg), (pc)) = (HotCount)(val)) - -/* Dispatch table management. */ -LJ_FUNC void lj_dispatch_init(GG_State *GG); -#if LJ_HASJIT -LJ_FUNC void lj_dispatch_init_hotcount(global_State *g); -#endif -LJ_FUNC void lj_dispatch_update(global_State *g); - -/* Instruction dispatch callback for hooks or when recording. */ -LJ_FUNCA void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc); -LJ_FUNCA ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns*pc); -LJ_FUNCA void LJ_FASTCALL lj_dispatch_return(lua_State *L, const BCIns *pc); - -#if LJ_HASFFI && !defined(_BUILDVM_H) -/* Save/restore errno and GetLastError() around hooks, exits and recording. */ -#include -#if LJ_TARGET_WINDOWS -#define WIN32_LEAN_AND_MEAN -#include -#define ERRNO_SAVE int olderr = errno; DWORD oldwerr = GetLastError(); -#define ERRNO_RESTORE errno = olderr; SetLastError(oldwerr); -#else -#define ERRNO_SAVE int olderr = errno; -#define ERRNO_RESTORE errno = olderr; -#endif -#else -#define ERRNO_SAVE -#define ERRNO_RESTORE -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_emit_arm.h b/third-party/LuaJIT-2.0.2/src/lj_emit_arm.h deleted file mode 100644 index b76a9a45f9..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_emit_arm.h +++ /dev/null @@ -1,356 +0,0 @@ -/* -** ARM instruction emitter. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Constant encoding --------------------------------------------------- */ - -static uint8_t emit_invai[16] = { - /* AND */ (ARMI_AND^ARMI_BIC) >> 21, - /* EOR */ 0, - /* SUB */ (ARMI_SUB^ARMI_ADD) >> 21, - /* RSB */ 0, - /* ADD */ (ARMI_ADD^ARMI_SUB) >> 21, - /* ADC */ (ARMI_ADC^ARMI_SBC) >> 21, - /* SBC */ (ARMI_SBC^ARMI_ADC) >> 21, - /* RSC */ 0, - /* TST */ 0, - /* TEQ */ 0, - /* CMP */ (ARMI_CMP^ARMI_CMN) >> 21, - /* CMN */ (ARMI_CMN^ARMI_CMP) >> 21, - /* ORR */ 0, - /* MOV */ (ARMI_MOV^ARMI_MVN) >> 21, - /* BIC */ (ARMI_BIC^ARMI_AND) >> 21, - /* MVN */ (ARMI_MVN^ARMI_MOV) >> 21 -}; - -/* Encode constant in K12 format for data processing instructions. */ -static uint32_t emit_isk12(ARMIns ai, int32_t n) -{ - uint32_t invai, i, m = (uint32_t)n; - /* K12: unsigned 8 bit value, rotated in steps of two bits. */ - for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) - if (m <= 255) return ARMI_K12|m|i; - /* Otherwise try negation/complement with the inverse instruction. */ - invai = emit_invai[((ai >> 21) & 15)]; - if (!invai) return 0; /* Failed. No inverse instruction. */ - m = ~(uint32_t)n; - if (invai == ((ARMI_SUB^ARMI_ADD) >> 21) || - invai == (ARMI_CMP^ARMI_CMN) >> 21) m++; - for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) - if (m <= 255) return ARMI_K12|(invai<<21)|m|i; - return 0; /* Failed. */ -} - -/* -- Emit basic instructions --------------------------------------------- */ - -static void emit_dnm(ASMState *as, ARMIns ai, Reg rd, Reg rn, Reg rm) -{ - *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn) | ARMF_M(rm); -} - -static void emit_dm(ASMState *as, ARMIns ai, Reg rd, Reg rm) -{ - *--as->mcp = ai | ARMF_D(rd) | ARMF_M(rm); -} - -static void emit_dn(ASMState *as, ARMIns ai, Reg rd, Reg rn) -{ - *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn); -} - -static void emit_nm(ASMState *as, ARMIns ai, Reg rn, Reg rm) -{ - *--as->mcp = ai | ARMF_N(rn) | ARMF_M(rm); -} - -static void emit_d(ASMState *as, ARMIns ai, Reg rd) -{ - *--as->mcp = ai | ARMF_D(rd); -} - -static void emit_n(ASMState *as, ARMIns ai, Reg rn) -{ - *--as->mcp = ai | ARMF_N(rn); -} - -static void emit_m(ASMState *as, ARMIns ai, Reg rm) -{ - *--as->mcp = ai | ARMF_M(rm); -} - -static void emit_lsox(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) -{ - lua_assert(ofs >= -255 && ofs <= 255); - if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; - *--as->mcp = ai | ARMI_LS_P | ARMI_LSX_I | ARMF_D(rd) | ARMF_N(rn) | - ((ofs & 0xf0) << 4) | (ofs & 0x0f); -} - -static void emit_lso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) -{ - lua_assert(ofs >= -4095 && ofs <= 4095); - /* Combine LDR/STR pairs to LDRD/STRD. */ - if (*as->mcp == (ai|ARMI_LS_P|ARMI_LS_U|ARMF_D(rd^1)|ARMF_N(rn)|(ofs^4)) && - (ai & ~(ARMI_LDR^ARMI_STR)) == ARMI_STR && rd != rn && - (uint32_t)ofs <= 252 && !(ofs & 3) && !((rd ^ (ofs >>2)) & 1) && - as->mcp != as->mcloop) { - as->mcp++; - emit_lsox(as, ai == ARMI_LDR ? ARMI_LDRD : ARMI_STRD, rd&~1, rn, ofs&~4); - return; - } - if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; - *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd) | ARMF_N(rn) | ofs; -} - -#if !LJ_SOFTFP -static void emit_vlso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) -{ - lua_assert(ofs >= -1020 && ofs <= 1020 && (ofs&3) == 0); - if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; - *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd & 15) | ARMF_N(rn) | (ofs >> 2); -} -#endif - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Prefer spills of BASE/L. */ -#define emit_canremat(ref) ((ref) < ASMREF_L) - -/* Try to find a one step delta relative to another constant. */ -static int emit_kdelta1(ASMState *as, Reg d, int32_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lua_assert(r != d); - if (emit_canremat(ref)) { - int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); - uint32_t k = emit_isk12(ARMI_ADD, delta); - if (k) { - if (k == ARMI_K12) - emit_dm(as, ARMI_MOV, d, r); - else - emit_dn(as, ARMI_ADD^k, d, r); - return 1; - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Try to find a two step delta relative to another constant. */ -static int emit_kdelta2(ASMState *as, Reg d, int32_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lua_assert(r != d); - if (emit_canremat(ref)) { - int32_t other = ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i; - if (other) { - int32_t delta = i - other; - uint32_t sh, inv = 0, k2, k; - if (delta < 0) { delta = -delta; inv = ARMI_ADD^ARMI_SUB; } - sh = lj_ffs(delta) & ~1; - k2 = emit_isk12(0, delta & (255 << sh)); - k = emit_isk12(0, delta & ~(255 << sh)); - if (k) { - emit_dn(as, ARMI_ADD^k2^inv, d, d); - emit_dn(as, ARMI_ADD^k^inv, d, r); - return 1; - } - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Load a 32 bit constant into a GPR. */ -static void emit_loadi(ASMState *as, Reg r, int32_t i) -{ - uint32_t k = emit_isk12(ARMI_MOV, i); - lua_assert(rset_test(as->freeset, r) || r == RID_TMP); - if (k) { - /* Standard K12 constant. */ - emit_d(as, ARMI_MOV^k, r); - } else if ((as->flags & JIT_F_ARMV6T2) && (uint32_t)i < 0x00010000u) { - /* 16 bit loword constant for ARMv6T2. */ - emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r); - } else if (emit_kdelta1(as, r, i)) { - /* One step delta relative to another constant. */ - } else if ((as->flags & JIT_F_ARMV6T2)) { - /* 32 bit hiword/loword constant for ARMv6T2. */ - emit_d(as, ARMI_MOVT|((i>>16) & 0x0fff)|(((i>>16) & 0xf000)<<4), r); - emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r); - } else if (emit_kdelta2(as, r, i)) { - /* Two step delta relative to another constant. */ - } else { - /* Otherwise construct the constant with up to 4 instructions. */ - /* NYI: use mvn+bic, use pc-relative loads. */ - for (;;) { - uint32_t sh = lj_ffs(i) & ~1; - int32_t m = i & (255 << sh); - i &= ~(255 << sh); - if (i == 0) { - emit_d(as, ARMI_MOV ^ emit_isk12(0, m), r); - break; - } - emit_dn(as, ARMI_ORR ^ emit_isk12(0, m), r, r); - } - } -} - -#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) - -static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); - -/* Get/set from constant pointer. */ -static void emit_lsptr(ASMState *as, ARMIns ai, Reg r, void *p) -{ - int32_t i = i32ptr(p); - emit_lso(as, ai, r, ra_allock(as, (i & ~4095), rset_exclude(RSET_GPR, r)), - (i & 4095)); -} - -#if !LJ_SOFTFP -/* Load a number constant into an FPR. */ -static void emit_loadn(ASMState *as, Reg r, cTValue *tv) -{ - int32_t i; - if ((as->flags & JIT_F_VFPV3) && !tv->u32.lo) { - uint32_t hi = tv->u32.hi; - uint32_t b = ((hi >> 22) & 0x1ff); - if (!(hi & 0xffff) && (b == 0x100 || b == 0x0ff)) { - *--as->mcp = ARMI_VMOVI_D | ARMF_D(r & 15) | - ((tv->u32.hi >> 12) & 0x00080000) | - ((tv->u32.hi >> 4) & 0x00070000) | - ((tv->u32.hi >> 16) & 0x0000000f); - return; - } - } - i = i32ptr(tv); - emit_vlso(as, ARMI_VLDR_D, r, - ra_allock(as, (i & ~1020), RSET_GPR), (i & 1020)); -} -#endif - -/* Get/set global_State fields. */ -#define emit_getgl(as, r, field) \ - emit_lsptr(as, ARMI_LDR, (r), (void *)&J2G(as->J)->field) -#define emit_setgl(as, r, field) \ - emit_lsptr(as, ARMI_STR, (r), (void *)&J2G(as->J)->field) - -/* Trace number is determined from pc of exit instruction. */ -#define emit_setvmstate(as, i) UNUSED(i) - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for internal jumps. */ -typedef MCode *MCLabel; - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -static void emit_branch(ASMState *as, ARMIns ai, MCode *target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = (target - p) - 1; - lua_assert(((delta + 0x00800000) >> 24) == 0); - *--p = ai | ((uint32_t)delta & 0x00ffffffu); - as->mcp = p; -} - -#define emit_jmp(as, target) emit_branch(as, ARMI_B, (target)) - -static void emit_call(ASMState *as, void *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = ((char *)target - (char *)p) - 8; - if ((((delta>>2) + 0x00800000) >> 24) == 0) { - if ((delta & 1)) - *p = ARMI_BLX | ((uint32_t)(delta>>2) & 0x00ffffffu) | ((delta&2) << 27); - else - *p = ARMI_BL | ((uint32_t)(delta>>2) & 0x00ffffffu); - } else { /* Target out of range: need indirect call. But don't use R0-R3. */ - Reg r = ra_allock(as, i32ptr(target), RSET_RANGE(RID_R4, RID_R12+1)); - *p = ARMI_BLXr | ARMF_M(r); - } -} - -/* -- Emit generic operations --------------------------------------------- */ - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ -#if LJ_SOFTFP - lua_assert(!irt_isnum(ir->t)); UNUSED(ir); -#else - if (dst >= RID_MAX_GPR) { - emit_dm(as, irt_isnum(ir->t) ? ARMI_VMOV_D : ARMI_VMOV_S, - (dst & 15), (src & 15)); - return; - } -#endif - if (as->mcp != as->mcloop) { /* Swap early registers for loads/stores. */ - MCode ins = *as->mcp, swp = (src^dst); - if ((ins & 0x0c000000) == 0x04000000 && (ins & 0x02000010) != 0x02000010) { - if (!((ins ^ (dst << 16)) & 0x000f0000)) - *as->mcp = ins ^ (swp << 16); /* Swap N in load/store. */ - if (!(ins & 0x00100000) && !((ins ^ (dst << 12)) & 0x0000f000)) - *as->mcp = ins ^ (swp << 12); /* Swap D in store. */ - } - } - emit_dm(as, ARMI_MOV, dst, src); -} - -/* Generic load of register from stack slot. */ -static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ -#if LJ_SOFTFP - lua_assert(!irt_isnum(ir->t)); UNUSED(ir); -#else - if (r >= RID_MAX_GPR) - emit_vlso(as, irt_isnum(ir->t) ? ARMI_VLDR_D : ARMI_VLDR_S, r, RID_SP, ofs); - else -#endif - emit_lso(as, ARMI_LDR, r, RID_SP, ofs); -} - -/* Generic store of register to stack slot. */ -static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ -#if LJ_SOFTFP - lua_assert(!irt_isnum(ir->t)); UNUSED(ir); -#else - if (r >= RID_MAX_GPR) - emit_vlso(as, irt_isnum(ir->t) ? ARMI_VSTR_D : ARMI_VSTR_S, r, RID_SP, ofs); - else -#endif - emit_lso(as, ARMI_STR, r, RID_SP, ofs); -} - -/* Emit an arithmetic/logic operation with a constant operand. */ -static void emit_opk(ASMState *as, ARMIns ai, Reg dest, Reg src, - int32_t i, RegSet allow) -{ - uint32_t k = emit_isk12(ai, i); - if (k) - emit_dn(as, ai^k, dest, src); - else - emit_dnm(as, ai, dest, src, ra_allock(as, i, allow)); -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) - emit_opk(as, ARMI_ADD, r, r, ofs, rset_exclude(RSET_GPR, r)); -} - -#define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) - diff --git a/third-party/LuaJIT-2.0.2/src/lj_emit_mips.h b/third-party/LuaJIT-2.0.2/src/lj_emit_mips.h deleted file mode 100644 index 74821b8b87..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_emit_mips.h +++ /dev/null @@ -1,211 +0,0 @@ -/* -** MIPS instruction emitter. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Emit basic instructions --------------------------------------------- */ - -static void emit_dst(ASMState *as, MIPSIns mi, Reg rd, Reg rs, Reg rt) -{ - *--as->mcp = mi | MIPSF_D(rd) | MIPSF_S(rs) | MIPSF_T(rt); -} - -static void emit_dta(ASMState *as, MIPSIns mi, Reg rd, Reg rt, uint32_t a) -{ - *--as->mcp = mi | MIPSF_D(rd) | MIPSF_T(rt) | MIPSF_A(a); -} - -#define emit_ds(as, mi, rd, rs) emit_dst(as, (mi), (rd), (rs), 0) -#define emit_tg(as, mi, rt, rg) emit_dst(as, (mi), (rg)&31, 0, (rt)) - -static void emit_tsi(ASMState *as, MIPSIns mi, Reg rt, Reg rs, int32_t i) -{ - *--as->mcp = mi | MIPSF_T(rt) | MIPSF_S(rs) | (i & 0xffff); -} - -#define emit_ti(as, mi, rt, i) emit_tsi(as, (mi), (rt), 0, (i)) -#define emit_hsi(as, mi, rh, rs, i) emit_tsi(as, (mi), (rh) & 31, (rs), (i)) - -static void emit_fgh(ASMState *as, MIPSIns mi, Reg rf, Reg rg, Reg rh) -{ - *--as->mcp = mi | MIPSF_F(rf&31) | MIPSF_G(rg&31) | MIPSF_H(rh&31); -} - -#define emit_fg(as, mi, rf, rg) emit_fgh(as, (mi), (rf), (rg), 0) - -static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift) -{ - if ((as->flags & JIT_F_MIPS32R2)) { - emit_dta(as, MIPSI_ROTR, dest, src, shift); - } else { - emit_dst(as, MIPSI_OR, dest, dest, tmp); - emit_dta(as, MIPSI_SLL, dest, src, (-shift)&31); - emit_dta(as, MIPSI_SRL, tmp, src, shift); - } -} - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Prefer rematerialization of BASE/L from global_State over spills. */ -#define emit_canremat(ref) ((ref) <= REF_BASE) - -/* Try to find a one step delta relative to another constant. */ -static int emit_kdelta1(ASMState *as, Reg t, int32_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lua_assert(r != t); - if (ref < ASMREF_L) { - int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); - if (checki16(delta)) { - emit_tsi(as, MIPSI_ADDIU, t, r, delta); - return 1; - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Load a 32 bit constant into a GPR. */ -static void emit_loadi(ASMState *as, Reg r, int32_t i) -{ - if (checki16(i)) { - emit_ti(as, MIPSI_LI, r, i); - } else { - if ((i & 0xffff)) { - int32_t jgl = i32ptr(J2G(as->J)); - if ((uint32_t)(i-jgl) < 65536) { - emit_tsi(as, MIPSI_ADDIU, r, RID_JGL, i-jgl-32768); - return; - } else if (emit_kdelta1(as, r, i)) { - return; - } else if ((i >> 16) == 0) { - emit_tsi(as, MIPSI_ORI, r, RID_ZERO, i); - return; - } - emit_tsi(as, MIPSI_ORI, r, r, i); - } - emit_ti(as, MIPSI_LUI, r, (i >> 16)); - } -} - -#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) - -static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); -static void ra_allockreg(ASMState *as, int32_t k, Reg r); - -/* Get/set from constant pointer. */ -static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow) -{ - int32_t jgl = i32ptr(J2G(as->J)); - int32_t i = i32ptr(p); - Reg base; - if ((uint32_t)(i-jgl) < 65536) { - i = i-jgl-32768; - base = RID_JGL; - } else { - base = ra_allock(as, i-(int16_t)i, allow); - } - emit_tsi(as, mi, r, base, i); -} - -#define emit_loadn(as, r, tv) \ - emit_lsptr(as, MIPSI_LDC1, ((r) & 31), (void *)(tv), RSET_GPR) - -/* Get/set global_State fields. */ -static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs) -{ - emit_tsi(as, mi, r, RID_JGL, ofs-32768); -} - -#define emit_getgl(as, r, field) \ - emit_lsglptr(as, MIPSI_LW, (r), (int32_t)offsetof(global_State, field)) -#define emit_setgl(as, r, field) \ - emit_lsglptr(as, MIPSI_SW, (r), (int32_t)offsetof(global_State, field)) - -/* Trace number is determined from per-trace exit stubs. */ -#define emit_setvmstate(as, i) UNUSED(i) - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for internal jumps. */ -typedef MCode *MCLabel; - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -static void emit_branch(ASMState *as, MIPSIns mi, Reg rs, Reg rt, MCode *target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = target - p; - lua_assert(((delta + 0x8000) >> 16) == 0); - *--p = mi | MIPSF_S(rs) | MIPSF_T(rt) | ((uint32_t)delta & 0xffffu); - as->mcp = p; -} - -static void emit_jmp(ASMState *as, MCode *target) -{ - *--as->mcp = MIPSI_NOP; - emit_branch(as, MIPSI_B, RID_ZERO, RID_ZERO, (target)); -} - -static void emit_call(ASMState *as, void *target) -{ - MCode *p = as->mcp; - *--p = MIPSI_NOP; - if ((((uintptr_t)target ^ (uintptr_t)p) >> 28) == 0) - *--p = MIPSI_JAL | (((uintptr_t)target >>2) & 0x03ffffffu); - else /* Target out of range: need indirect call. */ - *--p = MIPSI_JALR | MIPSF_S(RID_CFUNCADDR); - as->mcp = p; - ra_allockreg(as, i32ptr(target), RID_CFUNCADDR); -} - -/* -- Emit generic operations --------------------------------------------- */ - -#define emit_move(as, dst, src) \ - emit_ds(as, MIPSI_MOVE, (dst), (src)) - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ - if (dst < RID_MAX_GPR) - emit_move(as, dst, src); - else - emit_fg(as, irt_isnum(ir->t) ? MIPSI_MOV_D : MIPSI_MOV_S, dst, src); -} - -/* Generic load of register from stack slot. */ -static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tsi(as, MIPSI_LW, r, RID_SP, ofs); - else - emit_tsi(as, irt_isnum(ir->t) ? MIPSI_LDC1 : MIPSI_LWC1, - (r & 31), RID_SP, ofs); -} - -/* Generic store of register to stack slot. */ -static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tsi(as, MIPSI_SW, r, RID_SP, ofs); - else - emit_tsi(as, irt_isnum(ir->t) ? MIPSI_SDC1 : MIPSI_SWC1, - (r&31), RID_SP, ofs); -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) { - lua_assert(checki16(ofs)); - emit_tsi(as, MIPSI_ADDIU, r, r, ofs); - } -} - -#define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) - diff --git a/third-party/LuaJIT-2.0.2/src/lj_emit_ppc.h b/third-party/LuaJIT-2.0.2/src/lj_emit_ppc.h deleted file mode 100644 index a589f3a607..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_emit_ppc.h +++ /dev/null @@ -1,238 +0,0 @@ -/* -** PPC instruction emitter. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Emit basic instructions --------------------------------------------- */ - -static void emit_tab(ASMState *as, PPCIns pi, Reg rt, Reg ra, Reg rb) -{ - *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | PPCF_B(rb); -} - -#define emit_asb(as, pi, ra, rs, rb) emit_tab(as, (pi), (rs), (ra), (rb)) -#define emit_as(as, pi, ra, rs) emit_tab(as, (pi), (rs), (ra), 0) -#define emit_ab(as, pi, ra, rb) emit_tab(as, (pi), 0, (ra), (rb)) - -static void emit_tai(ASMState *as, PPCIns pi, Reg rt, Reg ra, int32_t i) -{ - *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | (i & 0xffff); -} - -#define emit_ti(as, pi, rt, i) emit_tai(as, (pi), (rt), 0, (i)) -#define emit_ai(as, pi, ra, i) emit_tai(as, (pi), 0, (ra), (i)) -#define emit_asi(as, pi, ra, rs, i) emit_tai(as, (pi), (rs), (ra), (i)) - -#define emit_fab(as, pi, rf, ra, rb) \ - emit_tab(as, (pi), (rf)&31, (ra)&31, (rb)&31) -#define emit_fb(as, pi, rf, rb) emit_tab(as, (pi), (rf)&31, 0, (rb)&31) -#define emit_fac(as, pi, rf, ra, rc) \ - emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, 0) -#define emit_facb(as, pi, rf, ra, rc, rb) \ - emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, (rb)&31) -#define emit_fai(as, pi, rf, ra, i) emit_tai(as, (pi), (rf)&31, (ra), (i)) - -static void emit_rot(ASMState *as, PPCIns pi, Reg ra, Reg rs, - int32_t n, int32_t b, int32_t e) -{ - *--as->mcp = pi | PPCF_T(rs) | PPCF_A(ra) | PPCF_B(n) | - PPCF_MB(b) | PPCF_ME(e); -} - -static void emit_slwi(ASMState *as, Reg ra, Reg rs, int32_t n) -{ - lua_assert(n >= 0 && n < 32); - emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31-n); -} - -static void emit_rotlwi(ASMState *as, Reg ra, Reg rs, int32_t n) -{ - lua_assert(n >= 0 && n < 32); - emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31); -} - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Prefer rematerialization of BASE/L from global_State over spills. */ -#define emit_canremat(ref) ((ref) <= REF_BASE) - -/* Try to find a one step delta relative to another constant. */ -static int emit_kdelta1(ASMState *as, Reg t, int32_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lua_assert(r != t); - if (ref < ASMREF_L) { - int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); - if (checki16(delta)) { - emit_tai(as, PPCI_ADDI, t, r, delta); - return 1; - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Load a 32 bit constant into a GPR. */ -static void emit_loadi(ASMState *as, Reg r, int32_t i) -{ - if (checki16(i)) { - emit_ti(as, PPCI_LI, r, i); - } else { - if ((i & 0xffff)) { - int32_t jgl = i32ptr(J2G(as->J)); - if ((uint32_t)(i-jgl) < 65536) { - emit_tai(as, PPCI_ADDI, r, RID_JGL, i-jgl-32768); - return; - } else if (emit_kdelta1(as, r, i)) { - return; - } - emit_asi(as, PPCI_ORI, r, r, i); - } - emit_ti(as, PPCI_LIS, r, (i >> 16)); - } -} - -#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) - -static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); - -/* Get/set from constant pointer. */ -static void emit_lsptr(ASMState *as, PPCIns pi, Reg r, void *p, RegSet allow) -{ - int32_t jgl = i32ptr(J2G(as->J)); - int32_t i = i32ptr(p); - Reg base; - if ((uint32_t)(i-jgl) < 65536) { - i = i-jgl-32768; - base = RID_JGL; - } else { - base = ra_allock(as, i-(int16_t)i, allow); - } - emit_tai(as, pi, r, base, i); -} - -#define emit_loadn(as, r, tv) \ - emit_lsptr(as, PPCI_LFD, ((r) & 31), (void *)(tv), RSET_GPR) - -/* Get/set global_State fields. */ -static void emit_lsglptr(ASMState *as, PPCIns pi, Reg r, int32_t ofs) -{ - emit_tai(as, pi, r, RID_JGL, ofs-32768); -} - -#define emit_getgl(as, r, field) \ - emit_lsglptr(as, PPCI_LWZ, (r), (int32_t)offsetof(global_State, field)) -#define emit_setgl(as, r, field) \ - emit_lsglptr(as, PPCI_STW, (r), (int32_t)offsetof(global_State, field)) - -/* Trace number is determined from per-trace exit stubs. */ -#define emit_setvmstate(as, i) UNUSED(i) - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for internal jumps. */ -typedef MCode *MCLabel; - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -static void emit_condbranch(ASMState *as, PPCIns pi, PPCCC cc, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = (char *)target - (char *)p; - lua_assert(((delta + 0x8000) >> 16) == 0); - pi ^= (delta & 0x8000) * (PPCF_Y/0x8000); - *p = pi | PPCF_CC(cc) | ((uint32_t)delta & 0xffffu); -} - -static void emit_jmp(ASMState *as, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = (char *)target - (char *)p; - *p = PPCI_B | (delta & 0x03fffffcu); -} - -static void emit_call(ASMState *as, void *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = (char *)target - (char *)p; - if ((((delta>>2) + 0x00800000) >> 24) == 0) { - *p = PPCI_BL | (delta & 0x03fffffcu); - } else { /* Target out of range: need indirect call. Don't use arg reg. */ - RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); - Reg r = ra_allock(as, i32ptr(target), allow); - *p = PPCI_BCTRL; - p[-1] = PPCI_MTCTR | PPCF_T(r); - as->mcp = p-1; - } -} - -/* -- Emit generic operations --------------------------------------------- */ - -#define emit_mr(as, dst, src) \ - emit_asb(as, PPCI_MR, (dst), (src), (src)) - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ - UNUSED(ir); - if (dst < RID_MAX_GPR) - emit_mr(as, dst, src); - else - emit_fb(as, PPCI_FMR, dst, src); -} - -/* Generic load of register from stack slot. */ -static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tai(as, PPCI_LWZ, r, RID_SP, ofs); - else - emit_fai(as, irt_isnum(ir->t) ? PPCI_LFD : PPCI_LFS, r, RID_SP, ofs); -} - -/* Generic store of register to stack slot. */ -static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tai(as, PPCI_STW, r, RID_SP, ofs); - else - emit_fai(as, irt_isnum(ir->t) ? PPCI_STFD : PPCI_STFS, r, RID_SP, ofs); -} - -/* Emit a compare (for equality) with a constant operand. */ -static void emit_cmpi(ASMState *as, Reg r, int32_t k) -{ - if (checki16(k)) { - emit_ai(as, PPCI_CMPWI, r, k); - } else if (checku16(k)) { - emit_ai(as, PPCI_CMPLWI, r, k); - } else { - emit_ai(as, PPCI_CMPLWI, RID_TMP, k); - emit_asi(as, PPCI_XORIS, RID_TMP, r, (k >> 16)); - } -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) { - emit_tai(as, PPCI_ADDI, r, r, ofs); - if (!checki16(ofs)) - emit_tai(as, PPCI_ADDIS, r, r, (ofs + 32768) >> 16); - } -} - -static void emit_spsub(ASMState *as, int32_t ofs) -{ - if (ofs) { - emit_tai(as, PPCI_STWU, RID_TMP, RID_SP, -ofs); - emit_tai(as, PPCI_ADDI, RID_TMP, RID_SP, - CFRAME_SIZE + (as->parent ? as->parent->spadjust : 0)); - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_emit_x86.h b/third-party/LuaJIT-2.0.2/src/lj_emit_x86.h deleted file mode 100644 index bd184a3058..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_emit_x86.h +++ /dev/null @@ -1,466 +0,0 @@ -/* -** x86/x64 instruction emitter. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Emit basic instructions --------------------------------------------- */ - -#define MODRM(mode, r1, r2) ((MCode)((mode)+(((r1)&7)<<3)+((r2)&7))) - -#if LJ_64 -#define REXRB(p, rr, rb) \ - { MCode rex = 0x40 + (((rr)>>1)&4) + (((rb)>>3)&1); \ - if (rex != 0x40) *--(p) = rex; } -#define FORCE_REX 0x200 -#define REX_64 (FORCE_REX|0x080000) -#else -#define REXRB(p, rr, rb) ((void)0) -#define FORCE_REX 0 -#define REX_64 0 -#endif - -#define emit_i8(as, i) (*--as->mcp = (MCode)(i)) -#define emit_i32(as, i) (*(int32_t *)(as->mcp-4) = (i), as->mcp -= 4) -#define emit_u32(as, u) (*(uint32_t *)(as->mcp-4) = (u), as->mcp -= 4) - -#define emit_x87op(as, xo) \ - (*(uint16_t *)(as->mcp-2) = (uint16_t)(xo), as->mcp -= 2) - -/* op */ -static LJ_AINLINE MCode *emit_op(x86Op xo, Reg rr, Reg rb, Reg rx, - MCode *p, int delta) -{ - int n = (int8_t)xo; -#if defined(__GNUC__) - if (__builtin_constant_p(xo) && n == -2) - p[delta-2] = (MCode)(xo >> 24); - else if (__builtin_constant_p(xo) && n == -3) - *(uint16_t *)(p+delta-3) = (uint16_t)(xo >> 16); - else -#endif - *(uint32_t *)(p+delta-5) = (uint32_t)xo; - p += n + delta; -#if LJ_64 - { - uint32_t rex = 0x40 + ((rr>>1)&(4+(FORCE_REX>>1)))+((rx>>2)&2)+((rb>>3)&1); - if (rex != 0x40) { - rex |= (rr >> 16); - if (n == -4) { *p = (MCode)rex; rex = (MCode)(xo >> 8); } - else if ((xo & 0xffffff) == 0x6600fd) { *p = (MCode)rex; rex = 0x66; } - *--p = (MCode)rex; - } - } -#else - UNUSED(rr); UNUSED(rb); UNUSED(rx); -#endif - return p; -} - -/* op + modrm */ -#define emit_opm(xo, mode, rr, rb, p, delta) \ - (p[(delta)-1] = MODRM((mode), (rr), (rb)), \ - emit_op((xo), (rr), (rb), 0, (p), (delta))) - -/* op + modrm + sib */ -#define emit_opmx(xo, mode, scale, rr, rb, rx, p) \ - (p[-1] = MODRM((scale), (rx), (rb)), \ - p[-2] = MODRM((mode), (rr), RID_ESP), \ - emit_op((xo), (rr), (rb), (rx), (p), -1)) - -/* op r1, r2 */ -static void emit_rr(ASMState *as, x86Op xo, Reg r1, Reg r2) -{ - MCode *p = as->mcp; - as->mcp = emit_opm(xo, XM_REG, r1, r2, p, 0); -} - -#if LJ_64 && defined(LUA_USE_ASSERT) -/* [addr] is sign-extended in x64 and must be in lower 2G (not 4G). */ -static int32_t ptr2addr(const void *p) -{ - lua_assert((uintptr_t)p < (uintptr_t)0x80000000); - return i32ptr(p); -} -#else -#define ptr2addr(p) (i32ptr((p))) -#endif - -/* op r, [addr] */ -static void emit_rma(ASMState *as, x86Op xo, Reg rr, const void *addr) -{ - MCode *p = as->mcp; - *(int32_t *)(p-4) = ptr2addr(addr); -#if LJ_64 - p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP); - as->mcp = emit_opm(xo, XM_OFS0, rr, RID_ESP, p, -5); -#else - as->mcp = emit_opm(xo, XM_OFS0, rr, RID_EBP, p, -4); -#endif -} - -/* op r, [base+ofs] */ -static void emit_rmro(ASMState *as, x86Op xo, Reg rr, Reg rb, int32_t ofs) -{ - MCode *p = as->mcp; - x86Mode mode; - if (ra_hasreg(rb)) { - if (ofs == 0 && (rb&7) != RID_EBP) { - mode = XM_OFS0; - } else if (checki8(ofs)) { - *--p = (MCode)ofs; - mode = XM_OFS8; - } else { - p -= 4; - *(int32_t *)p = ofs; - mode = XM_OFS32; - } - if ((rb&7) == RID_ESP) - *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP); - } else { - *(int32_t *)(p-4) = ofs; -#if LJ_64 - p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP); - p -= 5; - rb = RID_ESP; -#else - p -= 4; - rb = RID_EBP; -#endif - mode = XM_OFS0; - } - as->mcp = emit_opm(xo, mode, rr, rb, p, 0); -} - -/* op r, [base+idx*scale+ofs] */ -static void emit_rmrxo(ASMState *as, x86Op xo, Reg rr, Reg rb, Reg rx, - x86Mode scale, int32_t ofs) -{ - MCode *p = as->mcp; - x86Mode mode; - if (ofs == 0 && (rb&7) != RID_EBP) { - mode = XM_OFS0; - } else if (checki8(ofs)) { - mode = XM_OFS8; - *--p = (MCode)ofs; - } else { - mode = XM_OFS32; - p -= 4; - *(int32_t *)p = ofs; - } - as->mcp = emit_opmx(xo, mode, scale, rr, rb, rx, p); -} - -/* op r, i */ -static void emit_gri(ASMState *as, x86Group xg, Reg rb, int32_t i) -{ - MCode *p = as->mcp; - x86Op xo; - if (checki8(i)) { - *--p = (MCode)i; - xo = XG_TOXOi8(xg); - } else { - p -= 4; - *(int32_t *)p = i; - xo = XG_TOXOi(xg); - } - as->mcp = emit_opm(xo, XM_REG, (Reg)(xg & 7) | (rb & REX_64), rb, p, 0); -} - -/* op [base+ofs], i */ -static void emit_gmroi(ASMState *as, x86Group xg, Reg rb, int32_t ofs, - int32_t i) -{ - x86Op xo; - if (checki8(i)) { - emit_i8(as, i); - xo = XG_TOXOi8(xg); - } else { - emit_i32(as, i); - xo = XG_TOXOi(xg); - } - emit_rmro(as, xo, (Reg)(xg & 7), rb, ofs); -} - -#define emit_shifti(as, xg, r, i) \ - (emit_i8(as, (i)), emit_rr(as, XO_SHIFTi, (Reg)(xg), (r))) - -/* op r, rm/mrm */ -static void emit_mrm(ASMState *as, x86Op xo, Reg rr, Reg rb) -{ - MCode *p = as->mcp; - x86Mode mode = XM_REG; - if (rb == RID_MRM) { - rb = as->mrm.base; - if (rb == RID_NONE) { - rb = RID_EBP; - mode = XM_OFS0; - p -= 4; - *(int32_t *)p = as->mrm.ofs; - if (as->mrm.idx != RID_NONE) - goto mrmidx; -#if LJ_64 - *--p = MODRM(XM_SCALE1, RID_ESP, RID_EBP); - rb = RID_ESP; -#endif - } else { - if (as->mrm.ofs == 0 && (rb&7) != RID_EBP) { - mode = XM_OFS0; - } else if (checki8(as->mrm.ofs)) { - *--p = (MCode)as->mrm.ofs; - mode = XM_OFS8; - } else { - p -= 4; - *(int32_t *)p = as->mrm.ofs; - mode = XM_OFS32; - } - if (as->mrm.idx != RID_NONE) { - mrmidx: - as->mcp = emit_opmx(xo, mode, as->mrm.scale, rr, rb, as->mrm.idx, p); - return; - } - if ((rb&7) == RID_ESP) - *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP); - } - } - as->mcp = emit_opm(xo, mode, rr, rb, p, 0); -} - -/* op rm/mrm, i */ -static void emit_gmrmi(ASMState *as, x86Group xg, Reg rb, int32_t i) -{ - x86Op xo; - if (checki8(i)) { - emit_i8(as, i); - xo = XG_TOXOi8(xg); - } else { - emit_i32(as, i); - xo = XG_TOXOi(xg); - } - emit_mrm(as, xo, (Reg)(xg & 7) | (rb & REX_64), (rb & ~REX_64)); -} - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Instruction selection for XMM moves. */ -#define XMM_MOVRR(as) ((as->flags & JIT_F_SPLIT_XMM) ? XO_MOVSD : XO_MOVAPS) -#define XMM_MOVRM(as) ((as->flags & JIT_F_SPLIT_XMM) ? XO_MOVLPD : XO_MOVSD) - -/* mov [base+ofs], i */ -static void emit_movmroi(ASMState *as, Reg base, int32_t ofs, int32_t i) -{ - emit_i32(as, i); - emit_rmro(as, XO_MOVmi, 0, base, ofs); -} - -/* mov [base+ofs], r */ -#define emit_movtomro(as, r, base, ofs) \ - emit_rmro(as, XO_MOVto, (r), (base), (ofs)) - -/* Get/set global_State fields. */ -#define emit_opgl(as, xo, r, field) \ - emit_rma(as, (xo), (r), (void *)&J2G(as->J)->field) -#define emit_getgl(as, r, field) emit_opgl(as, XO_MOV, (r), field) -#define emit_setgl(as, r, field) emit_opgl(as, XO_MOVto, (r), field) - -#define emit_setvmstate(as, i) \ - (emit_i32(as, i), emit_opgl(as, XO_MOVmi, 0, vmstate)) - -/* mov r, i / xor r, r */ -static void emit_loadi(ASMState *as, Reg r, int32_t i) -{ - /* XOR r,r is shorter, but modifies the flags. This is bad for HIOP. */ - if (i == 0 && !(LJ_32 && (IR(as->curins)->o == IR_HIOP || - (as->curins+1 < as->T->nins && - IR(as->curins+1)->o == IR_HIOP)))) { - emit_rr(as, XO_ARITH(XOg_XOR), r, r); - } else { - MCode *p = as->mcp; - *(int32_t *)(p-4) = i; - p[-5] = (MCode)(XI_MOVri+(r&7)); - p -= 5; - REXRB(p, 0, r); - as->mcp = p; - } -} - -/* mov r, addr */ -#define emit_loada(as, r, addr) \ - emit_loadi(as, (r), ptr2addr((addr))) - -#if LJ_64 -/* mov r, imm64 or shorter 32 bit extended load. */ -static void emit_loadu64(ASMState *as, Reg r, uint64_t u64) -{ - if (checku32(u64)) { /* 32 bit load clears upper 32 bits. */ - emit_loadi(as, r, (int32_t)u64); - } else if (checki32((int64_t)u64)) { /* Sign-extended 32 bit load. */ - MCode *p = as->mcp; - *(int32_t *)(p-4) = (int32_t)u64; - as->mcp = emit_opm(XO_MOVmi, XM_REG, REX_64, r, p, -4); - } else { /* Full-size 64 bit load. */ - MCode *p = as->mcp; - *(uint64_t *)(p-8) = u64; - p[-9] = (MCode)(XI_MOVri+(r&7)); - p[-10] = 0x48 + ((r>>3)&1); - p -= 10; - as->mcp = p; - } -} -#endif - -/* movsd r, [&tv->n] / xorps r, r */ -static void emit_loadn(ASMState *as, Reg r, cTValue *tv) -{ - if (tvispzero(tv)) /* Use xor only for +0. */ - emit_rr(as, XO_XORPS, r, r); - else - emit_rma(as, XMM_MOVRM(as), r, &tv->n); -} - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for short jumps. */ -typedef MCode *MCLabel; - -#if LJ_32 && LJ_HASFFI -/* jmp short target */ -static void emit_sjmp(ASMState *as, MCLabel target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = target - p; - lua_assert(delta == (int8_t)delta); - p[-1] = (MCode)(int8_t)delta; - p[-2] = XI_JMPs; - as->mcp = p - 2; -} -#endif - -/* jcc short target */ -static void emit_sjcc(ASMState *as, int cc, MCLabel target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = target - p; - lua_assert(delta == (int8_t)delta); - p[-1] = (MCode)(int8_t)delta; - p[-2] = (MCode)(XI_JCCs+(cc&15)); - as->mcp = p - 2; -} - -/* jcc short (pending target) */ -static MCLabel emit_sjcc_label(ASMState *as, int cc) -{ - MCode *p = as->mcp; - p[-1] = 0; - p[-2] = (MCode)(XI_JCCs+(cc&15)); - as->mcp = p - 2; - return p; -} - -/* Fixup jcc short target. */ -static void emit_sfixup(ASMState *as, MCLabel source) -{ - source[-1] = (MCode)(as->mcp-source); -} - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -/* Compute relative 32 bit offset for jump and call instructions. */ -static LJ_AINLINE int32_t jmprel(MCode *p, MCode *target) -{ - ptrdiff_t delta = target - p; - lua_assert(delta == (int32_t)delta); - return (int32_t)delta; -} - -/* jcc target */ -static void emit_jcc(ASMState *as, int cc, MCode *target) -{ - MCode *p = as->mcp; - *(int32_t *)(p-4) = jmprel(p, target); - p[-5] = (MCode)(XI_JCCn+(cc&15)); - p[-6] = 0x0f; - as->mcp = p - 6; -} - -/* jmp target */ -static void emit_jmp(ASMState *as, MCode *target) -{ - MCode *p = as->mcp; - *(int32_t *)(p-4) = jmprel(p, target); - p[-5] = XI_JMP; - as->mcp = p - 5; -} - -/* call target */ -static void emit_call_(ASMState *as, MCode *target) -{ - MCode *p = as->mcp; -#if LJ_64 - if (target-p != (int32_t)(target-p)) { - /* Assumes RID_RET is never an argument to calls and always clobbered. */ - emit_rr(as, XO_GROUP5, XOg_CALL, RID_RET); - emit_loadu64(as, RID_RET, (uint64_t)target); - return; - } -#endif - *(int32_t *)(p-4) = jmprel(p, target); - p[-5] = XI_CALL; - as->mcp = p - 5; -} - -#define emit_call(as, f) emit_call_(as, (MCode *)(void *)(f)) - -/* -- Emit generic operations --------------------------------------------- */ - -/* Use 64 bit operations to handle 64 bit IR types. */ -#if LJ_64 -#define REX_64IR(ir, r) ((r) + (irt_is64((ir)->t) ? REX_64 : 0)) -#else -#define REX_64IR(ir, r) (r) -#endif - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ - UNUSED(ir); - if (dst < RID_MAX_GPR) - emit_rr(as, XO_MOV, REX_64IR(ir, dst), src); - else - emit_rr(as, XMM_MOVRR(as), dst, src); -} - -/* Generic load of register from stack slot. */ -static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_rmro(as, XO_MOV, REX_64IR(ir, r), RID_ESP, ofs); - else - emit_rmro(as, irt_isnum(ir->t) ? XMM_MOVRM(as) : XO_MOVSS, r, RID_ESP, ofs); -} - -/* Generic store of register to stack slot. */ -static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_rmro(as, XO_MOVto, REX_64IR(ir, r), RID_ESP, ofs); - else - emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, r, RID_ESP, ofs); -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) { - if ((as->flags & JIT_F_LEA_AGU)) - emit_rmro(as, XO_LEA, r, r, ofs); - else - emit_gri(as, XG_ARITHi(XOg_ADD), r, ofs); - } -} - -#define emit_spsub(as, ofs) emit_addptr(as, RID_ESP|REX_64, -(ofs)) - -/* Prefer rematerialization of BASE/L from global_State over spills. */ -#define emit_canremat(ref) ((ref) <= REF_BASE) - diff --git a/third-party/LuaJIT-2.0.2/src/lj_err.c b/third-party/LuaJIT-2.0.2/src/lj_err.c deleted file mode 100644 index 42cd12b45e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_err.c +++ /dev/null @@ -1,785 +0,0 @@ -/* -** Error handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_err_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_func.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_ff.h" -#include "lj_trace.h" -#include "lj_vm.h" - -/* -** LuaJIT can either use internal or external frame unwinding: -** -** - Internal frame unwinding (INT) is free-standing and doesn't require -** any OS or library support. -** -** - External frame unwinding (EXT) uses the system-provided unwind handler. -** -** Pros and Cons: -** -** - EXT requires unwind tables for *all* functions on the C stack between -** the pcall/catch and the error/throw. This is the default on x64, -** but needs to be manually enabled on x86/PPC for non-C++ code. -** -** - INT is faster when actually throwing errors (but this happens rarely). -** Setting up error handlers is zero-cost in any case. -** -** - EXT provides full interoperability with C++ exceptions. You can throw -** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames. -** C++ destructors are called as needed. C++ exceptions caught by pcall -** are converted to the string "C++ exception". Lua errors can be caught -** with catch (...) in C++. -** -** - INT has only limited support for automatically catching C++ exceptions -** on POSIX systems using DWARF2 stack unwinding. Other systems may use -** the wrapper function feature. Lua errors thrown through C++ frames -** cannot be caught by C++ code and C++ destructors are not run. -** -** EXT is the default on x64 systems, INT is the default on all other systems. -** -** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack -** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled -** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set -** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules -** and all C libraries that have callbacks which may be used to call back -** into Lua. C++ code must *not* be compiled with -fno-exceptions. -** -** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH. -** EXT is mandatory on WIN64 since the calling convention has an abundance -** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15). -** EXT is mandatory on POSIX/x64 since the interpreter doesn't save r12/r13. -*/ - -#if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) -#define LJ_UNWIND_EXT 1 -#elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS -#define LJ_UNWIND_EXT 1 -#endif - -/* -- Error messages ------------------------------------------------------ */ - -/* Error message strings. */ -LJ_DATADEF const char *lj_err_allmsg = -#define ERRDEF(name, msg) msg "\0" -#include "lj_errmsg.h" -; - -/* -- Internal frame unwinding -------------------------------------------- */ - -/* Unwind Lua stack and move error message to new top. */ -LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top) -{ - lj_func_closeuv(L, top); - if (top < L->top-1) { - copyTV(L, top, L->top-1); - L->top = top+1; - } - lj_state_relimitstack(L); -} - -/* Unwind until stop frame. Optionally cleanup frames. */ -static void *err_unwind(lua_State *L, void *stopcf, int errcode) -{ - TValue *frame = L->base-1; - void *cf = L->cframe; - while (cf) { - int32_t nres = cframe_nres(cframe_raw(cf)); - if (nres < 0) { /* C frame without Lua frame? */ - TValue *top = restorestack(L, -nres); - if (frame < top) { /* Frame reached? */ - if (errcode) { - L->cframe = cframe_prev(cf); - L->base = frame+1; - unwindstack(L, top); - } - return cf; - } - } - if (frame <= tvref(L->stack)) - break; - switch (frame_typep(frame)) { - case FRAME_LUA: /* Lua frame. */ - case FRAME_LUAP: - frame = frame_prevl(frame); - break; - case FRAME_C: /* C frame. */ -#if LJ_HASFFI - unwind_c: -#endif -#if LJ_UNWIND_EXT - if (errcode) { - L->cframe = cframe_prev(cf); - L->base = frame_prevd(frame) + 1; - unwindstack(L, frame); - } else if (cf != stopcf) { - cf = cframe_prev(cf); - frame = frame_prevd(frame); - break; - } - return NULL; /* Continue unwinding. */ -#else - UNUSED(stopcf); - cf = cframe_prev(cf); - frame = frame_prevd(frame); - break; -#endif - case FRAME_CP: /* Protected C frame. */ - if (cframe_canyield(cf)) { /* Resume? */ - if (errcode) { - hook_leave(G(L)); /* Assumes nobody uses coroutines inside hooks. */ - L->cframe = NULL; - L->status = (uint8_t)errcode; - } - return cf; - } - if (errcode) { - L->cframe = cframe_prev(cf); - L->base = frame_prevd(frame) + 1; - unwindstack(L, frame); - } - return cf; - case FRAME_CONT: /* Continuation frame. */ -#if LJ_HASFFI - if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) - goto unwind_c; -#endif - case FRAME_VARG: /* Vararg frame. */ - frame = frame_prevd(frame); - break; - case FRAME_PCALL: /* FF pcall() frame. */ - case FRAME_PCALLH: /* FF pcall() frame inside hook. */ - if (errcode) { - if (errcode == LUA_YIELD) { - frame = frame_prevd(frame); - break; - } - if (frame_typep(frame) == FRAME_PCALL) - hook_leave(G(L)); - L->cframe = cf; - L->base = frame_prevd(frame) + 1; - unwindstack(L, L->base); - } - return (void *)((intptr_t)cf | CFRAME_UNWIND_FF); - } - } - /* No C frame. */ - if (errcode) { - L->cframe = NULL; - L->base = tvref(L->stack)+1; - unwindstack(L, L->base); - if (G(L)->panic) - G(L)->panic(L); - exit(EXIT_FAILURE); - } - return L; /* Anything non-NULL will do. */ -} - -/* -- External frame unwinding -------------------------------------------- */ - -#if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_TARGET_WINDOWS - -/* -** We have to use our own definitions instead of the mandatory (!) unwind.h, -** since various OS, distros and compilers mess up the header installation. -*/ - -typedef struct _Unwind_Exception -{ - uint64_t exclass; - void (*excleanup)(int, struct _Unwind_Exception); - uintptr_t p1, p2; -} __attribute__((__aligned__)) _Unwind_Exception; - -typedef struct _Unwind_Context _Unwind_Context; - -#define _URC_OK 0 -#define _URC_FATAL_PHASE1_ERROR 3 -#define _URC_HANDLER_FOUND 6 -#define _URC_INSTALL_CONTEXT 7 -#define _URC_CONTINUE_UNWIND 8 -#define _URC_FAILURE 9 - -#if !LJ_TARGET_ARM - -extern uintptr_t _Unwind_GetCFA(_Unwind_Context *); -extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t); -extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t); -extern void _Unwind_DeleteException(_Unwind_Exception *); -extern int _Unwind_RaiseException(_Unwind_Exception *); - -#define _UA_SEARCH_PHASE 1 -#define _UA_CLEANUP_PHASE 2 -#define _UA_HANDLER_FRAME 4 -#define _UA_FORCE_UNWIND 8 - -#define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */ -#define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c)) -#define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff) -#define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff)) - -/* DWARF2 personality handler referenced from interpreter .eh_frame. */ -LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions, - uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx) -{ - void *cf; - lua_State *L; - if (version != 1) - return _URC_FATAL_PHASE1_ERROR; - UNUSED(uexclass); - cf = (void *)_Unwind_GetCFA(ctx); - L = cframe_L(cf); - if ((actions & _UA_SEARCH_PHASE)) { -#if LJ_UNWIND_EXT - if (err_unwind(L, cf, 0) == NULL) - return _URC_CONTINUE_UNWIND; -#endif - if (!LJ_UEXCLASS_CHECK(uexclass)) { - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); - } - return _URC_HANDLER_FOUND; - } - if ((actions & _UA_CLEANUP_PHASE)) { - int errcode; - if (LJ_UEXCLASS_CHECK(uexclass)) { - errcode = LJ_UEXCLASS_ERRCODE(uexclass); - } else { - if ((actions & _UA_HANDLER_FRAME)) - _Unwind_DeleteException(uex); - errcode = LUA_ERRRUN; - } -#if LJ_UNWIND_EXT - cf = err_unwind(L, cf, errcode); - if ((actions & _UA_FORCE_UNWIND)) { - return _URC_CONTINUE_UNWIND; - } else if (cf) { - _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode); - _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ? - lj_vm_unwind_ff_eh : - lj_vm_unwind_c_eh)); - return _URC_INSTALL_CONTEXT; - } -#if LJ_TARGET_X86ORX64 - else if ((actions & _UA_HANDLER_FRAME)) { - /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/ - ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h - */ - _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode); - _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow); - return _URC_INSTALL_CONTEXT; - } -#endif -#else - /* This is not the proper way to escape from the unwinder. We get away with - ** it on non-x64 because the interpreter restores all callee-saved regs. - */ - lj_err_throw(L, errcode); -#endif - } - return _URC_CONTINUE_UNWIND; -} - -#if LJ_UNWIND_EXT -#if LJ_TARGET_OSX || defined(__OpenBSD__) -/* Sorry, no thread safety for OSX. Complain to Apple, not me. */ -static _Unwind_Exception static_uex; -#else -static __thread _Unwind_Exception static_uex; -#endif - -/* Raise DWARF2 exception. */ -static void err_raise_ext(int errcode) -{ - static_uex.exclass = LJ_UEXCLASS_MAKE(errcode); - static_uex.excleanup = NULL; - _Unwind_RaiseException(&static_uex); -} -#endif - -#else - -extern void _Unwind_DeleteException(void *); -extern int __gnu_unwind_frame (void *, _Unwind_Context *); -extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *); -extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *); - -static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r) -{ - uint32_t v; - _Unwind_VRS_Get(ctx, 0, r, 0, &v); - return v; -} - -static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v) -{ - _Unwind_VRS_Set(ctx, 0, r, 0, &v); -} - -#define _US_VIRTUAL_UNWIND_FRAME 0 -#define _US_UNWIND_FRAME_STARTING 1 -#define _US_ACTION_MASK 3 -#define _US_FORCE_UNWIND 8 - -/* ARM unwinder personality handler referenced from interpreter .ARM.extab. */ -LJ_FUNCA int lj_err_unwind_arm(int state, void *ucb, _Unwind_Context *ctx) -{ - void *cf = (void *)_Unwind_GetGR(ctx, 13); - lua_State *L = cframe_L(cf); - if ((state & _US_ACTION_MASK) == _US_VIRTUAL_UNWIND_FRAME) { - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); - return _URC_HANDLER_FOUND; - } - if ((state&(_US_ACTION_MASK|_US_FORCE_UNWIND)) == _US_UNWIND_FRAME_STARTING) { - _Unwind_DeleteException(ucb); - _Unwind_SetGR(ctx, 15, (uint32_t)(void *)lj_err_throw); - _Unwind_SetGR(ctx, 0, (uint32_t)L); - _Unwind_SetGR(ctx, 1, (uint32_t)LUA_ERRRUN); - return _URC_INSTALL_CONTEXT; - } - if (__gnu_unwind_frame(ucb, ctx) != _URC_OK) - return _URC_FAILURE; - return _URC_CONTINUE_UNWIND; -} - -#endif - -#elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS - -/* -** Someone in Redmond owes me several days of my life. A lot of this is -** undocumented or just plain wrong on MSDN. Some of it can be gathered -** from 3rd party docs or must be found by trial-and-error. They really -** don't want you to write your own language-specific exception handler -** or to interact gracefully with MSVC. :-( -** -** Apparently MSVC doesn't call C++ destructors for foreign exceptions -** unless you compile your C++ code with /EHa. Unfortunately this means -** catch (...) also catches things like access violations. The use of -** _set_se_translator doesn't really help, because it requires /EHa, too. -*/ - -#define WIN32_LEAN_AND_MEAN -#include - -/* Taken from: http://www.nynaeve.net/?p=99 */ -typedef struct UndocumentedDispatcherContext { - ULONG64 ControlPc; - ULONG64 ImageBase; - PRUNTIME_FUNCTION FunctionEntry; - ULONG64 EstablisherFrame; - ULONG64 TargetIp; - PCONTEXT ContextRecord; - PEXCEPTION_ROUTINE LanguageHandler; - PVOID HandlerData; - PUNWIND_HISTORY_TABLE HistoryTable; - ULONG ScopeIndex; - ULONG Fill0; -} UndocumentedDispatcherContext; - -/* Another wild guess. */ -extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow); - -#ifdef MINGW_SDK_INIT -/* Workaround for broken MinGW64 declaration. */ -VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx"); -#define RtlUnwindEx RtlUnwindEx_FIXED -#endif - -#define LJ_MSVC_EXCODE ((DWORD)0xe06d7363) -#define LJ_GCC_EXCODE ((DWORD)0x20474343) - -#define LJ_EXCODE ((DWORD)0xe24c4a00) -#define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c)) -#define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff) -#define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff)) - -/* Win64 exception handler for interpreter frame. */ -LJ_FUNCA EXCEPTION_DISPOSITION lj_err_unwind_win64(EXCEPTION_RECORD *rec, - void *cf, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch) -{ - lua_State *L = cframe_L(cf); - int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ? - LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN; - if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */ - /* Unwind internal frames. */ - err_unwind(L, cf, errcode); - } else { - void *cf2 = err_unwind(L, cf, 0); - if (cf2) { /* We catch it, so start unwinding the upper frames. */ - if (rec->ExceptionCode == LJ_MSVC_EXCODE || - rec->ExceptionCode == LJ_GCC_EXCODE) { - __DestructExceptionObject(rec, 1); - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); - } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) { - /* Don't catch access violations etc. */ - return ExceptionContinueSearch; - } - /* Unwind the stack and call all handlers for all lower C frames - ** (including ourselves) again with EH_UNWINDING set. Then set - ** rsp = cf, rax = errcode and jump to the specified target. - */ - RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ? - lj_vm_unwind_ff_eh : - lj_vm_unwind_c_eh), - rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable); - /* RtlUnwindEx should never return. */ - } - } - return ExceptionContinueSearch; -} - -/* Raise Windows exception. */ -static void err_raise_ext(int errcode) -{ - RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL); -} - -#endif - -/* -- Error handling ------------------------------------------------------ */ - -/* Throw error. Find catch frame, unwind stack and continue. */ -LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode) -{ - global_State *g = G(L); - lj_trace_abort(g); - setgcrefnull(g->jit_L); - L->status = 0; -#if LJ_UNWIND_EXT - err_raise_ext(errcode); - /* - ** A return from this function signals a corrupt C stack that cannot be - ** unwound. We have no choice but to call the panic function and exit. - ** - ** Usually this is caused by a C function without unwind information. - ** This should never happen on x64, but may happen if you've manually - ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every* - ** non-C++ file with -funwind-tables. - */ - if (G(L)->panic) - G(L)->panic(L); -#else - { - void *cf = err_unwind(L, NULL, errcode); - if (cframe_unwind_ff(cf)) - lj_vm_unwind_ff(cframe_raw(cf)); - else - lj_vm_unwind_c(cframe_raw(cf), errcode); - } -#endif - exit(EXIT_FAILURE); -} - -/* Return string object for error message. */ -LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em) -{ - return lj_str_newz(L, err2msg(em)); -} - -/* Out-of-memory error. */ -LJ_NOINLINE void lj_err_mem(lua_State *L) -{ - if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */ - lj_vm_unwind_c(L->cframe, LUA_ERRMEM); - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM)); - lj_err_throw(L, LUA_ERRMEM); -} - -/* Find error function for runtime errors. Requires an extra stack traversal. */ -static ptrdiff_t finderrfunc(lua_State *L) -{ - cTValue *frame = L->base-1, *bot = tvref(L->stack); - void *cf = L->cframe; - while (frame > bot) { - lua_assert(cf != NULL); - while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */ - if (frame >= restorestack(L, -cframe_nres(cf))) - break; - if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */ - return cframe_errfunc(cf); - cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */ - if (cf == NULL) - return 0; - } - switch (frame_typep(frame)) { - case FRAME_LUA: - case FRAME_LUAP: - frame = frame_prevl(frame); - break; - case FRAME_C: - cf = cframe_prev(cf); - /* fallthrough */ - case FRAME_CONT: -#if LJ_HASFFI - if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) - cf = cframe_prev(cf); -#endif - case FRAME_VARG: - frame = frame_prevd(frame); - break; - case FRAME_CP: - if (cframe_canyield(cf)) return 0; - if (cframe_errfunc(cf) >= 0) - return cframe_errfunc(cf); - frame = frame_prevd(frame); - break; - case FRAME_PCALL: - case FRAME_PCALLH: - if (frame_ftsz(frame) >= (ptrdiff_t)(2*sizeof(TValue))) /* xpcall? */ - return savestack(L, frame-1); /* Point to xpcall's errorfunc. */ - return 0; - default: - lua_assert(0); - return 0; - } - } - return 0; -} - -/* Runtime error. */ -LJ_NOINLINE void lj_err_run(lua_State *L) -{ - ptrdiff_t ef = finderrfunc(L); - if (ef) { - TValue *errfunc = restorestack(L, ef); - TValue *top = L->top; - lj_trace_abort(G(L)); - if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) { - setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR)); - lj_err_throw(L, LUA_ERRERR); - } - L->status = LUA_ERRERR; - copyTV(L, top, top-1); - copyTV(L, top-1, errfunc); - L->top = top+1; - lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */ - } - lj_err_throw(L, LUA_ERRRUN); -} - -/* Formatted runtime error message. */ -LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...) -{ - const char *msg; - va_list argp; - va_start(argp, em); - if (curr_funcisL(L)) L->top = curr_topL(L); - msg = lj_str_pushvf(L, err2msg(em), argp); - va_end(argp); - lj_debug_addloc(L, msg, L->base-1, NULL); - lj_err_run(L); -} - -/* Non-vararg variant for better calling conventions. */ -LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em) -{ - err_msgv(L, em); -} - -/* Lexer error. */ -LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok, - BCLine line, ErrMsg em, va_list argp) -{ - char buff[LUA_IDSIZE]; - const char *msg; - lj_debug_shortname(buff, src); - msg = lj_str_pushvf(L, err2msg(em), argp); - msg = lj_str_pushf(L, "%s:%d: %s", buff, line, msg); - if (tok) - lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok); - lj_err_throw(L, LUA_ERRSYNTAX); -} - -/* Typecheck error for operands. */ -LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm) -{ - const char *tname = lj_typename(o); - const char *opname = err2msg(opm); - if (curr_funcisL(L)) { - GCproto *pt = curr_proto(L); - const BCIns *pc = cframe_Lpc(L) - 1; - const char *oname = NULL; - const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname); - if (kind) - err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname); - } - err_msgv(L, LJ_ERR_BADOPRV, opname, tname); -} - -/* Typecheck error for ordered comparisons. */ -LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2) -{ - const char *t1 = lj_typename(o1); - const char *t2 = lj_typename(o2); - err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2); - /* This assumes the two "boolean" entries are commoned by the C compiler. */ -} - -/* Typecheck error for __call. */ -LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o) -{ - /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object: - ** L->base still points to the caller. So add a dummy frame with L instead - ** of a function. See lua_getstack(). - */ - const BCIns *pc = cframe_Lpc(L); - if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) { - const char *tname = lj_typename(o); - setframe_pc(o, pc); - setframe_gc(o, obj2gco(L)); - L->top = L->base = o+1; - err_msgv(L, LJ_ERR_BADCALL, tname); - } - lj_err_optype(L, o, LJ_ERR_OPCALL); -} - -/* Error in context of caller. */ -LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg) -{ - TValue *frame = L->base-1; - TValue *pframe = NULL; - if (frame_islua(frame)) { - pframe = frame_prevl(frame); - } else if (frame_iscont(frame)) { -#if LJ_HASFFI - if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) { - pframe = frame; - frame = NULL; - } else -#endif - { - pframe = frame_prevd(frame); -#if LJ_HASFFI - /* Remove frame for FFI metamethods. */ - if (frame_func(frame)->c.ffid >= FF_ffi_meta___index && - frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) { - L->base = pframe+1; - L->top = frame; - setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame)); - } -#endif - } - } - lj_debug_addloc(L, msg, pframe, frame); - lj_err_run(L); -} - -/* Formatted error in context of caller. */ -LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...) -{ - const char *msg; - va_list argp; - va_start(argp, em); - msg = lj_str_pushvf(L, err2msg(em), argp); - va_end(argp); - lj_err_callermsg(L, msg); -} - -/* Error in context of caller. */ -LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em) -{ - lj_err_callermsg(L, err2msg(em)); -} - -/* Argument error message. */ -LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg, - const char *msg) -{ - const char *fname = "?"; - const char *ftype = lj_debug_funcname(L, L->base - 1, &fname); - if (narg < 0 && narg > LUA_REGISTRYINDEX) - narg = (int)(L->top - L->base) + narg + 1; - if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */ - msg = lj_str_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg); - else - msg = lj_str_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg); - lj_err_callermsg(L, msg); -} - -/* Formatted argument error. */ -LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...) -{ - const char *msg; - va_list argp; - va_start(argp, em); - msg = lj_str_pushvf(L, err2msg(em), argp); - va_end(argp); - err_argmsg(L, narg, msg); -} - -/* Argument error. */ -LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em) -{ - err_argmsg(L, narg, err2msg(em)); -} - -/* Typecheck error for arguments. */ -LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname) -{ - TValue *o = narg < 0 ? L->top + narg : L->base + narg-1; - const char *tname = o < L->top ? lj_typename(o) : lj_obj_typename[0]; - const char *msg = lj_str_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname); - err_argmsg(L, narg, msg); -} - -/* Typecheck error for arguments. */ -LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt) -{ - lj_err_argtype(L, narg, lj_obj_typename[tt+1]); -} - -/* -- Public error handling API ------------------------------------------- */ - -LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf) -{ - lua_CFunction old = G(L)->panic; - G(L)->panic = panicf; - return old; -} - -/* Forwarders for the public API (C calling convention and no LJ_NORET). */ -LUA_API int lua_error(lua_State *L) -{ - lj_err_run(L); - return 0; /* unreachable */ -} - -LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg) -{ - err_argmsg(L, narg, msg); - return 0; /* unreachable */ -} - -LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname) -{ - lj_err_argtype(L, narg, xname); - return 0; /* unreachable */ -} - -LUALIB_API void luaL_where(lua_State *L, int level) -{ - int size; - cTValue *frame = lj_debug_frame(L, level, &size); - lj_debug_addloc(L, "", frame, size ? frame+size : NULL); -} - -LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...) -{ - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = lj_str_pushvf(L, fmt, argp); - va_end(argp); - lj_err_callermsg(L, msg); - return 0; /* unreachable */ -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_err.h b/third-party/LuaJIT-2.0.2/src/lj_err.h deleted file mode 100644 index dbea409100..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_err.h +++ /dev/null @@ -1,41 +0,0 @@ -/* -** Error handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_ERR_H -#define _LJ_ERR_H - -#include - -#include "lj_obj.h" - -typedef enum { -#define ERRDEF(name, msg) \ - LJ_ERR_##name, LJ_ERR_##name##_ = LJ_ERR_##name + sizeof(msg)-1, -#include "lj_errmsg.h" - LJ_ERR__MAX -} ErrMsg; - -LJ_DATA const char *lj_err_allmsg; -#define err2msg(em) (lj_err_allmsg+(int)(em)) - -LJ_FUNC GCstr *lj_err_str(lua_State *L, ErrMsg em); -LJ_FUNCA_NORET void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode); -LJ_FUNC_NORET void lj_err_mem(lua_State *L); -LJ_FUNC_NORET void lj_err_run(lua_State *L); -LJ_FUNC_NORET void lj_err_msg(lua_State *L, ErrMsg em); -LJ_FUNC_NORET void lj_err_lex(lua_State *L, GCstr *src, const char *tok, - BCLine line, ErrMsg em, va_list argp); -LJ_FUNC_NORET void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm); -LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2); -LJ_FUNC_NORET void lj_err_optype_call(lua_State *L, TValue *o); -LJ_FUNC_NORET void lj_err_callermsg(lua_State *L, const char *msg); -LJ_FUNC_NORET void lj_err_callerv(lua_State *L, ErrMsg em, ...); -LJ_FUNC_NORET void lj_err_caller(lua_State *L, ErrMsg em); -LJ_FUNC_NORET void lj_err_arg(lua_State *L, int narg, ErrMsg em); -LJ_FUNC_NORET void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...); -LJ_FUNC_NORET void lj_err_argtype(lua_State *L, int narg, const char *xname); -LJ_FUNC_NORET void lj_err_argt(lua_State *L, int narg, int tt); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_errmsg.h b/third-party/LuaJIT-2.0.2/src/lj_errmsg.h deleted file mode 100644 index fd46acd496..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_errmsg.h +++ /dev/null @@ -1,192 +0,0 @@ -/* -** VM error messages. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* This file may be included multiple times with different ERRDEF macros. */ - -/* Basic error handling. */ -ERRDEF(ERRMEM, "not enough memory") -ERRDEF(ERRERR, "error in error handling") -ERRDEF(ERRCPP, "C++ exception") - -/* Allocations. */ -ERRDEF(STROV, "string length overflow") -ERRDEF(UDATAOV, "userdata length overflow") -ERRDEF(STKOV, "stack overflow") -ERRDEF(STKOVM, "stack overflow (%s)") -ERRDEF(TABOV, "table overflow") - -/* Table indexing. */ -ERRDEF(NANIDX, "table index is NaN") -ERRDEF(NILIDX, "table index is nil") -ERRDEF(NEXTIDX, "invalid key to " LUA_QL("next")) - -/* Metamethod resolving. */ -ERRDEF(BADCALL, "attempt to call a %s value") -ERRDEF(BADOPRT, "attempt to %s %s " LUA_QS " (a %s value)") -ERRDEF(BADOPRV, "attempt to %s a %s value") -ERRDEF(BADCMPT, "attempt to compare %s with %s") -ERRDEF(BADCMPV, "attempt to compare two %s values") -ERRDEF(GETLOOP, "loop in gettable") -ERRDEF(SETLOOP, "loop in settable") -ERRDEF(OPCALL, "call") -ERRDEF(OPINDEX, "index") -ERRDEF(OPARITH, "perform arithmetic on") -ERRDEF(OPCAT, "concatenate") -ERRDEF(OPLEN, "get length of") - -/* Type checks. */ -ERRDEF(BADSELF, "calling " LUA_QS " on bad self (%s)") -ERRDEF(BADARG, "bad argument #%d to " LUA_QS " (%s)") -ERRDEF(BADTYPE, "%s expected, got %s") -ERRDEF(BADVAL, "invalid value") -ERRDEF(NOVAL, "value expected") -ERRDEF(NOCORO, "coroutine expected") -ERRDEF(NOTABN, "nil or table expected") -ERRDEF(NOLFUNC, "Lua function expected") -ERRDEF(NOFUNCL, "function or level expected") -ERRDEF(NOSFT, "string/function/table expected") -ERRDEF(NOPROXY, "boolean or proxy expected") -ERRDEF(FORINIT, LUA_QL("for") " initial value must be a number") -ERRDEF(FORLIM, LUA_QL("for") " limit must be a number") -ERRDEF(FORSTEP, LUA_QL("for") " step must be a number") - -/* C API checks. */ -ERRDEF(NOENV, "no calling environment") -ERRDEF(CYIELD, "attempt to yield across C-call boundary") -ERRDEF(BADLU, "bad light userdata pointer") -ERRDEF(NOGCMM, "bad action while in __gc metamethod") -#if LJ_TARGET_WINDOWS -ERRDEF(BADFPU, "bad FPU precision (use D3DCREATE_FPU_PRESERVE with DirectX)") -#endif - -/* Standard library function errors. */ -ERRDEF(ASSERT, "assertion failed!") -ERRDEF(PROTMT, "cannot change a protected metatable") -ERRDEF(UNPACK, "too many results to unpack") -ERRDEF(RDRSTR, "reader function must return a string") -ERRDEF(PRTOSTR, LUA_QL("tostring") " must return a string to " LUA_QL("print")) -ERRDEF(IDXRNG, "index out of range") -ERRDEF(BASERNG, "base out of range") -ERRDEF(LVLRNG, "level out of range") -ERRDEF(INVLVL, "invalid level") -ERRDEF(INVOPT, "invalid option") -ERRDEF(INVOPTM, "invalid option " LUA_QS) -ERRDEF(INVFMT, "invalid format") -ERRDEF(SETFENV, LUA_QL("setfenv") " cannot change environment of given object") -ERRDEF(CORUN, "cannot resume running coroutine") -ERRDEF(CODEAD, "cannot resume dead coroutine") -ERRDEF(COSUSP, "cannot resume non-suspended coroutine") -ERRDEF(TABINS, "wrong number of arguments to " LUA_QL("insert")) -ERRDEF(TABCAT, "invalid value (%s) at index %d in table for " LUA_QL("concat")) -ERRDEF(TABSORT, "invalid order function for sorting") -ERRDEF(IOCLFL, "attempt to use a closed file") -ERRDEF(IOSTDCL, "standard file is closed") -ERRDEF(OSUNIQF, "unable to generate a unique filename") -ERRDEF(OSDATEF, "field " LUA_QS " missing in date table") -ERRDEF(STRDUMP, "unable to dump given function") -ERRDEF(STRSLC, "string slice too long") -ERRDEF(STRPATB, "missing " LUA_QL("[") " after " LUA_QL("%f") " in pattern") -ERRDEF(STRPATC, "invalid pattern capture") -ERRDEF(STRPATE, "malformed pattern (ends with " LUA_QL("%") ")") -ERRDEF(STRPATM, "malformed pattern (missing " LUA_QL("]") ")") -ERRDEF(STRPATU, "unbalanced pattern") -ERRDEF(STRPATX, "pattern too complex") -ERRDEF(STRCAPI, "invalid capture index") -ERRDEF(STRCAPN, "too many captures") -ERRDEF(STRCAPU, "unfinished capture") -ERRDEF(STRFMTO, "invalid option " LUA_QL("%%%c") " to " LUA_QL("format")) -ERRDEF(STRFMTR, "invalid format (repeated flags)") -ERRDEF(STRFMTW, "invalid format (width or precision too long)") -ERRDEF(STRGSRV, "invalid replacement value (a %s)") -ERRDEF(BADMODN, "name conflict for module " LUA_QS) -#if LJ_HASJIT -#if LJ_TARGET_X86ORX64 -ERRDEF(NOJIT, "JIT compiler disabled, CPU does not support SSE2") -#else -ERRDEF(NOJIT, "JIT compiler disabled") -#endif -#elif defined(LJ_ARCH_NOJIT) -ERRDEF(NOJIT, "no JIT compiler for this architecture (yet)") -#else -ERRDEF(NOJIT, "JIT compiler permanently disabled by build option") -#endif -ERRDEF(JITOPT, "unknown or malformed optimization flag " LUA_QS) - -/* Lexer/parser errors. */ -ERRDEF(XMODE, "attempt to load chunk with wrong mode") -ERRDEF(XNEAR, "%s near " LUA_QS) -ERRDEF(XELEM, "lexical element too long") -ERRDEF(XLINES, "chunk has too many lines") -ERRDEF(XLEVELS, "chunk has too many syntax levels") -ERRDEF(XNUMBER, "malformed number") -ERRDEF(XLSTR, "unfinished long string") -ERRDEF(XLCOM, "unfinished long comment") -ERRDEF(XSTR, "unfinished string") -ERRDEF(XESC, "invalid escape sequence") -ERRDEF(XLDELIM, "invalid long string delimiter") -ERRDEF(XTOKEN, LUA_QS " expected") -ERRDEF(XJUMP, "control structure too long") -ERRDEF(XSLOTS, "function or expression too complex") -ERRDEF(XLIMC, "chunk has more than %d local variables") -ERRDEF(XLIMM, "main function has more than %d %s") -ERRDEF(XLIMF, "function at line %d has more than %d %s") -ERRDEF(XMATCH, LUA_QS " expected (to close " LUA_QS " at line %d)") -ERRDEF(XFIXUP, "function too long for return fixup") -ERRDEF(XPARAM, " or " LUA_QL("...") " expected") -#if !LJ_52 -ERRDEF(XAMBIG, "ambiguous syntax (function call x new statement)") -#endif -ERRDEF(XFUNARG, "function arguments expected") -ERRDEF(XSYMBOL, "unexpected symbol") -ERRDEF(XDOTS, "cannot use " LUA_QL("...") " outside a vararg function") -ERRDEF(XSYNTAX, "syntax error") -ERRDEF(XFOR, LUA_QL("=") " or " LUA_QL("in") " expected") -ERRDEF(XBREAK, "no loop to break") -ERRDEF(XLUNDEF, "undefined label " LUA_QS) -ERRDEF(XLDUP, "duplicate label " LUA_QS) -ERRDEF(XGSCOPE, " jumps into the scope of local " LUA_QS) - -/* Bytecode reader errors. */ -ERRDEF(BCFMT, "cannot load incompatible bytecode") -ERRDEF(BCBAD, "cannot load malformed bytecode") - -#if LJ_HASFFI -/* FFI errors. */ -ERRDEF(FFI_INVTYPE, "invalid C type") -ERRDEF(FFI_INVSIZE, "size of C type is unknown or too large") -ERRDEF(FFI_BADSCL, "bad storage class") -ERRDEF(FFI_DECLSPEC, "declaration specifier expected") -ERRDEF(FFI_BADTAG, "undeclared or implicit tag " LUA_QS) -ERRDEF(FFI_REDEF, "attempt to redefine " LUA_QS) -ERRDEF(FFI_NUMPARAM, "wrong number of type parameters") -ERRDEF(FFI_INITOV, "too many initializers for " LUA_QS) -ERRDEF(FFI_BADCONV, "cannot convert " LUA_QS " to " LUA_QS) -ERRDEF(FFI_BADLEN, "attempt to get length of " LUA_QS) -ERRDEF(FFI_BADCONCAT, "attempt to concatenate " LUA_QS " and " LUA_QS) -ERRDEF(FFI_BADARITH, "attempt to perform arithmetic on " LUA_QS " and " LUA_QS) -ERRDEF(FFI_BADCOMP, "attempt to compare " LUA_QS " with " LUA_QS) -ERRDEF(FFI_BADCALL, LUA_QS " is not callable") -ERRDEF(FFI_NUMARG, "wrong number of arguments for function call") -ERRDEF(FFI_BADMEMBER, LUA_QS " has no member named " LUA_QS) -ERRDEF(FFI_BADIDX, LUA_QS " cannot be indexed") -ERRDEF(FFI_BADIDXW, LUA_QS " cannot be indexed with " LUA_QS) -ERRDEF(FFI_BADMM, LUA_QS " has no " LUA_QS " metamethod") -ERRDEF(FFI_WRCONST, "attempt to write to constant location") -ERRDEF(FFI_NODECL, "missing declaration for symbol " LUA_QS) -ERRDEF(FFI_BADCBACK, "bad callback") -#if LJ_OS_NOJIT -ERRDEF(FFI_CBACKOV, "no support for callbacks on this OS") -#else -ERRDEF(FFI_CBACKOV, "too many callbacks") -#endif -ERRDEF(FFI_NYIPACKBIT, "NYI: packed bit fields") -ERRDEF(FFI_NYICALL, "NYI: cannot call this C function (yet)") -#endif - -#undef ERRDEF - -/* Detecting unused error messages: - awk -F, '/^ERRDEF/ { gsub(/ERRDEF./, ""); printf "grep -q LJ_ERR_%s *.[ch] || echo %s\n", $1, $1}' lj_errmsg.h | sh -*/ diff --git a/third-party/LuaJIT-2.0.2/src/lj_ff.h b/third-party/LuaJIT-2.0.2/src/lj_ff.h deleted file mode 100644 index b4a430108c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ff.h +++ /dev/null @@ -1,18 +0,0 @@ -/* -** Fast function IDs. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FF_H -#define _LJ_FF_H - -/* Fast function ID. */ -typedef enum { - FF_LUA_ = FF_LUA, /* Lua function (must be 0). */ - FF_C_ = FF_C, /* Regular C function (must be 1). */ -#define FFDEF(name) FF_##name, -#include "lj_ffdef.h" - FF__MAX -} FastFunc; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ffrecord.c b/third-party/LuaJIT-2.0.2/src/lj_ffrecord.c deleted file mode 100644 index 35e2e88e31..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ffrecord.c +++ /dev/null @@ -1,889 +0,0 @@ -/* -** Fast function call recorder. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_ffrecord_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_ff.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_record.h" -#include "lj_ffrecord.h" -#include "lj_crecord.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_strscan.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* -- Fast function recording handlers ------------------------------------ */ - -/* Conventions for fast function call handlers: -** -** The argument slots start at J->base[0]. All of them are guaranteed to be -** valid and type-specialized references. J->base[J->maxslot] is set to 0 -** as a sentinel. The runtime argument values start at rd->argv[0]. -** -** In general fast functions should check for presence of all of their -** arguments and for the correct argument types. Some simplifications -** are allowed if the interpreter throws instead. But even if recording -** is aborted, the generated IR must be consistent (no zero-refs). -** -** The number of results in rd->nres is set to 1. Handlers that return -** a different number of results need to override it. A negative value -** prevents return processing (e.g. for pending calls). -** -** Results need to be stored starting at J->base[0]. Return processing -** moves them to the right slots later. -** -** The per-ffid auxiliary data is the value of the 2nd part of the -** LJLIB_REC() annotation. This allows handling similar functionality -** in a common handler. -*/ - -/* Type of handler to record a fast function. */ -typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd); - -/* Get runtime value of int argument. */ -static int32_t argv2int(jit_State *J, TValue *o) -{ - if (!lj_strscan_numberobj(o)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - return tvisint(o) ? intV(o) : lj_num2int(numV(o)); -} - -/* Get runtime value of string argument. */ -static GCstr *argv2str(jit_State *J, TValue *o) -{ - if (LJ_LIKELY(tvisstr(o))) { - return strV(o); - } else { - GCstr *s; - if (!tvisnumber(o)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - if (tvisint(o)) - s = lj_str_fromint(J->L, intV(o)); - else - s = lj_str_fromnum(J->L, &o->n); - setstrV(J->L, o, s); - return s; - } -} - -/* Return number of results wanted by caller. */ -static ptrdiff_t results_wanted(jit_State *J) -{ - TValue *frame = J->L->base-1; - if (frame_islua(frame)) - return (ptrdiff_t)bc_b(frame_pc(frame)[-1]) - 1; - else - return -1; -} - -/* Throw error for unsupported variant of fast function. */ -LJ_NORET static void recff_nyiu(jit_State *J) -{ - setfuncV(J->L, &J->errinfo, J->fn); - lj_trace_err_info(J, LJ_TRERR_NYIFFU); -} - -/* Fallback handler for all fast functions that are not recorded (yet). */ -static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd) -{ - setfuncV(J->L, &J->errinfo, J->fn); - lj_trace_err_info(J, LJ_TRERR_NYIFF); - UNUSED(rd); -} - -/* C functions can have arbitrary side-effects and are not recorded (yet). */ -static void LJ_FASTCALL recff_c(jit_State *J, RecordFFData *rd) -{ - setfuncV(J->L, &J->errinfo, J->fn); - lj_trace_err_info(J, LJ_TRERR_NYICF); - UNUSED(rd); -} - -/* -- Base library fast functions ----------------------------------------- */ - -static void LJ_FASTCALL recff_assert(jit_State *J, RecordFFData *rd) -{ - /* Arguments already specialized. The interpreter throws for nil/false. */ - rd->nres = J->maxslot; /* Pass through all arguments. */ -} - -static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd) -{ - /* Arguments already specialized. Result is a constant string. Neat, huh? */ - uint32_t t; - if (tvisnumber(&rd->argv[0])) - t = ~LJ_TNUMX; - else if (LJ_64 && tvislightud(&rd->argv[0])) - t = ~LJ_TLIGHTUD; - else - t = ~itype(&rd->argv[0]); - J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[t])); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_getmetatable(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tr) { - RecordIndex ix; - ix.tab = tr; - copyTV(J->L, &ix.tabv, &rd->argv[0]); - if (lj_record_mm_lookup(J, &ix, MM_metatable)) - J->base[0] = ix.mobj; - else - J->base[0] = ix.mt; - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - TRef mt = J->base[1]; - if (tref_istab(tr) && (tref_istab(mt) || (mt && tref_isnil(mt)))) { - TRef fref, mtref; - RecordIndex ix; - ix.tab = tr; - copyTV(J->L, &ix.tabv, &rd->argv[0]); - lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */ - fref = emitir(IRT(IR_FREF, IRT_P32), tr, IRFL_TAB_META); - mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt; - emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref); - if (!tref_isnil(mt)) - emitir(IRT(IR_TBAR, IRT_TAB), tr, 0); - J->base[0] = tr; - J->needsnap = 1; - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_rawget(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; ix.key = J->base[1]; - if (tref_istab(ix.tab) && ix.key) { - ix.val = 0; ix.idxchain = 0; - settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); - copyTV(J->L, &ix.keyv, &rd->argv[1]); - J->base[0] = lj_record_idx(J, &ix); - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_rawset(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; ix.key = J->base[1]; ix.val = J->base[2]; - if (tref_istab(ix.tab) && ix.key && ix.val) { - ix.idxchain = 0; - settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); - copyTV(J->L, &ix.keyv, &rd->argv[1]); - copyTV(J->L, &ix.valv, &rd->argv[2]); - lj_record_idx(J, &ix); - /* Pass through table at J->base[0] as result. */ - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd) -{ - TRef tra = J->base[0]; - TRef trb = J->base[1]; - if (tra && trb) { - int diff = lj_record_objcmp(J, tra, trb, &rd->argv[0], &rd->argv[1]); - J->base[0] = diff ? TREF_FALSE : TREF_TRUE; - } /* else: Interpreter will throw. */ -} - -#if LJ_52 -static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tref_isstr(tr)) - J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN); - else if (tref_istab(tr)) - J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, tr); - /* else: Interpreter will throw. */ - UNUSED(rd); -} -#endif - -/* Determine mode of select() call. */ -int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv) -{ - if (tref_isstr(tr) && *strVdata(tv) == '#') { /* select('#', ...) */ - if (strV(tv)->len == 1) { - emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv))); - } else { - TRef trptr = emitir(IRT(IR_STRREF, IRT_P32), tr, lj_ir_kint(J, 0)); - TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY); - emitir(IRTG(IR_EQ, IRT_INT), trchar, lj_ir_kint(J, '#')); - } - return 0; - } else { /* select(n, ...) */ - int32_t start = argv2int(J, tv); - if (start == 0) lj_trace_err(J, LJ_TRERR_BADTYPE); /* A bit misleading. */ - return start; - } -} - -static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tr) { - ptrdiff_t start = lj_ffrecord_select_mode(J, tr, &rd->argv[0]); - if (start == 0) { /* select('#', ...) */ - J->base[0] = lj_ir_kint(J, J->maxslot - 1); - } else if (tref_isk(tr)) { /* select(k, ...) */ - ptrdiff_t n = (ptrdiff_t)J->maxslot; - if (start < 0) start += n; - else if (start > n) start = n; - rd->nres = n - start; - if (start >= 1) { - ptrdiff_t i; - for (i = 0; i < n - start; i++) - J->base[i] = J->base[start+i]; - } /* else: Interpreter will throw. */ - } else { - recff_nyiu(J); - } - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - TRef base = J->base[1]; - if (tr && !tref_isnil(base)) { - base = lj_opt_narrow_toint(J, base); - if (!tref_isk(base) || IR(tref_ref(base))->i != 10) - recff_nyiu(J); - } - if (tref_isnumber_str(tr)) { - if (tref_isstr(tr)) { - TValue tmp; - if (!lj_strscan_num(strV(&rd->argv[0]), &tmp)) - recff_nyiu(J); /* Would need an inverted STRTO for this case. */ - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - } -#if LJ_HASFFI - } else if (tref_iscdata(tr)) { - lj_crecord_tonumber(J, rd); - return; -#endif - } else { - tr = TREF_NIL; - } - J->base[0] = tr; - UNUSED(rd); -} - -static TValue *recff_metacall_cp(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - lj_record_tailcall(J, 0, 1); - UNUSED(L); UNUSED(dummy); - return NULL; -} - -static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm) -{ - RecordIndex ix; - ix.tab = J->base[0]; - copyTV(J->L, &ix.tabv, &rd->argv[0]); - if (lj_record_mm_lookup(J, &ix, mm)) { /* Has metamethod? */ - int errcode; - TValue argv0; - /* Temporarily insert metamethod below object. */ - J->base[1] = J->base[0]; - J->base[0] = ix.mobj; - copyTV(J->L, &argv0, &rd->argv[0]); - copyTV(J->L, &rd->argv[1], &rd->argv[0]); - copyTV(J->L, &rd->argv[0], &ix.mobjv); - /* Need to protect lj_record_tailcall because it may throw. */ - errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp); - /* Always undo Lua stack changes to avoid confusing the interpreter. */ - copyTV(J->L, &rd->argv[0], &argv0); - if (errcode) - lj_err_throw(J->L, errcode); /* Propagate errors. */ - rd->nres = -1; /* Pending call. */ - return 1; /* Tailcalled to metamethod. */ - } - return 0; -} - -static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tref_isstr(tr)) { - /* Ignore __tostring in the string base metatable. */ - /* Pass on result in J->base[0]. */ - } else if (!recff_metacall(J, rd, MM_tostring)) { - if (tref_isnumber(tr)) { - J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0); - } else if (tref_ispri(tr)) { - J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[tref_type(tr)])); - } else { - recff_nyiu(J); - } - } -} - -static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; - if (tref_istab(ix.tab)) { - if (!tvisnumber(&rd->argv[1])) /* No support for string coercion. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); - setintV(&ix.keyv, numberVint(&rd->argv[1])+1); - settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); - ix.val = 0; ix.idxchain = 0; - ix.key = lj_opt_narrow_toint(J, J->base[1]); - J->base[0] = ix.key = emitir(IRTI(IR_ADD), ix.key, lj_ir_kint(J, 1)); - J->base[1] = lj_record_idx(J, &ix); - rd->nres = tref_isnil(J->base[1]) ? 0 : 2; - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_ipairs(jit_State *J, RecordFFData *rd) -{ - if (!(LJ_52 && recff_metacall(J, rd, MM_ipairs))) { - TRef tab = J->base[0]; - if (tref_istab(tab)) { - J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0])); - J->base[1] = tab; - J->base[2] = lj_ir_kint(J, 0); - rd->nres = 3; - } /* else: Interpreter will throw. */ - } -} - -static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd) -{ - if (J->maxslot >= 1) { - lj_record_call(J, 0, J->maxslot - 1); - rd->nres = -1; /* Pending call. */ - } /* else: Interpreter will throw. */ -} - -static TValue *recff_xpcall_cp(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - lj_record_call(J, 1, J->maxslot - 2); - UNUSED(L); UNUSED(dummy); - return NULL; -} - -static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd) -{ - if (J->maxslot >= 2) { - TValue argv0, argv1; - TRef tmp; - int errcode; - /* Swap function and traceback. */ - tmp = J->base[0]; J->base[0] = J->base[1]; J->base[1] = tmp; - copyTV(J->L, &argv0, &rd->argv[0]); - copyTV(J->L, &argv1, &rd->argv[1]); - copyTV(J->L, &rd->argv[0], &argv1); - copyTV(J->L, &rd->argv[1], &argv0); - /* Need to protect lj_record_call because it may throw. */ - errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp); - /* Always undo Lua stack swap to avoid confusing the interpreter. */ - copyTV(J->L, &rd->argv[0], &argv0); - copyTV(J->L, &rd->argv[1], &argv1); - if (errcode) - lj_err_throw(J->L, errcode); /* Propagate errors. */ - rd->nres = -1; /* Pending call. */ - } /* else: Interpreter will throw. */ -} - -/* -- Math library fast functions ----------------------------------------- */ - -static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_knum_abs(J)); - UNUSED(rd); -} - -/* Record rounding functions math.floor and math.ceil. */ -static void LJ_FASTCALL recff_math_round(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (!tref_isinteger(tr)) { /* Pass through integers unmodified. */ - tr = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, tr), rd->data); - /* Result is integral (or NaN/Inf), but may not fit an int32_t. */ - if (LJ_DUALNUM) { /* Try to narrow using a guarded conversion to int. */ - lua_Number n = lj_vm_foldfpm(numberVnum(&rd->argv[0]), rd->data); - if (n == (lua_Number)lj_num2int(n)) - tr = emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_CHECK); - } - J->base[0] = tr; - } -} - -/* Record unary math.* functions, mapped to IR_FPMATH opcode. */ -static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd) -{ - J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data); -} - -/* Record math.log. */ -static void LJ_FASTCALL recff_math_log(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - if (J->base[1]) { -#ifdef LUAJIT_NO_LOG2 - uint32_t fpm = IRFPM_LOG; -#else - uint32_t fpm = IRFPM_LOG2; -#endif - TRef trb = lj_ir_tonum(J, J->base[1]); - tr = emitir(IRTN(IR_FPMATH), tr, fpm); - trb = emitir(IRTN(IR_FPMATH), trb, fpm); - trb = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), trb); - tr = emitir(IRTN(IR_MUL), tr, trb); - } else { - tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_LOG); - } - J->base[0] = tr; - UNUSED(rd); -} - -/* Record math.atan2. */ -static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - TRef tr2 = lj_ir_tonum(J, J->base[1]); - J->base[0] = emitir(IRTN(IR_ATAN2), tr, tr2); - UNUSED(rd); -} - -/* Record math.ldexp. */ -static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); -#if LJ_TARGET_X86ORX64 - TRef tr2 = lj_ir_tonum(J, J->base[1]); -#else - TRef tr2 = lj_opt_narrow_toint(J, J->base[1]); -#endif - J->base[0] = emitir(IRTN(IR_LDEXP), tr, tr2); - UNUSED(rd); -} - -/* Record math.asin, math.acos, math.atan. */ -static void LJ_FASTCALL recff_math_atrig(jit_State *J, RecordFFData *rd) -{ - TRef y = lj_ir_tonum(J, J->base[0]); - TRef x = lj_ir_knum_one(J); - uint32_t ffid = rd->data; - if (ffid != FF_math_atan) { - TRef tmp = emitir(IRTN(IR_MUL), y, y); - tmp = emitir(IRTN(IR_SUB), x, tmp); - tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_SQRT); - if (ffid == FF_math_asin) { x = tmp; } else { x = y; y = tmp; } - } - J->base[0] = emitir(IRTN(IR_ATAN2), y, x); -} - -static void LJ_FASTCALL recff_math_htrig(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data); -} - -static void LJ_FASTCALL recff_math_modf(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tref_isinteger(tr)) { - J->base[0] = tr; - J->base[1] = lj_ir_kint(J, 0); - } else { - TRef trt; - tr = lj_ir_tonum(J, tr); - trt = emitir(IRTN(IR_FPMATH), tr, IRFPM_TRUNC); - J->base[0] = trt; - J->base[1] = emitir(IRTN(IR_SUB), tr, trt); - } - rd->nres = 2; -} - -static void LJ_FASTCALL recff_math_degrad(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - TRef trm = lj_ir_knum(J, numV(&J->fn->c.upvalue[0])); - J->base[0] = emitir(IRTN(IR_MUL), tr, trm); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - if (!tref_isnumber_str(J->base[1])) - lj_trace_err(J, LJ_TRERR_BADTYPE); - J->base[0] = lj_opt_narrow_pow(J, tr, J->base[1], &rd->argv[1]); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_math_minmax(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonumber(J, J->base[0]); - uint32_t op = rd->data; - BCReg i; - for (i = 1; J->base[i] != 0; i++) { - TRef tr2 = lj_ir_tonumber(J, J->base[i]); - IRType t = IRT_INT; - if (!(tref_isinteger(tr) && tref_isinteger(tr2))) { - if (tref_isinteger(tr)) tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); - if (tref_isinteger(tr2)) tr2 = emitir(IRTN(IR_CONV), tr2, IRCONV_NUM_INT); - t = IRT_NUM; - } - tr = emitir(IRT(op, t), tr, tr2); - } - J->base[0] = tr; -} - -static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd) -{ - GCudata *ud = udataV(&J->fn->c.upvalue[0]); - TRef tr, one; - lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */ - tr = lj_ir_call(J, IRCALL_lj_math_random_step, lj_ir_kptr(J, uddata(ud))); - one = lj_ir_knum_one(J); - tr = emitir(IRTN(IR_SUB), tr, one); - if (J->base[0]) { - TRef tr1 = lj_ir_tonum(J, J->base[0]); - if (J->base[1]) { /* d = floor(d*(r2-r1+1.0)) + r1 */ - TRef tr2 = lj_ir_tonum(J, J->base[1]); - tr2 = emitir(IRTN(IR_SUB), tr2, tr1); - tr2 = emitir(IRTN(IR_ADD), tr2, one); - tr = emitir(IRTN(IR_MUL), tr, tr2); - tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); - tr = emitir(IRTN(IR_ADD), tr, tr1); - } else { /* d = floor(d*r1) + 1.0 */ - tr = emitir(IRTN(IR_MUL), tr, tr1); - tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); - tr = emitir(IRTN(IR_ADD), tr, one); - } - } - J->base[0] = tr; - UNUSED(rd); -} - -/* -- Bit library fast functions ------------------------------------------ */ - -/* Record unary bit.tobit, bit.bnot, bit.bswap. */ -static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_opt_narrow_tobit(J, J->base[0]); - J->base[0] = (rd->data == IR_TOBIT) ? tr : emitir(IRTI(rd->data), tr, 0); -} - -/* Record N-ary bit.band, bit.bor, bit.bxor. */ -static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_opt_narrow_tobit(J, J->base[0]); - uint32_t op = rd->data; - BCReg i; - for (i = 1; J->base[i] != 0; i++) - tr = emitir(IRTI(op), tr, lj_opt_narrow_tobit(J, J->base[i])); - J->base[0] = tr; -} - -/* Record bit shifts. */ -static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_opt_narrow_tobit(J, J->base[0]); - TRef tsh = lj_opt_narrow_tobit(J, J->base[1]); - IROp op = (IROp)rd->data; - if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && - !tref_isk(tsh)) - tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31)); -#ifdef LJ_TARGET_UNIFYROT - if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) { - op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR; - tsh = emitir(IRTI(IR_NEG), tsh, tsh); - } -#endif - J->base[0] = emitir(IRTI(op), tr, tsh); -} - -/* -- String library fast functions --------------------------------------- */ - -static void LJ_FASTCALL recff_string_len(jit_State *J, RecordFFData *rd) -{ - J->base[0] = emitir(IRTI(IR_FLOAD), lj_ir_tostr(J, J->base[0]), IRFL_STR_LEN); - UNUSED(rd); -} - -/* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */ -static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) -{ - TRef trstr = lj_ir_tostr(J, J->base[0]); - TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN); - TRef tr0 = lj_ir_kint(J, 0); - TRef trstart, trend; - GCstr *str = argv2str(J, &rd->argv[0]); - int32_t start, end; - if (rd->data) { /* string.sub(str, start [,end]) */ - start = argv2int(J, &rd->argv[1]); - trstart = lj_opt_narrow_toint(J, J->base[1]); - trend = J->base[2]; - if (tref_isnil(trend)) { - trend = lj_ir_kint(J, -1); - end = -1; - } else { - trend = lj_opt_narrow_toint(J, trend); - end = argv2int(J, &rd->argv[2]); - } - } else { /* string.byte(str, [,start [,end]]) */ - if (!tref_isnil(J->base[1])) { - start = argv2int(J, &rd->argv[1]); - trstart = lj_opt_narrow_toint(J, J->base[1]); - trend = J->base[2]; - if (tref_isnil(trend)) { - trend = trstart; - end = start; - } else { - trend = lj_opt_narrow_toint(J, trend); - end = argv2int(J, &rd->argv[2]); - } - } else { - trend = trstart = lj_ir_kint(J, 1); - end = start = 1; - } - } - if (end < 0) { - emitir(IRTGI(IR_LT), trend, tr0); - trend = emitir(IRTI(IR_ADD), emitir(IRTI(IR_ADD), trlen, trend), - lj_ir_kint(J, 1)); - end = end+(int32_t)str->len+1; - } else if ((MSize)end <= str->len) { - emitir(IRTGI(IR_ULE), trend, trlen); - } else { - emitir(IRTGI(IR_GT), trend, trlen); - end = (int32_t)str->len; - trend = trlen; - } - if (start < 0) { - emitir(IRTGI(IR_LT), trstart, tr0); - trstart = emitir(IRTI(IR_ADD), trlen, trstart); - start = start+(int32_t)str->len; - emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), trstart, tr0); - if (start < 0) { - trstart = tr0; - start = 0; - } - } else { - if (start == 0) { - emitir(IRTGI(IR_EQ), trstart, tr0); - trstart = tr0; - } else { - trstart = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, -1)); - emitir(IRTGI(IR_GE), trstart, tr0); - start--; - } - } - if (rd->data) { /* Return string.sub result. */ - if (end - start >= 0) { - /* Also handle empty range here, to avoid extra traces. */ - TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart); - emitir(IRTGI(IR_GE), trslen, tr0); - trptr = emitir(IRT(IR_STRREF, IRT_P32), trstr, trstart); - J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen); - } else { /* Range underflow: return empty string. */ - emitir(IRTGI(IR_LT), trend, trstart); - J->base[0] = lj_ir_kstr(J, lj_str_new(J->L, strdata(str), 0)); - } - } else { /* Return string.byte result(s). */ - ptrdiff_t i, len = end - start; - if (len > 0) { - TRef trslen = emitir(IRTI(IR_SUB), trend, trstart); - emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len)); - if (J->baseslot + len > LJ_MAX_JSLOTS) - lj_trace_err_info(J, LJ_TRERR_STACKOV); - rd->nres = len; - for (i = 0; i < len; i++) { - TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i)); - tmp = emitir(IRT(IR_STRREF, IRT_P32), trstr, tmp); - J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY); - } - } else { /* Empty range or range underflow: return no results. */ - emitir(IRTGI(IR_LE), trend, trstart); - rd->nres = 0; - } - } -} - -/* -- Table library fast functions ---------------------------------------- */ - -static void LJ_FASTCALL recff_table_getn(jit_State *J, RecordFFData *rd) -{ - if (tref_istab(J->base[0])) - J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, J->base[0]); - /* else: Interpreter will throw. */ - UNUSED(rd); -} - -static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd) -{ - TRef tab = J->base[0]; - rd->nres = 0; - if (tref_istab(tab)) { - if (tref_isnil(J->base[1])) { /* Simple pop: t[#t] = nil */ - TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, tab); - GCtab *t = tabV(&rd->argv[0]); - MSize len = lj_tab_len(t); - emitir(IRTGI(len ? IR_NE : IR_EQ), trlen, lj_ir_kint(J, 0)); - if (len) { - RecordIndex ix; - ix.tab = tab; - ix.key = trlen; - settabV(J->L, &ix.tabv, t); - setintV(&ix.keyv, len); - ix.idxchain = 0; - if (results_wanted(J) != 0) { /* Specialize load only if needed. */ - ix.val = 0; - J->base[0] = lj_record_idx(J, &ix); /* Load previous value. */ - rd->nres = 1; - /* Assumes ix.key/ix.tab is not modified for raw lj_record_idx(). */ - } - ix.val = TREF_NIL; - lj_record_idx(J, &ix); /* Remove value. */ - } - } else { /* Complex case: remove in the middle. */ - recff_nyiu(J); - } - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; - ix.val = J->base[1]; - rd->nres = 0; - if (tref_istab(ix.tab) && ix.val) { - if (!J->base[2]) { /* Simple push: t[#t+1] = v */ - TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, ix.tab); - GCtab *t = tabV(&rd->argv[0]); - ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); - settabV(J->L, &ix.tabv, t); - setintV(&ix.keyv, lj_tab_len(t) + 1); - ix.idxchain = 0; - lj_record_idx(J, &ix); /* Set new value. */ - } else { /* Complex case: insert in the middle. */ - recff_nyiu(J); - } - } /* else: Interpreter will throw. */ -} - -/* -- I/O library fast functions ------------------------------------------ */ - -/* Get FILE* for I/O function. Any I/O error aborts recording, so there's -** no need to encode the alternate cases for any of the guards. -*/ -static TRef recff_io_fp(jit_State *J, TRef *udp, int32_t id) -{ - TRef tr, ud, fp; - if (id) { /* io.func() */ - tr = lj_ir_kptr(J, &J2G(J)->gcroot[id]); - ud = emitir(IRT(IR_XLOAD, IRT_UDATA), tr, 0); - } else { /* fp:method() */ - ud = J->base[0]; - if (!tref_isudata(ud)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - tr = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE)); - } - *udp = ud; - fp = emitir(IRT(IR_FLOAD, IRT_PTR), ud, IRFL_UDATA_FILE); - emitir(IRTG(IR_NE, IRT_PTR), fp, lj_ir_knull(J, IRT_PTR)); - return fp; -} - -static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd) -{ - TRef ud, fp = recff_io_fp(J, &ud, rd->data); - TRef zero = lj_ir_kint(J, 0); - TRef one = lj_ir_kint(J, 1); - ptrdiff_t i = rd->data == 0 ? 1 : 0; - for (; J->base[i]; i++) { - TRef str = lj_ir_tostr(J, J->base[i]); - TRef buf = emitir(IRT(IR_STRREF, IRT_P32), str, zero); - TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN); - if (tref_isk(len) && IR(tref_ref(len))->i == 1) { - TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY); - tr = lj_ir_call(J, IRCALL_fputc, tr, fp); - if (results_wanted(J) != 0) /* Check result only if not ignored. */ - emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1)); - } else { - TRef tr = lj_ir_call(J, IRCALL_fwrite, buf, one, len, fp); - if (results_wanted(J) != 0) /* Check result only if not ignored. */ - emitir(IRTGI(IR_EQ), tr, len); - } - } - J->base[0] = LJ_52 ? ud : TREF_TRUE; -} - -static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd) -{ - TRef ud, fp = recff_io_fp(J, &ud, rd->data); - TRef tr = lj_ir_call(J, IRCALL_fflush, fp); - if (results_wanted(J) != 0) /* Check result only if not ignored. */ - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0)); - J->base[0] = TREF_TRUE; -} - -/* -- Record calls to fast functions -------------------------------------- */ - -#include "lj_recdef.h" - -static uint32_t recdef_lookup(GCfunc *fn) -{ - if (fn->c.ffid < sizeof(recff_idmap)/sizeof(recff_idmap[0])) - return recff_idmap[fn->c.ffid]; - else - return 0; -} - -/* Record entry to a fast function or C function. */ -void lj_ffrecord_func(jit_State *J) -{ - RecordFFData rd; - uint32_t m = recdef_lookup(J->fn); - rd.data = m & 0xff; - rd.nres = 1; /* Default is one result. */ - rd.argv = J->L->base; - J->base[J->maxslot] = 0; /* Mark end of arguments. */ - (recff_func[m >> 8])(J, &rd); /* Call recff_* handler. */ - if (rd.nres >= 0) { - if (J->postproc == LJ_POST_NONE) J->postproc = LJ_POST_FFRETRY; - lj_record_ret(J, 0, rd.nres); - } -} - -#undef IR -#undef emitir - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ffrecord.h b/third-party/LuaJIT-2.0.2/src/lj_ffrecord.h deleted file mode 100644 index 9a30f359ed..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ffrecord.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -** Fast function call recorder. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FFRECORD_H -#define _LJ_FFRECORD_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -/* Data used by handlers to record a fast function. */ -typedef struct RecordFFData { - TValue *argv; /* Runtime argument values. */ - ptrdiff_t nres; /* Number of returned results (defaults to 1). */ - uint32_t data; /* Per-ffid auxiliary data (opcode, literal etc.). */ -} RecordFFData; - -LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv); -LJ_FUNC void lj_ffrecord_func(jit_State *J); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_frame.h b/third-party/LuaJIT-2.0.2/src/lj_frame.h deleted file mode 100644 index 60ce1a9f0d..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_frame.h +++ /dev/null @@ -1,183 +0,0 @@ -/* -** Stack frames. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FRAME_H -#define _LJ_FRAME_H - -#include "lj_obj.h" -#include "lj_bc.h" - -/* -- Lua stack frame ----------------------------------------------------- */ - -/* Frame type markers in callee function slot (callee base-1). */ -enum { - FRAME_LUA, FRAME_C, FRAME_CONT, FRAME_VARG, - FRAME_LUAP, FRAME_CP, FRAME_PCALL, FRAME_PCALLH -}; -#define FRAME_TYPE 3 -#define FRAME_P 4 -#define FRAME_TYPEP (FRAME_TYPE|FRAME_P) - -/* Macros to access and modify Lua frames. */ -#define frame_gc(f) (gcref((f)->fr.func)) -#define frame_func(f) (&frame_gc(f)->fn) -#define frame_ftsz(f) ((f)->fr.tp.ftsz) - -#define frame_type(f) (frame_ftsz(f) & FRAME_TYPE) -#define frame_typep(f) (frame_ftsz(f) & FRAME_TYPEP) -#define frame_islua(f) (frame_type(f) == FRAME_LUA) -#define frame_isc(f) (frame_type(f) == FRAME_C) -#define frame_iscont(f) (frame_typep(f) == FRAME_CONT) -#define frame_isvarg(f) (frame_typep(f) == FRAME_VARG) -#define frame_ispcall(f) ((frame_ftsz(f) & 6) == FRAME_PCALL) - -#define frame_pc(f) (mref((f)->fr.tp.pcr, const BCIns)) -#define frame_contpc(f) (frame_pc((f)-1)) -#if LJ_64 -#define frame_contf(f) \ - ((ASMFunction)(void *)((intptr_t)lj_vm_asm_begin + \ - (intptr_t)(int32_t)((f)-1)->u32.lo)) -#else -#define frame_contf(f) ((ASMFunction)gcrefp(((f)-1)->gcr, void)) -#endif -#define frame_delta(f) (frame_ftsz(f) >> 3) -#define frame_sized(f) (frame_ftsz(f) & ~FRAME_TYPEP) - -#define frame_prevl(f) ((f) - (1+bc_a(frame_pc(f)[-1]))) -#define frame_prevd(f) ((TValue *)((char *)(f) - frame_sized(f))) -#define frame_prev(f) (frame_islua(f)?frame_prevl(f):frame_prevd(f)) -/* Note: this macro does not skip over FRAME_VARG. */ - -#define setframe_pc(f, pc) (setmref((f)->fr.tp.pcr, (pc))) -#define setframe_ftsz(f, sz) ((f)->fr.tp.ftsz = (sz)) -#define setframe_gc(f, p) (setgcref((f)->fr.func, (p))) - -/* -- C stack frame ------------------------------------------------------- */ - -/* Macros to access and modify the C stack frame chain. */ - -/* These definitions must match with the arch-specific *.dasc files. */ -#if LJ_TARGET_X86 -#define CFRAME_OFS_ERRF (15*4) -#define CFRAME_OFS_NRES (14*4) -#define CFRAME_OFS_PREV (13*4) -#define CFRAME_OFS_L (12*4) -#define CFRAME_OFS_PC (6*4) -#define CFRAME_OFS_MULTRES (5*4) -#define CFRAME_SIZE (12*4) -#define CFRAME_SHIFT_MULTRES 0 -#elif LJ_TARGET_X64 -#if LJ_ABI_WIN -#define CFRAME_OFS_PREV (13*8) -#define CFRAME_OFS_PC (25*4) -#define CFRAME_OFS_L (24*4) -#define CFRAME_OFS_ERRF (23*4) -#define CFRAME_OFS_NRES (22*4) -#define CFRAME_OFS_MULTRES (21*4) -#define CFRAME_SIZE (10*8) -#define CFRAME_SIZE_JIT (CFRAME_SIZE + 9*16 + 4*8) -#define CFRAME_SHIFT_MULTRES 0 -#else -#define CFRAME_OFS_PREV (4*8) -#define CFRAME_OFS_PC (7*4) -#define CFRAME_OFS_L (6*4) -#define CFRAME_OFS_ERRF (5*4) -#define CFRAME_OFS_NRES (4*4) -#define CFRAME_OFS_MULTRES (1*4) -#define CFRAME_SIZE (10*8) -#define CFRAME_SIZE_JIT (CFRAME_SIZE + 16) -#define CFRAME_SHIFT_MULTRES 0 -#endif -#elif LJ_TARGET_ARM -#define CFRAME_OFS_ERRF 24 -#define CFRAME_OFS_NRES 20 -#define CFRAME_OFS_PREV 16 -#define CFRAME_OFS_L 12 -#define CFRAME_OFS_PC 8 -#define CFRAME_OFS_MULTRES 4 -#if LJ_ARCH_HASFPU -#define CFRAME_SIZE 128 -#else -#define CFRAME_SIZE 64 -#endif -#define CFRAME_SHIFT_MULTRES 3 -#elif LJ_TARGET_PPC -#if LJ_TARGET_XBOX360 -#define CFRAME_OFS_ERRF 424 -#define CFRAME_OFS_NRES 420 -#define CFRAME_OFS_PREV 400 -#define CFRAME_OFS_L 416 -#define CFRAME_OFS_PC 412 -#define CFRAME_OFS_MULTRES 408 -#define CFRAME_SIZE 384 -#define CFRAME_SHIFT_MULTRES 3 -#elif LJ_ARCH_PPC64 -#define CFRAME_OFS_ERRF 472 -#define CFRAME_OFS_NRES 468 -#define CFRAME_OFS_PREV 448 -#define CFRAME_OFS_L 464 -#define CFRAME_OFS_PC 460 -#define CFRAME_OFS_MULTRES 456 -#define CFRAME_SIZE 400 -#define CFRAME_SHIFT_MULTRES 3 -#else -#define CFRAME_OFS_ERRF 48 -#define CFRAME_OFS_NRES 44 -#define CFRAME_OFS_PREV 40 -#define CFRAME_OFS_L 36 -#define CFRAME_OFS_PC 32 -#define CFRAME_OFS_MULTRES 28 -#define CFRAME_SIZE 272 -#define CFRAME_SHIFT_MULTRES 3 -#endif -#elif LJ_TARGET_PPCSPE -#define CFRAME_OFS_ERRF 28 -#define CFRAME_OFS_NRES 24 -#define CFRAME_OFS_PREV 20 -#define CFRAME_OFS_L 16 -#define CFRAME_OFS_PC 12 -#define CFRAME_OFS_MULTRES 8 -#define CFRAME_SIZE 184 -#define CFRAME_SHIFT_MULTRES 3 -#elif LJ_TARGET_MIPS -#define CFRAME_OFS_ERRF 124 -#define CFRAME_OFS_NRES 120 -#define CFRAME_OFS_PREV 116 -#define CFRAME_OFS_L 112 -#define CFRAME_OFS_PC 20 -#define CFRAME_OFS_MULTRES 16 -#define CFRAME_SIZE 112 -#define CFRAME_SHIFT_MULTRES 3 -#else -#error "Missing CFRAME_* definitions for this architecture" -#endif - -#ifndef CFRAME_SIZE_JIT -#define CFRAME_SIZE_JIT CFRAME_SIZE -#endif - -#define CFRAME_RESUME 1 -#define CFRAME_UNWIND_FF 2 /* Only used in unwinder. */ -#define CFRAME_RAWMASK (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF)) - -#define cframe_errfunc(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF)) -#define cframe_nres(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES)) -#define cframe_prev(cf) (*(void **)(((char *)(cf))+CFRAME_OFS_PREV)) -#define cframe_multres(cf) (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES)) -#define cframe_multres_n(cf) (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES) -#define cframe_L(cf) \ - (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th) -#define cframe_pc(cf) \ - (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns)) -#define setcframe_L(cf, L) \ - (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L))) -#define setcframe_pc(cf, pc) \ - (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc))) -#define cframe_canyield(cf) ((intptr_t)(cf) & CFRAME_RESUME) -#define cframe_unwind_ff(cf) ((intptr_t)(cf) & CFRAME_UNWIND_FF) -#define cframe_raw(cf) ((void *)((intptr_t)(cf) & CFRAME_RAWMASK)) -#define cframe_Lpc(L) cframe_pc(cframe_raw(L->cframe)) - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_func.c b/third-party/LuaJIT-2.0.2/src/lj_func.c deleted file mode 100644 index 83f2d0b58a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_func.c +++ /dev/null @@ -1,185 +0,0 @@ -/* -** Function handling (prototypes, functions and upvalues). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_func_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_func.h" -#include "lj_trace.h" -#include "lj_vm.h" - -/* -- Prototypes ---------------------------------------------------------- */ - -void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt) -{ - lj_mem_free(g, pt, pt->sizept); -} - -/* -- Upvalues ------------------------------------------------------------ */ - -static void unlinkuv(GCupval *uv) -{ - lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); - setgcrefr(uvnext(uv)->prev, uv->prev); - setgcrefr(uvprev(uv)->next, uv->next); -} - -/* Find existing open upvalue for a stack slot or create a new one. */ -static GCupval *func_finduv(lua_State *L, TValue *slot) -{ - global_State *g = G(L); - GCRef *pp = &L->openupval; - GCupval *p; - GCupval *uv; - /* Search the sorted list of open upvalues. */ - while (gcref(*pp) != NULL && uvval((p = gco2uv(gcref(*pp)))) >= slot) { - lua_assert(!p->closed && uvval(p) != &p->tv); - if (uvval(p) == slot) { /* Found open upvalue pointing to same slot? */ - if (isdead(g, obj2gco(p))) /* Resurrect it, if it's dead. */ - flipwhite(obj2gco(p)); - return p; - } - pp = &p->nextgc; - } - /* No matching upvalue found. Create a new one. */ - uv = lj_mem_newt(L, sizeof(GCupval), GCupval); - newwhite(g, uv); - uv->gct = ~LJ_TUPVAL; - uv->closed = 0; /* Still open. */ - setmref(uv->v, slot); /* Pointing to the stack slot. */ - /* NOBARRIER: The GCupval is new (marked white) and open. */ - setgcrefr(uv->nextgc, *pp); /* Insert into sorted list of open upvalues. */ - setgcref(*pp, obj2gco(uv)); - setgcref(uv->prev, obj2gco(&g->uvhead)); /* Insert into GC list, too. */ - setgcrefr(uv->next, g->uvhead.next); - setgcref(uvnext(uv)->prev, obj2gco(uv)); - setgcref(g->uvhead.next, obj2gco(uv)); - lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); - return uv; -} - -/* Create an empty and closed upvalue. */ -static GCupval *func_emptyuv(lua_State *L) -{ - GCupval *uv = (GCupval *)lj_mem_newgco(L, sizeof(GCupval)); - uv->gct = ~LJ_TUPVAL; - uv->closed = 1; - setnilV(&uv->tv); - setmref(uv->v, &uv->tv); - return uv; -} - -/* Close all open upvalues pointing to some stack level or above. */ -void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level) -{ - GCupval *uv; - global_State *g = G(L); - while (gcref(L->openupval) != NULL && - uvval((uv = gco2uv(gcref(L->openupval)))) >= level) { - GCobj *o = obj2gco(uv); - lua_assert(!isblack(o) && !uv->closed && uvval(uv) != &uv->tv); - setgcrefr(L->openupval, uv->nextgc); /* No longer in open list. */ - if (isdead(g, o)) { - lj_func_freeuv(g, uv); - } else { - unlinkuv(uv); - lj_gc_closeuv(g, uv); - } - } -} - -void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv) -{ - if (!uv->closed) - unlinkuv(uv); - lj_mem_freet(g, uv); -} - -/* -- Functions (closures) ------------------------------------------------ */ - -GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env) -{ - GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeCfunc(nelems)); - fn->c.gct = ~LJ_TFUNC; - fn->c.ffid = FF_C; - fn->c.nupvalues = (uint8_t)nelems; - /* NOBARRIER: The GCfunc is new (marked white). */ - setmref(fn->c.pc, &G(L)->bc_cfunc_ext); - setgcref(fn->c.env, obj2gco(env)); - return fn; -} - -static GCfunc *func_newL(lua_State *L, GCproto *pt, GCtab *env) -{ - uint32_t count; - GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeLfunc((MSize)pt->sizeuv)); - fn->l.gct = ~LJ_TFUNC; - fn->l.ffid = FF_LUA; - fn->l.nupvalues = 0; /* Set to zero until upvalues are initialized. */ - /* NOBARRIER: Really a setgcref. But the GCfunc is new (marked white). */ - setmref(fn->l.pc, proto_bc(pt)); - setgcref(fn->l.env, obj2gco(env)); - /* Saturating 3 bit counter (0..7) for created closures. */ - count = (uint32_t)pt->flags + PROTO_CLCOUNT; - pt->flags = (uint8_t)(count - ((count >> PROTO_CLC_BITS) & PROTO_CLCOUNT)); - return fn; -} - -/* Create a new Lua function with empty upvalues. */ -GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env) -{ - GCfunc *fn = func_newL(L, pt, env); - MSize i, nuv = pt->sizeuv; - /* NOBARRIER: The GCfunc is new (marked white). */ - for (i = 0; i < nuv; i++) { - GCupval *uv = func_emptyuv(L); - uv->dhash = (uint32_t)(uintptr_t)pt ^ ((uint32_t)proto_uv(pt)[i] << 24); - setgcref(fn->l.uvptr[i], obj2gco(uv)); - } - fn->l.nupvalues = (uint8_t)nuv; - return fn; -} - -/* Do a GC check and create a new Lua function with inherited upvalues. */ -GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent) -{ - GCfunc *fn; - GCRef *puv; - MSize i, nuv; - TValue *base; - lj_gc_check_fixtop(L); - fn = func_newL(L, pt, tabref(parent->env)); - /* NOBARRIER: The GCfunc is new (marked white). */ - puv = parent->uvptr; - nuv = pt->sizeuv; - base = L->base; - for (i = 0; i < nuv; i++) { - uint32_t v = proto_uv(pt)[i]; - GCupval *uv; - if ((v & PROTO_UV_LOCAL)) { - uv = func_finduv(L, base + (v & 0xff)); - uv->immutable = ((v / PROTO_UV_IMMUTABLE) & 1); - uv->dhash = (uint32_t)(uintptr_t)mref(parent->pc, char) ^ (v << 24); - } else { - uv = &gcref(puv[v])->uv; - } - setgcref(fn->l.uvptr[i], obj2gco(uv)); - } - fn->l.nupvalues = (uint8_t)nuv; - return fn; -} - -void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *fn) -{ - MSize size = isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) : - sizeCfunc((MSize)fn->c.nupvalues); - lj_mem_free(g, fn, size); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_func.h b/third-party/LuaJIT-2.0.2/src/lj_func.h deleted file mode 100644 index 73280a8e1a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_func.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -** Function handling (prototypes, functions and upvalues). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FUNC_H -#define _LJ_FUNC_H - -#include "lj_obj.h" - -/* Prototypes. */ -LJ_FUNC void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt); - -/* Upvalues. */ -LJ_FUNCA void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level); -LJ_FUNC void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv); - -/* Functions (closures). */ -LJ_FUNC GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env); -LJ_FUNC GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env); -LJ_FUNCA GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent); -LJ_FUNC void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *c); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_gc.c b/third-party/LuaJIT-2.0.2/src/lj_gc.c deleted file mode 100644 index 79f8b72044..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_gc.c +++ /dev/null @@ -1,839 +0,0 @@ -/* -** Garbage collector. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_gc_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_udata.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#endif -#include "lj_trace.h" -#include "lj_vm.h" - -#define GCSTEPSIZE 1024u -#define GCSWEEPMAX 40 -#define GCSWEEPCOST 10 -#define GCFINALIZECOST 100 - -/* Macros to set GCobj colors and flags. */ -#define white2gray(x) ((x)->gch.marked &= (uint8_t)~LJ_GC_WHITES) -#define gray2black(x) ((x)->gch.marked |= LJ_GC_BLACK) -#define isfinalized(u) ((u)->marked & LJ_GC_FINALIZED) - -/* -- Mark phase ---------------------------------------------------------- */ - -/* Mark a TValue (if needed). */ -#define gc_marktv(g, tv) \ - { lua_assert(!tvisgcv(tv) || (~itype(tv) == gcval(tv)->gch.gct)); \ - if (tviswhite(tv)) gc_mark(g, gcV(tv)); } - -/* Mark a GCobj (if needed). */ -#define gc_markobj(g, o) \ - { if (iswhite(obj2gco(o))) gc_mark(g, obj2gco(o)); } - -/* Mark a string object. */ -#define gc_mark_str(s) ((s)->marked &= (uint8_t)~LJ_GC_WHITES) - -/* Mark a white GCobj. */ -static void gc_mark(global_State *g, GCobj *o) -{ - int gct = o->gch.gct; - lua_assert(iswhite(o) && !isdead(g, o)); - white2gray(o); - if (LJ_UNLIKELY(gct == ~LJ_TUDATA)) { - GCtab *mt = tabref(gco2ud(o)->metatable); - gray2black(o); /* Userdata are never gray. */ - if (mt) gc_markobj(g, mt); - gc_markobj(g, tabref(gco2ud(o)->env)); - } else if (LJ_UNLIKELY(gct == ~LJ_TUPVAL)) { - GCupval *uv = gco2uv(o); - gc_marktv(g, uvval(uv)); - if (uv->closed) - gray2black(o); /* Closed upvalues are never gray. */ - } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) { - lua_assert(gct == ~LJ_TFUNC || gct == ~LJ_TTAB || - gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO); - setgcrefr(o->gch.gclist, g->gc.gray); - setgcref(g->gc.gray, o); - } -} - -/* Mark GC roots. */ -static void gc_mark_gcroot(global_State *g) -{ - ptrdiff_t i; - for (i = 0; i < GCROOT_MAX; i++) - if (gcref(g->gcroot[i]) != NULL) - gc_markobj(g, gcref(g->gcroot[i])); -} - -/* Start a GC cycle and mark the root set. */ -static void gc_mark_start(global_State *g) -{ - setgcrefnull(g->gc.gray); - setgcrefnull(g->gc.grayagain); - setgcrefnull(g->gc.weak); - gc_markobj(g, mainthread(g)); - gc_markobj(g, tabref(mainthread(g)->env)); - gc_marktv(g, &g->registrytv); - gc_mark_gcroot(g); - g->gc.state = GCSpropagate; -} - -/* Mark open upvalues. */ -static void gc_mark_uv(global_State *g) -{ - GCupval *uv; - for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) { - lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); - if (isgray(obj2gco(uv))) - gc_marktv(g, uvval(uv)); - } -} - -/* Mark userdata in mmudata list. */ -static void gc_mark_mmudata(global_State *g) -{ - GCobj *root = gcref(g->gc.mmudata); - GCobj *u = root; - if (u) { - do { - u = gcnext(u); - makewhite(g, u); /* Could be from previous GC. */ - gc_mark(g, u); - } while (u != root); - } -} - -/* Separate userdata objects to be finalized to mmudata list. */ -size_t lj_gc_separateudata(global_State *g, int all) -{ - size_t m = 0; - GCRef *p = &mainthread(g)->nextgc; - GCobj *o; - while ((o = gcref(*p)) != NULL) { - if (!(iswhite(o) || all) || isfinalized(gco2ud(o))) { - p = &o->gch.nextgc; /* Nothing to do. */ - } else if (!lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc)) { - markfinalized(o); /* Done, as there's no __gc metamethod. */ - p = &o->gch.nextgc; - } else { /* Otherwise move userdata to be finalized to mmudata list. */ - m += sizeudata(gco2ud(o)); - markfinalized(o); - *p = o->gch.nextgc; - if (gcref(g->gc.mmudata)) { /* Link to end of mmudata list. */ - GCobj *root = gcref(g->gc.mmudata); - setgcrefr(o->gch.nextgc, root->gch.nextgc); - setgcref(root->gch.nextgc, o); - setgcref(g->gc.mmudata, o); - } else { /* Create circular list. */ - setgcref(o->gch.nextgc, o); - setgcref(g->gc.mmudata, o); - } - } - } - return m; -} - -/* -- Propagation phase --------------------------------------------------- */ - -/* Traverse a table. */ -static int gc_traverse_tab(global_State *g, GCtab *t) -{ - int weak = 0; - cTValue *mode; - GCtab *mt = tabref(t->metatable); - if (mt) - gc_markobj(g, mt); - mode = lj_meta_fastg(g, mt, MM_mode); - if (mode && tvisstr(mode)) { /* Valid __mode field? */ - const char *modestr = strVdata(mode); - int c; - while ((c = *modestr++)) { - if (c == 'k') weak |= LJ_GC_WEAKKEY; - else if (c == 'v') weak |= LJ_GC_WEAKVAL; - else if (c == 'K') weak = (int)(~0u & ~LJ_GC_WEAKVAL); - } - if (weak > 0) { /* Weak tables are cleared in the atomic phase. */ - t->marked = (uint8_t)((t->marked & ~LJ_GC_WEAK) | weak); - setgcrefr(t->gclist, g->gc.weak); - setgcref(g->gc.weak, obj2gco(t)); - } - } - if (weak == LJ_GC_WEAK) /* Nothing to mark if both keys/values are weak. */ - return 1; - if (!(weak & LJ_GC_WEAKVAL)) { /* Mark array part. */ - MSize i, asize = t->asize; - for (i = 0; i < asize; i++) - gc_marktv(g, arrayslot(t, i)); - } - if (t->hmask > 0) { /* Mark hash part. */ - Node *node = noderef(t->node); - MSize i, hmask = t->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (!tvisnil(&n->val)) { /* Mark non-empty slot. */ - lua_assert(!tvisnil(&n->key)); - if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key); - if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val); - } - } - } - return weak; -} - -/* Traverse a function. */ -static void gc_traverse_func(global_State *g, GCfunc *fn) -{ - gc_markobj(g, tabref(fn->c.env)); - if (isluafunc(fn)) { - uint32_t i; - lua_assert(fn->l.nupvalues <= funcproto(fn)->sizeuv); - gc_markobj(g, funcproto(fn)); - for (i = 0; i < fn->l.nupvalues; i++) /* Mark Lua function upvalues. */ - gc_markobj(g, &gcref(fn->l.uvptr[i])->uv); - } else { - uint32_t i; - for (i = 0; i < fn->c.nupvalues; i++) /* Mark C function upvalues. */ - gc_marktv(g, &fn->c.upvalue[i]); - } -} - -#if LJ_HASJIT -/* Mark a trace. */ -static void gc_marktrace(global_State *g, TraceNo traceno) -{ - GCobj *o = obj2gco(traceref(G2J(g), traceno)); - lua_assert(traceno != G2J(g)->cur.traceno); - if (iswhite(o)) { - white2gray(o); - setgcrefr(o->gch.gclist, g->gc.gray); - setgcref(g->gc.gray, o); - } -} - -/* Traverse a trace. */ -static void gc_traverse_trace(global_State *g, GCtrace *T) -{ - IRRef ref; - if (T->traceno == 0) return; - for (ref = T->nk; ref < REF_TRUE; ref++) { - IRIns *ir = &T->ir[ref]; - if (ir->o == IR_KGC) - gc_markobj(g, ir_kgc(ir)); - } - if (T->link) gc_marktrace(g, T->link); - if (T->nextroot) gc_marktrace(g, T->nextroot); - if (T->nextside) gc_marktrace(g, T->nextside); - gc_markobj(g, gcref(T->startpt)); -} - -/* The current trace is a GC root while not anchored in the prototype (yet). */ -#define gc_traverse_curtrace(g) gc_traverse_trace(g, &G2J(g)->cur) -#else -#define gc_traverse_curtrace(g) UNUSED(g) -#endif - -/* Traverse a prototype. */ -static void gc_traverse_proto(global_State *g, GCproto *pt) -{ - ptrdiff_t i; - gc_mark_str(proto_chunkname(pt)); - for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */ - gc_markobj(g, proto_kgc(pt, i)); -#if LJ_HASJIT - if (pt->trace) gc_marktrace(g, pt->trace); -#endif -} - -/* Traverse the frame structure of a stack. */ -static MSize gc_traverse_frames(global_State *g, lua_State *th) -{ - TValue *frame, *top = th->top-1, *bot = tvref(th->stack); - /* Note: extra vararg frame not skipped, marks function twice (harmless). */ - for (frame = th->base-1; frame > bot; frame = frame_prev(frame)) { - GCfunc *fn = frame_func(frame); - TValue *ftop = frame; - if (isluafunc(fn)) ftop += funcproto(fn)->framesize; - if (ftop > top) top = ftop; - gc_markobj(g, fn); /* Need to mark hidden function (or L). */ - } - top++; /* Correct bias of -1 (frame == base-1). */ - if (top > tvref(th->maxstack)) top = tvref(th->maxstack); - return (MSize)(top - bot); /* Return minimum needed stack size. */ -} - -/* Traverse a thread object. */ -static void gc_traverse_thread(global_State *g, lua_State *th) -{ - TValue *o, *top = th->top; - for (o = tvref(th->stack)+1; o < top; o++) - gc_marktv(g, o); - if (g->gc.state == GCSatomic) { - top = tvref(th->stack) + th->stacksize; - for (; o < top; o++) /* Clear unmarked slots. */ - setnilV(o); - } - gc_markobj(g, tabref(th->env)); - lj_state_shrinkstack(th, gc_traverse_frames(g, th)); -} - -/* Propagate one gray object. Traverse it and turn it black. */ -static size_t propagatemark(global_State *g) -{ - GCobj *o = gcref(g->gc.gray); - int gct = o->gch.gct; - lua_assert(isgray(o)); - gray2black(o); - setgcrefr(g->gc.gray, o->gch.gclist); /* Remove from gray list. */ - if (LJ_LIKELY(gct == ~LJ_TTAB)) { - GCtab *t = gco2tab(o); - if (gc_traverse_tab(g, t) > 0) - black2gray(o); /* Keep weak tables gray. */ - return sizeof(GCtab) + sizeof(TValue) * t->asize + - sizeof(Node) * (t->hmask + 1); - } else if (LJ_LIKELY(gct == ~LJ_TFUNC)) { - GCfunc *fn = gco2func(o); - gc_traverse_func(g, fn); - return isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) : - sizeCfunc((MSize)fn->c.nupvalues); - } else if (LJ_LIKELY(gct == ~LJ_TPROTO)) { - GCproto *pt = gco2pt(o); - gc_traverse_proto(g, pt); - return pt->sizept; - } else if (LJ_LIKELY(gct == ~LJ_TTHREAD)) { - lua_State *th = gco2th(o); - setgcrefr(th->gclist, g->gc.grayagain); - setgcref(g->gc.grayagain, o); - black2gray(o); /* Threads are never black. */ - gc_traverse_thread(g, th); - return sizeof(lua_State) + sizeof(TValue) * th->stacksize; - } else { -#if LJ_HASJIT - GCtrace *T = gco2trace(o); - gc_traverse_trace(g, T); - return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + - T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry); -#else - lua_assert(0); - return 0; -#endif - } -} - -/* Propagate all gray objects. */ -static size_t gc_propagate_gray(global_State *g) -{ - size_t m = 0; - while (gcref(g->gc.gray) != NULL) - m += propagatemark(g); - return m; -} - -/* -- Sweep phase --------------------------------------------------------- */ - -/* Try to shrink some common data structures. */ -static void gc_shrink(global_State *g, lua_State *L) -{ - if (g->strnum <= (g->strmask >> 2) && g->strmask > LJ_MIN_STRTAB*2-1) - lj_str_resize(L, g->strmask >> 1); /* Shrink string table. */ - if (g->tmpbuf.sz > LJ_MIN_SBUF*2) - lj_str_resizebuf(L, &g->tmpbuf, g->tmpbuf.sz >> 1); /* Shrink temp buf. */ -} - -/* Type of GC free functions. */ -typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o); - -/* GC free functions for LJ_TSTR .. LJ_TUDATA. ORDER LJ_T */ -static const GCFreeFunc gc_freefunc[] = { - (GCFreeFunc)lj_str_free, - (GCFreeFunc)lj_func_freeuv, - (GCFreeFunc)lj_state_free, - (GCFreeFunc)lj_func_freeproto, - (GCFreeFunc)lj_func_free, -#if LJ_HASJIT - (GCFreeFunc)lj_trace_free, -#else - (GCFreeFunc)0, -#endif -#if LJ_HASFFI - (GCFreeFunc)lj_cdata_free, -#else - (GCFreeFunc)0, -#endif - (GCFreeFunc)lj_tab_free, - (GCFreeFunc)lj_udata_free -}; - -/* Full sweep of a GC list. */ -#define gc_fullsweep(g, p) gc_sweep(g, (p), LJ_MAX_MEM) - -/* Partial sweep of a GC list. */ -static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim) -{ - /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */ - int ow = otherwhite(g); - GCobj *o; - while ((o = gcref(*p)) != NULL && lim-- > 0) { - if (o->gch.gct == ~LJ_TTHREAD) /* Need to sweep open upvalues, too. */ - gc_fullsweep(g, &gco2th(o)->openupval); - if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */ - lua_assert(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED)); - makewhite(g, o); /* Value is alive, change to the current white. */ - p = &o->gch.nextgc; - } else { /* Otherwise value is dead, free it. */ - lua_assert(isdead(g, o) || ow == LJ_GC_SFIXED); - setgcrefr(*p, o->gch.nextgc); - if (o == gcref(g->gc.root)) - setgcrefr(g->gc.root, o->gch.nextgc); /* Adjust list anchor. */ - gc_freefunc[o->gch.gct - ~LJ_TSTR](g, o); - } - } - return p; -} - -/* Check whether we can clear a key or a value slot from a table. */ -static int gc_mayclear(cTValue *o, int val) -{ - if (tvisgcv(o)) { /* Only collectable objects can be weak references. */ - if (tvisstr(o)) { /* But strings cannot be used as weak references. */ - gc_mark_str(strV(o)); /* And need to be marked. */ - return 0; - } - if (iswhite(gcV(o))) - return 1; /* Object is about to be collected. */ - if (tvisudata(o) && val && isfinalized(udataV(o))) - return 1; /* Finalized userdata is dropped only from values. */ - } - return 0; /* Cannot clear. */ -} - -/* Clear collected entries from weak tables. */ -static void gc_clearweak(GCobj *o) -{ - while (o) { - GCtab *t = gco2tab(o); - lua_assert((t->marked & LJ_GC_WEAK)); - if ((t->marked & LJ_GC_WEAKVAL)) { - MSize i, asize = t->asize; - for (i = 0; i < asize; i++) { - /* Clear array slot when value is about to be collected. */ - TValue *tv = arrayslot(t, i); - if (gc_mayclear(tv, 1)) - setnilV(tv); - } - } - if (t->hmask > 0) { - Node *node = noderef(t->node); - MSize i, hmask = t->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - /* Clear hash slot when key or value is about to be collected. */ - if (!tvisnil(&n->val) && (gc_mayclear(&n->key, 0) || - gc_mayclear(&n->val, 1))) - setnilV(&n->val); - } - } - o = gcref(t->gclist); - } -} - -/* Call a userdata or cdata finalizer. */ -static void gc_call_finalizer(global_State *g, lua_State *L, - cTValue *mo, GCobj *o) -{ - /* Save and restore lots of state around the __gc callback. */ - uint8_t oldh = hook_save(g); - MSize oldt = g->gc.threshold; - int errcode; - TValue *top; - lj_trace_abort(g); - top = L->top; - L->top = top+2; - hook_entergc(g); /* Disable hooks and new traces during __gc. */ - g->gc.threshold = LJ_MAX_MEM; /* Prevent GC steps. */ - copyTV(L, top, mo); - setgcV(L, top+1, o, ~o->gch.gct); - errcode = lj_vm_pcall(L, top+1, 1+0, -1); /* Stack: |mo|o| -> | */ - hook_restore(g, oldh); - g->gc.threshold = oldt; /* Restore GC threshold. */ - if (errcode) - lj_err_throw(L, errcode); /* Propagate errors. */ -} - -/* Finalize one userdata or cdata object from the mmudata list. */ -static void gc_finalize(lua_State *L) -{ - global_State *g = G(L); - GCobj *o = gcnext(gcref(g->gc.mmudata)); - cTValue *mo; - lua_assert(gcref(g->jit_L) == NULL); /* Must not be called on trace. */ - /* Unchain from list of userdata to be finalized. */ - if (o == gcref(g->gc.mmudata)) - setgcrefnull(g->gc.mmudata); - else - setgcrefr(gcref(g->gc.mmudata)->gch.nextgc, o->gch.nextgc); -#if LJ_HASFFI - if (o->gch.gct == ~LJ_TCDATA) { - TValue tmp, *tv; - /* Add cdata back to the GC list and make it white. */ - setgcrefr(o->gch.nextgc, g->gc.root); - setgcref(g->gc.root, o); - makewhite(g, o); - o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN; - /* Resolve finalizer. */ - setcdataV(L, &tmp, gco2cd(o)); - tv = lj_tab_set(L, ctype_ctsG(g)->finalizer, &tmp); - if (!tvisnil(tv)) { - copyTV(L, &tmp, tv); - setnilV(tv); /* Clear entry in finalizer table. */ - gc_call_finalizer(g, L, &tmp, o); - } - return; - } -#endif - /* Add userdata back to the main userdata list and make it white. */ - setgcrefr(o->gch.nextgc, mainthread(g)->nextgc); - setgcref(mainthread(g)->nextgc, o); - makewhite(g, o); - /* Resolve the __gc metamethod. */ - mo = lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc); - if (mo) - gc_call_finalizer(g, L, mo, o); -} - -/* Finalize all userdata objects from mmudata list. */ -void lj_gc_finalize_udata(lua_State *L) -{ - while (gcref(G(L)->gc.mmudata) != NULL) - gc_finalize(L); -} - -#if LJ_HASFFI -/* Finalize all cdata objects from finalizer table. */ -void lj_gc_finalize_cdata(lua_State *L) -{ - global_State *g = G(L); - CTState *cts = ctype_ctsG(g); - if (cts) { - GCtab *t = cts->finalizer; - Node *node = noderef(t->node); - ptrdiff_t i; - setgcrefnull(t->metatable); /* Mark finalizer table as disabled. */ - for (i = (ptrdiff_t)t->hmask; i >= 0; i--) - if (!tvisnil(&node[i].val) && tviscdata(&node[i].key)) { - GCobj *o = gcV(&node[i].key); - TValue tmp; - makewhite(g, o); - o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN; - copyTV(L, &tmp, &node[i].val); - setnilV(&node[i].val); - gc_call_finalizer(g, L, &tmp, o); - } - } -} -#endif - -/* Free all remaining GC objects. */ -void lj_gc_freeall(global_State *g) -{ - MSize i, strmask; - /* Free everything, except super-fixed objects (the main thread). */ - g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED; - gc_fullsweep(g, &g->gc.root); - strmask = g->strmask; - for (i = 0; i <= strmask; i++) /* Free all string hash chains. */ - gc_fullsweep(g, &g->strhash[i]); -} - -/* -- Collector ----------------------------------------------------------- */ - -/* Atomic part of the GC cycle, transitioning from mark to sweep phase. */ -static void atomic(global_State *g, lua_State *L) -{ - size_t udsize; - - gc_mark_uv(g); /* Need to remark open upvalues (the thread may be dead). */ - gc_propagate_gray(g); /* Propagate any left-overs. */ - - setgcrefr(g->gc.gray, g->gc.weak); /* Empty the list of weak tables. */ - setgcrefnull(g->gc.weak); - lua_assert(!iswhite(obj2gco(mainthread(g)))); - gc_markobj(g, L); /* Mark running thread. */ - gc_traverse_curtrace(g); /* Traverse current trace. */ - gc_mark_gcroot(g); /* Mark GC roots (again). */ - gc_propagate_gray(g); /* Propagate all of the above. */ - - setgcrefr(g->gc.gray, g->gc.grayagain); /* Empty the 2nd chance list. */ - setgcrefnull(g->gc.grayagain); - gc_propagate_gray(g); /* Propagate it. */ - - udsize = lj_gc_separateudata(g, 0); /* Separate userdata to be finalized. */ - gc_mark_mmudata(g); /* Mark them. */ - udsize += gc_propagate_gray(g); /* And propagate the marks. */ - - /* All marking done, clear weak tables. */ - gc_clearweak(gcref(g->gc.weak)); - - /* Prepare for sweep phase. */ - g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */ - g->strempty.marked = g->gc.currentwhite; - setmref(g->gc.sweep, &g->gc.root); - g->gc.estimate = g->gc.total - (MSize)udsize; /* Initial estimate. */ -} - -/* GC state machine. Returns a cost estimate for each step performed. */ -static size_t gc_onestep(lua_State *L) -{ - global_State *g = G(L); - switch (g->gc.state) { - case GCSpause: - gc_mark_start(g); /* Start a new GC cycle by marking all GC roots. */ - return 0; - case GCSpropagate: - if (gcref(g->gc.gray) != NULL) - return propagatemark(g); /* Propagate one gray object. */ - g->gc.state = GCSatomic; /* End of mark phase. */ - return 0; - case GCSatomic: - if (gcref(g->jit_L)) /* Don't run atomic phase on trace. */ - return LJ_MAX_MEM; - atomic(g, L); - g->gc.state = GCSsweepstring; /* Start of sweep phase. */ - g->gc.sweepstr = 0; - return 0; - case GCSsweepstring: { - MSize old = g->gc.total; - gc_fullsweep(g, &g->strhash[g->gc.sweepstr++]); /* Sweep one chain. */ - if (g->gc.sweepstr > g->strmask) - g->gc.state = GCSsweep; /* All string hash chains sweeped. */ - lua_assert(old >= g->gc.total); - g->gc.estimate -= old - g->gc.total; - return GCSWEEPCOST; - } - case GCSsweep: { - MSize old = g->gc.total; - setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX)); - if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) { - gc_shrink(g, L); - if (gcref(g->gc.mmudata)) { /* Need any finalizations? */ - g->gc.state = GCSfinalize; - } else { /* Otherwise skip this phase to help the JIT. */ - g->gc.state = GCSpause; /* End of GC cycle. */ - g->gc.debt = 0; - } - } - lua_assert(old >= g->gc.total); - g->gc.estimate -= old - g->gc.total; - return GCSWEEPMAX*GCSWEEPCOST; - } - case GCSfinalize: - if (gcref(g->gc.mmudata) != NULL) { - if (gcref(g->jit_L)) /* Don't call finalizers on trace. */ - return LJ_MAX_MEM; - gc_finalize(L); /* Finalize one userdata object. */ - if (g->gc.estimate > GCFINALIZECOST) - g->gc.estimate -= GCFINALIZECOST; - return GCFINALIZECOST; - } - g->gc.state = GCSpause; /* End of GC cycle. */ - g->gc.debt = 0; - return 0; - default: - lua_assert(0); - return 0; - } -} - -/* Perform a limited amount of incremental GC steps. */ -int LJ_FASTCALL lj_gc_step(lua_State *L) -{ - global_State *g = G(L); - MSize lim; - int32_t ostate = g->vmstate; - setvmstate(g, GC); - lim = (GCSTEPSIZE/100) * g->gc.stepmul; - if (lim == 0) - lim = LJ_MAX_MEM; - g->gc.debt += g->gc.total - g->gc.threshold; - do { - lim -= (MSize)gc_onestep(L); - if (g->gc.state == GCSpause) { - g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; - g->vmstate = ostate; - return 1; /* Finished a GC cycle. */ - } - } while ((int32_t)lim > 0); - if (g->gc.debt < GCSTEPSIZE) { - g->gc.threshold = g->gc.total + GCSTEPSIZE; - } else { - g->gc.debt -= GCSTEPSIZE; - g->gc.threshold = g->gc.total; - } - g->vmstate = ostate; - return 0; -} - -/* Ditto, but fix the stack top first. */ -void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L) -{ - if (curr_funcisL(L)) L->top = curr_topL(L); - lj_gc_step(L); -} - -#if LJ_HASJIT -/* Perform multiple GC steps. Called from JIT-compiled code. */ -int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps) -{ - lua_State *L = gco2th(gcref(g->jit_L)); - L->base = mref(G(L)->jit_base, TValue); - L->top = curr_topL(L); - while (steps-- > 0 && lj_gc_step(L) == 0) - ; - /* Return 1 to force a trace exit. */ - return (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize); -} -#endif - -/* Perform a full GC cycle. */ -void lj_gc_fullgc(lua_State *L) -{ - global_State *g = G(L); - int32_t ostate = g->vmstate; - setvmstate(g, GC); - if (g->gc.state <= GCSatomic) { /* Caught somewhere in the middle. */ - setmref(g->gc.sweep, &g->gc.root); /* Sweep everything (preserving it). */ - setgcrefnull(g->gc.gray); /* Reset lists from partial propagation. */ - setgcrefnull(g->gc.grayagain); - setgcrefnull(g->gc.weak); - g->gc.state = GCSsweepstring; /* Fast forward to the sweep phase. */ - g->gc.sweepstr = 0; - } - while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep) - gc_onestep(L); /* Finish sweep. */ - lua_assert(g->gc.state == GCSfinalize || g->gc.state == GCSpause); - /* Now perform a full GC. */ - g->gc.state = GCSpause; - do { gc_onestep(L); } while (g->gc.state != GCSpause); - g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; - g->vmstate = ostate; -} - -/* -- Write barriers ------------------------------------------------------ */ - -/* Move the GC propagation frontier forward. */ -void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v) -{ - lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); - lua_assert(o->gch.gct != ~LJ_TTAB); - /* Preserve invariant during propagation. Otherwise it doesn't matter. */ - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) - gc_mark(g, v); /* Move frontier forward. */ - else - makewhite(g, o); /* Make it white to avoid the following barrier. */ -} - -/* Specialized barrier for closed upvalue. Pass &uv->tv. */ -void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv) -{ -#define TV2MARKED(x) \ - (*((uint8_t *)(x) - offsetof(GCupval, tv) + offsetof(GCupval, marked))) - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) - gc_mark(g, gcV(tv)); - else - TV2MARKED(tv) = (TV2MARKED(tv) & (uint8_t)~LJ_GC_COLORS) | curwhite(g); -#undef TV2MARKED -} - -/* Close upvalue. Also needs a write barrier. */ -void lj_gc_closeuv(global_State *g, GCupval *uv) -{ - GCobj *o = obj2gco(uv); - /* Copy stack slot to upvalue itself and point to the copy. */ - copyTV(mainthread(g), &uv->tv, uvval(uv)); - setmref(uv->v, &uv->tv); - uv->closed = 1; - setgcrefr(o->gch.nextgc, g->gc.root); - setgcref(g->gc.root, o); - if (isgray(o)) { /* A closed upvalue is never gray, so fix this. */ - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) { - gray2black(o); /* Make it black and preserve invariant. */ - if (tviswhite(&uv->tv)) - lj_gc_barrierf(g, o, gcV(&uv->tv)); - } else { - makewhite(g, o); /* Make it white, i.e. sweep the upvalue. */ - lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); - } - } -} - -#if LJ_HASJIT -/* Mark a trace if it's saved during the propagation phase. */ -void lj_gc_barriertrace(global_State *g, uint32_t traceno) -{ - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) - gc_marktrace(g, traceno); -} -#endif - -/* -- Allocator ----------------------------------------------------------- */ - -/* Call pluggable memory allocator to allocate or resize a fragment. */ -void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz) -{ - global_State *g = G(L); - lua_assert((osz == 0) == (p == NULL)); - p = g->allocf(g->allocd, p, osz, nsz); - if (p == NULL && nsz > 0) - lj_err_mem(L); - lua_assert((nsz == 0) == (p == NULL)); - lua_assert(checkptr32(p)); - g->gc.total = (g->gc.total - osz) + nsz; - return p; -} - -/* Allocate new GC object and link it to the root set. */ -void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size) -{ - global_State *g = G(L); - GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size); - if (o == NULL) - lj_err_mem(L); - lua_assert(checkptr32(o)); - g->gc.total += size; - setgcrefr(o->gch.nextgc, g->gc.root); - setgcref(g->gc.root, o); - newwhite(g, o); - return o; -} - -/* Resize growable vector. */ -void *lj_mem_grow(lua_State *L, void *p, MSize *szp, MSize lim, MSize esz) -{ - MSize sz = (*szp) << 1; - if (sz < LJ_MIN_VECSZ) - sz = LJ_MIN_VECSZ; - if (sz > lim) - sz = lim; - p = lj_mem_realloc(L, p, (*szp)*esz, sz*esz); - *szp = sz; - return p; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_gc.h b/third-party/LuaJIT-2.0.2/src/lj_gc.h deleted file mode 100644 index 22f7fea54c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_gc.h +++ /dev/null @@ -1,134 +0,0 @@ -/* -** Garbage collector. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_GC_H -#define _LJ_GC_H - -#include "lj_obj.h" - -/* Garbage collector states. Order matters. */ -enum { - GCSpause, GCSpropagate, GCSatomic, GCSsweepstring, GCSsweep, GCSfinalize -}; - -/* Bitmasks for marked field of GCobj. */ -#define LJ_GC_WHITE0 0x01 -#define LJ_GC_WHITE1 0x02 -#define LJ_GC_BLACK 0x04 -#define LJ_GC_FINALIZED 0x08 -#define LJ_GC_WEAKKEY 0x08 -#define LJ_GC_WEAKVAL 0x10 -#define LJ_GC_CDATA_FIN 0x10 -#define LJ_GC_FIXED 0x20 -#define LJ_GC_SFIXED 0x40 - -#define LJ_GC_WHITES (LJ_GC_WHITE0 | LJ_GC_WHITE1) -#define LJ_GC_COLORS (LJ_GC_WHITES | LJ_GC_BLACK) -#define LJ_GC_WEAK (LJ_GC_WEAKKEY | LJ_GC_WEAKVAL) - -/* Macros to test and set GCobj colors. */ -#define iswhite(x) ((x)->gch.marked & LJ_GC_WHITES) -#define isblack(x) ((x)->gch.marked & LJ_GC_BLACK) -#define isgray(x) (!((x)->gch.marked & (LJ_GC_BLACK|LJ_GC_WHITES))) -#define tviswhite(x) (tvisgcv(x) && iswhite(gcV(x))) -#define otherwhite(g) (g->gc.currentwhite ^ LJ_GC_WHITES) -#define isdead(g, v) ((v)->gch.marked & otherwhite(g) & LJ_GC_WHITES) - -#define curwhite(g) ((g)->gc.currentwhite & LJ_GC_WHITES) -#define newwhite(g, x) (obj2gco(x)->gch.marked = (uint8_t)curwhite(g)) -#define makewhite(g, x) \ - ((x)->gch.marked = ((x)->gch.marked & (uint8_t)~LJ_GC_COLORS) | curwhite(g)) -#define flipwhite(x) ((x)->gch.marked ^= LJ_GC_WHITES) -#define black2gray(x) ((x)->gch.marked &= (uint8_t)~LJ_GC_BLACK) -#define fixstring(s) ((s)->marked |= LJ_GC_FIXED) -#define markfinalized(x) ((x)->gch.marked |= LJ_GC_FINALIZED) - -/* Collector. */ -LJ_FUNC size_t lj_gc_separateudata(global_State *g, int all); -LJ_FUNC void lj_gc_finalize_udata(lua_State *L); -#if LJ_HASFFI -LJ_FUNC void lj_gc_finalize_cdata(lua_State *L); -#else -#define lj_gc_finalize_cdata(L) UNUSED(L) -#endif -LJ_FUNC void lj_gc_freeall(global_State *g); -LJ_FUNCA int LJ_FASTCALL lj_gc_step(lua_State *L); -LJ_FUNCA void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L); -#if LJ_HASJIT -LJ_FUNC int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps); -#endif -LJ_FUNC void lj_gc_fullgc(lua_State *L); - -/* GC check: drive collector forward if the GC threshold has been reached. */ -#define lj_gc_check(L) \ - { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \ - lj_gc_step(L); } -#define lj_gc_check_fixtop(L) \ - { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \ - lj_gc_step_fixtop(L); } - -/* Write barriers. */ -LJ_FUNC void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v); -LJ_FUNCA void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv); -LJ_FUNC void lj_gc_closeuv(global_State *g, GCupval *uv); -#if LJ_HASJIT -LJ_FUNC void lj_gc_barriertrace(global_State *g, uint32_t traceno); -#endif - -/* Move the GC propagation frontier back for tables (make it gray again). */ -static LJ_AINLINE void lj_gc_barrierback(global_State *g, GCtab *t) -{ - GCobj *o = obj2gco(t); - lua_assert(isblack(o) && !isdead(g, o)); - lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); - black2gray(o); - setgcrefr(t->gclist, g->gc.grayagain); - setgcref(g->gc.grayagain, o); -} - -/* Barrier for stores to table objects. TValue and GCobj variant. */ -#define lj_gc_anybarriert(L, t) \ - { if (LJ_UNLIKELY(isblack(obj2gco(t)))) lj_gc_barrierback(G(L), (t)); } -#define lj_gc_barriert(L, t, tv) \ - { if (tviswhite(tv) && isblack(obj2gco(t))) \ - lj_gc_barrierback(G(L), (t)); } -#define lj_gc_objbarriert(L, t, o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) \ - lj_gc_barrierback(G(L), (t)); } - -/* Barrier for stores to any other object. TValue and GCobj variant. */ -#define lj_gc_barrier(L, p, tv) \ - { if (tviswhite(tv) && isblack(obj2gco(p))) \ - lj_gc_barrierf(G(L), obj2gco(p), gcV(tv)); } -#define lj_gc_objbarrier(L, p, o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ - lj_gc_barrierf(G(L), obj2gco(p), obj2gco(o)); } - -/* Allocator. */ -LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz); -LJ_FUNC void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size); -LJ_FUNC void *lj_mem_grow(lua_State *L, void *p, - MSize *szp, MSize lim, MSize esz); - -#define lj_mem_new(L, s) lj_mem_realloc(L, NULL, 0, (s)) - -static LJ_AINLINE void lj_mem_free(global_State *g, void *p, size_t osize) -{ - g->gc.total -= (MSize)osize; - g->allocf(g->allocd, p, osize, 0); -} - -#define lj_mem_newvec(L, n, t) ((t *)lj_mem_new(L, (MSize)((n)*sizeof(t)))) -#define lj_mem_reallocvec(L, p, on, n, t) \ - ((p) = (t *)lj_mem_realloc(L, p, (on)*sizeof(t), (MSize)((n)*sizeof(t)))) -#define lj_mem_growvec(L, p, n, m, t) \ - ((p) = (t *)lj_mem_grow(L, (p), &(n), (m), (MSize)sizeof(t))) -#define lj_mem_freevec(g, p, n, t) lj_mem_free(g, (p), (n)*sizeof(t)) - -#define lj_mem_newobj(L, t) ((t *)lj_mem_newgco(L, sizeof(t))) -#define lj_mem_newt(L, s, t) ((t *)lj_mem_new(L, (s))) -#define lj_mem_freet(g, p) lj_mem_free(g, (p), sizeof(*(p))) - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_gdbjit.c b/third-party/LuaJIT-2.0.2/src/lj_gdbjit.c deleted file mode 100644 index 284195a15d..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_gdbjit.c +++ /dev/null @@ -1,793 +0,0 @@ -/* -** Client for the GDB JIT API. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_gdbjit_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_frame.h" -#include "lj_jit.h" -#include "lj_dispatch.h" - -/* This is not compiled in by default. -** Enable with -DLUAJIT_USE_GDBJIT in the Makefile and recompile everything. -*/ -#ifdef LUAJIT_USE_GDBJIT - -/* The GDB JIT API allows JIT compilers to pass debug information about -** JIT-compiled code back to GDB. You need at least GDB 7.0 or higher -** to see it in action. -** -** This is a passive API, so it works even when not running under GDB -** or when attaching to an already running process. Alas, this implies -** enabling it always has a non-negligible overhead -- do not use in -** release mode! -** -** The LuaJIT GDB JIT client is rather minimal at the moment. It gives -** each trace a symbol name and adds a source location and frame unwind -** information. Obviously LuaJIT itself and any embedding C application -** should be compiled with debug symbols, too (see the Makefile). -** -** Traces are named TRACE_1, TRACE_2, ... these correspond to the trace -** numbers from -jv or -jdump. Use "break TRACE_1" or "tbreak TRACE_1" etc. -** to set breakpoints on specific traces (even ahead of their creation). -** -** The source location for each trace allows listing the corresponding -** source lines with the GDB command "list" (but only if the Lua source -** has been loaded from a file). Currently this is always set to the -** location where the trace has been started. -** -** Frame unwind information can be inspected with the GDB command -** "info frame". This also allows proper backtraces across JIT-compiled -** code with the GDB command "bt". -** -** You probably want to add the following settings to a .gdbinit file -** (or add them to ~/.gdbinit): -** set disassembly-flavor intel -** set breakpoint pending on -** -** Here's a sample GDB session: -** ------------------------------------------------------------------------ - -$ cat >x.lua -for outer=1,100 do - for inner=1,100 do end -end -^D - -$ luajit -jv x.lua -[TRACE 1 x.lua:2] -[TRACE 2 (1/3) x.lua:1 -> 1] - -$ gdb --quiet --args luajit x.lua -(gdb) tbreak TRACE_1 -Function "TRACE_1" not defined. -Temporary breakpoint 1 (TRACE_1) pending. -(gdb) run -Starting program: luajit x.lua - -Temporary breakpoint 1, TRACE_1 () at x.lua:2 -2 for inner=1,100 do end -(gdb) list -1 for outer=1,100 do -2 for inner=1,100 do end -3 end -(gdb) bt -#0 TRACE_1 () at x.lua:2 -#1 0x08053690 in lua_pcall [...] -[...] -#7 0x0806ff90 in main [...] -(gdb) disass TRACE_1 -Dump of assembler code for function TRACE_1: -0xf7fd9fba : mov DWORD PTR ds:0xf7e0e2a0,0x1 -0xf7fd9fc4 : movsd xmm7,QWORD PTR [edx+0x20] -[...] -0xf7fd9ff8 : jmp 0xf7fd2014 -End of assembler dump. -(gdb) tbreak TRACE_2 -Function "TRACE_2" not defined. -Temporary breakpoint 2 (TRACE_2) pending. -(gdb) cont -Continuing. - -Temporary breakpoint 2, TRACE_2 () at x.lua:1 -1 for outer=1,100 do -(gdb) info frame -Stack level 0, frame at 0xffffd7c0: - eip = 0xf7fd9f60 in TRACE_2 (x.lua:1); saved eip 0x8053690 - called by frame at 0xffffd7e0 - source language unknown. - Arglist at 0xffffd78c, args: - Locals at 0xffffd78c, Previous frame's sp is 0xffffd7c0 - Saved registers: - ebx at 0xffffd7ac, ebp at 0xffffd7b8, esi at 0xffffd7b0, edi at 0xffffd7b4, - eip at 0xffffd7bc -(gdb) - -** ------------------------------------------------------------------------ -*/ - -/* -- GDB JIT API --------------------------------------------------------- */ - -/* GDB JIT actions. */ -enum { - GDBJIT_NOACTION = 0, - GDBJIT_REGISTER, - GDBJIT_UNREGISTER -}; - -/* GDB JIT entry. */ -typedef struct GDBJITentry { - struct GDBJITentry *next_entry; - struct GDBJITentry *prev_entry; - const char *symfile_addr; - uint64_t symfile_size; -} GDBJITentry; - -/* GDB JIT descriptor. */ -typedef struct GDBJITdesc { - uint32_t version; - uint32_t action_flag; - GDBJITentry *relevant_entry; - GDBJITentry *first_entry; -} GDBJITdesc; - -GDBJITdesc __jit_debug_descriptor = { - 1, GDBJIT_NOACTION, NULL, NULL -}; - -/* GDB sets a breakpoint at this function. */ -void LJ_NOINLINE __jit_debug_register_code() -{ - __asm__ __volatile__(""); -}; - -/* -- In-memory ELF object definitions ------------------------------------ */ - -/* ELF definitions. */ -typedef struct ELFheader { - uint8_t emagic[4]; - uint8_t eclass; - uint8_t eendian; - uint8_t eversion; - uint8_t eosabi; - uint8_t eabiversion; - uint8_t epad[7]; - uint16_t type; - uint16_t machine; - uint32_t version; - uintptr_t entry; - uintptr_t phofs; - uintptr_t shofs; - uint32_t flags; - uint16_t ehsize; - uint16_t phentsize; - uint16_t phnum; - uint16_t shentsize; - uint16_t shnum; - uint16_t shstridx; -} ELFheader; - -typedef struct ELFsectheader { - uint32_t name; - uint32_t type; - uintptr_t flags; - uintptr_t addr; - uintptr_t ofs; - uintptr_t size; - uint32_t link; - uint32_t info; - uintptr_t align; - uintptr_t entsize; -} ELFsectheader; - -#define ELFSECT_IDX_ABS 0xfff1 - -enum { - ELFSECT_TYPE_PROGBITS = 1, - ELFSECT_TYPE_SYMTAB = 2, - ELFSECT_TYPE_STRTAB = 3, - ELFSECT_TYPE_NOBITS = 8 -}; - -#define ELFSECT_FLAGS_WRITE 1 -#define ELFSECT_FLAGS_ALLOC 2 -#define ELFSECT_FLAGS_EXEC 4 - -typedef struct ELFsymbol { -#if LJ_64 - uint32_t name; - uint8_t info; - uint8_t other; - uint16_t sectidx; - uintptr_t value; - uint64_t size; -#else - uint32_t name; - uintptr_t value; - uint32_t size; - uint8_t info; - uint8_t other; - uint16_t sectidx; -#endif -} ELFsymbol; - -enum { - ELFSYM_TYPE_FUNC = 2, - ELFSYM_TYPE_FILE = 4, - ELFSYM_BIND_LOCAL = 0 << 4, - ELFSYM_BIND_GLOBAL = 1 << 4, -}; - -/* DWARF definitions. */ -#define DW_CIE_VERSION 1 - -enum { - DW_CFA_nop = 0x0, - DW_CFA_offset_extended = 0x5, - DW_CFA_def_cfa = 0xc, - DW_CFA_def_cfa_offset = 0xe, - DW_CFA_offset_extended_sf = 0x11, - DW_CFA_advance_loc = 0x40, - DW_CFA_offset = 0x80 -}; - -enum { - DW_EH_PE_udata4 = 3, - DW_EH_PE_textrel = 0x20 -}; - -enum { - DW_TAG_compile_unit = 0x11 -}; - -enum { - DW_children_no = 0, - DW_children_yes = 1 -}; - -enum { - DW_AT_name = 0x03, - DW_AT_stmt_list = 0x10, - DW_AT_low_pc = 0x11, - DW_AT_high_pc = 0x12 -}; - -enum { - DW_FORM_addr = 0x01, - DW_FORM_data4 = 0x06, - DW_FORM_string = 0x08 -}; - -enum { - DW_LNS_extended_op = 0, - DW_LNS_copy = 1, - DW_LNS_advance_pc = 2, - DW_LNS_advance_line = 3 -}; - -enum { - DW_LNE_end_sequence = 1, - DW_LNE_set_address = 2 -}; - -enum { -#if LJ_TARGET_X86 - DW_REG_AX, DW_REG_CX, DW_REG_DX, DW_REG_BX, - DW_REG_SP, DW_REG_BP, DW_REG_SI, DW_REG_DI, - DW_REG_RA, -#elif LJ_TARGET_X64 - /* Yes, the order is strange, but correct. */ - DW_REG_AX, DW_REG_DX, DW_REG_CX, DW_REG_BX, - DW_REG_SI, DW_REG_DI, DW_REG_BP, DW_REG_SP, - DW_REG_8, DW_REG_9, DW_REG_10, DW_REG_11, - DW_REG_12, DW_REG_13, DW_REG_14, DW_REG_15, - DW_REG_RA, -#elif LJ_TARGET_ARM - DW_REG_SP = 13, - DW_REG_RA = 14, -#elif LJ_TARGET_PPC - DW_REG_SP = 1, - DW_REG_RA = 65, - DW_REG_CR = 70, -#elif LJ_TARGET_MIPS - DW_REG_SP = 29, - DW_REG_RA = 31, -#else -#error "Unsupported target architecture" -#endif -}; - -/* Minimal list of sections for the in-memory ELF object. */ -enum { - GDBJIT_SECT_NULL, - GDBJIT_SECT_text, - GDBJIT_SECT_eh_frame, - GDBJIT_SECT_shstrtab, - GDBJIT_SECT_strtab, - GDBJIT_SECT_symtab, - GDBJIT_SECT_debug_info, - GDBJIT_SECT_debug_abbrev, - GDBJIT_SECT_debug_line, - GDBJIT_SECT__MAX -}; - -enum { - GDBJIT_SYM_UNDEF, - GDBJIT_SYM_FILE, - GDBJIT_SYM_FUNC, - GDBJIT_SYM__MAX -}; - -/* In-memory ELF object. */ -typedef struct GDBJITobj { - ELFheader hdr; /* ELF header. */ - ELFsectheader sect[GDBJIT_SECT__MAX]; /* ELF sections. */ - ELFsymbol sym[GDBJIT_SYM__MAX]; /* ELF symbol table. */ - uint8_t space[4096]; /* Space for various section data. */ -} GDBJITobj; - -/* Combined structure for GDB JIT entry and ELF object. */ -typedef struct GDBJITentryobj { - GDBJITentry entry; - size_t sz; - GDBJITobj obj; -} GDBJITentryobj; - -/* Template for in-memory ELF header. */ -static const ELFheader elfhdr_template = { - .emagic = { 0x7f, 'E', 'L', 'F' }, - .eclass = LJ_64 ? 2 : 1, - .eendian = LJ_ENDIAN_SELECT(1, 2), - .eversion = 1, -#if LJ_TARGET_LINUX - .eosabi = 0, /* Nope, it's not 3. */ -#elif defined(__FreeBSD__) - .eosabi = 9, -#elif defined(__NetBSD__) - .eosabi = 2, -#elif defined(__OpenBSD__) - .eosabi = 12, -#elif (defined(__sun__) && defined(__svr4__)) - .eosabi = 6, -#else - .eosabi = 0, -#endif - .eabiversion = 0, - .epad = { 0, 0, 0, 0, 0, 0, 0 }, - .type = 1, -#if LJ_TARGET_X86 - .machine = 3, -#elif LJ_TARGET_X64 - .machine = 62, -#elif LJ_TARGET_ARM - .machine = 40, -#elif LJ_TARGET_PPC - .machine = 20, -#elif LJ_TARGET_MIPS - .machine = 8, -#else -#error "Unsupported target architecture" -#endif - .version = 1, - .entry = 0, - .phofs = 0, - .shofs = offsetof(GDBJITobj, sect), - .flags = 0, - .ehsize = sizeof(ELFheader), - .phentsize = 0, - .phnum = 0, - .shentsize = sizeof(ELFsectheader), - .shnum = GDBJIT_SECT__MAX, - .shstridx = GDBJIT_SECT_shstrtab -}; - -/* -- In-memory ELF object generation ------------------------------------- */ - -/* Context for generating the ELF object for the GDB JIT API. */ -typedef struct GDBJITctx { - uint8_t *p; /* Pointer to next address in obj.space. */ - uint8_t *startp; /* Pointer to start address in obj.space. */ - GCtrace *T; /* Generate symbols for this trace. */ - uintptr_t mcaddr; /* Machine code address. */ - MSize szmcode; /* Size of machine code. */ - MSize spadjp; /* Stack adjustment for parent trace or interpreter. */ - MSize spadj; /* Stack adjustment for trace itself. */ - BCLine lineno; /* Starting line number. */ - const char *filename; /* Starting file name. */ - size_t objsize; /* Final size of ELF object. */ - GDBJITobj obj; /* In-memory ELF object. */ -} GDBJITctx; - -/* Add a zero-terminated string. */ -static uint32_t gdbjit_strz(GDBJITctx *ctx, const char *str) -{ - uint8_t *p = ctx->p; - uint32_t ofs = (uint32_t)(p - ctx->startp); - do { - *p++ = (uint8_t)*str; - } while (*str++); - ctx->p = p; - return ofs; -} - -/* Append a decimal number. */ -static void gdbjit_catnum(GDBJITctx *ctx, uint32_t n) -{ - if (n >= 10) { uint32_t m = n / 10; n = n % 10; gdbjit_catnum(ctx, m); } - *ctx->p++ = '0' + n; -} - -/* Add a ULEB128 value. */ -static void gdbjit_uleb128(GDBJITctx *ctx, uint32_t v) -{ - uint8_t *p = ctx->p; - for (; v >= 0x80; v >>= 7) - *p++ = (uint8_t)((v & 0x7f) | 0x80); - *p++ = (uint8_t)v; - ctx->p = p; -} - -/* Add a SLEB128 value. */ -static void gdbjit_sleb128(GDBJITctx *ctx, int32_t v) -{ - uint8_t *p = ctx->p; - for (; (uint32_t)(v+0x40) >= 0x80; v >>= 7) - *p++ = (uint8_t)((v & 0x7f) | 0x80); - *p++ = (uint8_t)(v & 0x7f); - ctx->p = p; -} - -/* Shortcuts to generate DWARF structures. */ -#define DB(x) (*p++ = (x)) -#define DI8(x) (*(int8_t *)p = (x), p++) -#define DU16(x) (*(uint16_t *)p = (x), p += 2) -#define DU32(x) (*(uint32_t *)p = (x), p += 4) -#define DADDR(x) (*(uintptr_t *)p = (x), p += sizeof(uintptr_t)) -#define DUV(x) (ctx->p = p, gdbjit_uleb128(ctx, (x)), p = ctx->p) -#define DSV(x) (ctx->p = p, gdbjit_sleb128(ctx, (x)), p = ctx->p) -#define DSTR(str) (ctx->p = p, gdbjit_strz(ctx, (str)), p = ctx->p) -#define DALIGNNOP(s) while ((uintptr_t)p & ((s)-1)) *p++ = DW_CFA_nop -#define DSECT(name, stmt) \ - { uint32_t *szp_##name = (uint32_t *)p; p += 4; stmt \ - *szp_##name = (uint32_t)((p-(uint8_t *)szp_##name)-4); } \ - -/* Initialize ELF section headers. */ -static void LJ_FASTCALL gdbjit_secthdr(GDBJITctx *ctx) -{ - ELFsectheader *sect; - - *ctx->p++ = '\0'; /* Empty string at start of string table. */ - -#define SECTDEF(id, tp, al) \ - sect = &ctx->obj.sect[GDBJIT_SECT_##id]; \ - sect->name = gdbjit_strz(ctx, "." #id); \ - sect->type = ELFSECT_TYPE_##tp; \ - sect->align = (al) - - SECTDEF(text, NOBITS, 16); - sect->flags = ELFSECT_FLAGS_ALLOC|ELFSECT_FLAGS_EXEC; - sect->addr = ctx->mcaddr; - sect->ofs = 0; - sect->size = ctx->szmcode; - - SECTDEF(eh_frame, PROGBITS, sizeof(uintptr_t)); - sect->flags = ELFSECT_FLAGS_ALLOC; - - SECTDEF(shstrtab, STRTAB, 1); - SECTDEF(strtab, STRTAB, 1); - - SECTDEF(symtab, SYMTAB, sizeof(uintptr_t)); - sect->ofs = offsetof(GDBJITobj, sym); - sect->size = sizeof(ctx->obj.sym); - sect->link = GDBJIT_SECT_strtab; - sect->entsize = sizeof(ELFsymbol); - sect->info = GDBJIT_SYM_FUNC; - - SECTDEF(debug_info, PROGBITS, 1); - SECTDEF(debug_abbrev, PROGBITS, 1); - SECTDEF(debug_line, PROGBITS, 1); - -#undef SECTDEF -} - -/* Initialize symbol table. */ -static void LJ_FASTCALL gdbjit_symtab(GDBJITctx *ctx) -{ - ELFsymbol *sym; - - *ctx->p++ = '\0'; /* Empty string at start of string table. */ - - sym = &ctx->obj.sym[GDBJIT_SYM_FILE]; - sym->name = gdbjit_strz(ctx, "JIT mcode"); - sym->sectidx = ELFSECT_IDX_ABS; - sym->info = ELFSYM_TYPE_FILE|ELFSYM_BIND_LOCAL; - - sym = &ctx->obj.sym[GDBJIT_SYM_FUNC]; - sym->name = gdbjit_strz(ctx, "TRACE_"); ctx->p--; - gdbjit_catnum(ctx, ctx->T->traceno); *ctx->p++ = '\0'; - sym->sectidx = GDBJIT_SECT_text; - sym->value = 0; - sym->size = ctx->szmcode; - sym->info = ELFSYM_TYPE_FUNC|ELFSYM_BIND_GLOBAL; -} - -/* Initialize .eh_frame section. */ -static void LJ_FASTCALL gdbjit_ehframe(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - uint8_t *framep = p; - - /* Emit DWARF EH CIE. */ - DSECT(CIE, - DU32(0); /* Offset to CIE itself. */ - DB(DW_CIE_VERSION); - DSTR("zR"); /* Augmentation. */ - DUV(1); /* Code alignment factor. */ - DSV(-(int32_t)sizeof(uintptr_t)); /* Data alignment factor. */ - DB(DW_REG_RA); /* Return address register. */ - DB(1); DB(DW_EH_PE_textrel|DW_EH_PE_udata4); /* Augmentation data. */ - DB(DW_CFA_def_cfa); DUV(DW_REG_SP); DUV(sizeof(uintptr_t)); -#if LJ_TARGET_PPC - DB(DW_CFA_offset_extended_sf); DB(DW_REG_RA); DSV(-1); -#else - DB(DW_CFA_offset|DW_REG_RA); DUV(1); -#endif - DALIGNNOP(sizeof(uintptr_t)); - ) - - /* Emit DWARF EH FDE. */ - DSECT(FDE, - DU32((uint32_t)(p-framep)); /* Offset to CIE. */ - DU32(0); /* Machine code offset relative to .text. */ - DU32(ctx->szmcode); /* Machine code length. */ - DB(0); /* Augmentation data. */ - /* Registers saved in CFRAME. */ -#if LJ_TARGET_X86 - DB(DW_CFA_offset|DW_REG_BP); DUV(2); - DB(DW_CFA_offset|DW_REG_DI); DUV(3); - DB(DW_CFA_offset|DW_REG_SI); DUV(4); - DB(DW_CFA_offset|DW_REG_BX); DUV(5); -#elif LJ_TARGET_X64 - DB(DW_CFA_offset|DW_REG_BP); DUV(2); - DB(DW_CFA_offset|DW_REG_BX); DUV(3); - DB(DW_CFA_offset|DW_REG_15); DUV(4); - DB(DW_CFA_offset|DW_REG_14); DUV(5); - /* Extra registers saved for JIT-compiled code. */ - DB(DW_CFA_offset|DW_REG_13); DUV(9); - DB(DW_CFA_offset|DW_REG_12); DUV(10); -#elif LJ_TARGET_ARM - { - int i; - for (i = 11; i >= 4; i--) { DB(DW_CFA_offset|i); DUV(2+(11-i)); } - } -#elif LJ_TARGET_PPC - { - int i; - DB(DW_CFA_offset_extended); DB(DW_REG_CR); DUV(55); - for (i = 14; i <= 31; i++) { - DB(DW_CFA_offset|i); DUV(37+(31-i)); - DB(DW_CFA_offset|32|i); DUV(2+2*(31-i)); - } - } -#elif LJ_TARGET_MIPS - { - int i; - DB(DW_CFA_offset|30); DUV(2); - for (i = 23; i >= 16; i--) { DB(DW_CFA_offset|i); DUV(26-i); } - for (i = 30; i >= 20; i -= 2) { DB(DW_CFA_offset|32|i); DUV(42-i); } - } -#else -#error "Unsupported target architecture" -#endif - if (ctx->spadjp != ctx->spadj) { /* Parent/interpreter stack frame size. */ - DB(DW_CFA_def_cfa_offset); DUV(ctx->spadjp); - DB(DW_CFA_advance_loc|1); /* Only an approximation. */ - } - DB(DW_CFA_def_cfa_offset); DUV(ctx->spadj); /* Trace stack frame size. */ - DALIGNNOP(sizeof(uintptr_t)); - ) - - ctx->p = p; -} - -/* Initialize .debug_info section. */ -static void LJ_FASTCALL gdbjit_debuginfo(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - - DSECT(info, - DU16(2); /* DWARF version. */ - DU32(0); /* Abbrev offset. */ - DB(sizeof(uintptr_t)); /* Pointer size. */ - - DUV(1); /* Abbrev #1: DW_TAG_compile_unit. */ - DSTR(ctx->filename); /* DW_AT_name. */ - DADDR(ctx->mcaddr); /* DW_AT_low_pc. */ - DADDR(ctx->mcaddr + ctx->szmcode); /* DW_AT_high_pc. */ - DU32(0); /* DW_AT_stmt_list. */ - ) - - ctx->p = p; -} - -/* Initialize .debug_abbrev section. */ -static void LJ_FASTCALL gdbjit_debugabbrev(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - - /* Abbrev #1: DW_TAG_compile_unit. */ - DUV(1); DUV(DW_TAG_compile_unit); - DB(DW_children_no); - DUV(DW_AT_name); DUV(DW_FORM_string); - DUV(DW_AT_low_pc); DUV(DW_FORM_addr); - DUV(DW_AT_high_pc); DUV(DW_FORM_addr); - DUV(DW_AT_stmt_list); DUV(DW_FORM_data4); - DB(0); DB(0); - - ctx->p = p; -} - -#define DLNE(op, s) (DB(DW_LNS_extended_op), DUV(1+(s)), DB((op))) - -/* Initialize .debug_line section. */ -static void LJ_FASTCALL gdbjit_debugline(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - - DSECT(line, - DU16(2); /* DWARF version. */ - DSECT(header, - DB(1); /* Minimum instruction length. */ - DB(1); /* is_stmt. */ - DI8(0); /* Line base for special opcodes. */ - DB(2); /* Line range for special opcodes. */ - DB(3+1); /* Opcode base at DW_LNS_advance_line+1. */ - DB(0); DB(1); DB(1); /* Standard opcode lengths. */ - /* Directory table. */ - DB(0); - /* File name table. */ - DSTR(ctx->filename); DUV(0); DUV(0); DUV(0); - DB(0); - ) - - DLNE(DW_LNE_set_address, sizeof(uintptr_t)); DADDR(ctx->mcaddr); - if (ctx->lineno) { - DB(DW_LNS_advance_line); DSV(ctx->lineno-1); - } - DB(DW_LNS_copy); - DB(DW_LNS_advance_pc); DUV(ctx->szmcode); - DLNE(DW_LNE_end_sequence, 0); - ) - - ctx->p = p; -} - -#undef DLNE - -/* Undef shortcuts. */ -#undef DB -#undef DI8 -#undef DU16 -#undef DU32 -#undef DADDR -#undef DUV -#undef DSV -#undef DSTR -#undef DALIGNNOP -#undef DSECT - -/* Type of a section initializer callback. */ -typedef void (LJ_FASTCALL *GDBJITinitf)(GDBJITctx *ctx); - -/* Call section initializer and set the section offset and size. */ -static void gdbjit_initsect(GDBJITctx *ctx, int sect, GDBJITinitf initf) -{ - ctx->startp = ctx->p; - ctx->obj.sect[sect].ofs = (uintptr_t)((char *)ctx->p - (char *)&ctx->obj); - initf(ctx); - ctx->obj.sect[sect].size = (uintptr_t)(ctx->p - ctx->startp); -} - -#define SECTALIGN(p, a) \ - ((p) = (uint8_t *)(((uintptr_t)(p) + ((a)-1)) & ~(uintptr_t)((a)-1))) - -/* Build in-memory ELF object. */ -static void gdbjit_buildobj(GDBJITctx *ctx) -{ - GDBJITobj *obj = &ctx->obj; - /* Fill in ELF header and clear structures. */ - memcpy(&obj->hdr, &elfhdr_template, sizeof(ELFheader)); - memset(&obj->sect, 0, sizeof(ELFsectheader)*GDBJIT_SECT__MAX); - memset(&obj->sym, 0, sizeof(ELFsymbol)*GDBJIT_SYM__MAX); - /* Initialize sections. */ - ctx->p = obj->space; - gdbjit_initsect(ctx, GDBJIT_SECT_shstrtab, gdbjit_secthdr); - gdbjit_initsect(ctx, GDBJIT_SECT_strtab, gdbjit_symtab); - gdbjit_initsect(ctx, GDBJIT_SECT_debug_info, gdbjit_debuginfo); - gdbjit_initsect(ctx, GDBJIT_SECT_debug_abbrev, gdbjit_debugabbrev); - gdbjit_initsect(ctx, GDBJIT_SECT_debug_line, gdbjit_debugline); - SECTALIGN(ctx->p, sizeof(uintptr_t)); - gdbjit_initsect(ctx, GDBJIT_SECT_eh_frame, gdbjit_ehframe); - ctx->objsize = (size_t)((char *)ctx->p - (char *)obj); - lua_assert(ctx->objsize < sizeof(GDBJITobj)); -} - -#undef SECTALIGN - -/* -- Interface to GDB JIT API -------------------------------------------- */ - -/* Add new entry to GDB JIT symbol chain. */ -static void gdbjit_newentry(lua_State *L, GDBJITctx *ctx) -{ - /* Allocate memory for GDB JIT entry and ELF object. */ - MSize sz = (MSize)(sizeof(GDBJITentryobj) - sizeof(GDBJITobj) + ctx->objsize); - GDBJITentryobj *eo = lj_mem_newt(L, sz, GDBJITentryobj); - memcpy(&eo->obj, &ctx->obj, ctx->objsize); /* Copy ELF object. */ - eo->sz = sz; - ctx->T->gdbjit_entry = (void *)eo; - /* Link new entry to chain and register it. */ - eo->entry.prev_entry = NULL; - eo->entry.next_entry = __jit_debug_descriptor.first_entry; - if (eo->entry.next_entry) - eo->entry.next_entry->prev_entry = &eo->entry; - eo->entry.symfile_addr = (const char *)&eo->obj; - eo->entry.symfile_size = ctx->objsize; - __jit_debug_descriptor.first_entry = &eo->entry; - __jit_debug_descriptor.relevant_entry = &eo->entry; - __jit_debug_descriptor.action_flag = GDBJIT_REGISTER; - __jit_debug_register_code(); -} - -/* Add debug info for newly compiled trace and notify GDB. */ -void lj_gdbjit_addtrace(jit_State *J, GCtrace *T) -{ - GDBJITctx ctx; - GCproto *pt = &gcref(T->startpt)->pt; - TraceNo parent = T->ir[REF_BASE].op1; - const BCIns *startpc = mref(T->startpc, const BCIns); - ctx.T = T; - ctx.mcaddr = (uintptr_t)T->mcode; - ctx.szmcode = T->szmcode; - ctx.spadjp = CFRAME_SIZE_JIT + - (MSize)(parent ? traceref(J, parent)->spadjust : 0); - ctx.spadj = CFRAME_SIZE_JIT + T->spadjust; - lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc); - ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); - ctx.filename = proto_chunknamestr(pt); - if (*ctx.filename == '@' || *ctx.filename == '=') - ctx.filename++; - else - ctx.filename = "(string)"; - gdbjit_buildobj(&ctx); - gdbjit_newentry(J->L, &ctx); -} - -/* Delete debug info for trace and notify GDB. */ -void lj_gdbjit_deltrace(jit_State *J, GCtrace *T) -{ - GDBJITentryobj *eo = (GDBJITentryobj *)T->gdbjit_entry; - if (eo) { - if (eo->entry.prev_entry) - eo->entry.prev_entry->next_entry = eo->entry.next_entry; - else - __jit_debug_descriptor.first_entry = eo->entry.next_entry; - if (eo->entry.next_entry) - eo->entry.next_entry->prev_entry = eo->entry.prev_entry; - __jit_debug_descriptor.relevant_entry = &eo->entry; - __jit_debug_descriptor.action_flag = GDBJIT_UNREGISTER; - __jit_debug_register_code(); - lj_mem_free(J2G(J), eo, eo->sz); - } -} - -#endif -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_gdbjit.h b/third-party/LuaJIT-2.0.2/src/lj_gdbjit.h deleted file mode 100644 index 481cb22a36..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_gdbjit.h +++ /dev/null @@ -1,22 +0,0 @@ -/* -** Client for the GDB JIT API. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_GDBJIT_H -#define _LJ_GDBJIT_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT && defined(LUAJIT_USE_GDBJIT) - -LJ_FUNC void lj_gdbjit_addtrace(jit_State *J, GCtrace *T); -LJ_FUNC void lj_gdbjit_deltrace(jit_State *J, GCtrace *T); - -#else -#define lj_gdbjit_addtrace(J, T) UNUSED(T) -#define lj_gdbjit_deltrace(J, T) UNUSED(T) -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ir.c b/third-party/LuaJIT-2.0.2/src/lj_ir.c deleted file mode 100644 index e1a59105cf..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ir.c +++ /dev/null @@ -1,501 +0,0 @@ -/* -** SSA IR (Intermediate Representation) emitter. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_ir_c -#define LUA_CORE - -/* For pointers to libc/libm functions. */ -#include -#include - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_carith.h" -#endif -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_lib.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* -- IR tables ----------------------------------------------------------- */ - -/* IR instruction modes. */ -LJ_DATADEF const uint8_t lj_ir_mode[IR__MAX+1] = { -IRDEF(IRMODE) - 0 -}; - -/* IR type sizes. */ -LJ_DATADEF const uint8_t lj_ir_type_size[IRT__MAX+1] = { -#define IRTSIZE(name, size) size, -IRTDEF(IRTSIZE) -#undef IRTSIZE - 0 -}; - -/* C call info for CALL* instructions. */ -LJ_DATADEF const CCallInfo lj_ir_callinfo[] = { -#define IRCALLCI(cond, name, nargs, kind, type, flags) \ - { (ASMFunction)IRCALLCOND_##cond(name), \ - (nargs)|(CCI_CALL_##kind)|(IRT_##type<irbuf + J->irbotlim; - MSize szins = J->irtoplim - J->irbotlim; - if (szins) { - baseir = (IRIns *)lj_mem_realloc(J->L, baseir, szins*sizeof(IRIns), - 2*szins*sizeof(IRIns)); - J->irtoplim = J->irbotlim + 2*szins; - } else { - baseir = (IRIns *)lj_mem_realloc(J->L, NULL, 0, LJ_MIN_IRSZ*sizeof(IRIns)); - J->irbotlim = REF_BASE - LJ_MIN_IRSZ/4; - J->irtoplim = J->irbotlim + LJ_MIN_IRSZ; - } - J->cur.ir = J->irbuf = baseir - J->irbotlim; -} - -/* Grow IR buffer at the bottom or shift it up. */ -static void lj_ir_growbot(jit_State *J) -{ - IRIns *baseir = J->irbuf + J->irbotlim; - MSize szins = J->irtoplim - J->irbotlim; - lua_assert(szins != 0); - lua_assert(J->cur.nk == J->irbotlim); - if (J->cur.nins + (szins >> 1) < J->irtoplim) { - /* More than half of the buffer is free on top: shift up by a quarter. */ - MSize ofs = szins >> 2; - memmove(baseir + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns)); - J->irbotlim -= ofs; - J->irtoplim -= ofs; - J->cur.ir = J->irbuf = baseir - J->irbotlim; - } else { - /* Double the buffer size, but split the growth amongst top/bottom. */ - IRIns *newbase = lj_mem_newt(J->L, 2*szins*sizeof(IRIns), IRIns); - MSize ofs = szins >= 256 ? 128 : (szins >> 1); /* Limit bottom growth. */ - memcpy(newbase + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns)); - lj_mem_free(G(J->L), baseir, szins*sizeof(IRIns)); - J->irbotlim -= ofs; - J->irtoplim = J->irbotlim + 2*szins; - J->cur.ir = J->irbuf = newbase - J->irbotlim; - } -} - -/* Emit IR without any optimizations. */ -TRef LJ_FASTCALL lj_ir_emit(jit_State *J) -{ - IRRef ref = lj_ir_nextins(J); - IRIns *ir = IR(ref); - IROp op = fins->o; - ir->prev = J->chain[op]; - J->chain[op] = (IRRef1)ref; - ir->o = op; - ir->op1 = fins->op1; - ir->op2 = fins->op2; - J->guardemit.irt |= fins->t.irt; - return TREF(ref, irt_t((ir->t = fins->t))); -} - -/* Emit call to a C function. */ -TRef lj_ir_call(jit_State *J, IRCallID id, ...) -{ - const CCallInfo *ci = &lj_ir_callinfo[id]; - uint32_t n = CCI_NARGS(ci); - TRef tr = TREF_NIL; - va_list argp; - va_start(argp, id); - if ((ci->flags & CCI_L)) n--; - if (n > 0) - tr = va_arg(argp, IRRef); - while (n-- > 1) - tr = emitir(IRT(IR_CARG, IRT_NIL), tr, va_arg(argp, IRRef)); - va_end(argp); - if (CCI_OP(ci) == IR_CALLS) - J->needsnap = 1; /* Need snapshot after call with side effect. */ - return emitir(CCI_OPTYPE(ci), tr, id); -} - -/* -- Interning of constants ---------------------------------------------- */ - -/* -** IR instructions for constants are kept between J->cur.nk >= ref < REF_BIAS. -** They are chained like all other instructions, but grow downwards. -** The are interned (like strings in the VM) to facilitate reference -** comparisons. The same constant must get the same reference. -*/ - -/* Get ref of next IR constant and optionally grow IR. -** Note: this may invalidate all IRIns *! -*/ -static LJ_AINLINE IRRef ir_nextk(jit_State *J) -{ - IRRef ref = J->cur.nk; - if (LJ_UNLIKELY(ref <= J->irbotlim)) lj_ir_growbot(J); - J->cur.nk = --ref; - return ref; -} - -/* Intern int32_t constant. */ -TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - for (ref = J->chain[IR_KINT]; ref; ref = cir[ref].prev) - if (cir[ref].i == k) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - ir->i = k; - ir->t.irt = IRT_INT; - ir->o = IR_KINT; - ir->prev = J->chain[IR_KINT]; - J->chain[IR_KINT] = (IRRef1)ref; -found: - return TREF(ref, IRT_INT); -} - -/* The MRef inside the KNUM/KINT64 IR instructions holds the address of the -** 64 bit constant. The constants themselves are stored in a chained array -** and shared across traces. -** -** Rationale for choosing this data structure: -** - The address of the constants is embedded in the generated machine code -** and must never move. A resizable array or hash table wouldn't work. -** - Most apps need very few non-32 bit integer constants (less than a dozen). -** - Linear search is hard to beat in terms of speed and low complexity. -*/ -typedef struct K64Array { - MRef next; /* Pointer to next list. */ - MSize numk; /* Number of used elements in this array. */ - TValue k[LJ_MIN_K64SZ]; /* Array of constants. */ -} K64Array; - -/* Free all chained arrays. */ -void lj_ir_k64_freeall(jit_State *J) -{ - K64Array *k; - for (k = mref(J->k64, K64Array); k; ) { - K64Array *next = mref(k->next, K64Array); - lj_mem_free(J2G(J), k, sizeof(K64Array)); - k = next; - } -} - -/* Find 64 bit constant in chained array or add it. */ -cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64) -{ - K64Array *k, *kp = NULL; - TValue *ntv; - MSize idx; - /* Search for the constant in the whole chain of arrays. */ - for (k = mref(J->k64, K64Array); k; k = mref(k->next, K64Array)) { - kp = k; /* Remember previous element in list. */ - for (idx = 0; idx < k->numk; idx++) { /* Search one array. */ - TValue *tv = &k->k[idx]; - if (tv->u64 == u64) /* Needed for +-0/NaN/absmask. */ - return tv; - } - } - /* Constant was not found, need to add it. */ - if (!(kp && kp->numk < LJ_MIN_K64SZ)) { /* Allocate a new array. */ - K64Array *kn = lj_mem_newt(J->L, sizeof(K64Array), K64Array); - setmref(kn->next, NULL); - kn->numk = 0; - if (kp) - setmref(kp->next, kn); /* Chain to the end of the list. */ - else - setmref(J->k64, kn); /* Link first array. */ - kp = kn; - } - ntv = &kp->k[kp->numk++]; /* Add to current array. */ - ntv->u64 = u64; - return ntv; -} - -/* Intern 64 bit constant, given by its address. */ -TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64; - for (ref = J->chain[op]; ref; ref = cir[ref].prev) - if (ir_k64(&cir[ref]) == tv) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - lua_assert(checkptr32(tv)); - setmref(ir->ptr, tv); - ir->t.irt = t; - ir->o = op; - ir->prev = J->chain[op]; - J->chain[op] = (IRRef1)ref; -found: - return TREF(ref, t); -} - -/* Intern FP constant, given by its 64 bit pattern. */ -TRef lj_ir_knum_u64(jit_State *J, uint64_t u64) -{ - return lj_ir_k64(J, IR_KNUM, lj_ir_k64_find(J, u64)); -} - -/* Intern 64 bit integer constant. */ -TRef lj_ir_kint64(jit_State *J, uint64_t u64) -{ - return lj_ir_k64(J, IR_KINT64, lj_ir_k64_find(J, u64)); -} - -/* Check whether a number is int and return it. -0 is NOT considered an int. */ -static int numistrueint(lua_Number n, int32_t *kp) -{ - int32_t k = lj_num2int(n); - if (n == (lua_Number)k) { - if (kp) *kp = k; - if (k == 0) { /* Special check for -0. */ - TValue tv; - setnumV(&tv, n); - if (tv.u32.hi != 0) - return 0; - } - return 1; - } - return 0; -} - -/* Intern number as int32_t constant if possible, otherwise as FP constant. */ -TRef lj_ir_knumint(jit_State *J, lua_Number n) -{ - int32_t k; - if (numistrueint(n, &k)) - return lj_ir_kint(J, k); - else - return lj_ir_knum(J, n); -} - -/* Intern GC object "constant". */ -TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - lua_assert(!isdead(J2G(J), o)); - for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev) - if (ir_kgc(&cir[ref]) == o) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - /* NOBARRIER: Current trace is a GC root. */ - setgcref(ir->gcr, o); - ir->t.irt = (uint8_t)t; - ir->o = IR_KGC; - ir->prev = J->chain[IR_KGC]; - J->chain[IR_KGC] = (IRRef1)ref; -found: - return TREF(ref, t); -} - -/* Intern 32 bit pointer constant. */ -TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - lua_assert((void *)(intptr_t)i32ptr(ptr) == ptr); - for (ref = J->chain[op]; ref; ref = cir[ref].prev) - if (mref(cir[ref].ptr, void) == ptr) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - setmref(ir->ptr, ptr); - ir->t.irt = IRT_P32; - ir->o = op; - ir->prev = J->chain[op]; - J->chain[op] = (IRRef1)ref; -found: - return TREF(ref, IRT_P32); -} - -/* Intern typed NULL constant. */ -TRef lj_ir_knull(jit_State *J, IRType t) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - for (ref = J->chain[IR_KNULL]; ref; ref = cir[ref].prev) - if (irt_t(cir[ref].t) == t) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - ir->i = 0; - ir->t.irt = (uint8_t)t; - ir->o = IR_KNULL; - ir->prev = J->chain[IR_KNULL]; - J->chain[IR_KNULL] = (IRRef1)ref; -found: - return TREF(ref, t); -} - -/* Intern key slot. */ -TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot); - IRRef ref; - /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */ - lua_assert(tref_isk(key) && slot == (IRRef)(IRRef1)slot); - for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev) - if (cir[ref].op12 == op12) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - ir->op12 = op12; - ir->t.irt = IRT_P32; - ir->o = IR_KSLOT; - ir->prev = J->chain[IR_KSLOT]; - J->chain[IR_KSLOT] = (IRRef1)ref; -found: - return TREF(ref, IRT_P32); -} - -/* -- Access to IR constants ---------------------------------------------- */ - -/* Copy value of IR constant. */ -void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir) -{ - UNUSED(L); - lua_assert(ir->o != IR_KSLOT); /* Common mistake. */ - switch (ir->o) { - case IR_KPRI: setitype(tv, irt_toitype(ir->t)); break; - case IR_KINT: setintV(tv, ir->i); break; - case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break; - case IR_KPTR: case IR_KKPTR: case IR_KNULL: - setlightudV(tv, mref(ir->ptr, void)); - break; - case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break; -#if LJ_HASFFI - case IR_KINT64: { - GCcdata *cd = lj_cdata_new_(L, CTID_INT64, 8); - *(uint64_t *)cdataptr(cd) = ir_kint64(ir)->u64; - setcdataV(L, tv, cd); - break; - } -#endif - default: lua_assert(0); break; - } -} - -/* -- Convert IR operand types -------------------------------------------- */ - -/* Convert from string to number. */ -TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr) -{ - if (!tref_isnumber(tr)) { - if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - else - lj_trace_err(J, LJ_TRERR_BADTYPE); - } - return tr; -} - -/* Convert from integer or string to number. */ -TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr) -{ - if (!tref_isnum(tr)) { - if (tref_isinteger(tr)) - tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); - else if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - else - lj_trace_err(J, LJ_TRERR_BADTYPE); - } - return tr; -} - -/* Convert from integer or number to string. */ -TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr) -{ - if (!tref_isstr(tr)) { - if (!tref_isnumber(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - tr = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0); - } - return tr; -} - -/* -- Miscellaneous IR ops ------------------------------------------------ */ - -/* Evaluate numeric comparison. */ -int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op) -{ - switch (op) { - case IR_EQ: return (a == b); - case IR_NE: return (a != b); - case IR_LT: return (a < b); - case IR_GE: return (a >= b); - case IR_LE: return (a <= b); - case IR_GT: return (a > b); - case IR_ULT: return !(a >= b); - case IR_UGE: return !(a < b); - case IR_ULE: return !(a > b); - case IR_UGT: return !(a <= b); - default: lua_assert(0); return 0; - } -} - -/* Evaluate string comparison. */ -int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op) -{ - int res = lj_str_cmp(a, b); - switch (op) { - case IR_LT: return (res < 0); - case IR_GE: return (res >= 0); - case IR_LE: return (res <= 0); - case IR_GT: return (res > 0); - default: lua_assert(0); return 0; - } -} - -/* Rollback IR to previous state. */ -void lj_ir_rollback(jit_State *J, IRRef ref) -{ - IRRef nins = J->cur.nins; - while (nins > ref) { - IRIns *ir; - nins--; - ir = IR(nins); - J->chain[ir->o] = ir->prev; - } - J->cur.nins = nins; -} - -#undef IR -#undef fins -#undef emitir - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ir.h b/third-party/LuaJIT-2.0.2/src/lj_ir.h deleted file mode 100644 index a98243253f..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ir.h +++ /dev/null @@ -1,551 +0,0 @@ -/* -** SSA IR (Intermediate Representation) format. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_IR_H -#define _LJ_IR_H - -#include "lj_obj.h" - -/* -- IR instructions ----------------------------------------------------- */ - -/* IR instruction definition. Order matters, see below. ORDER IR */ -#define IRDEF(_) \ - /* Guarded assertions. */ \ - /* Must be properly aligned to flip opposites (^1) and (un)ordered (^4). */ \ - _(LT, N , ref, ref) \ - _(GE, N , ref, ref) \ - _(LE, N , ref, ref) \ - _(GT, N , ref, ref) \ - \ - _(ULT, N , ref, ref) \ - _(UGE, N , ref, ref) \ - _(ULE, N , ref, ref) \ - _(UGT, N , ref, ref) \ - \ - _(EQ, C , ref, ref) \ - _(NE, C , ref, ref) \ - \ - _(ABC, N , ref, ref) \ - _(RETF, S , ref, ref) \ - \ - /* Miscellaneous ops. */ \ - _(NOP, N , ___, ___) \ - _(BASE, N , lit, lit) \ - _(PVAL, N , lit, ___) \ - _(GCSTEP, S , ___, ___) \ - _(HIOP, S , ref, ref) \ - _(LOOP, S , ___, ___) \ - _(USE, S , ref, ___) \ - _(PHI, S , ref, ref) \ - _(RENAME, S , ref, lit) \ - \ - /* Constants. */ \ - _(KPRI, N , ___, ___) \ - _(KINT, N , cst, ___) \ - _(KGC, N , cst, ___) \ - _(KPTR, N , cst, ___) \ - _(KKPTR, N , cst, ___) \ - _(KNULL, N , cst, ___) \ - _(KNUM, N , cst, ___) \ - _(KINT64, N , cst, ___) \ - _(KSLOT, N , ref, lit) \ - \ - /* Bit ops. */ \ - _(BNOT, N , ref, ___) \ - _(BSWAP, N , ref, ___) \ - _(BAND, C , ref, ref) \ - _(BOR, C , ref, ref) \ - _(BXOR, C , ref, ref) \ - _(BSHL, N , ref, ref) \ - _(BSHR, N , ref, ref) \ - _(BSAR, N , ref, ref) \ - _(BROL, N , ref, ref) \ - _(BROR, N , ref, ref) \ - \ - /* Arithmetic ops. ORDER ARITH */ \ - _(ADD, C , ref, ref) \ - _(SUB, N , ref, ref) \ - _(MUL, C , ref, ref) \ - _(DIV, N , ref, ref) \ - _(MOD, N , ref, ref) \ - _(POW, N , ref, ref) \ - _(NEG, N , ref, ref) \ - \ - _(ABS, N , ref, ref) \ - _(ATAN2, N , ref, ref) \ - _(LDEXP, N , ref, ref) \ - _(MIN, C , ref, ref) \ - _(MAX, C , ref, ref) \ - _(FPMATH, N , ref, lit) \ - \ - /* Overflow-checking arithmetic ops. */ \ - _(ADDOV, CW, ref, ref) \ - _(SUBOV, NW, ref, ref) \ - _(MULOV, CW, ref, ref) \ - \ - /* Memory ops. A = array, H = hash, U = upvalue, F = field, S = stack. */ \ - \ - /* Memory references. */ \ - _(AREF, R , ref, ref) \ - _(HREFK, R , ref, ref) \ - _(HREF, L , ref, ref) \ - _(NEWREF, S , ref, ref) \ - _(UREFO, LW, ref, lit) \ - _(UREFC, LW, ref, lit) \ - _(FREF, R , ref, lit) \ - _(STRREF, N , ref, ref) \ - \ - /* Loads and Stores. These must be in the same order. */ \ - _(ALOAD, L , ref, ___) \ - _(HLOAD, L , ref, ___) \ - _(ULOAD, L , ref, ___) \ - _(FLOAD, L , ref, lit) \ - _(XLOAD, L , ref, lit) \ - _(SLOAD, L , lit, lit) \ - _(VLOAD, L , ref, ___) \ - \ - _(ASTORE, S , ref, ref) \ - _(HSTORE, S , ref, ref) \ - _(USTORE, S , ref, ref) \ - _(FSTORE, S , ref, ref) \ - _(XSTORE, S , ref, ref) \ - \ - /* Allocations. */ \ - _(SNEW, N , ref, ref) /* CSE is ok, not marked as A. */ \ - _(XSNEW, A , ref, ref) \ - _(TNEW, AW, lit, lit) \ - _(TDUP, AW, ref, ___) \ - _(CNEW, AW, ref, ref) \ - _(CNEWI, NW, ref, ref) /* CSE is ok, not marked as A. */ \ - \ - /* Barriers. */ \ - _(TBAR, S , ref, ___) \ - _(OBAR, S , ref, ref) \ - _(XBAR, S , ___, ___) \ - \ - /* Type conversions. */ \ - _(CONV, NW, ref, lit) \ - _(TOBIT, N , ref, ref) \ - _(TOSTR, N , ref, ___) \ - _(STRTO, N , ref, ___) \ - \ - /* Calls. */ \ - _(CALLN, N , ref, lit) \ - _(CALLL, L , ref, lit) \ - _(CALLS, S , ref, lit) \ - _(CALLXS, S , ref, ref) \ - _(CARG, N , ref, ref) \ - \ - /* End of list. */ - -/* IR opcodes (max. 256). */ -typedef enum { -#define IRENUM(name, m, m1, m2) IR_##name, -IRDEF(IRENUM) -#undef IRENUM - IR__MAX -} IROp; - -/* Stored opcode. */ -typedef uint8_t IROp1; - -LJ_STATIC_ASSERT(((int)IR_EQ^1) == (int)IR_NE); -LJ_STATIC_ASSERT(((int)IR_LT^1) == (int)IR_GE); -LJ_STATIC_ASSERT(((int)IR_LE^1) == (int)IR_GT); -LJ_STATIC_ASSERT(((int)IR_LT^3) == (int)IR_GT); -LJ_STATIC_ASSERT(((int)IR_LT^4) == (int)IR_ULT); - -/* Delta between xLOAD and xSTORE. */ -#define IRDELTA_L2S ((int)IR_ASTORE - (int)IR_ALOAD) - -LJ_STATIC_ASSERT((int)IR_HLOAD + IRDELTA_L2S == (int)IR_HSTORE); -LJ_STATIC_ASSERT((int)IR_ULOAD + IRDELTA_L2S == (int)IR_USTORE); -LJ_STATIC_ASSERT((int)IR_FLOAD + IRDELTA_L2S == (int)IR_FSTORE); -LJ_STATIC_ASSERT((int)IR_XLOAD + IRDELTA_L2S == (int)IR_XSTORE); - -/* -- Named IR literals --------------------------------------------------- */ - -/* FPMATH sub-functions. ORDER FPM. */ -#define IRFPMDEF(_) \ - _(FLOOR) _(CEIL) _(TRUNC) /* Must be first and in this order. */ \ - _(SQRT) _(EXP) _(EXP2) _(LOG) _(LOG2) _(LOG10) \ - _(SIN) _(COS) _(TAN) \ - _(OTHER) - -typedef enum { -#define FPMENUM(name) IRFPM_##name, -IRFPMDEF(FPMENUM) -#undef FPMENUM - IRFPM__MAX -} IRFPMathOp; - -/* FLOAD fields. */ -#define IRFLDEF(_) \ - _(STR_LEN, offsetof(GCstr, len)) \ - _(FUNC_ENV, offsetof(GCfunc, l.env)) \ - _(FUNC_PC, offsetof(GCfunc, l.pc)) \ - _(TAB_META, offsetof(GCtab, metatable)) \ - _(TAB_ARRAY, offsetof(GCtab, array)) \ - _(TAB_NODE, offsetof(GCtab, node)) \ - _(TAB_ASIZE, offsetof(GCtab, asize)) \ - _(TAB_HMASK, offsetof(GCtab, hmask)) \ - _(TAB_NOMM, offsetof(GCtab, nomm)) \ - _(UDATA_META, offsetof(GCudata, metatable)) \ - _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \ - _(UDATA_FILE, sizeof(GCudata)) \ - _(CDATA_CTYPEID, offsetof(GCcdata, ctypeid)) \ - _(CDATA_PTR, sizeof(GCcdata)) \ - _(CDATA_INT, sizeof(GCcdata)) \ - _(CDATA_INT64, sizeof(GCcdata)) \ - _(CDATA_INT64_4, sizeof(GCcdata) + 4) - -typedef enum { -#define FLENUM(name, ofs) IRFL_##name, -IRFLDEF(FLENUM) -#undef FLENUM - IRFL__MAX -} IRFieldID; - -/* SLOAD mode bits, stored in op2. */ -#define IRSLOAD_PARENT 0x01 /* Coalesce with parent trace. */ -#define IRSLOAD_FRAME 0x02 /* Load hiword of frame. */ -#define IRSLOAD_TYPECHECK 0x04 /* Needs type check. */ -#define IRSLOAD_CONVERT 0x08 /* Number to integer conversion. */ -#define IRSLOAD_READONLY 0x10 /* Read-only, omit slot store. */ -#define IRSLOAD_INHERIT 0x20 /* Inherited by exits/side traces. */ - -/* XLOAD mode, stored in op2. */ -#define IRXLOAD_READONLY 1 /* Load from read-only data. */ -#define IRXLOAD_VOLATILE 2 /* Load from volatile data. */ -#define IRXLOAD_UNALIGNED 4 /* Unaligned load. */ - -/* CONV mode, stored in op2. */ -#define IRCONV_SRCMASK 0x001f /* Source IRType. */ -#define IRCONV_DSTMASK 0x03e0 /* Dest. IRType (also in ir->t). */ -#define IRCONV_DSH 5 -#define IRCONV_NUM_INT ((IRT_NUM<>2)&3)) -#define irm_iscomm(m) ((m) & IRM_C) -#define irm_kind(m) ((m) & IRM_S) - -#define IRMODE(name, m, m1, m2) (((IRM##m1)|((IRM##m2)<<2)|(IRM_##m))^IRM_W), - -LJ_DATA const uint8_t lj_ir_mode[IR__MAX+1]; - -/* -- IR instruction types ------------------------------------------------ */ - -/* Map of itypes to non-negative numbers. ORDER LJ_T. -** LJ_TUPVAL/LJ_TTRACE never appear in a TValue. Use these itypes for -** IRT_P32 and IRT_P64, which never escape the IR. -** The various integers are only used in the IR and can only escape to -** a TValue after implicit or explicit conversion. Their types must be -** contiguous and next to IRT_NUM (see the typerange macros below). -*/ -#define IRTDEF(_) \ - _(NIL, 4) _(FALSE, 4) _(TRUE, 4) _(LIGHTUD, LJ_64 ? 8 : 4) _(STR, 4) \ - _(P32, 4) _(THREAD, 4) _(PROTO, 4) _(FUNC, 4) _(P64, 8) _(CDATA, 4) \ - _(TAB, 4) _(UDATA, 4) \ - _(FLOAT, 4) _(NUM, 8) _(I8, 1) _(U8, 1) _(I16, 2) _(U16, 2) \ - _(INT, 4) _(U32, 4) _(I64, 8) _(U64, 8) \ - _(SOFTFP, 4) /* There is room for 9 more types. */ - -/* IR result type and flags (8 bit). */ -typedef enum { -#define IRTENUM(name, size) IRT_##name, -IRTDEF(IRTENUM) -#undef IRTENUM - IRT__MAX, - - /* Native pointer type and the corresponding integer type. */ - IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32, - IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT, - IRT_UINTP = LJ_64 ? IRT_U64 : IRT_U32, - - /* Additional flags. */ - IRT_MARK = 0x20, /* Marker for misc. purposes. */ - IRT_ISPHI = 0x40, /* Instruction is left or right PHI operand. */ - IRT_GUARD = 0x80, /* Instruction is a guard. */ - - /* Masks. */ - IRT_TYPE = 0x1f, - IRT_T = 0xff -} IRType; - -#define irtype_ispri(irt) ((uint32_t)(irt) <= IRT_TRUE) - -/* Stored IRType. */ -typedef struct IRType1 { uint8_t irt; } IRType1; - -#define IRT(o, t) ((uint32_t)(((o)<<8) | (t))) -#define IRTI(o) (IRT((o), IRT_INT)) -#define IRTN(o) (IRT((o), IRT_NUM)) -#define IRTG(o, t) (IRT((o), IRT_GUARD|(t))) -#define IRTGI(o) (IRT((o), IRT_GUARD|IRT_INT)) - -#define irt_t(t) ((IRType)(t).irt) -#define irt_type(t) ((IRType)((t).irt & IRT_TYPE)) -#define irt_sametype(t1, t2) ((((t1).irt ^ (t2).irt) & IRT_TYPE) == 0) -#define irt_typerange(t, first, last) \ - ((uint32_t)((t).irt & IRT_TYPE) - (uint32_t)(first) <= (uint32_t)(last-first)) - -#define irt_isnil(t) (irt_type(t) == IRT_NIL) -#define irt_ispri(t) ((uint32_t)irt_type(t) <= IRT_TRUE) -#define irt_islightud(t) (irt_type(t) == IRT_LIGHTUD) -#define irt_isstr(t) (irt_type(t) == IRT_STR) -#define irt_istab(t) (irt_type(t) == IRT_TAB) -#define irt_iscdata(t) (irt_type(t) == IRT_CDATA) -#define irt_isfloat(t) (irt_type(t) == IRT_FLOAT) -#define irt_isnum(t) (irt_type(t) == IRT_NUM) -#define irt_isint(t) (irt_type(t) == IRT_INT) -#define irt_isi8(t) (irt_type(t) == IRT_I8) -#define irt_isu8(t) (irt_type(t) == IRT_U8) -#define irt_isi16(t) (irt_type(t) == IRT_I16) -#define irt_isu16(t) (irt_type(t) == IRT_U16) -#define irt_isu32(t) (irt_type(t) == IRT_U32) -#define irt_isi64(t) (irt_type(t) == IRT_I64) -#define irt_isu64(t) (irt_type(t) == IRT_U64) - -#define irt_isfp(t) (irt_isnum(t) || irt_isfloat(t)) -#define irt_isinteger(t) (irt_typerange((t), IRT_I8, IRT_INT)) -#define irt_isgcv(t) (irt_typerange((t), IRT_STR, IRT_UDATA)) -#define irt_isaddr(t) (irt_typerange((t), IRT_LIGHTUD, IRT_UDATA)) -#define irt_isint64(t) (irt_typerange((t), IRT_I64, IRT_U64)) - -#if LJ_64 -#define IRT_IS64 \ - ((1u<> irt_type(t)) & 1) -#define irt_is64orfp(t) (((IRT_IS64|(1u<>irt_type(t)) & 1) - -#define irt_size(t) (lj_ir_type_size[irt_t((t))]) - -LJ_DATA const uint8_t lj_ir_type_size[]; - -static LJ_AINLINE IRType itype2irt(const TValue *tv) -{ - if (tvisint(tv)) - return IRT_INT; - else if (tvisnum(tv)) - return IRT_NUM; -#if LJ_64 - else if (tvislightud(tv)) - return IRT_LIGHTUD; -#endif - else - return (IRType)~itype(tv); -} - -static LJ_AINLINE uint32_t irt_toitype_(IRType t) -{ - lua_assert(!LJ_64 || t != IRT_LIGHTUD); - if (LJ_DUALNUM && t > IRT_NUM) { - return LJ_TISNUM; - } else { - lua_assert(t <= IRT_NUM); - return ~(uint32_t)t; - } -} - -#define irt_toitype(t) irt_toitype_(irt_type((t))) - -#define irt_isguard(t) ((t).irt & IRT_GUARD) -#define irt_ismarked(t) ((t).irt & IRT_MARK) -#define irt_setmark(t) ((t).irt |= IRT_MARK) -#define irt_clearmark(t) ((t).irt &= ~IRT_MARK) -#define irt_isphi(t) ((t).irt & IRT_ISPHI) -#define irt_setphi(t) ((t).irt |= IRT_ISPHI) -#define irt_clearphi(t) ((t).irt &= ~IRT_ISPHI) - -/* Stored combined IR opcode and type. */ -typedef uint16_t IROpT; - -/* -- IR references ------------------------------------------------------- */ - -/* IR references. */ -typedef uint16_t IRRef1; /* One stored reference. */ -typedef uint32_t IRRef2; /* Two stored references. */ -typedef uint32_t IRRef; /* Used to pass around references. */ - -/* Fixed references. */ -enum { - REF_BIAS = 0x8000, - REF_TRUE = REF_BIAS-3, - REF_FALSE = REF_BIAS-2, - REF_NIL = REF_BIAS-1, /* \--- Constants grow downwards. */ - REF_BASE = REF_BIAS, /* /--- IR grows upwards. */ - REF_FIRST = REF_BIAS+1, - REF_DROP = 0xffff -}; - -/* Note: IRMlit operands must be < REF_BIAS, too! -** This allows for fast and uniform manipulation of all operands -** without looking up the operand mode in lj_ir_mode: -** - CSE calculates the maximum reference of two operands. -** This must work with mixed reference/literal operands, too. -** - DCE marking only checks for operand >= REF_BIAS. -** - LOOP needs to substitute reference operands. -** Constant references and literals must not be modified. -*/ - -#define IRREF2(lo, hi) ((IRRef2)(lo) | ((IRRef2)(hi) << 16)) - -#define irref_isk(ref) ((ref) < REF_BIAS) - -/* Tagged IR references (32 bit). -** -** +-------+-------+---------------+ -** | irt | flags | ref | -** +-------+-------+---------------+ -** -** The tag holds a copy of the IRType and speeds up IR type checks. -*/ -typedef uint32_t TRef; - -#define TREF_REFMASK 0x0000ffff -#define TREF_FRAME 0x00010000 -#define TREF_CONT 0x00020000 - -#define TREF(ref, t) ((TRef)((ref) + ((t)<<24))) - -#define tref_ref(tr) ((IRRef1)(tr)) -#define tref_t(tr) ((IRType)((tr)>>24)) -#define tref_type(tr) ((IRType)(((tr)>>24) & IRT_TYPE)) -#define tref_typerange(tr, first, last) \ - ((((tr)>>24) & IRT_TYPE) - (TRef)(first) <= (TRef)(last-first)) - -#define tref_istype(tr, t) (((tr) & (IRT_TYPE<<24)) == ((t)<<24)) -#define tref_isnil(tr) (tref_istype((tr), IRT_NIL)) -#define tref_isfalse(tr) (tref_istype((tr), IRT_FALSE)) -#define tref_istrue(tr) (tref_istype((tr), IRT_TRUE)) -#define tref_isstr(tr) (tref_istype((tr), IRT_STR)) -#define tref_isfunc(tr) (tref_istype((tr), IRT_FUNC)) -#define tref_iscdata(tr) (tref_istype((tr), IRT_CDATA)) -#define tref_istab(tr) (tref_istype((tr), IRT_TAB)) -#define tref_isudata(tr) (tref_istype((tr), IRT_UDATA)) -#define tref_isnum(tr) (tref_istype((tr), IRT_NUM)) -#define tref_isint(tr) (tref_istype((tr), IRT_INT)) - -#define tref_isbool(tr) (tref_typerange((tr), IRT_FALSE, IRT_TRUE)) -#define tref_ispri(tr) (tref_typerange((tr), IRT_NIL, IRT_TRUE)) -#define tref_istruecond(tr) (!tref_typerange((tr), IRT_NIL, IRT_FALSE)) -#define tref_isinteger(tr) (tref_typerange((tr), IRT_I8, IRT_INT)) -#define tref_isnumber(tr) (tref_typerange((tr), IRT_NUM, IRT_INT)) -#define tref_isnumber_str(tr) (tref_isnumber((tr)) || tref_isstr((tr))) -#define tref_isgcv(tr) (tref_typerange((tr), IRT_STR, IRT_UDATA)) - -#define tref_isk(tr) (irref_isk(tref_ref((tr)))) -#define tref_isk2(tr1, tr2) (irref_isk(tref_ref((tr1) | (tr2)))) - -#define TREF_PRI(t) (TREF(REF_NIL-(t), (t))) -#define TREF_NIL (TREF_PRI(IRT_NIL)) -#define TREF_FALSE (TREF_PRI(IRT_FALSE)) -#define TREF_TRUE (TREF_PRI(IRT_TRUE)) - -/* -- IR format ----------------------------------------------------------- */ - -/* IR instruction format (64 bit). -** -** 16 16 8 8 8 8 -** +-------+-------+---+---+---+---+ -** | op1 | op2 | t | o | r | s | -** +-------+-------+---+---+---+---+ -** | op12/i/gco | ot | prev | (alternative fields in union) -** +---------------+-------+-------+ -** 32 16 16 -** -** prev is only valid prior to register allocation and then reused for r + s. -*/ - -typedef union IRIns { - struct { - LJ_ENDIAN_LOHI( - IRRef1 op1; /* IR operand 1. */ - , IRRef1 op2; /* IR operand 2. */ - ) - IROpT ot; /* IR opcode and type (overlaps t and o). */ - IRRef1 prev; /* Previous ins in same chain (overlaps r and s). */ - }; - struct { - IRRef2 op12; /* IR operand 1 and 2 (overlaps op1 and op2). */ - LJ_ENDIAN_LOHI( - IRType1 t; /* IR type. */ - , IROp1 o; /* IR opcode. */ - ) - LJ_ENDIAN_LOHI( - uint8_t r; /* Register allocation (overlaps prev). */ - , uint8_t s; /* Spill slot allocation (overlaps prev). */ - ) - }; - int32_t i; /* 32 bit signed integer literal (overlaps op12). */ - GCRef gcr; /* GCobj constant (overlaps op12). */ - MRef ptr; /* Pointer constant (overlaps op12). */ -} IRIns; - -#define ir_kgc(ir) check_exp((ir)->o == IR_KGC, gcref((ir)->gcr)) -#define ir_kstr(ir) (gco2str(ir_kgc((ir)))) -#define ir_ktab(ir) (gco2tab(ir_kgc((ir)))) -#define ir_kfunc(ir) (gco2func(ir_kgc((ir)))) -#define ir_kcdata(ir) (gco2cd(ir_kgc((ir)))) -#define ir_knum(ir) check_exp((ir)->o == IR_KNUM, mref((ir)->ptr, cTValue)) -#define ir_kint64(ir) check_exp((ir)->o == IR_KINT64, mref((ir)->ptr,cTValue)) -#define ir_k64(ir) \ - check_exp((ir)->o == IR_KNUM || (ir)->o == IR_KINT64, mref((ir)->ptr,cTValue)) -#define ir_kptr(ir) \ - check_exp((ir)->o == IR_KPTR || (ir)->o == IR_KKPTR, mref((ir)->ptr, void)) - -/* A store or any other op with a non-weak guard has a side-effect. */ -static LJ_AINLINE int ir_sideeff(IRIns *ir) -{ - return (((ir->t.irt | ~IRT_GUARD) & lj_ir_mode[ir->o]) >= IRM_S); -} - -LJ_STATIC_ASSERT((int)IRT_GUARD == (int)IRM_W); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_ircall.h b/third-party/LuaJIT-2.0.2/src/lj_ircall.h deleted file mode 100644 index 7fcc532ebb..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_ircall.h +++ /dev/null @@ -1,271 +0,0 @@ -/* -** IR CALL* instruction definitions. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_IRCALL_H -#define _LJ_IRCALL_H - -#include "lj_obj.h" -#include "lj_ir.h" -#include "lj_jit.h" - -/* C call info for CALL* instructions. */ -typedef struct CCallInfo { - ASMFunction func; /* Function pointer. */ - uint32_t flags; /* Number of arguments and flags. */ -} CCallInfo; - -#define CCI_NARGS(ci) ((ci)->flags & 0xff) /* Extract # of args. */ -#define CCI_NARGS_MAX 32 /* Max. # of args. */ - -#define CCI_OTSHIFT 16 -#define CCI_OPTYPE(ci) ((ci)->flags >> CCI_OTSHIFT) /* Get op/type. */ -#define CCI_OPSHIFT 24 -#define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */ - -#define CCI_CALL_N (IR_CALLN << CCI_OPSHIFT) -#define CCI_CALL_L (IR_CALLL << CCI_OPSHIFT) -#define CCI_CALL_S (IR_CALLS << CCI_OPSHIFT) -#define CCI_CALL_FN (CCI_CALL_N|CCI_CC_FASTCALL) -#define CCI_CALL_FL (CCI_CALL_L|CCI_CC_FASTCALL) -#define CCI_CALL_FS (CCI_CALL_S|CCI_CC_FASTCALL) - -/* C call info flags. */ -#define CCI_L 0x0100 /* Implicit L arg. */ -#define CCI_CASTU64 0x0200 /* Cast u64 result to number. */ -#define CCI_NOFPRCLOBBER 0x0400 /* Does not clobber any FPRs. */ -#define CCI_VARARG 0x0800 /* Vararg function. */ - -#define CCI_CC_MASK 0x3000 /* Calling convention mask. */ -#define CCI_CC_SHIFT 12 -/* ORDER CC */ -#define CCI_CC_CDECL 0x0000 /* Default cdecl calling convention. */ -#define CCI_CC_THISCALL 0x1000 /* Thiscall calling convention. */ -#define CCI_CC_FASTCALL 0x2000 /* Fastcall calling convention. */ -#define CCI_CC_STDCALL 0x3000 /* Stdcall calling convention. */ - -/* Helpers for conditional function definitions. */ -#define IRCALLCOND_ANY(x) x - -#if LJ_TARGET_X86ORX64 -#define IRCALLCOND_FPMATH(x) NULL -#else -#define IRCALLCOND_FPMATH(x) x -#endif - -#if LJ_SOFTFP -#define IRCALLCOND_SOFTFP(x) x -#if LJ_HASFFI -#define IRCALLCOND_SOFTFP_FFI(x) x -#else -#define IRCALLCOND_SOFTFP_FFI(x) NULL -#endif -#else -#define IRCALLCOND_SOFTFP(x) NULL -#define IRCALLCOND_SOFTFP_FFI(x) NULL -#endif - -#define LJ_NEED_FP64 (LJ_TARGET_ARM || LJ_TARGET_PPC || LJ_TARGET_MIPS) - -#if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) -#define IRCALLCOND_FP64_FFI(x) x -#else -#define IRCALLCOND_FP64_FFI(x) NULL -#endif - -#if LJ_HASFFI -#define IRCALLCOND_FFI(x) x -#if LJ_32 -#define IRCALLCOND_FFI32(x) x -#else -#define IRCALLCOND_FFI32(x) NULL -#endif -#else -#define IRCALLCOND_FFI(x) NULL -#define IRCALLCOND_FFI32(x) NULL -#endif - -#if LJ_SOFTFP -#define ARG1_FP 2 /* Treat as 2 32 bit arguments. */ -#else -#define ARG1_FP 1 -#endif - -#if LJ_32 -#define ARG2_64 4 /* Treat as 4 32 bit arguments. */ -#else -#define ARG2_64 2 -#endif - -/* Function definitions for CALL* instructions. */ -#define IRCALLDEF(_) \ - _(ANY, lj_str_cmp, 2, FN, INT, CCI_NOFPRCLOBBER) \ - _(ANY, lj_str_new, 3, S, STR, CCI_L) \ - _(ANY, lj_strscan_num, 2, FN, INT, 0) \ - _(ANY, lj_str_fromint, 2, FN, STR, CCI_L) \ - _(ANY, lj_str_fromnum, 2, FN, STR, CCI_L) \ - _(ANY, lj_tab_new1, 2, FS, TAB, CCI_L) \ - _(ANY, lj_tab_dup, 2, FS, TAB, CCI_L) \ - _(ANY, lj_tab_newkey, 3, S, P32, CCI_L) \ - _(ANY, lj_tab_len, 1, FL, INT, 0) \ - _(ANY, lj_gc_step_jit, 2, FS, NIL, CCI_L) \ - _(ANY, lj_gc_barrieruv, 2, FS, NIL, 0) \ - _(ANY, lj_mem_newgco, 2, FS, P32, CCI_L) \ - _(ANY, lj_math_random_step, 1, FS, NUM, CCI_CASTU64|CCI_NOFPRCLOBBER) \ - _(ANY, lj_vm_modi, 2, FN, INT, 0) \ - _(ANY, sinh, ARG1_FP, N, NUM, 0) \ - _(ANY, cosh, ARG1_FP, N, NUM, 0) \ - _(ANY, tanh, ARG1_FP, N, NUM, 0) \ - _(ANY, fputc, 2, S, INT, 0) \ - _(ANY, fwrite, 4, S, INT, 0) \ - _(ANY, fflush, 1, S, INT, 0) \ - /* ORDER FPM */ \ - _(FPMATH, lj_vm_floor, ARG1_FP, N, NUM, 0) \ - _(FPMATH, lj_vm_ceil, ARG1_FP, N, NUM, 0) \ - _(FPMATH, lj_vm_trunc, ARG1_FP, N, NUM, 0) \ - _(FPMATH, sqrt, ARG1_FP, N, NUM, 0) \ - _(FPMATH, exp, ARG1_FP, N, NUM, 0) \ - _(FPMATH, lj_vm_exp2, ARG1_FP, N, NUM, 0) \ - _(FPMATH, log, ARG1_FP, N, NUM, 0) \ - _(FPMATH, lj_vm_log2, ARG1_FP, N, NUM, 0) \ - _(FPMATH, log10, ARG1_FP, N, NUM, 0) \ - _(FPMATH, sin, ARG1_FP, N, NUM, 0) \ - _(FPMATH, cos, ARG1_FP, N, NUM, 0) \ - _(FPMATH, tan, ARG1_FP, N, NUM, 0) \ - _(FPMATH, lj_vm_powi, ARG1_FP+1, N, NUM, 0) \ - _(FPMATH, pow, ARG1_FP*2, N, NUM, 0) \ - _(FPMATH, atan2, ARG1_FP*2, N, NUM, 0) \ - _(FPMATH, ldexp, ARG1_FP+1, N, NUM, 0) \ - _(SOFTFP, lj_vm_tobit, 2, N, INT, 0) \ - _(SOFTFP, softfp_add, 4, N, NUM, 0) \ - _(SOFTFP, softfp_sub, 4, N, NUM, 0) \ - _(SOFTFP, softfp_mul, 4, N, NUM, 0) \ - _(SOFTFP, softfp_div, 4, N, NUM, 0) \ - _(SOFTFP, softfp_cmp, 4, N, NIL, 0) \ - _(SOFTFP, softfp_i2d, 1, N, NUM, 0) \ - _(SOFTFP, softfp_d2i, 2, N, INT, 0) \ - _(SOFTFP_FFI, softfp_ui2d, 1, N, NUM, 0) \ - _(SOFTFP_FFI, softfp_f2d, 1, N, NUM, 0) \ - _(SOFTFP_FFI, softfp_d2ui, 2, N, INT, 0) \ - _(SOFTFP_FFI, softfp_d2f, 2, N, FLOAT, 0) \ - _(SOFTFP_FFI, softfp_i2f, 1, N, FLOAT, 0) \ - _(SOFTFP_FFI, softfp_ui2f, 1, N, FLOAT, 0) \ - _(SOFTFP_FFI, softfp_f2i, 1, N, INT, 0) \ - _(SOFTFP_FFI, softfp_f2ui, 1, N, INT, 0) \ - _(FP64_FFI, fp64_l2d, 2, N, NUM, 0) \ - _(FP64_FFI, fp64_ul2d, 2, N, NUM, 0) \ - _(FP64_FFI, fp64_l2f, 2, N, FLOAT, 0) \ - _(FP64_FFI, fp64_ul2f, 2, N, FLOAT, 0) \ - _(FP64_FFI, fp64_d2l, ARG1_FP, N, I64, 0) \ - _(FP64_FFI, fp64_d2ul, ARG1_FP, N, U64, 0) \ - _(FP64_FFI, fp64_f2l, 1, N, I64, 0) \ - _(FP64_FFI, fp64_f2ul, 1, N, U64, 0) \ - _(FFI, lj_carith_divi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_divu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_modi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_modu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_powi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_powu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \ - _(FFI, lj_cdata_setfin, 2, FN, P32, CCI_L) \ - _(FFI, strlen, 1, L, INTP, 0) \ - _(FFI, memcpy, 3, S, PTR, 0) \ - _(FFI, memset, 3, S, PTR, 0) \ - _(FFI, lj_vm_errno, 0, S, INT, CCI_NOFPRCLOBBER) \ - _(FFI32, lj_carith_mul64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) - \ - /* End of list. */ - -typedef enum { -#define IRCALLENUM(cond, name, nargs, kind, type, flags) IRCALL_##name, -IRCALLDEF(IRCALLENUM) -#undef IRCALLENUM - IRCALL__MAX -} IRCallID; - -LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...); - -LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1]; - -/* Soft-float declarations. */ -#if LJ_SOFTFP -#if LJ_TARGET_ARM -#define softfp_add __aeabi_dadd -#define softfp_sub __aeabi_dsub -#define softfp_mul __aeabi_dmul -#define softfp_div __aeabi_ddiv -#define softfp_cmp __aeabi_cdcmple -#define softfp_i2d __aeabi_i2d -#define softfp_d2i __aeabi_d2iz -#define softfp_ui2d __aeabi_ui2d -#define softfp_f2d __aeabi_f2d -#define softfp_d2ui __aeabi_d2uiz -#define softfp_d2f __aeabi_d2f -#define softfp_i2f __aeabi_i2f -#define softfp_ui2f __aeabi_ui2f -#define softfp_f2i __aeabi_f2iz -#define softfp_f2ui __aeabi_f2uiz -#define fp64_l2d __aeabi_l2d -#define fp64_ul2d __aeabi_ul2d -#define fp64_l2f __aeabi_l2f -#define fp64_ul2f __aeabi_ul2f -#if LJ_TARGET_IOS -#define fp64_d2l __fixdfdi -#define fp64_d2ul __fixunsdfdi -#define fp64_f2l __fixsfdi -#define fp64_f2ul __fixunssfdi -#else -#define fp64_d2l __aeabi_d2lz -#define fp64_d2ul __aeabi_d2ulz -#define fp64_f2l __aeabi_f2lz -#define fp64_f2ul __aeabi_f2ulz -#endif -#else -#error "Missing soft-float definitions for target architecture" -#endif -extern double softfp_add(double a, double b); -extern double softfp_sub(double a, double b); -extern double softfp_mul(double a, double b); -extern double softfp_div(double a, double b); -extern void softfp_cmp(double a, double b); -extern double softfp_i2d(int32_t a); -extern int32_t softfp_d2i(double a); -#if LJ_HASFFI -extern double softfp_ui2d(uint32_t a); -extern double softfp_f2d(float a); -extern uint32_t softfp_d2ui(double a); -extern float softfp_d2f(double a); -extern float softfp_i2f(int32_t a); -extern float softfp_ui2f(uint32_t a); -extern int32_t softfp_f2i(float a); -extern uint32_t softfp_f2ui(float a); -#endif -#endif - -#if LJ_HASFFI && LJ_NEED_FP64 && !(LJ_TARGET_ARM && LJ_SOFTFP) -#ifdef __GNUC__ -#define fp64_l2d __floatdidf -#define fp64_ul2d __floatundidf -#define fp64_l2f __floatdisf -#define fp64_ul2f __floatundisf -#define fp64_d2l __fixdfdi -#define fp64_d2ul __fixunsdfdi -#define fp64_f2l __fixsfdi -#define fp64_f2ul __fixunssfdi -#else -#error "Missing fp64 helper definitions for this compiler" -#endif -#endif - -#if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) -extern double fp64_l2d(int64_t a); -extern double fp64_ul2d(uint64_t a); -extern float fp64_l2f(int64_t a); -extern float fp64_ul2f(uint64_t a); -extern int64_t fp64_d2l(double a); -extern uint64_t fp64_d2ul(double a); -extern int64_t fp64_f2l(float a); -extern uint64_t fp64_f2ul(float a); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_iropt.h b/third-party/LuaJIT-2.0.2/src/lj_iropt.h deleted file mode 100644 index a74f018f31..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_iropt.h +++ /dev/null @@ -1,161 +0,0 @@ -/* -** Common header for IR emitter and optimizations. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_IROPT_H -#define _LJ_IROPT_H - -#include - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -/* IR emitter. */ -LJ_FUNC void LJ_FASTCALL lj_ir_growtop(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_ir_emit(jit_State *J); - -/* Save current IR in J->fold.ins, but do not emit it (yet). */ -static LJ_AINLINE void lj_ir_set_(jit_State *J, uint16_t ot, IRRef1 a, IRRef1 b) -{ - J->fold.ins.ot = ot; J->fold.ins.op1 = a; J->fold.ins.op2 = b; -} - -#define lj_ir_set(J, ot, a, b) \ - lj_ir_set_(J, (uint16_t)(ot), (IRRef1)(a), (IRRef1)(b)) - -/* Get ref of next IR instruction and optionally grow IR. -** Note: this may invalidate all IRIns*! -*/ -static LJ_AINLINE IRRef lj_ir_nextins(jit_State *J) -{ - IRRef ref = J->cur.nins; - if (LJ_UNLIKELY(ref >= J->irtoplim)) lj_ir_growtop(J); - J->cur.nins = ref + 1; - return ref; -} - -/* Interning of constants. */ -LJ_FUNC TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k); -LJ_FUNC void lj_ir_k64_freeall(jit_State *J); -LJ_FUNC TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv); -LJ_FUNC cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64); -LJ_FUNC TRef lj_ir_knum_u64(jit_State *J, uint64_t u64); -LJ_FUNC TRef lj_ir_knumint(jit_State *J, lua_Number n); -LJ_FUNC TRef lj_ir_kint64(jit_State *J, uint64_t u64); -LJ_FUNC TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t); -LJ_FUNC TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr); -LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t); -LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot); - -#if LJ_64 -#define lj_ir_kintp(J, k) lj_ir_kint64(J, (uint64_t)(k)) -#else -#define lj_ir_kintp(J, k) lj_ir_kint(J, (int32_t)(k)) -#endif - -static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n) -{ - TValue tv; - tv.n = n; - return lj_ir_knum_u64(J, tv.u64); -} - -#define lj_ir_kstr(J, str) lj_ir_kgc(J, obj2gco((str)), IRT_STR) -#define lj_ir_ktab(J, tab) lj_ir_kgc(J, obj2gco((tab)), IRT_TAB) -#define lj_ir_kfunc(J, func) lj_ir_kgc(J, obj2gco((func)), IRT_FUNC) -#define lj_ir_kptr(J, ptr) lj_ir_kptr_(J, IR_KPTR, (ptr)) -#define lj_ir_kkptr(J, ptr) lj_ir_kptr_(J, IR_KKPTR, (ptr)) - -/* Special FP constants. */ -#define lj_ir_knum_zero(J) lj_ir_knum_u64(J, U64x(00000000,00000000)) -#define lj_ir_knum_one(J) lj_ir_knum_u64(J, U64x(3ff00000,00000000)) -#define lj_ir_knum_tobit(J) lj_ir_knum_u64(J, U64x(43380000,00000000)) - -/* Special 128 bit SIMD constants. */ -#define lj_ir_knum_abs(J) lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_ABS)) -#define lj_ir_knum_neg(J) lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_NEG)) - -/* Access to constants. */ -LJ_FUNC void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir); - -/* Convert IR operand types. */ -LJ_FUNC TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr); -LJ_FUNC TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr); -LJ_FUNC TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr); - -/* Miscellaneous IR ops. */ -LJ_FUNC int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op); -LJ_FUNC int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op); -LJ_FUNC void lj_ir_rollback(jit_State *J, IRRef ref); - -/* Emit IR instructions with on-the-fly optimizations. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_fold(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_cse(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim); - -/* Special return values for the fold functions. */ -enum { - NEXTFOLD, /* Couldn't fold, pass on. */ - RETRYFOLD, /* Retry fold with modified fins. */ - KINTFOLD, /* Return ref for int constant in fins->i. */ - FAILFOLD, /* Guard would always fail. */ - DROPFOLD, /* Guard eliminated. */ - MAX_FOLD -}; - -#define INTFOLD(k) ((J->fold.ins.i = (k)), (TRef)KINTFOLD) -#define INT64FOLD(k) (lj_ir_kint64(J, (k))) -#define CONDFOLD(cond) ((TRef)FAILFOLD + (TRef)(cond)) -#define LEFTFOLD (J->fold.ins.op1) -#define RIGHTFOLD (J->fold.ins.op2) -#define CSEFOLD (lj_opt_cse(J)) -#define EMITFOLD (lj_ir_emit(J)) - -/* Load/store forwarding. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J); -LJ_FUNC int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J); -LJ_FUNC int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim); -LJ_FUNC int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref); - -/* Dead-store elimination. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J); - -/* Narrowing. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef key); -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr); -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr); -#if LJ_HASFFI -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef key); -#endif -LJ_FUNC TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc, - TValue *vb, TValue *vc, IROp op); -LJ_FUNC TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc); -LJ_FUNC TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vc); -LJ_FUNC TRef lj_opt_narrow_pow(jit_State *J, TRef rb, TRef rc, TValue *vc); -LJ_FUNC IRType lj_opt_narrow_forl(jit_State *J, cTValue *forbase); - -/* Optimization passes. */ -LJ_FUNC void lj_opt_dce(jit_State *J); -LJ_FUNC int lj_opt_loop(jit_State *J); -#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) -LJ_FUNC void lj_opt_split(jit_State *J); -#else -#define lj_opt_split(J) UNUSED(J) -#endif -LJ_FUNC void lj_opt_sink(jit_State *J); - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_jit.h b/third-party/LuaJIT-2.0.2/src/lj_jit.h deleted file mode 100644 index c0b1c41e68..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_jit.h +++ /dev/null @@ -1,416 +0,0 @@ -/* -** Common definitions for the JIT compiler. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_JIT_H -#define _LJ_JIT_H - -#include "lj_obj.h" -#include "lj_ir.h" - -/* JIT engine flags. */ -#define JIT_F_ON 0x00000001 - -/* CPU-specific JIT engine flags. */ -#if LJ_TARGET_X86ORX64 -#define JIT_F_CMOV 0x00000010 -#define JIT_F_SSE2 0x00000020 -#define JIT_F_SSE3 0x00000040 -#define JIT_F_SSE4_1 0x00000080 -#define JIT_F_P4 0x00000100 -#define JIT_F_PREFER_IMUL 0x00000200 -#define JIT_F_SPLIT_XMM 0x00000400 -#define JIT_F_LEA_AGU 0x00000800 - -/* Names for the CPU-specific flags. Must match the order above. */ -#define JIT_F_CPU_FIRST JIT_F_CMOV -#define JIT_F_CPUSTRING "\4CMOV\4SSE2\4SSE3\6SSE4.1\2P4\3AMD\2K8\4ATOM" -#elif LJ_TARGET_ARM -#define JIT_F_ARMV6_ 0x00000010 -#define JIT_F_ARMV6T2_ 0x00000020 -#define JIT_F_ARMV7 0x00000040 -#define JIT_F_VFPV2 0x00000080 -#define JIT_F_VFPV3 0x00000100 - -#define JIT_F_ARMV6 (JIT_F_ARMV6_|JIT_F_ARMV6T2_|JIT_F_ARMV7) -#define JIT_F_ARMV6T2 (JIT_F_ARMV6T2_|JIT_F_ARMV7) -#define JIT_F_VFP (JIT_F_VFPV2|JIT_F_VFPV3) - -/* Names for the CPU-specific flags. Must match the order above. */ -#define JIT_F_CPU_FIRST JIT_F_ARMV6_ -#define JIT_F_CPUSTRING "\5ARMv6\7ARMv6T2\5ARMv7\5VFPv2\5VFPv3" -#elif LJ_TARGET_PPC -#define JIT_F_SQRT 0x00000010 -#define JIT_F_ROUND 0x00000020 - -/* Names for the CPU-specific flags. Must match the order above. */ -#define JIT_F_CPU_FIRST JIT_F_SQRT -#define JIT_F_CPUSTRING "\4SQRT\5ROUND" -#elif LJ_TARGET_MIPS -#define JIT_F_MIPS32R2 0x00000010 - -/* Names for the CPU-specific flags. Must match the order above. */ -#define JIT_F_CPU_FIRST JIT_F_MIPS32R2 -#define JIT_F_CPUSTRING "\010MIPS32R2" -#else -#define JIT_F_CPU_FIRST 0 -#define JIT_F_CPUSTRING "" -#endif - -/* Optimization flags. */ -#define JIT_F_OPT_MASK 0x0fff0000 - -#define JIT_F_OPT_FOLD 0x00010000 -#define JIT_F_OPT_CSE 0x00020000 -#define JIT_F_OPT_DCE 0x00040000 -#define JIT_F_OPT_FWD 0x00080000 -#define JIT_F_OPT_DSE 0x00100000 -#define JIT_F_OPT_NARROW 0x00200000 -#define JIT_F_OPT_LOOP 0x00400000 -#define JIT_F_OPT_ABC 0x00800000 -#define JIT_F_OPT_SINK 0x01000000 -#define JIT_F_OPT_FUSE 0x02000000 - -/* Optimizations names for -O. Must match the order above. */ -#define JIT_F_OPT_FIRST JIT_F_OPT_FOLD -#define JIT_F_OPTSTRING \ - "\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4sink\4fuse" - -/* Optimization levels set a fixed combination of flags. */ -#define JIT_F_OPT_0 0 -#define JIT_F_OPT_1 (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE) -#define JIT_F_OPT_2 (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP) -#define JIT_F_OPT_3 (JIT_F_OPT_2|\ - JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_SINK|JIT_F_OPT_FUSE) -#define JIT_F_OPT_DEFAULT JIT_F_OPT_3 - -#if LJ_TARGET_WINDOWS || LJ_64 -/* See: http://blogs.msdn.com/oldnewthing/archive/2003/10/08/55239.aspx */ -#define JIT_P_sizemcode_DEFAULT 64 -#else -/* Could go as low as 4K, but the mmap() overhead would be rather high. */ -#define JIT_P_sizemcode_DEFAULT 32 -#endif - -/* Optimization parameters and their defaults. Length is a char in octal! */ -#define JIT_PARAMDEF(_) \ - _(\010, maxtrace, 1000) /* Max. # of traces in cache. */ \ - _(\011, maxrecord, 4000) /* Max. # of recorded IR instructions. */ \ - _(\012, maxirconst, 500) /* Max. # of IR constants of a trace. */ \ - _(\007, maxside, 100) /* Max. # of side traces of a root trace. */ \ - _(\007, maxsnap, 500) /* Max. # of snapshots for a trace. */ \ - \ - _(\007, hotloop, 56) /* # of iter. to detect a hot loop/call. */ \ - _(\007, hotexit, 10) /* # of taken exits to start a side trace. */ \ - _(\007, tryside, 4) /* # of attempts to compile a side trace. */ \ - \ - _(\012, instunroll, 4) /* Max. unroll for instable loops. */ \ - _(\012, loopunroll, 15) /* Max. unroll for loop ops in side traces. */ \ - _(\012, callunroll, 3) /* Max. unroll for recursive calls. */ \ - _(\011, recunroll, 2) /* Min. unroll for true recursion. */ \ - \ - /* Size of each machine code area (in KBytes). */ \ - _(\011, sizemcode, JIT_P_sizemcode_DEFAULT) \ - /* Max. total size of all machine code areas (in KBytes). */ \ - _(\010, maxmcode, 512) \ - /* End of list. */ - -enum { -#define JIT_PARAMENUM(len, name, value) JIT_P_##name, -JIT_PARAMDEF(JIT_PARAMENUM) -#undef JIT_PARAMENUM - JIT_P__MAX -}; - -#define JIT_PARAMSTR(len, name, value) #len #name -#define JIT_P_STRING JIT_PARAMDEF(JIT_PARAMSTR) - -/* Trace compiler state. */ -typedef enum { - LJ_TRACE_IDLE, /* Trace compiler idle. */ - LJ_TRACE_ACTIVE = 0x10, - LJ_TRACE_RECORD, /* Bytecode recording active. */ - LJ_TRACE_START, /* New trace started. */ - LJ_TRACE_END, /* End of trace. */ - LJ_TRACE_ASM, /* Assemble trace. */ - LJ_TRACE_ERR /* Trace aborted with error. */ -} TraceState; - -/* Post-processing action. */ -typedef enum { - LJ_POST_NONE, /* No action. */ - LJ_POST_FIXCOMP, /* Fixup comparison and emit pending guard. */ - LJ_POST_FIXGUARD, /* Fixup and emit pending guard. */ - LJ_POST_FIXGUARDSNAP, /* Fixup and emit pending guard and snapshot. */ - LJ_POST_FIXBOOL, /* Fixup boolean result. */ - LJ_POST_FIXCONST, /* Fixup constant results. */ - LJ_POST_FFRETRY /* Suppress recording of retried fast functions. */ -} PostProc; - -/* Machine code type. */ -#if LJ_TARGET_X86ORX64 -typedef uint8_t MCode; -#else -typedef uint32_t MCode; -#endif - -/* Stack snapshot header. */ -typedef struct SnapShot { - uint16_t mapofs; /* Offset into snapshot map. */ - IRRef1 ref; /* First IR ref for this snapshot. */ - uint8_t nslots; /* Number of valid slots. */ - uint8_t topslot; /* Maximum frame extent. */ - uint8_t nent; /* Number of compressed entries. */ - uint8_t count; /* Count of taken exits for this snapshot. */ -} SnapShot; - -#define SNAPCOUNT_DONE 255 /* Already compiled and linked a side trace. */ - -/* Compressed snapshot entry. */ -typedef uint32_t SnapEntry; - -#define SNAP_FRAME 0x010000 /* Frame slot. */ -#define SNAP_CONT 0x020000 /* Continuation slot. */ -#define SNAP_NORESTORE 0x040000 /* No need to restore slot. */ -#define SNAP_SOFTFPNUM 0x080000 /* Soft-float number. */ -LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME); -LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT); - -#define SNAP(slot, flags, ref) (((SnapEntry)(slot) << 24) + (flags) + (ref)) -#define SNAP_TR(slot, tr) \ - (((SnapEntry)(slot) << 24) + ((tr) & (TREF_CONT|TREF_FRAME|TREF_REFMASK))) -#define SNAP_MKPC(pc) ((SnapEntry)u32ptr(pc)) -#define SNAP_MKFTSZ(ftsz) ((SnapEntry)(ftsz)) -#define snap_ref(sn) ((sn) & 0xffff) -#define snap_slot(sn) ((BCReg)((sn) >> 24)) -#define snap_isframe(sn) ((sn) & SNAP_FRAME) -#define snap_pc(sn) ((const BCIns *)(uintptr_t)(sn)) -#define snap_setref(sn, ref) (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref)) - -/* Snapshot and exit numbers. */ -typedef uint32_t SnapNo; -typedef uint32_t ExitNo; - -/* Trace number. */ -typedef uint32_t TraceNo; /* Used to pass around trace numbers. */ -typedef uint16_t TraceNo1; /* Stored trace number. */ - -/* Type of link. ORDER LJ_TRLINK */ -typedef enum { - LJ_TRLINK_NONE, /* Incomplete trace. No link, yet. */ - LJ_TRLINK_ROOT, /* Link to other root trace. */ - LJ_TRLINK_LOOP, /* Loop to same trace. */ - LJ_TRLINK_TAILREC, /* Tail-recursion. */ - LJ_TRLINK_UPREC, /* Up-recursion. */ - LJ_TRLINK_DOWNREC, /* Down-recursion. */ - LJ_TRLINK_INTERP, /* Fallback to interpreter. */ - LJ_TRLINK_RETURN /* Return to interpreter. */ -} TraceLink; - -/* Trace object. */ -typedef struct GCtrace { - GCHeader; - uint8_t topslot; /* Top stack slot already checked to be allocated. */ - uint8_t linktype; /* Type of link. */ - IRRef nins; /* Next IR instruction. Biased with REF_BIAS. */ - GCRef gclist; - IRIns *ir; /* IR instructions/constants. Biased with REF_BIAS. */ - IRRef nk; /* Lowest IR constant. Biased with REF_BIAS. */ - uint16_t nsnap; /* Number of snapshots. */ - uint16_t nsnapmap; /* Number of snapshot map elements. */ - SnapShot *snap; /* Snapshot array. */ - SnapEntry *snapmap; /* Snapshot map. */ - GCRef startpt; /* Starting prototype. */ - MRef startpc; /* Bytecode PC of starting instruction. */ - BCIns startins; /* Original bytecode of starting instruction. */ - MSize szmcode; /* Size of machine code. */ - MCode *mcode; /* Start of machine code. */ - MSize mcloop; /* Offset of loop start in machine code. */ - uint16_t nchild; /* Number of child traces (root trace only). */ - uint16_t spadjust; /* Stack pointer adjustment (offset in bytes). */ - TraceNo1 traceno; /* Trace number. */ - TraceNo1 link; /* Linked trace (or self for loops). */ - TraceNo1 root; /* Root trace of side trace (or 0 for root traces). */ - TraceNo1 nextroot; /* Next root trace for same prototype. */ - TraceNo1 nextside; /* Next side trace of same root trace. */ - uint8_t sinktags; /* Trace has SINK tags. */ - uint8_t unused1; -#ifdef LUAJIT_USE_GDBJIT - void *gdbjit_entry; /* GDB JIT entry. */ -#endif -} GCtrace; - -#define gco2trace(o) check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o)) -#define traceref(J, n) \ - check_exp((n)>0 && (MSize)(n)sizetrace, (GCtrace *)gcref(J->trace[(n)])) - -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist)); - -static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap) -{ - if (snap+1 == &T->snap[T->nsnap]) - return T->nsnapmap; - else - return (snap+1)->mapofs; -} - -/* Round-robin penalty cache for bytecodes leading to aborted traces. */ -typedef struct HotPenalty { - MRef pc; /* Starting bytecode PC. */ - uint16_t val; /* Penalty value, i.e. hotcount start. */ - uint16_t reason; /* Abort reason (really TraceErr). */ -} HotPenalty; - -#define PENALTY_SLOTS 64 /* Penalty cache slot. Must be a power of 2. */ -#define PENALTY_MIN (36*2) /* Minimum penalty value. */ -#define PENALTY_MAX 60000 /* Maximum penalty value. */ -#define PENALTY_RNDBITS 4 /* # of random bits to add to penalty value. */ - -/* Round-robin backpropagation cache for narrowing conversions. */ -typedef struct BPropEntry { - IRRef1 key; /* Key: original reference. */ - IRRef1 val; /* Value: reference after conversion. */ - IRRef mode; /* Mode for this entry (currently IRCONV_*). */ -} BPropEntry; - -/* Number of slots for the backpropagation cache. Must be a power of 2. */ -#define BPROP_SLOTS 16 - -/* Scalar evolution analysis cache. */ -typedef struct ScEvEntry { - IRRef1 idx; /* Index reference. */ - IRRef1 start; /* Constant start reference. */ - IRRef1 stop; /* Constant stop reference. */ - IRRef1 step; /* Constant step reference. */ - IRType1 t; /* Scalar type. */ - uint8_t dir; /* Direction. 1: +, 0: -. */ -} ScEvEntry; - -/* 128 bit SIMD constants. */ -enum { - LJ_KSIMD_ABS, - LJ_KSIMD_NEG, - LJ_KSIMD__MAX -}; - -/* Get 16 byte aligned pointer to SIMD constant. */ -#define LJ_KSIMD(J, n) \ - ((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15)) - -/* Set/reset flag to activate the SPLIT pass for the current trace. */ -#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) -#define lj_needsplit(J) (J->needsplit = 1) -#define lj_resetsplit(J) (J->needsplit = 0) -#else -#define lj_needsplit(J) UNUSED(J) -#define lj_resetsplit(J) UNUSED(J) -#endif - -/* Fold state is used to fold instructions on-the-fly. */ -typedef struct FoldState { - IRIns ins; /* Currently emitted instruction. */ - IRIns left; /* Instruction referenced by left operand. */ - IRIns right; /* Instruction referenced by right operand. */ -} FoldState; - -/* JIT compiler state. */ -typedef struct jit_State { - GCtrace cur; /* Current trace. */ - - lua_State *L; /* Current Lua state. */ - const BCIns *pc; /* Current PC. */ - GCfunc *fn; /* Current function. */ - GCproto *pt; /* Current prototype. */ - TRef *base; /* Current frame base, points into J->slots. */ - - uint32_t flags; /* JIT engine flags. */ - BCReg maxslot; /* Relative to baseslot. */ - BCReg baseslot; /* Current frame base, offset into J->slots. */ - - uint8_t mergesnap; /* Allowed to merge with next snapshot. */ - uint8_t needsnap; /* Need snapshot before recording next bytecode. */ - IRType1 guardemit; /* Accumulated IRT_GUARD for emitted instructions. */ - uint8_t bcskip; /* Number of bytecode instructions to skip. */ - - FoldState fold; /* Fold state. */ - - const BCIns *bc_min; /* Start of allowed bytecode range for root trace. */ - MSize bc_extent; /* Extent of the range. */ - - TraceState state; /* Trace compiler state. */ - - int32_t instunroll; /* Unroll counter for instable loops. */ - int32_t loopunroll; /* Unroll counter for loop ops in side traces. */ - int32_t tailcalled; /* Number of successive tailcalls. */ - int32_t framedepth; /* Current frame depth. */ - int32_t retdepth; /* Return frame depth (count of RETF). */ - - MRef k64; /* Pointer to chained array of 64 bit constants. */ - TValue ksimd[LJ_KSIMD__MAX*2+1]; /* 16 byte aligned SIMD constants. */ - - IRIns *irbuf; /* Temp. IR instruction buffer. Biased with REF_BIAS. */ - IRRef irtoplim; /* Upper limit of instuction buffer (biased). */ - IRRef irbotlim; /* Lower limit of instuction buffer (biased). */ - IRRef loopref; /* Last loop reference or ref of final LOOP (or 0). */ - - MSize sizesnap; /* Size of temp. snapshot buffer. */ - SnapShot *snapbuf; /* Temp. snapshot buffer. */ - SnapEntry *snapmapbuf; /* Temp. snapshot map buffer. */ - MSize sizesnapmap; /* Size of temp. snapshot map buffer. */ - - PostProc postproc; /* Required post-processing after execution. */ -#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) - int needsplit; /* Need SPLIT pass. */ -#endif - - GCRef *trace; /* Array of traces. */ - TraceNo freetrace; /* Start of scan for next free trace. */ - MSize sizetrace; /* Size of trace array. */ - - IRRef1 chain[IR__MAX]; /* IR instruction skip-list chain anchors. */ - TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA]; /* Stack slot map. */ - - int32_t param[JIT_P__MAX]; /* JIT engine parameters. */ - - MCode *exitstubgroup[LJ_MAX_EXITSTUBGR]; /* Exit stub group addresses. */ - - HotPenalty penalty[PENALTY_SLOTS]; /* Penalty slots. */ - uint32_t penaltyslot; /* Round-robin index into penalty slots. */ - uint32_t prngstate; /* PRNG state. */ - - BPropEntry bpropcache[BPROP_SLOTS]; /* Backpropagation cache slots. */ - uint32_t bpropslot; /* Round-robin index into bpropcache slots. */ - - ScEvEntry scev; /* Scalar evolution analysis cache slots. */ - - const BCIns *startpc; /* Bytecode PC of starting instruction. */ - TraceNo parent; /* Parent of current side trace (0 for root traces). */ - ExitNo exitno; /* Exit number in parent of current side trace. */ - - BCIns *patchpc; /* PC for pending re-patch. */ - BCIns patchins; /* Instruction for pending re-patch. */ - - int mcprot; /* Protection of current mcode area. */ - MCode *mcarea; /* Base of current mcode area. */ - MCode *mctop; /* Top of current mcode area. */ - MCode *mcbot; /* Bottom of current mcode area. */ - size_t szmcarea; /* Size of current mcode area. */ - size_t szallmcarea; /* Total size of all allocated mcode areas. */ - - TValue errinfo; /* Additional info element for trace errors. */ -} -#if LJ_TARGET_ARM -LJ_ALIGN(16) /* For DISPATCH-relative addresses in assembler part. */ -#endif -jit_State; - -/* Trivial PRNG e.g. used for penalty randomization. */ -static LJ_AINLINE uint32_t LJ_PRNG_BITS(jit_State *J, int bits) -{ - /* Yes, this LCG is very weak, but that doesn't matter for our use case. */ - J->prngstate = J->prngstate * 1103515245 + 12345; - return J->prngstate >> (32-bits); -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_lex.c b/third-party/LuaJIT-2.0.2/src/lj_lex.c deleted file mode 100644 index 9f2b06f8f5..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_lex.c +++ /dev/null @@ -1,481 +0,0 @@ -/* -** Lexical analyzer. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_lex_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#if LJ_HASFFI -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lualib.h" -#endif -#include "lj_state.h" -#include "lj_lex.h" -#include "lj_parse.h" -#include "lj_char.h" -#include "lj_strscan.h" - -/* Lua lexer token names. */ -static const char *const tokennames[] = { -#define TKSTR1(name) #name, -#define TKSTR2(name, sym) #sym, -TKDEF(TKSTR1, TKSTR2) -#undef TKSTR1 -#undef TKSTR2 - NULL -}; - -/* -- Buffer handling ----------------------------------------------------- */ - -#define char2int(c) ((int)(uint8_t)(c)) -#define next(ls) \ - (ls->current = (ls->n--) > 0 ? char2int(*ls->p++) : fillbuf(ls)) -#define save_and_next(ls) (save(ls, ls->current), next(ls)) -#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') -#define END_OF_STREAM (-1) - -static int fillbuf(LexState *ls) -{ - size_t sz; - const char *buf = ls->rfunc(ls->L, ls->rdata, &sz); - if (buf == NULL || sz == 0) return END_OF_STREAM; - ls->n = (MSize)sz - 1; - ls->p = buf; - return char2int(*(ls->p++)); -} - -static LJ_NOINLINE void save_grow(LexState *ls, int c) -{ - MSize newsize; - if (ls->sb.sz >= LJ_MAX_STR/2) - lj_lex_error(ls, 0, LJ_ERR_XELEM); - newsize = ls->sb.sz * 2; - lj_str_resizebuf(ls->L, &ls->sb, newsize); - ls->sb.buf[ls->sb.n++] = (char)c; -} - -static LJ_AINLINE void save(LexState *ls, int c) -{ - if (LJ_UNLIKELY(ls->sb.n + 1 > ls->sb.sz)) - save_grow(ls, c); - else - ls->sb.buf[ls->sb.n++] = (char)c; -} - -static void inclinenumber(LexState *ls) -{ - int old = ls->current; - lua_assert(currIsNewline(ls)); - next(ls); /* skip `\n' or `\r' */ - if (currIsNewline(ls) && ls->current != old) - next(ls); /* skip `\n\r' or `\r\n' */ - if (++ls->linenumber >= LJ_MAX_LINE) - lj_lex_error(ls, ls->token, LJ_ERR_XLINES); -} - -/* -- Scanner for terminals ----------------------------------------------- */ - -/* Parse a number literal. */ -static void lex_number(LexState *ls, TValue *tv) -{ - StrScanFmt fmt; - int c, xp = 'e'; - lua_assert(lj_char_isdigit(ls->current)); - if ((c = ls->current) == '0') { - save_and_next(ls); - if ((ls->current | 0x20) == 'x') xp = 'p'; - } - while (lj_char_isident(ls->current) || ls->current == '.' || - ((ls->current == '-' || ls->current == '+') && (c | 0x20) == xp)) { - c = ls->current; - save_and_next(ls); - } - save(ls, '\0'); - fmt = lj_strscan_scan((const uint8_t *)ls->sb.buf, tv, - (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) | - (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0)); - if (LJ_DUALNUM && fmt == STRSCAN_INT) { - setitype(tv, LJ_TISNUM); - } else if (fmt == STRSCAN_NUM) { - /* Already in correct format. */ -#if LJ_HASFFI - } else if (fmt != STRSCAN_ERROR) { - lua_State *L = ls->L; - GCcdata *cd; - lua_assert(fmt == STRSCAN_I64 || fmt == STRSCAN_U64 || fmt == STRSCAN_IMAG); - if (!ctype_ctsG(G(L))) { - ptrdiff_t oldtop = savestack(L, L->top); - luaopen_ffi(L); /* Load FFI library on-demand. */ - L->top = restorestack(L, oldtop); - } - if (fmt == STRSCAN_IMAG) { - cd = lj_cdata_new_(L, CTID_COMPLEX_DOUBLE, 2*sizeof(double)); - ((double *)cdataptr(cd))[0] = 0; - ((double *)cdataptr(cd))[1] = numV(tv); - } else { - cd = lj_cdata_new_(L, fmt==STRSCAN_I64 ? CTID_INT64 : CTID_UINT64, 8); - *(uint64_t *)cdataptr(cd) = tv->u64; - } - lj_parse_keepcdata(ls, tv, cd); -#endif - } else { - lua_assert(fmt == STRSCAN_ERROR); - lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER); - } -} - -static int skip_sep(LexState *ls) -{ - int count = 0; - int s = ls->current; - lua_assert(s == '[' || s == ']'); - save_and_next(ls); - while (ls->current == '=') { - save_and_next(ls); - count++; - } - return (ls->current == s) ? count : (-count) - 1; -} - -static void read_long_string(LexState *ls, TValue *tv, int sep) -{ - save_and_next(ls); /* skip 2nd `[' */ - if (currIsNewline(ls)) /* string starts with a newline? */ - inclinenumber(ls); /* skip it */ - for (;;) { - switch (ls->current) { - case END_OF_STREAM: - lj_lex_error(ls, TK_eof, tv ? LJ_ERR_XLSTR : LJ_ERR_XLCOM); - break; - case ']': - if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd `]' */ - goto endloop; - } - break; - case '\n': - case '\r': - save(ls, '\n'); - inclinenumber(ls); - if (!tv) lj_str_resetbuf(&ls->sb); /* avoid wasting space */ - break; - default: - if (tv) save_and_next(ls); - else next(ls); - break; - } - } endloop: - if (tv) { - GCstr *str = lj_parse_keepstr(ls, ls->sb.buf + (2 + (MSize)sep), - ls->sb.n - 2*(2 + (MSize)sep)); - setstrV(ls->L, tv, str); - } -} - -static void read_string(LexState *ls, int delim, TValue *tv) -{ - save_and_next(ls); - while (ls->current != delim) { - switch (ls->current) { - case END_OF_STREAM: - lj_lex_error(ls, TK_eof, LJ_ERR_XSTR); - continue; - case '\n': - case '\r': - lj_lex_error(ls, TK_string, LJ_ERR_XSTR); - continue; - case '\\': { - int c = next(ls); /* Skip the '\\'. */ - switch (c) { - case 'a': c = '\a'; break; - case 'b': c = '\b'; break; - case 'f': c = '\f'; break; - case 'n': c = '\n'; break; - case 'r': c = '\r'; break; - case 't': c = '\t'; break; - case 'v': c = '\v'; break; - case 'x': /* Hexadecimal escape '\xXX'. */ - c = (next(ls) & 15u) << 4; - if (!lj_char_isdigit(ls->current)) { - if (!lj_char_isxdigit(ls->current)) goto err_xesc; - c += 9 << 4; - } - c += (next(ls) & 15u); - if (!lj_char_isdigit(ls->current)) { - if (!lj_char_isxdigit(ls->current)) goto err_xesc; - c += 9; - } - break; - case 'z': /* Skip whitespace. */ - next(ls); - while (lj_char_isspace(ls->current)) - if (currIsNewline(ls)) inclinenumber(ls); else next(ls); - continue; - case '\n': case '\r': save(ls, '\n'); inclinenumber(ls); continue; - case '\\': case '\"': case '\'': break; - case END_OF_STREAM: continue; - default: - if (!lj_char_isdigit(c)) - goto err_xesc; - c -= '0'; /* Decimal escape '\ddd'. */ - if (lj_char_isdigit(next(ls))) { - c = c*10 + (ls->current - '0'); - if (lj_char_isdigit(next(ls))) { - c = c*10 + (ls->current - '0'); - if (c > 255) { - err_xesc: - lj_lex_error(ls, TK_string, LJ_ERR_XESC); - } - next(ls); - } - } - save(ls, c); - continue; - } - save(ls, c); - next(ls); - continue; - } - default: - save_and_next(ls); - break; - } - } - save_and_next(ls); /* skip delimiter */ - setstrV(ls->L, tv, lj_parse_keepstr(ls, ls->sb.buf + 1, ls->sb.n - 2)); -} - -/* -- Main lexical scanner ------------------------------------------------ */ - -static int llex(LexState *ls, TValue *tv) -{ - lj_str_resetbuf(&ls->sb); - for (;;) { - if (lj_char_isident(ls->current)) { - GCstr *s; - if (lj_char_isdigit(ls->current)) { /* Numeric literal. */ - lex_number(ls, tv); - return TK_number; - } - /* Identifier or reserved word. */ - do { - save_and_next(ls); - } while (lj_char_isident(ls->current)); - s = lj_parse_keepstr(ls, ls->sb.buf, ls->sb.n); - setstrV(ls->L, tv, s); - if (s->reserved > 0) /* Reserved word? */ - return TK_OFS + s->reserved; - return TK_name; - } - switch (ls->current) { - case '\n': - case '\r': - inclinenumber(ls); - continue; - case ' ': - case '\t': - case '\v': - case '\f': - next(ls); - continue; - case '-': - next(ls); - if (ls->current != '-') return '-'; - /* else is a comment */ - next(ls); - if (ls->current == '[') { - int sep = skip_sep(ls); - lj_str_resetbuf(&ls->sb); /* `skip_sep' may dirty the buffer */ - if (sep >= 0) { - read_long_string(ls, NULL, sep); /* long comment */ - lj_str_resetbuf(&ls->sb); - continue; - } - } - /* else short comment */ - while (!currIsNewline(ls) && ls->current != END_OF_STREAM) - next(ls); - continue; - case '[': { - int sep = skip_sep(ls); - if (sep >= 0) { - read_long_string(ls, tv, sep); - return TK_string; - } else if (sep == -1) { - return '['; - } else { - lj_lex_error(ls, TK_string, LJ_ERR_XLDELIM); - continue; - } - } - case '=': - next(ls); - if (ls->current != '=') return '='; else { next(ls); return TK_eq; } - case '<': - next(ls); - if (ls->current != '=') return '<'; else { next(ls); return TK_le; } - case '>': - next(ls); - if (ls->current != '=') return '>'; else { next(ls); return TK_ge; } - case '~': - next(ls); - if (ls->current != '=') return '~'; else { next(ls); return TK_ne; } - case ':': - next(ls); - if (ls->current != ':') return ':'; else { next(ls); return TK_label; } - case '"': - case '\'': - read_string(ls, ls->current, tv); - return TK_string; - case '.': - save_and_next(ls); - if (ls->current == '.') { - next(ls); - if (ls->current == '.') { - next(ls); - return TK_dots; /* ... */ - } - return TK_concat; /* .. */ - } else if (!lj_char_isdigit(ls->current)) { - return '.'; - } else { - lex_number(ls, tv); - return TK_number; - } - case END_OF_STREAM: - return TK_eof; - default: { - int c = ls->current; - next(ls); - return c; /* Single-char tokens (+ - / ...). */ - } - } - } -} - -/* -- Lexer API ----------------------------------------------------------- */ - -/* Setup lexer state. */ -int lj_lex_setup(lua_State *L, LexState *ls) -{ - int header = 0; - ls->L = L; - ls->fs = NULL; - ls->n = 0; - ls->p = NULL; - ls->vstack = NULL; - ls->sizevstack = 0; - ls->vtop = 0; - ls->bcstack = NULL; - ls->sizebcstack = 0; - ls->lookahead = TK_eof; /* No look-ahead token. */ - ls->linenumber = 1; - ls->lastline = 1; - lj_str_resizebuf(ls->L, &ls->sb, LJ_MIN_SBUF); - next(ls); /* Read-ahead first char. */ - if (ls->current == 0xef && ls->n >= 2 && char2int(ls->p[0]) == 0xbb && - char2int(ls->p[1]) == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ - ls->n -= 2; - ls->p += 2; - next(ls); - header = 1; - } - if (ls->current == '#') { /* Skip POSIX #! header line. */ - do { - next(ls); - if (ls->current == END_OF_STREAM) return 0; - } while (!currIsNewline(ls)); - inclinenumber(ls); - header = 1; - } - if (ls->current == LUA_SIGNATURE[0]) { /* Bytecode dump. */ - if (header) { - /* - ** Loading bytecode with an extra header is disabled for security - ** reasons. This may circumvent the usual check for bytecode vs. - ** Lua code by looking at the first char. Since this is a potential - ** security violation no attempt is made to echo the chunkname either. - */ - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_BCBAD)); - lj_err_throw(L, LUA_ERRSYNTAX); - } - return 1; - } - return 0; -} - -/* Cleanup lexer state. */ -void lj_lex_cleanup(lua_State *L, LexState *ls) -{ - global_State *g = G(L); - lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine); - lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo); - lj_str_freebuf(g, &ls->sb); -} - -void lj_lex_next(LexState *ls) -{ - ls->lastline = ls->linenumber; - if (LJ_LIKELY(ls->lookahead == TK_eof)) { /* No lookahead token? */ - ls->token = llex(ls, &ls->tokenval); /* Get next token. */ - } else { /* Otherwise return lookahead token. */ - ls->token = ls->lookahead; - ls->lookahead = TK_eof; - ls->tokenval = ls->lookaheadval; - } -} - -LexToken lj_lex_lookahead(LexState *ls) -{ - lua_assert(ls->lookahead == TK_eof); - ls->lookahead = llex(ls, &ls->lookaheadval); - return ls->lookahead; -} - -const char *lj_lex_token2str(LexState *ls, LexToken token) -{ - if (token > TK_OFS) - return tokennames[token-TK_OFS-1]; - else if (!lj_char_iscntrl(token)) - return lj_str_pushf(ls->L, "%c", token); - else - return lj_str_pushf(ls->L, "char(%d)", token); -} - -void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...) -{ - const char *tok; - va_list argp; - if (token == 0) { - tok = NULL; - } else if (token == TK_name || token == TK_string || token == TK_number) { - save(ls, '\0'); - tok = ls->sb.buf; - } else { - tok = lj_lex_token2str(ls, token); - } - va_start(argp, em); - lj_err_lex(ls->L, ls->chunkname, tok, ls->linenumber, em, argp); - va_end(argp); -} - -void lj_lex_init(lua_State *L) -{ - uint32_t i; - for (i = 0; i < TK_RESERVED; i++) { - GCstr *s = lj_str_newz(L, tokennames[i]); - fixstring(s); /* Reserved words are never collected. */ - s->reserved = (uint8_t)(i+1); - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_lex.h b/third-party/LuaJIT-2.0.2/src/lj_lex.h deleted file mode 100644 index 6e18e4b080..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_lex.h +++ /dev/null @@ -1,85 +0,0 @@ -/* -** Lexical analyzer. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_LEX_H -#define _LJ_LEX_H - -#include - -#include "lj_obj.h" -#include "lj_err.h" - -/* Lua lexer tokens. */ -#define TKDEF(_, __) \ - _(and) _(break) _(do) _(else) _(elseif) _(end) _(false) \ - _(for) _(function) _(goto) _(if) _(in) _(local) _(nil) _(not) _(or) \ - _(repeat) _(return) _(then) _(true) _(until) _(while) \ - __(concat, ..) __(dots, ...) __(eq, ==) __(ge, >=) __(le, <=) __(ne, ~=) \ - __(label, ::) __(number, ) __(name, ) __(string, ) \ - __(eof, ) - -enum { - TK_OFS = 256, -#define TKENUM1(name) TK_##name, -#define TKENUM2(name, sym) TK_##name, -TKDEF(TKENUM1, TKENUM2) -#undef TKENUM1 -#undef TKENUM2 - TK_RESERVED = TK_while - TK_OFS -}; - -typedef int LexToken; - -/* Combined bytecode ins/line. Only used during bytecode generation. */ -typedef struct BCInsLine { - BCIns ins; /* Bytecode instruction. */ - BCLine line; /* Line number for this bytecode. */ -} BCInsLine; - -/* Info for local variables. Only used during bytecode generation. */ -typedef struct VarInfo { - GCRef name; /* Local variable name or goto/label name. */ - BCPos startpc; /* First point where the local variable is active. */ - BCPos endpc; /* First point where the local variable is dead. */ - uint8_t slot; /* Variable slot. */ - uint8_t info; /* Variable/goto/label info. */ -} VarInfo; - -/* Lua lexer state. */ -typedef struct LexState { - struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */ - struct lua_State *L; /* Lua state. */ - TValue tokenval; /* Current token value. */ - TValue lookaheadval; /* Lookahead token value. */ - int current; /* Current character (charint). */ - LexToken token; /* Current token. */ - LexToken lookahead; /* Lookahead token. */ - MSize n; /* Bytes left in input buffer. */ - const char *p; /* Current position in input buffer. */ - SBuf sb; /* String buffer for tokens. */ - lua_Reader rfunc; /* Reader callback. */ - void *rdata; /* Reader callback data. */ - BCLine linenumber; /* Input line counter. */ - BCLine lastline; /* Line of last token. */ - GCstr *chunkname; /* Current chunk name (interned string). */ - const char *chunkarg; /* Chunk name argument. */ - const char *mode; /* Allow loading bytecode (b) and/or source text (t). */ - VarInfo *vstack; /* Stack for names and extents of local variables. */ - MSize sizevstack; /* Size of variable stack. */ - MSize vtop; /* Top of variable stack. */ - BCInsLine *bcstack; /* Stack for bytecode instructions/line numbers. */ - MSize sizebcstack; /* Size of bytecode stack. */ - uint32_t level; /* Syntactical nesting level. */ -} LexState; - -LJ_FUNC int lj_lex_setup(lua_State *L, LexState *ls); -LJ_FUNC void lj_lex_cleanup(lua_State *L, LexState *ls); -LJ_FUNC void lj_lex_next(LexState *ls); -LJ_FUNC LexToken lj_lex_lookahead(LexState *ls); -LJ_FUNC const char *lj_lex_token2str(LexState *ls, LexToken token); -LJ_FUNC_NORET void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...); -LJ_FUNC void lj_lex_init(lua_State *L); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_lib.c b/third-party/LuaJIT-2.0.2/src/lj_lib.c deleted file mode 100644 index 331eaa6a8c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_lib.c +++ /dev/null @@ -1,258 +0,0 @@ -/* -** Library function support. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_lib_c -#define LUA_CORE - -#include "lauxlib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_bc.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_lib.h" - -/* -- Library initialization ---------------------------------------------- */ - -static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize) -{ - if (libname) { - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); - lua_getfield(L, -1, libname); - if (!tvistab(L->top-1)) { - L->top--; - if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL) - lj_err_callerv(L, LJ_ERR_BADMODN, libname); - settabV(L, L->top, tabV(L->top-1)); - L->top++; - lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ - } - L->top--; - settabV(L, L->top-1, tabV(L->top)); - } else { - lua_createtable(L, 0, hsize); - } - return tabV(L->top-1); -} - -void lj_lib_register(lua_State *L, const char *libname, - const uint8_t *p, const lua_CFunction *cf) -{ - GCtab *env = tabref(L->env); - GCfunc *ofn = NULL; - int ffid = *p++; - BCIns *bcff = &L2GG(L)->bcff[*p++]; - GCtab *tab = lib_create_table(L, libname, *p++); - ptrdiff_t tpos = L->top - L->base; - - /* Avoid barriers further down. */ - lj_gc_anybarriert(L, tab); - tab->nomm = 0; - - for (;;) { - uint32_t tag = *p++; - MSize len = tag & LIBINIT_LENMASK; - tag &= LIBINIT_TAGMASK; - if (tag != LIBINIT_STRING) { - const char *name; - MSize nuv = (MSize)(L->top - L->base - tpos); - GCfunc *fn = lj_func_newC(L, nuv, env); - if (nuv) { - L->top = L->base + tpos; - memcpy(fn->c.upvalue, L->top, sizeof(TValue)*nuv); - } - fn->c.ffid = (uint8_t)(ffid++); - name = (const char *)p; - p += len; - if (tag == LIBINIT_CF) - setmref(fn->c.pc, &G(L)->bc_cfunc_int); - else - setmref(fn->c.pc, bcff++); - if (tag == LIBINIT_ASM_) - fn->c.f = ofn->c.f; /* Copy handler from previous function. */ - else - fn->c.f = *cf++; /* Get cf or handler from C function table. */ - if (len) { - /* NOBARRIER: See above for common barrier. */ - setfuncV(L, lj_tab_setstr(L, tab, lj_str_new(L, name, len)), fn); - } - ofn = fn; - } else { - switch (tag | len) { - case LIBINIT_SET: - L->top -= 2; - if (tvisstr(L->top+1) && strV(L->top+1)->len == 0) - env = tabV(L->top); - else /* NOBARRIER: See above for common barrier. */ - copyTV(L, lj_tab_set(L, tab, L->top+1), L->top); - break; - case LIBINIT_NUMBER: - memcpy(&L->top->n, p, sizeof(double)); - L->top++; - p += sizeof(double); - break; - case LIBINIT_COPY: - copyTV(L, L->top, L->top - *p++); - L->top++; - break; - case LIBINIT_LASTCL: - setfuncV(L, L->top++, ofn); - break; - case LIBINIT_FFID: - ffid++; - break; - case LIBINIT_END: - return; - default: - setstrV(L, L->top++, lj_str_new(L, (const char *)p, len)); - p += len; - break; - } - } - } -} - -/* -- Type checks --------------------------------------------------------- */ - -TValue *lj_lib_checkany(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (o >= L->top) - lj_err_arg(L, narg, LJ_ERR_NOVAL); - return o; -} - -GCstr *lj_lib_checkstr(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (o < L->top) { - if (LJ_LIKELY(tvisstr(o))) { - return strV(o); - } else if (tvisnumber(o)) { - GCstr *s = lj_str_fromnumber(L, o); - setstrV(L, o, s); - return s; - } - } - lj_err_argt(L, narg, LUA_TSTRING); - return NULL; /* unreachable */ -} - -GCstr *lj_lib_optstr(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL; -} - -#if LJ_DUALNUM -void lj_lib_checknumber(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && lj_strscan_numberobj(o))) - lj_err_argt(L, narg, LUA_TNUMBER); -} -#endif - -lua_Number lj_lib_checknum(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && - (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o))))) - lj_err_argt(L, narg, LUA_TNUMBER); - if (LJ_UNLIKELY(tvisint(o))) { - lua_Number n = (lua_Number)intV(o); - setnumV(o, n); - return n; - } else { - return numV(o); - } -} - -int32_t lj_lib_checkint(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && lj_strscan_numberobj(o))) - lj_err_argt(L, narg, LUA_TNUMBER); - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else { - int32_t i = lj_num2int(numV(o)); - if (LJ_DUALNUM) setintV(o, i); - return i; - } -} - -int32_t lj_lib_optint(lua_State *L, int narg, int32_t def) -{ - TValue *o = L->base + narg-1; - return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def; -} - -int32_t lj_lib_checkbit(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && lj_strscan_numberobj(o))) - lj_err_argt(L, narg, LUA_TNUMBER); - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else { - int32_t i = lj_num2bit(numV(o)); - if (LJ_DUALNUM) setintV(o, i); - return i; - } -} - -GCfunc *lj_lib_checkfunc(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && tvisfunc(o))) - lj_err_argt(L, narg, LUA_TFUNCTION); - return funcV(o); -} - -GCtab *lj_lib_checktab(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && tvistab(o))) - lj_err_argt(L, narg, LUA_TTABLE); - return tabV(o); -} - -GCtab *lj_lib_checktabornil(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (o < L->top) { - if (tvistab(o)) - return tabV(o); - else if (tvisnil(o)) - return NULL; - } - lj_err_arg(L, narg, LJ_ERR_NOTABN); - return NULL; /* unreachable */ -} - -int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst) -{ - GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg); - if (s) { - const char *opt = strdata(s); - MSize len = s->len; - int i; - for (i = 0; *(const uint8_t *)lst; i++) { - if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0) - return i; - lst += 1+*(const uint8_t *)lst; - } - lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt); - } - return def; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_lib.h b/third-party/LuaJIT-2.0.2/src/lj_lib.h deleted file mode 100644 index 2fe6d2a8d4..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_lib.h +++ /dev/null @@ -1,112 +0,0 @@ -/* -** Library function support. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_LIB_H -#define _LJ_LIB_H - -#include "lj_obj.h" - -/* -** A fallback handler is called by the assembler VM if the fast path fails: -** -** - too few arguments: unrecoverable. -** - wrong argument type: recoverable, if coercion succeeds. -** - bad argument value: unrecoverable. -** - stack overflow: recoverable, if stack reallocation succeeds. -** - extra handling: recoverable. -** -** The unrecoverable cases throw an error with lj_err_arg(), lj_err_argtype(), -** lj_err_caller() or lj_err_callermsg(). -** The recoverable cases return 0 or the number of results + 1. -** The assembler VM retries the fast path only if 0 is returned. -** This time the fallback must not be called again or it gets stuck in a loop. -*/ - -/* Return values from fallback handler. */ -#define FFH_RETRY 0 -#define FFH_UNREACHABLE FFH_RETRY -#define FFH_RES(n) ((n)+1) -#define FFH_TAILCALL (-1) - -LJ_FUNC TValue *lj_lib_checkany(lua_State *L, int narg); -LJ_FUNC GCstr *lj_lib_checkstr(lua_State *L, int narg); -LJ_FUNC GCstr *lj_lib_optstr(lua_State *L, int narg); -#if LJ_DUALNUM -LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg); -#else -#define lj_lib_checknumber(L, narg) lj_lib_checknum((L), (narg)) -#endif -LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg); -LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg); -LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def); -LJ_FUNC int32_t lj_lib_checkbit(lua_State *L, int narg); -LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg); -LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg); -LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg); -LJ_FUNC int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst); - -/* Avoid including lj_frame.h. */ -#define lj_lib_upvalue(L, n) \ - (&gcref((L->base-1)->fr.func)->fn.c.upvalue[(n)-1]) - -#if LJ_TARGET_WINDOWS -#define lj_lib_checkfpu(L) \ - do { setnumV(L->top++, (lua_Number)1437217655); \ - if (lua_tointeger(L, -1) != 1437217655) lj_err_caller(L, LJ_ERR_BADFPU); \ - L->top--; } while (0) -#else -#define lj_lib_checkfpu(L) UNUSED(L) -#endif - -/* Push internal function on the stack. */ -static LJ_AINLINE void lj_lib_pushcc(lua_State *L, lua_CFunction f, - int id, int n) -{ - GCfunc *fn; - lua_pushcclosure(L, f, n); - fn = funcV(L->top-1); - fn->c.ffid = (uint8_t)id; - setmref(fn->c.pc, &G(L)->bc_cfunc_int); -} - -#define lj_lib_pushcf(L, fn, id) (lj_lib_pushcc(L, (fn), (id), 0)) - -/* Library function declarations. Scanned by buildvm. */ -#define LJLIB_CF(name) static int lj_cf_##name(lua_State *L) -#define LJLIB_ASM(name) static int lj_ffh_##name(lua_State *L) -#define LJLIB_ASM_(name) -#define LJLIB_SET(name) -#define LJLIB_PUSH(arg) -#define LJLIB_REC(handler) -#define LJLIB_NOREGUV -#define LJLIB_NOREG - -#define LJ_LIB_REG(L, regname, name) \ - lj_lib_register(L, regname, lj_lib_init_##name, lj_lib_cf_##name) - -LJ_FUNC void lj_lib_register(lua_State *L, const char *libname, - const uint8_t *init, const lua_CFunction *cf); - -/* Library init data tags. */ -#define LIBINIT_LENMASK 0x3f -#define LIBINIT_TAGMASK 0xc0 -#define LIBINIT_CF 0x00 -#define LIBINIT_ASM 0x40 -#define LIBINIT_ASM_ 0x80 -#define LIBINIT_STRING 0xc0 -#define LIBINIT_MAXSTR 0x39 -#define LIBINIT_SET 0xfa -#define LIBINIT_NUMBER 0xfb -#define LIBINIT_COPY 0xfc -#define LIBINIT_LASTCL 0xfd -#define LIBINIT_FFID 0xfe -#define LIBINIT_END 0xff - -/* Exported library functions. */ - -typedef struct RandomState RandomState; -LJ_FUNC uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_load.c b/third-party/LuaJIT-2.0.2/src/lj_load.c deleted file mode 100644 index 9d892678e9..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_load.c +++ /dev/null @@ -1,168 +0,0 @@ -/* -** Load and dump code. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include -#include - -#define lj_load_c -#define LUA_CORE - -#include "lua.h" -#include "lauxlib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_func.h" -#include "lj_frame.h" -#include "lj_vm.h" -#include "lj_lex.h" -#include "lj_bcdump.h" -#include "lj_parse.h" - -/* -- Load Lua source code and bytecode ----------------------------------- */ - -static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud) -{ - LexState *ls = (LexState *)ud; - GCproto *pt; - GCfunc *fn; - int bc; - UNUSED(dummy); - cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ - bc = lj_lex_setup(L, ls); - if (ls->mode && !strchr(ls->mode, bc ? 'b' : 't')) { - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_XMODE)); - lj_err_throw(L, LUA_ERRSYNTAX); - } - pt = bc ? lj_bcread(ls) : lj_parse(ls); - fn = lj_func_newL_empty(L, pt, tabref(L->env)); - /* Don't combine above/below into one statement. */ - setfuncV(L, L->top++, fn); - return NULL; -} - -LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data, - const char *chunkname, const char *mode) -{ - LexState ls; - int status; - ls.rfunc = reader; - ls.rdata = data; - ls.chunkarg = chunkname ? chunkname : "?"; - ls.mode = mode; - lj_str_initbuf(&ls.sb); - status = lj_vm_cpcall(L, NULL, &ls, cpparser); - lj_lex_cleanup(L, &ls); - lj_gc_check(L); - return status; -} - -LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, - const char *chunkname) -{ - return lua_loadx(L, reader, data, chunkname, NULL); -} - -typedef struct FileReaderCtx { - FILE *fp; - char buf[LUAL_BUFFERSIZE]; -} FileReaderCtx; - -static const char *reader_file(lua_State *L, void *ud, size_t *size) -{ - FileReaderCtx *ctx = (FileReaderCtx *)ud; - UNUSED(L); - if (feof(ctx->fp)) return NULL; - *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp); - return *size > 0 ? ctx->buf : NULL; -} - -LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, - const char *mode) -{ - FileReaderCtx ctx; - int status; - const char *chunkname; - if (filename) { - ctx.fp = fopen(filename, "rb"); - if (ctx.fp == NULL) { - lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno)); - return LUA_ERRFILE; - } - chunkname = lua_pushfstring(L, "@%s", filename); - } else { - ctx.fp = stdin; - chunkname = "=stdin"; - } - status = lua_loadx(L, reader_file, &ctx, chunkname, mode); - if (ferror(ctx.fp)) { - L->top -= filename ? 2 : 1; - lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno)); - if (filename) - fclose(ctx.fp); - return LUA_ERRFILE; - } - if (filename) { - L->top--; - copyTV(L, L->top-1, L->top); - fclose(ctx.fp); - } - return status; -} - -LUALIB_API int luaL_loadfile(lua_State *L, const char *filename) -{ - return luaL_loadfilex(L, filename, NULL); -} - -typedef struct StringReaderCtx { - const char *str; - size_t size; -} StringReaderCtx; - -static const char *reader_string(lua_State *L, void *ud, size_t *size) -{ - StringReaderCtx *ctx = (StringReaderCtx *)ud; - UNUSED(L); - if (ctx->size == 0) return NULL; - *size = ctx->size; - ctx->size = 0; - return ctx->str; -} - -LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size, - const char *name, const char *mode) -{ - StringReaderCtx ctx; - ctx.str = buf; - ctx.size = size; - return lua_loadx(L, reader_string, &ctx, name, mode); -} - -LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, - const char *name) -{ - return luaL_loadbufferx(L, buf, size, name, NULL); -} - -LUALIB_API int luaL_loadstring(lua_State *L, const char *s) -{ - return luaL_loadbuffer(L, s, strlen(s), s); -} - -/* -- Dump bytecode ------------------------------------------------------- */ - -LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) -{ - cTValue *o = L->top-1; - api_check(L, L->top > L->base); - if (tvisfunc(o) && isluafunc(funcV(o))) - return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0); - else - return 1; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_mcode.c b/third-party/LuaJIT-2.0.2/src/lj_mcode.c deleted file mode 100644 index cb79e8cdd7..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_mcode.c +++ /dev/null @@ -1,360 +0,0 @@ -/* -** Machine code management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_mcode_c -#define LUA_CORE - -#include "lj_obj.h" -#if LJ_HASJIT -#include "lj_gc.h" -#include "lj_jit.h" -#include "lj_mcode.h" -#include "lj_trace.h" -#include "lj_dispatch.h" -#endif -#if LJ_HASJIT || LJ_HASFFI -#include "lj_vm.h" -#endif - -/* -- OS-specific functions ----------------------------------------------- */ - -#if LJ_HASJIT || LJ_HASFFI - -/* Define this if you want to run LuaJIT with Valgrind. */ -#ifdef LUAJIT_USE_VALGRIND -#include -#endif - -#if LJ_TARGET_IOS -void sys_icache_invalidate(void *start, size_t len); -#endif - -/* Synchronize data/instruction cache. */ -void lj_mcode_sync(void *start, void *end) -{ -#ifdef LUAJIT_USE_VALGRIND - VALGRIND_DISCARD_TRANSLATIONS(start, (char *)end-(char *)start); -#endif -#if LJ_TARGET_X86ORX64 - UNUSED(start); UNUSED(end); -#elif LJ_TARGET_IOS - sys_icache_invalidate(start, (char *)end-(char *)start); -#elif LJ_TARGET_PPC - lj_vm_cachesync(start, end); -#elif defined(__GNUC__) - __clear_cache(start, end); -#else -#error "Missing builtin to flush instruction cache" -#endif -} - -#endif - -#if LJ_HASJIT - -#if LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#define MCPROT_RW PAGE_READWRITE -#define MCPROT_RX PAGE_EXECUTE_READ -#define MCPROT_RWX PAGE_EXECUTE_READWRITE - -static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, DWORD prot) -{ - void *p = VirtualAlloc((void *)hint, sz, - MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, prot); - if (!p && !hint) - lj_trace_err(J, LJ_TRERR_MCODEAL); - return p; -} - -static void mcode_free(jit_State *J, void *p, size_t sz) -{ - UNUSED(J); UNUSED(sz); - VirtualFree(p, 0, MEM_RELEASE); -} - -static void mcode_setprot(void *p, size_t sz, DWORD prot) -{ - DWORD oprot; - VirtualProtect(p, sz, prot, &oprot); -} - -#elif LJ_TARGET_POSIX - -#include - -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif - -#define MCPROT_RW (PROT_READ|PROT_WRITE) -#define MCPROT_RX (PROT_READ|PROT_EXEC) -#define MCPROT_RWX (PROT_READ|PROT_WRITE|PROT_EXEC) - -static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) -{ - void *p = mmap((void *)hint, sz, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (p == MAP_FAILED) { - if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL); - p = NULL; - } - return p; -} - -static void mcode_free(jit_State *J, void *p, size_t sz) -{ - UNUSED(J); - munmap(p, sz); -} - -static void mcode_setprot(void *p, size_t sz, int prot) -{ - mprotect(p, sz, prot); -} - -#elif LJ_64 - -#error "Missing OS support for explicit placement of executable memory" - -#else - -/* Fallback allocator. This will fail if memory is not executable by default. */ -#define LUAJIT_UNPROTECT_MCODE -#define MCPROT_RW 0 -#define MCPROT_RX 0 -#define MCPROT_RWX 0 - -static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) -{ - UNUSED(hint); UNUSED(prot); - return lj_mem_new(J->L, sz); -} - -static void mcode_free(jit_State *J, void *p, size_t sz) -{ - lj_mem_free(J2G(J), p, sz); -} - -#define mcode_setprot(p, sz, prot) UNUSED(p) - -#endif - -/* -- MCode area protection ----------------------------------------------- */ - -/* Define this ONLY if the page protection twiddling becomes a bottleneck. */ -#ifdef LUAJIT_UNPROTECT_MCODE - -/* It's generally considered to be a potential security risk to have -** pages with simultaneous write *and* execute access in a process. -** -** Do not even think about using this mode for server processes or -** apps handling untrusted external data (such as a browser). -** -** The security risk is not in LuaJIT itself -- but if an adversary finds -** any *other* flaw in your C application logic, then any RWX memory page -** simplifies writing an exploit considerably. -*/ -#define MCPROT_GEN MCPROT_RWX -#define MCPROT_RUN MCPROT_RWX - -static void mcode_protect(jit_State *J, int prot) -{ - UNUSED(J); UNUSED(prot); -} - -#else - -/* This is the default behaviour and much safer: -** -** Most of the time the memory pages holding machine code are executable, -** but NONE of them is writable. -** -** The current memory area is marked read-write (but NOT executable) only -** during the short time window while the assembler generates machine code. -*/ -#define MCPROT_GEN MCPROT_RW -#define MCPROT_RUN MCPROT_RX - -/* Change protection of MCode area. */ -static void mcode_protect(jit_State *J, int prot) -{ - if (J->mcprot != prot) { - mcode_setprot(J->mcarea, J->szmcarea, prot); - J->mcprot = prot; - } -} - -#endif - -/* -- MCode area allocation ----------------------------------------------- */ - -#if LJ_TARGET_X64 -#define mcode_validptr(p) ((p) && (uintptr_t)(p) < (uintptr_t)1<<47) -#else -#define mcode_validptr(p) ((p) && (uintptr_t)(p) < 0xffff0000) -#endif - -#ifdef LJ_TARGET_JUMPRANGE - -/* Get memory within relative jump distance of our code in 64 bit mode. */ -static void *mcode_alloc(jit_State *J, size_t sz) -{ - /* Target an address in the static assembler code (64K aligned). - ** Try addresses within a distance of target-range/2+1MB..target+range/2-1MB. - ** Use half the jump range so every address in the range can reach any other. - */ -#if LJ_TARGET_MIPS - /* Use the middle of the 256MB-aligned region. */ - uintptr_t target = ((uintptr_t)(void *)lj_vm_exit_handler & 0xf0000000u) + - 0x08000000u; -#else - uintptr_t target = (uintptr_t)(void *)lj_vm_exit_handler & ~(uintptr_t)0xffff; -#endif - const uintptr_t range = (1u << (LJ_TARGET_JUMPRANGE-1)) - (1u << 21); - /* First try a contiguous area below the last one. */ - uintptr_t hint = J->mcarea ? (uintptr_t)J->mcarea - sz : 0; - int i; - for (i = 0; i < 32; i++) { /* 32 attempts ought to be enough ... */ - if (mcode_validptr(hint)) { - void *p = mcode_alloc_at(J, hint, sz, MCPROT_GEN); - - if (mcode_validptr(p) && - ((uintptr_t)p + sz - target < range || target - (uintptr_t)p < range)) - return p; - if (p) mcode_free(J, p, sz); /* Free badly placed area. */ - } - /* Next try probing pseudo-random addresses. */ - do { - hint = (0x78fb ^ LJ_PRNG_BITS(J, 15)) << 16; /* 64K aligned. */ - } while (!(hint + sz < range)); - hint = target + hint - (range>>1); - } - lj_trace_err(J, LJ_TRERR_MCODEAL); /* Give up. OS probably ignores hints? */ - return NULL; -} - -#else - -/* All memory addresses are reachable by relative jumps. */ -#define mcode_alloc(J, sz) mcode_alloc_at((J), 0, (sz), MCPROT_GEN) - -#endif - -/* -- MCode area management ----------------------------------------------- */ - -/* Linked list of MCode areas. */ -typedef struct MCLink { - MCode *next; /* Next area. */ - size_t size; /* Size of current area. */ -} MCLink; - -/* Allocate a new MCode area. */ -static void mcode_allocarea(jit_State *J) -{ - MCode *oldarea = J->mcarea; - size_t sz = (size_t)J->param[JIT_P_sizemcode] << 10; - sz = (sz + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1); - J->mcarea = (MCode *)mcode_alloc(J, sz); - J->szmcarea = sz; - J->mcprot = MCPROT_GEN; - J->mctop = (MCode *)((char *)J->mcarea + J->szmcarea); - J->mcbot = (MCode *)((char *)J->mcarea + sizeof(MCLink)); - ((MCLink *)J->mcarea)->next = oldarea; - ((MCLink *)J->mcarea)->size = sz; - J->szallmcarea += sz; -} - -/* Free all MCode areas. */ -void lj_mcode_free(jit_State *J) -{ - MCode *mc = J->mcarea; - J->mcarea = NULL; - J->szallmcarea = 0; - while (mc) { - MCode *next = ((MCLink *)mc)->next; - mcode_free(J, mc, ((MCLink *)mc)->size); - mc = next; - } -} - -/* -- MCode transactions -------------------------------------------------- */ - -/* Reserve the remainder of the current MCode area. */ -MCode *lj_mcode_reserve(jit_State *J, MCode **lim) -{ - if (!J->mcarea) - mcode_allocarea(J); - else - mcode_protect(J, MCPROT_GEN); - *lim = J->mcbot; - return J->mctop; -} - -/* Commit the top part of the current MCode area. */ -void lj_mcode_commit(jit_State *J, MCode *top) -{ - J->mctop = top; - mcode_protect(J, MCPROT_RUN); -} - -/* Abort the reservation. */ -void lj_mcode_abort(jit_State *J) -{ - mcode_protect(J, MCPROT_RUN); -} - -/* Set/reset protection to allow patching of MCode areas. */ -MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish) -{ -#ifdef LUAJIT_UNPROTECT_MCODE - UNUSED(J); UNUSED(ptr); UNUSED(finish); - return NULL; -#else - if (finish) { - if (J->mcarea == ptr) - mcode_protect(J, MCPROT_RUN); - else - mcode_setprot(ptr, ((MCLink *)ptr)->size, MCPROT_RUN); - return NULL; - } else { - MCode *mc = J->mcarea; - /* Try current area first to use the protection cache. */ - if (ptr >= mc && ptr < (MCode *)((char *)mc + J->szmcarea)) { - mcode_protect(J, MCPROT_GEN); - return mc; - } - /* Otherwise search through the list of MCode areas. */ - for (;;) { - mc = ((MCLink *)mc)->next; - lua_assert(mc != NULL); - if (ptr >= mc && ptr < (MCode *)((char *)mc + ((MCLink *)mc)->size)) { - mcode_setprot(mc, ((MCLink *)mc)->size, MCPROT_GEN); - return mc; - } - } - } -#endif -} - -/* Limit of MCode reservation reached. */ -void lj_mcode_limiterr(jit_State *J, size_t need) -{ - size_t sizemcode, maxmcode; - lj_mcode_abort(J); - sizemcode = (size_t)J->param[JIT_P_sizemcode] << 10; - sizemcode = (sizemcode + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1); - maxmcode = (size_t)J->param[JIT_P_maxmcode] << 10; - if ((size_t)need > sizemcode) - lj_trace_err(J, LJ_TRERR_MCODEOV); /* Too long for any area. */ - if (J->szallmcarea + sizemcode > maxmcode) - lj_trace_err(J, LJ_TRERR_MCODEAL); - mcode_allocarea(J); - lj_trace_err(J, LJ_TRERR_MCODELM); /* Retry with new area. */ -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_mcode.h b/third-party/LuaJIT-2.0.2/src/lj_mcode.h deleted file mode 100644 index 2ba371bc1d..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_mcode.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -** Machine code management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_MCODE_H -#define _LJ_MCODE_H - -#include "lj_obj.h" - -#if LJ_HASJIT || LJ_HASFFI -LJ_FUNC void lj_mcode_sync(void *start, void *end); -#endif - -#if LJ_HASJIT - -#include "lj_jit.h" - -LJ_FUNC void lj_mcode_free(jit_State *J); -LJ_FUNC MCode *lj_mcode_reserve(jit_State *J, MCode **lim); -LJ_FUNC void lj_mcode_commit(jit_State *J, MCode *m); -LJ_FUNC void lj_mcode_abort(jit_State *J); -LJ_FUNC MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish); -LJ_FUNC_NORET void lj_mcode_limiterr(jit_State *J, size_t need); - -#define lj_mcode_commitbot(J, m) (J->mcbot = (m)) - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_meta.c b/third-party/LuaJIT-2.0.2/src/lj_meta.c deleted file mode 100644 index 441d571ac7..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_meta.c +++ /dev/null @@ -1,466 +0,0 @@ -/* -** Metamethod handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_meta_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_vm.h" -#include "lj_strscan.h" - -/* -- Metamethod handling ------------------------------------------------- */ - -/* String interning of metamethod names for fast indexing. */ -void lj_meta_init(lua_State *L) -{ -#define MMNAME(name) "__" #name - const char *metanames = MMDEF(MMNAME); -#undef MMNAME - global_State *g = G(L); - const char *p, *q; - uint32_t mm; - for (mm = 0, p = metanames; *p; mm++, p = q) { - GCstr *s; - for (q = p+2; *q && *q != '_'; q++) ; - s = lj_str_new(L, p, (size_t)(q-p)); - /* NOBARRIER: g->gcroot[] is a GC root. */ - setgcref(g->gcroot[GCROOT_MMNAME+mm], obj2gco(s)); - } -} - -/* Negative caching of a few fast metamethods. See the lj_meta_fast() macro. */ -cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name) -{ - cTValue *mo = lj_tab_getstr(mt, name); - lua_assert(mm <= MM_FAST); - if (!mo || tvisnil(mo)) { /* No metamethod? */ - mt->nomm |= (uint8_t)(1u<metatable); - else if (tvisudata(o)) - mt = tabref(udataV(o)->metatable); - else - mt = tabref(basemt_obj(G(L), o)); - if (mt) { - cTValue *mo = lj_tab_getstr(mt, mmname_str(G(L), mm)); - if (mo) - return mo; - } - return niltv(L); -} - -#if LJ_HASFFI -/* Tailcall from C function. */ -int lj_meta_tailcall(lua_State *L, cTValue *tv) -{ - TValue *base = L->base; - TValue *top = L->top; - const BCIns *pc = frame_pc(base-1); /* Preserve old PC from frame. */ - copyTV(L, base-1, tv); /* Replace frame with new object. */ - top->u32.lo = LJ_CONT_TAILCALL; - setframe_pc(top, pc); - setframe_gc(top+1, obj2gco(L)); /* Dummy frame object. */ - setframe_ftsz(top+1, (int)((char *)(top+2) - (char *)base) + FRAME_CONT); - L->base = L->top = top+2; - /* - ** before: [old_mo|PC] [... ...] - ** ^base ^top - ** after: [new_mo|itype] [... ...] [NULL|PC] [dummy|delta] - ** ^base/top - ** tailcall: [new_mo|PC] [... ...] - ** ^base ^top - */ - return 0; -} -#endif - -/* Setup call to metamethod to be run by Assembler VM. */ -static TValue *mmcall(lua_State *L, ASMFunction cont, cTValue *mo, - cTValue *a, cTValue *b) -{ - /* - ** |-- framesize -> top top+1 top+2 top+3 - ** before: [func slots ...] - ** mm setup: [func slots ...] [cont|?] [mo|tmtype] [a] [b] - ** in asm: [func slots ...] [cont|PC] [mo|delta] [a] [b] - ** ^-- func base ^-- mm base - ** after mm: [func slots ...] [result] - ** ^-- copy to base[PC_RA] --/ for lj_cont_ra - ** istruecond + branch for lj_cont_cond* - ** ignore for lj_cont_nop - ** next PC: [func slots ...] - */ - TValue *top = L->top; - if (curr_funcisL(L)) top = curr_topL(L); - setcont(top, cont); /* Assembler VM stores PC in upper word. */ - copyTV(L, top+1, mo); /* Store metamethod and two arguments. */ - copyTV(L, top+2, a); - copyTV(L, top+3, b); - return top+2; /* Return new base. */ -} - -/* -- C helpers for some instructions, called from assembler VM ----------- */ - -/* Helper for TGET*. __index chain and metamethod. */ -cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k) -{ - int loop; - for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { - cTValue *mo; - if (LJ_LIKELY(tvistab(o))) { - GCtab *t = tabV(o); - cTValue *tv = lj_tab_get(L, t, k); - if (!tvisnil(tv) || - !(mo = lj_meta_fast(L, tabref(t->metatable), MM_index))) - return tv; - } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_index))) { - lj_err_optype(L, o, LJ_ERR_OPINDEX); - return NULL; /* unreachable */ - } - if (tvisfunc(mo)) { - L->top = mmcall(L, lj_cont_ra, mo, o, k); - return NULL; /* Trigger metamethod call. */ - } - o = mo; - } - lj_err_msg(L, LJ_ERR_GETLOOP); - return NULL; /* unreachable */ -} - -/* Helper for TSET*. __newindex chain and metamethod. */ -TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k) -{ - TValue tmp; - int loop; - for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { - cTValue *mo; - if (LJ_LIKELY(tvistab(o))) { - GCtab *t = tabV(o); - cTValue *tv = lj_tab_get(L, t, k); - if (LJ_LIKELY(!tvisnil(tv))) { - t->nomm = 0; /* Invalidate negative metamethod cache. */ - lj_gc_anybarriert(L, t); - return (TValue *)tv; - } else if (!(mo = lj_meta_fast(L, tabref(t->metatable), MM_newindex))) { - t->nomm = 0; /* Invalidate negative metamethod cache. */ - lj_gc_anybarriert(L, t); - if (tv != niltv(L)) - return (TValue *)tv; - if (tvisnil(k)) lj_err_msg(L, LJ_ERR_NILIDX); - else if (tvisint(k)) { setnumV(&tmp, (lua_Number)intV(k)); k = &tmp; } - else if (tvisnum(k) && tvisnan(k)) lj_err_msg(L, LJ_ERR_NANIDX); - return lj_tab_newkey(L, t, k); - } - } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_newindex))) { - lj_err_optype(L, o, LJ_ERR_OPINDEX); - return NULL; /* unreachable */ - } - if (tvisfunc(mo)) { - L->top = mmcall(L, lj_cont_nop, mo, o, k); - /* L->top+2 = v filled in by caller. */ - return NULL; /* Trigger metamethod call. */ - } - copyTV(L, &tmp, mo); - o = &tmp; - } - lj_err_msg(L, LJ_ERR_SETLOOP); - return NULL; /* unreachable */ -} - -static cTValue *str2num(cTValue *o, TValue *n) -{ - if (tvisnum(o)) - return o; - else if (tvisint(o)) - return (setnumV(n, (lua_Number)intV(o)), n); - else if (tvisstr(o) && lj_strscan_num(strV(o), n)) - return n; - else - return NULL; -} - -/* Helper for arithmetic instructions. Coercion, metamethod. */ -TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, cTValue *rc, - BCReg op) -{ - MMS mm = bcmode_mm(op); - TValue tempb, tempc; - cTValue *b, *c; - if ((b = str2num(rb, &tempb)) != NULL && - (c = str2num(rc, &tempc)) != NULL) { /* Try coercion first. */ - setnumV(ra, lj_vm_foldarith(numV(b), numV(c), (int)mm-MM_add)); - return NULL; - } else { - cTValue *mo = lj_meta_lookup(L, rb, mm); - if (tvisnil(mo)) { - mo = lj_meta_lookup(L, rc, mm); - if (tvisnil(mo)) { - if (str2num(rb, &tempb) == NULL) rc = rb; - lj_err_optype(L, rc, LJ_ERR_OPARITH); - return NULL; /* unreachable */ - } - } - return mmcall(L, lj_cont_ra, mo, rb, rc); - } -} - -/* In-place coercion of a number to a string. */ -static LJ_AINLINE int tostring(lua_State *L, TValue *o) -{ - if (tvisstr(o)) { - return 1; - } else if (tvisnumber(o)) { - setstrV(L, o, lj_str_fromnumber(L, o)); - return 1; - } else { - return 0; - } -} - -/* Helper for CAT. Coercion, iterative concat, __concat metamethod. */ -TValue *lj_meta_cat(lua_State *L, TValue *top, int left) -{ - int fromc = 0; - if (left < 0) { left = -left; fromc = 1; } - do { - int n = 1; - if (!(tvisstr(top-1) || tvisnumber(top-1)) || !tostring(L, top)) { - cTValue *mo = lj_meta_lookup(L, top-1, MM_concat); - if (tvisnil(mo)) { - mo = lj_meta_lookup(L, top, MM_concat); - if (tvisnil(mo)) { - if (tvisstr(top-1) || tvisnumber(top-1)) top++; - lj_err_optype(L, top-1, LJ_ERR_OPCAT); - return NULL; /* unreachable */ - } - } - /* One of the top two elements is not a string, call __cat metamethod: - ** - ** before: [...][CAT stack .........................] - ** top-1 top top+1 top+2 - ** pick two: [...][CAT stack ...] [o1] [o2] - ** setup mm: [...][CAT stack ...] [cont|?] [mo|tmtype] [o1] [o2] - ** in asm: [...][CAT stack ...] [cont|PC] [mo|delta] [o1] [o2] - ** ^-- func base ^-- mm base - ** after mm: [...][CAT stack ...] <--push-- [result] - ** next step: [...][CAT stack .............] - */ - copyTV(L, top+2, top); /* Careful with the order of stack copies! */ - copyTV(L, top+1, top-1); - copyTV(L, top, mo); - setcont(top-1, lj_cont_cat); - return top+1; /* Trigger metamethod call. */ - } else if (strV(top)->len == 0) { /* Shortcut. */ - (void)tostring(L, top-1); - } else { - /* Pick as many strings as possible from the top and concatenate them: - ** - ** before: [...][CAT stack ...........................] - ** pick str: [...][CAT stack ...] [...... strings ......] - ** concat: [...][CAT stack ...] [result] - ** next step: [...][CAT stack ............] - */ - MSize tlen = strV(top)->len; - char *buffer; - int i; - for (n = 1; n <= left && tostring(L, top-n); n++) { - MSize len = strV(top-n)->len; - if (len >= LJ_MAX_STR - tlen) - lj_err_msg(L, LJ_ERR_STROV); - tlen += len; - } - buffer = lj_str_needbuf(L, &G(L)->tmpbuf, tlen); - n--; - tlen = 0; - for (i = n; i >= 0; i--) { - MSize len = strV(top-i)->len; - memcpy(buffer + tlen, strVdata(top-i), len); - tlen += len; - } - setstrV(L, top-n, lj_str_new(L, buffer, tlen)); - } - left -= n; - top -= n; - } while (left >= 1); - if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) { - if (!fromc) L->top = curr_topL(L); - lj_gc_step(L); - } - return NULL; -} - -/* Helper for LEN. __len metamethod. */ -TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o) -{ - cTValue *mo = lj_meta_lookup(L, o, MM_len); - if (tvisnil(mo)) { - if (LJ_52 && tvistab(o)) - tabref(tabV(o)->metatable)->nomm |= (uint8_t)(1u<gch.metatable), MM_eq); - if (mo) { - TValue *top; - uint32_t it; - if (tabref(o1->gch.metatable) != tabref(o2->gch.metatable)) { - cTValue *mo2 = lj_meta_fast(L, tabref(o2->gch.metatable), MM_eq); - if (mo2 == NULL || !lj_obj_equal(mo, mo2)) - return (TValue *)(intptr_t)ne; - } - top = curr_top(L); - setcont(top, ne ? lj_cont_condf : lj_cont_condt); - copyTV(L, top+1, mo); - it = ~(uint32_t)o1->gch.gct; - setgcV(L, top+2, o1, it); - setgcV(L, top+3, o2, it); - return top+2; /* Trigger metamethod call. */ - } - return (TValue *)(intptr_t)ne; -} - -#if LJ_HASFFI -TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins) -{ - ASMFunction cont = (bc_op(ins) & 1) ? lj_cont_condf : lj_cont_condt; - int op = (int)bc_op(ins) & ~1; - TValue tv; - cTValue *mo, *o2, *o1 = &L->base[bc_a(ins)]; - cTValue *o1mm = o1; - if (op == BC_ISEQV) { - o2 = &L->base[bc_d(ins)]; - if (!tviscdata(o1mm)) o1mm = o2; - } else if (op == BC_ISEQS) { - setstrV(L, &tv, gco2str(proto_kgc(curr_proto(L), ~(ptrdiff_t)bc_d(ins)))); - o2 = &tv; - } else if (op == BC_ISEQN) { - o2 = &mref(curr_proto(L)->k, cTValue)[bc_d(ins)]; - } else { - lua_assert(op == BC_ISEQP); - setitype(&tv, ~bc_d(ins)); - o2 = &tv; - } - mo = lj_meta_lookup(L, o1mm, MM_eq); - if (LJ_LIKELY(!tvisnil(mo))) - return mmcall(L, cont, mo, o1, o2); - else - return (TValue *)(intptr_t)(bc_op(ins) & 1); -} -#endif - -/* Helper for ordered comparisons. String compare, __lt/__le metamethods. */ -TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op) -{ - if (LJ_HASFFI && (tviscdata(o1) || tviscdata(o2))) { - ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt; - MMS mm = (op & 2) ? MM_le : MM_lt; - cTValue *mo = lj_meta_lookup(L, tviscdata(o1) ? o1 : o2, mm); - if (LJ_UNLIKELY(tvisnil(mo))) goto err; - return mmcall(L, cont, mo, o1, o2); - } else if (LJ_52 || itype(o1) == itype(o2)) { - /* Never called with two numbers. */ - if (tvisstr(o1) && tvisstr(o2)) { - int32_t res = lj_str_cmp(strV(o1), strV(o2)); - return (TValue *)(intptr_t)(((op&2) ? res <= 0 : res < 0) ^ (op&1)); - } else { - trymt: - while (1) { - ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt; - MMS mm = (op & 2) ? MM_le : MM_lt; - cTValue *mo = lj_meta_lookup(L, o1, mm); -#if LJ_52 - if (tvisnil(mo) && tvisnil((mo = lj_meta_lookup(L, o2, mm)))) -#else - cTValue *mo2 = lj_meta_lookup(L, o2, mm); - if (tvisnil(mo) || !lj_obj_equal(mo, mo2)) -#endif - { - if (op & 2) { /* MM_le not found: retry with MM_lt. */ - cTValue *ot = o1; o1 = o2; o2 = ot; /* Swap operands. */ - op ^= 3; /* Use LT and flip condition. */ - continue; - } - goto err; - } - return mmcall(L, cont, mo, o1, o2); - } - } - } else if (tvisbool(o1) && tvisbool(o2)) { - goto trymt; - } else { - err: - lj_err_comp(L, o1, o2); - return NULL; - } -} - -/* Helper for calls. __call metamethod. */ -void lj_meta_call(lua_State *L, TValue *func, TValue *top) -{ - cTValue *mo = lj_meta_lookup(L, func, MM_call); - TValue *p; - if (!tvisfunc(mo)) - lj_err_optype_call(L, func); - for (p = top; p > func; p--) copyTV(L, p, p-1); - copyTV(L, func, mo); -} - -/* Helper for FORI. Coercion. */ -void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o) -{ - if (!lj_strscan_numberobj(o)) lj_err_msg(L, LJ_ERR_FORINIT); - if (!lj_strscan_numberobj(o+1)) lj_err_msg(L, LJ_ERR_FORLIM); - if (!lj_strscan_numberobj(o+2)) lj_err_msg(L, LJ_ERR_FORSTEP); - if (LJ_DUALNUM) { - /* Ensure all slots are integers or all slots are numbers. */ - int32_t k[3]; - int nint = 0; - ptrdiff_t i; - for (i = 0; i <= 2; i++) { - if (tvisint(o+i)) { - k[i] = intV(o+i); nint++; - } else { - k[i] = lj_num2int(numV(o+i)); nint += ((lua_Number)k[i] == numV(o+i)); - } - } - if (nint == 3) { /* Narrow to integers. */ - setintV(o, k[0]); - setintV(o+1, k[1]); - setintV(o+2, k[2]); - } else if (nint != 0) { /* Widen to numbers. */ - if (tvisint(o)) setnumV(o, (lua_Number)intV(o)); - if (tvisint(o+1)) setnumV(o+1, (lua_Number)intV(o+1)); - if (tvisint(o+2)) setnumV(o+2, (lua_Number)intV(o+2)); - } - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_meta.h b/third-party/LuaJIT-2.0.2/src/lj_meta.h deleted file mode 100644 index 6af5e51443..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_meta.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -** Metamethod handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_META_H -#define _LJ_META_H - -#include "lj_obj.h" - -/* Metamethod handling */ -LJ_FUNC void lj_meta_init(lua_State *L); -LJ_FUNC cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name); -LJ_FUNC cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm); -#if LJ_HASFFI -LJ_FUNC int lj_meta_tailcall(lua_State *L, cTValue *tv); -#endif - -#define lj_meta_fastg(g, mt, mm) \ - ((mt) == NULL ? NULL : ((mt)->nomm & (1u<<(mm))) ? NULL : \ - lj_meta_cache(mt, mm, mmname_str(g, mm))) -#define lj_meta_fast(L, mt, mm) lj_meta_fastg(G(L), mt, mm) - -/* C helpers for some instructions, called from assembler VM. */ -LJ_FUNCA cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k); -LJ_FUNCA TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k); -LJ_FUNCA TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, - cTValue *rc, BCReg op); -LJ_FUNCA TValue *lj_meta_cat(lua_State *L, TValue *top, int left); -LJ_FUNCA TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o); -LJ_FUNCA TValue *lj_meta_equal(lua_State *L, GCobj *o1, GCobj *o2, int ne); -LJ_FUNCA TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins); -LJ_FUNCA TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op); -LJ_FUNCA void lj_meta_call(lua_State *L, TValue *func, TValue *top); -LJ_FUNCA void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_obj.c b/third-party/LuaJIT-2.0.2/src/lj_obj.c deleted file mode 100644 index 322b7bec7c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_obj.c +++ /dev/null @@ -1,35 +0,0 @@ -/* -** Miscellaneous object handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_obj_c -#define LUA_CORE - -#include "lj_obj.h" - -/* Object type names. */ -LJ_DATADEF const char *const lj_obj_typename[] = { /* ORDER LUA_T */ - "no value", "nil", "boolean", "userdata", "number", "string", - "table", "function", "userdata", "thread", "proto", "cdata" -}; - -LJ_DATADEF const char *const lj_obj_itypename[] = { /* ORDER LJ_T */ - "nil", "boolean", "boolean", "userdata", "string", "upval", "thread", - "proto", "function", "trace", "cdata", "table", "userdata", "number" -}; - -/* Compare two objects without calling metamethods. */ -int lj_obj_equal(cTValue *o1, cTValue *o2) -{ - if (itype(o1) == itype(o2)) { - if (tvispri(o1)) - return 1; - if (!tvisnum(o1)) - return gcrefeq(o1->gcr, o2->gcr); - } else if (!tvisnumber(o1) || !tvisnumber(o2)) { - return 0; - } - return numberVnum(o1) == numberVnum(o2); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_obj.h b/third-party/LuaJIT-2.0.2/src/lj_obj.h deleted file mode 100644 index b967819d07..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_obj.h +++ /dev/null @@ -1,856 +0,0 @@ -/* -** LuaJIT VM tags, values and objects. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#ifndef _LJ_OBJ_H -#define _LJ_OBJ_H - -#include "lua.h" -#include "lj_def.h" -#include "lj_arch.h" - -/* -- Memory references (32 bit address space) ---------------------------- */ - -/* Memory size. */ -typedef uint32_t MSize; - -/* Memory reference */ -typedef struct MRef { - uint32_t ptr32; /* Pseudo 32 bit pointer. */ -} MRef; - -#define mref(r, t) ((t *)(void *)(uintptr_t)(r).ptr32) - -#define setmref(r, p) ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p)) -#define setmrefr(r, v) ((r).ptr32 = (v).ptr32) - -/* -- GC object references (32 bit address space) ------------------------- */ - -/* GCobj reference */ -typedef struct GCRef { - uint32_t gcptr32; /* Pseudo 32 bit pointer. */ -} GCRef; - -/* Common GC header for all collectable objects. */ -#define GCHeader GCRef nextgc; uint8_t marked; uint8_t gct -/* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */ - -#define gcref(r) ((GCobj *)(uintptr_t)(r).gcptr32) -#define gcrefp(r, t) ((t *)(void *)(uintptr_t)(r).gcptr32) -#define gcrefu(r) ((r).gcptr32) -#define gcrefi(r) ((int32_t)(r).gcptr32) -#define gcrefeq(r1, r2) ((r1).gcptr32 == (r2).gcptr32) -#define gcnext(gc) (gcref((gc)->gch.nextgc)) - -#define setgcref(r, gc) ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch) -#define setgcrefi(r, i) ((r).gcptr32 = (uint32_t)(i)) -#define setgcrefp(r, p) ((r).gcptr32 = (uint32_t)(uintptr_t)(p)) -#define setgcrefnull(r) ((r).gcptr32 = 0) -#define setgcrefr(r, v) ((r).gcptr32 = (v).gcptr32) - -/* IMPORTANT NOTE: -** -** All uses of the setgcref* macros MUST be accompanied with a write barrier. -** -** This is to ensure the integrity of the incremental GC. The invariant -** to preserve is that a black object never points to a white object. -** I.e. never store a white object into a field of a black object. -** -** It's ok to LEAVE OUT the write barrier ONLY in the following cases: -** - The source is not a GC object (NULL). -** - The target is a GC root. I.e. everything in global_State. -** - The target is a lua_State field (threads are never black). -** - The target is a stack slot, see setgcV et al. -** - The target is an open upvalue, i.e. pointing to a stack slot. -** - The target is a newly created object (i.e. marked white). But make -** sure nothing invokes the GC inbetween. -** - The target and the source are the same object (self-reference). -** - The target already contains the object (e.g. moving elements around). -** -** The most common case is a store to a stack slot. All other cases where -** a barrier has been omitted are annotated with a NOBARRIER comment. -** -** The same logic applies for stores to table slots (array part or hash -** part). ALL uses of lj_tab_set* require a barrier for the stored value -** *and* the stored key, based on the above rules. In practice this means -** a barrier is needed if *either* of the key or value are a GC object. -** -** It's ok to LEAVE OUT the write barrier in the following special cases: -** - The stored value is nil. The key doesn't matter because it's either -** not resurrected or lj_tab_newkey() will take care of the key barrier. -** - The key doesn't matter if the *previously* stored value is guaranteed -** to be non-nil (because the key is kept alive in the table). -** - The key doesn't matter if it's guaranteed not to be part of the table, -** since lj_tab_newkey() takes care of the key barrier. This applies -** trivially to new tables, but watch out for resurrected keys. Storing -** a nil value leaves the key in the table! -** -** In case of doubt use lj_gc_anybarriert() as it's rather cheap. It's used -** by the interpreter for all table stores. -** -** Note: In contrast to Lua's GC, LuaJIT's GC does *not* specially mark -** dead keys in tables. The reference is left in, but it's guaranteed to -** be never dereferenced as long as the value is nil. It's ok if the key is -** freed or if any object subsequently gets the same address. -** -** Not destroying dead keys helps to keep key hash slots stable. This avoids -** specialization back-off for HREFK when a value flips between nil and -** non-nil and the GC gets in the way. It also allows safely hoisting -** HREF/HREFK across GC steps. Dead keys are only removed if a table is -** resized (i.e. by NEWREF) and xREF must not be CSEd across a resize. -** -** The trade-off is that a write barrier for tables must take the key into -** account, too. Implicitly resurrecting the key by storing a non-nil value -** may invalidate the incremental GC invariant. -*/ - -/* -- Common type definitions --------------------------------------------- */ - -/* Types for handling bytecodes. Need this here, details in lj_bc.h. */ -typedef uint32_t BCIns; /* Bytecode instruction. */ -typedef uint32_t BCPos; /* Bytecode position. */ -typedef uint32_t BCReg; /* Bytecode register. */ -typedef int32_t BCLine; /* Bytecode line number. */ - -/* Internal assembler functions. Never call these directly from C. */ -typedef void (*ASMFunction)(void); - -/* Resizable string buffer. Need this here, details in lj_str.h. */ -typedef struct SBuf { - char *buf; /* String buffer base. */ - MSize n; /* String buffer length. */ - MSize sz; /* String buffer size. */ -} SBuf; - -/* -- Tags and values ----------------------------------------------------- */ - -/* Frame link. */ -typedef union { - int32_t ftsz; /* Frame type and size of previous frame. */ - MRef pcr; /* Overlaps PC for Lua frames. */ -} FrameLink; - -/* Tagged value. */ -typedef LJ_ALIGN(8) union TValue { - uint64_t u64; /* 64 bit pattern overlaps number. */ - lua_Number n; /* Number object overlaps split tag/value object. */ - struct { - LJ_ENDIAN_LOHI( - union { - GCRef gcr; /* GCobj reference (if any). */ - int32_t i; /* Integer value. */ - }; - , uint32_t it; /* Internal object tag. Must overlap MSW of number. */ - ) - }; - struct { - LJ_ENDIAN_LOHI( - GCRef func; /* Function for next frame (or dummy L). */ - , FrameLink tp; /* Link to previous frame. */ - ) - } fr; - struct { - LJ_ENDIAN_LOHI( - uint32_t lo; /* Lower 32 bits of number. */ - , uint32_t hi; /* Upper 32 bits of number. */ - ) - } u32; -} TValue; - -typedef const TValue cTValue; - -#define tvref(r) (mref(r, TValue)) - -/* More external and GCobj tags for internal objects. */ -#define LAST_TT LUA_TTHREAD -#define LUA_TPROTO (LAST_TT+1) -#define LUA_TCDATA (LAST_TT+2) - -/* Internal object tags. -** -** Internal tags overlap the MSW of a number object (must be a double). -** Interpreted as a double these are special NaNs. The FPU only generates -** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available -** for use as internal tags. Small negative numbers are used to shorten the -** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate). -** -** ---MSW---.---LSW--- -** primitive types | itype | | -** lightuserdata | itype | void * | (32 bit platforms) -** lightuserdata |ffff| void * | (64 bit platforms, 47 bit pointers) -** GC objects | itype | GCRef | -** int (LJ_DUALNUM)| itype | int | -** number -------double------ -** -** ORDER LJ_T -** Primitive types nil/false/true must be first, lightuserdata next. -** GC objects are at the end, table/userdata must be lowest. -** Also check lj_ir.h for similar ordering constraints. -*/ -#define LJ_TNIL (~0u) -#define LJ_TFALSE (~1u) -#define LJ_TTRUE (~2u) -#define LJ_TLIGHTUD (~3u) -#define LJ_TSTR (~4u) -#define LJ_TUPVAL (~5u) -#define LJ_TTHREAD (~6u) -#define LJ_TPROTO (~7u) -#define LJ_TFUNC (~8u) -#define LJ_TTRACE (~9u) -#define LJ_TCDATA (~10u) -#define LJ_TTAB (~11u) -#define LJ_TUDATA (~12u) -/* This is just the canonical number type used in some places. */ -#define LJ_TNUMX (~13u) - -/* Integers have itype == LJ_TISNUM doubles have itype < LJ_TISNUM */ -#if LJ_64 -#define LJ_TISNUM 0xfffeffffu -#else -#define LJ_TISNUM LJ_TNUMX -#endif -#define LJ_TISTRUECOND LJ_TFALSE -#define LJ_TISPRI LJ_TTRUE -#define LJ_TISGCV (LJ_TSTR+1) -#define LJ_TISTABUD LJ_TTAB - -/* -- String object ------------------------------------------------------- */ - -/* String object header. String payload follows. */ -typedef struct GCstr { - GCHeader; - uint8_t reserved; /* Used by lexer for fast lookup of reserved words. */ - uint8_t unused; - MSize hash; /* Hash of string. */ - MSize len; /* Size of string. */ -} GCstr; - -#define strref(r) (&gcref((r))->str) -#define strdata(s) ((const char *)((s)+1)) -#define strdatawr(s) ((char *)((s)+1)) -#define strVdata(o) strdata(strV(o)) -#define sizestring(s) (sizeof(struct GCstr)+(s)->len+1) - -/* -- Userdata object ----------------------------------------------------- */ - -/* Userdata object. Payload follows. */ -typedef struct GCudata { - GCHeader; - uint8_t udtype; /* Userdata type. */ - uint8_t unused2; - GCRef env; /* Should be at same offset in GCfunc. */ - MSize len; /* Size of payload. */ - GCRef metatable; /* Must be at same offset in GCtab. */ - uint32_t align1; /* To force 8 byte alignment of the payload. */ -} GCudata; - -/* Userdata types. */ -enum { - UDTYPE_USERDATA, /* Regular userdata. */ - UDTYPE_IO_FILE, /* I/O library FILE. */ - UDTYPE_FFI_CLIB, /* FFI C library namespace. */ - UDTYPE__MAX -}; - -#define uddata(u) ((void *)((u)+1)) -#define sizeudata(u) (sizeof(struct GCudata)+(u)->len) - -/* -- C data object ------------------------------------------------------- */ - -/* C data object. Payload follows. */ -typedef struct GCcdata { - GCHeader; - uint16_t ctypeid; /* C type ID. */ -} GCcdata; - -/* Prepended to variable-sized or realigned C data objects. */ -typedef struct GCcdataVar { - uint16_t offset; /* Offset to allocated memory (relative to GCcdata). */ - uint16_t extra; /* Extra space allocated (incl. GCcdata + GCcdatav). */ - MSize len; /* Size of payload. */ -} GCcdataVar; - -#define cdataptr(cd) ((void *)((cd)+1)) -#define cdataisv(cd) ((cd)->marked & 0x80) -#define cdatav(cd) ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar))) -#define cdatavlen(cd) check_exp(cdataisv(cd), cdatav(cd)->len) -#define sizecdatav(cd) (cdatavlen(cd) + cdatav(cd)->extra) -#define memcdatav(cd) ((void *)((char *)(cd) - cdatav(cd)->offset)) - -/* -- Prototype object ---------------------------------------------------- */ - -#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) -#define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1)) - -typedef struct GCproto { - GCHeader; - uint8_t numparams; /* Number of parameters. */ - uint8_t framesize; /* Fixed frame size. */ - MSize sizebc; /* Number of bytecode instructions. */ - GCRef gclist; - MRef k; /* Split constant array (points to the middle). */ - MRef uv; /* Upvalue list. local slot|0x8000 or parent uv idx. */ - MSize sizekgc; /* Number of collectable constants. */ - MSize sizekn; /* Number of lua_Number constants. */ - MSize sizept; /* Total size including colocated arrays. */ - uint8_t sizeuv; /* Number of upvalues. */ - uint8_t flags; /* Miscellaneous flags (see below). */ - uint16_t trace; /* Anchor for chain of root traces. */ - /* ------ The following fields are for debugging/tracebacks only ------ */ - GCRef chunkname; /* Name of the chunk this function was defined in. */ - BCLine firstline; /* First line of the function definition. */ - BCLine numline; /* Number of lines for the function definition. */ - MRef lineinfo; /* Compressed map from bytecode ins. to source line. */ - MRef uvinfo; /* Upvalue names. */ - MRef varinfo; /* Names and compressed extents of local variables. */ -} GCproto; - -/* Flags for prototype. */ -#define PROTO_CHILD 0x01 /* Has child prototypes. */ -#define PROTO_VARARG 0x02 /* Vararg function. */ -#define PROTO_FFI 0x04 /* Uses BC_KCDATA for FFI datatypes. */ -#define PROTO_NOJIT 0x08 /* JIT disabled for this function. */ -#define PROTO_ILOOP 0x10 /* Patched bytecode with ILOOP etc. */ -/* Only used during parsing. */ -#define PROTO_HAS_RETURN 0x20 /* Already emitted a return. */ -#define PROTO_FIXUP_RETURN 0x40 /* Need to fixup emitted returns. */ -/* Top bits used for counting created closures. */ -#define PROTO_CLCOUNT 0x20 /* Base of saturating 3 bit counter. */ -#define PROTO_CLC_BITS 3 -#define PROTO_CLC_POLY (3*PROTO_CLCOUNT) /* Polymorphic threshold. */ - -#define PROTO_UV_LOCAL 0x8000 /* Upvalue for local slot. */ -#define PROTO_UV_IMMUTABLE 0x4000 /* Immutable upvalue. */ - -#define proto_kgc(pt, idx) \ - check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \ - gcref(mref((pt)->k, GCRef)[(idx)])) -#define proto_knumtv(pt, idx) \ - check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)]) -#define proto_bc(pt) ((BCIns *)((char *)(pt) + sizeof(GCproto))) -#define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt))) -#define proto_uv(pt) (mref((pt)->uv, uint16_t)) - -#define proto_chunkname(pt) (strref((pt)->chunkname)) -#define proto_chunknamestr(pt) (strdata(proto_chunkname((pt)))) -#define proto_lineinfo(pt) (mref((pt)->lineinfo, const void)) -#define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t)) -#define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t)) - -/* -- Upvalue object ------------------------------------------------------ */ - -typedef struct GCupval { - GCHeader; - uint8_t closed; /* Set if closed (i.e. uv->v == &uv->u.value). */ - uint8_t immutable; /* Immutable value. */ - union { - TValue tv; /* If closed: the value itself. */ - struct { /* If open: double linked list, anchored at thread. */ - GCRef prev; - GCRef next; - }; - }; - MRef v; /* Points to stack slot (open) or above (closed). */ - uint32_t dhash; /* Disambiguation hash: dh1 != dh2 => cannot alias. */ -} GCupval; - -#define uvprev(uv_) (&gcref((uv_)->prev)->uv) -#define uvnext(uv_) (&gcref((uv_)->next)->uv) -#define uvval(uv_) (mref((uv_)->v, TValue)) - -/* -- Function object (closures) ------------------------------------------ */ - -/* Common header for functions. env should be at same offset in GCudata. */ -#define GCfuncHeader \ - GCHeader; uint8_t ffid; uint8_t nupvalues; \ - GCRef env; GCRef gclist; MRef pc - -typedef struct GCfuncC { - GCfuncHeader; - lua_CFunction f; /* C function to be called. */ - TValue upvalue[1]; /* Array of upvalues (TValue). */ -} GCfuncC; - -typedef struct GCfuncL { - GCfuncHeader; - GCRef uvptr[1]; /* Array of _pointers_ to upvalue objects (GCupval). */ -} GCfuncL; - -typedef union GCfunc { - GCfuncC c; - GCfuncL l; -} GCfunc; - -#define FF_LUA 0 -#define FF_C 1 -#define isluafunc(fn) ((fn)->c.ffid == FF_LUA) -#define iscfunc(fn) ((fn)->c.ffid == FF_C) -#define isffunc(fn) ((fn)->c.ffid > FF_C) -#define funcproto(fn) \ - check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto))) -#define sizeCfunc(n) (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n)) -#define sizeLfunc(n) (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n)) - -/* -- Table object -------------------------------------------------------- */ - -/* Hash node. */ -typedef struct Node { - TValue val; /* Value object. Must be first field. */ - TValue key; /* Key object. */ - MRef next; /* Hash chain. */ - MRef freetop; /* Top of free elements (stored in t->node[0]). */ -} Node; - -LJ_STATIC_ASSERT(offsetof(Node, val) == 0); - -typedef struct GCtab { - GCHeader; - uint8_t nomm; /* Negative cache for fast metamethods. */ - int8_t colo; /* Array colocation. */ - MRef array; /* Array part. */ - GCRef gclist; - GCRef metatable; /* Must be at same offset in GCudata. */ - MRef node; /* Hash part. */ - uint32_t asize; /* Size of array part (keys [0, asize-1]). */ - uint32_t hmask; /* Hash part mask (size of hash part - 1). */ -} GCtab; - -#define sizetabcolo(n) ((n)*sizeof(TValue) + sizeof(GCtab)) -#define tabref(r) (&gcref((r))->tab) -#define noderef(r) (mref((r), Node)) -#define nextnode(n) (mref((n)->next, Node)) - -/* -- State objects ------------------------------------------------------- */ - -/* VM states. */ -enum { - LJ_VMST_INTERP, /* Interpreter. */ - LJ_VMST_C, /* C function. */ - LJ_VMST_GC, /* Garbage collector. */ - LJ_VMST_EXIT, /* Trace exit handler. */ - LJ_VMST_RECORD, /* Trace recorder. */ - LJ_VMST_OPT, /* Optimizer. */ - LJ_VMST_ASM, /* Assembler. */ - LJ_VMST__MAX -}; - -#define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st) - -/* Metamethods. ORDER MM */ -#ifdef LJ_HASFFI -#define MMDEF_FFI(_) _(new) -#else -#define MMDEF_FFI(_) -#endif - -#if LJ_52 || LJ_HASFFI -#define MMDEF_PAIRS(_) _(pairs) _(ipairs) -#else -#define MMDEF_PAIRS(_) -#define MM_pairs 255 -#define MM_ipairs 255 -#endif - -#define MMDEF(_) \ - _(index) _(newindex) _(gc) _(mode) _(eq) _(len) \ - /* Only the above (fast) metamethods are negative cached (max. 8). */ \ - _(lt) _(le) _(concat) _(call) \ - /* The following must be in ORDER ARITH. */ \ - _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \ - /* The following are used in the standard libraries. */ \ - _(metatable) _(tostring) MMDEF_FFI(_) MMDEF_PAIRS(_) - -typedef enum { -#define MMENUM(name) MM_##name, -MMDEF(MMENUM) -#undef MMENUM - MM__MAX, - MM____ = MM__MAX, - MM_FAST = MM_len -} MMS; - -/* GC root IDs. */ -typedef enum { - GCROOT_MMNAME, /* Metamethod names. */ - GCROOT_MMNAME_LAST = GCROOT_MMNAME + MM__MAX-1, - GCROOT_BASEMT, /* Metatables for base types. */ - GCROOT_BASEMT_NUM = GCROOT_BASEMT + ~LJ_TNUMX, - GCROOT_IO_INPUT, /* Userdata for default I/O input file. */ - GCROOT_IO_OUTPUT, /* Userdata for default I/O output file. */ - GCROOT_MAX -} GCRootID; - -#define basemt_it(g, it) ((g)->gcroot[GCROOT_BASEMT+~(it)]) -#define basemt_obj(g, o) ((g)->gcroot[GCROOT_BASEMT+itypemap(o)]) -#define mmname_str(g, mm) (strref((g)->gcroot[GCROOT_MMNAME+(mm)])) - -typedef struct GCState { - MSize total; /* Memory currently allocated. */ - MSize threshold; /* Memory threshold. */ - uint8_t currentwhite; /* Current white color. */ - uint8_t state; /* GC state. */ - uint8_t unused1; - uint8_t unused2; - MSize sweepstr; /* Sweep position in string table. */ - GCRef root; /* List of all collectable objects. */ - MRef sweep; /* Sweep position in root list. */ - GCRef gray; /* List of gray objects. */ - GCRef grayagain; /* List of objects for atomic traversal. */ - GCRef weak; /* List of weak tables (to be cleared). */ - GCRef mmudata; /* List of userdata (to be finalized). */ - MSize stepmul; /* Incremental GC step granularity. */ - MSize debt; /* Debt (how much GC is behind schedule). */ - MSize estimate; /* Estimate of memory actually in use. */ - MSize pause; /* Pause between successive GC cycles. */ -} GCState; - -/* Global state, shared by all threads of a Lua universe. */ -typedef struct global_State { - GCRef *strhash; /* String hash table (hash chain anchors). */ - MSize strmask; /* String hash mask (size of hash table - 1). */ - MSize strnum; /* Number of strings in hash table. */ - lua_Alloc allocf; /* Memory allocator. */ - void *allocd; /* Memory allocator data. */ - GCState gc; /* Garbage collector. */ - SBuf tmpbuf; /* Temporary buffer for string concatenation. */ - Node nilnode; /* Fallback 1-element hash part (nil key and value). */ - GCstr strempty; /* Empty string. */ - uint8_t stremptyz; /* Zero terminator of empty string. */ - uint8_t hookmask; /* Hook mask. */ - uint8_t dispatchmode; /* Dispatch mode. */ - uint8_t vmevmask; /* VM event mask. */ - GCRef mainthref; /* Link to main thread. */ - TValue registrytv; /* Anchor for registry. */ - TValue tmptv, tmptv2; /* Temporary TValues. */ - GCupval uvhead; /* Head of double-linked list of all open upvalues. */ - int32_t hookcount; /* Instruction hook countdown. */ - int32_t hookcstart; /* Start count for instruction hook counter. */ - lua_Hook hookf; /* Hook function. */ - lua_CFunction wrapf; /* Wrapper for C function calls. */ - lua_CFunction panic; /* Called as a last resort for errors. */ - volatile int32_t vmstate; /* VM state or current JIT code trace number. */ - BCIns bc_cfunc_int; /* Bytecode for internal C function calls. */ - BCIns bc_cfunc_ext; /* Bytecode for external C function calls. */ - GCRef jit_L; /* Current JIT code lua_State or NULL. */ - MRef jit_base; /* Current JIT code L->base. */ - MRef ctype_state; /* Pointer to C type state. */ - GCRef gcroot[GCROOT_MAX]; /* GC roots. */ -} global_State; - -#define mainthread(g) (&gcref(g->mainthref)->th) -#define niltv(L) \ - check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val) -#define niltvg(g) \ - check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val) - -/* Hook management. Hook event masks are defined in lua.h. */ -#define HOOK_EVENTMASK 0x0f -#define HOOK_ACTIVE 0x10 -#define HOOK_ACTIVE_SHIFT 4 -#define HOOK_VMEVENT 0x20 -#define HOOK_GC 0x40 -#define hook_active(g) ((g)->hookmask & HOOK_ACTIVE) -#define hook_enter(g) ((g)->hookmask |= HOOK_ACTIVE) -#define hook_entergc(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_GC)) -#define hook_vmevent(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT)) -#define hook_leave(g) ((g)->hookmask &= ~HOOK_ACTIVE) -#define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK) -#define hook_restore(g, h) \ - ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h)) - -/* Per-thread state object. */ -struct lua_State { - GCHeader; - uint8_t dummy_ffid; /* Fake FF_C for curr_funcisL() on dummy frames. */ - uint8_t status; /* Thread status. */ - MRef glref; /* Link to global state. */ - GCRef gclist; /* GC chain. */ - TValue *base; /* Base of currently executing function. */ - TValue *top; /* First free slot in the stack. */ - MRef maxstack; /* Last free slot in the stack. */ - MRef stack; /* Stack base. */ - GCRef openupval; /* List of open upvalues in the stack. */ - GCRef env; /* Thread environment (table of globals). */ - void *cframe; /* End of C stack frame chain. */ - MSize stacksize; /* True stack size (incl. LJ_STACK_EXTRA). */ -}; - -#define G(L) (mref(L->glref, global_State)) -#define registry(L) (&G(L)->registrytv) - -/* Macros to access the currently executing (Lua) function. */ -#define curr_func(L) (&gcref((L->base-1)->fr.func)->fn) -#define curr_funcisL(L) (isluafunc(curr_func(L))) -#define curr_proto(L) (funcproto(curr_func(L))) -#define curr_topL(L) (L->base + curr_proto(L)->framesize) -#define curr_top(L) (curr_funcisL(L) ? curr_topL(L) : L->top) - -/* -- GC object definition and conversions -------------------------------- */ - -/* GC header for generic access to common fields of GC objects. */ -typedef struct GChead { - GCHeader; - uint8_t unused1; - uint8_t unused2; - GCRef env; - GCRef gclist; - GCRef metatable; -} GChead; - -/* The env field SHOULD be at the same offset for all GC objects. */ -LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env)); -LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env)); - -/* The metatable field MUST be at the same offset for all GC objects. */ -LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable)); -LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable)); - -/* The gclist field MUST be at the same offset for all GC objects. */ -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist)); -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist)); -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist)); -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist)); - -typedef union GCobj { - GChead gch; - GCstr str; - GCupval uv; - lua_State th; - GCproto pt; - GCfunc fn; - GCcdata cd; - GCtab tab; - GCudata ud; -} GCobj; - -/* Macros to convert a GCobj pointer into a specific value. */ -#define gco2str(o) check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str) -#define gco2uv(o) check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv) -#define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th) -#define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt) -#define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn) -#define gco2cd(o) check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd) -#define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab) -#define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud) - -/* Macro to convert any collectable object into a GCobj pointer. */ -#define obj2gco(v) ((GCobj *)(v)) - -/* -- TValue getters/setters ---------------------------------------------- */ - -#ifdef LUA_USE_ASSERT -#include "lj_gc.h" -#endif - -/* Macros to test types. */ -#define itype(o) ((o)->it) -#define tvisnil(o) (itype(o) == LJ_TNIL) -#define tvisfalse(o) (itype(o) == LJ_TFALSE) -#define tvistrue(o) (itype(o) == LJ_TTRUE) -#define tvisbool(o) (tvisfalse(o) || tvistrue(o)) -#if LJ_64 -#define tvislightud(o) (((int32_t)itype(o) >> 15) == -2) -#else -#define tvislightud(o) (itype(o) == LJ_TLIGHTUD) -#endif -#define tvisstr(o) (itype(o) == LJ_TSTR) -#define tvisfunc(o) (itype(o) == LJ_TFUNC) -#define tvisthread(o) (itype(o) == LJ_TTHREAD) -#define tvisproto(o) (itype(o) == LJ_TPROTO) -#define tviscdata(o) (itype(o) == LJ_TCDATA) -#define tvistab(o) (itype(o) == LJ_TTAB) -#define tvisudata(o) (itype(o) == LJ_TUDATA) -#define tvisnumber(o) (itype(o) <= LJ_TISNUM) -#define tvisint(o) (LJ_DUALNUM && itype(o) == LJ_TISNUM) -#define tvisnum(o) (itype(o) < LJ_TISNUM) - -#define tvistruecond(o) (itype(o) < LJ_TISTRUECOND) -#define tvispri(o) (itype(o) >= LJ_TISPRI) -#define tvistabud(o) (itype(o) <= LJ_TISTABUD) /* && !tvisnum() */ -#define tvisgcv(o) ((itype(o) - LJ_TISGCV) > (LJ_TNUMX - LJ_TISGCV)) - -/* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */ -#define tvisnan(o) ((o)->n != (o)->n) -#if LJ_64 -#define tviszero(o) (((o)->u64 << 1) == 0) -#else -#define tviszero(o) (((o)->u32.lo | ((o)->u32.hi << 1)) == 0) -#endif -#define tvispzero(o) ((o)->u64 == 0) -#define tvismzero(o) ((o)->u64 == U64x(80000000,00000000)) -#define tvispone(o) ((o)->u64 == U64x(3ff00000,00000000)) -#define rawnumequal(o1, o2) ((o1)->u64 == (o2)->u64) - -/* Macros to convert type ids. */ -#if LJ_64 -#define itypemap(o) \ - (tvisnumber(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o)) -#else -#define itypemap(o) (tvisnumber(o) ? ~LJ_TNUMX : ~itype(o)) -#endif - -/* Macros to get tagged values. */ -#define gcval(o) (gcref((o)->gcr)) -#define boolV(o) check_exp(tvisbool(o), (LJ_TFALSE - (o)->it)) -#if LJ_64 -#define lightudV(o) \ - check_exp(tvislightud(o), (void *)((o)->u64 & U64x(00007fff,ffffffff))) -#else -#define lightudV(o) check_exp(tvislightud(o), gcrefp((o)->gcr, void)) -#endif -#define gcV(o) check_exp(tvisgcv(o), gcval(o)) -#define strV(o) check_exp(tvisstr(o), &gcval(o)->str) -#define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn) -#define threadV(o) check_exp(tvisthread(o), &gcval(o)->th) -#define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt) -#define cdataV(o) check_exp(tviscdata(o), &gcval(o)->cd) -#define tabV(o) check_exp(tvistab(o), &gcval(o)->tab) -#define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud) -#define numV(o) check_exp(tvisnum(o), (o)->n) -#define intV(o) check_exp(tvisint(o), (int32_t)(o)->i) - -/* Macros to set tagged values. */ -#define setitype(o, i) ((o)->it = (i)) -#define setnilV(o) ((o)->it = LJ_TNIL) -#define setboolV(o, x) ((o)->it = LJ_TFALSE-(uint32_t)(x)) - -static LJ_AINLINE void setlightudV(TValue *o, void *p) -{ -#if LJ_64 - o->u64 = (uint64_t)p | (((uint64_t)0xffff) << 48); -#else - setgcrefp(o->gcr, p); setitype(o, LJ_TLIGHTUD); -#endif -} - -#if LJ_64 -#define checklightudptr(L, p) \ - (((uint64_t)(p) >> 47) ? (lj_err_msg(L, LJ_ERR_BADLU), NULL) : (p)) -#define setcont(o, f) \ - ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin) -#else -#define checklightudptr(L, p) (p) -#define setcont(o, f) setlightudV((o), (void *)(f)) -#endif - -#define tvchecklive(L, o) \ - UNUSED(L), lua_assert(!tvisgcv(o) || \ - ((~itype(o) == gcval(o)->gch.gct) && !isdead(G(L), gcval(o)))) - -static LJ_AINLINE void setgcV(lua_State *L, TValue *o, GCobj *v, uint32_t itype) -{ - setgcref(o->gcr, v); setitype(o, itype); tvchecklive(L, o); -} - -#define define_setV(name, type, tag) \ -static LJ_AINLINE void name(lua_State *L, TValue *o, type *v) \ -{ \ - setgcV(L, o, obj2gco(v), tag); \ -} -define_setV(setstrV, GCstr, LJ_TSTR) -define_setV(setthreadV, lua_State, LJ_TTHREAD) -define_setV(setprotoV, GCproto, LJ_TPROTO) -define_setV(setfuncV, GCfunc, LJ_TFUNC) -define_setV(setcdataV, GCcdata, LJ_TCDATA) -define_setV(settabV, GCtab, LJ_TTAB) -define_setV(setudataV, GCudata, LJ_TUDATA) - -#define setnumV(o, x) ((o)->n = (x)) -#define setnanV(o) ((o)->u64 = U64x(fff80000,00000000)) -#define setpinfV(o) ((o)->u64 = U64x(7ff00000,00000000)) -#define setminfV(o) ((o)->u64 = U64x(fff00000,00000000)) - -static LJ_AINLINE void setintV(TValue *o, int32_t i) -{ -#if LJ_DUALNUM - o->i = (uint32_t)i; setitype(o, LJ_TISNUM); -#else - o->n = (lua_Number)i; -#endif -} - -static LJ_AINLINE void setint64V(TValue *o, int64_t i) -{ - if (LJ_DUALNUM && LJ_LIKELY(i == (int64_t)(int32_t)i)) - setintV(o, (int32_t)i); - else - setnumV(o, (lua_Number)i); -} - -#if LJ_64 -#define setintptrV(o, i) setint64V((o), (i)) -#else -#define setintptrV(o, i) setintV((o), (i)) -#endif - -/* Copy tagged values. */ -static LJ_AINLINE void copyTV(lua_State *L, TValue *o1, const TValue *o2) -{ - *o1 = *o2; tvchecklive(L, o1); -} - -/* -- Number to integer conversion ---------------------------------------- */ - -#if LJ_SOFTFP -LJ_ASMF int32_t lj_vm_tobit(double x); -#endif - -static LJ_AINLINE int32_t lj_num2bit(lua_Number n) -{ -#if LJ_SOFTFP - return lj_vm_tobit(n); -#else - TValue o; - o.n = n + 6755399441055744.0; /* 2^52 + 2^51 */ - return (int32_t)o.u32.lo; -#endif -} - -#if LJ_TARGET_X86 && !defined(__SSE2__) -#define lj_num2int(n) lj_num2bit((n)) -#else -#define lj_num2int(n) ((int32_t)(n)) -#endif - -static LJ_AINLINE uint64_t lj_num2u64(lua_Number n) -{ -#ifdef _MSC_VER - if (n >= 9223372036854775808.0) /* They think it's a feature. */ - return (uint64_t)(int64_t)(n - 18446744073709551616.0); - else -#endif - return (uint64_t)n; -} - -static LJ_AINLINE int32_t numberVint(cTValue *o) -{ - if (LJ_LIKELY(tvisint(o))) - return intV(o); - else - return lj_num2int(numV(o)); -} - -static LJ_AINLINE lua_Number numberVnum(cTValue *o) -{ - if (LJ_UNLIKELY(tvisint(o))) - return (lua_Number)intV(o); - else - return numV(o); -} - -/* -- Miscellaneous object handling --------------------------------------- */ - -/* Names and maps for internal and external object tags. */ -LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1]; -LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1]; - -#define lj_typename(o) (lj_obj_itypename[itypemap(o)]) - -/* Compare two objects without calling metamethods. */ -LJ_FUNC int lj_obj_equal(cTValue *o1, cTValue *o2); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_opt_dce.c b/third-party/LuaJIT-2.0.2/src/lj_opt_dce.c deleted file mode 100644 index d64cca76cc..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_opt_dce.c +++ /dev/null @@ -1,77 +0,0 @@ -/* -** DCE: Dead Code Elimination. Pre-LOOP only -- ASM already performs DCE. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_dce_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Scan through all snapshots and mark all referenced instructions. */ -static void dce_marksnap(jit_State *J) -{ - SnapNo i, nsnap = J->cur.nsnap; - for (i = 0; i < nsnap; i++) { - SnapShot *snap = &J->cur.snap[i]; - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - IRRef ref = snap_ref(map[n]); - if (ref >= REF_FIRST) - irt_setmark(IR(ref)->t); - } - } -} - -/* Backwards propagate marks. Replace unused instructions with NOPs. */ -static void dce_propagate(jit_State *J) -{ - IRRef1 *pchain[IR__MAX]; - IRRef ins; - uint32_t i; - for (i = 0; i < IR__MAX; i++) pchain[i] = &J->chain[i]; - for (ins = J->cur.nins-1; ins >= REF_FIRST; ins--) { - IRIns *ir = IR(ins); - if (irt_ismarked(ir->t)) { - irt_clearmark(ir->t); - pchain[ir->o] = &ir->prev; - } else if (!ir_sideeff(ir)) { - *pchain[ir->o] = ir->prev; /* Reroute original instruction chain. */ - ir->t.irt = IRT_NIL; - ir->o = IR_NOP; /* Replace instruction with NOP. */ - ir->op1 = ir->op2 = 0; - ir->prev = 0; - continue; - } - if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t); - if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t); - } -} - -/* Dead Code Elimination. -** -** First backpropagate marks for all used instructions. Then replace -** the unused ones with a NOP. Note that compressing the IR to eliminate -** the NOPs does not pay off. -*/ -void lj_opt_dce(jit_State *J) -{ - if ((J->flags & JIT_F_OPT_DCE)) { - dce_marksnap(J); - dce_propagate(J); - } -} - -#undef IR - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_opt_fold.c b/third-party/LuaJIT-2.0.2/src/lj_opt_fold.c deleted file mode 100644 index fe37b98a88..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_opt_fold.c +++ /dev/null @@ -1,2295 +0,0 @@ -/* -** FOLD: Constant Folding, Algebraic Simplifications and Reassociation. -** ABCelim: Array Bounds Check Elimination. -** CSE: Common-Subexpression Elimination. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_fold_c -#define LUA_CORE - -#include - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_carith.h" -#include "lj_vm.h" -#include "lj_strscan.h" - -/* Here's a short description how the FOLD engine processes instructions: -** -** The FOLD engine receives a single instruction stored in fins (J->fold.ins). -** The instruction and its operands are used to select matching fold rules. -** These are applied iteratively until a fixed point is reached. -** -** The 8 bit opcode of the instruction itself plus the opcodes of the -** two instructions referenced by its operands form a 24 bit key -** 'ins left right' (unused operands -> 0, literals -> lowest 8 bits). -** -** This key is used for partial matching against the fold rules. The -** left/right operand fields of the key are successively masked with -** the 'any' wildcard, from most specific to least specific: -** -** ins left right -** ins any right -** ins left any -** ins any any -** -** The masked key is used to lookup a matching fold rule in a semi-perfect -** hash table. If a matching rule is found, the related fold function is run. -** Multiple rules can share the same fold function. A fold rule may return -** one of several special values: -** -** - NEXTFOLD means no folding was applied, because an additional test -** inside the fold function failed. Matching continues against less -** specific fold rules. Finally the instruction is passed on to CSE. -** -** - RETRYFOLD means the instruction was modified in-place. Folding is -** retried as if this instruction had just been received. -** -** All other return values are terminal actions -- no further folding is -** applied: -** -** - INTFOLD(i) returns a reference to the integer constant i. -** -** - LEFTFOLD and RIGHTFOLD return the left/right operand reference -** without emitting an instruction. -** -** - CSEFOLD and EMITFOLD pass the instruction directly to CSE or emit -** it without passing through any further optimizations. -** -** - FAILFOLD, DROPFOLD and CONDFOLD only apply to instructions which have -** no result (e.g. guarded assertions): FAILFOLD means the guard would -** always fail, i.e. the current trace is pointless. DROPFOLD means -** the guard is always true and has been eliminated. CONDFOLD is a -** shortcut for FAILFOLD + cond (i.e. drop if true, otherwise fail). -** -** - Any other return value is interpreted as an IRRef or TRef. This -** can be a reference to an existing or a newly created instruction. -** Only the least-significant 16 bits (IRRef1) are used to form a TRef -** which is finally returned to the caller. -** -** The FOLD engine receives instructions both from the trace recorder and -** substituted instructions from LOOP unrolling. This means all types -** of instructions may end up here, even though the recorder bypasses -** FOLD in some cases. Thus all loads, stores and allocations must have -** an any/any rule to avoid being passed on to CSE. -** -** Carefully read the following requirements before adding or modifying -** any fold rules: -** -** Requirement #1: All fold rules must preserve their destination type. -** -** Consistently use INTFOLD() (KINT result) or lj_ir_knum() (KNUM result). -** Never use lj_ir_knumint() which can have either a KINT or KNUM result. -** -** Requirement #2: Fold rules should not create *new* instructions which -** reference operands *across* PHIs. -** -** E.g. a RETRYFOLD with 'fins->op1 = fleft->op1' is invalid if the -** left operand is a PHI. Then fleft->op1 would point across the PHI -** frontier to an invariant instruction. Adding a PHI for this instruction -** would be counterproductive. The solution is to add a barrier which -** prevents folding across PHIs, i.e. 'PHIBARRIER(fleft)' in this case. -** The only exception is for recurrences with high latencies like -** repeated int->num->int conversions. -** -** One could relax this condition a bit if the referenced instruction is -** a PHI, too. But this often leads to worse code due to excessive -** register shuffling. -** -** Note: returning *existing* instructions (e.g. LEFTFOLD) is ok, though. -** Even returning fleft->op1 would be ok, because a new PHI will added, -** if needed. But again, this leads to excessive register shuffling and -** should be avoided. -** -** Requirement #3: The set of all fold rules must be monotonic to guarantee -** termination. -** -** The goal is optimization, so one primarily wants to add strength-reducing -** rules. This means eliminating an instruction or replacing an instruction -** with one or more simpler instructions. Don't add fold rules which point -** into the other direction. -** -** Some rules (like commutativity) do not directly reduce the strength of -** an instruction, but enable other fold rules (e.g. by moving constants -** to the right operand). These rules must be made unidirectional to avoid -** cycles. -** -** Rule of thumb: the trace recorder expands the IR and FOLD shrinks it. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) -#define fleft (&J->fold.left) -#define fright (&J->fold.right) -#define knumleft (ir_knum(fleft)->n) -#define knumright (ir_knum(fright)->n) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Fold function type. Fastcall on x86 significantly reduces their size. */ -typedef IRRef (LJ_FASTCALL *FoldFunc)(jit_State *J); - -/* Macros for the fold specs, so buildvm can recognize them. */ -#define LJFOLD(x) -#define LJFOLDX(x) -#define LJFOLDF(name) static TRef LJ_FASTCALL fold_##name(jit_State *J) -/* Note: They must be at the start of a line or buildvm ignores them! */ - -/* Barrier to prevent using operands across PHIs. */ -#define PHIBARRIER(ir) if (irt_isphi((ir)->t)) return NEXTFOLD - -/* Barrier to prevent folding across a GC step. -** GC steps can only happen at the head of a trace and at LOOP. -** And the GC is only driven forward if there is at least one allocation. -*/ -#define gcstep_barrier(J, ref) \ - ((ref) < J->chain[IR_LOOP] && \ - (J->chain[IR_SNEW] || J->chain[IR_XSNEW] || \ - J->chain[IR_TNEW] || J->chain[IR_TDUP] || \ - J->chain[IR_CNEW] || J->chain[IR_CNEWI] || J->chain[IR_TOSTR])) - -/* -- Constant folding for FP numbers ------------------------------------- */ - -LJFOLD(ADD KNUM KNUM) -LJFOLD(SUB KNUM KNUM) -LJFOLD(MUL KNUM KNUM) -LJFOLD(DIV KNUM KNUM) -LJFOLD(NEG KNUM KNUM) -LJFOLD(ABS KNUM KNUM) -LJFOLD(ATAN2 KNUM KNUM) -LJFOLD(LDEXP KNUM KNUM) -LJFOLD(MIN KNUM KNUM) -LJFOLD(MAX KNUM KNUM) -LJFOLDF(kfold_numarith) -{ - lua_Number a = knumleft; - lua_Number b = knumright; - lua_Number y = lj_vm_foldarith(a, b, fins->o - IR_ADD); - return lj_ir_knum(J, y); -} - -LJFOLD(LDEXP KNUM KINT) -LJFOLDF(kfold_ldexp) -{ -#if LJ_TARGET_X86ORX64 - UNUSED(J); - return NEXTFOLD; -#else - return lj_ir_knum(J, ldexp(knumleft, fright->i)); -#endif -} - -LJFOLD(FPMATH KNUM any) -LJFOLDF(kfold_fpmath) -{ - lua_Number a = knumleft; - lua_Number y = lj_vm_foldfpm(a, fins->op2); - return lj_ir_knum(J, y); -} - -LJFOLD(POW KNUM KINT) -LJFOLDF(kfold_numpow) -{ - lua_Number a = knumleft; - lua_Number b = (lua_Number)fright->i; - lua_Number y = lj_vm_foldarith(a, b, IR_POW - IR_ADD); - return lj_ir_knum(J, y); -} - -/* Must not use kfold_kref for numbers (could be NaN). */ -LJFOLD(EQ KNUM KNUM) -LJFOLD(NE KNUM KNUM) -LJFOLD(LT KNUM KNUM) -LJFOLD(GE KNUM KNUM) -LJFOLD(LE KNUM KNUM) -LJFOLD(GT KNUM KNUM) -LJFOLD(ULT KNUM KNUM) -LJFOLD(UGE KNUM KNUM) -LJFOLD(ULE KNUM KNUM) -LJFOLD(UGT KNUM KNUM) -LJFOLDF(kfold_numcomp) -{ - return CONDFOLD(lj_ir_numcmp(knumleft, knumright, (IROp)fins->o)); -} - -/* -- Constant folding for 32 bit integers -------------------------------- */ - -static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op) -{ - switch (op) { - case IR_ADD: k1 += k2; break; - case IR_SUB: k1 -= k2; break; - case IR_MUL: k1 *= k2; break; - case IR_MOD: k1 = lj_vm_modi(k1, k2); break; - case IR_NEG: k1 = -k1; break; - case IR_BAND: k1 &= k2; break; - case IR_BOR: k1 |= k2; break; - case IR_BXOR: k1 ^= k2; break; - case IR_BSHL: k1 <<= (k2 & 31); break; - case IR_BSHR: k1 = (int32_t)((uint32_t)k1 >> (k2 & 31)); break; - case IR_BSAR: k1 >>= (k2 & 31); break; - case IR_BROL: k1 = (int32_t)lj_rol((uint32_t)k1, (k2 & 31)); break; - case IR_BROR: k1 = (int32_t)lj_ror((uint32_t)k1, (k2 & 31)); break; - case IR_MIN: k1 = k1 < k2 ? k1 : k2; break; - case IR_MAX: k1 = k1 > k2 ? k1 : k2; break; - default: lua_assert(0); break; - } - return k1; -} - -LJFOLD(ADD KINT KINT) -LJFOLD(SUB KINT KINT) -LJFOLD(MUL KINT KINT) -LJFOLD(MOD KINT KINT) -LJFOLD(NEG KINT KINT) -LJFOLD(BAND KINT KINT) -LJFOLD(BOR KINT KINT) -LJFOLD(BXOR KINT KINT) -LJFOLD(BSHL KINT KINT) -LJFOLD(BSHR KINT KINT) -LJFOLD(BSAR KINT KINT) -LJFOLD(BROL KINT KINT) -LJFOLD(BROR KINT KINT) -LJFOLD(MIN KINT KINT) -LJFOLD(MAX KINT KINT) -LJFOLDF(kfold_intarith) -{ - return INTFOLD(kfold_intop(fleft->i, fright->i, (IROp)fins->o)); -} - -LJFOLD(ADDOV KINT KINT) -LJFOLD(SUBOV KINT KINT) -LJFOLD(MULOV KINT KINT) -LJFOLDF(kfold_intovarith) -{ - lua_Number n = lj_vm_foldarith((lua_Number)fleft->i, (lua_Number)fright->i, - fins->o - IR_ADDOV); - int32_t k = lj_num2int(n); - if (n != (lua_Number)k) - return FAILFOLD; - return INTFOLD(k); -} - -LJFOLD(BNOT KINT) -LJFOLDF(kfold_bnot) -{ - return INTFOLD(~fleft->i); -} - -LJFOLD(BSWAP KINT) -LJFOLDF(kfold_bswap) -{ - return INTFOLD((int32_t)lj_bswap((uint32_t)fleft->i)); -} - -LJFOLD(LT KINT KINT) -LJFOLD(GE KINT KINT) -LJFOLD(LE KINT KINT) -LJFOLD(GT KINT KINT) -LJFOLD(ULT KINT KINT) -LJFOLD(UGE KINT KINT) -LJFOLD(ULE KINT KINT) -LJFOLD(UGT KINT KINT) -LJFOLD(ABC KINT KINT) -LJFOLDF(kfold_intcomp) -{ - int32_t a = fleft->i, b = fright->i; - switch ((IROp)fins->o) { - case IR_LT: return CONDFOLD(a < b); - case IR_GE: return CONDFOLD(a >= b); - case IR_LE: return CONDFOLD(a <= b); - case IR_GT: return CONDFOLD(a > b); - case IR_ULT: return CONDFOLD((uint32_t)a < (uint32_t)b); - case IR_UGE: return CONDFOLD((uint32_t)a >= (uint32_t)b); - case IR_ULE: return CONDFOLD((uint32_t)a <= (uint32_t)b); - case IR_ABC: - case IR_UGT: return CONDFOLD((uint32_t)a > (uint32_t)b); - default: lua_assert(0); return FAILFOLD; - } -} - -LJFOLD(UGE any KINT) -LJFOLDF(kfold_intcomp0) -{ - if (fright->i == 0) - return DROPFOLD; - return NEXTFOLD; -} - -/* -- Constant folding for 64 bit integers -------------------------------- */ - -static uint64_t kfold_int64arith(uint64_t k1, uint64_t k2, IROp op) -{ - switch (op) { -#if LJ_64 || LJ_HASFFI - case IR_ADD: k1 += k2; break; - case IR_SUB: k1 -= k2; break; -#endif -#if LJ_HASFFI - case IR_MUL: k1 *= k2; break; - case IR_BAND: k1 &= k2; break; - case IR_BOR: k1 |= k2; break; - case IR_BXOR: k1 ^= k2; break; -#endif - default: UNUSED(k2); lua_assert(0); break; - } - return k1; -} - -LJFOLD(ADD KINT64 KINT64) -LJFOLD(SUB KINT64 KINT64) -LJFOLD(MUL KINT64 KINT64) -LJFOLD(BAND KINT64 KINT64) -LJFOLD(BOR KINT64 KINT64) -LJFOLD(BXOR KINT64 KINT64) -LJFOLDF(kfold_int64arith) -{ - return INT64FOLD(kfold_int64arith(ir_k64(fleft)->u64, - ir_k64(fright)->u64, (IROp)fins->o)); -} - -LJFOLD(DIV KINT64 KINT64) -LJFOLD(MOD KINT64 KINT64) -LJFOLD(POW KINT64 KINT64) -LJFOLDF(kfold_int64arith2) -{ -#if LJ_HASFFI - uint64_t k1 = ir_k64(fleft)->u64, k2 = ir_k64(fright)->u64; - if (irt_isi64(fins->t)) { - k1 = fins->o == IR_DIV ? lj_carith_divi64((int64_t)k1, (int64_t)k2) : - fins->o == IR_MOD ? lj_carith_modi64((int64_t)k1, (int64_t)k2) : - lj_carith_powi64((int64_t)k1, (int64_t)k2); - } else { - k1 = fins->o == IR_DIV ? lj_carith_divu64(k1, k2) : - fins->o == IR_MOD ? lj_carith_modu64(k1, k2) : - lj_carith_powu64(k1, k2); - } - return INT64FOLD(k1); -#else - UNUSED(J); lua_assert(0); return FAILFOLD; -#endif -} - -LJFOLD(BSHL KINT64 KINT) -LJFOLD(BSHR KINT64 KINT) -LJFOLD(BSAR KINT64 KINT) -LJFOLD(BROL KINT64 KINT) -LJFOLD(BROR KINT64 KINT) -LJFOLDF(kfold_int64shift) -{ -#if LJ_HASFFI || LJ_64 - uint64_t k = ir_k64(fleft)->u64; - int32_t sh = (fright->i & 63); - switch ((IROp)fins->o) { - case IR_BSHL: k <<= sh; break; -#if LJ_HASFFI - case IR_BSHR: k >>= sh; break; - case IR_BSAR: k = (uint64_t)((int64_t)k >> sh); break; - case IR_BROL: k = lj_rol(k, sh); break; - case IR_BROR: k = lj_ror(k, sh); break; -#endif - default: lua_assert(0); break; - } - return INT64FOLD(k); -#else - UNUSED(J); lua_assert(0); return FAILFOLD; -#endif -} - -LJFOLD(BNOT KINT64) -LJFOLDF(kfold_bnot64) -{ -#if LJ_HASFFI - return INT64FOLD(~ir_k64(fleft)->u64); -#else - UNUSED(J); lua_assert(0); return FAILFOLD; -#endif -} - -LJFOLD(BSWAP KINT64) -LJFOLDF(kfold_bswap64) -{ -#if LJ_HASFFI - return INT64FOLD(lj_bswap64(ir_k64(fleft)->u64)); -#else - UNUSED(J); lua_assert(0); return FAILFOLD; -#endif -} - -LJFOLD(LT KINT64 KINT64) -LJFOLD(GE KINT64 KINT64) -LJFOLD(LE KINT64 KINT64) -LJFOLD(GT KINT64 KINT64) -LJFOLD(ULT KINT64 KINT64) -LJFOLD(UGE KINT64 KINT64) -LJFOLD(ULE KINT64 KINT64) -LJFOLD(UGT KINT64 KINT64) -LJFOLDF(kfold_int64comp) -{ -#if LJ_HASFFI - uint64_t a = ir_k64(fleft)->u64, b = ir_k64(fright)->u64; - switch ((IROp)fins->o) { - case IR_LT: return CONDFOLD(a < b); - case IR_GE: return CONDFOLD(a >= b); - case IR_LE: return CONDFOLD(a <= b); - case IR_GT: return CONDFOLD(a > b); - case IR_ULT: return CONDFOLD((uint64_t)a < (uint64_t)b); - case IR_UGE: return CONDFOLD((uint64_t)a >= (uint64_t)b); - case IR_ULE: return CONDFOLD((uint64_t)a <= (uint64_t)b); - case IR_UGT: return CONDFOLD((uint64_t)a > (uint64_t)b); - default: lua_assert(0); return FAILFOLD; - } -#else - UNUSED(J); lua_assert(0); return FAILFOLD; -#endif -} - -LJFOLD(UGE any KINT64) -LJFOLDF(kfold_int64comp0) -{ -#if LJ_HASFFI - if (ir_k64(fright)->u64 == 0) - return DROPFOLD; - return NEXTFOLD; -#else - UNUSED(J); lua_assert(0); return FAILFOLD; -#endif -} - -/* -- Constant folding for strings ---------------------------------------- */ - -LJFOLD(SNEW KKPTR KINT) -LJFOLDF(kfold_snew_kptr) -{ - GCstr *s = lj_str_new(J->L, (const char *)ir_kptr(fleft), (size_t)fright->i); - return lj_ir_kstr(J, s); -} - -LJFOLD(SNEW any KINT) -LJFOLDF(kfold_snew_empty) -{ - if (fright->i == 0) - return lj_ir_kstr(J, &J2G(J)->strempty); - return NEXTFOLD; -} - -LJFOLD(STRREF KGC KINT) -LJFOLDF(kfold_strref) -{ - GCstr *str = ir_kstr(fleft); - lua_assert((MSize)fright->i <= str->len); - return lj_ir_kkptr(J, (char *)strdata(str) + fright->i); -} - -LJFOLD(STRREF SNEW any) -LJFOLDF(kfold_strref_snew) -{ - PHIBARRIER(fleft); - if (irref_isk(fins->op2) && fright->i == 0) { - return fleft->op1; /* strref(snew(ptr, len), 0) ==> ptr */ - } else { - /* Reassociate: strref(snew(strref(str, a), len), b) ==> strref(str, a+b) */ - IRIns *ir = IR(fleft->op1); - IRRef1 str = ir->op1; /* IRIns * is not valid across emitir. */ - lua_assert(ir->o == IR_STRREF); - PHIBARRIER(ir); - fins->op2 = emitir(IRTI(IR_ADD), ir->op2, fins->op2); /* Clobbers fins! */ - fins->op1 = str; - fins->ot = IRT(IR_STRREF, IRT_P32); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(CALLN CARG IRCALL_lj_str_cmp) -LJFOLDF(kfold_strcmp) -{ - if (irref_isk(fleft->op1) && irref_isk(fleft->op2)) { - GCstr *a = ir_kstr(IR(fleft->op1)); - GCstr *b = ir_kstr(IR(fleft->op2)); - return INTFOLD(lj_str_cmp(a, b)); - } - return NEXTFOLD; -} - -/* -- Constant folding of pointer arithmetic ------------------------------ */ - -LJFOLD(ADD KGC KINT) -LJFOLD(ADD KGC KINT64) -LJFOLDF(kfold_add_kgc) -{ - GCobj *o = ir_kgc(fleft); -#if LJ_64 - ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64; -#else - ptrdiff_t ofs = fright->i; -#endif -#if LJ_HASFFI - if (irt_iscdata(fleft->t)) { - CType *ct = ctype_raw(ctype_ctsG(J2G(J)), gco2cd(o)->ctypeid); - if (ctype_isnum(ct->info) || ctype_isenum(ct->info) || - ctype_isptr(ct->info) || ctype_isfunc(ct->info) || - ctype_iscomplex(ct->info) || ctype_isvector(ct->info)) - return lj_ir_kkptr(J, (char *)o + ofs); - } -#endif - return lj_ir_kptr(J, (char *)o + ofs); -} - -LJFOLD(ADD KPTR KINT) -LJFOLD(ADD KPTR KINT64) -LJFOLD(ADD KKPTR KINT) -LJFOLD(ADD KKPTR KINT64) -LJFOLDF(kfold_add_kptr) -{ - void *p = ir_kptr(fleft); -#if LJ_64 - ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64; -#else - ptrdiff_t ofs = fright->i; -#endif - return lj_ir_kptr_(J, fleft->o, (char *)p + ofs); -} - -LJFOLD(ADD any KGC) -LJFOLD(ADD any KPTR) -LJFOLD(ADD any KKPTR) -LJFOLDF(kfold_add_kright) -{ - if (fleft->o == IR_KINT || fleft->o == IR_KINT64) { - IRRef1 tmp = fins->op1; fins->op1 = fins->op2; fins->op2 = tmp; - return RETRYFOLD; - } - return NEXTFOLD; -} - -/* -- Constant folding of conversions ------------------------------------- */ - -LJFOLD(TOBIT KNUM KNUM) -LJFOLDF(kfold_tobit) -{ - return INTFOLD(lj_num2bit(knumleft)); -} - -LJFOLD(CONV KINT IRCONV_NUM_INT) -LJFOLDF(kfold_conv_kint_num) -{ - return lj_ir_knum(J, (lua_Number)fleft->i); -} - -LJFOLD(CONV KINT IRCONV_NUM_U32) -LJFOLDF(kfold_conv_kintu32_num) -{ - return lj_ir_knum(J, (lua_Number)(uint32_t)fleft->i); -} - -LJFOLD(CONV KINT IRCONV_INT_I8) -LJFOLD(CONV KINT IRCONV_INT_U8) -LJFOLD(CONV KINT IRCONV_INT_I16) -LJFOLD(CONV KINT IRCONV_INT_U16) -LJFOLDF(kfold_conv_kint_ext) -{ - int32_t k = fleft->i; - if ((fins->op2 & IRCONV_SRCMASK) == IRT_I8) k = (int8_t)k; - else if ((fins->op2 & IRCONV_SRCMASK) == IRT_U8) k = (uint8_t)k; - else if ((fins->op2 & IRCONV_SRCMASK) == IRT_I16) k = (int16_t)k; - else k = (uint16_t)k; - return INTFOLD(k); -} - -LJFOLD(CONV KINT IRCONV_I64_INT) -LJFOLD(CONV KINT IRCONV_U64_INT) -LJFOLD(CONV KINT IRCONV_I64_U32) -LJFOLD(CONV KINT IRCONV_U64_U32) -LJFOLDF(kfold_conv_kint_i64) -{ - if ((fins->op2 & IRCONV_SEXT)) - return INT64FOLD((uint64_t)(int64_t)fleft->i); - else - return INT64FOLD((uint64_t)(int64_t)(uint32_t)fleft->i); -} - -LJFOLD(CONV KINT64 IRCONV_NUM_I64) -LJFOLDF(kfold_conv_kint64_num_i64) -{ - return lj_ir_knum(J, (lua_Number)(int64_t)ir_kint64(fleft)->u64); -} - -LJFOLD(CONV KINT64 IRCONV_NUM_U64) -LJFOLDF(kfold_conv_kint64_num_u64) -{ - return lj_ir_knum(J, (lua_Number)ir_kint64(fleft)->u64); -} - -LJFOLD(CONV KINT64 IRCONV_INT_I64) -LJFOLD(CONV KINT64 IRCONV_U32_I64) -LJFOLDF(kfold_conv_kint64_int_i64) -{ - return INTFOLD((int32_t)ir_kint64(fleft)->u64); -} - -LJFOLD(CONV KNUM IRCONV_INT_NUM) -LJFOLDF(kfold_conv_knum_int_num) -{ - lua_Number n = knumleft; - if (!(fins->op2 & IRCONV_TRUNC)) { - int32_t k = lj_num2int(n); - if (irt_isguard(fins->t) && n != (lua_Number)k) { - /* We're about to create a guard which always fails, like CONV +1.5. - ** Some pathological loops cause this during LICM, e.g.: - ** local x,k,t = 0,1.5,{1,[1.5]=2} - ** for i=1,200 do x = x+ t[k]; k = k == 1 and 1.5 or 1 end - ** assert(x == 300) - */ - return FAILFOLD; - } - return INTFOLD(k); - } else { - return INTFOLD((int32_t)n); - } -} - -LJFOLD(CONV KNUM IRCONV_U32_NUM) -LJFOLDF(kfold_conv_knum_u32_num) -{ - lua_assert((fins->op2 & IRCONV_TRUNC)); -#ifdef _MSC_VER - { /* Workaround for MSVC bug. */ - volatile uint32_t u = (uint32_t)knumleft; - return INTFOLD((int32_t)u); - } -#else - return INTFOLD((int32_t)(uint32_t)knumleft); -#endif -} - -LJFOLD(CONV KNUM IRCONV_I64_NUM) -LJFOLDF(kfold_conv_knum_i64_num) -{ - lua_assert((fins->op2 & IRCONV_TRUNC)); - return INT64FOLD((uint64_t)(int64_t)knumleft); -} - -LJFOLD(CONV KNUM IRCONV_U64_NUM) -LJFOLDF(kfold_conv_knum_u64_num) -{ - lua_assert((fins->op2 & IRCONV_TRUNC)); - return INT64FOLD(lj_num2u64(knumleft)); -} - -LJFOLD(TOSTR KNUM) -LJFOLDF(kfold_tostr_knum) -{ - return lj_ir_kstr(J, lj_str_fromnum(J->L, &knumleft)); -} - -LJFOLD(TOSTR KINT) -LJFOLDF(kfold_tostr_kint) -{ - return lj_ir_kstr(J, lj_str_fromint(J->L, fleft->i)); -} - -LJFOLD(STRTO KGC) -LJFOLDF(kfold_strto) -{ - TValue n; - if (lj_strscan_num(ir_kstr(fleft), &n)) - return lj_ir_knum(J, numV(&n)); - return FAILFOLD; -} - -/* -- Constant folding of equality checks --------------------------------- */ - -/* Don't constant-fold away FLOAD checks against KNULL. */ -LJFOLD(EQ FLOAD KNULL) -LJFOLD(NE FLOAD KNULL) -LJFOLDX(lj_opt_cse) - -/* But fold all other KNULL compares, since only KNULL is equal to KNULL. */ -LJFOLD(EQ any KNULL) -LJFOLD(NE any KNULL) -LJFOLD(EQ KNULL any) -LJFOLD(NE KNULL any) -LJFOLD(EQ KINT KINT) /* Constants are unique, so same refs <==> same value. */ -LJFOLD(NE KINT KINT) -LJFOLD(EQ KINT64 KINT64) -LJFOLD(NE KINT64 KINT64) -LJFOLD(EQ KGC KGC) -LJFOLD(NE KGC KGC) -LJFOLDF(kfold_kref) -{ - return CONDFOLD((fins->op1 == fins->op2) ^ (fins->o == IR_NE)); -} - -/* -- Algebraic shortcuts ------------------------------------------------- */ - -LJFOLD(FPMATH FPMATH IRFPM_FLOOR) -LJFOLD(FPMATH FPMATH IRFPM_CEIL) -LJFOLD(FPMATH FPMATH IRFPM_TRUNC) -LJFOLDF(shortcut_round) -{ - IRFPMathOp op = (IRFPMathOp)fleft->op2; - if (op == IRFPM_FLOOR || op == IRFPM_CEIL || op == IRFPM_TRUNC) - return LEFTFOLD; /* round(round_left(x)) = round_left(x) */ - return NEXTFOLD; -} - -LJFOLD(ABS ABS KNUM) -LJFOLDF(shortcut_left) -{ - return LEFTFOLD; /* f(g(x)) ==> g(x) */ -} - -LJFOLD(ABS NEG KNUM) -LJFOLDF(shortcut_dropleft) -{ - PHIBARRIER(fleft); - fins->op1 = fleft->op1; /* abs(neg(x)) ==> abs(x) */ - return RETRYFOLD; -} - -/* Note: no safe shortcuts with STRTO and TOSTR ("1e2" ==> +100 ==> "100"). */ -LJFOLD(NEG NEG any) -LJFOLD(BNOT BNOT) -LJFOLD(BSWAP BSWAP) -LJFOLDF(shortcut_leftleft) -{ - PHIBARRIER(fleft); /* See above. Fold would be ok, but not beneficial. */ - return fleft->op1; /* f(g(x)) ==> x */ -} - -/* -- FP algebraic simplifications ---------------------------------------- */ - -/* FP arithmetic is tricky -- there's not much to simplify. -** Please note the following common pitfalls before sending "improvements": -** x+0 ==> x is INVALID for x=-0 -** 0-x ==> -x is INVALID for x=+0 -** x*0 ==> 0 is INVALID for x=-0, x=+-Inf or x=NaN -*/ - -LJFOLD(ADD NEG any) -LJFOLDF(simplify_numadd_negx) -{ - PHIBARRIER(fleft); - fins->o = IR_SUB; /* (-a) + b ==> b - a */ - fins->op1 = fins->op2; - fins->op2 = fleft->op1; - return RETRYFOLD; -} - -LJFOLD(ADD any NEG) -LJFOLDF(simplify_numadd_xneg) -{ - PHIBARRIER(fright); - fins->o = IR_SUB; /* a + (-b) ==> a - b */ - fins->op2 = fright->op1; - return RETRYFOLD; -} - -LJFOLD(SUB any KNUM) -LJFOLDF(simplify_numsub_k) -{ - lua_Number n = knumright; - if (n == 0.0) /* x - (+-0) ==> x */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(SUB NEG KNUM) -LJFOLDF(simplify_numsub_negk) -{ - PHIBARRIER(fleft); - fins->op2 = fleft->op1; /* (-x) - k ==> (-k) - x */ - fins->op1 = (IRRef1)lj_ir_knum(J, -knumright); - return RETRYFOLD; -} - -LJFOLD(SUB any NEG) -LJFOLDF(simplify_numsub_xneg) -{ - PHIBARRIER(fright); - fins->o = IR_ADD; /* a - (-b) ==> a + b */ - fins->op2 = fright->op1; - return RETRYFOLD; -} - -LJFOLD(MUL any KNUM) -LJFOLD(DIV any KNUM) -LJFOLDF(simplify_nummuldiv_k) -{ - lua_Number n = knumright; - if (n == 1.0) { /* x o 1 ==> x */ - return LEFTFOLD; - } else if (n == -1.0) { /* x o -1 ==> -x */ - fins->o = IR_NEG; - fins->op2 = (IRRef1)lj_ir_knum_neg(J); - return RETRYFOLD; - } else if (fins->o == IR_MUL && n == 2.0) { /* x * 2 ==> x + x */ - fins->o = IR_ADD; - fins->op2 = fins->op1; - return RETRYFOLD; - } else if (fins->o == IR_DIV) { /* x / 2^k ==> x * 2^-k */ - uint64_t u = ir_knum(fright)->u64; - uint32_t ex = ((uint32_t)(u >> 52) & 0x7ff); - if ((u & U64x(000fffff,ffffffff)) == 0 && ex - 1 < 0x7fd) { - u = (u & ((uint64_t)1 << 63)) | ((uint64_t)(0x7fe - ex) << 52); - fins->o = IR_MUL; /* Multiply by exact reciprocal. */ - fins->op2 = lj_ir_knum_u64(J, u); - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(MUL NEG KNUM) -LJFOLD(DIV NEG KNUM) -LJFOLDF(simplify_nummuldiv_negk) -{ - PHIBARRIER(fleft); - fins->op1 = fleft->op1; /* (-a) o k ==> a o (-k) */ - fins->op2 = (IRRef1)lj_ir_knum(J, -knumright); - return RETRYFOLD; -} - -LJFOLD(MUL NEG NEG) -LJFOLD(DIV NEG NEG) -LJFOLDF(simplify_nummuldiv_negneg) -{ - PHIBARRIER(fleft); - PHIBARRIER(fright); - fins->op1 = fleft->op1; /* (-a) o (-b) ==> a o b */ - fins->op2 = fright->op1; - return RETRYFOLD; -} - -LJFOLD(POW any KINT) -LJFOLDF(simplify_numpow_xk) -{ - int32_t k = fright->i; - TRef ref = fins->op1; - if (k == 0) /* x ^ 0 ==> 1 */ - return lj_ir_knum_one(J); /* Result must be a number, not an int. */ - if (k == 1) /* x ^ 1 ==> x */ - return LEFTFOLD; - if ((uint32_t)(k+65536) > 2*65536u) /* Limit code explosion. */ - return NEXTFOLD; - if (k < 0) { /* x ^ (-k) ==> (1/x) ^ k. */ - ref = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), ref); - k = -k; - } - /* Unroll x^k for 1 <= k <= 65536. */ - for (; (k & 1) == 0; k >>= 1) /* Handle leading zeros. */ - ref = emitir(IRTN(IR_MUL), ref, ref); - if ((k >>= 1) != 0) { /* Handle trailing bits. */ - TRef tmp = emitir(IRTN(IR_MUL), ref, ref); - for (; k != 1; k >>= 1) { - if (k & 1) - ref = emitir(IRTN(IR_MUL), ref, tmp); - tmp = emitir(IRTN(IR_MUL), tmp, tmp); - } - ref = emitir(IRTN(IR_MUL), ref, tmp); - } - return ref; -} - -LJFOLD(POW KNUM any) -LJFOLDF(simplify_numpow_kx) -{ - lua_Number n = knumleft; - if (n == 2.0) { /* 2.0 ^ i ==> ldexp(1.0, tonum(i)) */ - fins->o = IR_CONV; -#if LJ_TARGET_X86ORX64 - fins->op1 = fins->op2; - fins->op2 = IRCONV_NUM_INT; - fins->op2 = (IRRef1)lj_opt_fold(J); -#endif - fins->op1 = (IRRef1)lj_ir_knum_one(J); - fins->o = IR_LDEXP; - return RETRYFOLD; - } - return NEXTFOLD; -} - -/* -- Simplify conversions ------------------------------------------------ */ - -LJFOLD(CONV CONV IRCONV_NUM_INT) /* _NUM */ -LJFOLDF(shortcut_conv_num_int) -{ - PHIBARRIER(fleft); - /* Only safe with a guarded conversion to int. */ - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_NUM && irt_isguard(fleft->t)) - return fleft->op1; /* f(g(x)) ==> x */ - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_INT_NUM) /* _INT */ -LJFOLD(CONV CONV IRCONV_U32_NUM) /* _U32*/ -LJFOLDF(simplify_conv_int_num) -{ - /* Fold even across PHI to avoid expensive num->int conversions in loop. */ - if ((fleft->op2 & IRCONV_SRCMASK) == - ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) - return fleft->op1; - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_I64_NUM) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_U64_NUM) /* _INT or _U32 */ -LJFOLDF(simplify_conv_i64_num) -{ - PHIBARRIER(fleft); - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) { - /* Reduce to a sign-extension. */ - fins->op1 = fleft->op1; - fins->op2 = ((IRT_I64<<5)|IRT_INT|IRCONV_SEXT); - return RETRYFOLD; - } else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) { -#if LJ_TARGET_X64 - return fleft->op1; -#else - /* Reduce to a zero-extension. */ - fins->op1 = fleft->op1; - fins->op2 = (IRT_I64<<5)|IRT_U32; - return RETRYFOLD; -#endif - } - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_INT_I64) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_INT_U64) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_U32_I64) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_U32_U64) /* _INT or _U32 */ -LJFOLDF(simplify_conv_int_i64) -{ - int src; - PHIBARRIER(fleft); - src = (fleft->op2 & IRCONV_SRCMASK); - if (src == IRT_INT || src == IRT_U32) { - if (src == ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) { - return fleft->op1; - } else { - fins->op2 = ((fins->op2 & IRCONV_DSTMASK) | src); - fins->op1 = fleft->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_FLOAT_NUM) /* _FLOAT */ -LJFOLDF(simplify_conv_flt_num) -{ - PHIBARRIER(fleft); - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_FLOAT) - return fleft->op1; - return NEXTFOLD; -} - -/* Shortcut TOBIT + IRT_NUM <- IRT_INT/IRT_U32 conversion. */ -LJFOLD(TOBIT CONV KNUM) -LJFOLDF(simplify_tobit_conv) -{ - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT || - (fleft->op2 & IRCONV_SRCMASK) == IRT_U32) { - /* Fold even across PHI to avoid expensive num->int conversions in loop. */ - lua_assert(irt_isnum(fleft->t)); - return fleft->op1; - } - return NEXTFOLD; -} - -/* Shortcut floor/ceil/round + IRT_NUM <- IRT_INT/IRT_U32 conversion. */ -LJFOLD(FPMATH CONV IRFPM_FLOOR) -LJFOLD(FPMATH CONV IRFPM_CEIL) -LJFOLD(FPMATH CONV IRFPM_TRUNC) -LJFOLDF(simplify_floor_conv) -{ - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT || - (fleft->op2 & IRCONV_SRCMASK) == IRT_U32) - return LEFTFOLD; - return NEXTFOLD; -} - -/* Strength reduction of widening. */ -LJFOLD(CONV any IRCONV_I64_INT) -LJFOLD(CONV any IRCONV_U64_INT) -LJFOLDF(simplify_conv_sext) -{ - IRRef ref = fins->op1; - int64_t ofs = 0; - if (!(fins->op2 & IRCONV_SEXT)) - return NEXTFOLD; - PHIBARRIER(fleft); - if (fleft->o == IR_XLOAD && (irt_isu8(fleft->t) || irt_isu16(fleft->t))) - goto ok_reduce; - if (fleft->o == IR_ADD && irref_isk(fleft->op2)) { - ofs = (int64_t)IR(fleft->op2)->i; - ref = fleft->op1; - } - /* Use scalar evolution analysis results to strength-reduce sign-extension. */ - if (ref == J->scev.idx) { - IRRef lo = J->scev.dir ? J->scev.start : J->scev.stop; - lua_assert(irt_isint(J->scev.t)); - if (lo && IR(lo)->i + ofs >= 0) { - ok_reduce: -#if LJ_TARGET_X64 - /* Eliminate widening. All 32 bit ops do an implicit zero-extension. */ - return LEFTFOLD; -#else - /* Reduce to a (cheaper) zero-extension. */ - fins->op2 &= ~IRCONV_SEXT; - return RETRYFOLD; -#endif - } - } - return NEXTFOLD; -} - -/* Strength reduction of narrowing. */ -LJFOLD(CONV ADD IRCONV_INT_I64) -LJFOLD(CONV SUB IRCONV_INT_I64) -LJFOLD(CONV MUL IRCONV_INT_I64) -LJFOLD(CONV ADD IRCONV_INT_U64) -LJFOLD(CONV SUB IRCONV_INT_U64) -LJFOLD(CONV MUL IRCONV_INT_U64) -LJFOLD(CONV ADD IRCONV_U32_I64) -LJFOLD(CONV SUB IRCONV_U32_I64) -LJFOLD(CONV MUL IRCONV_U32_I64) -LJFOLD(CONV ADD IRCONV_U32_U64) -LJFOLD(CONV SUB IRCONV_U32_U64) -LJFOLD(CONV MUL IRCONV_U32_U64) -LJFOLDF(simplify_conv_narrow) -{ - IROp op = (IROp)fleft->o; - IRType t = irt_type(fins->t); - IRRef op1 = fleft->op1, op2 = fleft->op2, mode = fins->op2; - PHIBARRIER(fleft); - op1 = emitir(IRTI(IR_CONV), op1, mode); - op2 = emitir(IRTI(IR_CONV), op2, mode); - fins->ot = IRT(op, t); - fins->op1 = op1; - fins->op2 = op2; - return RETRYFOLD; -} - -/* Special CSE rule for CONV. */ -LJFOLD(CONV any any) -LJFOLDF(cse_conv) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { - IRRef op1 = fins->op1, op2 = (fins->op2 & IRCONV_MODEMASK); - uint8_t guard = irt_isguard(fins->t); - IRRef ref = J->chain[IR_CONV]; - while (ref > op1) { - IRIns *ir = IR(ref); - /* Commoning with stronger checks is ok. */ - if (ir->op1 == op1 && (ir->op2 & IRCONV_MODEMASK) == op2 && - irt_isguard(ir->t) >= guard) - return ref; - ref = ir->prev; - } - } - return EMITFOLD; /* No fallthrough to regular CSE. */ -} - -/* FP conversion narrowing. */ -LJFOLD(TOBIT ADD KNUM) -LJFOLD(TOBIT SUB KNUM) -LJFOLD(CONV ADD IRCONV_INT_NUM) -LJFOLD(CONV SUB IRCONV_INT_NUM) -LJFOLD(CONV ADD IRCONV_I64_NUM) -LJFOLD(CONV SUB IRCONV_I64_NUM) -LJFOLDF(narrow_convert) -{ - PHIBARRIER(fleft); - /* Narrowing ignores PHIs and repeating it inside the loop is not useful. */ - if (J->chain[IR_LOOP]) - return NEXTFOLD; - lua_assert(fins->o != IR_CONV || (fins->op2&IRCONV_CONVMASK) != IRCONV_TOBIT); - return lj_opt_narrow_convert(J); -} - -/* -- Integer algebraic simplifications ----------------------------------- */ - -LJFOLD(ADD any KINT) -LJFOLD(ADDOV any KINT) -LJFOLD(SUBOV any KINT) -LJFOLDF(simplify_intadd_k) -{ - if (fright->i == 0) /* i o 0 ==> i */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(MULOV any KINT) -LJFOLDF(simplify_intmul_k) -{ - if (fright->i == 0) /* i * 0 ==> 0 */ - return RIGHTFOLD; - if (fright->i == 1) /* i * 1 ==> i */ - return LEFTFOLD; - if (fright->i == 2) { /* i * 2 ==> i + i */ - fins->o = IR_ADDOV; - fins->op2 = fins->op1; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(SUB any KINT) -LJFOLDF(simplify_intsub_k) -{ - if (fright->i == 0) /* i - 0 ==> i */ - return LEFTFOLD; - fins->o = IR_ADD; /* i - k ==> i + (-k) */ - fins->op2 = (IRRef1)lj_ir_kint(J, -fright->i); /* Overflow for -2^31 ok. */ - return RETRYFOLD; -} - -LJFOLD(SUB KINT any) -LJFOLD(SUB KINT64 any) -LJFOLDF(simplify_intsub_kleft) -{ - if (fleft->o == IR_KINT ? (fleft->i == 0) : (ir_kint64(fleft)->u64 == 0)) { - fins->o = IR_NEG; /* 0 - i ==> -i */ - fins->op1 = fins->op2; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(ADD any KINT64) -LJFOLDF(simplify_intadd_k64) -{ - if (ir_kint64(fright)->u64 == 0) /* i + 0 ==> i */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(SUB any KINT64) -LJFOLDF(simplify_intsub_k64) -{ - uint64_t k = ir_kint64(fright)->u64; - if (k == 0) /* i - 0 ==> i */ - return LEFTFOLD; - fins->o = IR_ADD; /* i - k ==> i + (-k) */ - fins->op2 = (IRRef1)lj_ir_kint64(J, (uint64_t)-(int64_t)k); - return RETRYFOLD; -} - -static TRef simplify_intmul_k(jit_State *J, int32_t k) -{ - /* Note: many more simplifications are possible, e.g. 2^k1 +- 2^k2. - ** But this is mainly intended for simple address arithmetic. - ** Also it's easier for the backend to optimize the original multiplies. - */ - if (k == 1) { /* i * 1 ==> i */ - return LEFTFOLD; - } else if ((k & (k-1)) == 0) { /* i * 2^k ==> i << k */ - fins->o = IR_BSHL; - fins->op2 = lj_ir_kint(J, lj_fls((uint32_t)k)); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(MUL any KINT) -LJFOLDF(simplify_intmul_k32) -{ - if (fright->i == 0) /* i * 0 ==> 0 */ - return INTFOLD(0); - else if (fright->i > 0) - return simplify_intmul_k(J, fright->i); - return NEXTFOLD; -} - -LJFOLD(MUL any KINT64) -LJFOLDF(simplify_intmul_k64) -{ - if (ir_kint64(fright)->u64 == 0) /* i * 0 ==> 0 */ - return INT64FOLD(0); -#if LJ_64 - /* NYI: SPLIT for BSHL and 32 bit backend support. */ - else if (ir_kint64(fright)->u64 < 0x80000000u) - return simplify_intmul_k(J, (int32_t)ir_kint64(fright)->u64); -#endif - return NEXTFOLD; -} - -LJFOLD(MOD any KINT) -LJFOLDF(simplify_intmod_k) -{ - int32_t k = fright->i; - lua_assert(k != 0); - if (k > 0 && (k & (k-1)) == 0) { /* i % (2^k) ==> i & (2^k-1) */ - fins->o = IR_BAND; - fins->op2 = lj_ir_kint(J, k-1); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(MOD KINT any) -LJFOLDF(simplify_intmod_kleft) -{ - if (fleft->i == 0) - return INTFOLD(0); - return NEXTFOLD; -} - -LJFOLD(SUB any any) -LJFOLD(SUBOV any any) -LJFOLDF(simplify_intsub) -{ - if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) /* i - i ==> 0 */ - return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0); - return NEXTFOLD; -} - -LJFOLD(SUB ADD any) -LJFOLDF(simplify_intsubadd_leftcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fleft); - if (fins->op2 == fleft->op1) /* (i + j) - i ==> j */ - return fleft->op2; - if (fins->op2 == fleft->op2) /* (i + j) - j ==> i */ - return fleft->op1; - } - return NEXTFOLD; -} - -LJFOLD(SUB SUB any) -LJFOLDF(simplify_intsubsub_leftcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fleft); - if (fins->op2 == fleft->op1) { /* (i - j) - i ==> 0 - j */ - fins->op1 = (IRRef1)lj_ir_kint(J, 0); - fins->op2 = fleft->op2; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(SUB any SUB) -LJFOLDF(simplify_intsubsub_rightcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fright); - if (fins->op1 == fright->op1) /* i - (i - j) ==> j */ - return fright->op2; - } - return NEXTFOLD; -} - -LJFOLD(SUB any ADD) -LJFOLDF(simplify_intsubadd_rightcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fright); - if (fins->op1 == fright->op1) { /* i - (i + j) ==> 0 - j */ - fins->op2 = fright->op2; - fins->op1 = (IRRef1)lj_ir_kint(J, 0); - return RETRYFOLD; - } - if (fins->op1 == fright->op2) { /* i - (j + i) ==> 0 - j */ - fins->op2 = fright->op1; - fins->op1 = (IRRef1)lj_ir_kint(J, 0); - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(SUB ADD ADD) -LJFOLDF(simplify_intsubaddadd_cancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fleft); - PHIBARRIER(fright); - if (fleft->op1 == fright->op1) { /* (i + j1) - (i + j2) ==> j1 - j2 */ - fins->op1 = fleft->op2; - fins->op2 = fright->op2; - return RETRYFOLD; - } - if (fleft->op1 == fright->op2) { /* (i + j1) - (j2 + i) ==> j1 - j2 */ - fins->op1 = fleft->op2; - fins->op2 = fright->op1; - return RETRYFOLD; - } - if (fleft->op2 == fright->op1) { /* (j1 + i) - (i + j2) ==> j1 - j2 */ - fins->op1 = fleft->op1; - fins->op2 = fright->op2; - return RETRYFOLD; - } - if (fleft->op2 == fright->op2) { /* (j1 + i) - (j2 + i) ==> j1 - j2 */ - fins->op1 = fleft->op1; - fins->op2 = fright->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(BAND any KINT) -LJFOLD(BAND any KINT64) -LJFOLDF(simplify_band_k) -{ - int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : - (int64_t)ir_k64(fright)->u64; - if (k == 0) /* i & 0 ==> 0 */ - return RIGHTFOLD; - if (k == -1) /* i & -1 ==> i */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(BOR any KINT) -LJFOLD(BOR any KINT64) -LJFOLDF(simplify_bor_k) -{ - int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : - (int64_t)ir_k64(fright)->u64; - if (k == 0) /* i | 0 ==> i */ - return LEFTFOLD; - if (k == -1) /* i | -1 ==> -1 */ - return RIGHTFOLD; - return NEXTFOLD; -} - -LJFOLD(BXOR any KINT) -LJFOLD(BXOR any KINT64) -LJFOLDF(simplify_bxor_k) -{ - int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : - (int64_t)ir_k64(fright)->u64; - if (k == 0) /* i xor 0 ==> i */ - return LEFTFOLD; - if (k == -1) { /* i xor -1 ==> ~i */ - fins->o = IR_BNOT; - fins->op2 = 0; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(BSHL any KINT) -LJFOLD(BSHR any KINT) -LJFOLD(BSAR any KINT) -LJFOLD(BROL any KINT) -LJFOLD(BROR any KINT) -LJFOLDF(simplify_shift_ik) -{ - int32_t mask = irt_is64(fins->t) ? 63 : 31; - int32_t k = (fright->i & mask); - if (k == 0) /* i o 0 ==> i */ - return LEFTFOLD; - if (k == 1 && fins->o == IR_BSHL) { /* i << 1 ==> i + i */ - fins->o = IR_ADD; - fins->op2 = fins->op1; - return RETRYFOLD; - } - if (k != fright->i) { /* i o k ==> i o (k & mask) */ - fins->op2 = (IRRef1)lj_ir_kint(J, k); - return RETRYFOLD; - } -#ifndef LJ_TARGET_UNIFYROT - if (fins->o == IR_BROR) { /* bror(i, k) ==> brol(i, (-k)&mask) */ - fins->o = IR_BROL; - fins->op2 = (IRRef1)lj_ir_kint(J, (-k)&mask); - return RETRYFOLD; - } -#endif - return NEXTFOLD; -} - -LJFOLD(BSHL any BAND) -LJFOLD(BSHR any BAND) -LJFOLD(BSAR any BAND) -LJFOLD(BROL any BAND) -LJFOLD(BROR any BAND) -LJFOLDF(simplify_shift_andk) -{ - IRIns *irk = IR(fright->op2); - PHIBARRIER(fright); - if ((fins->o < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && - irk->o == IR_KINT) { /* i o (j & mask) ==> i o j */ - int32_t mask = irt_is64(fins->t) ? 63 : 31; - int32_t k = irk->i & mask; - if (k == mask) { - fins->op2 = fright->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(BSHL KINT any) -LJFOLD(BSHR KINT any) -LJFOLD(BSHL KINT64 any) -LJFOLD(BSHR KINT64 any) -LJFOLDF(simplify_shift1_ki) -{ - int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i : - (int64_t)ir_k64(fleft)->u64; - if (k == 0) /* 0 o i ==> 0 */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(BSAR KINT any) -LJFOLD(BROL KINT any) -LJFOLD(BROR KINT any) -LJFOLD(BSAR KINT64 any) -LJFOLD(BROL KINT64 any) -LJFOLD(BROR KINT64 any) -LJFOLDF(simplify_shift2_ki) -{ - int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i : - (int64_t)ir_k64(fleft)->u64; - if (k == 0 || k == -1) /* 0 o i ==> 0; -1 o i ==> -1 */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(BSHL BAND KINT) -LJFOLD(BSHR BAND KINT) -LJFOLD(BROL BAND KINT) -LJFOLD(BROR BAND KINT) -LJFOLDF(simplify_shiftk_andk) -{ - IRIns *irk = IR(fleft->op2); - PHIBARRIER(fleft); - if (irk->o == IR_KINT) { /* (i & k1) o k2 ==> (i o k2) & (k1 o k2) */ - int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o); - fins->op1 = fleft->op1; - fins->op1 = (IRRef1)lj_opt_fold(J); - fins->op2 = (IRRef1)lj_ir_kint(J, k); - fins->ot = IRTI(IR_BAND); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(BAND BSHL KINT) -LJFOLD(BAND BSHR KINT) -LJFOLDF(simplify_andk_shiftk) -{ - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KINT && - kfold_intop(-1, irk->i, (IROp)fleft->o) == fright->i) - return LEFTFOLD; /* (i o k1) & k2 ==> i, if (-1 o k1) == k2 */ - return NEXTFOLD; -} - -/* -- Reassociation ------------------------------------------------------- */ - -LJFOLD(ADD ADD KINT) -LJFOLD(MUL MUL KINT) -LJFOLD(BAND BAND KINT) -LJFOLD(BOR BOR KINT) -LJFOLD(BXOR BXOR KINT) -LJFOLDF(reassoc_intarith_k) -{ - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KINT) { - int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o); - if (k == irk->i) /* (i o k1) o k2 ==> i o k1, if (k1 o k2) == k1. */ - return LEFTFOLD; - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint(J, k); - return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ - } - return NEXTFOLD; -} - -LJFOLD(ADD ADD KINT64) -LJFOLD(MUL MUL KINT64) -LJFOLD(BAND BAND KINT64) -LJFOLD(BOR BOR KINT64) -LJFOLD(BXOR BXOR KINT64) -LJFOLDF(reassoc_intarith_k64) -{ -#if LJ_HASFFI || LJ_64 - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KINT64) { - uint64_t k = kfold_int64arith(ir_k64(irk)->u64, - ir_k64(fright)->u64, (IROp)fins->o); - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint64(J, k); - return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ - } - return NEXTFOLD; -#else - UNUSED(J); lua_assert(0); return FAILFOLD; -#endif -} - -LJFOLD(MIN MIN any) -LJFOLD(MAX MAX any) -LJFOLD(BAND BAND any) -LJFOLD(BOR BOR any) -LJFOLDF(reassoc_dup) -{ - if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2) - return LEFTFOLD; /* (a o b) o a ==> a o b; (a o b) o b ==> a o b */ - return NEXTFOLD; -} - -LJFOLD(BXOR BXOR any) -LJFOLDF(reassoc_bxor) -{ - PHIBARRIER(fleft); - if (fins->op2 == fleft->op1) /* (a xor b) xor a ==> b */ - return fleft->op2; - if (fins->op2 == fleft->op2) /* (a xor b) xor b ==> a */ - return fleft->op1; - return NEXTFOLD; -} - -LJFOLD(BSHL BSHL KINT) -LJFOLD(BSHR BSHR KINT) -LJFOLD(BSAR BSAR KINT) -LJFOLD(BROL BROL KINT) -LJFOLD(BROR BROR KINT) -LJFOLDF(reassoc_shift) -{ - IRIns *irk = IR(fleft->op2); - PHIBARRIER(fleft); /* The (shift any KINT) rule covers k2 == 0 and more. */ - if (irk->o == IR_KINT) { /* (i o k1) o k2 ==> i o (k1 + k2) */ - int32_t mask = irt_is64(fins->t) ? 63 : 31; - int32_t k = (irk->i & mask) + (fright->i & mask); - if (k > mask) { /* Combined shift too wide? */ - if (fins->o == IR_BSHL || fins->o == IR_BSHR) - return mask == 31 ? INTFOLD(0) : INT64FOLD(0); - else if (fins->o == IR_BSAR) - k = mask; - else - k &= mask; - } - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint(J, k); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(MIN MIN KNUM) -LJFOLD(MAX MAX KNUM) -LJFOLD(MIN MIN KINT) -LJFOLD(MAX MAX KINT) -LJFOLDF(reassoc_minmax_k) -{ - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KNUM) { - lua_Number a = ir_knum(irk)->n; - lua_Number y = lj_vm_foldarith(a, knumright, fins->o - IR_ADD); - if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */ - return LEFTFOLD; - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_knum(J, y); - return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */ - } else if (irk->o == IR_KINT) { - int32_t a = irk->i; - int32_t y = kfold_intop(a, fright->i, fins->o); - if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */ - return LEFTFOLD; - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint(J, y); - return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */ - } - return NEXTFOLD; -} - -LJFOLD(MIN MAX any) -LJFOLD(MAX MIN any) -LJFOLDF(reassoc_minmax_left) -{ - if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2) - return RIGHTFOLD; /* (b o1 a) o2 b ==> b; (a o1 b) o2 b ==> b */ - return NEXTFOLD; -} - -LJFOLD(MIN any MAX) -LJFOLD(MAX any MIN) -LJFOLDF(reassoc_minmax_right) -{ - if (fins->op1 == fright->op1 || fins->op1 == fright->op2) - return LEFTFOLD; /* a o2 (a o1 b) ==> a; a o2 (b o1 a) ==> a */ - return NEXTFOLD; -} - -/* -- Array bounds check elimination -------------------------------------- */ - -/* Eliminate ABC across PHIs to handle t[i-1] forwarding case. -** ABC(asize, (i+k)+(-k)) ==> ABC(asize, i), but only if it already exists. -** Could be generalized to (i+k1)+k2 ==> i+(k1+k2), but needs better disambig. -*/ -LJFOLD(ABC any ADD) -LJFOLDF(abc_fwd) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) { - if (irref_isk(fright->op2)) { - IRIns *add2 = IR(fright->op1); - if (add2->o == IR_ADD && irref_isk(add2->op2) && - IR(fright->op2)->i == -IR(add2->op2)->i) { - IRRef ref = J->chain[IR_ABC]; - IRRef lim = add2->op1; - if (fins->op1 > lim) lim = fins->op1; - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op1 == fins->op1 && ir->op2 == add2->op1) - return DROPFOLD; - ref = ir->prev; - } - } - } - } - return NEXTFOLD; -} - -/* Eliminate ABC for constants. -** ABC(asize, k1), ABC(asize k2) ==> ABC(asize, max(k1, k2)) -** Drop second ABC if k2 is lower. Otherwise patch first ABC with k2. -*/ -LJFOLD(ABC any KINT) -LJFOLDF(abc_k) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) { - IRRef ref = J->chain[IR_ABC]; - IRRef asize = fins->op1; - while (ref > asize) { - IRIns *ir = IR(ref); - if (ir->op1 == asize && irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (fright->i > k) - ir->op2 = fins->op2; - return DROPFOLD; - } - ref = ir->prev; - } - return EMITFOLD; /* Already performed CSE. */ - } - return NEXTFOLD; -} - -/* Eliminate invariant ABC inside loop. */ -LJFOLD(ABC any any) -LJFOLDF(abc_invar) -{ - if (!irt_isint(fins->t) && J->chain[IR_LOOP]) /* Currently marked as PTR. */ - return DROPFOLD; - return NEXTFOLD; -} - -/* -- Commutativity ------------------------------------------------------- */ - -/* The refs of commutative ops are canonicalized. Lower refs go to the right. -** Rationale behind this: -** - It (also) moves constants to the right. -** - It reduces the number of FOLD rules (e.g. (BOR any KINT) suffices). -** - It helps CSE to find more matches. -** - The assembler generates better code with constants at the right. -*/ - -LJFOLD(ADD any any) -LJFOLD(MUL any any) -LJFOLD(ADDOV any any) -LJFOLD(MULOV any any) -LJFOLDF(comm_swap) -{ - if (fins->op1 < fins->op2) { /* Move lower ref to the right. */ - IRRef1 tmp = fins->op1; - fins->op1 = fins->op2; - fins->op2 = tmp; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(EQ any any) -LJFOLD(NE any any) -LJFOLDF(comm_equal) -{ - /* For non-numbers only: x == x ==> drop; x ~= x ==> fail */ - if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) - return CONDFOLD(fins->o == IR_EQ); - return fold_comm_swap(J); -} - -LJFOLD(LT any any) -LJFOLD(GE any any) -LJFOLD(LE any any) -LJFOLD(GT any any) -LJFOLD(ULT any any) -LJFOLD(UGE any any) -LJFOLD(ULE any any) -LJFOLD(UGT any any) -LJFOLDF(comm_comp) -{ - /* For non-numbers only: x <=> x ==> drop; x <> x ==> fail */ - if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) - return CONDFOLD((fins->o ^ (fins->o >> 1)) & 1); - if (fins->op1 < fins->op2) { /* Move lower ref to the right. */ - IRRef1 tmp = fins->op1; - fins->op1 = fins->op2; - fins->op2 = tmp; - fins->o ^= 3; /* GT <-> LT, GE <-> LE, does not affect U */ - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(BAND any any) -LJFOLD(BOR any any) -LJFOLD(MIN any any) -LJFOLD(MAX any any) -LJFOLDF(comm_dup) -{ - if (fins->op1 == fins->op2) /* x o x ==> x */ - return LEFTFOLD; - return fold_comm_swap(J); -} - -LJFOLD(BXOR any any) -LJFOLDF(comm_bxor) -{ - if (fins->op1 == fins->op2) /* i xor i ==> 0 */ - return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0); - return fold_comm_swap(J); -} - -/* -- Simplification of compound expressions ------------------------------ */ - -static TRef kfold_xload(jit_State *J, IRIns *ir, const void *p) -{ - int32_t k; - switch (irt_type(ir->t)) { - case IRT_NUM: return lj_ir_knum_u64(J, *(uint64_t *)p); - case IRT_I8: k = (int32_t)*(int8_t *)p; break; - case IRT_U8: k = (int32_t)*(uint8_t *)p; break; - case IRT_I16: k = (int32_t)(int16_t)lj_getu16(p); break; - case IRT_U16: k = (int32_t)(uint16_t)lj_getu16(p); break; - case IRT_INT: case IRT_U32: k = (int32_t)lj_getu32(p); break; - case IRT_I64: case IRT_U64: return lj_ir_kint64(J, *(uint64_t *)p); - default: return 0; - } - return lj_ir_kint(J, k); -} - -/* Turn: string.sub(str, a, b) == kstr -** into: string.byte(str, a) == string.byte(kstr, 1) etc. -** Note: this creates unaligned XLOADs on x86/x64. -*/ -LJFOLD(EQ SNEW KGC) -LJFOLD(NE SNEW KGC) -LJFOLDF(merge_eqne_snew_kgc) -{ - GCstr *kstr = ir_kstr(fright); - int32_t len = (int32_t)kstr->len; - lua_assert(irt_isstr(fins->t)); - -#if LJ_TARGET_UNALIGNED -#define FOLD_SNEW_MAX_LEN 4 /* Handle string lengths 0, 1, 2, 3, 4. */ -#define FOLD_SNEW_TYPE8 IRT_I8 /* Creates shorter immediates. */ -#else -#define FOLD_SNEW_MAX_LEN 1 /* Handle string lengths 0 or 1. */ -#define FOLD_SNEW_TYPE8 IRT_U8 /* Prefer unsigned loads. */ -#endif - - PHIBARRIER(fleft); - if (len <= FOLD_SNEW_MAX_LEN) { - IROp op = (IROp)fins->o; - IRRef strref = fleft->op1; - lua_assert(IR(strref)->o == IR_STRREF); - if (op == IR_EQ) { - emitir(IRTGI(IR_EQ), fleft->op2, lj_ir_kint(J, len)); - /* Caveat: fins/fleft/fright is no longer valid after emitir. */ - } else { - /* NE is not expanded since this would need an OR of two conds. */ - if (!irref_isk(fleft->op2)) /* Only handle the constant length case. */ - return NEXTFOLD; - if (IR(fleft->op2)->i != len) - return DROPFOLD; - } - if (len > 0) { - /* A 4 byte load for length 3 is ok -- all strings have an extra NUL. */ - uint16_t ot = (uint16_t)(len == 1 ? IRT(IR_XLOAD, FOLD_SNEW_TYPE8) : - len == 2 ? IRT(IR_XLOAD, IRT_U16) : - IRTI(IR_XLOAD)); - TRef tmp = emitir(ot, strref, - IRXLOAD_READONLY | (len > 1 ? IRXLOAD_UNALIGNED : 0)); - TRef val = kfold_xload(J, IR(tref_ref(tmp)), strdata(kstr)); - if (len == 3) - tmp = emitir(IRTI(IR_BAND), tmp, - lj_ir_kint(J, LJ_ENDIAN_SELECT(0x00ffffff, 0xffffff00))); - fins->op1 = (IRRef1)tmp; - fins->op2 = (IRRef1)val; - fins->ot = (IROpT)IRTGI(op); - return RETRYFOLD; - } else { - return DROPFOLD; - } - } - return NEXTFOLD; -} - -/* -- Loads --------------------------------------------------------------- */ - -/* Loads cannot be folded or passed on to CSE in general. -** Alias analysis is needed to check for forwarding opportunities. -** -** Caveat: *all* loads must be listed here or they end up at CSE! -*/ - -LJFOLD(ALOAD any) -LJFOLDX(lj_opt_fwd_aload) - -/* From HREF fwd (see below). Must eliminate, not supported by fwd/backend. */ -LJFOLD(HLOAD KKPTR) -LJFOLDF(kfold_hload_kkptr) -{ - UNUSED(J); - lua_assert(ir_kptr(fleft) == niltvg(J2G(J))); - return TREF_NIL; -} - -LJFOLD(HLOAD any) -LJFOLDX(lj_opt_fwd_hload) - -LJFOLD(ULOAD any) -LJFOLDX(lj_opt_fwd_uload) - -LJFOLD(CALLL any IRCALL_lj_tab_len) -LJFOLDX(lj_opt_fwd_tab_len) - -/* Upvalue refs are really loads, but there are no corresponding stores. -** So CSE is ok for them, except for UREFO across a GC step (see below). -** If the referenced function is const, its upvalue addresses are const, too. -** This can be used to improve CSE by looking for the same address, -** even if the upvalues originate from a different function. -*/ -LJFOLD(UREFO KGC any) -LJFOLD(UREFC KGC any) -LJFOLDF(cse_uref) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { - IRRef ref = J->chain[fins->o]; - GCfunc *fn = ir_kfunc(fleft); - GCupval *uv = gco2uv(gcref(fn->l.uvptr[(fins->op2 >> 8)])); - while (ref > 0) { - IRIns *ir = IR(ref); - if (irref_isk(ir->op1)) { - GCfunc *fn2 = ir_kfunc(IR(ir->op1)); - if (gco2uv(gcref(fn2->l.uvptr[(ir->op2 >> 8)])) == uv) { - if (fins->o == IR_UREFO && gcstep_barrier(J, ref)) - break; - return ref; - } - } - ref = ir->prev; - } - } - return EMITFOLD; -} - -LJFOLD(HREFK any any) -LJFOLDX(lj_opt_fwd_hrefk) - -LJFOLD(HREF TNEW any) -LJFOLDF(fwd_href_tnew) -{ - if (lj_opt_fwd_href_nokey(J)) - return lj_ir_kkptr(J, niltvg(J2G(J))); - return NEXTFOLD; -} - -LJFOLD(HREF TDUP KPRI) -LJFOLD(HREF TDUP KGC) -LJFOLD(HREF TDUP KNUM) -LJFOLDF(fwd_href_tdup) -{ - TValue keyv; - lj_ir_kvalue(J->L, &keyv, fright); - if (lj_tab_get(J->L, ir_ktab(IR(fleft->op1)), &keyv) == niltvg(J2G(J)) && - lj_opt_fwd_href_nokey(J)) - return lj_ir_kkptr(J, niltvg(J2G(J))); - return NEXTFOLD; -} - -/* We can safely FOLD/CSE array/hash refs and field loads, since there -** are no corresponding stores. But we need to check for any NEWREF with -** an aliased table, as it may invalidate all of the pointers and fields. -** Only HREF needs the NEWREF check -- AREF and HREFK already depend on -** FLOADs. And NEWREF itself is treated like a store (see below). -*/ -LJFOLD(FLOAD TNEW IRFL_TAB_ASIZE) -LJFOLDF(fload_tab_tnew_asize) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD(fleft->op1); - return NEXTFOLD; -} - -LJFOLD(FLOAD TNEW IRFL_TAB_HMASK) -LJFOLDF(fload_tab_tnew_hmask) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD((1 << fleft->op2)-1); - return NEXTFOLD; -} - -LJFOLD(FLOAD TDUP IRFL_TAB_ASIZE) -LJFOLDF(fload_tab_tdup_asize) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->asize); - return NEXTFOLD; -} - -LJFOLD(FLOAD TDUP IRFL_TAB_HMASK) -LJFOLDF(fload_tab_tdup_hmask) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->hmask); - return NEXTFOLD; -} - -LJFOLD(HREF any any) -LJFOLD(FLOAD any IRFL_TAB_ARRAY) -LJFOLD(FLOAD any IRFL_TAB_NODE) -LJFOLD(FLOAD any IRFL_TAB_ASIZE) -LJFOLD(FLOAD any IRFL_TAB_HMASK) -LJFOLDF(fload_tab_ah) -{ - TRef tr = lj_opt_cse(J); - return lj_opt_fwd_tptr(J, tref_ref(tr)) ? tr : EMITFOLD; -} - -/* Strings are immutable, so we can safely FOLD/CSE the related FLOAD. */ -LJFOLD(FLOAD KGC IRFL_STR_LEN) -LJFOLDF(fload_str_len_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return INTFOLD((int32_t)ir_kstr(fleft)->len); - return NEXTFOLD; -} - -LJFOLD(FLOAD SNEW IRFL_STR_LEN) -LJFOLDF(fload_str_len_snew) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) { - PHIBARRIER(fleft); - return fleft->op2; - } - return NEXTFOLD; -} - -/* The C type ID of cdata objects is immutable. */ -LJFOLD(FLOAD KGC IRFL_CDATA_CTYPEID) -LJFOLDF(fload_cdata_typeid_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return INTFOLD((int32_t)ir_kcdata(fleft)->ctypeid); - return NEXTFOLD; -} - -/* Get the contents of immutable cdata objects. */ -LJFOLD(FLOAD KGC IRFL_CDATA_PTR) -LJFOLD(FLOAD KGC IRFL_CDATA_INT) -LJFOLD(FLOAD KGC IRFL_CDATA_INT64) -LJFOLDF(fload_cdata_int64_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) { - void *p = cdataptr(ir_kcdata(fleft)); - if (irt_is64(fins->t)) - return INT64FOLD(*(uint64_t *)p); - else - return INTFOLD(*(int32_t *)p); - } - return NEXTFOLD; -} - -LJFOLD(FLOAD CNEW IRFL_CDATA_CTYPEID) -LJFOLD(FLOAD CNEWI IRFL_CDATA_CTYPEID) -LJFOLDF(fload_cdata_typeid_cnew) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return fleft->op1; /* No PHI barrier needed. CNEW/CNEWI op1 is const. */ - return NEXTFOLD; -} - -/* Pointer, int and int64 cdata objects are immutable. */ -LJFOLD(FLOAD CNEWI IRFL_CDATA_PTR) -LJFOLD(FLOAD CNEWI IRFL_CDATA_INT) -LJFOLD(FLOAD CNEWI IRFL_CDATA_INT64) -LJFOLDF(fload_cdata_ptr_int64_cnew) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return fleft->op2; /* Fold even across PHI to avoid allocations. */ - return NEXTFOLD; -} - -LJFOLD(FLOAD any IRFL_STR_LEN) -LJFOLD(FLOAD any IRFL_CDATA_CTYPEID) -LJFOLD(FLOAD any IRFL_CDATA_PTR) -LJFOLD(FLOAD any IRFL_CDATA_INT) -LJFOLD(FLOAD any IRFL_CDATA_INT64) -LJFOLD(VLOAD any any) /* Vararg loads have no corresponding stores. */ -LJFOLDX(lj_opt_cse) - -/* All other field loads need alias analysis. */ -LJFOLD(FLOAD any any) -LJFOLDX(lj_opt_fwd_fload) - -/* This is for LOOP only. Recording handles SLOADs internally. */ -LJFOLD(SLOAD any any) -LJFOLDF(fwd_sload) -{ - if ((fins->op2 & IRSLOAD_FRAME)) { - TRef tr = lj_opt_cse(J); - return tref_ref(tr) < J->chain[IR_RETF] ? EMITFOLD : tr; - } else { - lua_assert(J->slot[fins->op1] != 0); - return J->slot[fins->op1]; - } -} - -/* Only fold for KKPTR. The pointer _and_ the contents must be const. */ -LJFOLD(XLOAD KKPTR any) -LJFOLDF(xload_kptr) -{ - TRef tr = kfold_xload(J, fins, ir_kptr(fleft)); - return tr ? tr : NEXTFOLD; -} - -LJFOLD(XLOAD any any) -LJFOLDX(lj_opt_fwd_xload) - -/* -- Write barriers ------------------------------------------------------ */ - -/* Write barriers are amenable to CSE, but not across any incremental -** GC steps. -** -** The same logic applies to open upvalue references, because a stack -** may be resized during a GC step (not the current stack, but maybe that -** of a coroutine). -*/ -LJFOLD(TBAR any) -LJFOLD(OBAR any any) -LJFOLD(UREFO any any) -LJFOLDF(barrier_tab) -{ - TRef tr = lj_opt_cse(J); - if (gcstep_barrier(J, tref_ref(tr))) /* CSE across GC step? */ - return EMITFOLD; /* Raw emit. Assumes fins is left intact by CSE. */ - return tr; -} - -LJFOLD(TBAR TNEW) -LJFOLD(TBAR TDUP) -LJFOLDF(barrier_tnew_tdup) -{ - /* New tables are always white and never need a barrier. */ - if (fins->op1 < J->chain[IR_LOOP]) /* Except across a GC step. */ - return NEXTFOLD; - return DROPFOLD; -} - -/* -- Stores and allocations ---------------------------------------------- */ - -/* Stores and allocations cannot be folded or passed on to CSE in general. -** But some stores can be eliminated with dead-store elimination (DSE). -** -** Caveat: *all* stores and allocs must be listed here or they end up at CSE! -*/ - -LJFOLD(ASTORE any any) -LJFOLD(HSTORE any any) -LJFOLDX(lj_opt_dse_ahstore) - -LJFOLD(USTORE any any) -LJFOLDX(lj_opt_dse_ustore) - -LJFOLD(FSTORE any any) -LJFOLDX(lj_opt_dse_fstore) - -LJFOLD(XSTORE any any) -LJFOLDX(lj_opt_dse_xstore) - -LJFOLD(NEWREF any any) /* Treated like a store. */ -LJFOLD(CALLS any any) -LJFOLD(CALLL any any) /* Safeguard fallback. */ -LJFOLD(CALLXS any any) -LJFOLD(XBAR) -LJFOLD(RETF any any) /* Modifies BASE. */ -LJFOLD(TNEW any any) -LJFOLD(TDUP any) -LJFOLD(CNEW any any) -LJFOLD(XSNEW any any) -LJFOLDX(lj_ir_emit) - -/* ------------------------------------------------------------------------ */ - -/* Every entry in the generated hash table is a 32 bit pattern: -** -** xxxxxxxx iiiiiii lllllll rrrrrrrrrr -** -** xxxxxxxx = 8 bit index into fold function table -** iiiiiii = 7 bit folded instruction opcode -** lllllll = 7 bit left instruction opcode -** rrrrrrrrrr = 8 bit right instruction opcode or 10 bits from literal field -*/ - -#include "lj_folddef.h" - -/* ------------------------------------------------------------------------ */ - -/* Fold IR instruction. */ -TRef LJ_FASTCALL lj_opt_fold(jit_State *J) -{ - uint32_t key, any; - IRRef ref; - - if (LJ_UNLIKELY((J->flags & JIT_F_OPT_MASK) != JIT_F_OPT_DEFAULT)) { - lua_assert(((JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE|JIT_F_OPT_DSE) | - JIT_F_OPT_DEFAULT) == JIT_F_OPT_DEFAULT); - /* Folding disabled? Chain to CSE, but not for loads/stores/allocs. */ - if (!(J->flags & JIT_F_OPT_FOLD) && irm_kind(lj_ir_mode[fins->o]) == IRM_N) - return lj_opt_cse(J); - - /* No FOLD, forwarding or CSE? Emit raw IR for loads, except for SLOAD. */ - if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE)) != - (JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE) && - irm_kind(lj_ir_mode[fins->o]) == IRM_L && fins->o != IR_SLOAD) - return lj_ir_emit(J); - - /* No FOLD or DSE? Emit raw IR for stores. */ - if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_DSE)) != - (JIT_F_OPT_FOLD|JIT_F_OPT_DSE) && - irm_kind(lj_ir_mode[fins->o]) == IRM_S) - return lj_ir_emit(J); - } - - /* Fold engine start/retry point. */ -retry: - /* Construct key from opcode and operand opcodes (unless literal/none). */ - key = ((uint32_t)fins->o << 17); - if (fins->op1 >= J->cur.nk) { - key += (uint32_t)IR(fins->op1)->o << 10; - *fleft = *IR(fins->op1); - } - if (fins->op2 >= J->cur.nk) { - key += (uint32_t)IR(fins->op2)->o; - *fright = *IR(fins->op2); - } else { - key += (fins->op2 & 0x3ffu); /* Literal mask. Must include IRCONV_*MASK. */ - } - - /* Check for a match in order from most specific to least specific. */ - any = 0; - for (;;) { - uint32_t k = key | (any & 0x1ffff); - uint32_t h = fold_hashkey(k); - uint32_t fh = fold_hash[h]; /* Lookup key in semi-perfect hash table. */ - if ((fh & 0xffffff) == k || (fh = fold_hash[h+1], (fh & 0xffffff) == k)) { - ref = (IRRef)tref_ref(fold_func[fh >> 24](J)); - if (ref != NEXTFOLD) - break; - } - if (any == 0xfffff) /* Exhausted folding. Pass on to CSE. */ - return lj_opt_cse(J); - any = (any | (any >> 10)) ^ 0xffc00; - } - - /* Return value processing, ordered by frequency. */ - if (LJ_LIKELY(ref >= MAX_FOLD)) - return TREF(ref, irt_t(IR(ref)->t)); - if (ref == RETRYFOLD) - goto retry; - if (ref == KINTFOLD) - return lj_ir_kint(J, fins->i); - if (ref == FAILFOLD) - lj_trace_err(J, LJ_TRERR_GFAIL); - lua_assert(ref == DROPFOLD); - return REF_DROP; -} - -/* -- Common-Subexpression Elimination ------------------------------------ */ - -/* CSE an IR instruction. This is very fast due to the skip-list chains. */ -TRef LJ_FASTCALL lj_opt_cse(jit_State *J) -{ - /* Avoid narrow to wide store-to-load forwarding stall */ - IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16); - IROp op = fins->o; - if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { - /* Limited search for same operands in per-opcode chain. */ - IRRef ref = J->chain[op]; - IRRef lim = fins->op1; - if (fins->op2 > lim) lim = fins->op2; /* Relies on lit < REF_BIAS. */ - while (ref > lim) { - if (IR(ref)->op12 == op12) - return TREF(ref, irt_t(IR(ref)->t)); /* Common subexpression found. */ - ref = IR(ref)->prev; - } - } - /* Otherwise emit IR (inlined for speed). */ - { - IRRef ref = lj_ir_nextins(J); - IRIns *ir = IR(ref); - ir->prev = J->chain[op]; - ir->op12 = op12; - J->chain[op] = (IRRef1)ref; - ir->o = fins->o; - J->guardemit.irt |= fins->t.irt; - return TREF(ref, irt_t((ir->t = fins->t))); - } -} - -/* CSE with explicit search limit. */ -TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim) -{ - IRRef ref = J->chain[fins->o]; - IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16); - while (ref > lim) { - if (IR(ref)->op12 == op12) - return ref; - ref = IR(ref)->prev; - } - return lj_ir_emit(J); -} - -/* ------------------------------------------------------------------------ */ - -#undef IR -#undef fins -#undef fleft -#undef fright -#undef knumleft -#undef knumright -#undef emitir - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_opt_loop.c b/third-party/LuaJIT-2.0.2/src/lj_opt_loop.c deleted file mode 100644 index 3a119f4703..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_opt_loop.c +++ /dev/null @@ -1,437 +0,0 @@ -/* -** LOOP: Loop Optimizations. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_loop_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_err.h" -#include "lj_str.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_vm.h" - -/* Loop optimization: -** -** Traditional Loop-Invariant Code Motion (LICM) splits the instructions -** of a loop into invariant and variant instructions. The invariant -** instructions are hoisted out of the loop and only the variant -** instructions remain inside the loop body. -** -** Unfortunately LICM is mostly useless for compiling dynamic languages. -** The IR has many guards and most of the subsequent instructions are -** control-dependent on them. The first non-hoistable guard would -** effectively prevent hoisting of all subsequent instructions. -** -** That's why we use a special form of unrolling using copy-substitution, -** combined with redundancy elimination: -** -** The recorded instruction stream is re-emitted to the compiler pipeline -** with substituted operands. The substitution table is filled with the -** refs returned by re-emitting each instruction. This can be done -** on-the-fly, because the IR is in strict SSA form, where every ref is -** defined before its use. -** -** This aproach generates two code sections, separated by the LOOP -** instruction: -** -** 1. The recorded instructions form a kind of pre-roll for the loop. It -** contains a mix of invariant and variant instructions and performs -** exactly one loop iteration (but not necessarily the 1st iteration). -** -** 2. The loop body contains only the variant instructions and performs -** all remaining loop iterations. -** -** On first sight that looks like a waste of space, because the variant -** instructions are present twice. But the key insight is that the -** pre-roll honors the control-dependencies for *both* the pre-roll itself -** *and* the loop body! -** -** It also means one doesn't have to explicitly model control-dependencies -** (which, BTW, wouldn't help LICM much). And it's much easier to -** integrate sparse snapshotting with this approach. -** -** One of the nicest aspects of this approach is that all of the -** optimizations of the compiler pipeline (FOLD, CSE, FWD, etc.) can be -** reused with only minor restrictions (e.g. one should not fold -** instructions across loop-carried dependencies). -** -** But in general all optimizations can be applied which only need to look -** backwards into the generated instruction stream. At any point in time -** during the copy-substitution process this contains both a static loop -** iteration (the pre-roll) and a dynamic one (from the to-be-copied -** instruction up to the end of the partial loop body). -** -** Since control-dependencies are implicitly kept, CSE also applies to all -** kinds of guards. The major advantage is that all invariant guards can -** be hoisted, too. -** -** Load/store forwarding works across loop iterations, too. This is -** important if loop-carried dependencies are kept in upvalues or tables. -** E.g. 'self.idx = self.idx + 1' deep down in some OO-style method may -** become a forwarded loop-recurrence after inlining. -** -** Since the IR is in SSA form, loop-carried dependencies have to be -** modeled with PHI instructions. The potential candidates for PHIs are -** collected on-the-fly during copy-substitution. After eliminating the -** redundant ones, PHI instructions are emitted *below* the loop body. -** -** Note that this departure from traditional SSA form doesn't change the -** semantics of the PHI instructions themselves. But it greatly simplifies -** on-the-fly generation of the IR and the machine code. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Emit raw IR without passing through optimizations. */ -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- PHI elimination ----------------------------------------------------- */ - -/* Emit or eliminate collected PHIs. */ -static void loop_emit_phi(jit_State *J, IRRef1 *subst, IRRef1 *phi, IRRef nphi, - SnapNo onsnap) -{ - int passx = 0; - IRRef i, nslots; - IRRef invar = J->chain[IR_LOOP]; - /* Pass #1: mark redundant and potentially redundant PHIs. */ - for (i = 0; i < nphi; i++) { - IRRef lref = phi[i]; - IRRef rref = subst[lref]; - if (lref == rref || rref == REF_DROP) { /* Invariants are redundant. */ - irt_setmark(IR(lref)->t); - } else if (!(IR(rref)->op1 == lref || IR(rref)->op2 == lref)) { - /* Quick check for simple recurrences failed, need pass2. */ - irt_setmark(IR(lref)->t); - passx = 1; - } - } - /* Pass #2: traverse variant part and clear marks of non-redundant PHIs. */ - if (passx) { - SnapNo s; - for (i = J->cur.nins-1; i > invar; i--) { - IRIns *ir = IR(i); - if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t); - if (!irref_isk(ir->op1)) { - irt_clearmark(IR(ir->op1)->t); - if (ir->op1 < invar && - ir->o >= IR_CALLN && ir->o <= IR_CARG) { /* ORDER IR */ - ir = IR(ir->op1); - while (ir->o == IR_CARG) { - if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t); - if (irref_isk(ir->op1)) break; - ir = IR(ir->op1); - irt_clearmark(ir->t); - } - } - } - } - for (s = J->cur.nsnap-1; s >= onsnap; s--) { - SnapShot *snap = &J->cur.snap[s]; - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - IRRef ref = snap_ref(map[n]); - if (!irref_isk(ref)) irt_clearmark(IR(ref)->t); - } - } - } - /* Pass #3: add PHIs for variant slots without a corresponding SLOAD. */ - nslots = J->baseslot+J->maxslot; - for (i = 1; i < nslots; i++) { - IRRef ref = tref_ref(J->slot[i]); - while (!irref_isk(ref) && ref != subst[ref]) { - IRIns *ir = IR(ref); - irt_clearmark(ir->t); /* Unmark potential uses, too. */ - if (irt_isphi(ir->t) || irt_ispri(ir->t)) - break; - irt_setphi(ir->t); - if (nphi >= LJ_MAX_PHI) - lj_trace_err(J, LJ_TRERR_PHIOV); - phi[nphi++] = (IRRef1)ref; - ref = subst[ref]; - if (ref > invar) - break; - } - } - /* Pass #4: propagate non-redundant PHIs. */ - while (passx) { - passx = 0; - for (i = 0; i < nphi; i++) { - IRRef lref = phi[i]; - IRIns *ir = IR(lref); - if (!irt_ismarked(ir->t)) { /* Propagate only from unmarked PHIs. */ - IRRef rref = subst[lref]; - if (lref == rref) { /* Mark redundant PHI. */ - irt_setmark(ir->t); - } else { - IRIns *irr = IR(rref); - if (irt_ismarked(irr->t)) { /* Right ref points to other PHI? */ - irt_clearmark(irr->t); /* Mark that PHI as non-redundant. */ - passx = 1; /* Retry. */ - } - } - } - } - } - /* Pass #5: emit PHI instructions or eliminate PHIs. */ - for (i = 0; i < nphi; i++) { - IRRef lref = phi[i]; - IRIns *ir = IR(lref); - if (!irt_ismarked(ir->t)) { /* Emit PHI if not marked. */ - IRRef rref = subst[lref]; - if (rref > invar) - irt_setphi(IR(rref)->t); - emitir_raw(IRT(IR_PHI, irt_type(ir->t)), lref, rref); - } else { /* Otherwise eliminate PHI. */ - irt_clearmark(ir->t); - irt_clearphi(ir->t); - } - } -} - -/* -- Loop unrolling using copy-substitution ------------------------------ */ - -/* Copy-substitute snapshot. */ -static void loop_subst_snap(jit_State *J, SnapShot *osnap, - SnapEntry *loopmap, IRRef1 *subst) -{ - SnapEntry *nmap, *omap = &J->cur.snapmap[osnap->mapofs]; - SnapEntry *nextmap = &J->cur.snapmap[snap_nextofs(&J->cur, osnap)]; - MSize nmapofs; - MSize on, ln, nn, onent = osnap->nent; - BCReg nslots = osnap->nslots; - SnapShot *snap = &J->cur.snap[J->cur.nsnap]; - if (irt_isguard(J->guardemit)) { /* Guard inbetween? */ - nmapofs = J->cur.nsnapmap; - J->cur.nsnap++; /* Add new snapshot. */ - } else { /* Otherwise overwrite previous snapshot. */ - snap--; - nmapofs = snap->mapofs; - } - J->guardemit.irt = 0; - /* Setup new snapshot. */ - snap->mapofs = (uint16_t)nmapofs; - snap->ref = (IRRef1)J->cur.nins; - snap->nslots = nslots; - snap->topslot = osnap->topslot; - snap->count = 0; - nmap = &J->cur.snapmap[nmapofs]; - /* Substitute snapshot slots. */ - on = ln = nn = 0; - while (on < onent) { - SnapEntry osn = omap[on], lsn = loopmap[ln]; - if (snap_slot(lsn) < snap_slot(osn)) { /* Copy slot from loop map. */ - nmap[nn++] = lsn; - ln++; - } else { /* Copy substituted slot from snapshot map. */ - if (snap_slot(lsn) == snap_slot(osn)) ln++; /* Shadowed loop slot. */ - if (!irref_isk(snap_ref(osn))) - osn = snap_setref(osn, subst[snap_ref(osn)]); - nmap[nn++] = osn; - on++; - } - } - while (snap_slot(loopmap[ln]) < nslots) /* Copy remaining loop slots. */ - nmap[nn++] = loopmap[ln++]; - snap->nent = (uint8_t)nn; - omap += onent; - nmap += nn; - while (omap < nextmap) /* Copy PC + frame links. */ - *nmap++ = *omap++; - J->cur.nsnapmap = (uint16_t)(nmap - J->cur.snapmap); -} - -/* Unroll loop. */ -static void loop_unroll(jit_State *J) -{ - IRRef1 phi[LJ_MAX_PHI]; - uint32_t nphi = 0; - IRRef1 *subst; - SnapNo onsnap; - SnapShot *osnap, *loopsnap; - SnapEntry *loopmap, *psentinel; - IRRef ins, invar; - - /* Use temp buffer for substitution table. - ** Only non-constant refs in [REF_BIAS,invar) are valid indexes. - ** Caveat: don't call into the VM or run the GC or the buffer may be gone. - */ - invar = J->cur.nins; - subst = (IRRef1 *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, - (invar-REF_BIAS)*sizeof(IRRef1)) - REF_BIAS; - subst[REF_BASE] = REF_BASE; - - /* LOOP separates the pre-roll from the loop body. */ - emitir_raw(IRTG(IR_LOOP, IRT_NIL), 0, 0); - - /* Grow snapshot buffer and map for copy-substituted snapshots. - ** Need up to twice the number of snapshots minus #0 and loop snapshot. - ** Need up to twice the number of entries plus fallback substitutions - ** from the loop snapshot entries for each new snapshot. - ** Caveat: both calls may reallocate J->cur.snap and J->cur.snapmap! - */ - onsnap = J->cur.nsnap; - lj_snap_grow_buf(J, 2*onsnap-2); - lj_snap_grow_map(J, J->cur.nsnapmap*2+(onsnap-2)*J->cur.snap[onsnap-1].nent); - - /* The loop snapshot is used for fallback substitutions. */ - loopsnap = &J->cur.snap[onsnap-1]; - loopmap = &J->cur.snapmap[loopsnap->mapofs]; - /* The PC of snapshot #0 and the loop snapshot must match. */ - psentinel = &loopmap[loopsnap->nent]; - lua_assert(*psentinel == J->cur.snapmap[J->cur.snap[0].nent]); - *psentinel = SNAP(255, 0, 0); /* Replace PC with temporary sentinel. */ - - /* Start substitution with snapshot #1 (#0 is empty for root traces). */ - osnap = &J->cur.snap[1]; - - /* Copy and substitute all recorded instructions and snapshots. */ - for (ins = REF_FIRST; ins < invar; ins++) { - IRIns *ir; - IRRef op1, op2; - - if (ins >= osnap->ref) /* Instruction belongs to next snapshot? */ - loop_subst_snap(J, osnap++, loopmap, subst); /* Copy-substitute it. */ - - /* Substitute instruction operands. */ - ir = IR(ins); - op1 = ir->op1; - if (!irref_isk(op1)) op1 = subst[op1]; - op2 = ir->op2; - if (!irref_isk(op2)) op2 = subst[op2]; - if (irm_kind(lj_ir_mode[ir->o]) == IRM_N && - op1 == ir->op1 && op2 == ir->op2) { /* Regular invariant ins? */ - subst[ins] = (IRRef1)ins; /* Shortcut. */ - } else { - /* Re-emit substituted instruction to the FOLD/CSE/etc. pipeline. */ - IRType1 t = ir->t; /* Get this first, since emitir may invalidate ir. */ - IRRef ref = tref_ref(emitir(ir->ot & ~IRT_ISPHI, op1, op2)); - subst[ins] = (IRRef1)ref; - if (ref != ins) { - IRIns *irr = IR(ref); - if (ref < invar) { /* Loop-carried dependency? */ - /* Potential PHI? */ - if (!irref_isk(ref) && !irt_isphi(irr->t) && !irt_ispri(irr->t)) { - irt_setphi(irr->t); - if (nphi >= LJ_MAX_PHI) - lj_trace_err(J, LJ_TRERR_PHIOV); - phi[nphi++] = (IRRef1)ref; - } - /* Check all loop-carried dependencies for type instability. */ - if (!irt_sametype(t, irr->t)) { - if (irt_isinteger(t) && irt_isinteger(irr->t)) - continue; - else if (irt_isnum(t) && irt_isinteger(irr->t)) /* Fix int->num. */ - ref = tref_ref(emitir(IRTN(IR_CONV), ref, IRCONV_NUM_INT)); - else if (irt_isnum(irr->t) && irt_isinteger(t)) /* Fix num->int. */ - ref = tref_ref(emitir(IRTGI(IR_CONV), ref, - IRCONV_INT_NUM|IRCONV_CHECK)); - else - lj_trace_err(J, LJ_TRERR_TYPEINS); - subst[ins] = (IRRef1)ref; - irr = IR(ref); - goto phiconv; - } - } else if (ref != REF_DROP && irr->o == IR_CONV && - ref > invar && irr->op1 < invar) { - /* May need an extra PHI for a CONV. */ - ref = irr->op1; - irr = IR(ref); - phiconv: - if (ref < invar && !irref_isk(ref) && !irt_isphi(irr->t)) { - irt_setphi(irr->t); - if (nphi >= LJ_MAX_PHI) - lj_trace_err(J, LJ_TRERR_PHIOV); - phi[nphi++] = (IRRef1)ref; - } - } - } - } - } - if (!irt_isguard(J->guardemit)) /* Drop redundant snapshot. */ - J->cur.nsnapmap = (uint16_t)J->cur.snap[--J->cur.nsnap].mapofs; - lua_assert(J->cur.nsnapmap <= J->sizesnapmap); - *psentinel = J->cur.snapmap[J->cur.snap[0].nent]; /* Restore PC. */ - - loop_emit_phi(J, subst, phi, nphi, onsnap); -} - -/* Undo any partial changes made by the loop optimization. */ -static void loop_undo(jit_State *J, IRRef ins, SnapNo nsnap, MSize nsnapmap) -{ - ptrdiff_t i; - SnapShot *snap = &J->cur.snap[nsnap-1]; - SnapEntry *map = J->cur.snapmap; - map[snap->mapofs + snap->nent] = map[J->cur.snap[0].nent]; /* Restore PC. */ - J->cur.nsnapmap = (uint16_t)nsnapmap; - J->cur.nsnap = nsnap; - J->guardemit.irt = 0; - lj_ir_rollback(J, ins); - for (i = 0; i < BPROP_SLOTS; i++) { /* Remove backprop. cache entries. */ - BPropEntry *bp = &J->bpropcache[i]; - if (bp->val >= ins) - bp->key = 0; - } - for (ins--; ins >= REF_FIRST; ins--) { /* Remove flags. */ - IRIns *ir = IR(ins); - irt_clearphi(ir->t); - irt_clearmark(ir->t); - } -} - -/* Protected callback for loop optimization. */ -static TValue *cploop_opt(lua_State *L, lua_CFunction dummy, void *ud) -{ - UNUSED(L); UNUSED(dummy); - loop_unroll((jit_State *)ud); - return NULL; -} - -/* Loop optimization. */ -int lj_opt_loop(jit_State *J) -{ - IRRef nins = J->cur.nins; - SnapNo nsnap = J->cur.nsnap; - MSize nsnapmap = J->cur.nsnapmap; - int errcode = lj_vm_cpcall(J->L, NULL, J, cploop_opt); - if (LJ_UNLIKELY(errcode)) { - lua_State *L = J->L; - if (errcode == LUA_ERRRUN && tvisnumber(L->top-1)) { /* Trace error? */ - int32_t e = numberVint(L->top-1); - switch ((TraceError)e) { - case LJ_TRERR_TYPEINS: /* Type instability. */ - case LJ_TRERR_GFAIL: /* Guard would always fail. */ - /* Unrolling via recording fixes many cases, e.g. a flipped boolean. */ - if (--J->instunroll < 0) /* But do not unroll forever. */ - break; - L->top--; /* Remove error object. */ - loop_undo(J, nins, nsnap, nsnapmap); - return 1; /* Loop optimization failed, continue recording. */ - default: - break; - } - } - lj_err_throw(L, errcode); /* Propagate all other errors. */ - } - return 0; /* Loop optimization is ok. */ -} - -#undef IR -#undef emitir -#undef emitir_raw - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_opt_mem.c b/third-party/LuaJIT-2.0.2/src/lj_opt_mem.c deleted file mode 100644 index 98974ce33c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_opt_mem.c +++ /dev/null @@ -1,907 +0,0 @@ -/* -** Memory access optimizations. -** AA: Alias Analysis using high-level semantic disambiguation. -** FWD: Load Forwarding (L2L) + Store Forwarding (S2L). -** DSE: Dead-Store Elimination. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_mem_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_tab.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) -#define fleft (&J->fold.left) -#define fright (&J->fold.right) - -/* -** Caveat #1: return value is not always a TRef -- only use with tref_ref(). -** Caveat #2: FWD relies on active CSE for xREF operands -- see lj_opt_fold(). -*/ - -/* Return values from alias analysis. */ -typedef enum { - ALIAS_NO, /* The two refs CANNOT alias (exact). */ - ALIAS_MAY, /* The two refs MAY alias (inexact). */ - ALIAS_MUST /* The two refs MUST alias (exact). */ -} AliasRet; - -/* -- ALOAD/HLOAD forwarding and ASTORE/HSTORE elimination ---------------- */ - -/* Simplified escape analysis: check for intervening stores. */ -static AliasRet aa_escape(jit_State *J, IRIns *ir, IRIns *stop) -{ - IRRef ref = (IRRef)(ir - J->cur.ir); /* The ref that might be stored. */ - for (ir++; ir < stop; ir++) - if (ir->op2 == ref && - (ir->o == IR_ASTORE || ir->o == IR_HSTORE || - ir->o == IR_USTORE || ir->o == IR_FSTORE)) - return ALIAS_MAY; /* Reference was stored and might alias. */ - return ALIAS_NO; /* Reference was not stored. */ -} - -/* Alias analysis for two different table references. */ -static AliasRet aa_table(jit_State *J, IRRef ta, IRRef tb) -{ - IRIns *taba = IR(ta), *tabb = IR(tb); - int newa, newb; - lua_assert(ta != tb); - lua_assert(irt_istab(taba->t) && irt_istab(tabb->t)); - /* Disambiguate new allocations. */ - newa = (taba->o == IR_TNEW || taba->o == IR_TDUP); - newb = (tabb->o == IR_TNEW || tabb->o == IR_TDUP); - if (newa && newb) - return ALIAS_NO; /* Two different allocations never alias. */ - if (newb) { /* At least one allocation? */ - IRIns *tmp = taba; taba = tabb; tabb = tmp; - } else if (!newa) { - return ALIAS_MAY; /* Anything else: we just don't know. */ - } - return aa_escape(J, taba, tabb); -} - -/* Alias analysis for array and hash access using key-based disambiguation. */ -static AliasRet aa_ahref(jit_State *J, IRIns *refa, IRIns *refb) -{ - IRRef ka = refa->op2; - IRRef kb = refb->op2; - IRIns *keya, *keyb; - IRRef ta, tb; - if (refa == refb) - return ALIAS_MUST; /* Shortcut for same refs. */ - keya = IR(ka); - if (keya->o == IR_KSLOT) { ka = keya->op1; keya = IR(ka); } - keyb = IR(kb); - if (keyb->o == IR_KSLOT) { kb = keyb->op1; keyb = IR(kb); } - ta = (refa->o==IR_HREFK || refa->o==IR_AREF) ? IR(refa->op1)->op1 : refa->op1; - tb = (refb->o==IR_HREFK || refb->o==IR_AREF) ? IR(refb->op1)->op1 : refb->op1; - if (ka == kb) { - /* Same key. Check for same table with different ref (NEWREF vs. HREF). */ - if (ta == tb) - return ALIAS_MUST; /* Same key, same table. */ - else - return aa_table(J, ta, tb); /* Same key, possibly different table. */ - } - if (irref_isk(ka) && irref_isk(kb)) - return ALIAS_NO; /* Different constant keys. */ - if (refa->o == IR_AREF) { - /* Disambiguate array references based on index arithmetic. */ - int32_t ofsa = 0, ofsb = 0; - IRRef basea = ka, baseb = kb; - lua_assert(refb->o == IR_AREF); - /* Gather base and offset from t[base] or t[base+-ofs]. */ - if (keya->o == IR_ADD && irref_isk(keya->op2)) { - basea = keya->op1; - ofsa = IR(keya->op2)->i; - if (basea == kb && ofsa != 0) - return ALIAS_NO; /* t[base+-ofs] vs. t[base]. */ - } - if (keyb->o == IR_ADD && irref_isk(keyb->op2)) { - baseb = keyb->op1; - ofsb = IR(keyb->op2)->i; - if (ka == baseb && ofsb != 0) - return ALIAS_NO; /* t[base] vs. t[base+-ofs]. */ - } - if (basea == baseb && ofsa != ofsb) - return ALIAS_NO; /* t[base+-o1] vs. t[base+-o2] and o1 != o2. */ - } else { - /* Disambiguate hash references based on the type of their keys. */ - lua_assert((refa->o==IR_HREF || refa->o==IR_HREFK || refa->o==IR_NEWREF) && - (refb->o==IR_HREF || refb->o==IR_HREFK || refb->o==IR_NEWREF)); - if (!irt_sametype(keya->t, keyb->t)) - return ALIAS_NO; /* Different key types. */ - } - if (ta == tb) - return ALIAS_MAY; /* Same table, cannot disambiguate keys. */ - else - return aa_table(J, ta, tb); /* Try to disambiguate tables. */ -} - -/* Array and hash load forwarding. */ -static TRef fwd_ahload(jit_State *J, IRRef xref) -{ - IRIns *xr = IR(xref); - IRRef lim = xref; /* Search limit. */ - IRRef ref; - - /* Search for conflicting stores. */ - ref = J->chain[fins->o+IRDELTA_L2S]; - while (ref > xref) { - IRIns *store = IR(ref); - switch (aa_ahref(J, xr, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - - /* No conflicting store (yet): const-fold loads from allocations. */ - { - IRIns *ir = (xr->o == IR_HREFK || xr->o == IR_AREF) ? IR(xr->op1) : xr; - IRRef tab = ir->op1; - ir = IR(tab); - if (ir->o == IR_TNEW || (ir->o == IR_TDUP && irref_isk(xr->op2))) { - /* A NEWREF with a number key may end up pointing to the array part. - ** But it's referenced from HSTORE and not found in the ASTORE chain. - ** For now simply consider this a conflict without forwarding anything. - */ - if (xr->o == IR_AREF) { - IRRef ref2 = J->chain[IR_NEWREF]; - while (ref2 > tab) { - IRIns *newref = IR(ref2); - if (irt_isnum(IR(newref->op2)->t)) - goto cselim; - ref2 = newref->prev; - } - } - /* NEWREF inhibits CSE for HREF, and dependent FLOADs from HREFK/AREF. - ** But the above search for conflicting stores was limited by xref. - ** So continue searching, limited by the TNEW/TDUP. Store forwarding - ** is ok, too. A conflict does NOT limit the search for a matching load. - */ - while (ref > tab) { - IRIns *store = IR(ref); - switch (aa_ahref(J, xr, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: goto cselim; /* Conflicting store. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - lua_assert(ir->o != IR_TNEW || irt_isnil(fins->t)); - if (irt_ispri(fins->t)) { - return TREF_PRI(irt_type(fins->t)); - } else if (irt_isnum(fins->t) || (LJ_DUALNUM && irt_isint(fins->t)) || - irt_isstr(fins->t)) { - TValue keyv; - cTValue *tv; - IRIns *key = IR(xr->op2); - if (key->o == IR_KSLOT) key = IR(key->op1); - lj_ir_kvalue(J->L, &keyv, key); - tv = lj_tab_get(J->L, ir_ktab(IR(ir->op1)), &keyv); - lua_assert(itype2irt(tv) == irt_type(fins->t)); - if (irt_isnum(fins->t)) - return lj_ir_knum_u64(J, tv->u64); - else if (LJ_DUALNUM && irt_isint(fins->t)) - return lj_ir_kint(J, intV(tv)); - else - return lj_ir_kstr(J, strV(tv)); - } - /* Othwerwise: don't intern as a constant. */ - } - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - ref = J->chain[fins->o]; - while (ref > lim) { - IRIns *load = IR(ref); - if (load->op1 == xref) - return ref; /* Load forwarding. */ - ref = load->prev; - } - return 0; /* Conflict or no match. */ -} - -/* Reassociate ALOAD across PHIs to handle t[i-1] forwarding case. */ -static TRef fwd_aload_reassoc(jit_State *J) -{ - IRIns *irx = IR(fins->op1); - IRIns *key = IR(irx->op2); - if (key->o == IR_ADD && irref_isk(key->op2)) { - IRIns *add2 = IR(key->op1); - if (add2->o == IR_ADD && irref_isk(add2->op2) && - IR(key->op2)->i == -IR(add2->op2)->i) { - IRRef ref = J->chain[IR_AREF]; - IRRef lim = add2->op1; - if (irx->op1 > lim) lim = irx->op1; - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op1 == irx->op1 && ir->op2 == add2->op1) - return fwd_ahload(J, ref); - ref = ir->prev; - } - } - } - return 0; -} - -/* ALOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J) -{ - IRRef ref; - if ((ref = fwd_ahload(J, fins->op1)) || - (ref = fwd_aload_reassoc(J))) - return ref; - return EMITFOLD; -} - -/* HLOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J) -{ - IRRef ref = fwd_ahload(J, fins->op1); - if (ref) - return ref; - return EMITFOLD; -} - -/* HREFK forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J) -{ - IRRef tab = fleft->op1; - IRRef ref = J->chain[IR_NEWREF]; - while (ref > tab) { - IRIns *newref = IR(ref); - if (tab == newref->op1) { - if (fright->op1 == newref->op2) - return ref; /* Forward from NEWREF. */ - else - goto docse; - } else if (aa_table(J, tab, newref->op1) != ALIAS_NO) { - goto docse; - } - ref = newref->prev; - } - /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */ - if (IR(tab)->o == IR_TDUP) - fins->t.irt &= ~IRT_GUARD; /* Drop HREFK guard. */ -docse: - return CSEFOLD; -} - -/* Check whether HREF of TNEW/TDUP can be folded to niltv. */ -int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J) -{ - IRRef lim = fins->op1; /* Search limit. */ - IRRef ref; - - /* The key for an ASTORE may end up in the hash part after a NEWREF. */ - if (irt_isnum(fright->t) && J->chain[IR_NEWREF] > lim) { - ref = J->chain[IR_ASTORE]; - while (ref > lim) { - if (ref < J->chain[IR_NEWREF]) - return 0; /* Conflict. */ - ref = IR(ref)->prev; - } - } - - /* Search for conflicting stores. */ - ref = J->chain[IR_HSTORE]; - while (ref > lim) { - IRIns *store = IR(ref); - if (aa_ahref(J, fins, IR(store->op1)) != ALIAS_NO) - return 0; /* Conflict. */ - ref = store->prev; - } - - return 1; /* No conflict. Can fold to niltv. */ -} - -/* Check whether there's no aliasing NEWREF for the left operand. */ -int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim) -{ - IRRef ta = fins->op1; - IRRef ref = J->chain[IR_NEWREF]; - while (ref > lim) { - IRIns *newref = IR(ref); - if (ta == newref->op1 || aa_table(J, ta, newref->op1) != ALIAS_NO) - return 0; /* Conflict. */ - ref = newref->prev; - } - return 1; /* No conflict. Can safely FOLD/CSE. */ -} - -/* ASTORE/HSTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J) -{ - IRRef xref = fins->op1; /* xREF reference. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRIns *xr = IR(xref); - IRRef1 *refp = &J->chain[fins->o]; - IRRef ref = *refp; - while (ref > xref) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_ahref(J, xr, IR(store->op1))) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: /* Store to MAYBE the same location. */ - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: /* Store to the same location. */ - if (store->op2 == val) /* Same value: drop the new store. */ - return DROPFOLD; - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards (includes conflicting loads). */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t) || ir->o == IR_CALLL) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - store->o = IR_NOP; - store->t.irt = IRT_NIL; - store->op1 = store->op2 = 0; - store->prev = 0; - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* -- ULOAD forwarding ---------------------------------------------------- */ - -/* The current alias analysis for upvalues is very simplistic. It only -** disambiguates between the unique upvalues of the same function. -** This is good enough for now, since most upvalues are read-only. -** -** A more precise analysis would be feasible with the help of the parser: -** generate a unique key for every upvalue, even across all prototypes. -** Lacking a realistic use-case, it's unclear whether this is beneficial. -*/ -static AliasRet aa_uref(IRIns *refa, IRIns *refb) -{ - if (refa->o != refb->o) - return ALIAS_NO; /* Different UREFx type. */ - if (refa->op1 == refb->op1) { /* Same function. */ - if (refa->op2 == refb->op2) - return ALIAS_MUST; /* Same function, same upvalue idx. */ - else - return ALIAS_NO; /* Same function, different upvalue idx. */ - } else { /* Different functions, check disambiguation hash values. */ - if (((refa->op2 ^ refb->op2) & 0xff)) - return ALIAS_NO; /* Upvalues with different hash values cannot alias. */ - else - return ALIAS_MAY; /* No conclusion can be drawn for same hash value. */ - } -} - -/* ULOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J) -{ - IRRef uref = fins->op1; - IRRef lim = uref; /* Search limit. */ - IRIns *xr = IR(uref); - IRRef ref; - - /* Search for conflicting stores. */ - ref = J->chain[IR_USTORE]; - while (ref > uref) { - IRIns *store = IR(ref); - switch (aa_uref(xr, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - return lj_opt_cselim(J, lim); -} - -/* USTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J) -{ - IRRef xref = fins->op1; /* xREF reference. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRIns *xr = IR(xref); - IRRef1 *refp = &J->chain[IR_USTORE]; - IRRef ref = *refp; - while (ref > xref) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_uref(xr, IR(store->op1))) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: /* Store to MAYBE the same location. */ - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: /* Store to the same location. */ - if (store->op2 == val) /* Same value: drop the new store. */ - return DROPFOLD; - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards (includes conflicting loads). */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t)) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - store->o = IR_NOP; - store->t.irt = IRT_NIL; - store->op1 = store->op2 = 0; - store->prev = 0; - if (ref+1 < J->cur.nins && - store[1].o == IR_OBAR && store[1].op1 == xref) { - IRRef1 *bp = &J->chain[IR_OBAR]; - IRIns *obar; - for (obar = IR(*bp); *bp > ref+1; obar = IR(*bp)) - bp = &obar->prev; - /* Remove OBAR, too. */ - *bp = obar->prev; - obar->o = IR_NOP; - obar->t.irt = IRT_NIL; - obar->op1 = obar->op2 = 0; - obar->prev = 0; - } - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* -- FLOAD forwarding and FSTORE elimination ----------------------------- */ - -/* Alias analysis for field access. -** Field loads are cheap and field stores are rare. -** Simple disambiguation based on field types is good enough. -*/ -static AliasRet aa_fref(jit_State *J, IRIns *refa, IRIns *refb) -{ - if (refa->op2 != refb->op2) - return ALIAS_NO; /* Different fields. */ - if (refa->op1 == refb->op1) - return ALIAS_MUST; /* Same field, same object. */ - else if (refa->op2 >= IRFL_TAB_META && refa->op2 <= IRFL_TAB_NOMM) - return aa_table(J, refa->op1, refb->op1); /* Disambiguate tables. */ - else - return ALIAS_MAY; /* Same field, possibly different object. */ -} - -/* Only the loads for mutable fields end up here (see FOLD). */ -TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J) -{ - IRRef oref = fins->op1; /* Object reference. */ - IRRef fid = fins->op2; /* Field ID. */ - IRRef lim = oref; /* Search limit. */ - IRRef ref; - - /* Search for conflicting stores. */ - ref = J->chain[IR_FSTORE]; - while (ref > oref) { - IRIns *store = IR(ref); - switch (aa_fref(J, fins, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - - /* No conflicting store: const-fold field loads from allocations. */ - if (fid == IRFL_TAB_META) { - IRIns *ir = IR(oref); - if (ir->o == IR_TNEW || ir->o == IR_TDUP) - return lj_ir_knull(J, IRT_TAB); - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - return lj_opt_cselim(J, lim); -} - -/* FSTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J) -{ - IRRef fref = fins->op1; /* FREF reference. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRIns *xr = IR(fref); - IRRef1 *refp = &J->chain[IR_FSTORE]; - IRRef ref = *refp; - while (ref > fref) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_fref(J, xr, IR(store->op1))) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: - if (store->op2 == val) /* Same value: drop the new store. */ - return DROPFOLD; - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards or conflicting loads. */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t) || (ir->o == IR_FLOAD && ir->op2 == xr->op2)) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - store->o = IR_NOP; - store->t.irt = IRT_NIL; - store->op1 = store->op2 = 0; - store->prev = 0; - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* -- XLOAD forwarding and XSTORE elimination ----------------------------- */ - -/* Find cdata allocation for a reference (if any). */ -static IRIns *aa_findcnew(jit_State *J, IRIns *ir) -{ - while (ir->o == IR_ADD) { - if (!irref_isk(ir->op1)) { - IRIns *ir1 = aa_findcnew(J, IR(ir->op1)); /* Left-recursion. */ - if (ir1) return ir1; - } - if (irref_isk(ir->op2)) return NULL; - ir = IR(ir->op2); /* Flatten right-recursion. */ - } - return ir->o == IR_CNEW ? ir : NULL; -} - -/* Alias analysis for two cdata allocations. */ -static AliasRet aa_cnew(jit_State *J, IRIns *refa, IRIns *refb) -{ - IRIns *cnewa = aa_findcnew(J, refa); - IRIns *cnewb = aa_findcnew(J, refb); - if (cnewa == cnewb) - return ALIAS_MAY; /* Same allocation or neither is an allocation. */ - if (cnewa && cnewb) - return ALIAS_NO; /* Two different allocations never alias. */ - if (cnewb) { cnewa = cnewb; refb = refa; } - return aa_escape(J, cnewa, refb); -} - -/* Alias analysis for XLOAD/XSTORE. */ -static AliasRet aa_xref(jit_State *J, IRIns *refa, IRIns *xa, IRIns *xb) -{ - ptrdiff_t ofsa = 0, ofsb = 0; - IRIns *refb = IR(xb->op1); - IRIns *basea = refa, *baseb = refb; - if (refa == refb && irt_sametype(xa->t, xb->t)) - return ALIAS_MUST; /* Shortcut for same refs with identical type. */ - /* Offset-based disambiguation. */ - if (refa->o == IR_ADD && irref_isk(refa->op2)) { - IRIns *irk = IR(refa->op2); - basea = IR(refa->op1); - ofsa = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : - (ptrdiff_t)irk->i; - if (basea == refb && ofsa != 0) - return ALIAS_NO; /* base+-ofs vs. base. */ - } - if (refb->o == IR_ADD && irref_isk(refb->op2)) { - IRIns *irk = IR(refb->op2); - baseb = IR(refb->op1); - ofsb = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : - (ptrdiff_t)irk->i; - if (refa == baseb && ofsb != 0) - return ALIAS_NO; /* base vs. base+-ofs. */ - } - /* This implements (very) strict aliasing rules. - ** Different types do NOT alias, except for differences in signedness. - ** Type punning through unions is allowed (but forces a reload). - */ - if (basea == baseb) { - ptrdiff_t sza = irt_size(xa->t), szb = irt_size(xb->t); - if (ofsa == ofsb) { - if (sza == szb && irt_isfp(xa->t) == irt_isfp(xb->t)) - return ALIAS_MUST; /* Same-sized, same-kind. May need to convert. */ - } else if (ofsa + sza <= ofsb || ofsb + szb <= ofsa) { - return ALIAS_NO; /* Non-overlapping base+-o1 vs. base+-o2. */ - } - /* NYI: extract, extend or reinterpret bits (int <-> fp). */ - return ALIAS_MAY; /* Overlapping or type punning: force reload. */ - } - if (!irt_sametype(xa->t, xb->t) && - !(irt_typerange(xa->t, IRT_I8, IRT_U64) && - ((xa->t.irt - IRT_I8) ^ (xb->t.irt - IRT_I8)) == 1)) - return ALIAS_NO; - /* NYI: structural disambiguation. */ - return aa_cnew(J, basea, baseb); /* Try to disambiguate allocations. */ -} - -/* Return CSEd reference or 0. Caveat: swaps lower ref to the right! */ -static IRRef reassoc_trycse(jit_State *J, IROp op, IRRef op1, IRRef op2) -{ - IRRef ref = J->chain[op]; - IRRef lim = op1; - if (op2 > lim) { lim = op2; op2 = op1; op1 = lim; } - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op1 == op1 && ir->op2 == op2) - return ref; - ref = ir->prev; - } - return 0; -} - -/* Reassociate index references. */ -static IRRef reassoc_xref(jit_State *J, IRIns *ir) -{ - ptrdiff_t ofs = 0; - if (ir->o == IR_ADD && irref_isk(ir->op2)) { /* Get constant offset. */ - IRIns *irk = IR(ir->op2); - ofs = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : - (ptrdiff_t)irk->i; - ir = IR(ir->op1); - } - if (ir->o == IR_ADD) { /* Add of base + index. */ - /* Index ref > base ref for loop-carried dependences. Only check op1. */ - IRIns *ir2, *ir1 = IR(ir->op1); - int32_t shift = 0; - IRRef idxref; - /* Determine index shifts. Don't bother with IR_MUL here. */ - if (ir1->o == IR_BSHL && irref_isk(ir1->op2)) - shift = IR(ir1->op2)->i; - else if (ir1->o == IR_ADD && ir1->op1 == ir1->op2) - shift = 1; - else - ir1 = ir; - ir2 = IR(ir1->op1); - /* A non-reassociated add. Must be a loop-carried dependence. */ - if (ir2->o == IR_ADD && irt_isint(ir2->t) && irref_isk(ir2->op2)) - ofs += (ptrdiff_t)IR(ir2->op2)->i << shift; - else - return 0; - idxref = ir2->op1; - /* Try to CSE the reassociated chain. Give up if not found. */ - if (ir1 != ir && - !(idxref = reassoc_trycse(J, ir1->o, idxref, - ir1->o == IR_BSHL ? ir1->op2 : idxref))) - return 0; - if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, ir->op2))) - return 0; - if (ofs != 0) { - IRRef refk = tref_ref(lj_ir_kintp(J, ofs)); - if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, refk))) - return 0; - } - return idxref; /* Success, found a reassociated index reference. Phew. */ - } - return 0; /* Failure. */ -} - -/* XLOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J) -{ - IRRef xref = fins->op1; - IRIns *xr = IR(xref); - IRRef lim = xref; /* Search limit. */ - IRRef ref; - - if ((fins->op2 & IRXLOAD_READONLY)) - goto cselim; - if ((fins->op2 & IRXLOAD_VOLATILE)) - goto doemit; - - /* Search for conflicting stores. */ - ref = J->chain[IR_XSTORE]; -retry: - if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS]; - if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR]; - while (ref > lim) { - IRIns *store = IR(ref); - switch (aa_xref(J, xr, fins, store)) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: - /* Emit conversion if the loaded type doesn't match the forwarded type. */ - if (!irt_sametype(fins->t, IR(store->op2)->t)) { - IRType st = irt_type(fins->t); - if (st == IRT_I8 || st == IRT_I16) { /* Trunc + sign-extend. */ - st |= IRCONV_SEXT; - } else if (st == IRT_U8 || st == IRT_U16) { /* Trunc + zero-extend. */ - } else if (st == IRT_INT) { - st = irt_type(IR(store->op2)->t); /* Needs dummy CONV.int.*. */ - } else { /* I64/U64 are boxed, U32 is hidden behind a CONV.num.u32. */ - goto store_fwd; - } - fins->ot = IRTI(IR_CONV); - fins->op1 = store->op2; - fins->op2 = (IRT_INT<<5)|st; - return RETRYFOLD; - } - store_fwd: - return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - ref = J->chain[IR_XLOAD]; - while (ref > lim) { - /* CSE for XLOAD depends on the type, but not on the IRXLOAD_* flags. */ - if (IR(ref)->op1 == xref && irt_sametype(IR(ref)->t, fins->t)) - return ref; - ref = IR(ref)->prev; - } - - /* Reassociate XLOAD across PHIs to handle a[i-1] forwarding case. */ - if (!(fins->op2 & IRXLOAD_READONLY) && J->chain[IR_LOOP] && - xref == fins->op1 && (xref = reassoc_xref(J, xr)) != 0) { - ref = J->chain[IR_XSTORE]; - while (ref > lim) /* Skip stores that have already been checked. */ - ref = IR(ref)->prev; - lim = xref; - xr = IR(xref); - goto retry; /* Retry with the reassociated reference. */ - } -doemit: - return EMITFOLD; -} - -/* XSTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J) -{ - IRRef xref = fins->op1; - IRIns *xr = IR(xref); - IRRef lim = xref; /* Search limit. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRRef1 *refp = &J->chain[IR_XSTORE]; - IRRef ref = *refp; - if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS]; - if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR]; - while (ref > lim) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_xref(J, xr, fins, store)) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: - if (store->op2 == val) /* Same value: drop the new store. */ - return DROPFOLD; - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards or any XLOADs (no AA performed). */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t) || ir->o == IR_XLOAD) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - store->o = IR_NOP; - store->t.irt = IRT_NIL; - store->op1 = store->op2 = 0; - store->prev = 0; - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* -- Forwarding of lj_tab_len -------------------------------------------- */ - -/* This is rather simplistic right now, but better than nothing. */ -TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J) -{ - IRRef tab = fins->op1; /* Table reference. */ - IRRef lim = tab; /* Search limit. */ - IRRef ref; - - /* Any ASTORE is a conflict and limits the search. */ - if (J->chain[IR_ASTORE] > lim) lim = J->chain[IR_ASTORE]; - - /* Search for conflicting HSTORE with numeric key. */ - ref = J->chain[IR_HSTORE]; - while (ref > lim) { - IRIns *store = IR(ref); - IRIns *href = IR(store->op1); - IRIns *key = IR(href->op2); - if (irt_isnum(key->o == IR_KSLOT ? IR(key->op1)->t : key->t)) { - lim = ref; /* Conflicting store found, limits search for TLEN. */ - break; - } - ref = store->prev; - } - - /* Try to find a matching load. Below the conflicting store, if any. */ - return lj_opt_cselim(J, lim); -} - -/* -- ASTORE/HSTORE previous type analysis -------------------------------- */ - -/* Check whether the previous value for a table store is non-nil. -** This can be derived either from a previous store or from a previous -** load (because all loads from tables perform a type check). -** -** The result of the analysis can be used to avoid the metatable check -** and the guard against HREF returning niltv. Both of these are cheap, -** so let's not spend too much effort on the analysis. -** -** A result of 1 is exact: previous value CANNOT be nil. -** A result of 0 is inexact: previous value MAY be nil. -*/ -int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref) -{ - /* First check stores. */ - IRRef ref = J->chain[loadop+IRDELTA_L2S]; - while (ref > xref) { - IRIns *store = IR(ref); - if (store->op1 == xref) { /* Same xREF. */ - /* A nil store MAY alias, but a non-nil store MUST alias. */ - return !irt_isnil(store->t); - } else if (irt_isnil(store->t)) { /* Must check any nil store. */ - IRRef skref = IR(store->op1)->op2; - IRRef xkref = IR(xref)->op2; - /* Same key type MAY alias. Need ALOAD check due to multiple int types. */ - if (loadop == IR_ALOAD || irt_sametype(IR(skref)->t, IR(xkref)->t)) { - if (skref == xkref || !irref_isk(skref) || !irref_isk(xkref)) - return 0; /* A nil store with same const key or var key MAY alias. */ - /* Different const keys CANNOT alias. */ - } /* Different key types CANNOT alias. */ - } /* Other non-nil stores MAY alias. */ - ref = store->prev; - } - - /* Check loads since nothing could be derived from stores. */ - ref = J->chain[loadop]; - while (ref > xref) { - IRIns *load = IR(ref); - if (load->op1 == xref) { /* Same xREF. */ - /* A nil load MAY alias, but a non-nil load MUST alias. */ - return !irt_isnil(load->t); - } /* Other non-nil loads MAY alias. */ - ref = load->prev; - } - return 0; /* Nothing derived at all, previous value MAY be nil. */ -} - -/* ------------------------------------------------------------------------ */ - -#undef IR -#undef fins -#undef fleft -#undef fright - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_opt_narrow.c b/third-party/LuaJIT-2.0.2/src/lj_opt_narrow.c deleted file mode 100644 index caf2a8df1c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_opt_narrow.c +++ /dev/null @@ -1,648 +0,0 @@ -/* -** NARROW: Narrowing of numbers to integers (double to int32_t). -** STRIPOV: Stripping of overflow checks. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_narrow_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_bc.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_vm.h" -#include "lj_strscan.h" - -/* Rationale for narrowing optimizations: -** -** Lua has only a single number type and this is a FP double by default. -** Narrowing doubles to integers does not pay off for the interpreter on a -** current-generation x86/x64 machine. Most FP operations need the same -** amount of execution resources as their integer counterparts, except -** with slightly longer latencies. Longer latencies are a non-issue for -** the interpreter, since they are usually hidden by other overhead. -** -** The total CPU execution bandwidth is the sum of the bandwidth of the FP -** and the integer units, because they execute in parallel. The FP units -** have an equal or higher bandwidth than the integer units. Not using -** them means losing execution bandwidth. Moving work away from them to -** the already quite busy integer units is a losing proposition. -** -** The situation for JIT-compiled code is a bit different: the higher code -** density makes the extra latencies much more visible. Tight loops expose -** the latencies for updating the induction variables. Array indexing -** requires narrowing conversions with high latencies and additional -** guards (to check that the index is really an integer). And many common -** optimizations only work on integers. -** -** One solution would be speculative, eager narrowing of all number loads. -** This causes many problems, like losing -0 or the need to resolve type -** mismatches between traces. It also effectively forces the integer type -** to have overflow-checking semantics. This impedes many basic -** optimizations and requires adding overflow checks to all integer -** arithmetic operations (whereas FP arithmetics can do without). -** -** Always replacing an FP op with an integer op plus an overflow check is -** counter-productive on a current-generation super-scalar CPU. Although -** the overflow check branches are highly predictable, they will clog the -** execution port for the branch unit and tie up reorder buffers. This is -** turning a pure data-flow dependency into a different data-flow -** dependency (with slightly lower latency) *plus* a control dependency. -** In general, you don't want to do this since latencies due to data-flow -** dependencies can be well hidden by out-of-order execution. -** -** A better solution is to keep all numbers as FP values and only narrow -** when it's beneficial to do so. LuaJIT uses predictive narrowing for -** induction variables and demand-driven narrowing for index expressions, -** integer arguments and bit operations. Additionally it can eliminate or -** hoist most of the resulting overflow checks. Regular arithmetic -** computations are never narrowed to integers. -** -** The integer type in the IR has convenient wrap-around semantics and -** ignores overflow. Extra operations have been added for -** overflow-checking arithmetic (ADDOV/SUBOV) instead of an extra type. -** Apart from reducing overall complexity of the compiler, this also -** nicely solves the problem where you want to apply algebraic -** simplifications to ADD, but not to ADDOV. And the x86/x64 assembler can -** use lea instead of an add for integer ADD, but not for ADDOV (lea does -** not affect the flags, but it helps to avoid register moves). -** -** -** All of the above has to be reconsidered for architectures with slow FP -** operations or without a hardware FPU. The dual-number mode of LuaJIT -** addresses this issue. Arithmetic operations are performed on integers -** as far as possible and overflow checks are added as needed. -** -** This implies that narrowing for integer arguments and bit operations -** should also strip overflow checks, e.g. replace ADDOV with ADD. The -** original overflow guards are weak and can be eliminated by DCE, if -** there's no other use. -** -** A slight twist is that it's usually beneficial to use overflow-checked -** integer arithmetics if all inputs are already integers. This is the only -** change that affects the single-number mode, too. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- Elimination of narrowing type conversions --------------------------- */ - -/* Narrowing of index expressions and bit operations is demand-driven. The -** trace recorder emits a narrowing type conversion (CONV.int.num or TOBIT) -** in all of these cases (e.g. array indexing or string indexing). FOLD -** already takes care of eliminating simple redundant conversions like -** CONV.int.num(CONV.num.int(x)) ==> x. -** -** But the surrounding code is FP-heavy and arithmetic operations are -** performed on FP numbers (for the single-number mode). Consider a common -** example such as 'x=t[i+1]', with 'i' already an integer (due to induction -** variable narrowing). The index expression would be recorded as -** CONV.int.num(ADD(CONV.num.int(i), 1)) -** which is clearly suboptimal. -** -** One can do better by recursively backpropagating the narrowing type -** conversion across FP arithmetic operations. This turns FP ops into -** their corresponding integer counterparts. Depending on the semantics of -** the conversion they also need to check for overflow. Currently only ADD -** and SUB are supported. -** -** The above example can be rewritten as -** ADDOV(CONV.int.num(CONV.num.int(i)), 1) -** and then into ADDOV(i, 1) after folding of the conversions. The original -** FP ops remain in the IR and are eliminated by DCE since all references to -** them are gone. -** -** [In dual-number mode the trace recorder already emits ADDOV etc., but -** this can be further reduced. See below.] -** -** Special care has to be taken to avoid narrowing across an operation -** which is potentially operating on non-integral operands. One obvious -** case is when an expression contains a non-integral constant, but ends -** up as an integer index at runtime (like t[x+1.5] with x=0.5). -** -** Operations with two non-constant operands illustrate a similar problem -** (like t[a+b] with a=1.5 and b=2.5). Backpropagation has to stop there, -** unless it can be proven that either operand is integral (e.g. by CSEing -** a previous conversion). As a not-so-obvious corollary this logic also -** applies for a whole expression tree (e.g. t[(a+1)+(b+1)]). -** -** Correctness of the transformation is guaranteed by avoiding to expand -** the tree by adding more conversions than the one we would need to emit -** if not backpropagating. TOBIT employs a more optimistic rule, because -** the conversion has special semantics, designed to make the life of the -** compiler writer easier. ;-) -** -** Using on-the-fly backpropagation of an expression tree doesn't work -** because it's unknown whether the transform is correct until the end. -** This either requires IR rollback and cache invalidation for every -** subtree or a two-pass algorithm. The former didn't work out too well, -** so the code now combines a recursive collector with a stack-based -** emitter. -** -** [A recursive backpropagation algorithm with backtracking, employing -** skip-list lookup and round-robin caching, emitting stack operations -** on-the-fly for a stack-based interpreter -- and all of that in a meager -** kilobyte? Yep, compilers are a great treasure chest. Throw away your -** textbooks and read the codebase of a compiler today!] -** -** There's another optimization opportunity for array indexing: it's -** always accompanied by an array bounds-check. The outermost overflow -** check may be delegated to the ABC operation. This works because ABC is -** an unsigned comparison and wrap-around due to overflow creates negative -** numbers. -** -** But this optimization is only valid for constants that cannot overflow -** an int32_t into the range of valid array indexes [0..2^27+1). A check -** for +-2^30 is safe since -2^31 - 2^30 wraps to 2^30 and 2^31-1 + 2^30 -** wraps to -2^30-1. -** -** It's also good enough in practice, since e.g. t[i+1] or t[i-10] are -** quite common. So the above example finally ends up as ADD(i, 1)! -** -** Later on, the assembler is able to fuse the whole array reference and -** the ADD into the memory operands of loads and other instructions. This -** is why LuaJIT is able to generate very pretty (and fast) machine code -** for array indexing. And that, my dear, concludes another story about -** one of the hidden secrets of LuaJIT ... -*/ - -/* Maximum backpropagation depth and maximum stack size. */ -#define NARROW_MAX_BACKPROP 100 -#define NARROW_MAX_STACK 256 - -/* The stack machine has a 32 bit instruction format: [IROpT | IRRef1] -** The lower 16 bits hold a reference (or 0). The upper 16 bits hold -** the IR opcode + type or one of the following special opcodes: -*/ -enum { - NARROW_REF, /* Push ref. */ - NARROW_CONV, /* Push conversion of ref. */ - NARROW_SEXT, /* Push sign-extension of ref. */ - NARROW_INT /* Push KINT ref. The next code holds an int32_t. */ -}; - -typedef uint32_t NarrowIns; - -#define NARROWINS(op, ref) (((op) << 16) + (ref)) -#define narrow_op(ins) ((IROpT)((ins) >> 16)) -#define narrow_ref(ins) ((IRRef1)(ins)) - -/* Context used for narrowing of type conversions. */ -typedef struct NarrowConv { - jit_State *J; /* JIT compiler state. */ - NarrowIns *sp; /* Current stack pointer. */ - NarrowIns *maxsp; /* Maximum stack pointer minus redzone. */ - int lim; /* Limit on the number of emitted conversions. */ - IRRef mode; /* Conversion mode (IRCONV_*). */ - IRType t; /* Destination type: IRT_INT or IRT_I64. */ - NarrowIns stack[NARROW_MAX_STACK]; /* Stack holding stack-machine code. */ -} NarrowConv; - -/* Lookup a reference in the backpropagation cache. */ -static BPropEntry *narrow_bpc_get(jit_State *J, IRRef1 key, IRRef mode) -{ - ptrdiff_t i; - for (i = 0; i < BPROP_SLOTS; i++) { - BPropEntry *bp = &J->bpropcache[i]; - /* Stronger checks are ok, too. */ - if (bp->key == key && bp->mode >= mode && - ((bp->mode ^ mode) & IRCONV_MODEMASK) == 0) - return bp; - } - return NULL; -} - -/* Add an entry to the backpropagation cache. */ -static void narrow_bpc_set(jit_State *J, IRRef1 key, IRRef1 val, IRRef mode) -{ - uint32_t slot = J->bpropslot; - BPropEntry *bp = &J->bpropcache[slot]; - J->bpropslot = (slot + 1) & (BPROP_SLOTS-1); - bp->key = key; - bp->val = val; - bp->mode = mode; -} - -/* Backpropagate overflow stripping. */ -static void narrow_stripov_backprop(NarrowConv *nc, IRRef ref, int depth) -{ - jit_State *J = nc->J; - IRIns *ir = IR(ref); - if (ir->o == IR_ADDOV || ir->o == IR_SUBOV || - (ir->o == IR_MULOV && (nc->mode & IRCONV_CONVMASK) == IRCONV_ANY)) { - BPropEntry *bp = narrow_bpc_get(nc->J, ref, IRCONV_TOBIT); - if (bp) { - ref = bp->val; - } else if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) { - narrow_stripov_backprop(nc, ir->op1, depth); - narrow_stripov_backprop(nc, ir->op2, depth); - *nc->sp++ = NARROWINS(IRT(ir->o - IR_ADDOV + IR_ADD, IRT_INT), ref); - return; - } - } - *nc->sp++ = NARROWINS(NARROW_REF, ref); -} - -/* Backpropagate narrowing conversion. Return number of needed conversions. */ -static int narrow_conv_backprop(NarrowConv *nc, IRRef ref, int depth) -{ - jit_State *J = nc->J; - IRIns *ir = IR(ref); - IRRef cref; - - /* Check the easy cases first. */ - if (ir->o == IR_CONV && (ir->op2 & IRCONV_SRCMASK) == IRT_INT) { - if ((nc->mode & IRCONV_CONVMASK) <= IRCONV_ANY) - narrow_stripov_backprop(nc, ir->op1, depth+1); - else - *nc->sp++ = NARROWINS(NARROW_REF, ir->op1); /* Undo conversion. */ - if (nc->t == IRT_I64) - *nc->sp++ = NARROWINS(NARROW_SEXT, 0); /* Sign-extend integer. */ - return 0; - } else if (ir->o == IR_KNUM) { /* Narrow FP constant. */ - lua_Number n = ir_knum(ir)->n; - if ((nc->mode & IRCONV_CONVMASK) == IRCONV_TOBIT) { - /* Allows a wider range of constants. */ - int64_t k64 = (int64_t)n; - if (n == (lua_Number)k64) { /* Only if const doesn't lose precision. */ - *nc->sp++ = NARROWINS(NARROW_INT, 0); - *nc->sp++ = (NarrowIns)k64; /* But always truncate to 32 bits. */ - return 0; - } - } else { - int32_t k = lj_num2int(n); - /* Only if constant is a small integer. */ - if (checki16(k) && n == (lua_Number)k) { - *nc->sp++ = NARROWINS(NARROW_INT, 0); - *nc->sp++ = (NarrowIns)k; - return 0; - } - } - return 10; /* Never narrow other FP constants (this is rare). */ - } - - /* Try to CSE the conversion. Stronger checks are ok, too. */ - cref = J->chain[fins->o]; - while (cref > ref) { - IRIns *cr = IR(cref); - if (cr->op1 == ref && - (fins->o == IR_TOBIT || - ((cr->op2 & IRCONV_MODEMASK) == (nc->mode & IRCONV_MODEMASK) && - irt_isguard(cr->t) >= irt_isguard(fins->t)))) { - *nc->sp++ = NARROWINS(NARROW_REF, cref); - return 0; /* Already there, no additional conversion needed. */ - } - cref = cr->prev; - } - - /* Backpropagate across ADD/SUB. */ - if (ir->o == IR_ADD || ir->o == IR_SUB) { - /* Try cache lookup first. */ - IRRef mode = nc->mode; - BPropEntry *bp; - /* Inner conversions need a stronger check. */ - if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX && depth > 0) - mode += IRCONV_CHECK-IRCONV_INDEX; - bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode); - if (bp) { - *nc->sp++ = NARROWINS(NARROW_REF, bp->val); - return 0; - } else if (nc->t == IRT_I64) { - /* Try sign-extending from an existing (checked) conversion to int. */ - mode = (IRT_INT<<5)|IRT_NUM|IRCONV_INDEX; - bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode); - if (bp) { - *nc->sp++ = NARROWINS(NARROW_REF, bp->val); - *nc->sp++ = NARROWINS(NARROW_SEXT, 0); - return 0; - } - } - if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) { - NarrowIns *savesp = nc->sp; - int count = narrow_conv_backprop(nc, ir->op1, depth); - count += narrow_conv_backprop(nc, ir->op2, depth); - if (count <= nc->lim) { /* Limit total number of conversions. */ - *nc->sp++ = NARROWINS(IRT(ir->o, nc->t), ref); - return count; - } - nc->sp = savesp; /* Too many conversions, need to backtrack. */ - } - } - - /* Otherwise add a conversion. */ - *nc->sp++ = NARROWINS(NARROW_CONV, ref); - return 1; -} - -/* Emit the conversions collected during backpropagation. */ -static IRRef narrow_conv_emit(jit_State *J, NarrowConv *nc) -{ - /* The fins fields must be saved now -- emitir() overwrites them. */ - IROpT guardot = irt_isguard(fins->t) ? IRTG(IR_ADDOV-IR_ADD, 0) : 0; - IROpT convot = fins->ot; - IRRef1 convop2 = fins->op2; - NarrowIns *next = nc->stack; /* List of instructions from backpropagation. */ - NarrowIns *last = nc->sp; - NarrowIns *sp = nc->stack; /* Recycle the stack to store operands. */ - while (next < last) { /* Simple stack machine to process the ins. list. */ - NarrowIns ref = *next++; - IROpT op = narrow_op(ref); - if (op == NARROW_REF) { - *sp++ = ref; - } else if (op == NARROW_CONV) { - *sp++ = emitir_raw(convot, ref, convop2); /* Raw emit avoids a loop. */ - } else if (op == NARROW_SEXT) { - lua_assert(sp >= nc->stack+1); - sp[-1] = emitir(IRT(IR_CONV, IRT_I64), sp[-1], - (IRT_I64<<5)|IRT_INT|IRCONV_SEXT); - } else if (op == NARROW_INT) { - lua_assert(next < last); - *sp++ = nc->t == IRT_I64 ? - lj_ir_kint64(J, (int64_t)(int32_t)*next++) : - lj_ir_kint(J, *next++); - } else { /* Regular IROpT. Pops two operands and pushes one result. */ - IRRef mode = nc->mode; - lua_assert(sp >= nc->stack+2); - sp--; - /* Omit some overflow checks for array indexing. See comments above. */ - if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX) { - if (next == last && irref_isk(narrow_ref(sp[0])) && - (uint32_t)IR(narrow_ref(sp[0]))->i + 0x40000000u < 0x80000000u) - guardot = 0; - else /* Otherwise cache a stronger check. */ - mode += IRCONV_CHECK-IRCONV_INDEX; - } - sp[-1] = emitir(op+guardot, sp[-1], sp[0]); - /* Add to cache. */ - if (narrow_ref(ref)) - narrow_bpc_set(J, narrow_ref(ref), narrow_ref(sp[-1]), mode); - } - } - lua_assert(sp == nc->stack+1); - return nc->stack[0]; -} - -/* Narrow a type conversion of an arithmetic operation. */ -TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J) -{ - if ((J->flags & JIT_F_OPT_NARROW)) { - NarrowConv nc; - nc.J = J; - nc.sp = nc.stack; - nc.maxsp = &nc.stack[NARROW_MAX_STACK-4]; - nc.t = irt_type(fins->t); - if (fins->o == IR_TOBIT) { - nc.mode = IRCONV_TOBIT; /* Used only in the backpropagation cache. */ - nc.lim = 2; /* TOBIT can use a more optimistic rule. */ - } else { - nc.mode = fins->op2; - nc.lim = 1; - } - if (narrow_conv_backprop(&nc, fins->op1, 0) <= nc.lim) - return narrow_conv_emit(J, &nc); - } - return NEXTFOLD; -} - -/* -- Narrowing of implicit conversions ----------------------------------- */ - -/* Recursively strip overflow checks. */ -static TRef narrow_stripov(jit_State *J, TRef tr, int lastop, IRRef mode) -{ - IRRef ref = tref_ref(tr); - IRIns *ir = IR(ref); - int op = ir->o; - if (op >= IR_ADDOV && op <= lastop) { - BPropEntry *bp = narrow_bpc_get(J, ref, mode); - if (bp) { - return TREF(bp->val, irt_t(IR(bp->val)->t)); - } else { - IRRef op1 = ir->op1, op2 = ir->op2; /* The IR may be reallocated. */ - op1 = narrow_stripov(J, op1, lastop, mode); - op2 = narrow_stripov(J, op2, lastop, mode); - tr = emitir(IRT(op - IR_ADDOV + IR_ADD, - ((mode & IRCONV_DSTMASK) >> IRCONV_DSH)), op1, op2); - narrow_bpc_set(J, ref, tref_ref(tr), mode); - } - } else if (LJ_64 && (mode & IRCONV_SEXT) && !irt_is64(ir->t)) { - tr = emitir(IRT(IR_CONV, IRT_INTP), tr, mode); - } - return tr; -} - -/* Narrow array index. */ -TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef tr) -{ - IRIns *ir; - lua_assert(tref_isnumber(tr)); - if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ - return emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_INDEX); - /* Omit some overflow checks for array indexing. See comments above. */ - ir = IR(tref_ref(tr)); - if ((ir->o == IR_ADDOV || ir->o == IR_SUBOV) && irref_isk(ir->op2) && - (uint32_t)IR(ir->op2)->i + 0x40000000u < 0x80000000u) - return emitir(IRTI(ir->o - IR_ADDOV + IR_ADD), ir->op1, ir->op2); - return tr; -} - -/* Narrow conversion to integer operand (overflow undefined). */ -TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr) -{ - if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ - return emitir(IRTI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_ANY); - if (!tref_isinteger(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - /* - ** Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV. - ** Use IRCONV_TOBIT for the cache entries, since the semantics are the same. - */ - return narrow_stripov(J, tr, IR_MULOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT); -} - -/* Narrow conversion to bitop operand (overflow wrapped). */ -TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr) -{ - if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ - return emitir(IRTI(IR_TOBIT), tr, lj_ir_knum_tobit(J)); - if (!tref_isinteger(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - /* - ** Wrapped overflow semantics allow stripping of ADDOV and SUBOV. - ** MULOV cannot be stripped due to precision widening. - */ - return narrow_stripov(J, tr, IR_SUBOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT); -} - -#if LJ_HASFFI -/* Narrow C array index (overflow undefined). */ -TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef tr) -{ - lua_assert(tref_isnumber(tr)); - if (tref_isnum(tr)) - return emitir(IRT(IR_CONV, IRT_INTP), tr, - (IRT_INTP<<5)|IRT_NUM|IRCONV_TRUNC|IRCONV_ANY); - /* Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV. */ - return narrow_stripov(J, tr, IR_MULOV, - LJ_64 ? ((IRT_INTP<<5)|IRT_INT|IRCONV_SEXT) : - ((IRT_INTP<<5)|IRT_INT|IRCONV_TOBIT)); -} -#endif - -/* -- Narrowing of arithmetic operators ----------------------------------- */ - -/* Check whether a number fits into an int32_t (-0 is ok, too). */ -static int numisint(lua_Number n) -{ - return (n == (lua_Number)lj_num2int(n)); -} - -/* Narrowing of arithmetic operations. */ -TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc, - TValue *vb, TValue *vc, IROp op) -{ - if (tref_isstr(rb)) { - rb = emitir(IRTG(IR_STRTO, IRT_NUM), rb, 0); - lj_strscan_num(strV(vb), vb); - } - if (tref_isstr(rc)) { - rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0); - lj_strscan_num(strV(vc), vc); - } - /* Must not narrow MUL in non-DUALNUM variant, because it loses -0. */ - if ((op >= IR_ADD && op <= (LJ_DUALNUM ? IR_MUL : IR_SUB)) && - tref_isinteger(rb) && tref_isinteger(rc) && - numisint(lj_vm_foldarith(numberVnum(vb), numberVnum(vc), - (int)op - (int)IR_ADD))) - return emitir(IRTGI((int)op - (int)IR_ADD + (int)IR_ADDOV), rb, rc); - if (!tref_isnum(rb)) rb = emitir(IRTN(IR_CONV), rb, IRCONV_NUM_INT); - if (!tref_isnum(rc)) rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); - return emitir(IRTN(op), rb, rc); -} - -/* Narrowing of unary minus operator. */ -TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc) -{ - if (tref_isstr(rc)) { - rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0); - lj_strscan_num(strV(vc), vc); - } - if (tref_isinteger(rc)) { - if ((uint32_t)numberVint(vc) != 0x80000000u) - return emitir(IRTGI(IR_SUBOV), lj_ir_kint(J, 0), rc); - rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); - } - return emitir(IRTN(IR_NEG), rc, lj_ir_knum_neg(J)); -} - -/* Narrowing of modulo operator. */ -TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vc) -{ - TRef tmp; - if (tvisstr(vc) && !lj_strscan_num(strV(vc), vc)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - if ((LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) && - tref_isinteger(rb) && tref_isinteger(rc) && - (tvisint(vc) ? intV(vc) != 0 : !tviszero(vc))) { - emitir(IRTGI(IR_NE), rc, lj_ir_kint(J, 0)); - return emitir(IRTI(IR_MOD), rb, rc); - } - /* b % c ==> b - floor(b/c)*c */ - rb = lj_ir_tonum(J, rb); - rc = lj_ir_tonum(J, rc); - tmp = emitir(IRTN(IR_DIV), rb, rc); - tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_FLOOR); - tmp = emitir(IRTN(IR_MUL), tmp, rc); - return emitir(IRTN(IR_SUB), rb, tmp); -} - -/* Narrowing of power operator or math.pow. */ -TRef lj_opt_narrow_pow(jit_State *J, TRef rb, TRef rc, TValue *vc) -{ - if (tvisstr(vc) && !lj_strscan_num(strV(vc), vc)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - /* Narrowing must be unconditional to preserve (-x)^i semantics. */ - if (tvisint(vc) || numisint(numV(vc))) { - int checkrange = 0; - /* Split pow is faster for bigger exponents. But do this only for (+k)^i. */ - if (tref_isk(rb) && (int32_t)ir_knum(IR(tref_ref(rb)))->u32.hi >= 0) { - int32_t k = numberVint(vc); - if (!(k >= -65536 && k <= 65536)) goto split_pow; - checkrange = 1; - } - if (!tref_isinteger(rc)) { - if (tref_isstr(rc)) - rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0); - /* Guarded conversion to integer! */ - rc = emitir(IRTGI(IR_CONV), rc, IRCONV_INT_NUM|IRCONV_CHECK); - } - if (checkrange && !tref_isk(rc)) { /* Range guard: -65536 <= i <= 65536 */ - TRef tmp = emitir(IRTI(IR_ADD), rc, lj_ir_kint(J, 65536)); - emitir(IRTGI(IR_ULE), tmp, lj_ir_kint(J, 2*65536)); - } - return emitir(IRTN(IR_POW), rb, rc); - } -split_pow: - /* FOLD covers most cases, but some are easier to do here. */ - if (tref_isk(rb) && tvispone(ir_knum(IR(tref_ref(rb))))) - return rb; /* 1 ^ x ==> 1 */ - rc = lj_ir_tonum(J, rc); - if (tref_isk(rc) && ir_knum(IR(tref_ref(rc)))->n == 0.5) - return emitir(IRTN(IR_FPMATH), rb, IRFPM_SQRT); /* x ^ 0.5 ==> sqrt(x) */ - /* Split up b^c into exp2(c*log2(b)). Assembler may rejoin later. */ - rb = emitir(IRTN(IR_FPMATH), rb, IRFPM_LOG2); - rc = emitir(IRTN(IR_MUL), rb, rc); - return emitir(IRTN(IR_FPMATH), rc, IRFPM_EXP2); -} - -/* -- Predictive narrowing of induction variables ------------------------- */ - -/* Narrow a single runtime value. */ -static int narrow_forl(jit_State *J, cTValue *o) -{ - if (tvisint(o)) return 1; - if (LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) return numisint(numV(o)); - return 0; -} - -/* Narrow the FORL index type by looking at the runtime values. */ -IRType lj_opt_narrow_forl(jit_State *J, cTValue *tv) -{ - lua_assert(tvisnumber(&tv[FORL_IDX]) && - tvisnumber(&tv[FORL_STOP]) && - tvisnumber(&tv[FORL_STEP])); - /* Narrow only if the runtime values of start/stop/step are all integers. */ - if (narrow_forl(J, &tv[FORL_IDX]) && - narrow_forl(J, &tv[FORL_STOP]) && - narrow_forl(J, &tv[FORL_STEP])) { - /* And if the loop index can't possibly overflow. */ - lua_Number step = numberVnum(&tv[FORL_STEP]); - lua_Number sum = numberVnum(&tv[FORL_STOP]) + step; - if (0 <= step ? (sum <= 2147483647.0) : (sum >= -2147483648.0)) - return IRT_INT; - } - return IRT_NUM; -} - -#undef IR -#undef fins -#undef emitir -#undef emitir_raw - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_opt_sink.c b/third-party/LuaJIT-2.0.2/src/lj_opt_sink.c deleted file mode 100644 index 56e463687e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_opt_sink.c +++ /dev/null @@ -1,245 +0,0 @@ -/* -** SINK: Allocation Sinking and Store Sinking. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_sink_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_target.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Check whether the store ref points to an eligible allocation. */ -static IRIns *sink_checkalloc(jit_State *J, IRIns *irs) -{ - IRIns *ir = IR(irs->op1); - if (!irref_isk(ir->op2)) - return NULL; /* Non-constant key. */ - if (ir->o == IR_HREFK || ir->o == IR_AREF) - ir = IR(ir->op1); - else if (!(ir->o == IR_HREF || ir->o == IR_NEWREF || - ir->o == IR_FREF || ir->o == IR_ADD)) - return NULL; /* Unhandled reference type (for XSTORE). */ - ir = IR(ir->op1); - if (!(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW)) - return NULL; /* Not an allocation. */ - return ir; /* Return allocation. */ -} - -/* Recursively check whether a value depends on a PHI. */ -static int sink_phidep(jit_State *J, IRRef ref) -{ - IRIns *ir = IR(ref); - if (irt_isphi(ir->t)) return 1; - if (ir->op1 >= REF_FIRST && sink_phidep(J, ir->op1)) return 1; - if (ir->op2 >= REF_FIRST && sink_phidep(J, ir->op2)) return 1; - return 0; -} - -/* Check whether a value is a sinkable PHI or loop-invariant. */ -static int sink_checkphi(jit_State *J, IRIns *ira, IRRef ref) -{ - if (ref >= REF_FIRST) { - IRIns *ir = IR(ref); - if (irt_isphi(ir->t) || (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && - irt_isphi(IR(ir->op1)->t))) { - ira->prev++; - return 1; /* Sinkable PHI. */ - } - /* Otherwise the value must be loop-invariant. */ - return ref < J->loopref && !sink_phidep(J, ref); - } - return 1; /* Constant (non-PHI). */ -} - -/* Mark non-sinkable allocations using single-pass backward propagation. -** -** Roots for the marking process are: -** - Some PHIs or snapshots (see below). -** - Non-PHI, non-constant values stored to PHI allocations. -** - All guards. -** - Any remaining loads not eliminated by store-to-load forwarding. -** - Stores with non-constant keys. -** - All stored values. -*/ -static void sink_mark_ins(jit_State *J) -{ - IRIns *ir, *irlast = IR(J->cur.nins-1); - for (ir = irlast ; ; ir--) { - switch (ir->o) { - case IR_BASE: - return; /* Finished. */ - case IR_CALLL: /* IRCALL_lj_tab_len */ - case IR_ALOAD: case IR_HLOAD: case IR_XLOAD: case IR_TBAR: - irt_setmark(IR(ir->op1)->t); /* Mark ref for remaining loads. */ - break; - case IR_FLOAD: - if (irt_ismarked(ir->t) || ir->op2 == IRFL_TAB_META) - irt_setmark(IR(ir->op1)->t); /* Mark table for remaining loads. */ - break; - case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: { - IRIns *ira = sink_checkalloc(J, ir); - if (!ira || (irt_isphi(ira->t) && !sink_checkphi(J, ira, ir->op2))) - irt_setmark(IR(ir->op1)->t); /* Mark ineligible ref. */ - irt_setmark(IR(ir->op2)->t); /* Mark stored value. */ - break; - } -#if LJ_HASFFI - case IR_CNEWI: - if (irt_isphi(ir->t) && - (!sink_checkphi(J, ir, ir->op2) || - (LJ_32 && ir+1 < irlast && (ir+1)->o == IR_HIOP && - !sink_checkphi(J, ir, (ir+1)->op2)))) - irt_setmark(ir->t); /* Mark ineligible allocation. */ - /* fallthrough */ -#endif - case IR_USTORE: - irt_setmark(IR(ir->op2)->t); /* Mark stored value. */ - break; -#if LJ_HASFFI - case IR_CALLXS: -#endif - case IR_CALLS: - irt_setmark(IR(ir->op1)->t); /* Mark (potentially) stored values. */ - break; - case IR_PHI: { - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - irl->prev = irr->prev = 0; /* Clear PHI value counts. */ - if (irl->o == irr->o && - (irl->o == IR_TNEW || irl->o == IR_TDUP || - (LJ_HASFFI && (irl->o == IR_CNEW || irl->o == IR_CNEWI)))) - break; - irt_setmark(irl->t); - irt_setmark(irr->t); - break; - } - default: - if (irt_ismarked(ir->t) || irt_isguard(ir->t)) { /* Propagate mark. */ - if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t); - if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t); - } - break; - } - } -} - -/* Mark all instructions referenced by a snapshot. */ -static void sink_mark_snap(jit_State *J, SnapShot *snap) -{ - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - IRRef ref = snap_ref(map[n]); - if (!irref_isk(ref)) - irt_setmark(IR(ref)->t); - } -} - -/* Iteratively remark PHI refs with differing marks or PHI value counts. */ -static void sink_remark_phi(jit_State *J) -{ - IRIns *ir; - int remark; - do { - remark = 0; - for (ir = IR(J->cur.nins-1); ir->o == IR_PHI; ir--) { - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - if (((irl->t.irt ^ irr->t.irt) & IRT_MARK)) - remark = 1; - else if (irl->prev == irr->prev) - continue; - irt_setmark(IR(ir->op1)->t); - irt_setmark(IR(ir->op2)->t); - } - } while (remark); -} - -/* Sweep instructions and tag sunken allocations and stores. */ -static void sink_sweep_ins(jit_State *J) -{ - IRIns *ir, *irfirst = IR(J->cur.nk); - for (ir = IR(J->cur.nins-1) ; ir >= irfirst; ir--) { - switch (ir->o) { - case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: { - IRIns *ira = sink_checkalloc(J, ir); - if (ira && !irt_ismarked(ira->t)) { - int delta = (int)(ir - ira); - ir->prev = REGSP(RID_SINK, delta > 255 ? 255 : delta); - } else { - ir->prev = REGSP_INIT; - } - break; - } - case IR_NEWREF: - if (!irt_ismarked(IR(ir->op1)->t)) { - ir->prev = REGSP(RID_SINK, 0); - } else { - irt_clearmark(ir->t); - ir->prev = REGSP_INIT; - } - break; -#if LJ_HASFFI - case IR_CNEW: case IR_CNEWI: -#endif - case IR_TNEW: case IR_TDUP: - if (!irt_ismarked(ir->t)) { - ir->t.irt &= ~IRT_GUARD; - ir->prev = REGSP(RID_SINK, 0); - J->cur.sinktags = 1; /* Signal present SINK tags to assembler. */ - } else { - irt_clearmark(ir->t); - ir->prev = REGSP_INIT; - } - break; - case IR_PHI: { - IRIns *ira = IR(ir->op2); - if (!irt_ismarked(ira->t) && - (ira->o == IR_TNEW || ira->o == IR_TDUP || - (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI)))) { - ir->prev = REGSP(RID_SINK, 0); - } else { - ir->prev = REGSP_INIT; - } - break; - } - default: - irt_clearmark(ir->t); - ir->prev = REGSP_INIT; - break; - } - } -} - -/* Allocation sinking and store sinking. -** -** 1. Mark all non-sinkable allocations. -** 2. Then sink all remaining allocations and the related stores. -*/ -void lj_opt_sink(jit_State *J) -{ - const uint32_t need = (JIT_F_OPT_SINK|JIT_F_OPT_FWD| - JIT_F_OPT_DCE|JIT_F_OPT_CSE|JIT_F_OPT_FOLD); - if ((J->flags & need) == need && - (J->chain[IR_TNEW] || J->chain[IR_TDUP] || - (LJ_HASFFI && (J->chain[IR_CNEW] || J->chain[IR_CNEWI])))) { - if (!J->loopref) - sink_mark_snap(J, &J->cur.snap[J->cur.nsnap-1]); - sink_mark_ins(J); - if (J->loopref) - sink_remark_phi(J); - sink_sweep_ins(J); - } -} - -#undef IR - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_opt_split.c b/third-party/LuaJIT-2.0.2/src/lj_opt_split.c deleted file mode 100644 index 5a8c33b949..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_opt_split.c +++ /dev/null @@ -1,731 +0,0 @@ -/* -** SPLIT: Split 64 bit IR instructions into 32 bit IR instructions. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_split_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) - -#include "lj_err.h" -#include "lj_str.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_vm.h" - -/* SPLIT pass: -** -** This pass splits up 64 bit IR instructions into multiple 32 bit IR -** instructions. It's only active for soft-float targets or for 32 bit CPUs -** which lack native 64 bit integer operations (the FFI is currently the -** only emitter for 64 bit integer instructions). -** -** Splitting the IR in a separate pass keeps each 32 bit IR assembler -** backend simple. Only a small amount of extra functionality needs to be -** implemented. This is much easier than adding support for allocating -** register pairs to each backend (believe me, I tried). A few simple, but -** important optimizations can be performed by the SPLIT pass, which would -** be tedious to do in the backend. -** -** The basic idea is to replace each 64 bit IR instruction with its 32 bit -** equivalent plus an extra HIOP instruction. The splitted IR is not passed -** through FOLD or any other optimizations, so each HIOP is guaranteed to -** immediately follow it's counterpart. The actual functionality of HIOP is -** inferred from the previous instruction. -** -** The operands of HIOP hold the hiword input references. The output of HIOP -** is the hiword output reference, which is also used to hold the hiword -** register or spill slot information. The register allocator treats this -** instruction independently of any other instruction, which improves code -** quality compared to using fixed register pairs. -** -** It's easier to split up some instructions into two regular 32 bit -** instructions. E.g. XLOAD is split up into two XLOADs with two different -** addresses. Obviously 64 bit constants need to be split up into two 32 bit -** constants, too. Some hiword instructions can be entirely omitted, e.g. -** when zero-extending a 32 bit value to 64 bits. 64 bit arguments for calls -** are split up into two 32 bit arguments each. -** -** On soft-float targets, floating-point instructions are directly converted -** to soft-float calls by the SPLIT pass (except for comparisons and MIN/MAX). -** HIOP for number results has the type IRT_SOFTFP ("sfp" in -jdump). -** -** Here's the IR and x64 machine code for 'x.b = x.a + 1' for a struct with -** two int64_t fields: -** -** 0100 p32 ADD base +8 -** 0101 i64 XLOAD 0100 -** 0102 i64 ADD 0101 +1 -** 0103 p32 ADD base +16 -** 0104 i64 XSTORE 0103 0102 -** -** mov rax, [esi+0x8] -** add rax, +0x01 -** mov [esi+0x10], rax -** -** Here's the transformed IR and the x86 machine code after the SPLIT pass: -** -** 0100 p32 ADD base +8 -** 0101 int XLOAD 0100 -** 0102 p32 ADD base +12 -** 0103 int XLOAD 0102 -** 0104 int ADD 0101 +1 -** 0105 int HIOP 0103 +0 -** 0106 p32 ADD base +16 -** 0107 int XSTORE 0106 0104 -** 0108 int HIOP 0106 0105 -** -** mov eax, [esi+0x8] -** mov ecx, [esi+0xc] -** add eax, +0x01 -** adc ecx, +0x00 -** mov [esi+0x10], eax -** mov [esi+0x14], ecx -** -** You may notice the reassociated hiword address computation, which is -** later fused into the mov operands by the assembler. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Directly emit the transformed IR without updating chains etc. */ -static IRRef split_emit(jit_State *J, uint16_t ot, IRRef1 op1, IRRef1 op2) -{ - IRRef nref = lj_ir_nextins(J); - IRIns *ir = IR(nref); - ir->ot = ot; - ir->op1 = op1; - ir->op2 = op2; - return nref; -} - -#if LJ_SOFTFP -/* Emit a (checked) number to integer conversion. */ -static IRRef split_num2int(jit_State *J, IRRef lo, IRRef hi, int check) -{ - IRRef tmp, res; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), lo, hi); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hi, lo); -#endif - res = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_softfp_d2i); - if (check) { - tmp = split_emit(J, IRTI(IR_CALLN), res, IRCALL_softfp_i2d); - split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); - split_emit(J, IRTGI(IR_EQ), tmp, lo); - split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), tmp+1, hi); - } - return res; -} - -/* Emit a CALLN with one split 64 bit argument. */ -static IRRef split_call_l(jit_State *J, IRRef1 *hisubst, IRIns *oir, - IRIns *ir, IRCallID id) -{ - IRRef tmp, op1 = ir->op1; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); -#endif - ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); - return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); -} - -/* Emit a CALLN with one split 64 bit argument and a 32 bit argument. */ -static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir, - IRIns *ir, IRCallID id) -{ - IRRef tmp, op1 = ir->op1, op2 = ir->op2; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); -#endif - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); - ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); - return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); -} -#endif - -/* Emit a CALLN with two split 64 bit arguments. */ -static IRRef split_call_ll(jit_State *J, IRRef1 *hisubst, IRIns *oir, - IRIns *ir, IRCallID id) -{ - IRRef tmp, op1 = ir->op1, op2 = ir->op2; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); -#endif - ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); - return split_emit(J, - IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT), - tmp, tmp); -} - -/* Get a pointer to the other 32 bit word (LE: hiword, BE: loword). */ -static IRRef split_ptr(jit_State *J, IRIns *oir, IRRef ref) -{ - IRRef nref = oir[ref].prev; - IRIns *ir = IR(nref); - int32_t ofs = 4; - if (ir->o == IR_KPTR) - return lj_ir_kptr(J, (char *)ir_kptr(ir) + ofs); - if (ir->o == IR_ADD && irref_isk(ir->op2) && !irt_isphi(oir[ref].t)) { - /* Reassociate address. */ - ofs += IR(ir->op2)->i; - nref = ir->op1; - if (ofs == 0) return nref; - } - return split_emit(J, IRTI(IR_ADD), nref, lj_ir_kint(J, ofs)); -} - -/* Substitute references of a snapshot. */ -static void split_subst_snap(jit_State *J, SnapShot *snap, IRIns *oir) -{ - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - IRIns *ir = &oir[snap_ref(sn)]; - if (!(LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && irref_isk(snap_ref(sn)))) - map[n] = ((sn & 0xffff0000) | ir->prev); - } -} - -/* Transform the old IR to the new IR. */ -static void split_ir(jit_State *J) -{ - IRRef nins = J->cur.nins, nk = J->cur.nk; - MSize irlen = nins - nk; - MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1)); - IRIns *oir = (IRIns *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, need); - IRRef1 *hisubst; - IRRef ref, snref; - SnapShot *snap; - - /* Copy old IR to buffer. */ - memcpy(oir, IR(nk), irlen*sizeof(IRIns)); - /* Bias hiword substitution table and old IR. Loword kept in field prev. */ - hisubst = (IRRef1 *)&oir[irlen] - nk; - oir -= nk; - - /* Remove all IR instructions, but retain IR constants. */ - J->cur.nins = REF_FIRST; - J->loopref = 0; - - /* Process constants and fixed references. */ - for (ref = nk; ref <= REF_BASE; ref++) { - IRIns *ir = &oir[ref]; - if ((LJ_SOFTFP && ir->o == IR_KNUM) || ir->o == IR_KINT64) { - /* Split up 64 bit constant. */ - TValue tv = *ir_k64(ir); - ir->prev = lj_ir_kint(J, (int32_t)tv.u32.lo); - hisubst[ref] = lj_ir_kint(J, (int32_t)tv.u32.hi); - } else { - ir->prev = ref; /* Identity substitution for loword. */ - hisubst[ref] = 0; - } - } - - /* Process old IR instructions. */ - snap = J->cur.snap; - snref = snap->ref; - for (ref = REF_FIRST; ref < nins; ref++) { - IRIns *ir = &oir[ref]; - IRRef nref = lj_ir_nextins(J); - IRIns *nir = IR(nref); - IRRef hi = 0; - - if (ref >= snref) { - snap->ref = nref; - split_subst_snap(J, snap++, oir); - snref = snap < &J->cur.snap[J->cur.nsnap] ? snap->ref : ~(IRRef)0; - } - - /* Copy-substitute old instruction to new instruction. */ - nir->op1 = ir->op1 < nk ? ir->op1 : oir[ir->op1].prev; - nir->op2 = ir->op2 < nk ? ir->op2 : oir[ir->op2].prev; - ir->prev = nref; /* Loword substitution. */ - nir->o = ir->o; - nir->t.irt = ir->t.irt & ~(IRT_MARK|IRT_ISPHI); - hisubst[ref] = 0; - - /* Split 64 bit instructions. */ -#if LJ_SOFTFP - if (irt_isnum(ir->t)) { - nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */ - /* Note: hi ref = lo ref + 1! Required for SNAP_SOFTFPNUM logic. */ - switch (ir->o) { - case IR_ADD: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_add); - break; - case IR_SUB: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_sub); - break; - case IR_MUL: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_mul); - break; - case IR_DIV: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_div); - break; - case IR_POW: - hi = split_call_li(J, hisubst, oir, ir, IRCALL_lj_vm_powi); - break; - case IR_FPMATH: - /* Try to rejoin pow from EXP2, MUL and LOG2. */ - if (nir->op2 == IRFPM_EXP2 && nir->op1 > J->loopref) { - IRIns *irp = IR(nir->op1); - if (irp->o == IR_CALLN && irp->op2 == IRCALL_softfp_mul) { - IRIns *irm4 = IR(irp->op1); - IRIns *irm3 = IR(irm4->op1); - IRIns *irm12 = IR(irm3->op1); - IRIns *irl1 = IR(irm12->op1); - if (irm12->op1 > J->loopref && irl1->o == IR_CALLN && - irl1->op2 == IRCALL_lj_vm_log2) { - IRRef tmp = irl1->op1; /* Recycle first two args from LOG2. */ - IRRef arg3 = irm3->op2, arg4 = irm4->op2; - J->cur.nins--; - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg3); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg4); - ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_pow); - hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); - break; - } - } - } - hi = split_call_l(J, hisubst, oir, ir, IRCALL_lj_vm_floor + ir->op2); - break; - case IR_ATAN2: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_atan2); - break; - case IR_LDEXP: - hi = split_call_li(J, hisubst, oir, ir, IRCALL_ldexp); - break; - case IR_NEG: case IR_ABS: - nir->o = IR_CONV; /* Pass through loword. */ - nir->op2 = (IRT_INT << 5) | IRT_INT; - hi = split_emit(J, IRT(ir->o == IR_NEG ? IR_BXOR : IR_BAND, IRT_SOFTFP), - hisubst[ir->op1], hisubst[ir->op2]); - break; - case IR_SLOAD: - if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from int to number. */ - nir->op2 &= ~IRSLOAD_CONVERT; - ir->prev = nref = split_emit(J, IRTI(IR_CALLN), nref, - IRCALL_softfp_i2d); - hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - break; - } - /* fallthrough */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - case IR_STRTO: - hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - break; - case IR_XLOAD: { - IRIns inslo = *nir; /* Save/undo the emit of the lo XLOAD. */ - J->cur.nins--; - hi = split_ptr(J, oir, ir->op1); /* Insert the hiref ADD. */ - nref = lj_ir_nextins(J); - nir = IR(nref); - *nir = inslo; /* Re-emit lo XLOAD immediately before hi XLOAD. */ - hi = split_emit(J, IRT(IR_XLOAD, IRT_SOFTFP), hi, ir->op2); -#if LJ_LE - ir->prev = nref; -#else - ir->prev = hi; hi = nref; -#endif - break; - } - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_XSTORE: - split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nir->op1, hisubst[ir->op2]); - break; - case IR_CONV: { /* Conversion to number. Others handled below. */ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); - UNUSED(st); -#if LJ_32 && LJ_HASFFI - if (st == IRT_I64 || st == IRT_U64) { - hi = split_call_l(J, hisubst, oir, ir, - st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d); - break; - } -#endif - lua_assert(st == IRT_INT || - (LJ_32 && LJ_HASFFI && (st == IRT_U32 || st == IRT_FLOAT))); - nir->o = IR_CALLN; -#if LJ_32 && LJ_HASFFI - nir->op2 = st == IRT_INT ? IRCALL_softfp_i2d : - st == IRT_FLOAT ? IRCALL_softfp_f2d : - IRCALL_softfp_ui2d; -#else - nir->op2 = IRCALL_softfp_i2d; -#endif - hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - break; - } - case IR_CALLN: - case IR_CALLL: - case IR_CALLS: - case IR_CALLXS: - goto split_call; - case IR_PHI: - if (nir->op1 == nir->op2) - J->cur.nins--; /* Drop useless PHIs. */ - if (hisubst[ir->op1] != hisubst[ir->op2]) - split_emit(J, IRT(IR_PHI, IRT_SOFTFP), - hisubst[ir->op1], hisubst[ir->op2]); - break; - case IR_HIOP: - J->cur.nins--; /* Drop joining HIOP. */ - ir->prev = nir->op1; - hi = nir->op2; - break; - default: - lua_assert(ir->o <= IR_NE || ir->o == IR_MIN || ir->o == IR_MAX); - hi = split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), - hisubst[ir->op1], hisubst[ir->op2]); - break; - } - } else -#endif -#if LJ_32 && LJ_HASFFI - if (irt_isint64(ir->t)) { - IRRef hiref = hisubst[ir->op1]; - nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */ - switch (ir->o) { - case IR_ADD: - case IR_SUB: - /* Use plain op for hiword if loword cannot produce a carry/borrow. */ - if (irref_isk(nir->op2) && IR(nir->op2)->i == 0) { - ir->prev = nir->op1; /* Pass through loword. */ - nir->op1 = hiref; nir->op2 = hisubst[ir->op2]; - hi = nref; - break; - } - /* fallthrough */ - case IR_NEG: - hi = split_emit(J, IRTI(IR_HIOP), hiref, hisubst[ir->op2]); - break; - case IR_MUL: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_lj_carith_mul64); - break; - case IR_DIV: - hi = split_call_ll(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : - IRCALL_lj_carith_divu64); - break; - case IR_MOD: - hi = split_call_ll(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : - IRCALL_lj_carith_modu64); - break; - case IR_POW: - hi = split_call_ll(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : - IRCALL_lj_carith_powu64); - break; - case IR_FLOAD: - lua_assert(ir->op2 == IRFL_CDATA_INT64); - hi = split_emit(J, IRTI(IR_FLOAD), nir->op1, IRFL_CDATA_INT64_4); -#if LJ_BE - ir->prev = hi; hi = nref; -#endif - break; - case IR_XLOAD: - hi = split_emit(J, IRTI(IR_XLOAD), split_ptr(J, oir, ir->op1), ir->op2); -#if LJ_BE - ir->prev = hi; hi = nref; -#endif - break; - case IR_XSTORE: - split_emit(J, IRTI(IR_HIOP), nir->op1, hisubst[ir->op2]); - break; - case IR_CONV: { /* Conversion to 64 bit integer. Others handled below. */ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if LJ_SOFTFP - if (st == IRT_NUM) { /* NUM to 64 bit int conv. */ - hi = split_call_l(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul); - } else if (st == IRT_FLOAT) { /* FLOAT to 64 bit int conv. */ - nir->o = IR_CALLN; - nir->op2 = irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul; - hi = split_emit(J, IRTI(IR_HIOP), nref, nref); - } -#else - if (st == IRT_NUM || st == IRT_FLOAT) { /* FP to 64 bit int conv. */ - hi = split_emit(J, IRTI(IR_HIOP), nir->op1, nref); - } -#endif - else if (st == IRT_I64 || st == IRT_U64) { /* 64/64 bit cast. */ - /* Drop cast, since assembler doesn't care. */ - goto fwdlo; - } else if ((ir->op2 & IRCONV_SEXT)) { /* Sign-extend to 64 bit. */ - IRRef k31 = lj_ir_kint(J, 31); - nir = IR(nref); /* May have been reallocated. */ - ir->prev = nir->op1; /* Pass through loword. */ - nir->o = IR_BSAR; /* hi = bsar(lo, 31). */ - nir->op2 = k31; - hi = nref; - } else { /* Zero-extend to 64 bit. */ - hi = lj_ir_kint(J, 0); - goto fwdlo; - } - break; - } - case IR_CALLXS: - goto split_call; - case IR_PHI: { - IRRef hiref2; - if ((irref_isk(nir->op1) && irref_isk(nir->op2)) || - nir->op1 == nir->op2) - J->cur.nins--; /* Drop useless PHIs. */ - hiref2 = hisubst[ir->op2]; - if (!((irref_isk(hiref) && irref_isk(hiref2)) || hiref == hiref2)) - split_emit(J, IRTI(IR_PHI), hiref, hiref2); - break; - } - case IR_HIOP: - J->cur.nins--; /* Drop joining HIOP. */ - ir->prev = nir->op1; - hi = nir->op2; - break; - default: - lua_assert(ir->o <= IR_NE); /* Comparisons. */ - split_emit(J, IRTGI(IR_HIOP), hiref, hisubst[ir->op2]); - break; - } - } else -#endif -#if LJ_SOFTFP - if (ir->o == IR_SLOAD) { - if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from number to int. */ - nir->op2 &= ~IRSLOAD_CONVERT; - if (!(nir->op2 & IRSLOAD_TYPECHECK)) - nir->t.irt = IRT_INT; /* Drop guard. */ - split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - ir->prev = split_num2int(J, nref, nref+1, irt_isguard(ir->t)); - } - } else if (ir->o == IR_TOBIT) { - IRRef tmp, op1 = ir->op1; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); -#endif - ir->prev = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_lj_vm_tobit); - } else if (ir->o == IR_TOSTR) { - if (hisubst[ir->op1]) { - if (irref_isk(ir->op1)) - nir->op1 = ir->op1; - else - split_emit(J, IRT(IR_HIOP, IRT_NIL), hisubst[ir->op1], nref); - } - } else if (ir->o == IR_HREF || ir->o == IR_NEWREF) { - if (irref_isk(ir->op2) && hisubst[ir->op2]) - nir->op2 = ir->op2; - } else -#endif - if (ir->o == IR_CONV) { /* See above, too. */ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if LJ_32 && LJ_HASFFI - if (st == IRT_I64 || st == IRT_U64) { /* Conversion from 64 bit int. */ -#if LJ_SOFTFP - if (irt_isfloat(ir->t)) { - split_call_l(J, hisubst, oir, ir, - st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f); - J->cur.nins--; /* Drop unused HIOP. */ - } -#else - if (irt_isfp(ir->t)) { /* 64 bit integer to FP conversion. */ - ir->prev = split_emit(J, IRT(IR_HIOP, irt_type(ir->t)), - hisubst[ir->op1], nref); - } -#endif - else { /* Truncate to lower 32 bits. */ - fwdlo: - ir->prev = nir->op1; /* Forward loword. */ - /* Replace with NOP to avoid messing up the snapshot logic. */ - nir->ot = IRT(IR_NOP, IRT_NIL); - nir->op1 = nir->op2 = 0; - } - } -#endif -#if LJ_SOFTFP && LJ_32 && LJ_HASFFI - else if (irt_isfloat(ir->t)) { - if (st == IRT_NUM) { - split_call_l(J, hisubst, oir, ir, IRCALL_softfp_d2f); - J->cur.nins--; /* Drop unused HIOP. */ - } else { - nir->o = IR_CALLN; - nir->op2 = st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f; - } - } else if (st == IRT_FLOAT) { - nir->o = IR_CALLN; - nir->op2 = irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui; - } else -#endif -#if LJ_SOFTFP - if (st == IRT_NUM || (LJ_32 && LJ_HASFFI && st == IRT_FLOAT)) { - if (irt_isguard(ir->t)) { - lua_assert(st == IRT_NUM && irt_isint(ir->t)); - J->cur.nins--; - ir->prev = split_num2int(J, nir->op1, hisubst[ir->op1], 1); - } else { - split_call_l(J, hisubst, oir, ir, -#if LJ_32 && LJ_HASFFI - st == IRT_NUM ? - (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) : - (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui) -#else - IRCALL_softfp_d2i -#endif - ); - J->cur.nins--; /* Drop unused HIOP. */ - } - } -#endif - } else if (ir->o == IR_CALLXS) { - IRRef hiref; - split_call: - hiref = hisubst[ir->op1]; - if (hiref) { - IROpT ot = nir->ot; - IRRef op2 = nir->op2; - nir->ot = IRT(IR_CARG, IRT_NIL); -#if LJ_LE - nir->op2 = hiref; -#else - nir->op2 = nir->op1; nir->op1 = hiref; -#endif - ir->prev = nref = split_emit(J, ot, nref, op2); - } - if (LJ_SOFTFP ? irt_is64(ir->t) : irt_isint64(ir->t)) - hi = split_emit(J, - IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT), - nref, nref); - } else if (ir->o == IR_CARG) { - IRRef hiref = hisubst[ir->op1]; - if (hiref) { - IRRef op2 = nir->op2; -#if LJ_LE - nir->op2 = hiref; -#else - nir->op2 = nir->op1; nir->op1 = hiref; -#endif - ir->prev = nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2); - nir = IR(nref); - } - hiref = hisubst[ir->op2]; - if (hiref) { -#if !LJ_TARGET_X86 - int carg = 0; - IRIns *cir; - for (cir = IR(nir->op1); cir->o == IR_CARG; cir = IR(cir->op1)) - carg++; - if ((carg & 1) == 0) { /* Align 64 bit arguments. */ - IRRef op2 = nir->op2; - nir->op2 = REF_NIL; - nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2); - nir = IR(nref); - } -#endif -#if LJ_BE - { IRRef tmp = nir->op2; nir->op2 = hiref; hiref = tmp; } -#endif - ir->prev = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, hiref); - } - } else if (ir->o == IR_CNEWI) { - if (hisubst[ir->op2]) - split_emit(J, IRT(IR_HIOP, IRT_NIL), nref, hisubst[ir->op2]); - } else if (ir->o == IR_LOOP) { - J->loopref = nref; /* Needed by assembler. */ - } - hisubst[ref] = hi; /* Store hiword substitution. */ - } - if (snref == nins) { /* Substitution for last snapshot. */ - snap->ref = J->cur.nins; - split_subst_snap(J, snap, oir); - } - - /* Add PHI marks. */ - for (ref = J->cur.nins-1; ref >= REF_FIRST; ref--) { - IRIns *ir = IR(ref); - if (ir->o != IR_PHI) break; - if (!irref_isk(ir->op1)) irt_setphi(IR(ir->op1)->t); - if (ir->op2 > J->loopref) irt_setphi(IR(ir->op2)->t); - } -} - -/* Protected callback for split pass. */ -static TValue *cpsplit(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - split_ir(J); - UNUSED(L); UNUSED(dummy); - return NULL; -} - -#if defined(LUA_USE_ASSERT) || LJ_SOFTFP -/* Slow, but sure way to check whether a SPLIT pass is needed. */ -static int split_needsplit(jit_State *J) -{ - IRIns *ir, *irend; - IRRef ref; - for (ir = IR(REF_FIRST), irend = IR(J->cur.nins); ir < irend; ir++) - if (LJ_SOFTFP ? irt_is64orfp(ir->t) : irt_isint64(ir->t)) - return 1; - if (LJ_SOFTFP) { - for (ref = J->chain[IR_SLOAD]; ref; ref = IR(ref)->prev) - if ((IR(ref)->op2 & IRSLOAD_CONVERT)) - return 1; - if (J->chain[IR_TOBIT]) - return 1; - } - for (ref = J->chain[IR_CONV]; ref; ref = IR(ref)->prev) { - IRType st = (IR(ref)->op2 & IRCONV_SRCMASK); - if ((LJ_SOFTFP && (st == IRT_NUM || st == IRT_FLOAT)) || - st == IRT_I64 || st == IRT_U64) - return 1; - } - return 0; /* Nope. */ -} -#endif - -/* SPLIT pass. */ -void lj_opt_split(jit_State *J) -{ -#if LJ_SOFTFP - if (!J->needsplit) - J->needsplit = split_needsplit(J); -#else - lua_assert(J->needsplit >= split_needsplit(J)); /* Verify flag. */ -#endif - if (J->needsplit) { - int errcode = lj_vm_cpcall(J->L, NULL, J, cpsplit); - if (errcode) { - /* Completely reset the trace to avoid inconsistent dump on abort. */ - J->cur.nins = J->cur.nk = REF_BASE; - J->cur.nsnap = 0; - lj_err_throw(J->L, errcode); /* Propagate errors. */ - } - } -} - -#undef IR - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_parse.c b/third-party/LuaJIT-2.0.2/src/lj_parse.c deleted file mode 100644 index 7ff7d72855..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_parse.c +++ /dev/null @@ -1,2750 +0,0 @@ -/* -** Lua parser (source code -> bytecode). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_parse_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_state.h" -#include "lj_bc.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_lex.h" -#include "lj_parse.h" -#include "lj_vm.h" -#include "lj_vmevent.h" - -/* -- Parser structures and definitions ----------------------------------- */ - -/* Expression kinds. */ -typedef enum { - /* Constant expressions must be first and in this order: */ - VKNIL, - VKFALSE, - VKTRUE, - VKSTR, /* sval = string value */ - VKNUM, /* nval = number value */ - VKLAST = VKNUM, - VKCDATA, /* nval = cdata value, not treated as a constant expression */ - /* Non-constant expressions follow: */ - VLOCAL, /* info = local register, aux = vstack index */ - VUPVAL, /* info = upvalue index, aux = vstack index */ - VGLOBAL, /* sval = string value */ - VINDEXED, /* info = table register, aux = index reg/byte/string const */ - VJMP, /* info = instruction PC */ - VRELOCABLE, /* info = instruction PC */ - VNONRELOC, /* info = result register */ - VCALL, /* info = instruction PC, aux = base */ - VVOID -} ExpKind; - -/* Expression descriptor. */ -typedef struct ExpDesc { - union { - struct { - uint32_t info; /* Primary info. */ - uint32_t aux; /* Secondary info. */ - } s; - TValue nval; /* Number value. */ - GCstr *sval; /* String value. */ - } u; - ExpKind k; - BCPos t; /* True condition jump list. */ - BCPos f; /* False condition jump list. */ -} ExpDesc; - -/* Macros for expressions. */ -#define expr_hasjump(e) ((e)->t != (e)->f) - -#define expr_isk(e) ((e)->k <= VKLAST) -#define expr_isk_nojump(e) (expr_isk(e) && !expr_hasjump(e)) -#define expr_isnumk(e) ((e)->k == VKNUM) -#define expr_isnumk_nojump(e) (expr_isnumk(e) && !expr_hasjump(e)) -#define expr_isstrk(e) ((e)->k == VKSTR) - -#define expr_numtv(e) check_exp(expr_isnumk((e)), &(e)->u.nval) -#define expr_numberV(e) numberVnum(expr_numtv((e))) - -/* Initialize expression. */ -static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info) -{ - e->k = k; - e->u.s.info = info; - e->f = e->t = NO_JMP; -} - -/* Check number constant for +-0. */ -static int expr_numiszero(ExpDesc *e) -{ - TValue *o = expr_numtv(e); - return tvisint(o) ? (intV(o) == 0) : tviszero(o); -} - -/* Per-function linked list of scope blocks. */ -typedef struct FuncScope { - struct FuncScope *prev; /* Link to outer scope. */ - MSize vstart; /* Start of block-local variables. */ - uint8_t nactvar; /* Number of active vars outside the scope. */ - uint8_t flags; /* Scope flags. */ -} FuncScope; - -#define FSCOPE_LOOP 0x01 /* Scope is a (breakable) loop. */ -#define FSCOPE_BREAK 0x02 /* Break used in scope. */ -#define FSCOPE_GOLA 0x04 /* Goto or label used in scope. */ -#define FSCOPE_UPVAL 0x08 /* Upvalue in scope. */ -#define FSCOPE_NOCLOSE 0x10 /* Do not close upvalues. */ - -#define NAME_BREAK ((GCstr *)(uintptr_t)1) - -/* Index into variable stack. */ -typedef uint16_t VarIndex; -#define LJ_MAX_VSTACK (65536 - LJ_MAX_UPVAL) - -/* Variable/goto/label info. */ -#define VSTACK_VAR_RW 0x01 /* R/W variable. */ -#define VSTACK_GOTO 0x02 /* Pending goto. */ -#define VSTACK_LABEL 0x04 /* Label. */ - -/* Per-function state. */ -typedef struct FuncState { - GCtab *kt; /* Hash table for constants. */ - LexState *ls; /* Lexer state. */ - lua_State *L; /* Lua state. */ - FuncScope *bl; /* Current scope. */ - struct FuncState *prev; /* Enclosing function. */ - BCPos pc; /* Next bytecode position. */ - BCPos lasttarget; /* Bytecode position of last jump target. */ - BCPos jpc; /* Pending jump list to next bytecode. */ - BCReg freereg; /* First free register. */ - BCReg nactvar; /* Number of active local variables. */ - BCReg nkn, nkgc; /* Number of lua_Number/GCobj constants */ - BCLine linedefined; /* First line of the function definition. */ - BCInsLine *bcbase; /* Base of bytecode stack. */ - BCPos bclim; /* Limit of bytecode stack. */ - MSize vbase; /* Base of variable stack for this function. */ - uint8_t flags; /* Prototype flags. */ - uint8_t numparams; /* Number of parameters. */ - uint8_t framesize; /* Fixed frame size. */ - uint8_t nuv; /* Number of upvalues */ - VarIndex varmap[LJ_MAX_LOCVAR]; /* Map from register to variable idx. */ - VarIndex uvmap[LJ_MAX_UPVAL]; /* Map from upvalue to variable idx. */ - VarIndex uvtmp[LJ_MAX_UPVAL]; /* Temporary upvalue map. */ -} FuncState; - -/* Binary and unary operators. ORDER OPR */ -typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, /* ORDER ARITH */ - OPR_CONCAT, - OPR_NE, OPR_EQ, - OPR_LT, OPR_GE, OPR_LE, OPR_GT, - OPR_AND, OPR_OR, - OPR_NOBINOPR -} BinOpr; - -LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT); -LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT); -LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT); -LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD); -LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD); -LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD); -LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD); - -/* -- Error handling ------------------------------------------------------ */ - -LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em) -{ - lj_lex_error(ls, ls->token, em); -} - -LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken token) -{ - lj_lex_error(ls, ls->token, LJ_ERR_XTOKEN, lj_lex_token2str(ls, token)); -} - -LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what) -{ - if (fs->linedefined == 0) - lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what); - else - lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what); -} - -#define checklimit(fs, v, l, m) if ((v) >= (l)) err_limit(fs, l, m) -#define checklimitgt(fs, v, l, m) if ((v) > (l)) err_limit(fs, l, m) -#define checkcond(ls, c, em) { if (!(c)) err_syntax(ls, em); } - -/* -- Management of constants --------------------------------------------- */ - -/* Return bytecode encoding for primitive constant. */ -#define const_pri(e) check_exp((e)->k <= VKTRUE, (e)->k) - -#define tvhaskslot(o) ((o)->u32.hi == 0) -#define tvkslot(o) ((o)->u32.lo) - -/* Add a number constant. */ -static BCReg const_num(FuncState *fs, ExpDesc *e) -{ - lua_State *L = fs->L; - TValue *o; - lua_assert(expr_isnumk(e)); - o = lj_tab_set(L, fs->kt, &e->u.nval); - if (tvhaskslot(o)) - return tvkslot(o); - o->u64 = fs->nkn; - return fs->nkn++; -} - -/* Add a GC object constant. */ -static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype) -{ - lua_State *L = fs->L; - TValue key, *o; - setgcV(L, &key, gc, itype); - /* NOBARRIER: the key is new or kept alive. */ - o = lj_tab_set(L, fs->kt, &key); - if (tvhaskslot(o)) - return tvkslot(o); - o->u64 = fs->nkgc; - return fs->nkgc++; -} - -/* Add a string constant. */ -static BCReg const_str(FuncState *fs, ExpDesc *e) -{ - lua_assert(expr_isstrk(e) || e->k == VGLOBAL); - return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR); -} - -/* Anchor string constant to avoid GC. */ -GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len) -{ - /* NOBARRIER: the key is new or kept alive. */ - lua_State *L = ls->L; - GCstr *s = lj_str_new(L, str, len); - TValue *tv = lj_tab_setstr(L, ls->fs->kt, s); - if (tvisnil(tv)) setboolV(tv, 1); - lj_gc_check(L); - return s; -} - -#if LJ_HASFFI -/* Anchor cdata to avoid GC. */ -void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd) -{ - /* NOBARRIER: the key is new or kept alive. */ - lua_State *L = ls->L; - setcdataV(L, tv, cd); - setboolV(lj_tab_set(L, ls->fs->kt, tv), 1); -} -#endif - -/* -- Jump list handling -------------------------------------------------- */ - -/* Get next element in jump list. */ -static BCPos jmp_next(FuncState *fs, BCPos pc) -{ - ptrdiff_t delta = bc_j(fs->bcbase[pc].ins); - if ((BCPos)delta == NO_JMP) - return NO_JMP; - else - return (BCPos)(((ptrdiff_t)pc+1)+delta); -} - -/* Check if any of the instructions on the jump list produce no value. */ -static int jmp_novalue(FuncState *fs, BCPos list) -{ - for (; list != NO_JMP; list = jmp_next(fs, list)) { - BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins; - if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG)) - return 1; - } - return 0; -} - -/* Patch register of test instructions. */ -static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg) -{ - BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc]; - BCOp op = bc_op(ilp->ins); - if (op == BC_ISTC || op == BC_ISFC) { - if (reg != NO_REG && reg != bc_d(ilp->ins)) { - setbc_a(&ilp->ins, reg); - } else { /* Nothing to store or already in the right register. */ - setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC)); - setbc_a(&ilp->ins, 0); - } - } else if (bc_a(ilp->ins) == NO_REG) { - if (reg == NO_REG) { - ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0); - } else { - setbc_a(&ilp->ins, reg); - if (reg >= bc_a(ilp[1].ins)) - setbc_a(&ilp[1].ins, reg+1); - } - } else { - return 0; /* Cannot patch other instructions. */ - } - return 1; -} - -/* Drop values for all instructions on jump list. */ -static void jmp_dropval(FuncState *fs, BCPos list) -{ - for (; list != NO_JMP; list = jmp_next(fs, list)) - jmp_patchtestreg(fs, list, NO_REG); -} - -/* Patch jump instruction to target. */ -static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest) -{ - BCIns *jmp = &fs->bcbase[pc].ins; - BCPos offset = dest-(pc+1)+BCBIAS_J; - lua_assert(dest != NO_JMP); - if (offset > BCMAX_D) - err_syntax(fs->ls, LJ_ERR_XJUMP); - setbc_d(jmp, offset); -} - -/* Append to jump list. */ -static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2) -{ - if (l2 == NO_JMP) { - return; - } else if (*l1 == NO_JMP) { - *l1 = l2; - } else { - BCPos list = *l1; - BCPos next; - while ((next = jmp_next(fs, list)) != NO_JMP) /* Find last element. */ - list = next; - jmp_patchins(fs, list, l2); - } -} - -/* Patch jump list and preserve produced values. */ -static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget, - BCReg reg, BCPos dtarget) -{ - while (list != NO_JMP) { - BCPos next = jmp_next(fs, list); - if (jmp_patchtestreg(fs, list, reg)) - jmp_patchins(fs, list, vtarget); /* Jump to target with value. */ - else - jmp_patchins(fs, list, dtarget); /* Jump to default target. */ - list = next; - } -} - -/* Jump to following instruction. Append to list of pending jumps. */ -static void jmp_tohere(FuncState *fs, BCPos list) -{ - fs->lasttarget = fs->pc; - jmp_append(fs, &fs->jpc, list); -} - -/* Patch jump list to target. */ -static void jmp_patch(FuncState *fs, BCPos list, BCPos target) -{ - if (target == fs->pc) { - jmp_tohere(fs, list); - } else { - lua_assert(target < fs->pc); - jmp_patchval(fs, list, target, NO_REG, target); - } -} - -/* -- Bytecode register allocator ----------------------------------------- */ - -/* Bump frame size. */ -static void bcreg_bump(FuncState *fs, BCReg n) -{ - BCReg sz = fs->freereg + n; - if (sz > fs->framesize) { - if (sz >= LJ_MAX_SLOTS) - err_syntax(fs->ls, LJ_ERR_XSLOTS); - fs->framesize = (uint8_t)sz; - } -} - -/* Reserve registers. */ -static void bcreg_reserve(FuncState *fs, BCReg n) -{ - bcreg_bump(fs, n); - fs->freereg += n; -} - -/* Free register. */ -static void bcreg_free(FuncState *fs, BCReg reg) -{ - if (reg >= fs->nactvar) { - fs->freereg--; - lua_assert(reg == fs->freereg); - } -} - -/* Free register for expression. */ -static void expr_free(FuncState *fs, ExpDesc *e) -{ - if (e->k == VNONRELOC) - bcreg_free(fs, e->u.s.info); -} - -/* -- Bytecode emitter ---------------------------------------------------- */ - -/* Emit bytecode instruction. */ -static BCPos bcemit_INS(FuncState *fs, BCIns ins) -{ - BCPos pc = fs->pc; - LexState *ls = fs->ls; - jmp_patchval(fs, fs->jpc, pc, NO_REG, pc); - fs->jpc = NO_JMP; - if (LJ_UNLIKELY(pc >= fs->bclim)) { - ptrdiff_t base = fs->bcbase - ls->bcstack; - checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions"); - lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine); - fs->bclim = (BCPos)(ls->sizebcstack - base); - fs->bcbase = ls->bcstack + base; - } - fs->bcbase[pc].ins = ins; - fs->bcbase[pc].line = ls->lastline; - fs->pc = pc+1; - return pc; -} - -#define bcemit_ABC(fs, o, a, b, c) bcemit_INS(fs, BCINS_ABC(o, a, b, c)) -#define bcemit_AD(fs, o, a, d) bcemit_INS(fs, BCINS_AD(o, a, d)) -#define bcemit_AJ(fs, o, a, j) bcemit_INS(fs, BCINS_AJ(o, a, j)) - -#define bcptr(fs, e) (&(fs)->bcbase[(e)->u.s.info].ins) - -/* -- Bytecode emitter for expressions ------------------------------------ */ - -/* Discharge non-constant expression to any register. */ -static void expr_discharge(FuncState *fs, ExpDesc *e) -{ - BCIns ins; - if (e->k == VUPVAL) { - ins = BCINS_AD(BC_UGET, 0, e->u.s.info); - } else if (e->k == VGLOBAL) { - ins = BCINS_AD(BC_GGET, 0, const_str(fs, e)); - } else if (e->k == VINDEXED) { - BCReg rc = e->u.s.aux; - if ((int32_t)rc < 0) { - ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc); - } else if (rc > BCMAX_C) { - ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1)); - } else { - bcreg_free(fs, rc); - ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc); - } - bcreg_free(fs, e->u.s.info); - } else if (e->k == VCALL) { - e->u.s.info = e->u.s.aux; - e->k = VNONRELOC; - return; - } else if (e->k == VLOCAL) { - e->k = VNONRELOC; - return; - } else { - return; - } - e->u.s.info = bcemit_INS(fs, ins); - e->k = VRELOCABLE; -} - -/* Emit bytecode to set a range of registers to nil. */ -static void bcemit_nil(FuncState *fs, BCReg from, BCReg n) -{ - if (fs->pc > fs->lasttarget) { /* No jumps to current position? */ - BCIns *ip = &fs->bcbase[fs->pc-1].ins; - BCReg pto, pfrom = bc_a(*ip); - switch (bc_op(*ip)) { /* Try to merge with the previous instruction. */ - case BC_KPRI: - if (bc_d(*ip) != ~LJ_TNIL) break; - if (from == pfrom) { - if (n == 1) return; - } else if (from == pfrom+1) { - from = pfrom; - n++; - } else { - break; - } - *ip = BCINS_AD(BC_KNIL, from, from+n-1); /* Replace KPRI. */ - return; - case BC_KNIL: - pto = bc_d(*ip); - if (pfrom <= from && from <= pto+1) { /* Can we connect both ranges? */ - if (from+n-1 > pto) - setbc_d(ip, from+n-1); /* Patch previous instruction range. */ - return; - } - break; - default: - break; - } - } - /* Emit new instruction or replace old instruction. */ - bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) : - BCINS_AD(BC_KNIL, from, from+n-1)); -} - -/* Discharge an expression to a specific register. Ignore branches. */ -static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg) -{ - BCIns ins; - expr_discharge(fs, e); - if (e->k == VKSTR) { - ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e)); - } else if (e->k == VKNUM) { -#if LJ_DUALNUM - cTValue *tv = expr_numtv(e); - if (tvisint(tv) && checki16(intV(tv))) - ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv)); - else -#else - lua_Number n = expr_numberV(e); - int32_t k = lj_num2int(n); - if (checki16(k) && n == (lua_Number)k) - ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k); - else -#endif - ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e)); -#if LJ_HASFFI - } else if (e->k == VKCDATA) { - fs->flags |= PROTO_FFI; - ins = BCINS_AD(BC_KCDATA, reg, - const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA)); -#endif - } else if (e->k == VRELOCABLE) { - setbc_a(bcptr(fs, e), reg); - goto noins; - } else if (e->k == VNONRELOC) { - if (reg == e->u.s.info) - goto noins; - ins = BCINS_AD(BC_MOV, reg, e->u.s.info); - } else if (e->k == VKNIL) { - bcemit_nil(fs, reg, 1); - goto noins; - } else if (e->k <= VKTRUE) { - ins = BCINS_AD(BC_KPRI, reg, const_pri(e)); - } else { - lua_assert(e->k == VVOID || e->k == VJMP); - return; - } - bcemit_INS(fs, ins); -noins: - e->u.s.info = reg; - e->k = VNONRELOC; -} - -/* Forward declaration. */ -static BCPos bcemit_jmp(FuncState *fs); - -/* Discharge an expression to a specific register. */ -static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg) -{ - expr_toreg_nobranch(fs, e, reg); - if (e->k == VJMP) - jmp_append(fs, &e->t, e->u.s.info); /* Add it to the true jump list. */ - if (expr_hasjump(e)) { /* Discharge expression with branches. */ - BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP; - if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) { - BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs); - jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE); - bcemit_AJ(fs, BC_JMP, fs->freereg, 1); - jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE); - jmp_tohere(fs, jval); - } - jend = fs->pc; - fs->lasttarget = jend; - jmp_patchval(fs, e->f, jend, reg, jfalse); - jmp_patchval(fs, e->t, jend, reg, jtrue); - } - e->f = e->t = NO_JMP; - e->u.s.info = reg; - e->k = VNONRELOC; -} - -/* Discharge an expression to the next free register. */ -static void expr_tonextreg(FuncState *fs, ExpDesc *e) -{ - expr_discharge(fs, e); - expr_free(fs, e); - bcreg_reserve(fs, 1); - expr_toreg(fs, e, fs->freereg - 1); -} - -/* Discharge an expression to any register. */ -static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e) -{ - expr_discharge(fs, e); - if (e->k == VNONRELOC) { - if (!expr_hasjump(e)) return e->u.s.info; /* Already in a register. */ - if (e->u.s.info >= fs->nactvar) { - expr_toreg(fs, e, e->u.s.info); /* Discharge to temp. register. */ - return e->u.s.info; - } - } - expr_tonextreg(fs, e); /* Discharge to next register. */ - return e->u.s.info; -} - -/* Partially discharge expression to a value. */ -static void expr_toval(FuncState *fs, ExpDesc *e) -{ - if (expr_hasjump(e)) - expr_toanyreg(fs, e); - else - expr_discharge(fs, e); -} - -/* Emit store for LHS expression. */ -static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e) -{ - BCIns ins; - if (var->k == VLOCAL) { - fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW; - expr_free(fs, e); - expr_toreg(fs, e, var->u.s.info); - return; - } else if (var->k == VUPVAL) { - fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW; - expr_toval(fs, e); - if (e->k <= VKTRUE) - ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e)); - else if (e->k == VKSTR) - ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e)); - else if (e->k == VKNUM) - ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e)); - else - ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e)); - } else if (var->k == VGLOBAL) { - BCReg ra = expr_toanyreg(fs, e); - ins = BCINS_AD(BC_GSET, ra, const_str(fs, var)); - } else { - BCReg ra, rc; - lua_assert(var->k == VINDEXED); - ra = expr_toanyreg(fs, e); - rc = var->u.s.aux; - if ((int32_t)rc < 0) { - ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc); - } else if (rc > BCMAX_C) { - ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1)); - } else { - /* Free late alloced key reg to avoid assert on free of value reg. */ - /* This can only happen when called from expr_table(). */ - lua_assert(e->k != VNONRELOC || ra < fs->nactvar || - rc < ra || (bcreg_free(fs, rc),1)); - ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc); - } - } - bcemit_INS(fs, ins); - expr_free(fs, e); -} - -/* Emit method lookup expression. */ -static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key) -{ - BCReg idx, func, obj = expr_toanyreg(fs, e); - expr_free(fs, e); - func = fs->freereg; - bcemit_AD(fs, BC_MOV, func+1, obj); /* Copy object to first argument. */ - lua_assert(expr_isstrk(key)); - idx = const_str(fs, key); - if (idx <= BCMAX_C) { - bcreg_reserve(fs, 2); - bcemit_ABC(fs, BC_TGETS, func, obj, idx); - } else { - bcreg_reserve(fs, 3); - bcemit_AD(fs, BC_KSTR, func+2, idx); - bcemit_ABC(fs, BC_TGETV, func, obj, func+2); - fs->freereg--; - } - e->u.s.info = func; - e->k = VNONRELOC; -} - -/* -- Bytecode emitter for branches --------------------------------------- */ - -/* Emit unconditional branch. */ -static BCPos bcemit_jmp(FuncState *fs) -{ - BCPos jpc = fs->jpc; - BCPos j = fs->pc - 1; - BCIns *ip = &fs->bcbase[j].ins; - fs->jpc = NO_JMP; - if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO) - setbc_j(ip, NO_JMP); - else - j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP); - jmp_append(fs, &j, jpc); - return j; -} - -/* Invert branch condition of bytecode instruction. */ -static void invertcond(FuncState *fs, ExpDesc *e) -{ - BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins; - setbc_op(ip, bc_op(*ip)^1); -} - -/* Emit conditional branch. */ -static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond) -{ - BCPos pc; - if (e->k == VRELOCABLE) { - BCIns *ip = bcptr(fs, e); - if (bc_op(*ip) == BC_NOT) { - *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip)); - return bcemit_jmp(fs); - } - } - if (e->k != VNONRELOC) { - bcreg_reserve(fs, 1); - expr_toreg_nobranch(fs, e, fs->freereg-1); - } - bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info); - pc = bcemit_jmp(fs); - expr_free(fs, e); - return pc; -} - -/* Emit branch on true condition. */ -static void bcemit_branch_t(FuncState *fs, ExpDesc *e) -{ - BCPos pc; - expr_discharge(fs, e); - if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE) - pc = NO_JMP; /* Never jump. */ - else if (e->k == VJMP) - invertcond(fs, e), pc = e->u.s.info; - else if (e->k == VKFALSE || e->k == VKNIL) - expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs); - else - pc = bcemit_branch(fs, e, 0); - jmp_append(fs, &e->f, pc); - jmp_tohere(fs, e->t); - e->t = NO_JMP; -} - -/* Emit branch on false condition. */ -static void bcemit_branch_f(FuncState *fs, ExpDesc *e) -{ - BCPos pc; - expr_discharge(fs, e); - if (e->k == VKNIL || e->k == VKFALSE) - pc = NO_JMP; /* Never jump. */ - else if (e->k == VJMP) - pc = e->u.s.info; - else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE) - expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs); - else - pc = bcemit_branch(fs, e, 1); - jmp_append(fs, &e->t, pc); - jmp_tohere(fs, e->f); - e->f = NO_JMP; -} - -/* -- Bytecode emitter for operators -------------------------------------- */ - -/* Try constant-folding of arithmetic operators. */ -static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2) -{ - TValue o; - lua_Number n; - if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0; - n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD); - setnumV(&o, n); - if (tvisnan(&o) || tvismzero(&o)) return 0; /* Avoid NaN and -0 as consts. */ - if (LJ_DUALNUM) { - int32_t k = lj_num2int(n); - if ((lua_Number)k == n) { - setintV(&e1->u.nval, k); - return 1; - } - } - setnumV(&e1->u.nval, n); - return 1; -} - -/* Emit arithmetic operator. */ -static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2) -{ - BCReg rb, rc, t; - uint32_t op; - if (foldarith(opr, e1, e2)) - return; - if (opr == OPR_POW) { - op = BC_POW; - rc = expr_toanyreg(fs, e2); - rb = expr_toanyreg(fs, e1); - } else { - op = opr-OPR_ADD+BC_ADDVV; - /* Must discharge 2nd operand first since VINDEXED might free regs. */ - expr_toval(fs, e2); - if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C) - op -= BC_ADDVV-BC_ADDVN; - else - rc = expr_toanyreg(fs, e2); - /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */ - lua_assert(expr_isnumk(e1) || e1->k == VNONRELOC); - expr_toval(fs, e1); - /* Avoid two consts to satisfy bytecode constraints. */ - if (expr_isnumk(e1) && !expr_isnumk(e2) && - (t = const_num(fs, e1)) <= BCMAX_B) { - rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV; - } else { - rb = expr_toanyreg(fs, e1); - } - } - /* Using expr_free might cause asserts if the order is wrong. */ - if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--; - if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--; - e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc); - e1->k = VRELOCABLE; -} - -/* Emit comparison operator. */ -static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2) -{ - ExpDesc *eret = e1; - BCIns ins; - expr_toval(fs, e1); - if (opr == OPR_EQ || opr == OPR_NE) { - BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV; - BCReg ra; - if (expr_isk(e1)) { e1 = e2; e2 = eret; } /* Need constant in 2nd arg. */ - ra = expr_toanyreg(fs, e1); /* First arg must be in a reg. */ - expr_toval(fs, e2); - switch (e2->k) { - case VKNIL: case VKFALSE: case VKTRUE: - ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2)); - break; - case VKSTR: - ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2)); - break; - case VKNUM: - ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2)); - break; - default: - ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2)); - break; - } - } else { - uint32_t op = opr-OPR_LT+BC_ISLT; - BCReg ra, rd; - if ((op-BC_ISLT) & 1) { /* GT -> LT, GE -> LE */ - e1 = e2; e2 = eret; /* Swap operands. */ - op = ((op-BC_ISLT)^3)+BC_ISLT; - expr_toval(fs, e1); - } - rd = expr_toanyreg(fs, e2); - ra = expr_toanyreg(fs, e1); - ins = BCINS_AD(op, ra, rd); - } - /* Using expr_free might cause asserts if the order is wrong. */ - if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--; - if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--; - bcemit_INS(fs, ins); - eret->u.s.info = bcemit_jmp(fs); - eret->k = VJMP; -} - -/* Fixup left side of binary operator. */ -static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e) -{ - if (op == OPR_AND) { - bcemit_branch_t(fs, e); - } else if (op == OPR_OR) { - bcemit_branch_f(fs, e); - } else if (op == OPR_CONCAT) { - expr_tonextreg(fs, e); - } else if (op == OPR_EQ || op == OPR_NE) { - if (!expr_isk_nojump(e)) expr_toanyreg(fs, e); - } else { - if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e); - } -} - -/* Emit binary operator. */ -static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2) -{ - if (op <= OPR_POW) { - bcemit_arith(fs, op, e1, e2); - } else if (op == OPR_AND) { - lua_assert(e1->t == NO_JMP); /* List must be closed. */ - expr_discharge(fs, e2); - jmp_append(fs, &e2->f, e1->f); - *e1 = *e2; - } else if (op == OPR_OR) { - lua_assert(e1->f == NO_JMP); /* List must be closed. */ - expr_discharge(fs, e2); - jmp_append(fs, &e2->t, e1->t); - *e1 = *e2; - } else if (op == OPR_CONCAT) { - expr_toval(fs, e2); - if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) { - lua_assert(e1->u.s.info == bc_b(*bcptr(fs, e2))-1); - expr_free(fs, e1); - setbc_b(bcptr(fs, e2), e1->u.s.info); - e1->u.s.info = e2->u.s.info; - } else { - expr_tonextreg(fs, e2); - expr_free(fs, e2); - expr_free(fs, e1); - e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info); - } - e1->k = VRELOCABLE; - } else { - lua_assert(op == OPR_NE || op == OPR_EQ || - op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT); - bcemit_comp(fs, op, e1, e2); - } -} - -/* Emit unary operator. */ -static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e) -{ - if (op == BC_NOT) { - /* Swap true and false lists. */ - { BCPos temp = e->f; e->f = e->t; e->t = temp; } - jmp_dropval(fs, e->f); - jmp_dropval(fs, e->t); - expr_discharge(fs, e); - if (e->k == VKNIL || e->k == VKFALSE) { - e->k = VKTRUE; - return; - } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) { - e->k = VKFALSE; - return; - } else if (e->k == VJMP) { - invertcond(fs, e); - return; - } else if (e->k == VRELOCABLE) { - bcreg_reserve(fs, 1); - setbc_a(bcptr(fs, e), fs->freereg-1); - e->u.s.info = fs->freereg-1; - e->k = VNONRELOC; - } else { - lua_assert(e->k == VNONRELOC); - } - } else { - lua_assert(op == BC_UNM || op == BC_LEN); - if (op == BC_UNM && !expr_hasjump(e)) { /* Constant-fold negations. */ -#if LJ_HASFFI - if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */ - GCcdata *cd = cdataV(&e->u.nval); - int64_t *p = (int64_t *)cdataptr(cd); - if (cd->ctypeid == CTID_COMPLEX_DOUBLE) - p[1] ^= (int64_t)U64x(80000000,00000000); - else - *p = -*p; - return; - } else -#endif - if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */ - TValue *o = expr_numtv(e); - if (tvisint(o)) { - int32_t k = intV(o); - if (k == -k) - setnumV(o, -(lua_Number)k); - else - setintV(o, -k); - return; - } else { - o->u64 ^= U64x(80000000,00000000); - return; - } - } - } - expr_toanyreg(fs, e); - } - expr_free(fs, e); - e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info); - e->k = VRELOCABLE; -} - -/* -- Lexer support ------------------------------------------------------- */ - -/* Check and consume optional token. */ -static int lex_opt(LexState *ls, LexToken tok) -{ - if (ls->token == tok) { - lj_lex_next(ls); - return 1; - } - return 0; -} - -/* Check and consume token. */ -static void lex_check(LexState *ls, LexToken tok) -{ - if (ls->token != tok) - err_token(ls, tok); - lj_lex_next(ls); -} - -/* Check for matching token. */ -static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line) -{ - if (!lex_opt(ls, what)) { - if (line == ls->linenumber) { - err_token(ls, what); - } else { - const char *swhat = lj_lex_token2str(ls, what); - const char *swho = lj_lex_token2str(ls, who); - lj_lex_error(ls, ls->token, LJ_ERR_XMATCH, swhat, swho, line); - } - } -} - -/* Check for string token. */ -static GCstr *lex_str(LexState *ls) -{ - GCstr *s; - if (ls->token != TK_name && (LJ_52 || ls->token != TK_goto)) - err_token(ls, TK_name); - s = strV(&ls->tokenval); - lj_lex_next(ls); - return s; -} - -/* -- Variable handling --------------------------------------------------- */ - -#define var_get(ls, fs, i) ((ls)->vstack[(fs)->varmap[(i)]]) - -/* Define a new local variable. */ -static void var_new(LexState *ls, BCReg n, GCstr *name) -{ - FuncState *fs = ls->fs; - MSize vtop = ls->vtop; - checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables"); - if (LJ_UNLIKELY(vtop >= ls->sizevstack)) { - if (ls->sizevstack >= LJ_MAX_VSTACK) - lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); - lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); - } - lua_assert((uintptr_t)name < VARNAME__MAX || - lj_tab_getstr(fs->kt, name) != NULL); - /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ - setgcref(ls->vstack[vtop].name, obj2gco(name)); - fs->varmap[fs->nactvar+n] = (uint16_t)vtop; - ls->vtop = vtop+1; -} - -#define var_new_lit(ls, n, v) \ - var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1)) - -#define var_new_fixed(ls, n, vn) \ - var_new(ls, (n), (GCstr *)(uintptr_t)(vn)) - -/* Add local variables. */ -static void var_add(LexState *ls, BCReg nvars) -{ - FuncState *fs = ls->fs; - BCReg nactvar = fs->nactvar; - while (nvars--) { - VarInfo *v = &var_get(ls, fs, nactvar); - v->startpc = fs->pc; - v->slot = nactvar++; - v->info = 0; - } - fs->nactvar = nactvar; -} - -/* Remove local variables. */ -static void var_remove(LexState *ls, BCReg tolevel) -{ - FuncState *fs = ls->fs; - while (fs->nactvar > tolevel) - var_get(ls, fs, --fs->nactvar).endpc = fs->pc; -} - -/* Lookup local variable name. */ -static BCReg var_lookup_local(FuncState *fs, GCstr *n) -{ - int i; - for (i = fs->nactvar-1; i >= 0; i--) { - if (n == strref(var_get(fs->ls, fs, i).name)) - return (BCReg)i; - } - return (BCReg)-1; /* Not found. */ -} - -/* Lookup or add upvalue index. */ -static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e) -{ - MSize i, n = fs->nuv; - for (i = 0; i < n; i++) - if (fs->uvmap[i] == vidx) - return i; /* Already exists. */ - /* Otherwise create a new one. */ - checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues"); - lua_assert(e->k == VLOCAL || e->k == VUPVAL); - fs->uvmap[n] = (uint16_t)vidx; - fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info); - fs->nuv = n+1; - return n; -} - -/* Forward declaration. */ -static void fscope_uvmark(FuncState *fs, BCReg level); - -/* Recursively lookup variables in enclosing functions. */ -static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first) -{ - if (fs) { - BCReg reg = var_lookup_local(fs, name); - if ((int32_t)reg >= 0) { /* Local in this function? */ - expr_init(e, VLOCAL, reg); - if (!first) - fscope_uvmark(fs, reg); /* Scope now has an upvalue. */ - return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]); - } else { - MSize vidx = var_lookup_(fs->prev, name, e, 0); /* Var in outer func? */ - if ((int32_t)vidx >= 0) { /* Yes, make it an upvalue here. */ - e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e); - e->k = VUPVAL; - return vidx; - } - } - } else { /* Not found in any function, must be a global. */ - expr_init(e, VGLOBAL, 0); - e->u.sval = name; - } - return (MSize)-1; /* Global. */ -} - -/* Lookup variable name. */ -#define var_lookup(ls, e) \ - var_lookup_((ls)->fs, lex_str(ls), (e), 1) - -/* -- Goto an label handling ---------------------------------------------- */ - -/* Add a new goto or label. */ -static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc) -{ - FuncState *fs = ls->fs; - MSize vtop = ls->vtop; - if (LJ_UNLIKELY(vtop >= ls->sizevstack)) { - if (ls->sizevstack >= LJ_MAX_VSTACK) - lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); - lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); - } - lua_assert(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL); - /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ - setgcref(ls->vstack[vtop].name, obj2gco(name)); - ls->vstack[vtop].startpc = pc; - ls->vstack[vtop].slot = (uint8_t)fs->nactvar; - ls->vstack[vtop].info = info; - ls->vtop = vtop+1; - return vtop; -} - -#define gola_isgoto(v) ((v)->info & VSTACK_GOTO) -#define gola_islabel(v) ((v)->info & VSTACK_LABEL) -#define gola_isgotolabel(v) ((v)->info & (VSTACK_GOTO|VSTACK_LABEL)) - -/* Patch goto to jump to label. */ -static void gola_patch(LexState *ls, VarInfo *vg, VarInfo *vl) -{ - FuncState *fs = ls->fs; - BCPos pc = vg->startpc; - setgcrefnull(vg->name); /* Invalidate pending goto. */ - setbc_a(&fs->bcbase[pc].ins, vl->slot); - jmp_patch(fs, pc, vl->startpc); -} - -/* Patch goto to close upvalues. */ -static void gola_close(LexState *ls, VarInfo *vg) -{ - FuncState *fs = ls->fs; - BCPos pc = vg->startpc; - BCIns *ip = &fs->bcbase[pc].ins; - lua_assert(gola_isgoto(vg)); - lua_assert(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO); - setbc_a(ip, vg->slot); - if (bc_op(*ip) == BC_JMP) { - BCPos next = jmp_next(fs, pc); - if (next != NO_JMP) jmp_patch(fs, next, pc); /* Jump to UCLO. */ - setbc_op(ip, BC_UCLO); /* Turn into UCLO. */ - setbc_j(ip, NO_JMP); - } -} - -/* Resolve pending forward gotos for label. */ -static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx) -{ - VarInfo *vg = ls->vstack + bl->vstart; - VarInfo *vl = ls->vstack + idx; - for (; vg < vl; vg++) - if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) { - if (vg->slot < vl->slot) { - GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name); - lua_assert((uintptr_t)name >= VARNAME__MAX); - ls->linenumber = ls->fs->bcbase[vg->startpc].line; - lua_assert(strref(vg->name) != NAME_BREAK); - lj_lex_error(ls, 0, LJ_ERR_XGSCOPE, - strdata(strref(vg->name)), strdata(name)); - } - gola_patch(ls, vg, vl); - } -} - -/* Fixup remaining gotos and labels for scope. */ -static void gola_fixup(LexState *ls, FuncScope *bl) -{ - VarInfo *v = ls->vstack + bl->vstart; - VarInfo *ve = ls->vstack + ls->vtop; - for (; v < ve; v++) { - GCstr *name = strref(v->name); - if (name != NULL) { /* Only consider remaining valid gotos/labels. */ - if (gola_islabel(v)) { - VarInfo *vg; - setgcrefnull(v->name); /* Invalidate label that goes out of scope. */ - for (vg = v+1; vg < ve; vg++) /* Resolve pending backward gotos. */ - if (strref(vg->name) == name && gola_isgoto(vg)) { - if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot) - gola_close(ls, vg); - gola_patch(ls, vg, v); - } - } else if (gola_isgoto(v)) { - if (bl->prev) { /* Propagate goto or break to outer scope. */ - bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA; - v->slot = bl->nactvar; - if ((bl->flags & FSCOPE_UPVAL)) - gola_close(ls, v); - } else { /* No outer scope: undefined goto label or no loop. */ - ls->linenumber = ls->fs->bcbase[v->startpc].line; - if (name == NAME_BREAK) - lj_lex_error(ls, 0, LJ_ERR_XBREAK); - else - lj_lex_error(ls, 0, LJ_ERR_XLUNDEF, strdata(name)); - } - } - } - } -} - -/* Find existing label. */ -static VarInfo *gola_findlabel(LexState *ls, GCstr *name) -{ - VarInfo *v = ls->vstack + ls->fs->bl->vstart; - VarInfo *ve = ls->vstack + ls->vtop; - for (; v < ve; v++) - if (strref(v->name) == name && gola_islabel(v)) - return v; - return NULL; -} - -/* -- Scope handling ------------------------------------------------------ */ - -/* Begin a scope. */ -static void fscope_begin(FuncState *fs, FuncScope *bl, int flags) -{ - bl->nactvar = (uint8_t)fs->nactvar; - bl->flags = flags; - bl->vstart = fs->ls->vtop; - bl->prev = fs->bl; - fs->bl = bl; - lua_assert(fs->freereg == fs->nactvar); -} - -/* End a scope. */ -static void fscope_end(FuncState *fs) -{ - FuncScope *bl = fs->bl; - LexState *ls = fs->ls; - fs->bl = bl->prev; - var_remove(ls, bl->nactvar); - fs->freereg = fs->nactvar; - lua_assert(bl->nactvar == fs->nactvar); - if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL) - bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0); - if ((bl->flags & FSCOPE_BREAK)) { - if ((bl->flags & FSCOPE_LOOP)) { - MSize idx = gola_new(ls, NAME_BREAK, VSTACK_LABEL, fs->pc); - ls->vtop = idx; /* Drop break label immediately. */ - gola_resolve(ls, bl, idx); - return; - } /* else: need the fixup step to propagate the breaks. */ - } else if (!(bl->flags & FSCOPE_GOLA)) { - return; - } - gola_fixup(ls, bl); -} - -/* Mark scope as having an upvalue. */ -static void fscope_uvmark(FuncState *fs, BCReg level) -{ - FuncScope *bl; - for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev) - ; - if (bl) - bl->flags |= FSCOPE_UPVAL; -} - -/* -- Function state management ------------------------------------------- */ - -/* Fixup bytecode for prototype. */ -static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n) -{ - BCInsLine *base = fs->bcbase; - MSize i; - pt->sizebc = n; - bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF, - fs->framesize, 0); - for (i = 1; i < n; i++) - bc[i] = base[i].ins; -} - -/* Fixup upvalues for child prototype, step #2. */ -static void fs_fixup_uv2(FuncState *fs, GCproto *pt) -{ - VarInfo *vstack = fs->ls->vstack; - uint16_t *uv = proto_uv(pt); - MSize i, n = pt->sizeuv; - for (i = 0; i < n; i++) { - VarIndex vidx = uv[i]; - if (vidx >= LJ_MAX_VSTACK) - uv[i] = vidx - LJ_MAX_VSTACK; - else if ((vstack[vidx].info & VSTACK_VAR_RW)) - uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL; - else - uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL | PROTO_UV_IMMUTABLE; - } -} - -/* Fixup constants for prototype. */ -static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr) -{ - GCtab *kt; - TValue *array; - Node *node; - MSize i, hmask; - checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants"); - checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants"); - setmref(pt->k, kptr); - pt->sizekn = fs->nkn; - pt->sizekgc = fs->nkgc; - kt = fs->kt; - array = tvref(kt->array); - for (i = 0; i < kt->asize; i++) - if (tvhaskslot(&array[i])) { - TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])]; - if (LJ_DUALNUM) - setintV(tv, (int32_t)i); - else - setnumV(tv, (lua_Number)i); - } - node = noderef(kt->node); - hmask = kt->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (tvhaskslot(&n->val)) { - ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val); - lua_assert(!tvisint(&n->key)); - if (tvisnum(&n->key)) { - TValue *tv = &((TValue *)kptr)[kidx]; - if (LJ_DUALNUM) { - lua_Number nn = numV(&n->key); - int32_t k = lj_num2int(nn); - lua_assert(!tvismzero(&n->key)); - if ((lua_Number)k == nn) - setintV(tv, k); - else - *tv = n->key; - } else { - *tv = n->key; - } - } else { - GCobj *o = gcV(&n->key); - setgcref(((GCRef *)kptr)[~kidx], o); - lj_gc_objbarrier(fs->L, pt, o); - if (tvisproto(&n->key)) - fs_fixup_uv2(fs, gco2pt(o)); - } - } - } -} - -/* Fixup upvalues for prototype, step #1. */ -static void fs_fixup_uv1(FuncState *fs, GCproto *pt, uint16_t *uv) -{ - setmref(pt->uv, uv); - pt->sizeuv = fs->nuv; - memcpy(uv, fs->uvtmp, fs->nuv*sizeof(VarIndex)); -} - -#ifndef LUAJIT_DISABLE_DEBUGINFO -/* Prepare lineinfo for prototype. */ -static size_t fs_prep_line(FuncState *fs, BCLine numline) -{ - return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2); -} - -/* Fixup lineinfo for prototype. */ -static void fs_fixup_line(FuncState *fs, GCproto *pt, - void *lineinfo, BCLine numline) -{ - BCInsLine *base = fs->bcbase + 1; - BCLine first = fs->linedefined; - MSize i = 0, n = fs->pc-1; - pt->firstline = fs->linedefined; - pt->numline = numline; - setmref(pt->lineinfo, lineinfo); - if (LJ_LIKELY(numline < 256)) { - uint8_t *li = (uint8_t *)lineinfo; - do { - BCLine delta = base[i].line - first; - lua_assert(delta >= 0 && delta < 256); - li[i] = (uint8_t)delta; - } while (++i < n); - } else if (LJ_LIKELY(numline < 65536)) { - uint16_t *li = (uint16_t *)lineinfo; - do { - BCLine delta = base[i].line - first; - lua_assert(delta >= 0 && delta < 65536); - li[i] = (uint16_t)delta; - } while (++i < n); - } else { - uint32_t *li = (uint32_t *)lineinfo; - do { - BCLine delta = base[i].line - first; - lua_assert(delta >= 0); - li[i] = (uint32_t)delta; - } while (++i < n); - } -} - -/* Resize buffer if needed. */ -static LJ_NOINLINE void fs_buf_resize(LexState *ls, MSize len) -{ - MSize sz = ls->sb.sz * 2; - while (ls->sb.n + len > sz) sz = sz * 2; - lj_str_resizebuf(ls->L, &ls->sb, sz); -} - -static LJ_AINLINE void fs_buf_need(LexState *ls, MSize len) -{ - if (LJ_UNLIKELY(ls->sb.n + len > ls->sb.sz)) - fs_buf_resize(ls, len); -} - -/* Add string to buffer. */ -static void fs_buf_str(LexState *ls, const char *str, MSize len) -{ - char *p = ls->sb.buf + ls->sb.n; - MSize i; - ls->sb.n += len; - for (i = 0; i < len; i++) p[i] = str[i]; -} - -/* Add ULEB128 value to buffer. */ -static void fs_buf_uleb128(LexState *ls, uint32_t v) -{ - MSize n = ls->sb.n; - uint8_t *p = (uint8_t *)ls->sb.buf; - for (; v >= 0x80; v >>= 7) - p[n++] = (uint8_t)((v & 0x7f) | 0x80); - p[n++] = (uint8_t)v; - ls->sb.n = n; -} - -/* Prepare variable info for prototype. */ -static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar) -{ - VarInfo *vs =ls->vstack, *ve; - MSize i, n; - BCPos lastpc; - lj_str_resetbuf(&ls->sb); /* Copy to temp. string buffer. */ - /* Store upvalue names. */ - for (i = 0, n = fs->nuv; i < n; i++) { - GCstr *s = strref(vs[fs->uvmap[i]].name); - MSize len = s->len+1; - fs_buf_need(ls, len); - fs_buf_str(ls, strdata(s), len); - } - *ofsvar = ls->sb.n; - lastpc = 0; - /* Store local variable names and compressed ranges. */ - for (ve = vs + ls->vtop, vs += fs->vbase; vs < ve; vs++) { - if (!gola_isgotolabel(vs)) { - GCstr *s = strref(vs->name); - BCPos startpc; - if ((uintptr_t)s < VARNAME__MAX) { - fs_buf_need(ls, 1 + 2*5); - ls->sb.buf[ls->sb.n++] = (uint8_t)(uintptr_t)s; - } else { - MSize len = s->len+1; - fs_buf_need(ls, len + 2*5); - fs_buf_str(ls, strdata(s), len); - } - startpc = vs->startpc; - fs_buf_uleb128(ls, startpc-lastpc); - fs_buf_uleb128(ls, vs->endpc-startpc); - lastpc = startpc; - } - } - fs_buf_need(ls, 1); - ls->sb.buf[ls->sb.n++] = '\0'; /* Terminator for varinfo. */ - return ls->sb.n; -} - -/* Fixup variable info for prototype. */ -static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar) -{ - setmref(pt->uvinfo, p); - setmref(pt->varinfo, (char *)p + ofsvar); - memcpy(p, ls->sb.buf, ls->sb.n); /* Copy from temp. string buffer. */ -} -#else - -/* Initialize with empty debug info, if disabled. */ -#define fs_prep_line(fs, numline) (UNUSED(numline), 0) -#define fs_fixup_line(fs, pt, li, numline) \ - pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL) -#define fs_prep_var(ls, fs, ofsvar) (UNUSED(ofsvar), 0) -#define fs_fixup_var(ls, pt, p, ofsvar) \ - setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL) - -#endif - -/* Check if bytecode op returns. */ -static int bcopisret(BCOp op) -{ - switch (op) { - case BC_CALLMT: case BC_CALLT: - case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: - return 1; - default: - return 0; - } -} - -/* Fixup return instruction for prototype. */ -static void fs_fixup_ret(FuncState *fs) -{ - BCPos lastpc = fs->pc; - if (lastpc <= fs->lasttarget || !bcopisret(bc_op(fs->bcbase[lastpc-1].ins))) { - if ((fs->bl->flags & FSCOPE_UPVAL)) - bcemit_AJ(fs, BC_UCLO, 0, 0); - bcemit_AD(fs, BC_RET0, 0, 1); /* Need final return. */ - } - fs->bl->flags |= FSCOPE_NOCLOSE; /* Handled above. */ - fscope_end(fs); - lua_assert(fs->bl == NULL); - /* May need to fixup returns encoded before first function was created. */ - if (fs->flags & PROTO_FIXUP_RETURN) { - BCPos pc; - for (pc = 1; pc < lastpc; pc++) { - BCIns ins = fs->bcbase[pc].ins; - BCPos offset; - switch (bc_op(ins)) { - case BC_CALLMT: case BC_CALLT: - case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: - offset = bcemit_INS(fs, ins)-(pc+1)+BCBIAS_J; /* Copy return ins. */ - if (offset > BCMAX_D) - err_syntax(fs->ls, LJ_ERR_XFIXUP); - /* Replace with UCLO plus branch. */ - fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset); - break; - case BC_UCLO: - return; /* We're done. */ - default: - break; - } - } - } -} - -/* Finish a FuncState and return the new prototype. */ -static GCproto *fs_finish(LexState *ls, BCLine line) -{ - lua_State *L = ls->L; - FuncState *fs = ls->fs; - BCLine numline = line - fs->linedefined; - size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar; - GCproto *pt; - - /* Apply final fixups. */ - fs_fixup_ret(fs); - - /* Calculate total size of prototype including all colocated arrays. */ - sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef); - sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1); - ofsk = sizept; sizept += fs->nkn*sizeof(TValue); - ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2; - ofsli = sizept; sizept += fs_prep_line(fs, numline); - ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar); - - /* Allocate prototype and initialize its fields. */ - pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept); - pt->gct = ~LJ_TPROTO; - pt->sizept = (MSize)sizept; - pt->trace = 0; - pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN)); - pt->numparams = fs->numparams; - pt->framesize = fs->framesize; - setgcref(pt->chunkname, obj2gco(ls->chunkname)); - - /* Close potentially uninitialized gap between bc and kgc. */ - *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0; - fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc); - fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk)); - fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv)); - fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline); - fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar); - - lj_vmevent_send(L, BC, - setprotoV(L, L->top++, pt); - ); - - L->top--; /* Pop table of constants. */ - ls->vtop = fs->vbase; /* Reset variable stack. */ - ls->fs = fs->prev; - lua_assert(ls->fs != NULL || ls->token == TK_eof); - return pt; -} - -/* Initialize a new FuncState. */ -static void fs_init(LexState *ls, FuncState *fs) -{ - lua_State *L = ls->L; - fs->prev = ls->fs; ls->fs = fs; /* Append to list. */ - fs->ls = ls; - fs->vbase = ls->vtop; - fs->L = L; - fs->pc = 0; - fs->lasttarget = 0; - fs->jpc = NO_JMP; - fs->freereg = 0; - fs->nkgc = 0; - fs->nkn = 0; - fs->nactvar = 0; - fs->nuv = 0; - fs->bl = NULL; - fs->flags = 0; - fs->framesize = 1; /* Minimum frame size. */ - fs->kt = lj_tab_new(L, 0, 0); - /* Anchor table of constants in stack to avoid being collected. */ - settabV(L, L->top, fs->kt); - incr_top(L); -} - -/* -- Expressions --------------------------------------------------------- */ - -/* Forward declaration. */ -static void expr(LexState *ls, ExpDesc *v); - -/* Return string expression. */ -static void expr_str(LexState *ls, ExpDesc *e) -{ - expr_init(e, VKSTR, 0); - e->u.sval = lex_str(ls); -} - -/* Return index expression. */ -static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e) -{ - /* Already called: expr_toval(fs, e). */ - t->k = VINDEXED; - if (expr_isnumk(e)) { -#if LJ_DUALNUM - if (tvisint(expr_numtv(e))) { - int32_t k = intV(expr_numtv(e)); - if (checku8(k)) { - t->u.s.aux = BCMAX_C+1+(uint32_t)k; /* 256..511: const byte key */ - return; - } - } -#else - lua_Number n = expr_numberV(e); - int32_t k = lj_num2int(n); - if (checku8(k) && n == (lua_Number)k) { - t->u.s.aux = BCMAX_C+1+(uint32_t)k; /* 256..511: const byte key */ - return; - } -#endif - } else if (expr_isstrk(e)) { - BCReg idx = const_str(fs, e); - if (idx <= BCMAX_C) { - t->u.s.aux = ~idx; /* -256..-1: const string key */ - return; - } - } - t->u.s.aux = expr_toanyreg(fs, e); /* 0..255: register */ -} - -/* Parse index expression with named field. */ -static void expr_field(LexState *ls, ExpDesc *v) -{ - FuncState *fs = ls->fs; - ExpDesc key; - expr_toanyreg(fs, v); - lj_lex_next(ls); /* Skip dot or colon. */ - expr_str(ls, &key); - expr_index(fs, v, &key); -} - -/* Parse index expression with brackets. */ -static void expr_bracket(LexState *ls, ExpDesc *v) -{ - lj_lex_next(ls); /* Skip '['. */ - expr(ls, v); - expr_toval(ls->fs, v); - lex_check(ls, ']'); -} - -/* Get value of constant expression. */ -static void expr_kvalue(TValue *v, ExpDesc *e) -{ - if (e->k <= VKTRUE) { - setitype(v, ~(uint32_t)e->k); - } else if (e->k == VKSTR) { - setgcref(v->gcr, obj2gco(e->u.sval)); - setitype(v, LJ_TSTR); - } else { - lua_assert(tvisnumber(expr_numtv(e))); - *v = *expr_numtv(e); - } -} - -/* Parse table constructor expression. */ -static void expr_table(LexState *ls, ExpDesc *e) -{ - FuncState *fs = ls->fs; - BCLine line = ls->linenumber; - GCtab *t = NULL; - int vcall = 0, needarr = 0, fixt = 0; - uint32_t narr = 1; /* First array index. */ - uint32_t nhash = 0; /* Number of hash entries. */ - BCReg freg = fs->freereg; - BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0); - expr_init(e, VNONRELOC, freg); - bcreg_reserve(fs, 1); - freg++; - lex_check(ls, '{'); - while (ls->token != '}') { - ExpDesc key, val; - vcall = 0; - if (ls->token == '[') { - expr_bracket(ls, &key); /* Already calls expr_toval. */ - if (!expr_isk(&key)) expr_index(fs, e, &key); - if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++; - lex_check(ls, '='); - } else if ((ls->token == TK_name || (!LJ_52 && ls->token == TK_goto)) && - lj_lex_lookahead(ls) == '=') { - expr_str(ls, &key); - lex_check(ls, '='); - nhash++; - } else { - expr_init(&key, VKNUM, 0); - setintV(&key.u.nval, (int)narr); - narr++; - needarr = vcall = 1; - } - expr(ls, &val); - if (expr_isk(&key) && key.k != VKNIL && - (key.k == VKSTR || expr_isk_nojump(&val))) { - TValue k, *v; - if (!t) { /* Create template table on demand. */ - BCReg kidx; - t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash)); - kidx = const_gc(fs, obj2gco(t), LJ_TTAB); - fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx); - } - vcall = 0; - expr_kvalue(&k, &key); - v = lj_tab_set(fs->L, t, &k); - lj_gc_anybarriert(fs->L, t); - if (expr_isk_nojump(&val)) { /* Add const key/value to template table. */ - expr_kvalue(v, &val); - } else { /* Otherwise create dummy string key (avoids lj_tab_newkey). */ - settabV(fs->L, v, t); /* Preserve key with table itself as value. */ - fixt = 1; /* Fix this later, after all resizes. */ - goto nonconst; - } - } else { - nonconst: - if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; } - if (expr_isk(&key)) expr_index(fs, e, &key); - bcemit_store(fs, e, &val); - } - fs->freereg = freg; - if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break; - } - lex_match(ls, '}', '{', line); - if (vcall) { - BCInsLine *ilp = &fs->bcbase[fs->pc-1]; - ExpDesc en; - lua_assert(bc_a(ilp->ins) == freg && - bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB)); - expr_init(&en, VKNUM, 0); - en.u.nval.u32.lo = narr-1; - en.u.nval.u32.hi = 0x43300000; /* Biased integer to avoid denormals. */ - if (narr > 256) { fs->pc--; ilp--; } - ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en)); - setbc_b(&ilp[-1].ins, 0); - } - if (pc == fs->pc-1) { /* Make expr relocable if possible. */ - e->u.s.info = pc; - fs->freereg--; - e->k = VRELOCABLE; - } else { - e->k = VNONRELOC; /* May have been changed by expr_index. */ - } - if (!t) { /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */ - BCIns *ip = &fs->bcbase[pc].ins; - if (!needarr) narr = 0; - else if (narr < 3) narr = 3; - else if (narr > 0x7ff) narr = 0x7ff; - setbc_d(ip, narr|(hsize2hbits(nhash)<<11)); - } else { - if (needarr && t->asize < narr) - lj_tab_reasize(fs->L, t, narr-1); - if (fixt) { /* Fix value for dummy keys in template table. */ - Node *node = noderef(t->node); - uint32_t i, hmask = t->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (tvistab(&n->val)) { - lua_assert(tabV(&n->val) == t); - setnilV(&n->val); /* Turn value into nil. */ - } - } - } - lj_gc_check(fs->L); - } -} - -/* Parse function parameters. */ -static BCReg parse_params(LexState *ls, int needself) -{ - FuncState *fs = ls->fs; - BCReg nparams = 0; - lex_check(ls, '('); - if (needself) - var_new_lit(ls, nparams++, "self"); - if (ls->token != ')') { - do { - if (ls->token == TK_name || (!LJ_52 && ls->token == TK_goto)) { - var_new(ls, nparams++, lex_str(ls)); - } else if (ls->token == TK_dots) { - lj_lex_next(ls); - fs->flags |= PROTO_VARARG; - break; - } else { - err_syntax(ls, LJ_ERR_XPARAM); - } - } while (lex_opt(ls, ',')); - } - var_add(ls, nparams); - lua_assert(fs->nactvar == nparams); - bcreg_reserve(fs, nparams); - lex_check(ls, ')'); - return nparams; -} - -/* Forward declaration. */ -static void parse_chunk(LexState *ls); - -/* Parse body of a function. */ -static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line) -{ - FuncState fs, *pfs = ls->fs; - FuncScope bl; - GCproto *pt; - ptrdiff_t oldbase = pfs->bcbase - ls->bcstack; - fs_init(ls, &fs); - fscope_begin(&fs, &bl, 0); - fs.linedefined = line; - fs.numparams = (uint8_t)parse_params(ls, needself); - fs.bcbase = pfs->bcbase + pfs->pc; - fs.bclim = pfs->bclim - pfs->pc; - bcemit_AD(&fs, BC_FUNCF, 0, 0); /* Placeholder. */ - parse_chunk(ls); - if (ls->token != TK_end) lex_match(ls, TK_end, TK_function, line); - pt = fs_finish(ls, (ls->lastline = ls->linenumber)); - pfs->bcbase = ls->bcstack + oldbase; /* May have been reallocated. */ - pfs->bclim = (BCPos)(ls->sizebcstack - oldbase); - /* Store new prototype in the constant array of the parent. */ - expr_init(e, VRELOCABLE, - bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO))); -#if LJ_HASFFI - pfs->flags |= (fs.flags & PROTO_FFI); -#endif - if (!(pfs->flags & PROTO_CHILD)) { - if (pfs->flags & PROTO_HAS_RETURN) - pfs->flags |= PROTO_FIXUP_RETURN; - pfs->flags |= PROTO_CHILD; - } - lj_lex_next(ls); -} - -/* Parse expression list. Last expression is left open. */ -static BCReg expr_list(LexState *ls, ExpDesc *v) -{ - BCReg n = 1; - expr(ls, v); - while (lex_opt(ls, ',')) { - expr_tonextreg(ls->fs, v); - expr(ls, v); - n++; - } - return n; -} - -/* Parse function argument list. */ -static void parse_args(LexState *ls, ExpDesc *e) -{ - FuncState *fs = ls->fs; - ExpDesc args; - BCIns ins; - BCReg base; - BCLine line = ls->linenumber; - if (ls->token == '(') { -#if !LJ_52 - if (line != ls->lastline) - err_syntax(ls, LJ_ERR_XAMBIG); -#endif - lj_lex_next(ls); - if (ls->token == ')') { /* f(). */ - args.k = VVOID; - } else { - expr_list(ls, &args); - if (args.k == VCALL) /* f(a, b, g()) or f(a, b, ...). */ - setbc_b(bcptr(fs, &args), 0); /* Pass on multiple results. */ - } - lex_match(ls, ')', '(', line); - } else if (ls->token == '{') { - expr_table(ls, &args); - } else if (ls->token == TK_string) { - expr_init(&args, VKSTR, 0); - args.u.sval = strV(&ls->tokenval); - lj_lex_next(ls); - } else { - err_syntax(ls, LJ_ERR_XFUNARG); - return; /* Silence compiler. */ - } - lua_assert(e->k == VNONRELOC); - base = e->u.s.info; /* Base register for call. */ - if (args.k == VCALL) { - ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1); - } else { - if (args.k != VVOID) - expr_tonextreg(fs, &args); - ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base); - } - expr_init(e, VCALL, bcemit_INS(fs, ins)); - e->u.s.aux = base; - fs->bcbase[fs->pc - 1].line = line; - fs->freereg = base+1; /* Leave one result by default. */ -} - -/* Parse primary expression. */ -static void expr_primary(LexState *ls, ExpDesc *v) -{ - FuncState *fs = ls->fs; - /* Parse prefix expression. */ - if (ls->token == '(') { - BCLine line = ls->linenumber; - lj_lex_next(ls); - expr(ls, v); - lex_match(ls, ')', '(', line); - expr_discharge(ls->fs, v); - } else if (ls->token == TK_name || (!LJ_52 && ls->token == TK_goto)) { - var_lookup(ls, v); - } else { - err_syntax(ls, LJ_ERR_XSYMBOL); - } - for (;;) { /* Parse multiple expression suffixes. */ - if (ls->token == '.') { - expr_field(ls, v); - } else if (ls->token == '[') { - ExpDesc key; - expr_toanyreg(fs, v); - expr_bracket(ls, &key); - expr_index(fs, v, &key); - } else if (ls->token == ':') { - ExpDesc key; - lj_lex_next(ls); - expr_str(ls, &key); - bcemit_method(fs, v, &key); - parse_args(ls, v); - } else if (ls->token == '(' || ls->token == TK_string || ls->token == '{') { - expr_tonextreg(fs, v); - parse_args(ls, v); - } else { - break; - } - } -} - -/* Parse simple expression. */ -static void expr_simple(LexState *ls, ExpDesc *v) -{ - switch (ls->token) { - case TK_number: - expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokenval)) ? VKCDATA : VKNUM, 0); - copyTV(ls->L, &v->u.nval, &ls->tokenval); - break; - case TK_string: - expr_init(v, VKSTR, 0); - v->u.sval = strV(&ls->tokenval); - break; - case TK_nil: - expr_init(v, VKNIL, 0); - break; - case TK_true: - expr_init(v, VKTRUE, 0); - break; - case TK_false: - expr_init(v, VKFALSE, 0); - break; - case TK_dots: { /* Vararg. */ - FuncState *fs = ls->fs; - BCReg base; - checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS); - bcreg_reserve(fs, 1); - base = fs->freereg-1; - expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams)); - v->u.s.aux = base; - break; - } - case '{': /* Table constructor. */ - expr_table(ls, v); - return; - case TK_function: - lj_lex_next(ls); - parse_body(ls, v, 0, ls->linenumber); - return; - default: - expr_primary(ls, v); - return; - } - lj_lex_next(ls); -} - -/* Manage syntactic levels to avoid blowing up the stack. */ -static void synlevel_begin(LexState *ls) -{ - if (++ls->level >= LJ_MAX_XLEVEL) - lj_lex_error(ls, 0, LJ_ERR_XLEVELS); -} - -#define synlevel_end(ls) ((ls)->level--) - -/* Convert token to binary operator. */ -static BinOpr token2binop(LexToken tok) -{ - switch (tok) { - case '+': return OPR_ADD; - case '-': return OPR_SUB; - case '*': return OPR_MUL; - case '/': return OPR_DIV; - case '%': return OPR_MOD; - case '^': return OPR_POW; - case TK_concat: return OPR_CONCAT; - case TK_ne: return OPR_NE; - case TK_eq: return OPR_EQ; - case '<': return OPR_LT; - case TK_le: return OPR_LE; - case '>': return OPR_GT; - case TK_ge: return OPR_GE; - case TK_and: return OPR_AND; - case TK_or: return OPR_OR; - default: return OPR_NOBINOPR; - } -} - -/* Priorities for each binary operator. ORDER OPR. */ -static const struct { - uint8_t left; /* Left priority. */ - uint8_t right; /* Right priority. */ -} priority[] = { - {6,6}, {6,6}, {7,7}, {7,7}, {7,7}, /* ADD SUB MUL DIV MOD */ - {10,9}, {5,4}, /* POW CONCAT (right associative) */ - {3,3}, {3,3}, /* EQ NE */ - {3,3}, {3,3}, {3,3}, {3,3}, /* LT GE GT LE */ - {2,2}, {1,1} /* AND OR */ -}; - -#define UNARY_PRIORITY 8 /* Priority for unary operators. */ - -/* Forward declaration. */ -static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit); - -/* Parse unary expression. */ -static void expr_unop(LexState *ls, ExpDesc *v) -{ - BCOp op; - if (ls->token == TK_not) { - op = BC_NOT; - } else if (ls->token == '-') { - op = BC_UNM; - } else if (ls->token == '#') { - op = BC_LEN; - } else { - expr_simple(ls, v); - return; - } - lj_lex_next(ls); - expr_binop(ls, v, UNARY_PRIORITY); - bcemit_unop(ls->fs, op, v); -} - -/* Parse binary expressions with priority higher than the limit. */ -static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit) -{ - BinOpr op; - synlevel_begin(ls); - expr_unop(ls, v); - op = token2binop(ls->token); - while (op != OPR_NOBINOPR && priority[op].left > limit) { - ExpDesc v2; - BinOpr nextop; - lj_lex_next(ls); - bcemit_binop_left(ls->fs, op, v); - /* Parse binary expression with higher priority. */ - nextop = expr_binop(ls, &v2, priority[op].right); - bcemit_binop(ls->fs, op, v, &v2); - op = nextop; - } - synlevel_end(ls); - return op; /* Return unconsumed binary operator (if any). */ -} - -/* Parse expression. */ -static void expr(LexState *ls, ExpDesc *v) -{ - expr_binop(ls, v, 0); /* Priority 0: parse whole expression. */ -} - -/* Assign expression to the next register. */ -static void expr_next(LexState *ls) -{ - ExpDesc e; - expr(ls, &e); - expr_tonextreg(ls->fs, &e); -} - -/* Parse conditional expression. */ -static BCPos expr_cond(LexState *ls) -{ - ExpDesc v; - expr(ls, &v); - if (v.k == VKNIL) v.k = VKFALSE; - bcemit_branch_t(ls->fs, &v); - return v.f; -} - -/* -- Assignments --------------------------------------------------------- */ - -/* List of LHS variables. */ -typedef struct LHSVarList { - ExpDesc v; /* LHS variable. */ - struct LHSVarList *prev; /* Link to previous LHS variable. */ -} LHSVarList; - -/* Eliminate write-after-read hazards for local variable assignment. */ -static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v) -{ - FuncState *fs = ls->fs; - BCReg reg = v->u.s.info; /* Check against this variable. */ - BCReg tmp = fs->freereg; /* Rename to this temp. register (if needed). */ - int hazard = 0; - for (; lh; lh = lh->prev) { - if (lh->v.k == VINDEXED) { - if (lh->v.u.s.info == reg) { /* t[i], t = 1, 2 */ - hazard = 1; - lh->v.u.s.info = tmp; - } - if (lh->v.u.s.aux == reg) { /* t[i], i = 1, 2 */ - hazard = 1; - lh->v.u.s.aux = tmp; - } - } - } - if (hazard) { - bcemit_AD(fs, BC_MOV, tmp, reg); /* Rename conflicting variable. */ - bcreg_reserve(fs, 1); - } -} - -/* Adjust LHS/RHS of an assignment. */ -static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e) -{ - FuncState *fs = ls->fs; - int32_t extra = (int32_t)nvars - (int32_t)nexps; - if (e->k == VCALL) { - extra++; /* Compensate for the VCALL itself. */ - if (extra < 0) extra = 0; - setbc_b(bcptr(fs, e), extra+1); /* Fixup call results. */ - if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1); - } else { - if (e->k != VVOID) - expr_tonextreg(fs, e); /* Close last expression. */ - if (extra > 0) { /* Leftover LHS are set to nil. */ - BCReg reg = fs->freereg; - bcreg_reserve(fs, (BCReg)extra); - bcemit_nil(fs, reg, (BCReg)extra); - } - } -} - -/* Recursively parse assignment statement. */ -static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars) -{ - ExpDesc e; - checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX); - if (lex_opt(ls, ',')) { /* Collect LHS list and recurse upwards. */ - LHSVarList vl; - vl.prev = lh; - expr_primary(ls, &vl.v); - if (vl.v.k == VLOCAL) - assign_hazard(ls, lh, &vl.v); - checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names"); - parse_assignment(ls, &vl, nvars+1); - } else { /* Parse RHS. */ - BCReg nexps; - lex_check(ls, '='); - nexps = expr_list(ls, &e); - if (nexps == nvars) { - if (e.k == VCALL) { - if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) { /* Vararg assignment. */ - ls->fs->freereg--; - e.k = VRELOCABLE; - } else { /* Multiple call results. */ - e.u.s.info = e.u.s.aux; /* Base of call is not relocatable. */ - e.k = VNONRELOC; - } - } - bcemit_store(ls->fs, &lh->v, &e); - return; - } - assign_adjust(ls, nvars, nexps, &e); - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* Drop leftover regs. */ - } - /* Assign RHS to LHS and recurse downwards. */ - expr_init(&e, VNONRELOC, ls->fs->freereg-1); - bcemit_store(ls->fs, &lh->v, &e); -} - -/* Parse call statement or assignment. */ -static void parse_call_assign(LexState *ls) -{ - FuncState *fs = ls->fs; - LHSVarList vl; - expr_primary(ls, &vl.v); - if (vl.v.k == VCALL) { /* Function call statement. */ - setbc_b(bcptr(fs, &vl.v), 1); /* No results. */ - } else { /* Start of an assignment. */ - vl.prev = NULL; - parse_assignment(ls, &vl, 1); - } -} - -/* Parse 'local' statement. */ -static void parse_local(LexState *ls) -{ - if (lex_opt(ls, TK_function)) { /* Local function declaration. */ - ExpDesc v, b; - FuncState *fs = ls->fs; - var_new(ls, 0, lex_str(ls)); - expr_init(&v, VLOCAL, fs->freereg); - v.u.s.aux = fs->varmap[fs->freereg]; - bcreg_reserve(fs, 1); - var_add(ls, 1); - parse_body(ls, &b, 0, ls->linenumber); - /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */ - expr_free(fs, &b); - expr_toreg(fs, &b, v.u.s.info); - /* The upvalue is in scope, but the local is only valid after the store. */ - var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc; - } else { /* Local variable declaration. */ - ExpDesc e; - BCReg nexps, nvars = 0; - do { /* Collect LHS. */ - var_new(ls, nvars++, lex_str(ls)); - } while (lex_opt(ls, ',')); - if (lex_opt(ls, '=')) { /* Optional RHS. */ - nexps = expr_list(ls, &e); - } else { /* Or implicitly set to nil. */ - e.k = VVOID; - nexps = 0; - } - assign_adjust(ls, nvars, nexps, &e); - var_add(ls, nvars); - } -} - -/* Parse 'function' statement. */ -static void parse_func(LexState *ls, BCLine line) -{ - FuncState *fs; - ExpDesc v, b; - int needself = 0; - lj_lex_next(ls); /* Skip 'function'. */ - /* Parse function name. */ - var_lookup(ls, &v); - while (ls->token == '.') /* Multiple dot-separated fields. */ - expr_field(ls, &v); - if (ls->token == ':') { /* Optional colon to signify method call. */ - needself = 1; - expr_field(ls, &v); - } - parse_body(ls, &b, needself, line); - fs = ls->fs; - bcemit_store(fs, &v, &b); - fs->bcbase[fs->pc - 1].line = line; /* Set line for the store. */ -} - -/* -- Control transfer statements ----------------------------------------- */ - -/* Check for end of block. */ -static int endofblock(LexToken token) -{ - switch (token) { - case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof: - return 1; - default: - return 0; - } -} - -/* Parse 'return' statement. */ -static void parse_return(LexState *ls) -{ - BCIns ins; - FuncState *fs = ls->fs; - lj_lex_next(ls); /* Skip 'return'. */ - fs->flags |= PROTO_HAS_RETURN; - if (endofblock(ls->token) || ls->token == ';') { /* Bare return. */ - ins = BCINS_AD(BC_RET0, 0, 1); - } else { /* Return with one or more values. */ - ExpDesc e; /* Receives the _last_ expression in the list. */ - BCReg nret = expr_list(ls, &e); - if (nret == 1) { /* Return one result. */ - if (e.k == VCALL) { /* Check for tail call. */ - BCIns *ip = bcptr(fs, &e); - /* It doesn't pay off to add BC_VARGT just for 'return ...'. */ - if (bc_op(*ip) == BC_VARG) goto notailcall; - fs->pc--; - ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip)); - } else { /* Can return the result from any register. */ - ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2); - } - } else { - if (e.k == VCALL) { /* Append all results from a call. */ - notailcall: - setbc_b(bcptr(fs, &e), 0); - ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar); - } else { - expr_tonextreg(fs, &e); /* Force contiguous registers. */ - ins = BCINS_AD(BC_RET, fs->nactvar, nret+1); - } - } - } - if (fs->flags & PROTO_CHILD) - bcemit_AJ(fs, BC_UCLO, 0, 0); /* May need to close upvalues first. */ - bcemit_INS(fs, ins); -} - -/* Parse 'break' statement. */ -static void parse_break(LexState *ls) -{ - ls->fs->bl->flags |= FSCOPE_BREAK; - gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs)); -} - -/* Parse 'goto' statement. */ -static void parse_goto(LexState *ls) -{ - FuncState *fs = ls->fs; - GCstr *name = lex_str(ls); - VarInfo *vl = gola_findlabel(ls, name); - if (vl) /* Treat backwards goto within same scope like a loop. */ - bcemit_AJ(fs, BC_LOOP, vl->slot, -1); /* No BC range check. */ - fs->bl->flags |= FSCOPE_GOLA; - gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs)); -} - -/* Parse label. */ -static void parse_label(LexState *ls) -{ - FuncState *fs = ls->fs; - GCstr *name; - MSize idx; - fs->lasttarget = fs->pc; - fs->bl->flags |= FSCOPE_GOLA; - lj_lex_next(ls); /* Skip '::'. */ - name = lex_str(ls); - if (gola_findlabel(ls, name)) - lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name)); - idx = gola_new(ls, name, VSTACK_LABEL, fs->pc); - lex_check(ls, TK_label); - /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */ - for (;;) { - if (ls->token == TK_label) { - synlevel_begin(ls); - parse_label(ls); - synlevel_end(ls); - } else if (LJ_52 && ls->token == ';') { - lj_lex_next(ls); - } else { - break; - } - } - /* Trailing label is considered to be outside of scope. */ - if (endofblock(ls->token) && ls->token != TK_until) - ls->vstack[idx].slot = fs->bl->nactvar; - gola_resolve(ls, fs->bl, idx); -} - -/* -- Blocks, loops and conditional statements ---------------------------- */ - -/* Parse a block. */ -static void parse_block(LexState *ls) -{ - FuncState *fs = ls->fs; - FuncScope bl; - fscope_begin(fs, &bl, 0); - parse_chunk(ls); - fscope_end(fs); -} - -/* Parse 'while' statement. */ -static void parse_while(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - BCPos start, loop, condexit; - FuncScope bl; - lj_lex_next(ls); /* Skip 'while'. */ - start = fs->lasttarget = fs->pc; - condexit = expr_cond(ls); - fscope_begin(fs, &bl, FSCOPE_LOOP); - lex_check(ls, TK_do); - loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0); - parse_block(ls); - jmp_patch(fs, bcemit_jmp(fs), start); - lex_match(ls, TK_end, TK_while, line); - fscope_end(fs); - jmp_tohere(fs, condexit); - jmp_patchins(fs, loop, fs->pc); -} - -/* Parse 'repeat' statement. */ -static void parse_repeat(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - BCPos loop = fs->lasttarget = fs->pc; - BCPos condexit; - FuncScope bl1, bl2; - fscope_begin(fs, &bl1, FSCOPE_LOOP); /* Breakable loop scope. */ - fscope_begin(fs, &bl2, 0); /* Inner scope. */ - lj_lex_next(ls); /* Skip 'repeat'. */ - bcemit_AD(fs, BC_LOOP, fs->nactvar, 0); - parse_chunk(ls); - lex_match(ls, TK_until, TK_repeat, line); - condexit = expr_cond(ls); /* Parse condition (still inside inner scope). */ - if (!(bl2.flags & FSCOPE_UPVAL)) { /* No upvalues? Just end inner scope. */ - fscope_end(fs); - } else { /* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */ - parse_break(ls); /* Break from loop and close upvalues. */ - jmp_tohere(fs, condexit); - fscope_end(fs); /* End inner scope and close upvalues. */ - condexit = bcemit_jmp(fs); - } - jmp_patch(fs, condexit, loop); /* Jump backwards if !cond. */ - jmp_patchins(fs, loop, fs->pc); - fscope_end(fs); /* End loop scope. */ -} - -/* Parse numeric 'for'. */ -static void parse_for_num(LexState *ls, GCstr *varname, BCLine line) -{ - FuncState *fs = ls->fs; - BCReg base = fs->freereg; - FuncScope bl; - BCPos loop, loopend; - /* Hidden control variables. */ - var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX); - var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP); - var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP); - /* Visible copy of index variable. */ - var_new(ls, FORL_EXT, varname); - lex_check(ls, '='); - expr_next(ls); - lex_check(ls, ','); - expr_next(ls); - if (lex_opt(ls, ',')) { - expr_next(ls); - } else { - bcemit_AD(fs, BC_KSHORT, fs->freereg, 1); /* Default step is 1. */ - bcreg_reserve(fs, 1); - } - var_add(ls, 3); /* Hidden control variables. */ - lex_check(ls, TK_do); - loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP); - fscope_begin(fs, &bl, 0); /* Scope for visible variables. */ - var_add(ls, 1); - bcreg_reserve(fs, 1); - parse_block(ls); - fscope_end(fs); - /* Perform loop inversion. Loop control instructions are at the end. */ - loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP); - fs->bcbase[loopend].line = line; /* Fix line for control ins. */ - jmp_patchins(fs, loopend, loop+1); - jmp_patchins(fs, loop, fs->pc); -} - -/* Try to predict whether the iterator is next() and specialize the bytecode. -** Detecting next() and pairs() by name is simplistic, but quite effective. -** The interpreter backs off if the check for the closure fails at runtime. -*/ -static int predict_next(LexState *ls, FuncState *fs, BCPos pc) -{ - BCIns ins = fs->bcbase[pc].ins; - GCstr *name; - cTValue *o; - switch (bc_op(ins)) { - case BC_MOV: - name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name)); - break; - case BC_UGET: - name = gco2str(gcref(ls->vstack[fs->uvmap[bc_d(ins)]].name)); - break; - case BC_GGET: - /* There's no inverse index (yet), so lookup the strings. */ - o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "pairs")); - if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins)) - return 1; - o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "next")); - if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins)) - return 1; - return 0; - default: - return 0; - } - return (name->len == 5 && !strcmp(strdata(name), "pairs")) || - (name->len == 4 && !strcmp(strdata(name), "next")); -} - -/* Parse 'for' iterator. */ -static void parse_for_iter(LexState *ls, GCstr *indexname) -{ - FuncState *fs = ls->fs; - ExpDesc e; - BCReg nvars = 0; - BCLine line; - BCReg base = fs->freereg + 3; - BCPos loop, loopend, exprpc = fs->pc; - FuncScope bl; - int isnext; - /* Hidden control variables. */ - var_new_fixed(ls, nvars++, VARNAME_FOR_GEN); - var_new_fixed(ls, nvars++, VARNAME_FOR_STATE); - var_new_fixed(ls, nvars++, VARNAME_FOR_CTL); - /* Visible variables returned from iterator. */ - var_new(ls, nvars++, indexname); - while (lex_opt(ls, ',')) - var_new(ls, nvars++, lex_str(ls)); - lex_check(ls, TK_in); - line = ls->linenumber; - assign_adjust(ls, 3, expr_list(ls, &e), &e); - bcreg_bump(fs, 3); /* The iterator needs another 3 slots (func + 2 args). */ - isnext = (nvars <= 5 && predict_next(ls, fs, exprpc)); - var_add(ls, 3); /* Hidden control variables. */ - lex_check(ls, TK_do); - loop = bcemit_AJ(fs, isnext ? BC_ISNEXT : BC_JMP, base, NO_JMP); - fscope_begin(fs, &bl, 0); /* Scope for visible variables. */ - var_add(ls, nvars-3); - bcreg_reserve(fs, nvars-3); - parse_block(ls); - fscope_end(fs); - /* Perform loop inversion. Loop control instructions are at the end. */ - jmp_patchins(fs, loop, fs->pc); - bcemit_ABC(fs, isnext ? BC_ITERN : BC_ITERC, base, nvars-3+1, 2+1); - loopend = bcemit_AJ(fs, BC_ITERL, base, NO_JMP); - fs->bcbase[loopend-1].line = line; /* Fix line for control ins. */ - fs->bcbase[loopend].line = line; - jmp_patchins(fs, loopend, loop+1); -} - -/* Parse 'for' statement. */ -static void parse_for(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - GCstr *varname; - FuncScope bl; - fscope_begin(fs, &bl, FSCOPE_LOOP); - lj_lex_next(ls); /* Skip 'for'. */ - varname = lex_str(ls); /* Get first variable name. */ - if (ls->token == '=') - parse_for_num(ls, varname, line); - else if (ls->token == ',' || ls->token == TK_in) - parse_for_iter(ls, varname); - else - err_syntax(ls, LJ_ERR_XFOR); - lex_match(ls, TK_end, TK_for, line); - fscope_end(fs); /* Resolve break list. */ -} - -/* Parse condition and 'then' block. */ -static BCPos parse_then(LexState *ls) -{ - BCPos condexit; - lj_lex_next(ls); /* Skip 'if' or 'elseif'. */ - condexit = expr_cond(ls); - lex_check(ls, TK_then); - parse_block(ls); - return condexit; -} - -/* Parse 'if' statement. */ -static void parse_if(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - BCPos flist; - BCPos escapelist = NO_JMP; - flist = parse_then(ls); - while (ls->token == TK_elseif) { /* Parse multiple 'elseif' blocks. */ - jmp_append(fs, &escapelist, bcemit_jmp(fs)); - jmp_tohere(fs, flist); - flist = parse_then(ls); - } - if (ls->token == TK_else) { /* Parse optional 'else' block. */ - jmp_append(fs, &escapelist, bcemit_jmp(fs)); - jmp_tohere(fs, flist); - lj_lex_next(ls); /* Skip 'else'. */ - parse_block(ls); - } else { - jmp_append(fs, &escapelist, flist); - } - jmp_tohere(fs, escapelist); - lex_match(ls, TK_end, TK_if, line); -} - -/* -- Parse statements ---------------------------------------------------- */ - -/* Parse a statement. Returns 1 if it must be the last one in a chunk. */ -static int parse_stmt(LexState *ls) -{ - BCLine line = ls->linenumber; - switch (ls->token) { - case TK_if: - parse_if(ls, line); - break; - case TK_while: - parse_while(ls, line); - break; - case TK_do: - lj_lex_next(ls); - parse_block(ls); - lex_match(ls, TK_end, TK_do, line); - break; - case TK_for: - parse_for(ls, line); - break; - case TK_repeat: - parse_repeat(ls, line); - break; - case TK_function: - parse_func(ls, line); - break; - case TK_local: - lj_lex_next(ls); - parse_local(ls); - break; - case TK_return: - parse_return(ls); - return 1; /* Must be last. */ - case TK_break: - lj_lex_next(ls); - parse_break(ls); - return !LJ_52; /* Must be last in Lua 5.1. */ -#if LJ_52 - case ';': - lj_lex_next(ls); - break; -#endif - case TK_label: - parse_label(ls); - break; - case TK_goto: - if (LJ_52 || lj_lex_lookahead(ls) == TK_name) { - lj_lex_next(ls); - parse_goto(ls); - break; - } /* else: fallthrough */ - default: - parse_call_assign(ls); - break; - } - return 0; -} - -/* A chunk is a list of statements optionally separated by semicolons. */ -static void parse_chunk(LexState *ls) -{ - int islast = 0; - synlevel_begin(ls); - while (!islast && !endofblock(ls->token)) { - islast = parse_stmt(ls); - lex_opt(ls, ';'); - lua_assert(ls->fs->framesize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar); - ls->fs->freereg = ls->fs->nactvar; /* Free registers after each stmt. */ - } - synlevel_end(ls); -} - -/* Entry point of bytecode parser. */ -GCproto *lj_parse(LexState *ls) -{ - FuncState fs; - FuncScope bl; - GCproto *pt; - lua_State *L = ls->L; -#ifdef LUAJIT_DISABLE_DEBUGINFO - ls->chunkname = lj_str_newlit(L, "="); -#else - ls->chunkname = lj_str_newz(L, ls->chunkarg); -#endif - setstrV(L, L->top, ls->chunkname); /* Anchor chunkname string. */ - incr_top(L); - ls->level = 0; - fs_init(ls, &fs); - fs.linedefined = 0; - fs.numparams = 0; - fs.bcbase = NULL; - fs.bclim = 0; - fs.flags |= PROTO_VARARG; /* Main chunk is always a vararg func. */ - fscope_begin(&fs, &bl, 0); - bcemit_AD(&fs, BC_FUNCV, 0, 0); /* Placeholder. */ - lj_lex_next(ls); /* Read-ahead first token. */ - parse_chunk(ls); - if (ls->token != TK_eof) - err_token(ls, TK_eof); - pt = fs_finish(ls, ls->linenumber); - L->top--; /* Drop chunkname. */ - lua_assert(fs.prev == NULL); - lua_assert(ls->fs == NULL); - lua_assert(pt->sizeuv == 0); - return pt; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_parse.h b/third-party/LuaJIT-2.0.2/src/lj_parse.h deleted file mode 100644 index 558b4e2d01..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_parse.h +++ /dev/null @@ -1,18 +0,0 @@ -/* -** Lua parser (source code -> bytecode). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_PARSE_H -#define _LJ_PARSE_H - -#include "lj_obj.h" -#include "lj_lex.h" - -LJ_FUNC GCproto *lj_parse(LexState *ls); -LJ_FUNC GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t l); -#if LJ_HASFFI -LJ_FUNC void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_record.c b/third-party/LuaJIT-2.0.2/src/lj_record.c deleted file mode 100644 index 7336e0ac14..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_record.c +++ /dev/null @@ -1,2247 +0,0 @@ -/* -** Trace recorder (bytecode -> SSA IR). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_record_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_bc.h" -#include "lj_ff.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_record.h" -#include "lj_ffrecord.h" -#include "lj_snap.h" -#include "lj_dispatch.h" -#include "lj_vm.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Emit raw IR without passing through optimizations. */ -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- Sanity checks ------------------------------------------------------- */ - -#ifdef LUA_USE_ASSERT -/* Sanity check the whole IR -- sloooow. */ -static void rec_check_ir(jit_State *J) -{ - IRRef i, nins = J->cur.nins, nk = J->cur.nk; - lua_assert(nk <= REF_BIAS && nins >= REF_BIAS && nins < 65536); - for (i = nins-1; i >= nk; i--) { - IRIns *ir = IR(i); - uint32_t mode = lj_ir_mode[ir->o]; - IRRef op1 = ir->op1; - IRRef op2 = ir->op2; - switch (irm_op1(mode)) { - case IRMnone: lua_assert(op1 == 0); break; - case IRMref: lua_assert(op1 >= nk); - lua_assert(i >= REF_BIAS ? op1 < i : op1 > i); break; - case IRMlit: break; - case IRMcst: lua_assert(i < REF_BIAS); continue; - } - switch (irm_op2(mode)) { - case IRMnone: lua_assert(op2 == 0); break; - case IRMref: lua_assert(op2 >= nk); - lua_assert(i >= REF_BIAS ? op2 < i : op2 > i); break; - case IRMlit: break; - case IRMcst: lua_assert(0); break; - } - if (ir->prev) { - lua_assert(ir->prev >= nk); - lua_assert(i >= REF_BIAS ? ir->prev < i : ir->prev > i); - lua_assert(ir->o == IR_NOP || IR(ir->prev)->o == ir->o); - } - } -} - -/* Compare stack slots and frames of the recorder and the VM. */ -static void rec_check_slots(jit_State *J) -{ - BCReg s, nslots = J->baseslot + J->maxslot; - int32_t depth = 0; - cTValue *base = J->L->base - J->baseslot; - lua_assert(J->baseslot >= 1 && J->baseslot < LJ_MAX_JSLOTS); - lua_assert(J->baseslot == 1 || (J->slot[J->baseslot-1] & TREF_FRAME)); - lua_assert(nslots < LJ_MAX_JSLOTS); - for (s = 0; s < nslots; s++) { - TRef tr = J->slot[s]; - if (tr) { - cTValue *tv = &base[s]; - IRRef ref = tref_ref(tr); - IRIns *ir; - lua_assert(ref >= J->cur.nk && ref < J->cur.nins); - ir = IR(ref); - lua_assert(irt_t(ir->t) == tref_t(tr)); - if (s == 0) { - lua_assert(tref_isfunc(tr)); - } else if ((tr & TREF_FRAME)) { - GCfunc *fn = gco2func(frame_gc(tv)); - BCReg delta = (BCReg)(tv - frame_prev(tv)); - lua_assert(tref_isfunc(tr)); - if (tref_isk(tr)) lua_assert(fn == ir_kfunc(ir)); - lua_assert(s > delta ? (J->slot[s-delta] & TREF_FRAME) : (s == delta)); - depth++; - } else if ((tr & TREF_CONT)) { - lua_assert(ir_kptr(ir) == gcrefp(tv->gcr, void)); - lua_assert((J->slot[s+1] & TREF_FRAME)); - depth++; - } else { - if (tvisnumber(tv)) - lua_assert(tref_isnumber(tr)); /* Could be IRT_INT etc., too. */ - else - lua_assert(itype2irt(tv) == tref_type(tr)); - if (tref_isk(tr)) { /* Compare constants. */ - TValue tvk; - lj_ir_kvalue(J->L, &tvk, ir); - if (!(tvisnum(&tvk) && tvisnan(&tvk))) - lua_assert(lj_obj_equal(tv, &tvk)); - else - lua_assert(tvisnum(tv) && tvisnan(tv)); - } - } - } - } - lua_assert(J->framedepth == depth); -} -#endif - -/* -- Type handling and specialization ------------------------------------ */ - -/* Note: these functions return tagged references (TRef). */ - -/* Specialize a slot to a specific type. Note: slot can be negative! */ -static TRef sloadt(jit_State *J, int32_t slot, IRType t, int mode) -{ - /* Caller may set IRT_GUARD in t. */ - TRef ref = emitir_raw(IRT(IR_SLOAD, t), (int32_t)J->baseslot+slot, mode); - J->base[slot] = ref; - return ref; -} - -/* Specialize a slot to the runtime type. Note: slot can be negative! */ -static TRef sload(jit_State *J, int32_t slot) -{ - IRType t = itype2irt(&J->L->base[slot]); - TRef ref = emitir_raw(IRTG(IR_SLOAD, t), (int32_t)J->baseslot+slot, - IRSLOAD_TYPECHECK); - if (irtype_ispri(t)) ref = TREF_PRI(t); /* Canonicalize primitive refs. */ - J->base[slot] = ref; - return ref; -} - -/* Get TRef from slot. Load slot and specialize if not done already. */ -#define getslot(J, s) (J->base[(s)] ? J->base[(s)] : sload(J, (int32_t)(s))) - -/* Get TRef for current function. */ -static TRef getcurrf(jit_State *J) -{ - if (J->base[-1]) - return J->base[-1]; - lua_assert(J->baseslot == 1); - return sloadt(J, -1, IRT_FUNC, IRSLOAD_READONLY); -} - -/* Compare for raw object equality. -** Returns 0 if the objects are the same. -** Returns 1 if they are different, but the same type. -** Returns 2 for two different types. -** Comparisons between primitives always return 1 -- no caller cares about it. -*/ -int lj_record_objcmp(jit_State *J, TRef a, TRef b, cTValue *av, cTValue *bv) -{ - int diff = !lj_obj_equal(av, bv); - if (!tref_isk2(a, b)) { /* Shortcut, also handles primitives. */ - IRType ta = tref_isinteger(a) ? IRT_INT : tref_type(a); - IRType tb = tref_isinteger(b) ? IRT_INT : tref_type(b); - if (ta != tb) { - /* Widen mixed number/int comparisons to number/number comparison. */ - if (ta == IRT_INT && tb == IRT_NUM) { - a = emitir(IRTN(IR_CONV), a, IRCONV_NUM_INT); - ta = IRT_NUM; - } else if (ta == IRT_NUM && tb == IRT_INT) { - b = emitir(IRTN(IR_CONV), b, IRCONV_NUM_INT); - } else { - return 2; /* Two different types are never equal. */ - } - } - emitir(IRTG(diff ? IR_NE : IR_EQ, ta), a, b); - } - return diff; -} - -/* Constify a value. Returns 0 for non-representable object types. */ -TRef lj_record_constify(jit_State *J, cTValue *o) -{ - if (tvisgcv(o)) - return lj_ir_kgc(J, gcV(o), itype2irt(o)); - else if (tvisint(o)) - return lj_ir_kint(J, intV(o)); - else if (tvisnum(o)) - return lj_ir_knumint(J, numV(o)); - else if (tvisbool(o)) - return TREF_PRI(itype2irt(o)); - else - return 0; /* Can't represent lightuserdata (pointless). */ -} - -/* -- Record loop ops ----------------------------------------------------- */ - -/* Loop event. */ -typedef enum { - LOOPEV_LEAVE, /* Loop is left or not entered. */ - LOOPEV_ENTERLO, /* Loop is entered with a low iteration count left. */ - LOOPEV_ENTER /* Loop is entered. */ -} LoopEvent; - -/* Canonicalize slots: convert integers to numbers. */ -static void canonicalize_slots(jit_State *J) -{ - BCReg s; - if (LJ_DUALNUM) return; - for (s = J->baseslot+J->maxslot-1; s >= 1; s--) { - TRef tr = J->slot[s]; - if (tref_isinteger(tr)) { - IRIns *ir = IR(tref_ref(tr)); - if (!(ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_READONLY))) - J->slot[s] = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); - } - } -} - -/* Stop recording. */ -static void rec_stop(jit_State *J, TraceLink linktype, TraceNo lnk) -{ - lj_trace_end(J); - J->cur.linktype = (uint8_t)linktype; - J->cur.link = (uint16_t)lnk; - /* Looping back at the same stack level? */ - if (lnk == J->cur.traceno && J->framedepth + J->retdepth == 0) { - if ((J->flags & JIT_F_OPT_LOOP)) /* Shall we try to create a loop? */ - goto nocanon; /* Do not canonicalize or we lose the narrowing. */ - if (J->cur.root) /* Otherwise ensure we always link to the root trace. */ - J->cur.link = J->cur.root; - } - canonicalize_slots(J); -nocanon: - /* Note: all loop ops must set J->pc to the following instruction! */ - lj_snap_add(J); /* Add loop snapshot. */ - J->needsnap = 0; - J->mergesnap = 1; /* In case recording continues. */ -} - -/* Search bytecode backwards for a int/num constant slot initializer. */ -static TRef find_kinit(jit_State *J, const BCIns *endpc, BCReg slot, IRType t) -{ - /* This algorithm is rather simplistic and assumes quite a bit about - ** how the bytecode is generated. It works fine for FORI initializers, - ** but it won't necessarily work in other cases (e.g. iterator arguments). - ** It doesn't do anything fancy, either (like backpropagating MOVs). - */ - const BCIns *pc, *startpc = proto_bc(J->pt); - for (pc = endpc-1; pc > startpc; pc--) { - BCIns ins = *pc; - BCOp op = bc_op(ins); - /* First try to find the last instruction that stores to this slot. */ - if (bcmode_a(op) == BCMbase && bc_a(ins) <= slot) { - return 0; /* Multiple results, e.g. from a CALL or KNIL. */ - } else if (bcmode_a(op) == BCMdst && bc_a(ins) == slot) { - if (op == BC_KSHORT || op == BC_KNUM) { /* Found const. initializer. */ - /* Now try to verify there's no forward jump across it. */ - const BCIns *kpc = pc; - for (; pc > startpc; pc--) - if (bc_op(*pc) == BC_JMP) { - const BCIns *target = pc+bc_j(*pc)+1; - if (target > kpc && target <= endpc) - return 0; /* Conditional assignment. */ - } - if (op == BC_KSHORT) { - int32_t k = (int32_t)(int16_t)bc_d(ins); - return t == IRT_INT ? lj_ir_kint(J, k) : lj_ir_knum(J, (lua_Number)k); - } else { - cTValue *tv = proto_knumtv(J->pt, bc_d(ins)); - if (t == IRT_INT) { - int32_t k = numberVint(tv); - if (tvisint(tv) || numV(tv) == (lua_Number)k) /* -0 is ok here. */ - return lj_ir_kint(J, k); - return 0; /* Type mismatch. */ - } else { - return lj_ir_knum(J, numberVnum(tv)); - } - } - } - return 0; /* Non-constant initializer. */ - } - } - return 0; /* No assignment to this slot found? */ -} - -/* Load and optionally convert a FORI argument from a slot. */ -static TRef fori_load(jit_State *J, BCReg slot, IRType t, int mode) -{ - int conv = (tvisint(&J->L->base[slot]) != (t==IRT_INT)) ? IRSLOAD_CONVERT : 0; - return sloadt(J, (int32_t)slot, - t + (((mode & IRSLOAD_TYPECHECK) || - (conv && t == IRT_INT && !(mode >> 16))) ? - IRT_GUARD : 0), - mode + conv); -} - -/* Peek before FORI to find a const initializer. Otherwise load from slot. */ -static TRef fori_arg(jit_State *J, const BCIns *fori, BCReg slot, - IRType t, int mode) -{ - TRef tr = J->base[slot]; - if (!tr) { - tr = find_kinit(J, fori, slot, t); - if (!tr) - tr = fori_load(J, slot, t, mode); - } - return tr; -} - -/* Return the direction of the FOR loop iterator. -** It's important to exactly reproduce the semantics of the interpreter. -*/ -static int rec_for_direction(cTValue *o) -{ - return (tvisint(o) ? intV(o) : (int32_t)o->u32.hi) >= 0; -} - -/* Simulate the runtime behavior of the FOR loop iterator. */ -static LoopEvent rec_for_iter(IROp *op, cTValue *o, int isforl) -{ - lua_Number stopv = numberVnum(&o[FORL_STOP]); - lua_Number idxv = numberVnum(&o[FORL_IDX]); - lua_Number stepv = numberVnum(&o[FORL_STEP]); - if (isforl) - idxv += stepv; - if (rec_for_direction(&o[FORL_STEP])) { - if (idxv <= stopv) { - *op = IR_LE; - return idxv + 2*stepv > stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER; - } - *op = IR_GT; return LOOPEV_LEAVE; - } else { - if (stopv <= idxv) { - *op = IR_GE; - return idxv + 2*stepv < stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER; - } - *op = IR_LT; return LOOPEV_LEAVE; - } -} - -/* Record checks for FOR loop overflow and step direction. */ -static void rec_for_check(jit_State *J, IRType t, int dir, - TRef stop, TRef step, int init) -{ - if (!tref_isk(step)) { - /* Non-constant step: need a guard for the direction. */ - TRef zero = (t == IRT_INT) ? lj_ir_kint(J, 0) : lj_ir_knum_zero(J); - emitir(IRTG(dir ? IR_GE : IR_LT, t), step, zero); - /* Add hoistable overflow checks for a narrowed FORL index. */ - if (init && t == IRT_INT) { - if (tref_isk(stop)) { - /* Constant stop: optimize check away or to a range check for step. */ - int32_t k = IR(tref_ref(stop))->i; - if (dir) { - if (k > 0) - emitir(IRTGI(IR_LE), step, lj_ir_kint(J, (int32_t)0x7fffffff-k)); - } else { - if (k < 0) - emitir(IRTGI(IR_GE), step, lj_ir_kint(J, (int32_t)0x80000000-k)); - } - } else { - /* Stop+step variable: need full overflow check. */ - TRef tr = emitir(IRTGI(IR_ADDOV), step, stop); - emitir(IRTI(IR_USE), tr, 0); /* ADDOV is weak. Avoid dead result. */ - } - } - } else if (init && t == IRT_INT && !tref_isk(stop)) { - /* Constant step: optimize overflow check to a range check for stop. */ - int32_t k = IR(tref_ref(step))->i; - k = (int32_t)(dir ? 0x7fffffff : 0x80000000) - k; - emitir(IRTGI(dir ? IR_LE : IR_GE), stop, lj_ir_kint(J, k)); - } -} - -/* Record a FORL instruction. */ -static void rec_for_loop(jit_State *J, const BCIns *fori, ScEvEntry *scev, - int init) -{ - BCReg ra = bc_a(*fori); - cTValue *tv = &J->L->base[ra]; - TRef idx = J->base[ra+FORL_IDX]; - IRType t = idx ? tref_type(idx) : - (init || LJ_DUALNUM) ? lj_opt_narrow_forl(J, tv) : IRT_NUM; - int mode = IRSLOAD_INHERIT + - ((!LJ_DUALNUM || tvisint(tv) == (t == IRT_INT)) ? IRSLOAD_READONLY : 0); - TRef stop = fori_arg(J, fori, ra+FORL_STOP, t, mode); - TRef step = fori_arg(J, fori, ra+FORL_STEP, t, mode); - int tc, dir = rec_for_direction(&tv[FORL_STEP]); - lua_assert(bc_op(*fori) == BC_FORI || bc_op(*fori) == BC_JFORI); - scev->t.irt = t; - scev->dir = dir; - scev->stop = tref_ref(stop); - scev->step = tref_ref(step); - rec_for_check(J, t, dir, stop, step, init); - scev->start = tref_ref(find_kinit(J, fori, ra+FORL_IDX, IRT_INT)); - tc = (LJ_DUALNUM && - !(scev->start && irref_isk(scev->stop) && irref_isk(scev->step) && - tvisint(&tv[FORL_IDX]) == (t == IRT_INT))) ? - IRSLOAD_TYPECHECK : 0; - if (tc) { - J->base[ra+FORL_STOP] = stop; - J->base[ra+FORL_STEP] = step; - } - if (!idx) - idx = fori_load(J, ra+FORL_IDX, t, - IRSLOAD_INHERIT + tc + (J->scev.start << 16)); - if (!init) - J->base[ra+FORL_IDX] = idx = emitir(IRT(IR_ADD, t), idx, step); - J->base[ra+FORL_EXT] = idx; - scev->idx = tref_ref(idx); - J->maxslot = ra+FORL_EXT+1; -} - -/* Record FORL/JFORL or FORI/JFORI. */ -static LoopEvent rec_for(jit_State *J, const BCIns *fori, int isforl) -{ - BCReg ra = bc_a(*fori); - TValue *tv = &J->L->base[ra]; - TRef *tr = &J->base[ra]; - IROp op; - LoopEvent ev; - TRef stop; - IRType t; - if (isforl) { /* Handle FORL/JFORL opcodes. */ - TRef idx = tr[FORL_IDX]; - if (tref_ref(idx) == J->scev.idx) { - t = J->scev.t.irt; - stop = J->scev.stop; - idx = emitir(IRT(IR_ADD, t), idx, J->scev.step); - tr[FORL_EXT] = tr[FORL_IDX] = idx; - } else { - ScEvEntry scev; - rec_for_loop(J, fori, &scev, 0); - t = scev.t.irt; - stop = scev.stop; - } - } else { /* Handle FORI/JFORI opcodes. */ - BCReg i; - lj_meta_for(J->L, tv); - t = (LJ_DUALNUM || tref_isint(tr[FORL_IDX])) ? lj_opt_narrow_forl(J, tv) : - IRT_NUM; - for (i = FORL_IDX; i <= FORL_STEP; i++) { - if (!tr[i]) sload(J, ra+i); - lua_assert(tref_isnumber_str(tr[i])); - if (tref_isstr(tr[i])) - tr[i] = emitir(IRTG(IR_STRTO, IRT_NUM), tr[i], 0); - if (t == IRT_INT) { - if (!tref_isinteger(tr[i])) - tr[i] = emitir(IRTGI(IR_CONV), tr[i], IRCONV_INT_NUM|IRCONV_CHECK); - } else { - if (!tref_isnum(tr[i])) - tr[i] = emitir(IRTN(IR_CONV), tr[i], IRCONV_NUM_INT); - } - } - tr[FORL_EXT] = tr[FORL_IDX]; - stop = tr[FORL_STOP]; - rec_for_check(J, t, rec_for_direction(&tv[FORL_STEP]), - stop, tr[FORL_STEP], 1); - } - - ev = rec_for_iter(&op, tv, isforl); - if (ev == LOOPEV_LEAVE) { - J->maxslot = ra+FORL_EXT+1; - J->pc = fori+1; - } else { - J->maxslot = ra; - J->pc = fori+bc_j(*fori)+1; - } - lj_snap_add(J); - - emitir(IRTG(op, t), tr[FORL_IDX], stop); - - if (ev == LOOPEV_LEAVE) { - J->maxslot = ra; - J->pc = fori+bc_j(*fori)+1; - } else { - J->maxslot = ra+FORL_EXT+1; - J->pc = fori+1; - } - J->needsnap = 1; - return ev; -} - -/* Record ITERL/JITERL. */ -static LoopEvent rec_iterl(jit_State *J, const BCIns iterins) -{ - BCReg ra = bc_a(iterins); - lua_assert(J->base[ra] != 0); - if (!tref_isnil(J->base[ra])) { /* Looping back? */ - J->base[ra-1] = J->base[ra]; /* Copy result of ITERC to control var. */ - J->maxslot = ra-1+bc_b(J->pc[-1]); - J->pc += bc_j(iterins)+1; - return LOOPEV_ENTER; - } else { - J->maxslot = ra-3; - J->pc++; - return LOOPEV_LEAVE; - } -} - -/* Record LOOP/JLOOP. Now, that was easy. */ -static LoopEvent rec_loop(jit_State *J, BCReg ra) -{ - if (ra < J->maxslot) J->maxslot = ra; - J->pc++; - return LOOPEV_ENTER; -} - -/* Check if a loop repeatedly failed to trace because it didn't loop back. */ -static int innerloopleft(jit_State *J, const BCIns *pc) -{ - ptrdiff_t i; - for (i = 0; i < PENALTY_SLOTS; i++) - if (mref(J->penalty[i].pc, const BCIns) == pc) { - if ((J->penalty[i].reason == LJ_TRERR_LLEAVE || - J->penalty[i].reason == LJ_TRERR_LINNER) && - J->penalty[i].val >= 2*PENALTY_MIN) - return 1; - break; - } - return 0; -} - -/* Handle the case when an interpreted loop op is hit. */ -static void rec_loop_interp(jit_State *J, const BCIns *pc, LoopEvent ev) -{ - if (J->parent == 0) { - if (pc == J->startpc && J->framedepth + J->retdepth == 0) { - /* Same loop? */ - if (ev == LOOPEV_LEAVE) /* Must loop back to form a root trace. */ - lj_trace_err(J, LJ_TRERR_LLEAVE); - rec_stop(J, LJ_TRLINK_LOOP, J->cur.traceno); /* Looping root trace. */ - } else if (ev != LOOPEV_LEAVE) { /* Entering inner loop? */ - /* It's usually better to abort here and wait until the inner loop - ** is traced. But if the inner loop repeatedly didn't loop back, - ** this indicates a low trip count. In this case try unrolling - ** an inner loop even in a root trace. But it's better to be a bit - ** more conservative here and only do it for very short loops. - */ - if (bc_j(*pc) != -1 && !innerloopleft(J, pc)) - lj_trace_err(J, LJ_TRERR_LINNER); /* Root trace hit an inner loop. */ - if ((ev != LOOPEV_ENTERLO && - J->loopref && J->cur.nins - J->loopref > 24) || --J->loopunroll < 0) - lj_trace_err(J, LJ_TRERR_LUNROLL); /* Limit loop unrolling. */ - J->loopref = J->cur.nins; - } - } else if (ev != LOOPEV_LEAVE) { /* Side trace enters an inner loop. */ - J->loopref = J->cur.nins; - if (--J->loopunroll < 0) - lj_trace_err(J, LJ_TRERR_LUNROLL); /* Limit loop unrolling. */ - } /* Side trace continues across a loop that's left or not entered. */ -} - -/* Handle the case when an already compiled loop op is hit. */ -static void rec_loop_jit(jit_State *J, TraceNo lnk, LoopEvent ev) -{ - if (J->parent == 0) { /* Root trace hit an inner loop. */ - /* Better let the inner loop spawn a side trace back here. */ - lj_trace_err(J, LJ_TRERR_LINNER); - } else if (ev != LOOPEV_LEAVE) { /* Side trace enters a compiled loop. */ - J->instunroll = 0; /* Cannot continue across a compiled loop op. */ - if (J->pc == J->startpc && J->framedepth + J->retdepth == 0) - rec_stop(J, LJ_TRLINK_LOOP, J->cur.traceno); /* Form an extra loop. */ - else - rec_stop(J, LJ_TRLINK_ROOT, lnk); /* Link to the loop. */ - } /* Side trace continues across a loop that's left or not entered. */ -} - -/* -- Record calls and returns -------------------------------------------- */ - -/* Specialize to the runtime value of the called function or its prototype. */ -static TRef rec_call_specialize(jit_State *J, GCfunc *fn, TRef tr) -{ - TRef kfunc; - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - /* Too many closures created? Probably not a monomorphic function. */ - if (pt->flags >= PROTO_CLC_POLY) { /* Specialize to prototype instead. */ - TRef trpt = emitir(IRT(IR_FLOAD, IRT_P32), tr, IRFL_FUNC_PC); - emitir(IRTG(IR_EQ, IRT_P32), trpt, lj_ir_kptr(J, proto_bc(pt))); - (void)lj_ir_kgc(J, obj2gco(pt), IRT_PROTO); /* Prevent GC of proto. */ - return tr; - } - } - /* Otherwise specialize to the function (closure) value itself. */ - kfunc = lj_ir_kfunc(J, fn); - emitir(IRTG(IR_EQ, IRT_FUNC), tr, kfunc); - return kfunc; -} - -/* Record call setup. */ -static void rec_call_setup(jit_State *J, BCReg func, ptrdiff_t nargs) -{ - RecordIndex ix; - TValue *functv = &J->L->base[func]; - TRef *fbase = &J->base[func]; - ptrdiff_t i; - for (i = 0; i <= nargs; i++) - (void)getslot(J, func+i); /* Ensure func and all args have a reference. */ - if (!tref_isfunc(fbase[0])) { /* Resolve __call metamethod. */ - ix.tab = fbase[0]; - copyTV(J->L, &ix.tabv, functv); - if (!lj_record_mm_lookup(J, &ix, MM_call) || !tref_isfunc(ix.mobj)) - lj_trace_err(J, LJ_TRERR_NOMM); - for (i = ++nargs; i > 0; i--) /* Shift arguments up. */ - fbase[i] = fbase[i-1]; - fbase[0] = ix.mobj; /* Replace function. */ - functv = &ix.mobjv; - } - fbase[0] = TREF_FRAME | rec_call_specialize(J, funcV(functv), fbase[0]); - J->maxslot = (BCReg)nargs; -} - -/* Record call. */ -void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs) -{ - rec_call_setup(J, func, nargs); - /* Bump frame. */ - J->framedepth++; - J->base += func+1; - J->baseslot += func+1; -} - -/* Record tail call. */ -void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs) -{ - rec_call_setup(J, func, nargs); - if (frame_isvarg(J->L->base - 1)) { - BCReg cbase = (BCReg)frame_delta(J->L->base - 1); - if (--J->framedepth < 0) - lj_trace_err(J, LJ_TRERR_NYIRETL); - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - func += cbase; - } - /* Move func + args down. */ - memmove(&J->base[-1], &J->base[func], sizeof(TRef)*(J->maxslot+1)); - /* Note: the new TREF_FRAME is now at J->base[-1] (even for slot #0). */ - /* Tailcalls can form a loop, so count towards the loop unroll limit. */ - if (++J->tailcalled > J->loopunroll) - lj_trace_err(J, LJ_TRERR_LUNROLL); -} - -/* Check unroll limits for down-recursion. */ -static int check_downrec_unroll(jit_State *J, GCproto *pt) -{ - IRRef ptref; - for (ptref = J->chain[IR_KGC]; ptref; ptref = IR(ptref)->prev) - if (ir_kgc(IR(ptref)) == obj2gco(pt)) { - int count = 0; - IRRef ref; - for (ref = J->chain[IR_RETF]; ref; ref = IR(ref)->prev) - if (IR(ref)->op1 == ptref) - count++; - if (count) { - if (J->pc == J->startpc) { - if (count + J->tailcalled > J->param[JIT_P_recunroll]) - return 1; - } else { - lj_trace_err(J, LJ_TRERR_DOWNREC); - } - } - } - return 0; -} - -/* Record return. */ -void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) -{ - TValue *frame = J->L->base - 1; - ptrdiff_t i; - for (i = 0; i < gotresults; i++) - (void)getslot(J, rbase+i); /* Ensure all results have a reference. */ - while (frame_ispcall(frame)) { /* Immediately resolve pcall() returns. */ - BCReg cbase = (BCReg)frame_delta(frame); - if (--J->framedepth < 0) - lj_trace_err(J, LJ_TRERR_NYIRETL); - lua_assert(J->baseslot > 1); - gotresults++; - rbase += cbase; - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - J->base[--rbase] = TREF_TRUE; /* Prepend true to results. */ - frame = frame_prevd(frame); - } - /* Return to lower frame via interpreter for unhandled cases. */ - if (J->framedepth == 0 && J->pt && bc_isret(bc_op(*J->pc)) && - (!frame_islua(frame) || - (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))))) { - /* NYI: specialize to frame type and return directly, not via RET*. */ - for (i = -1; i < (ptrdiff_t)rbase; i++) - J->base[i] = 0; /* Purge dead slots. */ - J->maxslot = rbase + (BCReg)gotresults; - rec_stop(J, LJ_TRLINK_RETURN, 0); /* Return to interpreter. */ - return; - } - if (frame_isvarg(frame)) { - BCReg cbase = (BCReg)frame_delta(frame); - if (--J->framedepth < 0) /* NYI: return of vararg func to lower frame. */ - lj_trace_err(J, LJ_TRERR_NYIRETL); - lua_assert(J->baseslot > 1); - rbase += cbase; - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - frame = frame_prevd(frame); - } - if (frame_islua(frame)) { /* Return to Lua frame. */ - BCIns callins = *(frame_pc(frame)-1); - ptrdiff_t nresults = bc_b(callins) ? (ptrdiff_t)bc_b(callins)-1 :gotresults; - BCReg cbase = bc_a(callins); - GCproto *pt = funcproto(frame_func(frame - (cbase+1))); - if (J->framedepth == 0 && J->pt && frame == J->L->base - 1) { - if (check_downrec_unroll(J, pt)) { - J->maxslot = (BCReg)(rbase + gotresults); - lj_snap_purge(J); - rec_stop(J, LJ_TRLINK_DOWNREC, J->cur.traceno); /* Down-recursion. */ - return; - } - lj_snap_add(J); - } - for (i = 0; i < nresults; i++) /* Adjust results. */ - J->base[i-1] = i < gotresults ? J->base[rbase+i] : TREF_NIL; - J->maxslot = cbase+(BCReg)nresults; - if (J->framedepth > 0) { /* Return to a frame that is part of the trace. */ - J->framedepth--; - lua_assert(J->baseslot > cbase+1); - J->baseslot -= cbase+1; - J->base -= cbase+1; - } else if (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))) { - /* Return to lower frame would leave the loop in a root trace. */ - lj_trace_err(J, LJ_TRERR_LLEAVE); - } else { /* Return to lower frame. Guard for the target we return to. */ - TRef trpt = lj_ir_kgc(J, obj2gco(pt), IRT_PROTO); - TRef trpc = lj_ir_kptr(J, (void *)frame_pc(frame)); - emitir(IRTG(IR_RETF, IRT_P32), trpt, trpc); - J->retdepth++; - J->needsnap = 1; - lua_assert(J->baseslot == 1); - /* Shift result slots up and clear the slots of the new frame below. */ - memmove(J->base + cbase, J->base-1, sizeof(TRef)*nresults); - memset(J->base-1, 0, sizeof(TRef)*(cbase+1)); - } - } else if (frame_iscont(frame)) { /* Return to continuation frame. */ - ASMFunction cont = frame_contf(frame); - BCReg cbase = (BCReg)frame_delta(frame); - if ((J->framedepth -= 2) < 0) - lj_trace_err(J, LJ_TRERR_NYIRETL); - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - J->maxslot = cbase-2; - if (cont == lj_cont_ra) { - /* Copy result to destination slot. */ - BCReg dst = bc_a(*(frame_contpc(frame)-1)); - J->base[dst] = gotresults ? J->base[cbase+rbase] : TREF_NIL; - if (dst >= J->maxslot) J->maxslot = dst+1; - } else if (cont == lj_cont_nop) { - /* Nothing to do here. */ - } else if (cont == lj_cont_cat) { - lua_assert(0); - } else { - /* Result type already specialized. */ - lua_assert(cont == lj_cont_condf || cont == lj_cont_condt); - } - } else { - lj_trace_err(J, LJ_TRERR_NYIRETL); /* NYI: handle return to C frame. */ - } - lua_assert(J->baseslot >= 1); -} - -/* -- Metamethod handling ------------------------------------------------- */ - -/* Prepare to record call to metamethod. */ -static BCReg rec_mm_prep(jit_State *J, ASMFunction cont) -{ - BCReg s, top = curr_proto(J->L)->framesize; - TRef trcont; - setcont(&J->L->base[top], cont); -#if LJ_64 - trcont = lj_ir_kptr(J, (void *)((int64_t)cont - (int64_t)lj_vm_asm_begin)); -#else - trcont = lj_ir_kptr(J, (void *)cont); -#endif - J->base[top] = trcont | TREF_CONT; - J->framedepth++; - for (s = J->maxslot; s < top; s++) - J->base[s] = 0; /* Clear frame gap to avoid resurrecting previous refs. */ - return top+1; -} - -/* Record metamethod lookup. */ -int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm) -{ - RecordIndex mix; - GCtab *mt; - if (tref_istab(ix->tab)) { - mt = tabref(tabV(&ix->tabv)->metatable); - mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META); - } else if (tref_isudata(ix->tab)) { - int udtype = udataV(&ix->tabv)->udtype; - mt = tabref(udataV(&ix->tabv)->metatable); - /* The metatables of special userdata objects are treated as immutable. */ - if (udtype != UDTYPE_USERDATA) { - cTValue *mo; - if (LJ_HASFFI && udtype == UDTYPE_FFI_CLIB) { - /* Specialize to the C library namespace object. */ - emitir(IRTG(IR_EQ, IRT_P32), ix->tab, lj_ir_kptr(J, udataV(&ix->tabv))); - } else { - /* Specialize to the type of userdata. */ - TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), ix->tab, IRFL_UDATA_UDTYPE); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, udtype)); - } - immutable_mt: - mo = lj_tab_getstr(mt, mmname_str(J2G(J), mm)); - if (!mo || tvisnil(mo)) - return 0; /* No metamethod. */ - /* Treat metamethod or index table as immutable, too. */ - if (!(tvisfunc(mo) || tvistab(mo))) - lj_trace_err(J, LJ_TRERR_BADTYPE); - copyTV(J->L, &ix->mobjv, mo); - ix->mobj = lj_ir_kgc(J, gcV(mo), tvisfunc(mo) ? IRT_FUNC : IRT_TAB); - ix->mtv = mt; - ix->mt = TREF_NIL; /* Dummy value for comparison semantics. */ - return 1; /* Got metamethod or index table. */ - } - mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_UDATA_META); - } else { - /* Specialize to base metatable. Must flush mcode in lua_setmetatable(). */ - mt = tabref(basemt_obj(J2G(J), &ix->tabv)); - if (mt == NULL) { - ix->mt = TREF_NIL; - return 0; /* No metamethod. */ - } - /* The cdata metatable is treated as immutable. */ - if (LJ_HASFFI && tref_iscdata(ix->tab)) goto immutable_mt; - ix->mt = mix.tab = lj_ir_ktab(J, mt); - goto nocheck; - } - ix->mt = mt ? mix.tab : TREF_NIL; - emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mix.tab, lj_ir_knull(J, IRT_TAB)); -nocheck: - if (mt) { - GCstr *mmstr = mmname_str(J2G(J), mm); - cTValue *mo = lj_tab_getstr(mt, mmstr); - if (mo && !tvisnil(mo)) - copyTV(J->L, &ix->mobjv, mo); - ix->mtv = mt; - settabV(J->L, &mix.tabv, mt); - setstrV(J->L, &mix.keyv, mmstr); - mix.key = lj_ir_kstr(J, mmstr); - mix.val = 0; - mix.idxchain = 0; - ix->mobj = lj_record_idx(J, &mix); - return !tref_isnil(ix->mobj); /* 1 if metamethod found, 0 if not. */ - } - return 0; /* No metamethod. */ -} - -/* Record call to arithmetic metamethod. */ -static TRef rec_mm_arith(jit_State *J, RecordIndex *ix, MMS mm) -{ - /* Set up metamethod call first to save ix->tab and ix->tabv. */ - BCReg func = rec_mm_prep(J, lj_cont_ra); - TRef *base = J->base + func; - TValue *basev = J->L->base + func; - base[1] = ix->tab; base[2] = ix->key; - copyTV(J->L, basev+1, &ix->tabv); - copyTV(J->L, basev+2, &ix->keyv); - if (!lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ - if (mm != MM_unm) { - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, &ix->keyv); - if (lj_record_mm_lookup(J, ix, mm)) /* Lookup mm on 2nd operand. */ - goto ok; - } - lj_trace_err(J, LJ_TRERR_NOMM); - } -ok: - base[0] = ix->mobj; - copyTV(J->L, basev+0, &ix->mobjv); - lj_record_call(J, func, 2); - return 0; /* No result yet. */ -} - -/* Record call to __len metamethod. */ -static TRef rec_mm_len(jit_State *J, TRef tr, TValue *tv) -{ - RecordIndex ix; - ix.tab = tr; - copyTV(J->L, &ix.tabv, tv); - if (lj_record_mm_lookup(J, &ix, MM_len)) { - BCReg func = rec_mm_prep(J, lj_cont_ra); - TRef *base = J->base + func; - TValue *basev = J->L->base + func; - base[0] = ix.mobj; copyTV(J->L, basev+0, &ix.mobjv); - base[1] = tr; copyTV(J->L, basev+1, tv); -#if LJ_52 - base[2] = tr; copyTV(J->L, basev+2, tv); -#else - base[2] = TREF_NIL; setnilV(basev+2); -#endif - lj_record_call(J, func, 2); - } else { - if (LJ_52 && tref_istab(tr)) - return lj_ir_call(J, IRCALL_lj_tab_len, tr); - lj_trace_err(J, LJ_TRERR_NOMM); - } - return 0; /* No result yet. */ -} - -/* Call a comparison metamethod. */ -static void rec_mm_callcomp(jit_State *J, RecordIndex *ix, int op) -{ - BCReg func = rec_mm_prep(J, (op&1) ? lj_cont_condf : lj_cont_condt); - TRef *base = J->base + func; - TValue *tv = J->L->base + func; - base[0] = ix->mobj; base[1] = ix->val; base[2] = ix->key; - copyTV(J->L, tv+0, &ix->mobjv); - copyTV(J->L, tv+1, &ix->valv); - copyTV(J->L, tv+2, &ix->keyv); - lj_record_call(J, func, 2); -} - -/* Record call to equality comparison metamethod (for tab and udata only). */ -static void rec_mm_equal(jit_State *J, RecordIndex *ix, int op) -{ - ix->tab = ix->val; - copyTV(J->L, &ix->tabv, &ix->valv); - if (lj_record_mm_lookup(J, ix, MM_eq)) { /* Lookup mm on 1st operand. */ - cTValue *bv; - TRef mo1 = ix->mobj; - TValue mo1v; - copyTV(J->L, &mo1v, &ix->mobjv); - /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */ - bv = &ix->keyv; - if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else { /* Lookup metamethod on 2nd operand and compare both. */ - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, bv); - if (!lj_record_mm_lookup(J, ix, MM_eq) || - lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv)) - return; - } - rec_mm_callcomp(J, ix, op); - } -} - -/* Record call to ordered comparison metamethods (for arbitrary objects). */ -static void rec_mm_comp(jit_State *J, RecordIndex *ix, int op) -{ - ix->tab = ix->val; - copyTV(J->L, &ix->tabv, &ix->valv); - while (1) { - MMS mm = (op & 2) ? MM_le : MM_lt; /* Try __le + __lt or only __lt. */ -#if LJ_52 - if (!lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, &ix->keyv); - if (!lj_record_mm_lookup(J, ix, mm)) /* Lookup mm on 2nd operand. */ - goto nomatch; - } - rec_mm_callcomp(J, ix, op); - return; -#else - if (lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ - cTValue *bv; - TRef mo1 = ix->mobj; - TValue mo1v; - copyTV(J->L, &mo1v, &ix->mobjv); - /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */ - bv = &ix->keyv; - if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else { /* Lookup metamethod on 2nd operand and compare both. */ - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, bv); - if (!lj_record_mm_lookup(J, ix, mm) || - lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv)) - goto nomatch; - } - rec_mm_callcomp(J, ix, op); - return; - } -#endif - nomatch: - /* Lookup failed. Retry with __lt and swapped operands. */ - if (!(op & 2)) break; /* Already at __lt. Interpreter will throw. */ - ix->tab = ix->key; ix->key = ix->val; ix->val = ix->tab; - copyTV(J->L, &ix->tabv, &ix->keyv); - copyTV(J->L, &ix->keyv, &ix->valv); - copyTV(J->L, &ix->valv, &ix->tabv); - op ^= 3; - } -} - -#if LJ_HASFFI -/* Setup call to cdata comparison metamethod. */ -static void rec_mm_comp_cdata(jit_State *J, RecordIndex *ix, int op, MMS mm) -{ - lj_snap_add(J); - if (tref_iscdata(ix->val)) { - ix->tab = ix->val; - copyTV(J->L, &ix->tabv, &ix->valv); - } else { - lua_assert(tref_iscdata(ix->key)); - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, &ix->keyv); - } - lj_record_mm_lookup(J, ix, mm); - rec_mm_callcomp(J, ix, op); -} -#endif - -/* -- Indexed access ------------------------------------------------------ */ - -/* Record bounds-check. */ -static void rec_idx_abc(jit_State *J, TRef asizeref, TRef ikey, uint32_t asize) -{ - /* Try to emit invariant bounds checks. */ - if ((J->flags & (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) == - (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) { - IRRef ref = tref_ref(ikey); - IRIns *ir = IR(ref); - int32_t ofs = 0; - IRRef ofsref = 0; - /* Handle constant offsets. */ - if (ir->o == IR_ADD && irref_isk(ir->op2)) { - ofsref = ir->op2; - ofs = IR(ofsref)->i; - ref = ir->op1; - ir = IR(ref); - } - /* Got scalar evolution analysis results for this reference? */ - if (ref == J->scev.idx) { - int32_t stop; - lua_assert(irt_isint(J->scev.t) && ir->o == IR_SLOAD); - stop = numberVint(&(J->L->base - J->baseslot)[ir->op1 + FORL_STOP]); - /* Runtime value for stop of loop is within bounds? */ - if ((int64_t)stop + ofs < (int64_t)asize) { - /* Emit invariant bounds check for stop. */ - emitir(IRTG(IR_ABC, IRT_P32), asizeref, ofs == 0 ? J->scev.stop : - emitir(IRTI(IR_ADD), J->scev.stop, ofsref)); - /* Emit invariant bounds check for start, if not const or negative. */ - if (!(J->scev.dir && J->scev.start && - (int64_t)IR(J->scev.start)->i + ofs >= 0)) - emitir(IRTG(IR_ABC, IRT_P32), asizeref, ikey); - return; - } - } - } - emitir(IRTGI(IR_ABC), asizeref, ikey); /* Emit regular bounds check. */ -} - -/* Record indexed key lookup. */ -static TRef rec_idx_key(jit_State *J, RecordIndex *ix) -{ - TRef key; - GCtab *t = tabV(&ix->tabv); - ix->oldv = lj_tab_get(J->L, t, &ix->keyv); /* Lookup previous value. */ - - /* Integer keys are looked up in the array part first. */ - key = ix->key; - if (tref_isnumber(key)) { - int32_t k = numberVint(&ix->keyv); - if (!tvisint(&ix->keyv) && numV(&ix->keyv) != (lua_Number)k) - k = LJ_MAX_ASIZE; - if ((MSize)k < LJ_MAX_ASIZE) { /* Potential array key? */ - TRef ikey = lj_opt_narrow_index(J, key); - TRef asizeref = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE); - if ((MSize)k < t->asize) { /* Currently an array key? */ - TRef arrayref; - rec_idx_abc(J, asizeref, ikey, t->asize); - arrayref = emitir(IRT(IR_FLOAD, IRT_P32), ix->tab, IRFL_TAB_ARRAY); - return emitir(IRT(IR_AREF, IRT_P32), arrayref, ikey); - } else { /* Currently not in array (may be an array extension)? */ - emitir(IRTGI(IR_ULE), asizeref, ikey); /* Inv. bounds check. */ - if (k == 0 && tref_isk(key)) - key = lj_ir_knum_zero(J); /* Canonicalize 0 or +-0.0 to +0.0. */ - /* And continue with the hash lookup. */ - } - } else if (!tref_isk(key)) { - /* We can rule out const numbers which failed the integerness test - ** above. But all other numbers are potential array keys. - */ - if (t->asize == 0) { /* True sparse tables have an empty array part. */ - /* Guard that the array part stays empty. */ - TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE); - emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0)); - } else { - lj_trace_err(J, LJ_TRERR_NYITMIX); - } - } - } - - /* Otherwise the key is located in the hash part. */ - if (t->hmask == 0) { /* Shortcut for empty hash part. */ - /* Guard that the hash part stays empty. */ - TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK); - emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0)); - return lj_ir_kkptr(J, niltvg(J2G(J))); - } - if (tref_isinteger(key)) /* Hash keys are based on numbers, not ints. */ - key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT); - if (tref_isk(key)) { - /* Optimize lookup of constant hash keys. */ - MSize hslot = (MSize)((char *)ix->oldv - (char *)&noderef(t->node)[0].val); - if (t->hmask > 0 && hslot <= t->hmask*(MSize)sizeof(Node) && - hslot <= 65535*(MSize)sizeof(Node)) { - TRef node, kslot; - TRef hm = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK); - emitir(IRTGI(IR_EQ), hm, lj_ir_kint(J, (int32_t)t->hmask)); - node = emitir(IRT(IR_FLOAD, IRT_P32), ix->tab, IRFL_TAB_NODE); - kslot = lj_ir_kslot(J, key, hslot / sizeof(Node)); - return emitir(IRTG(IR_HREFK, IRT_P32), node, kslot); - } - } - /* Fall back to a regular hash lookup. */ - return emitir(IRT(IR_HREF, IRT_P32), ix->tab, key); -} - -/* Determine whether a key is NOT one of the fast metamethod names. */ -static int nommstr(jit_State *J, TRef key) -{ - if (tref_isstr(key)) { - if (tref_isk(key)) { - GCstr *str = ir_kstr(IR(tref_ref(key))); - uint32_t mm; - for (mm = 0; mm <= MM_FAST; mm++) - if (mmname_str(J2G(J), mm) == str) - return 0; /* MUST be one the fast metamethod names. */ - } else { - return 0; /* Variable string key MAY be a metamethod name. */ - } - } - return 1; /* CANNOT be a metamethod name. */ -} - -/* Record indexed load/store. */ -TRef lj_record_idx(jit_State *J, RecordIndex *ix) -{ - TRef xref; - IROp xrefop, loadop; - cTValue *oldv; - - while (!tref_istab(ix->tab)) { /* Handle non-table lookup. */ - /* Never call raw lj_record_idx() on non-table. */ - lua_assert(ix->idxchain != 0); - if (!lj_record_mm_lookup(J, ix, ix->val ? MM_newindex : MM_index)) - lj_trace_err(J, LJ_TRERR_NOMM); - handlemm: - if (tref_isfunc(ix->mobj)) { /* Handle metamethod call. */ - BCReg func = rec_mm_prep(J, ix->val ? lj_cont_nop : lj_cont_ra); - TRef *base = J->base + func; - TValue *tv = J->L->base + func; - base[0] = ix->mobj; base[1] = ix->tab; base[2] = ix->key; - setfuncV(J->L, tv+0, funcV(&ix->mobjv)); - copyTV(J->L, tv+1, &ix->tabv); - copyTV(J->L, tv+2, &ix->keyv); - if (ix->val) { - base[3] = ix->val; - copyTV(J->L, tv+3, &ix->valv); - lj_record_call(J, func, 3); /* mobj(tab, key, val) */ - return 0; - } else { - lj_record_call(J, func, 2); /* res = mobj(tab, key) */ - return 0; /* No result yet. */ - } - } - /* Otherwise retry lookup with metaobject. */ - ix->tab = ix->mobj; - copyTV(J->L, &ix->tabv, &ix->mobjv); - if (--ix->idxchain == 0) - lj_trace_err(J, LJ_TRERR_IDXLOOP); - } - - /* First catch nil and NaN keys for tables. */ - if (tvisnil(&ix->keyv) || (tvisnum(&ix->keyv) && tvisnan(&ix->keyv))) { - if (ix->val) /* Better fail early. */ - lj_trace_err(J, LJ_TRERR_STORENN); - if (tref_isk(ix->key)) { - if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_index)) - goto handlemm; - return TREF_NIL; - } - } - - /* Record the key lookup. */ - xref = rec_idx_key(J, ix); - xrefop = IR(tref_ref(xref))->o; - loadop = xrefop == IR_AREF ? IR_ALOAD : IR_HLOAD; - /* The lj_meta_tset() inconsistency is gone, but better play safe. */ - oldv = xrefop == IR_KKPTR ? (cTValue *)ir_kptr(IR(tref_ref(xref))) : ix->oldv; - - if (ix->val == 0) { /* Indexed load */ - IRType t = itype2irt(oldv); - TRef res; - if (oldv == niltvg(J2G(J))) { - emitir(IRTG(IR_EQ, IRT_P32), xref, lj_ir_kkptr(J, niltvg(J2G(J)))); - res = TREF_NIL; - } else { - res = emitir(IRTG(loadop, t), xref, 0); - } - if (t == IRT_NIL && ix->idxchain && lj_record_mm_lookup(J, ix, MM_index)) - goto handlemm; - if (irtype_ispri(t)) res = TREF_PRI(t); /* Canonicalize primitives. */ - return res; - } else { /* Indexed store. */ - GCtab *mt = tabref(tabV(&ix->tabv)->metatable); - int keybarrier = tref_isgcv(ix->key) && !tref_isnil(ix->val); - if (tvisnil(oldv)) { /* Previous value was nil? */ - /* Need to duplicate the hasmm check for the early guards. */ - int hasmm = 0; - if (ix->idxchain && mt) { - cTValue *mo = lj_tab_getstr(mt, mmname_str(J2G(J), MM_newindex)); - hasmm = mo && !tvisnil(mo); - } - if (hasmm) - emitir(IRTG(loadop, IRT_NIL), xref, 0); /* Guard for nil value. */ - else if (xrefop == IR_HREF) - emitir(IRTG(oldv == niltvg(J2G(J)) ? IR_EQ : IR_NE, IRT_P32), - xref, lj_ir_kkptr(J, niltvg(J2G(J)))); - if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_newindex)) { - lua_assert(hasmm); - goto handlemm; - } - lua_assert(!hasmm); - if (oldv == niltvg(J2G(J))) { /* Need to insert a new key. */ - TRef key = ix->key; - if (tref_isinteger(key)) /* NEWREF needs a TValue as a key. */ - key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT); - xref = emitir(IRT(IR_NEWREF, IRT_P32), ix->tab, key); - keybarrier = 0; /* NEWREF already takes care of the key barrier. */ - } - } else if (!lj_opt_fwd_wasnonnil(J, loadop, tref_ref(xref))) { - /* Cannot derive that the previous value was non-nil, must do checks. */ - if (xrefop == IR_HREF) /* Guard against store to niltv. */ - emitir(IRTG(IR_NE, IRT_P32), xref, lj_ir_kkptr(J, niltvg(J2G(J)))); - if (ix->idxchain) { /* Metamethod lookup required? */ - /* A check for NULL metatable is cheaper (hoistable) than a load. */ - if (!mt) { - TRef mtref = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META); - emitir(IRTG(IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB)); - } else { - IRType t = itype2irt(oldv); - emitir(IRTG(loadop, t), xref, 0); /* Guard for non-nil value. */ - } - } - } else { - keybarrier = 0; /* Previous non-nil value kept the key alive. */ - } - /* Convert int to number before storing. */ - if (!LJ_DUALNUM && tref_isinteger(ix->val)) - ix->val = emitir(IRTN(IR_CONV), ix->val, IRCONV_NUM_INT); - emitir(IRT(loadop+IRDELTA_L2S, tref_type(ix->val)), xref, ix->val); - if (keybarrier || tref_isgcv(ix->val)) - emitir(IRT(IR_TBAR, IRT_NIL), ix->tab, 0); - /* Invalidate neg. metamethod cache for stores with certain string keys. */ - if (!nommstr(J, ix->key)) { - TRef fref = emitir(IRT(IR_FREF, IRT_P32), ix->tab, IRFL_TAB_NOMM); - emitir(IRT(IR_FSTORE, IRT_U8), fref, lj_ir_kint(J, 0)); - } - J->needsnap = 1; - return 0; - } -} - -/* -- Upvalue access ------------------------------------------------------ */ - -/* Check whether upvalue is immutable and ok to constify. */ -static int rec_upvalue_constify(jit_State *J, GCupval *uvp) -{ - if (uvp->immutable) { - cTValue *o = uvval(uvp); - /* Don't constify objects that may retain large amounts of memory. */ -#if LJ_HASFFI - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - if (!cdataisv(cd) && !(cd->marked & LJ_GC_CDATA_FIN)) { - CType *ct = ctype_raw(ctype_ctsG(J2G(J)), cd->ctypeid); - if (!ctype_hassize(ct->info) || ct->size <= 16) - return 1; - } - return 0; - } -#else - UNUSED(J); -#endif - if (!(tvistab(o) || tvisudata(o) || tvisthread(o))) - return 1; - } - return 0; -} - -/* Record upvalue load/store. */ -static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val) -{ - GCupval *uvp = &gcref(J->fn->l.uvptr[uv])->uv; - TRef fn = getcurrf(J); - IRRef uref; - int needbarrier = 0; - if (rec_upvalue_constify(J, uvp)) { /* Try to constify immutable upvalue. */ - TRef tr, kfunc; - lua_assert(val == 0); - if (!tref_isk(fn)) { /* Late specialization of current function. */ - if (J->pt->flags >= PROTO_CLC_POLY) - goto noconstify; - kfunc = lj_ir_kfunc(J, J->fn); - emitir(IRTG(IR_EQ, IRT_FUNC), fn, kfunc); - J->base[-1] = TREF_FRAME | kfunc; - fn = kfunc; - } - tr = lj_record_constify(J, uvval(uvp)); - if (tr) - return tr; - } -noconstify: - /* Note: this effectively limits LJ_MAX_UPVAL to 127. */ - uv = (uv << 8) | (hashrot(uvp->dhash, uvp->dhash + HASH_BIAS) & 0xff); - if (!uvp->closed) { - /* In current stack? */ - if (uvval(uvp) >= tvref(J->L->stack) && - uvval(uvp) < tvref(J->L->maxstack)) { - int32_t slot = (int32_t)(uvval(uvp) - (J->L->base - J->baseslot)); - if (slot >= 0) { /* Aliases an SSA slot? */ - slot -= (int32_t)J->baseslot; /* Note: slot number may be negative! */ - /* NYI: add IR to guard that it's still aliasing the same slot. */ - if (val == 0) { - return getslot(J, slot); - } else { - J->base[slot] = val; - if (slot >= (int32_t)J->maxslot) J->maxslot = (BCReg)(slot+1); - return 0; - } - } - } - uref = tref_ref(emitir(IRTG(IR_UREFO, IRT_P32), fn, uv)); - } else { - needbarrier = 1; - uref = tref_ref(emitir(IRTG(IR_UREFC, IRT_P32), fn, uv)); - } - if (val == 0) { /* Upvalue load */ - IRType t = itype2irt(uvval(uvp)); - TRef res = emitir(IRTG(IR_ULOAD, t), uref, 0); - if (irtype_ispri(t)) res = TREF_PRI(t); /* Canonicalize primitive refs. */ - return res; - } else { /* Upvalue store. */ - /* Convert int to number before storing. */ - if (!LJ_DUALNUM && tref_isinteger(val)) - val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT); - emitir(IRT(IR_USTORE, tref_type(val)), uref, val); - if (needbarrier && tref_isgcv(val)) - emitir(IRT(IR_OBAR, IRT_NIL), uref, val); - J->needsnap = 1; - return 0; - } -} - -/* -- Record calls to Lua functions --------------------------------------- */ - -/* Check unroll limits for calls. */ -static void check_call_unroll(jit_State *J, TraceNo lnk) -{ - cTValue *frame = J->L->base - 1; - void *pc = mref(frame_func(frame)->l.pc, void); - int32_t depth = J->framedepth; - int32_t count = 0; - if ((J->pt->flags & PROTO_VARARG)) depth--; /* Vararg frame still missing. */ - for (; depth > 0; depth--) { /* Count frames with same prototype. */ - frame = frame_prev(frame); - if (mref(frame_func(frame)->l.pc, void) == pc) - count++; - } - if (J->pc == J->startpc) { - if (count + J->tailcalled > J->param[JIT_P_recunroll]) { - J->pc++; - if (J->framedepth + J->retdepth == 0) - rec_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno); /* Tail-recursion. */ - else - rec_stop(J, LJ_TRLINK_UPREC, J->cur.traceno); /* Up-recursion. */ - } - } else { - if (count > J->param[JIT_P_callunroll]) { - if (lnk) { /* Possible tail- or up-recursion. */ - lj_trace_flush(J, lnk); /* Flush trace that only returns. */ - /* Set a small, pseudo-random hotcount for a quick retry of JFUNC*. */ - hotcount_set(J2GG(J), J->pc+1, LJ_PRNG_BITS(J, 4)); - } - lj_trace_err(J, LJ_TRERR_CUNROLL); - } - } -} - -/* Record Lua function setup. */ -static void rec_func_setup(jit_State *J) -{ - GCproto *pt = J->pt; - BCReg s, numparams = pt->numparams; - if ((pt->flags & PROTO_NOJIT)) - lj_trace_err(J, LJ_TRERR_CJITOFF); - if (J->baseslot + pt->framesize >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); - /* Fill up missing parameters with nil. */ - for (s = J->maxslot; s < numparams; s++) - J->base[s] = TREF_NIL; - /* The remaining slots should never be read before they are written. */ - J->maxslot = numparams; -} - -/* Record Lua vararg function setup. */ -static void rec_func_vararg(jit_State *J) -{ - GCproto *pt = J->pt; - BCReg s, fixargs, vframe = J->maxslot+1; - lua_assert((pt->flags & PROTO_VARARG)); - if (J->baseslot + vframe + pt->framesize >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); - J->base[vframe-1] = J->base[-1]; /* Copy function up. */ - /* Copy fixarg slots up and set their original slots to nil. */ - fixargs = pt->numparams < J->maxslot ? pt->numparams : J->maxslot; - for (s = 0; s < fixargs; s++) { - J->base[vframe+s] = J->base[s]; - J->base[s] = TREF_NIL; - } - J->maxslot = fixargs; - J->framedepth++; - J->base += vframe; - J->baseslot += vframe; -} - -/* Record entry to a Lua function. */ -static void rec_func_lua(jit_State *J) -{ - rec_func_setup(J); - check_call_unroll(J, 0); -} - -/* Record entry to an already compiled function. */ -static void rec_func_jit(jit_State *J, TraceNo lnk) -{ - GCtrace *T; - rec_func_setup(J); - T = traceref(J, lnk); - if (T->linktype == LJ_TRLINK_RETURN) { /* Trace returns to interpreter? */ - check_call_unroll(J, lnk); - /* Temporarily unpatch JFUNC* to continue recording across function. */ - J->patchins = *J->pc; - J->patchpc = (BCIns *)J->pc; - *J->patchpc = T->startins; - return; - } - J->instunroll = 0; /* Cannot continue across a compiled function. */ - if (J->pc == J->startpc && J->framedepth + J->retdepth == 0) - rec_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno); /* Extra tail-recursion. */ - else - rec_stop(J, LJ_TRLINK_ROOT, lnk); /* Link to the function. */ -} - -/* -- Vararg handling ----------------------------------------------------- */ - -/* Detect y = select(x, ...) idiom. */ -static int select_detect(jit_State *J) -{ - BCIns ins = J->pc[1]; - if (bc_op(ins) == BC_CALLM && bc_b(ins) == 2 && bc_c(ins) == 1) { - cTValue *func = &J->L->base[bc_a(ins)]; - if (tvisfunc(func) && funcV(func)->c.ffid == FF_select) - return 1; - } - return 0; -} - -/* Record vararg instruction. */ -static void rec_varg(jit_State *J, BCReg dst, ptrdiff_t nresults) -{ - int32_t numparams = J->pt->numparams; - ptrdiff_t nvararg = frame_delta(J->L->base-1) - numparams - 1; - lua_assert(frame_isvarg(J->L->base-1)); - if (J->framedepth > 0) { /* Simple case: varargs defined on-trace. */ - ptrdiff_t i; - if (nvararg < 0) nvararg = 0; - if (nresults == -1) { - nresults = nvararg; - J->maxslot = dst + (BCReg)nvararg; - } else if (dst + nresults > J->maxslot) { - J->maxslot = dst + (BCReg)nresults; - } - for (i = 0; i < nresults; i++) { - J->base[dst+i] = i < nvararg ? J->base[i - nvararg - 1] : TREF_NIL; - lua_assert(J->base[dst+i] != 0); - } - } else { /* Unknown number of varargs passed to trace. */ - TRef fr = emitir(IRTI(IR_SLOAD), 0, IRSLOAD_READONLY|IRSLOAD_FRAME); - int32_t frofs = 8*(1+numparams)+FRAME_VARG; - if (nresults >= 0) { /* Known fixed number of results. */ - ptrdiff_t i; - if (nvararg > 0) { - ptrdiff_t nload = nvararg >= nresults ? nresults : nvararg; - TRef vbase; - if (nvararg >= nresults) - emitir(IRTGI(IR_GE), fr, lj_ir_kint(J, frofs+8*(int32_t)nresults)); - else - emitir(IRTGI(IR_EQ), fr, lj_ir_kint(J, frame_ftsz(J->L->base-1))); - vbase = emitir(IRTI(IR_SUB), REF_BASE, fr); - vbase = emitir(IRT(IR_ADD, IRT_P32), vbase, lj_ir_kint(J, frofs-8)); - for (i = 0; i < nload; i++) { - IRType t = itype2irt(&J->L->base[i-1-nvararg]); - TRef aref = emitir(IRT(IR_AREF, IRT_P32), - vbase, lj_ir_kint(J, (int32_t)i)); - TRef tr = emitir(IRTG(IR_VLOAD, t), aref, 0); - if (irtype_ispri(t)) tr = TREF_PRI(t); /* Canonicalize primitives. */ - J->base[dst+i] = tr; - } - } else { - emitir(IRTGI(IR_LE), fr, lj_ir_kint(J, frofs)); - nvararg = 0; - } - for (i = nvararg; i < nresults; i++) - J->base[dst+i] = TREF_NIL; - if (dst + (BCReg)nresults > J->maxslot) - J->maxslot = dst + (BCReg)nresults; - } else if (select_detect(J)) { /* y = select(x, ...) */ - TRef tridx = J->base[dst-1]; - TRef tr = TREF_NIL; - ptrdiff_t idx = lj_ffrecord_select_mode(J, tridx, &J->L->base[dst-1]); - if (idx < 0) goto nyivarg; - if (idx != 0 && !tref_isinteger(tridx)) - tridx = emitir(IRTGI(IR_CONV), tridx, IRCONV_INT_NUM|IRCONV_INDEX); - if (idx != 0 && tref_isk(tridx)) { - emitir(IRTGI(idx <= nvararg ? IR_GE : IR_LT), - fr, lj_ir_kint(J, frofs+8*(int32_t)idx)); - frofs -= 8; /* Bias for 1-based index. */ - } else if (idx <= nvararg) { /* Compute size. */ - TRef tmp = emitir(IRTI(IR_ADD), fr, lj_ir_kint(J, -frofs)); - if (numparams) - emitir(IRTGI(IR_GE), tmp, lj_ir_kint(J, 0)); - tr = emitir(IRTI(IR_BSHR), tmp, lj_ir_kint(J, 3)); - if (idx != 0) { - tridx = emitir(IRTI(IR_ADD), tridx, lj_ir_kint(J, -1)); - rec_idx_abc(J, tr, tridx, (uint32_t)nvararg); - } - } else { - TRef tmp = lj_ir_kint(J, frofs); - if (idx != 0) { - TRef tmp2 = emitir(IRTI(IR_BSHL), tridx, lj_ir_kint(J, 3)); - tmp = emitir(IRTI(IR_ADD), tmp2, tmp); - } else { - tr = lj_ir_kint(J, 0); - } - emitir(IRTGI(IR_LT), fr, tmp); - } - if (idx != 0 && idx <= nvararg) { - IRType t; - TRef aref, vbase = emitir(IRTI(IR_SUB), REF_BASE, fr); - vbase = emitir(IRT(IR_ADD, IRT_P32), vbase, lj_ir_kint(J, frofs-8)); - t = itype2irt(&J->L->base[idx-2-nvararg]); - aref = emitir(IRT(IR_AREF, IRT_P32), vbase, tridx); - tr = emitir(IRTG(IR_VLOAD, t), aref, 0); - if (irtype_ispri(t)) tr = TREF_PRI(t); /* Canonicalize primitives. */ - } - J->base[dst-2] = tr; - J->maxslot = dst-1; - J->bcskip = 2; /* Skip CALLM + select. */ - } else { - nyivarg: - setintV(&J->errinfo, BC_VARG); - lj_trace_err_info(J, LJ_TRERR_NYIBC); - } - } -} - -/* -- Record allocations -------------------------------------------------- */ - -static TRef rec_tnew(jit_State *J, uint32_t ah) -{ - uint32_t asize = ah & 0x7ff; - uint32_t hbits = ah >> 11; - if (asize == 0x7ff) asize = 0x801; - return emitir(IRTG(IR_TNEW, IRT_TAB), asize, hbits); -} - -/* -- Record bytecode ops ------------------------------------------------- */ - -/* Prepare for comparison. */ -static void rec_comp_prep(jit_State *J) -{ - /* Prevent merging with snapshot #0 (GC exit) since we fixup the PC. */ - if (J->cur.nsnap == 1 && J->cur.snap[0].ref == J->cur.nins) - emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0); - lj_snap_add(J); -} - -/* Fixup comparison. */ -static void rec_comp_fixup(jit_State *J, const BCIns *pc, int cond) -{ - BCIns jmpins = pc[1]; - const BCIns *npc = pc + 2 + (cond ? bc_j(jmpins) : 0); - SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; - /* Set PC to opposite target to avoid re-recording the comp. in side trace. */ - J->cur.snapmap[snap->mapofs + snap->nent] = SNAP_MKPC(npc); - J->needsnap = 1; - if (bc_a(jmpins) < J->maxslot) J->maxslot = bc_a(jmpins); - lj_snap_shrink(J); /* Shrink last snapshot if possible. */ -} - -/* Record the next bytecode instruction (_before_ it's executed). */ -void lj_record_ins(jit_State *J) -{ - cTValue *lbase; - RecordIndex ix; - const BCIns *pc; - BCIns ins; - BCOp op; - TRef ra, rb, rc; - - /* Perform post-processing action before recording the next instruction. */ - if (LJ_UNLIKELY(J->postproc != LJ_POST_NONE)) { - switch (J->postproc) { - case LJ_POST_FIXCOMP: /* Fixup comparison. */ - pc = frame_pc(&J2G(J)->tmptv); - rec_comp_fixup(J, pc, (!tvistruecond(&J2G(J)->tmptv2) ^ (bc_op(*pc)&1))); - /* fallthrough */ - case LJ_POST_FIXGUARD: /* Fixup and emit pending guard. */ - case LJ_POST_FIXGUARDSNAP: /* Fixup and emit pending guard and snapshot. */ - if (!tvistruecond(&J2G(J)->tmptv2)) { - J->fold.ins.o ^= 1; /* Flip guard to opposite. */ - if (J->postproc == LJ_POST_FIXGUARDSNAP) { - SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; - J->cur.snapmap[snap->mapofs+snap->nent-1]--; /* False -> true. */ - } - } - lj_opt_fold(J); /* Emit pending guard. */ - /* fallthrough */ - case LJ_POST_FIXBOOL: - if (!tvistruecond(&J2G(J)->tmptv2)) { - BCReg s; - TValue *tv = J->L->base; - for (s = 0; s < J->maxslot; s++) /* Fixup stack slot (if any). */ - if (J->base[s] == TREF_TRUE && tvisfalse(&tv[s])) { - J->base[s] = TREF_FALSE; - break; - } - } - break; - case LJ_POST_FIXCONST: - { - BCReg s; - TValue *tv = J->L->base; - for (s = 0; s < J->maxslot; s++) /* Constify stack slots (if any). */ - if (J->base[s] == TREF_NIL && !tvisnil(&tv[s])) - J->base[s] = lj_record_constify(J, &tv[s]); - } - break; - case LJ_POST_FFRETRY: /* Suppress recording of retried fast function. */ - if (bc_op(*J->pc) >= BC__MAX) - return; - break; - default: lua_assert(0); break; - } - J->postproc = LJ_POST_NONE; - } - - /* Need snapshot before recording next bytecode (e.g. after a store). */ - if (J->needsnap) { - J->needsnap = 0; - lj_snap_purge(J); - lj_snap_add(J); - J->mergesnap = 1; - } - - /* Skip some bytecodes. */ - if (LJ_UNLIKELY(J->bcskip > 0)) { - J->bcskip--; - return; - } - - /* Record only closed loops for root traces. */ - pc = J->pc; - if (J->framedepth == 0 && - (MSize)((char *)pc - (char *)J->bc_min) >= J->bc_extent) - lj_trace_err(J, LJ_TRERR_LLEAVE); - -#ifdef LUA_USE_ASSERT - rec_check_slots(J); - rec_check_ir(J); -#endif - - /* Keep a copy of the runtime values of var/num/str operands. */ -#define rav (&ix.valv) -#define rbv (&ix.tabv) -#define rcv (&ix.keyv) - - lbase = J->L->base; - ins = *pc; - op = bc_op(ins); - ra = bc_a(ins); - ix.val = 0; - switch (bcmode_a(op)) { - case BCMvar: - copyTV(J->L, rav, &lbase[ra]); ix.val = ra = getslot(J, ra); break; - default: break; /* Handled later. */ - } - rb = bc_b(ins); - rc = bc_c(ins); - switch (bcmode_b(op)) { - case BCMnone: rb = 0; rc = bc_d(ins); break; /* Upgrade rc to 'rd'. */ - case BCMvar: - copyTV(J->L, rbv, &lbase[rb]); ix.tab = rb = getslot(J, rb); break; - default: break; /* Handled later. */ - } - switch (bcmode_c(op)) { - case BCMvar: - copyTV(J->L, rcv, &lbase[rc]); ix.key = rc = getslot(J, rc); break; - case BCMpri: setitype(rcv, ~rc); ix.key = rc = TREF_PRI(IRT_NIL+rc); break; - case BCMnum: { cTValue *tv = proto_knumtv(J->pt, rc); - copyTV(J->L, rcv, tv); ix.key = rc = tvisint(tv) ? lj_ir_kint(J, intV(tv)) : - lj_ir_knumint(J, numV(tv)); } break; - case BCMstr: { GCstr *s = gco2str(proto_kgc(J->pt, ~(ptrdiff_t)rc)); - setstrV(J->L, rcv, s); ix.key = rc = lj_ir_kstr(J, s); } break; - default: break; /* Handled later. */ - } - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: -#if LJ_HASFFI - if (tref_iscdata(ra) || tref_iscdata(rc)) { - rec_mm_comp_cdata(J, &ix, op, ((int)op & 2) ? MM_le : MM_lt); - break; - } -#endif - /* Emit nothing for two numeric or string consts. */ - if (!(tref_isk2(ra,rc) && tref_isnumber_str(ra) && tref_isnumber_str(rc))) { - IRType ta = tref_isinteger(ra) ? IRT_INT : tref_type(ra); - IRType tc = tref_isinteger(rc) ? IRT_INT : tref_type(rc); - int irop; - if (ta != tc) { - /* Widen mixed number/int comparisons to number/number comparison. */ - if (ta == IRT_INT && tc == IRT_NUM) { - ra = emitir(IRTN(IR_CONV), ra, IRCONV_NUM_INT); - ta = IRT_NUM; - } else if (ta == IRT_NUM && tc == IRT_INT) { - rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); - } else if (LJ_52) { - ta = IRT_NIL; /* Force metamethod for different types. */ - } else if (!((ta == IRT_FALSE || ta == IRT_TRUE) && - (tc == IRT_FALSE || tc == IRT_TRUE))) { - break; /* Interpreter will throw for two different types. */ - } - } - rec_comp_prep(J); - irop = (int)op - (int)BC_ISLT + (int)IR_LT; - if (ta == IRT_NUM) { - if ((irop & 1)) irop ^= 4; /* ISGE/ISGT are unordered. */ - if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop)) - irop ^= 5; - } else if (ta == IRT_INT) { - if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop)) - irop ^= 1; - } else if (ta == IRT_STR) { - if (!lj_ir_strcmp(strV(rav), strV(rcv), (IROp)irop)) irop ^= 1; - ra = lj_ir_call(J, IRCALL_lj_str_cmp, ra, rc); - rc = lj_ir_kint(J, 0); - ta = IRT_INT; - } else { - rec_mm_comp(J, &ix, (int)op); - break; - } - emitir(IRTG(irop, ta), ra, rc); - rec_comp_fixup(J, J->pc, ((int)op ^ irop) & 1); - } - break; - - case BC_ISEQV: case BC_ISNEV: - case BC_ISEQS: case BC_ISNES: - case BC_ISEQN: case BC_ISNEN: - case BC_ISEQP: case BC_ISNEP: -#if LJ_HASFFI - if (tref_iscdata(ra) || tref_iscdata(rc)) { - rec_mm_comp_cdata(J, &ix, op, MM_eq); - break; - } -#endif - /* Emit nothing for two non-table, non-udata consts. */ - if (!(tref_isk2(ra, rc) && !(tref_istab(ra) || tref_isudata(ra)))) { - int diff; - rec_comp_prep(J); - diff = lj_record_objcmp(J, ra, rc, rav, rcv); - if (diff == 2 || !(tref_istab(ra) || tref_isudata(ra))) - rec_comp_fixup(J, J->pc, ((int)op & 1) == !diff); - else if (diff == 1) /* Only check __eq if different, but same type. */ - rec_mm_equal(J, &ix, (int)op); - } - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: - if ((op & 1) == tref_istruecond(rc)) - rc = 0; /* Don't store if condition is not true. */ - /* fallthrough */ - case BC_IST: case BC_ISF: /* Type specialization suffices. */ - if (bc_a(pc[1]) < J->maxslot) - J->maxslot = bc_a(pc[1]); /* Shrink used slots. */ - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_NOT: - /* Type specialization already forces const result. */ - rc = tref_istruecond(rc) ? TREF_FALSE : TREF_TRUE; - break; - - case BC_LEN: - if (tref_isstr(rc)) - rc = emitir(IRTI(IR_FLOAD), rc, IRFL_STR_LEN); - else if (!LJ_52 && tref_istab(rc)) - rc = lj_ir_call(J, IRCALL_lj_tab_len, rc); - else - rc = rec_mm_len(J, rc, rcv); - break; - - /* -- Arithmetic ops ---------------------------------------------------- */ - - case BC_UNM: - if (tref_isnumber_str(rc)) { - rc = lj_opt_narrow_unm(J, rc, rcv); - } else { - ix.tab = rc; - copyTV(J->L, &ix.tabv, rcv); - rc = rec_mm_arith(J, &ix, MM_unm); - } - break; - - case BC_ADDNV: case BC_SUBNV: case BC_MULNV: case BC_DIVNV: case BC_MODNV: - /* Swap rb/rc and rbv/rcv. rav is temp. */ - ix.tab = rc; ix.key = rc = rb; rb = ix.tab; - copyTV(J->L, rav, rbv); - copyTV(J->L, rbv, rcv); - copyTV(J->L, rcv, rav); - if (op == BC_MODNV) - goto recmod; - /* fallthrough */ - case BC_ADDVN: case BC_SUBVN: case BC_MULVN: case BC_DIVVN: - case BC_ADDVV: case BC_SUBVV: case BC_MULVV: case BC_DIVVV: { - MMS mm = bcmode_mm(op); - if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) - rc = lj_opt_narrow_arith(J, rb, rc, rbv, rcv, - (int)mm - (int)MM_add + (int)IR_ADD); - else - rc = rec_mm_arith(J, &ix, mm); - break; - } - - case BC_MODVN: case BC_MODVV: - recmod: - if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) - rc = lj_opt_narrow_mod(J, rb, rc, rcv); - else - rc = rec_mm_arith(J, &ix, MM_mod); - break; - - case BC_POW: - if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) - rc = lj_opt_narrow_pow(J, lj_ir_tonum(J, rb), rc, rcv); - else - rc = rec_mm_arith(J, &ix, MM_pow); - break; - - /* -- Constant and move ops --------------------------------------------- */ - - case BC_MOV: - /* Clear gap of method call to avoid resurrecting previous refs. */ - if (ra > J->maxslot) J->base[ra-1] = 0; - break; - case BC_KSTR: case BC_KNUM: case BC_KPRI: - break; - case BC_KSHORT: - rc = lj_ir_kint(J, (int32_t)(int16_t)rc); - break; - case BC_KNIL: - while (ra <= rc) - J->base[ra++] = TREF_NIL; - if (rc >= J->maxslot) J->maxslot = rc+1; - break; -#if LJ_HASFFI - case BC_KCDATA: - rc = lj_ir_kgc(J, proto_kgc(J->pt, ~(ptrdiff_t)rc), IRT_CDATA); - break; -#endif - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - rc = rec_upvalue(J, rc, 0); - break; - case BC_USETV: case BC_USETS: case BC_USETN: case BC_USETP: - rec_upvalue(J, ra, rc); - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_GGET: case BC_GSET: - settabV(J->L, &ix.tabv, tabref(J->fn->l.env)); - ix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), getcurrf(J), IRFL_FUNC_ENV); - ix.idxchain = LJ_MAX_IDXCHAIN; - rc = lj_record_idx(J, &ix); - break; - - case BC_TGETB: case BC_TSETB: - setintV(&ix.keyv, (int32_t)rc); - ix.key = lj_ir_kint(J, (int32_t)rc); - /* fallthrough */ - case BC_TGETV: case BC_TGETS: case BC_TSETV: case BC_TSETS: - ix.idxchain = LJ_MAX_IDXCHAIN; - rc = lj_record_idx(J, &ix); - break; - - case BC_TNEW: - rc = rec_tnew(J, rc); - break; - case BC_TDUP: - rc = emitir(IRTG(IR_TDUP, IRT_TAB), - lj_ir_ktab(J, gco2tab(proto_kgc(J->pt, ~(ptrdiff_t)rc))), 0); - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_ITERC: - J->base[ra] = getslot(J, ra-3); - J->base[ra+1] = getslot(J, ra-2); - J->base[ra+2] = getslot(J, ra-1); - { /* Do the actual copy now because lj_record_call needs the values. */ - TValue *b = &J->L->base[ra]; - copyTV(J->L, b, b-3); - copyTV(J->L, b+1, b-2); - copyTV(J->L, b+2, b-1); - } - lj_record_call(J, ra, (ptrdiff_t)rc-1); - break; - - /* L->top is set to L->base+ra+rc+NARGS-1+1. See lj_dispatch_ins(). */ - case BC_CALLM: - rc = (BCReg)(J->L->top - J->L->base) - ra; - /* fallthrough */ - case BC_CALL: - lj_record_call(J, ra, (ptrdiff_t)rc-1); - break; - - case BC_CALLMT: - rc = (BCReg)(J->L->top - J->L->base) - ra; - /* fallthrough */ - case BC_CALLT: - lj_record_tailcall(J, ra, (ptrdiff_t)rc-1); - break; - - case BC_VARG: - rec_varg(J, ra, (ptrdiff_t)rb-1); - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - /* L->top is set to L->base+ra+rc+NRESULTS-1, see lj_dispatch_ins(). */ - rc = (BCReg)(J->L->top - J->L->base) - ra + 1; - /* fallthrough */ - case BC_RET: case BC_RET0: case BC_RET1: - lj_record_ret(J, ra, (ptrdiff_t)rc-1); - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORI: - if (rec_for(J, pc, 0) != LOOPEV_LEAVE) - J->loopref = J->cur.nins; - break; - case BC_JFORI: - lua_assert(bc_op(pc[(ptrdiff_t)rc-BCBIAS_J]) == BC_JFORL); - if (rec_for(J, pc, 0) != LOOPEV_LEAVE) /* Link to existing loop. */ - rec_stop(J, LJ_TRLINK_ROOT, bc_d(pc[(ptrdiff_t)rc-BCBIAS_J])); - /* Continue tracing if the loop is not entered. */ - break; - - case BC_FORL: - rec_loop_interp(J, pc, rec_for(J, pc+((ptrdiff_t)rc-BCBIAS_J), 1)); - break; - case BC_ITERL: - rec_loop_interp(J, pc, rec_iterl(J, *pc)); - break; - case BC_LOOP: - rec_loop_interp(J, pc, rec_loop(J, ra)); - break; - - case BC_JFORL: - rec_loop_jit(J, rc, rec_for(J, pc+bc_j(traceref(J, rc)->startins), 1)); - break; - case BC_JITERL: - rec_loop_jit(J, rc, rec_iterl(J, traceref(J, rc)->startins)); - break; - case BC_JLOOP: - rec_loop_jit(J, rc, rec_loop(J, ra)); - break; - - case BC_IFORL: - case BC_IITERL: - case BC_ILOOP: - case BC_IFUNCF: - case BC_IFUNCV: - lj_trace_err(J, LJ_TRERR_BLACKL); - break; - - case BC_JMP: - if (ra < J->maxslot) - J->maxslot = ra; /* Shrink used slots. */ - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - rec_func_lua(J); - break; - case BC_JFUNCF: - rec_func_jit(J, rc); - break; - - case BC_FUNCV: - rec_func_vararg(J); - rec_func_lua(J); - break; - case BC_JFUNCV: - lua_assert(0); /* Cannot happen. No hotcall counting for varag funcs. */ - break; - - case BC_FUNCC: - case BC_FUNCCW: - lj_ffrecord_func(J); - break; - - default: - if (op >= BC__MAX) { - lj_ffrecord_func(J); - break; - } - /* fallthrough */ - case BC_ITERN: - case BC_ISNEXT: - case BC_CAT: - case BC_UCLO: - case BC_FNEW: - case BC_TSETM: - setintV(&J->errinfo, (int32_t)op); - lj_trace_err_info(J, LJ_TRERR_NYIBC); - break; - } - - /* rc == 0 if we have no result yet, e.g. pending __index metamethod call. */ - if (bcmode_a(op) == BCMdst && rc) { - J->base[ra] = rc; - if (ra >= J->maxslot) J->maxslot = ra+1; - } - -#undef rav -#undef rbv -#undef rcv - - /* Limit the number of recorded IR instructions. */ - if (J->cur.nins > REF_FIRST+(IRRef)J->param[JIT_P_maxrecord]) - lj_trace_err(J, LJ_TRERR_TRACEOV); -} - -/* -- Recording setup ----------------------------------------------------- */ - -/* Setup recording for a root trace started by a hot loop. */ -static const BCIns *rec_setup_root(jit_State *J) -{ - /* Determine the next PC and the bytecode range for the loop. */ - const BCIns *pcj, *pc = J->pc; - BCIns ins = *pc; - BCReg ra = bc_a(ins); - switch (bc_op(ins)) { - case BC_FORL: - J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); - pc += 1+bc_j(ins); - J->bc_min = pc; - break; - case BC_ITERL: - lua_assert(bc_op(pc[-1]) == BC_ITERC); - J->maxslot = ra + bc_b(pc[-1]) - 1; - J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); - pc += 1+bc_j(ins); - lua_assert(bc_op(pc[-1]) == BC_JMP); - J->bc_min = pc; - break; - case BC_LOOP: - /* Only check BC range for real loops, but not for "repeat until true". */ - pcj = pc + bc_j(ins); - ins = *pcj; - if (bc_op(ins) == BC_JMP && bc_j(ins) < 0) { - J->bc_min = pcj+1 + bc_j(ins); - J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); - } - J->maxslot = ra; - pc++; - break; - case BC_RET: - case BC_RET0: - case BC_RET1: - /* No bytecode range check for down-recursive root traces. */ - J->maxslot = ra + bc_d(ins); - break; - case BC_FUNCF: - /* No bytecode range check for root traces started by a hot call. */ - J->maxslot = J->pt->numparams; - pc++; - break; - default: - lua_assert(0); - break; - } - return pc; -} - -/* Setup for recording a new trace. */ -void lj_record_setup(jit_State *J) -{ - uint32_t i; - - /* Initialize state related to current trace. */ - memset(J->slot, 0, sizeof(J->slot)); - memset(J->chain, 0, sizeof(J->chain)); - memset(J->bpropcache, 0, sizeof(J->bpropcache)); - J->scev.idx = REF_NIL; - - J->baseslot = 1; /* Invoking function is at base[-1]. */ - J->base = J->slot + J->baseslot; - J->maxslot = 0; - J->framedepth = 0; - J->retdepth = 0; - - J->instunroll = J->param[JIT_P_instunroll]; - J->loopunroll = J->param[JIT_P_loopunroll]; - J->tailcalled = 0; - J->loopref = 0; - - J->bc_min = NULL; /* Means no limit. */ - J->bc_extent = ~(MSize)0; - - /* Emit instructions for fixed references. Also triggers initial IR alloc. */ - emitir_raw(IRT(IR_BASE, IRT_P32), J->parent, J->exitno); - for (i = 0; i <= 2; i++) { - IRIns *ir = IR(REF_NIL-i); - ir->i = 0; - ir->t.irt = (uint8_t)(IRT_NIL+i); - ir->o = IR_KPRI; - ir->prev = 0; - } - J->cur.nk = REF_TRUE; - - J->startpc = J->pc; - setmref(J->cur.startpc, J->pc); - if (J->parent) { /* Side trace. */ - GCtrace *T = traceref(J, J->parent); - TraceNo root = T->root ? T->root : J->parent; - J->cur.root = (uint16_t)root; - J->cur.startins = BCINS_AD(BC_JMP, 0, 0); - /* Check whether we could at least potentially form an extra loop. */ - if (J->exitno == 0 && T->snap[0].nent == 0) { - /* We can narrow a FORL for some side traces, too. */ - if (J->pc > proto_bc(J->pt) && bc_op(J->pc[-1]) == BC_JFORI && - bc_d(J->pc[bc_j(J->pc[-1])-1]) == root) { - lj_snap_add(J); - rec_for_loop(J, J->pc-1, &J->scev, 1); - goto sidecheck; - } - } else { - J->startpc = NULL; /* Prevent forming an extra loop. */ - } - lj_snap_replay(J, T); - sidecheck: - if (traceref(J, J->cur.root)->nchild >= J->param[JIT_P_maxside] || - T->snap[J->exitno].count >= J->param[JIT_P_hotexit] + - J->param[JIT_P_tryside]) { - rec_stop(J, LJ_TRLINK_INTERP, 0); - } - } else { /* Root trace. */ - J->cur.root = 0; - J->cur.startins = *J->pc; - J->pc = rec_setup_root(J); - /* Note: the loop instruction itself is recorded at the end and not - ** at the start! So snapshot #0 needs to point to the *next* instruction. - */ - lj_snap_add(J); - if (bc_op(J->cur.startins) == BC_FORL) - rec_for_loop(J, J->pc-1, &J->scev, 1); - if (1 + J->pt->framesize >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); - } -#ifdef LUAJIT_ENABLE_CHECKHOOK - /* Regularly check for instruction/line hooks from compiled code and - ** exit to the interpreter if the hooks are set. - ** - ** This is a compile-time option and disabled by default, since the - ** hook checks may be quite expensive in tight loops. - ** - ** Note this is only useful if hooks are *not* set most of the time. - ** Use this only if you want to *asynchronously* interrupt the execution. - ** - ** You can set the instruction hook via lua_sethook() with a count of 1 - ** from a signal handler or another native thread. Please have a look - ** at the first few functions in luajit.c for an example (Ctrl-C handler). - */ - { - TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), - lj_ir_kptr(J, &J2G(J)->hookmask), IRXLOAD_VOLATILE); - tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (LUA_MASKLINE|LUA_MASKCOUNT))); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0)); - } -#endif -} - -#undef IR -#undef emitir_raw -#undef emitir - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_record.h b/third-party/LuaJIT-2.0.2/src/lj_record.h deleted file mode 100644 index 287b26045e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_record.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -** Trace recorder (bytecode -> SSA IR). -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_RECORD_H -#define _LJ_RECORD_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -/* Context for recording an indexed load/store. */ -typedef struct RecordIndex { - TValue tabv; /* Runtime value of table (or indexed object). */ - TValue keyv; /* Runtime value of key. */ - TValue valv; /* Runtime value of stored value. */ - TValue mobjv; /* Runtime value of metamethod object. */ - GCtab *mtv; /* Runtime value of metatable object. */ - cTValue *oldv; /* Runtime value of previously stored value. */ - TRef tab; /* Table (or indexed object) reference. */ - TRef key; /* Key reference. */ - TRef val; /* Value reference for a store or 0 for a load. */ - TRef mt; /* Metatable reference. */ - TRef mobj; /* Metamethod object reference. */ - int idxchain; /* Index indirections left or 0 for raw lookup. */ -} RecordIndex; - -LJ_FUNC int lj_record_objcmp(jit_State *J, TRef a, TRef b, - cTValue *av, cTValue *bv); -LJ_FUNC TRef lj_record_constify(jit_State *J, cTValue *o); - -LJ_FUNC void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs); -LJ_FUNC void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs); -LJ_FUNC void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults); - -LJ_FUNC int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm); -LJ_FUNC TRef lj_record_idx(jit_State *J, RecordIndex *ix); - -LJ_FUNC void lj_record_ins(jit_State *J); -LJ_FUNC void lj_record_setup(jit_State *J); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_snap.c b/third-party/LuaJIT-2.0.2/src/lj_snap.c deleted file mode 100644 index 30ff915289..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_snap.c +++ /dev/null @@ -1,862 +0,0 @@ -/* -** Snapshot handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_snap_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_target.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#endif - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Emit raw IR without passing through optimizations. */ -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- Snapshot buffer allocation ------------------------------------------ */ - -/* Grow snapshot buffer. */ -void lj_snap_grow_buf_(jit_State *J, MSize need) -{ - MSize maxsnap = (MSize)J->param[JIT_P_maxsnap]; - if (need > maxsnap) - lj_trace_err(J, LJ_TRERR_SNAPOV); - lj_mem_growvec(J->L, J->snapbuf, J->sizesnap, maxsnap, SnapShot); - J->cur.snap = J->snapbuf; -} - -/* Grow snapshot map buffer. */ -void lj_snap_grow_map_(jit_State *J, MSize need) -{ - if (need < 2*J->sizesnapmap) - need = 2*J->sizesnapmap; - else if (need < 64) - need = 64; - J->snapmapbuf = (SnapEntry *)lj_mem_realloc(J->L, J->snapmapbuf, - J->sizesnapmap*sizeof(SnapEntry), need*sizeof(SnapEntry)); - J->cur.snapmap = J->snapmapbuf; - J->sizesnapmap = need; -} - -/* -- Snapshot generation ------------------------------------------------- */ - -/* Add all modified slots to the snapshot. */ -static MSize snapshot_slots(jit_State *J, SnapEntry *map, BCReg nslots) -{ - IRRef retf = J->chain[IR_RETF]; /* Limits SLOAD restore elimination. */ - BCReg s; - MSize n = 0; - for (s = 0; s < nslots; s++) { - TRef tr = J->slot[s]; - IRRef ref = tref_ref(tr); - if (ref) { - SnapEntry sn = SNAP_TR(s, tr); - IRIns *ir = IR(ref); - if (!(sn & (SNAP_CONT|SNAP_FRAME)) && - ir->o == IR_SLOAD && ir->op1 == s && ref > retf) { - /* No need to snapshot unmodified non-inherited slots. */ - if (!(ir->op2 & IRSLOAD_INHERIT)) - continue; - /* No need to restore readonly slots and unmodified non-parent slots. */ - if (!(LJ_DUALNUM && (ir->op2 & IRSLOAD_CONVERT)) && - (ir->op2 & (IRSLOAD_READONLY|IRSLOAD_PARENT)) != IRSLOAD_PARENT) - sn |= SNAP_NORESTORE; - } - if (LJ_SOFTFP && irt_isnum(ir->t)) - sn |= SNAP_SOFTFPNUM; - map[n++] = sn; - } - } - return n; -} - -/* Add frame links at the end of the snapshot. */ -static BCReg snapshot_framelinks(jit_State *J, SnapEntry *map) -{ - cTValue *frame = J->L->base - 1; - cTValue *lim = J->L->base - J->baseslot; - cTValue *ftop = frame + funcproto(frame_func(frame))->framesize; - MSize f = 0; - map[f++] = SNAP_MKPC(J->pc); /* The current PC is always the first entry. */ - while (frame > lim) { /* Backwards traversal of all frames above base. */ - if (frame_islua(frame)) { - map[f++] = SNAP_MKPC(frame_pc(frame)); - frame = frame_prevl(frame); - if (frame + funcproto(frame_func(frame))->framesize > ftop) - ftop = frame + funcproto(frame_func(frame))->framesize; - } else if (frame_iscont(frame)) { - map[f++] = SNAP_MKFTSZ(frame_ftsz(frame)); - map[f++] = SNAP_MKPC(frame_contpc(frame)); - frame = frame_prevd(frame); - } else { - lua_assert(!frame_isc(frame)); - map[f++] = SNAP_MKFTSZ(frame_ftsz(frame)); - frame = frame_prevd(frame); - } - } - lua_assert(f == (MSize)(1 + J->framedepth)); - return (BCReg)(ftop - lim); -} - -/* Take a snapshot of the current stack. */ -static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap) -{ - BCReg nslots = J->baseslot + J->maxslot; - MSize nent; - SnapEntry *p; - /* Conservative estimate. */ - lj_snap_grow_map(J, nsnapmap + nslots + (MSize)J->framedepth+1); - p = &J->cur.snapmap[nsnapmap]; - nent = snapshot_slots(J, p, nslots); - snap->topslot = (uint8_t)snapshot_framelinks(J, p + nent); - snap->mapofs = (uint16_t)nsnapmap; - snap->ref = (IRRef1)J->cur.nins; - snap->nent = (uint8_t)nent; - snap->nslots = (uint8_t)nslots; - snap->count = 0; - J->cur.nsnapmap = (uint16_t)(nsnapmap + nent + 1 + J->framedepth); -} - -/* Add or merge a snapshot. */ -void lj_snap_add(jit_State *J) -{ - MSize nsnap = J->cur.nsnap; - MSize nsnapmap = J->cur.nsnapmap; - /* Merge if no ins. inbetween or if requested and no guard inbetween. */ - if (J->mergesnap ? !irt_isguard(J->guardemit) : - (nsnap > 0 && J->cur.snap[nsnap-1].ref == J->cur.nins)) { - if (nsnap == 1) { /* But preserve snap #0 PC. */ - emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0); - goto nomerge; - } - nsnapmap = J->cur.snap[--nsnap].mapofs; - } else { - nomerge: - lj_snap_grow_buf(J, nsnap+1); - J->cur.nsnap = (uint16_t)(nsnap+1); - } - J->mergesnap = 0; - J->guardemit.irt = 0; - snapshot_stack(J, &J->cur.snap[nsnap], nsnapmap); -} - -/* -- Snapshot modification ----------------------------------------------- */ - -#define SNAP_USEDEF_SLOTS (LJ_MAX_JSLOTS+LJ_STACK_EXTRA) - -/* Find unused slots with reaching-definitions bytecode data-flow analysis. */ -static BCReg snap_usedef(jit_State *J, uint8_t *udf, - const BCIns *pc, BCReg maxslot) -{ - BCReg s; - GCobj *o; - - if (maxslot == 0) return 0; -#ifdef LUAJIT_USE_VALGRIND - /* Avoid errors for harmless reads beyond maxslot. */ - memset(udf, 1, SNAP_USEDEF_SLOTS); -#else - memset(udf, 1, maxslot); -#endif - - /* Treat open upvalues as used. */ - o = gcref(J->L->openupval); - while (o) { - if (uvval(gco2uv(o)) < J->L->base) break; - udf[uvval(gco2uv(o)) - J->L->base] = 0; - o = gcref(o->gch.nextgc); - } - -#define USE_SLOT(s) udf[(s)] &= ~1 -#define DEF_SLOT(s) udf[(s)] *= 3 - - /* Scan through following bytecode and check for uses/defs. */ - lua_assert(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc); - for (;;) { - BCIns ins = *pc++; - BCOp op = bc_op(ins); - switch (bcmode_b(op)) { - case BCMvar: USE_SLOT(bc_b(ins)); break; - default: break; - } - switch (bcmode_c(op)) { - case BCMvar: USE_SLOT(bc_c(ins)); break; - case BCMrbase: - lua_assert(op == BC_CAT); - for (s = bc_b(ins); s <= bc_c(ins); s++) USE_SLOT(s); - for (; s < maxslot; s++) DEF_SLOT(s); - break; - case BCMjump: - handle_jump: { - BCReg minslot = bc_a(ins); - if (op >= BC_FORI && op <= BC_JFORL) minslot += FORL_EXT; - else if (op >= BC_ITERL && op <= BC_JITERL) minslot += bc_b(pc[-2])-1; - else if (op == BC_UCLO) { pc += bc_j(ins); break; } - for (s = minslot; s < maxslot; s++) DEF_SLOT(s); - return minslot < maxslot ? minslot : maxslot; - } - case BCMlit: - if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { - goto handle_jump; - } else if (bc_isret(op)) { - BCReg top = op == BC_RETM ? maxslot : (bc_a(ins) + bc_d(ins)-1); - for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s); - for (; s < top; s++) USE_SLOT(s); - for (; s < maxslot; s++) DEF_SLOT(s); - return 0; - } - break; - case BCMfunc: return maxslot; /* NYI: will abort, anyway. */ - default: break; - } - switch (bcmode_a(op)) { - case BCMvar: USE_SLOT(bc_a(ins)); break; - case BCMdst: - if (!(op == BC_ISTC || op == BC_ISFC)) DEF_SLOT(bc_a(ins)); - break; - case BCMbase: - if (op >= BC_CALLM && op <= BC_VARG) { - BCReg top = (op == BC_CALLM || op == BC_CALLMT || bc_c(ins) == 0) ? - maxslot : (bc_a(ins) + bc_c(ins)); - s = bc_a(ins) - ((op == BC_ITERC || op == BC_ITERN) ? 3 : 0); - for (; s < top; s++) USE_SLOT(s); - for (; s < maxslot; s++) DEF_SLOT(s); - if (op == BC_CALLT || op == BC_CALLMT) { - for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s); - return 0; - } - } else if (op == BC_KNIL) { - for (s = bc_a(ins); s <= bc_d(ins); s++) DEF_SLOT(s); - } else if (op == BC_TSETM) { - for (s = bc_a(ins)-1; s < maxslot; s++) USE_SLOT(s); - } - break; - default: break; - } - lua_assert(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc); - } - -#undef USE_SLOT -#undef DEF_SLOT - - return 0; /* unreachable */ -} - -/* Purge dead slots before the next snapshot. */ -void lj_snap_purge(jit_State *J) -{ - uint8_t udf[SNAP_USEDEF_SLOTS]; - BCReg maxslot = J->maxslot; - BCReg s = snap_usedef(J, udf, J->pc, maxslot); - for (; s < maxslot; s++) - if (udf[s] != 0) - J->base[s] = 0; /* Purge dead slots. */ -} - -/* Shrink last snapshot. */ -void lj_snap_shrink(jit_State *J) -{ - SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, m, nlim, nent = snap->nent; - uint8_t udf[SNAP_USEDEF_SLOTS]; - BCReg maxslot = J->maxslot; - BCReg minslot = snap_usedef(J, udf, snap_pc(map[nent]), maxslot); - BCReg baseslot = J->baseslot; - maxslot += baseslot; - minslot += baseslot; - snap->nslots = (uint8_t)maxslot; - for (n = m = 0; n < nent; n++) { /* Remove unused slots from snapshot. */ - BCReg s = snap_slot(map[n]); - if (s < minslot || (s < maxslot && udf[s-baseslot] == 0)) - map[m++] = map[n]; /* Only copy used slots. */ - } - snap->nent = (uint8_t)m; - nlim = J->cur.nsnapmap - snap->mapofs - 1; - while (n <= nlim) map[m++] = map[n++]; /* Move PC + frame links down. */ - J->cur.nsnapmap = (uint16_t)(snap->mapofs + m); /* Free up space in map. */ -} - -/* -- Snapshot access ----------------------------------------------------- */ - -/* Initialize a Bloom Filter with all renamed refs. -** There are very few renames (often none), so the filter has -** very few bits set. This makes it suitable for negative filtering. -*/ -static BloomFilter snap_renamefilter(GCtrace *T, SnapNo lim) -{ - BloomFilter rfilt = 0; - IRIns *ir; - for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--) - if (ir->op2 <= lim) - bloomset(rfilt, ir->op1); - return rfilt; -} - -/* Process matching renames to find the original RegSP. */ -static RegSP snap_renameref(GCtrace *T, SnapNo lim, IRRef ref, RegSP rs) -{ - IRIns *ir; - for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--) - if (ir->op1 == ref && ir->op2 <= lim) - rs = ir->prev; - return rs; -} - -/* Copy RegSP from parent snapshot to the parent links of the IR. */ -IRIns *lj_snap_regspmap(GCtrace *T, SnapNo snapno, IRIns *ir) -{ - SnapShot *snap = &T->snap[snapno]; - SnapEntry *map = &T->snapmap[snap->mapofs]; - BloomFilter rfilt = snap_renamefilter(T, snapno); - MSize n = 0; - IRRef ref = 0; - for ( ; ; ir++) { - uint32_t rs; - if (ir->o == IR_SLOAD) { - if (!(ir->op2 & IRSLOAD_PARENT)) break; - for ( ; ; n++) { - lua_assert(n < snap->nent); - if (snap_slot(map[n]) == ir->op1) { - ref = snap_ref(map[n++]); - break; - } - } - } else if (LJ_SOFTFP && ir->o == IR_HIOP) { - ref++; - } else if (ir->o == IR_PVAL) { - ref = ir->op1 + REF_BIAS; - } else { - break; - } - rs = T->ir[ref].prev; - if (bloomtest(rfilt, ref)) - rs = snap_renameref(T, snapno, ref, rs); - ir->prev = (uint16_t)rs; - lua_assert(regsp_used(rs)); - } - return ir; -} - -/* -- Snapshot replay ----------------------------------------------------- */ - -/* Replay constant from parent trace. */ -static TRef snap_replay_const(jit_State *J, IRIns *ir) -{ - /* Only have to deal with constants that can occur in stack slots. */ - switch ((IROp)ir->o) { - case IR_KPRI: return TREF_PRI(irt_type(ir->t)); - case IR_KINT: return lj_ir_kint(J, ir->i); - case IR_KGC: return lj_ir_kgc(J, ir_kgc(ir), irt_t(ir->t)); - case IR_KNUM: return lj_ir_k64(J, IR_KNUM, ir_knum(ir)); - case IR_KINT64: return lj_ir_k64(J, IR_KINT64, ir_kint64(ir)); - case IR_KPTR: return lj_ir_kptr(J, ir_kptr(ir)); /* Continuation. */ - default: lua_assert(0); return TREF_NIL; break; - } -} - -/* De-duplicate parent reference. */ -static TRef snap_dedup(jit_State *J, SnapEntry *map, MSize nmax, IRRef ref) -{ - MSize j; - for (j = 0; j < nmax; j++) - if (snap_ref(map[j]) == ref) - return J->slot[snap_slot(map[j])] & ~(SNAP_CONT|SNAP_FRAME); - return 0; -} - -/* Emit parent reference with de-duplication. */ -static TRef snap_pref(jit_State *J, GCtrace *T, SnapEntry *map, MSize nmax, - BloomFilter seen, IRRef ref) -{ - IRIns *ir = &T->ir[ref]; - TRef tr; - if (irref_isk(ref)) - tr = snap_replay_const(J, ir); - else if (!regsp_used(ir->prev)) - tr = 0; - else if (!bloomtest(seen, ref) || (tr = snap_dedup(J, map, nmax, ref)) == 0) - tr = emitir(IRT(IR_PVAL, irt_type(ir->t)), ref - REF_BIAS, 0); - return tr; -} - -/* Check whether a sunk store corresponds to an allocation. Slow path. */ -static int snap_sunk_store2(jit_State *J, IRIns *ira, IRIns *irs) -{ - if (irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE || irs->o == IR_XSTORE) { - IRIns *irk = IR(irs->op1); - if (irk->o == IR_AREF || irk->o == IR_HREFK) - irk = IR(irk->op1); - return (IR(irk->op1) == ira); - } - return 0; -} - -/* Check whether a sunk store corresponds to an allocation. Fast path. */ -static LJ_AINLINE int snap_sunk_store(jit_State *J, IRIns *ira, IRIns *irs) -{ - if (irs->s != 255) - return (ira + irs->s == irs); /* Fast check. */ - return snap_sunk_store2(J, ira, irs); -} - -/* Replay snapshot state to setup side trace. */ -void lj_snap_replay(jit_State *J, GCtrace *T) -{ - SnapShot *snap = &T->snap[J->exitno]; - SnapEntry *map = &T->snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - BloomFilter seen = 0; - int pass23 = 0; - J->framedepth = 0; - /* Emit IR for slots inherited from parent snapshot. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - IRRef ref = snap_ref(sn); - IRIns *ir = &T->ir[ref]; - TRef tr; - /* The bloom filter avoids O(nent^2) overhead for de-duping slots. */ - if (bloomtest(seen, ref) && (tr = snap_dedup(J, map, n, ref)) != 0) - goto setslot; - bloomset(seen, ref); - if (irref_isk(ref)) { - tr = snap_replay_const(J, ir); - } else if (!regsp_used(ir->prev)) { - pass23 = 1; - lua_assert(s != 0); - tr = s; - } else { - IRType t = irt_type(ir->t); - uint32_t mode = IRSLOAD_INHERIT|IRSLOAD_PARENT; - if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) t = IRT_NUM; - if (ir->o == IR_SLOAD) mode |= (ir->op2 & IRSLOAD_READONLY); - tr = emitir_raw(IRT(IR_SLOAD, t), s, mode); - } - setslot: - J->slot[s] = tr | (sn&(SNAP_CONT|SNAP_FRAME)); /* Same as TREF_* flags. */ - J->framedepth += ((sn & (SNAP_CONT|SNAP_FRAME)) && s); - if ((sn & SNAP_FRAME)) - J->baseslot = s+1; - } - if (pass23) { - IRIns *irlast = &T->ir[snap->ref]; - pass23 = 0; - /* Emit dependent PVALs. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - IRRef refp = snap_ref(sn); - IRIns *ir = &T->ir[refp]; - if (regsp_reg(ir->r) == RID_SUNK) { - if (J->slot[snap_slot(sn)] != snap_slot(sn)) continue; - pass23 = 1; - lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || - ir->o == IR_CNEW || ir->o == IR_CNEWI); - if (ir->op1 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op1); - if (ir->op2 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op2); - if (LJ_HASFFI && ir->o == IR_CNEWI) { - if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) - snap_pref(J, T, map, nent, seen, (ir+1)->op2); - } else { - IRIns *irs; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { - if (snap_pref(J, T, map, nent, seen, irs->op2) == 0) - snap_pref(J, T, map, nent, seen, T->ir[irs->op2].op1); - else if ((LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) && - irs+1 < irlast && (irs+1)->o == IR_HIOP) - snap_pref(J, T, map, nent, seen, (irs+1)->op2); - } - } - } else if (!irref_isk(refp) && !regsp_used(ir->prev)) { - lua_assert(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT); - J->slot[snap_slot(sn)] = snap_pref(J, T, map, nent, seen, ir->op1); - } - } - /* Replay sunk instructions. */ - for (n = 0; pass23 && n < nent; n++) { - SnapEntry sn = map[n]; - IRRef refp = snap_ref(sn); - IRIns *ir = &T->ir[refp]; - if (regsp_reg(ir->r) == RID_SUNK) { - TRef op1, op2; - if (J->slot[snap_slot(sn)] != snap_slot(sn)) { /* De-dup allocs. */ - J->slot[snap_slot(sn)] = J->slot[J->slot[snap_slot(sn)]]; - continue; - } - op1 = ir->op1; - if (op1 >= T->nk) op1 = snap_pref(J, T, map, nent, seen, op1); - op2 = ir->op2; - if (op2 >= T->nk) op2 = snap_pref(J, T, map, nent, seen, op2); - if (LJ_HASFFI && ir->o == IR_CNEWI) { - if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) { - lj_needsplit(J); /* Emit joining HIOP. */ - op2 = emitir_raw(IRT(IR_HIOP, IRT_I64), op2, - snap_pref(J, T, map, nent, seen, (ir+1)->op2)); - } - J->slot[snap_slot(sn)] = emitir(ir->ot, op1, op2); - } else { - IRIns *irs; - TRef tr = emitir(ir->ot, op1, op2); - J->slot[snap_slot(sn)] = tr; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { - IRIns *irr = &T->ir[irs->op1]; - TRef val, key = irr->op2, tmp = tr; - if (irr->o != IR_FREF) { - IRIns *irk = &T->ir[key]; - if (irr->o == IR_HREFK) - key = lj_ir_kslot(J, snap_replay_const(J, &T->ir[irk->op1]), - irk->op2); - else - key = snap_replay_const(J, irk); - if (irr->o == IR_HREFK || irr->o == IR_AREF) { - IRIns *irf = &T->ir[irr->op1]; - tmp = emitir(irf->ot, tmp, irf->op2); - } - } - tmp = emitir(irr->ot, tmp, key); - val = snap_pref(J, T, map, nent, seen, irs->op2); - if (val == 0) { - IRIns *irc = &T->ir[irs->op2]; - lua_assert(irc->o == IR_CONV && irc->op2 == IRCONV_NUM_INT); - val = snap_pref(J, T, map, nent, seen, irc->op1); - val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT); - } else if ((LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) && - irs+1 < irlast && (irs+1)->o == IR_HIOP) { - IRType t = IRT_I64; - if (LJ_SOFTFP && irt_type((irs+1)->t) == IRT_SOFTFP) - t = IRT_NUM; - lj_needsplit(J); - if (irref_isk(irs->op2) && irref_isk((irs+1)->op2)) { - uint64_t k = (uint32_t)T->ir[irs->op2].i + - ((uint64_t)T->ir[(irs+1)->op2].i << 32); - val = lj_ir_k64(J, t == IRT_I64 ? IR_KINT64 : IR_KNUM, - lj_ir_k64_find(J, k)); - } else { - val = emitir_raw(IRT(IR_HIOP, t), val, - snap_pref(J, T, map, nent, seen, (irs+1)->op2)); - } - tmp = emitir(IRT(irs->o, t), tmp, val); - continue; - } - tmp = emitir(irs->ot, tmp, val); - } else if (LJ_HASFFI && irs->o == IR_XBAR && ir->o == IR_CNEW) { - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); - } - } - } - } - } - J->base = J->slot + J->baseslot; - J->maxslot = snap->nslots - J->baseslot; - lj_snap_add(J); - if (pass23) /* Need explicit GC step _after_ initial snapshot. */ - emitir_raw(IRTG(IR_GCSTEP, IRT_NIL), 0, 0); -} - -/* -- Snapshot restore ---------------------------------------------------- */ - -static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRIns *ir, TValue *o); - -/* Restore a value from the trace exit state. */ -static void snap_restoreval(jit_State *J, GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRRef ref, TValue *o) -{ - IRIns *ir = &T->ir[ref]; - IRType1 t = ir->t; - RegSP rs = ir->prev; - if (irref_isk(ref)) { /* Restore constant slot. */ - lj_ir_kvalue(J->L, o, ir); - return; - } - if (LJ_UNLIKELY(bloomtest(rfilt, ref))) - rs = snap_renameref(T, snapno, ref, rs); - if (ra_hasspill(regsp_spill(rs))) { /* Restore from spill slot. */ - int32_t *sps = &ex->spill[regsp_spill(rs)]; - if (irt_isinteger(t)) { - setintV(o, *sps); -#if !LJ_SOFTFP - } else if (irt_isnum(t)) { - o->u64 = *(uint64_t *)sps; -#endif - } else if (LJ_64 && irt_islightud(t)) { - /* 64 bit lightuserdata which may escape already has the tag bits. */ - o->u64 = *(uint64_t *)sps; - } else { - lua_assert(!irt_ispri(t)); /* PRI refs never have a spill slot. */ - setgcrefi(o->gcr, *sps); - setitype(o, irt_toitype(t)); - } - } else { /* Restore from register. */ - Reg r = regsp_reg(rs); - if (ra_noreg(r)) { - lua_assert(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT); - snap_restoreval(J, T, ex, snapno, rfilt, ir->op1, o); - if (LJ_DUALNUM) setnumV(o, (lua_Number)intV(o)); - return; - } else if (irt_isinteger(t)) { - setintV(o, (int32_t)ex->gpr[r-RID_MIN_GPR]); -#if !LJ_SOFTFP - } else if (irt_isnum(t)) { - setnumV(o, ex->fpr[r-RID_MIN_FPR]); -#endif - } else if (LJ_64 && irt_islightud(t)) { - /* 64 bit lightuserdata which may escape already has the tag bits. */ - o->u64 = ex->gpr[r-RID_MIN_GPR]; - } else { - if (!irt_ispri(t)) - setgcrefi(o->gcr, ex->gpr[r-RID_MIN_GPR]); - setitype(o, irt_toitype(t)); - } - } -} - -#if LJ_HASFFI -/* Restore raw data from the trace exit state. */ -static void snap_restoredata(GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRRef ref, void *dst, CTSize sz) -{ - IRIns *ir = &T->ir[ref]; - RegSP rs = ir->prev; - int32_t *src; - uint64_t tmp; - if (irref_isk(ref)) { - if (ir->o == IR_KNUM || ir->o == IR_KINT64) { - src = mref(ir->ptr, int32_t); - } else if (sz == 8) { - tmp = (uint64_t)(uint32_t)ir->i; - src = (int32_t *)&tmp; - } else { - src = &ir->i; - } - } else { - if (LJ_UNLIKELY(bloomtest(rfilt, ref))) - rs = snap_renameref(T, snapno, ref, rs); - if (ra_hasspill(regsp_spill(rs))) { - src = &ex->spill[regsp_spill(rs)]; - if (sz == 8 && !irt_is64(ir->t)) { - tmp = (uint64_t)(uint32_t)*src; - src = (int32_t *)&tmp; - } - } else { - Reg r = regsp_reg(rs); - if (ra_noreg(r)) { - /* Note: this assumes CNEWI is never used for SOFTFP split numbers. */ - lua_assert(sz == 8 && ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT); - snap_restoredata(T, ex, snapno, rfilt, ir->op1, dst, 4); - *(lua_Number *)dst = (lua_Number)*(int32_t *)dst; - return; - } - src = (int32_t *)&ex->gpr[r-RID_MIN_GPR]; -#if !LJ_SOFTFP - if (r >= RID_MAX_GPR) { - src = (int32_t *)&ex->fpr[r-RID_MIN_FPR]; -#if LJ_TARGET_PPC - if (sz == 4) { /* PPC FPRs are always doubles. */ - *(float *)dst = (float)*(double *)src; - return; - } -#else - if (LJ_BE && sz == 4) src++; -#endif - } -#endif - } - } - lua_assert(sz == 1 || sz == 2 || sz == 4 || sz == 8); - if (sz == 4) *(int32_t *)dst = *src; - else if (sz == 8) *(int64_t *)dst = *(int64_t *)src; - else if (sz == 1) *(int8_t *)dst = (int8_t)*src; - else *(int16_t *)dst = (int16_t)*src; -} -#endif - -/* Unsink allocation from the trace exit state. Unsink sunk stores. */ -static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRIns *ir, TValue *o) -{ - lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || - ir->o == IR_CNEW || ir->o == IR_CNEWI); -#if LJ_HASFFI - if (ir->o == IR_CNEW || ir->o == IR_CNEWI) { - CTState *cts = ctype_ctsG(J2G(J)); - CTypeID id = (CTypeID)T->ir[ir->op1].i; - CTSize sz = lj_ctype_size(cts, id); - GCcdata *cd = lj_cdata_new(cts, id, sz); - setcdataV(J->L, o, cd); - if (ir->o == IR_CNEWI) { - uint8_t *p = (uint8_t *)cdataptr(cd); - lua_assert(sz == 4 || sz == 8); - if (LJ_32 && sz == 8 && ir+1 < T->ir + T->nins && (ir+1)->o == IR_HIOP) { - snap_restoredata(T, ex, snapno, rfilt, (ir+1)->op2, LJ_LE?p+4:p, 4); - if (LJ_BE) p += 4; - sz = 4; - } - snap_restoredata(T, ex, snapno, rfilt, ir->op2, p, sz); - } else { - IRIns *irs, *irlast = &T->ir[T->snap[snapno].ref]; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { - IRIns *iro = &T->ir[T->ir[irs->op1].op2]; - uint8_t *p = (uint8_t *)cd; - CTSize szs; - lua_assert(irs->o == IR_XSTORE && T->ir[irs->op1].o == IR_ADD); - lua_assert(iro->o == IR_KINT || iro->o == IR_KINT64); - if (irt_is64(irs->t)) szs = 8; - else if (irt_isi8(irs->t) || irt_isu8(irs->t)) szs = 1; - else if (irt_isi16(irs->t) || irt_isu16(irs->t)) szs = 2; - else szs = 4; - if (LJ_64 && iro->o == IR_KINT64) - p += (int64_t)ir_k64(iro)->u64; - else - p += iro->i; - lua_assert(p >= (uint8_t *)cdataptr(cd) && - p + szs <= (uint8_t *)cdataptr(cd) + sz); - if (LJ_32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) { - lua_assert(szs == 4); - snap_restoredata(T, ex, snapno, rfilt, (irs+1)->op2, LJ_LE?p+4:p,4); - if (LJ_BE) p += 4; - } - snap_restoredata(T, ex, snapno, rfilt, irs->op2, p, szs); - } - } - } else -#endif - { - IRIns *irs, *irlast; - GCtab *t = ir->o == IR_TNEW ? lj_tab_new(J->L, ir->op1, ir->op2) : - lj_tab_dup(J->L, ir_ktab(&T->ir[ir->op1])); - settabV(J->L, o, t); - irlast = &T->ir[T->snap[snapno].ref]; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { - IRIns *irk = &T->ir[irs->op1]; - TValue tmp, *val; - lua_assert(irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE); - if (irk->o == IR_FREF) { - lua_assert(irk->op2 == IRFL_TAB_META); - snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, &tmp); - /* NOBARRIER: The table is new (marked white). */ - setgcref(t->metatable, obj2gco(tabV(&tmp))); - } else { - irk = &T->ir[irk->op2]; - if (irk->o == IR_KSLOT) irk = &T->ir[irk->op1]; - lj_ir_kvalue(J->L, &tmp, irk); - val = lj_tab_set(J->L, t, &tmp); - /* NOBARRIER: The table is new (marked white). */ - snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, val); - if (LJ_SOFTFP && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) { - snap_restoreval(J, T, ex, snapno, rfilt, (irs+1)->op2, &tmp); - val->u32.hi = tmp.u32.lo; - } - } - } - } -} - -/* Restore interpreter state from exit state with the help of a snapshot. */ -const BCIns *lj_snap_restore(jit_State *J, void *exptr) -{ - ExitState *ex = (ExitState *)exptr; - SnapNo snapno = J->exitno; /* For now, snapno == exitno. */ - GCtrace *T = traceref(J, J->parent); - SnapShot *snap = &T->snap[snapno]; - MSize n, nent = snap->nent; - SnapEntry *map = &T->snapmap[snap->mapofs]; - SnapEntry *flinks = &T->snapmap[snap_nextofs(T, snap)-1]; - int32_t ftsz0; - TValue *frame; - BloomFilter rfilt = snap_renamefilter(T, snapno); - const BCIns *pc = snap_pc(map[nent]); - lua_State *L = J->L; - - /* Set interpreter PC to the next PC to get correct error messages. */ - setcframe_pc(cframe_raw(L->cframe), pc+1); - - /* Make sure the stack is big enough for the slots from the snapshot. */ - if (LJ_UNLIKELY(L->base + snap->topslot >= tvref(L->maxstack))) { - L->top = curr_topL(L); - lj_state_growstack(L, snap->topslot - curr_proto(L)->framesize); - } - - /* Fill stack slots with data from the registers and spill slots. */ - frame = L->base-1; - ftsz0 = frame_ftsz(frame); /* Preserve link to previous frame in slot #0. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - if (!(sn & SNAP_NORESTORE)) { - TValue *o = &frame[snap_slot(sn)]; - IRRef ref = snap_ref(sn); - IRIns *ir = &T->ir[ref]; - if (ir->r == RID_SUNK) { - MSize j; - for (j = 0; j < n; j++) - if (snap_ref(map[j]) == ref) { /* De-duplicate sunk allocations. */ - copyTV(L, o, &frame[snap_slot(map[j])]); - goto dupslot; - } - snap_unsink(J, T, ex, snapno, rfilt, ir, o); - dupslot: - continue; - } - snap_restoreval(J, T, ex, snapno, rfilt, ref, o); - if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && tvisint(o)) { - TValue tmp; - snap_restoreval(J, T, ex, snapno, rfilt, ref+1, &tmp); - o->u32.hi = tmp.u32.lo; - } else if ((sn & (SNAP_CONT|SNAP_FRAME))) { - /* Overwrite tag with frame link. */ - o->fr.tp.ftsz = snap_slot(sn) != 0 ? (int32_t)*flinks-- : ftsz0; - L->base = o+1; - } - } - } - lua_assert(map + nent == flinks); - - /* Compute current stack top. */ - switch (bc_op(*pc)) { - case BC_CALLM: case BC_CALLMT: case BC_RETM: case BC_TSETM: - L->top = frame + snap->nslots; - break; - default: - L->top = curr_topL(L); - break; - } - return pc; -} - -#undef IR -#undef emitir_raw -#undef emitir - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_snap.h b/third-party/LuaJIT-2.0.2/src/lj_snap.h deleted file mode 100644 index aff97e5e4d..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_snap.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -** Snapshot handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_SNAP_H -#define _LJ_SNAP_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -LJ_FUNC void lj_snap_add(jit_State *J); -LJ_FUNC void lj_snap_purge(jit_State *J); -LJ_FUNC void lj_snap_shrink(jit_State *J); -LJ_FUNC IRIns *lj_snap_regspmap(GCtrace *T, SnapNo snapno, IRIns *ir); -LJ_FUNC void lj_snap_replay(jit_State *J, GCtrace *T); -LJ_FUNC const BCIns *lj_snap_restore(jit_State *J, void *exptr); -LJ_FUNC void lj_snap_grow_buf_(jit_State *J, MSize need); -LJ_FUNC void lj_snap_grow_map_(jit_State *J, MSize need); - -static LJ_AINLINE void lj_snap_grow_buf(jit_State *J, MSize need) -{ - if (LJ_UNLIKELY(need > J->sizesnap)) lj_snap_grow_buf_(J, need); -} - -static LJ_AINLINE void lj_snap_grow_map(jit_State *J, MSize need) -{ - if (LJ_UNLIKELY(need > J->sizesnapmap)) lj_snap_grow_map_(J, need); -} - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_state.c b/third-party/LuaJIT-2.0.2/src/lj_state.c deleted file mode 100644 index 8c53d37f1f..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_state.c +++ /dev/null @@ -1,287 +0,0 @@ -/* -** State and stack handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_state_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_trace.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_lex.h" -#include "lj_alloc.h" - -/* -- Stack handling ------------------------------------------------------ */ - -/* Stack sizes. */ -#define LJ_STACK_MIN LUA_MINSTACK /* Min. stack size. */ -#define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */ -#define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */ -#define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA) - -/* Explanation of LJ_STACK_EXTRA: -** -** Calls to metamethods store their arguments beyond the current top -** without checking for the stack limit. This avoids stack resizes which -** would invalidate passed TValue pointers. The stack check is performed -** later by the function header. This can safely resize the stack or raise -** an error. Thus we need some extra slots beyond the current stack limit. -** -** Most metamethods need 4 slots above top (cont, mobj, arg1, arg2) plus -** one extra slot if mobj is not a function. Only lj_meta_tset needs 5 -** slots above top, but then mobj is always a function. So we can get by -** with 5 extra slots. -*/ - -/* Resize stack slots and adjust pointers in state. */ -static void resizestack(lua_State *L, MSize n) -{ - TValue *st, *oldst = tvref(L->stack); - ptrdiff_t delta; - MSize oldsize = L->stacksize; - MSize realsize = n + 1 + LJ_STACK_EXTRA; - GCobj *up; - lua_assert((MSize)(tvref(L->maxstack)-oldst)==L->stacksize-LJ_STACK_EXTRA-1); - st = (TValue *)lj_mem_realloc(L, tvref(L->stack), - (MSize)(L->stacksize*sizeof(TValue)), - (MSize)(realsize*sizeof(TValue))); - setmref(L->stack, st); - delta = (char *)st - (char *)oldst; - setmref(L->maxstack, st + n); - while (oldsize < realsize) /* Clear new slots. */ - setnilV(st + oldsize++); - L->stacksize = realsize; - L->base = (TValue *)((char *)L->base + delta); - L->top = (TValue *)((char *)L->top + delta); - for (up = gcref(L->openupval); up != NULL; up = gcnext(up)) - setmref(gco2uv(up)->v, (TValue *)((char *)uvval(gco2uv(up)) + delta)); - if (obj2gco(L) == gcref(G(L)->jit_L)) - setmref(G(L)->jit_base, mref(G(L)->jit_base, char) + delta); -} - -/* Relimit stack after error, in case the limit was overdrawn. */ -void lj_state_relimitstack(lua_State *L) -{ - if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1) - resizestack(L, LJ_STACK_MAX); -} - -/* Try to shrink the stack (called from GC). */ -void lj_state_shrinkstack(lua_State *L, MSize used) -{ - if (L->stacksize > LJ_STACK_MAXEX) - return; /* Avoid stack shrinking while handling stack overflow. */ - if (4*used < L->stacksize && - 2*(LJ_STACK_START+LJ_STACK_EXTRA) < L->stacksize && - obj2gco(L) != gcref(G(L)->jit_L)) /* Don't shrink stack of live trace. */ - resizestack(L, L->stacksize >> 1); -} - -/* Try to grow stack. */ -void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need) -{ - MSize n; - if (L->stacksize > LJ_STACK_MAXEX) /* Overflow while handling overflow? */ - lj_err_throw(L, LUA_ERRERR); - n = L->stacksize + need; - if (n > LJ_STACK_MAX) { - n += 2*LUA_MINSTACK; - } else if (n < 2*L->stacksize) { - n = 2*L->stacksize; - if (n >= LJ_STACK_MAX) - n = LJ_STACK_MAX; - } - resizestack(L, n); - if (L->stacksize > LJ_STACK_MAXEX) - lj_err_msg(L, LJ_ERR_STKOV); -} - -void LJ_FASTCALL lj_state_growstack1(lua_State *L) -{ - lj_state_growstack(L, 1); -} - -/* Allocate basic stack for new state. */ -static void stack_init(lua_State *L1, lua_State *L) -{ - TValue *stend, *st = lj_mem_newvec(L, LJ_STACK_START+LJ_STACK_EXTRA, TValue); - setmref(L1->stack, st); - L1->stacksize = LJ_STACK_START + LJ_STACK_EXTRA; - stend = st + L1->stacksize; - setmref(L1->maxstack, stend - LJ_STACK_EXTRA - 1); - L1->base = L1->top = st+1; - setthreadV(L1, st, L1); /* Needed for curr_funcisL() on empty stack. */ - while (st < stend) /* Clear new slots. */ - setnilV(st++); -} - -/* -- State handling ------------------------------------------------------ */ - -/* Open parts that may cause memory-allocation errors. */ -static TValue *cpluaopen(lua_State *L, lua_CFunction dummy, void *ud) -{ - global_State *g = G(L); - UNUSED(dummy); - UNUSED(ud); - stack_init(L, L); - /* NOBARRIER: State initialization, all objects are white. */ - setgcref(L->env, obj2gco(lj_tab_new(L, 0, LJ_MIN_GLOBAL))); - settabV(L, registry(L), lj_tab_new(L, 0, LJ_MIN_REGISTRY)); - lj_str_resize(L, LJ_MIN_STRTAB-1); - lj_meta_init(L); - lj_lex_init(L); - fixstring(lj_err_str(L, LJ_ERR_ERRMEM)); /* Preallocate memory error msg. */ - g->gc.threshold = 4*g->gc.total; - lj_trace_initstate(g); - return NULL; -} - -static void close_state(lua_State *L) -{ - global_State *g = G(L); - lj_func_closeuv(L, tvref(L->stack)); - lj_gc_freeall(g); - lua_assert(gcref(g->gc.root) == obj2gco(L)); - lua_assert(g->strnum == 0); - lj_trace_freestate(g); -#if LJ_HASFFI - lj_ctype_freestate(g); -#endif - lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); - lj_str_freebuf(g, &g->tmpbuf); - lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); - lua_assert(g->gc.total == sizeof(GG_State)); -#ifndef LUAJIT_USE_SYSMALLOC - if (g->allocf == lj_alloc_f) - lj_alloc_destroy(g->allocd); - else -#endif - g->allocf(g->allocd, G2GG(g), sizeof(GG_State), 0); -} - -#if LJ_64 -lua_State *lj_state_newstate(lua_Alloc f, void *ud) -#else -LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) -#endif -{ - GG_State *GG = (GG_State *)f(ud, NULL, 0, sizeof(GG_State)); - lua_State *L = &GG->L; - global_State *g = &GG->g; - if (GG == NULL || !checkptr32(GG)) return NULL; - memset(GG, 0, sizeof(GG_State)); - L->gct = ~LJ_TTHREAD; - L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED; /* Prevent free. */ - L->dummy_ffid = FF_C; - setmref(L->glref, g); - g->gc.currentwhite = LJ_GC_WHITE0 | LJ_GC_FIXED; - g->strempty.marked = LJ_GC_WHITE0; - g->strempty.gct = ~LJ_TSTR; - g->allocf = f; - g->allocd = ud; - setgcref(g->mainthref, obj2gco(L)); - setgcref(g->uvhead.prev, obj2gco(&g->uvhead)); - setgcref(g->uvhead.next, obj2gco(&g->uvhead)); - g->strmask = ~(MSize)0; - setnilV(registry(L)); - setnilV(&g->nilnode.val); - setnilV(&g->nilnode.key); - setmref(g->nilnode.freetop, &g->nilnode); - lj_str_initbuf(&g->tmpbuf); - g->gc.state = GCSpause; - setgcref(g->gc.root, obj2gco(L)); - setmref(g->gc.sweep, &g->gc.root); - g->gc.total = sizeof(GG_State); - g->gc.pause = LUAI_GCPAUSE; - g->gc.stepmul = LUAI_GCMUL; - lj_dispatch_init((GG_State *)L); - L->status = LUA_ERRERR+1; /* Avoid touching the stack upon memory error. */ - if (lj_vm_cpcall(L, NULL, NULL, cpluaopen) != 0) { - /* Memory allocation error: free partial state. */ - close_state(L); - return NULL; - } - L->status = 0; - return L; -} - -static TValue *cpfinalize(lua_State *L, lua_CFunction dummy, void *ud) -{ - UNUSED(dummy); - UNUSED(ud); - lj_gc_finalize_cdata(L); - lj_gc_finalize_udata(L); - /* Frame pop omitted. */ - return NULL; -} - -LUA_API void lua_close(lua_State *L) -{ - global_State *g = G(L); - int i; - L = mainthread(g); /* Only the main thread can be closed. */ - lj_func_closeuv(L, tvref(L->stack)); - lj_gc_separateudata(g, 1); /* Separate udata which have GC metamethods. */ -#if LJ_HASJIT - G2J(g)->flags &= ~JIT_F_ON; - G2J(g)->state = LJ_TRACE_IDLE; - lj_dispatch_update(g); -#endif - for (i = 0;;) { - hook_enter(g); - L->status = 0; - L->cframe = NULL; - L->base = L->top = tvref(L->stack) + 1; - if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == 0) { - if (++i >= 10) break; - lj_gc_separateudata(g, 1); /* Separate udata again. */ - if (gcref(g->gc.mmudata) == NULL) /* Until nothing is left to do. */ - break; - } - } - close_state(L); -} - -lua_State *lj_state_new(lua_State *L) -{ - lua_State *L1 = lj_mem_newobj(L, lua_State); - L1->gct = ~LJ_TTHREAD; - L1->dummy_ffid = FF_C; - L1->status = 0; - L1->stacksize = 0; - setmref(L1->stack, NULL); - L1->cframe = NULL; - /* NOBARRIER: The lua_State is new (marked white). */ - setgcrefnull(L1->openupval); - setmrefr(L1->glref, L->glref); - setgcrefr(L1->env, L->env); - stack_init(L1, L); /* init stack */ - lua_assert(iswhite(obj2gco(L1))); - return L1; -} - -void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L) -{ - lua_assert(L != mainthread(g)); - lj_func_closeuv(L, tvref(L->stack)); - lua_assert(gcref(L->openupval) == NULL); - lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); - lj_mem_freet(g, L); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_state.h b/third-party/LuaJIT-2.0.2/src/lj_state.h deleted file mode 100644 index 527f054efa..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_state.h +++ /dev/null @@ -1,35 +0,0 @@ -/* -** State and stack handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_STATE_H -#define _LJ_STATE_H - -#include "lj_obj.h" - -#define incr_top(L) \ - (++L->top >= tvref(L->maxstack) && (lj_state_growstack1(L), 0)) - -#define savestack(L, p) ((char *)(p) - mref(L->stack, char)) -#define restorestack(L, n) ((TValue *)(mref(L->stack, char) + (n))) - -LJ_FUNC void lj_state_relimitstack(lua_State *L); -LJ_FUNC void lj_state_shrinkstack(lua_State *L, MSize used); -LJ_FUNCA void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need); -LJ_FUNC void LJ_FASTCALL lj_state_growstack1(lua_State *L); - -static LJ_AINLINE void lj_state_checkstack(lua_State *L, MSize need) -{ - if ((mref(L->maxstack, char) - (char *)L->top) <= - (ptrdiff_t)need*(ptrdiff_t)sizeof(TValue)) - lj_state_growstack(L, need); -} - -LJ_FUNC lua_State *lj_state_new(lua_State *L); -LJ_FUNC void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L); -#if LJ_64 -LJ_FUNC lua_State *lj_state_newstate(lua_Alloc f, void *ud); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_str.c b/third-party/LuaJIT-2.0.2/src/lj_str.c deleted file mode 100644 index 6548ee4d38..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_str.c +++ /dev/null @@ -1,339 +0,0 @@ -/* -** String handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include - -#define lj_str_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_state.h" -#include "lj_char.h" - -/* -- String interning ---------------------------------------------------- */ - -/* Ordered compare of strings. Assumes string data is 4-byte aligned. */ -int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b) -{ - MSize i, n = a->len > b->len ? b->len : a->len; - for (i = 0; i < n; i += 4) { - /* Note: innocuous access up to end of string + 3. */ - uint32_t va = *(const uint32_t *)(strdata(a)+i); - uint32_t vb = *(const uint32_t *)(strdata(b)+i); - if (va != vb) { -#if LJ_LE - va = lj_bswap(va); vb = lj_bswap(vb); -#endif - i -= n; - if ((int32_t)i >= -3) { - va >>= 32+(i<<3); vb >>= 32+(i<<3); - if (va == vb) break; - } - return va < vb ? -1 : 1; - } - } - return (int32_t)(a->len - b->len); -} - -/* Fast string data comparison. Caveat: unaligned access to 1st string! */ -static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len) -{ - MSize i = 0; - lua_assert(len > 0); - lua_assert((((uintptr_t)a+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4); - do { /* Note: innocuous access up to end of string + 3. */ - uint32_t v = lj_getu32(a+i) ^ *(const uint32_t *)(b+i); - if (v) { - i -= len; -#if LJ_LE - return (int32_t)i >= -3 ? (v << (32+(i<<3))) : 1; -#else - return (int32_t)i >= -3 ? (v >> (32+(i<<3))) : 1; -#endif - } - i += 4; - } while (i < len); - return 0; -} - -/* Resize the string hash table (grow and shrink). */ -void lj_str_resize(lua_State *L, MSize newmask) -{ - global_State *g = G(L); - GCRef *newhash; - MSize i; - if (g->gc.state == GCSsweepstring || newmask >= LJ_MAX_STRTAB-1) - return; /* No resizing during GC traversal or if already too big. */ - newhash = lj_mem_newvec(L, newmask+1, GCRef); - memset(newhash, 0, (newmask+1)*sizeof(GCRef)); - for (i = g->strmask; i != ~(MSize)0; i--) { /* Rehash old table. */ - GCobj *p = gcref(g->strhash[i]); - while (p) { /* Follow each hash chain and reinsert all strings. */ - MSize h = gco2str(p)->hash & newmask; - GCobj *next = gcnext(p); - /* NOBARRIER: The string table is a GC root. */ - setgcrefr(p->gch.nextgc, newhash[h]); - setgcref(newhash[h], p); - p = next; - } - } - lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); - g->strmask = newmask; - g->strhash = newhash; -} - -/* Intern a string and return string object. */ -GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx) -{ - global_State *g; - GCstr *s; - GCobj *o; - MSize len = (MSize)lenx; - MSize a, b, h = len; - if (lenx >= LJ_MAX_STR) - lj_err_msg(L, LJ_ERR_STROV); - g = G(L); - /* Compute string hash. Constants taken from lookup3 hash by Bob Jenkins. */ - if (len >= 4) { /* Caveat: unaligned access! */ - a = lj_getu32(str); - h ^= lj_getu32(str+len-4); - b = lj_getu32(str+(len>>1)-2); - h ^= b; h -= lj_rol(b, 14); - b += lj_getu32(str+(len>>2)-1); - } else if (len > 0) { - a = *(const uint8_t *)str; - h ^= *(const uint8_t *)(str+len-1); - b = *(const uint8_t *)(str+(len>>1)); - h ^= b; h -= lj_rol(b, 14); - } else { - return &g->strempty; - } - a ^= h; a -= lj_rol(h, 11); - b ^= a; b -= lj_rol(a, 25); - h ^= b; h -= lj_rol(b, 16); - /* Check if the string has already been interned. */ - o = gcref(g->strhash[h & g->strmask]); - if (LJ_LIKELY((((uintptr_t)str+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4)) { - while (o != NULL) { - GCstr *sx = gco2str(o); - if (sx->len == len && str_fastcmp(str, strdata(sx), len) == 0) { - /* Resurrect if dead. Can only happen with fixstring() (keywords). */ - if (isdead(g, o)) flipwhite(o); - return sx; /* Return existing string. */ - } - o = gcnext(o); - } - } else { /* Slow path: end of string is too close to a page boundary. */ - while (o != NULL) { - GCstr *sx = gco2str(o); - if (sx->len == len && memcmp(str, strdata(sx), len) == 0) { - /* Resurrect if dead. Can only happen with fixstring() (keywords). */ - if (isdead(g, o)) flipwhite(o); - return sx; /* Return existing string. */ - } - o = gcnext(o); - } - } - /* Nope, create a new string. */ - s = lj_mem_newt(L, sizeof(GCstr)+len+1, GCstr); - newwhite(g, s); - s->gct = ~LJ_TSTR; - s->len = len; - s->hash = h; - s->reserved = 0; - memcpy(strdatawr(s), str, len); - strdatawr(s)[len] = '\0'; /* Zero-terminate string. */ - /* Add it to string hash table. */ - h &= g->strmask; - s->nextgc = g->strhash[h]; - /* NOBARRIER: The string table is a GC root. */ - setgcref(g->strhash[h], obj2gco(s)); - if (g->strnum++ > g->strmask) /* Allow a 100% load factor. */ - lj_str_resize(L, (g->strmask<<1)+1); /* Grow string table. */ - return s; /* Return newly interned string. */ -} - -void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s) -{ - g->strnum--; - lj_mem_free(g, s, sizestring(s)); -} - -/* -- Type conversions ---------------------------------------------------- */ - -/* Print number to buffer. Canonicalizes non-finite values. */ -size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o) -{ - if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */ - lua_Number n = o->n; -#if __BIONIC__ - if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; } -#endif - return (size_t)lua_number2str(s, n); - } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) { - s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3; - } else if ((o->u32.hi & 0x80000000) == 0) { - s[0] = 'i'; s[1] = 'n'; s[2] = 'f'; return 3; - } else { - s[0] = '-'; s[1] = 'i'; s[2] = 'n'; s[3] = 'f'; return 4; - } -} - -/* Print integer to buffer. Returns pointer to start. */ -char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k) -{ - uint32_t u = (uint32_t)(k < 0 ? -k : k); - p += 1+10; - do { *--p = (char)('0' + u % 10); } while (u /= 10); - if (k < 0) *--p = '-'; - return p; -} - -/* Convert number to string. */ -GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np) -{ - char buf[LJ_STR_NUMBUF]; - size_t len = lj_str_bufnum(buf, (TValue *)np); - return lj_str_new(L, buf, len); -} - -/* Convert integer to string. */ -GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k) -{ - char s[1+10]; - char *p = lj_str_bufint(s, k); - return lj_str_new(L, p, (size_t)(s+sizeof(s)-p)); -} - -GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o) -{ - return tvisint(o) ? lj_str_fromint(L, intV(o)) : lj_str_fromnum(L, &o->n); -} - -/* -- String formatting --------------------------------------------------- */ - -static void addstr(lua_State *L, SBuf *sb, const char *str, MSize len) -{ - char *p; - MSize i; - if (sb->n + len > sb->sz) { - MSize sz = sb->sz * 2; - while (sb->n + len > sz) sz = sz * 2; - lj_str_resizebuf(L, sb, sz); - } - p = sb->buf + sb->n; - sb->n += len; - for (i = 0; i < len; i++) p[i] = str[i]; -} - -static void addchar(lua_State *L, SBuf *sb, int c) -{ - if (sb->n + 1 > sb->sz) { - MSize sz = sb->sz * 2; - lj_str_resizebuf(L, sb, sz); - } - sb->buf[sb->n++] = (char)c; -} - -/* Push formatted message as a string object to Lua stack. va_list variant. */ -const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) -{ - SBuf *sb = &G(L)->tmpbuf; - lj_str_needbuf(L, sb, (MSize)strlen(fmt)); - lj_str_resetbuf(sb); - for (;;) { - const char *e = strchr(fmt, '%'); - if (e == NULL) break; - addstr(L, sb, fmt, (MSize)(e-fmt)); - /* This function only handles %s, %c, %d, %f and %p formats. */ - switch (e[1]) { - case 's': { - const char *s = va_arg(argp, char *); - if (s == NULL) s = "(null)"; - addstr(L, sb, s, (MSize)strlen(s)); - break; - } - case 'c': - addchar(L, sb, va_arg(argp, int)); - break; - case 'd': { - char buf[LJ_STR_INTBUF]; - char *p = lj_str_bufint(buf, va_arg(argp, int32_t)); - addstr(L, sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); - break; - } - case 'f': { - char buf[LJ_STR_NUMBUF]; - TValue tv; - MSize len; - tv.n = (lua_Number)(va_arg(argp, LUAI_UACNUMBER)); - len = (MSize)lj_str_bufnum(buf, &tv); - addstr(L, sb, buf, len); - break; - } - case 'p': { -#define FMTP_CHARS (2*sizeof(ptrdiff_t)) - char buf[2+FMTP_CHARS]; - ptrdiff_t p = (ptrdiff_t)(va_arg(argp, void *)); - ptrdiff_t i, lasti = 2+FMTP_CHARS; - if (p == 0) { - addstr(L, sb, "NULL", 4); - break; - } -#if LJ_64 - /* Shorten output for 64 bit pointers. */ - lasti = 2+2*4+((p >> 32) ? 2+2*(lj_fls((uint32_t)(p >> 32))>>3) : 0); -#endif - buf[0] = '0'; - buf[1] = 'x'; - for (i = lasti-1; i >= 2; i--, p >>= 4) - buf[i] = "0123456789abcdef"[(p & 15)]; - addstr(L, sb, buf, (MSize)lasti); - break; - } - case '%': - addchar(L, sb, '%'); - break; - default: - addchar(L, sb, '%'); - addchar(L, sb, e[1]); - break; - } - fmt = e+2; - } - addstr(L, sb, fmt, (MSize)strlen(fmt)); - setstrV(L, L->top, lj_str_new(L, sb->buf, sb->n)); - incr_top(L); - return strVdata(L->top - 1); -} - -/* Push formatted message as a string object to Lua stack. Vararg variant. */ -const char *lj_str_pushf(lua_State *L, const char *fmt, ...) -{ - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = lj_str_pushvf(L, fmt, argp); - va_end(argp); - return msg; -} - -/* -- Buffer handling ----------------------------------------------------- */ - -char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz) -{ - if (sz > sb->sz) { - if (sz < LJ_MIN_SBUF) sz = LJ_MIN_SBUF; - lj_str_resizebuf(L, sb, sz); - } - return sb->buf; -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_str.h b/third-party/LuaJIT-2.0.2/src/lj_str.h deleted file mode 100644 index 3aa036625f..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_str.h +++ /dev/null @@ -1,50 +0,0 @@ -/* -** String handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_STR_H -#define _LJ_STR_H - -#include - -#include "lj_obj.h" - -/* String interning. */ -LJ_FUNC int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b); -LJ_FUNC void lj_str_resize(lua_State *L, MSize newmask); -LJ_FUNCA GCstr *lj_str_new(lua_State *L, const char *str, size_t len); -LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s); - -#define lj_str_newz(L, s) (lj_str_new(L, s, strlen(s))) -#define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1)) - -/* Type conversions. */ -LJ_FUNC size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o); -LJ_FUNC char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k); -LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np); -LJ_FUNC GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k); -LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o); - -#define LJ_STR_INTBUF (1+10) -#define LJ_STR_NUMBUF LUAI_MAXNUMBER2STR - -/* String formatting. */ -LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp); -LJ_FUNC const char *lj_str_pushf(lua_State *L, const char *fmt, ...) -#if defined(__GNUC__) - __attribute__ ((format (printf, 2, 3))) -#endif - ; - -/* Resizable string buffers. Struct definition in lj_obj.h. */ -LJ_FUNC char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz); - -#define lj_str_initbuf(sb) ((sb)->buf = NULL, (sb)->sz = 0) -#define lj_str_resetbuf(sb) ((sb)->n = 0) -#define lj_str_resizebuf(L, sb, size) \ - ((sb)->buf = (char *)lj_mem_realloc(L, (sb)->buf, (sb)->sz, (size)), \ - (sb)->sz = (size)) -#define lj_str_freebuf(g, sb) lj_mem_free(g, (void *)(sb)->buf, (sb)->sz) - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_strscan.c b/third-party/LuaJIT-2.0.2/src/lj_strscan.c deleted file mode 100644 index a71b86a283..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_strscan.c +++ /dev/null @@ -1,497 +0,0 @@ -/* -** String scanning. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include - -#define lj_strscan_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_char.h" -#include "lj_strscan.h" - -/* -- Scanning numbers ---------------------------------------------------- */ - -/* -** Rationale for the builtin string to number conversion library: -** -** It removes a dependency on libc's strtod(), which is a true portability -** nightmare. Mainly due to the plethora of supported OS and toolchain -** combinations. Sadly, the various implementations -** a) are often buggy, incomplete (no hex floats) and/or imprecise, -** b) sometimes crash or hang on certain inputs, -** c) return non-standard NaNs that need to be filtered out, and -** d) fail if the locale-specific decimal separator is not a dot, -** which can only be fixed with atrocious workarounds. -** -** Also, most of the strtod() implementations are hopelessly bloated, -** which is not just an I-cache hog, but a problem for static linkage -** on embedded systems, too. -** -** OTOH the builtin conversion function is very compact. Even though it -** does a lot more, like parsing long longs, octal or imaginary numbers -** and returning the result in different formats: -** a) It needs less than 3 KB (!) of machine code (on x64 with -Os), -** b) it doesn't perform any dynamic allocation and, -** c) it needs only around 600 bytes of stack space. -** -** The builtin function is faster than strtod() for typical inputs, e.g. -** "123", "1.5" or "1e6". Arguably, it's slower for very large exponents, -** which are not very common (this could be fixed, if needed). -** -** And most importantly, the builtin function is equally precise on all -** platforms. It correctly converts and rounds any input to a double. -** If this is not the case, please send a bug report -- but PLEASE verify -** that the implementation you're comparing to is not the culprit! -** -** The implementation quickly pre-scans the entire string first and -** handles simple integers on-the-fly. Otherwise, it dispatches to the -** base-specific parser. Hex and octal is straightforward. -** -** Decimal to binary conversion uses a fixed-length circular buffer in -** base 100. Some simple cases are handled directly. For other cases, the -** number in the buffer is up-scaled or down-scaled until the integer part -** is in the proper range. Then the integer part is rounded and converted -** to a double which is finally rescaled to the result. Denormals need -** special treatment to prevent incorrect 'double rounding'. -*/ - -/* Definitions for circular decimal digit buffer (base 100 = 2 digits/byte). */ -#define STRSCAN_DIG 1024 -#define STRSCAN_MAXDIG 800 /* 772 + extra are sufficient. */ -#define STRSCAN_DDIG (STRSCAN_DIG/2) -#define STRSCAN_DMASK (STRSCAN_DDIG-1) - -/* Helpers for circular buffer. */ -#define DNEXT(a) (((a)+1) & STRSCAN_DMASK) -#define DPREV(a) (((a)-1) & STRSCAN_DMASK) -#define DLEN(lo, hi) ((int32_t)(((lo)-(hi)) & STRSCAN_DMASK)) - -#define casecmp(c, k) (((c) | 0x20) == k) - -/* Final conversion to double. */ -static void strscan_double(uint64_t x, TValue *o, int32_t ex2, int32_t neg) -{ - double n; - - /* Avoid double rounding for denormals. */ - if (LJ_UNLIKELY(ex2 <= -1075 && x != 0)) { - /* NYI: all of this generates way too much code on 32 bit CPUs. */ -#if defined(__GNUC__) && LJ_64 - int32_t b = (int32_t)(__builtin_clzll(x)^63); -#else - int32_t b = (x>>32) ? 32+(int32_t)lj_fls((uint32_t)(x>>32)) : - (int32_t)lj_fls((uint32_t)x); -#endif - if ((int32_t)b + ex2 <= -1023 && (int32_t)b + ex2 >= -1075) { - uint64_t rb = (uint64_t)1 << (-1075-ex2); - if ((x & rb) && ((x & (rb+rb+rb-1)))) x += rb+rb; - x = (x & ~(rb+rb-1)); - } - } - - /* Convert to double using a signed int64_t conversion, then rescale. */ - lua_assert((int64_t)x >= 0); - n = (double)(int64_t)x; - if (neg) n = -n; - if (ex2) n = ldexp(n, ex2); - o->n = n; -} - -/* Parse hexadecimal number. */ -static StrScanFmt strscan_hex(const uint8_t *p, TValue *o, - StrScanFmt fmt, uint32_t opt, - int32_t ex2, int32_t neg, uint32_t dig) -{ - uint64_t x = 0; - uint32_t i; - - /* Scan hex digits. */ - for (i = dig > 16 ? 16 : dig ; i; i--, p++) { - uint32_t d = (*p != '.' ? *p : *++p); if (d > '9') d += 9; - x = (x << 4) + (d & 15); - } - - /* Summarize rounding-effect of excess digits. */ - for (i = 16; i < dig; i++, p++) - x |= ((*p != '.' ? *p : *++p) != '0'), ex2 += 4; - - /* Format-specific handling. */ - switch (fmt) { - case STRSCAN_INT: - if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) { - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_INT; /* Fast path for 32 bit integers. */ - } - if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; break; } - /* fallthrough */ - case STRSCAN_U32: - if (dig > 8) return STRSCAN_ERROR; - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_U32; - case STRSCAN_I64: - case STRSCAN_U64: - if (dig > 16) return STRSCAN_ERROR; - o->u64 = neg ? (uint64_t)-(int64_t)x : x; - return fmt; - default: - break; - } - - /* Reduce range then convert to double. */ - if ((x & U64x(c0000000,0000000))) { x = (x >> 2) | (x & 3); ex2 += 2; } - strscan_double(x, o, ex2, neg); - return fmt; -} - -/* Parse octal number. */ -static StrScanFmt strscan_oct(const uint8_t *p, TValue *o, - StrScanFmt fmt, int32_t neg, uint32_t dig) -{ - uint64_t x = 0; - - /* Scan octal digits. */ - if (dig > 22 || (dig == 22 && *p > '1')) return STRSCAN_ERROR; - while (dig-- > 0) { - if (!(*p >= '0' && *p <= '7')) return STRSCAN_ERROR; - x = (x << 3) + (*p++ & 7); - } - - /* Format-specific handling. */ - switch (fmt) { - case STRSCAN_INT: - if (x >= 0x80000000u+neg) fmt = STRSCAN_U32; - /* fallthrough */ - case STRSCAN_U32: - if ((x >> 32)) return STRSCAN_ERROR; - o->i = neg ? -(int32_t)x : (int32_t)x; - break; - default: - case STRSCAN_I64: - case STRSCAN_U64: - o->u64 = neg ? (uint64_t)-(int64_t)x : x; - break; - } - return fmt; -} - -/* Parse decimal number. */ -static StrScanFmt strscan_dec(const uint8_t *p, TValue *o, - StrScanFmt fmt, uint32_t opt, - int32_t ex10, int32_t neg, uint32_t dig) -{ - uint8_t xi[STRSCAN_DDIG], *xip = xi; - - if (dig) { - uint32_t i = dig; - if (i > STRSCAN_MAXDIG) { - ex10 += (int32_t)(i - STRSCAN_MAXDIG); - i = STRSCAN_MAXDIG; - } - /* Scan unaligned leading digit. */ - if (((ex10^i) & 1)) - *xip++ = ((*p != '.' ? *p : *++p) & 15), i--, p++; - /* Scan aligned double-digits. */ - for ( ; i > 1; i -= 2) { - uint32_t d = 10 * ((*p != '.' ? *p : *++p) & 15); p++; - *xip++ = d + ((*p != '.' ? *p : *++p) & 15); p++; - } - /* Scan and realign trailing digit. */ - if (i) *xip++ = 10 * ((*p != '.' ? *p : *++p) & 15), ex10--, p++; - - /* Summarize rounding-effect of excess digits. */ - if (dig > STRSCAN_MAXDIG) { - do { - if ((*p != '.' ? *p : *++p) != '0') { xip[-1] |= 1; break; } - p++; - } while (--dig > STRSCAN_MAXDIG); - dig = STRSCAN_MAXDIG; - } else { /* Simplify exponent. */ - while (ex10 > 0 && dig <= 18) *xip++ = 0, ex10 -= 2, dig += 2; - } - } else { /* Only got zeros. */ - ex10 = 0; - xi[0] = 0; - } - - /* Fast path for numbers in integer format (but handles e.g. 1e6, too). */ - if (dig <= 20 && ex10 == 0) { - uint8_t *xis; - uint64_t x = xi[0]; - double n; - for (xis = xi+1; xis < xip; xis++) x = x * 100 + *xis; - if (!(dig == 20 && (xi[0] > 18 || (int64_t)x >= 0))) { /* No overflow? */ - /* Format-specific handling. */ - switch (fmt) { - case STRSCAN_INT: - if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) { - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_INT; /* Fast path for 32 bit integers. */ - } - if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; goto plainnumber; } - /* fallthrough */ - case STRSCAN_U32: - if ((x >> 32) != 0) return STRSCAN_ERROR; - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_U32; - case STRSCAN_I64: - case STRSCAN_U64: - o->u64 = neg ? (uint64_t)-(int64_t)x : x; - return fmt; - default: - plainnumber: /* Fast path for plain numbers < 2^63. */ - if ((int64_t)x < 0) break; - n = (double)(int64_t)x; - if (neg) n = -n; - o->n = n; - return fmt; - } - } - } - - /* Slow non-integer path. */ - if (fmt == STRSCAN_INT) { - if ((opt & STRSCAN_OPT_C)) return STRSCAN_ERROR; - fmt = STRSCAN_NUM; - } else if (fmt > STRSCAN_INT) { - return STRSCAN_ERROR; - } - { - uint32_t hi = 0, lo = (uint32_t)(xip-xi); - int32_t ex2 = 0, idig = (int32_t)lo + (ex10 >> 1); - - lua_assert(lo > 0 && (ex10 & 1) == 0); - - /* Handle simple overflow/underflow. */ - if (idig > 310/2) { if (neg) setminfV(o); else setpinfV(o); return fmt; } - else if (idig < -326/2) { o->n = neg ? -0.0 : 0.0; return fmt; } - - /* Scale up until we have at least 17 or 18 integer part digits. */ - while (idig < 9 && idig < DLEN(lo, hi)) { - uint32_t i, cy = 0; - ex2 -= 6; - for (i = DPREV(lo); ; i = DPREV(i)) { - uint32_t d = (xi[i] << 6) + cy; - cy = (((d >> 2) * 5243) >> 17); d = d - cy * 100; /* Div/mod 100. */ - xi[i] = (uint8_t)d; - if (i == hi) break; - if (d == 0 && i == DPREV(lo)) lo = i; - } - if (cy) { - hi = DPREV(hi); - if (xi[DPREV(lo)] == 0) lo = DPREV(lo); - else if (hi == lo) { lo = DPREV(lo); xi[DPREV(lo)] |= xi[lo]; } - xi[hi] = (uint8_t)cy; idig++; - } - } - - /* Scale down until no more than 17 or 18 integer part digits remain. */ - while (idig > 9) { - uint32_t i, cy = 0; - ex2 += 6; - for (i = hi; i != lo; i = DNEXT(i)) { - cy += xi[i]; - xi[i] = (cy >> 6); - cy = 100 * (cy & 0x3f); - if (xi[i] == 0 && i == hi) hi = DNEXT(hi), idig--; - } - while (cy) { - if (hi == lo) { xi[DPREV(lo)] |= 1; break; } - xi[lo] = (cy >> 6); lo = DNEXT(lo); - cy = 100 * (cy & 0x3f); - } - } - - /* Collect integer part digits and convert to rescaled double. */ - { - uint64_t x = xi[hi]; - uint32_t i; - for (i = DNEXT(hi); --idig > 0 && i != lo; i = DNEXT(i)) - x = x * 100 + xi[i]; - if (i == lo) { - while (--idig >= 0) x = x * 100; - } else { /* Gather round bit from remaining digits. */ - x <<= 1; ex2--; - do { - if (xi[i]) { x |= 1; break; } - i = DNEXT(i); - } while (i != lo); - } - strscan_double(x, o, ex2, neg); - } - } - return fmt; -} - -/* Scan string containing a number. Returns format. Returns value in o. */ -StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt) -{ - int32_t neg = 0; - - /* Remove leading space, parse sign and non-numbers. */ - if (LJ_UNLIKELY(!lj_char_isdigit(*p))) { - while (lj_char_isspace(*p)) p++; - if (*p == '+' || *p == '-') neg = (*p++ == '-'); - if (LJ_UNLIKELY(*p >= 'A')) { /* Parse "inf", "infinity" or "nan". */ - TValue tmp; - setnanV(&tmp); - if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'f')) { - if (neg) setminfV(&tmp); else setpinfV(&tmp); - p += 3; - if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'i') && - casecmp(p[3],'t') && casecmp(p[4],'y')) p += 5; - } else if (casecmp(p[0],'n') && casecmp(p[1],'a') && casecmp(p[2],'n')) { - p += 3; - } - while (lj_char_isspace(*p)) p++; - if (*p) return STRSCAN_ERROR; - o->u64 = tmp.u64; - return STRSCAN_NUM; - } - } - - /* Parse regular number. */ - { - StrScanFmt fmt = STRSCAN_INT; - int cmask = LJ_CHAR_DIGIT; - int base = (opt & STRSCAN_OPT_C) && *p == '0' ? 0 : 10; - const uint8_t *sp, *dp = NULL; - uint32_t dig = 0, hasdig = 0, x = 0; - int32_t ex = 0; - - /* Determine base and skip leading zeros. */ - if (LJ_UNLIKELY(*p <= '0')) { - if (*p == '0' && casecmp(p[1], 'x')) - base = 16, cmask = LJ_CHAR_XDIGIT, p += 2; - for ( ; ; p++) { - if (*p == '0') { - hasdig = 1; - } else if (*p == '.') { - if (dp) return STRSCAN_ERROR; - dp = p; - } else { - break; - } - } - } - - /* Preliminary digit and decimal point scan. */ - for (sp = p; ; p++) { - if (LJ_LIKELY(lj_char_isa(*p, cmask))) { - x = x * 10 + (*p & 15); /* For fast path below. */ - dig++; - } else if (*p == '.') { - if (dp) return STRSCAN_ERROR; - dp = p; - } else { - break; - } - } - if (!(hasdig | dig)) return STRSCAN_ERROR; - - /* Handle decimal point. */ - if (dp) { - fmt = STRSCAN_NUM; - if (dig) { - ex = (int32_t)(dp-(p-1)); dp = p-1; - while (ex < 0 && *dp-- == '0') ex++, dig--; /* Skip trailing zeros. */ - if (base == 16) ex *= 4; - } - } - - /* Parse exponent. */ - if (casecmp(*p, (uint32_t)(base == 16 ? 'p' : 'e'))) { - uint32_t xx; - int negx = 0; - fmt = STRSCAN_NUM; p++; - if (*p == '+' || *p == '-') negx = (*p++ == '-'); - if (!lj_char_isdigit(*p)) return STRSCAN_ERROR; - xx = (*p++ & 15); - while (lj_char_isdigit(*p)) { - if (xx < 65536) xx = xx * 10 + (*p & 15); - p++; - } - ex += negx ? -(int32_t)xx : (int32_t)xx; - } - - /* Parse suffix. */ - if (*p) { - /* I (IMAG), U (U32), LL (I64), ULL/LLU (U64), L (long), UL/LU (ulong). */ - /* NYI: f (float). Not needed until cp_number() handles non-integers. */ - if (casecmp(*p, 'i')) { - if (!(opt & STRSCAN_OPT_IMAG)) return STRSCAN_ERROR; - p++; fmt = STRSCAN_IMAG; - } else if (fmt == STRSCAN_INT) { - if (casecmp(*p, 'u')) p++, fmt = STRSCAN_U32; - if (casecmp(*p, 'l')) { - p++; - if (casecmp(*p, 'l')) p++, fmt += STRSCAN_I64 - STRSCAN_INT; - else if (!(opt & STRSCAN_OPT_C)) return STRSCAN_ERROR; - else if (sizeof(long) == 8) fmt += STRSCAN_I64 - STRSCAN_INT; - } - if (casecmp(*p, 'u') && (fmt == STRSCAN_INT || fmt == STRSCAN_I64)) - p++, fmt += STRSCAN_U32 - STRSCAN_INT; - if ((fmt == STRSCAN_U32 && !(opt & STRSCAN_OPT_C)) || - (fmt >= STRSCAN_I64 && !(opt & STRSCAN_OPT_LL))) - return STRSCAN_ERROR; - } - while (lj_char_isspace(*p)) p++; - if (*p) return STRSCAN_ERROR; - } - - /* Fast path for decimal 32 bit integers. */ - if (fmt == STRSCAN_INT && base == 10 && - (dig < 10 || (dig == 10 && *sp <= '2' && x < 0x80000000u+neg))) { - int32_t y = neg ? -(int32_t)x : (int32_t)x; - if ((opt & STRSCAN_OPT_TONUM)) { - o->n = (double)y; - return STRSCAN_NUM; - } else { - o->i = y; - return STRSCAN_INT; - } - } - - /* Dispatch to base-specific parser. */ - if (base == 0 && !(fmt == STRSCAN_NUM || fmt == STRSCAN_IMAG)) - return strscan_oct(sp, o, fmt, neg, dig); - if (base == 16) - fmt = strscan_hex(sp, o, fmt, opt, ex, neg, dig); - else - fmt = strscan_dec(sp, o, fmt, opt, ex, neg, dig); - - /* Try to convert number to integer, if requested. */ - if (fmt == STRSCAN_NUM && (opt & STRSCAN_OPT_TOINT)) { - double n = o->n; - int32_t i = lj_num2int(n); - if (n == (lua_Number)i) { o->i = i; return STRSCAN_INT; } - } - return fmt; - } -} - -int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o) -{ - StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), o, - STRSCAN_OPT_TONUM); - lua_assert(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM); - return (fmt != STRSCAN_ERROR); -} - -#if LJ_DUALNUM -int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o) -{ - StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), o, - STRSCAN_OPT_TOINT); - lua_assert(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM || fmt == STRSCAN_INT); - if (fmt == STRSCAN_INT) setitype(o, LJ_TISNUM); - return (fmt != STRSCAN_ERROR); -} -#endif - -#undef DNEXT -#undef DPREV -#undef DLEN - diff --git a/third-party/LuaJIT-2.0.2/src/lj_strscan.h b/third-party/LuaJIT-2.0.2/src/lj_strscan.h deleted file mode 100644 index 9557d67382..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_strscan.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -** String scanning. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_STRSCAN_H -#define _LJ_STRSCAN_H - -#include "lj_obj.h" - -/* Options for accepted/returned formats. */ -#define STRSCAN_OPT_TOINT 0x01 /* Convert to int32_t, if possible. */ -#define STRSCAN_OPT_TONUM 0x02 /* Always convert to double. */ -#define STRSCAN_OPT_IMAG 0x04 -#define STRSCAN_OPT_LL 0x08 -#define STRSCAN_OPT_C 0x10 - -/* Returned format. */ -typedef enum { - STRSCAN_ERROR, - STRSCAN_NUM, STRSCAN_IMAG, - STRSCAN_INT, STRSCAN_U32, STRSCAN_I64, STRSCAN_U64, -} StrScanFmt; - -LJ_FUNC StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt); -LJ_FUNC int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o); -#if LJ_DUALNUM -LJ_FUNC int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o); -#else -#define lj_strscan_number(s, o) lj_strscan_num((s), (o)) -#endif - -/* Check for number or convert string to number/int in-place (!). */ -static LJ_AINLINE int lj_strscan_numberobj(TValue *o) -{ - return tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), o)); -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_tab.c b/third-party/LuaJIT-2.0.2/src/lj_tab.c deleted file mode 100644 index ccad1f6872..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_tab.c +++ /dev/null @@ -1,624 +0,0 @@ -/* -** Table handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_tab_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" - -/* -- Object hashing ------------------------------------------------------ */ - -/* Hash values are masked with the table hash mask and used as an index. */ -static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash) -{ - Node *n = noderef(t->node); - return &n[hash & t->hmask]; -} - -/* String hashes are precomputed when they are interned. */ -#define hashstr(t, s) hashmask(t, (s)->hash) - -#define hashlohi(t, lo, hi) hashmask((t), hashrot((lo), (hi))) -#define hashnum(t, o) hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1)) -#define hashptr(t, p) hashlohi((t), u32ptr(p), u32ptr(p) + HASH_BIAS) -#define hashgcref(t, r) hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS) - -/* Hash an arbitrary key and return its anchor position in the hash table. */ -static Node *hashkey(const GCtab *t, cTValue *key) -{ - lua_assert(!tvisint(key)); - if (tvisstr(key)) - return hashstr(t, strV(key)); - else if (tvisnum(key)) - return hashnum(t, key); - else if (tvisbool(key)) - return hashmask(t, boolV(key)); - else - return hashgcref(t, key->gcr); - /* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */ -} - -/* -- Table creation and destruction -------------------------------------- */ - -/* Create new hash part for table. */ -static LJ_AINLINE void newhpart(lua_State *L, GCtab *t, uint32_t hbits) -{ - uint32_t hsize; - Node *node; - lua_assert(hbits != 0); - if (hbits > LJ_MAX_HBITS) - lj_err_msg(L, LJ_ERR_TABOV); - hsize = 1u << hbits; - node = lj_mem_newvec(L, hsize, Node); - setmref(node->freetop, &node[hsize]); - setmref(t->node, node); - t->hmask = hsize-1; -} - -/* -** Q: Why all of these copies of t->hmask, t->node etc. to local variables? -** A: Because alias analysis for C is _really_ tough. -** Even state-of-the-art C compilers won't produce good code without this. -*/ - -/* Clear hash part of table. */ -static LJ_AINLINE void clearhpart(GCtab *t) -{ - uint32_t i, hmask = t->hmask; - Node *node = noderef(t->node); - lua_assert(t->hmask != 0); - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - setmref(n->next, NULL); - setnilV(&n->key); - setnilV(&n->val); - } -} - -/* Clear array part of table. */ -static LJ_AINLINE void clearapart(GCtab *t) -{ - uint32_t i, asize = t->asize; - TValue *array = tvref(t->array); - for (i = 0; i < asize; i++) - setnilV(&array[i]); -} - -/* Create a new table. Note: the slots are not initialized (yet). */ -static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits) -{ - GCtab *t; - /* First try to colocate the array part. */ - if (LJ_MAX_COLOSIZE != 0 && asize > 0 && asize <= LJ_MAX_COLOSIZE) { - lua_assert((sizeof(GCtab) & 7) == 0); - t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize)); - t->gct = ~LJ_TTAB; - t->nomm = (uint8_t)~0; - t->colo = (int8_t)asize; - setmref(t->array, (TValue *)((char *)t + sizeof(GCtab))); - setgcrefnull(t->metatable); - t->asize = asize; - t->hmask = 0; - setmref(t->node, &G(L)->nilnode); - } else { /* Otherwise separately allocate the array part. */ - t = lj_mem_newobj(L, GCtab); - t->gct = ~LJ_TTAB; - t->nomm = (uint8_t)~0; - t->colo = 0; - setmref(t->array, NULL); - setgcrefnull(t->metatable); - t->asize = 0; /* In case the array allocation fails. */ - t->hmask = 0; - setmref(t->node, &G(L)->nilnode); - if (asize > 0) { - if (asize > LJ_MAX_ASIZE) - lj_err_msg(L, LJ_ERR_TABOV); - setmref(t->array, lj_mem_newvec(L, asize, TValue)); - t->asize = asize; - } - } - if (hbits) - newhpart(L, t, hbits); - return t; -} - -/* Create a new table. -** -** IMPORTANT NOTE: The API differs from lua_createtable()! -** -** The array size is non-inclusive. E.g. asize=128 creates array slots -** for 0..127, but not for 128. If you need slots 1..128, pass asize=129 -** (slot 0 is wasted in this case). -** -** The hash size is given in hash bits. hbits=0 means no hash part. -** hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on. -*/ -GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits) -{ - GCtab *t = newtab(L, asize, hbits); - clearapart(t); - if (t->hmask > 0) clearhpart(t); - return t; -} - -#if LJ_HASJIT -GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize) -{ - GCtab *t = newtab(L, ahsize & 0xffffff, ahsize >> 24); - clearapart(t); - if (t->hmask > 0) clearhpart(t); - return t; -} -#endif - -/* Duplicate a table. */ -GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt) -{ - GCtab *t; - uint32_t asize, hmask; - t = newtab(L, kt->asize, kt->hmask > 0 ? lj_fls(kt->hmask)+1 : 0); - lua_assert(kt->asize == t->asize && kt->hmask == t->hmask); - t->nomm = 0; /* Keys with metamethod names may be present. */ - asize = kt->asize; - if (asize > 0) { - TValue *array = tvref(t->array); - TValue *karray = tvref(kt->array); - if (asize < 64) { /* An inlined loop beats memcpy for < 512 bytes. */ - uint32_t i; - for (i = 0; i < asize; i++) - copyTV(L, &array[i], &karray[i]); - } else { - memcpy(array, karray, asize*sizeof(TValue)); - } - } - hmask = kt->hmask; - if (hmask > 0) { - uint32_t i; - Node *node = noderef(t->node); - Node *knode = noderef(kt->node); - ptrdiff_t d = (char *)node - (char *)knode; - setmref(node->freetop, (Node *)((char *)noderef(knode->freetop) + d)); - for (i = 0; i <= hmask; i++) { - Node *kn = &knode[i]; - Node *n = &node[i]; - Node *next = nextnode(kn); - /* Don't use copyTV here, since it asserts on a copy of a dead key. */ - n->val = kn->val; n->key = kn->key; - setmref(n->next, next == NULL? next : (Node *)((char *)next + d)); - } - } - return t; -} - -/* Free a table. */ -void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t) -{ - if (t->hmask > 0) - lj_mem_freevec(g, noderef(t->node), t->hmask+1, Node); - if (t->asize > 0 && LJ_MAX_COLOSIZE != 0 && t->colo <= 0) - lj_mem_freevec(g, tvref(t->array), t->asize, TValue); - if (LJ_MAX_COLOSIZE != 0 && t->colo) - lj_mem_free(g, t, sizetabcolo((uint32_t)t->colo & 0x7f)); - else - lj_mem_freet(g, t); -} - -/* -- Table resizing ------------------------------------------------------ */ - -/* Resize a table to fit the new array/hash part sizes. */ -static void resizetab(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits) -{ - Node *oldnode = noderef(t->node); - uint32_t oldasize = t->asize; - uint32_t oldhmask = t->hmask; - if (asize > oldasize) { /* Array part grows? */ - TValue *array; - uint32_t i; - if (asize > LJ_MAX_ASIZE) - lj_err_msg(L, LJ_ERR_TABOV); - if (LJ_MAX_COLOSIZE != 0 && t->colo > 0) { - /* A colocated array must be separated and copied. */ - TValue *oarray = tvref(t->array); - array = lj_mem_newvec(L, asize, TValue); - t->colo = (int8_t)(t->colo | 0x80); /* Mark as separated (colo < 0). */ - for (i = 0; i < oldasize; i++) - copyTV(L, &array[i], &oarray[i]); - } else { - array = (TValue *)lj_mem_realloc(L, tvref(t->array), - oldasize*sizeof(TValue), asize*sizeof(TValue)); - } - setmref(t->array, array); - t->asize = asize; - for (i = oldasize; i < asize; i++) /* Clear newly allocated slots. */ - setnilV(&array[i]); - } - /* Create new (empty) hash part. */ - if (hbits) { - newhpart(L, t, hbits); - clearhpart(t); - } else { - global_State *g = G(L); - setmref(t->node, &g->nilnode); - t->hmask = 0; - } - if (asize < oldasize) { /* Array part shrinks? */ - TValue *array = tvref(t->array); - uint32_t i; - t->asize = asize; /* Note: This 'shrinks' even colocated arrays. */ - for (i = asize; i < oldasize; i++) /* Reinsert old array values. */ - if (!tvisnil(&array[i])) - copyTV(L, lj_tab_setinth(L, t, (int32_t)i), &array[i]); - /* Physically shrink only separated arrays. */ - if (LJ_MAX_COLOSIZE != 0 && t->colo <= 0) - setmref(t->array, lj_mem_realloc(L, array, - oldasize*sizeof(TValue), asize*sizeof(TValue))); - } - if (oldhmask > 0) { /* Reinsert pairs from old hash part. */ - global_State *g; - uint32_t i; - for (i = 0; i <= oldhmask; i++) { - Node *n = &oldnode[i]; - if (!tvisnil(&n->val)) - copyTV(L, lj_tab_set(L, t, &n->key), &n->val); - } - g = G(L); - lj_mem_freevec(g, oldnode, oldhmask+1, Node); - } -} - -static uint32_t countint(cTValue *key, uint32_t *bins) -{ - lua_assert(!tvisint(key)); - if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) { - bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++; - return 1; - } - } - return 0; -} - -static uint32_t countarray(const GCtab *t, uint32_t *bins) -{ - uint32_t na, b, i; - if (t->asize == 0) return 0; - for (na = i = b = 0; b < LJ_MAX_ABITS; b++) { - uint32_t n, top = 2u << b; - TValue *array; - if (top >= t->asize) { - top = t->asize-1; - if (i > top) - break; - } - array = tvref(t->array); - for (n = 0; i <= top; i++) - if (!tvisnil(&array[i])) - n++; - bins[b] += n; - na += n; - } - return na; -} - -static uint32_t counthash(const GCtab *t, uint32_t *bins, uint32_t *narray) -{ - uint32_t total, na, i, hmask = t->hmask; - Node *node = noderef(t->node); - for (total = na = 0, i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (!tvisnil(&n->val)) { - na += countint(&n->key, bins); - total++; - } - } - *narray += na; - return total; -} - -static uint32_t bestasize(uint32_t bins[], uint32_t *narray) -{ - uint32_t b, sum, na = 0, sz = 0, nn = *narray; - for (b = 0, sum = 0; 2*nn > (1u< 0 && 2*(sum += bins[b]) > (1u<hmask > 0 ? lj_fls(t->hmask)+1 : 0); -} - -/* -- Table getters ------------------------------------------------------- */ - -cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key) -{ - TValue k; - Node *n; - k.n = (lua_Number)key; - n = hashnum(t, &k); - do { - if (tvisnum(&n->key) && n->key.n == k.n) - return &n->val; - } while ((n = nextnode(n))); - return NULL; -} - -cTValue *lj_tab_getstr(GCtab *t, GCstr *key) -{ - Node *n = hashstr(t, key); - do { - if (tvisstr(&n->key) && strV(&n->key) == key) - return &n->val; - } while ((n = nextnode(n))); - return NULL; -} - -cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key) -{ - if (tvisstr(key)) { - cTValue *tv = lj_tab_getstr(t, strV(key)); - if (tv) - return tv; - } else if (tvisint(key)) { - cTValue *tv = lj_tab_getint(t, intV(key)); - if (tv) - return tv; - } else if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if (nk == (lua_Number)k) { - cTValue *tv = lj_tab_getint(t, k); - if (tv) - return tv; - } else { - goto genlookup; /* Else use the generic lookup. */ - } - } else if (!tvisnil(key)) { - Node *n; - genlookup: - n = hashkey(t, key); - do { - if (lj_obj_equal(&n->key, key)) - return &n->val; - } while ((n = nextnode(n))); - } - return niltv(L); -} - -/* -- Table setters ------------------------------------------------------- */ - -/* Insert new key. Use Brent's variation to optimize the chain length. */ -TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key) -{ - Node *n = hashkey(t, key); - if (!tvisnil(&n->val) || t->hmask == 0) { - Node *nodebase = noderef(t->node); - Node *collide, *freenode = noderef(nodebase->freetop); - lua_assert(freenode >= nodebase && freenode <= nodebase+t->hmask+1); - do { - if (freenode == nodebase) { /* No free node found? */ - rehashtab(L, t, key); /* Rehash table. */ - return lj_tab_set(L, t, key); /* Retry key insertion. */ - } - } while (!tvisnil(&(--freenode)->key)); - setmref(nodebase->freetop, freenode); - lua_assert(freenode != &G(L)->nilnode); - collide = hashkey(t, &n->key); - if (collide != n) { /* Colliding node not the main node? */ - while (noderef(collide->next) != n) /* Find predecessor. */ - collide = nextnode(collide); - setmref(collide->next, freenode); /* Relink chain. */ - /* Copy colliding node into free node and free main node. */ - freenode->val = n->val; - freenode->key = n->key; - freenode->next = n->next; - setmref(n->next, NULL); - setnilV(&n->val); - /* Rechain pseudo-resurrected string keys with colliding hashes. */ - while (nextnode(freenode)) { - Node *nn = nextnode(freenode); - if (tvisstr(&nn->key) && !tvisnil(&nn->val) && - hashstr(t, strV(&nn->key)) == n) { - freenode->next = nn->next; - nn->next = n->next; - setmref(n->next, nn); - } else { - freenode = nn; - } - } - } else { /* Otherwise use free node. */ - setmrefr(freenode->next, n->next); /* Insert into chain. */ - setmref(n->next, freenode); - n = freenode; - } - } - n->key.u64 = key->u64; - if (LJ_UNLIKELY(tvismzero(&n->key))) - n->key.u64 = 0; - lj_gc_anybarriert(L, t); - lua_assert(tvisnil(&n->val)); - return &n->val; -} - -TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key) -{ - TValue k; - Node *n; - k.n = (lua_Number)key; - n = hashnum(t, &k); - do { - if (tvisnum(&n->key) && n->key.n == k.n) - return &n->val; - } while ((n = nextnode(n))); - return lj_tab_newkey(L, t, &k); -} - -TValue *lj_tab_setstr(lua_State *L, GCtab *t, GCstr *key) -{ - TValue k; - Node *n = hashstr(t, key); - do { - if (tvisstr(&n->key) && strV(&n->key) == key) - return &n->val; - } while ((n = nextnode(n))); - setstrV(L, &k, key); - return lj_tab_newkey(L, t, &k); -} - -TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key) -{ - Node *n; - t->nomm = 0; /* Invalidate negative metamethod cache. */ - if (tvisstr(key)) { - return lj_tab_setstr(L, t, strV(key)); - } else if (tvisint(key)) { - return lj_tab_setint(L, t, intV(key)); - } else if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if (nk == (lua_Number)k) - return lj_tab_setint(L, t, k); - if (tvisnan(key)) - lj_err_msg(L, LJ_ERR_NANIDX); - /* Else use the generic lookup. */ - } else if (tvisnil(key)) { - lj_err_msg(L, LJ_ERR_NILIDX); - } - n = hashkey(t, key); - do { - if (lj_obj_equal(&n->key, key)) - return &n->val; - } while ((n = nextnode(n))); - return lj_tab_newkey(L, t, key); -} - -/* -- Table traversal ----------------------------------------------------- */ - -/* Get the traversal index of a key. */ -static uint32_t keyindex(lua_State *L, GCtab *t, cTValue *key) -{ - TValue tmp; - if (tvisint(key)) { - int32_t k = intV(key); - if ((uint32_t)k < t->asize) - return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */ - setnumV(&tmp, (lua_Number)k); - key = &tmp; - } else if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if ((uint32_t)k < t->asize && nk == (lua_Number)k) - return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */ - } - if (!tvisnil(key)) { - Node *n = hashkey(t, key); - do { - if (lj_obj_equal(&n->key, key)) - return t->asize + (uint32_t)(n - noderef(t->node)); - /* Hash key indexes: [t->asize..t->asize+t->nmask] */ - } while ((n = nextnode(n))); - if (key->u32.hi == 0xfffe7fff) /* ITERN was despecialized while running. */ - return key->u32.lo - 1; - lj_err_msg(L, LJ_ERR_NEXTIDX); - return 0; /* unreachable */ - } - return ~0u; /* A nil key starts the traversal. */ -} - -/* Advance to the next step in a table traversal. */ -int lj_tab_next(lua_State *L, GCtab *t, TValue *key) -{ - uint32_t i = keyindex(L, t, key); /* Find predecessor key index. */ - for (i++; i < t->asize; i++) /* First traverse the array keys. */ - if (!tvisnil(arrayslot(t, i))) { - setintV(key, i); - copyTV(L, key+1, arrayslot(t, i)); - return 1; - } - for (i -= t->asize; i <= t->hmask; i++) { /* Then traverse the hash keys. */ - Node *n = &noderef(t->node)[i]; - if (!tvisnil(&n->val)) { - copyTV(L, key, &n->key); - copyTV(L, key+1, &n->val); - return 1; - } - } - return 0; /* End of traversal. */ -} - -/* -- Table length calculation -------------------------------------------- */ - -static MSize unbound_search(GCtab *t, MSize j) -{ - cTValue *tv; - MSize i = j; /* i is zero or a present index */ - j++; - /* find `i' and `j' such that i is present and j is not */ - while ((tv = lj_tab_getint(t, (int32_t)j)) && !tvisnil(tv)) { - i = j; - j *= 2; - if (j > (MSize)(INT_MAX-2)) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ - i = 1; - while ((tv = lj_tab_getint(t, (int32_t)i)) && !tvisnil(tv)) i++; - return i - 1; - } - } - /* now do a binary search between them */ - while (j - i > 1) { - MSize m = (i+j)/2; - cTValue *tvb = lj_tab_getint(t, (int32_t)m); - if (tvb && !tvisnil(tvb)) i = m; else j = m; - } - return i; -} - -/* -** Try to find a boundary in table `t'. A `boundary' is an integer index -** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). -*/ -MSize LJ_FASTCALL lj_tab_len(GCtab *t) -{ - MSize j = (MSize)t->asize; - if (j > 1 && tvisnil(arrayslot(t, j-1))) { - MSize i = 1; - while (j - i > 1) { - MSize m = (i+j)/2; - if (tvisnil(arrayslot(t, m-1))) j = m; else i = m; - } - return i-1; - } - if (j) j--; - if (t->hmask <= 0) - return j; - return unbound_search(t, j); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_tab.h b/third-party/LuaJIT-2.0.2/src/lj_tab.h deleted file mode 100644 index 2787caa0d6..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_tab.h +++ /dev/null @@ -1,67 +0,0 @@ -/* -** Table handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TAB_H -#define _LJ_TAB_H - -#include "lj_obj.h" - -/* Hash constants. Tuned using a brute force search. */ -#define HASH_BIAS (-0x04c11db7) -#define HASH_ROT1 14 -#define HASH_ROT2 5 -#define HASH_ROT3 13 - -/* Scramble the bits of numbers and pointers. */ -static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi) -{ -#if LJ_TARGET_X86ORX64 - /* Prefer variant that compiles well for a 2-operand CPU. */ - lo ^= hi; hi = lj_rol(hi, HASH_ROT1); - lo -= hi; hi = lj_rol(hi, HASH_ROT2); - hi ^= lo; hi -= lj_rol(lo, HASH_ROT3); -#else - lo ^= hi; - lo = lo - lj_rol(hi, HASH_ROT1); - hi = lo ^ lj_rol(hi, HASH_ROT1 + HASH_ROT2); - hi = hi - lj_rol(lo, HASH_ROT3); -#endif - return hi; -} - -#define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0) - -LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits); -#if LJ_HASJIT -LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize); -#endif -LJ_FUNCA GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt); -LJ_FUNC void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t); -LJ_FUNCA void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize); - -/* Caveat: all getters except lj_tab_get() can return NULL! */ - -LJ_FUNCA cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key); -LJ_FUNC cTValue *lj_tab_getstr(GCtab *t, GCstr *key); -LJ_FUNCA cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key); - -/* Caveat: all setters require a write barrier for the stored value. */ - -LJ_FUNCA TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key); -LJ_FUNC TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key); -LJ_FUNC TValue *lj_tab_setstr(lua_State *L, GCtab *t, GCstr *key); -LJ_FUNC TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key); - -#define inarray(t, key) ((MSize)(key) < (MSize)(t)->asize) -#define arrayslot(t, i) (&tvref((t)->array)[(i)]) -#define lj_tab_getint(t, key) \ - (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_getinth((t), (key))) -#define lj_tab_setint(L, t, key) \ - (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_setinth(L, (t), (key))) - -LJ_FUNCA int lj_tab_next(lua_State *L, GCtab *t, TValue *key); -LJ_FUNCA MSize LJ_FASTCALL lj_tab_len(GCtab *t); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_target.h b/third-party/LuaJIT-2.0.2/src/lj_target.h deleted file mode 100644 index eed69d1236..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_target.h +++ /dev/null @@ -1,162 +0,0 @@ -/* -** Definitions for target CPU. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_H -#define _LJ_TARGET_H - -#include "lj_def.h" -#include "lj_arch.h" - -/* -- Registers and spill slots ------------------------------------------- */ - -/* Register type (uint8_t in ir->r). */ -typedef uint32_t Reg; - -/* The hi-bit is NOT set for an allocated register. This means the value -** can be directly used without masking. The hi-bit is set for a register -** allocation hint or for RID_INIT, RID_SINK or RID_SUNK. -*/ -#define RID_NONE 0x80 -#define RID_MASK 0x7f -#define RID_INIT (RID_NONE|RID_MASK) -#define RID_SINK (RID_INIT-1) -#define RID_SUNK (RID_INIT-2) - -#define ra_noreg(r) ((r) & RID_NONE) -#define ra_hasreg(r) (!((r) & RID_NONE)) - -/* The ra_hashint() macro assumes a previous test for ra_noreg(). */ -#define ra_hashint(r) ((r) < RID_SUNK) -#define ra_gethint(r) ((Reg)((r) & RID_MASK)) -#define ra_sethint(rr, r) rr = (uint8_t)((r)|RID_NONE) -#define ra_samehint(r1, r2) (ra_gethint((r1)^(r2)) == 0) - -/* Spill slot 0 means no spill slot has been allocated. */ -#define SPS_NONE 0 - -#define ra_hasspill(s) ((s) != SPS_NONE) - -/* Combined register and spill slot (uint16_t in ir->prev). */ -typedef uint32_t RegSP; - -#define REGSP(r, s) ((r) + ((s) << 8)) -#define REGSP_HINT(r) ((r)|RID_NONE) -#define REGSP_INIT REGSP(RID_INIT, 0) - -#define regsp_reg(rs) ((rs) & 255) -#define regsp_spill(rs) ((rs) >> 8) -#define regsp_used(rs) \ - (((rs) & ~REGSP(RID_MASK, 0)) != REGSP(RID_NONE, 0)) - -/* -- Register sets ------------------------------------------------------- */ - -/* Bitset for registers. 32 registers suffice for most architectures. -** Note that one set holds bits for both GPRs and FPRs. -*/ -#if LJ_TARGET_PPC || LJ_TARGET_MIPS -typedef uint64_t RegSet; -#else -typedef uint32_t RegSet; -#endif - -#define RID2RSET(r) (((RegSet)1) << (r)) -#define RSET_EMPTY ((RegSet)0) -#define RSET_RANGE(lo, hi) ((RID2RSET((hi)-(lo))-1) << (lo)) - -#define rset_test(rs, r) ((int)((rs) >> (r)) & 1) -#define rset_set(rs, r) (rs |= RID2RSET(r)) -#define rset_clear(rs, r) (rs &= ~RID2RSET(r)) -#define rset_exclude(rs, r) (rs & ~RID2RSET(r)) -#if LJ_TARGET_PPC || LJ_TARGET_MIPS -#define rset_picktop(rs) ((Reg)(__builtin_clzll(rs)^63)) -#define rset_pickbot(rs) ((Reg)__builtin_ctzll(rs)) -#else -#define rset_picktop(rs) ((Reg)lj_fls(rs)) -#define rset_pickbot(rs) ((Reg)lj_ffs(rs)) -#endif - -/* -- Register allocation cost -------------------------------------------- */ - -/* The register allocation heuristic keeps track of the cost for allocating -** a specific register: -** -** A free register (obviously) has a cost of 0 and a 1-bit in the free mask. -** -** An already allocated register has the (non-zero) IR reference in the lowest -** bits and the result of a blended cost-model in the higher bits. -** -** The allocator first checks the free mask for a hit. Otherwise an (unrolled) -** linear search for the minimum cost is used. The search doesn't need to -** keep track of the position of the minimum, which makes it very fast. -** The lowest bits of the minimum cost show the desired IR reference whose -** register is the one to evict. -** -** Without the cost-model this degenerates to the standard heuristics for -** (reverse) linear-scan register allocation. Since code generation is done -** in reverse, a live interval extends from the last use to the first def. -** For an SSA IR the IR reference is the first (and only) def and thus -** trivially marks the end of the interval. The LSRA heuristics says to pick -** the register whose live interval has the furthest extent, i.e. the lowest -** IR reference in our case. -** -** A cost-model should take into account other factors, like spill-cost and -** restore- or rematerialization-cost, which depend on the kind of instruction. -** E.g. constants have zero spill costs, variant instructions have higher -** costs than invariants and PHIs should preferably never be spilled. -** -** Here's a first cut at simple, but effective blended cost-model for R-LSRA: -** - Due to careful design of the IR, constants already have lower IR -** references than invariants and invariants have lower IR references -** than variants. -** - The cost in the upper 16 bits is the sum of the IR reference and a -** weighted score. The score currently only takes into account whether -** the IRT_ISPHI bit is set in the instruction type. -** - The PHI weight is the minimum distance (in IR instructions) a PHI -** reference has to be further apart from a non-PHI reference to be spilled. -** - It should be a power of two (for speed) and must be between 2 and 32768. -** Good values for the PHI weight seem to be between 40 and 150. -** - Further study is required. -*/ -#define REGCOST_PHI_WEIGHT 64 - -/* Cost for allocating a specific register. */ -typedef uint32_t RegCost; - -/* Note: assumes 16 bit IRRef1. */ -#define REGCOST(cost, ref) ((RegCost)(ref) + ((RegCost)(cost) << 16)) -#define regcost_ref(rc) ((IRRef1)(rc)) - -#define REGCOST_T(t) \ - ((RegCost)((t)&IRT_ISPHI) * (((RegCost)(REGCOST_PHI_WEIGHT)<<16)/IRT_ISPHI)) -#define REGCOST_REF_T(ref, t) (REGCOST((ref), (ref)) + REGCOST_T((t))) - -/* -- Target-specific definitions ----------------------------------------- */ - -#if LJ_TARGET_X86ORX64 -#include "lj_target_x86.h" -#elif LJ_TARGET_ARM -#include "lj_target_arm.h" -#elif LJ_TARGET_PPC -#include "lj_target_ppc.h" -#elif LJ_TARGET_MIPS -#include "lj_target_mips.h" -#else -#error "Missing include for target CPU" -#endif - -#ifdef EXITSTUBS_PER_GROUP -/* Return the address of an exit stub. */ -static LJ_AINLINE char *exitstub_addr_(char **group, uint32_t exitno) -{ - lua_assert(group[exitno / EXITSTUBS_PER_GROUP] != NULL); - return (char *)group[exitno / EXITSTUBS_PER_GROUP] + - EXITSTUB_SPACING*(exitno % EXITSTUBS_PER_GROUP); -} -/* Avoid dependence on lj_jit.h if only including lj_target.h. */ -#define exitstub_addr(J, exitno) \ - ((MCode *)exitstub_addr_((char **)((J)->exitstubgroup), (exitno))) -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_target_arm.h b/third-party/LuaJIT-2.0.2/src/lj_target_arm.h deleted file mode 100644 index bec55772b1..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_target_arm.h +++ /dev/null @@ -1,274 +0,0 @@ -/* -** Definitions for ARM CPUs. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_ARM_H -#define _LJ_TARGET_ARM_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#define GPRDEF(_) \ - _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \ - _(R8) _(R9) _(R10) _(R11) _(R12) _(SP) _(LR) _(PC) -#if LJ_SOFTFP -#define FPRDEF(_) -#else -#define FPRDEF(_) \ - _(D0) _(D1) _(D2) _(D3) _(D4) _(D5) _(D6) _(D7) \ - _(D8) _(D9) _(D10) _(D11) _(D12) _(D13) _(D14) _(D15) -#endif -#define VRIDDEF(_) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_TMP = RID_LR, - - /* Calling conventions. */ - RID_RET = RID_R0, - RID_RETLO = RID_R0, - RID_RETHI = RID_R1, -#if LJ_SOFTFP - RID_FPRET = RID_R0, -#else - RID_FPRET = RID_D0, -#endif - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_R9, /* Interpreter BASE. */ - RID_LPC = RID_R6, /* Interpreter PC. */ - RID_DISPATCH = RID_R7, /* Interpreter DISPATCH table. */ - RID_LREG = RID_R8, /* Interpreter L. */ - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_R0, - RID_MAX_GPR = RID_PC+1, - RID_MIN_FPR = RID_MAX_GPR, -#if LJ_SOFTFP - RID_MAX_FPR = RID_MIN_FPR, -#else - RID_MAX_FPR = RID_D15+1, -#endif - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR -}; - -#define RID_NUM_KREF RID_NUM_GPR -#define RID_MIN_KREF RID_R0 - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except sp, lr and pc. */ -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_R12+1)) -#define RSET_GPREVEN \ - (RID2RSET(RID_R0)|RID2RSET(RID_R2)|RID2RSET(RID_R4)|RID2RSET(RID_R6)| \ - RID2RSET(RID_R8)|RID2RSET(RID_R10)) -#define RSET_GPRODD \ - (RID2RSET(RID_R1)|RID2RSET(RID_R3)|RID2RSET(RID_R5)|RID2RSET(RID_R7)| \ - RID2RSET(RID_R9)|RID2RSET(RID_R11)) -#if LJ_SOFTFP -#define RSET_FPR 0 -#else -#define RSET_FPR (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)) -#endif -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -/* ABI-specific register sets. lr is an implicit scratch register. */ -#define RSET_SCRATCH_GPR_ (RSET_RANGE(RID_R0, RID_R3+1)|RID2RSET(RID_R12)) -#ifdef __APPLE__ -#define RSET_SCRATCH_GPR (RSET_SCRATCH_GPR_|RID2RSET(RID_R9)) -#else -#define RSET_SCRATCH_GPR RSET_SCRATCH_GPR_ -#endif -#if LJ_SOFTFP -#define RSET_SCRATCH_FPR 0 -#else -#define RSET_SCRATCH_FPR (RSET_RANGE(RID_D0, RID_D7+1)) -#endif -#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) -#define REGARG_FIRSTGPR RID_R0 -#define REGARG_LASTGPR RID_R3 -#define REGARG_NUMGPR 4 -#if LJ_ABI_SOFTFP -#define REGARG_FIRSTFPR 0 -#define REGARG_LASTFPR 0 -#define REGARG_NUMFPR 0 -#else -#define REGARG_FIRSTFPR RID_D0 -#define REGARG_LASTFPR RID_D7 -#define REGARG_NUMFPR 8 -#endif - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. -*/ -#define SPS_FIXED 2 -#define SPS_FIRST 2 - -#define SPOFS_TMP 0 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { -#if !LJ_SOFTFP - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ -#endif - int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* PC after instruction that caused an exit. Used to find the trace number. */ -#define EXITSTATE_PCREG RID_PC -/* Highest exit + 1 indicates stack check. */ -#define EXITSTATE_CHECKEXIT 1 - -#define EXITSTUB_SPACING 4 -#define EXITSTUBS_PER_GROUP 32 - -/* -- Instructions -------------------------------------------------------- */ - -/* Instruction fields. */ -#define ARMF_CC(ai, cc) (((ai) ^ ARMI_CCAL) | ((cc) << 28)) -#define ARMF_N(r) ((r) << 16) -#define ARMF_D(r) ((r) << 12) -#define ARMF_S(r) ((r) << 8) -#define ARMF_M(r) (r) -#define ARMF_SH(sh, n) (((sh) << 5) | ((n) << 7)) -#define ARMF_RSH(sh, r) (0x10 | ((sh) << 5) | ARMF_S(r)) - -typedef enum ARMIns { - ARMI_CCAL = 0xe0000000, - ARMI_S = 0x000100000, - ARMI_K12 = 0x02000000, - ARMI_KNEG = 0x00200000, - ARMI_LS_W = 0x00200000, - ARMI_LS_U = 0x00800000, - ARMI_LS_P = 0x01000000, - ARMI_LS_R = 0x02000000, - ARMI_LSX_I = 0x00400000, - - ARMI_AND = 0xe0000000, - ARMI_EOR = 0xe0200000, - ARMI_SUB = 0xe0400000, - ARMI_RSB = 0xe0600000, - ARMI_ADD = 0xe0800000, - ARMI_ADC = 0xe0a00000, - ARMI_SBC = 0xe0c00000, - ARMI_RSC = 0xe0e00000, - ARMI_TST = 0xe1100000, - ARMI_TEQ = 0xe1300000, - ARMI_CMP = 0xe1500000, - ARMI_CMN = 0xe1700000, - ARMI_ORR = 0xe1800000, - ARMI_MOV = 0xe1a00000, - ARMI_BIC = 0xe1c00000, - ARMI_MVN = 0xe1e00000, - - ARMI_NOP = 0xe1a00000, - - ARMI_MUL = 0xe0000090, - ARMI_SMULL = 0xe0c00090, - - ARMI_LDR = 0xe4100000, - ARMI_LDRB = 0xe4500000, - ARMI_LDRH = 0xe01000b0, - ARMI_LDRSB = 0xe01000d0, - ARMI_LDRSH = 0xe01000f0, - ARMI_LDRD = 0xe00000d0, - ARMI_STR = 0xe4000000, - ARMI_STRB = 0xe4400000, - ARMI_STRH = 0xe00000b0, - ARMI_STRD = 0xe00000f0, - ARMI_PUSH = 0xe92d0000, - - ARMI_B = 0xea000000, - ARMI_BL = 0xeb000000, - ARMI_BLX = 0xfa000000, - ARMI_BLXr = 0xe12fff30, - - /* ARMv6 */ - ARMI_REV = 0xe6bf0f30, - ARMI_SXTB = 0xe6af0070, - ARMI_SXTH = 0xe6bf0070, - ARMI_UXTB = 0xe6ef0070, - ARMI_UXTH = 0xe6ff0070, - - /* ARMv6T2 */ - ARMI_MOVW = 0xe3000000, - ARMI_MOVT = 0xe3400000, - - /* VFP */ - ARMI_VMOV_D = 0xeeb00b40, - ARMI_VMOV_S = 0xeeb00a40, - ARMI_VMOVI_D = 0xeeb00b00, - - ARMI_VMOV_R_S = 0xee100a10, - ARMI_VMOV_S_R = 0xee000a10, - ARMI_VMOV_RR_D = 0xec500b10, - ARMI_VMOV_D_RR = 0xec400b10, - - ARMI_VADD_D = 0xee300b00, - ARMI_VSUB_D = 0xee300b40, - ARMI_VMUL_D = 0xee200b00, - ARMI_VMLA_D = 0xee000b00, - ARMI_VMLS_D = 0xee000b40, - ARMI_VNMLS_D = 0xee100b00, - ARMI_VDIV_D = 0xee800b00, - - ARMI_VABS_D = 0xeeb00bc0, - ARMI_VNEG_D = 0xeeb10b40, - ARMI_VSQRT_D = 0xeeb10bc0, - - ARMI_VCMP_D = 0xeeb40b40, - ARMI_VCMPZ_D = 0xeeb50b40, - - ARMI_VMRS = 0xeef1fa10, - - ARMI_VCVT_S32_F32 = 0xeebd0ac0, - ARMI_VCVT_S32_F64 = 0xeebd0bc0, - ARMI_VCVT_U32_F32 = 0xeebc0ac0, - ARMI_VCVT_U32_F64 = 0xeebc0bc0, - ARMI_VCVTR_S32_F32 = 0xeebd0a40, - ARMI_VCVTR_S32_F64 = 0xeebd0b40, - ARMI_VCVTR_U32_F32 = 0xeebc0a40, - ARMI_VCVTR_U32_F64 = 0xeebc0b40, - ARMI_VCVT_F32_S32 = 0xeeb80ac0, - ARMI_VCVT_F64_S32 = 0xeeb80bc0, - ARMI_VCVT_F32_U32 = 0xeeb80a40, - ARMI_VCVT_F64_U32 = 0xeeb80b40, - ARMI_VCVT_F32_F64 = 0xeeb70bc0, - ARMI_VCVT_F64_F32 = 0xeeb70ac0, - - ARMI_VLDR_S = 0xed100a00, - ARMI_VLDR_D = 0xed100b00, - ARMI_VSTR_S = 0xed000a00, - ARMI_VSTR_D = 0xed000b00, -} ARMIns; - -typedef enum ARMShift { - ARMSH_LSL, ARMSH_LSR, ARMSH_ASR, ARMSH_ROR -} ARMShift; - -/* ARM condition codes. */ -typedef enum ARMCC { - CC_EQ, CC_NE, CC_CS, CC_CC, CC_MI, CC_PL, CC_VS, CC_VC, - CC_HI, CC_LS, CC_GE, CC_LT, CC_GT, CC_LE, CC_AL, - CC_HS = CC_CS, CC_LO = CC_CC -} ARMCC; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_target_mips.h b/third-party/LuaJIT-2.0.2/src/lj_target_mips.h deleted file mode 100644 index 36f46c8301..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_target_mips.h +++ /dev/null @@ -1,257 +0,0 @@ -/* -** Definitions for MIPS CPUs. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_MIPS_H -#define _LJ_TARGET_MIPS_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#define GPRDEF(_) \ - _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \ - _(R8) _(R9) _(R10) _(R11) _(R12) _(R13) _(R14) _(R15) \ - _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \ - _(R24) _(R25) _(SYS1) _(SYS2) _(R28) _(SP) _(R30) _(RA) -#define FPRDEF(_) \ - _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \ - _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \ - _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \ - _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31) -#define VRIDDEF(_) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_ZERO = RID_R0, - RID_TMP = RID_RA, - - /* Calling conventions. */ - RID_RET = RID_R2, -#if LJ_LE - RID_RETHI = RID_R3, - RID_RETLO = RID_R2, -#else - RID_RETHI = RID_R2, - RID_RETLO = RID_R3, -#endif - RID_FPRET = RID_F0, - RID_CFUNCADDR = RID_R25, - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_R16, /* Interpreter BASE. */ - RID_LPC = RID_R18, /* Interpreter PC. */ - RID_DISPATCH = RID_R19, /* Interpreter DISPATCH table. */ - RID_LREG = RID_R20, /* Interpreter L. */ - RID_JGL = RID_R30, /* On-trace: global_State + 32768. */ - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_R0, - RID_MAX_GPR = RID_RA+1, - RID_MIN_FPR = RID_F0, - RID_MAX_FPR = RID_F31+1, - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR /* Only even regs are used. */ -}; - -#define RID_NUM_KREF RID_NUM_GPR -#define RID_MIN_KREF RID_R0 - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except ZERO, TMP, SP, SYS1, SYS2 and JGL. */ -#define RSET_FIXED \ - (RID2RSET(RID_ZERO)|RID2RSET(RID_TMP)|RID2RSET(RID_SP)|\ - RID2RSET(RID_SYS1)|RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)) -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) -#define RSET_FPR \ - (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\ - RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\ - RID2RSET(RID_F16)|RID2RSET(RID_F18)|RID2RSET(RID_F20)|RID2RSET(RID_F22)|\ - RID2RSET(RID_F24)|RID2RSET(RID_F26)|RID2RSET(RID_F28)|RID2RSET(RID_F30)) -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -#define RSET_SCRATCH_GPR \ - (RSET_RANGE(RID_R1, RID_R15+1)|\ - RID2RSET(RID_R24)|RID2RSET(RID_R25)|RID2RSET(RID_R28)) -#define RSET_SCRATCH_FPR \ - (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\ - RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\ - RID2RSET(RID_F16)|RID2RSET(RID_F18)) -#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) -#define REGARG_FIRSTGPR RID_R4 -#define REGARG_LASTGPR RID_R7 -#define REGARG_NUMGPR 4 -#define REGARG_FIRSTFPR RID_F12 -#define REGARG_LASTFPR RID_F14 -#define REGARG_NUMFPR 2 - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. -*/ -#define SPS_FIXED 5 -#define SPS_FIRST 4 - -#define SPOFS_TMP 0 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ - int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* Highest exit + 1 indicates stack check. */ -#define EXITSTATE_CHECKEXIT 1 - -/* Return the address of a per-trace exit stub. */ -static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p) -{ - while (*p == 0x00000000) p++; /* Skip MIPSI_NOP. */ - return p; -} -/* Avoid dependence on lj_jit.h if only including lj_target.h. */ -#define exitstub_trace_addr(T, exitno) \ - exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode)) - -/* -- Instructions -------------------------------------------------------- */ - -/* Instruction fields. */ -#define MIPSF_S(r) ((r) << 21) -#define MIPSF_T(r) ((r) << 16) -#define MIPSF_D(r) ((r) << 11) -#define MIPSF_R(r) ((r) << 21) -#define MIPSF_H(r) ((r) << 16) -#define MIPSF_G(r) ((r) << 11) -#define MIPSF_F(r) ((r) << 6) -#define MIPSF_A(n) ((n) << 6) -#define MIPSF_M(n) ((n) << 11) - -typedef enum MIPSIns { - /* Integer instructions. */ - MIPSI_MOVE = 0x00000021, - MIPSI_NOP = 0x00000000, - - MIPSI_LI = 0x24000000, - MIPSI_LU = 0x34000000, - MIPSI_LUI = 0x3c000000, - - MIPSI_ADDIU = 0x24000000, - MIPSI_ANDI = 0x30000000, - MIPSI_ORI = 0x34000000, - MIPSI_XORI = 0x38000000, - MIPSI_SLTI = 0x28000000, - MIPSI_SLTIU = 0x2c000000, - - MIPSI_ADDU = 0x00000021, - MIPSI_SUBU = 0x00000023, - MIPSI_MUL = 0x70000002, - MIPSI_AND = 0x00000024, - MIPSI_OR = 0x00000025, - MIPSI_XOR = 0x00000026, - MIPSI_NOR = 0x00000027, - MIPSI_SLT = 0x0000002a, - MIPSI_SLTU = 0x0000002b, - MIPSI_MOVZ = 0x0000000a, - MIPSI_MOVN = 0x0000000b, - - MIPSI_SLL = 0x00000000, - MIPSI_SRL = 0x00000002, - MIPSI_SRA = 0x00000003, - MIPSI_ROTR = 0x00200002, /* MIPS32R2 */ - MIPSI_SLLV = 0x00000004, - MIPSI_SRLV = 0x00000006, - MIPSI_SRAV = 0x00000007, - MIPSI_ROTRV = 0x00000046, /* MIPS32R2 */ - - MIPSI_SEB = 0x7c000420, /* MIPS32R2 */ - MIPSI_SEH = 0x7c000620, /* MIPS32R2 */ - MIPSI_WSBH = 0x7c0000a0, /* MIPS32R2 */ - - MIPSI_B = 0x10000000, - MIPSI_J = 0x08000000, - MIPSI_JAL = 0x0c000000, - MIPSI_JR = 0x00000008, - MIPSI_JALR = 0x0000f809, - - MIPSI_BEQ = 0x10000000, - MIPSI_BNE = 0x14000000, - MIPSI_BLEZ = 0x18000000, - MIPSI_BGTZ = 0x1c000000, - MIPSI_BLTZ = 0x04000000, - MIPSI_BGEZ = 0x04010000, - - /* Load/store instructions. */ - MIPSI_LW = 0x8c000000, - MIPSI_SW = 0xac000000, - MIPSI_LB = 0x80000000, - MIPSI_SB = 0xa0000000, - MIPSI_LH = 0x84000000, - MIPSI_SH = 0xa4000000, - MIPSI_LBU = 0x90000000, - MIPSI_LHU = 0x94000000, - MIPSI_LWC1 = 0xc4000000, - MIPSI_SWC1 = 0xe4000000, - MIPSI_LDC1 = 0xd4000000, - MIPSI_SDC1 = 0xf4000000, - - /* FP instructions. */ - MIPSI_MOV_S = 0x46000006, - MIPSI_MOV_D = 0x46200006, - MIPSI_MOVT_D = 0x46210011, - MIPSI_MOVF_D = 0x46200011, - - MIPSI_ABS_D = 0x46200005, - MIPSI_NEG_D = 0x46200007, - - MIPSI_ADD_D = 0x46200000, - MIPSI_SUB_D = 0x46200001, - MIPSI_MUL_D = 0x46200002, - MIPSI_DIV_D = 0x46200003, - MIPSI_SQRT_D = 0x46200004, - - MIPSI_ADD_S = 0x46000000, - MIPSI_SUB_S = 0x46000001, - - MIPSI_CVT_D_S = 0x46000021, - MIPSI_CVT_W_S = 0x46000024, - MIPSI_CVT_S_D = 0x46200020, - MIPSI_CVT_W_D = 0x46200024, - MIPSI_CVT_S_W = 0x46800020, - MIPSI_CVT_D_W = 0x46800021, - - MIPSI_TRUNC_W_S = 0x4600000d, - MIPSI_TRUNC_W_D = 0x4620000d, - MIPSI_FLOOR_W_S = 0x4600000f, - MIPSI_FLOOR_W_D = 0x4620000f, - - MIPSI_MFC1 = 0x44000000, - MIPSI_MTC1 = 0x44800000, - - MIPSI_BC1F = 0x45000000, - MIPSI_BC1T = 0x45010000, - - MIPSI_C_EQ_D = 0x46200032, - MIPSI_C_OLT_D = 0x46200034, - MIPSI_C_ULT_D = 0x46200035, - MIPSI_C_OLE_D = 0x46200036, - MIPSI_C_ULE_D = 0x46200037, - -} MIPSIns; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_target_ppc.h b/third-party/LuaJIT-2.0.2/src/lj_target_ppc.h deleted file mode 100644 index 4e95c3a414..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_target_ppc.h +++ /dev/null @@ -1,280 +0,0 @@ -/* -** Definitions for PPC CPUs. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_PPC_H -#define _LJ_TARGET_PPC_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#define GPRDEF(_) \ - _(R0) _(SP) _(SYS1) _(R3) _(R4) _(R5) _(R6) _(R7) \ - _(R8) _(R9) _(R10) _(R11) _(R12) _(SYS2) _(R14) _(R15) \ - _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \ - _(R24) _(R25) _(R26) _(R27) _(R28) _(R29) _(R30) _(R31) -#define FPRDEF(_) \ - _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \ - _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \ - _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \ - _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31) -#define VRIDDEF(_) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_TMP = RID_R0, - - /* Calling conventions. */ - RID_RET = RID_R3, - RID_RETHI = RID_R3, - RID_RETLO = RID_R4, - RID_FPRET = RID_F1, - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_R14, /* Interpreter BASE. */ - RID_LPC = RID_R16, /* Interpreter PC. */ - RID_DISPATCH = RID_R17, /* Interpreter DISPATCH table. */ - RID_LREG = RID_R18, /* Interpreter L. */ - RID_JGL = RID_R31, /* On-trace: global_State + 32768. */ - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_R0, - RID_MAX_GPR = RID_R31+1, - RID_MIN_FPR = RID_F0, - RID_MAX_FPR = RID_F31+1, - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR -}; - -#define RID_NUM_KREF RID_NUM_GPR -#define RID_MIN_KREF RID_R0 - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except TMP, SP, SYS1, SYS2 and JGL. */ -#define RSET_FIXED \ - (RID2RSET(RID_TMP)|RID2RSET(RID_SP)|RID2RSET(RID_SYS1)|\ - RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)) -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) -#define RSET_FPR RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR) -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -#define RSET_SCRATCH_GPR (RSET_RANGE(RID_R3, RID_R12+1)) -#define RSET_SCRATCH_FPR (RSET_RANGE(RID_F0, RID_F13+1)) -#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) -#define REGARG_FIRSTGPR RID_R3 -#define REGARG_LASTGPR RID_R10 -#define REGARG_NUMGPR 8 -#define REGARG_FIRSTFPR RID_F1 -#define REGARG_LASTFPR RID_F8 -#define REGARG_NUMFPR 8 - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. -** [sp+12] tmplo word \ -** [sp+ 8] tmphi word / tmp dword, parameter area for callee -** [sp+ 4] tmpw, LR of callee -** [sp+ 0] stack chain -*/ -#define SPS_FIXED 7 -#define SPS_FIRST 4 - -/* Stack offsets for temporary slots. Used for FP<->int conversions etc. */ -#define SPOFS_TMPW 4 -#define SPOFS_TMP 8 -#define SPOFS_TMPHI 8 -#define SPOFS_TMPLO 12 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ - int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* Highest exit + 1 indicates stack check. */ -#define EXITSTATE_CHECKEXIT 1 - -/* Return the address of a per-trace exit stub. */ -static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p, uint32_t exitno) -{ - while (*p == 0x60000000) p++; /* Skip PPCI_NOP. */ - return p + 3 + exitno; -} -/* Avoid dependence on lj_jit.h if only including lj_target.h. */ -#define exitstub_trace_addr(T, exitno) \ - exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode), (exitno)) - -/* -- Instructions -------------------------------------------------------- */ - -/* Instruction fields. */ -#define PPCF_CC(cc) ((((cc) & 3) << 16) | (((cc) & 4) << 22)) -#define PPCF_T(r) ((r) << 21) -#define PPCF_A(r) ((r) << 16) -#define PPCF_B(r) ((r) << 11) -#define PPCF_C(r) ((r) << 6) -#define PPCF_MB(n) ((n) << 6) -#define PPCF_ME(n) ((n) << 1) -#define PPCF_Y 0x00200000 -#define PPCF_DOT 0x00000001 - -typedef enum PPCIns { - /* Integer instructions. */ - PPCI_MR = 0x7c000378, - PPCI_NOP = 0x60000000, - - PPCI_LI = 0x38000000, - PPCI_LIS = 0x3c000000, - - PPCI_ADD = 0x7c000214, - PPCI_ADDC = 0x7c000014, - PPCI_ADDO = 0x7c000614, - PPCI_ADDE = 0x7c000114, - PPCI_ADDZE = 0x7c000194, - PPCI_ADDME = 0x7c0001d4, - PPCI_ADDI = 0x38000000, - PPCI_ADDIS = 0x3c000000, - PPCI_ADDIC = 0x30000000, - PPCI_ADDICDOT = 0x34000000, - - PPCI_SUBF = 0x7c000050, - PPCI_SUBFC = 0x7c000010, - PPCI_SUBFO = 0x7c000450, - PPCI_SUBFE = 0x7c000110, - PPCI_SUBFZE = 0x7c000190, - PPCI_SUBFME = 0x7c0001d0, - PPCI_SUBFIC = 0x20000000, - - PPCI_NEG = 0x7c0000d0, - - PPCI_AND = 0x7c000038, - PPCI_ANDC = 0x7c000078, - PPCI_NAND = 0x7c0003b8, - PPCI_ANDIDOT = 0x70000000, - PPCI_ANDISDOT = 0x74000000, - - PPCI_OR = 0x7c000378, - PPCI_NOR = 0x7c0000f8, - PPCI_ORI = 0x60000000, - PPCI_ORIS = 0x64000000, - - PPCI_XOR = 0x7c000278, - PPCI_EQV = 0x7c000238, - PPCI_XORI = 0x68000000, - PPCI_XORIS = 0x6c000000, - - PPCI_CMPW = 0x7c000000, - PPCI_CMPLW = 0x7c000040, - PPCI_CMPWI = 0x2c000000, - PPCI_CMPLWI = 0x28000000, - - PPCI_MULLW = 0x7c0001d6, - PPCI_MULLI = 0x1c000000, - PPCI_MULLWO = 0x7c0005d6, - - PPCI_EXTSB = 0x7c000774, - PPCI_EXTSH = 0x7c000734, - - PPCI_SLW = 0x7c000030, - PPCI_SRW = 0x7c000430, - PPCI_SRAW = 0x7c000630, - PPCI_SRAWI = 0x7c000670, - - PPCI_RLWNM = 0x5c000000, - PPCI_RLWINM = 0x54000000, - PPCI_RLWIMI = 0x50000000, - - PPCI_B = 0x48000000, - PPCI_BL = 0x48000001, - PPCI_BC = 0x40800000, - PPCI_BCL = 0x40800001, - PPCI_BCTR = 0x4e800420, - PPCI_BCTRL = 0x4e800421, - - PPCI_CRANDC = 0x4c000102, - PPCI_CRXOR = 0x4c000182, - PPCI_CRAND = 0x4c000202, - PPCI_CREQV = 0x4c000242, - PPCI_CRORC = 0x4c000342, - PPCI_CROR = 0x4c000382, - - PPCI_MFLR = 0x7c0802a6, - PPCI_MTCTR = 0x7c0903a6, - - PPCI_MCRXR = 0x7c000400, - - /* Load/store instructions. */ - PPCI_LWZ = 0x80000000, - PPCI_LBZ = 0x88000000, - PPCI_STW = 0x90000000, - PPCI_STB = 0x98000000, - PPCI_LHZ = 0xa0000000, - PPCI_LHA = 0xa8000000, - PPCI_STH = 0xb0000000, - - PPCI_STWU = 0x94000000, - - PPCI_LFS = 0xc0000000, - PPCI_LFD = 0xc8000000, - PPCI_STFS = 0xd0000000, - PPCI_STFD = 0xd8000000, - - PPCI_LWZX = 0x7c00002e, - PPCI_LBZX = 0x7c0000ae, - PPCI_STWX = 0x7c00012e, - PPCI_STBX = 0x7c0001ae, - PPCI_LHZX = 0x7c00022e, - PPCI_LHAX = 0x7c0002ae, - PPCI_STHX = 0x7c00032e, - - PPCI_LWBRX = 0x7c00042c, - PPCI_STWBRX = 0x7c00052c, - - PPCI_LFSX = 0x7c00042e, - PPCI_LFDX = 0x7c0004ae, - PPCI_STFSX = 0x7c00052e, - PPCI_STFDX = 0x7c0005ae, - - /* FP instructions. */ - PPCI_FMR = 0xfc000090, - PPCI_FNEG = 0xfc000050, - PPCI_FABS = 0xfc000210, - - PPCI_FRSP = 0xfc000018, - PPCI_FCTIWZ = 0xfc00001e, - - PPCI_FADD = 0xfc00002a, - PPCI_FSUB = 0xfc000028, - PPCI_FMUL = 0xfc000032, - PPCI_FDIV = 0xfc000024, - PPCI_FSQRT = 0xfc00002c, - - PPCI_FMADD = 0xfc00003a, - PPCI_FMSUB = 0xfc000038, - PPCI_FNMSUB = 0xfc00003c, - - PPCI_FCMPU = 0xfc000000, - PPCI_FSEL = 0xfc00002e, -} PPCIns; - -typedef enum PPCCC { - CC_GE, CC_LE, CC_NE, CC_NS, CC_LT, CC_GT, CC_EQ, CC_SO -} PPCCC; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_target_x86.h b/third-party/LuaJIT-2.0.2/src/lj_target_x86.h deleted file mode 100644 index 84b0871da0..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_target_x86.h +++ /dev/null @@ -1,342 +0,0 @@ -/* -** Definitions for x86 and x64 CPUs. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_X86_H -#define _LJ_TARGET_X86_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#if LJ_64 -#define GPRDEF(_) \ - _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI) \ - _(R8D) _(R9D) _(R10D) _(R11D) _(R12D) _(R13D) _(R14D) _(R15D) -#define FPRDEF(_) \ - _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7) \ - _(XMM8) _(XMM9) _(XMM10) _(XMM11) _(XMM12) _(XMM13) _(XMM14) _(XMM15) -#else -#define GPRDEF(_) \ - _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI) -#define FPRDEF(_) \ - _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7) -#endif -#define VRIDDEF(_) \ - _(MRM) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_MRM = RID_MAX, /* Pseudo-id for ModRM operand. */ - - /* Calling conventions. */ - RID_RET = RID_EAX, -#if LJ_64 - RID_FPRET = RID_XMM0, -#else - RID_RETLO = RID_EAX, - RID_RETHI = RID_EDX, -#endif - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_EDX, /* Interpreter BASE. */ -#if LJ_64 && !LJ_ABI_WIN - RID_LPC = RID_EBX, /* Interpreter PC. */ - RID_DISPATCH = RID_R14D, /* Interpreter DISPATCH table. */ -#else - RID_LPC = RID_ESI, /* Interpreter PC. */ - RID_DISPATCH = RID_EBX, /* Interpreter DISPATCH table. */ -#endif - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_EAX, - RID_MIN_FPR = RID_XMM0, - RID_MAX_GPR = RID_MIN_FPR, - RID_MAX_FPR = RID_MAX, - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR, -}; - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except the stack pointer. */ -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR)-RID2RSET(RID_ESP)) -#define RSET_FPR (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)) -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -#if LJ_64 -/* Note: this requires the use of FORCE_REX! */ -#define RSET_GPR8 RSET_GPR -#else -#define RSET_GPR8 (RSET_RANGE(RID_EAX, RID_EBX+1)) -#endif - -/* ABI-specific register sets. */ -#define RSET_ACD (RID2RSET(RID_EAX)|RID2RSET(RID_ECX)|RID2RSET(RID_EDX)) -#if LJ_64 -#if LJ_ABI_WIN -/* Windows x64 ABI. */ -#define RSET_SCRATCH \ - (RSET_ACD|RSET_RANGE(RID_R8D, RID_R11D+1)|RSET_RANGE(RID_XMM0, RID_XMM5+1)) -#define REGARG_GPRS \ - (RID_ECX|((RID_EDX|((RID_R8D|(RID_R9D<<5))<<5))<<5)) -#define REGARG_NUMGPR 4 -#define REGARG_NUMFPR 4 -#define REGARG_FIRSTFPR RID_XMM0 -#define REGARG_LASTFPR RID_XMM3 -#define STACKARG_OFS (4*8) -#else -/* The rest of the civilized x64 world has a common ABI. */ -#define RSET_SCRATCH \ - (RSET_ACD|RSET_RANGE(RID_ESI, RID_R11D+1)|RSET_FPR) -#define REGARG_GPRS \ - (RID_EDI|((RID_ESI|((RID_EDX|((RID_ECX|((RID_R8D|(RID_R9D \ - <<5))<<5))<<5))<<5))<<5)) -#define REGARG_NUMGPR 6 -#define REGARG_NUMFPR 8 -#define REGARG_FIRSTFPR RID_XMM0 -#define REGARG_LASTFPR RID_XMM7 -#define STACKARG_OFS 0 -#endif -#else -/* Common x86 ABI. */ -#define RSET_SCRATCH (RSET_ACD|RSET_FPR) -#define REGARG_GPRS (RID_ECX|(RID_EDX<<5)) /* Fastcall only. */ -#define REGARG_NUMGPR 2 /* Fastcall only. */ -#define REGARG_NUMFPR 0 -#define STACKARG_OFS 0 -#endif - -#if LJ_64 -/* Prefer the low 8 regs of each type to reduce REX prefixes. */ -#undef rset_picktop -#define rset_picktop(rs) (lj_fls(lj_bswap(rs)) ^ 0x18) -#endif - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. -*/ -#if LJ_64 -#if LJ_ABI_WIN -#define SPS_FIXED (4*2) -#define SPS_FIRST (4*2) /* Don't use callee register save area. */ -#else -#define SPS_FIXED 4 -#define SPS_FIRST 2 -#endif -#else -#define SPS_FIXED 6 -#define SPS_FIRST 2 -#endif - -#define SPOFS_TMP 0 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ - intptr_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* Limited by the range of a short fwd jump (127): (2+2)*(32-1)-2 = 122. */ -#define EXITSTUB_SPACING (2+2) -#define EXITSTUBS_PER_GROUP 32 - -/* -- x86 ModRM operand encoding ------------------------------------------ */ - -typedef enum { - XM_OFS0 = 0x00, XM_OFS8 = 0x40, XM_OFS32 = 0x80, XM_REG = 0xc0, - XM_SCALE1 = 0x00, XM_SCALE2 = 0x40, XM_SCALE4 = 0x80, XM_SCALE8 = 0xc0, - XM_MASK = 0xc0 -} x86Mode; - -/* Structure to hold variable ModRM operand. */ -typedef struct { - int32_t ofs; /* Offset. */ - uint8_t base; /* Base register or RID_NONE. */ - uint8_t idx; /* Index register or RID_NONE. */ - uint8_t scale; /* Index scale (XM_SCALE1 .. XM_SCALE8). */ -} x86ModRM; - -/* -- Opcodes ------------------------------------------------------------- */ - -/* Macros to construct variable-length x86 opcodes. -(len+1) is in LSB. */ -#define XO_(o) ((uint32_t)(0x0000fe + (0x##o<<24))) -#define XO_FPU(a,b) ((uint32_t)(0x00fd + (0x##a<<16)+(0x##b<<24))) -#define XO_0f(o) ((uint32_t)(0x0f00fd + (0x##o<<24))) -#define XO_66(o) ((uint32_t)(0x6600fd + (0x##o<<24))) -#define XO_660f(o) ((uint32_t)(0x0f66fc + (0x##o<<24))) -#define XO_f20f(o) ((uint32_t)(0x0ff2fc + (0x##o<<24))) -#define XO_f30f(o) ((uint32_t)(0x0ff3fc + (0x##o<<24))) - -/* This list of x86 opcodes is not intended to be complete. Opcodes are only -** included when needed. Take a look at DynASM or jit.dis_x86 to see the -** whole mess. -*/ -typedef enum { - /* Fixed length opcodes. XI_* prefix. */ - XI_NOP = 0x90, - XI_XCHGa = 0x90, - XI_CALL = 0xe8, - XI_JMP = 0xe9, - XI_JMPs = 0xeb, - XI_PUSH = 0x50, /* Really 50+r. */ - XI_JCCs = 0x70, /* Really 7x. */ - XI_JCCn = 0x80, /* Really 0f8x. */ - XI_LEA = 0x8d, - XI_MOVrib = 0xb0, /* Really b0+r. */ - XI_MOVri = 0xb8, /* Really b8+r. */ - XI_ARITHib = 0x80, - XI_ARITHi = 0x81, - XI_ARITHi8 = 0x83, - XI_PUSHi8 = 0x6a, - XI_TESTb = 0x84, - XI_TEST = 0x85, - XI_MOVmi = 0xc7, - XI_GROUP5 = 0xff, - - /* Note: little-endian byte-order! */ - XI_FLDZ = 0xeed9, - XI_FLD1 = 0xe8d9, - XI_FLDLG2 = 0xecd9, - XI_FLDLN2 = 0xedd9, - XI_FDUP = 0xc0d9, /* Really fld st0. */ - XI_FPOP = 0xd8dd, /* Really fstp st0. */ - XI_FPOP1 = 0xd9dd, /* Really fstp st1. */ - XI_FRNDINT = 0xfcd9, - XI_FSIN = 0xfed9, - XI_FCOS = 0xffd9, - XI_FPTAN = 0xf2d9, - XI_FPATAN = 0xf3d9, - XI_FSCALE = 0xfdd9, - XI_FYL2X = 0xf1d9, - - /* Variable-length opcodes. XO_* prefix. */ - XO_MOV = XO_(8b), - XO_MOVto = XO_(89), - XO_MOVtow = XO_66(89), - XO_MOVtob = XO_(88), - XO_MOVmi = XO_(c7), - XO_MOVmib = XO_(c6), - XO_LEA = XO_(8d), - XO_ARITHib = XO_(80), - XO_ARITHi = XO_(81), - XO_ARITHi8 = XO_(83), - XO_ARITHiw8 = XO_66(83), - XO_SHIFTi = XO_(c1), - XO_SHIFT1 = XO_(d1), - XO_SHIFTcl = XO_(d3), - XO_IMUL = XO_0f(af), - XO_IMULi = XO_(69), - XO_IMULi8 = XO_(6b), - XO_CMP = XO_(3b), - XO_TESTb = XO_(84), - XO_TEST = XO_(85), - XO_GROUP3b = XO_(f6), - XO_GROUP3 = XO_(f7), - XO_GROUP5b = XO_(fe), - XO_GROUP5 = XO_(ff), - XO_MOVZXb = XO_0f(b6), - XO_MOVZXw = XO_0f(b7), - XO_MOVSXb = XO_0f(be), - XO_MOVSXw = XO_0f(bf), - XO_MOVSXd = XO_(63), - XO_BSWAP = XO_0f(c8), - XO_CMOV = XO_0f(40), - - XO_MOVSD = XO_f20f(10), - XO_MOVSDto = XO_f20f(11), - XO_MOVSS = XO_f30f(10), - XO_MOVSSto = XO_f30f(11), - XO_MOVLPD = XO_660f(12), - XO_MOVAPS = XO_0f(28), - XO_XORPS = XO_0f(57), - XO_ANDPS = XO_0f(54), - XO_ADDSD = XO_f20f(58), - XO_SUBSD = XO_f20f(5c), - XO_MULSD = XO_f20f(59), - XO_DIVSD = XO_f20f(5e), - XO_SQRTSD = XO_f20f(51), - XO_MINSD = XO_f20f(5d), - XO_MAXSD = XO_f20f(5f), - XO_ROUNDSD = 0x0b3a0ffc, /* Really 66 0f 3a 0b. See asm_fpmath. */ - XO_UCOMISD = XO_660f(2e), - XO_CVTSI2SD = XO_f20f(2a), - XO_CVTSD2SI = XO_f20f(2d), - XO_CVTTSD2SI= XO_f20f(2c), - XO_CVTSI2SS = XO_f30f(2a), - XO_CVTSS2SI = XO_f30f(2d), - XO_CVTTSS2SI= XO_f30f(2c), - XO_CVTSS2SD = XO_f30f(5a), - XO_CVTSD2SS = XO_f20f(5a), - XO_ADDSS = XO_f30f(58), - XO_MOVD = XO_660f(6e), - XO_MOVDto = XO_660f(7e), - - XO_FLDd = XO_(d9), XOg_FLDd = 0, - XO_FLDq = XO_(dd), XOg_FLDq = 0, - XO_FILDd = XO_(db), XOg_FILDd = 0, - XO_FILDq = XO_(df), XOg_FILDq = 5, - XO_FSTPd = XO_(d9), XOg_FSTPd = 3, - XO_FSTPq = XO_(dd), XOg_FSTPq = 3, - XO_FISTPq = XO_(df), XOg_FISTPq = 7, - XO_FISTTPq = XO_(dd), XOg_FISTTPq = 1, - XO_FADDq = XO_(dc), XOg_FADDq = 0, - XO_FLDCW = XO_(d9), XOg_FLDCW = 5, - XO_FNSTCW = XO_(d9), XOg_FNSTCW = 7 -} x86Op; - -/* x86 opcode groups. */ -typedef uint32_t x86Group; - -#define XG_(i8, i, g) ((x86Group)(((i8) << 16) + ((i) << 8) + (g))) -#define XG_ARITHi(g) XG_(XI_ARITHi8, XI_ARITHi, g) -#define XG_TOXOi(xg) ((x86Op)(0x000000fe + (((xg)<<16) & 0xff000000))) -#define XG_TOXOi8(xg) ((x86Op)(0x000000fe + (((xg)<<8) & 0xff000000))) - -#define XO_ARITH(a) ((x86Op)(0x030000fe + ((a)<<27))) -#define XO_ARITHw(a) ((x86Op)(0x036600fd + ((a)<<27))) - -typedef enum { - XOg_ADD, XOg_OR, XOg_ADC, XOg_SBB, XOg_AND, XOg_SUB, XOg_XOR, XOg_CMP, - XOg_X_IMUL -} x86Arith; - -typedef enum { - XOg_ROL, XOg_ROR, XOg_RCL, XOg_RCR, XOg_SHL, XOg_SHR, XOg_SAL, XOg_SAR -} x86Shift; - -typedef enum { - XOg_TEST, XOg_TEST_, XOg_NOT, XOg_NEG, XOg_MUL, XOg_IMUL, XOg_DIV, XOg_IDIV -} x86Group3; - -typedef enum { - XOg_INC, XOg_DEC, XOg_CALL, XOg_CALLfar, XOg_JMP, XOg_JMPfar, XOg_PUSH -} x86Group5; - -/* x86 condition codes. */ -typedef enum { - CC_O, CC_NO, CC_B, CC_NB, CC_E, CC_NE, CC_BE, CC_NBE, - CC_S, CC_NS, CC_P, CC_NP, CC_L, CC_NL, CC_LE, CC_NLE, - CC_C = CC_B, CC_NAE = CC_C, CC_NC = CC_NB, CC_AE = CC_NB, - CC_Z = CC_E, CC_NZ = CC_NE, CC_NA = CC_BE, CC_A = CC_NBE, - CC_PE = CC_P, CC_PO = CC_NP, CC_NGE = CC_L, CC_GE = CC_NL, - CC_NG = CC_LE, CC_G = CC_NLE -} x86CC; - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_trace.c b/third-party/LuaJIT-2.0.2/src/lj_trace.c deleted file mode 100644 index c70fc247ec..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_trace.c +++ /dev/null @@ -1,815 +0,0 @@ -/* -** Trace management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_trace_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_frame.h" -#include "lj_state.h" -#include "lj_bc.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_mcode.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_gdbjit.h" -#include "lj_record.h" -#include "lj_asm.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_vmevent.h" -#include "lj_target.h" - -/* -- Error handling ------------------------------------------------------ */ - -/* Synchronous abort with error message. */ -void lj_trace_err(jit_State *J, TraceError e) -{ - setnilV(&J->errinfo); /* No error info. */ - setintV(J->L->top++, (int32_t)e); - lj_err_throw(J->L, LUA_ERRRUN); -} - -/* Synchronous abort with error message and error info. */ -void lj_trace_err_info(jit_State *J, TraceError e) -{ - setintV(J->L->top++, (int32_t)e); - lj_err_throw(J->L, LUA_ERRRUN); -} - -/* -- Trace management ---------------------------------------------------- */ - -/* The current trace is first assembled in J->cur. The variable length -** arrays point to shared, growable buffers (J->irbuf etc.). When trace -** recording ends successfully, the current trace and its data structures -** are copied to a new (compact) GCtrace object. -*/ - -/* Find a free trace number. */ -static TraceNo trace_findfree(jit_State *J) -{ - MSize osz, lim; - if (J->freetrace == 0) - J->freetrace = 1; - for (; J->freetrace < J->sizetrace; J->freetrace++) - if (traceref(J, J->freetrace) == NULL) - return J->freetrace++; - /* Need to grow trace array. */ - lim = (MSize)J->param[JIT_P_maxtrace] + 1; - if (lim < 2) lim = 2; else if (lim > 65535) lim = 65535; - osz = J->sizetrace; - if (osz >= lim) - return 0; /* Too many traces. */ - lj_mem_growvec(J->L, J->trace, J->sizetrace, lim, GCRef); - for (; osz < J->sizetrace; osz++) - setgcrefnull(J->trace[osz]); - return J->freetrace; -} - -#define TRACE_APPENDVEC(field, szfield, tp) \ - T->field = (tp *)p; \ - memcpy(p, J->cur.field, J->cur.szfield*sizeof(tp)); \ - p += J->cur.szfield*sizeof(tp); - -#ifdef LUAJIT_USE_PERFTOOLS -/* -** Create symbol table of JIT-compiled code. For use with Linux perf tools. -** Example usage: -** perf record -f -e cycles luajit test.lua -** perf report -s symbol -** rm perf.data /tmp/perf-*.map -*/ -#include -#include - -static void perftools_addtrace(GCtrace *T) -{ - static FILE *fp; - GCproto *pt = &gcref(T->startpt)->pt; - const BCIns *startpc = mref(T->startpc, const BCIns); - const char *name = proto_chunknamestr(pt); - BCLine lineno; - if (name[0] == '@' || name[0] == '=') - name++; - else - name = "(string)"; - lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc); - lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); - if (!fp) { - char fname[40]; - sprintf(fname, "/tmp/perf-%d.map", getpid()); - if (!(fp = fopen(fname, "w"))) return; - setlinebuf(fp); - } - fprintf(fp, "%lx %x TRACE_%d::%s:%u\n", - (long)T->mcode, T->szmcode, T->traceno, name, lineno); -} -#endif - -/* Save current trace by copying and compacting it. */ -static void trace_save(jit_State *J) -{ - size_t sztr = ((sizeof(GCtrace)+7)&~7); - size_t szins = (J->cur.nins-J->cur.nk)*sizeof(IRIns); - size_t sz = sztr + szins + - J->cur.nsnap*sizeof(SnapShot) + - J->cur.nsnapmap*sizeof(SnapEntry); - GCtrace *T = lj_mem_newt(J->L, (MSize)sz, GCtrace); - char *p = (char *)T + sztr; - memcpy(T, &J->cur, sizeof(GCtrace)); - setgcrefr(T->nextgc, J2G(J)->gc.root); - setgcrefp(J2G(J)->gc.root, T); - newwhite(J2G(J), T); - T->gct = ~LJ_TTRACE; - T->ir = (IRIns *)p - J->cur.nk; - memcpy(p, J->cur.ir+J->cur.nk, szins); - p += szins; - TRACE_APPENDVEC(snap, nsnap, SnapShot) - TRACE_APPENDVEC(snapmap, nsnapmap, SnapEntry) - J->cur.traceno = 0; - setgcrefp(J->trace[T->traceno], T); - lj_gc_barriertrace(J2G(J), T->traceno); - lj_gdbjit_addtrace(J, T); -#ifdef LUAJIT_USE_PERFTOOLS - perftools_addtrace(T); -#endif -} - -void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T) -{ - jit_State *J = G2J(g); - if (T->traceno) { - lj_gdbjit_deltrace(J, T); - if (T->traceno < J->freetrace) - J->freetrace = T->traceno; - setgcrefnull(J->trace[T->traceno]); - } - lj_mem_free(g, T, - ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + - T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry)); -} - -/* Re-enable compiling a prototype by unpatching any modified bytecode. */ -void lj_trace_reenableproto(GCproto *pt) -{ - if ((pt->flags & PROTO_ILOOP)) { - BCIns *bc = proto_bc(pt); - BCPos i, sizebc = pt->sizebc;; - pt->flags &= ~PROTO_ILOOP; - if (bc_op(bc[0]) == BC_IFUNCF) - setbc_op(&bc[0], BC_FUNCF); - for (i = 1; i < sizebc; i++) { - BCOp op = bc_op(bc[i]); - if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP) - setbc_op(&bc[i], (int)op+(int)BC_LOOP-(int)BC_ILOOP); - } - } -} - -/* Unpatch the bytecode modified by a root trace. */ -static void trace_unpatch(jit_State *J, GCtrace *T) -{ - BCOp op = bc_op(T->startins); - BCIns *pc = mref(T->startpc, BCIns); - UNUSED(J); - if (op == BC_JMP) - return; /* No need to unpatch branches in parent traces (yet). */ - switch (bc_op(*pc)) { - case BC_JFORL: - lua_assert(traceref(J, bc_d(*pc)) == T); - *pc = T->startins; - pc += bc_j(T->startins); - lua_assert(bc_op(*pc) == BC_JFORI); - setbc_op(pc, BC_FORI); - break; - case BC_JITERL: - case BC_JLOOP: - lua_assert(op == BC_ITERL || op == BC_LOOP || bc_isret(op)); - *pc = T->startins; - break; - case BC_JMP: - lua_assert(op == BC_ITERL); - pc += bc_j(*pc)+2; - if (bc_op(*pc) == BC_JITERL) { - lua_assert(traceref(J, bc_d(*pc)) == T); - *pc = T->startins; - } - break; - case BC_JFUNCF: - lua_assert(op == BC_FUNCF); - *pc = T->startins; - break; - default: /* Already unpatched. */ - break; - } -} - -/* Flush a root trace. */ -static void trace_flushroot(jit_State *J, GCtrace *T) -{ - GCproto *pt = &gcref(T->startpt)->pt; - lua_assert(T->root == 0 && pt != NULL); - /* First unpatch any modified bytecode. */ - trace_unpatch(J, T); - /* Unlink root trace from chain anchored in prototype. */ - if (pt->trace == T->traceno) { /* Trace is first in chain. Easy. */ - pt->trace = T->nextroot; - } else if (pt->trace) { /* Otherwise search in chain of root traces. */ - GCtrace *T2 = traceref(J, pt->trace); - if (T2) { - for (; T2->nextroot; T2 = traceref(J, T2->nextroot)) - if (T2->nextroot == T->traceno) { - T2->nextroot = T->nextroot; /* Unlink from chain. */ - break; - } - } - } -} - -/* Flush a trace. Only root traces are considered. */ -void lj_trace_flush(jit_State *J, TraceNo traceno) -{ - if (traceno > 0 && traceno < J->sizetrace) { - GCtrace *T = traceref(J, traceno); - if (T && T->root == 0) - trace_flushroot(J, T); - } -} - -/* Flush all traces associated with a prototype. */ -void lj_trace_flushproto(global_State *g, GCproto *pt) -{ - while (pt->trace != 0) - trace_flushroot(G2J(g), traceref(G2J(g), pt->trace)); -} - -/* Flush all traces. */ -int lj_trace_flushall(lua_State *L) -{ - jit_State *J = L2J(L); - ptrdiff_t i; - if ((J2G(J)->hookmask & HOOK_GC)) - return 1; - for (i = (ptrdiff_t)J->sizetrace-1; i > 0; i--) { - GCtrace *T = traceref(J, i); - if (T) { - if (T->root == 0) - trace_flushroot(J, T); - lj_gdbjit_deltrace(J, T); - T->traceno = 0; - setgcrefnull(J->trace[i]); - } - } - J->cur.traceno = 0; - J->freetrace = 0; - /* Clear penalty cache. */ - memset(J->penalty, 0, sizeof(J->penalty)); - /* Free the whole machine code and invalidate all exit stub groups. */ - lj_mcode_free(J); - memset(J->exitstubgroup, 0, sizeof(J->exitstubgroup)); - lj_vmevent_send(L, TRACE, - setstrV(L, L->top++, lj_str_newlit(L, "flush")); - ); - return 0; -} - -/* Initialize JIT compiler state. */ -void lj_trace_initstate(global_State *g) -{ - jit_State *J = G2J(g); - TValue *tv; - /* Initialize SIMD constants. */ - tv = LJ_KSIMD(J, LJ_KSIMD_ABS); - tv[0].u64 = U64x(7fffffff,ffffffff); - tv[1].u64 = U64x(7fffffff,ffffffff); - tv = LJ_KSIMD(J, LJ_KSIMD_NEG); - tv[0].u64 = U64x(80000000,00000000); - tv[1].u64 = U64x(80000000,00000000); -} - -/* Free everything associated with the JIT compiler state. */ -void lj_trace_freestate(global_State *g) -{ - jit_State *J = G2J(g); -#ifdef LUA_USE_ASSERT - { /* This assumes all traces have already been freed. */ - ptrdiff_t i; - for (i = 1; i < (ptrdiff_t)J->sizetrace; i++) - lua_assert(i == (ptrdiff_t)J->cur.traceno || traceref(J, i) == NULL); - } -#endif - lj_mcode_free(J); - lj_ir_k64_freeall(J); - lj_mem_freevec(g, J->snapmapbuf, J->sizesnapmap, SnapEntry); - lj_mem_freevec(g, J->snapbuf, J->sizesnap, SnapShot); - lj_mem_freevec(g, J->irbuf + J->irbotlim, J->irtoplim - J->irbotlim, IRIns); - lj_mem_freevec(g, J->trace, J->sizetrace, GCRef); -} - -/* -- Penalties and blacklisting ------------------------------------------ */ - -/* Blacklist a bytecode instruction. */ -static void blacklist_pc(GCproto *pt, BCIns *pc) -{ - setbc_op(pc, (int)bc_op(*pc)+(int)BC_ILOOP-(int)BC_LOOP); - pt->flags |= PROTO_ILOOP; -} - -/* Penalize a bytecode instruction. */ -static void penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e) -{ - uint32_t i, val = PENALTY_MIN; - for (i = 0; i < PENALTY_SLOTS; i++) - if (mref(J->penalty[i].pc, const BCIns) == pc) { /* Cache slot found? */ - /* First try to bump its hotcount several times. */ - val = ((uint32_t)J->penalty[i].val << 1) + - LJ_PRNG_BITS(J, PENALTY_RNDBITS); - if (val > PENALTY_MAX) { - blacklist_pc(pt, pc); /* Blacklist it, if that didn't help. */ - return; - } - goto setpenalty; - } - /* Assign a new penalty cache slot. */ - i = J->penaltyslot; - J->penaltyslot = (J->penaltyslot + 1) & (PENALTY_SLOTS-1); - setmref(J->penalty[i].pc, pc); -setpenalty: - J->penalty[i].val = (uint16_t)val; - J->penalty[i].reason = e; - hotcount_set(J2GG(J), pc+1, val); -} - -/* -- Trace compiler state machine ---------------------------------------- */ - -/* Start tracing. */ -static void trace_start(jit_State *J) -{ - lua_State *L; - TraceNo traceno; - - if ((J->pt->flags & PROTO_NOJIT)) { /* JIT disabled for this proto? */ - if (J->parent == 0) { - /* Lazy bytecode patching to disable hotcount events. */ - lua_assert(bc_op(*J->pc) == BC_FORL || bc_op(*J->pc) == BC_ITERL || - bc_op(*J->pc) == BC_LOOP || bc_op(*J->pc) == BC_FUNCF); - setbc_op(J->pc, (int)bc_op(*J->pc)+(int)BC_ILOOP-(int)BC_LOOP); - J->pt->flags |= PROTO_ILOOP; - } - J->state = LJ_TRACE_IDLE; /* Silently ignored. */ - return; - } - - /* Get a new trace number. */ - traceno = trace_findfree(J); - if (LJ_UNLIKELY(traceno == 0)) { /* No free trace? */ - lua_assert((J2G(J)->hookmask & HOOK_GC) == 0); - lj_trace_flushall(J->L); - J->state = LJ_TRACE_IDLE; /* Silently ignored. */ - return; - } - setgcrefp(J->trace[traceno], &J->cur); - - /* Setup enough of the current trace to be able to send the vmevent. */ - memset(&J->cur, 0, sizeof(GCtrace)); - J->cur.traceno = traceno; - J->cur.nins = J->cur.nk = REF_BASE; - J->cur.ir = J->irbuf; - J->cur.snap = J->snapbuf; - J->cur.snapmap = J->snapmapbuf; - J->mergesnap = 0; - J->needsnap = 0; - J->bcskip = 0; - J->guardemit.irt = 0; - J->postproc = LJ_POST_NONE; - lj_resetsplit(J); - setgcref(J->cur.startpt, obj2gco(J->pt)); - - L = J->L; - lj_vmevent_send(L, TRACE, - setstrV(L, L->top++, lj_str_newlit(L, "start")); - setintV(L->top++, traceno); - setfuncV(L, L->top++, J->fn); - setintV(L->top++, proto_bcpos(J->pt, J->pc)); - if (J->parent) { - setintV(L->top++, J->parent); - setintV(L->top++, J->exitno); - } - ); - lj_record_setup(J); -} - -/* Stop tracing. */ -static void trace_stop(jit_State *J) -{ - BCIns *pc = mref(J->cur.startpc, BCIns); - BCOp op = bc_op(J->cur.startins); - GCproto *pt = &gcref(J->cur.startpt)->pt; - TraceNo traceno = J->cur.traceno; - lua_State *L; - - switch (op) { - case BC_FORL: - setbc_op(pc+bc_j(J->cur.startins), BC_JFORI); /* Patch FORI, too. */ - /* fallthrough */ - case BC_LOOP: - case BC_ITERL: - case BC_FUNCF: - /* Patch bytecode of starting instruction in root trace. */ - setbc_op(pc, (int)op+(int)BC_JLOOP-(int)BC_LOOP); - setbc_d(pc, traceno); - addroot: - /* Add to root trace chain in prototype. */ - J->cur.nextroot = pt->trace; - pt->trace = (TraceNo1)traceno; - break; - case BC_RET: - case BC_RET0: - case BC_RET1: - *pc = BCINS_AD(BC_JLOOP, J->cur.snap[0].nslots, traceno); - goto addroot; - case BC_JMP: - /* Patch exit branch in parent to side trace entry. */ - lua_assert(J->parent != 0 && J->cur.root != 0); - lj_asm_patchexit(J, traceref(J, J->parent), J->exitno, J->cur.mcode); - /* Avoid compiling a side trace twice (stack resizing uses parent exit). */ - traceref(J, J->parent)->snap[J->exitno].count = SNAPCOUNT_DONE; - /* Add to side trace chain in root trace. */ - { - GCtrace *root = traceref(J, J->cur.root); - root->nchild++; - J->cur.nextside = root->nextside; - root->nextside = (TraceNo1)traceno; - } - break; - default: - lua_assert(0); - break; - } - - /* Commit new mcode only after all patching is done. */ - lj_mcode_commit(J, J->cur.mcode); - J->postproc = LJ_POST_NONE; - trace_save(J); - - L = J->L; - lj_vmevent_send(L, TRACE, - setstrV(L, L->top++, lj_str_newlit(L, "stop")); - setintV(L->top++, traceno); - ); -} - -/* Start a new root trace for down-recursion. */ -static int trace_downrec(jit_State *J) -{ - /* Restart recording at the return instruction. */ - lua_assert(J->pt != NULL); - lua_assert(bc_isret(bc_op(*J->pc))); - if (bc_op(*J->pc) == BC_RETM) - return 0; /* NYI: down-recursion with RETM. */ - J->parent = 0; - J->exitno = 0; - J->state = LJ_TRACE_RECORD; - trace_start(J); - return 1; -} - -/* Abort tracing. */ -static int trace_abort(jit_State *J) -{ - lua_State *L = J->L; - TraceError e = LJ_TRERR_RECERR; - TraceNo traceno; - - J->postproc = LJ_POST_NONE; - lj_mcode_abort(J); - if (tvisnumber(L->top-1)) - e = (TraceError)numberVint(L->top-1); - if (e == LJ_TRERR_MCODELM) { - L->top--; /* Remove error object */ - J->state = LJ_TRACE_ASM; - return 1; /* Retry ASM with new MCode area. */ - } - /* Penalize or blacklist starting bytecode instruction. */ - if (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))) - penalty_pc(J, &gcref(J->cur.startpt)->pt, mref(J->cur.startpc, BCIns), e); - - /* Is there anything to abort? */ - traceno = J->cur.traceno; - if (traceno) { - ptrdiff_t errobj = savestack(L, L->top-1); /* Stack may be resized. */ - J->cur.link = 0; - J->cur.linktype = LJ_TRLINK_NONE; - lj_vmevent_send(L, TRACE, - TValue *frame; - const BCIns *pc; - GCfunc *fn; - setstrV(L, L->top++, lj_str_newlit(L, "abort")); - setintV(L->top++, traceno); - /* Find original Lua function call to generate a better error message. */ - frame = J->L->base-1; - pc = J->pc; - while (!isluafunc(frame_func(frame))) { - pc = (frame_iscont(frame) ? frame_contpc(frame) : frame_pc(frame)) - 1; - frame = frame_prev(frame); - } - fn = frame_func(frame); - setfuncV(L, L->top++, fn); - setintV(L->top++, proto_bcpos(funcproto(fn), pc)); - copyTV(L, L->top++, restorestack(L, errobj)); - copyTV(L, L->top++, &J->errinfo); - ); - /* Drop aborted trace after the vmevent (which may still access it). */ - setgcrefnull(J->trace[traceno]); - if (traceno < J->freetrace) - J->freetrace = traceno; - J->cur.traceno = 0; - } - L->top--; /* Remove error object */ - if (e == LJ_TRERR_DOWNREC) - return trace_downrec(J); - else if (e == LJ_TRERR_MCODEAL) - lj_trace_flushall(L); - return 0; -} - -/* Perform pending re-patch of a bytecode instruction. */ -static LJ_AINLINE void trace_pendpatch(jit_State *J, int force) -{ - if (LJ_UNLIKELY(J->patchpc)) { - if (force || J->bcskip == 0) { - *J->patchpc = J->patchins; - J->patchpc = NULL; - } else { - J->bcskip = 0; - } - } -} - -/* State machine for the trace compiler. Protected callback. */ -static TValue *trace_state(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - UNUSED(dummy); - do { - retry: - switch (J->state) { - case LJ_TRACE_START: - J->state = LJ_TRACE_RECORD; /* trace_start() may change state. */ - trace_start(J); - lj_dispatch_update(J2G(J)); - break; - - case LJ_TRACE_RECORD: - trace_pendpatch(J, 0); - setvmstate(J2G(J), RECORD); - lj_vmevent_send_(L, RECORD, - /* Save/restore tmptv state for trace recorder. */ - TValue savetv = J2G(J)->tmptv; - TValue savetv2 = J2G(J)->tmptv2; - setintV(L->top++, J->cur.traceno); - setfuncV(L, L->top++, J->fn); - setintV(L->top++, J->pt ? (int32_t)proto_bcpos(J->pt, J->pc) : -1); - setintV(L->top++, J->framedepth); - , - J2G(J)->tmptv = savetv; - J2G(J)->tmptv2 = savetv2; - ); - lj_record_ins(J); - break; - - case LJ_TRACE_END: - trace_pendpatch(J, 1); - J->loopref = 0; - if ((J->flags & JIT_F_OPT_LOOP) && - J->cur.link == J->cur.traceno && J->framedepth + J->retdepth == 0) { - setvmstate(J2G(J), OPT); - lj_opt_dce(J); - if (lj_opt_loop(J)) { /* Loop optimization failed? */ - J->cur.link = 0; - J->cur.linktype = LJ_TRLINK_NONE; - J->loopref = J->cur.nins; - J->state = LJ_TRACE_RECORD; /* Try to continue recording. */ - break; - } - J->loopref = J->chain[IR_LOOP]; /* Needed by assembler. */ - } - lj_opt_split(J); - lj_opt_sink(J); - J->state = LJ_TRACE_ASM; - break; - - case LJ_TRACE_ASM: - setvmstate(J2G(J), ASM); - lj_asm_trace(J, &J->cur); - trace_stop(J); - setvmstate(J2G(J), INTERP); - J->state = LJ_TRACE_IDLE; - lj_dispatch_update(J2G(J)); - return NULL; - - default: /* Trace aborted asynchronously. */ - setintV(L->top++, (int32_t)LJ_TRERR_RECERR); - /* fallthrough */ - case LJ_TRACE_ERR: - trace_pendpatch(J, 1); - if (trace_abort(J)) - goto retry; - setvmstate(J2G(J), INTERP); - J->state = LJ_TRACE_IDLE; - lj_dispatch_update(J2G(J)); - return NULL; - } - } while (J->state > LJ_TRACE_RECORD); - return NULL; -} - -/* -- Event handling ------------------------------------------------------ */ - -/* A bytecode instruction is about to be executed. Record it. */ -void lj_trace_ins(jit_State *J, const BCIns *pc) -{ - /* Note: J->L must already be set. pc is the true bytecode PC here. */ - J->pc = pc; - J->fn = curr_func(J->L); - J->pt = isluafunc(J->fn) ? funcproto(J->fn) : NULL; - while (lj_vm_cpcall(J->L, NULL, (void *)J, trace_state) != 0) - J->state = LJ_TRACE_ERR; -} - -/* A hotcount triggered. Start recording a root trace. */ -void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc) -{ - /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */ - ERRNO_SAVE - /* Reset hotcount. */ - hotcount_set(J2GG(J), pc, J->param[JIT_P_hotloop]*HOTCOUNT_LOOP); - /* Only start a new trace if not recording or inside __gc call or vmevent. */ - if (J->state == LJ_TRACE_IDLE && - !(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT))) { - J->parent = 0; /* Root trace. */ - J->exitno = 0; - J->state = LJ_TRACE_START; - lj_trace_ins(J, pc-1); - } - ERRNO_RESTORE -} - -/* Check for a hot side exit. If yes, start recording a side trace. */ -static void trace_hotside(jit_State *J, const BCIns *pc) -{ - SnapShot *snap = &traceref(J, J->parent)->snap[J->exitno]; - if (!(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT)) && - snap->count != SNAPCOUNT_DONE && - ++snap->count >= J->param[JIT_P_hotexit]) { - lua_assert(J->state == LJ_TRACE_IDLE); - /* J->parent is non-zero for a side trace. */ - J->state = LJ_TRACE_START; - lj_trace_ins(J, pc); - } -} - -/* Tiny struct to pass data to protected call. */ -typedef struct ExitDataCP { - jit_State *J; - void *exptr; /* Pointer to exit state. */ - const BCIns *pc; /* Restart interpreter at this PC. */ -} ExitDataCP; - -/* Need to protect lj_snap_restore because it may throw. */ -static TValue *trace_exit_cp(lua_State *L, lua_CFunction dummy, void *ud) -{ - ExitDataCP *exd = (ExitDataCP *)ud; - cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ - exd->pc = lj_snap_restore(exd->J, exd->exptr); - UNUSED(dummy); - return NULL; -} - -#ifndef LUAJIT_DISABLE_VMEVENT -/* Push all registers from exit state. */ -static void trace_exit_regs(lua_State *L, ExitState *ex) -{ - int32_t i; - setintV(L->top++, RID_NUM_GPR); - setintV(L->top++, RID_NUM_FPR); - for (i = 0; i < RID_NUM_GPR; i++) { - if (sizeof(ex->gpr[i]) == sizeof(int32_t)) - setintV(L->top++, (int32_t)ex->gpr[i]); - else - setnumV(L->top++, (lua_Number)ex->gpr[i]); - } -#if !LJ_SOFTFP - for (i = 0; i < RID_NUM_FPR; i++) { - setnumV(L->top, ex->fpr[i]); - if (LJ_UNLIKELY(tvisnan(L->top))) - setnanV(L->top); - L->top++; - } -#endif -} -#endif - -#ifdef EXITSTATE_PCREG -/* Determine trace number from pc of exit instruction. */ -static TraceNo trace_exit_find(jit_State *J, MCode *pc) -{ - TraceNo traceno; - for (traceno = 1; traceno < J->sizetrace; traceno++) { - GCtrace *T = traceref(J, traceno); - if (T && pc >= T->mcode && pc < (MCode *)((char *)T->mcode + T->szmcode)) - return traceno; - } - lua_assert(0); - return 0; -} -#endif - -/* A trace exited. Restore interpreter state. */ -int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr) -{ - ERRNO_SAVE - lua_State *L = J->L; - ExitState *ex = (ExitState *)exptr; - ExitDataCP exd; - int errcode; - const BCIns *pc; - void *cf; - GCtrace *T; -#ifdef EXITSTATE_PCREG - J->parent = trace_exit_find(J, (MCode *)(intptr_t)ex->gpr[EXITSTATE_PCREG]); -#endif - T = traceref(J, J->parent); UNUSED(T); -#ifdef EXITSTATE_CHECKEXIT - if (J->exitno == T->nsnap) { /* Treat stack check like a parent exit. */ - lua_assert(T->root != 0); - J->exitno = T->ir[REF_BASE].op2; - J->parent = T->ir[REF_BASE].op1; - T = traceref(J, J->parent); - } -#endif - lua_assert(T != NULL && J->exitno < T->nsnap); - exd.J = J; - exd.exptr = exptr; - errcode = lj_vm_cpcall(L, NULL, &exd, trace_exit_cp); - if (errcode) - return -errcode; /* Return negated error code. */ - - lj_vmevent_send(L, TEXIT, - lj_state_checkstack(L, 4+RID_NUM_GPR+RID_NUM_FPR+LUA_MINSTACK); - setintV(L->top++, J->parent); - setintV(L->top++, J->exitno); - trace_exit_regs(L, ex); - ); - - pc = exd.pc; - cf = cframe_raw(L->cframe); - setcframe_pc(cf, pc); - if (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize) { - if (!(G(L)->hookmask & HOOK_GC)) - lj_gc_step(L); /* Exited because of GC: drive GC forward. */ - } else { - trace_hotside(J, pc); - } - if (bc_op(*pc) == BC_JLOOP) { - BCIns *retpc = &traceref(J, bc_d(*pc))->startins; - if (bc_isret(bc_op(*retpc))) { - if (J->state == LJ_TRACE_RECORD) { - J->patchins = *pc; - J->patchpc = (BCIns *)pc; - *J->patchpc = *retpc; - J->bcskip = 1; - } else { - pc = retpc; - setcframe_pc(cf, pc); - } - } - } - /* Return MULTRES or 0. */ - ERRNO_RESTORE - switch (bc_op(*pc)) { - case BC_CALLM: case BC_CALLMT: - return (int)((BCReg)(L->top - L->base) - bc_a(*pc) - bc_c(*pc)); - case BC_RETM: - return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc) - bc_d(*pc)); - case BC_TSETM: - return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc)); - default: - if (bc_op(*pc) >= BC_FUNCF) - return (int)((BCReg)(L->top - L->base) + 1); - return 0; - } -} - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_trace.h b/third-party/LuaJIT-2.0.2/src/lj_trace.h deleted file mode 100644 index e30d3d599e..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_trace.h +++ /dev/null @@ -1,53 +0,0 @@ -/* -** Trace management. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TRACE_H -#define _LJ_TRACE_H - -#include "lj_obj.h" - -#if LJ_HASJIT -#include "lj_jit.h" -#include "lj_dispatch.h" - -/* Trace errors. */ -typedef enum { -#define TREDEF(name, msg) LJ_TRERR_##name, -#include "lj_traceerr.h" - LJ_TRERR__MAX -} TraceError; - -LJ_FUNC_NORET void lj_trace_err(jit_State *J, TraceError e); -LJ_FUNC_NORET void lj_trace_err_info(jit_State *J, TraceError e); - -/* Trace management. */ -LJ_FUNC void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T); -LJ_FUNC void lj_trace_reenableproto(GCproto *pt); -LJ_FUNC void lj_trace_flushproto(global_State *g, GCproto *pt); -LJ_FUNC void lj_trace_flush(jit_State *J, TraceNo traceno); -LJ_FUNC int lj_trace_flushall(lua_State *L); -LJ_FUNC void lj_trace_initstate(global_State *g); -LJ_FUNC void lj_trace_freestate(global_State *g); - -/* Event handling. */ -LJ_FUNC void lj_trace_ins(jit_State *J, const BCIns *pc); -LJ_FUNCA void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc); -LJ_FUNCA int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr); - -/* Signal asynchronous abort of trace or end of trace. */ -#define lj_trace_abort(g) (G2J(g)->state &= ~LJ_TRACE_ACTIVE) -#define lj_trace_end(J) (J->state = LJ_TRACE_END) - -#else - -#define lj_trace_flushall(L) (UNUSED(L), 0) -#define lj_trace_initstate(g) UNUSED(g) -#define lj_trace_freestate(g) UNUSED(g) -#define lj_trace_abort(g) UNUSED(g) -#define lj_trace_end(J) UNUSED(J) - -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_traceerr.h b/third-party/LuaJIT-2.0.2/src/lj_traceerr.h deleted file mode 100644 index 2ef4ad63e7..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_traceerr.h +++ /dev/null @@ -1,61 +0,0 @@ -/* -** Trace compiler error messages. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* This file may be included multiple times with different TREDEF macros. */ - -/* Recording. */ -TREDEF(RECERR, "error thrown or hook called during recording") -TREDEF(TRACEOV, "trace too long") -TREDEF(STACKOV, "trace too deep") -TREDEF(SNAPOV, "too many snapshots") -TREDEF(BLACKL, "blacklisted") -TREDEF(NYIBC, "NYI: bytecode %d") - -/* Recording loop ops. */ -TREDEF(LLEAVE, "leaving loop in root trace") -TREDEF(LINNER, "inner loop in root trace") -TREDEF(LUNROLL, "loop unroll limit reached") - -/* Recording calls/returns. */ -TREDEF(BADTYPE, "bad argument type") -TREDEF(CJITOFF, "call to JIT-disabled function") -TREDEF(CUNROLL, "call unroll limit reached") -TREDEF(DOWNREC, "down-recursion, restarting") -TREDEF(NYICF, "NYI: C function %p") -TREDEF(NYIFF, "NYI: FastFunc %s") -TREDEF(NYIFFU, "NYI: unsupported variant of FastFunc %s") -TREDEF(NYIRETL, "NYI: return to lower frame") - -/* Recording indexed load/store. */ -TREDEF(STORENN, "store with nil or NaN key") -TREDEF(NOMM, "missing metamethod") -TREDEF(IDXLOOP, "looping index lookup") -TREDEF(NYITMIX, "NYI: mixed sparse/dense table") - -/* Recording C data operations. */ -TREDEF(NOCACHE, "symbol not in cache") -TREDEF(NYICONV, "NYI: unsupported C type conversion") -TREDEF(NYICALL, "NYI: unsupported C function type") - -/* Optimizations. */ -TREDEF(GFAIL, "guard would always fail") -TREDEF(PHIOV, "too many PHIs") -TREDEF(TYPEINS, "persistent type instability") - -/* Assembler. */ -TREDEF(MCODEAL, "failed to allocate mcode memory") -TREDEF(MCODEOV, "machine code too long") -TREDEF(MCODELM, "hit mcode limit (retrying)") -TREDEF(SPILLOV, "too many spill slots") -TREDEF(BADRA, "inconsistent register allocation") -TREDEF(NYIIR, "NYI: cannot assemble IR instruction %d") -TREDEF(NYIPHI, "NYI: PHI shuffling too complex") -TREDEF(NYICOAL, "NYI: register coalescing too complex") - -#undef TREDEF - -/* Detecting unused error messages: - awk -F, '/^TREDEF/ { gsub(/TREDEF./, ""); printf "grep -q LJ_TRERR_%s *.[ch] || echo %s\n", $1, $1}' lj_traceerr.h | sh -*/ diff --git a/third-party/LuaJIT-2.0.2/src/lj_udata.c b/third-party/LuaJIT-2.0.2/src/lj_udata.c deleted file mode 100644 index df5e7f30bf..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_udata.c +++ /dev/null @@ -1,34 +0,0 @@ -/* -** Userdata handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_udata_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_udata.h" - -GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env) -{ - GCudata *ud = lj_mem_newt(L, sizeof(GCudata) + sz, GCudata); - global_State *g = G(L); - newwhite(g, ud); /* Not finalized. */ - ud->gct = ~LJ_TUDATA; - ud->udtype = UDTYPE_USERDATA; - ud->len = sz; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcrefnull(ud->metatable); - setgcref(ud->env, obj2gco(env)); - /* Chain to userdata list (after main thread). */ - setgcrefr(ud->nextgc, mainthread(g)->nextgc); - setgcref(mainthread(g)->nextgc, obj2gco(ud)); - return ud; -} - -void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud) -{ - lj_mem_free(g, ud, sizeudata(ud)); -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_udata.h b/third-party/LuaJIT-2.0.2/src/lj_udata.h deleted file mode 100644 index f62c02bcee..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_udata.h +++ /dev/null @@ -1,14 +0,0 @@ -/* -** Userdata handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_UDATA_H -#define _LJ_UDATA_H - -#include "lj_obj.h" - -LJ_FUNC GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env); -LJ_FUNC void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_vm.h b/third-party/LuaJIT-2.0.2/src/lj_vm.h deleted file mode 100644 index c5d05de44d..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_vm.h +++ /dev/null @@ -1,116 +0,0 @@ -/* -** Assembler VM interface definitions. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_VM_H -#define _LJ_VM_H - -#include "lj_obj.h" - -/* Entry points for ASM parts of VM. */ -LJ_ASMF void lj_vm_call(lua_State *L, TValue *base, int nres1); -LJ_ASMF int lj_vm_pcall(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); -typedef TValue *(*lua_CPFunction)(lua_State *L, lua_CFunction func, void *ud); -LJ_ASMF int lj_vm_cpcall(lua_State *L, lua_CFunction func, void *ud, - lua_CPFunction cp); -LJ_ASMF int lj_vm_resume(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); -LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_c(void *cframe, int errcode); -LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_ff(void *cframe); -LJ_ASMF void lj_vm_unwind_c_eh(void); -LJ_ASMF void lj_vm_unwind_ff_eh(void); -#if LJ_TARGET_X86ORX64 -LJ_ASMF void lj_vm_unwind_rethrow(void); -#endif - -/* Miscellaneous functions. */ -#if LJ_TARGET_X86ORX64 -LJ_ASMF int lj_vm_cpuid(uint32_t f, uint32_t res[4]); -#endif -#if LJ_TARGET_PPC -void lj_vm_cachesync(void *start, void *end); -#endif -LJ_ASMF double lj_vm_foldarith(double x, double y, int op); -#if LJ_HASJIT -LJ_ASMF double lj_vm_foldfpm(double x, int op); -#endif -#if !LJ_ARCH_HASFPU -/* Declared in lj_obj.h: LJ_ASMF int32_t lj_vm_tobit(double x); */ -#endif - -/* Dispatch targets for recording and hooks. */ -LJ_ASMF void lj_vm_record(void); -LJ_ASMF void lj_vm_inshook(void); -LJ_ASMF void lj_vm_rethook(void); -LJ_ASMF void lj_vm_callhook(void); - -/* Trace exit handling. */ -LJ_ASMF void lj_vm_exit_handler(void); -LJ_ASMF void lj_vm_exit_interp(void); - -/* Internal math helper functions. */ -#if LJ_TARGET_X86ORX64 || LJ_TARGET_PPC -#define lj_vm_floor floor -#define lj_vm_ceil ceil -#else -LJ_ASMF double lj_vm_floor(double); -LJ_ASMF double lj_vm_ceil(double); -#if LJ_TARGET_ARM -LJ_ASMF double lj_vm_floor_sf(double); -LJ_ASMF double lj_vm_ceil_sf(double); -#endif -#endif -#if defined(LUAJIT_NO_LOG2) || LJ_TARGET_X86ORX64 -LJ_ASMF double lj_vm_log2(double); -#else -#define lj_vm_log2 log2 -#endif - -#if LJ_HASJIT -#if LJ_TARGET_X86ORX64 -LJ_ASMF void lj_vm_floor_sse(void); -LJ_ASMF void lj_vm_ceil_sse(void); -LJ_ASMF void lj_vm_trunc_sse(void); -LJ_ASMF void lj_vm_exp_x87(void); -LJ_ASMF void lj_vm_exp2_x87(void); -LJ_ASMF void lj_vm_pow_sse(void); -LJ_ASMF void lj_vm_powi_sse(void); -#else -#if LJ_TARGET_PPC -#define lj_vm_trunc trunc -#else -LJ_ASMF double lj_vm_trunc(double); -#if LJ_TARGET_ARM -LJ_ASMF double lj_vm_trunc_sf(double); -#endif -#endif -LJ_ASMF double lj_vm_powi(double, int32_t); -#ifdef LUAJIT_NO_EXP2 -LJ_ASMF double lj_vm_exp2(double); -#else -#define lj_vm_exp2 exp2 -#endif -#endif -LJ_ASMF int32_t LJ_FASTCALL lj_vm_modi(int32_t, int32_t); -#if LJ_HASFFI -LJ_ASMF int lj_vm_errno(void); -#endif -#endif - -/* Continuations for metamethods. */ -LJ_ASMF void lj_cont_cat(void); /* Continue with concatenation. */ -LJ_ASMF void lj_cont_ra(void); /* Store result in RA from instruction. */ -LJ_ASMF void lj_cont_nop(void); /* Do nothing, just continue execution. */ -LJ_ASMF void lj_cont_condt(void); /* Branch if result is true. */ -LJ_ASMF void lj_cont_condf(void); /* Branch if result is false. */ -LJ_ASMF void lj_cont_hook(void); /* Continue from hook yield. */ - -enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */ - -/* Start of the ASM code. */ -LJ_ASMF char lj_vm_asm_begin[]; - -/* Bytecode offsets are relative to lj_vm_asm_begin. */ -#define makeasmfunc(ofs) ((ASMFunction)(lj_vm_asm_begin + (ofs))) - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_vmevent.c b/third-party/LuaJIT-2.0.2/src/lj_vmevent.c deleted file mode 100644 index 21ad08feff..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_vmevent.c +++ /dev/null @@ -1,57 +0,0 @@ -/* -** VM event handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include - -#define lj_vmevent_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_vmevent.h" - -ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev) -{ - global_State *g = G(L); - GCstr *s = lj_str_newlit(L, LJ_VMEVENTS_REGKEY); - cTValue *tv = lj_tab_getstr(tabV(registry(L)), s); - if (tvistab(tv)) { - int hash = VMEVENT_HASH(ev); - tv = lj_tab_getint(tabV(tv), hash); - if (tv && tvisfunc(tv)) { - lj_state_checkstack(L, LUA_MINSTACK); - setfuncV(L, L->top++, funcV(tv)); - return savestack(L, L->top); - } - } - g->vmevmask &= ~VMEVENT_MASK(ev); /* No handler: cache this fact. */ - return 0; -} - -void lj_vmevent_call(lua_State *L, ptrdiff_t argbase) -{ - global_State *g = G(L); - uint8_t oldmask = g->vmevmask; - uint8_t oldh = hook_save(g); - int status; - g->vmevmask = 0; /* Disable all events. */ - hook_vmevent(g); - status = lj_vm_pcall(L, restorestack(L, argbase), 0+1, 0); - if (LJ_UNLIKELY(status)) { - /* Really shouldn't use stderr here, but where else to complain? */ - L->top--; - fputs("VM handler failed: ", stderr); - fputs(tvisstr(L->top) ? strVdata(L->top) : "?", stderr); - fputc('\n', stderr); - } - hook_restore(g, oldh); - if (g->vmevmask != VMEVENT_NOCACHE) - g->vmevmask = oldmask; /* Restore event mask, but not if not modified. */ -} - diff --git a/third-party/LuaJIT-2.0.2/src/lj_vmevent.h b/third-party/LuaJIT-2.0.2/src/lj_vmevent.h deleted file mode 100644 index 11dedb42f3..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_vmevent.h +++ /dev/null @@ -1,59 +0,0 @@ -/* -** VM event handling. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_VMEVENT_H -#define _LJ_VMEVENT_H - -#include "lj_obj.h" - -/* Registry key for VM event handler table. */ -#define LJ_VMEVENTS_REGKEY "_VMEVENTS" -#define LJ_VMEVENTS_HSIZE 4 - -#define VMEVENT_MASK(ev) ((uint8_t)1 << ((int)(ev) & 7)) -#define VMEVENT_HASH(ev) ((int)(ev) & ~7) -#define VMEVENT_HASHIDX(h) ((int)(h) << 3) -#define VMEVENT_NOCACHE 255 - -#define VMEVENT_DEF(name, hash) \ - LJ_VMEVENT_##name##_, \ - LJ_VMEVENT_##name = ((LJ_VMEVENT_##name##_) & 7)|((hash) << 3) - -/* VM event IDs. */ -typedef enum { - VMEVENT_DEF(BC, 0x00003883), - VMEVENT_DEF(TRACE, 0xb2d91467), - VMEVENT_DEF(RECORD, 0x9284bf4f), - VMEVENT_DEF(TEXIT, 0xb29df2b0), - LJ_VMEVENT__MAX -} VMEvent; - -#ifdef LUAJIT_DISABLE_VMEVENT -#define lj_vmevent_send(L, ev, args) UNUSED(L) -#define lj_vmevent_send_(L, ev, args, post) UNUSED(L) -#else -#define lj_vmevent_send(L, ev, args) \ - if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ - ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ - if (argbase) { \ - args \ - lj_vmevent_call(L, argbase); \ - } \ - } -#define lj_vmevent_send_(L, ev, args, post) \ - if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ - ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ - if (argbase) { \ - args \ - lj_vmevent_call(L, argbase); \ - post \ - } \ - } - -LJ_FUNC ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev); -LJ_FUNC void lj_vmevent_call(lua_State *L, ptrdiff_t argbase); -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lj_vmmath.c b/third-party/LuaJIT-2.0.2/src/lj_vmmath.c deleted file mode 100644 index aa6903033c..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lj_vmmath.c +++ /dev/null @@ -1,140 +0,0 @@ -/* -** Math helper functions for assembler VM. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_vmmath_c -#define LUA_CORE - -#include -#include - -#include "lj_obj.h" -#include "lj_ir.h" -#include "lj_vm.h" - -/* -- Helper functions for generated machine code ------------------------- */ - -#if LJ_TARGET_X86ORX64 -/* Wrapper functions to avoid linker issues on OSX. */ -LJ_FUNCA double lj_vm_sinh(double x) { return sinh(x); } -LJ_FUNCA double lj_vm_cosh(double x) { return cosh(x); } -LJ_FUNCA double lj_vm_tanh(double x) { return tanh(x); } -#endif - -#if !LJ_TARGET_X86ORX64 -double lj_vm_foldarith(double x, double y, int op) -{ - switch (op) { - case IR_ADD - IR_ADD: return x+y; break; - case IR_SUB - IR_ADD: return x-y; break; - case IR_MUL - IR_ADD: return x*y; break; - case IR_DIV - IR_ADD: return x/y; break; - case IR_MOD - IR_ADD: return x-lj_vm_floor(x/y)*y; break; - case IR_POW - IR_ADD: return pow(x, y); break; - case IR_NEG - IR_ADD: return -x; break; - case IR_ABS - IR_ADD: return fabs(x); break; -#if LJ_HASJIT - case IR_ATAN2 - IR_ADD: return atan2(x, y); break; - case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break; - case IR_MIN - IR_ADD: return x > y ? y : x; break; - case IR_MAX - IR_ADD: return x < y ? y : x; break; -#endif - default: return x; - } -} -#endif - -#if LJ_HASJIT - -#ifdef LUAJIT_NO_LOG2 -double lj_vm_log2(double a) -{ - return log(a) * 1.4426950408889634074; -} -#endif - -#ifdef LUAJIT_NO_EXP2 -double lj_vm_exp2(double a) -{ - return exp(a * 0.6931471805599453); -} -#endif - -#if !(LJ_TARGET_ARM || LJ_TARGET_PPC) -int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b) -{ - uint32_t y, ua, ub; - lua_assert(b != 0); /* This must be checked before using this function. */ - ua = a < 0 ? (uint32_t)-a : (uint32_t)a; - ub = b < 0 ? (uint32_t)-b : (uint32_t)b; - y = ua % ub; - if (y != 0 && (a^b) < 0) y = y - ub; - if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y; - return (int32_t)y; -} -#endif - -#if !LJ_TARGET_X86ORX64 -/* Unsigned x^k. */ -static double lj_vm_powui(double x, uint32_t k) -{ - double y; - lua_assert(k != 0); - for (; (k & 1) == 0; k >>= 1) x *= x; - y = x; - if ((k >>= 1) != 0) { - for (;;) { - x *= x; - if (k == 1) break; - if (k & 1) y *= x; - k >>= 1; - } - y *= x; - } - return y; -} - -/* Signed x^k. */ -double lj_vm_powi(double x, int32_t k) -{ - if (k > 1) - return lj_vm_powui(x, (uint32_t)k); - else if (k == 1) - return x; - else if (k == 0) - return 1.0; - else - return 1.0 / lj_vm_powui(x, (uint32_t)-k); -} - -/* Computes fpm(x) for extended math functions. */ -double lj_vm_foldfpm(double x, int fpm) -{ - switch (fpm) { - case IRFPM_FLOOR: return lj_vm_floor(x); - case IRFPM_CEIL: return lj_vm_ceil(x); - case IRFPM_TRUNC: return lj_vm_trunc(x); - case IRFPM_SQRT: return sqrt(x); - case IRFPM_EXP: return exp(x); - case IRFPM_EXP2: return lj_vm_exp2(x); - case IRFPM_LOG: return log(x); - case IRFPM_LOG2: return lj_vm_log2(x); - case IRFPM_LOG10: return log10(x); - case IRFPM_SIN: return sin(x); - case IRFPM_COS: return cos(x); - case IRFPM_TAN: return tan(x); - default: lua_assert(0); - } - return 0; -} -#endif - -#if LJ_HASFFI -int lj_vm_errno(void) -{ - return errno; -} -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/ljamalg.c b/third-party/LuaJIT-2.0.2/src/ljamalg.c deleted file mode 100644 index 962b313442..0000000000 --- a/third-party/LuaJIT-2.0.2/src/ljamalg.c +++ /dev/null @@ -1,93 +0,0 @@ -/* -** LuaJIT core and libraries amalgamation. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -+--------------------------------------------------------------------------+ -| WARNING: Compiling the amalgamation needs a lot of virtual memory | -| (around 200 MB with GCC 4.x)! If you don't have enough physical memory | -| your machine will start swapping to disk and the compile will not finish | -| within a reasonable amount of time. | -| So either compile on a bigger machine or use the non-amalgamated build. | -+--------------------------------------------------------------------------+ -*/ - -#define ljamalg_c -#define LUA_CORE - -/* To get the mremap prototype. Must be defined before any system includes. */ -#if defined(__linux__) && !defined(_GNU_SOURCE) -#define _GNU_SOURCE -#endif - -#ifndef WINVER -#define WINVER 0x0500 -#endif - -#include "lua.h" -#include "lauxlib.h" - -#include "lj_gc.c" -#include "lj_err.c" -#include "lj_char.c" -#include "lj_bc.c" -#include "lj_obj.c" -#include "lj_str.c" -#include "lj_tab.c" -#include "lj_func.c" -#include "lj_udata.c" -#include "lj_meta.c" -#include "lj_debug.c" -#include "lj_state.c" -#include "lj_dispatch.c" -#include "lj_vmevent.c" -#include "lj_vmmath.c" -#include "lj_strscan.c" -#include "lj_api.c" -#include "lj_lex.c" -#include "lj_parse.c" -#include "lj_bcread.c" -#include "lj_bcwrite.c" -#include "lj_load.c" -#include "lj_ctype.c" -#include "lj_cdata.c" -#include "lj_cconv.c" -#include "lj_ccall.c" -#include "lj_ccallback.c" -#include "lj_carith.c" -#include "lj_clib.c" -#include "lj_cparse.c" -#include "lj_lib.c" -#include "lj_ir.c" -#include "lj_opt_mem.c" -#include "lj_opt_fold.c" -#include "lj_opt_narrow.c" -#include "lj_opt_dce.c" -#include "lj_opt_loop.c" -#include "lj_opt_split.c" -#include "lj_opt_sink.c" -#include "lj_mcode.c" -#include "lj_snap.c" -#include "lj_record.c" -#include "lj_crecord.c" -#include "lj_ffrecord.c" -#include "lj_asm.c" -#include "lj_trace.c" -#include "lj_gdbjit.c" -#include "lj_alloc.c" - -#include "lib_aux.c" -#include "lib_base.c" -#include "lib_math.c" -#include "lib_string.c" -#include "lib_table.c" -#include "lib_io.c" -#include "lib_os.c" -#include "lib_package.c" -#include "lib_debug.c" -#include "lib_bit.c" -#include "lib_jit.c" -#include "lib_ffi.c" -#include "lib_init.c" - diff --git a/third-party/LuaJIT-2.0.2/src/lua.h b/third-party/LuaJIT-2.0.2/src/lua.h deleted file mode 100644 index c83fd3bbe7..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lua.h +++ /dev/null @@ -1,393 +0,0 @@ -/* -** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ -** Lua - An Extensible Extension Language -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) -** See Copyright Notice at the end of this file -*/ - - -#ifndef lua_h -#define lua_h - -#include -#include - - -#include "luaconf.h" - - -#define LUA_VERSION "Lua 5.1" -#define LUA_RELEASE "Lua 5.1.4" -#define LUA_VERSION_NUM 501 -#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" - - -/* mark for precompiled code (`Lua') */ -#define LUA_SIGNATURE "\033Lua" - -/* option for multiple returns in `lua_pcall' and `lua_call' */ -#define LUA_MULTRET (-1) - - -/* -** pseudo-indices -*/ -#define LUA_REGISTRYINDEX (-10000) -#define LUA_ENVIRONINDEX (-10001) -#define LUA_GLOBALSINDEX (-10002) -#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) - - -/* thread status; 0 is OK */ -#define LUA_YIELD 1 -#define LUA_ERRRUN 2 -#define LUA_ERRSYNTAX 3 -#define LUA_ERRMEM 4 -#define LUA_ERRERR 5 - - -typedef struct lua_State lua_State; - -typedef int (*lua_CFunction) (lua_State *L); - - -/* -** functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); - - -/* -** prototype for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - - -/* -** basic types -*/ -#define LUA_TNONE (-1) - -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - - - -/* minimum Lua stack available to a C function */ -#define LUA_MINSTACK 20 - - -/* -** generic extra include file -*/ -#if defined(LUA_USER_H) -#include LUA_USER_H -#endif - - -/* type of numbers in Lua */ -typedef LUA_NUMBER lua_Number; - - -/* type for integer functions */ -typedef LUA_INTEGER lua_Integer; - - - -/* -** state manipulation -*/ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); -LUA_API void (lua_close) (lua_State *L); -LUA_API lua_State *(lua_newthread) (lua_State *L); - -LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); - - -/* -** basic stack manipulation -*/ -LUA_API int (lua_gettop) (lua_State *L); -LUA_API void (lua_settop) (lua_State *L, int idx); -LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_remove) (lua_State *L, int idx); -LUA_API void (lua_insert) (lua_State *L, int idx); -LUA_API void (lua_replace) (lua_State *L, int idx); -LUA_API int (lua_checkstack) (lua_State *L, int sz); - -LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); - - -/* -** access functions (stack -> C) -*/ - -LUA_API int (lua_isnumber) (lua_State *L, int idx); -LUA_API int (lua_isstring) (lua_State *L, int idx); -LUA_API int (lua_iscfunction) (lua_State *L, int idx); -LUA_API int (lua_isuserdata) (lua_State *L, int idx); -LUA_API int (lua_type) (lua_State *L, int idx); -LUA_API const char *(lua_typename) (lua_State *L, int tp); - -LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); - -LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); -LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); -LUA_API int (lua_toboolean) (lua_State *L, int idx); -LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); -LUA_API size_t (lua_objlen) (lua_State *L, int idx); -LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); -LUA_API void *(lua_touserdata) (lua_State *L, int idx); -LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); -LUA_API const void *(lua_topointer) (lua_State *L, int idx); - - -/* -** push functions (C -> stack) -*/ -LUA_API void (lua_pushnil) (lua_State *L); -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); -LUA_API void (lua_pushstring) (lua_State *L, const char *s); -LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, - va_list argp); -LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); -LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); -LUA_API void (lua_pushboolean) (lua_State *L, int b); -LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); -LUA_API int (lua_pushthread) (lua_State *L); - - -/* -** get functions (Lua -> stack) -*/ -LUA_API void (lua_gettable) (lua_State *L, int idx); -LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawget) (lua_State *L, int idx); -LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); -LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); -LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API void (lua_getfenv) (lua_State *L, int idx); - - -/* -** set functions (stack -> Lua) -*/ -LUA_API void (lua_settable) (lua_State *L, int idx); -LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); -LUA_API int (lua_setmetatable) (lua_State *L, int objindex); -LUA_API int (lua_setfenv) (lua_State *L, int idx); - - -/* -** `load' and `call' functions (load and run Lua code) -*/ -LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); -LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); -LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); -LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname); - -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); - - -/* -** coroutine functions -*/ -LUA_API int (lua_yield) (lua_State *L, int nresults); -LUA_API int (lua_resume) (lua_State *L, int narg); -LUA_API int (lua_status) (lua_State *L); - -/* -** garbage-collection function and options -*/ - -#define LUA_GCSTOP 0 -#define LUA_GCRESTART 1 -#define LUA_GCCOLLECT 2 -#define LUA_GCCOUNT 3 -#define LUA_GCCOUNTB 4 -#define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 - -LUA_API int (lua_gc) (lua_State *L, int what, int data); - - -/* -** miscellaneous functions -*/ - -LUA_API int (lua_error) (lua_State *L); - -LUA_API int (lua_next) (lua_State *L, int idx); - -LUA_API void (lua_concat) (lua_State *L, int n); - -LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); - - - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - -#define lua_pop(L,n) lua_settop(L, -(n)-1) - -#define lua_newtable(L) lua_createtable(L, 0, 0) - -#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) - -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) - -#define lua_strlen(L,i) lua_objlen(L, (i)) - -#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) -#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) -#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) -#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) -#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) -#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) -#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) - -#define lua_pushliteral(L, s) \ - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) - -#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) -#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) - -#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) - - - -/* -** compatibility macros and functions -*/ - -#define lua_open() luaL_newstate() - -#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) - -#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) - -#define lua_Chunkreader lua_Reader -#define lua_Chunkwriter lua_Writer - - -/* hack */ -LUA_API void lua_setlevel (lua_State *from, lua_State *to); - - -/* -** {====================================================================== -** Debug API -** ======================================================================= -*/ - - -/* -** Event codes -*/ -#define LUA_HOOKCALL 0 -#define LUA_HOOKRET 1 -#define LUA_HOOKLINE 2 -#define LUA_HOOKCOUNT 3 -#define LUA_HOOKTAILRET 4 - - -/* -** Event masks -*/ -#define LUA_MASKCALL (1 << LUA_HOOKCALL) -#define LUA_MASKRET (1 << LUA_HOOKRET) -#define LUA_MASKLINE (1 << LUA_HOOKLINE) -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) - -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debuger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - - -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); -LUA_API lua_Hook lua_gethook (lua_State *L); -LUA_API int lua_gethookmask (lua_State *L); -LUA_API int lua_gethookcount (lua_State *L); - -/* From Lua 5.2. */ -LUA_API void *lua_upvalueid (lua_State *L, int idx, int n); -LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); -LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname, const char *mode); - - -struct lua_Debug { - int event; - const char *name; /* (n) */ - const char *namewhat; /* (n) `global', `local', `field', `method' */ - const char *what; /* (S) `Lua', `C', `main', `tail' */ - const char *source; /* (S) */ - int currentline; /* (l) */ - int nups; /* (u) number of upvalues */ - int linedefined; /* (S) */ - int lastlinedefined; /* (S) */ - char short_src[LUA_IDSIZE]; /* (S) */ - /* private part */ - int i_ci; /* active function */ -}; - -/* }====================================================================== */ - - -/****************************************************************************** -* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -******************************************************************************/ - - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lua.hpp b/third-party/LuaJIT-2.0.2/src/lua.hpp deleted file mode 100644 index 07e9002dc5..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lua.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// C++ wrapper for LuaJIT header files. - -extern "C" { -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" -#include "luajit.h" -} - diff --git a/third-party/LuaJIT-2.0.2/src/luaconf.h b/third-party/LuaJIT-2.0.2/src/luaconf.h deleted file mode 100644 index d55caab1d0..0000000000 --- a/third-party/LuaJIT-2.0.2/src/luaconf.h +++ /dev/null @@ -1,139 +0,0 @@ -/* -** Configuration header. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef luaconf_h -#define luaconf_h - -#include -#include - -/* Default path for loading Lua and C modules with require(). */ -#if defined(_WIN32) -/* -** In Windows, any exclamation mark ('!') in the path is replaced by the -** path of the directory of the executable file of the current process. -*/ -#define LUA_LDIR "!\\lua\\" -#define LUA_CDIR "!\\" -#define LUA_PATH_DEFAULT \ - ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" -#define LUA_CPATH_DEFAULT \ - ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" -#else -/* -** Note to distribution maintainers: do NOT patch the following line! -** Please read ../doc/install.html#distro and pass PREFIX=/usr instead. -*/ -#define LUA_ROOT "/usr/local/" -#define LUA_LDIR LUA_ROOT "share/lua/5.1/" -#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" -#ifdef LUA_XROOT -#define LUA_JDIR LUA_XROOT "share/luajit-2.0.2/" -#define LUA_XPATH \ - ";" LUA_XROOT "share/lua/5.1/?.lua;" LUA_XROOT "share/lua/5.1/?/init.lua" -#define LUA_XCPATH LUA_XROOT "lib/lua/5.1/?.so;" -#else -#define LUA_JDIR LUA_ROOT "share/luajit-2.0.2/" -#define LUA_XPATH -#define LUA_XCPATH -#endif -#define LUA_PATH_DEFAULT \ - "./?.lua;" LUA_JDIR"?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua" LUA_XPATH -#define LUA_CPATH_DEFAULT \ - "./?.so;" LUA_CDIR"?.so;" LUA_XCPATH LUA_CDIR"loadall.so" -#endif - -/* Environment variable names for path overrides and initialization code. */ -#define LUA_PATH "LUA_PATH" -#define LUA_CPATH "LUA_CPATH" -#define LUA_INIT "LUA_INIT" - -/* Special file system characters. */ -#if defined(_WIN32) -#define LUA_DIRSEP "\\" -#else -#define LUA_DIRSEP "/" -#endif -#define LUA_PATHSEP ";" -#define LUA_PATH_MARK "?" -#define LUA_EXECDIR "!" -#define LUA_IGMARK "-" -#define LUA_PATH_CONFIG \ - LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \ - LUA_EXECDIR "\n" LUA_IGMARK - -/* Quoting in error messages. */ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - -/* Various tunables. */ -#define LUAI_MAXSTACK 65500 /* Max. # of stack slots for a thread (<64K). */ -#define LUAI_MAXCSTACK 8000 /* Max. # of stack slots for a C func (<10K). */ -#define LUAI_GCPAUSE 200 /* Pause GC until memory is at 200%. */ -#define LUAI_GCMUL 200 /* Run GC at 200% of allocation speed. */ -#define LUA_MAXCAPTURES 32 /* Max. pattern captures. */ - -/* Compatibility with older library function names. */ -#define LUA_COMPAT_MOD /* OLD: math.mod, NEW: math.fmod */ -#define LUA_COMPAT_GFIND /* OLD: string.gfind, NEW: string.gmatch */ - -/* Configuration for the frontend (the luajit executable). */ -#if defined(luajit_c) -#define LUA_PROGNAME "luajit" /* Fallback frontend name. */ -#define LUA_PROMPT "> " /* Interactive prompt. */ -#define LUA_PROMPT2 ">> " /* Continuation prompt. */ -#define LUA_MAXINPUT 512 /* Max. input line length. */ -#endif - -/* Note: changing the following defines breaks the Lua 5.1 ABI. */ -#define LUA_INTEGER ptrdiff_t -#define LUA_IDSIZE 60 /* Size of lua_Debug.short_src. */ -/* -** Size of lauxlib and io.* on-stack buffers. Weird workaround to avoid using -** unreasonable amounts of stack space, but still retain ABI compatibility. -** Blame Lua for depending on BUFSIZ in the ABI, blame **** for wrecking it. -*/ -#define LUAL_BUFFERSIZE (BUFSIZ > 16384 ? 8192 : BUFSIZ) - -/* The following defines are here only for compatibility with luaconf.h -** from the standard Lua distribution. They must not be changed for LuaJIT. -*/ -#define LUA_NUMBER_DOUBLE -#define LUA_NUMBER double -#define LUAI_UACNUMBER double -#define LUA_NUMBER_SCAN "%lf" -#define LUA_NUMBER_FMT "%.14g" -#define lua_number2str(s, n) sprintf((s), LUA_NUMBER_FMT, (n)) -#define LUAI_MAXNUMBER2STR 32 -#define LUA_INTFRMLEN "l" -#define LUA_INTFRM_T long - -/* Linkage of public API functions. */ -#if defined(LUA_BUILD_AS_DLL) -#if defined(LUA_CORE) || defined(LUA_LIB) -#define LUA_API __declspec(dllexport) -#else -#define LUA_API __declspec(dllimport) -#endif -#else -#define LUA_API extern -#endif - -#define LUALIB_API LUA_API - -/* Support for internal assertions. */ -#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) -#include -#endif -#ifdef LUA_USE_ASSERT -#define lua_assert(x) assert(x) -#endif -#ifdef LUA_USE_APICHECK -#define luai_apicheck(L, o) { (void)L; assert(o); } -#else -#define luai_apicheck(L, o) { (void)L; } -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/luajit.c b/third-party/LuaJIT-2.0.2/src/luajit.c deleted file mode 100644 index e0eacc423a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/luajit.c +++ /dev/null @@ -1,571 +0,0 @@ -/* -** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include -#include - -#define luajit_c - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" -#include "luajit.h" - -#include "lj_arch.h" - -#if LJ_TARGET_POSIX -#include -#define lua_stdin_is_tty() isatty(0) -#elif LJ_TARGET_WINDOWS -#include -#ifdef __BORLANDC__ -#define lua_stdin_is_tty() isatty(_fileno(stdin)) -#else -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) -#endif -#else -#define lua_stdin_is_tty() 1 -#endif - -#if !LJ_TARGET_CONSOLE -#include -#endif - -static lua_State *globalL = NULL; -static const char *progname = LUA_PROGNAME; - -#if !LJ_TARGET_CONSOLE -static void lstop(lua_State *L, lua_Debug *ar) -{ - (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); - /* Avoid luaL_error -- a C hook doesn't add an extra frame. */ - luaL_where(L, 0); - lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1)); - lua_error(L); -} - -static void laction(int i) -{ - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); -} -#endif - -static void print_usage(void) -{ - fprintf(stderr, - "usage: %s [options]... [script [args]...].\n" - "Available options are:\n" - " -e chunk Execute string " LUA_QL("chunk") ".\n" - " -l name Require library " LUA_QL("name") ".\n" - " -b ... Save or list bytecode.\n" - " -j cmd Perform LuaJIT control command.\n" - " -O[opt] Control LuaJIT optimizations.\n" - " -i Enter interactive mode after executing " LUA_QL("script") ".\n" - " -v Show version information.\n" - " -E Ignore environment variables.\n" - " -- Stop handling options.\n" - " - Execute stdin and stop handling options.\n" - , - progname); - fflush(stderr); -} - -static void l_message(const char *pname, const char *msg) -{ - if (pname) fprintf(stderr, "%s: ", pname); - fprintf(stderr, "%s\n", msg); - fflush(stderr); -} - -static int report(lua_State *L, int status) -{ - if (status && !lua_isnil(L, -1)) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); - lua_pop(L, 1); - } - return status; -} - -static int traceback(lua_State *L) -{ - if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */ - if (lua_isnoneornil(L, 1) || - !luaL_callmeta(L, 1, "__tostring") || - !lua_isstring(L, -1)) - return 1; /* Return non-string error object. */ - lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */ - } - luaL_traceback(L, L, lua_tostring(L, 1), 1); - return 1; -} - -static int docall(lua_State *L, int narg, int clear) -{ - int status; - int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, traceback); /* push traceback function */ - lua_insert(L, base); /* put it under chunk and args */ -#if !LJ_TARGET_CONSOLE - signal(SIGINT, laction); -#endif - status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); -#if !LJ_TARGET_CONSOLE - signal(SIGINT, SIG_DFL); -#endif - lua_remove(L, base); /* remove traceback function */ - /* force a complete garbage collection in case of errors */ - if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); - return status; -} - -static void print_version(void) -{ - fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout); -} - -static void print_jit_status(lua_State *L) -{ - int n; - const char *s; - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ - lua_remove(L, -2); - lua_getfield(L, -1, "status"); - lua_remove(L, -2); - n = lua_gettop(L); - lua_call(L, 0, LUA_MULTRET); - fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout); - for (n++; (s = lua_tostring(L, n)); n++) { - putc(' ', stdout); - fputs(s, stdout); - } - putc('\n', stdout); -} - -static int getargs(lua_State *L, char **argv, int n) -{ - int narg; - int i; - int argc = 0; - while (argv[argc]) argc++; /* count total number of arguments */ - narg = argc - (n + 1); /* number of arguments to the script */ - luaL_checkstack(L, narg + 3, "too many arguments to script"); - for (i = n+1; i < argc; i++) - lua_pushstring(L, argv[i]); - lua_createtable(L, narg, n + 1); - for (i = 0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - n); - } - return narg; -} - -static int dofile(lua_State *L, const char *name) -{ - int status = luaL_loadfile(L, name) || docall(L, 0, 1); - return report(L, status); -} - -static int dostring(lua_State *L, const char *s, const char *name) -{ - int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); - return report(L, status); -} - -static int dolibrary(lua_State *L, const char *name) -{ - lua_getglobal(L, "require"); - lua_pushstring(L, name); - return report(L, docall(L, 1, 1)); -} - -static void write_prompt(lua_State *L, int firstline) -{ - const char *p; - lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2; - fputs(p, stdout); - fflush(stdout); - lua_pop(L, 1); /* remove global */ -} - -static int incomplete(lua_State *L, int status) -{ - if (status == LUA_ERRSYNTAX) { - size_t lmsg; - const char *msg = lua_tolstring(L, -1, &lmsg); - const char *tp = msg + lmsg - (sizeof(LUA_QL("")) - 1); - if (strstr(msg, LUA_QL("")) == tp) { - lua_pop(L, 1); - return 1; - } - } - return 0; /* else... */ -} - -static int pushline(lua_State *L, int firstline) -{ - char buf[LUA_MAXINPUT]; - write_prompt(L, firstline); - if (fgets(buf, LUA_MAXINPUT, stdin)) { - size_t len = strlen(buf); - if (len > 0 && buf[len-1] == '\n') - buf[len-1] = '\0'; - if (firstline && buf[0] == '=') - lua_pushfstring(L, "return %s", buf+1); - else - lua_pushstring(L, buf); - return 1; - } - return 0; -} - -static int loadline(lua_State *L) -{ - int status; - lua_settop(L, 0); - if (!pushline(L, 1)) - return -1; /* no input */ - for (;;) { /* repeat until gets a complete line */ - status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); - if (!incomplete(L, status)) break; /* cannot try to add lines? */ - if (!pushline(L, 0)) /* no more input? */ - return -1; - lua_pushliteral(L, "\n"); /* add a new line... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } - lua_remove(L, 1); /* remove line */ - return status; -} - -static void dotty(lua_State *L) -{ - int status; - const char *oldprogname = progname; - progname = NULL; - while ((status = loadline(L)) != -1) { - if (status == 0) status = docall(L, 0, 0); - report(L, status); - if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) - l_message(progname, - lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", - lua_tostring(L, -1))); - } - } - lua_settop(L, 0); /* clear stack */ - fputs("\n", stdout); - fflush(stdout); - progname = oldprogname; -} - -static int handle_script(lua_State *L, char **argv, int n) -{ - int status; - const char *fname; - int narg = getargs(L, argv, n); /* collect arguments */ - lua_setglobal(L, "arg"); - fname = argv[n]; - if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - lua_insert(L, -(narg+1)); - if (status == 0) - status = docall(L, narg, 0); - else - lua_pop(L, narg); - return report(L, status); -} - -/* Load add-on module. */ -static int loadjitmodule(lua_State *L) -{ - lua_getglobal(L, "require"); - lua_pushliteral(L, "jit."); - lua_pushvalue(L, -3); - lua_concat(L, 2); - if (lua_pcall(L, 1, 1, 0)) { - const char *msg = lua_tostring(L, -1); - if (msg && !strncmp(msg, "module ", 7)) { - err: - l_message(progname, - "unknown luaJIT command or jit.* modules not installed"); - return 1; - } else { - return report(L, 1); - } - } - lua_getfield(L, -1, "start"); - if (lua_isnil(L, -1)) goto err; - lua_remove(L, -2); /* Drop module table. */ - return 0; -} - -/* Run command with options. */ -static int runcmdopt(lua_State *L, const char *opt) -{ - int narg = 0; - if (opt && *opt) { - for (;;) { /* Split arguments. */ - const char *p = strchr(opt, ','); - narg++; - if (!p) break; - if (p == opt) - lua_pushnil(L); - else - lua_pushlstring(L, opt, (size_t)(p - opt)); - opt = p + 1; - } - if (*opt) - lua_pushstring(L, opt); - else - lua_pushnil(L); - } - return report(L, lua_pcall(L, narg, 0, 0)); -} - -/* JIT engine control command: try jit library first or load add-on module. */ -static int dojitcmd(lua_State *L, const char *cmd) -{ - const char *opt = strchr(cmd, '='); - lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ - lua_remove(L, -2); - lua_pushvalue(L, -2); - lua_gettable(L, -2); /* Lookup library function. */ - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); /* Drop non-function and jit.* table, keep module name. */ - if (loadjitmodule(L)) - return 1; - } else { - lua_remove(L, -2); /* Drop jit.* table. */ - } - lua_remove(L, -2); /* Drop module name. */ - return runcmdopt(L, opt ? opt+1 : opt); -} - -/* Optimization flags. */ -static int dojitopt(lua_State *L, const char *opt) -{ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit.opt"); /* Get jit.opt.* module table. */ - lua_remove(L, -2); - lua_getfield(L, -1, "start"); - lua_remove(L, -2); - return runcmdopt(L, opt); -} - -/* Save or list bytecode. */ -static int dobytecode(lua_State *L, char **argv) -{ - int narg = 0; - lua_pushliteral(L, "bcsave"); - if (loadjitmodule(L)) - return 1; - if (argv[0][2]) { - narg++; - argv[0][1] = '-'; - lua_pushstring(L, argv[0]+1); - } - for (argv++; *argv != NULL; narg++, argv++) - lua_pushstring(L, *argv); - return report(L, lua_pcall(L, narg, 0, 0)); -} - -/* check that argument has no extra characters at the end */ -#define notail(x) {if ((x)[2] != '\0') return -1;} - -#define FLAGS_INTERACTIVE 1 -#define FLAGS_VERSION 2 -#define FLAGS_EXEC 4 -#define FLAGS_OPTION 8 -#define FLAGS_NOENV 16 - -static int collectargs(char **argv, int *flags) -{ - int i; - for (i = 1; argv[i] != NULL; i++) { - if (argv[i][0] != '-') /* Not an option? */ - return i; - switch (argv[i][1]) { /* Check option. */ - case '-': - notail(argv[i]); - return (argv[i+1] != NULL ? i+1 : 0); - case '\0': - return i; - case 'i': - notail(argv[i]); - *flags |= FLAGS_INTERACTIVE; - /* fallthrough */ - case 'v': - notail(argv[i]); - *flags |= FLAGS_VERSION; - break; - case 'e': - *flags |= FLAGS_EXEC; - case 'j': /* LuaJIT extension */ - case 'l': - *flags |= FLAGS_OPTION; - if (argv[i][2] == '\0') { - i++; - if (argv[i] == NULL) return -1; - } - break; - case 'O': break; /* LuaJIT extension */ - case 'b': /* LuaJIT extension */ - if (*flags) return -1; - *flags |= FLAGS_EXEC; - return 0; - case 'E': - *flags |= FLAGS_NOENV; - break; - default: return -1; /* invalid option */ - } - } - return 0; -} - -static int runargs(lua_State *L, char **argv, int n) -{ - int i; - for (i = 1; i < n; i++) { - if (argv[i] == NULL) continue; - lua_assert(argv[i][0] == '-'); - switch (argv[i][1]) { /* option */ - case 'e': { - const char *chunk = argv[i] + 2; - if (*chunk == '\0') chunk = argv[++i]; - lua_assert(chunk != NULL); - if (dostring(L, chunk, "=(command line)") != 0) - return 1; - break; - } - case 'l': { - const char *filename = argv[i] + 2; - if (*filename == '\0') filename = argv[++i]; - lua_assert(filename != NULL); - if (dolibrary(L, filename)) - return 1; /* stop if file fails */ - break; - } - case 'j': { /* LuaJIT extension */ - const char *cmd = argv[i] + 2; - if (*cmd == '\0') cmd = argv[++i]; - lua_assert(cmd != NULL); - if (dojitcmd(L, cmd)) - return 1; - break; - } - case 'O': /* LuaJIT extension */ - if (dojitopt(L, argv[i] + 2)) - return 1; - break; - case 'b': /* LuaJIT extension */ - return dobytecode(L, argv+i); - default: break; - } - } - return 0; -} - -static int handle_luainit(lua_State *L) -{ -#if LJ_TARGET_CONSOLE - const char *init = NULL; -#else - const char *init = getenv(LUA_INIT); -#endif - if (init == NULL) - return 0; /* status OK */ - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, "=" LUA_INIT); -} - -static struct Smain { - char **argv; - int argc; - int status; -} smain; - -static int pmain(lua_State *L) -{ - struct Smain *s = &smain; - char **argv = s->argv; - int script; - int flags = 0; - globalL = L; - if (argv[0] && argv[0][0]) progname = argv[0]; - LUAJIT_VERSION_SYM(); /* linker-enforced version check */ - script = collectargs(argv, &flags); - if (script < 0) { /* invalid args? */ - print_usage(); - s->status = 1; - return 0; - } - if ((flags & FLAGS_NOENV)) { - lua_pushboolean(L, 1); - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - } - lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ - luaL_openlibs(L); /* open libraries */ - lua_gc(L, LUA_GCRESTART, -1); - if (!(flags & FLAGS_NOENV)) { - s->status = handle_luainit(L); - if (s->status != 0) return 0; - } - if ((flags & FLAGS_VERSION)) print_version(); - s->status = runargs(L, argv, (script > 0) ? script : s->argc); - if (s->status != 0) return 0; - if (script) { - s->status = handle_script(L, argv, script); - if (s->status != 0) return 0; - } - if ((flags & FLAGS_INTERACTIVE)) { - print_jit_status(L); - dotty(L); - } else if (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) { - if (lua_stdin_is_tty()) { - print_version(); - print_jit_status(L); - dotty(L); - } else { - dofile(L, NULL); /* executes stdin as a file */ - } - } - return 0; -} - -int main(int argc, char **argv) -{ - int status; - lua_State *L = lua_open(); /* create state */ - if (L == NULL) { - l_message(argv[0], "cannot create state: not enough memory"); - return EXIT_FAILURE; - } - smain.argc = argc; - smain.argv = argv; - status = lua_cpcall(L, pmain, NULL); - report(L, status); - lua_close(L); - return (status || smain.status) ? EXIT_FAILURE : EXIT_SUCCESS; -} - diff --git a/third-party/LuaJIT-2.0.2/src/luajit.h b/third-party/LuaJIT-2.0.2/src/luajit.h deleted file mode 100644 index f33b64c566..0000000000 --- a/third-party/LuaJIT-2.0.2/src/luajit.h +++ /dev/null @@ -1,70 +0,0 @@ -/* -** LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/ -** -** Copyright (C) 2005-2013 Mike Pall. All rights reserved. -** -** Permission is hereby granted, free of charge, to any person obtaining -** a copy of this software and associated documentation files (the -** "Software"), to deal in the Software without restriction, including -** without limitation the rights to use, copy, modify, merge, publish, -** distribute, sublicense, and/or sell copies of the Software, and to -** permit persons to whom the Software is furnished to do so, subject to -** the following conditions: -** -** The above copyright notice and this permission notice shall be -** included in all copies or substantial portions of the Software. -** -** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -** -** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] -*/ - -#ifndef _LUAJIT_H -#define _LUAJIT_H - -#include "lua.h" - -#define LUAJIT_VERSION "LuaJIT 2.0.2" -#define LUAJIT_VERSION_NUM 20002 /* Version 2.0.2 = 02.00.02. */ -#define LUAJIT_VERSION_SYM luaJIT_version_2_0_2 -#define LUAJIT_COPYRIGHT "Copyright (C) 2005-2013 Mike Pall" -#define LUAJIT_URL "http://luajit.org/" - -/* Modes for luaJIT_setmode. */ -#define LUAJIT_MODE_MASK 0x00ff - -enum { - LUAJIT_MODE_ENGINE, /* Set mode for whole JIT engine. */ - LUAJIT_MODE_DEBUG, /* Set debug mode (idx = level). */ - - LUAJIT_MODE_FUNC, /* Change mode for a function. */ - LUAJIT_MODE_ALLFUNC, /* Recurse into subroutine protos. */ - LUAJIT_MODE_ALLSUBFUNC, /* Change only the subroutines. */ - - LUAJIT_MODE_TRACE, /* Flush a compiled trace. */ - - LUAJIT_MODE_WRAPCFUNC = 0x10, /* Set wrapper mode for C function calls. */ - - LUAJIT_MODE_MAX -}; - -/* Flags or'ed in to the mode. */ -#define LUAJIT_MODE_OFF 0x0000 /* Turn feature off. */ -#define LUAJIT_MODE_ON 0x0100 /* Turn feature on. */ -#define LUAJIT_MODE_FLUSH 0x0200 /* Flush JIT-compiled code. */ - -/* LuaJIT public C API. */ - -/* Control the JIT engine. */ -LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode); - -/* Enforce (dynamic) linker error for version mismatches. Call from main. */ -LUA_API void LUAJIT_VERSION_SYM(void); - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/lualib.h b/third-party/LuaJIT-2.0.2/src/lualib.h deleted file mode 100644 index 18c6234da7..0000000000 --- a/third-party/LuaJIT-2.0.2/src/lualib.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -** Standard library header. -** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LUALIB_H -#define _LUALIB_H - -#include "lua.h" - -#define LUA_FILEHANDLE "FILE*" - -#define LUA_COLIBNAME "coroutine" -#define LUA_MATHLIBNAME "math" -#define LUA_STRLIBNAME "string" -#define LUA_TABLIBNAME "table" -#define LUA_IOLIBNAME "io" -#define LUA_OSLIBNAME "os" -#define LUA_LOADLIBNAME "package" -#define LUA_DBLIBNAME "debug" -#define LUA_BITLIBNAME "bit" -#define LUA_JITLIBNAME "jit" -#define LUA_FFILIBNAME "ffi" - -LUALIB_API int luaopen_base(lua_State *L); -LUALIB_API int luaopen_math(lua_State *L); -LUALIB_API int luaopen_string(lua_State *L); -LUALIB_API int luaopen_table(lua_State *L); -LUALIB_API int luaopen_io(lua_State *L); -LUALIB_API int luaopen_os(lua_State *L); -LUALIB_API int luaopen_package(lua_State *L); -LUALIB_API int luaopen_debug(lua_State *L); -LUALIB_API int luaopen_bit(lua_State *L); -LUALIB_API int luaopen_jit(lua_State *L); -LUALIB_API int luaopen_ffi(lua_State *L); - -LUALIB_API void luaL_openlibs(lua_State *L); - -#ifndef lua_assert -#define lua_assert(x) ((void)0) -#endif - -#endif diff --git a/third-party/LuaJIT-2.0.2/src/msvcbuild.bat b/third-party/LuaJIT-2.0.2/src/msvcbuild.bat deleted file mode 100644 index cdb42a8de5..0000000000 --- a/third-party/LuaJIT-2.0.2/src/msvcbuild.bat +++ /dev/null @@ -1,113 +0,0 @@ -@rem Script to build LuaJIT with MSVC. -@rem Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -@rem -@rem Either open a "Visual Studio .NET Command Prompt" -@rem (Note that the Express Edition does not contain an x64 compiler) -@rem -or- -@rem Open a "Windows SDK Command Shell" and set the compiler environment: -@rem setenv /release /x86 -@rem -or- -@rem setenv /release /x64 -@rem -@rem Then cd to this directory and run this script. - -@if not defined INCLUDE goto :FAIL - -@setlocal -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set LJLIB=lib /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set LJDLLNAME=lua51.dll -@set LJLIBNAME=lua51.lib -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@set DASMFLAGS=-D WIN -D JIT -D FFI -D P64 -@set LJARCH=x64 -@minilua -@if errorlevel 8 goto :X64 -@set DASMFLAGS=-D WIN -D JIT -D FFI -@set LJARCH=x86 -:X64 -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_x86.dasc -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m peobj -o lj_vm.obj -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set LJCOMPILE=%LJCOMPILE% /Zi -@set LJLINK=%LJLINK% /debug -:NODEBUG -@if "%1"=="amalg" goto :AMALGDLL -@if "%1"=="static" goto :STATIC -%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c -@if errorlevel 1 goto :BAD -%LJLINK% /DLL /out:%LJDLLNAME% lj_*.obj lib_*.obj -@if errorlevel 1 goto :BAD -@goto :MTDLL -:STATIC -%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:%LJLIBNAME% lj_*.obj lib_*.obj -@if errorlevel 1 goto :BAD -@goto :MTDLL -:AMALGDLL -%LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c -@if errorlevel 1 goto :BAD -%LJLINK% /DLL /out:%LJDLLNAME% ljamalg.obj lj_vm.obj -@if errorlevel 1 goto :BAD -:MTDLL -if exist %LJDLLNAME%.manifest^ - %LJMT% -manifest %LJDLLNAME%.manifest -outputresource:%LJDLLNAME%;2 - -%LJCOMPILE% luajit.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:luajit.exe luajit.obj %LJLIBNAME% -@if errorlevel 1 goto :BAD -if exist luajit.exe.manifest^ - %LJMT% -manifest luajit.exe.manifest -outputresource:luajit.exe - -@del *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for Windows/%LJARCH% === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo You must open a "Visual Studio .NET Command Prompt" to run this script -:END diff --git a/third-party/LuaJIT-2.0.2/src/vm_arm.dasc b/third-party/LuaJIT-2.0.2/src/vm_arm.dasc deleted file mode 100644 index 114416a46a..0000000000 --- a/third-party/LuaJIT-2.0.2/src/vm_arm.dasc +++ /dev/null @@ -1,4487 +0,0 @@ -|// Low-level VM code for ARM CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -| -|.arch arm -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -| -|// The following must be C callee-save. -|.define MASKR8, r4 // 255*8 constant for fast bytecode decoding. -|.define KBASE, r5 // Constants of current Lua function. -|.define PC, r6 // Next PC. -|.define DISPATCH, r7 // Opcode dispatch table. -|.define LREG, r8 // Register holding lua_State (also in SAVE_L). -| -|// C callee-save in EABI, but often refetched. Temporary in iOS 3.0+. -|.define BASE, r9 // Base of current Lua stack frame. -| -|// The following temporaries are not saved across C calls, except for RA/RC. -|.define RA, r10 // Callee-save. -|.define RC, r11 // Callee-save. -|.define RB, r12 -|.define OP, r12 // Overlaps RB, must not be lr. -|.define INS, lr -| -|// Calling conventions. Also used as temporaries. -|.define CARG1, r0 -|.define CARG2, r1 -|.define CARG3, r2 -|.define CARG4, r3 -|.define CARG12, r0 // For 1st soft-fp double. -|.define CARG34, r2 // For 2nd soft-fp double. -| -|.define CRET1, r0 -|.define CRET2, r1 -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.define SAVE_R4, [sp, #28] -|.define CFRAME_SPACE, #28 -|.define SAVE_ERRF, [sp, #24] -|.define SAVE_NRES, [sp, #20] -|.define SAVE_CFRAME, [sp, #16] -|.define SAVE_L, [sp, #12] -|.define SAVE_PC, [sp, #8] -|.define SAVE_MULTRES, [sp, #4] -|.define ARG5, [sp] -| -|.define TMPDhi, [sp, #4] -|.define TMPDlo, [sp] -|.define TMPD, [sp] -|.define TMPDp, sp -| -|.if FPU -|.macro saveregs -| push {r5, r6, r7, r8, r9, r10, r11, lr} -| vpush {d8-d15} -| sub sp, sp, CFRAME_SPACE+4 -| str r4, SAVE_R4 -|.endmacro -|.macro restoreregs_ret -| ldr r4, SAVE_R4 -| add sp, sp, CFRAME_SPACE+4 -| vpop {d8-d15} -| pop {r5, r6, r7, r8, r9, r10, r11, pc} -|.endmacro -|.else -|.macro saveregs -| push {r4, r5, r6, r7, r8, r9, r10, r11, lr} -| sub sp, sp, CFRAME_SPACE -|.endmacro -|.macro restoreregs_ret -| add sp, sp, CFRAME_SPACE -| pop {r4, r5, r6, r7, r8, r9, r10, r11, pc} -|.endmacro -|.endif -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -| -|//----------------------------------------------------------------------- -| -|// Trap for not-yet-implemented parts. -|.macro NYI; ud; .endmacro -| -|//----------------------------------------------------------------------- -| -|// Access to frame relative to BASE. -|.define FRAME_FUNC, #-8 -|.define FRAME_PC, #-4 -| -|.macro decode_RA8, dst, ins; and dst, MASKR8, ins, lsr #5; .endmacro -|.macro decode_RB8, dst, ins; and dst, MASKR8, ins, lsr #21; .endmacro -|.macro decode_RC8, dst, ins; and dst, MASKR8, ins, lsr #13; .endmacro -|.macro decode_RD, dst, ins; lsr dst, ins, #16; .endmacro -|.macro decode_OP, dst, ins; and dst, ins, #255; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| ldrb OP, [PC] -|.endmacro -|.macro ins_NEXT2 -| ldr INS, [PC], #4 -|.endmacro -|// Instruction decode+dispatch. -|.macro ins_NEXT3 -| ldr OP, [DISPATCH, OP, lsl #2] -| decode_RA8 RA, INS -| decode_RD RC, INS -| bx OP -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -| ins_NEXT3 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -| .define ins_next3, ins_NEXT3 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| .endmacro -| .macro ins_next3 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Avoid register name substitution for field name. -#define field_pc pc -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| ldr PC, LFUNC:CARG3->field_pc -| ldrb OP, [PC] // STALL: load PC. early PC. -| ldr INS, [PC], #4 -| ldr OP, [DISPATCH, OP, lsl #2] // STALL: load OP. early OP. -| decode_RA8 RA, INS -| add RA, RA, BASE -| bx OP -|.endmacro -| -|.macro ins_call -| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| str PC, [BASE, FRAME_PC] -| ins_callt // STALL: locked PC. -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to test operand types. -|.macro checktp, reg, tp; cmn reg, #-tp; .endmacro -|.macro checktpeq, reg, tp; cmneq reg, #-tp; .endmacro -|.macro checktpne, reg, tp; cmnne reg, #-tp; .endmacro -|.macro checkstr, reg, target; checktp reg, LJ_TSTR; bne target; .endmacro -|.macro checktab, reg, target; checktp reg, LJ_TTAB; bne target; .endmacro -|.macro checkfunc, reg, target; checktp reg, LJ_TFUNC; bne target; .endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro hotcheck, delta -| lsr CARG1, PC, #1 -| and CARG1, CARG1, #126 -| sub CARG1, CARG1, #-GG_DISP2HOT -| ldrh CARG2, [DISPATCH, CARG1] -| subs CARG2, CARG2, #delta -| strh CARG2, [DISPATCH, CARG1] -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP -| blo ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL -| blo ->vm_hotcall -|.endmacro -| -|// Set current VM state. -|.macro mv_vmstate, reg, st; mvn reg, #LJ_VMST_..st; .endmacro -|.macro st_vmstate, reg; str reg, [DISPATCH, #DISPATCH_GL(vmstate)]; .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp -| ldr tmp, [DISPATCH, #DISPATCH_GL(gc.grayagain)] -| bic mark, mark, #LJ_GC_BLACK // black2gray(tab) -| str tab, [DISPATCH, #DISPATCH_GL(gc.grayagain)] -| strb mark, tab->marked -| str tmp, tab->gclist -|.endmacro -| -|.macro .IOS, a, b -|.if IOS -| a, b -|.endif -|.endmacro -| -|//----------------------------------------------------------------------- - -#if !LJ_DUALNUM -#error "Only dual-number mode supported for ARM target" -#endif - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: RB = previous base. - | tst PC, #FRAME_P - | beq ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame. - | mvn CARG2, #~LJ_TTRUE - | mov BASE, RB - | // Prepending may overwrite the pcall frame, so do it at the end. - | str CARG2, [RA, FRAME_PC] // Prepend true to results. - | sub RA, RA, #8 - | - |->vm_returnc: - | adds RC, RC, #8 // RC = (nresults+1)*8. - | mov CRET1, #LUA_YIELD - | beq ->vm_unwind_c_eh - | str RC, SAVE_MULTRES - | ands CARG1, PC, #FRAME_TYPE - | beq ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return - | // CARG1 = PC & FRAME_TYPE - | bic RB, PC, #FRAME_TYPEP - | cmp CARG1, #FRAME_C - | sub RB, BASE, RB // RB = previous base. - | bne ->vm_returnp - | - | str RB, L->base - | ldr KBASE, SAVE_NRES - | mv_vmstate CARG4, C - | sub BASE, BASE, #8 - | subs CARG3, RC, #8 - | lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8 - | st_vmstate CARG4 - | beq >2 - |1: - | subs CARG3, CARG3, #8 - | ldrd CARG12, [RA], #8 - | strd CARG12, [BASE], #8 - | bne <1 - |2: - | cmp KBASE, RC // More/less results wanted? - | bne >6 - |3: - | str BASE, L->top // Store new top. - | - |->vm_leave_cp: - | ldr RC, SAVE_CFRAME // Restore previous C frame. - | mov CRET1, #0 // Ok return status for vm_pcall. - | str RC, L->cframe - | - |->vm_leave_unw: - | restoreregs_ret - | - |6: - | blt >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | ldr CARG3, L->maxstack - | mvn CARG2, #~LJ_TNIL - | cmp BASE, CARG3 - | bhs >8 - | str CARG2, [BASE, #4] - | add RC, RC, #8 - | add BASE, BASE, #8 - | b <2 - | - |7: // Less results wanted. - | sub CARG1, RC, KBASE - | cmp KBASE, #0 // LUA_MULTRET+1 case? - | subne BASE, BASE, CARG1 // Either keep top or shrink it. - | b <3 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | str BASE, L->top // Save current top held in BASE (yes). - | mov CARG2, KBASE - | mov CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->top // Need the (realloced) L->top in BASE. - | b <2 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | mov sp, CARG1 - | mov CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | ldr L, SAVE_L - | mv_vmstate CARG4, C - | ldr GL:CARG3, L->glref - | str CARG4, GL:CARG3->vmstate - | b ->vm_leave_unw - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated. - | mov sp, CARG1 - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | ldr L, SAVE_L - | mov MASKR8, #255 - | mov RC, #16 // 2 results: false + error message. - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | ldr BASE, L->base - | ldr DISPATCH, L->glref // Setup pointer to dispatch table. - | mvn CARG1, #~LJ_TFALSE - | sub RA, BASE, #8 // Results start at BASE-8. - | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame. - | add DISPATCH, DISPATCH, #GG_G2DISP - | mv_vmstate CARG2, INTERP - | str CARG1, [BASE, #-4] // Prepend false to error message. - | st_vmstate CARG2 - | b ->vm_returnc - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | // CARG1 = L - | mov CARG2, #LUA_MINSTACK - | b >2 - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | add RC, BASE, RC - | sub RA, RA, BASE - | mov CARG1, L - | str BASE, L->base - | add PC, PC, #4 // Must point after first instruction. - | str RC, L->top - | lsr CARG3, RA, #3 - |2: - | // L->base = new base, L->top = top - | str PC, SAVE_PC - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->base - | ldr RC, L->top - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | sub NARGS8:RC, RC, BASE - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | mov L, CARG1 - | ldr DISPATCH, L:CARG1->glref // Setup pointer to dispatch table. - | mov BASE, CARG2 - | add DISPATCH, DISPATCH, #GG_G2DISP - | str L, SAVE_L - | mov PC, #FRAME_CP - | str CARG3, SAVE_NRES - | add CARG2, sp, #CFRAME_RESUME - | ldrb CARG1, L->status - | str CARG3, SAVE_ERRF - | str CARG2, L->cframe - | str CARG3, SAVE_CFRAME - | cmp CARG1, #0 - | str L, SAVE_PC // Any value outside of bytecode is ok. - | beq >3 - | - | // Resume after yield (like a return). - | mov RA, BASE - | ldr BASE, L->base - | ldr CARG1, L->top - | mov MASKR8, #255 - | strb CARG3, L->status - | sub RC, CARG1, BASE - | ldr PC, [BASE, FRAME_PC] - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | mv_vmstate CARG2, INTERP - | add RC, RC, #8 - | ands CARG1, PC, #FRAME_TYPE - | st_vmstate CARG2 - | str RC, SAVE_MULTRES - | beq ->BC_RET_Z - | b ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | mov PC, #FRAME_CP - | str CARG4, SAVE_ERRF - | b >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | mov PC, #FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | ldr RC, L:CARG1->cframe - | str CARG3, SAVE_NRES - | mov L, CARG1 - | str CARG1, SAVE_L - | mov BASE, CARG2 - | str sp, L->cframe // Add our C frame to cframe chain. - | ldr DISPATCH, L->glref // Setup pointer to dispatch table. - | str CARG1, SAVE_PC // Any value outside of bytecode is ok. - | str RC, SAVE_CFRAME - | add DISPATCH, DISPATCH, #GG_G2DISP - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | ldr RB, L->base // RB = old base (for vmeta_call). - | ldr CARG1, L->top - | mov MASKR8, #255 - | add PC, PC, BASE - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | sub PC, PC, RB // PC = frame delta + frame type - | mv_vmstate CARG2, INTERP - | sub NARGS8:RC, CARG1, BASE - | st_vmstate CARG2 - | - |->vm_call_dispatch: - | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC - | ldrd CARG34, [BASE, FRAME_FUNC] - | checkfunc CARG4, ->vmeta_call - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | mov L, CARG1 - | ldr RA, L:CARG1->stack - | str CARG1, SAVE_L - | ldr RB, L->top - | str CARG1, SAVE_PC // Any value outside of bytecode is ok. - | ldr RC, L->cframe - | sub RA, RA, RB // Compute -savestack(L, L->top). - | str sp, L->cframe // Add our C frame to cframe chain. - | mov RB, #0 - | str RA, SAVE_NRES // Neg. delta means cframe w/o frame. - | str RB, SAVE_ERRF // No error function. - | str RC, SAVE_CFRAME - | blx CARG4 // (lua_State *L, lua_CFunction func, void *ud) - | ldr DISPATCH, L->glref // Setup pointer to dispatch table. - | movs BASE, CRET1 - | mov PC, #FRAME_CP - | add DISPATCH, DISPATCH, #GG_G2DISP - | bne <3 // Else continue with the call. - | b ->vm_leave_cp // No base? Just remove C frame. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8 - | ldr LFUNC:CARG3, [RB, FRAME_FUNC] - | ldr CARG1, [BASE, #-16] // Get continuation. - | mov CARG4, BASE - | mov BASE, RB // Restore caller BASE. - |.if FFI - | cmp CARG1, #1 - |.endif - | ldr PC, [CARG4, #-12] // Restore PC from [cont|PC]. - | ldr CARG3, LFUNC:CARG3->field_pc - | mvn INS, #~LJ_TNIL - | add CARG2, RA, RC - | str INS, [CARG2, #-4] // Ensure one valid arg. - |.if FFI - | bls >1 - |.endif - | ldr KBASE, [CARG3, #PC2PROTO(k)] - | // BASE = base, RA = resultptr, CARG4 = meta base - | bx CARG1 - | - |.if FFI - |1: - | beq ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - | ldr CARG3, [BASE, FRAME_FUNC] - | sub CARG4, CARG4, #16 - | sub RC, CARG4, BASE - | b ->vm_call_tail - |.endif - | - |->cont_cat: // RA = resultptr, CARG4 = meta base - | ldr INS, [PC, #-4] - | sub CARG2, CARG4, #16 - | ldrd CARG34, [RA] - | str BASE, L->base - | decode_RB8 RC, INS - | decode_RA8 RA, INS - | add CARG1, BASE, RC - | subs CARG1, CARG2, CARG1 - | strdne CARG34, [CARG2] - | movne CARG3, CARG1 - | bne ->BC_CAT_Z - | strd CARG34, [BASE, RA] - | b ->cont_nop - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | add CARG2, BASE, RB - | b >2 - | - |->vmeta_tgets: - | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv) - | mvn CARG4, #~LJ_TTAB - | str TAB:RB, [CARG2] - | str CARG4, [CARG2, #4] - |2: - | mvn CARG4, #~LJ_TSTR - | str STR:RC, TMPDlo - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tgetb: // RC = index - | decode_RB8 RB, INS - | str RC, TMPDlo - | mvn CARG4, #~LJ_TISNUM - | add CARG2, BASE, RB - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tgetv: - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | beq >3 - | ldrd CARG34, [CRET1] - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | rsb CARG1, BASE, #FRAME_CONT - | ldr BASE, L->top - | mov NARGS8:RC, #16 // 2 args for func(t, k). - | str PC, [BASE, #-12] // [cont|PC] - | add PC, CARG1, BASE - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | b ->vm_call_dispatch_f - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | add CARG2, BASE, RB - | b >2 - | - |->vmeta_tsets: - | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv) - | mvn CARG4, #~LJ_TTAB - | str TAB:RB, [CARG2] - | str CARG4, [CARG2, #4] - |2: - | mvn CARG4, #~LJ_TSTR - | str STR:RC, TMPDlo - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tsetb: // RC = index - | decode_RB8 RB, INS - | str RC, TMPDlo - | mvn CARG4, #~LJ_TISNUM - | add CARG2, BASE, RB - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tsetv: - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | ldrd CARG34, [BASE, RA] - | beq >3 - | ins_next1 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | strd CARG34, [CRET1] - | ins_next2 - | ins_next3 - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | rsb CARG1, BASE, #FRAME_CONT - | ldr BASE, L->top - | mov NARGS8:RC, #24 // 3 args for func(t, k, v). - | strd CARG34, [BASE, #16] // Copy value to third argument. - | str PC, [BASE, #-12] // [cont|PC] - | add PC, CARG1, BASE - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | b ->vm_call_dispatch_f - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | mov CARG1, L - | sub PC, PC, #4 - | mov CARG2, RA - | str BASE, L->base - | mov CARG3, RC - | str PC, SAVE_PC - | decode_OP CARG4, INS - | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // Returns 0/1 or TValue * (metamethod). - |3: - | .IOS ldr BASE, L->base - | cmp CRET1, #1 - | bhi ->vmeta_binop - |4: - | ldrh RB, [PC, #2] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | subhs PC, RB, #0x20000 - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | ldr INS, [PC, #-4] - | ldrd CARG12, [RA] - | decode_RA8 CARG3, INS - | strd CARG12, [BASE, CARG3] - | b ->cont_nop - | - |->cont_condt: // RA = resultptr - | ldr CARG2, [RA, #4] - | mvn CARG1, #~LJ_TTRUE - | cmp CARG1, CARG2 // Branch if result is true. - | b <4 - | - |->cont_condf: // RA = resultptr - | ldr CARG2, [RA, #4] - | checktp CARG2, LJ_TFALSE // Branch if result is false. - | b <4 - | - |->vmeta_equal: - | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - | - |->vmeta_equal_cd: - |.if FFI - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | mov CARG2, INS - | str PC, SAVE_PC - | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |.endif - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_vn: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG3, BASE, RB - | add CARG4, KBASE, RC - | b >1 - | - |->vmeta_arith_nv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG4, BASE, RB - | add CARG3, KBASE, RC - | b >1 - | - |->vmeta_unm: - | ldr INS, [PC, #-8] - | sub PC, PC, #4 - | add CARG3, BASE, RC - | add CARG4, BASE, RC - | b >1 - | - |->vmeta_arith_vv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG3, BASE, RB - | add CARG4, BASE, RC - |1: - | decode_OP OP, INS - | add CARG2, BASE, RA - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | str OP, ARG5 - | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // Returns NULL (finished) or TValue * (metamethod). - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | beq ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | sub CARG2, CRET1, BASE - | str PC, [CRET1, #-12] // [cont|PC] - | add PC, CARG2, #FRAME_CONT - | mov BASE, CRET1 - | mov NARGS8:RC, #16 // 2 args for func(o1, o2). - | b ->vm_call_dispatch - | - |->vmeta_len: - | add CARG2, BASE, RC - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_len // (lua_State *L, TValue *o) - | // Returns NULL (retry) or TValue * (metamethod base). - | .IOS ldr BASE, L->base -#if LJ_52 - | cmp CRET1, #0 - | bne ->vmeta_binop // Binop call for compatibility. - | ldr TAB:CARG1, [BASE, RC] - | b ->BC_LEN_Z -#else - | b ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // RB = old base, BASE = new base, RC = nargs*8 - | mov CARG1, L - | str RB, L->base // This is the callers base! - | sub CARG2, BASE, #8 - | str PC, SAVE_PC - | add CARG3, BASE, NARGS8:RC - | .IOS mov RA, BASE - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | .IOS mov BASE, RA - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | mov CARG1, L - | str BASE, L->base - | sub CARG2, RA, #8 - | str PC, SAVE_PC - | add CARG3, RA, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | .IOS ldr BASE, L->base - | ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here. - | ldr PC, [BASE, FRAME_PC] - | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. - | b ->BC_CALLT2_Z - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mov CARG1, L - | str BASE, L->base - | mov CARG2, RA - | str PC, SAVE_PC - | bl extern lj_meta_for // (lua_State *L, TValue *base) - | .IOS ldr BASE, L->base - |.if JIT - | ldrb OP, [PC, #-4] - |.endif - | ldr INS, [PC, #-4] - |.if JIT - | cmp OP, #BC_JFORI - |.endif - | decode_RA8 RA, INS - | decode_RD RC, INS - |.if JIT - | beq =>BC_JFORI - |.endif - | b =>BC_FORI - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | ldrd CARG12, [BASE] - | ldrd CARG34, [BASE, #8] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name - | .ffunc_1 name - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name - | .ffunc_2 name - | checktp CARG2, LJ_TISNUM - | cmnlo CARG4, #-LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |.macro .ffunc_d, name - | .ffunc name - | ldr CARG2, [BASE, #4] - | cmp NARGS8:RC, #8 - | vldr d0, [BASE] - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |.macro .ffunc_dd, name - | .ffunc name - | ldr CARG2, [BASE, #4] - | ldr CARG4, [BASE, #12] - | cmp NARGS8:RC, #16 - | vldr d0, [BASE] - | vldr d1, [BASE, #8] - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | cmnlo CARG4, #-LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2. - |.macro ffgccheck - | ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)] - | ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)] - | cmp CARG1, CARG2 - | blge ->fff_gcstep - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | checktp CARG2, LJ_TTRUE - | bhi ->fff_fallback - | ldr PC, [BASE, FRAME_PC] - | strd CARG12, [BASE, #-8] - | mov RB, BASE - | subs RA, NARGS8:RC, #8 - | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8. - | beq ->fff_res // Done if exactly 1 argument. - |1: - | ldrd CARG12, [RB, #8] - | subs RA, RA, #8 - | strd CARG12, [RB], #8 - | bne <1 - | b ->fff_res - | - |.ffunc type - | ldr CARG2, [BASE, #4] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | mvnlo CARG2, #~LJ_TISNUM - | rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1 - | lsl CARG4, CARG4, #3 - | ldrd CARG12, [CFUNC:CARG3, CARG4] - | b ->fff_restv - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | checktp CARG2, LJ_TTAB - | cmnne CARG2, #-LJ_TUDATA - | bne >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | ldr TAB:RB, TAB:CARG1->metatable - |2: - | mvn CARG2, #~LJ_TNIL - | ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])] - | cmp TAB:RB, #0 - | beq ->fff_restv - | ldr CARG3, TAB:RB->hmask - | ldr CARG4, STR:RC->hash - | ldr NODE:INS, TAB:RB->node - | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask - | add CARG3, CARG3, CARG3, lsl #1 - | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 - |3: // Rearranged logic, because we expect _not_ to find the key. - | ldrd CARG34, NODE:INS->key // STALL: early NODE:INS. - | ldrd CARG12, NODE:INS->val - | ldr NODE:INS, NODE:INS->next - | checktp CARG4, LJ_TSTR - | cmpeq CARG3, STR:RC - | beq >5 - | cmp NODE:INS, #0 - | bne <3 - |4: - | mov CARG1, RB // Use metatable as default result. - | mvn CARG2, #~LJ_TTAB - | b ->fff_restv - |5: - | checktp CARG2, LJ_TNIL - | bne ->fff_restv - | b <4 - | - |6: - | checktp CARG2, LJ_TISNUM - | mvnhs CARG2, CARG2 - | movlo CARG2, #~LJ_TISNUM - | add CARG4, DISPATCH, CARG2, lsl #2 - | ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])] - | b <2 - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | checktp CARG2, LJ_TTAB - | ldreq TAB:RB, TAB:CARG1->metatable - | checktpeq CARG4, LJ_TTAB - | ldrbeq CARG4, TAB:CARG1->marked - | cmpeq TAB:RB, #0 - | bne ->fff_fallback - | tst CARG4, #LJ_GC_BLACK // isblack(table) - | str TAB:CARG3, TAB:CARG1->metatable - | beq ->fff_restv - | barrierback TAB:CARG1, CARG4, CARG3 - | b ->fff_restv - | - |.ffunc rawget - | ldrd CARG34, [BASE] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | mov CARG2, CARG3 - | checktab CARG4, ->fff_fallback - | mov CARG1, L - | add CARG3, BASE, #8 - | .IOS mov RA, BASE - | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // Returns cTValue *. - | .IOS mov BASE, RA - | ldrd CARG12, [CRET1] - | b ->fff_restv - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 - | bne ->fff_fallback - | checktp CARG2, LJ_TISNUM - | bls ->fff_restv - | b ->fff_fallback - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | checktp CARG2, LJ_TSTR - | // A __tostring method in the string base metatable is ignored. - | beq ->fff_restv - | // Handle numbers inline, unless a number base metatable is present. - | ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])] - | str BASE, L->base - | checktp CARG2, LJ_TISNUM - | cmpls CARG4, #0 - | str PC, SAVE_PC // Redundant (but a defined value). - | bhi ->fff_fallback - | ffgccheck - | mov CARG1, L - | mov CARG2, BASE - | bl extern lj_str_fromnumber // (lua_State *L, cTValue *o) - | // Returns GCstr *. - | ldr BASE, L->base - | mvn CARG2, #~LJ_TSTR - | b ->fff_restv - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | mvn CARG4, #~LJ_TNIL - | checktab CARG2, ->fff_fallback - | strd CARG34, [BASE, NARGS8:RC] // Set missing 2nd arg to nil. - | ldr PC, [BASE, FRAME_PC] - | mov CARG2, CARG1 - | str BASE, L->base // Add frame since C call can throw. - | mov CARG1, L - | str BASE, L->top // Dummy frame length is ok. - | add CARG3, BASE, #8 - | str PC, SAVE_PC - | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key) - | // Returns 0 at end of traversal. - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | mvneq CRET2, #~LJ_TNIL - | beq ->fff_restv // End of traversal: return nil. - | ldrd CARG12, [BASE, #8] // Copy key and value to results. - | ldrd CARG34, [BASE, #16] - | mov RC, #(2+1)*8 - | strd CARG12, [BASE, #-8] - | strd CARG34, [BASE] - | b ->fff_res - | - |.ffunc_1 pairs - | checktab CARG2, ->fff_fallback -#if LJ_52 - | ldr TAB:RB, TAB:CARG1->metatable -#endif - | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0] - | ldr PC, [BASE, FRAME_PC] -#if LJ_52 - | cmp TAB:RB, #0 - | bne ->fff_fallback -#endif - | mvn CARG2, #~LJ_TNIL - | mov RC, #(3+1)*8 - | strd CFUNC:CARG34, [BASE, #-8] - | str CARG2, [BASE, #12] - | b ->fff_res - | - |.ffunc_2 ipairs_aux - | checktp CARG2, LJ_TTAB - | checktpeq CARG4, LJ_TISNUM - | bne ->fff_fallback - | ldr RB, TAB:CARG1->asize - | ldr RC, TAB:CARG1->array - | add CARG3, CARG3, #1 - | ldr PC, [BASE, FRAME_PC] - | cmp CARG3, RB - | add RC, RC, CARG3, lsl #3 - | strd CARG34, [BASE, #-8] - | ldrdlo CARG12, [RC] - | mov RC, #(0+1)*8 - | bhs >2 // Not in array part? - |1: - | checktp CARG2, LJ_TNIL - | movne RC, #(2+1)*8 - | strdne CARG12, [BASE] - | b ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | ldr RB, TAB:CARG1->hmask - | mov CARG2, CARG3 - | cmp RB, #0 - | beq ->fff_res - | .IOS mov RA, BASE - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | .IOS mov BASE, RA - | cmp CRET1, #0 - | beq ->fff_res - | ldrd CARG12, [CRET1] - | b <1 - | - |.ffunc_1 ipairs - | checktab CARG2, ->fff_fallback -#if LJ_52 - | ldr TAB:RB, TAB:CARG1->metatable -#endif - | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0] - | ldr PC, [BASE, FRAME_PC] -#if LJ_52 - | cmp TAB:RB, #0 - | bne ->fff_fallback -#endif - | mov CARG1, #0 - | mvn CARG2, #~LJ_TISNUM - | mov RC, #(3+1)*8 - | strd CFUNC:CARG34, [BASE, #-8] - | strd CARG12, [BASE, #8] - | b ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - | tst RA, #HOOK_ACTIVE // Remember active hook before pcall. - | mov RB, BASE - | add BASE, BASE, #8 - | moveq PC, #8+FRAME_PCALL - | movne PC, #8+FRAME_PCALLH - | sub NARGS8:RC, NARGS8:RC, #8 - | b ->vm_call_dispatch - | - |.ffunc_2 xpcall - | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)] - | checkfunc CARG4, ->fff_fallback // Traceback must be a function. - | mov RB, BASE - | strd CARG12, [BASE, #8] // Swap function and traceback. - | strd CARG34, [BASE] - | tst RA, #HOOK_ACTIVE // Remember active hook before pcall. - | add BASE, BASE, #16 - | moveq PC, #16+FRAME_PCALL - | movne PC, #16+FRAME_PCALLH - | sub NARGS8:RC, NARGS8:RC, #16 - | b ->vm_call_dispatch - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | checktp CARG2, LJ_TTHREAD - | bne ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr - |.endif - | ldr PC, [BASE, FRAME_PC] - | str BASE, L->base - | ldr CARG2, L:CARG1->top - | ldrb RA, L:CARG1->status - | ldr RB, L:CARG1->base - | add CARG3, CARG2, NARGS8:RC - | add CARG4, CARG2, RA - | str PC, SAVE_PC - | cmp CARG4, RB - | beq ->fff_fallback - | ldr CARG4, L:CARG1->maxstack - | ldr RB, L:CARG1->cframe - | cmp RA, #LUA_YIELD - | cmpls CARG3, CARG4 - | cmpls RB, #0 - | bhi ->fff_fallback - |1: - |.if resume - | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC. - | add BASE, BASE, #8 - | sub NARGS8:RC, NARGS8:RC, #8 - |.endif - | str CARG3, L:CARG1->top - | str BASE, L->top - |2: // Move args to coroutine. - | ldrd CARG34, [BASE, RB] - | cmp RB, NARGS8:RC - | strdne CARG34, [CARG2, RB] - | add RB, RB, #8 - | bne <2 - | - | mov CARG3, #0 - | mov L:RA, L:CARG1 - | mov CARG4, #0 - | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | // Returns thread status. - |4: - | ldr CARG3, L:RA->base - | mv_vmstate CARG2, INTERP - | ldr CARG4, L:RA->top - | st_vmstate CARG2 - | cmp CRET1, #LUA_YIELD - | ldr BASE, L->base - | bhi >8 - | subs RC, CARG4, CARG3 - | ldr CARG1, L->maxstack - | add CARG2, BASE, RC - | beq >6 // No results? - | cmp CARG2, CARG1 - | mov RB, #0 - | bhi >9 // Need to grow stack? - | - | sub CARG4, RC, #8 - | str CARG3, L:RA->top // Clear coroutine stack. - |5: // Move results from coroutine. - | ldrd CARG12, [CARG3, RB] - | cmp RB, CARG4 - | strd CARG12, [BASE, RB] - | add RB, RB, #8 - | bne <5 - |6: - |.if resume - | mvn CARG3, #~LJ_TTRUE - | add RC, RC, #16 - |7: - | str CARG3, [BASE, #-4] // Prepend true/false to results. - | sub RA, BASE, #8 - |.else - | mov RA, BASE - | add RC, RC, #8 - |.endif - | ands CARG1, PC, #FRAME_TYPE - | str PC, SAVE_PC - | str RC, SAVE_MULTRES - | beq ->BC_RET_Z - | b ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | ldrd CARG12, [CARG4, #-8]! - | mvn CARG3, #~LJ_TFALSE - | mov RC, #(2+1)*8 - | str CARG4, L:RA->top // Remove error from coroutine stack. - | strd CARG12, [BASE] // Copy error message. - | b <7 - |.else - | mov CARG1, L - | mov CARG2, L:RA - | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - | // Never returns. - |.endif - | - |9: // Handle stack expansion on return from yield. - | mov CARG1, L - | lsr CARG2, RC, #3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | mov CRET1, #0 - | b <4 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | ldr CARG1, L->cframe - | add CARG2, BASE, NARGS8:RC - | str BASE, L->base - | tst CARG1, #CFRAME_RESUME - | str CARG2, L->top - | mov CRET1, #LUA_YIELD - | mov CARG3, #0 - | beq ->fff_fallback - | str CARG3, L->cframe - | strb CRET1, L->status - | b ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.macro math_round, func - | .ffunc_1 math_ .. func - | checktp CARG2, LJ_TISNUM - | beq ->fff_restv - | bhi ->fff_fallback - | // Round FP value and normalize result. - | lsl CARG3, CARG2, #1 - | adds RB, CARG3, #0x00200000 - | bpl >2 // |x| < 1? - | mvn CARG4, #0x3e0 - | subs RB, CARG4, RB, asr #21 - | lsl CARG4, CARG2, #11 - | lsl CARG3, CARG1, #11 - | orr CARG4, CARG4, #0x80000000 - | rsb INS, RB, #32 - | orr CARG4, CARG4, CARG1, lsr #21 - | bls >3 // |x| >= 2^31? - | orr CARG3, CARG3, CARG4, lsl INS - | lsr CARG1, CARG4, RB - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 - | addne CARG1, CARG1, #1 - |.else - | bics CARG3, CARG3, CARG2, asr #31 - | addsne CARG1, CARG1, #1 - | ldrdvs CARG12, >9 - | bvs ->fff_restv - |.endif - | cmp CARG2, #0 - | rsblt CARG1, CARG1, #0 - |1: - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |2: // |x| < 1 - | bcs ->fff_restv // |x| is not finite. - | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1 - | moveq CARG1, #0 - | mvnne CARG1, #0 - |.else - | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1 - | moveq CARG1, #0 - | movne CARG1, #1 - |.endif - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |3: // |x| >= 2^31. Check for x == -(2^31). - | cmpeq CARG4, #0x80000000 - |.if "func" == "floor" - | cmpeq CARG3, #0 - |.endif - | bne >4 - | cmp CARG2, #0 - | movmi CARG1, #0x80000000 - | bmi <1 - |4: - | bl ->vm_..func.._sf - | b ->fff_restv - |.endmacro - | - | math_round floor - | math_round ceil - | - |.align 8 - |9: - | .long 0x00000000, 0x41e00000 // 2^31. - | - |.ffunc_1 math_abs - | checktp CARG2, LJ_TISNUM - | bhi ->fff_fallback - | bicne CARG2, CARG2, #0x80000000 - | bne ->fff_restv - | cmp CARG1, #0 - | rsbslt CARG1, CARG1, #0 - | ldrdvs CARG12, <9 - | // Fallthrough. - | - |->fff_restv: - | // CARG12 = TValue result. - | ldr PC, [BASE, FRAME_PC] - | strd CARG12, [BASE, #-8] - |->fff_res1: - | // PC = return. - | mov RC, #(1+1)*8 - |->fff_res: - | // RC = (nresults+1)*8, PC = return. - | ands CARG1, PC, #FRAME_TYPE - | ldreq INS, [PC, #-4] - | str RC, SAVE_MULTRES - | sub RA, BASE, #8 - | bne ->vm_return - | decode_RB8 RB, INS - |5: - | cmp RB, RC // More results expected? - | bhi >6 - | decode_RA8 CARG1, INS - | ins_next1 - | ins_next2 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | sub BASE, RA, CARG1 - | ins_next3 - | - |6: // Fill up results with nil. - | add CARG2, RA, RC - | mvn CARG1, #~LJ_TNIL - | add RC, RC, #8 - | str CARG1, [CARG2, #-4] - | b <5 - | - |.macro math_extern, func - |.if HFABI - | .ffunc_d math_ .. func - |.else - | .ffunc_n math_ .. func - |.endif - | .IOS mov RA, BASE - | bl extern func - | .IOS mov BASE, RA - |.if HFABI - | b ->fff_resd - |.else - | b ->fff_restv - |.endif - |.endmacro - | - |.macro math_extern2, func - |.if HFABI - | .ffunc_dd math_ .. func - |.else - | .ffunc_nn math_ .. func - |.endif - | .IOS mov RA, BASE - | bl extern func - | .IOS mov BASE, RA - |.if HFABI - | b ->fff_resd - |.else - | b ->fff_restv - |.endif - |.endmacro - | - |.if FPU - | .ffunc_d math_sqrt - | vsqrt.f64 d0, d0 - |->fff_resd: - | ldr PC, [BASE, FRAME_PC] - | vstr d0, [BASE, #-8] - | b ->fff_res1 - |.else - | math_extern sqrt - |.endif - | - |.ffunc math_log - |.if HFABI - | ldr CARG2, [BASE, #4] - | cmp NARGS8:RC, #8 // Need exactly 1 argument. - | vldr d0, [BASE] - | bne ->fff_fallback - |.else - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 // Need exactly 1 argument. - | bne ->fff_fallback - |.endif - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - | .IOS mov RA, BASE - | bl extern log - | .IOS mov BASE, RA - |.if HFABI - | b ->fff_resd - |.else - | b ->fff_restv - |.endif - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |->ff_math_deg: - |.if FPU - | .ffunc_d math_rad - | vldr d1, CFUNC:CARG3->upvalue[0] - | vmul.f64 d0, d0, d1 - | b ->fff_resd - |.else - | .ffunc_n math_rad - | ldrd CARG34, CFUNC:CARG3->upvalue[0] - | bl extern __aeabi_dmul - | b ->fff_restv - |.endif - | - |.if HFABI - | .ffunc math_ldexp - | ldr CARG4, [BASE, #4] - | ldrd CARG12, [BASE, #8] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | vldr d0, [BASE] - | checktp CARG4, LJ_TISNUM - | bhs ->fff_fallback - | checktp CARG2, LJ_TISNUM - | bne ->fff_fallback - | .IOS mov RA, BASE - | bl extern ldexp // (double x, int exp) - | .IOS mov BASE, RA - | b ->fff_resd - |.else - |.ffunc_2 math_ldexp - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - | checktp CARG4, LJ_TISNUM - | bne ->fff_fallback - | .IOS mov RA, BASE - | bl extern ldexp // (double x, int exp) - | .IOS mov BASE, RA - | b ->fff_restv - |.endif - | - |.if HFABI - |.ffunc_d math_frexp - | mov CARG1, sp - | .IOS mov RA, BASE - | bl extern frexp - | .IOS mov BASE, RA - | ldr CARG3, [sp] - | mvn CARG4, #~LJ_TISNUM - | ldr PC, [BASE, FRAME_PC] - | vstr d0, [BASE, #-8] - | mov RC, #(2+1)*8 - | strd CARG34, [BASE] - | b ->fff_res - |.else - |.ffunc_n math_frexp - | mov CARG3, sp - | .IOS mov RA, BASE - | bl extern frexp - | .IOS mov BASE, RA - | ldr CARG3, [sp] - | mvn CARG4, #~LJ_TISNUM - | ldr PC, [BASE, FRAME_PC] - | strd CARG12, [BASE, #-8] - | mov RC, #(2+1)*8 - | strd CARG34, [BASE] - | b ->fff_res - |.endif - | - |.if HFABI - |.ffunc_d math_modf - | sub CARG1, BASE, #8 - | ldr PC, [BASE, FRAME_PC] - | .IOS mov RA, BASE - | bl extern modf - | .IOS mov BASE, RA - | mov RC, #(2+1)*8 - | vstr d0, [BASE] - | b ->fff_res - |.else - |.ffunc_n math_modf - | sub CARG3, BASE, #8 - | ldr PC, [BASE, FRAME_PC] - | .IOS mov RA, BASE - | bl extern modf - | .IOS mov BASE, RA - | mov RC, #(2+1)*8 - | strd CARG12, [BASE] - | b ->fff_res - |.endif - | - |.macro math_minmax, name, cond, fcond - |.if FPU - | .ffunc_1 name - | add RB, BASE, RC - | checktp CARG2, LJ_TISNUM - | add RA, BASE, #8 - | bne >4 - |1: // Handle integers. - | ldrd CARG34, [RA] - | cmp RA, RB - | bhs ->fff_restv - | checktp CARG4, LJ_TISNUM - | bne >3 - | cmp CARG1, CARG3 - | add RA, RA, #8 - | mov..cond CARG1, CARG3 - | b <1 - |3: // Convert intermediate result to number and continue below. - | vmov s4, CARG1 - | bhi ->fff_fallback - | vldr d1, [RA] - | vcvt.f64.s32 d0, s4 - | b >6 - | - |4: - | vldr d0, [BASE] - | bhi ->fff_fallback - |5: // Handle numbers. - | ldrd CARG34, [RA] - | vldr d1, [RA] - | cmp RA, RB - | bhs ->fff_resd - | checktp CARG4, LJ_TISNUM - | bhs >7 - |6: - | vcmp.f64 d0, d1 - | vmrs - | add RA, RA, #8 - | vmov..fcond.f64 d0, d1 - | b <5 - |7: // Convert integer to number and continue above. - | vmov s4, CARG3 - | bhi ->fff_fallback - | vcvt.f64.s32 d1, s4 - | b <6 - | - |.else - | - | .ffunc_1 name - | checktp CARG2, LJ_TISNUM - | mov RA, #8 - | bne >4 - |1: // Handle integers. - | ldrd CARG34, [BASE, RA] - | cmp RA, RC - | bhs ->fff_restv - | checktp CARG4, LJ_TISNUM - | bne >3 - | cmp CARG1, CARG3 - | add RA, RA, #8 - | mov..cond CARG1, CARG3 - | b <1 - |3: // Convert intermediate result to number and continue below. - | bhi ->fff_fallback - | bl extern __aeabi_i2d - | ldrd CARG34, [BASE, RA] - | b >6 - | - |4: - | bhi ->fff_fallback - |5: // Handle numbers. - | ldrd CARG34, [BASE, RA] - | cmp RA, RC - | bhs ->fff_restv - | checktp CARG4, LJ_TISNUM - | bhs >7 - |6: - | bl extern __aeabi_cdcmple - | add RA, RA, #8 - | mov..fcond CARG1, CARG3 - | mov..fcond CARG2, CARG4 - | b <5 - |7: // Convert integer to number and continue above. - | bhi ->fff_fallback - | strd CARG12, TMPD - | mov CARG1, CARG3 - | bl extern __aeabi_i2d - | ldrd CARG34, TMPD - | b <6 - |.endif - |.endmacro - | - | math_minmax math_min, gt, hi - | math_minmax math_max, lt, lo - | - |//-- String library ----------------------------------------------------- - | - |.ffunc_1 string_len - | checkstr CARG2, ->fff_fallback - | ldr CARG1, STR:CARG1->len - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |.ffunc string_byte // Only handle the 1-arg case here. - | ldrd CARG12, [BASE] - | ldr PC, [BASE, FRAME_PC] - | cmp NARGS8:RC, #8 - | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument. - | bne ->fff_fallback - | ldr CARG3, STR:CARG1->len - | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end). - | mvn CARG2, #~LJ_TISNUM - | cmp CARG3, #0 - | moveq RC, #(0+1)*8 - | movne RC, #(1+1)*8 - | strd CARG12, [BASE, #-8] - | b ->fff_res - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | ldrd CARG12, [BASE] - | ldr PC, [BASE, FRAME_PC] - | cmp NARGS8:RC, #8 // Need exactly 1 argument. - | checktpeq CARG2, LJ_TISNUM - | bicseq CARG4, CARG1, #255 - | mov CARG3, #1 - | bne ->fff_fallback - | str CARG1, TMPD - | mov CARG2, TMPDp // Points to stack. Little-endian. - |->fff_newstr: - | // CARG2 = str, CARG3 = len. - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_str_new // (lua_State *L, char *str, size_t l) - | // Returns GCstr *. - | ldr BASE, L->base - | mvn CARG2, #~LJ_TSTR - | b ->fff_restv - | - |.ffunc string_sub - | ffgccheck - | ldrd CARG12, [BASE] - | ldrd CARG34, [BASE, #16] - | cmp NARGS8:RC, #16 - | mvn RB, #0 - | beq >1 - | blo ->fff_fallback - | checktp CARG4, LJ_TISNUM - | mov RB, CARG3 - | bne ->fff_fallback - |1: - | ldrd CARG34, [BASE, #8] - | checktp CARG2, LJ_TSTR - | ldreq CARG2, STR:CARG1->len - | checktpeq CARG4, LJ_TISNUM - | bne ->fff_fallback - | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end - | add CARG4, CARG2, #1 - | cmp CARG3, #0 // if (start < 0) start += len+1 - | addlt CARG3, CARG3, CARG4 - | cmp CARG3, #1 // if (start < 1) start = 1 - | movlt CARG3, #1 - | cmp RB, #0 // if (end < 0) end += len+1 - | addlt RB, RB, CARG4 - | bic RB, RB, RB, asr #31 // if (end < 0) end = 0 - | cmp RB, CARG2 // if (end > len) end = len - | add CARG1, STR:CARG1, #sizeof(GCstr)-1 - | movgt RB, CARG2 - | add CARG2, CARG1, CARG3 - | subs CARG3, RB, CARG3 // len = end - start - | add CARG3, CARG3, #1 // len += 1 - | bge ->fff_newstr - |->fff_emptystr: - | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty) - | mvn CARG2, #~LJ_TSTR - | b ->fff_restv - | - |.ffunc string_rep // Only handle the 1-char case inline. - | ffgccheck - | ldrd CARG12, [BASE] - | ldrd CARG34, [BASE, #8] - | cmp NARGS8:RC, #16 - | bne ->fff_fallback // Exactly 2 arguments - | checktp CARG2, LJ_TSTR - | checktpeq CARG4, LJ_TISNUM - | bne ->fff_fallback - | subs CARG4, CARG3, #1 - | ldr CARG2, STR:CARG1->len - | blt ->fff_emptystr // Count <= 0? - | cmp CARG2, #1 - | blo ->fff_emptystr // Zero-length string? - | bne ->fff_fallback // Fallback for > 1-char strings. - | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)] - | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)] - | ldr CARG1, STR:CARG1[1] - | cmp RB, CARG3 - | blo ->fff_fallback - |1: // Fill buffer with char. - | strb CARG1, [CARG2, CARG4] - | subs CARG4, CARG4, #1 - | bge <1 - | b ->fff_newstr - | - |.ffunc string_reverse - | ffgccheck - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - | checkstr CARG2, ->fff_fallback - | ldr CARG3, STR:CARG1->len - | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)] - | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)] - | mov CARG4, CARG3 - | add CARG1, STR:CARG1, #sizeof(GCstr) - | cmp RB, CARG3 - | blo ->fff_fallback - |1: // Reverse string copy. - | ldrb RB, [CARG1], #1 - | subs CARG4, CARG4, #1 - | blt ->fff_newstr - | strb RB, [CARG2, CARG4] - | b <1 - | - |.macro ffstring_case, name, lo - | .ffunc name - | ffgccheck - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - | checkstr CARG2, ->fff_fallback - | ldr CARG3, STR:CARG1->len - | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)] - | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)] - | mov CARG4, #0 - | add CARG1, STR:CARG1, #sizeof(GCstr) - | cmp RB, CARG3 - | blo ->fff_fallback - |1: // ASCII case conversion. - | ldrb RB, [CARG1, CARG4] - | cmp CARG4, CARG3 - | bhs ->fff_newstr - | sub RC, RB, #lo - | cmp RC, #26 - | eorlo RB, RB, #0x20 - | strb RB, [CARG2, CARG4] - | add CARG4, CARG4, #1 - | b <1 - |.endmacro - | - |ffstring_case string_lower, 65 - |ffstring_case string_upper, 97 - | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | checktab CARG2, ->fff_fallback - | .IOS mov RA, BASE - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | .IOS mov BASE, RA - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |//-- Bit library -------------------------------------------------------- - | - |// FP number to bit conversion for soft-float. Clobbers r0-r3. - |->vm_tobit_fb: - | bhi ->fff_fallback - |->vm_tobit: - | lsl RB, CARG2, #1 - | adds RB, RB, #0x00200000 - | movpl CARG1, #0 // |x| < 1? - | bxpl lr - | mvn CARG4, #0x3e0 - | subs RB, CARG4, RB, asr #21 - | bmi >1 // |x| >= 2^32? - | lsl CARG4, CARG2, #11 - | orr CARG4, CARG4, #0x80000000 - | orr CARG4, CARG4, CARG1, lsr #21 - | cmp CARG2, #0 - | lsr CARG1, CARG4, RB - | rsblt CARG1, CARG1, #0 - | bx lr - |1: - | add RB, RB, #21 - | lsr CARG4, CARG1, RB - | rsb RB, RB, #20 - | lsl CARG1, CARG2, #12 - | cmp CARG2, #0 - | orr CARG1, CARG4, CARG1, lsl RB - | rsblt CARG1, CARG1, #0 - | bx lr - | - |.macro .ffunc_bit, name - | .ffunc_1 bit_..name - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - |.endmacro - | - |.ffunc_bit tobit - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | mov CARG3, CARG1 - | mov RA, #8 - |1: - | ldrd CARG12, [BASE, RA] - | cmp RA, NARGS8:RC - | add RA, RA, #8 - | bge >2 - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - | ins CARG3, CARG3, CARG1 - | b <1 - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, orr - |.ffunc_bit_op bxor, eor - | - |2: - | mvn CARG4, #~LJ_TISNUM - | ldr PC, [BASE, FRAME_PC] - | strd CARG34, [BASE, #-8] - | b ->fff_res1 - | - |.ffunc_bit bswap - | eor CARG3, CARG1, CARG1, ror #16 - | bic CARG3, CARG3, #0x00ff0000 - | ror CARG1, CARG1, #8 - | mvn CARG2, #~LJ_TISNUM - | eor CARG1, CARG1, CARG3, lsr #8 - | b ->fff_restv - | - |.ffunc_bit bnot - | mvn CARG1, CARG1 - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |.macro .ffunc_bit_sh, name, ins, shmod - | .ffunc bit_..name - | ldrd CARG12, [BASE, #8] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - |.if shmod == 0 - | and RA, CARG1, #31 - |.else - | rsb RA, CARG1, #0 - |.endif - | ldrd CARG12, [BASE] - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - | ins CARG1, CARG1, RA - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - |.endmacro - | - |.ffunc_bit_sh lshift, lsl, 0 - |.ffunc_bit_sh rshift, lsr, 0 - |.ffunc_bit_sh arshift, asr, 0 - |.ffunc_bit_sh rol, ror, 1 - |.ffunc_bit_sh ror, ror, 0 - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RC = nargs*8 - | ldr CARG3, [BASE, FRAME_FUNC] - | ldr CARG2, L->maxstack - | add CARG1, BASE, NARGS8:RC - | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC. - | str CARG1, L->top - | ldr CARG3, CFUNC:CARG3->f - | str BASE, L->base - | add CARG1, CARG1, #8*LUA_MINSTACK - | str PC, SAVE_PC // Redundant (but a defined value). - | cmp CARG1, CARG2 - | mov CARG1, L - | bhi >5 // Need to grow stack. - | blx CARG3 // (lua_State *L) - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | ldr BASE, L->base - | cmp CRET1, #0 - | lsl RC, CRET1, #3 - | sub RA, BASE, #8 - | bgt ->fff_res // Returned nresults+1? - |1: // Returned 0 or -1: retry fast path. - | ldr CARG1, L->top - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | sub NARGS8:RC, CARG1, BASE - | bne ->vm_call_tail // Returned -1? - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | ands CARG1, PC, #FRAME_TYPE - | bic CARG2, PC, #FRAME_TYPEP - | ldreq INS, [PC, #-4] - | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8. - | addeq CARG2, CARG2, #8 - | sub RB, BASE, CARG2 - | b ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | mov CARG2, #LUA_MINSTACK - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->base - | cmp CARG1, CARG1 // Set zero-flag to force retry. - | b <1 - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | mov RA, lr - | str BASE, L->base - | add CARG2, BASE, NARGS8:RC - | str PC, SAVE_PC // Redundant (but a defined value). - | str CARG2, L->top - | mov CARG1, L - | bl extern lj_gc_step // (lua_State *L) - | ldr BASE, L->base - | mov lr, RA // Help return address predictor. - | ldr CFUNC:CARG3, [BASE, FRAME_FUNC] - | bx lr - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] - | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent. - | bne >5 - | // Decrement the hookcount for consistency, but always do the call. - | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | tst CARG1, #HOOK_ACTIVE - | bne >1 - | sub CARG2, CARG2, #1 - | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT - | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | b >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] - | tst CARG1, #HOOK_ACTIVE // Hook already active? - | beq >1 - |5: // Re-dispatch to static ins. - | decode_OP OP, INS - | add OP, DISPATCH, OP, lsl #2 - | ldr pc, [OP, #GG_DISP2STATIC] - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] - | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | tst CARG1, #HOOK_ACTIVE // Hook already active? - | bne <5 - | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT - | beq <5 - | subs CARG2, CARG2, #1 - | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | beq >1 - | tst CARG1, #LUA_MASKLINE - | beq <5 - |1: - | mov CARG1, L - | str BASE, L->base - | mov CARG2, PC - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |3: - | ldr BASE, L->base - |4: // Re-dispatch to static ins. - | ldrb OP, [PC, #-4] - | ldr INS, [PC, #-4] - | add OP, DISPATCH, OP, lsl #2 - | ldr OP, [OP, #GG_DISP2STATIC] - | decode_RA8 RA, INS - | decode_RD RC, INS - | bx OP - | - |->cont_hook: // Continue from hook yield. - | ldr CARG1, [CARG4, #-24] - | add PC, PC, #4 - | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins. - | b <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L). - | sub CARG1, DISPATCH, #-GG_DISP2J - | str PC, SAVE_PC - | ldr CARG3, LFUNC:CARG3->field_pc - | mov CARG2, PC - | str L, [DISPATCH, #DISPATCH_J(L)] - | ldrb CARG3, [CARG3, #PC2PROTO(framesize)] - | str BASE, L->base - | add CARG3, BASE, CARG3, lsl #3 - | str CARG3, L->top - | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc) - | b <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mov CARG2, PC - |.if JIT - | b >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | orr CARG2, PC, #1 - |1: - |.endif - | add CARG4, BASE, RC - | str PC, SAVE_PC - | mov CARG1, L - | str BASE, L->base - | sub RA, RA, BASE - | str CARG4, L->top - | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc) - | // Returns ASMFunction. - | ldr BASE, L->base - | ldr CARG4, L->top - | mov CARG2, #0 - | add RA, BASE, RA - | sub NARGS8:RC, CARG4, BASE - | str CARG2, SAVE_PC // Invalidate for subsequent line hook. - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | ldr INS, [PC, #-4] - | bx CRET1 - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_exit_handler: - |.if JIT - | sub sp, sp, #12 - | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12} - | ldr CARG1, [sp, #64] // Load original value of lr. - | ldr DISPATCH, [lr] // Load DISPATCH. - | add CARG3, sp, #64 // Recompute original value of sp. - | mv_vmstate CARG4, EXIT - | str CARG3, [sp, #52] // Store sp in RID_SP - | st_vmstate CARG4 - | ldr CARG2, [CARG1, #-4]! // Get exit instruction. - | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC. - | str CARG1, [sp, #60] - |.if FPU - | vpush {d0-d15} - |.endif - | lsl CARG2, CARG2, #8 - | add CARG1, CARG1, CARG2, asr #6 - | ldr CARG2, [lr, #4] // Load exit stub group offset. - | sub CARG1, CARG1, lr - | ldr L, [DISPATCH, #DISPATCH_GL(jit_L)] - | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number. - | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)] - | str CARG1, [DISPATCH, #DISPATCH_J(exitno)] - | mov CARG4, #0 - | str L, [DISPATCH, #DISPATCH_J(L)] - | str BASE, L->base - | str CARG4, [DISPATCH, #DISPATCH_GL(jit_L)] - | sub CARG1, DISPATCH, #-GG_DISP2J - | mov CARG2, sp - | bl extern lj_trace_exit // (jit_State *J, ExitState *ex) - | // Returns MULTRES (unscaled) or negated error code. - | ldr CARG2, L->cframe - | ldr BASE, L->base - | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated. - | mov sp, CARG2 - | ldr PC, SAVE_PC // Get SAVE_PC. - | str L, SAVE_L // Set SAVE_L (on-trace resume/yield). - | b >1 - |.endif - |->vm_exit_interp: - | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set. - |.if JIT - | ldr L, SAVE_L - |1: - | cmp CARG1, #0 - | blt >3 // Check for error from exit. - | lsl RC, CARG1, #3 - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | str RC, SAVE_MULTRES - | mov CARG3, #0 - | ldr CARG2, LFUNC:CARG2->field_pc - | str CARG3, [DISPATCH, #DISPATCH_GL(jit_L)] - | mv_vmstate CARG4, INTERP - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | // Modified copy of ins_next which handles function header dispatch, too. - | ldrb OP, [PC] - | mov MASKR8, #255 - | ldr INS, [PC], #4 - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | st_vmstate CARG4 - | cmp OP, #BC_FUNCF // Function header? - | ldr OP, [DISPATCH, OP, lsl #2] - | decode_RA8 RA, INS - | lsrlo RC, INS, #16 // No: Decode operands A*8 and D. - | subhs RC, RC, #8 - | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8 - | bx OP - | - |3: // Rethrow error from the right C frame. - | rsb CARG2, CARG1, #0 - | mov CARG1, L - | bl extern lj_err_throw // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// FP value rounding. Called from JIT code. - |// - |// double lj_vm_floor/ceil/trunc(double x); - |.macro vm_round, func, hf - |.if hf == 1 - | vmov CARG1, CARG2, d0 - |.endif - | lsl CARG3, CARG2, #1 - | adds RB, CARG3, #0x00200000 - | bpl >2 // |x| < 1? - | mvn CARG4, #0x3cc - | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0. - | bxlo lr // |x| >= 2^52: done. - | mvn CARG4, #1 - | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask - | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask - | subs RB, RB, #32 - | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask - | orrpl CARG3, CARG3, CARG4 - | mvnpl CARG4, #1 - | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0) - |.else - | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0) - |.endif - |.if hf == 1 - | vmoveq d0, CARG1, CARG2 - |.endif - | bxeq lr // iszero: done. - | mvn CARG4, #1 - | cmp RB, #0 - | lslpl CARG3, CARG4, RB - | mvnmi CARG3, #0 - | add RB, RB, #32 - | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask - | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry - |.if hf == 1 - | vmov d0, CARG1, CARG2 - |.endif - | bx lr - | - |2: // |x| < 1: - | bxcs lr // |x| is not finite. - | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0) - |.else - | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0) - |.endif - | mov CARG1, #0 // lo = 0 - | and CARG2, CARG2, #0x80000000 - | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0) - | orrne CARG2, CARG2, CARG4 - |.if hf == 1 - | vmov d0, CARG1, CARG2 - |.endif - | bx lr - |.endmacro - | - |9: - | .long 0x3ff00000 // hiword(+1.0) - | - |->vm_floor: - |.if HFABI - | vm_round floor, 1 - |.endif - |->vm_floor_sf: - | vm_round floor, 0 - | - |->vm_ceil: - |.if HFABI - | vm_round ceil, 1 - |.endif - |->vm_ceil_sf: - | vm_round ceil, 0 - | - |.macro vm_trunc, hf - |.if JIT - |.if hf == 1 - | vmov CARG1, CARG2, d0 - |.endif - | lsl CARG3, CARG2, #1 - | adds RB, CARG3, #0x00200000 - | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0. - | movpl CARG1, #0 - |.if hf == 1 - | vmovpl d0, CARG1, CARG2 - |.endif - | bxpl lr - | mvn CARG4, #0x3cc - | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0. - | bxlo lr // |x| >= 2^52: already done. - | mvn CARG4, #1 - | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask - | subs RB, RB, #32 - | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask - |.if hf == 1 - | vmov d0, CARG1, CARG2 - |.endif - | bx lr - |.endif - |.endmacro - | - |->vm_trunc: - |.if HFABI - | vm_trunc 1 - |.endif - |->vm_trunc_sf: - | vm_trunc 0 - | - | // double lj_vm_mod(double dividend, double divisor); - |->vm_mod: - |.if FPU - | // Special calling convention. Also, RC (r11) is not preserved. - | vdiv.f64 d0, d6, d7 - | mov RC, lr - | vmov CARG1, CARG2, d0 - | bl ->vm_floor_sf - | vmov d0, CARG1, CARG2 - | vmul.f64 d0, d0, d7 - | mov lr, RC - | vsub.f64 d6, d6, d0 - | bx lr - |.else - | push {r0, r1, r2, r3, r4, lr} - | bl extern __aeabi_ddiv - | bl ->vm_floor_sf - | ldrd CARG34, [sp, #8] - | bl extern __aeabi_dmul - | ldrd CARG34, [sp] - | eor CARG2, CARG2, #0x80000000 - | bl extern __aeabi_dadd - | add sp, sp, #20 - | pop {pc} - |.endif - | - | // int lj_vm_modi(int dividend, int divisor); - |->vm_modi: - | ands RB, CARG1, #0x80000000 - | rsbmi CARG1, CARG1, #0 // a = |dividend| - | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor). - | cmp CARG2, #0 - | rsbmi CARG2, CARG2, #0 // b = |divisor| - | subs CARG4, CARG2, #1 - | cmpne CARG1, CARG2 - | moveq CARG1, #0 // if (b == 1 || a == b) a = 0 - | tsthi CARG2, CARG4 - | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1 - | bls >1 - | // Use repeated subtraction to get the remainder. - | clz CARG3, CARG1 - | clz CARG4, CARG2 - | sub CARG4, CARG4, CARG3 - | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8 - | addne pc, pc, CARG3, lsl #3 // Duff's device. - | nop - { - int i; - for (i = 31; i >= 0; i--) { - | cmp CARG1, CARG2, lsl #i - | subhs CARG1, CARG1, CARG2, lsl #i - } - } - |1: - | cmp CARG1, #0 - | cmpne RB, #0 - | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b - | eors CARG2, CARG1, RB, lsl #1 - | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y - | bx lr - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. - |// Saveregs already performed. Callback slot number in [sp], g in r12. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | ldr CTSTATE, GL:r12->ctype_state - | add DISPATCH, r12, #GG_G2DISP - |.if FPU - | str r4, SAVE_R4 - | add r4, sp, CFRAME_SPACE+4+8*8 - | vstmdb r4!, {d8-d15} - |.endif - |.if HFABI - | add r12, CTSTATE, #offsetof(CTState, cb.fpr[8]) - |.endif - | strd CARG34, CTSTATE->cb.gpr[2] - | strd CARG12, CTSTATE->cb.gpr[0] - |.if HFABI - | vstmdb r12!, {d0-d7} - |.endif - | ldr CARG4, [sp] - | add CARG3, sp, #CFRAME_SIZE - | mov CARG1, CTSTATE - | lsr CARG4, CARG4, #3 - | str CARG3, CTSTATE->cb.stack - | mov CARG2, sp - | str CARG4, CTSTATE->cb.slot - | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok. - | bl extern lj_ccallback_enter // (CTState *cts, void *cf) - | // Returns lua_State *. - | ldr BASE, L:CRET1->base - | mv_vmstate CARG2, INTERP - | ldr RC, L:CRET1->top - | mov MASKR8, #255 - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | mov L, CRET1 - | sub RC, RC, BASE - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | st_vmstate CARG2 - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)] - | str BASE, L->base - | str CARG4, L->top - | str L, CTSTATE->L - | mov CARG1, CTSTATE - | mov CARG2, RA - | bl extern lj_ccallback_leave // (CTState *cts, TValue *o) - | ldrd CARG12, CTSTATE->cb.gpr[0] - |.if HFABI - | vldr d0, CTSTATE->cb.fpr[0] - |.endif - | b ->vm_leave_unw - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, r4 - | push {CCSTATE, r5, r11, lr} - | mov CCSTATE, CARG1 - | ldr CARG1, CCSTATE:CARG1->spadj - | ldrb CARG2, CCSTATE->nsp - | add CARG3, CCSTATE, #offsetof(CCallState, stack) - |.if HFABI - | add RB, CCSTATE, #offsetof(CCallState, fpr[0]) - |.endif - | mov r11, sp - | sub sp, sp, CARG1 // Readjust stack. - | subs CARG2, CARG2, #1 - |.if HFABI - | vldm RB, {d0-d7} - |.endif - | ldr RB, CCSTATE->func - | bmi >2 - |1: // Copy stack slots. - | ldr CARG4, [CARG3, CARG2, lsl #2] - | str CARG4, [sp, CARG2, lsl #2] - | subs CARG2, CARG2, #1 - | bpl <1 - |2: - | ldrd CARG12, CCSTATE->gpr[0] - | ldrd CARG34, CCSTATE->gpr[2] - | blx RB - | mov sp, r11 - |.if HFABI - | add r12, CCSTATE, #offsetof(CCallState, fpr[4]) - |.endif - | strd CRET1, CCSTATE->gpr[0] - |.if HFABI - | vstmdb r12!, {d0-d3} - |.endif - | pop {CCSTATE, r5, r11, pc} - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RC = src2, JMP with RC = target - | lsl RC, RC, #3 - | ldrd CARG12, [RA, BASE]! - | ldrh RB, [PC, #2] - | ldrd CARG34, [RC, BASE]! - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TISNUM - | bne >3 - | checktp CARG4, LJ_TISNUM - | bne >4 - | cmp CARG1, CARG3 - if (op == BC_ISLT) { - | sublt PC, RB, #0x20000 - } else if (op == BC_ISGE) { - | subge PC, RB, #0x20000 - } else if (op == BC_ISLE) { - | suble PC, RB, #0x20000 - } else { - | subgt PC, RB, #0x20000 - } - |1: - | ins_next - | - |3: // CARG12 is not an integer. - |.if FPU - | vldr d0, [RA] - | bhi ->vmeta_comp - | // d0 is a number. - | checktp CARG4, LJ_TISNUM - | vldr d1, [RC] - | blo >5 - | bhi ->vmeta_comp - | // d0 is a number, CARG3 is an integer. - | vmov s4, CARG3 - | vcvt.f64.s32 d1, s4 - | b >5 - |4: // CARG1 is an integer, CARG34 is not an integer. - | vldr d1, [RC] - | bhi ->vmeta_comp - | // CARG1 is an integer, d1 is a number. - | vmov s4, CARG1 - | vcvt.f64.s32 d0, s4 - |5: // d0 and d1 are numbers. - | vcmp.f64 d0, d1 - | vmrs - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - if (op == BC_ISLT) { - | sublo PC, RB, #0x20000 - } else if (op == BC_ISGE) { - | subhs PC, RB, #0x20000 - } else if (op == BC_ISLE) { - | subls PC, RB, #0x20000 - } else { - | subhi PC, RB, #0x20000 - } - | b <1 - |.else - | bhi ->vmeta_comp - | // CARG12 is a number. - | checktp CARG4, LJ_TISNUM - | movlo RA, RB // Save RB. - | blo >5 - | bhi ->vmeta_comp - | // CARG12 is a number, CARG3 is an integer. - | mov CARG1, CARG3 - | mov RC, RA - | mov RA, RB // Save RB. - | bl extern __aeabi_i2d - | mov CARG3, CARG1 - | mov CARG4, CARG2 - | ldrd CARG12, [RC] // Restore first operand. - | b >5 - |4: // CARG1 is an integer, CARG34 is not an integer. - | bhi ->vmeta_comp - | // CARG1 is an integer, CARG34 is a number. - | mov RA, RB // Save RB. - | bl extern __aeabi_i2d - | ldrd CARG34, [RC] // Restore second operand. - |5: // CARG12 and CARG34 are numbers. - | bl extern __aeabi_cdcmple - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - if (op == BC_ISLT) { - | sublo PC, RA, #0x20000 - } else if (op == BC_ISGE) { - | subhs PC, RA, #0x20000 - } else if (op == BC_ISLE) { - | subls PC, RA, #0x20000 - } else { - | subhi PC, RA, #0x20000 - } - | b <1 - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RC = src2, JMP with RC = target - | lsl RC, RC, #3 - | ldrd CARG12, [RA, BASE]! - | ldrh RB, [PC, #2] - | ldrd CARG34, [RC, BASE]! - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TISNUM - | cmnls CARG4, #-LJ_TISNUM - if (vk) { - | bls ->BC_ISEQN_Z - } else { - | bls ->BC_ISNEN_Z - } - | // Either or both types are not numbers. - |.if FFI - | checktp CARG2, LJ_TCDATA - | checktpne CARG4, LJ_TCDATA - | beq ->vmeta_equal_cd - |.endif - | cmp CARG2, CARG4 // Compare types. - | bne >2 // Not the same type? - | checktp CARG2, LJ_TISPRI - | bhs >1 // Same type and primitive type? - | - | // Same types and not a primitive type. Compare GCobj or pvalue. - | cmp CARG1, CARG3 - if (vk) { - | bne >3 // Different GCobjs or pvalues? - |1: // Branch if same. - | sub PC, RB, #0x20000 - |2: // Different. - | ins_next - |3: - | checktp CARG2, LJ_TISTABUD - | bhi <2 // Different objects and not table/ud? - } else { - | beq >1 // Same GCobjs or pvalues? - | checktp CARG2, LJ_TISTABUD - | bhi >2 // Different objects and not table/ud? - } - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | ldr TAB:RA, TAB:CARG1->metatable - | cmp TAB:RA, #0 - if (vk) { - | beq <2 // No metatable? - } else { - | beq >2 // No metatable? - } - | ldrb RA, TAB:RA->nomm - | mov CARG4, #1-vk // ne = 0 or 1. - | mov CARG2, CARG1 - | tst RA, #1<vmeta_equal // 'no __eq' flag not set? - if (vk) { - | b <2 - } else { - |2: // Branch if different. - | sub PC, RB, #0x20000 - |1: // Same. - | ins_next - } - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RC = str_const (~), JMP with RC = target - | mvn RC, RC - | ldrd CARG12, [BASE, RA] - | ldrh RB, [PC, #2] - | ldr STR:CARG3, [KBASE, RC, lsl #2] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TSTR - |.if FFI - | bne >7 - | cmp CARG1, CARG3 - |.else - | cmpeq CARG1, CARG3 - |.endif - if (vk) { - | subeq PC, RB, #0x20000 - |1: - } else { - |1: - | subne PC, RB, #0x20000 - } - | ins_next - | - |.if FFI - |7: - | checktp CARG2, LJ_TCDATA - | bne <1 - | b ->vmeta_equal_cd - |.endif - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RC = num_const (~), JMP with RC = target - | lsl RC, RC, #3 - | ldrd CARG12, [RA, BASE]! - | ldrh RB, [PC, #2] - | ldrd CARG34, [RC, KBASE]! - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - if (vk) { - |->BC_ISEQN_Z: - } else { - |->BC_ISNEN_Z: - } - | checktp CARG2, LJ_TISNUM - | bne >3 - | checktp CARG4, LJ_TISNUM - | bne >4 - | cmp CARG1, CARG3 - if (vk) { - | subeq PC, RB, #0x20000 - |1: - } else { - |1: - | subne PC, RB, #0x20000 - } - |2: - | ins_next - | - |3: // CARG12 is not an integer. - |.if FFI - | bhi >7 - |.else - if (!vk) { - | subhi PC, RB, #0x20000 - } - | bhi <2 - |.endif - |.if FPU - | checktp CARG4, LJ_TISNUM - | vmov s4, CARG3 - | vldr d0, [RA] - | vldrlo d1, [RC] - | vcvths.f64.s32 d1, s4 - | b >5 - |4: // CARG1 is an integer, d1 is a number. - | vmov s4, CARG1 - | vldr d1, [RC] - | vcvt.f64.s32 d0, s4 - |5: // d0 and d1 are numbers. - | vcmp.f64 d0, d1 - | vmrs - if (vk) { - | subeq PC, RB, #0x20000 - } else { - | subne PC, RB, #0x20000 - } - | b <2 - |.else - | // CARG12 is a number. - | checktp CARG4, LJ_TISNUM - | movlo RA, RB // Save RB. - | blo >5 - | // CARG12 is a number, CARG3 is an integer. - | mov CARG1, CARG3 - | mov RC, RA - |4: // CARG1 is an integer, CARG34 is a number. - | mov RA, RB // Save RB. - | bl extern __aeabi_i2d - | ldrd CARG34, [RC] // Restore other operand. - |5: // CARG12 and CARG34 are numbers. - | bl extern __aeabi_cdcmpeq - if (vk) { - | subeq PC, RA, #0x20000 - } else { - | subne PC, RA, #0x20000 - } - | b <2 - |.endif - | - |.if FFI - |7: - | checktp CARG2, LJ_TCDATA - | bne <1 - | b ->vmeta_equal_cd - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RC = primitive_type (~), JMP with RC = target - | ldrd CARG12, [BASE, RA] - | ldrh RB, [PC, #2] - | add PC, PC, #4 - | mvn RC, RC - | add RB, PC, RB, lsl #2 - |.if FFI - | checktp CARG2, LJ_TCDATA - | beq ->vmeta_equal_cd - |.endif - | cmp CARG2, RC - if (vk) { - | subeq PC, RB, #0x20000 - } else { - | subne PC, RB, #0x20000 - } - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RC = src, JMP with RC = target - | add RC, BASE, RC, lsl #3 - | ldrh RB, [PC, #2] - | ldrd CARG12, [RC] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TTRUE - if (op == BC_ISTC || op == BC_IST) { - | subls PC, RB, #0x20000 - if (op == BC_ISTC) { - | strdls CARG12, [BASE, RA] - } - } else { - | subhi PC, RB, #0x20000 - if (op == BC_ISFC) { - | strdhi CARG12, [BASE, RA] - } - } - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RC = src - | lsl RC, RC, #3 - | ins_next1 - | ldrd CARG12, [BASE, RC] - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_NOT: - | // RA = dst*8, RC = src - | add RC, BASE, RC, lsl #3 - | ins_next1 - | ldr CARG1, [RC, #4] - | add RA, BASE, RA - | ins_next2 - | checktp CARG1, LJ_TTRUE - | mvnls CARG2, #~LJ_TFALSE - | mvnhi CARG2, #~LJ_TTRUE - | str CARG2, [RA, #4] - | ins_next3 - break; - case BC_UNM: - | // RA = dst*8, RC = src - | lsl RC, RC, #3 - | ldrd CARG12, [BASE, RC] - | ins_next1 - | ins_next2 - | checktp CARG2, LJ_TISNUM - | bhi ->vmeta_unm - | eorne CARG2, CARG2, #0x80000000 - | bne >5 - | rsbseq CARG1, CARG1, #0 - | ldrdvs CARG12, >9 - |5: - | strd CARG12, [BASE, RA] - | ins_next3 - | - |.align 8 - |9: - | .long 0x00000000, 0x41e00000 // 2^31. - break; - case BC_LEN: - | // RA = dst*8, RC = src - | lsl RC, RC, #3 - | ldrd CARG12, [BASE, RC] - | checkstr CARG2, >2 - | ldr CARG1, STR:CARG1->len - |1: - | mvn CARG2, #~LJ_TISNUM - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |2: - | checktab CARG2, ->vmeta_len -#if LJ_52 - | ldr TAB:CARG3, TAB:CARG1->metatable - | cmp TAB:CARG3, #0 - | bne >9 - |3: -#endif - |->BC_LEN_Z: - | .IOS mov RC, BASE - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | .IOS mov BASE, RC - | b <1 -#if LJ_52 - |9: - | ldrb CARG4, TAB:CARG3->nomm - | tst CARG4, #1<vmeta_len -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithcheck, cond, ncond, target - ||if (vk == 1) { - | cmn CARG4, #-LJ_TISNUM - | cmn..cond CARG2, #-LJ_TISNUM - ||} else { - | cmn CARG2, #-LJ_TISNUM - | cmn..cond CARG4, #-LJ_TISNUM - ||} - | b..ncond target - |.endmacro - |.macro ins_arithcheck_int, target - | ins_arithcheck eq, ne, target - |.endmacro - |.macro ins_arithcheck_num, target - | ins_arithcheck lo, hs, target - |.endmacro - | - |.macro ins_arithpre - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | .if FPU - | ldrd CARG12, [RB, BASE]! - | ldrd CARG34, [RC, KBASE]! - | .else - | ldrd CARG12, [BASE, RB] - | ldrd CARG34, [KBASE, RC] - | .endif - || break; - ||case 1: - | .if FPU - | ldrd CARG34, [RB, BASE]! - | ldrd CARG12, [RC, KBASE]! - | .else - | ldrd CARG34, [BASE, RB] - | ldrd CARG12, [KBASE, RC] - | .endif - || break; - ||default: - | .if FPU - | ldrd CARG12, [RB, BASE]! - | ldrd CARG34, [RC, BASE]! - | .else - | ldrd CARG12, [BASE, RB] - | ldrd CARG34, [BASE, RC] - | .endif - || break; - ||} - |.endmacro - | - |.macro ins_arithpre_fpu, reg1, reg2 - |.if FPU - ||if (vk == 1) { - | vldr reg2, [RB] - | vldr reg1, [RC] - ||} else { - | vldr reg1, [RB] - | vldr reg2, [RC] - ||} - |.endif - |.endmacro - | - |.macro ins_arithpost_fpu, reg - | ins_next1 - | add RA, BASE, RA - | ins_next2 - | vstr reg, [RA] - | ins_next3 - |.endmacro - | - |.macro ins_arithfallback, ins - ||switch (vk) { - ||case 0: - | ins ->vmeta_arith_vn - || break; - ||case 1: - | ins ->vmeta_arith_nv - || break; - ||default: - | ins ->vmeta_arith_vv - || break; - ||} - |.endmacro - | - |.macro ins_arithdn, intins, fpins, fpcall - | ins_arithpre - |.if "intins" ~= "vm_modi" and not FPU - | ins_next1 - |.endif - | ins_arithcheck_int >5 - |.if "intins" == "smull" - | smull CARG1, RC, CARG3, CARG1 - | cmp RC, CARG1, asr #31 - | ins_arithfallback bne - |.elif "intins" == "vm_modi" - | movs CARG2, CARG3 - | ins_arithfallback beq - | bl ->vm_modi - | mvn CARG2, #~LJ_TISNUM - |.else - | intins CARG1, CARG1, CARG3 - | ins_arithfallback bvs - |.endif - |4: - |.if "intins" == "vm_modi" or FPU - | ins_next1 - |.endif - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |5: // FP variant. - | ins_arithpre_fpu d6, d7 - | ins_arithfallback ins_arithcheck_num - |.if FPU - |.if "intins" == "vm_modi" - | bl fpcall - |.else - | fpins d6, d6, d7 - |.endif - | ins_arithpost_fpu d6 - |.else - | bl fpcall - |.if "intins" ~= "vm_modi" - | ins_next1 - |.endif - | b <4 - |.endif - |.endmacro - | - |.macro ins_arithfp, fpins, fpcall - | ins_arithpre - |.if "fpins" ~= "extern" or HFABI - | ins_arithpre_fpu d0, d1 - |.endif - | ins_arithfallback ins_arithcheck_num - |.if "fpins" == "extern" - | .IOS mov RC, BASE - | bl fpcall - | .IOS mov BASE, RC - |.elif FPU - | fpins d0, d0, d1 - |.else - | bl fpcall - |.endif - |.if ("fpins" ~= "extern" or HFABI) and FPU - | ins_arithpost_fpu d0 - |.else - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |.endif - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arithdn adds, vadd.f64, extern __aeabi_dadd - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arithdn subs, vsub.f64, extern __aeabi_dsub - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arithdn smull, vmul.f64, extern __aeabi_dmul - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arithfp vdiv.f64, extern __aeabi_ddiv - break; - case BC_MODVN: case BC_MODNV: case BC_MODVV: - | ins_arithdn vm_modi, vm_mod, ->vm_mod - break; - case BC_POW: - | // NYI: (partial) integer arithmetic. - | ins_arithfp extern, extern pow - break; - - case BC_CAT: - | decode_RB8 RC, INS - | decode_RC8 RB, INS - | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!) - | sub CARG3, RB, RC - | str BASE, L->base - | add CARG2, BASE, RB - |->BC_CAT_Z: - | // RA = dst*8, RC = src_start*8, CARG2 = top-1 - | mov CARG1, L - | str PC, SAVE_PC - | lsr CARG3, CARG3, #3 - | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // Returns NULL (finished) or TValue * (metamethod). - | ldr BASE, L->base - | cmp CRET1, #0 - | bne ->vmeta_binop - | ldrd CARG34, [BASE, RC] - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] // Copy result to RA. - | ins_next3 - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RC = str_const (~) - | mvn RC, RC - | ins_next1 - | ldr CARG1, [KBASE, RC, lsl #2] - | mvn CARG2, #~LJ_TSTR - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RC = cdata_const (~) - | mvn RC, RC - | ins_next1 - | ldr CARG1, [KBASE, RC, lsl #2] - | mvn CARG2, #~LJ_TCDATA - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, (RC = int16_literal) - | mov CARG1, INS, asr #16 // Refetch sign-extended reg. - | mvn CARG2, #~LJ_TISNUM - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_KNUM: - | // RA = dst*8, RC = num_const - | lsl RC, RC, #3 - | ins_next1 - | ldrd CARG12, [KBASE, RC] - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_KPRI: - | // RA = dst*8, RC = primitive_type (~) - | add RA, BASE, RA - | mvn RC, RC - | ins_next1 - | ins_next2 - | str RC, [RA, #4] - | ins_next3 - break; - case BC_KNIL: - | // RA = base*8, RC = end - | add RA, BASE, RA - | add RC, BASE, RC, lsl #3 - | mvn CARG1, #~LJ_TNIL - | str CARG1, [RA, #4] - | add RA, RA, #8 - |1: - | str CARG1, [RA, #4] - | cmp RA, RC - | add RA, RA, #8 - | blt <1 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RC = uvnum - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsl RC, RC, #2 - | add RC, RC, #offsetof(GCfuncL, uvptr) - | ldr UPVAL:CARG2, [LFUNC:CARG2, RC] - | ldr CARG2, UPVAL:CARG2->v - | ldrd CARG34, [CARG2] - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - break; - case BC_USETV: - | // RA = uvnum*8, RC = src - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | lsl RC, RC, #3 - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | ldrd CARG34, [BASE, RC] - | ldrb RB, UPVAL:CARG2->marked - | ldrb RC, UPVAL:CARG2->closed - | ldr CARG2, UPVAL:CARG2->v - | tst RB, #LJ_GC_BLACK // isblack(uv) - | add RB, CARG4, #-LJ_TISGCV - | cmpne RC, #0 - | strd CARG34, [CARG2] - | bne >2 // Upvalue is closed and black? - |1: - | ins_next - | - |2: // Check if new value is collectable. - | cmn RB, #-(LJ_TISNUM - LJ_TISGCV) - | ldrbhi RC, GCOBJ:CARG3->gch.marked - | bls <1 // tvisgcv(v) - | sub CARG1, DISPATCH, #-GG_DISP2G - | tst RC, #LJ_GC_WHITES - | // Crossed a write barrier. Move the barrier forward. - |.if IOS - | beq <1 - | mov RC, BASE - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | mov BASE, RC - |.else - | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv) - |.endif - | b <1 - break; - case BC_USETS: - | // RA = uvnum*8, RC = str_const (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | mvn RC, RC - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | ldr STR:CARG3, [KBASE, RC, lsl #2] - | mvn CARG4, #~LJ_TSTR - | ldrb RB, UPVAL:CARG2->marked - | ldr CARG2, UPVAL:CARG2->v - | ldrb RC, UPVAL:CARG2->closed - | tst RB, #LJ_GC_BLACK // isblack(uv) - | ldrb RB, STR:CARG3->marked - | strd CARG34, [CARG2] - | bne >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | tst RB, #LJ_GC_WHITES // iswhite(str) - | cmpne RC, #0 - | sub CARG1, DISPATCH, #-GG_DISP2G - | // Crossed a write barrier. Move the barrier forward. - |.if IOS - | beq <1 - | mov RC, BASE - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | mov BASE, RC - |.else - | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv) - |.endif - | b <1 - break; - case BC_USETN: - | // RA = uvnum*8, RC = num_const - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | lsl RC, RC, #3 - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | ldrd CARG34, [KBASE, RC] - | ldr CARG2, UPVAL:CARG2->v - | ins_next1 - | ins_next2 - | strd CARG34, [CARG2] - | ins_next3 - break; - case BC_USETP: - | // RA = uvnum*8, RC = primitive_type (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | mvn RC, RC - | ldr CARG2, UPVAL:CARG2->v - | ins_next1 - | ins_next2 - | str RC, [CARG2, #4] - | ins_next3 - break; - - case BC_UCLO: - | // RA = level*8, RC = target - | ldr CARG3, L->openupval - | add RC, PC, RC, lsl #2 - | str BASE, L->base - | cmp CARG3, #0 - | sub PC, RC, #0x20000 - | beq >1 - | mov CARG1, L - | add CARG2, BASE, RA - | bl extern lj_func_closeuv // (lua_State *L, TValue *level) - | ldr BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RC = proto_const (~) (holding function prototype) - | mvn RC, RC - | str BASE, L->base - | ldr CARG2, [KBASE, RC, lsl #2] - | str PC, SAVE_PC - | ldr CARG3, [BASE, FRAME_FUNC] - | mov CARG1, L - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | bl extern lj_func_newL_gc - | // Returns GCfuncL *. - | ldr BASE, L->base - | mvn CARG2, #~LJ_TFUNC - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RC = (hbits|asize) | tab_const (~) - if (op == BC_TDUP) { - | mvn RC, RC - } - | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)] - | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)] - | str BASE, L->base - | str PC, SAVE_PC - | cmp CARG3, CARG4 - | mov CARG1, L - | bhs >5 - |1: - if (op == BC_TNEW) { - | lsl CARG2, RC, #21 - | lsr CARG3, RC, #11 - | asr RC, CARG2, #21 - | lsr CARG2, CARG2, #21 - | cmn RC, #1 - | addeq CARG2, CARG2, #2 - | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Returns GCtab *. - } else { - | ldr CARG2, [KBASE, RC, lsl #2] - | bl extern lj_tab_dup // (lua_State *L, Table *kt) - | // Returns GCtab *. - } - | ldr BASE, L->base - | mvn CARG2, #~LJ_TTAB - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |5: - | bl extern lj_gc_step_fixtop // (lua_State *L) - | mov CARG1, L - | b <1 - break; - - case BC_GGET: - | // RA = dst*8, RC = str_const (~) - case BC_GSET: - | // RA = dst*8, RC = str_const (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | mvn RC, RC - | ldr TAB:CARG1, LFUNC:CARG2->env - | ldr STR:RC, [KBASE, RC, lsl #2] - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - break; - - case BC_TGETV: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = dst*8, RB = table*8, RC = key*8 - | ldrd TAB:CARG12, [BASE, RB] - | ldrd CARG34, [BASE, RC] - | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12. - | checktp CARG4, LJ_TISNUM // Integer key? - | ldreq CARG4, TAB:CARG1->array - | ldreq CARG2, TAB:CARG1->asize - | bne >9 - | - | add CARG4, CARG4, CARG3, lsl #3 - | cmp CARG3, CARG2 // In array part? - | ldrdlo CARG34, [CARG4] - | bhs ->vmeta_tgetv - | ins_next1 // Overwrites RB! - | checktp CARG4, LJ_TNIL - | beq >5 - |1: - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG2, TAB:CARG1->metatable - | cmp TAB:CARG2, #0 - | beq <1 // No metatable: done. - | ldrb CARG2, TAB:CARG2->nomm - | tst CARG2, #1<vmeta_tgetv - | - |9: - | checktp CARG4, LJ_TSTR // String key? - | moveq STR:RC, CARG3 - | beq ->BC_TGETS_Z - | b ->vmeta_tgetv - break; - case BC_TGETS: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = dst*8, RB = table*8, RC = str_const (~) - | ldrd CARG12, [BASE, RB] - | mvn RC, RC - | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC. - | checktab CARG2, ->vmeta_tgets1 - |->BC_TGETS_Z: - | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8 - | ldr CARG3, TAB:CARG1->hmask - | ldr CARG4, STR:RC->hash - | ldr NODE:INS, TAB:CARG1->node - | mov TAB:RB, TAB:CARG1 - | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask - | add CARG3, CARG3, CARG3, lsl #1 - | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 - |1: - | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS. - | ldrd CARG34, NODE:INS->val - | ldr NODE:INS, NODE:INS->next - | checktp CARG2, LJ_TSTR - | cmpeq CARG1, STR:RC - | bne >4 - | checktp CARG4, LJ_TNIL - | beq >5 - |3: - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |4: // Follow hash chain. - | cmp NODE:INS, #0 - | bne <1 - | // End of hash chain: key not found, nil result. - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG1, TAB:RB->metatable - | mov CARG3, #0 // Optional clear of undef. value (during load stall). - | mvn CARG4, #~LJ_TNIL - | cmp TAB:CARG1, #0 - | beq <3 // No metatable: done. - | ldrb CARG2, TAB:CARG1->nomm - | tst CARG2, #1<vmeta_tgets - break; - case BC_TGETB: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = dst*8, RB = table*8, RC = index - | ldrd CARG12, [BASE, RB] - | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12. - | ldr CARG3, TAB:CARG1->asize - | ldr CARG4, TAB:CARG1->array - | lsl CARG2, RC, #3 - | cmp RC, CARG3 - | ldrdlo CARG34, [CARG4, CARG2] - | bhs ->vmeta_tgetb - | ins_next1 // Overwrites RB! - | checktp CARG4, LJ_TNIL - | beq >5 - |1: - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG2, TAB:CARG1->metatable - | cmp TAB:CARG2, #0 - | beq <1 // No metatable: done. - | ldrb CARG2, TAB:CARG2->nomm - | tst CARG2, #1<vmeta_tgetb - break; - - case BC_TSETV: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = src*8, RB = table*8, RC = key*8 - | ldrd TAB:CARG12, [BASE, RB] - | ldrd CARG34, [BASE, RC] - | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12. - | checktp CARG4, LJ_TISNUM // Integer key? - | ldreq CARG2, TAB:CARG1->array - | ldreq CARG4, TAB:CARG1->asize - | bne >9 - | - | add CARG2, CARG2, CARG3, lsl #3 - | cmp CARG3, CARG4 // In array part? - | ldrlo INS, [CARG2, #4] - | bhs ->vmeta_tsetv - | ins_next1 // Overwrites RB! - | checktp INS, LJ_TNIL - | ldrb INS, TAB:CARG1->marked - | ldrd CARG34, [BASE, RA] - | beq >5 - |1: - | tst INS, #LJ_GC_BLACK // isblack(table) - | strd CARG34, [CARG2] - | bne >7 - |2: - | ins_next2 - | ins_next3 - | - |5: // Check for __newindex if previous value is nil. - | ldr TAB:RA, TAB:CARG1->metatable - | cmp TAB:RA, #0 - | beq <1 // No metatable: done. - | ldrb RA, TAB:RA->nomm - | tst RA, #1<vmeta_tsetv - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG1, INS, CARG3 - | b <2 - | - |9: - | checktp CARG4, LJ_TSTR // String key? - | moveq STR:RC, CARG3 - | beq ->BC_TSETS_Z - | b ->vmeta_tsetv - break; - case BC_TSETS: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = src*8, RB = table*8, RC = str_const (~) - | ldrd CARG12, [BASE, RB] - | mvn RC, RC - | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC. - | checktab CARG2, ->vmeta_tsets1 - |->BC_TSETS_Z: - | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8 - | ldr CARG3, TAB:CARG1->hmask - | ldr CARG4, STR:RC->hash - | ldr NODE:INS, TAB:CARG1->node - | mov TAB:RB, TAB:CARG1 - | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask - | add CARG3, CARG3, CARG3, lsl #1 - | mov CARG4, #0 - | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 - | strb CARG4, TAB:RB->nomm // Clear metamethod cache. - |1: - | ldrd CARG12, NODE:INS->key - | ldr CARG4, NODE:INS->val.it - | ldr NODE:CARG3, NODE:INS->next - | checktp CARG2, LJ_TSTR - | cmpeq CARG1, STR:RC - | bne >5 - | ldrb CARG2, TAB:RB->marked - | checktp CARG4, LJ_TNIL // Key found, but nil value? - | ldrd CARG34, [BASE, RA] - | beq >4 - |2: - | tst CARG2, #LJ_GC_BLACK // isblack(table) - | strd CARG34, NODE:INS->val - | bne >7 - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | ldr TAB:CARG1, TAB:RB->metatable - | cmp TAB:CARG1, #0 - | beq <2 // No metatable: done. - | ldrb CARG1, TAB:CARG1->nomm - | tst CARG1, #1<vmeta_tsets - | - |5: // Follow hash chain. - | movs NODE:INS, NODE:CARG3 - | bne <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | ldr TAB:CARG1, TAB:RB->metatable - | mov CARG3, TMPDp - | str PC, SAVE_PC - | cmp TAB:CARG1, #0 // No metatable: continue. - | str BASE, L->base - | ldrbne CARG2, TAB:CARG1->nomm - | mov CARG1, L - | beq >6 - | tst CARG2, #1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | mvn CARG4, #~LJ_TSTR - | str STR:RC, TMPDlo - | mov CARG2, TAB:RB - | str CARG4, TMPDhi - | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Returns TValue *. - | ldr BASE, L->base - | ldrd CARG34, [BASE, RA] - | strd CARG34, [CRET1] - | b <3 // No 2nd write barrier needed. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, CARG2, CARG3 - | b <3 - break; - case BC_TSETB: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = src*8, RB = table*8, RC = index - | ldrd CARG12, [BASE, RB] - | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12. - | ldr CARG3, TAB:CARG1->asize - | ldr RB, TAB:CARG1->array - | lsl CARG2, RC, #3 - | cmp RC, CARG3 - | ldrdlo CARG34, [CARG2, RB]! - | bhs ->vmeta_tsetb - | ins_next1 // Overwrites RB! - | checktp CARG4, LJ_TNIL - | ldrb INS, TAB:CARG1->marked - | ldrd CARG34, [BASE, RA] - | beq >5 - |1: - | tst INS, #LJ_GC_BLACK // isblack(table) - | strd CARG34, [CARG2] - | bne >7 - |2: - | ins_next2 - | ins_next3 - | - |5: // Check for __newindex if previous value is nil. - | ldr TAB:RA, TAB:CARG1->metatable - | cmp TAB:RA, #0 - | beq <1 // No metatable: done. - | ldrb RA, TAB:RA->nomm - | tst RA, #1<vmeta_tsetb - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG1, INS, CARG3 - | b <2 - break; - - case BC_TSETM: - | // RA = base*8 (table at base-1), RC = num_const (start index) - | add RA, BASE, RA - |1: - | ldr RB, SAVE_MULTRES - | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table. - | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word. - | subs RB, RB, #8 - | ldr CARG4, TAB:CARG2->asize - | beq >4 // Nothing to copy? - | add CARG3, CARG1, RB, lsr #3 - | cmp CARG3, CARG4 - | ldr CARG4, TAB:CARG2->array - | add RB, RA, RB - | bhi >5 - | add INS, CARG4, CARG1, lsl #3 - | ldrb CARG1, TAB:CARG2->marked - |3: // Copy result slots to table. - | ldrd CARG34, [RA], #8 - | strd CARG34, [INS], #8 - | cmp RA, RB - | blo <3 - | tst CARG1, #LJ_GC_BLACK // isblack(table) - | bne >7 - |4: - | ins_next - | - |5: // Need to resize array part. - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | // Must not reallocate the stack. - | .IOS ldr BASE, L->base - | b <1 - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, CARG1, CARG3 - | b <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = nresults+1,) RC = extra_nargs - | ldr CARG1, SAVE_MULTRES - | decode_RC8 NARGS8:RC, INS - | add NARGS8:RC, NARGS8:RC, CARG1 - | b ->BC_CALL_Z - break; - case BC_CALL: - | decode_RC8 NARGS8:RC, INS - | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8 - |->BC_CALL_Z: - | mov RB, BASE // Save old BASE for vmeta_call. - | ldrd CARG34, [BASE, RA]! - | sub NARGS8:RC, NARGS8:RC, #8 - | add BASE, BASE, #8 - | checkfunc CARG4, ->vmeta_call - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs - | ldr CARG1, SAVE_MULTRES - | add NARGS8:RC, CARG1, RC, lsl #3 - | b ->BC_CALLT1_Z - break; - case BC_CALLT: - | lsl NARGS8:RC, RC, #3 - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - |->BC_CALLT1_Z: - | ldrd LFUNC:CARG34, [RA, BASE]! - | sub NARGS8:RC, NARGS8:RC, #8 - | add RA, RA, #8 - | checkfunc CARG4, ->vmeta_callt - | ldr PC, [BASE, FRAME_PC] - |->BC_CALLT2_Z: - | mov RB, #0 - | ldrb CARG4, LFUNC:CARG3->ffid - | tst PC, #FRAME_TYPE - | bne >7 - |1: - | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC. - | cmp NARGS8:RC, #0 - | beq >3 - |2: - | ldrd CARG12, [RA, RB] - | add INS, RB, #8 - | cmp INS, NARGS8:RC - | strd CARG12, [BASE, RB] - | mov RB, INS - | bne <2 - |3: - | cmp CARG4, #1 // (> FF_C) Calling a fast function? - | bhi >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | ldr INS, [PC, #-4] - | decode_RA8 RA, INS - | sub CARG1, BASE, RA - | ldr LFUNC:CARG1, [CARG1, #-16] - | ldr CARG1, LFUNC:CARG1->field_pc - | ldr KBASE, [CARG1, #PC2PROTO(k)] - | b <4 - | - |7: // Tailcall from a vararg function. - | eor PC, PC, #FRAME_VARG - | tst PC, #FRAME_TYPEP // Vararg frame below? - | movne CARG4, #0 // Clear ffid if no Lua function below. - | bne <1 - | sub BASE, BASE, PC - | ldr PC, [BASE, FRAME_PC] - | tst PC, #FRAME_TYPE - | movne CARG4, #0 // Clear ffid if no Lua function below. - | b <1 - break; - - case BC_ITERC: - | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1)) - | add RA, BASE, RA - | mov RB, BASE // Save old BASE for vmeta_call. - | ldrd CARG34, [RA, #-16] - | ldrd CARG12, [RA, #-8] - | add BASE, RA, #8 - | strd CARG34, [RA, #8] // Copy state. - | strd CARG12, [RA, #16] // Copy control var. - | // STALL: locked CARG34. - | ldrd LFUNC:CARG34, [RA, #-24] - | mov NARGS8:RC, #16 // Iterators get 2 arguments. - | // STALL: load CARG34. - | strd LFUNC:CARG34, [RA] // Copy callable. - | checkfunc CARG4, ->vmeta_call - | ins_call - break; - - case BC_ITERN: - | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1)) - |.if JIT - | // NYI: add hotloop, record BC_ITERN. - |.endif - | add RA, BASE, RA - | ldr TAB:RB, [RA, #-16] - | ldr CARG1, [RA, #-8] // Get index from control var. - | ldr INS, TAB:RB->asize - | ldr CARG2, TAB:RB->array - | add PC, PC, #4 - |1: // Traverse array part. - | subs RC, CARG1, INS - | add CARG3, CARG2, CARG1, lsl #3 - | bhs >5 // Index points after array part? - | ldrd CARG34, [CARG3] - | checktp CARG4, LJ_TNIL - | addeq CARG1, CARG1, #1 // Skip holes in array part. - | beq <1 - | ldrh RC, [PC, #-2] - | mvn CARG2, #~LJ_TISNUM - | strd CARG34, [RA, #8] - | add RC, PC, RC, lsl #2 - | add RB, CARG1, #1 - | strd CARG12, [RA] - | sub PC, RC, #0x20000 - | str RB, [RA, #-8] // Update control var. - |3: - | ins_next - | - |5: // Traverse hash part. - | ldr CARG4, TAB:RB->hmask - | ldr NODE:RB, TAB:RB->node - |6: - | add CARG1, RC, RC, lsl #1 - | cmp RC, CARG4 // End of iteration? Branch to ITERL+1. - | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8 - | bhi <3 - | ldrd CARG12, NODE:CARG3->val - | checktp CARG2, LJ_TNIL - | add RC, RC, #1 - | beq <6 // Skip holes in hash part. - | ldrh RB, [PC, #-2] - | add RC, RC, INS - | ldrd CARG34, NODE:CARG3->key - | str RC, [RA, #-8] // Update control var. - | strd CARG12, [RA, #8] - | add RC, PC, RB, lsl #2 - | sub PC, RC, #0x20000 - | strd CARG34, [RA] - | b <3 - break; - - case BC_ISNEXT: - | // RA = base*8, RC = target (points to ITERN) - | add RA, BASE, RA - | add RC, PC, RC, lsl #2 - | ldrd CFUNC:CARG12, [RA, #-24] - | ldr CARG3, [RA, #-12] - | ldr CARG4, [RA, #-4] - | checktp CARG2, LJ_TFUNC - | ldrbeq CARG1, CFUNC:CARG1->ffid - | checktpeq CARG3, LJ_TTAB - | checktpeq CARG4, LJ_TNIL - | cmpeq CARG1, #FF_next_N - | subeq PC, RC, #0x20000 - | bne >5 - | ins_next1 - | ins_next2 - | mov CARG1, #0 - | mvn CARG2, #0x00018000 - | strd CARG1, [RA, #-8] // Initialize control var. - |1: - | ins_next3 - |5: // Despecialize bytecode if any of the checks fail. - | mov CARG1, #BC_JMP - | mov OP, #BC_ITERC - | strb CARG1, [PC, #-4] - | sub PC, RC, #0x20000 - | strb OP, [PC] // Subsumes ins_next1. - | ins_next2 - | b <1 - break; - - case BC_VARG: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | ldr CARG1, [BASE, FRAME_PC] - | add RC, BASE, RC - | add RA, BASE, RA - | add RC, RC, #FRAME_VARG - | add CARG4, RA, RB - | sub CARG3, BASE, #8 // CARG3 = vtop - | sub RC, RC, CARG1 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | cmp RB, #0 - | sub CARG1, CARG3, RC - | beq >5 // Copy all varargs? - | sub CARG4, CARG4, #16 - |1: // Copy vararg slots to destination slots. - | cmp RC, CARG3 - | ldrdlo CARG12, [RC], #8 - | mvnhs CARG2, #~LJ_TNIL - | cmp RA, CARG4 - | strd CARG12, [RA], #8 - | blo <1 - |2: - | ins_next - | - |5: // Copy all varargs. - | ldr CARG4, L->maxstack - | cmp CARG1, #0 - | movle RB, #8 // MULTRES = (0+1)*8 - | addgt RB, CARG1, #8 - | add CARG2, RA, CARG1 - | str RB, SAVE_MULTRES - | ble <2 - | cmp CARG2, CARG4 - | bhi >7 - |6: - | ldrd CARG12, [RC], #8 - | strd CARG12, [RA], #8 - | cmp RC, CARG3 - | blo <6 - | b <2 - | - |7: // Grow stack for varargs. - | lsr CARG2, CARG1, #3 - | str RA, L->top - | mov CARG1, L - | str BASE, L->base - | sub RC, RC, BASE // Need delta, because BASE may change. - | str PC, SAVE_PC - | sub RA, RA, BASE - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->base - | add RA, BASE, RA - | add RC, BASE, RC - | sub CARG3, BASE, #8 - | b <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RC = extra results - | ldr CARG1, SAVE_MULTRES - | ldr PC, [BASE, FRAME_PC] - | add RA, BASE, RA - | add RC, CARG1, RC, lsl #3 - | b ->BC_RETM_Z - break; - - case BC_RET: - | // RA = results*8, RC = nresults+1 - | ldr PC, [BASE, FRAME_PC] - | lsl RC, RC, #3 - | add RA, BASE, RA - |->BC_RETM_Z: - | str RC, SAVE_MULTRES - |1: - | ands CARG1, PC, #FRAME_TYPE - | eor CARG2, PC, #FRAME_VARG - | bne ->BC_RETV2_Z - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return - | ldr INS, [PC, #-4] - | subs CARG4, RC, #8 - | sub CARG3, BASE, #8 - | beq >3 - |2: - | ldrd CARG12, [RA], #8 - | add BASE, BASE, #8 - | subs CARG4, CARG4, #8 - | strd CARG12, [BASE, #-16] - | bne <2 - |3: - | decode_RA8 RA, INS - | sub CARG4, CARG3, RA - | decode_RB8 RB, INS - | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC] - |5: - | cmp RB, RC // More results expected? - | bhi >6 - | mov BASE, CARG4 - | ldr CARG2, LFUNC:CARG1->field_pc - | ins_next1 - | ins_next2 - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | ins_next3 - | - |6: // Fill up results with nil. - | mvn CARG2, #~LJ_TNIL - | add BASE, BASE, #8 - | add RC, RC, #8 - | str CARG2, [BASE, #-12] - | b <5 - | - |->BC_RETV1_Z: // Non-standard return case. - | add RA, BASE, RA - |->BC_RETV2_Z: - | tst CARG2, #FRAME_TYPEP - | bne ->vm_return - | // Return from vararg function: relocate BASE down. - | sub BASE, BASE, CARG2 - | ldr PC, [BASE, FRAME_PC] - | b <1 - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RC = nresults+1 - | ldr PC, [BASE, FRAME_PC] - | lsl RC, RC, #3 - | str RC, SAVE_MULTRES - | ands CARG1, PC, #FRAME_TYPE - | eor CARG2, PC, #FRAME_VARG - | ldreq INS, [PC, #-4] - | bne ->BC_RETV1_Z - if (op == BC_RET1) { - | ldrd CARG12, [BASE, RA] - } - | sub CARG4, BASE, #8 - | decode_RA8 RA, INS - if (op == BC_RET1) { - | strd CARG12, [CARG4] - } - | sub BASE, CARG4, RA - | decode_RB8 RB, INS - | ldr LFUNC:CARG1, [BASE, FRAME_FUNC] - |5: - | cmp RB, RC - | bhi >6 - | ldr CARG2, LFUNC:CARG1->field_pc - | ins_next1 - | ins_next2 - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | ins_next3 - | - |6: // Fill up results with nil. - | sub CARG2, CARG4, #4 - | mvn CARG3, #~LJ_TNIL - | str CARG3, [CARG2, RC] - | add RC, RC, #8 - | b <5 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4] - |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12] - |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20] - |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28] - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RC = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - | ldrd CARG12, [RA, BASE]! - if (op != BC_JFORL) { - | add RC, PC, RC, lsl #2 - } - if (!vk) { - | ldrd CARG34, FOR_STOP - | checktp CARG2, LJ_TISNUM - | ldr RB, FOR_TSTEP - | bne >5 - | checktp CARG4, LJ_TISNUM - | ldr CARG4, FOR_STEP - | checktpeq RB, LJ_TISNUM - | bne ->vmeta_for - | cmp CARG4, #0 - | blt >4 - | cmp CARG1, CARG3 - } else { - | ldrd CARG34, FOR_STEP - | checktp CARG2, LJ_TISNUM - | bne >5 - | adds CARG1, CARG1, CARG3 - | ldr CARG4, FOR_STOP - if (op == BC_IFORL) { - | addvs RC, PC, #0x20000 // Overflow: prevent branch. - } else { - | bvs >2 // Overflow: do not enter mcode. - } - | cmp CARG3, #0 - | blt >4 - | cmp CARG1, CARG4 - } - |1: - if (op == BC_FORI) { - | subgt PC, RC, #0x20000 - } else if (op == BC_JFORI) { - | sub PC, RC, #0x20000 - | ldrhle RC, [PC, #-2] - } else if (op == BC_IFORL) { - | suble PC, RC, #0x20000 - } - if (vk) { - | strd CARG12, FOR_IDX - } - |2: - | ins_next1 - | ins_next2 - | strd CARG12, FOR_EXT - if (op == BC_JFORI || op == BC_JFORL) { - | ble =>BC_JLOOP - } - |3: - | ins_next3 - | - |4: // Invert check for negative step. - if (!vk) { - | cmp CARG3, CARG1 - } else { - | cmp CARG4, CARG1 - } - | b <1 - | - |5: // FP loop. - if (!vk) { - | cmnlo CARG4, #-LJ_TISNUM - | cmnlo RB, #-LJ_TISNUM - | bhs ->vmeta_for - |.if FPU - | vldr d0, FOR_IDX - | vldr d1, FOR_STOP - | cmp RB, #0 - | vstr d0, FOR_EXT - |.else - | cmp RB, #0 - | strd CARG12, FOR_EXT - | blt >8 - |.endif - } else { - |.if FPU - | vldr d0, FOR_IDX - | vldr d2, FOR_STEP - | vldr d1, FOR_STOP - | cmp CARG4, #0 - | vadd.f64 d0, d0, d2 - |.else - | cmp CARG4, #0 - | blt >8 - | bl extern __aeabi_dadd - | strd CARG12, FOR_IDX - | ldrd CARG34, FOR_STOP - | strd CARG12, FOR_EXT - |.endif - } - |6: - |.if FPU - | vcmpge.f64 d0, d1 - | vcmplt.f64 d1, d0 - | vmrs - |.else - | bl extern __aeabi_cdcmple - |.endif - if (vk) { - |.if FPU - | vstr d0, FOR_IDX - | vstr d0, FOR_EXT - |.endif - } - if (op == BC_FORI) { - | subhi PC, RC, #0x20000 - } else if (op == BC_JFORI) { - | sub PC, RC, #0x20000 - | ldrhls RC, [PC, #-2] - | bls =>BC_JLOOP - } else if (op == BC_IFORL) { - | subls PC, RC, #0x20000 - } else { - | bls =>BC_JLOOP - } - | ins_next1 - | ins_next2 - | b <3 - | - |.if not FPU - |8: // Invert check for negative step. - if (vk) { - | bl extern __aeabi_dadd - | strd CARG12, FOR_IDX - | strd CARG12, FOR_EXT - } - | mov CARG3, CARG1 - | mov CARG4, CARG2 - | ldrd CARG12, FOR_STOP - | b <6 - |.endif - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RC = target - | ldrd CARG12, [RA, BASE]! - if (op == BC_JITERL) { - | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil. - | strdne CARG12, [RA, #-8] - | bne =>BC_JLOOP - } else { - | add RC, PC, RC, lsl #2 - | // STALL: load CARG12. - | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil. - | subne PC, RC, #0x20000 // Otherwise save control var + branch. - | strdne CARG12, [RA, #-8] - } - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RC = target (loop extent) - | // Note: RA/RC is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RC = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base (ignored), RC = traceno - | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)] - | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0. - | ldr TRACE:RC, [CARG1, RC, lsl #2] - | st_vmstate CARG2 - | ldr RA, TRACE:RC->mcode - | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)] - | str L, [DISPATCH, #DISPATCH_GL(jit_L)] - | bx RA - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RC = target - | add RC, PC, RC, lsl #2 - | sub PC, RC, #0x20000 - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 - | ldr CARG1, L->maxstack - | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)] - | ldr KBASE, [PC, #-4+PC2PROTO(k)] - | cmp RA, CARG1 - | bhi ->vm_growstack_l - if (op != BC_JFUNCF) { - | ins_next1 - | ins_next2 - } - |2: - | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters. - | mvn CARG4, #~LJ_TNIL - | blo >3 - if (op == BC_JFUNCF) { - | decode_RD RC, INS - | b =>BC_JLOOP - } else { - | ins_next3 - } - | - |3: // Clear missing parameters. - | strd CARG34, [BASE, NARGS8:RC] - | add NARGS8:RC, NARGS8:RC, #8 - | b <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 - | ldr CARG1, L->maxstack - | add CARG4, BASE, RC - | add RA, RA, RC - | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC. - | add CARG2, RC, #8+FRAME_VARG - | ldr KBASE, [PC, #-4+PC2PROTO(k)] - | cmp RA, CARG1 - | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG. - | bhs ->vm_growstack_l - | ldrb RB, [PC, #-4+PC2PROTO(numparams)] - | mov RA, BASE - | mov RC, CARG4 - | cmp RB, #0 - | add BASE, CARG4, #8 - | beq >3 - | mvn CARG3, #~LJ_TNIL - |1: - | cmp RA, RC // Less args than parameters? - | ldrdlo CARG12, [RA], #8 - | movhs CARG2, CARG3 - | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC). - |2: - | subs RB, RB, #1 - | strd CARG12, [CARG4, #8]! - | bne <1 - |3: - | ins_next - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | ldr CARG4, CFUNC:CARG3->f - } else { - | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)] - } - | add CARG2, RA, NARGS8:RC - | ldr CARG1, L->maxstack - | add RC, BASE, NARGS8:RC - | str BASE, L->base - | cmp CARG2, CARG1 - | str RC, L->top - if (op == BC_FUNCCW) { - | ldr CARG2, CFUNC:CARG3->f - } - | mv_vmstate CARG3, C - | mov CARG1, L - | bhi ->vm_growstack_c // Need to grow stack. - | st_vmstate CARG3 - | blx CARG4 // (lua_State *L [, lua_CFunction f]) - | // Returns nresults. - | ldr BASE, L->base - | mv_vmstate CARG3, INTERP - | ldr CRET2, L->top - | lsl RC, CRET1, #3 - | st_vmstate CARG3 - | ldr PC, [BASE, FRAME_PC] - | sub RA, CRET2, RC // RA = L->top - nresults*8 - | b ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 0xe\n" /* Return address is in lr. */ - "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */ - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" - "\t.long .Lbegin\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */ - fcofs, CFRAME_SIZE); - for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--) /* offset r4-r11 */ - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i)); -#if LJ_ARCH_HASFPU - for (i = 15; i >= 8; i--) /* offset d8-d15 */ - fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n", - 64+2*i, 10+2*(15-i)); - fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25); /* offset r4 */ -#endif - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" - "\t.long lj_vm_ffi_call\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */ - "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */ - "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */ - "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */ - "\t.align 2\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif - break; - default: - break; - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/vm_mips.dasc b/third-party/LuaJIT-2.0.2/src/vm_mips.dasc deleted file mode 100644 index a81dbeebf7..0000000000 --- a/third-party/LuaJIT-2.0.2/src/vm_mips.dasc +++ /dev/null @@ -1,4241 +0,0 @@ -|// Low-level VM code for MIPS CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -| -|.arch mips -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -|// Don't use: r0 = 0, r26/r27 = reserved, r28 = gp, r29 = sp, r31 = ra -| -|// The following must be C callee-save (but BASE is often refetched). -|.define BASE, r16 // Base of current Lua stack frame. -|.define KBASE, r17 // Constants of current Lua function. -|.define PC, r18 // Next PC. -|.define DISPATCH, r19 // Opcode dispatch table. -|.define LREG, r20 // Register holding lua_State (also in SAVE_L). -|.define MULTRES, r21 // Size of multi-result: (nresults+1)*8. -|// NYI: r22 currently unused. -| -|.define JGL, r30 // On-trace: global_State + 32768. -| -|// Constants for type-comparisons, stores and conversions. C callee-save. -|.define TISNIL, r30 -|.define TOBIT, f30 // 2^52 + 2^51. -| -|// The following temporaries are not saved across C calls, except for RA. -|.define RA, r23 // Callee-save. -|.define RB, r8 -|.define RC, r9 -|.define RD, r10 -|.define INS, r11 -| -|.define AT, r1 // Assembler temporary. -|.define TMP0, r12 -|.define TMP1, r13 -|.define TMP2, r14 -|.define TMP3, r15 -| -|// Calling conventions. -|.define CFUNCADDR, r25 -|.define CARG1, r4 -|.define CARG2, r5 -|.define CARG3, r6 -|.define CARG4, r7 -| -|.define CRET1, r2 -|.define CRET2, r3 -| -|.define FARG1, f12 -|.define FARG2, f14 -| -|.define FRET1, f0 -|.define FRET2, f2 -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.define CFRAME_SPACE, 112 // Delta for sp. -| -|.define SAVE_ERRF, 124(sp) // 32 bit C frame info. -|.define SAVE_NRES, 120(sp) -|.define SAVE_CFRAME, 116(sp) -|.define SAVE_L, 112(sp) -|//----- 8 byte aligned, ^^^^ 16 byte register save area, owned by interpreter. -|.define SAVE_GPR_, 72 // .. 72+10*4: 32 bit GPR saves. -|.define SAVE_FPR_, 24 // .. 24+6*8: 64 bit FPR saves. -|.define SAVE_PC, 20(sp) -|.define ARG5, 16(sp) -|.define CSAVE_4, 12(sp) -|.define CSAVE_3, 8(sp) -|.define CSAVE_2, 4(sp) -|.define CSAVE_1, 0(sp) -|//----- 8 byte aligned, ^^^^ 16 byte register save area, owned by callee. -| -|.define ARG5_OFS, 16 -|.define SAVE_MULTRES, ARG5 -| -|.macro saveregs -| addiu sp, sp, -CFRAME_SPACE -| sw ra, SAVE_GPR_+9*4(sp) -| sw r30, SAVE_GPR_+8*4(sp) -| sdc1 f30, SAVE_FPR_+5*8(sp) -| sw r23, SAVE_GPR_+7*4(sp) -| sw r22, SAVE_GPR_+6*4(sp) -| sdc1 f28, SAVE_FPR_+4*8(sp) -| sw r21, SAVE_GPR_+5*4(sp) -| sw r20, SAVE_GPR_+4*4(sp) -| sdc1 f26, SAVE_FPR_+3*8(sp) -| sw r19, SAVE_GPR_+3*4(sp) -| sw r18, SAVE_GPR_+2*4(sp) -| sdc1 f24, SAVE_FPR_+2*8(sp) -| sw r17, SAVE_GPR_+1*4(sp) -| sw r16, SAVE_GPR_+0*4(sp) -| sdc1 f22, SAVE_FPR_+1*8(sp) -| sdc1 f20, SAVE_FPR_+0*8(sp) -|.endmacro -| -|.macro restoreregs_ret -| lw ra, SAVE_GPR_+9*4(sp) -| lw r30, SAVE_GPR_+8*4(sp) -| ldc1 f30, SAVE_FPR_+5*8(sp) -| lw r23, SAVE_GPR_+7*4(sp) -| lw r22, SAVE_GPR_+6*4(sp) -| ldc1 f28, SAVE_FPR_+4*8(sp) -| lw r21, SAVE_GPR_+5*4(sp) -| lw r20, SAVE_GPR_+4*4(sp) -| ldc1 f26, SAVE_FPR_+3*8(sp) -| lw r19, SAVE_GPR_+3*4(sp) -| lw r18, SAVE_GPR_+2*4(sp) -| ldc1 f24, SAVE_FPR_+2*8(sp) -| lw r17, SAVE_GPR_+1*4(sp) -| lw r16, SAVE_GPR_+0*4(sp) -| ldc1 f22, SAVE_FPR_+1*8(sp) -| ldc1 f20, SAVE_FPR_+0*8(sp) -| jr ra -| addiu sp, sp, CFRAME_SPACE -|.endmacro -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -| -|//----------------------------------------------------------------------- -| -|// Trap for not-yet-implemented parts. -|.macro NYI; .long 0xf0f0f0f0; .endmacro -| -|// Macros to mark delay slots. -|.macro ., a; a; .endmacro -|.macro ., a,b; a,b; .endmacro -|.macro ., a,b,c; a,b,c; .endmacro -| -|//----------------------------------------------------------------------- -| -|// Endian-specific defines. -|.define FRAME_PC, LJ_ENDIAN_SELECT(-4,-8) -|.define FRAME_FUNC, LJ_ENDIAN_SELECT(-8,-4) -|.define HI, LJ_ENDIAN_SELECT(4,0) -|.define LO, LJ_ENDIAN_SELECT(0,4) -|.define OFS_RD, LJ_ENDIAN_SELECT(2,0) -|.define OFS_RA, LJ_ENDIAN_SELECT(1,2) -|.define OFS_OP, LJ_ENDIAN_SELECT(0,3) -| -|// Instruction decode. -|.macro decode_OP1, dst, ins; andi dst, ins, 0xff; .endmacro -|.macro decode_OP4a, dst, ins; andi dst, ins, 0xff; .endmacro -|.macro decode_OP4b, dst; sll dst, dst, 2; .endmacro -|.macro decode_RC4a, dst, ins; srl dst, ins, 14; .endmacro -|.macro decode_RC4b, dst; andi dst, dst, 0x3fc; .endmacro -|.macro decode_RD4b, dst; sll dst, dst, 2; .endmacro -|.macro decode_RA8a, dst, ins; srl dst, ins, 5; .endmacro -|.macro decode_RA8b, dst; andi dst, dst, 0x7f8; .endmacro -|.macro decode_RB8a, dst, ins; srl dst, ins, 21; .endmacro -|.macro decode_RB8b, dst; andi dst, dst, 0x7f8; .endmacro -|.macro decode_RD8a, dst, ins; srl dst, ins, 16; .endmacro -|.macro decode_RD8b, dst; sll dst, dst, 3; .endmacro -|.macro decode_RDtoRC8, dst, src; andi dst, src, 0x7f8; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| lw INS, 0(PC) -| addiu PC, PC, 4 -|.endmacro -|// Instruction decode+dispatch. -|.macro ins_NEXT2 -| decode_OP4a TMP1, INS -| decode_OP4b TMP1 -| addu TMP0, DISPATCH, TMP1 -| decode_RD8a RD, INS -| lw AT, 0(TMP0) -| decode_RA8a RA, INS -| decode_RD8b RD -| jr AT -| decode_RA8b RA -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| lw PC, LFUNC:RB->pc -| lw INS, 0(PC) -| addiu PC, PC, 4 -| decode_OP4a TMP1, INS -| decode_RA8a RA, INS -| decode_OP4b TMP1 -| decode_RA8b RA -| addu TMP0, DISPATCH, TMP1 -| lw TMP0, 0(TMP0) -| jr TMP0 -| addu RA, RA, BASE -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| sw PC, FRAME_PC(BASE) -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|.macro branch_RD -| srl TMP0, RD, 1 -| lui AT, (-(BCBIAS_J*4 >> 16) & 65535) -| addu TMP0, TMP0, AT -| addu PC, PC, TMP0 -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -#define GG_DISP2GOT (GG_OFS(got) - GG_OFS(dispatch)) -#define DISPATCH_GOT(name) (GG_DISP2GOT + 4*LJ_GOT_##name) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro load_got, func -| lw CFUNCADDR, DISPATCH_GOT(func)(DISPATCH) -|.endmacro -|// Much faster. Sadly, there's no easy way to force the required code layout. -|// .macro call_intern, func; bal extern func; .endmacro -|.macro call_intern, func; jalr CFUNCADDR; .endmacro -|.macro call_extern; jalr CFUNCADDR; .endmacro -|.macro jmp_extern; jr CFUNCADDR; .endmacro -| -|.macro hotcheck, delta, target -| srl TMP1, PC, 1 -| andi TMP1, TMP1, 126 -| addu TMP1, TMP1, DISPATCH -| lhu TMP2, GG_DISP2HOT(TMP1) -| addiu TMP2, TMP2, -delta -| bltz TMP2, target -|. sh TMP2, GG_DISP2HOT(TMP1) -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP, ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL, ->vm_hotcall -|.endmacro -| -|// Set current VM state. Uses TMP0. -|.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro -|.macro st_vmstate; sw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp, target -| lw tmp, DISPATCH_GL(gc.grayagain)(DISPATCH) -| andi mark, mark, ~LJ_GC_BLACK & 255 // black2gray(tab) -| sw tab, DISPATCH_GL(gc.grayagain)(DISPATCH) -| sb mark, tab->marked -| b target -|. sw tmp, tab->gclist -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: TMP2 = previous base. - | andi AT, PC, FRAME_P - | beqz AT, ->cont_dispatch - |. li TMP1, LJ_TTRUE - | - | // Return from pcall or xpcall fast func. - | lw PC, FRAME_PC(TMP2) // Fetch PC of previous frame. - | move BASE, TMP2 // Restore caller base. - | // Prepending may overwrite the pcall frame, so do it at the end. - | sw TMP1, FRAME_PC(RA) // Prepend true to results. - | addiu RA, RA, -8 - | - |->vm_returnc: - | addiu RD, RD, 8 // RD = (nresults+1)*8. - | andi TMP0, PC, FRAME_TYPE - | beqz RD, ->vm_unwind_c_eh - |. li CRET1, LUA_YIELD - | beqz TMP0, ->BC_RET_Z // Handle regular return to Lua. - |. move MULTRES, RD - | - |->vm_return: - | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return - | // TMP0 = PC & FRAME_TYPE - | li TMP2, -8 - | xori AT, TMP0, FRAME_C - | and TMP2, PC, TMP2 - | bnez AT, ->vm_returnp - | subu TMP2, BASE, TMP2 // TMP2 = previous base. - | - | addiu TMP1, RD, -8 - | sw TMP2, L->base - | li_vmstate C - | lw TMP2, SAVE_NRES - | addiu BASE, BASE, -8 - | st_vmstate - | beqz TMP1, >2 - |. sll TMP2, TMP2, 3 - |1: - | addiu TMP1, TMP1, -8 - | ldc1 f0, 0(RA) - | addiu RA, RA, 8 - | sdc1 f0, 0(BASE) - | bnez TMP1, <1 - |. addiu BASE, BASE, 8 - | - |2: - | bne TMP2, RD, >6 - |3: - |. sw BASE, L->top // Store new top. - | - |->vm_leave_cp: - | lw TMP0, SAVE_CFRAME // Restore previous C frame. - | move CRET1, r0 // Ok return status for vm_pcall. - | sw TMP0, L->cframe - | - |->vm_leave_unw: - | restoreregs_ret - | - |6: - | lw TMP1, L->maxstack - | slt AT, TMP2, RD - | bnez AT, >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - |. slt AT, BASE, TMP1 - | beqz AT, >8 - |. nop - | sw TISNIL, HI(BASE) - | addiu RD, RD, 8 - | b <2 - |. addiu BASE, BASE, 8 - | - |7: // Less results wanted. - | subu TMP0, RD, TMP2 - | subu TMP0, BASE, TMP0 // Either keep top or shrink it. - | b <3 - |. movn BASE, TMP0, TMP2 // LUA_MULTRET+1 case? - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | load_got lj_state_growstack - | move MULTRES, RD - | move CARG2, TMP2 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | lw TMP2, SAVE_NRES - | lw BASE, L->top // Need the (realloced) L->top in BASE. - | move RD, MULTRES - | b <2 - |. sll TMP2, TMP2, 3 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | move sp, CARG1 - | move CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | lw L, SAVE_L - | li TMP0, ~LJ_VMST_C - | lw GL:TMP1, L->glref - | b ->vm_leave_unw - |. sw TMP0, GL:TMP1->vmstate - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | li AT, -4 - | and sp, CARG1, AT - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | lw L, SAVE_L - | lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | li TISNIL, LJ_TNIL - | lw BASE, L->base - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | mtc1 TMP3, TOBIT - | li TMP1, LJ_TFALSE - | li_vmstate INTERP - | lw PC, FRAME_PC(BASE) // Fetch PC of previous frame. - | cvt.d.s TOBIT, TOBIT - | addiu RA, BASE, -8 // Results start at BASE-8. - | addiu DISPATCH, DISPATCH, GG_G2DISP - | sw TMP1, HI(RA) // Prepend false to error message. - | st_vmstate - | b ->vm_returnc - |. li RD, 16 // 2 results: false + error message. - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | b >2 - |. li CARG2, LUA_MINSTACK - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | addu RC, BASE, RC - | subu RA, RA, BASE - | sw BASE, L->base - | addiu PC, PC, 4 // Must point after first instruction. - | sw RC, L->top - | srl CARG2, RA, 3 - |2: - | // L->base = new base, L->top = top - | load_got lj_state_growstack - | sw PC, SAVE_PC - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | lw BASE, L->base - | lw RC, L->top - | lw LFUNC:RB, FRAME_FUNC(BASE) - | subu RC, RC, BASE - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | move L, CARG1 - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | move BASE, CARG2 - | lbu TMP1, L->status - | sw L, SAVE_L - | li PC, FRAME_CP - | addiu TMP0, sp, CFRAME_RESUME - | addiu DISPATCH, DISPATCH, GG_G2DISP - | sw r0, SAVE_NRES - | sw r0, SAVE_ERRF - | sw TMP0, L->cframe - | sw r0, SAVE_CFRAME - | beqz TMP1, >3 - |. sw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | - | // Resume after yield (like a return). - | move RA, BASE - | lw BASE, L->base - | lw TMP1, L->top - | lw PC, FRAME_PC(BASE) - | lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | subu RD, TMP1, BASE - | mtc1 TMP3, TOBIT - | sb r0, L->status - | cvt.d.s TOBIT, TOBIT - | li_vmstate INTERP - | addiu RD, RD, 8 - | st_vmstate - | move MULTRES, RD - | andi TMP0, PC, FRAME_TYPE - | beqz TMP0, ->BC_RET_Z - |. li TISNIL, LJ_TNIL - | b ->vm_return - |. nop - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | sw CARG4, SAVE_ERRF - | b >1 - |. li PC, FRAME_CP - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | li PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | lw TMP1, L:CARG1->cframe - | sw CARG3, SAVE_NRES - | move L, CARG1 - | sw CARG1, SAVE_L - | move BASE, CARG2 - | sw sp, L->cframe // Add our C frame to cframe chain. - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | sw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sw TMP1, SAVE_CFRAME - | addiu DISPATCH, DISPATCH, GG_G2DISP - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | lw TMP2, L->base // TMP2 = old base (used in vmeta_call). - | lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | lw TMP1, L->top - | mtc1 TMP3, TOBIT - | addu PC, PC, BASE - | subu NARGS8:RC, TMP1, BASE - | subu PC, PC, TMP2 // PC = frame delta + frame type - | cvt.d.s TOBIT, TOBIT - | li_vmstate INTERP - | li TISNIL, LJ_TNIL - | st_vmstate - | - |->vm_call_dispatch: - | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC - | lw TMP0, FRAME_PC(BASE) - | li AT, LJ_TFUNC - | bne TMP0, AT, ->vmeta_call - |. lw LFUNC:RB, FRAME_FUNC(BASE) - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | move L, CARG1 - | lw TMP0, L:CARG1->stack - | sw CARG1, SAVE_L - | lw TMP1, L->top - | sw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | subu TMP0, TMP0, TMP1 // Compute -savestack(L, L->top). - | lw TMP1, L->cframe - | sw sp, L->cframe // Add our C frame to cframe chain. - | sw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame. - | sw r0, SAVE_ERRF // No error function. - | move CFUNCADDR, CARG4 - | jalr CARG4 // (lua_State *L, lua_CFunction func, void *ud) - |. sw TMP1, SAVE_CFRAME - | move BASE, CRET1 - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | li PC, FRAME_CP - | bnez CRET1, <3 // Else continue with the call. - |. addiu DISPATCH, DISPATCH, GG_G2DISP - | b ->vm_leave_cp // No base? Just remove C frame. - |. nop - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the - |// stack, so BASE doesn't need to be reloaded across these calls. - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8 - | lw TMP0, -16+LO(BASE) // Continuation. - | move RB, BASE - | move BASE, TMP2 // Restore caller BASE. - | lw LFUNC:TMP1, FRAME_FUNC(TMP2) - |.if FFI - | sltiu AT, TMP0, 2 - |.endif - | lw PC, -16+HI(RB) // Restore PC from [cont|PC]. - | addu TMP2, RA, RD - | lw TMP1, LFUNC:TMP1->pc - |.if FFI - | bnez AT, >1 - |.endif - |. sw TISNIL, -8+HI(TMP2) // Ensure one valid arg. - | // BASE = base, RA = resultptr, RB = meta base - | jr TMP0 // Jump to continuation. - |. lw KBASE, PC2PROTO(k)(TMP1) - | - |.if FFI - |1: - | bnez TMP0, ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - |. addiu TMP1, RB, -16 - | b ->vm_call_tail - |. subu RC, TMP1, BASE - |.endif - | - |->cont_cat: // RA = resultptr, RB = meta base - | lw INS, -4(PC) - | addiu CARG2, RB, -16 - | ldc1 f0, 0(RA) - | decode_RB8a MULTRES, INS - | decode_RA8a RA, INS - | decode_RB8b MULTRES - | decode_RA8b RA - | addu TMP1, BASE, MULTRES - | sw BASE, L->base - | subu CARG3, CARG2, TMP1 - | bne TMP1, CARG2, ->BC_CAT_Z - |. sdc1 f0, 0(CARG2) - | addu RA, BASE, RA - | b ->cont_nop - |. sdc1 f0, 0(RA) - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP0, HI(CARG3) - | - |->vmeta_tgets: - | addiu CARG2, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TTAB - | sw TAB:RB, LO(CARG2) - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv2) - | sw TMP0, HI(CARG2) - | li TMP1, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP1, HI(CARG3) - | - |->vmeta_tgetb: // TMP0 = index - | mtc1 TMP0, f0 - | cvt.d.w f0, f0 - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | sdc1 f0, 0(CARG3) - | - |->vmeta_tgetv: - |1: - | load_got lj_meta_tget - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - |. move CARG1, L - | // Returns TValue * (finished) or NULL (metamethod). - | beqz CRET1, >3 - |. addiu TMP1, BASE, -FRAME_CONT - | ldc1 f0, 0(CRET1) - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | lw BASE, L->top - | sw PC, -16+HI(BASE) // [cont|PC] - | subu PC, BASE, TMP1 - | lw LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | b ->vm_call_dispatch_f - |. li NARGS8:RC, 16 // 2 args for func(t, k). - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP0, HI(CARG3) - | - |->vmeta_tsets: - | addiu CARG2, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TTAB - | sw TAB:RB, LO(CARG2) - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv2) - | sw TMP0, HI(CARG2) - | li TMP1, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP1, HI(CARG3) - | - |->vmeta_tsetb: // TMP0 = index - | mtc1 TMP0, f0 - | cvt.d.w f0, f0 - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | sdc1 f0, 0(CARG3) - | - |->vmeta_tsetv: - |1: - | load_got lj_meta_tset - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - |. move CARG1, L - | // Returns TValue * (finished) or NULL (metamethod). - | beqz CRET1, >3 - |. ldc1 f0, 0(RA) - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | ins_next1 - | sdc1 f0, 0(CRET1) - | ins_next2 - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | addiu TMP1, BASE, -FRAME_CONT - | lw BASE, L->top - | sw PC, -16+HI(BASE) // [cont|PC] - | subu PC, BASE, TMP1 - | lw LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | sdc1 f0, 16(BASE) // Copy value to third argument. - | b ->vm_call_dispatch_f - |. li NARGS8:RC, 24 // 3 args for func(t, k, v) - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | // CARG2, CARG3 are already set by BC_ISLT/BC_ISGE/BC_ISLE/BC_ISGT. - | load_got lj_meta_comp - | addiu PC, PC, -4 - | sw BASE, L->base - | sw PC, SAVE_PC - | decode_OP1 CARG4, INS - | call_intern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - |3: - | sltiu AT, CRET1, 2 - | beqz AT, ->vmeta_binop - | negu TMP2, CRET1 - |4: - | lhu RD, OFS_RD(PC) - | addiu PC, PC, 4 - | lui TMP1, (-(BCBIAS_J*4 >> 16) & 65535) - | sll RD, RD, 2 - | addu RD, RD, TMP1 - | and RD, RD, TMP2 - | addu PC, PC, RD - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | lbu TMP1, -4+OFS_RA(PC) - | ldc1 f0, 0(RA) - | sll TMP1, TMP1, 3 - | addu TMP1, BASE, TMP1 - | b ->cont_nop - |. sdc1 f0, 0(TMP1) - | - |->cont_condt: // RA = resultptr - | lw TMP0, HI(RA) - | sltiu AT, TMP0, LJ_TISTRUECOND - | b <4 - |. negu TMP2, AT // Branch if result is true. - | - |->cont_condf: // RA = resultptr - | lw TMP0, HI(RA) - | sltiu AT, TMP0, LJ_TISTRUECOND - | b <4 - |. addiu TMP2, AT, -1 // Branch if result is false. - | - |->vmeta_equal: - | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. - | load_got lj_meta_equal - | addiu PC, PC, -4 - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |. nop - | - |->vmeta_equal_cd: - |.if FFI - | load_got lj_meta_equal_cd - | move CARG2, INS - | addiu PC, PC, -4 - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_equal_cd // (lua_State *L, BCIns op) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |. nop - |.endif - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_unm: - | move CARG4, CARG3 - | - |->vmeta_arith: - | load_got lj_meta_arith - | decode_OP1 TMP0, INS - | sw BASE, L->base - | sw PC, SAVE_PC - | move CARG2, RA - | sw TMP0, ARG5 - | call_intern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - |. move CARG1, L - | // Returns NULL (finished) or TValue * (metamethod). - | beqz CRET1, ->cont_nop - |. nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | subu TMP1, CRET1, BASE - | sw PC, -16+HI(CRET1) // [cont|PC] - | move TMP2, BASE - | addiu PC, TMP1, FRAME_CONT - | move BASE, CRET1 - | b ->vm_call_dispatch - |. li NARGS8:RC, 16 // 2 args for func(o1, o2). - | - |->vmeta_len: - | // CARG2 already set by BC_LEN. -#if LJ_52 - | move MULTRES, CARG1 -#endif - | load_got lj_meta_len - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_len // (lua_State *L, TValue *o) - |. move CARG1, L - | // Returns NULL (retry) or TValue * (metamethod base). -#if LJ_52 - | bnez CRET1, ->vmeta_binop // Binop call for compatibility. - |. nop - | b ->BC_LEN_Z - |. move CARG1, MULTRES -#else - | b ->vmeta_binop // Binop call for compatibility. - |. nop -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // TMP2 = old base, BASE = new base, RC = nargs*8 - | load_got lj_meta_call - | sw TMP2, L->base // This is the callers base! - | addiu CARG2, BASE, -8 - | sw PC, SAVE_PC - | addu CARG3, BASE, RC - | move MULTRES, NARGS8:RC - | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - |. move CARG1, L - | lw LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | addiu NARGS8:RC, MULTRES, 8 // Got one more argument now. - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | load_got lj_meta_call - | sw BASE, L->base - | addiu CARG2, RA, -8 - | sw PC, SAVE_PC - | addu CARG3, RA, RC - | move MULTRES, NARGS8:RC - | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - |. move CARG1, L - | lw TMP1, FRAME_PC(BASE) - | lw LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here. - | b ->BC_CALLT_Z - |. addiu NARGS8:RC, MULTRES, 8 // Got one more argument now. - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | load_got lj_meta_for - | sw BASE, L->base - | move CARG2, RA - | sw PC, SAVE_PC - | move MULTRES, INS - | call_intern lj_meta_for // (lua_State *L, TValue *base) - |. move CARG1, L - |.if JIT - | decode_OP1 TMP0, MULTRES - | li AT, BC_JFORI - |.endif - | decode_RA8a RA, MULTRES - | decode_RD8a RD, MULTRES - | decode_RA8b RA - |.if JIT - | beq TMP0, AT, =>BC_JFORI - |. decode_RD8b RD - | b =>BC_FORI - |. nop - |.else - | b =>BC_FORI - |. decode_RD8b RD - |.endif - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | beqz NARGS8:RC, ->fff_fallback - |. lw CARG3, HI(BASE) - | lw CARG1, LO(BASE) - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | sltiu AT, NARGS8:RC, 16 - | lw CARG3, HI(BASE) - | bnez AT, ->fff_fallback - |. lw CARG4, 8+HI(BASE) - | lw CARG1, LO(BASE) - | lw CARG2, 8+LO(BASE) - |.endmacro - | - |.macro .ffunc_n, name // Caveat: has delay slot! - |->ff_ .. name: - | lw CARG3, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. ldc1 FARG1, 0(BASE) - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name // Caveat: has delay slot! - |->ff_ .. name: - | sltiu AT, NARGS8:RC, 16 - | lw CARG3, HI(BASE) - | bnez AT, ->fff_fallback - |. lw CARG4, 8+HI(BASE) - | ldc1 FARG1, 0(BASE) - | ldc1 FARG2, 8(BASE) - | sltiu TMP0, CARG3, LJ_TISNUM - | sltiu TMP1, CARG4, LJ_TISNUM - | and TMP0, TMP0, TMP1 - | beqz TMP0, ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1 and has delay slot! - |.macro ffgccheck - | lw TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | lw TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | subu AT, TMP0, TMP1 - | bgezal AT, ->fff_gcstep - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | sltiu AT, CARG3, LJ_TISTRUECOND - | beqz AT, ->fff_fallback - |. addiu RA, BASE, -8 - | lw PC, FRAME_PC(BASE) - | addiu RD, NARGS8:RC, 8 // Compute (nresults+1)*8. - | addu TMP2, RA, NARGS8:RC - | sw CARG3, HI(RA) - | addiu TMP1, BASE, 8 - | beq BASE, TMP2, ->fff_res // Done if exactly 1 argument. - |. sw CARG1, LO(RA) - |1: - | ldc1 f0, 0(TMP1) - | sdc1 f0, -8(TMP1) - | bne TMP1, TMP2, <1 - |. addiu TMP1, TMP1, 8 - | b ->fff_res - |. nop - | - |.ffunc type - | lw CARG3, HI(BASE) - | li TMP1, LJ_TISNUM - | beqz NARGS8:RC, ->fff_fallback - |. sltiu TMP0, CARG3, LJ_TISNUM - | movz TMP1, CARG3, TMP0 - | not TMP1, TMP1 - | sll TMP1, TMP1, 3 - | addu TMP1, CFUNC:RB, TMP1 - | b ->fff_resn - |. ldc1 FRET1, CFUNC:TMP1->upvalue - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | li AT, LJ_TTAB - | bne CARG3, AT, >6 - |. li AT, LJ_TUDATA - |1: // Field metatable must be at same offset for GCtab and GCudata! - | lw TAB:CARG1, TAB:CARG1->metatable - |2: - | lw STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH) - | beqz TAB:CARG1, ->fff_restv - |. li CARG3, LJ_TNIL - | lw TMP0, TAB:CARG1->hmask - | li CARG3, LJ_TTAB // Use metatable as default result. - | lw TMP1, STR:RC->hash - | lw NODE:TMP2, TAB:CARG1->node - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | addu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - | li AT, LJ_TSTR - |3: // Rearranged logic, because we expect _not_ to find the key. - | lw CARG4, offsetof(Node, key)+HI(NODE:TMP2) - | lw TMP0, offsetof(Node, key)+LO(NODE:TMP2) - | lw NODE:TMP3, NODE:TMP2->next - | bne CARG4, AT, >4 - |. lw CARG2, offsetof(Node, val)+HI(NODE:TMP2) - | beq TMP0, STR:RC, >5 - |. lw TMP1, offsetof(Node, val)+LO(NODE:TMP2) - |4: - | beqz NODE:TMP3, ->fff_restv // Not found, keep default result. - |. move NODE:TMP2, NODE:TMP3 - | b <3 - |. nop - |5: - | beq CARG2, TISNIL, ->fff_restv // Ditto for nil value. - |. nop - | move CARG3, CARG2 // Return value of mt.__metatable. - | b ->fff_restv - |. move CARG1, TMP1 - | - |6: - | beq CARG3, AT, <1 - |. sltiu TMP0, CARG3, LJ_TISNUM - | li TMP1, LJ_TISNUM - | movz TMP1, CARG3, TMP0 - | not TMP1, TMP1 - | sll TMP1, TMP1, 2 - | addu TMP1, DISPATCH, TMP1 - | b <2 - |. lw TAB:CARG1, DISPATCH_GL(gcroot[GCROOT_BASEMT])(TMP1) - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | li AT, LJ_TTAB - | bne CARG3, AT, ->fff_fallback - |. addiu CARG4, CARG4, -LJ_TTAB - | lw TAB:TMP1, TAB:CARG1->metatable - | lbu TMP3, TAB:CARG1->marked - | or AT, CARG4, TAB:TMP1 - | bnez AT, ->fff_fallback - |. andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | beqz AT, ->fff_restv - |. sw TAB:CARG2, TAB:CARG1->metatable - | barrierback TAB:CARG1, TMP3, TMP0, ->fff_restv - | - |.ffunc rawget - | lw CARG4, HI(BASE) - | sltiu AT, NARGS8:RC, 16 - | lw TAB:CARG2, LO(BASE) - | load_got lj_tab_get - | addiu CARG4, CARG4, -LJ_TTAB - | or AT, AT, CARG4 - | bnez AT, ->fff_fallback - | addiu CARG3, BASE, 8 - | call_intern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - |. move CARG1, L - | // Returns cTValue *. - | b ->fff_resn - |. ldc1 FRET1, 0(CRET1) - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | lw CARG1, HI(BASE) - | xori AT, NARGS8:RC, 8 - | sltiu CARG1, CARG1, LJ_TISNUM - | movn CARG1, r0, AT - | beqz CARG1, ->fff_fallback // Exactly one number argument. - |. ldc1 FRET1, 0(BASE) - | b ->fff_resn - |. nop - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | li AT, LJ_TSTR - | // A __tostring method in the string base metatable is ignored. - | beq CARG3, AT, ->fff_restv // String key? - | // Handle numbers inline, unless a number base metatable is present. - |. lw TMP1, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH) - | sltiu TMP0, CARG3, LJ_TISNUM - | sltiu TMP1, TMP1, 1 - | and TMP0, TMP0, TMP1 - | beqz TMP0, ->fff_fallback - |. sw BASE, L->base // Add frame since C call can throw. - | ffgccheck - |. sw PC, SAVE_PC // Redundant (but a defined value). - | load_got lj_str_fromnum - | move CARG1, L - | call_intern lj_str_fromnum // (lua_State *L, lua_Number *np) - |. move CARG2, BASE - | // Returns GCstr *. - | li CARG3, LJ_TSTR - | b ->fff_restv - |. move CARG1, CRET1 - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc next - | lw CARG1, HI(BASE) - | lw TAB:CARG2, LO(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. addu TMP2, BASE, NARGS8:RC - | li AT, LJ_TTAB - | sw TISNIL, HI(TMP2) // Set missing 2nd arg to nil. - | bne CARG1, AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) - | load_got lj_tab_next - | sw BASE, L->base // Add frame since C call can throw. - | sw BASE, L->top // Dummy frame length is ok. - | addiu CARG3, BASE, 8 - | sw PC, SAVE_PC - | call_intern lj_tab_next // (lua_State *L, GCtab *t, TValue *key) - |. move CARG1, L - | // Returns 0 at end of traversal. - | beqz CRET1, ->fff_restv // End of traversal: return nil. - |. li CARG3, LJ_TNIL - | ldc1 f0, 8(BASE) // Copy key and value to results. - | addiu RA, BASE, -8 - | ldc1 f2, 16(BASE) - | li RD, (2+1)*8 - | sdc1 f0, 0(RA) - | b ->fff_res - |. sdc1 f2, 8(RA) - | - |.ffunc_1 pairs - | li AT, LJ_TTAB - | bne CARG3, AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) -#if LJ_52 - | lw TAB:TMP2, TAB:CARG1->metatable - | ldc1 f0, CFUNC:RB->upvalue[0] - | bnez TAB:TMP2, ->fff_fallback -#else - | ldc1 f0, CFUNC:RB->upvalue[0] -#endif - |. addiu RA, BASE, -8 - | sw TISNIL, 8+HI(BASE) - | li RD, (3+1)*8 - | b ->fff_res - |. sdc1 f0, 0(RA) - | - |.ffunc ipairs_aux - | sltiu AT, NARGS8:RC, 16 - | lw CARG3, HI(BASE) - | lw TAB:CARG1, LO(BASE) - | lw CARG4, 8+HI(BASE) - | bnez AT, ->fff_fallback - |. ldc1 FARG2, 8(BASE) - | addiu CARG3, CARG3, -LJ_TTAB - | sltiu AT, CARG4, LJ_TISNUM - | li TMP0, 1 - | movn AT, r0, CARG3 - | mtc1 TMP0, FARG1 - | beqz AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) - | cvt.w.d FRET1, FARG2 - | cvt.d.w FARG1, FARG1 - | lw TMP0, TAB:CARG1->asize - | lw TMP1, TAB:CARG1->array - | mfc1 TMP2, FRET1 - | addiu RA, BASE, -8 - | add.d FARG2, FARG2, FARG1 - | addiu TMP2, TMP2, 1 - | sltu AT, TMP2, TMP0 - | sll TMP3, TMP2, 3 - | addu TMP3, TMP1, TMP3 - | beqz AT, >2 // Not in array part? - |. sdc1 FARG2, 0(RA) - | lw TMP2, HI(TMP3) - | ldc1 f0, 0(TMP3) - |1: - | beq TMP2, TISNIL, ->fff_res // End of iteration, return 0 results. - |. li RD, (0+1)*8 - | li RD, (2+1)*8 - | b ->fff_res - |. sdc1 f0, 8(RA) - |2: // Check for empty hash part first. Otherwise call C function. - | lw TMP0, TAB:CARG1->hmask - | load_got lj_tab_getinth - | beqz TMP0, ->fff_res - |. li RD, (0+1)*8 - | call_intern lj_tab_getinth // (GCtab *t, int32_t key) - |. move CARG2, TMP2 - | // Returns cTValue * or NULL. - | beqz CRET1, ->fff_res - |. li RD, (0+1)*8 - | lw TMP2, HI(CRET1) - | b <1 - |. ldc1 f0, 0(CRET1) - | - |.ffunc_1 ipairs - | li AT, LJ_TTAB - | bne CARG3, AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) -#if LJ_52 - | lw TAB:TMP2, TAB:CARG1->metatable - | ldc1 f0, CFUNC:RB->upvalue[0] - | bnez TAB:TMP2, ->fff_fallback -#else - | ldc1 f0, CFUNC:RB->upvalue[0] -#endif - |. addiu RA, BASE, -8 - | sw r0, 8+HI(BASE) - | sw r0, 8+LO(BASE) - | li RD, (3+1)*8 - | b ->fff_res - |. sdc1 f0, 0(RA) - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | beqz NARGS8:RC, ->fff_fallback - | move TMP2, BASE - | addiu BASE, BASE, 8 - | // Remember active hook before pcall. - | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT - | andi TMP3, TMP3, 1 - | addiu PC, TMP3, 8+FRAME_PCALL - | b ->vm_call_dispatch - |. addiu NARGS8:RC, NARGS8:RC, -8 - | - |.ffunc xpcall - | sltiu AT, NARGS8:RC, 16 - | lw CARG4, 8+HI(BASE) - | bnez AT, ->fff_fallback - |. ldc1 FARG2, 8(BASE) - | ldc1 FARG1, 0(BASE) - | lbu TMP1, DISPATCH_GL(hookmask)(DISPATCH) - | li AT, LJ_TFUNC - | move TMP2, BASE - | bne CARG4, AT, ->fff_fallback // Traceback must be a function. - | addiu BASE, BASE, 16 - | // Remember active hook before pcall. - | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT - | sdc1 FARG2, 0(TMP2) // Swap function and traceback. - | andi TMP3, TMP3, 1 - | sdc1 FARG1, 8(TMP2) - | addiu PC, TMP3, 16+FRAME_PCALL - | b ->vm_call_dispatch - |. addiu NARGS8:RC, NARGS8:RC, -16 - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | li AT, LJ_TTHREAD - | bne CARG3, AT, ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | lw L:CARG1, CFUNC:RB->upvalue[0].gcr - |.endif - | lbu TMP0, L:CARG1->status - | lw TMP1, L:CARG1->cframe - | lw CARG2, L:CARG1->top - | lw TMP2, L:CARG1->base - | addiu TMP3, TMP0, -LUA_YIELD - | bgtz TMP3, ->fff_fallback // st > LUA_YIELD? - |. xor TMP2, TMP2, CARG2 - | bnez TMP1, ->fff_fallback // cframe != 0? - |. or AT, TMP2, TMP0 - | lw TMP0, L:CARG1->maxstack - | beqz AT, ->fff_fallback // base == top && st == 0? - |. lw PC, FRAME_PC(BASE) - | addu TMP2, CARG2, NARGS8:RC - | sltu AT, TMP0, TMP2 - | bnez AT, ->fff_fallback // Stack overflow? - |. sw PC, SAVE_PC - | sw BASE, L->base - |1: - |.if resume - | addiu BASE, BASE, 8 // Keep resumed thread in stack for GC. - | addiu NARGS8:RC, NARGS8:RC, -8 - | addiu TMP2, TMP2, -8 - |.endif - | sw TMP2, L:CARG1->top - | addu TMP1, BASE, NARGS8:RC - | move CARG3, CARG2 - | sw BASE, L->top - |2: // Move args to coroutine. - | ldc1 f0, 0(BASE) - | sltu AT, BASE, TMP1 - | beqz AT, >3 - |. addiu BASE, BASE, 8 - | sdc1 f0, 0(CARG3) - | b <2 - |. addiu CARG3, CARG3, 8 - |3: - | bal ->vm_resume // (lua_State *L, TValue *base, 0, 0) - |. move L:RA, L:CARG1 - | // Returns thread status. - |4: - | lw TMP2, L:RA->base - | sltiu AT, CRET1, LUA_YIELD+1 - | lw TMP3, L:RA->top - | li_vmstate INTERP - | lw BASE, L->base - | st_vmstate - | beqz AT, >8 - |. subu RD, TMP3, TMP2 - | lw TMP0, L->maxstack - | beqz RD, >6 // No results? - |. addu TMP1, BASE, RD - | sltu AT, TMP0, TMP1 - | bnez AT, >9 // Need to grow stack? - |. addu TMP3, TMP2, RD - | sw TMP2, L:RA->top // Clear coroutine stack. - | move TMP1, BASE - |5: // Move results from coroutine. - | ldc1 f0, 0(TMP2) - | addiu TMP2, TMP2, 8 - | sltu AT, TMP2, TMP3 - | sdc1 f0, 0(TMP1) - | bnez AT, <5 - |. addiu TMP1, TMP1, 8 - |6: - | andi TMP0, PC, FRAME_TYPE - |.if resume - | li TMP1, LJ_TTRUE - | addiu RA, BASE, -8 - | sw TMP1, -8+HI(BASE) // Prepend true to results. - | addiu RD, RD, 16 - |.else - | move RA, BASE - | addiu RD, RD, 8 - |.endif - |7: - | sw PC, SAVE_PC - | beqz TMP0, ->BC_RET_Z - |. move MULTRES, RD - | b ->vm_return - |. nop - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | addiu TMP3, TMP3, -8 - | li TMP1, LJ_TFALSE - | ldc1 f0, 0(TMP3) - | sw TMP3, L:RA->top // Remove error from coroutine stack. - | li RD, (2+1)*8 - | sw TMP1, -8+HI(BASE) // Prepend false to results. - | addiu RA, BASE, -8 - | sdc1 f0, 0(BASE) // Copy error message. - | b <7 - |. andi TMP0, PC, FRAME_TYPE - |.else - | load_got lj_ffh_coroutine_wrap_err - | move CARG2, L:RA - | call_intern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - |. move CARG1, L - |.endif - | - |9: // Handle stack expansion on return from yield. - | load_got lj_state_growstack - | srl CARG2, RD, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | b <4 - |. li CRET1, 0 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | lw TMP0, L->cframe - | addu TMP1, BASE, NARGS8:RC - | sw BASE, L->base - | andi TMP0, TMP0, CFRAME_RESUME - | sw TMP1, L->top - | beqz TMP0, ->fff_fallback - |. li CRET1, LUA_YIELD - | sw r0, L->cframe - | b ->vm_leave_unw - |. sb CRET1, L->status - | - |//-- Math library ------------------------------------------------------- - | - |.ffunc_n math_abs - |. abs.d FRET1, FARG1 - |->fff_resn: - | lw PC, FRAME_PC(BASE) - | addiu RA, BASE, -8 - | b ->fff_res1 - |. sdc1 FRET1, -8(BASE) - | - |->fff_restv: - | // CARG3/CARG1 = TValue result. - | lw PC, FRAME_PC(BASE) - | sw CARG3, -8+HI(BASE) - | addiu RA, BASE, -8 - | sw CARG1, -8+LO(BASE) - |->fff_res1: - | // RA = results, PC = return. - | li RD, (1+1)*8 - |->fff_res: - | // RA = results, RD = (nresults+1)*8, PC = return. - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->vm_return - |. move MULTRES, RD - | lw INS, -4(PC) - | decode_RB8a RB, INS - | decode_RB8b RB - |5: - | sltu AT, RD, RB - | bnez AT, >6 // More results expected? - |. decode_RA8a TMP0, INS - | decode_RA8b TMP0 - | ins_next1 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | subu BASE, RA, TMP0 - | ins_next2 - | - |6: // Fill up results with nil. - | addu TMP1, RA, RD - | addiu RD, RD, 8 - | b <5 - |. sw TISNIL, -8+HI(TMP1) - | - |.macro math_extern, func - |->ff_math_ .. func: - | lw CARG3, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. load_got func - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->fff_fallback - |. nop - | call_extern - |. ldc1 FARG1, 0(BASE) - | b ->fff_resn - |. nop - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nn math_ .. func - |. load_got func - | call_extern - |. nop - | b ->fff_resn - |. nop - |.endmacro - | - |.macro math_round, func - | .ffunc_n math_ .. func - |. nop - | bal ->vm_ .. func - |. nop - | b ->fff_resn - |. nop - |.endmacro - | - | math_round floor - | math_round ceil - | - |.ffunc math_log - | lw CARG3, HI(BASE) - | li AT, 8 - | bne NARGS8:RC, AT, ->fff_fallback // Exactly 1 argument. - |. load_got log - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->fff_fallback - |. nop - | call_extern - |. ldc1 FARG1, 0(BASE) - | b ->fff_resn - |. nop - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.ffunc_n math_sqrt - |. sqrt.d FRET1, FARG1 - | b ->fff_resn - |. nop - | - |->ff_math_deg: - |.ffunc_n math_rad - |. ldc1 FARG2, CFUNC:RB->upvalue[0] - | b ->fff_resn - |. mul.d FRET1, FARG1, FARG2 - | - |.ffunc_nn math_ldexp - | cvt.w.d FARG2, FARG2 - | load_got ldexp - | mfc1 CARG3, FARG2 - | call_extern - |. nop - | b ->fff_resn - |. nop - | - |.ffunc_n math_frexp - | load_got frexp - | lw PC, FRAME_PC(BASE) - | call_extern - |. addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | lw TMP1, DISPATCH_GL(tmptv)(DISPATCH) - | addiu RA, BASE, -8 - | mtc1 TMP1, FARG2 - | sdc1 FRET1, 0(RA) - | cvt.d.w FARG2, FARG2 - | sdc1 FARG2, 8(RA) - | b ->fff_res - |. li RD, (2+1)*8 - | - |.ffunc_n math_modf - | load_got modf - | lw PC, FRAME_PC(BASE) - | call_extern - |. addiu CARG3, BASE, -8 - | addiu RA, BASE, -8 - | sdc1 FRET1, 0(BASE) - | b ->fff_res - |. li RD, (2+1)*8 - | - |.macro math_minmax, name, ismax - |->ff_ .. name: - | lw CARG3, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. ldc1 FRET1, 0(BASE) - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->fff_fallback - |. addu TMP2, BASE, NARGS8:RC - | addiu TMP1, BASE, 8 - | beq TMP1, TMP2, ->fff_resn - |1: - |. lw CARG3, HI(TMP1) - | ldc1 FARG1, 0(TMP1) - | addiu TMP1, TMP1, 8 - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->fff_fallback - |.if ismax - |. c.olt.d FARG1, FRET1 - |.else - |. c.olt.d FRET1, FARG1 - |.endif - | bne TMP1, TMP2, <1 - |. movf.d FRET1, FARG1 - | b ->fff_resn - |. nop - |.endmacro - | - | math_minmax math_min, 0 - | math_minmax math_max, 1 - | - |//-- String library ----------------------------------------------------- - | - |.ffunc_1 string_len - | li AT, LJ_TSTR - | bne CARG3, AT, ->fff_fallback - |. nop - | b ->fff_resi - |. lw CRET1, STR:CARG1->len - | - |.ffunc string_byte // Only handle the 1-arg case here. - | lw CARG3, HI(BASE) - | lw STR:CARG1, LO(BASE) - | xori AT, NARGS8:RC, 8 - | addiu CARG3, CARG3, -LJ_TSTR - | or AT, AT, CARG3 - | bnez AT, ->fff_fallback // Need exactly 1 string argument. - |. nop - | lw TMP0, STR:CARG1->len - | lbu TMP1, STR:CARG1[1] // Access is always ok (NUL at end). - | addiu RA, BASE, -8 - | sltu RD, r0, TMP0 - | mtc1 TMP1, f0 - | addiu RD, RD, 1 - | cvt.d.w f0, f0 - | lw PC, FRAME_PC(BASE) - | sll RD, RD, 3 // RD = ((str->len != 0)+1)*8 - | b ->fff_res - |. sdc1 f0, 0(RA) - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | lw CARG3, HI(BASE) - | ldc1 FARG1, 0(BASE) - | li AT, 8 - | bne NARGS8:RC, AT, ->fff_fallback // Exactly 1 argument. - |. sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->fff_fallback - |. li CARG3, 1 - | cvt.w.d FARG1, FARG1 - | addiu CARG2, sp, ARG5_OFS - | sltiu AT, TMP0, 256 - | mfc1 TMP0, FARG1 - | beqz AT, ->fff_fallback - |. sw TMP0, ARG5 - |->fff_newstr: - | load_got lj_str_new - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_str_new // (lua_State *L, char *str, size_t l) - |. move CARG1, L - | // Returns GCstr *. - | lw BASE, L->base - | move CARG1, CRET1 - | b ->fff_restv - |. li CARG3, LJ_TSTR - | - |.ffunc string_sub - | ffgccheck - | addiu AT, NARGS8:RC, -16 - | lw CARG3, 16+HI(BASE) - | ldc1 f0, 16(BASE) - | lw TMP0, HI(BASE) - | lw STR:CARG1, LO(BASE) - | bltz AT, ->fff_fallback - | lw CARG2, 8+HI(BASE) - | ldc1 f2, 8(BASE) - | beqz AT, >1 - |. li CARG4, -1 - | cvt.w.d f0, f0 - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->fff_fallback - |. mfc1 CARG4, f0 - |1: - | sltiu AT, CARG2, LJ_TISNUM - | beqz AT, ->fff_fallback - |. li AT, LJ_TSTR - | cvt.w.d f2, f2 - | bne TMP0, AT, ->fff_fallback - |. lw CARG2, STR:CARG1->len - | mfc1 CARG3, f2 - | // STR:CARG1 = str, CARG2 = str->len, CARG3 = start, CARG4 = end - | slt AT, CARG4, r0 - | addiu TMP0, CARG2, 1 - | addu TMP1, CARG4, TMP0 - | slt TMP3, CARG3, r0 - | movn CARG4, TMP1, AT // if (end < 0) end += len+1 - | addu TMP1, CARG3, TMP0 - | movn CARG3, TMP1, TMP3 // if (start < 0) start += len+1 - | li TMP2, 1 - | slt AT, CARG4, r0 - | slt TMP3, r0, CARG3 - | movn CARG4, r0, AT // if (end < 0) end = 0 - | movz CARG3, TMP2, TMP3 // if (start < 1) start = 1 - | slt AT, CARG2, CARG4 - | movn CARG4, CARG2, AT // if (end > len) end = len - | addu CARG2, STR:CARG1, CARG3 - | subu CARG3, CARG4, CARG3 // len = end - start - | addiu CARG2, CARG2, sizeof(GCstr)-1 - | bgez CARG3, ->fff_newstr - |. addiu CARG3, CARG3, 1 // len++ - |->fff_emptystr: // Return empty string. - | addiu STR:CARG1, DISPATCH, DISPATCH_GL(strempty) - | b ->fff_restv - |. li CARG3, LJ_TSTR - | - |.ffunc string_rep // Only handle the 1-char case inline. - | ffgccheck - | lw TMP0, HI(BASE) - | addiu AT, NARGS8:RC, -16 // Exactly 2 arguments. - | lw CARG4, 8+HI(BASE) - | lw STR:CARG1, LO(BASE) - | addiu TMP0, TMP0, -LJ_TSTR - | ldc1 f0, 8(BASE) - | or AT, AT, TMP0 - | bnez AT, ->fff_fallback - |. sltiu AT, CARG4, LJ_TISNUM - | cvt.w.d f0, f0 - | beqz AT, ->fff_fallback - |. lw TMP0, STR:CARG1->len - | mfc1 CARG3, f0 - | lw TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | li AT, 1 - | blez CARG3, ->fff_emptystr // Count <= 0? - |. sltu AT, AT, TMP0 - | beqz TMP0, ->fff_emptystr // Zero length string? - |. sltu TMP0, TMP1, CARG3 - | or AT, AT, TMP0 - | lw CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | bnez AT, ->fff_fallback // Fallback for > 1-char strings. - |. lbu TMP0, STR:CARG1[1] - | addu TMP2, CARG2, CARG3 - |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?). - | addiu TMP2, TMP2, -1 - | sltu AT, CARG2, TMP2 - | bnez AT, <1 - |. sb TMP0, 0(TMP2) - | b ->fff_newstr - |. nop - | - |.ffunc string_reverse - | ffgccheck - | lw CARG3, HI(BASE) - | lw STR:CARG1, LO(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. li AT, LJ_TSTR - | bne CARG3, AT, ->fff_fallback - |. lw TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | lw CARG3, STR:CARG1->len - | addiu CARG1, STR:CARG1, #STR - | lw CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | sltu AT, TMP1, CARG3 - | bnez AT, ->fff_fallback - |. addu TMP3, CARG1, CARG3 - | addu CARG4, CARG2, CARG3 - |1: // Reverse string copy. - | lbu TMP1, 0(CARG1) - | sltu AT, CARG1, TMP3 - | beqz AT, ->fff_newstr - |. addiu CARG1, CARG1, 1 - | addiu CARG4, CARG4, -1 - | b <1 - | sb TMP1, 0(CARG4) - | - |.macro ffstring_case, name, lo - | .ffunc name - | ffgccheck - | lw CARG3, HI(BASE) - | lw STR:CARG1, LO(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. li AT, LJ_TSTR - | bne CARG3, AT, ->fff_fallback - |. lw TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | lw CARG3, STR:CARG1->len - | addiu CARG1, STR:CARG1, #STR - | lw CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | sltu AT, TMP1, CARG3 - | bnez AT, ->fff_fallback - |. addu TMP3, CARG1, CARG3 - | move CARG4, CARG2 - |1: // ASCII case conversion. - | lbu TMP1, 0(CARG1) - | sltu AT, CARG1, TMP3 - | beqz AT, ->fff_newstr - |. addiu TMP0, TMP1, -lo - | xori TMP2, TMP1, 0x20 - | sltiu AT, TMP0, 26 - | movn TMP1, TMP2, AT - | addiu CARG1, CARG1, 1 - | sb TMP1, 0(CARG4) - | b <1 - |. addiu CARG4, CARG4, 1 - |.endmacro - | - |ffstring_case string_lower, 65 - |ffstring_case string_upper, 97 - | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | li AT, LJ_TTAB - | bne CARG3, AT, ->fff_fallback - |. load_got lj_tab_len - | call_intern lj_tab_len // (GCtab *t) - |. nop - | // Returns uint32_t (but less than 2^31). - | b ->fff_resi - |. nop - | - |//-- Bit library -------------------------------------------------------- - | - |.macro .ffunc_bit, name - | .ffunc_n bit_..name - |. add.d FARG1, FARG1, TOBIT - | mfc1 CRET1, FARG1 - |.endmacro - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | addiu TMP1, BASE, 8 - | addu TMP2, BASE, NARGS8:RC - |1: - | lw CARG4, HI(TMP1) - | beq TMP1, TMP2, ->fff_resi - |. ldc1 FARG1, 0(TMP1) - | sltiu AT, CARG4, LJ_TISNUM - | beqz AT, ->fff_fallback - | add.d FARG1, FARG1, TOBIT - | mfc1 CARG2, FARG1 - | ins CRET1, CRET1, CARG2 - | b <1 - |. addiu TMP1, TMP1, 8 - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, or - |.ffunc_bit_op bxor, xor - | - |.ffunc_bit bswap - | srl TMP0, CRET1, 24 - | srl TMP2, CRET1, 8 - | sll TMP1, CRET1, 24 - | andi TMP2, TMP2, 0xff00 - | or TMP0, TMP0, TMP1 - | andi CRET1, CRET1, 0xff00 - | or TMP0, TMP0, TMP2 - | sll CRET1, CRET1, 8 - | b ->fff_resi - |. or CRET1, TMP0, CRET1 - | - |.ffunc_bit bnot - | b ->fff_resi - |. not CRET1, CRET1 - | - |.macro .ffunc_bit_sh, name, ins, shmod - | .ffunc_nn bit_..name - |. add.d FARG1, FARG1, TOBIT - | add.d FARG2, FARG2, TOBIT - | mfc1 CARG1, FARG1 - | mfc1 CARG2, FARG2 - |.if shmod == 1 - | li AT, 32 - | subu TMP0, AT, CARG2 - | sllv CARG2, CARG1, CARG2 - | srlv CARG1, CARG1, TMP0 - |.elif shmod == 2 - | li AT, 32 - | subu TMP0, AT, CARG2 - | srlv CARG2, CARG1, CARG2 - | sllv CARG1, CARG1, TMP0 - |.endif - | b ->fff_resi - |. ins CRET1, CARG1, CARG2 - |.endmacro - | - |.ffunc_bit_sh lshift, sllv, 0 - |.ffunc_bit_sh rshift, srlv, 0 - |.ffunc_bit_sh arshift, srav, 0 - |// Can't use rotrv, since it's only in MIPS32R2. - |.ffunc_bit_sh rol, or, 1 - |.ffunc_bit_sh ror, or, 2 - | - |.ffunc_bit tobit - |->fff_resi: - | mtc1 CRET1, FRET1 - | b ->fff_resn - |. cvt.d.w FRET1, FRET1 - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RB = CFUNC, RC = nargs*8 - | lw TMP3, CFUNC:RB->f - | addu TMP1, BASE, NARGS8:RC - | lw PC, FRAME_PC(BASE) // Fallback may overwrite PC. - | addiu TMP0, TMP1, 8*LUA_MINSTACK - | lw TMP2, L->maxstack - | sw PC, SAVE_PC // Redundant (but a defined value). - | sltu AT, TMP2, TMP0 - | sw BASE, L->base - | sw TMP1, L->top - | bnez AT, >5 // Need to grow stack. - |. move CFUNCADDR, TMP3 - | jalr TMP3 // (lua_State *L) - |. move CARG1, L - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | lw BASE, L->base - | sll RD, CRET1, 3 - | bgtz CRET1, ->fff_res // Returned nresults+1? - |. addiu RA, BASE, -8 - |1: // Returned 0 or -1: retry fast path. - | lw TMP0, L->top - | lw LFUNC:RB, FRAME_FUNC(BASE) - | bnez CRET1, ->vm_call_tail // Returned -1? - |. subu NARGS8:RC, TMP0, BASE - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | andi TMP0, PC, FRAME_TYPE - | li AT, -4 - | bnez TMP0, >3 - |. and TMP1, PC, AT - | lbu TMP1, OFS_RA(PC) - | sll TMP1, TMP1, 3 - | addiu TMP1, TMP1, 8 - |3: - | b ->vm_call_dispatch // Resolve again for tailcall. - |. subu TMP2, BASE, TMP1 - | - |5: // Grow stack for fallback handler. - | load_got lj_state_growstack - | li CARG2, LUA_MINSTACK - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | lw BASE, L->base - | b <1 - |. li CRET1, 0 // Force retry. - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | move MULTRES, ra - | load_got lj_gc_step - | sw BASE, L->base - | addu TMP0, BASE, NARGS8:RC - | sw PC, SAVE_PC // Redundant (but a defined value). - | sw TMP0, L->top - | call_intern lj_gc_step // (lua_State *L) - |. move CARG1, L - | lw BASE, L->base - | move ra, MULTRES - | lw TMP0, L->top - | lw CFUNC:RB, FRAME_FUNC(BASE) - | jr ra - |. subu NARGS8:RC, TMP0, BASE - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andi AT, TMP3, HOOK_VMEVENT // No recording while in vmevent. - | bnez AT, >5 - | // Decrement the hookcount for consistency, but always do the call. - |. lw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE - | bnez AT, >1 - |. addiu TMP2, TMP2, -1 - | andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqz AT, >1 - |. nop - | b >1 - |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE // Hook already active? - | beqz AT, >1 - |5: // Re-dispatch to static ins. - |. lw AT, GG_DISP2STATIC(TMP0) // Assumes TMP0 holds DISPATCH+OP*4. - | jr AT - |. nop - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | lw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE // Hook already active? - | bnez AT, <5 - |. andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqz AT, <5 - |. addiu TMP2, TMP2, -1 - | beqz TMP2, >1 - |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, LUA_MASKLINE - | beqz AT, <5 - |1: - |. load_got lj_dispatch_ins - | sw MULTRES, SAVE_MULTRES - | move CARG2, PC - | sw BASE, L->base - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | call_intern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |. move CARG1, L - |3: - | lw BASE, L->base - |4: // Re-dispatch to static ins. - | lw INS, -4(PC) - | decode_OP4a TMP1, INS - | decode_OP4b TMP1 - | addu TMP0, DISPATCH, TMP1 - | decode_RD8a RD, INS - | lw AT, GG_DISP2STATIC(TMP0) - | decode_RA8a RA, INS - | decode_RD8b RD - | jr AT - | decode_RA8b RA - | - |->cont_hook: // Continue from hook yield. - | addiu PC, PC, 4 - | b <4 - |. lw MULTRES, -24+LO(RB) // Restore MULTRES for *M ins. - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | lw LFUNC:TMP1, FRAME_FUNC(BASE) - | addiu CARG1, DISPATCH, GG_DISP2J - | sw PC, SAVE_PC - | lw TMP1, LFUNC:TMP1->pc - | move CARG2, PC - | sw L, DISPATCH_J(L)(DISPATCH) - | lbu TMP1, PC2PROTO(framesize)(TMP1) - | load_got lj_trace_hot - | sw BASE, L->base - | sll TMP1, TMP1, 3 - | addu TMP1, BASE, TMP1 - | call_intern lj_trace_hot // (jit_State *J, const BCIns *pc) - |. sw TMP1, L->top - | b <3 - |. nop - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - |.if JIT - | b >1 - |.endif - |. move CARG2, PC - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | ori CARG2, PC, 1 - |1: - |.endif - | load_got lj_dispatch_call - | addu TMP0, BASE, RC - | sw PC, SAVE_PC - | sw BASE, L->base - | subu RA, RA, BASE - | sw TMP0, L->top - | call_intern lj_dispatch_call // (lua_State *L, const BCIns *pc) - |. move CARG1, L - | // Returns ASMFunction. - | lw BASE, L->base - | lw TMP0, L->top - | sw r0, SAVE_PC // Invalidate for subsequent line hook. - | subu NARGS8:RC, TMP0, BASE - | addu RA, BASE, RA - | lw LFUNC:RB, FRAME_FUNC(BASE) - | jr CRET1 - |. lw INS, -4(PC) - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro savex_, a, b - | sdc1 f..a, 16+a*8(sp) - | sw r..a, 16+32*8+a*4(sp) - | sw r..b, 16+32*8+b*4(sp) - |.endmacro - | - |->vm_exit_handler: - |.if JIT - | addiu sp, sp, -(16+32*8+32*4) - | savex_ 0, 1 - | savex_ 2, 3 - | savex_ 4, 5 - | savex_ 6, 7 - | savex_ 8, 9 - | savex_ 10, 11 - | savex_ 12, 13 - | savex_ 14, 15 - | savex_ 16, 17 - | savex_ 18, 19 - | savex_ 20, 21 - | savex_ 22, 23 - | savex_ 24, 25 - | savex_ 26, 27 - | sdc1 f28, 16+28*8(sp) - | sw r28, 16+32*8+28*4(sp) - | sdc1 f30, 16+30*8(sp) - | sw r30, 16+32*8+30*4(sp) - | sw r0, 16+32*8+31*4(sp) // Clear RID_TMP. - | li_vmstate EXIT - | addiu TMP2, sp, 16+32*8+32*4 // Recompute original value of sp. - | addiu DISPATCH, JGL, -GG_DISP2G-32768 - | lw TMP1, 0(TMP2) // Load exit number. - | st_vmstate - | sw TMP2, 16+32*8+29*4(sp) // Store sp in RID_SP. - | lw L, DISPATCH_GL(jit_L)(DISPATCH) - | lw BASE, DISPATCH_GL(jit_base)(DISPATCH) - | load_got lj_trace_exit - | sw L, DISPATCH_J(L)(DISPATCH) - | sw ra, DISPATCH_J(parent)(DISPATCH) // Store trace number. - | sw TMP1, DISPATCH_J(exitno)(DISPATCH) // Store exit number. - | addiu CARG1, DISPATCH, GG_DISP2J - | sw BASE, L->base - | call_intern lj_trace_exit // (jit_State *J, ExitState *ex) - |. addiu CARG2, sp, 16 - | // Returns MULTRES (unscaled) or negated error code. - | lw TMP1, L->cframe - | li AT, -4 - | lw BASE, L->base - | and sp, TMP1, AT - | lw PC, SAVE_PC // Get SAVE_PC. - | b >1 - |. sw L, SAVE_L // Set SAVE_L (on-trace resume/yield). - |.endif - |->vm_exit_interp: - |.if JIT - | // CRET1 = MULTRES or negated error code, BASE, PC and JGL set. - | lw L, SAVE_L - | addiu DISPATCH, JGL, -GG_DISP2G-32768 - |1: - | bltz CRET1, >3 // Check for error from exit. - |. lw LFUNC:TMP1, FRAME_FUNC(BASE) - | lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | sll MULTRES, CRET1, 3 - | li TISNIL, LJ_TNIL - | sw MULTRES, SAVE_MULTRES - | mtc1 TMP3, TOBIT - | lw TMP1, LFUNC:TMP1->pc - | sw r0, DISPATCH_GL(jit_L)(DISPATCH) - | lw KBASE, PC2PROTO(k)(TMP1) - | cvt.d.s TOBIT, TOBIT - | // Modified copy of ins_next which handles function header dispatch, too. - | lw INS, 0(PC) - | addiu PC, PC, 4 - | // Assumes TISNIL == ~LJ_VMST_INTERP == -1 - | sw TISNIL, DISPATCH_GL(vmstate)(DISPATCH) - | decode_OP4a TMP1, INS - | decode_OP4b TMP1 - | sltiu TMP2, TMP1, BC_FUNCF*4 // Function header? - | addu TMP0, DISPATCH, TMP1 - | decode_RD8a RD, INS - | lw AT, 0(TMP0) - | decode_RA8a RA, INS - | beqz TMP2, >2 - |. decode_RA8b RA - | jr AT - |. decode_RD8b RD - |2: - | addiu RC, MULTRES, -8 - | jr AT - |. add RA, RA, BASE - | - |3: // Rethrow error from the right C frame. - | load_got lj_err_throw - | negu CARG2, CRET1 - | call_intern lj_err_throw // (lua_State *L, int errcode) - |. move CARG1, L - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Modifies AT, TMP0, FRET1, FRET2, f4. Keeps all others incl. FARG1. - |.macro vm_round, func - | lui TMP0, 0x4330 // Hiword of 2^52 (double). - | mtc1 r0, f4 - | mtc1 TMP0, f5 - | abs.d FRET2, FARG1 // |x| - | mfc1 AT, f13 - | c.olt.d 0, FRET2, f4 - | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52 - | bc1f 0, >1 // Truncate only if |x| < 2^52. - |. sub.d FRET1, FRET1, f4 - | slt AT, AT, r0 - |.if "func" == "ceil" - | lui TMP0, 0xbff0 // Hiword of -1 (double). Preserves -0. - |.else - | lui TMP0, 0x3ff0 // Hiword of +1 (double). - |.endif - |.if "func" == "trunc" - | mtc1 TMP0, f5 - | c.olt.d 0, FRET2, FRET1 // |x| < result? - | sub.d FRET2, FRET1, f4 - | movt.d FRET1, FRET2, 0 // If yes, subtract +1. - | neg.d FRET2, FRET1 - | jr ra - |. movn.d FRET1, FRET2, AT // Merge sign bit back in. - |.else - | neg.d FRET2, FRET1 - | mtc1 TMP0, f5 - | movn.d FRET1, FRET2, AT // Merge sign bit back in. - |.if "func" == "ceil" - | c.olt.d 0, FRET1, FARG1 // x > result? - |.else - | c.olt.d 0, FARG1, FRET1 // x < result? - |.endif - | sub.d FRET2, FRET1, f4 // If yes, subtract +-1. - | jr ra - |. movt.d FRET1, FRET2, 0 - |.endif - |1: - | jr ra - |. mov.d FRET1, FARG1 - |.endmacro - | - |->vm_floor: - | vm_round floor - |->vm_ceil: - | vm_round ceil - |->vm_trunc: - |.if JIT - | vm_round trunc - |.endif - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in r1, g in r2. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | saveregs - | lw CTSTATE, GL:r2->ctype_state - | addiu DISPATCH, r2, GG_G2DISP - | load_got lj_ccallback_enter - | sw r1, CTSTATE->cb.slot - | sw CARG1, CTSTATE->cb.gpr[0] - | sw CARG2, CTSTATE->cb.gpr[1] - | sdc1 FARG1, CTSTATE->cb.fpr[0] - | sw CARG3, CTSTATE->cb.gpr[2] - | sw CARG4, CTSTATE->cb.gpr[3] - | sdc1 FARG2, CTSTATE->cb.fpr[1] - | addiu TMP0, sp, CFRAME_SPACE+16 - | sw TMP0, CTSTATE->cb.stack - | sw r0, SAVE_PC // Any value outside of bytecode is ok. - | move CARG2, sp - | call_intern lj_ccallback_enter // (CTState *cts, void *cf) - |. move CARG1, CTSTATE - | // Returns lua_State *. - | lw BASE, L:CRET1->base - | lw RC, L:CRET1->top - | move L, CRET1 - | lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | lw LFUNC:RB, FRAME_FUNC(BASE) - | mtc1 TMP3, TOBIT - | li_vmstate INTERP - | li TISNIL, LJ_TNIL - | subu RC, RC, BASE - | st_vmstate - | cvt.d.s TOBIT, TOBIT - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | load_got lj_ccallback_leave - | lw CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH) - | sw BASE, L->base - | sw RB, L->top - | sw L, CTSTATE->L - | move CARG2, RA - | call_intern lj_ccallback_leave // (CTState *cts, TValue *o) - |. move CARG1, CTSTATE - | lw CRET1, CTSTATE->cb.gpr[0] - | ldc1 FRET1, CTSTATE->cb.fpr[0] - | lw CRET2, CTSTATE->cb.gpr[1] - | b ->vm_leave_unw - |. ldc1 FRET2, CTSTATE->cb.fpr[1] - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, CARG1 - | lw TMP1, CCSTATE->spadj - | lbu CARG2, CCSTATE->nsp - | move TMP2, sp - | subu sp, sp, TMP1 - | sw ra, -4(TMP2) - | sll CARG2, CARG2, 2 - | sw r16, -8(TMP2) - | sw CCSTATE, -12(TMP2) - | move r16, TMP2 - | addiu TMP1, CCSTATE, offsetof(CCallState, stack) - | addiu TMP2, sp, 16 - | beqz CARG2, >2 - |. addu TMP3, TMP1, CARG2 - |1: - | lw TMP0, 0(TMP1) - | addiu TMP1, TMP1, 4 - | sltu AT, TMP1, TMP3 - | sw TMP0, 0(TMP2) - | bnez AT, <1 - |. addiu TMP2, TMP2, 4 - |2: - | lw CFUNCADDR, CCSTATE->func - | lw CARG2, CCSTATE->gpr[1] - | lw CARG3, CCSTATE->gpr[2] - | lw CARG4, CCSTATE->gpr[3] - | ldc1 FARG1, CCSTATE->fpr[0] - | ldc1 FARG2, CCSTATE->fpr[1] - | jalr CFUNCADDR - |. lw CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1. - | lw CCSTATE:TMP1, -12(r16) - | lw TMP2, -8(r16) - | lw ra, -4(r16) - | sw CRET1, CCSTATE:TMP1->gpr[0] - | sw CRET2, CCSTATE:TMP1->gpr[1] - | sdc1 FRET1, CCSTATE:TMP1->fpr[0] - | sdc1 FRET2, CCSTATE:TMP1->fpr[1] - | move sp, r16 - | jr ra - |. move r16, TMP2 - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RD = src2*8, JMP with RD = target - | addu CARG2, BASE, RA - | addu CARG3, BASE, RD - | lw TMP0, HI(CARG2) - | lw TMP1, HI(CARG3) - | ldc1 f0, 0(CARG2) - | ldc1 f2, 0(CARG3) - | sltiu TMP0, TMP0, LJ_TISNUM - | sltiu TMP1, TMP1, LJ_TISNUM - | lhu TMP2, OFS_RD(PC) - | and TMP0, TMP0, TMP1 - | addiu PC, PC, 4 - | beqz TMP0, ->vmeta_comp - |. lui TMP1, (-(BCBIAS_J*4 >> 16) & 65535) - | decode_RD4b TMP2 - | addu TMP2, TMP2, TMP1 - if (op == BC_ISLT || op == BC_ISGE) { - | c.olt.d f0, f2 - } else { - | c.ole.d f0, f2 - } - if (op == BC_ISLT || op == BC_ISLE) { - | movf TMP2, r0 - } else { - | movt TMP2, r0 - } - | addu PC, PC, TMP2 - |1: - | ins_next - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RD = src2*8, JMP with RD = target - | addu RA, BASE, RA - | addiu PC, PC, 4 - | lw TMP0, HI(RA) - | ldc1 f0, 0(RA) - | addu RD, BASE, RD - | lhu TMP2, -4+OFS_RD(PC) - | lw TMP1, HI(RD) - | ldc1 f2, 0(RD) - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sltiu AT, TMP0, LJ_TISNUM - | sltiu CARG1, TMP1, LJ_TISNUM - | decode_RD4b TMP2 - | and AT, AT, CARG1 - | beqz AT, >5 - |. addu TMP2, TMP2, TMP3 - | c.eq.d f0, f2 - if (vk) { - | movf TMP2, r0 - } else { - | movt TMP2, r0 - } - |1: - | addu PC, PC, TMP2 - | ins_next - |5: // Either or both types are not numbers. - | lw CARG2, LO(RA) - | lw CARG3, LO(RD) - |.if FFI - | li TMP3, LJ_TCDATA - | beq TMP0, TMP3, ->vmeta_equal_cd - |.endif - |. sltiu AT, TMP0, LJ_TISPRI // Not a primitive? - |.if FFI - | beq TMP1, TMP3, ->vmeta_equal_cd - |.endif - |. xor TMP3, CARG2, CARG3 // Same tv? - | xor TMP1, TMP1, TMP0 // Same type? - | sltiu CARG1, TMP0, LJ_TISTABUD+1 // Table or userdata? - | movz TMP3, r0, AT // Ignore tv if primitive. - | movn CARG1, r0, TMP1 // Tab/ud and same type? - | or AT, TMP1, TMP3 // Same type && (pri||same tv). - | movz CARG1, r0, AT - | beqz CARG1, <1 // Done if not tab/ud or not same type or same tv. - if (vk) { - |. movn TMP2, r0, AT - } else { - |. movz TMP2, r0, AT - } - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | lw TAB:TMP1, TAB:CARG2->metatable - | beqz TAB:TMP1, <1 // No metatable? - |. nop - | lbu TMP1, TAB:TMP1->nomm - | andi TMP1, TMP1, 1<vmeta_equal // Handle __eq metamethod. - |. li CARG4, 1-vk // ne = 0 or 1. - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RD = str_const*8 (~), JMP with RD = target - | addu RA, BASE, RA - | addiu PC, PC, 4 - | lw TMP0, HI(RA) - | srl RD, RD, 1 - | lw STR:TMP3, LO(RA) - | subu RD, KBASE, RD - | lhu TMP2, -4+OFS_RD(PC) - |.if FFI - | li AT, LJ_TCDATA - | beq TMP0, AT, ->vmeta_equal_cd - |.endif - |. lw STR:TMP1, -4(RD) // KBASE-4-str_const*4 - | addiu TMP0, TMP0, -LJ_TSTR - | decode_RD4b TMP2 - | xor TMP1, STR:TMP1, STR:TMP3 - | or TMP0, TMP0, TMP1 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - if (vk) { - | movn TMP2, r0, TMP0 - } else { - | movz TMP2, r0, TMP0 - } - | addu PC, PC, TMP2 - | ins_next - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RD = num_const*8, JMP with RD = target - | addu RA, BASE, RA - | addiu PC, PC, 4 - | lw TMP0, HI(RA) - | ldc1 f0, 0(RA) - | addu RD, KBASE, RD - | lhu TMP2, -4+OFS_RD(PC) - | ldc1 f2, 0(RD) - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sltiu AT, TMP0, LJ_TISNUM - | decode_RD4b TMP2 - |.if FFI - | beqz AT, >5 - |.else - | beqz AT, >1 - |.endif - |. addu TMP2, TMP2, TMP3 - | c.eq.d f0, f2 - if (vk) { - | movf TMP2, r0 - | addu PC, PC, TMP2 - |1: - } else { - | movt TMP2, r0 - |1: - | addu PC, PC, TMP2 - } - | ins_next - |.if FFI - |5: - | li AT, LJ_TCDATA - | beq TMP0, AT, ->vmeta_equal_cd - |. nop - | b <1 - |. nop - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target - | addu RA, BASE, RA - | srl TMP1, RD, 3 - | lw TMP0, HI(RA) - | lhu TMP2, OFS_RD(PC) - | not TMP1, TMP1 - | addiu PC, PC, 4 - |.if FFI - | li AT, LJ_TCDATA - | beq TMP0, AT, ->vmeta_equal_cd - |.endif - |. xor TMP0, TMP0, TMP1 - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - if (vk) { - | movn TMP2, r0, TMP0 - } else { - | movz TMP2, r0, TMP0 - } - | addu PC, PC, TMP2 - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RD = src*8, JMP with RD = target - | addu RD, BASE, RD - | lhu TMP2, OFS_RD(PC) - | lw TMP0, HI(RD) - | addiu PC, PC, 4 - if (op == BC_IST || op == BC_ISF) { - | sltiu TMP0, TMP0, LJ_TISTRUECOND - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - if (op == BC_IST) { - | movz TMP2, r0, TMP0 - } else { - | movn TMP2, r0, TMP0 - } - | addu PC, PC, TMP2 - } else { - | sltiu TMP0, TMP0, LJ_TISTRUECOND - | ldc1 f0, 0(RD) - if (op == BC_ISTC) { - | beqz TMP0, >1 - } else { - | bnez TMP0, >1 - } - |. addu RA, BASE, RA - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - | sdc1 f0, 0(RA) - | addu PC, PC, TMP2 - |1: - } - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RD = src*8 - | addu RD, BASE, RD - | addu RA, BASE, RA - | ldc1 f0, 0(RD) - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - break; - case BC_NOT: - | // RA = dst*8, RD = src*8 - | addu RD, BASE, RD - | addu RA, BASE, RA - | lw TMP0, HI(RD) - | li TMP1, LJ_TFALSE - | sltiu TMP0, TMP0, LJ_TISTRUECOND - | addiu TMP1, TMP0, LJ_TTRUE - | ins_next1 - | sw TMP1, HI(RA) - | ins_next2 - break; - case BC_UNM: - | // RA = dst*8, RD = src*8 - | addu CARG3, BASE, RD - | addu RA, BASE, RA - | lw TMP0, HI(CARG3) - | ldc1 f0, 0(CARG3) - | sltiu AT, TMP0, LJ_TISNUM - | beqz AT, ->vmeta_unm - |. neg.d f0, f0 - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - break; - case BC_LEN: - | // RA = dst*8, RD = src*8 - | addu CARG2, BASE, RD - | addu RA, BASE, RA - | lw TMP0, HI(CARG2) - | lw CARG1, LO(CARG2) - | li AT, LJ_TSTR - | bne TMP0, AT, >2 - |. li AT, LJ_TTAB - | lw CRET1, STR:CARG1->len - |1: - | mtc1 CRET1, f0 - | cvt.d.w f0, f0 - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - |2: - | bne TMP0, AT, ->vmeta_len - |. nop -#if LJ_52 - | lw TAB:TMP2, TAB:CARG1->metatable - | bnez TAB:TMP2, >9 - |. nop - |3: -#endif - |->BC_LEN_Z: - | load_got lj_tab_len - | call_intern lj_tab_len // (GCtab *t) - |. nop - | // Returns uint32_t (but less than 2^31). - | b <1 - |. nop -#if LJ_52 - |9: - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_len - |. nop -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithpre - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||switch (vk) { - ||case 0: - | addu CARG3, BASE, RB - | addu CARG4, KBASE, RC - | lw TMP1, HI(CARG3) - | ldc1 f20, 0(CARG3) - | ldc1 f22, 0(CARG4) - | sltiu AT, TMP1, LJ_TISNUM - || break; - ||case 1: - | addu CARG4, BASE, RB - | addu CARG3, KBASE, RC - | lw TMP1, HI(CARG4) - | ldc1 f22, 0(CARG4) - | ldc1 f20, 0(CARG3) - | sltiu AT, TMP1, LJ_TISNUM - || break; - ||default: - | addu CARG3, BASE, RB - | addu CARG4, BASE, RC - | lw TMP1, HI(CARG3) - | lw TMP2, HI(CARG4) - | ldc1 f20, 0(CARG3) - | ldc1 f22, 0(CARG4) - | sltiu AT, TMP1, LJ_TISNUM - | sltiu TMP0, TMP2, LJ_TISNUM - | and AT, AT, TMP0 - || break; - ||} - | beqz AT, ->vmeta_arith - |. addu RA, BASE, RA - |.endmacro - | - |.macro fpmod, a, b, c - |->BC_MODVN_Z: - | bal ->vm_floor // floor(b/c) - |. div.d FARG1, b, c - | mul.d a, FRET1, c - | sub.d a, b, a // b - floor(b/c)*c - |.endmacro - | - |.macro ins_arith, ins - | ins_arithpre - |.if "ins" == "fpmod_" - | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - |. nop - |.else - | ins f0, f20, f22 - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - |.endif - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arith add.d - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arith sub.d - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith mul.d - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arith div.d - break; - case BC_MODVN: - | ins_arith fpmod - break; - case BC_MODNV: case BC_MODVV: - | ins_arith fpmod_ - break; - case BC_POW: - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | addu CARG3, BASE, RB - | addu CARG4, BASE, RC - | lw TMP1, HI(CARG3) - | lw TMP2, HI(CARG4) - | ldc1 FARG1, 0(CARG3) - | ldc1 FARG2, 0(CARG4) - | sltiu AT, TMP1, LJ_TISNUM - | sltiu TMP0, TMP2, LJ_TISNUM - | and AT, AT, TMP0 - | load_got pow - | beqz AT, ->vmeta_arith - |. addu RA, BASE, RA - | call_extern - |. nop - | ins_next1 - | sdc1 FRET1, 0(RA) - | ins_next2 - break; - - case BC_CAT: - | // RA = dst*8, RB = src_start*8, RC = src_end*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | subu CARG3, RC, RB - | sw BASE, L->base - | addu CARG2, BASE, RC - | move MULTRES, RB - |->BC_CAT_Z: - | load_got lj_meta_cat - | srl CARG3, CARG3, 3 - | sw PC, SAVE_PC - | call_intern lj_meta_cat // (lua_State *L, TValue *top, int left) - |. move CARG1, L - | // Returns NULL (finished) or TValue * (metamethod). - | bnez CRET1, ->vmeta_binop - |. lw BASE, L->base - | addu RB, BASE, MULTRES - | ldc1 f0, 0(RB) - | addu RA, BASE, RA - | ins_next1 - | sdc1 f0, 0(RA) // Copy result from RB to RA. - | ins_next2 - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RD = str_const*8 (~) - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | ins_next1 - | lw TMP0, -4(TMP1) // KBASE-4-str_const*4 - | addu RA, BASE, RA - | li TMP2, LJ_TSTR - | sw TMP0, LO(RA) - | sw TMP2, HI(RA) - | ins_next2 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RD = cdata_const*8 (~) - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | ins_next1 - | lw TMP0, -4(TMP1) // KBASE-4-cdata_const*4 - | addu RA, BASE, RA - | li TMP2, LJ_TCDATA - | sw TMP0, LO(RA) - | sw TMP2, HI(RA) - | ins_next2 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, RD = int16_literal*8 - | sra RD, INS, 16 - | mtc1 RD, f0 - | addu RA, BASE, RA - | cvt.d.w f0, f0 - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - break; - case BC_KNUM: - | // RA = dst*8, RD = num_const*8 - | addu RD, KBASE, RD - | addu RA, BASE, RA - | ldc1 f0, 0(RD) - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - break; - case BC_KPRI: - | // RA = dst*8, RD = primitive_type*8 (~) - | srl TMP1, RD, 3 - | addu RA, BASE, RA - | not TMP0, TMP1 - | ins_next1 - | sw TMP0, HI(RA) - | ins_next2 - break; - case BC_KNIL: - | // RA = base*8, RD = end*8 - | addu RA, BASE, RA - | sw TISNIL, HI(RA) - | addiu RA, RA, 8 - | addu RD, BASE, RD - |1: - | sw TISNIL, HI(RA) - | slt AT, RA, RD - | bnez AT, <1 - |. addiu RA, RA, 8 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RD = uvnum*8 - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RD, RD, 1 - | addu RD, RD, LFUNC:RB - | lw UPVAL:RB, LFUNC:RD->uvptr - | ins_next1 - | lw TMP1, UPVAL:RB->v - | ldc1 f0, 0(TMP1) - | addu RA, BASE, RA - | sdc1 f0, 0(RA) - | ins_next2 - break; - case BC_USETV: - | // RA = uvnum*8, RD = src*8 - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | addu RD, BASE, RD - | addu RA, RA, LFUNC:RB - | ldc1 f0, 0(RD) - | lw UPVAL:RB, LFUNC:RA->uvptr - | lbu TMP3, UPVAL:RB->marked - | lw CARG2, UPVAL:RB->v - | andi TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbu TMP0, UPVAL:RB->closed - | lw TMP2, HI(RD) - | sdc1 f0, 0(CARG2) - | li AT, LJ_GC_BLACK|1 - | or TMP3, TMP3, TMP0 - | beq TMP3, AT, >2 // Upvalue is closed and black? - |. addiu TMP2, TMP2, -(LJ_TISNUM+1) - |1: - | ins_next - | - |2: // Check if new value is collectable. - | sltiu AT, TMP2, LJ_TISGCV - (LJ_TISNUM+1) - | beqz AT, <1 // tvisgcv(v) - |. lw TMP1, LO(RD) - | lbu TMP3, GCOBJ:TMP1->gch.marked - | andi TMP3, TMP3, LJ_GC_WHITES // iswhite(v) - | beqz TMP3, <1 - |. load_got lj_gc_barrieruv - | // Crossed a write barrier. Move the barrier forward. - | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv) - |. addiu CARG1, DISPATCH, GG_DISP2G - | b <1 - |. nop - break; - case BC_USETS: - | // RA = uvnum*8, RD = str_const*8 (~) - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | srl TMP1, RD, 1 - | addu RA, RA, LFUNC:RB - | subu TMP1, KBASE, TMP1 - | lw UPVAL:RB, LFUNC:RA->uvptr - | lw STR:TMP1, -4(TMP1) // KBASE-4-str_const*4 - | lbu TMP2, UPVAL:RB->marked - | lw CARG2, UPVAL:RB->v - | lbu TMP3, STR:TMP1->marked - | andi AT, TMP2, LJ_GC_BLACK // isblack(uv) - | lbu TMP2, UPVAL:RB->closed - | li TMP0, LJ_TSTR - | sw STR:TMP1, LO(CARG2) - | bnez AT, >2 - |. sw TMP0, HI(CARG2) - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | beqz TMP2, <1 - |. andi AT, TMP3, LJ_GC_WHITES // iswhite(str) - | beqz AT, <1 - |. load_got lj_gc_barrieruv - | // Crossed a write barrier. Move the barrier forward. - | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv) - |. addiu CARG1, DISPATCH, GG_DISP2G - | b <1 - |. nop - break; - case BC_USETN: - | // RA = uvnum*8, RD = num_const*8 - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | addu RD, KBASE, RD - | addu RA, RA, LFUNC:RB - | ldc1 f0, 0(RD) - | lw UPVAL:RB, LFUNC:RA->uvptr - | ins_next1 - | lw TMP1, UPVAL:RB->v - | sdc1 f0, 0(TMP1) - | ins_next2 - break; - case BC_USETP: - | // RA = uvnum*8, RD = primitive_type*8 (~) - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | srl TMP0, RD, 3 - | addu RA, RA, LFUNC:RB - | not TMP0, TMP0 - | lw UPVAL:RB, LFUNC:RA->uvptr - | ins_next1 - | lw TMP1, UPVAL:RB->v - | sw TMP0, HI(TMP1) - | ins_next2 - break; - - case BC_UCLO: - | // RA = level*8, RD = target - | lw TMP2, L->openupval - | branch_RD // Do this first since RD is not saved. - | load_got lj_func_closeuv - | sw BASE, L->base - | beqz TMP2, >1 - |. move CARG1, L - | call_intern lj_func_closeuv // (lua_State *L, TValue *level) - |. addu CARG2, BASE, RA - | lw BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype) - | srl TMP1, RD, 1 - | load_got lj_func_newL_gc - | subu TMP1, KBASE, TMP1 - | lw CARG3, FRAME_FUNC(BASE) - | lw CARG2, -4(TMP1) // KBASE-4-tab_const*4 - | sw BASE, L->base - | sw PC, SAVE_PC - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | call_intern lj_func_newL_gc - |. move CARG1, L - | // Returns GCfuncL *. - | lw BASE, L->base - | li TMP0, LJ_TFUNC - | ins_next1 - | addu RA, BASE, RA - | sw TMP0, HI(RA) - | sw LFUNC:CRET1, LO(RA) - | ins_next2 - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~) - | lw TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | lw TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | sw BASE, L->base - | sw PC, SAVE_PC - | sltu AT, TMP0, TMP1 - | beqz AT, >5 - |1: - if (op == BC_TNEW) { - | load_got lj_tab_new - | srl CARG2, RD, 3 - | andi CARG2, CARG2, 0x7ff - | li TMP0, 0x801 - | addiu AT, CARG2, -0x7ff - | srl CARG3, RD, 14 - | movz CARG2, TMP0, AT - | // (lua_State *L, int32_t asize, uint32_t hbits) - | call_intern lj_tab_new - |. move CARG1, L - | // Returns Table *. - } else { - | load_got lj_tab_dup - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | move CARG1, L - | call_intern lj_tab_dup // (lua_State *L, Table *kt) - |. lw CARG2, -4(TMP1) // KBASE-4-str_const*4 - | // Returns Table *. - } - | lw BASE, L->base - | ins_next1 - | addu RA, BASE, RA - | li TMP0, LJ_TTAB - | sw TAB:CRET1, LO(RA) - | sw TMP0, HI(RA) - | ins_next2 - |5: - | load_got lj_gc_step_fixtop - | move MULTRES, RD - | call_intern lj_gc_step_fixtop // (lua_State *L) - |. move CARG1, L - | b <1 - |. move RD, MULTRES - break; - - case BC_GGET: - | // RA = dst*8, RD = str_const*8 (~) - case BC_GSET: - | // RA = src*8, RD = str_const*8 (~) - | lw LFUNC:TMP2, FRAME_FUNC(BASE) - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | lw TAB:RB, LFUNC:TMP2->env - | lw STR:RC, -4(TMP1) // KBASE-4-str_const*4 - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - |. addu RA, BASE, RA - break; - - case BC_TGETV: - | // RA = dst*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | addu CARG2, BASE, RB - | addu CARG3, BASE, RC - | lw TMP1, HI(CARG2) - | lw TMP2, HI(CARG3) - | lw TAB:RB, LO(CARG2) - | li AT, LJ_TTAB - | ldc1 f0, 0(CARG3) - | bne TMP1, AT, ->vmeta_tgetv - |. addu RA, BASE, RA - | sltiu AT, TMP2, LJ_TISNUM - | beqz AT, >5 - |. li AT, LJ_TSTR - | - | // Convert number key to integer, check for integerness and range. - | cvt.w.d f2, f0 - | lw TMP0, TAB:RB->asize - | mfc1 TMP2, f2 - | cvt.d.w f4, f2 - | lw TMP1, TAB:RB->array - | c.eq.d f0, f4 - | sltu AT, TMP2, TMP0 - | movf AT, r0 - | sll TMP2, TMP2, 3 - | beqz AT, ->vmeta_tgetv // Integer key and in array part? - |. addu TMP2, TMP1, TMP2 - | lw TMP0, HI(TMP2) - | beq TMP0, TISNIL, >2 - |. ldc1 f0, 0(TMP2) - |1: - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - | - |2: // Check for __index if table value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_tgetv - |. nop - | - |5: - | bne TMP2, AT, ->vmeta_tgetv - |. lw STR:RC, LO(CARG3) - | b ->BC_TGETS_Z // String key? - |. nop - break; - case BC_TGETS: - | // RA = dst*8, RB = table*8, RC = str_const*4 (~) - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RC4a RC, INS - | lw TMP0, HI(CARG2) - | decode_RC4b RC - | li AT, LJ_TTAB - | lw TAB:RB, LO(CARG2) - | subu CARG3, KBASE, RC - | lw STR:RC, -4(CARG3) // KBASE-4-str_const*4 - | bne TMP0, AT, ->vmeta_tgets1 - |. addu RA, BASE, RA - |->BC_TGETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8 - | lw TMP0, TAB:RB->hmask - | lw TMP1, STR:RC->hash - | lw NODE:TMP2, TAB:RB->node - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | addu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | lw CARG1, offsetof(Node, key)+HI(NODE:TMP2) - | lw TMP0, offsetof(Node, key)+LO(NODE:TMP2) - | lw NODE:TMP1, NODE:TMP2->next - | lw CARG2, offsetof(Node, val)+HI(NODE:TMP2) - | addiu CARG1, CARG1, -LJ_TSTR - | xor TMP0, TMP0, STR:RC - | or AT, CARG1, TMP0 - | bnez AT, >4 - |. lw TAB:TMP3, TAB:RB->metatable - | beq CARG2, TISNIL, >5 // Key found, but nil value? - |. lw CARG1, offsetof(Node, val)+LO(NODE:TMP2) - |3: - | ins_next1 - | sw CARG2, HI(RA) - | sw CARG1, LO(RA) - | ins_next2 - | - |4: // Follow hash chain. - | bnez NODE:TMP1, <1 - |. move NODE:TMP2, NODE:TMP1 - | // End of hash chain: key not found, nil result. - | - |5: // Check for __index if table value is nil. - | beqz TAB:TMP3, <3 // No metatable: done. - |. li CARG2, LJ_TNIL - | lbu TMP0, TAB:TMP3->nomm - | andi TMP0, TMP0, 1<vmeta_tgets - |. nop - break; - case BC_TGETB: - | // RA = dst*8, RB = table*8, RC = index*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RDtoRC8 RC, RD - | lw CARG1, HI(CARG2) - | li AT, LJ_TTAB - | lw TAB:RB, LO(CARG2) - | addu RA, BASE, RA - | bne CARG1, AT, ->vmeta_tgetb - |. srl TMP0, RC, 3 - | lw TMP1, TAB:RB->asize - | lw TMP2, TAB:RB->array - | sltu AT, TMP0, TMP1 - | beqz AT, ->vmeta_tgetb - |. addu RC, TMP2, RC - | lw TMP1, HI(RC) - | beq TMP1, TISNIL, >5 - |. ldc1 f0, 0(RC) - |1: - | ins_next1 - | sdc1 f0, 0(RA) - | ins_next2 - | - |5: // Check for __index if table value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP1, TAB:TMP2->nomm - | andi TMP1, TMP1, 1<vmeta_tgetb // Caveat: preserve TMP0! - |. nop - break; - - case BC_TSETV: - | // RA = src*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | addu CARG2, BASE, RB - | addu CARG3, BASE, RC - | lw TMP1, HI(CARG2) - | lw TMP2, HI(CARG3) - | lw TAB:RB, LO(CARG2) - | li AT, LJ_TTAB - | ldc1 f0, 0(CARG3) - | bne TMP1, AT, ->vmeta_tsetv - |. addu RA, BASE, RA - | sltiu AT, TMP2, LJ_TISNUM - | beqz AT, >5 - |. li AT, LJ_TSTR - | - | // Convert number key to integer, check for integerness and range. - | cvt.w.d f2, f0 - | lw TMP0, TAB:RB->asize - | mfc1 TMP2, f2 - | cvt.d.w f4, f2 - | lw TMP1, TAB:RB->array - | c.eq.d f0, f4 - | sltu AT, TMP2, TMP0 - | movf AT, r0 - | sll TMP2, TMP2, 3 - | beqz AT, ->vmeta_tsetv // Integer key and in array part? - |. addu TMP1, TMP1, TMP2 - | lbu TMP3, TAB:RB->marked - | lw TMP0, HI(TMP1) - | beq TMP0, TISNIL, >3 - |. ldc1 f0, 0(RA) - |1: - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |. sdc1 f0, 0(TMP1) - |2: - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP2, TAB:TMP2->nomm - | andi TMP2, TMP2, 1<vmeta_tsetv - |. nop - | - |5: - | bne TMP2, AT, ->vmeta_tsetv - |. lw STR:RC, LO(CARG3) - | b ->BC_TSETS_Z // String key? - |. nop - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <2 - break; - case BC_TSETS: - | // RA = src*8, RB = table*8, RC = str_const*8 (~) - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RC4a RC, INS - | lw TMP0, HI(CARG2) - | decode_RC4b RC - | li AT, LJ_TTAB - | subu CARG3, KBASE, RC - | lw TAB:RB, LO(CARG2) - | lw STR:RC, -4(CARG3) // KBASE-4-str_const*4 - | bne TMP0, AT, ->vmeta_tsets1 - |. addu RA, BASE, RA - |->BC_TSETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = BASE+src*8 - | lw TMP0, TAB:RB->hmask - | lw TMP1, STR:RC->hash - | lw NODE:TMP2, TAB:RB->node - | sb r0, TAB:RB->nomm // Clear metamethod cache. - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | addu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - | ldc1 f20, 0(RA) - |1: - | lw CARG1, offsetof(Node, key)+HI(NODE:TMP2) - | lw TMP0, offsetof(Node, key)+LO(NODE:TMP2) - | li AT, LJ_TSTR - | lw NODE:TMP1, NODE:TMP2->next - | bne CARG1, AT, >5 - |. lw CARG2, offsetof(Node, val)+HI(NODE:TMP2) - | bne TMP0, STR:RC, >5 - |. lbu TMP3, TAB:RB->marked - | beq CARG2, TISNIL, >4 // Key found, but nil value? - |. lw TAB:TMP0, TAB:RB->metatable - |2: - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |. sdc1 f20, NODE:TMP2->val - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | beqz TAB:TMP0, <2 // No metatable: done. - |. nop - | lbu TMP0, TAB:TMP0->nomm - | andi TMP0, TMP0, 1<vmeta_tsets - |. nop - | - |5: // Follow hash chain. - | bnez NODE:TMP1, <1 - |. move NODE:TMP2, NODE:TMP1 - | // End of hash chain: key not found, add a new one - | - | // But check for __newindex first. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, >6 // No metatable: continue. - |. addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |. li AT, LJ_TSTR - |6: - | load_got lj_tab_newkey - | sw STR:RC, LO(CARG3) - | sw AT, HI(CARG3) - | sw BASE, L->base - | move CARG2, TAB:RB - | sw PC, SAVE_PC - | call_intern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k - |. move CARG1, L - | // Returns TValue *. - | lw BASE, L->base - | b <3 // No 2nd write barrier needed. - |. sdc1 f20, 0(CRET1) - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <3 - break; - case BC_TSETB: - | // RA = src*8, RB = table*8, RC = index*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RDtoRC8 RC, RD - | lw CARG1, HI(CARG2) - | li AT, LJ_TTAB - | lw TAB:RB, LO(CARG2) - | addu RA, BASE, RA - | bne CARG1, AT, ->vmeta_tsetb - |. srl TMP0, RC, 3 - | lw TMP1, TAB:RB->asize - | lw TMP2, TAB:RB->array - | sltu AT, TMP0, TMP1 - | beqz AT, ->vmeta_tsetb - |. addu RC, TMP2, RC - | lw TMP1, HI(RC) - | lbu TMP3, TAB:RB->marked - | beq TMP1, TISNIL, >5 - |. ldc1 f0, 0(RA) - |1: - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |. sdc1 f0, 0(RC) - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP1, TAB:TMP2->nomm - | andi TMP1, TMP1, 1<vmeta_tsetb // Caveat: preserve TMP0! - |. nop - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <2 - break; - - case BC_TSETM: - | // RA = base*8 (table at base-1), RD = num_const*8 (start index) - | addu RA, BASE, RA - |1: - | addu TMP3, KBASE, RD - | lw TAB:CARG2, -8+LO(RA) // Guaranteed to be a table. - | addiu TMP0, MULTRES, -8 - | lw TMP3, LO(TMP3) // Integer constant is in lo-word. - | beqz TMP0, >4 // Nothing to copy? - |. srl CARG3, TMP0, 3 - | addu CARG3, CARG3, TMP3 - | lw TMP2, TAB:CARG2->asize - | sll TMP1, TMP3, 3 - | lbu TMP3, TAB:CARG2->marked - | lw CARG1, TAB:CARG2->array - | sltu AT, TMP2, CARG3 - | bnez AT, >5 - |. addu TMP2, RA, TMP0 - | addu TMP1, TMP1, CARG1 - | andi TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |3: // Copy result slots to table. - | ldc1 f0, 0(RA) - | addiu RA, RA, 8 - | sltu AT, RA, TMP2 - | sdc1 f0, 0(TMP1) - | bnez AT, <3 - |. addiu TMP1, TMP1, 8 - | bnez TMP0, >7 - |. nop - |4: - | ins_next - | - |5: // Need to resize array part. - | load_got lj_tab_reasize - | sw BASE, L->base - | sw PC, SAVE_PC - | move BASE, RD - | call_intern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - |. move CARG1, L - | // Must not reallocate the stack. - | move RD, BASE - | b <1 - |. lw BASE, L->base // Reload BASE for lack of a saved register. - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, TMP0, <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8 - | decode_RDtoRC8 NARGS8:RC, RD - | b ->BC_CALL_Z - |. addu NARGS8:RC, NARGS8:RC, MULTRES - break; - case BC_CALL: - | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8 - | decode_RDtoRC8 NARGS8:RC, RD - |->BC_CALL_Z: - | move TMP2, BASE - | addu BASE, BASE, RA - | li AT, LJ_TFUNC - | lw TMP0, HI(BASE) - | lw LFUNC:RB, LO(BASE) - | addiu BASE, BASE, 8 - | bne TMP0, AT, ->vmeta_call - |. addiu NARGS8:RC, NARGS8:RC, -8 - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs*8 - | addu NARGS8:RD, NARGS8:RD, MULTRES // BC_CALLT gets RC from RD. - | // Fall through. Assumes BC_CALLT follows. - break; - case BC_CALLT: - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - | addu RA, BASE, RA - | li AT, LJ_TFUNC - | lw TMP0, HI(RA) - | lw LFUNC:RB, LO(RA) - | move NARGS8:RC, RD - | lw TMP1, FRAME_PC(BASE) - | addiu RA, RA, 8 - | bne TMP0, AT, ->vmeta_callt - |. addiu NARGS8:RC, NARGS8:RC, -8 - |->BC_CALLT_Z: - | andi TMP0, TMP1, FRAME_TYPE // Caveat: preserve TMP0 until the 'or'. - | lbu TMP3, LFUNC:RB->ffid - | bnez TMP0, >7 - |. xori TMP2, TMP1, FRAME_VARG - |1: - | sw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC. - | sltiu AT, TMP3, 2 // (> FF_C) Calling a fast function? - | move TMP2, BASE - | beqz NARGS8:RC, >3 - |. move TMP3, NARGS8:RC - |2: - | ldc1 f0, 0(RA) - | addiu RA, RA, 8 - | addiu TMP3, TMP3, -8 - | sdc1 f0, 0(TMP2) - | bnez TMP3, <2 - |. addiu TMP2, TMP2, 8 - |3: - | or TMP0, TMP0, AT - | beqz TMP0, >5 - |. nop - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | lw INS, -4(TMP1) - | decode_RA8a RA, INS - | decode_RA8b RA - | subu TMP1, BASE, RA - | lw LFUNC:TMP1, -8+FRAME_FUNC(TMP1) - | lw TMP1, LFUNC:TMP1->pc - | b <4 - |. lw KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE. - | - |7: // Tailcall from a vararg function. - | andi AT, TMP2, FRAME_TYPEP - | bnez AT, <1 // Vararg frame below? - |. subu TMP2, BASE, TMP2 // Relocate BASE down. - | move BASE, TMP2 - | lw TMP1, FRAME_PC(TMP2) - | b <1 - |. andi TMP0, TMP1, FRAME_TYPE - break; - - case BC_ITERC: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8)) - | move TMP2, BASE - | addu BASE, BASE, RA - | li AT, LJ_TFUNC - | lw TMP1, -24+HI(BASE) - | lw LFUNC:RB, -24+LO(BASE) - | ldc1 f2, -8(BASE) - | ldc1 f0, -16(BASE) - | sw TMP1, HI(BASE) // Copy callable. - | sw LFUNC:RB, LO(BASE) - | sdc1 f2, 16(BASE) // Copy control var. - | sdc1 f0, 8(BASE) // Copy state. - | addiu BASE, BASE, 8 - | bne TMP1, AT, ->vmeta_call - |. li NARGS8:RC, 16 // Iterators get 2 arguments. - | ins_call - break; - - case BC_ITERN: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8) - |.if JIT - | // NYI: add hotloop, record BC_ITERN. - |.endif - | addu RA, BASE, RA - | lw TAB:RB, -16+LO(RA) - | lw RC, -8+LO(RA) // Get index from control var. - | lw TMP0, TAB:RB->asize - | lw TMP1, TAB:RB->array - | addiu PC, PC, 4 - |1: // Traverse array part. - | sltu AT, RC, TMP0 - | beqz AT, >5 // Index points after array part? - |. sll TMP3, RC, 3 - | addu TMP3, TMP1, TMP3 - | lw TMP2, HI(TMP3) - | ldc1 f0, 0(TMP3) - | mtc1 RC, f2 - | lhu RD, -4+OFS_RD(PC) - | beq TMP2, TISNIL, <1 // Skip holes in array part. - |. addiu RC, RC, 1 - | cvt.d.w f2, f2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sdc1 f0, 8(RA) - | decode_RD4b RD - | addu RD, RD, TMP3 - | sw RC, -8+LO(RA) // Update control var. - | addu PC, PC, RD - | sdc1 f2, 0(RA) - |3: - | ins_next - | - |5: // Traverse hash part. - | lw TMP1, TAB:RB->hmask - | subu RC, RC, TMP0 - | lw TMP2, TAB:RB->node - |6: - | sltu AT, TMP1, RC // End of iteration? Branch to ITERL+1. - | bnez AT, <3 - |. sll TMP3, RC, 5 - | sll RB, RC, 3 - | subu TMP3, TMP3, RB - | addu NODE:TMP3, TMP3, TMP2 - | lw RB, HI(NODE:TMP3) - | ldc1 f0, 0(NODE:TMP3) - | lhu RD, -4+OFS_RD(PC) - | beq RB, TISNIL, <6 // Skip holes in hash part. - |. addiu RC, RC, 1 - | ldc1 f2, NODE:TMP3->key - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sdc1 f0, 8(RA) - | addu RC, RC, TMP0 - | decode_RD4b RD - | addu RD, RD, TMP3 - | sdc1 f2, 0(RA) - | addu PC, PC, RD - | b <3 - |. sw RC, -8+LO(RA) // Update control var. - break; - - case BC_ISNEXT: - | // RA = base*8, RD = target (points to ITERN) - | addu RA, BASE, RA - | lw TMP0, -24+HI(RA) - | lw CFUNC:TMP1, -24+LO(RA) - | lw TMP2, -16+HI(RA) - | lw TMP3, -8+HI(RA) - | li AT, LJ_TFUNC - | bne TMP0, AT, >5 - |. addiu TMP2, TMP2, -LJ_TTAB - | lbu TMP1, CFUNC:TMP1->ffid - | addiu TMP3, TMP3, -LJ_TNIL - | srl TMP0, RD, 1 - | or TMP2, TMP2, TMP3 - | addiu TMP1, TMP1, -FF_next_N - | addu TMP0, PC, TMP0 - | or TMP1, TMP1, TMP2 - | bnez TMP1, >5 - |. lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535) - | addu PC, TMP0, TMP2 - | lui TMP1, 0xfffe - | ori TMP1, TMP1, 0x7fff - | sw r0, -8+LO(RA) // Initialize control var. - | sw TMP1, -8+HI(RA) - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | li TMP3, BC_JMP - | li TMP1, BC_ITERC - | sb TMP3, -4+OFS_OP(PC) - | addu PC, TMP0, TMP2 - | b <1 - |. sb TMP1, OFS_OP(PC) - break; - - case BC_VARG: - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | lw TMP0, FRAME_PC(BASE) - | decode_RDtoRC8 RC, RD - | decode_RB8a RB, INS - | addu RC, BASE, RC - | decode_RB8b RB - | addu RA, BASE, RA - | addiu RC, RC, FRAME_VARG - | addu TMP2, RA, RB - | addiu TMP3, BASE, -8 // TMP3 = vtop - | subu RC, RC, TMP0 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | beqz RB, >5 // Copy all varargs? - |. subu TMP1, TMP3, RC - | addiu TMP2, TMP2, -16 - |1: // Copy vararg slots to destination slots. - | lw CARG1, HI(RC) - | sltu AT, RC, TMP3 - | lw CARG2, LO(RC) - | addiu RC, RC, 8 - | movz CARG1, TISNIL, AT - | sw CARG1, HI(RA) - | sw CARG2, LO(RA) - | sltu AT, RA, TMP2 - | bnez AT, <1 - |. addiu RA, RA, 8 - |3: - | ins_next - | - |5: // Copy all varargs. - | lw TMP0, L->maxstack - | blez TMP1, <3 // No vararg slots? - |. li MULTRES, 8 // MULTRES = (0+1)*8 - | addu TMP2, RA, TMP1 - | sltu AT, TMP0, TMP2 - | bnez AT, >7 - |. addiu MULTRES, TMP1, 8 - |6: - | ldc1 f0, 0(RC) - | addiu RC, RC, 8 - | sdc1 f0, 0(RA) - | sltu AT, RC, TMP3 - | bnez AT, <6 // More vararg slots? - |. addiu RA, RA, 8 - | b <3 - |. nop - | - |7: // Grow stack for varargs. - | load_got lj_state_growstack - | sw RA, L->top - | subu RA, RA, BASE - | sw BASE, L->base - | subu BASE, RC, BASE // Need delta, because BASE may change. - | sw PC, SAVE_PC - | srl CARG2, TMP1, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | move RC, BASE - | lw BASE, L->base - | addu RA, BASE, RA - | addu RC, BASE, RC - | b <6 - |. addiu TMP3, BASE, -8 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RD = extra_nresults*8 - | addu RD, RD, MULTRES // MULTRES >= 8, so RD >= 8. - | // Fall through. Assumes BC_RET follows. - break; - - case BC_RET: - | // RA = results*8, RD = (nresults+1)*8 - | lw PC, FRAME_PC(BASE) - | addu RA, BASE, RA - | move MULTRES, RD - |1: - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->BC_RETV_Z - |. xori TMP1, PC, FRAME_VARG - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return - | lw INS, -4(PC) - | addiu TMP2, BASE, -8 - | addiu RC, RD, -8 - | decode_RA8a TMP0, INS - | decode_RB8a RB, INS - | decode_RA8b TMP0 - | decode_RB8b RB - | addu TMP3, TMP2, RB - | beqz RC, >3 - |. subu BASE, TMP2, TMP0 - |2: - | ldc1 f0, 0(RA) - | addiu RA, RA, 8 - | addiu RC, RC, -8 - | sdc1 f0, 0(TMP2) - | bnez RC, <2 - |. addiu TMP2, TMP2, 8 - |3: - | addiu TMP3, TMP3, -8 - |5: - | sltu AT, TMP2, TMP3 - | bnez AT, >6 - |. lw LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lw TMP1, LFUNC:TMP1->pc - | lw KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | sw TISNIL, HI(TMP2) - | b <5 - |. addiu TMP2, TMP2, 8 - | - |->BC_RETV_Z: // Non-standard return case. - | andi TMP2, TMP1, FRAME_TYPEP - | bnez TMP2, ->vm_return - |. nop - | // Return from vararg function: relocate BASE down. - | subu BASE, BASE, TMP1 - | b <1 - |. lw PC, FRAME_PC(BASE) - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RD = (nresults+1)*8 - | lw PC, FRAME_PC(BASE) - | addu RA, BASE, RA - | move MULTRES, RD - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->BC_RETV_Z - |. xori TMP1, PC, FRAME_VARG - | - | lw INS, -4(PC) - | addiu TMP2, BASE, -8 - if (op == BC_RET1) { - | ldc1 f0, 0(RA) - } - | decode_RB8a RB, INS - | decode_RA8a RA, INS - | decode_RB8b RB - | decode_RA8b RA - if (op == BC_RET1) { - | sdc1 f0, 0(TMP2) - } - | subu BASE, TMP2, RA - |5: - | sltu AT, RD, RB - | bnez AT, >6 - |. lw LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lw TMP1, LFUNC:TMP1->pc - | lw KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | addiu TMP2, TMP2, 8 - | addiu RD, RD, 8 - | b <5 - if (op == BC_RET1) { - |. sw TISNIL, HI(TMP2) - } else { - |. sw TISNIL, -8+HI(TMP2) - } - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RD = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - | addu RA, BASE, RA - if (vk) { - | ldc1 f0, FORL_IDX*8(RA) - | ldc1 f4, FORL_STEP*8(RA) - | ldc1 f2, FORL_STOP*8(RA) - | lw TMP3, FORL_STEP*8+HI(RA) - | add.d f0, f0, f4 - | sdc1 f0, FORL_IDX*8(RA) - } else { - | lw TMP1, FORL_IDX*8+HI(RA) - | lw TMP3, FORL_STEP*8+HI(RA) - | lw TMP2, FORL_STOP*8+HI(RA) - | sltiu TMP1, TMP1, LJ_TISNUM - | sltiu TMP0, TMP3, LJ_TISNUM - | sltiu TMP2, TMP2, LJ_TISNUM - | and TMP1, TMP1, TMP0 - | and TMP1, TMP1, TMP2 - | ldc1 f0, FORL_IDX*8(RA) - | beqz TMP1, ->vmeta_for - |. ldc1 f2, FORL_STOP*8(RA) - } - if (op != BC_JFORL) { - | srl RD, RD, 1 - | lui TMP0, (-(BCBIAS_J*4 >> 16) & 65535) - } - | c.le.d 0, f0, f2 - | c.le.d 1, f2, f0 - | sdc1 f0, FORL_EXT*8(RA) - if (op == BC_JFORI) { - | li TMP1, 1 - | li TMP2, 1 - | addu TMP0, RD, TMP0 - | slt TMP3, TMP3, r0 - | movf TMP1, r0, 0 - | addu PC, PC, TMP0 - | movf TMP2, r0, 1 - | lhu RD, -4+OFS_RD(PC) - | movn TMP1, TMP2, TMP3 - | bnez TMP1, =>BC_JLOOP - |. decode_RD8b RD - } else if (op == BC_JFORL) { - | li TMP1, 1 - | li TMP2, 1 - | slt TMP3, TMP3, r0 - | movf TMP1, r0, 0 - | movf TMP2, r0, 1 - | movn TMP1, TMP2, TMP3 - | bnez TMP1, =>BC_JLOOP - |. nop - } else { - | addu TMP1, RD, TMP0 - | slt TMP3, TMP3, r0 - | move TMP2, TMP1 - if (op == BC_FORI) { - | movt TMP1, r0, 0 - | movt TMP2, r0, 1 - } else { - | movf TMP1, r0, 0 - | movf TMP2, r0, 1 - } - | movn TMP1, TMP2, TMP3 - | addu PC, PC, TMP1 - } - | ins_next - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RD = target - | addu RA, BASE, RA - | lw TMP1, HI(RA) - | beq TMP1, TISNIL, >1 // Stop if iterator returned nil. - |. lw TMP2, LO(RA) - if (op == BC_JITERL) { - | sw TMP1, -8+HI(RA) - | b =>BC_JLOOP - |. sw TMP2, -8+LO(RA) - } else { - | branch_RD // Otherwise save control var + branch. - | sw TMP1, -8+HI(RA) - | sw TMP2, -8+LO(RA) - } - |1: - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base*8 (ignored), RD = traceno*8 - | lw TMP1, DISPATCH_J(trace)(DISPATCH) - | srl RD, RD, 1 - | li AT, 0 - | addu TMP1, TMP1, RD - | // Traces on MIPS don't store the trace number, so use 0. - | sw AT, DISPATCH_GL(vmstate)(DISPATCH) - | lw TRACE:TMP2, 0(TMP1) - | sw BASE, DISPATCH_GL(jit_base)(DISPATCH) - | sw L, DISPATCH_GL(jit_L)(DISPATCH) - | lw TMP2, TRACE:TMP2->mcode - | jr TMP2 - |. addiu JGL, DISPATCH, GG_DISP2G+32768 - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RD = target - | branch_RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lw TMP2, L->maxstack - | lbu TMP1, -4+PC2PROTO(numparams)(PC) - | lw KBASE, -4+PC2PROTO(k)(PC) - | sltu AT, TMP2, RA - | bnez AT, ->vm_growstack_l - |. sll TMP1, TMP1, 3 - if (op != BC_JFUNCF) { - | ins_next1 - } - |2: - | sltu AT, NARGS8:RC, TMP1 // Check for missing parameters. - | bnez AT, >3 - |. addu AT, BASE, NARGS8:RC - if (op == BC_JFUNCF) { - | decode_RD8a RD, INS - | b =>BC_JLOOP - |. decode_RD8b RD - } else { - | ins_next2 - } - | - |3: // Clear missing parameters. - | sw TISNIL, HI(AT) - | b <2 - |. addiu NARGS8:RC, NARGS8:RC, 8 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | addu TMP1, BASE, RC - | lw TMP2, L->maxstack - | addu TMP0, RA, RC - | sw LFUNC:RB, LO(TMP1) // Store copy of LFUNC. - | addiu TMP3, RC, 8+FRAME_VARG - | sltu AT, TMP0, TMP2 - | lw KBASE, -4+PC2PROTO(k)(PC) - | beqz AT, ->vm_growstack_l - |. sw TMP3, HI(TMP1) // Store delta + FRAME_VARG. - | lbu TMP2, -4+PC2PROTO(numparams)(PC) - | move RA, BASE - | move RC, TMP1 - | ins_next1 - | beqz TMP2, >3 - |. addiu BASE, TMP1, 8 - |1: - | lw TMP0, HI(RA) - | lw TMP3, LO(RA) - | sltu AT, RA, RC // Less args than parameters? - | move CARG1, TMP0 - | movz TMP0, TISNIL, AT // Clear missing parameters. - | movn CARG1, TISNIL, AT // Clear old fixarg slot (help the GC). - | sw TMP3, 8+LO(TMP1) - | addiu TMP2, TMP2, -1 - | sw TMP0, 8+HI(TMP1) - | addiu TMP1, TMP1, 8 - | sw CARG1, HI(RA) - | bnez TMP2, <1 - |. addiu RA, RA, 8 - |3: - | ins_next2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | lw CFUNCADDR, CFUNC:RB->f - } else { - | lw CFUNCADDR, DISPATCH_GL(wrapf)(DISPATCH) - } - | addu TMP1, RA, NARGS8:RC - | lw TMP2, L->maxstack - | addu RC, BASE, NARGS8:RC - | sw BASE, L->base - | sltu AT, TMP2, TMP1 - | sw RC, L->top - | li_vmstate C - if (op == BC_FUNCCW) { - | lw CARG2, CFUNC:RB->f - } - | bnez AT, ->vm_growstack_c // Need to grow stack. - |. move CARG1, L - | jalr CFUNCADDR // (lua_State *L [, lua_CFunction f]) - |. st_vmstate - | // Returns nresults. - | lw BASE, L->base - | sll RD, CRET1, 3 - | lw TMP1, L->top - | li_vmstate INTERP - | lw PC, FRAME_PC(BASE) // Fetch PC of caller. - | subu RA, TMP1, RD // RA = L->top - nresults*8 - | b ->vm_returnc - |. st_vmstate - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.4byte .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.4byte 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 31\n" - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.4byte .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.4byte .Lframe0\n" - "\t.4byte .Lbegin\n" - "\t.4byte %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x9f\n\t.sleb128 1\n" - "\t.byte 0x9e\n\t.sleb128 2\n", - fcofs, CFRAME_SIZE); - for (i = 23; i >= 16; i--) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 26-i); - for (i = 30; i >= 20; i -= 2) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+32+i, 42-i); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.4byte .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.4byte .Lframe0\n" - "\t.4byte lj_vm_ffi_call\n" - "\t.4byte %d\n" - "\t.byte 0x9f\n\t.uleb128 1\n" - "\t.byte 0x90\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0x10\n" - "\t.align 2\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif - fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n"); - fprintf(ctx->fp, - "\t.globl lj_err_unwind_dwarf\n" - ".Lframe1:\n" - "\t.4byte .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.4byte 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 31\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0\n" - "\t.4byte lj_err_unwind_dwarf\n" - "\t.byte 0\n" - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.4byte .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.4byte .LASFDE2-.Lframe1\n" - "\t.4byte .Lbegin\n" - "\t.4byte %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x9f\n\t.sleb128 1\n" - "\t.byte 0x9e\n\t.sleb128 2\n", - fcofs, CFRAME_SIZE); - for (i = 23; i >= 16; i--) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 26-i); - for (i = 30; i >= 20; i -= 2) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+32+i, 42-i); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE2:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.4byte .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.4byte 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 31\n" - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0\n" - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.4byte .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.4byte .LASFDE3-.Lframe2\n" - "\t.4byte lj_vm_ffi_call\n" - "\t.4byte %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x9f\n\t.uleb128 1\n" - "\t.byte 0x90\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0x10\n" - "\t.align 2\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif - break; - default: - break; - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/vm_ppc.dasc b/third-party/LuaJIT-2.0.2/src/vm_ppc.dasc deleted file mode 100644 index f253081100..0000000000 --- a/third-party/LuaJIT-2.0.2/src/vm_ppc.dasc +++ /dev/null @@ -1,5137 +0,0 @@ -|// Low-level VM code for PowerPC CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -| -|.arch ppc -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// DynASM defines used by the PPC port: -|// -|// P64 64 bit pointers (only for GPR64 testing). -|// Note: a full PPC64 _LP64 port is not planned. -|// GPR64 64 bit registers (but possibly 32 bit pointers, e.g. PS3). -|// Affects reg saves, stack layout, carry/overflow/dot flags etc. -|// FRAME32 Use 32 bit frame layout, even with GPR64 (Xbox 360). -|// TOC Need table of contents (64 bit or 32 bit variant, e.g. PS3). -|// Function pointers are really a struct: code, TOC, env (optional). -|// TOCENV Function pointers have an environment pointer, too (not on PS3). -|// PPE Power Processor Element of Cell (PS3) or Xenon (Xbox 360). -|// Must avoid (slow) micro-coded instructions. -| -|.if P64 -|.define TOC, 1 -|.define TOCENV, 1 -|.macro lpx, a, b, c; ldx a, b, c; .endmacro -|.macro lp, a, b; ld a, b; .endmacro -|.macro stp, a, b; std a, b; .endmacro -|.define decode_OPP, decode_OP8 -|.if FFI -|// Missing: Calling conventions, 64 bit regs, TOC. -|.error lib_ffi not yet implemented for PPC64 -|.endif -|.else -|.macro lpx, a, b, c; lwzx a, b, c; .endmacro -|.macro lp, a, b; lwz a, b; .endmacro -|.macro stp, a, b; stw a, b; .endmacro -|.define decode_OPP, decode_OP4 -|.endif -| -|// Convenience macros for TOC handling. -|.if TOC -|// Linker needs a TOC patch area for every external call relocation. -|.macro blex, target; bl extern target@plt; nop; .endmacro -|.macro .toc, a, b; a, b; .endmacro -|.if P64 -|.define TOC_OFS, 8 -|.define ENV_OFS, 16 -|.else -|.define TOC_OFS, 4 -|.define ENV_OFS, 8 -|.endif -|.else // No TOC. -|.macro blex, target; bl extern target@plt; .endmacro -|.macro .toc, a, b; .endmacro -|.endif -|.macro .tocenv, a, b; .if TOCENV; a, b; .endif; .endmacro -| -|.macro .gpr64, a, b; .if GPR64; a, b; .endif; .endmacro -| -|.macro andix., y, a, i -|.if PPE -| rlwinm y, a, 0, 31-lj_fls(i), 31-lj_ffs(i) -| cmpwi y, 0 -|.else -| andi. y, a, i -|.endif -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -|// Don't use: r1 = sp, r2 and r13 = reserved (TOC, TLS or SDATA) -| -|// The following must be C callee-save (but BASE is often refetched). -|.define BASE, r14 // Base of current Lua stack frame. -|.define KBASE, r15 // Constants of current Lua function. -|.define PC, r16 // Next PC. -|.define DISPATCH, r17 // Opcode dispatch table. -|.define LREG, r18 // Register holding lua_State (also in SAVE_L). -|.define MULTRES, r19 // Size of multi-result: (nresults+1)*8. -|.define JGL, r31 // On-trace: global_State + 32768. -| -|// Constants for type-comparisons, stores and conversions. C callee-save. -|.define TISNUM, r22 -|.define TISNIL, r23 -|.define ZERO, r24 -|.define TOBIT, f30 // 2^52 + 2^51. -|.define TONUM, f31 // 2^52 + 2^51 + 2^31. -| -|// The following temporaries are not saved across C calls, except for RA. -|.define RA, r20 // Callee-save. -|.define RB, r10 -|.define RC, r11 -|.define RD, r12 -|.define INS, r7 // Overlaps CARG5. -| -|.define TMP0, r0 -|.define TMP1, r8 -|.define TMP2, r9 -|.define TMP3, r6 // Overlaps CARG4. -| -|// Saved temporaries. -|.define SAVE0, r21 -| -|// Calling conventions. -|.define CARG1, r3 -|.define CARG2, r4 -|.define CARG3, r5 -|.define CARG4, r6 // Overlaps TMP3. -|.define CARG5, r7 // Overlaps INS. -| -|.define FARG1, f1 -|.define FARG2, f2 -| -|.define CRET1, r3 -|.define CRET2, r4 -| -|.define TOCREG, r2 // TOC register (only used by C code). -|.define ENVREG, r11 // Environment pointer (nested C functions). -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.if GPR64 -|.if FRAME32 -| -|// 456(sp) // \ 32/64 bit C frame info -|.define TONUM_LO, 452(sp) // | -|.define TONUM_HI, 448(sp) // | -|.define TMPD_LO, 444(sp) // | -|.define TMPD_HI, 440(sp) // | -|.define SAVE_CR, 432(sp) // | 64 bit CR save. -|.define SAVE_ERRF, 424(sp) // > Parameter save area. -|.define SAVE_NRES, 420(sp) // | -|.define SAVE_L, 416(sp) // | -|.define SAVE_PC, 412(sp) // | -|.define SAVE_MULTRES, 408(sp) // | -|.define SAVE_CFRAME, 400(sp) // / 64 bit C frame chain. -|// 392(sp) // Reserved. -|.define CFRAME_SPACE, 384 // Delta for sp. -|// Back chain for sp: 384(sp) <-- sp entering interpreter -|.define SAVE_LR, 376(sp) // 32 bit LR stored in hi-part. -|.define SAVE_GPR_, 232 // .. 232+18*8: 64 bit GPR saves. -|.define SAVE_FPR_, 88 // .. 88+18*8: 64 bit FPR saves. -|// 80(sp) // Needed for 16 byte stack frame alignment. -|// 16(sp) // Callee parameter save area (ABI mandated). -|// 8(sp) // Reserved -|// Back chain for sp: 0(sp) <-- sp while in interpreter -|// 32 bit sp stored in hi-part of 0(sp). -| -|.define TMPD_BLO, 447(sp) -|.define TMPD, TMPD_HI -|.define TONUM_D, TONUM_HI -| -|.else -| -|// 508(sp) // \ 32 bit C frame info. -|.define SAVE_ERRF, 472(sp) // | -|.define SAVE_NRES, 468(sp) // | -|.define SAVE_L, 464(sp) // > Parameter save area. -|.define SAVE_PC, 460(sp) // | -|.define SAVE_MULTRES, 456(sp) // | -|.define SAVE_CFRAME, 448(sp) // / 64 bit C frame chain. -|.define SAVE_LR, 416(sp) -|.define CFRAME_SPACE, 400 // Delta for sp. -|// Back chain for sp: 400(sp) <-- sp entering interpreter -|.define SAVE_FPR_, 256 // .. 256+18*8: 64 bit FPR saves. -|.define SAVE_GPR_, 112 // .. 112+18*8: 64 bit GPR saves. -|// 48(sp) // Callee parameter save area (ABI mandated). -|.define SAVE_TOC, 40(sp) // TOC save area. -|.define TMPD_LO, 36(sp) // \ Link editor temp (ABI mandated). -|.define TMPD_HI, 32(sp) // / -|.define TONUM_LO, 28(sp) // \ Compiler temp (ABI mandated). -|.define TONUM_HI, 24(sp) // / -|// Next frame lr: 16(sp) -|.define SAVE_CR, 8(sp) // 64 bit CR save. -|// Back chain for sp: 0(sp) <-- sp while in interpreter -| -|.define TMPD_BLO, 39(sp) -|.define TMPD, TMPD_HI -|.define TONUM_D, TONUM_HI -| -|.endif -|.else -| -|.define SAVE_LR, 276(sp) -|.define CFRAME_SPACE, 272 // Delta for sp. -|// Back chain for sp: 272(sp) <-- sp entering interpreter -|.define SAVE_FPR_, 128 // .. 128+18*8: 64 bit FPR saves. -|.define SAVE_GPR_, 56 // .. 56+18*4: 32 bit GPR saves. -|.define SAVE_CR, 52(sp) // 32 bit CR save. -|.define SAVE_ERRF, 48(sp) // 32 bit C frame info. -|.define SAVE_NRES, 44(sp) -|.define SAVE_CFRAME, 40(sp) -|.define SAVE_L, 36(sp) -|.define SAVE_PC, 32(sp) -|.define SAVE_MULTRES, 28(sp) -|.define UNUSED1, 24(sp) -|.define TMPD_LO, 20(sp) -|.define TMPD_HI, 16(sp) -|.define TONUM_LO, 12(sp) -|.define TONUM_HI, 8(sp) -|// Next frame lr: 4(sp) -|// Back chain for sp: 0(sp) <-- sp while in interpreter -| -|.define TMPD_BLO, 23(sp) -|.define TMPD, TMPD_HI -|.define TONUM_D, TONUM_HI -| -|.endif -| -|.macro save_, reg -|.if GPR64 -| std r..reg, SAVE_GPR_+(reg-14)*8(sp) -|.else -| stw r..reg, SAVE_GPR_+(reg-14)*4(sp) -|.endif -| stfd f..reg, SAVE_FPR_+(reg-14)*8(sp) -|.endmacro -|.macro rest_, reg -|.if GPR64 -| ld r..reg, SAVE_GPR_+(reg-14)*8(sp) -|.else -| lwz r..reg, SAVE_GPR_+(reg-14)*4(sp) -|.endif -| lfd f..reg, SAVE_FPR_+(reg-14)*8(sp) -|.endmacro -| -|.macro saveregs -|.if GPR64 and not FRAME32 -| stdu sp, -CFRAME_SPACE(sp) -|.else -| stwu sp, -CFRAME_SPACE(sp) -|.endif -| save_ 14; save_ 15; save_ 16 -| mflr r0 -| save_ 17; save_ 18; save_ 19; save_ 20; save_ 21; save_ 22 -|.if GPR64 and not FRAME32 -| std r0, SAVE_LR -|.else -| stw r0, SAVE_LR -|.endif -| save_ 23; save_ 24; save_ 25 -| mfcr r0 -| save_ 26; save_ 27; save_ 28; save_ 29; save_ 30; save_ 31 -|.if GPR64 -| std r0, SAVE_CR -|.else -| stw r0, SAVE_CR -|.endif -| .toc std TOCREG, SAVE_TOC -|.endmacro -| -|.macro restoreregs -|.if GPR64 and not FRAME32 -| ld r0, SAVE_LR -|.else -| lwz r0, SAVE_LR -|.endif -|.if GPR64 -| ld r12, SAVE_CR -|.else -| lwz r12, SAVE_CR -|.endif -| rest_ 14; rest_ 15; rest_ 16; rest_ 17; rest_ 18; rest_ 19 -| mtlr r0; -|.if PPE; mtocrf 0x20, r12; .else; mtcrf 0x38, r12; .endif -| rest_ 20; rest_ 21; rest_ 22; rest_ 23; rest_ 24; rest_ 25 -|.if PPE; mtocrf 0x10, r12; .endif -| rest_ 26; rest_ 27; rest_ 28; rest_ 29; rest_ 30; rest_ 31 -|.if PPE; mtocrf 0x08, r12; .endif -| addi sp, sp, CFRAME_SPACE -|.endmacro -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -| -|//----------------------------------------------------------------------- -| -|// These basic macros should really be part of DynASM. -|.macro srwi, rx, ry, n; rlwinm rx, ry, 32-n, n, 31; .endmacro -|.macro slwi, rx, ry, n; rlwinm rx, ry, n, 0, 31-n; .endmacro -|.macro rotlwi, rx, ry, n; rlwinm rx, ry, n, 0, 31; .endmacro -|.macro rotlw, rx, ry, rn; rlwnm rx, ry, rn, 0, 31; .endmacro -|.macro subi, rx, ry, i; addi rx, ry, -i; .endmacro -| -|// Trap for not-yet-implemented parts. -|.macro NYI; tw 4, sp, sp; .endmacro -| -|// int/FP conversions. -|.macro tonum_i, freg, reg -| xoris reg, reg, 0x8000 -| stw reg, TONUM_LO -| lfd freg, TONUM_D -| fsub freg, freg, TONUM -|.endmacro -| -|.macro tonum_u, freg, reg -| stw reg, TONUM_LO -| lfd freg, TONUM_D -| fsub freg, freg, TOBIT -|.endmacro -| -|.macro toint, reg, freg, tmpfreg -| fctiwz tmpfreg, freg -| stfd tmpfreg, TMPD -| lwz reg, TMPD_LO -|.endmacro -| -|.macro toint, reg, freg -| toint reg, freg, freg -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Access to frame relative to BASE. -|.define FRAME_PC, -8 -|.define FRAME_FUNC, -4 -| -|// Instruction decode. -|.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro -|.macro decode_OP8, dst, ins; rlwinm dst, ins, 3, 21, 28; .endmacro -|.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro -|.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro -|.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro -|.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro -| -|.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro -|.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| lwz INS, 0(PC) -| addi PC, PC, 4 -|.endmacro -|// Instruction decode+dispatch. Note: optimized for e300! -|.macro ins_NEXT2 -| decode_OPP TMP1, INS -| lpx TMP0, DISPATCH, TMP1 -| mtctr TMP0 -| decode_RB8 RB, INS -| decode_RD8 RD, INS -| decode_RA8 RA, INS -| decode_RC8 RC, INS -| bctr -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| lwz PC, LFUNC:RB->pc -| lwz INS, 0(PC) -| addi PC, PC, 4 -| decode_OPP TMP1, INS -| decode_RA8 RA, INS -| lpx TMP0, DISPATCH, TMP1 -| add RA, RA, BASE -| mtctr TMP0 -| bctr -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| stw PC, FRAME_PC(BASE) -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to test operand types. -|.macro checknum, reg; cmplw reg, TISNUM; .endmacro -|.macro checknum, cr, reg; cmplw cr, reg, TISNUM; .endmacro -|.macro checkstr, reg; cmpwi reg, LJ_TSTR; .endmacro -|.macro checktab, reg; cmpwi reg, LJ_TTAB; .endmacro -|.macro checkfunc, reg; cmpwi reg, LJ_TFUNC; .endmacro -|.macro checknil, reg; cmpwi reg, LJ_TNIL; .endmacro -| -|.macro branch_RD -| srwi TMP0, RD, 1 -| addis PC, PC, -(BCBIAS_J*4 >> 16) -| add PC, PC, TMP0 -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro hotcheck, delta, target -| rlwinm TMP1, PC, 31, 25, 30 -| addi TMP1, TMP1, GG_DISP2HOT -| lhzx TMP2, DISPATCH, TMP1 -| addic. TMP2, TMP2, -delta -| sthx TMP2, DISPATCH, TMP1 -| blt target -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP, ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL, ->vm_hotcall -|.endmacro -| -|// Set current VM state. Uses TMP0. -|.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro -|.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp -| lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH) -| // Assumes LJ_GC_BLACK is 0x04. -| rlwinm mark, mark, 0, 30, 28 // black2gray(tab) -| stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH) -| stb mark, tab->marked -| stw tmp, tab->gclist -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: TMP2 = previous base. - | andix. TMP0, PC, FRAME_P - | li TMP1, LJ_TTRUE - | beq ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame. - | mr BASE, TMP2 // Restore caller base. - | // Prepending may overwrite the pcall frame, so do it at the end. - | stwu TMP1, FRAME_PC(RA) // Prepend true to results. - | - |->vm_returnc: - | addi RD, RD, 8 // RD = (nresults+1)*8. - | andix. TMP0, PC, FRAME_TYPE - | cmpwi cr1, RD, 0 - | li CRET1, LUA_YIELD - | beq cr1, ->vm_unwind_c_eh - | mr MULTRES, RD - | beq ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return - | // TMP0 = PC & FRAME_TYPE - | cmpwi TMP0, FRAME_C - | rlwinm TMP2, PC, 0, 0, 28 - | li_vmstate C - | sub TMP2, BASE, TMP2 // TMP2 = previous base. - | bney ->vm_returnp - | - | addic. TMP1, RD, -8 - | stp TMP2, L->base - | lwz TMP2, SAVE_NRES - | subi BASE, BASE, 8 - | st_vmstate - | slwi TMP2, TMP2, 3 - | beq >2 - |1: - | addic. TMP1, TMP1, -8 - | lfd f0, 0(RA) - | addi RA, RA, 8 - | stfd f0, 0(BASE) - | addi BASE, BASE, 8 - | bney <1 - | - |2: - | cmpw TMP2, RD // More/less results wanted? - | bne >6 - |3: - | stp BASE, L->top // Store new top. - | - |->vm_leave_cp: - | lp TMP0, SAVE_CFRAME // Restore previous C frame. - | li CRET1, 0 // Ok return status for vm_pcall. - | stp TMP0, L->cframe - | - |->vm_leave_unw: - | restoreregs - | blr - | - |6: - | ble >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | lwz TMP1, L->maxstack - | cmplw BASE, TMP1 - | bge >8 - | stw TISNIL, 0(BASE) - | addi RD, RD, 8 - | addi BASE, BASE, 8 - | b <2 - | - |7: // Less results wanted. - | subfic TMP3, TMP2, 0 // LUA_MULTRET+1 case? - | sub TMP0, RD, TMP2 - | subfe TMP1, TMP1, TMP1 // TMP1 = TMP2 == 0 ? 0 : -1 - | and TMP0, TMP0, TMP1 - | sub BASE, BASE, TMP0 // Either keep top or shrink it. - | b <3 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | stp BASE, L->top // Save current top held in BASE (yes). - | mr SAVE0, RD - | mr CARG2, TMP2 - | mr CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | lwz TMP2, SAVE_NRES - | mr RD, SAVE0 - | slwi TMP2, TMP2, 3 - | lp BASE, L->top // Need the (realloced) L->top in BASE. - | b <2 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | mr sp, CARG1 - | mr CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | lwz L, SAVE_L - | .toc ld TOCREG, SAVE_TOC - | li TMP0, ~LJ_VMST_C - | lwz GL:TMP1, L->glref - | stw TMP0, GL:TMP1->vmstate - | b ->vm_leave_unw - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - |.if GPR64 - | rldicr sp, CARG1, 0, 61 - |.else - | rlwinm sp, CARG1, 0, 0, 29 - |.endif - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | lwz L, SAVE_L - | .toc ld TOCREG, SAVE_TOC - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp BASE, L->base - | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | li ZERO, 0 - | stw TMP3, TMPD - | li TMP1, LJ_TFALSE - | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | li TISNIL, LJ_TNIL - | li_vmstate INTERP - | lfs TOBIT, TMPD - | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame. - | la RA, -8(BASE) // Results start at BASE-8. - | stw TMP3, TMPD - | addi DISPATCH, DISPATCH, GG_G2DISP - | stw TMP1, 0(RA) // Prepend false to error message. - | li RD, 16 // 2 results: false + error message. - | st_vmstate - | lfs TONUM, TMPD - | b ->vm_returnc - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | li CARG2, LUA_MINSTACK - | b >2 - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | add RC, BASE, RC - | sub RA, RA, BASE - | stp BASE, L->base - | addi PC, PC, 4 // Must point after first instruction. - | stp RC, L->top - | srwi CARG2, RA, 3 - |2: - | // L->base = new base, L->top = top - | stw PC, SAVE_PC - | mr CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | lp BASE, L->base - | lp RC, L->top - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | sub RC, RC, BASE - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | mr L, CARG1 - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | mr BASE, CARG2 - | lbz TMP1, L->status - | stw L, SAVE_L - | li PC, FRAME_CP - | addi TMP0, sp, CFRAME_RESUME - | addi DISPATCH, DISPATCH, GG_G2DISP - | stw CARG3, SAVE_NRES - | cmplwi TMP1, 0 - | stw CARG3, SAVE_ERRF - | stp TMP0, L->cframe - | stp CARG3, SAVE_CFRAME - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | beq >3 - | - | // Resume after yield (like a return). - | mr RA, BASE - | lp BASE, L->base - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp TMP1, L->top - | lwz PC, FRAME_PC(BASE) - | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | stb CARG3, L->status - | stw TMP3, TMPD - | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | lfs TOBIT, TMPD - | sub RD, TMP1, BASE - | stw TMP3, TMPD - | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | addi RD, RD, 8 - | stw TMP0, TONUM_HI - | li_vmstate INTERP - | li ZERO, 0 - | st_vmstate - | andix. TMP0, PC, FRAME_TYPE - | mr MULTRES, RD - | lfs TONUM, TMPD - | li TISNIL, LJ_TNIL - | beq ->BC_RET_Z - | b ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | li PC, FRAME_CP - | stw CARG4, SAVE_ERRF - | b >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | li PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | lp TMP1, L:CARG1->cframe - | stw CARG3, SAVE_NRES - | mr L, CARG1 - | stw CARG1, SAVE_L - | mr BASE, CARG2 - | stp sp, L->cframe // Add our C frame to cframe chain. - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | stp TMP1, SAVE_CFRAME - | addi DISPATCH, DISPATCH, GG_G2DISP - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | lp TMP2, L->base // TMP2 = old base (used in vmeta_call). - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp TMP1, L->top - | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | add PC, PC, BASE - | stw TMP3, TMPD - | li ZERO, 0 - | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | lfs TOBIT, TMPD - | sub PC, PC, TMP2 // PC = frame delta + frame type - | stw TMP3, TMPD - | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | sub NARGS8:RC, TMP1, BASE - | stw TMP0, TONUM_HI - | li_vmstate INTERP - | lfs TONUM, TMPD - | li TISNIL, LJ_TNIL - | st_vmstate - | - |->vm_call_dispatch: - | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC - | lwz TMP0, FRAME_PC(BASE) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | checkfunc TMP0; bne ->vmeta_call - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | mr L, CARG1 - | lwz TMP0, L:CARG1->stack - | stw CARG1, SAVE_L - | lp TMP1, L->top - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top). - | lp TMP1, L->cframe - | stp sp, L->cframe // Add our C frame to cframe chain. - | .toc lp CARG4, 0(CARG4) - | li TMP2, 0 - | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame. - | stw TMP2, SAVE_ERRF // No error function. - | stp TMP1, SAVE_CFRAME - | mtctr CARG4 - | bctrl // (lua_State *L, lua_CFunction func, void *ud) - |.if PPE - | mr BASE, CRET1 - | cmpwi CRET1, 0 - |.else - | mr. BASE, CRET1 - |.endif - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | li PC, FRAME_CP - | addi DISPATCH, DISPATCH, GG_G2DISP - | bne <3 // Else continue with the call. - | b ->vm_leave_cp // No base? Just remove C frame. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the - |// stack, so BASE doesn't need to be reloaded across these calls. - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8 - | lwz TMP0, -12(BASE) // Continuation. - | mr RB, BASE - | mr BASE, TMP2 // Restore caller BASE. - | lwz LFUNC:TMP1, FRAME_FUNC(TMP2) - |.if FFI - | cmplwi TMP0, 1 - |.endif - | lwz PC, -16(RB) // Restore PC from [cont|PC]. - | subi TMP2, RD, 8 - | lwz TMP1, LFUNC:TMP1->pc - | stwx TISNIL, RA, TMP2 // Ensure one valid arg. - |.if FFI - | ble >1 - |.endif - | lwz KBASE, PC2PROTO(k)(TMP1) - | // BASE = base, RA = resultptr, RB = meta base - | mtctr TMP0 - | bctr // Jump to continuation. - | - |.if FFI - |1: - | beq ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - | subi TMP1, RB, 16 - | sub RC, TMP1, BASE - | b ->vm_call_tail - |.endif - | - |->cont_cat: // RA = resultptr, RB = meta base - | lwz INS, -4(PC) - | subi CARG2, RB, 16 - | decode_RB8 SAVE0, INS - | lfd f0, 0(RA) - | add TMP1, BASE, SAVE0 - | stp BASE, L->base - | cmplw TMP1, CARG2 - | sub CARG3, CARG2, TMP1 - | decode_RA8 RA, INS - | stfd f0, 0(CARG2) - | bney ->BC_CAT_Z - | stfdx f0, BASE, RA - | b ->cont_nop - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TSTR - | decode_RB8 RB, INS - | stw STR:RC, 4(CARG3) - | add CARG2, BASE, RB - | stw TMP0, 0(CARG3) - | b >1 - | - |->vmeta_tgets: - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TTAB - | stw TAB:RB, 4(CARG2) - | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH) - | stw TMP0, 0(CARG2) - | li TMP1, LJ_TSTR - | stw STR:RC, 4(CARG3) - | stw TMP1, 0(CARG3) - | b >1 - | - |->vmeta_tgetb: // TMP0 = index - |.if not DUALNUM - | tonum_u f0, TMP0 - |.endif - | decode_RB8 RB, INS - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | add CARG2, BASE, RB - |.if DUALNUM - | stw TISNUM, 0(CARG3) - | stw TMP0, 4(CARG3) - |.else - | stfd f0, 0(CARG3) - |.endif - | b >1 - | - |->vmeta_tgetv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | cmplwi CRET1, 0 - | beq >3 - | lfd f0, 0(CRET1) - | ins_next1 - | stfdx f0, BASE, RA - | ins_next2 - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | subfic TMP1, BASE, FRAME_CONT - | lp BASE, L->top - | stw PC, -16(BASE) // [cont|PC] - | add PC, TMP1, BASE - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | li NARGS8:RC, 16 // 2 args for func(t, k). - | b ->vm_call_dispatch_f - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TSTR - | decode_RB8 RB, INS - | stw STR:RC, 4(CARG3) - | add CARG2, BASE, RB - | stw TMP0, 0(CARG3) - | b >1 - | - |->vmeta_tsets: - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TTAB - | stw TAB:RB, 4(CARG2) - | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH) - | stw TMP0, 0(CARG2) - | li TMP1, LJ_TSTR - | stw STR:RC, 4(CARG3) - | stw TMP1, 0(CARG3) - | b >1 - | - |->vmeta_tsetb: // TMP0 = index - |.if not DUALNUM - | tonum_u f0, TMP0 - |.endif - | decode_RB8 RB, INS - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | add CARG2, BASE, RB - |.if DUALNUM - | stw TISNUM, 0(CARG3) - | stw TMP0, 4(CARG3) - |.else - | stfd f0, 0(CARG3) - |.endif - | b >1 - | - |->vmeta_tsetv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | cmplwi CRET1, 0 - | lfdx f0, BASE, RA - | beq >3 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | ins_next1 - | stfd f0, 0(CRET1) - | ins_next2 - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | subfic TMP1, BASE, FRAME_CONT - | lp BASE, L->top - | stw PC, -16(BASE) // [cont|PC] - | add PC, TMP1, BASE - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | li NARGS8:RC, 24 // 3 args for func(t, k, v) - | stfd f0, 16(BASE) // Copy value to third argument. - | b ->vm_call_dispatch_f - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | mr CARG1, L - | subi PC, PC, 4 - |.if DUALNUM - | mr CARG2, RA - |.else - | add CARG2, BASE, RA - |.endif - | stw PC, SAVE_PC - |.if DUALNUM - | mr CARG3, RD - |.else - | add CARG3, BASE, RD - |.endif - | stp BASE, L->base - | decode_OP1 CARG4, INS - | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // Returns 0/1 or TValue * (metamethod). - |3: - | cmplwi CRET1, 1 - | bgt ->vmeta_binop - | subfic CRET1, CRET1, 0 - |4: - | lwz INS, 0(PC) - | addi PC, PC, 4 - | decode_RD4 TMP2, INS - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | and TMP2, TMP2, CRET1 - | add PC, PC, TMP2 - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | lwz INS, -4(PC) - | lfd f0, 0(RA) - | decode_RA8 TMP1, INS - | stfdx f0, BASE, TMP1 - | b ->cont_nop - | - |->cont_condt: // RA = resultptr - | lwz TMP0, 0(RA) - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is true. - | subfe CRET1, CRET1, CRET1 - | not CRET1, CRET1 - | b <4 - | - |->cont_condf: // RA = resultptr - | lwz TMP0, 0(RA) - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is false. - | subfe CRET1, CRET1, CRET1 - | b <4 - | - |->vmeta_equal: - | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. - | subi PC, PC, 4 - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - | - |->vmeta_equal_cd: - |.if FFI - | mr CARG2, INS - | subi PC, PC, 4 - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |.endif - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_nv: - | add CARG3, KBASE, RC - | add CARG4, BASE, RB - | b >1 - |->vmeta_arith_nv2: - |.if DUALNUM - | mr CARG3, RC - | mr CARG4, RB - | b >1 - |.endif - | - |->vmeta_unm: - | mr CARG3, RD - | mr CARG4, RD - | b >1 - | - |->vmeta_arith_vn: - | add CARG3, BASE, RB - | add CARG4, KBASE, RC - | b >1 - | - |->vmeta_arith_vv: - | add CARG3, BASE, RB - | add CARG4, BASE, RC - |.if DUALNUM - | b >1 - |.endif - |->vmeta_arith_vn2: - |->vmeta_arith_vv2: - |.if DUALNUM - | mr CARG3, RB - | mr CARG4, RC - |.endif - |1: - | add CARG2, BASE, RA - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS. - | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // Returns NULL (finished) or TValue * (metamethod). - | cmplwi CRET1, 0 - | beq ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | sub TMP1, CRET1, BASE - | stw PC, -16(CRET1) // [cont|PC] - | mr TMP2, BASE - | addi PC, TMP1, FRAME_CONT - | mr BASE, CRET1 - | li NARGS8:RC, 16 // 2 args for func(o1, o2). - | b ->vm_call_dispatch - | - |->vmeta_len: -#if LJ_52 - | mr SAVE0, CARG1 -#endif - | mr CARG2, RD - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_len // (lua_State *L, TValue *o) - | // Returns NULL (retry) or TValue * (metamethod base). -#if LJ_52 - | cmplwi CRET1, 0 - | bne ->vmeta_binop // Binop call for compatibility. - | mr CARG1, SAVE0 - | b ->BC_LEN_Z -#else - | b ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // TMP2 = old base, BASE = new base, RC = nargs*8 - | mr CARG1, L - | stp TMP2, L->base // This is the callers base! - | subi CARG2, BASE, 8 - | stw PC, SAVE_PC - | add CARG3, BASE, RC - | mr SAVE0, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | addi NARGS8:RC, SAVE0, 8 // Got one more argument now. - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | mr CARG1, L - | stp BASE, L->base - | subi CARG2, RA, 8 - | stw PC, SAVE_PC - | add CARG3, RA, RC - | mr SAVE0, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | lwz TMP1, FRAME_PC(BASE) - | addi NARGS8:RC, SAVE0, 8 // Got one more argument now. - | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here. - | b ->BC_CALLT_Z - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mr CARG1, L - | stp BASE, L->base - | mr CARG2, RA - | stw PC, SAVE_PC - | mr SAVE0, INS - | bl extern lj_meta_for // (lua_State *L, TValue *base) - |.if JIT - | decode_OP1 TMP0, SAVE0 - |.endif - | decode_RA8 RA, SAVE0 - |.if JIT - | cmpwi TMP0, BC_JFORI - |.endif - | decode_RD8 RD, SAVE0 - |.if JIT - | beqy =>BC_JFORI - |.endif - | b =>BC_FORI - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lwz CARG1, 4(BASE) - | blt ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 0(BASE) - | lwz CARG4, 8(BASE) - | lwz CARG1, 4(BASE) - | lwz CARG2, 12(BASE) - | blt ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lfd FARG1, 0(BASE) - | blt ->fff_fallback - | checknum CARG3; bge ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 0(BASE) - | lfd FARG1, 0(BASE) - | lwz CARG4, 8(BASE) - | lfd FARG2, 8(BASE) - | blt ->fff_fallback - | checknum CARG3; bge ->fff_fallback - | checknum CARG4; bge ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1. - |.macro ffgccheck - | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | cmplw TMP0, TMP1 - | bgel ->fff_gcstep - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | li TMP1, LJ_TFALSE - | la RA, -8(BASE) - | cmplw cr1, CARG3, TMP1 - | lwz PC, FRAME_PC(BASE) - | bge cr1, ->fff_fallback - | stw CARG3, 0(RA) - | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8. - | stw CARG1, 4(RA) - | beq ->fff_res // Done if exactly 1 argument. - | li TMP1, 8 - | subi RC, RC, 8 - |1: - | cmplw TMP1, RC - | lfdx f0, BASE, TMP1 - | stfdx f0, RA, TMP1 - | addi TMP1, TMP1, 8 - | bney <1 - | b ->fff_res - | - |.ffunc type - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - | blt ->fff_fallback - | .gpr64 extsw CARG1, CARG1 - | subfc TMP0, TISNUM, CARG1 - | subfe TMP2, CARG1, CARG1 - | orc TMP1, TMP2, TMP0 - | addi TMP1, TMP1, ~LJ_TISNUM+1 - | slwi TMP1, TMP1, 3 - | la TMP2, CFUNC:RB->upvalue - | lfdx FARG1, TMP2, TMP1 - | b ->fff_resn - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | checktab CARG3; bne >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | lwz TAB:CARG1, TAB:CARG1->metatable - |2: - | li CARG3, LJ_TNIL - | cmplwi TAB:CARG1, 0 - | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH) - | beq ->fff_restv - | lwz TMP0, TAB:CARG1->hmask - | li CARG3, LJ_TTAB // Use metatable as default result. - | lwz TMP1, STR:RC->hash - | lwz NODE:TMP2, TAB:CARG1->node - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |3: // Rearranged logic, because we expect _not_ to find the key. - | lwz CARG4, NODE:TMP2->key - | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2) - | lwz CARG2, NODE:TMP2->val - | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2) - | checkstr CARG4; bne >4 - | cmpw TMP0, STR:RC; beq >5 - |4: - | lwz NODE:TMP2, NODE:TMP2->next - | cmplwi NODE:TMP2, 0 - | beq ->fff_restv // Not found, keep default result. - | b <3 - |5: - | checknil CARG2 - | beq ->fff_restv // Ditto for nil value. - | mr CARG3, CARG2 // Return value of mt.__metatable. - | mr CARG1, TMP1 - | b ->fff_restv - | - |6: - | cmpwi CARG3, LJ_TUDATA; beq <1 - | .gpr64 extsw CARG3, CARG3 - | subfc TMP0, TISNUM, CARG3 - | subfe TMP2, CARG3, CARG3 - | orc TMP1, TMP2, TMP0 - | addi TMP1, TMP1, ~LJ_TISNUM+1 - | slwi TMP1, TMP1, 2 - | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH) - | lwzx TAB:CARG1, TMP2, TMP1 - | b <2 - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | checktab CARG3; bne ->fff_fallback - | lwz TAB:TMP1, TAB:CARG1->metatable - | checktab CARG4; bne ->fff_fallback - | cmplwi TAB:TMP1, 0 - | lbz TMP3, TAB:CARG1->marked - | bne ->fff_fallback - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - | stw TAB:CARG2, TAB:CARG1->metatable - | beq ->fff_restv - | barrierback TAB:CARG1, TMP3, TMP0 - | b ->fff_restv - | - |.ffunc rawget - | cmplwi NARGS8:RC, 16 - | lwz CARG4, 0(BASE) - | lwz TAB:CARG2, 4(BASE) - | blt ->fff_fallback - | checktab CARG4; bne ->fff_fallback - | la CARG3, 8(BASE) - | mr CARG1, L - | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // Returns cTValue *. - | lfd FARG1, 0(CRET1) - | b ->fff_resn - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - | lfd FARG1, 0(BASE) - | bne ->fff_fallback // Exactly one argument. - | checknum CARG1; bgt ->fff_fallback - | b ->fff_resn - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | checkstr CARG3 - | // A __tostring method in the string base metatable is ignored. - | beq ->fff_restv // String key? - | // Handle numbers inline, unless a number base metatable is present. - | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH) - | checknum CARG3 - | cmplwi cr1, TMP0, 0 - | stp BASE, L->base // Add frame since C call can throw. - | crorc 4*cr0+eq, 4*cr0+gt, 4*cr1+eq - | stw PC, SAVE_PC // Redundant (but a defined value). - | beq ->fff_fallback - | ffgccheck - | mr CARG1, L - | mr CARG2, BASE - |.if DUALNUM - | bl extern lj_str_fromnumber // (lua_State *L, cTValue *o) - |.else - | bl extern lj_str_fromnum // (lua_State *L, lua_Number *np) - |.endif - | // Returns GCstr *. - | li CARG3, LJ_TSTR - | b ->fff_restv - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc next - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - | lwz TAB:CARG2, 4(BASE) - | blt ->fff_fallback - | stwx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil. - | checktab CARG1 - | lwz PC, FRAME_PC(BASE) - | bne ->fff_fallback - | stp BASE, L->base // Add frame since C call can throw. - | mr CARG1, L - | stp BASE, L->top // Dummy frame length is ok. - | la CARG3, 8(BASE) - | stw PC, SAVE_PC - | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key) - | // Returns 0 at end of traversal. - | cmplwi CRET1, 0 - | li CARG3, LJ_TNIL - | beq ->fff_restv // End of traversal: return nil. - | lfd f0, 8(BASE) // Copy key and value to results. - | la RA, -8(BASE) - | lfd f1, 16(BASE) - | stfd f0, 0(RA) - | li RD, (2+1)*8 - | stfd f1, 8(RA) - | b ->fff_res - | - |.ffunc_1 pairs - | checktab CARG3 - | lwz PC, FRAME_PC(BASE) - | bne ->fff_fallback -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - | lfd f0, CFUNC:RB->upvalue[0] - | cmplwi TAB:TMP2, 0 - | la RA, -8(BASE) - | bne ->fff_fallback -#else - | lfd f0, CFUNC:RB->upvalue[0] - | la RA, -8(BASE) -#endif - | stw TISNIL, 8(BASE) - | li RD, (3+1)*8 - | stfd f0, 0(RA) - | b ->fff_res - | - |.ffunc ipairs_aux - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 0(BASE) - | lwz TAB:CARG1, 4(BASE) - | lwz CARG4, 8(BASE) - |.if DUALNUM - | lwz TMP2, 12(BASE) - |.else - | lfd FARG2, 8(BASE) - |.endif - | blt ->fff_fallback - | checktab CARG3 - | checknum cr1, CARG4 - | lwz PC, FRAME_PC(BASE) - |.if DUALNUM - | bne ->fff_fallback - | bne cr1, ->fff_fallback - |.else - | lus TMP0, 0x3ff0 - | stw ZERO, TMPD_LO - | bne ->fff_fallback - | stw TMP0, TMPD_HI - | bge cr1, ->fff_fallback - | lfd FARG1, TMPD - | toint TMP2, FARG2, f0 - |.endif - | lwz TMP0, TAB:CARG1->asize - | lwz TMP1, TAB:CARG1->array - |.if not DUALNUM - | fadd FARG2, FARG2, FARG1 - |.endif - | addi TMP2, TMP2, 1 - | la RA, -8(BASE) - | cmplw TMP0, TMP2 - |.if DUALNUM - | stw TISNUM, 0(RA) - | slwi TMP3, TMP2, 3 - | stw TMP2, 4(RA) - |.else - | slwi TMP3, TMP2, 3 - | stfd FARG2, 0(RA) - |.endif - | ble >2 // Not in array part? - | lwzx TMP2, TMP1, TMP3 - | lfdx f0, TMP1, TMP3 - |1: - | checknil TMP2 - | li RD, (0+1)*8 - | beq ->fff_res // End of iteration, return 0 results. - | li RD, (2+1)*8 - | stfd f0, 8(RA) - | b ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | lwz TMP0, TAB:CARG1->hmask - | cmplwi TMP0, 0 - | li RD, (0+1)*8 - | beq ->fff_res - | mr CARG2, TMP2 - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | cmplwi CRET1, 0 - | li RD, (0+1)*8 - | beq ->fff_res - | lwz TMP2, 0(CRET1) - | lfd f0, 0(CRET1) - | b <1 - | - |.ffunc_1 ipairs - | checktab CARG3 - | lwz PC, FRAME_PC(BASE) - | bne ->fff_fallback -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - | lfd f0, CFUNC:RB->upvalue[0] - | cmplwi TAB:TMP2, 0 - | la RA, -8(BASE) - | bne ->fff_fallback -#else - | lfd f0, CFUNC:RB->upvalue[0] - | la RA, -8(BASE) -#endif - |.if DUALNUM - | stw TISNUM, 8(BASE) - |.else - | stw ZERO, 8(BASE) - |.endif - | stw ZERO, 12(BASE) - | li RD, (3+1)*8 - | stfd f0, 0(RA) - | b ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | cmplwi NARGS8:RC, 8 - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | blt ->fff_fallback - | mr TMP2, BASE - | la BASE, 8(BASE) - | // Remember active hook before pcall. - | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31 - | subi NARGS8:RC, NARGS8:RC, 8 - | addi PC, TMP3, 8+FRAME_PCALL - | b ->vm_call_dispatch - | - |.ffunc xpcall - | cmplwi NARGS8:RC, 16 - | lwz CARG4, 8(BASE) - | lfd FARG2, 8(BASE) - | lfd FARG1, 0(BASE) - | blt ->fff_fallback - | lbz TMP1, DISPATCH_GL(hookmask)(DISPATCH) - | mr TMP2, BASE - | checkfunc CARG4; bne ->fff_fallback // Traceback must be a function. - | la BASE, 16(BASE) - | // Remember active hook before pcall. - | rlwinm TMP1, TMP1, 32-HOOK_ACTIVE_SHIFT, 31, 31 - | stfd FARG2, 0(TMP2) // Swap function and traceback. - | subi NARGS8:RC, NARGS8:RC, 16 - | stfd FARG1, 8(TMP2) - | addi PC, TMP1, 16+FRAME_PCALL - | b ->vm_call_dispatch - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | cmpwi CARG3, LJ_TTHREAD; bne ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr - |.endif - | lbz TMP0, L:CARG1->status - | lp TMP1, L:CARG1->cframe - | lp CARG2, L:CARG1->top - | cmplwi cr0, TMP0, LUA_YIELD - | lp TMP2, L:CARG1->base - | cmplwi cr1, TMP1, 0 - | lwz TMP0, L:CARG1->maxstack - | cmplw cr7, CARG2, TMP2 - | lwz PC, FRAME_PC(BASE) - | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0 - | add TMP2, CARG2, NARGS8:RC - | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD - | cmplw cr1, TMP2, TMP0 - | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt - | stw PC, SAVE_PC - | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov - | stp BASE, L->base - | blt cr6, ->fff_fallback - |1: - |.if resume - | addi BASE, BASE, 8 // Keep resumed thread in stack for GC. - | subi NARGS8:RC, NARGS8:RC, 8 - | subi TMP2, TMP2, 8 - |.endif - | stp TMP2, L:CARG1->top - | li TMP1, 0 - | stp BASE, L->top - |2: // Move args to coroutine. - | cmpw TMP1, NARGS8:RC - | lfdx f0, BASE, TMP1 - | beq >3 - | stfdx f0, CARG2, TMP1 - | addi TMP1, TMP1, 8 - | b <2 - |3: - | li CARG3, 0 - | mr L:SAVE0, L:CARG1 - | li CARG4, 0 - | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | // Returns thread status. - |4: - | lp TMP2, L:SAVE0->base - | cmplwi CRET1, LUA_YIELD - | lp TMP3, L:SAVE0->top - | li_vmstate INTERP - | lp BASE, L->base - | st_vmstate - | bgt >8 - | sub RD, TMP3, TMP2 - | lwz TMP0, L->maxstack - | cmplwi RD, 0 - | add TMP1, BASE, RD - | beq >6 // No results? - | cmplw TMP1, TMP0 - | li TMP1, 0 - | bgt >9 // Need to grow stack? - | - | subi TMP3, RD, 8 - | stp TMP2, L:SAVE0->top // Clear coroutine stack. - |5: // Move results from coroutine. - | cmplw TMP1, TMP3 - | lfdx f0, TMP2, TMP1 - | stfdx f0, BASE, TMP1 - | addi TMP1, TMP1, 8 - | bne <5 - |6: - | andix. TMP0, PC, FRAME_TYPE - |.if resume - | li TMP1, LJ_TTRUE - | la RA, -8(BASE) - | stw TMP1, -8(BASE) // Prepend true to results. - | addi RD, RD, 16 - |.else - | mr RA, BASE - | addi RD, RD, 8 - |.endif - |7: - | stw PC, SAVE_PC - | mr MULTRES, RD - | beq ->BC_RET_Z - | b ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | andix. TMP0, PC, FRAME_TYPE - | la TMP3, -8(TMP3) - | li TMP1, LJ_TFALSE - | lfd f0, 0(TMP3) - | stp TMP3, L:SAVE0->top // Remove error from coroutine stack. - | li RD, (2+1)*8 - | stw TMP1, -8(BASE) // Prepend false to results. - | la RA, -8(BASE) - | stfd f0, 0(BASE) // Copy error message. - | b <7 - |.else - | mr CARG1, L - | mr CARG2, L:SAVE0 - | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - |.endif - | - |9: // Handle stack expansion on return from yield. - | mr CARG1, L - | srwi CARG2, RD, 3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | li CRET1, 0 - | b <4 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | lp TMP0, L->cframe - | add TMP1, BASE, NARGS8:RC - | stp BASE, L->base - | andix. TMP0, TMP0, CFRAME_RESUME - | stp TMP1, L->top - | li CRET1, LUA_YIELD - | beq ->fff_fallback - | stp ZERO, L->cframe - | stb CRET1, L->status - | b ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.ffunc_1 math_abs - | checknum CARG3 - |.if DUALNUM - | bne >2 - | srawi TMP1, CARG1, 31 - | xor TMP2, TMP1, CARG1 - |.if GPR64 - | lus TMP0, 0x8000 - | sub CARG1, TMP2, TMP1 - | cmplw CARG1, TMP0 - | beq >1 - |.else - | sub. CARG1, TMP2, TMP1 - | blt >1 - |.endif - |->fff_resi: - | lwz PC, FRAME_PC(BASE) - | la RA, -8(BASE) - | stw TISNUM, -8(BASE) - | stw CRET1, -4(BASE) - | b ->fff_res1 - |1: - | lus CARG3, 0x41e0 // 2^31. - | li CARG1, 0 - | b ->fff_restv - |2: - |.endif - | bge ->fff_fallback - | rlwinm CARG3, CARG3, 0, 1, 31 - | // Fallthrough. - | - |->fff_restv: - | // CARG3/CARG1 = TValue result. - | lwz PC, FRAME_PC(BASE) - | stw CARG3, -8(BASE) - | la RA, -8(BASE) - | stw CARG1, -4(BASE) - |->fff_res1: - | // RA = results, PC = return. - | li RD, (1+1)*8 - |->fff_res: - | // RA = results, RD = (nresults+1)*8, PC = return. - | andix. TMP0, PC, FRAME_TYPE - | mr MULTRES, RD - | bney ->vm_return - | lwz INS, -4(PC) - | decode_RB8 RB, INS - |5: - | cmplw RB, RD // More results expected? - | decode_RA8 TMP0, INS - | bgt >6 - | ins_next1 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | sub BASE, RA, TMP0 - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | stwx TISNIL, RA, TMP1 - | b <5 - | - |.macro math_extern, func - | .ffunc_n math_ .. func - | blex func - | b ->fff_resn - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nn math_ .. func - | blex func - | b ->fff_resn - |.endmacro - | - |.macro math_round, func - | .ffunc_1 math_ .. func - | checknum CARG3; beqy ->fff_restv - | rlwinm TMP2, CARG3, 12, 21, 31 - | bge ->fff_fallback - | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023 - | cmplwi cr1, TMP2, 31 // 0 <= exp < 31? - | subfic TMP0, TMP2, 31 - | blt >3 - | slwi TMP1, CARG3, 11 - | srwi TMP3, CARG1, 21 - | oris TMP1, TMP1, 0x8000 - | addi TMP2, TMP2, 1 - | or TMP1, TMP1, TMP3 - | slwi CARG2, CARG1, 11 - | bge cr1, >4 - | slw TMP3, TMP1, TMP2 - | srw RD, TMP1, TMP0 - | or TMP3, TMP3, CARG2 - | srawi TMP2, CARG3, 31 - |.if "func" == "floor" - | and TMP1, TMP3, TMP2 - | addic TMP0, TMP1, -1 - | subfe TMP1, TMP0, TMP1 - | add CARG1, RD, TMP1 - | xor CARG1, CARG1, TMP2 - | sub CARG1, CARG1, TMP2 - | b ->fff_resi - |.else - | andc TMP1, TMP3, TMP2 - | addic TMP0, TMP1, -1 - | subfe TMP1, TMP0, TMP1 - | add CARG1, RD, TMP1 - | cmpw CARG1, RD - | xor CARG1, CARG1, TMP2 - | sub CARG1, CARG1, TMP2 - | bge ->fff_resi - | // Overflow to 2^31. - | lus CARG3, 0x41e0 // 2^31. - | li CARG1, 0 - | b ->fff_restv - |.endif - |3: // |x| < 1 - | slwi TMP2, CARG3, 1 - | srawi TMP1, CARG3, 31 - | or TMP2, CARG1, TMP2 // ztest = (hi+hi) | lo - |.if "func" == "floor" - | and TMP1, TMP2, TMP1 // (ztest & sign) == 0 ? 0 : -1 - | subfic TMP2, TMP1, 0 - | subfe CARG1, CARG1, CARG1 - |.else - | andc TMP1, TMP2, TMP1 // (ztest & ~sign) == 0 ? 0 : 1 - | addic TMP2, TMP1, -1 - | subfe CARG1, TMP2, TMP1 - |.endif - | b ->fff_resi - |4: // exp >= 31. Check for -(2^31). - | xoris TMP1, TMP1, 0x8000 - | srawi TMP2, CARG3, 31 - |.if "func" == "floor" - | or TMP1, TMP1, CARG2 - |.endif - |.if PPE - | orc TMP1, TMP1, TMP2 - | cmpwi TMP1, 0 - |.else - | orc. TMP1, TMP1, TMP2 - |.endif - | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | lus CARG1, 0x8000 // -(2^31). - | beqy ->fff_resi - |5: - | lfd FARG1, 0(BASE) - | blex func - | b ->fff_resn - |.endmacro - | - |.if DUALNUM - | math_round floor - | math_round ceil - |.else - | // NYI: use internal implementation. - | math_extern floor - | math_extern ceil - |.endif - | - |.if SQRT - |.ffunc_n math_sqrt - | fsqrt FARG1, FARG1 - | b ->fff_resn - |.else - | math_extern sqrt - |.endif - | - |.ffunc math_log - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lfd FARG1, 0(BASE) - | bne ->fff_fallback // Need exactly 1 argument. - | checknum CARG3; bge ->fff_fallback - | blex log - | b ->fff_resn - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |->ff_math_deg: - |.ffunc_n math_rad - | lfd FARG2, CFUNC:RB->upvalue[0] - | fmul FARG1, FARG1, FARG2 - | b ->fff_resn - | - |.if DUALNUM - |.ffunc math_ldexp - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 0(BASE) - | lfd FARG1, 0(BASE) - | lwz CARG4, 8(BASE) - |.if GPR64 - | lwz CARG2, 12(BASE) - |.else - | lwz CARG1, 12(BASE) - |.endif - | blt ->fff_fallback - | checknum CARG3; bge ->fff_fallback - | checknum CARG4; bne ->fff_fallback - |.else - |.ffunc_nn math_ldexp - |.if GPR64 - | toint CARG2, FARG2 - |.else - | toint CARG1, FARG2 - |.endif - |.endif - | blex ldexp - | b ->fff_resn - | - |.ffunc_n math_frexp - |.if GPR64 - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - |.else - | la CARG1, DISPATCH_GL(tmptv)(DISPATCH) - |.endif - | lwz PC, FRAME_PC(BASE) - | blex frexp - | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH) - | la RA, -8(BASE) - |.if not DUALNUM - | tonum_i FARG2, TMP1 - |.endif - | stfd FARG1, 0(RA) - | li RD, (2+1)*8 - |.if DUALNUM - | stw TISNUM, 8(RA) - | stw TMP1, 12(RA) - |.else - | stfd FARG2, 8(RA) - |.endif - | b ->fff_res - | - |.ffunc_n math_modf - |.if GPR64 - | la CARG2, -8(BASE) - |.else - | la CARG1, -8(BASE) - |.endif - | lwz PC, FRAME_PC(BASE) - | blex modf - | la RA, -8(BASE) - | stfd FARG1, 0(BASE) - | li RD, (2+1)*8 - | b ->fff_res - | - |.macro math_minmax, name, ismax - |.if DUALNUM - | .ffunc_1 name - | checknum CARG3 - | addi TMP1, BASE, 8 - | add TMP2, BASE, NARGS8:RC - | bne >4 - |1: // Handle integers. - | lwz CARG4, 0(TMP1) - | cmplw cr1, TMP1, TMP2 - | lwz CARG2, 4(TMP1) - | bge cr1, ->fff_resi - | checknum CARG4 - | xoris TMP0, CARG1, 0x8000 - | xoris TMP3, CARG2, 0x8000 - | bne >3 - | subfc TMP3, TMP3, TMP0 - | subfe TMP0, TMP0, TMP0 - |.if ismax - | andc TMP3, TMP3, TMP0 - |.else - | and TMP3, TMP3, TMP0 - |.endif - | add CARG1, TMP3, CARG2 - |.if GPR64 - | rldicl CARG1, CARG1, 0, 32 - |.endif - | addi TMP1, TMP1, 8 - | b <1 - |3: - | bge ->fff_fallback - | // Convert intermediate result to number and continue below. - | tonum_i FARG1, CARG1 - | lfd FARG2, 0(TMP1) - | b >6 - |4: - | lfd FARG1, 0(BASE) - | bge ->fff_fallback - |5: // Handle numbers. - | lwz CARG4, 0(TMP1) - | cmplw cr1, TMP1, TMP2 - | lfd FARG2, 0(TMP1) - | bge cr1, ->fff_resn - | checknum CARG4; bge >7 - |6: - | fsub f0, FARG1, FARG2 - | addi TMP1, TMP1, 8 - |.if ismax - | fsel FARG1, f0, FARG1, FARG2 - |.else - | fsel FARG1, f0, FARG2, FARG1 - |.endif - | b <5 - |7: // Convert integer to number and continue above. - | lwz CARG2, 4(TMP1) - | bne ->fff_fallback - | tonum_i FARG2, CARG2 - | b <6 - |.else - | .ffunc_n name - | li TMP1, 8 - |1: - | lwzx CARG2, BASE, TMP1 - | lfdx FARG2, BASE, TMP1 - | cmplw cr1, TMP1, NARGS8:RC - | checknum CARG2 - | bge cr1, ->fff_resn - | bge ->fff_fallback - | fsub f0, FARG1, FARG2 - | addi TMP1, TMP1, 8 - |.if ismax - | fsel FARG1, f0, FARG1, FARG2 - |.else - | fsel FARG1, f0, FARG2, FARG1 - |.endif - | b <1 - |.endif - |.endmacro - | - | math_minmax math_min, 0 - | math_minmax math_max, 1 - | - |//-- String library ----------------------------------------------------- - | - |.ffunc_1 string_len - | checkstr CARG3; bne ->fff_fallback - | lwz CRET1, STR:CARG1->len - | b ->fff_resi - | - |.ffunc string_byte // Only handle the 1-arg case here. - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lwz STR:CARG1, 4(BASE) - | bne ->fff_fallback // Need exactly 1 argument. - | checkstr CARG3 - | bne ->fff_fallback - | lwz TMP0, STR:CARG1->len - |.if DUALNUM - | lbz CARG1, STR:CARG1[1] // Access is always ok (NUL at end). - | li RD, (0+1)*8 - | lwz PC, FRAME_PC(BASE) - | cmplwi TMP0, 0 - | la RA, -8(BASE) - | beqy ->fff_res - | b ->fff_resi - |.else - | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end). - | addic TMP3, TMP0, -1 // RD = ((str->len != 0)+1)*8 - | subfe RD, TMP3, TMP0 - | stw TMP1, TONUM_LO // Inlined tonum_u f0, TMP1. - | addi RD, RD, 1 - | lfd f0, TONUM_D - | la RA, -8(BASE) - | lwz PC, FRAME_PC(BASE) - | fsub f0, f0, TOBIT - | slwi RD, RD, 3 - | stfd f0, 0(RA) - | b ->fff_res - |.endif - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - |.if DUALNUM - | lwz TMP0, 4(BASE) - | bne ->fff_fallback // Exactly 1 argument. - | checknum CARG3; bne ->fff_fallback - | la CARG2, 7(BASE) - |.else - | lfd FARG1, 0(BASE) - | bne ->fff_fallback // Exactly 1 argument. - | checknum CARG3; bge ->fff_fallback - | toint TMP0, FARG1 - | la CARG2, TMPD_BLO - |.endif - | li CARG3, 1 - | cmplwi TMP0, 255; bgt ->fff_fallback - |->fff_newstr: - | mr CARG1, L - | stp BASE, L->base - | stw PC, SAVE_PC - | bl extern lj_str_new // (lua_State *L, char *str, size_t l) - | // Returns GCstr *. - | lp BASE, L->base - | li CARG3, LJ_TSTR - | b ->fff_restv - | - |.ffunc string_sub - | ffgccheck - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 16(BASE) - |.if not DUALNUM - | lfd f0, 16(BASE) - |.endif - | lwz TMP0, 0(BASE) - | lwz STR:CARG1, 4(BASE) - | blt ->fff_fallback - | lwz CARG2, 8(BASE) - |.if DUALNUM - | lwz TMP1, 12(BASE) - |.else - | lfd f1, 8(BASE) - |.endif - | li TMP2, -1 - | beq >1 - |.if DUALNUM - | checknum CARG3 - | lwz TMP2, 20(BASE) - | bne ->fff_fallback - |1: - | checknum CARG2; bne ->fff_fallback - |.else - | checknum CARG3; bge ->fff_fallback - | toint TMP2, f0 - |1: - | checknum CARG2; bge ->fff_fallback - |.endif - | checkstr TMP0; bne ->fff_fallback - |.if not DUALNUM - | toint TMP1, f1 - |.endif - | lwz TMP0, STR:CARG1->len - | cmplw TMP0, TMP2 // len < end? (unsigned compare) - | addi TMP3, TMP2, 1 - | blt >5 - |2: - | cmpwi TMP1, 0 // start <= 0? - | add TMP3, TMP1, TMP0 - | ble >7 - |3: - | sub CARG3, TMP2, TMP1 - | addi CARG2, STR:CARG1, #STR-1 - | srawi TMP0, CARG3, 31 - | addi CARG3, CARG3, 1 - | add CARG2, CARG2, TMP1 - | andc CARG3, CARG3, TMP0 - |.if GPR64 - | rldicl CARG2, CARG2, 0, 32 - | rldicl CARG3, CARG3, 0, 32 - |.endif - | b ->fff_newstr - | - |5: // Negative end or overflow. - | cmpw TMP0, TMP2 // len >= end? (signed compare) - | add TMP2, TMP0, TMP3 // Negative end: end = end+len+1. - | bge <2 - | mr TMP2, TMP0 // Overflow: end = len. - | b <2 - | - |7: // Negative start or underflow. - | .gpr64 extsw TMP1, TMP1 - | addic CARG3, TMP1, -1 - | subfe CARG3, CARG3, CARG3 - | srawi CARG2, TMP3, 31 // Note: modifies carry. - | andc TMP3, TMP3, CARG3 - | andc TMP1, TMP3, CARG2 - | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0) - | b <3 - | - |.ffunc string_rep // Only handle the 1-char case inline. - | ffgccheck - | cmplwi NARGS8:RC, 16 - | lwz TMP0, 0(BASE) - | lwz STR:CARG1, 4(BASE) - | lwz CARG4, 8(BASE) - |.if DUALNUM - | lwz CARG3, 12(BASE) - |.else - | lfd FARG2, 8(BASE) - |.endif - | bne ->fff_fallback // Exactly 2 arguments. - | checkstr TMP0; bne ->fff_fallback - |.if DUALNUM - | checknum CARG4; bne ->fff_fallback - |.else - | checknum CARG4; bge ->fff_fallback - | toint CARG3, FARG2 - |.endif - | lwz TMP0, STR:CARG1->len - | cmpwi CARG3, 0 - | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | ble >2 // Count <= 0? (or non-int) - | cmplwi TMP0, 1 - | subi TMP2, CARG3, 1 - | blt >2 // Zero length string? - | cmplw cr1, TMP1, CARG3 - | bne ->fff_fallback // Fallback for > 1-char strings. - | lbz TMP0, STR:CARG1[1] - | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | blt cr1, ->fff_fallback - |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?). - | cmplwi TMP2, 0 - | stbx TMP0, CARG2, TMP2 - | subi TMP2, TMP2, 1 - | bne <1 - | b ->fff_newstr - |2: // Return empty string. - | la STR:CARG1, DISPATCH_GL(strempty)(DISPATCH) - | li CARG3, LJ_TSTR - | b ->fff_restv - | - |.ffunc string_reverse - | ffgccheck - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lwz STR:CARG1, 4(BASE) - | blt ->fff_fallback - | checkstr CARG3 - | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | bne ->fff_fallback - | lwz CARG3, STR:CARG1->len - | la CARG1, #STR(STR:CARG1) - | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | li TMP2, 0 - | cmplw TMP1, CARG3 - | subi TMP3, CARG3, 1 - | blt ->fff_fallback - |1: // Reverse string copy. - | cmpwi TMP3, 0 - | lbzx TMP1, CARG1, TMP2 - | blty ->fff_newstr - | stbx TMP1, CARG2, TMP3 - | subi TMP3, TMP3, 1 - | addi TMP2, TMP2, 1 - | b <1 - | - |.macro ffstring_case, name, lo - | .ffunc name - | ffgccheck - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lwz STR:CARG1, 4(BASE) - | blt ->fff_fallback - | checkstr CARG3 - | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | bne ->fff_fallback - | lwz CARG3, STR:CARG1->len - | la CARG1, #STR(STR:CARG1) - | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | cmplw TMP1, CARG3 - | li TMP2, 0 - | blt ->fff_fallback - |1: // ASCII case conversion. - | cmplw TMP2, CARG3 - | lbzx TMP1, CARG1, TMP2 - | bgey ->fff_newstr - | subi TMP0, TMP1, lo - | xori TMP3, TMP1, 0x20 - | addic TMP0, TMP0, -26 - | subfe TMP3, TMP3, TMP3 - | rlwinm TMP3, TMP3, 0, 26, 26 // x &= 0x20. - | xor TMP1, TMP1, TMP3 - | stbx TMP1, CARG2, TMP2 - | addi TMP2, TMP2, 1 - | b <1 - |.endmacro - | - |ffstring_case string_lower, 65 - |ffstring_case string_upper, 97 - | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | checktab CARG3; bne ->fff_fallback - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | b ->fff_resi - | - |//-- Bit library -------------------------------------------------------- - | - |.macro .ffunc_bit, name - |.if DUALNUM - | .ffunc_1 bit_..name - | checknum CARG3; bnel ->fff_tobit_fb - |.else - | .ffunc_n bit_..name - | fadd FARG1, FARG1, TOBIT - | stfd FARG1, TMPD - | lwz CARG1, TMPD_LO - |.endif - |.endmacro - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | addi TMP1, BASE, 8 - | add TMP2, BASE, NARGS8:RC - |1: - | lwz CARG4, 0(TMP1) - | cmplw cr1, TMP1, TMP2 - |.if DUALNUM - | lwz CARG2, 4(TMP1) - |.else - | lfd FARG1, 0(TMP1) - |.endif - | bgey cr1, ->fff_resi - | checknum CARG4 - |.if DUALNUM - | bnel ->fff_bitop_fb - |.else - | fadd FARG1, FARG1, TOBIT - | bge ->fff_fallback - | stfd FARG1, TMPD - | lwz CARG2, TMPD_LO - |.endif - | ins CARG1, CARG1, CARG2 - | addi TMP1, TMP1, 8 - | b <1 - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, or - |.ffunc_bit_op bxor, xor - | - |.ffunc_bit bswap - | rotlwi TMP0, CARG1, 8 - | rlwimi TMP0, CARG1, 24, 0, 7 - | rlwimi TMP0, CARG1, 24, 16, 23 - | mr CRET1, TMP0 - | b ->fff_resi - | - |.ffunc_bit bnot - | not CRET1, CARG1 - | b ->fff_resi - | - |.macro .ffunc_bit_sh, name, ins, shmod - |.if DUALNUM - | .ffunc_2 bit_..name - | checknum CARG3; bnel ->fff_tobit_fb - | // Note: no inline conversion from number for 2nd argument! - | checknum CARG4; bne ->fff_fallback - |.else - | .ffunc_nn bit_..name - | fadd FARG1, FARG1, TOBIT - | fadd FARG2, FARG2, TOBIT - | stfd FARG1, TMPD - | lwz CARG1, TMPD_LO - | stfd FARG2, TMPD - | lwz CARG2, TMPD_LO - |.endif - |.if shmod == 1 - | rlwinm CARG2, CARG2, 0, 27, 31 - |.elif shmod == 2 - | neg CARG2, CARG2 - |.endif - | ins CRET1, CARG1, CARG2 - | b ->fff_resi - |.endmacro - | - |.ffunc_bit_sh lshift, slw, 1 - |.ffunc_bit_sh rshift, srw, 1 - |.ffunc_bit_sh arshift, sraw, 1 - |.ffunc_bit_sh rol, rotlw, 0 - |.ffunc_bit_sh ror, rotlw, 2 - | - |.ffunc_bit tobit - |.if DUALNUM - | b ->fff_resi - |.else - |->fff_resi: - | tonum_i FARG1, CRET1 - |.endif - |->fff_resn: - | lwz PC, FRAME_PC(BASE) - | la RA, -8(BASE) - | stfd FARG1, -8(BASE) - | b ->fff_res1 - | - |// Fallback FP number to bit conversion. - |->fff_tobit_fb: - |.if DUALNUM - | lfd FARG1, 0(BASE) - | bgt ->fff_fallback - | fadd FARG1, FARG1, TOBIT - | stfd FARG1, TMPD - | lwz CARG1, TMPD_LO - | blr - |.endif - |->fff_bitop_fb: - |.if DUALNUM - | lfd FARG1, 0(TMP1) - | bgt ->fff_fallback - | fadd FARG1, FARG1, TOBIT - | stfd FARG1, TMPD - | lwz CARG2, TMPD_LO - | blr - |.endif - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RB = CFUNC, RC = nargs*8 - | lp TMP3, CFUNC:RB->f - | add TMP1, BASE, NARGS8:RC - | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC. - | addi TMP0, TMP1, 8*LUA_MINSTACK - | lwz TMP2, L->maxstack - | stw PC, SAVE_PC // Redundant (but a defined value). - | .toc lp TMP3, 0(TMP3) - | cmplw TMP0, TMP2 - | stp BASE, L->base - | stp TMP1, L->top - | mr CARG1, L - | bgt >5 // Need to grow stack. - | mtctr TMP3 - | bctrl // (lua_State *L) - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | lp BASE, L->base - | cmpwi CRET1, 0 - | slwi RD, CRET1, 3 - | la RA, -8(BASE) - | bgt ->fff_res // Returned nresults+1? - |1: // Returned 0 or -1: retry fast path. - | lp TMP0, L->top - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | sub NARGS8:RC, TMP0, BASE - | bne ->vm_call_tail // Returned -1? - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | andix. TMP0, PC, FRAME_TYPE - | rlwinm TMP1, PC, 0, 0, 28 - | bne >3 - | lwz INS, -4(PC) - | decode_RA8 TMP1, INS - | addi TMP1, TMP1, 8 - |3: - | sub TMP2, BASE, TMP1 - | b ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | li CARG2, LUA_MINSTACK - | bl extern lj_state_growstack // (lua_State *L, int n) - | lp BASE, L->base - | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry. - | b <1 - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | mflr SAVE0 - | stp BASE, L->base - | add TMP0, BASE, NARGS8:RC - | stw PC, SAVE_PC // Redundant (but a defined value). - | stp TMP0, L->top - | mr CARG1, L - | bl extern lj_gc_step // (lua_State *L) - | lp BASE, L->base - | mtlr SAVE0 - | lp TMP0, L->top - | sub NARGS8:RC, TMP0, BASE - | lwz CFUNC:RB, FRAME_FUNC(BASE) - | blr - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andix. TMP0, TMP3, HOOK_VMEVENT // No recording while in vmevent. - | bne >5 - | // Decrement the hookcount for consistency, but always do the call. - | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andix. TMP0, TMP3, HOOK_ACTIVE - | bne >1 - | subi TMP2, TMP2, 1 - | andi. TMP0, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqy >1 - | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | b >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active? - | beq >1 - |5: // Re-dispatch to static ins. - | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OPP TMP1, INS. - | lpx TMP0, DISPATCH, TMP1 - | mtctr TMP0 - | bctr - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active? - | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0 - | bne <5 - | - | cmpwi cr1, TMP0, 0 - | addic. TMP2, TMP2, -1 - | beq cr1, <5 - | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | beq >1 - | bge cr1, <5 - |1: - | mr CARG1, L - | stw MULTRES, SAVE_MULTRES - | mr CARG2, PC - | stp BASE, L->base - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |3: - | lp BASE, L->base - |4: // Re-dispatch to static ins. - | lwz INS, -4(PC) - | decode_OPP TMP1, INS - | decode_RB8 RB, INS - | addi TMP1, TMP1, GG_DISP2STATIC - | decode_RD8 RD, INS - | lpx TMP0, DISPATCH, TMP1 - | decode_RA8 RA, INS - | decode_RC8 RC, INS - | mtctr TMP0 - | bctr - | - |->cont_hook: // Continue from hook yield. - | addi PC, PC, 4 - | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins. - | b <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | addi CARG1, DISPATCH, GG_DISP2J - | stw PC, SAVE_PC - | lwz TMP1, LFUNC:TMP1->pc - | mr CARG2, PC - | stw L, DISPATCH_J(L)(DISPATCH) - | lbz TMP1, PC2PROTO(framesize)(TMP1) - | stp BASE, L->base - | slwi TMP1, TMP1, 3 - | add TMP1, BASE, TMP1 - | stp TMP1, L->top - | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc) - | b <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mr CARG2, PC - |.if JIT - | b >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | ori CARG2, PC, 1 - |1: - |.endif - | add TMP0, BASE, RC - | stw PC, SAVE_PC - | mr CARG1, L - | stp BASE, L->base - | sub RA, RA, BASE - | stp TMP0, L->top - | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc) - | // Returns ASMFunction. - | lp BASE, L->base - | lp TMP0, L->top - | stw ZERO, SAVE_PC // Invalidate for subsequent line hook. - | sub NARGS8:RC, TMP0, BASE - | add RA, BASE, RA - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | lwz INS, -4(PC) - | mtctr CRET1 - | bctr - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro savex_, a, b, c, d - | stfd f..a, 16+a*8(sp) - | stfd f..b, 16+b*8(sp) - | stfd f..c, 16+c*8(sp) - | stfd f..d, 16+d*8(sp) - |.endmacro - | - |->vm_exit_handler: - |.if JIT - | addi sp, sp, -(16+32*8+32*4) - | stmw r2, 16+32*8+2*4(sp) - | addi DISPATCH, JGL, -GG_DISP2G-32768 - | li CARG2, ~LJ_VMST_EXIT - | lwz CARG1, 16+32*8+32*4(sp) // Get stack chain. - | stw CARG2, DISPATCH_GL(vmstate)(DISPATCH) - | savex_ 0,1,2,3 - | stw CARG1, 0(sp) // Store extended stack chain. - | mcrxr cr0 // Clear SO flag. - | savex_ 4,5,6,7 - | addi CARG2, sp, 16+32*8+32*4 // Recompute original value of sp. - | savex_ 8,9,10,11 - | stw CARG2, 16+32*8+1*4(sp) // Store sp in RID_SP. - | savex_ 12,13,14,15 - | mflr CARG3 - | li TMP1, 0 - | savex_ 16,17,18,19 - | stw TMP1, 16+32*8+0*4(sp) // Clear RID_TMP. - | savex_ 20,21,22,23 - | lhz CARG4, 2(CARG3) // Load trace number. - | savex_ 24,25,26,27 - | lwz L, DISPATCH_GL(jit_L)(DISPATCH) - | savex_ 28,29,30,31 - | sub CARG3, TMP0, CARG3 // Compute exit number. - | lp BASE, DISPATCH_GL(jit_base)(DISPATCH) - | srwi CARG3, CARG3, 2 - | stw L, DISPATCH_J(L)(DISPATCH) - | subi CARG3, CARG3, 2 - | stw TMP1, DISPATCH_GL(jit_L)(DISPATCH) - | stw CARG4, DISPATCH_J(parent)(DISPATCH) - | stp BASE, L->base - | addi CARG1, DISPATCH, GG_DISP2J - | stw CARG3, DISPATCH_J(exitno)(DISPATCH) - | addi CARG2, sp, 16 - | bl extern lj_trace_exit // (jit_State *J, ExitState *ex) - | // Returns MULTRES (unscaled) or negated error code. - | lp TMP1, L->cframe - | lwz TMP2, 0(sp) - | lp BASE, L->base - |.if GPR64 - | rldicr sp, TMP1, 0, 61 - |.else - | rlwinm sp, TMP1, 0, 0, 29 - |.endif - | lwz PC, SAVE_PC // Get SAVE_PC. - | stw TMP2, 0(sp) - | stw L, SAVE_L // Set SAVE_L (on-trace resume/yield). - | b >1 - |.endif - |->vm_exit_interp: - |.if JIT - | // CARG1 = MULTRES or negated error code, BASE, PC and JGL set. - | lwz L, SAVE_L - | addi DISPATCH, JGL, -GG_DISP2G-32768 - |1: - | cmpwi CARG1, 0 - | blt >3 // Check for error from exit. - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | slwi MULTRES, CARG1, 3 - | li TMP2, 0 - | stw MULTRES, SAVE_MULTRES - | lwz TMP1, LFUNC:TMP1->pc - | stw TMP2, DISPATCH_GL(jit_L)(DISPATCH) - | lwz KBASE, PC2PROTO(k)(TMP1) - | // Setup type comparison constants. - | li TISNUM, LJ_TISNUM - | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | stw TMP3, TMPD - | li ZERO, 0 - | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | lfs TOBIT, TMPD - | stw TMP3, TMPD - | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | li TISNIL, LJ_TNIL - | stw TMP0, TONUM_HI - | lfs TONUM, TMPD - | // Modified copy of ins_next which handles function header dispatch, too. - | lwz INS, 0(PC) - | addi PC, PC, 4 - | // Assumes TISNIL == ~LJ_VMST_INTERP == -1. - | stw TISNIL, DISPATCH_GL(vmstate)(DISPATCH) - | decode_OPP TMP1, INS - | decode_RA8 RA, INS - | lpx TMP0, DISPATCH, TMP1 - | mtctr TMP0 - | cmplwi TMP1, BC_FUNCF*4 // Function header? - | bge >2 - | decode_RB8 RB, INS - | decode_RD8 RD, INS - | decode_RC8 RC, INS - | bctr - |2: - | subi RC, MULTRES, 8 - | add RA, RA, BASE - | bctr - | - |3: // Rethrow error from the right C frame. - | neg CARG2, CARG1 - | mr CARG1, L - | bl extern lj_err_throw // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// NYI: Use internal implementations of floor, ceil, trunc. - | - |->vm_modi: - | divwo. TMP0, CARG1, CARG2 - | bso >1 - |.if GPR64 - | xor CARG3, CARG1, CARG2 - | cmpwi CARG3, 0 - |.else - | xor. CARG3, CARG1, CARG2 - |.endif - | mullw TMP0, TMP0, CARG2 - | sub CARG1, CARG1, TMP0 - | bgelr - | cmpwi CARG1, 0; beqlr - | add CARG1, CARG1, CARG2 - | blr - |1: - | cmpwi CARG2, 0 - | li CARG1, 0 - | beqlr - | mcrxr cr0 // Clear SO for -2147483648 % -1 and return 0. - | blr - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |// void lj_vm_cachesync(void *start, void *end) - |// Flush D-Cache and invalidate I-Cache. Assumes 32 byte cache line size. - |// This is a good lower bound, except for very ancient PPC models. - |->vm_cachesync: - |.if JIT or FFI - | // Compute start of first cache line and number of cache lines. - | rlwinm CARG1, CARG1, 0, 0, 26 - | sub CARG2, CARG2, CARG1 - | addi CARG2, CARG2, 31 - | rlwinm. CARG2, CARG2, 27, 5, 31 - | beqlr - | mtctr CARG2 - | mr CARG3, CARG1 - |1: // Flush D-Cache. - | dcbst r0, CARG1 - | addi CARG1, CARG1, 32 - | bdnz <1 - | sync - | mtctr CARG2 - |1: // Invalidate I-Cache. - | icbi r0, CARG3 - | addi CARG3, CARG3, 32 - | bdnz <1 - | isync - | blr - |.endif - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in r11, g in r12. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | saveregs - | lwz CTSTATE, GL:r12->ctype_state - | addi DISPATCH, r12, GG_G2DISP - | stw r11, CTSTATE->cb.slot - | stw r3, CTSTATE->cb.gpr[0] - | stfd f1, CTSTATE->cb.fpr[0] - | stw r4, CTSTATE->cb.gpr[1] - | stfd f2, CTSTATE->cb.fpr[1] - | stw r5, CTSTATE->cb.gpr[2] - | stfd f3, CTSTATE->cb.fpr[2] - | stw r6, CTSTATE->cb.gpr[3] - | stfd f4, CTSTATE->cb.fpr[3] - | stw r7, CTSTATE->cb.gpr[4] - | stfd f5, CTSTATE->cb.fpr[4] - | stw r8, CTSTATE->cb.gpr[5] - | stfd f6, CTSTATE->cb.fpr[5] - | stw r9, CTSTATE->cb.gpr[6] - | stfd f7, CTSTATE->cb.fpr[6] - | stw r10, CTSTATE->cb.gpr[7] - | stfd f8, CTSTATE->cb.fpr[7] - | addi TMP0, sp, CFRAME_SPACE+8 - | stw TMP0, CTSTATE->cb.stack - | mr CARG1, CTSTATE - | stw CTSTATE, SAVE_PC // Any value outside of bytecode is ok. - | mr CARG2, sp - | bl extern lj_ccallback_enter // (CTState *cts, void *cf) - | // Returns lua_State *. - | lp BASE, L:CRET1->base - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp RC, L:CRET1->top - | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | li ZERO, 0 - | mr L, CRET1 - | stw TMP3, TMPD - | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | stw TMP0, TONUM_HI - | li TISNIL, LJ_TNIL - | li_vmstate INTERP - | lfs TOBIT, TMPD - | stw TMP3, TMPD - | sub RC, RC, BASE - | st_vmstate - | lfs TONUM, TMPD - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | lwz CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH) - | stp BASE, L->base - | stp RB, L->top - | stp L, CTSTATE->L - | mr CARG1, CTSTATE - | mr CARG2, RA - | bl extern lj_ccallback_leave // (CTState *cts, TValue *o) - | lwz CRET1, CTSTATE->cb.gpr[0] - | lfd FARG1, CTSTATE->cb.fpr[0] - | lwz CRET2, CTSTATE->cb.gpr[1] - | b ->vm_leave_unw - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, CARG1 - | lwz TMP1, CCSTATE->spadj - | mflr TMP0 - | lbz CARG2, CCSTATE->nsp - | lbz CARG3, CCSTATE->nfpr - | neg TMP1, TMP1 - | stw TMP0, 4(sp) - | cmpwi cr1, CARG3, 0 - | mr TMP2, sp - | addic. CARG2, CARG2, -1 - | stwux sp, sp, TMP1 - | crnot 4*cr1+eq, 4*cr1+eq // For vararg calls. - | stw r14, -4(TMP2) - | stw CCSTATE, -8(TMP2) - | mr r14, TMP2 - | la TMP1, CCSTATE->stack - | slwi CARG2, CARG2, 2 - | blty >2 - | la TMP2, 8(sp) - |1: - | lwzx TMP0, TMP1, CARG2 - | stwx TMP0, TMP2, CARG2 - | addic. CARG2, CARG2, -4 - | bge <1 - |2: - | bney cr1, >3 - | lfd f1, CCSTATE->fpr[0] - | lfd f2, CCSTATE->fpr[1] - | lfd f3, CCSTATE->fpr[2] - | lfd f4, CCSTATE->fpr[3] - | lfd f5, CCSTATE->fpr[4] - | lfd f6, CCSTATE->fpr[5] - | lfd f7, CCSTATE->fpr[6] - | lfd f8, CCSTATE->fpr[7] - |3: - | lp TMP0, CCSTATE->func - | lwz CARG2, CCSTATE->gpr[1] - | lwz CARG3, CCSTATE->gpr[2] - | lwz CARG4, CCSTATE->gpr[3] - | lwz CARG5, CCSTATE->gpr[4] - | mtctr TMP0 - | lwz r8, CCSTATE->gpr[5] - | lwz r9, CCSTATE->gpr[6] - | lwz r10, CCSTATE->gpr[7] - | lwz CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1. - | bctrl - | lwz CCSTATE:TMP1, -8(r14) - | lwz TMP2, -4(r14) - | lwz TMP0, 4(r14) - | stw CARG1, CCSTATE:TMP1->gpr[0] - | stfd FARG1, CCSTATE:TMP1->fpr[0] - | stw CARG2, CCSTATE:TMP1->gpr[1] - | mtlr TMP0 - | stw CARG3, CCSTATE:TMP1->gpr[2] - | mr sp, r14 - | stw CARG4, CCSTATE:TMP1->gpr[3] - | mr r14, TMP2 - | blr - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RD = src2*8, JMP with RD = target - |.if DUALNUM - | lwzux TMP0, RA, BASE - | addi PC, PC, 4 - | lwz CARG2, 4(RA) - | lwzux TMP1, RD, BASE - | lwz TMP2, -4(PC) - | checknum cr0, TMP0 - | lwz CARG3, 4(RD) - | decode_RD4 TMP2, TMP2 - | checknum cr1, TMP1 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | bne cr0, >7 - | bne cr1, >8 - | cmpw CARG2, CARG3 - if (op == BC_ISLT) { - | bge >2 - } else if (op == BC_ISGE) { - | blt >2 - } else if (op == BC_ISLE) { - | bgt >2 - } else { - | ble >2 - } - |1: - | add PC, PC, TMP2 - |2: - | ins_next - | - |7: // RA is not an integer. - | bgt cr0, ->vmeta_comp - | // RA is a number. - | lfd f0, 0(RA) - | bgt cr1, ->vmeta_comp - | blt cr1, >4 - | // RA is a number, RD is an integer. - | tonum_i f1, CARG3 - | b >5 - | - |8: // RA is an integer, RD is not an integer. - | bgt cr1, ->vmeta_comp - | // RA is an integer, RD is a number. - | tonum_i f0, CARG2 - |4: - | lfd f1, 0(RD) - |5: - | fcmpu cr0, f0, f1 - if (op == BC_ISLT) { - | bge <2 - } else if (op == BC_ISGE) { - | blt <2 - } else if (op == BC_ISLE) { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | bge <2 - } else { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | blt <2 - } - | b <1 - |.else - | lwzx TMP0, BASE, RA - | addi PC, PC, 4 - | lfdx f0, BASE, RA - | lwzx TMP1, BASE, RD - | checknum cr0, TMP0 - | lwz TMP2, -4(PC) - | lfdx f1, BASE, RD - | checknum cr1, TMP1 - | decode_RD4 TMP2, TMP2 - | bge cr0, ->vmeta_comp - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | bge cr1, ->vmeta_comp - | fcmpu cr0, f0, f1 - if (op == BC_ISLT) { - | bge >1 - } else if (op == BC_ISGE) { - | blt >1 - } else if (op == BC_ISLE) { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | bge >1 - } else { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | blt >1 - } - | add PC, PC, TMP2 - |1: - | ins_next - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RD = src2*8, JMP with RD = target - |.if DUALNUM - | lwzux TMP0, RA, BASE - | addi PC, PC, 4 - | lwz CARG2, 4(RA) - | lwzux TMP1, RD, BASE - | checknum cr0, TMP0 - | lwz TMP2, -4(PC) - | checknum cr1, TMP1 - | decode_RD4 TMP2, TMP2 - | lwz CARG3, 4(RD) - | cror 4*cr7+gt, 4*cr0+gt, 4*cr1+gt - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - if (vk) { - | ble cr7, ->BC_ISEQN_Z - } else { - | ble cr7, ->BC_ISNEN_Z - } - |.else - | lwzux TMP0, RA, BASE - | lwz TMP2, 0(PC) - | lfd f0, 0(RA) - | addi PC, PC, 4 - | lwzux TMP1, RD, BASE - | checknum cr0, TMP0 - | decode_RD4 TMP2, TMP2 - | lfd f1, 0(RD) - | checknum cr1, TMP1 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | bge cr0, >5 - | bge cr1, >5 - | fcmpu cr0, f0, f1 - if (vk) { - | bne >1 - | add PC, PC, TMP2 - } else { - | beq >1 - | add PC, PC, TMP2 - } - |1: - | ins_next - |.endif - |5: // Either or both types are not numbers. - |.if not DUALNUM - | lwz CARG2, 4(RA) - | lwz CARG3, 4(RD) - |.endif - |.if FFI - | cmpwi cr7, TMP0, LJ_TCDATA - | cmpwi cr5, TMP1, LJ_TCDATA - |.endif - | not TMP3, TMP0 - | cmplw TMP0, TMP1 - | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive? - |.if FFI - | cror 4*cr7+eq, 4*cr7+eq, 4*cr5+eq - |.endif - | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata? - |.if FFI - | beq cr7, ->vmeta_equal_cd - |.endif - | cmplw cr5, CARG2, CARG3 - | crandc 4*cr0+gt, 4*cr0+eq, 4*cr1+gt // 2: Same type and primitive. - | crorc 4*cr0+lt, 4*cr5+eq, 4*cr0+eq // 1: Same tv or different type. - | crand 4*cr0+eq, 4*cr0+eq, 4*cr5+eq // 0: Same type and same tv. - | mr SAVE0, PC - | cror 4*cr0+eq, 4*cr0+eq, 4*cr0+gt // 0 or 2. - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+gt // 1 or 2. - if (vk) { - | bne cr0, >6 - | add PC, PC, TMP2 - |6: - } else { - | beq cr0, >6 - | add PC, PC, TMP2 - |6: - } - |.if DUALNUM - | bge cr0, >2 // Done if 1 or 2. - |1: - | ins_next - |2: - |.else - | blt cr0, <1 // Done if 1 or 2. - |.endif - | blt cr6, <1 // Done if not tab/ud. - | - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | lwz TAB:TMP2, TAB:CARG2->metatable - | li CARG4, 1-vk // ne = 0 or 1. - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable? - | lbz TMP2, TAB:TMP2->nomm - | andix. TMP2, TMP2, 1<vmeta_equal // Handle __eq metamethod. - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RD = str_const*8 (~), JMP with RD = target - | lwzux TMP0, RA, BASE - | srwi RD, RD, 1 - | lwz STR:TMP3, 4(RA) - | lwz TMP2, 0(PC) - | subfic RD, RD, -4 - | addi PC, PC, 4 - |.if FFI - | cmpwi TMP0, LJ_TCDATA - |.endif - | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4 - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TSTR - |.if FFI - | beq ->vmeta_equal_cd - |.endif - | sub TMP1, STR:TMP1, STR:TMP3 - | or TMP0, TMP0, TMP1 - | decode_RD4 TMP2, TMP2 - | subfic TMP0, TMP0, 0 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | subfe TMP1, TMP1, TMP1 - if (vk) { - | andc TMP2, TMP2, TMP1 - } else { - | and TMP2, TMP2, TMP1 - } - | add PC, PC, TMP2 - | ins_next - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RD = num_const*8, JMP with RD = target - |.if DUALNUM - | lwzux TMP0, RA, BASE - | addi PC, PC, 4 - | lwz CARG2, 4(RA) - | lwzux TMP1, RD, KBASE - | checknum cr0, TMP0 - | lwz TMP2, -4(PC) - | checknum cr1, TMP1 - | decode_RD4 TMP2, TMP2 - | lwz CARG3, 4(RD) - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - if (vk) { - |->BC_ISEQN_Z: - } else { - |->BC_ISNEN_Z: - } - | bne cr0, >7 - | bne cr1, >8 - | cmpw CARG2, CARG3 - |4: - |.else - if (vk) { - |->BC_ISEQN_Z: // Dummy label. - } else { - |->BC_ISNEN_Z: // Dummy label. - } - | lwzx TMP0, BASE, RA - | addi PC, PC, 4 - | lfdx f0, BASE, RA - | lwz TMP2, -4(PC) - | lfdx f1, KBASE, RD - | decode_RD4 TMP2, TMP2 - | checknum TMP0 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | bge >3 - | fcmpu cr0, f0, f1 - |.endif - if (vk) { - | bne >1 - | add PC, PC, TMP2 - |1: - |.if not FFI - |3: - |.endif - } else { - | beq >2 - |1: - |.if not FFI - |3: - |.endif - | add PC, PC, TMP2 - |2: - } - | ins_next - |.if FFI - |3: - | cmpwi TMP0, LJ_TCDATA - | beq ->vmeta_equal_cd - | b <1 - |.endif - |.if DUALNUM - |7: // RA is not an integer. - | bge cr0, <3 - | // RA is a number. - | lfd f0, 0(RA) - | blt cr1, >1 - | // RA is a number, RD is an integer. - | tonum_i f1, CARG3 - | b >2 - | - |8: // RA is an integer, RD is a number. - | tonum_i f0, CARG2 - |1: - | lfd f1, 0(RD) - |2: - | fcmpu cr0, f0, f1 - | b <4 - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target - | lwzx TMP0, BASE, RA - | srwi TMP1, RD, 3 - | lwz TMP2, 0(PC) - | not TMP1, TMP1 - | addi PC, PC, 4 - |.if FFI - | cmpwi TMP0, LJ_TCDATA - |.endif - | sub TMP0, TMP0, TMP1 - |.if FFI - | beq ->vmeta_equal_cd - |.endif - | decode_RD4 TMP2, TMP2 - | .gpr64 extsw TMP0, TMP0 - | addic TMP0, TMP0, -1 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | subfe TMP1, TMP1, TMP1 - if (vk) { - | and TMP2, TMP2, TMP1 - } else { - | andc TMP2, TMP2, TMP1 - } - | add PC, PC, TMP2 - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RD = src*8, JMP with RD = target - | lwzx TMP0, BASE, RD - | lwz INS, 0(PC) - | addi PC, PC, 4 - if (op == BC_IST || op == BC_ISF) { - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TTRUE - | decode_RD4 TMP2, INS - | subfe TMP1, TMP1, TMP1 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - if (op == BC_IST) { - | andc TMP2, TMP2, TMP1 - } else { - | and TMP2, TMP2, TMP1 - } - | add PC, PC, TMP2 - } else { - | li TMP1, LJ_TFALSE - | lfdx f0, BASE, RD - | cmplw TMP0, TMP1 - if (op == BC_ISTC) { - | bge >1 - } else { - | blt >1 - } - | addis PC, PC, -(BCBIAS_J*4 >> 16) - | decode_RD4 TMP2, INS - | stfdx f0, BASE, RA - | add PC, PC, TMP2 - |1: - } - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RD = src*8 - | ins_next1 - | lfdx f0, BASE, RD - | stfdx f0, BASE, RA - | ins_next2 - break; - case BC_NOT: - | // RA = dst*8, RD = src*8 - | ins_next1 - | lwzx TMP0, BASE, RD - | .gpr64 extsw TMP0, TMP0 - | subfic TMP1, TMP0, LJ_TTRUE - | adde TMP0, TMP0, TMP1 - | stwx TMP0, BASE, RA - | ins_next2 - break; - case BC_UNM: - | // RA = dst*8, RD = src*8 - | lwzux TMP1, RD, BASE - | lwz TMP0, 4(RD) - | checknum TMP1 - |.if DUALNUM - | bne >5 - |.if GPR64 - | lus TMP2, 0x8000 - | neg TMP0, TMP0 - | cmplw TMP0, TMP2 - | beq >4 - |.else - | nego. TMP0, TMP0 - | bso >4 - |1: - |.endif - | ins_next1 - | stwux TISNUM, RA, BASE - | stw TMP0, 4(RA) - |3: - | ins_next2 - |4: - |.if not GPR64 - | // Potential overflow. - | mcrxr cr0; bley <1 // Ignore unrelated overflow. - |.endif - | lus TMP1, 0x41e0 // 2^31. - | li TMP0, 0 - | b >7 - |.endif - |5: - | bge ->vmeta_unm - | xoris TMP1, TMP1, 0x8000 - |7: - | ins_next1 - | stwux TMP1, RA, BASE - | stw TMP0, 4(RA) - |.if DUALNUM - | b <3 - |.else - | ins_next2 - |.endif - break; - case BC_LEN: - | // RA = dst*8, RD = src*8 - | lwzux TMP0, RD, BASE - | lwz CARG1, 4(RD) - | checkstr TMP0; bne >2 - | lwz CRET1, STR:CARG1->len - |1: - |.if DUALNUM - | ins_next1 - | stwux TISNUM, RA, BASE - | stw CRET1, 4(RA) - |.else - | tonum_u f0, CRET1 // Result is a non-negative integer. - | ins_next1 - | stfdx f0, BASE, RA - |.endif - | ins_next2 - |2: - | checktab TMP0; bne ->vmeta_len -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - | cmplwi TAB:TMP2, 0 - | bne >9 - |3: -#endif - |->BC_LEN_Z: - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | b <1 -#if LJ_52 - |9: - | lbz TMP0, TAB:TMP2->nomm - | andix. TMP0, TMP0, 1<vmeta_len -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithpre - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | lwzx TMP1, BASE, RB - | .if DUALNUM - | lwzx TMP2, KBASE, RC - | .endif - | lfdx f14, BASE, RB - | lfdx f15, KBASE, RC - | .if DUALNUM - | checknum cr0, TMP1 - | checknum cr1, TMP2 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_vn - | .else - | checknum TMP1; bge ->vmeta_arith_vn - | .endif - || break; - ||case 1: - | lwzx TMP1, BASE, RB - | .if DUALNUM - | lwzx TMP2, KBASE, RC - | .endif - | lfdx f15, BASE, RB - | lfdx f14, KBASE, RC - | .if DUALNUM - | checknum cr0, TMP1 - | checknum cr1, TMP2 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_nv - | .else - | checknum TMP1; bge ->vmeta_arith_nv - | .endif - || break; - ||default: - | lwzx TMP1, BASE, RB - | lwzx TMP2, BASE, RC - | lfdx f14, BASE, RB - | lfdx f15, BASE, RC - | checknum cr0, TMP1 - | checknum cr1, TMP2 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_vv - || break; - ||} - |.endmacro - | - |.macro ins_arithfallback, ins - ||switch (vk) { - ||case 0: - | ins ->vmeta_arith_vn2 - || break; - ||case 1: - | ins ->vmeta_arith_nv2 - || break; - ||default: - | ins ->vmeta_arith_vv2 - || break; - ||} - |.endmacro - | - |.macro intmod, a, b, c - | bl ->vm_modi - |.endmacro - | - |.macro fpmod, a, b, c - |->BC_MODVN_Z: - | fdiv FARG1, b, c - | // NYI: Use internal implementation of floor. - | blex floor // floor(b/c) - | fmul a, FARG1, c - | fsub a, b, a // b - floor(b/c)*c - |.endmacro - | - |.macro ins_arithfp, fpins - | ins_arithpre - |.if "fpins" == "fpmod_" - | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - |.else - | fpins f0, f14, f15 - | ins_next1 - | stfdx f0, BASE, RA - | ins_next2 - |.endif - |.endmacro - | - |.macro ins_arithdn, intins, fpins - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | lwzux TMP1, RB, BASE - | lwzux TMP2, RC, KBASE - | lwz CARG1, 4(RB) - | checknum cr0, TMP1 - | lwz CARG2, 4(RC) - || break; - ||case 1: - | lwzux TMP1, RB, BASE - | lwzux TMP2, RC, KBASE - | lwz CARG2, 4(RB) - | checknum cr0, TMP1 - | lwz CARG1, 4(RC) - || break; - ||default: - | lwzux TMP1, RB, BASE - | lwzux TMP2, RC, BASE - | lwz CARG1, 4(RB) - | checknum cr0, TMP1 - | lwz CARG2, 4(RC) - || break; - ||} - | checknum cr1, TMP2 - | bne >5 - | bne cr1, >5 - | intins CARG1, CARG1, CARG2 - | bso >4 - |1: - | ins_next1 - | stwux TISNUM, RA, BASE - | stw CARG1, 4(RA) - |2: - | ins_next2 - |4: // Overflow. - | mcrxr cr0; bley <1 // Ignore unrelated overflow. - | ins_arithfallback b - |5: // FP variant. - ||if (vk == 1) { - | lfd f15, 0(RB) - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | lfd f14, 0(RC) - ||} else { - | lfd f14, 0(RB) - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | lfd f15, 0(RC) - ||} - | ins_arithfallback bge - |.if "fpins" == "fpmod_" - | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - |.else - | fpins f0, f14, f15 - | ins_next1 - | stfdx f0, BASE, RA - | b <2 - |.endif - |.endmacro - | - |.macro ins_arith, intins, fpins - |.if DUALNUM - | ins_arithdn intins, fpins - |.else - | ins_arithfp fpins - |.endif - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - |.if GPR64 - |.macro addo32., y, a, b - | // Need to check overflow for (a<<32) + (b<<32). - | rldicr TMP0, a, 32, 31 - | rldicr TMP3, b, 32, 31 - | addo. TMP0, TMP0, TMP3 - | add y, a, b - |.endmacro - | ins_arith addo32., fadd - |.else - | ins_arith addo., fadd - |.endif - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - |.if GPR64 - |.macro subo32., y, a, b - | // Need to check overflow for (a<<32) - (b<<32). - | rldicr TMP0, a, 32, 31 - | rldicr TMP3, b, 32, 31 - | subo. TMP0, TMP0, TMP3 - | sub y, a, b - |.endmacro - | ins_arith subo32., fsub - |.else - | ins_arith subo., fsub - |.endif - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith mullwo., fmul - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arithfp fdiv - break; - case BC_MODVN: - | ins_arith intmod, fpmod - break; - case BC_MODNV: case BC_MODVV: - | ins_arith intmod, fpmod_ - break; - case BC_POW: - | // NYI: (partial) integer arithmetic. - | lwzx TMP1, BASE, RB - | lfdx FARG1, BASE, RB - | lwzx TMP2, BASE, RC - | lfdx FARG2, BASE, RC - | checknum cr0, TMP1 - | checknum cr1, TMP2 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_vv - | blex pow - | ins_next1 - | stfdx FARG1, BASE, RA - | ins_next2 - break; - - case BC_CAT: - | // RA = dst*8, RB = src_start*8, RC = src_end*8 - | sub CARG3, RC, RB - | stp BASE, L->base - | add CARG2, BASE, RC - | mr SAVE0, RB - |->BC_CAT_Z: - | stw PC, SAVE_PC - | mr CARG1, L - | srwi CARG3, CARG3, 3 - | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // Returns NULL (finished) or TValue * (metamethod). - | cmplwi CRET1, 0 - | lp BASE, L->base - | bne ->vmeta_binop - | ins_next1 - | lfdx f0, BASE, SAVE0 // Copy result from RB to RA. - | stfdx f0, BASE, RA - | ins_next2 - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RD = str_const*8 (~) - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | ins_next1 - | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4 - | li TMP2, LJ_TSTR - | stwux TMP2, RA, BASE - | stw TMP0, 4(RA) - | ins_next2 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RD = cdata_const*8 (~) - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | ins_next1 - | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4 - | li TMP2, LJ_TCDATA - | stwux TMP2, RA, BASE - | stw TMP0, 4(RA) - | ins_next2 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, RD = int16_literal*8 - |.if DUALNUM - | slwi RD, RD, 13 - | srawi RD, RD, 16 - | ins_next1 - | stwux TISNUM, RA, BASE - | stw RD, 4(RA) - | ins_next2 - |.else - | // The soft-float approach is faster. - | slwi RD, RD, 13 - | srawi TMP1, RD, 31 - | xor TMP2, TMP1, RD - | sub TMP2, TMP2, TMP1 // TMP2 = abs(x) - | cntlzw TMP3, TMP2 - | subfic TMP1, TMP3, 0x40d // TMP1 = exponent-1 - | slw TMP2, TMP2, TMP3 // TMP2 = left aligned mantissa - | subfic TMP3, RD, 0 - | slwi TMP1, TMP1, 20 - | rlwimi RD, TMP2, 21, 1, 31 // hi = sign(x) | (mantissa>>11) - | subfe TMP0, TMP0, TMP0 - | add RD, RD, TMP1 // hi = hi + exponent-1 - | and RD, RD, TMP0 // hi = x == 0 ? 0 : hi - | ins_next1 - | stwux RD, RA, BASE - | stw ZERO, 4(RA) - | ins_next2 - |.endif - break; - case BC_KNUM: - | // RA = dst*8, RD = num_const*8 - | ins_next1 - | lfdx f0, KBASE, RD - | stfdx f0, BASE, RA - | ins_next2 - break; - case BC_KPRI: - | // RA = dst*8, RD = primitive_type*8 (~) - | srwi TMP1, RD, 3 - | not TMP0, TMP1 - | ins_next1 - | stwx TMP0, BASE, RA - | ins_next2 - break; - case BC_KNIL: - | // RA = base*8, RD = end*8 - | stwx TISNIL, BASE, RA - | addi RA, RA, 8 - |1: - | stwx TISNIL, BASE, RA - | cmpw RA, RD - | addi RA, RA, 8 - | blt <1 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RD = uvnum*8 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RD, RD, 1 - | addi RD, RD, offsetof(GCfuncL, uvptr) - | lwzx UPVAL:RB, LFUNC:RB, RD - | ins_next1 - | lwz TMP1, UPVAL:RB->v - | lfd f0, 0(TMP1) - | stfdx f0, BASE, RA - | ins_next2 - break; - case BC_USETV: - | // RA = uvnum*8, RD = src*8 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | lfdux f0, RD, BASE - | lwzx UPVAL:RB, LFUNC:RB, RA - | lbz TMP3, UPVAL:RB->marked - | lwz CARG2, UPVAL:RB->v - | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbz TMP0, UPVAL:RB->closed - | lwz TMP2, 0(RD) - | stfd f0, 0(CARG2) - | cmplwi cr1, TMP0, 0 - | lwz TMP1, 4(RD) - | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | subi TMP2, TMP2, (LJ_TISNUM+1) - | bne >2 // Upvalue is closed and black? - |1: - | ins_next - | - |2: // Check if new value is collectable. - | cmplwi TMP2, LJ_TISGCV - (LJ_TISNUM+1) - | bge <1 // tvisgcv(v) - | lbz TMP3, GCOBJ:TMP1->gch.marked - | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(v) - | la CARG1, GG_DISP2G(DISPATCH) - | // Crossed a write barrier. Move the barrier forward. - | beq <1 - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETS: - | // RA = uvnum*8, RD = str_const*8 (~) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi TMP1, RD, 1 - | srwi RA, RA, 1 - | subfic TMP1, TMP1, -4 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4 - | lwzx UPVAL:RB, LFUNC:RB, RA - | lbz TMP3, UPVAL:RB->marked - | lwz CARG2, UPVAL:RB->v - | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbz TMP3, STR:TMP1->marked - | lbz TMP2, UPVAL:RB->closed - | li TMP0, LJ_TSTR - | stw STR:TMP1, 4(CARG2) - | stw TMP0, 0(CARG2) - | bne >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(str) - | cmplwi cr1, TMP2, 0 - | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | la CARG1, GG_DISP2G(DISPATCH) - | // Crossed a write barrier. Move the barrier forward. - | beq <1 - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETN: - | // RA = uvnum*8, RD = num_const*8 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | lfdx f0, KBASE, RD - | lwzx UPVAL:RB, LFUNC:RB, RA - | ins_next1 - | lwz TMP1, UPVAL:RB->v - | stfd f0, 0(TMP1) - | ins_next2 - break; - case BC_USETP: - | // RA = uvnum*8, RD = primitive_type*8 (~) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | srwi TMP0, RD, 3 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | not TMP0, TMP0 - | lwzx UPVAL:RB, LFUNC:RB, RA - | ins_next1 - | lwz TMP1, UPVAL:RB->v - | stw TMP0, 0(TMP1) - | ins_next2 - break; - - case BC_UCLO: - | // RA = level*8, RD = target - | lwz TMP1, L->openupval - | branch_RD // Do this first since RD is not saved. - | stp BASE, L->base - | cmplwi TMP1, 0 - | mr CARG1, L - | beq >1 - | add CARG2, BASE, RA - | bl extern lj_func_closeuv // (lua_State *L, TValue *level) - | lp BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype) - | srwi TMP1, RD, 1 - | stp BASE, L->base - | subfic TMP1, TMP1, -4 - | stw PC, SAVE_PC - | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4 - | mr CARG1, L - | lwz CARG3, FRAME_FUNC(BASE) - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | bl extern lj_func_newL_gc - | // Returns GCfuncL *. - | lp BASE, L->base - | li TMP0, LJ_TFUNC - | stwux TMP0, RA, BASE - | stw LFUNC:CRET1, 4(RA) - | ins_next - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~) - | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | mr CARG1, L - | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | stp BASE, L->base - | cmplw TMP0, TMP1 - | stw PC, SAVE_PC - | bge >5 - |1: - if (op == BC_TNEW) { - | rlwinm CARG2, RD, 29, 21, 31 - | rlwinm CARG3, RD, 18, 27, 31 - | cmpwi CARG2, 0x7ff; beq >3 - |2: - | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Returns Table *. - } else { - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4 - | bl extern lj_tab_dup // (lua_State *L, Table *kt) - | // Returns Table *. - } - | lp BASE, L->base - | li TMP0, LJ_TTAB - | stwux TMP0, RA, BASE - | stw TAB:CRET1, 4(RA) - | ins_next - if (op == BC_TNEW) { - |3: - | li CARG2, 0x801 - | b <2 - } - |5: - | mr SAVE0, RD - | bl extern lj_gc_step_fixtop // (lua_State *L) - | mr RD, SAVE0 - | mr CARG1, L - | b <1 - break; - - case BC_GGET: - | // RA = dst*8, RD = str_const*8 (~) - case BC_GSET: - | // RA = src*8, RD = str_const*8 (~) - | lwz LFUNC:TMP2, FRAME_FUNC(BASE) - | srwi TMP1, RD, 1 - | lwz TAB:RB, LFUNC:TMP2->env - | subfic TMP1, TMP1, -4 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - break; - - case BC_TGETV: - | // RA = dst*8, RB = table*8, RC = key*8 - | lwzux CARG1, RB, BASE - | lwzux CARG2, RC, BASE - | lwz TAB:RB, 4(RB) - |.if DUALNUM - | lwz RC, 4(RC) - |.else - | lfd f0, 0(RC) - |.endif - | checktab CARG1 - | checknum cr1, CARG2 - | bne ->vmeta_tgetv - |.if DUALNUM - | lwz TMP0, TAB:RB->asize - | bne cr1, >5 - | lwz TMP1, TAB:RB->array - | cmplw TMP0, RC - | slwi TMP2, RC, 3 - |.else - | bge cr1, >5 - | // Convert number key to integer, check for integerness and range. - | fctiwz f1, f0 - | fadd f2, f0, TOBIT - | stfd f1, TMPD - | lwz TMP0, TAB:RB->asize - | fsub f2, f2, TOBIT - | lwz TMP2, TMPD_LO - | lwz TMP1, TAB:RB->array - | fcmpu cr1, f0, f2 - | cmplw cr0, TMP0, TMP2 - | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq - | slwi TMP2, TMP2, 3 - |.endif - | ble ->vmeta_tgetv // Integer key and in array part? - | lwzx TMP0, TMP1, TMP2 - | lfdx f14, TMP1, TMP2 - | checknil TMP0; beq >2 - |1: - | ins_next1 - | stfdx f14, BASE, RA - | ins_next2 - | - |2: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP0, TAB:TMP2->nomm - | andix. TMP0, TMP0, 1<vmeta_tgetv - | - |5: - | checkstr CARG2; bne ->vmeta_tgetv - |.if not DUALNUM - | lwz STR:RC, 4(RC) - |.endif - | b ->BC_TGETS_Z // String key? - break; - case BC_TGETS: - | // RA = dst*8, RB = table*8, RC = str_const*8 (~) - | lwzux CARG1, RB, BASE - | srwi TMP1, RC, 1 - | lwz TAB:RB, 4(RB) - | subfic TMP1, TMP1, -4 - | checktab CARG1 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - | bne ->vmeta_tgets1 - |->BC_TGETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8 - | lwz TMP0, TAB:RB->hmask - | lwz TMP1, STR:RC->hash - | lwz NODE:TMP2, TAB:RB->node - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | lwz CARG1, NODE:TMP2->key - | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2) - | lwz CARG2, NODE:TMP2->val - | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2) - | checkstr CARG1; bne >4 - | cmpw TMP0, STR:RC; bne >4 - | checknil CARG2; beq >5 // Key found, but nil value? - |3: - | stwux CARG2, RA, BASE - | stw TMP1, 4(RA) - | ins_next - | - |4: // Follow hash chain. - | lwz NODE:TMP2, NODE:TMP2->next - | cmplwi NODE:TMP2, 0 - | bne <1 - | // End of hash chain: key not found, nil result. - | li CARG2, LJ_TNIL - | - |5: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <3 // No metatable: done. - | lbz TMP0, TAB:TMP2->nomm - | andix. TMP0, TMP0, 1<vmeta_tgets - break; - case BC_TGETB: - | // RA = dst*8, RB = table*8, RC = index*8 - | lwzux CARG1, RB, BASE - | srwi TMP0, RC, 3 - | lwz TAB:RB, 4(RB) - | checktab CARG1; bne ->vmeta_tgetb - | lwz TMP1, TAB:RB->asize - | lwz TMP2, TAB:RB->array - | cmplw TMP0, TMP1; bge ->vmeta_tgetb - | lwzx TMP1, TMP2, RC - | lfdx f0, TMP2, RC - | checknil TMP1; beq >5 - |1: - | ins_next1 - | stfdx f0, BASE, RA - | ins_next2 - | - |5: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP2, TAB:TMP2->nomm - | andix. TMP2, TMP2, 1<vmeta_tgetb // Caveat: preserve TMP0! - break; - - case BC_TSETV: - | // RA = src*8, RB = table*8, RC = key*8 - | lwzux CARG1, RB, BASE - | lwzux CARG2, RC, BASE - | lwz TAB:RB, 4(RB) - |.if DUALNUM - | lwz RC, 4(RC) - |.else - | lfd f0, 0(RC) - |.endif - | checktab CARG1 - | checknum cr1, CARG2 - | bne ->vmeta_tsetv - |.if DUALNUM - | lwz TMP0, TAB:RB->asize - | bne cr1, >5 - | lwz TMP1, TAB:RB->array - | cmplw TMP0, RC - | slwi TMP0, RC, 3 - |.else - | bge cr1, >5 - | // Convert number key to integer, check for integerness and range. - | fctiwz f1, f0 - | fadd f2, f0, TOBIT - | stfd f1, TMPD - | lwz TMP0, TAB:RB->asize - | fsub f2, f2, TOBIT - | lwz TMP2, TMPD_LO - | lwz TMP1, TAB:RB->array - | fcmpu cr1, f0, f2 - | cmplw cr0, TMP0, TMP2 - | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq - | slwi TMP0, TMP2, 3 - |.endif - | ble ->vmeta_tsetv // Integer key and in array part? - | lwzx TMP2, TMP1, TMP0 - | lbz TMP3, TAB:RB->marked - | lfdx f14, BASE, RA - | checknil TMP2; beq >3 - |1: - | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table) - | stfdx f14, TMP1, TMP0 - | bne >7 - |2: - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP2, TAB:TMP2->nomm - | andix. TMP2, TMP2, 1<vmeta_tsetv - | - |5: - | checkstr CARG2; bne ->vmeta_tsetv - |.if not DUALNUM - | lwz STR:RC, 4(RC) - |.endif - | b ->BC_TSETS_Z // String key? - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <2 - break; - case BC_TSETS: - | // RA = src*8, RB = table*8, RC = str_const*8 (~) - | lwzux CARG1, RB, BASE - | srwi TMP1, RC, 1 - | lwz TAB:RB, 4(RB) - | subfic TMP1, TMP1, -4 - | checktab CARG1 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - | bne ->vmeta_tsets1 - |->BC_TSETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8 - | lwz TMP0, TAB:RB->hmask - | lwz TMP1, STR:RC->hash - | lwz NODE:TMP2, TAB:RB->node - | stb ZERO, TAB:RB->nomm // Clear metamethod cache. - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | lfdx f14, BASE, RA - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | lbz TMP3, TAB:RB->marked - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | lwz CARG1, NODE:TMP2->key - | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2) - | lwz CARG2, NODE:TMP2->val - | lwz NODE:TMP1, NODE:TMP2->next - | checkstr CARG1; bne >5 - | cmpw TMP0, STR:RC; bne >5 - | checknil CARG2; beq >4 // Key found, but nil value? - |2: - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - | stfd f14, NODE:TMP2->val - | bne >7 - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | lwz TAB:TMP1, TAB:RB->metatable - | cmplwi TAB:TMP1, 0 - | beq <2 // No metatable: done. - | lbz TMP0, TAB:TMP1->nomm - | andix. TMP0, TMP0, 1<vmeta_tsets - | - |5: // Follow hash chain. - | cmplwi NODE:TMP1, 0 - | mr NODE:TMP2, NODE:TMP1 - | bne <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | lwz TAB:TMP1, TAB:RB->metatable - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | stw PC, SAVE_PC - | mr CARG1, L - | cmplwi TAB:TMP1, 0 - | stp BASE, L->base - | beq >6 // No metatable: continue. - | lbz TMP0, TAB:TMP1->nomm - | andix. TMP0, TMP0, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | li TMP0, LJ_TSTR - | stw STR:RC, 4(CARG3) - | mr CARG2, TAB:RB - | stw TMP0, 0(CARG3) - | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Returns TValue *. - | lp BASE, L->base - | stfd f14, 0(CRET1) - | b <3 // No 2nd write barrier needed. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <3 - break; - case BC_TSETB: - | // RA = src*8, RB = table*8, RC = index*8 - | lwzux CARG1, RB, BASE - | srwi TMP0, RC, 3 - | lwz TAB:RB, 4(RB) - | checktab CARG1; bne ->vmeta_tsetb - | lwz TMP1, TAB:RB->asize - | lwz TMP2, TAB:RB->array - | lbz TMP3, TAB:RB->marked - | cmplw TMP0, TMP1 - | lfdx f14, BASE, RA - | bge ->vmeta_tsetb - | lwzx TMP1, TMP2, RC - | checknil TMP1; beq >5 - |1: - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - | stfdx f14, TMP2, RC - | bne >7 - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | lwz TAB:TMP1, TAB:RB->metatable - | cmplwi TAB:TMP1, 0 - | beq <1 // No metatable: done. - | lbz TMP1, TAB:TMP1->nomm - | andix. TMP1, TMP1, 1<vmeta_tsetb // Caveat: preserve TMP0! - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <2 - break; - - case BC_TSETM: - | // RA = base*8 (table at base-1), RD = num_const*8 (start index) - | add RA, BASE, RA - |1: - | add TMP3, KBASE, RD - | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table. - | addic. TMP0, MULTRES, -8 - | lwz TMP3, 4(TMP3) // Integer constant is in lo-word. - | srwi CARG3, TMP0, 3 - | beq >4 // Nothing to copy? - | add CARG3, CARG3, TMP3 - | lwz TMP2, TAB:CARG2->asize - | slwi TMP1, TMP3, 3 - | lbz TMP3, TAB:CARG2->marked - | cmplw CARG3, TMP2 - | add TMP2, RA, TMP0 - | lwz TMP0, TAB:CARG2->array - | bgt >5 - | add TMP1, TMP1, TMP0 - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |3: // Copy result slots to table. - | lfd f0, 0(RA) - | addi RA, RA, 8 - | cmpw cr1, RA, TMP2 - | stfd f0, 0(TMP1) - | addi TMP1, TMP1, 8 - | blt cr1, <3 - | bne >7 - |4: - | ins_next - | - |5: // Need to resize array part. - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | mr SAVE0, RD - | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | // Must not reallocate the stack. - | mr RD, SAVE0 - | b <1 - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, TMP0 - | b <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8 - | add NARGS8:RC, NARGS8:RC, MULTRES - | // Fall through. Assumes BC_CALL follows. - break; - case BC_CALL: - | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8 - | mr TMP2, BASE - | lwzux TMP0, BASE, RA - | lwz LFUNC:RB, 4(BASE) - | subi NARGS8:RC, NARGS8:RC, 8 - | addi BASE, BASE, 8 - | checkfunc TMP0; bne ->vmeta_call - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs*8 - | add NARGS8:RC, NARGS8:RC, MULTRES - | // Fall through. Assumes BC_CALLT follows. - break; - case BC_CALLT: - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - | lwzux TMP0, RA, BASE - | lwz LFUNC:RB, 4(RA) - | subi NARGS8:RC, NARGS8:RC, 8 - | lwz TMP1, FRAME_PC(BASE) - | checkfunc TMP0 - | addi RA, RA, 8 - | bne ->vmeta_callt - |->BC_CALLT_Z: - | andix. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand. - | lbz TMP3, LFUNC:RB->ffid - | xori TMP2, TMP1, FRAME_VARG - | cmplwi cr1, NARGS8:RC, 0 - | bne >7 - |1: - | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC. - | li TMP2, 0 - | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function? - | beq cr1, >3 - |2: - | addi TMP3, TMP2, 8 - | lfdx f0, RA, TMP2 - | cmplw cr1, TMP3, NARGS8:RC - | stfdx f0, BASE, TMP2 - | mr TMP2, TMP3 - | bne cr1, <2 - |3: - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt - | beq >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | lwz INS, -4(TMP1) - | decode_RA8 RA, INS - | sub TMP1, BASE, RA - | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1) - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE. - | b <4 - | - |7: // Tailcall from a vararg function. - | andix. TMP0, TMP2, FRAME_TYPEP - | bne <1 // Vararg frame below? - | sub BASE, BASE, TMP2 // Relocate BASE down. - | lwz TMP1, FRAME_PC(BASE) - | andix. TMP0, TMP1, FRAME_TYPE - | b <1 - break; - - case BC_ITERC: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8)) - | mr TMP2, BASE - | add BASE, BASE, RA - | lwz TMP1, -24(BASE) - | lwz LFUNC:RB, -20(BASE) - | lfd f1, -8(BASE) - | lfd f0, -16(BASE) - | stw TMP1, 0(BASE) // Copy callable. - | stw LFUNC:RB, 4(BASE) - | checkfunc TMP1 - | stfd f1, 16(BASE) // Copy control var. - | li NARGS8:RC, 16 // Iterators get 2 arguments. - | stfdu f0, 8(BASE) // Copy state. - | bne ->vmeta_call - | ins_call - break; - - case BC_ITERN: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8) - |.if JIT - | // NYI: add hotloop, record BC_ITERN. - |.endif - | add RA, BASE, RA - | lwz TAB:RB, -12(RA) - | lwz RC, -4(RA) // Get index from control var. - | lwz TMP0, TAB:RB->asize - | lwz TMP1, TAB:RB->array - | addi PC, PC, 4 - |1: // Traverse array part. - | cmplw RC, TMP0 - | slwi TMP3, RC, 3 - | bge >5 // Index points after array part? - | lwzx TMP2, TMP1, TMP3 - | lfdx f0, TMP1, TMP3 - | checknil TMP2 - | lwz INS, -4(PC) - | beq >4 - |.if DUALNUM - | stw RC, 4(RA) - | stw TISNUM, 0(RA) - |.else - | tonum_u f1, RC - |.endif - | addi RC, RC, 1 - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | stfd f0, 8(RA) - | decode_RD4 TMP1, INS - | stw RC, -4(RA) // Update control var. - | add PC, TMP1, TMP3 - |.if not DUALNUM - | stfd f1, 0(RA) - |.endif - |3: - | ins_next - | - |4: // Skip holes in array part. - | addi RC, RC, 1 - | b <1 - | - |5: // Traverse hash part. - | lwz TMP1, TAB:RB->hmask - | sub RC, RC, TMP0 - | lwz TMP2, TAB:RB->node - |6: - | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1. - | slwi TMP3, RC, 5 - | bgty <3 - | slwi RB, RC, 3 - | sub TMP3, TMP3, RB - | lwzx RB, TMP2, TMP3 - | lfdx f0, TMP2, TMP3 - | add NODE:TMP3, TMP2, TMP3 - | checknil RB - | lwz INS, -4(PC) - | beq >7 - | lfd f1, NODE:TMP3->key - | addis TMP2, PC, -(BCBIAS_J*4 >> 16) - | stfd f0, 8(RA) - | add RC, RC, TMP0 - | decode_RD4 TMP1, INS - | stfd f1, 0(RA) - | addi RC, RC, 1 - | add PC, TMP1, TMP2 - | stw RC, -4(RA) // Update control var. - | b <3 - | - |7: // Skip holes in hash part. - | addi RC, RC, 1 - | b <6 - break; - - case BC_ISNEXT: - | // RA = base*8, RD = target (points to ITERN) - | add RA, BASE, RA - | lwz TMP0, -24(RA) - | lwz CFUNC:TMP1, -20(RA) - | lwz TMP2, -16(RA) - | lwz TMP3, -8(RA) - | cmpwi cr0, TMP2, LJ_TTAB - | cmpwi cr1, TMP0, LJ_TFUNC - | cmpwi cr6, TMP3, LJ_TNIL - | bne cr1, >5 - | lbz TMP1, CFUNC:TMP1->ffid - | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq - | cmpwi cr7, TMP1, FF_next_N - | srwi TMP0, RD, 1 - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq - | add TMP3, PC, TMP0 - | bne cr0, >5 - | lus TMP1, 0xfffe - | ori TMP1, TMP1, 0x7fff - | stw ZERO, -4(RA) // Initialize control var. - | stw TMP1, -8(RA) - | addis PC, TMP3, -(BCBIAS_J*4 >> 16) - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | li TMP0, BC_JMP - | li TMP1, BC_ITERC - | stb TMP0, -1(PC) - | addis PC, TMP3, -(BCBIAS_J*4 >> 16) - | stb TMP1, 3(PC) - | b <1 - break; - - case BC_VARG: - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | lwz TMP0, FRAME_PC(BASE) - | add RC, BASE, RC - | add RA, BASE, RA - | addi RC, RC, FRAME_VARG - | add TMP2, RA, RB - | subi TMP3, BASE, 8 // TMP3 = vtop - | sub RC, RC, TMP0 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | cmplwi cr1, RB, 0 - |.if PPE - | sub TMP1, TMP3, RC - | cmpwi TMP1, 0 - |.else - | sub. TMP1, TMP3, RC - |.endif - | beq cr1, >5 // Copy all varargs? - | subi TMP2, TMP2, 16 - | ble >2 // No vararg slots? - |1: // Copy vararg slots to destination slots. - | lfd f0, 0(RC) - | addi RC, RC, 8 - | stfd f0, 0(RA) - | cmplw RA, TMP2 - | cmplw cr1, RC, TMP3 - | bge >3 // All destination slots filled? - | addi RA, RA, 8 - | blt cr1, <1 // More vararg slots? - |2: // Fill up remainder with nil. - | stw TISNIL, 0(RA) - | cmplw RA, TMP2 - | addi RA, RA, 8 - | blt <2 - |3: - | ins_next - | - |5: // Copy all varargs. - | lwz TMP0, L->maxstack - | li MULTRES, 8 // MULTRES = (0+1)*8 - | bley <3 // No vararg slots? - | add TMP2, RA, TMP1 - | cmplw TMP2, TMP0 - | addi MULTRES, TMP1, 8 - | bgt >7 - |6: - | lfd f0, 0(RC) - | addi RC, RC, 8 - | stfd f0, 0(RA) - | cmplw RC, TMP3 - | addi RA, RA, 8 - | blt <6 // More vararg slots? - | b <3 - | - |7: // Grow stack for varargs. - | mr CARG1, L - | stp RA, L->top - | sub SAVE0, RC, BASE // Need delta, because BASE may change. - | stp BASE, L->base - | sub RA, RA, BASE - | stw PC, SAVE_PC - | srwi CARG2, TMP1, 3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | lp BASE, L->base - | add RA, BASE, RA - | add RC, BASE, SAVE0 - | subi TMP3, BASE, 8 - | b <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RD = extra_nresults*8 - | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8. - | // Fall through. Assumes BC_RET follows. - break; - - case BC_RET: - | // RA = results*8, RD = (nresults+1)*8 - | lwz PC, FRAME_PC(BASE) - | add RA, BASE, RA - | mr MULTRES, RD - |1: - | andix. TMP0, PC, FRAME_TYPE - | xori TMP1, PC, FRAME_VARG - | bne ->BC_RETV_Z - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return - | lwz INS, -4(PC) - | cmpwi RD, 8 - | subi TMP2, BASE, 8 - | subi RC, RD, 8 - | decode_RB8 RB, INS - | beq >3 - | li TMP1, 0 - |2: - | addi TMP3, TMP1, 8 - | lfdx f0, RA, TMP1 - | cmpw TMP3, RC - | stfdx f0, TMP2, TMP1 - | beq >3 - | addi TMP1, TMP3, 8 - | lfdx f1, RA, TMP3 - | cmpw TMP1, RC - | stfdx f1, TMP2, TMP3 - | bne <2 - |3: - |5: - | cmplw RB, RD - | decode_RA8 RA, INS - | bgt >6 - | sub BASE, TMP2, RA - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | stwx TISNIL, TMP2, TMP1 - | b <5 - | - |->BC_RETV_Z: // Non-standard return case. - | andix. TMP2, TMP1, FRAME_TYPEP - | bne ->vm_return - | // Return from vararg function: relocate BASE down. - | sub BASE, BASE, TMP1 - | lwz PC, FRAME_PC(BASE) - | b <1 - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RD = (nresults+1)*8 - | lwz PC, FRAME_PC(BASE) - | add RA, BASE, RA - | mr MULTRES, RD - | andix. TMP0, PC, FRAME_TYPE - | xori TMP1, PC, FRAME_VARG - | bney ->BC_RETV_Z - | - | lwz INS, -4(PC) - | subi TMP2, BASE, 8 - | decode_RB8 RB, INS - if (op == BC_RET1) { - | lfd f0, 0(RA) - | stfd f0, 0(TMP2) - } - |5: - | cmplw RB, RD - | decode_RA8 RA, INS - | bgt >6 - | sub BASE, TMP2, RA - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | stwx TISNIL, TMP2, TMP1 - | b <5 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RD = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - |.if DUALNUM - | // Integer loop. - | lwzux TMP1, RA, BASE - | lwz CARG1, FORL_IDX*8+4(RA) - | cmplw cr0, TMP1, TISNUM - if (vk) { - | lwz CARG3, FORL_STEP*8+4(RA) - | bne >9 - |.if GPR64 - | // Need to check overflow for (a<<32) + (b<<32). - | rldicr TMP0, CARG1, 32, 31 - | rldicr TMP2, CARG3, 32, 31 - | add CARG1, CARG1, CARG3 - | addo. TMP0, TMP0, TMP2 - |.else - | addo. CARG1, CARG1, CARG3 - |.endif - | cmpwi cr6, CARG3, 0 - | lwz CARG2, FORL_STOP*8+4(RA) - | bso >6 - |4: - | stw CARG1, FORL_IDX*8+4(RA) - } else { - | lwz TMP3, FORL_STEP*8(RA) - | lwz CARG3, FORL_STEP*8+4(RA) - | lwz TMP2, FORL_STOP*8(RA) - | lwz CARG2, FORL_STOP*8+4(RA) - | cmplw cr7, TMP3, TISNUM - | cmplw cr1, TMP2, TISNUM - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq - | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | cmpwi cr6, CARG3, 0 - | bne >9 - } - | blt cr6, >5 - | cmpw CARG1, CARG2 - |1: - | stw TISNUM, FORL_EXT*8(RA) - if (op != BC_JFORL) { - | srwi RD, RD, 1 - } - | stw CARG1, FORL_EXT*8+4(RA) - if (op != BC_JFORL) { - | add RD, PC, RD - } - if (op == BC_FORI) { - | bgt >3 // See FP loop below. - } else if (op == BC_JFORI) { - | addis PC, RD, -(BCBIAS_J*4 >> 16) - | bley >7 - } else if (op == BC_IFORL) { - | bgt >2 - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } else { - | bley =>BC_JLOOP - } - |2: - | ins_next - |5: // Invert check for negative step. - | cmpw CARG2, CARG1 - | b <1 - if (vk) { - |6: // Potential overflow. - | mcrxr cr0; bley <4 // Ignore unrelated overflow. - | b <2 - } - |.endif - if (vk) { - |.if DUALNUM - |9: // FP loop. - | lfd f1, FORL_IDX*8(RA) - |.else - | lfdux f1, RA, BASE - |.endif - | lfd f3, FORL_STEP*8(RA) - | lfd f2, FORL_STOP*8(RA) - | lwz TMP3, FORL_STEP*8(RA) - | fadd f1, f1, f3 - | stfd f1, FORL_IDX*8(RA) - } else { - |.if DUALNUM - |9: // FP loop. - |.else - | lwzux TMP1, RA, BASE - | lwz TMP3, FORL_STEP*8(RA) - | lwz TMP2, FORL_STOP*8(RA) - | cmplw cr0, TMP1, TISNUM - | cmplw cr7, TMP3, TISNUM - | cmplw cr1, TMP2, TISNUM - |.endif - | lfd f1, FORL_IDX*8(RA) - | crand 4*cr0+lt, 4*cr0+lt, 4*cr7+lt - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | lfd f2, FORL_STOP*8(RA) - | bge ->vmeta_for - } - | cmpwi cr6, TMP3, 0 - if (op != BC_JFORL) { - | srwi RD, RD, 1 - } - | stfd f1, FORL_EXT*8(RA) - if (op != BC_JFORL) { - | add RD, PC, RD - } - | fcmpu cr0, f1, f2 - if (op == BC_JFORI) { - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } - | blt cr6, >5 - if (op == BC_FORI) { - | bgt >3 - } else if (op == BC_IFORL) { - |.if DUALNUM - | bgty <2 - |.else - | bgt >2 - |.endif - |1: - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } else if (op == BC_JFORI) { - | bley >7 - } else { - | bley =>BC_JLOOP - } - |.if DUALNUM - | b <2 - |.else - |2: - | ins_next - |.endif - |5: // Negative step. - if (op == BC_FORI) { - | bge <2 - |3: // Used by integer loop, too. - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } else if (op == BC_IFORL) { - | bgey <1 - } else if (op == BC_JFORI) { - | bgey >7 - } else { - | bgey =>BC_JLOOP - } - | b <2 - if (op == BC_JFORI) { - |7: - | lwz INS, -4(PC) - | decode_RD8 RD, INS - | b =>BC_JLOOP - } - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RD = target - | lwzux TMP1, RA, BASE - | lwz TMP2, 4(RA) - | checknil TMP1; beq >1 // Stop if iterator returned nil. - if (op == BC_JITERL) { - | stw TMP1, -8(RA) - | stw TMP2, -4(RA) - | b =>BC_JLOOP - } else { - | branch_RD // Otherwise save control var + branch. - | stw TMP1, -8(RA) - | stw TMP2, -4(RA) - } - |1: - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base*8 (ignored), RD = traceno*8 - | lwz TMP1, DISPATCH_J(trace)(DISPATCH) - | srwi RD, RD, 1 - | // Traces on PPC don't store the trace number, so use 0. - | stw ZERO, DISPATCH_GL(vmstate)(DISPATCH) - | lwzx TRACE:TMP2, TMP1, RD - | mcrxr cr0 // Clear SO flag. - | lp TMP2, TRACE:TMP2->mcode - | stw BASE, DISPATCH_GL(jit_base)(DISPATCH) - | mtctr TMP2 - | stw L, DISPATCH_GL(jit_L)(DISPATCH) - | addi JGL, DISPATCH, GG_DISP2G+32768 - | bctr - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RD = target - | branch_RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lwz TMP2, L->maxstack - | lbz TMP1, -4+PC2PROTO(numparams)(PC) - | lwz KBASE, -4+PC2PROTO(k)(PC) - | cmplw RA, TMP2 - | slwi TMP1, TMP1, 3 - | bgt ->vm_growstack_l - if (op != BC_JFUNCF) { - | ins_next1 - } - |2: - | cmplw NARGS8:RC, TMP1 // Check for missing parameters. - | blt >3 - if (op == BC_JFUNCF) { - | decode_RD8 RD, INS - | b =>BC_JLOOP - } else { - | ins_next2 - } - | - |3: // Clear missing parameters. - | stwx TISNIL, BASE, NARGS8:RC - | addi NARGS8:RC, NARGS8:RC, 8 - | b <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lwz TMP2, L->maxstack - | add TMP1, BASE, RC - | add TMP0, RA, RC - | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC. - | addi TMP3, RC, 8+FRAME_VARG - | lwz KBASE, -4+PC2PROTO(k)(PC) - | cmplw TMP0, TMP2 - | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG. - | bge ->vm_growstack_l - | lbz TMP2, -4+PC2PROTO(numparams)(PC) - | mr RA, BASE - | mr RC, TMP1 - | ins_next1 - | cmpwi TMP2, 0 - | addi BASE, TMP1, 8 - | beq >3 - |1: - | cmplw RA, RC // Less args than parameters? - | lwz TMP0, 0(RA) - | lwz TMP3, 4(RA) - | bge >4 - | stw TISNIL, 0(RA) // Clear old fixarg slot (help the GC). - | addi RA, RA, 8 - |2: - | addic. TMP2, TMP2, -1 - | stw TMP0, 8(TMP1) - | stw TMP3, 12(TMP1) - | addi TMP1, TMP1, 8 - | bne <1 - |3: - | ins_next2 - | - |4: // Clear missing parameters. - | li TMP0, LJ_TNIL - | b <2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | lp RD, CFUNC:RB->f - } else { - | lp RD, DISPATCH_GL(wrapf)(DISPATCH) - } - | add TMP1, RA, NARGS8:RC - | lwz TMP2, L->maxstack - | .toc lp TMP3, 0(RD) - | add RC, BASE, NARGS8:RC - | stp BASE, L->base - | cmplw TMP1, TMP2 - | stp RC, L->top - | li_vmstate C - |.if TOC - | mtctr TMP3 - |.else - | mtctr RD - |.endif - if (op == BC_FUNCCW) { - | lp CARG2, CFUNC:RB->f - } - | mr CARG1, L - | bgt ->vm_growstack_c // Need to grow stack. - | .toc lp TOCREG, TOC_OFS(RD) - | .tocenv lp ENVREG, ENV_OFS(RD) - | st_vmstate - | bctrl // (lua_State *L [, lua_CFunction f]) - | // Returns nresults. - | lp BASE, L->base - | .toc ld TOCREG, SAVE_TOC - | slwi RD, CRET1, 3 - | lp TMP1, L->top - | li_vmstate INTERP - | lwz PC, FRAME_PC(BASE) // Fetch PC of caller. - | sub RA, TMP1, RD // RA = L->top - nresults*8 - | st_vmstate - | b ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" - "\t.long .Lbegin\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n", - fcofs, CFRAME_SIZE); - for (i = 14; i <= 31; i++) - fprintf(ctx->fp, - "\t.byte %d\n\t.uleb128 %d\n" - "\t.byte %d\n\t.uleb128 %d\n", - 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i)); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" -#if LJ_TARGET_PS3 - "\t.long .lj_vm_ffi_call\n" -#else - "\t.long lj_vm_ffi_call\n" -#endif - "\t.long %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x8e\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0xe\n" - "\t.align 2\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if !LJ_NO_UNWIND - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe1:\n" - "\t.long .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.long lj_err_unwind_dwarf-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.long .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.long .LASFDE2-.Lframe1\n" - "\t.long .Lbegin-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n", - fcofs, CFRAME_SIZE); - for (i = 14; i <= 31; i++) - fprintf(ctx->fp, - "\t.byte %d\n\t.uleb128 %d\n" - "\t.byte %d\n\t.uleb128 %d\n", - 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i)); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE2:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.long .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.long .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.long .LASFDE3-.Lframe2\n" - "\t.long lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x8e\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0xe\n" - "\t.align 2\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif -#endif - break; - default: - break; - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/vm_ppcspe.dasc b/third-party/LuaJIT-2.0.2/src/vm_ppcspe.dasc deleted file mode 100644 index 293e391913..0000000000 --- a/third-party/LuaJIT-2.0.2/src/vm_ppcspe.dasc +++ /dev/null @@ -1,3691 +0,0 @@ -|// Low-level VM code for PowerPC/e500 CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -| -|.arch ppc -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -|// Don't use: r1 = sp, r2 and r13 = reserved and/or small data area ptr -| -|// The following must be C callee-save (but BASE is often refetched). -|.define BASE, r14 // Base of current Lua stack frame. -|.define KBASE, r15 // Constants of current Lua function. -|.define PC, r16 // Next PC. -|.define DISPATCH, r17 // Opcode dispatch table. -|.define LREG, r18 // Register holding lua_State (also in SAVE_L). -|.define MULTRES, r19 // Size of multi-result: (nresults+1)*8. -| -|// Constants for vectorized type-comparisons (hi+low GPR). C callee-save. -|.define TISNUM, r22 -|.define TISSTR, r23 -|.define TISTAB, r24 -|.define TISFUNC, r25 -|.define TISNIL, r26 -|.define TOBIT, r27 -|.define ZERO, TOBIT // Zero in lo word. -| -|// The following temporaries are not saved across C calls, except for RA. -|.define RA, r20 // Callee-save. -|.define RB, r10 -|.define RC, r11 -|.define RD, r12 -|.define INS, r7 // Overlaps CARG5. -| -|.define TMP0, r0 -|.define TMP1, r8 -|.define TMP2, r9 -|.define TMP3, r6 // Overlaps CARG4. -| -|// Saved temporaries. -|.define SAVE0, r21 -| -|// Calling conventions. -|.define CARG1, r3 -|.define CARG2, r4 -|.define CARG3, r5 -|.define CARG4, r6 // Overlaps TMP3. -|.define CARG5, r7 // Overlaps INS. -| -|.define CRET1, r3 -|.define CRET2, r4 -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.define SAVE_LR, 188(sp) -|.define CFRAME_SPACE, 184 // Delta for sp. -|// Back chain for sp: 184(sp) <-- sp entering interpreter -|.define SAVE_r31, 176(sp) // 64 bit register saves. -|.define SAVE_r30, 168(sp) -|.define SAVE_r29, 160(sp) -|.define SAVE_r28, 152(sp) -|.define SAVE_r27, 144(sp) -|.define SAVE_r26, 136(sp) -|.define SAVE_r25, 128(sp) -|.define SAVE_r24, 120(sp) -|.define SAVE_r23, 112(sp) -|.define SAVE_r22, 104(sp) -|.define SAVE_r21, 96(sp) -|.define SAVE_r20, 88(sp) -|.define SAVE_r19, 80(sp) -|.define SAVE_r18, 72(sp) -|.define SAVE_r17, 64(sp) -|.define SAVE_r16, 56(sp) -|.define SAVE_r15, 48(sp) -|.define SAVE_r14, 40(sp) -|.define SAVE_CR, 36(sp) -|.define UNUSED1, 32(sp) -|.define SAVE_ERRF, 28(sp) // 32 bit C frame info. -|.define SAVE_NRES, 24(sp) -|.define SAVE_CFRAME, 20(sp) -|.define SAVE_L, 16(sp) -|.define SAVE_PC, 12(sp) -|.define SAVE_MULTRES, 8(sp) -|// Next frame lr: 4(sp) -|// Back chain for sp: 0(sp) <-- sp while in interpreter -| -|.macro save_, reg; evstdd reg, SAVE_..reg; .endmacro -|.macro rest_, reg; evldd reg, SAVE_..reg; .endmacro -| -|.macro saveregs -| stwu sp, -CFRAME_SPACE(sp) -| save_ r14; save_ r15; save_ r16; save_ r17; save_ r18; save_ r19 -| mflr r0; mfcr r12 -| save_ r20; save_ r21; save_ r22; save_ r23; save_ r24; save_ r25 -| stw r0, SAVE_LR; stw r12, SAVE_CR -| save_ r26; save_ r27; save_ r28; save_ r29; save_ r30; save_ r31 -|.endmacro -| -|.macro restoreregs -| lwz r0, SAVE_LR; lwz r12, SAVE_CR -| rest_ r14; rest_ r15; rest_ r16; rest_ r17; rest_ r18; rest_ r19 -| mtlr r0; mtcrf 0x38, r12 -| rest_ r20; rest_ r21; rest_ r22; rest_ r23; rest_ r24; rest_ r25 -| rest_ r26; rest_ r27; rest_ r28; rest_ r29; rest_ r30; rest_ r31 -| addi sp, sp, CFRAME_SPACE -|.endmacro -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -| -|//----------------------------------------------------------------------- -| -|// These basic macros should really be part of DynASM. -|.macro srwi, rx, ry, n; rlwinm rx, ry, 32-n, n, 31; .endmacro -|.macro slwi, rx, ry, n; rlwinm rx, ry, n, 0, 31-n; .endmacro -|.macro rotlwi, rx, ry, n; rlwinm rx, ry, n, 0, 31; .endmacro -|.macro rotlw, rx, ry, rn; rlwnm rx, ry, rn, 0, 31; .endmacro -|.macro subi, rx, ry, i; addi rx, ry, -i; .endmacro -| -|// Trap for not-yet-implemented parts. -|.macro NYI; tw 4, sp, sp; .endmacro -| -|//----------------------------------------------------------------------- -| -|// Access to frame relative to BASE. -|.define FRAME_PC, -8 -|.define FRAME_FUNC, -4 -| -|// Instruction decode. -|.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro -|.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro -|.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro -|.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro -|.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro -| -|.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro -|.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| lwz INS, 0(PC) -| addi PC, PC, 4 -|.endmacro -|// Instruction decode+dispatch. -|.macro ins_NEXT2 -| decode_OP4 TMP1, INS -| decode_RB8 RB, INS -| decode_RD8 RD, INS -| lwzx TMP0, DISPATCH, TMP1 -| decode_RA8 RA, INS -| decode_RC8 RC, INS -| mtctr TMP0 -| bctr -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| lwz PC, LFUNC:RB->pc -| lwz INS, 0(PC) -| addi PC, PC, 4 -| decode_OP4 TMP1, INS -| decode_RA8 RA, INS -| lwzx TMP0, DISPATCH, TMP1 -| add RA, RA, BASE -| mtctr TMP0 -| bctr -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| stw PC, FRAME_PC(BASE) -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to test operand types. -|.macro checknum, reg; evcmpltu reg, TISNUM; .endmacro -|.macro checkstr, reg; evcmpeq reg, TISSTR; .endmacro -|.macro checktab, reg; evcmpeq reg, TISTAB; .endmacro -|.macro checkfunc, reg; evcmpeq reg, TISFUNC; .endmacro -|.macro checknil, reg; evcmpeq reg, TISNIL; .endmacro -|.macro checkok, label; blt label; .endmacro -|.macro checkfail, label; bge label; .endmacro -|.macro checkanyfail, label; bns label; .endmacro -|.macro checkallok, label; bso label; .endmacro -| -|.macro branch_RD -| srwi TMP0, RD, 1 -| add PC, PC, TMP0 -| addis PC, PC, -(BCBIAS_J*4 >> 16) -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro hotloop -| NYI -|.endmacro -| -|.macro hotcall -| NYI -|.endmacro -| -|// Set current VM state. Uses TMP0. -|.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro -|.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp -| lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH) -| // Assumes LJ_GC_BLACK is 0x04. -| rlwinm mark, mark, 0, 30, 28 // black2gray(tab) -| stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH) -| stb mark, tab->marked -| stw tmp, tab->gclist -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: TMP2 = previous base. - | andi. TMP0, PC, FRAME_P - | evsplati TMP1, LJ_TTRUE - | beq ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame. - | mr BASE, TMP2 // Restore caller base. - | // Prepending may overwrite the pcall frame, so do it at the end. - | stwu TMP1, FRAME_PC(RA) // Prepend true to results. - | - |->vm_returnc: - | addi RD, RD, 8 // RD = (nresults+1)*8. - | andi. TMP0, PC, FRAME_TYPE - | cmpwi cr1, RD, 0 - | li CRET1, LUA_YIELD - | beq cr1, ->vm_unwind_c_eh - | mr MULTRES, RD - | beq ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return - | // TMP0 = PC & FRAME_TYPE - | cmpwi TMP0, FRAME_C - | rlwinm TMP2, PC, 0, 0, 28 - | li_vmstate C - | sub TMP2, BASE, TMP2 // TMP2 = previous base. - | bne ->vm_returnp - | - | addic. TMP1, RD, -8 - | stw TMP2, L->base - | lwz TMP2, SAVE_NRES - | subi BASE, BASE, 8 - | st_vmstate - | slwi TMP2, TMP2, 3 - | beq >2 - |1: - | addic. TMP1, TMP1, -8 - | evldd TMP0, 0(RA) - | addi RA, RA, 8 - | evstdd TMP0, 0(BASE) - | addi BASE, BASE, 8 - | bne <1 - | - |2: - | cmpw TMP2, RD // More/less results wanted? - | bne >6 - |3: - | stw BASE, L->top // Store new top. - | - |->vm_leave_cp: - | lwz TMP0, SAVE_CFRAME // Restore previous C frame. - | li CRET1, 0 // Ok return status for vm_pcall. - | stw TMP0, L->cframe - | - |->vm_leave_unw: - | restoreregs - | blr - | - |6: - | ble >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | lwz TMP1, L->maxstack - | cmplw BASE, TMP1 - | bge >8 - | evstdd TISNIL, 0(BASE) - | addi RD, RD, 8 - | addi BASE, BASE, 8 - | b <2 - | - |7: // Less results wanted. - | sub TMP0, RD, TMP2 - | cmpwi TMP2, 0 // LUA_MULTRET+1 case? - | sub TMP0, BASE, TMP0 // Subtract the difference. - | iseleq BASE, BASE, TMP0 // Either keep top or shrink it. - | b <3 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | stw BASE, L->top // Save current top held in BASE (yes). - | mr SAVE0, RD - | mr CARG2, TMP2 - | mr CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | lwz TMP2, SAVE_NRES - | mr RD, SAVE0 - | slwi TMP2, TMP2, 3 - | lwz BASE, L->top // Need the (realloced) L->top in BASE. - | b <2 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | mr sp, CARG1 - | mr CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | lwz L, SAVE_L - | li TMP0, ~LJ_VMST_C - | lwz GL:TMP1, L->glref - | stw TMP0, GL:TMP1->vmstate - | b ->vm_leave_unw - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | rlwinm sp, CARG1, 0, 0, 29 - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | lwz L, SAVE_L - | evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants. - | evsplati TISFUNC, LJ_TFUNC - | lus TOBIT, 0x4338 - | evsplati TISTAB, LJ_TTAB - | li TMP0, 0 - | lwz BASE, L->base - | evmergelo TOBIT, TOBIT, TMP0 - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | evsplati TISSTR, LJ_TSTR - | li TMP1, LJ_TFALSE - | evsplati TISNIL, LJ_TNIL - | li_vmstate INTERP - | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame. - | la RA, -8(BASE) // Results start at BASE-8. - | addi DISPATCH, DISPATCH, GG_G2DISP - | stw TMP1, 0(RA) // Prepend false to error message. - | li RD, 16 // 2 results: false + error message. - | st_vmstate - | b ->vm_returnc - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | li CARG2, LUA_MINSTACK - | b >2 - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | add RC, BASE, RC - | sub RA, RA, BASE - | stw BASE, L->base - | addi PC, PC, 4 // Must point after first instruction. - | stw RC, L->top - | srwi CARG2, RA, 3 - |2: - | // L->base = new base, L->top = top - | stw PC, SAVE_PC - | mr CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | lwz BASE, L->base - | lwz RC, L->top - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | sub RC, RC, BASE - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | mr L, CARG1 - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | mr BASE, CARG2 - | lbz TMP1, L->status - | stw L, SAVE_L - | li PC, FRAME_CP - | addi TMP0, sp, CFRAME_RESUME - | addi DISPATCH, DISPATCH, GG_G2DISP - | stw CARG3, SAVE_NRES - | cmplwi TMP1, 0 - | stw CARG3, SAVE_ERRF - | stw TMP0, L->cframe - | stw CARG3, SAVE_CFRAME - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | beq >3 - | - | // Resume after yield (like a return). - | mr RA, BASE - | lwz BASE, L->base - | evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants. - | lwz TMP1, L->top - | evsplati TISFUNC, LJ_TFUNC - | lus TOBIT, 0x4338 - | evsplati TISTAB, LJ_TTAB - | lwz PC, FRAME_PC(BASE) - | li TMP2, 0 - | evsplati TISSTR, LJ_TSTR - | sub RD, TMP1, BASE - | evmergelo TOBIT, TOBIT, TMP2 - | stb CARG3, L->status - | andi. TMP0, PC, FRAME_TYPE - | li_vmstate INTERP - | addi RD, RD, 8 - | evsplati TISNIL, LJ_TNIL - | mr MULTRES, RD - | st_vmstate - | beq ->BC_RET_Z - | b ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | li PC, FRAME_CP - | stw CARG4, SAVE_ERRF - | b >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | li PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | lwz TMP1, L:CARG1->cframe - | stw CARG3, SAVE_NRES - | mr L, CARG1 - | stw CARG1, SAVE_L - | mr BASE, CARG2 - | stw sp, L->cframe // Add our C frame to cframe chain. - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | stw TMP1, SAVE_CFRAME - | addi DISPATCH, DISPATCH, GG_G2DISP - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | lwz TMP2, L->base // TMP2 = old base (used in vmeta_call). - | evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants. - | lwz TMP1, L->top - | evsplati TISFUNC, LJ_TFUNC - | add PC, PC, BASE - | evsplati TISTAB, LJ_TTAB - | lus TOBIT, 0x4338 - | li TMP0, 0 - | sub PC, PC, TMP2 // PC = frame delta + frame type - | evsplati TISSTR, LJ_TSTR - | sub NARGS8:RC, TMP1, BASE - | evmergelo TOBIT, TOBIT, TMP0 - | li_vmstate INTERP - | evsplati TISNIL, LJ_TNIL - | st_vmstate - | - |->vm_call_dispatch: - | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC - | li TMP0, -8 - | evlddx LFUNC:RB, BASE, TMP0 - | checkfunc LFUNC:RB - | checkfail ->vmeta_call - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | mr L, CARG1 - | lwz TMP0, L:CARG1->stack - | stw CARG1, SAVE_L - | lwz TMP1, L->top - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top). - | lwz TMP1, L->cframe - | stw sp, L->cframe // Add our C frame to cframe chain. - | li TMP2, 0 - | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame. - | stw TMP2, SAVE_ERRF // No error function. - | stw TMP1, SAVE_CFRAME - | mtctr CARG4 - | bctrl // (lua_State *L, lua_CFunction func, void *ud) - | mr. BASE, CRET1 - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | li PC, FRAME_CP - | addi DISPATCH, DISPATCH, GG_G2DISP - | bne <3 // Else continue with the call. - | b ->vm_leave_cp // No base? Just remove C frame. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the - |// stack, so BASE doesn't need to be reloaded across these calls. - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8 - | lwz TMP0, -12(BASE) // Continuation. - | mr RB, BASE - | mr BASE, TMP2 // Restore caller BASE. - | lwz LFUNC:TMP1, FRAME_FUNC(TMP2) - | cmplwi TMP0, 0 - | lwz PC, -16(RB) // Restore PC from [cont|PC]. - | beq >1 - | subi TMP2, RD, 8 - | lwz TMP1, LFUNC:TMP1->pc - | evstddx TISNIL, RA, TMP2 // Ensure one valid arg. - | lwz KBASE, PC2PROTO(k)(TMP1) - | // BASE = base, RA = resultptr, RB = meta base - | mtctr TMP0 - | bctr // Jump to continuation. - | - |1: // Tail call from C function. - | subi TMP1, RB, 16 - | sub RC, TMP1, BASE - | b ->vm_call_tail - | - |->cont_cat: // RA = resultptr, RB = meta base - | lwz INS, -4(PC) - | subi CARG2, RB, 16 - | decode_RB8 SAVE0, INS - | evldd TMP0, 0(RA) - | add TMP1, BASE, SAVE0 - | stw BASE, L->base - | cmplw TMP1, CARG2 - | sub CARG3, CARG2, TMP1 - | decode_RA8 RA, INS - | evstdd TMP0, 0(CARG2) - | bne ->BC_CAT_Z - | evstddx TMP0, BASE, RA - | b ->cont_nop - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | evmergelo STR:RC, TISSTR, STR:RC - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | decode_RB8 RB, INS - | evstdd STR:RC, 0(CARG3) - | add CARG2, BASE, RB - | b >1 - | - |->vmeta_tgets: - | evmergelo TAB:RB, TISTAB, TAB:RB - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - | evmergelo STR:RC, TISSTR, STR:RC - | evstdd TAB:RB, 0(CARG2) - | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH) - | evstdd STR:RC, 0(CARG3) - | b >1 - | - |->vmeta_tgetb: // TMP0 = index - | efdcfsi TMP0, TMP0 - | decode_RB8 RB, INS - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | add CARG2, BASE, RB - | evstdd TMP0, 0(CARG3) - | b >1 - | - |->vmeta_tgetv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | stw BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | cmplwi CRET1, 0 - | beq >3 - | evldd TMP0, 0(CRET1) - | evstddx TMP0, BASE, RA - | ins_next - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | subfic TMP1, BASE, FRAME_CONT - | lwz BASE, L->top - | stw PC, -16(BASE) // [cont|PC] - | add PC, TMP1, BASE - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | li NARGS8:RC, 16 // 2 args for func(t, k). - | b ->vm_call_dispatch_f - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | evmergelo STR:RC, TISSTR, STR:RC - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | decode_RB8 RB, INS - | evstdd STR:RC, 0(CARG3) - | add CARG2, BASE, RB - | b >1 - | - |->vmeta_tsets: - | evmergelo TAB:RB, TISTAB, TAB:RB - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - | evmergelo STR:RC, TISSTR, STR:RC - | evstdd TAB:RB, 0(CARG2) - | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH) - | evstdd STR:RC, 0(CARG3) - | b >1 - | - |->vmeta_tsetb: // TMP0 = index - | efdcfsi TMP0, TMP0 - | decode_RB8 RB, INS - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | add CARG2, BASE, RB - | evstdd TMP0, 0(CARG3) - | b >1 - | - |->vmeta_tsetv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | stw BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | cmplwi CRET1, 0 - | evlddx TMP0, BASE, RA - | beq >3 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | evstdd TMP0, 0(CRET1) - | ins_next - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | subfic TMP1, BASE, FRAME_CONT - | lwz BASE, L->top - | stw PC, -16(BASE) // [cont|PC] - | add PC, TMP1, BASE - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | li NARGS8:RC, 24 // 3 args for func(t, k, v) - | evstdd TMP0, 16(BASE) // Copy value to third argument. - | b ->vm_call_dispatch_f - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | mr CARG1, L - | subi PC, PC, 4 - | add CARG2, BASE, RA - | stw PC, SAVE_PC - | add CARG3, BASE, RD - | stw BASE, L->base - | decode_OP1 CARG4, INS - | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // Returns 0/1 or TValue * (metamethod). - |3: - | cmplwi CRET1, 1 - | bgt ->vmeta_binop - |4: - | lwz INS, 0(PC) - | addi PC, PC, 4 - | decode_RD4 TMP2, INS - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | add TMP2, TMP2, TMP3 - | isellt PC, PC, TMP2 - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | lwz INS, -4(PC) - | evldd TMP0, 0(RA) - | decode_RA8 TMP1, INS - | evstddx TMP0, BASE, TMP1 - | b ->cont_nop - | - |->cont_condt: // RA = resultptr - | lwz TMP0, 0(RA) - | li TMP1, LJ_TTRUE - | cmplw TMP1, TMP0 // Branch if result is true. - | b <4 - | - |->cont_condf: // RA = resultptr - | lwz TMP0, 0(RA) - | li TMP1, LJ_TFALSE - | cmplw TMP0, TMP1 // Branch if result is false. - | b <4 - | - |->vmeta_equal: - | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. - | subi PC, PC, 4 - | stw BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_vn: - | add CARG3, BASE, RB - | add CARG4, KBASE, RC - | b >1 - | - |->vmeta_arith_nv: - | add CARG3, KBASE, RC - | add CARG4, BASE, RB - | b >1 - | - |->vmeta_unm: - | add CARG3, BASE, RD - | mr CARG4, CARG3 - | b >1 - | - |->vmeta_arith_vv: - | add CARG3, BASE, RB - | add CARG4, BASE, RC - |1: - | add CARG2, BASE, RA - | stw BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS. - | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // Returns NULL (finished) or TValue * (metamethod). - | cmplwi CRET1, 0 - | beq ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | sub TMP1, CRET1, BASE - | stw PC, -16(CRET1) // [cont|PC] - | mr TMP2, BASE - | addi PC, TMP1, FRAME_CONT - | mr BASE, CRET1 - | li NARGS8:RC, 16 // 2 args for func(o1, o2). - | b ->vm_call_dispatch - | - |->vmeta_len: -#if LJ_52 - | mr SAVE0, CARG1 -#endif - | add CARG2, BASE, RD - | stw BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_len // (lua_State *L, TValue *o) - | // Returns NULL (retry) or TValue * (metamethod base). -#if LJ_52 - | cmplwi CRET1, 0 - | bne ->vmeta_binop // Binop call for compatibility. - | mr CARG1, SAVE0 - | b ->BC_LEN_Z -#else - | b ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // TMP2 = old base, BASE = new base, RC = nargs*8 - | mr CARG1, L - | stw TMP2, L->base // This is the callers base! - | subi CARG2, BASE, 8 - | stw PC, SAVE_PC - | add CARG3, BASE, RC - | mr SAVE0, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | addi NARGS8:RC, SAVE0, 8 // Got one more argument now. - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | mr CARG1, L - | stw BASE, L->base - | subi CARG2, RA, 8 - | stw PC, SAVE_PC - | add CARG3, RA, RC - | mr SAVE0, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | lwz TMP1, FRAME_PC(BASE) - | addi NARGS8:RC, SAVE0, 8 // Got one more argument now. - | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here. - | b ->BC_CALLT_Z - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mr CARG1, L - | stw BASE, L->base - | mr CARG2, RA - | stw PC, SAVE_PC - | mr SAVE0, INS - | bl extern lj_meta_for // (lua_State *L, TValue *base) - |.if JIT - | decode_OP1 TMP0, SAVE0 - |.endif - | decode_RA8 RA, SAVE0 - |.if JIT - | cmpwi TMP0, BC_JFORI - |.endif - | decode_RD8 RD, SAVE0 - |.if JIT - | beq =>BC_JFORI - |.endif - | b =>BC_FORI - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 8 - | evldd CARG1, 0(BASE) - | blt ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 16 - | evldd CARG1, 0(BASE) - | evldd CARG2, 8(BASE) - | blt ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name - | .ffunc_1 name - | checknum CARG1 - | checkfail ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name - | .ffunc_2 name - | evmergehi TMP0, CARG1, CARG2 - | checknum TMP0 - | checkanyfail ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1. - |.macro ffgccheck - | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | cmplw TMP0, TMP1 - | bgel ->fff_gcstep - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc assert - | cmplwi NARGS8:RC, 8 - | evldd TMP0, 0(BASE) - | blt ->fff_fallback - | evaddw TMP1, TISNIL, TISNIL // Synthesize LJ_TFALSE. - | la RA, -8(BASE) - | evcmpltu cr1, TMP0, TMP1 - | lwz PC, FRAME_PC(BASE) - | bge cr1, ->fff_fallback - | evstdd TMP0, 0(RA) - | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8. - | beq ->fff_res // Done if exactly 1 argument. - | li TMP1, 8 - | subi RC, RC, 8 - |1: - | cmplw TMP1, RC - | evlddx TMP0, BASE, TMP1 - | evstddx TMP0, RA, TMP1 - | addi TMP1, TMP1, 8 - | bne <1 - | b ->fff_res - | - |.ffunc type - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - | blt ->fff_fallback - | li TMP2, ~LJ_TNUMX - | cmplw CARG1, TISNUM - | not TMP1, CARG1 - | isellt TMP1, TMP2, TMP1 - | slwi TMP1, TMP1, 3 - | la TMP2, CFUNC:RB->upvalue - | evlddx STR:CRET1, TMP2, TMP1 - | b ->fff_restv - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | checktab CARG1 - | evmergehi TMP1, CARG1, CARG1 - | checkfail >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | lwz TAB:RB, TAB:CARG1->metatable - |2: - | evmr CRET1, TISNIL - | cmplwi TAB:RB, 0 - | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH) - | beq ->fff_restv - | lwz TMP0, TAB:RB->hmask - | evmergelo CRET1, TISTAB, TAB:RB // Use metatable as default result. - | lwz TMP1, STR:RC->hash - | lwz NODE:TMP2, TAB:RB->node - | evmergelo STR:RC, TISSTR, STR:RC - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |3: // Rearranged logic, because we expect _not_ to find the key. - | evldd TMP0, NODE:TMP2->key - | evldd TMP1, NODE:TMP2->val - | evcmpeq TMP0, STR:RC - | lwz NODE:TMP2, NODE:TMP2->next - | checkallok >5 - | cmplwi NODE:TMP2, 0 - | beq ->fff_restv // Not found, keep default result. - | b <3 - |5: - | checknil TMP1 - | checkok ->fff_restv // Ditto for nil value. - | evmr CRET1, TMP1 // Return value of mt.__metatable. - | b ->fff_restv - | - |6: - | cmpwi TMP1, LJ_TUDATA - | not TMP1, TMP1 - | beq <1 - | checknum CARG1 - | slwi TMP1, TMP1, 2 - | li TMP2, 4*~LJ_TNUMX - | isellt TMP1, TMP2, TMP1 - | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH) - | lwzx TAB:RB, TMP2, TMP1 - | b <2 - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | evmergehi TMP0, TAB:CARG1, TAB:CARG2 - | checktab TMP0 - | checkanyfail ->fff_fallback - | lwz TAB:TMP1, TAB:CARG1->metatable - | cmplwi TAB:TMP1, 0 - | lbz TMP3, TAB:CARG1->marked - | bne ->fff_fallback - | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - | stw TAB:CARG2, TAB:CARG1->metatable - | beq ->fff_restv - | barrierback TAB:CARG1, TMP3, TMP0 - | b ->fff_restv - | - |.ffunc rawget - | cmplwi NARGS8:RC, 16 - | evldd CARG2, 0(BASE) - | blt ->fff_fallback - | checktab CARG2 - | la CARG3, 8(BASE) - | checkfail ->fff_fallback - | mr CARG1, L - | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // Returns cTValue *. - | evldd CRET1, 0(CRET1) - | b ->fff_restv - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | cmplwi NARGS8:RC, 8 - | evldd CARG1, 0(BASE) - | bne ->fff_fallback // Exactly one argument. - | checknum CARG1 - | checkok ->fff_restv - | b ->fff_fallback - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | checkstr CARG1 - | // A __tostring method in the string base metatable is ignored. - | checkok ->fff_restv // String key? - | // Handle numbers inline, unless a number base metatable is present. - | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH) - | checknum CARG1 - | cmplwi cr1, TMP0, 0 - | stw BASE, L->base // Add frame since C call can throw. - | crand 4*cr0+eq, 4*cr0+lt, 4*cr1+eq - | stw PC, SAVE_PC // Redundant (but a defined value). - | bne ->fff_fallback - | ffgccheck - | mr CARG1, L - | mr CARG2, BASE - | bl extern lj_str_fromnum // (lua_State *L, lua_Number *np) - | // Returns GCstr *. - | evmergelo STR:CRET1, TISSTR, STR:CRET1 - | b ->fff_restv - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc next - | cmplwi NARGS8:RC, 8 - | evldd CARG2, 0(BASE) - | blt ->fff_fallback - | evstddx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil. - | checktab TAB:CARG2 - | lwz PC, FRAME_PC(BASE) - | checkfail ->fff_fallback - | stw BASE, L->base // Add frame since C call can throw. - | mr CARG1, L - | stw BASE, L->top // Dummy frame length is ok. - | la CARG3, 8(BASE) - | stw PC, SAVE_PC - | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key) - | // Returns 0 at end of traversal. - | cmplwi CRET1, 0 - | evmr CRET1, TISNIL - | beq ->fff_restv // End of traversal: return nil. - | evldd TMP0, 8(BASE) // Copy key and value to results. - | la RA, -8(BASE) - | evldd TMP1, 16(BASE) - | evstdd TMP0, 0(RA) - | li RD, (2+1)*8 - | evstdd TMP1, 8(RA) - | b ->fff_res - | - |.ffunc_1 pairs - | checktab TAB:CARG1 - | lwz PC, FRAME_PC(BASE) - | checkfail ->fff_fallback -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0] - | cmplwi TAB:TMP2, 0 - | la RA, -8(BASE) - | bne ->fff_fallback -#else - | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0] - | la RA, -8(BASE) -#endif - | evstdd TISNIL, 8(BASE) - | li RD, (3+1)*8 - | evstdd CFUNC:TMP0, 0(RA) - | b ->fff_res - | - |.ffunc_2 ipairs_aux - | checktab TAB:CARG1 - | lwz PC, FRAME_PC(BASE) - | checkfail ->fff_fallback - | checknum CARG2 - | lus TMP3, 0x3ff0 - | checkfail ->fff_fallback - | efdctsi TMP2, CARG2 - | lwz TMP0, TAB:CARG1->asize - | evmergelo TMP3, TMP3, ZERO - | lwz TMP1, TAB:CARG1->array - | efdadd CARG2, CARG2, TMP3 - | addi TMP2, TMP2, 1 - | la RA, -8(BASE) - | cmplw TMP0, TMP2 - | slwi TMP3, TMP2, 3 - | evstdd CARG2, 0(RA) - | ble >2 // Not in array part? - | evlddx TMP1, TMP1, TMP3 - |1: - | checknil TMP1 - | li RD, (0+1)*8 - | checkok ->fff_res // End of iteration, return 0 results. - | li RD, (2+1)*8 - | evstdd TMP1, 8(RA) - | b ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | lwz TMP0, TAB:CARG1->hmask - | cmplwi TMP0, 0 - | li RD, (0+1)*8 - | beq ->fff_res - | mr CARG2, TMP2 - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | cmplwi CRET1, 0 - | li RD, (0+1)*8 - | beq ->fff_res - | evldd TMP1, 0(CRET1) - | b <1 - | - |.ffunc_1 ipairs - | checktab TAB:CARG1 - | lwz PC, FRAME_PC(BASE) - | checkfail ->fff_fallback -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0] - | cmplwi TAB:TMP2, 0 - | la RA, -8(BASE) - | bne ->fff_fallback -#else - | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0] - | la RA, -8(BASE) -#endif - | evsplati TMP1, 0 - | li RD, (3+1)*8 - | evstdd TMP1, 8(BASE) - | evstdd CFUNC:TMP0, 0(RA) - | b ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | cmplwi NARGS8:RC, 8 - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | blt ->fff_fallback - | mr TMP2, BASE - | la BASE, 8(BASE) - | // Remember active hook before pcall. - | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31 - | subi NARGS8:RC, NARGS8:RC, 8 - | addi PC, TMP3, 8+FRAME_PCALL - | b ->vm_call_dispatch - | - |.ffunc_2 xpcall - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | mr TMP2, BASE - | checkfunc CARG2 // Traceback must be a function. - | checkfail ->fff_fallback - | la BASE, 16(BASE) - | // Remember active hook before pcall. - | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31 - | evstdd CARG2, 0(TMP2) // Swap function and traceback. - | subi NARGS8:RC, NARGS8:RC, 16 - | evstdd CARG1, 8(TMP2) - | addi PC, TMP3, 16+FRAME_PCALL - | b ->vm_call_dispatch - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | evmergehi TMP0, L:CARG1, L:CARG1 - |.else - |.ffunc coroutine_wrap_aux - | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr - |.endif - |.if resume - | cmpwi TMP0, LJ_TTHREAD - | bne ->fff_fallback - |.endif - | lbz TMP0, L:CARG1->status - | lwz TMP1, L:CARG1->cframe - | lwz CARG2, L:CARG1->top - | cmplwi cr0, TMP0, LUA_YIELD - | lwz TMP2, L:CARG1->base - | cmplwi cr1, TMP1, 0 - | lwz TMP0, L:CARG1->maxstack - | cmplw cr7, CARG2, TMP2 - | lwz PC, FRAME_PC(BASE) - | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0 - | add TMP2, CARG2, NARGS8:RC - | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD - | cmplw cr1, TMP2, TMP0 - | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt - | stw PC, SAVE_PC - | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov - | stw BASE, L->base - | blt cr6, ->fff_fallback - |1: - |.if resume - | addi BASE, BASE, 8 // Keep resumed thread in stack for GC. - | subi NARGS8:RC, NARGS8:RC, 8 - | subi TMP2, TMP2, 8 - |.endif - | stw TMP2, L:CARG1->top - | li TMP1, 0 - | stw BASE, L->top - |2: // Move args to coroutine. - | cmpw TMP1, NARGS8:RC - | evlddx TMP0, BASE, TMP1 - | beq >3 - | evstddx TMP0, CARG2, TMP1 - | addi TMP1, TMP1, 8 - | b <2 - |3: - | li CARG3, 0 - | mr L:SAVE0, L:CARG1 - | li CARG4, 0 - | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | // Returns thread status. - |4: - | lwz TMP2, L:SAVE0->base - | cmplwi CRET1, LUA_YIELD - | lwz TMP3, L:SAVE0->top - | li_vmstate INTERP - | lwz BASE, L->base - | st_vmstate - | bgt >8 - | sub RD, TMP3, TMP2 - | lwz TMP0, L->maxstack - | cmplwi RD, 0 - | add TMP1, BASE, RD - | beq >6 // No results? - | cmplw TMP1, TMP0 - | li TMP1, 0 - | bgt >9 // Need to grow stack? - | - | subi TMP3, RD, 8 - | stw TMP2, L:SAVE0->top // Clear coroutine stack. - |5: // Move results from coroutine. - | cmplw TMP1, TMP3 - | evlddx TMP0, TMP2, TMP1 - | evstddx TMP0, BASE, TMP1 - | addi TMP1, TMP1, 8 - | bne <5 - |6: - | andi. TMP0, PC, FRAME_TYPE - |.if resume - | li TMP1, LJ_TTRUE - | la RA, -8(BASE) - | stw TMP1, -8(BASE) // Prepend true to results. - | addi RD, RD, 16 - |.else - | mr RA, BASE - | addi RD, RD, 8 - |.endif - |7: - | stw PC, SAVE_PC - | mr MULTRES, RD - | beq ->BC_RET_Z - | b ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | andi. TMP0, PC, FRAME_TYPE - | la TMP3, -8(TMP3) - | li TMP1, LJ_TFALSE - | evldd TMP0, 0(TMP3) - | stw TMP3, L:SAVE0->top // Remove error from coroutine stack. - | li RD, (2+1)*8 - | stw TMP1, -8(BASE) // Prepend false to results. - | la RA, -8(BASE) - | evstdd TMP0, 0(BASE) // Copy error message. - | b <7 - |.else - | mr CARG1, L - | mr CARG2, L:SAVE0 - | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - |.endif - | - |9: // Handle stack expansion on return from yield. - | mr CARG1, L - | srwi CARG2, RD, 3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | li CRET1, 0 - | b <4 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | lwz TMP0, L->cframe - | add TMP1, BASE, NARGS8:RC - | stw BASE, L->base - | andi. TMP0, TMP0, CFRAME_RESUME - | stw TMP1, L->top - | li CRET1, LUA_YIELD - | beq ->fff_fallback - | stw ZERO, L->cframe - | stb CRET1, L->status - | b ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.ffunc_n math_abs - | efdabs CRET1, CARG1 - | // Fallthrough. - | - |->fff_restv: - | // CRET1 = TValue result. - | lwz PC, FRAME_PC(BASE) - | la RA, -8(BASE) - | evstdd CRET1, 0(RA) - |->fff_res1: - | // RA = results, PC = return. - | li RD, (1+1)*8 - |->fff_res: - | // RA = results, RD = (nresults+1)*8, PC = return. - | andi. TMP0, PC, FRAME_TYPE - | mr MULTRES, RD - | bne ->vm_return - | lwz INS, -4(PC) - | decode_RB8 RB, INS - |5: - | cmplw RB, RD // More results expected? - | decode_RA8 TMP0, INS - | bgt >6 - | ins_next1 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | sub BASE, RA, TMP0 - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | evstddx TISNIL, RA, TMP1 - | b <5 - | - |.macro math_extern, func - | .ffunc math_ .. func - | cmplwi NARGS8:RC, 8 - | evldd CARG2, 0(BASE) - | blt ->fff_fallback - | checknum CARG2 - | evmergehi CARG1, CARG2, CARG2 - | checkfail ->fff_fallback - | bl extern func@plt - | evmergelo CRET1, CRET1, CRET2 - | b ->fff_restv - |.endmacro - | - |.macro math_extern2, func - | .ffunc math_ .. func - | cmplwi NARGS8:RC, 16 - | evldd CARG2, 0(BASE) - | evldd CARG4, 8(BASE) - | blt ->fff_fallback - | evmergehi CARG1, CARG4, CARG2 - | checknum CARG1 - | evmergehi CARG3, CARG4, CARG4 - | checkanyfail ->fff_fallback - | bl extern func@plt - | evmergelo CRET1, CRET1, CRET2 - | b ->fff_restv - |.endmacro - | - |.macro math_round, func - | .ffunc math_ .. func - | cmplwi NARGS8:RC, 8 - | evldd CARG2, 0(BASE) - | blt ->fff_fallback - | checknum CARG2 - | evmergehi CARG1, CARG2, CARG2 - | checkfail ->fff_fallback - | lwz PC, FRAME_PC(BASE) - | bl ->vm_..func.._hilo; - | la RA, -8(BASE) - | evstdd CRET2, 0(RA) - | b ->fff_res1 - |.endmacro - | - | math_round floor - | math_round ceil - | - | math_extern sqrt - | - |.ffunc math_log - | cmplwi NARGS8:RC, 8 - | evldd CARG2, 0(BASE) - | bne ->fff_fallback // Need exactly 1 argument. - | checknum CARG2 - | evmergehi CARG1, CARG2, CARG2 - | checkfail ->fff_fallback - | bl extern log@plt - | evmergelo CRET1, CRET1, CRET2 - | b ->fff_restv - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |->ff_math_deg: - |.ffunc_n math_rad - | evldd CARG2, CFUNC:RB->upvalue[0] - | efdmul CRET1, CARG1, CARG2 - | b ->fff_restv - | - |.ffunc math_ldexp - | cmplwi NARGS8:RC, 16 - | evldd CARG2, 0(BASE) - | evldd CARG4, 8(BASE) - | blt ->fff_fallback - | evmergehi CARG1, CARG4, CARG2 - | checknum CARG1 - | checkanyfail ->fff_fallback - | efdctsi CARG3, CARG4 - | bl extern ldexp@plt - | evmergelo CRET1, CRET1, CRET2 - | b ->fff_restv - | - |.ffunc math_frexp - | cmplwi NARGS8:RC, 8 - | evldd CARG2, 0(BASE) - | blt ->fff_fallback - | checknum CARG2 - | evmergehi CARG1, CARG2, CARG2 - | checkfail ->fff_fallback - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | lwz PC, FRAME_PC(BASE) - | bl extern frexp@plt - | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH) - | evmergelo CRET1, CRET1, CRET2 - | efdcfsi CRET2, TMP1 - | la RA, -8(BASE) - | evstdd CRET1, 0(RA) - | li RD, (2+1)*8 - | evstdd CRET2, 8(RA) - | b ->fff_res - | - |.ffunc math_modf - | cmplwi NARGS8:RC, 8 - | evldd CARG2, 0(BASE) - | blt ->fff_fallback - | checknum CARG2 - | evmergehi CARG1, CARG2, CARG2 - | checkfail ->fff_fallback - | la CARG3, -8(BASE) - | lwz PC, FRAME_PC(BASE) - | bl extern modf@plt - | evmergelo CRET1, CRET1, CRET2 - | la RA, -8(BASE) - | evstdd CRET1, 0(BASE) - | li RD, (2+1)*8 - | b ->fff_res - | - |.macro math_minmax, name, cmpop - | .ffunc_1 name - | checknum CARG1 - | li TMP1, 8 - | checkfail ->fff_fallback - |1: - | evlddx CARG2, BASE, TMP1 - | cmplw cr1, TMP1, NARGS8:RC - | checknum CARG2 - | bge cr1, ->fff_restv // Ok, since CRET1 = CARG1. - | checkfail ->fff_fallback - | cmpop CARG2, CARG1 - | addi TMP1, TMP1, 8 - | crmove 4*cr0+lt, 4*cr0+gt - | evsel CARG1, CARG2, CARG1 - | b <1 - |.endmacro - | - | math_minmax math_min, efdtstlt - | math_minmax math_max, efdtstgt - | - |//-- String library ----------------------------------------------------- - | - |.ffunc_1 string_len - | checkstr STR:CARG1 - | checkfail ->fff_fallback - | lwz TMP0, STR:CARG1->len - | efdcfsi CRET1, TMP0 - | b ->fff_restv - | - |.ffunc string_byte // Only handle the 1-arg case here. - | cmplwi NARGS8:RC, 8 - | evldd STR:CARG1, 0(BASE) - | bne ->fff_fallback // Need exactly 1 argument. - | checkstr STR:CARG1 - | la RA, -8(BASE) - | checkfail ->fff_fallback - | lwz TMP0, STR:CARG1->len - | li RD, (0+1)*8 - | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end). - | li TMP2, (1+1)*8 - | cmplwi TMP0, 0 - | lwz PC, FRAME_PC(BASE) - | efdcfsi CRET1, TMP1 - | iseleq RD, RD, TMP2 - | evstdd CRET1, 0(RA) - | b ->fff_res - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | cmplwi NARGS8:RC, 8 - | evldd CARG1, 0(BASE) - | bne ->fff_fallback // Exactly 1 argument. - | checknum CARG1 - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - | checkfail ->fff_fallback - | efdctsiz TMP0, CARG1 - | li CARG3, 1 - | cmplwi TMP0, 255 - | stb TMP0, 0(CARG2) - | bgt ->fff_fallback - |->fff_newstr: - | mr CARG1, L - | stw BASE, L->base - | stw PC, SAVE_PC - | bl extern lj_str_new // (lua_State *L, char *str, size_t l) - | // Returns GCstr *. - | lwz BASE, L->base - | evmergelo STR:CRET1, TISSTR, STR:CRET1 - | b ->fff_restv - | - |.ffunc string_sub - | ffgccheck - | cmplwi NARGS8:RC, 16 - | evldd CARG3, 16(BASE) - | evldd STR:CARG1, 0(BASE) - | blt ->fff_fallback - | evldd CARG2, 8(BASE) - | li TMP2, -1 - | beq >1 - | checknum CARG3 - | checkfail ->fff_fallback - | efdctsiz TMP2, CARG3 - |1: - | checknum CARG2 - | checkfail ->fff_fallback - | checkstr STR:CARG1 - | efdctsiz TMP1, CARG2 - | checkfail ->fff_fallback - | lwz TMP0, STR:CARG1->len - | cmplw TMP0, TMP2 // len < end? (unsigned compare) - | add TMP3, TMP2, TMP0 - | blt >5 - |2: - | cmpwi TMP1, 0 // start <= 0? - | add TMP3, TMP1, TMP0 - | ble >7 - |3: - | sub. CARG3, TMP2, TMP1 - | addi CARG2, STR:CARG1, #STR-1 - | addi CARG3, CARG3, 1 - | add CARG2, CARG2, TMP1 - | isellt CARG3, r0, CARG3 - | b ->fff_newstr - | - |5: // Negative end or overflow. - | cmpw TMP0, TMP2 - | addi TMP3, TMP3, 1 - | iselgt TMP2, TMP3, TMP0 // end = end > len ? len : end+len+1 - | b <2 - | - |7: // Negative start or underflow. - | cmpwi cr1, TMP3, 0 - | iseleq TMP1, r0, TMP3 - | isel TMP1, r0, TMP1, 4*cr1+lt - | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0) - | b <3 - | - |.ffunc string_rep // Only handle the 1-char case inline. - | ffgccheck - | cmplwi NARGS8:RC, 16 - | evldd CARG1, 0(BASE) - | evldd CARG2, 8(BASE) - | bne ->fff_fallback // Exactly 2 arguments. - | checknum CARG2 - | checkfail ->fff_fallback - | checkstr STR:CARG1 - | efdctsiz CARG3, CARG2 - | checkfail ->fff_fallback - | lwz TMP0, STR:CARG1->len - | cmpwi CARG3, 0 - | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | ble >2 // Count <= 0? (or non-int) - | cmplwi TMP0, 1 - | subi TMP2, CARG3, 1 - | blt >2 // Zero length string? - | cmplw cr1, TMP1, CARG3 - | bne ->fff_fallback // Fallback for > 1-char strings. - | lbz TMP0, STR:CARG1[1] - | lwz CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | blt cr1, ->fff_fallback - |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?). - | cmplwi TMP2, 0 - | stbx TMP0, CARG2, TMP2 - | subi TMP2, TMP2, 1 - | bne <1 - | b ->fff_newstr - |2: // Return empty string. - | la STR:CRET1, DISPATCH_GL(strempty)(DISPATCH) - | evmergelo CRET1, TISSTR, STR:CRET1 - | b ->fff_restv - | - |.ffunc string_reverse - | ffgccheck - | cmplwi NARGS8:RC, 8 - | evldd CARG1, 0(BASE) - | blt ->fff_fallback - | checkstr STR:CARG1 - | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | checkfail ->fff_fallback - | lwz CARG3, STR:CARG1->len - | la CARG1, #STR(STR:CARG1) - | lwz CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | li TMP2, 0 - | cmplw TMP1, CARG3 - | subi TMP3, CARG3, 1 - | blt ->fff_fallback - |1: // Reverse string copy. - | cmpwi TMP3, 0 - | lbzx TMP1, CARG1, TMP2 - | blt ->fff_newstr - | stbx TMP1, CARG2, TMP3 - | subi TMP3, TMP3, 1 - | addi TMP2, TMP2, 1 - | b <1 - | - |.macro ffstring_case, name, lo - | .ffunc name - | ffgccheck - | cmplwi NARGS8:RC, 8 - | evldd CARG1, 0(BASE) - | blt ->fff_fallback - | checkstr STR:CARG1 - | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH) - | checkfail ->fff_fallback - | lwz CARG3, STR:CARG1->len - | la CARG1, #STR(STR:CARG1) - | lwz CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH) - | cmplw TMP1, CARG3 - | li TMP2, 0 - | blt ->fff_fallback - |1: // ASCII case conversion. - | cmplw TMP2, CARG3 - | lbzx TMP1, CARG1, TMP2 - | bge ->fff_newstr - | subi TMP0, TMP1, lo - | xori TMP3, TMP1, 0x20 - | cmplwi TMP0, 26 - | isellt TMP1, TMP3, TMP1 - | stbx TMP1, CARG2, TMP2 - | addi TMP2, TMP2, 1 - | b <1 - |.endmacro - | - |ffstring_case string_lower, 65 - |ffstring_case string_upper, 97 - | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | checktab CARG1 - | checkfail ->fff_fallback - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | efdcfsi CRET1, CRET1 - | b ->fff_restv - | - |//-- Bit library -------------------------------------------------------- - | - |.macro .ffunc_bit, name - | .ffunc_n bit_..name - | efdadd CARG1, CARG1, TOBIT - |.endmacro - | - |.ffunc_bit tobit - |->fff_resbit: - | efdcfsi CRET1, CARG1 - | b ->fff_restv - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | li TMP1, 8 - |1: - | evlddx CARG2, BASE, TMP1 - | cmplw cr1, TMP1, NARGS8:RC - | checknum CARG2 - | bge cr1, ->fff_resbit - | checkfail ->fff_fallback - | efdadd CARG2, CARG2, TOBIT - | ins CARG1, CARG1, CARG2 - | addi TMP1, TMP1, 8 - | b <1 - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, or - |.ffunc_bit_op bxor, xor - | - |.ffunc_bit bswap - | rotlwi TMP0, CARG1, 8 - | rlwimi TMP0, CARG1, 24, 0, 7 - | rlwimi TMP0, CARG1, 24, 16, 23 - | efdcfsi CRET1, TMP0 - | b ->fff_restv - | - |.ffunc_bit bnot - | not TMP0, CARG1 - | efdcfsi CRET1, TMP0 - | b ->fff_restv - | - |.macro .ffunc_bit_sh, name, ins, shmod - | .ffunc_nn bit_..name - | efdadd CARG2, CARG2, TOBIT - | efdadd CARG1, CARG1, TOBIT - |.if shmod == 1 - | rlwinm CARG2, CARG2, 0, 27, 31 - |.elif shmod == 2 - | neg CARG2, CARG2 - |.endif - | ins TMP0, CARG1, CARG2 - | efdcfsi CRET1, TMP0 - | b ->fff_restv - |.endmacro - | - |.ffunc_bit_sh lshift, slw, 1 - |.ffunc_bit_sh rshift, srw, 1 - |.ffunc_bit_sh arshift, sraw, 1 - |.ffunc_bit_sh rol, rotlw, 0 - |.ffunc_bit_sh ror, rotlw, 2 - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RB = CFUNC, RC = nargs*8 - | lwz TMP3, CFUNC:RB->f - | add TMP1, BASE, NARGS8:RC - | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC. - | addi TMP0, TMP1, 8*LUA_MINSTACK - | lwz TMP2, L->maxstack - | stw PC, SAVE_PC // Redundant (but a defined value). - | cmplw TMP0, TMP2 - | stw BASE, L->base - | stw TMP1, L->top - | mr CARG1, L - | bgt >5 // Need to grow stack. - | mtctr TMP3 - | bctrl // (lua_State *L) - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | lwz BASE, L->base - | cmpwi CRET1, 0 - | slwi RD, CRET1, 3 - | la RA, -8(BASE) - | bgt ->fff_res // Returned nresults+1? - |1: // Returned 0 or -1: retry fast path. - | lwz TMP0, L->top - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | sub NARGS8:RC, TMP0, BASE - | bne ->vm_call_tail // Returned -1? - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | andi. TMP0, PC, FRAME_TYPE - | rlwinm TMP1, PC, 0, 0, 28 - | bne >3 - | lwz INS, -4(PC) - | decode_RA8 TMP1, INS - | addi TMP1, TMP1, 8 - |3: - | sub TMP2, BASE, TMP1 - | b ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | li CARG2, LUA_MINSTACK - | bl extern lj_state_growstack // (lua_State *L, int n) - | lwz BASE, L->base - | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry. - | b <1 - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | mflr SAVE0 - | stw BASE, L->base - | add TMP0, BASE, NARGS8:RC - | stw PC, SAVE_PC // Redundant (but a defined value). - | stw TMP0, L->top - | mr CARG1, L - | bl extern lj_gc_step // (lua_State *L) - | lwz BASE, L->base - | mtlr SAVE0 - | lwz TMP0, L->top - | sub NARGS8:RC, TMP0, BASE - | lwz CFUNC:RB, FRAME_FUNC(BASE) - | blr - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | NYI - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andi. TMP0, TMP3, HOOK_ACTIVE // Hook already active? - | beq >1 - |5: // Re-dispatch to static ins. - | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OP4 TMP1, INS. - | lwzx TMP0, DISPATCH, TMP1 - | mtctr TMP0 - | bctr - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi. TMP0, TMP3, HOOK_ACTIVE // Hook already active? - | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0 - | bne <5 - | - | cmpwi cr1, TMP0, 0 - | addic. TMP2, TMP2, -1 - | beq cr1, <5 - | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | beq >1 - | bge cr1, <5 - |1: - | mr CARG1, L - | stw MULTRES, SAVE_MULTRES - | mr CARG2, PC - | stw BASE, L->base - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |3: - | lwz BASE, L->base - |4: // Re-dispatch to static ins. - | lwz INS, -4(PC) - | decode_OP4 TMP1, INS - | decode_RB8 RB, INS - | addi TMP1, TMP1, GG_DISP2STATIC - | decode_RD8 RD, INS - | lwzx TMP0, DISPATCH, TMP1 - | decode_RA8 RA, INS - | decode_RC8 RC, INS - | mtctr TMP0 - | bctr - | - |->cont_hook: // Continue from hook yield. - | addi PC, PC, 4 - | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins. - | b <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | NYI - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mr CARG2, PC - |.if JIT - | b >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | ori CARG2, PC, 1 - |1: - |.endif - | add TMP0, BASE, RC - | stw PC, SAVE_PC - | mr CARG1, L - | stw BASE, L->base - | sub RA, RA, BASE - | stw TMP0, L->top - | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc) - | // Returns ASMFunction. - | lwz BASE, L->base - | lwz TMP0, L->top - | stw ZERO, SAVE_PC // Invalidate for subsequent line hook. - | sub NARGS8:RC, TMP0, BASE - | add RA, BASE, RA - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | mtctr CRET1 - | bctr - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_exit_handler: - |.if JIT - | NYI - |.endif - |->vm_exit_interp: - |.if JIT - | NYI - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// FP value rounding. Called by math.floor/math.ceil fast functions - |// and from JIT code. - |// - |// This can be inlined if the CPU has the frin/friz/frip/frim instructions. - |// The alternative hard-float approaches have a deep dependency chain. - |// The resulting latency is at least 3x-7x the double-precision FP latency - |// (e500v2: 6cy, e600: 5cy, Cell: 10cy) or around 20-70 cycles. - |// - |// The soft-float approach is tedious, but much faster (e500v2: ~11cy/~6cy). - |// However it relies on a fast way to transfer the FP value to GPRs - |// (e500v2: 0cy for lo-word, 1cy for hi-word). - |// - |.macro vm_round, name, mode - | // Used temporaries: TMP0, TMP1, TMP2, TMP3. - |->name.._efd: // Input: CARG2, output: CRET2 - | evmergehi CARG1, CARG2, CARG2 - |->name.._hilo: - | // Input: CARG1 (hi), CARG2 (hi, lo), output: CRET2 - | rlwinm TMP2, CARG1, 12, 21, 31 - | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023 - | li TMP1, -1 - | cmplwi cr1, TMP2, 51 // 0 <= exp <= 51? - | subfic TMP0, TMP2, 52 - | bgt cr1, >1 - | lus TMP3, 0xfff0 - | slw TMP0, TMP1, TMP0 // lomask = -1 << (52-exp) - | sraw TMP1, TMP3, TMP2 // himask = (int32_t)0xfff00000 >> exp - |.if mode == 2 // trunc(x): - | evmergelo TMP0, TMP1, TMP0 - | evand CRET2, CARG2, TMP0 // hi &= himask, lo &= lomask - |.else - | andc TMP2, CARG2, TMP0 - | andc TMP3, CARG1, TMP1 - | or TMP2, TMP2, TMP3 // ztest = (hi&~himask) | (lo&~lomask) - | srawi TMP3, CARG1, 31 // signmask = (int32_t)hi >> 31 - |.if mode == 0 // floor(x): - | and. TMP2, TMP2, TMP3 // iszero = ((ztest & signmask) == 0) - |.else // ceil(x): - | andc. TMP2, TMP2, TMP3 // iszero = ((ztest & ~signmask) == 0) - |.endif - | and CARG2, CARG2, TMP0 // lo &= lomask - | and CARG1, CARG1, TMP1 // hi &= himask - | subc TMP0, CARG2, TMP0 - | iseleq TMP0, CARG2, TMP0 // lo = iszero ? lo : lo-lomask - | sube TMP1, CARG1, TMP1 - | iseleq TMP1, CARG1, TMP1 // hi = iszero ? hi : hi-himask+carry - | evmergelo CRET2, TMP1, TMP0 - |.endif - | blr - |1: - | bgtlr // Already done if >=2^52, +-inf or nan. - |.if mode == 2 // trunc(x): - | rlwinm TMP1, CARG1, 0, 0, 0 // hi = sign(x) - | li TMP0, 0 - | evmergelo CRET2, TMP1, TMP0 - |.else - | rlwinm TMP2, CARG1, 0, 1, 31 - | srawi TMP0, CARG1, 31 // signmask = (int32_t)hi >> 31 - | or TMP2, TMP2, CARG2 // ztest = abs(hi) | lo - | lus TMP1, 0x3ff0 - |.if mode == 0 // floor(x): - | and. TMP2, TMP2, TMP0 // iszero = ((ztest & signmask) == 0) - |.else // ceil(x): - | andc. TMP2, TMP2, TMP0 // iszero = ((ztest & ~signmask) == 0) - |.endif - | li TMP0, 0 - | iseleq TMP1, r0, TMP1 - | rlwimi CARG1, TMP1, 0, 1, 31 // hi = sign(x) | (iszero ? 0.0 : 1.0) - | evmergelo CRET2, CARG1, TMP0 - |.endif - | blr - |.endmacro - | - |->vm_floor: - | mflr CARG3 - | evmergelo CARG2, CARG1, CARG2 - | bl ->vm_floor_hilo - | mtlr CARG3 - | evmergehi CRET1, CRET2, CRET2 - | blr - | - | vm_round vm_floor, 0 - | vm_round vm_ceil, 1 - |.if JIT - | vm_round vm_trunc, 2 - |.else - |->vm_trunc_efd: - |->vm_trunc_hilo: - |.endif - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_ffi_call: - |.if FFI - | NYI - |.endif - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RD = src2*8, JMP with RD = target - | evlddx TMP0, BASE, RA - | addi PC, PC, 4 - | evlddx TMP1, BASE, RD - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | lwz TMP2, -4(PC) - | evmergehi RB, TMP0, TMP1 - | decode_RD4 TMP2, TMP2 - | checknum RB - | add TMP2, TMP2, TMP3 - | checkanyfail ->vmeta_comp - | efdcmplt TMP0, TMP1 - if (op == BC_ISLE || op == BC_ISGT) { - | efdcmpeq cr1, TMP0, TMP1 - | cror 4*cr0+gt, 4*cr0+gt, 4*cr1+gt - } - if (op == BC_ISLT || op == BC_ISLE) { - | iselgt PC, TMP2, PC - } else { - | iselgt PC, PC, TMP2 - } - | ins_next - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RD = src2*8, JMP with RD = target - | evlddx CARG2, BASE, RA - | addi PC, PC, 4 - | evlddx CARG3, BASE, RD - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | lwz TMP2, -4(PC) - | evmergehi RB, CARG2, CARG3 - | decode_RD4 TMP2, TMP2 - | checknum RB - | add TMP2, TMP2, TMP3 - | checkanyfail >5 - | efdcmpeq CARG2, CARG3 - if (vk) { - | iselgt PC, TMP2, PC - } else { - | iselgt PC, PC, TMP2 - } - |1: - | ins_next - | - |5: // Either or both types are not numbers. - | evcmpeq CARG2, CARG3 - | not TMP3, RB - | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive? - | crorc 4*cr7+lt, 4*cr0+so, 4*cr0+lt // 1: Same tv or different type. - | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata? - | crandc 4*cr7+gt, 4*cr0+lt, 4*cr1+gt // 2: Same type and primitive. - | mr SAVE0, PC - if (vk) { - | isel PC, TMP2, PC, 4*cr7+gt - } else { - | isel TMP2, PC, TMP2, 4*cr7+gt - } - | cror 4*cr7+lt, 4*cr7+lt, 4*cr7+gt // 1 or 2. - if (vk) { - | isel PC, TMP2, PC, 4*cr0+so - } else { - | isel PC, PC, TMP2, 4*cr0+so - } - | blt cr7, <1 // Done if 1 or 2. - | blt cr6, <1 // Done if not tab/ud. - | - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | lwz TAB:TMP2, TAB:CARG2->metatable - | li CARG4, 1-vk // ne = 0 or 1. - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable? - | lbz TMP2, TAB:TMP2->nomm - | andi. TMP2, TMP2, 1<vmeta_equal // Handle __eq metamethod. - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RD = str_const*8 (~), JMP with RD = target - | evlddx TMP0, BASE, RA - | srwi RD, RD, 1 - | lwz INS, 0(PC) - | subfic RD, RD, -4 - | addi PC, PC, 4 - | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4 - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | decode_RD4 TMP2, INS - | evmergelo STR:TMP1, TISSTR, STR:TMP1 - | add TMP2, TMP2, TMP3 - | evcmpeq TMP0, STR:TMP1 - if (vk) { - | isel PC, TMP2, PC, 4*cr0+so - } else { - | isel PC, PC, TMP2, 4*cr0+so - } - | ins_next - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RD = num_const*8, JMP with RD = target - | evlddx TMP0, BASE, RA - | addi PC, PC, 4 - | evlddx TMP1, KBASE, RD - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | lwz INS, -4(PC) - | checknum TMP0 - | checkfail >5 - | efdcmpeq TMP0, TMP1 - |1: - | decode_RD4 TMP2, INS - | add TMP2, TMP2, TMP3 - if (vk) { - | iselgt PC, TMP2, PC - |5: - } else { - | iselgt PC, PC, TMP2 - } - |3: - | ins_next - if (!vk) { - |5: - | decode_RD4 TMP2, INS - | add PC, TMP2, TMP3 - | b <3 - } - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target - | lwzx TMP0, BASE, RA - | srwi TMP1, RD, 3 - | lwz INS, 0(PC) - | addi PC, PC, 4 - | not TMP1, TMP1 - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | cmplw TMP0, TMP1 - | decode_RD4 TMP2, INS - | add TMP2, TMP2, TMP3 - if (vk) { - | iseleq PC, TMP2, PC - } else { - | iseleq PC, PC, TMP2 - } - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RD = src*8, JMP with RD = target - | evlddx TMP0, BASE, RD - | evaddw TMP1, TISNIL, TISNIL // Synthesize LJ_TFALSE. - | lwz INS, 0(PC) - | evcmpltu TMP0, TMP1 - | addi PC, PC, 4 - if (op == BC_IST || op == BC_ISF) { - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | decode_RD4 TMP2, INS - | add TMP2, TMP2, TMP3 - if (op == BC_IST) { - | isellt PC, TMP2, PC - } else { - | isellt PC, PC, TMP2 - } - } else { - if (op == BC_ISTC) { - | checkfail >1 - } else { - | checkok >1 - } - | addis PC, PC, -(BCBIAS_J*4 >> 16) - | decode_RD4 TMP2, INS - | evstddx TMP0, BASE, RA - | add PC, PC, TMP2 - |1: - } - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RD = src*8 - | ins_next1 - | evlddx TMP0, BASE, RD - | evstddx TMP0, BASE, RA - | ins_next2 - break; - case BC_NOT: - | // RA = dst*8, RD = src*8 - | ins_next1 - | lwzx TMP0, BASE, RD - | subfic TMP1, TMP0, LJ_TTRUE - | adde TMP0, TMP0, TMP1 - | stwx TMP0, BASE, RA - | ins_next2 - break; - case BC_UNM: - | // RA = dst*8, RD = src*8 - | evlddx TMP0, BASE, RD - | checknum TMP0 - | checkfail ->vmeta_unm - | efdneg TMP0, TMP0 - | ins_next1 - | evstddx TMP0, BASE, RA - | ins_next2 - break; - case BC_LEN: - | // RA = dst*8, RD = src*8 - | evlddx CARG1, BASE, RD - | checkstr CARG1 - | checkfail >2 - | lwz CRET1, STR:CARG1->len - |1: - | ins_next1 - | efdcfsi TMP0, CRET1 - | evstddx TMP0, BASE, RA - | ins_next2 - |2: - | checktab CARG1 - | checkfail ->vmeta_len -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - | cmplwi TAB:TMP2, 0 - | bne >9 - |3: -#endif - |->BC_LEN_Z: - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | b <1 -#if LJ_52 - |9: - | lbz TMP0, TAB:TMP2->nomm - | andi. TMP0, TMP0, 1<vmeta_len -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithpre, t0, t1 - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | evlddx t0, BASE, RB - | checknum t0 - | evlddx t1, KBASE, RC - | checkfail ->vmeta_arith_vn - || break; - ||case 1: - | evlddx t1, BASE, RB - | checknum t1 - | evlddx t0, KBASE, RC - | checkfail ->vmeta_arith_nv - || break; - ||default: - | evlddx t0, BASE, RB - | evlddx t1, BASE, RC - | evmergehi TMP2, t0, t1 - | checknum TMP2 - | checkanyfail ->vmeta_arith_vv - || break; - ||} - |.endmacro - | - |.macro ins_arith, ins - | ins_arithpre TMP0, TMP1 - | ins_next1 - | ins TMP0, TMP0, TMP1 - | evstddx TMP0, BASE, RA - | ins_next2 - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arith efdadd - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arith efdsub - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith efdmul - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arith efddiv - break; - case BC_MODVN: - | ins_arithpre RD, SAVE0 - |->BC_MODVN_Z: - | efddiv CARG2, RD, SAVE0 - | bl ->vm_floor_efd // floor(b/c) - | efdmul TMP0, CRET2, SAVE0 - | ins_next1 - | efdsub TMP0, RD, TMP0 // b - floor(b/c)*c - | evstddx TMP0, BASE, RA - | ins_next2 - break; - case BC_MODNV: case BC_MODVV: - | ins_arithpre RD, SAVE0 - | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - break; - case BC_POW: - | evlddx CARG2, BASE, RB - | evlddx CARG4, BASE, RC - | evmergehi CARG1, CARG4, CARG2 - | checknum CARG1 - | evmergehi CARG3, CARG4, CARG4 - | checkanyfail ->vmeta_arith_vv - | bl extern pow@plt - | evmergelo CRET2, CRET1, CRET2 - | evstddx CRET2, BASE, RA - | ins_next - break; - - case BC_CAT: - | // RA = dst*8, RB = src_start*8, RC = src_end*8 - | sub CARG3, RC, RB - | stw BASE, L->base - | add CARG2, BASE, RC - | mr SAVE0, RB - |->BC_CAT_Z: - | stw PC, SAVE_PC - | mr CARG1, L - | srwi CARG3, CARG3, 3 - | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // Returns NULL (finished) or TValue * (metamethod). - | cmplwi CRET1, 0 - | lwz BASE, L->base - | bne ->vmeta_binop - | evlddx TMP0, BASE, SAVE0 // Copy result from RB to RA. - | evstddx TMP0, BASE, RA - | ins_next - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RD = str_const*8 (~) - | ins_next1 - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4 - | evmergelo TMP0, TISSTR, TMP0 - | evstddx TMP0, BASE, RA - | ins_next2 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RD = cdata_const*8 (~) - | ins_next1 - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4 - | li TMP2, LJ_TCDATA - | evmergelo TMP0, TMP2, TMP0 - | evstddx TMP0, BASE, RA - | ins_next2 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, RD = int16_literal*8 - | srwi TMP1, RD, 3 - | extsh TMP1, TMP1 - | ins_next1 - | efdcfsi TMP0, TMP1 - | evstddx TMP0, BASE, RA - | ins_next2 - break; - case BC_KNUM: - | // RA = dst*8, RD = num_const*8 - | evlddx TMP0, KBASE, RD - | ins_next1 - | evstddx TMP0, BASE, RA - | ins_next2 - break; - case BC_KPRI: - | // RA = dst*8, RD = primitive_type*8 (~) - | srwi TMP1, RD, 3 - | not TMP0, TMP1 - | ins_next1 - | stwx TMP0, BASE, RA - | ins_next2 - break; - case BC_KNIL: - | // RA = base*8, RD = end*8 - | evstddx TISNIL, BASE, RA - | addi RA, RA, 8 - |1: - | evstddx TISNIL, BASE, RA - | cmpw RA, RD - | addi RA, RA, 8 - | blt <1 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RD = uvnum*8 - | ins_next1 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RD, RD, 1 - | addi RD, RD, offsetof(GCfuncL, uvptr) - | lwzx UPVAL:RB, LFUNC:RB, RD - | lwz TMP1, UPVAL:RB->v - | evldd TMP0, 0(TMP1) - | evstddx TMP0, BASE, RA - | ins_next2 - break; - case BC_USETV: - | // RA = uvnum*8, RD = src*8 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | evlddx TMP1, BASE, RD - | lwzx UPVAL:RB, LFUNC:RB, RA - | lbz TMP3, UPVAL:RB->marked - | lwz CARG2, UPVAL:RB->v - | andi. TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbz TMP0, UPVAL:RB->closed - | evmergehi TMP2, TMP1, TMP1 - | evstdd TMP1, 0(CARG2) - | cmplwi cr1, TMP0, 0 - | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | subi TMP2, TMP2, (LJ_TISNUM+1) - | bne >2 // Upvalue is closed and black? - |1: - | ins_next - | - |2: // Check if new value is collectable. - | cmplwi TMP2, LJ_TISGCV - (LJ_TISNUM+1) - | bge <1 // tvisgcv(v) - | lbz TMP3, GCOBJ:TMP1->gch.marked - | andi. TMP3, TMP3, LJ_GC_WHITES // iswhite(v) - | la CARG1, GG_DISP2G(DISPATCH) - | // Crossed a write barrier. Move the barrier forward. - | beq <1 - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETS: - | // RA = uvnum*8, RD = str_const*8 (~) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi TMP1, RD, 1 - | srwi RA, RA, 1 - | subfic TMP1, TMP1, -4 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4 - | lwzx UPVAL:RB, LFUNC:RB, RA - | evmergelo STR:TMP1, TISSTR, STR:TMP1 - | lbz TMP3, UPVAL:RB->marked - | lwz CARG2, UPVAL:RB->v - | andi. TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbz TMP3, STR:TMP1->marked - | lbz TMP2, UPVAL:RB->closed - | evstdd STR:TMP1, 0(CARG2) - | bne >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | andi. TMP3, TMP3, LJ_GC_WHITES // iswhite(str) - | cmplwi cr1, TMP2, 0 - | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | la CARG1, GG_DISP2G(DISPATCH) - | // Crossed a write barrier. Move the barrier forward. - | beq <1 - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETN: - | // RA = uvnum*8, RD = num_const*8 - | ins_next1 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | evlddx TMP0, KBASE, RD - | lwzx UPVAL:RB, LFUNC:RB, RA - | lwz TMP1, UPVAL:RB->v - | evstdd TMP0, 0(TMP1) - | ins_next2 - break; - case BC_USETP: - | // RA = uvnum*8, RD = primitive_type*8 (~) - | ins_next1 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | srwi TMP0, RD, 3 - | lwzx UPVAL:RB, LFUNC:RB, RA - | not TMP0, TMP0 - | lwz TMP1, UPVAL:RB->v - | stw TMP0, 0(TMP1) - | ins_next2 - break; - - case BC_UCLO: - | // RA = level*8, RD = target - | lwz TMP1, L->openupval - | branch_RD // Do this first since RD is not saved. - | stw BASE, L->base - | cmplwi TMP1, 0 - | mr CARG1, L - | beq >1 - | add CARG2, BASE, RA - | bl extern lj_func_closeuv // (lua_State *L, TValue *level) - | lwz BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype) - | srwi TMP1, RD, 1 - | stw BASE, L->base - | subfic TMP1, TMP1, -4 - | stw PC, SAVE_PC - | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4 - | mr CARG1, L - | lwz CARG3, FRAME_FUNC(BASE) - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | bl extern lj_func_newL_gc - | // Returns GCfuncL *. - | lwz BASE, L->base - | evmergelo LFUNC:CRET1, TISFUNC, LFUNC:CRET1 - | evstddx LFUNC:CRET1, BASE, RA - | ins_next - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~) - | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | mr CARG1, L - | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | stw BASE, L->base - | cmplw TMP0, TMP1 - | stw PC, SAVE_PC - | bge >5 - |1: - if (op == BC_TNEW) { - | rlwinm CARG2, RD, 29, 21, 31 - | rlwinm CARG3, RD, 18, 27, 31 - | cmpwi CARG2, 0x7ff - | li TMP1, 0x801 - | iseleq CARG2, TMP1, CARG2 - | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Returns Table *. - } else { - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4 - | bl extern lj_tab_dup // (lua_State *L, Table *kt) - | // Returns Table *. - } - | lwz BASE, L->base - | evmergelo TAB:CRET1, TISTAB, TAB:CRET1 - | evstddx TAB:CRET1, BASE, RA - | ins_next - |5: - | mr SAVE0, RD - | bl extern lj_gc_step_fixtop // (lua_State *L) - | mr RD, SAVE0 - | mr CARG1, L - | b <1 - break; - - case BC_GGET: - | // RA = dst*8, RD = str_const*8 (~) - case BC_GSET: - | // RA = src*8, RD = str_const*8 (~) - | lwz LFUNC:TMP2, FRAME_FUNC(BASE) - | srwi TMP1, RD, 1 - | lwz TAB:RB, LFUNC:TMP2->env - | subfic TMP1, TMP1, -4 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - break; - - case BC_TGETV: - | // RA = dst*8, RB = table*8, RC = key*8 - | evlddx TAB:RB, BASE, RB - | evlddx RC, BASE, RC - | checktab TAB:RB - | checkfail ->vmeta_tgetv - | checknum RC - | checkfail >5 - | // Convert number key to integer - | efdctsi TMP2, RC - | lwz TMP0, TAB:RB->asize - | efdcfsi TMP1, TMP2 - | cmplw cr0, TMP0, TMP2 - | efdcmpeq cr1, RC, TMP1 - | lwz TMP1, TAB:RB->array - | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+gt - | slwi TMP2, TMP2, 3 - | ble ->vmeta_tgetv // Integer key and in array part? - | evlddx TMP1, TMP1, TMP2 - | checknil TMP1 - | checkok >2 - |1: - | evstddx TMP1, BASE, RA - | ins_next - | - |2: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP0, TAB:TMP2->nomm - | andi. TMP0, TMP0, 1<vmeta_tgetv - | - |5: - | checkstr STR:RC // String key? - | checkok ->BC_TGETS_Z - | b ->vmeta_tgetv - break; - case BC_TGETS: - | // RA = dst*8, RB = table*8, RC = str_const*8 (~) - | evlddx TAB:RB, BASE, RB - | srwi TMP1, RC, 1 - | checktab TAB:RB - | subfic TMP1, TMP1, -4 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - | checkfail ->vmeta_tgets1 - |->BC_TGETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8 - | lwz TMP0, TAB:RB->hmask - | lwz TMP1, STR:RC->hash - | lwz NODE:TMP2, TAB:RB->node - | evmergelo STR:RC, TISSTR, STR:RC - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | evldd TMP0, NODE:TMP2->key - | evldd TMP1, NODE:TMP2->val - | evcmpeq TMP0, STR:RC - | checkanyfail >4 - | checknil TMP1 - | checkok >5 // Key found, but nil value? - |3: - | evstddx TMP1, BASE, RA - | ins_next - | - |4: // Follow hash chain. - | lwz NODE:TMP2, NODE:TMP2->next - | cmplwi NODE:TMP2, 0 - | bne <1 - | // End of hash chain: key not found, nil result. - | evmr TMP1, TISNIL - | - |5: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <3 // No metatable: done. - | lbz TMP0, TAB:TMP2->nomm - | andi. TMP0, TMP0, 1<vmeta_tgets - break; - case BC_TGETB: - | // RA = dst*8, RB = table*8, RC = index*8 - | evlddx TAB:RB, BASE, RB - | srwi TMP0, RC, 3 - | checktab TAB:RB - | checkfail ->vmeta_tgetb - | lwz TMP1, TAB:RB->asize - | lwz TMP2, TAB:RB->array - | cmplw TMP0, TMP1 - | bge ->vmeta_tgetb - | evlddx TMP1, TMP2, RC - | checknil TMP1 - | checkok >5 - |1: - | ins_next1 - | evstddx TMP1, BASE, RA - | ins_next2 - | - |5: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP2, TAB:TMP2->nomm - | andi. TMP2, TMP2, 1<vmeta_tgetb // Caveat: preserve TMP0! - break; - - case BC_TSETV: - | // RA = src*8, RB = table*8, RC = key*8 - | evlddx TAB:RB, BASE, RB - | evlddx RC, BASE, RC - | checktab TAB:RB - | checkfail ->vmeta_tsetv - | checknum RC - | checkfail >5 - | // Convert number key to integer - | efdctsi TMP2, RC - | evlddx SAVE0, BASE, RA - | lwz TMP0, TAB:RB->asize - | efdcfsi TMP1, TMP2 - | cmplw cr0, TMP0, TMP2 - | efdcmpeq cr1, RC, TMP1 - | lwz TMP1, TAB:RB->array - | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+gt - | slwi TMP0, TMP2, 3 - | ble ->vmeta_tsetv // Integer key and in array part? - | lbz TMP3, TAB:RB->marked - | evlddx TMP2, TMP1, TMP0 - | checknil TMP2 - | checkok >3 - |1: - | andi. TMP2, TMP3, LJ_GC_BLACK // isblack(table) - | evstddx SAVE0, TMP1, TMP0 - | bne >7 - |2: - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP2, TAB:TMP2->nomm - | andi. TMP2, TMP2, 1<vmeta_tsetv - | - |5: - | checkstr STR:RC // String key? - | checkok ->BC_TSETS_Z - | b ->vmeta_tsetv - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <2 - break; - case BC_TSETS: - | // RA = src*8, RB = table*8, RC = str_const*8 (~) - | evlddx TAB:RB, BASE, RB - | srwi TMP1, RC, 1 - | checktab TAB:RB - | subfic TMP1, TMP1, -4 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - | checkfail ->vmeta_tsets1 - |->BC_TSETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8 - | lwz TMP0, TAB:RB->hmask - | lwz TMP1, STR:RC->hash - | lwz NODE:TMP2, TAB:RB->node - | evmergelo STR:RC, TISSTR, STR:RC - | stb ZERO, TAB:RB->nomm // Clear metamethod cache. - | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask - | evlddx SAVE0, BASE, RA - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | lbz TMP3, TAB:RB->marked - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | evldd TMP0, NODE:TMP2->key - | evldd TMP1, NODE:TMP2->val - | evcmpeq TMP0, STR:RC - | checkanyfail >5 - | checknil TMP1 - | checkok >4 // Key found, but nil value? - |2: - | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - | evstdd SAVE0, NODE:TMP2->val - | bne >7 - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | lwz TAB:TMP1, TAB:RB->metatable - | cmplwi TAB:TMP1, 0 - | beq <2 // No metatable: done. - | lbz TMP0, TAB:TMP1->nomm - | andi. TMP0, TMP0, 1<vmeta_tsets - | - |5: // Follow hash chain. - | lwz NODE:TMP2, NODE:TMP2->next - | cmplwi NODE:TMP2, 0 - | bne <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | lwz TAB:TMP1, TAB:RB->metatable - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | stw PC, SAVE_PC - | mr CARG1, L - | cmplwi TAB:TMP1, 0 - | stw BASE, L->base - | beq >6 // No metatable: continue. - | lbz TMP0, TAB:TMP1->nomm - | andi. TMP0, TMP0, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | mr CARG2, TAB:RB - | evstdd STR:RC, 0(CARG3) - | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Returns TValue *. - | lwz BASE, L->base - | evstdd SAVE0, 0(CRET1) - | b <3 // No 2nd write barrier needed. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <3 - break; - case BC_TSETB: - | // RA = src*8, RB = table*8, RC = index*8 - | evlddx TAB:RB, BASE, RB - | srwi TMP0, RC, 3 - | checktab TAB:RB - | checkfail ->vmeta_tsetb - | lwz TMP1, TAB:RB->asize - | lwz TMP2, TAB:RB->array - | lbz TMP3, TAB:RB->marked - | cmplw TMP0, TMP1 - | evlddx SAVE0, BASE, RA - | bge ->vmeta_tsetb - | evlddx TMP1, TMP2, RC - | checknil TMP1 - | checkok >5 - |1: - | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - | evstddx SAVE0, TMP2, RC - | bne >7 - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | lwz TAB:TMP1, TAB:RB->metatable - | cmplwi TAB:TMP1, 0 - | beq <1 // No metatable: done. - | lbz TMP1, TAB:TMP1->nomm - | andi. TMP1, TMP1, 1<vmeta_tsetb // Caveat: preserve TMP0! - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <2 - break; - - case BC_TSETM: - | // RA = base*8 (table at base-1), RD = num_const*8 (start index) - | add RA, BASE, RA - |1: - | add TMP3, KBASE, RD - | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table. - | addic. TMP0, MULTRES, -8 - | lwz TMP3, 4(TMP3) // Integer constant is in lo-word. - | srwi CARG3, TMP0, 3 - | beq >4 // Nothing to copy? - | add CARG3, CARG3, TMP3 - | lwz TMP2, TAB:CARG2->asize - | slwi TMP1, TMP3, 3 - | lbz TMP3, TAB:CARG2->marked - | cmplw CARG3, TMP2 - | add TMP2, RA, TMP0 - | lwz TMP0, TAB:CARG2->array - | bgt >5 - | add TMP1, TMP1, TMP0 - | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |3: // Copy result slots to table. - | evldd TMP0, 0(RA) - | addi RA, RA, 8 - | cmpw cr1, RA, TMP2 - | evstdd TMP0, 0(TMP1) - | addi TMP1, TMP1, 8 - | blt cr1, <3 - | bne >7 - |4: - | ins_next - | - |5: // Need to resize array part. - | stw BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | mr SAVE0, RD - | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | // Must not reallocate the stack. - | mr RD, SAVE0 - | b <1 - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, TMP0 - | b <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8 - | add NARGS8:RC, NARGS8:RC, MULTRES - | // Fall through. Assumes BC_CALL follows. - break; - case BC_CALL: - | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8 - | evlddx LFUNC:RB, BASE, RA - | mr TMP2, BASE - | add BASE, BASE, RA - | subi NARGS8:RC, NARGS8:RC, 8 - | checkfunc LFUNC:RB - | addi BASE, BASE, 8 - | checkfail ->vmeta_call - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs*8 - | add NARGS8:RC, NARGS8:RC, MULTRES - | // Fall through. Assumes BC_CALLT follows. - break; - case BC_CALLT: - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - | evlddx LFUNC:RB, BASE, RA - | add RA, BASE, RA - | lwz TMP1, FRAME_PC(BASE) - | subi NARGS8:RC, NARGS8:RC, 8 - | checkfunc LFUNC:RB - | addi RA, RA, 8 - | checkfail ->vmeta_callt - |->BC_CALLT_Z: - | andi. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand. - | lbz TMP3, LFUNC:RB->ffid - | xori TMP2, TMP1, FRAME_VARG - | cmplwi cr1, NARGS8:RC, 0 - | bne >7 - |1: - | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC. - | li TMP2, 0 - | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function? - | beq cr1, >3 - |2: - | addi TMP3, TMP2, 8 - | evlddx TMP0, RA, TMP2 - | cmplw cr1, TMP3, NARGS8:RC - | evstddx TMP0, BASE, TMP2 - | mr TMP2, TMP3 - | bne cr1, <2 - |3: - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt - | beq >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | lwz INS, -4(TMP1) - | decode_RA8 RA, INS - | sub TMP1, BASE, RA - | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1) - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE. - | b <4 - | - |7: // Tailcall from a vararg function. - | andi. TMP0, TMP2, FRAME_TYPEP - | bne <1 // Vararg frame below? - | sub BASE, BASE, TMP2 // Relocate BASE down. - | lwz TMP1, FRAME_PC(BASE) - | andi. TMP0, TMP1, FRAME_TYPE - | b <1 - break; - - case BC_ITERC: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8)) - | subi RA, RA, 24 // evldd doesn't support neg. offsets. - | mr TMP2, BASE - | evlddx LFUNC:RB, BASE, RA - | add BASE, BASE, RA - | evldd TMP0, 8(BASE) - | evldd TMP1, 16(BASE) - | evstdd LFUNC:RB, 24(BASE) // Copy callable. - | checkfunc LFUNC:RB - | evstdd TMP0, 32(BASE) // Copy state. - | li NARGS8:RC, 16 // Iterators get 2 arguments. - | evstdd TMP1, 40(BASE) // Copy control var. - | addi BASE, BASE, 32 - | checkfail ->vmeta_call - | ins_call - break; - - case BC_ITERN: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8) - |.if JIT - | // NYI: add hotloop, record BC_ITERN. - |.endif - | add RA, BASE, RA - | lwz TAB:RB, -12(RA) - | lwz RC, -4(RA) // Get index from control var. - | lwz TMP0, TAB:RB->asize - | lwz TMP1, TAB:RB->array - | addi PC, PC, 4 - |1: // Traverse array part. - | cmplw RC, TMP0 - | slwi TMP3, RC, 3 - | bge >5 // Index points after array part? - | evlddx TMP2, TMP1, TMP3 - | checknil TMP2 - | lwz INS, -4(PC) - | checkok >4 - | efdcfsi TMP0, RC - | addi RC, RC, 1 - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - | evstdd TMP2, 8(RA) - | decode_RD4 TMP1, INS - | stw RC, -4(RA) // Update control var. - | add PC, TMP1, TMP3 - | evstdd TMP0, 0(RA) - |3: - | ins_next - | - |4: // Skip holes in array part. - | addi RC, RC, 1 - | b <1 - | - |5: // Traverse hash part. - | lwz TMP1, TAB:RB->hmask - | sub RC, RC, TMP0 - | lwz TMP2, TAB:RB->node - |6: - | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1. - | slwi TMP3, RC, 5 - | bgt <3 - | slwi RB, RC, 3 - | sub TMP3, TMP3, RB - | evlddx RB, TMP2, TMP3 - | add NODE:TMP3, TMP2, TMP3 - | checknil RB - | lwz INS, -4(PC) - | checkok >7 - | evldd TMP3, NODE:TMP3->key - | addis TMP2, PC, -(BCBIAS_J*4 >> 16) - | evstdd RB, 8(RA) - | add RC, RC, TMP0 - | decode_RD4 TMP1, INS - | evstdd TMP3, 0(RA) - | addi RC, RC, 1 - | add PC, TMP1, TMP2 - | stw RC, -4(RA) // Update control var. - | b <3 - | - |7: // Skip holes in hash part. - | addi RC, RC, 1 - | b <6 - break; - - case BC_ISNEXT: - | // RA = base*8, RD = target (points to ITERN) - | add RA, BASE, RA - | li TMP2, -24 - | evlddx CFUNC:TMP1, RA, TMP2 - | lwz TMP2, -16(RA) - | lwz TMP3, -8(RA) - | evmergehi TMP0, CFUNC:TMP1, CFUNC:TMP1 - | cmpwi cr0, TMP2, LJ_TTAB - | cmpwi cr1, TMP0, LJ_TFUNC - | cmpwi cr6, TMP3, LJ_TNIL - | bne cr1, >5 - | lbz TMP1, CFUNC:TMP1->ffid - | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq - | cmpwi cr7, TMP1, FF_next_N - | srwi TMP0, RD, 1 - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq - | add TMP3, PC, TMP0 - | bne cr0, >5 - | lus TMP1, 0xfffe - | ori TMP1, TMP1, 0x7fff - | stw ZERO, -4(RA) // Initialize control var. - | stw TMP1, -8(RA) - | addis PC, TMP3, -(BCBIAS_J*4 >> 16) - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | li TMP0, BC_JMP - | li TMP1, BC_ITERC - | stb TMP0, -1(PC) - | addis PC, TMP3, -(BCBIAS_J*4 >> 16) - | stb TMP1, 3(PC) - | b <1 - break; - - case BC_VARG: - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | lwz TMP0, FRAME_PC(BASE) - | add RC, BASE, RC - | add RA, BASE, RA - | addi RC, RC, FRAME_VARG - | add TMP2, RA, RB - | subi TMP3, BASE, 8 // TMP3 = vtop - | sub RC, RC, TMP0 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | cmplwi cr1, RB, 0 - | sub. TMP1, TMP3, RC - | beq cr1, >5 // Copy all varargs? - | subi TMP2, TMP2, 16 - | ble >2 // No vararg slots? - |1: // Copy vararg slots to destination slots. - | evldd TMP0, 0(RC) - | addi RC, RC, 8 - | evstdd TMP0, 0(RA) - | cmplw RA, TMP2 - | cmplw cr1, RC, TMP3 - | bge >3 // All destination slots filled? - | addi RA, RA, 8 - | blt cr1, <1 // More vararg slots? - |2: // Fill up remainder with nil. - | evstdd TISNIL, 0(RA) - | cmplw RA, TMP2 - | addi RA, RA, 8 - | blt <2 - |3: - | ins_next - | - |5: // Copy all varargs. - | lwz TMP0, L->maxstack - | li MULTRES, 8 // MULTRES = (0+1)*8 - | ble <3 // No vararg slots? - | add TMP2, RA, TMP1 - | cmplw TMP2, TMP0 - | addi MULTRES, TMP1, 8 - | bgt >7 - |6: - | evldd TMP0, 0(RC) - | addi RC, RC, 8 - | evstdd TMP0, 0(RA) - | cmplw RC, TMP3 - | addi RA, RA, 8 - | blt <6 // More vararg slots? - | b <3 - | - |7: // Grow stack for varargs. - | mr CARG1, L - | stw RA, L->top - | sub SAVE0, RC, BASE // Need delta, because BASE may change. - | stw BASE, L->base - | sub RA, RA, BASE - | stw PC, SAVE_PC - | srwi CARG2, TMP1, 3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | lwz BASE, L->base - | add RA, BASE, RA - | add RC, BASE, SAVE0 - | subi TMP3, BASE, 8 - | b <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RD = extra_nresults*8 - | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8. - | // Fall through. Assumes BC_RET follows. - break; - - case BC_RET: - | // RA = results*8, RD = (nresults+1)*8 - | lwz PC, FRAME_PC(BASE) - | add RA, BASE, RA - | mr MULTRES, RD - |1: - | andi. TMP0, PC, FRAME_TYPE - | xori TMP1, PC, FRAME_VARG - | bne ->BC_RETV_Z - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return - | lwz INS, -4(PC) - | cmpwi RD, 8 - | subi TMP2, BASE, 8 - | subi RC, RD, 8 - | decode_RB8 RB, INS - | beq >3 - | li TMP1, 0 - |2: - | addi TMP3, TMP1, 8 - | evlddx TMP0, RA, TMP1 - | cmpw TMP3, RC - | evstddx TMP0, TMP2, TMP1 - | beq >3 - | addi TMP1, TMP3, 8 - | evlddx TMP0, RA, TMP3 - | cmpw TMP1, RC - | evstddx TMP0, TMP2, TMP3 - | bne <2 - |3: - |5: - | cmplw RB, RD - | decode_RA8 RA, INS - | bgt >6 - | sub BASE, TMP2, RA - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | evstddx TISNIL, TMP2, TMP1 - | b <5 - | - |->BC_RETV_Z: // Non-standard return case. - | andi. TMP2, TMP1, FRAME_TYPEP - | bne ->vm_return - | // Return from vararg function: relocate BASE down. - | sub BASE, BASE, TMP1 - | lwz PC, FRAME_PC(BASE) - | b <1 - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RD = (nresults+1)*8 - | lwz PC, FRAME_PC(BASE) - | add RA, BASE, RA - | mr MULTRES, RD - | andi. TMP0, PC, FRAME_TYPE - | xori TMP1, PC, FRAME_VARG - | bne ->BC_RETV_Z - | - | lwz INS, -4(PC) - | subi TMP2, BASE, 8 - | decode_RB8 RB, INS - if (op == BC_RET1) { - | evldd TMP0, 0(RA) - | evstdd TMP0, 0(TMP2) - } - |5: - | cmplw RB, RD - | decode_RA8 RA, INS - | bgt >6 - | sub BASE, TMP2, RA - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | evstddx TISNIL, TMP2, TMP1 - | b <5 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RD = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - | add RA, BASE, RA - | evldd TMP1, FORL_IDX*8(RA) - | evldd TMP3, FORL_STEP*8(RA) - | evldd TMP2, FORL_STOP*8(RA) - if (!vk) { - | evcmpgtu cr0, TMP1, TISNUM - | evcmpgtu cr7, TMP3, TISNUM - | evcmpgtu cr1, TMP2, TISNUM - | cror 4*cr0+lt, 4*cr0+lt, 4*cr7+lt - | cror 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | blt ->vmeta_for - } - if (vk) { - | efdadd TMP1, TMP1, TMP3 - | evstdd TMP1, FORL_IDX*8(RA) - } - | evcmpgts TMP3, TISNIL - | evstdd TMP1, FORL_EXT*8(RA) - | bge >2 - | efdcmpgt TMP1, TMP2 - |1: - if (op != BC_JFORL) { - | srwi RD, RD, 1 - | add RD, PC, RD - if (op == BC_JFORI) { - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } else { - | addis RD, RD, -(BCBIAS_J*4 >> 16) - } - } - if (op == BC_FORI) { - | iselgt PC, RD, PC - } else if (op == BC_IFORL) { - | iselgt PC, PC, RD - } else { - | ble =>BC_JLOOP - } - | ins_next - |2: - | efdcmpgt TMP2, TMP1 - | b <1 - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RD = target - | evlddx TMP1, BASE, RA - | subi RA, RA, 8 - | checknil TMP1 - | checkok >1 // Stop if iterator returned nil. - if (op == BC_JITERL) { - | NYI - } else { - | branch_RD // Otherwise save control var + branch. - | evstddx TMP1, BASE, RA - } - |1: - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | NYI - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RD = target - | branch_RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lwz TMP2, L->maxstack - | lbz TMP1, -4+PC2PROTO(numparams)(PC) - | lwz KBASE, -4+PC2PROTO(k)(PC) - | cmplw RA, TMP2 - | slwi TMP1, TMP1, 3 - | bgt ->vm_growstack_l - | ins_next1 - |2: - | cmplw NARGS8:RC, TMP1 // Check for missing parameters. - | ble >3 - if (op == BC_JFUNCF) { - | NYI - } else { - | ins_next2 - } - | - |3: // Clear missing parameters. - | evstddx TISNIL, BASE, NARGS8:RC - | addi NARGS8:RC, NARGS8:RC, 8 - | b <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lwz TMP2, L->maxstack - | add TMP1, BASE, RC - | add TMP0, RA, RC - | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC. - | addi TMP3, RC, 8+FRAME_VARG - | lwz KBASE, -4+PC2PROTO(k)(PC) - | cmplw TMP0, TMP2 - | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG. - | bge ->vm_growstack_l - | lbz TMP2, -4+PC2PROTO(numparams)(PC) - | mr RA, BASE - | mr RC, TMP1 - | ins_next1 - | cmpwi TMP2, 0 - | addi BASE, TMP1, 8 - | beq >3 - |1: - | cmplw RA, RC // Less args than parameters? - | evldd TMP0, 0(RA) - | bge >4 - | evstdd TISNIL, 0(RA) // Clear old fixarg slot (help the GC). - | addi RA, RA, 8 - |2: - | addic. TMP2, TMP2, -1 - | evstdd TMP0, 8(TMP1) - | addi TMP1, TMP1, 8 - | bne <1 - |3: - | ins_next2 - | - |4: // Clear missing parameters. - | evmr TMP0, TISNIL - | b <2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | lwz TMP3, CFUNC:RB->f - } else { - | lwz TMP3, DISPATCH_GL(wrapf)(DISPATCH) - } - | add TMP1, RA, NARGS8:RC - | lwz TMP2, L->maxstack - | add RC, BASE, NARGS8:RC - | stw BASE, L->base - | cmplw TMP1, TMP2 - | stw RC, L->top - | li_vmstate C - | mtctr TMP3 - if (op == BC_FUNCCW) { - | lwz CARG2, CFUNC:RB->f - } - | mr CARG1, L - | bgt ->vm_growstack_c // Need to grow stack. - | st_vmstate - | bctrl // (lua_State *L [, lua_CFunction f]) - | // Returns nresults. - | lwz TMP1, L->top - | slwi RD, CRET1, 3 - | lwz BASE, L->base - | li_vmstate INTERP - | lwz PC, FRAME_PC(BASE) // Fetch PC of caller. - | sub RA, TMP1, RD // RA = L->top - nresults*8 - | st_vmstate - | b ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" - "\t.long .Lbegin\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x5\n\t.uleb128 70\n\t.sleb128 37\n", - (int)ctx->codesz, CFRAME_SIZE); - for (i = 14; i <= 31; i++) - fprintf(ctx->fp, - "\t.byte %d\n\t.uleb128 %d\n" - "\t.byte 5\n\t.uleb128 %d\n\t.uleb128 %d\n", - 0x80+i, 1+2*(31-i), 1200+i, 2+2*(31-i)); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe1:\n" - "\t.long .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.long lj_err_unwind_dwarf-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .LASFDE1-.Lframe1\n" - "\t.long .Lbegin-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x5\n\t.uleb128 70\n\t.sleb128 37\n", - (int)ctx->codesz, CFRAME_SIZE); - for (i = 14; i <= 31; i++) - fprintf(ctx->fp, - "\t.byte %d\n\t.uleb128 %d\n" - "\t.byte 5\n\t.uleb128 %d\n\t.uleb128 %d\n", - 0x80+i, 1+2*(31-i), 1200+i, 2+2*(31-i)); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE1:\n\n"); - break; - default: - break; - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/vm_x86.dasc b/third-party/LuaJIT-2.0.2/src/vm_x86.dasc deleted file mode 100644 index f25dfd302b..0000000000 --- a/third-party/LuaJIT-2.0.2/src/vm_x86.dasc +++ /dev/null @@ -1,6374 +0,0 @@ -|// Low-level VM code for x86 CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h -| -|.if P64 -|.arch x64 -|.else -|.arch x86 -|.endif -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|//----------------------------------------------------------------------- -| -|.if P64 -|.define X64, 1 -|.define SSE, 1 -|.if WIN -|.define X64WIN, 1 -|.endif -|.endif -| -|// Fixed register assignments for the interpreter. -|// This is very fragile and has many dependencies. Caveat emptor. -|.define BASE, edx // Not C callee-save, refetched anyway. -|.if not X64 -|.define KBASE, edi // Must be C callee-save. -|.define KBASEa, KBASE -|.define PC, esi // Must be C callee-save. -|.define PCa, PC -|.define DISPATCH, ebx // Must be C callee-save. -|.elif X64WIN -|.define KBASE, edi // Must be C callee-save. -|.define KBASEa, rdi -|.define PC, esi // Must be C callee-save. -|.define PCa, rsi -|.define DISPATCH, ebx // Must be C callee-save. -|.else -|.define KBASE, r15d // Must be C callee-save. -|.define KBASEa, r15 -|.define PC, ebx // Must be C callee-save. -|.define PCa, rbx -|.define DISPATCH, r14d // Must be C callee-save. -|.endif -| -|.define RA, ecx -|.define RAH, ch -|.define RAL, cl -|.define RB, ebp // Must be ebp (C callee-save). -|.define RC, eax // Must be eax. -|.define RCW, ax -|.define RCH, ah -|.define RCL, al -|.define OP, RB -|.define RD, RC -|.define RDW, RCW -|.define RDL, RCL -|.if X64 -|.define RAa, rcx -|.define RBa, rbp -|.define RCa, rax -|.define RDa, rax -|.else -|.define RAa, RA -|.define RBa, RB -|.define RCa, RC -|.define RDa, RD -|.endif -| -|.if not X64 -|.define FCARG1, ecx // x86 fastcall arguments. -|.define FCARG2, edx -|.elif X64WIN -|.define CARG1, rcx // x64/WIN64 C call arguments. -|.define CARG2, rdx -|.define CARG3, r8 -|.define CARG4, r9 -|.define CARG1d, ecx -|.define CARG2d, edx -|.define CARG3d, r8d -|.define CARG4d, r9d -|.define FCARG1, CARG1d // Upwards compatible to x86 fastcall. -|.define FCARG2, CARG2d -|.else -|.define CARG1, rdi // x64/POSIX C call arguments. -|.define CARG2, rsi -|.define CARG3, rdx -|.define CARG4, rcx -|.define CARG5, r8 -|.define CARG6, r9 -|.define CARG1d, edi -|.define CARG2d, esi -|.define CARG3d, edx -|.define CARG4d, ecx -|.define CARG5d, r8d -|.define CARG6d, r9d -|.define FCARG1, CARG1d // Simulate x86 fastcall. -|.define FCARG2, CARG2d -|.endif -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS, int -|.type TRACE, GCtrace -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|//----------------------------------------------------------------------- -|.if not X64 // x86 stack layout. -| -|.define CFRAME_SPACE, aword*7 // Delta for esp (see <--). -|.macro saveregs_ -| push edi; push esi; push ebx -| sub esp, CFRAME_SPACE -|.endmacro -|.macro saveregs -| push ebp; saveregs_ -|.endmacro -|.macro restoreregs -| add esp, CFRAME_SPACE -| pop ebx; pop esi; pop edi; pop ebp -|.endmacro -| -|.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only. -|.define SAVE_NRES, aword [esp+aword*14] -|.define SAVE_CFRAME, aword [esp+aword*13] -|.define SAVE_L, aword [esp+aword*12] -|//----- 16 byte aligned, ^^^ arguments from C caller -|.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter. -|.define SAVE_R4, aword [esp+aword*10] -|.define SAVE_R3, aword [esp+aword*9] -|.define SAVE_R2, aword [esp+aword*8] -|//----- 16 byte aligned -|.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves. -|.define SAVE_PC, aword [esp+aword*6] -|.define TMP2, aword [esp+aword*5] -|.define TMP1, aword [esp+aword*4] -|//----- 16 byte aligned -|.define ARG4, aword [esp+aword*3] -|.define ARG3, aword [esp+aword*2] -|.define ARG2, aword [esp+aword*1] -|.define ARG1, aword [esp] //<-- esp while in interpreter. -|//----- 16 byte aligned, ^^^ arguments for C callee -| -|// FPARGx overlaps ARGx and ARG(x+1) on x86. -|.define FPARG3, qword [esp+qword*1] -|.define FPARG1, qword [esp] -|// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ). -|.define TMPQ, qword [esp+aword*4] -|.define TMP3, ARG4 -|.define ARG5, TMP1 -|.define TMPa, TMP1 -|.define MULTRES, TMP2 -| -|// Arguments for vm_call and vm_pcall. -|.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME! -| -|// Arguments for vm_cpcall. -|.define INARG_CP_CALL, SAVE_ERRF -|.define INARG_CP_UD, SAVE_NRES -|.define INARG_CP_FUNC, SAVE_CFRAME -| -|//----------------------------------------------------------------------- -|.elif X64WIN // x64/Windows stack layout -| -|.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--). -|.macro saveregs_ -| push rdi; push rsi; push rbx -| sub rsp, CFRAME_SPACE -|.endmacro -|.macro saveregs -| push rbp; saveregs_ -|.endmacro -|.macro restoreregs -| add rsp, CFRAME_SPACE -| pop rbx; pop rsi; pop rdi; pop rbp -|.endmacro -| -|.define SAVE_CFRAME, aword [rsp+aword*13] -|.define SAVE_PC, dword [rsp+dword*25] -|.define SAVE_L, dword [rsp+dword*24] -|.define SAVE_ERRF, dword [rsp+dword*23] -|.define SAVE_NRES, dword [rsp+dword*22] -|.define TMP2, dword [rsp+dword*21] -|.define TMP1, dword [rsp+dword*20] -|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter -|.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*8] -|.define SAVE_R3, aword [rsp+aword*7] -|.define SAVE_R2, aword [rsp+aword*6] -|.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves. -|.define ARG5, aword [rsp+aword*4] -|.define CSAVE_4, aword [rsp+aword*3] -|.define CSAVE_3, aword [rsp+aword*2] -|.define CSAVE_2, aword [rsp+aword*1] -|.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter. -|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee -| -|// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ). -|.define TMPQ, qword [rsp+aword*10] -|.define MULTRES, TMP2 -|.define TMPa, ARG5 -|.define ARG5d, dword [rsp+aword*4] -|.define TMP3, ARG5d -| -|//----------------------------------------------------------------------- -|.else // x64/POSIX stack layout -| -|.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--). -|.macro saveregs_ -| push rbx; push r15; push r14 -| sub rsp, CFRAME_SPACE -|.endmacro -|.macro saveregs -| push rbp; saveregs_ -|.endmacro -|.macro restoreregs -| add rsp, CFRAME_SPACE -| pop r14; pop r15; pop rbx; pop rbp -|.endmacro -| -|//----- 16 byte aligned, -|.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*8] -|.define SAVE_R3, aword [rsp+aword*7] -|.define SAVE_R2, aword [rsp+aword*6] -|.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves. -|.define SAVE_CFRAME, aword [rsp+aword*4] -|.define SAVE_PC, dword [rsp+dword*7] -|.define SAVE_L, dword [rsp+dword*6] -|.define SAVE_ERRF, dword [rsp+dword*5] -|.define SAVE_NRES, dword [rsp+dword*4] -|.define TMPa, aword [rsp+aword*1] -|.define TMP2, dword [rsp+dword*1] -|.define TMP1, dword [rsp] //<-- rsp while in interpreter. -|//----- 16 byte aligned -| -|// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ). -|.define TMPQ, qword [rsp] -|.define TMP3, dword [rsp+aword*1] -|.define MULTRES, TMP2 -| -|.endif -| -|//----------------------------------------------------------------------- -| -|// Instruction headers. -|.macro ins_A; .endmacro -|.macro ins_AD; .endmacro -|.macro ins_AJ; .endmacro -|.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro -|.macro ins_AB_; movzx RB, RCH; .endmacro -|.macro ins_A_C; movzx RC, RCL; .endmacro -|.macro ins_AND; not RDa; .endmacro -| -|// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster). -|.macro ins_NEXT -| mov RC, [PC] -| movzx RA, RCH -| movzx OP, RCL -| add PC, 4 -| shr RC, 16 -|.if X64 -| jmp aword [DISPATCH+OP*8] -|.else -| jmp aword [DISPATCH+OP*4] -|.endif -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| // Around 10%-30% slower on Core2, a lot more slower on P4. -| .macro ins_next -| jmp ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC -| mov PC, LFUNC:RB->pc -| mov RA, [PC] -| movzx OP, RAL -| movzx RA, RAH -| add PC, 4 -|.if X64 -| jmp aword [DISPATCH+OP*8] -|.else -| jmp aword [DISPATCH+OP*4] -|.endif -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC, RD = nargs+1 -| mov [BASE-4], PC -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to test operand types. -|.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro -|.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro -|.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro -|.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro -|.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro -| -|// These operands must be used with movzx. -|.define PC_OP, byte [PC-4] -|.define PC_RA, byte [PC-3] -|.define PC_RB, byte [PC-1] -|.define PC_RC, byte [PC-2] -|.define PC_RD, word [PC-2] -| -|.macro branchPC, reg -| lea PC, [PC+reg*4-BCBIAS_J*4] -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|// Decrement hashed hotcount and trigger trace recorder if zero. -|.macro hotloop, reg -| mov reg, PC -| shr reg, 1 -| and reg, HOTCOUNT_PCMASK -| sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP -| jb ->vm_hotloop -|.endmacro -| -|.macro hotcall, reg -| mov reg, PC -| shr reg, 1 -| and reg, HOTCOUNT_PCMASK -| sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL -| jb ->vm_hotcall -|.endmacro -| -|// Set current VM state. -|.macro set_vmstate, st -| mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st -|.endmacro -| -|// x87 compares. -|.macro fcomparepp // Compare and pop st0 >< st1. -| fucomip st1 -| fpop -|.endmacro -| -|.macro fdup; fld st0; .endmacro -|.macro fpop1; fstp st1; .endmacro -| -|// Synthesize SSE FP constants. -|.macro sseconst_abs, reg, tmp // Synthesize abs mask. -|.if X64 -| mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp -|.else -| pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1 -|.endif -|.endmacro -| -|.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const. -|.if X64 -| mov64 tmp, U64x(val,00000000); movd reg, tmp -|.else -| mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51 -|.endif -|.endmacro -| -|.macro sseconst_sign, reg, tmp // Synthesize sign mask. -| sseconst_hi reg, tmp, 80000000 -|.endmacro -|.macro sseconst_1, reg, tmp // Synthesize 1.0. -| sseconst_hi reg, tmp, 3ff00000 -|.endmacro -|.macro sseconst_m1, reg, tmp // Synthesize -1.0. -| sseconst_hi reg, tmp, bff00000 -|.endmacro -|.macro sseconst_2p52, reg, tmp // Synthesize 2^52. -| sseconst_hi reg, tmp, 43300000 -|.endmacro -|.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51. -| sseconst_hi reg, tmp, 43380000 -|.endmacro -| -|// Move table write barrier back. Overwrites reg. -|.macro barrierback, tab, reg -| and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab) -| mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)] -| mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab -| mov tab->gclist, reg -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | test PC, FRAME_P - | jz ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | and PC, -8 - | sub BASE, PC // Restore caller base. - | lea RAa, [RA+PC-8] // Rebase RA and prepend one result. - | mov PC, [BASE-4] // Fetch PC of previous frame. - | // Prepending may overwrite the pcall frame, so do it at the end. - | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results. - | - |->vm_returnc: - | add RD, 1 // RD = nresults+1 - | jz ->vm_unwind_yield - | mov MULTRES, RD - | test PC, FRAME_TYPE - | jz ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return - | xor PC, FRAME_C - | test PC, FRAME_TYPE - | jnz ->vm_returnp - | - | // Return to C. - | set_vmstate C - | and PC, -8 - | sub PC, BASE - | neg PC // Previous base = BASE - delta. - | - | sub RD, 1 - | jz >2 - |1: // Move results down. - |.if X64 - | mov RBa, [BASE+RA] - | mov [BASE-8], RBa - |.else - | mov RB, [BASE+RA] - | mov [BASE-8], RB - | mov RB, [BASE+RA+4] - | mov [BASE-4], RB - |.endif - | add BASE, 8 - | sub RD, 1 - | jnz <1 - |2: - | mov L:RB, SAVE_L - | mov L:RB->base, PC - |3: - | mov RD, MULTRES - | mov RA, SAVE_NRES // RA = wanted nresults+1 - |4: - | cmp RA, RD - | jne >6 // More/less results wanted? - |5: - | sub BASE, 8 - | mov L:RB->top, BASE - | - |->vm_leave_cp: - | mov RAa, SAVE_CFRAME // Restore previous C frame. - | mov L:RB->cframe, RAa - | xor eax, eax // Ok return status for vm_pcall. - | - |->vm_leave_unw: - | restoreregs - | ret - | - |6: - | jb >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | cmp BASE, L:RB->maxstack - | ja >8 - | mov dword [BASE-4], LJ_TNIL - | add BASE, 8 - | add RD, 1 - | jmp <4 - | - |7: // Less results wanted. - | test RA, RA - | jz <5 // But check for LUA_MULTRET+1. - | sub RA, RD // Negative result! - | lea BASE, [BASE+RA*8] // Correct top. - | jmp <5 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | mov L:RB->top, BASE // Save current top held in BASE (yes). - | mov MULTRES, RD // Need to fill only remainder with nil. - | mov FCARG2, RA - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->top // Need the (realloced) L->top in BASE. - | jmp <3 - | - |->vm_unwind_yield: - | mov al, LUA_YIELD - | jmp ->vm_unwind_c_eh - | - |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - |.if X64 - | mov eax, CARG2d // Error return status for vm_pcall. - | mov rsp, CARG1 - |.else - | mov eax, FCARG2 // Error return status for vm_pcall. - | mov esp, FCARG1 - |.endif - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | mov L:RB, SAVE_L - | mov GL:RB, L:RB->glref - | mov dword GL:RB->vmstate, ~LJ_VMST_C - | jmp ->vm_leave_unw - | - |->vm_unwind_rethrow: - |.if X64 and not X64WIN - | mov FCARG1, SAVE_L - | mov FCARG2, eax - | restoreregs - | jmp extern lj_err_throw@8 // (lua_State *L, int errcode) - |.endif - | - |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall. - | // (void *cframe) - |.if X64 - | and CARG1, CFRAME_RAWMASK - | mov rsp, CARG1 - |.else - | and FCARG1, CFRAME_RAWMASK - | mov esp, FCARG1 - |.endif - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | mov L:RB, SAVE_L - | mov RAa, -8 // Results start at BASE+RA = BASE-8. - | mov RD, 1+1 // Really 1+2 results, incr. later. - | mov BASE, L:RB->base - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | add DISPATCH, GG_G2DISP - | mov PC, [BASE-4] // Fetch PC of previous frame. - | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message. - | set_vmstate INTERP - | jmp ->vm_returnc // Increments RD/MULTRES and returns. - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | mov FCARG2, LUA_MINSTACK - | jmp >2 - | - |->vm_growstack_v: // Grow stack for vararg Lua function. - | sub RD, 8 - | jmp >1 - | - |->vm_growstack_f: // Grow stack for fixarg Lua function. - | // BASE = new base, RD = nargs+1, RB = L, PC = first PC - | lea RD, [BASE+NARGS:RD*8-8] - |1: - | movzx RA, byte [PC-4+PC2PROTO(framesize)] - | add PC, 4 // Must point after first instruction. - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov SAVE_PC, PC - | mov FCARG2, RA - |2: - | // RB = L, L->base = new base, L->top = top - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->base - | mov RD, L:RB->top - | mov LFUNC:RB, [BASE-8] - | sub RD, BASE - | shr RD, 3 - | add NARGS:RD, 1 - | // BASE = new base, RB = LFUNC, RD = nargs+1 - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - |.if X64 - | mov L:RB, CARG1d // Caveat: CARG1d may be RA. - | mov SAVE_L, CARG1d - | mov RA, CARG2d - |.else - | mov L:RB, SAVE_L - | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME! - |.endif - | mov PC, FRAME_CP - | xor RD, RD - | lea KBASEa, [esp+CFRAME_RESUME] - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | add DISPATCH, GG_G2DISP - | mov L:RB->cframe, KBASEa - | mov SAVE_PC, RD // Any value outside of bytecode is ok. - | mov SAVE_CFRAME, RDa - |.if X64 - | mov SAVE_NRES, RD - | mov SAVE_ERRF, RD - |.endif - | cmp byte L:RB->status, RDL - | je >3 // Initial resume (like a call). - | - | // Resume after yield (like a return). - | set_vmstate INTERP - | mov byte L:RB->status, RDL - | mov BASE, L:RB->base - | mov RD, L:RB->top - | sub RD, RA - | shr RD, 3 - | add RD, 1 // RD = nresults+1 - | sub RA, BASE // RA = resultofs - | mov PC, [BASE-4] - | mov MULTRES, RD - | test PC, FRAME_TYPE - | jz ->BC_RET_Z - | jmp ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | mov PC, FRAME_CP - |.if X64 - | mov SAVE_ERRF, CARG4d - |.endif - | jmp >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | mov PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - |.if X64 - | mov SAVE_NRES, CARG3d - | mov L:RB, CARG1d // Caveat: CARG1d may be RA. - | mov SAVE_L, CARG1d - | mov RA, CARG2d - |.else - | mov L:RB, SAVE_L - | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME! - |.endif - | - | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASEa - | mov SAVE_PC, L:RB // Any value outside of bytecode is ok. - |.if X64 - | mov L:RB->cframe, rsp - |.else - | mov L:RB->cframe, esp - |.endif - | - |2: // Entry point for vm_cpcall below (RA = base, RB = L, PC = ftype). - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | add DISPATCH, GG_G2DISP - | - |3: // Entry point for vm_resume above (RA = base, RB = L, PC = ftype). - | set_vmstate INTERP - | mov BASE, L:RB->base // BASE = old base (used in vmeta_call). - | add PC, RA - | sub PC, BASE // PC = frame delta + frame type - | - | mov RD, L:RB->top - | sub RD, RA - | shr NARGS:RD, 3 - | add NARGS:RD, 1 // RD = nargs+1 - | - |->vm_call_dispatch: - | mov LFUNC:RB, [RA-8] - | cmp dword [RA-4], LJ_TFUNC - | jne ->vmeta_call // Ensure KBASE defined and != BASE. - | - |->vm_call_dispatch_f: - | mov BASE, RA - | ins_call - | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - |.if X64 - | mov L:RB, CARG1d // Caveat: CARG1d may be RA. - | mov SAVE_L, CARG1d - |.else - | mov L:RB, SAVE_L - | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap! - | mov RC, INARG_CP_UD // Get args before they are overwritten. - | mov RA, INARG_CP_FUNC - | mov BASE, INARG_CP_CALL - |.endif - | mov SAVE_PC, L:RB // Any value outside of bytecode is ok. - | - | mov KBASE, L:RB->stack // Compute -savestack(L, L->top). - | sub KBASE, L:RB->top - | mov SAVE_ERRF, 0 // No error function. - | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame. - | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe). - | - |.if X64 - | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASEa - | mov L:RB->cframe, rsp - | - | call CARG4 // (lua_State *L, lua_CFunction func, void *ud) - |.else - | mov ARG3, RC // Have to copy args downwards. - | mov ARG2, RA - | mov ARG1, L:RB - | - | mov KBASE, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASE - | mov L:RB->cframe, esp - | - | call BASE // (lua_State *L, lua_CFunction func, void *ud) - |.endif - | // TValue * (new base) or NULL returned in eax (RC). - | test RC, RC - | jz ->vm_leave_cp // No base? Just remove C frame. - | mov RA, RC - | mov PC, FRAME_CP - | jmp <2 // Else continue with the call. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES) - | add RA, BASE - | and PC, -8 - | mov RB, BASE - | sub BASE, PC // Restore caller BASE. - | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg. - | mov RC, RA // ... in [RC] - | mov PC, [RB-12] // Restore PC from [cont|PC]. - |.if X64 - | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug. - |.if FFI - | cmp RA, 1 - | jbe >1 - |.endif - | lea KBASEa, qword [=>0] - | add RAa, KBASEa - |.else - | mov RA, dword [RB-16] - |.if FFI - | cmp RA, 1 - | jbe >1 - |.endif - |.endif - | mov LFUNC:KBASE, [BASE-8] - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | // BASE = base, RC = result, RB = meta base - | jmp RAa // Jump to continuation. - | - |.if FFI - |1: - | je ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: Tail call from C function. - | sub RB, BASE - | shr RB, 3 - | lea RD, [RB-1] - | jmp ->vm_call_tail - |.endif - | - |->cont_cat: // BASE = base, RC = result, RB = mbase - | movzx RA, PC_RB - | sub RB, 16 - | lea RA, [BASE+RA*8] - | sub RA, RB - | je ->cont_ra - | neg RA - | shr RA, 3 - |.if X64WIN - | mov CARG3d, RA - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | mov RCa, [RC] - | mov [RB], RCa - | mov CARG2d, RB - |.elif X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | mov CARG3d, RA - | mov RAa, [RC] - | mov [RB], RAa - | mov CARG2d, RB - |.else - | mov ARG3, RA - | mov RA, [RC+4] - | mov RC, [RC] - | mov [RB+4], RA - | mov [RB], RC - | mov ARG2, RB - |.endif - | jmp ->BC_CAT_Z - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets: - | mov TMP1, RC // RC = GCstr * - | mov TMP2, LJ_TSTR - | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2. - | cmp PC_OP, BC_GGET - | jne >1 - | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv. - | mov [RA], TAB:RB // RB = GCtab * - | mov dword [RA+4], LJ_TTAB - | mov RB, RA - | jmp >2 - | - |->vmeta_tgetb: - | movzx RC, PC_RC - |.if DUALNUM - | mov TMP2, LJ_TISNUM - | mov TMP1, RC - |.elif SSE - | cvtsi2sd xmm0, RC - | movsd TMPQ, xmm0 - |.else - | mov ARG4, RC - | fild ARG4 - | fstp TMPQ - |.endif - | lea RCa, TMPQ // Store temp. TValue in TMPQ. - | jmp >1 - | - |->vmeta_tgetv: - | movzx RC, PC_RC // Reload TValue *k from RC. - | lea RC, [BASE+RC*8] - |1: - | movzx RB, PC_RB // Reload TValue *t from RB. - | lea RB, [BASE+RB*8] - |2: - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, RB - | mov CARG3, RCa // May be 64 bit ptr to stack. - | mov L:RB, L:CARG1d - |.else - | mov ARG2, RB - | mov L:RB, SAVE_L - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // TValue * (finished) or NULL (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz >3 - |->cont_ra: // BASE = base, RC = result - | movzx RA, PC_RA - |.if X64 - | mov RBa, [RC] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [RC+4] - | mov RC, [RC] - | mov [BASE+RA*8+4], RB - | mov [BASE+RA*8], RC - |.endif - | ins_next - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | mov RA, L:RB->top - | mov [RA-12], PC // [cont|PC] - | lea PC, [RA+FRAME_CONT] - | sub PC, BASE - | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here. - | mov NARGS:RD, 2+1 // 2 args for func(t, k). - | jmp ->vm_call_dispatch_f - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets: - | mov TMP1, RC // RC = GCstr * - | mov TMP2, LJ_TSTR - | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2. - | cmp PC_OP, BC_GSET - | jne >1 - | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv. - | mov [RA], TAB:RB // RB = GCtab * - | mov dword [RA+4], LJ_TTAB - | mov RB, RA - | jmp >2 - | - |->vmeta_tsetb: - | movzx RC, PC_RC - |.if DUALNUM - | mov TMP2, LJ_TISNUM - | mov TMP1, RC - |.elif SSE - | cvtsi2sd xmm0, RC - | movsd TMPQ, xmm0 - |.else - | mov ARG4, RC - | fild ARG4 - | fstp TMPQ - |.endif - | lea RCa, TMPQ // Store temp. TValue in TMPQ. - | jmp >1 - | - |->vmeta_tsetv: - | movzx RC, PC_RC // Reload TValue *k from RC. - | lea RC, [BASE+RC*8] - |1: - | movzx RB, PC_RB // Reload TValue *t from RB. - | lea RB, [BASE+RB*8] - |2: - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, RB - | mov CARG3, RCa // May be 64 bit ptr to stack. - | mov L:RB, L:CARG1d - |.else - | mov ARG2, RB - | mov L:RB, SAVE_L - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // TValue * (finished) or NULL (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz >3 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | movzx RA, PC_RA - |.if X64 - | mov RBa, [BASE+RA*8] - | mov [RC], RBa - |.else - | mov RB, [BASE+RA*8+4] - | mov RA, [BASE+RA*8] - | mov [RC+4], RB - | mov [RC], RA - |.endif - |->cont_nop: // BASE = base, (RC = result) - | ins_next - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | mov RA, L:RB->top - | mov [RA-12], PC // [cont|PC] - | movzx RC, PC_RA - | // Copy value to third argument. - |.if X64 - | mov RBa, [BASE+RC*8] - | mov [RA+16], RBa - |.else - | mov RB, [BASE+RC*8+4] - | mov RC, [BASE+RC*8] - | mov [RA+20], RB - | mov [RA+16], RC - |.endif - | lea PC, [RA+FRAME_CONT] - | sub PC, BASE - | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here. - | mov NARGS:RD, 3+1 // 3 args for func(t, k, v). - | jmp ->vm_call_dispatch_f - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - |.if X64 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE. - |.if X64WIN - | lea CARG3d, [BASE+RD*8] - | lea CARG2d, [BASE+RA*8] - |.else - | lea CARG2d, [BASE+RA*8] - | lea CARG3d, [BASE+RD*8] - |.endif - | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA. - | movzx CARG4d, PC_OP - |.else - | movzx RB, PC_OP - | lea RD, [BASE+RD*8] - | lea RA, [BASE+RA*8] - | mov ARG4, RB - | mov L:RB, SAVE_L - | mov ARG3, RD - | mov ARG2, RA - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - |3: - | mov BASE, L:RB->base - | cmp RC, 1 - | ja ->vmeta_binop - |4: - | lea PC, [PC+4] - | jb >6 - |5: - | movzx RD, PC_RD - | branchPC RD - |6: - | ins_next - | - |->cont_condt: // BASE = base, RC = result - | add PC, 4 - | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true. - | jb <5 - | jmp <6 - | - |->cont_condf: // BASE = base, RC = result - | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false. - | jmp <4 - | - |->vmeta_equal: - | sub PC, 4 - |.if X64WIN - | mov CARG3d, RD - | mov CARG4d, RB - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d == BASE. - | mov CARG2d, RA - | mov CARG1d, L:RB // Caveat: CARG1d == RA. - |.elif X64 - | mov CARG2d, RA - | mov CARG4d, RB // Caveat: CARG4d == RA. - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG3d == BASE. - | mov CARG3d, RD - | mov CARG1d, L:RB - |.else - | mov ARG4, RB - | mov L:RB, SAVE_L - | mov ARG3, RD - | mov ARG2, RA - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - | jmp <3 - | - |->vmeta_equal_cd: - |.if FFI - | sub PC, 4 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG1, L:RB - | mov FCARG2, dword [PC-4] - | mov SAVE_PC, PC - | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - | jmp <3 - |.endif - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_vno: - |.if DUALNUM - | movzx RB, PC_RB - |.endif - |->vmeta_arith_vn: - | lea RC, [KBASE+RC*8] - | jmp >1 - | - |->vmeta_arith_nvo: - |.if DUALNUM - | movzx RC, PC_RC - |.endif - |->vmeta_arith_nv: - | lea RC, [KBASE+RC*8] - | lea RB, [BASE+RB*8] - | xchg RB, RC - | jmp >2 - | - |->vmeta_unm: - | lea RC, [BASE+RD*8] - | mov RB, RC - | jmp >2 - | - |->vmeta_arith_vvo: - |.if DUALNUM - | movzx RB, PC_RB - |.endif - |->vmeta_arith_vv: - | lea RC, [BASE+RC*8] - |1: - | lea RB, [BASE+RB*8] - |2: - | lea RA, [BASE+RA*8] - |.if X64WIN - | mov CARG3d, RB - | mov CARG4d, RC - | movzx RC, PC_OP - | mov ARG5d, RC - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d == BASE. - | mov CARG2d, RA - | mov CARG1d, L:RB // Caveat: CARG1d == RA. - |.elif X64 - | movzx CARG5d, PC_OP - | mov CARG2d, RA - | mov CARG4d, RC // Caveat: CARG4d == RA. - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE. - | mov CARG3d, RB - | mov L:RB, L:CARG1d - |.else - | mov ARG3, RB - | mov L:RB, SAVE_L - | mov ARG4, RC - | movzx RC, PC_OP - | mov ARG2, RA - | mov ARG5, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // NULL (finished) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = base, RC = new base, stack = cont/func/o1/o2 - | mov RA, RC - | sub RC, BASE - | mov [RA-12], PC // [cont|PC] - | lea PC, [RC+FRAME_CONT] - | mov NARGS:RD, 2+1 // 2 args for func(o1, o2). - | jmp ->vm_call_dispatch - | - |->vmeta_len: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB - | mov SAVE_PC, PC - | call extern lj_meta_len@8 // (lua_State *L, TValue *o) - | // NULL (retry) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base -#if LJ_52 - | test RC, RC - | jne ->vmeta_binop // Binop call for compatibility. - | movzx RD, PC_RD - | mov TAB:FCARG1, [BASE+RD*8] - | jmp ->BC_LEN_Z -#else - | jmp ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call_ra: - | lea RA, [BASE+RA*8+8] - |->vmeta_call: // Resolve and call __call metamethod. - | // BASE = old base, RA = new base, RC = nargs+1, PC = return - | mov TMP2, RA // Save RA, RC for us. - | mov TMP1, NARGS:RD - | sub RA, 8 - |.if X64 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, RA - | lea CARG3d, [RA+NARGS:RD*8] - | mov CARG1d, L:RB // Caveat: CARG1d may be RA. - |.else - | lea RC, [RA+NARGS:RD*8] - | mov L:RB, SAVE_L - | mov ARG2, RA - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE // This is the callers base! - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | mov BASE, L:RB->base - | mov RA, TMP2 - | mov NARGS:RD, TMP1 - | mov LFUNC:RB, [RA-8] - | add NARGS:RD, 1 - | // This is fragile. L->base must not move, KBASE must always be defined. - | cmp KBASE, BASE // Continue with CALLT if flag set. - | je ->BC_CALLT_Z - | mov BASE, RA - | ins_call // Otherwise call resolved metamethod. - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG2, RA // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA - | mov SAVE_PC, PC - | call extern lj_meta_for@8 // (lua_State *L, TValue *base) - | mov BASE, L:RB->base - | mov RC, [PC-4] - | movzx RA, RCH - | movzx OP, RCL - | shr RC, 16 - |.if X64 - | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI. - |.else - | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI. - |.endif - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | cmp NARGS:RD, 1+1; jb ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | cmp NARGS:RD, 2+1; jb ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name - | .ffunc_1 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | fld qword [BASE] - |.endmacro - | - |.macro .ffunc_n, name, op - | .ffunc_1 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | op - | fld qword [BASE] - |.endmacro - | - |.macro .ffunc_nsse, name, op - | .ffunc_1 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | op xmm0, qword [BASE] - |.endmacro - | - |.macro .ffunc_nsse, name - | .ffunc_nsse name, movsd - |.endmacro - | - |.macro .ffunc_nn, name - | .ffunc_2 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback - | fld qword [BASE] - | fld qword [BASE+8] - |.endmacro - | - |.macro .ffunc_nnsse, name - | .ffunc_2 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback - | movsd xmm0, qword [BASE] - | movsd xmm1, qword [BASE+8] - |.endmacro - | - |.macro .ffunc_nnr, name - | .ffunc_2 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback - | fld qword [BASE+8] - | fld qword [BASE] - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses label 1. - |.macro ffgccheck - | mov RB, [DISPATCH+DISPATCH_GL(gc.total)] - | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)] - | jb >1 - | call ->fff_gcstep - |1: - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | mov RB, [BASE+4] - | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback - | mov PC, [BASE-4] - | mov MULTRES, RD - | mov [BASE-4], RB - | mov RB, [BASE] - | mov [BASE-8], RB - | sub RD, 2 - | jz >2 - | mov RA, BASE - |1: - | add RA, 8 - |.if X64 - | mov RBa, [RA] - | mov [RA-8], RBa - |.else - | mov RB, [RA+4] - | mov [RA-4], RB - | mov RB, [RA] - | mov [RA-8], RB - |.endif - | sub RD, 1 - | jnz <1 - |2: - | mov RD, MULTRES - | jmp ->fff_res_ - | - |.ffunc_1 type - | mov RB, [BASE+4] - |.if X64 - | mov RA, RB - | sar RA, 15 - | cmp RA, -2 - | je >3 - |.endif - | mov RC, ~LJ_TNUMX - | not RB - | cmp RC, RB - | cmova RC, RB - |2: - | mov CFUNC:RB, [BASE-8] - | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))] - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TSTR - | mov [BASE-8], STR:RC - | jmp ->fff_res1 - |.if X64 - |3: - | mov RC, ~LJ_TLIGHTUD - | jmp <2 - |.endif - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | mov RB, [BASE+4] - | mov PC, [BASE-4] - | cmp RB, LJ_TTAB; jne >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | mov TAB:RB, [BASE] - | mov TAB:RB, TAB:RB->metatable - |2: - | test TAB:RB, TAB:RB - | mov dword [BASE-4], LJ_TNIL - | jz ->fff_res1 - | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)] - | mov dword [BASE-4], LJ_TTAB // Store metatable as default result. - | mov [BASE-8], TAB:RB - | mov RA, TAB:RB->hmask - | and RA, STR:RC->hash - | imul RA, #NODE - | add NODE:RA, TAB:RB->node - |3: // Rearranged logic, because we expect _not_ to find the key. - | cmp dword NODE:RA->key.it, LJ_TSTR - | jne >4 - | cmp dword NODE:RA->key.gcr, STR:RC - | je >5 - |4: - | mov NODE:RA, NODE:RA->next - | test NODE:RA, NODE:RA - | jnz <3 - | jmp ->fff_res1 // Not found, keep default result. - |5: - | mov RB, [RA+4] - | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value. - | mov RC, [RA] - | mov [BASE-4], RB // Return value of mt.__metatable. - | mov [BASE-8], RC - | jmp ->fff_res1 - | - |6: - | cmp RB, LJ_TUDATA; je <1 - |.if X64 - | cmp RB, LJ_TNUMX; ja >8 - | cmp RB, LJ_TISNUM; jbe >7 - | mov RB, LJ_TLIGHTUD - | jmp >8 - |7: - |.else - | cmp RB, LJ_TISNUM; ja >8 - |.endif - | mov RB, LJ_TNUMX - |8: - | not RB - | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])] - | jmp <2 - | - |.ffunc_2 setmetatable - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | // Fast path: no mt for table yet and not clearing the mt. - | mov TAB:RB, [BASE] - | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback - | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback - | mov TAB:RC, [BASE+8] - | mov TAB:RB->metatable, TAB:RC - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TTAB // Return original table. - | mov [BASE-8], TAB:RB - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jz >1 - | // Possible write barrier. Table is black, but skip iswhite(mt) check. - | barrierback TAB:RB, RC - |1: - | jmp ->fff_res1 - | - |.ffunc_2 rawget - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - |.if X64WIN - | mov RB, BASE // Save BASE. - | lea CARG3d, [BASE+8] - | mov CARG2d, [BASE] // Caveat: CARG2d == BASE. - | mov CARG1d, SAVE_L - |.elif X64 - | mov RB, BASE // Save BASE. - | mov CARG2d, [BASE] - | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE. - | mov CARG1d, SAVE_L - |.else - | mov TAB:RD, [BASE] - | mov L:RB, SAVE_L - | mov ARG2, TAB:RD - | mov ARG1, L:RB - | mov RB, BASE // Save BASE. - | add BASE, 8 - | mov ARG3, BASE - |.endif - | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // cTValue * returned in eax (RD). - | mov BASE, RB // Restore BASE. - | // Copy table slot. - |.if X64 - | mov RBa, [RD] - | mov PC, [BASE-4] - | mov [BASE-8], RBa - |.else - | mov RB, [RD] - | mov RD, [RD+4] - | mov PC, [BASE-4] - | mov [BASE-8], RB - | mov [BASE-4], RD - |.endif - | jmp ->fff_res1 - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument. - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne >1 - | mov RB, dword [BASE]; jmp ->fff_resi - |1: - | ja ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - |.if SSE - | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0 - |.else - | fld qword [BASE]; jmp ->fff_resn - |.endif - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | mov PC, [BASE-4] - | cmp dword [BASE+4], LJ_TSTR; jne >3 - | // A __tostring method in the string base metatable is ignored. - | mov STR:RD, [BASE] - |2: - | mov dword [BASE-4], LJ_TSTR - | mov [BASE-8], STR:RD - | jmp ->fff_res1 - |3: // Handle numbers inline, unless a number base metatable is present. - | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback - | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0 - | jne ->fff_fallback - | ffgccheck // Caveat: uses label 1. - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Add frame since C call can throw. - | mov SAVE_PC, PC // Redundant (but a defined value). - |.if X64 and not X64WIN - | mov FCARG2, BASE // Otherwise: FCARG2 == BASE - |.endif - | mov L:FCARG1, L:RB - |.if DUALNUM - | call extern lj_str_fromnumber@8 // (lua_State *L, cTValue *o) - |.else - | call extern lj_str_fromnum@8 // (lua_State *L, lua_Number *np) - |.endif - | // GCstr returned in eax (RD). - | mov BASE, L:RB->base - | jmp <2 - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | je >2 // Missing 2nd arg? - |1: - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Add frame since C call can throw. - | mov L:RB->top, BASE // Dummy frame length is ok. - | mov PC, [BASE-4] - |.if X64WIN - | lea CARG3d, [BASE+8] - | mov CARG2d, [BASE] // Caveat: CARG2d == BASE. - | mov CARG1d, L:RB - |.elif X64 - | mov CARG2d, [BASE] - | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE. - | mov CARG1d, L:RB - |.else - | mov TAB:RD, [BASE] - | mov ARG2, TAB:RD - | mov ARG1, L:RB - | add BASE, 8 - | mov ARG3, BASE - |.endif - | mov SAVE_PC, PC // Needed for ITERN fallback. - | call extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key) - | // Flag returned in eax (RD). - | mov BASE, L:RB->base - | test RD, RD; jz >3 // End of traversal? - | // Copy key and value to results. - |.if X64 - | mov RBa, [BASE+8] - | mov RDa, [BASE+16] - | mov [BASE-8], RBa - | mov [BASE], RDa - |.else - | mov RB, [BASE+8] - | mov RD, [BASE+12] - | mov [BASE-8], RB - | mov [BASE-4], RD - | mov RB, [BASE+16] - | mov RD, [BASE+20] - | mov [BASE], RB - | mov [BASE+4], RD - |.endif - |->fff_res2: - | mov RD, 1+2 - | jmp ->fff_res - |2: // Set missing 2nd arg to nil. - | mov dword [BASE+12], LJ_TNIL - | jmp <1 - |3: // End of traversal: return nil. - | mov dword [BASE-4], LJ_TNIL - | jmp ->fff_res1 - | - |.ffunc_1 pairs - | mov TAB:RB, [BASE] - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback -#if LJ_52 - | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback -#endif - | mov CFUNC:RB, [BASE-8] - | mov CFUNC:RD, CFUNC:RB->upvalue[0] - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TFUNC - | mov [BASE-8], CFUNC:RD - | mov dword [BASE+12], LJ_TNIL - | mov RD, 1+3 - | jmp ->fff_res - | - |.ffunc_1 ipairs_aux - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | mov PC, [BASE-4] - |.if DUALNUM - | mov RD, dword [BASE+8] - | add RD, 1 - | mov dword [BASE-4], LJ_TISNUM - | mov dword [BASE-8], RD - |.elif SSE - | movsd xmm0, qword [BASE+8] - | sseconst_1 xmm1, RBa - | addsd xmm0, xmm1 - | cvtsd2si RD, xmm0 - | movsd qword [BASE-8], xmm0 - |.else - | fld qword [BASE+8] - | fld1 - | faddp st1 - | fist ARG1 - | fstp qword [BASE-8] - | mov RD, ARG1 - |.endif - | mov TAB:RB, [BASE] - | cmp RD, TAB:RB->asize; jae >2 // Not in array part? - | shl RD, 3 - | add RD, TAB:RB->array - |1: - | cmp dword [RD+4], LJ_TNIL; je ->fff_res0 - | // Copy array slot. - |.if X64 - | mov RBa, [RD] - | mov [BASE], RBa - |.else - | mov RB, [RD] - | mov RD, [RD+4] - | mov [BASE], RB - | mov [BASE+4], RD - |.endif - | jmp ->fff_res2 - |2: // Check for empty hash part first. Otherwise call C function. - | cmp dword TAB:RB->hmask, 0; je ->fff_res0 - | mov FCARG1, TAB:RB - | mov RB, BASE // Save BASE. - | mov FCARG2, RD // Caveat: FCARG2 == BASE - | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key) - | // cTValue * or NULL returned in eax (RD). - | mov BASE, RB - | test RD, RD - | jnz <1 - |->fff_res0: - | mov RD, 1+0 - | jmp ->fff_res - | - |.ffunc_1 ipairs - | mov TAB:RB, [BASE] - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback -#if LJ_52 - | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback -#endif - | mov CFUNC:RB, [BASE-8] - | mov CFUNC:RD, CFUNC:RB->upvalue[0] - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TFUNC - | mov [BASE-8], CFUNC:RD - |.if DUALNUM - | mov dword [BASE+12], LJ_TISNUM - | mov dword [BASE+8], 0 - |.elif SSE - | xorps xmm0, xmm0 - | movsd qword [BASE+8], xmm0 - |.else - | fldz - | fstp qword [BASE+8] - |.endif - | mov RD, 1+3 - | jmp ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc_1 pcall - | lea RA, [BASE+8] - | sub NARGS:RD, 1 - | mov PC, 8+FRAME_PCALL - |1: - | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)] - | shr RB, HOOK_ACTIVE_SHIFT - | and RB, 1 - | add PC, RB // Remember active hook before pcall. - | jmp ->vm_call_dispatch - | - |.ffunc_2 xpcall - | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback - | mov RB, [BASE+4] // Swap function and traceback. - | mov [BASE+12], RB - | mov dword [BASE+4], LJ_TFUNC - | mov LFUNC:RB, [BASE] - | mov PC, [BASE+8] - | mov [BASE+8], LFUNC:RB - | mov [BASE], PC - | lea RA, [BASE+16] - | sub NARGS:RD, 2 - | mov PC, 16+FRAME_PCALL - | jmp <1 - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | mov L:RB, [BASE] - |.else - |.ffunc coroutine_wrap_aux - | mov CFUNC:RB, [BASE-8] - | mov L:RB, CFUNC:RB->upvalue[0].gcr - |.endif - | mov PC, [BASE-4] - | mov SAVE_PC, PC - |.if X64 - | mov TMP1, L:RB - |.else - | mov ARG1, L:RB - |.endif - |.if resume - | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback - |.endif - | cmp aword L:RB->cframe, 0; jne ->fff_fallback - | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback - | mov RA, L:RB->top - | je >1 // Status != LUA_YIELD (i.e. 0)? - | cmp RA, L:RB->base // Check for presence of initial func. - | je ->fff_fallback - |1: - |.if resume - | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread). - |.else - | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1). - |.endif - | cmp PC, L:RB->maxstack; ja ->fff_fallback - | mov L:RB->top, PC - | - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - |.if resume - | add BASE, 8 // Keep resumed thread in stack for GC. - |.endif - | mov L:RB->top, BASE - |.if resume - | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move. - |.else - | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move. - |.endif - | sub RBa, PCa // Relative to PC. - | - | cmp PC, RA - | je >3 - |2: // Move args to coroutine. - |.if X64 - | mov RCa, [PC+RB] - | mov [PC-8], RCa - |.else - | mov RC, [PC+RB+4] - | mov [PC-4], RC - | mov RC, [PC+RB] - | mov [PC-8], RC - |.endif - | sub PC, 8 - | cmp PC, RA - | jne <2 - |3: - |.if X64 - | mov CARG2d, RA - | mov CARG1d, TMP1 - |.else - | mov ARG2, RA - | xor RA, RA - | mov ARG4, RA - | mov ARG3, RA - |.endif - | call ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | set_vmstate INTERP - | - | mov L:RB, SAVE_L - |.if X64 - | mov L:PC, TMP1 - |.else - | mov L:PC, ARG1 // The callee doesn't modify SAVE_L. - |.endif - | mov BASE, L:RB->base - | cmp eax, LUA_YIELD - | ja >8 - |4: - | mov RA, L:PC->base - | mov KBASE, L:PC->top - | mov L:PC->top, RA // Clear coroutine stack. - | mov PC, KBASE - | sub PC, RA - | je >6 // No results? - | lea RD, [BASE+PC] - | shr PC, 3 - | cmp RD, L:RB->maxstack - | ja >9 // Need to grow stack? - | - | mov RB, BASE - | sub RBa, RAa - |5: // Move results from coroutine. - |.if X64 - | mov RDa, [RA] - | mov [RA+RB], RDa - |.else - | mov RD, [RA] - | mov [RA+RB], RD - | mov RD, [RA+4] - | mov [RA+RB+4], RD - |.endif - | add RA, 8 - | cmp RA, KBASE - | jne <5 - |6: - |.if resume - | lea RD, [PC+2] // nresults+1 = 1 + true + results. - | mov dword [BASE-4], LJ_TTRUE // Prepend true to results. - |.else - | lea RD, [PC+1] // nresults+1 = 1 + results. - |.endif - |7: - | mov PC, SAVE_PC - | mov MULTRES, RD - |.if resume - | mov RAa, -8 - |.else - | xor RA, RA - |.endif - | test PC, FRAME_TYPE - | jz ->BC_RET_Z - | jmp ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | mov dword [BASE-4], LJ_TFALSE // Prepend false to results. - | mov RA, L:PC->top - | sub RA, 8 - | mov L:PC->top, RA // Clear error from coroutine stack. - | // Copy error message. - |.if X64 - | mov RDa, [RA] - | mov [BASE], RDa - |.else - | mov RD, [RA] - | mov [BASE], RD - | mov RD, [RA+4] - | mov [BASE+4], RD - |.endif - | mov RD, 1+2 // nresults+1 = 1 + false + error. - | jmp <7 - |.else - | mov FCARG2, L:PC - | mov FCARG1, L:RB - | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co) - | // Error function does not return. - |.endif - | - |9: // Handle stack expansion on return from yield. - |.if X64 - | mov L:RA, TMP1 - |.else - | mov L:RA, ARG1 // The callee doesn't modify SAVE_L. - |.endif - | mov L:RA->top, KBASE // Undo coroutine stack clearing. - | mov FCARG2, PC - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - |.if X64 - | mov L:PC, TMP1 - |.else - | mov L:PC, ARG1 - |.endif - | mov BASE, L:RB->base - | jmp <4 // Retry the stack move. - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | mov L:RB, SAVE_L - | test aword L:RB->cframe, CFRAME_RESUME - | jz ->fff_fallback - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB->top, RD - | xor RD, RD - | mov aword L:RB->cframe, RDa - | mov al, LUA_YIELD - | mov byte L:RB->status, al - | jmp ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.if not DUALNUM - |->fff_resi: // Dummy. - |.endif - | - |.if SSE - |->fff_resn: - | mov PC, [BASE-4] - | fstp qword [BASE-8] - | jmp ->fff_res1 - |.endif - | - | .ffunc_1 math_abs - |.if DUALNUM - | cmp dword [BASE+4], LJ_TISNUM; jne >2 - | mov RB, dword [BASE] - | cmp RB, 0; jns ->fff_resi - | neg RB; js >1 - |->fff_resbit: - |->fff_resi: - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TISNUM - | mov dword [BASE-8], RB - | jmp ->fff_res1 - |1: - | mov PC, [BASE-4] - | mov dword [BASE-4], 0x41e00000 // 2^31. - | mov dword [BASE-8], 0 - | jmp ->fff_res1 - |2: - | ja ->fff_fallback - |.else - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - |.endif - | - |.if SSE - | movsd xmm0, qword [BASE] - | sseconst_abs xmm1, RDa - | andps xmm0, xmm1 - |->fff_resxmm0: - | mov PC, [BASE-4] - | movsd qword [BASE-8], xmm0 - | // fallthrough - |.else - | fld qword [BASE] - | fabs - | // fallthrough - |->fff_resxmm0: // Dummy. - |->fff_resn: - | mov PC, [BASE-4] - | fstp qword [BASE-8] - |.endif - | - |->fff_res1: - | mov RD, 1+1 - |->fff_res: - | mov MULTRES, RD - |->fff_res_: - | test PC, FRAME_TYPE - | jnz >7 - |5: - | cmp PC_RB, RDL // More results expected? - | ja >6 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | movzx RA, PC_RA - | not RAa // Note: ~RA = -(RA+1) - | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8 - | ins_next - | - |6: // Fill up results with nil. - | mov dword [BASE+RD*8-12], LJ_TNIL - | add RD, 1 - | jmp <5 - | - |7: // Non-standard return case. - | mov RAa, -8 // Results start at BASE+RA = BASE-8. - | jmp ->vm_return - | - |.macro math_round, func - | .ffunc math_ .. func - |.if DUALNUM - | cmp dword [BASE+4], LJ_TISNUM; jne >1 - | mov RB, dword [BASE]; jmp ->fff_resi - |1: - | ja ->fff_fallback - |.else - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - |.endif - |.if SSE - | movsd xmm0, qword [BASE] - | call ->vm_ .. func - | .if DUALNUM - | cvtsd2si RB, xmm0 - | cmp RB, 0x80000000 - | jne ->fff_resi - | cvtsi2sd xmm1, RB - | ucomisd xmm0, xmm1 - | jp ->fff_resxmm0 - | je ->fff_resi - | .endif - | jmp ->fff_resxmm0 - |.else - | fld qword [BASE] - | call ->vm_ .. func - | .if DUALNUM - | fist ARG1 - | mov RB, ARG1 - | cmp RB, 0x80000000; jne >2 - | fdup - | fild ARG1 - | fcomparepp - | jp ->fff_resn - | jne ->fff_resn - |2: - | fpop - | jmp ->fff_resi - | .else - | jmp ->fff_resn - | .endif - |.endif - |.endmacro - | - | math_round floor - | math_round ceil - | - |.if SSE - |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0 - |.else - |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn - |.endif - | - |.ffunc math_log - | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument. - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | fldln2; fld qword [BASE]; fyl2x; jmp ->fff_resn - | - |.ffunc_n math_log10, fldlg2; fyl2x; jmp ->fff_resn - |.ffunc_n math_exp; call ->vm_exp_x87; jmp ->fff_resn - | - |.ffunc_n math_sin; fsin; jmp ->fff_resn - |.ffunc_n math_cos; fcos; jmp ->fff_resn - |.ffunc_n math_tan; fptan; fpop; jmp ->fff_resn - | - |.ffunc_n math_asin - | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fpatan - | jmp ->fff_resn - |.ffunc_n math_acos - | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fxch; fpatan - | jmp ->fff_resn - |.ffunc_n math_atan; fld1; fpatan; jmp ->fff_resn - | - |.macro math_extern, func - |.if SSE - | .ffunc_nsse math_ .. func - | .if not X64 - | movsd FPARG1, xmm0 - | .endif - |.else - | .ffunc_n math_ .. func - | fstp FPARG1 - |.endif - | mov RB, BASE - | call extern lj_vm_ .. func - | mov BASE, RB - | .if X64 - | jmp ->fff_resxmm0 - | .else - | jmp ->fff_resn - | .endif - |.endmacro - | - | math_extern sinh - | math_extern cosh - | math_extern tanh - | - |->ff_math_deg: - |.if SSE - |.ffunc_nsse math_rad - | mov CFUNC:RB, [BASE-8] - | mulsd xmm0, qword CFUNC:RB->upvalue[0] - | jmp ->fff_resxmm0 - |.else - |.ffunc_n math_rad - | mov CFUNC:RB, [BASE-8] - | fmul qword CFUNC:RB->upvalue[0] - | jmp ->fff_resn - |.endif - | - |.ffunc_nn math_atan2; fpatan; jmp ->fff_resn - |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn - | - |.ffunc_1 math_frexp - | mov RB, [BASE+4] - | cmp RB, LJ_TISNUM; jae ->fff_fallback - | mov PC, [BASE-4] - | mov RC, [BASE] - | mov [BASE-4], RB; mov [BASE-8], RC - | shl RB, 1; cmp RB, 0xffe00000; jae >3 - | or RC, RB; jz >3 - | mov RC, 1022 - | cmp RB, 0x00200000; jb >4 - |1: - | shr RB, 21; sub RB, RC // Extract and unbias exponent. - |.if SSE - | cvtsi2sd xmm0, RB - |.else - | mov TMP1, RB; fild TMP1 - |.endif - | mov RB, [BASE-4] - | and RB, 0x800fffff // Mask off exponent. - | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0. - | mov [BASE-4], RB - |2: - |.if SSE - | movsd qword [BASE], xmm0 - |.else - | fstp qword [BASE] - |.endif - | mov RD, 1+2 - | jmp ->fff_res - |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0. - |.if SSE - | xorps xmm0, xmm0; jmp <2 - |.else - | fldz; jmp <2 - |.endif - |4: // Handle denormals by multiplying with 2^54 and adjusting the bias. - |.if SSE - | movsd xmm0, qword [BASE] - | sseconst_hi xmm1, RBa, 43500000 // 2^54. - | mulsd xmm0, xmm1 - | movsd qword [BASE-8], xmm0 - |.else - | fld qword [BASE] - | mov TMP1, 0x5a800000; fmul TMP1 // x = x*2^54 - | fstp qword [BASE-8] - |.endif - | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1 - | - |.if SSE - |.ffunc_nsse math_modf - |.else - |.ffunc_n math_modf - |.endif - | mov RB, [BASE+4] - | mov PC, [BASE-4] - | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf? - |.if SSE - | movaps xmm4, xmm0 - | call ->vm_trunc - | subsd xmm4, xmm0 - |1: - | movsd qword [BASE-8], xmm0 - | movsd qword [BASE], xmm4 - |.else - | fdup - | call ->vm_trunc - | fsub st1, st0 - |1: - | fstp qword [BASE-8] - | fstp qword [BASE] - |.endif - | mov RC, [BASE-4]; mov RB, [BASE+4] - | xor RC, RB; js >3 // Need to adjust sign? - |2: - | mov RD, 1+2 - | jmp ->fff_res - |3: - | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction. - | jmp <2 - |4: - |.if SSE - | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0. - |.else - | fldz; fxch; jmp <1 // Return +-Inf and +-0. - |.endif - | - |.ffunc_nnr math_fmod - |1: ; fprem; fnstsw ax; sahf; jp <1 - | fpop1 - | jmp ->fff_resn - | - |.if SSE - |.ffunc_nnsse math_pow; call ->vm_pow; jmp ->fff_resxmm0 - |.else - |.ffunc_nn math_pow; call ->vm_pow; jmp ->fff_resn - |.endif - | - |.macro math_minmax, name, cmovop, fcmovop, sseop - | .ffunc name - | mov RA, 2 - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne >4 - | mov RB, dword [BASE] - |1: // Handle integers. - | cmp RA, RD; jae ->fff_resi - | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3 - | cmp RB, dword [BASE+RA*8-8] - | cmovop RB, dword [BASE+RA*8-8] - | add RA, 1 - | jmp <1 - |3: - | ja ->fff_fallback - | // Convert intermediate result to number and continue below. - |.if SSE - | cvtsi2sd xmm0, RB - |.else - | mov TMP1, RB - | fild TMP1 - |.endif - | jmp >6 - |4: - | ja ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | - |.if SSE - | movsd xmm0, qword [BASE] - |5: // Handle numbers or integers. - | cmp RA, RD; jae ->fff_resxmm0 - | cmp dword [BASE+RA*8-4], LJ_TISNUM - |.if DUALNUM - | jb >6 - | ja ->fff_fallback - | cvtsi2sd xmm1, dword [BASE+RA*8-8] - | jmp >7 - |.else - | jae ->fff_fallback - |.endif - |6: - | movsd xmm1, qword [BASE+RA*8-8] - |7: - | sseop xmm0, xmm1 - | add RA, 1 - | jmp <5 - |.else - | fld qword [BASE] - |5: // Handle numbers or integers. - | cmp RA, RD; jae ->fff_resn - | cmp dword [BASE+RA*8-4], LJ_TISNUM - |.if DUALNUM - | jb >6 - | ja >9 - | fild dword [BASE+RA*8-8] - | jmp >7 - |.else - | jae >9 - |.endif - |6: - | fld qword [BASE+RA*8-8] - |7: - | fucomi st1; fcmovop st1; fpop1 - | add RA, 1 - | jmp <5 - |.endif - |.endmacro - | - | math_minmax math_min, cmovg, fcmovnbe, minsd - | math_minmax math_max, cmovl, fcmovbe, maxsd - |.if not SSE - |9: - | fpop; jmp ->fff_fallback - |.endif - | - |//-- String library ----------------------------------------------------- - | - |.ffunc_1 string_len - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | mov STR:RB, [BASE] - |.if DUALNUM - | mov RB, dword STR:RB->len; jmp ->fff_resi - |.elif SSE - | cvtsi2sd xmm0, dword STR:RB->len; jmp ->fff_resxmm0 - |.else - | fild dword STR:RB->len; jmp ->fff_resn - |.endif - | - |.ffunc string_byte // Only handle the 1-arg case here. - | cmp NARGS:RD, 1+1; jne ->fff_fallback - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | mov STR:RB, [BASE] - | mov PC, [BASE-4] - | cmp dword STR:RB->len, 1 - | jb ->fff_res0 // Return no results for empty string. - | movzx RB, byte STR:RB[1] - |.if DUALNUM - | jmp ->fff_resi - |.elif SSE - | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0 - |.else - | mov TMP1, RB; fild TMP1; jmp ->fff_resn - |.endif - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg. - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - | mov RB, dword [BASE] - | cmp RB, 255; ja ->fff_fallback - | mov TMP2, RB - |.elif SSE - | jae ->fff_fallback - | cvttsd2si RB, qword [BASE] - | cmp RB, 255; ja ->fff_fallback - | mov TMP2, RB - |.else - | jae ->fff_fallback - | fld qword [BASE] - | fistp TMP2 - | cmp TMP2, 255; ja ->fff_fallback - |.endif - |.if X64 - | mov TMP3, 1 - |.else - | mov ARG3, 1 - |.endif - | lea RDa, TMP2 // Points to stack. Little-endian. - |->fff_newstr: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - |.if X64 - | mov CARG3d, TMP3 // Zero-extended to size_t. - | mov CARG2, RDa // May be 64 bit ptr to stack. - | mov CARG1d, L:RB - |.else - | mov ARG2, RD - | mov ARG1, L:RB - |.endif - | mov SAVE_PC, PC - | call extern lj_str_new // (lua_State *L, char *str, size_t l) - | // GCstr * returned in eax (RD). - | mov BASE, L:RB->base - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TSTR - | mov [BASE-8], STR:RD - | jmp ->fff_res1 - | - |.ffunc string_sub - | ffgccheck - | mov TMP2, -1 - | cmp NARGS:RD, 1+2; jb ->fff_fallback - | jna >1 - | cmp dword [BASE+20], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - | mov RB, dword [BASE+16] - | mov TMP2, RB - |.elif SSE - | jae ->fff_fallback - | cvttsd2si RB, qword [BASE+16] - | mov TMP2, RB - |.else - | jae ->fff_fallback - | fld qword [BASE+16] - | fistp TMP2 - |.endif - |1: - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | mov STR:RB, [BASE] - | mov TMP3, STR:RB - | mov RB, STR:RB->len - |.if DUALNUM - | mov RA, dword [BASE+8] - |.elif SSE - | cvttsd2si RA, qword [BASE+8] - |.else - | fld qword [BASE+8] - | fistp ARG3 - | mov RA, ARG3 - |.endif - | mov RC, TMP2 - | cmp RB, RC // len < end? (unsigned compare) - | jb >5 - |2: - | test RA, RA // start <= 0? - | jle >7 - |3: - | mov STR:RB, TMP3 - | sub RC, RA // start > end? - | jl ->fff_emptystr - | lea RB, [STR:RB+RA+#STR-1] - | add RC, 1 - |4: - |.if X64 - | mov TMP3, RC - |.else - | mov ARG3, RC - |.endif - | mov RD, RB - | jmp ->fff_newstr - | - |5: // Negative end or overflow. - | jl >6 - | lea RC, [RC+RB+1] // end = end+(len+1) - | jmp <2 - |6: // Overflow. - | mov RC, RB // end = len - | jmp <2 - | - |7: // Negative start or underflow. - | je >8 - | add RA, RB // start = start+(len+1) - | add RA, 1 - | jg <3 // start > 0? - |8: // Underflow. - | mov RA, 1 // start = 1 - | jmp <3 - | - |->fff_emptystr: // Range underflow. - | xor RC, RC // Zero length. Any ptr in RB is ok. - | jmp <4 - | - |.ffunc string_rep // Only handle the 1-char case inline. - | ffgccheck - | cmp NARGS:RD, 2+1; jne ->fff_fallback // Exactly 2 arguments. - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM - | mov STR:RB, [BASE] - |.if DUALNUM - | jne ->fff_fallback - | mov RC, dword [BASE+8] - |.elif SSE - | jae ->fff_fallback - | cvttsd2si RC, qword [BASE+8] - |.else - | jae ->fff_fallback - | fld qword [BASE+8] - | fistp TMP2 - | mov RC, TMP2 - |.endif - | test RC, RC - | jle ->fff_emptystr // Count <= 0? (or non-int) - | cmp dword STR:RB->len, 1 - | jb ->fff_emptystr // Zero length string? - | jne ->fff_fallback_2 // Fallback for > 1-char strings. - | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_2 - | movzx RA, byte STR:RB[1] - | mov RB, [DISPATCH+DISPATCH_GL(tmpbuf.buf)] - |.if X64 - | mov TMP3, RC - |.else - | mov ARG3, RC - |.endif - |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?). - | mov [RB], RAL - | add RB, 1 - | sub RC, 1 - | jnz <1 - | mov RD, [DISPATCH+DISPATCH_GL(tmpbuf.buf)] - | jmp ->fff_newstr - | - |.ffunc_1 string_reverse - | ffgccheck - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | mov STR:RB, [BASE] - | mov RC, STR:RB->len - | test RC, RC - | jz ->fff_emptystr // Zero length string? - | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1 - | add RB, #STR - | mov TMP2, PC // Need another temp register. - |.if X64 - | mov TMP3, RC - |.else - | mov ARG3, RC - |.endif - | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)] - |1: - | movzx RA, byte [RB] - | add RB, 1 - | sub RC, 1 - | mov [PC+RC], RAL - | jnz <1 - | mov RD, PC - | mov PC, TMP2 - | jmp ->fff_newstr - | - |.macro ffstring_case, name, lo, hi - | .ffunc_1 name - | ffgccheck - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | mov STR:RB, [BASE] - | mov RC, STR:RB->len - | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1 - | add RB, #STR - | mov TMP2, PC // Need another temp register. - |.if X64 - | mov TMP3, RC - |.else - | mov ARG3, RC - |.endif - | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)] - | jmp >3 - |1: // ASCII case conversion. Yes, this is suboptimal code (do you care?). - | movzx RA, byte [RB+RC] - | cmp RA, lo - | jb >2 - | cmp RA, hi - | ja >2 - | xor RA, 0x20 - |2: - | mov [PC+RC], RAL - |3: - | sub RC, 1 - | jns <1 - | mov RD, PC - | mov PC, TMP2 - | jmp ->fff_newstr - |.endmacro - | - |ffstring_case string_lower, 0x41, 0x5a - |ffstring_case string_upper, 0x61, 0x7a - | - |//-- Table library ------------------------------------------------------ - | - |.ffunc_1 table_getn - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | mov RB, BASE // Save BASE. - | mov TAB:FCARG1, [BASE] - | call extern lj_tab_len@4 // LJ_FASTCALL (GCtab *t) - | // Length of table returned in eax (RD). - | mov BASE, RB // Restore BASE. - |.if DUALNUM - | mov RB, RD; jmp ->fff_resi - |.elif SSE - | cvtsi2sd xmm0, RD; jmp ->fff_resxmm0 - |.else - | mov ARG1, RD; fild ARG1; jmp ->fff_resn - |.endif - | - |//-- Bit library -------------------------------------------------------- - | - |.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!). - | - |.macro .ffunc_bit, name, kind - | .ffunc_1 name - |.if kind == 2 - |.if SSE - | sseconst_tobit xmm1, RBa - |.else - | mov TMP1, TOBIT_BIAS - |.endif - |.endif - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne >1 - | mov RB, dword [BASE] - |.if kind > 0 - | jmp >2 - |.else - | jmp ->fff_resbit - |.endif - |1: - | ja ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - |.if SSE - | movsd xmm0, qword [BASE] - |.if kind < 2 - | sseconst_tobit xmm1, RBa - |.endif - | addsd xmm0, xmm1 - | movd RB, xmm0 - |.else - | fld qword [BASE] - |.if kind < 2 - | mov TMP1, TOBIT_BIAS - |.endif - | fadd TMP1 - | fstp FPARG1 - |.if kind > 0 - | mov RB, ARG1 - |.endif - |.endif - |2: - |.endmacro - | - |.ffunc_bit bit_tobit, 0 - |.if DUALNUM or SSE - |.if not SSE - | mov RB, ARG1 - |.endif - | jmp ->fff_resbit - |.else - | fild ARG1 - | jmp ->fff_resn - |.endif - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name, 2 - | mov TMP2, NARGS:RD // Save for fallback. - | lea RD, [BASE+NARGS:RD*8-16] - |1: - | cmp RD, BASE - | jbe ->fff_resbit - | cmp dword [RD+4], LJ_TISNUM - |.if DUALNUM - | jne >2 - | ins RB, dword [RD] - | sub RD, 8 - | jmp <1 - |2: - | ja ->fff_fallback_bit_op - |.else - | jae ->fff_fallback_bit_op - |.endif - |.if SSE - | movsd xmm0, qword [RD] - | addsd xmm0, xmm1 - | movd RA, xmm0 - | ins RB, RA - |.else - | fld qword [RD] - | fadd TMP1 - | fstp FPARG1 - | ins RB, ARG1 - |.endif - | sub RD, 8 - | jmp <1 - |.endmacro - | - |.ffunc_bit_op bit_band, and - |.ffunc_bit_op bit_bor, or - |.ffunc_bit_op bit_bxor, xor - | - |.ffunc_bit bit_bswap, 1 - | bswap RB - | jmp ->fff_resbit - | - |.ffunc_bit bit_bnot, 1 - | not RB - |.if DUALNUM - | jmp ->fff_resbit - |.elif SSE - |->fff_resbit: - | cvtsi2sd xmm0, RB - | jmp ->fff_resxmm0 - |.else - |->fff_resbit: - | mov ARG1, RB - | fild ARG1 - | jmp ->fff_resn - |.endif - | - |->fff_fallback_bit_op: - | mov NARGS:RD, TMP2 // Restore for fallback - | jmp ->fff_fallback - | - |.macro .ffunc_bit_sh, name, ins - |.if DUALNUM - | .ffunc_bit name, 1 - | // Note: no inline conversion from number for 2nd argument! - | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback - | mov RA, dword [BASE+8] - |.elif SSE - | .ffunc_nnsse name - | sseconst_tobit xmm2, RBa - | addsd xmm0, xmm2 - | addsd xmm1, xmm2 - | movd RB, xmm0 - | movd RA, xmm1 - |.else - | .ffunc_nn name - | mov TMP1, TOBIT_BIAS - | fadd TMP1 - | fstp FPARG3 - | fadd TMP1 - | fstp FPARG1 - | mov RA, ARG3 - | mov RB, ARG1 - |.endif - | ins RB, cl // Assumes RA is ecx. - | jmp ->fff_resbit - |.endmacro - | - |.ffunc_bit_sh bit_lshift, shl - |.ffunc_bit_sh bit_rshift, shr - |.ffunc_bit_sh bit_arshift, sar - |.ffunc_bit_sh bit_rol, rol - |.ffunc_bit_sh bit_ror, ror - | - |//----------------------------------------------------------------------- - | - |->fff_fallback_2: - | mov NARGS:RD, 1+2 // Other args are ignored, anyway. - | jmp ->fff_fallback - |->fff_fallback_1: - | mov NARGS:RD, 1+1 // Other args are ignored, anyway. - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RD = nargs+1 - | mov L:RB, SAVE_L - | mov PC, [BASE-4] // Fallback may overwrite PC. - | mov SAVE_PC, PC // Redundant (but a defined value). - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler. - | mov L:RB->top, RD - | mov CFUNC:RD, [BASE-8] - | cmp RA, L:RB->maxstack - | ja >5 // Need to grow stack. - |.if X64 - | mov CARG1d, L:RB - |.else - | mov ARG1, L:RB - |.endif - | call aword CFUNC:RD->f // (lua_State *L) - | mov BASE, L:RB->base - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | test RD, RD; jg ->fff_res // Returned nresults+1? - |1: - | mov RA, L:RB->top - | sub RA, BASE - | shr RA, 3 - | test RD, RD - | lea NARGS:RD, [RA+1] - | mov LFUNC:RB, [BASE-8] - | jne ->vm_call_tail // Returned -1? - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | mov RA, BASE - | test PC, FRAME_TYPE - | jnz >3 - | movzx RB, PC_RA - | not RBa // Note: ~RB = -(RB+1) - | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8 - | jmp ->vm_call_dispatch // Resolve again for tailcall. - |3: - | mov RB, PC - | and RB, -8 - | sub BASE, RB - | jmp ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | mov FCARG2, LUA_MINSTACK - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->base - | xor RD, RD // Simulate a return 0. - | jmp <1 // Dumb retry (goes through ff first). - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RD = nargs+1 - | pop RBa // Must keep stack at same level. - | mov TMPa, RBa // Save return address - | mov L:RB, SAVE_L - | mov SAVE_PC, PC // Redundant (but a defined value). - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | mov FCARG1, L:RB - | mov L:RB->top, RD - | call extern lj_gc_step@4 // (lua_State *L) - | mov BASE, L:RB->base - | mov RD, L:RB->top - | sub RD, BASE - | shr RD, 3 - | add NARGS:RD, 1 - | mov RBa, TMPa - | push RBa // Restore return address. - | ret - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_VMEVENT // No recording while in vmevent. - | jnz >5 - | // Decrement the hookcount for consistency, but always do the call. - | test RDL, HOOK_ACTIVE - | jnz >1 - | test RDL, LUA_MASKLINE|LUA_MASKCOUNT - | jz >1 - | dec dword [DISPATCH+DISPATCH_GL(hookcount)] - | jmp >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_ACTIVE // Hook already active? - | jnz >5 - | jmp >1 - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_ACTIVE // Hook already active? - | jnz >5 - | - | test RDL, LUA_MASKLINE|LUA_MASKCOUNT - | jz >5 - | dec dword [DISPATCH+DISPATCH_GL(hookcount)] - | jz >1 - | test RDL, LUA_MASKLINE - | jz >5 - |1: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG2, PC // Caveat: FCARG2 == BASE - | mov FCARG1, L:RB - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | call extern lj_dispatch_ins@8 // (lua_State *L, BCIns *pc) - |3: - | mov BASE, L:RB->base - |4: - | movzx RA, PC_RA - |5: - | movzx OP, PC_OP - | movzx RD, PC_RD - |.if X64 - | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins. - |.else - | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins. - |.endif - | - |->cont_hook: // Continue from hook yield. - | add PC, 4 - | mov RA, [RB-24] - | mov MULTRES, RA // Restore MULTRES for *M ins. - | jmp <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L). - | mov RB, LFUNC:RB->pc - | movzx RD, byte [RB+PC2PROTO(framesize)] - | lea RD, [BASE+RD*8] - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov FCARG2, PC - | lea FCARG1, [DISPATCH+GG_DISP2J] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa - | mov SAVE_PC, PC - | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc) - | jmp <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mov SAVE_PC, PC - |.if JIT - | jmp >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | mov SAVE_PC, PC - | or PC, 1 // Marker for hot call. - |1: - |.endif - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov FCARG2, PC - | mov FCARG1, L:RB - | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc) - | // ASMFunction returned in eax/rax (RDa). - | mov SAVE_PC, 0 // Invalidate for subsequent line hook. - |.if JIT - | and PC, -2 - |.endif - | mov BASE, L:RB->base - | mov RAa, RDa - | mov RD, L:RB->top - | sub RD, BASE - | mov RBa, RAa - | movzx RA, PC_RA - | shr RD, 3 - | add NARGS:RD, 1 - | jmp RBa - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Called from an exit stub with the exit number on the stack. - |// The 16 bit exit number is stored with two (sign-extended) push imm8. - |->vm_exit_handler: - |.if JIT - |.if X64 - | push r13; push r12 - | push r11; push r10; push r9; push r8 - | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp - | push rbx; push rdx; push rcx; push rax - | movzx RC, byte [rbp-8] // Reconstruct exit number. - | mov RCH, byte [rbp-16] - | mov [rbp-8], r15; mov [rbp-16], r14 - |.else - | push ebp; lea ebp, [esp+12]; push ebp - | push ebx; push edx; push ecx; push eax - | movzx RC, byte [ebp-4] // Reconstruct exit number. - | mov RCH, byte [ebp-8] - | mov [ebp-4], edi; mov [ebp-8], esi - |.endif - | // Caveat: DISPATCH is ebx. - | mov DISPATCH, [ebp] - | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number. - | set_vmstate EXIT - | mov [DISPATCH+DISPATCH_J(exitno)], RC - | mov [DISPATCH+DISPATCH_J(parent)], RA - |.if X64 - |.if X64WIN - | sub rsp, 16*8+4*8 // Room for SSE regs + save area. - |.else - | sub rsp, 16*8 // Room for SSE regs. - |.endif - | add rbp, -128 - | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14 - | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12 - | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10 - | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8 - | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6 - | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4 - | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2 - | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0 - |.else - | sub esp, 8*8+16 // Room for SSE regs + args. - | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6 - | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4 - | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2 - | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0 - |.endif - | // Caveat: RB is ebp. - | mov L:RB, [DISPATCH+DISPATCH_GL(jit_L)] - | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa - | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0 - | mov L:RB->base, BASE - |.if X64WIN - | lea CARG2, [rsp+4*8] - |.elif X64 - | mov CARG2, rsp - |.else - | lea FCARG2, [esp+16] - |.endif - | lea FCARG1, [DISPATCH+GG_DISP2J] - | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex) - | // MULTRES or negated error code returned in eax (RD). - | mov RAa, L:RB->cframe - | and RAa, CFRAME_RAWMASK - |.if X64WIN - | // Reposition stack later. - |.elif X64 - | mov rsp, RAa // Reposition stack to C frame. - |.else - | mov esp, RAa // Reposition stack to C frame. - |.endif - | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield). - | mov BASE, L:RB->base - | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC. - |.if X64 - | jmp >1 - |.endif - |.endif - |->vm_exit_interp: - | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set. - |.if JIT - |.if X64 - | // Restore additional callee-save registers only used in compiled code. - |.if X64WIN - | lea RAa, [rsp+9*16+4*8] - |1: - | movdqa xmm15, [RAa-9*16] - | movdqa xmm14, [RAa-8*16] - | movdqa xmm13, [RAa-7*16] - | movdqa xmm12, [RAa-6*16] - | movdqa xmm11, [RAa-5*16] - | movdqa xmm10, [RAa-4*16] - | movdqa xmm9, [RAa-3*16] - | movdqa xmm8, [RAa-2*16] - | movdqa xmm7, [RAa-1*16] - | mov rsp, RAa // Reposition stack to C frame. - | movdqa xmm6, [RAa] - | mov r15, CSAVE_3 - | mov r14, CSAVE_4 - |.else - | add rsp, 16 // Reposition stack to C frame. - |1: - |.endif - | mov r13, TMPa - | mov r12, TMPQ - |.endif - | test RD, RD; js >3 // Check for error from exit. - | mov MULTRES, RD - | mov LFUNC:KBASE, [BASE-8] - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0 - | set_vmstate INTERP - | // Modified copy of ins_next which handles function header dispatch, too. - | mov RC, [PC] - | movzx RA, RCH - | movzx OP, RCL - | add PC, 4 - | shr RC, 16 - | cmp OP, BC_FUNCF // Function header? - | jb >2 - | mov RC, MULTRES // RC/RD holds nres+1. - |2: - |.if X64 - | jmp aword [DISPATCH+OP*8] - |.else - | jmp aword [DISPATCH+OP*4] - |.endif - | - |3: // Rethrow error from the right C frame. - | neg RD - | mov FCARG1, L:RB - | mov FCARG2, RD - | call extern lj_err_throw@8 // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// FP value rounding. Called by math.floor/math.ceil fast functions - |// and from JIT code. - | - |// x87 variant: Arg/ret on x87 stack. No int/xmm registers modified. - |.macro vm_round_x87, mode1, mode2 - | fnstcw word [esp+4] // Caveat: overwrites ARG1 and ARG2. - | mov [esp+8], eax - | mov ax, mode1 - | or ax, [esp+4] - |.if mode2 ~= 0xffff - | and ax, mode2 - |.endif - | mov [esp+6], ax - | fldcw word [esp+6] - | frndint - | fldcw word [esp+4] - | mov eax, [esp+8] - | ret - |.endmacro - | - |// SSE variant: arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified. - |.macro vm_round_sse, mode - | sseconst_abs xmm2, RDa - | sseconst_2p52 xmm3, RDa - | movaps xmm1, xmm0 - | andpd xmm1, xmm2 // |x| - | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|. - | jbe >1 - | andnpd xmm2, xmm0 // Isolate sign bit. - |.if mode == 2 // trunc(x)? - | movaps xmm0, xmm1 - | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52 - | subsd xmm1, xmm3 - | sseconst_1 xmm3, RDa - | cmpsd xmm0, xmm1, 1 // |x| < result? - | andpd xmm0, xmm3 - | subsd xmm1, xmm0 // If yes, subtract -1. - | orpd xmm1, xmm2 // Merge sign bit back in. - |.else - | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52 - | subsd xmm1, xmm3 - | orpd xmm1, xmm2 // Merge sign bit back in. - | .if mode == 1 // ceil(x)? - | sseconst_m1 xmm2, RDa // Must subtract -1 to preserve -0. - | cmpsd xmm0, xmm1, 6 // x > result? - | .else // floor(x)? - | sseconst_1 xmm2, RDa - | cmpsd xmm0, xmm1, 1 // x < result? - | .endif - | andpd xmm0, xmm2 - | subsd xmm1, xmm0 // If yes, subtract +-1. - |.endif - | movaps xmm0, xmm1 - |1: - | ret - |.endmacro - | - |.macro vm_round, name, ssemode, mode1, mode2 - |->name: - |.if not SSE - | vm_round_x87 mode1, mode2 - |.endif - |->name .. _sse: - | vm_round_sse ssemode - |.endmacro - | - | vm_round vm_floor, 0, 0x0400, 0xf7ff - | vm_round vm_ceil, 1, 0x0800, 0xfbff - | vm_round vm_trunc, 2, 0x0c00, 0xffff - | - |// FP modulo x%y. Called by BC_MOD* and vm_arith. - |->vm_mod: - |.if SSE - |// Args in xmm0/xmm1, return value in xmm0. - |// Caveat: xmm0-xmm5 and RC (eax) modified! - | movaps xmm5, xmm0 - | divsd xmm0, xmm1 - | sseconst_abs xmm2, RDa - | sseconst_2p52 xmm3, RDa - | movaps xmm4, xmm0 - | andpd xmm4, xmm2 // |x/y| - | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|. - | jbe >1 - | andnpd xmm2, xmm0 // Isolate sign bit. - | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52 - | subsd xmm4, xmm3 - | orpd xmm4, xmm2 // Merge sign bit back in. - | sseconst_1 xmm2, RDa - | cmpsd xmm0, xmm4, 1 // x/y < result? - | andpd xmm0, xmm2 - | subsd xmm4, xmm0 // If yes, subtract 1.0. - | movaps xmm0, xmm5 - | mulsd xmm1, xmm4 - | subsd xmm0, xmm1 - | ret - |1: - | mulsd xmm1, xmm0 - | movaps xmm0, xmm5 - | subsd xmm0, xmm1 - | ret - |.else - |// Args/ret on x87 stack (y on top). No xmm registers modified. - |// Caveat: needs 3 slots on x87 stack! RC (eax) modified! - | fld st1 - | fdiv st1 - | fnstcw word [esp+4] - | mov ax, 0x0400 - | or ax, [esp+4] - | and ax, 0xf7ff - | mov [esp+6], ax - | fldcw word [esp+6] - | frndint - | fldcw word [esp+4] - | fmulp st1 - | fsubp st1 - | ret - |.endif - | - |// FP log2(x). Called by math.log(x, base). - |->vm_log2: - |.if X64WIN - | movsd qword [rsp+8], xmm0 // Use scratch area. - | fld1 - | fld qword [rsp+8] - | fyl2x - | fstp qword [rsp+8] - | movsd xmm0, qword [rsp+8] - |.elif X64 - | movsd qword [rsp-8], xmm0 // Use red zone. - | fld1 - | fld qword [rsp-8] - | fyl2x - | fstp qword [rsp-8] - | movsd xmm0, qword [rsp-8] - |.else - | fld1 - | fld qword [esp+4] - | fyl2x - |.endif - | ret - | - |// FP exponentiation e^x and 2^x. Called by math.exp fast function and - |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified. - |// Caveat: needs 3 slots on x87 stack! - |->vm_exp_x87: - | fldl2e; fmulp st1 // e^x ==> 2^(x*log2(e)) - |->vm_exp2_x87: - | .if X64WIN - | .define expscratch, dword [rsp+8] // Use scratch area. - | .elif X64 - | .define expscratch, dword [rsp-8] // Use red zone. - | .else - | .define expscratch, dword [esp+4] // Needs 4 byte scratch area. - | .endif - | fst expscratch // Caveat: overwrites ARG1. - | cmp expscratch, 0x7f800000; je >1 // Special case: e^+Inf = +Inf - | cmp expscratch, 0xff800000; je >2 // Special case: e^-Inf = 0 - |->vm_exp2raw: // Entry point for vm_pow. Without +-Inf check. - | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part. - | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int - |1: - | ret - |2: - | fpop; fldz; ret - | - |// Generic power function x^y. Called by BC_POW, math.pow fast function, - |// and vm_arith. - |// Args/ret on x87 stack (y on top). RC (eax) modified. - |// Caveat: needs 3 slots on x87 stack! - |->vm_pow: - |.if not SSE - | fist dword [esp+4] // Store/reload int before comparison. - | fild dword [esp+4] // Integral exponent used in vm_powi. - | fucomip st1 - | jnz >8 // Branch for FP exponents. - | jp >9 // Branch for NaN exponent. - | fpop // Pop y and fallthrough to vm_powi. - | - |// FP/int power function x^i. Arg1/ret on x87 stack. - |// Arg2 (int) on C stack. RC (eax) modified. - |// Caveat: needs 2 slots on x87 stack! - | mov eax, [esp+4] - | cmp eax, 1; jle >6 // i<=1? - | // Now 1 < (unsigned)i <= 0x80000000. - |1: // Handle leading zeros. - | test eax, 1; jnz >2 - | fmul st0 - | shr eax, 1 - | jmp <1 - |2: - | shr eax, 1; jz >5 - | fdup - |3: // Handle trailing bits. - | fmul st0 - | shr eax, 1; jz >4 - | jnc <3 - | fmul st1, st0 - | jmp <3 - |4: - | fmulp st1 - |5: - | ret - |6: - | je <5 // x^1 ==> x - | jb >7 - | fld1; fdivrp st1 - | neg eax - | cmp eax, 1; je <5 // x^-1 ==> 1/x - | jmp <1 // x^-i ==> (1/x)^i - |7: - | fpop; fld1 // x^0 ==> 1 - | ret - | - |8: // FP/FP power function x^y. - | fst dword [esp+4] - | fxch - | fst dword [esp+8] - | mov eax, [esp+4]; shl eax, 1 - | cmp eax, 0xff000000; je >2 // x^+-Inf? - | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y? - | cmp eax, 0xff000000; je >4 // +-Inf^y? - | fyl2x - | jmp ->vm_exp2raw - | - |9: // Handle x^NaN. - | fld1 - | fucomip st2 - | je >1 // 1^NaN ==> 1 - | fxch // x^NaN ==> NaN - |1: - | fpop - | ret - | - |2: // Handle x^+-Inf. - | fabs - | fld1 - | fucomip st1 - | je >3 // +-1^+-Inf ==> 1 - | fpop; fabs; fldz; mov eax, 0; setc al - | ror eax, 1; xor eax, [esp+4]; jns >3 // |x|<>1, x^+-Inf ==> +Inf/0 - | fxch - |3: - | fpop1; fabs - | ret - | - |4: // Handle +-0^y or +-Inf^y. - | cmp dword [esp+4], 0; jge <3 // y >= 0, x^y ==> |x| - | fpop; fpop - | test eax, eax; jz >5 // y < 0, +-0^y ==> +Inf - | fldz // y < 0, +-Inf^y ==> 0 - | ret - |5: - | mov dword [esp+4], 0x7f800000 // Return +Inf. - | fld dword [esp+4] - | ret - |.endif - | - |// Args in xmm0/xmm1. Ret in xmm0. xmm0-xmm2 and RC (eax) modified. - |// Needs 16 byte scratch area for x86. Also called from JIT code. - |->vm_pow_sse: - | cvtsd2si eax, xmm1 - | cvtsi2sd xmm2, eax - | ucomisd xmm1, xmm2 - | jnz >8 // Branch for FP exponents. - | jp >9 // Branch for NaN exponent. - | // Fallthrough to vm_powi_sse. - | - |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified. - |->vm_powi_sse: - | cmp eax, 1; jle >6 // i<=1? - | // Now 1 < (unsigned)i <= 0x80000000. - |1: // Handle leading zeros. - | test eax, 1; jnz >2 - | mulsd xmm0, xmm0 - | shr eax, 1 - | jmp <1 - |2: - | shr eax, 1; jz >5 - | movaps xmm1, xmm0 - |3: // Handle trailing bits. - | mulsd xmm0, xmm0 - | shr eax, 1; jz >4 - | jnc <3 - | mulsd xmm1, xmm0 - | jmp <3 - |4: - | mulsd xmm0, xmm1 - |5: - | ret - |6: - | je <5 // x^1 ==> x - | jb >7 // x^0 ==> 1 - | neg eax - | call <1 - | sseconst_1 xmm1, RDa - | divsd xmm1, xmm0 - | movaps xmm0, xmm1 - | ret - |7: - | sseconst_1 xmm0, RDa - | ret - | - |8: // FP/FP power function x^y. - |.if X64 - | movd rax, xmm1; shl rax, 1 - | rol rax, 12; cmp rax, 0xffe; je >2 // x^+-Inf? - | movd rax, xmm0; shl rax, 1; je >4 // +-0^y? - | rol rax, 12; cmp rax, 0xffe; je >5 // +-Inf^y? - | .if X64WIN - | movsd qword [rsp+16], xmm1 // Use scratch area. - | movsd qword [rsp+8], xmm0 - | fld qword [rsp+16] - | fld qword [rsp+8] - | .else - | movsd qword [rsp-16], xmm1 // Use red zone. - | movsd qword [rsp-8], xmm0 - | fld qword [rsp-16] - | fld qword [rsp-8] - | .endif - |.else - | movsd qword [esp+12], xmm1 // Needs 16 byte scratch area. - | movsd qword [esp+4], xmm0 - | cmp dword [esp+12], 0; jne >1 - | mov eax, [esp+16]; shl eax, 1 - | cmp eax, 0xffe00000; je >2 // x^+-Inf? - |1: - | cmp dword [esp+4], 0; jne >1 - | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y? - | cmp eax, 0xffe00000; je >5 // +-Inf^y? - |1: - | fld qword [esp+12] - | fld qword [esp+4] - |.endif - | fyl2x // y*log2(x) - | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part. - | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int - |.if X64WIN - | fstp qword [rsp+8] // Use scratch area. - | movsd xmm0, qword [rsp+8] - |.elif X64 - | fstp qword [rsp-8] // Use red zone. - | movsd xmm0, qword [rsp-8] - |.else - | fstp qword [esp+4] // Needs 8 byte scratch area. - | movsd xmm0, qword [esp+4] - |.endif - | ret - | - |9: // Handle x^NaN. - | sseconst_1 xmm2, RDa - | ucomisd xmm0, xmm2; je >1 // 1^NaN ==> 1 - | movaps xmm0, xmm1 // x^NaN ==> NaN - |1: - | ret - | - |2: // Handle x^+-Inf. - | sseconst_abs xmm2, RDa - | andpd xmm0, xmm2 // |x| - | sseconst_1 xmm2, RDa - | ucomisd xmm0, xmm2; je <1 // +-1^+-Inf ==> 1 - | movmskpd eax, xmm1 - | xorps xmm0, xmm0 - | mov ah, al; setc al; xor al, ah; jne <1 // |x|<>1, x^+-Inf ==> +Inf/0 - |3: - | sseconst_hi xmm0, RDa, 7ff00000 // +Inf - | ret - | - |4: // Handle +-0^y. - | movmskpd eax, xmm1; test eax, eax; jnz <3 // y < 0, +-0^y ==> +Inf - | xorps xmm0, xmm0 // y >= 0, +-0^y ==> 0 - | ret - | - |5: // Handle +-Inf^y. - | movmskpd eax, xmm1; test eax, eax; jz <3 // y >= 0, +-Inf^y ==> +Inf - | xorps xmm0, xmm0 // y < 0, +-Inf^y ==> 0 - | ret - | - |// Callable from C: double lj_vm_foldfpm(double x, int fpm) - |// Computes fpm(x) for extended math functions. ORDER FPM. - |->vm_foldfpm: - |.if JIT - |.if X64 - | .if X64WIN - | .define fpmop, CARG2d - | .else - | .define fpmop, CARG1d - | .endif - | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil - | cmp fpmop, 3; jb ->vm_trunc; ja >2 - | sqrtsd xmm0, xmm0; ret - |2: - | .if X64WIN - | movsd qword [rsp+8], xmm0 // Use scratch area. - | fld qword [rsp+8] - | .else - | movsd qword [rsp-8], xmm0 // Use red zone. - | fld qword [rsp-8] - | .endif - | cmp fpmop, 5; ja >2 - | .if X64WIN; pop rax; .endif - | je >1 - | call ->vm_exp_x87 - | .if X64WIN; push rax; .endif - | jmp >7 - |1: - | call ->vm_exp2_x87 - | .if X64WIN; push rax; .endif - | jmp >7 - |2: ; cmp fpmop, 7; je >1; ja >2 - | fldln2; fxch; fyl2x; jmp >7 - |1: ; fld1; fxch; fyl2x; jmp >7 - |2: ; cmp fpmop, 9; je >1; ja >2 - | fldlg2; fxch; fyl2x; jmp >7 - |1: ; fsin; jmp >7 - |2: ; cmp fpmop, 11; je >1; ja >9 - | fcos; jmp >7 - |1: ; fptan; fpop - |7: - | .if X64WIN - | fstp qword [rsp+8] // Use scratch area. - | movsd xmm0, qword [rsp+8] - | .else - | fstp qword [rsp-8] // Use red zone. - | movsd xmm0, qword [rsp-8] - | .endif - | ret - |.else // x86 calling convention. - | .define fpmop, eax - |.if SSE - | mov fpmop, [esp+12] - | movsd xmm0, qword [esp+4] - | cmp fpmop, 1; je >1; ja >2 - | call ->vm_floor; jmp >7 - |1: ; call ->vm_ceil; jmp >7 - |2: ; cmp fpmop, 3; je >1; ja >2 - | call ->vm_trunc; jmp >7 - |1: - | sqrtsd xmm0, xmm0 - |7: - | movsd qword [esp+4], xmm0 // Overwrite callee-owned args. - | fld qword [esp+4] - | ret - |2: ; fld qword [esp+4] - | cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87 - |2: ; cmp fpmop, 7; je >1; ja >2 - | fldln2; fxch; fyl2x; ret - |1: ; fld1; fxch; fyl2x; ret - |2: ; cmp fpmop, 9; je >1; ja >2 - | fldlg2; fxch; fyl2x; ret - |1: ; fsin; ret - |2: ; cmp fpmop, 11; je >1; ja >9 - | fcos; ret - |1: ; fptan; fpop; ret - |.else - | mov fpmop, [esp+12] - | fld qword [esp+4] - | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil - | cmp fpmop, 3; jb ->vm_trunc; ja >2 - | fsqrt; ret - |2: ; cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87 - | cmp fpmop, 7; je >1; ja >2 - | fldln2; fxch; fyl2x; ret - |1: ; fld1; fxch; fyl2x; ret - |2: ; cmp fpmop, 9; je >1; ja >2 - | fldlg2; fxch; fyl2x; ret - |1: ; fsin; ret - |2: ; cmp fpmop, 11; je >1; ja >9 - | fcos; ret - |1: ; fptan; fpop; ret - |.endif - |.endif - |9: ; int3 // Bad fpm. - |.endif - | - |// Callable from C: double lj_vm_foldarith(double x, double y, int op) - |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -) - |// and basic math functions. ORDER ARITH - |->vm_foldarith: - |.if X64 - | - | .if X64WIN - | .define foldop, CARG3d - | .else - | .define foldop, CARG1d - | .endif - | cmp foldop, 1; je >1; ja >2 - | addsd xmm0, xmm1; ret - |1: ; subsd xmm0, xmm1; ret - |2: ; cmp foldop, 3; je >1; ja >2 - | mulsd xmm0, xmm1; ret - |1: ; divsd xmm0, xmm1; ret - |2: ; cmp foldop, 5; jb ->vm_mod; je ->vm_pow - | cmp foldop, 7; je >1; ja >2 - | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; ret - |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; ret - |2: ; cmp foldop, 9; ja >2 - |.if X64WIN - | movsd qword [rsp+8], xmm0 // Use scratch area. - | movsd qword [rsp+16], xmm1 - | fld qword [rsp+8] - | fld qword [rsp+16] - |.else - | movsd qword [rsp-8], xmm0 // Use red zone. - | movsd qword [rsp-16], xmm1 - | fld qword [rsp-8] - | fld qword [rsp-16] - |.endif - | je >1 - | fpatan - |7: - |.if X64WIN - | fstp qword [rsp+8] // Use scratch area. - | movsd xmm0, qword [rsp+8] - |.else - | fstp qword [rsp-8] // Use red zone. - | movsd xmm0, qword [rsp-8] - |.endif - | ret - |1: ; fxch; fscale; fpop1; jmp <7 - |2: ; cmp foldop, 11; je >1; ja >9 - | minsd xmm0, xmm1; ret - |1: ; maxsd xmm0, xmm1; ret - |9: ; int3 // Bad op. - | - |.elif SSE // x86 calling convention with SSE ops. - | - | .define foldop, eax - | mov foldop, [esp+20] - | movsd xmm0, qword [esp+4] - | movsd xmm1, qword [esp+12] - | cmp foldop, 1; je >1; ja >2 - | addsd xmm0, xmm1 - |7: - | movsd qword [esp+4], xmm0 // Overwrite callee-owned args. - | fld qword [esp+4] - | ret - |1: ; subsd xmm0, xmm1; jmp <7 - |2: ; cmp foldop, 3; je >1; ja >2 - | mulsd xmm0, xmm1; jmp <7 - |1: ; divsd xmm0, xmm1; jmp <7 - |2: ; cmp foldop, 5 - | je >1; ja >2 - | call ->vm_mod; jmp <7 - |1: ; pop edx; call ->vm_pow; push edx; jmp <7 // Writes to scratch area. - |2: ; cmp foldop, 7; je >1; ja >2 - | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; jmp <7 - |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; jmp <7 - |2: ; cmp foldop, 9; ja >2 - | fld qword [esp+4] // Reload from stack - | fld qword [esp+12] - | je >1 - | fpatan; ret - |1: ; fxch; fscale; fpop1; ret - |2: ; cmp foldop, 11; je >1; ja >9 - | minsd xmm0, xmm1; jmp <7 - |1: ; maxsd xmm0, xmm1; jmp <7 - |9: ; int3 // Bad op. - | - |.else // x86 calling convention with x87 ops. - | - | mov eax, [esp+20] - | fld qword [esp+4] - | fld qword [esp+12] - | cmp eax, 1; je >1; ja >2 - | faddp st1; ret - |1: ; fsubp st1; ret - |2: ; cmp eax, 3; je >1; ja >2 - | fmulp st1; ret - |1: ; fdivp st1; ret - |2: ; cmp eax, 5; jb ->vm_mod; je ->vm_pow - | cmp eax, 7; je >1; ja >2 - | fpop; fchs; ret - |1: ; fpop; fabs; ret - |2: ; cmp eax, 9; je >1; ja >2 - | fpatan; ret - |1: ; fxch; fscale; fpop1; ret - |2: ; cmp eax, 11; je >1; ja >9 - | fucomi st1; fcmovnbe st1; fpop1; ret - |1: ; fucomi st1; fcmovbe st1; fpop1; ret - |9: ; int3 // Bad op. - | - |.endif - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |// int lj_vm_cpuid(uint32_t f, uint32_t res[4]) - |->vm_cpuid: - |.if X64 - | mov eax, CARG1d - | .if X64WIN; push rsi; mov rsi, CARG2; .endif - | push rbx - | cpuid - | mov [rsi], eax - | mov [rsi+4], ebx - | mov [rsi+8], ecx - | mov [rsi+12], edx - | pop rbx - | .if X64WIN; pop rsi; .endif - | ret - |.else - | pushfd - | pop edx - | mov ecx, edx - | xor edx, 0x00200000 // Toggle ID bit in flags. - | push edx - | popfd - | pushfd - | pop edx - | xor eax, eax // Zero means no features supported. - | cmp ecx, edx - | jz >1 // No ID toggle means no CPUID support. - | mov eax, [esp+4] // Argument 1 is function number. - | push edi - | push ebx - | cpuid - | mov edi, [esp+16] // Argument 2 is result area. - | mov [edi], eax - | mov [edi+4], ebx - | mov [edi+8], ecx - | mov [edi+12], edx - | pop ebx - | pop edi - |1: - | ret - |.endif - | - |//----------------------------------------------------------------------- - |//-- Assertions --------------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->assert_bad_for_arg_type: -#ifdef LUA_USE_ASSERT - | int3 -#endif - | int3 - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in ah/al. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - |.if not X64 - | sub esp, 16 // Leave room for SAVE_ERRF etc. - |.endif - | saveregs_ // ebp/rbp already saved. ebp now holds global_State *. - | lea DISPATCH, [ebp+GG_G2DISP] - | mov CTSTATE, GL:ebp->ctype_state - | movzx eax, ax - | mov CTSTATE->cb.slot, eax - |.if X64 - | mov CTSTATE->cb.gpr[0], CARG1 - | mov CTSTATE->cb.gpr[1], CARG2 - | mov CTSTATE->cb.gpr[2], CARG3 - | mov CTSTATE->cb.gpr[3], CARG4 - | movsd qword CTSTATE->cb.fpr[0], xmm0 - | movsd qword CTSTATE->cb.fpr[1], xmm1 - | movsd qword CTSTATE->cb.fpr[2], xmm2 - | movsd qword CTSTATE->cb.fpr[3], xmm3 - |.if X64WIN - | lea rax, [rsp+CFRAME_SIZE+4*8] - |.else - | lea rax, [rsp+CFRAME_SIZE] - | mov CTSTATE->cb.gpr[4], CARG5 - | mov CTSTATE->cb.gpr[5], CARG6 - | movsd qword CTSTATE->cb.fpr[4], xmm4 - | movsd qword CTSTATE->cb.fpr[5], xmm5 - | movsd qword CTSTATE->cb.fpr[6], xmm6 - | movsd qword CTSTATE->cb.fpr[7], xmm7 - |.endif - | mov CTSTATE->cb.stack, rax - | mov CARG2, rsp - |.else - | lea eax, [esp+CFRAME_SIZE+16] - | mov CTSTATE->cb.gpr[0], FCARG1 - | mov CTSTATE->cb.gpr[1], FCARG2 - | mov CTSTATE->cb.stack, eax - | mov FCARG1, [esp+CFRAME_SIZE+12] // Move around misplaced retaddr/ebp. - | mov FCARG2, [esp+CFRAME_SIZE+8] - | mov SAVE_RET, FCARG1 - | mov SAVE_R4, FCARG2 - | mov FCARG2, esp - |.endif - | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok. - | mov FCARG1, CTSTATE - | call extern lj_ccallback_enter@8 // (CTState *cts, void *cf) - | // lua_State * returned in eax (RD). - | set_vmstate INTERP - | mov BASE, L:RD->base - | mov RD, L:RD->top - | sub RD, BASE - | mov LFUNC:RB, [BASE-8] - | shr RD, 3 - | add RD, 1 - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | mov L:RA, SAVE_L - | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)] - | mov aword CTSTATE->L, L:RAa - | mov L:RA->base, BASE - | mov L:RA->top, RB - | mov FCARG1, CTSTATE - | mov FCARG2, RC - | call extern lj_ccallback_leave@8 // (CTState *cts, TValue *o) - |.if X64 - | mov rax, CTSTATE->cb.gpr[0] - | movsd xmm0, qword CTSTATE->cb.fpr[0] - | jmp ->vm_leave_unw - |.else - | mov L:RB, SAVE_L - | mov eax, CTSTATE->cb.gpr[0] - | mov edx, CTSTATE->cb.gpr[1] - | cmp dword CTSTATE->cb.gpr[2], 1 - | jb >7 - | je >6 - | fld qword CTSTATE->cb.fpr[0].d - | jmp >7 - |6: - | fld dword CTSTATE->cb.fpr[0].f - |7: - | mov ecx, L:RB->top - | movzx ecx, word [ecx+6] // Get stack adjustment and copy up. - | mov SAVE_L, ecx // Must be one slot above SAVE_RET - | restoreregs - | pop ecx // Move return addr from SAVE_RET. - | add esp, [esp] // Adjust stack. - | add esp, 16 - | push ecx - | ret - |.endif - |.endif - | - |->vm_ffi_call@4: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - |.if X64 - | .type CCSTATE, CCallState, rbx - | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1 - |.else - | .type CCSTATE, CCallState, ebx - | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1 - |.endif - | - | // Readjust stack. - |.if X64 - | mov eax, CCSTATE->spadj - | sub rsp, rax - |.else - | sub esp, CCSTATE->spadj - |.if WIN - | mov CCSTATE->spadj, esp - |.endif - |.endif - | - | // Copy stack slots. - | movzx ecx, byte CCSTATE->nsp - | sub ecx, 1 - | js >2 - |1: - |.if X64 - | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)] - | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax - |.else - | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)] - | mov [esp+ecx*4], eax - |.endif - | sub ecx, 1 - | jns <1 - |2: - | - |.if X64 - | movzx eax, byte CCSTATE->nfpr - | mov CARG1, CCSTATE->gpr[0] - | mov CARG2, CCSTATE->gpr[1] - | mov CARG3, CCSTATE->gpr[2] - | mov CARG4, CCSTATE->gpr[3] - |.if not X64WIN - | mov CARG5, CCSTATE->gpr[4] - | mov CARG6, CCSTATE->gpr[5] - |.endif - | test eax, eax; jz >5 - | movaps xmm0, CCSTATE->fpr[0] - | movaps xmm1, CCSTATE->fpr[1] - | movaps xmm2, CCSTATE->fpr[2] - | movaps xmm3, CCSTATE->fpr[3] - |.if not X64WIN - | cmp eax, 4; jbe >5 - | movaps xmm4, CCSTATE->fpr[4] - | movaps xmm5, CCSTATE->fpr[5] - | movaps xmm6, CCSTATE->fpr[6] - | movaps xmm7, CCSTATE->fpr[7] - |.endif - |5: - |.else - | mov FCARG1, CCSTATE->gpr[0] - | mov FCARG2, CCSTATE->gpr[1] - |.endif - | - | call aword CCSTATE->func - | - |.if X64 - | mov CCSTATE->gpr[0], rax - | movaps CCSTATE->fpr[0], xmm0 - |.if not X64WIN - | mov CCSTATE->gpr[1], rdx - | movaps CCSTATE->fpr[1], xmm1 - |.endif - |.else - | mov CCSTATE->gpr[0], eax - | mov CCSTATE->gpr[1], edx - | cmp byte CCSTATE->resx87, 1 - | jb >7 - | je >6 - | fstp qword CCSTATE->fpr[0].d[0] - | jmp >7 - |6: - | fstp dword CCSTATE->fpr[0].f[0] - |7: - |.if WIN - | sub CCSTATE->spadj, esp - |.endif - |.endif - | - |.if X64 - | mov rbx, [rbp-8]; leave; ret - |.else - | mov ebx, [ebp-4]; leave; ret - |.endif - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |// Note: aligning all instructions does not pay off. - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - |.macro jmp_comp, lt, ge, le, gt, target - ||switch (op) { - ||case BC_ISLT: - | lt target - ||break; - ||case BC_ISGE: - | ge target - ||break; - ||case BC_ISLE: - | le target - ||break; - ||case BC_ISGT: - | gt target - ||break; - ||default: break; /* Shut up GCC. */ - ||} - |.endmacro - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1, RD = src2, JMP with RD = target - | ins_AD - |.if DUALNUM - | checkint RA, >7 - | checkint RD, >8 - | mov RB, dword [BASE+RA*8] - | add PC, 4 - | cmp RB, dword [BASE+RD*8] - | jmp_comp jge, jl, jg, jle, >9 - |6: - | movzx RD, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RA is not an integer. - | ja ->vmeta_comp - | // RA is a number. - | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp - | // RA is a number, RD is an integer. - |.if SSE - | cvtsi2sd xmm0, dword [BASE+RD*8] - | jmp >2 - |.else - | fld qword [BASE+RA*8] - | fild dword [BASE+RD*8] - | jmp >3 - |.endif - | - |8: // RA is an integer, RD is not an integer. - | ja ->vmeta_comp - | // RA is an integer, RD is a number. - |.if SSE - | cvtsi2sd xmm1, dword [BASE+RA*8] - | movsd xmm0, qword [BASE+RD*8] - | add PC, 4 - | ucomisd xmm0, xmm1 - | jmp_comp jbe, ja, jb, jae, <9 - | jmp <6 - |.else - | fild dword [BASE+RA*8] - | jmp >2 - |.endif - |.else - | checknum RA, ->vmeta_comp - | checknum RD, ->vmeta_comp - |.endif - |.if SSE - |1: - | movsd xmm0, qword [BASE+RD*8] - |2: - | add PC, 4 - | ucomisd xmm0, qword [BASE+RA*8] - |3: - |.else - |1: - | fld qword [BASE+RA*8] // Reverse order, i.e like cmp D, A. - |2: - | fld qword [BASE+RD*8] - |3: - | add PC, 4 - | fcomparepp - |.endif - | // Unordered: all of ZF CF PF set, ordered: PF clear. - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - |.if DUALNUM - | jmp_comp jbe, ja, jb, jae, <9 - | jmp <6 - |.else - | jmp_comp jbe, ja, jb, jae, >1 - | movzx RD, PC_RD - | branchPC RD - |1: - | ins_next - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | ins_AD // RA = src1, RD = src2, JMP with RD = target - | mov RB, [BASE+RD*8+4] - | add PC, 4 - |.if DUALNUM - | cmp RB, LJ_TISNUM; jne >7 - | checkint RA, >8 - | mov RB, dword [BASE+RD*8] - | cmp RB, dword [BASE+RA*8] - if (vk) { - | jne >9 - } else { - | je >9 - } - | movzx RD, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RD is not an integer. - | ja >5 - | // RD is a number. - | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5 - | // RD is a number, RA is an integer. - |.if SSE - | cvtsi2sd xmm0, dword [BASE+RA*8] - |.else - | fild dword [BASE+RA*8] - |.endif - | jmp >2 - | - |8: // RD is an integer, RA is not an integer. - | ja >5 - | // RD is an integer, RA is a number. - |.if SSE - | cvtsi2sd xmm0, dword [BASE+RD*8] - | ucomisd xmm0, qword [BASE+RA*8] - |.else - | fild dword [BASE+RD*8] - | fld qword [BASE+RA*8] - |.endif - | jmp >4 - | - |.else - | cmp RB, LJ_TISNUM; jae >5 - | checknum RA, >5 - |.endif - |.if SSE - |1: - | movsd xmm0, qword [BASE+RA*8] - |2: - | ucomisd xmm0, qword [BASE+RD*8] - |4: - |.else - |1: - | fld qword [BASE+RA*8] - |2: - | fld qword [BASE+RD*8] - |4: - | fcomparepp - |.endif - iseqne_fp: - if (vk) { - | jp >2 // Unordered means not equal. - | jne >2 - } else { - | jp >2 // Unordered means not equal. - | je >1 - } - iseqne_end: - if (vk) { - |1: // EQ: Branch to the target. - | movzx RD, PC_RD - | branchPC RD - |2: // NE: Fallthrough to next instruction. - |.if not FFI - |3: - |.endif - } else { - |.if not FFI - |3: - |.endif - |2: // NE: Branch to the target. - | movzx RD, PC_RD - | branchPC RD - |1: // EQ: Fallthrough to next instruction. - } - if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV || - op == BC_ISEQN || op == BC_ISNEN)) { - | jmp <9 - } else { - | ins_next - } - | - if (op == BC_ISEQV || op == BC_ISNEV) { - |5: // Either or both types are not numbers. - |.if FFI - | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd - | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd - |.endif - | checktp RA, RB // Compare types. - | jne <2 // Not the same type? - | cmp RB, LJ_TISPRI - | jae <1 // Same type and primitive type? - | - | // Same types and not a primitive type. Compare GCobj or pvalue. - | mov RA, [BASE+RA*8] - | mov RD, [BASE+RD*8] - | cmp RA, RD - | je <1 // Same GCobjs or pvalues? - | cmp RB, LJ_TISTABUD - | ja <2 // Different objects and not table/ud? - |.if X64 - | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata. - | jb <2 - |.endif - | - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | mov TAB:RB, TAB:RA->metatable - | test TAB:RB, TAB:RB - | jz <2 // No metatable? - | test byte TAB:RB->nomm, 1<vmeta_equal // Handle __eq metamethod. - } else { - |.if FFI - |3: - | cmp RB, LJ_TCDATA - if (LJ_DUALNUM && vk) { - | jne <9 - } else { - | jne <2 - } - | jmp ->vmeta_equal_cd - |.endif - } - break; - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | ins_AND // RA = src, RD = str const, JMP with RD = target - | mov RB, [BASE+RA*8+4] - | add PC, 4 - | cmp RB, LJ_TSTR; jne >3 - | mov RA, [BASE+RA*8] - | cmp RA, [KBASE+RD*4] - iseqne_test: - if (vk) { - | jne >2 - } else { - | je >1 - } - goto iseqne_end; - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | ins_AD // RA = src, RD = num const, JMP with RD = target - | mov RB, [BASE+RA*8+4] - | add PC, 4 - |.if DUALNUM - | cmp RB, LJ_TISNUM; jne >7 - | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8 - | mov RB, dword [KBASE+RD*8] - | cmp RB, dword [BASE+RA*8] - if (vk) { - | jne >9 - } else { - | je >9 - } - | movzx RD, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RA is not an integer. - | ja >3 - | // RA is a number. - | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1 - | // RA is a number, RD is an integer. - |.if SSE - | cvtsi2sd xmm0, dword [KBASE+RD*8] - |.else - | fild dword [KBASE+RD*8] - |.endif - | jmp >2 - | - |8: // RA is an integer, RD is a number. - |.if SSE - | cvtsi2sd xmm0, dword [BASE+RA*8] - | ucomisd xmm0, qword [KBASE+RD*8] - |.else - | fild dword [BASE+RA*8] - | fld qword [KBASE+RD*8] - |.endif - | jmp >4 - |.else - | cmp RB, LJ_TISNUM; jae >3 - |.endif - |.if SSE - |1: - | movsd xmm0, qword [KBASE+RD*8] - |2: - | ucomisd xmm0, qword [BASE+RA*8] - |4: - |.else - |1: - | fld qword [KBASE+RD*8] - |2: - | fld qword [BASE+RA*8] - |4: - | fcomparepp - |.endif - goto iseqne_fp; - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target - | mov RB, [BASE+RA*8+4] - | add PC, 4 - | cmp RB, RD - if (!LJ_HASFFI) goto iseqne_test; - if (vk) { - | jne >3 - | movzx RD, PC_RD - | branchPC RD - |2: - | ins_next - |3: - | cmp RB, LJ_TCDATA; jne <2 - | jmp ->vmeta_equal_cd - } else { - | je >2 - | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd - | movzx RD, PC_RD - | branchPC RD - |2: - | ins_next - } - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | ins_AD // RA = dst or unused, RD = src, JMP with RD = target - | mov RB, [BASE+RD*8+4] - | add PC, 4 - | cmp RB, LJ_TISTRUECOND - if (op == BC_IST || op == BC_ISTC) { - | jae >1 - } else { - | jb >1 - } - if (op == BC_ISTC || op == BC_ISFC) { - | mov [BASE+RA*8+4], RB - | mov RB, [BASE+RD*8] - | mov [BASE+RA*8], RB - } - | movzx RD, PC_RD - | branchPC RD - |1: // Fallthrough to the next instruction. - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | ins_AD // RA = dst, RD = src - |.if X64 - | mov RBa, [BASE+RD*8] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [BASE+RD*8+4] - | mov RD, [BASE+RD*8] - | mov [BASE+RA*8+4], RB - | mov [BASE+RA*8], RD - |.endif - | ins_next_ - break; - case BC_NOT: - | ins_AD // RA = dst, RD = src - | xor RB, RB - | checktp RD, LJ_TISTRUECOND - | adc RB, LJ_TTRUE - | mov [BASE+RA*8+4], RB - | ins_next - break; - case BC_UNM: - | ins_AD // RA = dst, RD = src - |.if DUALNUM - | checkint RD, >5 - | mov RB, [BASE+RD*8] - | neg RB - | jo >4 - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RB - |9: - | ins_next - |4: - | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31. - | mov dword [BASE+RA*8], 0 - | jmp <9 - |5: - | ja ->vmeta_unm - |.else - | checknum RD, ->vmeta_unm - |.endif - |.if SSE - | movsd xmm0, qword [BASE+RD*8] - | sseconst_sign xmm1, RDa - | xorps xmm0, xmm1 - | movsd qword [BASE+RA*8], xmm0 - |.else - | fld qword [BASE+RD*8] - | fchs - | fstp qword [BASE+RA*8] - |.endif - |.if DUALNUM - | jmp <9 - |.else - | ins_next - |.endif - break; - case BC_LEN: - | ins_AD // RA = dst, RD = src - | checkstr RD, >2 - | mov STR:RD, [BASE+RD*8] - |.if DUALNUM - | mov RD, dword STR:RD->len - |1: - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RD - |.elif SSE - | xorps xmm0, xmm0 - | cvtsi2sd xmm0, dword STR:RD->len - |1: - | movsd qword [BASE+RA*8], xmm0 - |.else - | fild dword STR:RD->len - |1: - | fstp qword [BASE+RA*8] - |.endif - | ins_next - |2: - | checktab RD, ->vmeta_len - | mov TAB:FCARG1, [BASE+RD*8] -#if LJ_52 - | mov TAB:RB, TAB:FCARG1->metatable - | cmp TAB:RB, 0 - | jnz >9 - |3: -#endif - |->BC_LEN_Z: - | mov RB, BASE // Save BASE. - | call extern lj_tab_len@4 // (GCtab *t) - | // Length of table returned in eax (RD). - |.if DUALNUM - | // Nothing to do. - |.elif SSE - | cvtsi2sd xmm0, RD - |.else - | mov ARG1, RD - | fild ARG1 - |.endif - | mov BASE, RB // Restore BASE. - | movzx RA, PC_RA - | jmp <1 -#if LJ_52 - |9: // Check for __len. - | test byte TAB:RB->nomm, 1<vmeta_len // 'no __len' flag NOT set: check. -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithpre, x87ins, sseins, ssereg - | ins_ABC - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | checknum RB, ->vmeta_arith_vn - | .if DUALNUM - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn - | .endif - | .if SSE - | movsd xmm0, qword [BASE+RB*8] - | sseins ssereg, qword [KBASE+RC*8] - | .else - | fld qword [BASE+RB*8] - | x87ins qword [KBASE+RC*8] - | .endif - || break; - ||case 1: - | checknum RB, ->vmeta_arith_nv - | .if DUALNUM - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv - | .endif - | .if SSE - | movsd xmm0, qword [KBASE+RC*8] - | sseins ssereg, qword [BASE+RB*8] - | .else - | fld qword [KBASE+RC*8] - | x87ins qword [BASE+RB*8] - | .endif - || break; - ||default: - | checknum RB, ->vmeta_arith_vv - | checknum RC, ->vmeta_arith_vv - | .if SSE - | movsd xmm0, qword [BASE+RB*8] - | sseins ssereg, qword [BASE+RC*8] - | .else - | fld qword [BASE+RB*8] - | x87ins qword [BASE+RC*8] - | .endif - || break; - ||} - |.endmacro - | - |.macro ins_arithdn, intins - | ins_ABC - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | checkint RB, ->vmeta_arith_vn - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn - | mov RB, [BASE+RB*8] - | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno - || break; - ||case 1: - | checkint RB, ->vmeta_arith_nv - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv - | mov RC, [KBASE+RC*8] - | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo - || break; - ||default: - | checkint RB, ->vmeta_arith_vv - | checkint RC, ->vmeta_arith_vv - | mov RB, [BASE+RB*8] - | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo - || break; - ||} - | mov dword [BASE+RA*8+4], LJ_TISNUM - ||if (vk == 1) { - | mov dword [BASE+RA*8], RC - ||} else { - | mov dword [BASE+RA*8], RB - ||} - | ins_next - |.endmacro - | - |.macro ins_arithpost - |.if SSE - | movsd qword [BASE+RA*8], xmm0 - |.else - | fstp qword [BASE+RA*8] - |.endif - |.endmacro - | - |.macro ins_arith, x87ins, sseins - | ins_arithpre x87ins, sseins, xmm0 - | ins_arithpost - | ins_next - |.endmacro - | - |.macro ins_arith, intins, x87ins, sseins - |.if DUALNUM - | ins_arithdn intins - |.else - | ins_arith, x87ins, sseins - |.endif - |.endmacro - - | // RA = dst, RB = src1 or num const, RC = src2 or num const - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arith add, fadd, addsd - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arith sub, fsub, subsd - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith imul, fmul, mulsd - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arith fdiv, divsd - break; - case BC_MODVN: - | ins_arithpre fld, movsd, xmm1 - |->BC_MODVN_Z: - | call ->vm_mod - | ins_arithpost - | ins_next - break; - case BC_MODNV: case BC_MODVV: - | ins_arithpre fld, movsd, xmm1 - | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - break; - case BC_POW: - | ins_arithpre fld, movsd, xmm1 - | call ->vm_pow - | ins_arithpost - | ins_next - break; - - case BC_CAT: - | ins_ABC // RA = dst, RB = src_start, RC = src_end - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | lea CARG2d, [BASE+RC*8] - | mov CARG3d, RC - | sub CARG3d, RB - |->BC_CAT_Z: - | mov L:RB, L:CARG1d - |.else - | lea RA, [BASE+RC*8] - | sub RC, RB - | mov ARG2, RA - | mov ARG3, RC - |->BC_CAT_Z: - | mov L:RB, SAVE_L - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // NULL (finished) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jnz ->vmeta_binop - | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB]. - | movzx RA, PC_RA - |.if X64 - | mov RCa, [BASE+RB*8] - | mov [BASE+RA*8], RCa - |.else - | mov RC, [BASE+RB*8+4] - | mov RB, [BASE+RB*8] - | mov [BASE+RA*8+4], RC - | mov [BASE+RA*8], RB - |.endif - | ins_next - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | ins_AND // RA = dst, RD = str const (~) - | mov RD, [KBASE+RD*4] - | mov dword [BASE+RA*8+4], LJ_TSTR - | mov [BASE+RA*8], RD - | ins_next - break; - case BC_KCDATA: - |.if FFI - | ins_AND // RA = dst, RD = cdata const (~) - | mov RD, [KBASE+RD*4] - | mov dword [BASE+RA*8+4], LJ_TCDATA - | mov [BASE+RA*8], RD - | ins_next - |.endif - break; - case BC_KSHORT: - | ins_AD // RA = dst, RD = signed int16 literal - |.if DUALNUM - | movsx RD, RDW - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RD - |.elif SSE - | movsx RD, RDW // Sign-extend literal. - | cvtsi2sd xmm0, RD - | movsd qword [BASE+RA*8], xmm0 - |.else - | fild PC_RD // Refetch signed RD from instruction. - | fstp qword [BASE+RA*8] - |.endif - | ins_next - break; - case BC_KNUM: - | ins_AD // RA = dst, RD = num const - |.if SSE - | movsd xmm0, qword [KBASE+RD*8] - | movsd qword [BASE+RA*8], xmm0 - |.else - | fld qword [KBASE+RD*8] - | fstp qword [BASE+RA*8] - |.endif - | ins_next - break; - case BC_KPRI: - | ins_AND // RA = dst, RD = primitive type (~) - | mov [BASE+RA*8+4], RD - | ins_next - break; - case BC_KNIL: - | ins_AD // RA = dst_start, RD = dst_end - | lea RA, [BASE+RA*8+12] - | lea RD, [BASE+RD*8+4] - | mov RB, LJ_TNIL - | mov [RA-8], RB // Sets minimum 2 slots. - |1: - | mov [RA], RB - | add RA, 8 - | cmp RA, RD - | jbe <1 - | ins_next - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | ins_AD // RA = dst, RD = upvalue # - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)] - | mov RB, UPVAL:RB->v - |.if X64 - | mov RDa, [RB] - | mov [BASE+RA*8], RDa - |.else - | mov RD, [RB+4] - | mov RB, [RB] - | mov [BASE+RA*8+4], RD - | mov [BASE+RA*8], RB - |.endif - | ins_next - break; - case BC_USETV: -#define TV2MARKOFS \ - ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)) - | ins_AD // RA = upvalue #, RD = src - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | cmp byte UPVAL:RB->closed, 0 - | mov RB, UPVAL:RB->v - | mov RA, [BASE+RD*8] - | mov RD, [BASE+RD*8+4] - | mov [RB], RA - | mov [RB+4], RD - | jz >1 - | // Check barrier for closed upvalue. - | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv) - | jnz >2 - |1: - | ins_next - | - |2: // Upvalue is black. Check if new value is collectable and white. - | sub RD, LJ_TISGCV - | cmp RD, LJ_TISNUM - LJ_TISGCV // tvisgcv(v) - | jbe <1 - | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v) - | jz <1 - | // Crossed a write barrier. Move the barrier forward. - |.if X64 and not X64WIN - | mov FCARG2, RB - | mov RB, BASE // Save BASE. - |.else - | xchg FCARG2, RB // Save BASE (FCARG2 == BASE). - |.endif - | lea GL:FCARG1, [DISPATCH+GG_DISP2G] - | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv) - | mov BASE, RB // Restore BASE. - | jmp <1 - break; -#undef TV2MARKOFS - case BC_USETS: - | ins_AND // RA = upvalue #, RD = str const (~) - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | mov GCOBJ:RA, [KBASE+RD*4] - | mov RD, UPVAL:RB->v - | mov [RD], GCOBJ:RA - | mov dword [RD+4], LJ_TSTR - | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv) - | jnz >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str) - | jz <1 - | cmp byte UPVAL:RB->closed, 0 - | jz <1 - | // Crossed a write barrier. Move the barrier forward. - | mov RB, BASE // Save BASE (FCARG2 == BASE). - | mov FCARG2, RD - | lea GL:FCARG1, [DISPATCH+GG_DISP2G] - | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv) - | mov BASE, RB // Restore BASE. - | jmp <1 - break; - case BC_USETN: - | ins_AD // RA = upvalue #, RD = num const - | mov LFUNC:RB, [BASE-8] - |.if SSE - | movsd xmm0, qword [KBASE+RD*8] - |.else - | fld qword [KBASE+RD*8] - |.endif - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | mov RA, UPVAL:RB->v - |.if SSE - | movsd qword [RA], xmm0 - |.else - | fstp qword [RA] - |.endif - | ins_next - break; - case BC_USETP: - | ins_AND // RA = upvalue #, RD = primitive type (~) - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | mov RA, UPVAL:RB->v - | mov [RA+4], RD - | ins_next - break; - case BC_UCLO: - | ins_AD // RA = level, RD = target - | branchPC RD // Do this first to free RD. - | mov L:RB, SAVE_L - | cmp dword L:RB->openupval, 0 - | je >1 - | mov L:RB->base, BASE - | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA - | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level) - | mov BASE, L:RB->base - |1: - | ins_next - break; - - case BC_FNEW: - | ins_AND // RA = dst, RD = proto const (~) (holding function prototype) - |.if X64 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG3d, [BASE-8] - | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *. - | mov CARG1d, L:RB - |.else - | mov LFUNC:RA, [BASE-8] - | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *. - | mov L:RB, SAVE_L - | mov ARG3, LFUNC:RA - | mov ARG2, PROTO:RD - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | call extern lj_func_newL_gc - | // GCfuncL * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RA, PC_RA - | mov [BASE+RA*8], LFUNC:RC - | mov dword [BASE+RA*8+4], LJ_TFUNC - | ins_next - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - | ins_AD // RA = dst, RD = hbits|asize - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov RA, [DISPATCH+DISPATCH_GL(gc.total)] - | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)] - | mov SAVE_PC, PC - | jae >5 - |1: - |.if X64 - | mov CARG3d, RD - | and RD, 0x7ff - | shr CARG3d, 11 - |.else - | mov RA, RD - | and RD, 0x7ff - | shr RA, 11 - | mov ARG3, RA - |.endif - | cmp RD, 0x7ff - | je >3 - |2: - |.if X64 - | mov L:CARG1d, L:RB - | mov CARG2d, RD - |.else - | mov ARG1, L:RB - | mov ARG2, RD - |.endif - | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Table * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RA, PC_RA - | mov [BASE+RA*8], TAB:RC - | mov dword [BASE+RA*8+4], LJ_TTAB - | ins_next - |3: // Turn 0x7ff into 0x801. - | mov RD, 0x801 - | jmp <2 - |5: - | mov L:FCARG1, L:RB - | call extern lj_gc_step_fixtop@4 // (lua_State *L) - | movzx RD, PC_RD - | jmp <1 - break; - case BC_TDUP: - | ins_AND // RA = dst, RD = table const (~) (holding template table) - | mov L:RB, SAVE_L - | mov RA, [DISPATCH+DISPATCH_GL(gc.total)] - | mov SAVE_PC, PC - | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)] - | mov L:RB->base, BASE - | jae >3 - |2: - | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA - | call extern lj_tab_dup@8 // (lua_State *L, Table *kt) - | // Table * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RA, PC_RA - | mov [BASE+RA*8], TAB:RC - | mov dword [BASE+RA*8+4], LJ_TTAB - | ins_next - |3: - | mov L:FCARG1, L:RB - | call extern lj_gc_step_fixtop@4 // (lua_State *L) - | movzx RD, PC_RD // Need to reload RD. - | not RDa - | jmp <2 - break; - - case BC_GGET: - | ins_AND // RA = dst, RD = str const (~) - | mov LFUNC:RB, [BASE-8] - | mov TAB:RB, LFUNC:RB->env - | mov STR:RC, [KBASE+RD*4] - | jmp ->BC_TGETS_Z - break; - case BC_GSET: - | ins_AND // RA = src, RD = str const (~) - | mov LFUNC:RB, [BASE-8] - | mov TAB:RB, LFUNC:RB->env - | mov STR:RC, [KBASE+RD*4] - | jmp ->BC_TSETS_Z - break; - - case BC_TGETV: - | ins_ABC // RA = dst, RB = table, RC = key - | checktab RB, ->vmeta_tgetv - | mov TAB:RB, [BASE+RB*8] - | - | // Integer key? - |.if DUALNUM - | checkint RC, >5 - | mov RC, dword [BASE+RC*8] - |.else - | // Convert number to int and back and compare. - | checknum RC, >5 - |.if SSE - | movsd xmm0, qword [BASE+RC*8] - | cvtsd2si RC, xmm0 - | cvtsi2sd xmm1, RC - | ucomisd xmm0, xmm1 - |.else - | fld qword [BASE+RC*8] - | fist ARG1 - | fild ARG1 - | fcomparepp - | mov RC, ARG1 - |.endif - | jne ->vmeta_tgetv // Generic numeric key? Use fallback. - |.endif - | cmp RC, TAB:RB->asize // Takes care of unordered, too. - | jae ->vmeta_tgetv // Not in array part? Use fallback. - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath. - | je >2 - | // Get array slot. - |.if X64 - | mov RBa, [RC] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [RC] - | mov RC, [RC+4] - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+4], RC - |.endif - |1: - | ins_next - | - |2: // Check for __index if table value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz >3 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tgetv // 'no __index' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - |3: - | mov dword [BASE+RA*8+4], LJ_TNIL - | jmp <1 - | - |5: // String key? - | checkstr RC, ->vmeta_tgetv - | mov STR:RC, [BASE+RC*8] - | jmp ->BC_TGETS_Z - break; - case BC_TGETS: - | ins_ABC // RA = dst, RB = table, RC = str const (~) - | not RCa - | mov STR:RC, [KBASE+RC*4] - | checktab RB, ->vmeta_tgets - | mov TAB:RB, [BASE+RB*8] - |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA. - | mov RA, TAB:RB->hmask - | and RA, STR:RC->hash - | imul RA, #NODE - | add NODE:RA, TAB:RB->node - |1: - | cmp dword NODE:RA->key.it, LJ_TSTR - | jne >4 - | cmp dword NODE:RA->key.gcr, STR:RC - | jne >4 - | // Ok, key found. Assumes: offsetof(Node, val) == 0 - | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath. - | je >5 // Key found, but nil value? - | movzx RC, PC_RA - | // Get node value. - |.if X64 - | mov RBa, [RA] - | mov [BASE+RC*8], RBa - |.else - | mov RB, [RA] - | mov RA, [RA+4] - | mov [BASE+RC*8], RB - | mov [BASE+RC*8+4], RA - |.endif - |2: - | ins_next - | - |3: - | movzx RC, PC_RA - | mov dword [BASE+RC*8+4], LJ_TNIL - | jmp <2 - | - |4: // Follow hash chain. - | mov NODE:RA, NODE:RA->next - | test NODE:RA, NODE:RA - | jnz <1 - | // End of hash chain: key not found, nil result. - | - |5: // Check for __index if table value is nil. - | mov TAB:RA, TAB:RB->metatable - | test TAB:RA, TAB:RA - | jz <3 // No metatable: done. - | test byte TAB:RA->nomm, 1<vmeta_tgets // Caveat: preserve STR:RC. - break; - case BC_TGETB: - | ins_ABC // RA = dst, RB = table, RC = byte literal - | checktab RB, ->vmeta_tgetb - | mov TAB:RB, [BASE+RB*8] - | cmp RC, TAB:RB->asize - | jae ->vmeta_tgetb - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath. - | je >2 - | // Get array slot. - |.if X64 - | mov RBa, [RC] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [RC] - | mov RC, [RC+4] - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+4], RC - |.endif - |1: - | ins_next - | - |2: // Check for __index if table value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz >3 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tgetb // 'no __index' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - |3: - | mov dword [BASE+RA*8+4], LJ_TNIL - | jmp <1 - break; - - case BC_TSETV: - | ins_ABC // RA = src, RB = table, RC = key - | checktab RB, ->vmeta_tsetv - | mov TAB:RB, [BASE+RB*8] - | - | // Integer key? - |.if DUALNUM - | checkint RC, >5 - | mov RC, dword [BASE+RC*8] - |.else - | // Convert number to int and back and compare. - | checknum RC, >5 - |.if SSE - | movsd xmm0, qword [BASE+RC*8] - | cvtsd2si RC, xmm0 - | cvtsi2sd xmm1, RC - | ucomisd xmm0, xmm1 - |.else - | fld qword [BASE+RC*8] - | fist ARG1 - | fild ARG1 - | fcomparepp - | mov RC, ARG1 - |.endif - | jne ->vmeta_tsetv // Generic numeric key? Use fallback. - |.endif - | cmp RC, TAB:RB->asize // Takes care of unordered, too. - | jae ->vmeta_tsetv - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL - | je >3 // Previous value is nil? - |1: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: // Set array slot. - |.if X64 - | mov RBa, [BASE+RA*8] - | mov [RC], RBa - |.else - | mov RB, [BASE+RA*8+4] - | mov RA, [BASE+RA*8] - | mov [RC+4], RB - | mov [RC], RA - |.endif - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz <1 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tsetv // 'no __newindex' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - | jmp <1 - | - |5: // String key? - | checkstr RC, ->vmeta_tsetv - | mov STR:RC, [BASE+RC*8] - | jmp ->BC_TSETS_Z - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, RA - | movzx RA, PC_RA // Restore RA. - | jmp <2 - break; - case BC_TSETS: - | ins_ABC // RA = src, RB = table, RC = str const (~) - | not RCa - | mov STR:RC, [KBASE+RC*4] - | checktab RB, ->vmeta_tsets - | mov TAB:RB, [BASE+RB*8] - |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA. - | mov RA, TAB:RB->hmask - | and RA, STR:RC->hash - | imul RA, #NODE - | mov byte TAB:RB->nomm, 0 // Clear metamethod cache. - | add NODE:RA, TAB:RB->node - |1: - | cmp dword NODE:RA->key.it, LJ_TSTR - | jne >5 - | cmp dword NODE:RA->key.gcr, STR:RC - | jne >5 - | // Ok, key found. Assumes: offsetof(Node, val) == 0 - | cmp dword [RA+4], LJ_TNIL - | je >4 // Previous value is nil? - |2: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |3: // Set node value. - | movzx RC, PC_RA - |.if X64 - | mov RBa, [BASE+RC*8] - | mov [RA], RBa - |.else - | mov RB, [BASE+RC*8+4] - | mov RC, [BASE+RC*8] - | mov [RA+4], RB - | mov [RA], RC - |.endif - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz <2 - | mov TMP1, RA // Save RA. - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - | mov RA, TMP1 // Restore RA. - | jmp <2 - | - |5: // Follow hash chain. - | mov NODE:RA, NODE:RA->next - | test NODE:RA, NODE:RA - | jnz <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | mov TAB:RA, TAB:RB->metatable - | test TAB:RA, TAB:RA - | jz >6 // No metatable: continue. - | test byte TAB:RA->nomm, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | mov TMP1, STR:RC - | mov TMP2, LJ_TSTR - | mov TMP3, TAB:RB // Save TAB:RB for us. - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | lea CARG3, TMP1 - | mov CARG2d, TAB:RB - | mov L:RB, L:CARG1d - |.else - | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2. - | mov ARG2, TAB:RB - | mov L:RB, SAVE_L - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Handles write barrier for the new key. TValue * returned in eax (RC). - | mov BASE, L:RB->base - | mov TAB:RB, TMP3 // Need TAB:RB for barrier. - | mov RA, eax - | jmp <2 // Must check write barrier for value. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, RC // Destroys STR:RC. - | jmp <3 - break; - case BC_TSETB: - | ins_ABC // RA = src, RB = table, RC = byte literal - | checktab RB, ->vmeta_tsetb - | mov TAB:RB, [BASE+RB*8] - | cmp RC, TAB:RB->asize - | jae ->vmeta_tsetb - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL - | je >3 // Previous value is nil? - |1: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: // Set array slot. - |.if X64 - | mov RAa, [BASE+RA*8] - | mov [RC], RAa - |.else - | mov RB, [BASE+RA*8+4] - | mov RA, [BASE+RA*8] - | mov [RC+4], RB - | mov [RC], RA - |.endif - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz <1 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tsetb // 'no __newindex' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - | jmp <1 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, RA - | movzx RA, PC_RA // Restore RA. - | jmp <2 - break; - - case BC_TSETM: - | ins_AD // RA = base (table at base-1), RD = num const (start index) - | mov TMP1, KBASE // Need one more free register. - | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word. - |1: - | lea RA, [BASE+RA*8] - | mov TAB:RB, [RA-8] // Guaranteed to be a table. - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: - | mov RD, MULTRES - | sub RD, 1 - | jz >4 // Nothing to copy? - | add RD, KBASE // Compute needed size. - | cmp RD, TAB:RB->asize - | ja >5 // Doesn't fit into array part? - | sub RD, KBASE - | shl KBASE, 3 - | add KBASE, TAB:RB->array - |3: // Copy result slots to table. - |.if X64 - | mov RBa, [RA] - | add RA, 8 - | mov [KBASE], RBa - |.else - | mov RB, [RA] - | mov [KBASE], RB - | mov RB, [RA+4] - | add RA, 8 - | mov [KBASE+4], RB - |.endif - | add KBASE, 8 - | sub RD, 1 - | jnz <3 - |4: - | mov KBASE, TMP1 - | ins_next - | - |5: // Need to resize array part. - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, TAB:RB - | mov CARG3d, RD - | mov L:RB, L:CARG1d - |.else - | mov ARG2, TAB:RB - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov ARG3, RD - | mov ARG1, L:RB - |.endif - | mov SAVE_PC, PC - | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | mov BASE, L:RB->base - | movzx RA, PC_RA // Restore RA. - | jmp <1 // Retry. - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:RB, RD - | jmp <2 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALL: case BC_CALLM: - | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs - if (op == BC_CALLM) { - | add NARGS:RD, MULTRES - } - | cmp dword [BASE+RA*8+4], LJ_TFUNC - | mov LFUNC:RB, [BASE+RA*8] - | jne ->vmeta_call_ra - | lea BASE, [BASE+RA*8+8] - | ins_call - break; - - case BC_CALLMT: - | ins_AD // RA = base, RD = extra_nargs - | add NARGS:RD, MULTRES - | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op. - break; - case BC_CALLT: - | ins_AD // RA = base, RD = nargs+1 - | lea RA, [BASE+RA*8+8] - | mov KBASE, BASE // Use KBASE for move + vmeta_call hint. - | mov LFUNC:RB, [RA-8] - | cmp dword [RA-4], LJ_TFUNC - | jne ->vmeta_call - |->BC_CALLT_Z: - | mov PC, [BASE-4] - | test PC, FRAME_TYPE - | jnz >7 - |1: - | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below. - | mov MULTRES, NARGS:RD - | sub NARGS:RD, 1 - | jz >3 - |2: // Move args down. - |.if X64 - | mov RBa, [RA] - | add RA, 8 - | mov [KBASE], RBa - |.else - | mov RB, [RA] - | mov [KBASE], RB - | mov RB, [RA+4] - | add RA, 8 - | mov [KBASE+4], RB - |.endif - | add KBASE, 8 - | sub NARGS:RD, 1 - | jnz <2 - | - | mov LFUNC:RB, [BASE-8] - |3: - | mov NARGS:RD, MULTRES - | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function? - | ja >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function. - | test PC, FRAME_TYPE // Lua frame below? - | jnz <4 - | movzx RA, PC_RA - | not RAa - | lea RA, [BASE+RA*8] - | mov LFUNC:KBASE, [RA-8] // Need to prepare KBASE. - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | jmp <4 - | - |7: // Tailcall from a vararg function. - | sub PC, FRAME_VARG - | test PC, FRAME_TYPEP - | jnz >8 // Vararg frame below? - | sub BASE, PC // Need to relocate BASE/KBASE down. - | mov KBASE, BASE - | mov PC, [BASE-4] - | jmp <1 - |8: - | add PC, FRAME_VARG - | jmp <1 - break; - - case BC_ITERC: - | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1) - | lea RA, [BASE+RA*8+8] // fb = base+1 - |.if X64 - | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3]. - | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2]. - | mov [RA], RBa - | mov [RA+8], RCa - |.else - | mov RB, [RA-24] // Copy state. fb[0] = fb[-3]. - | mov RC, [RA-20] - | mov [RA], RB - | mov [RA+4], RC - | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2]. - | mov RC, [RA-12] - | mov [RA+8], RB - | mov [RA+12], RC - |.endif - | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4] - | mov RC, [RA-28] - | mov [RA-8], LFUNC:RB - | mov [RA-4], RC - | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call. - | mov NARGS:RD, 2+1 - | jne ->vmeta_call - | mov BASE, RA - | ins_call - break; - - case BC_ITERN: - | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1)) - |.if JIT - | // NYI: add hotloop, record BC_ITERN. - |.endif - | mov TMP1, KBASE // Need two more free registers. - | mov TMP2, DISPATCH - | mov TAB:RB, [BASE+RA*8-16] - | mov RC, [BASE+RA*8-8] // Get index from control var. - | mov DISPATCH, TAB:RB->asize - | add PC, 4 - | mov KBASE, TAB:RB->array - |1: // Traverse array part. - | cmp RC, DISPATCH; jae >5 // Index points after array part? - | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4 - |.if DUALNUM - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RC - |.elif SSE - | cvtsi2sd xmm0, RC - |.else - | fild dword [BASE+RA*8-8] - |.endif - | // Copy array slot to returned value. - |.if X64 - | mov RBa, [KBASE+RC*8] - | mov [BASE+RA*8+8], RBa - |.else - | mov RB, [KBASE+RC*8+4] - | mov [BASE+RA*8+12], RB - | mov RB, [KBASE+RC*8] - | mov [BASE+RA*8+8], RB - |.endif - | add RC, 1 - | // Return array index as a numeric key. - |.if DUALNUM - | // See above. - |.elif SSE - | movsd qword [BASE+RA*8], xmm0 - |.else - | fstp qword [BASE+RA*8] - |.endif - | mov [BASE+RA*8-8], RC // Update control var. - |2: - | movzx RD, PC_RD // Get target from ITERL. - | branchPC RD - |3: - | mov DISPATCH, TMP2 - | mov KBASE, TMP1 - | ins_next - | - |4: // Skip holes in array part. - | add RC, 1 - |.if not (DUALNUM or SSE) - | mov [BASE+RA*8-8], RC - |.endif - | jmp <1 - | - |5: // Traverse hash part. - | sub RC, DISPATCH - |6: - | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1. - | imul KBASE, RC, #NODE - | add NODE:KBASE, TAB:RB->node - | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7 - | lea DISPATCH, [RC+DISPATCH+1] - | // Copy key and value from hash slot. - |.if X64 - | mov RBa, NODE:KBASE->key - | mov RCa, NODE:KBASE->val - | mov [BASE+RA*8], RBa - | mov [BASE+RA*8+8], RCa - |.else - | mov RB, NODE:KBASE->key.gcr - | mov RC, NODE:KBASE->key.it - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+4], RC - | mov RB, NODE:KBASE->val.gcr - | mov RC, NODE:KBASE->val.it - | mov [BASE+RA*8+8], RB - | mov [BASE+RA*8+12], RC - |.endif - | mov [BASE+RA*8-8], DISPATCH - | jmp <2 - | - |7: // Skip holes in hash part. - | add RC, 1 - | jmp <6 - break; - - case BC_ISNEXT: - | ins_AD // RA = base, RD = target (points to ITERN) - | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5 - | mov CFUNC:RB, [BASE+RA*8-24] - | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5 - | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5 - | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5 - | branchPC RD - | mov dword [BASE+RA*8-8], 0 // Initialize control var. - | mov dword [BASE+RA*8-4], 0xfffe7fff - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | mov PC_OP, BC_JMP - | branchPC RD - | mov byte [PC], BC_ITERC - | jmp <1 - break; - - case BC_VARG: - | ins_ABC // RA = base, RB = nresults+1, RC = numparams - | mov TMP1, KBASE // Need one more free register. - | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)] - | lea RA, [BASE+RA*8] - | sub KBASE, [BASE-4] - | // Note: KBASE may now be even _above_ BASE if nargs was < numparams. - | test RB, RB - | jz >5 // Copy all varargs? - | lea RB, [RA+RB*8-8] - | cmp KBASE, BASE // No vararg slots? - | jnb >2 - |1: // Copy vararg slots to destination slots. - |.if X64 - | mov RCa, [KBASE-8] - | add KBASE, 8 - | mov [RA], RCa - |.else - | mov RC, [KBASE-8] - | mov [RA], RC - | mov RC, [KBASE-4] - | add KBASE, 8 - | mov [RA+4], RC - |.endif - | add RA, 8 - | cmp RA, RB // All destination slots filled? - | jnb >3 - | cmp KBASE, BASE // No more vararg slots? - | jb <1 - |2: // Fill up remainder with nil. - | mov dword [RA+4], LJ_TNIL - | add RA, 8 - | cmp RA, RB - | jb <2 - |3: - | mov KBASE, TMP1 - | ins_next - | - |5: // Copy all varargs. - | mov MULTRES, 1 // MULTRES = 0+1 - | mov RC, BASE - | sub RC, KBASE - | jbe <3 // No vararg slots? - | mov RB, RC - | shr RB, 3 - | add RB, 1 - | mov MULTRES, RB // MULTRES = #varargs+1 - | mov L:RB, SAVE_L - | add RC, RA - | cmp RC, L:RB->maxstack - | ja >7 // Need to grow stack? - |6: // Copy all vararg slots. - |.if X64 - | mov RCa, [KBASE-8] - | add KBASE, 8 - | mov [RA], RCa - |.else - | mov RC, [KBASE-8] - | mov [RA], RC - | mov RC, [KBASE-4] - | add KBASE, 8 - | mov [RA+4], RC - |.endif - | add RA, 8 - | cmp KBASE, BASE // No more vararg slots? - | jb <6 - | jmp <3 - | - |7: // Grow stack for varargs. - | mov L:RB->base, BASE - | mov L:RB->top, RA - | mov SAVE_PC, PC - | sub KBASE, BASE // Need delta, because BASE may change. - | mov FCARG2, MULTRES - | sub FCARG2, 1 - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->base - | mov RA, L:RB->top - | add KBASE, BASE - | jmp <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | ins_AD // RA = results, RD = extra_nresults - | add RD, MULTRES // MULTRES >=1, so RD >=1. - | // Fall through. Assumes BC_RET follows and ins_AD is a no-op. - break; - - case BC_RET: case BC_RET0: case BC_RET1: - | ins_AD // RA = results, RD = nresults+1 - if (op != BC_RET0) { - | shl RA, 3 - } - |1: - | mov PC, [BASE-4] - | mov MULTRES, RD // Save nresults+1. - | test PC, FRAME_TYPE // Check frame type marker. - | jnz >7 // Not returning to a fixarg Lua func? - switch (op) { - case BC_RET: - |->BC_RET_Z: - | mov KBASE, BASE // Use KBASE for result move. - | sub RD, 1 - | jz >3 - |2: // Move results down. - |.if X64 - | mov RBa, [KBASE+RA] - | mov [KBASE-8], RBa - |.else - | mov RB, [KBASE+RA] - | mov [KBASE-8], RB - | mov RB, [KBASE+RA+4] - | mov [KBASE-4], RB - |.endif - | add KBASE, 8 - | sub RD, 1 - | jnz <2 - |3: - | mov RD, MULTRES // Note: MULTRES may be >255. - | movzx RB, PC_RB // So cannot compare with RDL! - |5: - | cmp RB, RD // More results expected? - | ja >6 - break; - case BC_RET1: - |.if X64 - | mov RBa, [BASE+RA] - | mov [BASE-8], RBa - |.else - | mov RB, [BASE+RA+4] - | mov [BASE-4], RB - | mov RB, [BASE+RA] - | mov [BASE-8], RB - |.endif - /* fallthrough */ - case BC_RET0: - |5: - | cmp PC_RB, RDL // More results expected? - | ja >6 - default: - break; - } - | movzx RA, PC_RA - | not RAa // Note: ~RA = -(RA+1) - | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8 - | mov LFUNC:KBASE, [BASE-8] - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | ins_next - | - |6: // Fill up results with nil. - if (op == BC_RET) { - | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base. - | add KBASE, 8 - } else { - | mov dword [BASE+RD*8-12], LJ_TNIL - } - | add RD, 1 - | jmp <5 - | - |7: // Non-standard return case. - | lea RB, [PC-FRAME_VARG] - | test RB, FRAME_TYPEP - | jnz ->vm_return - | // Return from vararg function: relocate BASE down and RA up. - | sub BASE, RB - if (op != BC_RET0) { - | add RA, RB - } - | jmp <1 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4] - |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12] - |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20] - |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28] - - case BC_FORL: - |.if JIT - | hotloop RB - |.endif - | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - vk = (op == BC_IFORL || op == BC_JFORL); - | ins_AJ // RA = base, RD = target (after end of loop or start of loop) - | lea RA, [BASE+RA*8] - if (LJ_DUALNUM) { - | cmp FOR_TIDX, LJ_TISNUM; jne >9 - if (!vk) { - | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for - | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for - | mov RB, dword FOR_IDX - | cmp dword FOR_STEP, 0; jl >5 - } else { -#ifdef LUA_USE_ASSERT - | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type - | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type -#endif - | mov RB, dword FOR_STEP - | test RB, RB; js >5 - | add RB, dword FOR_IDX; jo >1 - | mov dword FOR_IDX, RB - } - | cmp RB, dword FOR_STOP - | mov FOR_TEXT, LJ_TISNUM - | mov dword FOR_EXT, RB - if (op == BC_FORI) { - | jle >7 - |1: - |6: - | branchPC RD - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RD, PC_RD - | jle =>BC_JLOOP - |1: - |6: - } else if (op == BC_IFORL) { - | jg >7 - |6: - | branchPC RD - |1: - } else { - | jle =>BC_JLOOP - |1: - |6: - } - |7: - | ins_next - | - |5: // Invert check for negative step. - if (vk) { - | add RB, dword FOR_IDX; jo <1 - | mov dword FOR_IDX, RB - } - | cmp RB, dword FOR_STOP - | mov FOR_TEXT, LJ_TISNUM - | mov dword FOR_EXT, RB - if (op == BC_FORI) { - | jge <7 - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RD, PC_RD - | jge =>BC_JLOOP - } else if (op == BC_IFORL) { - | jl <7 - } else { - | jge =>BC_JLOOP - } - | jmp <6 - |9: // Fallback to FP variant. - } else if (!vk) { - | cmp FOR_TIDX, LJ_TISNUM - } - if (!vk) { - | jae ->vmeta_for - | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for - } else { -#ifdef LUA_USE_ASSERT - | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type - | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type -#endif - } - | mov RB, FOR_TSTEP // Load type/hiword of for step. - if (!vk) { - | cmp RB, LJ_TISNUM; jae ->vmeta_for - } - |.if SSE - | movsd xmm0, qword FOR_IDX - | movsd xmm1, qword FOR_STOP - if (vk) { - | addsd xmm0, qword FOR_STEP - | movsd qword FOR_IDX, xmm0 - | test RB, RB; js >3 - } else { - | jl >3 - } - | ucomisd xmm1, xmm0 - |1: - | movsd qword FOR_EXT, xmm0 - |.else - | fld qword FOR_STOP - | fld qword FOR_IDX - if (vk) { - | fadd qword FOR_STEP // nidx = idx + step - | fst qword FOR_IDX - | fst qword FOR_EXT - | test RB, RB; js >1 - } else { - | fst qword FOR_EXT - | jl >1 - } - | fxch // Swap lim/(n)idx if step non-negative. - |1: - | fcomparepp - |.endif - if (op == BC_FORI) { - |.if DUALNUM - | jnb <7 - |.else - | jnb >2 - | branchPC RD - |.endif - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RD, PC_RD - | jnb =>BC_JLOOP - } else if (op == BC_IFORL) { - |.if DUALNUM - | jb <7 - |.else - | jb >2 - | branchPC RD - |.endif - } else { - | jnb =>BC_JLOOP - } - |.if DUALNUM - | jmp <6 - |.else - |2: - | ins_next - |.endif - |.if SSE - |3: // Invert comparison if step is negative. - | ucomisd xmm0, xmm1 - | jmp <1 - |.endif - break; - - case BC_ITERL: - |.if JIT - | hotloop RB - |.endif - | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | ins_AJ // RA = base, RD = target - | lea RA, [BASE+RA*8] - | mov RB, [RA+4] - | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil. - if (op == BC_JITERL) { - | mov [RA-4], RB - | mov RB, [RA] - | mov [RA-8], RB - | jmp =>BC_JLOOP - } else { - | branchPC RD // Otherwise save control var + branch. - | mov RD, [RA] - | mov [RA-4], RB - | mov [RA-8], RD - } - |1: - | ins_next - break; - - case BC_LOOP: - | ins_A // RA = base, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop RB - |.endif - | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op. - break; - - case BC_ILOOP: - | ins_A // RA = base, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | ins_AD // RA = base (ignored), RD = traceno - | mov RA, [DISPATCH+DISPATCH_J(trace)] - | mov TRACE:RD, [RA+RD*4] - | mov RDa, TRACE:RD->mcode - | mov L:RB, SAVE_L - | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE - | mov [DISPATCH+DISPATCH_GL(jit_L)], L:RB - | // Save additional callee-save registers only used in compiled code. - |.if X64WIN - | mov TMPQ, r12 - | mov TMPa, r13 - | mov CSAVE_4, r14 - | mov CSAVE_3, r15 - | mov RAa, rsp - | sub rsp, 9*16+4*8 - | movdqa [RAa], xmm6 - | movdqa [RAa-1*16], xmm7 - | movdqa [RAa-2*16], xmm8 - | movdqa [RAa-3*16], xmm9 - | movdqa [RAa-4*16], xmm10 - | movdqa [RAa-5*16], xmm11 - | movdqa [RAa-6*16], xmm12 - | movdqa [RAa-7*16], xmm13 - | movdqa [RAa-8*16], xmm14 - | movdqa [RAa-9*16], xmm15 - |.elif X64 - | mov TMPQ, r12 - | mov TMPa, r13 - | sub rsp, 16 - |.endif - | jmp RDa - |.endif - break; - - case BC_JMP: - | ins_AJ // RA = unused, RD = target - | branchPC RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - /* - ** Reminder: A function may be called with func/args above L->maxstack, - ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot, - ** too. This means all FUNC* ops (including fast functions) must check - ** for stack overflow _before_ adding more slots! - */ - - case BC_FUNCF: - |.if JIT - | hotcall RB - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | ins_AD // BASE = new base, RA = framesize, RD = nargs+1 - | mov KBASE, [PC-4+PC2PROTO(k)] - | mov L:RB, SAVE_L - | lea RA, [BASE+RA*8] // Top of frame. - | cmp RA, L:RB->maxstack - | ja ->vm_growstack_f - | movzx RA, byte [PC-4+PC2PROTO(numparams)] - | cmp NARGS:RD, RA // Check for missing parameters. - | jbe >3 - |2: - if (op == BC_JFUNCF) { - | movzx RD, PC_RD - | jmp =>BC_JLOOP - } else { - | ins_next - } - | - |3: // Clear missing parameters. - | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL - | add NARGS:RD, 1 - | cmp NARGS:RD, RA - | jbe <3 - | jmp <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | int3 // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | ins_AD // BASE = new base, RA = framesize, RD = nargs+1 - | lea RB, [NARGS:RD*8+FRAME_VARG] - | lea RD, [BASE+NARGS:RD*8] - | mov LFUNC:KBASE, [BASE-8] - | mov [RD-4], RB // Store delta + FRAME_VARG. - | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC. - | mov L:RB, SAVE_L - | lea RA, [RD+RA*8] - | cmp RA, L:RB->maxstack - | ja ->vm_growstack_v // Need to grow stack. - | mov RA, BASE - | mov BASE, RD - | movzx RB, byte [PC-4+PC2PROTO(numparams)] - | test RB, RB - | jz >2 - |1: // Copy fixarg slots up to new frame. - | add RA, 8 - | cmp RA, BASE - | jnb >3 // Less args than parameters? - | mov KBASE, [RA-8] - | mov [RD], KBASE - | mov KBASE, [RA-4] - | mov [RD+4], KBASE - | add RD, 8 - | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC). - | sub RB, 1 - | jnz <1 - |2: - if (op == BC_JFUNCV) { - | movzx RD, PC_RD - | jmp =>BC_JLOOP - } else { - | mov KBASE, [PC-4+PC2PROTO(k)] - | ins_next - } - | - |3: // Clear missing parameters. - | mov dword [RD+4], LJ_TNIL - | add RD, 8 - | sub RB, 1 - | jnz <3 - | jmp <2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1 - | mov CFUNC:RB, [BASE-8] - | mov KBASEa, CFUNC:RB->f - | mov L:RB, SAVE_L - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB->base, BASE - | lea RA, [RD+8*LUA_MINSTACK] - | cmp RA, L:RB->maxstack - | mov L:RB->top, RD - if (op == BC_FUNCC) { - |.if X64 - | mov CARG1d, L:RB // Caveat: CARG1d may be RA. - |.else - | mov ARG1, L:RB - |.endif - } else { - |.if X64 - | mov CARG2, KBASEa - | mov CARG1d, L:RB // Caveat: CARG1d may be RA. - |.else - | mov ARG2, KBASEa - | mov ARG1, L:RB - |.endif - } - | ja ->vm_growstack_c // Need to grow stack. - | set_vmstate C - if (op == BC_FUNCC) { - | call KBASEa // (lua_State *L) - } else { - | // (lua_State *L, lua_CFunction f) - | call aword [DISPATCH+DISPATCH_GL(wrapf)] - } - | set_vmstate INTERP - | // nresults returned in eax (RD). - | mov BASE, L:RB->base - | lea RA, [BASE+RD*8] - | neg RA - | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8 - | mov PC, [BASE-4] // Fetch PC of caller. - | jmp ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - dasm_growpc(Dst, BC__MAX); - build_subroutines(ctx); - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); -#if LJ_64 -#define SZPTR "8" -#define BSZPTR "3" -#define REG_SP "0x7" -#define REG_RA "0x10" -#else -#define SZPTR "4" -#define BSZPTR "2" -#define REG_SP "0x4" -#define REG_RA "0x8" -#endif - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n" - "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n" - "\t.align " SZPTR "\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" -#if LJ_64 - "\t.quad .Lbegin\n" - "\t.quad %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */ -#else - "\t.long .Lbegin\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */ - "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */ - "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE0:\n\n", fcofs, CFRAME_SIZE); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" -#if LJ_64 - "\t.quad lj_vm_ffi_call\n" - "\t.quad %d\n" - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ -#else - "\t.long lj_vm_ffi_call\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */ - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if (defined(__sun__) && defined(__svr4__)) -#if LJ_64 - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n"); -#else - fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n"); -#endif -#else - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n"); -#endif - fprintf(ctx->fp, - ".Lframe1:\n" - "\t.long .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.long lj_err_unwind_dwarf-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n" - "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n" - "\t.align " SZPTR "\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.long .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.long .LASFDE2-.Lframe1\n" - "\t.long .Lbegin-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ -#if LJ_64 - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */ -#else - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */ - "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */ - "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE2:\n\n", fcofs, CFRAME_SIZE); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.long .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n" - "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n" - "\t.align " SZPTR "\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.long .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.long .LASFDE3-.Lframe2\n" - "\t.long lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ -#if LJ_64 - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ -#else - "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */ - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif - break; - /* Mental note: never let Apple design an assembler. - ** Or a linker. Or a plastic case. But I digress. - */ - case BUILD_machasm: { -#if LJ_HASFFI - int fcsize = 0; -#endif - int i; - fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n"); - fprintf(ctx->fp, - "EH_frame1:\n" - "\t.set L$set$x,LECIEX-LSCIEX\n" - "\t.long L$set$x\n" - "LSCIEX:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zPR\\0\"\n" - "\t.byte 0x1\n" - "\t.byte 128-" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.byte 6\n" /* augmentation length */ - "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */ -#if LJ_64 - "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n" -#else - "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */ -#endif - "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n" - "\t.align " BSZPTR "\n" - "LECIEX:\n\n"); - for (i = 0; i < ctx->nsym; i++) { - const char *name = ctx->sym[i].name; - int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs; - if (size == 0) continue; -#if LJ_HASFFI - if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; } -#endif - fprintf(ctx->fp, - "%s.eh:\n" - "LSFDE%d:\n" - "\t.set L$set$%d,LEFDE%d-LASFDE%d\n" - "\t.long L$set$%d\n" - "LASFDE%d:\n" - "\t.long LASFDE%d-EH_frame1\n" - "\t.long %s-.\n" - "\t.long %d\n" - "\t.byte 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */ -#if LJ_64 - "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */ -#else - "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/ - "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */ - "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */ - "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */ -#endif - "\t.align " BSZPTR "\n" - "LEFDE%d:\n\n", - name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i); - } -#if LJ_HASFFI - if (fcsize) { - fprintf(ctx->fp, - "EH_frame2:\n" - "\t.set L$set$y,LECIEY-LSCIEY\n" - "\t.long L$set$y\n" - "LSCIEY:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zR\\0\"\n" - "\t.byte 0x1\n" - "\t.byte 128-" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.byte 1\n" /* augmentation length */ -#if LJ_64 - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n" -#else - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */ -#endif - "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n" - "\t.align " BSZPTR "\n" - "LECIEY:\n\n"); - fprintf(ctx->fp, - "_lj_vm_ffi_call.eh:\n" - "LSFDEY:\n" - "\t.set L$set$yy,LEFDEY-LASFDEY\n" - "\t.long L$set$yy\n" - "LASFDEY:\n" - "\t.long LASFDEY-EH_frame2\n" - "\t.long _lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.byte 0\n" /* augmentation length */ -#if LJ_64 - "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */ -#else - "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */ - "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/ - "\t.byte 0xd\n\t.uleb128 0x4\n" /* def_cfa_register ebp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */ -#endif - "\t.align " BSZPTR "\n" - "LEFDEY:\n\n", fcsize); - } -#endif -#if LJ_64 - fprintf(ctx->fp, "\t.subsections_via_symbols\n"); -#else - fprintf(ctx->fp, - "\t.non_lazy_symbol_pointer\n" - "L_lj_err_unwind_dwarf$non_lazy_ptr:\n" - ".indirect_symbol _lj_err_unwind_dwarf\n" - ".long 0\n"); -#endif - } - break; - default: /* Difficult for other modes. */ - break; - } -} - diff --git a/third-party/LuaJIT-2.0.2/src/xedkbuild.bat b/third-party/LuaJIT-2.0.2/src/xedkbuild.bat deleted file mode 100644 index 375f1955db..0000000000 --- a/third-party/LuaJIT-2.0.2/src/xedkbuild.bat +++ /dev/null @@ -1,92 +0,0 @@ -@rem Script to build LuaJIT with the Xbox 360 SDK. -@rem Donated to the public domain. -@rem -@rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler) -@rem Then cd to this directory and run this script. - -@if not defined INCLUDE goto :FAIL -@if not defined XEDK goto :FAIL - -@setlocal -@rem ---- Host compiler ---- -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@rem Error out for 64 bit host compiler -@minilua -@if errorlevel 8 goto :FAIL - -@set DASMFLAGS=-D GPR64 -D FRAME32 -D PPE -D SQRT -D DUALNUM -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_ppc.dasc -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% /D_XBOX_VER=200 /DLUAJIT_TARGET=LUAJIT_ARCH_PPC host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m peobj -o lj_vm.obj -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@rem ---- Cross compiler ---- -@set LJCOMPILE="%XEDK%\bin\win32\cl" /nologo /c /MT /O2 /W3 /GF /Gm- /GR- /GS- /Gy /openmp- /D_CRT_SECURE_NO_DEPRECATE /DNDEBUG /D_XBOX /D_LIB /DLUAJIT_USE_SYSMALLOC -@set LJLIB="%XEDK%\bin\win32\lib" /nologo -@set INCLUDE="%XEDK%\include\xbox" - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set LJCOMPILE="%LJCOMPILE%" /Zi -:NODEBUG -@if "%1"=="amalg" goto :AMALG -%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:luajit20.lib lj_*.obj lib_*.obj -@if errorlevel 1 goto :BAD -@goto :NOAMALG -:AMALG -%LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:luajit20.lib ljamalg.obj lj_vm.obj -@if errorlevel 1 goto :BAD -:NOAMALG - -@del *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for Xbox 360 === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo To run this script you must open a "Visual Studio .NET Command Prompt" -@echo (32 bit host compiler). The Xbox 360 SDK must be installed, too. -:END diff --git a/userspace/.gitignore b/userspace/.gitignore index 9daeafb986..e69de29bb2 100644 --- a/userspace/.gitignore +++ b/userspace/.gitignore @@ -1 +0,0 @@ -test diff --git a/userspace/async/async_key_value_source.h b/userspace/async/async_key_value_source.h new file mode 100644 index 0000000000..2f639d4b62 --- /dev/null +++ b/userspace/async/async_key_value_source.h @@ -0,0 +1,345 @@ +/* +Copyright (C) 2019 Sysdig, Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sysdig +{ + +/** + * Base class for classes that need to collect values asynchronously from some + * value source. Subclasses will override the the run_impl() method and + * implement the concrete value lookup behavior. In that method, subclasses + * will use use dequeue_next_key() method to get the key that it will use to + * collect the value(s), collect the appropriate value(s), and call the + * store_value() method to save the value. The run_impl() method should + * continue to dequeue and process values while the dequeue_next_key() method + * returns true. + * + * The constructor for this class accepts a maximum wait time; this specifies + * how long client code is willing to wait for a synchronous response (i.e., + * how long the lookup() method will block waiting for the requested value). + * If the async_key_value_source is able to collect the requested value + * within that time period, then the lookup() method will return them. + * + * If the lookup() method is unable to collect the requested value within + * the requested time period, then one of two things will happen. + * + *
    + *
  1. If the client supplied a callback handler in the call to lookup(), then + * that callback handler will be invoked by the async_key_value_source once + * the value has been collected. Note that the callback handler will be + * invoked in the context of the asynchronous thread associated with the + * async_key_value_source.
  2. + *
  3. If the client did not supply a handler, then the value will be stored, + * and the next call to the lookup() method with the same key will return + * the previously collected value. If lookup() is not called with the + * specified ttl time, then this component will prune the stored value.
  4. + *
+ * + * @tparam key_type The type of the keys for which concrete subclasses will + * query. This type must have a valid operator==(). + * @tparam value_type The type of value that concrete subclasses will + * receive from a query. This type must have a valid + * operator=(). + */ +template +class async_key_value_source +{ +public: + /** + * If provided to the constructor as max_wait_ms, then lookup will + * not wait for a response. + */ + const static uint64_t NO_WAIT_LOOKUP = 0; + + /** + * A callback handler will take a key and a output reference to the + * value. + */ + typedef std::function callback_handler; + + /** + * Initialize this new async_key_value_source, which will block + * synchronously for the given max_wait_ms for value collection. + * + * @param[in] max_wait_ms The maximum amount of time that client code + * is willing to wait for lookup() to collect + * a value before falling back to an async + * return. + * @param[in] ttl_ms The time, in milliseconds, that a cached + * value will live before being considered + * "too old" and being pruned. + */ + async_key_value_source(uint64_t max_wait_ms, uint64_t ttl_ms) noexcept; + + async_key_value_source(const async_key_value_source&) = delete; + async_key_value_source(async_key_value_source&&) = delete; + async_key_value_source& operator=(const async_key_value_source&) = delete; + + virtual ~async_key_value_source(); + + /** + * Returns the maximum amount of time, in milliseconds, that a call to + * lookup() will block synchronously before returning. + */ + uint64_t get_max_wait() const; + + /** + * Returns the maximum amount of time, in milliseconds, that a cached + * value will live before being pruned. + */ + uint64_t get_ttl() const; + + /** + * Lookup value(s) based on the given key. This method will block + * the caller for up the max_wait_ms time specified at construction + * for the desired value(s) to be available. + * + * @param[in] key The key to the value for which the client + * wishes to query. + * @param[out] value If this method is able to fetch the desired + * value within the max_wait_ms specified at + * construction time, then this output parameter + * will contain the collected value. The value + * of this parameter is defined only if this method + * returns true. + * @param[in] handler If this method is unable to collect the requested + * value(s) before the timeout, and if this parameter + * is a valid, non-empty, function, then this class + * will invoke the given handler from the async + * thread immediately after the collected values + * are available. If this handler is empty, then + * this async_key_value_source will store the + * values until either the next call to lookup() + * or until its ttl expires, whichever comes first. + * The handler is responsible for any thread-safety + * guarantees. + * + * @returns true if this method was able to lookup and return the + * value synchronously; false otherwise. + */ + bool lookup(const key_type& key, + value_type& value, + const callback_handler& handler = callback_handler()); + + /** + * Lookup a value based on the specified key, after an initial delay. + * This method behaves identically to `lookup()`, except that the request + * is dispatched `delay` milliseconds after the call. + * + * @see lookup() for details + */ + bool lookup_delayed(const key_type& key, + value_type& value, + std::chrono::milliseconds delay, + const callback_handler& handler = callback_handler()); + + /** + * Determines if the async thread associated with this + * async_key_value_source is running. + * + * Note: This API is for information only. Clients should + * not use this to implement any sort of complex behavior. Such + * use will lead to race conditions. For example, is_running() and + * lookup() could potentially race, causing is_running() to return + * false after lookup() has started the thread. + * + * @returns true if the async thread is running, false otherwise. + */ + bool is_running() const; + + /** + * Return all results available so far + * + * All available results are moved from the internal map to the returned map + * so subsequent `lookup()` and/or `get_complete_results()` calls won't + * return them again. + * + * Sometimes there's no good place to call `lookup()` again + * on the async data source -- e.g. the container detection engine + * may never be called again for a particular container (if the only + * process in that container never calls `execve()` or `chroot()` + * or `clone()`). + * + * The best solution in that case is to supply a callback to be ran + * from the async lookup, but that introduces thread safety issues + * to the involved data. + * + * `get_complete_results()` allows batch processing of lookup results + * in the main thread. + * + * @return a map of lookup key -> result + */ + std::unordered_map get_complete_results(); + +protected: + /** + * Stops the thread associated with this async_key_value_source, if + * it is running; otherwise, does nothing. The only use for this is + * in a destructor to ensure that the async thread stops when the + * object is destroyed. + */ + void stop(); + + /** + * Dequeues an entry from the request queue and returns it in the given + * key. Concrete subclasses will call this method to get the next key + * for which to collect values. + * + * @returns true if there was a key to dequeue, false otherwise. + */ + bool dequeue_next_key(key_type& key); + + /** + * Get the (potentially partial) value for the given key. + * + * @param[in] key The key whose value is needed. + * + * @returns the value associated with the given key. + */ + value_type get_value(const key_type& key); + + /** + * Stores a value for the given key. Concrete subclasses will call + * this method from their run_impl() method to save (or otherwise + * notify the client about) an available value. + * + * @param[in] key The key for which the client asked for the value. + * @param[in] value The collected value. + */ + void store_value(const key_type& key, const value_type& value); + + /** + * Concrete subclasses must override this method to perform the + * asynchronous value lookup. The implementation should: + * + *
    + *
  • Loop while dequeue_next_key() is true.
  • + *
  • Get any existing value for that key using get_value()
  • + *
  • Do whatever work is necessary to lookup the value associated + * with that key.
  • + *
  • Call store_value to store the updated value, and to + * notify any client code waiting on that data.
  • + *
+ */ + virtual void run_impl() = 0; + + /** + * Determine the time to wait for the next request + * + * @return the absolute time until which run() may block while waiting + * for an incoming request + */ + std::chrono::steady_clock::time_point get_deadline() const; + +private: + /** + * Holds information associated with a single lookup() request. + */ + struct lookup_request + { + lookup_request(): + m_available(false), + m_value(), + m_available_condition(), + m_callback(), + m_start_time(std::chrono::steady_clock::now()) + { } + + lookup_request(const lookup_request& rhs) : + m_available(rhs.m_available), + m_value(rhs.m_value), + m_available_condition(/*not rhs*/), + m_callback(rhs.m_callback), + m_start_time(rhs.m_start_time) + { } + + /** Is the value here available? */ + bool m_available; + + /** The value for a key. */ + value_type m_value; + + /** Block in lookup() waiting for a sync response. */ + std::condition_variable m_available_condition; + + /** + * A optional client-specified callback handler for async + * response notification. + */ + callback_handler m_callback; + + /** The time at which this request was made. */ + std::chrono::time_point m_start_time; + }; + + typedef std::map value_map; + + /** + * The entry point of the async thread, which blocks waiting for work + * and dispatches work to run_impl(). + */ + void run(); + + /** + * Remove any entries that are older than the time-to-live. + */ + void prune_stale_requests(); + + uint64_t m_max_wait_ms; + uint64_t m_ttl_ms; + std::thread m_thread; + bool m_running; + bool m_terminate; + + /** + * Protects the state of instances of this class. This protected does + * not extend to subclasses (i.e., this mutex should not be held when + * dispatching to overridden methods). + */ + mutable std::mutex m_mutex; + + /** + * Enables run() to block waiting for the m_request_queue to become + * non-empty. + */ + std::condition_variable m_queue_not_empty_condition; + + using queue_item_t = std::pair, key_type>; + std::priority_queue, std::greater> m_request_queue; + std::set m_request_set; + value_map m_value_map; +}; + + +} // end namespace sysdig + +#include "async_key_value_source.tpp" diff --git a/userspace/async/async_key_value_source.tpp b/userspace/async/async_key_value_source.tpp new file mode 100644 index 0000000000..6d2ab43e10 --- /dev/null +++ b/userspace/async/async_key_value_source.tpp @@ -0,0 +1,388 @@ +/* +Copyright (C) 2019 Sysdig, Inc. + +This file is part of sysdig. + +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 "logger.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sysdig +{ + +template +async_key_value_source::async_key_value_source( + const uint64_t max_wait_ms, + const uint64_t ttl_ms) noexcept: + m_max_wait_ms(max_wait_ms), + m_ttl_ms(ttl_ms), + m_thread(), + m_running(false), + m_terminate(false), + m_mutex(), + m_queue_not_empty_condition(), + m_value_map() +{ } + +template +async_key_value_source::~async_key_value_source() +{ + try + { + stop(); + } + catch(...) + { + g_logger.log(std::string(__FUNCTION__) + + ": Exception in destructor", + sinsp_logger::SEV_ERROR); + } +} + +template +uint64_t async_key_value_source::get_max_wait() const +{ + return m_max_wait_ms; +} + +template +uint64_t async_key_value_source::get_ttl() const +{ + return m_ttl_ms; +} + +template +void async_key_value_source::stop() +{ + bool join_needed = false; + + { + std::unique_lock guard(m_mutex); + + if(m_running) + { + m_terminate = true; + join_needed = true; + + // The async thread might be waiting for new events + // so wake it up + m_queue_not_empty_condition.notify_one(); + } + } // Drop the mutex before join() + + if (join_needed) + { + m_thread.join(); + + // Remove any pointers from the thread to this object + // (just to be safe) + m_thread = std::thread(); + } +} + +template +bool async_key_value_source::is_running() const +{ + // Since this is for information only and it's ok to race, we + // explicitly do not lock here. + + return m_running; +} + +template +void async_key_value_source::run() +{ + m_running = true; + + while(!m_terminate) + { + { + std::unique_lock guard(m_mutex); + + while(!m_terminate) + { + // Wait for something to show up on the queue + auto deadline = get_deadline(); + if (deadline == std::chrono::steady_clock::time_point::min()) + { + break; + } + else if (deadline == std::chrono::steady_clock::time_point::max()) + { + // https://stackoverflow.com/questions/39041450/stdcondition-variable-wait-until-surprising-behaviour + m_queue_not_empty_condition.wait(guard); + } + else + { + m_queue_not_empty_condition.wait_until(guard, deadline); + } + } + + prune_stale_requests(); + } + + if(!m_terminate) + { + + run_impl(); + } + } + + m_running = false; +} + +template +bool async_key_value_source::lookup_delayed( + const key_type& key, + value_type& value, + std::chrono::milliseconds delay, + const callback_handler& handler) +{ + std::unique_lock guard(m_mutex); + + if(!m_running && !m_thread.joinable()) + { + m_thread = std::thread(&async_key_value_source::run, this); + } + + typename value_map::iterator itr = m_value_map.find(key); + bool request_complete; + + if (itr == m_value_map.end()) + { + // Haven't made the request yet. Be explicit and validate insertion. + auto insert_result = m_value_map.emplace(key, lookup_request()); + + if(!insert_result.second) + { + g_logger.log("async_key_value_source: Failed to insert an empty item " + "into the container cache.", sinsp_logger::SEV_ERROR); + return false; + } + + // Replace the itr with the mapped value + itr = insert_result.first; + + // Not sure why setting the value is needed, but being consistent with + // previous implementation. + itr->second.m_value = value; + + // Make request to API and let the async thread know about it + if (std::find(m_request_set.begin(), + m_request_set.end(), + key) == m_request_set.end()) + { + auto start_time = std::chrono::steady_clock::now() + delay; + m_request_queue.push(std::make_pair(start_time, key)); + m_request_set.insert(key); + m_queue_not_empty_condition.notify_one(); + } + request_complete = false; + } + else + { + request_complete = itr->second.m_available; + } + + if(!request_complete && m_max_wait_ms > 0) + { + // + // If the client code is willing to wait a short amount of time + // to satisfy the request, then wait for the async thread to + // pick up the newly-added request and execute it. If + // processing that request takes too much time, then we'll + // not be able to return the value information on this call, + // and the async thread will continue handling the request so + // that it'll be available on the next call. + // + itr->second.m_available_condition.wait_for( + guard, + std::chrono::milliseconds(m_max_wait_ms)); + + // Replace the iterator in case something changed + itr = m_value_map.find(key); + request_complete = (itr != m_value_map.end()) && itr->second.m_available; + } + + if(request_complete) + { + // Pass the value back the caller and erase from the list. + value = itr->second.m_value; + m_value_map.erase(itr); + } + else + { + // Set the callback to fill the value later + itr->second.m_callback = handler; + } + + return request_complete; +} + +template +bool async_key_value_source::lookup( + const key_type& key, + value_type& value, + const callback_handler& handler) +{ + return lookup_delayed(key, value, std::chrono::milliseconds::zero(), handler); +} + +template +bool async_key_value_source::dequeue_next_key(key_type& key) +{ + std::lock_guard guard(m_mutex); + bool key_found = false; + + if(!m_request_queue.empty()) + { + auto top_element = m_request_queue.top(); + if(top_element.first < std::chrono::steady_clock::now()) + { + key_found = true; + key = std::move(top_element.second); + m_request_queue.pop(); + m_request_set.erase(key); + } + } + + return key_found; +} + +template +value_type async_key_value_source::get_value( + const key_type& key) +{ + std::lock_guard guard(m_mutex); + + return m_value_map[key].m_value; +} + +template +void async_key_value_source::store_value( + const key_type& key, + const value_type& value) +{ + std::lock_guard guard(m_mutex); + + typename value_map::iterator itr = m_value_map.find(key); + if(itr == m_value_map.end()) + { + g_logger.log("async_key_value_source: Container not found when committing " + "to container cache. Either the container no longer exists or " + "the container lookup took longer than the timeout.", + sinsp_logger::SEV_WARNING); + return; + } + + if (itr->second.m_callback) + { + itr->second.m_callback(key, value); + m_value_map.erase(itr); + } + else + { + itr->second.m_value = value; + itr->second.m_available = true; + itr->second.m_available_condition.notify_one(); + } +} + +/** + * Prune any "old" outstanding requests. This method expects that the caller + * is holding m_mutex. + */ +template +void async_key_value_source::prune_stale_requests() +{ + // Avoid both iterating over and modifying the map by saving a list + // of keys to prune. + std::vector keys_to_prune; + + for(auto i = m_value_map.begin(); + !m_terminate && (i != m_value_map.end()); + ++i) + { + const auto now = std::chrono::steady_clock::now(); + + const uint64_t age_ms = + std::chrono::duration_cast( + now - i->second.m_start_time).count(); + + if(age_ms > m_ttl_ms) + { + keys_to_prune.push_back(i->first); + } + } + + for(auto i = keys_to_prune.begin(); + !m_terminate && (i != keys_to_prune.end()); + ++i) + { + m_value_map.erase(*i); + } +} + +template +std::unordered_map async_key_value_source::get_complete_results() +{ + std::unordered_map results; + + std::lock_guard guard(m_mutex); + + for(const auto& it : m_value_map) + { + if(it.second.m_available) + { + results[it.first] = it.second.m_value; + } + } + + for(const auto& it : results) + { + m_value_map.erase(it.first); + } + + return results; +} + +// called with m_mutex held +template +std::chrono::steady_clock::time_point async_key_value_source::get_deadline() const +{ + if (m_request_queue.empty()) + { + return std::chrono::steady_clock::time_point::max(); + } + + auto next_request = m_request_queue.top(); + if (next_request.first <= std::chrono::steady_clock::now()) + { + return std::chrono::steady_clock::time_point::min(); + } + + return next_request.first; +} + +} // end namespace sysdig + diff --git a/userspace/common/sysdig_types.h b/userspace/common/sysdig_types.h index 705e4407af..27c7a6a7e8 100644 --- a/userspace/common/sysdig_types.h +++ b/userspace/common/sysdig_types.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #ifdef _WIN32 @@ -30,3 +31,19 @@ along with sysdig. If not, see . #include #include #endif /* _WIN32 */ + +// +// Import/typedef in userspace the kernel types +// +#if defined(__linux__) && !defined(UDIG) +#include +#else +typedef unsigned long long __u64; +typedef long long __s64; +typedef uint32_t __u32; +typedef int32_t __s32; +typedef uint16_t __u16; +typedef int16_t __s16; +typedef uint8_t __u8; +typedef int8_t __s8; +#endif diff --git a/userspace/libscap/CMakeLists.txt b/userspace/libscap/CMakeLists.txt index bb512ad5f2..d005c75c9b 100644 --- a/userspace/libscap/CMakeLists.txt +++ b/userspace/libscap/CMakeLists.txt @@ -1,18 +1,96 @@ -include_directories(${PROJECT_SOURCE_DIR}/common) - -add_library(scap STATIC - scap.c - scap_event.c - scap_fds.c - scap_iflist.c - scap_savefile.c - scap_procs.c - scap_userlist.c - flags_table.c - event_table.c - syscall_info_table.c) - -if (WIN32) +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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_directories("${PROJECT_SOURCE_DIR}/common") +if(NOT MINIMAL_BUILD) +include_directories("${ZLIB_INCLUDE}") +endif() +if(CYGWIN) +include_directories("${WIN_HAL_INCLUDE}") +endif() + +list(APPEND targetfiles + scap.c + scap_event.c + scap_fds.c + scap_iflist.c + scap_savefile.c + scap_procs.c + scap_userlist.c + syscall_info_table.c + ../../driver/dynamic_params_table.c + ../../driver/event_table.c + ../../driver/flags_table.c) + +if(NOT APPLE) + list(APPEND targetfiles + scap_udig.c) +endif() + + +if(CMAKE_SYSTEM_NAME MATCHES "Linux") + list(APPEND targetfiles + scap_bpf.c + ../../driver/syscall_table.c + ../../driver/fillers_table.c) + + include_directories(${PROJECT_BINARY_DIR}/driver/src) +endif() + +if(CYGWIN) + list(APPEND targetfiles + windows_hal.c) +endif() + +add_library(scap STATIC + ${targetfiles}) + +if(USE_BUNDLED_ZLIB AND NOT MINIMAL_BUILD) + add_dependencies(scap zlib) +endif() + +if (CMAKE_SYSTEM_NAME MATCHES "SunOS") + target_link_libraries(scap + socket nsl) +elseif (CMAKE_SYSTEM_NAME MATCHES "Linux") + target_link_libraries(scap + elf + rt) +elseif (WIN32) target_link_libraries(scap Ws2_32.lib) +elseif (CYGWIN) + target_link_libraries(scap + /lib/w32api/libpsapi.a + ${WIN_HAL_LIB}/dragent_win_hal.lib) +endif() + +if(NOT MINIMAL_BUILD) +target_link_libraries(scap + "${ZLIB_LIB}") +endif() + +if(CMAKE_SYSTEM_NAME MATCHES "Linux") + option(BUILD_LIBSCAP_EXAMPLES "Build libscap examples" ON) + + if (BUILD_LIBSCAP_EXAMPLES) + add_subdirectory(examples/01-open) + add_subdirectory(examples/02-validatebuffer) + endif() + + include(FindMakedev) endif() diff --git a/userspace/libscap/compat/bpf.h b/userspace/libscap/compat/bpf.h new file mode 100644 index 0000000000..e72116afa4 --- /dev/null +++ b/userspace/libscap/compat/bpf.h @@ -0,0 +1,2807 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + */ +#ifndef _UAPI__LINUX_BPF_H__ +#define _UAPI__LINUX_BPF_H__ + +#include +#include "bpf_common.h" + +/* Extended instruction set based on top of classic BPF */ + +/* instruction classes */ +#define BPF_ALU64 0x07 /* alu mode in double word width */ + +/* ld/ldx fields */ +#define BPF_DW 0x18 /* double word (64-bit) */ +#define BPF_XADD 0xc0 /* exclusive add */ + +/* alu/jmp fields */ +#define BPF_MOV 0xb0 /* mov reg to reg */ +#define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */ + +/* change endianness of a register */ +#define BPF_END 0xd0 /* flags for endianness conversion: */ +#define BPF_TO_LE 0x00 /* convert to little-endian */ +#define BPF_TO_BE 0x08 /* convert to big-endian */ +#define BPF_FROM_LE BPF_TO_LE +#define BPF_FROM_BE BPF_TO_BE + +/* jmp encodings */ +#define BPF_JNE 0x50 /* jump != */ +#define BPF_JLT 0xa0 /* LT is unsigned, '<' */ +#define BPF_JLE 0xb0 /* LE is unsigned, '<=' */ +#define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */ +#define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */ +#define BPF_JSLT 0xc0 /* SLT is signed, '<' */ +#define BPF_JSLE 0xd0 /* SLE is signed, '<=' */ +#define BPF_CALL 0x80 /* function call */ +#define BPF_EXIT 0x90 /* function return */ + +/* Register numbers */ +enum { + BPF_REG_0 = 0, + BPF_REG_1, + BPF_REG_2, + BPF_REG_3, + BPF_REG_4, + BPF_REG_5, + BPF_REG_6, + BPF_REG_7, + BPF_REG_8, + BPF_REG_9, + BPF_REG_10, + __MAX_BPF_REG, +}; + +/* BPF has 10 general purpose 64-bit registers and stack frame. */ +#define MAX_BPF_REG __MAX_BPF_REG + +struct bpf_insn { + __u8 code; /* opcode */ + __u8 dst_reg:4; /* dest register */ + __u8 src_reg:4; /* source register */ + __s16 off; /* signed offset */ + __s32 imm; /* signed immediate constant */ +}; + +/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */ +struct bpf_lpm_trie_key { + __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */ + __u8 data[0]; /* Arbitrary size */ +}; + +struct bpf_cgroup_storage_key { + __u64 cgroup_inode_id; /* cgroup inode id */ + __u32 attach_type; /* program attach type */ +}; + +/* BPF syscall commands, see bpf(2) man-page for details. */ +enum bpf_cmd { + BPF_MAP_CREATE, + BPF_MAP_LOOKUP_ELEM, + BPF_MAP_UPDATE_ELEM, + BPF_MAP_DELETE_ELEM, + BPF_MAP_GET_NEXT_KEY, + BPF_PROG_LOAD, + BPF_OBJ_PIN, + BPF_OBJ_GET, + BPF_PROG_ATTACH, + BPF_PROG_DETACH, + BPF_PROG_TEST_RUN, + BPF_PROG_GET_NEXT_ID, + BPF_MAP_GET_NEXT_ID, + BPF_PROG_GET_FD_BY_ID, + BPF_MAP_GET_FD_BY_ID, + BPF_OBJ_GET_INFO_BY_FD, + BPF_PROG_QUERY, + BPF_RAW_TRACEPOINT_OPEN, + BPF_BTF_LOAD, + BPF_BTF_GET_FD_BY_ID, + BPF_TASK_FD_QUERY, +}; + +enum bpf_map_type { + BPF_MAP_TYPE_UNSPEC, + BPF_MAP_TYPE_HASH, + BPF_MAP_TYPE_ARRAY, + BPF_MAP_TYPE_PROG_ARRAY, + BPF_MAP_TYPE_PERF_EVENT_ARRAY, + BPF_MAP_TYPE_PERCPU_HASH, + BPF_MAP_TYPE_PERCPU_ARRAY, + BPF_MAP_TYPE_STACK_TRACE, + BPF_MAP_TYPE_CGROUP_ARRAY, + BPF_MAP_TYPE_LRU_HASH, + BPF_MAP_TYPE_LRU_PERCPU_HASH, + BPF_MAP_TYPE_LPM_TRIE, + BPF_MAP_TYPE_ARRAY_OF_MAPS, + BPF_MAP_TYPE_HASH_OF_MAPS, + BPF_MAP_TYPE_DEVMAP, + BPF_MAP_TYPE_SOCKMAP, + BPF_MAP_TYPE_CPUMAP, + BPF_MAP_TYPE_XSKMAP, + BPF_MAP_TYPE_SOCKHASH, + BPF_MAP_TYPE_CGROUP_STORAGE, + BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, +}; + +enum bpf_prog_type { + BPF_PROG_TYPE_UNSPEC, + BPF_PROG_TYPE_SOCKET_FILTER, + BPF_PROG_TYPE_KPROBE, + BPF_PROG_TYPE_SCHED_CLS, + BPF_PROG_TYPE_SCHED_ACT, + BPF_PROG_TYPE_TRACEPOINT, + BPF_PROG_TYPE_XDP, + BPF_PROG_TYPE_PERF_EVENT, + BPF_PROG_TYPE_CGROUP_SKB, + BPF_PROG_TYPE_CGROUP_SOCK, + BPF_PROG_TYPE_LWT_IN, + BPF_PROG_TYPE_LWT_OUT, + BPF_PROG_TYPE_LWT_XMIT, + BPF_PROG_TYPE_SOCK_OPS, + BPF_PROG_TYPE_SK_SKB, + BPF_PROG_TYPE_CGROUP_DEVICE, + BPF_PROG_TYPE_SK_MSG, + BPF_PROG_TYPE_RAW_TRACEPOINT, + BPF_PROG_TYPE_CGROUP_SOCK_ADDR, + BPF_PROG_TYPE_LWT_SEG6LOCAL, + BPF_PROG_TYPE_LIRC_MODE2, + BPF_PROG_TYPE_SK_REUSEPORT, + BPF_PROG_TYPE_FLOW_DISSECTOR, +}; + +enum bpf_attach_type { + BPF_CGROUP_INET_INGRESS, + BPF_CGROUP_INET_EGRESS, + BPF_CGROUP_INET_SOCK_CREATE, + BPF_CGROUP_SOCK_OPS, + BPF_SK_SKB_STREAM_PARSER, + BPF_SK_SKB_STREAM_VERDICT, + BPF_CGROUP_DEVICE, + BPF_SK_MSG_VERDICT, + BPF_CGROUP_INET4_BIND, + BPF_CGROUP_INET6_BIND, + BPF_CGROUP_INET4_CONNECT, + BPF_CGROUP_INET6_CONNECT, + BPF_CGROUP_INET4_POST_BIND, + BPF_CGROUP_INET6_POST_BIND, + BPF_CGROUP_UDP4_SENDMSG, + BPF_CGROUP_UDP6_SENDMSG, + BPF_LIRC_MODE2, + BPF_FLOW_DISSECTOR, + __MAX_BPF_ATTACH_TYPE +}; + +#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE + +/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command + * + * NONE(default): No further bpf programs allowed in the subtree. + * + * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program, + * the program in this cgroup yields to sub-cgroup program. + * + * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program, + * that cgroup program gets run in addition to the program in this cgroup. + * + * Only one program is allowed to be attached to a cgroup with + * NONE or BPF_F_ALLOW_OVERRIDE flag. + * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will + * release old program and attach the new one. Attach flags has to match. + * + * Multiple programs are allowed to be attached to a cgroup with + * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order + * (those that were attached first, run first) + * The programs of sub-cgroup are executed first, then programs of + * this cgroup and then programs of parent cgroup. + * When children program makes decision (like picking TCP CA or sock bind) + * parent program has a chance to override it. + * + * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups. + * A cgroup with NONE doesn't allow any programs in sub-cgroups. + * Ex1: + * cgrp1 (MULTI progs A, B) -> + * cgrp2 (OVERRIDE prog C) -> + * cgrp3 (MULTI prog D) -> + * cgrp4 (OVERRIDE prog E) -> + * cgrp5 (NONE prog F) + * the event in cgrp5 triggers execution of F,D,A,B in that order. + * if prog F is detached, the execution is E,D,A,B + * if prog F and D are detached, the execution is E,A,B + * if prog F, E and D are detached, the execution is C,A,B + * + * All eligible programs are executed regardless of return code from + * earlier programs. + */ +#define BPF_F_ALLOW_OVERRIDE (1U << 0) +#define BPF_F_ALLOW_MULTI (1U << 1) + +/* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the + * verifier will perform strict alignment checking as if the kernel + * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, + * and NET_IP_ALIGN defined to 2. + */ +#define BPF_F_STRICT_ALIGNMENT (1U << 0) + +/* when bpf_ldimm64->src_reg == BPF_PSEUDO_MAP_FD, bpf_ldimm64->imm == fd */ +#define BPF_PSEUDO_MAP_FD 1 + +/* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative + * offset to another bpf function + */ +#define BPF_PSEUDO_CALL 1 + +/* flags for BPF_MAP_UPDATE_ELEM command */ +#define BPF_ANY 0 /* create new element or update existing */ +#define BPF_NOEXIST 1 /* create new element if it didn't exist */ +#define BPF_EXIST 2 /* update existing element */ + +/* flags for BPF_MAP_CREATE command */ +#define BPF_F_NO_PREALLOC (1U << 0) +/* Instead of having one common LRU list in the + * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list + * which can scale and perform better. + * Note, the LRU nodes (including free nodes) cannot be moved + * across different LRU lists. + */ +#define BPF_F_NO_COMMON_LRU (1U << 1) +/* Specify numa node during map creation */ +#define BPF_F_NUMA_NODE (1U << 2) + +/* flags for BPF_PROG_QUERY */ +#define BPF_F_QUERY_EFFECTIVE (1U << 0) + +#define BPF_OBJ_NAME_LEN 16U + +/* Flags for accessing BPF object */ +#define BPF_F_RDONLY (1U << 3) +#define BPF_F_WRONLY (1U << 4) + +/* Flag for stack_map, store build_id+offset instead of pointer */ +#define BPF_F_STACK_BUILD_ID (1U << 5) + +enum bpf_stack_build_id_status { + /* user space need an empty entry to identify end of a trace */ + BPF_STACK_BUILD_ID_EMPTY = 0, + /* with valid build_id and offset */ + BPF_STACK_BUILD_ID_VALID = 1, + /* couldn't get build_id, fallback to ip */ + BPF_STACK_BUILD_ID_IP = 2, +}; + +#define BPF_BUILD_ID_SIZE 20 +struct bpf_stack_build_id { + __s32 status; + unsigned char build_id[BPF_BUILD_ID_SIZE]; + union { + __u64 offset; + __u64 ip; + }; +}; + +union bpf_attr { + struct { /* anonymous struct used by BPF_MAP_CREATE command */ + __u32 map_type; /* one of enum bpf_map_type */ + __u32 key_size; /* size of key in bytes */ + __u32 value_size; /* size of value in bytes */ + __u32 max_entries; /* max number of entries in a map */ + __u32 map_flags; /* BPF_MAP_CREATE related + * flags defined above. + */ + __u32 inner_map_fd; /* fd pointing to the inner map */ + __u32 numa_node; /* numa node (effective only if + * BPF_F_NUMA_NODE is set). + */ + char map_name[BPF_OBJ_NAME_LEN]; + __u32 map_ifindex; /* ifindex of netdev to create on */ + __u32 btf_fd; /* fd pointing to a BTF type data */ + __u32 btf_key_type_id; /* BTF type_id of the key */ + __u32 btf_value_type_id; /* BTF type_id of the value */ + }; + + struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ + __u32 map_fd; + __aligned_u64 key; + union { + __aligned_u64 value; + __aligned_u64 next_key; + }; + __u64 flags; + }; + + struct { /* anonymous struct used by BPF_PROG_LOAD command */ + __u32 prog_type; /* one of enum bpf_prog_type */ + __u32 insn_cnt; + __aligned_u64 insns; + __aligned_u64 license; + __u32 log_level; /* verbosity level of verifier */ + __u32 log_size; /* size of user buffer */ + __aligned_u64 log_buf; /* user supplied buffer */ + __u32 kern_version; /* checked when prog_type=kprobe */ + __u32 prog_flags; + char prog_name[BPF_OBJ_NAME_LEN]; + __u32 prog_ifindex; /* ifindex of netdev to prep for */ + /* For some prog types expected attach type must be known at + * load time to verify attach type specific parts of prog + * (context accesses, allowed helpers, etc). + */ + __u32 expected_attach_type; + }; + + struct { /* anonymous struct used by BPF_OBJ_* commands */ + __aligned_u64 pathname; + __u32 bpf_fd; + __u32 file_flags; + }; + + struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ + __u32 target_fd; /* container object to attach to */ + __u32 attach_bpf_fd; /* eBPF program to attach */ + __u32 attach_type; + __u32 attach_flags; + }; + + struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ + __u32 prog_fd; + __u32 retval; + __u32 data_size_in; + __u32 data_size_out; + __aligned_u64 data_in; + __aligned_u64 data_out; + __u32 repeat; + __u32 duration; + } test; + + struct { /* anonymous struct used by BPF_*_GET_*_ID */ + union { + __u32 start_id; + __u32 prog_id; + __u32 map_id; + __u32 btf_id; + }; + __u32 next_id; + __u32 open_flags; + }; + + struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ + __u32 bpf_fd; + __u32 info_len; + __aligned_u64 info; + } info; + + struct { /* anonymous struct used by BPF_PROG_QUERY command */ + __u32 target_fd; /* container object to query */ + __u32 attach_type; + __u32 query_flags; + __u32 attach_flags; + __aligned_u64 prog_ids; + __u32 prog_cnt; + } query; + + struct { + __u64 name; + __u32 prog_fd; + } raw_tracepoint; + + struct { /* anonymous struct for BPF_BTF_LOAD */ + __aligned_u64 btf; + __aligned_u64 btf_log_buf; + __u32 btf_size; + __u32 btf_log_size; + __u32 btf_log_level; + }; + + struct { + __u32 pid; /* input: pid */ + __u32 fd; /* input: fd */ + __u32 flags; /* input: flags */ + __u32 buf_len; /* input/output: buf len */ + __aligned_u64 buf; /* input/output: + * tp_name for tracepoint + * symbol for kprobe + * filename for uprobe + */ + __u32 prog_id; /* output: prod_id */ + __u32 fd_type; /* output: BPF_FD_TYPE_* */ + __u64 probe_offset; /* output: probe_offset */ + __u64 probe_addr; /* output: probe_addr */ + } task_fd_query; +} __attribute__((aligned(8))); + +/* The description below is an attempt at providing documentation to eBPF + * developers about the multiple available eBPF helper functions. It can be + * parsed and used to produce a manual page. The workflow is the following, + * and requires the rst2man utility: + * + * $ ./scripts/bpf_helpers_doc.py \ + * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst + * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7 + * $ man /tmp/bpf-helpers.7 + * + * Note that in order to produce this external documentation, some RST + * formatting is used in the descriptions to get "bold" and "italics" in + * manual pages. Also note that the few trailing white spaces are + * intentional, removing them would break paragraphs for rst2man. + * + * Start of BPF helper function descriptions: + * + * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key) + * Description + * Perform a lookup in *map* for an entry associated to *key*. + * Return + * Map value associated to *key*, or **NULL** if no entry was + * found. + * + * int bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags) + * Description + * Add or update the value of the entry associated to *key* in + * *map* with *value*. *flags* is one of: + * + * **BPF_NOEXIST** + * The entry for *key* must not exist in the map. + * **BPF_EXIST** + * The entry for *key* must already exist in the map. + * **BPF_ANY** + * No condition on the existence of the entry for *key*. + * + * Flag value **BPF_NOEXIST** cannot be used for maps of types + * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all + * elements always exist), the helper would return an error. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_map_delete_elem(struct bpf_map *map, const void *key) + * Description + * Delete entry with *key* from *map*. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_probe_read(void *dst, u32 size, const void *src) + * Description + * For tracing programs, safely attempt to read *size* bytes from + * address *src* and store the data in *dst*. + * Return + * 0 on success, or a negative error in case of failure. + * + * u64 bpf_ktime_get_ns(void) + * Description + * Return the time elapsed since system boot, in nanoseconds. + * Return + * Current *ktime*. + * + * int bpf_trace_printk(const char *fmt, u32 fmt_size, ...) + * Description + * This helper is a "printk()-like" facility for debugging. It + * prints a message defined by format *fmt* (of size *fmt_size*) + * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if + * available. It can take up to three additional **u64** + * arguments (as an eBPF helpers, the total number of arguments is + * limited to five). + * + * Each time the helper is called, it appends a line to the trace. + * The format of the trace is customizable, and the exact output + * one will get depends on the options set in + * *\/sys/kernel/debug/tracing/trace_options* (see also the + * *README* file under the same directory). However, it usually + * defaults to something like: + * + * :: + * + * telnet-470 [001] .N.. 419421.045894: 0x00000001: + * + * In the above: + * + * * ``telnet`` is the name of the current task. + * * ``470`` is the PID of the current task. + * * ``001`` is the CPU number on which the task is + * running. + * * In ``.N..``, each character refers to a set of + * options (whether irqs are enabled, scheduling + * options, whether hard/softirqs are running, level of + * preempt_disabled respectively). **N** means that + * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED** + * are set. + * * ``419421.045894`` is a timestamp. + * * ``0x00000001`` is a fake value used by BPF for the + * instruction pointer register. + * * ```` is the message formatted with + * *fmt*. + * + * The conversion specifiers supported by *fmt* are similar, but + * more limited than for printk(). They are **%d**, **%i**, + * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**, + * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size + * of field, padding with zeroes, etc.) is available, and the + * helper will return **-EINVAL** (but print nothing) if it + * encounters an unknown specifier. + * + * Also, note that **bpf_trace_printk**\ () is slow, and should + * only be used for debugging purposes. For this reason, a notice + * bloc (spanning several lines) is printed to kernel logs and + * states that the helper should not be used "for production use" + * the first time this helper is used (or more precisely, when + * **trace_printk**\ () buffers are allocated). For passing values + * to user space, perf events should be preferred. + * Return + * The number of bytes written to the buffer, or a negative error + * in case of failure. + * + * u32 bpf_get_prandom_u32(void) + * Description + * Get a pseudo-random number. + * + * From a security point of view, this helper uses its own + * pseudo-random internal state, and cannot be used to infer the + * seed of other random functions in the kernel. However, it is + * essential to note that the generator used by the helper is not + * cryptographically secure. + * Return + * A random 32-bit unsigned value. + * + * u32 bpf_get_smp_processor_id(void) + * Description + * Get the SMP (symmetric multiprocessing) processor id. Note that + * all programs run with preemption disabled, which means that the + * SMP processor id is stable during all the execution of the + * program. + * Return + * The SMP id of the processor running the program. + * + * int bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags) + * Description + * Store *len* bytes from address *from* into the packet + * associated to *skb*, at *offset*. *flags* are a combination of + * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the + * checksum for the packet after storing the bytes) and + * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\ + * **->swhash** and *skb*\ **->l4hash** to 0). + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size) + * Description + * Recompute the layer 3 (e.g. IP) checksum for the packet + * associated to *skb*. Computation is incremental, so the helper + * must know the former value of the header field that was + * modified (*from*), the new value of this field (*to*), and the + * number of bytes (2 or 4) for this field, stored in *size*. + * Alternatively, it is possible to store the difference between + * the previous and the new values of the header field in *to*, by + * setting *from* and *size* to 0. For both methods, *offset* + * indicates the location of the IP checksum within the packet. + * + * This helper works in combination with **bpf_csum_diff**\ (), + * which does not update the checksum in-place, but offers more + * flexibility and can handle sizes larger than 2 or 4 for the + * checksum to update. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags) + * Description + * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the + * packet associated to *skb*. Computation is incremental, so the + * helper must know the former value of the header field that was + * modified (*from*), the new value of this field (*to*), and the + * number of bytes (2 or 4) for this field, stored on the lowest + * four bits of *flags*. Alternatively, it is possible to store + * the difference between the previous and the new values of the + * header field in *to*, by setting *from* and the four lowest + * bits of *flags* to 0. For both methods, *offset* indicates the + * location of the IP checksum within the packet. In addition to + * the size of the field, *flags* can be added (bitwise OR) actual + * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left + * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and + * for updates resulting in a null checksum the value is set to + * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates + * the checksum is to be computed against a pseudo-header. + * + * This helper works in combination with **bpf_csum_diff**\ (), + * which does not update the checksum in-place, but offers more + * flexibility and can handle sizes larger than 2 or 4 for the + * checksum to update. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index) + * Description + * This special helper is used to trigger a "tail call", or in + * other words, to jump into another eBPF program. The same stack + * frame is used (but values on stack and in registers for the + * caller are not accessible to the callee). This mechanism allows + * for program chaining, either for raising the maximum number of + * available eBPF instructions, or to execute given programs in + * conditional blocks. For security reasons, there is an upper + * limit to the number of successive tail calls that can be + * performed. + * + * Upon call of this helper, the program attempts to jump into a + * program referenced at index *index* in *prog_array_map*, a + * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes + * *ctx*, a pointer to the context. + * + * If the call succeeds, the kernel immediately runs the first + * instruction of the new program. This is not a function call, + * and it never returns to the previous program. If the call + * fails, then the helper has no effect, and the caller continues + * to run its subsequent instructions. A call can fail if the + * destination program for the jump does not exist (i.e. *index* + * is superior to the number of entries in *prog_array_map*), or + * if the maximum number of tail calls has been reached for this + * chain of programs. This limit is defined in the kernel by the + * macro **MAX_TAIL_CALL_CNT** (not accessible to user space), + * which is currently set to 32. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags) + * Description + * Clone and redirect the packet associated to *skb* to another + * net device of index *ifindex*. Both ingress and egress + * interfaces can be used for redirection. The **BPF_F_INGRESS** + * value in *flags* is used to make the distinction (ingress path + * is selected if the flag is present, egress path otherwise). + * This is the only flag supported for now. + * + * In comparison with **bpf_redirect**\ () helper, + * **bpf_clone_redirect**\ () has the associated cost of + * duplicating the packet buffer, but this can be executed out of + * the eBPF program. Conversely, **bpf_redirect**\ () is more + * efficient, but it is handled through an action code where the + * redirection happens only after the eBPF program has returned. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * u64 bpf_get_current_pid_tgid(void) + * Return + * A 64-bit integer containing the current tgid and pid, and + * created as such: + * *current_task*\ **->tgid << 32 \|** + * *current_task*\ **->pid**. + * + * u64 bpf_get_current_uid_gid(void) + * Return + * A 64-bit integer containing the current GID and UID, and + * created as such: *current_gid* **<< 32 \|** *current_uid*. + * + * int bpf_get_current_comm(char *buf, u32 size_of_buf) + * Description + * Copy the **comm** attribute of the current task into *buf* of + * *size_of_buf*. The **comm** attribute contains the name of + * the executable (excluding the path) for the current task. The + * *size_of_buf* must be strictly positive. On success, the + * helper makes sure that the *buf* is NUL-terminated. On failure, + * it is filled with zeroes. + * Return + * 0 on success, or a negative error in case of failure. + * + * u32 bpf_get_cgroup_classid(struct sk_buff *skb) + * Description + * Retrieve the classid for the current task, i.e. for the net_cls + * cgroup to which *skb* belongs. + * + * This helper can be used on TC egress path, but not on ingress. + * + * The net_cls cgroup provides an interface to tag network packets + * based on a user-provided identifier for all traffic coming from + * the tasks belonging to the related cgroup. See also the related + * kernel documentation, available from the Linux sources in file + * *Documentation/cgroup-v1/net_cls.txt*. + * + * The Linux kernel has two versions for cgroups: there are + * cgroups v1 and cgroups v2. Both are available to users, who can + * use a mixture of them, but note that the net_cls cgroup is for + * cgroup v1 only. This makes it incompatible with BPF programs + * run on cgroups, which is a cgroup-v2-only feature (a socket can + * only hold data for one version of cgroups at a time). + * + * This helper is only available is the kernel was compiled with + * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to + * "**y**" or to "**m**". + * Return + * The classid, or 0 for the default unconfigured classid. + * + * int bpf_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) + * Description + * Push a *vlan_tci* (VLAN tag control information) of protocol + * *vlan_proto* to the packet associated to *skb*, then update + * the checksum. Note that if *vlan_proto* is different from + * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to + * be **ETH_P_8021Q**. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_vlan_pop(struct sk_buff *skb) + * Description + * Pop a VLAN header from the packet associated to *skb*. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_get_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) + * Description + * Get tunnel metadata. This helper takes a pointer *key* to an + * empty **struct bpf_tunnel_key** of **size**, that will be + * filled with tunnel metadata for the packet associated to *skb*. + * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which + * indicates that the tunnel is based on IPv6 protocol instead of + * IPv4. + * + * The **struct bpf_tunnel_key** is an object that generalizes the + * principal parameters used by various tunneling protocols into a + * single struct. This way, it can be used to easily make a + * decision based on the contents of the encapsulation header, + * "summarized" in this struct. In particular, it holds the IP + * address of the remote end (IPv4 or IPv6, depending on the case) + * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also, + * this struct exposes the *key*\ **->tunnel_id**, which is + * generally mapped to a VNI (Virtual Network Identifier), making + * it programmable together with the **bpf_skb_set_tunnel_key**\ + * () helper. + * + * Let's imagine that the following code is part of a program + * attached to the TC ingress interface, on one end of a GRE + * tunnel, and is supposed to filter out all messages coming from + * remote ends with IPv4 address other than 10.0.0.1: + * + * :: + * + * int ret; + * struct bpf_tunnel_key key = {}; + * + * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); + * if (ret < 0) + * return TC_ACT_SHOT; // drop packet + * + * if (key.remote_ipv4 != 0x0a000001) + * return TC_ACT_SHOT; // drop packet + * + * return TC_ACT_OK; // accept packet + * + * This interface can also be used with all encapsulation devices + * that can operate in "collect metadata" mode: instead of having + * one network device per specific configuration, the "collect + * metadata" mode only requires a single device where the + * configuration can be extracted from this helper. + * + * This can be used together with various tunnels such as VXLan, + * Geneve, GRE or IP in IP (IPIP). + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_set_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags) + * Description + * Populate tunnel metadata for packet associated to *skb.* The + * tunnel metadata is set to the contents of *key*, of *size*. The + * *flags* can be set to a combination of the following values: + * + * **BPF_F_TUNINFO_IPV6** + * Indicate that the tunnel is based on IPv6 protocol + * instead of IPv4. + * **BPF_F_ZERO_CSUM_TX** + * For IPv4 packets, add a flag to tunnel metadata + * indicating that checksum computation should be skipped + * and checksum set to zeroes. + * **BPF_F_DONT_FRAGMENT** + * Add a flag to tunnel metadata indicating that the + * packet should not be fragmented. + * **BPF_F_SEQ_NUMBER** + * Add a flag to tunnel metadata indicating that a + * sequence number should be added to tunnel header before + * sending the packet. This flag was added for GRE + * encapsulation, but might be used with other protocols + * as well in the future. + * + * Here is a typical usage on the transmit path: + * + * :: + * + * struct bpf_tunnel_key key; + * populate key ... + * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0); + * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0); + * + * See also the description of the **bpf_skb_get_tunnel_key**\ () + * helper for additional information. + * Return + * 0 on success, or a negative error in case of failure. + * + * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags) + * Description + * Read the value of a perf event counter. This helper relies on a + * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of + * the perf event counter is selected when *map* is updated with + * perf event file descriptors. The *map* is an array whose size + * is the number of available CPUs, and each cell contains a value + * relative to one CPU. The value to retrieve is indicated by + * *flags*, that contains the index of the CPU to look up, masked + * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to + * **BPF_F_CURRENT_CPU** to indicate that the value for the + * current CPU should be retrieved. + * + * Note that before Linux 4.13, only hardware perf event can be + * retrieved. + * + * Also, be aware that the newer helper + * **bpf_perf_event_read_value**\ () is recommended over + * **bpf_perf_event_read**\ () in general. The latter has some ABI + * quirks where error and counter value are used as a return code + * (which is wrong to do since ranges may overlap). This issue is + * fixed with **bpf_perf_event_read_value**\ (), which at the same + * time provides more features over the **bpf_perf_event_read**\ + * () interface. Please refer to the description of + * **bpf_perf_event_read_value**\ () for details. + * Return + * The value of the perf event counter read from the map, or a + * negative error code in case of failure. + * + * int bpf_redirect(u32 ifindex, u64 flags) + * Description + * Redirect the packet to another net device of index *ifindex*. + * This helper is somewhat similar to **bpf_clone_redirect**\ + * (), except that the packet is not cloned, which provides + * increased performance. + * + * Except for XDP, both ingress and egress interfaces can be used + * for redirection. The **BPF_F_INGRESS** value in *flags* is used + * to make the distinction (ingress path is selected if the flag + * is present, egress path otherwise). Currently, XDP only + * supports redirection to the egress interface, and accepts no + * flag at all. + * + * The same effect can be attained with the more generic + * **bpf_redirect_map**\ (), which requires specific maps to be + * used but offers better performance. + * Return + * For XDP, the helper returns **XDP_REDIRECT** on success or + * **XDP_ABORTED** on error. For other program types, the values + * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on + * error. + * + * u32 bpf_get_route_realm(struct sk_buff *skb) + * Description + * Retrieve the realm or the route, that is to say the + * **tclassid** field of the destination for the *skb*. The + * identifier retrieved is a user-provided tag, similar to the + * one used with the net_cls cgroup (see description for + * **bpf_get_cgroup_classid**\ () helper), but here this tag is + * held by a route (a destination entry), not by a task. + * + * Retrieving this identifier works with the clsact TC egress hook + * (see also **tc-bpf(8)**), or alternatively on conventional + * classful egress qdiscs, but not on TC ingress path. In case of + * clsact TC egress hook, this has the advantage that, internally, + * the destination entry has not been dropped yet in the transmit + * path. Therefore, the destination entry does not need to be + * artificially held via **netif_keep_dst**\ () for a classful + * qdisc until the *skb* is freed. + * + * This helper is available only if the kernel was compiled with + * **CONFIG_IP_ROUTE_CLASSID** configuration option. + * Return + * The realm of the route for the packet associated to *skb*, or 0 + * if none was found. + * + * int bpf_perf_event_output(struct pt_reg *ctx, struct bpf_map *map, u64 flags, void *data, u64 size) + * Description + * Write raw *data* blob into a special BPF perf event held by + * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf + * event must have the following attributes: **PERF_SAMPLE_RAW** + * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and + * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. + * + * The *flags* are used to indicate the index in *map* for which + * the value must be put, masked with **BPF_F_INDEX_MASK**. + * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** + * to indicate that the index of the current CPU core should be + * used. + * + * The value to write, of *size*, is passed through eBPF stack and + * pointed by *data*. + * + * The context of the program *ctx* needs also be passed to the + * helper. + * + * On user space, a program willing to read the values needs to + * call **perf_event_open**\ () on the perf event (either for + * one or for all CPUs) and to store the file descriptor into the + * *map*. This must be done before the eBPF program can send data + * into it. An example is available in file + * *samples/bpf/trace_output_user.c* in the Linux kernel source + * tree (the eBPF program counterpart is in + * *samples/bpf/trace_output_kern.c*). + * + * **bpf_perf_event_output**\ () achieves better performance + * than **bpf_trace_printk**\ () for sharing data with user + * space, and is much better suitable for streaming data from eBPF + * programs. + * + * Note that this helper is not restricted to tracing use cases + * and can be used with programs attached to TC or XDP as well, + * where it allows for passing data to user space listeners. Data + * can be: + * + * * Only custom structs, + * * Only the packet payload, or + * * A combination of both. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len) + * Description + * This helper was provided as an easy way to load data from a + * packet. It can be used to load *len* bytes from *offset* from + * the packet associated to *skb*, into the buffer pointed by + * *to*. + * + * Since Linux 4.7, usage of this helper has mostly been replaced + * by "direct packet access", enabling packet data to be + * manipulated with *skb*\ **->data** and *skb*\ **->data_end** + * pointing respectively to the first byte of packet data and to + * the byte after the last byte of packet data. However, it + * remains useful if one wishes to read large quantities of data + * at once from a packet into the eBPF stack. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_get_stackid(struct pt_reg *ctx, struct bpf_map *map, u64 flags) + * Description + * Walk a user or a kernel stack and return its id. To achieve + * this, the helper needs *ctx*, which is a pointer to the context + * on which the tracing program is executed, and a pointer to a + * *map* of type **BPF_MAP_TYPE_STACK_TRACE**. + * + * The last argument, *flags*, holds the number of stack frames to + * skip (from 0 to 255), masked with + * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set + * a combination of the following flags: + * + * **BPF_F_USER_STACK** + * Collect a user space stack instead of a kernel stack. + * **BPF_F_FAST_STACK_CMP** + * Compare stacks by hash only. + * **BPF_F_REUSE_STACKID** + * If two different stacks hash into the same *stackid*, + * discard the old one. + * + * The stack id retrieved is a 32 bit long integer handle which + * can be further combined with other data (including other stack + * ids) and used as a key into maps. This can be useful for + * generating a variety of graphs (such as flame graphs or off-cpu + * graphs). + * + * For walking a stack, this helper is an improvement over + * **bpf_probe_read**\ (), which can be used with unrolled loops + * but is not efficient and consumes a lot of eBPF instructions. + * Instead, **bpf_get_stackid**\ () can collect up to + * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that + * this limit can be controlled with the **sysctl** program, and + * that it should be manually increased in order to profile long + * user stacks (such as stacks for Java programs). To do so, use: + * + * :: + * + * # sysctl kernel.perf_event_max_stack= + * Return + * The positive or null stack id on success, or a negative error + * in case of failure. + * + * s64 bpf_csum_diff(__be32 *from, u32 from_size, __be32 *to, u32 to_size, __wsum seed) + * Description + * Compute a checksum difference, from the raw buffer pointed by + * *from*, of length *from_size* (that must be a multiple of 4), + * towards the raw buffer pointed by *to*, of size *to_size* + * (same remark). An optional *seed* can be added to the value + * (this can be cascaded, the seed may come from a previous call + * to the helper). + * + * This is flexible enough to be used in several ways: + * + * * With *from_size* == 0, *to_size* > 0 and *seed* set to + * checksum, it can be used when pushing new data. + * * With *from_size* > 0, *to_size* == 0 and *seed* set to + * checksum, it can be used when removing data from a packet. + * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it + * can be used to compute a diff. Note that *from_size* and + * *to_size* do not need to be equal. + * + * This helper can be used in combination with + * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to + * which one can feed in the difference computed with + * **bpf_csum_diff**\ (). + * Return + * The checksum result, or a negative error code in case of + * failure. + * + * int bpf_skb_get_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size) + * Description + * Retrieve tunnel options metadata for the packet associated to + * *skb*, and store the raw tunnel option data to the buffer *opt* + * of *size*. + * + * This helper can be used with encapsulation devices that can + * operate in "collect metadata" mode (please refer to the related + * note in the description of **bpf_skb_get_tunnel_key**\ () for + * more details). A particular example where this can be used is + * in combination with the Geneve encapsulation protocol, where it + * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper) + * and retrieving arbitrary TLVs (Type-Length-Value headers) from + * the eBPF program. This allows for full customization of these + * headers. + * Return + * The size of the option data retrieved. + * + * int bpf_skb_set_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size) + * Description + * Set tunnel options metadata for the packet associated to *skb* + * to the option data contained in the raw buffer *opt* of *size*. + * + * See also the description of the **bpf_skb_get_tunnel_opt**\ () + * helper for additional information. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_change_proto(struct sk_buff *skb, __be16 proto, u64 flags) + * Description + * Change the protocol of the *skb* to *proto*. Currently + * supported are transition from IPv4 to IPv6, and from IPv6 to + * IPv4. The helper takes care of the groundwork for the + * transition, including resizing the socket buffer. The eBPF + * program is expected to fill the new headers, if any, via + * **skb_store_bytes**\ () and to recompute the checksums with + * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ + * (). The main case for this helper is to perform NAT64 + * operations out of an eBPF program. + * + * Internally, the GSO type is marked as dodgy so that headers are + * checked and segments are recalculated by the GSO/GRO engine. + * The size for GSO target is adapted as well. + * + * All values for *flags* are reserved for future usage, and must + * be left at zero. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_change_type(struct sk_buff *skb, u32 type) + * Description + * Change the packet type for the packet associated to *skb*. This + * comes down to setting *skb*\ **->pkt_type** to *type*, except + * the eBPF program does not have a write access to *skb*\ + * **->pkt_type** beside this helper. Using a helper here allows + * for graceful handling of errors. + * + * The major use case is to change incoming *skb*s to + * **PACKET_HOST** in a programmatic way instead of having to + * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for + * example. + * + * Note that *type* only allows certain values. At this time, they + * are: + * + * **PACKET_HOST** + * Packet is for us. + * **PACKET_BROADCAST** + * Send packet to all. + * **PACKET_MULTICAST** + * Send packet to group. + * **PACKET_OTHERHOST** + * Send packet to someone else. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_under_cgroup(struct sk_buff *skb, struct bpf_map *map, u32 index) + * Description + * Check whether *skb* is a descendant of the cgroup2 held by + * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. + * Return + * The return value depends on the result of the test, and can be: + * + * * 0, if the *skb* failed the cgroup2 descendant test. + * * 1, if the *skb* succeeded the cgroup2 descendant test. + * * A negative error code, if an error occurred. + * + * u32 bpf_get_hash_recalc(struct sk_buff *skb) + * Description + * Retrieve the hash of the packet, *skb*\ **->hash**. If it is + * not set, in particular if the hash was cleared due to mangling, + * recompute this hash. Later accesses to the hash can be done + * directly with *skb*\ **->hash**. + * + * Calling **bpf_set_hash_invalid**\ (), changing a packet + * prototype with **bpf_skb_change_proto**\ (), or calling + * **bpf_skb_store_bytes**\ () with the + * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear + * the hash and to trigger a new computation for the next call to + * **bpf_get_hash_recalc**\ (). + * Return + * The 32-bit hash. + * + * u64 bpf_get_current_task(void) + * Return + * A pointer to the current task struct. + * + * int bpf_probe_write_user(void *dst, const void *src, u32 len) + * Description + * Attempt in a safe way to write *len* bytes from the buffer + * *src* to *dst* in memory. It only works for threads that are in + * user context, and *dst* must be a valid user space address. + * + * This helper should not be used to implement any kind of + * security mechanism because of TOC-TOU attacks, but rather to + * debug, divert, and manipulate execution of semi-cooperative + * processes. + * + * Keep in mind that this feature is meant for experiments, and it + * has a risk of crashing the system and running programs. + * Therefore, when an eBPF program using this helper is attached, + * a warning including PID and process name is printed to kernel + * logs. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_current_task_under_cgroup(struct bpf_map *map, u32 index) + * Description + * Check whether the probe is being run is the context of a given + * subset of the cgroup2 hierarchy. The cgroup2 to test is held by + * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. + * Return + * The return value depends on the result of the test, and can be: + * + * * 0, if the *skb* task belongs to the cgroup2. + * * 1, if the *skb* task does not belong to the cgroup2. + * * A negative error code, if an error occurred. + * + * int bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags) + * Description + * Resize (trim or grow) the packet associated to *skb* to the + * new *len*. The *flags* are reserved for future usage, and must + * be left at zero. + * + * The basic idea is that the helper performs the needed work to + * change the size of the packet, then the eBPF program rewrites + * the rest via helpers like **bpf_skb_store_bytes**\ (), + * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ () + * and others. This helper is a slow path utility intended for + * replies with control messages. And because it is targeted for + * slow path, the helper itself can afford to be slow: it + * implicitly linearizes, unclones and drops offloads from the + * *skb*. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_pull_data(struct sk_buff *skb, u32 len) + * Description + * Pull in non-linear data in case the *skb* is non-linear and not + * all of *len* are part of the linear section. Make *len* bytes + * from *skb* readable and writable. If a zero value is passed for + * *len*, then the whole length of the *skb* is pulled. + * + * This helper is only needed for reading and writing with direct + * packet access. + * + * For direct packet access, testing that offsets to access + * are within packet boundaries (test on *skb*\ **->data_end**) is + * susceptible to fail if offsets are invalid, or if the requested + * data is in non-linear parts of the *skb*. On failure the + * program can just bail out, or in the case of a non-linear + * buffer, use a helper to make the data available. The + * **bpf_skb_load_bytes**\ () helper is a first solution to access + * the data. Another one consists in using **bpf_skb_pull_data** + * to pull in once the non-linear parts, then retesting and + * eventually access the data. + * + * At the same time, this also makes sure the *skb* is uncloned, + * which is a necessary condition for direct write. As this needs + * to be an invariant for the write part only, the verifier + * detects writes and adds a prologue that is calling + * **bpf_skb_pull_data()** to effectively unclone the *skb* from + * the very beginning in case it is indeed cloned. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum) + * Description + * Add the checksum *csum* into *skb*\ **->csum** in case the + * driver has supplied a checksum for the entire packet into that + * field. Return an error otherwise. This helper is intended to be + * used in combination with **bpf_csum_diff**\ (), in particular + * when the checksum needs to be updated after data has been + * written into the packet through direct packet access. + * Return + * The checksum on success, or a negative error code in case of + * failure. + * + * void bpf_set_hash_invalid(struct sk_buff *skb) + * Description + * Invalidate the current *skb*\ **->hash**. It can be used after + * mangling on headers through direct packet access, in order to + * indicate that the hash is outdated and to trigger a + * recalculation the next time the kernel tries to access this + * hash or when the **bpf_get_hash_recalc**\ () helper is called. + * + * int bpf_get_numa_node_id(void) + * Description + * Return the id of the current NUMA node. The primary use case + * for this helper is the selection of sockets for the local NUMA + * node, when the program is attached to sockets using the + * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**), + * but the helper is also available to other eBPF program types, + * similarly to **bpf_get_smp_processor_id**\ (). + * Return + * The id of current NUMA node. + * + * int bpf_skb_change_head(struct sk_buff *skb, u32 len, u64 flags) + * Description + * Grows headroom of packet associated to *skb* and adjusts the + * offset of the MAC header accordingly, adding *len* bytes of + * space. It automatically extends and reallocates memory as + * required. + * + * This helper can be used on a layer 3 *skb* to push a MAC header + * for redirection into a layer 2 device. + * + * All values for *flags* are reserved for future usage, and must + * be left at zero. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_xdp_adjust_head(struct xdp_buff *xdp_md, int delta) + * Description + * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that + * it is possible to use a negative value for *delta*. This helper + * can be used to prepare the packet for pushing or popping + * headers. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr) + * Description + * Copy a NUL terminated string from an unsafe address + * *unsafe_ptr* to *dst*. The *size* should include the + * terminating NUL byte. In case the string length is smaller than + * *size*, the target is not padded with further NUL bytes. If the + * string length is larger than *size*, just *size*-1 bytes are + * copied and the last byte is set to NUL. + * + * On success, the length of the copied string is returned. This + * makes this helper useful in tracing programs for reading + * strings, and more importantly to get its length at runtime. See + * the following snippet: + * + * :: + * + * SEC("kprobe/sys_open") + * void bpf_sys_open(struct pt_regs *ctx) + * { + * char buf[PATHLEN]; // PATHLEN is defined to 256 + * int res = bpf_probe_read_str(buf, sizeof(buf), + * ctx->di); + * + * // Consume buf, for example push it to + * // userspace via bpf_perf_event_output(); we + * // can use res (the string length) as event + * // size, after checking its boundaries. + * } + * + * In comparison, using **bpf_probe_read()** helper here instead + * to read the string would require to estimate the length at + * compile time, and would often result in copying more memory + * than necessary. + * + * Another useful use case is when parsing individual process + * arguments or individual environment variables navigating + * *current*\ **->mm->arg_start** and *current*\ + * **->mm->env_start**: using this helper and the return value, + * one can quickly iterate at the right offset of the memory area. + * Return + * On success, the strictly positive length of the string, + * including the trailing NUL character. On error, a negative + * value. + * + * u64 bpf_get_socket_cookie(struct sk_buff *skb) + * Description + * If the **struct sk_buff** pointed by *skb* has a known socket, + * retrieve the cookie (generated by the kernel) of this socket. + * If no cookie has been set yet, generate a new cookie. Once + * generated, the socket cookie remains stable for the life of the + * socket. This helper can be useful for monitoring per socket + * networking traffic statistics as it provides a unique socket + * identifier per namespace. + * Return + * A 8-byte long non-decreasing number on success, or 0 if the + * socket field is missing inside *skb*. + * + * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx) + * Description + * Equivalent to bpf_get_socket_cookie() helper that accepts + * *skb*, but gets socket from **struct bpf_sock_addr** contex. + * Return + * A 8-byte long non-decreasing number. + * + * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx) + * Description + * Equivalent to bpf_get_socket_cookie() helper that accepts + * *skb*, but gets socket from **struct bpf_sock_ops** contex. + * Return + * A 8-byte long non-decreasing number. + * + * u32 bpf_get_socket_uid(struct sk_buff *skb) + * Return + * The owner UID of the socket associated to *skb*. If the socket + * is **NULL**, or if it is not a full socket (i.e. if it is a + * time-wait or a request socket instead), **overflowuid** value + * is returned (note that **overflowuid** might also be the actual + * UID value for the socket). + * + * u32 bpf_set_hash(struct sk_buff *skb, u32 hash) + * Description + * Set the full hash for *skb* (set the field *skb*\ **->hash**) + * to value *hash*. + * Return + * 0 + * + * int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen) + * Description + * Emulate a call to **setsockopt()** on the socket associated to + * *bpf_socket*, which must be a full socket. The *level* at + * which the option resides and the name *optname* of the option + * must be specified, see **setsockopt(2)** for more information. + * The option value of length *optlen* is pointed by *optval*. + * + * This helper actually implements a subset of **setsockopt()**. + * It supports the following *level*\ s: + * + * * **SOL_SOCKET**, which supports the following *optname*\ s: + * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, + * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**. + * * **IPPROTO_TCP**, which supports the following *optname*\ s: + * **TCP_CONGESTION**, **TCP_BPF_IW**, + * **TCP_BPF_SNDCWND_CLAMP**. + * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. + * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_adjust_room(struct sk_buff *skb, u32 len_diff, u32 mode, u64 flags) + * Description + * Grow or shrink the room for data in the packet associated to + * *skb* by *len_diff*, and according to the selected *mode*. + * + * There is a single supported mode at this time: + * + * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer + * (room space is added or removed below the layer 3 header). + * + * All values for *flags* are reserved for future usage, and must + * be left at zero. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags) + * Description + * Redirect the packet to the endpoint referenced by *map* at + * index *key*. Depending on its type, this *map* can contain + * references to net devices (for forwarding packets through other + * ports), or to CPUs (for redirecting XDP frames to another CPU; + * but this is only implemented for native XDP (with driver + * support) as of this writing). + * + * All values for *flags* are reserved for future usage, and must + * be left at zero. + * + * When used to redirect packets to net devices, this helper + * provides a high performance increase over **bpf_redirect**\ (). + * This is due to various implementation details of the underlying + * mechanisms, one of which is the fact that **bpf_redirect_map**\ + * () tries to send packet as a "bulk" to the device. + * Return + * **XDP_REDIRECT** on success, or **XDP_ABORTED** on error. + * + * int bpf_sk_redirect_map(struct bpf_map *map, u32 key, u64 flags) + * Description + * Redirect the packet to the socket referenced by *map* (of type + * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and + * egress interfaces can be used for redirection. The + * **BPF_F_INGRESS** value in *flags* is used to make the + * distinction (ingress path is selected if the flag is present, + * egress path otherwise). This is the only flag supported for now. + * Return + * **SK_PASS** on success, or **SK_DROP** on error. + * + * int bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags) + * Description + * Add an entry to, or update a *map* referencing sockets. The + * *skops* is used as a new value for the entry associated to + * *key*. *flags* is one of: + * + * **BPF_NOEXIST** + * The entry for *key* must not exist in the map. + * **BPF_EXIST** + * The entry for *key* must already exist in the map. + * **BPF_ANY** + * No condition on the existence of the entry for *key*. + * + * If the *map* has eBPF programs (parser and verdict), those will + * be inherited by the socket being added. If the socket is + * already attached to eBPF programs, this results in an error. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta) + * Description + * Adjust the address pointed by *xdp_md*\ **->data_meta** by + * *delta* (which can be positive or negative). Note that this + * operation modifies the address stored in *xdp_md*\ **->data**, + * so the latter must be loaded only after the helper has been + * called. + * + * The use of *xdp_md*\ **->data_meta** is optional and programs + * are not required to use it. The rationale is that when the + * packet is processed with XDP (e.g. as DoS filter), it is + * possible to push further meta data along with it before passing + * to the stack, and to give the guarantee that an ingress eBPF + * program attached as a TC classifier on the same device can pick + * this up for further post-processing. Since TC works with socket + * buffers, it remains possible to set from XDP the **mark** or + * **priority** pointers, or other pointers for the socket buffer. + * Having this scratch space generic and programmable allows for + * more flexibility as the user is free to store whatever meta + * data they need. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size) + * Description + * Read the value of a perf event counter, and store it into *buf* + * of size *buf_size*. This helper relies on a *map* of type + * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event + * counter is selected when *map* is updated with perf event file + * descriptors. The *map* is an array whose size is the number of + * available CPUs, and each cell contains a value relative to one + * CPU. The value to retrieve is indicated by *flags*, that + * contains the index of the CPU to look up, masked with + * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to + * **BPF_F_CURRENT_CPU** to indicate that the value for the + * current CPU should be retrieved. + * + * This helper behaves in a way close to + * **bpf_perf_event_read**\ () helper, save that instead of + * just returning the value observed, it fills the *buf* + * structure. This allows for additional data to be retrieved: in + * particular, the enabled and running times (in *buf*\ + * **->enabled** and *buf*\ **->running**, respectively) are + * copied. In general, **bpf_perf_event_read_value**\ () is + * recommended over **bpf_perf_event_read**\ (), which has some + * ABI issues and provides fewer functionalities. + * + * These values are interesting, because hardware PMU (Performance + * Monitoring Unit) counters are limited resources. When there are + * more PMU based perf events opened than available counters, + * kernel will multiplex these events so each event gets certain + * percentage (but not all) of the PMU time. In case that + * multiplexing happens, the number of samples or counter value + * will not reflect the case compared to when no multiplexing + * occurs. This makes comparison between different runs difficult. + * Typically, the counter value should be normalized before + * comparing to other experiments. The usual normalization is done + * as follows. + * + * :: + * + * normalized_counter = counter * t_enabled / t_running + * + * Where t_enabled is the time enabled for event and t_running is + * the time running for event since last normalization. The + * enabled and running times are accumulated since the perf event + * open. To achieve scaling factor between two invocations of an + * eBPF program, users can can use CPU id as the key (which is + * typical for perf array usage model) to remember the previous + * value and do the calculation inside the eBPF program. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size) + * Description + * For en eBPF program attached to a perf event, retrieve the + * value of the event counter associated to *ctx* and store it in + * the structure pointed by *buf* and of size *buf_size*. Enabled + * and running times are also stored in the structure (see + * description of helper **bpf_perf_event_read_value**\ () for + * more details). + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen) + * Description + * Emulate a call to **getsockopt()** on the socket associated to + * *bpf_socket*, which must be a full socket. The *level* at + * which the option resides and the name *optname* of the option + * must be specified, see **getsockopt(2)** for more information. + * The retrieved value is stored in the structure pointed by + * *opval* and of length *optlen*. + * + * This helper actually implements a subset of **getsockopt()**. + * It supports the following *level*\ s: + * + * * **IPPROTO_TCP**, which supports *optname* + * **TCP_CONGESTION**. + * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. + * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_override_return(struct pt_reg *regs, u64 rc) + * Description + * Used for error injection, this helper uses kprobes to override + * the return value of the probed function, and to set it to *rc*. + * The first argument is the context *regs* on which the kprobe + * works. + * + * This helper works by setting setting the PC (program counter) + * to an override function which is run in place of the original + * probed function. This means the probed function is not run at + * all. The replacement function just returns with the required + * value. + * + * This helper has security implications, and thus is subject to + * restrictions. It is only available if the kernel was compiled + * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration + * option, and in this case it only works on functions tagged with + * **ALLOW_ERROR_INJECTION** in the kernel code. + * + * Also, the helper is only available for the architectures having + * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing, + * x86 architecture is the only one to support this feature. + * Return + * 0 + * + * int bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval) + * Description + * Attempt to set the value of the **bpf_sock_ops_cb_flags** field + * for the full TCP socket associated to *bpf_sock_ops* to + * *argval*. + * + * The primary use of this field is to determine if there should + * be calls to eBPF programs of type + * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP + * code. A program of the same type can change its value, per + * connection and as necessary, when the connection is + * established. This field is directly accessible for reading, but + * this helper must be used for updates in order to return an + * error if an eBPF program tries to set a callback that is not + * supported in the current kernel. + * + * The supported callback values that *argval* can combine are: + * + * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out) + * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission) + * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change) + * + * Here are some examples of where one could call such eBPF + * program: + * + * * When RTO fires. + * * When a packet is retransmitted. + * * When the connection terminates. + * * When a packet is sent. + * * When a packet is received. + * Return + * Code **-EINVAL** if the socket is not a full TCP socket; + * otherwise, a positive number containing the bits that could not + * be set is returned (which comes down to 0 if all bits were set + * as required). + * + * int bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags) + * Description + * This helper is used in programs implementing policies at the + * socket level. If the message *msg* is allowed to pass (i.e. if + * the verdict eBPF program returns **SK_PASS**), redirect it to + * the socket referenced by *map* (of type + * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and + * egress interfaces can be used for redirection. The + * **BPF_F_INGRESS** value in *flags* is used to make the + * distinction (ingress path is selected if the flag is present, + * egress path otherwise). This is the only flag supported for now. + * Return + * **SK_PASS** on success, or **SK_DROP** on error. + * + * int bpf_msg_apply_bytes(struct sk_msg_buff *msg, u32 bytes) + * Description + * For socket policies, apply the verdict of the eBPF program to + * the next *bytes* (number of bytes) of message *msg*. + * + * For example, this helper can be used in the following cases: + * + * * A single **sendmsg**\ () or **sendfile**\ () system call + * contains multiple logical messages that the eBPF program is + * supposed to read and for which it should apply a verdict. + * * An eBPF program only cares to read the first *bytes* of a + * *msg*. If the message has a large payload, then setting up + * and calling the eBPF program repeatedly for all bytes, even + * though the verdict is already known, would create unnecessary + * overhead. + * + * When called from within an eBPF program, the helper sets a + * counter internal to the BPF infrastructure, that is used to + * apply the last verdict to the next *bytes*. If *bytes* is + * smaller than the current data being processed from a + * **sendmsg**\ () or **sendfile**\ () system call, the first + * *bytes* will be sent and the eBPF program will be re-run with + * the pointer for start of data pointing to byte number *bytes* + * **+ 1**. If *bytes* is larger than the current data being + * processed, then the eBPF verdict will be applied to multiple + * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are + * consumed. + * + * Note that if a socket closes with the internal counter holding + * a non-zero value, this is not a problem because data is not + * being buffered for *bytes* and is sent as it is received. + * Return + * 0 + * + * int bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes) + * Description + * For socket policies, prevent the execution of the verdict eBPF + * program for message *msg* until *bytes* (byte number) have been + * accumulated. + * + * This can be used when one needs a specific number of bytes + * before a verdict can be assigned, even if the data spans + * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme + * case would be a user calling **sendmsg**\ () repeatedly with + * 1-byte long message segments. Obviously, this is bad for + * performance, but it is still valid. If the eBPF program needs + * *bytes* bytes to validate a header, this helper can be used to + * prevent the eBPF program to be called again until *bytes* have + * been accumulated. + * Return + * 0 + * + * int bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags) + * Description + * For socket policies, pull in non-linear data from user space + * for *msg* and set pointers *msg*\ **->data** and *msg*\ + * **->data_end** to *start* and *end* bytes offsets into *msg*, + * respectively. + * + * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a + * *msg* it can only parse data that the (**data**, **data_end**) + * pointers have already consumed. For **sendmsg**\ () hooks this + * is likely the first scatterlist element. But for calls relying + * on the **sendpage** handler (e.g. **sendfile**\ ()) this will + * be the range (**0**, **0**) because the data is shared with + * user space and by default the objective is to avoid allowing + * user space to modify data while (or after) eBPF verdict is + * being decided. This helper can be used to pull in data and to + * set the start and end pointer to given values. Data will be + * copied if necessary (i.e. if data was not linear and if start + * and end pointers do not point to the same chunk). + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * + * All values for *flags* are reserved for future usage, and must + * be left at zero. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_bind(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len) + * Description + * Bind the socket associated to *ctx* to the address pointed by + * *addr*, of length *addr_len*. This allows for making outgoing + * connection from the desired IP address, which can be useful for + * example when all processes inside a cgroup should use one + * single IP address on a host that has multiple IP configured. + * + * This helper works for IPv4 and IPv6, TCP and UDP sockets. The + * domain (*addr*\ **->sa_family**) must be **AF_INET** (or + * **AF_INET6**). Looking for a free port to bind to can be + * expensive, therefore binding to port is not permitted by the + * helper: *addr*\ **->sin_port** (or **sin6_port**, respectively) + * must be set to zero. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_xdp_adjust_tail(struct xdp_buff *xdp_md, int delta) + * Description + * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is + * only possible to shrink the packet as of this writing, + * therefore *delta* must be a negative integer. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_skb_get_xfrm_state(struct sk_buff *skb, u32 index, struct bpf_xfrm_state *xfrm_state, u32 size, u64 flags) + * Description + * Retrieve the XFRM state (IP transform framework, see also + * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*. + * + * The retrieved value is stored in the **struct bpf_xfrm_state** + * pointed by *xfrm_state* and of length *size*. + * + * All values for *flags* are reserved for future usage, and must + * be left at zero. + * + * This helper is available only if the kernel was compiled with + * **CONFIG_XFRM** configuration option. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_get_stack(struct pt_regs *regs, void *buf, u32 size, u64 flags) + * Description + * Return a user or a kernel stack in bpf program provided buffer. + * To achieve this, the helper needs *ctx*, which is a pointer + * to the context on which the tracing program is executed. + * To store the stacktrace, the bpf program provides *buf* with + * a nonnegative *size*. + * + * The last argument, *flags*, holds the number of stack frames to + * skip (from 0 to 255), masked with + * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set + * the following flags: + * + * **BPF_F_USER_STACK** + * Collect a user space stack instead of a kernel stack. + * **BPF_F_USER_BUILD_ID** + * Collect buildid+offset instead of ips for user stack, + * only valid if **BPF_F_USER_STACK** is also specified. + * + * **bpf_get_stack**\ () can collect up to + * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject + * to sufficient large buffer size. Note that + * this limit can be controlled with the **sysctl** program, and + * that it should be manually increased in order to profile long + * user stacks (such as stacks for Java programs). To do so, use: + * + * :: + * + * # sysctl kernel.perf_event_max_stack= + * Return + * A non-negative value equal to or less than *size* on success, + * or a negative error in case of failure. + * + * int bpf_skb_load_bytes_relative(const struct sk_buff *skb, u32 offset, void *to, u32 len, u32 start_header) + * Description + * This helper is similar to **bpf_skb_load_bytes**\ () in that + * it provides an easy way to load *len* bytes from *offset* + * from the packet associated to *skb*, into the buffer pointed + * by *to*. The difference to **bpf_skb_load_bytes**\ () is that + * a fifth argument *start_header* exists in order to select a + * base offset to start from. *start_header* can be one of: + * + * **BPF_HDR_START_MAC** + * Base offset to load data from is *skb*'s mac header. + * **BPF_HDR_START_NET** + * Base offset to load data from is *skb*'s network header. + * + * In general, "direct packet access" is the preferred method to + * access packet data, however, this helper is in particular useful + * in socket filters where *skb*\ **->data** does not always point + * to the start of the mac header and where "direct packet access" + * is not available. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags) + * Description + * Do FIB lookup in kernel tables using parameters in *params*. + * If lookup is successful and result shows packet is to be + * forwarded, the neighbor tables are searched for the nexthop. + * If successful (ie., FIB lookup shows forwarding and nexthop + * is resolved), the nexthop address is returned in ipv4_dst + * or ipv6_dst based on family, smac is set to mac address of + * egress device, dmac is set to nexthop mac address, rt_metric + * is set to metric from route (IPv4/IPv6 only), and ifindex + * is set to the device index of the nexthop from the FIB lookup. + * + * *plen* argument is the size of the passed in struct. + * *flags* argument can be a combination of one or more of the + * following values: + * + * **BPF_FIB_LOOKUP_DIRECT** + * Do a direct table lookup vs full lookup using FIB + * rules. + * **BPF_FIB_LOOKUP_OUTPUT** + * Perform lookup from an egress perspective (default is + * ingress). + * + * *ctx* is either **struct xdp_md** for XDP programs or + * **struct sk_buff** tc cls_act programs. + * Return + * * < 0 if any input argument is invalid + * * 0 on success (packet is forwarded, nexthop neighbor exists) + * * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the + * packet is not forwarded or needs assist from full stack + * + * int bpf_sock_hash_update(struct bpf_sock_ops_kern *skops, struct bpf_map *map, void *key, u64 flags) + * Description + * Add an entry to, or update a sockhash *map* referencing sockets. + * The *skops* is used as a new value for the entry associated to + * *key*. *flags* is one of: + * + * **BPF_NOEXIST** + * The entry for *key* must not exist in the map. + * **BPF_EXIST** + * The entry for *key* must already exist in the map. + * **BPF_ANY** + * No condition on the existence of the entry for *key*. + * + * If the *map* has eBPF programs (parser and verdict), those will + * be inherited by the socket being added. If the socket is + * already attached to eBPF programs, this results in an error. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags) + * Description + * This helper is used in programs implementing policies at the + * socket level. If the message *msg* is allowed to pass (i.e. if + * the verdict eBPF program returns **SK_PASS**), redirect it to + * the socket referenced by *map* (of type + * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and + * egress interfaces can be used for redirection. The + * **BPF_F_INGRESS** value in *flags* is used to make the + * distinction (ingress path is selected if the flag is present, + * egress path otherwise). This is the only flag supported for now. + * Return + * **SK_PASS** on success, or **SK_DROP** on error. + * + * int bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags) + * Description + * This helper is used in programs implementing policies at the + * skb socket level. If the sk_buff *skb* is allowed to pass (i.e. + * if the verdeict eBPF program returns **SK_PASS**), redirect it + * to the socket referenced by *map* (of type + * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and + * egress interfaces can be used for redirection. The + * **BPF_F_INGRESS** value in *flags* is used to make the + * distinction (ingress path is selected if the flag is present, + * egress otherwise). This is the only flag supported for now. + * Return + * **SK_PASS** on success, or **SK_DROP** on error. + * + * int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) + * Description + * Encapsulate the packet associated to *skb* within a Layer 3 + * protocol header. This header is provided in the buffer at + * address *hdr*, with *len* its size in bytes. *type* indicates + * the protocol of the header and can be one of: + * + * **BPF_LWT_ENCAP_SEG6** + * IPv6 encapsulation with Segment Routing Header + * (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, + * the IPv6 header is computed by the kernel. + * **BPF_LWT_ENCAP_SEG6_INLINE** + * Only works if *skb* contains an IPv6 packet. Insert a + * Segment Routing Header (**struct ipv6_sr_hdr**) inside + * the IPv6 header. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len) + * Description + * Store *len* bytes from address *from* into the packet + * associated to *skb*, at *offset*. Only the flags, tag and TLVs + * inside the outermost IPv6 Segment Routing Header can be + * modified through this helper. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta) + * Description + * Adjust the size allocated to TLVs in the outermost IPv6 + * Segment Routing Header contained in the packet associated to + * *skb*, at position *offset* by *delta* bytes. Only offsets + * after the segments are accepted. *delta* can be as well + * positive (growing) as negative (shrinking). + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len) + * Description + * Apply an IPv6 Segment Routing action of type *action* to the + * packet associated to *skb*. Each action takes a parameter + * contained at address *param*, and of length *param_len* bytes. + * *action* can be one of: + * + * **SEG6_LOCAL_ACTION_END_X** + * End.X action: Endpoint with Layer-3 cross-connect. + * Type of *param*: **struct in6_addr**. + * **SEG6_LOCAL_ACTION_END_T** + * End.T action: Endpoint with specific IPv6 table lookup. + * Type of *param*: **int**. + * **SEG6_LOCAL_ACTION_END_B6** + * End.B6 action: Endpoint bound to an SRv6 policy. + * Type of param: **struct ipv6_sr_hdr**. + * **SEG6_LOCAL_ACTION_END_B6_ENCAP** + * End.B6.Encap action: Endpoint bound to an SRv6 + * encapsulation policy. + * Type of param: **struct ipv6_sr_hdr**. + * + * A call to this helper is susceptible to change the underlaying + * packet buffer. Therefore, at load time, all checks on pointers + * previously done by the verifier are invalidated and must be + * performed again, if the helper is used in combination with + * direct packet access. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_rc_keydown(void *ctx, u32 protocol, u64 scancode, u32 toggle) + * Description + * This helper is used in programs implementing IR decoding, to + * report a successfully decoded key press with *scancode*, + * *toggle* value in the given *protocol*. The scancode will be + * translated to a keycode using the rc keymap, and reported as + * an input key down event. After a period a key up event is + * generated. This period can be extended by calling either + * **bpf_rc_keydown** () again with the same values, or calling + * **bpf_rc_repeat** (). + * + * Some protocols include a toggle bit, in case the button was + * released and pressed again between consecutive scancodes. + * + * The *ctx* should point to the lirc sample as passed into + * the program. + * + * The *protocol* is the decoded protocol number (see + * **enum rc_proto** for some predefined values). + * + * This helper is only available is the kernel was compiled with + * the **CONFIG_BPF_LIRC_MODE2** configuration option set to + * "**y**". + * Return + * 0 + * + * int bpf_rc_repeat(void *ctx) + * Description + * This helper is used in programs implementing IR decoding, to + * report a successfully decoded repeat key message. This delays + * the generation of a key up event for previously generated + * key down event. + * + * Some IR protocols like NEC have a special IR message for + * repeating last button, for when a button is held down. + * + * The *ctx* should point to the lirc sample as passed into + * the program. + * + * This helper is only available is the kernel was compiled with + * the **CONFIG_BPF_LIRC_MODE2** configuration option set to + * "**y**". + * Return + * 0 + * + * uint64_t bpf_skb_cgroup_id(struct sk_buff *skb) + * Description + * Return the cgroup v2 id of the socket associated with the *skb*. + * This is roughly similar to the **bpf_get_cgroup_classid**\ () + * helper for cgroup v1 by providing a tag resp. identifier that + * can be matched on or used for map lookups e.g. to implement + * policy. The cgroup v2 id of a given path in the hierarchy is + * exposed in user space through the f_handle API in order to get + * to the same 64-bit id. + * + * This helper can be used on TC egress path, but not on ingress, + * and is available only if the kernel was compiled with the + * **CONFIG_SOCK_CGROUP_DATA** configuration option. + * Return + * The id is returned or 0 in case the id could not be retrieved. + * + * u64 bpf_skb_ancestor_cgroup_id(struct sk_buff *skb, int ancestor_level) + * Description + * Return id of cgroup v2 that is ancestor of cgroup associated + * with the *skb* at the *ancestor_level*. The root cgroup is at + * *ancestor_level* zero and each step down the hierarchy + * increments the level. If *ancestor_level* == level of cgroup + * associated with *skb*, then return value will be same as that + * of **bpf_skb_cgroup_id**\ (). + * + * The helper is useful to implement policies based on cgroups + * that are upper in hierarchy than immediate cgroup associated + * with *skb*. + * + * The format of returned id and helper limitations are same as in + * **bpf_skb_cgroup_id**\ (). + * Return + * The id is returned or 0 in case the id could not be retrieved. + * + * u64 bpf_get_current_cgroup_id(void) + * Return + * A 64-bit integer containing the current cgroup id based + * on the cgroup within which the current task is running. + * + * void* get_local_storage(void *map, u64 flags) + * Description + * Get the pointer to the local storage area. + * The type and the size of the local storage is defined + * by the *map* argument. + * The *flags* meaning is specific for each map type, + * and has to be 0 for cgroup local storage. + * + * Depending on the bpf program type, a local storage area + * can be shared between multiple instances of the bpf program, + * running simultaneously. + * + * A user should care about the synchronization by himself. + * For example, by using the BPF_STX_XADD instruction to alter + * the shared data. + * Return + * Pointer to the local storage area. + * + * int bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags) + * Description + * Select a SO_REUSEPORT sk from a BPF_MAP_TYPE_REUSEPORT_ARRAY map + * It checks the selected sk is matching the incoming + * request in the skb. + * Return + * 0 on success, or a negative error in case of failure. + */ +#define __BPF_FUNC_MAPPER(FN) \ + FN(unspec), \ + FN(map_lookup_elem), \ + FN(map_update_elem), \ + FN(map_delete_elem), \ + FN(probe_read), \ + FN(ktime_get_ns), \ + FN(trace_printk), \ + FN(get_prandom_u32), \ + FN(get_smp_processor_id), \ + FN(skb_store_bytes), \ + FN(l3_csum_replace), \ + FN(l4_csum_replace), \ + FN(tail_call), \ + FN(clone_redirect), \ + FN(get_current_pid_tgid), \ + FN(get_current_uid_gid), \ + FN(get_current_comm), \ + FN(get_cgroup_classid), \ + FN(skb_vlan_push), \ + FN(skb_vlan_pop), \ + FN(skb_get_tunnel_key), \ + FN(skb_set_tunnel_key), \ + FN(perf_event_read), \ + FN(redirect), \ + FN(get_route_realm), \ + FN(perf_event_output), \ + FN(skb_load_bytes), \ + FN(get_stackid), \ + FN(csum_diff), \ + FN(skb_get_tunnel_opt), \ + FN(skb_set_tunnel_opt), \ + FN(skb_change_proto), \ + FN(skb_change_type), \ + FN(skb_under_cgroup), \ + FN(get_hash_recalc), \ + FN(get_current_task), \ + FN(probe_write_user), \ + FN(current_task_under_cgroup), \ + FN(skb_change_tail), \ + FN(skb_pull_data), \ + FN(csum_update), \ + FN(set_hash_invalid), \ + FN(get_numa_node_id), \ + FN(skb_change_head), \ + FN(xdp_adjust_head), \ + FN(probe_read_str), \ + FN(get_socket_cookie), \ + FN(get_socket_uid), \ + FN(set_hash), \ + FN(setsockopt), \ + FN(skb_adjust_room), \ + FN(redirect_map), \ + FN(sk_redirect_map), \ + FN(sock_map_update), \ + FN(xdp_adjust_meta), \ + FN(perf_event_read_value), \ + FN(perf_prog_read_value), \ + FN(getsockopt), \ + FN(override_return), \ + FN(sock_ops_cb_flags_set), \ + FN(msg_redirect_map), \ + FN(msg_apply_bytes), \ + FN(msg_cork_bytes), \ + FN(msg_pull_data), \ + FN(bind), \ + FN(xdp_adjust_tail), \ + FN(skb_get_xfrm_state), \ + FN(get_stack), \ + FN(skb_load_bytes_relative), \ + FN(fib_lookup), \ + FN(sock_hash_update), \ + FN(msg_redirect_hash), \ + FN(sk_redirect_hash), \ + FN(lwt_push_encap), \ + FN(lwt_seg6_store_bytes), \ + FN(lwt_seg6_adjust_srh), \ + FN(lwt_seg6_action), \ + FN(rc_repeat), \ + FN(rc_keydown), \ + FN(skb_cgroup_id), \ + FN(get_current_cgroup_id), \ + FN(get_local_storage), \ + FN(sk_select_reuseport), \ + FN(skb_ancestor_cgroup_id), + +/* integer value in 'imm' field of BPF_CALL instruction selects which helper + * function eBPF program intends to call + */ +#define __BPF_ENUM_FN(x) BPF_FUNC_ ## x +enum bpf_func_id { + __BPF_FUNC_MAPPER(__BPF_ENUM_FN) + __BPF_FUNC_MAX_ID, +}; +#undef __BPF_ENUM_FN + +/* All flags used by eBPF helper functions, placed here. */ + +/* BPF_FUNC_skb_store_bytes flags. */ +#define BPF_F_RECOMPUTE_CSUM (1ULL << 0) +#define BPF_F_INVALIDATE_HASH (1ULL << 1) + +/* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags. + * First 4 bits are for passing the header field size. + */ +#define BPF_F_HDR_FIELD_MASK 0xfULL + +/* BPF_FUNC_l4_csum_replace flags. */ +#define BPF_F_PSEUDO_HDR (1ULL << 4) +#define BPF_F_MARK_MANGLED_0 (1ULL << 5) +#define BPF_F_MARK_ENFORCE (1ULL << 6) + +/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */ +#define BPF_F_INGRESS (1ULL << 0) + +/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */ +#define BPF_F_TUNINFO_IPV6 (1ULL << 0) + +/* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */ +#define BPF_F_SKIP_FIELD_MASK 0xffULL +#define BPF_F_USER_STACK (1ULL << 8) +/* flags used by BPF_FUNC_get_stackid only. */ +#define BPF_F_FAST_STACK_CMP (1ULL << 9) +#define BPF_F_REUSE_STACKID (1ULL << 10) +/* flags used by BPF_FUNC_get_stack only. */ +#define BPF_F_USER_BUILD_ID (1ULL << 11) + +/* BPF_FUNC_skb_set_tunnel_key flags. */ +#define BPF_F_ZERO_CSUM_TX (1ULL << 1) +#define BPF_F_DONT_FRAGMENT (1ULL << 2) +#define BPF_F_SEQ_NUMBER (1ULL << 3) + +/* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and + * BPF_FUNC_perf_event_read_value flags. + */ +#define BPF_F_INDEX_MASK 0xffffffffULL +#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK +/* BPF_FUNC_perf_event_output for sk_buff input context. */ +#define BPF_F_CTXLEN_MASK (0xfffffULL << 32) + +/* Mode for BPF_FUNC_skb_adjust_room helper. */ +enum bpf_adj_room_mode { + BPF_ADJ_ROOM_NET, +}; + +/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ +enum bpf_hdr_start_off { + BPF_HDR_START_MAC, + BPF_HDR_START_NET, +}; + +/* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ +enum bpf_lwt_encap_mode { + BPF_LWT_ENCAP_SEG6, + BPF_LWT_ENCAP_SEG6_INLINE +}; + +/* user accessible mirror of in-kernel sk_buff. + * new fields can only be added to the end of this structure + */ +struct __sk_buff { + __u32 len; + __u32 pkt_type; + __u32 mark; + __u32 queue_mapping; + __u32 protocol; + __u32 vlan_present; + __u32 vlan_tci; + __u32 vlan_proto; + __u32 priority; + __u32 ingress_ifindex; + __u32 ifindex; + __u32 tc_index; + __u32 cb[5]; + __u32 hash; + __u32 tc_classid; + __u32 data; + __u32 data_end; + __u32 napi_id; + + /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */ + __u32 family; + __u32 remote_ip4; /* Stored in network byte order */ + __u32 local_ip4; /* Stored in network byte order */ + __u32 remote_ip6[4]; /* Stored in network byte order */ + __u32 local_ip6[4]; /* Stored in network byte order */ + __u32 remote_port; /* Stored in network byte order */ + __u32 local_port; /* stored in host byte order */ + /* ... here. */ + + __u32 data_meta; + struct bpf_flow_keys *flow_keys; +}; + +struct bpf_tunnel_key { + __u32 tunnel_id; + union { + __u32 remote_ipv4; + __u32 remote_ipv6[4]; + }; + __u8 tunnel_tos; + __u8 tunnel_ttl; + __u16 tunnel_ext; /* Padding, future use. */ + __u32 tunnel_label; +}; + +/* user accessible mirror of in-kernel xfrm_state. + * new fields can only be added to the end of this structure + */ +struct bpf_xfrm_state { + __u32 reqid; + __u32 spi; /* Stored in network byte order */ + __u16 family; + __u16 ext; /* Padding, future use. */ + union { + __u32 remote_ipv4; /* Stored in network byte order */ + __u32 remote_ipv6[4]; /* Stored in network byte order */ + }; +}; + +/* Generic BPF return codes which all BPF program types may support. + * The values are binary compatible with their TC_ACT_* counter-part to + * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT + * programs. + * + * XDP is handled separately, see XDP_*. + */ +enum bpf_ret_code { + BPF_OK = 0, + /* 1 reserved */ + BPF_DROP = 2, + /* 3-6 reserved */ + BPF_REDIRECT = 7, + /* >127 are reserved for prog type specific return codes */ +}; + +struct bpf_sock { + __u32 bound_dev_if; + __u32 family; + __u32 type; + __u32 protocol; + __u32 mark; + __u32 priority; + __u32 src_ip4; /* Allows 1,2,4-byte read. + * Stored in network byte order. + */ + __u32 src_ip6[4]; /* Allows 1,2,4-byte read. + * Stored in network byte order. + */ + __u32 src_port; /* Allows 4-byte read. + * Stored in host byte order + */ +}; + +#define XDP_PACKET_HEADROOM 256 + +/* User return codes for XDP prog type. + * A valid XDP program must return one of these defined values. All other + * return codes are reserved for future use. Unknown return codes will + * result in packet drops and a warning via bpf_warn_invalid_xdp_action(). + */ +enum xdp_action { + XDP_ABORTED = 0, + XDP_DROP, + XDP_PASS, + XDP_TX, + XDP_REDIRECT, +}; + +/* user accessible metadata for XDP packet hook + * new fields must be added to the end of this structure + */ +struct xdp_md { + __u32 data; + __u32 data_end; + __u32 data_meta; + /* Below access go through struct xdp_rxq_info */ + __u32 ingress_ifindex; /* rxq->dev->ifindex */ + __u32 rx_queue_index; /* rxq->queue_index */ +}; + +enum sk_action { + SK_DROP = 0, + SK_PASS, +}; + +/* user accessible metadata for SK_MSG packet hook, new fields must + * be added to the end of this structure + */ +struct sk_msg_md { + void *data; + void *data_end; + + __u32 family; + __u32 remote_ip4; /* Stored in network byte order */ + __u32 local_ip4; /* Stored in network byte order */ + __u32 remote_ip6[4]; /* Stored in network byte order */ + __u32 local_ip6[4]; /* Stored in network byte order */ + __u32 remote_port; /* Stored in network byte order */ + __u32 local_port; /* stored in host byte order */ +}; + +struct sk_reuseport_md { + /* + * Start of directly accessible data. It begins from + * the tcp/udp header. + */ + void *data; + void *data_end; /* End of directly accessible data */ + /* + * Total length of packet (starting from the tcp/udp header). + * Note that the directly accessible bytes (data_end - data) + * could be less than this "len". Those bytes could be + * indirectly read by a helper "bpf_skb_load_bytes()". + */ + __u32 len; + /* + * Eth protocol in the mac header (network byte order). e.g. + * ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD) + */ + __u32 eth_protocol; + __u32 ip_protocol; /* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */ + __u32 bind_inany; /* Is sock bound to an INANY address? */ + __u32 hash; /* A hash of the packet 4 tuples */ +}; + +#define BPF_TAG_SIZE 8 + +struct bpf_prog_info { + __u32 type; + __u32 id; + __u8 tag[BPF_TAG_SIZE]; + __u32 jited_prog_len; + __u32 xlated_prog_len; + __aligned_u64 jited_prog_insns; + __aligned_u64 xlated_prog_insns; + __u64 load_time; /* ns since boottime */ + __u32 created_by_uid; + __u32 nr_map_ids; + __aligned_u64 map_ids; + char name[BPF_OBJ_NAME_LEN]; + __u32 ifindex; + __u32 gpl_compatible:1; + __u64 netns_dev; + __u64 netns_ino; + __u32 nr_jited_ksyms; + __u32 nr_jited_func_lens; + __aligned_u64 jited_ksyms; + __aligned_u64 jited_func_lens; +} __attribute__((aligned(8))); + +struct bpf_map_info { + __u32 type; + __u32 id; + __u32 key_size; + __u32 value_size; + __u32 max_entries; + __u32 map_flags; + char name[BPF_OBJ_NAME_LEN]; + __u32 ifindex; + __u32 :32; + __u64 netns_dev; + __u64 netns_ino; + __u32 btf_id; + __u32 btf_key_type_id; + __u32 btf_value_type_id; +} __attribute__((aligned(8))); + +struct bpf_btf_info { + __aligned_u64 btf; + __u32 btf_size; + __u32 id; +} __attribute__((aligned(8))); + +/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed + * by user and intended to be used by socket (e.g. to bind to, depends on + * attach attach type). + */ +struct bpf_sock_addr { + __u32 user_family; /* Allows 4-byte read, but no write. */ + __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. + * Stored in network byte order. + */ + __u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. + * Stored in network byte order. + */ + __u32 user_port; /* Allows 4-byte read and write. + * Stored in network byte order + */ + __u32 family; /* Allows 4-byte read, but no write */ + __u32 type; /* Allows 4-byte read, but no write */ + __u32 protocol; /* Allows 4-byte read, but no write */ + __u32 msg_src_ip4; /* Allows 1,2,4-byte read an 4-byte write. + * Stored in network byte order. + */ + __u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. + * Stored in network byte order. + */ +}; + +/* User bpf_sock_ops struct to access socket values and specify request ops + * and their replies. + * Some of this fields are in network (bigendian) byte order and may need + * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h). + * New fields can only be added at the end of this structure + */ +struct bpf_sock_ops { + __u32 op; + union { + __u32 args[4]; /* Optionally passed to bpf program */ + __u32 reply; /* Returned by bpf program */ + __u32 replylong[4]; /* Optionally returned by bpf prog */ + }; + __u32 family; + __u32 remote_ip4; /* Stored in network byte order */ + __u32 local_ip4; /* Stored in network byte order */ + __u32 remote_ip6[4]; /* Stored in network byte order */ + __u32 local_ip6[4]; /* Stored in network byte order */ + __u32 remote_port; /* Stored in network byte order */ + __u32 local_port; /* stored in host byte order */ + __u32 is_fullsock; /* Some TCP fields are only valid if + * there is a full socket. If not, the + * fields read as zero. + */ + __u32 snd_cwnd; + __u32 srtt_us; /* Averaged RTT << 3 in usecs */ + __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */ + __u32 state; + __u32 rtt_min; + __u32 snd_ssthresh; + __u32 rcv_nxt; + __u32 snd_nxt; + __u32 snd_una; + __u32 mss_cache; + __u32 ecn_flags; + __u32 rate_delivered; + __u32 rate_interval_us; + __u32 packets_out; + __u32 retrans_out; + __u32 total_retrans; + __u32 segs_in; + __u32 data_segs_in; + __u32 segs_out; + __u32 data_segs_out; + __u32 lost_out; + __u32 sacked_out; + __u32 sk_txhash; + __u64 bytes_received; + __u64 bytes_acked; +}; + +/* Definitions for bpf_sock_ops_cb_flags */ +#define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0) +#define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1) +#define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2) +#define BPF_SOCK_OPS_ALL_CB_FLAGS 0x7 /* Mask of all currently + * supported cb flags + */ + +/* List of known BPF sock_ops operators. + * New entries can only be added at the end + */ +enum { + BPF_SOCK_OPS_VOID, + BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or + * -1 if default value should be used + */ + BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized + * window (in packets) or -1 if default + * value should be used + */ + BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an + * active connection is initialized + */ + BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an + * active connection is + * established + */ + BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a + * passive connection is + * established + */ + BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control + * needs ECN + */ + BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is + * based on the path and may be + * dependent on the congestion control + * algorithm. In general it indicates + * a congestion threshold. RTTs above + * this indicate congestion + */ + BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered. + * Arg1: value of icsk_retransmits + * Arg2: value of icsk_rto + * Arg3: whether RTO has expired + */ + BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted. + * Arg1: sequence number of 1st byte + * Arg2: # segments + * Arg3: return value of + * tcp_transmit_skb (0 => success) + */ + BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state. + * Arg1: old_state + * Arg2: new_state + */ + BPF_SOCK_OPS_TCP_LISTEN_CB, /* Called on listen(2), right after + * socket transition to LISTEN state. + */ +}; + +/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect + * changes between the TCP and BPF versions. Ideally this should never happen. + * If it does, we need to add code to convert them before calling + * the BPF sock_ops function. + */ +enum { + BPF_TCP_ESTABLISHED = 1, + BPF_TCP_SYN_SENT, + BPF_TCP_SYN_RECV, + BPF_TCP_FIN_WAIT1, + BPF_TCP_FIN_WAIT2, + BPF_TCP_TIME_WAIT, + BPF_TCP_CLOSE, + BPF_TCP_CLOSE_WAIT, + BPF_TCP_LAST_ACK, + BPF_TCP_LISTEN, + BPF_TCP_CLOSING, /* Now a valid state */ + BPF_TCP_NEW_SYN_RECV, + + BPF_TCP_MAX_STATES /* Leave at the end! */ +}; + +#define TCP_BPF_IW 1001 /* Set TCP initial congestion window */ +#define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */ + +struct bpf_perf_event_value { + __u64 counter; + __u64 enabled; + __u64 running; +}; + +#define BPF_DEVCG_ACC_MKNOD (1ULL << 0) +#define BPF_DEVCG_ACC_READ (1ULL << 1) +#define BPF_DEVCG_ACC_WRITE (1ULL << 2) + +#define BPF_DEVCG_DEV_BLOCK (1ULL << 0) +#define BPF_DEVCG_DEV_CHAR (1ULL << 1) + +struct bpf_cgroup_dev_ctx { + /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */ + __u32 access_type; + __u32 major; + __u32 minor; +}; + +struct bpf_raw_tracepoint_args { + __u64 args[0]; +}; + +/* DIRECT: Skip the FIB rules and go to FIB table associated with device + * OUTPUT: Do lookup from egress perspective; default is ingress + */ +#define BPF_FIB_LOOKUP_DIRECT BIT(0) +#define BPF_FIB_LOOKUP_OUTPUT BIT(1) + +enum { + BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */ + BPF_FIB_LKUP_RET_BLACKHOLE, /* dest is blackholed; can be dropped */ + BPF_FIB_LKUP_RET_UNREACHABLE, /* dest is unreachable; can be dropped */ + BPF_FIB_LKUP_RET_PROHIBIT, /* dest not allowed; can be dropped */ + BPF_FIB_LKUP_RET_NOT_FWDED, /* packet is not forwarded */ + BPF_FIB_LKUP_RET_FWD_DISABLED, /* fwding is not enabled on ingress */ + BPF_FIB_LKUP_RET_UNSUPP_LWT, /* fwd requires encapsulation */ + BPF_FIB_LKUP_RET_NO_NEIGH, /* no neighbor entry for nh */ + BPF_FIB_LKUP_RET_FRAG_NEEDED, /* fragmentation required to fwd */ +}; + +struct bpf_fib_lookup { + /* input: network family for lookup (AF_INET, AF_INET6) + * output: network family of egress nexthop + */ + __u8 family; + + /* set if lookup is to consider L4 data - e.g., FIB rules */ + __u8 l4_protocol; + __be16 sport; + __be16 dport; + + /* total length of packet from network header - used for MTU check */ + __u16 tot_len; + + /* input: L3 device index for lookup + * output: device index from FIB lookup + */ + __u32 ifindex; + + union { + /* inputs to lookup */ + __u8 tos; /* AF_INET */ + __be32 flowinfo; /* AF_INET6, flow_label + priority */ + + /* output: metric of fib result (IPv4/IPv6 only) */ + __u32 rt_metric; + }; + + union { + __be32 ipv4_src; + __u32 ipv6_src[4]; /* in6_addr; network order */ + }; + + /* input to bpf_fib_lookup, ipv{4,6}_dst is destination address in + * network header. output: bpf_fib_lookup sets to gateway address + * if FIB lookup returns gateway route + */ + union { + __be32 ipv4_dst; + __u32 ipv6_dst[4]; /* in6_addr; network order */ + }; + + /* output */ + __be16 h_vlan_proto; + __be16 h_vlan_TCI; + __u8 smac[6]; /* ETH_ALEN */ + __u8 dmac[6]; /* ETH_ALEN */ +}; + +enum bpf_task_fd_type { + BPF_FD_TYPE_RAW_TRACEPOINT, /* tp name */ + BPF_FD_TYPE_TRACEPOINT, /* tp name */ + BPF_FD_TYPE_KPROBE, /* (symbol + offset) or addr */ + BPF_FD_TYPE_KRETPROBE, /* (symbol + offset) or addr */ + BPF_FD_TYPE_UPROBE, /* filename + offset */ + BPF_FD_TYPE_URETPROBE, /* filename + offset */ +}; + +struct bpf_flow_keys { + __u16 nhoff; + __u16 thoff; + __u16 addr_proto; /* ETH_P_* of valid addrs */ + __u8 is_frag; + __u8 is_first_frag; + __u8 is_encap; + __u8 ip_proto; + __be16 n_proto; + __be16 sport; + __be16 dport; + union { + struct { + __be32 ipv4_src; + __be32 ipv4_dst; + }; + struct { + __u32 ipv6_src[4]; /* in6_addr; network order */ + __u32 ipv6_dst[4]; /* in6_addr; network order */ + }; + }; +}; + +#endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/userspace/libscap/compat/bpf_common.h b/userspace/libscap/compat/bpf_common.h new file mode 100644 index 0000000000..ee97668bda --- /dev/null +++ b/userspace/libscap/compat/bpf_common.h @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI__LINUX_BPF_COMMON_H__ +#define _UAPI__LINUX_BPF_COMMON_H__ + +/* Instruction classes */ +#define BPF_CLASS(code) ((code) & 0x07) +#define BPF_LD 0x00 +#define BPF_LDX 0x01 +#define BPF_ST 0x02 +#define BPF_STX 0x03 +#define BPF_ALU 0x04 +#define BPF_JMP 0x05 +#define BPF_RET 0x06 +#define BPF_MISC 0x07 + +/* ld/ldx fields */ +#define BPF_SIZE(code) ((code) & 0x18) +#define BPF_W 0x00 /* 32-bit */ +#define BPF_H 0x08 /* 16-bit */ +#define BPF_B 0x10 /* 8-bit */ +/* eBPF BPF_DW 0x18 64-bit */ +#define BPF_MODE(code) ((code) & 0xe0) +#define BPF_IMM 0x00 +#define BPF_ABS 0x20 +#define BPF_IND 0x40 +#define BPF_MEM 0x60 +#define BPF_LEN 0x80 +#define BPF_MSH 0xa0 + +/* alu/jmp fields */ +#define BPF_OP(code) ((code) & 0xf0) +#define BPF_ADD 0x00 +#define BPF_SUB 0x10 +#define BPF_MUL 0x20 +#define BPF_DIV 0x30 +#define BPF_OR 0x40 +#define BPF_AND 0x50 +#define BPF_LSH 0x60 +#define BPF_RSH 0x70 +#define BPF_NEG 0x80 +#define BPF_MOD 0x90 +#define BPF_XOR 0xa0 + +#define BPF_JA 0x00 +#define BPF_JEQ 0x10 +#define BPF_JGT 0x20 +#define BPF_JGE 0x30 +#define BPF_JSET 0x40 +#define BPF_SRC(code) ((code) & 0x08) +#define BPF_K 0x00 +#define BPF_X 0x08 + +#ifndef BPF_MAXINSNS +#define BPF_MAXINSNS 4096 +#endif + +#endif /* _UAPI__LINUX_BPF_COMMON_H__ */ diff --git a/userspace/libscap/compat/misc.h b/userspace/libscap/compat/misc.h new file mode 100644 index 0000000000..28f136ee6f --- /dev/null +++ b/userspace/libscap/compat/misc.h @@ -0,0 +1,42 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#ifndef __COMPAT_MISC_H +#define __COMPAT_MISC_H + +#ifndef __NR_bpf +#ifdef __x86_64__ +#define __NR_bpf 321 +#else +#define __NR_bpf 357 +#endif /* __x86_64__ */ +#endif /* __NR_bpf */ + +#ifndef CLOCK_BOOTTIME +#define CLOCK_BOOTTIME 7 +#endif + +/* + O_TMPFILE was introduced in Linux >= 3.11 and defined as (__O_TMPFILE | O_DIRECTORY). + To maintain compatiblity with different build environments, the below is added. +*/ +#ifndef O_TMPFILE +#define O_TMPFILE 020200000 +#endif + +#endif diff --git a/userspace/libscap/compat/perf_event.h b/userspace/libscap/compat/perf_event.h new file mode 100644 index 0000000000..1e301785e4 --- /dev/null +++ b/userspace/libscap/compat/perf_event.h @@ -0,0 +1,1130 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Performance events: + * + * Copyright (C) 2008-2009, Thomas Gleixner + * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar + * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra + * + * Data type definitions, declarations, prototypes. + * + * Started by: Thomas Gleixner and Ingo Molnar + * + * For licencing details see kernel-base/COPYING + */ +#ifndef _UAPI_LINUX_PERF_EVENT_H +#define _UAPI_LINUX_PERF_EVENT_H + +#include +#include +#include + +/* + * User-space ABI bits: + */ + +/* + * attr.type + */ +enum perf_type_id { + PERF_TYPE_HARDWARE = 0, + PERF_TYPE_SOFTWARE = 1, + PERF_TYPE_TRACEPOINT = 2, + PERF_TYPE_HW_CACHE = 3, + PERF_TYPE_RAW = 4, + PERF_TYPE_BREAKPOINT = 5, + + PERF_TYPE_MAX, /* non-ABI */ +}; + +/* + * Generalized performance event event_id types, used by the + * attr.event_id parameter of the sys_perf_event_open() + * syscall: + */ +enum perf_hw_id { + /* + * Common hardware events, generalized by the kernel: + */ + PERF_COUNT_HW_CPU_CYCLES = 0, + PERF_COUNT_HW_INSTRUCTIONS = 1, + PERF_COUNT_HW_CACHE_REFERENCES = 2, + PERF_COUNT_HW_CACHE_MISSES = 3, + PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4, + PERF_COUNT_HW_BRANCH_MISSES = 5, + PERF_COUNT_HW_BUS_CYCLES = 6, + PERF_COUNT_HW_STALLED_CYCLES_FRONTEND = 7, + PERF_COUNT_HW_STALLED_CYCLES_BACKEND = 8, + PERF_COUNT_HW_REF_CPU_CYCLES = 9, + + PERF_COUNT_HW_MAX, /* non-ABI */ +}; + +/* + * Generalized hardware cache events: + * + * { L1-D, L1-I, LLC, ITLB, DTLB, BPU, NODE } x + * { read, write, prefetch } x + * { accesses, misses } + */ +enum perf_hw_cache_id { + PERF_COUNT_HW_CACHE_L1D = 0, + PERF_COUNT_HW_CACHE_L1I = 1, + PERF_COUNT_HW_CACHE_LL = 2, + PERF_COUNT_HW_CACHE_DTLB = 3, + PERF_COUNT_HW_CACHE_ITLB = 4, + PERF_COUNT_HW_CACHE_BPU = 5, + PERF_COUNT_HW_CACHE_NODE = 6, + + PERF_COUNT_HW_CACHE_MAX, /* non-ABI */ +}; + +enum perf_hw_cache_op_id { + PERF_COUNT_HW_CACHE_OP_READ = 0, + PERF_COUNT_HW_CACHE_OP_WRITE = 1, + PERF_COUNT_HW_CACHE_OP_PREFETCH = 2, + + PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */ +}; + +enum perf_hw_cache_op_result_id { + PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0, + PERF_COUNT_HW_CACHE_RESULT_MISS = 1, + + PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */ +}; + +/* + * Special "software" events provided by the kernel, even if the hardware + * does not support performance events. These events measure various + * physical and sw events of the kernel (and allow the profiling of them as + * well): + */ +enum perf_sw_ids { + PERF_COUNT_SW_CPU_CLOCK = 0, + PERF_COUNT_SW_TASK_CLOCK = 1, + PERF_COUNT_SW_PAGE_FAULTS = 2, + PERF_COUNT_SW_CONTEXT_SWITCHES = 3, + PERF_COUNT_SW_CPU_MIGRATIONS = 4, + PERF_COUNT_SW_PAGE_FAULTS_MIN = 5, + PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6, + PERF_COUNT_SW_ALIGNMENT_FAULTS = 7, + PERF_COUNT_SW_EMULATION_FAULTS = 8, + PERF_COUNT_SW_DUMMY = 9, + PERF_COUNT_SW_BPF_OUTPUT = 10, + + PERF_COUNT_SW_MAX, /* non-ABI */ +}; + +/* + * Bits that can be set in attr.sample_type to request information + * in the overflow packets. + */ +enum perf_event_sample_format { + PERF_SAMPLE_IP = 1U << 0, + PERF_SAMPLE_TID = 1U << 1, + PERF_SAMPLE_TIME = 1U << 2, + PERF_SAMPLE_ADDR = 1U << 3, + PERF_SAMPLE_READ = 1U << 4, + PERF_SAMPLE_CALLCHAIN = 1U << 5, + PERF_SAMPLE_ID = 1U << 6, + PERF_SAMPLE_CPU = 1U << 7, + PERF_SAMPLE_PERIOD = 1U << 8, + PERF_SAMPLE_STREAM_ID = 1U << 9, + PERF_SAMPLE_RAW = 1U << 10, + PERF_SAMPLE_BRANCH_STACK = 1U << 11, + PERF_SAMPLE_REGS_USER = 1U << 12, + PERF_SAMPLE_STACK_USER = 1U << 13, + PERF_SAMPLE_WEIGHT = 1U << 14, + PERF_SAMPLE_DATA_SRC = 1U << 15, + PERF_SAMPLE_IDENTIFIER = 1U << 16, + PERF_SAMPLE_TRANSACTION = 1U << 17, + PERF_SAMPLE_REGS_INTR = 1U << 18, + PERF_SAMPLE_PHYS_ADDR = 1U << 19, + + PERF_SAMPLE_MAX = 1U << 20, /* non-ABI */ + + __PERF_SAMPLE_CALLCHAIN_EARLY = 1ULL << 63, /* non-ABI; internal use */ +}; + +/* + * values to program into branch_sample_type when PERF_SAMPLE_BRANCH is set + * + * If the user does not pass priv level information via branch_sample_type, + * the kernel uses the event's priv level. Branch and event priv levels do + * not have to match. Branch priv level is checked for permissions. + * + * The branch types can be combined, however BRANCH_ANY covers all types + * of branches and therefore it supersedes all the other types. + */ +enum perf_branch_sample_type_shift { + PERF_SAMPLE_BRANCH_USER_SHIFT = 0, /* user branches */ + PERF_SAMPLE_BRANCH_KERNEL_SHIFT = 1, /* kernel branches */ + PERF_SAMPLE_BRANCH_HV_SHIFT = 2, /* hypervisor branches */ + + PERF_SAMPLE_BRANCH_ANY_SHIFT = 3, /* any branch types */ + PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT = 4, /* any call branch */ + PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT = 5, /* any return branch */ + PERF_SAMPLE_BRANCH_IND_CALL_SHIFT = 6, /* indirect calls */ + PERF_SAMPLE_BRANCH_ABORT_TX_SHIFT = 7, /* transaction aborts */ + PERF_SAMPLE_BRANCH_IN_TX_SHIFT = 8, /* in transaction */ + PERF_SAMPLE_BRANCH_NO_TX_SHIFT = 9, /* not in transaction */ + PERF_SAMPLE_BRANCH_COND_SHIFT = 10, /* conditional branches */ + + PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT = 11, /* call/ret stack */ + PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT = 12, /* indirect jumps */ + PERF_SAMPLE_BRANCH_CALL_SHIFT = 13, /* direct call */ + + PERF_SAMPLE_BRANCH_NO_FLAGS_SHIFT = 14, /* no flags */ + PERF_SAMPLE_BRANCH_NO_CYCLES_SHIFT = 15, /* no cycles */ + + PERF_SAMPLE_BRANCH_TYPE_SAVE_SHIFT = 16, /* save branch type */ + + PERF_SAMPLE_BRANCH_MAX_SHIFT /* non-ABI */ +}; + +enum perf_branch_sample_type { + PERF_SAMPLE_BRANCH_USER = 1U << PERF_SAMPLE_BRANCH_USER_SHIFT, + PERF_SAMPLE_BRANCH_KERNEL = 1U << PERF_SAMPLE_BRANCH_KERNEL_SHIFT, + PERF_SAMPLE_BRANCH_HV = 1U << PERF_SAMPLE_BRANCH_HV_SHIFT, + + PERF_SAMPLE_BRANCH_ANY = 1U << PERF_SAMPLE_BRANCH_ANY_SHIFT, + PERF_SAMPLE_BRANCH_ANY_CALL = 1U << PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT, + PERF_SAMPLE_BRANCH_ANY_RETURN = 1U << PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT, + PERF_SAMPLE_BRANCH_IND_CALL = 1U << PERF_SAMPLE_BRANCH_IND_CALL_SHIFT, + PERF_SAMPLE_BRANCH_ABORT_TX = 1U << PERF_SAMPLE_BRANCH_ABORT_TX_SHIFT, + PERF_SAMPLE_BRANCH_IN_TX = 1U << PERF_SAMPLE_BRANCH_IN_TX_SHIFT, + PERF_SAMPLE_BRANCH_NO_TX = 1U << PERF_SAMPLE_BRANCH_NO_TX_SHIFT, + PERF_SAMPLE_BRANCH_COND = 1U << PERF_SAMPLE_BRANCH_COND_SHIFT, + + PERF_SAMPLE_BRANCH_CALL_STACK = 1U << PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT, + PERF_SAMPLE_BRANCH_IND_JUMP = 1U << PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT, + PERF_SAMPLE_BRANCH_CALL = 1U << PERF_SAMPLE_BRANCH_CALL_SHIFT, + + PERF_SAMPLE_BRANCH_NO_FLAGS = 1U << PERF_SAMPLE_BRANCH_NO_FLAGS_SHIFT, + PERF_SAMPLE_BRANCH_NO_CYCLES = 1U << PERF_SAMPLE_BRANCH_NO_CYCLES_SHIFT, + + PERF_SAMPLE_BRANCH_TYPE_SAVE = + 1U << PERF_SAMPLE_BRANCH_TYPE_SAVE_SHIFT, + + PERF_SAMPLE_BRANCH_MAX = 1U << PERF_SAMPLE_BRANCH_MAX_SHIFT, +}; + +/* + * Common flow change classification + */ +enum { + PERF_BR_UNKNOWN = 0, /* unknown */ + PERF_BR_COND = 1, /* conditional */ + PERF_BR_UNCOND = 2, /* unconditional */ + PERF_BR_IND = 3, /* indirect */ + PERF_BR_CALL = 4, /* function call */ + PERF_BR_IND_CALL = 5, /* indirect function call */ + PERF_BR_RET = 6, /* function return */ + PERF_BR_SYSCALL = 7, /* syscall */ + PERF_BR_SYSRET = 8, /* syscall return */ + PERF_BR_COND_CALL = 9, /* conditional function call */ + PERF_BR_COND_RET = 10, /* conditional function return */ + PERF_BR_MAX, +}; + +#define PERF_SAMPLE_BRANCH_PLM_ALL \ + (PERF_SAMPLE_BRANCH_USER|\ + PERF_SAMPLE_BRANCH_KERNEL|\ + PERF_SAMPLE_BRANCH_HV) + +/* + * Values to determine ABI of the registers dump. + */ +enum perf_sample_regs_abi { + PERF_SAMPLE_REGS_ABI_NONE = 0, + PERF_SAMPLE_REGS_ABI_32 = 1, + PERF_SAMPLE_REGS_ABI_64 = 2, +}; + +/* + * Values for the memory transaction event qualifier, mostly for + * abort events. Multiple bits can be set. + */ +enum { + PERF_TXN_ELISION = (1 << 0), /* From elision */ + PERF_TXN_TRANSACTION = (1 << 1), /* From transaction */ + PERF_TXN_SYNC = (1 << 2), /* Instruction is related */ + PERF_TXN_ASYNC = (1 << 3), /* Instruction not related */ + PERF_TXN_RETRY = (1 << 4), /* Retry possible */ + PERF_TXN_CONFLICT = (1 << 5), /* Conflict abort */ + PERF_TXN_CAPACITY_WRITE = (1 << 6), /* Capacity write abort */ + PERF_TXN_CAPACITY_READ = (1 << 7), /* Capacity read abort */ + + PERF_TXN_MAX = (1 << 8), /* non-ABI */ + + /* bits 32..63 are reserved for the abort code */ + + PERF_TXN_ABORT_MASK = (0xffffffffULL << 32), + PERF_TXN_ABORT_SHIFT = 32, +}; + +/* + * The format of the data returned by read() on a perf event fd, + * as specified by attr.read_format: + * + * struct read_format { + * { u64 value; + * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED + * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING + * { u64 id; } && PERF_FORMAT_ID + * } && !PERF_FORMAT_GROUP + * + * { u64 nr; + * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED + * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING + * { u64 value; + * { u64 id; } && PERF_FORMAT_ID + * } cntr[nr]; + * } && PERF_FORMAT_GROUP + * }; + */ +enum perf_event_read_format { + PERF_FORMAT_TOTAL_TIME_ENABLED = 1U << 0, + PERF_FORMAT_TOTAL_TIME_RUNNING = 1U << 1, + PERF_FORMAT_ID = 1U << 2, + PERF_FORMAT_GROUP = 1U << 3, + + PERF_FORMAT_MAX = 1U << 4, /* non-ABI */ +}; + +#define PERF_ATTR_SIZE_VER0 64 /* sizeof first published struct */ +#define PERF_ATTR_SIZE_VER1 72 /* add: config2 */ +#define PERF_ATTR_SIZE_VER2 80 /* add: branch_sample_type */ +#define PERF_ATTR_SIZE_VER3 96 /* add: sample_regs_user */ + /* add: sample_stack_user */ +#define PERF_ATTR_SIZE_VER4 104 /* add: sample_regs_intr */ +#define PERF_ATTR_SIZE_VER5 112 /* add: aux_watermark */ + +/* + * Hardware event_id to monitor via a performance monitoring event: + * + * @sample_max_stack: Max number of frame pointers in a callchain, + * should be < /proc/sys/kernel/perf_event_max_stack + */ +struct perf_event_attr { + + /* + * Major type: hardware/software/tracepoint/etc. + */ + __u32 type; + + /* + * Size of the attr structure, for fwd/bwd compat. + */ + __u32 size; + + /* + * Type specific configuration information. + */ + __u64 config; + + union { + __u64 sample_period; + __u64 sample_freq; + }; + + __u64 sample_type; + __u64 read_format; + + __u64 disabled : 1, /* off by default */ + inherit : 1, /* children inherit it */ + pinned : 1, /* must always be on PMU */ + exclusive : 1, /* only group on PMU */ + exclude_user : 1, /* don't count user */ + exclude_kernel : 1, /* ditto kernel */ + exclude_hv : 1, /* ditto hypervisor */ + exclude_idle : 1, /* don't count when idle */ + mmap : 1, /* include mmap data */ + comm : 1, /* include comm data */ + freq : 1, /* use freq, not period */ + inherit_stat : 1, /* per task counts */ + enable_on_exec : 1, /* next exec enables */ + task : 1, /* trace fork/exit */ + watermark : 1, /* wakeup_watermark */ + /* + * precise_ip: + * + * 0 - SAMPLE_IP can have arbitrary skid + * 1 - SAMPLE_IP must have constant skid + * 2 - SAMPLE_IP requested to have 0 skid + * 3 - SAMPLE_IP must have 0 skid + * + * See also PERF_RECORD_MISC_EXACT_IP + */ + precise_ip : 2, /* skid constraint */ + mmap_data : 1, /* non-exec mmap data */ + sample_id_all : 1, /* sample_type all events */ + + exclude_host : 1, /* don't count in host */ + exclude_guest : 1, /* don't count in guest */ + + exclude_callchain_kernel : 1, /* exclude kernel callchains */ + exclude_callchain_user : 1, /* exclude user callchains */ + mmap2 : 1, /* include mmap with inode data */ + comm_exec : 1, /* flag comm events that are due to an exec */ + use_clockid : 1, /* use @clockid for time fields */ + context_switch : 1, /* context switch data */ + write_backward : 1, /* Write ring buffer from end to beginning */ + namespaces : 1, /* include namespaces data */ + __reserved_1 : 35; + + union { + __u32 wakeup_events; /* wakeup every n events */ + __u32 wakeup_watermark; /* bytes before wakeup */ + }; + + __u32 bp_type; + union { + __u64 bp_addr; + __u64 kprobe_func; /* for perf_kprobe */ + __u64 uprobe_path; /* for perf_uprobe */ + __u64 config1; /* extension of config */ + }; + union { + __u64 bp_len; + __u64 kprobe_addr; /* when kprobe_func == NULL */ + __u64 probe_offset; /* for perf_[k,u]probe */ + __u64 config2; /* extension of config1 */ + }; + __u64 branch_sample_type; /* enum perf_branch_sample_type */ + + /* + * Defines set of user regs to dump on samples. + * See asm/perf_regs.h for details. + */ + __u64 sample_regs_user; + + /* + * Defines size of the user stack to dump on samples. + */ + __u32 sample_stack_user; + + __s32 clockid; + /* + * Defines set of regs to dump for each sample + * state captured on: + * - precise = 0: PMU interrupt + * - precise > 0: sampled instruction + * + * See asm/perf_regs.h for details. + */ + __u64 sample_regs_intr; + + /* + * Wakeup watermark for AUX area + */ + __u32 aux_watermark; + __u16 sample_max_stack; + __u16 __reserved_2; /* align to __u64 */ +}; + +/* + * Structure used by below PERF_EVENT_IOC_QUERY_BPF command + * to query bpf programs attached to the same perf tracepoint + * as the given perf event. + */ +struct perf_event_query_bpf { + /* + * The below ids array length + */ + __u32 ids_len; + /* + * Set by the kernel to indicate the number of + * available programs + */ + __u32 prog_cnt; + /* + * User provided buffer to store program ids + */ + __u32 ids[0]; +}; + +#define perf_flags(attr) (*(&(attr)->read_format + 1)) + +/* + * Ioctls that can be done on a perf event fd: + */ +#define PERF_EVENT_IOC_ENABLE _IO ('$', 0) +#define PERF_EVENT_IOC_DISABLE _IO ('$', 1) +#define PERF_EVENT_IOC_REFRESH _IO ('$', 2) +#define PERF_EVENT_IOC_RESET _IO ('$', 3) +#define PERF_EVENT_IOC_PERIOD _IOW('$', 4, __u64) +#define PERF_EVENT_IOC_SET_OUTPUT _IO ('$', 5) +#define PERF_EVENT_IOC_SET_FILTER _IOW('$', 6, char *) +#define PERF_EVENT_IOC_ID _IOR('$', 7, __u64 *) +#define PERF_EVENT_IOC_SET_BPF _IOW('$', 8, __u32) +#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32) +#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *) +#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) + +enum perf_event_ioc_flags { + PERF_IOC_FLAG_GROUP = 1U << 0, +}; + +/* + * Structure of the page that can be mapped via mmap + */ +struct perf_event_mmap_page { + __u32 version; /* version number of this structure */ + __u32 compat_version; /* lowest version this is compat with */ + + /* + * Bits needed to read the hw events in user-space. + * + * u32 seq, time_mult, time_shift, index, width; + * u64 count, enabled, running; + * u64 cyc, time_offset; + * s64 pmc = 0; + * + * do { + * seq = pc->lock; + * barrier() + * + * enabled = pc->time_enabled; + * running = pc->time_running; + * + * if (pc->cap_usr_time && enabled != running) { + * cyc = rdtsc(); + * time_offset = pc->time_offset; + * time_mult = pc->time_mult; + * time_shift = pc->time_shift; + * } + * + * index = pc->index; + * count = pc->offset; + * if (pc->cap_user_rdpmc && index) { + * width = pc->pmc_width; + * pmc = rdpmc(index - 1); + * } + * + * barrier(); + * } while (pc->lock != seq); + * + * NOTE: for obvious reason this only works on self-monitoring + * processes. + */ + __u32 lock; /* seqlock for synchronization */ + __u32 index; /* hardware event identifier */ + __s64 offset; /* add to hardware event value */ + __u64 time_enabled; /* time event active */ + __u64 time_running; /* time event on cpu */ + union { + __u64 capabilities; + struct { + __u64 cap_bit0 : 1, /* Always 0, deprecated, see commit 860f085b74e9 */ + cap_bit0_is_deprecated : 1, /* Always 1, signals that bit 0 is zero */ + + cap_user_rdpmc : 1, /* The RDPMC instruction can be used to read counts */ + cap_user_time : 1, /* The time_* fields are used */ + cap_user_time_zero : 1, /* The time_zero field is used */ + cap_____res : 59; + }; + }; + + /* + * If cap_user_rdpmc this field provides the bit-width of the value + * read using the rdpmc() or equivalent instruction. This can be used + * to sign extend the result like: + * + * pmc <<= 64 - width; + * pmc >>= 64 - width; // signed shift right + * count += pmc; + */ + __u16 pmc_width; + + /* + * If cap_usr_time the below fields can be used to compute the time + * delta since time_enabled (in ns) using rdtsc or similar. + * + * u64 quot, rem; + * u64 delta; + * + * quot = (cyc >> time_shift); + * rem = cyc & (((u64)1 << time_shift) - 1); + * delta = time_offset + quot * time_mult + + * ((rem * time_mult) >> time_shift); + * + * Where time_offset,time_mult,time_shift and cyc are read in the + * seqcount loop described above. This delta can then be added to + * enabled and possible running (if index), improving the scaling: + * + * enabled += delta; + * if (index) + * running += delta; + * + * quot = count / running; + * rem = count % running; + * count = quot * enabled + (rem * enabled) / running; + */ + __u16 time_shift; + __u32 time_mult; + __u64 time_offset; + /* + * If cap_usr_time_zero, the hardware clock (e.g. TSC) can be calculated + * from sample timestamps. + * + * time = timestamp - time_zero; + * quot = time / time_mult; + * rem = time % time_mult; + * cyc = (quot << time_shift) + (rem << time_shift) / time_mult; + * + * And vice versa: + * + * quot = cyc >> time_shift; + * rem = cyc & (((u64)1 << time_shift) - 1); + * timestamp = time_zero + quot * time_mult + + * ((rem * time_mult) >> time_shift); + */ + __u64 time_zero; + __u32 size; /* Header size up to __reserved[] fields. */ + + /* + * Hole for extension of the self monitor capabilities + */ + + __u8 __reserved[118*8+4]; /* align to 1k. */ + + /* + * Control data for the mmap() data buffer. + * + * User-space reading the @data_head value should issue an smp_rmb(), + * after reading this value. + * + * When the mapping is PROT_WRITE the @data_tail value should be + * written by userspace to reflect the last read data, after issuing + * an smp_mb() to separate the data read from the ->data_tail store. + * In this case the kernel will not over-write unread data. + * + * See perf_output_put_handle() for the data ordering. + * + * data_{offset,size} indicate the location and size of the perf record + * buffer within the mmapped area. + */ + __u64 data_head; /* head in the data section */ + __u64 data_tail; /* user-space written tail */ + __u64 data_offset; /* where the buffer starts */ + __u64 data_size; /* data buffer size */ + + /* + * AUX area is defined by aux_{offset,size} fields that should be set + * by the userspace, so that + * + * aux_offset >= data_offset + data_size + * + * prior to mmap()ing it. Size of the mmap()ed area should be aux_size. + * + * Ring buffer pointers aux_{head,tail} have the same semantics as + * data_{head,tail} and same ordering rules apply. + */ + __u64 aux_head; + __u64 aux_tail; + __u64 aux_offset; + __u64 aux_size; +}; + +#define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0) +#define PERF_RECORD_MISC_CPUMODE_UNKNOWN (0 << 0) +#define PERF_RECORD_MISC_KERNEL (1 << 0) +#define PERF_RECORD_MISC_USER (2 << 0) +#define PERF_RECORD_MISC_HYPERVISOR (3 << 0) +#define PERF_RECORD_MISC_GUEST_KERNEL (4 << 0) +#define PERF_RECORD_MISC_GUEST_USER (5 << 0) + +/* + * Indicates that /proc/PID/maps parsing are truncated by time out. + */ +#define PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT (1 << 12) +/* + * Following PERF_RECORD_MISC_* are used on different + * events, so can reuse the same bit position: + * + * PERF_RECORD_MISC_MMAP_DATA - PERF_RECORD_MMAP* events + * PERF_RECORD_MISC_COMM_EXEC - PERF_RECORD_COMM event + * PERF_RECORD_MISC_SWITCH_OUT - PERF_RECORD_SWITCH* events + */ +#define PERF_RECORD_MISC_MMAP_DATA (1 << 13) +#define PERF_RECORD_MISC_COMM_EXEC (1 << 13) +#define PERF_RECORD_MISC_SWITCH_OUT (1 << 13) +/* + * These PERF_RECORD_MISC_* flags below are safely reused + * for the following events: + * + * PERF_RECORD_MISC_EXACT_IP - PERF_RECORD_SAMPLE of precise events + * PERF_RECORD_MISC_SWITCH_OUT_PREEMPT - PERF_RECORD_SWITCH* events + * + * + * PERF_RECORD_MISC_EXACT_IP: + * Indicates that the content of PERF_SAMPLE_IP points to + * the actual instruction that triggered the event. See also + * perf_event_attr::precise_ip. + * + * PERF_RECORD_MISC_SWITCH_OUT_PREEMPT: + * Indicates that thread was preempted in TASK_RUNNING state. + */ +#define PERF_RECORD_MISC_EXACT_IP (1 << 14) +#define PERF_RECORD_MISC_SWITCH_OUT_PREEMPT (1 << 14) +/* + * Reserve the last bit to indicate some extended misc field + */ +#define PERF_RECORD_MISC_EXT_RESERVED (1 << 15) + +struct perf_event_header { + __u32 type; + __u16 misc; + __u16 size; +}; + +struct perf_ns_link_info { + __u64 dev; + __u64 ino; +}; + +enum { + NET_NS_INDEX = 0, + UTS_NS_INDEX = 1, + IPC_NS_INDEX = 2, + PID_NS_INDEX = 3, + USER_NS_INDEX = 4, + MNT_NS_INDEX = 5, + CGROUP_NS_INDEX = 6, + + NR_NAMESPACES, /* number of available namespaces */ +}; + +enum perf_event_type { + + /* + * If perf_event_attr.sample_id_all is set then all event types will + * have the sample_type selected fields related to where/when + * (identity) an event took place (TID, TIME, ID, STREAM_ID, CPU, + * IDENTIFIER) described in PERF_RECORD_SAMPLE below, it will be stashed + * just after the perf_event_header and the fields already present for + * the existing fields, i.e. at the end of the payload. That way a newer + * perf.data file will be supported by older perf tools, with these new + * optional fields being ignored. + * + * struct sample_id { + * { u32 pid, tid; } && PERF_SAMPLE_TID + * { u64 time; } && PERF_SAMPLE_TIME + * { u64 id; } && PERF_SAMPLE_ID + * { u64 stream_id;} && PERF_SAMPLE_STREAM_ID + * { u32 cpu, res; } && PERF_SAMPLE_CPU + * { u64 id; } && PERF_SAMPLE_IDENTIFIER + * } && perf_event_attr::sample_id_all + * + * Note that PERF_SAMPLE_IDENTIFIER duplicates PERF_SAMPLE_ID. The + * advantage of PERF_SAMPLE_IDENTIFIER is that its position is fixed + * relative to header.size. + */ + + /* + * The MMAP events record the PROT_EXEC mappings so that we can + * correlate userspace IPs to code. They have the following structure: + * + * struct { + * struct perf_event_header header; + * + * u32 pid, tid; + * u64 addr; + * u64 len; + * u64 pgoff; + * char filename[]; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_MMAP = 1, + + /* + * struct { + * struct perf_event_header header; + * u64 id; + * u64 lost; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_LOST = 2, + + /* + * struct { + * struct perf_event_header header; + * + * u32 pid, tid; + * char comm[]; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_COMM = 3, + + /* + * struct { + * struct perf_event_header header; + * u32 pid, ppid; + * u32 tid, ptid; + * u64 time; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_EXIT = 4, + + /* + * struct { + * struct perf_event_header header; + * u64 time; + * u64 id; + * u64 stream_id; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_THROTTLE = 5, + PERF_RECORD_UNTHROTTLE = 6, + + /* + * struct { + * struct perf_event_header header; + * u32 pid, ppid; + * u32 tid, ptid; + * u64 time; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_FORK = 7, + + /* + * struct { + * struct perf_event_header header; + * u32 pid, tid; + * + * struct read_format values; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_READ = 8, + + /* + * struct { + * struct perf_event_header header; + * + * # + * # Note that PERF_SAMPLE_IDENTIFIER duplicates PERF_SAMPLE_ID. + * # The advantage of PERF_SAMPLE_IDENTIFIER is that its position + * # is fixed relative to header. + * # + * + * { u64 id; } && PERF_SAMPLE_IDENTIFIER + * { u64 ip; } && PERF_SAMPLE_IP + * { u32 pid, tid; } && PERF_SAMPLE_TID + * { u64 time; } && PERF_SAMPLE_TIME + * { u64 addr; } && PERF_SAMPLE_ADDR + * { u64 id; } && PERF_SAMPLE_ID + * { u64 stream_id;} && PERF_SAMPLE_STREAM_ID + * { u32 cpu, res; } && PERF_SAMPLE_CPU + * { u64 period; } && PERF_SAMPLE_PERIOD + * + * { struct read_format values; } && PERF_SAMPLE_READ + * + * { u64 nr, + * u64 ips[nr]; } && PERF_SAMPLE_CALLCHAIN + * + * # + * # The RAW record below is opaque data wrt the ABI + * # + * # That is, the ABI doesn't make any promises wrt to + * # the stability of its content, it may vary depending + * # on event, hardware, kernel version and phase of + * # the moon. + * # + * # In other words, PERF_SAMPLE_RAW contents are not an ABI. + * # + * + * { u32 size; + * char data[size];}&& PERF_SAMPLE_RAW + * + * { u64 nr; + * { u64 from, to, flags } lbr[nr];} && PERF_SAMPLE_BRANCH_STACK + * + * { u64 abi; # enum perf_sample_regs_abi + * u64 regs[weight(mask)]; } && PERF_SAMPLE_REGS_USER + * + * { u64 size; + * char data[size]; + * u64 dyn_size; } && PERF_SAMPLE_STACK_USER + * + * { u64 weight; } && PERF_SAMPLE_WEIGHT + * { u64 data_src; } && PERF_SAMPLE_DATA_SRC + * { u64 transaction; } && PERF_SAMPLE_TRANSACTION + * { u64 abi; # enum perf_sample_regs_abi + * u64 regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR + * { u64 phys_addr;} && PERF_SAMPLE_PHYS_ADDR + * }; + */ + PERF_RECORD_SAMPLE = 9, + + /* + * The MMAP2 records are an augmented version of MMAP, they add + * maj, min, ino numbers to be used to uniquely identify each mapping + * + * struct { + * struct perf_event_header header; + * + * u32 pid, tid; + * u64 addr; + * u64 len; + * u64 pgoff; + * u32 maj; + * u32 min; + * u64 ino; + * u64 ino_generation; + * u32 prot, flags; + * char filename[]; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_MMAP2 = 10, + + /* + * Records that new data landed in the AUX buffer part. + * + * struct { + * struct perf_event_header header; + * + * u64 aux_offset; + * u64 aux_size; + * u64 flags; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_AUX = 11, + + /* + * Indicates that instruction trace has started + * + * struct { + * struct perf_event_header header; + * u32 pid; + * u32 tid; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_ITRACE_START = 12, + + /* + * Records the dropped/lost sample number. + * + * struct { + * struct perf_event_header header; + * + * u64 lost; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_LOST_SAMPLES = 13, + + /* + * Records a context switch in or out (flagged by + * PERF_RECORD_MISC_SWITCH_OUT). See also + * PERF_RECORD_SWITCH_CPU_WIDE. + * + * struct { + * struct perf_event_header header; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_SWITCH = 14, + + /* + * CPU-wide version of PERF_RECORD_SWITCH with next_prev_pid and + * next_prev_tid that are the next (switching out) or previous + * (switching in) pid/tid. + * + * struct { + * struct perf_event_header header; + * u32 next_prev_pid; + * u32 next_prev_tid; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_SWITCH_CPU_WIDE = 15, + + /* + * struct { + * struct perf_event_header header; + * u32 pid; + * u32 tid; + * u64 nr_namespaces; + * { u64 dev, inode; } [nr_namespaces]; + * struct sample_id sample_id; + * }; + */ + PERF_RECORD_NAMESPACES = 16, + + PERF_RECORD_MAX, /* non-ABI */ +}; + +#define PERF_MAX_STACK_DEPTH 127 +#define PERF_MAX_CONTEXTS_PER_STACK 8 + +enum perf_callchain_context { + PERF_CONTEXT_HV = (__u64)-32, + PERF_CONTEXT_KERNEL = (__u64)-128, + PERF_CONTEXT_USER = (__u64)-512, + + PERF_CONTEXT_GUEST = (__u64)-2048, + PERF_CONTEXT_GUEST_KERNEL = (__u64)-2176, + PERF_CONTEXT_GUEST_USER = (__u64)-2560, + + PERF_CONTEXT_MAX = (__u64)-4095, +}; + +/** + * PERF_RECORD_AUX::flags bits + */ +#define PERF_AUX_FLAG_TRUNCATED 0x01 /* record was truncated to fit */ +#define PERF_AUX_FLAG_OVERWRITE 0x02 /* snapshot from overwrite mode */ +#define PERF_AUX_FLAG_PARTIAL 0x04 /* record contains gaps */ +#define PERF_AUX_FLAG_COLLISION 0x08 /* sample collided with another */ + +#define PERF_FLAG_FD_NO_GROUP (1UL << 0) +#define PERF_FLAG_FD_OUTPUT (1UL << 1) +#define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */ +#define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */ + +#if defined(__LITTLE_ENDIAN_BITFIELD) +union perf_mem_data_src { + __u64 val; + struct { + __u64 mem_op:5, /* type of opcode */ + mem_lvl:14, /* memory hierarchy level */ + mem_snoop:5, /* snoop mode */ + mem_lock:2, /* lock instr */ + mem_dtlb:7, /* tlb access */ + mem_lvl_num:4, /* memory hierarchy level number */ + mem_remote:1, /* remote */ + mem_snoopx:2, /* snoop mode, ext */ + mem_rsvd:24; + }; +}; +#elif defined(__BIG_ENDIAN_BITFIELD) +union perf_mem_data_src { + __u64 val; + struct { + __u64 mem_rsvd:24, + mem_snoopx:2, /* snoop mode, ext */ + mem_remote:1, /* remote */ + mem_lvl_num:4, /* memory hierarchy level number */ + mem_dtlb:7, /* tlb access */ + mem_lock:2, /* lock instr */ + mem_snoop:5, /* snoop mode */ + mem_lvl:14, /* memory hierarchy level */ + mem_op:5; /* type of opcode */ + }; +}; +#else +#error "Unknown endianness" +#endif + +/* type of opcode (load/store/prefetch,code) */ +#define PERF_MEM_OP_NA 0x01 /* not available */ +#define PERF_MEM_OP_LOAD 0x02 /* load instruction */ +#define PERF_MEM_OP_STORE 0x04 /* store instruction */ +#define PERF_MEM_OP_PFETCH 0x08 /* prefetch */ +#define PERF_MEM_OP_EXEC 0x10 /* code (execution) */ +#define PERF_MEM_OP_SHIFT 0 + +/* memory hierarchy (memory level, hit or miss) */ +#define PERF_MEM_LVL_NA 0x01 /* not available */ +#define PERF_MEM_LVL_HIT 0x02 /* hit level */ +#define PERF_MEM_LVL_MISS 0x04 /* miss level */ +#define PERF_MEM_LVL_L1 0x08 /* L1 */ +#define PERF_MEM_LVL_LFB 0x10 /* Line Fill Buffer */ +#define PERF_MEM_LVL_L2 0x20 /* L2 */ +#define PERF_MEM_LVL_L3 0x40 /* L3 */ +#define PERF_MEM_LVL_LOC_RAM 0x80 /* Local DRAM */ +#define PERF_MEM_LVL_REM_RAM1 0x100 /* Remote DRAM (1 hop) */ +#define PERF_MEM_LVL_REM_RAM2 0x200 /* Remote DRAM (2 hops) */ +#define PERF_MEM_LVL_REM_CCE1 0x400 /* Remote Cache (1 hop) */ +#define PERF_MEM_LVL_REM_CCE2 0x800 /* Remote Cache (2 hops) */ +#define PERF_MEM_LVL_IO 0x1000 /* I/O memory */ +#define PERF_MEM_LVL_UNC 0x2000 /* Uncached memory */ +#define PERF_MEM_LVL_SHIFT 5 + +#define PERF_MEM_REMOTE_REMOTE 0x01 /* Remote */ +#define PERF_MEM_REMOTE_SHIFT 37 + +#define PERF_MEM_LVLNUM_L1 0x01 /* L1 */ +#define PERF_MEM_LVLNUM_L2 0x02 /* L2 */ +#define PERF_MEM_LVLNUM_L3 0x03 /* L3 */ +#define PERF_MEM_LVLNUM_L4 0x04 /* L4 */ +/* 5-0xa available */ +#define PERF_MEM_LVLNUM_ANY_CACHE 0x0b /* Any cache */ +#define PERF_MEM_LVLNUM_LFB 0x0c /* LFB */ +#define PERF_MEM_LVLNUM_RAM 0x0d /* RAM */ +#define PERF_MEM_LVLNUM_PMEM 0x0e /* PMEM */ +#define PERF_MEM_LVLNUM_NA 0x0f /* N/A */ + +#define PERF_MEM_LVLNUM_SHIFT 33 + +/* snoop mode */ +#define PERF_MEM_SNOOP_NA 0x01 /* not available */ +#define PERF_MEM_SNOOP_NONE 0x02 /* no snoop */ +#define PERF_MEM_SNOOP_HIT 0x04 /* snoop hit */ +#define PERF_MEM_SNOOP_MISS 0x08 /* snoop miss */ +#define PERF_MEM_SNOOP_HITM 0x10 /* snoop hit modified */ +#define PERF_MEM_SNOOP_SHIFT 19 + +#define PERF_MEM_SNOOPX_FWD 0x01 /* forward */ +/* 1 free */ +#define PERF_MEM_SNOOPX_SHIFT 37 + +/* locked instruction */ +#define PERF_MEM_LOCK_NA 0x01 /* not available */ +#define PERF_MEM_LOCK_LOCKED 0x02 /* locked transaction */ +#define PERF_MEM_LOCK_SHIFT 24 + +/* TLB access */ +#define PERF_MEM_TLB_NA 0x01 /* not available */ +#define PERF_MEM_TLB_HIT 0x02 /* hit level */ +#define PERF_MEM_TLB_MISS 0x04 /* miss level */ +#define PERF_MEM_TLB_L1 0x08 /* L1 */ +#define PERF_MEM_TLB_L2 0x10 /* L2 */ +#define PERF_MEM_TLB_WK 0x20 /* Hardware Walker*/ +#define PERF_MEM_TLB_OS 0x40 /* OS fault handler */ +#define PERF_MEM_TLB_SHIFT 26 + +#define PERF_MEM_S(a, s) \ + (((__u64)PERF_MEM_##a##_##s) << PERF_MEM_##a##_SHIFT) + +/* + * single taken branch record layout: + * + * from: source instruction (may not always be a branch insn) + * to: branch target + * mispred: branch target was mispredicted + * predicted: branch target was predicted + * + * support for mispred, predicted is optional. In case it + * is not supported mispred = predicted = 0. + * + * in_tx: running in a hardware transaction + * abort: aborting a hardware transaction + * cycles: cycles from last branch (or 0 if not supported) + * type: branch type + */ +struct perf_branch_entry { + __u64 from; + __u64 to; + __u64 mispred:1, /* target mispredicted */ + predicted:1,/* target predicted */ + in_tx:1, /* in transaction */ + abort:1, /* transaction abort */ + cycles:16, /* cycle count to last branch */ + type:4, /* branch type */ + reserved:40; +}; + +#endif /* _UAPI_LINUX_PERF_EVENT_H */ diff --git a/userspace/libscap/event_table.c b/userspace/libscap/event_table.c deleted file mode 100644 index f70b7fd9c7..0000000000 --- a/userspace/libscap/event_table.c +++ /dev/null @@ -1,179 +0,0 @@ -/* -Copyright (C) 2013-2014 Draios inc. - -This file is part of sysdig. - -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. - -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . -*/ - -#include "../common/sysdig_types.h" -#include "../../driver/ppm_events_public.h" - -const struct ppm_event_info g_event_info[PPM_EVENT_MAX] = { - /* PPME_GENERIC_E */{"syscall", EC_OTHER, EF_NONE, 2, {{"ID", PT_SYSCALLID, PF_DEC}, {"nativeID", PT_UINT16, PF_DEC} } }, - /* PPME_GENERIC_X */{"syscall", EC_OTHER, EF_NONE, 1, {{"ID", PT_SYSCALLID, PF_DEC} } }, - /* PPME_SYSCALL_OPEN_E */{"open", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SYSCALL_OPEN_X */{"open", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 4, {{"fd", PT_FD, PF_DEC}, {"name", PT_FSPATH, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_HEX} } }, - /* PPME_SYSCALL_CLOSE_E */{"close", EC_IO_OTHER, (enum ppm_event_flags)(EF_DESTROYS_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_CLOSE_X */{"close", EC_IO_OTHER, (enum ppm_event_flags)(EF_DESTROYS_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_READ_E */{"read", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_READ_X */{"read", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_WRITE_E */{"write", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_WRITE_X */{"write", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_BRK_E */{"brk", EC_MEMORY, EF_NONE, 1, {{"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_BRK_X */{"brk", EC_MEMORY, EF_NONE, 1, {{"res", PT_UINT64, PF_HEX} } }, - /* PPME_SYSCALL_EXECVE_E */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 0}, - /* PPME_SYSCALL_EXECVE_X */{"execve", EC_PROCESS, EF_MODIFIES_STATE, 8, {{"res", PT_ERRNO, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_UINT64, PF_DEC} } }, - /* PPME_CLONE_E */{"clone", EC_PROCESS, EF_MODIFIES_STATE, 0}, - /* PPME_CLONE_X */{"clone", EC_PROCESS, EF_MODIFIES_STATE, 11, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC} } }, - /* PPME_PROCEXIT_E */{"procexit", EC_PROCESS, EF_MODIFIES_STATE, 0}, - /* PPME_NA1 */{"NA1", EC_PROCESS, EF_UNUSED, 0}, - /* PPME_SOCKET_SOCKET_E */{"socket", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"domain", PT_FLAGS32, PF_DEC, socket_families}, {"type", PT_UINT32, PF_DEC}, {"proto", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_SOCKET_X */{"socket", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_BIND_E */{"bind", EC_NET, EF_USES_FD, 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_BIND_X */{"bind", EC_NET, EF_USES_FD, 2, {{"res", PT_ERRNO, PF_DEC}, {"addr", PT_SOCKADDR, PF_NA} } }, - /* PPME_SOCKET_CONNECT_E */{"connect", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_CONNECT_X */{"connect", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 2, {{"res", PT_ERRNO, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_LISTEN_E */{"listen", EC_NET, EF_USES_FD, 2, {{"fd", PT_FD, PF_DEC}, {"backlog", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_LISTEN_X */{"listen", EC_NET, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SOCKET_ACCEPT_E */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SOCKET_ACCEPT_X */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC} } }, - /* PPME_SYSCALL_SEND_E */{"send", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_SEND_X */{"send", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_SENDTO_E */{"sendto", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_SENDTO_X */{"sendto", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_RECV_E */{"recv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_RECV_X */{"recv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_RECVFROM_E */{"recvfrom", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_RECVFROM_X */{"recvfrom", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_SHUTDOWN_E */{"shutdown", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 2, {{"fd", PT_FD, PF_DEC}, {"how", PT_FLAGS8, PF_HEX, shutdown_how} } }, - /* PPME_SOCKET_SHUTDOWN_X */{"shutdown", EC_NET, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SOCKET_GETSOCKNAME_E */{"getsockname", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETSOCKNAME_X */{"getsockname", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETPEERNAME_E */{"getpeername", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETPEERNAME_X */{"getpeername", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_SOCKETPAIR_E */{"socketpair", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"domain", PT_FLAGS32, PF_DEC, socket_families}, {"type", PT_UINT32, PF_DEC}, {"proto", PT_UINT32, PF_DEC} } }, - /* PPME_SOCKET_SOECKETPAIR_X */{"socketpair", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 5, {{"res", PT_ERRNO, PF_DEC}, {"fd1", PT_FD, PF_DEC}, {"fd2", PT_FD, PF_DEC}, {"source", PT_UINT64, PF_HEX}, {"peer", PT_UINT64, PF_HEX} } }, - /* PPME_SOCKET_SETSOCKOPT_E */{"setsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_SETSOCKOPT_X */{"setsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETSOCKOPT_E */{"getsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_GETSOCKOPT_X */{"getsockopt", EC_NET, EF_NONE, 0}, - /* PPME_SOCKET_SENDMSG_E */{"sendmsg", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_SENDMSG_X */{"sendmsg", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SOCKET_SENDMMSG_E */{"sendmmsg", EC_IO_WRITE, EF_NONE, 0}, - /* PPME_SOCKET_SENDMMSG_X */{"sendmmsg", EC_IO_WRITE, EF_NONE, 0}, - /* PPME_SOCKET_RECVMSG_E */{"recvmsg", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SOCKET_RECVMSG_X */{"recvmsg", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 4, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA}, {"tuple", PT_SOCKTUPLE, PF_NA} } }, - /* PPME_SOCKET_RECVMMSG_E */{"recvmmsg", EC_IO_READ, EF_NONE, 0}, - /* PPME_SOCKET_RECVMMSG_X */{"recvmmsg", EC_IO_READ, EF_NONE, 0}, - /* PPME_SOCKET_ACCEPT4_E */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"flags", PT_INT32, PF_HEX} } }, - /* PPME_SOCKET_ACCEPT4_X */{"accept", EC_NET, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"tuple", PT_SOCKTUPLE, PF_NA}, {"queuepct", PT_UINT8, PF_DEC} } }, - /* PPME_SYSCALL_CREAT_E */{"creat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SYSCALL_CREAT_X */{"creat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"name", PT_FSPATH, PF_NA}, {"mode", PT_UINT32, PF_HEX} } }, - /* PPME_SOCKET_PIPE_E */{"pipe", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 0}, - /* PPME_SOCKET_PIPE_X */{"pipe", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 4, {{"res", PT_ERRNO, PF_DEC}, {"fd1", PT_FD, PF_DEC}, {"fd2", PT_FD, PF_DEC}, {"ino", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_EVENTFD_E */{"eventfd", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 2, {{"initval", PT_UINT64, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX} } }, - /* PPME_SYSCALL_EVENTFD_X */{"eventfd", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_FUTEX_E */{"futex", EC_IPC, EF_NONE, 3, {{"addr", PT_UINT64, PF_HEX}, {"op", PT_FLAGS16, PF_HEX, futex_operations}, {"val", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_FUTEX_X */{"futex", EC_IPC, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_STAT_E */{"stat", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_STAT_X */{"stat", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_LSTAT_E */{"lstat", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_LSTAT_X */{"lstat", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_FSTAT_E */{"fstat", EC_FILE, EF_USES_FD, 1, {{"fd", PT_FD, PF_NA} } }, - /* PPME_SYSCALL_FSTAT_X */{"fstat", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_STAT64_E */{"stat64", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_STAT64_X */{"stat64", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_LSTAT64_E */{"lstat64", EC_FILE, EF_NONE, 0}, - /* PPME_SYSCALL_LSTAT64_X */{"lstat64", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_FSTAT64_E */{"fstat64", EC_FILE, EF_USES_FD, 1, {{"fd", PT_FD, PF_NA} } }, - /* PPME_SYSCALL_FSTAT64_X */{"fstat64", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_EPOLLWAIT_E */{"epoll_wait", EC_WAIT, EF_WAITS, 1, {{"maxevents", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_EPOLLWAIT_X */{"epoll_wait", EC_WAIT, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_POLL_E */{"poll", EC_WAIT, EF_WAITS, 2, {{"fds", PT_FDLIST, PF_DEC}, {"timeout", PT_INT64, PF_DEC} } }, - /* PPME_SYSCALL_POLL_X */{"poll", EC_WAIT, EF_WAITS, 2, {{"res", PT_ERRNO, PF_DEC}, {"fds", PT_FDLIST, PF_DEC} } }, - /* PPME_SYSCALL_SELECT_E */{"select", EC_WAIT, EF_WAITS, 0}, - /* PPME_SYSCALL_SELECT_X */{"select", EC_WAIT, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_NEWSELECT_E */{"select", EC_WAIT, EF_WAITS, 0}, - /* PPME_SYSCALL_NEWSELECT_X */{"select", EC_WAIT, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_LSEEK_E */{"lseek", EC_FILE, EF_USES_FD, 3, {{"fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC}, {"whence", PT_FLAGS8, PF_DEC, lseek_whence} } }, - /* PPME_SYSCALL_LSEEK_X */{"lseek", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_LLSEEK_E */{"llseek", EC_FILE, EF_USES_FD, 3, {{"fd", PT_FD, PF_DEC}, {"offset", PT_UINT64, PF_DEC}, {"whence", PT_FLAGS8, PF_DEC, lseek_whence} } }, - /* PPME_SYSCALL_LLSEEK_X */{"llseek", EC_FILE, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_IOCTL_E */{"ioctl", EC_IO_OTHER, EF_USES_FD, 2, {{"fd", PT_FD, PF_DEC}, {"request", PT_UINT64, PF_HEX} } }, - /* PPME_SYSCALL_IOCTL_X */{"ioctl", EC_IO_OTHER, EF_USES_FD, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_GETCWD_E */{"getcwd", EC_FILE, EF_NONE, 0}, - /* Note: path is PT_CHARBUF and not PT_FSPATH because we assume it's abosulte and will never need resolution */ - /* PPME_SYSCALL_GETCWD_X */{"getcwd", EC_FILE, EF_NONE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_CHARBUF, PF_NA} } }, - /* Note: path is PT_CHARBUF and not PT_FSPATH because we don't want it to be resolved, since the event handler already changes it */ - /* PPME_SYSCALL_CHDIR_E */{"chdir", EC_FILE, EF_MODIFIES_STATE, 0}, - /* PPME_SYSCALL_CHDIR_X */{"chdir", EC_FILE, EF_MODIFIES_STATE, 2, {{"res", PT_ERRNO, PF_DEC}, {"path", PT_CHARBUF, PF_NA} } }, - /* PPME_SYSCALL_FCHDIR_E */{"fchdir", EC_FILE, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_NA} } }, - /* PPME_SYSCALL_FCHDIR_X */{"fchdir", EC_FILE, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_MKDIR_E */{"mkdir", EC_FILE, EF_NONE, 2, {{"path", PT_FSPATH, PF_NA}, {"mode", PT_UINT32, PF_HEX} } }, - /* PPME_SYSCALL_MKDIR_X */{"mkdir", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_RMDIR_E */{"rmdir", EC_FILE, EF_NONE, 1, {{"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_RMDIR_X */{"rmdir", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_OPENAT_E */{"openat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 4, {{"dirfd", PT_FD, PF_DEC}, {"name", PT_CHARBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, openat_flags}, {"mode", PT_UINT32, PF_HEX} } }, - /* PPME_SYSCALL_OPENAT_X */{"openat", EC_FILE, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_LINK_E */{"link", EC_FILE, EF_NONE, 2, {{"oldpath", PT_FSPATH, PF_NA}, {"newpath", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_LINK_X */{"link", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_LINKAT_E */{"linkat", EC_FILE, EF_NONE, 4, {{"olddir", PT_FD, PF_DEC}, {"oldpath", PT_CHARBUF, PF_NA}, {"newdir", PT_FD, PF_DEC}, {"newpath", PT_CHARBUF, PF_NA} } }, - /* PPME_SYSCALL_LINKAT_X */{"linkat", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_UNLINK_E */{"unlink", EC_FILE, EF_NONE, 1, {{"path", PT_FSPATH, PF_NA} } }, - /* PPME_SYSCALL_UNLINK_X */{"unlink", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_UNLINKAT_E */{"unlinkat", EC_FILE, EF_NONE, 2, {{"dirfd", PT_FD, PF_DEC}, {"name", PT_CHARBUF, PF_NA} } }, - /* PPME_SYSCALL_UNLINKAT_X */{"unlinkat", EC_FILE, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_PREAD_E */{"pread", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PREAD_X */{"pread", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_PWRITE_E */{"pwrite", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PWRITE_X */{"pwrite", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_READV_E */{"readv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_READV_X */{"readv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_WRITEV_E */{"writev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_WRITEV_X */{"writev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_PREADV_E */{"preadv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 2, {{"fd", PT_FD, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PREADV_X */{"preadv", EC_IO_READ, (enum ppm_event_flags)(EF_USES_FD | EF_READS_FROM_FD), 3, {{"res", PT_ERRNO, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_PWRITEV_E */{"pwritev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 3, {{"fd", PT_FD, PF_DEC}, {"size", PT_UINT32, PF_DEC}, {"pos", PT_UINT64, PF_DEC} } }, - /* PPME_SYSCALL_PWRITEV_X */{"pwritev", EC_IO_WRITE, (enum ppm_event_flags)(EF_USES_FD | EF_WRITES_TO_FD), 2, {{"res", PT_ERRNO, PF_DEC}, {"data", PT_BYTEBUF, PF_NA} } }, - /* PPME_SYSCALL_DUP_E */{"dup", EC_IO_OTHER, (enum ppm_event_flags)(EF_CREATES_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"fd", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_DUP_X */{"dup", EC_IO_OTHER, (enum ppm_event_flags)(EF_CREATES_FD | EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_SIGNALFD_E */{"signalfd", EC_SIGNAL, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 3, {{"fd", PT_FD, PF_DEC}, {"mask", PT_UINT32, PF_HEX}, {"flags", PT_FLAGS8, PF_HEX} } }, - /* PPME_SYSCALL_SIGNALFD_X */{"signalfd", EC_SIGNAL, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_KILL_E */{"kill", EC_SIGNAL, EF_NONE, 2, {{"pid", PT_PID, PF_DEC}, {"sig", PT_SIGTYPE, PF_DEC} } }, - /* PPME_SYSCALL_KILL_X */{"kill", EC_SIGNAL, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_TKILL_E */{"tkill", EC_SIGNAL, EF_NONE, 2, {{"tid", PT_PID, PF_DEC}, {"sig", PT_SIGTYPE, PF_DEC} } }, - /* PPME_SYSCALL_TKILL_X */{"tkill", EC_SIGNAL, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_TGKILL_E */{"tgkill", EC_SIGNAL, EF_NONE, 3, {{"pid", PT_PID, PF_DEC}, {"tid", PT_PID, PF_DEC}, {"sig", PT_SIGTYPE, PF_DEC} } }, - /* PPME_SYSCALL_TGKILL_X */{"tgkill", EC_SIGNAL, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_NANOSLEEP_E */{"nanosleep", EC_SLEEP, EF_WAITS, 1, {{"interval", PT_RELTIME, PF_DEC} } }, - /* PPME_SYSCALL_NANOSLEEP_X */{"nanosleep", EC_SLEEP, EF_WAITS, 1, {{"res", PT_ERRNO, PF_DEC} } }, - /* PPME_SYSCALL_TIMERFD_CREATE_E */{"timerfd_create", EC_TIME, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 2, {{"clockid", PT_UINT8, PF_DEC}, {"flags", PT_FLAGS8, PF_HEX} } }, - /* PPME_SYSCALL_TIMERFD_CREATE_X */{"timerfd_create", EC_TIME, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_INOTIFY_INIT_E */{"inotify_init", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"flags", PT_FLAGS8, PF_HEX} } }, - /* PPME_SYSCALL_INOTIFY_INIT_X */{"inotify_init", EC_IPC, (enum ppm_event_flags)(EF_CREATES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SYSCALL_GETRLIMIT_E */{"getrlimit", EC_PROCESS, EF_NONE, 1, {{"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, - /* PPME_SYSCALL_GETRLIMIT_X */{"getrlimit", EC_PROCESS, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"cur", PT_INT64, PF_DEC}, {"max", PT_INT64, PF_DEC} } }, - /* PPME_SYSCALL_SETRLIMIT_E */{"setrlimit", EC_PROCESS, EF_NONE, 1, {{"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, - /* PPME_SYSCALL_SETRLIMIT_X */{"setrlimit", EC_PROCESS, EF_NONE, 3, {{"res", PT_ERRNO, PF_DEC}, {"cur", PT_INT64, PF_DEC}, {"max", PT_INT64, PF_DEC} } }, - /* PPME_SYSCALL_PRLIMIT_E */{"prlimit", EC_PROCESS, EF_NONE, 2, {{"pid", PT_PID, PF_DEC}, {"resource", PT_FLAGS8, PF_DEC, rlimit_resources} } }, - /* PPME_SYSCALL_PRLIMIT_X */{"prlimit", EC_PROCESS, EF_NONE, 5, {{"res", PT_ERRNO, PF_DEC}, {"newcur", PT_INT64, PF_DEC}, {"newmax", PT_INT64, PF_DEC}, {"oldcur", PT_INT64, PF_DEC}, {"oldmax", PT_INT64, PF_DEC} } }, - /* PPME_SCHEDSWITCH_E */{"switch", EC_SCHEDULER, EF_NONE, 1, {{"next", PT_PID, PF_DEC} } }, - /* PPME_SCHEDSWITCH_X */{"NA2", EC_SCHEDULER, EF_UNUSED, 0}, - /* PPME_DROP_E */{"drop", EC_INTERNAL, EF_NONE, 1, {{"ratio", PT_UINT32, PF_DEC} } }, - /* PPME_DROP_X */{"drop", EC_INTERNAL, EF_NONE, 1, {{"ratio", PT_UINT32, PF_DEC} } }, - /* PPME_SYSCALL_FCNTL_E */{"fcntl", EC_IO_OTHER, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 2, {{"fd", PT_FD, PF_DEC}, {"cmd", PT_FLAGS8, PF_DEC, fcntl_commands} } }, - /* PPME_SYSCALL_FCNTL_X */{"fcntl", EC_IO_OTHER, (enum ppm_event_flags)(EF_USES_FD | EF_MODIFIES_STATE), 1, {{"res", PT_FD, PF_DEC} } }, - /* PPME_SCHEDSWITCHEX_E */{"switch", EC_SCHEDULER, EF_NONE, 5, {{"next", PT_PID, PF_DEC}, {"pgft_maj", PT_UINT32, PF_DEC}, {"pgft_min", PT_UINT32, PF_DEC}, {"next_pgft_maj", PT_UINT32, PF_DEC}, {"next_pgft_min", PT_UINT32, PF_DEC} } }, - /* PPME_SCHEDSWITCHEX_X */{"NA2", EC_SCHEDULER, EF_UNUSED, 0}, -}; diff --git a/userspace/libscap/examples/01-open/CMakeLists.txt b/userspace/libscap/examples/01-open/CMakeLists.txt index a414e410b1..6f7c5f07b7 100644 --- a/userspace/libscap/examples/01-open/CMakeLists.txt +++ b/userspace/libscap/examples/01-open/CMakeLists.txt @@ -1,5 +1,5 @@ -include_directories(${PROJECT_SOURCE_DIR}/common) -include_directories(${PROJECT_SOURCE_DIR}/userspace/libscap) +include_directories("../../../common") +include_directories("../../") add_executable(scap-open test.c) diff --git a/userspace/libscap/examples/01-open/test.c b/userspace/libscap/examples/01-open/test.c index 24f5c54e0d..ba2be7a004 100644 --- a/userspace/libscap/examples/01-open/test.c +++ b/userspace/libscap/examples/01-open/test.c @@ -1,25 +1,45 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include - +#include #include +uint64_t g_nevts = 0; +scap_t* g_h = NULL; + +static void signal_callback(int signal) +{ + scap_stats s; + printf("events captured: %" PRIu64 "\n", g_nevts); + scap_get_stats(g_h, &s); + printf("seen by driver: %" PRIu64 "\n", s.n_evts); + printf("Number of dropped events: %" PRIu64 "\n", s.n_drops); + printf("Number of dropped events caused by full buffer: %" PRIu64 "\n", s.n_drops_buffer); + printf("Number of dropped events caused by invalid memory access: %" PRIu64 "\n", s.n_drops_pf); + printf("Number of dropped events caused by an invalid condition in the kernel instrumentation: %" PRIu64 "\n", s.n_drops_bug); + printf("Number of preemptions: %" PRIu64 "\n", s.n_preemptions); + printf("Number of events skipped due to the tid being in a set of suppressed tids: %" PRIu64 "\n", s.n_suppressed); + printf("Number of threads currently being suppressed: %" PRIu64 "\n", s.n_tids_suppressed); + exit(0); +} + int main(int argc, char** argv) { char error[SCAP_LASTERR_SIZE]; @@ -27,25 +47,36 @@ int main(int argc, char** argv) scap_evt* ev; uint16_t cpuid; - scap_t* h = scap_open_live(error); - if(h == NULL) + if(signal(SIGINT, signal_callback) == SIG_ERR) { - fprintf(stderr, "%s\n", error); + fprintf(stderr, "An error occurred while setting SIGINT signal handler.\n"); + return -1; + } + + g_h = scap_open_live(error, &res); + if(g_h == NULL) + { + fprintf(stderr, "%s (%d)\n", error, res); return -1; } while(1) { - res = scap_next(h, &ev, &cpuid); + res = scap_next(g_h, &ev, &cpuid); if(res > 0) { - fprintf(stderr, "%s\n", scap_getlasterr(h)); - scap_close(h); + fprintf(stderr, "%s\n", scap_getlasterr(g_h)); + scap_close(g_h); return -1; } + + if(res != SCAP_TIMEOUT) + { + g_nevts++; + } } - scap_close(h); + scap_close(g_h); return 0; } diff --git a/userspace/libscap/examples/02-validatebuffer/CMakeLists.txt b/userspace/libscap/examples/02-validatebuffer/CMakeLists.txt index 59f5fe1b09..294e218cbe 100644 --- a/userspace/libscap/examples/02-validatebuffer/CMakeLists.txt +++ b/userspace/libscap/examples/02-validatebuffer/CMakeLists.txt @@ -1,5 +1,5 @@ -include_directories(${PROJECT_SOURCE_DIR}/common) -include_directories(${PROJECT_SOURCE_DIR}/userspace/libscap) +include_directories("../../../common") +include_directories("../..") add_executable(scap-validatebuffer test.c) diff --git a/userspace/libscap/examples/02-validatebuffer/test.c b/userspace/libscap/examples/02-validatebuffer/test.c index cd1a3c28ae..0672c49264 100644 --- a/userspace/libscap/examples/02-validatebuffer/test.c +++ b/userspace/libscap/examples/02-validatebuffer/test.c @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include @@ -38,7 +39,11 @@ size_t g_get_event_size(enum ppm_event_type event_type, uint16_t* lens) res += lens[j]; } +#ifdef PPM_ENABLE_SENTINEL return res + j * sizeof(uint16_t) + sizeof(struct ppm_evt_hdr) + sizeof(uint32_t); +#else + return res + j * sizeof(uint16_t) + sizeof(struct ppm_evt_hdr); +#endif } int32_t g_check_integrity(uint32_t* cur_event, char* copy_buffer, int buf_len, OUT uint32_t* nevents) @@ -65,7 +70,8 @@ int32_t g_check_integrity(uint32_t* cur_event, char* copy_buffer, int buf_len, O hdr = (struct ppm_evt_hdr*)(copy_buffer + offset); - if(buf_len < sizeof(struct ppm_evt_hdr) + g_event_info[hdr->type].nparams * sizeof(uint16_t)) + uint16_t type = hdr->type; + if(buf_len < sizeof(struct ppm_evt_hdr) + g_event_info[type].nparams * sizeof(uint16_t)) { fprintf(stderr, "Error: event not on buffer boundary, offset %x, data to read %d\n", offset, @@ -87,7 +93,7 @@ int32_t g_check_integrity(uint32_t* cur_event, char* copy_buffer, int buf_len, O if(event_size < sizeof(struct ppm_evt_hdr) + g_event_info[hdr->type].nparams * sizeof(uint16_t)) { fprintf(stderr, "Error: event size too short %u, cnt %u, offset %x\n", - event_size, + (unsigned int)event_size, (*cur_event == -1)?0:*cur_event, offset); return SCAP_FAILURE; @@ -96,10 +102,10 @@ int32_t g_check_integrity(uint32_t* cur_event, char* copy_buffer, int buf_len, O #ifdef PPM_ENABLE_SENTINEL sentinel_begin = ((struct ppm_evt_hdr*)(copy_buffer + offset))->sentinel_begin; sentinel_end = *(uint32_t*)(copy_buffer + offset + event_size - sizeof(uint32_t)); - + if(sentinel_begin != sentinel_end) { - fprintf(stderr, "Error: sentinel begin %d, sentinel end %d, evt_type %u, evt_size %u, cnt %u, offset %x, remaining %u\n", + fprintf(stderr, "Error: sentinel begin %d, sentinel end %d, evt_type %u, evt_size %zu, cnt %u, offset %x, remaining %u\n", sentinel_begin, sentinel_end, (uint32_t)hdr->type, @@ -163,11 +169,11 @@ int main() &new_mask); */ - scap_t* h = scap_open_live(error); + scap_t* h = scap_open_live(error, &ret); if(h == NULL) { - fprintf(stderr, "%s\n", error); - return -1; + fprintf(stderr, "%s (%d)\n", error, ret); + return ret; } ndevs = scap_get_ndevs(h); @@ -192,7 +198,7 @@ int main() { uint32_t nevents; - ret = scap_readbuf(h, j, false, &buf, &buflen); + ret = scap_readbuf(h, j, &buf, &buflen); if(ret != SCAP_SUCCESS) { @@ -242,11 +248,21 @@ int main() if(nloops == 1000) { - printf("bps:%" PRIu64 " totbytes:%" PRIu64 " - evts/s:%" PRIu64 " totevs:%" PRIu64 "\n", + scap_stats stats; + + if(scap_get_stats(h, &stats) != SCAP_SUCCESS) + { + fprintf(stderr, "%s\n", scap_getlasterr(h)); + scap_close(h); + return -1; + } + + printf("bps:%" PRIu64 " totbytes:%" PRIu64 " - evts/s:%" PRIu64 " totevs:%" PRIu64 " drops:%" PRIu64 "\n", totbytes - oldtotbytes, totbytes, totevents - oldtotevents, - totevents); + totevents, + stats.n_drops); oldtotbytes = totbytes; oldtotevents = totevents; diff --git a/userspace/libscap/flags_table.c b/userspace/libscap/flags_table.c deleted file mode 100644 index c2ce94455d..0000000000 --- a/userspace/libscap/flags_table.c +++ /dev/null @@ -1,208 +0,0 @@ -/* -Copyright (C) 2013-2014 Draios inc. - -This file is part of sysdig. - -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. - -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . -*/ - -#include "../common/sysdig_types.h" -#include "../../driver/ppm_events_public.h" - -const struct ppm_name_value socket_families[] = { - {"AF_NFC", 39}, - {"AF_ALG", 38}, - {"AF_CAIF", 37}, - {"AF_IEEE802154", 36}, - {"AF_PHONET", 35}, - {"AF_ISDN", 34}, - {"AF_RXRPC", 33}, - {"AF_IUCV", 32}, - {"AF_BLUETOOTH", 31}, - {"AF_TIPC", 30}, - {"AF_CAN", 29}, - {"AF_LLC", 26}, - {"AF_WANPIPE", 25}, - {"AF_PPPOX", 24}, - {"AF_IRDA", 23}, - {"AF_SNA", 22}, - {"AF_RDS", 21}, - {"AF_ATMSVC", 20}, - {"AF_ECONET", 19}, - {"AF_ASH", 18}, - {"AF_PACKET", 17}, - {"AF_ROUTE", PPM_AF_NETLINK}, - {"AF_NETLINK", 16}, - {"AF_KEY", 15}, - {"AF_SECURITY", 14}, - {"AF_NETBEUI", 13}, - {"AF_DECnet", 12}, - {"AF_ROSE", 11}, - {"AF_INET6", 10}, - {"AF_X25", 9}, - {"AF_ATMPVC", 8}, - {"AF_BRIDGE", 7}, - {"AF_NETROM", 6}, - {"AF_APPLETALK", 5}, - {"AF_IPX", 4}, - {"AF_AX25", 3}, - {"AF_INET", 2}, - {"AF_LOCAL", 1}, - {"AF_UNIX", 1}, - {"AF_UNSPEC", 0}, - {0, 0}, -}; - -const struct ppm_name_value file_flags[] = { - {"O_LARGEFILE", (1 << 11)}, - {"O_DIRECTORY", (1 << 10)}, - {"O_DIRECT", (1 << 9)}, - {"O_TRUNC", (1 << 8)}, - {"O_SYNC", (1 << 7)}, - {"O_NONBLOCK", (1 << 6)}, - {"O_EXCL", (1 << 5)}, - {"O_DSYNC", (1 << 4)}, - {"O_APPEND", (1 << 3)}, - {"O_CREAT", (1 << 2)}, - {"O_RDWR", (PPM_O_RDONLY | PPM_O_WRONLY)}, - {"O_WRONLY", (1 << 1)}, - {"O_RDONLY", (1 << 0)}, - {"O_NONE", 0}, - {0, 0}, -}; - -const struct ppm_name_value clone_flags[] = { - {"CLONE_FILES", (1 << 0)}, - {"CLONE_FS", (1 << 1)}, - {"CLONE_IO", (1 << 2)}, - {"CLONE_NEWIPC", (1 << 3)}, - {"CLONE_NEWNET", (1 << 4)}, - {"CLONE_NEWNS", (1 << 5)}, - {"CLONE_NEWPID", (1 << 6)}, - {"CLONE_NEWUTS", (1 << 7)}, - {"CLONE_PARENT", (1 << 8)}, - {"CLONE_PARENT_SETTID", (1 << 9)}, - {"CLONE_PTRACE", (1 << 10)}, - {"CLONE_SIGHAND", (1 << 11)}, - {"CLONE_SYSVSEM", (1 << 12)}, - {"CLONE_THREAD", (1 << 13)}, - {"CLONE_UNTRACED", (1 << 14)}, - {"CLONE_VM", (1 << 15)}, - {"CLONE_INVERTED", (1 << 16)}, - {"NAME_CHANGED", (1 << 17)}, - {"CLOSED", (1 << 18)}, - {0, 0}, -}; - -const struct ppm_name_value futex_operations[] = { - {"FUTEX_CLOCK_REALTIME", 256}, - {"FUTEX_PRIVATE_FLAG", 128}, - {"FUTEX_CMP_REQUEUE_PI", 12}, - {"FUTEX_WAIT_REQUEUE_PI", 11}, - {"FUTEX_WAKE_BITSET", 10}, - {"FUTEX_WAIT_BITSET", 9}, - {"FUTEX_TRYLOCK_PI", 8}, - {"FUTEX_UNLOCK_PI", 7}, - {"FUTEX_LOCK_PI", 6}, - {"FUTEX_WAKE_OP", 5}, - {"FUTEX_CMP_REQUEUE", 4}, - {"FUTEX_REQUEUE", 3}, - {"FUTEX_FD", 2}, - {"FUTEX_WAKE", 1}, - {"FUTEX_WAIT", 0}, - {0, 0}, -}; - -const struct ppm_name_value poll_flags[] = { - {"POLLIN", (1 << 0)}, - {"POLLPRI", (1 << 1)}, - {"POLLOUT", (1 << 2)}, - {"POLLRDHUP", (1 << 3)}, - {"POLLERR", (1 << 4)}, - {"POLLHUP", (1 << 5)}, - {"POLLNVAL", (1 << 6)}, - {"POLLRDNORM", (1 << 7)}, - {"POLLRDBAND", (1 << 8)}, - {"POLLWRNORM", (1 << 9)}, - {"POLLWRBAND", (1 << 10)}, - {0, 0}, -}; - -const struct ppm_name_value lseek_whence[] = { - {"SEEK_END", 2}, - {"SEEK_CUR", 1}, - {"SEEK_SET", 0}, - {0, 0}, -}; - -const struct ppm_name_value shutdown_how[] = { - {"SHUT_RDWR", 2}, - {"SHUT_WR", 1}, - {"SHUT_RD", 0}, - {0, 0}, -}; - -const struct ppm_name_value openat_flags[] = { - {"AT_FDCWD", -100}, - {0, 0}, -}; - -const struct ppm_name_value rlimit_resources[] = { - {"RLIMIT_UNKNOWN", 255}, - {"RLIMIT_RTTIME", 15}, - {"RLIMIT_RTPRIO", 14}, - {"RLIMIT_NICE", 13}, - {"RLIMIT_MSGQUEUE", 12}, - {"RLIMIT_SIGPENDING", 11}, - {"RLIMIT_LOCKS", 10}, - {"RLIMIT_AS", 9}, - {"RLIMIT_MEMLOCK", 8}, - {"RLIMIT_NOFILE", 7}, - {"RLIMIT_NPROC", 6}, - {"RLIMIT_RSS", 5}, - {"RLIMIT_CORE", 4}, - {"RLIMIT_STACK", 3}, - {"RLIMIT_DATA", 2}, - {"RLIMIT_FSIZE", 1}, - {"RLIMIT_CPU", 0}, - {0, 0}, -}; - -const struct ppm_name_value fcntl_commands[] = { - {"F_GETPIPE_SZ", 29}, - {"F_SETPIPE_SZ", 28}, - {"F_NOTIFY", 27}, - {"F_DUPFD_CLOEXEC", 26}, - {"F_CANCELLK", 25}, - {"F_GETLEASE", 24}, - {"F_SETLEASE", 23}, - {"F_GETOWN_EX", 22}, - {"F_SETOWN_EX", 21}, - {"F_SETLKW64", 19}, - {"F_SETLK64", 18}, - {"F_GETLK64", 17}, - {"F_GETSIG", 15}, - {"F_SETSIG", 13}, - {"F_GETOWN", 12}, - {"F_SETOWN", 10}, - {"F_SETLKW", 9}, - {"F_SETLK", 8}, - {"F_GETLK", 6}, - {"F_SETFL", 5}, - {"F_GETFL", 4}, - {"F_SETFD", 3}, - {"F_GETFD", 2}, - {"F_DUPFD", 1}, - {"UNKNOWN", 0}, - {0, 0}, -}; diff --git a/userspace/libscap/scap-int.h b/userspace/libscap/scap-int.h index b699412763..51eaf83cd0 100644 --- a/userspace/libscap/scap-int.h +++ b/userspace/libscap/scap-int.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ //////////////////////////////////////////////////////////////////////////// @@ -26,6 +27,9 @@ along with sysdig. If not, see . extern "C" { #endif +#ifdef CYGWING_AGENT +typedef struct wh_t wh_t; +#endif #ifdef _WIN32 #define _CRTDBG_MAP_ALLOC @@ -33,49 +37,161 @@ extern "C" { #include #endif #include +#if defined(USE_ZLIB) && !defined(UDIG) +#include +#else +#define gzFile FILE* +#define gzflush(X, Y) fflush(X) +#define gzopen fopen +#define gzdopen(fd, mode) stdout +#define gzclose fclose +#define gzoffset ftell +#define gzwrite(F, B, S) fwrite(B, 1, S, F) +#define gzread(F, B, S) fread(B, 1, S, F) +#define gztell(F) ftell(F) +#define gzerror(F, E) ({*E = ferror(F); "error reading file descriptor";}) +#define gzseek fseek +#endif // -// The time scap_next will wait when a buffer is empty +// Read buffer timeout constants // -#define BUFFER_EMPTY_WAIT_TIME_MS 30 +#define BUFFER_EMPTY_WAIT_TIME_US_START 500 +#define BUFFER_EMPTY_WAIT_TIME_US_MAX (30 * 1000) +#define BUFFER_EMPTY_THRESHOLD_B 20000 // // Process flags // #define PF_CLONING 1 +// +// ebpf defs +// +#define BPF_PROGS_MAX 128 +#define BPF_MAPS_MAX 32 + // // The device descriptor // typedef struct scap_device { int m_fd; + int m_bufinfo_fd; // used by udig char* m_buffer; - struct ppm_ring_buffer_info* m_bufinfo; + uint32_t m_buffer_size; // used by udig uint32_t m_lastreadsize; char* m_sn_next_event; // Pointer to the next event available for scap_next uint32_t m_sn_len; // Number of bytes available in the buffer pointed by m_sn_next_event -// uint64_t m_sn_next_ts; // timestamp + union + { + // Anonymous struct with ppm stuff + struct + { + struct ppm_ring_buffer_info* m_bufinfo; + struct udig_ring_buffer_status* m_bufstatus; // used by udig + }; + // Anonymous struct with bpf stuff + struct + { + uint64_t m_evt_lost; + }; + }; }scap_device; + +typedef struct scap_tid +{ + uint64_t tid; + + UT_hash_handle hh; ///< makes this structure hashable +} scap_tid; + // // The open instance handle // struct scap { + scap_mode_t m_mode; scap_device* m_devs; - struct pollfd* m_pollfds; uint32_t m_ndevs; +#ifdef USE_ZLIB + gzFile m_file; +#else FILE* m_file; +#endif char* m_file_evt_buf; + uint32_t m_last_evt_dump_flags; char m_lasterr[SCAP_LASTERR_SIZE]; + + // Used for scap_strerror + char m_strerror_buf[SCAP_LASTERR_SIZE]; + scap_threadinfo* m_proclist; + scap_mountinfo* m_dev_list; scap_threadinfo m_fake_kernel_proc; uint64_t m_evtcnt; - uint32_t m_emptybuf_timeout_ms; scap_addrlist* m_addrlist; scap_machine_info m_machine_info; scap_userlist* m_userlist; + uint64_t m_buffer_empty_wait_time_us; + proc_entry_callback m_proc_callback; + void* m_proc_callback_context; + struct ppm_proclist_info* m_driver_procinfo; + bool refresh_proc_table_when_saving; + uint32_t m_fd_lookup_limit; + uint64_t m_unexpected_block_readsize; + uint32_t m_ncpus; + // Abstraction layer for windows +#ifdef CYGWING_AGENT + wh_t* m_whh; +#endif + bool m_bpf; + bool m_udig; + bool m_udig_capturing; + // Anonymous struct with bpf stuff + struct + { + int m_bpf_prog_fds[BPF_PROGS_MAX]; + int m_bpf_prog_cnt; + bool m_bpf_fillers[BPF_PROGS_MAX]; + int m_bpf_event_fd[BPF_PROGS_MAX]; + int m_bpf_map_fds[BPF_MAPS_MAX]; + int m_bpf_prog_array_map_idx; + }; + + // The set of process names that are suppressed + char **m_suppressed_comms; + uint32_t m_num_suppressed_comms; + + // The active set of threads that are suppressed + scap_tid *m_suppressed_tids; + + // The number of events that were skipped due to the comm + // matching an entry in m_suppressed_comms. + uint64_t m_num_suppressed_evts; +}; + +typedef enum ppm_dumper_type +{ + DT_FILE = 0, + DT_MEM = 1, +}ppm_dumper_type; + +struct scap_dumper +{ + gzFile m_f; + ppm_dumper_type m_type; + uint8_t* m_targetbuf; + uint8_t* m_targetbufcurpos; + uint8_t* m_targetbufend; +}; + +struct scap_ns_socket_list +{ + int64_t net_ns; + scap_fdinfo* sockets; + UT_hash_handle hh; }; // @@ -89,10 +205,12 @@ struct scap // // Read the full event buffer for the given processor -int32_t scap_readbuf(scap_t* handle, uint32_t proc, bool blocking, OUT char** buf, OUT uint32_t* len); +int32_t scap_readbuf(scap_t* handle, uint32_t proc, OUT char** buf, OUT uint32_t* len); +// Read a single thread info from /proc +int32_t scap_proc_read_thread(scap_t* handle, char* procdirname, uint64_t tid, struct scap_threadinfo** pi, char *error, bool scan_sockets); // Scan a directory containing process information -int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, int parenttid, int tid_to_scan, struct scap_threadinfo** pi, char *error, bool scan_sockets); -// Remove an entry from the process list by parsin a PPME_PROC_EXIT event +int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, char *error); +// Remove an entry from the process list by parsing a PPME_PROC_EXIT event // void scap_proc_schedule_removal(scap_t* handle, scap_evt* e); // Remove the process that was scheduled for deletion for this handle // void scap_proc_remove_scheduled(scap_t* handle); @@ -101,63 +219,99 @@ void scap_proc_free_table(scap_t* handle); // Copy the fd table of a process into the one of another process // int32_t scap_proc_copy_fd_table(scap_t* handle, scap_threadinfo* dst, scap_threadinfo* src); // Internal helper function to output the process table to screen -void scap_proc_print_info(scap_threadinfo* pi); +void scap_proc_print_info(scap_t *handle, scap_threadinfo* pi); void scap_proc_print_table(scap_t* handle); // Free all the state related to a process and delete it from the fd table void scap_proc_delete(scap_t* handle, scap_threadinfo* proc); // Internal helper function to output the fd table of a process -void scap_fd_print_table(scap_threadinfo* pi); +void scap_fd_print_table(scap_t *handle, scap_threadinfo* pi); // Internal helper function to output an fd table -void scap_fd_print_fd_table(scap_fdinfo* fds); +void scap_fd_print_fd_table(scap_t *handle, scap_fdinfo* fds); // Given an event, get the info entry for the process that generated it. // NOTE: this is different from scap_event_getprocinfo() because it returns the full event information // struct scap_threadinfo* scap_proc_get_from_event(scap_t* handle, scap_evt* e); -// Return the process info entry geiven a tid +// Return the process info entry given a tid // Free an fd table and set it to NULL when done void scap_fd_free_table(scap_t* handle, scap_fdinfo** fds); +void scap_fd_free_ns_sockets_list(scap_t* handle, struct scap_ns_socket_list** sockets); // Free a process' fd table void scap_fd_free_proc_fd_table(scap_t* handle, scap_threadinfo* pi); // Convert an fd entry's info into a string -int32_t scap_fd_info_to_string(scap_fdinfo* fdi, OUT char* str, uint32_t strlen); +int32_t scap_fd_info_to_string(scap_t *handle, scap_fdinfo* fdi, OUT char* str, uint32_t strlen); // Calculate the length on disk of an fd entry's info uint32_t scap_fd_info_len(scap_fdinfo* fdi); // Write the given fd info to disk -int32_t scap_fd_write_to_disk(scap_t* handle, scap_fdinfo* fdi, FILE *f); +int32_t scap_fd_write_to_disk(scap_t* handle, scap_fdinfo* fdi, scap_dumper_t* dumper, uint32_t len); // Populate the given fd by reading the info from disk -uint32_t scap_fd_read_from_disk(scap_t* handle, OUT scap_fdinfo* fdi, OUT size_t* nbytes, FILE *f); +uint32_t scap_fd_read_from_disk(scap_t* handle, OUT scap_fdinfo* fdi, OUT size_t* nbytes, uint32_t block_type, gzFile f); +// Parse the headers of a trace file and load the tables +int32_t scap_read_init(scap_t* handle, gzFile f); // Add the file descriptor info pointed by fdi to the fd table for process pi. // Note: silently skips if fdi->type is SCAP_FD_UNKNOWN. -int32_t scap_add_fd_to_proc_table(scap_t* handle, scap_threadinfo* pi, scap_fdinfo* fdi); +int32_t scap_add_fd_to_proc_table(scap_t* handle, scap_threadinfo* pi, scap_fdinfo* fdi, char *error); // Remove the given fd from the process table of the process pointed by pi void scap_fd_remove(scap_t* handle, scap_threadinfo* pi, int64_t fd); -// Parse the headers of a trace file and load the tables -int32_t scap_read_init(scap_t* handle, FILE* f); // Read an event from disk int32_t scap_next_offline(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid); -// read the filedescriptors for a given process directory -int32_t scap_fd_scan_fd_dir(scap_t* handle, char * procdir, scap_threadinfo* pi, scap_fdinfo * sockets, char *error); +// read the file descriptors for a given process directory +int32_t scap_fd_scan_fd_dir(scap_t* handle, char * procdir, scap_threadinfo* pi, struct scap_ns_socket_list** sockets_by_ns, char *error); // read tcp or udp sockets from the proc filesystem -int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t* handle, char * dir, int l4proto, scap_fdinfo ** sockets); +int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t* handle, const char * dir, int l4proto, scap_fdinfo ** sockets); // read all sockets and add them to the socket table hashed by their ino -int32_t scap_fd_read_sockets(scap_t* handle, scap_fdinfo** sockets); +int32_t scap_fd_read_sockets(scap_t* handle, char* procdir, struct scap_ns_socket_list* sockets, char *error); +// get the device major/minor number for the requested_mount_id, looking in procdir/mountinfo if needed +uint32_t scap_get_device_by_mount_id(scap_t *handle, const char *procdir, unsigned long requested_mount_id); // prints procs details for a give tid void scap_proc_print_proc_by_tid(scap_t* handle, uint64_t tid); // Allocate and return the list of interfaces on this system int32_t scap_create_iflist(scap_t* handle); // Free a previously allocated list of interfaces void scap_free_iflist(scap_addrlist* ifhandle); -// Allocate and return the list of interfaces on this system +// Allocate and return the list of users on this system int32_t scap_create_userlist(scap_t* handle); // Free a previously allocated list of users void scap_free_userlist(scap_userlist* uhandle); int32_t scap_fd_post_process_unix_sockets(scap_t* handle, scap_fdinfo* sockets); -uint32_t scap_event_compute_len(scap_evt* e); +int32_t scap_proc_fill_cgroups(scap_t *handle, struct scap_threadinfo* tinfo, const char* procdirname); + +bool scap_alloc_proclist_info(scap_t* handle, uint32_t n_entries); + +// Determine whether or not the provided event should be suppressed, +// based on its event type and parameters. May update the set of +// suppressed tids as a side-effect. +// +// Returns SCAP_FAILURE if we tried to add the tid to the suppressed +// tid set, but it could *not* be added, SCAP_SUCCESS otherwise. +int32_t scap_check_suppressed(scap_t *handle, scap_evt *pevent, + bool *suppressed); + +// Possibly add or remove the provided comm, tid combination to the +// set of suppressed processes. If the ptid is currently in the +// suppressed set, the tid will always be added to the suppressed +// set. Otherwise, the tid will be added if the comm matches an entry +// in suppressed_comms. +// +// Sets *suppressed to whether, after this check, the tid is suppressed. +// +// Returns SCAP_FAILURE if we tried to add the tid to the suppressed +// tid set, but it could *not* be added, SCAP_SUCCESS otherwise. +int32_t scap_update_suppressed(scap_t *handle, + const char *comm, + uint64_t tid, uint64_t ptid, + bool *suppressed); + +// Wrapper around strerror using buffer in handle +const char *scap_strerror(scap_t *handle, int errnum); // // ASSERT implementation // + +#ifdef ASSERT +#undef ASSERT +#endif // ASSERT #ifdef _DEBUG #define ASSERT(X) assert(X) #else // _DEBUG @@ -171,7 +325,17 @@ uint32_t scap_event_compute_len(scap_evt* e); (int)read_size,\ __FILE__,\ __LINE__);\ - ASSERT(false);\ + return SCAP_FAILURE;\ + } + +#define CHECK_READ_SIZE_WITH_FREE(alloc_buffer, read_size, expected_size) if(read_size != expected_size) \ + {\ + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "expecting %d bytes, read %d at %s, line %d. Is the file truncated?",\ + (int)expected_size,\ + (int)read_size,\ + __FILE__,\ + __LINE__);\ + free(alloc_buffer);\ return SCAP_FAILURE;\ } @@ -183,6 +347,30 @@ uint32_t scap_event_compute_len(scap_evt* e); #define MAX(X,Y) ((X) > (Y)? (X):(Y)) #endif +// +// Driver proc info table sizes +// +#define SCAP_DRIVER_PROCINFO_INITIAL_SIZE 7 +#define SCAP_DRIVER_PROCINFO_MAX_SIZE 128000 + +extern const enum ppm_syscall_code g_syscall_code_routing_table[]; +extern const struct syscall_evt_pair g_syscall_table[]; +extern const struct ppm_event_info g_event_info[]; +extern const struct ppm_syscall_desc g_syscall_info_table[]; +extern const struct ppm_event_entry g_ppm_events[]; +extern bool validate_info_table_size(); + +// +// udig stuff +// +int32_t udig_begin_capture(scap_t* handle, char *error); +void udig_start_capture(scap_t* handle); +void udig_stop_capture(scap_t* handle); +void udig_end_capture(scap_t* handle); +uint32_t udig_set_snaplen(scap_t* handle, uint32_t snaplen); +int32_t udig_stop_dropping_mode(scap_t* handle); +int32_t udig_start_dropping_mode(scap_t* handle, uint32_t sampling_ratio); + #ifdef __cplusplus } #endif diff --git a/userspace/libscap/scap.c b/userspace/libscap/scap.c index fe026a5b93..6f51588eda 100644 --- a/userspace/libscap/scap.c +++ b/userspace/libscap/scap.c @@ -1,116 +1,238 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include #include #ifndef _WIN32 #include +#include #include #include #include #include #include +#include +#include +#include +#include +#include #endif // _WIN32 +#ifdef CYGWING_AGENT +#define DRAGENT_WIN_HAL_C_ONLY +#include +#endif + #include "scap.h" +#ifdef HAS_CAPTURE +#ifndef CYGWING_AGENT +#include "driver_config.h" +#endif // CYGWING_AGENT +#endif // HAS_CAPTURE #include "../../driver/ppm_ringbuffer.h" #include "scap_savefile.h" #include "scap-int.h" +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) +#include "scap_bpf.h" +#endif //#define NDEBUG #include -char* scap_getlasterr(scap_t* handle) +static const char *SYSDIG_BPF_PROBE_ENV = "SYSDIG_BPF_PROBE"; + +// +// Probe version string size +// +#define SCAP_PROBE_VERSION_SIZE 32 + +const char* scap_getlasterr(scap_t* handle) +{ + return handle ? handle->m_lasterr : "null scap handle"; +} + +static int32_t copy_comms(scap_t *handle, const char **suppressed_comms) { - return handle->m_lasterr; + if(suppressed_comms) + { + uint32_t i; + const char *comm; + for(i = 0, comm = suppressed_comms[i]; comm && i < SCAP_MAX_SUPPRESSED_COMMS; i++, comm = suppressed_comms[i]) + { + int32_t res; + if((res = scap_suppress_events_comm(handle, comm)) != SCAP_SUCCESS) + { + return res; + } + } + } + + return SCAP_SUCCESS; } -scap_t* scap_open_live(char *error) +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) +scap_t* scap_open_live_int(char *error, int32_t *rc, + proc_entry_callback proc_callback, + void* proc_callback_context, + bool import_users, + const char *bpf_probe, + const char **suppressed_comms) { -#ifdef _WIN32 - snprintf(error, SCAP_LASTERR_SIZE, "live capture not supported on windows"); + snprintf(error, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + *rc = SCAP_NOT_SUPPORTED; return NULL; -#elif defined(__APPLE__) - snprintf(error, SCAP_LASTERR_SIZE, "live capture not supported on OSX"); +} + +scap_t* scap_open_udig_int(char *error, int32_t *rc, + proc_entry_callback proc_callback, + void* proc_callback_context, + bool import_users, + const char **suppressed_comms) +{ + snprintf(error, SCAP_LASTERR_SIZE, "udig capture not supported on %s", PLATFORM_NAME); + *rc = SCAP_NOT_SUPPORTED; return NULL; +} #else + +static uint32_t get_max_consumers() +{ + uint32_t max; + FILE *pfile = fopen("/sys/module/" PROBE_DEVICE_NAME "_probe/parameters/max_consumers", "r"); + if(pfile != NULL) + { + int w = fscanf(pfile, "%"PRIu32, &max); + if(w == 0) + { + return 0; + } + + fclose(pfile); + return max; + } + + return 0; +} + +scap_t* scap_open_live_int(char *error, int32_t *rc, + proc_entry_callback proc_callback, + void* proc_callback_context, + bool import_users, + const char *bpf_probe, + const char **suppressed_comms) +{ uint32_t j; - char dev[255]; + char filename[SCAP_MAX_PATH_SIZE]; scap_t* handle = NULL; - int len; uint32_t ndevs; - uint32_t res; // // Allocate the handle // - handle = (scap_t*)malloc(sizeof(scap_t)); + handle = (scap_t*) calloc(sizeof(scap_t), 1); if(!handle) { snprintf(error, SCAP_LASTERR_SIZE, "error allocating the scap_t structure"); + *rc = SCAP_FAILURE; return NULL; } // // Preliminary initializations // - handle->m_ndevs = 0; - handle->m_proclist = NULL; - handle->m_file = NULL; - handle->m_file_evt_buf = NULL; - handle->m_evtcnt = 0; - handle->m_addrlist = NULL; - handle->m_userlist = NULL; - handle->m_emptybuf_timeout_ms = BUFFER_EMPTY_WAIT_TIME_MS; + handle->m_mode = SCAP_MODE_LIVE; + handle->m_udig = false; // - // Find out how many devices we have to open, which equals to the number of CPUs + // While in theory we could always rely on the scap caller to properly + // set a BPF probe from the environment variable, it's in practice easier + // to do one more check here in scap so we don't have to repeat the logic + // in all the possible users of the libraries (sysdig, csysdig, dragent, ...) // - ndevs = sysconf(_SC_NPROCESSORS_ONLN); + if(!bpf_probe) + { + bpf_probe = scap_get_bpf_probe_from_env(); + } - // - // Allocate the device descriptors. - // - len = RING_BUF_SIZE * 2; + char buf[SCAP_MAX_PATH_SIZE]; + if(bpf_probe) + { + handle->m_bpf = true; - handle->m_devs = (scap_device*)malloc(ndevs * sizeof(scap_device)); - if(!handle->m_devs) + if(strlen(bpf_probe) == 0) + { + const char *home = getenv("HOME"); + if(!home) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "HOME environment not set"); + *rc = SCAP_FAILURE; + return NULL; + } + + snprintf(buf, sizeof(buf), "%s/.sysdig/%s-bpf.o", home, PROBE_NAME); + bpf_probe = buf; + } + } + else + { + handle->m_bpf = false; + } + + handle->m_ncpus = sysconf(_SC_NPROCESSORS_CONF); + if(handle->m_ncpus == -1) { scap_close(handle); - snprintf(error, SCAP_LASTERR_SIZE, "error allocating the device handles"); + snprintf(error, SCAP_LASTERR_SIZE, "_SC_NPROCESSORS_CONF: %s", scap_strerror(handle, errno)); + *rc = SCAP_FAILURE; return NULL; } // - // Allocate the array of poll fds. + // Find out how many devices we have to open, which equals to the number of CPUs // - handle->m_pollfds = (struct pollfd*)malloc(ndevs * sizeof(struct pollfd)); - if(!handle->m_pollfds) + ndevs = sysconf(_SC_NPROCESSORS_ONLN); + if(ndevs == -1) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "_SC_NPROCESSORS_ONLN: %s", scap_strerror(handle, errno)); + *rc = SCAP_FAILURE; + return NULL; + } + + handle->m_devs = (scap_device*) calloc(sizeof(scap_device), ndevs); + if(!handle->m_devs) { scap_close(handle); snprintf(error, SCAP_LASTERR_SIZE, "error allocating the device handles"); + *rc = SCAP_FAILURE; return NULL; } for(j = 0; j < ndevs; j++) { handle->m_devs[j].m_buffer = (char*)MAP_FAILED; - handle->m_devs[j].m_bufinfo = (struct ppm_ring_buffer_info*)MAP_FAILED; + if(!handle->m_bpf) + { + handle->m_devs[j].m_bufinfo = (struct ppm_ring_buffer_info*)MAP_FAILED; + handle->m_devs[j].m_bufstatus = (struct udig_ring_buffer_status*)MAP_FAILED; + } } handle->m_ndevs = ndevs; @@ -118,6 +240,8 @@ scap_t* scap_open_live(char *error) // // Extract machine information // + handle->m_proc_callback = proc_callback; + handle->m_proc_callback_context = proc_callback_context; handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN); handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); gethostname(handle->m_machine_info.hostname, sizeof(handle->m_machine_info.hostname) / sizeof(handle->m_machine_info.hostname[0])); @@ -125,11 +249,16 @@ scap_t* scap_open_live(char *error) handle->m_machine_info.reserved2 = 0; handle->m_machine_info.reserved3 = 0; handle->m_machine_info.reserved4 = 0; + handle->m_driver_procinfo = NULL; + handle->m_fd_lookup_limit = 0; +#ifdef CYGWING_AGENT + handle->m_whh = NULL; +#endif // // Create the interface list // - if(scap_create_iflist(handle) != SCAP_SUCCESS) + if((*rc = scap_create_iflist(handle)) != SCAP_SUCCESS) { scap_close(handle); snprintf(error, SCAP_LASTERR_SIZE, "error creating the interface list"); @@ -139,22 +268,18 @@ scap_t* scap_open_live(char *error) // // Create the user list // - if(scap_create_userlist(handle) != SCAP_SUCCESS) + if(import_users) { - scap_close(handle); - snprintf(error, SCAP_LASTERR_SIZE, "error creating the interface list"); - return NULL; + if((*rc = scap_create_userlist(handle)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the interface list"); + return NULL; + } } - - // - // Create the process list - // - error[0] = '\0'; - if((res = scap_proc_scan_proc_dir(handle, "/proc", -1, -1, NULL, error, true)) != SCAP_SUCCESS) + else { - scap_close(handle); - snprintf(error, SCAP_LASTERR_SIZE, "error creating the process list. Make sure you have root credentials."); - return NULL; + handle->m_userlist = NULL; } handle->m_fake_kernel_proc.tid = -1; @@ -163,73 +288,132 @@ scap_t* scap_open_live(char *error) snprintf(handle->m_fake_kernel_proc.comm, SCAP_MAX_PATH_SIZE, "kernel"); snprintf(handle->m_fake_kernel_proc.exe, SCAP_MAX_PATH_SIZE, "kernel"); handle->m_fake_kernel_proc.args[0] = 0; + handle->refresh_proc_table_when_saving = true; + + handle->m_suppressed_comms = NULL; + handle->m_num_suppressed_comms = 0; + handle->m_suppressed_tids = NULL; + handle->m_num_suppressed_evts = 0; + handle->m_buffer_empty_wait_time_us = BUFFER_EMPTY_WAIT_TIME_US_START; + + if ((*rc = copy_comms(handle, suppressed_comms)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error copying suppressed comms"); + return NULL; + } // // Open and initialize all the devices // - for(j = 0; j < handle->m_ndevs; j++) + if(handle->m_bpf) { - // - // Open the device - // - sprintf(dev, "/dev/sysdig%d", j); - - if((handle->m_devs[j].m_fd = open(dev, O_RDWR | O_SYNC)) < 0) + if((*rc = scap_bpf_load(handle, bpf_probe)) != SCAP_SUCCESS) { + snprintf(error, SCAP_LASTERR_SIZE, "%s", handle->m_lasterr); scap_close(handle); - snprintf(error, SCAP_LASTERR_SIZE, "error opening device %s. Make sure you have root credentials and that the sysdig-probe module is loaded.", dev); return NULL; } + } + else + { + int len; + uint32_t all_scanned_devs; // - // Init the polling fd for the device - // - handle->m_pollfds[j].fd = handle->m_devs[j].m_fd; - handle->m_pollfds[j].events = POLLIN; - - // - // Map the ring buffer + // Allocate the device descriptors. // - handle->m_devs[j].m_buffer = (char*)mmap(0, - len, - PROT_READ, - MAP_SHARED, - handle->m_devs[j].m_fd, - 0); + len = RING_BUF_SIZE * 2; - if(handle->m_devs[j].m_buffer == MAP_FAILED) + for(j = 0, all_scanned_devs = 0; j < handle->m_ndevs && all_scanned_devs < handle->m_ncpus; ++all_scanned_devs) { - // we cleanup this fd and then we let scap_close() take care of the other ones - close(handle->m_devs[j].m_fd); + // + // Open the device + // + snprintf(filename, sizeof(filename), "%s/dev/" PROBE_DEVICE_NAME "%d", scap_get_host_root(), all_scanned_devs); - scap_close(handle); + if((handle->m_devs[j].m_fd = open(filename, O_RDWR | O_SYNC)) < 0) + { + if(errno == ENODEV) + { + // + // This CPU is offline, so we just skip it + // + continue; + } + else if(errno == EBUSY) + { + uint32_t curr_max_consumers = get_max_consumers(); + snprintf(error, SCAP_LASTERR_SIZE, "Too many sysdig instances attached to device %s. Current value for /sys/module/" PROBE_DEVICE_NAME "_probe/parameters/max_consumers is '%"PRIu32"'.", filename, curr_max_consumers); + } + else + { + snprintf(error, SCAP_LASTERR_SIZE, "error opening device %s. Make sure you have root credentials and that the " PROBE_NAME " module is loaded.", filename); + } - snprintf(error, SCAP_LASTERR_SIZE, "error mapping the ring buffer for device %s", dev); - return NULL; - } + scap_close(handle); + *rc = SCAP_FAILURE; + return NULL; + } - // - // Map the ppm_ring_buffer_info that contains the buffer pointers - // - handle->m_devs[j].m_bufinfo = (struct ppm_ring_buffer_info*)mmap(0, - sizeof(struct ppm_ring_buffer_info), - PROT_READ | PROT_WRITE, - MAP_SHARED, - handle->m_devs[j].m_fd, - 0); + // Set close-on-exec for the fd + if (fcntl(handle->m_devs[j].m_fd, F_SETFD, FD_CLOEXEC) == -1) { + snprintf(error, SCAP_LASTERR_SIZE, "Can not set close-on-exec flag for fd for device %s (%s)", filename, scap_strerror(handle, errno)); + scap_close(handle); + *rc = SCAP_FAILURE; + return NULL; + } - if(handle->m_devs[j].m_bufinfo == MAP_FAILED) - { - // we cleanup this fd and then we let scap_close() take care of the other ones - munmap(handle->m_devs[j].m_buffer, len); - close(handle->m_devs[j].m_fd); + // + // Map the ring buffer + // + handle->m_devs[j].m_buffer = (char*)mmap(0, + len, + PROT_READ, + MAP_SHARED, + handle->m_devs[j].m_fd, + 0); + + if(handle->m_devs[j].m_buffer == MAP_FAILED) + { + // we cleanup this fd and then we let scap_close() take care of the other ones + close(handle->m_devs[j].m_fd); - scap_close(handle); + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error mapping the ring buffer for device %s", filename); + *rc = SCAP_FAILURE; + return NULL; + } - snprintf(error, SCAP_LASTERR_SIZE, "error mapping the ring buffer info for device %s", dev); - return NULL; + // + // Map the ppm_ring_buffer_info that contains the buffer pointers + // + handle->m_devs[j].m_bufinfo = (struct ppm_ring_buffer_info*)mmap(0, + sizeof(struct ppm_ring_buffer_info), + PROT_READ | PROT_WRITE, + MAP_SHARED, + handle->m_devs[j].m_fd, + 0); + + if(handle->m_devs[j].m_bufinfo == MAP_FAILED) + { + // we cleanup this fd and then we let scap_close() take care of the other ones + munmap(handle->m_devs[j].m_buffer, len); + close(handle->m_devs[j].m_fd); + + scap_close(handle); + + snprintf(error, SCAP_LASTERR_SIZE, "error mapping the ring buffer info for device %s", filename); + *rc = SCAP_FAILURE; + return NULL; + } + + ++j; } + } + for(j = 0; j < handle->m_ndevs; ++j) + { // // Additional initializations // @@ -238,724 +422,2130 @@ scap_t* scap_open_live(char *error) scap_stop_dropping_mode(handle); } + // + // Create the process list + // + error[0] = '\0'; + snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root()); + if((*rc = scap_proc_scan_proc_dir(handle, filename, error)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the process list. Make sure you have root credentials."); + return NULL; + } + + // + // Now that sysdig has done all its /proc parsing, start the capture + // + if((*rc = scap_start_capture(handle)) != SCAP_SUCCESS) + { + scap_close(handle); + return NULL; + } + return handle; -#endif // _WIN32 } -scap_t* scap_open_offline(char* fname, char *error) +scap_t* scap_open_udig_int(char *error, int32_t *rc, + proc_entry_callback proc_callback, + void* proc_callback_context, + bool import_users, + const char **suppressed_comms) { + char filename[SCAP_MAX_PATH_SIZE]; scap_t* handle = NULL; // // Allocate the handle // - handle = (scap_t*)malloc(sizeof(scap_t)); + handle = (scap_t*) calloc(sizeof(scap_t), 1); if(!handle) { snprintf(error, SCAP_LASTERR_SIZE, "error allocating the scap_t structure"); + *rc = SCAP_FAILURE; return NULL; } // // Preliminary initializations // - handle->m_devs = NULL; - handle->m_ndevs = 0; - handle->m_proclist = NULL; - handle->m_pollfds = NULL; - handle->m_evtcnt = 0; - handle->m_file = NULL; - handle->m_addrlist = NULL; - handle->m_userlist = NULL; - handle->m_machine_info.num_cpus = (uint32_t)-1; + handle->m_mode = SCAP_MODE_LIVE; + handle->m_udig = true; + handle->m_bpf = false; + handle->m_udig_capturing = false; + handle->m_ncpus = 1; - handle->m_file_evt_buf = (char*)malloc(FILE_READ_BUF_SIZE); - if(!handle->m_file_evt_buf) + handle->m_ndevs = 1; + + handle->m_devs = (scap_device*) calloc(sizeof(scap_device), handle->m_ndevs); + if(!handle->m_devs) { - snprintf(error, SCAP_LASTERR_SIZE, "error allocating the read buffer"); scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error allocating the device handles"); + *rc = SCAP_FAILURE; return NULL; } + handle->m_devs[0].m_buffer = MAP_FAILED; + handle->m_devs[0].m_bufinfo = MAP_FAILED; + handle->m_devs[0].m_bufstatus = MAP_FAILED; + handle->m_devs[0].m_fd = -1; + handle->m_devs[0].m_bufinfo_fd = -1; + // - // Open the file + // Extract machine information // - handle->m_file = fopen(fname, "rb"); - if(handle->m_file == NULL) - { - snprintf(error, SCAP_LASTERR_SIZE, "can't open file %s", fname); - scap_close(handle); - return NULL; - } + handle->m_proc_callback = proc_callback; + handle->m_proc_callback_context = proc_callback_context; + handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN); + handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); + gethostname(handle->m_machine_info.hostname, sizeof(handle->m_machine_info.hostname) / sizeof(handle->m_machine_info.hostname[0])); + handle->m_machine_info.reserved1 = 0; + handle->m_machine_info.reserved2 = 0; + handle->m_machine_info.reserved3 = 0; + handle->m_machine_info.reserved4 = 0; + handle->m_driver_procinfo = NULL; + handle->m_fd_lookup_limit = 0; // - // Validate the file and load the non-event blocks + // Create the interface list // - if(scap_read_init(handle, handle->m_file) != SCAP_SUCCESS) + if((*rc = scap_create_iflist(handle)) != SCAP_SUCCESS) { - snprintf(error, SCAP_LASTERR_SIZE, "%s", scap_getlasterr(handle)); scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the interface list"); return NULL; } // - // Add the fake process for kernel threads + // Create the user list // + if(import_users) + { + if((*rc = scap_create_userlist(handle)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the interface list"); + return NULL; + } + } + else + { + handle->m_userlist = NULL; + } + handle->m_fake_kernel_proc.tid = -1; handle->m_fake_kernel_proc.pid = -1; handle->m_fake_kernel_proc.flags = 0; snprintf(handle->m_fake_kernel_proc.comm, SCAP_MAX_PATH_SIZE, "kernel"); snprintf(handle->m_fake_kernel_proc.exe, SCAP_MAX_PATH_SIZE, "kernel"); handle->m_fake_kernel_proc.args[0] = 0; + handle->refresh_proc_table_when_saving = true; -//scap_proc_print_table(handle); - - return handle; -} - -int32_t scap_set_empty_buffer_timeout_ms(scap_t* handle, uint32_t timeout_ms) -{ - handle->m_emptybuf_timeout_ms = timeout_ms; - return SCAP_SUCCESS; -} + handle->m_suppressed_comms = NULL; + handle->m_num_suppressed_comms = 0; + handle->m_suppressed_tids = NULL; + handle->m_num_suppressed_evts = 0; + handle->m_buffer_empty_wait_time_us = BUFFER_EMPTY_WAIT_TIME_US_START; -void scap_close(scap_t* handle) -{ - if(handle->m_file) + if ((*rc = copy_comms(handle, suppressed_comms)) != SCAP_SUCCESS) { - fclose(handle->m_file); + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error copying suppressed comms"); + return NULL; } - else - { -#if !defined(_WIN32) && !defined(__APPLE__) - uint32_t j; - - ASSERT(handle->m_file == NULL); - - // - // Destroy all the device descriptors - // - for(j = 0; j < handle->m_ndevs; j++) - { - if(handle->m_devs[j].m_buffer != MAP_FAILED) - { - munmap(handle->m_devs[j].m_bufinfo, sizeof(struct ppm_ring_buffer_info)); - munmap(handle->m_devs[j].m_buffer, RING_BUF_SIZE * 2); - close(handle->m_devs[j].m_fd); - } - } - // - // Free the memory - // - if(handle->m_devs != NULL) - { - free(handle->m_devs); - } + // + // Map the ring buffer. + // + if(udig_alloc_ring(&(handle->m_devs[0].m_fd), + (uint8_t**)&handle->m_devs[0].m_buffer, + &handle->m_devs[0].m_buffer_size, + error) != SCAP_SUCCESS) + { + scap_close(handle); + *rc = SCAP_FAILURE; + return NULL; + } - if(handle->m_pollfds != NULL) - { - free(handle->m_pollfds); - } -#endif // _WIN32 + // Set close-on-exec for the fd + if (fcntl(handle->m_devs[0].m_fd, F_SETFD, FD_CLOEXEC) == -1) { + snprintf(error, SCAP_LASTERR_SIZE, "Can not set close-on-exec flag for fd for device %s (%s)", filename, scap_strerror(handle, errno)); + scap_close(handle); + *rc = SCAP_FAILURE; + return NULL; } - if(handle->m_file_evt_buf) + // + // Map the ppm_ring_buffer_info that contains the buffer pointers + // + if(udig_alloc_ring_descriptors(&(handle->m_devs[0].m_bufinfo_fd), + &handle->m_devs[0].m_bufinfo, + &handle->m_devs[0].m_bufstatus, + error) != SCAP_SUCCESS) { - free(handle->m_file_evt_buf); + scap_close(handle); + *rc = SCAP_FAILURE; + return NULL; } - // Free the process table - if(handle->m_proclist != NULL) + // + // Additional initializations + // + handle->m_devs[0].m_lastreadsize = 0; + handle->m_devs[0].m_sn_len = 0; + scap_stop_dropping_mode(handle); + + // + // Create the process list + // + error[0] = '\0'; + snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root()); + if((*rc = scap_proc_scan_proc_dir(handle, filename, error)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the process list. Make sure you have root credentials."); + return NULL; + } + + // + // Now that sysdig has done all its /proc parsing, start the capture + // + if(udig_begin_capture(handle, error) != SCAP_SUCCESS) + { + scap_close(handle); + return NULL; + } + + return handle; +} +#endif // !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + +scap_t* scap_open_offline_int(gzFile gzfile, + char *error, + int32_t *rc, + proc_entry_callback proc_callback, + void* proc_callback_context, + bool import_users, + uint64_t start_offset, + const char **suppressed_comms) +{ + scap_t* handle = NULL; + + // + // Allocate the handle + // + handle = (scap_t*)malloc(sizeof(scap_t)); + if(!handle) + { + snprintf(error, SCAP_LASTERR_SIZE, "error allocating the scap_t structure"); + *rc = SCAP_FAILURE; + return NULL; + } + + // + // Preliminary initializations + // + handle->m_mode = SCAP_MODE_CAPTURE; + handle->m_proc_callback = proc_callback; + handle->m_proc_callback_context = proc_callback_context; + handle->m_devs = NULL; + handle->m_ndevs = 0; + handle->m_proclist = NULL; + handle->m_dev_list = NULL; + handle->m_evtcnt = 0; + handle->m_file = NULL; + handle->m_addrlist = NULL; + handle->m_userlist = NULL; + handle->m_machine_info.num_cpus = (uint32_t)-1; + handle->m_last_evt_dump_flags = 0; + handle->m_driver_procinfo = NULL; + handle->refresh_proc_table_when_saving = true; + handle->m_fd_lookup_limit = 0; +#ifdef CYGWING_AGENT + handle->m_whh = NULL; +#endif + handle->m_bpf = false; + handle->m_udig = false; + handle->m_suppressed_comms = NULL; + handle->m_suppressed_tids = NULL; + + handle->m_file_evt_buf = (char*)malloc(FILE_READ_BUF_SIZE); + if(!handle->m_file_evt_buf) + { + snprintf(error, SCAP_LASTERR_SIZE, "error allocating the read buffer"); + scap_close(handle); + *rc = SCAP_FAILURE; + return NULL; + } + + handle->m_file = gzfile; + + // + // If this is a merged file, we might have to move the read offset to the next section + // + if(start_offset != 0) + { + scap_fseek(handle, start_offset); + } + + // + // Validate the file and load the non-event blocks + // + if((*rc = scap_read_init(handle, handle->m_file)) != SCAP_SUCCESS) + { + snprintf(error, SCAP_LASTERR_SIZE, "Could not initialize reader: %s", scap_getlasterr(handle)); + scap_close(handle); + return NULL; + } + + if(!import_users) + { + if(handle->m_userlist != NULL) + { + scap_free_userlist(handle->m_userlist); + handle->m_userlist = NULL; + } + } + + // + // Add the fake process for kernel threads + // + handle->m_fake_kernel_proc.tid = -1; + handle->m_fake_kernel_proc.pid = -1; + handle->m_fake_kernel_proc.flags = 0; + snprintf(handle->m_fake_kernel_proc.comm, SCAP_MAX_PATH_SIZE, "kernel"); + snprintf(handle->m_fake_kernel_proc.exe, SCAP_MAX_PATH_SIZE, "kernel"); + handle->m_fake_kernel_proc.args[0] = 0; + + handle->m_num_suppressed_comms = 0; + handle->m_num_suppressed_evts = 0; + + if ((*rc = copy_comms(handle, suppressed_comms)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error copying suppressed comms"); + return NULL; + } + + return handle; +} + +scap_t* scap_open_offline(const char* fname, char *error, int32_t* rc) +{ + gzFile gzfile = gzopen(fname, "rb"); + if(gzfile == NULL) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't open file %s", fname); + *rc = SCAP_FAILURE; + return NULL; + } + + return scap_open_offline_int(gzfile, error, rc, NULL, NULL, true, 0, NULL); +} + +scap_t* scap_open_offline_fd(int fd, char *error, int32_t *rc) +{ + gzFile gzfile = gzdopen(fd, "rb"); + if(gzfile == NULL) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't open fd %d", fd); + *rc = SCAP_FAILURE; + return NULL; + } + + return scap_open_offline_int(gzfile, error, rc, NULL, NULL, true, 0, NULL); +} + +scap_t* scap_open_live(char *error, int32_t *rc) +{ + return scap_open_live_int(error, rc, NULL, NULL, true, NULL, NULL); +} + +scap_t* scap_open_nodriver_int(char *error, int32_t *rc, + proc_entry_callback proc_callback, + void* proc_callback_context, + bool import_users) +{ +#if !defined(HAS_CAPTURE) + snprintf(error, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + *rc = SCAP_NOT_SUPPORTED; + return NULL; +#else + char filename[SCAP_MAX_PATH_SIZE]; + scap_t* handle = NULL; + + // + // Allocate the handle + // + handle = (scap_t*)malloc(sizeof(scap_t)); + if(!handle) + { + snprintf(error, SCAP_LASTERR_SIZE, "error allocating the scap_t structure"); + *rc = SCAP_FAILURE; + return NULL; + } + + // + // Preliminary initializations + // + memset(handle, 0, sizeof(scap_t)); + handle->m_mode = SCAP_MODE_NODRIVER; + + // + // Extract machine information + // + handle->m_proc_callback = proc_callback; + handle->m_proc_callback_context = proc_callback_context; + handle->m_machine_info.num_cpus = sysconf(_SC_NPROCESSORS_ONLN); + handle->m_machine_info.memory_size_bytes = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); + gethostname(handle->m_machine_info.hostname, sizeof(handle->m_machine_info.hostname) / sizeof(handle->m_machine_info.hostname[0])); + handle->m_machine_info.reserved1 = 0; + handle->m_machine_info.reserved2 = 0; + handle->m_machine_info.reserved3 = 0; + handle->m_machine_info.reserved4 = 0; + handle->m_driver_procinfo = NULL; + handle->m_fd_lookup_limit = SCAP_NODRIVER_MAX_FD_LOOKUP; // fd lookup is limited here because is very expensive + + // + // If this is part of the windows agent, open the windows HAL + // +#ifdef CYGWING_AGENT + handle->m_whh = wh_open(error); + if(handle->m_whh == NULL) + { + scap_close(handle); + *rc = SCAP_FAILURE; + return NULL; + } +#endif + + // + // Create the interface list + // + if((*rc = scap_create_iflist(handle)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the interface list"); + return NULL; + } + + // + // Create the user list + // + if(import_users) + { + if((*rc = scap_create_userlist(handle)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the interface list"); + return NULL; + } + } + else + { + handle->m_userlist = NULL; + } + + handle->m_fake_kernel_proc.tid = -1; + handle->m_fake_kernel_proc.pid = -1; + handle->m_fake_kernel_proc.flags = 0; + snprintf(handle->m_fake_kernel_proc.comm, SCAP_MAX_PATH_SIZE, "kernel"); + snprintf(handle->m_fake_kernel_proc.exe, SCAP_MAX_PATH_SIZE, "kernel"); + handle->m_fake_kernel_proc.args[0] = 0; + handle->refresh_proc_table_when_saving = true; + + // + // Create the process list + // + error[0] = '\0'; + snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root()); + if((*rc = scap_proc_scan_proc_dir(handle, filename, error)) != SCAP_SUCCESS) + { + scap_close(handle); + snprintf(error, SCAP_LASTERR_SIZE, "error creating the process list. Make sure you have root credentials."); + return NULL; + } + + return handle; +#endif // HAS_CAPTURE +} + +scap_t* scap_open(scap_open_args args, char *error, int32_t *rc) +{ + switch(args.mode) + { + case SCAP_MODE_CAPTURE: + { + gzFile gzfile; + + if(args.fd != 0) + { + gzfile = gzdopen(args.fd, "rb"); + } + else + { + gzfile = gzopen(args.fname, "rb"); + } + + if(gzfile == NULL) + { + if(args.fd != 0) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't open fd %d", args.fd); + } + else + { + snprintf(error, SCAP_LASTERR_SIZE, "can't open file %s", args.fname); + } + *rc = SCAP_FAILURE; + return NULL; + } + + return scap_open_offline_int(gzfile, error, rc, + args.proc_callback, args.proc_callback_context, + args.import_users, args.start_offset, + args.suppressed_comms); + } + case SCAP_MODE_LIVE: +#ifndef CYGWING_AGENT + if(args.udig) + { + return scap_open_udig_int(error, rc, args.proc_callback, + args.proc_callback_context, + args.import_users, + args.suppressed_comms); + } + else + { + return scap_open_live_int(error, rc, args.proc_callback, + args.proc_callback_context, + args.import_users, + args.bpf_probe, + args.suppressed_comms); + } +#else + snprintf(error, SCAP_LASTERR_SIZE, "scap_open: live mode currently not supported on windows. Use nodriver mode instead."); + *rc = SCAP_NOT_SUPPORTED; + return NULL; +#endif + case SCAP_MODE_NODRIVER: + return scap_open_nodriver_int(error, rc, args.proc_callback, + args.proc_callback_context, + args.import_users); + case SCAP_MODE_NONE: + // error + break; + } + + + snprintf(error, SCAP_LASTERR_SIZE, "incorrect mode %d", args.mode); + *rc = SCAP_FAILURE; + return NULL; +} + +void scap_close(scap_t* handle) +{ + if(handle->m_file) + { + gzclose(handle->m_file); + } + else if(handle->m_mode == SCAP_MODE_LIVE) + { +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + uint32_t j; + + ASSERT(handle->m_file == NULL); + + if(handle->m_devs != NULL) + { + if(handle->m_bpf) + { + if(scap_bpf_close(handle) != SCAP_SUCCESS) + { + ASSERT(false); + } + } + else if(handle->m_udig) + { + udig_end_capture(handle); + + if(handle->m_devs[0].m_buffer != MAP_FAILED) + { + udig_free_ring((uint8_t*)handle->m_devs[0].m_buffer, handle->m_devs[0].m_buffer_size); + } + if(handle->m_devs[0].m_bufinfo != MAP_FAILED) + { + udig_free_ring_descriptors((uint8_t*)handle->m_devs[0].m_bufinfo); + } + if(handle->m_devs[0].m_fd != -1) + { + close(handle->m_devs[0].m_fd); + } + if(handle->m_devs[0].m_bufinfo_fd != -1) + { + close(handle->m_devs[0].m_bufinfo_fd); + } + } + else + { + // + // Destroy all the device descriptors + // + for(j = 0; j < handle->m_ndevs; j++) + { + if(handle->m_devs[j].m_buffer != MAP_FAILED) + { + munmap(handle->m_devs[j].m_bufinfo, sizeof(struct ppm_ring_buffer_info)); + munmap(handle->m_devs[j].m_buffer, RING_BUF_SIZE * 2); + close(handle->m_devs[j].m_fd); + } + } + } + + // + // Free the memory + // + free(handle->m_devs); + } +#endif // HAS_CAPTURE + } + +#ifdef CYGWING_AGENT + if(handle->m_whh != NULL) + { + wh_close(handle->m_whh); + } +#endif + + if(handle->m_file_evt_buf) + { + free(handle->m_file_evt_buf); + } + + // Free the process table + if(handle->m_proclist != NULL) + { + scap_proc_free_table(handle); + } + + // Free the device table + if(handle->m_dev_list != NULL) + { + scap_free_device_table(handle); + } + + // Free the interface list + if(handle->m_addrlist) + { + scap_free_iflist(handle->m_addrlist); + } + + // Free the user list + if(handle->m_userlist) + { + scap_free_userlist(handle->m_userlist); + } + + if(handle->m_driver_procinfo) + { + free(handle->m_driver_procinfo); + handle->m_driver_procinfo = NULL; + } + + if(handle->m_suppressed_comms) + { + uint32_t i; + for(i=0; i < handle->m_num_suppressed_comms; i++) + { + free(handle->m_suppressed_comms[i]); + } + free(handle->m_suppressed_comms); + handle->m_suppressed_comms = NULL; + } + + if(handle->m_suppressed_tids) + { + struct scap_tid *tid; + struct scap_tid *ttid; + HASH_ITER(hh, handle->m_suppressed_tids, tid, ttid) + { + HASH_DEL(handle->m_suppressed_tids, tid); + free(tid); + } + + handle->m_suppressed_tids = NULL; + } + + // + // Release the handle + // + free(handle); +} + +scap_os_platform scap_get_os_platform(scap_t* handle) +{ +#if defined(_M_IX86) || defined(__i386__) +#ifdef linux + return SCAP_PFORM_LINUX_I386; +#else + return SCAP_PFORM_WINDOWS_I386; +#endif // linux +#else +#if defined(_M_X64) || defined(__AMD64__) +#ifdef linux + return SCAP_PFORM_LINUX_X64; +#else + return SCAP_PFORM_WINDOWS_X64; +#endif // linux +#else + return SCAP_PFORM_UNKNOWN; +#endif // defined(_M_X64) || defined(__AMD64__) +#endif // defined(_M_IX86) || defined(__i386__) +} + +uint32_t scap_get_ndevs(scap_t* handle) +{ + return handle->m_ndevs; +} + +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + +#ifndef _WIN32 +static inline void get_buf_pointers(struct ppm_ring_buffer_info* bufinfo, uint32_t* phead, uint32_t* ptail, uint64_t* pread_size) +#else +void get_buf_pointers(struct ppm_ring_buffer_info* bufinfo, uint32_t* phead, uint32_t* ptail, uint64_t* pread_size) +#endif +{ + *phead = bufinfo->head; + *ptail = bufinfo->tail; + + if(*ptail > *phead) + { + *pread_size = RING_BUF_SIZE - *ptail + *phead; + } + else + { + *pread_size = *phead - *ptail; + } +} + +static void scap_advance_tail(scap_t* handle, uint32_t cpuid) +{ + uint32_t ttail; + + if(handle->m_bpf) + { + return scap_bpf_advance_tail(handle, cpuid); + } + + // + // Update the tail based on the amount of data read in the *previous* call. + // Tail is never updated when we serve the data, because we assume that the caller is using + // the buffer we give to her until she calls us again. + // + ttail = handle->m_devs[cpuid].m_bufinfo->tail + handle->m_devs[cpuid].m_lastreadsize; + + // + // Make sure every read of the old buffer is completed before we move the tail and the + // producer (on another CPU) can start overwriting it. + // I use this instead of asm(mfence) because it should be portable even on the weirdest + // CPUs + // + __sync_synchronize(); + + if(ttail < RING_BUF_SIZE) + { + handle->m_devs[cpuid].m_bufinfo->tail = ttail; + } + else + { + handle->m_devs[cpuid].m_bufinfo->tail = ttail - RING_BUF_SIZE; + } + + handle->m_devs[cpuid].m_lastreadsize = 0; +} + +int32_t scap_readbuf(scap_t* handle, uint32_t cpuid, OUT char** buf, OUT uint32_t* len) +{ + uint32_t thead; + uint32_t ttail; + uint64_t read_size; + + if(handle->m_bpf) + { + return scap_bpf_readbuf(handle, cpuid, buf, len); + } + + // + // Read the pointers. + // + get_buf_pointers(handle->m_devs[cpuid].m_bufinfo, + &thead, + &ttail, + &read_size); + + // + // Remember read_size so we can update the tail at the next call + // + handle->m_devs[cpuid].m_lastreadsize = read_size; + + // + // Return the results + // + *len = read_size; + *buf = handle->m_devs[cpuid].m_buffer + ttail; + + return SCAP_SUCCESS; +} + +static uint64_t buf_size_used(scap_t* handle, uint32_t cpu) +{ + uint64_t read_size; + + if (handle->m_bpf) + { + uint64_t thead; + uint64_t ttail; + + scap_bpf_get_buf_pointers(handle->m_devs[cpu].m_buffer, &thead, &ttail, &read_size); + } + else + { + uint32_t thead; + uint32_t ttail; + + get_buf_pointers(handle->m_devs[cpu].m_bufinfo, &thead, &ttail, &read_size); + } + + return read_size; +} + +static bool are_buffers_empty(scap_t* handle) +{ + uint32_t j; + + for(j = 0; j < handle->m_ndevs; j++) + { + if(buf_size_used(handle, j) > BUFFER_EMPTY_THRESHOLD_B) + { + return false; + } + } + + return true; +} + +int32_t refill_read_buffers(scap_t* handle) +{ + uint32_t j; + uint32_t ndevs = handle->m_ndevs; + + if(are_buffers_empty(handle)) + { + usleep(handle->m_buffer_empty_wait_time_us); + handle->m_buffer_empty_wait_time_us = MIN(handle->m_buffer_empty_wait_time_us * 2, + BUFFER_EMPTY_WAIT_TIME_US_MAX); + } + else + { + handle->m_buffer_empty_wait_time_us = BUFFER_EMPTY_WAIT_TIME_US_START; + } + + // + // Refill our data for each of the devices + // + + for(j = 0; j < ndevs; j++) + { + struct scap_device *dev = &(handle->m_devs[j]); + + int32_t res = scap_readbuf(handle, + j, + &dev->m_sn_next_event, + &dev->m_sn_len); + + if(res != SCAP_SUCCESS) + { + return res; + } + } + + // + // Note: we might return a spurious timeout here in case the previous loop extracted valid data to parse. + // It's ok, since this is rare and the caller will just call us again after receiving a + // SCAP_TIMEOUT. + // + return SCAP_TIMEOUT; +} + +#endif // HAS_CAPTURE + +#ifndef _WIN32 +static inline int32_t scap_next_live(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +#else +static int32_t scap_next_live(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +#endif +{ +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + // + // this should be prevented at open time + // + ASSERT(false); + return SCAP_FAILURE; +#else + uint32_t j; + uint64_t max_ts = 0xffffffffffffffffLL; + scap_evt* pe = NULL; + uint32_t ndevs = handle->m_ndevs; + + *pcpuid = 65535; + + for(j = 0; j < ndevs; j++) + { + scap_device* dev = &(handle->m_devs[j]); + + if(dev->m_sn_len == 0) + { + // + // If we don't have data from this ring, but we are + // still occupying, free the resources for the + // producer rather than sitting on them. + // + if(dev->m_lastreadsize > 0) + { + scap_advance_tail(handle, j); + } + + continue; + } + + if(handle->m_bpf) + { + pe = scap_bpf_evt_from_perf_sample(dev->m_sn_next_event); + } + else + { + pe = (scap_evt *) dev->m_sn_next_event; + } + + // + // We want to consume the event with the lowest timestamp + // + if(pe->ts < max_ts) + { + if(pe->len > dev->m_sn_len) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_next buffer corruption"); + + // + // if you get the following assertion, first recompile the driver and libscap + // + ASSERT(false); + return SCAP_FAILURE; + } + + *pevent = pe; + *pcpuid = j; + max_ts = pe->ts; + } + } + + // + // Check which buffer has been picked + // + if(*pcpuid != 65535) + { + struct scap_device *dev = &handle->m_devs[*pcpuid]; + + // + // Update the pointers. + // + if(handle->m_bpf) + { + scap_bpf_advance_to_evt(handle, *pcpuid, true, + dev->m_sn_next_event, + &dev->m_sn_next_event, + &dev->m_sn_len); + } + else + { + ASSERT(dev->m_sn_len >= (*pevent)->len); + dev->m_sn_len -= (*pevent)->len; + dev->m_sn_next_event += (*pevent)->len; + } + + return SCAP_SUCCESS; + } + else + { + // + // All the buffers have been consumed. Check if there's enough data to keep going or + // if we should wait. + // + return refill_read_buffers(handle); + } +#endif +} + +#ifndef _WIN32 +static inline int32_t scap_next_udig(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +#else +static int32_t scap_next_udig(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +#endif +{ +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + // + // this should be prevented at open time + // + ASSERT(false); + return SCAP_FAILURE; +#else + uint32_t j; + uint64_t max_ts = 0xffffffffffffffffLL; + scap_evt* pe = NULL; + uint32_t ndevs = handle->m_ndevs; + + *pcpuid = 65535; + + for(j = 0; j < ndevs; j++) + { + scap_device* dev = &(handle->m_devs[j]); + + if(dev->m_sn_len == 0) + { + // + // If we don't have data from this ring, but we are + // still occupying, free the resources for the + // producer rather than sitting on them. + // + if(dev->m_lastreadsize > 0) + { + scap_advance_tail(handle, j); + } + + continue; + } + + if(handle->m_bpf) + { + pe = scap_bpf_evt_from_perf_sample(dev->m_sn_next_event); + } + else + { + pe = (scap_evt *) dev->m_sn_next_event; + } + + // + // We want to consume the event with the lowest timestamp + // + if(pe->ts < max_ts) + { + if(pe->len > dev->m_sn_len) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_next buffer corruption"); + + // + // if you get the following assertion, first recompile the driver and libscap + // + ASSERT(false); + return SCAP_FAILURE; + } + + *pevent = pe; + *pcpuid = j; + max_ts = pe->ts; + } + } + + // + // Check which buffer has been picked + // + if(*pcpuid != 65535) + { + struct scap_device *dev = &handle->m_devs[*pcpuid]; + + // + // Update the pointers. + // + if(handle->m_bpf) + { + scap_bpf_advance_to_evt(handle, *pcpuid, true, + dev->m_sn_next_event, + &dev->m_sn_next_event, + &dev->m_sn_len); + } + else + { + ASSERT(dev->m_sn_len >= (*pevent)->len); + dev->m_sn_len -= (*pevent)->len; + dev->m_sn_next_event += (*pevent)->len; + } + + return SCAP_SUCCESS; + } + else + { + // + // All the buffers have been consumed. Check if there's enough data to keep going or + // if we should wait. + // + return refill_read_buffers(handle); + } +#endif +} + +#ifndef _WIN32 +static int32_t scap_next_nodriver(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +{ + static scap_evt evt; + evt.len = 0; + evt.tid = -1; + evt.type = PPME_SYSDIGEVENT_X; + evt.nparams = 0; + + usleep(100000); + + struct timeval tv; + gettimeofday(&tv, NULL); + + evt.ts = tv.tv_sec * (uint64_t) 1000000000 + tv.tv_usec * 1000; + *pevent = &evt; + return SCAP_SUCCESS; +} +#endif // _WIN32 + +uint64_t scap_max_buf_used(scap_t* handle) +{ +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + uint64_t i; + uint64_t max = 0; + + for(i = 0; i < handle->m_ndevs; i++) + { + uint64_t size = buf_size_used(handle, i); + max = size > max ? size : max; + } + + return max; +#else + return 0; +#endif +} + +int32_t scap_next(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +{ + int32_t res = SCAP_FAILURE; + + switch(handle->m_mode) + { + case SCAP_MODE_CAPTURE: + res = scap_next_offline(handle, pevent, pcpuid); + break; + case SCAP_MODE_LIVE: + if(handle->m_udig) + { + res = scap_next_udig(handle, pevent, pcpuid); + } + else + { + res = scap_next_live(handle, pevent, pcpuid); + } + break; +#ifndef _WIN32 + case SCAP_MODE_NODRIVER: + res = scap_next_nodriver(handle, pevent, pcpuid); + break; +#endif + case SCAP_MODE_NONE: + res = SCAP_FAILURE; + } + + if(res == SCAP_SUCCESS) + { + bool suppressed; + + // Check to see if the event should be suppressed due + // to coming from a supressed tid + if((res = scap_check_suppressed(handle, *pevent, &suppressed)) != SCAP_SUCCESS) + { + return res; + } + + if(suppressed) + { + handle->m_num_suppressed_evts++; + return SCAP_TIMEOUT; + } + else + { + handle->m_evtcnt++; + } + } + + return res; +} + +// +// Return the process list for the given handle +// +scap_threadinfo* scap_get_proc_table(scap_t* handle) +{ + return handle->m_proclist; +} + +// +// Return the number of dropped events for the given handle +// +int32_t scap_get_stats(scap_t* handle, OUT scap_stats* stats) +{ + uint32_t j; + + stats->n_evts = 0; + stats->n_drops = 0; + stats->n_drops_buffer = 0; + stats->n_drops_pf = 0; + stats->n_drops_bug = 0; + stats->n_preemptions = 0; + stats->n_suppressed = handle->m_num_suppressed_evts; + stats->n_tids_suppressed = HASH_COUNT(handle->m_suppressed_tids); + +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + if(handle->m_bpf) + { + return scap_bpf_get_stats(handle, stats); + } + else + { + for(j = 0; j < handle->m_ndevs; j++) + { + stats->n_evts += handle->m_devs[j].m_bufinfo->n_evts; + stats->n_drops_buffer += handle->m_devs[j].m_bufinfo->n_drops_buffer; + stats->n_drops_pf += handle->m_devs[j].m_bufinfo->n_drops_pf; + stats->n_drops += handle->m_devs[j].m_bufinfo->n_drops_buffer + + handle->m_devs[j].m_bufinfo->n_drops_pf; + stats->n_preemptions += handle->m_devs[j].m_bufinfo->n_preemptions; + } + } +#endif + + return SCAP_SUCCESS; +} + +// +// Stop capturing the events +// +int32_t scap_stop_capture(scap_t* handle) +{ +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + uint32_t j; + + // + // Not supported for files + // + if(handle->m_mode == SCAP_MODE_LIVE) + { + // + // Disable capture on all the rings + // + for(j = 0; j < handle->m_ndevs; j++) + { + if(handle->m_bpf) + { + return scap_bpf_stop_capture(handle); + } + else if(handle->m_udig) + { + udig_stop_capture(handle); + } + else + { + if(ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_DISABLE_CAPTURE)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_stop_capture failed for device %" PRIu32, j); + ASSERT(false); + return SCAP_FAILURE; + } + } + } + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "cannot stop offline live captures"); + ASSERT(false); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +#endif // HAS_CAPTURE +} + +// +// Start capturing the events +// +int32_t scap_start_capture(scap_t* handle) +{ +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + uint32_t j; + + // + // Not supported for files + // + if(handle->m_mode == SCAP_MODE_LIVE) + { + // + // Enable capture on all the rings + // + if(handle->m_bpf) + { + return scap_bpf_start_capture(handle); + } + else if(handle->m_udig) + { + udig_start_capture(handle); + } + else + { + for(j = 0; j < handle->m_ndevs; j++) + { + if(ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_ENABLE_CAPTURE)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_start_capture failed for device %" PRIu32, j); + ASSERT(false); + return SCAP_FAILURE; + } + } + } + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "cannot start offline live captures"); + ASSERT(false); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +#endif // HAS_CAPTURE +} + +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) +static int32_t scap_set_dropping_mode(scap_t* handle, int request, uint32_t sampling_ratio) +{ + // + // Not supported for files + // + if(handle->m_mode != SCAP_MODE_LIVE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "%s: dropping not supported in replay mode", __FUNCTION__); + ASSERT(false); + return SCAP_FAILURE; + } + + if(handle->m_ndevs) + { + ASSERT((request == PPM_IOCTL_ENABLE_DROPPING_MODE && + ((sampling_ratio == 1) || + (sampling_ratio == 2) || + (sampling_ratio == 4) || + (sampling_ratio == 8) || + (sampling_ratio == 16) || + (sampling_ratio == 32) || + (sampling_ratio == 64) || + (sampling_ratio == 128))) || (request == PPM_IOCTL_DISABLE_DROPPING_MODE)); + + if(ioctl(handle->m_devs[0].m_fd, request, sampling_ratio)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "%s, request %d for sampling ratio %u: %s", + __FUNCTION__, request, sampling_ratio, scap_strerror(handle, errno)); + ASSERT(false); + return SCAP_FAILURE; + } + } + + return SCAP_SUCCESS; +} +#endif + +#if defined(HAS_CAPTURE) && ! defined(CYGWING_AGENT) +int32_t scap_enable_tracers_capture(scap_t* handle) +{ + // + // Not supported for files + // + if(handle->m_mode != SCAP_MODE_LIVE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_enable_tracers_capture not supported on this scap mode"); + ASSERT(false); + return SCAP_FAILURE; + } + + if(handle->m_ndevs) + { + if(handle->m_bpf) + { + return scap_bpf_enable_tracers_capture(handle); + } + else if(!handle->m_udig) + { + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_SET_TRACERS_CAPTURE)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "%s failed", __FUNCTION__); + ASSERT(false); + return SCAP_FAILURE; + } + } + } + + return SCAP_SUCCESS; +} +#endif + +#if defined(HAS_CAPTURE) && ! defined(CYGWING_AGENT) +int32_t scap_enable_page_faults(scap_t *handle) +{ + if(handle->m_mode != SCAP_MODE_LIVE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_enable_page_faults not supported on this scap mode"); + ASSERT(false); + return SCAP_FAILURE; + } + + if(handle->m_ndevs) + { + if(handle->m_bpf) + { + return scap_bpf_enable_page_faults(handle); + } + else + { + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_ENABLE_PAGE_FAULTS)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "%s failed", __FUNCTION__); + ASSERT(false); + return SCAP_FAILURE; + } + } + } + + return SCAP_SUCCESS; +} +#endif + +int32_t scap_stop_dropping_mode(scap_t* handle) +{ +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + if(handle->m_bpf) { - scap_proc_free_table(handle); + return scap_bpf_stop_dropping_mode(handle); } - - // Free the interface list - if(handle->m_addrlist) + if(handle->m_udig) { - scap_free_iflist(handle->m_addrlist); + return udig_stop_dropping_mode(handle); } - - // Free the user list - if(handle->m_userlist) + else { - scap_free_userlist(handle->m_userlist); + return scap_set_dropping_mode(handle, PPM_IOCTL_DISABLE_DROPPING_MODE, 0); } - - // - // Release the handle - // - free(handle); +#endif } -scap_os_patform scap_get_os_platform(scap_t* handle) +int32_t scap_start_dropping_mode(scap_t* handle, uint32_t sampling_ratio) { -#if defined(_M_IX86) || defined(__i386__) -#ifdef linux - return SCAP_PFORM_LINUX_I386; -#else - return SCAP_PFORM_WINDOWS_I386; -#endif // linux -#else -#if defined(_M_X64) || defined(__AMD64__) -#ifdef linux - return SCAP_PFORM_LINUX_X64; -#else - return SCAP_PFORM_WINDOWS_X64; -#endif // linux +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; #else - return SCAP_PFORM_UNKNOWN; -#endif // defined(_M_X64) || defined(__AMD64__) -#endif // defined(_M_IX86) || defined(__i386__) + if(handle->m_bpf) + { + return scap_bpf_start_dropping_mode(handle, sampling_ratio); + } + else if(handle->m_udig) + { + return udig_start_dropping_mode(handle, sampling_ratio); + } + else + { + return scap_set_dropping_mode(handle, PPM_IOCTL_ENABLE_DROPPING_MODE, sampling_ratio); + } +#endif } -uint32_t scap_get_ndevs(scap_t* handle) +// +// Return the list of device addresses +// +scap_addrlist* scap_get_ifaddr_list(scap_t* handle) { - return handle->m_ndevs; + return handle->m_addrlist; } -#ifndef _WIN32 -inline void get_buf_pointers(struct ppm_ring_buffer_info* bufinfo, uint32_t* phead, uint32_t* ptail, uint32_t* pread_size) -#else -void get_buf_pointers(struct ppm_ring_buffer_info* bufinfo, uint32_t* phead, uint32_t* ptail, uint32_t* pread_size) -#endif +// +// Return the list of machine users +// +scap_userlist* scap_get_user_list(scap_t* handle) { - *phead = bufinfo->head; - *ptail = bufinfo->tail; + return handle->m_userlist; +} - if(*ptail > *phead) +// +// Get the machine information +// +const scap_machine_info* scap_get_machine_info(scap_t* handle) +{ + if(handle->m_machine_info.num_cpus != (uint32_t)-1) { - *pread_size = RING_BUF_SIZE - *ptail + *phead; + return (const scap_machine_info*)&handle->m_machine_info; } else { - *pread_size = *phead - *ptail; + // + // Reading from a file with no process info block + // + return NULL; } } -#if !defined(_WIN32) && !defined(__APPLE__) -int32_t scap_readbuf(scap_t* handle, uint32_t cpuid, bool blocking, OUT char** buf, OUT uint32_t* len) +int32_t scap_set_snaplen(scap_t* handle, uint32_t snaplen) { - uint32_t thead; - uint32_t ttail; - uint32_t read_size; - // - // Update the tail based on the amount of data read in the *previous* call. - // Tail is never updated when we serve the data, because we assume that the caller is using - // the buffer we give to her until she calls us again. + // Not supported on files // - ttail = handle->m_devs[cpuid].m_bufinfo->tail + handle->m_devs[cpuid].m_lastreadsize; + if(handle->m_mode != SCAP_MODE_LIVE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "setting snaplen not supported on this scap mode"); + return SCAP_FAILURE; + } - // - // Make sure every read of the old buffer is completed before we move the tail and the - // producer (on another CPU) can start overwriting it. - // I use this instead of asm(mfence) because it should be portable even on the weirdest - // CPUs - // - __sync_synchronize(); +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else - if(ttail < RING_BUF_SIZE) + if(handle->m_bpf) { - handle->m_devs[cpuid].m_bufinfo->tail = ttail; + return scap_bpf_set_snaplen(handle, snaplen); } - else + else if(handle->m_udig) { - handle->m_devs[cpuid].m_bufinfo->tail = ttail - RING_BUF_SIZE; + return udig_set_snaplen(handle, snaplen); } - - // - // Does the user want to block? - // - if(blocking) + else { // - // If we are asked to operate in blocking mode, keep waiting until at least - // MIN_USERSPACE_READ_SIZE bytes are in the buffer. + // Tell the driver to change the snaplen // - while(true) + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_SET_SNAPLEN, snaplen)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_set_snaplen failed"); + ASSERT(false); + return SCAP_FAILURE; + } + { - get_buf_pointers(handle->m_devs[cpuid].m_bufinfo, - &thead, - &ttail, - &read_size); + uint32_t j; - if(read_size >= MIN_USERSPACE_READ_SIZE) + // + // Force a flush of the read buffers, so we don't capture events with the old snaplen + // + for(j = 0; j < handle->m_ndevs; j++) { - break; - } + scap_readbuf(handle, + j, + &handle->m_devs[j].m_sn_next_event, + &handle->m_devs[j].m_sn_len); - usleep(BUFFER_EMPTY_WAIT_TIME_MS * 1000); + handle->m_devs[j].m_sn_len = 0; + } } } - else + + return SCAP_SUCCESS; +#endif +} + +int64_t scap_get_readfile_offset(scap_t* handle) +{ + if(handle->m_mode != SCAP_MODE_CAPTURE) { - // - // If we are not asked to block, read the pointers and keep going. - // - get_buf_pointers(handle->m_devs[cpuid].m_bufinfo, - &thead, - &ttail, - &read_size); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_get_readfile_offset only works on captures"); + return -1; } - // - // logic check - // XXX should probably be an assertion, but for the moment we want to print some meaningful info and - // stop the processing. - // - if((handle->m_devs[cpuid].m_bufinfo->tail + read_size) % RING_BUF_SIZE != thead) + return gzoffset(handle->m_file); +} + +#ifndef CYGWING_AGENT +static int32_t scap_handle_eventmask(scap_t* handle, uint32_t op, uint32_t event_id) +{ + if (handle == NULL) { - snprintf(handle->m_lasterr, - SCAP_LASTERR_SIZE, - "buffer corruption. H=%u, T=%u, R=%u, S=%u (%u)", - thead, - handle->m_devs[cpuid].m_bufinfo->tail, - read_size, - RING_BUF_SIZE, - (handle->m_devs[cpuid].m_bufinfo->tail + read_size) % RING_BUF_SIZE); - ASSERT(false); return SCAP_FAILURE; } -#if 0 - printf("%u)H:%u T:%u Used:%u Free:%u Size=%u\n", - cpuid, - thead, - ttail, - read_size, - (uint32_t)(RING_BUF_SIZE - read_size - 1), - (uint32_t)RING_BUF_SIZE); -#endif - // - // Remember read_size so we can update the tail at the next call + // Not supported on files // - handle->m_devs[cpuid].m_lastreadsize = read_size; + if(handle->m_mode != SCAP_MODE_LIVE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "manipulating eventmasks not supported on this scap mode"); + return SCAP_FAILURE; + } +#if !defined(HAS_CAPTURE) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "eventmask not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else // - // Return the results + // Tell the driver to change the snaplen // - *len = read_size; - *buf = handle->m_devs[cpuid].m_buffer + ttail; - return SCAP_SUCCESS; -} + switch(op) { + case PPM_IOCTL_MASK_ZERO_EVENTS: + case PPM_IOCTL_MASK_SET_EVENT: + case PPM_IOCTL_MASK_UNSET_EVENT: + break; -bool check_scap_next_wait(scap_t* handle) -{ - uint32_t j; + default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "%s(%d) internal error", __FUNCTION__, op); + ASSERT(false); + return SCAP_FAILURE; + break; + } - for(j = 0; j < handle->m_ndevs; j++) + if(handle->m_bpf) { - uint32_t thead; - uint32_t ttail; - uint32_t read_size; - - get_buf_pointers(handle->m_devs[j].m_bufinfo, &thead, &ttail, &read_size); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "eventmask not supported on bpf"); + ASSERT(false); + return SCAP_FAILURE; + } + else + { + if(ioctl(handle->m_devs[0].m_fd, op, event_id)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, + "%s(%d) failed for event type %d", + __FUNCTION__, op, event_id); + ASSERT(false); + return SCAP_FAILURE; + } - if(read_size > 100000) { - return false; + uint32_t j; + + // + // Force a flush of the read buffers, so we don't capture events with the old snaplen + // + for(j = 0; j < handle->m_ndevs; j++) + { + scap_readbuf(handle, + j, + &handle->m_devs[j].m_sn_next_event, + &handle->m_devs[j].m_sn_len); + + handle->m_devs[j].m_sn_len = 0; + } } } - return true; + return SCAP_SUCCESS; +#endif // HAS_CAPTURE } +#endif // CYGWING_AGENT -#endif // _WIN32 +int32_t scap_clear_eventmask(scap_t* handle) { +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "eventmask not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + return(scap_handle_eventmask(handle, PPM_IOCTL_MASK_ZERO_EVENTS, 0)); +#endif +} -#ifndef _WIN32 -static inline int32_t scap_next_live(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +int32_t scap_set_eventmask(scap_t* handle, uint32_t event_id) { +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "eventmask not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; #else -static int32_t scap_next_live(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) + return(scap_handle_eventmask(handle, PPM_IOCTL_MASK_SET_EVENT, event_id)); #endif -{ -#if defined(_WIN32) || defined(__APPLE__) - // - // this should be prevented at open time - // - ASSERT(false); +} + +int32_t scap_unset_eventmask(scap_t* handle, uint32_t event_id) { +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "eventmask not supported on %s", PLATFORM_NAME); return SCAP_FAILURE; #else - uint32_t j; - uint64_t max_ts = 0xffffffffffffffff; - uint64_t max_buf_size = 0; - scap_evt* pe = NULL; - bool waited = false; + return(scap_handle_eventmask(handle, PPM_IOCTL_MASK_UNSET_EVENT, event_id)); +#endif +} - *pcpuid = 65535; +uint32_t scap_event_get_dump_flags(scap_t* handle) +{ + return handle->m_last_evt_dump_flags; +} - for(j = 0; j < handle->m_ndevs; j++) +int32_t scap_enable_dynamic_snaplen(scap_t* handle) +{ + // + // Not supported on files + // + if(handle->m_mode != SCAP_MODE_LIVE) { - if(handle->m_devs[j].m_sn_len == 0) - { - // - // The buffer for this CPU is fully consumed. - // This is a good time to check if we should wait - // - if(handle->m_emptybuf_timeout_ms != 0) - { - if(check_scap_next_wait(handle) && !waited) - { - usleep(BUFFER_EMPTY_WAIT_TIME_MS * 1000); - waited = true; - } - } - - // - // read another buffer - // - int32_t res = scap_readbuf(handle, - j, - false, - &handle->m_devs[j].m_sn_next_event, - &handle->m_devs[j].m_sn_len); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "setting snaplen not supported on this scap mode"); + return SCAP_FAILURE; + } - if(res != SCAP_SUCCESS) - { - return res; - } - } +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + // + // Tell the driver to change the snaplen + // + if(handle->m_bpf) + { + return scap_bpf_enable_dynamic_snaplen(handle); + } + if(handle->m_udig) + { // - // Make sure that we have data + // Not implemented for udig yet. // - if(handle->m_devs[j].m_sn_len != 0) + return SCAP_SUCCESS; + } + else + { + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_ENABLE_DYNAMIC_SNAPLEN)) { - if(handle->m_devs[j].m_sn_len > max_buf_size) - { - max_buf_size = handle->m_devs[j].m_sn_len; - } - - // - // We want to consume the event with the lowest timestamp - // - pe = (scap_evt*)handle->m_devs[j].m_sn_next_event; + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_enable_dynamic_snaplen failed"); + ASSERT(false); + return SCAP_FAILURE; + } + } -#ifdef _DEBUG - ASSERT(pe->len == scap_event_compute_len(pe)); + return SCAP_SUCCESS; #endif +} - if(pe->ts < max_ts) - { - if(pe->len > handle->m_devs[j].m_sn_len) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_next buffer corruption"); - - // - // if you get the following assertion, first recompile the driver and libscap - // - ASSERT(false); - return SCAP_FAILURE; - } - - *pevent = pe; - *pcpuid = j; - max_ts = pe->ts; - } - } +int32_t scap_disable_dynamic_snaplen(scap_t* handle) +{ + // + // Not supported on files + // + if(handle->m_mode != SCAP_MODE_LIVE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "setting snaplen not supported on this scap mode"); + return SCAP_FAILURE; } +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + // - // Check which buffer has been picked + // Tell the driver to change the snaplen // - if(*pcpuid != 65535) + if(handle->m_bpf) { - // - // Update the pointers. - // - ASSERT(handle->m_devs[*pcpuid].m_sn_len >= (*pevent)->len); - handle->m_devs[*pcpuid].m_sn_len -= (*pevent)->len; - handle->m_devs[*pcpuid].m_sn_next_event += (*pevent)->len; - - return SCAP_SUCCESS; + return scap_bpf_disable_dynamic_snaplen(handle); } else { - // - // This happens only when all the buffers are empty, which should be very rare. - // The caller want't receive an event, but shouldn't treat this as an error and should just retry. - // - return SCAP_TIMEOUT; + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_DISABLE_DYNAMIC_SNAPLEN)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_disable_dynamic_snaplen failed"); + ASSERT(false); + return SCAP_FAILURE; + } } + + return SCAP_SUCCESS; #endif } -int32_t scap_next(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid) +const char* scap_get_host_root() { - int32_t res; + char* p = getenv("SYSDIG_HOST_ROOT"); + static char env_str[SCAP_MAX_PATH_SIZE + 1]; + static bool inited = false; + if (! inited) { + strncpy(env_str, p ? p : "", SCAP_MAX_PATH_SIZE); + env_str[SCAP_MAX_PATH_SIZE] = '\0'; + inited = true; + } - if(handle->m_file) + return env_str; +} + +bool scap_alloc_proclist_info(scap_t* handle, uint32_t n_entries) +{ + uint32_t memsize; + + if(n_entries >= SCAP_DRIVER_PROCINFO_MAX_SIZE) { - res = scap_next_offline(handle, pevent, pcpuid); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "driver process list too big"); + return false; } - else + + memsize = sizeof(struct ppm_proclist_info) + + sizeof(struct ppm_proc_info) * n_entries; + + struct ppm_proclist_info *procinfo = (struct ppm_proclist_info*) realloc(handle->m_driver_procinfo, memsize); + if(procinfo == NULL) { - res = scap_next_live(handle, pevent, pcpuid); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "driver process list allocation error"); + return false; } - if(res == SCAP_SUCCESS) + if(handle->m_driver_procinfo == NULL) { - handle->m_evtcnt++; + procinfo->n_entries = 0; } - return res; -} + procinfo->max_entries = n_entries; + handle->m_driver_procinfo = procinfo; -// -// Return the process list for the given handle -// -scap_threadinfo* scap_get_proc_table(scap_t* handle) -{ - return handle->m_proclist; + return true; } -// -// Return the number of dropped events for the given handle -// -int32_t scap_get_stats(scap_t* handle, OUT scap_stats* stats) +struct ppm_proclist_info* scap_get_threadlist(scap_t* handle) { - uint32_t j; - - stats->n_evts = 0; - stats->n_drops = 0; - stats->n_preemptions = 0; - - for(j = 0; j < handle->m_ndevs; j++) + // + // Not supported on files + // + if(handle->m_mode != SCAP_MODE_LIVE) { - stats->n_evts += handle->m_devs[j].m_bufinfo->n_evts; - stats->n_drops += handle->m_devs[j].m_bufinfo->n_drops_buffer + - handle->m_devs[j].m_bufinfo->n_drops_pf; - stats->n_preemptions += handle->m_devs[j].m_bufinfo->n_preemptions; + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_get_threadlist not supported on this scap mode"); + return NULL; } - return SCAP_SUCCESS; -} - -// -// Stop capturing the events -// -int32_t scap_stop_capture(scap_t* handle) -{ -#ifdef _WIN32 - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on windows"); - return SCAP_FAILURE; -#elif defined(__APPLE__) - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on OSX"); - return SCAP_FAILURE; +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return NULL; #else - uint32_t j; - - // - // Not supported for files - // - if(handle->m_file) + if(handle->m_driver_procinfo == NULL) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "cannot stop offline captures"); - ASSERT(false); - return SCAP_FAILURE; + if(scap_alloc_proclist_info(handle, SCAP_DRIVER_PROCINFO_INITIAL_SIZE) == false) + { + return NULL; + } } - // - // Disable capture on all the rings - // - for(j = 0; j < handle->m_ndevs; j++) + if(handle->m_bpf) { - if(ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_DISABLE_CAPTURE)) + return scap_bpf_get_threadlist(handle); + } + else if(handle->m_udig) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_get_threadlist not supported on udig captures"); + return NULL; + } + else + { + int ioctlres = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_PROCLIST, handle->m_driver_procinfo); + if(ioctlres) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_stop_capture failed for device %" PRIu32, j); - ASSERT(false); - return SCAP_FAILURE; + if(errno == ENOSPC) + { + if(scap_alloc_proclist_info(handle, handle->m_driver_procinfo->n_entries + 256) == false) + { + return NULL; + } + else + { + return scap_get_threadlist(handle); + } + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Error calling PPM_IOCTL_GET_PROCLIST"); + return NULL; + } } } - // - // Since no new data is going to be produced, we disable read waits so that the remaining data - // can be consumd without slowdowns - // - handle->m_emptybuf_timeout_ms = 0; + return handle->m_driver_procinfo; +#endif // HAS_CAPTURE +} - return SCAP_SUCCESS; -#endif // _WIN32 +void scap_set_refresh_proc_table_when_saving(scap_t* handle, bool refresh) +{ + handle->refresh_proc_table_when_saving = refresh; } -// -// Start capturing the events -// -int32_t scap_start_capture(scap_t* handle) +uint64_t scap_get_unexpected_block_readsize(scap_t* handle) { -#ifdef _WIN32 - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture non supported on windows"); - return SCAP_FAILURE; -#elif defined(__APPLE__) - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture non supported on OSX"); - return SCAP_FAILURE; -#else - uint32_t j; + return handle->m_unexpected_block_readsize; +} +int32_t scap_enable_simpledriver_mode(scap_t* handle) +{ // - // Not supported for files + // Not supported on files // - if(handle->m_file) + if(handle->m_mode != SCAP_MODE_LIVE) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "cannot start offline captures"); - ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "setting simpledriver mode not supported on this scap mode"); return SCAP_FAILURE; } - // - // Enable capture on all the rings - // - for(j = 0; j < handle->m_ndevs; j++) +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + + if(handle->m_bpf) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "setting simpledriver mode not supported on bpf"); + ASSERT(false); + return SCAP_FAILURE; + } + else { - if(ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_ENABLE_CAPTURE)) + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_SET_SIMPLE_MODE)) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_start_capture failed for device %" PRIu32, j); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_enable_simpledriver_mode failed"); ASSERT(false); return SCAP_FAILURE; } } return SCAP_SUCCESS; -#endif // _WIN32 +#endif } -#if !defined (_WIN32) && !defined(__APPLE__) -static int32_t scap_set_dropping_mode(scap_t* handle, int request, uint32_t sampling_ratio) +int32_t scap_get_n_tracepoint_hit(scap_t* handle, long* ret) { - // - // Not supported for files + int ioctl_ret = 0; + // - if(handle->m_file) + // Not supported on files + // + if(handle->m_mode != SCAP_MODE_LIVE) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "dropping mode not supported on offline captures"); - ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "getting n_tracepoint_hit not supported on this scap mode"); return SCAP_FAILURE; } - if(handle->m_ndevs) +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + + if(handle->m_bpf) { - if(ioctl(handle->m_devs[0].m_fd, request, sampling_ratio)) + return scap_bpf_get_n_tracepoint_hit(handle, ret); + } + else if(handle->m_udig) + { + return SCAP_NOT_SUPPORTED; + } + else + { + ioctl_ret = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_N_TRACEPOINT_HIT, ret); + if(ioctl_ret != 0) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "%s failed", __FUNCTION__); + if(errno == ENOTTY) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_get_n_tracepoint_hit failed, ioctl not supported"); + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_get_n_tracepoint_hit failed (%s)", scap_strerror(handle, errno)); + } + ASSERT(false); return SCAP_FAILURE; - } + } } return SCAP_SUCCESS; -} #endif +} -int32_t scap_stop_dropping_mode(scap_t* handle) +#ifdef CYGWING_AGENT +wh_t* scap_get_wmi_handle(scap_t* handle) { -#ifdef _WIN32 - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on windows"); - return SCAP_FAILURE; -#elif defined(__APPLE__) - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on OSX"); - return SCAP_FAILURE; -#else - return scap_set_dropping_mode(handle, PPM_IOCTL_DISABLE_DROPPING_MODE, 0); + return handle->m_whh; +} #endif + +const char *scap_get_bpf_probe_from_env() +{ + return getenv(SYSDIG_BPF_PROBE_ENV); } -int32_t scap_start_dropping_mode(scap_t* handle, uint32_t sampling_ratio) +bool scap_get_bpf_enabled(scap_t *handle) { -#ifdef _WIN32 - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on windows"); - return SCAP_FAILURE; -#elif defined(__APPLE__) - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on OSX"); - return SCAP_FAILURE; -#else - return scap_set_dropping_mode(handle, PPM_IOCTL_ENABLE_DROPPING_MODE, sampling_ratio); -#endif + if(handle) + { + return handle->m_bpf; + } + + return false; } -// -// Return the list of device addresses -// -scap_addrlist* scap_get_ifaddr_list(scap_t* handle) +int32_t scap_suppress_events_comm(scap_t *handle, const char *comm) { - return handle->m_addrlist; + // If the comm is already present in the list, do nothing + uint32_t i; + for(i=0; im_num_suppressed_comms; i++) + { + if(strcmp(handle->m_suppressed_comms[i], comm) == 0) + { + return SCAP_SUCCESS; + } + } + + if(handle->m_num_suppressed_comms >= SCAP_MAX_SUPPRESSED_COMMS) + { + return SCAP_FAILURE; + } + + handle->m_num_suppressed_comms++; + handle->m_suppressed_comms = (char **) realloc(handle->m_suppressed_comms, + handle->m_num_suppressed_comms * sizeof(char *)); + + handle->m_suppressed_comms[handle->m_num_suppressed_comms-1] = strdup(comm); + + return SCAP_SUCCESS; } -// -// Return the list of machine users -// -scap_userlist* scap_get_user_list(scap_t* handle) +bool scap_check_suppressed_tid(scap_t *handle, int64_t tid) { - return handle->m_userlist; + scap_tid *stid; + HASH_FIND_INT64(handle->m_suppressed_tids, &tid, stid); + + return (stid != NULL); } -// -// Get the machine information -// -const scap_machine_info* scap_get_machine_info(scap_t* handle) +int32_t scap_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end) { - if(handle->m_machine_info.num_cpus != (uint32_t)-1) + // + // Not supported on files + // + if(handle->m_mode != SCAP_MODE_LIVE) { - return (const scap_machine_info*)&handle->m_machine_info; + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_set_fullcapture_port_range not supported on this scap mode"); + return SCAP_FAILURE; + } + +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on %s", PLATFORM_NAME); + return SCAP_FAILURE; +#else + + if(handle->m_bpf) + { + return scap_bpf_set_fullcapture_port_range(handle, range_start, range_end); } else { // - // Reading from a file with no process info block + // Encode the port range // - return NULL; + uint32_t arg = (range_end << 16) + range_start; + + // + // Beam the value down to the module + // + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_SET_FULLCAPTURE_PORT_RANGE, arg)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_set_fullcapture_port_range failed"); + ASSERT(false); + return SCAP_FAILURE; + } + + { + uint32_t j; + + // + // Force a flush of the read buffers, so we don't capture events with the old snaplen + // + for(j = 0; j < handle->m_ndevs; j++) + { + scap_readbuf(handle, + j, + &handle->m_devs[j].m_sn_next_event, + &handle->m_devs[j].m_sn_len); + + handle->m_devs[j].m_sn_len = 0; + } + } } + + return SCAP_SUCCESS; +#endif } -int32_t scap_set_snaplen(scap_t* handle, uint32_t snaplen) +int32_t scap_set_statsd_port(scap_t* const handle, const uint16_t port) { // // Not supported on files // - if(handle->m_file) + if(handle->m_mode != SCAP_MODE_LIVE) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "setting snaplen not supported on offline captures"); + snprintf(handle->m_lasterr, + SCAP_LASTERR_SIZE, + "scap_set_statsd_port not supported on this scap mode"); return SCAP_FAILURE; } -#ifdef _WIN32 - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on windows"); - return SCAP_FAILURE; -#elif defined(__APPLE__) - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "live capture not supported on OSX"); +#if !defined(HAS_CAPTURE) || defined(CYGWING_AGENT) + snprintf(handle->m_lasterr, + SCAP_LASTERR_SIZE, + "live capture not supported on %s", + PLATFORM_NAME); return SCAP_FAILURE; #else - // - // Tell the driver to change the snaplen - // - if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_SET_SNAPLEN, snaplen)) + + if(handle->m_bpf) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_set_snaplen failed"); - ASSERT(false); - return SCAP_FAILURE; + return scap_bpf_set_statsd_port(handle, port); } - + else { - uint32_t j; - // - // Force a flush of the read buffers, so we don't capture events with the old snaplen + // Beam the value down to the module // - for(j = 0; j < handle->m_ndevs; j++) + if(ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_SET_STATSD_PORT, port)) + { + snprintf(handle->m_lasterr, + SCAP_LASTERR_SIZE, + "scap_set_statsd_port: ioctl failed"); + ASSERT(false); + return SCAP_FAILURE; + } + { - scap_readbuf(handle, - j, - false, - &handle->m_devs[j].m_sn_next_event, - &handle->m_devs[j].m_sn_len); + uint32_t j; + + // + // Force a flush of the read buffers, so we don't + // capture events with the old snaplen + // + for(j = 0; j < handle->m_ndevs; j++) + { + scap_readbuf(handle, + j, + &handle->m_devs[j].m_sn_next_event, + &handle->m_devs[j].m_sn_len); - handle->m_devs[j].m_sn_len = 0; + handle->m_devs[j].m_sn_len = 0; + } } } return SCAP_SUCCESS; #endif } - diff --git a/userspace/libscap/scap.def b/userspace/libscap/scap.def index f594810372..d11b73da96 100644 --- a/userspace/libscap/scap.def +++ b/userspace/libscap/scap.def @@ -3,17 +3,24 @@ LIBRARY scap EXPORTS scap_open_live scap_open_offline + scap_open_offline_fd + scap_open scap_close scap_get_os_platform scap_get_ndevs scap_getlasterr + scap_max_buf_used scap_next scap_event_getlen scap_event_get_ts scap_dump_open + scap_dump_open_fd scap_dump_close + scap_dump_get_offset + scap_dump_flush scap_dump_ftell scap_dump + scap_event_reset_count scap_event_get_num scap_get_proc_table scap_event_getinfo @@ -31,4 +38,17 @@ EXPORTS scap_get_user_list scap_free_userlist scap_set_snaplen - + scap_get_readfile_offset + scap_clear_eventmask + scap_set_eventmask + scap_unset_eventmask + scap_number_of_bytes_to_write + scap_event_get_dump_flags + scap_enable_dynamic_snaplen + scap_disable_dynamic_snaplen + scap_proc_free_table + scap_is_thread_alive + scap_get_threadlist + scap_get_host_root + scap_ftell + scap_fseek diff --git a/userspace/libscap/scap.h b/userspace/libscap/scap.h index ce943bf727..802f8d0f1e 100644 --- a/userspace/libscap/scap.h +++ b/userspace/libscap/scap.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once @@ -24,7 +25,7 @@ extern "C" { /*! \mainpage libscap documentation - + \section Introduction libscap is the low-level sysdig component that exports the following functionality: @@ -32,7 +33,7 @@ extern "C" { - trace file management - event retrieval - extraction of system state from /proc - + This manual includes the following sections: - \ref scap_defs - \ref scap_functs @@ -46,9 +47,20 @@ extern "C" { * @{ */ +// +// Forward declarations +// +typedef struct scap scap_t; +typedef struct ppm_evt_hdr scap_evt; + +struct iovec; + // // Core types // +#ifndef __APPLE__ +#include +#endif #include "uthash.h" #include "../common/sysdig_types.h" #include "../../driver/ppm_events_public.h" @@ -63,6 +75,9 @@ extern "C" { #define SCAP_NOTFOUND 4 #define SCAP_INPUT_TOO_SMALL 5 #define SCAP_EOF 6 +#define SCAP_UNEXPECTED_BLOCK 7 +#define SCAP_VERSION_MISMATCH 8 +#define SCAP_NOT_SUPPORTED 9 // // Last error string size for scap_open_live() @@ -70,13 +85,18 @@ extern "C" { #define SCAP_LASTERR_SIZE 256 /*! - \brief Statisitcs about an in progress capture + \brief Statistics about an in progress capture */ typedef struct scap_stats { uint64_t n_evts; ///< Total number of events that were received by the driver. uint64_t n_drops; ///< Number of dropped events. + uint64_t n_drops_buffer; ///< Number of dropped events caused by full buffer. + uint64_t n_drops_pf; ///< Number of dropped events caused by invalid memory access. + uint64_t n_drops_bug; ///< Number of dropped events caused by an invalid condition in the kernel instrumentation. uint64_t n_preemptions; ///< Number of preemptions. + uint64_t n_suppressed; ///< Number of events skipped due to the tid being in a set of suppressed tids + uint64_t n_tids_suppressed; ///< Number of threads currently being suppressed }scap_stats; /*! @@ -91,6 +111,10 @@ typedef struct evt_param_info }evt_param_info; #define SCAP_MAX_PATH_SIZE 1024 +#define SCAP_MAX_ARGS_SIZE 4096 +#define SCAP_MAX_ENV_SIZE 4096 +#define SCAP_MAX_CGROUPS_SIZE 4096 +#define SCAP_MAX_SUPPRESSED_COMMS 32 /*! \brief File Descriptor type @@ -112,7 +136,9 @@ typedef enum scap_fd_type SCAP_FD_SIGNALFD = 11, SCAP_FD_EVENTPOLL = 12, SCAP_FD_INOTIFY = 13, - SCAP_FD_TIMERFD = 14 + SCAP_FD_TIMERFD = 14, + SCAP_FD_NETLINK = 15, + SCAP_FD_FILE_V2 = 16 }scap_fd_type; /*! @@ -172,6 +198,13 @@ typedef struct scap_fdinfo uint64_t destination; ///< Destination socket endpoint char fname[SCAP_MAX_PATH_SIZE]; ///< Name associated to this unix socket } unix_socket_info; ///< Information specific to unix sockets + struct + { + uint32_t open_flags; ///< Flags associated with the file + char fname[SCAP_MAX_PATH_SIZE]; ///< Name associated to this file + uint32_t mount_id; ///< The id of the vfs mount the file is in until we find dev major:minor + uint32_t dev; ///< Major/minor number of the device containing this file + } regularinfo; ///< Information specific to regular files char fname[SCAP_MAX_PATH_SIZE]; ///< The name for file system FDs }info; UT_hash_handle hh; ///< makes this structure hashable @@ -185,25 +218,104 @@ typedef struct scap_threadinfo uint64_t tid; ///< The thread/task id. uint64_t pid; ///< The id of the process containing this thread. In single thread processes, this is equal to tid. uint64_t ptid; ///< The id of the thread that created this thread. - char comm[SCAP_MAX_PATH_SIZE]; ///< Command name (e.g. "top") - char exe[SCAP_MAX_PATH_SIZE]; ///< Full command name (e.g. "/bin/top") - char args[SCAP_MAX_PATH_SIZE]; ///< Command line arguments (e.g. "-d1") + uint64_t sid; ///< The session id of the process containing this thread. + uint64_t vpgid; ///< The process group of this thread, as seen from its current pid namespace + char comm[SCAP_MAX_PATH_SIZE+1]; ///< Command name (e.g. "top") + char exe[SCAP_MAX_PATH_SIZE+1]; ///< argv[0] (e.g. "sshd: user@pts/4") + char exepath[SCAP_MAX_PATH_SIZE+1]; ///< full executable path + char args[SCAP_MAX_ARGS_SIZE+1]; ///< Command line arguments (e.g. "-d1") uint16_t args_len; ///< Command line arguments length - char cwd[SCAP_MAX_PATH_SIZE]; ///< The current working directory + char env[SCAP_MAX_ENV_SIZE+1]; ///< Environment + uint16_t env_len; ///< Environment length + char cwd[SCAP_MAX_PATH_SIZE+1]; ///< The current working directory int64_t fdlimit; ///< The maximum number of files this thread is allowed to open uint32_t flags; ///< the process flags. uint32_t uid; ///< user id uint32_t gid; ///< group id + uint32_t vmsize_kb; ///< total virtual memory (as kb) + uint32_t vmrss_kb; ///< resident non-swapped memory (as kb) + uint32_t vmswap_kb; ///< swapped memory (as kb) + uint64_t pfmajor; ///< number of major page faults since start + uint64_t pfminor; ///< number of minor page faults since start + int64_t vtid; + int64_t vpid; + char cgroups[SCAP_MAX_CGROUPS_SIZE]; + uint16_t cgroups_len; + char root[SCAP_MAX_PATH_SIZE+1]; + int filtered_out; ///< nonzero if this entry should not be saved to file scap_fdinfo* fdlist; ///< The fd table for this process + uint64_t clone_ts; + int32_t tty; + int32_t loginuid; ///< loginuid (auid) + UT_hash_handle hh; ///< makes this structure hashable }scap_threadinfo; +/*! + \brief Mount information +*/ +typedef struct { + uint64_t mount_id; ///< mount id from /proc/self/mountinfo + uint32_t dev; ///< device number + UT_hash_handle hh; ///< makes this structure hashable +} scap_mountinfo; + +typedef void (*proc_entry_callback)(void* context, + scap_t* handle, + int64_t tid, + scap_threadinfo* tinfo, + scap_fdinfo* fdinfo); + +/*! + \brief Arguments for scap_open +*/ +typedef enum { + /*! + * Default value that mostly exists so that sinsp can have a valid value + * before it is initialized. + */ + SCAP_MODE_NONE = 0, + /*! + * Read system call data from a capture file. + */ + SCAP_MODE_CAPTURE, + /*! + * Read system call data from the underlying operating system. + */ + SCAP_MODE_LIVE, + /*! + * Do not read system call data. If next is called, a dummy event is + * returned. + */ + SCAP_MODE_NODRIVER, +} scap_mode_t; + +typedef struct scap_open_args +{ + scap_mode_t mode; + int fd; // If non-zero, will be used instead of fname. + const char* fname; ///< The name of the file to open. NULL for live captures. + proc_entry_callback proc_callback; ///< Callback to be invoked for each thread/fd that is extracted from /proc, or NULL if no callback is needed. + void* proc_callback_context; ///< Opaque pointer that will be included in the calls to proc_callback. Ignored if proc_callback is NULL. + bool import_users; ///< true if the user list should be created when opening the capture. + uint64_t start_offset; ///< Used to start reading a capture file from an arbitrary offset. This is leveraged when opening merged files. + const char *bpf_probe; ///< The name of the BPF probe to open. If NULL, the kernel driver will be used. + const char *suppressed_comms[SCAP_MAX_SUPPRESSED_COMMS]; ///< A list of processes (comm) for which no + // events should be returned, with a trailing NULL value. + // You can provide additional comm + // values via scap_suppress_events_comm(). + bool udig; ///< If true, UDIG will be used for event capture. Otherwise, the kernel driver will be used. +}scap_open_args; + + // -// The follwing stuff is byte aligned because we save it to disk. +// The following stuff is byte aligned because we save it to disk. // #if defined _MSC_VER #pragma pack(push) #pragma pack(1) +#elif defined __sun +#pragma pack(1) #else #pragma pack(push, 1) #endif @@ -216,11 +328,11 @@ typedef struct _scap_machine_info uint32_t num_cpus; ///< Number of processors uint64_t memory_size_bytes; ///< Physical memory size uint64_t max_pid; ///< Highest PID number on this machine - char hostname[128]; ///< The machine hostname - uint64_t reserved1; ///< reserved for fututre use - uint64_t reserved2; ///< reserved for fututre use - uint64_t reserved3; ///< reserved for fututre use - uint64_t reserved4; ///< reserved for fututre use + char hostname[128]; ///< The machine hostname + uint64_t reserved1; ///< reserved for future use + uint64_t reserved2; ///< reserved for future use + uint64_t reserved3; ///< reserved for future use + uint64_t reserved4; ///< reserved for future use }scap_machine_info; @@ -243,6 +355,7 @@ typedef enum scap_ifinfo_type */ typedef struct scap_ifinfo_ipv4 { + // NB: new fields must be appended uint16_t type; ///< Interface type uint16_t ifnamelen; uint32_t addr; ///< Interface address @@ -253,7 +366,7 @@ typedef struct scap_ifinfo_ipv4 }scap_ifinfo_ipv4; /*! - \brief For backword compatibility only + \brief For backward compatibility only */ typedef struct scap_ifinfo_ipv4_nolinkspeed { @@ -270,6 +383,7 @@ typedef struct scap_ifinfo_ipv4_nolinkspeed */ typedef struct scap_ifinfo_ipv6 { + // NB: new fields must be appended uint16_t type; uint16_t ifnamelen; char addr[SCAP_IPV6_ADDR_LEN]; ///< Interface address @@ -292,7 +406,11 @@ typedef struct scap_ifinfo_ipv6_nolinkspeed char ifname[SCAP_MAX_PATH_SIZE]; }scap_ifinfo_ipv6_nolinkspeed; +#if defined __sun +#pragma pack() +#else #pragma pack(pop) +#endif /*! \brief List of the machine network interfaces @@ -350,14 +468,14 @@ typedef struct scap_userlist /*! \brief The OS on which the capture was made */ -typedef enum scap_os_patform +typedef enum scap_os_platform { SCAP_PFORM_UNKNOWN = 0, SCAP_PFORM_LINUX_I386 = 1, SCAP_PFORM_LINUX_X64 = 2, SCAP_PFORM_WINDOWS_I386 = 3, SCAP_PFORM_WINDOWS_X64 = 4, -}scap_os_patform; +}scap_os_platform; /*! \brief Indicates if an event is an enter one or an exit one @@ -368,7 +486,37 @@ typedef enum event_direction SCAP_ED_OUT = 1 }event_direction; +/*! + \brief Indicates the compression type used when writing a tracefile +*/ +typedef enum compression_mode +{ + SCAP_COMPRESSION_NONE = 0, + SCAP_COMPRESSION_GZIP = 1 +}compression_mode; + +/*! + \brief Flags for scap_dump +*/ +typedef enum scap_dump_flags +{ + SCAP_DF_NONE = 0, + SCAP_DF_STATE_ONLY = 1, ///< The event should be used for state update but it should + ///< not be shown to the user + SCAP_DF_TRACER = (1 << 1) ///< This event is a tracer +}scap_dump_flags; + typedef struct scap_dumper scap_dumper_t; + +/*! + \brief System call description struct. +*/ +struct ppm_syscall_desc { + enum ppm_event_category category; /**< System call category. */ + enum ppm_event_flags flags; + char *name; /**< System call name, e.g. 'open'. */ +}; + /*@}*/ /////////////////////////////////////////////////////////////////////////////// @@ -379,10 +527,32 @@ typedef struct scap_dumper scap_dumper_t; #define OUT // -// Forward declarations +// udig stuff // -typedef struct scap scap_t; -typedef struct ppm_evt_hdr scap_evt; +#define UDIG_RING_SM_FNAME "udig_buf" +#define UDIG_RING_DESCS_SM_FNAME "udig_descs" +#define UDIG_RING_SIZE (8 * 1024 * 1024) + +#ifndef __APPLE__ +struct udig_ring_buffer_status { + volatile uint64_t m_buffer_lock; + volatile int m_initialized; + volatile int m_capturing_pid; + volatile int m_stopped; + volatile struct timespec m_last_print_time; + struct udig_consumer_t m_consumer; +}; +#endif // __APPLE__ + +typedef struct ppm_ring_buffer_info ppm_ring_buffer_info; + +int32_t udig_alloc_ring(int* ring_fd, uint8_t** ring, uint32_t *ringsize, char *error); +int32_t udig_alloc_ring_descriptors(int* ring_descs_fd, + struct ppm_ring_buffer_info** ring_info, + struct udig_ring_buffer_status** ring_status, + char *error); +void udig_free_ring(uint8_t* addr, uint32_t size); +void udig_free_ring_descriptors(uint8_t* addr); /////////////////////////////////////////////////////////////////////////////// // API functions @@ -397,10 +567,12 @@ typedef struct ppm_evt_hdr scap_evt; \param error Pointer to a buffer that will contain the error string in case the function fails. The buffer must have size SCAP_LASTERR_SIZE. - + \param rc Integer pointer that will contain the scap return code in case the + function fails. + \return The capture instance handle in case of success. NULL in case of failure. */ -scap_t* scap_open_live(char *error); +scap_t* scap_open_live(char *error, int32_t *rc); /*! \brief Start an event capture from file. @@ -408,10 +580,38 @@ scap_t* scap_open_live(char *error); \param fname The name of the file to open. \param error Pointer to a buffer that will contain the error string in case the function fails. The buffer must have size SCAP_LASTERR_SIZE. - + \param rc Integer pointer that will contain the scap return code in case the + function fails. + + \return The capture instance handle in case of success. NULL in case of failure. +*/ +scap_t* scap_open_offline(const char* fname, char *error, int32_t *rc); + +/*! + \brief Start an event capture from an already opened file descriptor. + + \param fd The fd to use. + \param error Pointer to a buffer that will contain the error string in case the + function fails. The buffer must have size SCAP_LASTERR_SIZE. + \param rc Integer pointer that will contain the scap return code in case the + function fails. + \return The capture instance handle in case of success. NULL in case of failure. */ -scap_t* scap_open_offline(char* fname, char *error); +scap_t* scap_open_offline_fd(int fd, char *error, int32_t *rc); + +/*! + \brief Advanced function to start a capture. + + \param args a \ref scap_open_args structure containing the open parameters. + \param error Pointer to a buffer that will contain the error string in case the + function fails. The buffer must have size SCAP_LASTERR_SIZE. + \param rc Integer pointer that will contain the scap return code in case the + function fails. + + \return The capture instance handle in case of success. NULL in case of failure. +*/ +scap_t* scap_open(scap_open_args args, char *error, int32_t *rc); /*! \brief Close a capture handle. @@ -424,19 +624,24 @@ void scap_close(scap_t* handle); \brief Retrieve the OS platform for the given capture handle. \param handle Handle to the capture instance. - + \return The type of operating system on which the capture was made. - \note For live handles, the return value indicates the current local OS. - For offline handles, the return value indicates the OS where the data was + \note For live handles, the return value indicates the current local OS. + For offline handles, the return value indicates the OS where the data was originally captured. */ -scap_os_patform scap_get_os_platform(scap_t* handle); +scap_os_platform scap_get_os_platform(scap_t* handle); /*! \brief Return a string with the last error that happened on the given capture. */ -char* scap_getlasterr(scap_t* handle); +const char* scap_getlasterr(scap_t* handle); + +/*! + * \brief returns the maximum amount of memory used by any driver queue + */ +uint64_t scap_max_buf_used(scap_t* handle); /*! \brief Get the next event from the from the given capture instance @@ -445,11 +650,11 @@ char* scap_getlasterr(scap_t* handle); \param pevent User-provided event pointer that will be initialized with address of the event. \param pcpuid User-provided event pointer that will be initialized with the ID if the CPU where the event was captured. - - \return SCAP_SUCCESS if the call is succesful and pevent and pcpuid contain valid data. + + \return SCAP_SUCCESS if the call is successful and pevent and pcpuid contain valid data. SCAP_TIMEOUT in case the read timeout expired and no event is available. SCAP_EOF when the end of an offline capture is reached. - On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain the cause of the error. + On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain the cause of the error. */ int32_t scap_next(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid); @@ -457,8 +662,8 @@ int32_t scap_next(scap_t* handle, OUT scap_evt** pevent, OUT uint16_t* pcpuid); \brief Get the length of an event \param e pointer to an event returned by \ref scap_next. - - \return The event length in bytes. + + \return The event length in bytes. */ uint32_t scap_event_getlen(scap_evt* e); @@ -466,8 +671,8 @@ uint32_t scap_event_getlen(scap_evt* e); \brief Get the timestamp of an event \param e pointer to an event returned by \ref scap_next. - - \return The event timestamp, in nanoseconds since epoch. + + \return The event timestamp, in nanoseconds since epoch. */ uint64_t scap_event_get_ts(scap_evt* e); @@ -476,69 +681,133 @@ uint64_t scap_event_get_ts(scap_evt* e); instance \param handle Handle to the capture instance. - - \return The total number of events. + + \return The total number of events. */ uint64_t scap_event_get_num(scap_t* handle); /*! - \brief Return the meta-information describing the given event + \brief Reset the event count to 0. + + \param handle Handle to the capture instance. +*/ +void scap_event_reset_count(scap_t* handle); + +/*! + \brief Return the meta-information describing the given event \param e pointer to an event returned by \ref scap_next. - - \return The pointer to the the event table entry for the given event. + + \return The pointer to the the event table entry for the given event. */ const struct ppm_event_info* scap_event_getinfo(scap_evt* e); /*! - \brief Open a tracefile for writing + \brief Return the dump flags for the last event received from this handle \param handle Handle to the capture instance. - \param fname The name of the tracefile. - \return Dump handle that can be used to identify this specific dump instance. + \return The flags if the capture is offline, 0 if the capture is live. +*/ +uint32_t scap_event_get_dump_flags(scap_t* handle); + +/*! + \brief Return the current offset in the file opened by scap_open_offline(), + or -1 if this is a live capture. + + \param handle Handle to the capture instance. */ -scap_dumper_t* scap_dump_open(scap_t *handle, const char *fname); +int64_t scap_get_readfile_offset(scap_t* handle); /*! - \brief Close a tracefile. + \brief Open a trace file for writing + + \param handle Handle to the capture instance. + \param fname The name of the trace file. + + \return Dump handle that can be used to identify this specific dump instance. +*/ +scap_dumper_t* scap_dump_open(scap_t *handle, const char *fname, compression_mode compress, bool skip_proc_scan); + +/*! + \brief Open a trace file for writing, using the provided fd. + + \param handle Handle to the capture instance. + \param fd A file descriptor to which the dumper will write + + \return Dump handle that can be used to identify this specific dump instance. +*/ +scap_dumper_t* scap_dump_open_fd(scap_t *handle, int fd, compression_mode compress, bool skip_proc_scan); + +/*! + \brief Close a trace file. \param d The dump handle, returned by \ref scap_dump_open */ void scap_dump_close(scap_dumper_t *d); /*! - \brief Return the current size of a tracefile. + \brief Return the current size of a trace file. \param d The dump handle, returned by \ref scap_dump_open - \return The current size of the dump file pointed by d. + \return The current size of the dump file pointed by d. */ -uint64_t scap_dump_ftell(scap_dumper_t *d); +int64_t scap_dump_get_offset(scap_dumper_t *d); /*! - \brief Write an event to a trace file + \brief Return the position for the next write to a trace file. + This uses gztell, while scap_dump_get_offset uses gzoffset. + + \param d The dump handle, returned by \ref scap_dump_open + \return The next write position. +*/ +int64_t scap_dump_ftell(scap_dumper_t *d); + +/*! + \brief Flush all pending output into the file. + + \param d The dump handle, returned by \ref scap_dump_open +*/ +void scap_dump_flush(scap_dumper_t *d); + +/*! + \brief Tell how many bytes would be written (a dry run of scap_dump) + + \param e pointer to an event returned by \ref scap_next. + \param cpuid The cpu from which the event was captured. Returned by \ref scap_next. + \param bytes The number of bytes to write + + \return SCAP_SUCCESS if the call is successful. + On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain + the cause of the error. +*/ +int32_t scap_number_of_bytes_to_write(scap_evt *e, uint16_t cpuid, int32_t* bytes); + +/*! + \brief Write an event to a trace file \param handle Handle to the capture instance. \param d The dump handle, returned by \ref scap_dump_open \param e pointer to an event returned by \ref scap_next. \param cpuid The cpu from which the event was captured. Returned by \ref scap_next. - - \return SCAP_SUCCESS if the call is succesful. - On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain - the cause of the error. + \param flags The event flags. 0 means no flags. + + \return SCAP_SUCCESS if the call is successful. + On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain + the cause of the error. */ -int32_t scap_dump(scap_t *handle, scap_dumper_t *d, scap_evt* e, uint16_t cpuid); +int32_t scap_dump(scap_t *handle, scap_dumper_t *d, scap_evt* e, uint16_t cpuid, uint32_t flags); /*! \brief Get the process list for the given capture instance \param handle Handle to the capture instance. - + \return Pointer to the process list. - for live captures, the process list is created when the capture starts by scanning the + for live captures, the process list is created when the capture starts by scanning the proc file system. For offline captures, it is retrieved from the file. - The process list contains information about the processes that were already open when + The process list contains information about the processes that were already open when the capture started. It can be traversed with uthash, using the following syntax: \code @@ -558,15 +827,15 @@ int32_t scap_dump(scap_t *handle, scap_dumper_t *d, scap_evt* e, uint16_t cpuid) scap_threadinfo* scap_get_proc_table(scap_t* handle); /*! - \brief Return the capture statistics for the given capture handle. + \brief Return the capture statistics for the given capture handle. \param handle Handle to the capture instance. \param stats Pointer to a \ref scap_stats structure that will be filled with the statistics. - - \return SCAP_SECCESS if the call is succesful. - On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain - the cause of the error. + + \return SCAP_SECCESS if the call is successful. + On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain + the cause of the error. */ int32_t scap_get_stats(scap_t* handle, OUT scap_stats* stats); @@ -574,10 +843,10 @@ int32_t scap_get_stats(scap_t* handle, OUT scap_stats* stats); \brief This function can be used to temporarily interrupt event capture. \param handle Handle to the capture that will be stopped. - - \return SCAP_SUCCESS if the call is succesful. - On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain - the cause of the error. + + \return SCAP_SUCCESS if the call is successful. + On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain + the cause of the error. */ int32_t scap_stop_capture(scap_t* handle); @@ -586,9 +855,9 @@ int32_t scap_stop_capture(scap_t* handle); \param handle Handle to the capture that will be started. - \return SCAP_SUCCESS if the call is succesful. - On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain - the cause of the error. + \return SCAP_SUCCESS if the call is successful. + On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain + the cause of the error. */ int32_t scap_start_capture(scap_t* handle); @@ -614,22 +883,8 @@ scap_addrlist* scap_get_ifaddr_list(scap_t* handle); scap_userlist* scap_get_user_list(scap_t* handle); /*! - \brief This function can be used to specify the time after which \ref scap_next - returns when no events are available. - - \param handle Handle to the capture instance. - \param handle The number of milliseconds after which scap_next will return - SCAP_TIMEOUT. Use 0 if you want to the scap_next to always return immediately. - - \return SCAP_SUCCESS if the call is succesful. - On Failure, SCAP_FAILURE is returned and scap_getlasterr() can be used to obtain - the cause of the error. -*/ -int32_t scap_set_empty_buffer_timeout_ms(scap_t* handle, uint32_t timeout_ms); - -/*! - \brief Retrieve the table with the description of every event type that - the capture driver supports. + \brief Retrieve the table with the description of every event type that + the capture driver supports. \return The pointer to a table of \ref scap_userlist entries, each of which describes one of the events that can come from the driver. The table contains PPM_EVENT_MAX entries, @@ -640,8 +895,8 @@ int32_t scap_set_empty_buffer_timeout_ms(scap_t* handle, uint32_t timeout_ms); const struct ppm_event_info* scap_get_event_info_table(); /*! - \brief Retrieve the table with the description of system call that - the capture driver supports. + \brief Retrieve the table with the description of system call that + the capture driver supports. \return The pointer to a table of \ref ppm_syscall_desc entries, each of which describes one of the events that can come from the driver. The table contains SYSCALL_TABLE_SIZE entries, @@ -662,7 +917,7 @@ const struct ppm_syscall_desc* scap_get_syscall_info_table(); const scap_machine_info* scap_get_machine_info(scap_t* handle); /*! - \brief Set the capture snaplen, i.e. the maximum size an event parameter can + \brief Set the capture snaplen, i.e. the maximum size an event parameter can reach before the driver starts truncating it. \param handle Handle to the capture instance. @@ -670,7 +925,7 @@ const scap_machine_info* scap_get_machine_info(scap_t* handle); \note This function can only be called for live captures. \note By default, the driver captures the first 80 bytes of the buffers coming from - events like read, write, send, recv, etc. + events like read, write, send, recv, etc. If you're not interested in payloads, smaller values will save capture buffer space and make capture files smaller. Conversely, big values should be used with care because they can easily generate huge @@ -678,6 +933,73 @@ const scap_machine_info* scap_get_machine_info(scap_t* handle); */ int32_t scap_set_snaplen(scap_t* handle, uint32_t snaplen); +/*! + \brief Clear the event mask: no events will be passed to sysdig + + \param handle Handle to the capture instance. + + \note This function can only be called for live captures. +*/ +int32_t scap_clear_eventmask(scap_t* handle); + +/*! + \brief Set the event into the eventmask so that + sysdig-based apps can receive the event. Useful for offloading + operations such as evt.type=open + + \param handle Handle to the capture instance. + \param event id (example PPME_SOCKET_BIND_X) + \note This function can only be called for live captures. +*/ +int32_t scap_set_eventmask(scap_t* handle, uint32_t event_id); + + +/*! + \brief Unset the event into the eventmask so that + sysdig-based apps can no longer receive the event. It is + the opposite of scap_set_eventmask + + \param handle Handle to the capture instance. + \param event id (example PPME_SOCKET_BIND_X) + \note This function can only be called for live captures. +*/ +int32_t scap_unset_eventmask(scap_t* handle, uint32_t event_id); + + +/*! + \brief Get the root directory of the system. This usually changes + if sysdig runs in a container, so that all the information for the + host can be correctly extracted. +*/ +const char* scap_get_host_root(); + +/*! + \brief Get the process list. +*/ +struct ppm_proclist_info* scap_get_threadlist(scap_t* handle); + +const char *scap_get_bpf_probe_from_env(); + +bool scap_get_bpf_enabled(scap_t* handle); + +/*! + \brief stop returning events for all subsequently spawned + processes with the provided comm, as well as their children. + This includes fork()/clone()ed processes that might later + exec to a different comm. + + returns SCAP_FAILURE if there are already MAX_SUPPRESSED_COMMS comm + values, SCAP_SUCCESS otherwise. +*/ + +int32_t scap_suppress_events_comm(scap_t* handle, const char *comm); + +/*! + \brief return whether the provided tid is currently being suppressed. +*/ + +bool scap_check_suppressed_tid(scap_t *handle, int64_t tid); + /*@}*/ /////////////////////////////////////////////////////////////////////////////// @@ -691,7 +1013,7 @@ int32_t scap_set_snaplen(scap_t* handle, uint32_t snaplen); uint32_t scap_get_ndevs(scap_t* handle); // Retrieve a buffer of events from one of the cpus -extern int32_t scap_readbuf(scap_t* handle, uint32_t cpuid, bool blocking, OUT char** buf, OUT uint32_t* len); +extern int32_t scap_readbuf(scap_t* handle, uint32_t cpuid, OUT char** buf, OUT uint32_t* len); #ifdef PPM_ENABLE_SENTINEL // Get the sentinel at the beginning of the event @@ -702,11 +1024,69 @@ uint32_t scap_event_get_sentinel_begin(scap_evt* e); // The returned pointer must be freed via scap_proc_free by the caller. struct scap_threadinfo* scap_proc_get(scap_t* handle, int64_t tid, bool scan_sockets); -void scap_proc_free(scap_t* handle, struct scap_threadinfo* procinfo); +// Check if the given thread exists in ;proc +bool scap_is_thread_alive(scap_t* handle, int64_t pid, int64_t tid, const char* comm); -int32_t scap_stop_dropping_mode(scap_t* handle); +// like getpid() but returns the global PID even inside a container +int32_t scap_getpid_global(scap_t* handle, int64_t* pid); +struct scap_threadinfo *scap_proc_alloc(scap_t* handle); +void scap_proc_free(scap_t* handle, struct scap_threadinfo* procinfo); +void scap_dev_delete(scap_t* handle, scap_mountinfo* dev); +int32_t scap_stop_dropping_mode(scap_t* handle); int32_t scap_start_dropping_mode(scap_t* handle, uint32_t sampling_ratio); +int32_t scap_enable_dynamic_snaplen(scap_t* handle); +int32_t scap_disable_dynamic_snaplen(scap_t* handle); +void scap_proc_free_table(scap_t* handle); +void scap_free_device_table(scap_t* handle); +void scap_refresh_iflist(scap_t* handle); +void scap_refresh_proc_table(scap_t* handle); +void scap_set_refresh_proc_table_when_saving(scap_t* handle, bool refresh); +uint64_t scap_ftell(scap_t *handle); +void scap_fseek(scap_t *handle, uint64_t off); +int32_t scap_enable_tracers_capture(scap_t* handle); +int32_t scap_enable_page_faults(scap_t *handle); +uint64_t scap_get_unexpected_block_readsize(scap_t* handle); +int32_t scap_proc_add(scap_t* handle, uint64_t tid, scap_threadinfo* tinfo); +int32_t scap_fd_add(scap_t *handle, scap_threadinfo* tinfo, uint64_t fd, scap_fdinfo* fdinfo); +scap_dumper_t *scap_memory_dump_open(scap_t *handle, uint8_t* targetbuf, uint64_t targetbufsize); +#ifdef USE_ZLIB +int32_t compr(uint8_t* dest, uint64_t* destlen, const uint8_t* source, uint64_t sourcelen, int level); +#endif +uint8_t* scap_get_memorydumper_curpos(scap_dumper_t *d); +int32_t scap_write_proc_fds(scap_t *handle, struct scap_threadinfo *tinfo, scap_dumper_t *d); +int32_t scap_write_proclist_header(scap_t *handle, scap_dumper_t *d, uint32_t totlen); +int32_t scap_write_proclist_trailer(scap_t *handle, scap_dumper_t *d, uint32_t totlen); +int32_t scap_write_proclist_entry(scap_t *handle, scap_dumper_t *d, struct scap_threadinfo *tinfo, uint32_t len); +// Variant of scap_write_proclist_entry where array-backed information +// about the thread is provided separate from the scap_threadinfo +// struct. +int32_t scap_write_proclist_entry_bufs(scap_t *handle, scap_dumper_t *d, struct scap_threadinfo *tinfo, uint32_t len, + const char *comm, + const char *exe, + const char *exepath, + const struct iovec *args, int argscnt, + const struct iovec *envs, int envscnt, + const char *cwd, + const struct iovec *cgroups, int cgroupscnt, + const char *root); + +// Turn on processing only a subset syscalls. This is only appliable when scap +// is in LIVE mode. +int32_t scap_enable_simpledriver_mode(scap_t* handle); +int32_t scap_get_n_tracepoint_hit(scap_t* handle, long* ret); +#ifdef CYGWING_AGENT +typedef struct wh_t wh_t; +wh_t* scap_get_wmi_handle(scap_t* handle); +#endif +int32_t scap_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end); + +/** + * By default we have an expanded snaplen for the default statsd port. If the + * statsd port is non-standard, communicate that port value to the kernel to + * get the expanded snaplen for the correct port. + */ +int32_t scap_set_statsd_port(scap_t* handle, uint16_t port); #ifdef __cplusplus } diff --git a/userspace/libscap/scap.vcxproj b/userspace/libscap/scap.vcxproj index f85e429728..40b2836b3c 100644 --- a/userspace/libscap/scap.vcxproj +++ b/userspace/libscap/scap.vcxproj @@ -1,116 +1,117 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {A45C5520-F4BA-480A-A7D3-EB1E9DDFBE6C} - Win32Proj - - - - DynamicLibrary - true - v110 - - - DynamicLibrary - false - v110 - - - - - - - - - - - - - true - $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);.;../../driver - $(ProjectDir)\..\Debug\ - $(ProjectDir)\..\Debug\ - - - true - $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);.;../../driver - $(ProjectDir)\..\Release\ - $(ProjectDir)\..\Release\ - - - - WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_WINDOWS;_USRDLL;SCAP_EXPORTS;%(PreprocessorDefinitions) - MultiThreadedDebugDLL - Level3 - ProgramDatabase - Disabled - ../../common - - - MachineX86 - true - Console - $(ProjectDir)\..\Debug\$(TargetName).lib - scap.def - $(SolutionDir)Debug\$(TargetName)$(TargetExt) - $(OutDir)scap.pdb - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;%(AdditionalDependencies) - - - - - WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_WINDOWS;_USRDLL;SCAP_EXPORTS;%(PreprocessorDefinitions) - MultiThreadedDLL - Level3 - ProgramDatabase - ../../common - - - MachineX86 - true - Windows - true - true - $(ProjectDir)\..\Release\$(TargetName).lib - scap.def - $(SolutionDir)\Release\$(TargetName)$(TargetExt) - $(OutDir)scap.pdb - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;%(AdditionalDependencies) - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {A45C5520-F4BA-480A-A7D3-EB1E9DDFBE6C} + Win32Proj + + + + DynamicLibrary + true + v140 + + + DynamicLibrary + false + v140 + + + + + + + + + + + + + true + $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);.;../../driver + $(ProjectDir)\..\Debug\ + $(ProjectDir)\..\Debug\ + + + true + $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);.;../../driver + $(ProjectDir)\..\Release\ + $(ProjectDir)\..\Release\ + + + + WIN32;_CRT_SECURE_NO_WARNINGS;PLATFORM_NAME="Windows";_DEBUG;_WINDOWS;_USRDLL;SCAP_EXPORTS;%(PreprocessorDefinitions) + MultiThreadedDebugDLL + Level3 + ProgramDatabase + Disabled + ../../common;../../build/zlib-prefix/src/zlib + + + MachineX86 + true + Console + $(ProjectDir)\..\Debug\$(TargetName).lib + scap.def + $(SolutionDir)Debug\$(TargetName)$(TargetExt) + $(OutDir)scap.pdb + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;%(AdditionalDependencies);../../build/zlib-prefix/src/zlib/zdll.lib + + + + + WIN32;_CRT_SECURE_NO_WARNINGS;PLATFORM_NAME="Windows";NDEBUG;_WINDOWS;_USRDLL;SCAP_EXPORTS;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + ../../common;../../build/zlib-prefix/src/zlib + + + MachineX86 + true + Windows + true + true + $(ProjectDir)\..\Release\$(TargetName).lib + scap.def + $(SolutionDir)\Release\$(TargetName)$(TargetExt) + $(OutDir)scap.pdb + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Ws2_32.lib;%(AdditionalDependencies);../../build/zlib-prefix/src/zlib/zdll.lib + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/userspace/libscap/scap.vcxproj.filters b/userspace/libscap/scap.vcxproj.filters index dce9c6c463..1357805d20 100644 --- a/userspace/libscap/scap.vcxproj.filters +++ b/userspace/libscap/scap.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + diff --git a/userspace/libscap/scap_bpf.c b/userspace/libscap/scap_bpf.c new file mode 100644 index 0000000000..daf66eb3f5 --- /dev/null +++ b/userspace/libscap/scap_bpf.c @@ -0,0 +1,1578 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifndef MINIMAL_BUILD +#include +#endif // MINIMAL_BUILD +#include +#include +#include +#include +#include + +#include "scap.h" +#include "scap-int.h" +#include "scap_bpf.h" +#include "driver_config.h" +#include "../../driver/bpf/types.h" +#include "compat/misc.h" +#include "compat/bpf.h" + +// +// Some of this code is taken from the kernel samples under samples/bpf, +// namely the parsing of the ELF objects, which is very tedious and not +// worth reinventing from scratch. The code has been readapted and simplified +// to tailor the sysdig use case. In the future, sysdig can fully switch to +// libbpf, but at the moment is not very worth the effort considering the +// subset of features needed. +// + +struct bpf_map_data { + int fd; + size_t elf_offset; + struct bpf_map_def def; +}; + +static const int BUF_SIZE_PAGES = 2048; + +static const int BPF_LOG_SIZE = 1 << 18; + +#define FILLER_NAME_FN(x) #x, +static const char *g_filler_names[PPM_FILLER_MAX] = { + FILLER_LIST_MAPPER(FILLER_NAME_FN) +}; +#undef FILLER_NAME_FN + +static int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) +{ + return syscall(__NR_bpf, cmd, attr, size); +} + +static int sys_perf_event_open(struct perf_event_attr *attr, + pid_t pid, int cpu, int group_fd, + unsigned long flags) +{ + return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); +} + +static int32_t lookup_filler_id(const char *filler_name) +{ + int j; + + for(j = 0; j < sizeof(g_filler_names) / sizeof(g_filler_names[0]); ++j) + { + if(strcmp(filler_name, g_filler_names[j]) == 0) + { + return j; + } + } + + return -1; +} + +static int bpf_map_update_elem(int fd, const void *key, const void *value, uint64_t flags) +{ + union bpf_attr attr; + + bzero(&attr, sizeof(attr)); + + attr.map_fd = fd; + attr.key = (unsigned long) key; + attr.value = (unsigned long) value; + attr.flags = flags; + + return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); +} + +static int bpf_map_lookup_elem(int fd, const void *key, void *value) +{ + union bpf_attr attr; + + bzero(&attr, sizeof(attr)); + + attr.map_fd = fd; + attr.key = (unsigned long) key; + attr.value = (unsigned long) value; + + return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); +} + +static int bpf_map_create(enum bpf_map_type map_type, + int key_size, int value_size, int max_entries, + uint32_t map_flags) +{ + union bpf_attr attr; + + bzero(&attr, sizeof(attr)); + + attr.map_type = map_type; + attr.key_size = key_size; + attr.value_size = value_size; + attr.max_entries = max_entries; + attr.map_flags = map_flags; + + return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); +} + +static int bpf_load_program(const struct bpf_insn *insns, + enum bpf_prog_type type, + size_t insns_cnt, + char *log_buf, + size_t log_buf_sz) +{ + union bpf_attr attr; + int fd; + + bzero(&attr, sizeof(attr)); + + attr.prog_type = type; + attr.insn_cnt = (uint32_t) insns_cnt; + attr.insns = (unsigned long) insns; + attr.license = (unsigned long) "GPL"; + attr.log_buf = (unsigned long) NULL; + attr.log_size = 0; + attr.log_level = 0; + + fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); + if(fd >= 0 || !log_buf || !log_buf_sz) + { + return fd; + } + + attr.log_buf = (unsigned long) log_buf; + attr.log_size = log_buf_sz; + attr.log_level = 1; + log_buf[0] = 0; + + return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); +} + +static int bpf_raw_tracepoint_open(const char *name, int prog_fd) +{ + union bpf_attr attr; + + bzero(&attr, sizeof(attr)); + attr.raw_tracepoint.name = (unsigned long) name; + attr.raw_tracepoint.prog_fd = prog_fd; + + return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr)); +} + +#ifndef MINIMAL_BUILD +static int32_t get_elf_section(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname, GElf_Shdr *shdr, Elf_Data **data) +{ + Elf_Scn *scn = elf_getscn(elf, i); + if(!scn) + { + return SCAP_FAILURE; + } + + if(gelf_getshdr(scn, shdr) != shdr) + { + return SCAP_FAILURE; + } + + *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name); + if(!*shname || !shdr->sh_size) + { + return SCAP_FAILURE; + } + + *data = elf_getdata(scn, 0); + if(!*data || elf_getdata(scn, *data) != NULL) + { + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +static int cmp_symbols(const void *l, const void *r) +{ + const GElf_Sym *lsym = (const GElf_Sym *)l; + const GElf_Sym *rsym = (const GElf_Sym *)r; + + if(lsym->st_value < rsym->st_value) + { + return -1; + } + else if(lsym->st_value > rsym->st_value) + { + return 1; + } + else + { + return 0; + } +} + +static int32_t load_elf_maps_section(scap_t *handle, struct bpf_map_data *maps, + int maps_shndx, Elf *elf, Elf_Data *symbols, + int strtabidx, int *nr_maps) +{ + Elf_Data *data_maps; + GElf_Sym *sym; + Elf_Scn *scn; + int i; + + scn = elf_getscn(elf, maps_shndx); + if(scn) + { + data_maps = elf_getdata(scn, NULL); + } + + if(!scn || !data_maps) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Failed to get Elf_Data from maps section %d", maps_shndx); + return SCAP_FAILURE; + } + + *nr_maps = 0; + sym = calloc(BPF_MAPS_MAX + 1, sizeof(GElf_Sym)); + for(i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) + { + ASSERT(*nr_maps < BPF_MAPS_MAX + 1); + if(!gelf_getsym(symbols, i, &sym[*nr_maps])) + { + continue; + } + + if(sym[*nr_maps].st_shndx != maps_shndx) + { + continue; + } + + (*nr_maps)++; + } + + qsort(sym, *nr_maps, sizeof(GElf_Sym), cmp_symbols); + + ASSERT(data_maps->d_size / *nr_maps == sizeof(struct bpf_map_def)); + + for(i = 0; i < *nr_maps; i++) + { + struct bpf_map_def *def; + size_t offset; + + offset = sym[i].st_value; + def = (struct bpf_map_def *)(data_maps->d_buf + offset); + maps[i].elf_offset = offset; + memcpy(&maps[i].def, def, sizeof(struct bpf_map_def)); + } + + free(sym); + return SCAP_SUCCESS; +} +#endif // MINIMAL_BUILD + +static int32_t load_maps(scap_t *handle, struct bpf_map_data *maps, int nr_maps) +{ + int j; + + for(j = 0; j < nr_maps; ++j) + { + if(j == SYSDIG_PERF_MAP || + j == SYSDIG_LOCAL_STATE_MAP || + j == SYSDIG_FRAME_SCRATCH_MAP || + j == SYSDIG_TMP_SCRATCH_MAP) + { + maps[j].def.max_entries = handle->m_ncpus; + } + + handle->m_bpf_map_fds[j] = bpf_map_create(maps[j].def.type, + maps[j].def.key_size, + maps[j].def.value_size, + maps[j].def.max_entries, + maps[j].def.map_flags); + + maps[j].fd = handle->m_bpf_map_fds[j]; + + if(handle->m_bpf_map_fds[j] < 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't create map: %s", scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + if(maps[j].def.type == BPF_MAP_TYPE_PROG_ARRAY) + { + handle->m_bpf_prog_array_map_idx = j; + } + } + + return SCAP_SUCCESS; +} + +#ifndef MINIMAL_BUILD +static int32_t parse_relocations(scap_t *handle, Elf_Data *data, Elf_Data *symbols, + GElf_Shdr *shdr, struct bpf_insn *insn, + struct bpf_map_data *maps, int nr_maps) +{ + int nrels; + int i; + + nrels = shdr->sh_size / shdr->sh_entsize; + + for(i = 0; i < nrels; i++) + { + GElf_Sym sym; + GElf_Rel rel; + unsigned int insn_idx; + bool match = false; + int map_idx; + + gelf_getrel(data, i, &rel); + + insn_idx = rel.r_offset / sizeof(struct bpf_insn); + + gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym); + + if(insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid relocation for insn[%d].code 0x%x", insn_idx, insn[insn_idx].code); + return SCAP_FAILURE; + } + + insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; + + for(map_idx = 0; map_idx < nr_maps; map_idx++) + { + if(maps[map_idx].elf_offset == sym.st_value) + { + match = true; + break; + } + } + + if(match) + { + insn[insn_idx].imm = maps[map_idx].fd; + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid relocation for insn[%d] no map_data match\n", insn_idx); + return SCAP_FAILURE; + } + } + + return SCAP_SUCCESS; +} +#endif // MINIMAL_BUILD + +static int32_t load_tracepoint(scap_t* handle, const char *event, struct bpf_insn *prog, int size) +{ + struct perf_event_attr attr = {}; + enum bpf_prog_type program_type; + size_t insns_cnt; + char buf[256]; + bool raw_tp; + int efd; + int err; + int fd; + int id; + + insns_cnt = size / sizeof(struct bpf_insn); + + attr.type = PERF_TYPE_TRACEPOINT; + attr.sample_type = PERF_SAMPLE_RAW; + attr.sample_period = 1; + attr.wakeup_events = 1; + + char *error = malloc(BPF_LOG_SIZE); + if(!error) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "malloc(BPF_LOG_BUF_SIZE)"); + return SCAP_FAILURE; + } + + if(memcmp(event, "raw_tracepoint/", sizeof("raw_tracepoint/") - 1) == 0) + { + raw_tp = true; + program_type = BPF_PROG_TYPE_RAW_TRACEPOINT; + event += sizeof("raw_tracepoint/") - 1; + } + else + { + raw_tp = false; + program_type = BPF_PROG_TYPE_TRACEPOINT; + event += sizeof("tracepoint/") - 1; + } + + if(*event == 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "event name cannot be empty"); + return SCAP_FAILURE; + } + + fd = bpf_load_program(prog, program_type, insns_cnt, error, BPF_LOG_SIZE); + if(fd < 0) + { + fprintf(stderr, "%s", error); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "bpf_load_program() err=%d event=%s message=%s", errno, event, error); + free(error); + return SCAP_FAILURE; + } + + free(error); + + handle->m_bpf_prog_fds[handle->m_bpf_prog_cnt++] = fd; + + if(memcmp(event, "filler/", sizeof("filler/") - 1) == 0) + { + int prog_id; + + event += sizeof("filler/") - 1; + if(*event == 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "filler name cannot be empty"); + return SCAP_FAILURE; + } + + prog_id = lookup_filler_id(event); + if(prog_id == -1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid filler name: %s", event); + return SCAP_FAILURE; + } + + err = bpf_map_update_elem(handle->m_bpf_map_fds[handle->m_bpf_prog_array_map_idx], &prog_id, &fd, BPF_ANY); + if(err < 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "failure populating program array"); + return SCAP_FAILURE; + } + + handle->m_bpf_fillers[prog_id] = true; + + return SCAP_SUCCESS; + } + + if(raw_tp) + { + efd = bpf_raw_tracepoint_open(event, fd); + if(efd < 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "BPF_RAW_TRACEPOINT_OPEN: event %s: %s", event, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + } + else + { + strcpy(buf, "/sys/kernel/debug/tracing/events/"); + strcat(buf, event); + strcat(buf, "/id"); + + efd = open(buf, O_RDONLY, 0); + if(efd < 0) + { + if(strcmp(event, "exceptions/page_fault_user") == 0 || + strcmp(event, "exceptions/page_fault_kernel") == 0) + { + return SCAP_SUCCESS; + } + + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "failed to open event %s", event); + return SCAP_FAILURE; + } + + err = read(efd, buf, sizeof(buf)); + if(err < 0 || err >= sizeof(buf)) + { + close(efd); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "read from '%s' failed '%s'", event, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + close(efd); + + buf[err] = 0; + id = atoi(buf); + attr.config = id; + + efd = sys_perf_event_open(&attr, -1, 0, -1, 0); + if(efd < 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "event %d fd %d err %s", id, efd, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + if(ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd)) + { + close(efd); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "PERF_EVENT_IOC_SET_BPF: %s", scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + } + + handle->m_bpf_event_fd[handle->m_bpf_prog_cnt - 1] = efd; + + return SCAP_SUCCESS; +} + +#ifndef MINIMAL_BUILD +static int32_t load_bpf_file(scap_t *handle, const char *path) +{ + int j; + int maps_shndx = 0; + int strtabidx = 0; + GElf_Shdr shdr; + GElf_Shdr shdr_prog; + Elf_Data *data; + Elf_Data *data_prog; + Elf_Data *symbols = NULL; + char *shname; + char *shname_prog; + int nr_maps = 0; + struct bpf_map_data maps[BPF_MAPS_MAX]; + struct utsname osname; + int32_t res = SCAP_FAILURE; + + if(uname(&osname)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't call uname()"); + return SCAP_FAILURE; + } + + if(elf_version(EV_CURRENT) == EV_NONE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid ELF version"); + return SCAP_FAILURE; + } + + int program_fd = open(path, O_RDONLY, 0); + if(program_fd < 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't open BPF probe '%s': %s", path, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + Elf *elf = elf_begin(program_fd, ELF_C_READ_MMAP_PRIVATE, NULL); + if(!elf) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't read ELF format"); + goto cleanup; + } + + GElf_Ehdr ehdr; + if(gelf_getehdr(elf, &ehdr) != &ehdr) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't read ELF header"); + goto cleanup; + } + + for(j = 0; j < ehdr.e_shnum; ++j) + { + if(get_elf_section(elf, j, &ehdr, &shname, &shdr, &data) != SCAP_SUCCESS) + { + continue; + } + + if(strcmp(shname, "maps") == 0) + { + maps_shndx = j; + } + else if(shdr.sh_type == SHT_SYMTAB) + { + strtabidx = shdr.sh_link; + symbols = data; + } + else if(strcmp(shname, "kernel_version") == 0) { + if(strcmp(osname.release, data->d_buf)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "BPF probe is compiled for %s, but running version is %s", + (char *) data->d_buf, osname.release); + goto cleanup; + } + } + else if(strcmp(shname, "probe_version") == 0) { + if(strcmp(PROBE_VERSION, data->d_buf)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "BPF probe version is %s, but running version is %s", + (char *) data->d_buf, PROBE_VERSION); + goto cleanup; + } + } + } + + if(!symbols) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "missing SHT_SYMTAB section"); + goto cleanup; + } + + if(maps_shndx) + { + if(load_elf_maps_section(handle, maps, maps_shndx, elf, symbols, strtabidx, &nr_maps) != SCAP_SUCCESS) + { + goto cleanup; + } + + if(load_maps(handle, maps, nr_maps) != SCAP_SUCCESS) + { + goto cleanup; + } + } + + for(j = 0; j < ehdr.e_shnum; ++j) + { + if(get_elf_section(elf, j, &ehdr, &shname, &shdr, &data) != SCAP_SUCCESS) + { + continue; + } + + if(shdr.sh_type == SHT_REL) + { + struct bpf_insn *insns; + + if(get_elf_section(elf, shdr.sh_info, &ehdr, &shname_prog, &shdr_prog, &data_prog) != SCAP_SUCCESS) + { + continue; + } + + insns = (struct bpf_insn *) data_prog->d_buf; + + if(parse_relocations(handle, data, symbols, &shdr, insns, maps, nr_maps)) + { + continue; + } + } + } + + for(j = 0; j < ehdr.e_shnum; ++j) + { + if(get_elf_section(elf, j, &ehdr, &shname, &shdr, &data) != SCAP_SUCCESS) + { + continue; + } + + if(memcmp(shname, "tracepoint/", sizeof("tracepoint/") - 1) == 0 || + memcmp(shname, "raw_tracepoint/", sizeof("raw_tracepoint/") - 1) == 0) + { + if(load_tracepoint(handle, shname, data->d_buf, data->d_size) != SCAP_SUCCESS) + { + goto cleanup; + } + } + } + + res = SCAP_SUCCESS; +cleanup: + elf_end(elf); + close(program_fd); + return res; +} +#endif // MINIMAL_BUILD + +static void *perf_event_mmap(scap_t *handle, int fd) +{ + int page_size = getpagesize(); + int ring_size = page_size * BUF_SIZE_PAGES; + int header_size = page_size; + int total_size = ring_size * 2 + header_size; + + // + // All this playing with MAP_FIXED might be very very wrong, revisit + // + + void *tmp = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if(tmp == MAP_FAILED) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "mmap (1): %s", scap_strerror(handle, errno)); + return MAP_FAILED; + } + + // Map the second copy to allow us to handle the wrap case normally + void *p1 = mmap(tmp + ring_size, ring_size + header_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0); + if(p1 == MAP_FAILED) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "mmap (2): %s", scap_strerror(handle, errno)); + munmap(tmp, total_size); + return MAP_FAILED; + } + + ASSERT(p1 == tmp + ring_size); + + // Map the main copy + void *p2 = mmap(tmp, ring_size + header_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0); + if(p2 == MAP_FAILED) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "mmap (3): %s", scap_strerror(handle, errno)); + munmap(tmp, total_size); + return MAP_FAILED; + } + + ASSERT(p2 == tmp); + + return tmp; +} + +static int32_t populate_syscall_routing_table_map(scap_t *handle) +{ + int j; + + for(j = 0; j < SYSCALL_TABLE_SIZE; ++j) + { + long code = g_syscall_code_routing_table[j]; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SYSCALL_CODE_ROUTING_TABLE], &j, &code, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SYSCALL_CODE_ROUTING_TABLE bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + } + + return SCAP_SUCCESS; +} + +static int32_t populate_syscall_table_map(scap_t *handle) +{ + int j; + + for(j = 0; j < SYSCALL_TABLE_SIZE; ++j) + { + const struct syscall_evt_pair *p = &g_syscall_table[j]; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SYSCALL_TABLE], &j, p, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SYSCALL_TABLE bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + } + + return SCAP_SUCCESS; +} + +static int32_t populate_event_table_map(scap_t *handle) +{ + int j; + + for(j = 0; j < PPM_EVENT_MAX; ++j) + { + const struct ppm_event_info *e = &g_event_info[j]; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_EVENT_INFO_TABLE], &j, e, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_EVENT_INFO_TABLE bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + } + + return SCAP_SUCCESS; +} + +static int32_t populate_fillers_table_map(scap_t *handle) +{ + int j; + + for(j = 0; j < PPM_EVENT_MAX; ++j) + { + const struct ppm_event_entry *e = &g_ppm_events[j]; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_FILLERS_TABLE], &j, e, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_FILLERS_TABLE bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + } + + for(j = 0; j < PPM_FILLER_MAX; ++j) + { + if(!handle->m_bpf_fillers[j]) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Missing filler %d (%s)\n", j, g_filler_names[j]); + return SCAP_FAILURE; + } + } + + return SCAP_SUCCESS; +} + +// +// This is needed to make sure that the driver can properly +// lookup sockets. We generate a fake socket system call +// at the beginning so the calibration will surely take place. +// For more info, read the corresponding filler in kernel space. +// +static int32_t calibrate_socket_file_ops() +{ + int fd = socket(AF_INET, SOCK_DGRAM, 0); + if(fd == -1) + { + return SCAP_FAILURE; + } + + close(fd); + return SCAP_SUCCESS; +} + +int32_t scap_bpf_start_capture(scap_t *handle) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.capture_enabled = true; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + if(calibrate_socket_file_ops() != SCAP_SUCCESS) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "calibrate_socket_file_ops"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_stop_capture(scap_t *handle) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.capture_enabled = false; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_set_snaplen(scap_t* handle, uint32_t snaplen) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(snaplen > RW_MAX_SNAPLEN) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "snaplen can't exceed %d\n", RW_MAX_SNAPLEN); + return SCAP_FAILURE; + } + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.snaplen = snaplen; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.fullcapture_port_range_start = range_start; + settings.fullcapture_port_range_end = range_end; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_set_statsd_port(scap_t* const handle, const uint16_t port) +{ + struct sysdig_bpf_settings settings = {}; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.statsd_port = port; + + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_disable_dynamic_snaplen(scap_t* handle) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.do_dynamic_snaplen = false; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_start_dropping_mode(scap_t* handle, uint32_t sampling_ratio) +{ + switch(sampling_ratio) + { + case 1: + case 2: + case 4: + case 8: + case 16: + case 32: + case 64: + case 128: + break; + default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid sampling ratio size"); + return SCAP_FAILURE; + } + + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.sampling_ratio = sampling_ratio; + settings.dropping_mode = true; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_stop_dropping_mode(scap_t* handle) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.sampling_ratio = 1; + settings.dropping_mode = false; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_enable_dynamic_snaplen(scap_t* handle) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.do_dynamic_snaplen = true; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_enable_page_faults(scap_t* handle) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.page_faults = true; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_enable_tracers_capture(scap_t* handle) +{ + struct sysdig_bpf_settings settings; + int k = 0; + + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_lookup_elem < 0"); + return SCAP_FAILURE; + } + + settings.tracers_enabled = true; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_close(scap_t *handle) +{ + int j; + + int page_size = getpagesize(); + int ring_size = page_size * BUF_SIZE_PAGES; + int header_size = page_size; + int total_size = ring_size * 2 + header_size; + + for(j = 0; j < handle->m_ndevs; j++) + { + if(handle->m_devs[j].m_buffer != MAP_FAILED) + { +#ifdef _DEBUG + int ret; + ret = munmap(handle->m_devs[j].m_buffer, total_size); +#else + munmap(handle->m_devs[j].m_buffer, total_size); +#endif + ASSERT(ret == 0); + } + + if(handle->m_devs[j].m_fd > 0) + { + close(handle->m_devs[j].m_fd); + } + } + + for(j = 0; j < sizeof(handle->m_bpf_event_fd) / sizeof(handle->m_bpf_event_fd[0]); ++j) + { + if(handle->m_bpf_event_fd[j] > 0) + { + close(handle->m_bpf_event_fd[j]); + handle->m_bpf_event_fd[j] = 0; + } + } + + for(j = 0; j < sizeof(handle->m_bpf_prog_fds) / sizeof(handle->m_bpf_prog_fds[0]); ++j) + { + if(handle->m_bpf_prog_fds[j] > 0) + { + close(handle->m_bpf_prog_fds[j]); + handle->m_bpf_prog_fds[j] = 0; + } + } + + for(j = 0; j < sizeof(handle->m_bpf_map_fds) / sizeof(handle->m_bpf_map_fds[0]); ++j) + { + if(handle->m_bpf_map_fds[j] > 0) + { + close(handle->m_bpf_map_fds[j]); + handle->m_bpf_map_fds[j] = 0; + } + } + + handle->m_bpf_prog_cnt = 0; + handle->m_bpf_prog_array_map_idx = -1; + + return SCAP_SUCCESS; +} + +// +// This is completely horrible, revisit this shameful code +// with a proper solution +// +static int32_t set_boot_time(scap_t *handle, uint64_t *boot_time) +{ + struct timespec ts_uptime; + struct timeval tv_now; + uint64_t now; + uint64_t uptime; + + if(gettimeofday(&tv_now, NULL)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "gettimeofday"); + return SCAP_FAILURE; + } + + now = tv_now.tv_sec * (uint64_t) 1000000000 + tv_now.tv_usec * 1000; + + if(clock_gettime(CLOCK_BOOTTIME, &ts_uptime)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "clock_gettime"); + return SCAP_FAILURE; + } + + uptime = ts_uptime.tv_sec * (uint64_t) 1000000000 + ts_uptime.tv_nsec; + + *boot_time = now - uptime; + + return SCAP_SUCCESS; +} + +static int32_t set_runtime_params(scap_t *handle) +{ + struct rlimit rl; + rl.rlim_max = RLIM_INFINITY; + rl.rlim_cur = rl.rlim_max; + if(setrlimit(RLIMIT_MEMLOCK, &rl)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "setrlimit failed"); + return SCAP_FAILURE; + } + + FILE *f = fopen("/proc/sys/net/core/bpf_jit_enable", "w"); + if(!f) + { + // snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Can't open /proc/sys/net/core/bpf_jit_enable"); + // return SCAP_FAILURE; + + // Not every kernel has BPF_JIT enabled. Fix this after COS changes. + return SCAP_SUCCESS; + } + + if(fprintf(f, "1") != 1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Can't write to /proc/sys/net/core/bpf_jit_enable"); + fclose(f); + return SCAP_FAILURE; + } + + fclose(f); + + f = fopen("/proc/sys/net/core/bpf_jit_harden", "w"); + if(!f) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Can't open /proc/sys/net/core/bpf_jit_harden"); + return SCAP_FAILURE; + } + + if(fprintf(f, "0") != 1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Can't write to /proc/sys/net/core/bpf_jit_harden"); + fclose(f); + return SCAP_FAILURE; + } + + fclose(f); + + f = fopen("/proc/sys/net/core/bpf_jit_kallsyms", "w"); + if(!f) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Can't open /proc/sys/net/core/bpf_jit_kallsyms"); + return SCAP_FAILURE; + } + + if(fprintf(f, "1") != 1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Can't write to /proc/sys/net/core/bpf_jit_kallsyms"); + fclose(f); + return SCAP_FAILURE; + } + + fclose(f); + + return SCAP_SUCCESS; +} + +static int32_t set_default_settings(scap_t *handle) +{ + struct sysdig_bpf_settings settings; + + if(set_boot_time(handle, &settings.boot_time) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + settings.socket_file_ops = NULL; + settings.snaplen = RW_SNAPLEN; + settings.sampling_ratio = 1; + settings.capture_enabled = false; + settings.do_dynamic_snaplen = false; + settings.page_faults = false; + settings.dropping_mode = false; + settings.is_dropping = false; + settings.tracers_enabled = false; + settings.fullcapture_port_range_start = 0; + settings.fullcapture_port_range_end = 0; + settings.statsd_port = 8125; + + int k = 0; + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_SETTINGS_MAP], &k, &settings, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_SETTINGS_MAP bpf_map_update_elem < 0"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_load(scap_t *handle, const char *bpf_probe) +{ +#ifdef MINIMAL_BUILD + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "The eBPF probe driver is not supported when using a minimal build"); + return SCAP_FAILURE; +#else + int online_cpu; + int j; + + if(set_runtime_params(handle) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + handle->m_bpf_prog_array_map_idx = -1; + + if(!bpf_probe) + { + ASSERT(false); + return SCAP_FAILURE; + } + + if(load_bpf_file(handle, bpf_probe) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + if(populate_syscall_routing_table_map(handle) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + if(populate_syscall_table_map(handle) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + if(populate_event_table_map(handle) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + if(populate_fillers_table_map(handle) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + // + // Open and initialize all the devices + // + online_cpu = 0; + for(j = 0; j < handle->m_ncpus; ++j) + { + struct perf_event_attr attr = { + .sample_type = PERF_SAMPLE_RAW, + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_BPF_OUTPUT, + }; + int pmu_fd; + + if(j > 0) + { + char filename[SCAP_MAX_PATH_SIZE]; + int online; + FILE *fp; + + snprintf(filename, sizeof(filename), "/sys/devices/system/cpu/cpu%d/online", j); + + fp = fopen(filename, "r"); + if(fp == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't open %s: %s", filename, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + if(fscanf(fp, "%d", &online) != 1) + { + fclose(fp); + + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't read %s: %s", filename, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + fclose(fp); + + if(!online) + { + continue; + } + } + + if(online_cpu >= handle->m_ndevs) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "processors online: %d, expected: %d", online_cpu, handle->m_ndevs); + return SCAP_FAILURE; + } + + pmu_fd = sys_perf_event_open(&attr, -1, j, -1, 0); + if(pmu_fd < 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "pmu_fd < 0: %s", scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + handle->m_devs[online_cpu].m_fd = pmu_fd; + + if(bpf_map_update_elem(handle->m_bpf_map_fds[SYSDIG_PERF_MAP], &j, &pmu_fd, BPF_ANY) != 0) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "SYSDIG_PERF_MAP bpf_map_update_elem < 0: %s", scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + if(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "PERF_EVENT_IOC_ENABLE"); + return SCAP_FAILURE; + } + + // + // Map the ring buffer + // + handle->m_devs[online_cpu].m_buffer = perf_event_mmap(handle, pmu_fd); + if(handle->m_devs[online_cpu].m_buffer == MAP_FAILED) + { + return SCAP_FAILURE; + } + + ++online_cpu; + } + + if(online_cpu != handle->m_ndevs) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "processors online: %d, expected: %d", j, handle->m_ndevs); + return SCAP_FAILURE; + } + + if(set_default_settings(handle) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +#endif // MINIMAL_BUILD +} + +struct ppm_proclist_info *scap_bpf_get_threadlist(scap_t *handle) +{ + DIR *dir_p = NULL; + DIR *taskdir_p = NULL; + FILE *fp = NULL; + struct ppm_proclist_info *res = NULL; + struct dirent *dir_entry_p; + char procdirname[SCAP_MAX_PATH_SIZE]; + + handle->m_driver_procinfo->n_entries = 0; + + snprintf(procdirname, sizeof(procdirname), "%s/proc", scap_get_host_root()); + + dir_p = opendir(procdirname); + if(dir_p == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error opening the %s directory", procdirname); + goto error; + } + + while((dir_entry_p = readdir(dir_p)) != NULL) + { + char tasksdirname[SCAP_MAX_PATH_SIZE]; + struct dirent *taskdir_entry_p; + DIR *taskdir_p; + + if(strspn(dir_entry_p->d_name, "0123456789") != strlen(dir_entry_p->d_name)) + { + continue; + } + + snprintf(tasksdirname, sizeof(tasksdirname), "%s/%s/task", procdirname, dir_entry_p->d_name); + + taskdir_p = opendir(tasksdirname); + if(taskdir_p == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error opening the %s directory", tasksdirname); + continue; + } + + while((taskdir_entry_p = readdir(taskdir_p)) != NULL) + { + char filename[SCAP_MAX_PATH_SIZE]; + unsigned long utime; + unsigned long stime; + int tid; + + if(strspn(taskdir_entry_p->d_name, "0123456789") != strlen(taskdir_entry_p->d_name)) + { + continue; + } + + snprintf(filename, sizeof(filename), "%s/%s/stat", tasksdirname, taskdir_entry_p->d_name); + + fp = fopen(filename, "r"); + if(fp == NULL) + { + continue; + } + + if(fscanf(fp, "%d %*[^)] %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu", &tid, &utime, &stime) != 3) + { + fclose(fp); + fp = NULL; + continue; + } + + if(handle->m_driver_procinfo->n_entries == handle->m_driver_procinfo->max_entries) + { + if(!scap_alloc_proclist_info(handle, handle->m_driver_procinfo->n_entries + 256)) + { + goto error; + } + } + + handle->m_driver_procinfo->entries[handle->m_driver_procinfo->n_entries].pid = tid; + handle->m_driver_procinfo->entries[handle->m_driver_procinfo->n_entries].utime = utime; + handle->m_driver_procinfo->entries[handle->m_driver_procinfo->n_entries].stime = stime; + ++handle->m_driver_procinfo->n_entries; + + fclose(fp); + fp = NULL; + } + + closedir(taskdir_p); + taskdir_p = NULL; + } + + res = handle->m_driver_procinfo; +error: + if(dir_p) + { + closedir(dir_p); + } + + if(taskdir_p) + { + closedir(taskdir_p); + } + + if(fp) + { + fclose(fp); + } + + return res; +} + +int32_t scap_bpf_get_stats(scap_t* handle, OUT scap_stats* stats) +{ + int j; + + for(j = 0; j < handle->m_ncpus; j++) + { + struct sysdig_bpf_per_cpu_state v; + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_LOCAL_STATE_MAP], &j, &v)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Error looking up local state %d\n", j); + return SCAP_FAILURE; + } + + stats->n_evts += v.n_evts; + stats->n_drops_buffer += handle->m_devs[j].m_evt_lost + v.n_drops_buffer; + stats->n_drops_pf += v.n_drops_pf; + stats->n_drops_bug += v.n_drops_bug; + stats->n_drops += handle->m_devs[j].m_evt_lost + + v.n_drops_buffer + + v.n_drops_pf + + v.n_drops_bug; + } + + return SCAP_SUCCESS; +} + +int32_t scap_bpf_get_n_tracepoint_hit(scap_t* handle, long* ret) +{ + int j; + + for(j = 0; j < handle->m_ncpus; j++) + { + struct sysdig_bpf_per_cpu_state v; + if(bpf_map_lookup_elem(handle->m_bpf_map_fds[SYSDIG_LOCAL_STATE_MAP], &j, &v)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Error looking up local state %d\n", j); + return SCAP_FAILURE; + } + + ret[j] = v.n_evts; + } + + return SCAP_SUCCESS; +} diff --git a/userspace/libscap/scap_bpf.h b/userspace/libscap/scap_bpf.h new file mode 100644 index 0000000000..f632e224f3 --- /dev/null +++ b/userspace/libscap/scap_bpf.h @@ -0,0 +1,199 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#ifndef _SCAP_BPF_H +#define _SCAP_BPF_H + +#include "compat/perf_event.h" + +struct perf_event_sample { + struct perf_event_header header; + uint32_t size; + char data[]; +}; + +struct perf_lost_sample { + struct perf_event_header header; + uint64_t id; + uint64_t lost; +}; + +int32_t scap_bpf_load(scap_t *handle, const char *bpf_probe); +int32_t scap_bpf_start_capture(scap_t *handle); +int32_t scap_bpf_stop_capture(scap_t *handle); +int32_t scap_bpf_close(scap_t *handle); +int32_t scap_bpf_set_snaplen(scap_t* handle, uint32_t snaplen); +int32_t scap_bpf_set_fullcapture_port_range(scap_t* handle, uint16_t range_start, uint16_t range_end); +int32_t scap_bpf_set_statsd_port(scap_t* handle, uint16_t port); +int32_t scap_bpf_enable_dynamic_snaplen(scap_t* handle); +int32_t scap_bpf_disable_dynamic_snaplen(scap_t* handle); +int32_t scap_bpf_enable_page_faults(scap_t* handle); +int32_t scap_bpf_start_dropping_mode(scap_t* handle, uint32_t sampling_ratio); +int32_t scap_bpf_stop_dropping_mode(scap_t* handle); +int32_t scap_bpf_enable_tracers_capture(scap_t* handle); +struct ppm_proclist_info *scap_bpf_get_threadlist(scap_t *handle); +int32_t scap_bpf_get_stats(scap_t* handle, OUT scap_stats* stats); +int32_t scap_bpf_get_n_tracepoint_hit(scap_t* handle, long* ret); + +static inline scap_evt *scap_bpf_evt_from_perf_sample(void *evt) +{ + struct perf_event_sample *perf_evt = (struct perf_event_sample *) evt; + ASSERT(perf_evt->header.type == PERF_RECORD_SAMPLE); + return (scap_evt *) perf_evt->data; +} + +static inline void scap_bpf_get_buf_pointers(char *buf, uint64_t *phead, uint64_t *ptail, uint64_t *pread_size) +{ + struct perf_event_mmap_page *header; + uint64_t begin; + uint64_t end; + + header = (struct perf_event_mmap_page *) buf; + + *phead = header->data_head; + *ptail = header->data_tail; + + // clang-format off + asm volatile("" ::: "memory"); + // clang-format on + + begin = *ptail % header->data_size; + end = *phead % header->data_size; + + if(begin > end) + { + *pread_size = header->data_size - begin + end; + } + else + { + *pread_size = end - begin; + } +} + +static inline int32_t scap_bpf_advance_to_evt(scap_t *handle, uint16_t cpuid, bool skip_current, + char *cur_evt, char **next_evt, uint32_t *len) +{ + struct scap_device *dev; + void *base; + void *begin; + + dev = &handle->m_devs[cpuid]; + + struct perf_event_mmap_page *header = (struct perf_event_mmap_page *) dev->m_buffer; + + base = ((char *) header) + header->data_offset; + begin = cur_evt; + + while(*len) + { + struct perf_event_header *e = begin; + + ASSERT(*len >= sizeof(*e)); + ASSERT(*len >= e->size); + if(e->type == PERF_RECORD_SAMPLE) + { +#ifdef _DEBUG + struct perf_event_sample *sample = (struct perf_event_sample *) e; +#endif + ASSERT(*len >= sizeof(*sample)); + ASSERT(*len >= sample->size); + ASSERT(e->size == sizeof(*e) + sizeof(sample->size) + sample->size); + ASSERT(((scap_evt *) sample->data)->len <= sample->size); + + if(skip_current) + { + skip_current = false; + } + else + { + *next_evt = (char *) e; + break; + } + } + else if(e->type == PERF_RECORD_LOST) + { + struct perf_lost_sample *lost = (struct perf_lost_sample *) e; + ASSERT(*len >= sizeof(*lost)); + dev->m_evt_lost += lost->lost; + } + else + { + printf("Unknown event type=%d size=%d\n", + e->type, e->size); + ASSERT(false); + } + + if(begin + e->size > base + header->data_size) + { + begin = begin + e->size - header->data_size; + } + else if(begin + e->size == base + header->data_size) + { + begin = base; + } + else + { + begin += e->size; + } + + *len -= e->size; + } + + return SCAP_SUCCESS; +} + +static inline void scap_bpf_advance_tail(scap_t *handle, uint32_t cpuid) +{ + struct perf_event_mmap_page *header; + struct scap_device *dev; + + dev = &handle->m_devs[cpuid]; + header = (struct perf_event_mmap_page *)dev->m_buffer; + + // clang-format off + asm volatile("" ::: "memory"); + // clang-format on + + ASSERT(dev->m_lastreadsize > 0); + header->data_tail += dev->m_lastreadsize; + dev->m_lastreadsize = 0; +} + +static inline int32_t scap_bpf_readbuf(scap_t *handle, uint32_t cpuid, char **buf, uint32_t *len) +{ + struct perf_event_mmap_page *header; + struct scap_device *dev; + uint64_t tail; + uint64_t head; + uint64_t read_size; + char *p; + + dev = &handle->m_devs[cpuid]; + header = (struct perf_event_mmap_page *) dev->m_buffer; + + ASSERT(dev->m_lastreadsize == 0); + scap_bpf_get_buf_pointers((char *) header, &head, &tail, &read_size); + + dev->m_lastreadsize = read_size; + p = ((char *) header) + header->data_offset + tail % header->data_size; + *len = read_size; + + return scap_bpf_advance_to_evt(handle, cpuid, false, p, buf, len); +} + +#endif diff --git a/userspace/libscap/scap_event.c b/userspace/libscap/scap_event.c index 218e8130be..91fd961e74 100644 --- a/userspace/libscap/scap_event.c +++ b/userspace/libscap/scap_event.c @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include @@ -28,15 +29,12 @@ along with sysdig. If not, see . #include "scap.h" #include "scap-int.h" -// This is defined in the driver -extern const struct ppm_event_info g_event_info[]; -extern const struct ppm_syscall_desc g_syscall_info_table[]; - // // Get the event info table // const struct ppm_event_info* scap_get_event_info_table() { + ASSERT(validate_info_table_size()); return g_event_info; } @@ -48,28 +46,6 @@ const struct ppm_syscall_desc* scap_get_syscall_info_table() return g_syscall_info_table; } -uint32_t scap_event_compute_len(scap_evt* e) -{ - uint32_t j; - uint32_t res = 0; - uint16_t* lens = (uint16_t*)((char*)e + sizeof(struct ppm_evt_hdr)); - - ASSERT(e->type < PPM_EVENT_MAX); - - for(j = 0; j < g_event_info[e->type].nparams; j++) - { - res += lens[j]; - } - - res += g_event_info[e->type].nparams * sizeof(uint16_t) + sizeof(struct ppm_evt_hdr); - -#ifdef PPM_ENABLE_SENTINEL - res += sizeof(uint32_t); -#endif - - return res; -} - uint32_t scap_event_getlen(scap_evt* e) { return e->len; @@ -80,6 +56,11 @@ uint64_t scap_event_get_num(scap_t* handle) return handle->m_evtcnt; } +void scap_event_reset_count(scap_t* handle) +{ + handle->m_evtcnt = 0; +} + uint64_t scap_event_get_ts(scap_evt* e) { return e->ts; diff --git a/userspace/libscap/scap_fds.c b/userspace/libscap/scap_fds.c index 6ef4ca6159..62db27d022 100644 --- a/userspace/libscap/scap_fds.c +++ b/userspace/libscap/scap_fds.c @@ -1,29 +1,32 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + http://www.apache.org/licenses/LICENSE-2.0 -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . -*/ +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 #include #include "scap.h" #include "scap-int.h" +#include "scap_savefile.h" #include #include +#include #include "uthash.h" +#include "compat/misc.h" #ifdef _WIN32 #include #elif defined(__APPLE__) @@ -31,6 +34,7 @@ along with sysdig. If not, see . #include #include #include +#include #else #define __STDC_FORMAT_MACROS #include @@ -40,38 +44,68 @@ along with sysdig. If not, see . #include #include #include +#include +#include #include +#if defined(__linux__) +#if HAVE_SYS_MKDEV_H +#include +#endif +#ifdef HAVE_SYS_SYSMACROS_H +#include +#endif #include #include //#include //#include -#include +#endif #endif #define SOCKET_SCAN_BUFFER_SIZE 1024 * 1024 -int32_t scap_fd_print_ipv6_socket_info(scap_fdinfo *fdi, OUT char *str, uint32_t stlen) +int32_t scap_fd_print_ipv6_socket_info(scap_t *handle, scap_fdinfo *fdi, OUT char *str, uint32_t stlen) { char source_address[100]; char destination_address[100]; if(NULL == inet_ntop(AF_INET6,fdi->info.ipv6info.sip,source_address,100)) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, + "Could not convert IPv6 source address 0x%x%x%x%x (%s)", + fdi->info.ipv6info.sip[0], + fdi->info.ipv6info.sip[1], + fdi->info.ipv6info.sip[2], + fdi->info.ipv6info.sip[3], + scap_strerror(handle, errno)); return SCAP_FAILURE; } if(NULL == inet_ntop(AF_INET6,fdi->info.ipv6info.dip,destination_address,100)) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, + "Could not convert IPv6 source address 0x%x%x%x%x (%s)", + fdi->info.ipv6info.dip[0], + fdi->info.ipv6info.dip[1], + fdi->info.ipv6info.dip[2], + fdi->info.ipv6info.dip[3], + scap_strerror(handle, errno)); return SCAP_FAILURE; } snprintf(str,stlen,"%s:%u->%s:%u",source_address,fdi->info.ipv6info.sport,destination_address,fdi->info.ipv6info.dport); return SCAP_SUCCESS; } -int32_t scap_fd_print_ipv6_server_socket_info(scap_fdinfo *fdi, OUT char *str, uint32_t stlen) +int32_t scap_fd_print_ipv6_server_socket_info(scap_t *handle, scap_fdinfo *fdi, OUT char *str, uint32_t stlen) { char address[100]; if(NULL == inet_ntop(AF_INET6,fdi->info.ipv6serverinfo.ip,address,100)) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, + "Could not convert IPv6 source address 0x%x%x%x%x (%s)", + fdi->info.ipv6serverinfo.ip[0], + fdi->info.ipv6serverinfo.ip[1], + fdi->info.ipv6serverinfo.ip[2], + fdi->info.ipv6serverinfo.ip[3], + scap_strerror(handle, errno)); return SCAP_FAILURE; } snprintf(str,stlen,"%s:%u->:::*",address,fdi->info.ipv6serverinfo.port); @@ -81,13 +115,14 @@ int32_t scap_fd_print_ipv6_server_socket_info(scap_fdinfo *fdi, OUT char *str, u // // Convert an fd entry's info into a string // -int32_t scap_fd_info_to_string(scap_fdinfo *fdi, OUT char *str, uint32_t stlen) +int32_t scap_fd_info_to_string(scap_t *handle, scap_fdinfo *fdi, OUT char *str, uint32_t stlen) { // // Input validation // if((fdi)->type == SCAP_FD_UNKNOWN) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "fd type unknown"); return SCAP_FAILURE; } @@ -115,10 +150,10 @@ int32_t scap_fd_info_to_string(scap_fdinfo *fdi, OUT char *str, uint32_t stlen) (uint32_t)fdi->info.ipv4serverinfo.port); break; case SCAP_FD_IPV6_SOCK: - return scap_fd_print_ipv6_socket_info(fdi,str,stlen); + return scap_fd_print_ipv6_socket_info(handle,fdi,str,stlen); break; case SCAP_FD_IPV6_SERVSOCK: - return scap_fd_print_ipv6_server_socket_info(fdi,str,stlen); + return scap_fd_print_ipv6_server_socket_info(handle,fdi,str,stlen); break; case SCAP_FD_FIFO: snprintf(str, stlen, ""); @@ -141,15 +176,19 @@ int32_t scap_fd_info_to_string(scap_fdinfo *fdi, OUT char *str, uint32_t stlen) case SCAP_FD_UNIX_SOCK: snprintf(str, stlen, "%"PRIi64" %"PRIu64" %"PRIX64"-> %"PRIX64" %s", fdi->fd,fdi->ino, fdi->info.unix_socket_info.source,fdi->info.unix_socket_info.destination, fdi->info.unix_socket_info.fname); break; + case SCAP_FD_FILE_V2: case SCAP_FD_FILE: case SCAP_FD_DIRECTORY: break; case SCAP_FD_UNSUPPORTED: snprintf(str, stlen, ""); break; + case SCAP_FD_NETLINK: + snprintf(str, stlen, ""); + break; default: - printf("type = %d\n", fdi->type); ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "fd type unrecognized"); return SCAP_FAILURE; } @@ -161,7 +200,11 @@ int32_t scap_fd_info_to_string(scap_fdinfo *fdi, OUT char *str, uint32_t stlen) // uint32_t scap_fd_info_len(scap_fdinfo *fdi) { - uint32_t res = sizeof(fdi->ino) + 1 + sizeof(fdi->fd); + // + // NB: new fields must be appended + // + + uint32_t res = sizeof(uint32_t) + sizeof(fdi->ino) + 1 + sizeof(fdi->fd); switch(fdi->type) { @@ -190,10 +233,15 @@ uint32_t scap_fd_info_len(scap_fdinfo *fdi) sizeof(uint8_t); // l4proto break; case SCAP_FD_UNIX_SOCK: - res += - sizeof(uint64_t) + // unix source + res += + sizeof(uint64_t) + // unix source sizeof(uint64_t) + // unix destination - strnlen(fdi->info.unix_socket_info.fname, SCAP_MAX_PATH_SIZE) + 2; + (uint32_t)strnlen(fdi->info.unix_socket_info.fname, SCAP_MAX_PATH_SIZE) + 2; + break; + case SCAP_FD_FILE_V2: + res += sizeof(uint32_t) + // open_flags + (uint32_t)strnlen(fdi->info.regularinfo.fname, SCAP_MAX_PATH_SIZE) + 2 + + sizeof(uint32_t); // dev break; case SCAP_FD_FIFO: case SCAP_FD_FILE: @@ -204,7 +252,8 @@ uint32_t scap_fd_info_len(scap_fdinfo *fdi) case SCAP_FD_EVENTPOLL: case SCAP_FD_INOTIFY: case SCAP_FD_TIMERFD: - res += strnlen(fdi->info.fname, SCAP_MAX_PATH_SIZE) + 2; // 2 is the lenght field before the string + case SCAP_FD_NETLINK: + res += (uint32_t)strnlen(fdi->info.fname, SCAP_MAX_PATH_SIZE) + 2; // 2 is the length field before the string break; default: ASSERT(false); @@ -214,17 +263,20 @@ uint32_t scap_fd_info_len(scap_fdinfo *fdi) return res; } +int scap_dump_write(scap_dumper_t *d, void* buf, unsigned len); + // // Write the given fd info to disk // -int32_t scap_fd_write_to_disk(scap_t *handle, scap_fdinfo *fdi, FILE *f) +int32_t scap_fd_write_to_disk(scap_t *handle, scap_fdinfo *fdi, scap_dumper_t *d, uint32_t len) { uint8_t type = (uint8_t)fdi->type; uint16_t stlen; - if(fwrite(&(fdi->fd), sizeof(uint64_t), 1, f) != 1 || - fwrite(&(fdi->ino), sizeof(uint64_t), 1, f) != 1 || - fwrite(&(type), sizeof(uint8_t), 1, f) != 1) + if(scap_dump_write(d, &(len), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(fdi->fd), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(fdi->ino), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(type), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi1)"); return SCAP_FAILURE; @@ -233,58 +285,77 @@ int32_t scap_fd_write_to_disk(scap_t *handle, scap_fdinfo *fdi, FILE *f) switch(fdi->type) { case SCAP_FD_IPV4_SOCK: - if(fwrite(&(fdi->info.ipv4info.sip), sizeof(uint32_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv4info.dip), sizeof(uint32_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv4info.sport), sizeof(uint16_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv4info.dport), sizeof(uint16_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv4info.l4proto), sizeof(uint8_t), 1, f) != 1) + if(scap_dump_write(d, &(fdi->info.ipv4info.sip), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(fdi->info.ipv4info.dip), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(fdi->info.ipv4info.sport), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(fdi->info.ipv4info.dport), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(fdi->info.ipv4info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi2)"); return SCAP_FAILURE; } break; case SCAP_FD_IPV4_SERVSOCK: - if(fwrite(&(fdi->info.ipv4serverinfo.ip), sizeof(uint32_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv4serverinfo.port), sizeof(uint16_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv4serverinfo.l4proto), sizeof(uint8_t), 1, f) != 1) + if(scap_dump_write(d, &(fdi->info.ipv4serverinfo.ip), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(fdi->info.ipv4serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(fdi->info.ipv4serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi3)"); return SCAP_FAILURE; } break; case SCAP_FD_IPV6_SOCK: - if(fwrite((char*)fdi->info.ipv6info.sip, sizeof(uint32_t) * 4, 1, f) != 1 || - fwrite((char*)fdi->info.ipv6info.dip, sizeof(uint32_t) * 4, 1, f) != 1 || - fwrite(&(fdi->info.ipv6info.sport), sizeof(uint16_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv6info.dport), sizeof(uint16_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv6info.l4proto), sizeof(uint8_t), 1, f) != 1) + if(scap_dump_write(d, (char*)fdi->info.ipv6info.sip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 || + scap_dump_write(d, (char*)fdi->info.ipv6info.dip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 || + scap_dump_write(d, &(fdi->info.ipv6info.sport), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(fdi->info.ipv6info.dport), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(fdi->info.ipv6info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi7)"); } break; case SCAP_FD_IPV6_SERVSOCK: - if(fwrite(&(fdi->info.ipv6serverinfo.ip), sizeof(uint32_t) * 4, 1, f) != 1 || - fwrite(&(fdi->info.ipv6serverinfo.port), sizeof(uint16_t), 1, f) != 1 || - fwrite(&(fdi->info.ipv6serverinfo.l4proto), sizeof(uint8_t), 1, f) != 1) + if(scap_dump_write(d, &(fdi->info.ipv6serverinfo.ip), sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 || + scap_dump_write(d, &(fdi->info.ipv6serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(fdi->info.ipv6serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi8)"); } break; case SCAP_FD_UNIX_SOCK: - if(fwrite(&(fdi->info.unix_socket_info.source), sizeof(uint64_t), 1, f) != 1 || - fwrite(&(fdi->info.unix_socket_info.destination), sizeof(uint64_t), 1, f) != 1) + if(scap_dump_write(d, &(fdi->info.unix_socket_info.source), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(fdi->info.unix_socket_info.destination), sizeof(uint64_t)) != sizeof(uint64_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi4)"); return SCAP_FAILURE; } - stlen = strnlen(fdi->info.unix_socket_info.fname, SCAP_MAX_PATH_SIZE); - if(fwrite(&stlen, sizeof(uint16_t), 1, f) != 1 || - (stlen > 0 && fwrite(fdi->info.unix_socket_info.fname, stlen, 1, f) != 1)) + stlen = (uint16_t)strnlen(fdi->info.unix_socket_info.fname, SCAP_MAX_PATH_SIZE); + if(scap_dump_write(d, &stlen, sizeof(uint16_t)) != sizeof(uint16_t) || + (stlen > 0 && scap_dump_write(d, fdi->info.unix_socket_info.fname, stlen) != stlen)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi5)"); return SCAP_FAILURE; } break; + case SCAP_FD_FILE_V2: + if(scap_dump_write(d, &(fdi->info.regularinfo.open_flags), sizeof(uint32_t)) != sizeof(uint32_t)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi1)"); + return SCAP_FAILURE; + } + stlen = (uint16_t)strnlen(fdi->info.regularinfo.fname, SCAP_MAX_PATH_SIZE); + if(scap_dump_write(d, &stlen, sizeof(uint16_t)) != sizeof(uint16_t) || + (stlen > 0 && scap_dump_write(d, fdi->info.regularinfo.fname, stlen) != stlen)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi1)"); + return SCAP_FAILURE; + } + if(scap_dump_write(d, &(fdi->info.regularinfo.dev), sizeof(uint32_t)) != sizeof(uint32_t)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (dev)"); + return SCAP_FAILURE; + } + break; case SCAP_FD_FIFO: case SCAP_FD_FILE: case SCAP_FD_DIRECTORY: @@ -294,15 +365,21 @@ int32_t scap_fd_write_to_disk(scap_t *handle, scap_fdinfo *fdi, FILE *f) case SCAP_FD_EVENTPOLL: case SCAP_FD_INOTIFY: case SCAP_FD_TIMERFD: - stlen = strnlen(fdi->info.fname, SCAP_MAX_PATH_SIZE); - if(fwrite(&stlen, sizeof(uint16_t), 1, f) != 1 || - (stlen > 0 && fwrite(fdi->info.fname, stlen, 1, f) != 1)) + case SCAP_FD_NETLINK: + stlen = (uint16_t)strnlen(fdi->info.fname, SCAP_MAX_PATH_SIZE); + if(scap_dump_write(d, &stlen, sizeof(uint16_t)) != sizeof(uint16_t) || + (stlen > 0 && scap_dump_write(d, fdi->info.fname, stlen) != stlen)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi6)"); return SCAP_FAILURE; } break; + case SCAP_FD_UNKNOWN: + // Ignore UNKNOWN fds without failing + ASSERT(false); + break; default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Unknown fdi type %d", fdi->type); ASSERT(false); return SCAP_FAILURE; } @@ -310,21 +387,21 @@ int32_t scap_fd_write_to_disk(scap_t *handle, scap_fdinfo *fdi, FILE *f) return SCAP_SUCCESS; } -uint32_t scap_fd_read_prop_from_disk(scap_t *handle, OUT void *target, size_t expected_size, OUT size_t *nbytes, FILE *f) +uint32_t scap_fd_read_prop_from_disk(scap_t *handle, OUT void *target, size_t expected_size, OUT size_t *nbytes, gzFile f) { size_t readsize; - readsize = fread(target, 1, expected_size, f); + readsize = gzread(f, target, (unsigned int)expected_size); CHECK_READ_SIZE(readsize, expected_size); (*nbytes) += readsize; return SCAP_SUCCESS; } -uint32_t scap_fd_read_fname_from_disk(scap_t* handle, char* fname,OUT size_t* nbytes,FILE* f) +uint32_t scap_fd_read_fname_from_disk(scap_t* handle, char* fname,OUT size_t* nbytes, gzFile f) { size_t readsize; uint16_t stlen; - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); + readsize = gzread(f, &(stlen), sizeof(uint16_t)); CHECK_READ_SIZE(readsize, sizeof(uint16_t)); if(stlen >= SCAP_MAX_PATH_SIZE) @@ -335,7 +412,7 @@ uint32_t scap_fd_read_fname_from_disk(scap_t* handle, char* fname,OUT size_t* nb (*nbytes) += readsize; - readsize = fread(fname, 1, stlen, f); + readsize = gzread(f, fname, stlen); CHECK_READ_SIZE(readsize, stlen); (*nbytes) += stlen; @@ -349,29 +426,43 @@ uint32_t scap_fd_read_fname_from_disk(scap_t* handle, char* fname,OUT size_t* nb // Populate the given fd by reading the info from disk // Returns the number of read bytes. // -uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t *nbytes, FILE *f) +uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t *nbytes, uint32_t block_type, gzFile f) { uint8_t type; + uint32_t toread; + int fseekres; + uint32_t sub_len = 0; uint32_t res = SCAP_SUCCESS; *nbytes = 0; - if(scap_fd_read_prop_from_disk(handle, &(fdi->fd), sizeof(fdi->fd), nbytes, f) || + if((block_type == FDL_BLOCK_TYPE_V2 && scap_fd_read_prop_from_disk(handle, &sub_len, sizeof(uint32_t), nbytes, f)) || + scap_fd_read_prop_from_disk(handle, &(fdi->fd), sizeof(fdi->fd), nbytes, f) || scap_fd_read_prop_from_disk(handle, &(fdi->ino), sizeof(fdi->ino), nbytes, f) || scap_fd_read_prop_from_disk(handle, &type, sizeof(uint8_t), nbytes, f)) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read prop block for fd"); return SCAP_FAILURE; } + // If new parameters are added, sub_len can be used to + // see if they are available in the current capture. + // For example, for a 32bit parameter: + // + // if(sub_len && (*nbytes + sizeof(uint32_t)) <= sub_len) + // { + // ... + // } + fdi->type = (scap_fd_type)type; switch(fdi->type) { case SCAP_FD_IPV4_SOCK: - if(fread(&(fdi->info.ipv4info.sip), 1, sizeof(uint32_t), f) != sizeof(uint32_t) || - fread(&(fdi->info.ipv4info.dip), 1, sizeof(uint32_t), f) != sizeof(uint32_t) || - fread(&(fdi->info.ipv4info.sport), 1, sizeof(uint16_t), f) != sizeof(uint16_t) || - fread(&(fdi->info.ipv4info.dport), 1, sizeof(uint16_t), f) != sizeof(uint16_t) || - fread(&(fdi->info.ipv4info.l4proto), 1, sizeof(uint8_t), f) != sizeof(uint8_t)) + if(gzread(f, &(fdi->info.ipv4info.sip), sizeof(uint32_t)) != sizeof(uint32_t) || + gzread(f, &(fdi->info.ipv4info.dip), sizeof(uint32_t)) != sizeof(uint32_t) || + gzread(f, &(fdi->info.ipv4info.sport), sizeof(uint16_t)) != sizeof(uint16_t) || + gzread(f, &(fdi->info.ipv4info.dport), sizeof(uint16_t)) != sizeof(uint16_t) || + gzread(f, &(fdi->info.ipv4info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (1)"); return SCAP_FAILURE; @@ -381,9 +472,9 @@ uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t break; case SCAP_FD_IPV4_SERVSOCK: - if(fread(&(fdi->info.ipv4serverinfo.ip), 1, sizeof(uint32_t), f) != sizeof(uint32_t) || - fread(&(fdi->info.ipv4serverinfo.port), 1, sizeof(uint16_t), f) != sizeof(uint16_t) || - fread(&(fdi->info.ipv4serverinfo.l4proto), 1, sizeof(uint8_t), f) != sizeof(uint8_t)) + if(gzread(f, &(fdi->info.ipv4serverinfo.ip), sizeof(uint32_t)) != sizeof(uint32_t) || + gzread(f, &(fdi->info.ipv4serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) || + gzread(f, &(fdi->info.ipv4serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (2)"); return SCAP_FAILURE; @@ -392,11 +483,11 @@ uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t (*nbytes) += (sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t)); break; case SCAP_FD_IPV6_SOCK: - if(fread((char*)fdi->info.ipv6info.sip, 1, sizeof(uint32_t) * 4, f) != sizeof(uint32_t) * 4 || - fread((char*)fdi->info.ipv6info.dip, 1, sizeof(uint32_t) * 4, f) != sizeof(uint32_t) * 4 || - fread(&(fdi->info.ipv6info.sport), 1, sizeof(uint16_t), f) != sizeof(uint16_t) || - fread(&(fdi->info.ipv6info.dport), 1, sizeof(uint16_t), f) != sizeof(uint16_t) || - fread(&(fdi->info.ipv6info.l4proto), 1, sizeof(uint8_t), f) != sizeof(uint8_t)) + if(gzread(f, (char*)fdi->info.ipv6info.sip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 || + gzread(f, (char*)fdi->info.ipv6info.dip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 || + gzread(f, &(fdi->info.ipv6info.sport), sizeof(uint16_t)) != sizeof(uint16_t) || + gzread(f, &(fdi->info.ipv6info.dport), sizeof(uint16_t)) != sizeof(uint16_t) || + gzread(f, &(fdi->info.ipv6info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi3)"); } @@ -407,9 +498,9 @@ uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t sizeof(uint8_t)); // l4proto break; case SCAP_FD_IPV6_SERVSOCK: - if(fread((char*)fdi->info.ipv6serverinfo.ip, 1, sizeof(uint32_t) * 4, f) != sizeof(uint32_t) * 4|| - fread(&(fdi->info.ipv6serverinfo.port), 1, sizeof(uint16_t), f) != sizeof(uint16_t) || - fread(&(fdi->info.ipv6serverinfo.l4proto), 1, sizeof(uint8_t), f) != sizeof(uint8_t)) + if(gzread(f, (char*)fdi->info.ipv6serverinfo.ip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4|| + gzread(f, &(fdi->info.ipv6serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) || + gzread(f, &(fdi->info.ipv6serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi4)"); } @@ -418,15 +509,35 @@ uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t sizeof(uint8_t)); // l4proto break; case SCAP_FD_UNIX_SOCK: - if(fread(&(fdi->info.unix_socket_info.source), 1, sizeof(uint64_t), f) != sizeof(uint64_t) || - fread(&(fdi->info.unix_socket_info.destination), 1, sizeof(uint64_t), f) != sizeof(uint64_t)) + if(gzread(f, &(fdi->info.unix_socket_info.source), sizeof(uint64_t)) != sizeof(uint64_t) || + gzread(f, &(fdi->info.unix_socket_info.destination), sizeof(uint64_t)) != sizeof(uint64_t)) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (fi5)"); return SCAP_FAILURE; } (*nbytes) += (sizeof(uint64_t) + sizeof(uint64_t)); - res = scap_fd_read_fname_from_disk(handle, fdi->info.unix_socket_info.fname,nbytes,f); + res = scap_fd_read_fname_from_disk(handle, fdi->info.unix_socket_info.fname, nbytes, f); + break; + case SCAP_FD_FILE_V2: + if(gzread(f, &(fdi->info.regularinfo.open_flags), sizeof(uint32_t)) != sizeof(uint32_t)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (fi1)"); + return SCAP_FAILURE; + } + + (*nbytes) += sizeof(uint32_t); + res = scap_fd_read_fname_from_disk(handle, fdi->info.regularinfo.fname, nbytes, f); + if (!sub_len || (sub_len < *nbytes + sizeof(uint32_t))) + { + break; + } + if(gzread(f, &(fdi->info.regularinfo.dev), sizeof(uint32_t)) != sizeof(uint32_t)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (dev)"); + return SCAP_FAILURE; + } + (*nbytes) += sizeof(uint32_t); break; case SCAP_FD_FIFO: case SCAP_FD_FILE: @@ -437,15 +548,56 @@ uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t case SCAP_FD_EVENTPOLL: case SCAP_FD_INOTIFY: case SCAP_FD_TIMERFD: + case SCAP_FD_NETLINK: res = scap_fd_read_fname_from_disk(handle, fdi->info.fname,nbytes,f); break; + case SCAP_FD_UNKNOWN: + ASSERT(false); + break; default: snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file, wrong fd type %u", (uint32_t)fdi->type); return SCAP_FAILURE; } + + if(sub_len && *nbytes != sub_len) + { + if(*nbytes > sub_len) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Had read %zu bytes, but fdlist entry have length %u.", + *nbytes, sub_len); + return SCAP_FAILURE; + } + toread = sub_len - *nbytes; + fseekres = (int)gzseek(f, (long)toread, SEEK_CUR); + if(fseekres == -1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't skip %u bytes.", + (unsigned int)toread); + return SCAP_FAILURE; + } + *nbytes = sub_len; + } + return res; } +void scap_fd_free_ns_sockets_list(scap_t *handle, struct scap_ns_socket_list **sockets) +{ + struct scap_ns_socket_list *fdi; + struct scap_ns_socket_list *tfdi; + + if(*sockets) + { + HASH_ITER(hh, *sockets, fdi, tfdi) + { + HASH_DEL(*sockets, fdi); + scap_fd_free_table(handle, &fdi->sockets); + free(fdi); + } + *sockets = NULL; + } +} + void scap_fd_free_table(scap_t *handle, scap_fdinfo **fds) { struct scap_fdinfo *fdi; @@ -488,7 +640,7 @@ void scap_fd_remove(scap_t *handle, scap_threadinfo *tinfo, int64_t fd) // Looks like there's no fd to remove. // Likely, the fd creation event was dropped. // - //scap_proc_print_info(tinfo); + //scap_proc_print_info(handle, tinfo); // ASSERT(false); return; } @@ -501,7 +653,7 @@ void scap_fd_remove(scap_t *handle, scap_threadinfo *tinfo, int64_t fd) // Add the file descriptor info pointed by fdi to the fd table for process tinfo. // Note: silently skips if fdi->type is SCAP_FD_UNKNOWN. // -int32_t scap_add_fd_to_proc_table(scap_t *handle, scap_threadinfo *tinfo, scap_fdinfo *fdi) +int32_t scap_add_fd_to_proc_table(scap_t *handle, scap_threadinfo *tinfo, scap_fdinfo *fdi, char *error) { int32_t uth_status = SCAP_SUCCESS; scap_fdinfo *tfdi; @@ -522,37 +674,71 @@ int32_t scap_add_fd_to_proc_table(scap_t *handle, scap_threadinfo *tinfo, scap_f // HASH_DEL(tinfo->fdlist, tfdi); free(tfdi); - - // ASSERT(false); - // snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "duplicate fd %"PRIu64"for process%"PRIu64, fdi->fd, tinfo->tid); - // return SCAP_FAILURE; } // - // Add the fd to the table + // Add the fd to the table, or fire the notification callback // - HASH_ADD_INT64(tinfo->fdlist, fd, fdi); - if(uth_status != SCAP_SUCCESS) + if(handle->m_proc_callback == NULL) { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (2)"); - return SCAP_FAILURE; + HASH_ADD_INT64(tinfo->fdlist, fd, fdi); + if(uth_status != SCAP_SUCCESS) + { + snprintf(error, SCAP_LASTERR_SIZE, "process table allocation error (2)"); + return SCAP_FAILURE; + } + } + else + { + handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo->tid, tinfo, fdi); } return SCAP_SUCCESS; } -#if !defined(_WIN32) && !defined(__APPLE__) +// +// Delete a device entry +// +void scap_dev_delete(scap_t* handle, scap_mountinfo* dev) +{ + // + // First, remove the process descriptor from the table + // + HASH_DEL(handle->m_dev_list, dev); + + // + // Second, free the memory + // + free(dev); +} + +// +// Free the device table +// +void scap_free_device_table(scap_t* handle) +{ + scap_mountinfo *dev, *tdev; + + HASH_ITER(hh, handle->m_dev_list, dev, tdev) + { + scap_dev_delete(handle, dev); + } +} + +#if defined(HAS_CAPTURE) int32_t scap_fd_handle_pipe(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, char *error) { - char link_name[1024]; + char link_name[SCAP_MAX_PATH_SIZE]; ssize_t r; uint64_t ino; struct stat sb; - r = readlink(fname, link_name, 1024); + r = readlink(fname, link_name, SCAP_MAX_PATH_SIZE); if (r <= 0) { + snprintf(error, SCAP_LASTERR_SIZE, "Could not read link %s (%s)", + fname, scap_strerror(handle, errno)); return SCAP_FAILURE; } link_name[r] = '\0'; @@ -569,16 +755,187 @@ int32_t scap_fd_handle_pipe(scap_t *handle, char *fname, scap_threadinfo *tinfo, strncpy(fdi->info.fname, link_name, SCAP_MAX_PATH_SIZE); fdi->ino = ino; - return scap_add_fd_to_proc_table(handle, tinfo, fdi); + return scap_add_fd_to_proc_table(handle, tinfo, fdi, error); +} + +static inline uint32_t open_flags_to_scap(unsigned long flags) +{ + uint32_t res = 0; + + switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) { + case O_WRONLY: + res |= PPM_O_WRONLY; + break; + case O_RDWR: + res |= PPM_O_RDWR; + break; + default: + res |= PPM_O_RDONLY; + break; + } + + if (flags & O_CREAT) + res |= PPM_O_CREAT; + + if (flags & O_TMPFILE) + res |= PPM_O_TMPFILE; + + if (flags & O_APPEND) + res |= PPM_O_APPEND; + +#ifdef O_DSYNC + if (flags & O_DSYNC) + res |= PPM_O_DSYNC; +#endif + + if (flags & O_EXCL) + res |= PPM_O_EXCL; + + if (flags & O_NONBLOCK) + res |= PPM_O_NONBLOCK; + + if (flags & O_SYNC) + res |= PPM_O_SYNC; + + if (flags & O_TRUNC) + res |= PPM_O_TRUNC; + +#ifdef O_DIRECT + if (flags & O_DIRECT) + res |= PPM_O_DIRECT; +#endif + +#ifdef O_DIRECTORY + if (flags & O_DIRECTORY) + res |= PPM_O_DIRECTORY; +#endif + +#ifdef O_LARGEFILE + if (flags & O_LARGEFILE) + res |= PPM_O_LARGEFILE; +#endif + +#ifdef O_CLOEXEC + if (flags & O_CLOEXEC) + res |= PPM_O_CLOEXEC; +#endif + + return res; +} + +uint32_t scap_get_device_by_mount_id(scap_t *handle, const char *procdir, unsigned long requested_mount_id) +{ + char fd_dir_name[SCAP_MAX_PATH_SIZE]; + char line[SCAP_MAX_PATH_SIZE]; + FILE *finfo; + scap_mountinfo *mountinfo; + + HASH_FIND_INT64(handle->m_dev_list, &requested_mount_id, mountinfo); + if(mountinfo != NULL) + { + return mountinfo->dev; + } + + snprintf(fd_dir_name, SCAP_MAX_PATH_SIZE, "%smountinfo", procdir); + finfo = fopen(fd_dir_name, "r"); + if(finfo == NULL) + { + return 0; + } + + while(fgets(line, sizeof(line), finfo) != NULL) + { + uint32_t mount_id, major, minor; + if(sscanf(line, "%u %*u %u:%u", &mount_id, &major, &minor) != 3) + { + continue; + } + + if(mount_id == requested_mount_id) + { + uint32_t dev = makedev(major, minor); + mountinfo = malloc(sizeof(*mountinfo)); + if(mountinfo) + { + int32_t uth_status = SCAP_SUCCESS; + mountinfo->mount_id = mount_id; + mountinfo->dev = dev; + HASH_ADD_INT64(handle->m_dev_list, mount_id, mountinfo); + if(uth_status != SCAP_SUCCESS) + { + free(mountinfo); + } + } + fclose(finfo); + return dev; + } + } + fclose(finfo); + return 0; +} + +void scap_fd_flags_file(scap_t *handle, scap_fdinfo *fdi, const char *procdir) +{ + char fd_dir_name[SCAP_MAX_PATH_SIZE]; + char line[SCAP_MAX_PATH_SIZE]; + FILE *finfo; + + snprintf(fd_dir_name, SCAP_MAX_PATH_SIZE, "%sfdinfo/%" PRId64, procdir, fdi->fd); + finfo = fopen(fd_dir_name, "r"); + if(finfo == NULL) + { + return; + } + fdi->info.regularinfo.mount_id = 0; + fdi->info.regularinfo.dev = 0; + + while(fgets(line, sizeof(line), finfo) != NULL) + { + // We are interested in the flags and the mnt_id. + // + // The format of the file is: + // pos: XXXX + // flags: YYYYYYYY + // mnt_id: ZZZ + + if(!strncmp(line, "flags:\t", sizeof("flags:\t") - 1)) + { + uint32_t open_flags; + errno = 0; + unsigned long flags = strtoul(line + sizeof("flags:\t") - 1, NULL, 8); + + if(errno == ERANGE) + { + open_flags = PPM_O_NONE; + } + else + { + open_flags = open_flags_to_scap(flags); + } + + fdi->info.regularinfo.open_flags = open_flags; + } + else if(!strncmp(line, "mnt_id:\t", sizeof("mnt_id:\t") - 1)) + { + errno = 0; + unsigned long mount_id = strtoul(line + sizeof("mnt_id:\t") - 1, NULL, 10); + + if(errno != ERANGE) + { + fdi->info.regularinfo.mount_id = mount_id; + } + } + } + + fclose(finfo); } -int32_t scap_fd_handle_regular_file(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, char *error) +int32_t scap_fd_handle_regular_file(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, const char *procdir, char *error) { - char link_name[1024]; + char link_name[SCAP_MAX_PATH_SIZE]; ssize_t r; - int32_t res; - r = readlink(fname, link_name, 1024); + r = readlink(fname, link_name, SCAP_MAX_PATH_SIZE); if (r <= 0) { return SCAP_SUCCESS; @@ -614,31 +971,65 @@ int32_t scap_fd_handle_regular_file(scap_t *handle, char *fname, scap_threadinfo if(SCAP_FD_UNSUPPORTED == fdi->type) { // still not able to classify -// printf("unsupported %s -> %s\n",fname,link_name); + // printf("unsupported %s -> %s\n",fname,link_name); } fdi->info.fname[0] = '\0'; - } else { + } + else if(fdi->type == SCAP_FD_FILE_V2) + { + scap_fd_flags_file(handle, fdi, procdir); + strncpy(fdi->info.regularinfo.fname, link_name, SCAP_MAX_PATH_SIZE); + } + else + { strncpy(fdi->info.fname, link_name, SCAP_MAX_PATH_SIZE); } - res = scap_add_fd_to_proc_table(handle, tinfo, fdi); - return res; + return scap_add_fd_to_proc_table(handle, tinfo, fdi, error); } -int32_t scap_fd_handle_socket(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, scap_fdinfo *sockets, char *error) +int32_t scap_fd_handle_socket(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, char* procdir, uint64_t net_ns, struct scap_ns_socket_list **sockets_by_ns, char *error) { - char link_name[1024]; + char link_name[SCAP_MAX_PATH_SIZE]; ssize_t r; scap_fdinfo *tfdi; uint64_t ino; + struct scap_ns_socket_list* sockets = NULL; + int32_t uth_status = SCAP_SUCCESS; - if(sockets == NULL) + if(*sockets_by_ns == (void*)-1) { return SCAP_SUCCESS; } + else + { + HASH_FIND_INT64(*sockets_by_ns, &net_ns, sockets); + if(sockets == NULL) + { + sockets = malloc(sizeof(struct scap_ns_socket_list)); + sockets->net_ns = net_ns; + sockets->sockets = NULL; + char fd_error[SCAP_LASTERR_SIZE]; - r = readlink(fname, link_name, 1024); - if (r <= 0) + HASH_ADD_INT64(*sockets_by_ns, net_ns, sockets); + if(uth_status != SCAP_SUCCESS) + { + snprintf(error, SCAP_LASTERR_SIZE, "socket list allocation error"); + free(sockets); + return SCAP_FAILURE; + } + + if(scap_fd_read_sockets(handle, procdir, sockets, fd_error) == SCAP_FAILURE) + { + snprintf(error, SCAP_LASTERR_SIZE, "Cannot read sockets (%s)", fd_error); + sockets->sockets = NULL; + return SCAP_FAILURE; + } + } + } + + r = readlink(fname, link_name, SCAP_MAX_PATH_SIZE); + if(r <= 0) { return SCAP_SUCCESS; } @@ -652,19 +1043,19 @@ int32_t scap_fd_handle_socket(scap_t *handle, char *fname, scap_threadinfo *tinf { // it's a kind of socket, but we don't support it right now fdi->type = SCAP_FD_UNSUPPORTED; - return scap_add_fd_to_proc_table(handle, tinfo, fdi); + return scap_add_fd_to_proc_table(handle, tinfo, fdi, error); } // // Lookup ino in the list of sockets // - HASH_FIND_INT64(sockets, &ino, tfdi); + HASH_FIND_INT64(sockets->sockets, &ino, tfdi); if(tfdi != NULL) { memcpy(&(fdi->info), &(tfdi->info), sizeof(fdi->info)); fdi->ino = ino; fdi->type = tfdi->type; - return scap_add_fd_to_proc_table(handle, tinfo, fdi); + return scap_add_fd_to_proc_table(handle, tinfo, fdi, error); } else { @@ -672,22 +1063,28 @@ int32_t scap_fd_handle_socket(scap_t *handle, char *fname, scap_threadinfo *tinf } } -int32_t scap_fd_read_unix_sockets_from_proc_fs(scap_t *handle, scap_fdinfo **sockets) +int32_t scap_fd_read_unix_sockets_from_proc_fs(scap_t *handle, const char* filename, scap_fdinfo **sockets) { FILE *f; - char line[1024]; + char line[SCAP_MAX_PATH_SIZE]; int first_line = false; char *delimiters = " \t"; char *token; int32_t uth_status = SCAP_SUCCESS; - f = fopen("/proc/net/unix", "r"); + + f = fopen(filename, "r"); if(NULL == f) { ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not open sockets file %s (%s)", + filename, + scap_strerror(handle, errno)); return SCAP_FAILURE; } while(NULL != fgets(line, sizeof(line), f)) { + char *scratch; + // skip the first line ... contains field names if(!first_line) { @@ -702,34 +1099,75 @@ int32_t scap_fd_read_unix_sockets_from_proc_fs(scap_t *handle, scap_fdinfo **soc // parse the fields // // 1. Num - token = strtok(line, delimiters); - ASSERT(NULL != token); + token = strtok_r(line, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } - fdinfo->info.unix_socket_info.source = strtoul(token,NULL,16); + fdinfo->info.unix_socket_info.source = strtoul(token, NULL, 16); fdinfo->info.unix_socket_info.destination = 0; + // 2. RefCount - token = strtok(NULL, delimiters); - ASSERT(NULL != token); + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + // 3. Protocol - token = strtok(NULL, delimiters); - ASSERT(NULL != token); + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + // 4. Flags - token = strtok(NULL, delimiters); - ASSERT(NULL != token); + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + // 5. Type - token = strtok(NULL, delimiters); - ASSERT(NULL != token); + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + // 6. St - token = strtok(NULL, delimiters); - ASSERT(NULL != token); - + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + // 7. Inode - token = strtok(NULL, delimiters); - ASSERT(NULL != token); + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + sscanf(token, "%"PRIu64, &(fdinfo->ino)); // 8. Path - token = strtok(NULL, delimiters); + token = strtok_r(NULL, delimiters, &scratch); if(NULL != token) { strncpy(fdinfo->info.unix_socket_info.fname, token, SCAP_MAX_PATH_SIZE); @@ -742,15 +1180,162 @@ int32_t scap_fd_read_unix_sockets_from_proc_fs(scap_t *handle, scap_fdinfo **soc HASH_ADD_INT64((*sockets), ino, fdinfo); if(uth_status != SCAP_SUCCESS) { - // TODO: set some error message - break; + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "unix socket allocation error"); + fclose(f); + free(fdinfo); + return SCAP_FAILURE; } } fclose(f); return uth_status; } -int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t *handle, char *dir, int l4proto, scap_fdinfo **sockets) +//sk Eth Pid Groups Rmem Wmem Dump Locks Drops Inode +//ffff88011abfb000 0 0 00000000 0 0 0 2 0 13 + +int32_t scap_fd_read_netlink_sockets_from_proc_fs(scap_t *handle, const char* filename, scap_fdinfo **sockets) +{ + FILE *f; + char line[SCAP_MAX_PATH_SIZE]; + int first_line = false; + char *delimiters = " \t"; + char *token; + int32_t uth_status = SCAP_SUCCESS; + + f = fopen(filename, "r"); + if(NULL == f) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not open netlink sockets file %s (%s)", + filename, + scap_strerror(handle, errno)); + + return SCAP_FAILURE; + } + while(NULL != fgets(line, sizeof(line), f)) + { + char *scratch; + + // skip the first line ... contains field names + if(!first_line) + { + first_line = true; + continue; + } + scap_fdinfo *fdinfo = malloc(sizeof(scap_fdinfo)); + memset(fdinfo, 0, sizeof(scap_fdinfo)); + fdinfo->type = SCAP_FD_UNIX_SOCK; + + + // + // parse the fields + // + // 1. Num + token = strtok_r(line, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 2. Eth + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 3. Pid + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 4. Groups + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 5. Rmem + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 6. Wmem + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 7. Dump + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 8. Locks + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 9. Drops + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + // 10. Inode + token = strtok_r(NULL, delimiters, &scratch); + if(token == NULL) + { + ASSERT(false); + free(fdinfo); + continue; + } + + sscanf(token, "%"PRIu64, &(fdinfo->ino)); + + HASH_ADD_INT64((*sockets), ino, fdinfo); + if(uth_status != SCAP_SUCCESS) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "netlink socket allocation error"); + fclose(f); + free(fdinfo); + return SCAP_FAILURE; + } + } + fclose(f); + return uth_status; +} + +int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t *handle, const char *dir, int l4proto, scap_fdinfo **sockets) { FILE *f; int32_t uth_status = SCAP_SUCCESS; @@ -766,7 +1351,7 @@ int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 if(scan_buf == NULL) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scan_buf allocation error"); - return SCAP_FAILURE; + return SCAP_FAILURE; } f = fopen(dir, "r"); @@ -774,9 +1359,12 @@ int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 { ASSERT(false); free(scan_buf); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not open ipv4 sockets dir %s (%s)", + dir, + scap_strerror(handle, errno)); return SCAP_FAILURE; } - + while((rsize = fread(scan_buf, 1, SOCKET_SCAN_BUFFER_SIZE, f)) != 0) { char* scan_end = scan_buf + rsize; @@ -792,7 +1380,7 @@ int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 } scap_fdinfo *fdinfo = malloc(sizeof(scap_fdinfo)); - + // // Skip the sl field // @@ -909,11 +1497,12 @@ int32_t scap_fd_read_ipv4_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 if(uth_status != SCAP_SUCCESS) { uth_status = SCAP_FAILURE; - // TODO: set some error message + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ipv4 socket allocation error"); + free(fdinfo); break; } - scan_pos++; + scan_pos++; } } @@ -943,7 +1532,7 @@ int32_t scap_fd_read_ipv6_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 if(scan_buf == NULL) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scan_buf allocation error"); - return SCAP_FAILURE; + return SCAP_FAILURE; } f = fopen(dir, "r"); @@ -952,6 +1541,9 @@ int32_t scap_fd_read_ipv6_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 { ASSERT(false); free(scan_buf); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not open ipv6 sockets dir %s (%s)", + dir, + scap_strerror(handle, errno)); return SCAP_FAILURE; } @@ -970,7 +1562,7 @@ int32_t scap_fd_read_ipv6_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 } scap_fdinfo *fdinfo = malloc(sizeof(scap_fdinfo)); - + // // Skip the sl field // @@ -1128,11 +1720,11 @@ int32_t scap_fd_read_ipv6_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 if(uth_status != SCAP_SUCCESS) { uth_status = SCAP_FAILURE; - // TODO: set some error message + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ipv6 socket allocation error"); break; } - scan_pos++; + scan_pos++; } } @@ -1142,19 +1734,94 @@ int32_t scap_fd_read_ipv6_sockets_from_proc_fs(scap_t *handle, char *dir, int l4 return uth_status; } -int32_t scap_fd_read_sockets(scap_t *handle, scap_fdinfo **sockets) +int32_t scap_fd_read_sockets(scap_t *handle, char* procdir, struct scap_ns_socket_list *sockets, char *error) { - if(SCAP_FAILURE == scap_fd_read_ipv4_sockets_from_proc_fs(handle, "/proc/net/tcp", SCAP_L4_TCP, sockets) || - SCAP_FAILURE == scap_fd_read_ipv4_sockets_from_proc_fs(handle, "/proc/net/udp", SCAP_L4_UDP, sockets) || - SCAP_FAILURE == scap_fd_read_ipv4_sockets_from_proc_fs(handle, "/proc/net/raw", SCAP_L4_RAW, sockets) || - SCAP_FAILURE == scap_fd_read_ipv6_sockets_from_proc_fs(handle, "/proc/net/tcp6", SCAP_L4_TCP, sockets) || - SCAP_FAILURE == scap_fd_read_ipv6_sockets_from_proc_fs(handle, "/proc/net/udp6", SCAP_L4_UDP, sockets) || - SCAP_FAILURE == scap_fd_read_ipv6_sockets_from_proc_fs(handle, "/proc/net/raw6", SCAP_L4_RAW, sockets) || - SCAP_FAILURE == scap_fd_read_unix_sockets_from_proc_fs(handle, sockets)) + char filename[SCAP_MAX_PATH_SIZE]; + char netroot[SCAP_MAX_PATH_SIZE]; + + if(sockets->net_ns) + { + // + // Namespace support, look in /proc/PID/net/ + // + snprintf(netroot, sizeof(netroot), "%snet/", procdir); + } + else + { + // + // No namespace support, look in the base /proc + // + snprintf(netroot, sizeof(netroot), "%s/proc/net/", scap_get_host_root()); + } + + snprintf(filename, sizeof(filename), "%stcp", netroot); + if(scap_fd_read_ipv4_sockets_from_proc_fs(handle, filename, SCAP_L4_TCP, &sockets->sockets) == SCAP_FAILURE) + { + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read ipv4 tcp sockets (%s)", handle->m_lasterr); + return SCAP_FAILURE; + } + + snprintf(filename, sizeof(filename), "%sudp", netroot); + if(scap_fd_read_ipv4_sockets_from_proc_fs(handle, filename, SCAP_L4_UDP, &sockets->sockets) == SCAP_FAILURE) + { + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read ipv4 udp sockets (%s)", handle->m_lasterr); + return SCAP_FAILURE; + } + + snprintf(filename, sizeof(filename), "%sraw", netroot); + if(scap_fd_read_ipv4_sockets_from_proc_fs(handle, filename, SCAP_L4_RAW, &sockets->sockets) == SCAP_FAILURE) { - scap_fd_free_table(handle, sockets); + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read ipv4 raw sockets (%s)", handle->m_lasterr); return SCAP_FAILURE; } + + snprintf(filename, sizeof(filename), "%sunix", netroot); + if(scap_fd_read_unix_sockets_from_proc_fs(handle, filename, &sockets->sockets) == SCAP_FAILURE) + { + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read unix sockets (%s)", handle->m_lasterr); + return SCAP_FAILURE; + } + + snprintf(filename, sizeof(filename), "%snetlink", netroot); + if(scap_fd_read_netlink_sockets_from_proc_fs(handle, filename, &sockets->sockets) == SCAP_FAILURE) + { + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read netlink sockets (%s)", handle->m_lasterr); + return SCAP_FAILURE; + } + + snprintf(filename, sizeof(filename), "%stcp6", netroot); + /* We assume if there is /proc/net/tcp6 that ipv6 is available */ + if(access(filename, R_OK) == 0) + { + if(scap_fd_read_ipv6_sockets_from_proc_fs(handle, filename, SCAP_L4_TCP, &sockets->sockets) == SCAP_FAILURE) + { + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read ipv6 tcp sockets (%s)", handle->m_lasterr); + return SCAP_FAILURE; + } + + snprintf(filename, sizeof(filename), "%sudp6", netroot); + if(scap_fd_read_ipv6_sockets_from_proc_fs(handle, filename, SCAP_L4_UDP, &sockets->sockets) == SCAP_FAILURE) + { + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read ipv6 udp sockets (%s)", handle->m_lasterr); + return SCAP_FAILURE; + } + + snprintf(filename, sizeof(filename), "%sraw6", netroot); + if(scap_fd_read_ipv6_sockets_from_proc_fs(handle, filename, SCAP_L4_RAW, &sockets->sockets) == SCAP_FAILURE) + { + scap_fd_free_table(handle, &sockets->sockets); + snprintf(error, SCAP_LASTERR_SIZE, "Could not read ipv6 raw sockets (%s)", handle->m_lasterr); + return SCAP_FAILURE; + } + } + return SCAP_SUCCESS; } @@ -1213,18 +1880,22 @@ char * decode_st_mode(struct stat* sb) // // Scan the directory containing the fd's of a proc /proc/x/fd // -int32_t scap_fd_scan_fd_dir(scap_t *handle, char *procdir, scap_threadinfo *tinfo, scap_fdinfo *sockets, char *error) +int32_t scap_fd_scan_fd_dir(scap_t *handle, char *procdir, scap_threadinfo *tinfo, struct scap_ns_socket_list **sockets_by_ns, char *error) { DIR *dir_p; struct dirent *dir_entry_p; int32_t res = SCAP_SUCCESS; - char fd_dir_name[1024]; - char f_name[1024]; + char fd_dir_name[SCAP_MAX_PATH_SIZE]; + char f_name[SCAP_MAX_PATH_SIZE]; + char link_name[SCAP_MAX_PATH_SIZE]; struct stat sb; uint64_t fd; scap_fdinfo *fdi = NULL; + uint64_t net_ns; + ssize_t r; + uint16_t fd_added = 0; - snprintf(fd_dir_name, 1024, "%sfd", procdir); + snprintf(fd_dir_name, SCAP_MAX_PATH_SIZE, "%sfd", procdir); dir_p = opendir(fd_dir_name); if(dir_p == NULL) { @@ -1232,95 +1903,136 @@ int32_t scap_fd_scan_fd_dir(scap_t *handle, char *procdir, scap_threadinfo *tinf return SCAP_NOTFOUND; } - while((dir_entry_p = readdir(dir_p)) != NULL) + // + // Get the network namespace of the process + // + snprintf(f_name, sizeof(f_name), "%sns/net", procdir); + r = readlink(f_name, link_name, sizeof(link_name)); + if(r <= 0) + { + // + // No network namespace available. Assume global + // + net_ns = 0; + } + else + { + link_name[r] = '\0'; + sscanf(link_name, "net:[%"PRIi64"]", &net_ns); + } + + while((dir_entry_p = readdir(dir_p)) != NULL && + (handle->m_fd_lookup_limit == 0 || fd_added < handle->m_fd_lookup_limit)) { fdi = NULL; - snprintf(f_name, 1024, "%s/%s", fd_dir_name, dir_entry_p->d_name); + snprintf(f_name, SCAP_MAX_PATH_SIZE, "%s/%s", fd_dir_name, dir_entry_p->d_name); if(-1 == stat(f_name, &sb) || 1 != sscanf(dir_entry_p->d_name, "%"PRIu64, &fd)) { continue; } + + // In no driver mode to limit cpu usage we just parse sockets + // because we are interested only on them + if(handle->m_mode == SCAP_MODE_NODRIVER && !S_ISSOCK(sb.st_mode)) + { + continue; + } + switch(sb.st_mode & S_IFMT) { case S_IFIFO: res = scap_fd_allocate_fdinfo(handle, &fdi, fd, SCAP_FD_FIFO); if(SCAP_FAILURE == res) { + snprintf(error, SCAP_LASTERR_SIZE, "can't allocate scap fd handle for fifo fd %" PRIu64, fd); break; - } + } res = scap_fd_handle_pipe(handle, f_name, tinfo, fdi, error); break; case S_IFREG: case S_IFBLK: case S_IFCHR: case S_IFLNK: - res = scap_fd_allocate_fdinfo(handle, &fdi, fd, SCAP_FD_FILE); + res = scap_fd_allocate_fdinfo(handle, &fdi, fd, SCAP_FD_FILE_V2); if(SCAP_FAILURE == res) { + snprintf(error, SCAP_LASTERR_SIZE, "can't allocate scap fd handle for file fd %" PRIu64, fd); break; } fdi->ino = sb.st_ino; - res = scap_fd_handle_regular_file(handle, f_name, tinfo, fdi, error); + res = scap_fd_handle_regular_file(handle, f_name, tinfo, fdi, procdir, error); break; case S_IFDIR: res = scap_fd_allocate_fdinfo(handle, &fdi, fd, SCAP_FD_DIRECTORY); if(SCAP_FAILURE == res) { + snprintf(error, SCAP_LASTERR_SIZE, "can't allocate scap fd handle for dir fd %" PRIu64, fd); break; } fdi->ino = sb.st_ino; - res = scap_fd_handle_regular_file(handle, f_name, tinfo, fdi, error); + res = scap_fd_handle_regular_file(handle, f_name, tinfo, fdi, procdir, error); break; case S_IFSOCK: res = scap_fd_allocate_fdinfo(handle, &fdi, fd, SCAP_FD_UNKNOWN); if(SCAP_FAILURE == res) { + snprintf(error, SCAP_LASTERR_SIZE, "can't allocate scap fd handle for sock fd %" PRIu64, fd); break; } - res = scap_fd_handle_socket(handle, f_name, tinfo, fdi, sockets, error); - if(fdi->type == SCAP_FD_UNKNOWN) + res = scap_fd_handle_socket(handle, f_name, tinfo, fdi, procdir, net_ns, sockets_by_ns, error); + if(handle->m_proc_callback == NULL) { // we can land here if we've got a netlink socket - scap_fd_free_fdinfo(&fdi); + if(fdi->type == SCAP_FD_UNKNOWN) + { + scap_fd_free_fdinfo(&fdi); + } } break; default: res = scap_fd_allocate_fdinfo(handle, &fdi, fd, SCAP_FD_UNSUPPORTED); if(SCAP_FAILURE == res) { + snprintf(error, SCAP_LASTERR_SIZE, "can't allocate scap fd handle for unsupported fd %" PRIu64, fd); break; } fdi->ino = sb.st_ino; - res = scap_fd_handle_regular_file(handle, f_name, tinfo, fdi, error); + res = scap_fd_handle_regular_file(handle, f_name, tinfo, fdi, procdir, error); break; } - if(NULL != fdi) + + if(handle->m_proc_callback != NULL) { - ASSERT(SCAP_FD_UNKNOWN != fdi->type); + if(fdi) + { + scap_fd_free_fdinfo(&fdi); + } } + if(SCAP_SUCCESS != res) { break; + } else { + ++fd_added; } } - closedir(dir_p); return res; } -#endif // _WIN32 +#endif // HAS_CAPTURE // // Internal helper function to output the fd table of a process // -void scap_fd_print_table(scap_threadinfo *tinfo) +void scap_fd_print_table(scap_t *handle, scap_threadinfo *tinfo) { - scap_fd_print_fd_table(tinfo->fdlist); + scap_fd_print_fd_table(handle, tinfo->fdlist); } -void scap_fd_print_fd_table(scap_fdinfo *fds) +void scap_fd_print_fd_table(scap_t *handle, scap_fdinfo *fds) { scap_fdinfo *fdi; scap_fdinfo *tfdi; @@ -1328,7 +2040,7 @@ void scap_fd_print_fd_table(scap_fdinfo *fds) HASH_ITER(hh, fds, fdi, tfdi) { - if(scap_fd_info_to_string(fdi, str, SCAP_MAX_PATH_SIZE) != SCAP_SUCCESS) + if(scap_fd_info_to_string(handle, fdi, str, SCAP_MAX_PATH_SIZE) != SCAP_SUCCESS) { ASSERT(false); snprintf(str, SCAP_MAX_PATH_SIZE, "N.A."); diff --git a/userspace/libscap/scap_iflist.c b/userspace/libscap/scap_iflist.c index 8014c4d260..e70098015a 100644 --- a/userspace/libscap/scap_iflist.c +++ b/userspace/libscap/scap_iflist.c @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include @@ -21,7 +22,7 @@ along with sysdig. If not, see . #include "scap.h" #include "scap-int.h" -#if !defined(_WIN32) && !defined(__APPLE__) +#if defined(HAS_CAPTURE) #include #include #include @@ -157,6 +158,7 @@ int32_t scap_create_iflist(scap_t* handle) handle->m_addrlist->v4list[ifcnt4].netmask = 0; } +#ifndef CYGWING_AGENT if(tempIfAddr->ifa_ifu.ifu_broadaddr != NULL) { handle->m_addrlist->v4list[ifcnt4].bcast = *(uint32_t*)&(((struct sockaddr_in *)tempIfAddr->ifa_ifu.ifu_broadaddr)->sin_addr); @@ -165,7 +167,9 @@ int32_t scap_create_iflist(scap_t* handle) { handle->m_addrlist->v4list[ifcnt4].bcast = 0; } - +#else + handle->m_addrlist->v4list[ifcnt4].bcast = 0; +#endif strncpy(handle->m_addrlist->v4list[ifcnt4].ifname, tempIfAddr->ifa_name, SCAP_MAX_PATH_SIZE); handle->m_addrlist->v4list[ifcnt4].ifnamelen = strlen(tempIfAddr->ifa_name); @@ -193,6 +197,7 @@ int32_t scap_create_iflist(scap_t* handle) memset(handle->m_addrlist->v6list[ifcnt6].netmask, 0, 16); } +#ifndef CYGWING_AGENT if(tempIfAddr->ifa_ifu.ifu_broadaddr != NULL) { memcpy(handle->m_addrlist->v6list[ifcnt6].bcast, @@ -203,6 +208,9 @@ int32_t scap_create_iflist(scap_t* handle) { memset(handle->m_addrlist->v6list[ifcnt6].bcast, 0, 16); } +#else + handle->m_addrlist->v4list[ifcnt4].bcast = 0; +#endif strncpy(handle->m_addrlist->v6list[ifcnt6].ifname, tempIfAddr->ifa_name, SCAP_MAX_PATH_SIZE); handle->m_addrlist->v6list[ifcnt6].ifnamelen = strlen(tempIfAddr->ifa_name); @@ -225,7 +233,14 @@ int32_t scap_create_iflist(scap_t* handle) return SCAP_SUCCESS; } -#endif // _WIN32 + +void scap_refresh_iflist(scap_t* handle) +{ + scap_free_iflist(handle->m_addrlist); + handle->m_addrlist = NULL; + scap_create_iflist(handle); +} +#endif // HAS_CAPTURE // // Free a previously allocated list of interfaces diff --git a/userspace/libscap/scap_procs.c b/userspace/libscap/scap_procs.c index 2bb1d9224e..70ae102c03 100644 --- a/userspace/libscap/scap_procs.c +++ b/userspace/libscap/scap_procs.c @@ -1,37 +1,52 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#include #include #include -#ifndef _WIN32 +#include +#ifdef HAS_CAPTURE +#ifndef CYGWING_AGENT #include #include #include #include #include -#endif +#include +#include +#endif // CYGWING_AGENT +#endif // HAS_CAPTURE #include "scap.h" - +#include "../../driver/ppm_ringbuffer.h" #include "scap-int.h" +#ifdef CYGWING_AGENT +#include "windows_hal.h" +#endif -#if !defined(_WIN32) && !defined(__APPLE__) -int32_t scap_proc_fill_cwd(char* procdirname, struct scap_threadinfo* tinfo) +#if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32) +#define strerror_r(errnum, buf, size) strerror_s(buf, size, errnum) +#endif + +#if defined(HAS_CAPTURE) +#ifndef CYGWING_AGENT +int32_t scap_proc_fill_cwd(scap_t *handle, char* procdirname, struct scap_threadinfo* tinfo) { int target_res; char filename[SCAP_MAX_PATH_SIZE]; @@ -41,6 +56,8 @@ int32_t scap_proc_fill_cwd(char* procdirname, struct scap_threadinfo* tinfo) target_res = readlink(filename, tinfo->cwd, sizeof(tinfo->cwd) - 1); if(target_res <= 0) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "readlink %s failed (%s)", + filename, scap_strerror(handle, errno)); return SCAP_FAILURE; } @@ -48,17 +65,40 @@ int32_t scap_proc_fill_cwd(char* procdirname, struct scap_threadinfo* tinfo) return SCAP_SUCCESS; } -int32_t scap_proc_fill_info_from_stats(char* procdirname, struct scap_threadinfo* tinfo) +int32_t scap_proc_fill_info_from_stats(scap_t *handle, char* procdirname, struct scap_threadinfo* tinfo) { char filename[SCAP_MAX_PATH_SIZE]; uint32_t nfound = 0; - uint32_t tmp; + int64_t tmp; uint32_t uid; + uint64_t tgid; uint64_t ppid; - char line[128]; + uint64_t vpid; + uint64_t vtid; + int64_t sid; + int64_t pgid; + int64_t vpgid; + uint32_t vmsize_kb; + uint32_t vmrss_kb; + uint32_t vmswap_kb; + uint64_t pfmajor; + uint64_t pfminor; + int32_t tty; + char line[512]; + char tmpc; + char* s; tinfo->uid = (uint32_t)-1; tinfo->ptid = (uint32_t)-1LL; + tinfo->sid = 0; + tinfo->vpgid = 0; + tinfo->vmsize_kb = 0; + tinfo->vmrss_kb = 0; + tinfo->vmswap_kb = 0; + tinfo->pfmajor = 0; + tinfo->pfminor = 0; + tinfo->filtered_out = 0; + tinfo->tty = 0; snprintf(filename, sizeof(filename), "%sstatus", procdirname); @@ -66,16 +106,31 @@ int32_t scap_proc_fill_info_from_stats(char* procdirname, struct scap_threadinfo if(f == NULL) { ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "open status file %s failed (%s)", + filename, scap_strerror(handle, errno)); return SCAP_FAILURE; } while(fgets(line, sizeof(line), f) != NULL) { + if(strstr(line, "Tgid") == line) + { + nfound++; + + if(sscanf(line, "Tgid: %" PRIu64, &tgid) == 1) + { + tinfo->pid = tgid; + } + else + { + ASSERT(false); + } + } if(strstr(line, "Uid") == line) { nfound++; - if(sscanf(line, "Uid: %" PRIu32 " %" PRIu32, &tmp, &uid) == 2) + if(sscanf(line, "Uid: %" PRIu64 " %" PRIu32, &tmp, &uid) == 2) { tinfo->uid = uid; } @@ -88,7 +143,7 @@ int32_t scap_proc_fill_info_from_stats(char* procdirname, struct scap_threadinfo { nfound++; - if(sscanf(line, "Gid: %" PRIu32 " %" PRIu32, &tmp, &uid) == 2) + if(sscanf(line, "Gid: %" PRIu64 " %" PRIu32, &tmp, &uid) == 2) { tinfo->gid = uid; } @@ -110,14 +165,154 @@ int32_t scap_proc_fill_info_from_stats(char* procdirname, struct scap_threadinfo ASSERT(false); } } + else if(strstr(line, "VmSize:") == line) + { + nfound++; + + if(sscanf(line, "VmSize: %" PRIu32, &vmsize_kb) == 1) + { + tinfo->vmsize_kb = vmsize_kb; + } + else + { + ASSERT(false); + } + } + else if(strstr(line, "VmRSS:") == line) + { + nfound++; + + if(sscanf(line, "VmRSS: %" PRIu32, &vmrss_kb) == 1) + { + tinfo->vmrss_kb = vmrss_kb; + } + else + { + ASSERT(false); + } + } + else if(strstr(line, "VmSwap:") == line) + { + nfound++; + + if(sscanf(line, "VmSwap: %" PRIu32, &vmswap_kb) == 1) + { + tinfo->vmswap_kb = vmswap_kb; + } + else + { + ASSERT(false); + } + } + else if(strstr(line, "NSpid:") == line) + { + nfound++; + if(sscanf(line, "NSpid: %*u %" PRIu64, &vtid) == 1) + { + tinfo->vtid = vtid; + } + else + { + tinfo->vtid = tinfo->tid; + } + } + else if(strstr(line, "NSpgid:") == line) + { + nfound++; + if(sscanf(line, "NSpgid: %*u %" PRIu64, &vpgid) == 1) + { + tinfo->vpgid = vpgid; + } + } + else if(strstr(line, "NStgid:") == line) + { + nfound++; + if(sscanf(line, "NStgid: %*u %" PRIu64, &vpid) == 1) + { + tinfo->vpid = vpid; + } + else + { + tinfo->vpid = tinfo->pid; + } + } - if(nfound == 3) + if(nfound == 10) { break; } } - ASSERT(nfound == 3); + ASSERT(nfound == 10 || nfound == 7 || nfound == 6); + + fclose(f); + + snprintf(filename, sizeof(filename), "%sstat", procdirname); + + f = fopen(filename, "r"); + if(f == NULL) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "read stat file %s failed (%s)", + filename, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + + size_t ssres = fread(line, 1, sizeof(line) - 1, f); + if(ssres == 0) + { + ASSERT(false); + fclose(f); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read from stat file %s (%s)", + filename, scap_strerror(handle, errno)); + return SCAP_FAILURE; + } + line[ssres] = 0; + + s = strrchr(line, ')'); + if(s == NULL) + { + ASSERT(false); + fclose(f); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not find closing bracket in stat file %s", + filename); + return SCAP_FAILURE; + } + + // + // Extract the line content + // + if(sscanf(s + 2, "%c %" PRId64 " %" PRId64 " %" PRId64 " %" PRId32 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, + &tmpc, + &tmp, + &pgid, + &sid, + &tty, + &tmp, + &tmp, + &pfminor, + &tmp, + &pfmajor) != 10) + { + ASSERT(false); + fclose(f); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read expected fields from stat file %s", + filename); + return SCAP_FAILURE; + } + + tinfo->pfmajor = pfmajor; + tinfo->pfminor = pfminor; + tinfo->sid = (uint64_t) sid; + + // If we did not find vpgid above, set it to pgid from the + // global namespace. + if(tinfo->vpgid == 0) + { + tinfo->vpgid = pgid; + } + + tinfo->tty = tty; fclose(f); return SCAP_SUCCESS; @@ -127,16 +322,18 @@ int32_t scap_proc_fill_info_from_stats(char* procdirname, struct scap_threadinfo // use prlimit to extract the RLIMIT_NOFILE for the tid. On systems where prlimit // is not supported, just return -1 // -int32_t scap_proc_fill_flimit(uint64_t tid, struct scap_threadinfo* tinfo) +static int32_t scap_proc_fill_flimit(scap_t *handle, uint64_t tid, struct scap_threadinfo* tinfo) #ifdef SYS_prlimit64 { struct rlimit rl; +#ifdef __NR_prlimit64 if(syscall(SYS_prlimit64, tid, RLIMIT_NOFILE, NULL, &rl) == 0) { tinfo->fdlimit = rl.rlim_cur; return SCAP_SUCCESS; } +#endif tinfo->fdlimit = -1; return SCAP_SUCCESS; @@ -148,207 +345,601 @@ int32_t scap_proc_fill_flimit(uint64_t tid, struct scap_threadinfo* tinfo) } #endif -// -// Add a process to the list by parsing its entry under /proc -// -int32_t scap_proc_add_from_proc(scap_t* handle, uint32_t tid, int parenttid, int tid_to_scan, char* procdirname, scap_fdinfo* sockets, scap_threadinfo** procinfo, char *error) +int32_t scap_proc_fill_cgroups(scap_t *handle, struct scap_threadinfo* tinfo, const char* procdirname) { - char dir_name[256]; - char target_name[256]; - int target_res; - char filename[252]; - char line[SCAP_MAX_PATH_SIZE]; - struct scap_threadinfo* tinfo; - int32_t uth_status = SCAP_SUCCESS; - FILE* f; - size_t filesize; - size_t exe_len; - - snprintf(dir_name, sizeof(dir_name), "%s/%u/", procdirname, tid); - snprintf(filename, sizeof(filename), "%sexe", dir_name); + char filename[SCAP_MAX_PATH_SIZE]; + char line[SCAP_MAX_CGROUPS_SIZE]; - // - // Gather the executable full name - // - target_res = readlink(filename, target_name, sizeof(target_name) - 1); // Getting the target of the exe, i.e. to which binary it points to + tinfo->cgroups_len = 0; + snprintf(filename, sizeof(filename), "%scgroup", procdirname); - if(target_res <= 0) + if(access(filename, R_OK) == -1) { - // - // This is normal and happens with kernel threads, which we aren't interested in - // return SCAP_SUCCESS; } - target_name[target_res] = 0; - - // - // This is a real user level process. Allocate the procinfo structure. - // - tinfo = (scap_threadinfo*)malloc(sizeof(scap_threadinfo)); - if(tinfo == NULL) + FILE* f = fopen(filename, "r"); + if(f == NULL) { - snprintf(error, SCAP_LASTERR_SIZE, "process table allocation error (1)"); + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "open cgroup file %s failed (%s)", + filename, scap_strerror(handle, errno)); return SCAP_FAILURE; } - tinfo->tid = tid; - if(parenttid != -1) - { - tinfo->pid = parenttid; - } - else + while(fgets(line, sizeof(line), f) != NULL) { - tinfo->pid = tid; - } + char* token; + char* subsys_list; + char* cgroup; + char* scratch; + + // id + token = strtok_r(line, ":", &scratch); + if(token == NULL) + { + ASSERT(false); + fclose(f); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Did not find id in cgroup file %s", + filename); + return SCAP_FAILURE; + } - tinfo->fdlist = NULL; + // subsys + subsys_list = strtok_r(NULL, ":", &scratch); + if(subsys_list == NULL) + { + ASSERT(false); + fclose(f); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Did not find subsys in cgroup file %s", + filename); + return SCAP_FAILURE; + } - // - // If tid is different from pid, assume this is a thread and that the FDs are shared, and set the - // corresponding process flags. - // XXX we should see if the process creation flags are stored somewhere in /proc and handle this - // properly instead of making assumptions. - // - if(tinfo->tid == tinfo->pid) - { - tinfo->flags = 0; + // Hack to detect empty fields, because strtok does not support it + // strsep() should be used to fix this but it's not available + // on CentOS 6 (has been added from Glibc 2.19) + if(subsys_list-token-strlen(token) > 1) + { + // skip cgroups like this: + // 0::/init.scope + continue; + } + + // cgroup + cgroup = strtok_r(NULL, ":", &scratch); + if(cgroup == NULL) + { + ASSERT(false); + fclose(f); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Did not find cgroup in cgroup file %s", + filename); + return SCAP_FAILURE; + } + + // remove the \n + cgroup[strlen(cgroup) - 1] = 0; + + while((token = strtok_r(subsys_list, ",", &scratch)) != NULL) + { + subsys_list = NULL; + if(strlen(cgroup) + 1 + strlen(token) + 1 > SCAP_MAX_CGROUPS_SIZE - tinfo->cgroups_len) + { + ASSERT(false); + fclose(f); + return SCAP_SUCCESS; + } + + snprintf(tinfo->cgroups + tinfo->cgroups_len, SCAP_MAX_CGROUPS_SIZE - tinfo->cgroups_len, "%s=%s", token, cgroup); + tinfo->cgroups_len += strlen(cgroup) + 1 + strlen(token) + 1; + } } - else + + fclose(f); + return SCAP_SUCCESS; +} + +static int32_t scap_get_vtid(scap_t* handle, int64_t tid, int64_t *vtid) +{ + if(handle->m_mode != SCAP_MODE_LIVE) { - tinfo->flags = PPM_CL_CLONE_THREAD | PPM_CL_CLONE_FILES; + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get vtid (not in live mode)"); + return SCAP_FAILURE; } - snprintf(tinfo->exe, SCAP_MAX_PATH_SIZE, "%s", target_name); - - // - // Gather the command name - // - snprintf(filename, sizeof(filename), "%sstatus", dir_name); +#if !defined(HAS_CAPTURE) + ASSERT(false) + return SCAP_FAILURE; +#else - f = fopen(filename, "r"); - if(f == NULL) + if(handle->m_bpf || handle->m_udig) { - snprintf(error, SCAP_LASTERR_SIZE, "can't open %s", filename); - free(tinfo); - return SCAP_FAILURE; + *vtid = 0; } else { - if(fgets(line, SCAP_MAX_PATH_SIZE, f) == NULL) + *vtid = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_VTID, tid); + + if(*vtid == -1) { - snprintf(error, SCAP_LASTERR_SIZE, "can't read from %s", filename); - fclose(f); - free(tinfo); + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ioctl to get vtid failed (%s)", + scap_strerror(handle, errno)); return SCAP_FAILURE; } - - line[SCAP_MAX_PATH_SIZE - 1] = 0; - sscanf(line, "Name:%s", tinfo->comm); - fclose(f); } - // - // Gather the command line - // - snprintf(filename, sizeof(filename), "%scmdline", dir_name); + return SCAP_SUCCESS; +#endif +} - f = fopen(filename, "r"); - if(f == NULL) +static int32_t scap_get_vpid(scap_t* handle, int64_t tid, int64_t *vpid) +{ + if(handle->m_mode != SCAP_MODE_LIVE) { - snprintf(error, SCAP_LASTERR_SIZE, "can't open %s", filename); - free(tinfo); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get vtid (not in live mode)"); return SCAP_FAILURE; } + +#if !defined(HAS_CAPTURE) + ASSERT(false) + return SCAP_FAILURE; +#else + + if(handle->m_bpf || handle->m_udig) + { + *vpid = 0; + } else { - filesize = fread(line, 1, sizeof(line), f); - line[filesize - 1] = 0; - - exe_len = strlen(line); - if(exe_len < filesize) - { - ++exe_len; - } + *vpid = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_VPID, tid); - tinfo->args_len = filesize - exe_len; - if(tinfo->args_len > SCAP_MAX_PATH_SIZE) + if(*vpid == -1) { - tinfo->args_len = SCAP_MAX_PATH_SIZE; + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ioctl to get vpid failed (%s)", + scap_strerror(handle, errno)); + return SCAP_FAILURE; } + } - memcpy(tinfo->args, line + exe_len, tinfo->args_len); - tinfo->args[SCAP_MAX_PATH_SIZE - 1] = 0; + return SCAP_SUCCESS; +#endif +} - fclose(f); +int32_t scap_proc_fill_root(scap_t *handle, struct scap_threadinfo* tinfo, const char* procdirname) +{ + char root_path[SCAP_MAX_PATH_SIZE]; + snprintf(root_path, sizeof(root_path), "%sroot", procdirname); + if ( readlink(root_path, tinfo->root, sizeof(tinfo->root)) > 0) + { + return SCAP_SUCCESS; } - - // - // set the current working directory of the process - // - if(SCAP_FAILURE == scap_proc_fill_cwd(dir_name, tinfo)) + else { - snprintf(error, SCAP_LASTERR_SIZE, "can't fill cwd for %s", dir_name); - free(tinfo); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "readlink %s failed (%s)", + root_path, scap_strerror(handle, errno)); return SCAP_FAILURE; } +} - // - // extract the user id and ppid from /proc/pid/status - // - if(SCAP_FAILURE == scap_proc_fill_info_from_stats(dir_name, tinfo)) +int32_t scap_proc_fill_loginuid(scap_t *handle, struct scap_threadinfo* tinfo, const char* procdirname) +{ + uint32_t loginuid; + char loginuid_path[SCAP_MAX_PATH_SIZE]; + char line[512]; + snprintf(loginuid_path, sizeof(loginuid_path), "%sloginuid", procdirname); + FILE* f = fopen(loginuid_path, "r"); + if(f == NULL) { - snprintf(error, SCAP_LASTERR_SIZE, "can't fill cwd for %s", dir_name); - free(tinfo); + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Open loginuid file %s failed (%s)", + loginuid_path, scap_strerror(handle, errno)); return SCAP_FAILURE; } - - // - // Set the file limit - // - if(SCAP_FAILURE == scap_proc_fill_flimit(tinfo->tid, tinfo)) + if (fgets(line, sizeof(line), f) == NULL) { - snprintf(error, SCAP_LASTERR_SIZE, "can't fill flimit for %s", dir_name); - free(tinfo); + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read loginuid from %s (%s)", + loginuid_path, scap_strerror(handle, errno)); + fclose(f); return SCAP_FAILURE; } - // - // if tid_to_scan is set we assume is a runtime lookup so no - // need to use the table - // - if(tid_to_scan == -1) + fclose(f); + + if(sscanf(line, "%" PRId32, &loginuid) == 1) { - // - // Done. Add the entry to the process table - // - HASH_ADD_INT64(handle->m_proclist, tid, tinfo); - if(uth_status != SCAP_SUCCESS) + tinfo->loginuid = loginuid; + return SCAP_SUCCESS; + } + else + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read loginuid from %s", + loginuid_path); + return SCAP_FAILURE; + } +} + +// +// Add a process to the list by parsing its entry under /proc +// +static int32_t scap_proc_add_from_proc(scap_t* handle, uint32_t tid, char* procdirname, struct scap_ns_socket_list** sockets_by_ns, scap_threadinfo** procinfo, char *error) +{ + char dir_name[256]; + char target_name[SCAP_MAX_PATH_SIZE]; + int target_res; + char filename[252]; + char line[SCAP_MAX_ENV_SIZE]; + struct scap_threadinfo* tinfo; + int32_t uth_status = SCAP_SUCCESS; + FILE* f; + size_t filesize; + size_t exe_len; + bool free_tinfo = false; + int32_t res = SCAP_SUCCESS; + struct stat dirstat; + + snprintf(dir_name, sizeof(dir_name), "%s/%u/", procdirname, tid); + snprintf(filename, sizeof(filename), "%sexe", dir_name); + + // + // Gather the executable full name + // + target_res = readlink(filename, target_name, sizeof(target_name) - 1); // Getting the target of the exe, i.e. to which binary it points to + + if(target_res <= 0) + { + // + // No exe. This either + // - a kernel thread (if there is no cmdline). In that case we skip it. + // - a process that has been containerized or has some weird thing going on. In that case + // we accept it. + // + snprintf(filename, sizeof(filename), "%scmdline", dir_name); + f = fopen(filename, "r"); + if(f == NULL) + { + return SCAP_SUCCESS; + } + + ASSERT(sizeof(line) >= SCAP_MAX_PATH_SIZE); + + if(fgets(line, SCAP_MAX_PATH_SIZE, f) == NULL) + { + fclose(f); + return SCAP_SUCCESS; + } + else + { + fclose(f); + } + + target_name[0] = 0; + } + else + { + // null-terminate target_name (readlink() does not append a null byte) + target_name[target_res] = 0; + } + + // + // This is a real user level process. Allocate the procinfo structure. + // + if((tinfo = scap_proc_alloc(handle)) == NULL) + { + // Error message saved in handle->m_lasterr + snprintf(error, SCAP_LASTERR_SIZE, "can't allocate procinfo struct: %s", handle->m_lasterr); + return SCAP_FAILURE; + } + + tinfo->tid = tid; + + tinfo->fdlist = NULL; + + // + // Gathers the exepath + // + snprintf(tinfo->exepath, sizeof(tinfo->exepath), "%s", target_name); + + // + // Gather the command name + // + snprintf(filename, sizeof(filename), "%sstatus", dir_name); + + f = fopen(filename, "r"); + if(f == NULL) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't open %s (error %s)", filename, scap_strerror(handle, errno)); + free(tinfo); + return SCAP_FAILURE; + } + else + { + ASSERT(sizeof(line) >= SCAP_MAX_PATH_SIZE); + + if(fgets(line, SCAP_MAX_PATH_SIZE, f) == NULL) { - snprintf(error, SCAP_LASTERR_SIZE, "process table allocation error (2)"); + snprintf(error, SCAP_LASTERR_SIZE, "can't read from %s (%s)", + filename, scap_strerror(handle, errno)); + fclose(f); + free(tinfo); return SCAP_FAILURE; } + + line[SCAP_MAX_PATH_SIZE - 1] = 0; + sscanf(line, "Name:%1024s", tinfo->comm); + fclose(f); + } + + bool suppressed; + if ((res = scap_update_suppressed(handle, tinfo->comm, tid, 0, &suppressed)) != SCAP_SUCCESS) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't update set of suppressed tids (%s)", handle->m_lasterr); + free(tinfo); + return res; + } + + if (suppressed && !procinfo) + { + free(tinfo); + return SCAP_SUCCESS; + } + + // + // Gather the command line + // + snprintf(filename, sizeof(filename), "%scmdline", dir_name); + + f = fopen(filename, "r"); + if(f == NULL) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't open cmdline file %s (%s)", + filename, scap_strerror(handle, errno)); + free(tinfo); + return SCAP_FAILURE; + } + else + { + ASSERT(sizeof(line) >= SCAP_MAX_ARGS_SIZE); + + filesize = fread(line, 1, SCAP_MAX_ARGS_SIZE - 1, f); + if(filesize > 0) + { + line[filesize] = 0; + + exe_len = strlen(line); + if(exe_len < filesize) + { + ++exe_len; + } + + snprintf(tinfo->exe, SCAP_MAX_PATH_SIZE, "%s", line); + + tinfo->args_len = filesize - exe_len; + + memcpy(tinfo->args, line + exe_len, tinfo->args_len); + tinfo->args[SCAP_MAX_ARGS_SIZE - 1] = 0; + } + else + { + tinfo->args[0] = 0; + tinfo->exe[0] = 0; + } + + fclose(f); + } + + // + // Gather the environment + // + snprintf(filename, sizeof(filename), "%senviron", dir_name); + + f = fopen(filename, "r"); + if(f == NULL) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't open environ file %s (%s)", + filename, scap_strerror(handle, errno)); + free(tinfo); + return SCAP_FAILURE; + } + else + { + ASSERT(sizeof(line) >= SCAP_MAX_ENV_SIZE); + + filesize = fread(line, 1, SCAP_MAX_ENV_SIZE, f); + + if(filesize > 0) + { + line[filesize - 1] = 0; + + tinfo->env_len = filesize; + + memcpy(tinfo->env, line, tinfo->env_len); + tinfo->env[SCAP_MAX_ENV_SIZE - 1] = 0; + } + else + { + tinfo->env[0] = 0; + } + + fclose(f); + } + + // + // set the current working directory of the process + // + if(SCAP_FAILURE == scap_proc_fill_cwd(handle, dir_name, tinfo)) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't fill cwd for %s (%s)", + dir_name, handle->m_lasterr); + free(tinfo); + return SCAP_FAILURE; + } + + // + // extract the user id and ppid from /proc/pid/status + // + if(SCAP_FAILURE == scap_proc_fill_info_from_stats(handle, dir_name, tinfo)) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't fill cwd for %s (%s)", + dir_name, handle->m_lasterr); + free(tinfo); + return SCAP_FAILURE; + } + + // + // Set the file limit + // + if(SCAP_FAILURE == scap_proc_fill_flimit(handle, tinfo->tid, tinfo)) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't fill flimit for %s (%s)", + dir_name, handle->m_lasterr); + free(tinfo); + return SCAP_FAILURE; + } + + if(scap_proc_fill_cgroups(handle, tinfo, dir_name) == SCAP_FAILURE) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't fill cgroups for %s (%s)", + dir_name, handle->m_lasterr); + free(tinfo); + return SCAP_FAILURE; + } + + // These values should be read already from /status file, leave these + // fallback functions for older kernels < 4.1 + if(tinfo->vtid == 0 && scap_get_vtid(handle, tinfo->tid, &tinfo->vtid) == SCAP_FAILURE) + { + tinfo->vtid = tinfo->tid; + } + + if(tinfo->vpid == 0 && scap_get_vpid(handle, tinfo->tid, &tinfo->vpid) == SCAP_FAILURE) + { + tinfo->vpid = tinfo->pid; + } + + // + // set the current root of the process + // + if(SCAP_FAILURE == scap_proc_fill_root(handle, tinfo, dir_name)) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't fill root for %s (%s)", + dir_name, handle->m_lasterr); + free(tinfo); + return SCAP_FAILURE; + } + + // + // set the loginuid + // + if(SCAP_FAILURE == scap_proc_fill_loginuid(handle, tinfo, dir_name)) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't fill loginuid for %s (%s)", + dir_name, handle->m_lasterr); + free(tinfo); + return SCAP_FAILURE; + } + + if(stat(dir_name, &dirstat) == 0) + { + tinfo->clone_ts = dirstat.st_ctim.tv_sec*1000000000 + dirstat.st_ctim.tv_nsec; + } + + // If tid is different from pid, assume this is a thread and that the FDs are shared, and set the + // corresponding process flags. + // XXX we should see if the process creation flags are stored somewhere in /proc and handle this + // properly instead of making assumptions. + // + if(tinfo->tid == tinfo->pid) + { + tinfo->flags = 0; + } + else + { + tinfo->flags = PPM_CL_CLONE_THREAD | PPM_CL_CLONE_FILES; + } + + // + // if procinfo is set we assume this is a runtime lookup so no + // need to use the table + // + if(!procinfo) + { + // + // Done. Add the entry to the process table, or fire the notification callback + // + if(handle->m_proc_callback == NULL) + { + HASH_ADD_INT64(handle->m_proclist, tid, tinfo); + if(uth_status != SCAP_SUCCESS) + { + snprintf(error, SCAP_LASTERR_SIZE, "process table allocation error (2)"); + free(tinfo); + return SCAP_FAILURE; + } + } + else + { + handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo->tid, tinfo, NULL); + free_tinfo = true; + } } else { *procinfo = tinfo; } - + // // Only add fds for processes, not threads // - if(-1 == parenttid) + if(tinfo->pid == tinfo->tid) { - return scap_fd_scan_fd_dir(handle, dir_name, tinfo, sockets, error); + res = scap_fd_scan_fd_dir(handle, dir_name, tinfo, sockets_by_ns, error); } - return SCAP_SUCCESS; + if(free_tinfo) + { + free(tinfo); + } + + return res; +} + +// +// Read a single thread info from /proc +// +int32_t scap_proc_read_thread(scap_t* handle, char* procdirname, uint64_t tid, struct scap_threadinfo** pi, char *error, bool scan_sockets) +{ + struct scap_ns_socket_list* sockets_by_ns = NULL; + + int32_t res; + char add_error[SCAP_LASTERR_SIZE]; + + if(!scan_sockets) + { + sockets_by_ns = (void*)-1; + } + + res = scap_proc_add_from_proc(handle, tid, procdirname, &sockets_by_ns, pi, add_error); + if(res != SCAP_SUCCESS) + { + snprintf(error, SCAP_LASTERR_SIZE, "cannot add proc tid = %"PRIu64", dirname = %s, error=%s", tid, procdirname, add_error); + } + + if(sockets_by_ns != NULL && sockets_by_ns != (void*)-1) + { + scap_fd_free_ns_sockets_list(handle, &sockets_by_ns); + } + + return res; } // // Scan a directory containing multiple processes under /proc // -int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, int parenttid, int tid_to_scan, struct scap_threadinfo** procinfo, char *error, bool scan_sockets) +static int32_t _scap_proc_scan_proc_dir_impl(scap_t* handle, char* procdirname, int parenttid, char *error) { DIR *dir_p; struct dirent *dir_entry_p; @@ -357,34 +948,17 @@ int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, int parenttid int32_t res = SCAP_SUCCESS; char childdir[SCAP_MAX_PATH_SIZE]; - scap_fdinfo* sockets = NULL; + struct scap_ns_socket_list* sockets_by_ns = NULL; - tid = 0; dir_p = opendir(procdirname); if(dir_p == NULL) { - snprintf(error, SCAP_LASTERR_SIZE, "error opening the %s directory", procdirname); + snprintf(error, SCAP_LASTERR_SIZE, "error opening the %s directory (%s)", + procdirname, scap_strerror(handle, errno)); return SCAP_NOTFOUND; } - if(-1 == parenttid) - { - if(scan_sockets) - { - if(SCAP_FAILURE == scap_fd_read_sockets(handle, &sockets)) - { - closedir(dir_p); - return SCAP_FAILURE; - } - } - } - - if(tid_to_scan != -1) - { - *procinfo = NULL; - } - while((dir_entry_p = readdir(dir_p)) != NULL) { if(strspn(dir_entry_p->d_name, "0123456789") != strlen(dir_entry_p->d_name)) @@ -406,70 +980,133 @@ int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, int parenttid } // - // if tid_to_scan is set we assume is a runtime lookup so no - // need to use the table + // This is the initial /proc scan so duplicate threads + // are an error, or at least unexpected. Check the process + // list to see if we've encountered this tid already // - if(tid_to_scan == -1) + HASH_FIND_INT64(handle->m_proclist, &tid, tinfo); + if(tinfo != NULL) { - HASH_FIND_INT64(handle->m_proclist, &tid, tinfo); - if(tinfo != NULL) - { - ASSERT(false); - snprintf(error, SCAP_LASTERR_SIZE, "duplicate process %"PRIu64, tid); - res = SCAP_FAILURE; - break; - } + ASSERT(false); + snprintf(error, SCAP_LASTERR_SIZE, "duplicate process %"PRIu64, tid); + res = SCAP_FAILURE; + break; } - if(tid_to_scan == -1 || tid_to_scan == tid) + char add_error[SCAP_LASTERR_SIZE]; + + // + // We have a process that needs to be explored + // + res = scap_proc_add_from_proc(handle, tid, procdirname, &sockets_by_ns, NULL, add_error); + if(res != SCAP_SUCCESS) { - // - // We have a process that needs to be explored - // - res = scap_proc_add_from_proc(handle, tid, parenttid, tid_to_scan, procdirname, sockets, procinfo, error); - if(res != SCAP_SUCCESS) - { - snprintf(error, SCAP_LASTERR_SIZE, "cannot add procs tid = %"PRIu64", parenttid = %"PRIi32", dirname = %s", tid, parenttid, procdirname); - break; - } + snprintf(error, SCAP_LASTERR_SIZE, "cannot add procs tid = %"PRIu64", parenttid = %"PRIi32", dirname = %s, error=%s", tid, parenttid, procdirname, add_error); + break; + } - if(tid_to_scan != -1) + // + // See if this process includes tasks that need to be added + // + if(parenttid == -1 && handle->m_mode != SCAP_MODE_NODRIVER) + { + snprintf(childdir, sizeof(childdir), "%s/%u/task", procdirname, (int)tid); + if(_scap_proc_scan_proc_dir_impl(handle, childdir, tid, error) == SCAP_FAILURE) { - // - // procinfo should be filled, except when - // the main thread already terminated and - // the various proc files were not readable - // - // ASSERT(*procinfo); + res = SCAP_FAILURE; break; } } + } - // - // See if this process includes tasks that need to be added - // - snprintf(childdir, sizeof(childdir), "%s/%u/task", procdirname, (int)tid); - if(scap_proc_scan_proc_dir(handle, childdir, tid, tid_to_scan, procinfo, error, scan_sockets) == SCAP_FAILURE) + closedir(dir_p); + if(sockets_by_ns != NULL && sockets_by_ns != (void*)-1) + { + scap_fd_free_ns_sockets_list(handle, &sockets_by_ns); + } + return res; +} + +int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, char *error) +{ + return _scap_proc_scan_proc_dir_impl(handle, procdirname, -1, error); +} + +#endif // CYGWING_AGENT + +int32_t scap_getpid_global(scap_t* handle, int64_t* pid) +{ +#ifndef CYGWING_AGENT + if(handle->m_mode != SCAP_MODE_LIVE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get pid (not in live mode)"); + ASSERT(false); + return SCAP_FAILURE; + } + +#if !defined(HAS_CAPTURE) + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Cannot get pid (capture not enabled)"); + return SCAP_FAILURE; +#else + + if(handle->m_bpf || handle->m_udig) + { + char filename[SCAP_MAX_PATH_SIZE]; + char line[512]; + + snprintf(filename, sizeof(filename), "%s/proc/self/status", scap_get_host_root()); + + FILE* f = fopen(filename, "r"); + if(f == NULL) { - res = SCAP_FAILURE; - break; + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can not open status file %s (%s)", + filename, scap_strerror(handle, errno)); + return SCAP_FAILURE; } - if(tid_to_scan != -1 && *procinfo) + while(fgets(line, sizeof(line), f) != NULL) { - // - // We found the process we were looking for, no need to keep iterating - // - break; + if(sscanf(line, "Tgid: %" PRId64, pid) == 1) + { + fclose(f); + return SCAP_SUCCESS; + } + } + + fclose(f); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "could not find tgid in status file %s", + filename); + return SCAP_FAILURE; + } + else + { + *pid = ioctl(handle->m_devs[0].m_fd, PPM_IOCTL_GET_CURRENT_PID); + if(*pid == -1) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "ioctl to get pid failed (%s)", + scap_strerror(handle, errno)); + return SCAP_FAILURE; } } - closedir(dir_p); - scap_fd_free_table(handle, &sockets); - return res; + return SCAP_SUCCESS; +#endif +#else // CYGWING_AGENT + return getpid(); +#endif // CYGWING_AGENT } -#endif // _WIN32 +#endif // HAS_CAPTURE + +#ifdef CYGWING_AGENT +int32_t scap_proc_scan_proc_dir(scap_t* handle, char* procdirname, char *error) +{ + return scap_proc_scan_proc_dir_windows(handle, error); +} +#endif // // Delete a process entry @@ -508,18 +1145,124 @@ void scap_proc_free_table(scap_t* handle) struct scap_threadinfo* scap_proc_get(scap_t* handle, int64_t tid, bool scan_sockets) { -#if defined(_WIN32) || defined(__APPLE__) +#if !defined(HAS_CAPTURE) return NULL; #else + + // + // No /proc parsing for offline captures + // + if(handle->m_mode == SCAP_MODE_CAPTURE) + { + return NULL; + } + struct scap_threadinfo* tinfo = NULL; + char filename[SCAP_MAX_PATH_SIZE]; + snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root()); + if(scap_proc_read_thread(handle, filename, tid, &tinfo, handle->m_lasterr, scan_sockets) != SCAP_SUCCESS) + { + free(tinfo); + return NULL; + } + + return tinfo; +#endif // HAS_CAPTURE +} + +bool scap_is_thread_alive(scap_t* handle, int64_t pid, int64_t tid, const char* comm) +{ +#if !defined(HAS_CAPTURE) + return false; +#else + char charbuf[SCAP_MAX_PATH_SIZE]; + FILE* f; + + + // + // No /proc parsing for offline captures + // + if(handle->m_mode == SCAP_MODE_CAPTURE) + { + return false; + } + + snprintf(charbuf, sizeof(charbuf), "%s/proc/%" PRId64 "/task/%" PRId64 "/comm", scap_get_host_root(), pid, tid); + + f = fopen(charbuf, "r"); + + if(f != NULL) + { + if(fgets(charbuf, sizeof(charbuf), f) != NULL) + { + if(strncmp(charbuf, comm, strlen(comm)) == 0) + { + fclose(f); + return true; + } + } + + fclose(f); + } + else + { + // + // If /proc//task//comm does not exist but /proc//task//exe does exist, we assume we're on an ancient + // OS like RHEL5 and we return true. + // This could generate some false positives on such old distros, and we're going to accept it. + // + snprintf(charbuf, sizeof(charbuf), "%s/proc/%" PRId64 "/task/%" PRId64 "/exe", scap_get_host_root(), pid, tid); + f = fopen(charbuf, "r"); + if(f != NULL) + { + fclose(f); + return true; + } + + } + + return false; +#endif // HAS_CAPTURE +} + +#if defined(HAS_CAPTURE) +int scap_proc_scan_proc_table(scap_t *handle) +{ + char filename[SCAP_MAX_PATH_SIZE]; + // + // Create the process list + // + handle->m_lasterr[0] = '\0'; + + snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root()); + return scap_proc_scan_proc_dir(handle, filename, handle->m_lasterr); +} + +void scap_refresh_proc_table(scap_t* handle) +{ + if(handle->m_proclist) + { + scap_proc_free_table(handle); + handle->m_proclist = NULL; + } + scap_proc_scan_proc_table(handle); +} +#else +void scap_refresh_proc_table(scap_t* handle) +{ +} +#endif // HAS_CAPTURE - if(scap_proc_scan_proc_dir(handle, "/proc", -1, tid, &tinfo, handle->m_lasterr, scan_sockets) != SCAP_SUCCESS) +struct scap_threadinfo *scap_proc_alloc(scap_t *handle) +{ + struct scap_threadinfo *tinfo = (struct scap_threadinfo*) calloc(1, sizeof(scap_threadinfo)); + if(tinfo == NULL) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (1)"); return NULL; } return tinfo; -#endif // WIN32 } void scap_proc_free(scap_t* handle, struct scap_threadinfo* proc) @@ -528,13 +1271,45 @@ void scap_proc_free(scap_t* handle, struct scap_threadinfo* proc) free(proc); } +int32_t scap_proc_add(scap_t* handle, uint64_t tid, scap_threadinfo* tinfo) +{ + int32_t uth_status = SCAP_SUCCESS; + + HASH_ADD_INT64(handle->m_proclist, tid, tinfo); + if(uth_status == SCAP_SUCCESS) + { + return SCAP_SUCCESS; + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not add tid to hash table"); + return SCAP_FAILURE; + } +} + +int32_t scap_fd_add(scap_t *handle, scap_threadinfo* tinfo, uint64_t fd, scap_fdinfo* fdinfo) +{ + int32_t uth_status = SCAP_SUCCESS; + + HASH_ADD_INT64(tinfo->fdlist, fd, fdinfo); + if(uth_status == SCAP_SUCCESS) + { + return SCAP_SUCCESS; + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not add fd to hash table"); + return SCAP_FAILURE; + } +} + // // Internal helper functions to output the process table to screen // -void scap_proc_print_info(scap_threadinfo* tinfo) +void scap_proc_print_info(scap_t *handle, scap_threadinfo* tinfo) { fprintf(stderr, "TID:%"PRIu64" PID:%"PRIu64" FLAGS:%"PRIu32" COMM:%s EXE:%s ARGS:%s CWD:%s FLIMIT:%" PRId64 "\n", tinfo->tid, tinfo->pid, tinfo->flags,tinfo->comm, tinfo->exe, tinfo->args, tinfo->cwd, tinfo->fdlimit); - scap_fd_print_table(tinfo); + scap_fd_print_table(handle, tinfo); } void scap_proc_print_proc_by_tid(scap_t* handle, uint64_t tid) @@ -546,7 +1321,7 @@ void scap_proc_print_proc_by_tid(scap_t* handle, uint64_t tid) { if(tinfo->tid == tid) { - scap_proc_print_info(tinfo); + scap_proc_print_info(handle, tinfo); } } } @@ -560,8 +1335,174 @@ void scap_proc_print_table(scap_t* handle) HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo) { - scap_proc_print_info(tinfo); + scap_proc_print_info(handle, tinfo); } printf("*******************************************\n"); } + +const char *scap_strerror(scap_t *handle, int errnum) +{ + int rc; + if((rc = strerror_r(errnum, handle->m_strerror_buf, SCAP_LASTERR_SIZE) != 0)) + { + if(rc != ERANGE) + { + snprintf(handle->m_strerror_buf, SCAP_LASTERR_SIZE, "Errno %d", errnum); + } + } + + return handle->m_strerror_buf; +} + +int32_t scap_update_suppressed(scap_t *handle, + const char *comm, + uint64_t tid, uint64_t ptid, + bool *suppressed) +{ + uint32_t i; + scap_tid *stid; + + *suppressed = false; + + HASH_FIND_INT64(handle->m_suppressed_tids, &ptid, stid); + + if(stid != NULL) + { + *suppressed = true; + } + else + { + for(i=0; i < handle->m_num_suppressed_comms; i++) + { + if(strcmp(handle->m_suppressed_comms[i], comm) == 0) + { + *suppressed = true; + break; + } + } + } + + // Also check to see if the tid is already in the set of + // suppressed tids. + + HASH_FIND_INT64(handle->m_suppressed_tids, &tid, stid); + + if(*suppressed && stid == NULL) + { + stid = (scap_tid *) malloc(sizeof(scap_tid)); + stid->tid = tid; + int32_t uth_status = SCAP_SUCCESS; + + HASH_ADD_INT64(handle->m_suppressed_tids, tid, stid); + + if(uth_status != SCAP_SUCCESS) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't add tid to suppressed hash table"); + free(stid); + return SCAP_FAILURE; + } + *suppressed = true; + } + else if (!*suppressed && stid != NULL) + { + HASH_DEL(handle->m_suppressed_tids, stid); + free(stid); + *suppressed = false; + } + + return SCAP_SUCCESS; +} + +int32_t scap_check_suppressed(scap_t *handle, scap_evt *pevent, bool *suppressed) +{ + uint16_t *lens; + char *valptr; + uint32_t j; + int32_t res = SCAP_SUCCESS; + const char *comm = NULL; + uint64_t *ptid = NULL; + scap_tid *stid; + + *suppressed = false; + + // For events that can create a new tid (fork, vfork, clone), + // we need to check the comm, which might also update the set + // of suppressed tids. + + switch(pevent->type) + { + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_20_X: + case PPME_SYSCALL_EXECVE_19_X: + + lens = (uint16_t *)((char *)pevent + sizeof(struct ppm_evt_hdr)); + valptr = (char *)lens + pevent->nparams * sizeof(uint16_t); + + if(pevent->nparams < 14) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not find process comm in event argument list"); + return SCAP_FAILURE; + } + + // For all of these events, the comm is argument 14, + // so we need to walk the list of params that far to + // find the comm. + for(j = 0; j < 13; j++) + { + if(j == 5) + { + ptid = (uint64_t *) valptr; + } + + valptr += lens[j]; + } + + if(ptid == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not find ptid in event argument list"); + return SCAP_FAILURE; + } + + comm = valptr; + + if((res = scap_update_suppressed(handle, + comm, + pevent->tid, *ptid, + suppressed)) != SCAP_SUCCESS) + { + // scap_update_suppressed already set handle->m_lasterr on error. + return res; + } + + break; + + default: + + HASH_FIND_INT64(handle->m_suppressed_tids, &(pevent->tid), stid); + + // When threads exit they are always removed and no longer suppressed. + if(pevent->type == PPME_PROCEXIT_1_E) + { + if(stid != NULL) + { + HASH_DEL(handle->m_suppressed_tids, stid); + free(stid); + *suppressed = true; + } + else + { + *suppressed = false; + } + } + else + { + *suppressed = (stid != NULL); + } + + break; + } + + return SCAP_SUCCESS; +} diff --git a/userspace/libscap/scap_savefile.c b/userspace/libscap/scap_savefile.c old mode 100644 new mode 100755 index a0d8a438da..8830f19012 --- a/userspace/libscap/scap_savefile.c +++ b/userspace/libscap/scap_savefile.c @@ -1,1682 +1,2888 @@ -/* -Copyright (C) 2013-2014 Draios inc. - -This file is part of sysdig. - -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. - -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . -*/ - -#ifndef _WIN32 -#include -#endif - -#include -#include - -#include "scap.h" -#include "scap-int.h" -#include "scap_savefile.h" - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// -// WRITE FUNCTIONS -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// -#ifndef _WIN32 -static inline uint32_t scap_normalize_block_len(uint32_t blocklen) -#else -static uint32_t scap_normalize_block_len(uint32_t blocklen) -#endif -{ - return ((blocklen + 3) >> 2) << 2; -} - -static int32_t scap_write_padding(FILE *f, uint32_t blocklen) -{ - int32_t val = 0; - int32_t bytestowrite = scap_normalize_block_len(blocklen) - blocklen; - - if(fwrite(&val, 1, bytestowrite, f) == bytestowrite) - { - return SCAP_SUCCESS; - } - else - { - return SCAP_FAILURE; - } -} - -static int32_t scap_write_proc_fds(scap_t *handle, struct scap_threadinfo *tinfo, FILE *f) -{ - block_header bh; - uint32_t bt; - uint32_t totlen = MEMBER_SIZE(scap_threadinfo, tid); // This includes the tid - struct scap_fdinfo *fdi; - struct scap_fdinfo *tfdi; - - // - // First pass of the table to calculate the length - // - HASH_ITER(hh, tinfo->fdlist, fdi, tfdi) - { - totlen += scap_fd_info_len(fdi); - } - - // - // Create the block - // - bh.block_type = FDL_BLOCK_TYPE; - bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + totlen + 4); - - if(fwrite(&bh, sizeof(bh), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd1)"); - return SCAP_FAILURE; - } - - // - // Write the tid - // - if(fwrite(&tinfo->tid, sizeof(tinfo->tid), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd2)"); - return SCAP_FAILURE; - } - - // - // Second pass pass of the table to dump it - // - HASH_ITER(hh, tinfo->fdlist, fdi, tfdi) - { - if(scap_fd_write_to_disk(handle, fdi, f) != SCAP_SUCCESS) - { - return SCAP_FAILURE; - } - } - - // - // Add the padding - // - if(scap_write_padding(f, totlen) != SCAP_SUCCESS) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd3)"); - return SCAP_FAILURE; - } - - // - // Create the trailer - // - bt = bh.block_total_length; - if(fwrite(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd4)"); - return SCAP_FAILURE; - } - - return SCAP_SUCCESS; -} - -// -// Write the fd list blocks -// -int32_t scap_write_fdlist(scap_t *handle, FILE *f) -{ - struct scap_threadinfo *tinfo; - struct scap_threadinfo *ttinfo; - int32_t res; - - HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo) - { - res = scap_write_proc_fds(handle, tinfo, f); - if(res != SCAP_SUCCESS) - { - return res; - } - } - - return SCAP_SUCCESS; -} - -// -// Write the process list block -// -int32_t scap_write_proclist(scap_t *handle, FILE *f) -{ - block_header bh; - uint32_t bt; - uint32_t totlen = 0; - struct scap_threadinfo *tinfo; - struct scap_threadinfo *ttinfo; - uint16_t commlen; - uint16_t exelen; - uint16_t argslen; - uint16_t cwdlen; - - // - // First pass pass of the table to calculate the length - // - HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo) - { - totlen += - sizeof(uint64_t) + // tid - sizeof(uint64_t) + // pid - sizeof(uint64_t) + // ptid - 2 + strnlen(tinfo->comm, SCAP_MAX_PATH_SIZE) + - 2 + strnlen(tinfo->exe, SCAP_MAX_PATH_SIZE) + - 2 + tinfo->args_len + - 2 + strnlen(tinfo->cwd, SCAP_MAX_PATH_SIZE) + - sizeof(uint64_t) + // fdlimit - sizeof(uint32_t) + // uid - sizeof(uint32_t) + // gid - sizeof(uint32_t); - } - - // - // Create the block - // - bh.block_type = PL_BLOCK_TYPE; - bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + totlen + 4); - - if(fwrite(&bh, sizeof(bh), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (1)"); - return SCAP_FAILURE; - } - - // - // Second pass pass of the table to dump it - // - HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo) - { - commlen = strnlen(tinfo->comm, SCAP_MAX_PATH_SIZE); - exelen = strnlen(tinfo->exe, SCAP_MAX_PATH_SIZE); - argslen = tinfo->args_len; - cwdlen = strnlen(tinfo->cwd, SCAP_MAX_PATH_SIZE); - - if(fwrite(&(tinfo->tid), sizeof(uint64_t), 1, f) != 1 || - fwrite(&(tinfo->pid), sizeof(uint64_t), 1, f) != 1 || - fwrite(&(tinfo->ptid), sizeof(uint64_t), 1, f) != 1 || - fwrite(&commlen, sizeof(uint16_t), 1, f) != 1 || - fwrite(tinfo->comm, 1, commlen, f) != commlen || - fwrite(&exelen, sizeof(uint16_t), 1, f) != 1 || - fwrite(tinfo->exe, 1, exelen, f) != exelen || - fwrite(&argslen, sizeof(uint16_t), 1, f) != 1 || - fwrite(tinfo->args, 1, argslen, f) != argslen || - fwrite(&cwdlen, sizeof(uint16_t), 1, f) != 1 || - fwrite(tinfo->cwd, 1, cwdlen, f) != cwdlen || - fwrite(&(tinfo->fdlimit), sizeof(uint64_t), 1, f) != 1 || - fwrite(&(tinfo->flags), sizeof(uint32_t), 1, f) != 1 || - fwrite(&(tinfo->uid), sizeof(uint32_t), 1, f) != 1 || - fwrite(&(tinfo->gid), sizeof(uint32_t), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (2)"); - return SCAP_FAILURE; - } - } - - // - // Blocks need to be 4-byte padded - // - if(scap_write_padding(f, totlen) != SCAP_SUCCESS) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (3)"); - return SCAP_FAILURE; - } - - // - // Create the trailer - // - bt = bh.block_total_length; - if(fwrite(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (4)"); - return SCAP_FAILURE; - } - - return SCAP_SUCCESS; -} - -// -// Write the machine info block -// -int32_t scap_write_machine_info(scap_t *handle, FILE *f) -{ - block_header bh; - uint32_t bt; - - // - // Write the section header - // - bh.block_type = MI_BLOCK_TYPE; - bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + sizeof(scap_machine_info) + 4); - - bt = bh.block_total_length; - - if(fwrite(&bh, sizeof(bh), 1, f) != 1 || - fwrite(&handle->m_machine_info, sizeof(handle->m_machine_info), 1, f) != 1 || - fwrite(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (MI1)"); - return SCAP_FAILURE; - } - - return SCAP_SUCCESS; -} - -// -// Write the interface list block -// -int32_t scap_write_iflist(scap_t *handle, FILE *f) -{ - block_header bh; - uint32_t bt; - uint32_t entrylen; - uint32_t totlen = 0; - uint32_t j; - - // - // Get the interface list - // - if(handle->m_addrlist == NULL) - { - ASSERT(false); - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to trace file: interface list missing"); - return SCAP_FAILURE; - } - - // - // Create the block - // - bh.block_type = IL_BLOCK_TYPE; - bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + handle->m_addrlist->totlen + 4); - - if(fwrite(&bh, sizeof(bh), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF1)"); - return SCAP_FAILURE; - } - - // - // Dump the ipv4 list - // - for(j = 0; j < handle->m_addrlist->n_v4_addrs; j++) - { - scap_ifinfo_ipv4 *entry = &(handle->m_addrlist->v4list[j]); - - entrylen = sizeof(scap_ifinfo_ipv4) + entry->ifnamelen - SCAP_MAX_PATH_SIZE; - - if(fwrite(entry, entrylen, 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF2)"); - return SCAP_FAILURE; - } - - totlen += entrylen; - } - - // - // Dump the ipv6 list - // - for(j = 0; j < handle->m_addrlist->n_v6_addrs; j++) - { - scap_ifinfo_ipv6 *entry = &(handle->m_addrlist->v6list[j]); - - entrylen = sizeof(scap_ifinfo_ipv6) + entry->ifnamelen - SCAP_MAX_PATH_SIZE; - - if(fwrite(entry, entrylen, 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF2)"); - return SCAP_FAILURE; - } - - totlen += entrylen; - } - - // - // Blocks need to be 4-byte padded - // - if(scap_write_padding(f, totlen) != SCAP_SUCCESS) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF3)"); - return SCAP_FAILURE; - } - - // - // Create the trailer - // - bt = bh.block_total_length; - if(fwrite(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF4)"); - return SCAP_FAILURE; - } - - return SCAP_SUCCESS; -} - -// -// Write the user list block -// -int32_t scap_write_userlist(scap_t *handle, FILE *f) -{ - block_header bh; - uint32_t bt; - uint32_t j; - uint16_t namelen; - uint16_t homedirlen; - uint16_t shelllen; - uint8_t type; - uint32_t totlen = 0; - - // - // Make sure we have a user list interface list - // - if(handle->m_userlist == NULL) - { - ASSERT(false); - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to trace file: user list missing"); - return SCAP_FAILURE; - } - - // - // Calculate the block length - // - for(j = 0; j < handle->m_userlist->nusers; j++) - { - scap_userinfo* info = &handle->m_userlist->users[j]; - - namelen = strnlen(info->name, MAX_CREDENTIALS_STR_LEN); - homedirlen = strnlen(info->homedir, SCAP_MAX_PATH_SIZE); - shelllen = strnlen(info->shell, SCAP_MAX_PATH_SIZE); - - totlen += sizeof(type) + sizeof(info->uid) + sizeof(info->gid) + sizeof(uint16_t) + - namelen + sizeof(uint16_t) + homedirlen + sizeof(uint16_t) + shelllen; - } - - for(j = 0; j < handle->m_userlist->ngroups; j++) - { - scap_groupinfo* info = &handle->m_userlist->groups[j]; - - namelen = strnlen(info->name, MAX_CREDENTIALS_STR_LEN); - - totlen += sizeof(type) + sizeof(info->gid) + sizeof(uint16_t) + namelen; - } - - // - // Create the block - // - bh.block_type = UL_BLOCK_TYPE; - bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + totlen + 4); - - if(fwrite(&bh, sizeof(bh), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF1)"); - return SCAP_FAILURE; - } - - // - // Dump the users - // - type = USERBLOCK_TYPE_USER; - for(j = 0; j < handle->m_userlist->nusers; j++) - { - scap_userinfo* info = &handle->m_userlist->users[j]; - - namelen = strnlen(info->name, MAX_CREDENTIALS_STR_LEN); - homedirlen = strnlen(info->homedir, SCAP_MAX_PATH_SIZE); - shelllen = strnlen(info->shell, SCAP_MAX_PATH_SIZE); - - if(fwrite(&(type), sizeof(type), 1, f) != 1 || - fwrite(&(info->uid), sizeof(info->uid), 1, f) != 1 || - fwrite(&(info->gid), sizeof(info->gid), 1, f) != 1 || - fwrite(&namelen, sizeof(uint16_t), 1, f) != 1 || - fwrite(info->name, 1, namelen, f) != namelen || - fwrite(&homedirlen, sizeof(uint16_t), 1, f) != 1 || - fwrite(info->homedir, 1, homedirlen, f) != homedirlen || - fwrite(&shelllen, sizeof(uint16_t), 1, f) != 1 || - fwrite(info->shell, 1, shelllen, f) != shelllen) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (U1)"); - return SCAP_FAILURE; - } - } - - // - // Dump the groups - // - type = USERBLOCK_TYPE_GROUP; - for(j = 0; j < handle->m_userlist->ngroups; j++) - { - scap_groupinfo* info = &handle->m_userlist->groups[j]; - - namelen = strnlen(info->name, MAX_CREDENTIALS_STR_LEN); - - if(fwrite(&(type), sizeof(type), 1, f) != 1 || - fwrite(&(info->gid), sizeof(info->gid), 1, f) != 1 || - fwrite(&namelen, sizeof(uint16_t), 1, f) != 1 || - fwrite(info->name, 1, namelen, f) != namelen) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (U2)"); - return SCAP_FAILURE; - } - } - - // - // Blocks need to be 4-byte padded - // - if(scap_write_padding(f, totlen) != SCAP_SUCCESS) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF3)"); - return SCAP_FAILURE; - } - - // - // Create the trailer - // - bt = bh.block_total_length; - if(fwrite(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF4)"); - return SCAP_FAILURE; - } - - return SCAP_SUCCESS; -} - -// -// Create the dump file headers and add the tables -// -static scap_dumper_t *scap_setup_dump(scap_t *handle, FILE *f, const char *fname) -{ - block_header bh; - section_header_block sh; - uint32_t bt; - - // - // Write the section header - // - bh.block_type = SHB_BLOCK_TYPE; - bh.block_total_length = sizeof(block_header) + sizeof(section_header_block) + 4; - - sh.byte_order_magic = SHB_MAGIC; - sh.major_version = CURRENT_MAJOR_VERSION; - sh.minor_version = CURRENT_MINOR_VERSION; - sh.section_length = 0xffffffffffffffffLL; - - bt = bh.block_total_length; - - if(fwrite(&bh, sizeof(bh), 1, f) != 1 || - fwrite(&sh, sizeof(sh), 1, f) != 1 || - fwrite(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file %s (5)", fname); - return NULL; - } - - // - // If we're dumping in live mode, refresh the process tables list - // so we don't lose information about processes created in the interval - // between opening the handle and starting the dump - // -#if !defined(_WIN32) && !defined(__APPLE__) - if(handle->m_file == NULL) - { - scap_proc_free_table(handle); - if(scap_proc_scan_proc_dir(handle, "/proc", -1, -1, NULL, handle->m_lasterr, true) != SCAP_SUCCESS) - { - return NULL; - } - } -#endif - - // - // Write the machine info - // - if(scap_write_machine_info(handle, f) != SCAP_SUCCESS) - { - return NULL; - } - - // - // Write the interface list - // - if(scap_write_iflist(handle, f) != SCAP_SUCCESS) - { - return NULL; - } - - // - // Write the user list - // - if(scap_write_userlist(handle, f) != SCAP_SUCCESS) - { - return NULL; - } - - // - // Write the process list - // - if(scap_write_proclist(handle, f) != SCAP_SUCCESS) - { - return NULL; - } - - // - // Write the fd lists - // - - if(scap_write_fdlist(handle, f) != SCAP_SUCCESS) - { - return NULL; - } - - // - // Done, return the file - // - return (scap_dumper_t *)f; -} - -// -// Open a "savefile" for writing. -// -scap_dumper_t *scap_dump_open(scap_t *handle, const char *fname) -{ - FILE *f; - - if(fname[0] == '-' && fname[1] == '\0') - { - f = stdout; - fname = "standard output"; - } - else - { - f = fopen(fname, "wb"); - - if(f == NULL) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't open %s", fname); - return NULL; - } - } - - return scap_setup_dump(handle, f, fname); -} - -// -// Close a "savefile" opened with scap_dump_open -// -void scap_dump_close(scap_dumper_t *d) -{ - fclose((FILE *)d); -} - -// -// Return the current size of a tracefile -// -uint64_t scap_dump_ftell(scap_dumper_t *d) -{ - return (uint64_t)ftell((FILE *)d); -} - -// -// Write an event to a dump file -// -int32_t scap_dump(scap_t *handle, scap_dumper_t *d, scap_evt *e, uint16_t cpuid) -{ - block_header bh; - uint32_t bt; - FILE *f = (FILE *)d; - - // - // Write the section header - // - bh.block_type = EV_BLOCK_TYPE; - bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + sizeof(cpuid) + e->len + 4); - bt = bh.block_total_length; - - if(fwrite(&bh, sizeof(bh), 1, f) != 1 || - fwrite(&cpuid, sizeof(cpuid), 1, f) != 1 || - fwrite(e, e->len, 1, f) != 1 || - scap_write_padding(f, sizeof(cpuid) + e->len) != SCAP_SUCCESS || - fwrite(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (6)"); - return SCAP_FAILURE; - } - - // - // Enalbe this to make sure that everything is saved to disk during the tests - // -#if 0 - fflush(f); -#endif - - return SCAP_SUCCESS; -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// -// READ FUNCTIONS -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -// -// Load the machine info block -// -int32_t scap_read_machine_info(scap_t *handle, FILE *f, uint32_t block_length) -{ - // - // Read the section header block - // - if(fread(&handle->m_machine_info, sizeof(handle->m_machine_info), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading from file (1)"); - return SCAP_FAILURE; - } - - return SCAP_SUCCESS; -} - -// -// Parse a process list block -// -int32_t scap_read_proclist(scap_t *handle, FILE *f, uint32_t block_length) -{ - size_t readsize; - size_t totreadsize = 0; - struct scap_threadinfo tinfo; - uint16_t stlen; - uint32_t padding; - int32_t padding_len; - int32_t uth_status = SCAP_SUCCESS; - struct scap_threadinfo *ntinfo; - - tinfo.fdlist = NULL; - tinfo.flags = 0; - - while(((int32_t)block_length - (int32_t)totreadsize) >= 4) - { - // - // tid - // - readsize = fread(&(tinfo.tid), 1, sizeof(uint64_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint64_t)); - - totreadsize += readsize; - - // - // pid - // - readsize = fread(&(tinfo.pid), 1, sizeof(uint64_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint64_t)); - - totreadsize += readsize; - - // - // ptid - // - readsize = fread(&(tinfo.ptid), 1, sizeof(uint64_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint64_t)); - - totreadsize += readsize; - - // - // comm - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= SCAP_MAX_PATH_SIZE) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid commlen %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(tinfo.comm, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - tinfo.comm[stlen] = 0; - - totreadsize += readsize; - - // - // exe - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= SCAP_MAX_PATH_SIZE) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid exelen %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(tinfo.exe, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - tinfo.exe[stlen] = 0; - - totreadsize += readsize; - - // - // args - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= SCAP_MAX_PATH_SIZE) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid argslen %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(tinfo.args, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - tinfo.args[stlen] = 0; - tinfo.args_len = stlen; - - totreadsize += readsize; - - // - // cwd - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= SCAP_MAX_PATH_SIZE) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid cwdlen %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(tinfo.cwd, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - tinfo.cwd[stlen] = 0; - - totreadsize += readsize; - - // - // fdlimit - // - readsize = fread(&(tinfo.fdlimit), 1, sizeof(uint64_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint64_t)); - - totreadsize += readsize; - - // - // flags - // - readsize = fread(&(tinfo.flags), 1, sizeof(uint32_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint32_t)); - - totreadsize += readsize; - - // - // uid - // - readsize = fread(&(tinfo.uid), 1, sizeof(uint32_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint32_t)); - - totreadsize += readsize; - - // - // gid - // - readsize = fread(&(tinfo.gid), 1, sizeof(uint32_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint32_t)); - - totreadsize += readsize; - - // - // All parsed. Allocate the new entry and copy the temp one into into it. - // - ntinfo = (scap_threadinfo *)malloc(sizeof(scap_threadinfo)); - if(ntinfo == NULL) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd1)"); - return SCAP_FAILURE; - } - - // Structure copy - *ntinfo = tinfo; - - // - // All parsed. Add the entry to the table - // - HASH_ADD_INT64(handle->m_proclist, tid, ntinfo); - if(uth_status != SCAP_SUCCESS) - { - free(ntinfo); - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd2)"); - return SCAP_FAILURE; - } - } - - // - // Read the padding bytes so we properly align to the end of the data - // - padding_len = ((int32_t)block_length - (int32_t)totreadsize); - ASSERT(padding_len >= 0); - - readsize = fread(&padding, 1, padding_len, f); - CHECK_READ_SIZE(readsize, padding_len); - - return SCAP_SUCCESS; -} - -// -// Parse an interface list block -// -int32_t scap_read_iflist(scap_t *handle, FILE *f, uint32_t block_length) -{ - int32_t res = SCAP_SUCCESS; - size_t readsize; - size_t totreadsize; - char *readbuf = NULL; - char *pif; - uint16_t iftype; - uint16_t ifnamlen; - uint32_t toread; - uint32_t entrysize; - uint32_t ifcnt4 = 0; - uint32_t ifcnt6 = 0; - - // - // If the list of interfaces was already allocated for this handle (for example because this is - // not the first interface list block), free it - // - if(handle->m_addrlist != NULL) - { - scap_free_iflist(handle->m_addrlist); - handle->m_addrlist = NULL; - } - - // - // Bring the block to memory - // We assume that this block is always small enough that we can read it in a single shot - // - readbuf = (char *)malloc(block_length); - if(!readbuf) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_iflist"); - return SCAP_FAILURE; - } - - readsize = fread(readbuf, 1, block_length, f); - CHECK_READ_SIZE(readsize, block_length); - - // - // First pass, count the number of addresses - // - pif = readbuf; - totreadsize = 0; - - while(true) - { - toread = (int32_t)block_length - (int32_t)totreadsize; - - if(toread < 4) - { - break; - } - - iftype = *(uint16_t *)pif; - ifnamlen = *(uint16_t *)(pif + 2); - - if(iftype == SCAP_II_IPV4) - { - entrysize = sizeof(scap_ifinfo_ipv4) + ifnamlen - SCAP_MAX_PATH_SIZE; - } - else if(iftype == SCAP_II_IPV6) - { - entrysize = sizeof(scap_ifinfo_ipv6) + ifnamlen - SCAP_MAX_PATH_SIZE; - } - else if(iftype == SCAP_II_IPV4_NOLINKSPEED) - { - entrysize = sizeof(scap_ifinfo_ipv4_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; - } - else if(iftype == SCAP_II_IPV6_NOLINKSPEED) - { - entrysize = sizeof(scap_ifinfo_ipv6_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; - } - else - { - ASSERT(false); - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(1)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - if(toread < entrysize) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(2)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - pif += entrysize; - totreadsize += entrysize; - - if(iftype == SCAP_II_IPV4 || iftype == SCAP_II_IPV4_NOLINKSPEED) - { - ifcnt4++; - } - else if(iftype == SCAP_II_IPV6 || iftype == SCAP_II_IPV6_NOLINKSPEED) - { - ifcnt6++; - } - else - { - ASSERT(false); - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "unknown interface type %d", (int)iftype); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - } - - // - // Allocate the handle and the arrays - // - handle->m_addrlist = (scap_addrlist *)malloc(sizeof(scap_addrlist)); - if(!handle->m_addrlist) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_read_iflist allocation failed(1)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - handle->m_addrlist->n_v4_addrs = 0; - handle->m_addrlist->n_v6_addrs = 0; - handle->m_addrlist->v4list = NULL; - handle->m_addrlist->v6list = NULL; - handle->m_addrlist->totlen = block_length; - - if(ifcnt4 != 0) - { - handle->m_addrlist->v4list = (scap_ifinfo_ipv4 *)malloc(ifcnt4 * sizeof(scap_ifinfo_ipv4)); - if(!handle->m_addrlist->v4list) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_read_iflist allocation failed(2)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - } - else - { - handle->m_addrlist->v4list = NULL; - } - - if(ifcnt6 != 0) - { - handle->m_addrlist->v6list = (scap_ifinfo_ipv6 *)malloc(ifcnt6 * sizeof(scap_ifinfo_ipv6)); - if(!handle->m_addrlist->v6list) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "getifaddrs allocation failed(3)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - } - else - { - handle->m_addrlist->v6list = NULL; - } - - handle->m_addrlist->n_v4_addrs = ifcnt4; - handle->m_addrlist->n_v6_addrs = ifcnt6; - - // - // Second pass: populate the arrays - // - ifcnt4 = 0; - ifcnt6 = 0; - pif = readbuf; - totreadsize = 0; - - while(true) - { - toread = (int32_t)block_length - (int32_t)totreadsize; - - if(toread < 4) - { - break; - } - - iftype = *(uint16_t *)pif; - ifnamlen = *(uint16_t *)(pif + 2); - - if(ifnamlen >= SCAP_MAX_PATH_SIZE) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(0)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - if(iftype == SCAP_II_IPV4) - { - entrysize = sizeof(scap_ifinfo_ipv4) + ifnamlen - SCAP_MAX_PATH_SIZE; - - if(toread < entrysize) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(1)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - // Copy the entry - memcpy(handle->m_addrlist->v4list + ifcnt4, pif, entrysize); - - // Make sure the name string is NULL-terminated - *((char *)(handle->m_addrlist->v4list + ifcnt4) + entrysize) = 0; - - pif += entrysize; - totreadsize += entrysize; - - ifcnt4++; - } - else if(iftype == SCAP_II_IPV4_NOLINKSPEED) - { - scap_ifinfo_ipv4_nolinkspeed* src; - scap_ifinfo_ipv4* dst; - - entrysize = sizeof(scap_ifinfo_ipv4_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; - - if(toread < entrysize) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(1)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - // Copy the entry - src = (scap_ifinfo_ipv4_nolinkspeed*)pif; - dst = handle->m_addrlist->v4list + ifcnt4; - - dst->type = src->type; - dst->ifnamelen = src->ifnamelen; - dst->addr = src->addr; - dst->netmask = src->netmask; - dst->bcast = src->bcast; - dst->linkspeed = 0; - memcpy(dst->ifname, src->ifname, MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1)); - - // Make sure the name string is NULL-terminated - *((char *)(dst->ifname + MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1))) = 0; - - pif += entrysize; - totreadsize += entrysize; - - ifcnt4++; - } - else if(iftype == SCAP_II_IPV6) - { - entrysize = sizeof(scap_ifinfo_ipv6) + ifnamlen - SCAP_MAX_PATH_SIZE; - - if(toread < entrysize) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(1)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - // Copy the entry - memcpy(handle->m_addrlist->v6list + ifcnt6, pif, entrysize); - - // Make sure the name string is NULL-terminated - *((char *)(handle->m_addrlist->v6list + ifcnt6) + entrysize) = 0; - - pif += entrysize; - totreadsize += entrysize; - - ifcnt6++; - } - else if(iftype == SCAP_II_IPV6_NOLINKSPEED) - { - scap_ifinfo_ipv6_nolinkspeed* src; - scap_ifinfo_ipv6* dst; - entrysize = sizeof(scap_ifinfo_ipv6_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; - - if(toread < entrysize) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(1)"); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - - // Copy the entry - src = (scap_ifinfo_ipv6_nolinkspeed*)pif; - dst = handle->m_addrlist->v6list + ifcnt6; - - dst->type = src->type; - dst->ifnamelen = src->ifnamelen; - memcpy(dst->addr, src->addr, SCAP_IPV6_ADDR_LEN); - memcpy(dst->netmask, src->netmask, SCAP_IPV6_ADDR_LEN); - memcpy(dst->bcast, src->bcast, SCAP_IPV6_ADDR_LEN); - dst->linkspeed = 0; - memcpy(dst->ifname, src->ifname, MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1)); - - // Make sure the name string is NULL-terminated - *((char *)(dst->ifname + MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1))) = 0; - - pif += entrysize; - totreadsize += entrysize; - - ifcnt6++; - } - else - { - ASSERT(false); - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "unknown interface type %d", (int)iftype); - res = SCAP_FAILURE; - goto scap_read_iflist_error; - } - } - - // - // Release the read storage - // - free(readbuf); - - return res; - -scap_read_iflist_error: - scap_free_iflist(handle->m_addrlist); - - if(readbuf) - { - free(readbuf); - } - - return res; -} - -// -// Parse a user list block -// -int32_t scap_read_userlist(scap_t *handle, FILE *f, uint32_t block_length) -{ - size_t readsize; - size_t totreadsize = 0; - uint32_t padding; - int32_t padding_len; - uint8_t type; - uint16_t stlen; - - // - // If the list of users was already allocated for this handle (for example because this is - // not the first interface list block), free it - // - if(handle->m_userlist != NULL) - { - scap_free_userlist(handle->m_userlist); - handle->m_userlist = NULL; - } - - // - // Allocate and initialize the handle info - // - handle->m_userlist = (scap_userlist*)malloc(sizeof(scap_userlist)); - if(handle->m_userlist == NULL) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "userlist allocation failed(2)"); - return SCAP_FAILURE; - } - - handle->m_userlist->nusers = 0; - handle->m_userlist->ngroups = 0; - handle->m_userlist->totsavelen = 0; - handle->m_userlist->users = NULL; - handle->m_userlist->groups = NULL; - - // - // Import the blocks - // - while(((int32_t)block_length - (int32_t)totreadsize) >= 4) - { - // - // type - // - readsize = fread(&(type), 1, sizeof(type), f); - CHECK_READ_SIZE(readsize, sizeof(type)); - - totreadsize += readsize; - - if(type == USERBLOCK_TYPE_USER) - { - scap_userinfo* puser; - - handle->m_userlist->nusers++; - handle->m_userlist->users = (scap_userinfo*)realloc(handle->m_userlist->users, handle->m_userlist->nusers * sizeof(scap_userinfo)); - if(handle->m_userlist->users == NULL) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_userlist(1)"); - return SCAP_FAILURE; - } - - puser = &handle->m_userlist->users[handle->m_userlist->nusers -1]; - - // - // uid - // - readsize = fread(&(puser->uid), 1, sizeof(uint32_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint32_t)); - - totreadsize += readsize; - - // - // gid - // - readsize = fread(&(puser->gid), 1, sizeof(uint32_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint32_t)); - - totreadsize += readsize; - - // - // name - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= MAX_CREDENTIALS_STR_LEN) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid user name len %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(puser->name, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - puser->name[stlen] = 0; - - totreadsize += readsize; - - // - // homedir - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= MAX_CREDENTIALS_STR_LEN) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid user homedir len %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(puser->homedir, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - puser->homedir[stlen] = 0; - - totreadsize += readsize; - - // - // shell - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= MAX_CREDENTIALS_STR_LEN) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid user shell len %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(puser->shell, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - puser->shell[stlen] = 0; - - totreadsize += readsize; - } - else - { - scap_groupinfo* pgroup; - - handle->m_userlist->ngroups++; - handle->m_userlist->groups = (scap_groupinfo*)realloc(handle->m_userlist->groups, handle->m_userlist->ngroups * sizeof(scap_groupinfo)); - if(handle->m_userlist->groups == NULL) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_userlist(2)"); - return SCAP_FAILURE; - } - - pgroup = &handle->m_userlist->groups[handle->m_userlist->ngroups -1]; - - // - // gid - // - readsize = fread(&(pgroup->gid), 1, sizeof(uint32_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint32_t)); - - totreadsize += readsize; - - // - // name - // - readsize = fread(&(stlen), 1, sizeof(uint16_t), f); - CHECK_READ_SIZE(readsize, sizeof(uint16_t)); - - if(stlen >= MAX_CREDENTIALS_STR_LEN) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid group name len %d", stlen); - return SCAP_FAILURE; - } - - totreadsize += readsize; - - readsize = fread(pgroup->name, 1, stlen, f); - CHECK_READ_SIZE(readsize, stlen); - - // the string is not null-terminated on file - pgroup->name[stlen] = 0; - - totreadsize += readsize; - } - } - - // - // Read the padding bytes so we properly align to the end of the data - // - padding_len = ((int32_t)block_length - (int32_t)totreadsize); - ASSERT(padding_len >= 0); - - readsize = fread(&padding, 1, padding_len, f); - CHECK_READ_SIZE(readsize, padding_len); - - return SCAP_SUCCESS; -} - -// -// Parse a process list block -// -int32_t scap_read_fdlist(scap_t *handle, FILE *f, uint32_t block_length) -{ - size_t readsize; - size_t totreadsize = 0; - struct scap_threadinfo *tinfo; - scap_fdinfo fdi; - scap_fdinfo *nfdi; - // uint16_t stlen; - uint64_t tid; - int32_t uth_status = SCAP_SUCCESS; - uint32_t padding; - int32_t padding_len; - - // - // Read the tid - // - readsize = fread(&tid, 1, sizeof(tid), f); - CHECK_READ_SIZE(readsize, sizeof(tid)); - totreadsize += readsize; - - // - // Identify the process descriptor - // - HASH_FIND_INT64(handle->m_proclist, &tid, tinfo); - if(tinfo == NULL) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted trace file. FD block references TID %"PRIu64", which doesn't exist.", - tid); - return SCAP_FAILURE; - } - - while(((int32_t)block_length - (int32_t)totreadsize) >= 4) - { - if(scap_fd_read_from_disk(handle, &fdi, &readsize, f) != SCAP_SUCCESS) - { - return SCAP_FAILURE; - } - totreadsize += readsize; - - // - // Parsed successfully. Allocate the new entry and copy the temp one into into it. - // - nfdi = (scap_fdinfo *)malloc(sizeof(scap_fdinfo)); - if(nfdi == NULL) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd1)"); - return SCAP_FAILURE; - } - - // Structure copy - *nfdi = fdi; - - // - // Add the entry to the table - // - HASH_ADD_INT64(tinfo->fdlist, fd, nfdi); - if(uth_status != SCAP_SUCCESS) - { - free(nfdi); - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd2)"); - return SCAP_FAILURE; - } - } - - // - // Read the padding bytes so we properly align to the end of the data - // - padding_len = ((int32_t)block_length - (int32_t)totreadsize); - ASSERT(padding_len >= 0); - - readsize = fread(&padding, 1, padding_len, f); - CHECK_READ_SIZE(readsize, padding_len); - - return SCAP_SUCCESS; -} - -// -// Parse the headers of a trace file and load the tables -// -int32_t scap_read_init(scap_t *handle, FILE *f) -{ - block_header bh; - section_header_block sh; - uint32_t bt; - size_t readsize; - size_t toread; - int fseekres; - - // - // Read the section header block - // - if(fread(&bh, sizeof(bh), 1, f) != 1 || - fread(&sh, sizeof(sh), 1, f) != 1 || - fread(&bt, sizeof(bt), 1, f) != 1) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading from file (1)"); - return SCAP_FAILURE; - } - - if(bh.block_type != SHB_BLOCK_TYPE) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid block type"); - return SCAP_FAILURE; - } - - if(sh.byte_order_magic != 0x1a2b3c4d) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid magic number"); - return SCAP_FAILURE; - } - - // - // Read the metadata blocks (processes, FDs, etc.) - // - while(true) - { - readsize = fread(&bh, 1, sizeof(bh), f); - CHECK_READ_SIZE(readsize, sizeof(bh)); - - switch(bh.block_type) - { - case MI_BLOCK_TYPE: - case MI_BLOCK_TYPE_INT: - if(scap_read_machine_info(handle, f, bh.block_total_length - sizeof(block_header) - 4) != SCAP_SUCCESS) - { - return SCAP_FAILURE; - } - break; - case PL_BLOCK_TYPE: - case PL_BLOCK_TYPE_INT: - if(scap_read_proclist(handle, f, bh.block_total_length - sizeof(block_header) - 4) != SCAP_SUCCESS) - { - return SCAP_FAILURE; - } - break; - case FDL_BLOCK_TYPE: - case FDL_BLOCK_TYPE_INT: - if(scap_read_fdlist(handle, f, bh.block_total_length - sizeof(block_header) - 4) != SCAP_SUCCESS) - { - return SCAP_FAILURE; - } - break; - case EV_BLOCK_TYPE: - case EV_BLOCK_TYPE_INT: - // - // We're done with the metadata headers. Rewind the file position so we are aligned to start reading the events. - // - fseekres = fseek(f, (long)0 - sizeof(bh), SEEK_CUR); - if(fseekres == 0) - { - return SCAP_SUCCESS; - } - else - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error seeking in file"); - return SCAP_FAILURE; - } - case IL_BLOCK_TYPE: - case IL_BLOCK_TYPE_INT: - if(scap_read_iflist(handle, f, bh.block_total_length - sizeof(block_header) - 4) != SCAP_SUCCESS) - { - return SCAP_FAILURE; - } - break; - case UL_BLOCK_TYPE: - case UL_BLOCK_TYPE_INT: - if(scap_read_userlist(handle, f, bh.block_total_length - sizeof(block_header) - 4) != SCAP_SUCCESS) - { - return SCAP_FAILURE; - } - break; - default: - // - // Unknwon block type. Skip the block. - // - toread = bh.block_total_length - sizeof(block_header) - 4; - fseekres = fseek(f, toread, SEEK_CUR); - if(fseekres != 0) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't skip block of type %x and size %u.", - (int)bh.block_type, - (unsigned int)toread); - return SCAP_FAILURE; - } - break; - } - - // - // Read and validate the trailer - // - readsize = fread(&bt, 1, sizeof(bt), f); - CHECK_READ_SIZE(readsize, sizeof(bt)); - - if(bt != bh.block_total_length) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "wrong block total length, header=%u, trailer=%u", - bh.block_total_length, - bt); - return SCAP_FAILURE; - } - } - - return SCAP_SUCCESS; -} - -// -// Read an event from disk -// -int32_t scap_next_offline(scap_t *handle, OUT scap_evt **pevent, OUT uint16_t *pcpuid) -{ - block_header bh; - size_t readsize; - uint32_t readlen; - FILE *f = handle->m_file; - - ASSERT(f != NULL); - - // - // Read the block header - // - readsize = fread(&bh, 1, sizeof(bh), f); - if(readsize != sizeof(bh)) - { - if(readsize == 0) - { - // - // We read exactly 0 bytes. This indicates a correct end of file. - // - return SCAP_EOF; - } - else - { - CHECK_READ_SIZE(readsize, sizeof(bh)); - } - } - - if(bh.block_type != EV_BLOCK_TYPE && bh.block_type != EV_BLOCK_TYPE_INT) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "unexpected block type %u", (uint32_t)bh.block_type); - return SCAP_FAILURE; - } - - if(bh.block_total_length < sizeof(bh) + sizeof(struct ppm_evt_hdr) + 4) - { - snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "block length too short %u", (uint32_t)bh.block_total_length); - return SCAP_FAILURE; - } - - // - // Read the event - // - readlen = bh.block_total_length - sizeof(bh); - readsize = fread(handle->m_file_evt_buf, 1, readlen, f); - CHECK_READ_SIZE(readsize, readlen); - - *pcpuid = *(uint16_t *)handle->m_file_evt_buf; - *pevent = (struct ppm_evt_hdr *)(handle->m_file_evt_buf + sizeof(uint16_t)); - return SCAP_SUCCESS; -} +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include + +#ifndef _WIN32 +#include +#include +#else +struct iovec { + void *iov_base; /* Starting address */ + size_t iov_len; /* Number of bytes to transfer */ +}; +#endif + +#include "scap.h" +#include "scap-int.h" +#include "scap_savefile.h" + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +// WRITE FUNCTIONS +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +// +// Write data into a dump file +// +int scap_dump_write(scap_dumper_t *d, void* buf, unsigned len) +{ + if(d->m_type == DT_FILE) + { + return gzwrite(d->m_f, buf, len); + } + else + { + if(d->m_targetbufcurpos + len < d->m_targetbufend) + { + memcpy(d->m_targetbufcurpos, buf, len); + + d->m_targetbufcurpos += len; + return len; + } + else + { + return -1; + } + } +} + +int scap_dump_writev(scap_dumper_t *d, const struct iovec *iov, int iovcnt) +{ + unsigned totlen = 0; + int i; + + for (i = 0; i < iovcnt; i++) + { + if(scap_dump_write(d, iov[i].iov_base, iov[i].iov_len) < 0) + { + return -1; + } + + totlen += iov[i].iov_len; + } + + return totlen; +} + +#ifdef USE_ZLIB +int32_t compr(uint8_t* dest, uint64_t* destlen, const uint8_t* source, uint64_t sourcelen, int level) +{ + uLongf dl = compressBound(sourcelen); + + if(dl >= *destlen) + { + return SCAP_FAILURE; + } + + int res = compress2(dest, &dl, source, sourcelen, level); + if(res == Z_OK) + { + *destlen = (uint64_t)dl; + return SCAP_SUCCESS; + } + else + { + return SCAP_FAILURE; + } +} +#endif + +uint8_t* scap_get_memorydumper_curpos(scap_dumper_t *d) +{ + return d->m_targetbufcurpos; +} + +#ifndef _WIN32 +static inline uint32_t scap_normalize_block_len(uint32_t blocklen) +#else +static uint32_t scap_normalize_block_len(uint32_t blocklen) +#endif +{ + return ((blocklen + 3) >> 2) << 2; +} + +static int32_t scap_write_padding(scap_dumper_t *d, uint32_t blocklen) +{ + int32_t val = 0; + uint32_t bytestowrite = scap_normalize_block_len(blocklen) - blocklen; + + if(scap_dump_write(d, &val, bytestowrite) == bytestowrite) + { + return SCAP_SUCCESS; + } + else + { + return SCAP_FAILURE; + } +} + +int32_t scap_write_proc_fds(scap_t *handle, struct scap_threadinfo *tinfo, scap_dumper_t *d) +{ + block_header bh; + uint32_t bt; + uint32_t totlen = MEMBER_SIZE(scap_threadinfo, tid); // This includes the tid + uint32_t idx = 0; + struct scap_fdinfo *fdi; + struct scap_fdinfo *tfdi; + + uint32_t* lengths = calloc(HASH_COUNT(tinfo->fdlist), sizeof(uint32_t)); + if(lengths == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_write_proc_fds memory allocation failure"); + return SCAP_FAILURE; + } + + // + // First pass of the table to calculate the lengths + // + HASH_ITER(hh, tinfo->fdlist, fdi, tfdi) + { + if(fdi->type != SCAP_FD_UNINITIALIZED && + fdi->type != SCAP_FD_UNKNOWN) + { + uint32_t fl = scap_fd_info_len(fdi); + lengths[idx++] = fl; + totlen += fl; + } + } + idx = 0; + + // + // Create the block + // + bh.block_type = FDL_BLOCK_TYPE_V2; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + totlen + 4); + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh)) + { + free(lengths); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd1)"); + return SCAP_FAILURE; + } + + // + // Write the tid + // + if(scap_dump_write(d, &tinfo->tid, sizeof(tinfo->tid)) != sizeof(tinfo->tid)) + { + free(lengths); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd2)"); + return SCAP_FAILURE; + } + + // + // Second pass of the table to dump it + // + HASH_ITER(hh, tinfo->fdlist, fdi, tfdi) + { + if(fdi->type != SCAP_FD_UNINITIALIZED && fdi->type != SCAP_FD_UNKNOWN) + { + if(scap_fd_write_to_disk(handle, fdi, d, lengths[idx++]) != SCAP_SUCCESS) + { + free(lengths); + return SCAP_FAILURE; + } + } + } + + free(lengths); + + // + // Add the padding + // + if(scap_write_padding(d, totlen) != SCAP_SUCCESS) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd3)"); + return SCAP_FAILURE; + } + + // + // Create the trailer + // + bt = bh.block_total_length; + if(scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fd4)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Write the fd list blocks +// +static int32_t scap_write_fdlist(scap_t *handle, scap_dumper_t *d) +{ + struct scap_threadinfo *tinfo; + struct scap_threadinfo *ttinfo; + int32_t res; + + HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo) + { + if(!tinfo->filtered_out) + { + res = scap_write_proc_fds(handle, tinfo, d); + if(res != SCAP_SUCCESS) + { + return res; + } + } + } + + return SCAP_SUCCESS; +} + +// +// Write the process list block +// +int32_t scap_write_proclist_header(scap_t *handle, scap_dumper_t *d, uint32_t totlen) +{ + block_header bh; + + // + // Create the block header + // + bh.block_type = PL_BLOCK_TYPE_V9; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + totlen + 4); + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (1)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Write the process list block +// +int32_t scap_write_proclist_trailer(scap_t *handle, scap_dumper_t *d, uint32_t totlen) +{ + block_header bh; + uint32_t bt; + + bh.block_type = PL_BLOCK_TYPE_V9; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + totlen + 4); + + // + // Blocks need to be 4-byte padded + // + if(scap_write_padding(d, totlen) != SCAP_SUCCESS) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (3)"); + return SCAP_FAILURE; + } + + // + // Create the trailer + // + bt = bh.block_total_length; + if(scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (4)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Write the process list block +// +int32_t scap_write_proclist_entry(scap_t *handle, scap_dumper_t *d, struct scap_threadinfo *tinfo, uint32_t len) +{ + struct iovec args = {tinfo->args, tinfo->args_len}; + struct iovec env = {tinfo->env, tinfo->env_len}; + struct iovec cgroups = {tinfo->cgroups, tinfo->cgroups_len}; + + return scap_write_proclist_entry_bufs(handle, d, tinfo, len, + tinfo->comm, + tinfo->exe, + tinfo->exepath, + &args, 1, + &env, 1, + tinfo->cwd, + &cgroups, 1, + tinfo->root); +} + +static uint16_t iov_size(const struct iovec *iov, uint32_t iovcnt) +{ + uint16_t len = 0; + uint32_t i; + + for (i = 0; i < iovcnt; i++) + { + len += iov[i].iov_len; + } + + return len; +} + +int32_t scap_write_proclist_entry_bufs(scap_t *handle, scap_dumper_t *d, struct scap_threadinfo *tinfo, uint32_t len, + const char *comm, + const char *exe, + const char *exepath, + const struct iovec *args, int argscnt, + const struct iovec *envs, int envscnt, + const char *cwd, + const struct iovec *cgroups, int cgroupscnt, + const char *root) +{ + uint16_t commlen; + uint16_t exelen; + uint16_t exepathlen; + uint16_t cwdlen; + uint16_t rootlen; + uint16_t argslen; + uint16_t envlen; + uint16_t cgroupslen; + + commlen = (uint16_t)strnlen(comm, SCAP_MAX_PATH_SIZE); + exelen = (uint16_t)strnlen(exe, SCAP_MAX_PATH_SIZE); + exepathlen = (uint16_t)strnlen(exepath, SCAP_MAX_PATH_SIZE); + cwdlen = (uint16_t)strnlen(cwd, SCAP_MAX_PATH_SIZE); + rootlen = (uint16_t)strnlen(root, SCAP_MAX_PATH_SIZE); + + argslen = iov_size(args, argscnt); + envlen = iov_size(envs, envscnt); + cgroupslen = iov_size(cgroups, cgroupscnt); + + if(scap_dump_write(d, &len, sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(tinfo->tid), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(tinfo->pid), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(tinfo->ptid), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(tinfo->sid), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(tinfo->vpgid), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &commlen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, (char *) comm, commlen) != commlen || + scap_dump_write(d, &exelen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, (char *) exe, exelen) != exelen || + scap_dump_write(d, &exepathlen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, (char *) exepath, exepathlen) != exepathlen || + scap_dump_write(d, &argslen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_writev(d, args, argscnt) != argslen || + scap_dump_write(d, &cwdlen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, (char *) cwd, cwdlen) != cwdlen || + scap_dump_write(d, &(tinfo->fdlimit), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(tinfo->flags), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(tinfo->uid), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(tinfo->gid), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(tinfo->vmsize_kb), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(tinfo->vmrss_kb), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(tinfo->vmswap_kb), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(tinfo->pfmajor), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(tinfo->pfminor), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &envlen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_writev(d, envs, envscnt) != envlen || + scap_dump_write(d, &(tinfo->vtid), sizeof(int64_t)) != sizeof(int64_t) || + scap_dump_write(d, &(tinfo->vpid), sizeof(int64_t)) != sizeof(int64_t) || + scap_dump_write(d, &(cgroupslen), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_writev(d, cgroups, cgroupscnt) != cgroupslen || + scap_dump_write(d, &rootlen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, (char *) root, rootlen) != rootlen || + scap_dump_write(d, &(tinfo->loginuid), sizeof(uint32_t)) != sizeof(uint32_t)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (2)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Write the process list block +// +static int32_t scap_write_proclist(scap_t *handle, scap_dumper_t *d) +{ + uint32_t totlen = 0; + uint32_t idx = 0; + struct scap_threadinfo *tinfo; + struct scap_threadinfo *ttinfo; + + uint32_t* lengths = calloc(HASH_COUNT(handle->m_proclist), sizeof(uint32_t)); + if(lengths == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_write_proclist memory allocation failure"); + return SCAP_FAILURE; + } + + // + // First pass of the table to calculate the lengths + // + HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo) + { + if(!tinfo->filtered_out) + { + // + // NB: new fields must be appended + // + uint32_t il= (uint32_t) + (sizeof(uint32_t) + // len + sizeof(uint64_t) + // tid + sizeof(uint64_t) + // pid + sizeof(uint64_t) + // ptid + sizeof(uint64_t) + // sid + sizeof(uint64_t) + // vpgid + 2 + strnlen(tinfo->comm, SCAP_MAX_PATH_SIZE) + + 2 + strnlen(tinfo->exe, SCAP_MAX_PATH_SIZE) + + 2 + strnlen(tinfo->exepath, SCAP_MAX_PATH_SIZE) + + 2 + tinfo->args_len + + 2 + strnlen(tinfo->cwd, SCAP_MAX_PATH_SIZE) + + sizeof(uint64_t) + // fdlimit + sizeof(uint32_t) + // flags + sizeof(uint32_t) + // uid + sizeof(uint32_t) + // gid + sizeof(uint32_t) + // vmsize_kb + sizeof(uint32_t) + // vmrss_kb + sizeof(uint32_t) + // vmswap_kb + sizeof(uint64_t) + // pfmajor + sizeof(uint64_t) + // pfminor + 2 + tinfo->env_len + + sizeof(int64_t) + // vtid + sizeof(int64_t) + // vpid + 2 + tinfo->cgroups_len + + 2 + strnlen(tinfo->root, SCAP_MAX_PATH_SIZE) + + sizeof(int32_t)); // loginuid; + + lengths[idx++] = il; + totlen += il; + } + } + idx = 0; + + if(scap_write_proclist_header(handle, d, totlen) != SCAP_SUCCESS) + { + free(lengths); + return SCAP_FAILURE; + } + + // + // Second pass of the table to dump it + // + HASH_ITER(hh, handle->m_proclist, tinfo, ttinfo) + { + if(tinfo->filtered_out) + { + continue; + } + + if(scap_write_proclist_entry(handle, d, tinfo, lengths[idx++]) != SCAP_SUCCESS) + { + free(lengths); + return SCAP_FAILURE; + } + } + + free(lengths); + + return scap_write_proclist_trailer(handle, d, totlen); +} + +// +// Write the machine info block +// +static int32_t scap_write_machine_info(scap_t *handle, scap_dumper_t *d) +{ + block_header bh; + uint32_t bt; + + // + // Write the section header + // + bh.block_type = MI_BLOCK_TYPE; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + sizeof(scap_machine_info) + 4); + + bt = bh.block_total_length; + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh) || + scap_dump_write(d, &handle->m_machine_info, sizeof(handle->m_machine_info)) != sizeof(handle->m_machine_info) || + scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (MI1)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Write the interface list block +// +static int32_t scap_write_iflist(scap_t *handle, scap_dumper_t* d) +{ + block_header bh; + uint32_t bt; + uint32_t entrylen; + uint32_t totlen = 0; + uint32_t j; + + // + // Get the interface list + // + if(handle->m_addrlist == NULL) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to trace file: interface list missing"); + return SCAP_FAILURE; + } + + // + // Create the block + // + bh.block_type = IL_BLOCK_TYPE_V2; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + (handle->m_addrlist->n_v4_addrs + handle->m_addrlist->n_v6_addrs)*sizeof(uint32_t) + + handle->m_addrlist->totlen + 4); + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF1)"); + return SCAP_FAILURE; + } + + // + // Dump the ipv4 list + // + for(j = 0; j < handle->m_addrlist->n_v4_addrs; j++) + { + scap_ifinfo_ipv4 *entry = &(handle->m_addrlist->v4list[j]); + + entrylen = sizeof(scap_ifinfo_ipv4) + entry->ifnamelen - SCAP_MAX_PATH_SIZE; + + if(scap_dump_write(d, &entrylen, sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(entry->type), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(entry->ifnamelen), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(entry->addr), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(entry->netmask), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(entry->bcast), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(entry->linkspeed), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(entry->ifname), entry->ifnamelen) != entry->ifnamelen) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF2)"); + return SCAP_FAILURE; + } + + totlen += sizeof(uint32_t) + entrylen; + } + + // + // Dump the ipv6 list + // + for(j = 0; j < handle->m_addrlist->n_v6_addrs; j++) + { + scap_ifinfo_ipv6 *entry = &(handle->m_addrlist->v6list[j]); + + entrylen = sizeof(scap_ifinfo_ipv6) + entry->ifnamelen - SCAP_MAX_PATH_SIZE; + + if(scap_dump_write(d, &entrylen, sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(entry->type), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(entry->ifnamelen), sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, &(entry->addr), SCAP_IPV6_ADDR_LEN) != SCAP_IPV6_ADDR_LEN || + scap_dump_write(d, &(entry->netmask), SCAP_IPV6_ADDR_LEN) != SCAP_IPV6_ADDR_LEN || + scap_dump_write(d, &(entry->bcast), SCAP_IPV6_ADDR_LEN) != SCAP_IPV6_ADDR_LEN || + scap_dump_write(d, &(entry->linkspeed), sizeof(uint64_t)) != sizeof(uint64_t) || + scap_dump_write(d, &(entry->ifname), entry->ifnamelen) != entry->ifnamelen) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF2)"); + return SCAP_FAILURE; + } + + totlen += sizeof(uint32_t) + entrylen; + } + + // + // Blocks need to be 4-byte padded + // + if(scap_write_padding(d, totlen) != SCAP_SUCCESS) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF3)"); + return SCAP_FAILURE; + } + + // + // Create the trailer + // + bt = bh.block_total_length; + if(scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF4)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Write the user list block +// +static int32_t scap_write_userlist(scap_t *handle, scap_dumper_t* d) +{ + block_header bh; + uint32_t bt; + uint32_t j; + uint16_t namelen; + uint16_t homedirlen; + uint16_t shelllen; + uint8_t type; + uint32_t totlen = 0; + + // + // Make sure we have a user list interface list + // + if(handle->m_userlist == NULL) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to trace file: user list missing"); + return SCAP_FAILURE; + } + + uint32_t* lengths = calloc(handle->m_userlist->nusers + handle->m_userlist->ngroups, sizeof(uint32_t)); + if(lengths == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_write_userlist memory allocation failure (1)"); + return SCAP_FAILURE; + } + + // + // Calculate the lengths + // + for(j = 0; j < handle->m_userlist->nusers; j++) + { + scap_userinfo* info = &handle->m_userlist->users[j]; + + namelen = (uint16_t)strnlen(info->name, MAX_CREDENTIALS_STR_LEN); + homedirlen = (uint16_t)strnlen(info->homedir, SCAP_MAX_PATH_SIZE); + shelllen = (uint16_t)strnlen(info->shell, SCAP_MAX_PATH_SIZE); + + // NB: new fields must be appended + size_t ul = sizeof(uint32_t) + sizeof(type) + sizeof(info->uid) + sizeof(info->gid) + sizeof(uint16_t) + + namelen + sizeof(uint16_t) + homedirlen + sizeof(uint16_t) + shelllen; + totlen += ul; + lengths[j] = ul; + } + + for(j = 0; j < handle->m_userlist->ngroups; j++) + { + scap_groupinfo* info = &handle->m_userlist->groups[j]; + + namelen = (uint16_t)strnlen(info->name, MAX_CREDENTIALS_STR_LEN); + + // NB: new fields must be appended + uint32_t gl = sizeof(uint32_t) + sizeof(type) + sizeof(info->gid) + sizeof(uint16_t) + namelen; + totlen += gl; + lengths[handle->m_userlist->nusers + j] = gl; + } + + // + // Create the block + // + bh.block_type = UL_BLOCK_TYPE_V2; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + totlen + 4); + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh)) + { + free(lengths); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF1)"); + return SCAP_FAILURE; + } + + // + // Dump the users + // + type = USERBLOCK_TYPE_USER; + for(j = 0; j < handle->m_userlist->nusers; j++) + { + scap_userinfo* info = &handle->m_userlist->users[j]; + + namelen = (uint16_t)strnlen(info->name, MAX_CREDENTIALS_STR_LEN); + homedirlen = (uint16_t)strnlen(info->homedir, SCAP_MAX_PATH_SIZE); + shelllen = (uint16_t)strnlen(info->shell, SCAP_MAX_PATH_SIZE); + + if(scap_dump_write(d, &(lengths[j]), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(type), sizeof(type)) != sizeof(type) || + scap_dump_write(d, &(info->uid), sizeof(info->uid)) != sizeof(info->uid) || + scap_dump_write(d, &(info->gid), sizeof(info->gid)) != sizeof(info->gid) || + scap_dump_write(d, &namelen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, info->name, namelen) != namelen || + scap_dump_write(d, &homedirlen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, info->homedir, homedirlen) != homedirlen || + scap_dump_write(d, &shelllen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, info->shell, shelllen) != shelllen) + { + free(lengths); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (U1)"); + return SCAP_FAILURE; + } + } + + // + // Dump the groups + // + type = USERBLOCK_TYPE_GROUP; + for(j = 0; j < handle->m_userlist->ngroups; j++) + { + scap_groupinfo* info = &handle->m_userlist->groups[j]; + + namelen = (uint16_t)strnlen(info->name, MAX_CREDENTIALS_STR_LEN); + + if(scap_dump_write(d, &(lengths[handle->m_userlist->nusers + j]), sizeof(uint32_t)) != sizeof(uint32_t) || + scap_dump_write(d, &(type), sizeof(type)) != sizeof(type) || + scap_dump_write(d, &(info->gid), sizeof(info->gid)) != sizeof(info->gid) || + scap_dump_write(d, &namelen, sizeof(uint16_t)) != sizeof(uint16_t) || + scap_dump_write(d, info->name, namelen) != namelen) + { + free(lengths); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (U2)"); + return SCAP_FAILURE; + } + } + + free(lengths); + + // + // Blocks need to be 4-byte padded + // + if(scap_write_padding(d, totlen) != SCAP_SUCCESS) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF3)"); + return SCAP_FAILURE; + } + + // + // Create the trailer + // + bt = bh.block_total_length; + if(scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (IF4)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Create the dump file headers and add the tables +// +int32_t scap_setup_dump(scap_t *handle, scap_dumper_t* d, const char *fname) +{ + block_header bh; + section_header_block sh; + uint32_t bt; + + // + // Write the section header + // + bh.block_type = SHB_BLOCK_TYPE; + bh.block_total_length = sizeof(block_header) + sizeof(section_header_block) + 4; + + sh.byte_order_magic = SHB_MAGIC; + sh.major_version = CURRENT_MAJOR_VERSION; + sh.minor_version = CURRENT_MINOR_VERSION; + sh.section_length = 0xffffffffffffffffLL; + + bt = bh.block_total_length; + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh) || + scap_dump_write(d, &sh, sizeof(sh)) != sizeof(sh) || + scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file %s (5)", fname); + return SCAP_FAILURE; + } + + // + // If we're dumping in live mode, refresh the process tables list + // so we don't lose information about processes created in the interval + // between opening the handle and starting the dump + // +#if defined(HAS_CAPTURE) + if(handle->m_file == NULL && handle->refresh_proc_table_when_saving) + { + proc_entry_callback tcb = handle->m_proc_callback; + handle->m_proc_callback = NULL; + + scap_proc_free_table(handle); + char filename[SCAP_MAX_PATH_SIZE]; + snprintf(filename, sizeof(filename), "%s/proc", scap_get_host_root()); + if(scap_proc_scan_proc_dir(handle, filename, handle->m_lasterr) != SCAP_SUCCESS) + { + handle->m_proc_callback = tcb; + return SCAP_FAILURE; + } + + handle->m_proc_callback = tcb; + } +#endif + + // + // Write the machine info + // + if(scap_write_machine_info(handle, d) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + // + // Write the interface list + // + if(scap_write_iflist(handle, d) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + // + // Write the user list + // + if(scap_write_userlist(handle, d) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + // + // Write the process list + // + if(scap_write_proclist(handle, d) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + // + // Write the fd lists + // + if(scap_write_fdlist(handle, d) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + + // + // If the user doesn't need the thread table, free it + // + if(handle->m_proc_callback != NULL) + { + scap_proc_free_table(handle); + } + + // + // Done, return the file + // + return SCAP_SUCCESS; +} + +// fname is only used for log messages in scap_setup_dump +static scap_dumper_t *scap_dump_open_gzfile(scap_t *handle, gzFile gzfile, const char *fname, bool skip_proc_scan) +{ + scap_dumper_t* res = (scap_dumper_t*)malloc(sizeof(scap_dumper_t)); + res->m_f = gzfile; + res->m_type = DT_FILE; + res->m_targetbuf = NULL; + res->m_targetbufcurpos = NULL; + res->m_targetbufend = NULL; + + bool tmp_refresh_proc_table_when_saving = handle->refresh_proc_table_when_saving; + if(skip_proc_scan) + { + handle->refresh_proc_table_when_saving = false; + } + + if(scap_setup_dump(handle, res, fname) != SCAP_SUCCESS) + { + res = NULL; + } + + if(skip_proc_scan) + { + handle->refresh_proc_table_when_saving = tmp_refresh_proc_table_when_saving; + } + + return res; +} + +// +// Open a "savefile" for writing. +// +scap_dumper_t *scap_dump_open(scap_t *handle, const char *fname, compression_mode compress, bool skip_proc_scan) +{ + gzFile f = NULL; + int fd = -1; + const char* mode; + + switch(compress) + { + case SCAP_COMPRESSION_GZIP: + mode = "wb"; + break; + case SCAP_COMPRESSION_NONE: + mode = "wbT"; + break; + default: + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid compression mode"); + return NULL; + } + + if(fname[0] == '-' && fname[1] == '\0') + { +#ifndef _WIN32 + fd = dup(STDOUT_FILENO); +#else + fd = 1; +#endif + if(fd != -1) + { + f = gzdopen(fd, mode); + fname = "standard output"; + } + } + else + { + f = gzopen(fname, mode); + } + + if(f == NULL) + { +#ifndef _WIN32 + if(fd != -1) + { + close(fd); + } +#endif + + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't open %s", fname); + return NULL; + } + + return scap_dump_open_gzfile(handle, f, fname, skip_proc_scan); +} + +// +// Open a savefile for writing, using the provided fd +scap_dumper_t* scap_dump_open_fd(scap_t *handle, int fd, compression_mode compress, bool skip_proc_scan) +{ + gzFile f = NULL; + const char* mode; + + switch(compress) + { + case SCAP_COMPRESSION_GZIP: + mode = "wb"; + break; + case SCAP_COMPRESSION_NONE: + mode = "wbT"; + break; + default: + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid compression mode"); + return NULL; + } + + f = gzdopen(fd, mode); + + if(f == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "can't open fd %d", fd); + return NULL; + } + + return scap_dump_open_gzfile(handle, f, "", skip_proc_scan); +} + +// +// Open a memory "savefile" +// +scap_dumper_t *scap_memory_dump_open(scap_t *handle, uint8_t* targetbuf, uint64_t targetbufsize) +{ + scap_dumper_t* res = (scap_dumper_t*)malloc(sizeof(scap_dumper_t)); + if(res == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_dump_memory_open memory allocation failure (1)"); + return NULL; + } + + res->m_f = NULL; + res->m_type = DT_MEM; + res->m_targetbuf = targetbuf; + res->m_targetbufcurpos = targetbuf; + res->m_targetbufend = targetbuf + targetbufsize; + + // + // Disable proc parsing since it would be too heavy when saving to memory. + // Before doing that, backup handle->refresh_proc_table_when_saving so we can + // restore whatever the current setting is as soon as we're done. + // + bool tmp_refresh_proc_table_when_saving = handle->refresh_proc_table_when_saving; + handle->refresh_proc_table_when_saving = false; + + if(scap_setup_dump(handle, res, "") != SCAP_SUCCESS) + { + free(res); + res = NULL; + } + + handle->refresh_proc_table_when_saving = tmp_refresh_proc_table_when_saving; + + return res; +} + +// +// Close a "savefile" opened with scap_dump_open +// +void scap_dump_close(scap_dumper_t *d) +{ + if(d->m_type == DT_FILE) + { + gzclose(d->m_f); + } + + free(d); +} + +// +// Return the current size of a tracefile +// +int64_t scap_dump_get_offset(scap_dumper_t *d) +{ + if(d->m_type == DT_FILE) + { + return gzoffset(d->m_f); + } + else + { + return (int64_t)d->m_targetbufcurpos - (int64_t)d->m_targetbuf; + } +} + +int64_t scap_dump_ftell(scap_dumper_t *d) +{ + if(d->m_type == DT_FILE) + { + return gztell(d->m_f); + } + else + { + return (int64_t)d->m_targetbufcurpos - (int64_t)d->m_targetbuf; + } +} + +void scap_dump_flush(scap_dumper_t *d) +{ + if(d->m_type == DT_FILE) + { + gzflush(d->m_f, Z_FULL_FLUSH); + } +} + +// +// Tell me how many bytes we will have written if we did. +// +int32_t scap_number_of_bytes_to_write(scap_evt *e, uint16_t cpuid, int32_t *bytes) +{ + *bytes = scap_normalize_block_len(sizeof(block_header) + sizeof(cpuid) + e->len + 4); + + return SCAP_SUCCESS; +} + +// +// Write an event to a dump file +// +int32_t scap_dump(scap_t *handle, scap_dumper_t *d, scap_evt *e, uint16_t cpuid, uint32_t flags) +{ + block_header bh; + uint32_t bt; + + if(flags == 0) + { + // + // Write the section header + // + bh.block_type = EV_BLOCK_TYPE_V2; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + sizeof(cpuid) + e->len + 4); + bt = bh.block_total_length; + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh) || + scap_dump_write(d, &cpuid, sizeof(cpuid)) != sizeof(cpuid) || + scap_dump_write(d, e, e->len) != e->len || + scap_write_padding(d, sizeof(cpuid) + e->len) != SCAP_SUCCESS || + scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (6)"); + return SCAP_FAILURE; + } + } + else + { + // + // Write the section header + // + bh.block_type = EVF_BLOCK_TYPE_V2; + bh.block_total_length = scap_normalize_block_len(sizeof(block_header) + sizeof(cpuid) + sizeof(flags) + e->len + 4); + bt = bh.block_total_length; + + if(scap_dump_write(d, &bh, sizeof(bh)) != sizeof(bh) || + scap_dump_write(d, &cpuid, sizeof(cpuid)) != sizeof(cpuid) || + scap_dump_write(d, &flags, sizeof(flags)) != sizeof(flags) || + scap_dump_write(d, e, e->len) != e->len || + scap_write_padding(d, sizeof(cpuid) + e->len) != SCAP_SUCCESS || + scap_dump_write(d, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (7)"); + return SCAP_FAILURE; + } + } + + // + // Enable this to make sure that everything is saved to disk during the tests + // +#if 0 + fflush(f); +#endif + + return SCAP_SUCCESS; +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +// READ FUNCTIONS +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +// +// Load the machine info block +// +static int32_t scap_read_machine_info(scap_t *handle, gzFile f, uint32_t block_length) +{ + // + // Read the section header block + // + if(gzread(f, &handle->m_machine_info, sizeof(handle->m_machine_info)) != + sizeof(handle->m_machine_info)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading from file (1)"); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Parse a process list block +// +static int32_t scap_read_proclist(scap_t *handle, gzFile f, uint32_t block_length, uint32_t block_type) +{ + size_t readsize; + size_t subreadsize = 0; + size_t totreadsize = 0; + size_t padding_len; + uint16_t stlen; + uint32_t padding; + int32_t uth_status = SCAP_SUCCESS; + uint32_t toread; + int fseekres; + + while(((int32_t)block_length - (int32_t)totreadsize) >= 4) + { + struct scap_threadinfo tinfo; + + tinfo.fdlist = NULL; + tinfo.flags = 0; + tinfo.vmsize_kb = 0; + tinfo.vmrss_kb = 0; + tinfo.vmswap_kb = 0; + tinfo.pfmajor = 0; + tinfo.pfminor = 0; + tinfo.env_len = 0; + tinfo.vtid = -1; + tinfo.vpid = -1; + tinfo.cgroups_len = 0; + tinfo.filtered_out = 0; + tinfo.root[0] = 0; + tinfo.sid = -1; + tinfo.vpgid = -1; + tinfo.clone_ts = 0; + tinfo.tty = 0; + tinfo.exepath[0] = 0; + tinfo.loginuid = -1; + + // + // len + // + uint32_t sub_len = 0; + switch(block_type) + { + case PL_BLOCK_TYPE_V1: + case PL_BLOCK_TYPE_V1_INT: + case PL_BLOCK_TYPE_V2: + case PL_BLOCK_TYPE_V2_INT: + case PL_BLOCK_TYPE_V3: + case PL_BLOCK_TYPE_V3_INT: + case PL_BLOCK_TYPE_V4: + case PL_BLOCK_TYPE_V5: + case PL_BLOCK_TYPE_V6: + case PL_BLOCK_TYPE_V7: + case PL_BLOCK_TYPE_V8: + break; + case PL_BLOCK_TYPE_V9: + readsize = gzread(f, &(sub_len), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + break; + default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted process block type (fd1)"); + ASSERT(false); + return SCAP_FAILURE; + } + + // + // tid + // + readsize = gzread(f, &(tinfo.tid), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + // + // pid + // + readsize = gzread(f, &(tinfo.pid), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + // + // ptid + // + readsize = gzread(f, &(tinfo.ptid), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + switch(block_type) + { + case PL_BLOCK_TYPE_V1: + case PL_BLOCK_TYPE_V1_INT: + case PL_BLOCK_TYPE_V2: + case PL_BLOCK_TYPE_V2_INT: + case PL_BLOCK_TYPE_V3: + case PL_BLOCK_TYPE_V3_INT: + case PL_BLOCK_TYPE_V4: + case PL_BLOCK_TYPE_V5: + break; + case PL_BLOCK_TYPE_V6: + case PL_BLOCK_TYPE_V7: + case PL_BLOCK_TYPE_V8: + case PL_BLOCK_TYPE_V9: + readsize = gzread(f, &(tinfo.sid), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + break; + default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted process block type (fd1)"); + ASSERT(false); + return SCAP_FAILURE; + } + + // + // vpgid + // + switch(block_type) + { + case PL_BLOCK_TYPE_V1: + case PL_BLOCK_TYPE_V1_INT: + case PL_BLOCK_TYPE_V2: + case PL_BLOCK_TYPE_V2_INT: + case PL_BLOCK_TYPE_V3: + case PL_BLOCK_TYPE_V3_INT: + case PL_BLOCK_TYPE_V4: + case PL_BLOCK_TYPE_V5: + case PL_BLOCK_TYPE_V6: + case PL_BLOCK_TYPE_V7: + break; + case PL_BLOCK_TYPE_V8: + case PL_BLOCK_TYPE_V9: + readsize = gzread(f, &(tinfo.vpgid), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + break; + default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted process block type (fd1)"); + ASSERT(false); + return SCAP_FAILURE; + } + + // + // comm + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_PATH_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid commlen %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, tinfo.comm, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + tinfo.comm[stlen] = 0; + + subreadsize += readsize; + + // + // exe + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_PATH_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid exelen %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, tinfo.exe, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + tinfo.exe[stlen] = 0; + + subreadsize += readsize; + + switch(block_type) + { + case PL_BLOCK_TYPE_V1: + case PL_BLOCK_TYPE_V1_INT: + case PL_BLOCK_TYPE_V2: + case PL_BLOCK_TYPE_V2_INT: + case PL_BLOCK_TYPE_V3: + case PL_BLOCK_TYPE_V3_INT: + case PL_BLOCK_TYPE_V4: + case PL_BLOCK_TYPE_V5: + case PL_BLOCK_TYPE_V6: + break; + case PL_BLOCK_TYPE_V7: + case PL_BLOCK_TYPE_V8: + case PL_BLOCK_TYPE_V9: + // + // exepath + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_PATH_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid exepathlen %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, tinfo.exepath, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + tinfo.exepath[stlen] = 0; + + subreadsize += readsize; + + break; + default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted process block type (fd1)"); + ASSERT(false); + return SCAP_FAILURE; + } + + // + // args + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_ARGS_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid argslen %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, tinfo.args, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + tinfo.args[stlen] = 0; + tinfo.args_len = stlen; + + subreadsize += readsize; + + // + // cwd + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_PATH_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid cwdlen %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, tinfo.cwd, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + tinfo.cwd[stlen] = 0; + + subreadsize += readsize; + + // + // fdlimit + // + readsize = gzread(f, &(tinfo.fdlimit), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + // + // flags + // + readsize = gzread(f, &(tinfo.flags), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // uid + // + readsize = gzread(f, &(tinfo.uid), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // gid + // + readsize = gzread(f, &(tinfo.gid), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + switch(block_type) + { + case PL_BLOCK_TYPE_V1: + case PL_BLOCK_TYPE_V1_INT: + break; + case PL_BLOCK_TYPE_V2: + case PL_BLOCK_TYPE_V2_INT: + case PL_BLOCK_TYPE_V3: + case PL_BLOCK_TYPE_V3_INT: + case PL_BLOCK_TYPE_V4: + case PL_BLOCK_TYPE_V5: + case PL_BLOCK_TYPE_V6: + case PL_BLOCK_TYPE_V7: + case PL_BLOCK_TYPE_V8: + case PL_BLOCK_TYPE_V9: + // + // vmsize_kb + // + readsize = gzread(f, &(tinfo.vmsize_kb), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // vmrss_kb + // + readsize = gzread(f, &(tinfo.vmrss_kb), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // vmswap_kb + // + readsize = gzread(f, &(tinfo.vmswap_kb), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // pfmajor + // + readsize = gzread(f, &(tinfo.pfmajor), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + // + // pfminor + // + readsize = gzread(f, &(tinfo.pfminor), sizeof(uint64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + if(block_type == PL_BLOCK_TYPE_V3 || + block_type == PL_BLOCK_TYPE_V3_INT || + block_type == PL_BLOCK_TYPE_V4 || + block_type == PL_BLOCK_TYPE_V5 || + block_type == PL_BLOCK_TYPE_V6 || + block_type == PL_BLOCK_TYPE_V7 || + block_type == PL_BLOCK_TYPE_V8 || + block_type == PL_BLOCK_TYPE_V9) + { + // + // env + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_ENV_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid envlen %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, tinfo.env, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + tinfo.env[stlen] = 0; + tinfo.env_len = stlen; + + subreadsize += readsize; + } + + if(block_type == PL_BLOCK_TYPE_V4 || + block_type == PL_BLOCK_TYPE_V5 || + block_type == PL_BLOCK_TYPE_V6 || + block_type == PL_BLOCK_TYPE_V7 || + block_type == PL_BLOCK_TYPE_V8 || + block_type == PL_BLOCK_TYPE_V9) + { + // + // vtid + // + readsize = gzread(f, &(tinfo.vtid), sizeof(int64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + // + // vpid + // + readsize = gzread(f, &(tinfo.vpid), sizeof(int64_t)); + CHECK_READ_SIZE(readsize, sizeof(uint64_t)); + + subreadsize += readsize; + + // + // cgroups + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_CGROUPS_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid cgroupslen %d", stlen); + return SCAP_FAILURE; + } + tinfo.cgroups_len = stlen; + + subreadsize += readsize; + + readsize = gzread(f, tinfo.cgroups, stlen); + CHECK_READ_SIZE(readsize, stlen); + + subreadsize += readsize; + + if(block_type == PL_BLOCK_TYPE_V5 || + block_type == PL_BLOCK_TYPE_V6 || + block_type == PL_BLOCK_TYPE_V7 || + block_type == PL_BLOCK_TYPE_V8 || + block_type == PL_BLOCK_TYPE_V9) + { + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen > SCAP_MAX_PATH_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid rootlen %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, tinfo.root, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + tinfo.root[stlen] = 0; + + subreadsize += readsize; + } + } + break; + default: + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted process block type (fd1)"); + ASSERT(false); + return SCAP_FAILURE; + } + + // If new parameters are added, sub_len can be used to + // see if they are available in the current capture. + // For example, for a 32bit parameter: + // + // if(sub_len && (subreadsize + sizeof(uint32_t)) <= sub_len) + // { + // ... + // } + + // + // loginuid + // + if(sub_len && (subreadsize + sizeof(int32_t)) <= sub_len) + { + readsize = gzread(f, &(tinfo.loginuid), sizeof(int32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + subreadsize += readsize; + } + + + // + // All parsed. Add the entry to the table, or fire the notification callback + // + if(handle->m_proc_callback == NULL) + { + // + // All parsed. Allocate the new entry and copy the temp one into into it. + // + struct scap_threadinfo *ntinfo = (scap_threadinfo *)malloc(sizeof(scap_threadinfo)); + if(ntinfo == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd1)"); + return SCAP_FAILURE; + } + + // Structure copy + *ntinfo = tinfo; + + HASH_ADD_INT64(handle->m_proclist, tid, ntinfo); + if(uth_status != SCAP_SUCCESS) + { + free(ntinfo); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd2)"); + return SCAP_FAILURE; + } + } + else + { + handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo.tid, &tinfo, NULL); + } + + if(sub_len && subreadsize != sub_len) + { + if(subreadsize > sub_len) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Had read %lu bytes, but proclist entry have length %u.", + subreadsize, sub_len); + return SCAP_FAILURE; + } + toread = sub_len - subreadsize; + fseekres = (int)gzseek(f, (long)toread, SEEK_CUR); + if(fseekres == -1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't skip %u bytes.", + (unsigned int)toread); + return SCAP_FAILURE; + } + subreadsize = sub_len; + } + + totreadsize += subreadsize; + subreadsize = 0; + } + + // + // Read the padding bytes so we properly align to the end of the data + // + if(totreadsize > block_length) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_read_proclist read more %lu than a block %u", totreadsize, block_length); + ASSERT(false); + return SCAP_FAILURE; + } + padding_len = block_length - totreadsize; + + readsize = (size_t)gzread(f, &padding, (unsigned int)padding_len); + CHECK_READ_SIZE(readsize, padding_len); + + return SCAP_SUCCESS; +} + +// +// Parse an interface list block +// +static int32_t scap_read_iflist(scap_t *handle, gzFile f, uint32_t block_length, uint32_t block_type) +{ + int32_t res = SCAP_SUCCESS; + size_t readsize; + size_t totreadsize; + char *readbuf = NULL; + char *pif; + uint16_t iftype; + uint16_t ifnamlen; + uint32_t toread; + uint32_t entrysize; + uint32_t ifcnt4 = 0; + uint32_t ifcnt6 = 0; + + // + // If the list of interfaces was already allocated for this handle (for example because this is + // not the first interface list block), free it + // + if(handle->m_addrlist != NULL) + { + scap_free_iflist(handle->m_addrlist); + handle->m_addrlist = NULL; + } + + // + // Bring the block to memory + // We assume that this block is always small enough that we can read it in a single shot + // + readbuf = (char *)malloc(block_length); + if(!readbuf) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_iflist"); + return SCAP_FAILURE; + } + + readsize = gzread(f, readbuf, block_length); + CHECK_READ_SIZE_WITH_FREE(readbuf, readsize, block_length); + + // + // First pass, count the number of addresses + // + pif = readbuf; + totreadsize = 0; + + while(true) + { + toread = (int32_t)block_length - (int32_t)totreadsize; + + if(toread < 4) + { + break; + } + + if(block_type != IL_BLOCK_TYPE_V2) + { + iftype = *(uint16_t *)pif; + ifnamlen = *(uint16_t *)(pif + 2); + + if(iftype == SCAP_II_IPV4) + { + entrysize = sizeof(scap_ifinfo_ipv4) + ifnamlen - SCAP_MAX_PATH_SIZE; + } + else if(iftype == SCAP_II_IPV6) + { + entrysize = sizeof(scap_ifinfo_ipv6) + ifnamlen - SCAP_MAX_PATH_SIZE; + } + else if(iftype == SCAP_II_IPV4_NOLINKSPEED) + { + entrysize = sizeof(scap_ifinfo_ipv4_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; + } + else if(iftype == SCAP_II_IPV6_NOLINKSPEED) + { + entrysize = sizeof(scap_ifinfo_ipv6_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(1)"); + ASSERT(false); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + } + else + { + entrysize = *(uint32_t *)pif + sizeof(uint32_t); + iftype = *(uint16_t *)(pif + 4); + ifnamlen = *(uint16_t *)(pif + 4 + 2); + } + + if(toread < entrysize) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(2) toread=%u, entrysize=%u", toread, entrysize); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + pif += entrysize; + totreadsize += entrysize; + + if(iftype == SCAP_II_IPV4 || iftype == SCAP_II_IPV4_NOLINKSPEED) + { + ifcnt4++; + } + else if(iftype == SCAP_II_IPV6 || iftype == SCAP_II_IPV6_NOLINKSPEED) + { + ifcnt6++; + } + else + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "unknown interface type %d", (int)iftype); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + } + + // + // Allocate the handle and the arrays + // + handle->m_addrlist = (scap_addrlist *)malloc(sizeof(scap_addrlist)); + if(!handle->m_addrlist) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_read_iflist allocation failed(1)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + handle->m_addrlist->n_v4_addrs = 0; + handle->m_addrlist->n_v6_addrs = 0; + handle->m_addrlist->v4list = NULL; + handle->m_addrlist->v6list = NULL; + handle->m_addrlist->totlen = block_length - (ifcnt4 + ifcnt6) * sizeof(uint32_t); + + if(ifcnt4 != 0) + { + handle->m_addrlist->v4list = (scap_ifinfo_ipv4 *)malloc(ifcnt4 * sizeof(scap_ifinfo_ipv4)); + if(!handle->m_addrlist->v4list) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_read_iflist allocation failed(2)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + } + else + { + handle->m_addrlist->v4list = NULL; + } + + if(ifcnt6 != 0) + { + handle->m_addrlist->v6list = (scap_ifinfo_ipv6 *)malloc(ifcnt6 * sizeof(scap_ifinfo_ipv6)); + if(!handle->m_addrlist->v6list) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "getifaddrs allocation failed(3)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + } + else + { + handle->m_addrlist->v6list = NULL; + } + + handle->m_addrlist->n_v4_addrs = ifcnt4; + handle->m_addrlist->n_v6_addrs = ifcnt6; + + // + // Second pass: populate the arrays + // + ifcnt4 = 0; + ifcnt6 = 0; + pif = readbuf; + totreadsize = 0; + + while(true) + { + toread = (int32_t)block_length - (int32_t)totreadsize; + entrysize = 0; + + if(toread < 4) + { + break; + } + + if(block_type == IL_BLOCK_TYPE_V2) + { + entrysize = *(uint32_t *)pif; + totreadsize += sizeof(uint32_t); + pif += sizeof(uint32_t); + } + + iftype = *(uint16_t *)pif; + ifnamlen = *(uint16_t *)(pif + 2); + + if(ifnamlen >= SCAP_MAX_PATH_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(0)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + // If new parameters are added, entrysize can be used to + // see if they are available in the current capture. + // For example, for a 32bit parameter: + // + // if(entrysize && (ifsize + sizeof(uint32_t)) <= entrysize) + // { + // ifsize += sizeof(uint32_t); + // ... + // } + + uint32_t ifsize; + if(iftype == SCAP_II_IPV4) + { + ifsize = sizeof(uint16_t) + // type + sizeof(uint16_t) + // ifnamelen + sizeof(uint32_t) + // addr + sizeof(uint32_t) + // netmask + sizeof(uint32_t) + // bcast + sizeof(uint64_t) + // linkspeed + ifnamlen; + + if(toread < ifsize) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(3)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + // Copy the entry + memcpy(handle->m_addrlist->v4list + ifcnt4, pif, ifsize - ifnamlen); + + memcpy(handle->m_addrlist->v4list[ifcnt4].ifname, pif + ifsize - ifnamlen, ifnamlen); + + // Make sure the name string is NULL-terminated + *((char *)(handle->m_addrlist->v4list + ifcnt4) + ifsize) = 0; + + ifcnt4++; + } + else if(iftype == SCAP_II_IPV4_NOLINKSPEED) + { + scap_ifinfo_ipv4_nolinkspeed* src; + scap_ifinfo_ipv4* dst; + + ifsize = sizeof(scap_ifinfo_ipv4_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; + + if(toread < ifsize) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(4)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + // Copy the entry + src = (scap_ifinfo_ipv4_nolinkspeed*)pif; + dst = handle->m_addrlist->v4list + ifcnt4; + + dst->type = src->type; + dst->ifnamelen = src->ifnamelen; + dst->addr = src->addr; + dst->netmask = src->netmask; + dst->bcast = src->bcast; + dst->linkspeed = 0; + memcpy(dst->ifname, src->ifname, MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1)); + + // Make sure the name string is NULL-terminated + *((char *)(dst->ifname + MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1))) = 0; + + ifcnt4++; + } + else if(iftype == SCAP_II_IPV6) + { + ifsize = sizeof(uint16_t) + // type + sizeof(uint16_t) + // ifnamelen + SCAP_IPV6_ADDR_LEN + // addr + SCAP_IPV6_ADDR_LEN + // netmask + SCAP_IPV6_ADDR_LEN + // bcast + sizeof(uint64_t) + // linkspeed + ifnamlen; + + if(toread < ifsize) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(5)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + // Copy the entry + memcpy(handle->m_addrlist->v6list + ifcnt6, pif, ifsize - ifnamlen); + + memcpy(handle->m_addrlist->v6list[ifcnt6].ifname, pif + ifsize - ifnamlen, ifnamlen); + + // Make sure the name string is NULL-terminated + *((char *)(handle->m_addrlist->v6list + ifcnt6) + ifsize) = 0; + + ifcnt6++; + } + else if(iftype == SCAP_II_IPV6_NOLINKSPEED) + { + scap_ifinfo_ipv6_nolinkspeed* src; + scap_ifinfo_ipv6* dst; + ifsize = sizeof(scap_ifinfo_ipv6_nolinkspeed) + ifnamlen - SCAP_MAX_PATH_SIZE; + + if(toread < ifsize) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "trace file has corrupted interface list(6)"); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + // Copy the entry + src = (scap_ifinfo_ipv6_nolinkspeed*)pif; + dst = handle->m_addrlist->v6list + ifcnt6; + + dst->type = src->type; + dst->ifnamelen = src->ifnamelen; + memcpy(dst->addr, src->addr, SCAP_IPV6_ADDR_LEN); + memcpy(dst->netmask, src->netmask, SCAP_IPV6_ADDR_LEN); + memcpy(dst->bcast, src->bcast, SCAP_IPV6_ADDR_LEN); + dst->linkspeed = 0; + memcpy(dst->ifname, src->ifname, MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1)); + + // Make sure the name string is NULL-terminated + *((char *)(dst->ifname + MIN(dst->ifnamelen, SCAP_MAX_PATH_SIZE - 1))) = 0; + + ifcnt6++; + } + else + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "unknown interface type %d", (int)iftype); + res = SCAP_FAILURE; + goto scap_read_iflist_error; + } + + entrysize = entrysize ? entrysize : ifsize; + + pif += entrysize; + totreadsize += entrysize; + } + + // + // Release the read storage + // + free(readbuf); + + return res; + +scap_read_iflist_error: + scap_free_iflist(handle->m_addrlist); + handle->m_addrlist = NULL; + + if(readbuf) + { + free(readbuf); + } + + return res; +} + +// +// Parse a user list block +// +static int32_t scap_read_userlist(scap_t *handle, gzFile f, uint32_t block_length, uint32_t block_type) +{ + size_t readsize; + size_t totreadsize = 0; + size_t subreadsize = 0; + size_t padding_len; + uint32_t padding; + uint8_t type; + uint16_t stlen; + uint32_t toread; + int fseekres; + + // + // If the list of users was already allocated for this handle (for example because this is + // not the first interface list block), free it + // + if(handle->m_userlist != NULL) + { + scap_free_userlist(handle->m_userlist); + handle->m_userlist = NULL; + } + + // + // Allocate and initialize the handle info + // + handle->m_userlist = (scap_userlist*)malloc(sizeof(scap_userlist)); + if(handle->m_userlist == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "userlist allocation failed(2)"); + return SCAP_FAILURE; + } + + handle->m_userlist->nusers = 0; + handle->m_userlist->ngroups = 0; + handle->m_userlist->totsavelen = 0; + handle->m_userlist->users = NULL; + handle->m_userlist->groups = NULL; + + // + // Import the blocks + // + while(((int32_t)block_length - (int32_t)totreadsize) >= 4) + { + uint32_t sub_len = 0; + if(block_type == UL_BLOCK_TYPE_V2) + { + // + // len + // + readsize = gzread(f, &(sub_len), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + } + + // + // type + // + readsize = gzread(f, &(type), sizeof(type)); + CHECK_READ_SIZE(readsize, sizeof(type)); + + subreadsize += readsize; + + if(type == USERBLOCK_TYPE_USER) + { + scap_userinfo* puser; + + handle->m_userlist->nusers++; + handle->m_userlist->users = (scap_userinfo*)realloc(handle->m_userlist->users, handle->m_userlist->nusers * sizeof(scap_userinfo)); + if(handle->m_userlist->users == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_userlist(1)"); + return SCAP_FAILURE; + } + + puser = &handle->m_userlist->users[handle->m_userlist->nusers -1]; + + // + // uid + // + readsize = gzread(f, &(puser->uid), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // gid + // + readsize = gzread(f, &(puser->gid), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // name + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen >= MAX_CREDENTIALS_STR_LEN) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid user name len %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, puser->name, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + puser->name[stlen] = 0; + + subreadsize += readsize; + + // + // homedir + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen >= MAX_CREDENTIALS_STR_LEN) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid user homedir len %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, puser->homedir, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + puser->homedir[stlen] = 0; + + subreadsize += readsize; + + // + // shell + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen >= MAX_CREDENTIALS_STR_LEN) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid user shell len %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, puser->shell, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + puser->shell[stlen] = 0; + + subreadsize += readsize; + + // If new parameters are added, sub_len can be used to + // see if they are available in the current capture. + // For example, for a 32bit parameter: + // + // if(sub_len && (subreadsize + sizeof(uint32_t)) <= sub_len) + // { + // ... + // } + } + else + { + scap_groupinfo* pgroup; + + handle->m_userlist->ngroups++; + handle->m_userlist->groups = (scap_groupinfo*)realloc(handle->m_userlist->groups, handle->m_userlist->ngroups * sizeof(scap_groupinfo)); + if(handle->m_userlist->groups == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "memory allocation error in scap_read_userlist(2)"); + return SCAP_FAILURE; + } + + pgroup = &handle->m_userlist->groups[handle->m_userlist->ngroups -1]; + + // + // gid + // + readsize = gzread(f, &(pgroup->gid), sizeof(uint32_t)); + CHECK_READ_SIZE(readsize, sizeof(uint32_t)); + + subreadsize += readsize; + + // + // name + // + readsize = gzread(f, &(stlen), sizeof(uint16_t)); + CHECK_READ_SIZE(readsize, sizeof(uint16_t)); + + if(stlen >= MAX_CREDENTIALS_STR_LEN) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid group name len %d", stlen); + return SCAP_FAILURE; + } + + subreadsize += readsize; + + readsize = gzread(f, pgroup->name, stlen); + CHECK_READ_SIZE(readsize, stlen); + + // the string is not null-terminated on file + pgroup->name[stlen] = 0; + + subreadsize += readsize; + + // If new parameters are added, sub_len can be used to + // see if they are available in the current capture. + // For example, for a 32bit parameter: + // + // if(sub_len && (subreadsize + sizeof(uint32_t)) <= sub_len) + // { + // ... + // } + } + + if(sub_len && subreadsize != sub_len) + { + if(subreadsize > sub_len) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Had read %lu bytes, but userlist entry have length %u.", + subreadsize, sub_len); + return SCAP_FAILURE; + } + toread = sub_len - subreadsize; + fseekres = (int)gzseek(f, (long)toread, SEEK_CUR); + if(fseekres == -1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't skip %u bytes.", + (unsigned int)toread); + return SCAP_FAILURE; + } + subreadsize = sub_len; + } + + totreadsize += subreadsize; + subreadsize = 0; + } + + // + // Read the padding bytes so we properly align to the end of the data + // + if(totreadsize > block_length) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_read_userlist read more %lu than a block %u", totreadsize, block_length); + return SCAP_FAILURE; + } + padding_len = block_length - totreadsize; + + readsize = gzread(f, &padding, (unsigned int)padding_len); + CHECK_READ_SIZE(readsize, padding_len); + + return SCAP_SUCCESS; +} + +// +// Parse a process list block +// +static int32_t scap_read_fdlist(scap_t *handle, gzFile f, uint32_t block_length, uint32_t block_type) +{ + size_t readsize; + size_t totreadsize = 0; + size_t padding_len; + struct scap_threadinfo *tinfo; + scap_fdinfo fdi; + scap_fdinfo *nfdi; + // uint16_t stlen; + uint64_t tid; + int32_t uth_status = SCAP_SUCCESS; + uint32_t padding; + + // + // Read the tid + // + readsize = gzread(f, &tid, sizeof(tid)); + CHECK_READ_SIZE(readsize, sizeof(tid)); + totreadsize += readsize; + + if(handle->m_proc_callback == NULL) + { + // + // Identify the process descriptor + // + HASH_FIND_INT64(handle->m_proclist, &tid, tinfo); + if(tinfo == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted trace file. FD block references TID %"PRIu64", which doesn't exist.", + tid); + return SCAP_FAILURE; + } + } + else + { + tinfo = NULL; + } + + while(((int32_t)block_length - (int32_t)totreadsize) >= 4) + { + if(scap_fd_read_from_disk(handle, &fdi, &readsize, block_type, f) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + totreadsize += readsize; + + // + // Add the entry to the table, or fire the notification callback + // + if(handle->m_proc_callback == NULL) + { + // + // Parsed successfully. Allocate the new entry and copy the temp one into into it. + // + nfdi = (scap_fdinfo *)malloc(sizeof(scap_fdinfo)); + if(nfdi == NULL) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd1)"); + return SCAP_FAILURE; + } + + // Structure copy + *nfdi = fdi; + + ASSERT(tinfo != NULL); + + HASH_ADD_INT64(tinfo->fdlist, fd, nfdi); + if(uth_status != SCAP_SUCCESS) + { + free(nfdi); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "process table allocation error (fd2)"); + return SCAP_FAILURE; + } + } + else + { + ASSERT(tinfo == NULL); + + handle->m_proc_callback(handle->m_proc_callback_context, handle, tid, NULL, &fdi); + } + } + + // + // Read the padding bytes so we properly align to the end of the data + // + if(totreadsize > block_length) + { + ASSERT(false); + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "scap_read_fdlist read more %lu than a block %u", totreadsize, block_length); + return SCAP_FAILURE; + } + padding_len = block_length - totreadsize; + + readsize = gzread(f, &padding, (unsigned int)padding_len); + CHECK_READ_SIZE(readsize, padding_len); + + return SCAP_SUCCESS; +} + +// +// Parse the headers of a trace file and load the tables +// +int32_t scap_read_init(scap_t *handle, gzFile f) +{ + block_header bh; + section_header_block sh; + uint32_t bt; + size_t readsize; + size_t toread; + int fseekres; + int8_t found_mi = 0; + int8_t found_pl = 0; + int8_t found_fdl = 0; + int8_t found_il = 0; + int8_t found_ul = 0; + int8_t found_ev = 0; + + // + // Read the section header block + // + if(gzread(f, &bh, sizeof(bh)) != sizeof(bh) || + gzread(f, &sh, sizeof(sh)) != sizeof(sh) || + gzread(f, &bt, sizeof(bt)) != sizeof(bt)) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading from file (1)"); + return SCAP_FAILURE; + } + + if(bh.block_type != SHB_BLOCK_TYPE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid block type"); + return SCAP_FAILURE; + } + + if(sh.byte_order_magic != 0x1a2b3c4d) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid magic number"); + return SCAP_FAILURE; + } + + if(sh.major_version > CURRENT_MAJOR_VERSION) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, + "cannot correctly parse the capture. Upgrade your version of sysdig."); + return SCAP_VERSION_MISMATCH; + } + + // + // Read the metadata blocks (processes, FDs, etc.) + // + while(true) + { + readsize = gzread(f, &bh, sizeof(bh)); + + // + // If we don't find the event block header, + // it means there is no event in the file. + // + if (readsize == 0 && !found_ev && found_mi && found_pl && + found_il && found_fdl && found_ul) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "no events in file"); + return SCAP_FAILURE; + } + + CHECK_READ_SIZE(readsize, sizeof(bh)); + + switch(bh.block_type) + { + case MI_BLOCK_TYPE: + case MI_BLOCK_TYPE_INT: + found_mi = 1; + + if(scap_read_machine_info(handle, f, bh.block_total_length - sizeof(block_header) - 4) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + break; + case PL_BLOCK_TYPE_V1: + case PL_BLOCK_TYPE_V2: + case PL_BLOCK_TYPE_V3: + case PL_BLOCK_TYPE_V4: + case PL_BLOCK_TYPE_V5: + case PL_BLOCK_TYPE_V6: + case PL_BLOCK_TYPE_V7: + case PL_BLOCK_TYPE_V8: + case PL_BLOCK_TYPE_V9: + case PL_BLOCK_TYPE_V1_INT: + case PL_BLOCK_TYPE_V2_INT: + case PL_BLOCK_TYPE_V3_INT: + found_pl = 1; + + if(scap_read_proclist(handle, f, bh.block_total_length - sizeof(block_header) - 4, bh.block_type) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + break; + case FDL_BLOCK_TYPE: + case FDL_BLOCK_TYPE_INT: + case FDL_BLOCK_TYPE_V2: + found_fdl = 1; + + if(scap_read_fdlist(handle, f, bh.block_total_length - sizeof(block_header) - 4, bh.block_type) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + break; + case EV_BLOCK_TYPE: + case EV_BLOCK_TYPE_INT: + case EV_BLOCK_TYPE_V2: + case EVF_BLOCK_TYPE: + case EVF_BLOCK_TYPE_V2: + found_ev = 1; + + // + // We're done with the metadata headers. Rewind the file position so we are aligned to start reading the events. + // + fseekres = gzseek(f, (long)0 - sizeof(bh), SEEK_CUR); + if(fseekres != -1) + { + break; + } + else + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error seeking in file"); + return SCAP_FAILURE; + } + case IL_BLOCK_TYPE: + case IL_BLOCK_TYPE_INT: + case IL_BLOCK_TYPE_V2: + found_il = 1; + + if(scap_read_iflist(handle, f, bh.block_total_length - sizeof(block_header) - 4, bh.block_type) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + break; + case UL_BLOCK_TYPE: + case UL_BLOCK_TYPE_INT: + case UL_BLOCK_TYPE_V2: + found_ul = 1; + + if(scap_read_userlist(handle, f, bh.block_total_length - sizeof(block_header) - 4, bh.block_type) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + break; + default: + // + // Unknown block type. Skip the block. + // + toread = bh.block_total_length - sizeof(block_header) - 4; + fseekres = (int)gzseek(f, (long)toread, SEEK_CUR); + if(fseekres == -1) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't skip block of type %x and size %u.", + (int)bh.block_type, + (unsigned int)toread); + return SCAP_FAILURE; + } + break; + } + + if(found_ev) + { + break; + } + + // + // Read and validate the trailer + // + readsize = gzread(f, &bt, sizeof(bt)); + CHECK_READ_SIZE(readsize, sizeof(bt)); + + if(bt != bh.block_total_length) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "wrong block total length, header=%u, trailer=%u", + bh.block_total_length, + bt); + return SCAP_FAILURE; + } + } + + if(!found_mi) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't find machine info block."); + return SCAP_FAILURE; + } + + if(!found_ul) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't find user list block."); + return SCAP_FAILURE; + } + + if(!found_il) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't find interface list block."); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +// +// Read an event from disk +// +int32_t scap_next_offline(scap_t *handle, OUT scap_evt **pevent, OUT uint16_t *pcpuid) +{ + block_header bh; + size_t readsize; + uint32_t readlen; + size_t hdr_len; + gzFile f = handle->m_file; + + ASSERT(f != NULL); + + // + // We may have to repeat the whole process + // if the capture contains new syscalls + // + while(true) + { + // + // Read the block header + // + readsize = gzread(f, &bh, sizeof(bh)); + + if(readsize != sizeof(bh)) + { + int err_no = 0; + const char* err_str = gzerror(f, &err_no); + if(err_no) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading file: %s, ernum=%d", err_str, err_no); + return SCAP_FAILURE; + } + + if(readsize == 0) + { + // + // We read exactly 0 bytes. This indicates a correct end of file. + // + return SCAP_EOF; + } + else + { + CHECK_READ_SIZE(readsize, sizeof(bh)); + } + } + + if(bh.block_type != EV_BLOCK_TYPE && + bh.block_type != EV_BLOCK_TYPE_V2 && + bh.block_type != EV_BLOCK_TYPE_INT && + bh.block_type != EVF_BLOCK_TYPE && + bh.block_type != EVF_BLOCK_TYPE_V2) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "unexpected block type %u", (uint32_t)bh.block_type); + handle->m_unexpected_block_readsize = readsize; + return SCAP_UNEXPECTED_BLOCK; + } + + hdr_len = sizeof(struct ppm_evt_hdr); + if(bh.block_type != EV_BLOCK_TYPE_V2 && bh.block_type != EVF_BLOCK_TYPE_V2) + { + hdr_len -= 4; + } + + if(bh.block_total_length < sizeof(bh) + hdr_len + 4) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "block length too short %u", (uint32_t)bh.block_total_length); + return SCAP_FAILURE; + } + + // + // Read the event + // + readlen = bh.block_total_length - sizeof(bh); + if (readlen > FILE_READ_BUF_SIZE) { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "event block length %u greater than read buffer size %u", + readlen, + FILE_READ_BUF_SIZE); + return SCAP_FAILURE; + } + + readsize = gzread(f, handle->m_file_evt_buf, readlen); + CHECK_READ_SIZE(readsize, readlen); + + // + // EVF_BLOCK_TYPE has 32 bits of flags + // + *pcpuid = *(uint16_t *)handle->m_file_evt_buf; + + if(bh.block_type == EVF_BLOCK_TYPE || bh.block_type == EVF_BLOCK_TYPE_V2) + { + handle->m_last_evt_dump_flags = *(uint32_t*)(handle->m_file_evt_buf + sizeof(uint16_t)); + *pevent = (struct ppm_evt_hdr *)(handle->m_file_evt_buf + sizeof(uint16_t) + sizeof(uint32_t)); + } + else + { + handle->m_last_evt_dump_flags = 0; + *pevent = (struct ppm_evt_hdr *)(handle->m_file_evt_buf + sizeof(uint16_t)); + } + + if((*pevent)->type >= PPM_EVENT_MAX) + { + // + // We're reading a capture that contains new syscalls. + // We can't do anything else that skips them. + // + continue; + } + + if(bh.block_type != EV_BLOCK_TYPE_V2 && bh.block_type != EVF_BLOCK_TYPE_V2) + { + // + // We're reading a old capture which events don't have nparams in the header. + // Convert it to the current version. + // + if((readlen + sizeof(uint32_t)) > FILE_READ_BUF_SIZE) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "cannot convert v1 event block to v2 (%lu greater than read buffer size %u)", + readlen + sizeof(uint32_t), + FILE_READ_BUF_SIZE); + return SCAP_FAILURE; + } + + memmove((char *)*pevent + sizeof(struct ppm_evt_hdr), + (char *)*pevent + sizeof(struct ppm_evt_hdr) - sizeof(uint32_t), + readlen - ((char *)*pevent - handle->m_file_evt_buf) - (sizeof(struct ppm_evt_hdr) - sizeof(uint32_t))); + (*pevent)->len += sizeof(uint32_t); + + // In old captures, the length of PPME_NOTIFICATION_E and PPME_INFRASTRUCTURE_EVENT_E + // is not correct. Adjust it, otherwise the following code will never find a match + if((*pevent)->type == PPME_NOTIFICATION_E || (*pevent)->type == PPME_INFRASTRUCTURE_EVENT_E) + { + (*pevent)->len -= 3; + } + + // + // The number of parameters needs to be calculated based on the block len. + // Use the current number of parameters as starting point and decrease it + // until size matches. + // + char *end = (char *)*pevent + (*pevent)->len; + uint16_t *lens = (uint16_t *)((char *)*pevent + sizeof(struct ppm_evt_hdr)); + uint32_t nparams; + bool done = false; + for(nparams = g_event_info[(*pevent)->type].nparams; (int)nparams >= 0; nparams--) + { + char *valptr = (char *)lens + nparams * sizeof(uint16_t); + if(valptr > end) + { + continue; + } + uint32_t i; + for(i = 0; i < nparams; i++) + { + valptr += lens[i]; + } + if(valptr < end) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "cannot convert v1 event block to v2 (corrupted trace file - can't calculate nparams)."); + return SCAP_FAILURE; + } + ASSERT(valptr >= end); + if(valptr == end) + { + done = true; + break; + } + } + if(!done) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "cannot convert v1 event block to v2 (corrupted trace file - can't calculate nparams) (2)."); + return SCAP_FAILURE; + } + (*pevent)->nparams = nparams; + } + + break; + } + + return SCAP_SUCCESS; +} + +uint64_t scap_ftell(scap_t *handle) +{ + gzFile f = handle->m_file; + ASSERT(f != NULL); + + return gztell(f); +} + +void scap_fseek(scap_t *handle, uint64_t off) +{ + gzFile f = handle->m_file; + ASSERT(f != NULL); + + gzseek(f, off, SEEK_SET); +} diff --git a/userspace/libscap/scap_savefile.h b/userspace/libscap/scap_savefile.h index f54f74bbea..91c84ee019 100644 --- a/userspace/libscap/scap_savefile.h +++ b/userspace/libscap/scap_savefile.h @@ -1,25 +1,28 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ // Force struct alignment #if defined _MSC_VER #pragma pack(push) #pragma pack(1) +#elif defined __sun +#pragma pack(1) #else #pragma pack(push, 1) #endif @@ -42,9 +45,14 @@ typedef struct _block_header // Used to recognize if a section is in host byte order or not. #define SHB_MAGIC 0x1A2B3C4D // Major version of the file format supported by this library. +// Must be increased only when if the new version of the software +// is not able anymore to read older captures #define CURRENT_MAJOR_VERSION 1 // Minor version of the file format supported by this library. -#define CURRENT_MINOR_VERSION 0 +// We used to bump it every time the event table was updated, but +// after adding {retro,forward} captures compatibility support +// this is not required anymore. +#define CURRENT_MINOR_VERSION 2 typedef struct _section_header_block { @@ -54,52 +62,96 @@ typedef struct _section_header_block uint64_t section_length; }section_header_block; +// NB: +// Starting from scap version 1.2, block versions will no longer be changed. +// New block fields must be appended and, instead of using the version, the +// lengths of the sub blocks will be used to differentiate between versions. +// For more information, look at the comments inside the various scap_read_* +// functions. + /////////////////////////////////////////////////////////////////////////////// // MACHINE INFO BLOCK /////////////////////////////////////////////////////////////////////////////// -#define MI_BLOCK_TYPE 0x201 -#define MI_BLOCK_TYPE_INT 0x8002ABCD // This is the unofficial number used before the - // library release. We'll keep him for a while for - // backward compatibility +#define MI_BLOCK_TYPE 0x201 +#define MI_BLOCK_TYPE_INT 0x8002ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility /////////////////////////////////////////////////////////////////////////////// // PROCESS LIST BLOCK /////////////////////////////////////////////////////////////////////////////// -#define PL_BLOCK_TYPE 0x202 -#define PL_BLOCK_TYPE_INT 0x8000ABCD // This is the unofficial number used before the - // library release. We'll keep him for a while for - // backward compatibility +#define PL_BLOCK_TYPE_V1 0x202 +#define PL_BLOCK_TYPE_V1_INT 0x8000ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility + +#define PL_BLOCK_TYPE_V2 0x207 +#define PL_BLOCK_TYPE_V2_INT 0x8013ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility + +#define PL_BLOCK_TYPE_V3 0x209 +#define PL_BLOCK_TYPE_V3_INT 0x8014ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility + +#define PL_BLOCK_TYPE_V4 0x210 + +#define PL_BLOCK_TYPE_V5 0x211 + +#define PL_BLOCK_TYPE_V6 0x212 + +#define PL_BLOCK_TYPE_V7 0x213 + +#define PL_BLOCK_TYPE_V8 0x214 + +#define PL_BLOCK_TYPE_V9 0x215 /////////////////////////////////////////////////////////////////////////////// // FD LIST BLOCK /////////////////////////////////////////////////////////////////////////////// -#define FDL_BLOCK_TYPE 0x203 -#define FDL_BLOCK_TYPE_INT 0x8001ABCD // This is the unofficial number used before the - // library release. We'll keep him for a while for - // backward compatibility +#define FDL_BLOCK_TYPE 0x203 +#define FDL_BLOCK_TYPE_INT 0x8001ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility +#define FDL_BLOCK_TYPE_V2 0x218 /////////////////////////////////////////////////////////////////////////////// // EVENT BLOCK /////////////////////////////////////////////////////////////////////////////// -#define EV_BLOCK_TYPE 0x204 -#define EV_BLOCK_TYPE_INT 0x8010ABCD // This is the unofficial number used before the - // library release. We'll keep him for a while for - // backward compatibility +#define EV_BLOCK_TYPE 0x204 +#define EV_BLOCK_TYPE_INT 0x8010ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility +#define EV_BLOCK_TYPE_V2 0x216 /////////////////////////////////////////////////////////////////////////////// // INTERFACE LIST BLOCK /////////////////////////////////////////////////////////////////////////////// -#define IL_BLOCK_TYPE 0x205 -#define IL_BLOCK_TYPE_INT 0x8011ABCD // This is the unofficial number used before the - // library release. We'll keep him for a while for - // backward compatibility +#define IL_BLOCK_TYPE 0x205 +#define IL_BLOCK_TYPE_INT 0x8011ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility +#define IL_BLOCK_TYPE_V2 0x219 /////////////////////////////////////////////////////////////////////////////// // USER LIST BLOCK /////////////////////////////////////////////////////////////////////////////// -#define UL_BLOCK_TYPE 0x206 -#define UL_BLOCK_TYPE_INT 0x8012ABCD // This is the unofficial number used before the - // library release. We'll keep him for a while for - // backward compatibility +#define UL_BLOCK_TYPE 0x206 +#define UL_BLOCK_TYPE_INT 0x8012ABCD // This is the unofficial number used before the + // library release. We'll keep him for a while for + // backward compatibility +#define UL_BLOCK_TYPE_V2 0x220 + +/////////////////////////////////////////////////////////////////////////////// +// EVENT BLOCK WITH FLAGS +/////////////////////////////////////////////////////////////////////////////// +#define EVF_BLOCK_TYPE 0x208 + +#define EVF_BLOCK_TYPE_V2 0x217 +#if defined __sun +#pragma pack() +#else #pragma pack(pop) +#endif diff --git a/userspace/libscap/scap_udig.c b/userspace/libscap/scap_udig.c new file mode 100644 index 0000000000..3b5248853e --- /dev/null +++ b/userspace/libscap/scap_udig.c @@ -0,0 +1,388 @@ +#ifndef _WIN32 +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "scap.h" +#include "scap-int.h" +#include "../../driver/ppm_ringbuffer.h" + +#define PPM_PORT_STATSD 8125 + +#ifndef UDIG_INSTRUMENTER +#define ud_shm_open shm_open +#else +int ud_shm_open(const char *name, int flag, mode_t mode); +#endif + +/////////////////////////////////////////////////////////////////////////////// +// The following 2 function map the ring buffer and the ring buffer +// descriptors into the address space of this process. +// This is the buffer that will be consumed by scap. +/////////////////////////////////////////////////////////////////////////////// +int32_t udig_alloc_ring(int* ring_fd, + uint8_t** ring, + uint32_t *ringsize, + char *error) +{ + // + // First, try to open an existing ring + // + *ring_fd = ud_shm_open(UDIG_RING_SM_FNAME, O_RDWR, 0); + if(*ring_fd >= 0) + { + // + // Existing ring found, find out the size + // + struct stat rstat; + if(fstat(*ring_fd, &rstat) < 0) + { + snprintf(error, SCAP_LASTERR_SIZE, "udig_alloc_ring fstat error: %s\n", strerror(errno)); + return SCAP_FAILURE; + } + + *ringsize = (uint32_t)rstat.st_size; + } + else + { + // + // No ring found, allocate a new one. + // Note that, according to the man page, the content of the buffer will + // be initialized to 0. + // + *ringsize = UDIG_RING_SIZE; + + *ring_fd = ud_shm_open(UDIG_RING_SM_FNAME, O_CREAT | O_RDWR, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + if(*ring_fd >= 0) + { + // + // For some reason, shm_open doesn't always set the write flag for + // 'group' and 'other'. Fix it here. + // + fchmod(*ring_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + + if(ftruncate(*ring_fd, *ringsize) < 0) + { + snprintf(error, SCAP_LASTERR_SIZE, "udig_alloc_ring ftruncate error: %s\n", strerror(errno)); + close(*ring_fd); + return SCAP_FAILURE; + } + } + else + { + snprintf(error, SCAP_LASTERR_SIZE, "udig_alloc_ring shm_open error: %s\n", strerror(errno)); + return SCAP_FAILURE; + } + } + + // + // Map the ring. This is a multi-step process because we want to map two + // consecutive copies of the same memory to reuse the driver fillers, which + // expect to be able to go past the end of the ring. + // First of all, allocate enough space for the 2 copies. This allows us + // to find an area of consecutive memory that is big enough. + // + uint8_t* buf1 = (uint8_t*)mmap(NULL, (*ringsize) * 2, + PROT_WRITE, MAP_SHARED, + *ring_fd, 0); + if(buf1 == MAP_FAILED) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't map double buffer\n"); + close(*ring_fd); + return SCAP_FAILURE; + } + + // Now that we have the address, unmap the double-lenght buffer so we can + // use the two halves. + munmap(buf1, (*ringsize) * 2); + + // Map the first ring copy at exactly the beginning of the previously + // allocated area, forcing it with MAP_FIXED. + *ring = (uint8_t*)mmap(buf1, *ringsize, + PROT_WRITE, MAP_SHARED | MAP_FIXED, *ring_fd, 0); + if(*ring != buf1) + { + snprintf(error, SCAP_LASTERR_SIZE, "udig_alloc_ring mmap 2 error: %s\n", strerror(errno)); + close(*ring_fd); + return SCAP_FAILURE; + } + + // Map the second ring copy just after the end of the first one. + uint8_t* buf2 = buf1 + *ringsize; + uint8_t* ring2 = (uint8_t*)mmap(buf2, *ringsize, + PROT_WRITE, MAP_SHARED | MAP_FIXED, *ring_fd, 0); + if(ring2 != buf2) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't map second copy of buffer, needed %p, obtained %p, base=%p\n", + buf2, ring2, buf1); + close(*ring_fd); + munmap(*ring, *ringsize); + return SCAP_FAILURE; + } + + return SCAP_SUCCESS; +} + +int32_t udig_alloc_ring_descriptors(int* ring_descs_fd, + struct ppm_ring_buffer_info** ring_info, + struct udig_ring_buffer_status** ring_status, + char *error) +{ + uint32_t mem_size = sizeof(struct ppm_ring_buffer_info) + sizeof(struct udig_ring_buffer_status); + + // + // First, try to open an existing ring + // + *ring_descs_fd = ud_shm_open(UDIG_RING_DESCS_SM_FNAME, O_RDWR, 0); + if(*ring_descs_fd < 0) + { + // + // No existing ring file found in /dev/shm, create a new one. + // + *ring_descs_fd = ud_shm_open(UDIG_RING_DESCS_SM_FNAME, O_CREAT | O_RDWR | O_EXCL, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + if(*ring_descs_fd >= 0) + { + // + // For some reason, shm_open doesn't always set the write flag for + // 'group' and 'other'. Fix it here. + // + fchmod(*ring_descs_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + + // + // Ring created, set its size + // + if(ftruncate(*ring_descs_fd, mem_size) < 0) + { + snprintf(error, SCAP_LASTERR_SIZE, "udig_alloc_ring_descriptors ftruncate error: %s\n", strerror(errno)); + close(*ring_descs_fd); + shm_unlink(UDIG_RING_DESCS_SM_FNAME); + return SCAP_FAILURE; + } + } + else + { + snprintf(error, SCAP_LASTERR_SIZE, "udig_alloc_ring_descriptors shm_open error: %s\n", strerror(errno)); + shm_unlink(UDIG_RING_DESCS_SM_FNAME); + return SCAP_FAILURE; + } + } + + // + // Map the memory + // + uint8_t* descs = (uint8_t*)mmap(NULL, mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, + *ring_descs_fd, 0); + if(descs == MAP_FAILED) + { + snprintf(error, SCAP_LASTERR_SIZE, "can't map descriptors\n"); + close(*ring_descs_fd); + return SCAP_FAILURE; + } + + *ring_info = (struct ppm_ring_buffer_info*)descs; + + // + // Locate the ring buffer status object + // + *ring_status = (struct udig_ring_buffer_status*)((uint64_t)*ring_info + + sizeof(struct ppm_ring_buffer_info)); + + // + // If we are the original creators of the shared buffer, proceed to + // initialize it. + // Note that, according to the man page of ud_shm_open, we are guaranteed that + // the content of the buffer will initiually be initialized to 0. + // + if(__sync_bool_compare_and_swap(&((*ring_status)->m_initialized), 0, 1)) + { + (*ring_status)->m_buffer_lock = 0; + (*ring_status)->m_capturing_pid = 0; + (*ring_status)->m_stopped = 0; + (*ring_status)->m_last_print_time.tv_sec = 0; + (*ring_status)->m_last_print_time.tv_nsec = 0; + } + + return SCAP_SUCCESS; +} + +/////////////////////////////////////////////////////////////////////////////// +// These 2 function free the ring buffer and the ring buffer descriptors. +/////////////////////////////////////////////////////////////////////////////// +void udig_free_ring(uint8_t* addr, uint32_t size) +{ + munmap(addr, size / 2); + munmap(addr + size / 2, size / 2); +} + +void udig_free_ring_descriptors(uint8_t* addr) +{ + uint32_t mem_size = sizeof(struct ppm_ring_buffer_info) + sizeof(struct udig_ring_buffer_status); + munmap(addr, mem_size); +} + +/////////////////////////////////////////////////////////////////////////////// +// Capture control helpers. +/////////////////////////////////////////////////////////////////////////////// +bool acquire_and_init_ring_status_buffer(scap_t* handle) +{ + struct udig_ring_buffer_status* rbs = handle->m_devs[0].m_bufstatus; + bool res = __sync_bool_compare_and_swap(&(rbs->m_capturing_pid), 0, getpid()); + + if(res) + { + // + // Initialize the ring + // + rbs->m_stopped = 0; + rbs->m_last_print_time.tv_sec = 0; + rbs->m_last_print_time.tv_nsec = 0; + + // + // Initialize the consumer + // + struct udig_consumer_t* consumer = &(rbs->m_consumer); + + memset(consumer, 0, sizeof(struct udig_consumer_t)); + consumer->dropping_mode = 0; + consumer->snaplen = RW_SNAPLEN; + consumer->sampling_ratio = 1; + consumer->sampling_interval = 0; + consumer->is_dropping = 0; + consumer->do_dynamic_snaplen = false; + consumer->need_to_insert_drop_e = 0; + consumer->need_to_insert_drop_x = 0; + consumer->fullcapture_port_range_start = 0; + consumer->fullcapture_port_range_end = 0; + consumer->statsd_port = PPM_PORT_STATSD; + } + + return res; +} + +int32_t udig_begin_capture(scap_t* handle, char *error) +{ + struct udig_ring_buffer_status* rbs = handle->m_devs[0].m_bufstatus; + + if(rbs->m_capturing_pid != 0) + { + // + // Looks like there is already a consumer, but ther variable might still + // be set by a previous crashed consumer. To understand that, we check if + // there is an alive process with that pid. If not, we reset the variable. + // + char fbuf[48]; + snprintf(fbuf, sizeof(fbuf), "/proc/%d", rbs->m_capturing_pid); + FILE* f = fopen(fbuf, "r"); + if(f == NULL) + { + rbs->m_capturing_pid = 0; + } + else + { + fclose(f); + snprintf(error, SCAP_LASTERR_SIZE, "another udig capture is already active"); + return SCAP_FAILURE; + } + } + + struct ppm_ring_buffer_info* rbi = handle->m_devs[0].m_bufinfo; + rbi->head = 0; + rbi->tail = 0; + rbi->n_evts = 0; + rbi->n_drops_buffer = 0; + + if(acquire_and_init_ring_status_buffer(handle)) + { + handle->m_udig_capturing = true; + return SCAP_SUCCESS; + } + else + { + snprintf(error, SCAP_LASTERR_SIZE, "cannot start the capture"); + return SCAP_FAILURE; + } +} + +void udig_start_capture(scap_t* handle) +{ + struct udig_ring_buffer_status* rbs = handle->m_devs[0].m_bufstatus; + rbs->m_stopped = 0; +} + +void udig_stop_capture(scap_t* handle) +{ + struct udig_ring_buffer_status* rbs = handle->m_devs[0].m_bufstatus; + rbs->m_stopped = 1; +} + +void udig_end_capture(scap_t* handle) +{ + struct udig_ring_buffer_status* rbs = handle->m_devs[0].m_bufstatus; + if(handle->m_udig_capturing) + { + //__sync_bool_compare_and_swap(&(rbs->m_capturing_pid), getpid(), 0); + rbs->m_capturing_pid = 0; + } +} + +uint32_t udig_set_snaplen(scap_t* handle, uint32_t snaplen) +{ + struct udig_ring_buffer_status* rbs = handle->m_devs[0].m_bufstatus; + rbs->m_consumer.snaplen = snaplen; + return SCAP_SUCCESS; +} + +int32_t udig_stop_dropping_mode(scap_t* handle) +{ + struct udig_consumer_t* consumer = &(handle->m_devs[0].m_bufstatus->m_consumer); + consumer->dropping_mode = 0; + consumer->sampling_interval = 1000000000; + consumer->sampling_ratio = 1; + + return SCAP_SUCCESS; +} + +int32_t udig_start_dropping_mode(scap_t* handle, uint32_t sampling_ratio) +{ + struct udig_consumer_t* consumer = &(handle->m_devs[0].m_bufstatus->m_consumer); + + consumer->dropping_mode = 1; + + if(sampling_ratio != 1 && + sampling_ratio != 2 && + sampling_ratio != 4 && + sampling_ratio != 8 && + sampling_ratio != 16 && + sampling_ratio != 32 && + sampling_ratio != 64 && + sampling_ratio != 128) + { + snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid sampling ratio %u\n", sampling_ratio); + return SCAP_FAILURE; + } + + consumer->sampling_interval = 1000000000 / sampling_ratio; + consumer->sampling_ratio = sampling_ratio; + + return SCAP_SUCCESS; +} + +#endif // _WIN32 diff --git a/userspace/libscap/scap_userlist.c b/userspace/libscap/scap_userlist.c index 288e322c8c..82b0b5cea0 100644 --- a/userspace/libscap/scap_userlist.c +++ b/userspace/libscap/scap_userlist.c @@ -1,33 +1,34 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include #include "scap.h" #include "scap-int.h" -#if !defined(_WIN32) && !defined(__APPLE__) +#if defined(HAS_CAPTURE) #include #include #include // -// Allocate and return the list of interfaces on this system +// Allocate and return the list of users on this system // int32_t scap_create_userlist(scap_t* handle) { @@ -37,8 +38,8 @@ int32_t scap_create_userlist(scap_t* handle) struct group *g; // - // If the list of interfaces was already allocated for this handle (for example because this is - // not the first interface list block), free it + // If the list of users was already allocated for this handle (for example because this is + // not the first user list block), free it // if(handle->m_userlist != NULL) { @@ -49,10 +50,12 @@ int32_t scap_create_userlist(scap_t* handle) // // First pass: count the number of users and the number of groups // + setpwent(); p = getpwent(); for(usercnt = 0; p; p = getpwent(), usercnt++); endpwent(); + setgrent(); g = getgrent(); for(grpcnt = 0; g; g = getgrent(), grpcnt++); endgrent(); @@ -82,8 +85,8 @@ int32_t scap_create_userlist(scap_t* handle) if(handle->m_userlist->groups == NULL) { snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "grouplist allocation failed(2)"); - free(handle->m_userlist); free(handle->m_userlist->users); + free(handle->m_userlist); return SCAP_FAILURE; } @@ -92,15 +95,40 @@ int32_t scap_create_userlist(scap_t* handle) // //users + setpwent(); p = getpwent(); for(usercnt = 0; p; p = getpwent(), usercnt++) { handle->m_userlist->users[usercnt].uid = p->pw_uid; handle->m_userlist->users[usercnt].gid = p->pw_gid; - strncpy(handle->m_userlist->users[usercnt].name, p->pw_name, sizeof(handle->m_userlist->users[usercnt].name)); - strncpy(handle->m_userlist->users[usercnt].homedir, p->pw_dir, sizeof(handle->m_userlist->users[usercnt].homedir)); - strncpy(handle->m_userlist->users[usercnt].shell, p->pw_shell, sizeof(handle->m_userlist->users[usercnt].shell)); + + if(p->pw_name) + { + strncpy(handle->m_userlist->users[usercnt].name, p->pw_name, sizeof(handle->m_userlist->users[usercnt].name)); + } + else + { + *handle->m_userlist->users[usercnt].name = '\0'; + } + + if(p->pw_dir) + { + strncpy(handle->m_userlist->users[usercnt].homedir, p->pw_dir, sizeof(handle->m_userlist->users[usercnt].homedir)); + } + else + { + *handle->m_userlist->users[usercnt].homedir = '\0'; + } + + if(p->pw_shell) + { + strncpy(handle->m_userlist->users[usercnt].shell, p->pw_shell, sizeof(handle->m_userlist->users[usercnt].shell)); + } + else + { + *handle->m_userlist->users[usercnt].shell = '\0'; + } handle->m_userlist->totsavelen += sizeof(uint8_t) + // type @@ -114,12 +142,21 @@ int32_t scap_create_userlist(scap_t* handle) endpwent(); // groups + setgrent(); g = getgrent(); for(grpcnt = 0; g; g = getgrent(), grpcnt++) { handle->m_userlist->groups[grpcnt].gid = g->gr_gid; - strncpy(handle->m_userlist->groups[grpcnt].name, g->gr_name, sizeof(handle->m_userlist->groups[grpcnt].name)); + + if(g->gr_name) + { + strncpy(handle->m_userlist->groups[grpcnt].name, g->gr_name, sizeof(handle->m_userlist->groups[grpcnt].name)); + } + else + { + *handle->m_userlist->groups[grpcnt].name = '\0'; + } handle->m_userlist->totsavelen += sizeof(uint8_t) + // type @@ -131,7 +168,7 @@ int32_t scap_create_userlist(scap_t* handle) return SCAP_SUCCESS; } -#endif // _WIN32 +#endif // HAS_CAPTURE // // Free a previously allocated list of users diff --git a/userspace/libscap/settings.h b/userspace/libscap/settings.h index e2dd88572a..7e260d8a55 100644 --- a/userspace/libscap/settings.h +++ b/userspace/libscap/settings.h @@ -1,5 +1,29 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ // // This flag can be used to include unsupported or unrecognized sockets // in the fd tables. It's useful to debug close() leaks // -#define INCLUDE_UNKNOWN_SOCKET_FDS \ No newline at end of file +#define INCLUDE_UNKNOWN_SOCKET_FDS + +#ifndef MINIMAL_BUILD +#define USE_ZLIB +#endif // MINIMAL_BUILD + +#define SCAP_NODRIVER_MAX_FD_LOOKUP 20 diff --git a/userspace/libscap/syscall_info_table.c b/userspace/libscap/syscall_info_table.c index 1333f6359f..fb433a6e56 100644 --- a/userspace/libscap/syscall_info_table.c +++ b/userspace/libscap/syscall_info_table.c @@ -1,328 +1,360 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "../common/sysdig_types.h" #include "../../driver/ppm_events_public.h" +#include "scap.h" /* * SYSCALL INFO TABLE */ const struct ppm_syscall_desc g_syscall_info_table[PPM_SC_MAX] = { - /*dummy*/ { EC_OTHER, "" }, - /*PPM_SC_RESTART_SYSCALL*/ { EC_SYSTEM, "restart_syscall" }, - /*PPM_SC_EXIT*/ { EC_PROCESS, "exit" }, - /*PPM_SC_READ*/ { EC_IO_READ, "read" }, - /*PPM_SC_WRITE*/ { EC_IO_WRITE, "write" }, - /*PPM_SC_OPEN*/ { EC_FILE, "open" }, - /*PPM_SC_CLOSE*/ { EC_FILE, "close" }, - /*PPM_SC_CREAT*/ { EC_FILE, "creat" }, - /*PPM_SC_LINK*/ { EC_FILE, "link" }, - /*PPM_SC_UNLINK*/ { EC_FILE, "unlink" }, - /*PPM_SC_CHDIR*/ { EC_FILE, "chdir" }, - /*PPM_SC_TIME*/ { EC_TIME, "time" }, - /*PPM_SC_MKNOD*/ { EC_FILE, "mknod" }, - /*PPM_SC_CHMOD*/ { EC_FILE, "chmod" }, - /*PPM_SC_STAT*/ { EC_FILE, "stat" }, - /*PPM_SC_LSEEK*/ { EC_FILE, "lseek" }, - /*PPM_SC_GETPID*/ { EC_PROCESS, "getpid" }, - /*PPM_SC_MOUNT*/ { EC_FILE, "mount" }, - /*PPM_SC_PTRACE*/ { EC_OTHER, "ptrace" }, - /*PPM_SC_ALARM*/ { EC_TIME, "alarm" }, - /*PPM_SC_FSTAT*/ { EC_FILE, "fstat" }, - /*PPM_SC_PAUSE*/ { EC_WAIT, "pause" }, /* WAIT UNTIL A SIGNAL ARRIVES */ - /*PPM_SC_UTIME*/ { EC_TIME, "utime" }, - /*PPM_SC_ACCESS*/ { EC_FILE, "access" }, /* checks whether the calling process can access the file pathname */ - /*PPM_SC_SYNC*/ { EC_IO_OTHER, "sync" }, /* causes all buffered modifications to file metadata and data to be written to the underlying file systems. */ - /*PPM_SC_KILL*/ { EC_IPC, "kill" }, - /*PPM_SC_RENAME*/ { EC_FILE, "rename" }, - /*PPM_SC_MKDIR*/ { EC_FILE, "mkdir" }, - /*PPM_SC_RMDIR*/ { EC_FILE, "rmdir" }, - /*PPM_SC_DUP*/ { EC_IO_OTHER, "dup" }, - /*PPM_SC_PIPE*/ { EC_IPC, "pipe" }, - /*PPM_SC_TIMES*/ { EC_TIME, "times" }, - /*PPM_SC_BRK*/ { EC_MEMORY, "brk" }, - /*PPM_SC_ACCT*/ { EC_PROCESS, "acct" }, - /*PPM_SC_IOCTL*/ { EC_IO_OTHER, "ioctl" }, - /*PPM_SC_FCNTL*/ { EC_WAIT, "fcntl" }, - /*PPM_SC_SETPGID*/ { EC_PROCESS, "setpgid" }, - /*PPM_SC_UMASK*/ { EC_PROCESS, "umask" }, /* sets the calling process's file mode creation mask */ - /*PPM_SC_CHROOT*/ { EC_IPC, "chroot" }, /* changes the root directory of the calling process to that specified in path. This directory will be used for pathnames beginning with /. The root directory is inherited by all children of the calling process. */ - /*PPM_SC_USTAT*/ { EC_FILE, "ustat" }, /* returns information about a mounted file system. */ - /*PPM_SC_DUP2*/ { EC_IO_OTHER, "dup2" }, - /*PPM_SC_GETPPID*/ { EC_PROCESS, "getppid" }, - /*PPM_SC_GETPGRP*/ { EC_PROCESS, "getpgrp" }, - /*PPM_SC_SETSID*/ { EC_PROCESS, "setsid" }, /* creates a session and sets the process group ID */ - /*PPM_SC_SETHOSTNAME*/ { EC_SYSTEM, "sethostname" }, - /*PPM_SC_SETRLIMIT*/ { EC_PROCESS, "setrlimit" }, /* get/set resource (CPU, FDs, memory...) limits */ - /*PPM_SC_GETRUSAGE*/ { EC_PROCESS, "getrusage" }, /* returns resource usage measures for who */ - /*PPM_SC_GETTIMEOFDAY*/ { EC_TIME, "gettimeofday" }, - /*PPM_SC_SETTIMEOFDAY*/ { EC_TIME, "settimeofday" }, - /*PPM_SC_SYMLINK*/ { EC_FILE, "symlink" }, - /*PPM_SC_LSTAT*/ { EC_FILE, "lstat" }, - /*PPM_SC_READLINK*/ { EC_FILE, "readlink" }, - /*PPM_SC_USELIB*/ { EC_PROCESS, "uselib" }, /* load shared library */ - /*PPM_SC_SWAPON*/ { EC_PROCESS, "swapon" }, /* start/stop swapping to file/device */ - /*PPM_SC_REBOOT*/ { EC_SYSTEM, "reboot" }, - /*PPM_SC_MMAP*/ { EC_FILE, "mmap" }, - /*PPM_SC_MUNMAP*/ { EC_FILE, "munmap" }, - /*PPM_SC_TRUNCATE*/ { EC_FILE, "truncate" }, /* truncate a file to a specified length */ - /*PPM_SC_FTRUNCATE*/ { EC_FILE, "ftruncate" }, /* truncate a file to a specified length */ - /*PPM_SC_FCHMOD*/ { EC_FILE, "fchmod" }, - /*PPM_SC_GETPRIORITY*/ { EC_PROCESS, "getpriority" }, /* get/set program scheduling priority */ - /*PPM_SC_SETPRIORITY*/ { EC_PROCESS, "setpriority" }, /* get/set program scheduling priority */ - /*PPM_SC_STATFS*/ { EC_FILE, "statfs" }, /* returns information about a mounted file system */ - /*PPM_SC_FSTATFS*/ { EC_FILE, "fstatfs" }, /* returns information about a mounted file system */ - /*PPM_SC_SYSLOG*/ { EC_SYSTEM, "syslog" }, /* read and/or clear kernel message ring buffer; set console_loglevel */ - /*PPM_SC_SETITIMER*/ { EC_TIME, "setitimer" }, - /*PPM_SC_GETITIMER*/ { EC_TIME, "getitimer" }, - /*PPM_SC_UNAME*/ { EC_SYSTEM, "uname" }, /* get name and information about current kernel */ - /*PPM_SC_VHANGUP*/ { EC_OTHER , "vhangup" }, /* simulates a hangup on the current terminal. This call arranges for other users to have a "clean" terminal at login time. */ - /*PPM_SC_WAIT4*/ { EC_WAIT, "wait4" }, /* OBSOLETE */ - /*PPM_SC_SWAPOFF*/ { EC_SYSTEM, "swapoff" }, /* start/stop swapping to file/device */ - /*PPM_SC_SYSINFO*/ { EC_SYSTEM, "sysinfo" }, /* returns information on overall system statistics */ - /*PPM_SC_FSYNC*/ { EC_IO_OTHER, "fsync" }, /* sync file content */ - /*PPM_SC_SETDOMAINNAME*/ { EC_SYSTEM, "setdomainname" }, - /*PPM_SC_ADJTIMEX*/ { EC_SYSTEM, "adjtimex" }, /* tune kernel clock */ - /*PPM_SC_MPROTECT*/ { EC_MEMORY, "mprotect" }, /* set protection on a region of memory */ - /*PPM_SC_INIT_MODULE*/ { EC_SYSTEM, "init_module" }, /* load a kernel module */ - /*PPM_SC_DELETE_MODULE*/ { EC_SYSTEM, "delete_module" }, - /*PPM_SC_QUOTACTL*/ { EC_SYSTEM, "quotactl" }, - /*PPM_SC_GETPGID*/ { EC_PROCESS, "getpgid" }, - /*PPM_SC_FCHDIR*/ { EC_FILE, "fchdir" }, - /*PPM_SC_SYSFS*/ { EC_SYSTEM, "sysfs" }, /* get file system type information */ - /*PPM_SC_PERSONALITY*/ { EC_PROCESS, "personality" }, /* set the process execution domain */ - /*PPM_SC_GETDENTS*/ { EC_FILE, "getdents" }, /* get directory entries */ - /*PPM_SC_SELECT*/ { EC_WAIT, "select" }, - /*PPM_SC_FLOCK*/ { EC_FILE, "flock" }, /* apply or remove an advisory lock on an open file */ - /*PPM_SC_MSYNC*/ { EC_IO_OTHER, "msync" }, /* synchronize a file with a memory map */ - /*PPM_SC_READV*/ { EC_IO_READ, "readv" }, - /*PPM_SC_WRITEV*/ { EC_IO_WRITE, "writev" }, - /*PPM_SC_GETSID*/ { EC_PROCESS, "getsid" }, /* returns the session ID of the calling process */ - /*PPM_SC_FDATASYNC*/ { EC_IO_OTHER, "fdatasync" }, /* synchronize a file's in-core state with storage device */ - /*PPM_SC_MLOCK*/ { EC_MEMORY, "mlock" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ - /*PPM_SC_MUNLOCK*/ { EC_MEMORY, "munlock" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ - /*PPM_SC_MLOCKALL*/ { EC_MEMORY, "mlockall" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ - /*PPM_SC_MUNLOCKALL*/ { EC_MEMORY, "munlockall" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ - /*PPM_SC_SCHED_SETPARAM*/ { EC_PROCESS, "sched_setparam" }, - /*PPM_SC_SCHED_GETPARAM*/ { EC_PROCESS, "sched_getparam" }, - /*PPM_SC_SCHED_SETSCHEDULER*/ { EC_PROCESS, "sched_setscheduler" }, - /*PPM_SC_SCHED_GETSCHEDULER*/ { EC_PROCESS, "sched_getscheduler" }, - /*PPM_SC_SCHED_YIELD*/ { EC_SLEEP, "sched_yield" }, - /*PPM_SC_SCHED_GET_PRIORITY_MAX*/ { EC_PROCESS, "sched_get_priority_max" }, - /*PPM_SC_SCHED_GET_PRIORITY_MIN*/ { EC_PROCESS, "sched_get_priority_min" }, - /*PPM_SC_SCHED_RR_GET_INTERVAL*/ { EC_PROCESS, "sched_rr_get_interval" }, - /*PPM_SC_NANOSLEEP*/ { EC_SLEEP, "nanosleep" }, - /*PPM_SC_MREMAP*/ { EC_FILE, "mremap" }, - /*PPM_SC_POLL*/ { EC_WAIT, "poll" }, - /*PPM_SC_PRCTL*/ { EC_PROCESS, "prctl" }, /* operations on a process */ - /*PPM_SC_RT_SIGACTION*/ { EC_SIGNAL, "rt_sigaction" }, - /*PPM_SC_RT_SIGPROCMASK*/ { EC_SIGNAL, "rt_sigprocmask" }, - /*PPM_SC_RT_SIGPENDING*/ { EC_SIGNAL, "rt_sigpending" }, - /*PPM_SC_RT_SIGTIMEDWAIT*/ { EC_SIGNAL, "rt_sigtimedwait" }, - /*PPM_SC_RT_SIGQUEUEINFO*/ { EC_SIGNAL, "rt_sigqueueinfo" }, - /*PPM_SC_RT_SIGSUSPEND*/ { EC_SIGNAL, "rt_sigsuspend" }, - /*PPM_SC_GETCWD*/ { EC_FILE, "getcwd" }, - /*PPM_SC_CAPGET*/ { EC_PROCESS, "capget" }, /* set/get capabilities of thread(s) */ - /*PPM_SC_CAPSET*/ { EC_PROCESS, "capset" }, /* set/get capabilities of thread(s) */ - /*PPM_SC_SENDFILE*/ { EC_FILE, "sendfile" }, /* transfer data between file descriptors */ - /*PPM_SC_GETRLIMIT*/ { EC_PROCESS, "getrlimit" }, - /*PPM_SC_LCHOWN*/ { EC_FILE, "lchown" }, - /*PPM_SC_GETUID*/ { EC_USER, "getuid" }, - /*PPM_SC_GETGID*/ { EC_USER, "getgid" }, - /*PPM_SC_GETEUID*/ { EC_USER, "geteuid" }, - /*PPM_SC_GETEGID*/ { EC_USER, "getegid" }, - /*PPM_SC_SETREUID*/ { EC_USER, "setreuid" }, - /*PPM_SC_SETREGID*/ { EC_USER, "setregid" }, - /*PPM_SC_GETGROUPS*/ { EC_USER, "getgroups" }, /* returns the supplementary group IDs of the calling process */ - /*PPM_SC_SETGROUPS*/ { EC_USER, "setgroups" }, /* returns the supplementary group IDs of the calling process */ - /*PPM_SC_FCHOWN*/ { EC_FILE, "fchown" }, - /*PPM_SC_SETRESUID*/ { EC_USER, "setresuid" }, - /*PPM_SC_GETRESUID*/ { EC_USER, "getresuid" }, - /*PPM_SC_SETRESGID*/ { EC_USER, "setresgid" }, - /*PPM_SC_GETRESGID*/ { EC_USER, "getresgid" }, - /*PPM_SC_CHOWN*/ { EC_FILE, "chown" }, - /*PPM_SC_SETUID*/ { EC_USER, "setuid" }, - /*PPM_SC_SETGID*/ { EC_USER, "setgid" }, - /*PPM_SC_SETFSUID*/ { EC_USER, "setfsuid" }, - /*PPM_SC_SETFSGID*/ { EC_USER, "setfsgid" }, - /*PPM_SC_PIVOT_ROOT*/ { EC_PROCESS, "pivot_root" }, - /*PPM_SC_MINCORE*/ { EC_MEMORY, "mincore" }, /* determine whether pages are resident in memory */ - /*PPM_SC_MADVISE*/ { EC_MEMORY, "madvise" }, /* give advice about use of memory */ - /*PPM_SC_GETTID*/ { EC_PROCESS, "gettid" }, /* returns the caller's thread ID (TID) */ - /*PPM_SC_SETXATTR*/ { EC_FILE, "setxattr" }, /* set inode attribute */ - /*PPM_SC_LSETXATTR*/ { EC_FILE, "lsetxattr" }, - /*PPM_SC_FSETXATTR*/ { EC_FILE, "fsetxattr" }, - /*PPM_SC_GETXATTR*/ { EC_FILE, "getxattr" }, - /*PPM_SC_LGETXATTR*/ { EC_FILE, "lgetxattr" }, - /*PPM_SC_FGETXATTR*/ { EC_FILE, "fgetxattr" }, - /*PPM_SC_LISTXATTR*/ { EC_FILE, "listxattr" }, - /*PPM_SC_LLISTXATTR*/ { EC_FILE, "llistxattr" }, - /*PPM_SC_FLISTXATTR*/ { EC_FILE, "flistxattr" }, - /*PPM_SC_REMOVEXATTR*/ { EC_FILE, "removexattr" }, - /*PPM_SC_LREMOVEXATTR*/ { EC_FILE, "lremovexattr" }, - /*PPM_SC_FREMOVEXATTR*/ { EC_FILE, "fremovexattr" }, - /*PPM_SC_TKILL*/ { EC_SIGNAL, "tkill" }, /* send a signal to a thread */ - /*PPM_SC_FUTEX*/ { EC_IPC, "futex" }, - /*PPM_SC_SCHED_SETAFFINITY*/ { EC_PROCESS, "sched_setaffinity" }, - /*PPM_SC_SCHED_GETAFFINITY*/ { EC_PROCESS, "sched_getaffinity" }, - /*PPM_SC_SET_THREAD_AREA*/ { EC_PROCESS, "set_thread_area" }, - /*PPM_SC_GET_THREAD_AREA*/ { EC_PROCESS, "get_thread_area" }, - /*PPM_SC_IO_SETUP*/ { EC_IO_OTHER, "io_setup" }, /* create an asynchronous I/O context (for libaio) */ - /*PPM_SC_IO_DESTROY*/ { EC_IO_OTHER, "io_destroy" }, - /*PPM_SC_IO_GETEVENTS*/ { EC_IO_OTHER, "io_getevents" }, - /*PPM_SC_IO_SUBMIT*/ { EC_IO_OTHER, "io_submit" }, - /*PPM_SC_IO_CANCEL*/ { EC_IO_OTHER, "io_cancel" }, - /*PPM_SC_EXIT_GROUP*/ { EC_IO_OTHER, "exit_group" }, - /*PPM_SC_EPOLL_CREATE*/ { EC_WAIT, "epoll_create" }, - /*PPM_SC_EPOLL_CTL*/ { EC_WAIT, "epoll_ctl" }, - /*PPM_SC_EPOLL_WAIT*/ { EC_WAIT, "epoll_wait" }, - /*PPM_SC_REMAP_FILE_PAGES*/ { EC_FILE, "remap_file_pages" }, /* create a nonlinear file mapping */ - /*PPM_SC_SET_TID_ADDRESS*/ { EC_PROCESS, "set_tid_address" }, /* set pointer to thread ID */ - /*PPM_SC_TIMER_CREATE*/ { EC_TIME, "timer_create" }, - /*PPM_SC_TIMER_SETTIME*/ { EC_TIME, "timer_settime" }, - /*PPM_SC_TIMER_GETTIME*/ { EC_TIME, "timer_gettime" }, - /*PPM_SC_TIMER_GETOVERRUN*/ { EC_TIME, "timer_getoverrun" }, - /*PPM_SC_TIMER_DELETE*/ { EC_TIME, "timer_delete" }, - /*PPM_SC_CLOCK_SETTIME*/ { EC_TIME, "clock_settime" }, - /*PPM_SC_CLOCK_GETTIME*/ { EC_TIME, "clock_gettime" }, - /*PPM_SC_CLOCK_GETRES*/ { EC_TIME, "clock_getres" }, - /*PPM_SC_CLOCK_NANOSLEEP*/ { EC_SLEEP, "clock_nanosleep" }, - /*PPM_SC_TGKILL*/ { EC_SIGNAL, "tgkill" }, - /*PPM_SC_UTIMES*/ { EC_FILE, "utimes" }, /* change file last access and modification times */ - /*PPM_SC_MQ_OPEN*/ { EC_IPC, "mq_open" }, /* Message queues. See http://linux.die.net/man/7/mq_overview. */ - /*PPM_SC_MQ_UNLINK*/ { EC_IPC, "mq_unlink" }, - /*PPM_SC_MQ_TIMEDSEND*/ { EC_IPC, "mq_timedsend" }, - /*PPM_SC_MQ_TIMEDRECEIVE*/ { EC_IPC, "mq_timedreceive" }, - /*PPM_SC_MQ_NOTIFY*/ { EC_IPC, "mq_notify" }, - /*PPM_SC_MQ_GETSETATTR*/ { EC_IPC, "mq_getsetattr" }, - /*PPM_SC_KEXEC_LOAD*/ { EC_SYSTEM, "kexec_load" }, /* load a new kernel for later execution */ - /*PPM_SC_WAITID*/ { EC_WAIT, "waitid" }, - /*PPM_SC_ADD_KEY*/ { EC_SYSTEM, "add_key" }, /* add a key to the kernel's key management facility */ - /*PPM_SC_REQUEST_KEY*/ { EC_SYSTEM, "request_key" }, - /*PPM_SC_KEYCTL*/ { EC_SYSTEM, "keyctl" }, - /*PPM_SC_IOPRIO_SET*/ { EC_PROCESS, "ioprio_set" }, /* get/set I/O scheduling class and priority */ - /*PPM_SC_IOPRIO_GET*/ { EC_PROCESS, "ioprio_get" }, /* get/set I/O scheduling class and priority */ - /*PPM_SC_INOTIFY_INIT*/ { EC_IPC, "inotify_init" }, /* initialize an inotify event queue instance. See http://en.wikipedia.org/wiki/Inotify. */ - /*PPM_SC_INOTIFY_ADD_WATCH*/ { EC_IPC, "inotify_add_watch" }, - /*PPM_SC_INOTIFY_RM_WATCH*/ { EC_IPC, "inotify_rm_watch" }, - /*PPM_SC_OPENAT*/ { EC_FILE, "openat" }, - /*PPM_SC_MKDIRAT*/ { EC_FILE, "mkdirat" }, - /*PPM_SC_MKNODAT*/ { EC_FILE, "mknodat" }, - /*PPM_SC_FCHOWNAT*/ { EC_FILE, "fchownat" }, - /*PPM_SC_FUTIMESAT*/ { EC_FILE, "futimesat" }, - /*PPM_SC_UNLINKAT*/ { EC_FILE, "unlinkat" }, - /*PPM_SC_RENAMEAT*/ { EC_FILE, "renameat" }, - /*PPM_SC_LINKAT*/ { EC_FILE, "linkat" }, - /*PPM_SC_SYMLINKAT*/ { EC_FILE, "symlinkat" }, - /*PPM_SC_READLINKAT*/ { EC_FILE, "readlinkat" }, - /*PPM_SC_FCHMODAT*/ { EC_FILE, "fchmodat" }, - /*PPM_SC_FACCESSAT*/ { EC_FILE, "faccessat" }, - /*PPM_SC_PSELECT6*/ { EC_WAIT, "pselect6" }, - /*PPM_SC_PPOLL*/ { EC_WAIT, "ppoll" }, - /*PPM_SC_UNSHARE*/ { EC_PROCESS, "unshare" }, /* disassociate parts of the process execution context */ - /*PPM_SC_SET_ROBUST_LIST*/ { EC_PROCESS, "set_robust_list" }, /* get/set list of robust futexes */ - /*PPM_SC_GET_ROBUST_LIST*/ { EC_PROCESS, "get_robust_list" }, /* get/set list of robust futexes */ - /*PPM_SC_SPLICE*/ { EC_IPC, "splice" }, /* transfers up to len bytes of data from the file descriptor fd_in to the file descriptor fd_out, where one of the descriptors must refer to a pipe. */ - /*PPM_SC_TEE*/ { EC_IPC, "tee" }, /* tee() duplicates up to len bytes of data from the pipe referred to by the file descriptor fd_in to the pipe referred to by the file descriptor fd_out. It does not consume the data that is duplicated from fd_in. */ - /*PPM_SC_VMSPLICE*/ { EC_IPC, "vmsplice" }, /* splice user pages into a pipe */ - /*PPM_SC_GETCPU*/ { EC_PROCESS, "getcpu" }, /* determine CPU and NUMA node on which the calling thread is running */ - /*PPM_SC_EPOLL_PWAIT*/ { EC_WAIT, "epoll_pwait" }, - /*PPM_SC_UTIMENSAT*/ { EC_FILE, "utimensat" }, /* change file timestamps with nanosecond precision */ - /*PPM_SC_SIGNALFD*/ { EC_SIGNAL, "signalfd" }, /* create a pollable file descriptor for accepting signals */ - /*PPM_SC_TIMERFD_CREATE*/ { EC_TIME, "timerfd_create" }, /* // create and operate on a timer that delivers timer expiration notifications via a file descriptor */ - /*PPM_SC_EVENTFD*/ { EC_IPC, "eventfd" }, /* create a file descriptor for event notification */ - /*PPM_SC_TIMERFD_SETTIME*/ { EC_TIME, "timerfd_settime" }, /* create and operate on a timer that delivers timer expiration notifications via a file descriptor */ - /*PPM_SC_TIMERFD_GETTIME*/ { EC_TIME, "timerfd_gettime" }, /* create and operate on a timer that delivers timer expiration notifications via a file descriptor */ - /*PPM_SC_SIGNALFD4*/ { EC_SIGNAL, "signalfd4" }, /* create a pollable file descriptor for accepting signals */ - /*PPM_SC_EVENTFD2*/ { EC_IPC, "eventfd2" }, /* create a file descriptor for event notification */ - /*PPM_SC_EPOLL_CREATE1*/ { EC_WAIT, "epoll_create1" }, /* variant of epoll_create */ - /*PPM_SC_DUP3*/ { EC_IO_OTHER, "dup3" }, - /*PPM_SC_PIPE2*/ { EC_IPC, "pipe2" }, - /*PPM_SC_INOTIFY_INIT1*/ { EC_IPC, "inotify_init1" }, - /*PPM_SC_PREADV*/ { EC_IO_READ, "preadv" }, - /*PPM_SC_PWRITEV*/ { EC_IO_WRITE, "pwritev" }, - /*PPM_SC_RT_TGSIGQUEUEINFO*/ { EC_OTHER, "rt_tgsigqueueinfo" }, - /*PPM_SC_PERF_EVENT_OPEN*/ { EC_OTHER, "perf_event_open" }, - /*PPM_SC_FANOTIFY_INIT*/ { EC_IPC, "fanotify_init" }, - /*PPM_SC_PRLIMIT64*/ { EC_PROCESS, "prlimit64" }, - /*PPM_SC_CLOCK_ADJTIME*/ { EC_OTHER, "clock_adjtime" }, - /*PPM_SC_SYNCFS*/ { EC_FILE, "syncfs" }, - /*PPM_SC_SETNS*/ { EC_PROCESS, "setns" }, /* reassociate thread with a namespace */ - /*PPM_SC_GETDENTS64*/ { EC_IPC, "getdents64" }, + /*dummy*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "" }, + /*PPM_SC_RESTART_SYSCALL*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "restart_syscall" }, + /*PPM_SC_EXIT*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "exit" }, + /*PPM_SC_READ*/ { EC_IO_READ, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "read" }, + /*PPM_SC_WRITE*/ { EC_IO_WRITE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "write" }, + /*PPM_SC_OPEN*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "open" }, + /*PPM_SC_CLOSE*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "close" }, + /*PPM_SC_CREAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "creat" }, + /*PPM_SC_LINK*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "link" }, + /*PPM_SC_UNLINK*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "unlink" }, + /*PPM_SC_CHDIR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "chdir" }, + /*PPM_SC_TIME*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "time" }, + /*PPM_SC_MKNOD*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "mknod" }, + /*PPM_SC_CHMOD*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "chmod" }, + /*PPM_SC_STAT*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "stat" }, + /*PPM_SC_LSEEK*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "lseek" }, + /*PPM_SC_GETPID*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getpid" }, + /*PPM_SC_MOUNT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "mount" }, + /*PPM_SC_PTRACE*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "ptrace" }, + /*PPM_SC_ALARM*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "alarm" }, + /*PPM_SC_FSTAT*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fstat" }, + /*PPM_SC_PAUSE*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "pause" }, /* WAIT UNTIL A SIGNAL ARRIVES */ + /*PPM_SC_UTIME*/ { EC_TIME, (enum ppm_event_flags)(EF_NONE), "utime" }, + /*PPM_SC_ACCESS*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "access" }, /* checks whether the calling process can access the file pathname */ + /*PPM_SC_SYNC*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "sync" }, /* causes all buffered modifications to file metadata and data to be written to the underlying file systems. */ + /*PPM_SC_KILL*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "kill" }, + /*PPM_SC_RENAME*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "rename" }, + /*PPM_SC_MKDIR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "mkdir" }, + /*PPM_SC_RMDIR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "rmdir" }, + /*PPM_SC_DUP*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "dup" }, + /*PPM_SC_PIPE*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "pipe" }, + /*PPM_SC_TIMES*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "times" }, + /*PPM_SC_BRK*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "brk" }, + /*PPM_SC_ACCT*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "acct" }, + /*PPM_SC_IOCTL*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "ioctl" }, + /*PPM_SC_FCNTL*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fcntl" }, + /*PPM_SC_SETPGID*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "setpgid" }, + /*PPM_SC_UMASK*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "umask" }, /* sets the calling process's file mode creation mask */ + /*PPM_SC_CHROOT*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "chroot" }, /* changes the root directory of the calling process to that specified in path. This directory will be used for path names beginning with /. The root directory is inherited by all children of the calling process. */ + /*PPM_SC_USTAT*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "ustat" }, /* returns information about a mounted file system. */ + /*PPM_SC_DUP2*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "dup2" }, + /*PPM_SC_GETPPID*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getppid" }, + /*PPM_SC_GETPGRP*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getpgrp" }, + /*PPM_SC_SETSID*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "setsid" }, /* creates a session and sets the process group ID */ + /*PPM_SC_SETHOSTNAME*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "sethostname" }, + /*PPM_SC_SETRLIMIT*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "setrlimit" }, /* get/set resource (CPU, FDs, memory...) limits */ + /*PPM_SC_GETRUSAGE*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getrusage" }, /* returns resource usage measures for who */ + /*PPM_SC_GETTIMEOFDAY*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "gettimeofday" }, + /*PPM_SC_SETTIMEOFDAY*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "settimeofday" }, + /*PPM_SC_SYMLINK*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "symlink" }, + /*PPM_SC_LSTAT*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "lstat" }, + /*PPM_SC_READLINK*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "readlink" }, + /*PPM_SC_USELIB*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "uselib" }, /* load shared library */ + /*PPM_SC_SWAPON*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "swapon" }, /* start/stop swapping to file/device */ + /*PPM_SC_REBOOT*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "reboot" }, + /*PPM_SC_MMAP*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mmap" }, + /*PPM_SC_MUNMAP*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "munmap" }, + /*PPM_SC_TRUNCATE*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "truncate" }, /* truncate a file to a specified length */ + /*PPM_SC_FTRUNCATE*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "ftruncate" }, /* truncate a file to a specified length */ + /*PPM_SC_FCHMOD*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "fchmod" }, + /*PPM_SC_GETPRIORITY*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getpriority" }, /* get/set program scheduling priority */ + /*PPM_SC_SETPRIORITY*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "setpriority" }, /* get/set program scheduling priority */ + /*PPM_SC_STATFS*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "statfs" }, /* returns information about a mounted file system */ + /*PPM_SC_FSTATFS*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fstatfs" }, /* returns information about a mounted file system */ + /*PPM_SC_SYSLOG*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "syslog" }, /* read and/or clear kernel message ring buffer; set console_loglevel */ + /*PPM_SC_SETITIMER*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "setitimer" }, + /*PPM_SC_GETITIMER*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getitimer" }, + /*PPM_SC_UNAME*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "uname" }, /* get name and information about current kernel */ + /*PPM_SC_VHANGUP*/ { EC_OTHER , (enum ppm_event_flags)(EF_NONE), "vhangup" }, /* simulates a hangup on the current terminal. This call arranges for other users to have a "clean" terminal at login time. */ + /*PPM_SC_WAIT4*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "wait4" }, /* OBSOLETE */ + /*PPM_SC_SWAPOFF*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "swapoff" }, /* start/stop swapping to file/device */ + /*PPM_SC_SYSINFO*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "sysinfo" }, /* returns information on overall system statistics */ + /*PPM_SC_FSYNC*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fsync" }, /* sync file content */ + /*PPM_SC_SETDOMAINNAME*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "setdomainname" }, + /*PPM_SC_ADJTIMEX*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "adjtimex" }, /* tune kernel clock */ + /*PPM_SC_MPROTECT*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mprotect" }, /* set protection on a region of memory */ + /*PPM_SC_INIT_MODULE*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "init_module" }, /* load a kernel module */ + /*PPM_SC_DELETE_MODULE*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "delete_module" }, + /*PPM_SC_QUOTACTL*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "quotactl" }, + /*PPM_SC_GETPGID*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getpgid" }, + /*PPM_SC_FCHDIR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "fchdir" }, + /*PPM_SC_SYSFS*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "sysfs" }, /* get file system type information */ + /*PPM_SC_PERSONALITY*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "personality" }, /* set the process execution domain */ + /*PPM_SC_GETDENTS*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getdents" }, /* get directory entries */ + /*PPM_SC_SELECT*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "select" }, + /*PPM_SC_FLOCK*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "flock" }, /* apply or remove an advisory lock on an open file */ + /*PPM_SC_MSYNC*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "msync" }, /* synchronize a file with a memory map */ + /*PPM_SC_READV*/ { EC_IO_READ, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "readv" }, + /*PPM_SC_WRITEV*/ { EC_IO_WRITE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "writev" }, + /*PPM_SC_GETSID*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getsid" }, /* returns the session ID of the calling process */ + /*PPM_SC_FDATASYNC*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fdatasync" }, /* synchronize a file's in-core state with storage device */ + /*PPM_SC_MLOCK*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mlock" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ + /*PPM_SC_MUNLOCK*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "munlock" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ + /*PPM_SC_MLOCKALL*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mlockall" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ + /*PPM_SC_MUNLOCKALL*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "munlockall" }, /* mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM */ + /*PPM_SC_SCHED_SETPARAM*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "sched_setparam" }, + /*PPM_SC_SCHED_GETPARAM*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sched_getparam" }, + /*PPM_SC_SCHED_SETSCHEDULER*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "sched_setscheduler" }, + /*PPM_SC_SCHED_GETSCHEDULER*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sched_getscheduler" }, + /*PPM_SC_SCHED_YIELD*/ { EC_SLEEP, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sched_yield" }, + /*PPM_SC_SCHED_GET_PRIORITY_MAX*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sched_get_priority_max" }, + /*PPM_SC_SCHED_GET_PRIORITY_MIN*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sched_get_priority_min" }, + /*PPM_SC_SCHED_RR_GET_INTERVAL*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "sched_rr_get_interval" }, + /*PPM_SC_NANOSLEEP*/ { EC_SLEEP, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "nanosleep" }, + /*PPM_SC_MREMAP*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mremap" }, + /*PPM_SC_POLL*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "poll" }, + /*PPM_SC_PRCTL*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "prctl" }, /* operations on a process */ + /*PPM_SC_RT_SIGACTION*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "rt_sigaction" }, + /*PPM_SC_RT_SIGPROCMASK*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "rt_sigprocmask" }, + /*PPM_SC_RT_SIGPENDING*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "rt_sigpending" }, + /*PPM_SC_RT_SIGTIMEDWAIT*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "rt_sigtimedwait" }, + /*PPM_SC_RT_SIGQUEUEINFO*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_NONE), "rt_sigqueueinfo" }, + /*PPM_SC_RT_SIGSUSPEND*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "rt_sigsuspend" }, + /*PPM_SC_GETCWD*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getcwd" }, + /*PPM_SC_CAPGET*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "capget" }, /* set/get capabilities of thread(s) */ + /*PPM_SC_CAPSET*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "capset" }, /* set/get capabilities of thread(s) */ + /*PPM_SC_SENDFILE*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sendfile" }, /* transfer data between file descriptors */ + /*PPM_SC_GETRLIMIT*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getrlimit" }, + /*PPM_SC_LCHOWN*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "lchown" }, + /*PPM_SC_GETUID*/ { EC_USER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getuid" }, + /*PPM_SC_GETGID*/ { EC_USER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getgid" }, + /*PPM_SC_GETEUID*/ { EC_USER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "geteuid" }, + /*PPM_SC_GETEGID*/ { EC_USER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getegid" }, + /*PPM_SC_SETREUID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setreuid" }, + /*PPM_SC_SETREGID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setregid" }, + /*PPM_SC_GETGROUPS*/ { EC_USER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getgroups" }, /* returns the supplementary group IDs of the calling process */ + /*PPM_SC_SETGROUPS*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setgroups" }, /* returns the supplementary group IDs of the calling process */ + /*PPM_SC_FCHOWN*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "fchown" }, + /*PPM_SC_SETRESUID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setresuid" }, + /*PPM_SC_GETRESUID*/ { EC_USER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getresuid" }, + /*PPM_SC_SETRESGID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setresgid" }, + /*PPM_SC_GETRESGID*/ { EC_USER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getresgid" }, + /*PPM_SC_CHOWN*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "chown" }, + /*PPM_SC_SETUID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setuid" }, + /*PPM_SC_SETGID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setgid" }, + /*PPM_SC_SETFSUID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setfsuid" }, + /*PPM_SC_SETFSGID*/ { EC_USER, (enum ppm_event_flags)(EF_NONE), "setfsgid" }, + /*PPM_SC_PIVOT_ROOT*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "pivot_root" }, + /*PPM_SC_MINCORE*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mincore" }, /* determine whether pages are resident in memory */ + /*PPM_SC_MADVISE*/ { EC_MEMORY, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "madvise" }, /* give advice about use of memory */ + /*PPM_SC_GETTID*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "gettid" }, /* returns the caller's thread ID (TID) */ + /*PPM_SC_SETXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "setxattr" }, /* set inode attribute */ + /*PPM_SC_LSETXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "lsetxattr" }, + /*PPM_SC_FSETXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "fsetxattr" }, + /*PPM_SC_GETXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getxattr" }, + /*PPM_SC_LGETXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "lgetxattr" }, + /*PPM_SC_FGETXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fgetxattr" }, + /*PPM_SC_LISTXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "listxattr" }, + /*PPM_SC_LLISTXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "llistxattr" }, + /*PPM_SC_FLISTXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "flistxattr" }, + /*PPM_SC_REMOVEXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "removexattr" }, + /*PPM_SC_LREMOVEXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "lremovexattr" }, + /*PPM_SC_FREMOVEXATTR*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "fremovexattr" }, + /*PPM_SC_TKILL*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_NONE), "tkill" }, /* send a signal to a thread */ + /*PPM_SC_FUTEX*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "futex" }, + /*PPM_SC_SCHED_SETAFFINITY*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "sched_setaffinity" }, + /*PPM_SC_SCHED_GETAFFINITY*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sched_getaffinity" }, + /*PPM_SC_SET_THREAD_AREA*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "set_thread_area" }, + /*PPM_SC_GET_THREAD_AREA*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "get_thread_area" }, + /*PPM_SC_IO_SETUP*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "io_setup" }, /* create an asynchronous I/O context (for libaio) */ + /*PPM_SC_IO_DESTROY*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "io_destroy" }, + /*PPM_SC_IO_GETEVENTS*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "io_getevents" }, + /*PPM_SC_IO_SUBMIT*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "io_submit" }, + /*PPM_SC_IO_CANCEL*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "io_cancel" }, + /*PPM_SC_EXIT_GROUP*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "exit_group" }, + /*PPM_SC_EPOLL_CREATE*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "epoll_create" }, + /*PPM_SC_EPOLL_CTL*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "epoll_ctl" }, + /*PPM_SC_EPOLL_WAIT*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "epoll_wait" }, + /*PPM_SC_REMAP_FILE_PAGES*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "remap_file_pages" }, /* create a nonlinear file mapping */ + /*PPM_SC_SET_TID_ADDRESS*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "set_tid_address" }, /* set pointer to thread ID */ + /*PPM_SC_TIMER_CREATE*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timer_create" }, + /*PPM_SC_TIMER_SETTIME*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timer_settime" }, + /*PPM_SC_TIMER_GETTIME*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timer_gettime" }, + /*PPM_SC_TIMER_GETOVERRUN*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timer_getoverrun" }, + /*PPM_SC_TIMER_DELETE*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timer_delete" }, + /*PPM_SC_CLOCK_SETTIME*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "clock_settime" }, + /*PPM_SC_CLOCK_GETTIME*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "clock_gettime" }, + /*PPM_SC_CLOCK_GETRES*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "clock_getres" }, + /*PPM_SC_CLOCK_NANOSLEEP*/ { EC_SLEEP, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "clock_nanosleep" }, + /*PPM_SC_TGKILL*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_NONE), "tgkill" }, + /*PPM_SC_UTIMES*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "utimes" }, /* change file last access and modification times */ + /*PPM_SC_MQ_OPEN*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "mq_open" }, /* Message queues. See http://linux.die.net/man/7/mq_overview. */ + /*PPM_SC_MQ_UNLINK*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "mq_unlink" }, + /*PPM_SC_MQ_TIMEDSEND*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mq_timedsend" }, + /*PPM_SC_MQ_TIMEDRECEIVE*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mq_timedreceive" }, + /*PPM_SC_MQ_NOTIFY*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mq_notify" }, + /*PPM_SC_MQ_GETSETATTR*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mq_getsetattr" }, + /*PPM_SC_KEXEC_LOAD*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "kexec_load" }, /* load a new kernel for later execution */ + /*PPM_SC_WAITID*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "waitid" }, + /*PPM_SC_ADD_KEY*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "add_key" }, /* add a key to the kernel's key management facility */ + /*PPM_SC_REQUEST_KEY*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "request_key" }, + /*PPM_SC_KEYCTL*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "keyctl" }, + /*PPM_SC_IOPRIO_SET*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "ioprio_set" }, /* get/set I/O scheduling class and priority */ + /*PPM_SC_IOPRIO_GET*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "ioprio_get" }, /* get/set I/O scheduling class and priority */ + /*PPM_SC_INOTIFY_INIT*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "inotify_init" }, /* initialize an inotify event queue instance. See http://en.wikipedia.org/wiki/Inotify. */ + /*PPM_SC_INOTIFY_ADD_WATCH*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "inotify_add_watch" }, + /*PPM_SC_INOTIFY_RM_WATCH*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "inotify_rm_watch" }, + /*PPM_SC_OPENAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "openat" }, + /*PPM_SC_MKDIRAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "mkdirat" }, + /*PPM_SC_MKNODAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "mknodat" }, + /*PPM_SC_FCHOWNAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "fchownat" }, + /*PPM_SC_FUTIMESAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "futimesat" }, + /*PPM_SC_UNLINKAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "unlinkat" }, + /*PPM_SC_RENAMEAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "renameat" }, + /*PPM_SC_LINKAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "linkat" }, + /*PPM_SC_SYMLINKAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "symlinkat" }, + /*PPM_SC_READLINKAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "readlinkat" }, + /*PPM_SC_FCHMODAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "fchmodat" }, + /*PPM_SC_FACCESSAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "faccessat" }, + /*PPM_SC_PSELECT6*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "pselect6" }, + /*PPM_SC_PPOLL*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "ppoll" }, + /*PPM_SC_UNSHARE*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "unshare" }, /* disassociate parts of the process execution context */ + /*PPM_SC_SET_ROBUST_LIST*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "set_robust_list" }, /* get/set list of robust futexes */ + /*PPM_SC_GET_ROBUST_LIST*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "get_robust_list" }, /* get/set list of robust futexes */ + /*PPM_SC_SPLICE*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "splice" }, /* transfers up to len bytes of data from the file descriptor fd_in to the file descriptor fd_out, where one of the descriptors must refer to a pipe. */ + /*PPM_SC_TEE*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "tee" }, /* tee() duplicates up to len bytes of data from the pipe referred to by the file descriptor fd_in to the pipe referred to by the file descriptor fd_out. It does not consume the data that is duplicated from fd_in. */ + /*PPM_SC_VMSPLICE*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "vmsplice" }, /* splice user pages into a pipe */ + /*PPM_SC_GETCPU*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getcpu" }, /* determine CPU and NUMA node on which the calling thread is running */ + /*PPM_SC_EPOLL_PWAIT*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "epoll_pwait" }, + /*PPM_SC_UTIMENSAT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "utimensat" }, /* change file timestamps with nanosecond precision */ + /*PPM_SC_SIGNALFD*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "signalfd" }, /* create a pollable file descriptor for accepting signals */ + /*PPM_SC_TIMERFD_CREATE*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timerfd_create" }, /* // create and operate on a timer that delivers timer expiration notifications via a file descriptor */ + /*PPM_SC_EVENTFD*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "eventfd" }, /* create a file descriptor for event notification */ + /*PPM_SC_TIMERFD_SETTIME*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timerfd_settime" }, /* create and operate on a timer that delivers timer expiration notifications via a file descriptor */ + /*PPM_SC_TIMERFD_GETTIME*/ { EC_TIME, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "timerfd_gettime" }, /* create and operate on a timer that delivers timer expiration notifications via a file descriptor */ + /*PPM_SC_SIGNALFD4*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "signalfd4" }, /* create a pollable file descriptor for accepting signals */ + /*PPM_SC_EVENTFD2*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "eventfd2" }, /* create a file descriptor for event notification */ + /*PPM_SC_EPOLL_CREATE1*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "epoll_create1" }, /* variant of epoll_create */ + /*PPM_SC_DUP3*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "dup3" }, + /*PPM_SC_PIPE2*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "pipe2" }, + /*PPM_SC_INOTIFY_INIT1*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "inotify_init1" }, + /*PPM_SC_PREADV*/ { EC_IO_READ, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "preadv" }, + /*PPM_SC_PWRITEV*/ { EC_IO_WRITE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "pwritev" }, + /*PPM_SC_RT_TGSIGQUEUEINFO*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "rt_tgsigqueueinfo" }, + /*PPM_SC_PERF_EVENT_OPEN*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "perf_event_open" }, + /*PPM_SC_FANOTIFY_INIT*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "fanotify_init" }, + /*PPM_SC_PRLIMIT64*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "prlimit64" }, + /*PPM_SC_CLOCK_ADJTIME*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "clock_adjtime" }, + /*PPM_SC_SYNCFS*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "syncfs" }, + /*PPM_SC_SETNS*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "setns" }, /* reassociate thread with a namespace */ + /*PPM_SC_GETDENTS64*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getdents64" }, /* */ /* Non-multiplexed socket family */ /* */ - /*PPM_SC_SOCKET*/ { EC_NET, "socket" }, - /*PPM_SC_BIND*/ { EC_NET, "bind" }, - /*PPM_SC_CONNECT*/ { EC_NET, "connect" }, - /*PPM_SC_LISTEN*/ { EC_NET, "listen" }, - /*PPM_SC_ACCEPT*/ { EC_NET, "accept" }, - /*PPM_SC_GETSOCKNAME*/ { EC_NET, "getsockname" }, - /*PPM_SC_GETPEERNAME*/ { EC_NET, "getpeername" }, - /*PPM_SC_SOCKETPAIR*/ { EC_NET, "socketpair" }, - /*PPM_SC_SENDTO*/ { EC_NET, "sendto" }, - /*PPM_SC_RECVFROM*/ { EC_NET, "recvfrom" }, - /*PPM_SC_SHUTDOWN*/ { EC_NET, "shutdown" }, - /*PPM_SC_SETSOCKOPT*/ { EC_NET, "setsockopt" }, - /*PPM_SC_GETSOCKOPT*/ { EC_NET, "getsockopt" }, - /*PPM_SC_SENDMSG*/ { EC_NET, "sendmsg" }, - /*PPM_SC_SENDMMSG*/ { EC_NET, "sendmmsg" }, - /*PPM_SC_RECVMSG*/ { EC_NET, "recvmsg" }, - /*PPM_SC_RECVMMSG*/ { EC_NET, "recvmmsg" }, - /*PPM_SC_ACCEPT4*/ { EC_NET, "accept4" }, + /*PPM_SC_SOCKET*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "socket" }, + /*PPM_SC_BIND*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "bind" }, + /*PPM_SC_CONNECT*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "connect" }, + /*PPM_SC_LISTEN*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "listen" }, + /*PPM_SC_ACCEPT*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "accept" }, + /*PPM_SC_GETSOCKNAME*/ { EC_NET, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getsockname" }, + /*PPM_SC_GETPEERNAME*/ { EC_NET, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getpeername" }, + /*PPM_SC_SOCKETPAIR*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "socketpair" }, + /*PPM_SC_SENDTO*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "sendto" }, + /*PPM_SC_RECVFROM*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "recvfrom" }, + /*PPM_SC_SHUTDOWN*/ { EC_NET, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "shutdown" }, + /*PPM_SC_SETSOCKOPT*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "setsockopt" }, + /*PPM_SC_GETSOCKOPT*/ { EC_NET, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getsockopt" }, + /*PPM_SC_SENDMSG*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "sendmsg" }, + /*PPM_SC_SENDMMSG*/ { EC_NET, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sendmmsg" }, + /*PPM_SC_RECVMSG*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "recvmsg" }, + /*PPM_SC_RECVMMSG*/ { EC_NET, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "recvmmsg" }, + /*PPM_SC_ACCEPT4*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "accept4" }, /* * Non-multiplexed IPC family */ - /*PPM_SC_SEMOP*/ { EC_IPC, "semop" }, - /*PPM_SC_SEMGET*/ { EC_IPC, "semget" }, - /*PPM_SC_SEMCTL*/ { EC_IPC, "semctl" }, - /*PPM_SC_MSGSND*/ { EC_IPC, "msgsnd" }, - /*PPM_SC_MSGRCV*/ { EC_IPC, "msgrcv" }, - /*PPM_SC_MSGGET*/ { EC_IPC, "msgget" }, - /*PPM_SC_MSGCTL*/ { EC_IPC, "msgctl" }, - /*PPM_SC_SHMDT*/ { EC_IPC, "shmdt" }, - /*PPM_SC_SHMGET*/ { EC_IPC, "shmget" }, - /*PPM_SC_SHMCTL*/ { EC_IPC, "shmctl" }, - /*PPM_SC_STATFS64*/ { EC_FILE, "statfs64" }, - /*PPM_SC_FSTATFS64*/ { EC_FILE, "fstatfs64" }, - /*PPM_SC_FSTATAT64*/ { EC_FILE, "fstatat64" }, - /*PPM_SC_SENDFILE64*/ { EC_FILE, "sendfile64" }, - /*PPM_SC_UGETRLIMIT*/ { EC_PROCESS, "ugetrlimit" }, - /*PPM_SC_BDFLUSH*/ { EC_OTHER, "bdflush" }, /* deprecated */ - /*PPM_SC_SIGPROCMASK*/ { EC_SIGNAL, "sigprocmask" }, /* examine and change blocked signals */ - /*PPM_SC_IPC*/ { EC_IPC, "ipc" }, - /*PPM_SC_SOCKETCALL*/ { EC_NET, "socketcall" }, - /*PPM_SC_STAT64*/ { EC_FILE, "stat64" }, - /*PPM_SC_LSTAT64*/ { EC_FILE, "lstat64" }, - /*PPM_SC_FSTAT64*/ { EC_FILE, "fstat64" }, - /*PPM_SC_FCNTL64*/ { EC_FILE, "fcntl64" }, - /*PPM_SC_MMAP2*/ { EC_FILE, "mmap2" }, - /*PPM_SC__NEWSELECT*/ { EC_WAIT, "newselect" }, - /*PPM_SC_SGETMASK*/ { EC_SIGNAL, "sgetmask" }, /* manipulation of signal mask (obsolete) */ - /*PPM_SC_SSETMASK*/ { EC_SIGNAL, "ssetmask" }, /* manipulation of signal mask (obsolete) */ - /*PPM_SC_SIGPENDING*/ { EC_SIGNAL, "sigpending" }, /* examine pending signals */ - /*PPM_SC_OLDUNAME*/ { EC_SYSTEM, "olduname" }, - /*PPM_SC_UMOUNT*/ { EC_FILE, "umount" }, - /*PPM_SC_SIGNAL*/ { EC_SIGNAL, "signal" }, - /*PPM_SC_NICE*/ { EC_PROCESS, "nice" }, /* change process priority */ - /*PPM_SC_STIME*/ { EC_TIME, "stime" }, - /*PPM_SC__LLSEEK*/ { EC_FILE, "llseek" }, - /*PPM_SC_WAITPID*/ { EC_WAIT, "waitpid" }, - /*PPM_SC_PREAD64*/ { EC_FILE, "pread64" }, - /*PPM_SC_PWRITE64*/ { EC_FILE, "pwrite64" }, + /*PPM_SC_SEMOP*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "semop" }, + /*PPM_SC_SEMGET*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "semget" }, + /*PPM_SC_SEMCTL*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "semctl" }, + /*PPM_SC_MSGSND*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "msgsnd" }, + /*PPM_SC_MSGRCV*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "msgrcv" }, + /*PPM_SC_MSGGET*/ { EC_IPC, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "msgget" }, + /*PPM_SC_MSGCTL*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "msgctl" }, + /*PPM_SC_SHMDT*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "shmdt" }, + /*PPM_SC_SHMGET*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "shmget" }, + /*PPM_SC_SHMCTL*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "shmctl" }, + /*PPM_SC_STATFS64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "statfs64" }, + /*PPM_SC_FSTATFS64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fstatfs64" }, + /*PPM_SC_FSTATAT64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fstatat64" }, + /*PPM_SC_SENDFILE64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sendfile64" }, + /*PPM_SC_UGETRLIMIT*/ { EC_PROCESS, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "ugetrlimit" }, + /*PPM_SC_BDFLUSH*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "bdflush" }, /* deprecated */ + /*PPM_SC_SIGPROCMASK*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sigprocmask" }, /* examine and change blocked signals */ + /*PPM_SC_IPC*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "ipc" }, + /*PPM_SC_SOCKETCALL*/ { EC_NET, (enum ppm_event_flags)(EF_NONE), "socketcall" }, + /*PPM_SC_STAT64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "stat64" }, + /*PPM_SC_LSTAT64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "lstat64" }, + /*PPM_SC_FSTAT64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fstat64" }, + /*PPM_SC_FCNTL64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "fcntl64" }, + /*PPM_SC_MMAP2*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "mmap2" }, + /*PPM_SC__NEWSELECT*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "newselect" }, + /*PPM_SC_SGETMASK*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sgetmask" }, /* manipulation of signal mask (obsolete) */ + /*PPM_SC_SSETMASK*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_NONE), "ssetmask" }, /* manipulation of signal mask (obsolete) */ + /*PPM_SC_SIGPENDING*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sigpending" }, /* examine pending signals */ + /*PPM_SC_OLDUNAME*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "olduname" }, + /*PPM_SC_UMOUNT*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "umount" }, + /*PPM_SC_SIGNAL*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_NONE), "signal" }, + /*PPM_SC_NICE*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "nice" }, /* change process priority */ + /*PPM_SC_STIME*/ { EC_TIME, (enum ppm_event_flags)(EF_NONE), "stime" }, + /*PPM_SC__LLSEEK*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "llseek" }, + /*PPM_SC_WAITPID*/ { EC_WAIT, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "waitpid" }, + /*PPM_SC_PREAD64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "pread64" }, + /*PPM_SC_PWRITE64*/ { EC_FILE, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "pwrite64" }, + /*PPM_SC_ARCH_PRCTL*/ { EC_PROCESS, (enum ppm_event_flags)(EF_NONE), "arch_prctl" }, + /*PPM_SC_SHMAT*/ { EC_IPC, (enum ppm_event_flags)(EF_NONE), "shmat" }, + /*PPM_SC_SIGRETURN*/ { EC_SIGNAL, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "sigreturn" }, /* return from signal handler and cleanup stack frame */ + /*PPM_SC_FALLOCATE*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "fallocate" }, /* manipulate file space */ + /*PPM_SC_NEWFSSTAT*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "newfstatat" }, + /*PPM_SC_PROCESS_VM_READV*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "process_vm_readv" }, + /*PPM_SC_PROCESS_VM_WRITEV*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "process_vm_writev" }, + /*PPM_SC_FORK*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "fork" }, + /*PPM_SC_VFORK*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "vfork" }, + /*PPM_SC_SETUID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "setuid" }, + /*PPM_SC_GETUID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getuid" }, + /*PPM_SC_SETGID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "setgid" }, + /*PPM_SC_GETEUID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "geteuid" }, + /*PPM_SC_GETGID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getgid" }, + /*PPM_SC_SETRESUID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "setresuid" }, + /*PPM_SC_SETRESGID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "setresgid" }, + /*PPM_SC_GETRESUID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getresuid" }, + /*PPM_SC_GETRESGID32*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_DROP_SIMPLE_CONS), "getresgid" }, + /*PPM_SC_FINIT_MODULE*/ { EC_SYSTEM, (enum ppm_event_flags)(EF_NONE), "finit_module" }, /* load a kernel module */ + /*PPM_SC_BPF*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "bpf" }, + /*PPM_SC_SECCOMP*/ { EC_OTHER, (enum ppm_event_flags)(EF_NONE), "seccomp" }, + /*PPM_SC_SIGALTSTACK*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "sigaltstack" }, + /*PPM_SC_GETRANDOM*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "getrandom" }, + /*PPM_SC_FADVISE64*/ { EC_IO_OTHER, (enum ppm_event_flags)(EF_NONE), "fadvise64" }, + /*PPM_SC_RENAMEAT2*/ { EC_FILE, (enum ppm_event_flags)(EF_NONE), "renameat2" }, }; + +bool validate_info_table_size() +{ + return (sizeof(g_syscall_info_table) / sizeof(g_syscall_info_table[0]) == PPM_SC_MAX); +} diff --git a/userspace/libscap/uthash.h b/userspace/libscap/uthash.h index 1deb099a33..c767c42137 100644 --- a/userspace/libscap/uthash.h +++ b/userspace/libscap/uthash.h @@ -193,12 +193,12 @@ do { * "the usual" patch-up process for the app-order doubly-linked-list. * The use of _hd_hh_del below deserves special explanation. * These used to be expressed using (delptr) but that led to a bug - * if someone used the same symbol for the head and deletee, like + * if someone used the same symbol for the head and deleted, like * HASH_DELETE(hh,users,users); * We want that to work, but by changing the head (users) below - * we were forfeiting our ability to further refer to the deletee (users) + * we were forfeiting our ability to further refer to the deleted (users) * in the patch-up process. Solution: use scratch space to - * copy the deletee pointer, then the latter references are via that + * copy the deleted pointer, then the latter references are via that * scratch pointer rather than through the repointed (users) symbol. */ #define HASH_DELETE(hh,head,delptr) \ diff --git a/userspace/libscap/windows_hal.c b/userspace/libscap/windows_hal.c new file mode 100644 index 0000000000..15f1f572e2 --- /dev/null +++ b/userspace/libscap/windows_hal.c @@ -0,0 +1,148 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include +#include +#define DRAGENT_WIN_HAL_C_ONLY +#include + +#include "scap.h" +#include "scap-int.h" +#include "windows_hal.h" + +static int32_t addprocess_windows(wh_procinfo* wpi, scap_t* handle, char *error) +{ + struct scap_threadinfo* tinfo; + + // + // Allocate the procinfo object. + // + if((tinfo = scap_proc_alloc(handle)) == NULL) + { + snprintf(error, SCAP_LASTERR_SIZE, "addprocess_windows memory allocation error"); + return SCAP_FAILURE; + } + + // + // Fill the procinfo object + // + memset(tinfo, 0, sizeof(struct scap_threadinfo)); + + tinfo->pid = wpi->pid; + tinfo->tid = wpi->tid; + tinfo->ptid = wpi->ptid; + snprintf(tinfo->comm, SCAP_MAX_PATH_SIZE, "%s", wpi->comm); + snprintf(tinfo->exe, SCAP_MAX_PATH_SIZE, "%s", wpi->exe); + snprintf(tinfo->exepath, SCAP_MAX_PATH_SIZE, "%s", wpi->exepath); + snprintf(tinfo->args, SCAP_MAX_PATH_SIZE, "%s", wpi->args); + tinfo->args_len = wpi->args_len; + tinfo->vmsize_kb = wpi->vmsize_kb; + tinfo->pfmajor = wpi->pfmajor; + tinfo->pfminor = wpi->pfminor; + tinfo->clone_ts = wpi->clone_ts; + tinfo->tty = wpi->tty; + + wh_proc_perf_info pinfo = wh_wmi_get_proc_perf_info(handle->m_whh, tinfo->pid); + if(pinfo.m_result != 0) + { + tinfo->vmrss_kb = pinfo.m_memory_bytes / 1024; + tinfo->vmswap_kb = pinfo.m_swap_bytes / 1024; + } + else + { + tinfo->vmrss_kb = 0; + tinfo->vmswap_kb = 0; + } + + // + // Done. Add the entry to the process table, or fire the notification callback + // + if(handle->m_proc_callback == NULL) + { + snprintf(error, SCAP_LASTERR_SIZE, "process table construction in scap not supported on windows"); + return SCAP_FAILURE; + + // int32_t uth_status = SCAP_SUCCESS; + + // HASH_ADD_INT64(handle->m_proclist, pid, tinfo); + // if(uth_status != SCAP_SUCCESS) + // { + // snprintf(error, SCAP_LASTERR_SIZE, "process table allocation error (2)"); + // return SCAP_FAILURE; + // } + } + else + { + handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo->tid, tinfo, NULL); + free(tinfo); + } + + return SCAP_SUCCESS; +} + +typedef int (CALLBACK* LPFNDLLFUNC1)(); + +int32_t scap_proc_scan_proc_dir_windows(scap_t* handle, char *error) +{ + wh_proclist wgpres; + + // + // Get the system processes through WMI + // + wgpres = wh_wmi_get_procs(handle->m_whh); + if(wgpres.m_result == 0) + { + snprintf(error, SCAP_LASTERR_SIZE, "%s", wh_getlasterror(handle->m_whh)); + return SCAP_FAILURE; + } + + // + // While we're here, refresh the docker state and the process performance table + // + if(wh_is_docker_present(handle->m_whh)) + { + if(wh_docker_refresh(handle->m_whh) == 0) + { + snprintf(error, SCAP_LASTERR_SIZE, "%s", wh_getlasterror(handle->m_whh)); + return SCAP_FAILURE; + } + } + + if(wh_wmi_update_procs_perf(handle->m_whh) == 0) + { + snprintf(error, SCAP_LASTERR_SIZE, "%s", wh_getlasterror(handle->m_whh)); + return SCAP_FAILURE; + } + + // + // Add the received processes to the scap list + // + for(uint32_t j = 0; j < wgpres.m_count; j++) + { + wh_procinfo* wpi = &(wgpres.m_procs[j]); + + if(addprocess_windows(wpi, handle, error) != SCAP_SUCCESS) + { + return SCAP_FAILURE; + } + } + + return SCAP_SUCCESS; +} diff --git a/userspace/libscap/windows_hal.h b/userspace/libscap/windows_hal.h new file mode 100644 index 0000000000..8268b9a169 --- /dev/null +++ b/userspace/libscap/windows_hal.h @@ -0,0 +1,21 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +int32_t scap_proc_scan_proc_dir_windows(scap_t* handle, char *error); diff --git a/userspace/libsinsp/CMakeLists.txt b/userspace/libsinsp/CMakeLists.txt index 464514b062..d72f9115f2 100644 --- a/userspace/libsinsp/CMakeLists.txt +++ b/userspace/libsinsp/CMakeLists.txt @@ -1,36 +1,253 @@ -include_directories(./) -include_directories(../../common) -include_directories(../libscap) -include_directories(third-party/jsoncpp) -include_directories(third-party/jsoncpp) -include_directories(${PROJECT_SOURCE_DIR}/third-party/LuaJIT-2.0.2/src) - -add_library(sinsp STATIC - chisel.cpp - event.cpp - eventformatter.cpp - dumper.cpp - fdinfo.cpp - filter.cpp - filterchecks.cpp - ifinfo.cpp - internal_metrics.cpp - third-party/jsoncpp/jsoncpp.cpp - logger.cpp - parsers.cpp - threadinfo.cpp - sinsp.cpp - stats.cpp - utils.cpp) - -target_link_libraries(sinsp - scap) - -if(NOT WIN32) - target_link_libraries(sinsp - ${PROJECT_SOURCE_DIR}/third-party/LuaJIT-2.0.2/src/libluajit.a - dl) -else() - target_link_libraries(sinsp - ${PROJECT_SOURCE_DIR}/third-party/LuaJIT-2.0.2/src/lua51.lib) -endif() \ No newline at end of file +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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_directories(./) +include_directories(../../common) +include_directories(../libscap) +include_directories(../async) +include_directories(./include) +include_directories("${JSONCPP_INCLUDE}") +include_directories("${LUAJIT_INCLUDE}") + +if(NOT MINIMAL_BUILD) + include_directories("${CARES_INCLUDE}") +endif() + +if(NOT WIN32 AND NOT APPLE) + include_directories("${B64_INCLUDE}") + include_directories("${CURSES_INCLUDE_DIR}") + if(NOT MINIMAL_BUILD) + include_directories("${GRPC_INCLUDE}") + include_directories("${PROTOBUF_INCLUDE}") + include_directories("${OPENSSL_INCLUDE_DIR}") + include_directories("${CURL_INCLUDE_DIR}") + endif() # NOT MINIMAL_BUILD + include_directories("${JQ_INCLUDE}") + include_directories("${CMAKE_CURRENT_BINARY_DIR}") +endif() + +if(NOT WIN32) + include_directories("${TBB_INCLUDE_DIR}") +endif() # NOT WIN32 + +set(SINSP_SOURCES + chisel.cpp + chisel_api.cpp + container.cpp + container_engine/container_engine_base.cpp + container_info.cpp + ctext.cpp + cyclewriter.cpp + cursescomponents.cpp + cursestable.cpp + cursesspectro.cpp + cursesui.cpp + event.cpp + eventformatter.cpp + dns_manager.cpp + dumper.cpp + fdinfo.cpp + filter.cpp + filterchecks.cpp + gen_filter.cpp + http_parser.c + http_reason.cpp + ifinfo.cpp + json_query.cpp + json_error_log.cpp + lua_parser.cpp + lua_parser_api.cpp + memmem.cpp + tracers.cpp + internal_metrics.cpp + "${JSONCPP_LIB_SRC}" + logger.cpp + parsers.cpp + prefix_search.cpp + protodecoder.cpp + threadinfo.cpp + tuples.cpp + sinsp.cpp + stats.cpp + table.cpp + token_bucket.cpp + stopwatch.cpp + uri_parser.c + uri.cpp + user_event_logger.cpp + utils.cpp + user_event.cpp + value_parser.cpp + viewinfo.cpp +) + +if(NOT MINIMAL_BUILD) + list(APPEND SINSP_SOURCES + addrinfo.cpp + sinsp_auth.cpp + k8s.cpp + k8s_api_error.cpp + k8s_api_handler.cpp + k8s_component.cpp + k8s_daemonset_handler.cpp + k8s_deployment_handler.cpp + k8s_dispatcher.cpp + k8s_event_data.cpp + k8s_event_handler.cpp + k8s_handler.cpp + k8s_namespace_handler.cpp + k8s_net.cpp + k8s_node_handler.cpp + k8s_pod_handler.cpp + k8s_replicationcontroller_handler.cpp + k8s_replicaset_handler.cpp + k8s_service_handler.cpp + k8s_state.cpp + marathon_component.cpp + marathon_http.cpp + mesos_auth.cpp + mesos.cpp + mesos_collector.cpp + mesos_component.cpp + mesos_http.cpp + mesos_state.cpp + sinsp_curl.cpp + container_engine/docker_common.cpp) + if(WIN32) + list(APPEND SINSP_SOURCES + container_engine/docker_win.cpp) + else() + list(APPEND SINSP_SOURCES + container_engine/docker_linux.cpp + container_engine/libvirt_lxc.cpp + container_engine/lxc.cpp + container_engine/mesos.cpp + container_engine/rkt.cpp + container_engine/bpm.cpp + runc.cpp) + endif() + + if(NOT WIN32 AND NOT APPLE) + list(APPEND SINSP_SOURCES + cgroup_limits.cpp + cri.cpp + container_engine/cri.cpp + ${CMAKE_CURRENT_BINARY_DIR}/cri.grpc.pb.cc + ${CMAKE_CURRENT_BINARY_DIR}/cri.pb.cc + grpc_channel_registry.cpp + ) + endif() +endif() + +add_library(sinsp STATIC ${SINSP_SOURCES}) + +target_link_libraries(sinsp + scap + "${CURL_LIBRARIES}" + "${JSONCPP_LIB}" + "${TBB_LIB}" + "${CARES_LIB}") + +if(USE_BUNDLED_LUAJIT) + add_dependencies(sinsp luajit) +endif() + +if(NOT WIN32) + if(USE_BUNDLED_OPENSSL AND NOT MINIMAL_BUILD) + add_dependencies(sinsp openssl) + endif() # USE_BUNDLED_OPENSSL + if(USE_BUNDLED_CURL AND NOT MINIMAL_BUILD) + add_dependencies(sinsp curl) + endif() # USE_BUNDLED_CURL + if(USE_BUNDLED_TBB) + add_dependencies(sinsp tbb) + endif() # USE_BUNDLED_TBB + + + if(NOT APPLE) + if(NOT MINIMAL_BUILD) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cri.proto ${CMAKE_CURRENT_BINARY_DIR}/cri.proto COPYONLY) + set(CRI_PROTO_PATCH_MUSL_COMMAND "") + if (MUSL_OPTIMIZED_BUILD) + # We need to patch the CRI proto while under musl because musl + # does not fully respect the standard and defines stdin, stdout and stderr + # as compile time constants. However, those are already defined in cri.proto and that is a reference collision. + # see: https://git.musl-libc.org/cgit/musl/tree/include/stdio.h?id=0b0640219338b80cf47026d1970b5503414ed7f3#n60 + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/patch/musl-cri.proto.patch ${CMAKE_CURRENT_BINARY_DIR}/cmake/patch/musl-cri.proto.patch COPYONLY) + set(CRI_PROTO_PATCH_MUSL_COMMAND patch -d ${CMAKE_CURRENT_BINARY_DIR} -p3 -i ${CMAKE_CURRENT_BINARY_DIR}/cmake/patch/musl-cri.proto.patch) + endif() + + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cri.grpc.pb.cc + ${CMAKE_CURRENT_BINARY_DIR}/cri.grpc.pb.h + ${CMAKE_CURRENT_BINARY_DIR}/cri.pb.cc + ${CMAKE_CURRENT_BINARY_DIR}/cri.pb.h + COMMENT "Generate CRI grpc code" + DEPENDS + COMMAND ${CRI_PROTO_PATCH_MUSL_COMMAND} + COMMAND ${PROTOC} -I ${CMAKE_CURRENT_BINARY_DIR} --cpp_out=. ${CMAKE_CURRENT_BINARY_DIR}/cri.proto + COMMAND ${PROTOC} -I ${CMAKE_CURRENT_BINARY_DIR} --grpc_out=. --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${CMAKE_CURRENT_BINARY_DIR}/cri.proto + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + + if(USE_BUNDLED_GRPC) + add_dependencies(sinsp grpc) + endif() # USE_BUNDLED_GRPC + + target_link_libraries(sinsp + "${GRPCPP_LIB}" + "${GRPC_LIB}" + "${PROTOBUF_LIB}" + "${CARES_LIB}" + "${JQ_LIB}" + "${B64_LIB}") + + if(NOT MUSL_OPTIMIZED_BUILD) + target_link_libraries(sinsp + rt + anl) + endif() + + else() + target_link_libraries(sinsp + "${JQ_LIB}" + "${B64_LIB}" + rt) + endif() # NOT MINIMAL_BUILD + # when JQ is compiled statically, it will + # also compile a static object for oniguruma we need to link + if(USE_BUNDLED_JQ) + target_link_libraries(sinsp + "${ONIGURUMA_LIB}") + endif() + endif() # NOT APPLE + + if(USE_BUNDLED_OPENSSL AND NOT MINIMAL_BUILD) + target_link_libraries(sinsp + "${OPENSSL_LIBRARY_SSL}" + "${OPENSSL_LIBRARY_CRYPTO}") + else() + target_link_libraries(sinsp + "${OPENSSL_LIBRARIES}") + endif() + + target_link_libraries(sinsp + "${LUAJIT_LIB}" + dl + pthread) +else() + target_link_libraries(sinsp + "${LUAJIT_LIB}") +endif() # NOT WIN32 diff --git a/userspace/libsinsp/addrinfo.cpp b/userspace/libsinsp/addrinfo.cpp new file mode 100644 index 0000000000..5146dfdf5a --- /dev/null +++ b/userspace/libsinsp/addrinfo.cpp @@ -0,0 +1,39 @@ +/* +Copyright (C) 2020 Sysdig Inc. + +This file is part of sysdig. + +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 "addrinfo.h" + +#include +#include +#include +#include + +void ares_cb(void *arg, int status, int timeouts, struct hostent *host) +{ + if (status == ARES_SUCCESS) + { + struct in_addr addr; + char *p; + p = host->h_addr_list[0]; + memcpy(&addr, p, sizeof(struct in_addr)); + ares_cb_result *res = reinterpret_cast(arg); + auto addr_str = std::string(inet_ntoa(addr)); + res->address = addr_str; + res->addr = addr; + res->done = true; + } +} diff --git a/userspace/libsinsp/addrinfo.h b/userspace/libsinsp/addrinfo.h new file mode 100644 index 0000000000..31e25df849 --- /dev/null +++ b/userspace/libsinsp/addrinfo.h @@ -0,0 +1,35 @@ +/* +Copyright (C) 2020 Sysdig Inc. + +This file is part of sysdig. + +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. +*/ + +#ifndef MINIMAL_BUILD +#pragma once + +#include +#include + +struct ares_cb_result +{ + std::string address; + in_addr addr; + bool done = false; + bool call = false; +}; + +void ares_cb(void *arg, int status, int timeouts, struct hostent *host); + +#endif // MINIMAL_BUILD diff --git a/userspace/libsinsp/capture_stats_source.h b/userspace/libsinsp/capture_stats_source.h new file mode 100644 index 0000000000..b0f67833bb --- /dev/null +++ b/userspace/libsinsp/capture_stats_source.h @@ -0,0 +1,48 @@ +/* +Copyright (C) 2019 Sysdig Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include "sinsp_public.h" + +class scap_stats; + +/** + * Interface to an object that can provide capture statistics. + * + * Note that the intention here is to apply the Interface Segregation + * Principle (ISP) to class sinsp. Some clients of sinsp need only the + * get_capture_stats() API, and this interface exposes only that API. Do + * not add additional APIs here. If some client of sinsp needs a different + * set of APIs, introduce a new interface. + */ +class SINSP_PUBLIC capture_stats_source +{ +public: + virtual ~capture_stats_source() = default; + + /** + * Fill the given structure with statistics about the currently + * open capture. + * + * @note This may not work for a file-based capture source. + * + * @param[out] stats The capture statistics + */ + virtual void get_capture_stats(scap_stats* stats) const = 0; +}; diff --git a/userspace/libsinsp/cgroup_limits.cpp b/userspace/libsinsp/cgroup_limits.cpp new file mode 100644 index 0000000000..dea4245209 --- /dev/null +++ b/userspace/libsinsp/cgroup_limits.cpp @@ -0,0 +1,141 @@ +#include "cgroup_limits.h" + +#include +#include "cgroup_list_counter.h" +#include "sinsp.h" + +namespace { +// to prevent 32-bit number of kilobytes from overflowing, ignore values larger than 4 TiB. +// This reports extremely large values (e.g. almost-but-not-quite 9EiB as set by k8s) as unlimited. +// Note: we use the same maximum value for cpu shares/quotas as well; the typical values are much lower +// and so should never exceed CGROUP_VAL_MAX either +constexpr const int64_t CGROUP_VAL_MAX = (1ULL << 42u) - 1; + +/** + * \brief Read a single int64_t value from cgroupfs + * @param subsys path to the specific cgroup subsystem, e.g. /sys/fs/cgroup/cpu + * @param cgroup cgroup path within the cgroup mountpoint (like in /proc/pid/cgroup) + * @param filename the filename within the cgroup directory, e.g. cpu.shares + * @param out reference to the output value + * @return true if we successfully read the value and it's within reasonable range, + * reasonable being [0; CGROUP_VAL_MAX) + */ +bool read_cgroup_val(std::shared_ptr& subsys, + const std::string& cgroup, + const std::string& filename, + int64_t& out) +{ + std::string path = *subsys.get() + "/" + cgroup + "/" + filename; + std::ifstream cg_val(path); + + int64_t val = -1; + cg_val >> val; + + if(val <= 0 || val > CGROUP_VAL_MAX) + { + g_logger.format(sinsp_logger::SEV_DEBUG, "(cgroup-limits) value of %s (%lld) out of range, ignoring", + path.c_str(), val); + return false; + } + out = val; + return true; +} + +/** + * Read from a cpuset file to get the number of cpus in the cpuset + */ +bool read_cgroup_list_count(const std::string& subsys, + const std::string& cgroup, + const std::string& filename, + int32_t& out) +{ + std::string path = subsys + "/" + cgroup + "/" + filename; + std::ifstream cg_val(path); + + if(!cg_val) + { + return false; + } + + std::string cpuset_cpus((std::istreambuf_iterator(cg_val)), + std::istreambuf_iterator()); + + if(cpuset_cpus.empty()) + { + return false; + } + + libsinsp::cgroup_list_counter counter; + out = counter(cpuset_cpus.c_str()); + + g_logger.format(sinsp_logger::SEV_DEBUG, + "(cgroup-limits) Pulling cpu set from %s: %s = %d", + path.c_str(), + cpuset_cpus.c_str(), + out); + + return (out > 0); +} + +} + +namespace libsinsp { +namespace cgroup_limits { + +bool get_cgroup_resource_limits(const cgroup_limits_key& key, cgroup_limits_value& value, bool name_check) +{ + bool found_all = true; + std::shared_ptr memcg_root = sinsp::lookup_cgroup_dir("memory"); + if(name_check && key.m_mem_cgroup.find(key.m_container_id) == std::string::npos) + { + g_logger.format(sinsp_logger::SEV_INFO, "(cgroup-limits) mem cgroup for container [%s]: %s/%s -- no per-container memory cgroup, ignoring", + key.m_container_id.c_str(), memcg_root->c_str(), key.m_mem_cgroup.c_str()); + } + else + { + g_logger.format(sinsp_logger::SEV_DEBUG, "(cgroup-limits) mem cgroup for container [%s]: %s/%s", + key.m_container_id.c_str(), memcg_root->c_str(), key.m_mem_cgroup.c_str()); + found_all = read_cgroup_val(memcg_root, key.m_mem_cgroup, "memory.limit_in_bytes", value.m_memory_limit) && found_all; + } + + std::shared_ptr cpucg_root = sinsp::lookup_cgroup_dir("cpu"); + if(name_check && key.m_cpu_cgroup.find(key.m_container_id) == std::string::npos) + { + g_logger.format(sinsp_logger::SEV_INFO, "(cgroup-limits) cpu cgroup for container [%s]: %s/%s -- no per-container CPU cgroup, ignoring", + key.m_container_id.c_str(), cpucg_root->c_str(), key.m_cpu_cgroup.c_str()); + } + else + { + g_logger.format(sinsp_logger::SEV_DEBUG, "(cgroup-limits) cpu cgroup for container [%s]: %s/%s", + key.m_container_id.c_str(), cpucg_root->c_str(), key.m_cpu_cgroup.c_str()); + found_all = read_cgroup_val(cpucg_root, key.m_cpu_cgroup, "cpu.shares", value.m_cpu_shares) && found_all; + found_all = read_cgroup_val(cpucg_root, key.m_cpu_cgroup, "cpu.cfs_quota_us", value.m_cpu_quota) && found_all; + found_all = read_cgroup_val(cpucg_root, key.m_cpu_cgroup, "cpu.cfs_period_us", value.m_cpu_period) && found_all; + } + + std::shared_ptr cpuset_root = sinsp::lookup_cgroup_dir("cpuset"); + if (name_check && key.m_cpuset_cgroup.find(key.m_container_id) == std::string::npos) + { + g_logger.format(sinsp_logger::SEV_DEBUG, "(cgroup-limits) cpuset cgroup for container [%s]: %s/%s -- no per-container cpuset cgroup, ignoring", + key.m_container_id.c_str(), cpuset_root->c_str(), key.m_cpuset_cgroup.c_str()); + } + else + { + g_logger.format(sinsp_logger::SEV_DEBUG, "(cgroup-limits) cpuset cgroup for container [%s]: %s/%s", + key.m_container_id.c_str(), cpuset_root->c_str(), key.m_cpuset_cgroup.c_str()); + found_all = read_cgroup_list_count(*cpuset_root, + key.m_cpuset_cgroup, + "cpuset.cpus", + value.m_cpuset_cpu_count) && found_all; + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "(cgroup-limits) Got cgroup limits for container [%s]: " + "mem_limit=%ld, cpu_shares=%ld cpu_quota=%ld cpu_period=%ld cpuset_cpu_count=%d", + key.m_container_id.c_str(), + value.m_memory_limit, value.m_cpu_shares, value.m_cpu_quota, value.m_cpu_period, value.m_cpuset_cpu_count); + + return found_all; +} +} +} diff --git a/userspace/libsinsp/cgroup_limits.h b/userspace/libsinsp/cgroup_limits.h new file mode 100644 index 0000000000..9677c157fe --- /dev/null +++ b/userspace/libsinsp/cgroup_limits.h @@ -0,0 +1,131 @@ +#pragma once + +#include +#include +#include "async_key_value_source.h" + +namespace { +bool less_than(const std::string& lhs, const std::string& rhs, bool if_equal=false) +{ + int cmp = lhs.compare(rhs); + if(cmp < 0) + { + return true; + } + else if(cmp > 0) + { + return false; + } + else + { + return if_equal; + } +} +} + +namespace libsinsp { +namespace cgroup_limits { + +/** + * \brief The key for cgroup value lookup + * + * It's effectively a (container_id, cpu_cgroup, mem_cgroup) tuple + * that can be used as a hash key. + */ +struct cgroup_limits_key { + cgroup_limits_key() {} + + cgroup_limits_key(std::string container_id, + std::string cpu_cgroup_dir, + std::string mem_cgroup_dir, + std::string cpuset_cgroup_dir) : + m_container_id(std::move(container_id)), + m_cpu_cgroup(std::move(cpu_cgroup_dir)), + m_mem_cgroup(std::move(mem_cgroup_dir)), + m_cpuset_cgroup(std::move(cpuset_cgroup_dir)) { } + + bool operator<(const cgroup_limits_key& rhs) const + { + return less_than(m_container_id, rhs.m_container_id, + less_than(m_cpu_cgroup, rhs.m_cpu_cgroup, + less_than(m_mem_cgroup, rhs.m_mem_cgroup, + less_than(m_cpuset_cgroup, rhs.m_cpuset_cgroup)))); + } + + bool operator==(const cgroup_limits_key& rhs) const + { + return m_container_id == rhs.m_container_id && + m_cpu_cgroup == rhs.m_cpu_cgroup && + m_mem_cgroup == rhs.m_mem_cgroup && + m_cpuset_cgroup == rhs.m_cpuset_cgroup; + } + + explicit operator const std::string&() const + { + return m_container_id; + } + + std::string m_container_id; + std::string m_cpu_cgroup; + std::string m_mem_cgroup; + std::string m_cpuset_cgroup; +}; + +/** + * \brief The result of an asynchronous cgroup lookup + * + * This contains all the cgroup values we read during the asynchronous lookup + */ +struct cgroup_limits_value { + cgroup_limits_value() : + m_cpu_shares(0), + m_cpu_quota(0), + m_cpu_period(0), + m_memory_limit(0), + m_cpuset_cpu_count(0) {} + + int64_t m_cpu_shares; + int64_t m_cpu_quota; + int64_t m_cpu_period; + int64_t m_memory_limit; + int32_t m_cpuset_cpu_count; +}; + +/** + * \brief Read resource limits from cgroups + * @param key the container to read limits for + * @param value output value. when the return value is false, specific fields + * may or may not have been modified + * @param name_check if true and the container doesn't use its own cgroups + * for mem/cpu, we log a message and we ignore the values. + * "Use its own cgroups" means the container id is present in the cgroup + * path, which may not be true for all container engines. + * @return true when all values have been successfully read, false otherwise + * + * Note: reading a zero/negative/very large value is considered a failure, + * because it might mean that resource limits haven't yet been set. Essentially, + * `false` means "there's real chance the limits could conceivably change + * in the future", while `true` means we really don't expect them to change + * any more. + */ +bool get_cgroup_resource_limits(const cgroup_limits_key& key, cgroup_limits_value& value, bool name_check = true); + +} +} + +namespace std { +/** + * \brief Specialization of std::hash for cgroup_limits_key + * + * It allows `cgroup_limits_key` instances to be used as `unordered_map` keys + */ +template<> struct hash { + std::size_t operator()(const libsinsp::cgroup_limits::cgroup_limits_key& h) const { + size_t h1 = ::std::hash{}(h.m_container_id); + size_t h2 = ::std::hash{}(h.m_cpu_cgroup); + size_t h3 = ::std::hash{}(h.m_mem_cgroup); + size_t h4 = ::std::hash{}(h.m_cpuset_cgroup); + return h1 ^ (h2 << 1u) ^ (h3 << 2u) ^ (h4 << 3u); + } +}; +} \ No newline at end of file diff --git a/userspace/libsinsp/cgroup_list_counter.h b/userspace/libsinsp/cgroup_list_counter.h new file mode 100644 index 0000000000..1d8c2ca43e --- /dev/null +++ b/userspace/libsinsp/cgroup_list_counter.h @@ -0,0 +1,136 @@ +#pragma once + +#include +#include "logger.h" + +namespace libsinsp +{ + +/** + * Simple helper to read a comma-separated list that includes ranges and + * determine the total count of the values within. + * Examples: 1,4-5 = 3; 0-15 = 16; 3,7,11 = 3 + * + * See the "List Format" section of + * http://man7.org/linux/man-pages/man7/cpuset.7.html + * + * Returns -1 if string is invalid. + */ +class cgroup_list_counter +{ +public: + const int INVALID_CPU_COUNT = -1; + + /** + * Return the number of elements given by the buffer. If needed, log at the + * given log-level. + */ + int operator ()(const char *buffer) + { + reset(); + + int cpu_count = 0; + + try + { + const char *position = buffer; + for(; '\0' != *position; ++position) + { + if ('-' == *position) + { + if (nullptr == m_section_start) + { + throw std::runtime_error("duplicate range indicator before start"); + } + if (nullptr != m_range_indicator) + { + throw std::runtime_error("duplicate range indicators"); + } + + m_range_indicator = position; + } + else if (',' == *position) + { + cpu_count += process_section(m_section_start, position, m_range_indicator); + reset(); + } + else if (nullptr == m_section_start) + { + m_section_start = position; + } + + } + + // There is never a trailing comma so always process the + // final section + cpu_count += process_section(m_section_start, position, m_range_indicator); + + } + catch (const std::exception& ex) + { + g_logger.format(sinsp_logger::SEV_ERROR, + "Invalid List Format: %s. Detail: %s", + buffer, + ex.what()); + return INVALID_CPU_COUNT; + } + + return cpu_count; + } + +private: + + static int process_number(const char *section_start, const char *section_end) + { + std::string section(section_start, section_end - section_start); + return std::stoi(section.c_str()); + + } + + static int process_section(const char *section_start, const char *section_end, const char *range_indicator) + { + if (nullptr == section_start) + { + throw std::runtime_error("invalid end of section before start of section"); + } + + if (nullptr == section_end) + { + throw std::runtime_error("invalid end of section"); + } + + if (section_end <= section_start) + { + throw std::runtime_error("invalid section"); + } + + if (range_indicator) + { + // Split into two sections + int first = process_number(section_start, range_indicator); + int second = process_number(range_indicator + 1, section_end); + + if (second <= first) + { + throw std::runtime_error("invalid range"); + } + + return second - first + 1; + } + + // We don't care what the value is, we just want to know that it is a number + (void)process_number(section_start, section_end); + return 1; + } + + void reset() + { + m_section_start = nullptr; + m_range_indicator = nullptr; + } + + const char *m_section_start = nullptr; + const char *m_range_indicator = nullptr; +}; + +} diff --git a/userspace/libsinsp/chisel.cpp b/userspace/libsinsp/chisel.cpp index 1376e611d1..0a6e3cf85c 100644 --- a/userspace/libsinsp/chisel.cpp +++ b/userspace/libsinsp/chisel.cpp @@ -1,45 +1,50 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include #include -#include -#include #include #include -#ifndef _WIN32 +#ifdef _WIN32 +#include +#else #include #include +#include #endif #include #include #include "sinsp.h" +#include "sinsp_capture_interrupt_exception.h" #include "sinsp_int.h" #include "chisel.h" +#include "chisel_api.h" #include "filter.h" #include "filterchecks.h" +#include "table.h" #ifdef HAS_CHISELS - #define HAS_LUA_CHISELS #ifdef HAS_LUA_CHISELS + extern "C" { #include "lua.h" #include "lualib.h" @@ -52,17 +57,17 @@ extern sinsp_filter_check_list g_filterlist; extern sinsp_evttables g_infotables; /////////////////////////////////////////////////////////////////////////////// -// For LUA debugging +// For Lua debugging /////////////////////////////////////////////////////////////////////////////// #ifdef HAS_LUA_CHISELS -void lua_stackdump(lua_State *L) +void lua_stackdump(lua_State *L) { int i; int top = lua_gettop(L); - for (i = 1; i <= top; i++) + for (i = 1; i <= top; i++) { int t = lua_type(L, i); - switch (t) + switch (t) { case LUA_TSTRING: // strings @@ -90,456 +95,58 @@ void lua_stackdump(lua_State *L) #endif /////////////////////////////////////////////////////////////////////////////// -// LUA callbacks +// Lua callbacks /////////////////////////////////////////////////////////////////////////////// #ifdef HAS_LUA_CHISELS -class lua_cbacks -{ -public: - static uint32_t rawval_to_lua_stack(lua_State *ls, uint8_t* rawval, const filtercheck_field_info* finfo, uint32_t len) - { - ASSERT(rawval != NULL); - ASSERT(finfo != NULL); - - switch(finfo->m_type) - { - case PT_INT8: - lua_pushnumber(ls, *(int8_t*)rawval); - return 1; - case PT_INT16: - lua_pushnumber(ls, *(int16_t*)rawval); - return 1; - case PT_INT32: - lua_pushnumber(ls, *(int32_t*)rawval); - return 1; - case PT_INT64: - case PT_ERRNO: - case PT_PID: - lua_pushnumber(ls, (double)*(int64_t*)rawval); - return 1; - case PT_L4PROTO: // This can be resolved in the future - case PT_FLAGS8: - case PT_UINT8: - lua_pushnumber(ls, *(uint8_t*)rawval); - return 1; - case PT_PORT: // This can be resolved in the future - case PT_FLAGS16: - case PT_UINT16: - lua_pushnumber(ls, *(uint16_t*)rawval); - return 1; - case PT_FLAGS32: - case PT_UINT32: - lua_pushnumber(ls, *(uint32_t*)rawval); - return 1; - case PT_UINT64: - case PT_RELTIME: - case PT_ABSTIME: - lua_pushnumber(ls, (double)*(uint64_t*)rawval); - return 1; - case PT_CHARBUF: - lua_pushstring(ls, (char*)rawval); - return 1; - case PT_BYTEBUF: - if(rawval[len] == 0) - { - lua_pushstring(ls, (char*)rawval); - return 1; - } - else - { - lua_getglobal(ls, "sichisel"); - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - uint32_t max_len = len < sizeof(ch->m_lua_fld_storage) ? - len : sizeof(ch->m_lua_fld_storage) - 1; - - memcpy(ch->m_lua_fld_storage, rawval, max_len); - ch->m_lua_fld_storage[max_len] = 0; - lua_pushstring(ls, (char*)ch->m_lua_fld_storage); - return 1; - } - case PT_SOCKADDR: - ASSERT(false); - return 0; - case PT_SOCKFAMILY: - ASSERT(false); - return 0; - case PT_BOOL: - lua_pushboolean(ls, (*(uint32_t*)rawval != 0)); - return 1; - case PT_IPV4ADDR: - { - lua_getglobal(ls, "sichisel"); - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - snprintf(ch->m_lua_fld_storage, - sizeof(ch->m_lua_fld_storage), - "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8, - rawval[0], - rawval[1], - rawval[2], - rawval[3]); - - lua_pushstring(ls, ch->m_lua_fld_storage); - return 1; - } - default: - ASSERT(false); - throw sinsp_exception("wrong event type " + to_string((long long) finfo->m_type)); - } - } - - static int get_num(lua_State *ls) - { - lua_getglobal(ls, "sievt"); - sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - if(evt == NULL) - { - throw sinsp_exception("invalid call to evt.get_num()"); - } - - lua_pushnumber(ls, (double)evt->get_num()); - return 1; - } - - static int get_ts(lua_State *ls) - { - lua_getglobal(ls, "sievt"); - sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - if(evt == NULL) - { - throw sinsp_exception("invalid call to evt.get_ts()"); - } - - uint64_t ts = evt->get_ts(); - - lua_pushinteger(ls, (uint32_t)(ts / 1000000000)); - lua_pushinteger(ls, (uint32_t)(ts % 1000000000)); - return 2; - } - - static int get_type(lua_State *ls) - { - lua_getglobal(ls, "sievt"); - sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - if(evt == NULL) - { - throw sinsp_exception("invalid call to evt.get_type()"); - } - - const char* evname; - uint16_t etype = evt->get_type(); - - if(etype == PPME_GENERIC_E || etype == PPME_GENERIC_X) - { - sinsp_evt_param *parinfo = evt->get_param(0); - ASSERT(parinfo->m_len == sizeof(uint16_t)); - uint16_t evid = *(uint16_t *)parinfo->m_val; - - evname = g_infotables.m_syscall_info_table[evid].name; - } - else - { - evname = evt->get_name(); - } - - lua_pushstring(ls, evname); - - return 1; - } - - static int get_cpuid(lua_State *ls) - { - lua_getglobal(ls, "sievt"); - sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - if(evt == NULL) - { - throw sinsp_exception("invalid call to evt.get_cpuid()"); - } - - uint32_t cpuid = evt->get_cpuid(); - - lua_pushinteger(ls, cpuid); - return 1; - } - - static int request_field(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - sinsp* inspector = ch->m_inspector; - - const char* fld = lua_tostring(ls, 1); - - sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(fld, - inspector, - false); - - if(chk == NULL) - { - throw sinsp_exception("chisel requesting nonexistent field " + string(fld)); - } - - chk->parse_field_name(fld); - - lua_pushlightuserdata(ls, chk); - - ch->m_allocated_fltchecks.push_back(chk); - - return 1; - } - - static int field(lua_State *ls) - { - lua_getglobal(ls, "sievt"); - sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - if(evt == NULL) - { - throw sinsp_exception("invalid call to evt.field()"); - } - - sinsp_filter_check* chk = (sinsp_filter_check*)lua_topointer(ls, 1); - if(chk == NULL) - { - // - // This happens if the lua code is calling field() without invoking - // sysdig.request_field() before. - // - lua_pushnil(ls); - return 1; - } - - uint32_t vlen; - uint8_t* rawval = chk->extract(evt, &vlen); - - if(rawval != NULL) - { - return rawval_to_lua_stack(ls, rawval, chk->get_field_info(), vlen); - } - else - { - lua_pushnil(ls); - return 1; - } - } - - static int set_global_filter(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - const char* filter = lua_tostring(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - ch->m_inspector->set_filter(filter); - - return 0; - } - - static int set_filter(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - const char* filter = lua_tostring(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - ch->m_lua_cinfo->set_filter(filter); - - return 0; - } - - static int set_snaplen(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - const uint32_t snaplen = lua_tointeger(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - ch->m_inspector->set_snaplen(snaplen); - - return 0; - } - - static int is_live(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - lua_pushboolean(ls, ch->m_inspector->is_live()); - return 1; - } - - static int get_machine_info(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - const scap_machine_info* minfo = ch->m_inspector->get_machine_info(); - - lua_newtable(ls); - lua_pushstring(ls, "num_cpus"); - lua_pushnumber(ls, minfo->num_cpus); - lua_settable(ls, -3); - lua_pushstring(ls, "memory_size_bytes"); - lua_pushnumber(ls, (double)minfo->memory_size_bytes); - lua_settable(ls, -3); - lua_pushstring(ls, "max_pid"); - lua_pushnumber(ls, (double)minfo->max_pid); - lua_settable(ls, -3); - lua_pushstring(ls, "hostname"); - lua_pushstring(ls, minfo->hostname); - lua_settable(ls, -3); - - return 1; - } - - static int set_event_formatter(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - const char* formatter = lua_tostring(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - ch->m_lua_cinfo->set_formatter(formatter); - - return 0; - } - - static int set_interval_ns(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - uint64_t interval = (uint64_t)lua_tonumber(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - ch->m_lua_cinfo->set_callback_interval(interval); - - return 0; - } - - static int set_interval_s(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - uint64_t interval = (uint64_t)lua_tonumber(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - ch->m_lua_cinfo->set_callback_interval(interval * 1000000000); - - return 0; - } - - static int exec(lua_State *ls) - { - lua_getglobal(ls, "sichisel"); - - sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); - lua_pop(ls, 1); - - ASSERT(ch); - ASSERT(ch->m_lua_cinfo); - - const char* chname = lua_tostring(ls, 1); - if(chname == NULL) - { - throw sinsp_exception("invalid exec field name in chisel " + ch->m_filename); - } - - ch->m_new_chisel_to_exec = chname; - - ch->m_argvals.clear(); - uint32_t stackpos = 2; - - while(true) - { - const char* argval = lua_tostring(ls, stackpos++); - if(argval == NULL) - { - break; - } - - ch->m_argvals.push_back(argval); - } - - return 0; - } -}; - -const static struct luaL_reg ll_sysdig [] = +const static struct luaL_reg ll_sysdig [] = { {"set_filter", &lua_cbacks::set_global_filter}, {"set_snaplen", &lua_cbacks::set_snaplen}, + {"set_output_format", &lua_cbacks::set_output_format}, + {"set_fatfile_dump_mode", &lua_cbacks::set_fatfile_dump_mode}, {"is_live", &lua_cbacks::is_live}, + {"is_tty", &lua_cbacks::is_tty}, + {"get_terminal_info", &lua_cbacks::get_terminal_info}, + {"get_filter", &lua_cbacks::get_filter}, {"get_machine_info", &lua_cbacks::get_machine_info}, + {"get_thread_table", &lua_cbacks::get_thread_table}, + {"get_thread_table_nofds", &lua_cbacks::get_thread_table_nofds}, + {"get_thread_table_barebone", &lua_cbacks::get_thread_table_barebone}, + {"get_thread_table_barebone_nofds", &lua_cbacks::get_thread_table_barebone_nofds}, + {"get_container_table", &lua_cbacks::get_container_table}, + {"is_print_container_data", &lua_cbacks::is_print_container_data}, + {"get_output_format", &lua_cbacks::get_output_format}, + {"get_evtsource_name", &lua_cbacks::get_evtsource_name}, + {"get_firstevent_ts", &lua_cbacks::get_firstevent_ts}, + {"get_lastevent_ts", &lua_cbacks::get_lastevent_ts}, + {"make_ts", &lua_cbacks::make_ts}, + {"add_ts", &lua_cbacks::add_ts}, + {"subtract_ts", &lua_cbacks::subtract_ts}, + {"run_sysdig", &lua_cbacks::run_sysdig}, + {"end_capture", &lua_cbacks::end_capture}, + {"log", &lua_cbacks::log}, + {"udp_setpeername", &lua_cbacks::udp_setpeername}, + {"udp_send", &lua_cbacks::udp_send}, + {"get_read_progress", &lua_cbacks::get_read_progress}, +#ifdef HAS_ANALYZER + {"push_metric", &lua_cbacks::push_metric}, +#endif {NULL,NULL} }; -const static struct luaL_reg ll_chisel [] = +const static struct luaL_reg ll_chisel [] = { {"request_field", &lua_cbacks::request_field}, {"set_filter", &lua_cbacks::set_filter}, {"set_event_formatter", &lua_cbacks::set_event_formatter}, {"set_interval_ns", &lua_cbacks::set_interval_ns}, {"set_interval_s", &lua_cbacks::set_interval_s}, + {"set_precise_interval_ns", &lua_cbacks::set_precise_interval_ns}, {"exec", &lua_cbacks::exec}, {NULL,NULL} }; -const static struct luaL_reg ll_evt [] = +const static struct luaL_reg ll_evt [] = { {"field", &lua_cbacks::field}, {"get_num", &lua_cbacks::get_num}, @@ -550,64 +157,6 @@ const static struct luaL_reg ll_evt [] = }; #endif // HAS_LUA_CHISELS -/////////////////////////////////////////////////////////////////////////////// -// String helpers -/////////////////////////////////////////////////////////////////////////////// -// -// trim from start -// -string& ltrim(string &s) -{ - s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun(isspace)))); - return s; -} - -// -// trim from end -// -string& rtrim(string &s) -{ - s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), s.end()); - return s; -} - -// -// trim from both ends -// -string& trim(string &s) -{ - return ltrim(rtrim(s)); -} - -void replace_in_place(string &s, const string &search, const string &replace) -{ - for(size_t pos = 0; ; pos += replace.length()) - { - // Locate the substring to replace - pos = s.find(search, pos); - if(pos == string::npos ) break; - // Replace by erasing and inserting - s.erase(pos, search.length()); - s.insert(pos, replace ); - } -} - -void replace_in_place(string& str, string& substr_to_replace, string& new_substr) -{ - size_t index = 0; - uint32_t nsize = substr_to_replace.size(); - - while (true) - { - index = str.find(substr_to_replace, index); - if (index == string::npos) break; - - str.replace(index, nsize, new_substr); - - index += nsize; - } -} - /////////////////////////////////////////////////////////////////////////////// // chiselinfo implementation /////////////////////////////////////////////////////////////////////////////// @@ -617,9 +166,12 @@ chiselinfo::chiselinfo(sinsp* inspector) m_formatter = NULL; m_dumper = NULL; m_inspector = inspector; + m_has_nextrun_args = false; + m_end_capture = false; #ifdef HAS_LUA_CHISELS m_callback_interval = 0; + m_callback_precise_interval = 0; #endif } @@ -649,6 +201,8 @@ void chiselinfo::init(string filterstr, string formatterstr) void chiselinfo::set_filter(string filterstr) { + + sinsp_filter_compiler compiler(m_inspector, filterstr); if(m_filter) { delete m_filter; @@ -657,7 +211,7 @@ void chiselinfo::set_filter(string filterstr) if(filterstr != "") { - m_filter = new sinsp_filter(m_inspector, filterstr); + m_filter = compiler.compile(); } } @@ -684,6 +238,11 @@ void chiselinfo::set_callback_interval(uint64_t interval) { m_callback_interval = interval; } + +void chiselinfo::set_callback_precise_interval(uint64_t interval) +{ + m_callback_precise_interval = interval; +} #endif /////////////////////////////////////////////////////////////////////////////// @@ -692,29 +251,19 @@ void chiselinfo::set_callback_interval(uint64_t interval) sinsp_chisel::sinsp_chisel(sinsp* inspector, string filename) { m_inspector = inspector; - m_root = NULL; m_ls = NULL; m_lua_has_handle_evt = false; m_lua_is_first_evt = true; m_lua_cinfo = NULL; m_lua_last_interval_sample_time = 0; m_lua_last_interval_ts = 0; + m_udp_socket = 0; load(filename); } sinsp_chisel::~sinsp_chisel() { - for(vector::iterator it = m_subchisels.begin(); it != m_subchisels.end(); ++it) - { - delete *it; - } - - if(m_root != NULL) - { - delete m_root; - } - free_lua_chisel(); } @@ -735,11 +284,21 @@ void sinsp_chisel::free_lua_chisel() if(m_lua_cinfo != NULL) { - delete m_lua_cinfo; + delete m_lua_cinfo; m_lua_cinfo = NULL; } m_lua_script_info.reset(); + + if(m_udp_socket > 0) + { +#ifdef _WIN32 + closesocket(m_udp_socket); +#else + close(m_udp_socket); +#endif + m_udp_socket = 0; + } #endif } @@ -750,6 +309,7 @@ void parse_lua_chisel_arg(lua_State *ls, OUT chisel_desc* cd) string name; string type; string desc; + bool optional = false; while(lua_next(ls, -2) != 0) { @@ -768,6 +328,13 @@ void parse_lua_chisel_arg(lua_State *ls, OUT chisel_desc* cd) desc = lua_tostring(ls, -1); } } + else if(lua_isboolean(ls, -1)) + { + if(string(lua_tostring(ls, -2)) == "optional") + { + optional = (lua_toboolean(ls, -1) != 0); + } + } else { throw sinsp_exception(string(lua_tostring(ls, -2)) + " is not a string"); @@ -776,7 +343,7 @@ void parse_lua_chisel_arg(lua_State *ls, OUT chisel_desc* cd) lua_pop(ls, 1); } - cd->m_args.push_back(chiselarg_desc(name, type, desc)); + cd->m_args.push_back(chiselarg_desc(name, type, desc, optional)); } void parse_lua_chisel_args(lua_State *ls, OUT chisel_desc* cd) @@ -806,7 +373,7 @@ void parse_lua_chisel_args(lua_State *ls, OUT chisel_desc* cd) void sinsp_chisel::add_lua_package_path(lua_State* ls, const char* path) { lua_getglobal(ls, "package"); - lua_getfield(ls, -1, "path"); + lua_getfield(ls, -1, "path"); string cur_path = lua_tostring(ls, -1 ); cur_path += ';'; @@ -819,164 +386,765 @@ void sinsp_chisel::add_lua_package_path(lua_State* ls, const char* path) } #endif -// -// 1. Iterates through the chisel files on disk (.sc and .lua) -// 2. Opens them and extracts the fields (name, description, etc) -// 3. Adds them to the chisel_descs vector. -// -void sinsp_chisel::get_chisel_list(vector* chisel_descs) +sinsp_field_aggregation sinsp_chisel::string_to_aggregation(string ag) { - uint32_t j; + sinsp_field_aggregation res = A_NONE; - for(j = 0; j < g_chisel_dirs->size(); j++) + if(ag == "SUM") { - if(string(g_chisel_dirs->at(j).m_dir) == "") - { - continue; - } - - tinydir_dir dir; - tinydir_open(&dir, g_chisel_dirs->at(j).m_dir); - - while(dir.has_next) - { - tinydir_file file; - tinydir_readfile(&dir, &file); - - string fname(file.name); - string fpath(file.path); - - if(fname.find(".sc") == fname.size() - 3) - { - try - { - sinsp_chisel ch(NULL, fpath); + res = A_SUM; + } + else if(ag == "AVG") + { + res = A_AVG; + } + else if(ag == "TIME_AVG") + { + res = A_TIME_AVG; + } + else if(ag == "MIN") + { + res = A_MIN; + } + else if(ag == "MAX") + { + res = A_MAX; + } + else + { + throw sinsp_exception("unknown view column aggregation " + ag); + } + + return res; +} - chisel_desc cd; - cd.m_name = fname.substr(0, fname.rfind('.')); - cd.m_description = ch.m_description; +void sinsp_chisel::parse_view_column(lua_State *ls, OUT chisel_desc* cd, OUT void* columns) +{ + vector* cols = (vector*)columns; - const Json::Value args = (*ch.m_root)["info"]["arguments"]; - for(uint32_t k = 0; k < args.size(); k++) - { - cd.m_args.push_back(chiselarg_desc( - args[k]["name"].asString(), - args[k]["type"].asString(), - args[k]["description"].asString() - )); - } + lua_pushnil(ls); + + string tmpstr; + string name; + string description; + string field; + string filterfield; + uint32_t colsize = 0xffffffff; + uint32_t flags = TEF_NONE; + sinsp_field_aggregation aggregation = A_NONE; + sinsp_field_aggregation groupby_aggregation = A_NONE; + vector tags; - chisel_descs->push_back(cd); + while(lua_next(ls, -2) != 0) + { + string fldname = lua_tostring(ls, -2); + + if(fldname == "name") + { + name = lua_tostring(ls, -1); + } + else if(fldname == "description") + { + description = lua_tostring(ls, -1); + } + else if(fldname == "field") + { + field = lua_tostring(ls, -1); + } + else if(fldname == "filterfield") + { + filterfield = lua_tostring(ls, -1); + } + else if(fldname == "colsize") + { + if(lua_isnumber(ls, -1)) + { + colsize = (uint32_t)lua_tonumber(ls, -1); + } + else + { + throw sinsp_exception(string(lua_tostring(ls, -2)) + " must be a number"); + } + } + else if(fldname == "is_key") + { + if(lua_isboolean(ls, -1)) + { + bool ik = (lua_toboolean(ls, -1) != 0); + if(ik) + { + flags |= TEF_IS_KEY; } - catch(...) + } + else + { + throw sinsp_exception(string(lua_tostring(ls, -2)) + " must be a boolean value"); + } + } + else if(fldname == "filter_in_child_only") + { + if(lua_isboolean(ls, -1)) + { + bool ik = (lua_toboolean(ls, -1) != 0); + if(ik) { - // - // If there was an error opening the chisel, skip to the next one - // - goto next_file; + flags |= TEF_FILTER_IN_CHILD_ONLY; } } - -#ifdef HAS_LUA_CHISELS - if(fname.find(".lua") == fname.size() - 4) + else { - chisel_desc cd; - cd.m_name = fname.substr(0, fname.rfind('.')); - - lua_State* ls = lua_open(); - - luaL_openlibs(ls); - - // - // Load our own lua libs - // - luaL_openlib(ls, "sysdig", ll_sysdig, 0); - luaL_openlib(ls, "chisel", ll_chisel, 0); - luaL_openlib(ls, "evt", ll_evt, 0); - - // - // Add our chisel paths to package.path - // - for(uint32_t k = 0; k < g_chisel_dirs->size(); k++) + throw sinsp_exception(string(lua_tostring(ls, -2)) + " must be a boolean value"); + } + } + else if(fldname == "is_groupby_key") + { + if(lua_isboolean(ls, -1)) + { + bool ik = (lua_toboolean(ls, -1) != 0); + if(ik) { - string path(g_chisel_dirs->at(k).m_dir); - path += "?.lua"; - add_lua_package_path(ls, path.c_str()); + flags |= TEF_IS_GROUPBY_KEY; } - - // - // Load the script - // - if(luaL_loadfile(ls, fpath.c_str()) || lua_pcall(ls, 0, 0, 0)) + } + else + { + throw sinsp_exception(string(lua_tostring(ls, -2)) + " must be a boolean value"); + } + } + else if(fldname == "is_sorting") + { + if(lua_isboolean(ls, -1)) + { + bool ik = (lua_toboolean(ls, -1) != 0); + if(ik) { - goto next_lua_file; + flags |= TEF_IS_SORT_COLUMN; } + } + else + { + throw sinsp_exception(string(lua_tostring(ls, -2)) + " must be a boolean value"); + } + } + else if(fldname == "aggregation") + { + if(lua_isstring(ls, -1)) + { + string ag = lua_tostring(ls, -1); - // - // Extract the description - // - lua_getglobal(ls, "description"); - if(!lua_isstring(ls, -1)) - { - goto next_lua_file; - } + aggregation = string_to_aggregation(ag); + } + } + else if(fldname == "groupby_aggregation") + { + if(lua_isstring(ls, -1)) + { + string ag = lua_tostring(ls, -1); - cd.m_description = lua_tostring(ls, -1); + groupby_aggregation = string_to_aggregation(ag); + } + } + else if(fldname == "tags") + { + if(lua_istable(ls, -1)) + { + lua_pushnil(ls); - // - // Extract the short description - // - lua_getglobal(ls, "short_description"); - if(!lua_isstring(ls, -1)) - { - goto next_lua_file; - } - cd.m_shortdesc = lua_tostring(ls, -1); - - // - // Extract the category - // - cd.m_category = ""; - lua_getglobal(ls, "category"); - if(lua_isstring(ls, -1)) + while(lua_next(ls, -2) != 0) { - cd.m_category = lua_tostring(ls, -1); - } - - // - // Extract the hidden flag and skip the chisel if it's set - // - lua_getglobal(ls, "hidden"); - if(lua_isboolean(ls, -1)) + if(lua_isstring(ls, -1)) + { + tmpstr = lua_tostring(ls, -1); + tags.push_back(tmpstr); + } + else + { + throw sinsp_exception("tags column entries must be strings"); + } + + lua_pop(ls, 1); + } + } + else + { + throw sinsp_exception(string(lua_tostring(ls, -2)) + " is not a table"); + } + } + + lua_pop(ls, 1); + } + + if(filterfield != "" && ((flags & TEF_IS_KEY) == 0) && ((flags & TEF_IS_GROUPBY_KEY) == 0)) + { + throw sinsp_exception("wrong view column syntax: filterfield specified for a non key column"); + } + + cols->push_back(sinsp_view_column_info(field, + name, + description, + colsize, + (uint32_t)flags, + aggregation, + groupby_aggregation, + tags, + filterfield)); +} + +void sinsp_chisel::parse_view_columns(lua_State *ls, OUT chisel_desc* cd, OUT void* columns) +{ + string name; + string type; + string desc; + + lua_pushnil(ls); + + while(lua_next(ls, -2) != 0) + { + if(lua_istable(ls, -1)) + { + parse_view_column(ls, cd, columns); + } + else + { + throw sinsp_exception("view_info column entries must be tables"); + } + + lua_pop(ls, 1); + } +} + +void sinsp_chisel::parse_view_action(lua_State *ls, OUT chisel_desc* cd, OUT void* actions) +{ + vector* keys = (vector*)actions; + + lua_pushnil(ls); + + char key = 0; + string command; + string description; + string tmpstr; + bool ask_confirmation = false; + bool waitfinish = true; + + while(lua_next(ls, -2) != 0) + { + string fldname = lua_tostring(ls, -2); + + if(fldname == "hotkey") + { + tmpstr = lua_tostring(ls, -1); + if(tmpstr.size() == 1) + { + key = tmpstr[0]; + } + else + { + throw sinsp_exception("action 'key' field must be a single character string"); + } + } + else if(fldname == "command") + { + command = lua_tostring(ls, -1); + } + else if(fldname == "description") + { + description = lua_tostring(ls, -1); + } + else if(fldname == "wait_finish") + { + int wf = lua_toboolean(ls, -1); + + if(wf == 0) + { + waitfinish = false; + } + } + else if(fldname == "ask_confirmation") + { + int wf = lua_toboolean(ls, -1); + + if(wf == 1) + { + ask_confirmation = true; + } + } + + lua_pop(ls, 1); + } + + if(key == 0) + { + throw sinsp_exception("action missing the 'key' value"); + } + + if(command == "") + { + throw sinsp_exception("action missing the 'command' value"); + } + + keys->push_back(sinsp_view_action_info(key, + command, + description, + ask_confirmation, + waitfinish)); +} + +void sinsp_chisel::parse_view_actions(lua_State *ls, OUT chisel_desc* cd, OUT void* actions) +{ + string name; + string type; + string desc; + + lua_pushnil(ls); + + while(lua_next(ls, -2) != 0) + { + if(lua_istable(ls, -1)) + { + parse_view_action(ls, cd, actions); + } + else + { + throw sinsp_exception("view_info action entries must be tables"); + } + + lua_pop(ls, 1); + } +} + +bool sinsp_chisel::parse_view_info(lua_State *ls, OUT chisel_desc* cd) +{ + lua_getglobal(ls, "view_info"); + if(lua_isnoneornil(ls, -1)) + { + lua_close(ls); + return false; + } + + lua_pushnil(ls); + + string tmpstr; + string id; + string name; + string description; + vector applies_to; + string filter; + bool use_defaults = false; + sinsp_view_info::viewtype vt = sinsp_view_info::T_TABLE; + vector columns; + vector actions; + vector tags; + vector tips; + string drilldown_target; + string spectro_type; + bool drilldown_increase_depth = false; + bool is_root = false; + bool propagate_filter = true; + + while(lua_next(ls, -2) != 0) + { + string fldname = lua_tostring(ls, -2); + + if(fldname == "name") + { + name = lua_tostring(ls, -1); + } + else if(fldname == "id") + { + id = lua_tostring(ls, -1); + } + else if(fldname == "description") + { + description = lua_tostring(ls, -1); + } + else if(fldname == "tags") + { + if(lua_istable(ls, -1)) + { + lua_pushnil(ls); + + while(lua_next(ls, -2) != 0) { - int sares = lua_toboolean(ls, -1); + if(lua_isstring(ls, -1)) + { + tmpstr = lua_tostring(ls, -1); + tags.push_back(tmpstr); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + "tags entries must be strings"); + } + + lua_pop(ls, 1); + } + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " is not a table"); + } + } + else if(fldname == "tips") + { + if(lua_istable(ls, -1)) + { + lua_pushnil(ls); - if(sares) + while(lua_next(ls, -2) != 0) + { + if(lua_isstring(ls, -1)) { - goto next_lua_file; + tmpstr = lua_tostring(ls, -1); + tips.push_back(tmpstr); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + "tips column entries must be strings"); } - } - // - // Extract the args - // - lua_getglobal(ls, "args"); + lua_pop(ls, 1); + } + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " is not a table"); + } + } + else if(fldname == "view_type") + { + tmpstr = lua_tostring(ls, -1); + + if(tmpstr == "table") + { + vt = sinsp_view_info::T_TABLE; + } + else if(tmpstr == "list") + { + vt = sinsp_view_info::T_LIST; + } + else if(tmpstr == "spectrogram") + { + vt = sinsp_view_info::T_SPECTRO; + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " must be either 'table' or 'list'"); + } + } + else if(fldname == "drilldown_target") + { + drilldown_target = lua_tostring(ls, -1); + } + else if(fldname == "spectro_type") + { + spectro_type = lua_tostring(ls, -1); + } + else if(fldname == "applies_to") + { + if(lua_istable(ls, -1)) + { + lua_pushnil(ls); - try + while(lua_next(ls, -2) != 0) { - parse_lua_chisel_args(ls, &cd); + if(lua_isstring(ls, -1)) + { + tmpstr = lua_tostring(ls, -1); + applies_to.push_back(tmpstr); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + "tips column entries must be strings"); + } + + lua_pop(ls, 1); } - catch(...) + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " is not a table"); + } + } + else if(fldname == "filter") + { + filter = lua_tostring(ls, -1); + } + else if(fldname == "use_defaults") + { + if(lua_isboolean(ls, -1)) + { + use_defaults = (lua_toboolean(ls, -1) != 0); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " must be a boolean"); + } + } + else if(fldname == "is_root") + { + if(lua_isboolean(ls, -1)) + { + is_root = (lua_toboolean(ls, -1) != 0); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " must be a boolean"); + } + } + else if(fldname == "columns") + { + if(lua_istable(ls, -1)) + { + parse_view_columns(ls, cd, &columns); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " is not a table"); + } + } + else if(fldname == "actions") + { + if(lua_istable(ls, -1)) + { + parse_view_actions(ls, cd, &actions); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " is not a table"); + } + } + else if(fldname == "drilldown_increase_depth") + { + if(lua_isboolean(ls, -1)) + { + drilldown_increase_depth = (lua_toboolean(ls, -1) != 0); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " must be a boolean"); + } + } + else if(fldname == "propagate_filter") + { + if(lua_isboolean(ls, -1)) + { + propagate_filter = (lua_toboolean(ls, -1) != 0); + } + else + { + throw sinsp_exception("error in view " + cd->m_name + ": " + string(lua_tostring(ls, -2)) + " must be a boolean"); + } + } + + lua_pop(ls, 1); + } + + cd->m_viewinfo = sinsp_view_info(vt, + id, + name, + description, + tags, + tips, + columns, + applies_to, + filter, + drilldown_target, + use_defaults, + is_root, + actions, + drilldown_increase_depth, + spectro_type, + propagate_filter); + + return true; +} + + +#ifdef HAS_LUA_CHISELS +// Initializes a lua chisel +bool sinsp_chisel::init_lua_chisel(chisel_desc &cd, string const &fpath) +{ + lua_State* ls = lua_open(); + if(ls == NULL) + { + return false; + } + + luaL_openlibs(ls); + + // + // Load our own lua libs + // + luaL_openlib(ls, "sysdig", ll_sysdig, 0); + luaL_openlib(ls, "chisel", ll_chisel, 0); + luaL_openlib(ls, "evt", ll_evt, 0); + + // + // Add our chisel paths to package.path + // + for(vector::const_iterator it = g_chisel_dirs->begin(); + it != g_chisel_dirs->end(); ++it) + { + string path(it->m_dir); + path += "?.lua"; + add_lua_package_path(ls, path.c_str()); + } + + // + // Load the script + // + if(luaL_loadfile(ls, fpath.c_str()) || lua_pcall(ls, 0, 0, 0)) + { + goto failure; + } + + // + // Extract the description + // + lua_getglobal(ls, "description"); + if(!lua_isstring(ls, -1)) + { + return parse_view_info(ls, &cd); + } + cd.m_description = lua_tostring(ls, -1); + + // + // Extract the short description + // + lua_getglobal(ls, "short_description"); + if(!lua_isstring(ls, -1)) + { + goto failure; + } + cd.m_shortdesc = lua_tostring(ls, -1); + + // + // Extract the category + // + cd.m_category = ""; + lua_getglobal(ls, "category"); + if(lua_isstring(ls, -1)) + { + cd.m_category = lua_tostring(ls, -1); + } + + // + // Extract the hidden flag and skip the chisel if it's set + // + lua_getglobal(ls, "hidden"); + if(lua_isboolean(ls, -1)) + { + int sares = lua_toboolean(ls, -1); + if(sares) + { + goto failure; + } + } + + // + // Extract the args + // + lua_getglobal(ls, "args"); + if(lua_isnoneornil(ls, -1)) + { + goto failure; + } + + try + { + parse_lua_chisel_args(ls, &cd); + } + catch(...) + { + goto failure; + } + + return true; + +failure: + lua_close(ls); + return false; +} +#endif + +struct filename +{ + bool valid; + string name; + string ext; +}; + +static filename split_filename(string const &fname) +{ + filename res; + string::size_type idx = fname.rfind('.'); + if(idx == std::string::npos) + { + res.valid = false; + } + else + { + res.valid = true; + res.name = fname.substr(0, idx); + res.ext = fname.substr(idx+1); + } + return res; +} + +// +// 1. Iterates through the chisel files on disk (.sc and .lua) +// 2. Opens them and extracts the fields (name, description, etc) +// 3. Adds them to the chisel_descs vector. +// +void sinsp_chisel::get_chisel_list(vector* chisel_descs) +{ + for(vector::const_iterator it = g_chisel_dirs->begin(); + it != g_chisel_dirs->end(); ++it) + { + if(string(it->m_dir).empty()) + { + continue; + } + + tinydir_dir dir = {}; + + tinydir_open(&dir, it->m_dir.c_str()); + + while(dir.has_next) + { + tinydir_file file; + tinydir_readfile(&dir, &file); + + string fpath(file.path); + bool add_to_vector = false; + chisel_desc cd; + + filename fn = split_filename(string(file.name)); + if(fn.ext != "sc" && fn.ext != "lua") + { + goto next_file; + } + + for(vector::const_iterator it_desc = chisel_descs->begin(); + it_desc != chisel_descs->end(); ++it_desc) + { + if(fn.name == it_desc->m_name) { - goto next_lua_file; + goto next_file; } + } + cd.m_name = fn.name; +#ifdef HAS_LUA_CHISELS + if(fn.ext == "lua") + { + add_to_vector = init_lua_chisel(cd, fpath); + } + + if(add_to_vector) + { chisel_descs->push_back(cd); -next_lua_file: - lua_close(ls); } #endif - next_file: tinydir_next(&dir); } @@ -1013,252 +1181,285 @@ void sinsp_chisel::load(string cmdstr) ifstream is; // - // Try to open the file as is + // Try to open the file with lua extension // - if(!openfile(m_filename, &is)) + if(!openfile(m_filename + ".lua", &is)) { // - // Try to add the .sc extension + // Try to open the file as is // - if(!openfile(m_filename + ".sc", &is)) + if(!openfile(m_filename, &is)) { - if(!openfile(m_filename + ".lua", &is)) - { - throw sinsp_exception("can't open file " + m_filename); - } + throw sinsp_exception("can't open file " + m_filename); } } +#ifdef HAS_LUA_CHISELS // - // Bring the file into a string + // Load the file // - string docstr((istreambuf_iterator(is)), - istreambuf_iterator()); + std::istreambuf_iterator eos; + std::string scriptstr(std::istreambuf_iterator(is), eos); // - // Try to parse as json + // Open the script // - if(m_root != NULL) - { - delete m_root; - } + m_ls = lua_open(); + + luaL_openlibs(m_ls); - m_root = new Json::Value(); + // + // Load our own lua libs + // + luaL_openlib(m_ls, "sysdig", ll_sysdig, 0); + luaL_openlib(m_ls, "chisel", ll_chisel, 0); + luaL_openlib(m_ls, "evt", ll_evt, 0); - Json::Reader reader; - bool parsingSuccessful = reader.parse(docstr, (*m_root)); - if(parsingSuccessful) + // + // Add our chisel paths to package.path + // + for(uint32_t j = 0; j < g_chisel_dirs->size(); j++) { - // - // Extract the info - // - m_description = (*m_root)["info"]["description"].asString(); + string path(g_chisel_dirs->at(j).m_dir); + path += "?.lua"; + add_lua_package_path(m_ls, path.c_str()); } - else - { -#ifdef HAS_LUA_CHISELS - // - // Rewind the stream - // - is.seekg(0); - - // - // Load the file - // - std::istreambuf_iterator eos; - std::string scriptstr(std::istreambuf_iterator(is), eos); - - // - // Open the script - // - m_ls = lua_open(); - - luaL_openlibs(m_ls); - // - // Load our own lua libs - // - luaL_openlib(m_ls, "sysdig", ll_sysdig, 0); - luaL_openlib(m_ls, "chisel", ll_chisel, 0); - luaL_openlib(m_ls, "evt", ll_evt, 0); - - // - // Add our chisel paths to package.path - // - for(uint32_t j = 0; j < g_chisel_dirs->size(); j++) - { - string path(g_chisel_dirs->at(j).m_dir); - path += "?.lua"; - add_lua_package_path(m_ls, path.c_str()); - } - - // - // Load the script - // - if(luaL_loadstring(m_ls, scriptstr.c_str()) || lua_pcall(m_ls, 0, 0, 0)) - { - throw sinsp_exception("Failed to load chisel " + - m_filename + ": " + lua_tostring(m_ls, -1)); - } + // + // Load the script + // + if(luaL_loadstring(m_ls, scriptstr.c_str()) || lua_pcall(m_ls, 0, 0, 0)) + { + throw sinsp_exception("Failed to load chisel " + + m_filename + ": " + lua_tostring(m_ls, -1)); + } - // - // Allocate the chisel context for the script - // - m_lua_cinfo = new chiselinfo(m_inspector); + // + // Allocate the chisel context for the script + // + m_lua_cinfo = new chiselinfo(m_inspector); - // - // Set the context globals - // - lua_pushlightuserdata(m_ls, this); - lua_setglobal(m_ls, "sichisel"); + // + // Set the context globals + // + lua_pushlightuserdata(m_ls, this); + lua_setglobal(m_ls, "sichisel"); - // - // Extract the args - // - lua_getglobal(m_ls, "args"); - if(!lua_istable(m_ls, -1)) - { - throw sinsp_exception("Failed to load chisel " + - m_filename + ": args table missing"); - } + // + // Extract the args + // + lua_getglobal(m_ls, "args"); + if(!lua_istable(m_ls, -1)) + { + throw sinsp_exception("Failed to load chisel " + + m_filename + ": args table missing"); + } - try - { - parse_lua_chisel_args(m_ls, &m_lua_script_info); - } - catch(sinsp_exception& e) - { - throw e; - } + parse_lua_chisel_args(m_ls, &m_lua_script_info); - // - // Check if the script has an on_event - // - lua_getglobal(m_ls, "on_event"); - if(lua_isfunction(m_ls, -1)) - { - m_lua_has_handle_evt = true; - lua_pop(m_ls, 1); - } -#endif + // + // Check if the script has an on_event + // + lua_getglobal(m_ls, "on_event"); + if(lua_isfunction(m_ls, -1)) + { + m_lua_has_handle_evt = true; + lua_pop(m_ls, 1); } +#endif is.close(); } uint32_t sinsp_chisel::get_n_args() { - if(!m_ls) - { - return (*m_root)["info"]["arguments"].size(); - } - else - { + ASSERT(m_ls); + #ifdef HAS_LUA_CHISELS - return m_lua_script_info.m_args.size(); + return (uint32_t)m_lua_script_info.m_args.size(); #else - return 0; + return 0; #endif - } } -void sinsp_chisel::set_args(vector* argvals) +uint32_t sinsp_chisel::get_n_optional_args() { - uint32_t j, k; - - m_argvals = *argvals; + uint32_t j; + uint32_t res = 0; - if(!m_ls) + for(j = 0; j < m_lua_script_info.m_args.size(); j++) { - const Json::Value args = (*m_root)["info"]["arguments"]; + if(m_lua_script_info.m_args[j].m_optional) + { + res++; + } + } - // - // Validate the arguments - // - if(m_argvals.size() != args.size()) + return res; +} + +uint32_t sinsp_chisel::get_n_required_args() +{ + uint32_t j; + uint32_t res = 0; + + for(j = 0; j < m_lua_script_info.m_args.size(); j++) + { + if(!m_lua_script_info.m_args[j].m_optional) { - throw sinsp_exception("wrong number of parameters for chisel " + m_filename); + res++; } + } - // - // Apply the arguments - // - const Json::Value clst = (*m_root)["chisels"]; - - for(j = 0; j < clst.size(); j++) + return res; +} + +void sinsp_chisel::set_args(string args) +{ +#ifdef HAS_LUA_CHISELS + uint32_t j; + uint32_t n_required_args = get_n_required_args(); + uint32_t n_optional_args = get_n_optional_args(); + + ASSERT(m_ls); + + // + // Split the argument string into tokens + // + uint32_t token_begin = 0; + bool inquotes = false; + uint32_t quote_correction = 0; + + trim(args); + + if(args.size() != 0) + { + for(j = 0; j < args.size(); j++) { - string filter = clst[j]["filter"].asString(); - for(k = 0; k < args.size(); k++) + if(args[j] == ' ' && !inquotes) { - replace_in_place(filter, - string("$") + args[k]["name"].asString(), - string(m_argvals[k])); + m_argvals.push_back(args.substr(token_begin, j - quote_correction - token_begin)); + token_begin = j + 1; + quote_correction = 0; } - - string formatter = clst[j]["format"].asString(); - for(k = 0; k < args.size(); k++) + else if(args[j] == '\'' || args[j] == '`') { - replace_in_place(formatter, - string("$") + args[k]["name"].asString(), - string(m_argvals[k])); + if(inquotes) + { + quote_correction = 1; + inquotes = false; + } + else { + token_begin++; + inquotes = true; + } } + } - chiselinfo* ci = new chiselinfo(m_inspector); - ci->init(filter, formatter); - m_subchisels.push_back(ci); + if(inquotes) + { + throw sinsp_exception("corrupted parameters for chisel " + m_filename); } + + m_argvals.push_back(args.substr(token_begin, j - quote_correction - token_begin)); } - else + + // + // Validate the arguments + // + if(m_argvals.size() < n_required_args) + { + throw sinsp_exception("wrong number of parameters for chisel " + m_filename + + ", " + to_string((long long int)n_required_args) + " required, " + + to_string((long long int)m_argvals.size()) + " given"); + } + else if(m_argvals.size() > n_optional_args + n_required_args) { + throw sinsp_exception("too many parameters for chisel " + m_filename + + ", " + to_string((long long int)(n_required_args)) + " required, " + + to_string((long long int)(n_optional_args)) + " optional, " + + to_string((long long int)m_argvals.size()) + " given"); + } + + // + // Create the arguments vector + // + vector> vargs; + + for(j = 0; j < m_argvals.size(); j++) + { + vargs.push_back(pair(m_lua_script_info.m_args[j].m_name, + m_argvals[j])); + } + + set_args(vargs); +#endif +} + +void sinsp_chisel::set_args(vector> args) +{ #ifdef HAS_LUA_CHISELS - // - // Validate the arguments - // - if(m_argvals.size() != m_lua_script_info.m_args.size()) + uint32_t j; + uint32_t n_required_args = get_n_required_args(); + uint32_t n_optional_args = get_n_optional_args(); + + ASSERT(m_ls); + + // + // Validate the arguments + // + if(args.size() < n_required_args) + { + throw sinsp_exception("wrong number of parameters for chisel " + m_filename + + ", " + to_string((long long int)n_required_args) + " required, " + + to_string((long long int)args.size()) + " given"); + } + else if(args.size() > n_optional_args + n_required_args) + { + throw sinsp_exception("too many parameters for chisel " + m_filename + + ", " + to_string((long long int)(n_required_args)) + " required, " + + to_string((long long int)(n_optional_args)) + " optional, " + + to_string((long long int)args.size()) + " given"); + } + + // + // Push the arguments + // + for(j = 0; j < args.size(); j++) + { + lua_getglobal(m_ls, "on_set_arg"); + if(!lua_isfunction(m_ls, -1)) { - throw sinsp_exception("wrong number of parameters for chisel " + m_filename); + lua_pop(m_ls, 1); + throw sinsp_exception("chisel " + m_filename + " misses a set_arg() function."); } + lua_pushstring(m_ls, args[j].first.c_str()); + lua_pushstring(m_ls, args[j].second.c_str()); // - // Push the arguments + // call get_info() // - for(k = 0; k < m_lua_script_info.m_args.size(); k++) + if(lua_pcall(m_ls, 2, 1, 0) != 0) { - lua_getglobal(m_ls, "on_set_arg"); - if(!lua_isfunction(m_ls, -1)) - { - lua_pop(m_ls, 1); - throw sinsp_exception("chisel " + m_filename + " misses a set_arg() function."); - } - - lua_pushstring(m_ls, m_lua_script_info.m_args[k].m_name.c_str()); - lua_pushstring(m_ls, m_argvals[k].c_str()); - - // - // call get_info() - // - if(lua_pcall(m_ls, 2, 1, 0) != 0) - { - throw sinsp_exception(m_filename + " chisel error: " + lua_tostring(m_ls, -1)); - } - - if(!lua_isboolean(m_ls, -1)) - { - throw sinsp_exception(m_filename + " chisel error: wrong set_arg() return value."); - } + throw sinsp_exception(m_filename + " chisel error: " + lua_tostring(m_ls, -1)); + } - int sares = lua_toboolean(m_ls, -1); + if(!lua_isboolean(m_ls, -1)) + { + throw sinsp_exception(m_filename + " chisel error: wrong set_arg() return value."); + } - if(!sares) - { - throw sinsp_exception("set_arg() for chisel " + m_filename + " failed."); - } + int sares = lua_toboolean(m_ls, -1); - lua_pop(m_ls, 1); + if(!sares) + { + throw sinsp_exception("set_arg() for chisel " + m_filename + " failed."); } -#endif + + lua_pop(m_ls, 1); } +#endif } void sinsp_chisel::on_init() @@ -1268,18 +1469,28 @@ void sinsp_chisel::on_init() // lua_getglobal(m_ls, "on_init"); - if(lua_pcall(m_ls, 0, 1, 0) != 0) + if(!lua_isfunction(m_ls, -1)) { // - // No on_init. + // No on_init. // That's ok. Just return. // return; } + if(lua_pcall(m_ls, 0, 1, 0) != 0) + { + // + // Exception running init + // + const char* lerr = lua_tostring(m_ls, -1); + string err = m_filename + ": error in init(): " + lerr; + throw sinsp_exception(err); + } + if(m_new_chisel_to_exec == "") { - if(!lua_isboolean(m_ls, -1)) + if(!lua_isboolean(m_ls, -1)) { throw sinsp_exception(m_filename + " chisel error: wrong init() return value."); } @@ -1301,118 +1512,133 @@ void sinsp_chisel::on_init() load(m_new_chisel_to_exec); m_new_chisel_to_exec = ""; - vector args = m_argvals; + string args; + for(uint32_t j = 0; j < m_argvals.size(); j++) + { + if(m_argvals[j].find(" ") == string::npos) + { + args += m_argvals[j]; + } + else + { + args += string("'") + m_argvals[j] + "'"; + } + + if(j < m_argvals.size() - 1) + { + args += " "; + } + } + m_argvals.clear(); - set_args(&args); + set_args(args); on_init(); } } +void sinsp_chisel::first_event_inits(sinsp_evt* evt) +{ + uint64_t ts = evt->get_ts(); + + if(m_lua_cinfo->m_callback_interval != 0) + { + m_lua_last_interval_sample_time = ts - ts % m_lua_cinfo->m_callback_interval; + } + else if(m_lua_cinfo->m_callback_precise_interval != 0) + { + m_lua_last_interval_sample_time = ts; + } + + m_lua_is_first_evt = false; +} + bool sinsp_chisel::run(sinsp_evt* evt) { - uint32_t j; +#ifdef HAS_LUA_CHISELS string line; - if(!m_ls) - { - for(j = 0; j < m_subchisels.size(); j++) - { - // - // Output the line - // - if(m_subchisels[j]->m_filter != NULL) - { - if(!m_subchisels[j]->m_filter->run(evt)) - { - continue; - } - } + ASSERT(m_ls); - if(m_subchisels[j]->m_formatter->tostring(evt, &line)) - { - cout << line << endl; - } - } + // + // Make the event available to the API + // + lua_pushlightuserdata(m_ls, evt); + lua_setglobal(m_ls, "sievt"); - return true; - } - else + // + // If there is a timeout callback, see if it's time to call it + // + do_timeout(evt); + + // + // If there is a filter, run it + // + if(m_lua_cinfo->m_filter != NULL) { -#ifdef HAS_LUA_CHISELS - // - // If this is the first event, put the event pointer on the stack. - // We assume that the event pointer will never change. - // - if(m_lua_is_first_evt) + if(!m_lua_cinfo->m_filter->run(evt)) { - lua_pushlightuserdata(m_ls, evt); - lua_setglobal(m_ls, "sievt"); + return false; + } + } - uint64_t ts = evt->get_ts(); - if(m_lua_cinfo->m_callback_interval != 0) - { - m_lua_last_interval_sample_time = ts - ts % m_lua_cinfo->m_callback_interval; - } + // + // If the script has the on_event callback, call it + // + if(m_lua_has_handle_evt) + { + lua_getglobal(m_ls, "on_event"); - m_lua_is_first_evt = false; + if(lua_pcall(m_ls, 0, 1, 0) != 0) + { + throw sinsp_exception(m_filename + " chisel error: " + lua_tostring(m_ls, -1)); } - // - // If there is a timeout callback, see if it's time to call it - // - do_timeout(evt); + int oeres = lua_toboolean(m_ls, -1); + lua_pop(m_ls, 1); - // - // If there is a filter, run it - // - if(m_lua_cinfo->m_filter != NULL) + if(m_lua_cinfo->m_end_capture == true) { - if(!m_lua_cinfo->m_filter->run(evt)) - { - return false; - } + throw sinsp_capture_interrupt_exception(); } - // - // If the script has the on_event callback, call it - // - if(m_lua_has_handle_evt) + if(oeres == false) { - lua_getglobal(m_ls, "on_event"); - - if(lua_pcall(m_ls, 0, 1, 0) != 0) - { - throw sinsp_exception(m_filename + " chisel error: " + lua_tostring(m_ls, -1)); - } - - int oeres = lua_toboolean(m_ls, -1); - lua_pop(m_ls, 1); - - if(oeres == false) - { - return false; - } + return false; } + } - // - // If the script has a formatter, run it - // - if(m_lua_cinfo->m_formatter != NULL) + // + // If the script has a formatter, run it + // + if(m_lua_cinfo->m_formatter != NULL) + { + if(m_lua_cinfo->m_formatter->tostring(evt, &line)) { - if(m_lua_cinfo->m_formatter->tostring(evt, &line)) - { - cout << line << endl; - } + cout << line << endl; } + } - return true; + return true; #endif - } } void sinsp_chisel::do_timeout(sinsp_evt* evt) { + if(m_lua_is_first_evt) + { + // + // If this is the first event, put the event pointer on the stack. + // We assume that the event pointer will never change. + // + if(m_lua_is_first_evt) + { + first_event_inits(evt); + } + + return; + } + if(m_lua_cinfo->m_callback_interval != 0) { uint64_t ts = evt->get_ts(); @@ -1425,20 +1651,23 @@ void sinsp_chisel::do_timeout(sinsp_evt* evt) if(m_lua_last_interval_ts != 0) { delta = ts - m_lua_last_interval_ts; - ASSERT(delta > 0); + if(delta == 0) + { + return; + } } lua_getglobal(m_ls, "on_interval"); - - lua_pushnumber(m_ls, (double)(ts / 1000000000)); - lua_pushnumber(m_ls, (double)(ts % 1000000000)); - lua_pushnumber(m_ls, (double)delta); - if(lua_pcall(m_ls, 3, 1, 0) != 0) + lua_pushnumber(m_ls, (double)(ts / 1000000000)); + lua_pushnumber(m_ls, (double)(ts % 1000000000)); + lua_pushnumber(m_ls, (double)delta); + + if(lua_pcall(m_ls, 3, 1, 0) != 0) { throw sinsp_exception(m_filename + " chisel error: calling on_interval() failed:" + lua_tostring(m_ls, -1)); } - + int oeres = lua_toboolean(m_ls, -1); lua_pop(m_ls, 1); @@ -1446,26 +1675,80 @@ void sinsp_chisel::do_timeout(sinsp_evt* evt) { throw sinsp_exception("execution terminated by the " + m_filename + " chisel"); } - + m_lua_last_interval_sample_time = sample_time; m_lua_last_interval_ts = ts; } } + else if(m_lua_cinfo->m_callback_precise_interval != 0) + { + uint64_t ts = evt->get_ts(); + uint64_t interval = m_lua_cinfo->m_callback_precise_interval; + + if(ts - m_lua_last_interval_sample_time >= interval) + { + uint64_t t; + + for(t = m_lua_last_interval_sample_time; t <= ts - interval; t += interval) + { + lua_getglobal(m_ls, "on_interval"); + + lua_pushnumber(m_ls, (double)(t / 1000000000)); + lua_pushnumber(m_ls, (double)(t % 1000000000)); + lua_pushnumber(m_ls, (double)interval); + + if(lua_pcall(m_ls, 3, 1, 0) != 0) + { + throw sinsp_exception(m_filename + " chisel error: calling on_interval() failed:" + lua_tostring(m_ls, -1)); + } + + int oeres = lua_toboolean(m_ls, -1); + lua_pop(m_ls, 1); + + if(oeres == false) + { + throw sinsp_exception("execution terminated by the " + m_filename + " chisel"); + } + } + + m_lua_last_interval_sample_time = t; + } + } +} + +void sinsp_chisel::do_end_of_sample() +{ +#ifdef HAS_LUA_CHISELS + lua_getglobal(m_ls, "on_end_of_sample"); + + if(lua_pcall(m_ls, 0, 1, 0) != 0) + { + throw sinsp_exception(m_filename + " chisel error: calling on_end_of_sample() failed:" + lua_tostring(m_ls, -1)); + } + + int oeres = lua_toboolean(m_ls, -1); + lua_pop(m_ls, 1); + + if(oeres == false) + { + throw sinsp_exception("execution terminated by the " + m_filename + " chisel"); + } +#endif // HAS_LUA_CHISELS } void sinsp_chisel::on_capture_start() { #ifdef HAS_LUA_CHISELS lua_getglobal(m_ls, "on_capture_start"); - + if(lua_isfunction(m_ls, -1)) { - if(lua_pcall(m_ls, 0, 1, 0) != 0) + if(lua_pcall(m_ls, 0, 1, 0) != 0) { throw sinsp_exception(m_filename + " chisel error: " + lua_tostring(m_ls, -1)); } - if(!lua_isboolean(m_ls, -1)) + if(!lua_isboolean(m_ls, -1)) { throw sinsp_exception(m_filename + " chisel error: wrong on_capture_start() return value. Boolean expected."); } @@ -1491,11 +1774,11 @@ void sinsp_chisel::on_capture_end() uint64_t te = m_inspector->m_lastevent_ts; int64_t delta = te - ts; - lua_pushnumber(m_ls, (double)(te / 1000000000)); - lua_pushnumber(m_ls, (double)(te % 1000000000)); + lua_pushnumber(m_ls, (double)(te / 1000000000)); + lua_pushnumber(m_ls, (double)(te % 1000000000)); lua_pushnumber(m_ls, (double)delta); - if(lua_pcall(m_ls, 3, 0, 0) != 0) + if(lua_pcall(m_ls, 3, 0, 0) != 0) { throw sinsp_exception(m_filename + " chisel error: " + lua_tostring(m_ls, -1)); } @@ -1505,4 +1788,12 @@ void sinsp_chisel::on_capture_end() #endif // HAS_LUA_CHISELS } +bool sinsp_chisel::get_nextrun_args(OUT string* args) +{ + ASSERT(m_lua_cinfo != NULL); + + *args = m_lua_cinfo->m_nextrun_args; + return m_lua_cinfo->m_has_nextrun_args; +} + #endif // HAS_CHISELS diff --git a/userspace/libsinsp/chisel.h b/userspace/libsinsp/chisel.h index 05986e96bb..70a2f55028 100644 --- a/userspace/libsinsp/chisel.h +++ b/userspace/libsinsp/chisel.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once @@ -22,9 +23,7 @@ along with sysdig. If not, see . class sinsp_filter_check; class sinsp_evt_formatter; -namespace Json { - class Value; -} +class sinsp_view_info; typedef struct lua_State lua_State; @@ -39,22 +38,24 @@ typedef struct lua_State lua_State; typedef struct chiseldir_info { bool m_need_to_resolve; - char m_dir[1024]; + std::string m_dir; }chiseldir_info; class chiselarg_desc { public: - chiselarg_desc(string name, string type, string description) + chiselarg_desc(string name, string type, string description, bool optional) { m_name = name; m_type = type; m_description = description; + m_optional = optional; } string m_name; string m_type; string m_description; + bool m_optional; }; class chisel_desc @@ -74,9 +75,9 @@ class chisel_desc string m_category; string m_shortdesc; vector m_args; + sinsp_view_info m_viewinfo; }; - class chiselinfo { public: @@ -85,11 +86,16 @@ class chiselinfo void set_filter(string filterstr); void set_formatter(string formatterstr); void set_callback_interval(uint64_t interval); + void set_callback_precise_interval(uint64_t interval); ~chiselinfo(); sinsp_filter* m_filter; sinsp_evt_formatter* m_formatter; sinsp_dumper* m_dumper; uint64_t m_callback_interval; + uint64_t m_callback_precise_interval; + bool m_has_nextrun_args; + string m_nextrun_args; + bool m_end_capture; private: sinsp* m_inspector; @@ -103,22 +109,41 @@ class SINSP_PUBLIC sinsp_chisel static void add_lua_package_path(lua_State* ls, const char* path); static void get_chisel_list(vector* chisel_descs); void load(string cmdstr); + string get_name() + { + return m_filename; + } uint32_t get_n_args(); - void set_args(vector* argvals); + uint32_t get_n_optional_args(); + uint32_t get_n_required_args(); + void set_args(string args); + void set_args(vector> args); bool run(sinsp_evt* evt); void do_timeout(sinsp_evt* evt); + void do_end_of_sample(); void on_init(); void on_capture_start(); void on_capture_end(); + bool get_nextrun_args(OUT string* args); + chisel_desc* get_lua_script_info() + { + return &m_lua_script_info; + } private: bool openfile(string filename, OUT ifstream* is); void free_lua_chisel(); + static sinsp_field_aggregation string_to_aggregation(string ag); + static void parse_view_column(lua_State *ls, OUT chisel_desc* cd, OUT void* columns); + static void parse_view_columns(lua_State *ls, OUT chisel_desc* cd, OUT void* columns); + static void parse_view_action(lua_State *ls, OUT chisel_desc* cd, OUT void* actions); + static void parse_view_actions(lua_State *ls, OUT chisel_desc* cd, OUT void* actions); + static bool parse_view_info(lua_State *ls, OUT chisel_desc* cd); + static bool init_lua_chisel(chisel_desc &cd, string const &path); + void first_event_inits(sinsp_evt* evt); sinsp* m_inspector; string m_description; - Json::Value* m_root; - vector m_subchisels; vector m_argvals; string m_filename; lua_State* m_ls; @@ -128,9 +153,11 @@ class SINSP_PUBLIC sinsp_chisel uint64_t m_lua_last_interval_sample_time; uint64_t m_lua_last_interval_ts; vector m_allocated_fltchecks; - char m_lua_fld_storage[1024]; + char m_lua_fld_storage[PPM_MAX_ARG_SIZE]; chiselinfo* m_lua_cinfo; string m_new_chisel_to_exec; + int m_udp_socket; + struct sockaddr_in m_serveraddr; friend class lua_cbacks; }; diff --git a/userspace/libsinsp/chisel_api.cpp b/userspace/libsinsp/chisel_api.cpp new file mode 100644 index 0000000000..4bd3cf624c --- /dev/null +++ b/userspace/libsinsp/chisel_api.cpp @@ -0,0 +1,1595 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include +#include +#ifdef _WIN32 +#include +#else +#include +#include +#include +#include +#endif +#include +#include + +#include "sinsp.h" +#include "sinsp_int.h" +#include "chisel.h" +#include "chisel_api.h" +#include "filter.h" +#include "filterchecks.h" +#ifdef HAS_ANALYZER +#include "analyzer.h" +#endif + +#ifdef HAS_CHISELS +#define HAS_LUA_CHISELS + +#ifdef HAS_LUA_CHISELS +extern "C" { +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +} +#endif + +extern vector* g_chisel_dirs; +extern sinsp_filter_check_list g_filterlist; +extern sinsp_evttables g_infotables; +void lua_stackdump(lua_State *L); + +/////////////////////////////////////////////////////////////////////////////// +// Lua callbacks +/////////////////////////////////////////////////////////////////////////////// +#ifdef HAS_LUA_CHISELS + +uint32_t lua_cbacks::rawval_to_lua_stack(lua_State *ls, uint8_t* rawval, ppm_param_type ptype, uint32_t len) +{ + ASSERT(rawval != NULL); + + switch(ptype) + { + case PT_INT8: + lua_pushnumber(ls, *(int8_t*)rawval); + return 1; + case PT_INT16: + lua_pushnumber(ls, *(int16_t*)rawval); + return 1; + case PT_INT32: + lua_pushnumber(ls, *(int32_t*)rawval); + return 1; + case PT_INT64: + case PT_ERRNO: + case PT_PID: + case PT_FD: + lua_pushnumber(ls, (double)*(int64_t*)rawval); + return 1; + case PT_L4PROTO: // This can be resolved in the future + case PT_FLAGS8: + case PT_UINT8: + lua_pushnumber(ls, *(uint8_t*)rawval); + return 1; + case PT_PORT: // This can be resolved in the future + case PT_FLAGS16: + case PT_UINT16: + lua_pushnumber(ls, *(uint16_t*)rawval); + return 1; + case PT_FLAGS32: + case PT_UINT32: + case PT_MODE: + case PT_UID: + case PT_GID: + lua_pushnumber(ls, *(uint32_t*)rawval); + return 1; + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + lua_pushnumber(ls, (double)*(uint64_t*)rawval); + return 1; + case PT_DOUBLE: + lua_pushnumber(ls, *(double*)rawval); + return 1; + case PT_CHARBUF: + case PT_FSPATH: + case PT_FSRELPATH: + lua_pushlstring(ls, (char*)rawval, len); + return 1; + case PT_BYTEBUF: + if(rawval[len] == 0) + { + lua_pushlstring(ls, (char*)rawval, len); + return 1; + } + else + { + lua_getglobal(ls, "sichisel"); + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + uint32_t max_len = len < sizeof(ch->m_lua_fld_storage) ? + len : sizeof(ch->m_lua_fld_storage) - 1; + + memcpy(ch->m_lua_fld_storage, rawval, max_len); + ch->m_lua_fld_storage[max_len] = 0; + lua_pushlstring(ls, (char*)ch->m_lua_fld_storage, max_len); + return 1; + } + case PT_SOCKADDR: + ASSERT(false); + return 0; + case PT_SOCKFAMILY: + ASSERT(false); + return 0; + case PT_BOOL: + lua_pushboolean(ls, (*(uint32_t*)rawval != 0)); + return 1; + case PT_IPV4ADDR: + { + lua_getglobal(ls, "sichisel"); + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + snprintf(ch->m_lua_fld_storage, + sizeof(ch->m_lua_fld_storage), + "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8, + rawval[0], + rawval[1], + rawval[2], + rawval[3]); + + lua_pushstring(ls, ch->m_lua_fld_storage); + return 1; + } + case PT_IPV6ADDR: + { + char address[100]; + ipv6addr *ip = (ipv6addr *) rawval; + + lua_getglobal(ls, "sichisel"); + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + if(NULL == inet_ntop(AF_INET6, ip->m_b, address, 100)) + { + strcpy(address, ""); + } + + strncpy(ch->m_lua_fld_storage, + address, + sizeof(ch->m_lua_fld_storage)); + + lua_pushstring(ls, ch->m_lua_fld_storage); + return 1; + } + case PT_IPADDR: + { + if(len == sizeof(struct in_addr)) + { + return rawval_to_lua_stack(ls, rawval, PT_IPV4ADDR, len); + } + else if(len == sizeof(struct in6_addr)) + { + return rawval_to_lua_stack(ls, rawval, PT_IPV6ADDR, len); + } + else + { + throw sinsp_exception("rawval_to_lua_stack called with IP address of incorrect size " + to_string(len)); + } + } + break; + default: + ASSERT(false); + string err = "wrong event type " + to_string((long long) ptype); + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } +} + +int lua_cbacks::get_num(lua_State *ls) +{ + lua_getglobal(ls, "sievt"); + sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + if(evt == NULL) + { + string err = "invalid call to evt.get_num()"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + lua_pushnumber(ls, (double)evt->get_num()); + return 1; +} + +int lua_cbacks::get_ts(lua_State *ls) +{ + lua_getglobal(ls, "sievt"); + sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + if(evt == NULL) + { + string err = "invalid call to evt.get_ts()"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + uint64_t ts = evt->get_ts(); + + lua_pushinteger(ls, (uint32_t)(ts / 1000000000)); + lua_pushinteger(ls, (uint32_t)(ts % 1000000000)); + return 2; +} + +int lua_cbacks::get_type(lua_State *ls) +{ + lua_getglobal(ls, "sievt"); + sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + if(evt == NULL) + { + string err = "invalid call to evt.get_type()"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + const char* evname; + uint16_t etype = evt->get_type(); + + if(etype == PPME_GENERIC_E || etype == PPME_GENERIC_X) + { + sinsp_evt_param *parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint16_t)); + uint16_t evid = *(uint16_t *)parinfo->m_val; + + evname = g_infotables.m_syscall_info_table[evid].name; + } + else + { + evname = evt->get_name(); + } + + lua_pushstring(ls, evname); + + return 1; +} + +int lua_cbacks::get_cpuid(lua_State *ls) +{ + lua_getglobal(ls, "sievt"); + sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + if(evt == NULL) + { + string err = "invalid call to evt.get_cpuid()"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + uint32_t cpuid = evt->get_cpuid(); + + lua_pushinteger(ls, cpuid); + return 1; +} + +int lua_cbacks::request_field(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + sinsp* inspector = ch->m_inspector; + + const char* fld = lua_tostring(ls, 1); + + if(fld == NULL) + { + string err = "chisel requesting nil field"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(fld, + inspector, + false); + + if(chk == NULL) + { + string err = "chisel requesting nonexistent field " + string(fld); + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + chk->parse_field_name(fld, true, false); + + lua_pushlightuserdata(ls, chk); + + ch->m_allocated_fltchecks.push_back(chk); + + return 1; +} + +int lua_cbacks::field(lua_State *ls) +{ + lua_getglobal(ls, "sievt"); + sinsp_evt* evt = (sinsp_evt*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + if(evt == NULL) + { + string err = "invalid call to evt.field()"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + sinsp_filter_check* chk = (sinsp_filter_check*)lua_topointer(ls, 1); + if(chk == NULL) + { + // + // This happens if the lua code is calling field() without invoking + // sysdig.request_field() before. + // + lua_pushnil(ls); + return 1; + } + + uint32_t vlen; + uint8_t* rawval = chk->extract(evt, &vlen); + + if(rawval != NULL) + { + return rawval_to_lua_stack(ls, rawval, chk->get_field_info()->m_type, vlen); + } + else + { + lua_pushnil(ls); + return 1; + } +} + +int lua_cbacks::set_global_filter(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + const char* filter = lua_tostring(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + try + { + ch->m_inspector->set_filter(filter); + } + catch(const sinsp_exception& e) + { + string err = "invalid filter in chisel " + ch->m_filename + ": " + e.what(); + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + return 0; +} + +int lua_cbacks::set_filter(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + const char* filter = lua_tostring(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + try + { + ch->m_lua_cinfo->set_filter(filter); + } + catch(const sinsp_exception& e) + { + string err = "invalid filter in chisel " + ch->m_filename + ": " + e.what(); + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + return 0; +} + +int lua_cbacks::set_snaplen(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + const uint32_t snaplen = (uint32_t)lua_tointeger(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_inspector->set_snaplen(snaplen); + + return 0; +} + +int lua_cbacks::set_output_format(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + if(ch->m_inspector->get_buffer_format() != sinsp_evt::PF_NORMAL) + { + // + // This means that the user has forced the format on the command line. + // We give that priority and we do nothing. + // + return 0; + } + + const char* fmt = lua_tostring(ls, 1); + + if(string(fmt) == "normal") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_NORMAL); + } + else if(string(fmt) == "json") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_JSON); + } + else if(string(fmt) == "simple") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_SIMPLE); + } + else if(string(fmt) == "hex") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_HEX); + } + else if(string(fmt) == "hexascii") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_HEXASCII); + } + else if(string(fmt) == "ascii") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_EOLS); + } + else if(string(fmt) == "base64") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_BASE64); + } + else if(string(fmt) == "jsonbase64") + { + ch->m_inspector->set_buffer_format(sinsp_evt::PF_JSONBASE64); + } + else + { + string err = "invalid set_output_format value in chisel " + ch->m_filename; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + return 0; +} + +int lua_cbacks::set_fatfile_dump_mode(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + int mode = lua_toboolean(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_inspector->set_fatfile_dump_mode(mode != 0); + + return 0; +} + +int lua_cbacks::make_ts(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + uint32_t op1 = (uint32_t)lua_tointeger(ls, 1); + lua_pop(ls, 1); + uint32_t op2 = (uint32_t)lua_tointeger(ls, 2); + lua_pop(ls, 1); + + uint64_t sum = (uint64_t)op1 * ONE_SECOND_IN_NS + op2; + + lua_pushstring(ls, to_string((long long) sum).c_str()); + return 1; +} + +int lua_cbacks::add_ts(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + uint64_t op1 = sinsp_numparser::parseu64(lua_tostring(ls, 1)); + lua_pop(ls, 1); + uint64_t op2 = sinsp_numparser::parseu64(lua_tostring(ls, 2)); + lua_pop(ls, 1); + + uint64_t sum = (op1 + op2); + + lua_pushstring(ls, to_string((long long) sum).c_str()); + return 1; +} + +int lua_cbacks::subtract_ts(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + uint64_t op1 = sinsp_numparser::parseu64(lua_tostring(ls, 1)); + lua_pop(ls, 1); + uint64_t op2 = sinsp_numparser::parseu64(lua_tostring(ls, 2)); + lua_pop(ls, 1); + + uint64_t sum = (op1 - op2); + + lua_pushstring(ls, to_string((long long) sum).c_str()); + return 1; +} + +int lua_cbacks::run_sysdig(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + const char* args = lua_tostring(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_lua_cinfo->m_has_nextrun_args = true; + ch->m_lua_cinfo->m_nextrun_args = args; + + return 0; +} + +int lua_cbacks::end_capture(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_lua_cinfo->m_end_capture = true; + + return 0; +} + +int lua_cbacks::is_live(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + lua_pushboolean(ls, ch->m_inspector->is_live()); + return 1; +} + +int lua_cbacks::is_tty(lua_State *ls) +{ +#ifdef _WIN32 + int use_color = false; +#else + int use_color = isatty(1); +#endif + + lua_pushboolean(ls, use_color); + return 1; +} + +int lua_cbacks::get_terminal_info(lua_State *ls) +{ + int32_t width = -1; + int32_t height = -1; +#ifndef _WIN32 + struct winsize w; + + if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) + { + width = w.ws_col; + height = w.ws_row; + } +#endif + + lua_newtable(ls); + lua_pushstring(ls, "width"); + lua_pushnumber(ls, width); + lua_settable(ls, -3); + lua_pushstring(ls, "height"); + lua_pushnumber(ls, height); + lua_settable(ls, -3); + + return 1; +} + +int lua_cbacks::get_filter(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_inspector); + + string flts = ch->m_inspector->get_filter(); + + lua_pushstring(ls, flts.c_str()); + + return 1; +} + +int lua_cbacks::get_machine_info(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + const scap_machine_info* minfo = ch->m_inspector->get_machine_info(); + + if(minfo == NULL) + { + string err = "get_machine_info can only be called from the on_capture_start callback"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + lua_newtable(ls); + lua_pushstring(ls, "num_cpus"); + lua_pushnumber(ls, minfo->num_cpus); + lua_settable(ls, -3); + lua_pushstring(ls, "memory_size_bytes"); + lua_pushnumber(ls, (double)minfo->memory_size_bytes); + lua_settable(ls, -3); + lua_pushstring(ls, "max_pid"); + lua_pushnumber(ls, (double)minfo->max_pid); + lua_settable(ls, -3); + lua_pushstring(ls, "hostname"); + lua_pushstring(ls, minfo->hostname); + lua_settable(ls, -3); + + return 1; +} + +int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool barebone) +{ + unordered_map::iterator fdit; + uint32_t j; + sinsp_filter_compiler* compiler = NULL; + sinsp_filter* filter = NULL; + sinsp_evt tevt; + scap_evt tscapevt; + + // + // Get the chisel state + // + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + ASSERT(ch->m_inspector); + + // + // If the caller specified a filter, compile it + // + if(lua_isstring(ls, 1)) + { + string filterstr = lua_tostring(ls, 1); + lua_pop(ls, 1); + + if(filterstr != "") + { + try + { + compiler = new sinsp_filter_compiler(ch->m_inspector, filterstr, true); + filter = compiler->compile(); + } + catch(const sinsp_exception& e) + { + string err = "invalid filter argument for get_thread_table in chisel " + ch->m_filename + ": " + e.what(); + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + tscapevt.ts = ch->m_inspector->m_lastevent_ts; + tscapevt.type = PPME_SYSCALL_READ_X; + tscapevt.len = 0; + tscapevt.nparams = 0; + + tevt.m_inspector = ch->m_inspector; + tevt.m_info = &(g_infotables.m_event_info[PPME_SYSCALL_READ_X]); + tevt.m_pevt = NULL; + tevt.m_cpuid = 0; + tevt.m_evtnum = 0; + tevt.m_pevt = &tscapevt; + } + } + + threadinfo_map_t* threadtable = ch->m_inspector->m_thread_manager->get_threads(); + + ASSERT(threadtable != NULL); + + lua_newtable(ls); + + threadtable->loop([&] (sinsp_threadinfo& tinfo) { + // + // Check if there's at least an fd that matches the filter. + // If not, skip this thread + // + sinsp_fdtable* fdtable = tinfo.get_fd_table(); + + if(filter != NULL) + { + bool match = false; + + for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit) + { + tevt.m_tinfo = &tinfo; + tevt.m_fdinfo = &(fdit->second); + tscapevt.tid = tinfo.m_tid; + int64_t tlefd = tevt.m_tinfo->m_lastevent_fd; + tevt.m_tinfo->m_lastevent_fd = fdit->first; + + if(filter->run(&tevt)) + { + match = true; + break; + } + + tevt.m_tinfo->m_lastevent_fd = tlefd; + } + + if(!match) + { + return true; + } + } + + // + // Set the thread properties + // + lua_newtable(ls); + lua_pushliteral(ls, "tid"); + lua_pushnumber(ls, (uint32_t)tinfo.m_tid); + lua_settable(ls, -3); + lua_pushliteral(ls, "pid"); + lua_pushnumber(ls, (uint32_t)tinfo.m_pid); + lua_settable(ls, -3); + if(!barebone) + { + lua_pushliteral(ls, "ptid"); + lua_pushnumber(ls, (uint32_t)tinfo.m_ptid); + lua_settable(ls, -3); + lua_pushliteral(ls, "comm"); + lua_pushstring(ls, tinfo.m_comm.c_str()); + lua_settable(ls, -3); + lua_pushliteral(ls, "exe"); + lua_pushstring(ls, tinfo.m_exe.c_str()); + lua_settable(ls, -3); + lua_pushliteral(ls, "flags"); + lua_pushnumber(ls, (uint32_t)tinfo.m_flags); + lua_settable(ls, -3); + lua_pushliteral(ls, "fdlimit"); + lua_pushnumber(ls, (uint32_t)tinfo.m_fdlimit); + lua_settable(ls, -3); + lua_pushliteral(ls, "uid"); + lua_pushnumber(ls, (uint32_t)tinfo.m_uid); + lua_settable(ls, -3); + lua_pushliteral(ls, "gid"); + lua_pushnumber(ls, (uint32_t)tinfo.m_gid); + lua_settable(ls, -3); + lua_pushliteral(ls, "nchilds"); + lua_pushnumber(ls, (uint32_t)tinfo.m_nchilds); + lua_settable(ls, -3); + lua_pushliteral(ls, "vmsize_kb"); + lua_pushnumber(ls, (uint32_t)tinfo.m_vmsize_kb); + lua_settable(ls, -3); + lua_pushliteral(ls, "vmrss_kb"); + lua_pushnumber(ls, (uint32_t)tinfo.m_vmrss_kb); + lua_settable(ls, -3); + lua_pushliteral(ls, "vmswap_kb"); + lua_pushnumber(ls, (uint32_t)tinfo.m_vmswap_kb); + lua_settable(ls, -3); + lua_pushliteral(ls, "pfmajor"); + lua_pushnumber(ls, (uint32_t)tinfo.m_pfmajor); + lua_settable(ls, -3); + lua_pushliteral(ls, "pfminor"); + lua_pushnumber(ls, (uint32_t)tinfo.m_pfminor); + lua_settable(ls, -3); + lua_pushliteral(ls, "clone_ts"); + lua_pushstring(ls, to_string((long long int)tinfo.m_clone_ts).c_str()); + lua_settable(ls, -3); + + // + // Extract the user name + // + string username; + unordered_map::const_iterator uit; + + const unordered_map* userlist = ch->m_inspector->get_userlist(); + ASSERT(userlist->size() != 0); + + if(tinfo.m_uid == 0xffffffff) + { + username = ""; + } + else + { + uit = userlist->find(tinfo.m_uid); + if(uit == userlist->end()) + { + username = ""; + } + else + { + ASSERT(uit->second != NULL); + username = uit->second->name; + } + } + + lua_pushliteral(ls, "username"); + lua_pushstring(ls, username.c_str()); + lua_settable(ls, -3); + + // + // Create the arguments sub-table + // + lua_pushstring(ls, "args"); + + vector* args = &tinfo.m_args; + lua_newtable(ls); + for(j = 0; j < args->size(); j++) + { + lua_pushinteger(ls, j + 1); + lua_pushstring(ls, args->at(j).c_str()); + lua_settable(ls, -3); + } + lua_settable(ls,-3); + + // + // Create the environment variables sub-table + // + lua_pushstring(ls, "env"); + + const auto& env = tinfo.get_env(); + lua_newtable(ls); + for(j = 0; j < env.size(); j++) + { + lua_pushinteger(ls, j + 1); + lua_pushstring(ls, env.at(j).c_str()); + lua_settable(ls, -3); + } + lua_settable(ls,-3); + } + + // + // Create and populate the FD table + // + lua_pushstring(ls, "fdtable"); + lua_newtable(ls); + + if(include_fds) + { + for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit) + { + tevt.m_tinfo = &tinfo; + tevt.m_fdinfo = &(fdit->second); + tscapevt.tid = tinfo.m_tid; + int64_t tlefd = tevt.m_tinfo->m_lastevent_fd; + tevt.m_tinfo->m_lastevent_fd = fdit->first; + + if(filter != NULL) + { + if(filter->run(&tevt) == false) + { + continue; + } + } + + tevt.m_tinfo->m_lastevent_fd = tlefd; + + lua_newtable(ls); + if(!barebone) + { + lua_pushliteral(ls, "name"); + lua_pushstring(ls, fdit->second.tostring_clean().c_str()); + lua_settable(ls, -3); + lua_pushliteral(ls, "type"); + lua_pushstring(ls, fdit->second.get_typestring()); + lua_settable(ls, -3); + } + + scap_fd_type evt_type = fdit->second.m_type; + if(evt_type == SCAP_FD_IPV4_SOCK || evt_type == SCAP_FD_IPV4_SERVSOCK || + evt_type == SCAP_FD_IPV6_SOCK || evt_type == SCAP_FD_IPV6_SERVSOCK) + { + bool include_client; + char sipbuf[128], cipbuf[128]; + uint8_t *sip, *cip; + uint16_t sport, cport; + bool is_server; + int af; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + include_client = true; + af = AF_INET; + cip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4info.m_fields.m_sip); + sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4info.m_fields.m_dip); + cport = fdit->second.m_sockinfo.m_ipv4info.m_fields.m_sport; + sport = fdit->second.m_sockinfo.m_ipv4info.m_fields.m_dport; + is_server = fdit->second.is_role_server(); + } + else if (evt_type == SCAP_FD_IPV4_SERVSOCK) + { + include_client = false; + af = AF_INET; + cip = NULL; + sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4serverinfo.m_ip); + sport = fdit->second.m_sockinfo.m_ipv4serverinfo.m_port; + is_server = true; + } + else if (evt_type == SCAP_FD_IPV6_SOCK) + { + include_client = true; + af = AF_INET6; + cip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6info.m_fields.m_sip); + sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6info.m_fields.m_dip); + cport = fdit->second.m_sockinfo.m_ipv6info.m_fields.m_sport; + sport = fdit->second.m_sockinfo.m_ipv6info.m_fields.m_dport; + is_server = fdit->second.is_role_server(); + } + else + { + include_client = false; + af = AF_INET6; + cip = NULL; + sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6serverinfo.m_ip); + sport = fdit->second.m_sockinfo.m_ipv6serverinfo.m_port; + is_server = true; + } + + // Now convert the raw sip/cip to strings + if(NULL == inet_ntop(af, sip, sipbuf, sizeof(sipbuf))) + { + strcpy(sipbuf, ""); + } + + if(cip) + { + if(NULL == inet_ntop(af, cip, cipbuf, sizeof(cipbuf))) + { + strcpy(cipbuf, ""); + } + } + + if(include_client) + { + // cip + lua_pushliteral(ls, "cip"); + lua_pushstring(ls, cipbuf); + lua_settable(ls, -3); + } + + // sip + lua_pushliteral(ls, "sip"); + lua_pushstring(ls, sipbuf); + lua_settable(ls, -3); + + if(include_client) + { + // cport + lua_pushliteral(ls, "cport"); + lua_pushnumber(ls, cport); + lua_settable(ls, -3); + } + + // sport + lua_pushliteral(ls, "sport"); + lua_pushnumber(ls, sport); + lua_settable(ls, -3); + + // is_server + lua_pushliteral(ls, "is_server"); + lua_pushboolean(ls, is_server); + lua_settable(ls, -3); + + // l4proto + const char* l4ps; + scap_l4_proto l4p = fdit->second.get_l4proto(); + + switch(l4p) + { + case SCAP_L4_TCP: + l4ps = "tcp"; + break; + case SCAP_L4_UDP: + l4ps = "udp"; + break; + case SCAP_L4_ICMP: + l4ps = "icmp"; + break; + case SCAP_L4_RAW: + l4ps = "raw"; + break; + default: + l4ps = ""; + break; + } + + // l4proto + lua_pushliteral(ls, "l4proto"); + lua_pushstring(ls, l4ps); + lua_settable(ls, -3); + } + + // is_server + string l4proto; + + lua_rawseti(ls,-2, (uint32_t)fdit->first); + } + } + + + lua_settable(ls,-3); + + // + // Set the key for this entry + // + lua_rawseti(ls,-2, (uint32_t)tinfo.m_tid); + return true; + }); + + if(filter) + { + delete filter; + } + + return 1; +} + +int lua_cbacks::get_thread_table(lua_State *ls) +{ + return get_thread_table_int(ls, true, false); +} + +int lua_cbacks::get_thread_table_nofds(lua_State *ls) +{ + return get_thread_table_int(ls, false, false); +} + +int lua_cbacks::get_thread_table_barebone(lua_State *ls) +{ + return get_thread_table_int(ls, true, true); +} + +int lua_cbacks::get_thread_table_barebone_nofds(lua_State *ls) +{ + return get_thread_table_int(ls, false, true); +} + +int lua_cbacks::get_container_table(lua_State *ls) +{ +#ifndef _WIN32 + unordered_map::iterator fdit; + uint32_t j; + sinsp_evt tevt; + + // + // Get the chisel state + // + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + ASSERT(ch->m_inspector); + + // + // Retrieve the container list + // + const sinsp_container_manager::map_ptr_t ctable = ch->m_inspector->m_container_manager.get_containers(); + + lua_newtable(ls); + + // + // Go through the list + // + j = 0; + for(auto it = ctable->begin(); it != ctable->end(); ++it) + { + lua_newtable(ls); + lua_pushliteral(ls, "id"); + lua_pushstring(ls, it->second->m_id.c_str()); + lua_settable(ls, -3); + lua_pushliteral(ls, "name"); + lua_pushstring(ls, it->second->m_name.c_str()); + lua_settable(ls, -3); + lua_pushliteral(ls, "image"); + lua_pushstring(ls, it->second->m_image.c_str()); + lua_settable(ls, -3); + + lua_pushliteral(ls, "type"); + if(it->second->m_type == CT_DOCKER) + { + lua_pushstring(ls, "docker"); + } + else if(it->second->m_type == CT_LXC) + { + lua_pushstring(ls, "lxc"); + } + else if(it->second->m_type == CT_LIBVIRT_LXC) + { + lua_pushstring(ls, "libvirt_lxc"); + } + else if(it->second->m_type == CT_MESOS) + { + lua_pushstring(ls, "mesos"); + } + else if(it->second->m_type == CT_RKT) + { + lua_pushstring(ls, "rkt"); + } + else if(it->second->m_type == CT_CRI) + { + lua_pushstring(ls, "cri"); + } + else if(it->second->m_type == CT_CONTAINERD) + { + lua_pushstring(ls, "containerd"); + } + else if(it->second->m_type == CT_CRIO) + { + lua_pushstring(ls, "cri-o"); + } + else if(it->second->m_type == CT_BPM) + { + lua_pushstring(ls, "bpm"); + } + else + { + ASSERT(false); + lua_pushstring(ls, "unknown"); + } + lua_settable(ls, -3); + + // + // Set the key for this entry + // + lua_rawseti(ls,-2, (uint32_t)++j); + } + +#endif + return 1; +} + +int lua_cbacks::is_print_container_data(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + lua_pushboolean(ls, ch->m_inspector->is_print_container_data()); + return 1; +} + + +int lua_cbacks::get_output_format(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + sinsp_evt::param_fmt fmt = ch->m_inspector->get_buffer_format(); + + if(fmt & sinsp_evt::PF_JSON) + { + lua_pushstring(ls, "json"); + } + else + { + lua_pushstring(ls, "normal"); + } + + return 1; +} + +int lua_cbacks::get_evtsource_name(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + if(ch->m_inspector->is_live()) + { + lua_pushstring(ls, ""); + } + else + { + lua_pushstring(ls, ch->m_inspector->get_input_filename().c_str()); + } + + return 1; +} + +int lua_cbacks::get_firstevent_ts(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + lua_pushstring(ls, to_string(ch->m_inspector->m_firstevent_ts).c_str()); + + return 1; +} + +int lua_cbacks::get_lastevent_ts(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + lua_pushstring(ls, to_string(ch->m_inspector->m_lastevent_ts).c_str()); + + return 1; +} + +int lua_cbacks::set_event_formatter(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + const char* formatter = lua_tostring(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_lua_cinfo->set_formatter(formatter); + + return 0; +} + +int lua_cbacks::set_interval_ns(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + uint64_t interval = (uint64_t)lua_tonumber(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_lua_cinfo->set_callback_interval(interval); + + return 0; +} + +int lua_cbacks::set_interval_s(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + uint64_t interval = (uint64_t)lua_tonumber(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_lua_cinfo->set_callback_interval(interval * 1000000000); + + return 0; +} + +int lua_cbacks::set_precise_interval_ns(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + uint64_t interval = (uint64_t)lua_tonumber(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + ch->m_lua_cinfo->set_callback_precise_interval(interval); + + return 0; +} + +int lua_cbacks::exec(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + const char* chname = lua_tostring(ls, 1); + if(chname == NULL) + { + string err = "invalid exec field name in chisel " + ch->m_filename; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + ch->m_new_chisel_to_exec = chname; + + ch->m_argvals.clear(); + uint32_t stackpos = 2; + + while(true) + { + const char* argval = lua_tostring(ls, stackpos++); + if(argval == NULL) + { + break; + } + + ch->m_argvals.push_back(argval); + } + + return 0; +} + +int lua_cbacks::log(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + string message(lua_tostring(ls, 1)); + string sevstr(lua_tostring(ls, 2)); + + sinsp_logger::severity sevcode = sinsp_logger::SEV_INFO; + + if(sevstr == "debug") + { + sevcode = sinsp_logger::SEV_DEBUG; + } + else if(sevstr == "info") + { + sevcode = sinsp_logger::SEV_INFO; + } + else if(sevstr == "warning") + { + sevcode = sinsp_logger::SEV_WARNING; + } + else if(sevstr == "error") + { + sevcode = sinsp_logger::SEV_ERROR; + } + else if(sevstr == "critical") + { + sevcode = sinsp_logger::SEV_CRITICAL; + } + + g_logger.log(message, sevcode); + + return 0; +} + +int lua_cbacks::udp_setpeername(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + + string addr(lua_tostring(ls, 1)); + string ports(lua_tostring(ls, 2)); + uint16_t port = htons(sinsp_numparser::parseu16(ports)); + + ch->m_udp_socket = socket(AF_INET, SOCK_DGRAM, 0); + if(ch->m_udp_socket < 0) + { + string err = "udp_setpeername error: unable to create the socket"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + memset(&ch->m_serveraddr, 0, sizeof(ch->m_serveraddr)); + ch->m_serveraddr.sin_family = AF_INET; + ch->m_serveraddr.sin_port = port; + if(inet_pton(AF_INET, addr.c_str(), &ch->m_serveraddr.sin_addr) <= 0) + { + string err = "inet_pton error occurred"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + return 0; +} + +int lua_cbacks::udp_send(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + + string message(lua_tostring(ls, 1)); + + if(sendto(ch->m_udp_socket, message.c_str(), message.size(), 0, + (struct sockaddr *)&ch->m_serveraddr, sizeof(ch->m_serveraddr)) < 0) + { + string err = "udp_send error: cannot send the buffer: "; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + return 0; +} + +int lua_cbacks::get_read_progress(lua_State *ls) +{ + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_inspector); + + double pr = ch->m_inspector->get_read_progress(); + + lua_pushnumber(ls, pr); + + return 1; +} + +#ifdef HAS_ANALYZER +int lua_cbacks::push_metric(lua_State *ls) +{ + statsd_metric metric; + metric.m_type = statsd_metric::type_t::GAUGE; + + lua_getglobal(ls, "sichisel"); + + sinsp_chisel* ch = (sinsp_chisel*)lua_touserdata(ls, -1); + lua_pop(ls, 1); + + ASSERT(ch); + ASSERT(ch->m_lua_cinfo); + + sinsp* inspector = ch->m_inspector; + + // + // tags + // + if(lua_istable(ls, 3)) + { + lua_pushnil(ls); + + while(lua_next(ls, 3) != 0) + { + string tag = lua_tostring(ls, -1); + metric.m_tags[tag] = ""; + lua_pop(ls, 1); + } + + lua_pop(ls, 1); + } + else + { + string err = "error in chisel " + ch->m_filename + ": third argument must be a table"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + // + // Name + // + if(lua_isstring(ls, 1)) + { + metric.m_name = lua_tostring(ls, 1); + } + else + { + string err = "errord in chisel " + ch->m_filename + ": first argument must be a string"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + // + // Value + // + if(lua_isnumber(ls, 2)) + { + metric.m_value = lua_tonumber(ls, 2); + } + else + { + string err = "errord in chisel " + ch->m_filename + ": second argument must be a number"; + fprintf(stderr, "%s\n", err.c_str()); + throw sinsp_exception("chisel error"); + } + + if (inspector->m_external_event_processor) + { + inspector->m_external_event_processor->add_chisel_metric(&metric); + } + + return 0; +} + +#endif // HAS_ANALYZER +#endif // HAS_LUA_CHISELS +#endif // HAS_CHISELS diff --git a/userspace/libsinsp/chisel_api.h b/userspace/libsinsp/chisel_api.h new file mode 100644 index 0000000000..1816eb4e3b --- /dev/null +++ b/userspace/libsinsp/chisel_api.h @@ -0,0 +1,77 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#ifdef HAS_CHISELS + +class lua_cbacks +{ +public: + static uint32_t rawval_to_lua_stack(lua_State *ls, uint8_t* rawval, ppm_param_type ptype, uint32_t len); + + static int get_num(lua_State *ls); + static int get_ts(lua_State *ls); + static int get_type(lua_State *ls); + static int get_cpuid(lua_State *ls); + static int request_field(lua_State *ls); + static int field(lua_State *ls); + static int set_global_filter(lua_State *ls); + static int set_filter(lua_State *ls); + static int set_snaplen(lua_State *ls); + static int set_output_format(lua_State *ls); + static int set_fatfile_dump_mode(lua_State *ls); + static int make_ts(lua_State *ls); + static int add_ts(lua_State *ls); + static int subtract_ts(lua_State *ls); + static int run_sysdig(lua_State *ls); + static int end_capture(lua_State *ls); + static int is_live(lua_State *ls); + static int is_tty(lua_State *ls); + static int get_terminal_info(lua_State *ls); + static int get_filter(lua_State *ls); + static int get_machine_info(lua_State *ls); + static int get_thread_table(lua_State *ls); + static int get_thread_table_nofds(lua_State *ls); + static int get_thread_table_barebone(lua_State *ls); + static int get_thread_table_barebone_nofds(lua_State *ls); + static int get_container_table(lua_State *ls); + static int is_print_container_data(lua_State *ls); + static int get_output_format(lua_State *ls); + static int get_evtsource_name(lua_State *ls); + static int get_firstevent_ts(lua_State *ls); + static int get_lastevent_ts(lua_State *ls); + static int set_event_formatter(lua_State *ls); + static int set_interval_ns(lua_State *ls); + static int set_interval_s(lua_State *ls); + static int set_precise_interval_ns(lua_State *ls); + static int exec(lua_State *ls); + static int log(lua_State *ls); + static int udp_setpeername(lua_State *ls); + static int udp_send(lua_State *ls); + static int get_read_progress(lua_State *ls); +#ifdef HAS_ANALYZER + static int push_metric(lua_State *ls); +#endif +private: + static int get_thread_table_int(lua_State *ls, bool include_fds, bool barebone); +}; + +#endif // HAS_CHISELS + diff --git a/userspace/libsinsp/cmake/patch/musl-cri.proto.patch b/userspace/libsinsp/cmake/patch/musl-cri.proto.patch new file mode 100644 index 0000000000..fceb0d0ffa --- /dev/null +++ b/userspace/libsinsp/cmake/patch/musl-cri.proto.patch @@ -0,0 +1,68 @@ +diff --git a/userspace/libsinsp/cri.proto b/userspace/libsinsp/cri.proto +index 6e060c63..a7b5c694 100644 +--- a/userspace/libsinsp/cri.proto ++++ b/userspace/libsinsp/cri.proto +@@ -727,7 +727,7 @@ message ContainerConfig { + // use-cases (e.g. debugging). + // TODO: Determine if we need to continue supporting these fields that are + // part of Kubernetes's Container Spec. +- bool stdin = 12; ++ bool _stdin = 12; + bool stdin_once = 13; + bool tty = 14; + +@@ -919,9 +919,9 @@ message ExecSyncRequest { + + message ExecSyncResponse { + // Captured command stdout output. +- bytes stdout = 1; ++ bytes _stdout = 1; + // Captured command stderr output. +- bytes stderr = 2; ++ bytes _stderr = 2; + // Exit code the command finished with. Default: 0 (success). + int32 exit_code = 3; + } +@@ -935,16 +935,16 @@ message ExecRequest { + bool tty = 3; + // Whether to stream stdin. + // One of `stdin`, `stdout`, and `stderr` MUST be true. +- bool stdin = 4; ++ bool _stdin = 4; + // Whether to stream stdout. + // One of `stdin`, `stdout`, and `stderr` MUST be true. +- bool stdout = 5; ++ bool _stdout = 5; + // Whether to stream stderr. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + // If `tty` is true, `stderr` MUST be false. Multiplexing is not supported + // in this case. The output of stdout and stderr will be combined to a + // single stream. +- bool stderr = 6; ++ bool _stderr = 6; + } + + message ExecResponse { +@@ -957,19 +957,19 @@ message AttachRequest { + string container_id = 1; + // Whether to stream stdin. + // One of `stdin`, `stdout`, and `stderr` MUST be true. +- bool stdin = 2; ++ bool _stdin = 2; + // Whether the process being attached is running in a TTY. + // This must match the TTY setting in the ContainerConfig. + bool tty = 3; + // Whether to stream stdout. + // One of `stdin`, `stdout`, and `stderr` MUST be true. +- bool stdout = 4; ++ bool _stdout = 4; + // Whether to stream stderr. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + // If `tty` is true, `stderr` MUST be false. Multiplexing is not supported + // in this case. The output of stdout and stderr will be combined to a + // single stream. +- bool stderr = 5; ++ bool _stderr = 5; + } + + message AttachResponse { diff --git a/userspace/libsinsp/container.cpp b/userspace/libsinsp/container.cpp new file mode 100644 index 0000000000..32ab31033c --- /dev/null +++ b/userspace/libsinsp/container.cpp @@ -0,0 +1,640 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 + +#ifndef MINIMAL_BUILD +#ifdef HAS_CAPTURE +#include "container_engine/cri.h" +#endif // HAS_CAPTURE +#include "container_engine/docker.h" +#include "container_engine/rkt.h" +#include "container_engine/libvirt_lxc.h" +#include "container_engine/lxc.h" +#include "container_engine/mesos.h" +#include "container_engine/bpm.h" +#endif // MINIMAL_BUILD + +#include "sinsp.h" +#include "sinsp_int.h" +#include "container.h" +#include "utils.h" + +using namespace libsinsp; + +sinsp_container_manager::sinsp_container_manager(sinsp* inspector) : + m_inspector(inspector), + m_last_flush_time_ns(0) +{ +} + +sinsp_container_manager::~sinsp_container_manager() +{ +} + +bool sinsp_container_manager::remove_inactive_containers() +{ + bool res = false; + + if(m_last_flush_time_ns == 0) + { + m_last_flush_time_ns = m_inspector->m_lastevent_ts - m_inspector->m_inactive_container_scan_time_ns + 30 * ONE_SECOND_IN_NS; + } + + if(m_inspector->m_lastevent_ts > + m_last_flush_time_ns + m_inspector->m_inactive_container_scan_time_ns) + { + res = true; + + m_last_flush_time_ns = m_inspector->m_lastevent_ts; + + g_logger.format(sinsp_logger::SEV_INFO, "Flushing container table"); + + set containers_in_use; + + threadinfo_map_t* threadtable = m_inspector->m_thread_manager->get_threads(); + + threadtable->loop([&] (const sinsp_threadinfo& tinfo) { + if(!tinfo.m_container_id.empty()) + { + containers_in_use.insert(tinfo.m_container_id); + } + return true; + }); + + auto containers = m_containers.lock(); + for(auto it = containers->begin(); it != containers->end();) + { + if(containers_in_use.find(it->first) == containers_in_use.end()) + { + sinsp_container_info::ptr_t container = it->second; + for(const auto &remove_cb : m_remove_callbacks) + { + remove_cb(*container); + } + containers->erase(it++); + } + else + { + ++it; + } + } + } + + return res; +} + +sinsp_container_info::ptr_t sinsp_container_manager::get_container(const string& container_id) const +{ + auto containers = m_containers.lock(); + auto it = containers->find(container_id); + if(it != containers->end()) + { + return it->second; + } + + return nullptr; +} + +bool sinsp_container_manager::resolve_container(sinsp_threadinfo* tinfo, bool query_os_for_missing_info) +{ + ASSERT(tinfo); + bool matches = false; + + tinfo->m_container_id = ""; + if (m_inspector->m_parser->m_fd_listener) + { + matches = m_inspector->m_parser->m_fd_listener->on_resolve_container(this, tinfo, query_os_for_missing_info); + } + + // Delayed so there's a chance to set alternate socket paths, + // timeouts, after creation but before inspector open. + if(m_container_engines.size() == 0) + { + create_engines(); + } + + for(auto &eng : m_container_engines) + { + matches = matches || eng->resolve(tinfo, query_os_for_missing_info); + + if(matches) + { + break; + } + } + + // Also possibly set the category for the threadinfo + identify_category(tinfo); + + return matches; +} + +string sinsp_container_manager::container_to_json(const sinsp_container_info& container_info) +{ + Json::Value obj; + Json::Value& container = obj["container"]; + container["id"] = container_info.m_id; + container["full_id"] = container_info.m_full_id; + container["type"] = container_info.m_type; + container["name"] = container_info.m_name; + container["image"] = container_info.m_image; + container["imageid"] = container_info.m_imageid; + container["imagerepo"] = container_info.m_imagerepo; + container["imagetag"] = container_info.m_imagetag; + container["imagedigest"] = container_info.m_imagedigest; + container["privileged"] = container_info.m_privileged; + container["is_pod_sandbox"] = container_info.m_is_pod_sandbox; + container["lookup_state"] = static_cast(container_info.m_lookup_state); + container["created_time"] = static_cast(container_info.m_created_time); + + Json::Value mounts = Json::arrayValue; + + for (auto &mntinfo : container_info.m_mounts) + { + Json::Value mount; + + mount["Source"] = mntinfo.m_source; + mount["Destination"] = mntinfo.m_dest; + mount["Mode"] = mntinfo.m_mode; + mount["RW"] = mntinfo.m_rdwr; + mount["Propagation"] = mntinfo.m_propagation; + + mounts.append(mount); + } + + container["Mounts"] = mounts; + + sinsp_container_info::container_health_probe::add_health_probes(container_info.m_health_probes, container); + + char addrbuff[100]; + uint32_t iph = htonl(container_info.m_container_ip); + inet_ntop(AF_INET, &iph, addrbuff, sizeof(addrbuff)); + container["ip"] = addrbuff; + + Json::Value port_mappings = Json::arrayValue; + + for(auto &mapping : container_info.m_port_mappings) + { + Json::Value jmap; + jmap["HostIp"] = mapping.m_host_ip; + jmap["HostPort"] = mapping.m_host_port; + jmap["ContainerPort"] = mapping.m_container_port; + + port_mappings.append(jmap); + } + + container["port_mappings"] = port_mappings; + + Json::Value labels; + for (auto &pair : container_info.m_labels) + { + labels[pair.first] = pair.second; + } + container["labels"] = labels; + + Json::Value env_vars = Json::arrayValue; + + for (auto &var : container_info.m_env) + { + // Only append a limited set of mesos/marathon-related + // environment variables. + if(var.find("MESOS") != std::string::npos || + var.find("MARATHON") != std::string::npos || + var.find("mesos") != std::string::npos) + { + env_vars.append(var); + } + } + container["env"] = env_vars; + + container["memory_limit"] = (Json::Value::Int64) container_info.m_memory_limit; + container["swap_limit"] = (Json::Value::Int64) container_info.m_swap_limit; + container["cpu_shares"] = (Json::Value::Int64) container_info.m_cpu_shares; + container["cpu_quota"] = (Json::Value::Int64) container_info.m_cpu_quota; + container["cpu_period"] = (Json::Value::Int64) container_info.m_cpu_period; + container["cpuset_cpu_count"] = (Json::Value::Int) container_info.m_cpuset_cpu_count; + + if(!container_info.m_mesos_task_id.empty()) + { + container["mesos_task_id"] = container_info.m_mesos_task_id; + } + + container["metadata_deadline"] = (Json::Value::UInt64) container_info.m_metadata_deadline; + return Json::FastWriter().write(obj); +} + +bool sinsp_container_manager::container_to_sinsp_event(const string& json, sinsp_evt* evt, shared_ptr tinfo) +{ + size_t totlen = sizeof(scap_evt) + sizeof(uint16_t) + json.length() + 1; + + ASSERT(evt->m_pevt_storage == nullptr); + evt->m_pevt_storage = new char[totlen]; + evt->m_pevt = (scap_evt *) evt->m_pevt_storage; + + evt->m_cpuid = 0; + evt->m_evtnum = 0; + evt->m_inspector = m_inspector; + + scap_evt* scapevt = evt->m_pevt; + + if(m_inspector->m_lastevent_ts == 0) + { + // This can happen at startup when containers are + // being created as a part of the initial process + // scan. + scapevt->ts = sinsp_utils::get_current_time_ns(); + } + else + { + scapevt->ts = m_inspector->m_lastevent_ts; + } + scapevt->tid = -1; + scapevt->len = (uint32_t)totlen; + scapevt->type = PPME_CONTAINER_JSON_E; + scapevt->nparams = 1; + + uint16_t* lens = (uint16_t*)((char *)scapevt + sizeof(struct ppm_evt_hdr)); + char* valptr = (char*)lens + sizeof(uint16_t); + + *lens = (uint16_t)json.length() + 1; + memcpy(valptr, json.c_str(), *lens); + + evt->init(); + evt->m_tinfo_ref = tinfo; + evt->m_tinfo = tinfo.get(); + + return true; +} + +sinsp_container_manager::map_ptr_t sinsp_container_manager::get_containers() const +{ + return m_containers.lock(); +} + +void sinsp_container_manager::add_container(const sinsp_container_info::ptr_t& container_info, sinsp_threadinfo *thread) +{ + set_lookup_status(container_info->m_id, container_info->m_type, container_info->m_lookup_state); + { + auto containers = m_containers.lock(); + (*containers)[container_info->m_id] = container_info; + } + + for(const auto &new_cb : m_new_callbacks) + { + new_cb(*container_info, thread); + } +} + +void sinsp_container_manager::replace_container(const sinsp_container_info::ptr_t& container_info) +{ + auto containers = m_containers.lock(); + ASSERT(containers->find(container_info->m_id) != containers->end()); + (*containers)[container_info->m_id] = container_info; +} + +void sinsp_container_manager::notify_new_container(const sinsp_container_info& container_info) +{ + sinsp_evt *evt = new sinsp_evt(); + + if(container_to_sinsp_event(container_to_json(container_info), evt, container_info.get_tinfo(m_inspector))) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "notify_new_container (%s): created CONTAINER_JSON event, queuing to inspector", + container_info.m_id.c_str()); + + std::shared_ptr cevt(evt); + + // Enqueue it onto the queue of pending container events for the inspector +#ifndef _WIN32 + m_inspector->m_pending_container_evts.push(cevt); +#endif + } + else + { + g_logger.format(sinsp_logger::SEV_ERROR, + "notify_new_container (%s): could not create CONTAINER_JSON event, dropping", + container_info.m_id.c_str()); + delete evt; + } +} + +void sinsp_container_manager::dump_containers(scap_dumper_t* dumper) +{ + for(const auto& it : (*m_containers.lock())) + { + sinsp_evt evt; + if(container_to_sinsp_event(container_to_json(*it.second), &evt, it.second->get_tinfo(m_inspector))) + { + int32_t res = scap_dump(m_inspector->m_h, dumper, evt.m_pevt, evt.m_cpuid, 0); + if(res != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); + } + } + } +} + +string sinsp_container_manager::get_container_name(sinsp_threadinfo* tinfo) const +{ + string res; + + if(tinfo->m_container_id.empty()) + { + res = "host"; + } + else + { + const sinsp_container_info::ptr_t container_info = get_container(tinfo->m_container_id); + + if(!container_info) + { + return NULL; + } + + if(container_info->m_name.empty()) + { + return NULL; + } + + res = container_info->m_name; + } + + return res; +} + +void sinsp_container_manager::identify_category(sinsp_threadinfo *tinfo) +{ + if(tinfo->m_container_id.empty()) + { + return; + } + + if(tinfo->m_vpid == 1) + { + if(g_logger.get_severity() >= sinsp_logger::SEV_DEBUG) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "identify_category (%ld) (%s): initial process for container, assigning CAT_CONTAINER", + tinfo->m_tid, tinfo->m_comm.c_str()); + } + + tinfo->m_category = sinsp_threadinfo::CAT_CONTAINER; + + return; + } + + // Categories are passed from parent to child threads + const sinsp_threadinfo* ptinfo = tinfo->get_parent_thread(); + + if(ptinfo && ptinfo->m_category != sinsp_threadinfo::CAT_NONE) + { + if(g_logger.get_severity() >= sinsp_logger::SEV_DEBUG) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "identify_category (%ld) (%s): taking parent category %d", + tinfo->m_tid, tinfo->m_comm.c_str(), ptinfo->m_category); + } + + tinfo->m_category = ptinfo->m_category; + return; + } + + sinsp_container_info::ptr_t cinfo = get_container(tinfo->m_container_id); + if(!cinfo) + { + return; + } + + if(!cinfo->is_successful()) + { + if(g_logger.get_severity() >= sinsp_logger::SEV_DEBUG) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "identify_category (%ld) (%s): container metadata incomplete", + tinfo->m_tid, tinfo->m_comm.c_str()); + } + + return; + } + + // Otherwise, the thread is a part of a container health probe if: + // + // 1. the comm and args match one of the container's health probes + // 2. we traverse the parent state and do *not* find vpid=1, + // or find a process not in a container + // + // This indicates the initial process of the health probe. + + sinsp_container_info::container_health_probe::probe_type ptype = + cinfo->match_health_probe(tinfo); + + if(ptype == sinsp_container_info::container_health_probe::PT_NONE) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "identify_category (%ld) (%s): container health probe PT_NONE", + tinfo->m_tid, tinfo->m_comm.c_str()); + + return; + } + + bool found_container_init = false; + sinsp_threadinfo::visitor_func_t visitor = + [&found_container_init] (sinsp_threadinfo *ptinfo) + { + if(ptinfo->m_vpid == 1 && !ptinfo->m_container_id.empty()) + { + found_container_init = true; + + return false; + } + + return true; + }; + + tinfo->traverse_parent_state(visitor); + + if(!found_container_init) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "identify_category (%ld) (%s): not under container init, assigning category %s", + tinfo->m_tid, tinfo->m_comm.c_str(), + sinsp_container_info::container_health_probe::probe_type_names[ptype].c_str()); + + // Each health probe type maps to a command category + switch(ptype) + { + case sinsp_container_info::container_health_probe::PT_NONE: + case sinsp_container_info::container_health_probe::PT_END: + break; + case sinsp_container_info::container_health_probe::PT_HEALTHCHECK: + tinfo->m_category = sinsp_threadinfo::CAT_HEALTHCHECK; + break; + case sinsp_container_info::container_health_probe::PT_LIVENESS_PROBE: + tinfo->m_category = sinsp_threadinfo::CAT_LIVENESS_PROBE; + break; + case sinsp_container_info::container_health_probe::PT_READINESS_PROBE: + tinfo->m_category = sinsp_threadinfo::CAT_READINESS_PROBE; + break; + } + } +} + +void sinsp_container_manager::subscribe_on_new_container(new_container_cb callback) +{ + m_new_callbacks.emplace_back(callback); +} + +void sinsp_container_manager::subscribe_on_remove_container(remove_container_cb callback) +{ + m_remove_callbacks.emplace_back(callback); +} + +void sinsp_container_manager::create_engines() +{ +#ifndef MINIMAL_BUILD +#ifdef CYGWING_AGENT + { + auto docker_engine = std::make_shared(*this, m_inspector /*wmi source*/); + m_container_engines.push_back(docker_engine); + m_container_engine_by_type[CT_DOCKER] = docker_engine; + } +#else +#ifndef _WIN32 + { + auto docker_engine = std::make_shared(*this); + m_container_engines.push_back(docker_engine); + m_container_engine_by_type[CT_DOCKER] = docker_engine; + } + +#if defined(HAS_CAPTURE) + { + auto cri_engine = std::make_shared(*this); + m_container_engines.push_back(cri_engine); + m_container_engine_by_type[CT_CRI] = cri_engine; + m_container_engine_by_type[CT_CRIO] = cri_engine; + m_container_engine_by_type[CT_CONTAINERD] = cri_engine; + } +#endif + { + auto lxc_engine = std::make_shared(*this); + m_container_engines.push_back(lxc_engine); + m_container_engine_by_type[CT_LXC] = lxc_engine; + } + { + auto libvirt_lxc_engine = std::make_shared(*this); + m_container_engines.push_back(libvirt_lxc_engine); + m_container_engine_by_type[CT_LIBVIRT_LXC] = libvirt_lxc_engine; + } + + { + auto mesos_engine = std::make_shared(*this); + m_container_engines.push_back(mesos_engine); + m_container_engine_by_type[CT_MESOS] = mesos_engine; + } + { + auto rkt_engine = std::make_shared(*this); + m_container_engines.push_back(rkt_engine); + m_container_engine_by_type[CT_RKT] = rkt_engine; + } + { + auto bpm_engine = std::make_shared(*this); + m_container_engines.push_back(bpm_engine); + m_container_engine_by_type[CT_BPM] = bpm_engine; + } +#endif // _WIN32 +#endif // CYGWING_AGENT +#endif // MINIMAL_BUILD +} + +void sinsp_container_manager::update_container_with_size(sinsp_container_type type, + const std::string& container_id) +{ + auto found = m_container_engine_by_type.find(type); + if(found == m_container_engine_by_type.end()) + { + g_logger.format(sinsp_logger::SEV_ERROR, + "Container type %d not found when requesting size for %s", + type, + container_id.c_str()); + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "Request size for %s", + container_id.c_str()); + found->second->update_with_size(container_id); +} + +void sinsp_container_manager::cleanup() +{ + for(auto &eng : m_container_engines) + { + eng->cleanup(); + } +} + +void sinsp_container_manager::set_docker_socket_path(std::string socket_path) +{ +#if !defined(MINIMAL_BUILD) && defined(HAS_CAPTURE) + libsinsp::container_engine::docker::set_docker_sock(std::move(socket_path)); +#endif +} + +void sinsp_container_manager::set_query_docker_image_info(bool query_image_info) +{ +#if !defined(MINIMAL_BUILD) && !defined(_WIN32) + libsinsp::container_engine::docker_async_source::set_query_image_info(query_image_info); +#endif +} + +void sinsp_container_manager::set_cri_extra_queries(bool extra_queries) +{ +#if !defined(MINIMAL_BUILD) && defined(HAS_CAPTURE) + libsinsp::container_engine::cri::set_extra_queries(extra_queries); +#endif +} + +void sinsp_container_manager::set_cri_socket_path(const std::string &path) +{ +#if !defined(MINIMAL_BUILD) && defined(HAS_CAPTURE) + libsinsp::container_engine::cri::set_cri_socket_path(path); +#endif +} + +void sinsp_container_manager::set_cri_timeout(int64_t timeout_ms) +{ +#if !defined(MINIMAL_BUILD) && defined(HAS_CAPTURE) + libsinsp::container_engine::cri::set_cri_timeout(timeout_ms); +#endif +} + +void sinsp_container_manager::set_cri_async(bool async) +{ +#if !defined(MINIMAL_BUILD) && defined(HAS_CAPTURE) + libsinsp::container_engine::cri::set_async(async); +#endif +} + +void sinsp_container_manager::set_cri_delay(uint64_t delay_ms) +{ +#if !defined(MINIMAL_BUILD) && defined(HAS_CAPTURE) + libsinsp::container_engine::cri::set_cri_delay(delay_ms); +#endif +} + diff --git a/userspace/libsinsp/container.h b/userspace/libsinsp/container.h new file mode 100644 index 0000000000..892bf80bdf --- /dev/null +++ b/userspace/libsinsp/container.h @@ -0,0 +1,202 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#include +#include + +#include "scap.h" + +#include "event.h" +#include "container_info.h" + +#if !defined(_WIN32) && !defined(CYGWING_AGENT) && defined(HAS_CAPTURE) && !defined(MINIMAL_BUILD) +#include +#include +#include +#endif + +#include "container_engine/container_cache_interface.h" +#include "container_engine/container_engine_base.h" +#include "container_engine/sinsp_container_type.h" +#include "mutex.h" + +class sinsp_container_manager : + public libsinsp::container_engine::container_cache_interface +{ +public: + using map_ptr_t = libsinsp::ConstMutexGuard>; + + sinsp_container_manager(sinsp* inspector); + virtual ~sinsp_container_manager(); + + /** + * @brief Get the whole container map (read-only) + * @return the map of container_id -> shared_ptr + */ + map_ptr_t get_containers() const; + bool remove_inactive_containers(); + + /** + * @brief Add/update a container in the manager map, executing on_new_container callbacks + * + * @param container_info shared_ptr owning the container_info to add/update + * @param thread a thread in the container, only passed to callbacks + */ + void add_container(const sinsp_container_info::ptr_t& container_info, sinsp_threadinfo *thread) override; + + /** + * @brief Update a container by replacing its entry with a new one + * + * Does not call on_new_container callbacks + * + * @param container_info shared_ptr owning the updated container_info + */ + void replace_container(const sinsp_container_info::ptr_t& container_info) override; + + /** + * @brief Get a container_info by container id + * @param id the id of the container to look up + * @return a const pointer to the container_info + * + * Note: you cannot modify the returned object in any way, to update + * the container, get a new shared_ptr and pass it + * to replace_container() + */ + sinsp_container_info::ptr_t get_container(const std::string &id) const override; + + /** + * @brief Generate container JSON event from a new container + * @param container_info reference to the new sinsp_container_info + * + * Note: this is unrelated to on_new_container callbacks even though + * both happen during container creation + */ + void notify_new_container(const sinsp_container_info& container_info) override; + + /** + * @brief Detect container engine for a thread + * @param tinfo the thread to do container detection for + * @param query_os_for_missing_info should we consult external data sources? + * if true, we're working with a live capture and should + * query the OS (external files, daemons etc.); if false, + * we're reading a scap file so only rely on the thread info itself + * @return true if we have successfully determined the container engine, + * false otherwise + * + * Note: a return value of false doesn't mean that container detection failed, + * it may still be happening in the background asynchronously + */ + bool resolve_container(sinsp_threadinfo* tinfo, bool query_os_for_missing_info); + void dump_containers(scap_dumper_t* dumper); + std::string get_container_name(sinsp_threadinfo* tinfo) const; + + // Set tinfo's m_category based on the container context. It + // will *not* change any category to NONE, so a threadinfo + // that initially has a category will retain its category + // across execs e.g. "sh -c /bin/true" execing /bin/true. + void identify_category(sinsp_threadinfo *tinfo); + + bool container_exists(const std::string& container_id) const override{ + auto containers = m_containers.lock(); + return containers->find(container_id) != containers->end() || + m_lookups.find(container_id) != m_lookups.end(); + } + + typedef std::function new_container_cb; + typedef std::function remove_container_cb; + void subscribe_on_new_container(new_container_cb callback); + void subscribe_on_remove_container(remove_container_cb callback); + + void create_engines(); + + /** + * Update the container_info associated with the given type and container_id + * to include the size of the container layer. This is not filled in the + * initial request because it can easily take seconds. + */ + void update_container_with_size(sinsp_container_type type, + const std::string& container_id); + void cleanup(); + + void set_docker_socket_path(std::string socket_path); + void set_query_docker_image_info(bool query_image_info); + void set_cri_extra_queries(bool extra_queries); + void set_cri_socket_path(const std::string& path); + void set_cri_timeout(int64_t timeout_ms); + void set_cri_async(bool async); + void set_cri_delay(uint64_t delay_ms); + sinsp* get_inspector() { return m_inspector; } + + /** + * \brief set the status of an async container metadata lookup + * @param container_id the container id we're looking up + * @param ctype the container engine that is doing the lookup + * @param state the state of the lookup + * + * Container engines that do not do any lookups in external services need not + * bother with this. Otherwise, the engine needs to maintain the current + * state of the lookup via this method and call should_lookup() before + * starting a new lookup. + */ + void set_lookup_status(const std::string& container_id, sinsp_container_type ctype, sinsp_container_lookup_state state) override + { + m_lookups[container_id][ctype] = state; + } + + /** + * \brief do we want to start a new lookup for container metadata? + * @param container_id the container id we want to look up + * @param ctype the container engine that is doing the lookup + * @return true if there's no lookup in progress and we're free to start + * a new one, false otherwise + * + * This method effectively checks if m_lookups[container_id][ctype] + * exists, without creating unnecessary map entries along the way. + */ + bool should_lookup(const std::string& container_id, sinsp_container_type ctype) override + { + auto container_lookups = m_lookups.find(container_id); + if(container_lookups == m_lookups.end()) + { + return true; + } + auto engine_lookup = container_lookups->second.find(ctype); + return engine_lookup == container_lookups->second.end(); + } +private: + std::string container_to_json(const sinsp_container_info& container_info); + bool container_to_sinsp_event(const std::string& json, sinsp_evt* evt, std::shared_ptr tinfo); + std::string get_docker_env(const Json::Value &env_vars, const std::string &mti); + + std::list> m_container_engines; + std::map> m_container_engine_by_type; + + sinsp* m_inspector; + libsinsp::Mutex>> m_containers; + std::unordered_map> m_lookups; + uint64_t m_last_flush_time_ns; + std::list m_new_callbacks; + std::list m_remove_callbacks; + + friend class test_helper; +}; + diff --git a/userspace/libsinsp/container_engine/bpm.cpp b/userspace/libsinsp/container_engine/bpm.cpp new file mode 100644 index 0000000000..0e7ff4aade --- /dev/null +++ b/userspace/libsinsp/container_engine/bpm.cpp @@ -0,0 +1,70 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/bpm.h" +#include "sinsp.h" + +using namespace libsinsp::container_engine; + +bool bpm::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) +{ + sinsp_container_info container_info; + bool matches = false; + + for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it) + { + string cgroup = it->second; + size_t pos; + + // + // Non-systemd and systemd BPM + // + pos = cgroup.find("bpm-"); + if(pos != string::npos) + { + auto id_start = pos + sizeof("bpm-") - 1; + auto id_end = cgroup.find(".scope", id_start); + auto id = cgroup.substr(id_start, id_end - id_start); + + // As of BPM v1.0.3, the container ID is only allowed to contain the following chars + // see https://github.com/cloudfoundry-incubator/bpm-release/blob/v1.0.3/src/bpm/jobid/encoding.go + if (!id.empty() && strspn(id.c_str(), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-") == id.size()) + { + container_info.m_type = CT_BPM; + container_info.m_id = id; + matches = true; + break; + } + } + } + + if (!matches) + { + return false; + } + + tinfo->m_container_id = container_info.m_id; + if(container_cache().should_lookup(container_info.m_id, CT_BPM)) + { + container_info.m_name = container_info.m_id; + auto container = std::make_shared(container_info); + container_cache().add_container(container, tinfo); + container_cache().notify_new_container(container_info); + } + return true; +} diff --git a/userspace/libsinsp/container_engine/bpm.h b/userspace/libsinsp/container_engine/bpm.h new file mode 100644 index 0000000000..66763fd3c1 --- /dev/null +++ b/userspace/libsinsp/container_engine/bpm.h @@ -0,0 +1,40 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +class sinsp_container_info; +class sinsp_threadinfo; + +#include "container_engine/container_engine_base.h" +#include "container_engine/sinsp_container_type.h" + +namespace libsinsp { +namespace container_engine { + +class bpm : public container_engine_base +{ +public: + bpm(container_cache_interface& cache) : container_engine_base(cache) + {} + + bool resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) override; +}; +} +} diff --git a/userspace/libsinsp/container_engine/container_cache_interface.h b/userspace/libsinsp/container_engine/container_cache_interface.h new file mode 100644 index 0000000000..029172b20d --- /dev/null +++ b/userspace/libsinsp/container_engine/container_cache_interface.h @@ -0,0 +1,65 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include "container_info.h" + +namespace libsinsp +{ +namespace container_engine +{ + +/** + * Interface for a container cache for container engines. + */ +class container_cache_interface +{ +public: + virtual ~container_cache_interface() = default; + + virtual void notify_new_container(const sinsp_container_info& container_info) = 0; + + virtual bool should_lookup(const std::string& container_id, sinsp_container_type ctype) = 0; + + virtual void set_lookup_status(const std::string& container_id, sinsp_container_type ctype, sinsp_container_lookup_state state) = 0; + + /** + * Get a container from the cache. + */ + virtual sinsp_container_info::ptr_t get_container(const std::string& id) const = 0; + + /** + * Add a new container to the cache. + */ + virtual void add_container(const sinsp_container_info::ptr_t& container_info, sinsp_threadinfo *thread) = 0; + + /** + * Update a container by replacing its entry with a new one + */ + virtual void replace_container(const sinsp_container_info::ptr_t& container_info) = 0; + + /** + * Return whether the container exists in the cache. + */ + virtual bool container_exists(const std::string& container_id) const = 0; +}; + +} +} diff --git a/userspace/libsinsp/container_engine/container_engine_base.cpp b/userspace/libsinsp/container_engine/container_engine_base.cpp new file mode 100644 index 0000000000..3a5f12a6c6 --- /dev/null +++ b/userspace/libsinsp/container_engine/container_engine_base.cpp @@ -0,0 +1,44 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/container_engine_base.h" +#include "logger.h" + +namespace libsinsp +{ + +namespace container_engine +{ + +container_engine_base::container_engine_base(container_cache_interface &cache) : + m_cache(cache) +{ +} + +void container_engine_base::update_with_size(const std::string &container_id) +{ + SINSP_DEBUG("Updating container size not supported for this container type."); +} + +void container_engine_base::cleanup() +{ +} + +} +} diff --git a/userspace/libsinsp/container_engine/container_engine_base.h b/userspace/libsinsp/container_engine/container_engine_base.h new file mode 100644 index 0000000000..954931a0a6 --- /dev/null +++ b/userspace/libsinsp/container_engine/container_engine_base.h @@ -0,0 +1,69 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include "container_engine/container_cache_interface.h" + +class sinsp_threadinfo; + +namespace libsinsp { +namespace container_engine { + +/** + * Base class for container engine. This provides the interfaces to + * create a sinsp_container_info. + */ +class container_engine_base { +public: + container_engine_base(container_cache_interface &cache); + + virtual ~container_engine_base() = default; + + /** + * Find a container associated with the given tinfo and add it to the + * cache. + */ + virtual bool resolve(sinsp_threadinfo* tinfo, + bool query_os_for_missing_info) = 0; + + /** + * Update an existing container with the size of the container layer. + * The size is not requested as the part of the initial request (in resolve) + * because determining the size can take seconds. + */ + virtual void update_with_size(const std::string& container_id); + + virtual void cleanup(); + +protected: + /** + * Derived class accessor to the cache + */ + container_cache_interface& container_cache() + { + return m_cache; + } + +private: + container_cache_interface& m_cache; +}; +} +} + diff --git a/userspace/libsinsp/container_engine/cri.cpp b/userspace/libsinsp/container_engine/cri.cpp new file mode 100644 index 0000000000..ac1b70e15b --- /dev/null +++ b/userspace/libsinsp/container_engine/cri.cpp @@ -0,0 +1,427 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/cri.h" + +#include +#ifdef GRPC_INCLUDE_IS_GRPCPP +# include +#else +# include +#endif +#include "cri.pb.h" +#include "cri.grpc.pb.h" + +#include "cgroup_limits.h" +#include "runc.h" +#include "container_engine/mesos.h" +#include +#include "sinsp.h" +#include "sinsp_int.h" + +using namespace libsinsp::cri; +using namespace libsinsp::container_engine; +using namespace libsinsp::runc; + +namespace { +// do the CRI communication asynchronously +bool s_async = true; +// delay before talking to CRI/cgroups +uint64_t s_cri_lookup_delay_ms = 500; + +constexpr const cgroup_layout CRI_CGROUP_LAYOUT[] = { + {"/", ""}, // non-systemd containerd + {"/crio-", ""}, // non-systemd cri-o + {"/cri-containerd-", ".scope"}, // systemd containerd + {"/crio-", ".scope"}, // systemd cri-o + {nullptr, nullptr} +}; +} + +bool cri_async_source::parse_containerd(const runtime::v1alpha2::ContainerStatusResponse& status, sinsp_container_info &container) +{ + const auto &info_it = status.info().find("info"); + if(info_it == status.info().end()) + { + return false; + } + + Json::Value root; + Json::Reader reader; + if(!reader.parse(info_it->second, root)) + { + ASSERT(false); + return false; + } + + m_cri->parse_cri_env(root, container); + m_cri->parse_cri_json_image(root, container); + m_cri->parse_cri_runtime_spec(root, container); + + if(root.isMember("sandboxID") && root["sandboxID"].isString()) + { + const auto pod_sandbox_id = root["sandboxID"].asString(); + container.m_container_ip = ntohl(m_cri->get_pod_sandbox_ip(pod_sandbox_id)); + } + + return true; +} + +bool cri_async_source::parse_cri(sinsp_container_info& container, const libsinsp::cgroup_limits::cgroup_limits_key& key) +{ + runtime::v1alpha2::ContainerStatusResponse resp; + grpc::Status status = m_cri->get_container_status(container.m_id, resp); + + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s): Status from ContainerStatus: (%s)", + container.m_id.c_str(), + status.error_message().c_str()); + + if(!status.ok()) + { + if(m_cri->is_pod_sandbox(container.m_id)) + { + container.m_is_pod_sandbox = true; + return true; + } + g_logger.format(sinsp_logger::SEV_DEBUG, "cri (%s): id is neither a container nor a pod sandbox", + container.m_id.c_str()); + return false; + } + + if(!resp.has_status()) + { + ASSERT(false); + return false; + } + + const auto &resp_container = resp.status(); + container.m_full_id = resp_container.id(); + container.m_name = resp_container.metadata().name(); + + // This is in Nanoseconds(in CRI API). Need to convert it to seconds. + container.m_created_time = static_cast(resp_container.created_at() / ONE_SECOND_IN_NS ); + + for(const auto &pair : resp_container.labels()) + { + container.m_labels[pair.first] = pair.second; + } + + m_cri->parse_cri_image(resp_container, container); + m_cri->parse_cri_mounts(resp_container, container); + + if(parse_containerd(resp, container)) + { + return true; + } + + libsinsp::cgroup_limits::cgroup_limits_value limits; + libsinsp::cgroup_limits::get_cgroup_resource_limits(key, limits); + + container.m_memory_limit = limits.m_memory_limit; + container.m_cpu_shares = limits.m_cpu_shares; + container.m_cpu_quota = limits.m_cpu_quota; + container.m_cpu_period = limits.m_cpu_period; + container.m_cpuset_cpu_count = limits.m_cpuset_cpu_count; + + if(s_cri_extra_queries) + { + container.m_container_ip = m_cri->get_container_ip(container.m_id); + container.m_imageid = m_cri->get_container_image_id(resp_container.image_ref()); + } + + return true; +} + +void cri_async_source::run_impl() +{ + libsinsp::cgroup_limits::cgroup_limits_key key; + + while (dequeue_next_key(key)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri_async (%s): Source dequeued key", + key.m_container_id.c_str()); + + sinsp_container_info res; + + res.m_lookup_state = sinsp_container_lookup_state::SUCCESSFUL; + res.m_type = m_cri->get_cri_runtime_type(); + res.m_id = key.m_container_id; + + if(!parse_cri(res, key)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri_async (%s): Failed to get CRI metadata, returning successful=false", + key.m_container_id.c_str()); + res.m_lookup_state = sinsp_container_lookup_state::FAILED; + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri_async (%s): Parse successful, storing value", + key.m_container_id.c_str()); + + store_value(key, res); + } + +} + +bool cri_async_source::lookup_sync(const libsinsp::cgroup_limits::cgroup_limits_key& key, + sinsp_container_info& value) +{ + value.m_lookup_state = sinsp_container_lookup_state::SUCCESSFUL; + value.m_type = m_cri->get_cri_runtime_type(); + value.m_id = key.m_container_id; + + if(!parse_cri(value, key)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri_async (%s): Failed to get CRI metadata, returning successful=false", + key.m_container_id.c_str()); + value.m_lookup_state = sinsp_container_lookup_state::FAILED; + } + + return true; +} + +cri::cri(container_cache_interface &cache) : container_engine_base(cache) +{ + if(s_cri_unix_socket_path.empty()) { + return; + } + + auto cri_path = scap_get_host_root() + s_cri_unix_socket_path; + struct stat s = {}; + if(stat(cri_path.c_str(), &s) != 0 || (s.st_mode & S_IFMT) != S_IFSOCK) { + return; + } + + m_cri = std::unique_ptr(new libsinsp::cri::cri_interface(cri_path)); + if(!m_cri->is_ok()) + { + m_cri.reset(nullptr); + } +} + +void cri::cleanup() +{ + if(m_async_source) + { + m_async_source->quiesce(); + } + s_cri_extra_queries = true; +} + +void cri::set_cri_socket_path(const std::string& path) +{ + s_cri_unix_socket_path = path; +} + +void cri::set_cri_timeout(int64_t timeout_ms) +{ + s_cri_timeout = timeout_ms + s_cri_lookup_delay_ms; +} + +void cri::set_extra_queries(bool extra_queries) { + s_cri_extra_queries = extra_queries; +} + +void cri::set_async(bool async) +{ + s_async = async; +} + +void cri::set_cri_delay(uint64_t delay_ms) +{ + s_cri_lookup_delay_ms = delay_ms; +} + +bool cri::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) +{ + container_cache_interface *cache = &container_cache(); + std::string container_id; + + if(!matches_runc_cgroups(tinfo, CRI_CGROUP_LAYOUT, container_id)) + { + return false; + } + tinfo->m_container_id = container_id; + + if(!m_cri) + { + // This isn't an error in the case where the + // configured unix domain socket doesn't exist. In + // that case, s_cri isn't initialized at all. Hence, + // the DEBUG. + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s): Could not parse cri (no s_cri object)", + container_id.c_str()); + return false; + } + + if(!cache->should_lookup(container_id, m_cri->get_cri_runtime_type())) + { + return true; + } + + auto container = std::make_shared(); + container->m_id = container_id; + container->m_type = m_cri->get_cri_runtime_type(); + if (mesos::set_mesos_task_id(*container, tinfo)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s) Mesos CRI container, Mesos task ID: [%s]", + container_id.c_str(), container->m_mesos_task_id.c_str()); + } + + if (query_os_for_missing_info) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s): Performing lookup", + container_id.c_str()); + + container->m_lookup_state = sinsp_container_lookup_state::SUCCESSFUL; + libsinsp::cgroup_limits::cgroup_limits_key key( + container->m_id, + tinfo->get_cgroup("cpu"), + tinfo->get_cgroup("memory"), + tinfo->get_cgroup("cpuset")); + + if(!m_async_source) + { + auto async_source = new cri_async_source(cache, m_cri.get(), s_cri_timeout); + m_async_source = std::unique_ptr(async_source); + } + + cache->set_lookup_status(container_id, m_cri->get_cri_runtime_type(), sinsp_container_lookup_state::STARTED); + auto cb = [cache](const libsinsp::cgroup_limits::cgroup_limits_key& key, const sinsp_container_info& res) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri_async (%s): Source callback result=%d", + key.m_container_id.c_str(), + res.m_lookup_state); + + cache->notify_new_container(res); + }; + + sinsp_container_info result; + + bool done; + if(s_async) + { + done = m_async_source->lookup_delayed(key, result, chrono::milliseconds(s_cri_lookup_delay_ms), cb); + } + else + { + done = m_async_source->lookup_sync(key, result); + } + + if (done) + { + // if a previous lookup call already found the metadata, process it now + cb(key, result); + + if(s_async) + { + // This should *never* happen, in async mode as ttl is 0 (never wait) + g_logger.format(sinsp_logger::SEV_ERROR, + "cri_async (%s): Unexpected immediate return from cri_async lookup", + container_id.c_str()); + + } + } + } + else + { + cache->notify_new_container(*container); + } + return true; +} + +void cri::update_with_size(const std::string& container_id) +{ + sinsp_container_info::ptr_t existing = container_cache().get_container(container_id); + if(!existing) + { + g_logger.format(sinsp_logger::SEV_ERROR, + "cri (%s): Failed to locate existing container data", + container_id.c_str()); + ASSERT(false); + return; + } + + // Synchronously get the stats response and update the container table. + // Note that this needs to use the full id. + runtime::v1alpha2::ContainerStatsResponse resp; + grpc::Status status = m_cri->get_container_stats(existing->m_full_id, resp); + + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s): full id (%s): Status from ContainerStats: (%s)", + container_id.c_str(), + existing->m_full_id.c_str(), + status.error_message().empty() ? "SUCCESS" : status.error_message().c_str()); + + if(!status.ok()) + { + return; + } + + if(!resp.has_stats()) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s): Failed to update size: stats() not found", + container_id.c_str()); + ASSERT(false); + return; + } + + const auto& resp_stats = resp.stats(); + + if(!resp_stats.has_writable_layer()) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s): Failed to update size: writable_layer() not found", + container_id.c_str()); + ASSERT(false); + return; + } + + if(!resp_stats.writable_layer().has_used_bytes()) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "cri (%s): Failed to update size: used_bytes() not found", + container_id.c_str()); + ASSERT(false); + return; + } + + // Make a mutable copy of the existing container_info + shared_ptr updated(std::make_shared(*existing)); + updated->m_size_rw_bytes = resp_stats.writable_layer().used_bytes().value(); + + if(existing->m_size_rw_bytes == updated->m_size_rw_bytes) + { + // no data has changed + return; + } + + container_cache().replace_container(updated); +} + + diff --git a/userspace/libsinsp/container_engine/cri.h b/userspace/libsinsp/container_engine/cri.h new file mode 100644 index 0000000000..0b4c802c15 --- /dev/null +++ b/userspace/libsinsp/container_engine/cri.h @@ -0,0 +1,97 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#include + +class sinsp_threadinfo; + +#include "cgroup_limits.h" +#include "container_engine/container_engine_base.h" +#include "container_engine/sinsp_container_type.h" +#include "container_info.h" +#include + +namespace runtime { +namespace v1alpha2 { +class ContainerStatusResponse; +} +} + +namespace libsinsp { +namespace container_engine { + +/** + * Asynchronous metadata lookup for CRI containers + * + * There are two related reasons for asynchronous lookup: + * 1. Not blocking the main event processing thread + * + * 2. Apparently CRI can fail to find a freshly created container + * for a short while, so we should delay the query a bit. + */ +class cri_async_source : public sysdig::async_key_value_source< + libsinsp::cgroup_limits::cgroup_limits_key, + sinsp_container_info> +{ +public: + explicit cri_async_source(container_cache_interface *cache, ::libsinsp::cri::cri_interface *cri, uint64_t ttl_ms) : + async_key_value_source(NO_WAIT_LOOKUP, ttl_ms), + m_cache(cache), + m_cri(cri) + { + } + + void quiesce() { + async_key_value_source::stop(); + } + + bool lookup_sync(const libsinsp::cgroup_limits::cgroup_limits_key& key, + sinsp_container_info& value); + + bool parse_cri(sinsp_container_info& container, const libsinsp::cgroup_limits::cgroup_limits_key& key); +private: + bool parse_containerd(const runtime::v1alpha2::ContainerStatusResponse& status, sinsp_container_info& container); + void run_impl() override; + + container_cache_interface *m_cache; + ::libsinsp::cri::cri_interface *m_cri; +}; + +class cri : public container_engine_base +{ +public: + cri(container_cache_interface &cache); + bool resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) override; + void update_with_size(const std::string& container_id) override; + void cleanup() override; + static void set_cri_socket_path(const std::string& path); + static void set_cri_timeout(int64_t timeout_ms); + static void set_extra_queries(bool extra_queries); + static void set_async(bool async_limits); + static void set_cri_delay(uint64_t delay_ms); + +private: + std::unique_ptr m_async_source; + std::unique_ptr<::libsinsp::cri::cri_interface> m_cri; +}; +} +} diff --git a/userspace/libsinsp/container_engine/docker.h b/userspace/libsinsp/container_engine/docker.h new file mode 100644 index 0000000000..4fb030c3c7 --- /dev/null +++ b/userspace/libsinsp/container_engine/docker.h @@ -0,0 +1,207 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once +#ifndef MINIMAL_BUILD +#ifndef _WIN32 + +#include +#include +#include +#include + +#if !defined(_WIN32) +#include +#include +#include +#endif + +#include "json/json.h" + +#include "async_key_value_source.h" + +#include "container.h" +#include "container_info.h" + +#include "container_engine/container_engine_base.h" +#include "container_engine/sinsp_container_type.h" +#include "container_engine/wmi_handle_source.h" + +class sinsp; +class sinsp_threadinfo; + +namespace libsinsp { +namespace container_engine { + +struct docker_async_instruction +{ + docker_async_instruction() : + request_rw_size(false) + {} + + docker_async_instruction(const std::string container_id_value, + bool rw_size_value) : + container_id(container_id_value), + request_rw_size(rw_size_value) + {} + + bool operator<(const docker_async_instruction& rhs) const + { + if(container_id < rhs.container_id) + { + return true; + } + + return request_rw_size < rhs.request_rw_size; + } + + bool operator==(const docker_async_instruction& rhs) const + { + return container_id == rhs.container_id && + request_rw_size == rhs.request_rw_size; + } + + std::string container_id; + bool request_rw_size; +}; + +class docker_async_source : public sysdig::async_key_value_source +{ + enum docker_response + { + RESP_OK = 0, + RESP_BAD_REQUEST = 1, + RESP_ERROR = 2 + }; + +public: +#ifdef _WIN32 + docker_async_source(uint64_t max_wait_ms, uint64_t ttl_ms, container_cache_interface *cache); +#else + docker_async_source(uint64_t max_wait_ms, uint64_t ttl_ms, container_cache_interface *cache, std::string socket_path); +#endif + virtual ~docker_async_source(); + + static void set_query_image_info(bool query_image_info); + +protected: + void run_impl(); + +private: + // These 4 methods are OS-dependent and defined in docker_{linux,win}.cpp + void init_docker_conn(); + void free_docker_conn(); + std::string build_request(const std::string& url); + docker_response get_docker(const std::string& url, std::string &json); + + bool parse_docker(const docker_async_instruction& instruction, sinsp_container_info& container); + + // Look for a pod specification in this container's labels and + // if found set spec to the pod spec. + bool get_k8s_pod_spec(const Json::Value &config_obj, + Json::Value &spec); + + std::string normalize_arg(const std::string &arg); + + // Parse a healthcheck out of the provided healthcheck object, + // updating the container info with any healthcheck found. + void parse_healthcheck(const Json::Value &healthcheck_obj, + sinsp_container_info &container); + + // Parse either a readiness or liveness probe out of the + // provided object, updating the container info with any probe + // found. Returns true if the healthcheck/livenesss/readiness + // probe info was found and could be parsed. + bool parse_liveness_readiness_probe(const Json::Value &probe_obj, + sinsp_container_info::container_health_probe::probe_type ptype, + sinsp_container_info &container); + + // See if this config has a io.kubernetes.sandbox.id label + // referring to a different container. (NOTE: this is not the + // same as docker's sandbox id, which refers to networks.) If + // it does, try to copy the health checks from that container + // to the provided container_info pointer. Returns true if a + // sandbox container id was found, the corresponding container + // was found, and if the health checks could be copied from + // that container. + bool get_sandbox_liveness_readiness_probes(const Json::Value &config_obj, + sinsp_container_info &container); + + // Parse all healthchecks/liveness probes/readiness probes out + // of the provided object, updating the container info as required. + void parse_health_probes(const Json::Value &config_obj, + sinsp_container_info &container); + + container_cache_interface *m_cache; + + std::string m_api_version; + +#ifndef _WIN32 + std::string m_docker_unix_socket_path; + CURLM *m_curlm; + CURL *m_curl; +#endif + + static bool m_query_image_info; +}; + +class docker : public container_engine_base +{ +public: + +#ifdef _WIN32 + docker(container_cache_interface &cache, const wmi_handle_source&); +#else + docker(container_cache_interface &cache) : container_engine_base(cache) + {} +#endif + void cleanup() override; + static void parse_json_mounts(const Json::Value &mnt_obj, std::vector &mounts); + + // Container name only set for windows. For linux name must be fetched via lookup + static bool detect_docker(const sinsp_threadinfo* tinfo, std::string& container_id, std::string &container_name); + +#ifndef _WIN32 + static void set_docker_sock(std::string docker_sock) { + m_docker_sock = std::move(docker_sock); + } +#endif + +protected: + void parse_docker_async(const std::string& container_id, container_cache_interface *cache); + + std::unique_ptr m_docker_info_source; + + static std::string s_incomplete_info_name; +#ifdef _WIN32 + const wmi_handle_source& m_wmi_handle_source; +#else + static std::string m_docker_sock; +#endif + +private: + // implement container_engine_base + bool resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) override; + void update_with_size(const std::string& container_id) override; +}; +} +} + +#endif // _WIN32 +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/container_engine/docker_common.cpp b/userspace/libsinsp/container_engine/docker_common.cpp new file mode 100644 index 0000000000..a3e2bc38a5 --- /dev/null +++ b/userspace/libsinsp/container_engine/docker_common.cpp @@ -0,0 +1,871 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#ifndef _WIN32 + +#include "container_engine/docker.h" +#include "cgroup_list_counter.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "container.h" +#include "utils.h" +#include + +using namespace libsinsp::container_engine; + +docker_async_source::docker_async_source(uint64_t max_wait_ms, + uint64_t ttl_ms, + container_cache_interface *cache +#ifndef _WIN32 + , std::string socket_path +#endif + ) + : async_key_value_source(max_wait_ms, ttl_ms), + m_cache(cache), +#ifdef _WIN32 + m_api_version("/v1.30") +#else + m_api_version("/v1.24"), + m_docker_unix_socket_path(std::move(socket_path)), + m_curlm(NULL), + m_curl(NULL) +#endif +{ + init_docker_conn(); +} + +docker_async_source::~docker_async_source() +{ + this->stop(); + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async: Source destructor"); + + free_docker_conn(); +} + +void docker_async_source::run_impl() +{ + docker_async_instruction instruction; + + while (dequeue_next_key(instruction)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s : %s): Source dequeued key", + instruction.container_id.c_str(), + instruction.request_rw_size ? "true" : "false"); + + sinsp_container_info res; + + res.m_lookup_state = sinsp_container_lookup_state::SUCCESSFUL; + res.m_type = CT_DOCKER; + res.m_id = instruction.container_id; + + if(!parse_docker(instruction, res)) + { + // This is not always an error e.g. when using + // containerd as the runtime. Since the cgroup + // names are often identical between + // containerd and docker, we have to try to + // fetch both. + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Failed to get Docker metadata, returning successful=false", + instruction.container_id.c_str()); + res.m_lookup_state = sinsp_container_lookup_state::FAILED; + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Parse successful, storing value", + instruction.container_id.c_str()); + + // Return a result object either way, to ensure any + // new container callbacks are called. + store_value(instruction, res); + } +} + +bool docker_async_source::m_query_image_info = true; + +void docker::parse_json_mounts(const Json::Value &mnt_obj, vector &mounts) +{ + if(!mnt_obj.isNull() && mnt_obj.isArray()) + { + for(uint32_t i=0; i args; + + for(uint32_t i = 2; i < test_obj.size(); i++) + { + args.push_back(normalize_arg(test_obj[i].asString())); + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker (%s): Setting PT_HEALTHCHECK exe=%s nargs=%d", + container.m_id.c_str(), exe.c_str(), args.size()); + + container.m_health_probes.emplace_back(sinsp_container_info::container_health_probe::PT_HEALTHCHECK, + std::move(exe), + std::move(args)); + } + else if(test_obj[0].asString() == "CMD-SHELL") + { + std::string exe = "/bin/sh"; + std::vector args; + + args.push_back("-c"); + args.push_back(test_obj[1].asString()); + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker (%s): Setting PT_HEALTHCHECK exe=%s nargs=%d", + container.m_id.c_str(), exe.c_str(), args.size()); + + container.m_health_probes.emplace_back(sinsp_container_info::container_health_probe::PT_HEALTHCHECK, + std::move(exe), + std::move(args)); + } + else + { + g_logger.format(sinsp_logger::SEV_WARNING, "Could not parse health check from %s (Expected CMD/CMD-SHELL for multi-element Test array)", + Json::FastWriter().write(healthcheck_obj).c_str()); + return; + } +} + +bool docker_async_source::parse_liveness_readiness_probe(const Json::Value &probe_obj, + sinsp_container_info::container_health_probe::probe_type ptype, + sinsp_container_info &container) +{ + if(probe_obj.isNull() || + !probe_obj.isMember("exec") || + !probe_obj["exec"].isMember("command")) + { + g_logger.format(sinsp_logger::SEV_WARNING, "Could not parse liveness/readiness probe from %s", + Json::FastWriter().write(probe_obj).c_str()); + return false; + } + + const Json::Value command_obj = probe_obj["exec"]["command"]; + + if(!command_obj.isNull() && command_obj.isArray()) + { + std::string exe; + std::vector args; + + exe = normalize_arg(command_obj[0].asString()); + for(uint32_t i = 1; i < command_obj.size(); i++) + { + args.push_back(normalize_arg(command_obj[i].asString())); + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker (%s): Setting %s exe=%s nargs=%d", + container.m_id.c_str(), + sinsp_container_info::container_health_probe::probe_type_names[ptype].c_str(), + exe.c_str(), args.size()); + + container.m_health_probes.emplace_back(ptype, std::move(exe), std::move(args)); + } + + return true; +} + +bool docker_async_source::get_sandbox_liveness_readiness_probes(const Json::Value &config_obj, + sinsp_container_info &container) +{ + std::string sandbox_container_id; + std::string sandbox_label = "io.kubernetes.sandbox.id"; + + if(config_obj.isNull() || + !config_obj.isMember("Labels") || + !config_obj["Labels"].isMember(sandbox_label)) + { + SINSP_DEBUG("docker (%s): No sandbox label found, not copying liveness/readiness probes", + container.m_id.c_str()); + return false; + } + + sandbox_container_id = config_obj["Labels"][sandbox_label].asString(); + + if(sandbox_container_id.size() > 12) + { + sandbox_container_id.resize(12); + } + + sinsp_container_info::ptr_t sandbox_container = m_cache->get_container(sandbox_container_id); + + if(!sandbox_container) + { + SINSP_DEBUG("docker (%s): Sandbox container %s doesn't exist, not copying liveness/readiness probes", + container.m_id.c_str(), sandbox_container_id.c_str()); + return false; + } + + if(sandbox_container->m_health_probes.size() == 0) + { + SINSP_DEBUG("docker (%s): Sandbox container %s has no liveness/readiness probes, not copying", + container.m_id.c_str(), sandbox_container_id.c_str()); + return false; + } + + SINSP_DEBUG("docker (%s): Copying liveness/readiness probes from sandbox container %s", + container.m_id.c_str(), sandbox_container_id.c_str()); + container.m_health_probes = sandbox_container->m_health_probes; + + return true; +} + +void docker_async_source::parse_health_probes(const Json::Value &config_obj, + sinsp_container_info &container) +{ + Json::Value spec; + bool liveness_readiness_added = false; + + // When parsing the full container json for live containers, a label contains stringified json that + // contains the probes. + if (get_k8s_pod_spec(config_obj, spec)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker (%s): Parsing liveness/readiness probes from pod spec", + container.m_id.c_str()); + + if(spec.isMember("livenessProbe")) + { + if(parse_liveness_readiness_probe(spec["livenessProbe"], + sinsp_container_info::container_health_probe::PT_LIVENESS_PROBE, + container)) + { + liveness_readiness_added = true; + } + } + else if(spec.isMember("readinessProbe")) + { + if(parse_liveness_readiness_probe(spec["readinessProbe"], + sinsp_container_info::container_health_probe::PT_READINESS_PROBE, + container)) + { + liveness_readiness_added = true; + } + } + } + // Otherwise, try to copy the liveness/readiness probe from the sandbox container, if it exists. + else if (get_sandbox_liveness_readiness_probes(config_obj, container)) + { + liveness_readiness_added = true; + } + else + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker (%s): No liveness/readiness probes found", + container.m_id.c_str()); + } + + // To avoid any confusion about containers that both refer to + // a healthcheck and liveness/readiness probe, we only + // consider a healthcheck if no liveness/readiness was added. + if(!liveness_readiness_added && config_obj.isMember("Healthcheck")) + { + parse_healthcheck(config_obj["Healthcheck"], container); + } +} + +void docker_async_source::set_query_image_info(bool query_image_info) +{ + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async: Setting query_image_info=%s", + (query_image_info ? "true" : "false")); + + m_query_image_info = query_image_info; +} + +std::string docker::s_incomplete_info_name = "incomplete"; + +bool docker::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) +{ + std::string container_id, container_name; + container_cache_interface *cache = &container_cache(); + + if(!detect_docker(tinfo, container_id, container_name)) + { + return false; + } + + if(!m_docker_info_source) + { + g_logger.log("docker_async: Creating docker async source", + sinsp_logger::SEV_DEBUG); + uint64_t max_wait_ms = 10000; +#ifdef _WIN32 + docker_async_source *src = new docker_async_source(docker_async_source::NO_WAIT_LOOKUP, max_wait_ms, cache); +#else + docker_async_source *src = new docker_async_source(docker_async_source::NO_WAIT_LOOKUP, max_wait_ms, cache, m_docker_sock); +#endif + m_docker_info_source.reset(src); + } + + tinfo->m_container_id = container_id; + + sinsp_container_info::ptr_t container_info = cache->get_container(container_id); + + if(!container_info) + { + if(!query_os_for_missing_info) + { + auto container = std::make_shared(); + container->m_type = CT_DOCKER; + container->m_id = container_id; + cache->notify_new_container(*container); + return true; + } + +#ifdef HAS_CAPTURE + if(cache->should_lookup(container_id, CT_DOCKER)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): No existing container info", + container_id.c_str()); + + // give docker a chance to return metadata for this container + cache->set_lookup_status(container_id, CT_DOCKER, sinsp_container_lookup_state::STARTED); + parse_docker_async(container_id, cache); + } +#endif + return false; + } + + // Returning true will prevent other container engines from + // trying to resolve the container, so only return true if we + // have complete metadata. + return container_info->is_successful(); +} + +void docker::parse_docker_async(const string& container_id, container_cache_interface *cache) +{ + auto cb = [cache](const docker_async_instruction& instruction, const sinsp_container_info& res) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Source callback result=%d", + instruction.container_id.c_str(), + res.m_lookup_state); + + cache->notify_new_container(res); + }; + + sinsp_container_info result; + + docker_async_instruction instruction(container_id, false /*don't request size*/); + if(m_docker_info_source->lookup(instruction, result, cb)) + { + // if a previous lookup call already found the metadata, process it now + cb(instruction, result); + + // This should *never* happen, as ttl is 0 (never wait) + g_logger.format(sinsp_logger::SEV_ERROR, + "docker_async (%s): Unexpected immediate return from docker_info_source.lookup()", + container_id.c_str()); + } +} + +bool docker_async_source::parse_docker(const docker_async_instruction& instruction, sinsp_container_info& container) +{ + string json; + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Looking up info for container", + instruction.container_id.c_str()); + + std::string request = build_request("/containers/" + instruction.container_id + "/json"); + if(instruction.request_rw_size) + { + request += "?size=true"; + } + + docker_response resp = get_docker(request, json); + + switch(resp) { + case docker_response::RESP_BAD_REQUEST: + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Initial url fetch failed, trying w/o api version", + instruction.container_id.c_str()); + + m_api_version = ""; + json = ""; + resp = get_docker(build_request("/containers/" + instruction.container_id + "/json"), json); + if (resp == docker_response::RESP_OK) + { + break; + } + /* FALLTHRU */ + case docker_response::RESP_ERROR: + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Url fetch failed, returning false", + instruction.container_id.c_str()); + + return false; + + case docker_response::RESP_OK: + break; + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Parsing containers response \"%s\"", + instruction.container_id.c_str(), + json.c_str()); + + Json::Value root; + Json::Reader reader; + bool parsingSuccessful = reader.parse(json, root); + if(!parsingSuccessful) + { + g_logger.format(sinsp_logger::SEV_ERROR, + "docker_async (%s): Could not parse json \"%s\", returning false", + instruction.container_id.c_str(), + json.c_str()); + + ASSERT(false); + return false; + } + + const Json::Value& config_obj = root["Config"]; + + container.m_image = config_obj["Image"].asString(); + + string imgstr = root["Image"].asString(); + size_t cpos = imgstr.find(":"); + if(cpos != string::npos) + { + container.m_imageid = imgstr.substr(cpos + 1); + } + + parse_health_probes(config_obj, container); + + // containers can be spawned using just the imageID as image name, + // with or without the hash prefix (e.g. sha256:) + bool no_name = !container.m_imageid.empty() && + strncmp(container.m_image.c_str(), container.m_imageid.c_str(), + MIN(container.m_image.length(), container.m_imageid.length())) == 0; + no_name |= !imgstr.empty() && + strncmp(container.m_image.c_str(), imgstr.c_str(), + MIN(container.m_image.length(), imgstr.length())) == 0; + + if(!no_name || !m_query_image_info) + { + string hostname, port; + sinsp_utils::split_container_image(container.m_image, + hostname, + port, + container.m_imagerepo, + container.m_imagetag, + container.m_imagedigest, + false); + } + + if(m_query_image_info && !container.m_imageid.empty() && + (no_name || container.m_imagedigest.empty() || (!container.m_imagedigest.empty() && container.m_imagetag.empty()))) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s) image (%s): Fetching image info", + instruction.container_id.c_str(), + container.m_imageid.c_str()); + + string img_json; + std::string url = "/images/" + container.m_imageid + "/json?digests=1"; + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async url: %s", + url.c_str()); + + if(get_docker(build_request(url), img_json) == docker_response::RESP_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s) image (%s): Image info fetch returned \"%s\"", + instruction.container_id.c_str(), + container.m_imageid.c_str(), + img_json.c_str()); + + Json::Value img_root; + if(reader.parse(img_json, img_root)) + { + // img_root["RepoDigests"] contains only digests for images pulled from registries. + // If an image gets retagged and is never pushed to any registry, we will not find + // that entry in container.m_imagerepo. Also, for locally built images we have the + // same issue. This leads to container.m_imagedigest being empty as well. + unordered_set imageDigestSet; + for(const auto& rdig : img_root["RepoDigests"]) + { + if(rdig.isString()) + { + string repodigest = rdig.asString(); + string digest = repodigest.substr(repodigest.find('@')+1); + imageDigestSet.insert(digest); + if(container.m_imagerepo.empty()) + { + container.m_imagerepo = repodigest.substr(0, repodigest.find('@')); + } + if(repodigest.find(container.m_imagerepo) != string::npos) + { + container.m_imagedigest = digest; + break; + } + } + } + for(const auto& rtag : img_root["RepoTags"]) + { + if(rtag.isString()) + { + string repotag = rtag.asString(); + if(container.m_imagerepo.empty()) + { + container.m_imagerepo = repotag.substr(0, repotag.rfind(":")); + } + if(repotag.find(container.m_imagerepo) != string::npos) + { + container.m_imagetag = repotag.substr(repotag.rfind(":")+1); + break; + } + } + } + // fix image digest for locally tagged images or multiple repo digests. + // Case 1: One repo digest with many tags. + // Case 2: Many repo digests with the same digest value. + if(container.m_imagedigest.empty() && imageDigestSet.size() == 1) { + container.m_imagedigest = *imageDigestSet.begin(); + } + } + else + { + g_logger.format(sinsp_logger::SEV_ERROR, + "docker_async (%s) image (%s): Could not parse json image info \"%s\"", + instruction.container_id.c_str(), + container.m_imageid.c_str(), + img_json.c_str()); + } + } + else + { + g_logger.format(sinsp_logger::SEV_ERROR, + "docker_async (%s) image (%s): Could not fetch image info", + instruction.container_id.c_str(), + container.m_imageid.c_str()); + } + + } + if(container.m_imagetag.empty()) + { + container.m_imagetag = "latest"; + } + + container.m_full_id = root["Id"].asString(); + container.m_name = root["Name"].asString(); + // k8s Docker container names could have '/' as the first character. + if(!container.m_name.empty() && container.m_name[0] == '/') + { + container.m_name = container.m_name.substr(1); + } + if(container.m_name.find("k8s_POD") == 0) + { + container.m_is_pod_sandbox = true; + } + + // Get the created time - this will be string format i.e. "%Y-%m-%dT%H:%M:%SZ" + // Convert it to seconds. This can be done with get_epoc_utc_seconds() + container.m_created_time = static_cast(get_epoch_utc_seconds(root["Created"].asString())); + + const Json::Value& net_obj = root["NetworkSettings"]; + + string ip = net_obj["IPAddress"].asString(); + + if(ip.empty()) + { + const Json::Value& hconfig_obj = root["HostConfig"]; + string net_mode = hconfig_obj["NetworkMode"].asString(); + + if(strncmp(net_mode.c_str(), "container:", strlen("container:")) == 0) + { + std::string secondary_container_id = net_mode.substr(net_mode.find(":") + 1); + + sinsp_container_info pcnt; + pcnt.m_id = secondary_container_id; + + // This is a *blocking* fetch of the + // secondary container, but we're in a + // separate thread so this is ok. + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s), secondary (%s): Doing blocking fetch of secondary container", + instruction.container_id.c_str(), + secondary_container_id.c_str()); + + if(parse_docker(docker_async_instruction(secondary_container_id, + false /*don't request size since we just need the IP*/), + pcnt)) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s), secondary (%s): Secondary fetch successful", + instruction.container_id.c_str(), + secondary_container_id.c_str()); + container.m_container_ip = pcnt.m_container_ip; + } + else + { + g_logger.format(sinsp_logger::SEV_ERROR, + "docker_async (%s), secondary (%s): Secondary fetch failed", + instruction.container_id.c_str(), + secondary_container_id.c_str()); + } + } + } + else + { + if(inet_pton(AF_INET, ip.c_str(), &container.m_container_ip) == -1) + { + ASSERT(false); + } + container.m_container_ip = ntohl(container.m_container_ip); + } + + vector ports = net_obj["Ports"].getMemberNames(); + for(vector::const_iterator it = ports.begin(); it != ports.end(); ++it) + { + size_t tcp_pos = it->find("/tcp"); + if(tcp_pos == string::npos) + { + continue; + } + + uint16_t container_port = atoi(it->c_str()); + + const Json::Value& v = net_obj["Ports"][*it]; + if(v.isArray()) + { + for(uint32_t j = 0; j < v.size(); ++j) + { + sinsp_container_info::container_port_mapping port_mapping; + + ip = v[j]["HostIp"].asString(); + string port = v[j]["HostPort"].asString(); + + if(inet_pton(AF_INET, ip.c_str(), &port_mapping.m_host_ip) == -1) + { + ASSERT(false); + continue; + } + port_mapping.m_host_ip = ntohl(port_mapping.m_host_ip); + + port_mapping.m_container_port = container_port; + port_mapping.m_host_port = atoi(port.c_str()); + container.m_port_mappings.push_back(port_mapping); + } + } + } + + vector labels = config_obj["Labels"].getMemberNames(); + for(vector::const_iterator it = labels.begin(); it != labels.end(); ++it) + { + string val = config_obj["Labels"][*it].asString(); + container.m_labels[*it] = val; + } + + const Json::Value& env_vars = config_obj["Env"]; + + for(const auto& env_var : env_vars) + { + if(env_var.isString()) + { + container.m_env.emplace_back(env_var.asString()); + } + } + + const auto& host_config_obj = root["HostConfig"]; + container.m_memory_limit = host_config_obj["Memory"].asInt64(); + container.m_swap_limit = host_config_obj["MemorySwap"].asInt64(); + const auto cpu_shares = host_config_obj["CpuShares"].asInt64(); + if(cpu_shares > 0) + { + container.m_cpu_shares = cpu_shares; + } + container.m_cpu_quota = host_config_obj["CpuQuota"].asInt64(); + const auto cpu_period = host_config_obj["CpuPeriod"].asInt64(); + if(cpu_period > 0) + { + container.m_cpu_period = cpu_period; + } + const auto cpuset_cpus = host_config_obj["CpusetCpus"].asString(); + if (!cpuset_cpus.empty()) + { + libsinsp::cgroup_list_counter counter; + container.m_cpuset_cpu_count = counter(cpuset_cpus.c_str()); + } + const Json::Value& privileged = host_config_obj["Privileged"]; + if(!privileged.isNull() && privileged.isBool()) + { + container.m_privileged = privileged.asBool(); + } + + docker::parse_json_mounts(root["Mounts"], container.m_mounts); + + container.m_size_rw_bytes = root["SizeRw"].asInt64(); + +#ifdef HAS_ANALYZER + sinsp_utils::find_env(container.m_sysdig_agent_conf, container.get_env(), "SYSDIG_AGENT_CONF"); + // container.m_sysdig_agent_conf = get_docker_env(env_vars, "SYSDIG_AGENT_CONF"); +#endif + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): parse_docker returning true", + instruction.container_id.c_str()); + return true; +} + +void docker::update_with_size(const std::string &container_id) +{ + auto cb = [this](const docker_async_instruction& instruction, const sinsp_container_info& res) { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): with size callback result=%d", + instruction.container_id.c_str(), + res.m_lookup_state); + + sinsp_container_info::ptr_t updated = make_shared(res); + container_cache().replace_container(updated); + }; + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async size request (%s)", + container_id.c_str()); + + sinsp_container_info result; + docker_async_instruction instruction(container_id, true /*request rw size*/); + (void)m_docker_info_source->lookup(instruction, result, cb); +} + +#endif // _WIN32 diff --git a/userspace/libsinsp/container_engine/docker_linux.cpp b/userspace/libsinsp/container_engine/docker_linux.cpp new file mode 100644 index 0000000000..d1a88bbe6f --- /dev/null +++ b/userspace/libsinsp/container_engine/docker_linux.cpp @@ -0,0 +1,222 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/docker.h" + +#include "runc.h" +#include "container_engine/mesos.h" +#include "sinsp.h" +#include "sinsp_int.h" + +using namespace libsinsp::container_engine; +using namespace libsinsp::runc; + +namespace { + +size_t docker_curl_write_callback(const char* ptr, size_t size, size_t nmemb, string* json) +{ + const std::size_t total = size * nmemb; + json->append(ptr, total); + return total; +} + +constexpr const cgroup_layout DOCKER_CGROUP_LAYOUT[] = { + {"/", ""}, // non-systemd docker + {"/docker-", ".scope"}, // systemd docker + {nullptr, nullptr} +}; +} + +std::string docker::m_docker_sock = "/var/run/docker.sock"; + +void docker::cleanup() +{ + m_docker_info_source.reset(NULL); +} + +void docker_async_source::init_docker_conn() +{ + if(!m_curlm) + { + m_curl = curl_easy_init(); + m_curlm = curl_multi_init(); + + if(m_curlm) + { + curl_multi_setopt(m_curlm, CURLMOPT_PIPELINING, CURLPIPE_HTTP1|CURLPIPE_MULTIPLEX); + } + + if(m_curl) + { + auto docker_path = scap_get_host_root() + m_docker_unix_socket_path; + curl_easy_setopt(m_curl, CURLOPT_UNIX_SOCKET_PATH, docker_path.c_str()); + curl_easy_setopt(m_curl, CURLOPT_HTTPGET, 1); + curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, docker_curl_write_callback); + } + } +} + +void docker_async_source::free_docker_conn() +{ + if(m_curl) + { + curl_easy_cleanup(m_curl); + m_curl = NULL; + } + + if(m_curlm) + { + curl_multi_cleanup(m_curlm); + m_curlm = NULL; + } +} + +std::string docker_async_source::build_request(const std::string &url) +{ + return "http://localhost" + m_api_version + url; +} + +docker_async_source::docker_response docker_async_source::get_docker(const std::string& url, std::string &json) +{ + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): Fetching url", + url.c_str()); + + if(curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str()) != CURLE_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): curl_easy_setopt(CURLOPT_URL) failed", + url.c_str()); + + ASSERT(false); + return docker_response::RESP_ERROR; + } + if(curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &json) != CURLE_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): curl_easy_setopt(CURLOPT_WRITEDATA) failed", + url.c_str()); + ASSERT(false); + return docker_response::RESP_ERROR; + } + + if(curl_multi_add_handle(m_curlm, m_curl) != CURLM_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): curl_multi_add_handle() failed", + url.c_str()); + ASSERT(false); + return docker_response::RESP_ERROR; + } + + while(true) + { + int still_running; + CURLMcode res = curl_multi_perform(m_curlm, &still_running); + if(res != CURLM_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): curl_multi_perform() failed", + url.c_str()); + + ASSERT(false); + return docker_response::RESP_ERROR; + } + + if(still_running == 0) + { + break; + } + + int numfds; + res = curl_multi_wait(m_curlm, NULL, 0, 1000, &numfds); + if(res != CURLM_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): curl_multi_wait() failed", + url.c_str()); + ASSERT(false); + return docker_response::RESP_ERROR; + } + } + + if(curl_multi_remove_handle(m_curlm, m_curl) != CURLM_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): curl_multi_remove_handle() failed", + url.c_str()); + + ASSERT(false); + return docker_response::RESP_ERROR; + } + + long http_code = 0; + if(curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &http_code) != CURLE_OK) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): curl_easy_getinfo(CURLINFO_RESPONSE_CODE) failed", + url.c_str()); + ASSERT(false); + return docker_response::RESP_ERROR; + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): http_code=%ld", + url.c_str(), http_code); + + switch(http_code) + { + case 0: /* connection failed, apparently */ + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): returning RESP_ERROR", + url.c_str()); + return docker_response::RESP_ERROR; + case 200: + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): returning RESP_OK", + url.c_str()); + return docker_response::RESP_OK; + default: + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): returning RESP_BAD_REQUEST", + url.c_str()); + return docker_response::RESP_BAD_REQUEST; + } + + g_logger.format(sinsp_logger::SEV_DEBUG, + "docker_async (%s): fallthrough, returning RESP_OK", + url.c_str()); + + return docker_response::RESP_OK; +} + +bool docker::detect_docker(const sinsp_threadinfo *tinfo, std::string &container_id, std::string &container_name) +{ + if(matches_runc_cgroups(tinfo, DOCKER_CGROUP_LAYOUT, container_id)) + { + // The container name is only available in windows + container_name = s_incomplete_info_name; + + return true; + } + + return false; +} diff --git a/userspace/libsinsp/container_engine/docker_win.cpp b/userspace/libsinsp/container_engine/docker_win.cpp new file mode 100644 index 0000000000..3b7584a2e7 --- /dev/null +++ b/userspace/libsinsp/container_engine/docker_win.cpp @@ -0,0 +1,95 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#ifdef CYGWING_AGENT + +#include "container_engine/docker.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "dragent_win_hal_public.h" + +using namespace libsinsp::container_engine; + +docker::docker(container_cache_interface& cache, const wmi_handle_source& wmi_source) : + container_engine_base(cache), + m_wmi_handle_source(wmi_source) +{ +} + +void docker::cleanup() +{ + g_docker_info_source.reset(NULL); +} + +void docker_async_source::init_docker_conn() +{ +} + +void docker_async_source::free_docker_conn() +{ +} + +std::string docker_async_source::build_request(const std::string &url) +{ + return "GET " + m_api_version + url + " HTTP/1.1\r\nHost: docker\r\n\r\n"; +} + +bool docker::detect_docker(sinsp_threadinfo *tinfo, std::string &container_id, std::string &container_name) +{ + wh_docker_container_info wcinfo = wh_docker_resolve_pid(m_wmi_handle_source.get_wmi_handle(), tinfo->m_pid); + if(!wcinfo.m_res) + { + return false; + } + + container_id = wcinfo.m_container_id; + container_name = wcinfo.m_container_name; + + return true; +} + +docker_async_source::docker_response docker_async_source::get_docker(const std::string& url, std::string &json) +{ + const char* response = NULL; + bool qdres = wh_query_docker(m_inspector->get_wmi_handle(), + (char*)url.c_str(), + &response); + if(qdres == false) + { + ASSERT(false); + return docker_response::RESP_ERROR; + } + + json = response; + if(strncmp(json.c_str(), "HTTP/1.0 200 OK", sizeof("HTTP/1.0 200 OK") -1)) + { + return docker_response::RESP_BAD_REQUEST; + } + + size_t pos = json.find("{"); + if(pos == string::npos) + { + ASSERT(false); + return docker_response::RESP_ERROR; + } + json = json.substr(pos); + + return docker_response::RESP_OK; +} + +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/container_engine/libvirt_lxc.cpp b/userspace/libsinsp/container_engine/libvirt_lxc.cpp new file mode 100644 index 0000000000..98e4b9e8ce --- /dev/null +++ b/userspace/libsinsp/container_engine/libvirt_lxc.cpp @@ -0,0 +1,93 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/libvirt_lxc.h" +#include "sinsp.h" + +using namespace libsinsp::container_engine; + +bool libvirt_lxc::match(sinsp_threadinfo* tinfo, sinsp_container_info &container_info) +{ + for(const auto& it : tinfo->m_cgroups) + { + // + // Non-systemd libvirt-lxc + // + const auto& cgroup = it.second; + size_t pos = cgroup.find(".libvirt-lxc"); + if(pos != std::string::npos && + pos == cgroup.length() - sizeof(".libvirt-lxc") + 1) + { + size_t pos2 = cgroup.find_last_of("/"); + if(pos2 != std::string::npos) + { + container_info.m_type = CT_LIBVIRT_LXC; + container_info.m_id = cgroup.substr(pos2 + 1, pos - pos2 - 1); + return true; + } + } + + // + // systemd libvirt-lxc + // + pos = cgroup.find("-lxc\\x2"); + if(pos != std::string::npos) + { + size_t pos2 = cgroup.find(".scope"); + if(pos2 != std::string::npos && + pos2 == cgroup.length() - sizeof(".scope") + 1) + { + container_info.m_type = CT_LIBVIRT_LXC; + container_info.m_id = cgroup.substr(pos + sizeof("-lxc\\x2"), pos2 - pos - sizeof("-lxc\\x2")); + return true; + } + } + + // + // Legacy libvirt-lxc + // + pos = cgroup.find("/libvirt/lxc/"); + if(pos != std::string::npos) + { + container_info.m_type = CT_LIBVIRT_LXC; + container_info.m_id = cgroup.substr(pos + sizeof("/libvirt/lxc/") - 1); + return true; + } + } + return false; +} + +bool libvirt_lxc::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) +{ + auto container = std::make_shared(); + + if (!match(tinfo, *container)) + { + return false; + } + + tinfo->m_container_id = container->m_id; + if(container_cache().should_lookup(container->m_id, CT_LIBVIRT_LXC)) + { + container->m_name = container->m_id; + container_cache().add_container(container, tinfo); + container_cache().notify_new_container(*container); + } + return true; +} diff --git a/userspace/libsinsp/container_engine/libvirt_lxc.h b/userspace/libsinsp/container_engine/libvirt_lxc.h new file mode 100644 index 0000000000..82f2b515f5 --- /dev/null +++ b/userspace/libsinsp/container_engine/libvirt_lxc.h @@ -0,0 +1,41 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +class sinsp_container_info; +class sinsp_threadinfo; + +#include "container_engine/container_engine_base.h" +#include "container_engine/sinsp_container_type.h" + +namespace libsinsp { +namespace container_engine { +class libvirt_lxc : public container_engine_base +{ +public: + libvirt_lxc(container_cache_interface &cache) : container_engine_base(cache) + {} + + bool resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) override; +protected: + bool match(sinsp_threadinfo* tinfo, sinsp_container_info &container_info); +}; +} +} diff --git a/userspace/libsinsp/container_engine/lxc.cpp b/userspace/libsinsp/container_engine/lxc.cpp new file mode 100644 index 0000000000..ba58ade38c --- /dev/null +++ b/userspace/libsinsp/container_engine/lxc.cpp @@ -0,0 +1,72 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/lxc.h" +#include "sinsp.h" + +using namespace libsinsp::container_engine; + +bool lxc::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) +{ + auto container = std::make_shared(); + bool matches = false; + + for(const auto& it : tinfo->m_cgroups) + { + // + // Non-systemd LXC + // + const auto& cgroup = it.second; + size_t pos = cgroup.find("/lxc/"); + if(pos != std::string::npos) + { + auto id_start = pos + sizeof("/lxc/") - 1; + auto id_end = cgroup.find('/', id_start); + container->m_type = CT_LXC; + container->m_id = cgroup.substr(id_start, id_end - id_start); + matches = true; + break; + } + + pos = cgroup.find("/lxc.payload/"); + if(pos != std::string::npos) + { + auto id_start = pos + sizeof("/lxc.payload/") - 1; + auto id_end = cgroup.find('/', id_start); + container->m_type = CT_LXC; + container->m_id = cgroup.substr(id_start, id_end - id_start); + matches = true; + break; + } + } + + if (!matches) + { + return false; + } + + tinfo->m_container_id = container->m_id; + if (container_cache().should_lookup(container->m_id, CT_LXC)) + { + container->m_name = container->m_id; + container_cache().add_container(container, tinfo); + container_cache().notify_new_container(*container); + } + return true; +} diff --git a/userspace/libsinsp/container_engine/lxc.h b/userspace/libsinsp/container_engine/lxc.h new file mode 100644 index 0000000000..9bf61e50fb --- /dev/null +++ b/userspace/libsinsp/container_engine/lxc.h @@ -0,0 +1,39 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +class sinsp_container_info; +class sinsp_threadinfo; + +#include "container_engine/container_engine_base.h" +#include "container_engine/sinsp_container_type.h" + +namespace libsinsp { +namespace container_engine { +class lxc : public container_engine_base +{ +public: + lxc(container_cache_interface &cache) : container_engine_base(cache) + {} + + bool resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) override; +}; +} +} diff --git a/userspace/libsinsp/container_engine/mesos.cpp b/userspace/libsinsp/container_engine/mesos.cpp new file mode 100644 index 0000000000..029980da0a --- /dev/null +++ b/userspace/libsinsp/container_engine/mesos.cpp @@ -0,0 +1,140 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/mesos.h" + +#include + +#include "sinsp.h" +#include "sinsp_int.h" + +bool libsinsp::container_engine::mesos::match(sinsp_threadinfo* tinfo, sinsp_container_info &container_info) +{ + for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it) + { + string cgroup = it->second; + size_t pos; + + pos = cgroup.find("/mesos/"); + if(pos != string::npos) + { + // It should match `/mesos/a9f41620-b165-4d24-abe0-af0af92e7b20` + auto id = cgroup.substr(pos + sizeof("/mesos/") - 1); + if(id.size() == 36 && id.find_first_not_of("0123456789abcdefABCDEF-") == string::npos) + { + container_info.m_type = CT_MESOS; + container_info.m_id = move(id); + // Consider a mesos container valid only if we find the mesos_task_id + // this will exclude from the container itself the mesos-executor + // but makes sure that we have task_id parsed properly. Otherwise what happens + // is that we'll create a mesos container struct without a mesos_task_id + // and for all other processes we'll use it + return set_mesos_task_id(container_info, tinfo); + } + } + } + return false; +} + +bool libsinsp::container_engine::mesos::resolve(sinsp_threadinfo* tinfo, bool query_os_for_missing_info) +{ + auto container = std::make_shared(); + + if (!match(tinfo, *container)) + return false; + + tinfo->m_container_id = container->m_id; + if(container_cache().should_lookup(container->m_id, CT_MESOS)) + { + container->m_name = container->m_id; + container_cache().add_container(container, tinfo); + container_cache().notify_new_container(*container); + } + return true; +} + +string libsinsp::container_engine::mesos::get_env_mesos_task_id(sinsp_threadinfo* tinfo) +{ + string mtid; + + sinsp_threadinfo::visitor_func_t visitor = [&mtid] (sinsp_threadinfo *ptinfo) + { + // Mesos task ID detection is not a straightforward task; + // this list may have to be extended. + mtid = ptinfo->get_env("MESOS_TASK_ID"); // Marathon + if(!mtid.empty()) { return false; } + mtid = ptinfo->get_env("mesos_task_id"); // Chronos + if(!mtid.empty()) { return false; } + mtid = ptinfo->get_env("MESOS_EXECUTOR_ID"); // others + if(!mtid.empty()) { return false; } + + return true; + }; + + // Try the current thread first. visitor returns true if mtid + // was not filled in. In this case we should traverse the + // parents. + if(tinfo && visitor(tinfo)) + { + tinfo->traverse_parent_state(visitor); + } + + return mtid; +} + +bool libsinsp::container_engine::mesos::set_mesos_task_id(sinsp_container_info &container, sinsp_threadinfo* tinfo) +{ + ASSERT(tinfo); + + // there are applications that do not share their environment in /proc/[PID]/environ + // since we need MESOS_TASK_ID environment variable to discover Mesos containers, + // there is a workaround for such cases: + // - for docker containers, we discover it directly from container, through Remote API + // (see sinsp_container_manager::parse_docker() for details) + // - for mesos native containers, parent process has the MESOS_TASK_ID (or equivalent, see + // get_env_mesos_task_id(sinsp_threadinfo*) implementation) environment variable, so we + // peek into the parent process environment to discover it + + if(tinfo) + { + string& mtid = container.m_mesos_task_id; + if(mtid.empty()) + { + mtid = get_env_mesos_task_id(tinfo); + + // Ensure that the mesos task id vaguely looks + // like a real id. We assume it must be at + // least 3 characters and contain a dot or underscore + if(!mtid.empty() && mtid.length()>=3 && + (mtid.find_first_of("._") != std::string::npos)) + { + g_logger.log("Mesos native container: [" + container.m_id + "], Mesos task ID: " + mtid, sinsp_logger::SEV_DEBUG); + return true; + } + else + { + g_logger.log("Mesos container [" + container.m_id + "]," + "thread [" + std::to_string(tinfo->m_tid) + + "], has likely malformed mesos task id [" + mtid + "], ignoring", sinsp_logger::SEV_DEBUG); + } + } + } + return false; +} + diff --git a/userspace/libsinsp/container_engine/mesos.h b/userspace/libsinsp/container_engine/mesos.h new file mode 100644 index 0000000000..13f6c5cc03 --- /dev/null +++ b/userspace/libsinsp/container_engine/mesos.h @@ -0,0 +1,48 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include + +class sinsp_container_info; +class sinsp_threadinfo; + +#include "container_engine/container_engine_base.h" +#include "container_engine/sinsp_container_type.h" + +namespace libsinsp { +namespace container_engine { +class mesos : public container_engine_base +{ +public: + mesos(container_cache_interface& cache) : container_engine_base(cache) + {} + + bool resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) override; + + static bool set_mesos_task_id(sinsp_container_info& container, sinsp_threadinfo *tinfo); + +protected: + bool match(sinsp_threadinfo *tinfo, sinsp_container_info& container_info); + + static std::string get_env_mesos_task_id(sinsp_threadinfo *tinfo); +}; +} +} diff --git a/userspace/libsinsp/container_engine/rkt.cpp b/userspace/libsinsp/container_engine/rkt.cpp new file mode 100644 index 0000000000..46de35006d --- /dev/null +++ b/userspace/libsinsp/container_engine/rkt.cpp @@ -0,0 +1,272 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "container_engine/rkt.h" + +#include + +#include "sinsp.h" +#include "sinsp_int.h" + +using namespace libsinsp::container_engine; + +bool rkt::match(container_cache_interface *cache, sinsp_threadinfo *tinfo, sinsp_container_info& container_info, string& rkt_podid, string& rkt_appname, bool query_os_for_missing_info) +{ + for(auto it = tinfo->m_cgroups.begin(); it != tinfo->m_cgroups.end(); ++it) + { + string cgroup = it->second; + + static const string COREOS_PODID_VAR = "container_uuid="; + static const string SYSTEMD_UUID_ARG = "--uuid="; + static const string SERVICE_SUFFIX = ".service"; + if(cgroup.rfind(SERVICE_SUFFIX) == cgroup.size() - SERVICE_SUFFIX.size()) + { + // check if there is a parent with pod uuid var + sinsp_threadinfo::visitor_func_t visitor = [&](sinsp_threadinfo* ptinfo) + { + for(const auto& env_var : ptinfo->get_env()) + { + auto container_uuid_pos = env_var.find(COREOS_PODID_VAR); + if(container_uuid_pos == 0) + { + rkt_podid = env_var.substr(COREOS_PODID_VAR.size()); + return false; + } + } + for(const auto& arg : ptinfo->m_args) + { + if(arg.find(SYSTEMD_UUID_ARG) != string::npos) + { + rkt_podid = arg.substr(SYSTEMD_UUID_ARG.size()); + return false; + } + } + return true; + }; + tinfo->traverse_parent_state(visitor); + + if(!rkt_podid.empty()) + { + auto last_slash = cgroup.find_last_of("/"); + rkt_appname = cgroup.substr(last_slash + 1, cgroup.size() - last_slash - SERVICE_SUFFIX.size() - 1); + + char image_manifest_path[SCAP_MAX_PATH_SIZE]; + snprintf(image_manifest_path, sizeof(image_manifest_path), "%s/var/lib/rkt/pods/run/%s/appsinfo/%s/manifest", scap_get_host_root(), rkt_podid.c_str(), rkt_appname.c_str()); + + // First lookup if the container exists in our table, otherwise only if we are live check if it has + // an entry in /var/lib/rkt. In capture mode only the former will be used. + // In live mode former will be used only if we already hit that container + bool is_rkt_pod_id_valid = cache->container_exists(rkt_podid + ":" + rkt_appname); // if it's already on our table +#ifdef HAS_CAPTURE + if(!is_rkt_pod_id_valid && query_os_for_missing_info) + { + is_rkt_pod_id_valid = (access(image_manifest_path, F_OK) == 0); + } +#endif + if(is_rkt_pod_id_valid) + { + container_info.m_type = CT_RKT; + container_info.m_id = rkt_podid + ":" + rkt_appname; + container_info.m_name = rkt_appname; + return true; + } + } + } + } + + // Try parsing from process root, + // Strings used to detect rkt stage1-cores pods + // TODO: detecting stage1-coreos rkt pods in this way is deprecated + // we can remove it in the future + static const string COREOS_PREFIX = "/opt/stage2/"; + static const string COREOS_APP_SUFFIX = "/rootfs"; + static const string COREOS_PODID_VAR = "container_uuid="; + + auto prefix = tinfo->m_root.find(COREOS_PREFIX); + if(prefix == 0) + { + auto suffix = tinfo->m_root.find(COREOS_APP_SUFFIX, prefix); + if(suffix != string::npos) + { + bool valid_id = false; + rkt_appname = tinfo->m_root.substr(prefix + COREOS_PREFIX.size(), suffix - prefix - COREOS_PREFIX.size()); + // It is a rkt pod with stage1-coreos + + sinsp_threadinfo::visitor_func_t visitor = [&] (sinsp_threadinfo *ptinfo) + { + for(const auto& env_var : ptinfo->get_env()) + { + auto container_uuid_pos = env_var.find(COREOS_PODID_VAR); + if(container_uuid_pos == 0) + { + rkt_podid = env_var.substr(COREOS_PODID_VAR.size()); + container_info.m_type = CT_RKT; + container_info.m_id = rkt_podid + ":" + rkt_appname; + container_info.m_name = rkt_appname; + valid_id = true; + return false; + } + } + return true; + }; + + // Try the current thread first. visitor returns true if no coreos pid + // info was found. In this case we traverse the parents. + if (visitor(tinfo)) + { + tinfo->traverse_parent_state(visitor); + } + return valid_id; + } + } + else + { + // String used to detect stage1-fly pods + static const string FLY_PREFIX = "/var/lib/rkt/pods/run/"; + static const string FLY_PODID_SUFFIX = "/stage1/rootfs/opt/stage2/"; + static const string FLY_APP_SUFFIX = "/rootfs"; + + auto prefix = tinfo->m_root.find(FLY_PREFIX); + if(prefix == 0) + { + auto podid_suffix = tinfo->m_root.find(FLY_PODID_SUFFIX, prefix+FLY_PREFIX.size()); + if(podid_suffix != string::npos) + { + rkt_podid = tinfo->m_root.substr(prefix + FLY_PREFIX.size(), podid_suffix - prefix - FLY_PREFIX.size()); + auto appname_suffix = tinfo->m_root.find(FLY_APP_SUFFIX, podid_suffix+FLY_PODID_SUFFIX.size()); + if(appname_suffix != string::npos) + { + rkt_appname = tinfo->m_root.substr(podid_suffix + FLY_PODID_SUFFIX.size(), + appname_suffix-podid_suffix-FLY_PODID_SUFFIX.size()); + container_info.m_type = CT_RKT; + container_info.m_id = rkt_podid + ":" + rkt_appname; + container_info.m_name = rkt_appname; + return true; + } + } + } + } + return false; +} + +bool rkt::rkt::resolve(sinsp_threadinfo* tinfo, bool query_os_for_missing_info) +{ + container_cache_interface *cache = &container_cache(); + + auto container = std::make_shared(); + string rkt_podid, rkt_appname; + + if (!match(cache, tinfo, *container, rkt_podid, rkt_appname, query_os_for_missing_info)) + { + return false; + } + + tinfo->m_container_id = container->m_id; + if (!query_os_for_missing_info || !cache->should_lookup(container->m_id, CT_RKT)) + { + return true; + } + +#ifndef _WIN32 + bool have_rkt = parse_rkt(*container, rkt_podid, rkt_appname); +#else + bool have_rkt = true; +#endif + + if (have_rkt) + { + cache->add_container(container, tinfo); + cache->notify_new_container(*container); + return true; + } + else + { + return false; + } +} + +bool rkt::rkt::parse_rkt(sinsp_container_info &container, const string &podid, const string &appname) +{ + bool ret = false; + Json::Reader reader; + Json::Value jroot; + + char image_manifest_path[SCAP_MAX_PATH_SIZE]; + snprintf(image_manifest_path, sizeof(image_manifest_path), "%s/var/lib/rkt/pods/run/%s/appsinfo/%s/manifest", scap_get_host_root(), podid.c_str(), appname.c_str()); + ifstream image_manifest(image_manifest_path); + if(reader.parse(image_manifest, jroot)) + { + container.m_image = jroot["name"].asString(); + for(const auto& label_entry : jroot["labels"]) + { + container.m_labels.emplace(label_entry["name"].asString(), label_entry["value"].asString()); + } + auto version_label_it = container.m_labels.find("version"); + if(version_label_it != container.m_labels.end()) + { + container.m_image += ":" + version_label_it->second; + } + ret = true; + } + + char net_info_path[SCAP_MAX_PATH_SIZE]; + snprintf(net_info_path, sizeof(net_info_path), "%s/var/lib/rkt/pods/run/%s/net-info.json", scap_get_host_root(), podid.c_str()); + ifstream net_info(net_info_path); + if(reader.parse(net_info, jroot) && jroot.size() > 0) + { + const auto& first_net = jroot[0]; + if(inet_pton(AF_INET, first_net["ip"].asCString(), &container.m_container_ip) == -1) + { + ASSERT(false); + } + container.m_container_ip = ntohl(container.m_container_ip); + } + + char pod_manifest_path[SCAP_MAX_PATH_SIZE]; + snprintf(pod_manifest_path, sizeof(pod_manifest_path), "%s/var/lib/rkt/pods/run/%s/pod", scap_get_host_root(), podid.c_str()); + ifstream pod_manifest(pod_manifest_path); + unordered_map image_ports; + if(reader.parse(pod_manifest, jroot) && jroot.size() > 0) + { + for(const auto& japp : jroot["apps"]) + { + if (japp["name"].asString() == appname) + { + for(const auto& image_port : japp["app"]["ports"]) + { + image_ports[image_port["name"].asString()] = image_port["port"].asUInt(); + } + break; + } + } + for(const auto& jport : jroot["ports"]) + { + auto host_port = jport["hostPort"].asUInt(); + auto container_port_it = image_ports.find(jport["name"].asString()); + if(host_port > 0 && container_port_it != image_ports.end()) + { + sinsp_container_info::container_port_mapping port_mapping; + port_mapping.m_host_port = host_port; + port_mapping.m_container_port = container_port_it->second; + container.m_port_mappings.emplace_back(move(port_mapping)); + } + } + } + return ret; +} diff --git a/userspace/libsinsp/container_engine/rkt.h b/userspace/libsinsp/container_engine/rkt.h new file mode 100644 index 0000000000..dd7f9eb162 --- /dev/null +++ b/userspace/libsinsp/container_engine/rkt.h @@ -0,0 +1,47 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include + +class sinsp_container_info; +class sinsp_threadinfo; + +#include "container_engine/container_engine_base.h" +#include "sinsp_container_type.h" + +namespace libsinsp { +namespace container_engine { +class rkt : public container_engine_base +{ +public: + rkt(container_cache_interface& cache) : container_engine_base(cache) + {} + + bool resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) override; + +protected: + bool match(container_cache_interface *cache, sinsp_threadinfo *tinfo, sinsp_container_info& container_info, + std::string& rkt_podid, std::string& rkt_appname, bool query_os_for_missing_info); + + bool parse_rkt(sinsp_container_info& container, const std::string& podid, const std::string& appname); +}; +} +} diff --git a/userspace/libsinsp/container_engine/sinsp_container_type.h b/userspace/libsinsp/container_engine/sinsp_container_type.h new file mode 100644 index 0000000000..1db1bcca24 --- /dev/null +++ b/userspace/libsinsp/container_engine/sinsp_container_type.h @@ -0,0 +1,34 @@ +/* +Copyright (C) 2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +enum sinsp_container_type +{ + CT_DOCKER = 0, + CT_LXC = 1, + CT_LIBVIRT_LXC = 2, + CT_MESOS = 3, + CT_RKT = 4, + CT_CUSTOM = 5, + CT_CRI = 6, + CT_CONTAINERD = 7, + CT_CRIO = 8, + CT_BPM = 9, +}; diff --git a/userspace/libsinsp/container_engine/wmi_handle_source.h b/userspace/libsinsp/container_engine/wmi_handle_source.h new file mode 100644 index 0000000000..f5ee094802 --- /dev/null +++ b/userspace/libsinsp/container_engine/wmi_handle_source.h @@ -0,0 +1,48 @@ +/* +Copyright (C) 2019 Sysdig Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +typedef struct wh_t wh_t; + +/** + * Interface to an object that can provide a Windows Management + * Instrumentation (WHI) handle. This is needed for windows support. + * + * This class does nothing for non-windows to enable simpler + * cross-compilation. + * + * Note that the intention here is to apply the Interface Segregation + * Principle (ISP) to class sinsp. Some clients of sinsp need only the + * get_capture_stats() API, and this interface exposes only that API. Do + * not add additional APIs here. If some client of sinsp needs a different + * set of APIs, introduce a new interface. + */ +class SINSP_PUBLIC wmi_handle_source +{ +public: + virtual ~wmi_handle_source() = default; + + +#ifdef CYGWING_AGENT + /** + * Return a wmi handle + */ + virtual wh_t* get_wmi_handle() = 0; +#endif +}; diff --git a/userspace/libsinsp/container_info.cpp b/userspace/libsinsp/container_info.cpp new file mode 100644 index 0000000000..6b63d16bed --- /dev/null +++ b/userspace/libsinsp/container_info.cpp @@ -0,0 +1,189 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 + +#include "container_info.h" +#include "sinsp.h" +#include "sinsp_int.h" + +std::vector sinsp_container_info::container_health_probe::probe_type_names = { + "None", + "Healthcheck", + "LivenessProbe", + "ReadinessProbe", + "End" +}; + +sinsp_container_info::container_health_probe::container_health_probe() +{ +} + +sinsp_container_info::container_health_probe::container_health_probe(const probe_type ptype, + const std::string &&exe, + const std::vector &&args) + : m_probe_type(ptype), + m_health_probe_exe(exe), + m_health_probe_args(args) +{ +} + +sinsp_container_info::container_health_probe::~container_health_probe() +{ +} + +void sinsp_container_info::container_health_probe::parse_health_probes(const Json::Value &config_obj, + std::list &probes) +{ + // Add any health checks described in the container config/labels. + for(int i=PT_NONE; i != PT_END; i++) + { + string key = probe_type_names[i]; + const Json::Value& probe_obj = config_obj[key]; + + if(!probe_obj.isNull() && probe_obj.isObject()) + { + const Json::Value& probe_exe_obj = probe_obj["exe"]; + + if(!probe_exe_obj.isNull() && probe_exe_obj.isConvertibleTo(Json::stringValue)) + { + const Json::Value& probe_args_obj = probe_obj["args"]; + + std::string probe_exe = probe_exe_obj.asString(); + std::vector probe_args; + + if(!probe_args_obj.isNull() && probe_args_obj.isArray()) + { + for(const auto &item : probe_args_obj) + { + if(item.isConvertibleTo(Json::stringValue)) + { + probe_args.push_back(item.asString()); + } + } + } + g_logger.format(sinsp_logger::SEV_DEBUG, + "add_health_probes: adding %s %s %d", + probe_type_names[i].c_str(), + probe_exe.c_str(), + probe_args.size()); + + probes.emplace_back(static_cast(i), std::move(probe_exe), std::move(probe_args)); + } + } + } +} + +void sinsp_container_info::container_health_probe::add_health_probes(const std::list &probes, + Json::Value &config_obj) +{ + for(auto &probe : probes) + { + string key = probe_type_names[probe.m_probe_type]; + Json::Value args; + + config_obj[key]["exe"] = probe.m_health_probe_exe; + for(auto &arg : probe.m_health_probe_args) + { + args.append(arg); + } + + config_obj[key]["args"] = args; + } +} + +const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_idx(uint32_t idx) const +{ + if (idx >= m_mounts.size()) + { + return NULL; + } + + return &(m_mounts[idx]); +} + +const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_source(std::string &source) const +{ + // note: linear search + for (auto &mntinfo :m_mounts) + { + if(sinsp_utils::glob_match(source.c_str(), mntinfo.m_source.c_str())) + { + return &mntinfo; + } + } + + return NULL; +} + +const sinsp_container_info::container_mount_info *sinsp_container_info::mount_by_dest(std::string &dest) const +{ + // note: linear search + for (auto &mntinfo :m_mounts) + { + if(sinsp_utils::glob_match(dest.c_str(), mntinfo.m_dest.c_str())) + { + return &mntinfo; + } + } + + return NULL; +} + +std::shared_ptr sinsp_container_info::get_tinfo(sinsp* inspector) const +{ + std::shared_ptr tinfo(inspector->build_threadinfo()); + tinfo->m_tid = -1; + tinfo->m_pid = -1; + tinfo->m_vtid = -2; + tinfo->m_vpid = -2; + tinfo->m_comm = "container:" + m_id; + tinfo->m_container_id = m_id; + + return tinfo; +} + +sinsp_container_info::container_health_probe::probe_type sinsp_container_info::match_health_probe(sinsp_threadinfo *tinfo) const +{ + g_logger.format(sinsp_logger::SEV_DEBUG, + "match_health_probe (%s): %u health probes to consider", + m_id.c_str(), m_health_probes.size()); + + auto pred = [&] (const container_health_probe &p) { + g_logger.format(sinsp_logger::SEV_DEBUG, + "match_health_probe (%s): Matching tinfo %s %d against %s %d", + m_id.c_str(), + tinfo->m_exe.c_str(), tinfo->m_args.size(), + p.m_health_probe_exe.c_str(), p.m_health_probe_args.size()); + + return (p.m_health_probe_exe == tinfo->m_exe && + p.m_health_probe_args == tinfo->m_args); + }; + + auto match = std::find_if(m_health_probes.begin(), + m_health_probes.end(), + pred); + + if(match == m_health_probes.end()) + { + return container_health_probe::PT_NONE; + } + + return match->m_probe_type; +} diff --git a/userspace/libsinsp/container_info.h b/userspace/libsinsp/container_info.h new file mode 100644 index 0000000000..b51c3fe593 --- /dev/null +++ b/userspace/libsinsp/container_info.h @@ -0,0 +1,269 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include "container_engine/sinsp_container_type.h" +#include "json/json.h" + +class sinsp; +class sinsp_threadinfo; + +namespace std { +template<> struct hash { + std::size_t operator()(const sinsp_container_type& h) const { + return std::hash{}(static_cast(h)); + } +}; +} + +class sinsp_threadinfo; + +// Docker and CRI-compatible runtimes are very similar +static inline bool is_docker_compatible(sinsp_container_type t) +{ + return t == CT_DOCKER || + t == CT_CRI || + t == CT_CONTAINERD || + t == CT_CRIO; +} + +/** + * \brief the state of a container metadata lookup + * + * Some container engines (Docker, CRI) do external API calls to find container + * metadata. This value stores the state of the lookup (a separate value is kept + * for each container_id/engine pair). The purpose is to avoid repeated lookups + * after failure, especially when multiple engines match against the same process + * (e.g. Docker and containerd may use the same cgroup layout). + * + * If all engines fail to find metadata for a container, we need to remember that + * for each engine individually and there's only one sinsp_container_info->m_type + */ +enum class sinsp_container_lookup_state { + STARTED = 0, + SUCCESSFUL = 1, + FAILED = 2 +}; + +class sinsp_container_info +{ +public: + using ptr_t = std::shared_ptr; + + class container_port_mapping + { + public: + container_port_mapping(): + m_host_ip(0), + m_host_port(0), + m_container_port(0) + { + } + uint32_t m_host_ip; + uint16_t m_host_port; + uint16_t m_container_port; + }; + + class container_mount_info + { + public: + container_mount_info(): + m_source(""), + m_dest(""), + m_mode(""), + m_rdwr(false), + m_propagation("") + { + } + + container_mount_info(const std::string&& source, const std::string&& dest, + const std::string&& mode, const bool rw, + const std::string&& propagation) : + m_source(source), m_dest(dest), m_mode(mode), m_rdwr(rw), m_propagation(propagation) + { + } + + container_mount_info(const Json::Value &source, const Json::Value &dest, + const Json::Value &mode, const Json::Value &rw, + const Json::Value &propagation) + { + get_string_value(source, m_source); + get_string_value(dest, m_dest); + get_string_value(mode, m_mode); + get_string_value(propagation, m_propagation); + + if(!rw.isNull() && rw.isBool()) + { + m_rdwr = rw.asBool(); + } + } + + std::string to_string() const + { + return m_source + ":" + + m_dest + ":" + + m_mode + ":" + + (m_rdwr ? "true" : "false") + ":" + + m_propagation; + } + + inline void get_string_value(const Json::Value &val, std::string &result) + { + if(!val.isNull() && val.isString()) + { + result = val.asString(); + } + } + + std::string m_source; + std::string m_dest; + std::string m_mode; + bool m_rdwr; + std::string m_propagation; + }; + + class container_health_probe + { + public: + + // The type of health probe + enum probe_type { + PT_NONE = 0, + PT_HEALTHCHECK, + PT_LIVENESS_PROBE, + PT_READINESS_PROBE, + PT_END + }; + + // String representations of the above, suitable for + // parsing to/from json. Should be kept in sync with + // probe_type enum. + static std::vector probe_type_names; + + // Parse any health probes out of the provided + // container json, updating the list of probes. + static void parse_health_probes(const Json::Value &config_obj, + std::list &probes); + + // Serialize the list of health probes, adding to the provided json object + static void add_health_probes(const std::list &probes, + Json::Value &config_obj); + + container_health_probe(); + container_health_probe(const probe_type probe_type, + const std::string &&exe, + const std::vector &&args); + virtual ~container_health_probe(); + + // The probe_type that should be used for commands + // matching this health probe. + probe_type m_probe_type; + + // The actual health probe exe and args. + std::string m_health_probe_exe; + std::vector m_health_probe_args; + }; + + sinsp_container_info(): + m_container_ip(0), + m_privileged(false), + m_memory_limit(0), + m_swap_limit(0), + m_cpu_shares(1024), + m_cpu_quota(0), + m_cpu_period(100000), + m_cpuset_cpu_count(0), + m_is_pod_sandbox(false), + m_lookup_state(sinsp_container_lookup_state::SUCCESSFUL), + m_metadata_deadline(0), + m_size_rw_bytes(-1) + { + } + + const std::vector& get_env() const { return m_env; } + + const container_mount_info *mount_by_idx(uint32_t idx) const; + const container_mount_info *mount_by_source(std::string &source) const; + const container_mount_info *mount_by_dest(std::string &dest) const; + + bool is_pod_sandbox() const { + return m_is_pod_sandbox; + } + + bool is_successful() const { + return m_lookup_state == sinsp_container_lookup_state::SUCCESSFUL; + } + + std::shared_ptr get_tinfo(sinsp* inspector) const; + + // Match a process against the set of health probes + container_health_probe::probe_type match_health_probe(sinsp_threadinfo *tinfo) const; + + std::string m_id; + std::string m_full_id; + sinsp_container_type m_type; + std::string m_name; + std::string m_image; + std::string m_imageid; + std::string m_imagerepo; + std::string m_imagetag; + std::string m_imagedigest; + uint32_t m_container_ip; + bool m_privileged; + std::vector m_mounts; + std::vector m_port_mappings; + std::map m_labels; + std::vector m_env; + std::string m_mesos_task_id; + int64_t m_memory_limit; + int64_t m_swap_limit; + int64_t m_cpu_shares; + int64_t m_cpu_quota; + int64_t m_cpu_period; + int32_t m_cpuset_cpu_count; + std::list m_health_probes; + + bool m_is_pod_sandbox; + + sinsp_container_lookup_state m_lookup_state; +#ifdef HAS_ANALYZER + std::string m_sysdig_agent_conf; +#endif + uint64_t m_metadata_deadline; + + /** + * The size of files that have been created or changed by this container. + * This is not filled by default. + */ + int64_t m_size_rw_bytes; + + /** + * The time at which the container was created (IN SECONDS), cast from a value of `time_t` + * We choose int64_t as we are not certain what type `time_t` is in a given + * implementation; int64_t is the safest bet. Many default to int64_t anyway (e.g. CRI). + */ + int64_t m_created_time; +}; diff --git a/userspace/libsinsp/cri.cpp b/userspace/libsinsp/cri.cpp new file mode 100644 index 0000000000..498b18993e --- /dev/null +++ b/userspace/libsinsp/cri.cpp @@ -0,0 +1,427 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "cri.h" + +#include +#include "grpc_channel_registry.h" + +#include "sinsp.h" +#include "sinsp_int.h" + +namespace { +bool pod_uses_host_netns(const runtime::v1alpha2::PodSandboxStatusResponse& resp) +{ + const auto netns = resp.status().linux().namespaces().options().network(); + return netns == runtime::v1alpha2::NODE; +} +} + +namespace libsinsp { +namespace cri { +std::string s_cri_unix_socket_path = "/run/containerd/containerd.sock"; +int64_t s_cri_timeout = 1000; +int64_t s_cri_size_timeout = 10000; +sinsp_container_type s_cri_runtime_type = CT_CRI; +bool s_cri_extra_queries = true; + +cri_interface::cri_interface(const std::string& cri_path) +{ + std::shared_ptr channel = libsinsp::grpc_channel_registry::get_channel("unix://" + cri_path); + + m_cri = runtime::v1alpha2::RuntimeService::NewStub(channel); + + runtime::v1alpha2::VersionRequest vreq; + runtime::v1alpha2::VersionResponse vresp; + + vreq.set_version("v1alpha2"); + grpc::ClientContext context; + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(s_cri_timeout); + context.set_deadline(deadline); + grpc::Status status = m_cri->Version(&context, vreq, &vresp); + + if (!status.ok()) + { + g_logger.format(sinsp_logger::SEV_NOTICE, "cri: CRI runtime returned an error after version check at %s: %s", + s_cri_unix_socket_path.c_str(), status.error_message().c_str()); + m_cri.reset(nullptr); + s_cri_unix_socket_path = ""; + return; + } + + g_logger.format(sinsp_logger::SEV_INFO, "cri: CRI runtime: %s %s", vresp.runtime_name().c_str(), vresp.runtime_version().c_str()); + + m_cri_image = runtime::v1alpha2::ImageService::NewStub(channel); + + const std::string& runtime_name = vresp.runtime_name(); + if(runtime_name == "containerd") + { + m_cri_runtime_type = CT_CONTAINERD; + } else if(runtime_name == "cri-o") + { + m_cri_runtime_type = CT_CRIO; + } else + { + m_cri_runtime_type = CT_CRI; + } + + s_cri_runtime_type = m_cri_runtime_type; +} + +sinsp_container_type cri_interface::get_cri_runtime_type() const +{ + return m_cri_runtime_type; +} + +grpc::Status cri_interface::get_container_status(const std::string& container_id, runtime::v1alpha2::ContainerStatusResponse& resp) +{ + runtime::v1alpha2::ContainerStatusRequest req; + req.set_container_id(container_id); + req.set_verbose(true); + grpc::ClientContext context; + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(s_cri_timeout); + context.set_deadline(deadline); + return m_cri->ContainerStatus(&context, req, &resp); +} + +grpc::Status cri_interface::get_container_stats(const std::string& container_id, runtime::v1alpha2::ContainerStatsResponse& resp) +{ + runtime::v1alpha2::ContainerStatsRequest req; + req.set_container_id(container_id); + grpc::ClientContext context; + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(s_cri_size_timeout); + context.set_deadline(deadline); + return m_cri->ContainerStats(&context, req, &resp); +} + +bool cri_interface::parse_cri_image(const runtime::v1alpha2::ContainerStatus &status, sinsp_container_info &container) +{ + // image_ref may be one of two forms: + // host/image@sha256:digest + // sha256:digest + + bool have_digest = false; + const auto &image_ref = status.image_ref(); + auto digest_start = image_ref.find("sha256:"); + switch (digest_start) + { + case 0: // sha256:digest + have_digest = true; + break; + case string::npos: + break; + default: // host/image@sha256:digest + have_digest = image_ref[digest_start - 1] == '@'; + } + + string hostname, port, digest; + sinsp_utils::split_container_image(status.image().image(), + hostname, + port, + container.m_imagerepo, + container.m_imagetag, + digest, + false); + container.m_image = status.image().image(); + + + if(have_digest) + { + container.m_imagedigest = image_ref.substr(digest_start); + } + else + { + container.m_imagedigest = digest; + } + return true; +} + +bool cri_interface::parse_cri_mounts(const runtime::v1alpha2::ContainerStatus &status, sinsp_container_info &container) +{ + for(const auto &mount : status.mounts()) + { + const char *propagation; + switch(mount.propagation()) + { + case runtime::v1alpha2::MountPropagation::PROPAGATION_PRIVATE: + propagation = "private"; + break; + case runtime::v1alpha2::MountPropagation::PROPAGATION_HOST_TO_CONTAINER: + propagation = "rslave"; + break; + case runtime::v1alpha2::MountPropagation::PROPAGATION_BIDIRECTIONAL: + propagation = "rshared"; + break; + default: + propagation = "unknown"; + break; + } + container.m_mounts.emplace_back( + mount.host_path(), + mount.container_path(), + "", + !mount.readonly(), + propagation); + } + return true; + +} + +bool walk_down_json(const Json::Value &root, const Json::Value **out, const std::string &key) +{ + if(root.isMember(key)) + { + *out = &root[key]; + return true; + } + return false; +} + +template +bool walk_down_json(const Json::Value &root, const Json::Value **out, const std::string &key, Args... args) +{ + if(root.isMember(key)) + { + return walk_down_json(root[key], out, args...); + } + return false; +} + +bool set_numeric_32(const Json::Value& dict, const std::string& key, int32_t& val) +{ + if (!dict.isMember(key)) + { + return false; + } + const auto& json_val = dict[key]; + if (!json_val.isNumeric()) + { + return false; + } + val = json_val.asInt(); + return true; +} + +bool set_numeric_64(const Json::Value &dict, const std::string &key, int64_t &val) +{ + if(!dict.isMember(key)) + { + return false; + } + const auto &json_val = dict[key]; + if(!json_val.isNumeric()) + { + return false; + } + val = json_val.asInt64(); + return true; +} + +bool cri_interface::parse_cri_env(const Json::Value &info, sinsp_container_info &container) +{ + const Json::Value *envs; + if(!walk_down_json(info, &envs, "config", "envs") || !envs->isArray()) + { + return false; + } + + for(const auto &env_var : *envs) + { + const auto &key = env_var["key"]; + const auto &value = env_var["value"]; + + if(key.isString() && value.isString()) + { + auto var = key.asString(); + var += '='; + var += value.asString(); + container.m_env.emplace_back(var); + } + } + + return true; +} + +bool cri_interface::parse_cri_json_image(const Json::Value &info, sinsp_container_info &container) +{ + const Json::Value *image; + if(!walk_down_json(info, &image, "config", "image", "image") || !image->isString()) + { + return false; + } + + auto image_str = image->asString(); + auto pos = image_str.find(':'); + if(pos == string::npos) + { + container.m_imageid = move(image_str); + } else + { + container.m_imageid = image_str.substr(pos + 1); + } + + return true; +} + +bool cri_interface::parse_cri_runtime_spec(const Json::Value &info, sinsp_container_info &container) +{ + const Json::Value *linux = nullptr; + if(!walk_down_json(info, &linux, "runtimeSpec", "linux") || !linux->isObject()) + { + return false; + } + + const Json::Value *memory = nullptr; + if(walk_down_json(*linux, &memory, "resources", "memory")) + { + set_numeric_64(*memory, "limit", container.m_memory_limit); + container.m_swap_limit = container.m_memory_limit; + } + + const Json::Value *cpu = nullptr; + if(walk_down_json(*linux, &cpu, "resources", "cpu") && cpu->isObject()) + { + set_numeric_64(*cpu, "shares", container.m_cpu_shares); + set_numeric_64(*cpu, "quota", container.m_cpu_quota); + set_numeric_64(*cpu, "period", container.m_cpu_period); + set_numeric_32(*cpu, "cpuset_cpu_count", container.m_cpuset_cpu_count); + } + + const Json::Value *privileged; + if(walk_down_json(*linux, &privileged, "security_context", "privileged") && privileged->isBool()) + { + container.m_privileged = privileged->asBool(); + } + + return true; +} + +bool cri_interface::is_pod_sandbox(const std::string &container_id) +{ + runtime::v1alpha2::PodSandboxStatusRequest req; + runtime::v1alpha2::PodSandboxStatusResponse resp; + req.set_pod_sandbox_id(container_id); + req.set_verbose(true); + grpc::ClientContext context; + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(s_cri_timeout); + context.set_deadline(deadline); + grpc::Status status = m_cri->PodSandboxStatus(&context, req, &resp); + + return status.ok(); +} + +uint32_t cri_interface::get_pod_sandbox_ip(const std::string &pod_sandbox_id) +{ + runtime::v1alpha2::PodSandboxStatusRequest req; + runtime::v1alpha2::PodSandboxStatusResponse resp; + req.set_pod_sandbox_id(pod_sandbox_id); + req.set_verbose(true); + grpc::ClientContext context; + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(s_cri_timeout); + context.set_deadline(deadline); + grpc::Status status = m_cri->PodSandboxStatus(&context, req, &resp); + + if(!status.ok()) + { + return 0; + } + + if(pod_uses_host_netns(resp)) + { + return 0; + } + + const auto &pod_ip = resp.status().network().ip(); + if(pod_ip.empty()) + { + return 0; + } + + uint32_t ip; + if(inet_pton(AF_INET, pod_ip.c_str(), &ip) == -1) + { + ASSERT(false); + return 0; + } else + { + return ip; + } +} + +uint32_t cri_interface::get_container_ip(const std::string &container_id) +{ + runtime::v1alpha2::ListContainersRequest req; + runtime::v1alpha2::ListContainersResponse resp; + auto filter = req.mutable_filter(); + filter->set_id(container_id); + grpc::ClientContext context; + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(s_cri_timeout); + context.set_deadline(deadline); + grpc::Status lstatus = m_cri->ListContainers(&context, req, &resp); + + switch(resp.containers_size()) + { + case 0: + g_logger.format(sinsp_logger::SEV_WARNING, "Container id %s not in list from CRI", container_id.c_str()); + ASSERT(false); + break; + case 1: { + const auto& cri_container = resp.containers(0); + return ntohl(get_pod_sandbox_ip(cri_container.pod_sandbox_id())); + } + default: + g_logger.format(sinsp_logger::SEV_WARNING, "Container id %s matches more than once in list from CRI", container_id.c_str()); + ASSERT(false); + break; + } + return 0; +} + +std::string cri_interface::get_container_image_id(const std::string &image_ref) +{ + runtime::v1alpha2::ListImagesRequest req; + runtime::v1alpha2::ListImagesResponse resp; + auto filter = req.mutable_filter(); + auto spec = filter->mutable_image(); + spec->set_image(image_ref); + grpc::ClientContext context; + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(s_cri_timeout); + context.set_deadline(deadline); + grpc::Status status = m_cri_image->ListImages(&context, req, &resp); + + switch(resp.images_size()) + { + case 0: + g_logger.format(sinsp_logger::SEV_WARNING, "Image ref %s not in list from CRI", image_ref.c_str()); + ASSERT(false); + break; + case 1: { + const auto& image = resp.images(0); + return image.id(); + } + default: + g_logger.format(sinsp_logger::SEV_WARNING, "Image ref %s matches more than once in list from CRI", image_ref.c_str()); + ASSERT(false); + break; + } + + return ""; +} +} +} diff --git a/userspace/libsinsp/cri.h b/userspace/libsinsp/cri.h new file mode 100644 index 0000000000..19606fd57c --- /dev/null +++ b/userspace/libsinsp/cri.h @@ -0,0 +1,169 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#include + +#ifndef MINIMAL_BUILD +#include "cri.pb.h" +#include "cri.grpc.pb.h" +#endif // MINIMAL_BUILD + +#include "container_info.h" + +#ifdef GRPC_INCLUDE_IS_GRPCPP +# include +#else +# include +#endif + +namespace libsinsp { +namespace cri { + +// these shouldn't be globals but we still need references to *the* CRI runtime +extern std::string s_cri_unix_socket_path; +extern int64_t s_cri_timeout; +extern sinsp_container_type s_cri_runtime_type; +extern bool s_cri_extra_queries; + +class cri_interface +{ +public: + cri_interface(const std::string& cri_path); + + /** + * @brief did we manage to connect to CRI and get the runtime name/version? + * @return true if successfully connected to CRI + */ + bool is_ok() const + { + return m_cri != nullptr; + } + + /** + * @brief get the detected CRI runtime type + * @return one of CT_CRIO, CT_CONTAINERD, CT_CRI (for other CRI runtimes) + * corresponding to the CRI runtime type detected + */ + sinsp_container_type get_cri_runtime_type() const; + + /** + * @brief thin wrapper around CRI gRPC ContainerStatus call + * @param container_id container ID + * @param resp reference to the response (if the RPC is successful, it will be filled out) + * @return status of the gRPC call + */ + grpc::Status get_container_status(const std::string& container_id, runtime::v1alpha2::ContainerStatusResponse& resp); + + /** + * @brief thin wrapper around CRI gRPC ContainerStats call + * @param container_id container ID + * @param resp reference to the response (if the RPC is successful, it will be filled out) + * @return status of the gRPC call + */ + grpc::Status get_container_stats(const std::string& container_id, runtime::v1alpha2::ContainerStatsResponse& resp); + + /** + * @brief fill out container image information based on CRI response + * @param status `status` field of the ContainerStatusResponse + * @param container the container info to fill out + * @return true if successful + */ + bool parse_cri_image(const runtime::v1alpha2::ContainerStatus &status, sinsp_container_info &container); + + /** + * @brief fill out container mount information based on CRI response + * @param status `status` field of the ContainerStatusResponse + * @param container the container info to fill out + * @return true if successful + */ + bool parse_cri_mounts(const runtime::v1alpha2::ContainerStatus &status, sinsp_container_info &container); + + /** + * @brief fill out container environment variables based on CRI response + * @param info the `info` key of the `info` field of the ContainerStatusResponse + * @param container the container info to fill out + * @return true if successful + * + * Note: only containerd exposes this data + */ + bool parse_cri_env(const Json::Value &info, sinsp_container_info &container); + + /** + * @brief fill out extra image info based on CRI response + * @param info the `info` key of the `info` field of the ContainerStatusResponse + * @param container the container info to fill out + * @return true if successful + * + * Note: only containerd exposes this data + */ + bool parse_cri_json_image(const Json::Value &info, sinsp_container_info &container); + + /** + * @brief fill out extra container info (e.g. resource limits) based on CRI response + * @param info the `info` key of the `info` field of the ContainerStatusResponse + * @param container the container info to fill out + * @return true if successful + * + * Note: only containerd exposes this data + */ + bool parse_cri_runtime_spec(const Json::Value &info, sinsp_container_info &container); + + /** + * @brief check if the passed container ID is a pod sandbox (pause container) + * @param container_id the container ID to check + * @return true if it's a pod sandbox + */ + bool is_pod_sandbox(const std::string &container_id); + + /** + * @brief get pod IP address + * @param pod_sandbox_id container ID of the pod sandbox + * @return the IP address if possible, 0 otherwise (e.g. when the pod uses host netns) + */ + uint32_t get_pod_sandbox_ip(const std::string &pod_sandbox_id); + + /** + * @brief get container IP address + * @param container_id the container ID + * @return the IP address if possible, 0 otherwise (e.g. when the pod uses host netns) + * + * This method first finds the pod ID, then gets the IP address + * of the pod sandbox container + */ + uint32_t get_container_ip(const std::string &container_id); + + /** + * @brief get image id info from CRI + * @param image_ref the image ref from container metadata + * @return image id if found, empty string otherwise + */ + std::string get_container_image_id(const std::string &image_ref); + +private: + + std::unique_ptr m_cri; + std::unique_ptr m_cri_image; + sinsp_container_type m_cri_runtime_type; +}; + +} +} diff --git a/userspace/libsinsp/cri.proto b/userspace/libsinsp/cri.proto new file mode 100644 index 0000000000..6e060c639f --- /dev/null +++ b/userspace/libsinsp/cri.proto @@ -0,0 +1,1256 @@ +/* +Copyright 2018 The Kubernetes Authors. + +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. +*/ + +// To regenerate api.pb.go run hack/update-generated-runtime.sh +syntax = 'proto3'; + +package runtime.v1alpha2; +option go_package = "v1alpha2"; + +// Runtime service defines the public APIs for remote container runtimes +service RuntimeService { + // Version returns the runtime name, runtime version, and runtime API version. + rpc Version(VersionRequest) returns (VersionResponse) {} + + // RunPodSandbox creates and starts a pod-level sandbox. Runtimes must ensure + // the sandbox is in the ready state on success. + rpc RunPodSandbox(RunPodSandboxRequest) returns (RunPodSandboxResponse) {} + // StopPodSandbox stops any running process that is part of the sandbox and + // reclaims network resources (e.g., IP addresses) allocated to the sandbox. + // If there are any running containers in the sandbox, they must be forcibly + // terminated. + // This call is idempotent, and must not return an error if all relevant + // resources have already been reclaimed. kubelet will call StopPodSandbox + // at least once before calling RemovePodSandbox. It will also attempt to + // reclaim resources eagerly, as soon as a sandbox is not needed. Hence, + // multiple StopPodSandbox calls are expected. + rpc StopPodSandbox(StopPodSandboxRequest) returns (StopPodSandboxResponse) {} + // RemovePodSandbox removes the sandbox. If there are any running containers + // in the sandbox, they must be forcibly terminated and removed. + // This call is idempotent, and must not return an error if the sandbox has + // already been removed. + rpc RemovePodSandbox(RemovePodSandboxRequest) returns (RemovePodSandboxResponse) {} + // PodSandboxStatus returns the status of the PodSandbox. If the PodSandbox is not + // present, returns an error. + rpc PodSandboxStatus(PodSandboxStatusRequest) returns (PodSandboxStatusResponse) {} + // ListPodSandbox returns a list of PodSandboxes. + rpc ListPodSandbox(ListPodSandboxRequest) returns (ListPodSandboxResponse) {} + + // CreateContainer creates a new container in specified PodSandbox + rpc CreateContainer(CreateContainerRequest) returns (CreateContainerResponse) {} + // StartContainer starts the container. + rpc StartContainer(StartContainerRequest) returns (StartContainerResponse) {} + // StopContainer stops a running container with a grace period (i.e., timeout). + // This call is idempotent, and must not return an error if the container has + // already been stopped. + // TODO: what must the runtime do after the grace period is reached? + rpc StopContainer(StopContainerRequest) returns (StopContainerResponse) {} + // RemoveContainer removes the container. If the container is running, the + // container must be forcibly removed. + // This call is idempotent, and must not return an error if the container has + // already been removed. + rpc RemoveContainer(RemoveContainerRequest) returns (RemoveContainerResponse) {} + // ListContainers lists all containers by filters. + rpc ListContainers(ListContainersRequest) returns (ListContainersResponse) {} + // ContainerStatus returns status of the container. If the container is not + // present, returns an error. + rpc ContainerStatus(ContainerStatusRequest) returns (ContainerStatusResponse) {} + // UpdateContainerResources updates ContainerConfig of the container. + rpc UpdateContainerResources(UpdateContainerResourcesRequest) returns (UpdateContainerResourcesResponse) {} + // ReopenContainerLog asks runtime to reopen the stdout/stderr log file + // for the container. This is often called after the log file has been + // rotated. If the container is not running, container runtime can choose + // to either create a new log file and return nil, or return an error. + // Once it returns error, new container log file MUST NOT be created. + rpc ReopenContainerLog(ReopenContainerLogRequest) returns (ReopenContainerLogResponse) {} + + // ExecSync runs a command in a container synchronously. + rpc ExecSync(ExecSyncRequest) returns (ExecSyncResponse) {} + // Exec prepares a streaming endpoint to execute a command in the container. + rpc Exec(ExecRequest) returns (ExecResponse) {} + // Attach prepares a streaming endpoint to attach to a running container. + rpc Attach(AttachRequest) returns (AttachResponse) {} + // PortForward prepares a streaming endpoint to forward ports from a PodSandbox. + rpc PortForward(PortForwardRequest) returns (PortForwardResponse) {} + + // ContainerStats returns stats of the container. If the container does not + // exist, the call returns an error. + rpc ContainerStats(ContainerStatsRequest) returns (ContainerStatsResponse) {} + // ListContainerStats returns stats of all running containers. + rpc ListContainerStats(ListContainerStatsRequest) returns (ListContainerStatsResponse) {} + + // UpdateRuntimeConfig updates the runtime configuration based on the given request. + rpc UpdateRuntimeConfig(UpdateRuntimeConfigRequest) returns (UpdateRuntimeConfigResponse) {} + + // Status returns the status of the runtime. + rpc Status(StatusRequest) returns (StatusResponse) {} +} + +// ImageService defines the public APIs for managing images. +service ImageService { + // ListImages lists existing images. + rpc ListImages(ListImagesRequest) returns (ListImagesResponse) {} + // ImageStatus returns the status of the image. If the image is not + // present, returns a response with ImageStatusResponse.Image set to + // nil. + rpc ImageStatus(ImageStatusRequest) returns (ImageStatusResponse) {} + // PullImage pulls an image with authentication config. + rpc PullImage(PullImageRequest) returns (PullImageResponse) {} + // RemoveImage removes the image. + // This call is idempotent, and must not return an error if the image has + // already been removed. + rpc RemoveImage(RemoveImageRequest) returns (RemoveImageResponse) {} + // ImageFSInfo returns information of the filesystem that is used to store images. + rpc ImageFsInfo(ImageFsInfoRequest) returns (ImageFsInfoResponse) {} +} + +message VersionRequest { + // Version of the kubelet runtime API. + string version = 1; +} + +message VersionResponse { + // Version of the kubelet runtime API. + string version = 1; + // Name of the container runtime. + string runtime_name = 2; + // Version of the container runtime. The string must be + // semver-compatible. + string runtime_version = 3; + // API version of the container runtime. The string must be + // semver-compatible. + string runtime_api_version = 4; +} + +// DNSConfig specifies the DNS servers and search domains of a sandbox. +message DNSConfig { + // List of DNS servers of the cluster. + repeated string servers = 1; + // List of DNS search domains of the cluster. + repeated string searches = 2; + // List of DNS options. See https://linux.die.net/man/5/resolv.conf + // for all available options. + repeated string options = 3; +} + +enum Protocol { + TCP = 0; + UDP = 1; + SCTP = 2; +} + +// PortMapping specifies the port mapping configurations of a sandbox. +message PortMapping { + // Protocol of the port mapping. + Protocol protocol = 1; + // Port number within the container. Default: 0 (not specified). + int32 container_port = 2; + // Port number on the host. Default: 0 (not specified). + int32 host_port = 3; + // Host IP. + string host_ip = 4; +} + +enum MountPropagation { + // No mount propagation ("private" in Linux terminology). + PROPAGATION_PRIVATE = 0; + // Mounts get propagated from the host to the container ("rslave" in Linux). + PROPAGATION_HOST_TO_CONTAINER = 1; + // Mounts get propagated from the host to the container and from the + // container to the host ("rshared" in Linux). + PROPAGATION_BIDIRECTIONAL = 2; +} + +// Mount specifies a host volume to mount into a container. +message Mount { + // Path of the mount within the container. + string container_path = 1; + // Path of the mount on the host. If the hostPath doesn't exist, then runtimes + // should report error. If the hostpath is a symbolic link, runtimes should + // follow the symlink and mount the real destination to container. + string host_path = 2; + // If set, the mount is read-only. + bool readonly = 3; + // If set, the mount needs SELinux relabeling. + bool selinux_relabel = 4; + // Requested propagation mode. + MountPropagation propagation = 5; +} + +// A NamespaceMode describes the intended namespace configuration for each +// of the namespaces (Network, PID, IPC) in NamespaceOption. Runtimes should +// map these modes as appropriate for the technology underlying the runtime. +enum NamespaceMode { + // A POD namespace is common to all containers in a pod. + // For example, a container with a PID namespace of POD expects to view + // all of the processes in all of the containers in the pod. + POD = 0; + // A CONTAINER namespace is restricted to a single container. + // For example, a container with a PID namespace of CONTAINER expects to + // view only the processes in that container. + CONTAINER = 1; + // A NODE namespace is the namespace of the Kubernetes node. + // For example, a container with a PID namespace of NODE expects to view + // all of the processes on the host running the kubelet. + NODE = 2; +} + +// NamespaceOption provides options for Linux namespaces. +message NamespaceOption { + // Network namespace for this container/sandbox. + // Note: There is currently no way to set CONTAINER scoped network in the Kubernetes API. + // Namespaces currently set by the kubelet: POD, NODE + NamespaceMode network = 1; + // PID namespace for this container/sandbox. + // Note: The CRI default is POD, but the v1.PodSpec default is CONTAINER. + // The kubelet's runtime manager will set this to CONTAINER explicitly for v1 pods. + // Namespaces currently set by the kubelet: POD, CONTAINER, NODE + NamespaceMode pid = 2; + // IPC namespace for this container/sandbox. + // Note: There is currently no way to set CONTAINER scoped IPC in the Kubernetes API. + // Namespaces currently set by the kubelet: POD, NODE + NamespaceMode ipc = 3; +} + +// Int64Value is the wrapper of int64. +message Int64Value { + // The value. + int64 value = 1; +} + +// LinuxSandboxSecurityContext holds linux security configuration that will be +// applied to a sandbox. Note that: +// 1) It does not apply to containers in the pods. +// 2) It may not be applicable to a PodSandbox which does not contain any running +// process. +message LinuxSandboxSecurityContext { + // Configurations for the sandbox's namespaces. + // This will be used only if the PodSandbox uses namespace for isolation. + NamespaceOption namespace_options = 1; + // Optional SELinux context to be applied. + SELinuxOption selinux_options = 2; + // UID to run sandbox processes as, when applicable. + Int64Value run_as_user = 3; + // GID to run sandbox processes as, when applicable. run_as_group should only + // be specified when run_as_user is specified; otherwise, the runtime MUST error. + Int64Value run_as_group = 8; + // If set, the root filesystem of the sandbox is read-only. + bool readonly_rootfs = 4; + // List of groups applied to the first process run in the sandbox, in + // addition to the sandbox's primary GID. + repeated int64 supplemental_groups = 5; + // Indicates whether the sandbox will be asked to run a privileged + // container. If a privileged container is to be executed within it, this + // MUST be true. + // This allows a sandbox to take additional security precautions if no + // privileged containers are expected to be run. + bool privileged = 6; + // Seccomp profile for the sandbox, candidate values are: + // * runtime/default: the default profile for the container runtime + // * unconfined: unconfined profile, ie, no seccomp sandboxing + // * localhost/: the profile installed on the node. + // is the full path of the profile. + // Default: "", which is identical with unconfined. + string seccomp_profile_path = 7; +} + +// LinuxPodSandboxConfig holds platform-specific configurations for Linux +// host platforms and Linux-based containers. +message LinuxPodSandboxConfig { + // Parent cgroup of the PodSandbox. + // The cgroupfs style syntax will be used, but the container runtime can + // convert it to systemd semantics if needed. + string cgroup_parent = 1; + // LinuxSandboxSecurityContext holds sandbox security attributes. + LinuxSandboxSecurityContext security_context = 2; + // Sysctls holds linux sysctls config for the sandbox. + map sysctls = 3; +} + +// PodSandboxMetadata holds all necessary information for building the sandbox name. +// The container runtime is encouraged to expose the metadata associated with the +// PodSandbox in its user interface for better user experience. For example, +// the runtime can construct a unique PodSandboxName based on the metadata. +message PodSandboxMetadata { + // Pod name of the sandbox. Same as the pod name in the PodSpec. + string name = 1; + // Pod UID of the sandbox. Same as the pod UID in the PodSpec. + string uid = 2; + // Pod namespace of the sandbox. Same as the pod namespace in the PodSpec. + string namespace = 3; + // Attempt number of creating the sandbox. Default: 0. + uint32 attempt = 4; +} + +// PodSandboxConfig holds all the required and optional fields for creating a +// sandbox. +message PodSandboxConfig { + // Metadata of the sandbox. This information will uniquely identify the + // sandbox, and the runtime should leverage this to ensure correct + // operation. The runtime may also use this information to improve UX, such + // as by constructing a readable name. + PodSandboxMetadata metadata = 1; + // Hostname of the sandbox. + string hostname = 2; + // Path to the directory on the host in which container log files are + // stored. + // By default the log of a container going into the LogDirectory will be + // hooked up to STDOUT and STDERR. However, the LogDirectory may contain + // binary log files with structured logging data from the individual + // containers. For example, the files might be newline separated JSON + // structured logs, systemd-journald journal files, gRPC trace files, etc. + // E.g., + // PodSandboxConfig.LogDirectory = `/var/log/pods//` + // ContainerConfig.LogPath = `containerName/Instance#.log` + // + // WARNING: Log management and how kubelet should interface with the + // container logs are under active discussion in + // https://issues.k8s.io/24677. There *may* be future change of direction + // for logging as the discussion carries on. + string log_directory = 3; + // DNS config for the sandbox. + DNSConfig dns_config = 4; + // Port mappings for the sandbox. + repeated PortMapping port_mappings = 5; + // Key-value pairs that may be used to scope and select individual resources. + map labels = 6; + // Unstructured key-value map that may be set by the kubelet to store and + // retrieve arbitrary metadata. This will include any annotations set on a + // pod through the Kubernetes API. + // + // Annotations MUST NOT be altered by the runtime; the annotations stored + // here MUST be returned in the PodSandboxStatus associated with the pod + // this PodSandboxConfig creates. + // + // In general, in order to preserve a well-defined interface between the + // kubelet and the container runtime, annotations SHOULD NOT influence + // runtime behaviour. + // + // Annotations can also be useful for runtime authors to experiment with + // new features that are opaque to the Kubernetes APIs (both user-facing + // and the CRI). Whenever possible, however, runtime authors SHOULD + // consider proposing new typed fields for any new features instead. + map annotations = 7; + // Optional configurations specific to Linux hosts. + LinuxPodSandboxConfig linux = 8; +} + +message RunPodSandboxRequest { + // Configuration for creating a PodSandbox. + PodSandboxConfig config = 1; + // Named runtime configuration to use for this PodSandbox. + // If the runtime handler is unknown, this request should be rejected. An + // empty string should select the default handler, equivalent to the + // behavior before this feature was added. + // See https://git.k8s.io/community/keps/sig-node/0014-runtime-class.md + string runtime_handler = 2; +} + +message RunPodSandboxResponse { + // ID of the PodSandbox to run. + string pod_sandbox_id = 1; +} + +message StopPodSandboxRequest { + // ID of the PodSandbox to stop. + string pod_sandbox_id = 1; +} + +message StopPodSandboxResponse {} + +message RemovePodSandboxRequest { + // ID of the PodSandbox to remove. + string pod_sandbox_id = 1; +} + +message RemovePodSandboxResponse {} + +message PodSandboxStatusRequest { + // ID of the PodSandbox for which to retrieve status. + string pod_sandbox_id = 1; + // Verbose indicates whether to return extra information about the pod sandbox. + bool verbose = 2; +} + +// PodSandboxNetworkStatus is the status of the network for a PodSandbox. +message PodSandboxNetworkStatus { + // IP address of the PodSandbox. + string ip = 1; +} + +// Namespace contains paths to the namespaces. +message Namespace { + // Namespace options for Linux namespaces. + NamespaceOption options = 2; +} + +// LinuxSandboxStatus contains status specific to Linux sandboxes. +message LinuxPodSandboxStatus { + // Paths to the sandbox's namespaces. + Namespace namespaces = 1; +} + +enum PodSandboxState { + SANDBOX_READY = 0; + SANDBOX_NOTREADY = 1; +} + +// PodSandboxStatus contains the status of the PodSandbox. +message PodSandboxStatus { + // ID of the sandbox. + string id = 1; + // Metadata of the sandbox. + PodSandboxMetadata metadata = 2; + // State of the sandbox. + PodSandboxState state = 3; + // Creation timestamp of the sandbox in nanoseconds. Must be > 0. + int64 created_at = 4; + // Network contains network status if network is handled by the runtime. + PodSandboxNetworkStatus network = 5; + // Linux-specific status to a pod sandbox. + LinuxPodSandboxStatus linux = 6; + // Labels are key-value pairs that may be used to scope and select individual resources. + map labels = 7; + // Unstructured key-value map holding arbitrary metadata. + // Annotations MUST NOT be altered by the runtime; the value of this field + // MUST be identical to that of the corresponding PodSandboxConfig used to + // instantiate the pod sandbox this status represents. + map annotations = 8; +} + +message PodSandboxStatusResponse { + // Status of the PodSandbox. + PodSandboxStatus status = 1; + // Info is extra information of the PodSandbox. The key could be arbitrary string, and + // value should be in json format. The information could include anything useful for + // debug, e.g. network namespace for linux container based container runtime. + // It should only be returned non-empty when Verbose is true. + map info = 2; +} + +// PodSandboxStateValue is the wrapper of PodSandboxState. +message PodSandboxStateValue { + // State of the sandbox. + PodSandboxState state = 1; +} + +// PodSandboxFilter is used to filter a list of PodSandboxes. +// All those fields are combined with 'AND' +message PodSandboxFilter { + // ID of the sandbox. + string id = 1; + // State of the sandbox. + PodSandboxStateValue state = 2; + // LabelSelector to select matches. + // Only api.MatchLabels is supported for now and the requirements + // are ANDed. MatchExpressions is not supported yet. + map label_selector = 3; +} + +message ListPodSandboxRequest { + // PodSandboxFilter to filter a list of PodSandboxes. + PodSandboxFilter filter = 1; +} + + +// PodSandbox contains minimal information about a sandbox. +message PodSandbox { + // ID of the PodSandbox. + string id = 1; + // Metadata of the PodSandbox. + PodSandboxMetadata metadata = 2; + // State of the PodSandbox. + PodSandboxState state = 3; + // Creation timestamps of the PodSandbox in nanoseconds. Must be > 0. + int64 created_at = 4; + // Labels of the PodSandbox. + map labels = 5; + // Unstructured key-value map holding arbitrary metadata. + // Annotations MUST NOT be altered by the runtime; the value of this field + // MUST be identical to that of the corresponding PodSandboxConfig used to + // instantiate this PodSandbox. + map annotations = 6; +} + +message ListPodSandboxResponse { + // List of PodSandboxes. + repeated PodSandbox items = 1; +} + +// ImageSpec is an internal representation of an image. Currently, it wraps the +// value of a Container's Image field (e.g. imageID or imageDigest), but in the +// future it will include more detailed information about the different image types. +message ImageSpec { + string image = 1; +} + +message KeyValue { + string key = 1; + string value = 2; +} + +// LinuxContainerResources specifies Linux specific configuration for +// resources. +// TODO: Consider using Resources from opencontainers/runtime-spec/specs-go +// directly. +message LinuxContainerResources { + // CPU CFS (Completely Fair Scheduler) period. Default: 0 (not specified). + int64 cpu_period = 1; + // CPU CFS (Completely Fair Scheduler) quota. Default: 0 (not specified). + int64 cpu_quota = 2; + // CPU shares (relative weight vs. other containers). Default: 0 (not specified). + int64 cpu_shares = 3; + // Memory limit in bytes. Default: 0 (not specified). + int64 memory_limit_in_bytes = 4; + // OOMScoreAdj adjusts the oom-killer score. Default: 0 (not specified). + int64 oom_score_adj = 5; + // CpusetCpus constrains the allowed set of logical CPUs. Default: "" (not specified). + string cpuset_cpus = 6; + // CpusetMems constrains the allowed set of memory nodes. Default: "" (not specified). + string cpuset_mems = 7; +} + +// SELinuxOption are the labels to be applied to the container. +message SELinuxOption { + string user = 1; + string role = 2; + string type = 3; + string level = 4; +} + +// Capability contains the container capabilities to add or drop +message Capability { + // List of capabilities to add. + repeated string add_capabilities = 1; + // List of capabilities to drop. + repeated string drop_capabilities = 2; +} + +// LinuxContainerSecurityContext holds linux security configuration that will be applied to a container. +message LinuxContainerSecurityContext { + // Capabilities to add or drop. + Capability capabilities = 1; + // If set, run container in privileged mode. + // Privileged mode is incompatible with the following options. If + // privileged is set, the following features MAY have no effect: + // 1. capabilities + // 2. selinux_options + // 4. seccomp + // 5. apparmor + // + // Privileged mode implies the following specific options are applied: + // 1. All capabilities are added. + // 2. Sensitive paths, such as kernel module paths within sysfs, are not masked. + // 3. Any sysfs and procfs mounts are mounted RW. + // 4. Apparmor confinement is not applied. + // 5. Seccomp restrictions are not applied. + // 6. The device cgroup does not restrict access to any devices. + // 7. All devices from the host's /dev are available within the container. + // 8. SELinux restrictions are not applied (e.g. label=disabled). + bool privileged = 2; + // Configurations for the container's namespaces. + // Only used if the container uses namespace for isolation. + NamespaceOption namespace_options = 3; + // SELinux context to be optionally applied. + SELinuxOption selinux_options = 4; + // UID to run the container process as. Only one of run_as_user and + // run_as_username can be specified at a time. + Int64Value run_as_user = 5; + // GID to run the container process as. run_as_group should only be specified + // when run_as_user or run_as_username is specified; otherwise, the runtime + // MUST error. + Int64Value run_as_group = 12; + // User name to run the container process as. If specified, the user MUST + // exist in the container image (i.e. in the /etc/passwd inside the image), + // and be resolved there by the runtime; otherwise, the runtime MUST error. + string run_as_username = 6; + // If set, the root filesystem of the container is read-only. + bool readonly_rootfs = 7; + // List of groups applied to the first process run in the container, in + // addition to the container's primary GID. + repeated int64 supplemental_groups = 8; + // AppArmor profile for the container, candidate values are: + // * runtime/default: equivalent to not specifying a profile. + // * unconfined: no profiles are loaded + // * localhost/: profile loaded on the node + // (localhost) by name. The possible profile names are detailed at + // http://wiki.apparmor.net/index.php/AppArmor_Core_Policy_Reference + string apparmor_profile = 9; + // Seccomp profile for the container, candidate values are: + // * runtime/default: the default profile for the container runtime + // * unconfined: unconfined profile, ie, no seccomp sandboxing + // * localhost/: the profile installed on the node. + // is the full path of the profile. + // Default: "", which is identical with unconfined. + string seccomp_profile_path = 10; + // no_new_privs defines if the flag for no_new_privs should be set on the + // container. + bool no_new_privs = 11; + // masked_paths is a slice of paths that should be masked by the container + // runtime, this can be passed directly to the OCI spec. + repeated string masked_paths = 13; + // readonly_paths is a slice of paths that should be set as readonly by the + // container runtime, this can be passed directly to the OCI spec. + repeated string readonly_paths = 14; +} + +// LinuxContainerConfig contains platform-specific configuration for +// Linux-based containers. +message LinuxContainerConfig { + // Resources specification for the container. + LinuxContainerResources resources = 1; + // LinuxContainerSecurityContext configuration for the container. + LinuxContainerSecurityContext security_context = 2; +} + +// WindowsContainerSecurityContext holds windows security configuration that will be applied to a container. +message WindowsContainerSecurityContext { + // User name to run the container process as. If specified, the user MUST + // exist in the container image and be resolved there by the runtime; + // otherwise, the runtime MUST return error. + string run_as_username = 1; +} + +// WindowsContainerConfig contains platform-specific configuration for +// Windows-based containers. +message WindowsContainerConfig { + // Resources specification for the container. + WindowsContainerResources resources = 1; + // WindowsContainerSecurityContext configuration for the container. + WindowsContainerSecurityContext security_context = 2; +} + +// WindowsContainerResources specifies Windows specific configuration for +// resources. +message WindowsContainerResources { + // CPU shares (relative weight vs. other containers). Default: 0 (not specified). + int64 cpu_shares = 1; + // Number of CPUs available to the container. Default: 0 (not specified). + int64 cpu_count = 2; + // Specifies the portion of processor cycles that this container can use as a percentage times 100. + int64 cpu_maximum = 3; + // Memory limit in bytes. Default: 0 (not specified). + int64 memory_limit_in_bytes = 4; +} + +// ContainerMetadata holds all necessary information for building the container +// name. The container runtime is encouraged to expose the metadata in its user +// interface for better user experience. E.g., runtime can construct a unique +// container name based on the metadata. Note that (name, attempt) is unique +// within a sandbox for the entire lifetime of the sandbox. +message ContainerMetadata { + // Name of the container. Same as the container name in the PodSpec. + string name = 1; + // Attempt number of creating the container. Default: 0. + uint32 attempt = 2; +} + +// Device specifies a host device to mount into a container. +message Device { + // Path of the device within the container. + string container_path = 1; + // Path of the device on the host. + string host_path = 2; + // Cgroups permissions of the device, candidates are one or more of + // * r - allows container to read from the specified device. + // * w - allows container to write to the specified device. + // * m - allows container to create device files that do not yet exist. + string permissions = 3; +} + +// ContainerConfig holds all the required and optional fields for creating a +// container. +message ContainerConfig { + // Metadata of the container. This information will uniquely identify the + // container, and the runtime should leverage this to ensure correct + // operation. The runtime may also use this information to improve UX, such + // as by constructing a readable name. + ContainerMetadata metadata = 1 ; + // Image to use. + ImageSpec image = 2; + // Command to execute (i.e., entrypoint for docker) + repeated string command = 3; + // Args for the Command (i.e., command for docker) + repeated string args = 4; + // Current working directory of the command. + string working_dir = 5; + // List of environment variable to set in the container. + repeated KeyValue envs = 6; + // Mounts for the container. + repeated Mount mounts = 7; + // Devices for the container. + repeated Device devices = 8; + // Key-value pairs that may be used to scope and select individual resources. + // Label keys are of the form: + // label-key ::= prefixed-name | name + // prefixed-name ::= prefix '/' name + // prefix ::= DNS_SUBDOMAIN + // name ::= DNS_LABEL + map labels = 9; + // Unstructured key-value map that may be used by the kubelet to store and + // retrieve arbitrary metadata. + // + // Annotations MUST NOT be altered by the runtime; the annotations stored + // here MUST be returned in the ContainerStatus associated with the container + // this ContainerConfig creates. + // + // In general, in order to preserve a well-defined interface between the + // kubelet and the container runtime, annotations SHOULD NOT influence + // runtime behaviour. + map annotations = 10; + // Path relative to PodSandboxConfig.LogDirectory for container to store + // the log (STDOUT and STDERR) on the host. + // E.g., + // PodSandboxConfig.LogDirectory = `/var/log/pods//` + // ContainerConfig.LogPath = `containerName/Instance#.log` + // + // WARNING: Log management and how kubelet should interface with the + // container logs are under active discussion in + // https://issues.k8s.io/24677. There *may* be future change of direction + // for logging as the discussion carries on. + string log_path = 11; + + // Variables for interactive containers, these have very specialized + // use-cases (e.g. debugging). + // TODO: Determine if we need to continue supporting these fields that are + // part of Kubernetes's Container Spec. + bool stdin = 12; + bool stdin_once = 13; + bool tty = 14; + + // Configuration specific to Linux containers. + LinuxContainerConfig linux = 15; + // Configuration specific to Windows containers. + WindowsContainerConfig windows = 16; +} + +message CreateContainerRequest { + // ID of the PodSandbox in which the container should be created. + string pod_sandbox_id = 1; + // Config of the container. + ContainerConfig config = 2; + // Config of the PodSandbox. This is the same config that was passed + // to RunPodSandboxRequest to create the PodSandbox. It is passed again + // here just for easy reference. The PodSandboxConfig is immutable and + // remains the same throughout the lifetime of the pod. + PodSandboxConfig sandbox_config = 3; +} + +message CreateContainerResponse { + // ID of the created container. + string container_id = 1; +} + +message StartContainerRequest { + // ID of the container to start. + string container_id = 1; +} + +message StartContainerResponse {} + +message StopContainerRequest { + // ID of the container to stop. + string container_id = 1; + // Timeout in seconds to wait for the container to stop before forcibly + // terminating it. Default: 0 (forcibly terminate the container immediately) + int64 timeout = 2; +} + +message StopContainerResponse {} + +message RemoveContainerRequest { + // ID of the container to remove. + string container_id = 1; +} + +message RemoveContainerResponse {} + +enum ContainerState { + CONTAINER_CREATED = 0; + CONTAINER_RUNNING = 1; + CONTAINER_EXITED = 2; + CONTAINER_UNKNOWN = 3; +} + +// ContainerStateValue is the wrapper of ContainerState. +message ContainerStateValue { + // State of the container. + ContainerState state = 1; +} + +// ContainerFilter is used to filter containers. +// All those fields are combined with 'AND' +message ContainerFilter { + // ID of the container. + string id = 1; + // State of the container. + ContainerStateValue state = 2; + // ID of the PodSandbox. + string pod_sandbox_id = 3; + // LabelSelector to select matches. + // Only api.MatchLabels is supported for now and the requirements + // are ANDed. MatchExpressions is not supported yet. + map label_selector = 4; +} + +message ListContainersRequest { + ContainerFilter filter = 1; +} + +// Container provides the runtime information for a container, such as ID, hash, +// state of the container. +message Container { + // ID of the container, used by the container runtime to identify + // a container. + string id = 1; + // ID of the sandbox to which this container belongs. + string pod_sandbox_id = 2; + // Metadata of the container. + ContainerMetadata metadata = 3; + // Spec of the image. + ImageSpec image = 4; + // Reference to the image in use. For most runtimes, this should be an + // image ID. + string image_ref = 5; + // State of the container. + ContainerState state = 6; + // Creation time of the container in nanoseconds. + int64 created_at = 7; + // Key-value pairs that may be used to scope and select individual resources. + map labels = 8; + // Unstructured key-value map holding arbitrary metadata. + // Annotations MUST NOT be altered by the runtime; the value of this field + // MUST be identical to that of the corresponding ContainerConfig used to + // instantiate this Container. + map annotations = 9; +} + +message ListContainersResponse { + // List of containers. + repeated Container containers = 1; +} + +message ContainerStatusRequest { + // ID of the container for which to retrieve status. + string container_id = 1; + // Verbose indicates whether to return extra information about the container. + bool verbose = 2; +} + +// ContainerStatus represents the status of a container. +message ContainerStatus { + // ID of the container. + string id = 1; + // Metadata of the container. + ContainerMetadata metadata = 2; + // Status of the container. + ContainerState state = 3; + // Creation time of the container in nanoseconds. + int64 created_at = 4; + // Start time of the container in nanoseconds. Default: 0 (not specified). + int64 started_at = 5; + // Finish time of the container in nanoseconds. Default: 0 (not specified). + int64 finished_at = 6; + // Exit code of the container. Only required when finished_at != 0. Default: 0. + int32 exit_code = 7; + // Spec of the image. + ImageSpec image = 8; + // Reference to the image in use. For most runtimes, this should be an + // image ID + string image_ref = 9; + // Brief CamelCase string explaining why container is in its current state. + string reason = 10; + // Human-readable message indicating details about why container is in its + // current state. + string message = 11; + // Key-value pairs that may be used to scope and select individual resources. + map labels = 12; + // Unstructured key-value map holding arbitrary metadata. + // Annotations MUST NOT be altered by the runtime; the value of this field + // MUST be identical to that of the corresponding ContainerConfig used to + // instantiate the Container this status represents. + map annotations = 13; + // Mounts for the container. + repeated Mount mounts = 14; + // Log path of container. + string log_path = 15; +} + +message ContainerStatusResponse { + // Status of the container. + ContainerStatus status = 1; + // Info is extra information of the Container. The key could be arbitrary string, and + // value should be in json format. The information could include anything useful for + // debug, e.g. pid for linux container based container runtime. + // It should only be returned non-empty when Verbose is true. + map info = 2; +} + +message UpdateContainerResourcesRequest { + // ID of the container to update. + string container_id = 1; + // Resource configuration specific to Linux containers. + LinuxContainerResources linux = 2; +} + +message UpdateContainerResourcesResponse {} + +message ExecSyncRequest { + // ID of the container. + string container_id = 1; + // Command to execute. + repeated string cmd = 2; + // Timeout in seconds to stop the command. Default: 0 (run forever). + int64 timeout = 3; +} + +message ExecSyncResponse { + // Captured command stdout output. + bytes stdout = 1; + // Captured command stderr output. + bytes stderr = 2; + // Exit code the command finished with. Default: 0 (success). + int32 exit_code = 3; +} + +message ExecRequest { + // ID of the container in which to execute the command. + string container_id = 1; + // Command to execute. + repeated string cmd = 2; + // Whether to exec the command in a TTY. + bool tty = 3; + // Whether to stream stdin. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + bool stdin = 4; + // Whether to stream stdout. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + bool stdout = 5; + // Whether to stream stderr. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + // If `tty` is true, `stderr` MUST be false. Multiplexing is not supported + // in this case. The output of stdout and stderr will be combined to a + // single stream. + bool stderr = 6; +} + +message ExecResponse { + // Fully qualified URL of the exec streaming server. + string url = 1; +} + +message AttachRequest { + // ID of the container to which to attach. + string container_id = 1; + // Whether to stream stdin. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + bool stdin = 2; + // Whether the process being attached is running in a TTY. + // This must match the TTY setting in the ContainerConfig. + bool tty = 3; + // Whether to stream stdout. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + bool stdout = 4; + // Whether to stream stderr. + // One of `stdin`, `stdout`, and `stderr` MUST be true. + // If `tty` is true, `stderr` MUST be false. Multiplexing is not supported + // in this case. The output of stdout and stderr will be combined to a + // single stream. + bool stderr = 5; +} + +message AttachResponse { + // Fully qualified URL of the attach streaming server. + string url = 1; +} + +message PortForwardRequest { + // ID of the container to which to forward the port. + string pod_sandbox_id = 1; + // Port to forward. + repeated int32 port = 2; +} + +message PortForwardResponse { + // Fully qualified URL of the port-forward streaming server. + string url = 1; +} + +message ImageFilter { + // Spec of the image. + ImageSpec image = 1; +} + +message ListImagesRequest { + // Filter to list images. + ImageFilter filter = 1; +} + +// Basic information about a container image. +message Image { + // ID of the image. + string id = 1; + // Other names by which this image is known. + repeated string repo_tags = 2; + // Digests by which this image is known. + repeated string repo_digests = 3; + // Size of the image in bytes. Must be > 0. + uint64 size = 4; + // UID that will run the command(s). This is used as a default if no user is + // specified when creating the container. UID and the following user name + // are mutually exclusive. + Int64Value uid = 5; + // User name that will run the command(s). This is used if UID is not set + // and no user is specified when creating container. + string username = 6; +} + +message ListImagesResponse { + // List of images. + repeated Image images = 1; +} + +message ImageStatusRequest { + // Spec of the image. + ImageSpec image = 1; + // Verbose indicates whether to return extra information about the image. + bool verbose = 2; +} + +message ImageStatusResponse { + // Status of the image. + Image image = 1; + // Info is extra information of the Image. The key could be arbitrary string, and + // value should be in json format. The information could include anything useful + // for debug, e.g. image config for oci image based container runtime. + // It should only be returned non-empty when Verbose is true. + map info = 2; +} + +// AuthConfig contains authorization information for connecting to a registry. +message AuthConfig { + string username = 1; + string password = 2; + string auth = 3; + string server_address = 4; + // IdentityToken is used to authenticate the user and get + // an access token for the registry. + string identity_token = 5; + // RegistryToken is a bearer token to be sent to a registry + string registry_token = 6; +} + +message PullImageRequest { + // Spec of the image. + ImageSpec image = 1; + // Authentication configuration for pulling the image. + AuthConfig auth = 2; + // Config of the PodSandbox, which is used to pull image in PodSandbox context. + PodSandboxConfig sandbox_config = 3; +} + +message PullImageResponse { + // Reference to the image in use. For most runtimes, this should be an + // image ID or digest. + string image_ref = 1; +} + +message RemoveImageRequest { + // Spec of the image to remove. + ImageSpec image = 1; +} + +message RemoveImageResponse {} + +message NetworkConfig { + // CIDR to use for pod IP addresses. If the CIDR is empty, runtimes + // should omit it. + string pod_cidr = 1; +} + +message RuntimeConfig { + NetworkConfig network_config = 1; +} + +message UpdateRuntimeConfigRequest { + RuntimeConfig runtime_config = 1; +} + +message UpdateRuntimeConfigResponse {} + +// RuntimeCondition contains condition information for the runtime. +// There are 2 kinds of runtime conditions: +// 1. Required conditions: Conditions are required for kubelet to work +// properly. If any required condition is unmet, the node will be not ready. +// The required conditions include: +// * RuntimeReady: RuntimeReady means the runtime is up and ready to accept +// basic containers e.g. container only needs host network. +// * NetworkReady: NetworkReady means the runtime network is up and ready to +// accept containers which require container network. +// 2. Optional conditions: Conditions are informative to the user, but kubelet +// will not rely on. Since condition type is an arbitrary string, all conditions +// not required are optional. These conditions will be exposed to users to help +// them understand the status of the system. +message RuntimeCondition { + // Type of runtime condition. + string type = 1; + // Status of the condition, one of true/false. Default: false. + bool status = 2; + // Brief CamelCase string containing reason for the condition's last transition. + string reason = 3; + // Human-readable message indicating details about last transition. + string message = 4; +} + +// RuntimeStatus is information about the current status of the runtime. +message RuntimeStatus { + // List of current observed runtime conditions. + repeated RuntimeCondition conditions = 1; +} + +message StatusRequest { + // Verbose indicates whether to return extra information about the runtime. + bool verbose = 1; +} + +message StatusResponse { + // Status of the Runtime. + RuntimeStatus status = 1; + // Info is extra information of the Runtime. The key could be arbitrary string, and + // value should be in json format. The information could include anything useful for + // debug, e.g. plugins used by the container runtime. + // It should only be returned non-empty when Verbose is true. + map info = 2; +} + +message ImageFsInfoRequest {} + +// UInt64Value is the wrapper of uint64. +message UInt64Value { + // The value. + uint64 value = 1; +} + +// FilesystemIdentifier uniquely identify the filesystem. +message FilesystemIdentifier{ + // Mountpoint of a filesystem. + string mountpoint = 1; +} + +// FilesystemUsage provides the filesystem usage information. +message FilesystemUsage { + // Timestamp in nanoseconds at which the information were collected. Must be > 0. + int64 timestamp = 1; + // The unique identifier of the filesystem. + FilesystemIdentifier fs_id = 2; + // UsedBytes represents the bytes used for images on the filesystem. + // This may differ from the total bytes used on the filesystem and may not + // equal CapacityBytes - AvailableBytes. + UInt64Value used_bytes = 3; + // InodesUsed represents the inodes used by the images. + // This may not equal InodesCapacity - InodesAvailable because the underlying + // filesystem may also be used for purposes other than storing images. + UInt64Value inodes_used = 4; +} + +message ImageFsInfoResponse { + // Information of image filesystem(s). + repeated FilesystemUsage image_filesystems = 1; +} + +message ContainerStatsRequest{ + // ID of the container for which to retrieve stats. + string container_id = 1; +} + +message ContainerStatsResponse { + // Stats of the container. + ContainerStats stats = 1; +} + +message ListContainerStatsRequest{ + // Filter for the list request. + ContainerStatsFilter filter = 1; +} + +// ContainerStatsFilter is used to filter containers. +// All those fields are combined with 'AND' +message ContainerStatsFilter { + // ID of the container. + string id = 1; + // ID of the PodSandbox. + string pod_sandbox_id = 2; + // LabelSelector to select matches. + // Only api.MatchLabels is supported for now and the requirements + // are ANDed. MatchExpressions is not supported yet. + map label_selector = 3; +} + +message ListContainerStatsResponse { + // Stats of the container. + repeated ContainerStats stats = 1; +} + +// ContainerAttributes provides basic information of the container. +message ContainerAttributes { + // ID of the container. + string id = 1; + // Metadata of the container. + ContainerMetadata metadata = 2; + // Key-value pairs that may be used to scope and select individual resources. + map labels = 3; + // Unstructured key-value map holding arbitrary metadata. + // Annotations MUST NOT be altered by the runtime; the value of this field + // MUST be identical to that of the corresponding ContainerConfig used to + // instantiate the Container this status represents. + map annotations = 4; +} + +// ContainerStats provides the resource usage statistics for a container. +message ContainerStats { + // Information of the container. + ContainerAttributes attributes = 1; + // CPU usage gathered from the container. + CpuUsage cpu = 2; + // Memory usage gathered from the container. + MemoryUsage memory = 3; + // Usage of the writeable layer. + FilesystemUsage writable_layer = 4; +} + +// CpuUsage provides the CPU usage information. +message CpuUsage { + // Timestamp in nanoseconds at which the information were collected. Must be > 0. + int64 timestamp = 1; + // Cumulative CPU usage (sum across all cores) since object creation. + UInt64Value usage_core_nano_seconds = 2; +} + +// MemoryUsage provides the memory usage information. +message MemoryUsage { + // Timestamp in nanoseconds at which the information were collected. Must be > 0. + int64 timestamp = 1; + // The amount of working set memory in bytes. + UInt64Value working_set_bytes = 2; +} + +message ReopenContainerLogRequest { + // ID of the container for which to reopen the log. + string container_id = 1; +} + +message ReopenContainerLogResponse{ +} diff --git a/userspace/libsinsp/ctext.cpp b/userspace/libsinsp/ctext.cpp new file mode 100644 index 0000000000..cf1715ec2a --- /dev/null +++ b/userspace/libsinsp/ctext.cpp @@ -0,0 +1,1374 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#ifndef _WIN32 + +#include "ctext.h" +#include +#include +#include +#include + +using namespace std; + +#define CTEXT_UNDER_X 0x01 +#define CTEXT_OVER_X 0x02 +#define CTEXT_UNDER_Y 0x04 +#define CTEXT_OVER_Y 0x08 + +#define CTEXT_OVER (CTEXT_OVER_Y | CTEXT_OVER_X) +#define CTEXT_UNDER (CTEXT_UNDER_Y | CTEXT_UNDER_X) + +ctext_config config_default; + +void search_copy(ctext_search *dst, ctext_search *src) +{ + // Because c++ makes life impossibly difficult. + memcpy(dst, src, sizeof(ctext_search) - sizeof(string)); + dst->_query = src->_query; +} + +ctext::ctext(WINDOW *win, ctext_config *config) +{ + this->m_win = win; + + config_default.m_buffer_size = CTEXT_DEFAULT_BUFFER_SIZE; + config_default.m_bounding_box = CTEXT_DEFAULT_BOUNDING_BOX; + config_default.m_do_wrap = CTEXT_DEFAULT_DO_WRAP; + config_default.m_append_top = CTEXT_DEFAULT_APPEND_TOP; + config_default.m_scroll_on_append = CTEXT_DEFAULT_SCROLL_ON_APPEND; + config_default.m_auto_newline = CTEXT_DEFAULT_AUTO_NEWLINE; + + /* + this->m_debug = new ofstream(); + this->m_debug->open("debug1.txt"); + */ + + this->m_do_draw = true; + + if(config) + { + memcpy(&this->m_config, config, sizeof(ctext_config)); + } + else + { + memcpy(&this->m_config, &config_default, sizeof(ctext_config)); + } + + this->m_pos_start.x = this->m_pos_start.y = 0; + + this->m_attr_mask = 0; + this->m_last_search = 0; + this->m_event_counter = 0; + + this->m_max_y = 0; + + // initialized the buffer with the empty row + this->add_row(); +} + +int8_t ctext::search_off() +{ + int8_t ret = (this->m_last_search != 0); + this->m_last_search = 0; + return ret; +} + +int8_t ctext::set_config(ctext_config *config) +{ + memcpy(&this->m_config, config, sizeof(ctext_config)); + return this->redraw(); +} + +int8_t ctext::get_config(ctext_config *config) +{ + return !memcpy(config, &this->m_config, sizeof(ctext_config)); +} + +int8_t ctext::attach_curses_window(WINDOW *win) +{ + this->m_win = win; + return this->redraw(); +} + +int8_t ctext::highlight(ctext_search *context, int32_t mask) +{ + this->m_attr_mask |= mask; + this->redraw_partial(&context->pos, context->_query.size()); + this->m_attr_mask &= ~mask; + + return 0; +} + +int8_t ctext::set_query(ctext_search *p_search, string new_query) +{ + this->get_offset(&p_search->pos); + this->get_offset(&p_search->_start_pos); + + p_search->_query = new_query; + p_search->_last_match.y = -1; + p_search->_last_event = this->m_event_counter; + p_search->_match_count = 0; + + return 0; +} + +ctext_search *ctext::new_search(ctext_search *you_manage_this_memory, string to_search, bool is_case_insensitive, bool is_forward, bool do_wrap) +{ + ctext_search *p_search = you_manage_this_memory; + + if(!p_search) + { + return NULL; + } + + p_search->is_case_insensitive = is_case_insensitive; + p_search->do_wrap = do_wrap; + p_search->is_forward = is_forward; + + this->set_query(p_search, to_search); + + return p_search; +} + +int8_t ctext::highlight_matches(ctext_search *to_search) +{ + int8_t search_ret; + int32_t mask = A_BOLD | A_UNDERLINE; + + if(!to_search) + { + to_search = this->m_last_search; + + if(!to_search) + { + return 0; + } + } + + // We move the viewport pointer through the current viewport, + // highlighting all matching instances. + ctext_search in_viewport; + ctext_pos limit; + + search_copy(&in_viewport, to_search); + + // We will say the limit is the viewport height ... this makes sure we go over + // the maximum extent possible. We also make sure we do this after our first match + // otherwise this would reflect our current viewport, shameful! + limit.y = min(this->m_pos_start.y + this->m_win_height, (int32_t)this->m_buffer.size()); + + // Now we iterate through the viewport highlighting all of the instances, using the + // limit and the in_viewport pointer + if(this->m_event_counter != in_viewport._last_event) + { + search_ret = this->str_search_single(&in_viewport, &in_viewport, &limit); + } + + do + { + this->highlight(&in_viewport, mask); + search_ret = this->str_search_single(&in_viewport, &in_viewport, &limit); + mask = A_REVERSE; + } while(search_ret >= 0); + + return 0; +} + +int8_t ctext::str_search(ctext_search *to_search) +{ + int8_t search_ret, scroll_ret; + + this->m_last_search = to_search; + + // This makes sure that we scroll to a new y row + // if multiple matches are on the same viewport row. + for(;;) + { + search_ret = this->str_search_single(to_search, to_search); + if(search_ret == -1) + { + break; + } + + if(this->m_config.m_do_wrap) + { + scroll_ret = this->direct_scroll(&to_search->pos); + } + else + { + scroll_ret = this->direct_scroll(0, to_search->pos.y); + } + + // This makes sure we move forward ... but we only do this + // if we didn't push the event forward + if(!scroll_ret || to_search->_match_count == 1) + { + break; + } + } + + // This means that it was found somewhere and our + // pointer has been moved forward + if(search_ret >= 0) + { + // We can do a general scroll_to and redraw. + this->redraw(); + } + + return search_ret; +} + +int8_t ctext::str_search_single(ctext_search *to_search_in, ctext_search *new_pos_out, ctext_pos *limit) +{ + int32_t size = (int32_t)this->m_buffer.size(); + size_t found; + string haystack; + ctext_search res, *out; + + if(!to_search_in) + { + return -1; + } + + string query = to_search_in->_query; + + if(!new_pos_out) + { + search_copy(&res, to_search_in); + out = &res; + } + else + { + out = new_pos_out; + } + + // If a (scroll) event has happened since we last ran this, + // then we need to update exactly where we want to start + // the search from. + if(to_search_in->_last_event < this->m_event_counter) + { + out->pos.y = this->m_pos_start.y; + out->pos.x = this->m_pos_start.x; + out->_last_event = this->m_event_counter; + out->_match_count = 0; + } + + if(to_search_in->is_case_insensitive) + { + transform(query.begin(), query.end(), query.begin(), ::tolower); + } + + for(;;) + { + haystack = this->m_buffer[out->pos.y].data; + if(to_search_in->is_case_insensitive) + { + transform(haystack.begin(), haystack.end(), haystack.begin(), ::tolower); + } + + if(out->is_forward) + { + found = haystack.find(query, (size_t)( (out->pos.x == -2) ? out->pos.x + 2 : out->pos.x + 1)); + } + else + { + found = haystack.rfind(query, (size_t)( (out->pos.x == (int32_t)haystack.size()) ? out->pos.x : out->pos.x - 1)); + } + + if(found == string::npos) + { + if(out->is_forward) + { + out->pos.y = (out->pos.y + 1) % size; + out->pos.x = -2; + } + else + { + out->pos.y--; + + // Wrap if we are going backwards. + if(out->pos.y == -1) + { + out->pos.y = size - 1; + } + out->pos.x = (int32_t)this->m_buffer[out->pos.y].data.size(); + } + + // + // The edge case here is if there are no matches and we ARE wrapping, + // we don't want the idiot case of going through the haystack endlessly + // like a chump and locking up the application. + // + if( + (out->pos.y == out->_start_pos.y && (out->do_wrap == false || out->_last_match.y == -1)) || + (limit && out->pos.y > limit->y) + ) + { + return -1; + } + } + else + { + // This is all we really care about, we don't need + // to look at the x value + out->_last_match.y = out->pos.y; + out->pos.x = (int32_t)found; + out->_match_count++; + break; + } + } + + return 0; +} + +int32_t ctext::clear(int32_t row_count) +{ + int32_t ret = 0; + + if(row_count == -1) + { + ret = this->m_buffer.size(); + this->m_buffer.clear(); + this->add_row(); + } + else if(this->m_buffer.size()) + { + ret = this->m_buffer.size(); + this->m_buffer.erase(this->m_buffer.begin(), this->m_buffer.begin() + row_count); + ret -= this->m_buffer.size(); + } + + // We do the same logic when removing content + // .. perhaps forcing things down or upward + if(this->m_config.m_scroll_on_append) + { + this->get_win_size(); + + // Now we force it. + this->direct_scroll(0, this->m_buffer.size() - this->m_win_height); + } + + this->redraw(); + return ret; +} + +int8_t ctext::ob_start() +{ + int8_t ret = this->m_do_draw; + this->m_do_draw = false; + return ret; +} + +int8_t ctext::ob_end() +{ + int8_t ret = !this->m_do_draw; + this->m_do_draw = true; + this->redraw(); + return ret; +} + +int8_t ctext::direct_scroll(ctext_pos*p) +{ + return this->direct_scroll(p->x, p->y); +} + +int8_t ctext::direct_scroll(int32_t x, int32_t y) +{ + ctext_pos start; + memcpy(&start, &this->m_pos_start, sizeof(ctext_pos)); + + this->get_win_size(); + + if(this->m_config.m_bounding_box) + { + y = min(y, this->m_max_y - this->m_win_height); + x = max(0, x); + y = max(0, y); + } + + // Under this context we should only be x-scrolling + // to a modulus of a windows width + if(this->m_config.m_do_wrap) + { + // We always go *under* to make sure that the + // content appears in the viewport. + x -= x % this->m_win_width; + } + + this->m_pos_start.x = x; + this->m_pos_start.y = y; + + // If the values have changed and we have actively scrolled, + // return 0, otherwise return -1. + return (start.x != x || start.y != y) ? 0 : -1; +} + +int8_t ctext::scroll_to(ctext_pos *pos) +{ + return this->scroll_to(pos->x, pos->y); +} + +int8_t ctext::scroll_to(int32_t x, int32_t y) +{ + this->direct_scroll(x, y); + this->m_event_counter++; + return this->redraw(); +} + +int8_t ctext::get_offset(ctext_pos *pos) +{ + return this->get_offset(&pos->x, &pos->y); +} + +int8_t ctext::get_offset(int32_t*x, int32_t*y) +{ + *x = this->m_pos_start.x; + *y = this->m_pos_start.y; + + return 0; +} + +int8_t ctext::get_offset_percent(float*percent) +{ + this->get_win_size(); + *percent = (float)(this->m_pos_start.y) / (this->m_max_y - this->m_win_height); + + return 0; +} + +int8_t ctext::get_buf_size(int32_t*buf_size) +{ + *buf_size = this->m_max_y; + + return 0; +} + +int32_t ctext::available_rows() +{ + // Since our buffer clearing scheme permits us to overflow, + // we have to bind this to make sure that we return >= 0 values + if(this->m_config.m_buffer_size == -1) + { + return (int32_t)LONG_MAX; + } + return max(this->m_config.m_buffer_size - this->m_max_y - 1, 0); +} + +int32_t ctext::up(int32_t amount) +{ + return this->down(-amount); +} + +int32_t ctext::page_down(int32_t page_count) +{ + this->get_win_size(); + return this->down(page_count * this->m_win_height); +} + +int32_t ctext::page_up(int32_t page_count) +{ + this->get_win_size(); + return this->down(-page_count * this->m_win_height); +} + +// Let's do this real fast. +int8_t ctext::map_to_win(int32_t buffer_x, int32_t buffer_y, ctext_pos*win) +{ + int8_t ret = 0; + + // This is the trivial case. + if(!this->m_config.m_do_wrap) + { + // These are trivial. + win->y = buffer_y - this->m_pos_start.y; + win->x = buffer_x - this->m_pos_start.x; + + return ((buffer_x < this->m_pos_start.x)) | ((buffer_x > this->m_pos_start.x + this->m_win_width) << 1) + | ((buffer_y < this->m_pos_start.y) << 2) | ((buffer_y > this->m_pos_start.y + this->m_win_height) << 3); + } + // Otherwise it's much more challenging. + else + { + // If we are below the fold or we are at the first line and before + // the start of where we ought to be drawing + if(buffer_y < this->m_pos_start.y || (buffer_y == this->m_pos_start.y && buffer_x < this->m_pos_start.x)) + { + // We omit win calculations here since they + // would be more expensive then we'd like + win->x = win->y = -1; + + ret |= CTEXT_UNDER_Y; + } + else + { + // To see if it's an overflow y is a bit harder + int32_t new_y = this->m_pos_start.y; + + int32_t new_offset = this->m_pos_start.x; + + string *data = &this->m_buffer[new_y].data; + + for(win->y = 0; win->y < this->m_win_height; win->y++) + { + new_offset += this->m_win_width; + + // + // There's an edge case that requires this + // twice due to a short circuit exit that + // would be triggered at the end of a buffer, + // see below. + // + if(buffer_y == new_y && buffer_x < new_offset) + { + win->x = buffer_x - (new_offset - this->m_win_width); + return ret; + } + + if(new_offset > (int32_t)data->size()) + { + new_offset = 0; + new_y ++; + if(new_y > this->m_max_y) + { + break; + } + data = &this->m_buffer[new_y].data; + } + + if( + // We've passed it and we know it's not + // an underflow, so we're done. + (buffer_y < new_y) || + + // We are at the end line and our test point + // is below the offset that we just flew past + (buffer_y == new_y && buffer_x < new_offset) + ) + { + win->x = buffer_x - (new_offset - this->m_win_width); + return ret; + } + } + + // Keep the y at the end + // win->y = -1; + + // If we get here that means that we went all the way through + // a "generation" and didn't reach our hit point ... that means + // it's an overflow. + ret |= CTEXT_OVER_Y; + } + } + + return ret; +} + +int8_t ctext::y_scroll_calculate(int32_t amount, ctext_pos *pos) +{ + if(this->m_config.m_do_wrap) + { + int32_t new_y = this->m_pos_start.y; + int32_t new_offset = this->m_pos_start.x; + ctext_row *p_row = &this->m_buffer[this->m_pos_start.y]; + + this->get_win_size(); + + while(amount > 0) + { + new_offset += this->m_win_width; + amount --; + if(new_offset > (int32_t)p_row->data.size()) + { + if((new_y + this->m_win_height + 1) >= (int32_t)this->m_buffer.size()) + { + // + // This means that forwarding our buffer was a mistake + // so we undo our work and get out of here. + // + new_offset -= this->m_win_width; + break; + } + new_offset = 0; + new_y++; + p_row = &this->m_buffer[new_y]; + } + } + + while(amount < 0) + { + new_offset -= this->m_win_width; + amount ++; + if(new_offset < 0) + { + if(new_y - 1 < 0) + { + break; + } + new_y--; + p_row = &this->m_buffer[new_y]; + new_offset = p_row->data.size() - p_row->data.size() % this->m_win_width; + } + } + pos->x = new_offset; + pos->y = new_y; + } + else + { + pos->x = this->m_pos_start.x; + pos->y = this->m_pos_start.y + amount; + } + + return 0; +} + +int32_t ctext::down(int32_t amount) +{ + ctext_pos new_pos; + this->y_scroll_calculate(amount, &new_pos); + return this->scroll_to(&new_pos); +} + +int32_t ctext::jump_to_first_line() +{ + int32_t current_line = this->m_pos_start.y; + + // + // Now we try to scroll above the first + // line. the bounding box rule will + // take care of the differences for us. + // + this->scroll_to(this->m_pos_start.x, 0 - this->m_win_height + 1); + + return current_line - this->m_pos_start.y; +} + +int32_t ctext::jump_to_last_line() +{ + int32_t current_line = this->m_pos_start.y; + + this->get_win_size(); + this->scroll_to(this->m_pos_start.x, this->m_max_y - 1); + return current_line - this->m_pos_start.y; +} + +int32_t ctext::left(int32_t amount) +{ + return this->right(-amount); +} + +int32_t ctext::right(int32_t amount) +{ + return this->scroll_to(this->m_pos_start.x + amount, this->m_pos_start.y); +} + +void ctext::get_win_size() +{ + int32_t width = 0, height = 0; + + if(this->m_win) + { + getmaxyx(this->m_win, height, width); + } + this->m_win_width = width; + this->m_win_height = height; +} + +int8_t ctext::rebuf() +{ + // Memory management is expensive, so we only do this occasionally + if(this->m_config.m_buffer_size != -1 && (int32_t)this->m_buffer.size() > (this->m_config.m_buffer_size * 11 / 10)) + { + this->m_buffer.erase(this->m_buffer.begin(), this->m_buffer.end() - this->m_config.m_buffer_size); + } + + this->m_max_y = this->m_buffer.size() - 1; + + // + // Since we've changed the bounding box of the content we have to + // issue a rescroll on exactly our previous parameters. This may + // force us inward or may retain our position. + // + return this->direct_scroll(this->m_pos_start.x, this->m_pos_start.y); +} + +void ctext::add_format_if_needed() +{ + attr_t attrs; + int16_t color_pair; + + if(!this->m_win) + { + return; + } + + if(this->m_buffer.empty()) + { + return; + } + + // Get the most current row. + ctext_row *p_row = &this->m_buffer.back(); + + ctext_format p_format = {0,0,0}; + if(!p_row->format.empty()) + { + // And the most current format + p_format = p_row->format.back(); + } + + wattr_get(this->m_win, &attrs, &color_pair, 0); + + if(attrs != p_format.attrs || color_pair != p_format.color_pair) + { + // Our properties have changed so we need to record this. + ctext_format new_format; + + // This is our offset + new_format.offset = (int32_t)p_row->data.size(); + new_format.attrs = attrs; + new_format.color_pair = color_pair; + + // + // If the new thing we are adding has the same + // offset as the previous, then we dump the + // previous. + // + if(p_format.offset == new_format.offset && !p_row->format.empty()) + { + p_row->format.pop_back(); + } + p_row->format.push_back(new_format); + } +} + +ctext_row* ctext::add_row() +{ + ctext_row row; + + // If there is an existing line, then + // we carry over the format from the + // last line.. + if(!this->m_buffer.empty()) + { + ctext_row p_row = this->m_buffer.back(); + + if(!p_row.format.empty()) + { + ctext_format p_format( p_row.format.back() ); + + // Set the offset to the initial. + p_format.offset = 0; + row.format.push_back(p_format); + } + } + + this->m_buffer.push_back(row); + + return &this->m_buffer.back(); +} + +char* next_type(char* search, const char delim) +{ + while(*search && *search != delim) + { + search ++; + } + return search; +} + +int8_t ctext::vprintf(const char*format, va_list ap) +{ + char *p_line, *n_line; + char large_buffer[CTEXT_BUFFER_SIZE]; + + this->add_format_if_needed(); + ctext_row *p_row = &this->m_buffer.back(); + + vsnprintf(large_buffer, CTEXT_BUFFER_SIZE, format, ap); + + p_line = large_buffer; + do + { + n_line = next_type(p_line, '\n'); + + string wstr(p_line, n_line - p_line); + p_row->data += wstr; + + if(*n_line) + { + p_row = this->add_row(); + } + p_line = n_line + 1; + } + while (*n_line); + + if(this->m_config.m_auto_newline) + { + this->add_row(); + } + + // Since we are adding content we need to see if we are + // to force on scroll. + if(this->m_config.m_scroll_on_append) + { + this->get_win_size(); + + // Now we force it. + this->direct_scroll(0, this->m_buffer.size() - this->m_win_height); + } + + return this->redraw(); +} + +int cprintf(ctext*win, const char *format, ...) +{ + int ret; + va_list args; + va_start(args, format); + ret = win->vprintf(format, args); + va_end(args); + return ret; +} + +int8_t ctext::printf(const char*format, ...) +{ + int8_t ret; + + va_list args; + va_start(args, format); + ret = this->vprintf(format, args); + + va_end(args); + return ret; +} + +int8_t ctext::nprintf(const char*format, ...) +{ + int8_t ret; + va_list args; + + // First turn off the rerdaw flag + this->ob_start(); + + // Then call the variadic version + va_start(args, format); + ret = this->vprintf(format, args); + + va_end(args); + + // + // Then manually untoggle the flag + // (this is necessary because ob_end + // does TWO things, breaking loads of + // anti-patterns I'm sure.) + // + this->m_do_draw = true; + + return ret; +} + +#if 0 +int8_t ctext::redraw_partial_test() +{ + attr_t res_attrs; + int16_t res = 0; + int16_t res_color_pair; + int32_t x, y, end_x; + string *data; + + this->get_win_size(); + wattr_get(this->m_win, &res_attrs, &res_color_pair, 0); + wattr_off(this->m_win, COLOR_PAIR(res_color_pair), 0); + + this->rebuf(); + werase(this->m_win); + + x = this->m_pos_start.x; + y = this->m_pos_start.y; + data = &this->m_buffer[y].data; + + while(!(res & CTEXT_OVER_Y)) + { + this->m_attr_mask ^= A_REVERSE; + end_x = min(x + rand() % 9 + 1, (int32_t)data->size()); + res = this->redraw_partial(x, y, end_x, y); + wrefresh(this->m_win); + usleep(1000 * 500); + + x = end_x; + + if(end_x == (int32_t)data->size() || (res & CTEXT_OVER_X)) + { + y++; + x = 0; + if(y > this->m_max_y) + { + break; + } + data = &this->m_buffer[y].data; + } + } + + wrefresh(this->m_win); + wattr_set(this->m_win, res_attrs, res_color_pair, 0); + + return 0; +} +#endif + +int16_t ctext::redraw_partial(ctext_pos *pos, size_t len) +{ + return this->redraw_partial(pos->x, pos->y, pos->x + len, pos->y); +} + +// +// redraw_partial takes a buffer offset and sees if it is +// to be drawn within the current view port, which is specified +// by m_pos_start.x and m_pos_start.y. +// +int16_t ctext::redraw_partial( + int32_t buf_start_x, int32_t buf_start_y, + int32_t buf_end_x, int32_t buf_end_y) +{ + bool b_format = false; + string to_add; + ctext_row *p_source; + vector::iterator p_format; + bool is_first_line = true; + int16_t ret = 0; + int32_t num_added_x = 0; + int32_t num_to_add = 0; + int32_t win_current_x; + int32_t win_current_end_x; + int32_t buf_offset_x; + + // We need to get relative start and end positions. + ctext_pos win_start, win_end; + + buf_start_x = max(0, buf_start_x); + + ret = this->map_to_win(buf_start_x, buf_start_y, &win_start); + + // This means that none of this will map to screen, + // return the overflow and bail. + if(ret & CTEXT_OVER_Y) + { + return ret; + } + + ret = this->map_to_win(buf_end_x, buf_end_y, &win_end); + + // This also means that none of this will map to screen, + // return the underflow and bail. + if(ret & CTEXT_UNDER_Y) + { + return ret; + } + + // + // We start as m_pos_start.y in our list and move up to + // m_pos_start.y + m_win_height except in the case of + // wrap around. Because of this special case, + // we compute when to exit slightly differently. + // + // This is the current line of output, which stays + // below m_win_height + // + int32_t win_current_y = win_start.y; + int32_t buf_current_y = buf_start_y; + + // This is for horizontal scroll. + int32_t start_char = max(0, this->m_pos_start.x); + + while(win_current_y <= win_end.y) + { + win_current_end_x = this->m_win_width; + + // If we are at the last line to generate + if(win_current_y == win_end.y) + { + // Then we make sure that we end + // where we are supposed to. + win_current_end_x = win_end.x; + } + + wredrawln(this->m_win, win_current_y, 1); + + if((buf_current_y < this->m_max_y) && (buf_current_y >= 0)) + { + // We only buf_current_y into the object if we have the + // data to do so. + p_source = &this->m_buffer[buf_current_y]; + p_format = p_source->format.begin(); + + // Reset the offset. + win_current_x = -min(0, (int32_t)this->m_pos_start.x); + + if(is_first_line) + { + buf_offset_x = buf_start_x; + win_current_x += win_start.x; + } + else + { + buf_offset_x = start_char; + } + + for(;;) + { + // Our initial num_to_add is the remainder of window space + // - our start (end of the screen - starting position) + num_to_add = win_current_end_x - win_current_x; + b_format = false; + + wstandend(this->m_win); + + // If we have a format to account for and we haven't yet, + if(!p_source->format.empty() && p_format->offset <= buf_offset_x) + { + // Then we add it + wattr_set(this->m_win, p_format->attrs | this->m_attr_mask, p_format->color_pair, 0); + + // and tell ourselves below that we've done this. + b_format = true; + + // see if there's another num_to_add point + if((p_format + 1) != p_source->format.end()) + { + // + // If it's before our newline then we'll have to do something + // with with that. + // + // The first one is the characters we are to print this time, + // the second is how many characters we would have asked for + // if there was no format specified. + // + num_to_add = min((p_format + 1)->offset - buf_offset_x, num_to_add); + } + } + else if(this->m_attr_mask) + { + wattr_set(this->m_win, this->m_attr_mask, 0, 0); + } + + // + // If we can get that many characters than we grab them + // otherwise we do the empty string + // + if(buf_offset_x < (int32_t)p_source->data.size()) + { + to_add = p_source->data.substr(buf_offset_x, num_to_add); + + mvwaddstr(this->m_win, win_current_y, win_current_x, to_add.c_str()); + is_first_line = false; + } + else + { + to_add = ""; + } + + // This is the number of characters we've placed into + // the window. + num_added_x = to_add.size(); + buf_offset_x += num_added_x; + + // See if we need to reset our format + if(b_format) + { + // + // If the amount of data we tried to grab is less than + // the width of the window - win_offset then we know to + // turn off our attributes and push our format forward + // if necessary. + // + if( (p_format + 1) != p_source->format.end() && (p_format + 1)->offset >= buf_offset_x ) + { + p_format ++; + } + } + + // if we are at the end of the string, we break out + if((int32_t)p_source->data.size() <= buf_offset_x || (num_added_x == 0 && p_source->data.size() > 0)) + { + break; + } + + // Otherwise, move win_current_x forward + win_current_x += num_added_x; + + // Otherwise, if we are wrapping, then we do that here. + if(win_current_x == win_current_end_x) + { + // + // If we've hit the vertical bottom + // of our window then we break out + // of this + // + // Otherwise if we are not wrapping then + // we also break out of this. + // + if(win_current_y == win_end.y) + { + break; + } + + // Otherwise move our line forward + win_current_y++; + + // If we are at the last line to generate + if(win_current_y == win_end.y) + { + // Then we make sure that we end + // where we are supposed to. + win_current_end_x = win_end.x; + } + + // We reset the win_current_x back to its + // initial state + win_current_x = 0; + + // and we loop again. + } + } + } + buf_current_y++; + win_current_y++; + } + + return ret; +} + +int8_t ctext::redraw() +{ + // + // Bail out if we aren't supposed to draw + // this time. + // + // Calculate the bounds of everything first. + // + this->rebuf(); + if(!this->m_do_draw) + { + return 0; + } + + if(!this->m_win) + { + // Not doing anything without a window. + return -1; + } + + attr_t res_attrs; + int16_t res_color_pair; + bool is_first_line = true; + wattr_get(this->m_win, &res_attrs, &res_color_pair, 0); + wattr_off(this->m_win, COLOR_PAIR(res_color_pair), 0); + + this->get_win_size(); + + // + // By this time, if we are bounded by a box, + // it has been accounted for. + // + // Really our only point of interest is + // whether we need to append to bottom + // or append to top. + // + // We will assume that we can + // populate the window quick enough + // to avoid linear updating or paging. + // ... it's 2015 after all. + // + werase(this->m_win); + + // + // Regardless of whether this is append to top + // or bottom we generate top to bottom. + // + int32_t start_char = max(0, this->m_pos_start.x); + int32_t buf_offset = start_char; + // the endchar will be in the substr + + // + // We start as m_pos_start.y in our list and move up to + // m_pos_start.y + m_win_height except in the case of + // wrap around. Because of this special case, + // we compute when to exit slightly differently. + // + // This is the current line of output, which stays + // below m_win_height + // + int32_t line = 0; + + // Start at the beginning of the buffer. + int32_t index = this->m_pos_start.y; + int32_t directionality = +1; + int32_t cutoff; + int32_t num_added = 0; + int32_t win_offset = 0; + bool b_format = false; + string to_add; + ctext_row *p_source; + vector::iterator p_format; + + // If we are appending to the top then we start + // at the end and change our directionality. + if(this->m_config.m_append_top) + { + directionality = -1; + index = this->m_pos_start.y + this->m_win_height - 1; + } + + while(line <= this->m_win_height) + { + wredrawln(this->m_win, line, 1); + + if((index < this->m_max_y) && (index >= 0)) + { + // We only index into the object if we have the + // data to do so. + p_source = &this->m_buffer[index]; + p_format = p_source->format.begin(); + + // Reset the offset. + win_offset = -min(0, (int32_t)this->m_pos_start.x); + buf_offset = start_char; + + if(this->m_config.m_do_wrap) + { + buf_offset = is_first_line ? this->m_pos_start.x : 0; + } + + for(;;) + { + // Our initial cutoff is the remainder of window space + // - our start + cutoff = this->m_win_width - win_offset; + b_format = false; + + wstandend(this->m_win); + + // If we have a format to account for and we haven't yet, + if(!p_source->format.empty() && p_format->offset <= buf_offset) + { + // then we add it + wattr_set(this->m_win, p_format->attrs, p_format->color_pair, 0); + + // and tell ourselves below that we've done this. + b_format = true; + + // See if there's another cutoff point + if((p_format + 1) != p_source->format.end()) + { + // + // If it's before our newline then we'll have to do something + // with with that. + // + // The first one is the characters we are to print this time, + // the second is how many characters we would have asked for + // if there was no format specified. + // + cutoff = min((p_format + 1)->offset - buf_offset, cutoff); + } + } + + // If we can get that many characters than we grab them + // otherwise we do the empty string + if(buf_offset < (int32_t)p_source->data.size()) + { + to_add = p_source->data.substr(buf_offset, cutoff); + + mvwaddstr(this->m_win, line, win_offset, to_add.c_str()); + is_first_line = false; + } + else + { + to_add = ""; + } + + // This is the number of characters we've placed into + // the window. + num_added = to_add.size(); + buf_offset += num_added; + + // See if we need to reset our format + if(b_format) + { + // + // If the amount of data we tried to grab is less than + // the width of the window - win_offset then we know to + // turn off our attributes and push our format forward if + // necessary. + // + if( (p_format + 1) != p_source->format.end() && + (p_format + 1)->offset >= buf_offset + ) + { + p_format ++; + } + } + + // If we are at the end of the string, we break out + if((int32_t)p_source->data.size() <= buf_offset || (num_added == 0 && p_source->data.size() > 0)) + { + break; + } + + // otherwise, move win_offset forward + win_offset += num_added; + + // otherwise, if we are wrapping, then we do that here. + if(win_offset == this->m_win_width) + { + // + // If we've hit the vertical bottom + // of our window then we break out + // of this otherwise if we are not + // wrapping then we also break out + // of this. + // + if(line == this->m_win_height || !this->m_config.m_do_wrap) + { + break; + } + + // Otherwise move our line forward + line++; + + // We reset the win_offset back to its + // initial state + win_offset = 0; + + // And we loop again. + } + } + } + index += directionality; + line++; + } + + this->highlight_matches(); + wrefresh(this->m_win); + wattr_set(this->m_win, res_attrs, res_color_pair, 0); + + return 0; +} + +#endif // _WIN32 diff --git a/userspace/libsinsp/ctext.h b/userspace/libsinsp/ctext.h new file mode 100644 index 0000000000..bed9d36f82 --- /dev/null +++ b/userspace/libsinsp/ctext.h @@ -0,0 +1,534 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#ifndef _WIN32 +#include +#include +#include +#include +#include +#include + +#ifndef __83a9222a_c8b9_4f36_9721_5dfbaccb28d0_CTEXT +#define __83a9222a_c8b9_4f36_9721_5dfbaccb28d0_CTEXT +#define CTEXT_BUFFER_SIZE (4096) + +using namespace std; + +class ctext; + +struct ctext_config_struct +{ + // + // This specifies how many lines are kept + // in the ring-buffer. + // + // A value of -1 means to keep it completely + // unregulated. + // + int32_t m_buffer_size; +#define CTEXT_DEFAULT_BUFFER_SIZE 500 + + // + // The bounding box bool specifies whether + // we are allowed to move outside where the + // content exists. Pretend there's the + // following content and window (signified + // by the + marks) + // + // + + + // xxxx + // xxx + // xxxxx + // + + + // + // If we were to move say, 3 units right and + // had no bounding box (false) you'd see this: + // + // + + + // x + // + // xx + // + + + // + // In other words, we could potentially scroll + // well beyond our content. + // + // A bounding box set to true would prevent this, + // making sure that the viewport doesn't extend + // beyond the existing content. + // + bool m_bounding_box; +#define CTEXT_DEFAULT_BOUNDING_BOX false + + // + // Sometimes content can be extremely lengthy + // on one line, overwhelming any other content + // in view, losing the context of what the content + // means. + // + // In these cases, we can truncate long text from + // occupying the next row of text, and instead extend + // beyond the viewport of our window. Under these cases + // the user will have to scroll the viewport in order + // to see the remainder of the text. + // + bool m_do_wrap; +#define CTEXT_DEFAULT_DO_WRAP false + + // + // In most user interfaces, new text appears + // underneath previous text on a new line. + // + // However, sometimes it's more natural to see + // new entries come in ON TOP of old ones, pushing + // the old ones downward. + // + // m_append_top deals with this duality + // + bool m_append_top; +#define CTEXT_DEFAULT_APPEND_TOP false + + // + // Sometimes seeing new content is of the utmost + // importance and takes precedence over analysis + // of any historical data. + // + // In that case, the scroll_on_append will forcefully + // scroll the text so that the new content is + // visible. + // + bool m_scroll_on_append; +#define CTEXT_DEFAULT_SCROLL_ON_APPEND false + + // + // The auto_newline boolean will specify whether + // a newline is appended at the end of every printf + // call automatically for you ... as opposed to + // the more traditional printf where it is not. + // + // Our definition of newline is unixes, that is + // the single character of 0x0A, or \n. + // + bool m_auto_newline; +#define CTEXT_DEFAULT_AUTO_NEWLINE false +}; + +typedef struct ctext_config_struct ctext_config; + +typedef struct ctext_format_struct +{ + int32_t offset; + attr_t attrs; + int16_t color_pair; +} ctext_format; + +typedef struct ctext_pos_struct +{ + int32_t x; + int32_t y; +} ctext_pos; + +typedef struct ctext_search_struct +{ + // The current position of the + // search. ... you could do + // + // ct.get_offset(&search.pos); + // + // in order to initialize it to + // the current point. + // + ctext_pos pos; + + // Should we wrap around when + // we are done. + bool do_wrap; + + // True if we are searching forward + // false if we aren't. + bool is_forward; + + // Case insensitivity is defined in the + // classic (c >= 'A' ? c | 0x20) manner. + bool is_case_insensitive; + + // This is used internally, + // please don't modify. + ctext_pos _start_pos; + ctext_pos _last_match; + uint64_t _last_event; + int16_t _match_count; + + // The string to match + string _query; + +} ctext_search; + +typedef struct ctext_row_struct +{ + string data; + vector format; +} ctext_row; + +typedef vector ctext_buffer; + +class ctext +{ + public: + ctext(WINDOW *win = 0, ctext_config *config = 0); + + // + // A ctext instance has a configuration specified through + // the ctext_config structure above + // + // When this function is called, a copy of the structure + // is made so that further modifications are not reflected + // in a previously instantiated instance. + // + // Returns 0 on success + // + int8_t set_config(ctext_config *config); + + // + // get_config allows you to change a parameter in the + // configuration of a ctext instance and to duplicate + // an existing configuration in a new instance. + // + // Returns 0 on success + // + int8_t get_config(ctext_config *config); + + // + // At most 1 curses window may be attached at a time. + // + // This function specifies the curses window which + // will be attached given this instance. + // + // If one is already attached, it will be detached and + // potentially orphaned. + // + // Returns 0 on success + // + int8_t attach_curses_window(WINDOW *win); + + // + // Under normal circumstances, a user would like to + // remove all the existing content with a clear, starting + // anew. + // + // However, if you'd like to only remove part of the content, + // then you can pass a row_count in and clear will truncate + // row_count units of the oldest content. + // + // The return code is how many rows were cleared from the + // buffer. + // + int32_t clear(int32_t row_count = -1); + + // + // Scroll_to when appending to the bottom in the traditional + // default sense, will specify the top x and y coordinate + // of the viewport. + // + // However, if we are appending to the top, then since new + // content goes above the previous one, scroll_to specifies + // the lower left coordinate. + // + // Returns 0 on success + // + int8_t scroll_to(int32_t x, int32_t y); + int8_t scroll_to(ctext_pos *pos); + + // get_offset returns the current coordinates of the view port. + // The values from get_offset are complementary to those + // of scroll_to + // + // Returns 0 on success + // + int8_t get_offset(int32_t*x, int32_t*y); + int8_t get_offset(ctext_pos *pos); + + // + // get_offset_percent is a courtesy function returning + // a percentage value corresponding to the Y amount of + // scroll within the window. + // + // Returns 0 on success + // + int8_t get_offset_percent(float*percent); + + // + // get_buf_size returns the number of rows of content + // for y in the current buffer. + // + // Returns 0 on success + // + int8_t get_buf_size(int32_t*buf_size); + + // + // available_rows communicates how many rows + // are left given the buffer size specified + // in the config with respect to the amount + // of content placed in the buffer. + // + // If that sounds overly complex, I assure + // you this function does pretty much exactly + // what you'd expect ... it tells you how + // much space you have left in the buffer + // before its full and starts dropping + // content. + // + // Returns number of available rows + // + int32_t available_rows(); + + // + // Each of the directional functions, + // up, down, left, and right, can be + // called without an argument to move + // 1 unit in their respective direction. + // + // The return code is how far the movement + // happened. + // + int32_t up(int32_t amount = 1); + int32_t down(int32_t amount = 1); + int32_t left(int32_t amount = 1); + int32_t right(int32_t amount = 1); + + // + // Identical to the above functions but this + // time by an entire page of content (that + // is to say, the height of the current curses + // window.) + // + int32_t page_up(int32_t page_count = 1); + int32_t page_down(int32_t page_count = 1); + + // + // The jump_to_first_line and jump_to_last_line + // can conveniently be mapped to home/end keys + // and do what they say under the following + // condition: + // + // If the bounding_box is set to true, + // then the "first" and "last" line corresponds + // to an entire screen full of data. + // + // If the bounding box is set to false, then + // the screen will be empty except for 1 line + // corresponding to the first or last line. + // + // That is to say that with a bounding box off, + // you'd see something like + // + // + + + // + // + // xxxxx + // + + + // + // when we are doing jump_to_first_line. + // + // With a bounding_box on you'd see + // + // + + + // xxxxx + // xx + // xxx + // + + + // + // The return code is how many vertical lines + // were scrolled in order to accomplish the + // action. + // + int32_t jump_to_first_line(); + int32_t jump_to_last_line(); + + // + // printf is identical to printf(3) and can be called + // from the function at the end of this file, cprintf, + // with an instance variable. It places text into the + // buffer specified. + // + // You can take the function pointer of printf from + // an existing application and point it to this printf + // inside of an instance in order to migrate an existing + // application to this library seamlessly. + // + int8_t printf(const char*format, ...); + int8_t vprintf(const char*format, va_list ap); + + // + // nprintf is identical to the printf above EXCEPT for + // the fact that it doesn't refresh (redraw) the screen. + // + // In order to do that, a redraw (below) must be called + // manually. + // + int8_t nprintf(const char*format, ...); + + // + // Under normal (printf) conditions, this does not + // need to be called explicitly and is instead called + // each time a printf is called. + // + int8_t redraw(); + + // + // A naming convention inspired from php's ob_start, + // this function stops refreshing the screen until + // ob_end is called, upon which a refresh is done. + // + // Internally, a binary flag is flipped. That is + // to say that multiple ob_start calls will only + // set the flag to TRUE, all to be undone by a single + // ob_end call. + // + // Returns 0 if the call was meaningful (that is, + // it toggled state) - otherwise -1. + // + int8_t ob_start(); + int8_t ob_end(); + + // + // This highlights a search context given a mask. + // A few big mask options are A_REVERSE, A_UNDERLINE, + // A_BLINK, and A_BOLD. They can be binary ORed. + // + int8_t highlight(ctext_search *context = 0, int32_t mask = A_REVERSE); + + // + // This is how you initialize a search. + // + // You are free to toggle the properties of the object + // at your own pleasure or peril. + // + // Returns you_manage_this_memory back at you or NULL + // on an error. + // + ctext_search *new_search(ctext_search *you_manage_this_memory, string to_search, bool is_case_insensitive = false, bool is_forward = true, bool do_wrap = false); + + // + // If you want to modify the query of an existing search then you + // should call this function directly instead of trying to modify + // the parameter yourself. + // + // Returns 0 on success + // + int8_t set_query(ctext_search *p_search, string new_query); + + // + // After you've initiated your search you can then go over + // the body of text by re-executing the str_search function. + // + // You don't have to worry about incrementing any silly variables + // to avoid an infinite loop on the previous match or any of those + // annoyances that the base c/c++ libraries decided to make YOUR + // problem every time. + // + // This function will go to the "next" match (based on your parameters) + // and then highlight all the matches in the viewport. You can + // "turn off" this highlighting by running search_off (see below). + // + // Returns 0 every time a valid search is found and something + // "happened". Otherwise you get something non-zero, signifying that + // the search is "done". + // + int8_t str_search(ctext_search *to_search); + + // Turn off syntax highlighting from search. + int8_t search_off(); + + private: + // + // This function answers the question "where on the screen would + // the buffer at line X, character Y appear?" The *win gets populated + // with the answer or a value is returned if there's an overflow. + // + int8_t map_to_win(int32_t buffer_x, int32_t buffer_y, ctext_pos *win); + + int8_t y_scroll_calculate(int32_t amount, ctext_pos *pos); + int16_t redraw_partial(int32_t buf_start_x, int32_t buf_start_y, int32_t buf_end_x, int32_t buf_end_y); + int16_t redraw_partial(ctext_pos *pos, size_t len); + + // This is just a test function to make sure everything works. + int8_t redraw_partial_test(); + + ctext_row* add_row(); + void add_format_if_needed(); + int8_t rebuf(); + void get_win_size(); + + // Highlights the matches in he current viewport without + // doing any scrolling. + int8_t highlight_matches(ctext_search *context = 0); + + // + // Directly scroll to an x/y location with respect to + // the buffer without any redraw or other calculation. + // + // This just moves the internal pointers forward with + // respect to the internal configuration. + // + // The return value is 0 iff the value of the scroll + // was changed. Otherwise, if nothing changed in the + // request, -1 is returned. + // + int8_t direct_scroll(int32_t x, int32_t y); + int8_t direct_scroll(ctext_pos *pos); + + // A mast to apply to the text being rendered. + attr_t m_attr_mask; + + // + // Leave the new_pos_out as null for an idempotent version of this function - + // as in one that doesn't modify the to_search_in variable in returning a value. + // + // It's perfectly acceptable to pass the same variable as both to_search_in and + // new_pos_out if you want to execute it with a side-effect - as much of the + // implementation actually does. + // + int8_t str_search_single(ctext_search *to_search_in, ctext_search *new_pos_out = 0, ctext_pos *limit = 0); + + // Whether or not to draw when new text comes in or to skip the step. + bool m_do_draw; + WINDOW *m_win; + ctext_config m_config; + ctext_buffer m_buffer; + ctext_search *m_last_search; + + // The start point of the buffer with + // respect to the current viewport + ctext_pos m_pos_start; + + int32_t m_max_y; + int32_t m_win_width; + int32_t m_win_height; + uint64_t m_event_counter; + + ofstream *m_debug; +}; + +int cprintf(ctext*win, const char *format, ...); + +#endif + +#endif // _WIN32 diff --git a/userspace/libsinsp/cursescomponents.cpp b/userspace/libsinsp/cursescomponents.cpp new file mode 100644 index 0000000000..4003cb4e8c --- /dev/null +++ b/userspace/libsinsp/cursescomponents.cpp @@ -0,0 +1,1997 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include +#include +#ifndef _WIN32 +#include +#include +#endif +#include +#include +#include +#include +#include +#include +using namespace std; + +#include "sinsp.h" +#include "sinsp_int.h" +#include "../../driver/ppm_ringbuffer.h" +#include "filter.h" +#include "filterchecks.h" + +#ifdef CSYSDIG +#include "table.h" +#include "cursescomponents.h" +#include "cursestable.h" +#include "viewinfo.h" +#include "cursesui.h" +#include "utils.h" + +extern bool g_filterchecks_force_raw_times; + +/////////////////////////////////////////////////////////////////////////////// +// spy_text_renderer implementation +/////////////////////////////////////////////////////////////////////////////// +spy_text_renderer::spy_text_renderer(sinsp* inspector, + sinsp_cursesui* parent, + int32_t viz_type, + sysdig_output_type sotype, + bool print_containers, + sinsp_evt::param_fmt text_fmt) +{ + m_formatter = NULL; + m_inspector = inspector; + m_viz_type = viz_type; + m_linecnt = 0; + g_filterchecks_force_raw_times = false; + + // + // visualization-type inits + // + if(m_viz_type == VIEW_ID_DIG) + { + if(sotype == spy_text_renderer::OT_LATENCY) + { + if(print_containers) + { + m_formatter = new sinsp_evt_formatter(m_inspector, + "*(latency=%evt.latency.human) (fd=%fd.name) %evt.num %evt.time %evt.cpu %container.name (%container.id) %proc.name (%thread.tid:%thread.vtid) %evt.dir %evt.type %evt.info"); + } + else + { + m_formatter = new sinsp_evt_formatter(m_inspector, + "*(latency=%evt.latency.human) (fd=%fd.name) %evt.num %evt.time %evt.cpu %proc.name %thread.tid %evt.dir %evt.type %evt.info"); + } + } + else if(sotype == spy_text_renderer::OT_LATENCY_APP) + { + if(print_containers) + { + m_formatter = new sinsp_evt_formatter(m_inspector, + "*(latency=%tracer.latency.human) %evt.num %evt.time %evt.cpu %container.name (%container.id) %proc.name (%thread.tid:%thread.vtid) %evt.dir %evt.type %evt.info"); + } + else + { + m_formatter = new sinsp_evt_formatter(m_inspector, + "*(latency=%tracer.latency.human) %evt.num %evt.time %evt.cpu %proc.name %thread.tid %evt.dir %evt.type %evt.info"); + } + } + else + { + if(print_containers) + { + m_formatter = new sinsp_evt_formatter(m_inspector, + "*%evt.num %evt.time %evt.cpu %container.name (%container.id) %proc.name (%thread.tid:%thread.vtid) %evt.dir %evt.type %evt.info"); + } + else + { + m_formatter = new sinsp_evt_formatter(m_inspector, DEFAULT_OUTPUT_STR); + } + } + } + else + { + m_formatter = NULL; + } + + m_inspector->set_buffer_format(text_fmt); +} + +spy_text_renderer::~spy_text_renderer() +{ + if(m_formatter) + { + delete m_formatter; + } +} + +const char* spy_text_renderer::process_event_spy(sinsp_evt* evt, int64_t* len) +{ + // + // Drop any non I/O event + // + ppm_event_flags eflags = evt->get_info_flags(); + + if(!(eflags & EF_READS_FROM_FD || eflags & EF_WRITES_TO_FD)) + { + return NULL; + } + + // + // Get and validate the length + // + sinsp_evt_param* parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(int64_t)); + *len = *(int64_t*)parinfo->m_val; + if(*len <= 0) + { + return NULL; + } + + // + // Get thread and fd + // + sinsp_threadinfo* m_tinfo = evt->get_thread_info(); + if(m_tinfo == NULL) + { + return NULL; + } + + sinsp_fdinfo_t* m_fdinfo = evt->get_fd_info(); + if(m_fdinfo == NULL) + { + return NULL; + } + string fdname = m_fdinfo->m_name; + if(fdname == "") + { + fdname = "unnamed FD"; + } + + // + // Get the buffer + // + const char* resolved_argstr; + char* argstr; + argstr = (char*)evt->get_param_value_str("data", &resolved_argstr, m_inspector->get_buffer_format()); + + // + // Trim initial or final \n + // + if(argstr) + { + uint32_t argstrlen = strlen(argstr); + + if(argstrlen >= 1) + { + if(*argstr == '\n') + { + argstr++; + argstrlen--; + } + + if(argstrlen >= 1) + { + if(argstr[argstrlen -1] == '\n') + { + argstr[argstrlen - 1] = 0; + } + } + } + } + + return argstr; +} + +#ifndef NOCURSESUI +#include +#include "ctext.h" + +/////////////////////////////////////////////////////////////////////////////// +// curses_scrollable_list implementation +/////////////////////////////////////////////////////////////////////////////// +curses_scrollable_list::curses_scrollable_list() +{ + m_selct = 0; + m_firstrow = 0; + m_lastrow_selected = true; +} + +void curses_scrollable_list::sanitize_selection(int32_t datasize) +{ + if(m_firstrow > (datasize - (int32_t)m_h + 1)) + { + m_firstrow = datasize - (int32_t)m_h + 1; + } + + if(m_firstrow < 0) + { + m_firstrow = 0; + } + + if(m_selct > datasize - 1) + { + m_selct = datasize - 1; + } + + if(m_selct < 0) + { + m_selct = 0; + } + + if(m_firstrow > m_selct) + { + m_firstrow = m_selct; + } +} + +void curses_scrollable_list::selection_up(int32_t datasize) +{ + if(m_selct > 0) + { + if(m_selct <= (int32_t)m_firstrow) + { + m_firstrow--; + } + + m_selct--; + sanitize_selection(datasize); + } + + m_lastrow_selected = false; +} + +void curses_scrollable_list::selection_down(int32_t datasize) +{ + if(m_selct < datasize - 1) + { + if(m_selct - m_firstrow > (int32_t)m_h - 3) + { + m_firstrow++; + } + + m_selct++; + sanitize_selection(datasize); + } + + if(m_selct == datasize - 1) + { + m_lastrow_selected = true; + } +} + +void curses_scrollable_list::selection_pageup(int32_t datasize) +{ + m_firstrow -= (m_h - 1); + m_selct -= (m_h - 1); + + sanitize_selection(datasize); + + m_lastrow_selected = false; +} + +void curses_scrollable_list::selection_pagedown(int32_t datasize) +{ + m_firstrow += (m_h - 1); + m_selct += (m_h - 1); + + sanitize_selection(datasize); + + if(m_selct == datasize - 1) + { + m_lastrow_selected = true; + } +} + +void curses_scrollable_list::selection_home(int32_t datasize) +{ + m_firstrow = 0; + m_selct = 0; + + sanitize_selection(datasize); + + m_lastrow_selected = false; +} + +void curses_scrollable_list::selection_end(int32_t datasize) +{ + m_firstrow = datasize - 1; + m_selct = datasize - 1; + + sanitize_selection(datasize); + + m_lastrow_selected = true; +} + +void curses_scrollable_list::selection_goto(int32_t datasize, int32_t row) +{ + if(row == -1 || + row >= datasize) + { + ASSERT(false); + return; + } + + m_firstrow = row - (m_h /2); + m_selct = row; + + sanitize_selection(datasize); +} + +/////////////////////////////////////////////////////////////////////////////// +// curses_table_sidemenu implementation +/////////////////////////////////////////////////////////////////////////////// +curses_table_sidemenu::curses_table_sidemenu(sidemenu_type type, sinsp_cursesui* parent, uint32_t selct, uint32_t width) +{ + ASSERT(parent != NULL); + m_parent = parent; + m_h = m_parent->m_screenh - TABLE_Y_START - 1; + m_w = width; + m_y_start = TABLE_Y_START; + m_win = newwin(m_h, m_w, m_y_start, 0); + m_selct = selct; + m_selct_ori = m_selct; + m_type = type; + + if(m_selct > (int32_t)(m_h - 2)) + { + m_firstrow = m_selct - (int32_t)(m_h - 2); + } +} + +curses_table_sidemenu::~curses_table_sidemenu() +{ + delwin(m_win); +} + +void curses_table_sidemenu::render() +{ + int32_t j, k; + + ASSERT(m_entries.size() != 0); + + // + // Render window header + // + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_FOCUS]); + + wmove(m_win, 0, 0); + for(j = 0; j < (int32_t)m_w - 1; j++) + { + waddch(m_win, ' '); + } + + // white space at the right + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::PROCESS]); + waddch(m_win, ' '); + + ASSERT(m_title != ""); + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_FOCUS]); + mvwaddnstr(m_win, 0, 0, m_title.c_str(), m_w); + + // + // Render the rows + // + for(j = m_firstrow; j < MIN(m_firstrow + (int32_t)m_h - 1, (int32_t)m_entries.size()); j++) + { + if(j == m_selct) + { + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::PANEL_HIGHLIGHT_FOCUS]); + } + else + { + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::PROCESS]); + } + + // clear the line + wmove(m_win, j - m_firstrow + 1, 0); + for(k = 0; k < (int32_t)m_w - 1; k++) + { + waddch(m_win, ' '); + } + + // add the new line + mvwaddnstr(m_win, j - m_firstrow + 1, 0, m_entries.at(j).m_name.c_str(), m_w); + // put sorting order indicator at the right end of this row + if(m_parent->m_sidemenu_sorting_col == j) + { + wmove(m_win, j - m_firstrow + 1, m_w - 4); + char sort_order = m_parent->m_datatable->is_sorting_ascending() ? '^' : 'V'; + waddch(m_win, '('); + waddch(m_win, sort_order); + waddch(m_win, ')'); + } + + // white space at the right + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::PROCESS]); + wmove(m_win, j - m_firstrow + 1, m_w - 1); + waddch(m_win, ' '); + } + + wrefresh(m_win); +} + +// +// Update the view info page in the parent +// +void curses_table_sidemenu::update_view_info() +{ + if(m_parent->m_viewinfo_page) + { + delete m_parent->m_viewinfo_page; + + ASSERT(m_selct < (int32_t)m_entries.size()); + + m_parent->m_viewinfo_page = new curses_viewinfo_page(m_parent, + m_entries.at(m_selct).m_id, + TABLE_Y_START, + m_w, + m_parent->m_screenh - TABLE_Y_START - 1, + m_parent->m_screenw - m_w); + } +} + +// +// Return true if the parent should handle the event +// +sysdig_table_action curses_table_sidemenu::handle_input(int ch) +{ + int32_t prev_select; + int input; + + switch(ch) + { + case KEY_F(1): + case KEY_F(4): + case KEY_F(5): + case KEY_F(6): + case KEY_F(7): + case 6: + return STA_NONE; + case '\n': + case '\r': + case KEY_ENTER: + ASSERT(m_selct < (int32_t)m_entries.size()); + if(m_type == ST_VIEWS) + { + if(m_parent->m_spy_box == NULL) + { + m_parent->m_selected_view = m_entries.at(m_selct).m_id; + } + + m_parent->m_selected_view_sidemenu_entry = m_selct; + } else if(m_type == ST_COLUMNS) { + m_parent->m_selected_view_sort_sidemenu_entry = m_selct; + } + else + { + m_parent->m_selected_action_sidemenu_entry = m_selct; + } + + return STA_SWITCH_VIEW; + case KEY_BACKSPACE: + case 127: + case 27: // ESC + case KEY_RESIZE: + ASSERT(m_selct < (int32_t)m_entries.size()); + + if(m_type == ST_VIEWS) + { + if(m_parent->m_spy_box == NULL) + { + m_parent->m_selected_view = m_entries.at(m_selct).m_id; + } + + m_parent->m_selected_view_sidemenu_entry = m_selct_ori; + + return STA_SWITCH_VIEW; + } + else if(m_type == ST_COLUMNS) + { + m_parent->m_selected_view_sort_sidemenu_entry = m_selct_ori; + return STA_DESTROY_CHILD; + } + else + { + m_parent->m_selected_action_sidemenu_entry = m_selct_ori; + return STA_DESTROY_CHILD; + } + + case KEY_UP: + if(m_entries.size() == 0) + { + return STA_NONE; + } + + prev_select = m_selct; + + selection_up((int32_t)m_entries.size()); + + input = getch(); + if(input != -1) + { + return handle_input(input); + } + + if(m_selct != prev_select) + { + update_view_info(); + } + + render(); + return STA_NONE; + case KEY_DOWN: + if(m_entries.size() == 0) + { + return STA_NONE; + } + + prev_select = m_selct; + + selection_down((int32_t)m_entries.size()); + + input = getch(); + if(input != -1) + { + return handle_input(input); + } + + if(m_selct != prev_select) + { + update_view_info(); + } + + render(); + return STA_NONE; + case KEY_PPAGE: + if(m_entries.size() == 0) + { + return STA_NONE; + } + + prev_select = m_selct; + + selection_pageup((int32_t)m_entries.size()); + + input = getch(); + if(input != -1) + { + return handle_input(input); + } + + if(m_selct != prev_select) + { + update_view_info(); + } + + render(); + return STA_NONE; + case KEY_NPAGE: + if(m_entries.size() == 0) + { + return STA_NONE; + } + + prev_select = m_selct; + + selection_pagedown((int32_t)m_entries.size()); + + input = getch(); + if(input != -1) + { + return handle_input(input); + } + + if(m_selct != prev_select) + { + update_view_info(); + } + + render(); + return STA_NONE; + case KEY_HOME: + if(m_entries.size() == 0) + { + return STA_NONE; + } + + prev_select = m_selct; + + selection_home((int32_t)m_entries.size()); + + input = getch(); + if(input != -1) + { + return handle_input(input); + } + + if(m_selct != prev_select) + { + update_view_info(); + } + + render(); + return STA_NONE; + case KEY_END: + if(m_entries.size() == 0) + { + return STA_NONE; + } + + prev_select = m_selct; + + selection_end((int32_t)m_entries.size()); + + input = getch(); + if(input != -1) + { + return handle_input(input); + } + + if(m_selct != prev_select) + { + update_view_info(); + } + + render(); + return STA_NONE; + case KEY_MOUSE: + { + if(m_entries.size() == 0) + { + return STA_NONE; + } + + if(getmouse(&m_last_mevent) == OK) + { + // + // Bottom menu clicks are handled by the parent + // + if((uint32_t)m_last_mevent.y == m_parent->m_screenh - 1) + { + return STA_PARENT_HANDLE; + } + + if(m_last_mevent.bstate & BUTTON1_CLICKED) + { + if((uint32_t)m_last_mevent.y > TABLE_Y_START && + (uint32_t)m_last_mevent.y < TABLE_Y_START + m_h - 1) + { + // + // This is a click one of the menu entries. Update the selection. + // + m_selct = m_firstrow + (m_last_mevent.y - TABLE_Y_START - 1); + sanitize_selection((int32_t)m_entries.size()); + update_view_info(); + render(); + } + } + else if(m_last_mevent.bstate & BUTTON1_DOUBLE_CLICKED) + { + if((uint32_t)m_last_mevent.y > TABLE_Y_START && + (uint32_t)m_last_mevent.y < TABLE_Y_START + m_h - 1) + { + // + // This is a double click one of the menu entries. + // Update the selection. + // + m_selct = m_firstrow + (m_last_mevent.y - TABLE_Y_START - 1); + sanitize_selection((int32_t)m_entries.size()); + render(); + + // + // This delay is here just as a lazy way to give the user the + // feeling that the row has been clicked + // + usleep(200000); + + // + // Notify the parent that a selection has happened + // + ASSERT(m_selct < (int32_t)m_entries.size()); + if(m_parent->m_spy_box == NULL) + { + m_parent->m_selected_view = m_entries.at(m_selct).m_id; + } + + if(m_type == ST_VIEWS) + { + m_parent->m_selected_view_sidemenu_entry = m_selct; + } + else if(m_type == ST_COLUMNS) + { + m_parent->m_selected_view_sort_sidemenu_entry = m_selct; + } + else + { + m_parent->m_selected_action_sidemenu_entry = m_selct; + } + + return STA_SWITCH_VIEW; + } + } + } + } + + return STA_NONE; + default: + break; + } + + return STA_PARENT_HANDLE; +} + +/////////////////////////////////////////////////////////////////////////////// +// curses_textbox implementation +/////////////////////////////////////////////////////////////////////////////// +curses_textbox::curses_textbox(sinsp* inspector, sinsp_cursesui* parent, int32_t viz_type, spy_text_renderer::sysdig_output_type sotype) +{ + ASSERT(inspector != NULL); + ASSERT(parent != NULL); + + m_parent = parent; + m_win = NULL; + m_ctext = NULL; + m_filter = NULL; + m_text_renderer = NULL; + m_inspector = inspector; + n_prints = 0; + m_paused = false; + m_sidemenu = NULL; + m_searcher = NULL; + m_has_searched = false; + m_last_progress_update_ts = 0; + + ctext_config config; + + m_win = newwin(m_parent->m_screenh - 4, m_parent->m_screenw, TABLE_Y_START + 1, 0); + m_ctext = new ctext(m_win); + + m_ctext->get_config(&config); + + config.m_buffer_size = 500000; + config.m_scroll_on_append = true; + config.m_bounding_box = true; + + m_text_renderer = new spy_text_renderer(inspector, + parent, + viz_type, + sotype, + m_parent->m_print_containers, + sinsp_evt::PF_NORMAL); + + + if(viz_type == VIEW_ID_DIG) + { + config.m_do_wrap = false; + } + else + { + config.m_do_wrap = true; + } + + // + // set the config back + // + m_ctext->set_config(&config); + + // + // Allocate the searcher + // + m_searcher = new ctext_search(); + + // + // If we're offline, disable screen refresh until we've parsed the file + // + if(!m_inspector->is_live()) + { + m_ctext->ob_start(); + } + + // + // Initialize the inspector to capture longer buffers and format them in a + // readable way + // + m_inspector->set_buffer_format(sinsp_evt::PF_NORMAL); + m_inspector->set_snaplen(2000); + + // + // Tell the parent to check for input more often + // + m_parent->m_input_check_period_ns = 100000; + + // + // Initial screen refresh + // + render(); +} + +curses_textbox::~curses_textbox() +{ + if(m_sidemenu) + { + delete m_sidemenu; + } + + delwin(m_win); + + delete m_ctext; + + if(m_searcher) + { + delete m_searcher; + } + + if(m_filter != NULL) + { + delete m_filter; + } + + if(m_text_renderer) + { + delete m_text_renderer; + } + + // + // Restore default snaplen and output formatting + // + m_inspector->set_snaplen(80); + m_inspector->set_buffer_format(sinsp_evt::PF_EOLS); + + // + // Tell the parent to check for input at the usual frequency + // + m_parent->m_input_check_period_ns = UI_USER_INPUT_CHECK_PERIOD_NS; +} + +void curses_textbox::set_filter(string filter) +{ + sinsp_filter_compiler compiler(m_inspector, filter); + m_filter = compiler.compile(); +} + +void curses_textbox::print_no_data() +{ + attrset(m_parent->m_colors[sinsp_cursesui::PROCESS]); + + string wstr = "No Data For This Selection"; + mvprintw(m_parent->m_screenh / 2, + m_parent->m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + + refresh(); +} + +void curses_textbox::process_event_spy(sinsp_evt* evt, int32_t next_res) +{ + int64_t len; + const char* argstr = m_text_renderer->process_event_spy(evt, &len); + + if(argstr == NULL) + { + return; + } + + // Note: this can't be NULL because it's been validated by + // m_text_renderer->process_event_spy + sinsp_threadinfo* m_tinfo = evt->get_thread_info(); + + // + // Create the info string + // + string info_str = "------ "; + string dirstr; + string cnstr; + + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & EF_READS_FROM_FD) + { + dirstr = "Read "; + cnstr = "from "; + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::SPY_READ]); + } + else if(eflags & EF_WRITES_TO_FD) + { + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::SPY_WRITE]); + dirstr = "Write "; + cnstr = "to "; + } + + info_str += dirstr + to_string(len) + + "B " + + cnstr + + evt->get_fd_info()->m_name + + " (" + m_tinfo->m_comm.c_str() + ")"; + + // + // Sanitize the info string + // + sanitize_string(info_str); + + // + // Print the whole thing + // + m_ctext->printf("%s", info_str.c_str()); + + if(m_parent->m_print_containers) + { + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::LED_COLOR]); + + m_ctext->printf(" [%s]", m_inspector->m_container_manager.get_container_name(m_tinfo).c_str()); + + if(eflags & EF_READS_FROM_FD) + { + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::SPY_READ]); + } + else if(eflags & EF_WRITES_TO_FD) + { + wattrset(m_win, m_parent->m_colors[sinsp_cursesui::SPY_WRITE]); + } + } + + m_ctext->printf("\n"); + m_ctext->printf("\n"); + m_ctext->printf("%s", argstr); + + m_ctext->printf("\n"); + m_ctext->printf("\n"); + + n_prints++; + + if(n_prints == 1) + { + render(); + } +} + +void curses_textbox::process_event_dig(sinsp_evt* evt, int32_t next_res) +{ + if(!m_inspector->is_debug_enabled() && evt->get_category() & EC_INTERNAL) + { + return; + } + + string line; + + m_text_renderer->m_formatter->tostring(evt, &line); + + m_ctext->printf("%s\n", line.c_str()); + + n_prints++; + + if(n_prints == 1) + { + render(); + } + + uint64_t ts = evt->get_ts(); + + if(ts > (m_last_progress_update_ts + 100000000)) + { + render(); + m_last_progress_update_ts = ts; + } +} + +void curses_textbox::process_event(sinsp_evt* evt, int32_t next_res) +{ + // + // Check if this the end of the capture file, and if yes take note of that + // + if(next_res == SCAP_EOF) + { + m_parent->m_eof = 2; + m_ctext->jump_to_first_line(); + m_ctext->ob_end(); + render(); + + if(n_prints == 0) + { + print_no_data(); + } + + return; + } + + // + // If the user pressed 'p', skip the event + // + if(m_paused) + { + return; + } + + // + // Filter the event + // + if(m_filter) + { + if(!m_filter->run(evt)) + { + return; + } + } + + if(m_text_renderer->m_viz_type == VIEW_ID_SPY) + { + process_event_spy(evt, next_res); + } + else + { + process_event_dig(evt, next_res); + } +} + +void curses_textbox::populate_sidemenu() +{ + ASSERT(m_sidemenu != NULL); + m_entries.clear(); + m_entries.push_back(sidemenu_list_entry("Dotted ASCII", -1)); + m_entries.push_back(sidemenu_list_entry("Printable ASCII", -1)); + m_entries.push_back(sidemenu_list_entry("Hex", -1)); + + m_sidemenu->set_entries(&m_entries); + + switch(m_parent->m_spybox_text_format) + { + case sinsp_evt::PF_NORMAL: + m_sidemenu->m_selct = 0; + break; + case sinsp_evt::PF_EOLS: + m_sidemenu->m_selct = 1; + break; + case sinsp_evt::PF_HEXASCII: + m_sidemenu->m_selct = 2; + break; + case sinsp_evt::PF_JSON: + m_sidemenu->m_selct = 3; + break; + default: + ASSERT(false); + m_sidemenu->m_selct = 0; + break; + } + + m_sidemenu->set_title("View As"); +} + +void curses_textbox::render_header() +{ + move(2, 0); + attrset(m_parent->m_colors[sinsp_cursesui::FUNCTION_BAR]); + + for(uint32_t j = 0; j < m_parent->m_screenw; j++) + { + addch(' '); + } + + refresh(); +} + +void curses_textbox::render() +{ + m_ctext->redraw(); + render_header(); + m_parent->render(); + + if(m_paused) + { + string wstr = " PAUSED "; + attrset(m_parent->m_colors[sinsp_cursesui::LARGE_NUMBER]); + mvprintw(0, + m_parent->m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + } + + // + // If required, draw the side menu + // + if(m_sidemenu) + { + m_sidemenu->render(); + } +} + +// +// Return true if the parent should handle the event +// +sysdig_table_action curses_textbox::handle_input(int ch) +{ + if(m_sidemenu) + { + sysdig_table_action ta = m_sidemenu->handle_input(ch); + if(ta == STA_SWITCH_VIEW) + { + switch(m_parent->m_selected_view_sidemenu_entry) + { + case 0: + m_parent->m_spybox_text_format = sinsp_evt::PF_NORMAL; + break; + case 1: + m_parent->m_spybox_text_format = sinsp_evt::PF_EOLS; + break; + case 2: + m_parent->m_spybox_text_format = sinsp_evt::PF_HEXASCII; + break; + case 3: + m_parent->m_spybox_text_format = sinsp_evt::PF_JSON; + break; + default: + ASSERT(false); + break; + } + return STA_SWITCH_SPY; + } + else if(ta != STA_PARENT_HANDLE) + { + return STA_NONE; + } + } + + switch(ch) + { + case KEY_F(1): + case 'q': + case KEY_RESIZE: + return STA_PARENT_HANDLE; + case 27: // ESC + case KEY_BACKSPACE: + case 127: + return STA_DRILLUP; + case KEY_UP: + m_ctext->up(); + render(); + return STA_NONE; + case '\n': + case '\r': + case KEY_ENTER: + case KEY_DOWN: + m_ctext->down(); + render(); + return STA_NONE; + case KEY_LEFT: + m_ctext->left(); + render(); + return STA_NONE; + case KEY_RIGHT: + m_ctext->right(); + render(); + return STA_NONE; + case KEY_PPAGE: + m_ctext->page_up(); + render(); + return STA_NONE; + case ' ': + case KEY_NPAGE: + m_ctext->page_down(); + render(); + return STA_NONE; + case KEY_HOME: + m_ctext->jump_to_first_line(); + m_ctext->scroll_to(0, 0); + render(); + return STA_NONE; + case KEY_END: + m_ctext->jump_to_last_line(); + render(); + return STA_NONE; + case 'c': + case KEY_DC: + m_ctext->clear(); + render(); + return STA_NONE; + case 'p': + if(m_inspector->is_live()) + { + m_paused = !m_paused; + } + m_ctext->jump_to_last_line(); + m_parent->render(); + render(); + return STA_NONE; + case KEY_F(2): + if(m_parent->m_screenw < 20) + { + return STA_NONE; + } + + if(m_sidemenu == NULL) + { + m_sidemenu = new curses_table_sidemenu(curses_table_sidemenu::ST_VIEWS, + this->m_parent, 0, VIEW_SIDEMENU_WIDTH); + populate_sidemenu(); + clear(); + wresize(m_win, m_parent->m_screenh - 4, m_parent->m_screenw - 20); + mvwin(m_win, TABLE_Y_START + 1, 20); + wrefresh(m_win); + m_parent->render(); + render(); + m_ctext->redraw(); + } + else + { + delete m_sidemenu; + m_sidemenu = NULL; + + wresize(m_win, m_parent->m_screenh - 4, m_parent->m_screenw); + mvwin(m_win, TABLE_Y_START + 1, 0); + wrefresh(m_win); + m_parent->render(); + render(); + m_ctext->redraw(); + } + + return STA_NONE; + case '/': + case KEY_F(3): + on_search_next(); + m_parent->render(); + break; + case 6: // CTRL+F + m_search_type_is_goto = false; + m_parent->turn_search_on(this, "Text"); + break; + case 7: // CTRL+G + m_search_type_is_goto = true; + m_parent->turn_search_on(this, "Line"); + break; + case KEY_MOUSE: + { + if(getmouse(&m_last_mevent) == OK) + { + return STA_PARENT_HANDLE; + } + } + + break; + default: + break; + } + + return STA_NONE; +} + +void curses_textbox::reset() +{ + if(m_sidemenu != NULL) + { + delete m_sidemenu; + m_sidemenu = NULL; + + wresize(m_win, m_parent->m_screenh - 4, m_parent->m_screenw); + mvwin(m_win, TABLE_Y_START + 1, 0); + wrefresh(m_win); + } + + m_inspector->set_buffer_format(m_parent->m_spybox_text_format); + + // + // If we're offline, disable screen refresh until we've parsed the file + // + if(!m_inspector->is_live()) + { + m_ctext->ob_start(); + } + + // + // Disable pause + // + m_paused = false; + + // + // Clear the screen + // + m_ctext->clear(); + + // + // Redraw everything + // + m_parent->render(); + render(); + m_ctext->redraw(); + n_prints = 0; +} + +bool curses_textbox::get_position(OUT int32_t* pos, + OUT int32_t* totlines, + OUT float* percent, + OUT bool* truncated) +{ + int32_t ox; + + m_ctext->get_offset(&ox, pos); + m_ctext->get_buf_size(totlines); + m_ctext->get_offset_percent(percent); + *truncated = (m_ctext->available_rows() <= 0); + + return true; +} + +string* curses_textbox::get_last_search_string() +{ + return &m_last_search_string; +} + +int8_t curses_textbox::get_offset(int32_t* x, int32_t* y) +{ + return m_ctext->get_offset(x, y); +} + +int8_t curses_textbox::scroll_to(int32_t x, int32_t y) +{ + return m_ctext->scroll_to(x, y); +} + +void curses_textbox::up() +{ + m_ctext->up(); +} + +bool curses_textbox::on_search_key_pressed(string search_str) +{ + m_last_search_string = search_str; + + if(m_search_type_is_goto) + { + uint32_t line; + + try + { + line = sinsp_numparser::parseu32(search_str); + } + catch(...) + { + return false; + } + + int32_t totlines; + m_ctext->get_buf_size(&totlines); + + if(line > (uint32_t)totlines) + { + return false; + } + + scroll_to(0, line); + return true; + } + else + { + m_ctext->new_search(m_searcher, + search_str, + true); + + if(m_ctext->str_search(m_searcher) != 0) + { + return false; + } + else + { + m_has_searched = true; + return true; + } + } +} + +bool curses_textbox::on_search_next() +{ + if(!m_has_searched) + { + return false; + } + + if(m_ctext->str_search(m_searcher) != 0) + { + return false; + } + else + { + return true; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// curses_viewinfo_page implementation +/////////////////////////////////////////////////////////////////////////////// +curses_viewinfo_page::curses_viewinfo_page(sinsp_cursesui* parent, + uint32_t viewnum, + uint32_t starty, + uint32_t startx, + uint32_t h, + uint32_t w) +{ + m_parent = parent; + ctext_config config; + int input; + + sinsp_view_info* vinfo = parent->m_views.at(viewnum); + + m_win = newwin(h, w, starty, startx); + + m_ctext = new ctext(m_win); + + m_ctext->get_config(&config); + + config.m_buffer_size = 50000; + config.m_scroll_on_append = false; + config.m_bounding_box = true; + config.m_do_wrap = true; + parent->m_selected_view_sort_sidemenu_entry = 0; + m_ctext->set_config(&config); + + // + // Print title and info + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("%s\n", vinfo->m_name.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("%s\n\n", vinfo->m_description.c_str()); + + // Stop and check for keyboard input + input = getch(); + if(input != -1) + { + handle_input(input); + } + + // + // Print the tips if present + // + if(vinfo->m_tips.size() != 0) + { + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("Tips\n"); + + for(uint32_t j = 0; j < vinfo->m_tips.size(); j++) + { + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("%s\n\n", vinfo->m_tips[j].c_str()); + } + } + + // Stop and check for keyboard input + input = getch(); + if(input != -1) + { + handle_input(input); + } + + // + // Print columns info + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("Columns\n"); + + uint32_t j; + + if(vinfo->get_type() == sinsp_view_info::T_TABLE) + { + j = vinfo->does_groupby()? 2 : 1; + } + else + { + j = 0; + } + + for(; j < vinfo->m_columns.size(); j++) + { + auto c = &(vinfo->m_columns[j]); + + string desc; + desc = c->m_description; + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("%s", c->m_name.c_str()); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": %s", desc.c_str()); + m_ctext->printf("\n"); + + // Stop and check for keyboard input + input = getch(); + if(input != -1) + { + handle_input(input); + } + } + + m_ctext->printf("\n"); + + // + // Print the view ID + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("ID\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("%s\n\n", vinfo->m_id.c_str()); + + // Stop and check for keyboard input + input = getch(); + if(input != -1) + { + handle_input(input); + } + + // + // If there's a filter, print it + // + if(vinfo->get_filter(m_parent->m_view_depth) != "") + { + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("Filter\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("%s\n\n", vinfo->get_filter(m_parent->m_view_depth).c_str()); + } + + // + // Print the actions if present + // + if(vinfo->m_actions.size() != 0) + { + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("Action Hotkeys\n"); + + for(uint32_t j = 0; j < vinfo->m_actions.size(); j++) + { + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("%c", vinfo->m_actions[j].m_hotkey); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": %s (%s)\n", + vinfo->m_actions[j].m_description.c_str(), + vinfo->m_actions[j].m_command.c_str()); + } + } + + // Stop and check for keyboard input + input = getch(); + if(input != -1) + { + handle_input(input); + } + + // + // Done. Refresh the screen + // + m_ctext->redraw(); +} + +curses_viewinfo_page::~curses_viewinfo_page() +{ + delete m_ctext; + delwin(m_win); +} + +void curses_viewinfo_page::render() +{ + m_ctext->redraw(); +} + +sysdig_table_action curses_viewinfo_page::handle_input(int ch) +{ + int32_t totlines; + + m_ctext->get_buf_size(&totlines); + + if(totlines < (int32_t)m_parent->m_screenh) + { + return STA_DESTROY_CHILD; + } + + switch(ch) + { + case KEY_UP: + m_ctext->up(); + render(); + return STA_NONE; + case '\n': + case '\r': + case KEY_ENTER: + case KEY_DOWN: + m_ctext->down(); + render(); + return STA_NONE; + case KEY_LEFT: + m_ctext->left(); + render(); + return STA_NONE; + case KEY_RIGHT: + m_ctext->right(); + render(); + return STA_NONE; + case KEY_PPAGE: + m_ctext->page_up(); + render(); + return STA_NONE; + case ' ': + case KEY_NPAGE: + m_ctext->page_down(); + render(); + return STA_NONE; + case KEY_HOME: + m_ctext->jump_to_first_line(); + m_ctext->scroll_to(0, 0); + render(); + return STA_NONE; + case KEY_END: + m_ctext->jump_to_last_line(); + render(); + return STA_NONE; + default: + break; + } + + return STA_DESTROY_CHILD; +} + +/////////////////////////////////////////////////////////////////////////////// +// curses_mainhelp_page implementation +/////////////////////////////////////////////////////////////////////////////// +extern string g_version_string; + +curses_mainhelp_page::curses_mainhelp_page(sinsp_cursesui* parent) +{ + m_parent = parent; + ctext_config config; + + m_win = newwin(parent->m_screenh, parent->m_screenw, 0, 0); + + m_ctext = new ctext(m_win); + + m_ctext->get_config(&config); + + config.m_buffer_size = 50000; + config.m_scroll_on_append = false; + config.m_bounding_box = true; + config.m_do_wrap = true; + + m_ctext->set_config(&config); + + // + // Print title and info + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::TASKS_RUNNING]); + m_ctext->printf("csysdig %s. See man page for full documentation\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("Note: you can scroll this page by using the keyboard arrows.\n\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("How to use csysdig\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf( +"1. you can either see real time data, or analyze a trace file by using the -r command line flag.\n" +"2. you can switch to a different view by using the F2 key.\n" +"3. You can drill down into a selection by clicking enter. You can navigate back by typing backspace.\n" +"4. you can observe reads and writes (F5) or see sysdig events (F6) for any selection.\n\n" +); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("Drilling down\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf( +"You drill down by selecting an element in a view and then clicking enter. Once inside a selection, you can switch to a different view, and the new view will be applied in the context of the selection. For example, if you drill down into a process called foo and then switch to the Connections view, the output will include only the connections made or received by foo.\n\n" +"To drill down multiple times, keep clicking enter. For example, you can click on a container in the Containers view to get the processes running inside it, and then click on one of the processes to see its threads.\n\n" +); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("Actions and Hotkeys\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf( +"Each view has a list of command lines that can be executed in the context of the current selection by pressing 'hotkeys'. For example, pressing 'k' in the Processes view kills the selected process, pressing 'b' in the Containers view opens a bash shell in the selected container.\n" +"Each view supports different actions. You can see which actions a view supports by pressing F8. You can customize the view's actions by editing the view's Lua file.\n\n" +); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("Containers Support\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf( +"Starting csysdig with the -pc command line switch will cause many of the views to include additional container information. For example, the _Processes_ will include a column showing the container the process belongs to. Similarly, the _Connections_ view will show which container each connection belongs to.\n\n" +); + + // + // Explore window keys + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("\nKeyboard Shortcuts for the Views Window\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" Arrows"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": scroll the table "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("CTRL+F /"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": search\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" F2"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": switch view "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("F4 \\"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": filter(freetext or sysdig)\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" Enter"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": drill down "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("F10 q"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": quit\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("Bkspace"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": drill up "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("DEL c"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": clear the view content\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" F5 e"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": echo FDs for selection "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("F7"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": see info page for the selected view\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" F6 d"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": sysdig output for selection "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("p"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": pause screen updates\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" ? F1 h"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": show this help screen "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("F8"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": open the view's actions panel\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("1-9"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": sort column "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("F9 >"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": open the column sort panel\n"); + + // + // Text windows keys + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("\nKeyboard Shortcuts for the 'Echo' and 'Sysdig' Windows\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" Arrows"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": scroll the page "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("CTRL+F /"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": search\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("Bkspace"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": drill up "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" F3"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": find next\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" F2"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": choose buffer print format "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("p"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": pause visualization\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" DEL c"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": clear the screen "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("CTRL+G"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": go to line\n"); + + // + // Spectrogram window keys + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("\nKeyboard Shortcuts for the Spectrogram Window\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf(" F2"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": switch view "); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("p"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": pause\n"); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + m_ctext->printf("Bkspace"); + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf(": drill up\n\n"); + + // + // Mouse + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("\nMouse Usage\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("Clicking on column headers lets you sort the table.\n" + "Double clicking on row entries performs a drill down.\n" + "Clicking on the filter string at the top of the screen lets you change the sysdig filter.\n" + "You can use the mouse on the entries in the menu at the bottom of the screen to perform their respective actions.\n"); + + // + // Customization + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("\nCustomizing csysdig\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("csysdig is completely customizable. This means that you can modify any of the csysdig views, " + "and even create your own views. Like sysdig chisels, csysdig views are Lua scripts. Full information can " + "be found at the following github wiki page: https://github.com/draios/sysdig/wiki/csysdig-View-Format-Reference.\n"); + + // + // sysdig cloud + // + wattrset(m_win, parent->m_colors[sinsp_cursesui::HELP_BOLD]); + m_ctext->printf("\nNeed a distributed csysdig?\n", + g_version_string.c_str()); + + wattrset(m_win, parent->m_colors[sinsp_cursesui::PROCESS]); + m_ctext->printf("Sysdig cloud offers distributed csysdig functionality, a powerful web interface and much more.\nwww.sysdig.com.\n"); + + // + // Bottom padding to compensate for a ctext bug + // + uint64_t trlen = ((parent->m_screenh * 230 / parent->m_screenw)) / 10; + + for(uint32_t j = 0; j < trlen; j++) + { + m_ctext->printf("\n"); + } + + // + // Done. Refresh the screen + // + m_ctext->redraw(); +} + +curses_mainhelp_page::~curses_mainhelp_page() +{ + delete m_ctext; + delwin(m_win); +} + +void curses_mainhelp_page::render() +{ + m_ctext->redraw(); +} + +sysdig_table_action curses_mainhelp_page::handle_input(int ch) +{ + int32_t totlines; + + m_ctext->get_buf_size(&totlines); + + if(totlines < (int32_t)m_parent->m_screenh) + { + return STA_DESTROY_CHILD; + } + + switch(ch) + { + case KEY_RESIZE: + return STA_DESTROY_CHILD; + case KEY_F(1): + return STA_NONE; + case 'q': + case KEY_F(10): + return STA_PARENT_HANDLE; + case KEY_UP: + m_ctext->up(); + render(); + return STA_NONE; + case '\n': + case '\r': + case KEY_ENTER: + case KEY_DOWN: + m_ctext->down(); + render(); + return STA_NONE; + case KEY_PPAGE: + m_ctext->page_up(); + render(); + return STA_NONE; + case ' ': + case KEY_NPAGE: + m_ctext->page_down(); + render(); + return STA_NONE; + case KEY_HOME: + m_ctext->jump_to_first_line(); + m_ctext->scroll_to(0, 0); + render(); + return STA_NONE; + case KEY_END: + m_ctext->jump_to_last_line(); + render(); + return STA_NONE; + default: + break; + } + + return STA_DESTROY_CHILD; +} + +#endif // NOCURSESUI +#endif // CSYSDIG diff --git a/userspace/libsinsp/cursescomponents.h b/userspace/libsinsp/cursescomponents.h new file mode 100644 index 0000000000..1dff1a2d81 --- /dev/null +++ b/userspace/libsinsp/cursescomponents.h @@ -0,0 +1,262 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +class search_caller_interface +{ +public: + virtual bool on_search_key_pressed(string search_str) = 0; + virtual bool on_search_next() = 0; + virtual string* get_last_search_string() = 0; +}; + +class sidemenu_list_entry +{ +public: + sidemenu_list_entry(string name, uint32_t id) + { + m_name = name; + m_id = id; + } + + string m_name; + uint32_t m_id; +}; + +#ifdef CSYSDIG +class sinsp_filter_check_reference; +class curses_table; +class sinsp_cursesui; +class ctext; +typedef struct ctext_search_struct ctext_search; +class sinsp_evt_formatter; + +class spy_text_renderer +{ +public: + enum sysdig_output_type + { + OT_NORMAL, + OT_LATENCY, + OT_LATENCY_APP, + }; + + spy_text_renderer(sinsp* inspector, + sinsp_cursesui* parent, + int32_t viz_type, + sysdig_output_type sotype, + bool print_containers, + sinsp_evt::param_fmt text_fmt); + ~spy_text_renderer(); + const char* process_event_spy(sinsp_evt* evt, int64_t* len); + + sinsp_evt_formatter* m_formatter; + sinsp* m_inspector; + int32_t m_viz_type; + uint64_t m_linecnt; +}; + +#ifndef NOCURSESUI +#define TABLE_WIDTH 10000 +#define TABLE_Y_START 2 + +#include + +class sinsp_chart +{ +public: + virtual ~sinsp_chart() + { + } + + // + // Returns false if this chart doesn't support returning the current position + // + virtual bool get_position(OUT int32_t* pos, + OUT int32_t* totlines, + OUT float* percent, + OUT bool* truncated) = 0; +}; + +class curses_table_column_info +{ +public: + curses_table_column_info() + { + } + + // + // Use -1 as size for autosize + // + curses_table_column_info(IN filtercheck_field_info* info, int32_t size) + { + m_info = *info; + m_size = size; + } + +//private: + filtercheck_field_info m_info; + int32_t m_size; + string m_name; + + friend class curses_table; +}; + +class curses_scrollable_list +{ +public: + curses_scrollable_list(); + void sanitize_selection(int32_t datasize); + void selection_up(int32_t datasize); + void selection_down(int32_t datasize); + void selection_pageup(int32_t datasize); + void selection_pagedown(int32_t datasize); + void selection_home(int32_t datasize); + void selection_end(int32_t datasize); + void selection_goto(int32_t datasize, int32_t row); + + int32_t m_selct; + int32_t m_selct_ori; + int32_t m_firstrow; + uint32_t m_w; + uint32_t m_h; + bool m_lastrow_selected; +}; + +class curses_table_sidemenu : public curses_scrollable_list +{ +public: + enum sidemenu_type { + ST_NONE, + ST_VIEWS, + ST_ACTIONS, + ST_COLUMNS, + }; + + curses_table_sidemenu(sidemenu_type type, + sinsp_cursesui* parent, + uint32_t selct, + uint32_t width); + ~curses_table_sidemenu(); + void set_entries(vector* entries) + { + m_entries = *entries; + + if(m_entries.size() == 0) + { + m_selct = 0; + } + } + void set_title(string title) + { + m_title = title; + } + void render(); + sysdig_table_action handle_input(int ch); + + WINDOW* m_win; + int32_t m_y_start; + sinsp_cursesui* m_parent; + vector m_entries; + string m_title; + MEVENT m_last_mevent; + sidemenu_type m_type; + +private: + void update_view_info(); +}; + +class curses_textbox : +public sinsp_chart, public search_caller_interface +{ +public: + curses_textbox(sinsp* inspector, sinsp_cursesui* parent, int32_t viz_type, spy_text_renderer::sysdig_output_type sotype); + ~curses_textbox(); + void render(); + void set_filter(string filter); + void print_no_data(); + void process_event(sinsp_evt* evt, int32_t next_res); + void render_header(); + sysdig_table_action handle_input(int ch); + void populate_sidemenu(); + void reset(); + bool get_position(OUT int32_t* pos, OUT int32_t* totlines, OUT float* percent, OUT bool* truncated); + string* get_last_search_string(); + int8_t get_offset(int32_t* x, int32_t* y); + int8_t scroll_to(int32_t x, int32_t y); + void up(); + bool on_search_key_pressed(string search_str); + bool on_search_next(); + + MEVENT m_last_mevent; + +private: + inline void process_event_spy(sinsp_evt* evt, int32_t next_res); + inline void process_event_dig(sinsp_evt* evt, int32_t next_res); + + WINDOW *m_win; + ctext* m_ctext; + sinsp_cursesui* m_parent; + sinsp* m_inspector; + sinsp_filter* m_filter; + uint32_t n_prints; + bool m_paused; + curses_table_sidemenu* m_sidemenu; + vector m_entries; +// int32_t m_viz_type; +// sinsp_evt_formatter* m_formatter; + string m_last_search_string; + ctext_search* m_searcher; + bool m_has_searched; + bool m_search_type_is_goto; + uint64_t m_last_progress_update_ts; + spy_text_renderer* m_text_renderer; +}; + +class curses_mainhelp_page +{ +public: + curses_mainhelp_page(sinsp_cursesui* parent); + ~curses_mainhelp_page(); + sysdig_table_action handle_input(int ch); + void render(); + +private: + + WINDOW* m_win; + sinsp_cursesui* m_parent; + ctext* m_ctext; +}; + +class curses_viewinfo_page +{ +public: + curses_viewinfo_page(sinsp_cursesui* parent, uint32_t viewnum, uint32_t starty, uint32_t startx, uint32_t h, uint32_t w); + ~curses_viewinfo_page(); + sysdig_table_action handle_input(int ch); + void render(); + +private: + + WINDOW* m_win; + sinsp_cursesui* m_parent; + ctext* m_ctext; +}; + +#endif // NOCURSESUI +#endif // CSYSDIG diff --git a/userspace/libsinsp/cursesspectro.cpp b/userspace/libsinsp/cursesspectro.cpp new file mode 100644 index 0000000000..6858bc954b --- /dev/null +++ b/userspace/libsinsp/cursesspectro.cpp @@ -0,0 +1,720 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include +#include +#include +#ifndef _WIN32 +#include +#include +#endif +#include +#include +#include +#include +#include +#include +using namespace std; + +#include "sinsp.h" +#include "sinsp_int.h" +#include "../../driver/ppm_ringbuffer.h" +#include "filter.h" +#include "filterchecks.h" + +#ifdef CSYSDIG +#ifndef NOCURSESUI + +#include +#include "table.h" +#include "ctext.h" +#include "cursescomponents.h" +#include "cursestable.h" +#include "cursesspectro.h" +#include "cursesui.h" + +// +// The color palette that we will use for the chart +// +uint32_t g_colpalette[] = +{ + 22, 28, 64, 34, 2, 76, 46, 118, 154, 191, 227, 226, 11, 220, 209, 208, 202, 197, 9, 1 +// 17, 18, 21, 26, 27, 32, 33, 38, 39, 45, 51, 87, 159, 195, 231, 7 +// 238, 241, 243, 245, 246, 247, 39, 38, 33, 32, 27, 26, 21, 18, 17 +// 236, 237, 238, 239, 240, 241, 242, 243, 244,245,246,247,248,249,250,251,252,253,254, 255, +// 236, 238, 240, 242, 243, 244,246,248,250,252,254,195,159,87,45, 39, 33, 27,21 +}; +uint32_t g_colpalette_size = sizeof(g_colpalette) / sizeof(g_colpalette[0]); + +/////////////////////////////////////////////////////////////////////////////// +// ANSI terminal helpers +/////////////////////////////////////////////////////////////////////////////// +inline void ansi_movedown(int n) +{ + printf("\033[%dE", n); +} + +inline void ansi_moveup(int n) +{ + printf("\033[%dF", n); +} + +inline void ansi_hidecursor() +{ + printf("\033[?25l"); +} + +inline void ansi_showcursor() +{ + printf("\033[?25h"); +} + +inline void ansi_moveto(uint32_t y, uint32_t x) +{ + printf("\033[%d;%dH", y, x); +} + +inline void ansi_clearline() +{ + printf("\033[2K"); +} + +inline void ansi_setcolor(int col) +{ + // Background + printf("\033[48;5;%dm", col); + + // Foreground + if(col != 0) + { + printf("\033[38;5;%dm", 0); + } + else + { + printf("\033[38;5;%dm", 255); + } +} + +inline void ansi_reset_color() +{ + printf("\033[0m"); +} + +inline void ansi_clearscreen() +{ + printf("\033[2J"); +} + +/////////////////////////////////////////////////////////////////////////////// +// curses_spectro implementation +/////////////////////////////////////////////////////////////////////////////// +curses_spectro::curses_spectro(sinsp_cursesui* parent, sinsp* inspector, bool is_tracer) +{ + m_tblwin = NULL; + m_data = NULL; + m_table = NULL; + m_table_x_start = 0; + m_table_y_start = TABLE_Y_START; + m_drilled_up = false; + m_selection_changed = false; + m_parent = parent; + m_inspector = inspector; + m_converter = new sinsp_filter_check_reference(); + m_n_flushes = 0; + m_n_flushes_with_data = 0; + m_mouse_masked = false; + m_lastx = -1; + m_lasty = -1; + m_selstart_x = -1; + m_selstart_y = -1; + m_prev_sel_x1 = -1; + m_prev_sel_x2 = -1; + m_prev_sel_y1 = -1; + m_prev_sel_y2 = -1; + m_scroll_paused = false; + m_is_tracer = is_tracer; + m_selecting = false; + + // + // Define the table size + // + m_w = m_parent->m_screenw; + m_h = m_parent->m_screenh; + + // + // Create the table window + // + refresh(); + m_tblwin = newwin(2, m_w, m_parent->m_screenh - 3, 0); + + // + // Put the inspector in offline replay mode + // + if(!m_inspector->is_live()) + { + parent->m_offline_replay = true; + } +/* + for(uint32_t j = 0; j < 256; j++) + { + ansi_setcolor(j); + printf("%d ", (int)j); + } + exit(0); +*/ +} + +curses_spectro::~curses_spectro() +{ + ansi_moveto(m_h, 0); + printf("\n"); + + + // + // Disable offline replay mode + // + if(!m_inspector->is_live()) + { + m_parent->m_offline_replay = false; + } + + if(m_tblwin) + { + delwin(m_tblwin); + } + + delete m_converter; +} + +void curses_spectro::configure(sinsp_table* table) +{ + uint32_t j; + + m_table = table; + + vector* legend = m_table->get_legend(); + + for(j = 1; j < legend->size(); j++) + { + curses_table_column_info ci; + ci.m_info = legend->at(j); + m_legend.push_back(ci); + } +} + +void curses_spectro::print_error(string wstr) +{ + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::FAILED_SEARCH]); + + mvwprintw(m_tblwin, + m_parent->m_screenh / 2, + m_parent->m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); +} + +void curses_spectro::update_data(vector* data, bool force_selection_change) +{ + m_data = data; +} + +uint32_t curses_spectro::mkcol(uint64_t val) +{ + uint32_t refresh_per_sec = 2; + uint32_t col = log10((int)val * refresh_per_sec + 1) / log10(1.6); + + if(col < 1) + { + col = 1; + } + + if(col > g_colpalette_size - 1) + { + col = g_colpalette_size - 1; + } + + return g_colpalette[col - 1]; +} + +void curses_spectro::draw_axis() +{ + uint64_t x = 0; + + while(true) + { + if(x >= m_w) + { + break; + } + + uint32_t curtime = (uint32_t)((double)x * 11 / m_w); + uint32_t prevtime = (uint32_t)(((double)x - 1) * 11 / m_w); + + if(x == 0 || curtime != prevtime) + { + uint64_t aval = (uint64_t)pow(10, curtime); + + m_converter->set_val(PT_RELTIME, + (uint8_t*)&aval, + 8, + 0, + ppm_print_format::PF_DEC); + + string tstr = m_converter->tostring_nice(NULL, 0, 1000000000); + printf("|%s", tstr.c_str()); + x += tstr.size() + 1; + } + else + { + printf(" "); + x++; + } + } +} + +void curses_spectro::draw_menu(bool there_is_more) +{ + printf("F1"); + ansi_setcolor(24); + printf("Help "); + ansi_reset_color(); + + printf("F2"); + ansi_setcolor(24); + printf("Views "); + ansi_reset_color(); + + printf("p "); + ansi_setcolor(24); + printf("Pause "); + ansi_reset_color(); + + printf("BKSPACE"); + ansi_setcolor(24); + printf("Back"); + ansi_reset_color(); + + printf("MOUSE"); + ansi_setcolor(24); + printf("DrillDown"); + ansi_reset_color(); + + if(there_is_more) + { + printf("SPACE"); + ansi_setcolor(24); + printf("More"); + ansi_reset_color(); + + } +} + +void curses_spectro::render(bool data_changed) +{ + // + // Clear the screen + // + if(m_data == NULL) + { + return; + } + + m_n_flushes++; + + if(m_data->size() != 0) + { + if(m_legend.size() != m_data->at(0).m_values.size()) + { + ASSERT(false); + throw sinsp_exception("corrupted curses table data"); + } + } + else + { + if(m_n_flushes < 2) + { + printf("\n"); + } + else + { + return; + } + } + + if(data_changed) + { + unordered_map freqs; + + m_n_flushes_with_data++; + + // + // Create a map with the frequencies for every latency interval + // + for(auto d : *m_data) + { + sinsp_table_field* key = &(d.m_values[0]); + sinsp_table_field* data = &(d.m_values[1]); + if(key->m_len != 8) + { + throw sinsp_exception("the key of a spectrogram view must be a number"); + } + + uint64_t val = *(uint64_t*)key->m_val; + freqs[val] = *(uint32_t*)data->m_val; + } + + ansi_moveto(m_h - 2, 0); + + // + // Render the line + // + m_t_row.clear(m_table->m_prev_flush_time_ns); + + for(uint32_t j = 0; j < m_w - 1; j++) + { + auto it = freqs.find(j); + + if(it != freqs.end()) + { + m_t_row.push_back(it->second); + uint32_t col = mkcol(it->second); + ansi_setcolor(col); + printf(" "); + } + else + { + m_t_row.push_back(0); + ansi_setcolor(0); + printf(" "); + } + } + + m_history.push_back(m_t_row); + + bool will_pause = !m_inspector->is_live() && m_n_flushes_with_data % (m_h - 3) == 0; + + ansi_reset_color(); + ansi_moveto(m_h - 1, 0); + draw_axis(); + ansi_moveto(m_h, 0); + draw_menu(will_pause); + printf("\n"); + + if(will_pause) + { + m_scroll_paused = true; + } + } +} + +// +// Return false if the user wants us to exit +// +sysdig_table_action curses_spectro::handle_input(int ch) +{ + if(m_data == NULL) + { + return STA_PARENT_HANDLE; + } + + switch(ch) + { + case KEY_F(2): + clear(); + return STA_PARENT_HANDLE; + case KEY_ENTER: + return STA_DRILLDOWN; + case KEY_BACKSPACE: + case 127: + return STA_DRILLUP; + case KEY_MOUSE: + { + if(!m_parent->m_is_mousedrag_available) + { + string msgstr = "Mouse input not supported in this terminal"; + uint32_t xs = (msgstr.size() >= m_w)? 0 : (m_w / 2) - (msgstr.size() / 2); + ansi_moveto(m_h / 2, xs); + printf("%s\n", msgstr.c_str()); + + return STA_NONE; + } +/* + if(!m_mouse_masked) + { + mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL); + m_mouse_masked = true; + } +*/ + if(getmouse(&m_last_mevent) == OK) + { + if(m_last_mevent.bstate & BUTTON1_CLICKED) + { + g_logger.format("mouse clicked"); + + if(m_last_mevent.y == (int)m_h - 2) + { + if(m_last_mevent.x >= 3 && m_last_mevent.x <= 7) + { + return m_parent->handle_input(KEY_F(1)); + } + else if(m_last_mevent.x >= 10 && m_last_mevent.x <= 15) + { + return m_parent->handle_input(KEY_F(2)); + } + else if(m_last_mevent.x >= 18 && m_last_mevent.x <= 23) + { + return m_parent->handle_input('p'); + } + else if(m_last_mevent.x >= 31 && m_last_mevent.x <= 34) + { + return STA_DRILLUP; + } + } + else + { + if(m_inspector->is_live()) + { + break; + } + + if(!m_selecting) + { + m_selecting = true; + m_selstart_x = -1; + m_selstart_y = -1; + } + else + { + m_selecting = false; + + curses_spectro_history_row* start_row = get_history_row_from_coordinate(m_selstart_y); + curses_spectro_history_row* end_row = get_history_row_from_coordinate(m_prev_sel_y2 - 1); + uint64_t start_latency = latency_from_coordinate(m_selstart_x); + uint64_t end_latency = latency_from_coordinate(m_prev_sel_x2); + + if(start_row == NULL || end_row == NULL) + { + break; + } + + string lat_fld_name; + + if(m_is_tracer) + { + lat_fld_name = "span.duration"; + } + else + { + lat_fld_name = "evt.latency"; + } + + m_selection_filter = + "(evt.rawtime>=" + to_string(start_row->m_ts - m_table->m_refresh_interval_ns) + + " and evt.rawtime<=" + to_string(end_row->m_ts) + + ") and (" + lat_fld_name + ">=" + to_string(start_latency) + + " and " + lat_fld_name + "<" + to_string(end_latency) + ")"; + + g_logger.format("spectrogram drill down"); + g_logger.format("filter: %s", m_selection_filter.c_str()); + + m_selstart_x = -1; + m_selstart_y = -1; + + ansi_reset_color(); + + if(m_is_tracer) + { + return STA_DRILLDOWN; + } + else + { + return STA_DIG; + } + } + } + } + else + { + if(m_selecting) + { + if((m_last_mevent.y > (int)m_h - 4) || ((int)m_last_mevent.y <= (int)m_h - 3 - (int)m_history.size())) + { + break; + } + + if(m_selstart_x == -1) + { + m_selstart_x = m_last_mevent.x; + m_selstart_y = m_last_mevent.y; + } + + if(m_prev_sel_x1 != -1) + { + draw_square(m_prev_sel_y1, m_prev_sel_x1, + m_prev_sel_y2, m_prev_sel_x2, + ' '); + } + + m_prev_sel_y1 = m_selstart_y; + m_prev_sel_x1 = m_selstart_x; + m_prev_sel_y2 = m_last_mevent.y + 1; + m_prev_sel_x2 = m_last_mevent.x + 1; + + draw_square(m_selstart_y, m_selstart_x, + m_last_mevent.y + 1, m_last_mevent.x + 1, + 'X'); + } + } + } + } + break; + case 'c': + case KEY_DC: // Del + break; + case KEY_F(7): + return STA_NONE; + default: + break; + } + + return STA_PARENT_HANDLE; +} + +void curses_spectro::draw_square(int32_t y1, int32_t x1, int32_t y2, int32_t x2, char c) +{ + if(x2 < x1 || y2 < y1) + { + return; + } + + for(int32_t j = y1; j < y2; j++) + { + ansi_moveto(j + 1, x1 + 1); + + if(j == y1 || j == y2 - 1) + { + for(int32_t k = x1; k < x2; k++) + { + int64_t col = get_history_color_from_coordinate(j, k); + if(col == -1) + { + break; + } + + ansi_setcolor(col); + printf("%c", c); + } + } + else + { + int64_t col = get_history_color_from_coordinate(j, x1); + if(col == -1) + { + break; + } + + ansi_setcolor(col); + printf("%c", c); + + col = get_history_color_from_coordinate(j, x2 - 1); + if(col == -1) + { + break; + } + + ansi_moveto(j + 1, x2); + ansi_setcolor(col); + printf("%c", c); + } + + printf("\n"); + } +} + +int64_t curses_spectro::get_history_value_from_coordinate(uint32_t y, uint32_t x) +{ + if((m_h - y > 3) && + (m_h - y - 4) < m_history.size() - 1) + { + curses_spectro_history_row& row = m_history[m_history.size() - 1 - (m_h - y - 4)]; + + ASSERT(x >= 0); + ASSERT(row.m_data.size() == m_w - 1); + if(x >= row.m_data.size()) + { + return -1; + } + + return row.m_data[x]; + } + + return -1; +} + +int64_t curses_spectro::get_history_color_from_coordinate(uint32_t y, uint32_t x) +{ + int64_t hv = get_history_value_from_coordinate(y, x); + + if(hv != -1) + { + if(hv == 0) + { + return 0; + } + else + { + int64_t col = mkcol(hv); + return col; + } + } + else + { + return -1; + } +} + +curses_spectro_history_row* curses_spectro::get_history_row_from_coordinate(uint32_t y) +{ + if((y <= m_h - 4) && ((int)y > (int)m_h - 3 - (int)m_history.size())) + { + return &(m_history[m_history.size() - 1 - (m_h - y - 4)]); + } + else + { + return NULL; + } +} + +uint64_t curses_spectro::latency_from_coordinate(uint32_t x) +{ + double curtime = (double)x * 11 / m_w; + return (uint64_t)pow(10, curtime); +} + +void curses_spectro::recreate_win(int h) +{ + delwin(m_tblwin); + + m_tblwin = newwin(m_h - 3, m_w, m_table_y_start, m_table_x_start); + render(true); +} + +#endif // NOCURSESUI +#endif // CSYSDIG diff --git a/userspace/libsinsp/cursesspectro.h b/userspace/libsinsp/cursesspectro.h new file mode 100644 index 0000000000..2691b86190 --- /dev/null +++ b/userspace/libsinsp/cursesspectro.h @@ -0,0 +1,138 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#ifdef CSYSDIG +#ifndef NOCURSESUI + +class colpalette_entry +{ +public: + colpalette_entry(int color, char ch) + { + m_color = color; + m_char = ch; + } + + int m_color; + char m_char; +}; + +class curses_spectro_history_row +{ +public: + void clear(uint64_t ts) + { + m_ts = ts; + m_data.clear(); + } + + void push_back(uint32_t val) + { + m_data.push_back(val); + } + + uint64_t m_ts; + vector m_data; +}; + +class curses_spectro : + public sinsp_chart +{ +public: + enum alignment + { + ALIGN_LEFT, + ALIGN_RIGHT, + }; + + curses_spectro(sinsp_cursesui* parent, sinsp* inspector, bool is_tracer); + ~curses_spectro(); + + void configure(sinsp_table* table); + void update_data(vector* data, bool force_selection_change = false); + void render(bool data_changed); + sysdig_table_action handle_input(int ch); + void set_x_start(uint32_t x) + { + m_table_x_start = x; + } + void recreate_win(int h); + uint32_t get_data_size() + { + if(m_table != NULL) + { + return m_data->size(); + } + else + { + return 0; + } + } + bool get_position(OUT int32_t* pos, OUT int32_t* totlines, OUT float* percent, OUT bool* truncated) + { + return false; + } + + sinsp_table_field_storage m_last_key; + bool m_drilled_up; + bool m_selection_changed; + MEVENT m_last_mevent; + string m_selection_filter; + bool m_scroll_paused; + +private: + void print_error(string wstr); + uint32_t mkcol(uint64_t n); + void draw_axis(); + void draw_menu(bool there_is_more); + int64_t get_history_value_from_coordinate(uint32_t y, uint32_t x); + int64_t get_history_color_from_coordinate(uint32_t y, uint32_t x); + curses_spectro_history_row* get_history_row_from_coordinate(uint32_t y); + uint64_t latency_from_coordinate(uint32_t x); + void draw_square(int32_t y1, int32_t x1, int32_t y2, int32_t x2, char c); + + sinsp* m_inspector; + WINDOW* m_tblwin; + sinsp_cursesui* m_parent; + sinsp_table* m_table; + int32_t m_table_x_start; + uint32_t m_table_y_start; + vector m_legend; + vector* m_data; + uint32_t m_w; + uint32_t m_h; + vector m_colpalette; + sinsp_filter_check_reference* m_converter; + uint64_t m_n_flushes; + uint64_t m_n_flushes_with_data; + vector m_history; + curses_spectro_history_row m_t_row; + bool m_mouse_masked; + int32_t m_lastx, m_lasty; + int32_t m_selstart_x, m_selstart_y; + int32_t m_prev_sel_x1, m_prev_sel_x2; + int32_t m_prev_sel_y1, m_prev_sel_y2; + bool m_is_tracer; + bool m_selecting; + + friend class curses_spectro_sidemenu; +}; + +#endif // NOCURSESUI +#endif // CSYSDIG diff --git a/userspace/libsinsp/cursestable.cpp b/userspace/libsinsp/cursestable.cpp new file mode 100644 index 0000000000..69c2aa32cc --- /dev/null +++ b/userspace/libsinsp/cursestable.cpp @@ -0,0 +1,894 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include +#include +#ifndef _WIN32 +#include +#endif +#include +#include +#include +#include +#include +#include +using namespace std; + +#include "sinsp.h" +#include "sinsp_int.h" +#include "../../driver/ppm_ringbuffer.h" +#include "filter.h" +#include "filterchecks.h" + +#ifdef CSYSDIG +#ifndef NOCURSESUI + +#include +#include "table.h" +#include "cursescomponents.h" +#include "cursestable.h" +#include "cursesui.h" + +/////////////////////////////////////////////////////////////////////////////// +// curses_table implementation +/////////////////////////////////////////////////////////////////////////////// +curses_table::curses_table(sinsp_cursesui* parent, sinsp* inspector, sinsp_table::tabletype type) +{ + m_tblwin = NULL; + m_data = NULL; + m_table = NULL; + m_table_x_start = 0; + m_table_y_start = TABLE_Y_START; + m_drilled_up = false; + m_selection_changed = false; + m_parent = parent; + m_inspector = inspector; + m_type = type; + + m_converter = new sinsp_filter_check_reference(); + + // + // Column sizes initialization + // + m_colsizes[PT_NONE] = 0; + m_colsizes[PT_INT8] = 8; + m_colsizes[PT_INT16] = 8; + m_colsizes[PT_INT32] = 8; + m_colsizes[PT_INT64] = 8; + m_colsizes[PT_UINT8] = 8; + m_colsizes[PT_UINT16] = 8; + m_colsizes[PT_UINT32] = 8; + m_colsizes[PT_UINT64] = 8; + m_colsizes[PT_CHARBUF] = 32; + m_colsizes[PT_BYTEBUF] = 32; + m_colsizes[PT_ERRNO] = 8; + m_colsizes[PT_SOCKADDR] = 16; + m_colsizes[PT_SOCKTUPLE] = 16; + m_colsizes[PT_FD] = 32; + m_colsizes[PT_PID] = 16; + m_colsizes[PT_FDLIST] = 16; + m_colsizes[PT_FSPATH] = 32; + m_colsizes[PT_SYSCALLID] = 8; + m_colsizes[PT_SIGTYPE] = 8; + m_colsizes[PT_RELTIME] = 16; + m_colsizes[PT_ABSTIME] = 16; + m_colsizes[PT_PORT] = 8; + m_colsizes[PT_L4PROTO] = 8; + m_colsizes[PT_SOCKFAMILY] = 8; + m_colsizes[PT_BOOL] = 8; + m_colsizes[PT_IPV4ADDR] = 8; + m_colsizes[PT_IPV6ADDR] = 16; + m_colsizes[PT_DYN] = 8; + m_colsizes[PT_FLAGS8] = 32; + m_colsizes[PT_FLAGS16] = 32; + m_colsizes[PT_FLAGS32] = 32; + m_colsizes[PT_MODE] = 32; + m_colsizes[PT_UID] = 12; + m_colsizes[PT_GID] = 12; + m_colsizes[PT_DOUBLE] = 8; + m_colsizes[PT_SIGSET] = 32; + m_colsizes[PT_FSRELPATH] = 32; + + // + // Define the table size + // + m_w = TABLE_WIDTH; + m_h = m_parent->m_screenh - 3; + m_scrolloff_x = 0; + + // + // Create the table window + // + refresh(); + m_tblwin = newwin(m_h, m_w, m_table_y_start, 0); +} + +curses_table::~curses_table() +{ + if(m_tblwin) + { + delwin(m_tblwin); + } + + delete m_converter; +} + +void curses_table::configure(sinsp_table* table, + vector* colsizes, vector* colnames) +{ + uint32_t j; + + m_table = table; + + vector* legend = m_table->get_legend(); + + if(colsizes) + { + if(colsizes->size() != 0 && colsizes->size() != legend->size()) + { + throw sinsp_exception("invalid table legend for view " + m_parent->m_views.at(m_parent->m_selected_view)->m_name + + " : column sizes doesn't match (" + + to_string(colsizes->size()) + " column sizes, " + + to_string(legend->size()) + " entries in legend)"); + } + } + + if(colnames) + { + if(colnames->size() != 0 && colnames->size() != legend->size()) + { + throw sinsp_exception("invalid table legend for view " + m_parent->m_views.at(m_parent->m_selected_view)->m_name + + " : column names doesn't match (" + + to_string(colnames->size()) + " column names, " + + to_string(legend->size()) + " entries in legend)"); + } + } + + for(j = 1; j < legend->size(); j++) + { + curses_table_column_info ci; + ci.m_info = legend->at(j); + + if(colsizes->size() == 0 || colsizes->at(j) == -1) + { + ci.m_size = m_colsizes[legend->at(j).m_type]; + } + else + { + ci.m_size = colsizes->at(j); + } + + if(colnames->size() == 0) + { + ci.m_name = ci.m_info.m_name; + } + else + { + ci.m_name = colnames->at(j); + } + +/* + int32_t namelen = strlen(ci.m_info.m_name); + + if(ci.m_size < namelen + 1) + { + ci.m_size = namelen + 1; + } +*/ + m_legend.push_back(ci); + } +} + +void curses_table::update_rowkey(int32_t row) +{ + sinsp_table_field* rowkey = m_table->get_row_key(row); + + if(rowkey != NULL) + { + m_last_key.copy(rowkey); + m_last_key.m_isvalid = true; + } + else + { + m_last_key.m_isvalid = false; + } +} + +void curses_table::update_data(vector* data, bool force_selection_change) +{ + m_data = data; + + if(m_selection_changed && (m_last_key.m_isvalid || m_drilled_up || force_selection_change)) + { + int32_t selct = m_table->get_row_from_key(&m_last_key); + if(selct == -1) + { + m_selct--; + m_last_key.m_isvalid = false; + } + else + { + m_selct = selct; + +// if(m_drilled_up) + { + selection_goto((int32_t)m_data->size(), m_selct); + } + + render(true); +//m_drilled_up = false; + } + + sanitize_selection((int32_t)m_data->size()); + } + else + { + update_rowkey(m_selct); + } +} + +void curses_table::print_line_centered(string line, int32_t off) +{ + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PROCESS]); + + if(line.size() < m_parent->m_screenw) + { + mvwprintw(m_tblwin, + m_parent->m_screenh / 2 + off, + m_parent->m_screenw / 2 - line.size() / 2, + line.c_str()); + } + else + { + uint32_t spos = 0; + + for(uint32_t j = 0;; j++) + { + string ss = line.substr(spos, spos + m_parent->m_screenw); +glogf("2, %d %s\n", spos, ss.c_str()); + + mvwprintw(m_tblwin, + m_parent->m_screenh / 2 + off + j, + 0, + ss.c_str()); + + spos += m_parent->m_screenw; + if(spos >= line.size()) + { + break; + } + } + } +} + +void curses_table::print_wait() +{ + string wstr; + bool is_tracer_view = false; + + sinsp_view_info* vinfo = m_parent->get_selected_view(); + if(vinfo) + { + if(vinfo->m_id == "tracers" || + vinfo->m_id == "tracer_ids") + { + is_tracer_view = true; + } + } + else + { + ASSERT(false); + } + + if(is_tracer_view) + { + print_line_centered("No data for this view."); + print_line_centered("Note: in order to see any data here, you need to push tracers to sysdig from your app as described here: XXX.", 2); + } + else + { + if(m_inspector->is_live()) + { + wstr = "Collecting Data"; + } + else + { + if(m_parent->is_eof()) + { + wstr = "No Data For This View"; + } + } + + print_line_centered(wstr); + } +} + +void curses_table::print_error(string wstr) +{ + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::FAILED_SEARCH]); + + mvwprintw(m_tblwin, + m_parent->m_screenh / 2, + m_parent->m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); +} + +void curses_table::render(bool data_changed) +{ + uint32_t j, k; + int32_t l, m; + wclear(m_tblwin); + + // + // Clear the screen + // + if(m_data == NULL) + { + print_wait(); + goto render_end; + } + + if(m_data->size() != 0) + { + if(m_legend.size() != m_data->at(0).m_values.size()) + { + ASSERT(false); + throw sinsp_exception("corrupted curses table data"); + } + } + + if(data_changed) + { + m_column_startx.clear(); + + if(m_selct < 0) + { + m_selct = 0; + } + else if(m_selct > (int32_t)m_data->size() - 1) + { + m_selct = (int32_t)m_data->size() - 1; + } + + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_FOCUS]); + + // + // Render the column headers + // + wmove(m_tblwin, 0, 0); + + for(j = 0; j < m_w; j++) + { + if(m_type == sinsp_table::TT_TABLE) + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_FOCUS]); + } + else + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_LIST_FOCUS]); + } + + waddch(m_tblwin, ' '); + } + + for(j = 0, k = 0; j < m_legend.size(); j++) + { + if(j == m_table->get_sorting_col() - 1) + { + if(m_type == sinsp_table::TT_TABLE) + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HIGHLIGHT_FOCUS]); + } + else + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_LIST_HIGHLIGHT]); + } + } + else + { + if(m_type == sinsp_table::TT_TABLE) + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_FOCUS]); + } + else + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HEADER_LIST_FOCUS]); + } + } + + m_column_startx.push_back(k); + + string coltext = m_legend[j].m_name; + if((int32_t)coltext.size() > m_legend[j].m_size - 1) + { + coltext = coltext.substr(0, m_legend[j].m_size - 1); + } + + uint32_t tindex = m_table->m_do_merging? j + 2 : j + 1; + + curses_table::alignment al = get_field_alignment(m_table->m_types->at(tindex)); + if(al == curses_table::ALIGN_RIGHT) + { + coltext.insert(0, m_legend[j].m_size - coltext.size() - 1, ' '); + } + + mvwaddnstr(m_tblwin, 0, k, coltext.c_str(), m_legend[j].m_size - 1); + + for(l = strlen(m_legend[j].m_name.c_str()); l < m_legend[j].m_size; l++) + { + waddch(m_tblwin, ' '); + } + + k += m_legend[j].m_size; + } + + // + // If there is no data, print the "waiting for data" message + // + if(m_data->size() == 0) + { + if(!m_parent->is_searching()) + { + print_wait(); + goto render_end; + } + } + + // + // Render the rows + // + vector* row; + + for(l = 0; l < (int32_t)MIN(m_data->size(), m_h - 1); l++) + { + if(l + m_firstrow >= (int32_t)m_data->size()) + { + break; + } + + row = &(m_data->at(l + m_firstrow).m_values); + + // + // Pick the proper color based on the selection + // + if(l == m_selct - (int32_t)m_firstrow) + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PANEL_HIGHLIGHT_FOCUS]); + } + else + { + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PROCESS]); + } + + // + // Render the row + // + wmove(m_tblwin, l + 1, 0); + for(j = 0; j < m_w; j++) + { + waddch(m_tblwin, ' '); + } + + for(j = 0, k = 0; j < m_legend.size(); j++) + { + sinsp_filter_check* extractor = m_table->m_extractors->at(j + 1); + uint64_t td = 0; + + if(extractor->m_aggregation == A_TIME_AVG || + extractor->m_merge_aggregation == A_TIME_AVG) + { + td = m_parent->get_time_delta(); + } + + m_converter->set_val(m_legend[j].m_info.m_type, + row->at(j).m_val, + row->at(j).m_len, + row->at(j).m_cnt, + m_legend[j].m_info.m_print_format); + + uint32_t size = m_legend[j].m_size - 1; + + // + // size=0 means "use the whole available space" + // + if(size == 0) + { + size = m_w - k - 1; + } + + mvwaddnstr(m_tblwin, + l + 1, + k, + m_converter->tostring_nice(NULL, size, td), + size); + + k += m_legend[j].m_size; + } + } + + wattrset(m_tblwin, m_parent->m_colors[sinsp_cursesui::PROCESS]); + + if(l < (int32_t)m_h - 1) + { + for(m = l; m < (int32_t)m_h - 1; m++) + { + wmove(m_tblwin, m + 1, 0); + + for(j = 0; j < m_w; j++) + { + waddch(m_tblwin, ' '); + } + } + } + } + +render_end: + if(m_data && m_data->size() == 0) + { + if(m_parent->is_searching()) + { + print_error(string(" NO MATCH ")); + } + } + + if(m_parent->m_search_nomatch) + { + print_error(string(" NOT FOUND ")); + } + + if(m_scrolloff_x != 0) + { + chtype chstr[m_w]; + for(j = 0; j < m_h; j++) + { + mvwinchnstr(m_tblwin, j, 0, chstr, m_parent->m_screenw + m_scrolloff_x); + mvwaddchnstr(m_tblwin, j, 0, chstr + m_scrolloff_x, m_parent->m_screenw); + } + } + + wrefresh(m_tblwin); + m_parent->render(); + refresh(); +} + +string curses_table::get_field_val(string fldname) +{ + uint32_t j; + vector* row; + string res; + + row = &(m_data->at(m_selct).m_values); + + vector* legend; + + if(m_parent->m_datatable->m_postmerge_legend.size() != 0) + { + legend = &m_parent->m_datatable->m_postmerge_legend; + } + else + { + legend = &m_parent->m_datatable->m_premerge_legend; + } + + for(j = 1; j < legend->size(); j++) + { + auto le = legend->at(j); + + if(le.m_name == fldname) + { + uint32_t k = j - 1; + m_converter->set_val(m_legend[k].m_info.m_type, + row->at(k).m_val, + row->at(k).m_len, + row->at(k).m_cnt, + m_legend[k].m_info.m_print_format); + + res = m_converter->tostring_nice(NULL, 0, 0); + + break; + } + } + + if(j == legend->size()) + { + throw sinsp_exception("field '" + fldname + "'' not found in this view"); + } + + return res; +} + +// +// Return false if the user wants us to exit +// +sysdig_table_action curses_table::handle_input(int ch) +{ + if(m_data == NULL) + { + return STA_PARENT_HANDLE; + } + + + switch(ch) + { + case KEY_LEFT: + if(m_scrolloff_x > 0) + { + m_scrolloff_x -= 4; + render(true); + } + break; + case KEY_RIGHT: + if(m_scrolloff_x < m_w - m_parent->m_screenw) + { + m_scrolloff_x += 4; + render(true); + } + break; + case KEY_UP: + m_selection_changed = true; + selection_up((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + break; + case KEY_DOWN: + m_selection_changed = true; + selection_down((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + break; + case KEY_PPAGE: + m_selection_changed = true; + selection_pageup((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + break; + case KEY_NPAGE: + m_selection_changed = true; + selection_pagedown((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + break; + case KEY_HOME: + m_selection_changed = true; + selection_home((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + break; + case KEY_END: + m_selection_changed = true; + selection_end((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + break; + case '\n': + case '\r': + case KEY_ENTER: + return STA_DRILLDOWN; + case KEY_F(12): + return STA_SPECTRO; + case 288: + return STA_SPECTRO_FILE; + case KEY_BACKSPACE: + case 127: + return STA_DRILLUP; + case KEY_MOUSE: + { + uint32_t j; + + if(getmouse(&m_last_mevent) == OK) + { + if(m_last_mevent.bstate & BUTTON1_CLICKED) + { + // + // Bottom menu clicks are handled by the parent + // + if((uint32_t)m_last_mevent.y == m_parent->m_screenh - 1) + { + return STA_PARENT_HANDLE; + } + + ASSERT((m_data->size() == 0) || (m_column_startx.size() == m_data->at(0).m_values.size())); + + if((uint32_t)m_last_mevent.y == m_table_y_start) + { + // + // This is a click on a column header. Change the sorting accordingly. + // + for(j = 0; j < m_column_startx.size() - 1; j++) + { + if((uint32_t)m_last_mevent.x >= m_column_startx[j] && (uint32_t)m_last_mevent.x < m_column_startx[j + 1]) + { + m_table->set_sorting_col(j + 1); + break; + } + } + + if(j == m_column_startx.size() - 1) + { + m_table->set_sorting_col(j + 1); + } + + m_table->sort_sample(); + update_data(m_data); + render(true); + } + else if((uint32_t)m_last_mevent.y > m_table_y_start && + (uint32_t)m_last_mevent.y < m_table_y_start + m_h - 1) + { + // + // This is a click on a row. Update the selection. + // + m_selection_changed = true; + m_selct = m_firstrow + (m_last_mevent.y - m_table_y_start - 1); + sanitize_selection((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + } + } + else if(m_last_mevent.bstate & BUTTON1_DOUBLE_CLICKED) + { + if((uint32_t)m_last_mevent.y > m_table_y_start && + (uint32_t)m_last_mevent.y < m_table_y_start + m_h - 1) + { + // + // Update the selection + // + m_selection_changed = true; + m_selct = m_firstrow + (m_last_mevent.y - m_table_y_start - 1); + sanitize_selection((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + + // + // This delay is here just as a lazy way to give the user the + // feeling that the row has been clicked + // + usleep(200000); + + // + // Let the ui manager know that a drill down needs to happen + // + return STA_DRILLDOWN; + } + } + } + } + break; + case 'c': + case KEY_DC: + if(m_type == sinsp_table::TT_LIST) + { + m_table->clear(); + render(true); + m_lastrow_selected = true; + } + break; + default: + break; + } + + // + // Check if this view has any action configured, and if yes find if this key + // is one of the view hotkeys + // + sinsp_view_info* vinfo = m_parent->get_selected_view(); + + for(auto hk : vinfo->m_actions) + { + if(hk.m_hotkey == ch) + { + m_parent->run_action(&hk); + return STA_NONE; + } + } + + for(uint32_t i = 0; i < vinfo->max_col_sort_hotkeys; i++) + { + if(vinfo->m_col_sort_hotkeys[i] == ch) + { + if(i < vinfo->m_columns.size()) + { + m_table->set_sorting_col(i + 1); + m_table->sort_sample(); + update_data(m_data); + set_x_start(0); + recreate_win(m_parent->m_screenh - 3); + render(true); + break; + } + } + } + return STA_PARENT_HANDLE; +} + +curses_table::alignment curses_table::get_field_alignment(ppm_param_type type) +{ + switch(type) + { + case PT_INT8: + case PT_INT16: + case PT_INT32: + case PT_INT64: + case PT_UINT8: + case PT_UINT16: + case PT_UINT32: + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + case PT_DOUBLE: + return ALIGN_RIGHT; + default: + return ALIGN_LEFT; + } +} + +void curses_table::recreate_win(int h) +{ + delwin(m_tblwin); + + if(h != 0) + { + m_h = h; + } + + m_tblwin = newwin(m_h, m_w, m_table_y_start, m_table_x_start); + render(true); +} + +void curses_table::goto_row(int32_t row) +{ + m_selection_changed = true; + selection_goto((int32_t)m_data->size(), row); + update_rowkey(row); + render(true); +} + +bool curses_table::get_position(OUT int32_t* pos, + OUT int32_t* totlines, + OUT float* percent, + OUT bool* truncated) +{ + if(m_data == NULL || m_data->size() == 0) + { + return false; + } + + *pos = m_selct + 1; + *totlines = (int32_t)m_data->size(); + *percent = (float)(m_selct + 1) / (float)m_data->size(); + *truncated = false; + + return true; +} + +void curses_table::follow_end() +{ + if(m_lastrow_selected) + { + m_selection_changed = true; + selection_end((int32_t)m_data->size()); + update_rowkey(m_selct); + render(true); + } +} + +#endif // NOCURSESUI +#endif // CSYSDIG diff --git a/userspace/libsinsp/cursestable.h b/userspace/libsinsp/cursestable.h new file mode 100644 index 0000000000..a8fe4bb0b9 --- /dev/null +++ b/userspace/libsinsp/cursestable.h @@ -0,0 +1,95 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#ifdef CSYSDIG +#ifndef NOCURSESUI + +class curses_table : + public curses_scrollable_list, + public sinsp_chart +{ +public: + enum alignment + { + ALIGN_LEFT, + ALIGN_RIGHT, + }; + + curses_table(sinsp_cursesui* parent, sinsp* inspector, sinsp_table::tabletype type); + ~curses_table(); + + void configure(sinsp_table* table, + vector* colsizes, vector* colnames); + void update_data(vector* data, bool force_selection_change = false); + void render(bool data_changed); + sysdig_table_action handle_input(int ch); + void set_x_start(uint32_t x) + { + m_table_x_start = x; + } + void recreate_win(int h); + void update_rowkey(int32_t row); + void goto_row(int32_t row); + bool get_position(OUT int32_t* pos, OUT int32_t* totlines, OUT float* percent, OUT bool* truncated); + void follow_end(); + string get_field_val(string fldname); + uint32_t get_data_size() + { + if(m_table != NULL) + { + return m_data->size(); + } + else + { + return 0; + } + } + + sinsp_table_field_storage m_last_key; + bool m_drilled_up; + bool m_selection_changed; + MEVENT m_last_mevent; + +private: + alignment get_field_alignment(ppm_param_type type); + void print_error(string wstr); + void print_wait(); + void print_line_centered(string line, int32_t off = 0); + + sinsp* m_inspector; + WINDOW* m_tblwin; + sinsp_cursesui* m_parent; + sinsp_table* m_table; + int32_t m_table_x_start; + uint32_t m_table_y_start; + uint32_t m_scrolloff_x; + uint32_t m_colsizes[PT_MAX]; + vector m_legend; + vector* m_data; + sinsp_filter_check_reference* m_converter; + vector m_column_startx; + char alignbuf[64]; + sinsp_table::tabletype m_type; + + friend class curses_table_sidemenu; + friend class sinsp_cursesui; // for access to m_data in sinsp_cursesui::handle_input +}; + +#endif // NOCURSESUI +#endif // CSYSDIG diff --git a/userspace/libsinsp/cursesui.cpp b/userspace/libsinsp/cursesui.cpp new file mode 100644 index 0000000000..1eeb086420 --- /dev/null +++ b/userspace/libsinsp/cursesui.cpp @@ -0,0 +1,3495 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include "sinsp.h" +#include "sinsp_int.h" +#include "../../driver/ppm_ringbuffer.h" +#include "filter.h" +#include "filterchecks.h" + +#ifdef CSYSDIG + +#ifndef _WIN32 +#include +#else +#include +#define getch _getch +#endif +#include "table.h" +#include "cursescomponents.h" +#include "cursestable.h" +#include "cursesspectro.h" +#include "ctext.h" +#include "cursesui.h" + +extern int32_t g_csysdig_screen_w; +extern bool g_filterchecks_force_raw_times; + +#ifndef NOCURSESUI +#define ColorPair(i,j) COLOR_PAIR((7-i)*8+j) +#endif + +#ifndef _WIN32 +static int do_sleep(useconds_t usec) +{ + return usleep(usec); +} +#else +int do_sleep(DWORD usec) +{ + ASSERT(usec >= 1000); + Sleep(DWORD(usec / 1000)); + return 0; +} +#endif + +/////////////////////////////////////////////////////////////////////////////// +// json_spy_renderer implementation +/////////////////////////////////////////////////////////////////////////////// +json_spy_renderer::json_spy_renderer(sinsp* inspector, + sinsp_cursesui* parent, + int32_t viz_type, + spy_text_renderer::sysdig_output_type sotype, + bool print_containers, + sinsp_evt::param_fmt text_fmt) +{ + m_inspector = inspector; + m_filter = NULL; + m_root = Json::Value(Json::arrayValue); + m_linecnt = 0; + + m_json_spy_renderer = new spy_text_renderer(inspector, + parent, + viz_type, + sotype, + print_containers, + text_fmt); +} + +json_spy_renderer::~json_spy_renderer() +{ + delete m_json_spy_renderer; + + if(m_filter != NULL) + { + delete m_filter; + } +} + +void json_spy_renderer::set_filter(string filter) +{ + if(filter != "") + { + sinsp_filter_compiler compiler(m_inspector, filter); + m_filter = compiler.compile(); + } +} + +void json_spy_renderer::process_event_spy(sinsp_evt* evt, int32_t next_res) +{ + int64_t len; + const char* argstr = m_json_spy_renderer->process_event_spy(evt, &len); + + if(argstr != NULL) + { + Json::Value line; + m_linecnt++; + + uint64_t ts = evt->get_ts(); + line["ta"] = to_string(ts); + line["td"] = to_string(ts - m_inspector->m_firstevent_ts); + + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & EF_READS_FROM_FD) + { + line["d"] = "<"; + } + else if(eflags & EF_WRITES_TO_FD) + { + line["d"] = ">"; + } + + line["v"] = argstr; + line["l"] = to_string(len); + + string fdname = evt->get_fd_info()->m_name; + string tc; + tc.push_back(evt->get_fd_info()->get_typechar()); + int64_t fdnum = evt->get_fd_num(); + + line["fd"] = to_string(fdnum); + line["ft"] = string(tc); + + if(fdname != "") + { + sanitize_string(fdname); + line["f"] = to_string(fdnum) + "(<" + string(tc) + ">" + fdname + ")"; + line["fn"] = fdname; + } + else + { + line["f"] = to_string(fdnum) + "(<" + string(tc) + ">)"; + } + + sinsp_threadinfo* tinfo = evt->get_thread_info(); + ASSERT(tinfo); + + line["p"] = tinfo->m_comm; + + if(!tinfo->m_container_id.empty()) + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(container_info) + { + if(!container_info->m_name.empty()) + { + line["c"] = container_info->m_name; + } + } + } + + m_root.append(line); + } +} + +void json_spy_renderer::process_event_dig(sinsp_evt* evt, int32_t next_res) +{ + if(!m_inspector->is_debug_enabled() && evt->get_category() & EC_INTERNAL) + { + return; + } + + string line; + + m_json_spy_renderer->m_formatter->tostring(evt, &line); + m_root.append(line); + m_linecnt++; +} + +void json_spy_renderer::process_event(sinsp_evt* evt, int32_t next_res) +{ + // + // Filter the event + // + if(m_filter) + { + if(!m_filter->run(evt)) + { + return; + } + } + + // + // Render the output + // + if(m_json_spy_renderer->m_viz_type == VIEW_ID_SPY) + { + process_event_spy(evt, next_res); + } + else + { + process_event_dig(evt, next_res); + } +} + +string json_spy_renderer::get_data() +{ + Json::FastWriter writer; + + string res = writer.write(m_root); + + m_root.clear(); + + return res; +} + +uint64_t json_spy_renderer::get_count() +{ + return m_linecnt; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_cursesui implementation +/////////////////////////////////////////////////////////////////////////////// +sinsp_cursesui::sinsp_cursesui(sinsp* inspector, + string event_source_name, + string cmdline_capture_filter, + uint64_t refresh_interval_ns, + bool print_containers, + sinsp_table::output_type output_type, + bool is_mousedrag_available, + int32_t json_first_row, int32_t json_last_row, + int32_t sorting_col, + sinsp_evt::param_fmt json_spy_text_fmt) +{ + m_inspector = inspector; + m_event_source_name = event_source_name; + m_selected_view = 0; + m_prev_selected_view = 0; + m_selected_view_sidemenu_entry = 0; + m_selected_action_sidemenu_entry = 0; + m_datatable = NULL; + m_cmdline_capture_filter = cmdline_capture_filter; + m_paused = false; + m_last_input_check_ts = 0; + m_output_filtering = false; + m_output_searching = false; + m_is_filter_sysdig = false; + m_eof = 0; + m_offline_replay = false; + m_last_progress_evt = 0; + m_input_check_period_ns = UI_USER_INPUT_CHECK_PERIOD_NS; + m_search_nomatch = false; + m_chart = NULL; + m_n_evts_in_file = 0; + m_1st_evt_ts = 0; + m_last_evt_ts = 0; + m_evt_ts_delta = 0; + m_timedelta_formatter = new sinsp_filter_check_reference(); + m_refresh_interval_ns = refresh_interval_ns; + m_print_containers = print_containers; + m_output_type = output_type; + m_truncated_input = false; + m_view_depth = 0; + m_interactive = false; + m_json_first_row = json_first_row; + m_json_last_row = json_last_row; + m_sorting_col = sorting_col; + m_json_spy_renderer = NULL; + m_json_spy_text_fmt = json_spy_text_fmt; + + if(output_type == sinsp_table::OT_JSON) + { + g_filterchecks_force_raw_times = true; + } + +#ifndef NOCURSESUI + m_viz = NULL; + m_spectro = NULL; + m_spybox_text_format = sinsp_evt::PF_NORMAL; + m_view_sidemenu = NULL; + m_action_sidemenu = NULL; + m_spy_box = NULL; + m_search_caller_interface = NULL; + m_viewinfo_page = NULL; + m_mainhelp_page = NULL; + m_is_mousedrag_available = is_mousedrag_available; + + for (int i = 0; i < 8; i++) + { + for (int j = 0; j < 8; j++) + { + init_pair((7-i)*8+j, i, (j==0?-1:j)); + } + } + + m_view_sort_sidemenu = NULL; + m_selected_view_sort_sidemenu_entry = 0; + + if(output_type == sinsp_table::OT_CURSES) + { + // + // Colors initialization + // + m_colors[RESET_COLOR] = ColorPair(COLOR_WHITE,COLOR_BLACK); + m_colors[DEFAULT_COLOR] = ColorPair(COLOR_WHITE,COLOR_BLACK); + m_colors[FUNCTION_BAR] = ColorPair(COLOR_BLACK,COLOR_YELLOW); + m_colors[FUNCTION_KEY] = ColorPair( COLOR_WHITE,COLOR_BLACK); + m_colors[PANEL_HEADER_FOCUS] = ColorPair(COLOR_BLACK,COLOR_GREEN); + m_colors[PANEL_HEADER_UNFOCUS] = ColorPair(COLOR_BLACK,COLOR_GREEN); + m_colors[PANEL_HIGHLIGHT_FOCUS] = ColorPair(COLOR_BLACK,COLOR_CYAN); + m_colors[PANEL_HIGHLIGHT_UNFOCUS] = ColorPair(COLOR_BLACK, COLOR_WHITE); + m_colors[PANEL_HEADER_LIST_FOCUS] = ColorPair(COLOR_BLACK,COLOR_YELLOW); + m_colors[PANEL_HEADER_LIST_HIGHLIGHT] = ColorPair(COLOR_BLACK,COLOR_GREEN); + m_colors[FAILED_SEARCH] = ColorPair(COLOR_RED,COLOR_CYAN); + m_colors[UPTIME] = A_BOLD | ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[BATTERY] = A_BOLD | ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[LARGE_NUMBER] = A_BOLD | ColorPair(COLOR_RED,COLOR_BLACK); + m_colors[METER_TEXT] = ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[METER_VALUE] = A_BOLD | ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[LED_COLOR] = ColorPair(COLOR_GREEN,COLOR_BLACK); + m_colors[TASKS_RUNNING] = A_BOLD | ColorPair(COLOR_GREEN,COLOR_BLACK); + m_colors[PROCESS] = A_NORMAL; + m_colors[PROCESS_SHADOW] = A_BOLD | ColorPair(COLOR_BLACK,COLOR_BLACK); + m_colors[PROCESS_TAG] = A_BOLD | ColorPair(COLOR_YELLOW,COLOR_BLACK); + m_colors[PROCESS_MEGABYTES] = ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[PROCESS_BASENAME] = A_BOLD | ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[PROCESS_TREE] = ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[PROCESS_R_STATE] = ColorPair(COLOR_GREEN,COLOR_BLACK); + m_colors[PROCESS_D_STATE] = A_BOLD | ColorPair(COLOR_RED,COLOR_BLACK); + m_colors[PROCESS_HIGH_PRIORITY] = ColorPair(COLOR_RED,COLOR_BLACK); + m_colors[PROCESS_LOW_PRIORITY] = ColorPair(COLOR_RED,COLOR_BLACK); + m_colors[PROCESS_THREAD] = ColorPair(COLOR_GREEN,COLOR_BLACK); + m_colors[PROCESS_THREAD_BASENAME] = A_BOLD | ColorPair(COLOR_GREEN,COLOR_BLACK); + m_colors[BAR_BORDER] = A_BOLD; + m_colors[BAR_SHADOW] = A_BOLD | ColorPair(COLOR_BLACK,COLOR_BLACK); + m_colors[SWAP] = ColorPair(COLOR_RED,COLOR_BLACK); + m_colors[GRAPH_BLACK] = ColorPair(COLOR_BLACK,COLOR_BLACK); + m_colors[GRAPH_WHITE] = ColorPair(COLOR_WHITE,COLOR_WHITE); + m_colors[GRAPH_WHITE_D] = ColorPair(COLOR_GREEN,COLOR_WHITE); + m_colors[GRAPH_GREEN_L] = ColorPair(COLOR_WHITE,COLOR_GREEN); + m_colors[GRAPH_GREEN] = ColorPair(COLOR_WHITE,COLOR_GREEN); + m_colors[GRAPH_GREEN_D] = ColorPair(COLOR_YELLOW,COLOR_GREEN); + m_colors[GRAPH_YELLOW_L] = ColorPair(COLOR_GREEN,COLOR_YELLOW); + m_colors[GRAPH_YELLOW] = ColorPair(COLOR_WHITE,COLOR_YELLOW); + m_colors[GRAPH_YELLOW_D] = ColorPair(COLOR_RED,COLOR_YELLOW); + m_colors[GRAPH_RED_L] = ColorPair(COLOR_YELLOW,COLOR_RED); + m_colors[GRAPH_RED] = ColorPair(COLOR_WHITE,COLOR_RED); + m_colors[GRAPH_RED_D] = ColorPair(COLOR_MAGENTA,COLOR_RED); + m_colors[GRAPH_MAGENTA_L] = ColorPair(COLOR_RED,COLOR_MAGENTA); + m_colors[GRAPH_MAGENTA] = ColorPair(COLOR_MAGENTA,COLOR_MAGENTA); + m_colors[MEMORY_USED] = ColorPair(COLOR_GREEN,COLOR_BLACK); + m_colors[MEMORY_BUFFERS] = ColorPair(COLOR_BLUE,COLOR_BLACK); + m_colors[MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(COLOR_BLUE,COLOR_BLACK); + m_colors[MEMORY_CACHE] = ColorPair(COLOR_YELLOW,COLOR_BLACK); + m_colors[LOAD_AVERAGE_FIFTEEN] = A_BOLD | ColorPair(COLOR_BLACK,COLOR_BLACK); + m_colors[LOAD_AVERAGE_FIVE] = A_NORMAL; + m_colors[LOAD_AVERAGE_ONE] = A_BOLD; + m_colors[LOAD] = A_BOLD; + m_colors[HELP_BOLD] = A_BOLD | ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[CLOCK] = A_BOLD; + m_colors[CHECK_BOX] = ColorPair(COLOR_CYAN,COLOR_BLACK); + m_colors[CHECK_MARK] = A_BOLD; + m_colors[CHECK_TEXT] = A_NORMAL; + m_colors[HOSTNAME] = A_BOLD; + m_colors[CPU_NICE] = ColorPair(COLOR_BLUE,COLOR_BLACK); + m_colors[CPU_NICE_TEXT] = A_BOLD | ColorPair(COLOR_BLUE,COLOR_BLACK); + m_colors[CPU_NORMAL] = ColorPair(COLOR_GREEN,COLOR_BLACK); + m_colors[CPU_KERNEL] = ColorPair(COLOR_RED,COLOR_BLACK); + m_colors[CPU_IOWAIT] = A_BOLD | ColorPair(COLOR_BLACK, COLOR_BLACK); + m_colors[CPU_IRQ] = ColorPair(COLOR_YELLOW,COLOR_BLACK); + m_colors[CPU_SOFTIRQ] = ColorPair(COLOR_MAGENTA,COLOR_BLACK); + m_colors[SPY_READ] = ColorPair(COLOR_RED,COLOR_BLACK); + m_colors[SPY_WRITE] = ColorPair(COLOR_CYAN,COLOR_BLACK); + + // + // Populate the main menu entries + // + m_menuitems.push_back(sinsp_menuitem_info("F1", "Help", sinsp_menuitem_info::ALL, KEY_F(1))); + m_menuitems.push_back(sinsp_menuitem_info("F2", "Views", sinsp_menuitem_info::ALL, KEY_F(2))); + m_menuitems.push_back(sinsp_menuitem_info("F4", "Filter", sinsp_menuitem_info::ALL, KEY_F(4))); + m_menuitems.push_back(sinsp_menuitem_info("F5", "Echo", sinsp_menuitem_info::TABLE, KEY_F(5))); + m_menuitems.push_back(sinsp_menuitem_info("F6", "Dig", sinsp_menuitem_info::TABLE, KEY_F(6))); + m_menuitems.push_back(sinsp_menuitem_info("F7", "Legend", sinsp_menuitem_info::ALL, KEY_F(7))); + m_menuitems.push_back(sinsp_menuitem_info("F8", "Actions", sinsp_menuitem_info::ALL, KEY_F(8))); + m_menuitems.push_back(sinsp_menuitem_info("F9", "Sort", sinsp_menuitem_info::ALL, KEY_F(9))); + m_menuitems.push_back(sinsp_menuitem_info("F12", "Spectro", sinsp_menuitem_info::ALL, KEY_F(12))); + m_menuitems.push_back(sinsp_menuitem_info("CTRL+F", "Search", sinsp_menuitem_info::ALL, 6)); + m_menuitems.push_back(sinsp_menuitem_info("p", "Pause", sinsp_menuitem_info::ALL, 'p')); + m_menuitems.push_back(sinsp_menuitem_info("c", "Clear", sinsp_menuitem_info::LIST, 'c')); + + m_menuitems_spybox.push_back(sinsp_menuitem_info("F1", "Help", sinsp_menuitem_info::ALL, KEY_F(1))); + m_menuitems_spybox.push_back(sinsp_menuitem_info("F2", "View As", sinsp_menuitem_info::ALL, KEY_F(2))); + m_menuitems_spybox.push_back(sinsp_menuitem_info("CTRL+F", "Search", sinsp_menuitem_info::ALL, 6)); + m_menuitems_spybox.push_back(sinsp_menuitem_info("p", "Pause", sinsp_menuitem_info::ALL, 'p')); + m_menuitems_spybox.push_back(sinsp_menuitem_info("Bak", "Back", sinsp_menuitem_info::ALL, KEY_BACKSPACE)); + m_menuitems_spybox.push_back(sinsp_menuitem_info("c", "Clear", sinsp_menuitem_info::ALL, 'c')); + m_menuitems_spybox.push_back(sinsp_menuitem_info("CTRL+G", "Goto", sinsp_menuitem_info::ALL, 7)); + + // + // Get screen dimensions + // + getmaxyx(stdscr, m_screenh, m_screenw); + g_csysdig_screen_w = m_screenw; + } +#endif +} + +sinsp_cursesui::~sinsp_cursesui() +{ + if(m_datatable != NULL) + { + delete m_datatable; + } + + if(m_json_spy_renderer != NULL) + { + delete m_json_spy_renderer; + } + +#ifndef NOCURSESUI + if(m_output_type == sinsp_table::OT_CURSES) + { + if(m_viz != NULL) + { + delete m_viz; + } + + if(m_spectro != NULL) + { + delete m_spectro; + } + + if(m_view_sidemenu != NULL) + { + delete m_view_sidemenu; + } + + if(m_action_sidemenu != NULL) + { + delete m_action_sidemenu; + } + + if(m_viewinfo_page != NULL) + { + delete m_viewinfo_page; + } + + if(m_mainhelp_page != NULL) + { + delete m_mainhelp_page; + } + + if(m_spy_box) + { + delete m_spy_box; + } + } +#endif + + delete m_timedelta_formatter; +} + +void sinsp_cursesui::configure(sinsp_view_manager* views) +{ + if(views == NULL) + { + ASSERT(false); + throw sinsp_exception("trying to configure the command line UI with no views"); + } + + // + // Copy the input views + // + m_views = *views; + + // + // Determine which view is the starting one + // + m_selected_view = m_views.get_selected_view(); + m_selected_view_sidemenu_entry = m_selected_view; + m_selected_action_sidemenu_entry = 0; + m_selected_view_sort_sidemenu_entry = 0; + m_sidemenu_sorting_col = -1; +} + +void sinsp_cursesui::start(bool is_drilldown, bool is_spy_switch) +{ + // + // Input validation + // + if(m_selected_view >= 0) + { + if(m_selected_view >= (int32_t)m_views.size()) + { + if(m_views.size() == 0) + { + throw sinsp_exception("no views loaded"); + } + else + { + ASSERT(false); + throw sinsp_exception("invalid view"); + } + } + } + + // + // Delete the previous table and visualizations + // + if(m_datatable != NULL) + { + delete m_datatable; + m_datatable = NULL; + } + + if(m_json_spy_renderer != NULL) + { + delete m_json_spy_renderer; + m_json_spy_renderer = NULL; + } + +#ifndef NOCURSESUI + spy_text_renderer::sysdig_output_type dig_otype = spy_text_renderer::OT_NORMAL; + + if(m_output_type == sinsp_table::OT_CURSES) + { + if(m_viz != NULL) + { + delete m_viz; + m_viz = NULL; + } + + if(m_spectro != NULL) + { + delete m_spectro; + m_spectro = NULL; + if(m_views.at(m_prev_selected_view)->m_drilldown_target == "dig_app") + { + dig_otype = spy_text_renderer::OT_LATENCY_APP; + } + else + { + dig_otype = spy_text_renderer::OT_LATENCY; + } + } + + if(m_spy_box && !is_spy_switch) + { + delete m_spy_box; + m_spy_box = NULL; + } + + m_chart = NULL; + } +#endif + + // + // Update the filter based on what's selected + // + create_complete_filter(false); + + // + // If we need a new datatable, allocate it and set it up + // + sinsp_view_info* wi = NULL; + sinsp_table::tabletype ty = sinsp_table::TT_NONE; + + if(m_selected_view >= 0) + { + wi = m_views.at(m_selected_view); + + if(wi->m_type == sinsp_view_info::T_TABLE) + { + ty = sinsp_table::TT_TABLE; + m_datatable = new sinsp_table(m_inspector, ty, m_refresh_interval_ns, + m_output_type, m_json_first_row, m_json_last_row); + } + else if(wi->m_type == sinsp_view_info::T_LIST) + { + ty = sinsp_table::TT_LIST; + m_datatable = new sinsp_table(m_inspector, ty, m_refresh_interval_ns, + m_output_type, m_json_first_row, m_json_last_row); + } + else if(wi->m_type == sinsp_view_info::T_SPECTRO) + { + ty = sinsp_table::TT_TABLE; + + // + // Accelerate the refresh rate to 1/2s + // + if(m_refresh_interval_ns == 2000000000) + { + m_datatable = new sinsp_table(m_inspector, ty, m_refresh_interval_ns / 4, + m_output_type, m_json_first_row, m_json_last_row); + } + else + { + m_datatable = new sinsp_table(m_inspector, ty, m_refresh_interval_ns, + m_output_type, m_json_first_row, m_json_last_row); + } + } + else + { + ASSERT(false); + } + + try + { + m_datatable->configure(&wi->m_columns, + m_complete_filter, + wi->m_use_defaults, + m_view_depth); + } + catch(...) + { + delete m_datatable; + m_datatable = NULL; + throw; + } + + if(m_sorting_col != -1 && m_sorting_col < (int32_t)wi->m_columns.size()) + { + m_datatable->set_sorting_col(m_sorting_col); + } + else + { + m_datatable->set_sorting_col(wi->m_sortingcol); + } + } + else + { + // + // Create the visualization component + // + if(m_output_type == sinsp_table::OT_JSON) + { + m_json_spy_renderer= new json_spy_renderer(m_inspector, + this, + m_selected_view, + spy_text_renderer::OT_NORMAL, + m_print_containers, + m_json_spy_text_fmt); + + m_json_spy_renderer->set_filter(m_complete_filter); + } +#ifndef NOCURSESUI + else + { + m_spy_box = new curses_textbox(m_inspector, this, m_selected_view, dig_otype); + m_spy_box->reset(); + m_chart = m_spy_box; + m_spy_box->set_filter(m_complete_filter); + } +#endif + } + +#ifndef NOCURSESUI + if(m_output_type != sinsp_table::OT_CURSES) + { + return; + } + + // + // If we need a table or spectrogram visualization, allocate it and set it up + // + if(m_output_type != sinsp_table::OT_JSON) + { + if(m_selected_view >= 0) + { + if(wi != NULL && wi->m_type == sinsp_view_info::T_SPECTRO) + { + ASSERT(ty == sinsp_table::TT_TABLE); + m_spectro = new curses_spectro(this, + m_inspector, + m_views.at(m_selected_view)->m_id == "spectro_traces"); + m_viz = NULL; + m_chart = m_spectro; + } + else + { + ASSERT(ty != sinsp_table::TT_NONE); + m_viz = new curses_table(this, m_inspector, ty); + m_spectro = NULL; + m_chart = m_viz; + } + + vector colsizes; + vector colnames; + + ASSERT(wi != NULL); + + wi->get_col_names_and_sizes(&colnames, &colsizes); + + if(m_viz) + { + ASSERT(m_spectro == NULL); + m_viz->configure(m_datatable, &colsizes, &colnames); + } + else + { + ASSERT(m_spectro != NULL); + m_spectro->configure(m_datatable); + } + + if(!is_drilldown) + { + populate_view_sidemenu("", &m_sidemenu_viewlist); + } + } + } +#endif + + m_prev_selected_view = m_selected_view; +} + +#ifndef NOCURSESUI +void sinsp_cursesui::render_header() +{ + uint32_t j = 0; + uint32_t k = 0; + + // + // Show the 'viewing' line + // + attrset(m_colors[HELP_BOLD]); + move(0, 0); + for(j = 0; j < m_screenw; j++) + { + addch(' '); + } + + mvaddstr(0, 0, "Viewing:"); + k += sizeof("Viewing: ") - 1; + + attrset(m_colors[sinsp_cursesui::PROCESS]); + + string vs; + + if(m_selected_view >= 0) + { + sinsp_view_info* sv = get_selected_view(); + const char* vcs = sv->m_name.c_str(); + vs = vcs; + } + else + { + if(m_selected_view == VIEW_ID_SPY) + { + vs = "I/O activity"; + } + else if(m_selected_view == VIEW_ID_DIG) + { + vs = "sysdig output"; + } + else + { + ASSERT(false); + } + } + + mvaddstr(0, k, vs.c_str()); + + k+= vs.size() + 1; + + attrset(m_colors[HELP_BOLD]); + mvaddstr(0, k, "For: "); + k += sizeof("For: ") - 1; + + attrset(m_colors[sinsp_cursesui::PROCESS]); + + if(m_sel_hierarchy.size() != 0) + { + vs = ""; + + for(j = 0; j < m_sel_hierarchy.size(); j++) + { + uint32_t pv = m_sel_hierarchy.at(j)->m_prev_selected_view; + + if(m_sel_hierarchy.at(j)->m_field == "") + { + continue; + } + + if(m_views.at(pv)->m_type == sinsp_view_info::T_SPECTRO) + { + //vs += m_sel_hierarchy.at(j)->m_prev_manual_filter.c_str(); + vs += "spectrogram area"; + } + else + { + vs += m_sel_hierarchy.at(j)->m_field; + vs += "="; + vs += m_sel_hierarchy.at(j)->m_val; + } + + if(j < m_sel_hierarchy.size() - 1) + { + vs += " and "; + } + } + + if(vs == "") + { + vs = "whole machine"; + } + } + else + { + vs = "whole machine"; + } + + mvaddstr(0, k, vs.c_str()); + + if(m_paused) + { + string wstr = "PAUSED"; + attrset(m_colors[sinsp_cursesui::LARGE_NUMBER]); + mvprintw(0, + m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + } + + // + // Show the 'filter' line + // + attrset(m_colors[HELP_BOLD]); + + move(1, 0); + for(uint32_t j = 0; j < m_screenw; j++) + { + addch(' '); + } + + attrset(m_colors[HELP_BOLD]); + + mvaddstr(1, 0, "Source:"); + k = sizeof("Source: ") - 1; + + attrset(m_colors[sinsp_cursesui::PROCESS]); + + string srcstr; + + if(m_inspector->is_live()) + { + srcstr = "Live System"; + } + else + { + if(m_n_evts_in_file == 0) + { + m_n_evts_in_file = m_inspector->get_num_events(); + m_evt_ts_delta = m_last_evt_ts - m_1st_evt_ts; + } + + srcstr = m_inspector->get_input_filename(); + srcstr += " (" + to_string(m_n_evts_in_file) + " evts, "; + + if(m_truncated_input) + { + srcstr += " truncated, "; + } + + m_timedelta_formatter->set_val(PT_RELTIME, + (uint8_t*)&m_evt_ts_delta, + 8, + 0, + ppm_print_format::PF_DEC); + + srcstr += string(m_timedelta_formatter->tostring_nice(NULL, 0, 0)) + ")"; + } + + mvaddnstr(1, k, srcstr.c_str(), m_screenw - k - 1); + + k += srcstr.size() + 1; + m_filterstring_start_x = k; + + attrset(m_colors[HELP_BOLD]); + + mvaddstr(1, k, "Filter:"); + k += sizeof("Filter: ") - 1; + + attrset(m_colors[sinsp_cursesui::PROCESS]); + + string sflt; + if(m_complete_filter != "") + { + sflt = m_complete_filter.c_str(); + } + else + { + sflt = "none"; + } + + mvaddnstr(1, k, sflt.c_str(), m_screenw - k - 1); + + k += sflt.size(); + m_filterstring_end_x = k; +} + +void sinsp_cursesui::turn_search_on(search_caller_interface* ifc, string header_text) +{ + ASSERT(m_spy_box != NULL); + m_search_header_text = header_text; + m_spy_box->get_offset(&m_search_start_x, &m_search_start_y); + + m_search_caller_interface = ifc; + m_output_searching = false; + m_output_filtering = false; + m_cursor_pos = 0; + curs_set(1); + render(); +} + +void sinsp_cursesui::draw_bottom_menu(vector* items, bool istable) +{ + uint32_t j = 0; + uint32_t k = 0; + + // + // Clear the line + // + move(m_screenh - 1, 0); + for(uint32_t j = 0; j < m_screenw; j++) + { + addch(' '); + } + + m_mouse_to_key_list.clear(); + + for(j = 0; j < items->size(); j++) + { + if(istable && ((items->at(j).m_type & sinsp_menuitem_info::TABLE) == 0)) + { + continue; + } + + if((!istable) && ((items->at(j).m_type & sinsp_menuitem_info::LIST) == 0)) + { + continue; + } + + uint32_t startx = k; + + attrset(m_colors[PROCESS]); + string fks = items->at(j).m_key; + mvaddnstr(m_screenh - 1, k, fks.c_str(), MAX(fks.size(), 2)); + k += MAX(fks.size(), 2); + + attrset(m_colors[PANEL_HIGHLIGHT_FOCUS]); + fks = items->at(j).m_desc; + + if(fks.size() < 6) + { + fks.resize(6, ' '); + } + + mvaddnstr(m_screenh - 1, k, fks.c_str(), fks.size()); + k += fks.size(); + + m_mouse_to_key_list.add(sinsp_mouse_to_key_list_entry(startx, + m_screenh - 1, + k - 1, + m_screenh - 1, + items->at(j).m_keyboard_equivalent)); + } +} + +void sinsp_cursesui::render_default_main_menu() +{ + bool istable; + + if(m_datatable != NULL && m_datatable->m_type == sinsp_table::TT_TABLE) + { + istable = true; + } + else + { + istable = false; + } + + draw_bottom_menu(&m_menuitems, istable); +} + +void sinsp_cursesui::render_spy_main_menu() +{ + draw_bottom_menu(&m_menuitems_spybox, false); +} + +void sinsp_cursesui::render_filtersearch_main_menu() +{ + uint32_t k = 0; + string* str = 0; + + // + // Pick the right string based on what we're doing + // + if(m_output_filtering) + { + str = &m_manual_filter; + + if(*str == "" && m_is_filter_sysdig && m_complete_filter != "") + { + *str = m_complete_filter; + } + } + else if(m_output_searching) + { + str = &m_manual_search_text; + } + else + { + if(m_search_caller_interface) + { + str = m_search_caller_interface->get_last_search_string(); + } + else + { + ASSERT(false); + } + } + + // + // Only clear the line if this is the first refresh, to prevent deleting the + // text that the user is typing + // + if(m_cursor_pos == 0) + { + move(m_screenh - 1, 0); + for(uint32_t j = 0; j < m_screenw; j++) + { + addch(' '); + } + } + + attrset(m_colors[PROCESS]); + string fks = "F1"; + mvaddnstr(m_screenh - 1, k, fks.c_str(), 10); + k += fks.size(); + attrset(m_colors[PANEL_HIGHLIGHT_FOCUS]); + fks = "Help"; + fks.resize(6, ' '); + mvaddnstr(m_screenh - 1, k, fks.c_str(), 6); + k += 6; + + if(m_output_filtering) + { + attrset(m_colors[PROCESS]); + fks = "F2"; + mvaddnstr(m_screenh - 1, k, fks.c_str(), 10); + k += fks.size(); + attrset(m_colors[PANEL_HIGHLIGHT_FOCUS]); + if(m_is_filter_sysdig) + { + fks = "Text"; + } + else + { + fks = "sysdig"; + } + fks.resize(6, ' '); + mvaddnstr(m_screenh - 1, k, fks.c_str(), 6); + k += 6; + } + + attrset(m_colors[PROCESS]); + fks = "Enter"; + mvaddnstr(m_screenh - 1, k, fks.c_str(), 10); + k += fks.size(); + + attrset(m_colors[PANEL_HIGHLIGHT_FOCUS]); + fks = "Done"; + fks.resize(6, ' '); + mvaddnstr(m_screenh - 1, k, fks.c_str(), 6); + k += 6; + + attrset(m_colors[PROCESS]); + fks = "Esc"; + mvaddnstr(m_screenh - 1, k, fks.c_str(), 10); + k += fks.size(); + + attrset(m_colors[PANEL_HIGHLIGHT_FOCUS]); + fks = "Clear"; + fks.resize(6, ' '); + mvaddnstr(m_screenh - 1, k, fks.c_str(), 6); + k += 6; + + k++; + attrset(m_colors[PANEL_HIGHLIGHT_FOCUS]); + if(m_is_filter_sysdig) + { + fks = "Expression: "; + } + else + { + if(m_search_header_text == "") + { + fks = "Text to match: "; + } + else + { + fks = m_search_header_text + ": "; + } + } + mvaddnstr(m_screenh - 1, k, fks.c_str(), 20); + k += fks.size(); + + uint32_t cursor_pos = k; + + if(m_cursor_pos == 0) + { + for(; k < m_screenw; k++) + { + addch(' '); + } + + m_cursor_pos = cursor_pos; + + mvprintw(m_screenh - 1, m_cursor_pos, str->c_str()); + + m_cursor_pos += str->size(); + } + + move(m_screenh - 1, m_cursor_pos); +} + +void sinsp_cursesui::render_position_info() +{ + if(m_chart == NULL) + { + return; + } + + int32_t pos; + int32_t totlines; + float percent; + bool truncated; + if(m_chart->get_position(&pos, &totlines, &percent, &truncated)) + { + char prstr[128]; + string trs; + uint32_t csize = 18; + + attrset(m_colors[sinsp_cursesui::PROCESS_MEGABYTES]); + + move(m_screenh - 1, m_screenw - csize); + for(uint32_t k = 0; k < csize; k++) + { + addch(' '); + } + + if(truncated) + { + trs = "(truncated)"; + } + + if(percent != 0) + { + sprintf(prstr, "%d/%d(%.1f%%)%s", (int)pos, (int)totlines, percent * 100, trs.c_str()); + } + else + { + sprintf(prstr, "%d/%d(0.0%%)%s", (int)pos, (int)totlines, trs.c_str()); + } + + mvaddstr(m_screenh - 1, + m_screenw - strlen(prstr), + prstr); + } +} + +void sinsp_cursesui::render_main_menu() +{ + if(m_output_filtering || m_output_searching || m_search_caller_interface != NULL) + { + render_filtersearch_main_menu(); + } + else if(m_spy_box != NULL) + { + render_spy_main_menu(); + } + else + { + render_default_main_menu(); + } +} + +void sinsp_cursesui::render() +{ + if(m_spectro && !m_view_sidemenu) + { + return; + } + + // + // Draw the header at the top of the page + // + render_header(); + + // + // Print the position in the chart + // + if(m_output_filtering || m_output_searching || m_search_caller_interface != NULL) + { + render_position_info(); + } + + // + // Draw the menu at the bottom of the screen + // + render_main_menu(); + + // + // If required, draw the side menu + // + if(m_view_sidemenu) + { + m_view_sidemenu->render(); + } + + if(m_view_sort_sidemenu) + { + m_view_sort_sidemenu->render(); + } + + if(m_action_sidemenu) + { + m_action_sidemenu->render(); + } + + // + // Print the position in the chart + // + if(!(m_output_filtering || m_output_searching || m_search_caller_interface != NULL)) + { + render_position_info(); + } +} +#endif + +sinsp_view_info* sinsp_cursesui::get_selected_view() +{ + if(m_selected_view < 0) + { + return NULL; + } + + ASSERT(m_selected_view < (int32_t)m_views.size()); + return m_views.at(m_selected_view); +} + +sinsp_view_info* sinsp_cursesui::get_prev_selected_view() +{ + if(m_prev_selected_view < 0) + { + return NULL; + } + + ASSERT(m_prev_selected_view < (int32_t)m_views.size()); + return m_views.at(m_prev_selected_view); +} + +#ifndef NOCURSESUI +void sinsp_cursesui::populate_view_sidemenu(string field, vector* viewlist) +{ + uint32_t k = 0; + + viewlist->clear(); + uint64_t bpos = field.find('['); + if(bpos != string::npos) + { + field = field.substr(0, bpos); + } + + for(uint32_t j = 0; j < m_views.size(); ++j) + { + auto it = m_views.at(j); + + for(auto atit = it->m_applies_to.begin(); atit != it->m_applies_to.end(); ++atit) + { + if(*atit == field) + { + viewlist->push_back(sidemenu_list_entry(it->m_name, j)); + + if(it->m_name == m_views.at(m_selected_view)->m_name) + { + m_selected_view_sidemenu_entry = k; + + if(m_view_sidemenu != NULL) + { + m_view_sidemenu->m_selct = k; + } + } + + k++; + } + } + } + + if(m_view_sidemenu != NULL) + { + m_view_sidemenu->set_entries(viewlist); + } +} + +void sinsp_cursesui::populate_view_cols_sidemenu() +{ + int32_t k = 0; + + vector viewlist; + sinsp_view_info* vinfo = get_selected_view(); + + for(auto it : vinfo->m_columns) + { + if(it.m_name != "NA") + { + if(m_sidemenu_sorting_col == k) + { + viewlist.push_back(sidemenu_list_entry(it.m_name, k++)); + continue; + } + viewlist.push_back(sidemenu_list_entry(it.m_name, k++)); + } + } + + if(viewlist.size() == 0) + { + viewlist.push_back(sidemenu_list_entry("", 0)); + } + + if(m_view_sort_sidemenu != NULL) + { + m_view_sort_sidemenu->set_entries(&viewlist); + } +} + + + +void sinsp_cursesui::populate_action_sidemenu() +{ + uint32_t k = 0; + vector viewlist; + + m_selected_action_sidemenu_entry = 0; + + sinsp_view_info* vinfo = get_selected_view(); + + for(auto hk : vinfo->m_actions) + { + string str = string("(") + hk.m_hotkey + ") " + hk.m_description; + viewlist.push_back(sidemenu_list_entry(str, k++)); + } + + if(viewlist.size() == 0) + { + viewlist.push_back(sidemenu_list_entry("", 0)); + } + + if(m_action_sidemenu != NULL) + { + m_action_sidemenu->m_selct = 0; + m_action_sidemenu->set_entries(&viewlist); + } +} +#endif // NOCURSESUI + +string combine_filters(string flt1, string flt2) +{ + if(flt1 == "") + { + return flt2; + } + else + { + if(flt2 == "") + { + return flt1; + } + } + + string res = "(" + flt1 + ") and (" + flt2 + ")"; + return res; +} + +Json::Value sinsp_cursesui::generate_json_info_section() +{ + Json::Value jinfo; + Json::Value jlegend; + + sinsp_view_info* wi = NULL; + + if(m_selected_view >= 0) + { + wi = m_views.at(m_selected_view); + vector colsizes; + vector colnames; + + ASSERT(wi != NULL); + + jinfo["sortingCol"] = wi->m_sortingcol; + + for(auto av : wi->m_applies_to) + { + jinfo["appliesTo"].append(av); + } + + sinsp_view_column_info* kinfo = wi->get_key(); + if(kinfo) + { + jinfo["drillDownKeyField"] = kinfo->m_field; + jinfo["canDrillDown"] = true; + } + else + { + jinfo["canDrillDown"] = false; + } + + wi->get_col_names_and_sizes(&colnames, &colsizes); + + uint32_t off; + if(colnames.size() == m_datatable->m_types->size() - 1) + { + off = 1; + } + else + { + off = 0; + } + + vector* tlegend = m_datatable->get_legend(); + ASSERT(tlegend->size() == colnames.size()); + + for(uint32_t j = 1; j < colnames.size(); j++) + { + Json::Value jcinfo; + + jcinfo["name"] = colnames[j]; + jcinfo["size"] = colsizes[j]; + jcinfo["type"] = param_type_to_string(m_datatable->m_types->at(j + off)); + jcinfo["format"] = print_format_to_string(tlegend->at(j).m_print_format); + + jlegend.append(jcinfo); + } + } + + jinfo["legend"] = jlegend; + return jinfo; +} + +void sinsp_cursesui::handle_end_of_sample(sinsp_evt* evt, int32_t next_res) +{ + vector* sample; + m_datatable->flush(evt); + + // + // It's time to refresh the data for this chart. + // First of all, create the data for the chart + // + if(m_output_type == sinsp_table::OT_JSON && (m_inspector->is_live() || (m_eof > 0))) + { + printf("{\"progress\": 100, "); + + sample = m_datatable->get_sample(get_time_delta()); + + printf("\"count\": %" PRIu64 ", ", + m_datatable->m_json_output_lines_count); + + Json::Value root = generate_json_info_section(); + + if(m_views.at(m_selected_view)->m_type == sinsp_view_info::T_TABLE) + { + bool res; + execute_table_action(STA_DRILLDOWN_TEMPLATE, 0, &res); + create_complete_filter(true); + + root["filterTemplateF"] = m_complete_filter; + root["filterTemplate"] = m_complete_filter_noview; + } + + Json::FastWriter writer; + string jstr = writer.write(root); + printf("\"info\": %s", jstr.substr(0, jstr.size() - 1).c_str()); + + printf("}\n"); + //printf("%c", EOF); + } + else + { + if(m_output_type != sinsp_table::OT_JSON) + { + sample = m_datatable->get_sample(get_time_delta()); + } + } + +#ifndef NOCURSESUI + if(m_output_type == sinsp_table::OT_CURSES) + { + // + // If the help page has been shown, don't update the screen + // + if(m_viewinfo_page != NULL || m_mainhelp_page != NULL) + { + return; + } + + // + // Now refresh the UI. + // + if(!m_paused) + { + if(m_viz) + { + ASSERT(m_spectro == NULL); + m_viz->update_data(sample); + + if(m_datatable->m_type == sinsp_table::TT_LIST && m_inspector->is_live()) + { + m_viz->follow_end(); + } + + m_viz->render(true); + } + else if(m_spectro) + { + ASSERT(m_viz == NULL); + m_spectro->update_data(sample); + m_spectro->render(true); + } + } + + render(); + } +#endif + // + // If this is a trace file, check if we reached the end of the file. + // Or, if we are in replay mode, wait for a key press before processing + // the next sample. + // + if(!m_inspector->is_live()) + { +#ifndef NOCURSESUI +/* + if(m_output_type == sinsp_table::OT_CURSES) + { + if(m_offline_replay) + { + while(getch() != ' ') + { + usleep(10000); + } + } + } +*/ +#endif + } +} + +void sinsp_cursesui::restart_capture(bool is_spy_switch) +{ + if(!m_inspector->is_live() && m_n_evts_in_file == 0) + { + m_n_evts_in_file = m_inspector->get_num_events(); + m_evt_ts_delta = m_last_evt_ts - m_1st_evt_ts; + } + + m_inspector->close(); + start(true, is_spy_switch); + m_inspector->open(m_event_source_name); +} + +void sinsp_cursesui::create_complete_filter(bool templated) +{ + if(m_is_filter_sysdig) + { + if(m_manual_filter != "") + { + m_complete_filter = m_manual_filter; + } + + m_complete_filter_noview = m_complete_filter; + } + else + { + m_complete_filter = m_cmdline_capture_filter; + m_complete_filter = combine_filters(m_complete_filter, m_sel_hierarchy.tofilter(templated)); + + m_complete_filter_noview = m_complete_filter; + + // + // Note: m_selected_view is smaller than 0 when there's no view, because we're doing + // non-view stuff like spying. + // + if(m_selected_view >= 0) + { + m_complete_filter = combine_filters(m_complete_filter, + m_views.at(m_selected_view)->get_filter(m_view_depth)); + } + } +} + +void sinsp_cursesui::switch_view(bool is_spy_switch) +{ +#ifndef NOCURSESUI + if(m_output_type == sinsp_table::OT_CURSES) + { + // + // Clear the screen to make sure all the crap is removed + // + clear(); + + // + // If we're currently visualizing the spy box, reset it and return immediately + // + if(is_spy_switch) + { + if(m_spy_box) + { + m_spy_box->reset(); + } + } + } +#endif + + // + // Put the current view in the hierarchy stack + // +#if 1 + sinsp_view_info* psv = get_prev_selected_view(); + + if(psv != NULL) + { + if(m_sel_hierarchy.size() > 0) + { + sinsp_ui_selection_info* psinfo = m_sel_hierarchy.at(m_sel_hierarchy.size() - 1); + + m_sel_hierarchy.push_back(psinfo->m_field, psinfo->m_val, + psv->get_key(), psinfo->m_view_filter, + m_prev_selected_view, m_selected_view_sidemenu_entry, + NULL, psv->m_sortingcol, m_manual_filter, m_is_filter_sysdig, + m_datatable->is_sorting_ascending(), false); + } + else + { + m_sel_hierarchy.push_back("", "", + psv->get_key(), "", + m_prev_selected_view, m_selected_view_sidemenu_entry, + NULL, psv->m_sortingcol, m_manual_filter, m_is_filter_sysdig, + m_datatable->is_sorting_ascending(), false); + } + } +#endif + + // + // Clear the manual filter, but not if this is a sysdig filter and we're in the same + // view (applying sysdig filters causes the same view to the reloaded, and in that + // case we want to preserve the filter). + // + if(m_prev_selected_view != m_selected_view) + { + m_manual_filter = ""; + } + + // + // If this is a file, we need to restart the capture. + // If it's a live capture, we restart only if start() fails, which usually + // happens in case one of the filter fields requested thread state. + // + if(!m_inspector->is_live()) + { + m_eof = 0; + m_last_progress_evt = 0; + restart_capture(is_spy_switch); + } + else + { + // + // When live, also make sure to unpause the viz, otherwise the screen + // will stay empty. + // + if(m_paused) + { + pause(); + } + + try + { + start(true, is_spy_switch); + } + catch(...) + { + restart_capture(is_spy_switch); + } + } + +#ifndef NOCURSESUI + if(m_output_type == sinsp_table::OT_CURSES) + { + delete m_view_sidemenu; + m_view_sidemenu = NULL; + + delete m_action_sidemenu; + m_action_sidemenu = NULL; + + delete m_view_sort_sidemenu; + m_view_sort_sidemenu = NULL; + + if(m_viz != NULL) + { + m_viz->render(true); + } + else if(m_spectro != NULL) + { + m_spectro->render(true); + } + + render(); + } +#endif +} + +void sinsp_cursesui::spy_selection(string field, string val, + sinsp_view_column_info* column_info, + bool is_dig) +{ + uint32_t srtcol; + sinsp_table_field rowkeybak; + +#ifdef NOCURSESUI + if(true) +#else + if(m_viz) +#endif + { +#ifndef NOCURSESUI + sinsp_table_field* rowkey = m_datatable->get_row_key(m_viz->m_selct); +#else + sinsp_table_field* rowkey = NULL; +#endif + if(rowkey != NULL) + { + rowkeybak.m_val = new uint8_t[rowkey->m_len]; + memcpy(rowkeybak.m_val, rowkey->m_val, rowkey->m_len); + rowkeybak.m_len = rowkey->m_len; + } + + srtcol = m_datatable->get_sorting_col(); + } +#ifndef NOCURSESUI + else if(m_spectro) + { + m_is_filter_sysdig = true; + m_manual_filter = m_spectro->m_selection_filter; + srtcol = 0; + rowkeybak.m_val = NULL; + rowkeybak.m_len = 0; + srtcol = 2; + } + else + { + ASSERT(false); + return; + } +#endif + + ASSERT(m_selected_view < (int32_t)m_views.size()); + + if(m_views.at(m_selected_view)->m_drilldown_increase_depth) + { + m_view_depth++; + } + + string vfilter; + if(m_views.at(m_selected_view)->m_propagate_filter) + { + vfilter = m_views.at(m_selected_view)->get_filter(m_view_depth); + } + else + { + vfilter = ""; + } + + m_sel_hierarchy.push_back(field, val, column_info, + vfilter, + m_selected_view, m_selected_view_sidemenu_entry, + &rowkeybak, srtcol, m_manual_filter, m_is_filter_sysdig, + m_datatable->is_sorting_ascending(), true); + + if(is_dig) + { + m_selected_view = VIEW_ID_DIG; + } + else + { + m_selected_view = VIEW_ID_SPY; + } + + if(!m_inspector->is_live()) + { + m_eof = 0; + m_last_progress_evt = 0; + restart_capture(false); + } + else + { + try + { + start(true, false); + } + catch(...) + { + restart_capture(false); + } + } + +#ifndef NOCURSESUI + render(); +#endif +} + +// returns false if there is no suitable drill down view for this field +bool sinsp_cursesui::do_drilldown(string field, string val, + sinsp_view_column_info* column_info, + uint32_t new_view_num, filtercheck_field_info* info, + bool dont_restart) +{ + // + // unpause the thing if it's paused + // + if(m_paused) + { + pause(); + } + + // + // escape string parameters + // + if(info != NULL && info->m_type & PT_CHARBUF) + { + string escape = "\""; + val = escape + val + escape; + } + + // + // Do the drilldown + // + sinsp_table_field* rowkey = NULL; + +#ifndef NOCURSESUI + if(m_viz != NULL) + { + rowkey = m_datatable->get_row_key(m_viz->m_selct); + } +#endif + sinsp_table_field rowkeybak; + if(rowkey != NULL) + { + rowkeybak.m_val = new uint8_t[rowkey->m_len]; + memcpy(rowkeybak.m_val, rowkey->m_val, rowkey->m_len); + rowkeybak.m_len = rowkey->m_len; + } + + uint32_t srtcol; + srtcol = m_datatable->get_sorting_col(); + + if(m_views.at(m_selected_view)->m_drilldown_increase_depth) + { + if(m_views.at(new_view_num)->m_id != "spectro_tracers") + { + m_view_depth++; + } + } + + string vfilter; + if(m_views.at(m_selected_view)->m_propagate_filter) + { + vfilter = m_views.at(m_selected_view)->get_filter(m_view_depth); + } + else + { + vfilter = ""; + } + + m_sel_hierarchy.push_back(field, val, + column_info, vfilter, + m_selected_view, m_selected_view_sidemenu_entry, + &rowkeybak, srtcol, m_manual_filter, m_is_filter_sysdig, + m_datatable->is_sorting_ascending(), true); + + m_selected_view = new_view_num; + + // + // Reset the filter + // +#ifndef NOCURSESUI + if(m_output_type != sinsp_table::OT_JSON) + { + if(m_viz != NULL) + { + m_manual_filter = ""; + m_is_filter_sysdig = false; + } + else + { + ASSERT(m_spectro != NULL); + m_is_filter_sysdig = true; + m_manual_filter = m_spectro->m_selection_filter; + } + } +#endif + + if(!dont_restart) + { + if(!m_inspector->is_live()) + { + m_eof = 0; + m_last_progress_evt = 0; + restart_capture(false); + } + else + { + try + { + start(true, false); + } + catch(...) + { + restart_capture(false); + } + } + +#ifndef NOCURSESUI + clear(); + populate_view_sidemenu(field, &m_sidemenu_viewlist); + populate_action_sidemenu(); + + if(m_viz) + { + m_viz->render(true); + } + else if(m_spectro) + { + m_spectro->render(true); + } + render(); +#endif + } + + return true; +} + +// returns false if there is no suitable drill down view for this field +bool sinsp_cursesui::drilldown(string field, string val, + sinsp_view_column_info* column_info, + filtercheck_field_info* info, bool dont_restart) +{ + uint32_t j = 0; + + for(j = 0; j < m_views.size(); ++j) + { + if(m_views.at(j)->m_id == m_views.at(m_selected_view)->m_drilldown_target) + { + return do_drilldown(field, val, column_info, j, info, dont_restart); + } + } + + for(j = 0; j < m_views.size(); ++j) + { + auto it = m_views.at(j); + + for(auto atit = it->m_applies_to.begin(); atit != it->m_applies_to.end(); ++atit) + { + if(*atit == field) + { + return do_drilldown(field, val, column_info, j, info, dont_restart); + } + } + } + + return false; +} + +bool sinsp_cursesui::spectro_selection(string field, string val, + sinsp_view_column_info* column_info, + filtercheck_field_info* info, sysdig_table_action ta) +{ + uint32_t j = 0; + string spectro_name; + + if(m_views.at(m_selected_view)->m_spectro_type == "tracers") + { + spectro_name = "spectro_traces"; + } + else + { + if(ta == STA_SPECTRO) + { + spectro_name = "spectro_all"; + } + else + { + spectro_name = "spectro_file"; + } + } + + for(j = 0; j < m_views.size(); ++j) + { + if(m_views.at(j)->m_id == spectro_name) + { + return do_drilldown(field, val, column_info, j, info, false); + } + } + + return false; +} + +bool sinsp_cursesui::drillup() +{ + if(m_sel_hierarchy.size() > 0) + { + // + // unpause the thing if it's paused + // + if(m_paused) + { + pause(); + } + + // + // Do the drillup + // + string field; + sinsp_ui_selection_info* psinfo = NULL; + + sinsp_ui_selection_info* sinfo = m_sel_hierarchy.at(m_sel_hierarchy.size() - 1); + bool is_spctro_app = false; + + if(m_selected_view > 0 && m_views.at(m_selected_view)->m_id == "spectro_tracers") + { + is_spctro_app = true; + } + + m_manual_filter = ""; + + if(m_sel_hierarchy.size() > 1) + { + psinfo = m_sel_hierarchy.at(m_sel_hierarchy.size() - 2); + field = psinfo->m_field; + } + + sinsp_table_field rowkey = sinfo->m_rowkey; + + m_selected_view = sinfo->m_prev_selected_view; + m_selected_view_sidemenu_entry = sinfo->m_prev_selected_sidemenu_entry; + + if(m_views.at(m_selected_view)->m_drilldown_increase_depth && + !is_spctro_app) + { + if(sinfo != NULL && sinfo->m_is_drilldown) + { + m_view_depth--; + } + } + + if(m_views.at(m_selected_view)->m_type == sinsp_view_info::T_SPECTRO) + { + m_is_filter_sysdig = false; + } + else + { + m_manual_filter = sinfo->m_prev_manual_filter; + m_is_filter_sysdig = sinfo->m_prev_is_filter_sysdig; + } + + bool is_sorting_ascending = sinfo->m_prev_is_sorting_ascending; + + ASSERT(m_selected_view < (int32_t)m_views.size()); + + m_sel_hierarchy.pop_back(); + //m_views[m_selected_view].m_filter = m_sel_hierarchy.tofilter(); + + m_complete_filter = m_cmdline_capture_filter; + m_complete_filter = combine_filters(m_complete_filter, m_sel_hierarchy.tofilter(false)); + + if(!m_inspector->is_live()) + { + m_eof = 0; + m_last_progress_evt = 0; + restart_capture(false); + } + else + { + try + { + start(true, false); + } + catch(...) + { + restart_capture(false); + } + } +#ifndef NOCURSESUI + if(m_viz) + { + if(rowkey.m_val != NULL) + { + m_viz->m_last_key.copy(&rowkey); + m_viz->m_last_key.m_isvalid = true; + m_viz->m_selection_changed = true; + } + else + { + m_viz->m_last_key.m_isvalid = false; + } + + m_viz->m_drilled_up = true; + } + + populate_view_sidemenu(field, &m_sidemenu_viewlist); + populate_action_sidemenu(); + + // + // If sorting is different from the default one, restore it + // + if(sinfo->m_prev_sorting_col != m_views.at(m_selected_view)->m_sortingcol) + { + m_datatable->set_sorting_col(sinfo->m_prev_sorting_col); + } + + m_datatable->set_is_sorting_ascending(is_sorting_ascending); + + // + // If filtering is different from the default one, apply it + // + if(m_manual_filter != "" && !m_is_filter_sysdig) + { + m_datatable->set_freetext_filter(m_manual_filter); + } + + clear(); + if(m_viz) + { + m_viz->render(true); + } + else if(m_spectro) + { + m_spectro->render(true); + } + + render(); +#endif + + if(rowkey.m_val) + { + delete[] rowkey.m_val; + } + return true; + } + + return false; +} + +void sinsp_cursesui::pause() +{ + m_paused = !m_paused; + if(m_datatable != NULL) + { + m_datatable->set_paused(m_paused); + } +#ifndef NOCURSESUI + if(m_spectro == NULL) + { + render_header(); + } +#endif +} + +#ifndef NOCURSESUI +void sinsp_cursesui::print_progress(double progress) +{ + attrset(m_colors[sinsp_cursesui::PROCESS]); + + string wstr = "Processing File"; + mvprintw(m_screenh / 2, + m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + + // + // Using sprintf because to_string doesn't support setting the precision + // + char numbuf[64]; + sprintf(numbuf, "%.2lf", progress); + wstr = "Progress: " + string(numbuf); + mvprintw(m_screenh / 2 + 1, + m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + + refresh(); +} + +sysdig_table_action sinsp_cursesui::handle_textbox_input(int ch) +{ + bool closing = false; + string* str = NULL; + bool handled = true; + + // + // Pick the right string based on what we're doing + // + if(m_output_filtering) + { + str = &m_manual_filter; + } + else if(m_output_searching) + { + str = &m_manual_search_text; + } + else + { + if(m_search_caller_interface) + { + str = m_search_caller_interface->get_last_search_string(); + } + else + { + ASSERT(false); + } + } + + switch(ch) + { + case KEY_F(1): + m_mainhelp_page = new curses_mainhelp_page(this); + return STA_NONE; + case KEY_F(2): + m_is_filter_sysdig = !m_is_filter_sysdig; + *str = ""; + m_cursor_pos = 0; + render(); + return STA_NONE; + case KEY_DOWN: + case KEY_UP: + case KEY_PPAGE: + case KEY_NPAGE: + if(m_spy_box != NULL) + { + m_spy_box->handle_input(ch); + } + else + { + if(m_viz) + { + m_viz->handle_input(ch); + } + else if(m_spectro) + { + ASSERT(false); + } + } + return STA_NONE; + case 27: // ESC + *str = ""; + + if(m_spy_box != NULL) + { + m_spy_box->scroll_to(m_search_start_x, m_search_start_y); + m_spy_box->up(); + } + // FALL THROUGH + case '\n': + case '\r': + case KEY_ENTER: + case 6: // CTRL+F + case KEY_F(4): + closing = true; + curs_set(0); + + if(m_is_filter_sysdig && !m_output_searching) + { + if(*str != "") + { + sinsp_filter_compiler compiler(m_inspector, *str); + sinsp_filter* f; + + try + { + f = compiler.compile(); + } + catch(const sinsp_exception& e) + { + // + // Backup the cursor position + // + int cx, cy; + getyx(stdscr, cy, cx); + + // + // Print the error string + // + string wstr = "Invalid sysdig filter"; + + attrset(m_colors[sinsp_cursesui::FAILED_SEARCH]); + mvprintw(m_screenh / 2, + m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + + // + // Restore the cursor + // + attrset(m_colors[PANEL_HIGHLIGHT_FOCUS]); + move(cy, cx); + curs_set(1); + closing = false; + break; + } + + delete f; + } + } + + break; + case KEY_BACKSPACE: + case 127: + if(str->size() > 0) + { + m_cursor_pos--; + move(m_screenh - 1, m_cursor_pos); + addch(' '); + move(m_screenh - 1, m_cursor_pos); + *str = str->substr(0, str->size() - 1); + + if(str->size() < 2) + { + if(m_spy_box != NULL) + { + m_spy_box->scroll_to(m_search_start_x, m_search_start_y); + } + } + + break; + } + else + { + return STA_NONE; + } + case KEY_F(3): + if(m_search_caller_interface) + { + if(m_search_caller_interface->on_search_next()) + { + render(); + } + else + { + string wstr = " NOT FOUND "; + attrset(m_colors[sinsp_cursesui::FAILED_SEARCH]); + + mvprintw(m_screenh / 2, + m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + + render(); + } + } + + break; + default: + handled = false; + break; + } + + if(ch >= ' ' && ch <= '~') + { + addch(ch); + *str += ch; + m_cursor_pos++; + } + else + { + if(!handled) + { + return STA_NONE; + } + } + + if(m_output_filtering) + { + if(!m_is_filter_sysdig) + { + // + // Update the filter in the datatable + // + m_datatable->set_freetext_filter(*str); + + // + // Refresh the data and the visualization + // + m_viz->update_data(m_datatable->get_sample(get_time_delta()), true); + m_viz->render(true); + } + } + else if(m_output_searching) + { + sinsp_table_field* skey = m_datatable->search_in_sample(*str); + + if(skey != NULL) + { + int32_t selct = m_datatable->get_row_from_key(skey); + m_viz->goto_row(selct); + m_search_nomatch = false; + } + else + { + m_search_nomatch = true; + m_viz->render(true); + } + } + else + { + if(m_search_caller_interface) + { + if(m_search_caller_interface->on_search_key_pressed(*str)) + { + render(); + } + else + { + string wstr = " NOT FOUND "; + attrset(m_colors[sinsp_cursesui::FAILED_SEARCH]); + + mvprintw(m_screenh / 2, + m_screenw / 2 - wstr.size() / 2, + wstr.c_str()); + + render(); + } + + render(); + } + else + { + ASSERT(false); + } + } + + if(closing) + { + sysdig_table_action res = STA_NONE; + + if(m_is_filter_sysdig && !m_output_searching) + { + res = STA_SWITCH_VIEW; + } + + m_search_nomatch = false; + m_output_filtering = false; + m_output_searching = false; + m_search_caller_interface = NULL; + render(); + + if(res != STA_NONE) + { + return res; + } + } + + return STA_NONE; +} + +sysdig_table_action sinsp_cursesui::handle_input(int ch) +{ + // + // Avoid parsing keys during file load + // + if((!m_inspector->is_live()) && !is_eof() && + (m_spectro != NULL && !m_spectro->m_scroll_paused)) + { + if(ch != KEY_BACKSPACE && + ch != 127 && + ch != 'q' && + ch != KEY_F(10)) + { + return STA_NONE; + } + } + + if(m_mainhelp_page != NULL) + { + sysdig_table_action actn = m_mainhelp_page->handle_input(ch); + + if(actn == STA_DESTROY_CHILD) + { + delete m_mainhelp_page; + m_mainhelp_page = NULL; + + if(m_spy_box) + { + m_spy_box->render(); + } + + if(m_viz != NULL) + { + m_viz->render(true); + } + else if(m_spectro) + { + switch_view(false); + } + + if(m_viewinfo_page) + { + m_viewinfo_page->render(); + } + + render(); + return STA_NONE; + } + else if(actn != STA_PARENT_HANDLE) + { + return actn; + } + } + + if(m_view_sidemenu != NULL) + { + ASSERT(m_action_sidemenu == NULL); + + sysdig_table_action ta = m_view_sidemenu->handle_input(ch); + if(ta == STA_SWITCH_VIEW) + { + if(m_viewinfo_page) + { + delete m_viewinfo_page; + m_viewinfo_page = NULL; + } + return ta; + } + else if(ta != STA_PARENT_HANDLE) + { + return STA_NONE; + } + } + else + { + if(m_action_sidemenu != NULL) + { + sysdig_table_action ta = m_action_sidemenu->handle_input(ch); + if(ta == STA_SWITCH_VIEW) + { + sinsp_view_info* vinfo = get_selected_view(); + + g_logger.format("running action %d %s", m_selected_action_sidemenu_entry, + vinfo->m_name.c_str()); + if(vinfo->m_actions.size() != 0) + { + ASSERT(m_selected_action_sidemenu_entry < vinfo->m_actions.size()); + run_action(&vinfo->m_actions[m_selected_action_sidemenu_entry]); + } + + return ta; + } + else if(ta == STA_DESTROY_CHILD) + { + if(m_viz) + { + m_viz->set_x_start(0); + delete m_action_sidemenu; + m_action_sidemenu = NULL; + m_viz->set_x_start(0); + m_viz->recreate_win(m_screenh - 3); + m_viz->render(true); + m_viz->render(true); + } + else if(m_spectro) + { + delete m_action_sidemenu; + m_action_sidemenu = NULL; + m_spectro->recreate_win(m_screenh - 3); + m_spectro->render(true); + m_spectro->render(true); + + } + render(); + } + else if(ta != STA_PARENT_HANDLE) + { + return STA_NONE; + } + } + + if(m_view_sort_sidemenu != NULL) + { + sysdig_table_action ta = m_view_sort_sidemenu->handle_input(ch); + if(ta == STA_SWITCH_VIEW || ta == STA_DESTROY_CHILD) + { + if(ta == STA_SWITCH_VIEW) + { + ASSERT(m_selected_view_sort_sidemenu_entry < get_selected_view()->m_columns.size()); + m_datatable->set_sorting_col(m_selected_view_sort_sidemenu_entry+1); + m_datatable->sort_sample(); + m_viz->update_data(m_viz->m_data); + } + delete m_view_sort_sidemenu; + m_view_sort_sidemenu = NULL; + m_viz->set_x_start(0); + m_viz->recreate_win(m_screenh - 3); + m_viz->render(true); + render(); + if(ta == STA_SWITCH_VIEW) + { + return STA_NONE; + } + } + else if(ta != STA_PARENT_HANDLE) + { + return STA_NONE; + } + } + + } + + if(m_output_filtering || m_output_searching || m_search_caller_interface != NULL) + { + ASSERT(m_view_sidemenu == NULL); + ASSERT(m_action_sidemenu == NULL); + return handle_textbox_input(ch); + } + + if(m_spy_box != NULL) + { + ASSERT(m_view_sidemenu == NULL); + ASSERT(m_action_sidemenu == NULL); + ASSERT(m_output_filtering == false); + ASSERT(m_output_searching == false); + sysdig_table_action actn = m_spy_box->handle_input(ch); + + if(actn != STA_PARENT_HANDLE) + { + return actn; + } + } + + // + // Note: the info page doesn't handle input when the sidemenu is on, because in that + // case it's just going to passively show the info for the selected view + // + if(m_viewinfo_page && m_view_sidemenu == NULL) + { + ASSERT(m_view_sidemenu == NULL); + + sysdig_table_action actn = m_viewinfo_page->handle_input(ch); + + if(actn == STA_DESTROY_CHILD) + { + delete m_viewinfo_page; + m_viewinfo_page = NULL; + if(m_viz != NULL) + { + m_viz->render(true); + } + + render(); + return STA_NONE; + } + + return actn; + } + + // + // Pass the event to the table viz + // + if(m_viz) + { + sysdig_table_action actn = m_viz->handle_input(ch); + if(actn != STA_PARENT_HANDLE) + { + return actn; + } + } + else if(m_spectro) + { + sysdig_table_action actn = m_spectro->handle_input(ch); + if(actn != STA_PARENT_HANDLE) + { + return actn; + } + } + + switch(ch) + { + case '?': + case 'h': + case KEY_F(1): + m_mainhelp_page = new curses_mainhelp_page(this); + break; + case KEY_F(10): + case 'q': + return STA_QUIT; + case 'p': + pause(); + break; + case KEY_F(2): + if(m_action_sidemenu != NULL) + { + break; + } + + if(m_view_sidemenu == NULL) + { + if(m_viz) + { + m_viz->set_x_start(VIEW_SIDEMENU_WIDTH); + } + else if(m_spectro) + { + m_spectro->set_x_start(VIEW_SIDEMENU_WIDTH); + } + + m_view_sidemenu = new curses_table_sidemenu(curses_table_sidemenu::ST_VIEWS, + this, m_selected_view_sidemenu_entry, VIEW_SIDEMENU_WIDTH); + + m_view_sidemenu->set_entries(&m_sidemenu_viewlist); + m_view_sidemenu->set_title("Select View"); + render(); + + m_viewinfo_page = new curses_viewinfo_page(this, + m_selected_view, + TABLE_Y_START, + VIEW_SIDEMENU_WIDTH, + m_screenh - TABLE_Y_START - 1, + m_screenw - VIEW_SIDEMENU_WIDTH); + + if(m_spectro) + { + render(); + } + } + else + { + if(m_viewinfo_page) + { + delete m_viewinfo_page; + m_viewinfo_page = NULL; + } + + delete m_view_sidemenu; + m_view_sidemenu = NULL; + + if(m_viz) + { + m_viz->set_x_start(0); + m_viz->recreate_win(m_screenh - 3); + } + else if(m_spectro) + { + switch_view(false); + } + + render(); + } + + break; + case '/': + case 6: // CTRL+F + m_search_caller_interface = NULL; + m_output_searching = true; + //m_manual_search_text = ""; + m_cursor_pos = 0; + curs_set(1); + render(); + break; + case KEY_F(9): + case '>': // sort columns + if(m_view_sidemenu != NULL) + { + break; + } + if(m_view_sort_sidemenu == NULL) + { + m_viz->set_x_start(VIEW_SIDEMENU_WIDTH); + m_sidemenu_sorting_col = m_datatable->get_sorting_col() -1; + m_view_sort_sidemenu = new curses_table_sidemenu(curses_table_sidemenu::ST_COLUMNS, + this, m_sidemenu_sorting_col, VIEW_SIDEMENU_WIDTH); + + populate_view_cols_sidemenu(); + m_view_sort_sidemenu->set_title("Select sort column"); + + m_viz->set_x_start(VIEW_SIDEMENU_WIDTH); + m_viz->recreate_win(m_screenh - 3); + render(); + m_viewinfo_page = NULL; + } + else + { + m_viz->set_x_start(0); + delete m_view_sort_sidemenu; + m_view_sort_sidemenu = NULL; + m_viz->set_x_start(0); + m_viz->recreate_win(m_screenh - 3); + m_viz->render(true); + m_viz->render(true); + render(); + } + + break; + case '\\': + case KEY_F(4): + m_search_caller_interface = NULL; + m_output_filtering = true; + m_cursor_pos = 0; + curs_set(1); + render(); + break; + case KEY_F(5): + case 'e': + if(m_datatable == NULL) + { + // + // No F5 for non table displays + // + return STA_NONE; + } + else if(m_datatable->m_type == sinsp_table::TT_LIST) + { + // + // No F5 for list tables + // + return STA_NONE; + } + + if(m_datatable->m_sample_data != NULL && m_datatable->m_sample_data->size() != 0) + { + m_selected_view_sidemenu_entry = 0; + m_selected_action_sidemenu_entry = 0; + return STA_SPY; + } + break; + case KEY_F(6): + case 'd': + if(m_datatable == NULL) + { + // + // No F5 for non table displays + // + return STA_NONE; + } + else if(m_datatable->m_type == sinsp_table::TT_LIST) + { + // + // No F5 for list tables + // + return STA_NONE; + } + + if(m_datatable->m_sample_data != NULL && m_datatable->m_sample_data->size() != 0) + { + m_selected_view_sidemenu_entry = 0; + m_selected_action_sidemenu_entry = 0; + return STA_DIG; + } + + break; + case KEY_F(7): + m_viewinfo_page = new curses_viewinfo_page(this, + m_selected_view, + 0, + 0, + m_screenh, + m_screenw); + break; + case KEY_F(8): + if(m_view_sidemenu != NULL) + { + break; + } + + if(!m_viz) + { + ASSERT(false); + break; + } + + if(m_action_sidemenu == NULL) + { + m_viz->set_x_start(ACTION_SIDEMENU_WIDTH); + m_action_sidemenu = new curses_table_sidemenu(curses_table_sidemenu::ST_ACTIONS, + this, m_selected_action_sidemenu_entry, ACTION_SIDEMENU_WIDTH); + populate_action_sidemenu(); + m_action_sidemenu->set_title("Select Action"); + + m_viz->set_x_start(ACTION_SIDEMENU_WIDTH); + m_viz->recreate_win(m_screenh - 3); + + render(); + + m_viewinfo_page = NULL; + } + else + { + m_viz->set_x_start(0); + delete m_action_sidemenu; + m_action_sidemenu = NULL; + m_viz->set_x_start(0); + m_viz->recreate_win(m_screenh - 3); + m_viz->render(true); + m_viz->render(true); + render(); + } + + break; + case KEY_RESIZE: + getmaxyx(stdscr, m_screenh, m_screenw); + + render(); + + if(m_spy_box) + { + m_spy_box->render(); + m_spy_box->render(); + } + + if(m_viz != NULL) + { + m_viz->recreate_win(m_screenh - 3); + m_viz->render(true); + m_viz->render(true); + } + else if(m_spectro) + { + m_spectro->recreate_win(m_screenh - 3); + m_spectro->render(true); + m_spectro->render(true); + } + + if(m_viewinfo_page) + { + m_viewinfo_page->render(); + m_viewinfo_page->render(); + } + + render(); + + break; + case KEY_MOUSE: + { + MEVENT* event = NULL; + + if(m_view_sidemenu != NULL) + { + event = &m_view_sidemenu->m_last_mevent; + } + else if(m_view_sort_sidemenu != NULL) + { + event = &m_view_sort_sidemenu->m_last_mevent; + } + else if(m_action_sidemenu != NULL) + { + event = &m_action_sidemenu->m_last_mevent; + } + else if(m_spy_box != NULL) + { + event = &m_spy_box->m_last_mevent; + } + else if(m_viz != NULL) + { + event = &m_viz->m_last_mevent; + } + else if(m_spectro != NULL) + { + event = &m_spectro->m_last_mevent; + } + + if(event == NULL) + { + ASSERT(false); + break; + } + + if(event->bstate & BUTTON1_CLICKED || + event->bstate & BUTTON1_DOUBLE_CLICKED) + { + if((uint32_t)event->y == m_screenh - 1) + { + int keyc = m_mouse_to_key_list.get_key_from_coordinates(event->x, event->y); + if(keyc != -1) + { + return handle_input(keyc); + } + } + else if((uint32_t)event->y == 1 && + (uint32_t)event->x >= m_filterstring_start_x && + (uint32_t)event->x <= m_filterstring_end_x) + { + m_search_caller_interface = NULL; + m_is_filter_sysdig = true; + m_output_filtering = true; + m_manual_filter = m_complete_filter; + m_cursor_pos = 0; + curs_set(1); + render(); + } + } + } + + break; + default: + break; + } + + return STA_NONE; +} + +#endif // NOCURSESUI + +int32_t sinsp_cursesui::get_viewnum_by_name(string name) +{ + for(uint32_t j = 0; j < m_views.size(); ++j) + { + if(m_views.at(j)->m_id == name) + { + return j; + } + } + + return -1; +} + +// +// Note: +// - The return value determines if the application should quit. +// - res is set to false in case of error +// +bool sinsp_cursesui::handle_stdin_input(bool* res) +{ + string input; + + *res = true; + + // + // Get the user json input + // + while(true) + { + std::getline(std::cin, input); + if(input != "") + { + break; + } + } + + // + // Parse the input + // + Json::Value root; + Json::Reader reader; + bool pres = reader.parse(input, + root, + false); + + if(!pres) + { + fprintf(stderr, "unable to parse the json input: %s", + reader.getFormattedErrorMessages().c_str()); + *res = false; + return false; + } + + string astr = root["action"].asString(); + Json::Value args = root["args"]; + + sysdig_table_action ta; + uint32_t rownum = 0; + + if(astr == "apply") + { + ta = STA_SWITCH_VIEW; + + string vname = args["view"].asString(); + + m_selected_view = get_viewnum_by_name(vname); + if(m_selected_view == -1) + { + fprintf(stderr, "unknown view: %s", vname.c_str()); + *res = false; + return false; + } + } + else if(astr == "drilldown") + { + ta = STA_DRILLDOWN; + + rownum = args["rownum"].asInt(); + } + else if(astr == "drillup") + { + ta = STA_DRILLUP; + } + else if(astr == "quit") + { + return true; + } + else + { + fprintf(stderr, "invalid action: %s", astr.c_str()); + *res = false; + return false; + } + + bool tres; + execute_table_action(ta, rownum, &tres); + return false; +} + +uint64_t sinsp_cursesui::get_time_delta() +{ + if(m_inspector->is_live()) + { + return m_refresh_interval_ns; + } + else + { + return m_last_evt_ts - m_1st_evt_ts; + } +} + +void sinsp_cursesui::run_action(sinsp_view_action_info* action) +{ + string resolved_command; + bool replacing = false; + string fld_to_replace; + +#ifndef NOCURSESUI + ASSERT(m_viz != NULL); + ASSERT(m_spectro == NULL); + + if(m_viz->get_data_size() == 0) + { + // + // No elements in the table means no selection + // + return; + } +#endif // NOCURSESUI + + // + // Scan the command string and replace the field names with the values from the selection + // + for(uint32_t j = 0; j < action->m_command.size(); j++) + { + char sc = action->m_command[j]; + + if(sc == '%') + { + fld_to_replace = ""; + + if(replacing) + { + throw sinsp_exception("the following command has the wrong syntax: " + action->m_command); + } + + replacing = true; + } + else + { + if(replacing) + { + if(sc == ' ' || sc == '\t' || sc == '0') + { + replacing = false; +#ifndef NOCURSESUI + string val = m_viz->get_field_val(fld_to_replace); + resolved_command += val; +#endif // NOCURSESUI + resolved_command += sc; + } + else + { + fld_to_replace += sc; + } + } + else + { + resolved_command += sc; + } + } + } + + if(replacing) + { +#ifndef NOCURSESUI + string val = m_viz->get_field_val(fld_to_replace); + resolved_command += val; +#endif // NOCURSESUI + } + + g_logger.format("original command: %s", action->m_command.c_str()); + g_logger.format("running command: %s", resolved_command.c_str()); + +#ifndef NOCURSESUI + // + // Exit curses mode + // + endwin(); +#endif // NOCURSESUI + + // + // If needed, ask for confirmation + // + if(action->m_ask_confirmation) + { + printf("Confirm command '%s'? [y/N] ", resolved_command.c_str()); + fflush(stdout); + + // + // Wait for the enter key + // + while(int c = getch()) + { + if(c == -1) + { + do_sleep(10000); + continue; + } + else if(c == 'y' || c == 'Y') + { + break; + } + else + { + goto action_end; + } + } + } + + // + // Run the command + // + { + int sret = system(resolved_command.c_str()); + if(sret == -1) + { + g_logger.format("command failed"); + } + } + + // + // If needed, wait for the command to complete + // + if(action->m_waitfinish) + { + printf("Command finished. Press ENTER to return to csysdig."); + fflush(stdout); + + // + // Wait for the enter key + // + while(getch() == -1) + { + do_sleep(10000); + } + } + +action_end: + // + // Empty the keyboard buffer + // + while(getch() != -1); + +#ifndef NOCURSESUI + // + // Reenter curses mode + // + reset_prog_mode(); + + // + // Refresh the screen + // + render(); +#endif // NOCURSESUI +} + +#ifndef NOCURSESUI +bool sinsp_cursesui::is_spectro_paused(int input) +{ + if(m_spectro == NULL) + { + return false; + } + + if(input == ' ') + { + m_spectro->m_scroll_paused = false; + } + + return m_spectro->m_scroll_paused; +} +#endif // NOCURSESUI + +// +// Returns true if the caller should return immediatly after calling us. +// In that case, res is filled with the result. +// +bool sinsp_cursesui::execute_table_action(sysdig_table_action ta, uint32_t rownumber, bool* res) +{ + // + // Some events require that we perform additional actions + // + switch(ta) + { + case STA_QUIT: + *res = true; + return true; + case STA_SWITCH_VIEW: + switch_view(false); + *res = false; + return true; + case STA_SWITCH_SPY: + switch_view(true); + *res = false; + return true; + case STA_DRILLDOWN: + { +#ifndef NOCURSESUI + if(m_viz != NULL) + { + sinsp_view_column_info* kinfo = get_selected_view()->get_key(); + + // + // Note: kinfo is null for list views, which currently don't support + // drill down + // + if(kinfo != NULL) + { + auto res = m_datatable->get_row_key_name_and_val(m_viz->m_selct, false); + if(res.first != NULL) + { + drilldown(kinfo->get_filter_field(m_view_depth), + res.second.c_str(), + kinfo, + res.first, + false); + } + } + } + else +#endif + { + if(m_output_type == sinsp_table::OT_CURSES) + { + drilldown("", "", NULL, NULL, false); + } + else + { + sinsp_view_column_info* kinfo = get_selected_view()->get_key(); + auto res = m_datatable->get_row_key_name_and_val(rownumber, false); + if(res.first != NULL) + { + drilldown(kinfo->get_filter_field(m_view_depth), + res.second.c_str(), + kinfo, + res.first, + false); + } + } + } + } + + *res = false; + return true; + case STA_DRILLDOWN_TEMPLATE: + { + sinsp_view_column_info* kinfo = get_selected_view()->get_key(); + auto res = m_datatable->get_row_key_name_and_val(0, true); + if(res.first != NULL) + { + drilldown(kinfo->get_filter_field(m_view_depth), + res.second.c_str(), + kinfo, + res.first, + true); + } + } + + *res = false; + return true; + case STA_DRILLUP: + drillup(); + + *res = false; + return true; +#ifndef NOCURSESUI + case STA_SPECTRO: + case STA_SPECTRO_FILE: + { + sinsp_view_column_info* kinfo = get_selected_view()->get_key(); + + // + // Note: kinfo is null for list views, that currently don't support + // drill down + // + if(kinfo != NULL) + { + auto res = m_datatable->get_row_key_name_and_val(m_viz->m_selct, false); + if(res.first != NULL) + { + spectro_selection(get_selected_view()->get_key()->get_filter_field(m_view_depth), + res.second.c_str(), + get_selected_view()->get_key(), + res.first, ta); + } + } + } + + *res = false; + return true; +#endif + case STA_SPY: + { + pair res; +#ifndef NOCURSESUI + if(m_output_type == sinsp_table::OT_CURSES) + { + res = m_datatable->get_row_key_name_and_val(m_viz->m_selct, false); + } + else +#endif + { + res = m_datatable->get_row_key_name_and_val(rownumber, false); + } + + if(res.first != NULL) + { + spy_selection(get_selected_view()->get_key()->get_filter_field(m_view_depth), + res.second.c_str(), + get_selected_view()->get_key(), + false); + } + } + + *res = false; + return true; + case STA_DIG: + { +#ifndef NOCURSESUI + if(m_viz) + { + auto res = m_datatable->get_row_key_name_and_val(m_viz->m_selct, false); + if(res.first != NULL) + { + spy_selection(get_selected_view()->get_key()->get_filter_field(m_view_depth), + res.second.c_str(), + get_selected_view()->get_key(), + true); + } + } + else +#endif + { + if(m_output_type == sinsp_table::OT_CURSES) + { + spy_selection("", "", NULL, true); + } + else + { + auto res = m_datatable->get_row_key_name_and_val(rownumber, false); + if(res.first != NULL) + { + spy_selection(get_selected_view()->get_key()->get_filter_field(m_view_depth), + res.second.c_str(), + get_selected_view()->get_key(), + true); + } + } + } + } + + *res = false; + return true; + case STA_NONE: + break; + default: + ASSERT(false); + break; + } + + return false; +} + +#endif // CSYSDIG diff --git a/userspace/libsinsp/cursesui.h b/userspace/libsinsp/cursesui.h new file mode 100644 index 0000000000..6137fdebc0 --- /dev/null +++ b/userspace/libsinsp/cursesui.h @@ -0,0 +1,815 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#ifdef CSYSDIG + +#ifndef _WIN32 +#include +#endif + +#define UI_USER_INPUT_CHECK_PERIOD_NS 10000000 +#define VIEW_SIDEMENU_WIDTH 20 +#define ACTION_SIDEMENU_WIDTH 30 +#define FILTER_TEMPLATE_MAGIC "@#$f1CA^&;" +string combine_filters(string flt1, string flt2); +class ctext; +class sinsp_chart; +class curses_spectro; +extern sinsp_logger g_logger; + +class sinsp_menuitem_info +{ +public: + enum type + { + TABLE = 1, + LIST = 2, + ALL = TABLE | LIST, + }; + + sinsp_menuitem_info(string key, + string desc, + sinsp_menuitem_info::type type, + int keyboard_equivalent) + { + m_key = key; + m_desc = desc; + m_type = type; + m_keyboard_equivalent = keyboard_equivalent; + } + + string m_key; + string m_desc; + sinsp_menuitem_info::type m_type; + int m_keyboard_equivalent; +}; + +class sinsp_ui_selection_info +{ +public: + sinsp_ui_selection_info(string field, + string val, + sinsp_view_column_info* column_info, + string view_filter, + uint32_t prev_selected_view, + uint32_t prev_selected_sidemenu_entry, + sinsp_table_field* rowkey, + uint32_t prev_sorting_col, + string prev_manual_filter, + bool prev_is_filter_sysdig, + bool prev_is_sorting_ascending, + bool is_drilldown) + { + m_field = field; + m_column_info = column_info; + m_val = val; + m_view_filter = view_filter; + m_prev_selected_view = prev_selected_view; + m_prev_selected_sidemenu_entry = prev_selected_sidemenu_entry; + m_prev_sorting_col = prev_sorting_col; + m_prev_manual_filter = prev_manual_filter; + m_prev_is_filter_sysdig = prev_is_filter_sysdig; + m_prev_is_sorting_ascending = prev_is_sorting_ascending; + m_is_drilldown = is_drilldown; + + if(rowkey != NULL) + { + m_rowkey = *rowkey; + } + else + { + m_rowkey.m_len = 0; + m_rowkey.m_val = NULL; + } + } + + string m_field; + string m_val; + sinsp_view_column_info* m_column_info; + string m_view_filter; + uint32_t m_prev_selected_view; + uint32_t m_prev_selected_sidemenu_entry; + uint32_t m_prev_sorting_col; + string m_prev_manual_filter; + bool m_prev_is_filter_sysdig; + sinsp_table_field m_rowkey; + bool m_prev_is_sorting_ascending; + bool m_is_drilldown; +}; + +class sinsp_ui_selection_hierarchy +{ +public: + void push_back(string field, + string val, + sinsp_view_column_info* column_info, + string view_filter, + uint32_t prev_selected_view, + uint32_t prev_selected_sidemenu_entry, + sinsp_table_field* rowkey, + uint32_t prev_sorting_col, + string prev_manual_filter, + bool prev_is_filter_sysdig, + bool prev_is_sorting_ascending, + bool is_drilldown) + { + m_hierarchy.push_back(sinsp_ui_selection_info(field, + val, + column_info, + view_filter, + prev_selected_view, + prev_selected_sidemenu_entry, + rowkey, + prev_sorting_col, + prev_manual_filter, + prev_is_filter_sysdig, + prev_is_sorting_ascending, + is_drilldown)); + } + + ~sinsp_ui_selection_hierarchy() + { + for(auto e : m_hierarchy) + { + if(e.m_rowkey.m_val != NULL) + { + delete [] e.m_rowkey.m_val; + } + } + } + + string tofilter(bool templated) + { + string res; + uint32_t j; + uint32_t hs = (uint32_t)m_hierarchy.size(); + + for(j = 0; j < hs; j++) + { + bool has_filter = false; + uint32_t lastsize = res.size(); + + if(m_hierarchy[j].m_view_filter != "") + { + has_filter = true; + } + + if(hs > 1) + { + res += "("; + } + + if(has_filter) + { + res += "("; + res += m_hierarchy[j].m_view_filter; + res += ")"; + } + + if(m_hierarchy[j].m_field != "") + { + bool skip = false; + + if(m_hierarchy[j].m_column_info != NULL && + (m_hierarchy[j].m_column_info->m_flags & TEF_FILTER_IN_CHILD_ONLY)) + { + if(j < hs - 1) + { + skip = true; + } + } + + if(!skip) + { + if(has_filter) + { + res += " and "; + } + res += m_hierarchy[j].m_field; + res += "="; + + if(templated && (j == hs - 1)) + { + res += string("\"") + FILTER_TEMPLATE_MAGIC + "\""; + } + else + { + res += m_hierarchy[j].m_val; + } + } + } + + if(res.size() != lastsize) + { + if(hs > 1) + { + res += ")"; + } + + res += " and "; + + if(res.size() >= 7 && res.substr(res.size() - 7) == "() and ") + { + res = res.substr(0, res.size() - 7); + } + + } + } + + if(res.size() >= 5) + { + string trailer = res.substr(res.size() - 5).c_str(); + if(trailer == " and ") + { + res = res.substr(0, res.size() - 5); + } + } + + return res; + } + + uint32_t size() + { + return (uint32_t)m_hierarchy.size(); + } + + sinsp_ui_selection_info* at(uint32_t j) + { + return &m_hierarchy[j]; + } + + bool pop_back() + { + if(m_hierarchy.size() == 0) + { + return false; + } + else + { + m_hierarchy.pop_back(); + return true; + } + } + + +private: + vector m_hierarchy; +}; + +class sinsp_mouse_to_key_list_entry +{ +public: + sinsp_mouse_to_key_list_entry(uint32_t startx, + uint32_t starty, + uint32_t endx, + uint32_t endy, + int keyboard_equivalent) + { + m_startx = startx; + m_endx = endx; + m_starty = starty; + m_endy = endy; + m_keyboard_equivalent = keyboard_equivalent; + } + + uint32_t m_startx; + uint32_t m_endx; + uint32_t m_starty; + uint32_t m_endy; + int m_keyboard_equivalent; +}; + +class sinsp_mouse_to_key_list +{ +public: + void add(sinsp_mouse_to_key_list_entry entry) + { + m_list.push_back(entry); + } + + int get_key_from_coordinates(uint32_t x, uint32_t y) + { + for(auto e : m_list) + { + if(x >= e.m_startx && + x <= e.m_endx && + y >= e.m_starty && + y <= e.m_endy) + { + return e.m_keyboard_equivalent; + } + } + + return -1; + } + + void clear() + { + m_list.clear(); + } + + vector m_list; +}; + +class json_spy_renderer +{ +public: + json_spy_renderer(sinsp* inspector, + sinsp_cursesui* parent, + int32_t viz_type, + spy_text_renderer::sysdig_output_type sotype, + bool print_containers, + sinsp_evt::param_fmt text_fmt); + + ~json_spy_renderer(); + void set_filter(string filter); + void process_event(sinsp_evt* evt, int32_t next_res); + string get_data(); + uint64_t get_count(); + +private: + void process_event_spy(sinsp_evt* evt, int32_t next_res); + void process_event_dig(sinsp_evt* evt, int32_t next_res); + + spy_text_renderer* m_json_spy_renderer; + sinsp* m_inspector; + Json::Value m_root; + sinsp_filter* m_filter; + uint64_t m_linecnt; +}; + +class sinsp_cursesui +{ +public: + enum ColorElements_ { + RESET_COLOR, + DEFAULT_COLOR, + FUNCTION_BAR, + FUNCTION_KEY, + FAILED_SEARCH, + PANEL_HEADER_FOCUS, + PANEL_HEADER_UNFOCUS, + PANEL_HIGHLIGHT_FOCUS, + PANEL_HIGHLIGHT_UNFOCUS, + PANEL_HEADER_LIST_FOCUS, + PANEL_HEADER_LIST_HIGHLIGHT, + LARGE_NUMBER, + METER_TEXT, + METER_VALUE, + LED_COLOR, + UPTIME, + BATTERY, + TASKS_RUNNING, + SWAP, + PROCESS, + PROCESS_SHADOW, + PROCESS_TAG, + PROCESS_MEGABYTES, + PROCESS_TREE, + PROCESS_R_STATE, + PROCESS_D_STATE, + PROCESS_BASENAME, + PROCESS_HIGH_PRIORITY, + PROCESS_LOW_PRIORITY, + PROCESS_THREAD, + PROCESS_THREAD_BASENAME, + BAR_BORDER, + BAR_SHADOW, + GRAPH_BLACK, + GRAPH_WHITE, + GRAPH_WHITE_D, + GRAPH_GREEN_L, + GRAPH_GREEN, + GRAPH_GREEN_D, + GRAPH_YELLOW_L, + GRAPH_YELLOW, + GRAPH_YELLOW_D, + GRAPH_RED_L, + GRAPH_RED, + GRAPH_RED_D, + GRAPH_MAGENTA_L, + GRAPH_MAGENTA, + MEMORY_USED, + MEMORY_BUFFERS, + MEMORY_BUFFERS_TEXT, + MEMORY_CACHE, + LOAD, + LOAD_AVERAGE_FIFTEEN, + LOAD_AVERAGE_FIVE, + LOAD_AVERAGE_ONE, + CHECK_BOX, + CHECK_MARK, + CHECK_TEXT, + CLOCK, + HELP_BOLD, + HOSTNAME, + CPU_NICE, + CPU_NICE_TEXT, + CPU_NORMAL, + CPU_KERNEL, + CPU_IOWAIT, + CPU_IRQ, + CPU_SOFTIRQ, + SPY_READ, + SPY_WRITE, + LAST_COLORELEMENT + }; + + sinsp_cursesui(sinsp* inspector, string event_source_name, + string cmdline_capture_filter, uint64_t refresh_interval_ns, + bool print_containers, sinsp_table::output_type output_type, bool is_mousedrag_available, + int32_t json_first_row, int32_t json_last_row, int32_t sorting_col, + sinsp_evt::param_fmt json_spy_text_fmt); + ~sinsp_cursesui(); + void configure(sinsp_view_manager* views); + void start(bool is_drilldown, bool is_spy_switch); + sinsp_view_info* get_selected_view(); + sinsp_view_info* get_prev_selected_view(); + void pause(); + bool is_searching() + { + return m_output_filtering; + } + bool is_eof() + { + return m_eof != 0; + } + void set_truncated_input(bool truncated) + { + m_truncated_input = truncated; + } + void set_interactive(bool interactive) + { + m_interactive = interactive; + } +#ifndef NOCURSESUI + void render(); +#endif + void turn_search_on(search_caller_interface* ifc, string header_text); + uint64_t get_time_delta(); + void run_action(sinsp_view_action_info* action); + void spy_selection(string field, string val, sinsp_view_column_info* column_info, bool is_dig); + sysdig_table_action handle_input(int ch); + bool handle_stdin_input(bool* res); + + // + // Return true if the application is supposed to exit + // + inline bool process_event(sinsp_evt* evt, int32_t next_res) + { + uint64_t ts = evt->get_ts(); + if(!m_inspector->is_live()) + { + if(m_1st_evt_ts == 0) + { + m_1st_evt_ts = ts; + } + + m_last_evt_ts = ts; + } + + // + // Process the user input + // + if(m_output_type != sinsp_table::OT_JSON) + { +#ifndef NOCURSESUI + if((ts - m_last_input_check_ts > m_input_check_period_ns) || m_eof) + { + uint32_t ninputs = 0; + uint64_t evtnum = evt->get_num(); + + // + // If this is a file, print the progress once in a while + // + if(!m_inspector->is_live() && !m_offline_replay) + { + if(evtnum - m_last_progress_evt > 30000) + { + print_progress(m_inspector->get_read_progress()); + m_last_progress_evt = evtnum; + } + } + + // + // If we have more than one event in the queue, consume all of them + // + while(true) + { + int input = getch(); + bool sppaused = is_spectro_paused(input); + + if(input == -1) + { + // + // All events consumed + // + if(m_spectro) + { + if(sppaused) + { + usleep(100000); + continue; + } + else + { + break; + } + } + else + { + break; + } + } + else + { + ninputs++; + } + + // + // Handle the event + // + sysdig_table_action ta = handle_input(input); + + bool res; + if(execute_table_action(ta, 0, &res) == true) + { + return res; + } + } + + if(ninputs == 0) + { + m_last_input_check_ts = ts; + } + } +#endif + } + else + { + // + // JSON output. + // If this is a file, print the progress once in a while + // + if(!m_inspector->is_live() && !m_offline_replay) + { + uint64_t evtnum = evt->get_num(); + if(evtnum - m_last_progress_evt > 300000) + { + printf("{\"progress\": %.2lf},\n", m_inspector->get_read_progress()); + fflush(stdout); + m_last_progress_evt = evtnum; + } + } + } + + // + // We were reading from a file and we reached its end. + // Unless we are in raw mode, we keep looping because we want to handle user events, + // but we stop the processing here. We also make sure to sleep a bit to keep the + // CPU under control. + // + if(m_eof > 1) + { +#ifndef NOCURSESUI + usleep(10000); +#endif + if(m_output_type == sinsp_table::OT_RAW || m_output_type == sinsp_table::OT_JSON) + { + if(m_interactive) + { + bool sres; + return handle_stdin_input(&sres); + } + else + { + return true; + } + } + else + { + return false; + } + } + + // + // Perform event processing + // +#ifndef NOCURSESUI + if(m_spy_box) + { + m_spy_box->process_event(evt, next_res); + } + else +#endif + if(m_json_spy_renderer) + { + m_json_spy_renderer->process_event(evt, next_res); + + uint64_t evtnum = evt->get_num(); + if((evtnum - m_last_progress_evt > 2000) || (next_res == SCAP_EOF)) + { + string jdata = m_json_spy_renderer->get_data(); + double rprogress = m_inspector->get_read_progress(); + printf("{\"progress\": %.2lf,", rprogress); + if(next_res == SCAP_EOF) + { + printf(" \"count\": %" PRIu64 ", ", m_json_spy_renderer->get_count()); + } + printf("\"data\": %s", jdata.c_str()); + printf("}"); + + if(next_res != SCAP_EOF) + { + printf(","); + } + + fflush(stdout); + + m_last_progress_evt = evtnum; + } + + // + // Check if this the end of the capture file, and if yes take note of that + // + if(next_res == SCAP_EOF) + { + ASSERT(!m_inspector->is_live()); + m_eof++; + return true; + } + } + else + { + bool end_of_sample; + + // + // Check if it's time to flush + // + if(m_inspector->is_live() || m_offline_replay) + { + if(next_res == SCAP_EOF) + { + end_of_sample = true; + } + else + { + end_of_sample = (evt == NULL || ts > m_datatable->m_next_flush_time_ns); + } + } + else + { + // + // For files, we flush only once, at the end of the capture. + // + if(next_res == SCAP_EOF) + { + end_of_sample = true; + } + else + { + end_of_sample = false; + } + } + + if(end_of_sample) + { + handle_end_of_sample(evt, next_res); + + // + // Check if this the end of the capture file, and if yes take note of that + // + if(next_res == SCAP_EOF) + { + ASSERT(!m_inspector->is_live()); + m_eof++; + return false; + } + } + + m_datatable->process_event(evt); + } + + return false; + } + + sinsp_table* m_datatable; + int m_colors[LAST_COLORELEMENT]; + sinsp_view_manager m_views; + int32_t m_selected_view; + int32_t m_prev_selected_view; + uint32_t m_selected_view_sidemenu_entry; + uint32_t m_selected_action_sidemenu_entry; + uint32_t m_selected_view_sort_sidemenu_entry; + sinsp_ui_selection_hierarchy m_sel_hierarchy; + uint32_t m_screenw; + uint32_t m_screenh; + uint32_t m_eof; + uint64_t m_input_check_period_ns; + bool m_search_nomatch; + bool m_print_containers; + int32_t m_sidemenu_sorting_col; +#ifndef NOCURSESUI + curses_table* m_viz; + curses_spectro* m_spectro; + curses_table_sidemenu* m_view_sidemenu; + curses_table_sidemenu* m_action_sidemenu; + curses_viewinfo_page* m_viewinfo_page; + curses_table_sidemenu* m_view_sort_sidemenu; + curses_mainhelp_page* m_mainhelp_page; + curses_textbox* m_spy_box; + sinsp_evt::param_fmt m_spybox_text_format; +#endif + bool m_offline_replay; + uint64_t m_refresh_interval_ns; + sinsp* m_inspector; + uint32_t m_view_depth; + bool m_is_mousedrag_available; + +private: + Json::Value generate_json_info_section(); + void handle_end_of_sample(sinsp_evt* evt, int32_t next_res); + void restart_capture(bool is_spy_switch); + void switch_view(bool is_spy_switch); + bool spectro_selection(string field, string val, sinsp_view_column_info* column_info, filtercheck_field_info* info, sysdig_table_action ta); + bool do_drilldown(string field, string val, sinsp_view_column_info* column_info, uint32_t new_view_num, filtercheck_field_info* info, bool dont_restart); + // returns false if there is no suitable drill down view for this field + bool drilldown(string field, string val, sinsp_view_column_info* column_info, filtercheck_field_info* info, bool dont_restart); + // returns false if we are already at the top of the hierarchy + bool drillup(); + void create_complete_filter(bool templated); + bool execute_table_action(sysdig_table_action ta, uint32_t rownumber, bool* res); + int32_t get_viewnum_by_name(string name); + +#ifndef NOCURSESUI + void render_header(); + void draw_bottom_menu(vector* items, bool istable); + void render_default_main_menu(); + void render_filtersearch_main_menu(); + void render_spy_main_menu(); + void render_position_info(); + void render_main_menu(); + sysdig_table_action handle_textbox_input(int ch); + void populate_view_sidemenu(string field, vector* viewlist); + void populate_action_sidemenu(); + void populate_view_cols_sidemenu(); + void print_progress(double progress); + void show_selected_view_info(); + bool is_spectro_paused(int input); +#endif + + vector m_menuitems; + vector m_menuitems_spybox; + string m_event_source_name; + string m_cmdline_capture_filter; + string m_complete_filter; + string m_complete_filter_noview; + string m_manual_filter; + string m_manual_search_text; + bool m_paused; + uint64_t m_last_input_check_ts; + bool m_output_filtering; + bool m_output_searching; + uint32_t m_cursor_pos; + bool m_is_filter_sysdig; + uint64_t m_last_progress_evt; + vector m_sidemenu_viewlist; + sinsp_chart* m_chart; + search_caller_interface* m_search_caller_interface; + int32_t m_search_start_x, m_search_start_y; + uint64_t m_n_evts_in_file; + uint64_t m_1st_evt_ts; + uint64_t m_last_evt_ts; + uint64_t m_evt_ts_delta; + sinsp_filter_check_reference* m_timedelta_formatter; + sinsp_mouse_to_key_list m_mouse_to_key_list; + uint32_t m_filterstring_start_x; + uint32_t m_filterstring_end_x; + string m_search_header_text; + sinsp_table::output_type m_output_type; + bool m_truncated_input; + bool m_interactive; + int32_t m_json_first_row; + int32_t m_json_last_row; + int32_t m_sorting_col; + json_spy_renderer* m_json_spy_renderer; + sinsp_evt::param_fmt m_json_spy_text_fmt; +}; + +#endif // CSYSDIG diff --git a/userspace/libsinsp/cyclewriter.cpp b/userspace/libsinsp/cyclewriter.cpp new file mode 100644 index 0000000000..438e7554cd --- /dev/null +++ b/userspace/libsinsp/cyclewriter.cpp @@ -0,0 +1,264 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "sinsp.h" +#include "sinsp_int.h" +#include "cyclewriter.h" + +cycle_writer::cycle_writer(bool is_live) : + m_base_file_name(""), + m_rollover_mb(0L), + m_duration_seconds(0), + m_file_limit(0), + m_event_limit(0L), + m_last_time(0), + m_file_count_total(0), + m_file_index(0), + m_first_consider(false), + m_event_count(0L), + m_dumper(NULL), + m_past_names(NULL) +{ + // + // null terminate the first + // character of the limit format + // to say that we want things to + // be created when we consider() the + // next file. + // + m_limit_format[0] = 0; + this->live = is_live; +} + +bool cycle_writer::setup(string base_file_name, int rollover_mb, int duration_seconds, int file_limit, unsigned long event_limit, scap_dumper_t** dumper) +{ + if(m_first_consider) + { + return false; + } + m_base_file_name = base_file_name; + m_rollover_mb = rollover_mb * 1000000L; + m_duration_seconds = duration_seconds; + m_file_limit = file_limit; + m_event_limit = event_limit; + m_dumper = dumper; + + if(duration_seconds > 0 && file_limit > 0) + { + m_past_names = new string[file_limit]; + + for(int32_t j = 0; j < file_limit; j++) + { + m_past_names[j] = ""; + } + } + + // + // Seed the filename with an initial + // value. + // + consider(NULL); + return true; +} + +// +// consider a certain number of bytes given the parameters +// passed in through setup. Consider will recommend one +// of the following: +// +// * SAMEFILE - use the same file +// * NEWFILE - use a new file (inquiry with get_current_file_name()) +// * DOQUIT - end the capture. +// +cycle_writer::conclusion cycle_writer::consider(sinsp_evt* evt) +{ + if(m_first_consider == false) + { + m_first_consider = true; + } + + if(evt == NULL) // First run + { + if(!live && m_duration_seconds > 0 && m_base_file_name.find("%") != string::npos) // Here's the fuckin' bug + m_last_file_name = "first_dump.scap"; + else + { + if(live) m_last_time = time(NULL); + next_file(); + } + return NEWFILE; + } + + m_event_count++; + + if(m_duration_seconds > 0) + { + // + // If this is our first consideration, + // we set the timer up. + // + if(m_last_time == 0) + { + m_last_time = evt->get_ts() / 1000000000; // 10^(-9) because it's nanoseconds + } + + if((int)difftime(evt->get_ts() / 1000000000, m_last_time) >= m_duration_seconds) + { + m_last_time = evt->get_ts() / 1000000000; + m_last_reason = "Maximum Time Reached"; + return next_file(); + } + } + + if(m_rollover_mb > 0 && scap_dump_get_offset(*m_dumper) > m_rollover_mb) + { + m_last_reason = "Maximum File Size Reached"; + return next_file(); + } + + // Event limit + if(m_event_limit > 0 && m_event_count >= m_event_limit) + { + m_event_count = 0L; + m_last_reason = "Maximum Event Number Reached"; + return next_file(); + } + + // + // This is for any routine which restricts + // execution after an initial consider() + // + /*if(m_first_consider == false) + { + m_first_consider = true; + + // We need to generate an initial file name + // but still continue our logic. + next_file(); + }*/ + + return SAMEFILE; +} + +string cycle_writer::get_current_file_name() +{ + return m_last_file_name; +} + +// +// next_file doesn't return the file pointer +// instead it returns advice on whether a new +// file should be used or not. +// +// If it advices a new file, then the new file +// name advised can be found in the +// get_current_file_name() routine. +// +cycle_writer::conclusion cycle_writer::next_file() +{ + if (m_file_limit > 0 && m_file_index >= m_file_limit) + { + m_file_index = 0; + } + + + if(m_duration_seconds > 0) + { + // if the user has specified a format then use it + if(m_base_file_name.find("%") != string::npos) + { + const size_t our_size = 4096; + size_t their_size; + char file_name[our_size]; + const struct tm *our_time = localtime(&m_last_time); + + their_size = strftime(file_name, our_size, m_base_file_name.c_str(), our_time); + + if(their_size == 0) {/* TODO: if fail but as string size has been increased to 4096 it's very unlikely we get here */ } + + if(m_file_limit > 0) + { + if(m_past_names[m_file_index] != "") + { + remove(m_past_names[m_file_index].c_str()); + } + + m_past_names[m_file_index] = string(file_name); + } + + m_last_file_name = file_name; + } + else // if no format is provided, then use a counter + { + m_last_file_name = m_base_file_name + to_string(m_file_index); + } + } + else + { + m_last_file_name = m_base_file_name; + } + + if(m_rollover_mb > 0) + { + + if(m_limit_format[0] == 0) // I have no idea if this part is executed and, if so, if it works correctly + { + int digit_count = 0; + int our_file_limit = m_file_limit; + + while(our_file_limit > 0) + { + digit_count++; + our_file_limit /= 10; + } + + snprintf( + // The format we are trying to derive + m_limit_format, + sizeof(m_limit_format), + + // + // Read the string below like this: + // + // %05d + // + // Which is what we want. + // + "%%0%dd", + + digit_count + ); + } + + char index[22]; + + snprintf(index, sizeof(index), m_limit_format, m_file_index); + + m_last_file_name += index; + } + + if(m_event_limit > 0) + { + m_last_file_name = m_base_file_name + to_string(m_file_index); + } + + m_file_count_total++; + m_file_index++; + + return NEWFILE; +} diff --git a/userspace/libsinsp/cyclewriter.h b/userspace/libsinsp/cyclewriter.h new file mode 100644 index 0000000000..e1cf1d643f --- /dev/null +++ b/userspace/libsinsp/cyclewriter.h @@ -0,0 +1,173 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include +#include +#include + +using namespace std; + +class cycle_writer { +public: + // + // The conclusion is what consider() tells you to do + // after you tell it how many more bytes to consider. + // + // We don't deal directly with file pointers here to + // keep the concerns separated. This engine only advises + // a course of action. + // + enum conclusion { + + // Continue to use the same file + SAMEFILE, + + // Close the current file handle + // and use the one in get_current_file_name() + NEWFILE, + + // It's the end of the capture, + // close the file handle and exit. + // (actually never used, but kept for future + // implementations and backwards compatibility) + DOQUIT + }; + + cycle_writer(bool is_live); + ~cycle_writer() + { + if(m_past_names != NULL) + { + delete[] m_past_names; + } + }; + + // + // Setup sets all the parameters of the + // engine in one go so you don't miss anything. + // + // Also, if the engine has already started + // (via a call to consider()), then this will + // be locked down and return false. + // + bool setup(string base_file_name, int rollover_mb, int duration_seconds, int file_limit, unsigned long event_limit, scap_dumper_t** dumper); + + // + // Consider file size at the current time + // and tell us whether + // + // * a new file should be written, + // * we should use the same file, or + // * we should quit. + // + // If we should use a new file, or should quit, + // then m_last_reason will tell us why consider + // thought so, and in the case of a new file, + // get_current_file_name() will tell us the new + // capture file name to use. + // + cycle_writer::conclusion consider(sinsp_evt* evt); + + // + // The yields the current file name + // based on the input parameters and + // what has been past into consider. + // + string get_current_file_name(); + + // Last reason for a new file + string m_last_reason; + +private: + // + // This will yield a new file if + // needed or conclude that we need + // to exit. + // + cycle_writer::conclusion next_file(); + + // + // These are the variables that are set + // to specify how the engine will work. + // + // Use the setup() function to set them up. + // + // values <= 0 mean don't use the feature. + // + + // The base file name to write to + string m_base_file_name; // = "" + + // Number of bytes before rolling over + int64_t m_rollover_mb; // = 0 + + // Time in seconds between captures + int m_duration_seconds; // = 0 + + // Total number of allowed captures + int m_file_limit; // = 0 + + // Event division: every how many event + // should I split the file? + unsigned long m_event_limit; // = 0L + +private: + + // Last time of a capture + time_t m_last_time; + + // Total number of files written + int m_file_count_total; + + // Current index + int m_file_index; + + // + // This is the 0-left padded format + // for creating file names. Since + // we don't know what's what at first + // we just leave this hanging. + // + char m_limit_format[6]; + + // The last file name that + // was created (mostly for debugging) + string m_last_file_name; + + // + // This is toggled to true the + // first time consider is run ... + // it will lock the setup() from + // being further run + // + bool m_first_consider; // = false + + // number of events + unsigned long m_event_count; // = 0L + + scap_dumper_t** m_dumper; + + bool live; + + // Used when ciclewriting on time with specified + // name format for deleting "out-of-range" files + string *m_past_names; +}; + diff --git a/userspace/libsinsp/dns_manager.cpp b/userspace/libsinsp/dns_manager.cpp new file mode 100644 index 0000000000..8e9738cd60 --- /dev/null +++ b/userspace/libsinsp/dns_manager.cpp @@ -0,0 +1,202 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "dns_manager.h" + +void sinsp_dns_resolver::refresh(uint64_t erase_timeout, uint64_t base_refresh_timeout, uint64_t max_refresh_timeout, std::future f_exit) +{ +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + sinsp_dns_manager &manager = sinsp_dns_manager::get(); + while(true) + { + if(!manager.m_cache.empty()) + { + std::list to_delete; + + uint64_t ts = sinsp_utils::get_current_time_ns(); + + for(auto &it: manager.m_cache) + { + const std::string &name = it.first; + sinsp_dns_manager::dns_info &info = it.second; + + if((ts > info.m_last_used_ts) && + (ts - info.m_last_used_ts) > erase_timeout) + { + // remove the entry if it's hasn't been used for a whole hour + to_delete.push_back(name); + } + else if(ts > (info.m_last_resolve_ts + info.m_timeout)) + { + sinsp_dns_manager::dns_info refreshed_info = manager.resolve(name, ts); + refreshed_info.m_timeout = base_refresh_timeout; + refreshed_info.m_last_resolve_ts = info.m_last_resolve_ts = ts; + + // dns_info::operator!= will check if some + // v4 or v6 addresses are changed from the + // last resolution + if(refreshed_info != info) + { + info = refreshed_info; + } + else if(info.m_timeout < max_refresh_timeout) + { + // double the timeout until 320 secs + info.m_timeout <<= 1; + } + } + } + if(!to_delete.empty()) + { + manager.m_erase_mutex.lock(); + for(const auto &name : to_delete) + { + manager.m_cache.unsafe_erase(name); + } + manager.m_erase_mutex.unlock(); + } + } + + if(f_exit.wait_for(std::chrono::nanoseconds(base_refresh_timeout)) == std::future_status::ready) + { + break; + } + } +#endif +} + +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) +inline sinsp_dns_manager::dns_info sinsp_dns_manager::resolve(const std::string &name, uint64_t ts) +{ + dns_info dinfo; + + struct addrinfo hints, *result, *rp; + memset(&hints, 0, sizeof(struct addrinfo)); + + // Allow IPv4 or IPv6, all socket types, all protocols + hints.ai_family = AF_UNSPEC; + + int s = getaddrinfo(name.c_str(), NULL, &hints, &result); + if (!s && result) + { + for (rp = result; rp != NULL; rp = rp->ai_next) + { + if(rp->ai_family == AF_INET) + { + dinfo.m_v4_addrs.insert(((struct sockaddr_in*)rp->ai_addr)->sin_addr.s_addr); + } + else // AF_INET6 + { + ipv6addr v6; + memcpy(v6.m_b, ((struct sockaddr_in6*)rp->ai_addr)->sin6_addr.s6_addr, sizeof(ipv6addr)); + dinfo.m_v6_addrs.insert(v6); + } + } + freeaddrinfo(result); + } + return dinfo; +} +#endif + +bool sinsp_dns_manager::match(const char *name, int af, void *addr, uint64_t ts) +{ +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + if(!m_resolver) + { + m_resolver = new thread(sinsp_dns_resolver::refresh, m_erase_timeout, m_base_refresh_timeout, m_max_refresh_timeout, m_exit_signal.get_future()); + } + + string sname = string(name); + + m_erase_mutex.lock(); + + if(m_cache.find(sname) == m_cache.end()) + { + dns_info dinfo = resolve(sname, ts); + dinfo.m_timeout = m_base_refresh_timeout; + dinfo.m_last_resolve_ts = ts; + m_cache[sname] = dinfo; + } + + m_cache[sname].m_last_used_ts = ts; + dns_info &dinfo = m_cache[sname]; + + m_erase_mutex.unlock(); + + if(af == AF_INET6) + { + ipv6addr v6; + memcpy(v6.m_b, addr, sizeof(ipv6addr)); + return dinfo.m_v6_addrs.find(v6) != dinfo.m_v6_addrs.end(); + } + else if(af == AF_INET) + { + return dinfo.m_v4_addrs.find(*(uint32_t *)addr) != dinfo.m_v4_addrs.end(); + } +#endif + return false; +} + +string sinsp_dns_manager::name_of(int af, void *addr, uint64_t ts) +{ + string ret; + +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + if(!m_cache.empty()) + { + m_erase_mutex.lock(); + for(auto &it: m_cache) + { + const std::string &name = it.first; + sinsp_dns_manager::dns_info &info = it.second; + + if(af == AF_INET6) + { + ipv6addr v6; + memcpy(v6.m_b, addr, sizeof(ipv6addr)); + if (info.m_v6_addrs.find(v6) != info.m_v6_addrs.end()) + { + info.m_last_used_ts = ts; + ret = name; + break; + } + } + else if(af == AF_INET && info.m_v4_addrs.find(*(uint32_t *)addr) != info.m_v4_addrs.end()) + { + info.m_last_used_ts = ts; + ret = name; + break; + } + } + m_erase_mutex.unlock(); + } +#endif + return ret; +} + +void sinsp_dns_manager::cleanup() +{ + if(m_resolver) + { + m_exit_signal.set_value(); + m_resolver->join(); + m_resolver = NULL; + m_exit_signal = std::promise(); + } +} diff --git a/userspace/libsinsp/dns_manager.h b/userspace/libsinsp/dns_manager.h new file mode 100644 index 0000000000..9c3da91a57 --- /dev/null +++ b/userspace/libsinsp/dns_manager.h @@ -0,0 +1,130 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32) +#include +#else +#include +#include +#endif +#include +#include +#include +#include +#include +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) +#include "tbb/concurrent_unordered_map.h" +#endif +#include "sinsp.h" + + +struct sinsp_dns_resolver +{ + static void refresh(uint64_t erase_timeout, uint64_t base_refresh_timeout, uint64_t max_refresh_timeout, std::future f_exit); +}; + +class sinsp_dns_manager +{ +public: + + bool match(const char *name, int af, void *addr, uint64_t ts); + string name_of(int af, void *addr, uint64_t ts); + + void cleanup(); + + static sinsp_dns_manager& get() + { + static sinsp_dns_manager instance; + return instance; + }; + + void set_erase_timeout(uint64_t ns) + { + m_erase_timeout = ns; + }; + void set_base_refresh_timeout(uint64_t ns) + { + m_base_refresh_timeout = ns; + }; + void set_max_refresh_timeout(uint64_t ns) + { + m_max_refresh_timeout = ns; + }; + + size_t size() + { +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + return m_cache.size(); +#else + return 0; +#endif + }; + +private: + + sinsp_dns_manager() : + m_erase_timeout(3600 * ONE_SECOND_IN_NS), + m_base_refresh_timeout(10 * ONE_SECOND_IN_NS), + m_max_refresh_timeout(320 * ONE_SECOND_IN_NS) + {}; + sinsp_dns_manager(sinsp_dns_manager const&) = delete; + void operator=(sinsp_dns_manager const&) = delete; + +#if defined(HAS_CAPTURE) && !defined(CYGWING_AGENT) + struct dns_info + { + bool operator==(const dns_info &other) const + { + return m_v4_addrs == other.m_v4_addrs && m_v6_addrs == other.m_v6_addrs; + }; + bool operator!=(const dns_info &other) const + { + return !operator==(other); + }; + + uint64_t m_timeout; + uint64_t m_last_resolve_ts; + uint64_t m_last_used_ts; + std::set m_v4_addrs; + std::set m_v6_addrs; + }; + + static inline dns_info resolve(const std::string &name, uint64_t ts); + + typedef tbb::concurrent_unordered_map c_dns_table; + c_dns_table m_cache; +#endif + + // tbb concurrent unordered map is not thread-safe for deletions, + // so we still need a mutex, but the chances of waiting are really + // low, since we will almost never do an erase. + std::mutex m_erase_mutex; + + // used to let m_resolver know when to terminate + std::promise m_exit_signal; + + std::thread *m_resolver; + + uint64_t m_erase_timeout; + uint64_t m_base_refresh_timeout; + uint64_t m_max_refresh_timeout; + + friend sinsp_dns_resolver; +}; diff --git a/userspace/libsinsp/dumper.cpp b/userspace/libsinsp/dumper.cpp index 1670edd48e..f40facf91c 100644 --- a/userspace/libsinsp/dumper.cpp +++ b/userspace/libsinsp/dumper.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "sinsp.h" @@ -25,6 +26,17 @@ sinsp_dumper::sinsp_dumper(sinsp* inspector) { m_inspector = inspector; m_dumper = NULL; + m_target_memory_buffer = NULL; + m_target_memory_buffer_size = 0; + m_nevts = 0; +} + +sinsp_dumper::sinsp_dumper(sinsp* inspector, uint8_t* target_memory_buffer, uint64_t target_memory_buffer_size) +{ + m_inspector = inspector; + m_dumper = NULL; + m_target_memory_buffer = target_memory_buffer; + m_target_memory_buffer_size = target_memory_buffer_size; } sinsp_dumper::~sinsp_dumper() @@ -35,18 +47,92 @@ sinsp_dumper::~sinsp_dumper() } } -void sinsp_dumper::open(const string& filename) +void sinsp_dumper::open(const string& filename, bool compress, bool threads_from_sinsp) { if(m_inspector->m_h == NULL) { throw sinsp_exception("can't start event dump, inspector not opened yet"); } - m_dumper = scap_dump_open(m_inspector->m_h, filename.c_str()); + if(m_target_memory_buffer) + { + m_dumper = scap_memory_dump_open(m_inspector->m_h, m_target_memory_buffer, m_target_memory_buffer_size); + } + else + { + if(compress) + { + m_dumper = scap_dump_open(m_inspector->m_h, filename.c_str(), SCAP_COMPRESSION_GZIP, threads_from_sinsp); + } + else + { + m_dumper = scap_dump_open(m_inspector->m_h, filename.c_str(), SCAP_COMPRESSION_NONE, threads_from_sinsp); + } + } + if(m_dumper == NULL) { throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); } + + if(threads_from_sinsp) + { + m_inspector->m_thread_manager->dump_threads_to_file(m_dumper); + } + + m_inspector->m_container_manager.dump_containers(m_dumper); + + m_nevts = 0; +} + +void sinsp_dumper::fdopen(int fd, bool compress, bool threads_from_sinsp) +{ + if(m_inspector->m_h == NULL) + { + throw sinsp_exception("can't start event dump, inspector not opened yet"); + } + + if(compress) + { + m_dumper = scap_dump_open_fd(m_inspector->m_h, fd, SCAP_COMPRESSION_GZIP, threads_from_sinsp); + } + else + { + m_dumper = scap_dump_open_fd(m_inspector->m_h, fd, SCAP_COMPRESSION_NONE, threads_from_sinsp); + } + + if(m_dumper == NULL) + { + throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); + } + + if(threads_from_sinsp) + { + m_inspector->m_thread_manager->dump_threads_to_file(m_dumper); + } + + m_inspector->m_container_manager.dump_containers(m_dumper); + + m_nevts = 0; +} + +void sinsp_dumper::close() +{ + if(m_dumper != NULL) + { + scap_dump_close(m_dumper); + m_dumper = NULL; + } +} + +bool sinsp_dumper::is_open() +{ + return (m_dumper != NULL); +} + +bool sinsp_dumper::written_events() +{ + return m_nevts; } void sinsp_dumper::dump(sinsp_evt* evt) @@ -56,21 +142,57 @@ void sinsp_dumper::dump(sinsp_evt* evt) throw sinsp_exception("dumper not opened yet"); } - int32_t res = scap_dump(m_inspector->m_h, - m_dumper, evt->m_pevt, evt->m_cpuid); + scap_evt* pdevt = (evt->m_poriginal_evt)? evt->m_poriginal_evt : evt->m_pevt; + + int32_t res = scap_dump(m_inspector->m_h, + m_dumper, pdevt, evt->m_cpuid, 0); if(res != SCAP_SUCCESS) { throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); } + + m_nevts++; } uint64_t sinsp_dumper::written_bytes() +{ + if(m_dumper == NULL) + { + return 0; + } + + int64_t written_bytes = scap_dump_get_offset(m_dumper); + if(written_bytes == -1) + { + throw sinsp_exception("error getting offset"); + } + + return written_bytes; +} + +uint64_t sinsp_dumper::next_write_position() +{ + if(m_dumper == NULL) + { + return 0; + } + + int64_t position = scap_dump_ftell(m_dumper); + if(position == -1) + { + throw sinsp_exception("error getting offset"); + } + + return position; +} + +void sinsp_dumper::flush() { if(m_dumper == NULL) { throw sinsp_exception("dumper not opened yet"); } - return scap_dump_ftell(m_dumper); + scap_dump_flush(m_dumper); } diff --git a/userspace/libsinsp/dumper.h b/userspace/libsinsp/dumper.h index 569a97687d..4e2f1db9f4 100644 --- a/userspace/libsinsp/dumper.h +++ b/userspace/libsinsp/dumper.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once @@ -21,8 +22,8 @@ along with sysdig. If not, see . class sinsp; class sinsp_evt; -/** @defgroup dump Dumping events to disk - * Classes to perform miscellneous functionality +/** @defgroup dump Dumping events to disk + * Classes to perform miscellaneous functionality * @{ */ @@ -40,6 +41,15 @@ class SINSP_PUBLIC sinsp_dumper */ sinsp_dumper(sinsp* inspector); + /*! + \brief Constructs a dumper that saves to memory instead of disk. + Takes the address and the size of a preallocated memory buffer + where the data will go. + */ + sinsp_dumper(sinsp* inspector, + uint8_t* target_memory_buffer, + uint64_t target_memory_buffer_size); + ~sinsp_dumper(); /*! @@ -47,18 +57,60 @@ class SINSP_PUBLIC sinsp_dumper \param filename The name of the target file. + \param compress true to save the trace file in a compressed format. + + \param threads_from_sinsp If, true the thread and FD tables in the file + will be created from the current sinsp's tables instead of reusing the scap + ones. + \note There's no close() because the file is closed when the dumper is destroyed. */ - void open(const string& filename); + void open(const string& filename, + bool compress, + bool threads_from_sinsp=false); + + void fdopen(int fd, + bool compress, + bool threads_from_sinsp=false); /*! - \brief Return the current size of a tracefile. + \brief Closes the dump file. + */ + void close(); + + /*! + \brief Return whether or not the underling scap file has been + opened. + */ + bool is_open(); - \return The current size of the dump file. + /*! + \brief Return the number of events dumped so far. + */ + bool written_events(); + + /*! + \brief Return the current size of a trace file. + + \return The current size of the dump file. */ uint64_t written_bytes(); + /*! + \brief Return the starting position for the next write into + the file. (Under the covers, this uses gztell while + written_bytes uses gzoffset, which represent different values). + + \return The starting position for the next write. + */ + uint64_t next_write_position(); + + /*! + \brief Flush all pending output into the file. + */ + void flush(); + /*! \brief Writes an event to the file. @@ -66,9 +118,22 @@ class SINSP_PUBLIC sinsp_dumper */ void dump(sinsp_evt* evt); + inline uint8_t* get_memory_dump_cur_buf() + { + return scap_get_memorydumper_curpos(m_dumper); + } + + inline void set_inspector(sinsp *inspector) + { + m_inspector = inspector; + } + private: sinsp* m_inspector; scap_dumper_t* m_dumper; + uint8_t* m_target_memory_buffer; + uint64_t m_target_memory_buffer_size; + uint64_t m_nevts; }; /*@}*/ diff --git a/userspace/libsinsp/event.cpp b/userspace/libsinsp/event.cpp index 2529b87991..a0dc12128d 100644 --- a/userspace/libsinsp/event.cpp +++ b/userspace/libsinsp/event.cpp @@ -1,28 +1,36 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#include "sinsp_errno.h" + #ifndef _WIN32 #define __STDC_FORMAT_MACROS #include #include #include +#include +#else +#define NOMINMAX #endif +#include + #include "sinsp.h" #include "sinsp_int.h" @@ -30,95 +38,76 @@ along with sysdig. If not, see . extern sinsp_evttables g_infotables; -/////////////////////////////////////////////////////////////////////////////// -// sinsp_evt_param implementation -/////////////////////////////////////////////////////////////////////////////// -void sinsp_evt_param::init(char *valptr, uint16_t len) -{ - m_val = valptr; - m_len = len; -} +#define SET_NUMERIC_FORMAT(resfmt, fmt, ostr, ustr, xstr) do { \ + if(fmt == ppm_print_format::PF_OCT) \ + { \ + resfmt = (char*)"%#" ostr; \ + } \ + else if(fmt == ppm_print_format::PF_DEC) \ + { \ + resfmt = (char*)"%" ustr; \ + } \ + else if(fmt == ppm_print_format::PF_10_PADDED_DEC) \ + { \ + resfmt = (char*)"%09" ustr; \ + } \ + else if(fmt == ppm_print_format::PF_HEX) \ + { \ + resfmt = (char*)"%" xstr; \ + } \ + else \ + { \ + resfmt = (char*)"%" ustr; \ + } \ +} while(0) /////////////////////////////////////////////////////////////////////////////// // sinsp_evt implementation /////////////////////////////////////////////////////////////////////////////// sinsp_evt::sinsp_evt() : + m_pevt_storage(NULL), m_paramstr_storage(256), m_resolved_paramstr_storage(1024) { - m_params_loaded = false; + m_flags = EF_NONE; m_tinfo = NULL; #ifdef _DEBUG m_filtered_out = false; #endif + m_event_info_table = g_infotables.m_event_info; } sinsp_evt::sinsp_evt(sinsp *inspector) : + m_pevt_storage(NULL), m_paramstr_storage(1024), m_resolved_paramstr_storage(1024) { m_inspector = inspector; - m_params_loaded = false; + m_flags = EF_NONE; m_tinfo = NULL; #ifdef _DEBUG m_filtered_out = false; #endif + m_event_info_table = g_infotables.m_event_info; } sinsp_evt::~sinsp_evt() { + if(m_pevt_storage) + { + delete[] m_pevt_storage; + } } -void sinsp_evt::init() -{ - m_params_loaded = false; - m_info = scap_event_getinfo(m_pevt); - m_tinfo = NULL; - m_fdinfo = NULL; - m_iosize = 0; -} - -void sinsp_evt::init(uint8_t *evdata, uint16_t cpuid) -{ - m_params_loaded = false; - m_pevt = (scap_evt *)evdata; - m_info = scap_event_getinfo(m_pevt); - m_tinfo = NULL; - m_fdinfo = NULL; - m_iosize = 0; - m_cpuid = cpuid; - m_evtnum = 0; -} - -uint64_t sinsp_evt::get_num() -{ - return m_evtnum; -} - -int16_t sinsp_evt::get_cpuid() -{ - return m_cpuid; -} - -uint16_t sinsp_evt::get_type() -{ - return m_pevt->type; -} - -ppm_event_flags sinsp_evt::get_flags() -{ - return m_info->flags; -} - -uint64_t sinsp_evt::get_ts() +uint32_t sinsp_evt::get_dump_flags() { - return m_pevt->ts; + return scap_event_get_dump_flags(m_inspector->m_h); } -const char *sinsp_evt::get_name() +const char *sinsp_evt::get_name() const { return m_info->name; } -event_direction sinsp_evt::get_direction() +event_direction sinsp_evt::get_direction() const { return (event_direction)(m_pevt->type & PPME_DIRECTION_FLAG); } @@ -144,16 +133,17 @@ sinsp_threadinfo* sinsp_evt::get_thread_info(bool query_os_if_not_found) { return m_tinfo; } + else if(m_tinfo_ref) + { + m_tinfo = m_tinfo_ref.get(); - return m_inspector->get_thread(m_pevt->tid, query_os_if_not_found); -} + return m_tinfo; + } -sinsp_fdinfo_t* sinsp_evt::get_fd_info() -{ - return m_fdinfo; + return m_inspector->get_thread(m_pevt->tid, query_os_if_not_found, false); } -uint64_t sinsp_evt::get_fd_num() +int64_t sinsp_evt::get_fd_num() { if(m_fdinfo) { @@ -168,21 +158,21 @@ uint64_t sinsp_evt::get_fd_num() uint32_t sinsp_evt::get_num_params() { - if(!m_params_loaded) + if((m_flags & sinsp_evt::SINSP_EF_PARAMS_LOADED) == 0) { load_params(); - m_params_loaded = true; + m_flags |= (uint32_t)sinsp_evt::SINSP_EF_PARAMS_LOADED; } - return m_params.size(); + return (uint32_t)m_params.size(); } sinsp_evt_param *sinsp_evt::get_param(uint32_t id) { - if(!m_params_loaded) + if((m_flags & sinsp_evt::SINSP_EF_PARAMS_LOADED) == 0) { load_params(); - m_params_loaded = true; + m_flags |= (uint32_t)sinsp_evt::SINSP_EF_PARAMS_LOADED; } return &(m_params[id]); @@ -190,10 +180,10 @@ sinsp_evt_param *sinsp_evt::get_param(uint32_t id) const char *sinsp_evt::get_param_name(uint32_t id) { - if(!m_params_loaded) + if((m_flags & sinsp_evt::SINSP_EF_PARAMS_LOADED) == 0) { load_params(); - m_params_loaded = true; + m_flags |= (uint32_t)sinsp_evt::SINSP_EF_PARAMS_LOADED; } ASSERT(id < m_info->nparams); @@ -203,10 +193,10 @@ const char *sinsp_evt::get_param_name(uint32_t id) const struct ppm_param_info* sinsp_evt::get_param_info(uint32_t id) { - if(!m_params_loaded) + if((m_flags & sinsp_evt::SINSP_EF_PARAMS_LOADED) == 0) { load_params(); - m_params_loaded = true; + m_flags |= (uint32_t)sinsp_evt::SINSP_EF_PARAMS_LOADED; } ASSERT(id < m_info->nparams); @@ -234,21 +224,22 @@ uint32_t binary_buffer_to_hex_string(char *dst, char *src, uint32_t dstlen, uint num_chunks = 0; while(num_chunks < 8 && ptr < src + srclen) { - uint16_t* chunk = (uint16_t*)ptr; + uint16_t chunk = htons(*(uint16_t*)ptr); + if(ptr == src + srclen - 1) { - k += sprintf(row + k, " %.2x", *((uint8_t*)chunk)); + k += sprintf(row + k, " %.2x", *(((uint8_t*)&chunk) + 1)); } else { - k += sprintf(row + k, " %.4x", *chunk); + k += sprintf(row + k, " %.4x", chunk); } num_chunks++; ptr += sizeof(uint16_t); } - if(fmt == sinsp_evt::PF_HEXASCII) + if((fmt & sinsp_evt::PF_HEXASCII) || (fmt & sinsp_evt::PF_JSONHEXASCII)) { // Fill the row with spaces to align it to other rows while(num_chunks < 8) @@ -278,7 +269,7 @@ uint32_t binary_buffer_to_hex_string(char *dst, char *src, uint32_t dstlen, uint } row[k] = 0; - row_len = strlen(row); + row_len = (uint32_t)strlen(row); if(l + row_len >= dstlen - 1) { truncated = true; @@ -305,7 +296,10 @@ uint32_t binary_buffer_to_asciionly_string(char *dst, char *src, uint32_t dstlen uint32_t j; uint32_t k = 0; - dst[k++] = '\n'; + if(fmt != sinsp_evt::PF_EOLS_COMPACT) + { + dst[k++] = '\n'; + } for(j = 0; j < srclen; j++) { @@ -322,15 +316,15 @@ uint32_t binary_buffer_to_asciionly_string(char *dst, char *src, uint32_t dstlen if(isprint((int)(uint8_t)src[j])) { - switch(src[j]) - { - case '"': - case '\\': - dst[k++] = '\\'; - break; - default: - break; - } + // switch(src[j]) + // { + // case '"': + // case '\\': + // dst[k++] = '\\'; + // break; + // default: + // break; + // } dst[k] = src[j]; k++; @@ -374,15 +368,15 @@ uint32_t binary_buffer_to_string_dots(char *dst, char *src, uint32_t dstlen, uin if(isprint((int)(uint8_t)src[j])) { - switch(src[j]) - { - case '"': - case '\\': - dst[k++] = '\\'; - break; - default: - break; - } + // switch(src[j]) + // { + // case '"': + // case '\\': + // dst[k++] = '\\'; + // break; + // default: + // break; + // } dst[k] = src[j]; } @@ -397,6 +391,74 @@ uint32_t binary_buffer_to_string_dots(char *dst, char *src, uint32_t dstlen, uin return k; } +uint32_t binary_buffer_to_base64_string(char *dst, char *src, uint32_t dstlen, uint32_t srclen, sinsp_evt::param_fmt fmt) +{ + // + // base64 encoder, malloc-free version of: + // http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c + // + static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/'}; + static uint32_t mod_table[] = {0, 2, 1}; + + uint32_t j,k, enc_dstlen; + + enc_dstlen = 4 * ((srclen + 2) / 3); + // + // Make sure there's enough space in the target buffer. + // + if(enc_dstlen >= dstlen - 1) + { + return dstlen; + } + + for (j = 0, k = 0; j < srclen;) { + + uint32_t octet_a = j < srclen ? (unsigned char)src[j++] : 0; + uint32_t octet_b = j < srclen ? (unsigned char)src[j++] : 0; + uint32_t octet_c = j < srclen ? (unsigned char)src[j++] : 0; + + uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; + + dst[k++] = encoding_table[(triple >> 3 * 6) & 0x3F]; + dst[k++] = encoding_table[(triple >> 2 * 6) & 0x3F]; + dst[k++] = encoding_table[(triple >> 1 * 6) & 0x3F]; + dst[k++] = encoding_table[(triple >> 0 * 6) & 0x3F]; + } + + for (j = 0; j < mod_table[srclen % 3]; j++) + dst[enc_dstlen - 1 - j] = '='; + + return enc_dstlen; +} + +uint32_t binary_buffer_to_json_string(char *dst, char *src, uint32_t dstlen, uint32_t srclen, sinsp_evt::param_fmt fmt) +{ + uint32_t k = 0; + switch(fmt) + { + case sinsp_evt::PF_JSONHEX: + case sinsp_evt::PF_JSONHEXASCII: + k = binary_buffer_to_hex_string(dst, src, dstlen, srclen, fmt); + break; + case sinsp_evt::PF_JSONEOLS: + k = binary_buffer_to_asciionly_string(dst, src, dstlen, srclen, fmt); + break; + case sinsp_evt::PF_JSONBASE64: + k = binary_buffer_to_base64_string(dst, src, dstlen, srclen, fmt); + break; + default: + k = binary_buffer_to_string_dots(dst, src, dstlen, srclen, fmt); + } + return k; +} + uint32_t binary_buffer_to_string(char *dst, char *src, uint32_t dstlen, uint32_t srclen, sinsp_evt::param_fmt fmt) { uint32_t k = 0; @@ -413,11 +475,21 @@ uint32_t binary_buffer_to_string(char *dst, char *src, uint32_t dstlen, uint32_t return 0; } - if(fmt == sinsp_evt::PF_HEX || fmt == sinsp_evt::PF_HEXASCII) + if(fmt & sinsp_evt::PF_HEX || fmt & sinsp_evt::PF_HEXASCII) { k = binary_buffer_to_hex_string(dst, src, dstlen, srclen, fmt); } - else if(fmt == sinsp_evt::PF_EOLS) + else if(fmt & sinsp_evt::PF_BASE64) + { + k = binary_buffer_to_base64_string(dst, src, dstlen, srclen, fmt); + } + else if(fmt & sinsp_evt::PF_JSON || fmt & sinsp_evt::PF_JSONHEX + || fmt & sinsp_evt::PF_JSONEOLS || fmt & sinsp_evt::PF_JSONHEXASCII + || fmt & sinsp_evt::PF_JSONBASE64) + { + k = binary_buffer_to_json_string(dst, src, dstlen, srclen, fmt); + } + else if(fmt & (sinsp_evt::PF_EOLS | sinsp_evt::PF_EOLS_COMPACT)) { k = binary_buffer_to_asciionly_string(dst, src, dstlen, srclen, fmt); } @@ -433,7 +505,7 @@ uint32_t binary_buffer_to_string(char *dst, char *src, uint32_t dstlen, uint32_t uint32_t strcpy_sanitized(char *dest, char *src, uint32_t dstsize) { volatile char* tmp = (volatile char *)dest; - size_t j = 0; + uint32_t j = 0; g_invalidchar ic; while(j < dstsize) @@ -455,7 +527,7 @@ uint32_t strcpy_sanitized(char *dest, char *src, uint32_t dstsize) } // - // In case there wasn't enough space, null-termninate the destination + // In case there wasn't enough space, null-terminate the destination // if(dstsize) { @@ -465,181 +537,1025 @@ uint32_t strcpy_sanitized(char *dest, char *src, uint32_t dstsize) return dstsize; } -const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_str, sinsp_evt::param_fmt fmt) +int sinsp_evt::render_fd_json(Json::Value *ret, int64_t fd, const char** resolved_str, sinsp_evt::param_fmt fmt) { - uint32_t j; - ASSERT(id < m_info->nparams); + sinsp_threadinfo* tinfo = get_thread_info(); + if(tinfo == NULL) + { + return 0; + } + + if(fd >= 0) + { + sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd); + if(fdinfo) + { + char tch = fdinfo->get_typechar(); + char ipprotoch = 0; + + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK || + fdinfo->m_type == SCAP_FD_IPV6_SOCK || + fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK || + fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK) + { + scap_l4_proto l4p = fdinfo->get_l4proto(); + + switch(l4p) + { + case SCAP_L4_TCP: + ipprotoch = 't'; + break; + case SCAP_L4_UDP: + ipprotoch = 'u'; + break; + case SCAP_L4_ICMP: + ipprotoch = 'i'; + break; + case SCAP_L4_RAW: + ipprotoch = 'r'; + break; + default: + break; + } + } + + char typestr[3] = + { + (fmt & PF_SIMPLE)?(char)0:tch, + ipprotoch, + 0 + }; + + // + // Make sure we remove invalid characters from the resolved name + // + string sanitized_str = fdinfo->m_name; + + sanitize_string(sanitized_str); + + (*ret)["typechar"] = typestr; + (*ret)["name"] = sanitized_str; + } + } + else if(fd == PPM_AT_FDCWD) + { + // + // `fd` can be AT_FDCWD on all *at syscalls + // + (*ret)["name"] = "AT_FDCWD"; + } + else + { + // + // Resolve this as an errno + // + string errstr(sinsp_utils::errno_to_str((int32_t)fd)); + if(errstr != "") + { + (*ret)["error"] = errstr; + return 0; + } + } + + return 1; +} + +char* sinsp_evt::render_fd(int64_t fd, const char** resolved_str, sinsp_evt::param_fmt fmt) +{ + // + // Add the fd number + // + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "%" PRId64, fd); + + sinsp_threadinfo* tinfo = get_thread_info(); + if(tinfo == NULL) + { + // + // no thread. Definitely can't resolve the fd, just return the number + // + return &m_paramstr_storage[0]; + } + + if(fd >= 0) + { + sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd); + if(fdinfo) + { + char tch = fdinfo->get_typechar(); + char ipprotoch = 0; + + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK || + fdinfo->m_type == SCAP_FD_IPV6_SOCK || + fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK || + fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK) + { + scap_l4_proto l4p = fdinfo->get_l4proto(); + + switch(l4p) + { + case SCAP_L4_TCP: + ipprotoch = 't'; + break; + case SCAP_L4_UDP: + ipprotoch = 'u'; + break; + case SCAP_L4_ICMP: + ipprotoch = 'i'; + break; + case SCAP_L4_RAW: + ipprotoch = 'r'; + break; + default: + break; + } + } + + char typestr[3] = + { + (fmt & PF_SIMPLE)?(char)0:tch, + ipprotoch, + 0 + }; + + // + // Make sure we remove invalid characters from the resolved name + // + string sanitized_str = fdinfo->m_name; + + sanitize_string(sanitized_str); + + // + // Make sure the string will fit + // + if(sanitized_str.size() >= m_resolved_paramstr_storage.size()) + { + m_resolved_paramstr_storage.resize(sanitized_str.size() + 1); + } + + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "<%s>%s", typestr, sanitized_str.c_str()); + +/* XXX + if(sanitized_str.length() == 0) + { + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "<%c>", tch); + } + else + { + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "%s", sanitized_str.c_str()); + } +*/ + } + } + else if(fd == PPM_AT_FDCWD) + { + // + // `fd` can be AT_FDCWD on all *at syscalls + // + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "AT_FDCWD"); + } + else + { + // + // Resolve this as an errno + // + string errstr(sinsp_utils::errno_to_str((int32_t)fd)); + if(errstr != "") + { + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "%s", errstr.c_str()); + } + } + + return &m_paramstr_storage[0]; +} + +Json::Value sinsp_evt::get_param_as_json(uint32_t id, OUT const char** resolved_str, sinsp_evt::param_fmt fmt) +{ + const ppm_param_info* param_info; + char* payload; + uint16_t payload_len; + Json::Value ret; // // Make sure the params are actually loaded // - if(!m_params_loaded) + if((m_flags & sinsp_evt::SINSP_EF_PARAMS_LOADED) == 0) { load_params(); - m_params_loaded = true; + m_flags |= (uint32_t)sinsp_evt::SINSP_EF_PARAMS_LOADED; } + ASSERT(id < get_num_params()); + // // Reset the resolved string // - *resolved_str = &m_resolved_paramstr_storage[0]; m_resolved_paramstr_storage[0] = 0; // // Get the parameter // sinsp_evt_param *param = &(m_params[id]); + payload = param->m_val; + payload_len = param->m_len; + param_info = &(m_info->params[id]); + + // + // Get the parameter information + // + if(param_info->type == PT_DYN && param_info->info != NULL && payload_len != 0) + { + uint8_t dyn_idx = *(uint8_t*)payload; + + if(dyn_idx < param_info->ninfo) { + const struct ppm_param_info* dyn_params = + (const struct ppm_param_info*)param_info->info; + + payload += sizeof(uint8_t); + payload_len -= sizeof(uint8_t); - switch(m_info->params[id].type) + param_info = &dyn_params[dyn_idx]; + } + } + + switch(param_info->type) { case PT_INT8: - ASSERT(param->m_len == sizeof(int8_t)); - snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%" PRId8, *(int8_t *)param->m_val); + ASSERT(payload_len == sizeof(int8_t)); + ret = *(int8_t *)payload; break; + case PT_INT16: - ASSERT(param->m_len == sizeof(int16_t)); - snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%" PRId16, *(int16_t *)param->m_val); + ASSERT(payload_len == sizeof(int16_t)); + ret = *(int16_t *)payload; break; + case PT_INT32: - ASSERT(param->m_len == sizeof(int32_t)); - snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%" PRId32, *(int32_t *)param->m_val); + ASSERT(payload_len == sizeof(int32_t)); + ret = *(int32_t *)payload; break; + case PT_INT64: - ASSERT(param->m_len == sizeof(int64_t)); - snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%" PRId64, *(int64_t *)param->m_val); + ASSERT(payload_len == sizeof(int64_t)); + ret = (Json::Value::Int64)*(int64_t *)payload; break; - case PT_FD: - { - int64_t fd; - ASSERT(param->m_len == sizeof(int64_t)); - - fd = *(int64_t*)param->m_val; - - // - // Add the fd number - // - snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%" PRId64, fd); - sinsp_threadinfo* tinfo = get_thread_info(); - if(tinfo == NULL) - { - // - // no thread. Definitely can't resolve the fd, just return the number - // - break; - } + case PT_UINT8: + ASSERT(payload_len == sizeof(uint8_t)); + ret = *(uint8_t *)payload; + break; - if(fd >= 0) - { - sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd); - if(fdinfo) - { - char tch = fdinfo->get_typechar(); - char ipprotoch = 0; - - if(fdinfo->m_type == SCAP_FD_IPV4_SOCK || - fdinfo->m_type == SCAP_FD_IPV6_SOCK || - fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK || - fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK) - { - scap_l4_proto l4p = fdinfo->get_l4proto(); + case PT_UINT16: + ASSERT(payload_len == sizeof(uint16_t)); + ret = *(uint16_t *)payload; + break; - switch(l4p) - { - case SCAP_L4_TCP: - ipprotoch = 't'; - break; - case SCAP_L4_UDP: - ipprotoch = 'u'; - break; - case SCAP_L4_ICMP: - ipprotoch = 'i'; - break; - case SCAP_L4_RAW: - ipprotoch = 'r'; - break; - default: - break; - } - } + case PT_UINT32: + ASSERT(payload_len == sizeof(uint32_t)); + ret = *(uint32_t *)payload; + break; - char typestr[3] = - { - (fmt == PF_SIMPLE)?(char)0:tch, - ipprotoch, - 0 - }; + case PT_UINT64: + ASSERT(payload_len == sizeof(uint64_t)); + ret = (Json::Value::UInt64)*(int64_t *)payload; + break; - // - // Make sure we remove invalid characters from the resolved name - // - string sanitized_str = fdinfo->m_name; + case PT_PID: + { + ASSERT(payload_len == sizeof(int64_t)); + ret = (Json::Value::UInt64)*(int64_t *)payload; - sanitized_str.erase(remove_if(sanitized_str.begin(), sanitized_str.end(), g_invalidchar()), sanitized_str.end()); + sinsp_threadinfo* atinfo = m_inspector->get_thread(*(int64_t *)payload, false, true); + if(atinfo != NULL) + { + string& tcomm = atinfo->m_comm; // // Make sure the string will fit // - if(sanitized_str.size() >= m_resolved_paramstr_storage.size()) + if(tcomm.size() >= m_resolved_paramstr_storage.size()) { - m_resolved_paramstr_storage.resize(sanitized_str.size() + 1); + m_resolved_paramstr_storage.resize(tcomm.size() + 1); } snprintf(&m_resolved_paramstr_storage[0], - m_resolved_paramstr_storage.size(), - "<%s>%s", typestr, sanitized_str.c_str()); - -/* XXX - if(sanitized_str.length() == 0) + m_resolved_paramstr_storage.size(), + "%s", + tcomm.c_str()); + } + } + break; + + case PT_ERRNO: + { + ASSERT(payload_len == sizeof(int64_t)); + + int64_t val = *(int64_t *)payload; + + // + // Resolve this as an errno + // + string errstr; + + if(val < 0) + { + errstr = sinsp_utils::errno_to_str((int32_t)val); + + if(errstr != "") + { + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "%s", errstr.c_str()); + } + } + ret = (Json::Value::Int64)val; + } + break; + + case PT_FD: + { + // We use the string extractor to get + // the resolved path, but use our routine + // to get the actual value to return + ASSERT(payload_len == sizeof(int64_t)); + int64_t fd = *(int64_t*)payload; + render_fd_json(&ret, fd, resolved_str, fmt); + ret["num"] = (Json::Value::UInt64)*(int64_t *)payload; + break; + } + + case PT_CHARBUF: + case PT_FSPATH: + case PT_FSRELPATH: + case PT_BYTEBUF: + ret = get_param_as_str(id, resolved_str, fmt); + break; + + case PT_SOCKADDR: + if(payload_len == 0) + { + ret = Json::nullValue; + break; + } + else if(payload[0] == AF_UNIX) + { + ASSERT(payload_len > 1); + + // + // Sanitize the file string. + // + string sanitized_str = payload + 1; + sanitize_string(sanitized_str); + + ret = sanitized_str; + } + else if(payload[0] == PPM_AF_INET) + { + if(payload_len == 1 + 4 + 2) + { + const int ipv4_len = (3 + 1) * 4 + 1; + char ipv4_addr[ ipv4_len ]; + + snprintf( + ipv4_addr, + ipv4_len, + "%u.%u.%u.%u", + (unsigned int)(uint8_t)payload[1], + (unsigned int)(uint8_t)payload[2], + (unsigned int)(uint8_t)payload[3], + (unsigned int)(uint8_t)payload[4] + ); + ret["addr"] = string(ipv4_addr); + ret["port"] = *(uint16_t*)(payload + 5); + } + else + { + ASSERT(false); + ret = "INVALID IPv4"; + } + } + else + { + ret["family"] = (int)payload[0]; + } + break; + + case PT_SOCKTUPLE: + if(payload_len == 0) + { + ret = Json::nullValue; + break; + } + + if(payload[0] == PPM_AF_INET) + { + if(payload_len == 1 + 4 + 2 + 4 + 2) + { + Json::Value source; + Json::Value dest; + + const int ipv4_len = (3 + 1) * 4 + 1; + char ipv4_addr[ ipv4_len ]; + + snprintf( + ipv4_addr, + ipv4_len, + "%u.%u.%u.%u", + (unsigned int)(uint8_t)payload[1], + (unsigned int)(uint8_t)payload[2], + (unsigned int)(uint8_t)payload[3], + (unsigned int)(uint8_t)payload[4] + ); + + source["addr"] = string(ipv4_addr); + source["port"] = *(uint16_t*)(payload + 5); + + snprintf( + ipv4_addr, + ipv4_len, + "%u.%u.%u.%u", + (unsigned int)(uint8_t)payload[7], + (unsigned int)(uint8_t)payload[8], + (unsigned int)(uint8_t)payload[9], + (unsigned int)(uint8_t)payload[10] + ); + + + dest["addr"] = string(ipv4_addr); + dest["port"] = *(uint16_t*)(payload + 11); + + ret["src"] = source; + ret["dst"] = dest; + } + else + { + ASSERT(false); + ret = "INVALID IPv4"; + } + } + else if(payload[0] == PPM_AF_INET6) + { + if(payload_len == 1 + 16 + 2 + 16 + 2) + { + uint8_t* sip6 = (uint8_t*)payload + 1; + uint8_t* dip6 = (uint8_t*)payload + 19; + uint8_t* sip = (uint8_t*)payload + 13; + uint8_t* dip = (uint8_t*)payload + 31; + + if(sinsp_utils::is_ipv4_mapped_ipv6(sip6) && sinsp_utils::is_ipv4_mapped_ipv6(dip6)) { - snprintf(&m_resolved_paramstr_storage[0], - m_resolved_paramstr_storage.size(), - "<%c>", tch); + Json::Value source; + Json::Value dest; + + const int ipv4_len = (3 + 1) * 4 + 1; + char ipv4_addr[ ipv4_len ]; + + snprintf( + ipv4_addr, + ipv4_len, + "%u.%u.%u.%u", + (unsigned int)sip[0], + (unsigned int)sip[1], + (unsigned int)sip[2], + (unsigned int)sip[3] + ); + + source["addr"] = string(ipv4_addr); + source["port"] = (unsigned int)*(uint16_t*)(payload + 17); + + snprintf( + ipv4_addr, + ipv4_len, + "%u.%u.%u.%u", + (unsigned int)dip[0], + (unsigned int)dip[1], + (unsigned int)dip[2], + (unsigned int)dip[3] + ); + + dest["addr"] = string(ipv4_addr); + dest["port"] = (unsigned int)*(uint16_t*)(payload + 35); + + ret["src"] = source; + ret["dst"] = dest; + + break; } else { - snprintf(&m_resolved_paramstr_storage[0], - m_resolved_paramstr_storage.size(), - "%s", sanitized_str.c_str()); + char srcstr[INET6_ADDRSTRLEN]; + char dststr[INET6_ADDRSTRLEN]; + + if(inet_ntop(AF_INET6, sip6, srcstr, sizeof(srcstr)) && + inet_ntop(AF_INET6, dip6, dststr, sizeof(dststr))) + { + Json::Value source; + Json::Value dest; + + source["addr"] = srcstr; + source["port"] = (unsigned int)*(uint16_t*)(payload + 17); + + dest["addr"] = dststr; + dest["port"] = (unsigned int)*(uint16_t*)(payload + 35); + + ret["src"] = source; + ret["dst"] = dest; + + break; + } } -*/ } + ASSERT(false); + ret = "INVALID IPv6"; + } - else + else if(payload[0] == AF_UNIX) { + ASSERT(payload_len > 17); + // - // Resolve this as an errno + // Sanitize the file string. // - string errstr(sinsp_utils::errno_to_str((int32_t)fd)); - if(errstr != "") + string sanitized_str = payload + 17; + sanitize_string(sanitized_str); + + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "%" PRIx64 "->%" PRIx64 " %s", + *(uint64_t*)(payload + 1), + *(uint64_t*)(payload + 9), + sanitized_str.c_str()); + } + else + { + ret["family"] = (int)payload[0]; + } + break; + case PT_FDLIST: + ret = get_param_as_str(id, resolved_str, fmt); + break; + + case PT_SYSCALLID: + { + uint16_t scid = *(uint16_t *)payload; + if(scid >= PPM_SC_MAX) { + ASSERT(false); snprintf(&m_resolved_paramstr_storage[0], - m_resolved_paramstr_storage.size(), - "%s", errstr.c_str()); + m_resolved_paramstr_storage.size(), + ""); + break; + } + + const struct ppm_syscall_desc* desc = &(g_infotables.m_syscall_info_table[scid]); + + ret = scid; + + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "%s", + desc->name); + } + break; + case PT_SIGTYPE: + { + const char* sigstr; + + ASSERT(payload_len == sizeof(uint8_t)); + uint8_t val = *(uint8_t *)payload; + + sigstr = sinsp_utils::signal_to_str(val); + ret = val; + + if(sigstr) + { + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "%s", sigstr); + } + } + break; + case PT_RELTIME: + { + ASSERT(payload_len == sizeof(uint64_t)); + uint64_t val = *(uint64_t *)payload; + ret = (Json::Value::Int64)val; + + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "%lgs", + ((double)val) / 1000000000); + } + break; + case PT_FLAGS8: + case PT_FLAGS16: + case PT_FLAGS32: + { + uint32_t val = *(uint32_t *)payload & (((uint64_t)1 << payload_len * 8) - 1); + ret["val"] = val; + ret["flags"] = Json::arrayValue; + + const struct ppm_name_value *flags = (const struct ppm_name_value *)m_info->params[id].info; + uint32_t initial_val = val; + + while(flags != NULL && flags->name != NULL && flags->value != initial_val) + { + // If flag is 0, then initial_val needs to be 0 for the flag to be resolved + if((flags->value == 0 && initial_val == 0) || + (flags->value != 0 && (val & flags->value) == flags->value && val != 0)) + { + ret["flags"].append(flags->name); + + // We remove current flags value to avoid duplicate flags e.g. PPM_O_RDWR, PPM_O_RDONLY, PPM_O_WRONLY + val &= ~flags->value; + } + + flags++; + } + + if(flags != NULL && flags->name != NULL) + { + ret["flags"].append(flags->name); + } + + break; + } + case PT_MODE: + { + uint32_t val = *(uint32_t *)payload & (((uint64_t)1 << payload_len * 8) - 1); + ret["val"] = val; + ret["mode"] = Json::arrayValue; + + const struct ppm_name_value *mode = (const struct ppm_name_value *)m_info->params[id].info; + uint32_t initial_val = val; + + while(mode != NULL && mode->name != NULL && mode->value != initial_val) + { + // If mode is 0, then initial_val needs to be 0 for the mode to be resolved + if((mode->value == 0 && initial_val == 0) || + (mode->value != 0 && (val & mode->value) == mode->value && val != 0)) + { + ret["mode"].append(mode->name); + + // We remove current mode value to avoid duplicates + val &= ~mode->value; + } + + mode++; + } + + if(mode != NULL && mode->name != NULL) + { + ret["mode"].append(mode->name); + } + + break; + } + case PT_UID: + case PT_GID: + { + ASSERT(payload_len == sizeof(uint32_t)); + uint32_t val = *(uint32_t *)payload; + if(val < std::numeric_limits::max() ) + { + ret = val; + } + else + { + ret = -1; + } + break; + } + case PT_CHARBUFARRAY: + { + ASSERT(param->m_len == sizeof(uint64_t)); + vector* strvect = (vector*)*(uint64_t *)param->m_val; + + m_paramstr_storage[0] = 0; + + while(true) + { + vector::iterator it; + vector::iterator itbeg; + bool need_to_resize = false; + + // + // Copy the arguments + // + char* dst = &m_paramstr_storage[0]; + char* dstend = &m_paramstr_storage[0] + m_paramstr_storage.size() - 2; + + for(it = itbeg = strvect->begin(); it != strvect->end(); ++it) + { + char* src = *it; + + if(it != itbeg) + { + if(dst < dstend - 1) + { + *dst++ = ','; + } + } + + while(*src != 0 && dst < dstend) + { + *dst++ = *src++; + } + + if(dst == dstend) + { + // + // Reached the end of m_paramstr_storage, we need to resize it + // + need_to_resize = true; + break; + } } + + if(need_to_resize) + { + m_paramstr_storage.resize(m_paramstr_storage.size() * 2); + continue; + } + + *dst = 0; + + break; } } break; + case PT_CHARBUF_PAIR_ARRAY: + { + ASSERT(param->m_len == sizeof(uint64_t)); + pair*, vector*>* pairs = + (pair*, vector*>*)*(uint64_t *)param->m_val; + ASSERT(pairs->first->size() == pairs->second->size()); + + m_paramstr_storage[0] = 0; + + while(true) + { + vector::iterator it1; + vector::iterator itbeg1; + vector::iterator it2; + vector::iterator itbeg2; + bool need_to_resize = false; + + // + // Copy the arguments + // + char* dst = &m_paramstr_storage[0]; + char* dstend = &m_paramstr_storage[0] + m_paramstr_storage.size() - 2; + + for(it1 = itbeg1 = pairs->first->begin(), it2 = itbeg2 = pairs->second->begin(); + it1 != pairs->first->end(); + ++it1, ++it2) + { + char* src = *it1; + + if(it1 != itbeg1) + { + if(dst < dstend - 1) + { + *dst++ = ','; + } + } + + // + // Copy the first string + // + while(*src != 0 && dst < dstend) + { + *dst++ = *src++; + } + + if(dst < dstend - 1) + { + *dst++ = ':'; + } + + // + // Copy the second string + // + src = *it2; + while(*src != 0 && dst < dstend) + { + *dst++ = *src++; + } + + if(dst == dstend) + { + // + // Reached the end of m_paramstr_storage, we need to resize it + // + need_to_resize = true; + break; + } + } + + if(need_to_resize) + { + m_paramstr_storage.resize(m_paramstr_storage.size() * 2); + continue; + } + + *dst = 0; + + break; + } + } + case PT_ABSTIME: + // + // XXX not implemented yet + // + ASSERT(false); + case PT_DYN: + ASSERT(false); + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "INVALID DYNAMIC PARAMETER"); + break; + + case PT_SIGSET: + ret = get_param_as_str(id, resolved_str, fmt); + break; + + default: + ASSERT(false); + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "(n.a.)"); + break; + } + + *resolved_str = &m_resolved_paramstr_storage[0]; + + return ret; +} + +std::string sinsp_evt::get_base_dir(uint32_t id, sinsp_threadinfo *tinfo) +{ + std::string cwd = tinfo->get_cwd(); + + const ppm_param_info* param_info = &m_info->params[id]; + + // If it's a regular FSPATH, just return the thread's CWD + if (param_info->type != PT_FSRELPATH) + { + ASSERT(param_info->type == PT_FSPATH); + return cwd; + } + + uint64_t dirfd_id = (uint64_t)param_info->info; + if (dirfd_id >= m_info->nparams) + { + ASSERT(dirfd_id < m_info->nparams); + return cwd; + } + + const ppm_param_info* dir_param_info = &(m_info->params[dirfd_id]); + // Ensure the index points to an actual FD + if (dir_param_info->type != PT_FD) + { + ASSERT(dir_param_info->type == PT_FD); + return cwd; + } + + const sinsp_evt_param* dir_param = &m_params[dirfd_id]; + const int64_t dirfd = *(int64_t*)dir_param->m_val; + + // If the FD is special value PPM_AT_FDCWD, just use CWD + if (dirfd == PPM_AT_FDCWD) + { + return cwd; + } + + // If the previous param is a fd with a value other than AT_FDCWD, + // get the path to that fd and use it in place of CWD + std::string rel_path_base = tinfo->get_path_for_dir_fd(dirfd); + if (rel_path_base.empty()) + { + return rel_path_base; + } + sanitize_string(rel_path_base); + rel_path_base.append("/"); + return rel_path_base; +} + +const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_str, sinsp_evt::param_fmt fmt) +{ + char* prfmt; + const ppm_param_info* param_info; + char* payload; + uint32_t j; + uint16_t payload_len; + + // + // Make sure the params are actually loaded + // + if((m_flags & sinsp_evt::SINSP_EF_PARAMS_LOADED) == 0) + { + load_params(); + m_flags |= (uint32_t)sinsp_evt::SINSP_EF_PARAMS_LOADED; + } + + ASSERT(id < get_num_params()); + + // + // Reset the resolved string + // + m_resolved_paramstr_storage[0] = 0; + + // + // Get the parameter + // + sinsp_evt_param *param = &(m_params[id]); + payload = param->m_val; + payload_len = param->m_len; + param_info = &(m_info->params[id]); + + // + // Get the parameter information + // + if(param_info->type == PT_DYN && param_info->info != NULL && payload_len != 0) + { + uint8_t dyn_idx = *(uint8_t*)payload; + + if(dyn_idx < param_info->ninfo) { + const struct ppm_param_info* dyn_params = + (const struct ppm_param_info*)param_info->info; + + payload += sizeof(uint8_t); + payload_len -= sizeof(uint8_t); + + param_info = &dyn_params[dyn_idx]; + } + } + + ppm_print_format param_fmt = m_info->params[id].fmt; + + switch(param_info->type) + { + case PT_INT8: + ASSERT(payload_len == sizeof(int8_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo8, PRId8, PRIX8); + + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + prfmt, *(int8_t *)payload); + break; + case PT_INT16: + ASSERT(payload_len == sizeof(int16_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo16, PRId16, PRIX16); + + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + prfmt, *(int16_t *)payload); + break; + case PT_INT32: + ASSERT(payload_len == sizeof(int32_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo32, PRId32, PRIX32); + + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + prfmt, *(int32_t *)payload); + break; + case PT_INT64: + ASSERT(payload_len == sizeof(int64_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo64, PRId64, PRIX64); + + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + prfmt, *(int64_t *)payload); + break; + case PT_FD: + { + ASSERT(payload_len == sizeof(int64_t)); + int64_t fd = *(int64_t*)payload; + render_fd(fd, resolved_str, fmt); + break; + } case PT_PID: { - ASSERT(param->m_len == sizeof(int64_t)); + ASSERT(payload_len == sizeof(int64_t)); snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%" PRId64, *(int64_t *)param->m_val); + "%" PRId64, *(int64_t *)payload); - sinsp_threadinfo* atinfo = m_inspector->get_thread(*(int64_t *)param->m_val, false); + sinsp_threadinfo* atinfo = m_inspector->get_thread(*(int64_t *)payload, false, true); if(atinfo != NULL) { string& tcomm = atinfo->m_comm; @@ -660,28 +1576,34 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s } break; case PT_UINT8: - ASSERT(param->m_len == sizeof(uint8_t)); + ASSERT(payload_len == sizeof(uint8_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo8, PRId8, PRIX8); + snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%" PRIu8, *(uint8_t *)param->m_val); + prfmt, *(uint8_t *)payload); break; case PT_UINT16: - ASSERT(param->m_len == sizeof(uint16_t)); + ASSERT(payload_len == sizeof(uint16_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo16, PRId16, PRIX16); + snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%" PRIu16, *(uint16_t *)param->m_val); + prfmt, *(uint16_t *)payload); break; case PT_UINT32: - ASSERT(param->m_len == sizeof(uint32_t)); + ASSERT(payload_len == sizeof(uint32_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo32, PRId32, PRIX32); + snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%" PRIu32, *(uint32_t *)param->m_val); + prfmt, *(uint32_t *)payload); break; case PT_ERRNO: { - ASSERT(param->m_len == sizeof(int64_t)); + ASSERT(payload_len == sizeof(int64_t)); - int64_t val = *(int64_t *)param->m_val; + int64_t val = *(int64_t *)payload; snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), @@ -706,45 +1628,56 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s } break; case PT_UINT64: - ASSERT(param->m_len == sizeof(uint64_t)); + ASSERT(payload_len == sizeof(uint64_t)); + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo64, PRId64, PRIX64); + snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%" PRIu64, *(int64_t *)param->m_val); + prfmt, *(int64_t *)payload); + break; case PT_CHARBUF: // // Make sure the string will fit // - if(param->m_len > m_resolved_paramstr_storage.size()) + if(payload_len > m_paramstr_storage.size()) { - m_resolved_paramstr_storage.resize(param->m_len); + m_paramstr_storage.resize(payload_len); } snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%s", param->m_val); + "%s", payload); break; case PT_FSPATH: + case PT_FSRELPATH: { strcpy_sanitized(&m_paramstr_storage[0], - param->m_val, - MIN(param->m_len, m_paramstr_storage.size())); + payload, + MIN(payload_len, (uint32_t)m_paramstr_storage.size())); sinsp_threadinfo* tinfo = get_thread_info(); - if(tinfo) + if(tinfo && payload_len > 0) { - string fullpath; - string cwd = tinfo->get_cwd(); - - if(!sinsp_utils::concatenate_paths(&m_resolved_paramstr_storage[0], - m_resolved_paramstr_storage.size(), - (char*)cwd.c_str(), - cwd.length(), - param->m_val, - param->m_len)) + if(strncmp(payload, "", 4) != 0) { - m_resolved_paramstr_storage[0] = 0; + std::string cwd = get_base_dir(id, tinfo); + + if(payload_len + cwd.length() >= m_resolved_paramstr_storage.size()) + { + m_resolved_paramstr_storage.resize(payload_len + cwd.length() + 1, 0); + } + + if(!sinsp_utils::concatenate_paths(&m_resolved_paramstr_storage[0], + (uint32_t)m_resolved_paramstr_storage.size(), + (char*)cwd.c_str(), + (uint32_t)cwd.length(), + payload, + payload_len)) + { + m_resolved_paramstr_storage[0] = 0; + } } } else @@ -755,7 +1688,7 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s break; case PT_BYTEBUF: { - /* This would include quotes around the outpur string + /* This would include quotes around the output string m_paramstr_storage[0] = '"'; cres = binary_buffer_to_string(m_paramstr_storage + 1, param->m_val, @@ -768,9 +1701,9 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s while(true) { uint32_t blen = binary_buffer_to_string(&m_paramstr_storage[0], - param->m_val, - m_paramstr_storage.size() - 1, - param->m_len, + payload, + (uint32_t)m_paramstr_storage.size() - 1, + payload_len, fmt); if(blen >= m_paramstr_storage.size() - 1) @@ -782,12 +1715,33 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s continue; } + ASSERT(m_inspector != NULL); + if(m_inspector->m_max_evt_output_len != 0 && + blen > m_inspector->m_max_evt_output_len && + fmt == PF_NORMAL) + { + uint32_t real_len = MIN(blen, m_inspector->m_max_evt_output_len); + + m_rawbuf_str_len = real_len; + if(real_len > 3) + { + m_paramstr_storage[real_len - 3] = '.'; + m_paramstr_storage[real_len - 2] = '.'; + m_paramstr_storage[real_len - 1] = '.'; + } + + m_paramstr_storage[real_len] = 0; + } + else + { + m_rawbuf_str_len = blen; + } break; } } break; case PT_SOCKADDR: - if(param->m_len == 0) + if(payload_len == 0) { snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), @@ -795,33 +1749,34 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s break; } - else if(param->m_val[0] == AF_UNIX) + else if(payload[0] == AF_UNIX) { - ASSERT(param->m_len > 1); + ASSERT(payload_len > 1); // // Sanitize the file string. // - string sanitized_str = param->m_val + 1; - sanitized_str.erase(remove_if(sanitized_str.begin(), sanitized_str.end(), g_invalidchar()), sanitized_str.end()); + string sanitized_str = payload + 1; + sanitize_string(sanitized_str); snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), + m_paramstr_storage.size(), "%s", sanitized_str.c_str()); } - else if(param->m_val[0] == PPM_AF_INET) + else if(payload[0] == PPM_AF_INET) { - if(param->m_len == 1 + 4 + 2) + if(payload_len == 1 + 4 + 2) { + ipv4serverinfo addr; + addr.m_ip = *(uint32_t*)(payload + 1); + addr.m_port = *(uint16_t*)(payload+5); + addr.m_l4proto = (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN; + string straddr = ipv4serveraddr_to_string(&addr, m_inspector->m_hostname_and_port_resolution_enabled); snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%u.%u.%u.%u:%u", - (unsigned int)(uint8_t)param->m_val[1], - (unsigned int)(uint8_t)param->m_val[2], - (unsigned int)(uint8_t)param->m_val[3], - (unsigned int)(uint8_t)param->m_val[4], - (unsigned int)*(uint16_t*)(param->m_val+5)); + m_paramstr_storage.size(), + "%s", + straddr.c_str()); } else { @@ -831,15 +1786,37 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s "INVALID IPv4"); } } + else if(payload[0] == PPM_AF_INET6) + { + if(payload_len == 1 + 16 + 2) + { + ipv6serverinfo addr; + memcpy((uint8_t *) addr.m_ip.m_b, (uint8_t *) payload+1, sizeof(addr.m_ip.m_b)); + addr.m_port = *(uint16_t*)(payload+17); + addr.m_l4proto = (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN; + string straddr = ipv6serveraddr_to_string(&addr, m_inspector->m_hostname_and_port_resolution_enabled); + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "%s", + straddr.c_str()); + } + else + { + ASSERT(false); + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "INVALID IPv6"); + } + } else { snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "family %d", (int)param->m_val[0]); + "family %d", (int)payload[0]); } break; case PT_SOCKTUPLE: - if(param->m_len == 0) + if(payload_len == 0) { snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), @@ -847,24 +1824,22 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s break; } - - if(param->m_val[0] == PPM_AF_INET) + + if(payload[0] == PPM_AF_INET) { - if(param->m_len == 1 + 4 + 2 + 4 + 2) + if(payload_len == 1 + 4 + 2 + 4 + 2) { + ipv4tuple addr; + addr.m_fields.m_sip = *(uint32_t*)(payload + 1); + addr.m_fields.m_sport = *(uint16_t*)(payload+5); + addr.m_fields.m_dip = *(uint32_t*)(payload + 7); + addr.m_fields.m_dport = *(uint16_t*)(payload+11); + addr.m_fields.m_l4proto = (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN; + string straddr = ipv4tuple_to_string(&addr, m_inspector->m_hostname_and_port_resolution_enabled); snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%u.%u.%u.%u:%u->%u.%u.%u.%u:%u", - (unsigned int)(uint8_t)param->m_val[1], - (unsigned int)(uint8_t)param->m_val[2], - (unsigned int)(uint8_t)param->m_val[3], - (unsigned int)(uint8_t)param->m_val[4], - (unsigned int)*(uint16_t*)(param->m_val+5), - (unsigned int)(uint8_t)param->m_val[7], - (unsigned int)(uint8_t)param->m_val[8], - (unsigned int)(uint8_t)param->m_val[9], - (unsigned int)(uint8_t)param->m_val[10], - (unsigned int)*(uint16_t*)(param->m_val+11)); + m_paramstr_storage.size(), + "%s", + straddr.c_str()); } else { @@ -874,46 +1849,45 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s "INVALID IPv4"); } } - else if(param->m_val[0] == PPM_AF_INET6) + else if(payload[0] == PPM_AF_INET6) { - if(param->m_len == 1 + 16 + 2 + 16 + 2) + if(payload_len == 1 + 16 + 2 + 16 + 2) { - uint8_t* sip6 = (uint8_t*)param->m_val + 1; - uint8_t* dip6 = (uint8_t*)param->m_val + 19; - uint8_t* sip = (uint8_t*)param->m_val + 13; - uint8_t* dip = (uint8_t*)param->m_val + 31; + uint8_t* sip6 = (uint8_t*)payload + 1; + uint8_t* dip6 = (uint8_t*)payload + 19; + uint8_t* sip = (uint8_t*)payload + 13; + uint8_t* dip = (uint8_t*)payload + 31; if(sinsp_utils::is_ipv4_mapped_ipv6(sip6) && sinsp_utils::is_ipv4_mapped_ipv6(dip6)) { + ipv4tuple addr; + addr.m_fields.m_sip = *(uint32_t*)sip; + addr.m_fields.m_sport = *(uint16_t*)(payload+17); + addr.m_fields.m_dip = *(uint32_t*)dip; + addr.m_fields.m_dport = *(uint16_t*)(payload+35); + addr.m_fields.m_l4proto = (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN; + string straddr = ipv4tuple_to_string(&addr, m_inspector->m_hostname_and_port_resolution_enabled); + snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%u.%u.%u.%u:%u->%u.%u.%u.%u:%u", - (unsigned int)sip[0], - (unsigned int)sip[1], - (unsigned int)sip[2], - (unsigned int)sip[3], - (unsigned int)*(uint16_t*)(param->m_val + 17), - (unsigned int)dip[0], - (unsigned int)dip[1], - (unsigned int)dip[2], - (unsigned int)dip[3], - (unsigned int)*(uint16_t*)(param->m_val + 35)); + "%s", + straddr.c_str()); break; } else { char srcstr[INET6_ADDRSTRLEN]; char dststr[INET6_ADDRSTRLEN]; - if(inet_ntop(AF_INET6, sip6, srcstr, sizeof(srcstr)) && - inet_ntop(AF_INET6, sip6, dststr, sizeof(dststr))) + if(inet_ntop(AF_INET6, sip6, srcstr, sizeof(srcstr)) && + inet_ntop(AF_INET6, dip6, dststr, sizeof(dststr))) { snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "%s:%u->%s:%u", + "%s:%s->%s:%s", srcstr, - (unsigned int)*(uint16_t*)(param->m_val + 17), + port_to_string(*(uint16_t*)(payload + 17), (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN, m_inspector->m_hostname_and_port_resolution_enabled).c_str(), dststr, - (unsigned int)*(uint16_t*)(param->m_val + 35)); + port_to_string(*(uint16_t*)(payload + 35), (m_fdinfo != NULL) ? m_fdinfo->get_l4proto() : SCAP_L4_UNKNOWN, m_inspector->m_hostname_and_port_resolution_enabled).c_str()); break; } } @@ -924,28 +1898,28 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s m_paramstr_storage.size(), "INVALID IPv6"); } - else if(param->m_val[0] == AF_UNIX) + else if(payload[0] == AF_UNIX) { - ASSERT(param->m_len > 17); + ASSERT(payload_len > 17); // // Sanitize the file string. // - string sanitized_str = param->m_val + 17; - sanitized_str.erase(remove_if(sanitized_str.begin(), sanitized_str.end(), g_invalidchar()), sanitized_str.end()); + string sanitized_str = payload + 17; + sanitize_string(sanitized_str); snprintf(&m_paramstr_storage[0], - m_paramstr_storage.size(), - "%" PRIx64 "->%" PRIx64 " %s", - *(uint64_t*)(param->m_val + 1), - *(uint64_t*)(param->m_val + 9), + m_paramstr_storage.size(), + "%" PRIx64 "->%" PRIx64 " %s", + *(uint64_t*)(payload + 1), + *(uint64_t*)(payload + 9), sanitized_str.c_str()); } else { snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), - "family %d", (int)param->m_val[0]); + "family %d", (int)payload[0]); } break; case PT_FDLIST: @@ -956,7 +1930,7 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s break; } - uint16_t nfds = *(uint16_t *)param->m_val; + uint16_t nfds = *(uint16_t *)payload; uint32_t pos = 2; uint32_t spos = 0; @@ -965,7 +1939,7 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s for(j = 0; j < nfds; j++) { char tch; - int64_t fd = *(int64_t *)(param->m_val + pos); + int64_t fd = *(int64_t *)(payload + pos); sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd); if(fdinfo) @@ -977,27 +1951,28 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s tch = '?'; } - spos += snprintf(&m_paramstr_storage[0] + spos, - m_paramstr_storage.size() - spos, - "%" PRIu64 ":%c%x%c", - fd, - tch, - (uint32_t) * (int16_t *)(param->m_val + pos + 8), - (j < (uint32_t)(nfds - 1)) ? ' ' : '\0'); + int r = snprintf(&m_paramstr_storage[0] + spos, + m_paramstr_storage.size() - spos, + "%" PRIu64 ":%c%x%c", + fd, + tch, + (uint32_t) * (int16_t *)(payload + pos + 8), + (j < (uint32_t)(nfds - 1)) ? ' ' : '\0'); - if(spos < 0) + if(r < 0 || spos + r >= m_paramstr_storage.size() - 1) { m_paramstr_storage[m_paramstr_storage.size() - 1] = 0; break; } + spos += r; pos += 10; } } break; case PT_SYSCALLID: { - uint16_t scid = *(uint16_t *)param->m_val; + uint16_t scid = *(uint16_t *)payload; if(scid >= PPM_SC_MAX) { ASSERT(false); @@ -1024,8 +1999,8 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s { const char* sigstr; - ASSERT(param->m_len == sizeof(uint8_t)); - uint8_t val = *(uint8_t *)param->m_val; + ASSERT(payload_len == sizeof(uint8_t)); + uint8_t val = *(uint8_t *)payload; sigstr = sinsp_utils::signal_to_str(val); @@ -1045,36 +2020,49 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s { string sigstr; - ASSERT(param->m_len == sizeof(uint64_t)); - uint64_t val = *(uint64_t *)param->m_val; + ASSERT(payload_len == sizeof(uint64_t)); + uint64_t val = *(uint64_t *)payload; - snprintf(&m_paramstr_storage[0], + if(val == (uint64_t)(-1)) + { + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "none"); + m_resolved_paramstr_storage[0] = '\0'; + } + else + { + snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), "%" PRIu64, val); - snprintf(&m_resolved_paramstr_storage[0], - m_resolved_paramstr_storage.size(), - "%lgs", - ((double)val) / 1000000000); + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + "%lgs", + ((double)val) / 1000000000); + } } break; case PT_FLAGS8: case PT_FLAGS16: case PT_FLAGS32: { - uint32_t val = *(uint32_t *)param->m_val & (((uint64_t)1 << param->m_len * 8) - 1); + uint32_t val = (param_info->type == PT_FLAGS8) ? *(uint8_t *)payload : + (param_info->type == PT_FLAGS16) ? *(uint16_t *)payload : *(uint32_t *)payload; snprintf(&m_paramstr_storage[0], m_paramstr_storage.size(), "%" PRIu32, val); - const struct ppm_name_value *flags = m_info->params[id].symbols; + const struct ppm_name_value *flags = (const struct ppm_name_value *)m_info->params[id].info; const char *separator = ""; uint32_t initial_val = val; uint32_t j = 0; while(flags != NULL && flags->name != NULL && flags->value != initial_val) { - if((val & flags->value) == flags->value && val != 0) + // If flag is 0, then initial_val needs to be 0 for the flag to be resolved + if((flags->value == 0 && initial_val == 0) || + (flags->value != 0 && (val & flags->value) == flags->value && val != 0)) { if(m_resolved_paramstr_storage.size() < j + strlen(separator) + strlen(flags->name)) { @@ -1104,6 +2092,56 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s flags->name); } + break; + } + case PT_MODE: + { + uint32_t val = *(uint32_t *)payload; + SET_NUMERIC_FORMAT(prfmt, param_fmt, PRIo32, PRId32, PRIX32); + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + prfmt, val); + + const struct ppm_name_value *mode = (const struct ppm_name_value *)m_info->params[id].info; + const char *separator = ""; + uint32_t initial_val = val; + uint32_t j = 0; + + while(mode != NULL && mode->name != NULL && mode->value != initial_val) + { + // If mode is 0, then initial_val needs to be 0 for the mode to be resolved + if((mode->value == 0 && initial_val == 0) || + (mode->value != 0 && (val & mode->value) == mode->value && val != 0)) + { + size_t params_len = j + strlen(separator) + strlen(mode->name); + if(m_resolved_paramstr_storage.size() < params_len) + { + m_resolved_paramstr_storage.resize(params_len + 1); + } + + j += snprintf(&m_resolved_paramstr_storage[j], + m_resolved_paramstr_storage.size(), + "%s%s", + separator, + mode->name); + + separator = "|"; + // We remove current mode value to avoid duplicates + val &= ~mode->value; + } + + mode++; + } + + if(mode != NULL && mode->name != NULL) + { + j += snprintf(&m_resolved_paramstr_storage[j], + m_resolved_paramstr_storage.size(), + "%s%s", + separator, + mode->name); + } + break; } case PT_ABSTIME: @@ -1111,6 +2149,260 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s // XXX not implemented yet // ASSERT(false); + case PT_DYN: + ASSERT(false); + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "INVALID DYNAMIC PARAMETER"); + break; + case PT_UID: + { + uint32_t val = *(uint32_t *)payload; + if (val < std::numeric_limits::max()) + { + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "%d", val); + auto find_it = m_inspector->get_userlist()->find(val); + if (find_it != m_inspector->get_userlist()->end()) + { + scap_userinfo* user_info = find_it->second; + strcpy_sanitized(&m_resolved_paramstr_storage[0], user_info->name, + (uint32_t)m_resolved_paramstr_storage.size()); + } + else + { + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + ""); + } + } + else + { + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "-1"); + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + ""); + } + break; + } + case PT_GID: + { + uint32_t val = *(uint32_t *)payload; + if (val < std::numeric_limits::max()) + { + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "%d", val); + auto find_it = m_inspector->get_grouplist()->find(val); + if (find_it != m_inspector->get_grouplist()->end()) + { + scap_groupinfo* group_info = find_it->second; + strcpy_sanitized(&m_resolved_paramstr_storage[0], group_info->name, + (uint32_t)m_resolved_paramstr_storage.size()); + } + else + { + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + ""); + } + } + else + { + snprintf(&m_paramstr_storage[0], + m_paramstr_storage.size(), + "-1"); + snprintf(&m_resolved_paramstr_storage[0], + m_resolved_paramstr_storage.size(), + ""); + } + break; + } + case PT_CHARBUFARRAY: + { + ASSERT(param->m_len == sizeof(uint64_t)); + vector* strvect = (vector*)*(uint64_t *)param->m_val; + + m_paramstr_storage[0] = 0; + + while(true) + { + vector::iterator it; + vector::iterator itbeg; + bool need_to_resize = false; + + // + // Copy the arguments + // + char* dst = &m_paramstr_storage[0]; + char* dstend = &m_paramstr_storage[0] + m_paramstr_storage.size() - 2; + + for(it = itbeg = strvect->begin(); it != strvect->end(); ++it) + { + char* src = *it; + + if(it != itbeg) + { + if(dst < dstend - 1) + { + *dst++ = '.'; + } + } + + while(*src != 0 && dst < dstend) + { + *dst++ = *src++; + } + + if(dst == dstend) + { + // + // Reached the end of m_paramstr_storage, we need to resize it + // + need_to_resize = true; + break; + } + } + + if(need_to_resize) + { + m_paramstr_storage.resize(m_paramstr_storage.size() * 2); + continue; + } + + *dst = 0; + + break; + } + } + break; + case PT_CHARBUF_PAIR_ARRAY: + { + ASSERT(param->m_len == sizeof(uint64_t)); + pair*, vector*>* pairs = + (pair*, vector*>*)*(uint64_t *)param->m_val; + + m_paramstr_storage[0] = 0; + + if(pairs->first->size() != pairs->second->size()) + { + ASSERT(false); + break; + } + + while(true) + { + vector::iterator it1; + vector::iterator itbeg1; + vector::iterator it2; + vector::iterator itbeg2; + bool need_to_resize = false; + + // + // Copy the arguments + // + char* dst = &m_paramstr_storage[0]; + char* dstend = &m_paramstr_storage[0] + m_paramstr_storage.size() - 2; + + for(it1 = itbeg1 = pairs->first->begin(), it2 = itbeg2 = pairs->second->begin(); + it1 != pairs->first->end(); + ++it1, ++it2) + { + char* src = *it1; + + if(it1 != itbeg1) + { + if(dst < dstend - 1) + { + *dst++ = ','; + } + } + + // + // Copy the first string + // + while(*src != 0 && dst < dstend) + { + *dst++ = *src++; + } + + if(dst < dstend - 1) + { + *dst++ = ':'; + } + + // + // Copy the second string + // + src = *it2; + while(*src != 0 && dst < dstend) + { + *dst++ = *src++; + } + + if(dst == dstend) + { + // + // Reached the end of m_paramstr_storage, we need to resize it + // + need_to_resize = true; + break; + } + } + + if(need_to_resize) + { + m_paramstr_storage.resize(m_paramstr_storage.size() * 2); + continue; + } + + *dst = 0; + + break; + } + + break; + } + case PT_SIGSET: + { + ASSERT(payload_len == sizeof(uint32_t)); + uint32_t val = *(uint32_t *)payload; + + m_resolved_paramstr_storage[0] = '\0'; + m_paramstr_storage[0] = '\0'; + + char* storage = &m_paramstr_storage[0]; + int remaining = (int)m_paramstr_storage.size(); + bool first = true; + + for(int sig = 0; sig < 32; sig++) + { + if(val & (1U << sig) ) + { + const char* sigstr = sinsp_utils::signal_to_str(sig+1); + if(sigstr) + { + int printed = snprintf(storage, remaining, + "%s%s", + !first ? " " : "", + sigstr); + if(printed >= remaining) + { + storage[remaining-1] = '\0'; + break; + } + + first = false; + storage += printed; + remaining -= printed; + } + } + } + break; + } default: ASSERT(false); snprintf(&m_paramstr_storage[0], @@ -1119,10 +2411,12 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s break; } + *resolved_str = &m_resolved_paramstr_storage[0]; + return &m_paramstr_storage[0]; } -string sinsp_evt::get_param_value_str(string &name, bool resolved) +string sinsp_evt::get_param_value_str(const string &name, bool resolved) { for(uint32_t i = 0; i < get_num_params(); i++) { @@ -1177,10 +2471,10 @@ const sinsp_evt_param* sinsp_evt::get_param_value_raw(const char* name) // // Make sure the params are actually loaded // - if(!m_params_loaded) + if((m_flags & sinsp_evt::SINSP_EF_PARAMS_LOADED) == 0) { load_params(); - m_params_loaded = true; + m_flags |= (uint32_t)sinsp_evt::SINSP_EF_PARAMS_LOADED; } // @@ -1199,29 +2493,10 @@ const sinsp_evt_param* sinsp_evt::get_param_value_raw(const char* name) return NULL; } -void sinsp_evt::load_params() -{ - uint32_t j; - uint32_t nparams; - sinsp_evt_param par; - - nparams = m_info->nparams; - uint16_t *lens = (uint16_t *)((char *)m_pevt + sizeof(struct ppm_evt_hdr)); - char *valptr = (char *)lens + nparams * sizeof(uint16_t); - m_params.clear(); - - for(j = 0; j < nparams; j++) - { - par.init(valptr, lens[j]); - m_params.push_back(par); - valptr += lens[j]; - } -} - void sinsp_evt::get_category(OUT sinsp_evt::category* cat) { - if(get_type() == PPME_GENERIC_E || - get_type() == PPME_GENERIC_X) + if(m_pevt->type == PPME_GENERIC_E || + m_pevt->type == PPME_GENERIC_X) { // // This event is a syscall that doesn't have a filler yet. @@ -1231,8 +2506,15 @@ void sinsp_evt::get_category(OUT sinsp_evt::category* cat) ASSERT(parinfo->m_len == sizeof(uint16_t)); uint16_t id = *(uint16_t *)parinfo->m_val; - cat->m_category = g_infotables.m_syscall_info_table[id].category; - cat->m_subcategory = sinsp_evt::SC_NONE; + if(id < PPM_SC_MAX) + { + cat->m_category = g_infotables.m_syscall_info_table[id].category; + cat->m_subcategory = sinsp_evt::SC_NONE; + } + else + { + ASSERT(false); + } } else { @@ -1261,12 +2543,14 @@ void sinsp_evt::get_category(OUT sinsp_evt::category* cat) switch(m_fdinfo->m_type) { case SCAP_FD_FILE: + case SCAP_FD_FILE_V2: case SCAP_FD_DIRECTORY: cat->m_subcategory = SC_FILE; break; case SCAP_FD_IPV4_SOCK: case SCAP_FD_IPV6_SOCK: cat->m_subcategory = SC_NET; + break; case SCAP_FD_IPV4_SERVSOCK: case SCAP_FD_IPV6_SERVSOCK: cat->m_subcategory = SC_NET; @@ -1281,13 +2565,14 @@ void sinsp_evt::get_category(OUT sinsp_evt::category* cat) case SCAP_FD_UNSUPPORTED: case SCAP_FD_EVENTPOLL: case SCAP_FD_TIMERFD: + case SCAP_FD_NETLINK: cat->m_subcategory = SC_OTHER; break; case SCAP_FD_UNKNOWN: cat->m_subcategory = SC_OTHER; break; default: - ASSERT(false); +// ASSERT(false); cat->m_subcategory = SC_UNKNOWN; break; } @@ -1304,3 +2589,112 @@ bool sinsp_evt::is_filtered_out() { return m_filtered_out; } + +#ifdef HAS_FILTERING +scap_dump_flags sinsp_evt::get_dump_flags(OUT bool* should_drop) +{ + uint32_t dflags = SCAP_DF_NONE; + *should_drop = false; + + if(m_filtered_out) + { + if(m_inspector->m_isfatfile_enabled) + { + ppm_event_flags eflags = get_info_flags(); + + if(eflags & EF_MODIFIES_STATE) + { + dflags = SCAP_DF_STATE_ONLY; + } + else + { + *should_drop = true; + } + } + else + { + *should_drop = true; + } + + if(*should_drop) + { + ppm_event_category ecat = get_info_category(); + if(ecat & EC_INTERNAL) + { + *should_drop = false; + } + } + } + + if(m_flags & sinsp_evt::SINSP_EF_IS_TRACER) + { + dflags |= SCAP_DF_TRACER; + } + + return (scap_dump_flags)dflags; +} +#endif + +bool sinsp_evt::simple_consumer_consider() +{ + uint16_t etype = get_type(); + + if(etype == PPME_GENERIC_E || etype == PPME_GENERIC_X) + { + sinsp_evt_param *parinfo = get_param(0); + ASSERT(parinfo->m_len == sizeof(uint16_t)); + uint16_t scid = *(uint16_t *)parinfo->m_val; + + return sinsp::simple_consumer_consider_syscallid(scid); + } + + return sinsp::simple_consumer_consider_evtnum(etype); +} + +bool sinsp_evt::is_syscall_error() const +{ + return (m_errorcode != 0) && + (m_errorcode != SE_EINPROGRESS) && + (m_errorcode != SE_EAGAIN) && + (m_errorcode != SE_ETIMEDOUT); +} + +bool sinsp_evt::is_file_open_error() const +{ + return (m_fdinfo == nullptr) && + ((m_pevt->type == PPME_SYSCALL_OPEN_X) || + (m_pevt->type == PPME_SYSCALL_CREAT_X) || + (m_pevt->type == PPME_SYSCALL_OPENAT_X) || + (m_pevt->type == PPME_SYSCALL_OPENAT_2_X)); +} + +bool sinsp_evt::is_file_error() const +{ + return is_file_open_error() || + ((m_fdinfo != nullptr) && + ((m_fdinfo->m_type == SCAP_FD_FILE) || + (m_fdinfo->m_type == SCAP_FD_FILE_V2))); +} + +bool sinsp_evt::is_network_error() const +{ + if(m_fdinfo != nullptr) + { + return (m_fdinfo->m_type == SCAP_FD_IPV4_SOCK) || + (m_fdinfo->m_type == SCAP_FD_IPV6_SOCK); + } + else + { + return (m_pevt->type == PPME_SOCKET_ACCEPT_X) || + (m_pevt->type == PPME_SOCKET_ACCEPT4_X) || + (m_pevt->type == PPME_SOCKET_ACCEPT_5_X) || + (m_pevt->type == PPME_SOCKET_ACCEPT4_5_X) || + (m_pevt->type == PPME_SOCKET_CONNECT_X) || + (m_pevt->type == PPME_SOCKET_BIND_X); + } +} + +uint64_t sinsp_evt::get_lastevent_ts() const +{ + return m_tinfo->m_lastevent_ts; +} diff --git a/userspace/libsinsp/event.h b/userspace/libsinsp/event.h index b59a4e9fd4..c85924ab86 100644 --- a/userspace/libsinsp/event.h +++ b/userspace/libsinsp/event.h @@ -1,39 +1,56 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once +#include #ifndef VISIBILITY_PRIVATE #define VISIBILITY_PRIVATE private: #endif +#include "sinsp_inet.h" +#include "sinsp_public.h" +#include "scap.h" +#include "gen_filter.h" +#include "../../driver/ppm_events_public.h" +#include "settings.h" + typedef class sinsp sinsp; typedef class sinsp_threadinfo sinsp_threadinfo; + +namespace test_helpers { + class event_builder; + class sinsp_mock; +} + + /////////////////////////////////////////////////////////////////////////////// // Event arguments /////////////////////////////////////////////////////////////////////////////// typedef enum filtercheck_field_flags { - EPF_NONE = 0, - EPF_FILTER_ONLY, ///< this field can only be used as a filter. - EPF_PRINT_ONLY, ///< this field can only be printed. - EPF_REQUIRES_ARGUMENT, ///< this field includes an argument, under the form 'property.argument'. + EPF_NONE = 0, + EPF_FILTER_ONLY = 1 << 0, ///< this field can only be used as a filter. + EPF_PRINT_ONLY = 1 << 1, ///< this field can only be printed. + EPF_REQUIRES_ARGUMENT = 1 << 2, ///< this field includes an argument, under the form 'property.argument'. + EPF_TABLE_ONLY = 1 << 3, ///< this field is designed to be used in a table and won't appear in the list created by sysdig's '-l'. }filtercheck_field_flags; /*! @@ -43,7 +60,7 @@ typedef struct filtercheck_field_info { ppm_param_type m_type; ///< Field type. filtercheck_field_flags m_flags; ///< Field flags. - ppm_print_format m_print_format; ///< If this is a numeric field, this flag specifies if it should be rendered as decimal or hex. + ppm_print_format m_print_format; ///< If this is a numeric field, this flag specifies if it should be rendered as octal, decimal or hex. char m_name[64]; ///< Field name. char m_description[1024]; ///< Field description. }filtercheck_field_info; @@ -71,21 +88,25 @@ class SINSP_PUBLIC sinsp_evt_param { public: char* m_val; ///< Pointer to the event parameter data. - uint16_t m_len; ///< Lenght os the parameter pointed by m_val. + uint16_t m_len; ///< Length of the parameter pointed by m_val. private: - void init(char* valptr, uint16_t len); + inline void init(char* valptr, uint16_t len) + { + m_val = valptr; + m_len = len; + } friend class sinsp_evt; }; /*! \brief Event class. - This class is returned by \ref sinsp::next() and encapsulates the state - related to a captured event, and includes a bunch of members to manipulate - events and their parameters, including parsing, formatting and extracting + This class is returned by \ref sinsp::next() and encapsulates the state + related to a captured event, and includes a bunch of members to manipulate + events and their parameters, including parsing, formatting and extracting state like the event process or FD. */ -class SINSP_PUBLIC sinsp_evt +class SINSP_PUBLIC sinsp_evt : public gen_event { public: /*! @@ -93,12 +114,18 @@ class SINSP_PUBLIC sinsp_evt */ enum param_fmt { - PF_NORMAL, ///< Normal screen output - PF_JSON, ///< Json formatting - PF_SIMPLE, ///< Reduced output, e.g. not type character for FDs - PF_HEX, ///< Hexadecimal output - PF_HEXASCII,///< Hexadecimal + ASCII output - PF_EOLS, ///< Normal + end of lines + PF_NORMAL = (1 << 0), ///< Normal screen output + PF_JSON = (1 << 1), ///< Json formatting with data in normal screen format + PF_SIMPLE = (1 << 2), ///< Reduced output, e.g. not type character for FDs + PF_HEX = (1 << 3), ///< Hexadecimal output + PF_HEXASCII = (1 << 4), ///< Hexadecimal + ASCII output + PF_EOLS = (1 << 5), ///< Normal + end of lines + PF_EOLS_COMPACT = (1 << 6), ///< Normal + end of lines but with no force EOL at the beginning + PF_BASE64 = (1 << 7), ///< Base64 output + PF_JSONEOLS = (1 << 8), ///< Json formatting with data in hexadecimal format + PF_JSONHEX = (1 << 9), ///< Json formatting with data in hexadecimal format + PF_JSONHEXASCII = (1 << 10), ///< Json formatting with data in hexadecimal + ASCII format + PF_JSONBASE64 = (1 << 11), ///< Json formatting with data in base64 format }; /*! @@ -132,45 +159,83 @@ class SINSP_PUBLIC sinsp_evt sinsp_evt(sinsp* inspector); ~sinsp_evt(); + /*! + \brief Set the inspector. + */ + void inspector(sinsp *value) + { + m_inspector = value; + } + /*! \brief Get the incremental number of this event. */ - uint64_t get_num(); + inline uint64_t get_num() const + { + return m_evtnum; + } /*! \brief Get the number of the CPU where this event was captured. */ - int16_t get_cpuid(); + inline int16_t get_cpuid() const + { + return m_cpuid; + } /*! - \brief Get the event type. - + \brief Get the event type. + \note For a list of event types, refer to \ref etypes. */ - uint16_t get_type(); + inline uint16_t get_type() const override + { + return m_pevt->type; + } /*! \brief Get the event's flags. */ - ppm_event_flags get_flags(); + inline ppm_event_flags get_info_flags() const + { + return m_info->flags; + } + + /*! + \brief Get the event's category. + */ + inline ppm_event_category get_info_category() const + { + return m_info->category; + } /*! \brief Return the event direction: in or out. */ - event_direction get_direction(); + event_direction get_direction() const; /*! \brief Get the event timestamp. \return The event timestamp, in nanoseconds from epoch */ - uint64_t get_ts(); - + inline uint64_t get_ts() const override + { + return m_pevt->ts; + } /*! \brief Return the event name string, e.g. 'open' or 'socket'. */ - const char* get_name(); + const char* get_name() const; + + /*! + \brief Return the event category. + */ + inline ppm_event_category get_category() const + { + return m_info->category; + } /*! \brief Get the ID of the thread that generated the event. @@ -189,16 +254,29 @@ class SINSP_PUBLIC sinsp_evt /*! \brief Return the information about the FD on which this event operated. - \note For events that are not I/O related, get_fd_info() returns NULL. + \note For events that are not I/O related, get_fd_info() returns NULL. */ - sinsp_fdinfo_t* get_fd_info(); + inline sinsp_fdinfo_t* get_fd_info() + { + return m_fdinfo; + } + + inline bool fdinfo_name_changed() const + { + return m_fdinfo_name_changed; + } + + inline void set_fdinfo_name_changed(bool changed) + { + m_fdinfo_name_changed = changed; + } /*! \brief Return the number of the FD associated with this event. - \note For events that are not I/O related, get_fd_num() returns sinsp_evt::INVALID_FD_NUM. + \note For events that are not I/O related, get_fd_num() returns sinsp_evt::INVALID_FD_NUM. */ - uint64_t get_fd_num(); + int64_t get_fd_num(); /*! \brief Return the number of parameters that this event has. @@ -217,7 +295,7 @@ class SINSP_PUBLIC sinsp_evt \param id The parameter number. - \note Refer to the g_event_info structure in driver/event_table.c for + \note Refer to the g_event_info structure in driver/event_table.c for a list of event descriptions. */ const struct ppm_param_info* get_param_info(uint32_t id); @@ -240,11 +318,11 @@ class SINSP_PUBLIC sinsp_evt \brief Get a parameter as a C++ string. \param name The parameter name. - \param resolved If true, the library will try to resolve the parameter + \param resolved If true, the library will try to resolve the parameter before returning it. For example, and FD number will be converted into the correspondent file, TCP tuple, etc. */ - string get_param_value_str(string& name, bool resolved = true); + std::string get_param_value_str(const std::string& name, bool resolved = true); /*! \brief Return the event's category, based on the event type and the FD on @@ -257,8 +335,55 @@ class SINSP_PUBLIC sinsp_evt \brief Return true if the event has been rejected by the filtering system. */ bool is_filtered_out(); + scap_dump_flags get_dump_flags(OUT bool* should_drop); #endif + /*! + \brief Return whether or not a simple consumer that privileges low overhead to + full event capture should consider this event. (Generally, these events are + automatically filtered out, but some events related to internal tracking are + returned by next() anyway). + */ + + bool simple_consumer_consider(); + + inline uint16_t get_source() const override + { + return ESRC_SINSP; + } + + /*! + \brief Returns true if this event represents a system call error, + false otherwise. + */ + bool is_syscall_error() const; + + /*! + \brief Returns true if this event represents a file open system + call error, false otherwise. + + Precondition: is_syscall_error() must return true. + */ + bool is_file_open_error() const; + + /*! + \brief Returns true if this event represents a file-related system + call error (including open errors), false otherwise. + + Precondition: is_syscall_error() must return true. + */ + bool is_file_error() const; + + /*! + \brief Returns true if this event represents a network-related system + call error, false otherwise. + + Precondition: is_syscall_error() must return true. + */ + bool is_network_error() const; + + uint64_t get_lastevent_ts() const; + // Doxygen doesn't understand VISIBILITY_PRIVATE #ifdef _DOXYGEN private: @@ -266,48 +391,145 @@ class SINSP_PUBLIC sinsp_evt void set_iosize(uint32_t size); uint32_t get_iosize(); + + std::string get_base_dir(uint32_t id, sinsp_threadinfo *tinfo); + const char* get_param_as_str(uint32_t id, OUT const char** resolved_str, param_fmt fmt = PF_NORMAL); + Json::Value get_param_as_json(uint32_t id, OUT const char** resolved_str, param_fmt fmt = PF_NORMAL); + const char* get_param_value_str(const char* name, OUT const char** resolved_str, param_fmt fmt = PF_NORMAL); - void init(); - void init(uint8_t* evdata, uint16_t cpuid); - void load_params(); - string get_param_value_str(uint32_t id, bool resolved); - string get_param_value_str(const char* name, bool resolved = true); + inline void init_keep_threadinfo() + { + m_flags = EF_NONE; + m_info = &(m_event_info_table[m_pevt->type]); + m_fdinfo = NULL; + m_fdinfo_name_changed = false; + m_iosize = 0; + m_poriginal_evt = NULL; + } + inline void init() + { + init_keep_threadinfo(); + m_tinfo_ref.reset(); + m_tinfo = NULL; + } + inline void init(uint8_t* evdata, uint16_t cpuid) + { + m_flags = EF_NONE; + m_pevt = (scap_evt *)evdata; + m_info = &(m_event_info_table[m_pevt->type]); + m_tinfo_ref.reset(); + m_tinfo = NULL; + m_fdinfo = NULL; + m_fdinfo_name_changed = false; + m_iosize = 0; + m_cpuid = cpuid; + m_evtnum = 0; + m_poriginal_evt = NULL; + } + inline void init(scap_evt *scap_event, + ppm_event_info * ppm_event, + sinsp_threadinfo *threadinfo, + sinsp_fdinfo_t *fdinfo) + { + m_pevt = scap_event; + m_info = ppm_event; + m_tinfo_ref.reset(); // we don't own the threadinfo so don't try to manage its lifetime + m_tinfo = threadinfo; + m_fdinfo = fdinfo; + } + inline void load_params() + { + uint32_t j; + uint32_t nparams; + sinsp_evt_param par; + + // If we're reading a capture created with a newer version, it may contain + // new parameters. If instead we're reading an older version, the current + // event table entry may contain new parameters. + // Use the minimum between the two values. + nparams = m_info->nparams < m_pevt->nparams ? m_info->nparams : m_pevt->nparams; + uint16_t *lens = (uint16_t *)((char *)m_pevt + sizeof(struct ppm_evt_hdr)); + // The offset in the block is instead always based on the capture value. + char *valptr = (char *)lens + m_pevt->nparams * sizeof(uint16_t); + m_params.clear(); + + for(j = 0; j < nparams; j++) + { + par.init(valptr, lens[j]); + m_params.push_back(par); + valptr += lens[j]; + } + } + std::string get_param_value_str(uint32_t id, bool resolved); + std::string get_param_value_str(const char* name, bool resolved = true); + char* render_fd(int64_t fd, const char** resolved_str, sinsp_evt::param_fmt fmt); + int render_fd_json(Json::Value *ret, int64_t fd, const char** resolved_str, sinsp_evt::param_fmt fmt); + uint32_t get_dump_flags(); VISIBILITY_PRIVATE + enum flags + { + SINSP_EF_NONE = 0, + SINSP_EF_PARAMS_LOADED = 1, + SINSP_EF_IS_TRACER = (1 << 1), + }; sinsp* m_inspector; scap_evt* m_pevt; + scap_evt* m_poriginal_evt; // This is used when the original event is replaced by a different one (e.g. in the case of user events) + char *m_pevt_storage; // In some cases an alternate buffer is used to hold m_pevt. This points to that storage. uint16_t m_cpuid; uint64_t m_evtnum; + uint32_t m_flags; bool m_params_loaded; const struct ppm_event_info* m_info; - vector m_params; + std::vector m_params; - // Note: this is a lot of storage. We assume that it's not a bit deal since - // currently there's no case in which more than one single event is - // needed by the library users. We'll optmize this when we'll have the - // need. - vector m_paramstr_storage; - vector m_resolved_paramstr_storage; + std::vector m_paramstr_storage; + std::vector m_resolved_paramstr_storage; + // reference to keep threadinfo alive. currently only used for synthetic container event thread info + // it should either be null, or point to the same place as m_tinfo + std::shared_ptr m_tinfo_ref; sinsp_threadinfo* m_tinfo; sinsp_fdinfo_t* m_fdinfo; + + // If true, then the associated fdinfo changed names as a part + // of parsing this event. + bool m_fdinfo_name_changed; + uint32_t m_iosize; int32_t m_errorcode; + int32_t m_rawbuf_str_len; #ifdef HAS_FILTERING bool m_filtered_out; #endif + const struct ppm_event_info* m_event_info_table; friend class sinsp; friend class sinsp_parser; friend class sinsp_threadinfo; friend class sinsp_analyzer; friend class sinsp_filter_check_event; + friend class sinsp_filter_check_thread; + friend class sinsp_evttype_filter; friend class sinsp_dumper; friend class sinsp_analyzer_fd_listener; friend class sinsp_analyzer_parsers; + friend class lua_cbacks; + friend class sinsp_container_manager; + friend class sinsp_table; + friend class sinsp_cursesui; + friend class sinsp_baseliner; + friend class capture_job_handler; + friend class capture_job; + friend class sinsp_memory_dumper; + friend class sinsp_memory_dumper_job; + friend class protocol_manager; + friend class test_helpers::event_builder; + friend class test_helpers::sinsp_mock; }; /*@}*/ diff --git a/userspace/libsinsp/eventformatter.cpp b/userspace/libsinsp/eventformatter.cpp index a991191903..57f7b8fba8 100644 --- a/userspace/libsinsp/eventformatter.cpp +++ b/userspace/libsinsp/eventformatter.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "sinsp.h" @@ -75,20 +76,60 @@ void sinsp_evt_formatter::set_format(const string& fmt) const char* cfmt = lfmt.c_str(); m_tokens.clear(); + uint32_t lfmtlen = (uint32_t)lfmt.length(); - for(j = 0; j < lfmt.length(); j++) + for(j = 0; j < lfmtlen; j++) { if(cfmt[j] == '%') { + int toklen = 0; + if(last_nontoken_str_start != j) { rawstring_check* newtkn = new rawstring_check(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start)); - m_tokens.push_back(newtkn); + m_tokens.emplace_back(make_pair("", newtkn)); + m_tokenlens.push_back(0); m_chks_to_free.push_back(newtkn); } - sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(string(cfmt + j + 1), - m_inspector, + if(j == lfmtlen - 1) + { + throw sinsp_exception("invalid formatting syntax: formatting cannot end with a %"); + } + + // + // If the field specifier starts with a number, it means that we have a length modifier + // + if(isdigit(cfmt[j + 1])) + { + // + // Parse the token length + // + sscanf(cfmt+ j + 1, "%d", &toklen); + + // + // Advance until the beginning of the field name + // + while(true) + { + if(j == lfmtlen - 1) + { + throw sinsp_exception("invalid formatting syntax: formatting cannot end with a number"); + } + else if(isdigit(cfmt[j + 1])) + { + j++; + continue; + } + else + { + break; + } + } + } + + sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(string(cfmt + j + 1), + m_inspector, false); if(chk == NULL) @@ -98,10 +139,14 @@ void sinsp_evt_formatter::set_format(const string& fmt) m_chks_to_free.push_back(chk); - j += chk->parse_field_name(cfmt + j + 1); + const char * fstart = cfmt + j + 1; + uint32_t fsize = chk->parse_field_name(fstart, true, false); + + j += fsize; ASSERT(j <= lfmt.length()); - m_tokens.push_back(chk); + m_tokens.emplace_back(make_pair(string(fstart, fsize), chk)); + m_tokenlens.push_back(toklen); last_nontoken_str_start = j + 1; } @@ -109,37 +154,142 @@ void sinsp_evt_formatter::set_format(const string& fmt) if(last_nontoken_str_start != j) { - m_tokens.push_back(new rawstring_check(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start))); + sinsp_filter_check * chk = new rawstring_check(lfmt.substr(last_nontoken_str_start, j - last_nontoken_str_start)); + m_tokens.emplace_back(make_pair("", chk)); + m_chks_to_free.push_back(chk); + m_tokenlens.push_back(0); } } +bool sinsp_evt_formatter::on_capture_end(OUT string* res) +{ + res->clear(); + return res->size() > 0; +} + +bool sinsp_evt_formatter::resolve_tokens(sinsp_evt *evt, map& values) +{ + bool retval = true; + const filtercheck_field_info* fi; + uint32_t j = 0; + + ASSERT(m_tokenlens.size() == m_tokens.size()); + + for(j = 0; j < m_tokens.size(); j++) + { + char* str = m_tokens[j].second->tostring(evt); + + if(str == NULL) + { + if(m_require_all_values) + { + retval = false; + break; + } + else + { + str = (char*)""; + } + } + + fi = m_tokens[j].second->get_field_info(); + if(fi) + { + values[m_tokens[j].first] = string(str); + } + } + + return retval; +} + + bool sinsp_evt_formatter::tostring(sinsp_evt* evt, OUT string* res) { + bool retval = true; + const filtercheck_field_info* fi; + + uint32_t j = 0; vector::iterator it; res->clear(); - for(it = m_tokens.begin(); it != m_tokens.end(); ++it) - { - char* str = (*it)->tostring(evt); + ASSERT(m_tokenlens.size() == m_tokens.size()); - if(str != NULL) + for(j = 0; j < m_tokens.size(); j++) + { + if(m_inspector->get_buffer_format() == sinsp_evt::PF_JSON + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONEOLS + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONHEX + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONHEXASCII + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONBASE64) { - (*res) += str; + Json::Value json_value = m_tokens[j].second->tojson(evt); + + if(retval == false) + { + continue; + } + + if(json_value == Json::nullValue && m_require_all_values) + { + retval = false; + continue; + } + + fi = m_tokens[j].second->get_field_info(); + + if(fi) + { + m_root[m_tokens[j].first] = m_tokens[j].second->tojson(evt); + } } else { - if(m_require_all_values) + char* str = m_tokens[j].second->tostring(evt); + + if(retval == false) { - return false; + continue; + } + + if(str == NULL) + { + if(m_require_all_values) + { + retval = false; + continue; + } + else + { + str = (char*)""; + } + } + + uint32_t tks = m_tokenlens[j]; + + if(tks != 0) + { + string sstr(str); + sstr.resize(tks, ' '); + (*res) += sstr; } else { - (*res) += ""; + (*res) += str; } } } - return true; + if(m_inspector->get_buffer_format() == sinsp_evt::PF_JSON + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONEOLS + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONHEX + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONHEXASCII + || m_inspector->get_buffer_format() == sinsp_evt::PF_JSONBASE64) + { + (*res) = m_writer.write(m_root); + (*res) = res->substr(0, res->size() - 1); + } + + return retval; } #else // HAS_FILTERING @@ -150,12 +300,49 @@ sinsp_evt_formatter::sinsp_evt_formatter(sinsp* inspector, const string& fmt) void sinsp_evt_formatter::set_format(const string& fmt) { - throw sinsp_exception("sinsp_evt_formatter unvavailable because it was not compiled in the library"); + throw sinsp_exception("sinsp_evt_formatter unavailable because it was not compiled in the library"); +} + +bool sinsp_evt_formatter::resolve_tokens(sinsp_evt *evt, map& values) +{ + throw sinsp_exception("sinsp_evt_formatter unavailable because it was not compiled in the library"); } bool sinsp_evt_formatter::tostring(sinsp_evt* evt, OUT string* res) { - throw sinsp_exception("sinsp_evt_formatter unvavailable because it was not compiled in the library"); - return false; + throw sinsp_exception("sinsp_evt_formatter unavailable because it was not compiled in the library"); } #endif // HAS_FILTERING + +sinsp_evt_formatter_cache::sinsp_evt_formatter_cache(sinsp *inspector) + : m_inspector(inspector) +{ +} + +sinsp_evt_formatter_cache::~sinsp_evt_formatter_cache() +{ +} + +std::shared_ptr& sinsp_evt_formatter_cache::get_cached_formatter(string &format) +{ + auto it = m_formatter_cache.lower_bound(format); + + if(it == m_formatter_cache.end() || + it->first != format) + { + it = m_formatter_cache.emplace_hint(it, + std::make_pair(format, make_shared(m_inspector, format))); + } + + return it->second; +} + +bool sinsp_evt_formatter_cache::resolve_tokens(sinsp_evt *evt, string &format, map& values) +{ + return get_cached_formatter(format)->resolve_tokens(evt, values); +} + +bool sinsp_evt_formatter_cache::tostring(sinsp_evt *evt, string &format, OUT string *res) +{ + return get_cached_formatter(format)->tostring(evt, res); +} diff --git a/userspace/libsinsp/eventformatter.h b/userspace/libsinsp/eventformatter.h index e2df6f936c..72ef0ee61d 100644 --- a/userspace/libsinsp/eventformatter.h +++ b/userspace/libsinsp/eventformatter.h @@ -1,22 +1,24 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once +#include class sinsp_filter_check; @@ -35,33 +37,91 @@ class SINSP_PUBLIC sinsp_evt_formatter /*! \brief Constructs a formatter. - \param inspector Pointer to the inspector instance that will generate the + \param inspector Pointer to the inspector instance that will generate the events to be formatter. \param fmt The printf-like format to use. The accepted format is the same - as the one of the sysdig '-p' command line flag, so refer to the sysdig - manual for details. + as the one of the sysdig '-p' command line flag, so refer to the sysdig + manual for details. */ sinsp_evt_formatter(sinsp* inspector, const string& fmt); ~sinsp_evt_formatter(); + /*! + \brief Resolve all the formatted tokens and return them in a key/value + map. + + \param evt Pointer to the event to be converted into string. + \param res Reference to the map that will be filled with the result. + + \return true if all the tokens can be retrieved successfully, false + otherwise. + */ + bool resolve_tokens(sinsp_evt *evt, map& values); + /*! \brief Fills res with the string rendering of the event. \param evt Pointer to the event to be converted into string. - \param res Pointer to the string that will be filled with the result. + \param res Pointer to the string that will be filled with the result. - \return true if the string should be shown (based on the initial *), + \return true if the string should be shown (based on the initial *), false otherwise. */ bool tostring(sinsp_evt* evt, OUT string* res); + /*! + \brief Fills res with end of capture string rendering of the event. + \param res Pointer to the string that will be filled with the result. + + \return true if there is a string to show (based on the format), + false otherwise. + */ + bool on_capture_end(OUT string* res); + private: void set_format(const string& fmt); - vector m_tokens; + + // vector of (full string of the token, filtercheck) pairs + // e.g. ("proc.aname[2], ptr to sinsp_filter_check_thread) + vector> m_tokens; + vector m_tokenlens; sinsp* m_inspector; bool m_require_all_values; vector m_chks_to_free; + + Json::Value m_root; + Json::FastWriter m_writer; }; +/*! + \brief Caching version of sinsp_evt_formatter + This class is a wrapper around sinsp_evt_formatter, maintaining a + cache of previously seen formatters. It avoids the overhead of + recreating sinsp_evt_formatter objects for each event. +*/ +class SINSP_PUBLIC sinsp_evt_formatter_cache +{ +public: + sinsp_evt_formatter_cache(sinsp *inspector); + virtual ~sinsp_evt_formatter_cache(); + + // Resolve the tokens inside format and return them as a key/value map. + // Creates a new sinsp_evt_formatter object if necessary. + bool resolve_tokens(sinsp_evt *evt, std::string &format, map& values); + + // Fills in res with the event formatted according to + // format. Creates a new sinsp_evt_formatter object if + // necessary. + bool tostring(sinsp_evt *evt, std::string &format, OUT std::string *res); + +private: + + // Get the formatter for this format string. Creates a new + // sinsp_evt_formatter object if necessary. + std::shared_ptr& get_cached_formatter(string &format); + + std::map> m_formatter_cache; + sinsp *m_inspector; +}; /*@}*/ diff --git a/userspace/libsinsp/fdinfo.cpp b/userspace/libsinsp/fdinfo.cpp index 04c91f4933..cfa53530e4 100644 --- a/userspace/libsinsp/fdinfo.cpp +++ b/userspace/libsinsp/fdinfo.cpp @@ -1,35 +1,49 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #ifndef _WIN32 #define __STDC_FORMAT_MACROS #include +#include #endif #include "sinsp.h" #include "sinsp_int.h" +#include "scap-int.h" /////////////////////////////////////////////////////////////////////////////// -// sinsp_fdinfo inomlementation +// sinsp_fdinfo implementation /////////////////////////////////////////////////////////////////////////////// template<> sinsp_fdinfo_t::sinsp_fdinfo() { m_type = SCAP_FD_UNINITIALIZED; m_flags = FLAGS_NONE; + m_callbacks = NULL; + m_usrstate = NULL; +} + +template<> void sinsp_fdinfo_t::reset() +{ + m_type = SCAP_FD_UNINITIALIZED; + m_flags = FLAGS_NONE; + delete(m_callbacks); + m_callbacks = NULL; + m_usrstate = NULL; } template<> string* sinsp_fdinfo_t::tostring() @@ -41,6 +55,7 @@ template<> char sinsp_fdinfo_t::get_typechar() { switch(m_type) { + case SCAP_FD_FILE_V2: case SCAP_FD_FILE: return CHAR_FD_FILE; case SCAP_FD_IPV4_SOCK: @@ -71,58 +86,68 @@ template<> char sinsp_fdinfo_t::get_typechar() return CHAR_FD_INOTIFY; case SCAP_FD_TIMERFD: return CHAR_FD_TIMERFD; + case SCAP_FD_NETLINK: + return CHAR_FD_NETLINK; default: - ASSERT(false); +// ASSERT(false); return '?'; } } -template<> void sinsp_fdinfo_t::add_filename(const char* directory, uint32_t directorylen, const char* filename, uint32_t filenamelen) +template<> char* sinsp_fdinfo_t::get_typestring() { - char fullpath[SCAP_MAX_PATH_SIZE]; + switch(m_type) + { + case SCAP_FD_FILE_V2: + case SCAP_FD_FILE: + return (char*)"file"; + case SCAP_FD_DIRECTORY: + return (char*)"directory"; + case SCAP_FD_IPV4_SOCK: + case SCAP_FD_IPV4_SERVSOCK: + return (char*)"ipv4"; + case SCAP_FD_IPV6_SOCK: + case SCAP_FD_IPV6_SERVSOCK: + return (char*)"ipv6"; + case SCAP_FD_UNIX_SOCK: + return (char*)"unix"; + case SCAP_FD_FIFO: + return (char*)"pipe"; + case SCAP_FD_EVENT: + return (char*)"event"; + case SCAP_FD_SIGNALFD: + return (char*)"signalfd"; + case SCAP_FD_EVENTPOLL: + return (char*)"eventpoll"; + case SCAP_FD_INOTIFY: + return (char*)"inotify"; + case SCAP_FD_TIMERFD: + return (char*)"timerfd"; + case SCAP_FD_NETLINK: + return (char*)"netlink"; + default: + return (char*)""; + } +} - sinsp_utils::concatenate_paths(fullpath, SCAP_MAX_PATH_SIZE, directory, directorylen, filename, filenamelen); - +template<> string sinsp_fdinfo_t::tostring_clean() +{ + string m_tstr = m_name; + sanitize_string(m_tstr); + + return m_tstr; +} + +template<> void sinsp_fdinfo_t::add_filename(const char* fullpath) +{ m_name = fullpath; } template<> bool sinsp_fdinfo_t::set_net_role_by_guessing(sinsp* inspector, - sinsp_threadinfo* ptinfo, + sinsp_threadinfo* ptinfo, sinsp_fdinfo_t* pfdinfo, bool incoming) { -/* - bool is_sip_local = - inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(pfdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip); - bool is_dip_local = - inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(pfdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip); - - // - // If only the client is local, mark the role as client. - // If only the server is local, mark the role as server. - // - if(is_sip_local) - { - if(!is_dip_local) - { - pfdinfo->set_role_client(); - return true; - } - } - else if(is_dip_local) - { - if(!is_sip_local) - { - pfdinfo->set_role_server(); - return true; - } - } - - // - // Both addresses are local - // - ASSERT(is_sip_local && is_dip_local); -*/ // // If this process owns the port, mark it as server, otherwise mark it as client // @@ -167,6 +192,11 @@ template<> scap_l4_proto sinsp_fdinfo_t::get_l4proto() if(evt_type == SCAP_FD_IPV4_SOCK) { + if((scap_l4_proto)m_sockinfo.m_ipv4info.m_fields.m_l4proto == SCAP_L4_RAW) + { + return SCAP_L4_RAW; + } + if(is_role_none()) { return SCAP_L4_NA; @@ -180,6 +210,11 @@ template<> scap_l4_proto sinsp_fdinfo_t::get_l4proto() } else if(evt_type == SCAP_FD_IPV6_SOCK) { + if((scap_l4_proto)m_sockinfo.m_ipv6info.m_fields.m_l4proto == SCAP_L4_RAW) + { + return SCAP_L4_RAW; + } + if(is_role_none()) { return SCAP_L4_NA; @@ -197,89 +232,128 @@ template<> scap_l4_proto sinsp_fdinfo_t::get_l4proto() } } -/////////////////////////////////////////////////////////////////////////////// -// sinsp_fdtable implementation -/////////////////////////////////////////////////////////////////////////////// -sinsp_fdtable::sinsp_fdtable(sinsp* inspector) -{ - m_inspector = inspector; - reset_cache(); -} - -sinsp_fdinfo_t* sinsp_fdtable::find(int64_t fd) +template<> void sinsp_fdinfo_t::register_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec) { - unordered_map::iterator fdit = m_table.find(fd); + if(this->m_callbacks == NULL) + { + m_callbacks = new fd_callbacks_info(); + } - // - // Try looking up in our simple cache - // - if(m_last_accessed_fd != -1 && fd == m_last_accessed_fd) + switch(etype) { -#ifdef GATHER_INTERNAL_STATS - m_inspector->m_stats.m_n_cached_fd_lookups++; -#endif - return m_last_accessed_fdinfo; + case CT_READ: + m_callbacks->m_read_callbacks.push_back(dec); + break; + case CT_WRITE: + m_callbacks->m_write_callbacks.push_back(dec); + break; + default: + ASSERT(false); + break; } - // - // Caching failed, do a real lookup - // - fdit = m_table.find(fd); + return; +} - if(fdit == m_table.end()) +template<> void sinsp_fdinfo_t::unregister_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec) +{ + vector::iterator it; + + if(m_callbacks == NULL) { -#ifdef GATHER_INTERNAL_STATS - m_inspector->m_stats.m_n_failed_fd_lookups++; -#endif - return NULL; + ASSERT(false); + return; } - else + + switch(etype) { -#ifdef GATHER_INTERNAL_STATS - m_inspector->m_stats.m_n_noncached_fd_lookups++; -#endif - m_last_accessed_fd = fd; - m_last_accessed_fdinfo = &(fdit->second); - return &(fdit->second); + case CT_READ: + for(it = m_callbacks->m_read_callbacks.begin(); it != m_callbacks->m_read_callbacks.end(); ++it) + { + if(*it == dec) + { + m_callbacks->m_read_callbacks.erase(it); + return; + } + } + + break; + case CT_WRITE: + for(it = m_callbacks->m_write_callbacks.begin(); it != m_callbacks->m_write_callbacks.end(); ++it) + { + if(*it == dec) + { + m_callbacks->m_write_callbacks.erase(it); + return; + } + } + + break; + default: + ASSERT(false); + break; } + + return; } -sinsp_fdinfo_t* sinsp_fdtable::add(int64_t fd, sinsp_fdinfo_t* fdinfo) +/////////////////////////////////////////////////////////////////////////////// +// sinsp_fdtable implementation +/////////////////////////////////////////////////////////////////////////////// +sinsp_fdtable::sinsp_fdtable(sinsp* inspector) { - pair::iterator, bool> insert_res; - - insert_res = m_table.insert(std::make_pair(fd,*fdinfo)); + m_inspector = inspector; + reset_cache(); +} +sinsp_fdinfo_t* sinsp_fdtable::add(int64_t fd, sinsp_fdinfo_t* fdinfo) +{ // // Look for the FD in the table // - if(insert_res.second == true) + auto it = m_table.find(fd); + + // Three possible exits here: + // 1. fd is not on the table + // a. the table size is under the limit so create a new entry + // b. table size is over the limit, discard the fd + // 2. fd is already in the table, replace it + if(it == m_table.end()) { - // - // No entry in the table, this is the normal case - // - m_last_accessed_fd = -1; + if(m_table.size() < m_inspector->m_max_fdtable_size) + { + // + // No entry in the table, this is the normal case + // + m_last_accessed_fd = -1; #ifdef GATHER_INTERNAL_STATS - m_inspector->m_stats.m_n_added_fds++; + m_inspector->m_stats.m_n_added_fds++; #endif + pair::iterator, bool> insert_res = m_table.emplace(fd, *fdinfo); + return &(insert_res.first->second); + } + else + { + return nullptr; + } } else { // // the fd is already in the table. // - if(insert_res.first->second.m_flags & sinsp_fdinfo_t::FLAGS_CLOSE_IN_PROGRESS) + if(it->second.m_flags & sinsp_fdinfo_t::FLAGS_CLOSE_IN_PROGRESS) { // // Sometimes an FD-creating syscall can be called on an FD that is being closed (i.e - // the close enter has arrived but the close exit has not arrived yet). + // the close enter has arrived but the close exit has not arrived yet). // If this is the case, mark the new entry so that the successive close exit won't // destroy it. // fdinfo->m_flags &= ~sinsp_fdinfo_t::FLAGS_CLOSE_IN_PROGRESS; fdinfo->m_flags |= sinsp_fdinfo_t::FLAGS_CLOSE_CANCELED; - - m_table[CANCELED_FD_NUMBER] = insert_res.first->second; + + m_table[CANCELED_FD_NUMBER] = it->second; } else { @@ -293,16 +367,15 @@ sinsp_fdinfo_t* sinsp_fdtable::add(int64_t fd, sinsp_fdinfo_t* fdinfo) // choice. We include an assertion to catch the situation. // // XXX Can't have this enabled until the FD_CLOEXEC flag is supported -// ASSERT(false); + //ASSERT(false); } // // Replace the fd as a struct copy // - insert_res.first->second = *fdinfo; + it->second.copy(*fdinfo, true); + return &(it->second); } - - return &(insert_res.first->second); } void sinsp_fdtable::erase(int64_t fd) @@ -311,7 +384,7 @@ void sinsp_fdtable::erase(int64_t fd) if(fd == m_last_accessed_fd) { - m_last_accessed_fd = -1; + m_last_accessed_fd = -1; } if(fdit == m_table.end()) @@ -351,3 +424,21 @@ void sinsp_fdtable::reset_cache() { m_last_accessed_fd = -1; } + +void sinsp_fdtable::lookup_device(sinsp_fdinfo_t* fdi, uint64_t fd) +{ +#ifdef HAS_CAPTURE + if(m_inspector->is_capture()) + { + return; + } + + if(fdi->is_file() && fdi->m_dev == 0 && fdi->m_mount_id != 0) + { + char procdir[SCAP_MAX_PATH_SIZE]; + snprintf(procdir, sizeof(procdir), "%s/proc/%ld/", scap_get_host_root(), m_tid); + fdi->m_dev = scap_get_device_by_mount_id(m_inspector->m_h, procdir, fdi->m_mount_id); + fdi->m_mount_id = 0; // don't try again + } +#endif +} diff --git a/userspace/libsinsp/fdinfo.h b/userspace/libsinsp/fdinfo.h index 35ebe8d3c4..87d4533b4c 100644 --- a/userspace/libsinsp/fdinfo.h +++ b/userspace/libsinsp/fdinfo.h @@ -1,22 +1,26 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once +#include "sinsp_pd_callback_type.h" +#include +#include #ifdef _WIN32 #define CANCELED_FD_NUMBER INT64_MAX @@ -24,13 +28,15 @@ along with sysdig. If not, see . #define CANCELED_FD_NUMBER std::numeric_limits::max() #endif +class sinsp_protodecoder; + // fd type characters #define CHAR_FD_FILE 'f' #define CHAR_FD_IPV4_SOCK '4' #define CHAR_FD_IPV6_SOCK '6' #define CHAR_FD_DIRECTORY 'd' -#define CHAR_FD_IPV4_SERVSOCK '2' -#define CHAR_FD_IPV6_SERVSOCK '3' +#define CHAR_FD_IPV4_SERVSOCK '4' +#define CHAR_FD_IPV6_SERVSOCK '6' #define CHAR_FD_FIFO 'p' #define CHAR_FD_UNIX_SOCK 'u' #define CHAR_FD_EVENT 'e' @@ -40,6 +46,7 @@ along with sysdig. If not, see . #define CHAR_FD_EVENTPOLL 'l' #define CHAR_FD_INOTIFY 'i' #define CHAR_FD_TIMERFD 't' +#define CHAR_FD_NETLINK 'n' /** @defgroup state State management * A collection of classes to query process and FD state. @@ -55,6 +62,13 @@ typedef union _sinsp_sockinfo unix_tuple m_unixinfo; ///< The tuple if this a unix socket. }sinsp_sockinfo; +class fd_callbacks_info +{ +public: + std::vector m_write_callbacks; + std::vector m_read_callbacks; +}; + /*! \brief File Descriptor information class. This class contains the full state for a FD, and a bunch of functions to @@ -68,7 +82,77 @@ class SINSP_PUBLIC sinsp_fdinfo { public: sinsp_fdinfo(); - string* tostring(); + sinsp_fdinfo (const sinsp_fdinfo &other) + { + copy(other, false); + } + + ~sinsp_fdinfo() + { + if(m_callbacks != NULL) + { + delete m_callbacks; + } + + if(m_usrstate != NULL) + { + delete m_usrstate; + } + } + + sinsp_fdinfo& operator=(const sinsp_fdinfo& other) + { + copy(other, true); + return *this; + } + + void reset(); + std::string* tostring(); + + inline void copy(const sinsp_fdinfo &other, bool free_state) + { + m_type = other.m_type; + m_openflags = other.m_openflags; + m_sockinfo = other.m_sockinfo; + m_name = other.m_name; + m_oldname = other.m_oldname; + m_flags = other.m_flags; + m_dev = other.m_dev; + m_mount_id = other.m_mount_id; + m_ino = other.m_ino; + + if(free_state) + { + if(m_callbacks != NULL) + { + delete m_callbacks; + } + + if(m_usrstate != NULL) + { + delete m_usrstate; + } + } + + if(other.m_callbacks != NULL) + { + m_callbacks = new fd_callbacks_info(); + *m_callbacks = *other.m_callbacks; + } + else + { + m_callbacks = NULL; + } + + if(other.m_usrstate != NULL) + { + m_usrstate = new T(*other.m_usrstate); + } + else + { + m_usrstate = NULL; + } + } /*! \brief Return a single ASCII character that identifies the FD type. @@ -77,6 +161,18 @@ class SINSP_PUBLIC sinsp_fdinfo */ char get_typechar(); + /*! + \brief Return an ASCII string that identifies the FD type. + + Can be on of 'file', 'directory', ipv4', 'ipv6', 'unix', 'pipe', 'event', 'signalfd', 'eventpoll', 'inotify', 'signalfd'. + */ + char* get_typestring(); + + /*! + \brief Return the fd name, after removing unprintable or invalid characters from it. + */ + std::string tostring_clean(); + /*! \brief Returns true if this is a unix socket. */ @@ -125,11 +221,114 @@ class SINSP_PUBLIC sinsp_fdinfo return m_type == SCAP_FD_FIFO; } + /*! + \brief Returns true if this is a file. + */ + bool is_file() + { + return m_type == SCAP_FD_FILE || m_type == SCAP_FD_FILE_V2; + } + + /*! + \brief Returns true if this is a directory. + */ + bool is_directory() + { + return m_type == SCAP_FD_DIRECTORY; + } + + uint16_t get_serverport() + { + if(m_type == SCAP_FD_IPV4_SOCK) + { + return m_sockinfo.m_ipv4info.m_fields.m_dport; + } + else if(m_type == SCAP_FD_IPV6_SOCK) + { + return m_sockinfo.m_ipv6info.m_fields.m_dport; + } + else + { + return 0; + } + } + + uint32_t get_device() const + { + return m_dev; + } + + // see new_encode_dev in include/linux/kdev_t.h + uint32_t get_device_major() const + { + return (m_dev & 0xfff00) >> 8; + } + + // see new_encode_dev in include/linux/kdev_t.h + uint32_t get_device_minor() const + { + return (m_dev & 0xff) | ((m_dev >> 12) & 0xfff00); + } + /*! \brief If this is a socket, returns the IP protocol. Otherwise, return SCAP_FD_UNKNOWN. */ scap_l4_proto get_l4proto(); + /*! + \brief Used by protocol decoders to register callbacks related to this FD. + */ + void register_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec); + + /*! + \brief Used by protocol decoders to unregister callbacks related to this FD. + */ + void unregister_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec); + + /*! + \brief Return true if this FD is a socket server + */ + inline bool is_role_server() + { + return (m_flags & FLAGS_ROLE_SERVER) == FLAGS_ROLE_SERVER; + } + + /*! + \brief Return true if this FD is a socket client + */ + inline bool is_role_client() + { + return (m_flags & FLAGS_ROLE_CLIENT) == FLAGS_ROLE_CLIENT; + } + + /*! + \brief Return true if this FD is neither a client nor a server + */ + inline bool is_role_none() + { + return (m_flags & (FLAGS_ROLE_CLIENT | FLAGS_ROLE_SERVER)) == 0; + } + + inline bool is_socket_connected() + { + return (m_flags & FLAGS_SOCKET_CONNECTED) == FLAGS_SOCKET_CONNECTED; + } + + inline bool is_socket_pending() + { + return (m_flags & FLAGS_CONNECTION_PENDING) == FLAGS_CONNECTION_PENDING; + } + + inline bool is_socket_failed() + { + return (m_flags & FLAGS_CONNECTION_FAILED) == FLAGS_CONNECTION_FAILED; + } + + inline bool is_cloned() + { + return (m_flags & FLAGS_IS_CLONED) == FLAGS_IS_CLONED; + } + scap_fd_type m_type; ///< The fd type, e.g. file, directory, IPv4 socket... uint32_t m_openflags; ///< If this FD is a file, the flags that were used when opening it. See the PPM_O_* definitions in driver/ppm_events_public.h. @@ -139,7 +338,13 @@ class SINSP_PUBLIC sinsp_fdinfo */ sinsp_sockinfo m_sockinfo; - string m_name; ///< Human readable rendering of this FD. For files, this is the full file name. For sockets, this is the tuple. And so on. + std::string m_name; ///< Human readable rendering of this FD. For files, this is the full file name. For sockets, this is the tuple. And so on. + std::string m_oldname; // The name of this fd at the beginning of event parsing. Used to detect name changes that result from parsing an event. + + inline bool has_decoder_callbacks() + { + return (m_callbacks != NULL); + } VISIBILITY_PRIVATE @@ -155,48 +360,47 @@ VISIBILITY_PRIVATE { FLAGS_NONE = 0, FLAGS_FROM_PROC = (1 << 0), - FLAGS_TRANSACTION = (1 << 1), + //FLAGS_TRANSACTION = (1 << 1), FLAGS_ROLE_CLIENT = (1 << 2), FLAGS_ROLE_SERVER = (1 << 3), FLAGS_CLOSE_IN_PROGRESS = (1 << 4), FLAGS_CLOSE_CANCELED = (1 << 5), - // Pipe-specific flags FLAGS_IS_SOCKET_PIPE = (1 << 6), + FLAGS_IS_TRACER_FILE = (1 << 7), + FLAGS_IS_TRACER_FD = (1 << 8), + FLAGS_IS_NOT_TRACER_FD = (1 << 9), + FLAGS_IN_BASELINE_R = (1 << 10), + FLAGS_IN_BASELINE_RW = (1 << 11), + FLAGS_IN_BASELINE_OTHER = (1 << 12), + FLAGS_SOCKET_CONNECTED = (1 << 13), + FLAGS_IS_CLONED = (1 << 14), + FLAGS_CONNECTION_PENDING = (1 << 15), + FLAGS_CONNECTION_FAILED = (1 << 16), }; - void add_filename(const char* directory, uint32_t directorylen, const char* filename, uint32_t filenamelen); + void add_filename(const char* fullpath); - bool is_role_server() - { - return (m_flags & FLAGS_ROLE_SERVER) == FLAGS_ROLE_SERVER; - } - - bool is_role_client() +public: + inline bool is_transaction() const { - return (m_flags & FLAGS_ROLE_CLIENT) == FLAGS_ROLE_CLIENT; + return (m_usrstate != NULL); } - bool is_role_none() + T* get_usrstate() { - return (m_flags & (FLAGS_ROLE_CLIENT | FLAGS_ROLE_SERVER)) == 0; + return m_usrstate; } - bool is_transaction() - { - return (m_flags & FLAGS_TRANSACTION) == FLAGS_TRANSACTION; - } - void set_is_transaction() - { - m_flags |= FLAGS_TRANSACTION; - } + - void set_role_server() +private: + inline void set_role_server() { m_flags |= FLAGS_ROLE_SERVER; } - void set_role_client() + inline void set_role_client() { m_flags |= FLAGS_ROLE_CLIENT; } @@ -206,37 +410,105 @@ VISIBILITY_PRIVATE sinsp_fdinfo_t* pfdinfo, bool incoming); - void reset_flags() + inline void reset_flags() { m_flags = FLAGS_NONE; } - void set_socketpipe() + inline void set_socketpipe() { m_flags |= FLAGS_IS_SOCKET_PIPE; } - bool is_socketpipe() + inline bool is_socketpipe() { return (m_flags & FLAGS_IS_SOCKET_PIPE) == FLAGS_IS_SOCKET_PIPE; } - bool has_no_role() + inline bool has_no_role() { return !is_role_client() && !is_role_server(); } - T m_usrstate; + inline void set_inpipeline_r() + { + m_flags |= FLAGS_IN_BASELINE_R; + } + + inline void set_inpipeline_rw() + { + m_flags |= FLAGS_IN_BASELINE_RW; + } + + inline void set_inpipeline_other() + { + m_flags |= FLAGS_IN_BASELINE_OTHER; + } + + inline void reset_inpipeline() + { + m_flags &= ~FLAGS_IN_BASELINE_R; + m_flags &= ~FLAGS_IN_BASELINE_RW; + m_flags &= ~FLAGS_IN_BASELINE_OTHER; + } + + inline bool is_inpipeline_r() + { + return (m_flags & FLAGS_IN_BASELINE_R) == FLAGS_IN_BASELINE_R; + } + + inline bool is_inpipeline_rw() + { + return (m_flags & FLAGS_IN_BASELINE_RW) == FLAGS_IN_BASELINE_RW; + } + + inline bool is_inpipeline_other() + { + return (m_flags & FLAGS_IN_BASELINE_OTHER) == FLAGS_IN_BASELINE_OTHER; + } + + inline void set_socket_connected() + { + m_flags &= ~(FLAGS_CONNECTION_PENDING | FLAGS_CONNECTION_FAILED); + m_flags |= FLAGS_SOCKET_CONNECTED; + } + + inline void set_socket_pending() + { + m_flags &= ~(FLAGS_SOCKET_CONNECTED | FLAGS_CONNECTION_FAILED); + m_flags |= FLAGS_CONNECTION_PENDING; + } + + inline void set_socket_failed() + { + m_flags &= ~(FLAGS_SOCKET_CONNECTED | FLAGS_CONNECTION_PENDING); + m_flags |= FLAGS_CONNECTION_FAILED; + } + + inline void set_is_cloned() + { + m_flags |= FLAGS_IS_CLONED; + } + + T* m_usrstate; uint32_t m_flags; + uint32_t m_dev; + uint32_t m_mount_id; uint64_t m_ino; + fd_callbacks_info* m_callbacks; + + friend class sinsp; friend class sinsp_parser; friend class sinsp_threadinfo; friend class sinsp_analyzer; - friend class thread_analyzer_info; friend class sinsp_analyzer_fd_listener; friend class sinsp_fdtable; friend class sinsp_filter_check_fd; + friend class sinsp_filter_check_event; + friend class lua_cbacks; + friend class sinsp_baseliner; + friend class protocol_manager; }; /*@}*/ @@ -248,7 +520,46 @@ class sinsp_fdtable { public: sinsp_fdtable(sinsp* inspector); - sinsp_fdinfo_t* find(int64_t fd); + + inline sinsp_fdinfo_t* find(int64_t fd) + { + std::unordered_map::iterator fdit; + + // + // Try looking up in our simple cache + // + if(m_last_accessed_fd != -1 && fd == m_last_accessed_fd) + { + #ifdef GATHER_INTERNAL_STATS + m_inspector->m_stats.m_n_cached_fd_lookups++; + #endif + return m_last_accessed_fdinfo; + } + + // + // Caching failed, do a real lookup + // + fdit = m_table.find(fd); + + if(fdit == m_table.end()) + { + #ifdef GATHER_INTERNAL_STATS + m_inspector->m_stats.m_n_failed_fd_lookups++; + #endif + return NULL; + } + else + { + #ifdef GATHER_INTERNAL_STATS + m_inspector->m_stats.m_n_noncached_fd_lookups++; + #endif + m_last_accessed_fd = fd; + m_last_accessed_fdinfo = &(fdit->second); + lookup_device(&(fdit->second), fd); + return &(fdit->second); + } + } + // If the key is already present, overwrite the existing value and return false. sinsp_fdinfo_t* add(int64_t fd, sinsp_fdinfo_t* fdinfo); // If the key is present, returns true, otherwise returns false. @@ -258,11 +569,15 @@ class sinsp_fdtable void reset_cache(); sinsp* m_inspector; - unordered_map m_table; + std::unordered_map m_table; // // Simple fd cache // int64_t m_last_accessed_fd; sinsp_fdinfo_t *m_last_accessed_fdinfo; + uint64_t m_tid; + +private: + void lookup_device(sinsp_fdinfo_t* fdi, uint64_t fd); }; diff --git a/userspace/libsinsp/filter.cpp b/userspace/libsinsp/filter.cpp index 245aa0ec89..2d0bc4b221 100644 --- a/userspace/libsinsp/filter.cpp +++ b/userspace/libsinsp/filter.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ // @@ -22,16 +23,43 @@ along with sysdig. If not, see . // to configure everything with crappy documentation and code that doesn't compile, // I decided that I agree with this http://mortoray.com/2012/07/20/why-i-dont-use-a-parser-generator/ // and that I'm going with a manually written parser. The grammar is simple enough that it's not -// going to take more time. On the other hand I will avoid a crappy dependency that breaks my +// going to take more time. On the other hand I will avoid a crappy dependency that breaks my // code at every new release, and I will have a cleaner and easier to understand code base. // +#ifdef _WIN32 +#define NOMINMAX +#endif + +#include +#include + #include "sinsp.h" #include "sinsp_int.h" +#include "utils.h" #ifdef HAS_FILTERING #include "filter.h" #include "filterchecks.h" +#include "value_parser.h" +#ifndef _WIN32 +#include "arpa/inet.h" +#endif + +#ifndef _GNU_SOURCE +// +// Fallback implementation of memmem +// +void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen); +#endif + +#ifdef _WIN32 +#pragma comment(lib, "Ws2_32.lib") +#include +#else +#include +#endif + extern sinsp_filter_check_list g_filterlist; @@ -42,12 +70,22 @@ sinsp_filter_check_list::sinsp_filter_check_list() { ////////////////////////////////////////////////////////////////////////////// // ADD NEW FILTER CHECK CLASSES HERE - ////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////////// add_filter_check(new sinsp_filter_check_fd()); add_filter_check(new sinsp_filter_check_thread()); add_filter_check(new sinsp_filter_check_event()); add_filter_check(new sinsp_filter_check_user()); add_filter_check(new sinsp_filter_check_group()); + add_filter_check(new sinsp_filter_check_syslog()); + add_filter_check(new sinsp_filter_check_container()); + add_filter_check(new sinsp_filter_check_utils()); + add_filter_check(new sinsp_filter_check_fdlist()); +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + add_filter_check(new sinsp_filter_check_k8s()); + add_filter_check(new sinsp_filter_check_mesos()); +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + add_filter_check(new sinsp_filter_check_tracer()); + add_filter_check(new sinsp_filter_check_evtin()); } sinsp_filter_check_list::~sinsp_filter_check_list() @@ -75,7 +113,7 @@ void sinsp_filter_check_list::get_all_fields(OUT vectorm_inspector = inspector; - int32_t fldnamelen = m_check_list[j]->parse_field_name(name.c_str()); + int32_t fldnamelen = m_check_list[j]->parse_field_name(name.c_str(), false, true); if(fldnamelen != -1) { @@ -110,14 +148,27 @@ sinsp_filter_check* sinsp_filter_check_list::new_filter_check_from_fldname(strin // it's very likely that you've forgotten to add your filter to the list in // the constructor // - ASSERT(false); return NULL; } +sinsp_filter_check* sinsp_filter_check_list::new_filter_check_from_another(sinsp_filter_check *chk) +{ + sinsp_filter_check *newchk = chk->allocate_new(); + + newchk->m_inspector = chk->m_inspector; + newchk->m_field_id = chk->m_field_id; + newchk->m_field = &chk->m_info.m_fields[chk->m_field_id]; + + newchk->m_boolop = chk->m_boolop; + newchk->m_cmpop = chk->m_cmpop; + + return newchk; +} + /////////////////////////////////////////////////////////////////////////////// // type-based comparison functions /////////////////////////////////////////////////////////////////////////////// -bool flt_compare_uint64(ppm_cmp_operator op, uint64_t operand1, uint64_t operand2) +bool flt_compare_uint64(cmpop op, uint64_t operand1, uint64_t operand2) { switch(op) { @@ -133,13 +184,28 @@ bool flt_compare_uint64(ppm_cmp_operator op, uint64_t operand1, uint64_t operand return (operand1 > operand2); case CO_GE: return (operand1 >= operand2); - default: + case CO_CONTAINS: throw sinsp_exception("'contains' not supported for numeric filters"); return false; + case CO_ICONTAINS: + throw sinsp_exception("'icontains' not supported for numeric filters"); + return false; + case CO_STARTSWITH: + throw sinsp_exception("'startswith' not supported for numeric filters"); + return false; + case CO_ENDSWITH: + throw sinsp_exception("'endswith' not supported for numeric filters"); + return false; + case CO_GLOB: + throw sinsp_exception("'glob' not supported for numeric filters"); + return false; + default: + throw sinsp_exception("'unknown' not supported for numeric filters"); + return false; } } -bool flt_compare_int64(ppm_cmp_operator op, int64_t operand1, int64_t operand2) +bool flt_compare_int64(cmpop op, int64_t operand1, int64_t operand2) { switch(op) { @@ -155,13 +221,28 @@ bool flt_compare_int64(ppm_cmp_operator op, int64_t operand1, int64_t operand2) return (operand1 > operand2); case CO_GE: return (operand1 >= operand2); - default: + case CO_CONTAINS: throw sinsp_exception("'contains' not supported for numeric filters"); return false; + case CO_ICONTAINS: + throw sinsp_exception("'icontains' not supported for numeric filters"); + return false; + case CO_STARTSWITH: + throw sinsp_exception("'startswith' not supported for numeric filters"); + return false; + case CO_ENDSWITH: + throw sinsp_exception("'endswith' not supported for numeric filters"); + return false; + case CO_GLOB: + throw sinsp_exception("'glob' not supported for numeric filters"); + return false; + default: + throw sinsp_exception("'unknown' not supported for numeric filters"); + return false; } } -bool flt_compare_string(ppm_cmp_operator op, char* operand1, char* operand2) +bool flt_compare_string(cmpop op, char* operand1, char* operand2) { switch(op) { @@ -171,14 +252,59 @@ bool flt_compare_string(ppm_cmp_operator op, char* operand1, char* operand2) return (strcmp(operand1, operand2) != 0); case CO_CONTAINS: return (strstr(operand1, operand2) != NULL); + case CO_ICONTAINS: +#ifdef _WIN32 + return (_strnicmp(operand1, operand2, strlen(operand1)) != NULL); +#else + return (strcasestr(operand1, operand2) != NULL); +#endif + case CO_STARTSWITH: + return (strncmp(operand1, operand2, strlen(operand2)) == 0); + case CO_ENDSWITH: + return (sinsp_utils::endswith(operand1, operand2)); + case CO_GLOB: + return sinsp_utils::glob_match(operand2, operand1); + case CO_LT: + return (strcmp(operand1, operand2) < 0); + case CO_LE: + return (strcmp(operand1, operand2) <= 0); + case CO_GT: + return (strcmp(operand1, operand2) > 0); + case CO_GE: + return (strcmp(operand1, operand2) >= 0); + default: + ASSERT(false); + throw sinsp_exception("invalid filter operator " + std::to_string((long long) op)); + return false; + } +} + +bool flt_compare_buffer(cmpop op, char* operand1, char* operand2, uint32_t op1_len, uint32_t op2_len) +{ + switch(op) + { + case CO_EQ: + return op1_len == op2_len && (memcmp(operand1, operand2, op1_len) == 0); + case CO_NE: + return op1_len != op2_len || (memcmp(operand1, operand2, op1_len) != 0); + case CO_CONTAINS: + return (memmem(operand1, op1_len, operand2, op2_len) != NULL); + case CO_ICONTAINS: + throw sinsp_exception("'icontains' not supported for buffer filters"); + case CO_STARTSWITH: + return (memcmp(operand1, operand2, op2_len) == 0); + case CO_ENDSWITH: + return (sinsp_utils::endswith(operand1, operand2, op1_len, op2_len)); + case CO_GLOB: + throw sinsp_exception("'glob' not supported for buffer filters"); case CO_LT: - throw sinsp_exception("'<' not supported for numeric filters"); + throw sinsp_exception("'<' not supported for buffer filters"); case CO_LE: - throw sinsp_exception("'<=' not supported for numeric filters"); + throw sinsp_exception("'<=' not supported for buffer filters"); case CO_GT: - throw sinsp_exception("'>' not supported for numeric filters"); + throw sinsp_exception("'>' not supported for buffer filters"); case CO_GE: - throw sinsp_exception("'>=' not supported for numeric filters"); + throw sinsp_exception("'>=' not supported for buffer filters"); default: ASSERT(false); throw sinsp_exception("invalid filter operator " + std::to_string((long long) op)); @@ -186,8 +312,137 @@ bool flt_compare_string(ppm_cmp_operator op, char* operand1, char* operand2) } } -bool flt_compare(ppm_cmp_operator op, ppm_param_type type, void* operand1, void* operand2) +bool flt_compare_double(cmpop op, double operand1, double operand2) +{ + switch(op) + { + case CO_EQ: + return (operand1 == operand2); + case CO_NE: + return (operand1 != operand2); + case CO_LT: + return (operand1 < operand2); + case CO_LE: + return (operand1 <= operand2); + case CO_GT: + return (operand1 > operand2); + case CO_GE: + return (operand1 >= operand2); + case CO_CONTAINS: + throw sinsp_exception("'contains' not supported for numeric filters"); + return false; + case CO_ICONTAINS: + throw sinsp_exception("'icontains' not supported for numeric filters"); + return false; + case CO_STARTSWITH: + throw sinsp_exception("'startswith' not supported for numeric filters"); + return false; + case CO_ENDSWITH: + throw sinsp_exception("'endswith' not supported for numeric filters"); + return false; + case CO_GLOB: + throw sinsp_exception("'glob' not supported for numeric filters"); + return false; + default: + throw sinsp_exception("'unknown' not supported for numeric filters"); + return false; + } +} + +bool flt_compare_ipv4net(cmpop op, uint64_t operand1, ipv4net* operand2) +{ + switch(op) + { + case CO_EQ: + case CO_IN: + { + return ((operand1 & operand2->m_netmask) == (operand2->m_ip & operand2->m_netmask)); + } + case CO_NE: + return ((operand1 & operand2->m_netmask) != (operand2->m_ip & operand2->m_netmask)); + case CO_CONTAINS: + throw sinsp_exception("'contains' not supported for numeric filters"); + return false; + case CO_ICONTAINS: + throw sinsp_exception("'icontains' not supported for numeric filters"); + return false; + case CO_STARTSWITH: + throw sinsp_exception("'startswith' not supported for numeric filters"); + return false; + case CO_ENDSWITH: + throw sinsp_exception("'endswith' not supported for numeric filters"); + return false; + case CO_GLOB: + throw sinsp_exception("'glob' not supported for numeric filters"); + return false; + default: + throw sinsp_exception("comparison operator not supported for ipv4 networks"); + } +} + +bool flt_compare_ipv6addr(cmpop op, ipv6addr *operand1, ipv6addr *operand2) +{ + switch(op) + { + case CO_EQ: + case CO_IN: + return *operand1 == *operand2; + case CO_NE: + return *operand1 != *operand2; + case CO_CONTAINS: + throw sinsp_exception("'contains' not supported for ipv6 addresses"); + return false; + case CO_ICONTAINS: + throw sinsp_exception("'icontains' not supported for ipv6 addresses"); + return false; + case CO_STARTSWITH: + throw sinsp_exception("'startswith' not supported for ipv6 addresses"); + return false; + case CO_GLOB: + throw sinsp_exception("'glob' not supported for ipv6 addresses"); + return false; + default: + throw sinsp_exception("comparison operator not supported for ipv6 addresses"); + } +} + +bool flt_compare_ipv6net(cmpop op, ipv6addr *operand1, ipv6addr *operand2) +{ + switch(op) + { + case CO_EQ: + case CO_IN: + return operand1->in_subnet(*operand2); + case CO_NE: + return !operand1->in_subnet(*operand2); + case CO_CONTAINS: + throw sinsp_exception("'contains' not supported for ipv6 networks"); + return false; + case CO_ICONTAINS: + throw sinsp_exception("'icontains' not supported for ipv6 networks"); + return false; + case CO_STARTSWITH: + throw sinsp_exception("'startswith' not supported for ipv6 networks"); + return false; + case CO_GLOB: + throw sinsp_exception("'glob' not supported for ipv6 networks"); + return false; + default: + throw sinsp_exception("comparison operator not supported for ipv6 networks"); + } +} + +bool flt_compare(cmpop op, ppm_param_type type, void* operand1, void* operand2, uint32_t op1_len, uint32_t op2_len) { + // + // sinsp_filter_check_*::compare + // already discard NULL values + // + if(op == CO_EXISTS) + { + return true; + } + switch(type) { case PT_INT8: @@ -204,17 +459,50 @@ bool flt_compare(ppm_cmp_operator op, ppm_param_type type, void* operand1, void* case PT_FLAGS8: case PT_UINT8: case PT_SIGTYPE: - return flt_compare_uint64(op, (uint64_t)*(int8_t*)operand1, (uint64_t)*(int8_t*)operand2); + return flt_compare_uint64(op, (uint64_t)*(uint8_t*)operand1, (uint64_t)*(uint8_t*)operand2); case PT_FLAGS16: case PT_UINT16: case PT_PORT: case PT_SYSCALLID: - return flt_compare_uint64(op, (uint64_t)*(int16_t*)operand1, (uint64_t)*(int16_t*)operand2); + return flt_compare_uint64(op, (uint64_t)*(uint16_t*)operand1, (uint64_t)*(uint16_t*)operand2); case PT_UINT32: case PT_FLAGS32: + case PT_MODE: case PT_BOOL: case PT_IPV4ADDR: - return flt_compare_uint64(op, (uint64_t)*(int32_t*)operand1, (uint64_t)*(int32_t*)operand2); + return flt_compare_uint64(op, (uint64_t)*(uint32_t*)operand1, (uint64_t)*(uint32_t*)operand2); + case PT_IPV4NET: + return flt_compare_ipv4net(op, (uint64_t)*(uint32_t*)operand1, (ipv4net*)operand2); + case PT_IPV6ADDR: + return flt_compare_ipv6addr(op, (ipv6addr *)operand1, (ipv6addr *)operand2); + case PT_IPV6NET: + return flt_compare_ipv6net(op, (ipv6addr *)operand1, (ipv6addr*)operand2); + case PT_IPADDR: + if(op1_len == sizeof(struct in_addr)) + { + return flt_compare(op, PT_IPV4ADDR, operand1, operand2, op1_len, op2_len); + } + else if(op1_len == sizeof(struct in6_addr)) + { + return flt_compare(op, PT_IPV6ADDR, operand1, operand2, op1_len, op2_len); + } + else + { + throw sinsp_exception("rawval_to_string called with IP address of incorrect size " + to_string(op1_len)); + } + case PT_IPNET: + if(op1_len == sizeof(struct in_addr)) + { + return flt_compare(op, PT_IPV4NET, operand1, operand2, op1_len, op2_len); + } + else if(op1_len == sizeof(struct in6_addr)) + { + return flt_compare(op, PT_IPV6NET, operand1, operand2, op1_len, op2_len); + } + else + { + throw sinsp_exception("rawval_to_string called with IP network of incorrect size " + to_string(op1_len)); + } case PT_UINT64: case PT_RELTIME: case PT_ABSTIME: @@ -222,11 +510,120 @@ bool flt_compare(ppm_cmp_operator op, ppm_param_type type, void* operand1, void* case PT_CHARBUF: return flt_compare_string(op, (char*)operand1, (char*)operand2); case PT_BYTEBUF: - throw sinsp_exception("bytebuf comparison not implemented yet"); + return flt_compare_buffer(op, (char*)operand1, (char*)operand2, op1_len, op2_len); + case PT_DOUBLE: + return flt_compare_double(op, *(double*)operand1, *(double*)operand2); case PT_SOCKADDR: case PT_SOCKTUPLE: case PT_FDLIST: case PT_FSPATH: + case PT_SIGSET: + case PT_FSRELPATH: + default: + ASSERT(false); + return false; + } +} + +bool flt_compare_avg(cmpop op, + ppm_param_type type, + void* operand1, + void* operand2, + uint32_t op1_len, + uint32_t op2_len, + uint32_t cnt1, + uint32_t cnt2) +{ + int64_t i641, i642; + uint64_t u641, u642; + double d1, d2; + + // + // If count = 0 we assume that the value is zero too (there are assertions to + // check that, and we just divide by 1 + // + if(cnt1 == 0) + { + cnt1 = 1; + } + + if(cnt2 == 0) + { + cnt2 = 1; + } + + switch(type) + { + case PT_INT8: + i641 = ((int64_t)*(int8_t*)operand1) / cnt1; + i642 = ((int64_t)*(int8_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || i641 == 0); + ASSERT(cnt2 != 0 || i642 == 0); + return flt_compare_int64(op, i641, i642); + case PT_INT16: + i641 = ((int64_t)*(int16_t*)operand1) / cnt1; + i642 = ((int64_t)*(int16_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || i641 == 0); + ASSERT(cnt2 != 0 || i642 == 0); + return flt_compare_int64(op, i641, i642); + case PT_INT32: + i641 = ((int64_t)*(int32_t*)operand1) / cnt1; + i642 = ((int64_t)*(int32_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || i641 == 0); + ASSERT(cnt2 != 0 || i642 == 0); + return flt_compare_int64(op, i641, i642); + case PT_INT64: + case PT_FD: + case PT_PID: + case PT_ERRNO: + i641 = ((int64_t)*(int64_t*)operand1) / cnt1; + i642 = ((int64_t)*(int64_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || i641 == 0); + ASSERT(cnt2 != 0 || i642 == 0); + return flt_compare_int64(op, i641, i642); + case PT_FLAGS8: + case PT_UINT8: + case PT_SIGTYPE: + u641 = ((uint64_t)*(uint8_t*)operand1) / cnt1; + u642 = ((uint64_t)*(uint8_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || u641 == 0); + ASSERT(cnt2 != 0 || u642 == 0); + return flt_compare_uint64(op, u641, u642); + case PT_FLAGS16: + case PT_UINT16: + case PT_PORT: + case PT_SYSCALLID: + u641 = ((uint64_t)*(uint16_t*)operand1) / cnt1; + u642 = ((uint64_t)*(uint16_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || u641 == 0); + ASSERT(cnt2 != 0 || u642 == 0); + return flt_compare_uint64(op, u641, u642); + case PT_UINT32: + case PT_FLAGS32: + case PT_MODE: + case PT_BOOL: + case PT_IPV4ADDR: + case PT_IPV6ADDR: + // What does an average mean for ip addresses anyway? + u641 = ((uint64_t)*(uint32_t*)operand1) / cnt1; + u642 = ((uint64_t)*(uint32_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || u641 == 0); + ASSERT(cnt2 != 0 || u642 == 0); + return flt_compare_uint64(op, u641, u642); + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + u641 = (*(uint64_t*)operand1) / cnt1; + u642 = (*(uint64_t*)operand2) / cnt2; + ASSERT(cnt1 != 0 || u641 == 0); + ASSERT(cnt2 != 0 || u642 == 0); + return flt_compare_uint64(op, u641, u642); + case PT_DOUBLE: + d1 = (*(double*)operand1) / cnt1; + d2 = (*(double*)operand2) / cnt2; + ASSERT(cnt1 != 0 || d1 == 0); + ASSERT(cnt2 != 0 || d2 == 0); + return flt_compare_double(op, d1, d2); default: ASSERT(false); return false; @@ -236,15 +633,20 @@ bool flt_compare(ppm_cmp_operator op, ppm_param_type type, void* operand1, void* /////////////////////////////////////////////////////////////////////////////// // sinsp_filter_check implementation /////////////////////////////////////////////////////////////////////////////// -sinsp_filter_check::sinsp_filter_check() : - m_val_storage(256) +sinsp_filter_check::sinsp_filter_check() { m_boolop = BO_NONE; m_cmpop = CO_NONE; m_inspector = NULL; m_field = NULL; m_info.m_fields = NULL; - m_info.m_nfiedls = -1; + m_info.m_nfields = -1; + m_val_storage_len = 0; + m_aggregation = A_NONE; + m_merge_aggregation = A_NONE; + m_val_storages = vector> (1, vector(256)); + m_val_storages_min_size = (numeric_limits::max)(); + m_val_storages_max_size = (numeric_limits::min)(); } void sinsp_filter_check::set_inspector(sinsp* inspector) @@ -252,305 +654,473 @@ void sinsp_filter_check::set_inspector(sinsp* inspector) m_inspector = inspector; } -char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, const filtercheck_field_info* finfo, uint32_t len) +Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval, + ppm_param_type ptype, + ppm_print_format print_format, + uint32_t len) { - char* prfmt; - ASSERT(rawval != NULL); - ASSERT(finfo != NULL); - switch(finfo->m_type) + switch(ptype) { case PT_INT8: - if(finfo->m_print_format == PF_DEC) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRId8; + return *(int8_t *)rawval; } - else if(finfo->m_print_format == PF_HEX) + else if(print_format == PF_OCT || + print_format == PF_HEX) { - prfmt = (char*)"%" PRIX8; + return rawval_to_string(rawval, ptype, print_format, len); } else { ASSERT(false); - return NULL; + return Json::nullValue; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(int8_t *)rawval); - return m_getpropertystr_storage; case PT_INT16: - if(finfo->m_print_format == PF_DEC) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRId16; + return *(int16_t *)rawval; } - else if(finfo->m_print_format == PF_HEX) + else if(print_format == PF_OCT || + print_format == PF_HEX) { - prfmt = (char*)"%" PRIX16; + return rawval_to_string(rawval, ptype, print_format, len); } else { ASSERT(false); - return NULL; + return Json::nullValue; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(int16_t *)rawval); - return m_getpropertystr_storage; case PT_INT32: - if(finfo->m_print_format == PF_DEC) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRId32; + return *(int32_t *)rawval; } - else if(finfo->m_print_format == PF_HEX) + else if(print_format == PF_OCT || + print_format == PF_HEX) { - prfmt = (char*)"%" PRIX32; + return rawval_to_string(rawval, ptype, print_format, len); } else { ASSERT(false); - return NULL; + return Json::nullValue; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(int32_t *)rawval); - return m_getpropertystr_storage; case PT_INT64: case PT_PID: - if(finfo->m_print_format == PF_DEC) - { - prfmt = (char*)"%" PRId64; - } - else if(finfo->m_print_format == PF_10_PADDED_DEC) - { - prfmt = (char*)"%09" PRId64; - } - else if(finfo->m_print_format == PF_HEX) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRIX64; + return (Json::Value::Int64)*(int64_t *)rawval; } else { - prfmt = (char*)"%" PRId64; + return rawval_to_string(rawval, ptype, print_format, len); } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(int64_t *)rawval); - return m_getpropertystr_storage; case PT_L4PROTO: // This can be resolved in the future case PT_UINT8: - if(finfo->m_print_format == PF_DEC) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRIu8; + return *(uint8_t *)rawval; } - else if(finfo->m_print_format == PF_HEX) + else if(print_format == PF_OCT || + print_format == PF_HEX) { - prfmt = (char*)"%" PRIu8; + return rawval_to_string(rawval, ptype, print_format, len); } else { ASSERT(false); - return NULL; + return Json::nullValue; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(uint8_t *)rawval); - return m_getpropertystr_storage; case PT_PORT: // This can be resolved in the future case PT_UINT16: - if(finfo->m_print_format == PF_DEC) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRIu16; + return *(uint16_t *)rawval; } - else if(finfo->m_print_format == PF_HEX) + else if(print_format == PF_OCT || + print_format == PF_HEX) { - prfmt = (char*)"%" PRIu16; + return rawval_to_string(rawval, ptype, print_format, len); } else { ASSERT(false); - return NULL; + return Json::nullValue; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(uint16_t *)rawval); - return m_getpropertystr_storage; case PT_UINT32: - if(finfo->m_print_format == PF_DEC) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRIu32; + return *(uint32_t *)rawval; } - else if(finfo->m_print_format == PF_HEX) + else if(print_format == PF_OCT || + print_format == PF_HEX) { - prfmt = (char*)"%" PRIu32; + return rawval_to_string(rawval, ptype, print_format, len); } else { ASSERT(false); - return NULL; + return Json::nullValue; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(uint32_t *)rawval); - return m_getpropertystr_storage; case PT_UINT64: case PT_RELTIME: case PT_ABSTIME: - if(finfo->m_print_format == PF_DEC) + if(print_format == PF_DEC || + print_format == PF_ID) { - prfmt = (char*)"%" PRIu64; - } - else if(finfo->m_print_format == PF_10_PADDED_DEC) - { - prfmt = (char*)"%09" PRIu64; + return (Json::Value::UInt64)*(uint64_t *)rawval; } - else if(finfo->m_print_format == PF_HEX) + else if( + print_format == PF_10_PADDED_DEC || + print_format == PF_OCT || + print_format == PF_HEX) { - prfmt = (char*)"%" PRIX64; + return rawval_to_string(rawval, ptype, print_format, len); } else { ASSERT(false); - return NULL; + return Json::nullValue; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - prfmt, *(uint64_t *)rawval); - return m_getpropertystr_storage; + case PT_SOCKADDR: + case PT_SOCKFAMILY: + ASSERT(false); + return Json::nullValue; + + case PT_BOOL: + return Json::Value((bool)(*(uint32_t*)rawval != 0)); + case PT_CHARBUF: - return (char*)rawval; + case PT_FSPATH: case PT_BYTEBUF: - if(rawval[len] == 0) - { - return (char*)rawval; + case PT_IPV4ADDR: + case PT_IPV6ADDR: + case PT_IPADDR: + case PT_FSRELPATH: + return rawval_to_string(rawval, ptype, print_format, len); + default: + ASSERT(false); + throw sinsp_exception("wrong event type " + to_string((long long) ptype)); + } +} + +char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, + ppm_param_type ptype, + ppm_print_format print_format, + uint32_t len) +{ + char* prfmt; + + ASSERT(rawval != NULL); + + switch(ptype) + { + case PT_INT8: + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo8; + } + else if(print_format == PF_DEC || + print_format == PF_ID) + { + prfmt = (char*)"%" PRId8; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIX8; } else { - ASSERT(len < 1024 * 1024); - - if(len >= m_val_storage.size()) - { - m_val_storage.resize(len + 1); - } + ASSERT(false); + return NULL; + } - memcpy(&m_val_storage[0], rawval, len); - m_val_storage[len] = 0; - return (char*)&m_val_storage[0]; + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + prfmt, *(int8_t *)rawval); + return m_getpropertystr_storage; + case PT_INT16: + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo16; } - case PT_SOCKADDR: - ASSERT(false); - return NULL; - case PT_SOCKFAMILY: - ASSERT(false); - return NULL; - case PT_BOOL: - if(*(uint32_t*)rawval != 0) + else if(print_format == PF_DEC || + print_format == PF_ID) { - return (char*)"true"; + prfmt = (char*)"%" PRId16; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIX16; } else { - return (char*)"false"; + ASSERT(false); + return NULL; } - case PT_IPV4ADDR: + snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), - "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8, - rawval[0], - rawval[1], - rawval[2], - rawval[3]); + sizeof(m_getpropertystr_storage), + prfmt, *(int16_t *)rawval); return m_getpropertystr_storage; - default: - ASSERT(false); - throw sinsp_exception("wrong event type " + to_string((long long) finfo->m_type)); - } -} - -void sinsp_filter_check::string_to_rawval(const char* str, ppm_param_type ptype) -{ - switch(ptype) - { - case PT_INT8: - *(int8_t*)(&m_val_storage[0]) = sinsp_numparser::parsed8(str); - break; - case PT_INT16: - *(int16_t*)(&m_val_storage[0]) = sinsp_numparser::parsed16(str); - break; case PT_INT32: - *(int32_t*)(&m_val_storage[0]) = sinsp_numparser::parsed32(str); - break; + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo32; + } + else if(print_format == PF_DEC || + print_format == PF_ID) + { + prfmt = (char*)"%" PRId32; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIX32; + } + else + { + ASSERT(false); + return NULL; + } + + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + prfmt, *(int32_t *)rawval); + return m_getpropertystr_storage; case PT_INT64: + case PT_PID: case PT_ERRNO: - *(int64_t*)(&m_val_storage[0]) = sinsp_numparser::parsed64(str); - break; + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo64; + } + else if(print_format == PF_DEC || + print_format == PF_ID) + { + prfmt = (char*)"%" PRId64; + } + else if(print_format == PF_10_PADDED_DEC) + { + prfmt = (char*)"%09" PRId64; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIX64; + } + else + { + prfmt = (char*)"%" PRId64; + } + + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + prfmt, *(int64_t *)rawval); + return m_getpropertystr_storage; case PT_L4PROTO: // This can be resolved in the future - case PT_FLAGS8: case PT_UINT8: - *(uint8_t*)(&m_val_storage[0]) = sinsp_numparser::parseu8(str); - break; + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo8; + } + else if(print_format == PF_DEC || + print_format == PF_ID) + { + prfmt = (char*)"%" PRIu8; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIu8; + } + else + { + ASSERT(false); + return NULL; + } + + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + prfmt, *(uint8_t *)rawval); + return m_getpropertystr_storage; case PT_PORT: // This can be resolved in the future - case PT_FLAGS16: case PT_UINT16: - *(uint16_t*)(&m_val_storage[0]) = sinsp_numparser::parseu16(str); - break; - case PT_FLAGS32: + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo16; + } + else if(print_format == PF_DEC || + print_format == PF_ID) + { + prfmt = (char*)"%" PRIu16; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIu16; + } + else + { + ASSERT(false); + return NULL; + } + + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + prfmt, *(uint16_t *)rawval); + return m_getpropertystr_storage; case PT_UINT32: - *(uint32_t*)(&m_val_storage[0]) = sinsp_numparser::parseu32(str); - break; + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo32; + } + else if(print_format == PF_DEC || + print_format == PF_ID) + { + prfmt = (char*)"%" PRIu32; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIu32; + } + else + { + ASSERT(false); + return NULL; + } + + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + prfmt, *(uint32_t *)rawval); + return m_getpropertystr_storage; case PT_UINT64: - *(uint64_t*)(&m_val_storage[0]) = sinsp_numparser::parseu64(str); - break; case PT_RELTIME: case PT_ABSTIME: - *(uint64_t*)(&m_val_storage[0]) = sinsp_numparser::parseu64(str); - break; + if(print_format == PF_OCT) + { + prfmt = (char*)"%" PRIo64; + } + else if(print_format == PF_DEC || + print_format == PF_ID) + { + prfmt = (char*)"%" PRIu64; + } + else if(print_format == PF_10_PADDED_DEC) + { + prfmt = (char*)"%09" PRIu64; + } + else if(print_format == PF_HEX) + { + prfmt = (char*)"%" PRIX64; + } + else + { + ASSERT(false); + return NULL; + } + + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + prfmt, *(uint64_t *)rawval); + return m_getpropertystr_storage; case PT_CHARBUF: - case PT_SOCKADDR: - case PT_SOCKFAMILY: + case PT_FSPATH: + case PT_FSRELPATH: + return (char*)rawval; + case PT_BYTEBUF: + if(rawval[len] == 0) { - uint32_t len = strlen(str); - if(len >= m_val_storage.size()) + return (char*)rawval; + } + else + { + ASSERT(len < 1024 * 1024); + + if(len >= filter_value().size()) { - throw sinsp_exception("filter parameter too long:" + string(str)); + filter_value().resize(len + 1); } - memcpy((&m_val_storage[0]), str, len); - m_val_storage[len] = 0; + memcpy(filter_value_p(), rawval, len); + filter_value_p()[len] = 0; + return (char*)filter_value_p(); } - break; + case PT_SOCKADDR: + ASSERT(false); + return NULL; + case PT_SOCKFAMILY: + ASSERT(false); + return NULL; case PT_BOOL: - if(string(str) == "true") + if(*(uint32_t*)rawval != 0) { - *(uint32_t*)(&m_val_storage[0]) = 1; + return (char*)"true"; } - else if(string(str) == "false") + else { - *(uint32_t*)(&m_val_storage[0]) = 0; + return (char*)"false"; } - else + case PT_IPV4ADDR: + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8, + rawval[0], + rawval[1], + rawval[2], + rawval[3]); + return m_getpropertystr_storage; + case PT_IPV6ADDR: + { + char address[100]; + + if(NULL == inet_ntop(AF_INET6, rawval, address, 100)) { - throw sinsp_exception("filter error: unrecognized boolean value " + string(str)); + strcpy(address, ""); } - break; - case PT_IPV4ADDR: - if(inet_pton(AF_INET, str, (&m_val_storage[0])) != 1) + strncpy(m_getpropertystr_storage, + address, + 100); + + return m_getpropertystr_storage; + } + case PT_IPADDR: + if(len == sizeof(struct in_addr)) { - throw sinsp_exception("unrecognized IP address " + string(str)); + return rawval_to_string(rawval, PT_IPV4ADDR, print_format, len); } - break; + else if(len == sizeof(struct in6_addr)) + { + return rawval_to_string(rawval, PT_IPV6ADDR, print_format, len); + } + else + { + throw sinsp_exception("rawval_to_string called with IP address of incorrect size " + to_string(len)); + } + + case PT_DOUBLE: + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%.1lf", *(double*)rawval); + return m_getpropertystr_storage; default: ASSERT(false); - throw sinsp_exception("wrong event type " + to_string((long long) m_field->m_type)); + throw sinsp_exception("wrong event type " + to_string((long long) ptype)); } } @@ -564,25 +1134,44 @@ char* sinsp_filter_check::tostring(sinsp_evt* evt) return NULL; } - return rawval_to_string(rawval, m_field, len); + return rawval_to_string(rawval, m_field->m_type, m_field->m_print_format, len); } -int32_t sinsp_filter_check::parse_field_name(const char* str) +Json::Value sinsp_filter_check::tojson(sinsp_evt* evt) +{ + uint32_t len; + Json::Value jsonval = extract_as_js(evt, &len); + + if(jsonval == Json::nullValue) + { + uint8_t* rawval = extract(evt, &len); + if(rawval == NULL) + { + return Json::nullValue; + } + return rawval_to_json(rawval, m_field->m_type, m_field->m_print_format, len); + } + + return jsonval; +} + +int32_t sinsp_filter_check::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) { int32_t j; int32_t max_fldlen = -1; + uint32_t max_flags = 0; ASSERT(m_info.m_fields != NULL); - ASSERT(m_info.m_nfiedls != -1); + ASSERT(m_info.m_nfields != -1); string val(str); m_field_id = 0xffffffff; - for(j = 0; j < m_info.m_nfiedls; j++) + for(j = 0; j < m_info.m_nfields; j++) { string fldname = m_info.m_fields[j].m_name; - int32_t fldlen = fldname.length(); + int32_t fldlen = (uint32_t)fldname.length(); if(val.compare(0, fldlen, fldname) == 0) { @@ -591,165 +1180,220 @@ int32_t sinsp_filter_check::parse_field_name(const char* str) m_field_id = j; m_field = &m_info.m_fields[j]; max_fldlen = fldlen; + max_flags = (m_info.m_fields[j]).m_flags; } } } + if(!needed_for_filtering) + { + if(max_flags & EPF_FILTER_ONLY) + { + throw sinsp_exception(string(str) + " is filter only and cannot be used as a display field"); + } + } + return max_fldlen; } -void sinsp_filter_check::parse_filter_value(const char* str) +void sinsp_filter_check::add_filter_value(const char* str, uint32_t len, uint32_t i) { - string_to_rawval(str, m_field->m_type); -} + size_t parsed_len; -const filtercheck_field_info* sinsp_filter_check::get_field_info() -{ - return &m_info.m_fields[m_field_id]; + if (i >= m_val_storages.size()) + { + m_val_storages.push_back(vector(256)); + } + + parsed_len = parse_filter_value(str, len, filter_value_p(i), filter_value(i).size()); + + // XXX/mstemm this doesn't work if someone called + // add_filter_value more than once for a given index. + filter_value_t item(filter_value_p(i), parsed_len); + m_val_storages_members.insert(item); + + if(parsed_len < m_val_storages_min_size) + { + m_val_storages_min_size = parsed_len; + } + + if(parsed_len > m_val_storages_max_size) + { + m_val_storages_max_size = parsed_len; + } + + // If the operator is CO_PMATCH, also add the value to the paths set. + if (m_cmpop == CO_PMATCH) + { + m_val_storages_paths.add_search_path(item); + } } -bool sinsp_filter_check::compare(sinsp_evt *evt) +size_t sinsp_filter_check::parse_filter_value(const char* str, uint32_t len, uint8_t *storage, uint32_t storage_len) { - uint32_t len; - uint8_t* extracted_val = extract(evt, &len); + size_t parsed_len; - if(extracted_val == NULL) + // byte buffer, no parsing needed + if (m_field->m_type == PT_BYTEBUF) { - return false; + if(len >= storage_len) + { + throw sinsp_exception("filter parameter too long:" + string(str)); + } + memcpy(storage, str, len); + m_val_storage_len = len; + return len; + } + else + { + parsed_len = sinsp_filter_value_parser::string_to_rawval(str, len, storage, storage_len, m_field->m_type); } + validate_filter_value(str, len); - return flt_compare(m_cmpop, - m_info.m_fields[m_field_id].m_type, - extracted_val, - &m_val_storage[0]); + return parsed_len; } -/////////////////////////////////////////////////////////////////////////////// -// sinsp_filter_expression implementation -/////////////////////////////////////////////////////////////////////////////// -sinsp_filter_expression::sinsp_filter_expression() +const filtercheck_field_info* sinsp_filter_check::get_field_info() { - m_parent = NULL; + return &m_info.m_fields[m_field_id]; } -sinsp_filter_expression::~sinsp_filter_expression() +bool sinsp_filter_check::flt_compare(cmpop op, ppm_param_type type, void* operand1, uint32_t op1_len, uint32_t op2_len) { - uint32_t j; + if (op == CO_IN || op == CO_PMATCH || op == CO_INTERSECTS) + { + // Certain filterchecks can't be done as a set + // membership test/group match. For these, just loop over the + // values and see if any value is equal. + switch(type) + { + case PT_IPV4NET: + case PT_IPV6NET: + case PT_IPNET: + case PT_SOCKADDR: + case PT_SOCKTUPLE: + case PT_FDLIST: + case PT_FSPATH: + case PT_SIGSET: + case PT_FSRELPATH: + for (uint16_t i=0; i < m_val_storages.size(); i++) + { + if (::flt_compare(CO_EQ, + type, + operand1, + filter_value_p(i), + op1_len, + filter_value(i).size())) + { + return true; + } + } + return false; + default: + // For raw strings, the length may not be set. So we do a strlen to find it. + if(type == PT_CHARBUF && op1_len == 0) + { + op1_len = strlen((char *) operand1); + } + + filter_value_t item((uint8_t *) operand1, op1_len); + + if (op == CO_IN || op == CO_INTERSECTS) + { + // CO_INTERSECTS is really more interesting when a filtercheck can extract + // multiple values, and you're comparing the set of extracted values + // against the set of rhs values. sinsp_filter_checks only extract a + // single value, so CO_INTERSECTS is really the same as CO_IN. + + if(op1_len >= m_val_storages_min_size && + op1_len <= m_val_storages_max_size && + m_val_storages_members.find(item) != m_val_storages_members.end()) + { + return true; + } + } + else + { + if (m_val_storages_paths.match(item)) + { + return true; + } + } - for(j = 0; j < m_checks.size(); j++) + return false; + break; + } + } + else { - delete m_checks[j]; + return (::flt_compare(op, + type, + operand1, + filter_value_p(), + op1_len, + op2_len) + ); } } -sinsp_filter_check* sinsp_filter_expression::allocate_new() +uint8_t* sinsp_filter_check::extract(gen_event *evt, OUT uint32_t* len, bool sanitize_strings) { - ASSERT(false); - return NULL; + return extract((sinsp_evt *) evt, len, sanitize_strings); } -void sinsp_filter_expression::add_check(sinsp_filter_check* chk) +bool sinsp_filter_check::compare(gen_event *evt) { - m_checks.push_back(chk); + return compare((sinsp_evt *) evt); } -void sinsp_filter_expression::parse(string expr) +bool sinsp_filter_check::compare(sinsp_evt *evt) { -} + uint32_t evt_val_len=0; + bool sanitize_strings = false; + uint8_t* extracted_val = extract(evt, &evt_val_len, sanitize_strings); -bool sinsp_filter_expression::compare(sinsp_evt *evt) -{ - uint32_t j; - uint32_t size = m_checks.size(); - bool res = true; - bool chkres; - - for(j = 0; j < size; j++) + if(extracted_val == NULL) { - sinsp_filter_check* chk = m_checks[j]; - ASSERT(chk != NULL); - - chkres = chk->compare(evt); - if(j == 0) - { - switch(chk->m_boolop) - { - case BO_NONE: - res = chkres; - break; - case BO_NOT: - res = !chkres; - break; - default: - ASSERT(false); - break; - } - } - else - { - switch(chk->m_boolop) - { - case BO_OR: - res = res || chkres; - break; - case BO_AND: - res = res && chkres; - break; - case BO_ORNOT: - res = res || !chkres; - break; - case BO_ANDNOT: - res = res && !chkres; - break; - default: - ASSERT(false); - break; - } - } + return false; } - return res; + return flt_compare(m_cmpop, + m_info.m_fields[m_field_id].m_type, + extracted_val, + evt_val_len, + m_val_storage_len); +} + +sinsp_filter::sinsp_filter(sinsp *inspector) +{ + m_inspector = inspector; +} + +sinsp_filter::~sinsp_filter() +{ } /////////////////////////////////////////////////////////////////////////////// -// sinsp_filter implementation +// sinsp_filter_compiler implementation /////////////////////////////////////////////////////////////////////////////// -sinsp_filter::sinsp_filter(sinsp* inspector, string fltstr) +sinsp_filter_compiler::sinsp_filter_compiler(sinsp* inspector, const string& fltstr, bool ttable_only) { m_inspector = inspector; + m_ttable_only = ttable_only; m_scanpos = -1; m_scansize = 0; m_state = ST_NEED_EXPRESSION; - m_filter = new sinsp_filter_expression(); - m_curexpr = m_filter; + m_filter = new sinsp_filter(m_inspector); m_last_boolop = BO_NONE; m_nest_level = 0; - - try - { - compile(fltstr); - } - catch(sinsp_exception& e) - { - delete m_filter; - throw e; - } - catch(...) - { - delete m_filter; - throw sinsp_exception("error parsing the filter string"); - } + m_fltstr = fltstr; } -sinsp_filter::~sinsp_filter() +sinsp_filter_compiler::~sinsp_filter_compiler() { - if(m_filter) - { - delete m_filter; - } } -bool sinsp_filter::isblank(char c) +bool sinsp_filter_compiler::isblank(char c) { if(c == ' ' || c == '\t' || c == '\n' || c == '\r') { @@ -761,7 +1405,7 @@ bool sinsp_filter::isblank(char c) } } -bool sinsp_filter::is_special_char(char c) +bool sinsp_filter_compiler::is_special_char(char c) { if(c == '(' || c == ')' || c == '!' || c == '=' || c == '<' || c == '>') { @@ -771,7 +1415,7 @@ bool sinsp_filter::is_special_char(char c) return false; } -bool sinsp_filter::is_bracket(char c) +bool sinsp_filter_compiler::is_bracket(char c) { if(c == '(' || c == ')') { @@ -781,7 +1425,7 @@ bool sinsp_filter::is_bracket(char c) return false; } -char sinsp_filter::next() +char sinsp_filter_compiler::next() { while(true) { @@ -799,9 +1443,20 @@ char sinsp_filter::next() } } -string sinsp_filter::next_operand(bool expecting_first_operand) +vector sinsp_filter_compiler::next_operand(bool expecting_first_operand, bool in_or_pmatch_clause) { + vector res; + bool is_quoted = false; int32_t start; + int32_t nums[2]; + uint32_t num_pos; + enum ppm_escape_state + { + PES_NORMAL, + PES_SLASH, + PES_NUMBER, + PES_ERROR, + } escape_state; // // Skip spaces @@ -811,15 +1466,26 @@ string sinsp_filter::next_operand(bool expecting_first_operand) next(); } + // + // If there are quotes, don't stop on blank + // + if(m_scanpos < m_scansize && + (m_fltstr[m_scanpos] == '"' || m_fltstr[m_scanpos] == '\'')) + { + is_quoted = true; + m_scanpos++; + } + // // Mark the beginning of the word // start = m_scanpos; + escape_state = PES_NORMAL; + num_pos = 0; - for(; m_scanpos < m_scansize; m_scanpos++) + while(m_scanpos < m_scansize && escape_state != PES_ERROR) { char curchar = m_fltstr[m_scanpos]; - bool is_end_of_word; if(expecting_first_operand) @@ -828,35 +1494,116 @@ string sinsp_filter::next_operand(bool expecting_first_operand) } else { - is_end_of_word = (isblank(curchar) || is_bracket(curchar)); + is_end_of_word = (!is_quoted && (isblank(curchar) || is_bracket(curchar) || (in_or_pmatch_clause && curchar == ','))) || + (is_quoted && escape_state != PES_SLASH && (curchar == '"' || curchar == '\'')); } if(is_end_of_word) { + if(escape_state != PES_NORMAL) + { + escape_state = PES_ERROR; + break; + } + // // End of word // - ASSERT(m_scanpos > start); - string res = m_fltstr.substr(start, m_scanpos - start); + ASSERT(m_scanpos >= start); - if(curchar == '(' || curchar == ')') + if(curchar == '(' || curchar == ')' || (in_or_pmatch_clause && curchar == ',')) { m_scanpos--; } + res.push_back('\0'); return res; } + + switch(escape_state) + { + case PES_NORMAL: + if(curchar == '\\' && !expecting_first_operand) + { + escape_state = PES_SLASH; + } + else + { + res.push_back(curchar); + } + break; + case PES_SLASH: + switch(curchar) + { + case '\\': + case '"': + escape_state = PES_NORMAL; + res.push_back(curchar); + break; + case 'x': + escape_state = PES_NUMBER; + break; + default: + escape_state = PES_NORMAL; + res.push_back('\\'); + res.push_back(curchar); + break; + } + break; + case PES_NUMBER: + if(isdigit((int)curchar)) + { + nums[num_pos++] = curchar - '0'; + } + else if((curchar >= 'a' && curchar <= 'f') || (curchar >= 'A' && curchar <= 'F')) + { + nums[num_pos++] = tolower((int)curchar) - 'a' + 10; + } + else + { + escape_state = PES_ERROR; + } + + if(num_pos == 2 && escape_state != PES_ERROR) + { + res.push_back((char)(nums[0] * 16 + nums[1])); + + num_pos = 0; + escape_state = PES_NORMAL; + } + break; + default: + ASSERT(false); + escape_state = PES_ERROR; + break; + } + + m_scanpos++; + } + + if(escape_state == PES_ERROR) + { + throw sinsp_exception("filter error: unrecognized escape sequence at " + m_fltstr.substr(start, m_scanpos)); + } + else if(is_quoted) + { + throw sinsp_exception("filter error: unclosed quotes"); } // // End of filter // - return m_fltstr.substr(start, m_scansize - 1); + res.push_back('\0'); + return res; } -bool sinsp_filter::compare_no_consume(string str) +bool sinsp_filter_compiler::compare_no_consume(const string& str) { - if(m_scanpos + (int32_t)str.size() >= m_scansize) + // + // If the rest of the filter cannot contain the operand we may return + // The filter may finish with the operand itself though (e.g. "proc.name exists") + // + if(m_scanpos + (int32_t)str.size() > m_scansize) { return false; } @@ -873,7 +1620,7 @@ bool sinsp_filter::compare_no_consume(string str) } } -ppm_cmp_operator sinsp_filter::next_comparison_operator() +cmpop sinsp_filter_compiler::next_comparison_operator() { int32_t start; @@ -925,59 +1672,262 @@ ppm_cmp_operator sinsp_filter::next_comparison_operator() m_scanpos += 8; return CO_CONTAINS; } + else if(compare_no_consume("icontains")) + { + m_scanpos += 9; + return CO_ICONTAINS; + } + else if(compare_no_consume("startswith")) + { + m_scanpos += 10; + return CO_STARTSWITH; + } + else if(compare_no_consume("endswith")) + { + m_scanpos += 8; + return CO_ENDSWITH; + } + else if(compare_no_consume("glob")) + { + m_scanpos += 4; + return CO_GLOB; + } + else if(compare_no_consume("in")) + { + m_scanpos += 2; + return CO_IN; + } + else if(compare_no_consume("intersects")) + { + m_scanpos += 10; + return CO_INTERSECTS; + } + else if(compare_no_consume("pmatch")) + { + m_scanpos += 6; + return CO_PMATCH; + } + else if(compare_no_consume("exists")) + { + m_scanpos += 6; + return CO_EXISTS; + } else { throw sinsp_exception("filter error: unrecognized comparison operator after " + m_fltstr.substr(0, start)); } } -void sinsp_filter::parse_check(sinsp_filter_expression* parent_expr, boolop op) +void sinsp_filter_compiler::parse_check() { uint32_t startpos = m_scanpos; - string operand1 = next_operand(true); - sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(operand1, m_inspector, true); + vector operand1 = next_operand(true, false); + string str_operand1 = string((char *)&operand1[0]); + sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(str_operand1, m_inspector, true); + boolop op = m_last_boolop; + + if(chk == NULL) + { + throw sinsp_exception("filter error: unrecognized field " + + str_operand1 + " at pos " + to_string((long long) startpos)); + } + + if(m_ttable_only) + { + if(!(chk->get_fields()->m_flags & filter_check_info::FL_WORKS_ON_THREAD_TABLE)) + { + if(str_operand1 != "evt.rawtime" && + str_operand1 != "evt.rawtime.s" && + str_operand1 != "evt.rawtime.ns" && + str_operand1 != "evt.time" && + str_operand1 != "evt.time.s" && + str_operand1 != "evt.datetime" && + str_operand1 != "evt.reltime") + { + throw sinsp_exception("the given filter is not supported for thread table filtering"); + } + } + } + + cmpop co = next_comparison_operator(); + + chk->m_boolop = op; + chk->m_cmpop = co; + + chk->parse_field_name((char *)&operand1[0], true, true); + + if(co == CO_IN || co == CO_INTERSECTS || co == CO_PMATCH) + { + // + // Skip spaces + // + if(isblank(m_fltstr[m_scanpos])) + { + next(); + } + + if(m_fltstr[m_scanpos] != '(') + { + throw sinsp_exception("expected '(' after 'in/intersects/pmatch' operand"); + } + + // + // Skip '(' + // + m_scanpos++; + + if(chk->get_field_info()->m_type == PT_CHARBUF) + { + // + // For character buffers, we can check all + // values at once by putting them in a set and + // checking for set membership. + // + + // + // Create the 'or' sequence + // + uint32_t num_values = 0; + while(true) + { + // 'in' clause aware + vector operand2 = next_operand(false, true); + + chk->add_filter_value((char *)&operand2[0], (uint32_t)operand2.size() - 1, num_values); + num_values++; + next(); + + if(m_fltstr[m_scanpos] == ')') + { + break; + } + else if(m_fltstr[m_scanpos] == ',') + { + m_scanpos++; + } + else + { + throw sinsp_exception("expected either ')' or ',' after a value inside the 'in/pmatch' clause"); + } + } + m_filter->add_check(chk); + } + else if (co == CO_PMATCH) + { + // the pmatch operator can only work on charbufs + throw sinsp_exception("pmatch requires all charbuf arguments"); + } + else + { + // + // In this case we need to create '(field=value1 or field=value2 ...)' + // + + // + // Separate the 'or's from the + // rest of the conditions + // + m_filter->push_expression(op); + m_last_boolop = BO_NONE; + m_nest_level++; + + // + // The first boolean operand will be BO_NONE + // Then we will start putting BO_ORs + // + op = BO_NONE; + + // + // Create the 'or' sequence + // + while(true) + { + // 'in' clause aware + vector operand2 = next_operand(false, true); + + // + // Append every sinsp_filter_check creating the 'or' sequence + // + sinsp_filter_check* newchk = g_filterlist.new_filter_check_from_another(chk); + newchk->m_boolop = op; + newchk->m_cmpop = CO_EQ; + newchk->add_filter_value((char *)&operand2[0], (uint32_t)operand2.size() - 1); - if(chk == NULL) - { - throw sinsp_exception("filter error: unrecognized field " + - operand1 + " at pos " + to_string((long long) startpos)); - } + m_filter->add_check(newchk); - ppm_cmp_operator co = next_comparison_operator(); - string operand2 = next_operand(false); + next(); - chk->m_boolop = op; - chk->m_cmpop = co; - chk->parse_field_name(operand1.c_str()); - chk->parse_filter_value(operand2.c_str()); + if(m_fltstr[m_scanpos] == ')') + { + break; + } + else if(m_fltstr[m_scanpos] == ',') + { + m_scanpos++; + } + else + { + throw sinsp_exception("expected either ')' or ',' after a value inside the 'in' clause"); + } - parent_expr->add_check(chk); -} + // + // From now on we 'or' every newchk + // + op = BO_OR; + } -void sinsp_filter::push_expression(boolop op) -{ - sinsp_filter_expression* newexpr = new sinsp_filter_expression(); - newexpr->m_boolop = op; - newexpr->m_parent = m_curexpr; - m_last_boolop = BO_NONE; + // + // Come back to the rest of the filter + // + m_filter->pop_expression(); + m_nest_level--; + } + } + else + { + // + // In this case we want next() to return the very next character + // At this moment m_scanpos is already at it + // e.g. "(field exists) and ..." + // + if(co == CO_EXISTS) + { + m_scanpos--; + } + // + // Otherwise we need a value for the operand + // + else + { + vector operand2 = next_operand(false, false); + chk->add_filter_value((char *)&operand2[0], (uint32_t)operand2.size() - 1); + } - m_curexpr->m_checks.push_back((sinsp_filter_check*)newexpr); - m_curexpr = newexpr; - m_nest_level++; + m_filter->add_check(chk); + } } -void sinsp_filter::pop_expression() +sinsp_filter* sinsp_filter_compiler::compile() { - ASSERT(m_curexpr->m_parent != NULL); - - m_curexpr = m_curexpr->m_parent; - m_nest_level--; + try + { + return compile_(); + } + catch(const sinsp_exception& e) + { + delete m_filter; + throw; + } + catch(...) + { + delete m_filter; + throw sinsp_exception("error parsing the filter string"); + } } -void sinsp_filter::compile(string fltstr) +sinsp_filter* sinsp_filter_compiler::compile_() { - m_fltstr = fltstr; - m_scansize = m_fltstr.size(); + m_scansize = (uint32_t)m_fltstr.size(); while(true) { @@ -1002,7 +1952,7 @@ void sinsp_filter::compile(string fltstr) // // Good filter // - return; + return m_filter; break; case '(': @@ -1011,47 +1961,67 @@ void sinsp_filter::compile(string fltstr) throw sinsp_exception("unexpected '(' after " + m_fltstr.substr(0, m_scanpos)); } - push_expression(m_last_boolop); + m_filter->push_expression(m_last_boolop); + m_last_boolop = BO_NONE; + m_nest_level++; break; case ')': - pop_expression(); + m_filter->pop_expression(); + m_nest_level--; break; case 'o': - if(next() == 'r') + if(m_scanpos != 0 && m_state != ST_NEED_EXPRESSION) { - m_last_boolop = BO_OR; + if(next() == 'r') + { + m_last_boolop = BO_OR; + } + else + { + throw sinsp_exception("syntax error in filter at position " + to_string((long long)m_scanpos)); + } + + if(m_state != ST_EXPRESSION_DONE) + { + throw sinsp_exception("unexpected 'or' after " + m_fltstr.substr(0, m_scanpos)); + } + + m_state = ST_NEED_EXPRESSION; } else { - throw sinsp_exception("syntax error in filter at position " + to_string((long long) m_scanpos)); - } - - if(m_state != ST_EXPRESSION_DONE) - { - throw sinsp_exception("unexpected 'or' after " + m_fltstr.substr(0, m_scanpos)); + parse_check(); + m_state = ST_EXPRESSION_DONE; } - m_state = ST_NEED_EXPRESSION; - break; case 'a': - if(next() == 'n' && next() == 'd') + if(m_scanpos != 0 && m_state != ST_NEED_EXPRESSION) { - m_last_boolop = BO_AND; + + if(next() == 'n' && next() == 'd') + { + m_last_boolop = BO_AND; + } + else + { + throw sinsp_exception("syntax error in filter at position " + to_string((long long)m_scanpos)); + } + + if(m_state != ST_EXPRESSION_DONE) + { + throw sinsp_exception("unexpected 'and' after " + m_fltstr.substr(0, m_scanpos)); + } + + m_state = ST_NEED_EXPRESSION; } else { - throw sinsp_exception("syntax error in filter at position " + to_string((long long) m_scanpos)); - } - - if(m_state != ST_EXPRESSION_DONE) - { - throw sinsp_exception("unexpected 'and' after " + m_fltstr.substr(0, m_scanpos)); + parse_check(); + m_state = ST_EXPRESSION_DONE; } - m_state = ST_NEED_EXPRESSION; - break; case 'n': if(next() == 'o' && next() == 't') @@ -1072,20 +2042,337 @@ void sinsp_filter::compile(string fltstr) break; default: - parse_check(m_curexpr, m_last_boolop); - - m_state = ST_EXPRESSION_DONE; + if(m_state == ST_NEED_EXPRESSION) + { + parse_check(); + m_state = ST_EXPRESSION_DONE; + } + else + { + throw sinsp_exception("syntax error in filter at position " + to_string((long long) m_scanpos)); + } break; } } vector components = sinsp_split(m_fltstr, ' '); + return m_filter; +} + +sinsp_evttype_filter::sinsp_evttype_filter() +{ +} + +sinsp_evttype_filter::~sinsp_evttype_filter() +{ + for(const auto &val : m_filters) + { + delete val.second->filter; + delete val.second; + } + + for(auto &ruleset : m_rulesets) + { + delete ruleset; + } + m_filters.clear(); +} + +sinsp_evttype_filter::ruleset_filters::ruleset_filters() +{ + memset(m_filter_by_evttype, 0, PPM_EVENT_MAX * sizeof(list *)); + memset(m_filter_by_syscall, 0, PPM_SC_MAX * sizeof(list *)); +} + +sinsp_evttype_filter::ruleset_filters::~ruleset_filters() +{ + for(int i = 0; i < PPM_EVENT_MAX; i++) + { + if(m_filter_by_evttype[i]) + { + delete m_filter_by_evttype[i]; + m_filter_by_evttype[i] = NULL; + } + } + + for(int i = 0; i < PPM_SC_MAX; i++) + { + if(m_filter_by_syscall[i]) + { + delete m_filter_by_syscall[i]; + m_filter_by_syscall[i] = NULL; + } + } +} + +void sinsp_evttype_filter::ruleset_filters::add_filter(filter_wrapper *wrap) +{ + for(uint32_t etype = 0; etype < PPM_EVENT_MAX; etype++) + { + if(wrap->evttypes[etype]) + { + if(!m_filter_by_evttype[etype]) + { + m_filter_by_evttype[etype] = new std::list(); + } + + m_filter_by_evttype[etype]->push_back(wrap); + } + } + + for(uint32_t syscall = 0; syscall < PPM_SC_MAX; syscall++) + { + if(wrap->syscalls[syscall]) + { + if(!m_filter_by_syscall[syscall]) + { + m_filter_by_syscall[syscall] = new std::list(); + } + + m_filter_by_syscall[syscall]->push_back(wrap); + } + } +} + +void sinsp_evttype_filter::ruleset_filters::remove_filter(filter_wrapper *wrap) +{ + for(uint32_t etype = 0; etype < PPM_EVENT_MAX; etype++) + { + if(wrap->evttypes[etype]) + { + if(m_filter_by_evttype[etype]) + { + m_filter_by_evttype[etype]->erase(std::remove(m_filter_by_evttype[etype]->begin(), + m_filter_by_evttype[etype]->end(), + wrap), + m_filter_by_evttype[etype]->end()); + + if(m_filter_by_evttype[etype]->size() == 0) + { + delete m_filter_by_evttype[etype]; + m_filter_by_evttype[etype] = NULL; + } + } + } + } + + for(uint32_t syscall = 0; syscall < PPM_SC_MAX; syscall++) + { + if(wrap->syscalls[syscall]) + { + if(m_filter_by_syscall[syscall]) + { + m_filter_by_syscall[syscall]->erase(std::remove(m_filter_by_syscall[syscall]->begin(), + m_filter_by_syscall[syscall]->end(), + wrap), + m_filter_by_syscall[syscall]->end()); + + if(m_filter_by_syscall[syscall]->size() == 0) + { + delete m_filter_by_syscall[syscall]; + m_filter_by_syscall[syscall] = NULL; + } + } + } + } +} + + +bool sinsp_evttype_filter::ruleset_filters::run(sinsp_evt *evt) +{ + list *filters; + + uint16_t etype = evt->m_pevt->type; + + if(etype == PPME_GENERIC_E || etype == PPME_GENERIC_X) + { + sinsp_evt_param *parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint16_t)); + uint16_t evid = *(uint16_t *)parinfo->m_val; + + filters = m_filter_by_syscall[evid]; + } + else + { + filters = m_filter_by_evttype[etype]; + } + + if (!filters) { + return false; + } + + for (auto &wrap : *filters) + { + if(wrap->filter->run(evt)) + { + return true; + } + } + + return false; +} + +void sinsp_evttype_filter::ruleset_filters::evttypes_for_ruleset(std::vector &evttypes) +{ + evttypes.assign(PPM_EVENT_MAX+1, false); + + for(uint32_t etype = 0; etype < PPM_EVENT_MAX; etype++) + { + list *filters = m_filter_by_evttype[etype]; + if(filters) + { + evttypes[etype] = true; + } + } +} + +void sinsp_evttype_filter::ruleset_filters::syscalls_for_ruleset(std::vector &syscalls) +{ + syscalls.assign(PPM_SC_MAX+1, false); + + for(uint32_t evid = 0; evid < PPM_SC_MAX; evid++) + { + list *filters = m_filter_by_syscall[evid]; + if(filters) + { + syscalls[evid] = true; + } + } +} + + +void sinsp_evttype_filter::add(string &name, + set &evttypes, + set &syscalls, + set &tags, + sinsp_filter *filter) +{ + filter_wrapper *wrap = new filter_wrapper(); + wrap->filter = filter; + + // If no evttypes or syscalls are specified, the filter is + // enabled for all evttypes/syscalls. + bool def = ((evttypes.size() == 0 && syscalls.size() == 0) ? true : false); + + wrap->evttypes.assign(PPM_EVENT_MAX+1, def); + for(auto &evttype : evttypes) + { + wrap->evttypes[evttype] = true; + } + + wrap->syscalls.assign(PPM_SC_MAX+1, def); + for(auto &syscall : syscalls) + { + wrap->syscalls[syscall] = true; + } + + m_filters.insert(pair(name, wrap)); + + for(const auto &tag: tags) + { + auto it = m_filter_by_tag.lower_bound(tag); + + if(it == m_filter_by_tag.end() || + it->first != tag) + { + it = m_filter_by_tag.emplace_hint(it, + std::make_pair(tag, std::list())); + } + + it->second.push_back(wrap); + } +} + +void sinsp_evttype_filter::enable(const string &pattern, bool enabled, uint16_t ruleset) +{ + regex re(pattern); + + while (m_rulesets.size() < (size_t) ruleset + 1) + { + m_rulesets.push_back(new ruleset_filters()); + } + + for(const auto &val : m_filters) + { + if (regex_match(val.first, re)) + { + if(enabled) + { + m_rulesets[ruleset]->add_filter(val.second); + } + else + { + m_rulesets[ruleset]->remove_filter(val.second); + } + } + } +} + +void sinsp_evttype_filter::enable_tags(const set &tags, bool enabled, uint16_t ruleset) +{ + while (m_rulesets.size() < (size_t) ruleset + 1) + { + m_rulesets.push_back(new ruleset_filters()); + } + + for(const auto &tag : tags) + { + for(const auto &wrap : m_filter_by_tag[tag]) + { + if(enabled) + { + m_rulesets[ruleset]->add_filter(wrap); + } + else + { + m_rulesets[ruleset]->remove_filter(wrap); + } + } + } +} + +bool sinsp_evttype_filter::run(sinsp_evt *evt, uint16_t ruleset) +{ + if(m_rulesets.size() < (size_t) ruleset + 1) + { + return false; + } + + return m_rulesets[ruleset]->run(evt); +} + +void sinsp_evttype_filter::evttypes_for_ruleset(std::vector &evttypes, uint16_t ruleset) +{ + return m_rulesets[ruleset]->evttypes_for_ruleset(evttypes); +} + +void sinsp_evttype_filter::syscalls_for_ruleset(std::vector &syscalls, uint16_t ruleset) +{ + return m_rulesets[ruleset]->syscalls_for_ruleset(syscalls); +} + +sinsp_filter_factory::sinsp_filter_factory(sinsp *inspector) + : m_inspector(inspector) +{ } -bool sinsp_filter::run(sinsp_evt *evt) +sinsp_filter_factory::~sinsp_filter_factory() { - return m_filter->compare(evt); } +gen_event_filter *sinsp_filter_factory::new_filter() +{ + return new sinsp_filter(m_inspector); +} + + +gen_event_filter_check *sinsp_filter_factory::new_filtercheck(const char *fldname) +{ + return g_filterlist.new_filter_check_from_fldname(fldname, + m_inspector, + true); +} + + #endif // HAS_FILTERING diff --git a/userspace/libsinsp/filter.h b/userspace/libsinsp/filter.h index 3d7a91a7a1..9f54c005f9 100644 --- a/userspace/libsinsp/filter.h +++ b/userspace/libsinsp/filter.h @@ -1,36 +1,30 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once +#include +#include + #ifdef HAS_FILTERING -class sinsp_filter_expression; - -enum boolop -{ - BO_NONE = 0, - BO_NOT = 1, - BO_OR = 2, - BO_AND = 4, - BO_ORNOT = 3, - BO_ANDNOT = 5, -}; +#include "gen_filter.h" /** @defgroup filter Filtering events * Filtering infrastructure. @@ -38,33 +32,42 @@ enum boolop */ /*! - \brief This is the class that compiles and runs sysdig-type filters. + \brief This is the class that runs sysdig-type filters. +*/ +class SINSP_PUBLIC sinsp_filter : public gen_event_filter +{ +public: + sinsp_filter(sinsp* inspector); + ~sinsp_filter(); + +private: + sinsp* m_inspector; + + friend class sinsp_evt_formatter; +}; + + +/*! + \brief This is the class that compiles sysdig-type filters. */ -class SINSP_PUBLIC sinsp_filter +class SINSP_PUBLIC sinsp_filter_compiler { public: /*! - \brief Constructs the filter. + \brief Constructs the compiler. - \param inspector Pointer to the inspector instance that will generate the + \param inspector Pointer to the inspector instance that will generate the events to be filtered. - \param fmt The printf-like format to use. The accepted format is the same - as the one of the sysdig '-p' command line flag, so refer to the sysdig - manual for details. + \param fltstr the filter string to compile. + \param ttable_only for internal use only. \note Throws a sinsp_exception if the filter syntax is not valid. */ - sinsp_filter(sinsp* inspector, string fltstr); + sinsp_filter_compiler(sinsp* inspector/* xxx needed? */, const string& fltstr, bool ttable_only=false); - ~sinsp_filter(); - - /*! - \brief Applies the filter to the given event. + ~sinsp_filter_compiler(); - \param evt Pointer that needs to be filtered. - \return true if the event is accepted by the filter, false if it's rejected. - */ - bool run(sinsp_evt *evt); + sinsp_filter* compile(); private: enum state @@ -73,36 +76,147 @@ class SINSP_PUBLIC sinsp_filter ST_NEED_EXPRESSION, }; - char next(); - bool compare_no_consume(string str); + sinsp_filter* compile_(); - string next_operand(bool expecting_first_operand); - ppm_cmp_operator next_comparison_operator(); - void parse_check(sinsp_filter_expression* parent_expr, boolop op); - void push_expression(boolop op); - void pop_expression(); + char next(); + bool compare_no_consume(const string& str); - void compile(string fltstr); + vector next_operand(bool expecting_first_operand, bool in_clause); + cmpop next_comparison_operator(); + void parse_check(); static bool isblank(char c); static bool is_special_char(char c); static bool is_bracket(char c); sinsp* m_inspector; + bool m_ttable_only; string m_fltstr; int32_t m_scanpos; int32_t m_scansize; state m_state; - sinsp_filter_expression* m_curexpr; boolop m_last_boolop; int32_t m_nest_level; - sinsp_filter_expression* m_filter; + sinsp_filter* m_filter; friend class sinsp_evt_formatter; }; +/*! + \brief This class represents a filter optimized using event + types. It actually consists of collections of sinsp_filter objects + grouped by event type. +*/ + +class SINSP_PUBLIC sinsp_evttype_filter +{ +public: + sinsp_evttype_filter(); + virtual ~sinsp_evttype_filter(); + + void add(std::string &name, + std::set &evttypes, + std::set &syscalls, + std::set &tags, + sinsp_filter* filter); + + // rulesets are arbitrary numbers and should be managed by the caller. + // Note that rulesets are used to index into a std::vector so + // specifying unnecessarily large rulesets will result in + // unnecessarily large vectors. + + // Find those rules matching the provided pattern and set + // their enabled status to enabled. + void enable(const std::string &pattern, bool enabled, uint16_t ruleset = 0); + + // Find those rules that have a tag in the set of tags and set + // their enabled status to enabled. Note that the enabled + // status is on the rules, and not the tags--if a rule R has + // tags (a, b), and you call enable_tags([a], true) and then + // enable_tags([b], false), R will be disabled despite the + // fact it has tag a and was enabled by the first call to + // enable_tags. + void enable_tags(const std::set &tags, bool enabled, uint16_t ruleset = 0); + + // Match all filters against the provided event. + bool run(sinsp_evt *evt, uint16_t ruleset = 0); + + // Populate the provided vector, indexed by event type, of the + // event types associated with the given ruleset id. For + // example, evttypes[10] = true would mean that this ruleset + // relates to event type 10. + void evttypes_for_ruleset(std::vector &evttypes, uint16_t ruleset); + + // Populate the provided vector, indexed by syscall code, of the + // syscall codes associated with the given ruleset id. For + // example, syscalls[10] = true would mean that this ruleset + // relates to syscall code 10. + void syscalls_for_ruleset(std::vector &syscalls, uint16_t ruleset); + +private: + + struct filter_wrapper { + sinsp_filter *filter; + + // Indexes from event type to enabled/disabled. + std::vector evttypes; + + // Indexes from syscall code to enabled/disabled. + std::vector syscalls; + }; + + // A group of filters all having the same ruleset + class ruleset_filters { + public: + ruleset_filters(); + + virtual ~ruleset_filters(); + + void add_filter(filter_wrapper *wrap); + void remove_filter(filter_wrapper *wrap); + + bool run(sinsp_evt *evt); + + void evttypes_for_ruleset(std::vector &evttypes); + + void syscalls_for_ruleset(std::vector &syscalls); + + private: + // Maps from event type to filter. There can be multiple + // filters per event type. + std::list *m_filter_by_evttype[PPM_EVENT_MAX]; + + // Maps from syscall number to filter. There can be multiple + // filters per syscall number + std::list *m_filter_by_syscall[PPM_SC_MAX]; + }; + + std::vector m_rulesets; + + // Maps from tag to list of filters having that tag. + std::map> m_filter_by_tag; + + // This holds all the filters passed to add(), so they can + // be cleaned up. + map m_filters; +}; + /*@}*/ +class sinsp_filter_factory : public gen_event_filter_factory +{ +public: + sinsp_filter_factory(sinsp *inspector); + virtual ~sinsp_filter_factory(); + + gen_event_filter *new_filter(); + + gen_event_filter_check *new_filtercheck(const char *fldname); + +protected: + sinsp *m_inspector; +}; + #endif // HAS_FILTERING diff --git a/userspace/libsinsp/filter_value.h b/userspace/libsinsp/filter_value.h new file mode 100644 index 0000000000..4679953385 --- /dev/null +++ b/userspace/libsinsp/filter_value.h @@ -0,0 +1,60 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#include + +// Used for CO_IN/CO_PMATCH filterchecks using PT_CHARBUFs to allow +// for quick multi-value comparisons. Should also work for any +// filtercheck with a buffer and length. When compiling with gnu +// compilers, use the built in but not standard _hash_impl::hash +// function, which uses murmurhash2 and is quite fast. Otherwise, uses +// http://www.cse.yorku.ca/~oz/hash.html. + +typedef std::pair filter_value_t; + +struct g_hash_membuf +{ + size_t operator()(filter_value_t val) const + { +#if defined(__GNUC__) && !defined(__clang__) + return std::_Hash_impl::hash(val.first, val.second); +#else + size_t hash = 5381; + for(uint8_t *p = val.first; (uint32_t)(p-val.first) < val.second; p++) + { + int c = *p; + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + } + return hash; +#endif + } +}; + +struct g_equal_to_membuf +{ + bool operator()(filter_value_t a, filter_value_t b) const + { + return (a.second == b.second && + memcmp(a.first, b.first, a.second) == 0); + } +}; + diff --git a/userspace/libsinsp/filterchecks.cpp b/userspace/libsinsp/filterchecks.cpp index 1e2a63bbe9..2cccf937c1 100644 --- a/userspace/libsinsp/filterchecks.cpp +++ b/userspace/libsinsp/filterchecks.cpp @@ -1,52 +1,142 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include +#include #ifndef _WIN32 #include #endif #include "sinsp.h" #include "sinsp_int.h" +#include "dns_manager.h" #ifdef HAS_FILTERING #include "filter.h" #include "filterchecks.h" +#include "protodecoder.h" +#include "tracers.h" +#include "value_parser.h" extern sinsp_evttables g_infotables; +int32_t g_csysdig_screen_w = -1; +bool g_filterchecks_force_raw_times = false; + +#define RETURN_EXTRACT_VAR(x) do { \ + *len = sizeof((x)); \ + return (uint8_t*) &(x); \ +} while(0) + +#define RETURN_EXTRACT_PTR(x) do { \ + *len = sizeof(*(x)); \ + return (uint8_t*) (x); \ +} while(0) + +#define RETURN_EXTRACT_STRING(x) do { \ + *len = (x).size(); \ + return (uint8_t*) (x).c_str(); \ +} while(0) + +#define RETURN_EXTRACT_CSTR(x) do { \ + if((x)) \ + { \ + *len = strlen((char *) ((x))); \ + } \ + return (uint8_t*) ((x)); \ +} while(0) + +/////////////////////////////////////////////////////////////////////////////// +// Helper functions +/////////////////////////////////////////////////////////////////////////////// +int32_t gmt2local(time_t t) +{ + int dt, dir; + struct tm *gmt, *loc; + struct tm sgmt; + + if(t == 0) + { + t = time(NULL); + } + + gmt = &sgmt; + *gmt = *gmtime(&t); + loc = localtime(&t); + + dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 + (loc->tm_min - gmt->tm_min) * 60; + + dir = loc->tm_year - gmt->tm_year; + if(dir == 0) + { + dir = loc->tm_yday - gmt->tm_yday; + } + + dt += dir * 24 * 60 * 60; + + return dt; +} /////////////////////////////////////////////////////////////////////////////// // sinsp_filter_check_fd implementation /////////////////////////////////////////////////////////////////////////////// const filtercheck_field_info sinsp_filter_check_fd_fields[] = { - {PT_INT64, EPF_NONE, PF_DEC, "fd.num", "the unique number identifying the file descriptor."}, - {PT_CHARBUF, EPF_NONE, PF_DEC, "fd.type", "type of FD. Can be 'file', 'ipv4', 'ipv6', 'unix', 'pipe', 'event', 'signalfd', 'eventpoll', 'inotify' or 'signalfd'."}, - {PT_CHARBUF, EPF_NONE, PF_DEC, "fd.typechar", "type of FD as a single character. Can be 'f' for file, 4 for IPv4 socket, 6 for IPv6 socket, 'u' for unix socket, p for pipe, 'e' for eventfd, 's' for signalfd, 'l' for eventpoll, 'i' for inotify, or 's' for signalfd, 'o' for uknown."}, + {PT_INT64, EPF_NONE, PF_ID, "fd.num", "the unique number identifying the file descriptor."}, + {PT_CHARBUF, EPF_NONE, PF_DEC, "fd.type", "type of FD. Can be 'file', 'directory', 'ipv4', 'ipv6', 'unix', 'pipe', 'event', 'signalfd', 'eventpoll', 'inotify' or 'signalfd'."}, + {PT_CHARBUF, EPF_NONE, PF_DEC, "fd.typechar", "type of FD as a single character. Can be 'f' for file, 4 for IPv4 socket, 6 for IPv6 socket, 'u' for unix socket, p for pipe, 'e' for eventfd, 's' for signalfd, 'l' for eventpoll, 'i' for inotify, 'o' for unknown."}, {PT_CHARBUF, EPF_NONE, PF_NA, "fd.name", "FD full name. If the fd is a file, this field contains the full path. If the FD is a socket, this field contain the connection tuple."}, - {PT_IPV4ADDR, EPF_FILTER_ONLY, PF_NA, "fd.ip", "matches the ip address (client or server) of the fd."}, - {PT_IPV4ADDR, EPF_NONE, PF_NA, "fd.cip", "client IP address."}, - {PT_IPV4ADDR, EPF_NONE, PF_NA, "fd.sip", "server IP address."}, - {PT_PORT, EPF_FILTER_ONLY, PF_DEC, "fd.port", "matches the port (client or server) of the fd."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.directory", "If the fd is a file, the directory that contains it."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.filename", "If the fd is a file, the filename without the path."}, + {PT_IPADDR, EPF_FILTER_ONLY, PF_NA, "fd.ip", "matches the ip address (client or server) of the fd."}, + {PT_IPADDR, EPF_NONE, PF_NA, "fd.cip", "client IP address."}, + {PT_IPADDR, EPF_NONE, PF_NA, "fd.sip", "server IP address."}, + {PT_IPADDR, EPF_NONE, PF_NA, "fd.lip", "local IP address."}, + {PT_IPADDR, EPF_NONE, PF_NA, "fd.rip", "remote IP address."}, + {PT_PORT, EPF_FILTER_ONLY, PF_DEC, "fd.port", "matches the port (either client or server) of the fd."}, {PT_PORT, EPF_NONE, PF_DEC, "fd.cport", "for TCP/UDP FDs, the client port."}, {PT_PORT, EPF_NONE, PF_DEC, "fd.sport", "for TCP/UDP FDs, server port."}, + {PT_PORT, EPF_NONE, PF_DEC, "fd.lport", "for TCP/UDP FDs, the local port."}, + {PT_PORT, EPF_NONE, PF_DEC, "fd.rport", "for TCP/UDP FDs, the remote port."}, {PT_CHARBUF, EPF_NONE, PF_NA, "fd.l4proto", "the IP protocol of a socket. Can be 'tcp', 'udp', 'icmp' or 'raw'."}, - {PT_SOCKFAMILY, EPF_NONE, PF_DEC, "fd.sockfamily", "the socket family for socket events. Can be 'ip' or 'unix'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.sockfamily", "the socket family for socket events. Can be 'ip' or 'unix'."}, {PT_BOOL, EPF_NONE, PF_NA, "fd.is_server", "'true' if the process owning this FD is the server endpoint in the connection."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.uid", "a unique identifier for the FD, created by chaining the FD number and the thread ID."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.containername", "chaining of the container ID and the FD name. Useful when trying to identify which container an FD belongs to."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.containerdirectory", "chaining of the container ID and the directory name. Useful when trying to identify which container a directory belongs to."}, + {PT_PORT, EPF_FILTER_ONLY, PF_NA, "fd.proto", "matches the protocol (either client or server) of the fd."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.cproto", "for TCP/UDP FDs, the client protocol."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.sproto", "for TCP/UDP FDs, server protocol."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.lproto", "for TCP/UDP FDs, the local protocol."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.rproto", "for TCP/UDP FDs, the remote protocol."}, + {PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.net", "matches the IP network (client or server) of the fd."}, + {PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.cnet", "matches the client IP network of the fd."}, + {PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.snet", "matches the server IP network of the fd."}, + {PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.lnet", "matches the local IP network of the fd."}, + {PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.rnet", "matches the remote IP network of the fd."}, + {PT_BOOL, EPF_NONE, PF_NA, "fd.connected", "for TCP/UDP FDs, 'true' if the socket is connected."}, + {PT_BOOL, EPF_NONE, PF_NA, "fd.name_changed", "True when an event changes the name of an fd used by this event. This can occur in some cases such as udp connections where the connection tuple changes."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.cip.name", "Domain name associated with the client IP address."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.sip.name", "Domain name associated with the server IP address."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.lip.name", "Domain name associated with the local IP address."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fd.rip.name", "Domain name associated with the remote IP address."}, + {PT_INT32, EPF_NONE, PF_HEX, "fd.dev", "device number (major/minor) containing the referenced file"}, + {PT_INT32, EPF_NONE, PF_DEC, "fd.dev.major", "major device number containing the referenced file"}, + {PT_INT32, EPF_NONE, PF_DEC, "fd.dev.minor", "minor device number containing the referenced file"}, }; sinsp_filter_check_fd::sinsp_filter_check_fd() @@ -56,7 +146,8 @@ sinsp_filter_check_fd::sinsp_filter_check_fd() m_info.m_name = "fd"; m_info.m_fields = sinsp_filter_check_fd_fields; - m_info.m_nfiedls = sizeof(sinsp_filter_check_fd_fields) / sizeof(sinsp_filter_check_fd_fields[0]); + m_info.m_nfields = sizeof(sinsp_filter_check_fd_fields) / sizeof(sinsp_filter_check_fd_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; } sinsp_filter_check* sinsp_filter_check_fd::allocate_new() @@ -64,1222 +155,7526 @@ sinsp_filter_check* sinsp_filter_check_fd::allocate_new() return (sinsp_filter_check*) new sinsp_filter_check_fd(); } -int32_t sinsp_filter_check_fd::parse_field_name(const char* str) -{ - return sinsp_filter_check::parse_field_name(str); -} - -uint8_t* sinsp_filter_check_fd::extract_fdtype(sinsp_fdinfo_t* fdinfo) -{ - switch(fdinfo->m_type) - { - case SCAP_FD_FILE: - case SCAP_FD_DIRECTORY: - return (uint8_t*)"file"; - case SCAP_FD_IPV4_SOCK: - case SCAP_FD_IPV4_SERVSOCK: - return (uint8_t*)"ipv4"; - case SCAP_FD_IPV6_SOCK: - case SCAP_FD_IPV6_SERVSOCK: - return (uint8_t*)"ipv6"; - case SCAP_FD_UNIX_SOCK: - return (uint8_t*)"unix"; - case SCAP_FD_FIFO: - return (uint8_t*)"pipe"; - case SCAP_FD_EVENT: - return (uint8_t*)"event"; - case SCAP_FD_SIGNALFD: - return (uint8_t*)"signalfd"; - case SCAP_FD_EVENTPOLL: - return (uint8_t*)"eventpoll"; - case SCAP_FD_INOTIFY: - return (uint8_t*)"inotify"; - case SCAP_FD_TIMERFD: - return (uint8_t*)"timerfd"; - default: - return NULL; - } -} - -uint8_t* sinsp_filter_check_fd::extract(sinsp_evt *evt, OUT uint32_t* len) +bool sinsp_filter_check_fd::extract_fdname_from_creator(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) { - ASSERT(evt); - - if(!extract_fd(evt)) - { - return NULL; - } - - // - // TYPE_FDNUM doesn't need fdinfo - // - if(m_field_id == TYPE_FDNUM) - { - return (uint8_t*)&m_tinfo->m_lastevent_fd; - } + const char* resolved_argstr; + uint16_t etype = evt->get_type(); - if(m_fdinfo == NULL) + if(PPME_IS_ENTER(etype)) { - return NULL; + return false; } - switch(m_field_id) + switch(etype) { - case TYPE_FDNAME: - m_tstr = m_fdinfo->m_name; - m_tstr.erase(remove_if(m_tstr.begin(), m_tstr.end(), g_invalidchar()), m_tstr.end()); - - return (uint8_t*)m_tstr.c_str(); - case TYPE_FDTYPE: - return extract_fdtype(m_fdinfo); - case TYPE_FDTYPECHAR: - m_tcstr[0] = m_fdinfo->get_typechar(); - m_tcstr[1] = 0; - return m_tcstr; - case TYPE_CLIENTIP: + case PPME_SYSCALL_OPEN_X: + case PPME_SOCKET_ACCEPT_X: + case PPME_SOCKET_ACCEPT_5_X: + case PPME_SOCKET_ACCEPT4_X: + case PPME_SOCKET_ACCEPT4_5_X: + case PPME_SYSCALL_CREAT_X: { - scap_fd_type evt_type = m_fdinfo->m_type; - - if(m_fdinfo->is_role_none()) - { - return NULL; - } + const char* argstr = evt->get_param_as_str(1, &resolved_argstr, + m_inspector->get_buffer_format()); - if(evt_type == SCAP_FD_IPV4_SOCK) + if(resolved_argstr[0] != 0) { - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip); + m_tstr = resolved_argstr; } - } - - break; - case TYPE_SERVERIP: - { - scap_fd_type evt_type = m_fdinfo->m_type; - - if(m_fdinfo->is_role_none()) + else { - return NULL; + m_tstr = argstr; } - if(evt_type == SCAP_FD_IPV4_SOCK) - { - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip); - } - else if(evt_type == SCAP_FD_IPV4_SERVSOCK) - { - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip); - } + return true; } - - break; - case TYPE_CLIENTPORT: + case PPME_SOCKET_CONNECT_X: { - scap_fd_type evt_type = m_fdinfo->m_type; - - if(m_fdinfo->is_role_none()) - { - return NULL; - } + const char* argstr = evt->get_param_as_str(1, &resolved_argstr, + m_inspector->get_buffer_format()); - if(evt_type == SCAP_FD_IPV4_SOCK) + if(resolved_argstr[0] != 0) { - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport); + m_tstr = resolved_argstr; } - else if(evt_type == SCAP_FD_IPV6_SOCK) + else { - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport); + m_tstr = argstr; } + + return true; } - case TYPE_SERVERPORT: + case PPME_SYSCALL_OPENAT_X: + case PPME_SYSCALL_OPENAT_2_X: { - scap_fd_type evt_type = m_fdinfo->m_type; + sinsp_evt enter_evt; + sinsp_evt_param *parinfo; + char *name; + uint32_t namelen; + string sdir; - if(evt_type == SCAP_FD_IPV4_SOCK) + if(etype == PPME_SYSCALL_OPENAT_X) { - if(m_fdinfo->is_role_none()) + // + // XXX This is highly inefficient, as it re-requests the enter event and then + // does unnecessary allocations and copies. We assume that failed openat() happen + // rarely enough that we don't care. + // + if(!m_inspector->get_parser()->retrieve_enter_event(&enter_evt, evt)) { - return NULL; + return false; } - - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport); } - else if(evt_type == SCAP_FD_IPV4_SERVSOCK) - { - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port); - } - else if(evt_type == SCAP_FD_IPV6_SOCK) - { - if(m_fdinfo->is_role_none()) - { - return NULL; - } - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport); - } - else if(evt_type == SCAP_FD_IPV6_SERVSOCK) - { - return (uint8_t*)&(m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_port); - } - else - { - return NULL; - } - } - case TYPE_L4PROTO: - { - scap_l4_proto l4p = m_fdinfo->get_l4proto(); + parinfo = etype == PPME_SYSCALL_OPENAT_X ? enter_evt.get_param(1) : evt->get_param(2); + name = parinfo->m_val; + namelen = parinfo->m_len; - switch(l4p) + parinfo = etype == PPME_SYSCALL_OPENAT_X ? enter_evt.get_param(0) : evt->get_param(1); + ASSERT(parinfo->m_len == sizeof(int64_t)); + int64_t dirfd = *(int64_t *)parinfo->m_val; + + sinsp_parser::parse_openat_dir(evt, name, dirfd, &sdir); + + char fullpath[SCAP_MAX_PATH_SIZE]; + + sinsp_utils::concatenate_paths(fullpath, SCAP_MAX_PATH_SIZE, + sdir.c_str(), + (uint32_t)sdir.length(), + name, + namelen); + + m_tstr = fullpath; + if(sanitize_strings) { - case SCAP_L4_TCP: - m_tstr = "tcp"; - break; - case SCAP_L4_UDP: - m_tstr = "udp"; - break; - case SCAP_L4_ICMP: - m_tstr = "icmp"; - break; - case SCAP_L4_RAW: - m_tstr = "raw"; - break; - default: - m_tstr = ""; - break; + sanitize_string(m_tstr); } - return (uint8_t*)m_tstr.c_str(); - } - case TYPE_IS_SERVER: - { - m_tbool = - m_inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip); - return (uint8_t*)&m_tbool; + return true; } - break; default: - ASSERT(false); + m_tstr = ""; + return true; } - - return NULL; } -bool sinsp_filter_check_fd::compare_ip(sinsp_evt *evt) +uint8_t* sinsp_filter_check_fd::extract_from_null_fd(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) { - if(!extract_fd(evt)) + *len = 0; + // + // Even is there's no fd, we still try to extract a name from exit events that create + // one. With these events, the fact that there's no FD means that the call failed, + // but even if that happened we still want to collect the name. + // + switch(m_field_id) { - return false; + case TYPE_FDNAME: + { + if(extract_fdname_from_creator(evt, len, sanitize_strings) == true) + { + RETURN_EXTRACT_STRING(m_tstr); + } + else + { + return NULL; + } } - - if(m_fdinfo != NULL) + case TYPE_CONTAINERNAME: { - scap_fd_type evt_type = m_fdinfo->m_type; - - if(evt_type == SCAP_FD_IPV4_SOCK) + if(extract_fdname_from_creator(evt, len, sanitize_strings) == true) + { + m_tstr = m_tinfo->m_container_id + ':' + m_tstr; + RETURN_EXTRACT_STRING(m_tstr); + } + else + { + return NULL; + } + } + case TYPE_DIRECTORY: + case TYPE_CONTAINERDIRECTORY: + { + if(extract_fdname_from_creator(evt, len, sanitize_strings) == true) { - if(m_cmpop == CO_EQ) + if(sanitize_strings) { - if(flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, &m_val_storage[0]) || - flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, &m_val_storage[0])) - { - return true; - } + sanitize_string(m_tstr); } - else if(m_cmpop == CO_NE) + + size_t pos = m_tstr.rfind('/'); + if(pos != string::npos && pos != 0) { - if(flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, &m_val_storage[0]) && - flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, &m_val_storage[0])) + if(pos < m_tstr.size() - 1) { - return true; + m_tstr.resize(pos); } } else { - throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); + m_tstr = "/"; } - } - else if(evt_type == SCAP_FD_IPV4_SERVSOCK) - { - if(m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip == *(uint32_t*)&m_val_storage[0]) + + if(m_field_id == TYPE_CONTAINERDIRECTORY) { - return true; + m_tstr = m_tinfo->m_container_id + ':' + m_tstr; } - } - } - - return false; -} -bool sinsp_filter_check_fd::compare_port(sinsp_evt *evt) -{ - if(!extract_fd(evt)) - { - return false; + RETURN_EXTRACT_STRING(m_tstr); + } + else + { + return NULL; + } } - - if(m_fdinfo != NULL) + case TYPE_FILENAME: { - scap_fd_type evt_type = m_fdinfo->m_type; + if(evt->get_type() != PPME_SYSCALL_OPEN_E && evt->get_type() != PPME_SYSCALL_OPENAT_E && + evt->get_type() != PPME_SYSCALL_OPENAT_2_E && evt->get_type() != PPME_SYSCALL_CREAT_E) + { + return NULL; + } - if(evt_type == SCAP_FD_IPV4_SOCK) + if(extract_fdname_from_creator(evt, len, sanitize_strings) == true) { - if(m_cmpop == CO_EQ) + if(sanitize_strings) { - if(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport == *(uint16_t*)&m_val_storage[0] || - m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport == *(uint16_t*)&m_val_storage[0]) - { - return true; - } + sanitize_string(m_tstr); } - else if(m_cmpop == CO_NE) + + size_t pos = m_tstr.rfind('/'); + if(pos != string::npos) { - if(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport != *(uint16_t*)&m_val_storage[0] && - m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport != *(uint16_t*)&m_val_storage[0]) + if(pos < m_tstr.size() - 1) { - return true; + m_tstr = m_tstr.substr(pos + 1, string::npos); } } - else - { - throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); - } - } - else if(evt_type == SCAP_FD_IPV4_SERVSOCK) - { - if(m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port == *(uint16_t*)&m_val_storage[0]) - { - return true; - } + + RETURN_EXTRACT_STRING(m_tstr); } - else if(evt_type == SCAP_FD_IPV6_SOCK) + else { - if(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport == *(uint16_t*)&m_val_storage[0] || - m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport == *(uint16_t*)&m_val_storage[0]) - { - return true; - } + return NULL; } - else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + } + case TYPE_FDTYPECHAR: + *len = 1; + switch(PPME_MAKE_ENTER(evt->get_type())) { - if(m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_port == *(uint16_t*)&m_val_storage[0]) - { - return true; - } + case PPME_SYSCALL_OPEN_E: + case PPME_SYSCALL_OPENAT_E: + case PPME_SYSCALL_OPENAT_2_E: + case PPME_SYSCALL_CREAT_E: + m_tcstr[0] = CHAR_FD_FILE; + m_tcstr[1] = 0; + return m_tcstr; + case PPME_SOCKET_SOCKET_E: + case PPME_SOCKET_ACCEPT_E: + case PPME_SOCKET_ACCEPT_5_E: + case PPME_SOCKET_ACCEPT4_E: + case PPME_SOCKET_ACCEPT4_5_E: + // + // Note, this is not accurate, because it always + // returns IPv4 even if this could be IPv6 or unix. + // For the moment, I assume it's better than nothing, and doing + // real event parsing here would be a pain. + // + m_tcstr[0] = CHAR_FD_IPV4_SOCK; + m_tcstr[1] = 0; + return m_tcstr; + case PPME_SYSCALL_PIPE_E: + m_tcstr[0] = CHAR_FD_FIFO; + m_tcstr[1] = 0; + return m_tcstr; + case PPME_SYSCALL_EVENTFD_E: + m_tcstr[0] = CHAR_FD_EVENT; + m_tcstr[1] = 0; + return m_tcstr; + case PPME_SYSCALL_SIGNALFD_E: + m_tcstr[0] = CHAR_FD_SIGNAL; + m_tcstr[1] = 0; + return m_tcstr; + case PPME_SYSCALL_TIMERFD_CREATE_E: + m_tcstr[0] = CHAR_FD_TIMERFD; + m_tcstr[1] = 0; + return m_tcstr; + case PPME_SYSCALL_INOTIFY_INIT_E: + m_tcstr[0] = CHAR_FD_INOTIFY; + m_tcstr[1] = 0; + return m_tcstr; + default: + m_tcstr[0] = 'o'; + m_tcstr[1] = 0; + return m_tcstr; } + default: + return NULL; } - - return false; } -bool sinsp_filter_check_fd::extract_fd(sinsp_evt *evt) +uint8_t* sinsp_filter_check_fd::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) { - ppm_event_flags eflags = evt->get_flags(); + *len = 0; + ASSERT(evt); + + if(!extract_fd(evt)) + { + return NULL; + } // - // Make sure this is an event that creates or consumes an fd + // TYPE_FDNUM doesn't need fdinfo // - if(eflags & (EF_CREATES_FD | EF_USES_FD | EF_DESTROYS_FD)) + if(m_field_id == TYPE_FDNUM) { - // - // This is an fd-related event, get the thread info and the fd info - // + RETURN_EXTRACT_VAR(m_tinfo->m_lastevent_fd); + } + + switch(m_field_id) + { + case TYPE_FDNAME: + case TYPE_CONTAINERNAME: + if(m_fdinfo == NULL) + { + return extract_from_null_fd(evt, len, sanitize_strings); + } + + if(evt->get_type() == PPME_SOCKET_CONNECT_X) + { + sinsp_evt_param *parinfo; + + parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint64_t)); + int64_t retval = *(int64_t*)parinfo->m_val; + + if(retval < 0) + { + return extract_from_null_fd(evt, len, sanitize_strings); + } + } + + if(m_field_id == TYPE_CONTAINERNAME) + { + ASSERT(m_tinfo != NULL); + m_tstr = m_tinfo->m_container_id + ':' + m_fdinfo->m_name; + } + else + { + m_tstr = m_fdinfo->m_name; + } + + if(sanitize_strings) + { + sanitize_string(m_tstr); + } + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_FDTYPE: + if(m_fdinfo == NULL) + { + return NULL; + } + else + { + uint8_t *typestr = (uint8_t*)m_fdinfo->get_typestring(); + RETURN_EXTRACT_CSTR(typestr); + } + + case TYPE_DIRECTORY: + case TYPE_CONTAINERDIRECTORY: + { + if(m_fdinfo == NULL) + { + return extract_from_null_fd(evt, len, sanitize_strings); + } + + if(!(m_fdinfo->is_file() || m_fdinfo->is_directory())) + { + return NULL; + } + + m_tstr = m_fdinfo->m_name; + if(sanitize_strings) + { + sanitize_string(m_tstr); + } + + if(m_fdinfo->is_file()) + { + size_t pos = m_tstr.rfind('/'); + if(pos != string::npos && pos != 0) + { + if(pos < m_tstr.size() - 1) + { + m_tstr.resize(pos); + } + } + else + { + m_tstr = "/"; + } + } + + if(m_field_id == TYPE_CONTAINERDIRECTORY) + { + m_tstr = m_tinfo->m_container_id + ':' + m_tstr; + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_FILENAME: + { + if(m_fdinfo == NULL) + { + return extract_from_null_fd(evt, len, sanitize_strings); + } + + if(!m_fdinfo->is_file()) + { + return NULL; + } + + m_tstr = m_fdinfo->m_name; + if(sanitize_strings) + { + sanitize_string(m_tstr); + } + + size_t pos = m_tstr.rfind('/'); + if(pos != string::npos) + { + if(pos < m_tstr.size() - 1) + { + m_tstr = m_tstr.substr(pos + 1, string::npos); + } + } + else + { + m_tstr = "/"; + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_FDTYPECHAR: + if(m_fdinfo == NULL) + { + return extract_from_null_fd(evt, len, sanitize_strings); + } + + *len = 1; + m_tcstr[0] = m_fdinfo->get_typechar(); + m_tcstr[1] = 0; + return m_tcstr; + case TYPE_CNET: + case TYPE_CLIENTIP: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip); + } + else if (evt_type == SCAP_FD_IPV6_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip); + } + } + + break; + case TYPE_CLIENTIP_NAME: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + m_tstr.clear(); + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, evt->get_ts()); + } + else if (evt_type == SCAP_FD_IPV6_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET6, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b[0], evt->get_ts()); + } + + if(!m_tstr.empty()) + { + RETURN_EXTRACT_STRING(m_tstr); + } + } + + break; + case TYPE_SNET: + case TYPE_SERVERIP: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip); + } + else if(evt_type == SCAP_FD_IPV4_SERVSOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip); + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip); + } + else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_ip); + } + } + + break; + case TYPE_SERVERIP_NAME: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + m_tstr.clear(); + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, evt->get_ts()); + } + else if(evt_type == SCAP_FD_IPV4_SERVSOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET, &m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip, evt->get_ts()); + } + else if (evt_type == SCAP_FD_IPV6_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET6, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b[0], evt->get_ts()); + } + else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET6, &m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_ip.m_b[0], evt->get_ts()); + } + + if(!m_tstr.empty()) + { + RETURN_EXTRACT_STRING(m_tstr); + } + } + + break; + case TYPE_LNET: + case TYPE_RNET: + case TYPE_LIP: + case TYPE_RIP: + case TYPE_LIP_NAME: + case TYPE_RIP_NAME: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type != SCAP_FD_IPV4_SOCK && + evt_type != SCAP_FD_IPV6_SOCK) + { + return NULL; + } + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + bool is_local; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + is_local = m_inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, m_tinfo); + } + else + { + is_local = m_inspector->get_ifaddr_list()->is_ipv6addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip, m_tinfo); + } + + if(m_field_id != TYPE_LIP_NAME && m_field_id != TYPE_RIP_NAME) + { + if(is_local) + { + if(m_field_id == TYPE_LIP || m_field_id == TYPE_LNET) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip); + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip); + } + } + } + else + { + if(m_field_id == TYPE_LIP || m_field_id == TYPE_LNET) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip); + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip); + } + } + } + } + else + { + m_tstr.clear(); + if(is_local) + { + if(m_field_id == TYPE_LIP_NAME) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, evt->get_ts()); + } + else + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET6, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b[0], evt->get_ts()); + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, evt->get_ts()); + } + else + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET6, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b[0], evt->get_ts()); + } + } + } + else + { + if(m_field_id == TYPE_LIP_NAME) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, evt->get_ts()); + } + else + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET6, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b[0], evt->get_ts()); + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, evt->get_ts()); + } + else + { + m_tstr = sinsp_dns_manager::get().name_of(AF_INET6, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b[0], evt->get_ts()); + } + } + } + + if(!m_tstr.empty()) + { + RETURN_EXTRACT_STRING(m_tstr); + } + } + } + + break; + case TYPE_CLIENTPORT: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport); + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport); + } + } + case TYPE_CLIENTPROTO: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + m_tstr = ""; + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = port_to_string(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport, this->m_fdinfo->get_l4proto(), m_inspector->m_hostname_and_port_resolution_enabled); + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + m_tstr = port_to_string(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport, this->m_fdinfo->get_l4proto(), m_inspector->m_hostname_and_port_resolution_enabled); + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_SERVERPORT: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport); + } + else if(evt_type == SCAP_FD_IPV4_SERVSOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port); + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport); + } + else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_port); + } + else + { + return NULL; + } + } + case TYPE_SERVERPROTO: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + uint16_t nport = 0; + + scap_fd_type evt_type = m_fdinfo->m_type; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + if(m_fdinfo->is_role_none()) + { + return NULL; + } + nport = m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport; + } + else if(evt_type == SCAP_FD_IPV4_SERVSOCK) + { + nport = m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port; + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + if(m_fdinfo->is_role_none()) + { + return NULL; + } + nport = m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport; + } + else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + { + nport = m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_port; + } + else + { + return NULL; + } + + m_tstr = ""; + if(evt_type == SCAP_FD_IPV4_SOCK) + { + m_tstr = port_to_string(nport, this->m_fdinfo->get_l4proto(), m_inspector->m_hostname_and_port_resolution_enabled); + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + m_tstr = port_to_string(nport, this->m_fdinfo->get_l4proto(), m_inspector->m_hostname_and_port_resolution_enabled); + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_LPORT: + case TYPE_RPORT: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type != SCAP_FD_IPV4_SOCK && + evt_type != SCAP_FD_IPV6_SOCK) + { + return NULL; + } + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + bool is_local; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + is_local = m_inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, m_tinfo); + } + else + { + is_local = m_inspector->get_ifaddr_list()->is_ipv6addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip, m_tinfo); + } + + if(is_local) + { + if(m_field_id == TYPE_LPORT || m_field_id == TYPE_LPROTO) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport); + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport); + } + } + } + else + { + if(m_field_id == TYPE_LPORT || m_field_id == TYPE_LPROTO) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport); + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport); + } + else + { + RETURN_EXTRACT_VAR(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport); + } + } + } + } + + + case TYPE_LPROTO: + case TYPE_RPROTO: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type != SCAP_FD_IPV4_SOCK && + evt_type != SCAP_FD_IPV6_SOCK) + { + return NULL; + } + + if(m_fdinfo->is_role_none()) + { + return NULL; + } + + int16_t nport = 0; + + bool is_local; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + is_local = m_inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, m_tinfo); + } + else + { + is_local = m_inspector->get_ifaddr_list()->is_ipv6addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip, m_tinfo); + } + + if(is_local) + { + if(m_field_id == TYPE_LPORT || m_field_id == TYPE_LPROTO) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + nport = m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport; + } + else + { + nport = m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport; + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + nport = m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport; + } + else + { + nport = m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport; + } + } + } + else + { + if(m_field_id == TYPE_LPORT || m_field_id == TYPE_LPROTO) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + nport = m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport; + } + else + { + nport = m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport; + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + nport = m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport; + } + else + { + nport = m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport; + } + + } + } + + m_tstr = port_to_string(nport, this->m_fdinfo->get_l4proto(), m_inspector->m_hostname_and_port_resolution_enabled); + RETURN_EXTRACT_STRING(m_tstr); + } + + case TYPE_L4PROTO: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + scap_l4_proto l4p = m_fdinfo->get_l4proto(); + + switch(l4p) + { + case SCAP_L4_TCP: + m_tstr = "tcp"; + break; + case SCAP_L4_UDP: + m_tstr = "udp"; + break; + case SCAP_L4_ICMP: + m_tstr = "icmp"; + break; + case SCAP_L4_RAW: + m_tstr = "raw"; + break; + default: + m_tstr = ""; + break; + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_IS_SERVER: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + if(m_fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK || m_fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK) + { + m_tbool = true; + } + else if(m_fdinfo->m_type == SCAP_FD_IPV4_SOCK) + { + m_tbool = + m_inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, m_tinfo); + } + else if(m_fdinfo->m_type == SCAP_FD_IPV6_SOCK) + { + m_tbool = + m_inspector->get_ifaddr_list()->is_ipv6addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip, m_tinfo); + } + else + { + m_tbool = false; + } + + RETURN_EXTRACT_VAR(m_tbool); + } + break; + case TYPE_SOCKFAMILY: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + if(m_fdinfo->m_type == SCAP_FD_IPV4_SOCK || m_fdinfo->m_type == SCAP_FD_IPV6_SOCK || + m_fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK || m_fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK) + { + m_tstr = "ip"; + RETURN_EXTRACT_STRING(m_tstr); + } + else if(m_fdinfo->m_type == SCAP_FD_UNIX_SOCK) + { + m_tstr = "unix"; + RETURN_EXTRACT_STRING(m_tstr); + } + else + { + return NULL; + } + } + break; + case TYPE_UID: + { + if(evt->get_type() == PPME_CONTAINER_JSON_E) + { + return NULL; + } + ASSERT(m_tinfo != NULL); + + m_tstr = to_string(m_tinfo->m_tid) + to_string(m_tinfo->m_lastevent_fd); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + case TYPE_IS_CONNECTED: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + m_tbool = m_fdinfo->is_socket_connected(); + + RETURN_EXTRACT_VAR(m_tbool); + } + break; + case TYPE_NAME_CHANGED: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + m_tbool = evt->fdinfo_name_changed(); + + RETURN_EXTRACT_VAR(m_tbool); + } + break; + case TYPE_DEV: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + m_tbool = m_fdinfo->get_device(); + + RETURN_EXTRACT_VAR(m_tbool); + } + break; + case TYPE_DEV_MAJOR: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + m_tbool = m_fdinfo->get_device_major(); + + RETURN_EXTRACT_VAR(m_tbool); + } + break; + case TYPE_DEV_MINOR: + { + if(m_fdinfo == NULL) + { + return NULL; + } + + m_tbool = m_fdinfo->get_device_minor(); + + RETURN_EXTRACT_VAR(m_tbool); + } + break; + default: + ASSERT(false); + } + + return NULL; +} + +bool sinsp_filter_check_fd::compare_ip(sinsp_evt *evt) +{ + if(!extract_fd(evt)) + { + return false; + } + + if(m_fdinfo != NULL) + { + scap_fd_type evt_type = m_fdinfo->m_type; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + if(m_cmpop == CO_EQ || m_cmpop == CO_IN) + { + if(flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip) || + flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip)) + { + return true; + } + } + else if(m_cmpop == CO_NE) + { + if(flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip) && + flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip)) + { + return true; + } + } + else + { + throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); + } + } + else if(evt_type == SCAP_FD_IPV4_SERVSOCK) + { + if(m_cmpop == CO_EQ || m_cmpop == CO_NE || m_cmpop == CO_IN) + { + return flt_compare(m_cmpop, PT_IPV4ADDR, &m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip); + } + else + { + throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); + } + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + if(m_cmpop == CO_EQ || m_cmpop == CO_IN) + { + if(flt_compare(m_cmpop, PT_IPV6ADDR, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip) || + flt_compare(m_cmpop, PT_IPV6ADDR, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip)) + { + return true; + } + } + else if(m_cmpop == CO_NE) + { + if(flt_compare(m_cmpop, PT_IPV6ADDR, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip) && + flt_compare(m_cmpop, PT_IPV6ADDR, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip)) + { + return true; + } + } + else + { + throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); + } + } + else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + { + if(m_cmpop == CO_EQ || m_cmpop == CO_NE || m_cmpop == CO_IN) + { + return flt_compare(m_cmpop, PT_IPV6ADDR, &m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_ip); + } + else + { + throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); + } + } + } + + return false; +} + +bool sinsp_filter_check_fd::compare_net(sinsp_evt *evt) +{ + if(!extract_fd(evt)) + { + return false; + } + + if(m_fdinfo != NULL) + { + scap_fd_type evt_type = m_fdinfo->m_type; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + if(m_cmpop == CO_EQ || m_cmpop == CO_IN) + { + if(flt_compare_ipv4net(m_cmpop, m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, (ipv4net*)filter_value_p()) || + flt_compare_ipv4net(m_cmpop, m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, (ipv4net*)filter_value_p())) + { + return true; + } + } + else if(m_cmpop == CO_NE) + { + if(flt_compare_ipv4net(m_cmpop, m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, (ipv4net*)filter_value_p()) && + flt_compare_ipv4net(m_cmpop, m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, (ipv4net*)filter_value_p())) + { + return true; + } + } + else + { + throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); + } + } + else if(evt_type == SCAP_FD_IPV4_SERVSOCK) + { + + if(flt_compare_ipv4net(m_cmpop, m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip, (ipv4net*)filter_value_p())) + { + return true; + } + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + if(m_cmpop == CO_EQ || m_cmpop == CO_IN) + { + if(flt_compare_ipv6net(m_cmpop, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip, (ipv6addr*)filter_value_p()) || + flt_compare_ipv6net(m_cmpop, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip, (ipv6addr*)filter_value_p())) + { + return true; + } + } + else if(m_cmpop == CO_NE) + { + if(flt_compare_ipv6net(m_cmpop, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip, (ipv6addr*)filter_value_p()) && + flt_compare_ipv6net(m_cmpop, &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip, (ipv6addr*)filter_value_p())) + { + return true; + } + } + else + { + throw sinsp_exception("filter error: IP filter only supports '=' and '!=' operators"); + } + } + else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + { + if(flt_compare_ipv6net(m_cmpop, &m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_ip, (ipv6addr*)filter_value_p())) + { + return true; + } + } + } + + return false; +} + +bool sinsp_filter_check_fd::compare_port(sinsp_evt *evt) +{ + if(!extract_fd(evt)) + { + return false; + } + + if(m_fdinfo != NULL) + { + uint16_t* sport; + uint16_t* dport; + scap_fd_type evt_type = m_fdinfo->m_type; + + if(evt_type == SCAP_FD_IPV4_SOCK) + { + sport = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport; + dport = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport; + } + else if(evt_type == SCAP_FD_IPV4_SERVSOCK) + { + sport = &m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port; + dport = &m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port; + } + else if(evt_type == SCAP_FD_IPV6_SOCK) + { + sport = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport; + dport = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport; + } + else if(evt_type == SCAP_FD_IPV6_SERVSOCK) + { + sport = &m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_port; + dport = &m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_port; + } + else + { + return false; + } + + switch(m_cmpop) + { + case CO_EQ: + if(*sport == *(uint16_t*)filter_value_p() || + *dport == *(uint16_t*)filter_value_p()) + { + return true; + } + break; + case CO_NE: + if(*sport != *(uint16_t*)filter_value_p() && + *dport != *(uint16_t*)filter_value_p()) + { + return true; + } + break; + case CO_LT: + if(*sport < *(uint16_t*)filter_value_p() || + *dport < *(uint16_t*)filter_value_p()) + { + return true; + } + break; + case CO_LE: + if(*sport <= *(uint16_t*)filter_value_p() || + *dport <= *(uint16_t*)filter_value_p()) + { + return true; + } + break; + case CO_GT: + if(*sport > *(uint16_t*)filter_value_p() || + *dport > *(uint16_t*)filter_value_p()) + { + return true; + } + break; + case CO_GE: + if(*sport >= *(uint16_t*)filter_value_p() || + *dport >= *(uint16_t*)filter_value_p()) + { + return true; + } + break; + + case CO_IN: + if(flt_compare(m_cmpop, + PT_PORT, + sport, + sizeof(*sport)) || + flt_compare(m_cmpop, + PT_PORT, + dport, + sizeof(*dport))) + { + return true; + } + break; + default: + throw sinsp_exception("filter error: unsupported port comparison operator"); + } + } + + return false; +} + +bool sinsp_filter_check_fd::compare_domain(sinsp_evt *evt) +{ + if(!extract_fd(evt)) + { + return false; + } + + if(m_fdinfo != NULL) + { + scap_fd_type evt_type = m_fdinfo->m_type; + if(evt_type != SCAP_FD_IPV4_SOCK && + evt_type != SCAP_FD_IPV6_SOCK) + { + return false; + } + + if(m_fdinfo->is_role_none()) + { + return false; + } + + uint32_t *addr; + if(m_field_id == TYPE_CLIENTIP_NAME) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + addr = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip; + } + else + { + addr = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b[0]; + } + } + else if(m_field_id == TYPE_SERVERIP_NAME) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + addr = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip; + } + else + { + addr = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b[0]; + } + } + else + { + bool is_local; + if(evt_type == SCAP_FD_IPV4_SOCK) + { + is_local = m_inspector->get_ifaddr_list()->is_ipv4addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, m_tinfo); + } + else + { + is_local = m_inspector->get_ifaddr_list()->is_ipv6addr_in_local_machine(m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip, m_tinfo); + } + + if(is_local) + { + if(m_field_id == TYPE_LIP_NAME) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + addr = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip; + } + else + { + addr = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b[0]; + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + addr = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip; + } + else + { + addr = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b[0]; + } + } + } + else + { + if(m_field_id == TYPE_LIP_NAME) + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + addr = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip; + } + else + { + addr = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b[0]; + } + } + else + { + if(evt_type == SCAP_FD_IPV4_SOCK) + { + addr = &m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip; + } + else + { + addr = &m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b[0]; + } + } + } + } + + uint64_t ts = evt->get_ts(); + + if(m_cmpop == CO_IN) + { + for (uint16_t i=0; i < m_val_storages.size(); i++) + { + if(sinsp_dns_manager::get().match((const char *)filter_value_p(i), (evt_type == SCAP_FD_IPV6_SOCK)? AF_INET6 : AF_INET, addr, ts)) + { + return true; + } + } + + return false; + } + else if(m_cmpop == CO_EQ) + { + return sinsp_dns_manager::get().match((const char *)filter_value_p(), (evt_type == SCAP_FD_IPV6_SOCK)? AF_INET6 : AF_INET, addr, ts); + } + else if(m_cmpop == CO_NE) + { + return !sinsp_dns_manager::get().match((const char *)filter_value_p(), (evt_type == SCAP_FD_IPV6_SOCK)? AF_INET6 : AF_INET, addr, ts); + } + else + { + throw sinsp_exception("filter error: fd.*ip.name filter only supports '=' and '!=' operators"); + } + } + + return false; +} +bool sinsp_filter_check_fd::extract_fd(sinsp_evt *evt) +{ + ppm_event_flags eflags = evt->get_info_flags(); + + // + // Make sure this is an event that creates or consumes an fd + // + if(eflags & (EF_CREATES_FD | EF_USES_FD | EF_DESTROYS_FD)) + { + // + // This is an fd-related event, get the thread info and the fd info + // m_tinfo = evt->get_thread_info(); if(m_tinfo == NULL) { - return false; + return false; + } + + m_fdinfo = evt->get_fd_info(); + + if(m_fdinfo == NULL && m_tinfo->m_lastevent_fd != -1) + { + m_fdinfo = m_tinfo->get_fd(m_tinfo->m_lastevent_fd); + } + + // We'll check if fd is null below + } + else + { + return false; + } + + return true; +} + +bool sinsp_filter_check_fd::compare(sinsp_evt *evt) +{ + // + // Some fields are filter only and therefore get a special treatment + // + if(m_field_id == TYPE_IP) + { + return compare_ip(evt); + } + else if(m_field_id == TYPE_PORT || m_field_id == TYPE_PROTO) + { + return compare_port(evt); + } + else if(m_field_id == TYPE_NET) + { + return compare_net(evt); + } + + // + // Standard extract-based fields + // + uint32_t len = 0; + bool sanitize_strings = false; + uint8_t* extracted_val = extract(evt, &len, sanitize_strings); + + if(extracted_val == NULL) + { + // optimization for *_NAME fields + // the first time we will call compare_domain, the next ones + // we will the able to extract and use flt_compare + if(m_field_id == TYPE_CLIENTIP_NAME || + m_field_id == TYPE_SERVERIP_NAME || + m_field_id == TYPE_LIP_NAME || + m_field_id == TYPE_RIP_NAME) + { + return compare_domain(evt); + } + + return false; + } + + return flt_compare(m_cmpop, + m_info.m_fields[m_field_id].m_type, + extracted_val, + len); +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_thread implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_thread_fields[] = +{ + {PT_INT64, EPF_NONE, PF_ID, "proc.pid", "the id of the process generating the event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.exe", "the first command line argument (usually the executable name or a custom one)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.name", "the name (excluding the path) of the executable generating the event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.args", "the arguments passed on the command line when starting the process generating the event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.env", "the environment variables of the process generating the event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.cmdline", "full process command line, i.e. proc.name + proc.args."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.exeline", "full process command line, with exe as first argument, i.e. proc.exe + proc.args."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.cwd", "the current working directory of the event."}, + {PT_UINT32, EPF_NONE, PF_DEC, "proc.nthreads", "the number of threads that the process generating the event currently has, including the main process thread."}, + {PT_UINT32, EPF_NONE, PF_DEC, "proc.nchilds", "the number of child threads that the process generating the event currently has. This excludes the main process thread."}, + {PT_INT64, EPF_NONE, PF_ID, "proc.ppid", "the pid of the parent of the process generating the event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.pname", "the name (excluding the path) of the parent of the process generating the event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.pcmdline", "the full command line (proc.name + proc.args) of the parent of the process generating the event."}, + {PT_INT64, EPF_NONE, PF_ID, "proc.apid", "the pid of one of the process ancestors. E.g. proc.apid[1] returns the parent pid, proc.apid[2] returns the grandparent pid, and so on. proc.apid[0] is the pid of the current process. proc.apid without arguments can be used in filters only and matches any of the process ancestors, e.g. proc.apid=1234."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.aname", "the name (excluding the path) of one of the process ancestors. E.g. proc.aname[1] returns the parent name, proc.aname[2] returns the grandparent name, and so on. proc.aname[0] is the name of the current process. proc.aname without arguments can be used in filters only and matches any of the process ancestors, e.g. proc.aname=bash."}, + {PT_INT64, EPF_NONE, PF_ID, "proc.loginshellid", "the pid of the oldest shell among the ancestors of the current process, if there is one. This field can be used to separate different user sessions, and is useful in conjunction with chisels like spy_user."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "proc.duration", "number of nanoseconds since the process started."}, + {PT_UINT64, EPF_NONE, PF_DEC, "proc.fdopencount", "number of open FDs for the process"}, + {PT_INT64, EPF_NONE, PF_DEC, "proc.fdlimit", "maximum number of FDs the process can open."}, + {PT_DOUBLE, EPF_NONE, PF_DEC, "proc.fdusage", "the ratio between open FDs and maximum available FDs for the process."}, + {PT_UINT64, EPF_NONE, PF_DEC, "proc.vmsize", "total virtual memory for the process (as kb)."}, + {PT_UINT64, EPF_NONE, PF_DEC, "proc.vmrss", "resident non-swapped memory for the process (as kb)."}, + {PT_UINT64, EPF_NONE, PF_DEC, "proc.vmswap", "swapped memory for the process (as kb)."}, + {PT_UINT64, EPF_NONE, PF_DEC, "thread.pfmajor", "number of major page faults since thread start."}, + {PT_UINT64, EPF_NONE, PF_DEC, "thread.pfminor", "number of minor page faults since thread start."}, + {PT_INT64, EPF_NONE, PF_ID, "thread.tid", "the id of the thread generating the event."}, + {PT_BOOL, EPF_NONE, PF_NA, "thread.ismain", "'true' if the thread generating the event is the main one in the process."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "thread.exectime", "CPU time spent by the last scheduled thread, in nanoseconds. Exported by switch events only."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "thread.totexectime", "Total CPU time, in nanoseconds since the beginning of the capture, for the current thread. Exported by switch events only."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "thread.cgroups", "all the cgroups the thread belongs to, aggregated into a single string."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "thread.cgroup", "the cgroup the thread belongs to, for a specific subsystem. E.g. thread.cgroup.cpuacct."}, + {PT_INT64, EPF_NONE, PF_ID, "thread.vtid", "the id of the thread generating the event as seen from its current PID namespace."}, + {PT_INT64, EPF_NONE, PF_ID, "proc.vpid", "the id of the process generating the event as seen from its current PID namespace."}, + {PT_DOUBLE, EPF_NONE, PF_NA, "thread.cpu", "the CPU consumed by the thread in the last second."}, + {PT_DOUBLE, EPF_NONE, PF_NA, "thread.cpu.user", "the user CPU consumed by the thread in the last second."}, + {PT_DOUBLE, EPF_NONE, PF_NA, "thread.cpu.system", "the system CPU consumed by the thread in the last second."}, + {PT_UINT64, EPF_NONE, PF_DEC, "thread.vmsize", "For the process main thread, this is the total virtual memory for the process (as kb). For the other threads, this field is zero."}, + {PT_UINT64, EPF_NONE, PF_DEC, "thread.vmrss", "For the process main thread, this is the resident non-swapped memory for the process (as kb). For the other threads, this field is zero."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "thread.vmsize.b", "For the process main thread, this is the total virtual memory for the process (in bytes). For the other threads, this field is zero."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "thread.vmrss.b", "For the process main thread, this is the resident non-swapped memory for the process (in bytes). For the other threads, this field is zero."}, + {PT_INT64, EPF_NONE, PF_ID, "proc.sid", "the session id of the process generating the event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.sname", "the name of the current process's session leader. This is either the process with pid=proc.sid or the eldest ancestor that has the same sid as the current process."}, + {PT_INT32, EPF_NONE, PF_ID, "proc.tty", "The controlling terminal of the process. 0 for processes without a terminal."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "proc.exepath", "The full executable path of the process."}, + {PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "thread.nametid", "this field chains the process name and tid of a thread and can be used as a specific identifier of a thread for a specific execve."}, + {PT_INT64, EPF_NONE, PF_ID, "proc.vpgid", "the process group id of the process generating the event, as seen from its current PID namespace."}, + {PT_BOOL, EPF_NONE, PF_NA, "proc.is_container_healthcheck", "true if this process is running as a part of the container's health check."}, + {PT_BOOL, EPF_NONE, PF_NA, "proc.is_container_liveness_probe", "true if this process is running as a part of the container's liveness probe."}, + {PT_BOOL, EPF_NONE, PF_NA, "proc.is_container_readiness_probe", "true if this process is running as a part of the container's readiness probe."}, +}; + +sinsp_filter_check_thread::sinsp_filter_check_thread() +{ + m_info.m_name = "process"; + m_info.m_fields = sinsp_filter_check_thread_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_thread_fields) / sizeof(sinsp_filter_check_thread_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; + + m_u64val = 0; + m_cursec_ts = 0; +} + +sinsp_filter_check* sinsp_filter_check_thread::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_thread(); +} + +int32_t sinsp_filter_check_thread::extract_arg(string fldname, string val, OUT const struct ppm_param_info** parinfo) +{ + uint32_t parsed_len = 0; + + // + // 'arg' and 'resarg' are handled in a custom way + // + if(m_field_id == TYPE_APID || m_field_id == TYPE_ANAME) + { + if(val[fldname.size()] == '[') + { + parsed_len = (uint32_t)val.find(']'); + string numstr = val.substr(fldname.size() + 1, parsed_len - fldname.size() - 1); + m_argid = sinsp_numparser::parsed32(numstr); + parsed_len++; + } + else + { + throw sinsp_exception("filter syntax error: " + val); + } + } + else if(m_field_id == TYPE_CGROUP) + { + if(val[fldname.size()] == '.') + { + size_t endpos; + for(endpos = fldname.size() + 1; endpos < val.length(); ++endpos) + { + if(!isalpha(val[endpos]) + && val[endpos] != '_') + { + break; + } + } + + parsed_len = (uint32_t)endpos; + m_argname = val.substr(fldname.size() + 1, endpos - fldname.size() - 1); + } + else + { + throw sinsp_exception("filter syntax error: " + val); + } + } + + return parsed_len; +} + +int32_t sinsp_filter_check_thread::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + string val(str); + + if(string(val, 0, sizeof("arg") - 1) == "arg") + { + // + // 'arg' is handled in a custom way + // + throw sinsp_exception("filter error: proc.arg filter not implemented yet"); + } + else if(string(val, 0, sizeof("proc.apid") - 1) == "proc.apid") + { + m_field_id = TYPE_APID; + m_field = &m_info.m_fields[m_field_id]; + + int32_t res = 0; + + try + { + res = extract_arg("proc.apid", val, NULL); + } + catch(...) + { + if(val == "proc.apid") + { + m_argid = -1; + res = (int32_t)val.size(); + } + } + + return res; + } + else if(string(val, 0, sizeof("proc.aname") - 1) == "proc.aname") + { + m_field_id = TYPE_ANAME; + m_field = &m_info.m_fields[m_field_id]; + + int32_t res = 0; + + try + { + res = extract_arg("proc.aname", val, NULL); + } + catch(...) + { + if(val == "proc.aname") + { + m_argid = -1; + res = (int32_t)val.size(); + } + } + + return res; + } + else if(string(val, 0, sizeof("thread.totexectime") - 1) == "thread.totexectime") + { + // + // Allocate thread storage for the value + // + if(alloc_state) + { + m_th_state_id = m_inspector->reserve_thread_memory(sizeof(uint64_t)); + } + + return sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } + else if(string(val, 0, sizeof("thread.cgroup") - 1) == "thread.cgroup" && + string(val, 0, sizeof("thread.cgroups") - 1) != "thread.cgroups") + { + m_field_id = TYPE_CGROUP; + m_field = &m_info.m_fields[m_field_id]; + + return extract_arg("thread.cgroup", val, NULL); + } + else if(string(val, 0, sizeof("thread.cpu") - 1) == "thread.cpu") + { + if(alloc_state) + { + m_th_state_id = m_inspector->reserve_thread_memory(sizeof(uint64_t)); + } + + return sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } + else + { + return sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } +} + +uint64_t sinsp_filter_check_thread::extract_exectime(sinsp_evt *evt) +{ + uint64_t res = 0; + + if(m_last_proc_switch_times.size() == 0) + { + // + // Initialize the vector of CPU times + // + const scap_machine_info* minfo = m_inspector->get_machine_info(); + ASSERT(minfo->num_cpus != 0); + + for(uint32_t j = 0; j < minfo->num_cpus; j++) + { + m_last_proc_switch_times.push_back(0); + } + } + + uint32_t cpuid = evt->get_cpuid(); + uint64_t ts = evt->get_ts(); + uint64_t lasttime = m_last_proc_switch_times[cpuid]; + + if(lasttime != 0) + { + res = ts - lasttime; + } + + ASSERT(cpuid < m_last_proc_switch_times.size()); + + m_last_proc_switch_times[cpuid] = ts; + + return res; +} + +uint8_t* sinsp_filter_check_thread::extract_thread_cpu(sinsp_evt *evt, OUT uint32_t* len, sinsp_threadinfo* tinfo, bool extract_user, bool extract_system) +{ + uint16_t etype = evt->get_type(); + + if(etype == PPME_PROCINFO_E) + { + uint64_t user = 0; + uint64_t system = 0; + uint64_t tcpu; + + if(extract_user) + { + sinsp_evt_param* parinfo = evt->get_param(0); + user = *(uint64_t*)parinfo->m_val; + } + + if(extract_system) + { + sinsp_evt_param* parinfo = evt->get_param(1); + system = *(uint64_t*)parinfo->m_val; + } + + tcpu = user + system; + + uint64_t* last_t_tot_cpu = (uint64_t*)tinfo->get_private_state(m_th_state_id); + if(*last_t_tot_cpu != 0) + { + uint64_t deltaval = tcpu - *last_t_tot_cpu; + m_dval = (double)deltaval;// / (ONE_SECOND_IN_NS / 100); + if(m_dval > 100) + { + m_dval = 100; + } + } + else + { + m_dval = 0; + } + + *last_t_tot_cpu = tcpu; + + RETURN_EXTRACT_VAR(m_dval); + } + + return NULL; +} + +uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + sinsp_threadinfo* tinfo = evt->get_thread_info(); + + if(tinfo == NULL && + m_field_id != TYPE_TID && + m_field_id != TYPE_EXECTIME && + m_field_id != TYPE_TOTEXECTIME) + { + return NULL; + } + + switch(m_field_id) + { + case TYPE_TID: + m_u64val = evt->get_tid(); + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_PID: + RETURN_EXTRACT_VAR(tinfo->m_pid); + case TYPE_SID: + RETURN_EXTRACT_VAR(tinfo->m_sid); + case TYPE_VPGID: + RETURN_EXTRACT_VAR(tinfo->m_vpgid); + case TYPE_SNAME: + { + // + // Relying on the convention that a session id is the process id of the session leader + // + sinsp_threadinfo* sinfo = + m_inspector->get_thread(tinfo->m_sid, false, true); + + if(sinfo != NULL) + { + m_tstr = sinfo->get_comm(); + RETURN_EXTRACT_STRING(m_tstr); + } + else + { + // This can occur when the session leader process has exited. + // Find the highest ancestor process that has the same session id and + // declare it to be the session leader. + sinsp_threadinfo* mt = tinfo->get_main_thread(); + + if(mt == NULL) + { + return NULL; + } + + int64_t sid = mt->m_sid; + sinsp_threadinfo::visitor_func_t visitor = [sid, &mt] (sinsp_threadinfo *pt) + { + if(pt->m_sid != sid) + { + return false; + } + mt = pt; + return true; + }; + + mt->traverse_parent_state(visitor); + + // mt has been updated to the highest process that has the same session id. + // mt's comm is considered the session leader. + m_tstr = mt->get_comm(); + RETURN_EXTRACT_STRING(m_tstr); + } + } + case TYPE_TTY: + RETURN_EXTRACT_VAR(tinfo->m_tty); + case TYPE_NAME: + m_tstr = tinfo->get_comm(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_EXE: + m_tstr = tinfo->get_exe(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_EXEPATH: + m_tstr = tinfo->get_exepath(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_ARGS: + { + m_tstr.clear(); + + uint32_t j; + uint32_t nargs = (uint32_t)tinfo->m_args.size(); + + for(j = 0; j < nargs; j++) + { + m_tstr += tinfo->m_args[j]; + if(j < nargs -1) + { + m_tstr += ' '; + } + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_ENV: + { + m_tstr.clear(); + + uint32_t j; + const auto& env = tinfo->get_env(); + uint32_t nargs = (uint32_t)env.size(); + + for(j = 0; j < nargs; j++) + { + m_tstr += env[j]; + if(j < nargs -1) + { + m_tstr += ' '; + } + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_CMDLINE: + { + sinsp_threadinfo::populate_cmdline(m_tstr, tinfo); + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_EXELINE: + { + m_tstr = tinfo->get_exe() + " "; + + uint32_t j; + uint32_t nargs = (uint32_t)tinfo->m_args.size(); + + for(j = 0; j < nargs; j++) + { + m_tstr += tinfo->m_args[j]; + if(j < nargs -1) + { + m_tstr += ' '; + } + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_CWD: + m_tstr = tinfo->get_cwd(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_NTHREADS: + { + sinsp_threadinfo* ptinfo = tinfo->get_main_thread(); + if(ptinfo) + { + m_u64val = ptinfo->m_nchilds + 1; + RETURN_EXTRACT_VAR(m_u64val); + } + else + { + ASSERT(false); + return NULL; + } + } + case TYPE_NCHILDS: + RETURN_EXTRACT_VAR(tinfo->m_nchilds); + case TYPE_ISMAINTHREAD: + m_tbool = (uint32_t)tinfo->is_main_thread(); + RETURN_EXTRACT_VAR(m_tbool); + case TYPE_EXECTIME: + { + m_u64val = 0; + uint16_t etype = evt->get_type(); + + if(etype == PPME_SCHEDSWITCH_1_E || etype == PPME_SCHEDSWITCH_6_E) + { + m_u64val = extract_exectime(evt); + } + + RETURN_EXTRACT_VAR(m_u64val); + } + case TYPE_TOTEXECTIME: + { + m_u64val = 0; + uint16_t etype = evt->get_type(); + + if(etype == PPME_SCHEDSWITCH_1_E || etype == PPME_SCHEDSWITCH_6_E) + { + m_u64val = extract_exectime(evt); + } + + sinsp_threadinfo* tinfo = evt->get_thread_info(false); + + if(tinfo != NULL) + { + uint64_t* ptot = (uint64_t*)tinfo->get_private_state(m_th_state_id); + *ptot += m_u64val; + RETURN_EXTRACT_PTR(ptot); + } + else + { + return NULL; + } + } + case TYPE_PPID: + if(tinfo->is_main_thread()) + { + RETURN_EXTRACT_VAR(tinfo->m_ptid); + } + else + { + sinsp_threadinfo* mt = tinfo->get_main_thread(); + + if(mt != NULL) + { + RETURN_EXTRACT_VAR(mt->m_ptid); + } + else + { + return NULL; + } + } + case TYPE_PNAME: + { + sinsp_threadinfo* ptinfo = + m_inspector->get_thread(tinfo->m_ptid, false, true); + + if(ptinfo != NULL) + { + m_tstr = ptinfo->get_comm(); + RETURN_EXTRACT_STRING(m_tstr); + } + else + { + return NULL; + } + } + case TYPE_PCMDLINE: + { + sinsp_threadinfo* ptinfo = + m_inspector->get_thread(tinfo->m_ptid, false, true); + + if(ptinfo != NULL) + { + sinsp_threadinfo::populate_cmdline(m_tstr, ptinfo); + RETURN_EXTRACT_STRING(m_tstr); + } + else + { + return NULL; + } + } + case TYPE_APID: + { + sinsp_threadinfo* mt = NULL; + + if(tinfo->is_main_thread()) + { + mt = tinfo; + } + else + { + mt = tinfo->get_main_thread(); + + if(mt == NULL) + { + return NULL; + } + } + + // + // Search for a specific ancestors + // + for(int32_t j = 0; j < m_argid; j++) + { + mt = mt->get_parent_thread(); + + if(mt == NULL) + { + return NULL; + } + } + + RETURN_EXTRACT_VAR(mt->m_pid); + } + case TYPE_ANAME: + { + sinsp_threadinfo* mt = NULL; + + if(tinfo->is_main_thread()) + { + mt = tinfo; + } + else + { + mt = tinfo->get_main_thread(); + + if(mt == NULL) + { + return NULL; + } + } + + for(int32_t j = 0; j < m_argid; j++) + { + mt = mt->get_parent_thread(); + + if(mt == NULL) + { + return NULL; + } + } + + m_tstr = mt->get_comm(); + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_LOGINSHELLID: + { + sinsp_threadinfo* mt = NULL; + int64_t* res = NULL; + + if(tinfo->is_main_thread()) + { + mt = tinfo; + } + else + { + mt = tinfo->get_main_thread(); + + if(mt == NULL) + { + return NULL; + } + } + + sinsp_threadinfo::visitor_func_t check_thread_for_shell = [&res] (sinsp_threadinfo *pt) + { + size_t len = pt->m_comm.size(); + + if(len >= 2 && pt->m_comm[len - 2] == 's' && pt->m_comm[len - 1] == 'h') + { + res = &pt->m_pid; + } + + return true; + }; + + // First call the visitor on the main thread. + check_thread_for_shell(mt); + + // Then check all its parents to see if they are shells + mt->traverse_parent_state(check_thread_for_shell); + + RETURN_EXTRACT_PTR(res); + } + case TYPE_DURATION: + if(tinfo->m_clone_ts != 0) + { + m_s64val = evt->get_ts() - tinfo->m_clone_ts; + ASSERT(m_s64val > 0); + RETURN_EXTRACT_VAR(m_s64val); + } + else + { + return NULL; + } + case TYPE_FDOPENCOUNT: + m_u64val = tinfo->get_fd_opencount(); + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_FDLIMIT: + m_s64val = tinfo->get_fd_limit(); + RETURN_EXTRACT_VAR(m_s64val); + case TYPE_FDUSAGE: + m_dval = tinfo->get_fd_usage_pct_d(); + RETURN_EXTRACT_VAR(m_dval); + case TYPE_VMSIZE: + m_u64val = tinfo->m_vmsize_kb; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_VMRSS: + m_u64val = tinfo->m_vmrss_kb; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_VMSWAP: + m_u64val = tinfo->m_vmswap_kb; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_THREAD_VMSIZE: + if(tinfo->is_main_thread()) + { + m_u64val = tinfo->m_vmsize_kb; + } + else + { + m_u64val = 0; + } + + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_THREAD_VMRSS: + if(tinfo->is_main_thread()) + { + m_u64val = tinfo->m_vmrss_kb; + } + else + { + m_u64val = 0; + } + + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_THREAD_VMSIZE_B: + if(tinfo->is_main_thread()) + { + m_u64val = tinfo->m_vmsize_kb * 1024; + } + else + { + m_u64val = 0; + } + + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_THREAD_VMRSS_B: + if(tinfo->is_main_thread()) + { + m_u64val = tinfo->m_vmrss_kb * 1024; + } + else + { + m_u64val = 0; + } + + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_PFMAJOR: + m_u64val = tinfo->m_pfmajor; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_PFMINOR: + m_u64val = tinfo->m_pfminor; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_CGROUPS: + { + m_tstr.clear(); + + uint32_t j; + uint32_t nargs = (uint32_t)tinfo->m_cgroups.size(); + + if(nargs == 0) + { + return NULL; + } + + for(j = 0; j < nargs; j++) + { + m_tstr += tinfo->m_cgroups[j].first; + m_tstr += "="; + m_tstr += tinfo->m_cgroups[j].second; + if(j < nargs - 1) + { + m_tstr += ' '; + } + } + + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_CGROUP: + { + uint32_t nargs = (uint32_t)tinfo->m_cgroups.size(); + + if(nargs == 0) + { + return NULL; + } + + for(uint32_t j = 0; j < nargs; j++) + { + if(tinfo->m_cgroups[j].first == m_argname) + { + m_tstr = tinfo->m_cgroups[j].second; + RETURN_EXTRACT_STRING(m_tstr); + } + } + + return NULL; + } + case TYPE_VTID: + if(tinfo->m_vtid == -1) + { + return NULL; + } + + m_u64val = tinfo->m_vtid; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_VPID: + if(tinfo->m_vpid == -1) + { + return NULL; + } + + m_u64val = tinfo->m_vpid; + RETURN_EXTRACT_VAR(m_u64val); +/* + case TYPE_PROC_CPU: + { + uint16_t etype = evt->get_type(); + + if(etype == PPME_PROCINFO_E) + { + double thval; + uint64_t tcpu; + + sinsp_evt_param* parinfo = evt->get_param(0); + tcpu = *(uint64_t*)parinfo->m_val; + + parinfo = evt->get_param(1); + tcpu += *(uint64_t*)parinfo->m_val; + + if(tinfo->m_last_t_tot_cpu != 0) + { + uint64_t deltaval = tcpu - tinfo->m_last_t_tot_cpu; + thval = (double)deltaval;// / (ONE_SECOND_IN_NS / 100); + if(thval > 100) + { + thval = 100; + } + } + else + { + thval = 0; + } + + tinfo->m_last_t_tot_cpu = tcpu; + + uint64_t ets = evt->get_ts(); + sinsp_threadinfo* mt = tinfo->get_main_thread(); + + if(ets != mt->m_last_mt_cpu_ts) + { + mt->m_last_mt_tot_cpu = 0; + mt->m_last_mt_cpu_ts = ets; + } + + mt->m_last_mt_tot_cpu += thval; + m_dval = mt->m_last_mt_tot_cpu; + + RETURN_EXTRACT_VAR(m_dval); + } + + return NULL; + } +*/ + case TYPE_THREAD_CPU: + { + return extract_thread_cpu(evt, len, tinfo, true, true); + } + case TYPE_THREAD_CPU_USER: + { + return extract_thread_cpu(evt, len, tinfo, true, false); + } + case TYPE_THREAD_CPU_SYSTEM: + { + return extract_thread_cpu(evt, len, tinfo, false, true); + } + case TYPE_NAMETID: + m_tstr = tinfo->get_comm() + to_string(evt->get_tid()); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_IS_CONTAINER_HEALTHCHECK: + m_tbool = (tinfo->m_category == sinsp_threadinfo::CAT_HEALTHCHECK); + RETURN_EXTRACT_VAR(m_tbool); + case TYPE_IS_CONTAINER_LIVENESS_PROBE: + m_tbool = (tinfo->m_category == sinsp_threadinfo::CAT_LIVENESS_PROBE); + RETURN_EXTRACT_VAR(m_tbool); + case TYPE_IS_CONTAINER_READINESS_PROBE: + m_tbool = (tinfo->m_category == sinsp_threadinfo::CAT_READINESS_PROBE); + RETURN_EXTRACT_VAR(m_tbool); + default: + ASSERT(false); + return NULL; + } +} + +bool sinsp_filter_check_thread::compare_full_apid(sinsp_evt *evt) +{ + sinsp_threadinfo* tinfo = evt->get_thread_info(); + + if(tinfo == NULL) + { + return false; + } + + sinsp_threadinfo* mt = NULL; + + if(tinfo->is_main_thread()) + { + mt = tinfo; + } + else + { + mt = tinfo->get_main_thread(); + + if(mt == NULL) + { + return false; + } + } + + // + // No id specified, search in all of the ancestors + // + bool found = false; + sinsp_threadinfo::visitor_func_t visitor = [this, &found] (sinsp_threadinfo *pt) + { + bool res; + + res = flt_compare(m_cmpop, + PT_PID, + &pt->m_pid); + + if(res == true) + { + found = true; + + // Can stop traversing parent state + return false; + } + + return true; + }; + + mt->traverse_parent_state(visitor); + + return found; +} + +bool sinsp_filter_check_thread::compare_full_aname(sinsp_evt *evt) +{ + sinsp_threadinfo* tinfo = evt->get_thread_info(); + + if(tinfo == NULL) + { + return false; + } + + sinsp_threadinfo* mt = NULL; + + if(tinfo->is_main_thread()) + { + mt = tinfo; + } + else + { + mt = tinfo->get_main_thread(); + + if(mt == NULL) + { + return false; + } + } + + // + // No id specified, search in all of the ancestors + // + bool found = false; + sinsp_threadinfo::visitor_func_t visitor = [this, &found] (sinsp_threadinfo *pt) + { + bool res; + + res = flt_compare(m_cmpop, + PT_CHARBUF, + (void*)pt->m_comm.c_str()); + + if(res == true) + { + found = true; + + // Can stop traversing parent state + return false; + } + + return true; + }; + + mt->traverse_parent_state(visitor); + + return found; +} + +bool sinsp_filter_check_thread::compare(sinsp_evt *evt) +{ + if(m_field_id == TYPE_APID) + { + if(m_argid == -1) + { + return compare_full_apid(evt); + } + } + else if(m_field_id == TYPE_ANAME) + { + if(m_argid == -1) + { + return compare_full_aname(evt); + } + } + + return sinsp_filter_check::compare(evt); +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_event implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_event_fields[] = +{ + {PT_UINT64, EPF_NONE, PF_ID, "evt.num", "event number."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.time", "event timestamp as a time string that includes the nanosecond part."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.time.s", "event timestamp as a time string with no nanoseconds."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.time.iso8601", "event timestamp in ISO 8601 format, including nanoseconds and time zone offset (in UTC)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.datetime", "event timestamp as a time string that includes the date."}, + {PT_ABSTIME, EPF_NONE, PF_DEC, "evt.rawtime", "absolute event timestamp, i.e. nanoseconds from epoch."}, + {PT_ABSTIME, EPF_NONE, PF_DEC, "evt.rawtime.s", "integer part of the event timestamp (e.g. seconds since epoch)."}, + {PT_ABSTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.rawtime.ns", "fractional part of the absolute event timestamp."}, + {PT_RELTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.reltime", "number of nanoseconds from the beginning of the capture."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "evt.reltime.s", "number of seconds from the beginning of the capture."}, + {PT_RELTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.reltime.ns", "fractional part (in ns) of the time from the beginning of the capture."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "evt.latency", "delta between an exit event and the correspondent enter event, in nanoseconds."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "evt.latency.s", "integer part of the event latency delta."}, + {PT_RELTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.latency.ns", "fractional part of the event latency delta."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.latency.quantized", "10-base log of the delta between an exit event and the correspondent enter event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.latency.human", "delta between an exit event and the correspondent enter event, as a human readable string (e.g. 10.3ms)."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "evt.deltatime", "delta between this event and the previous event, in nanoseconds."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "evt.deltatime.s", "integer part of the delta between this event and the previous event."}, + {PT_RELTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.deltatime.ns", "fractional part of the delta between this event and the previous event."}, + {PT_CHARBUF, EPF_PRINT_ONLY, PF_NA, "evt.outputtime", "this depends on -t param, default is %evt.time ('h')."}, + {PT_CHARBUF, EPF_NONE, PF_DIR, "evt.dir", "event direction can be either '>' for enter events or '<' for exit events."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.type", "The name of the event (e.g. 'open')."}, + {PT_UINT32, EPF_REQUIRES_ARGUMENT, PF_NA, "evt.type.is", "allows one to specify an event type, and returns 1 for events that are of that type. For example, evt.type.is.open returns 1 for open events, 0 for any other event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "syscall.type", "For system call events, the name of the system call (e.g. 'open'). Unset for other events (e.g. switch or sysdig internal events). Use this field instead of evt.type if you need to make sure that the filtered/printed value is actually a system call."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.category", "The event category. Example values are 'file' (for file operations like open and close), 'net' (for network operations like socket and bind), memory (for things like brk or mmap), and so on."}, + {PT_INT16, EPF_NONE, PF_ID, "evt.cpu", "number of the CPU where this event happened."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.args", "all the event arguments, aggregated into a single string."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evt.arg", "one of the event arguments specified by name or by number. Some events (e.g. return codes or FDs) will be converted into a text representation when possible. E.g. 'evt.arg.fd' or 'evt.arg[0]'."}, + {PT_DYN, EPF_REQUIRES_ARGUMENT, PF_NA, "evt.rawarg", "one of the event arguments specified by name. E.g. 'evt.rawarg.fd'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.info", "for most events, this field returns the same value as evt.args. However, for some events (like writes to /dev/log) it provides higher level information coming from decoding the arguments."}, + {PT_BYTEBUF, EPF_NONE, PF_NA, "evt.buffer", "the binary data buffer for events that have one, like read(), recvfrom(), etc. Use this field in filters with 'contains' to search into I/O data buffers."}, + {PT_UINT64, EPF_NONE, PF_DEC, "evt.buflen", "the length of the binary data buffer for events that have one, like read(), recvfrom(), etc."}, + {PT_CHARBUF, EPF_NONE, PF_DEC, "evt.res", "event return value, as a string. If the event failed, the result is an error code string (e.g. 'ENOENT'), otherwise the result is the string 'SUCCESS'."}, + {PT_INT64, EPF_NONE, PF_DEC, "evt.rawres", "event return value, as a number (e.g. -2). Useful for range comparisons."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.failed", "'true' for events that returned an error status."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_io", "'true' for events that read or write to FDs, like read(), send, recvfrom(), etc."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_io_read", "'true' for events that read from FDs, like read(), recv(), recvfrom(), etc."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_io_write", "'true' for events that write to FDs, like write(), send(), etc."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "evt.io_dir", "'r' for events that read from FDs, like read(); 'w' for events that write to FDs, like write()."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_wait", "'true' for events that make the thread wait, e.g. sleep(), select(), poll()."}, + {PT_RELTIME, EPF_NONE, PF_DEC, "evt.wait_latency", "for events that make the thread wait (e.g. sleep(), select(), poll()), this is the time spent waiting for the event to return, in nanoseconds."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_syslog", "'true' for events that are writes to /dev/log."}, + {PT_UINT32, EPF_NONE, PF_DEC, "evt.count", "This filter field always returns 1 and can be used to count events from inside chisels."}, + {PT_UINT32, EPF_NONE, PF_DEC, "evt.count.error", "This filter field returns 1 for events that returned with an error, and can be used to count event failures from inside chisels."}, + {PT_UINT32, EPF_NONE, PF_DEC, "evt.count.error.file", "This filter field returns 1 for events that returned with an error and are related to file I/O, and can be used to count event failures from inside chisels."}, + {PT_UINT32, EPF_NONE, PF_DEC, "evt.count.error.net", "This filter field returns 1 for events that returned with an error and are related to network I/O, and can be used to count event failures from inside chisels."}, + {PT_UINT32, EPF_NONE, PF_DEC, "evt.count.error.memory", "This filter field returns 1 for events that returned with an error and are related to memory allocation, and can be used to count event failures from inside chisels."}, + {PT_UINT32, EPF_NONE, PF_DEC, "evt.count.error.other", "This filter field returns 1 for events that returned with an error and are related to none of the previous categories, and can be used to count event failures from inside chisels."}, + {PT_UINT32, EPF_NONE, PF_DEC, "evt.count.exit", "This filter field returns 1 for exit events, and can be used to count single events from inside chisels."}, + {PT_UINT32, EPF_TABLE_ONLY, PF_DEC, "evt.count.procinfo", "This filter field returns 1 for procinfo events generated by process main threads, and can be used to count processes from inside views."}, + {PT_UINT32, EPF_TABLE_ONLY, PF_DEC, "evt.count.threadinfo", "This filter field returns 1 for procinfo events, and can be used to count processes from inside views."}, + {PT_UINT64, (filtercheck_field_flags) (EPF_FILTER_ONLY | EPF_REQUIRES_ARGUMENT), PF_DEC, "evt.around", "Accepts the event if it's around the specified time interval. The syntax is evt.around[T]=D, where T is the value returned by %evt.rawtime for the event and D is a delta in milliseconds. For example, evt.around[1404996934793590564]=1000 will return the events with timestamp with one second before the timestamp and one second after it, for a total of two seconds of capture."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evt.abspath", "Absolute path calculated from dirfd and name during syscalls like renameat and symlinkat. Use 'evt.abspath.src' or 'evt.abspath.dst' for syscalls that support multiple paths."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.in", "the length of the binary data buffer, but only for input I/O events."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.out", "the length of the binary data buffer, but only for output I/O events."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.file", "the length of the binary data buffer, but only for file I/O events."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.file.in", "the length of the binary data buffer, but only for input file I/O events."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.file.out", "the length of the binary data buffer, but only for output file I/O events."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.net", "the length of the binary data buffer, but only for network I/O events."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.net.in", "the length of the binary data buffer, but only for input network I/O events."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "evt.buflen.net.out", "the length of the binary data buffer, but only for output network I/O events."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_open_read", "'true' for open/openat events where the path was opened for reading"}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_open_write", "'true' for open/openat events where the path was opened for writing"}, + {PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "evt.infra.docker.name", "for docker infrastructure events, the name of the event."}, + {PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "evt.infra.docker.container.id", "for docker infrastructure events, the id of the impacted container."}, + {PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "evt.infra.docker.container.name", "for docker infrastructure events, the name of the impacted container."}, + {PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "evt.infra.docker.container.image", "for docker infrastructure events, the image name of the impacted container."}, + {PT_BOOL, EPF_NONE, PF_NA, "evt.is_open_exec", "'true' for open/openat or creat events where a file is created with execute permissions"}, +}; + +sinsp_filter_check_event::sinsp_filter_check_event() +{ + m_is_compare = false; + m_info.m_name = "evt"; + m_info.m_fields = sinsp_filter_check_event_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_event_fields) / sizeof(sinsp_filter_check_event_fields[0]); + m_u64val = 0; + m_converter = new sinsp_filter_check_reference(); + + m_storage_size = UESTORAGE_INITIAL_BUFSIZE; + m_storage = (char*)malloc(m_storage_size); + if(m_storage == NULL) + { + throw sinsp_exception("memory allocation error in sinsp_filter_check_appevt::sinsp_filter_check_event"); + } + + m_cargname = NULL; +} + +sinsp_filter_check_event::~sinsp_filter_check_event() +{ + if(m_storage != NULL) + { + free(m_storage); + } + + if(m_converter != NULL) + { + delete m_converter; + } +} + +sinsp_filter_check* sinsp_filter_check_event::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_event(); +} + +int32_t sinsp_filter_check_event::extract_arg(string fldname, string val, OUT const struct ppm_param_info** parinfo) +{ + uint32_t parsed_len = 0; + + // + // 'arg' and 'resarg' are handled in a custom way + // + if(val[fldname.size()] == '[') + { + if(parinfo != NULL) + { + throw sinsp_exception("evt.arg fields must be expressed explicitly"); + } + + parsed_len = (uint32_t)val.find(']'); + string numstr = val.substr(fldname.size() + 1, parsed_len - fldname.size() - 1); + + if(m_field_id == TYPE_AROUND) + { + m_u64val = sinsp_numparser::parseu64(numstr); + } + else + { + m_argid = sinsp_numparser::parsed32(numstr); + } + + parsed_len++; + } + else if(val[fldname.size()] == '.') + { + if(m_field_id == TYPE_AROUND) + { + throw sinsp_exception("wrong syntax for evt.around"); + } + + const struct ppm_param_info* pi = + sinsp_utils::find_longest_matching_evt_param(val.substr(fldname.size() + 1)); + + if(pi == NULL) + { + throw sinsp_exception("unknown event argument " + val.substr(fldname.size() + 1)); + } + + m_argname = pi->name; + parsed_len = (uint32_t)(fldname.size() + strlen(pi->name) + 1); + m_argid = -1; + + if(parinfo != NULL) + { + *parinfo = pi; + } + } + else + { + throw sinsp_exception("filter syntax error: " + val); + } + + return parsed_len; +} + +int32_t sinsp_filter_check_event::extract_type(string fldname, string val, OUT const struct ppm_param_info** parinfo) +{ + uint32_t parsed_len = 0; + + if(val[fldname.size()] == '.') + { + string itype = val.substr(fldname.size() + 1); + + if(sinsp_numparser::tryparseu32(itype, &m_evtid)) + { + m_evtid1 = PPM_EVENT_MAX; + parsed_len = (uint32_t)(fldname.size() + itype.size() + 1); + return parsed_len; + } + + for(uint32_t j = 0; j < PPM_EVENT_MAX; j++) + { + const ppm_event_info* ei = &g_infotables.m_event_info[j]; + + if(itype == ei->name) + { + m_evtid = j; + m_evtid1 = j + 1; + parsed_len = (uint32_t)(fldname.size() + strlen(ei->name) + 1); + break; + } + } + } + else + { + throw sinsp_exception("filter syntax error: " + val); + } + + return parsed_len; +} + +int32_t sinsp_filter_check_event::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + string val(str); + int32_t res = 0; + + // + // A couple of fields are handled in a custom way + // + if(string(val, 0, sizeof("evt.arg") - 1) == "evt.arg" && + string(val, 0, sizeof("evt.args") - 1) != "evt.args") + { + m_field_id = TYPE_ARGSTR; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evt.arg", val, NULL); + } + else if(string(val, 0, sizeof("evt.rawarg") - 1) == "evt.rawarg") + { + m_field_id = TYPE_ARGRAW; + m_customfield = m_info.m_fields[m_field_id]; + m_field = &m_customfield; + + res = extract_arg("evt.rawarg", val, &m_arginfo); + + m_customfield.m_type = m_arginfo->type; + } + else if(string(val, 0, sizeof("evt.around") - 1) == "evt.around") + { + m_field_id = TYPE_AROUND; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evt.around", val, NULL); + } + else if(string(val, 0, sizeof("evt.latency") - 1) == "evt.latency" || + string(val, 0, sizeof("evt.latency.s") - 1) == "evt.latency.s" || + string(val, 0, sizeof("evt.latency.ns") - 1) == "evt.latency.ns" || + string(val, 0, sizeof("evt.latency.quantized") - 1) == "evt.latency.quantized" || + string(val, 0, sizeof("evt.latency.human") - 1) == "evt.latency.human") + { + // + // These fields need to store the previous event type in the thread state + // + if(alloc_state) + { + m_th_state_id = m_inspector->reserve_thread_memory(sizeof(uint16_t)); + } + + res = sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } + else if(string(val, 0, sizeof("evt.abspath") - 1) == "evt.abspath") + { + m_field_id = TYPE_ABSPATH; + m_field = &m_info.m_fields[m_field_id]; + + if(string(val, 0, sizeof("evt.abspath.src") - 1) == "evt.abspath.src") + { + m_argid = 1; + res = sizeof("evt.abspath.src") - 1; + } + else if(string(val, 0, sizeof("evt.abspath.dst") - 1) == "evt.abspath.dst") + { + m_argid = 2; + res = sizeof("evt.abspath.dst") - 1; + } + else + { + m_argid = 0; + res = sizeof("evt.abspath") - 1; + } + } + else if(string(val, 0, sizeof("evt.type.is") - 1) == "evt.type.is") + { + m_field_id = TYPE_TYPE_IS; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_type("evt.type.is", val, NULL); + } + else + { + res = sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } + + return res; +} + +size_t sinsp_filter_check_event::parse_filter_value(const char* str, uint32_t len, uint8_t *storage, uint32_t storage_len) +{ + size_t parsed_len; + if(m_field_id == sinsp_filter_check_event::TYPE_ARGRAW) + { + ASSERT(m_arginfo != NULL); + parsed_len = sinsp_filter_value_parser::string_to_rawval(str, len, filter_value_p(), filter_value().size(), m_arginfo->type); + } + else + { + parsed_len = sinsp_filter_check::parse_filter_value(str, len, storage, storage_len); + } + + return parsed_len; +} + + + +void sinsp_filter_check_event::validate_filter_value(const char* str, uint32_t len) +{ + string val(str); + + if(m_field_id == TYPE_TYPE) + { + sinsp_evttables* einfo = m_inspector->get_event_info_tables(); + const struct ppm_event_info* etable = einfo->m_event_info; + const struct ppm_syscall_desc* stable = einfo->m_syscall_info_table; + string stype(str, len); + + for(uint32_t j = 0; j < PPM_EVENT_MAX; j++) + { + if(stype == etable[j].name) + { + return; + } + } + + for(uint32_t j = 0; j < PPM_SC_MAX; j++) + { + if(stype == stable[j].name) + { + return; + } + } + + throw sinsp_exception("unknown event type " + stype); + } + else if(m_field_id == TYPE_AROUND) + { + if(m_cmpop != CO_EQ) + { + throw sinsp_exception("evt.around supports only '=' comparison operator"); + } + + m_tsdelta = sinsp_numparser::parseu64(str) * 1000000; + + return; + } +} + +const filtercheck_field_info* sinsp_filter_check_event::get_field_info() +{ + if(m_field_id == TYPE_ARGRAW) + { + return &m_customfield; + } + else + { + return &m_info.m_fields[m_field_id]; + } +} + +uint8_t* extract_argraw(sinsp_evt *evt, OUT uint32_t* len, const char *argname) +{ + const sinsp_evt_param* pi = evt->get_param_value_raw(argname); + + if(pi != NULL) + { + *len = pi->m_len; + return (uint8_t*)pi->m_val; + } + else + { + return NULL; + } +} + +uint8_t *sinsp_filter_check_event::extract_abspath(sinsp_evt *evt, OUT uint32_t *len) +{ + sinsp_evt_param *parinfo; + char *path; + uint32_t pathlen; + string spath; + + if(evt->m_tinfo == NULL) + { + return NULL; + } + + uint16_t etype = evt->get_type(); + + const char *dirfdarg = NULL, *patharg = NULL; + if(etype == PPME_SYSCALL_RENAMEAT_X || etype == PPME_SYSCALL_RENAMEAT2_X) + { + if(m_argid == 0 || m_argid == 1) + { + dirfdarg = "olddirfd"; + patharg = "oldpath"; + } + else if(m_argid == 2) + { + dirfdarg = "newdirfd"; + patharg = "newpath"; + } + } + else if(etype == PPME_SYSCALL_SYMLINKAT_X) + { + dirfdarg = "linkdirfd"; + patharg = "linkpath"; + } + else if(etype == PPME_SYSCALL_OPENAT_E || etype == PPME_SYSCALL_OPENAT_2_X) + { + dirfdarg = "dirfd"; + patharg = "name"; + } + else if(etype == PPME_SYSCALL_LINKAT_E || etype == PPME_SYSCALL_LINKAT_2_X) + { + if(m_argid == 0 || m_argid == 1) + { + dirfdarg = "olddir"; + patharg = "oldpath"; + } + else if(m_argid == 2) + { + dirfdarg = "newdir"; + patharg = "newpath"; + } + } + else if(etype == PPME_SYSCALL_UNLINKAT_E || etype == PPME_SYSCALL_UNLINKAT_2_X) + { + dirfdarg = "dirfd"; + patharg = "name"; + } + else if(etype == PPME_SYSCALL_MKDIRAT_X) + { + dirfdarg = "dirfd"; + patharg = "path"; + } + else if(etype == PPME_SYSCALL_FCHMODAT_X) + { + dirfdarg = "dirfd"; + patharg = "filename"; + } + + if(!dirfdarg || !patharg) + { + return 0; + } + + int dirfdargidx = -1, pathargidx = -1, idx = 0; + while (((dirfdargidx < 0) || (pathargidx < 0)) && (idx < (int) evt->get_num_params())) + { + const char *name = evt->get_param_name(idx); + if((dirfdargidx < 0) && (strcmp(name, dirfdarg) == 0)) + { + dirfdargidx = idx; + } + if((pathargidx < 0) && (strcmp(name, patharg) == 0)) + { + pathargidx = idx; + } + idx++; + } + + if((dirfdargidx < 0) || (pathargidx < 0)) + { + return 0; + } + + parinfo = evt->get_param(dirfdargidx); + ASSERT(parinfo->m_len == sizeof(int64_t)); + int64_t dirfd = *(int64_t *)parinfo->m_val; + + parinfo = evt->get_param(pathargidx); + path = parinfo->m_val; + pathlen = parinfo->m_len; + + string sdir; + + bool is_absolute = (path[0] == '/'); + if(is_absolute) + { + // + // The path is absolute. + // Some processes (e.g. irqbalance) actually do this: they pass an invalid fd and + // and absolute path, and openat succeeds. + // + sdir = "."; + } + else if(dirfd == PPM_AT_FDCWD) + { + sdir = evt->m_tinfo->get_cwd(); + } + else + { + evt->m_fdinfo = evt->m_tinfo->get_fd(dirfd); + + if(evt->m_fdinfo == NULL) + { + ASSERT(false); + sdir = "/"; + } + else + { + if(evt->m_fdinfo->m_name[evt->m_fdinfo->m_name.length()] == '/') + { + sdir = evt->m_fdinfo->m_name; + } + else + { + sdir = evt->m_fdinfo->m_name + '/'; + } + } + } + + char fullname[SCAP_MAX_PATH_SIZE]; + sinsp_utils::concatenate_paths(fullname, SCAP_MAX_PATH_SIZE, sdir.c_str(), (uint32_t)sdir.length(), path, pathlen); + + m_strstorage = fullname; + + RETURN_EXTRACT_STRING(m_strstorage); +} + +inline uint8_t* sinsp_filter_check_event::extract_buflen(sinsp_evt *evt, OUT uint32_t* len) +{ + if(evt->get_direction() == SCAP_ED_OUT) + { + sinsp_evt_param *parinfo; + int64_t retval; + + // + // Extract the return value + // + parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(int64_t)); + retval = *(int64_t *)parinfo->m_val; + + if(retval >= 0) + { + RETURN_EXTRACT_PTR(parinfo->m_val); + } + } + + return NULL; +} + +Json::Value sinsp_filter_check_event::extract_as_js(sinsp_evt *evt, OUT uint32_t* len) +{ + switch(m_field_id) + { + case TYPE_TIME: + case TYPE_TIME_S: + case TYPE_TIME_ISO8601: + case TYPE_DATETIME: + case TYPE_RUNTIME_TIME_OUTPUT_FORMAT: + return (Json::Value::Int64)evt->get_ts(); + + case TYPE_RAWTS: + case TYPE_RAWTS_S: + case TYPE_RAWTS_NS: + case TYPE_RELTS: + case TYPE_RELTS_S: + case TYPE_RELTS_NS: + case TYPE_LATENCY: + case TYPE_LATENCY_S: + case TYPE_LATENCY_NS: + case TYPE_DELTA: + case TYPE_DELTA_S: + case TYPE_DELTA_NS: + return (Json::Value::Int64)*(uint64_t*)extract(evt, len); + case TYPE_COUNT: + m_u32val = 1; + return m_u32val; + + default: + return Json::nullValue; + } + + return Json::nullValue; +} + +uint8_t* sinsp_filter_check_event::extract_error_count(sinsp_evt *evt, OUT uint32_t* len) +{ + const sinsp_evt_param* pi = evt->get_param_value_raw("res"); + + if(pi != NULL) + { + ASSERT(pi->m_len == sizeof(uint64_t)); + + int64_t res = *(int64_t*)pi->m_val; + if(res < 0) + { + m_u32val = 1; + RETURN_EXTRACT_VAR(m_u32val); + } + else + { + return NULL; + } + } + + if((evt->get_info_flags() & EF_CREATES_FD) && PPME_IS_EXIT(evt->get_type())) + { + pi = evt->get_param_value_raw("fd"); + + if(pi != NULL) + { + ASSERT(pi->m_len == sizeof(uint64_t)); + + int64_t res = *(int64_t*)pi->m_val; + if(res < 0) + { + m_u32val = 1; + RETURN_EXTRACT_VAR(m_u32val); + } + } + } + + return NULL; +} + +uint8_t* sinsp_filter_check_event::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + switch(m_field_id) + { + case TYPE_TIME: +// if(g_filterchecks_force_raw_times) + if(false) + { + m_strstorage = to_string(evt->get_ts()); + } + else + { + sinsp_utils::ts_to_string(evt->get_ts(), &m_strstorage, false, true); + } + RETURN_EXTRACT_STRING(m_strstorage); + case TYPE_TIME_S: + sinsp_utils::ts_to_string(evt->get_ts(), &m_strstorage, false, false); + RETURN_EXTRACT_STRING(m_strstorage); + case TYPE_TIME_ISO8601: + sinsp_utils::ts_to_iso_8601(evt->get_ts(), &m_strstorage); + RETURN_EXTRACT_STRING(m_strstorage); + case TYPE_DATETIME: + sinsp_utils::ts_to_string(evt->get_ts(), &m_strstorage, true, true); + RETURN_EXTRACT_STRING(m_strstorage); + case TYPE_RAWTS: + RETURN_EXTRACT_VAR(evt->m_pevt->ts); + case TYPE_RAWTS_S: + m_u64val = evt->get_ts() / ONE_SECOND_IN_NS; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_RAWTS_NS: + m_u64val = evt->get_ts() % ONE_SECOND_IN_NS; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_RELTS: + m_u64val = evt->get_ts() - m_inspector->m_firstevent_ts; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_RELTS_S: + m_u64val = (evt->get_ts() - m_inspector->m_firstevent_ts) / ONE_SECOND_IN_NS; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_RELTS_NS: + m_u64val = (evt->get_ts() - m_inspector->m_firstevent_ts) % ONE_SECOND_IN_NS; + RETURN_EXTRACT_VAR(m_u64val); + case TYPE_LATENCY: + { + m_u64val = 0; + + if(evt->m_tinfo != NULL) + { + ppm_event_category ecat = evt->get_info_category(); + if(ecat & EC_INTERNAL) + { + return NULL; + } + + m_u64val = evt->m_tinfo->m_latency; + } + + RETURN_EXTRACT_VAR(m_u64val); + } + case TYPE_LATENCY_HUMAN: + { + m_u64val = 0; + + if(evt->m_tinfo != NULL) + { + ppm_event_category ecat = evt->get_info_category(); + if(ecat & EC_INTERNAL) + { + return NULL; + } + + m_converter->set_val(PT_RELTIME, + (uint8_t*)&evt->m_tinfo->m_latency, + 8, + 0, + ppm_print_format::PF_DEC); + + m_strstorage = m_converter->tostring_nice(NULL, 0, 1000000000); + } + + RETURN_EXTRACT_STRING(m_strstorage); + } + case TYPE_LATENCY_S: + case TYPE_LATENCY_NS: + { + m_u64val = 0; + + if(evt->m_tinfo != NULL) + { + ppm_event_category ecat = evt->get_info_category(); + if(ecat & EC_INTERNAL) + { + return NULL; + } + + uint64_t lat = evt->m_tinfo->m_latency; + + if(m_field_id == TYPE_LATENCY_S) + { + m_u64val = lat / 1000000000; + } + else + { + m_u64val = lat % 1000000000; + } + } + + RETURN_EXTRACT_VAR(m_u64val); + } + case TYPE_LATENCY_QUANTIZED: + { + if(evt->m_tinfo != NULL) + { + ppm_event_category ecat = evt->get_info_category(); + if(ecat & EC_INTERNAL) + { + return NULL; + } + + uint64_t lat = evt->m_tinfo->m_latency; + if(lat != 0) + { + double llatency = log10((double)lat); + + if(llatency > 11) + { + llatency = 11; + } + + m_u64val = (uint64_t)(llatency * g_csysdig_screen_w / 11) + 1; + + RETURN_EXTRACT_VAR(m_u64val); + } + } + + return NULL; + } + case TYPE_DELTA: + case TYPE_DELTA_S: + case TYPE_DELTA_NS: + { + if(m_u64val == 0) + { + m_u64val = evt->get_ts(); + m_tsdelta = 0; + } + else + { + uint64_t tts = evt->get_ts(); + + if(m_field_id == TYPE_DELTA) + { + m_tsdelta = tts - m_u64val; + } + else if(m_field_id == TYPE_DELTA_S) + { + m_tsdelta = (tts - m_u64val) / ONE_SECOND_IN_NS; + } + else if(m_field_id == TYPE_DELTA_NS) + { + m_tsdelta = (tts - m_u64val) % ONE_SECOND_IN_NS; + } + + m_u64val = tts; + } + + RETURN_EXTRACT_VAR(m_tsdelta); + } + case TYPE_RUNTIME_TIME_OUTPUT_FORMAT: + { + char timebuffer[100]; + m_strstorage = ""; + switch(m_inspector->m_output_time_flag) + { + case 'h': + sinsp_utils::ts_to_string(evt->get_ts(), &m_strstorage, false, true); + RETURN_EXTRACT_STRING(m_strstorage); + + case 'a': + m_strstorage += to_string(evt->get_ts() / ONE_SECOND_IN_NS); + m_strstorage += "."; + m_strstorage += to_string(evt->get_ts() % ONE_SECOND_IN_NS); + RETURN_EXTRACT_STRING(m_strstorage); + + case 'r': + m_strstorage += to_string((evt->get_ts() - m_inspector->m_firstevent_ts) / ONE_SECOND_IN_NS); + m_strstorage += "."; + snprintf(timebuffer, sizeof(timebuffer), "%09llu", (evt->get_ts() - m_inspector->m_firstevent_ts) % ONE_SECOND_IN_NS); + m_strstorage += string(timebuffer); + RETURN_EXTRACT_STRING(m_strstorage); + + case 'd': + { + if(evt->m_tinfo != NULL) + { + long long unsigned lat = evt->m_tinfo->m_latency; + + m_strstorage += to_string(lat / 1000000000); + m_strstorage += "."; + snprintf(timebuffer, sizeof(timebuffer), "%09llu", lat % 1000000000); + m_strstorage += string(timebuffer); + } + else + { + m_strstorage = "0.000000000"; + } + + RETURN_EXTRACT_STRING(m_strstorage); + } + + case 'D': + if(m_u64val == 0) + { + m_u64val = evt->get_ts(); + m_tsdelta = 0; + } + uint64_t tts = evt->get_ts(); + + m_strstorage += to_string((tts - m_u64val) / ONE_SECOND_IN_NS); + m_tsdelta = (tts - m_u64val) / ONE_SECOND_IN_NS; + m_strstorage += "."; + snprintf(timebuffer, sizeof(timebuffer), "%09llu", (tts - m_u64val) % ONE_SECOND_IN_NS); + m_strstorage += string(timebuffer); + m_tsdelta = (tts - m_u64val) % ONE_SECOND_IN_NS; + + m_u64val = tts; + RETURN_EXTRACT_STRING(m_strstorage); + } + } + case TYPE_DIR: + if(PPME_IS_ENTER(evt->get_type())) + { + RETURN_EXTRACT_CSTR(">"); + } + else + { + RETURN_EXTRACT_CSTR("<"); + } + case TYPE_TYPE: + { + uint8_t* evname; + uint16_t etype = evt->m_pevt->type; + + if(etype == PPME_GENERIC_E || etype == PPME_GENERIC_X) + { + sinsp_evt_param *parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint16_t)); + uint16_t evid = *(uint16_t *)parinfo->m_val; + + evname = (uint8_t*)g_infotables.m_syscall_info_table[evid].name; + } + else + { + evname = (uint8_t*)evt->get_name(); + } + + RETURN_EXTRACT_CSTR(evname); + } + break; + case TYPE_TYPE_IS: + { + uint16_t etype = evt->m_pevt->type; + + if(etype == m_evtid || etype == m_evtid1) + { + m_u32val = 1; + } + else + { + m_u32val = 0; + } + + RETURN_EXTRACT_VAR(m_u32val); + } + break; + case TYPE_SYSCALL_TYPE: + { + uint8_t* evname; + uint16_t etype = evt->m_pevt->type; + enum ppm_event_flags flags = g_infotables.m_event_info[etype].flags; + + if(etype == PPME_SCHEDSWITCH_6_E || + (flags & EC_INTERNAL) || (flags & EF_SKIPPARSERESET)) + { + return NULL; + } + + if(etype == PPME_GENERIC_E || etype == PPME_GENERIC_X) + { + sinsp_evt_param *parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint16_t)); + uint16_t evid = *(uint16_t *)parinfo->m_val; + + evname = (uint8_t*)g_infotables.m_syscall_info_table[evid].name; + } + else + { + evname = (uint8_t*)evt->get_name(); + } + + RETURN_EXTRACT_CSTR(evname); + } + break; + case TYPE_CATEGORY: + sinsp_evt::category cat; + evt->get_category(&cat); + + switch(cat.m_category) + { + case EC_UNKNOWN: + m_strstorage = "unknown"; + break; + case EC_OTHER: + m_strstorage = "other"; + break; + case EC_FILE: + m_strstorage = "file"; + break; + case EC_NET: + m_strstorage = "net"; + break; + case EC_IPC: + m_strstorage = "IPC"; + break; + case EC_MEMORY: + m_strstorage = "memory"; + break; + case EC_PROCESS: + m_strstorage = "process"; + break; + case EC_SLEEP: + m_strstorage = "sleep"; + break; + case EC_SYSTEM: + m_strstorage = "system"; + break; + case EC_SIGNAL: + m_strstorage = "signal"; + break; + case EC_USER: + m_strstorage = "user"; + break; + case EC_TIME: + m_strstorage = "time"; + break; + case EC_PROCESSING: + m_strstorage = "processing"; + break; + case EC_IO_READ: + case EC_IO_WRITE: + case EC_IO_OTHER: + { + switch(cat.m_subcategory) + { + case sinsp_evt::SC_FILE: + m_strstorage = "file"; + break; + case sinsp_evt::SC_NET: + m_strstorage = "net"; + break; + case sinsp_evt::SC_IPC: + m_strstorage = "ipc"; + break; + case sinsp_evt::SC_NONE: + case sinsp_evt::SC_UNKNOWN: + case sinsp_evt::SC_OTHER: + m_strstorage = "unknown"; + break; + default: + ASSERT(false); + m_strstorage = "unknown"; + break; + } + } + break; + case EC_WAIT: + m_strstorage = "wait"; + break; + case EC_SCHEDULER: + m_strstorage = "scheduler"; + break; + default: + m_strstorage = "unknown"; + break; + } + + RETURN_EXTRACT_STRING(m_strstorage); + case TYPE_NUMBER: + RETURN_EXTRACT_VAR(evt->m_evtnum); + case TYPE_CPU: + RETURN_EXTRACT_VAR(evt->m_cpuid); + case TYPE_ARGRAW: + return extract_argraw(evt, len, m_arginfo->name); + break; + case TYPE_ARGSTR: + { + const char* resolved_argstr; + const char* argstr; + + ASSERT(m_inspector != NULL); + + if(m_argid != -1) + { + if(m_argid >= (int32_t)evt->get_num_params()) + { + return NULL; + } + + argstr = evt->get_param_as_str(m_argid, &resolved_argstr, m_inspector->get_buffer_format()); + } + else + { + argstr = evt->get_param_value_str(m_argname.c_str(), &resolved_argstr, m_inspector->get_buffer_format()); + } + + if(resolved_argstr != NULL && resolved_argstr[0] != 0) + { + RETURN_EXTRACT_CSTR(resolved_argstr); + } + else + { + RETURN_EXTRACT_CSTR(argstr); + } + } + break; + case TYPE_INFO: + { + sinsp_fdinfo_t* fdinfo = evt->m_fdinfo; + + if(fdinfo != NULL && fdinfo->m_callbacks != NULL) + { + char* il; + vector* cbacks = &(fdinfo->m_callbacks->m_write_callbacks); + + for(auto it = cbacks->begin(); it != cbacks->end(); ++it) + { + if((*it)->get_info_line(&il)) + { + RETURN_EXTRACT_CSTR(il); + } + } + } + } + // + // NOTE: this falls through to TYPE_ARGSTR, and that's what we want! + // Please don't add anything here! + // + case TYPE_ARGS: + { + if(evt->get_type() == PPME_GENERIC_E || evt->get_type() == PPME_GENERIC_X) + { + // + // Don't print the arguments for generic events: they have only internal use + // + RETURN_EXTRACT_CSTR(""); + } + + const char* resolved_argstr = NULL; + const char* argstr = NULL; + uint32_t nargs = evt->get_num_params(); + m_strstorage.clear(); + + for(uint32_t j = 0; j < nargs; j++) + { + ASSERT(m_inspector != NULL); + + argstr = evt->get_param_as_str(j, &resolved_argstr, m_inspector->get_buffer_format()); + + if(resolved_argstr[0] == 0) + { + m_strstorage += evt->get_param_name(j); + m_strstorage += '='; + m_strstorage += argstr; + m_strstorage += " "; + } + else + { + m_strstorage += evt->get_param_name(j); + m_strstorage += '='; + m_strstorage += argstr; + m_strstorage += string("(") + resolved_argstr + ") "; + } + } + + RETURN_EXTRACT_STRING(m_strstorage); + } + break; + case TYPE_BUFFER: + { + if(m_is_compare) + { + return extract_argraw(evt, len, "data"); + } + + const char* resolved_argstr; + const char* argstr; + argstr = evt->get_param_value_str("data", &resolved_argstr, m_inspector->get_buffer_format()); + *len = evt->m_rawbuf_str_len; + + return (uint8_t*)argstr; + } + case TYPE_BUFLEN: + if(evt->m_fdinfo && evt->get_category() & EC_IO_BASE) + { + return extract_buflen(evt, len); + } + break; + case TYPE_RESRAW: + { + const sinsp_evt_param* pi = evt->get_param_value_raw("res"); + + if(pi != NULL) + { + *len = pi->m_len; + return (uint8_t*)pi->m_val; + } + + if((evt->get_info_flags() & EF_CREATES_FD) && PPME_IS_EXIT(evt->get_type())) + { + pi = evt->get_param_value_raw("fd"); + + if(pi != NULL) + { + *len = pi->m_len; + return (uint8_t*)pi->m_val; + } + } + + return NULL; + } + break; + case TYPE_RESSTR: + { + const char* resolved_argstr; + const char* argstr; + + const sinsp_evt_param* pi = evt->get_param_value_raw("res"); + + if(pi != NULL) + { + ASSERT(pi->m_len == sizeof(int64_t)); + + int64_t res = *(int64_t*)pi->m_val; + + if(res >= 0) + { + RETURN_EXTRACT_CSTR("SUCCESS"); + } + else + { + argstr = evt->get_param_value_str("res", &resolved_argstr); + ASSERT(resolved_argstr != NULL && resolved_argstr[0] != 0); + + if(resolved_argstr != NULL && resolved_argstr[0] != 0) + { + RETURN_EXTRACT_CSTR(resolved_argstr); + } + else if(argstr != NULL) + { + RETURN_EXTRACT_CSTR(argstr); + } + } + } + else + { + if((evt->get_info_flags() & EF_CREATES_FD) && PPME_IS_EXIT(evt->get_type())) + { + pi = evt->get_param_value_raw("fd"); + + int64_t res = *(int64_t*)pi->m_val; + + if(res >= 0) + { + RETURN_EXTRACT_CSTR("SUCCESS"); + } + else + { + argstr = evt->get_param_value_str("fd", &resolved_argstr); + ASSERT(resolved_argstr != NULL && resolved_argstr[0] != 0); + + if(resolved_argstr != NULL && resolved_argstr[0] != 0) + { + RETURN_EXTRACT_CSTR(resolved_argstr); + } + else if(argstr != NULL) + { + RETURN_EXTRACT_CSTR(argstr); + } + } + } + } + + return NULL; + } + break; + case TYPE_FAILED: + { + m_u32val = 0; + const sinsp_evt_param* pi = evt->get_param_value_raw("res"); + + if(pi != NULL) + { + ASSERT(pi->m_len == sizeof(int64_t)); + if(*(int64_t*)pi->m_val < 0) + { + m_u32val = 1; + } + } + else if((evt->get_info_flags() & EF_CREATES_FD) && PPME_IS_EXIT(evt->get_type())) + { + pi = evt->get_param_value_raw("fd"); + + if(pi != NULL) + { + ASSERT(pi->m_len == sizeof(int64_t)); + if(*(int64_t*)pi->m_val < 0) + { + m_u32val = 1; + } + } + } + + RETURN_EXTRACT_VAR(m_u32val); + } + break; + case TYPE_ISIO: + { + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & (EF_READS_FROM_FD | EF_WRITES_TO_FD)) + { + m_u32val = 1; + } + else + { + m_u32val = 0; + } + } + + RETURN_EXTRACT_VAR(m_u32val); + case TYPE_ISIO_READ: + { + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & EF_READS_FROM_FD) + { + m_u32val = 1; + } + else + { + m_u32val = 0; + } + + RETURN_EXTRACT_VAR(m_u32val); + } + case TYPE_ISIO_WRITE: + { + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & EF_WRITES_TO_FD) + { + m_u32val = 1; + } + else + { + m_u32val = 0; + } + + RETURN_EXTRACT_VAR(m_u32val); + } + case TYPE_IODIR: + { + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & EF_WRITES_TO_FD) + { + m_strstorage = "write"; + } + else if(eflags & EF_READS_FROM_FD) + { + m_strstorage = "read"; + } + else + { + return NULL; + } + + RETURN_EXTRACT_STRING(m_strstorage); + } + case TYPE_ISWAIT: + { + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & (EF_WAITS)) + { + m_u32val = 1; + } + else + { + m_u32val = 0; + } + } + + RETURN_EXTRACT_VAR(m_u32val); + case TYPE_WAIT_LATENCY: + { + ppm_event_flags eflags = evt->get_info_flags(); + uint16_t etype = evt->m_pevt->type; + + if(eflags & (EF_WAITS) && PPME_IS_EXIT(etype)) + { + if(evt->m_tinfo != NULL) + { + m_u64val = evt->m_tinfo->m_latency; + } + else + { + m_u64val = 0; + } + + RETURN_EXTRACT_VAR(m_u64val); + } + else + { + return NULL; + } + } + case TYPE_ISSYSLOG: + { + m_u32val = 0; + + ppm_event_flags eflags = evt->get_info_flags(); + if(eflags & EF_WRITES_TO_FD) + { + sinsp_fdinfo_t* fdinfo = evt->m_fdinfo; + + if(fdinfo != NULL) + { + if(fdinfo->m_name.find("/dev/log") != string::npos) + { + m_u32val = 1; + } + } + } + + RETURN_EXTRACT_VAR(m_u32val); + } + case TYPE_COUNT: + m_u32val = 1; + RETURN_EXTRACT_VAR(m_u32val); + case TYPE_COUNT_ERROR: + return extract_error_count(evt, len); + case TYPE_COUNT_ERROR_FILE: + { + sinsp_fdinfo_t* fdinfo = evt->m_fdinfo; + + if(fdinfo != NULL) + { + if(fdinfo->m_type == SCAP_FD_FILE || + fdinfo->m_type == SCAP_FD_FILE_V2 || + fdinfo->m_type == SCAP_FD_DIRECTORY) + { + return extract_error_count(evt, len); + } + } + else + { + uint16_t etype = evt->get_type(); + + if(etype == PPME_SYSCALL_OPEN_X || + etype == PPME_SYSCALL_CREAT_X || + etype == PPME_SYSCALL_OPENAT_X || + etype == PPME_SYSCALL_OPENAT_2_X) + { + return extract_error_count(evt, len); + } + } + + return NULL; + } + case TYPE_COUNT_ERROR_NET: + { + sinsp_fdinfo_t* fdinfo = evt->m_fdinfo; + + if(fdinfo != NULL) + { + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK || + fdinfo->m_type == SCAP_FD_IPV6_SOCK || + fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK || + fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK || + fdinfo->m_type == SCAP_FD_UNIX_SOCK) + { + return extract_error_count(evt, len); + } + } + else + { + uint16_t etype = evt->get_type(); + + if(etype == PPME_SOCKET_ACCEPT_X || + etype == PPME_SOCKET_ACCEPT_5_X || + etype == PPME_SOCKET_ACCEPT4_X || + etype == PPME_SOCKET_ACCEPT4_5_X || + etype == PPME_SOCKET_CONNECT_X) + { + return extract_error_count(evt, len); + } + } + + return NULL; + } + case TYPE_COUNT_ERROR_MEMORY: + { + if(evt->get_category() == EC_MEMORY) + { + return extract_error_count(evt, len); + } + else + { + return NULL; + } + } + case TYPE_COUNT_ERROR_OTHER: + { + sinsp_fdinfo_t* fdinfo = evt->m_fdinfo; + + if(fdinfo != NULL) + { + if(!(fdinfo->m_type == SCAP_FD_FILE || + fdinfo->m_type == SCAP_FD_FILE_V2 || + fdinfo->m_type == SCAP_FD_DIRECTORY || + fdinfo->m_type == SCAP_FD_IPV4_SOCK || + fdinfo->m_type == SCAP_FD_IPV6_SOCK || + fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK || + fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK || + fdinfo->m_type == SCAP_FD_UNIX_SOCK)) + { + return extract_error_count(evt, len); + } + } + else + { + uint16_t etype = evt->get_type(); + + if(!(etype == PPME_SYSCALL_OPEN_X || + etype == PPME_SYSCALL_CREAT_X || + etype == PPME_SYSCALL_OPENAT_X || + etype == PPME_SYSCALL_OPENAT_2_X || + etype == PPME_SOCKET_ACCEPT_X || + etype == PPME_SOCKET_ACCEPT_5_X || + etype == PPME_SOCKET_ACCEPT4_X || + etype == PPME_SOCKET_ACCEPT4_5_X || + etype == PPME_SOCKET_CONNECT_X || + evt->get_category() == EC_MEMORY)) + { + return extract_error_count(evt, len); + } + } + + return NULL; + } + case TYPE_COUNT_EXIT: + if(PPME_IS_EXIT(evt->get_type())) + { + m_u32val = 1; + RETURN_EXTRACT_VAR(m_u32val); + } + else + { + return NULL; + } + case TYPE_COUNT_PROCINFO: + { + uint16_t etype = evt->get_type(); + + if(etype == PPME_PROCINFO_E) + { + sinsp_threadinfo* tinfo = evt->get_thread_info(); + + if(tinfo != NULL && tinfo->is_main_thread()) + { + m_u32val = 1; + RETURN_EXTRACT_VAR(m_u32val); + } + } + } + + break; + case TYPE_COUNT_THREADINFO: + { + uint16_t etype = evt->get_type(); + + if(etype == PPME_PROCINFO_E) + { + m_u32val = 1; + RETURN_EXTRACT_VAR(m_u32val); + } + } + + break; + case TYPE_ABSPATH: + return extract_abspath(evt, len); + case TYPE_BUFLEN_IN: + if(evt->m_fdinfo && evt->get_category() == EC_IO_READ) + { + return extract_buflen(evt, len); + } + + break; + case TYPE_BUFLEN_OUT: + if(evt->m_fdinfo && evt->get_category() == EC_IO_WRITE) + { + return extract_buflen(evt, len); + } + + break; + case TYPE_BUFLEN_FILE: + if(evt->m_fdinfo && evt->get_category() & EC_IO_BASE) + { + if(evt->m_fdinfo->m_type == SCAP_FD_FILE || evt->m_fdinfo->m_type == SCAP_FD_FILE_V2) + { + return extract_buflen(evt, len); + } + } + + break; + case TYPE_BUFLEN_FILE_IN: + if(evt->m_fdinfo && evt->get_category() == EC_IO_READ) + { + if(evt->m_fdinfo->m_type == SCAP_FD_FILE || evt->m_fdinfo->m_type == SCAP_FD_FILE_V2) + { + return extract_buflen(evt, len); + } + } + + break; + case TYPE_BUFLEN_FILE_OUT: + if(evt->m_fdinfo && evt->get_category() == EC_IO_WRITE) + { + if(evt->m_fdinfo->m_type == SCAP_FD_FILE || evt->m_fdinfo->m_type == SCAP_FD_FILE_V2) + { + return extract_buflen(evt, len); + } + } + + break; + case TYPE_BUFLEN_NET: + if(evt->m_fdinfo && evt->get_category() & EC_IO_BASE) + { + scap_fd_type etype = evt->m_fdinfo->m_type; + + if(etype >= SCAP_FD_IPV4_SOCK && etype <= SCAP_FD_IPV6_SERVSOCK) + { + return extract_buflen(evt, len); + } + } + + break; + case TYPE_BUFLEN_NET_IN: + if(evt->m_fdinfo && evt->get_category() == EC_IO_READ) + { + scap_fd_type etype = evt->m_fdinfo->m_type; + + if(etype >= SCAP_FD_IPV4_SOCK && etype <= SCAP_FD_IPV6_SERVSOCK) + { + return extract_buflen(evt, len); + } + } + + break; + case TYPE_BUFLEN_NET_OUT: + if(evt->m_fdinfo && evt->get_category() == EC_IO_WRITE) + { + scap_fd_type etype = evt->m_fdinfo->m_type; + + if(etype >= SCAP_FD_IPV4_SOCK && etype <= SCAP_FD_IPV6_SERVSOCK) + { + return extract_buflen(evt, len); + } + } + + break; + case TYPE_ISOPEN_READ: + case TYPE_ISOPEN_WRITE: + case TYPE_ISOPEN_EXEC: + { + uint16_t etype = evt->get_type(); + + m_u32val = 0; + sinsp_evt_param *parinfo; + // If any of the exec bits is on, we consider this an open+exec + uint32_t is_exec_mask = (PPM_S_IXUSR | PPM_S_IXGRP | PPM_S_IXOTH); + + if(etype == PPME_SYSCALL_OPEN_X || + etype == PPME_SYSCALL_OPENAT_E || + etype == PPME_SYSCALL_OPENAT_2_X) + { + // For both OPEN_X and OPENAT_E, + // flags is the 3rd argument. + parinfo = evt->get_param(etype == PPME_SYSCALL_OPENAT_2_X ? 3 : 2); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + uint32_t flags = *(uint32_t *)parinfo->m_val; + + // PPM open flags use 0x11 for + // PPM_O_RDWR, so there's no need to + // check that value explicitly. + if(m_field_id == TYPE_ISOPEN_READ && + flags & PPM_O_RDONLY) + { + m_u32val = 1; + } + + if(m_field_id == TYPE_ISOPEN_WRITE && + flags & PPM_O_WRONLY) + { + m_u32val = 1; + } + + if(m_field_id == TYPE_ISOPEN_EXEC && (flags & (PPM_O_TMPFILE | PPM_O_CREAT))) + { + parinfo = evt->get_param(etype == PPME_SYSCALL_OPENAT_2_X ? 4 : 3); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + uint32_t mode_bits = *(uint32_t *)parinfo->m_val; + m_u32val = (mode_bits & is_exec_mask)? 1 : 0; + } + } + else if ((m_field_id == TYPE_ISOPEN_EXEC) && (etype == PPME_SYSCALL_CREAT_X)) + { + parinfo = evt->get_param(2); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + uint32_t mode_bits = *(uint32_t *)parinfo->m_val; + m_u32val = (mode_bits & is_exec_mask)? 1 : 0; + } + + RETURN_EXTRACT_VAR(m_u32val); + } + break; + case TYPE_INFRA_DOCKER_NAME: + case TYPE_INFRA_DOCKER_CONTAINER_ID: + case TYPE_INFRA_DOCKER_CONTAINER_NAME: + case TYPE_INFRA_DOCKER_CONTAINER_IMAGE: + { + uint16_t etype = evt->m_pevt->type; + + if(etype == PPME_INFRASTRUCTURE_EVENT_E) + { + sinsp_evt_param* parinfo = evt->get_param(2); + char* descstr = (char*)parinfo->m_val; + vector elements = sinsp_split(descstr, ';'); + for(string ute : elements) + { + string e = trim(ute); + + if(m_field_id == TYPE_INFRA_DOCKER_NAME) + { + if(e.substr(0, sizeof("Event") - 1) == "Event") + { + vector subelements = sinsp_split(e, ':'); + ASSERT(subelements.size() == 2); + m_strstorage = trim(subelements[1]); + RETURN_EXTRACT_STRING(m_strstorage); + } + } + else if(m_field_id == TYPE_INFRA_DOCKER_CONTAINER_ID) + { + if(e.substr(0, sizeof("ID") - 1) == "ID") + { + vector subelements = sinsp_split(e, ':'); + ASSERT(subelements.size() == 2); + m_strstorage = trim(subelements[1]); + if(m_strstorage.length() > 12) + { + m_strstorage = m_strstorage.substr(0, 12); + } + RETURN_EXTRACT_STRING(m_strstorage); + } + } + else if(m_field_id == TYPE_INFRA_DOCKER_CONTAINER_NAME) + { + if(e.substr(0, sizeof("name") - 1) == "name") + { + vector subelements = sinsp_split(e, ':'); + ASSERT(subelements.size() == 2); + m_strstorage = trim(subelements[1]); + RETURN_EXTRACT_STRING(m_strstorage); + } + } + else if(m_field_id == TYPE_INFRA_DOCKER_CONTAINER_IMAGE) + { + if(e.substr(0, sizeof("Image") - 1) == "Image") + { + vector subelements = sinsp_split(e, ':'); + ASSERT(subelements.size() == 2); + m_strstorage = subelements[1]; + + if(m_strstorage.find("@") != string::npos) + { + m_strstorage = m_strstorage.substr(0, m_strstorage.find("@")); + } + else if(m_strstorage.find("sha256") != string::npos) + { + m_strstorage = e.substr(e.find(":") + 1); + } + m_strstorage = trim(m_strstorage); + RETURN_EXTRACT_STRING(m_strstorage); + } + } + } + } + } + break; + default: + ASSERT(false); + return NULL; + } + + return NULL; +} + +bool sinsp_filter_check_event::compare(sinsp_evt *evt) +{ + bool res; + + m_is_compare = true; + + if(m_field_id == TYPE_ARGRAW) + { + uint32_t len; + bool sanitize_strings = false; + uint8_t* extracted_val = extract(evt, &len, sanitize_strings); + + if(extracted_val == NULL) + { + return false; + } + + ASSERT(m_arginfo != NULL); + + res = flt_compare(m_cmpop, + m_arginfo->type, + extracted_val); + } + else if(m_field_id == TYPE_AROUND) + { + uint64_t ts = evt->get_ts(); + uint64_t t1 = ts - m_tsdelta; + uint64_t t2 = ts + m_tsdelta; + + bool res1 = ::flt_compare(CO_GE, + PT_UINT64, + &m_u64val, + &t1); + + bool res2 = ::flt_compare(CO_LE, + PT_UINT64, + &m_u64val, + &t2); + + return res1 && res2; + } + else + { + res = sinsp_filter_check::compare(evt); + } + + m_is_compare = false; + + return res; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_user implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_user_fields[] = +{ + {PT_UINT32, EPF_NONE, PF_ID, "user.uid", "user ID."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "user.name", "user name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "user.homedir", "home directory of the user."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "user.shell", "user's shell."}, + {PT_INT32, EPF_NONE, PF_ID, "user.loginuid", "audit user id (auid)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "user.loginname", "audit user name (auid)."}, +}; + +sinsp_filter_check_user::sinsp_filter_check_user() +{ + m_info.m_name = "user"; + m_info.m_fields = sinsp_filter_check_user_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_user_fields) / sizeof(sinsp_filter_check_user_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; +} + +sinsp_filter_check* sinsp_filter_check_user::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_user(); +} + +uint8_t* sinsp_filter_check_user::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + sinsp_threadinfo* tinfo = evt->get_thread_info(); + scap_userinfo* uinfo = nullptr; + + if(tinfo == NULL) + { + return NULL; + } + + if(m_field_id != TYPE_UID && m_field_id != TYPE_LOGINUID && m_field_id != TYPE_LOGINNAME) + { + ASSERT(m_inspector != NULL); + uinfo = m_inspector->get_user(tinfo->m_uid); + ASSERT(uinfo != NULL); + if(uinfo == NULL) + { + return NULL; + } + } + + switch(m_field_id) + { + case TYPE_UID: + RETURN_EXTRACT_VAR(tinfo->m_uid); + case TYPE_NAME: + RETURN_EXTRACT_CSTR(uinfo->name); + case TYPE_HOMEDIR: + RETURN_EXTRACT_CSTR(uinfo->homedir); + case TYPE_SHELL: + RETURN_EXTRACT_CSTR(uinfo->shell); + case TYPE_LOGINUID: + RETURN_EXTRACT_VAR(tinfo->m_loginuid); + case TYPE_LOGINNAME: + ASSERT(m_inspector != NULL); + uinfo = m_inspector->get_user(tinfo->m_loginuid); + if(uinfo == NULL) + { + return NULL; + } + RETURN_EXTRACT_CSTR(uinfo->name); + default: + ASSERT(false); + break; + } + + return NULL; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_group implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_group_fields[] = +{ + {PT_UINT64, EPF_NONE, PF_ID, "group.gid", "group ID."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "group.name", "group name."}, +}; + +sinsp_filter_check_group::sinsp_filter_check_group() +{ + m_info.m_name = "group"; + m_info.m_fields = sinsp_filter_check_group_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_group_fields) / sizeof(sinsp_filter_check_group_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; +} + +sinsp_filter_check* sinsp_filter_check_group::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_group(); +} + +uint8_t* sinsp_filter_check_group::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + sinsp_threadinfo* tinfo = evt->get_thread_info(); + + if(tinfo == NULL) + { + return NULL; + } + + switch(m_field_id) + { + case TYPE_GID: + RETURN_EXTRACT_VAR(tinfo->m_gid); + case TYPE_NAME: + { + unordered_map::iterator it; + + ASSERT(m_inspector != NULL); + unordered_map* grouplist = + (unordered_map*)m_inspector->get_grouplist(); + ASSERT(grouplist->size() != 0); + + if(tinfo->m_gid == 0xffffffff) + { + return NULL; + } + + it = grouplist->find(tinfo->m_gid); + if(it == grouplist->end()) + { + ASSERT(false); + return NULL; + } + + scap_groupinfo* ginfo = it->second; + ASSERT(ginfo != NULL); + + RETURN_EXTRACT_CSTR(ginfo->name); + } + default: + ASSERT(false); + break; + } + + return NULL; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_tracer implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_tracer_fields[] = +{ + {PT_INT64, EPF_NONE, PF_ID, "span.id", "ID of the span. This is a unique identifier that is used to match the enter and exit tracer events for this span. It can also be used to match different spans belonging to a trace."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "span.time", "time of the span's enter tracer as a human readable string that includes the nanosecond part."}, + {PT_UINT32, EPF_NONE, PF_DEC, "span.ntags", "number of tags that this span has."}, + {PT_UINT32, EPF_NONE, PF_DEC, "span.nargs", "number of arguments that this span has."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "span.tags", "dot-separated list of all of the span's tags."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "span.tag", "one of the span's tags, specified by 0-based offset, e.g. 'span.tag[1]'. You can use a negative offset to pick elements from the end of the tag list. For example, 'span.tag[-1]' returns the last tag."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "span.args", "comma-separated list of the span's arguments." }, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "span.arg", "one of the span arguments, specified by name or by 0-based offset. E.g. 'span.arg.xxx' or 'span.arg[1]'. You can use a negative offset to pick elements from the end of the tag list. For example, 'span.arg[-1]' returns the last argument." }, + {PT_CHARBUF, EPF_NONE, PF_NA, "span.enterargs", "comma-separated list of the span's enter tracer event arguments. For enter tracers, this is the same as evt.args. For exit tracers, this is the evt.args of the corresponding enter tracer." }, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "span.enterarg", "one of the span's enter arguments, specified by name or by 0-based offset. For enter tracer events, this is the same as evt.arg. For exit tracer events, this is the evt.arg of the corresponding enter event." }, + {PT_RELTIME, EPF_NONE, PF_DEC, "span.duration", "delta between this span's exit tracer event and the enter tracer event."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "span.duration.quantized", "10-base log of the delta between an exit tracer event and the correspondent enter event."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "span.duration.human", "delta between this span's exit tracer event and the enter event, as a human readable string (e.g. 10.3ms)."}, + {PT_RELTIME, (filtercheck_field_flags) (EPF_TABLE_ONLY | EPF_REQUIRES_ARGUMENT), PF_DEC, "span.duration.fortag", "duration of the span if the number of tags matches the field argument, otherwise 0. For example, span.duration.fortag[1] returns the duration of all the spans with 1 tag, and zero for all the other ones."}, + {PT_UINT64, EPF_TABLE_ONLY, PF_DEC, "span.count", "1 for span exit events."}, + {PT_UINT64, (filtercheck_field_flags) (EPF_TABLE_ONLY | EPF_REQUIRES_ARGUMENT), PF_DEC, "span.count.fortag", "1 if the span's number of tags matches the field argument, and zero for all the other ones."}, + {PT_UINT64, (filtercheck_field_flags) (EPF_TABLE_ONLY | EPF_REQUIRES_ARGUMENT), PF_DEC, "span.childcount.fortag", "1 if the span's number of tags is greater than the field argument, and zero for all the other ones."}, + {PT_CHARBUF, (filtercheck_field_flags) (EPF_TABLE_ONLY | EPF_REQUIRES_ARGUMENT), PF_NA, "span.idtag", "id used by the span list csysdig view."}, + {PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "span.rawtime", "id used by the span list csysdig view."}, + {PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "span.rawparenttime", "id used by the span list csysdig view."}, +}; + +sinsp_filter_check_tracer::sinsp_filter_check_tracer() +{ + m_storage = NULL; + m_info.m_name = "span"; + m_info.m_fields = sinsp_filter_check_tracer_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_tracer_fields) / sizeof(sinsp_filter_check_tracer_fields[0]); + m_converter = new sinsp_filter_check_reference(); + + m_storage_size = UESTORAGE_INITIAL_BUFSIZE; + m_storage = (char*)malloc(m_storage_size); + if(m_storage == NULL) + { + throw sinsp_exception("memory allocation error in sinsp_filter_check_tracer::sinsp_filter_check_tracer"); + } + + m_cargname = NULL; +} + +sinsp_filter_check_tracer::~sinsp_filter_check_tracer() +{ + if(m_converter != NULL) + { + delete m_converter; + } + + if(m_storage != NULL) + { + free(m_storage); + } +} + +sinsp_filter_check* sinsp_filter_check_tracer::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_tracer(); +} + +int32_t sinsp_filter_check_tracer::extract_arg(string fldname, string val, OUT const struct ppm_param_info** parinfo) +{ + uint32_t parsed_len = 0; + + // + // 'arg' and 'resarg' are handled in a custom way + // + if(val[fldname.size()] == '[') + { + if(parinfo != NULL) + { + throw sinsp_exception("tracer field must be expressed explicitly"); + } + + parsed_len = (uint32_t)val.find(']'); + string numstr = val.substr(fldname.size() + 1, parsed_len - fldname.size() - 1); + m_argid = sinsp_numparser::parsed32(numstr); + parsed_len++; + } + else if(val[fldname.size()] == '.') + { + if(fldname == "span.tag") + { + throw sinsp_exception("invalid syntax for span.tag"); + } + else if(fldname == "span.idtag") + { + throw sinsp_exception("invalid syntax for span.idtag"); + } + + m_argname = val.substr(fldname.size() + 1); + m_cargname = m_argname.c_str(); + parsed_len = (uint32_t)(fldname.size() + m_argname.size() + 1); + m_argid = TEXT_ARG_ID; + } + else + { + throw sinsp_exception("filter syntax error: " + val); + } + + return parsed_len; +} + +int32_t sinsp_filter_check_tracer::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + int32_t res; + string val(str); + + // + // A couple of fields are handled in a custom way + // + if(string(val, 0, sizeof("span.tag") - 1) == "span.tag" && + string(val, 0, sizeof("span.tags") - 1) != "span.tags") + { + m_field_id = TYPE_TAG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("span.tag", val, NULL); + } + else if(string(val, 0, sizeof("span.arg") - 1) == "span.arg" && + string(val, 0, sizeof("span.args") - 1) != "span.args") + { + m_field_id = TYPE_ARG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("span.arg", val, NULL); + } + else if(string(val, 0, sizeof("span.enterarg") - 1) == "span.enterarg" && + string(val, 0, sizeof("span.enterargs") - 1) != "span.enterargs") + { + m_field_id = TYPE_ENTERARG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("span.enterarg", val, NULL); + } + else if(string(val, 0, sizeof("span.duration.fortag") - 1) == "span.duration.fortag") + { + m_field_id = TYPE_TAGDURATION; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("span.duration.fortag", val, NULL); + } + else if(string(val, 0, sizeof("span.count.fortag") - 1) == "span.count.fortag") + { + m_field_id = TYPE_TAGCOUNT; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("span.count.fortag", val, NULL); + } + else if(string(val, 0, sizeof("span.childcount.fortag") - 1) == "span.childcount.fortag") + { + m_field_id = TYPE_TAGCHILDSCOUNT; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("span.childcount.fortag", val, NULL); + } + else if(string(val, 0, sizeof("span.idtag") - 1) == "span.idtag") + { + m_field_id = TYPE_IDTAG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("span.idtag", val, NULL); + } + else + { + res = sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } + + if(m_field_id == TYPE_DURATION || + m_field_id == TYPE_DURATION_QUANTIZED || + m_field_id == TYPE_DURATION_HUMAN || + m_field_id == TYPE_TAGDURATION || + m_field_id == TYPE_ARG || + m_field_id == TYPE_ARGS || + m_field_id == TYPE_ENTERARG || + m_field_id == TYPE_ENTERARGS || + m_field_id == TYPE_IDTAG || + m_field_id == TYPE_TIME || + m_field_id == TYPE_RAWTIME || + m_field_id == TYPE_RAWPARENTTIME + ) + { + m_inspector->request_tracer_state_tracking(); + m_needs_state_tracking = true; + } + + return res; +} + +uint8_t* sinsp_filter_check_tracer::extract_duration(uint16_t etype, sinsp_tracerparser* eparser, OUT uint32_t* len) +{ + if(etype == PPME_TRACER_X) + { + sinsp_partial_tracer* pae = eparser->m_enter_pae; + if(pae == NULL) + { + return NULL; + } + + m_s64val = eparser->m_exit_pae.m_time - pae->m_time; + if(m_s64val < 0) + { + ASSERT(false); + m_s64val = 0; + } + + RETURN_EXTRACT_VAR(m_s64val); + } + else + { + return NULL; + } +} + +uint8_t* sinsp_filter_check_tracer::extract_args(sinsp_partial_tracer* pae, OUT uint32_t* len) +{ + if(pae == NULL) + { + return NULL; + } + + vector::iterator nameit; + vector::iterator valit; + vector::iterator namesit; + vector::iterator valsit; + + uint32_t nargs = (uint32_t)pae->m_argnames.size(); + uint32_t encoded_args_len = pae->m_argnames_len + pae->m_argvals_len + + nargs + nargs + 2; + + if(m_storage_size < encoded_args_len) + { + m_storage = (char*)realloc(m_storage, encoded_args_len); + m_storage_size = encoded_args_len; + } + + char* p = m_storage; + + for(nameit = pae->m_argnames.begin(), valit = pae->m_argvals.begin(), + namesit = pae->m_argnamelens.begin(), valsit = pae->m_argvallens.begin(); + nameit != pae->m_argnames.end(); + ++nameit, ++namesit, ++valit, ++valsit) + { + strcpy(p, *nameit); + p += (*namesit); + *p++ = '='; + + memcpy(p, *valit, (*valsit)); + p += (*valsit); + *p++ = ','; + } + + if(p != m_storage) + { + *--p = 0; + } + else + { + *p = 0; + } + + RETURN_EXTRACT_CSTR(m_storage); +} + +uint8_t* sinsp_filter_check_tracer::extract_arg(sinsp_partial_tracer* pae, OUT uint32_t* len) +{ + char* res = NULL; + + if(pae == NULL) + { + return NULL; + } + + if(m_argid == TEXT_ARG_ID) + { + // + // Argument expressed as name, e.g. span.arg.name. + // Scan the argname list and find the match. + // + uint32_t j; + + for(j = 0; j < pae->m_nargs; j++) + { + if(strcmp(m_cargname, pae->m_argnames[j]) == 0) + { + res = pae->m_argvals[j]; + break; + } + } + } + else + { + // + // Argument expressed as id, e.g. span.arg[1]. + // Pick the corresponding value. + // + if(m_argid >= 0) + { + if(m_argid < (int32_t)pae->m_nargs) + { + res = pae->m_argvals[m_argid]; + } + } + else + { + int32_t id = (int32_t)pae->m_nargs + m_argid; + + if(id >= 0) + { + res = pae->m_argvals[id]; + } + } + } + + if (res) + { + *len = strlen(res); + } + return (uint8_t*)res; +} + +uint8_t* sinsp_filter_check_tracer::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + sinsp_tracerparser* eparser; + sinsp_threadinfo* tinfo = evt->get_thread_info(); + uint16_t etype = evt->get_type(); + + if(etype != PPME_TRACER_E && etype != PPME_TRACER_X) + { + return NULL; + } + + if(tinfo == NULL) + { + return NULL; + } + + eparser = tinfo->m_tracer_parser; + if(eparser == NULL) + { + return NULL; + } + else + { + if(m_needs_state_tracking && eparser->m_enter_pae == NULL) + { + return NULL; + } + } + + switch(m_field_id) + { + case TYPE_ID: + RETURN_EXTRACT_VAR(eparser->m_id); + case TYPE_TIME: + { + sinsp_utils::ts_to_string(evt->get_ts(), &m_strstorage, false, true); + RETURN_EXTRACT_STRING(m_strstorage); + } + case TYPE_NTAGS: + m_u32val = (uint32_t)eparser->m_tags.size(); + RETURN_EXTRACT_VAR(m_u32val); + case TYPE_NARGS: + { + sinsp_partial_tracer* pae = eparser->m_enter_pae; + if(pae == NULL) + { + return NULL; + } + + m_u32val = (uint32_t)pae->m_argvals.size(); + RETURN_EXTRACT_VAR(m_u32val); + } + case TYPE_TAGS: + { + vector::iterator it; + vector::iterator sit; + + uint32_t ntags = (uint32_t)eparser->m_tags.size(); + uint32_t encoded_tags_len = eparser->m_tot_taglens + ntags + 1; + + if(m_storage_size < encoded_tags_len) + { + m_storage = (char*)realloc(m_storage, encoded_tags_len); + m_storage_size = encoded_tags_len; + } + + char* p = m_storage; + + for(it = eparser->m_tags.begin(), sit = eparser->m_taglens.begin(); + it != eparser->m_tags.end(); ++it, ++sit) + { + memcpy(p, *it, (*sit)); + p += (*sit); + *p++ = '.'; + } + + if(p != m_storage) + { + *--p = 0; + } + else + { + *p = 0; + } + + RETURN_EXTRACT_CSTR(m_storage); + } + case TYPE_TAG: + { + char* res = NULL; + + if(m_argid >= 0) + { + if(m_argid < (int32_t)eparser->m_tags.size()) + { + res = eparser->m_tags[m_argid]; + } + } + else + { + int32_t id = (int32_t)eparser->m_tags.size() + m_argid; + + if(id >= 0) + { + res = eparser->m_tags[id]; + } + } + + RETURN_EXTRACT_CSTR(res); + } + case TYPE_IDTAG: + { + m_strstorage = to_string(eparser->m_id); + + if(m_argid >= 0) + { + if(m_argid < (int32_t)eparser->m_tags.size()) + { + m_strstorage += eparser->m_tags[m_argid]; + } + } + else + { + int32_t id = (int32_t)eparser->m_tags.size() + m_argid; + + if(id >= 0) + { + m_strstorage += eparser->m_tags[id]; + } + } + + RETURN_EXTRACT_STRING(m_strstorage); + } + case TYPE_ARGS: + if(PPME_IS_ENTER(etype)) + { + return extract_args(eparser->m_enter_pae, len); + } + else + { + return extract_args(&eparser->m_exit_pae, len); + } + case TYPE_ARG: + if(PPME_IS_ENTER(etype)) + { + return extract_arg(eparser->m_enter_pae, len); + } + else + { + return extract_arg(&eparser->m_exit_pae, len); + } + case TYPE_ENTERARGS: + return extract_args(eparser->m_enter_pae, len); + case TYPE_ENTERARG: + return extract_arg(eparser->m_enter_pae, len); + case TYPE_DURATION: + return (uint8_t*)extract_duration(etype, eparser, len); + case TYPE_DURATION_HUMAN: + { + if(extract_duration(etype, eparser, len) == NULL) + { + return NULL; + } + else + { + m_converter->set_val(PT_RELTIME, + (uint8_t*)&m_s64val, + 8, + 0, + ppm_print_format::PF_DEC); + + m_strstorage = m_converter->tostring_nice(NULL, 0, 1000000000); + } + + RETURN_EXTRACT_STRING(m_strstorage); + } + case TYPE_DURATION_QUANTIZED: + { + if(extract_duration(etype, eparser, len) == NULL) + { + return NULL; + } + else + { + uint64_t lat = m_s64val; + if(lat != 0) + { + double lduration = log10((double)lat); + + if(lduration > 11) + { + lduration = 11; + } + + m_s64val = (uint64_t)(lduration * g_csysdig_screen_w / 11) + 1; + + RETURN_EXTRACT_VAR(m_s64val); + } + } + + return NULL; + } + case TYPE_TAGDURATION: + if((int32_t)eparser->m_tags.size() - 1 == m_argid) + { + return (uint8_t*)extract_duration(etype, eparser, len); + } + else + { + return NULL; + } + case TYPE_COUNT: + if(evt->get_type() == PPME_TRACER_X) + { + m_s64val = 1; + } + else + { + m_s64val = 0; + } + + RETURN_EXTRACT_VAR(m_s64val); + case TYPE_TAGCOUNT: + if(PPME_IS_EXIT(evt->get_type()) && (int32_t)eparser->m_tags.size() - 1 == m_argid) + { + m_s64val = 1; + } + else + { + m_s64val = 0; + } + + RETURN_EXTRACT_VAR(m_s64val); + case TYPE_TAGCHILDSCOUNT: + if(PPME_IS_EXIT(evt->get_type()) && (int32_t)eparser->m_tags.size() > m_argid + 1) + { + m_s64val = 1; + } + else + { + m_s64val = 0; + } + + RETURN_EXTRACT_VAR(m_s64val); + case TYPE_RAWTIME: + { + m_strstorage = to_string(eparser->m_enter_pae->m_time); + RETURN_EXTRACT_STRING(m_strstorage); + } + case TYPE_RAWPARENTTIME: + { + sinsp_partial_tracer* pepae = eparser->find_parent_enter_pae(); + + if(pepae == NULL) + { + return NULL; + } + + m_strstorage = to_string(pepae->m_time); + RETURN_EXTRACT_STRING(m_strstorage); + } + default: + ASSERT(false); + break; + } + + return NULL; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_tracer implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_evtin_fields[] = +{ + { PT_INT64, EPF_NONE, PF_ID, "evtin.span.id", "accepts all the events that are between the enter and exit tracers of the spans with the given ID and are generated by the same thread that generated the tracers." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.ntags", "accepts all the events that are between the enter and exit tracers of the spans with the given number of tags and are generated by the same thread that generated the tracers." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.nargs", "accepts all the events that are between the enter and exit tracers of the spans with the given number of arguments and are generated by the same thread that generated the tracers." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.tags", "accepts all the events that are between the enter and exit tracers of the spans with the given tags and are generated by the same thread that generated the tracers." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.tag", "accepts all the events that are between the enter and exit tracers of the spans with the given tag and are generated by the same thread that generated the tracers. See the description of span.tag for information about the syntax accepted by this field." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.args", "accepts all the events that are between the enter and exit tracers of the spans with the given arguments and are generated by the same thread that generated the tracers." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.arg", "accepts all the events that are between the enter and exit tracers of the spans with the given argument and are generated by the same thread that generated the tracers. See the description of span.arg for information about the syntax accepted by this field." }, + { PT_INT64, EPF_NONE, PF_ID, "evtin.span.p.id", "same as evtin.span.id, but also accepts events generated by other threads in the same process that produced the span." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.p.ntags", "same as evtin.span.ntags, but also accepts events generated by other threads in the same process that produced the span." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.p.nargs", "same as evtin.span.nargs, but also accepts events generated by other threads in the same process that produced the span." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.p.tags", "same as evtin.span.tags, but also accepts events generated by other threads in the same process that produced the span." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.p.tag", "same as evtin.span.tag, but also accepts events generated by other threads in the same process that produced the span." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.p.args", "same as evtin.span.args, but also accepts events generated by other threads in the same process that produced the span." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.p.arg", "same as evtin.span.arg, but also accepts events generated by other threads in the same process that produced the span." }, + { PT_INT64, EPF_NONE, PF_ID, "evtin.span.s.id", "same as evtin.span.id, but also accepts events generated by the script that produced the span, i.e. by the processes whose parent PID is the same as the one of the process generating the span." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.s.ntags", "same as evtin.span.id, but also accepts events generated by the script that produced the span, i.e. by the processes whose parent PID is the same as the one of the process generating the span." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.s.nargs", "same as evtin.span.id, but also accepts events generated by the script that produced the span, i.e. by the processes whose parent PID is the same as the one of the process generating the span." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.s.tags", "same as evtin.span.id, but also accepts events generated by the script that produced the span, i.e. by the processes whose parent PID is the same as the one of the process generating the span." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.s.tag", "same as evtin.span.id, but also accepts events generated by the script that produced the span, i.e. by the processes whose parent PID is the same as the one of the process generating the span." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.s.args", "same as evtin.span.id, but also accepts events generated by the script that produced the span, i.e. by the processes whose parent PID is the same as the one of the process generating the span." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.s.arg", "same as evtin.span.id, but also accepts events generated by the script that produced the span, i.e. by the processes whose parent PID is the same as the one of the process generating the span." }, + { PT_INT64, EPF_NONE, PF_ID, "evtin.span.m.id", "same as evtin.span.id, but accepts all the events generated on the machine during the span, including other threads and other processes." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.m.ntags", "same as evtin.span.id, but accepts all the events generated on the machine during the span, including other threads and other processes." }, + { PT_UINT32, EPF_NONE, PF_DEC, "evtin.span.m.nargs", "same as evtin.span.id, but accepts all the events generated on the machine during the span, including other threads and other processes." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.m.tags", "same as evtin.span.id, but accepts all the events generated on the machine during the span, including other threads and other processes." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.m.tag", "same as evtin.span.id, but accepts all the events generated on the machine during the span, including other threads and other processes." }, + { PT_CHARBUF, EPF_NONE, PF_NA, "evtin.span.m.args", "same as evtin.span.id, but accepts all the events generated on the machine during the span, including other threads and other processes." }, + { PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evtin.span.m.arg", "same as evtin.span.id, but accepts all the events generated on the machine during the span, including other threads and other processes." }, +}; + +sinsp_filter_check_evtin::sinsp_filter_check_evtin() +{ + m_is_compare = false; + m_info.m_name = "evtin"; + m_info.m_fields = sinsp_filter_check_evtin_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_evtin_fields) / sizeof(sinsp_filter_check_evtin_fields[0]); + m_u64val = 0; + m_converter = new sinsp_filter_check_reference(); + + m_storage_size = UESTORAGE_INITIAL_BUFSIZE; + m_storage = (char*)malloc(m_storage_size); + if(m_storage == NULL) + { + throw sinsp_exception("memory allocation error in sinsp_filter_check_appevt::sinsp_filter_check_evtin"); + } + + m_cargname = NULL; +} + +sinsp_filter_check_evtin::~sinsp_filter_check_evtin() +{ + if(m_storage != NULL) + { + free(m_storage); + } + + if(m_converter != NULL) + { + delete m_converter; + } +} + +int32_t sinsp_filter_check_evtin::extract_arg(string fldname, string val) +{ + uint32_t parsed_len = 0; + + // + // 'arg' and 'resarg' are handled in a custom way + // + if(val[fldname.size()] == '[') + { + parsed_len = (uint32_t)val.find(']'); + string numstr = val.substr(fldname.size() + 1, parsed_len - fldname.size() - 1); + + m_argid = sinsp_numparser::parsed32(numstr); + + parsed_len++; + } + else if(val[fldname.size()] == '.') + { + const struct ppm_param_info* pi = + sinsp_utils::find_longest_matching_evt_param(val.substr(fldname.size() + 1)); + + if(pi == NULL) + { + throw sinsp_exception("unknown event argument " + val.substr(fldname.size() + 1)); + } + + m_argname = pi->name; + parsed_len = (uint32_t)(fldname.size() + strlen(pi->name) + 1); + m_argid = -1; + } + else + { + throw sinsp_exception("filter syntax error: " + val); + } + + return parsed_len; +} + +int32_t sinsp_filter_check_evtin::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + int32_t res; + string val(str); + + // + // All of the fields require state tracking + // + m_inspector->request_tracer_state_tracking(); + + // + // A couple of fields are handled in a custom way + // + if(string(val, 0, sizeof("evtin.span.tag") - 1) == "evtin.span.tag" && + string(val, 0, sizeof("evtin.span.tags") - 1) != "evtin.span.tags") + { + m_field_id = TYPE_TAG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.tag", val); + } + else if(string(val, 0, sizeof("evtin.span.arg") - 1) == "evtin.span.arg" && + string(val, 0, sizeof("evtin.span.args") - 1) != "evtin.span.args") + { + m_field_id = TYPE_ARG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.arg", val); + } + else if(string(val, 0, sizeof("evtin.span.p.tag") - 1) == "evtin.span.p.tag" && + string(val, 0, sizeof("evtin.span.p.tags") - 1) != "evtin.span.p.tags") + { + m_field_id = TYPE_P_TAG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.p.tag", val); + } + else if(string(val, 0, sizeof("evtin.span.p.arg") - 1) == "evtin.span.p.arg" && + string(val, 0, sizeof("evtin.span.p.args") - 1) != "evtin.span.p.args") + { + m_field_id = TYPE_P_ARG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.p.arg", val); + } + else if(string(val, 0, sizeof("evtin.span.s.tag") - 1) == "evtin.span.s.tag" && + string(val, 0, sizeof("evtin.span.s.tags") - 1) != "evtin.span.s.tags") + { + m_field_id = TYPE_S_TAG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.s.tag", val); + } + else if(string(val, 0, sizeof("evtin.span.s.arg") - 1) == "evtin.span.s.arg" && + string(val, 0, sizeof("evtin.span.s.args") - 1) != "evtin.span.s.args") + { + m_field_id = TYPE_S_ARG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.s.arg", val); + } + else if(string(val, 0, sizeof("evtin.span.m.tag") - 1) == "evtin.span.m.tag" && + string(val, 0, sizeof("evtin.span.m.tags") - 1) != "evtin.span.m.tags") + { + m_field_id = TYPE_M_TAG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.m.tag", val); + } + else if(string(val, 0, sizeof("evtin.span.m.arg") - 1) == "evtin.span.m.arg" && + string(val, 0, sizeof("evtin.span.m.args") - 1) != "evtin.span.m.args") + { + m_field_id = TYPE_M_ARG; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg("evtin.span.m.arg", val); + } + else + { + res = sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } + + return res; +} + +sinsp_filter_check* sinsp_filter_check_evtin::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_evtin(); +} + +inline uint8_t* sinsp_filter_check_evtin::extract_tracer(sinsp_evt *evt, sinsp_partial_tracer* pae, OUT uint32_t* len) +{ + ASSERT(pae); + uint32_t field_id = m_field_id; + + if(field_id >= TYPE_ID && field_id <= TYPE_ARG) + { + // + // If this is a thread-related field, reject anything that doesn't come from the same thread + // + if(static_cast(pae->m_tid) != evt->get_thread_info()->m_tid) + { + return NULL; + } + } + else if(field_id >= TYPE_P_ID && field_id <= TYPE_P_ARG) + { + // + // If this is a *.p.* field, reject anything that doesn't come from the same process + // + sinsp_threadinfo* tinfo = m_inspector->get_thread(pae->m_tid); + + if(tinfo) + { + if(tinfo->m_tid != evt->get_thread_info()->m_tid) + { + return NULL; + } + } + else + { + return NULL; + } + + field_id -= TYPE_P_ID; + } + else if(field_id >= TYPE_S_ID && field_id <= TYPE_S_ARG) + { + // + // If this is a *.p.* field, reject anything that doesn't share the same parent + // + sinsp_threadinfo* tinfo = m_inspector->get_thread(pae->m_tid); + + if(tinfo) + { + if(tinfo->m_pid != evt->get_thread_info()->m_ptid) + { + return NULL; + } + } + else + { + return NULL; + } + + field_id -= TYPE_S_ID; + } + else + { + field_id -= TYPE_M_ID; + } + + switch(field_id) + { + case TYPE_ID: + RETURN_EXTRACT_VAR(pae->m_id); + case TYPE_NTAGS: + m_u32val = (uint32_t)pae->m_tags.size(); + RETURN_EXTRACT_VAR(m_u32val); + case TYPE_NARGS: + m_u32val = (uint32_t)pae->m_argvals.size(); + RETURN_EXTRACT_VAR(m_u32val); + case TYPE_TAGS: + { + vector::iterator it; + vector::iterator sit; + + uint32_t encoded_tags_len = pae->m_tags_len + pae->m_ntags + 1; + + if(m_storage_size < encoded_tags_len) + { + m_storage = (char*)realloc(m_storage, encoded_tags_len); + m_storage_size = encoded_tags_len; + } + + char* p = m_storage; + + for(it = pae->m_tags.begin(), sit = pae->m_taglens.begin(); + it != pae->m_tags.end(); ++it, ++sit) + { + memcpy(p, *it, (*sit)); + p += (*sit); + *p++ = '.'; + } + + if(p != m_storage) + { + *--p = 0; + } + else + { + *p = 0; + } + + RETURN_EXTRACT_CSTR(m_storage); + } + case TYPE_TAG: + { + char* val = NULL; + + if(m_argid >= 0) + { + if(m_argid < (int32_t)pae->m_ntags) + { + val = pae->m_tags[m_argid]; + } + } + else + { + int32_t id = (int32_t)pae->m_ntags + m_argid; + + if(id >= 0) + { + val = pae->m_tags[id]; + } + } + + RETURN_EXTRACT_CSTR(val); + } + case TYPE_ARGS: + { + vector::iterator nameit; + vector::iterator valit; + vector::iterator namesit; + vector::iterator valsit; + + uint32_t nargs = (uint32_t)pae->m_argnames.size(); + uint32_t encoded_args_len = pae->m_argnames_len + pae->m_argvals_len + + nargs + nargs + 2; + + if(m_storage_size < encoded_args_len) + { + m_storage = (char*)realloc(m_storage, encoded_args_len); + m_storage_size = encoded_args_len; + } + + char* p = m_storage; + + for(nameit = pae->m_argnames.begin(), valit = pae->m_argvals.begin(), + namesit = pae->m_argnamelens.begin(), valsit = pae->m_argvallens.begin(); + nameit != pae->m_argnames.end(); + ++nameit, ++namesit, ++valit, ++valsit) + { + strcpy(p, *nameit); + p += (*namesit); + *p++ = ':'; + + memcpy(p, *valit, (*valsit)); + p += (*valsit); + *p++ = ','; + } + + if(p != m_storage) + { + *--p = 0; + } + else + { + *p = 0; + } + + RETURN_EXTRACT_CSTR(m_storage); + } + case TYPE_ARG: + { + char* val = NULL; + + if(m_argid == TEXT_ARG_ID) + { + // + // Argument expressed as name, e.g. evtin.span.arg.name. + // Scan the argname list and find the match. + // + uint32_t j; + + for(j = 0; j < pae->m_nargs; j++) + { + if(strcmp(m_cargname, pae->m_argnames[j]) == 0) + { + val = pae->m_argvals[j]; + break; + } + } + } + else + { + // + // Argument expressed as id, e.g. evtin.span.arg[1]. + // Pick the corresponding value. + // + if(m_argid >= 0) + { + if(m_argid < (int32_t)pae->m_nargs) + { + val = pae->m_argvals[m_argid]; + } + } + else + { + int32_t id = (int32_t)pae->m_nargs + m_argid; + + if(id >= 0) + { + val = pae->m_argvals[id]; + } + } + } + + RETURN_EXTRACT_CSTR(val); + } + default: + ASSERT(false); + break; + } + + return NULL; +} + +uint8_t* sinsp_filter_check_evtin::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + list* partial_tracers_list = &m_inspector->m_partial_tracers_list; + list::iterator it; + uint16_t etype = evt->get_type(); + + // + // Tracer events are excluded + // + if(etype == PPME_TRACER_E || etype == PPME_TRACER_X) + { + return NULL; + } + + // + // Events without thread information are excluded + // + sinsp_threadinfo* tinfo = evt->get_thread_info(); + if(tinfo == NULL || tinfo->m_tracer_parser == NULL) + { + return NULL; + } + + // + // Scan the list and see if there's a match + // + for(it = partial_tracers_list->begin(); it != partial_tracers_list->end(); ++it) + { + uint8_t* res = extract_tracer(evt, *it, len); + if(res != NULL) + { + return res; + } + } + + return NULL; +} + +inline bool sinsp_filter_check_evtin::compare_tracer(sinsp_evt *evt, sinsp_partial_tracer* pae) +{ + uint32_t len; + uint8_t* res = extract_tracer(evt, pae, &len); + + if(res == NULL) + { + return false; + } + + if(flt_compare(m_cmpop, m_info.m_fields[m_field_id].m_type, + res) == true) + { + return true; + } + else + { + return false; + } +} + +bool sinsp_filter_check_evtin::compare(sinsp_evt *evt) +{ + bool res; + + m_is_compare = true; + + list* partial_tracers_list = &m_inspector->m_partial_tracers_list; + list::iterator it; + uint16_t etype = evt->get_type(); + + sinsp_threadinfo* tinfo = evt->get_thread_info(); + if(tinfo == NULL) + { + res = false; + goto fcec_end; + } + + // + // Scan the list and see if there's a match + // + for(it = partial_tracers_list->begin(); it != partial_tracers_list->end(); ++it) + { + if(compare_tracer(evt, *it) == true) + { + if(etype == PPME_TRACER_E && *it == tinfo->m_tracer_parser->m_enter_pae) + { + res = false; + goto fcec_end; + } + + res = true; + goto fcec_end; + } + } + + // + // For PPME_TRACER_X events, it's possible that the pae is already returned to the pool. + // Get it from the parser. + // + if(etype == PPME_TRACER_X) + { + sinsp_tracerparser* eparser = tinfo->m_tracer_parser; + + if(eparser == NULL) + { + ASSERT(false); + res = false; + goto fcec_end; + } + + if(eparser->m_enter_pae == NULL) + { + res = false; + goto fcec_end; + } + + if(compare_tracer(evt, eparser->m_enter_pae) == true) + { + res = true; + goto fcec_end; + } + } + + res = false; + +fcec_end: + m_is_compare = false; + + return res; +} + +/////////////////////////////////////////////////////////////////////////////// +// rawstring_check implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info rawstring_check_fields[] = +{ + {PT_CHARBUF, EPF_NONE, PF_NA, "NA", "INTERNAL."}, +}; + +rawstring_check::rawstring_check(string text) +{ + m_field = rawstring_check_fields; + m_field_id = 0; + set_text(text); +} + +sinsp_filter_check* rawstring_check::allocate_new() +{ + ASSERT(false); + return NULL; +} + +void rawstring_check::set_text(string text) +{ + m_text_len = (uint32_t)text.size(); + m_text = text; +} + +int32_t rawstring_check::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + ASSERT(false); + return -1; +} + +uint8_t* rawstring_check::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = m_text_len; + return (uint8_t*)m_text.c_str(); +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_syslog implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_syslog_fields[] = +{ + {PT_CHARBUF, EPF_NONE, PF_NA, "syslog.facility.str", "facility as a string."}, + {PT_UINT32, EPF_NONE, PF_DEC, "syslog.facility", "facility as a number (0-23)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "syslog.severity.str", "severity as a string. Can have one of these values: emerg, alert, crit, err, warn, notice, info, debug"}, + {PT_UINT32, EPF_NONE, PF_DEC, "syslog.severity", "severity as a number (0-7)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "syslog.message", "message sent to syslog."}, +}; + +sinsp_filter_check_syslog::sinsp_filter_check_syslog() +{ + m_info.m_name = "syslog"; + m_info.m_fields = sinsp_filter_check_syslog_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_syslog_fields) / sizeof(sinsp_filter_check_syslog_fields[0]); + m_decoder = NULL; +} + +sinsp_filter_check* sinsp_filter_check_syslog::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_syslog(); +} + +int32_t sinsp_filter_check_syslog::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + int32_t res = sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + if(res != -1) + { + m_decoder = (sinsp_decoder_syslog*)m_inspector->require_protodecoder("syslog"); + } + + return res; +} + +uint8_t* sinsp_filter_check_syslog::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + const char *str; + ASSERT(m_decoder != NULL); + if(!m_decoder->is_data_valid()) + { + return NULL; + } + + switch(m_field_id) + { + case TYPE_FACILITY: + RETURN_EXTRACT_VAR(m_decoder->m_facility); + case TYPE_FACILITY_STR: + str = m_decoder->get_facility_str(); + RETURN_EXTRACT_CSTR(str); + case TYPE_SEVERITY: + RETURN_EXTRACT_VAR(m_decoder->m_severity); + case TYPE_SEVERITY_STR: + str = m_decoder->get_severity_str(); + RETURN_EXTRACT_CSTR(str); + case TYPE_MESSAGE: + RETURN_EXTRACT_STRING(m_decoder->m_msg); + default: + ASSERT(false); + return NULL; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_container implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_container_fields[] = +{ + {PT_CHARBUF, EPF_NONE, PF_NA, "container.id", "the container id."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.name", "the container name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.image", "the container image name (e.g. sysdig/sysdig:latest for docker, )."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.image.id", "the container image id (e.g. 6f7e2741b66b)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.type", "the container type, eg: docker or rkt"}, + {PT_BOOL, EPF_NONE, PF_NA, "container.privileged", "true for containers running as privileged, false otherwise"}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.mounts", "A space-separated list of mount information. Each item in the list has the format ::::"}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "container.mount", "Information about a single mount, specified by number (e.g. container.mount[0]) or mount source (container.mount[/usr/local]). The pathname can be a glob (container.mount[/usr/local/*]), in which case the first matching mount will be returned. The information has the format ::::. If there is no mount with the specified index or matching the provided source, returns the string \"none\" instead of a NULL value."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "container.mount.source", "the mount source, specified by number (e.g. container.mount.source[0]) or mount destination (container.mount.source[/host/lib/modules]). The pathname can be a glob."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "container.mount.dest", "the mount destination, specified by number (e.g. container.mount.dest[0]) or mount source (container.mount.dest[/lib/modules]). The pathname can be a glob."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "container.mount.mode", "the mount mode, specified by number (e.g. container.mount.mode[0]) or mount source (container.mount.mode[/usr/local]). The pathname can be a glob."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "container.mount.rdwr", "the mount rdwr value, specified by number (e.g. container.mount.rdwr[0]) or mount source (container.mount.rdwr[/usr/local]). The pathname can be a glob."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "container.mount.propagation", "the mount propagation value, specified by number (e.g. container.mount.propagation[0]) or mount source (container.mount.propagation[/usr/local]). The pathname can be a glob."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.image.repository", "the container image repository (e.g. sysdig/sysdig)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.image.tag", "the container image tag (e.g. stable, latest)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.image.digest", "the container image registry digest (e.g. sha256:d977378f890d445c15e51795296e4e5062f109ce6da83e0a355fc4ad8699d27)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.healthcheck", "The container's health check. Will be the null value (\"N/A\") if no healthcheck configured, \"NONE\" if configured but explicitly not created, and the healthcheck command line otherwise"}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.liveness_probe", "The container's liveness probe. Will be the null value (\"N/A\") if no liveness probe configured, the liveness probe command line otherwise"}, + {PT_CHARBUF, EPF_NONE, PF_NA, "container.readiness_probe", "The container's readiness probe. Will be the null value (\"N/A\") if no readiness probe configured, the readiness probe command line otherwise"} +}; + +sinsp_filter_check_container::sinsp_filter_check_container() +{ + m_info.m_name = "container"; + m_info.m_fields = sinsp_filter_check_container_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_container_fields) / sizeof(sinsp_filter_check_container_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; +} + +sinsp_filter_check* sinsp_filter_check_container::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_container(); +} + +int32_t sinsp_filter_check_container::extract_arg(const string &val, size_t basepos) +{ + size_t start = val.find_first_of('[', basepos); + if(start == string::npos) + { + throw sinsp_exception("filter syntax error: " + val); + } + + size_t end = val.find_first_of(']', start); + if(end == string::npos) + { + throw sinsp_exception("filter syntax error: " + val); + } + + string numstr = val.substr(start + 1, end-start-1); + try + { + m_argid = sinsp_numparser::parsed32(numstr); + } + catch (const sinsp_exception& e) + { + if(strstr(e.what(), "is not a valid number") == NULL) + { + throw; + } + + m_argid = -1; + m_argstr = numstr; + } + + return end+1; +} + +int32_t sinsp_filter_check_container::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + string val(str); + int32_t res = 0; + + size_t basepos = sizeof("container.mount"); + + // container.mount. fields allow for indexing by number or source/dest mount path. + if(val.find("container.mount.") == 0) + { + // Note--basepos includes the trailing null, which is + // equivalent to the trailing '.' here. + if(val.find("source", basepos) == basepos) + { + m_field_id = TYPE_CONTAINER_MOUNT_SOURCE; + } + else if(val.find("dest", basepos) == basepos) + { + m_field_id = TYPE_CONTAINER_MOUNT_DEST; + } + else if(val.find("mode", basepos) == basepos) + { + m_field_id = TYPE_CONTAINER_MOUNT_MODE; + } + else if(val.find("rdwr", basepos) == basepos) + { + m_field_id = TYPE_CONTAINER_MOUNT_RDWR; + } + else if(val.find("propagation", basepos) == basepos) + { + m_field_id = TYPE_CONTAINER_MOUNT_PROPAGATION; + } + else + { + throw sinsp_exception("filter syntax error: " + val); + } + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg(val, basepos); + } + else if (val.find("container.mount") == 0 && + val[basepos-1] != 's') + { + m_field_id = TYPE_CONTAINER_MOUNT; + m_field = &m_info.m_fields[m_field_id]; + + res = extract_arg(val, basepos-1); + } + else + { + res = sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); + } + + return res; +} + + +uint8_t* sinsp_filter_check_container::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + sinsp_threadinfo* tinfo = evt->get_thread_info(); + if(tinfo == NULL) + { + return NULL; + } + + switch(m_field_id) + { + case TYPE_CONTAINER_ID: + if(tinfo->m_container_id.empty()) + { + m_tstr = "host"; + } + else + { + m_tstr = tinfo->m_container_id; + } + + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_CONTAINER_NAME: + if(tinfo->m_container_id.empty()) + { + m_tstr = "host"; + } + else + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + if(container_info->m_name.empty()) + { + return NULL; + } + + m_tstr = container_info->m_name; + } + + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_CONTAINER_IMAGE: + if(tinfo->m_container_id.empty()) + { + return NULL; + } + else + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + if(container_info->m_image.empty()) + { + return NULL; + } + + m_tstr = container_info->m_image; + } + + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_CONTAINER_IMAGE_ID: + case TYPE_CONTAINER_IMAGE_REPOSITORY: + case TYPE_CONTAINER_IMAGE_TAG: + case TYPE_CONTAINER_IMAGE_DIGEST: + if(tinfo->m_container_id.empty()) + { + return NULL; + } + else + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + const string *field; + switch(m_field_id) + { + case TYPE_CONTAINER_IMAGE_ID: + field = &container_info->m_imageid; + break; + case TYPE_CONTAINER_IMAGE_REPOSITORY: + field = &container_info->m_imagerepo; + break; + case TYPE_CONTAINER_IMAGE_TAG: + field = &container_info->m_imagetag; + break; + case TYPE_CONTAINER_IMAGE_DIGEST: + field = &container_info->m_imagedigest; + break; + default: + break; + } + + if(field->empty()) + { + return NULL; + } + + m_tstr = *field; + } + + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_CONTAINER_TYPE: + if(tinfo->m_container_id.empty()) + { + m_tstr = "host"; + } + else + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + switch(container_info->m_type) + { + case sinsp_container_type::CT_DOCKER: + m_tstr = "docker"; + break; + case sinsp_container_type::CT_LXC: + m_tstr = "lxc"; + break; + case sinsp_container_type::CT_LIBVIRT_LXC: + m_tstr = "libvirt-lxc"; + break; + case sinsp_container_type::CT_MESOS: + m_tstr = "mesos"; + break; + case sinsp_container_type::CT_CRI: + m_tstr = "cri"; + break; + case sinsp_container_type::CT_CONTAINERD: + m_tstr = "containerd"; + break; + case sinsp_container_type::CT_CRIO: + m_tstr = "cri-o"; + break; + case sinsp_container_type::CT_RKT: + m_tstr = "rkt"; + break; + case sinsp_container_type::CT_BPM: + m_tstr = "bpm"; + break; + default: + ASSERT(false); + break; + } + } + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_CONTAINER_PRIVILEGED: + if(tinfo->m_container_id.empty()) + { + return NULL; + } + else + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + // Only return a true/false value for + // container types where we really know the + // privileged status. + if (!is_docker_compatible(container_info->m_type)) + { + return NULL; + } + + m_u32val = (container_info->m_privileged ? 1 : 0); + } + + RETURN_EXTRACT_VAR(m_u32val); + break; + case TYPE_CONTAINER_MOUNTS: + if(tinfo->m_container_id.empty()) + { + return NULL; + } + else + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + m_tstr = ""; + bool first = true; + for(auto &mntinfo : container_info->m_mounts) + { + if(first) + { + first = false; + } + else + { + m_tstr += ","; + } + + m_tstr += mntinfo.to_string(); + } + + RETURN_EXTRACT_STRING(m_tstr); + } + + break; + case TYPE_CONTAINER_MOUNT: + if(tinfo->m_container_id.empty()) + { + return NULL; + } + else + { + + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + const sinsp_container_info::container_mount_info *mntinfo; + + if(m_argid != -1) + { + mntinfo = container_info->mount_by_idx(m_argid); + } + else + { + mntinfo = container_info->mount_by_source(m_argstr); + } + + if(!mntinfo) + { + return NULL; + } + else + { + m_tstr = mntinfo->to_string(); + } + + RETURN_EXTRACT_STRING(m_tstr); + } + + break; + case TYPE_CONTAINER_MOUNT_SOURCE: + case TYPE_CONTAINER_MOUNT_DEST: + case TYPE_CONTAINER_MOUNT_MODE: + case TYPE_CONTAINER_MOUNT_RDWR: + case TYPE_CONTAINER_MOUNT_PROPAGATION: + if(tinfo->m_container_id.empty()) + { + return NULL; + } + else + { + + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + const sinsp_container_info::container_mount_info *mntinfo; + + if(m_argid != -1) + { + mntinfo = container_info->mount_by_idx(m_argid); + } + else + { + if (m_field_id == TYPE_CONTAINER_MOUNT_SOURCE) + { + mntinfo = container_info->mount_by_dest(m_argstr); + } + else + { + mntinfo = container_info->mount_by_source(m_argstr); + } + } + + if(!mntinfo) + { + return NULL; + } + + switch (m_field_id) + { + case TYPE_CONTAINER_MOUNT_SOURCE: + m_tstr = mntinfo->m_source; + break; + case TYPE_CONTAINER_MOUNT_DEST: + m_tstr = mntinfo->m_dest; + break; + case TYPE_CONTAINER_MOUNT_MODE: + m_tstr = mntinfo->m_mode; + break; + case TYPE_CONTAINER_MOUNT_RDWR: + m_tstr = (mntinfo->m_rdwr ? "true" : "false"); + break; + case TYPE_CONTAINER_MOUNT_PROPAGATION: + m_tstr = mntinfo->m_propagation; + break; + } + + RETURN_EXTRACT_STRING(m_tstr); } + break; + case TYPE_CONTAINER_HEALTHCHECK: + case TYPE_CONTAINER_LIVENESS_PROBE: + case TYPE_CONTAINER_READINESS_PROBE: + if(tinfo->m_container_id.empty()) + { + return NULL; + } + else + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info) + { + return NULL; + } + + for(auto &probe : container_info->m_health_probes) + { + if((m_field_id == TYPE_CONTAINER_HEALTHCHECK && + probe.m_probe_type == sinsp_container_info::container_health_probe::PT_HEALTHCHECK) || + (m_field_id == TYPE_CONTAINER_LIVENESS_PROBE && + probe.m_probe_type == sinsp_container_info::container_health_probe::PT_LIVENESS_PROBE) || + (m_field_id == TYPE_CONTAINER_READINESS_PROBE && + probe.m_probe_type == sinsp_container_info::container_health_probe::PT_READINESS_PROBE)) + { + m_tstr = probe.m_health_probe_exe; + + for(auto &arg : probe.m_health_probe_args) + { + m_tstr += " "; + m_tstr += arg; + } + + RETURN_EXTRACT_STRING(m_tstr); + } + } + + // If here, then the container didn't have any + // health probe matching the filtercheck + // field. + m_tstr = "NONE"; + RETURN_EXTRACT_STRING(m_tstr); + } + + default: + ASSERT(false); + break; + } + + return NULL; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_reference implementation +/////////////////////////////////////////////////////////////////////////////// +sinsp_filter_check_reference::sinsp_filter_check_reference() +{ + m_info.m_name = ""; + m_info.m_fields = &m_finfo; + m_info.m_nfields = 1; + m_info.m_flags = 0; + m_finfo.m_print_format = PF_DEC; + m_field = &m_finfo; +} + +sinsp_filter_check* sinsp_filter_check_reference::allocate_new() +{ + ASSERT(false); + return NULL; +} + +int32_t sinsp_filter_check_reference::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + ASSERT(false); + return -1; +} + +uint8_t* sinsp_filter_check_reference::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = m_len; + return m_val; +} + +// +// convert a number into a byte representation. +// E.g. 1230 becomes 1.23K +// +char* sinsp_filter_check_reference::format_bytes(double val, uint32_t str_len, bool is_int) +{ + char* pr_fmt; + + if(is_int) + { + pr_fmt = (char*)"%*.0lf%c"; + } + else + { + pr_fmt = (char*)"%*.2lf%c"; + } + + if(val > (1024LL * 1024 * 1024 * 1024 * 1024)) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + pr_fmt, str_len - 1, (val) / (1024LL * 1024 * 1024 * 1024 * 1024), 'P'); + } + else if(val > (1024LL * 1024 * 1024 * 1024)) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + pr_fmt, str_len - 1, (val) / (1024LL * 1024 * 1024 * 1024), 'T'); + } + else if(val > (1024LL * 1024 * 1024)) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + pr_fmt, str_len - 1, (val) / (1024LL * 1024 * 1024), 'G'); + } + else if(val > (1024 * 1024)) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + pr_fmt, str_len - 1, (val) / (1024 * 1024), 'M'); + } + else if(val > 1024) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + pr_fmt, str_len - 1, (val) / (1024), 'K'); + } + else + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + pr_fmt, str_len, val, 0); + } - m_fdinfo = evt->get_fd_info(); + uint32_t len = (uint32_t)strlen(m_getpropertystr_storage); - if(m_fdinfo == NULL && m_tinfo->m_lastevent_fd != -1) + if(len > str_len) + { + memmove(m_getpropertystr_storage, + m_getpropertystr_storage + len - str_len, + str_len + 1); // include trailing \0 + } + + return m_getpropertystr_storage; +} + +// +// convert a nanosecond time interval into a s.ns representation. +// E.g. 1100000000 becomes 1.1s +// +#define ONE_MILLISECOND_IN_NS 1000000 +#define ONE_MICROSECOND_IN_NS 1000 + +char* sinsp_filter_check_reference::format_time(uint64_t val, uint32_t str_len) +{ + if(val >= ONE_SECOND_IN_NS) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%u.%02us", (unsigned int)(val / ONE_SECOND_IN_NS), (unsigned int)((val % ONE_SECOND_IN_NS) / 10000000)); + } + else if(val >= ONE_SECOND_IN_NS / 100) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%ums", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000))); + } + else if(val >= ONE_SECOND_IN_NS / 1000) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%u.%02ums", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000)), (unsigned int)((val % ONE_MILLISECOND_IN_NS) / 10000)); + } + else if(val >= ONE_SECOND_IN_NS / 100000) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%uus", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000000))); + } + else if(val >= ONE_SECOND_IN_NS / 1000000) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%u.%02uus", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000000)), (unsigned int)((val % ONE_MICROSECOND_IN_NS) / 10)); + } + else + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%uns", (unsigned int)val); + } + + uint32_t reslen = (uint32_t)strlen(m_getpropertystr_storage); + if(reslen < str_len) + { + uint32_t padding_size = str_len - reslen; + + memmove(m_getpropertystr_storage + padding_size, + m_getpropertystr_storage, + str_len + 1); + + for(uint32_t j = 0; j < padding_size; j++) { - m_fdinfo = m_tinfo->get_fd(m_tinfo->m_lastevent_fd); + m_getpropertystr_storage[j] = ' '; } + } - // We'll check if fd is null below + return m_getpropertystr_storage; +} + +char* sinsp_filter_check_reference::print_double(uint8_t* rawval, uint32_t str_len) +{ + double val; + + switch(m_field->m_type) + { + case PT_INT8: + val = (double)*(int8_t*)rawval; + break; + case PT_INT16: + val = (double)*(int16_t*)rawval; + break; + case PT_INT32: + val = (double)*(int32_t*)rawval; + break; + case PT_INT64: + val = (double)*(int64_t*)rawval; + break; + case PT_UINT8: + val = (double)*(uint8_t*)rawval; + break; + case PT_UINT16: + val = (double)*(uint16_t*)rawval; + break; + case PT_UINT32: + val = (double)*(uint32_t*)rawval; + break; + case PT_UINT64: + val = (double)*(uint64_t*)rawval; + break; + default: + ASSERT(false); + val = 0; + break; + } + + if(m_cnt > 1) + { + val /= m_cnt; + } + + if(m_print_format == PF_ID) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%*lf", str_len, val); + return m_getpropertystr_storage; } else { - return false; + return format_bytes(val, str_len, false); } - return true; } -bool sinsp_filter_check_fd::compare(sinsp_evt *evt) +char* sinsp_filter_check_reference::print_int(uint8_t* rawval, uint32_t str_len) { - // - // A couple of fields are filter only and therefore get a special treatment - // - if(m_field_id == TYPE_IP) + int64_t val; + + switch(m_field->m_type) { - return compare_ip(evt); + case PT_INT8: + val = (int64_t)*(int8_t*)rawval; + break; + case PT_INT16: + val = (int64_t)*(int16_t*)rawval; + break; + case PT_INT32: + val = (int64_t)*(int32_t*)rawval; + break; + case PT_INT64: + val = (int64_t)*(int64_t*)rawval; + break; + case PT_UINT8: + val = (int64_t)*(uint8_t*)rawval; + break; + case PT_UINT16: + val = (int64_t)*(uint16_t*)rawval; + break; + case PT_UINT32: + val = (int64_t)*(uint32_t*)rawval; + break; + case PT_UINT64: + val = (int64_t)*(uint64_t*)rawval; + break; + default: + ASSERT(false); + val = 0; + break; } - else if(m_field_id == TYPE_PORT) + + if(m_cnt > 1) { - return compare_port(evt); + val /= (int64_t)m_cnt; } - // - // Standard extract-based fields - // + if(m_print_format == PF_ID) + { + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%*" PRId64, str_len, val); + return m_getpropertystr_storage; + } + else + { + return format_bytes((double)val, str_len, true); + } + +} + +char* sinsp_filter_check_reference::tostring_nice(sinsp_evt* evt, + uint32_t str_len, + uint64_t time_delta) +{ uint32_t len; - uint8_t* extracted_val = extract(evt, &len); + uint8_t* rawval = extract(evt, &len); - if(extracted_val == NULL) + if(rawval == NULL) { - return false; + return NULL; + } + + if(time_delta != 0) + { + m_cnt = (double)time_delta / ONE_SECOND_IN_NS; } - return flt_compare(m_cmpop, - m_info.m_fields[m_field_id].m_type, - extracted_val, - &m_val_storage[0]); + if(m_field->m_type >= PT_INT8 && m_field->m_type <= PT_UINT64) + { + if(m_print_format == PF_ID || m_cnt == 1 || m_cnt == 0) + { + return print_int(rawval, str_len); + } + else + { + return print_double(rawval, str_len); + } + } + else if(m_field->m_type == PT_RELTIME) + { + double val = (double)*(uint64_t*)rawval; + + if(m_cnt > 1) + { + val /= m_cnt; + } + + return format_time((int64_t)val, str_len); + } + else if(m_field->m_type == PT_DOUBLE) + { + double dval = (double)*(double*)rawval; + + if(m_cnt > 1) + { + dval /= m_cnt; + } + + snprintf(m_getpropertystr_storage, + sizeof(m_getpropertystr_storage), + "%*.2lf", str_len, dval); + return m_getpropertystr_storage; + } + else + { + return rawval_to_string(rawval, m_field->m_type, m_field->m_print_format, len); + } } -char* sinsp_filter_check_fd::tostring(sinsp_evt* evt) +Json::Value sinsp_filter_check_reference::tojson(sinsp_evt* evt, + uint32_t str_len, + uint64_t time_delta) { uint32_t len; - uint8_t* rawval = extract(evt, &len); if(rawval == NULL) { - return NULL; + return ""; + } + + if(time_delta != 0) + { + m_cnt = (double)time_delta / ONE_SECOND_IN_NS; + } + + if(m_field->m_type == PT_RELTIME) + { + double val = (double)*(uint64_t*)rawval; + + if(m_cnt > 1) + { + val /= m_cnt; + } + + return format_time((int64_t)val, str_len); } + else if(m_field->m_type == PT_DOUBLE) + { + double dval = (double)*(double*)rawval; + + if(m_cnt > 1) + { + dval /= m_cnt; + } - return rawval_to_string(rawval, m_field, len); + return dval; + } + else + { + return rawval_to_json(rawval, m_field->m_type, m_field->m_print_format, len); + } } /////////////////////////////////////////////////////////////////////////////// -// sinsp_filter_check_thread implementation +// sinsp_filter_check_utils implementation /////////////////////////////////////////////////////////////////////////////// -const filtercheck_field_info sinsp_filter_check_thread_fields[] = +const filtercheck_field_info sinsp_filter_check_utils_fields[] = { - {PT_INT64, EPF_NONE, PF_DEC, "proc.pid", "the id of the process generating the event."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "proc.exe", "the full name (including the path) of the executable generating the event."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "proc.name", "the name (excluding thr path) of the executable generating the event."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "proc.args", "the arguments passed on the command line when starting the process generating the event."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "proc.cwd", "the current working directory of the event."}, - {PT_UINT32, EPF_NONE, PF_DEC, "proc.nchilds", "the number of child threads of that the process generating the event currently has."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "proc.parentname", "the name (excluding thr path) of the parent of the process generating the event."}, - {PT_INT64, EPF_NONE, PF_DEC, "thread.tid", "the id of the thread generating the event."}, - {PT_BOOL, EPF_NONE, PF_NA, "thread.ismain", "'true' if the thread generating the event is the main one in the process."}, - {PT_RELTIME, EPF_NONE, PF_DEC, "thread.exectime", "Thread execution time. Exported only by switch events."}, -// {PT_UINT64, EPF_NONE, PF_DEC, "iobytes", "I/O bytes (either read or write) generated by I/O calls like read, write, send receive..."}, -// {PT_UINT64, EPF_NONE, PF_DEC, "totiobytes", "aggregated number of I/O bytes (either read or write) since the beginning of the capture."}, -// {PT_RELTIME, EPF_NONE, PF_DEC, "latency", "number of nanoseconds spent in the last system call."}, -// {PT_RELTIME, EPF_NONE, PF_DEC, "totlatency", "aggregated number of nanoseconds spent in system calls since the beginning of the capture.."}, + {PT_UINT64, EPF_NONE, PF_ID, "util.cnt", "incremental counter."}, }; -sinsp_filter_check_thread::sinsp_filter_check_thread() +sinsp_filter_check_utils::sinsp_filter_check_utils() { - m_info.m_name = "process"; - m_info.m_fields = sinsp_filter_check_thread_fields; - m_info.m_nfiedls = sizeof(sinsp_filter_check_thread_fields) / sizeof(sinsp_filter_check_thread_fields[0]); + m_info.m_name = "util"; + m_info.m_fields = sinsp_filter_check_utils_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_utils_fields) / sizeof(sinsp_filter_check_utils_fields[0]); + m_info.m_flags = filter_check_info::FL_HIDDEN; + m_cnt = 0; +} - m_u64val = 0; +sinsp_filter_check* sinsp_filter_check_utils::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_utils(); } -sinsp_filter_check* sinsp_filter_check_thread::allocate_new() +uint8_t* sinsp_filter_check_utils::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) { - return (sinsp_filter_check*) new sinsp_filter_check_thread(); + *len = 0; + switch(m_field_id) + { + case TYPE_CNT: + m_cnt++; + RETURN_EXTRACT_VAR(m_cnt); + default: + ASSERT(false); + break; + } + + return NULL; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_fdlist implementation +/////////////////////////////////////////////////////////////////////////////// +const filtercheck_field_info sinsp_filter_check_fdlist_fields[] = +{ + {PT_CHARBUF, EPF_NONE, PF_ID, "fdlist.nums", "for poll events, this is a comma-separated list of the FD numbers in the 'fds' argument, returned as a string."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fdlist.names", "for poll events, this is a comma-separated list of the FD names in the 'fds' argument, returned as a string."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fdlist.cips", "for poll events, this is a comma-separated list of the client IP addresses in the 'fds' argument, returned as a string."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "fdlist.sips", "for poll events, this is a comma-separated list of the server IP addresses in the 'fds' argument, returned as a string."}, + {PT_CHARBUF, EPF_NONE, PF_DEC, "fdlist.cports", "for TCP/UDP FDs, for poll events, this is a comma-separated list of the client TCP/UDP ports in the 'fds' argument, returned as a string."}, + {PT_CHARBUF, EPF_NONE, PF_DEC, "fdlist.sports", "for poll events, this is a comma-separated list of the server TCP/UDP ports in the 'fds' argument, returned as a string."}, +}; + +sinsp_filter_check_fdlist::sinsp_filter_check_fdlist() +{ + m_info.m_name = "fdlist"; + m_info.m_fields = sinsp_filter_check_fdlist_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_fdlist_fields) / sizeof(sinsp_filter_check_fdlist_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; } -int32_t sinsp_filter_check_thread::parse_field_name(const char* str) +sinsp_filter_check* sinsp_filter_check_fdlist::allocate_new() { - string val(str); + return (sinsp_filter_check*) new sinsp_filter_check_fdlist(); +} - if(string(val, 0, sizeof("arg") - 1) == "arg") +uint8_t* sinsp_filter_check_fdlist::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + ASSERT(evt); + sinsp_evt_param *parinfo; + + uint16_t etype = evt->get_type(); + + if(etype == PPME_SYSCALL_POLL_E || etype == PPME_SYSCALL_PPOLL_E) { - // - // 'arg' is handled in a custom way - // - throw sinsp_exception("filter error: proc.arg filter not implemented yet"); + parinfo = evt->get_param(0); + } + else if(etype == PPME_SYSCALL_POLL_X || etype == PPME_SYSCALL_PPOLL_X) + { + parinfo = evt->get_param(1); } else { - return sinsp_filter_check::parse_field_name(str); + return NULL; } -} -uint8_t* sinsp_filter_check_thread::extract(sinsp_evt *evt, OUT uint32_t* len) -{ + uint32_t j = 0; + char* payload = parinfo->m_val; + uint16_t nfds = *(uint16_t *)payload; + uint32_t pos = 2; sinsp_threadinfo* tinfo = evt->get_thread_info(); - if(tinfo == NULL && - m_field_id != TYPE_TID && - m_field_id != TYPE_EXECTIME) - { - return NULL; - } + m_strval.clear(); - switch(m_field_id) + for(j = 0; j < nfds; j++) { - case TYPE_TID: - m_u64val = evt->get_tid(); - return (uint8_t*)&m_u64val; - case TYPE_PID: - return (uint8_t*)&tinfo->m_pid; - case TYPE_NAME: - m_tstr = tinfo->get_comm(); - return (uint8_t*)m_tstr.c_str(); - case TYPE_EXE: - m_tstr = tinfo->get_exe(); - return (uint8_t*)m_tstr.c_str(); - case TYPE_ARGS: - { - m_tstr.clear(); - - uint32_t j; - uint32_t nargs = tinfo->m_args.size(); + bool add_comma = true; + int64_t fd = *(int64_t *)(payload + pos); - for(j = 0; j < nargs; j++) - { - m_tstr += tinfo->m_args[j]; - if(j < nargs -1) - { - m_tstr += ' '; - } - } + sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd); - return (uint8_t*)m_tstr.c_str(); + switch(m_field_id) + { + case TYPE_FDNUMS: + { + m_strval += to_string(fd); } - case TYPE_CWD: - m_tstr = tinfo->get_cwd(); - return (uint8_t*)m_tstr.c_str(); - case TYPE_ISMAINTHREAD: - m_tbool = (uint32_t)tinfo->is_main_thread(); - return (uint8_t*)&m_tbool; - case TYPE_EXECTIME: + break; + case TYPE_FDNAMES: { - m_u64val = 0; - uint16_t etype = evt->get_type(); - - if(etype == PPME_SCHEDSWITCH_E || etype == PPME_SCHEDSWITCHEX_X) + if(fdinfo != NULL) { - if(m_last_proc_switch_times.size() == 0) + if(fdinfo->m_name != "") { - // - // Initialize the vector of CPU times - // - const scap_machine_info* minfo = m_inspector->get_machine_info(); - ASSERT(minfo->num_cpus != 0); - - for(uint32_t j = 0; j < minfo->num_cpus; j++) - { - m_last_proc_switch_times.push_back(0); - } + m_strval += fdinfo->m_name; } - - uint32_t cpuid = evt->get_cpuid(); - uint64_t ts = evt->get_ts(); - uint64_t lasttime = m_last_proc_switch_times[cpuid]; - - if(lasttime != 0) + else { - m_u64val = ts - lasttime; + m_strval += ""; } - - ASSERT(cpuid < m_last_proc_switch_times.size()); - - m_last_proc_switch_times[cpuid] = ts; - } - - return (uint8_t*)&m_u64val; - } - case TYPE_PARENTNAME: - { - sinsp_threadinfo* ptinfo = - m_inspector->get_thread(tinfo->m_ptid); - - if(ptinfo != NULL) - { - m_tstr = ptinfo->get_comm(); - return (uint8_t*)m_tstr.c_str(); } else { - return NULL; + m_strval += ""; } } - case IOBYTES: - case TOTIOBYTES: + break; + case TYPE_CLIENTIPS: { - // - // Extract the return value - // - uint16_t etype = evt->get_type(); - uint64_t res; - - if(etype == PPME_SYSCALL_READ_X || etype == PPME_SYSCALL_WRITE_X || etype == PPME_SOCKET_RECV_X || - etype == PPME_SOCKET_SEND_X|| etype == PPME_SOCKET_RECVFROM_X || etype == PPME_SOCKET_RECVMSG_X || - etype == PPME_SOCKET_SENDTO_X || etype == PPME_SOCKET_SENDMSG_X || etype == PPME_SYSCALL_READV_X || - etype == PPME_SYSCALL_WRITEV_X || etype == PPME_SYSCALL_PREAD_X || etype == PPME_SYSCALL_PWRITE_X || - etype == PPME_SYSCALL_PREADV_X || etype == PPME_SYSCALL_PWRITEV_X) + if(fdinfo != NULL) { - sinsp_evt_param *parinfo = evt->get_param(0); - ASSERT(parinfo->m_len == sizeof(int64_t)); - res = *(int64_t *)parinfo->m_val; + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK) + { + inet_ntop(AF_INET, &fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip, m_addrbuff, sizeof(m_addrbuff)); + m_strval += m_addrbuff; + break; + } + else if(fdinfo->m_type == SCAP_FD_IPV6_SOCK) + { + inet_ntop(AF_INET6, fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b, m_addrbuff, sizeof(m_addrbuff)); + m_strval += m_addrbuff; + break; + } } - else + + add_comma = false; + } + break; + case TYPE_SERVERIPS: + { + if(fdinfo != NULL) { - res = 0; + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK) + { + inet_ntop(AF_INET, &fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip, m_addrbuff, sizeof(m_addrbuff)); + m_strval += m_addrbuff; + break; + } + else if(fdinfo->m_type == SCAP_FD_IPV6_SOCK) + { + inet_ntop(AF_INET6, fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b, m_addrbuff, sizeof(m_addrbuff)); + m_strval += m_addrbuff; + break; + } + else if(fdinfo->m_type == SCAP_FD_IPV4_SERVSOCK) + { + inet_ntop(AF_INET, &fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip, m_addrbuff, sizeof(m_addrbuff)); + m_strval += m_addrbuff; + break; + } + else if(fdinfo->m_type == SCAP_FD_IPV6_SERVSOCK) + { + inet_ntop(AF_INET, &fdinfo->m_sockinfo.m_ipv6serverinfo.m_ip.m_b, m_addrbuff, sizeof(m_addrbuff)); + m_strval += m_addrbuff; + break; + } } - if(m_field_id == IOBYTES) + add_comma = false; + } + break; + case TYPE_CLIENTPORTS: + { + if(fdinfo != NULL) { - m_u64val = res; - - if(m_u64val != 0) + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK) { - return (uint8_t*)&m_u64val; + m_strval += to_string(fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport); + break; } - else + else if(fdinfo->m_type == SCAP_FD_IPV6_SOCK) { - return NULL; + m_strval += to_string(fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport); + break; } } - else + + add_comma = false; + } + case TYPE_SERVERPORTS: + { + if(fdinfo != NULL) { - m_u64val += res; - return (uint8_t*)&m_u64val; + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK) + { + m_strval += to_string(fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport); + break; + } + else if(fdinfo->m_type == SCAP_FD_IPV6_SOCK) + { + m_strval += to_string(fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport); + break; + } } + + add_comma = false; + } + break; + default: + ASSERT(false); } - case LATENCY: - if(tinfo->m_latency != 0) + + if(j < nfds && add_comma) { - return (uint8_t*)&tinfo->m_latency; + m_strval += ","; } - else + + pos += 10; + } + + if(m_strval.size() != 0) + { + if(m_strval.back() == ',') { - return NULL; + m_strval = m_strval.substr(0, m_strval.size() - 1); } - case TOTLATENCY: - m_u64val += tinfo->m_latency; - return (uint8_t*)&m_u64val; - default: - ASSERT(false); + + RETURN_EXTRACT_STRING(m_strval); + } + else + { return NULL; } } +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + /////////////////////////////////////////////////////////////////////////////// -// sinsp_filter_check_event implementation +// sinsp_filter_check_k8s implementation /////////////////////////////////////////////////////////////////////////////// -const filtercheck_field_info sinsp_filter_check_event_fields[] = +const filtercheck_field_info sinsp_filter_check_k8s_fields[] = { - {PT_UINT64, EPF_NONE, PF_DEC, "evt.num", "event number."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "evt.time", "event timestamp as a time string that includes the nanosecond part."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "evt.time.s", "event timestamp as a time string with no nanoseconds."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "evt.datetime", "event timestamp as a time string that inclused the date."}, - {PT_ABSTIME, EPF_NONE, PF_DEC, "evt.rawtime", "absolute event timestamp, i.e. nanoseconds from epoch."}, - {PT_ABSTIME, EPF_NONE, PF_DEC, "evt.rawtime.s", "integer part of the event timestamp (e.g. seconds since epoch)."}, - {PT_ABSTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.rawtime.ns", "fractional part of the absolute event timestamp."}, - {PT_RELTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.reltime", "number of nanoseconds from the beginning of the capture."}, - {PT_RELTIME, EPF_NONE, PF_DEC, "evt.reltime.s", "number of seconds from the beginning of the capture."}, - {PT_RELTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.reltime.ns", "fractional part (in ns) of the time from the beginning of the capture."}, - {PT_RELTIME, EPF_NONE, PF_DEC, "evt.latency", "delta between an exit event and the correspondent enter event."}, - {PT_RELTIME, EPF_NONE, PF_DEC, "evt.latency.s", "integer part of the event latency delta."}, - {PT_RELTIME, EPF_NONE, PF_10_PADDED_DEC, "evt.latency.ns", "fractional part of the event latency delta."}, - {PT_CHARBUF, EPF_PRINT_ONLY, PF_NA, "evt.dir", "event direction can be either '>' for enter events or '<' for exit events."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "evt.type", "For system call events, this is the name of the system call (e.g. 'open')."}, - {PT_INT16, EPF_NONE, PF_DEC, "evt.cpu", "number of the CPU where this event happened."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "evt.args", "all the event arguments, aggregated into a single string."}, - {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "evt.arg", "one of the event arguments specified by name or by number. Some events (e.g. return codes or FDs) will be converted into a text representation when possible. E.g. 'resarg.fd' or 'resarg[0]'."}, - {PT_DYN, EPF_REQUIRES_ARGUMENT, PF_NA, "evt.rawarg", "one of the event arguments specified by name. E.g. 'arg.fd'."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "evt.buffer", "the binary data buffer for events that have one, like read(), recvfrom(), etc. Use this field in filters with 'contains' to search into I/O data buffers."}, - {PT_CHARBUF, EPF_NONE, PF_DEC, "evt.res", "event return value, as an error code string (e.g. 'ENOENT')."}, - {PT_INT64, EPF_NONE, PF_DEC, "evt.rawres", "event return value, as a number (e.g. -2). Useful for range comparisons."}, - {PT_BOOL, EPF_NONE, PF_NA, "evt.failed", "'true' for events that returned an error status."}, - {PT_BOOL, EPF_NONE, PF_NA, "evt.is_io", "'true' for events that read or write to FDs, like read(), send, recvfrom(), etc."}, - {PT_BOOL, EPF_NONE, PF_NA, "evt.is_io_read", "'true' for events that read from FDs, like read(), recv(), recvfrom(), etc."}, - {PT_BOOL, EPF_NONE, PF_NA, "evt.is_io_write", "'true' for events that write to FDs, like write(), send(), etc."}, - {PT_BOOL, EPF_NONE, PF_NA, "evt.is_wait", "'true' for events that make the thread wait, e.g. sleep(), select(), poll()."}, - {PT_UINT32, EPF_NONE, PF_DEC, "evt.count", "This filter field always returns 1 and can be used to count events from inside chisels."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.pod.name", "Kubernetes pod name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.pod.id", "Kubernetes pod id."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "k8s.pod.label", "Kubernetes pod label. E.g. 'k8s.pod.label.foo'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.pod.labels", "Kubernetes pod comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rc.name", "Kubernetes replication controller name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rc.id", "Kubernetes replication controller id."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "k8s.rc.label", "Kubernetes replication controller label. E.g. 'k8s.rc.label.foo'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rc.labels", "Kubernetes replication controller comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.svc.name", "Kubernetes service name (can return more than one value, concatenated)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.svc.id", "Kubernetes service id (can return more than one value, concatenated)."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "k8s.svc.label", "Kubernetes service label. E.g. 'k8s.svc.label.foo' (can return more than one value, concatenated)."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.svc.labels", "Kubernetes service comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.ns.name", "Kubernetes namespace name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.ns.id", "Kubernetes namespace id."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "k8s.ns.label", "Kubernetes namespace label. E.g. 'k8s.ns.label.foo'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.ns.labels", "Kubernetes namespace comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rs.name", "Kubernetes replica set name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rs.id", "Kubernetes replica set id."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "k8s.rs.label", "Kubernetes replica set label. E.g. 'k8s.rs.label.foo'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rs.labels", "Kubernetes replica set comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.deployment.name", "Kubernetes deployment name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.deployment.id", "Kubernetes deployment id."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "k8s.deployment.label", "Kubernetes deployment label. E.g. 'k8s.rs.label.foo'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "k8s.deployment.labels", "Kubernetes deployment comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, }; -sinsp_filter_check_event::sinsp_filter_check_event() +sinsp_filter_check_k8s::sinsp_filter_check_k8s() { - m_first_ts = 0; - m_info.m_name = "evt"; - m_info.m_fields = sinsp_filter_check_event_fields; - m_info.m_nfiedls = sizeof(sinsp_filter_check_event_fields) / sizeof(sinsp_filter_check_event_fields[0]); + m_info.m_name = "k8s"; + m_info.m_fields = sinsp_filter_check_k8s_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_k8s_fields) / sizeof(sinsp_filter_check_k8s_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; } -sinsp_filter_check* sinsp_filter_check_event::allocate_new() +sinsp_filter_check* sinsp_filter_check_k8s::allocate_new() { - return (sinsp_filter_check*) new sinsp_filter_check_event(); + return (sinsp_filter_check*) new sinsp_filter_check_k8s(); } -int32_t sinsp_filter_check_event::extract_arg(string fldname, string val, OUT const struct ppm_param_info** parinfo) +int32_t sinsp_filter_check_k8s::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) { - uint32_t parsed_len = 0; + string val(str); - // - // 'arg' and 'resarg' are handled in a custom way - // - if(val[fldname.size()] == '[') + if(string(val, 0, sizeof("k8s.pod.label") - 1) == "k8s.pod.label" && + string(val, 0, sizeof("k8s.pod.labels") - 1) != "k8s.pod.labels") { - if(parinfo != NULL) - { - throw sinsp_exception("evt.arg fields must be expressed explicitly"); - } + m_field_id = TYPE_K8S_POD_LABEL; + m_field = &m_info.m_fields[m_field_id]; - parsed_len = val.find(']'); - string numstr = val.substr(fldname.size() + 1, parsed_len - fldname.size() - 1); - m_argid = sinsp_numparser::parsed32(numstr); - parsed_len++; + return extract_arg("k8s.pod.label", val); } - else if(val[fldname.size()] == '.') + else if(string(val, 0, sizeof("k8s.rc.label") - 1) == "k8s.rc.label" && + string(val, 0, sizeof("k8s.rc.labels") - 1) != "k8s.rc.labels") { - const struct ppm_param_info* pi = - sinsp_utils::find_longest_matching_evt_param(val.substr(fldname.size() + 1)); - - m_argname = pi->name; - parsed_len = fldname.size() + strlen(pi->name) + 1; - m_argid = -1; + m_field_id = TYPE_K8S_RC_LABEL; + m_field = &m_info.m_fields[m_field_id]; - if(parinfo != NULL) - { - *parinfo = pi; - } + return extract_arg("k8s.rc.label", val); } - else + else if(string(val, 0, sizeof("k8s.rs.label") - 1) == "k8s.rs.label" && + string(val, 0, sizeof("k8s.rs.labels") - 1) != "k8s.rs.labels") { - throw sinsp_exception("filter syntax error: " + val); - } - - return parsed_len; -} - -int32_t sinsp_filter_check_event::parse_field_name(const char* str) -{ - string val(str); + m_field_id = TYPE_K8S_RS_LABEL; + m_field = &m_info.m_fields[m_field_id]; - // - // A couple of fields are handled in a custom way - // - if(string(val, 0, sizeof("evt.arg") - 1) == "evt.arg" && - string(val, 0, sizeof("evt.args") - 1) != "evt.args") + return extract_arg("k8s.rs.label", val); + } + else if(string(val, 0, sizeof("k8s.svc.label") - 1) == "k8s.svc.label" && + string(val, 0, sizeof("k8s.svc.labels") - 1) != "k8s.svc.labels") { - m_field_id = TYPE_ARGSTR; + m_field_id = TYPE_K8S_SVC_LABEL; m_field = &m_info.m_fields[m_field_id]; - return extract_arg("evt.arg", val, NULL); + return extract_arg("k8s.svc.label", val); } - else if(string(val, 0, sizeof("evt.rawarg") - 1) == "evt.rawarg") + else if(string(val, 0, sizeof("k8s.ns.label") - 1) == "k8s.ns.label" && + string(val, 0, sizeof("k8s.ns.labels") - 1) != "k8s.ns.labels") { - m_field_id = TYPE_ARGRAW; - m_customfield = m_info.m_fields[m_field_id]; - - int32_t res = extract_arg("evt.rawarg", val, &m_arginfo); - - m_customfield.m_type = m_arginfo->type; + m_field_id = TYPE_K8S_NS_LABEL; + m_field = &m_info.m_fields[m_field_id]; - return res; + return extract_arg("k8s.ns.label", val); } - else if(string(val, 0, sizeof("evt.latency") - 1) == "evt.latency" || - string(val, 0, sizeof("evt.latency.s") - 1) == "evt.latency.s" || - string(val, 0, sizeof("evt.latency.ns") - 1) == "evt.latency.ns") + else if(string(val, 0, sizeof("k8s.deployment.label") - 1) == "k8s.deployment.label" && + string(val, 0, sizeof("k8s.deployment.labels") - 1) != "k8s.deployment.labels") { - // - // These fields need to store the previuos event type in the thread state - // - m_th_state_id = m_inspector->reserve_thread_memory(sizeof(uint16_t)); - return sinsp_filter_check::parse_field_name(str); + m_field_id = TYPE_K8S_DEPLOYMENT_LABEL; + m_field = &m_info.m_fields[m_field_id]; + + return extract_arg("k8s.deployment.label", val); } else { - return sinsp_filter_check::parse_field_name(str); + return sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); } } -void sinsp_filter_check_event::parse_filter_value(const char* str) +int32_t sinsp_filter_check_k8s::extract_arg(const string& fldname, const string& val) { - string val(str); + int32_t parsed_len = 0; - if(m_field_id == TYPE_ARGRAW) + if(val[fldname.size()] == '.') { - // - // 'rawarg' is handled in a custom way - // - ASSERT(m_arginfo != NULL); - return sinsp_filter_check::string_to_rawval(str, m_arginfo->type); + size_t endpos; + for(endpos = fldname.size() + 1; endpos < val.length(); ++endpos) + { + if(!isalnum(val[endpos]) + && val[endpos] != '/' + && val[endpos] != '_' + && val[endpos] != '-' + && val[endpos] != '.') + { + break; + } + } + + parsed_len = (uint32_t)endpos; + m_argname = val.substr(fldname.size() + 1, endpos - fldname.size() - 1); } else { - return sinsp_filter_check::parse_filter_value(str); + throw sinsp_exception("filter syntax error: " + val); } + + return parsed_len; } -const filtercheck_field_info* sinsp_filter_check_event::get_field_info() +#ifdef HAS_ANALYZER + +// When using the analyzer, the necessary state is not collected, so +// these methods all return no info. + +const k8s_pod_t* sinsp_filter_check_k8s::find_pod_for_thread(const sinsp_threadinfo* tinfo) { - if(m_field_id == TYPE_ARGRAW) - { - return &m_customfield; - } - else + return NULL; +} + +const k8s_ns_t* sinsp_filter_check_k8s::find_ns_by_name(const string& ns_name) +{ + return NULL; +} + +const k8s_rc_t* sinsp_filter_check_k8s::find_rc_by_pod(const k8s_pod_t* pod) +{ + return NULL; +} + +const k8s_rs_t* sinsp_filter_check_k8s::find_rs_by_pod(const k8s_pod_t* pod) +{ + return NULL; +} + +vector sinsp_filter_check_k8s::find_svc_by_pod(const k8s_pod_t* pod) +{ + + vector empty; + + return empty; +} + +const k8s_deployment_t* sinsp_filter_check_k8s::find_deployment_by_pod(const k8s_pod_t* pod) +{ + return NULL; +} + +#else +const k8s_pod_t* sinsp_filter_check_k8s::find_pod_for_thread(const sinsp_threadinfo* tinfo) +{ + if(tinfo->m_container_id.empty()) { - return &m_info.m_fields[m_field_id]; + return NULL; } + + const k8s_state_t& k8s_state = m_inspector->m_k8s_client->get_state(); + + return k8s_state.get_pod(tinfo->m_container_id); } -int32_t sinsp_filter_check_event::gmt2local(time_t t) +const k8s_ns_t* sinsp_filter_check_k8s::find_ns_by_name(const string& ns_name) { - register int dt, dir; - register struct tm *gmt, *loc; - struct tm sgmt; + const k8s_state_t& k8s_state = m_inspector->m_k8s_client->get_state(); - if(t == 0) + const k8s_state_t::namespace_map& ns_map = k8s_state.get_namespace_map(); + k8s_state_t::namespace_map::const_iterator it = ns_map.find(ns_name); + if(it != ns_map.end()) { - t = time(NULL); + return it->second; } - gmt = &sgmt; - *gmt = *gmtime(&t); - loc = localtime(&t); + return NULL; +} - dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 + (loc->tm_min - gmt->tm_min) * 60; +const k8s_rc_t* sinsp_filter_check_k8s::find_rc_by_pod(const k8s_pod_t* pod) +{ + const k8s_state_t& k8s_state = m_inspector->m_k8s_client->get_state(); - dir = loc->tm_year - gmt->tm_year; - if(dir == 0) + const k8s_state_t::pod_rc_map& pod_rcs = k8s_state.get_pod_rc_map(); + k8s_state_t::pod_rc_map::const_iterator it = pod_rcs.find(pod->get_uid()); + if(it != pod_rcs.end()) { - dir = loc->tm_yday - gmt->tm_yday; + return it->second; } - dt += dir * 24 * 60 * 60; - - return (dt); + return NULL; } -void sinsp_filter_check_event::ts_to_string(uint64_t ts, OUT string* res, bool date, bool ns) +const k8s_rs_t* sinsp_filter_check_k8s::find_rs_by_pod(const k8s_pod_t* pod) { - struct tm *tm; - time_t Time; - uint64_t sec = ts / ONE_SECOND_IN_NS; - uint64_t nsec = ts % ONE_SECOND_IN_NS; - int32_t thiszone = gmt2local(0); - int32_t s = (sec + thiszone) % 86400; - int32_t bufsize = 0; - char buf[256]; + const k8s_state_t& k8s_state = m_inspector->m_k8s_client->get_state(); - if(date) + const k8s_state_t::pod_rs_map& pod_rss = k8s_state.get_pod_rs_map(); + k8s_state_t::pod_rs_map::const_iterator it = pod_rss.find(pod->get_uid()); + if(it != pod_rss.end()) { - Time = (sec + thiszone) - s; - tm = gmtime (&Time); - if(!tm) - { - bufsize = sprintf(buf, " "); - } - else - { - bufsize = sprintf(buf, "%04d-%02d-%02d ", - tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); - } + return it->second; } - if(ns) + return NULL; +} + +vector sinsp_filter_check_k8s::find_svc_by_pod(const k8s_pod_t* pod) +{ + const k8s_state_t& k8s_state = m_inspector->m_k8s_client->get_state(); + vector services; + + const k8s_state_t::pod_service_map& pod_services = k8s_state.get_pod_service_map(); + auto range = pod_services.equal_range(pod->get_uid()); + for(auto it = range.first; it != range.second; ++it) { - sprintf(buf + bufsize, "%02d:%02d:%02d.%09u", - s / 3600, (s % 3600) / 60, s % 60, (unsigned)nsec); + services.push_back(it->second); } - else + return services; +} + +const k8s_deployment_t* sinsp_filter_check_k8s::find_deployment_by_pod(const k8s_pod_t* pod) +{ + const k8s_state_t& k8s_state = m_inspector->m_k8s_client->get_state(); + + const k8s_state_t::pod_deployment_map& pod_deployments = k8s_state.get_pod_deployment_map(); + k8s_state_t::pod_deployment_map::const_iterator it = pod_deployments.find(pod->get_uid()); + if(it != pod_deployments.end()) { - sprintf(buf + bufsize, "%02d:%02d:%02d", - s / 3600, (s % 3600) / 60, s % 60); + return it->second; } - *res = buf; + return NULL; } +#endif -uint8_t* sinsp_filter_check_event::extract(sinsp_evt *evt, OUT uint32_t* len) +void sinsp_filter_check_k8s::concatenate_labels(const k8s_pair_list& labels, string* s) { - switch(m_field_id) + for(const k8s_pair_t& label_pair : labels) { - case TYPE_TIME: - ts_to_string(evt->get_ts(), &m_strstorage, false, true); - return (uint8_t*)m_strstorage.c_str(); - case TYPE_TIME_S: - ts_to_string(evt->get_ts(), &m_strstorage, false, false); - return (uint8_t*)m_strstorage.c_str(); - case TYPE_DATETIME: - ts_to_string(evt->get_ts(), &m_strstorage, true, true); - return (uint8_t*)m_strstorage.c_str(); - case TYPE_RAWTS: - return (uint8_t*)&evt->m_pevt->ts; - case TYPE_RAWTS_S: - m_u64val = evt->get_ts() / ONE_SECOND_IN_NS; - return (uint8_t*)&m_u64val; - case TYPE_RAWTS_NS: - m_u64val = evt->get_ts() % ONE_SECOND_IN_NS; - return (uint8_t*)&m_u64val; - case TYPE_RELTS: - if(m_first_ts == 0) + if(!s->empty()) { - m_first_ts = evt->get_ts(); + s->append(", "); } - m_u64val = evt->get_ts() - m_first_ts; - return (uint8_t*)&m_u64val; - case TYPE_RELTS_S: - if(m_first_ts == 0) + s->append(label_pair.first); + if(!label_pair.second.empty()) { - m_first_ts = evt->get_ts(); + s->append(":" + label_pair.second); } + } +} - m_u64val = (evt->get_ts() - m_first_ts) / ONE_SECOND_IN_NS; - return (uint8_t*)&m_u64val; - case TYPE_RELTS_NS: - if(m_first_ts == 0) +bool sinsp_filter_check_k8s::find_label(const k8s_pair_list& labels, const string& key, string* value) +{ + for(const k8s_pair_t& label_pair : labels) + { + if(label_pair.first == key) { - m_first_ts = evt->get_ts(); + *value = label_pair.second; + return true; } + } - m_u64val = (evt->get_ts() - m_first_ts) % ONE_SECOND_IN_NS; - return (uint8_t*)&m_u64val; - case TYPE_LATENCY: - { - m_u64val = 0; - - if(evt->get_direction() == SCAP_ED_IN) - { - if(evt->m_tinfo != NULL) - { - uint16_t* pt = (uint16_t*)evt->m_tinfo->get_private_state(m_th_state_id); - *pt = evt->get_type(); - } - - return (uint8_t*)&m_u64val; - } + return false; +} - if(evt->m_tinfo != NULL) - { - uint16_t* pt = (uint16_t*)evt->m_tinfo->get_private_state(m_th_state_id); - if(evt->m_tinfo->m_prevevent_ts && evt->get_type() == *pt + 1) - { - m_u64val = (evt->get_ts() - evt->m_tinfo->m_prevevent_ts); - } - } +uint8_t* sinsp_filter_check_k8s::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) +{ + *len = 0; + if(m_inspector->m_k8s_client == NULL) + { + return NULL; + } - return (uint8_t*)&m_u64val; - } - case TYPE_LATENCY_S: - case TYPE_LATENCY_NS: - { - m_u64val = 0; + ASSERT(evt); + if(evt == NULL) + { + ASSERT(false); + return NULL; + } - if(evt->get_direction() == SCAP_ED_IN) - { - if(evt->m_tinfo != NULL) - { - uint16_t* pt = (uint16_t*)evt->m_tinfo->get_private_state(m_th_state_id); - *pt = evt->get_type(); - } + sinsp_threadinfo* tinfo = evt->get_thread_info(); + if(tinfo == NULL) + { + return NULL; + } - return (uint8_t*)&m_u64val; - } + const k8s_pod_t* pod = find_pod_for_thread(tinfo); + if(pod == NULL) + { + return NULL; + } - if(evt->m_tinfo != NULL) - { - uint16_t* pt = (uint16_t*)evt->m_tinfo->get_private_state(m_th_state_id); - if(evt->m_tinfo->m_prevevent_ts && evt->get_type() == *pt + 1) - { - if(m_field_id == TYPE_LATENCY_S) - { - m_u64val = (evt->get_ts() - evt->m_tinfo->m_prevevent_ts) / 1000000000; - } - else - { - m_u64val = (evt->get_ts() - evt->m_tinfo->m_prevevent_ts) % 1000000000; - } - } - } + m_tstr.clear(); - return (uint8_t*)&m_u64val; + switch(m_field_id) + { + case TYPE_K8S_POD_NAME: + m_tstr = pod->get_name(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_K8S_POD_ID: + m_tstr = pod->get_uid(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_K8S_POD_LABEL: + { + if(find_label(pod->get_labels(), m_argname, &m_tstr)) + { + RETURN_EXTRACT_STRING(m_tstr); } - case TYPE_DIR: - if(PPME_IS_ENTER(evt->get_type())) + break; + } + case TYPE_K8S_POD_LABELS: + { + concatenate_labels(pod->get_labels(), &m_tstr); + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_K8S_RC_NAME: + { + const k8s_rc_t* rc = find_rc_by_pod(pod); + if(rc != NULL) { - return (uint8_t*)">"; + m_tstr = rc->get_name(); + RETURN_EXTRACT_STRING(m_tstr); } - else + break; + } + case TYPE_K8S_RC_ID: + { + const k8s_rc_t* rc = find_rc_by_pod(pod); + if(rc != NULL) { - return (uint8_t*)"<"; + m_tstr = rc->get_uid(); + RETURN_EXTRACT_STRING(m_tstr); } - case TYPE_TYPE: + break; + } + case TYPE_K8S_RC_LABEL: + { + const k8s_rc_t* rc = find_rc_by_pod(pod); + if(rc != NULL) { - uint8_t* evname; - - if(evt->m_pevt->type == PPME_GENERIC_E || evt->m_pevt->type == PPME_GENERIC_X) - { - sinsp_evt_param *parinfo = evt->get_param(0); - ASSERT(parinfo->m_len == sizeof(uint16_t)); - uint16_t evid = *(uint16_t *)parinfo->m_val; - - evname = (uint8_t*)g_infotables.m_syscall_info_table[evid].name; - } - else + if(find_label(rc->get_labels(), m_argname, &m_tstr)) { - evname = (uint8_t*)evt->get_name(); + RETURN_EXTRACT_STRING(m_tstr); } - - return evname; } break; - case TYPE_NUMBER: - return (uint8_t*)&evt->m_evtnum; - case TYPE_CPU: - return (uint8_t*)&evt->m_cpuid; - case TYPE_ARGRAW: + } + case TYPE_K8S_RC_LABELS: + { + const k8s_rc_t* rc = find_rc_by_pod(pod); + if(rc != NULL) { - const sinsp_evt_param* pi = evt->get_param_value_raw(m_arginfo->name); - - if(pi != NULL) - { - *len = pi->m_len; - return (uint8_t*)pi->m_val; - } - else - { - return NULL; - } + concatenate_labels(rc->get_labels(), &m_tstr); + RETURN_EXTRACT_STRING(m_tstr); } break; - case TYPE_ARGSTR: + } + case TYPE_K8S_RS_NAME: + { + const k8s_rs_t* rs = find_rs_by_pod(pod); + if(rs != NULL) { - const char* resolved_argstr; - const char* argstr; - - ASSERT(m_inspector != NULL); - - if(m_argid != -1) - { - if(m_argid >= (int32_t)evt->m_info->nparams) - { - return NULL; - } - - argstr = evt->get_param_as_str(m_argid, &resolved_argstr, m_inspector->get_buffer_format()); - } - else - { - argstr = evt->get_param_value_str(m_argname.c_str(), &resolved_argstr, m_inspector->get_buffer_format()); - } - - if(resolved_argstr != NULL && resolved_argstr[0] != 0) - { - return (uint8_t*)resolved_argstr; - } - else - { - return (uint8_t*)argstr; - } + m_tstr = rs->get_name(); + RETURN_EXTRACT_STRING(m_tstr); } break; - case TYPE_ARGS: + } + case TYPE_K8S_RS_ID: + { + const k8s_rs_t* rs = find_rs_by_pod(pod); + if(rs != NULL) { - if(evt->get_type() == PPME_GENERIC_E || evt->get_type() == PPME_GENERIC_X) - { - // - // Don't print the arguments for generic events: they have only internal use - // - return (uint8_t*)""; - } - - const char* resolved_argstr = NULL; - const char* argstr = NULL; - uint32_t nargs = evt->get_num_params(); - m_strstorage.clear(); - - for(uint32_t j = 0; j < nargs; j++) + m_tstr = rs->get_uid(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_K8S_RS_LABEL: + { + const k8s_rs_t* rs = find_rs_by_pod(pod); + if(rs != NULL) + { + if(find_label(rs->get_labels(), m_argname, &m_tstr)) { - ASSERT(m_inspector != NULL); - - argstr = evt->get_param_as_str(j, &resolved_argstr, m_inspector->get_buffer_format()); - - if(resolved_argstr[0] == 0) - { - m_strstorage += evt->get_param_name(j); - m_strstorage += '='; - m_strstorage += argstr; - m_strstorage += " "; - } - else - { - m_strstorage += evt->get_param_name(j); - m_strstorage += '='; - m_strstorage += argstr; - m_strstorage += string("(") + resolved_argstr + ") "; - } + RETURN_EXTRACT_STRING(m_tstr); } - - return (uint8_t*)m_strstorage.c_str(); } break; - case TYPE_BUFFER: + } + case TYPE_K8S_RS_LABELS: + { + const k8s_rs_t* rs = find_rs_by_pod(pod); + if(rs != NULL) { - const char* resolved_argstr; - const char* argstr; - argstr = evt->get_param_value_str("data", &resolved_argstr, m_inspector->get_buffer_format()); - - return (uint8_t*)argstr; - - break; + concatenate_labels(rs->get_labels(), &m_tstr); + RETURN_EXTRACT_STRING(m_tstr); } - case TYPE_RESRAW: + break; + } + case TYPE_K8S_SVC_NAME: + { + vector services = find_svc_by_pod(pod); + if(!services.empty()) { - const sinsp_evt_param* pi = evt->get_param_value_raw("res"); - - if(pi != NULL) - { - *len = pi->m_len; - return (uint8_t*)pi->m_val; - } - - if((evt->get_flags() & EF_CREATES_FD) && PPME_IS_EXIT(evt->get_type())) + for(const k8s_service_t* service : services) { - pi = evt->get_param_value_raw("fd"); - - if(pi != NULL) + if(!m_tstr.empty()) { - *len = pi->m_len; - return (uint8_t*)pi->m_val; + m_tstr.append(", "); } + + m_tstr.append(service->get_name()); } - return NULL; + RETURN_EXTRACT_STRING(m_tstr); } break; - case TYPE_RESSTR: + } + case TYPE_K8S_SVC_ID: + { + vector services = find_svc_by_pod(pod); + if(!services.empty()) { - const char* resolved_argstr; - const char* argstr; - - argstr = evt->get_param_value_str("res", &resolved_argstr); - - if(resolved_argstr != NULL && resolved_argstr[0] != 0) - { - return (uint8_t*)resolved_argstr; - } - else + for(const k8s_service_t* service : services) { - if(argstr == NULL) - { - if((evt->get_flags() & EF_CREATES_FD) && PPME_IS_EXIT(evt->get_type())) - { - argstr = evt->get_param_value_str("fd", &resolved_argstr); - - if(resolved_argstr != NULL && resolved_argstr[0] != 0) - { - return (uint8_t*)resolved_argstr; - } - else - { - return (uint8_t*)argstr; - } - } - else - { - return NULL; - } - } - else + if(!m_tstr.empty()) { - return (uint8_t*)argstr; + m_tstr.append(", "); } + + m_tstr.append(service->get_uid()); } + + RETURN_EXTRACT_STRING(m_tstr); } break; - case TYPE_FAILED: + } + case TYPE_K8S_SVC_LABEL: + { + vector services = find_svc_by_pod(pod); + if(!services.empty()) { - m_u32val = 0; - const sinsp_evt_param* pi = evt->get_param_value_raw("res"); - - if(pi != NULL) - { - ASSERT(pi->m_len == sizeof(int64_t)); - if(*(int64_t*)pi->m_val < 0) - { - m_u32val = 1; - } - } - else if((evt->get_flags() & EF_CREATES_FD) && PPME_IS_EXIT(evt->get_type())) + for(const k8s_service_t* service : services) { - pi = evt->get_param_value_raw("fd"); - - if(pi != NULL) + string val; + if(find_label(service->get_labels(), m_argname, &val)) { - ASSERT(pi->m_len == sizeof(int64_t)); - if(*(int64_t*)pi->m_val < 0) + if(!m_tstr.empty()) { - m_u32val = 1; + m_tstr.append(", "); } + + m_tstr.append(val); } } - return (uint8_t*)&m_u32val; - } - break; - case TYPE_ISIO: - { - ppm_event_flags eflags = evt->get_flags(); - if(eflags & (EF_READS_FROM_FD | EF_WRITES_TO_FD)) - { - m_u32val = 1; - } - else + if(!m_tstr.empty()) { - m_u32val = 0; + RETURN_EXTRACT_STRING(m_tstr); } } - - return (uint8_t*)&m_u32val; - case TYPE_ISIO_READ: + break; + } + case TYPE_K8S_SVC_LABELS: + { + vector services = find_svc_by_pod(pod); + if(!services.empty()) { - ppm_event_flags eflags = evt->get_flags(); - if(eflags & EF_READS_FROM_FD) - { - m_u32val = 1; - } - else + for(const k8s_service_t* service : services) { - m_u32val = 0; + concatenate_labels(service->get_labels(), &m_tstr); } - return (uint8_t*)&m_u32val; + RETURN_EXTRACT_STRING(m_tstr); } - case TYPE_ISIO_WRITE: + break; + } + case TYPE_K8S_NS_NAME: + { + m_tstr = pod->get_namespace(); + RETURN_EXTRACT_STRING(m_tstr); + } + case TYPE_K8S_NS_ID: + { + const k8s_ns_t* ns = find_ns_by_name(pod->get_namespace()); + if(ns != NULL) { - ppm_event_flags eflags = evt->get_flags(); - if(eflags & EF_WRITES_TO_FD) - { - m_u32val = 1; - } - else + m_tstr = ns->get_uid(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_K8S_NS_LABEL: + { + const k8s_ns_t* ns = find_ns_by_name(pod->get_namespace()); + if(ns != NULL) + { + if(find_label(ns->get_labels(), m_argname, &m_tstr)) { - m_u32val = 0; + RETURN_EXTRACT_STRING(m_tstr); } - - return (uint8_t*)&m_u32val; } - case TYPE_ISWAIT: + break; + } + case TYPE_K8S_NS_LABELS: + { + const k8s_ns_t* ns = find_ns_by_name(pod->get_namespace()); + if(ns != NULL) + { + concatenate_labels(ns->get_labels(), &m_tstr); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_K8S_DEPLOYMENT_NAME: + { + const k8s_deployment_t* deployment = find_deployment_by_pod(pod); + if(deployment != NULL) + { + m_tstr = deployment->get_name(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_K8S_DEPLOYMENT_ID: + { + const k8s_deployment_t* deployment = find_deployment_by_pod(pod); + if(deployment != NULL) + { + m_tstr = deployment->get_uid(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_K8S_DEPLOYMENT_LABEL: + { + const k8s_deployment_t* deployment = find_deployment_by_pod(pod); + if(deployment != NULL) { - ppm_event_flags eflags = evt->get_flags(); - if(eflags & (EF_WAITS)) + if(find_label(deployment->get_labels(), m_argname, &m_tstr)) { - m_u32val = 1; - } - else - { - m_u32val = 0; + RETURN_EXTRACT_STRING(m_tstr); } } - - return (uint8_t*)&m_u32val; - case TYPE_COUNT: - m_u32val = 1; - return (uint8_t*)&m_u32val; + break; + } + case TYPE_K8S_DEPLOYMENT_LABELS: + { + const k8s_deployment_t* deployment = find_deployment_by_pod(pod); + if(deployment != NULL) + { + concatenate_labels(deployment->get_labels(), &m_tstr); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } default: ASSERT(false); return NULL; @@ -1288,236 +7683,315 @@ uint8_t* sinsp_filter_check_event::extract(sinsp_evt *evt, OUT uint32_t* len) return NULL; } -char* sinsp_filter_check_event::tostring(sinsp_evt* evt) + + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter_check_mesos implementation +/////////////////////////////////////////////////////////////////////////////// + +const filtercheck_field_info sinsp_filter_check_mesos_fields[] = { - if(m_field_id == TYPE_ARGRAW) + {PT_CHARBUF, EPF_NONE, PF_NA, "mesos.task.name", "Mesos task name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "mesos.task.id", "Mesos task id."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "mesos.task.label", "Mesos task label. E.g. 'mesos.task.label.foo'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "mesos.task.labels", "Mesos task comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "mesos.framework.name", "Mesos framework name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "mesos.framework.id", "Mesos framework id."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "marathon.app.name", "Marathon app name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "marathon.app.id", "Marathon app id."}, + {PT_CHARBUF, EPF_REQUIRES_ARGUMENT, PF_NA, "marathon.app.label", "Marathon app label. E.g. 'marathon.app.label.foo'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "marathon.app.labels", "Marathon app comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "marathon.group.name", "Marathon group name."}, + {PT_CHARBUF, EPF_NONE, PF_NA, "marathon.group.id", "Marathon group id."}, +}; + +sinsp_filter_check_mesos::sinsp_filter_check_mesos() +{ + m_info.m_name = "mesos"; + m_info.m_fields = sinsp_filter_check_mesos_fields; + m_info.m_nfields = sizeof(sinsp_filter_check_mesos_fields) / sizeof(sinsp_filter_check_mesos_fields[0]); + m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE; +} + +sinsp_filter_check* sinsp_filter_check_mesos::allocate_new() +{ + return (sinsp_filter_check*) new sinsp_filter_check_mesos(); +} + +int32_t sinsp_filter_check_mesos::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) +{ + string val(str); + + if(string(val, 0, sizeof("mesos.task.label") - 1) == "mesos.task.label" && + string(val, 0, sizeof("mesos.task.labels") - 1) != "mesos.task.labels") { - uint32_t len; - uint8_t* rawval = extract(evt, &len); + m_field_id = TYPE_MESOS_TASK_LABEL; + m_field = &m_info.m_fields[m_field_id]; - if(rawval == NULL) - { - return NULL; - } + return extract_arg("mesos.task.label", val); + } + else if(string(val, 0, sizeof("marathon.app.label") - 1) == "marathon.app.label" && + string(val, 0, sizeof("marathon.app.labels") - 1) != "marathon.app.labels") + { + m_field_id = TYPE_MARATHON_APP_LABEL; + m_field = &m_info.m_fields[m_field_id]; - return rawval_to_string(rawval, &m_customfield, len); + return extract_arg("marathon.app.label", val); } else { - return sinsp_filter_check::tostring(evt); + return sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering); } } -bool sinsp_filter_check_event::compare(sinsp_evt *evt) +int32_t sinsp_filter_check_mesos::extract_arg(const string& fldname, const string& val) { - if(m_field_id == TYPE_ARGRAW) - { - uint32_t len; - uint8_t* extracted_val = extract(evt, &len); + int32_t parsed_len = 0; - if(extracted_val == NULL) + if(val[fldname.size()] == '.') + { + size_t endpos; + for(endpos = fldname.size() + 1; endpos < val.length(); ++endpos) { - return false; + if(!isalnum(val[endpos]) + && val[endpos] != '/' + && val[endpos] != '_' + && val[endpos] != '-' + && val[endpos] != '.') + { + break; + } } - ASSERT(m_arginfo != NULL); - - return flt_compare(m_cmpop, - m_arginfo->type, - extracted_val, - &m_val_storage[0]); + parsed_len = (uint32_t)endpos; + m_argname = val.substr(fldname.size() + 1, endpos - fldname.size() - 1); } else { - return sinsp_filter_check::compare(evt); + throw sinsp_exception("filter syntax error: " + val); } + + return parsed_len; } -/////////////////////////////////////////////////////////////////////////////// -// sinsp_filter_check_user implementation -/////////////////////////////////////////////////////////////////////////////// -const filtercheck_field_info sinsp_filter_check_user_fields[] = +mesos_task::ptr_t sinsp_filter_check_mesos::find_task_for_thread(const sinsp_threadinfo* tinfo) { - {PT_UINT64, EPF_NONE, PF_DEC, "user.id", "user ID."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "user.name", "user name."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "user.homedir", "home directory of the user."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "user.shell", "user's shell."}, -}; + ASSERT(m_inspector && tinfo); + if(tinfo) + { + if(tinfo->m_container_id.empty()) + { + return NULL; + } -sinsp_filter_check_user::sinsp_filter_check_user() -{ - m_info.m_name = "user"; - m_info.m_fields = sinsp_filter_check_user_fields; - m_info.m_nfiedls = sizeof(sinsp_filter_check_user_fields) / sizeof(sinsp_filter_check_user_fields[0]); + if(m_inspector && m_inspector->m_mesos_client) + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + if(!container_info || container_info->m_mesos_task_id.empty()) + { + return NULL; + } + const mesos_state_t& mesos_state = m_inspector->m_mesos_client->get_state(); + return mesos_state.get_task(container_info->m_mesos_task_id); + } + } + + return NULL; } -sinsp_filter_check* sinsp_filter_check_user::allocate_new() +const mesos_framework* sinsp_filter_check_mesos::find_framework_by_task(mesos_task::ptr_t task) { - return (sinsp_filter_check*) new sinsp_filter_check_user(); + if(task && m_inspector && m_inspector->m_mesos_client) + { + const mesos_state_t& mesos_state = m_inspector->m_mesos_client->get_state(); + return mesos_state.get_framework_for_task(task->get_uid()); + } + return NULL; } -uint8_t* sinsp_filter_check_user::extract(sinsp_evt *evt, OUT uint32_t* len) +marathon_app::ptr_t sinsp_filter_check_mesos::find_app_by_task(mesos_task::ptr_t task) { - sinsp_threadinfo* tinfo = evt->get_thread_info(); - scap_userinfo* uinfo; - - if(tinfo == NULL) + if(m_inspector && m_inspector->m_mesos_client) { - return NULL; + return m_inspector->m_mesos_client->get_state().get_app(task); } + return NULL; +} - if(m_field_id != TYPE_UID) +marathon_group::ptr_t sinsp_filter_check_mesos::find_group_by_task(mesos_task::ptr_t task) +{ + if(m_inspector && m_inspector->m_mesos_client) { - unordered_map::iterator it; - - ASSERT(m_inspector != NULL); - unordered_map* userlist = - (unordered_map*)m_inspector->get_userlist(); - ASSERT(userlist->size() != 0); + return m_inspector->m_mesos_client->get_state().get_group(task); + } + return NULL; +} - if(tinfo->m_uid == 0xffffffff) +void sinsp_filter_check_mesos::concatenate_labels(const mesos_pair_list& labels, string* s) +{ + for(const mesos_pair_t& label_pair : labels) + { + if(!s->empty()) { - return NULL; + s->append(", "); } - it = userlist->find(tinfo->m_uid); - if(it == userlist->end()) + s->append(label_pair.first); + if(!label_pair.second.empty()) { - ASSERT(false); - return NULL; + s->append(":" + label_pair.second); } - - uinfo = it->second; - ASSERT(uinfo != NULL); } +} - switch(m_field_id) +bool sinsp_filter_check_mesos::find_label(const mesos_pair_list& labels, const string& key, string* value) +{ + for(const mesos_pair_t& label_pair : labels) { - case TYPE_UID: - return (uint8_t*)&tinfo->m_uid; - case TYPE_NAME: - return (uint8_t*)uinfo->name; - case TYPE_HOMEDIR: - return (uint8_t*)uinfo->homedir; - case TYPE_SHELL: - return (uint8_t*) uinfo->shell; - default: - ASSERT(false); - break; + if(label_pair.first == key) + { + *value = label_pair.second; + return true; + } } - return NULL; + return false; } -/////////////////////////////////////////////////////////////////////////////// -// sinsp_filter_check_group implementation -/////////////////////////////////////////////////////////////////////////////// -const filtercheck_field_info sinsp_filter_check_group_fields[] = -{ - {PT_UINT64, EPF_NONE, PF_DEC, "group.gid", "group ID."}, - {PT_CHARBUF, EPF_NONE, PF_NA, "group.name", "group name."}, -}; - -sinsp_filter_check_group::sinsp_filter_check_group() +uint8_t* sinsp_filter_check_mesos::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings) { - m_info.m_name = "group"; - m_info.m_fields = sinsp_filter_check_group_fields; - m_info.m_nfiedls = sizeof(sinsp_filter_check_group_fields) / sizeof(sinsp_filter_check_group_fields[0]); -} + *len = 0; + if(!m_inspector || !m_inspector->m_mesos_client) + { + return NULL; + } -sinsp_filter_check* sinsp_filter_check_group::allocate_new() -{ - return (sinsp_filter_check*) new sinsp_filter_check_group(); -} + if(!evt) + { + ASSERT(false); + return NULL; + } -uint8_t* sinsp_filter_check_group::extract(sinsp_evt *evt, OUT uint32_t* len) -{ sinsp_threadinfo* tinfo = evt->get_thread_info(); + if(!tinfo) + { + return NULL; + } - if(tinfo == NULL) + mesos_task::ptr_t task = find_task_for_thread(tinfo); + if(!task) { return NULL; } + m_tstr.clear(); + switch(m_field_id) { - case TYPE_GID: - return (uint8_t*)&tinfo->m_gid; - case TYPE_NAME: + case TYPE_MESOS_TASK_NAME: + m_tstr = task->get_name(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_MESOS_TASK_ID: + m_tstr = task->get_uid(); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_MESOS_TASK_LABEL: + if(find_label(task->get_labels(), m_argname, &m_tstr)) { - unordered_map::iterator it; - - ASSERT(m_inspector != NULL); - unordered_map* grouplist = - (unordered_map*)m_inspector->get_grouplist(); - ASSERT(grouplist->size() != 0); - - if(tinfo->m_gid == 0xffffffff) - { - return NULL; - } - - it = grouplist->find(tinfo->m_gid); - if(it == grouplist->end()) - { - ASSERT(false); - return NULL; - } + RETURN_EXTRACT_STRING(m_tstr); + } + break; + case TYPE_MESOS_TASK_LABELS: + concatenate_labels(task->get_labels(), &m_tstr); + RETURN_EXTRACT_STRING(m_tstr); + case TYPE_MESOS_FRAMEWORK_NAME: + { + const mesos_framework* fw = find_framework_by_task(task); + if(fw) + { + m_tstr = fw->get_name(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_MESOS_FRAMEWORK_ID: + { + const mesos_framework* fw = find_framework_by_task(task); + if(fw) + { + m_tstr = fw->get_uid(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_MARATHON_APP_NAME: + { + marathon_app::ptr_t app = find_app_by_task(task); + if(app != NULL) + { + m_tstr = app->get_name(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_MARATHON_APP_ID: + { + marathon_app::ptr_t app = find_app_by_task(task); + if(app != NULL) + { + m_tstr = app->get_id(); + RETURN_EXTRACT_STRING(m_tstr); + } - scap_groupinfo* ginfo = it->second; - ASSERT(ginfo != NULL); + break; + } + case TYPE_MARATHON_APP_LABEL: + { + marathon_app::ptr_t app = find_app_by_task(task); + if(app && find_label(app->get_labels(), m_argname, &m_tstr)) + { + RETURN_EXTRACT_STRING(m_tstr); + } - return (uint8_t*)ginfo->name; + break; + } + case TYPE_MARATHON_APP_LABELS: + { + marathon_app::ptr_t app = find_app_by_task(task); + if(app) + { + concatenate_labels(app->get_labels(), &m_tstr); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } + case TYPE_MARATHON_GROUP_NAME: + { + marathon_app::ptr_t app = find_app_by_task(task); + if(app) + { + m_tstr = app->get_group_id(); + RETURN_EXTRACT_STRING(m_tstr); } + break; + } + case TYPE_MARATHON_GROUP_ID: + { + marathon_app::ptr_t app = find_app_by_task(task); + if(app) + { + m_tstr = app->get_group_id(); + RETURN_EXTRACT_STRING(m_tstr); + } + break; + } default: ASSERT(false); - break; + return NULL; } return NULL; } - -/////////////////////////////////////////////////////////////////////////////// -// rawstring_check implementation -/////////////////////////////////////////////////////////////////////////////// -const filtercheck_field_info rawstring_check_fields[] = -{ - {PT_CHARBUF, EPF_NONE, PF_NA, "NA", "INTERNAL."}, -}; - -rawstring_check::rawstring_check(string text) -{ - m_field = rawstring_check_fields; - set_text(text); -} - -sinsp_filter_check* rawstring_check::allocate_new() -{ - ASSERT(false); - return NULL; -} - -void rawstring_check::set_text(string text) -{ - m_text_len = text.size(); - m_text = text; -} - -int32_t rawstring_check::parse_field_name(const char* str) -{ - ASSERT(false); - return -1; -} - -void rawstring_check::parse_filter_value(const char* str) -{ - ASSERT(false); -} - -uint8_t* rawstring_check::extract(sinsp_evt *evt, OUT uint32_t* len) -{ - *len = m_text_len; - return (uint8_t*)m_text.c_str(); -} +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) #endif // HAS_FILTERING diff --git a/userspace/libsinsp/filterchecks.h b/userspace/libsinsp/filterchecks.h index 269dbeee00..5a49ac98c7 100644 --- a/userspace/libsinsp/filterchecks.h +++ b/userspace/libsinsp/filterchecks.h @@ -1,32 +1,44 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once +#include +#include +#include "filter_value.h" +#include "prefix_search.h" +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) +#include "k8s.h" +#include "mesos.h" +#endif #ifdef HAS_FILTERING +#include "gen_filter.h" + +class sinsp_filter_check_reference; -#define VALIDATE_STR_VAL if(val.length() >= sizeof(m_val_storage)) \ -{ \ - throw sinsp_exception("filter error: value too long: " + val); \ -} +bool flt_compare(cmpop op, ppm_param_type type, void* operand1, void* operand2, uint32_t op1_len = 0, uint32_t op2_len = 0); +bool flt_compare_avg(cmpop op, ppm_param_type type, void* operand1, void* operand2, uint32_t op1_len, uint32_t op2_len, uint32_t cnt1, uint32_t cnt2); +bool flt_compare_ipv4net(cmpop op, uint64_t operand1, ipv4net* operand2); +bool flt_compare_ipv6net(cmpop op, ipv6addr *operand1, ipv6addr* operand2); -bool flt_compare(ppm_cmp_operator op, ppm_param_type type, void* operand1, void* operand2); char* flt_to_string(uint8_t* rawval, filtercheck_field_info* finfo); +int32_t gmt2local(time_t t); class operand_info { @@ -42,11 +54,12 @@ class operand_info // NOTE: in order to add a new type of filter check, you need to add a class for // it and then add it to new_filter_check_from_name. /////////////////////////////////////////////////////////////////////////////// -class sinsp_filter_check + +class sinsp_filter_check : public gen_event_filter_check { public: sinsp_filter_check(); - + virtual ~sinsp_filter_check() { } @@ -60,37 +73,55 @@ class sinsp_filter_check // // Get the list of fields that this check exports // - virtual filter_check_info* get_filelds() + virtual filter_check_info* get_fields() { return &m_info; } // // Parse the name of the field. - // Returns the lenght of the parsed field if successful, an exception in + // Returns the length of the parsed field if successful, an exception in // case of error. // - virtual int32_t parse_field_name(const char* str); - + virtual int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + // // If this check is used by a filter, extract the constant to compare it to - // Doesn't return the field lenght because the filtering engine can calculate it. + // Doesn't return the field length because the filtering engine can calculate it. // - virtual void parse_filter_value(const char* str); + void add_filter_value(const char* str, uint32_t len, uint32_t i = 0 ); + virtual size_t parse_filter_value(const char* str, uint32_t len, uint8_t *storage, uint32_t storage_len); // - // Return the info about the field that this instance contains + // Called after parsing for optional validation of the filter value + // + void validate_filter_value(const char* str, uint32_t len) {} + + // + // Return the info about the field that this instance contains // virtual const filtercheck_field_info* get_field_info(); // - // Extract the field from the event + // Extract the field from the event. In sanitize_strings is true, any + // string values are sanitized to remove nonprintable characters. + // + uint8_t* extract(gen_event *evt, OUT uint32_t* len, bool sanitize_strings = true); + virtual uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true) = 0; + + // + // Extract the field as json from the event (by default, fall + // back to the regular extract functionality) // - virtual uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len) = 0; + virtual Json::Value extract_as_js(sinsp_evt *evt, OUT uint32_t* len) + { + return Json::nullValue; + } // // Compare the field with the constant value obtained from parse_filter_value() // + bool compare(gen_event *evt); virtual bool compare(sinsp_evt *evt); // @@ -98,20 +129,46 @@ class sinsp_filter_check // virtual char* tostring(sinsp_evt* evt); + // + // Extract the value from the event and convert it into a Json value + // or object + // + virtual Json::Value tojson(sinsp_evt* evt); + sinsp* m_inspector; - boolop m_boolop; - ppm_cmp_operator m_cmpop; + bool m_needs_state_tracking = false; + sinsp_field_aggregation m_aggregation; + sinsp_field_aggregation m_merge_aggregation; protected: - char* rawval_to_string(uint8_t* rawval, const filtercheck_field_info* finfo, uint32_t len); - void string_to_rawval(const char* str, ppm_param_type ptype); + bool flt_compare(cmpop op, ppm_param_type type, void* operand1, uint32_t op1_len = 0, uint32_t op2_len = 0); + + char* rawval_to_string(uint8_t* rawval, + ppm_param_type ptype, + ppm_print_format print_format, + uint32_t len); + Json::Value rawval_to_json(uint8_t* rawval, ppm_param_type ptype, ppm_print_format print_format, uint32_t len); + void string_to_rawval(const char* str, uint32_t len, ppm_param_type ptype); char m_getpropertystr_storage[1024]; - vector m_val_storage; + vector> m_val_storages; + inline uint8_t* filter_value_p(uint16_t i = 0) { return &m_val_storages[i][0]; } + inline vector filter_value(uint16_t i = 0) { return m_val_storages[i]; } + + unordered_set m_val_storages_members; + + path_prefix_search m_val_storages_paths; + + uint32_t m_val_storages_min_size; + uint32_t m_val_storages_max_size; + const filtercheck_field_info* m_field; filter_check_info m_info; uint32_t m_field_id; uint32_t m_th_state_id; + uint32_t m_val_storage_len; private: void set_inspector(sinsp* inspector); @@ -130,59 +187,13 @@ class sinsp_filter_check_list ~sinsp_filter_check_list(); void add_filter_check(sinsp_filter_check* filter_check); void get_all_fields(vector* list); - sinsp_filter_check* new_filter_check_from_fldname(string name, sinsp* inspector, bool do_exact_check); + sinsp_filter_check* new_filter_check_from_another(sinsp_filter_check *chk); + sinsp_filter_check* new_filter_check_from_fldname(const string& name, sinsp* inspector, bool do_exact_check); private: vector m_check_list; }; -/////////////////////////////////////////////////////////////////////////////// -// Filter expression class -// A filter expression contains multiple filters connected by boolean expressions, -// e.g. "check or check", "check and check and check", "not check" -/////////////////////////////////////////////////////////////////////////////// -class sinsp_filter_expression : public sinsp_filter_check -{ -public: - sinsp_filter_expression(); - ~sinsp_filter_expression(); - sinsp_filter_check* allocate_new(); - void add_check(sinsp_filter_check* chk); - // does nothing for sinsp_filter_expression - void parse(string expr); - bool compare(sinsp_evt *evt); - - // - // The following methods are part of the filter check interface but are irrelevant - // for this class, because they are used only for the leaves of the filtering tree. - // - int32_t parse_field_name(const char* str) - { - ASSERT(false); - return 0; - } - - void parse_filter_value(const char* str) - { - ASSERT(false); - } - - const filtercheck_field_info* get_field_info() - { - ASSERT(false); - return NULL; - } - - uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len) - { - ASSERT(false); - return NULL; - } - - sinsp_filter_expression* m_parent; - vector m_checks; -}; - /////////////////////////////////////////////////////////////////////////////// // Filter check classes /////////////////////////////////////////////////////////////////////////////// @@ -199,15 +210,43 @@ class sinsp_filter_check_fd : public sinsp_filter_check TYPE_FDTYPE = 1, TYPE_FDTYPECHAR = 2, TYPE_FDNAME = 3, - TYPE_IP = 4, - TYPE_CLIENTIP = 5, - TYPE_SERVERIP = 6, - TYPE_PORT = 7, - TYPE_CLIENTPORT = 8, - TYPE_SERVERPORT = 9, - TYPE_L4PROTO = 10, - TYPE_SOCKFAMILY = 11, - TYPE_IS_SERVER = 12, + TYPE_DIRECTORY = 4, + TYPE_FILENAME = 5, + TYPE_IP = 6, + TYPE_CLIENTIP = 7, + TYPE_SERVERIP = 8, + TYPE_LIP = 9, + TYPE_RIP = 10, + TYPE_PORT = 11, + TYPE_CLIENTPORT = 12, + TYPE_SERVERPORT = 13, + TYPE_LPORT = 14, + TYPE_RPORT = 15, + TYPE_L4PROTO = 16, + TYPE_SOCKFAMILY = 17, + TYPE_IS_SERVER = 18, + TYPE_UID = 19, + TYPE_CONTAINERNAME = 20, + TYPE_CONTAINERDIRECTORY = 21, + TYPE_PROTO = 22, + TYPE_CLIENTPROTO = 23, + TYPE_SERVERPROTO = 24, + TYPE_LPROTO = 25, + TYPE_RPROTO = 26, + TYPE_NET = 27, + TYPE_CNET = 28, + TYPE_SNET = 29, + TYPE_LNET = 30, + TYPE_RNET = 31, + TYPE_IS_CONNECTED = 32, + TYPE_NAME_CHANGED = 33, + TYPE_CLIENTIP_NAME = 34, + TYPE_SERVERIP_NAME = 35, + TYPE_LIP_NAME = 36, + TYPE_RIP_NAME = 37, + TYPE_DEV = 38, + TYPE_DEV_MAJOR = 39, + TYPE_DEV_MINOR = 40, }; enum fd_type @@ -228,13 +267,12 @@ class sinsp_filter_check_fd : public sinsp_filter_check sinsp_filter_check_fd(); sinsp_filter_check* allocate_new(); - int32_t parse_field_name(const char* str); - uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len); - uint8_t* extract_fdtype(sinsp_fdinfo_t* fdinfo); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); bool compare_ip(sinsp_evt *evt); + bool compare_net(sinsp_evt *evt); bool compare_port(sinsp_evt *evt); + bool compare_domain(sinsp_evt *evt); bool compare(sinsp_evt *evt); - char* tostring(sinsp_evt* evt); sinsp_threadinfo* m_tinfo; sinsp_fdinfo_t* m_fdinfo; @@ -244,11 +282,13 @@ class sinsp_filter_check_fd : public sinsp_filter_check uint32_t m_tbool; private: + uint8_t* extract_from_null_fd(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings); + bool extract_fdname_from_creator(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings); bool extract_fd(sinsp_evt *evt); }; // -// thread checks +// thread sinsp_filter_check_thread // class sinsp_filter_check_thread : public sinsp_filter_check { @@ -259,31 +299,76 @@ class sinsp_filter_check_thread : public sinsp_filter_check TYPE_EXE = 1, TYPE_NAME = 2, TYPE_ARGS = 3, - TYPE_CWD = 4, - TYPE_NCHILDS = 5, - TYPE_PARENTNAME = 6, - TYPE_TID = 7, - TYPE_ISMAINTHREAD = 8, - TYPE_EXECTIME = 9, - IOBYTES = 10, - TOTIOBYTES = 11, - LATENCY = 12, - TOTLATENCY = 13, + TYPE_ENV = 4, + TYPE_CMDLINE = 5, + TYPE_EXELINE = 6, + TYPE_CWD = 7, + TYPE_NTHREADS = 8, + TYPE_NCHILDS = 9, + TYPE_PPID = 10, + TYPE_PNAME = 11, + TYPE_PCMDLINE = 12, + TYPE_APID = 13, + TYPE_ANAME = 14, + TYPE_LOGINSHELLID = 15, + TYPE_DURATION = 16, + TYPE_FDOPENCOUNT = 17, + TYPE_FDLIMIT = 18, + TYPE_FDUSAGE = 19, + TYPE_VMSIZE = 20, + TYPE_VMRSS = 21, + TYPE_VMSWAP = 22, + TYPE_PFMAJOR = 23, + TYPE_PFMINOR = 24, + TYPE_TID = 25, + TYPE_ISMAINTHREAD = 26, + TYPE_EXECTIME = 27, + TYPE_TOTEXECTIME = 28, + TYPE_CGROUPS = 29, + TYPE_CGROUP = 30, + TYPE_VTID = 31, + TYPE_VPID = 32, + TYPE_THREAD_CPU = 33, + TYPE_THREAD_CPU_USER = 34, + TYPE_THREAD_CPU_SYSTEM = 35, + TYPE_THREAD_VMSIZE = 36, + TYPE_THREAD_VMRSS = 37, + TYPE_THREAD_VMSIZE_B = 38, + TYPE_THREAD_VMRSS_B = 39, + TYPE_SID = 40, + TYPE_SNAME = 41, + TYPE_TTY = 42, + TYPE_EXEPATH = 43, + TYPE_NAMETID = 44, + TYPE_VPGID = 45, + TYPE_IS_CONTAINER_HEALTHCHECK = 46, + TYPE_IS_CONTAINER_LIVENESS_PROBE = 47, + TYPE_IS_CONTAINER_READINESS_PROBE = 48, }; sinsp_filter_check_thread(); sinsp_filter_check* allocate_new(); - int32_t parse_field_name(const char* str); - uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + bool compare(sinsp_evt *evt); - // XXX this is overkill and wasted for most of the fields. - // It could be optimized by dynamically allocating the right amount - // of memory, but we don't care for the moment since we expect filters - // to be pretty small. +private: + uint64_t extract_exectime(sinsp_evt *evt); + int32_t extract_arg(string fldname, string val, OUT const struct ppm_param_info** parinfo); + uint8_t* extract_thread_cpu(sinsp_evt *evt, OUT uint32_t* len, sinsp_threadinfo* tinfo, bool extract_user, bool extract_system); + inline bool compare_full_apid(sinsp_evt *evt); + bool compare_full_aname(sinsp_evt *evt); + + int32_t m_argid; + string m_argname; uint32_t m_tbool; string m_tstr; uint64_t m_u64val; + int64_t m_s64val; + double m_dval; vector m_last_proc_switch_times; + uint32_t m_th_state_id; + uint64_t m_cursec_ts; }; // @@ -297,59 +382,112 @@ class sinsp_filter_check_event : public sinsp_filter_check TYPE_NUMBER = 0, TYPE_TIME = 1, TYPE_TIME_S = 2, - TYPE_DATETIME = 3, - TYPE_RAWTS = 4, - TYPE_RAWTS_S = 5, - TYPE_RAWTS_NS = 6, - TYPE_RELTS = 7, - TYPE_RELTS_S = 8, - TYPE_RELTS_NS = 9, - TYPE_LATENCY = 10, - TYPE_LATENCY_S = 11, - TYPE_LATENCY_NS = 12, - TYPE_DIR = 13, - TYPE_TYPE = 14, - TYPE_CPU = 15, - TYPE_ARGS = 16, - TYPE_ARGSTR = 17, - TYPE_ARGRAW = 18, - TYPE_BUFFER = 19, - TYPE_RESSTR = 20, - TYPE_RESRAW = 21, - TYPE_FAILED = 22, - TYPE_ISIO = 23, - TYPE_ISIO_READ = 24, - TYPE_ISIO_WRITE = 25, - TYPE_ISWAIT = 26, - TYPE_COUNT = 27, + TYPE_TIME_ISO8601 = 3, + TYPE_DATETIME = 4, + TYPE_RAWTS = 5, + TYPE_RAWTS_S = 6, + TYPE_RAWTS_NS = 7, + TYPE_RELTS = 8, + TYPE_RELTS_S = 9, + TYPE_RELTS_NS = 10, + TYPE_LATENCY = 11, + TYPE_LATENCY_S = 12, + TYPE_LATENCY_NS = 13, + TYPE_LATENCY_QUANTIZED = 14, + TYPE_LATENCY_HUMAN = 15, + TYPE_DELTA = 16, + TYPE_DELTA_S = 17, + TYPE_DELTA_NS = 18, + TYPE_RUNTIME_TIME_OUTPUT_FORMAT = 19, + TYPE_DIR = 20, + TYPE_TYPE = 21, + TYPE_TYPE_IS = 22, + TYPE_SYSCALL_TYPE = 23, + TYPE_CATEGORY = 24, + TYPE_CPU = 25, + TYPE_ARGS = 26, + TYPE_ARGSTR = 27, + TYPE_ARGRAW = 28, + TYPE_INFO = 29, + TYPE_BUFFER = 30, + TYPE_BUFLEN = 31, + TYPE_RESSTR = 32, + TYPE_RESRAW = 33, + TYPE_FAILED = 34, + TYPE_ISIO = 35, + TYPE_ISIO_READ = 36, + TYPE_ISIO_WRITE = 37, + TYPE_IODIR = 38, + TYPE_ISWAIT = 39, + TYPE_WAIT_LATENCY = 40, + TYPE_ISSYSLOG = 41, + TYPE_COUNT = 42, + TYPE_COUNT_ERROR = 43, + TYPE_COUNT_ERROR_FILE = 44, + TYPE_COUNT_ERROR_NET = 45, + TYPE_COUNT_ERROR_MEMORY = 46, + TYPE_COUNT_ERROR_OTHER = 47, + TYPE_COUNT_EXIT = 48, + TYPE_COUNT_PROCINFO = 49, + TYPE_COUNT_THREADINFO = 50, + TYPE_AROUND = 51, + TYPE_ABSPATH = 52, + TYPE_BUFLEN_IN = 53, + TYPE_BUFLEN_OUT = 54, + TYPE_BUFLEN_FILE = 55, + TYPE_BUFLEN_FILE_IN = 56, + TYPE_BUFLEN_FILE_OUT = 57, + TYPE_BUFLEN_NET = 58, + TYPE_BUFLEN_NET_IN = 59, + TYPE_BUFLEN_NET_OUT = 60, + TYPE_ISOPEN_READ = 61, + TYPE_ISOPEN_WRITE = 62, + TYPE_INFRA_DOCKER_NAME = 63, + TYPE_INFRA_DOCKER_CONTAINER_ID = 64, + TYPE_INFRA_DOCKER_CONTAINER_NAME = 65, + TYPE_INFRA_DOCKER_CONTAINER_IMAGE = 66, + TYPE_ISOPEN_EXEC = 67, }; sinsp_filter_check_event(); + ~sinsp_filter_check_event(); sinsp_filter_check* allocate_new(); - int32_t parse_field_name(const char* str); - void parse_filter_value(const char* str); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + size_t parse_filter_value(const char* str, uint32_t len, uint8_t *storage, uint32_t storage_len); + void validate_filter_value(const char* str, uint32_t len); const filtercheck_field_info* get_field_info(); - uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + Json::Value extract_as_js(sinsp_evt *evt, OUT uint32_t* len); bool compare(sinsp_evt *evt); - char* tostring(sinsp_evt* evt); - uint64_t m_first_ts; uint64_t m_u64val; + uint64_t m_tsdelta; uint32_t m_u32val; string m_strstorage; string m_argname; int32_t m_argid; + uint32_t m_evtid; + uint32_t m_evtid1; const ppm_param_info* m_arginfo; + // - // Note: this copy of the field is used by some fields, like TYPE_ARGS and + // Note: this copy of the field is used by some fields, like TYPE_ARGS and // TYPE_RESARG, that need to do on the fly type customization // filtercheck_field_info m_customfield; private: int32_t extract_arg(string fldname, string val, OUT const struct ppm_param_info** parinfo); - int32_t gmt2local(time_t t); - void ts_to_string(uint64_t ts, OUT string* res, bool full, bool ns); + int32_t extract_type(string fldname, string val, OUT const struct ppm_param_info** parinfo); + uint8_t* extract_error_count(sinsp_evt *evt, OUT uint32_t* len); + uint8_t *extract_abspath(sinsp_evt *evt, OUT uint32_t *len); + inline uint8_t* extract_buflen(sinsp_evt *evt, OUT uint32_t* len); + + bool m_is_compare; + char* m_storage; + uint32_t m_storage_size; + const char* m_cargname; + sinsp_filter_check_reference* m_converter; }; // @@ -364,11 +502,13 @@ class sinsp_filter_check_user : public sinsp_filter_check TYPE_NAME = 1, TYPE_HOMEDIR = 2, TYPE_SHELL = 3, + TYPE_LOGINUID = 4, + TYPE_LOGINNAME = 5, }; sinsp_filter_check_user(); sinsp_filter_check* allocate_new(); - uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); uint32_t m_uid; string m_strval; @@ -388,12 +528,140 @@ class sinsp_filter_check_group : public sinsp_filter_check sinsp_filter_check_group(); sinsp_filter_check* allocate_new(); - uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); uint32_t m_gid; string m_name; }; +// +// Tracers +// +#define TEXT_ARG_ID -1000000 + +class sinsp_filter_check_tracer : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_ID = 0, + TYPE_TIME, + TYPE_NTAGS, + TYPE_NARGS, + TYPE_TAGS, + TYPE_TAG, + TYPE_ARGS, + TYPE_ARG, + TYPE_ENTERARGS, + TYPE_ENTERARG, + TYPE_DURATION, + TYPE_DURATION_QUANTIZED, + TYPE_DURATION_HUMAN, + TYPE_TAGDURATION, + TYPE_COUNT, + TYPE_TAGCOUNT, + TYPE_TAGCHILDSCOUNT, + TYPE_IDTAG, + TYPE_RAWTIME, + TYPE_RAWPARENTTIME, + }; + + sinsp_filter_check_tracer(); + ~sinsp_filter_check_tracer(); + sinsp_filter_check* allocate_new(); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + +private: + int32_t extract_arg(string fldname, string val, OUT const struct ppm_param_info** parinfo); + inline uint8_t* extract_duration(uint16_t etype, sinsp_tracerparser* eparser, OUT uint32_t* len); + uint8_t* extract_args(sinsp_partial_tracer* pae, OUT uint32_t *len); + uint8_t* extract_arg(sinsp_partial_tracer* pae, OUT uint32_t *len); + + int32_t m_argid; + string m_argname; + const char* m_cargname; + char* m_storage; + uint32_t m_storage_size; + int64_t m_s64val; + int32_t m_u32val; + sinsp_filter_check_reference* m_converter; + string m_strstorage; +}; + +// +// Events in tracers checks +// +class sinsp_filter_check_evtin : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_ID = 0, + TYPE_NTAGS, + TYPE_NARGS, + TYPE_TAGS, + TYPE_TAG, + TYPE_ARGS, + TYPE_ARG, + TYPE_P_ID, + TYPE_P_NTAGS, + TYPE_P_NARGS, + TYPE_P_TAGS, + TYPE_P_TAG, + TYPE_P_ARGS, + TYPE_P_ARG, + TYPE_S_ID, + TYPE_S_NTAGS, + TYPE_S_NARGS, + TYPE_S_TAGS, + TYPE_S_TAG, + TYPE_S_ARGS, + TYPE_S_ARG, + TYPE_M_ID, + TYPE_M_NTAGS, + TYPE_M_NARGS, + TYPE_M_TAGS, + TYPE_M_TAG, + TYPE_M_ARGS, + TYPE_M_ARG, + }; + + sinsp_filter_check_evtin(); + ~sinsp_filter_check_evtin(); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + sinsp_filter_check* allocate_new(); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + bool compare(sinsp_evt *evt); + + uint64_t m_u64val; + uint64_t m_tsdelta; + uint32_t m_u32val; + string m_strstorage; + string m_argname; + int32_t m_argid; + uint32_t m_evtid; + uint32_t m_evtid1; + const ppm_param_info* m_arginfo; + + // + // Note: this copy of the field is used by some fields, like TYPE_ARGS and + // TYPE_RESARG, that need to do on the fly type customization + // + filtercheck_field_info m_customfield; + +private: + int32_t extract_arg(string fldname, string val); + inline uint8_t* extract_tracer(sinsp_evt *evt, sinsp_partial_tracer* pae, OUT uint32_t* len); + inline bool compare_tracer(sinsp_evt *evt, sinsp_partial_tracer* pae); + + bool m_is_compare; + char* m_storage; + uint32_t m_storage_size; + const char* m_cargname; + sinsp_filter_check_reference* m_converter; +}; + // // Fake filter check used by the event formatter to render format text // @@ -403,16 +671,262 @@ class rawstring_check : public sinsp_filter_check rawstring_check(string text); sinsp_filter_check* allocate_new(); void set_text(string text); - int32_t parse_field_name(const char* str); - void parse_filter_value(const char* str); - uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); // XXX this is overkill and wasted for most of the fields. // It could be optimized by dynamically allocating the right amount - // of memory, but we don't care for the moment since we expect filters + // of memory, but we don't care for the moment since we expect filters // to be pretty small. string m_text; uint32_t m_text_len; }; +// +// syslog checks +// +class sinsp_decoder_syslog; + +class sinsp_filter_check_syslog : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_FACILITY_STR = 0, + TYPE_FACILITY, + TYPE_SEVERITY_STR, + TYPE_SEVERITY, + TYPE_MESSAGE, + }; + + sinsp_filter_check_syslog(); + sinsp_filter_check* allocate_new(); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + + sinsp_decoder_syslog* m_decoder; + uint32_t m_gid; + string m_name; +}; + +class sinsp_filter_check_container : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_CONTAINER_ID = 0, + TYPE_CONTAINER_NAME, + TYPE_CONTAINER_IMAGE, + TYPE_CONTAINER_IMAGE_ID, + TYPE_CONTAINER_TYPE, + TYPE_CONTAINER_PRIVILEGED, + TYPE_CONTAINER_MOUNTS, + TYPE_CONTAINER_MOUNT, + TYPE_CONTAINER_MOUNT_SOURCE, + TYPE_CONTAINER_MOUNT_DEST, + TYPE_CONTAINER_MOUNT_MODE, + TYPE_CONTAINER_MOUNT_RDWR, + TYPE_CONTAINER_MOUNT_PROPAGATION, + TYPE_CONTAINER_IMAGE_REPOSITORY, + TYPE_CONTAINER_IMAGE_TAG, + TYPE_CONTAINER_IMAGE_DIGEST, + TYPE_CONTAINER_HEALTHCHECK, + TYPE_CONTAINER_LIVENESS_PROBE, + TYPE_CONTAINER_READINESS_PROBE, + }; + + sinsp_filter_check_container(); + sinsp_filter_check* allocate_new(); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + +private: + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + int32_t extract_arg(const string& val, size_t basename); + + string m_tstr; + uint32_t m_u32val; + int32_t m_argid; + string m_argstr; +}; + +// +// For internal use +// +class sinsp_filter_check_reference : public sinsp_filter_check +{ +public: + enum alignment + { + ALIGN_LEFT, + ALIGN_RIGHT, + }; + + sinsp_filter_check_reference(); + sinsp_filter_check* allocate_new(); + inline void set_val(ppm_param_type type, uint8_t* val, + int32_t len, uint32_t cnt, + ppm_print_format print_format) + { + m_finfo.m_type = type; + m_val = val; + m_len = len; + m_cnt = cnt; + m_print_format = print_format; + } + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + char* tostring_nice(sinsp_evt* evt, uint32_t str_len, uint64_t time_delta); + Json::Value tojson(sinsp_evt* evt, uint32_t str_len, uint64_t time_delta); + +private: + inline char* format_bytes(double val, uint32_t str_len, bool is_int); + inline char* format_time(uint64_t val, uint32_t str_len); + char* print_double(uint8_t* rawval, uint32_t str_len); + char* print_int(uint8_t* rawval, uint32_t str_len); + + filtercheck_field_info m_finfo; + uint8_t* m_val; + uint32_t m_len; + double m_cnt; // For averages, this stores the entry count + ppm_print_format m_print_format; +}; + +// +// For internal use +// +class sinsp_filter_check_utils : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_CNT, + }; + + sinsp_filter_check_utils(); + sinsp_filter_check* allocate_new(); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + +private: + uint64_t m_cnt; +}; + +// +// fdlist checks +// +class sinsp_filter_check_fdlist : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_FDNUMS = 0, + TYPE_FDNAMES = 1, + TYPE_CLIENTIPS = 2, + TYPE_SERVERIPS = 3, + TYPE_CLIENTPORTS = 4, + TYPE_SERVERPORTS = 5, + }; + + sinsp_filter_check_fdlist(); + sinsp_filter_check* allocate_new(); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + +private: + string m_strval; + char m_addrbuff[100]; +}; + +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + +class sinsp_filter_check_k8s : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_K8S_POD_NAME = 0, + TYPE_K8S_POD_ID, + TYPE_K8S_POD_LABEL, + TYPE_K8S_POD_LABELS, + TYPE_K8S_RC_NAME, + TYPE_K8S_RC_ID, + TYPE_K8S_RC_LABEL, + TYPE_K8S_RC_LABELS, + TYPE_K8S_SVC_NAME, + TYPE_K8S_SVC_ID, + TYPE_K8S_SVC_LABEL, + TYPE_K8S_SVC_LABELS, + TYPE_K8S_NS_NAME, + TYPE_K8S_NS_ID, + TYPE_K8S_NS_LABEL, + TYPE_K8S_NS_LABELS, + TYPE_K8S_RS_NAME, + TYPE_K8S_RS_ID, + TYPE_K8S_RS_LABEL, + TYPE_K8S_RS_LABELS, + TYPE_K8S_DEPLOYMENT_NAME, + TYPE_K8S_DEPLOYMENT_ID, + TYPE_K8S_DEPLOYMENT_LABEL, + TYPE_K8S_DEPLOYMENT_LABELS, + }; + + sinsp_filter_check_k8s(); + sinsp_filter_check* allocate_new(); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + +private: + int32_t extract_arg(const string& fldname, const string& val); + const k8s_pod_t* find_pod_for_thread(const sinsp_threadinfo* tinfo); + const k8s_ns_t* find_ns_by_name(const string& ns_name); + const k8s_rc_t* find_rc_by_pod(const k8s_pod_t* pod); + const k8s_rs_t* find_rs_by_pod(const k8s_pod_t* pod); + vector find_svc_by_pod(const k8s_pod_t* pod); + const k8s_deployment_t* find_deployment_by_pod(const k8s_pod_t* pod); + void concatenate_labels(const k8s_pair_list& labels, string* s); + bool find_label(const k8s_pair_list& labels, const string& key, string* value); + + string m_argname; + string m_tstr; +}; + + + +class sinsp_filter_check_mesos : public sinsp_filter_check +{ +public: + enum check_type + { + TYPE_MESOS_TASK_NAME = 0, + TYPE_MESOS_TASK_ID, + TYPE_MESOS_TASK_LABEL, + TYPE_MESOS_TASK_LABELS, + TYPE_MESOS_FRAMEWORK_NAME, + TYPE_MESOS_FRAMEWORK_ID, + TYPE_MARATHON_APP_NAME, + TYPE_MARATHON_APP_ID, + TYPE_MARATHON_APP_LABEL, + TYPE_MARATHON_APP_LABELS, + TYPE_MARATHON_GROUP_NAME, + TYPE_MARATHON_GROUP_ID, + }; + + sinsp_filter_check_mesos(); + sinsp_filter_check* allocate_new(); + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering); + uint8_t* extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings = true); + +private: + + int32_t extract_arg(const string& fldname, const string& val); + mesos_task::ptr_t find_task_for_thread(const sinsp_threadinfo* tinfo); + const mesos_framework* find_framework_by_task(mesos_task::ptr_t task); + marathon_app::ptr_t find_app_by_task(mesos_task::ptr_t task); + marathon_group::ptr_t find_group_by_task(mesos_task::ptr_t task); + void concatenate_labels(const mesos_pair_list& labels, string* s); + bool find_label(const mesos_pair_list& labels, const string& key, string* value); + + string m_argname; + string m_tstr; +}; +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + #endif // HAS_FILTERING diff --git a/userspace/libsinsp/gen_filter.cpp b/userspace/libsinsp/gen_filter.cpp new file mode 100644 index 0000000000..ec9db3fa3b --- /dev/null +++ b/userspace/libsinsp/gen_filter.cpp @@ -0,0 +1,221 @@ +/* +Copyright (C) 2013-2014 Draios inc. + +This file is part of sysdig. + +sysdig is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License version 2 as +published by the Free Software Foundation. + +sysdig is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with sysdig. If not, see . +*/ + +#include +#include "stdint.h" +#include "gen_filter.h" +#include "sinsp.h" +#include "sinsp_int.h" + +gen_event::gen_event() +{ +} + +gen_event::~gen_event() +{ +} + +void gen_event::set_check_id(int32_t id) +{ + if (id) { + m_check_id = id; + } +} + +int32_t gen_event::get_check_id() const +{ + return m_check_id; +} + +gen_event_filter_check::gen_event_filter_check() +{ +} + +gen_event_filter_check::~gen_event_filter_check() +{ +} + +void gen_event_filter_check::set_check_id(int32_t id) +{ + m_check_id = id; +} + +int32_t gen_event_filter_check::get_check_id() +{ + return m_check_id; +} + +/////////////////////////////////////////////////////////////////////////////// +// gen_event_filter_expression implementation +/////////////////////////////////////////////////////////////////////////////// +gen_event_filter_expression::gen_event_filter_expression() +{ + m_parent = NULL; +} + +gen_event_filter_expression::~gen_event_filter_expression() +{ + uint32_t j; + + for(j = 0; j < m_checks.size(); j++) + { + delete m_checks[j]; + } +} + +void gen_event_filter_expression::add_check(gen_event_filter_check* chk) +{ + m_checks.push_back(chk); +} + +bool gen_event_filter_expression::compare(gen_event *evt) +{ + uint32_t j; + uint32_t size = (uint32_t)m_checks.size(); + bool res = true; + gen_event_filter_check* chk = NULL; + + for(j = 0; j < size; j++) + { + chk = m_checks[j]; + ASSERT(chk != NULL); + + if(j == 0) + { + switch(chk->m_boolop) + { + case BO_NONE: + res = chk->compare(evt); + if (res) { + evt->set_check_id(chk->get_check_id()); + } + break; + case BO_NOT: + res = !chk->compare(evt); + break; + default: + ASSERT(false); + break; + } + } + else + { + switch(chk->m_boolop) + { + case BO_OR: + if(res) + { + goto done; + } + res = chk->compare(evt); + if (res) { + evt->set_check_id(chk->get_check_id()); + } + break; + case BO_AND: + if(!res) + { + goto done; + } + res = chk->compare(evt); + if (res) { + evt->set_check_id(chk->get_check_id()); + } + break; + case BO_ORNOT: + if(res) + { + goto done; + } + res = !chk->compare(evt); + if (res) { + evt->set_check_id(chk->get_check_id()); + } + break; + case BO_ANDNOT: + if(!res) + { + goto done; + } + res = !chk->compare(evt); + if (res) { + evt->set_check_id(chk->get_check_id()); + } + break; + default: + ASSERT(false); + break; + } + } + } + done: + + return res; +} + +uint8_t *gen_event_filter_expression::extract(gen_event *evt, uint32_t *len, bool sanitize_strings) +{ + return NULL; +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_filter implementation +/////////////////////////////////////////////////////////////////////////////// +gen_event_filter::gen_event_filter() +{ + m_filter = new gen_event_filter_expression(); + m_curexpr = m_filter; + +} + +gen_event_filter::~gen_event_filter() +{ + if(m_filter) + { + delete m_filter; + } +} + +void gen_event_filter::push_expression(boolop op) +{ + gen_event_filter_expression* newexpr = new gen_event_filter_expression(); + newexpr->m_boolop = op; + newexpr->m_parent = m_curexpr; + + add_check((gen_event_filter_check*)newexpr); + m_curexpr = newexpr; +} + +void gen_event_filter::pop_expression() +{ + ASSERT(m_curexpr->m_parent != NULL); + + m_curexpr = m_curexpr->m_parent; +} + +bool gen_event_filter::run(gen_event *evt) +{ + return m_filter->compare(evt); +} + +void gen_event_filter::add_check(gen_event_filter_check* chk) +{ + m_curexpr->add_check((gen_event_filter_check *) chk); +} + + diff --git a/userspace/libsinsp/gen_filter.h b/userspace/libsinsp/gen_filter.h new file mode 100644 index 0000000000..dbefc223e7 --- /dev/null +++ b/userspace/libsinsp/gen_filter.h @@ -0,0 +1,201 @@ +/* +Copyright (C) 2013-2018 Draios inc. + +This file is part of sysdig. + +sysdig is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License version 2 as +published by the Free Software Foundation. + +sysdig is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with sysdig. If not, see . +*/ + +#pragma once + +#include + +/* + * Operators to compare events + */ +enum cmpop { + CO_NONE = 0, + CO_EQ = 1, + CO_NE = 2, + CO_LT = 3, + CO_LE = 4, + CO_GT = 5, + CO_GE = 6, + CO_CONTAINS = 7, + CO_IN = 8, + CO_EXISTS = 9, + CO_ICONTAINS = 10, + CO_STARTSWITH = 11, + CO_GLOB = 12, + CO_PMATCH = 13, + CO_ENDSWITH = 14, + CO_INTERSECTS = 15, +}; + +enum boolop +{ + BO_NONE = 0, + BO_NOT = 1, + BO_OR = 2, + BO_AND = 4, + + // obtained by bitwise OR'ing with one of above ops + BO_ORNOT = 3, + BO_ANDNOT = 5, +}; + +enum evt_src +{ + ESRC_NONE = 0, + ESRC_SINSP = 1, + ESRC_K8S_AUDIT = 2, + ESRC_MAX = 3, +}; + +class gen_event +{ +public: + gen_event(); + virtual ~gen_event(); + + /*! + \brief Set an opaque "check id", corresponding to the id of the last filtercheck that matched this event. + */ + void set_check_id(int32_t id); + + /*! + \brief Get the opaque "check id" (-1 if not set). + */ + int32_t get_check_id() const; + + // Every event must expose a timestamp + virtual uint64_t get_ts() const = 0; + + /*! + \brief Get the source of the event. + */ + virtual uint16_t get_source() const = 0; + + /*! + \brief Get the type of the event. + */ + virtual uint16_t get_type() const = 0; + +private: + int32_t m_check_id = 0; + +}; + + +class gen_event_filter_check +{ +public: + gen_event_filter_check(); + virtual ~gen_event_filter_check(); + + boolop m_boolop; + cmpop m_cmpop; + + virtual int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) = 0; + virtual void add_filter_value(const char* str, uint32_t len, uint32_t i = 0 ) = 0; + virtual bool compare(gen_event *evt) = 0; + virtual uint8_t* extract(gen_event *evt, uint32_t* len, bool sanitize_strings = true) = 0; + + // + // Configure numeric id to be set on events that match this filter + // + void set_check_id(int32_t id); + virtual int32_t get_check_id(); + +private: + int32_t m_check_id = 0; + +}; + +/////////////////////////////////////////////////////////////////////////////// +// Filter expression class +// A filter expression contains multiple filters connected by boolean expressions, +// e.g. "check or check", "check and check and check", "not check" +/////////////////////////////////////////////////////////////////////////////// + +class gen_event_filter_expression : public gen_event_filter_check +{ +public: + gen_event_filter_expression(); + virtual ~gen_event_filter_expression(); + + // + // The following methods are part of the filter check interface but are irrelevant + // for this class, because they are used only for the leaves of the filtering tree. + // + int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) + { + return 0; + } + + void add_filter_value(const char* str, uint32_t len, uint32_t i = 0 ) + { + return; + } + + void add_check(gen_event_filter_check* chk); + + bool compare(gen_event *evt); + + uint8_t* extract(gen_event *evt, uint32_t* len, bool sanitize_strings = true); + + gen_event_filter_expression* m_parent; + std::vector m_checks; +}; + + + +class gen_event_filter +{ +public: + gen_event_filter(); + + virtual ~gen_event_filter(); + + /*! + \brief Applies the filter to the given event. + + \param evt Pointer that needs to be filtered. + \return true if the event is accepted by the filter, false if it's rejected. + */ + bool run(gen_event *evt); + void push_expression(boolop op); + void pop_expression(); + void add_check(gen_event_filter_check* chk); + +protected: + gen_event_filter_expression* m_curexpr; + gen_event_filter_expression* m_filter; + +}; + +class gen_event_filter_factory +{ +public: + + gen_event_filter_factory() {}; + virtual ~gen_event_filter_factory() {}; + + // Create a new filter + virtual gen_event_filter *new_filter() = 0; + + // Create a new filtercheck + virtual gen_event_filter_check *new_filtercheck(const char *fldname) = 0; +}; + + diff --git a/userspace/libsinsp/grpc_channel_registry.cpp b/userspace/libsinsp/grpc_channel_registry.cpp new file mode 100644 index 0000000000..6fc7ebfdf5 --- /dev/null +++ b/userspace/libsinsp/grpc_channel_registry.cpp @@ -0,0 +1,50 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "grpc_channel_registry.h" + + +std::map> libsinsp::grpc_channel_registry::s_channels; + +std::shared_ptr libsinsp::grpc_channel_registry::get_channel(const std::string &url, const grpc::ChannelArguments *args) +{ + std::shared_ptr chan; + auto it = s_channels.find(url); + if(it != s_channels.end()) + { + chan = it->second.lock(); + if (chan != nullptr) + { + return chan; + } + } + if (args) + { + chan = grpc::CreateCustomChannel(url, + grpc::InsecureChannelCredentials(), *args); + } + else + { + chan = grpc::CreateChannel(url, + grpc::InsecureChannelCredentials()); + } + s_channels[url] = chan; + + return chan; +} diff --git a/userspace/libsinsp/grpc_channel_registry.h b/userspace/libsinsp/grpc_channel_registry.h new file mode 100644 index 0000000000..cd16c08817 --- /dev/null +++ b/userspace/libsinsp/grpc_channel_registry.h @@ -0,0 +1,41 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#ifdef GRPC_INCLUDE_IS_GRPCPP +# include +#else +# include +#endif + +namespace libsinsp +{ +class grpc_channel_registry +{ +public: + // Return a (shared) grpc::Channel for the provided url. + static std::shared_ptr get_channel(const std::string &url, + const grpc::ChannelArguments *args = nullptr); + +private: + static std::map> s_channels; +}; +} diff --git a/userspace/libsinsp/http_parser.c b/userspace/libsinsp/http_parser.c new file mode 100644 index 0000000000..23bf83d8e6 --- /dev/null +++ b/userspace/libsinsp/http_parser.c @@ -0,0 +1,2470 @@ +/* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev + * + * Additional changes are licensed under the same terms as NGINX and + * copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include "http_parser.h" +#include +#include +#include +#include +#include +#include + +#ifndef ULLONG_MAX +# define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */ +#endif + +#ifndef MIN +# define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#endif + +#ifndef BIT_AT +# define BIT_AT(a, i) \ + (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \ + (1 << ((unsigned int) (i) & 7)))) +#endif + +#ifndef ELEM_AT +# define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v)) +#endif + +#define SET_ERRNO(e) \ +do { \ + parser->http_errno = (e); \ +} while(0) + +#define CURRENT_STATE() p_state +#define UPDATE_STATE(V) p_state = (enum state) (V); +#define RETURN(V) \ +do { \ + parser->state = CURRENT_STATE(); \ + return (V); \ +} while (0); +#define REEXECUTE() \ + goto reexecute; \ + + +#ifdef __GNUC__ +# define LIKELY(X) __builtin_expect(!!(X), 1) +# define UNLIKELY(X) __builtin_expect(!!(X), 0) +#else +# define LIKELY(X) (X) +# define UNLIKELY(X) (X) +#endif + + +/* Run the notify callback FOR, returning ER if it fails */ +#define CALLBACK_NOTIFY_(FOR, ER) \ +do { \ + assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ + \ + if (LIKELY(settings->on_##FOR)) { \ + parser->state = CURRENT_STATE(); \ + if (UNLIKELY(0 != settings->on_##FOR(parser))) { \ + SET_ERRNO(HPE_CB_##FOR); \ + } \ + UPDATE_STATE(parser->state); \ + \ + /* We either errored above or got paused; get out */ \ + if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \ + return (ER); \ + } \ + } \ +} while (0) + +/* Run the notify callback FOR and consume the current byte */ +#define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1) + +/* Run the notify callback FOR and don't consume the current byte */ +#define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data) + +/* Run data callback FOR with LEN bytes, returning ER if it fails */ +#define CALLBACK_DATA_(FOR, LEN, ER) \ +do { \ + assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ + \ + if (FOR##_mark) { \ + if (LIKELY(settings->on_##FOR)) { \ + parser->state = CURRENT_STATE(); \ + if (UNLIKELY(0 != \ + settings->on_##FOR(parser, FOR##_mark, (LEN)))) { \ + SET_ERRNO(HPE_CB_##FOR); \ + } \ + UPDATE_STATE(parser->state); \ + \ + /* We either errored above or got paused; get out */ \ + if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \ + return (ER); \ + } \ + } \ + FOR##_mark = NULL; \ + } \ +} while (0) + +/* Run the data callback FOR and consume the current byte */ +#define CALLBACK_DATA(FOR) \ + CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1) + +/* Run the data callback FOR and don't consume the current byte */ +#define CALLBACK_DATA_NOADVANCE(FOR) \ + CALLBACK_DATA_(FOR, p - FOR##_mark, p - data) + +/* Set the mark FOR; non-destructive if mark is already set */ +#define MARK(FOR) \ +do { \ + if (!FOR##_mark) { \ + FOR##_mark = p; \ + } \ +} while (0) + +/* Don't allow the total size of the HTTP headers (including the status + * line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect + * embedders against denial-of-service attacks where the attacker feeds + * us a never-ending header that the embedder keeps buffering. + * + * This check is arguably the responsibility of embedders but we're doing + * it on the embedder's behalf because most won't bother and this way we + * make the web a little safer. HTTP_MAX_HEADER_SIZE is still far bigger + * than any reasonable request or response so this should never affect + * day-to-day operation. + */ +#define COUNT_HEADER_SIZE(V) \ +do { \ + parser->nread += (V); \ + if (UNLIKELY(parser->nread > (HTTP_MAX_HEADER_SIZE))) { \ + SET_ERRNO(HPE_HEADER_OVERFLOW); \ + goto error; \ + } \ +} while (0) + + +#define PROXY_CONNECTION "proxy-connection" +#define CONNECTION "connection" +#define CONTENT_LENGTH "content-length" +#define TRANSFER_ENCODING "transfer-encoding" +#define UPGRADE "upgrade" +#define CHUNKED "chunked" +#define KEEP_ALIVE "keep-alive" +#define CLOSE "close" + + +static const char *method_strings[] = + { +#define XX(num, name, string) #string, + HTTP_METHOD_MAP(XX) +#undef XX + }; + + +/* Tokens as defined by rfc 2616. Also lowercases them. + * token = 1* + * separators = "(" | ")" | "<" | ">" | "@" + * | "," | ";" | ":" | "\" | <"> + * | "/" | "[" | "]" | "?" | "=" + * | "{" | "}" | SP | HT + */ +static const char tokens[256] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0, '!', 0, '#', '$', '%', '&', '\'', +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 0, 0, '*', '+', 0, '-', '.', 0, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + '0', '1', '2', '3', '4', '5', '6', '7', +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + '8', '9', 0, 0, 0, 0, 0, 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 'x', 'y', 'z', 0, 0, 0, '^', '_', +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 'x', 'y', 'z', 0, '|', 0, '~', 0 }; + + +static const int8_t unhex[256] = + {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + }; + + +#if HTTP_PARSER_STRICT +# define T(v) 0 +#else +# define T(v) v +#endif + + +static const uint8_t normal_url_char[32] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128, +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, }; + +#undef T + +enum state + { s_dead = 1 /* important that this is > 0 */ + + , s_start_req_or_res + , s_res_or_resp_H + , s_start_res + , s_res_H + , s_res_HT + , s_res_HTT + , s_res_HTTP + , s_res_first_http_major + , s_res_http_major + , s_res_first_http_minor + , s_res_http_minor + , s_res_first_status_code + , s_res_status_code + , s_res_status_start + , s_res_status + , s_res_line_almost_done + + , s_start_req + + , s_req_method + , s_req_spaces_before_url + , s_req_schema + , s_req_schema_slash + , s_req_schema_slash_slash + , s_req_server_start + , s_req_server + , s_req_server_with_at + , s_req_path + , s_req_query_string_start + , s_req_query_string + , s_req_fragment_start + , s_req_fragment + , s_req_http_start + , s_req_http_H + , s_req_http_HT + , s_req_http_HTT + , s_req_http_HTTP + , s_req_first_http_major + , s_req_http_major + , s_req_first_http_minor + , s_req_http_minor + , s_req_line_almost_done + + , s_header_field_start + , s_header_field + , s_header_value_discard_ws + , s_header_value_discard_ws_almost_done + , s_header_value_discard_lws + , s_header_value_start + , s_header_value + , s_header_value_lws + + , s_header_almost_done + + , s_chunk_size_start + , s_chunk_size + , s_chunk_parameters + , s_chunk_size_almost_done + + , s_headers_almost_done + , s_headers_done + + /* Important: 's_headers_done' must be the last 'header' state. All + * states beyond this must be 'body' states. It is used for overflow + * checking. See the PARSING_HEADER() macro. + */ + + , s_chunk_data + , s_chunk_data_almost_done + , s_chunk_data_done + + , s_body_identity + , s_body_identity_eof + + , s_message_done + }; + + +#define PARSING_HEADER(state) (state <= s_headers_done) + + +enum header_states + { h_general = 0 + , h_C + , h_CO + , h_CON + + , h_matching_connection + , h_matching_proxy_connection + , h_matching_content_length + , h_matching_transfer_encoding + , h_matching_upgrade + + , h_connection + , h_content_length + , h_transfer_encoding + , h_upgrade + + , h_matching_transfer_encoding_chunked + , h_matching_connection_token_start + , h_matching_connection_keep_alive + , h_matching_connection_close + , h_matching_connection_upgrade + , h_matching_connection_token + + , h_transfer_encoding_chunked + , h_connection_keep_alive + , h_connection_close + , h_connection_upgrade + }; + +enum http_host_state + { + s_http_host_dead = 1 + , s_http_userinfo_start + , s_http_userinfo + , s_http_host_start + , s_http_host_v6_start + , s_http_host + , s_http_host_v6 + , s_http_host_v6_end + , s_http_host_v6_zone_start + , s_http_host_v6_zone + , s_http_host_port_start + , s_http_host_port +}; + +/* Macros for character classes; depends on strict-mode */ +#define CR '\r' +#define LF '\n' +#define LOWER(c) (unsigned char)(c | 0x20) +#define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z') +#define IS_NUM(c) ((c) >= '0' && (c) <= '9') +#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) +#define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f')) +#define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \ + (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \ + (c) == ')') +#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \ + (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \ + (c) == '$' || (c) == ',') + +#define STRICT_TOKEN(c) (tokens[(unsigned char)c]) + +#if HTTP_PARSER_STRICT +#define TOKEN(c) (tokens[(unsigned char)c]) +#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c)) +#define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-') +#else +#define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c]) +#define IS_URL_CHAR(c) \ + (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80)) +#define IS_HOST_CHAR(c) \ + (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_') +#endif + +/** + * Verify that a char is a valid visible (printable) US-ASCII + * character or %x80-FF + **/ +#define IS_HEADER_CHAR(ch) \ + (ch == CR || ch == LF || ch == 9 || ((unsigned char)ch > 31 && ch != 127)) + +#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res) + + +#if HTTP_PARSER_STRICT +# define STRICT_CHECK(cond) \ +do { \ + if (cond) { \ + SET_ERRNO(HPE_STRICT); \ + goto error; \ + } \ +} while (0) +# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead) +#else +# define STRICT_CHECK(cond) +# define NEW_MESSAGE() start_state +#endif + + +/* Map errno values to strings for human-readable output */ +#define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s }, +static struct { + const char *name; + const char *description; +} http_strerror_tab[] = { + HTTP_ERRNO_MAP(HTTP_STRERROR_GEN) +}; +#undef HTTP_STRERROR_GEN + +int http_message_needs_eof(const http_parser *parser); + +/* Our URL parser. + * + * This is designed to be shared by http_parser_execute() for URL validation, + * hence it has a state transition + byte-for-byte interface. In addition, it + * is meant to be embedded in http_parser_parse_url(), which does the dirty + * work of turning state transitions URL components for its API. + * + * This function should only be invoked with non-space characters. It is + * assumed that the caller cares about (and can detect) the transition between + * URL and non-URL states by looking for these. + */ +static enum state +parse_url_char(enum state s, const char ch) +{ + if (ch == ' ' || ch == '\r' || ch == '\n') { + return s_dead; + } + +#if HTTP_PARSER_STRICT + if (ch == '\t' || ch == '\f') { + return s_dead; + } +#endif + + switch (s) { + case s_req_spaces_before_url: + /* Proxied requests are followed by scheme of an absolute URI (alpha). + * All methods except CONNECT are followed by '/' or '*'. + */ + + if (ch == '/' || ch == '*') { + return s_req_path; + } + + if (IS_ALPHA(ch)) { + return s_req_schema; + } + + break; + + case s_req_schema: + if (IS_ALPHA(ch)) { + return s; + } + + if (ch == ':') { + return s_req_schema_slash; + } + + break; + + case s_req_schema_slash: + if (ch == '/') { + return s_req_schema_slash_slash; + } + + break; + + case s_req_schema_slash_slash: + if (ch == '/') { + return s_req_server_start; + } + + break; + + case s_req_server_with_at: + if (ch == '@') { + return s_dead; + } + + /* FALLTHROUGH */ + case s_req_server_start: + case s_req_server: + if (ch == '/') { + return s_req_path; + } + + if (ch == '?') { + return s_req_query_string_start; + } + + if (ch == '@') { + return s_req_server_with_at; + } + + if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') { + return s_req_server; + } + + break; + + case s_req_path: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + return s_req_query_string_start; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_query_string_start: + case s_req_query_string: + if (IS_URL_CHAR(ch)) { + return s_req_query_string; + } + + switch (ch) { + case '?': + /* allow extra '?' in query string */ + return s_req_query_string; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_fragment_start: + if (IS_URL_CHAR(ch)) { + return s_req_fragment; + } + + switch (ch) { + case '?': + return s_req_fragment; + + case '#': + return s; + } + + break; + + case s_req_fragment: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + case '#': + return s; + } + + break; + + default: + break; + } + + /* We should never fall out of the switch above unless there's an error */ + return s_dead; +} + +size_t http_parser_execute (http_parser *parser, + const http_parser_settings *settings, + const char *data, + size_t len) +{ + char c, ch; + int8_t unhex_val; + const char *p = data; + const char *header_field_mark = 0; + const char *header_value_mark = 0; + const char *url_mark = 0; + const char *body_mark = 0; + const char *status_mark = 0; + enum state p_state = (enum state) parser->state; + const unsigned int lenient = parser->lenient_http_headers; + + /* We're in an error state. Don't bother doing anything. */ + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { + return 0; + } + + if (len == 0) { + switch (CURRENT_STATE()) { + case s_body_identity_eof: + /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if + * we got paused. + */ + CALLBACK_NOTIFY_NOADVANCE(message_complete); + return 0; + + case s_dead: + case s_start_req_or_res: + case s_start_res: + case s_start_req: + return 0; + + default: + SET_ERRNO(HPE_INVALID_EOF_STATE); + return 1; + } + } + + + if (CURRENT_STATE() == s_header_field) + header_field_mark = data; + if (CURRENT_STATE() == s_header_value) + header_value_mark = data; + switch (CURRENT_STATE()) { + case s_req_path: + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_server: + case s_req_server_with_at: + case s_req_query_string_start: + case s_req_query_string: + case s_req_fragment_start: + case s_req_fragment: + url_mark = data; + break; + case s_res_status: + status_mark = data; + break; + default: + break; + } + + for (p=data; p != data + len; p++) { + ch = *p; + + if (PARSING_HEADER(CURRENT_STATE())) + COUNT_HEADER_SIZE(1); + +reexecute: + switch (CURRENT_STATE()) { + + case s_dead: + /* this state is used after a 'Connection: close' message + * the parser will error out if it reads another message + */ + if (LIKELY(ch == CR || ch == LF)) + break; + + SET_ERRNO(HPE_CLOSED_CONNECTION); + goto error; + + case s_start_req_or_res: + { + if (ch == CR || ch == LF) + break; + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + if (ch == 'H') { + UPDATE_STATE(s_res_or_resp_H); + + CALLBACK_NOTIFY(message_begin); + } else { + parser->type = HTTP_REQUEST; + UPDATE_STATE(s_start_req); + REEXECUTE(); + } + + break; + } + + case s_res_or_resp_H: + if (ch == 'T') { + parser->type = HTTP_RESPONSE; + UPDATE_STATE(s_res_HT); + } else { + if (UNLIKELY(ch != 'E')) { + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + + parser->type = HTTP_REQUEST; + parser->method = HTTP_HEAD; + parser->index = 2; + UPDATE_STATE(s_req_method); + } + break; + + case s_start_res: + { + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + switch (ch) { + case 'H': + UPDATE_STATE(s_res_H); + break; + + case CR: + case LF: + break; + + default: + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + + CALLBACK_NOTIFY(message_begin); + break; + } + + case s_res_H: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_res_HT); + break; + + case s_res_HT: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_res_HTT); + break; + + case s_res_HTT: + STRICT_CHECK(ch != 'P'); + UPDATE_STATE(s_res_HTTP); + break; + + case s_res_HTTP: + STRICT_CHECK(ch != '/'); + UPDATE_STATE(s_res_first_http_major); + break; + + case s_res_first_http_major: + if (UNLIKELY(ch < '0' || ch > '9')) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major = ch - '0'; + UPDATE_STATE(s_res_http_major); + break; + + /* major HTTP version or dot */ + case s_res_http_major: + { + if (ch == '.') { + UPDATE_STATE(s_res_first_http_minor); + break; + } + + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major *= 10; + parser->http_major += ch - '0'; + + if (UNLIKELY(parser->http_major > 999)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + /* first digit of minor HTTP version */ + case s_res_first_http_minor: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor = ch - '0'; + UPDATE_STATE(s_res_http_minor); + break; + + /* minor HTTP version or end of request line */ + case s_res_http_minor: + { + if (ch == ' ') { + UPDATE_STATE(s_res_first_status_code); + break; + } + + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor *= 10; + parser->http_minor += ch - '0'; + + if (UNLIKELY(parser->http_minor > 999)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + case s_res_first_status_code: + { + if (!IS_NUM(ch)) { + if (ch == ' ') { + break; + } + + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + parser->status_code = ch - '0'; + UPDATE_STATE(s_res_status_code); + break; + } + + case s_res_status_code: + { + if (!IS_NUM(ch)) { + switch (ch) { + case ' ': + UPDATE_STATE(s_res_status_start); + break; + case CR: + UPDATE_STATE(s_res_line_almost_done); + break; + case LF: + UPDATE_STATE(s_header_field_start); + break; + default: + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + break; + } + + parser->status_code *= 10; + parser->status_code += ch - '0'; + + if (UNLIKELY(parser->status_code > 999)) { + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + + break; + } + + case s_res_status_start: + { + if (ch == CR) { + UPDATE_STATE(s_res_line_almost_done); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_field_start); + break; + } + + MARK(status); + UPDATE_STATE(s_res_status); + parser->index = 0; + break; + } + + case s_res_status: + if (ch == CR) { + UPDATE_STATE(s_res_line_almost_done); + CALLBACK_DATA(status); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_field_start); + CALLBACK_DATA(status); + break; + } + + break; + + case s_res_line_almost_done: + STRICT_CHECK(ch != LF); + UPDATE_STATE(s_header_field_start); + break; + + case s_start_req: + { + if (ch == CR || ch == LF) + break; + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + if (UNLIKELY(!IS_ALPHA(ch))) { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + parser->method = (enum http_method) 0; + parser->index = 1; + switch (ch) { + case 'A': parser->method = HTTP_ACL; break; + case 'B': parser->method = HTTP_BIND; break; + case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break; + case 'D': parser->method = HTTP_DELETE; break; + case 'G': parser->method = HTTP_GET; break; + case 'H': parser->method = HTTP_HEAD; break; + case 'L': parser->method = HTTP_LOCK; /* or LINK */ break; + case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break; + case 'N': parser->method = HTTP_NOTIFY; break; + case 'O': parser->method = HTTP_OPTIONS; break; + case 'P': parser->method = HTTP_POST; + /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */ + break; + case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break; + case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break; + case 'T': parser->method = HTTP_TRACE; break; + case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break; + default: + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + UPDATE_STATE(s_req_method); + + CALLBACK_NOTIFY(message_begin); + + break; + } + + case s_req_method: + { + const char *matcher; + if (UNLIKELY(ch == '\0')) { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + matcher = method_strings[parser->method]; + if (ch == ' ' && matcher[parser->index] == '\0') { + UPDATE_STATE(s_req_spaces_before_url); + } else if (ch == matcher[parser->index]) { + ; /* nada */ + } else if (IS_ALPHA(ch)) { + + switch (parser->method << 16 | parser->index << 8 | ch) { +#define XX(meth, pos, ch, new_meth) \ + case (HTTP_##meth << 16 | pos << 8 | ch): \ + parser->method = HTTP_##new_meth; break; + + XX(POST, 1, 'U', PUT) + XX(POST, 1, 'A', PATCH) + XX(CONNECT, 1, 'H', CHECKOUT) + XX(CONNECT, 2, 'P', COPY) + XX(MKCOL, 1, 'O', MOVE) + XX(MKCOL, 1, 'E', MERGE) + XX(MKCOL, 2, 'A', MKACTIVITY) + XX(MKCOL, 3, 'A', MKCALENDAR) + XX(SUBSCRIBE, 1, 'E', SEARCH) + XX(REPORT, 2, 'B', REBIND) + XX(POST, 1, 'R', PROPFIND) + XX(PROPFIND, 4, 'P', PROPPATCH) + XX(PUT, 2, 'R', PURGE) + XX(LOCK, 1, 'I', LINK) + XX(UNLOCK, 2, 'S', UNSUBSCRIBE) + XX(UNLOCK, 2, 'B', UNBIND) + XX(UNLOCK, 3, 'I', UNLINK) +#undef XX + + default: + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + } else if (ch == '-' && + parser->index == 1 && + parser->method == HTTP_MKCOL) { + parser->method = HTTP_MSEARCH; + } else { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + ++parser->index; + break; + } + + case s_req_spaces_before_url: + { + if (ch == ' ') break; + + MARK(url); + if (parser->method == HTTP_CONNECT) { + UPDATE_STATE(s_req_server_start); + } + + UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch)); + if (UNLIKELY(CURRENT_STATE() == s_dead)) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + + break; + } + + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + { + switch (ch) { + /* No whitespace allowed here */ + case ' ': + case CR: + case LF: + SET_ERRNO(HPE_INVALID_URL); + goto error; + default: + UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch)); + if (UNLIKELY(CURRENT_STATE() == s_dead)) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + } + + break; + } + + case s_req_server: + case s_req_server_with_at: + case s_req_path: + case s_req_query_string_start: + case s_req_query_string: + case s_req_fragment_start: + case s_req_fragment: + { + switch (ch) { + case ' ': + UPDATE_STATE(s_req_http_start); + CALLBACK_DATA(url); + break; + case CR: + case LF: + parser->http_major = 0; + parser->http_minor = 9; + UPDATE_STATE((ch == CR) ? + s_req_line_almost_done : + s_header_field_start); + CALLBACK_DATA(url); + break; + default: + UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch)); + if (UNLIKELY(CURRENT_STATE() == s_dead)) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + } + break; + } + + case s_req_http_start: + switch (ch) { + case 'H': + UPDATE_STATE(s_req_http_H); + break; + case ' ': + break; + default: + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + break; + + case s_req_http_H: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_req_http_HT); + break; + + case s_req_http_HT: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_req_http_HTT); + break; + + case s_req_http_HTT: + STRICT_CHECK(ch != 'P'); + UPDATE_STATE(s_req_http_HTTP); + break; + + case s_req_http_HTTP: + STRICT_CHECK(ch != '/'); + UPDATE_STATE(s_req_first_http_major); + break; + + /* first digit of major HTTP version */ + case s_req_first_http_major: + if (UNLIKELY(ch < '1' || ch > '9')) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major = ch - '0'; + UPDATE_STATE(s_req_http_major); + break; + + /* major HTTP version or dot */ + case s_req_http_major: + { + if (ch == '.') { + UPDATE_STATE(s_req_first_http_minor); + break; + } + + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major *= 10; + parser->http_major += ch - '0'; + + if (UNLIKELY(parser->http_major > 999)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + /* first digit of minor HTTP version */ + case s_req_first_http_minor: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor = ch - '0'; + UPDATE_STATE(s_req_http_minor); + break; + + /* minor HTTP version or end of request line */ + case s_req_http_minor: + { + if (ch == CR) { + UPDATE_STATE(s_req_line_almost_done); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_field_start); + break; + } + + /* XXX allow spaces after digit? */ + + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor *= 10; + parser->http_minor += ch - '0'; + + if (UNLIKELY(parser->http_minor > 999)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + /* end of request line */ + case s_req_line_almost_done: + { + if (UNLIKELY(ch != LF)) { + SET_ERRNO(HPE_LF_EXPECTED); + goto error; + } + + UPDATE_STATE(s_header_field_start); + break; + } + + case s_header_field_start: + { + if (ch == CR) { + UPDATE_STATE(s_headers_almost_done); + break; + } + + if (ch == LF) { + /* they might be just sending \n instead of \r\n so this would be + * the second \n to denote the end of headers*/ + UPDATE_STATE(s_headers_almost_done); + REEXECUTE(); + } + + c = TOKEN(ch); + + if (UNLIKELY(!c)) { + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + MARK(header_field); + + parser->index = 0; + UPDATE_STATE(s_header_field); + + switch (c) { + case 'c': + parser->header_state = h_C; + break; + + case 'p': + parser->header_state = h_matching_proxy_connection; + break; + + case 't': + parser->header_state = h_matching_transfer_encoding; + break; + + case 'u': + parser->header_state = h_matching_upgrade; + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_field: + { + const char* start = p; + for (; p != data + len; p++) { + ch = *p; + c = TOKEN(ch); + + if (!c) + break; + + switch (parser->header_state) { + case h_general: + break; + + case h_C: + parser->index++; + parser->header_state = (c == 'o' ? h_CO : h_general); + break; + + case h_CO: + parser->index++; + parser->header_state = (c == 'n' ? h_CON : h_general); + break; + + case h_CON: + parser->index++; + switch (c) { + case 'n': + parser->header_state = h_matching_connection; + break; + case 't': + parser->header_state = h_matching_content_length; + break; + default: + parser->header_state = h_general; + break; + } + break; + + /* connection */ + + case h_matching_connection: + parser->index++; + if (parser->index > sizeof(CONNECTION)-1 + || c != CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* proxy-connection */ + + case h_matching_proxy_connection: + parser->index++; + if (parser->index > sizeof(PROXY_CONNECTION)-1 + || c != PROXY_CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(PROXY_CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* content-length */ + + case h_matching_content_length: + parser->index++; + if (parser->index > sizeof(CONTENT_LENGTH)-1 + || c != CONTENT_LENGTH[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CONTENT_LENGTH)-2) { + parser->header_state = h_content_length; + } + break; + + /* transfer-encoding */ + + case h_matching_transfer_encoding: + parser->index++; + if (parser->index > sizeof(TRANSFER_ENCODING)-1 + || c != TRANSFER_ENCODING[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) { + parser->header_state = h_transfer_encoding; + } + break; + + /* upgrade */ + + case h_matching_upgrade: + parser->index++; + if (parser->index > sizeof(UPGRADE)-1 + || c != UPGRADE[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(UPGRADE)-2) { + parser->header_state = h_upgrade; + } + break; + + case h_connection: + case h_content_length: + case h_transfer_encoding: + case h_upgrade: + if (ch != ' ') parser->header_state = h_general; + break; + + default: + assert(0 && "Unknown header_state"); + break; + } + } + + COUNT_HEADER_SIZE(p - start); + + if (p == data + len) { + --p; + break; + } + + if (ch == ':') { + UPDATE_STATE(s_header_value_discard_ws); + CALLBACK_DATA(header_field); + break; + } + + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + case s_header_value_discard_ws: + if (ch == ' ' || ch == '\t') break; + + if (ch == CR) { + UPDATE_STATE(s_header_value_discard_ws_almost_done); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_value_discard_lws); + break; + } + + /* FALLTHROUGH */ + + case s_header_value_start: + { + MARK(header_value); + + UPDATE_STATE(s_header_value); + parser->index = 0; + + c = LOWER(ch); + + switch (parser->header_state) { + case h_upgrade: + parser->flags |= F_UPGRADE; + parser->header_state = h_general; + break; + + case h_transfer_encoding: + /* looking for 'Transfer-Encoding: chunked' */ + if ('c' == c) { + parser->header_state = h_matching_transfer_encoding_chunked; + } else { + parser->header_state = h_general; + } + break; + + case h_content_length: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + if (parser->flags & F_CONTENTLENGTH) { + SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + + parser->flags |= F_CONTENTLENGTH; + parser->content_length = ch - '0'; + break; + + case h_connection: + /* looking for 'Connection: keep-alive' */ + if (c == 'k') { + parser->header_state = h_matching_connection_keep_alive; + /* looking for 'Connection: close' */ + } else if (c == 'c') { + parser->header_state = h_matching_connection_close; + } else if (c == 'u') { + parser->header_state = h_matching_connection_upgrade; + } else { + parser->header_state = h_matching_connection_token; + } + break; + + /* Multi-value `Connection` header */ + case h_matching_connection_token_start: + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_value: + { + const char* start = p; + enum header_states h_state = (enum header_states) parser->header_state; + for (; p != data + len; p++) { + ch = *p; + if (ch == CR) { + UPDATE_STATE(s_header_almost_done); + parser->header_state = h_state; + CALLBACK_DATA(header_value); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_almost_done); + COUNT_HEADER_SIZE(p - start); + parser->header_state = h_state; + CALLBACK_DATA_NOADVANCE(header_value); + REEXECUTE(); + } + + if (!lenient && !IS_HEADER_CHAR(ch)) { + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + c = LOWER(ch); + + switch (h_state) { + case h_general: + { + const char* p_cr; + const char* p_lf; + size_t limit = data + len - p; + + limit = MIN(limit, HTTP_MAX_HEADER_SIZE); + + p_cr = (const char*) memchr(p, CR, limit); + p_lf = (const char*) memchr(p, LF, limit); + if (p_cr != NULL) { + if (p_lf != NULL && p_cr >= p_lf) + p = p_lf; + else + p = p_cr; + } else if (UNLIKELY(p_lf != NULL)) { + p = p_lf; + } else { + p = data + len; + } + --p; + + break; + } + + case h_connection: + case h_transfer_encoding: + assert(0 && "Shouldn't get here."); + break; + + case h_content_length: + { + uint64_t t; + + if (ch == ' ') break; + + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + } + + t = parser->content_length; + t *= 10; + t += ch - '0'; + + /* Overflow? Test against a conservative limit for simplicity. */ + if (UNLIKELY((ULLONG_MAX - 10) / 10 < parser->content_length)) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + } + + parser->content_length = t; + break; + } + + /* Transfer-Encoding: chunked */ + case h_matching_transfer_encoding_chunked: + parser->index++; + if (parser->index > sizeof(CHUNKED)-1 + || c != CHUNKED[parser->index]) { + h_state = h_general; + } else if (parser->index == sizeof(CHUNKED)-2) { + h_state = h_transfer_encoding_chunked; + } + break; + + case h_matching_connection_token_start: + /* looking for 'Connection: keep-alive' */ + if (c == 'k') { + h_state = h_matching_connection_keep_alive; + /* looking for 'Connection: close' */ + } else if (c == 'c') { + h_state = h_matching_connection_close; + } else if (c == 'u') { + h_state = h_matching_connection_upgrade; + } else if (STRICT_TOKEN(c)) { + h_state = h_matching_connection_token; + } else if (c == ' ' || c == '\t') { + /* Skip lws */ + } else { + h_state = h_general; + } + break; + + /* looking for 'Connection: keep-alive' */ + case h_matching_connection_keep_alive: + parser->index++; + if (parser->index > sizeof(KEEP_ALIVE)-1 + || c != KEEP_ALIVE[parser->index]) { + h_state = h_matching_connection_token; + } else if (parser->index == sizeof(KEEP_ALIVE)-2) { + h_state = h_connection_keep_alive; + } + break; + + /* looking for 'Connection: close' */ + case h_matching_connection_close: + parser->index++; + if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) { + h_state = h_matching_connection_token; + } else if (parser->index == sizeof(CLOSE)-2) { + h_state = h_connection_close; + } + break; + + /* looking for 'Connection: upgrade' */ + case h_matching_connection_upgrade: + parser->index++; + if (parser->index > sizeof(UPGRADE) - 1 || + c != UPGRADE[parser->index]) { + h_state = h_matching_connection_token; + } else if (parser->index == sizeof(UPGRADE)-2) { + h_state = h_connection_upgrade; + } + break; + + case h_matching_connection_token: + if (ch == ',') { + h_state = h_matching_connection_token_start; + parser->index = 0; + } + break; + + case h_transfer_encoding_chunked: + if (ch != ' ') h_state = h_general; + break; + + case h_connection_keep_alive: + case h_connection_close: + case h_connection_upgrade: + if (ch == ',') { + if (h_state == h_connection_keep_alive) { + parser->flags |= F_CONNECTION_KEEP_ALIVE; + } else if (h_state == h_connection_close) { + parser->flags |= F_CONNECTION_CLOSE; + } else if (h_state == h_connection_upgrade) { + parser->flags |= F_CONNECTION_UPGRADE; + } + h_state = h_matching_connection_token_start; + parser->index = 0; + } else if (ch != ' ') { + h_state = h_matching_connection_token; + } + break; + + default: + UPDATE_STATE(s_header_value); + h_state = h_general; + break; + } + } + parser->header_state = h_state; + + COUNT_HEADER_SIZE(p - start); + + if (p == data + len) + --p; + break; + } + + case s_header_almost_done: + { + if (UNLIKELY(ch != LF)) { + SET_ERRNO(HPE_LF_EXPECTED); + goto error; + } + + UPDATE_STATE(s_header_value_lws); + break; + } + + case s_header_value_lws: + { + if (ch == ' ' || ch == '\t') { + UPDATE_STATE(s_header_value_start); + REEXECUTE(); + } + + /* finished the header */ + switch (parser->header_state) { + case h_connection_keep_alive: + parser->flags |= F_CONNECTION_KEEP_ALIVE; + break; + case h_connection_close: + parser->flags |= F_CONNECTION_CLOSE; + break; + case h_transfer_encoding_chunked: + parser->flags |= F_CHUNKED; + break; + case h_connection_upgrade: + parser->flags |= F_CONNECTION_UPGRADE; + break; + default: + break; + } + + UPDATE_STATE(s_header_field_start); + REEXECUTE(); + } + + case s_header_value_discard_ws_almost_done: + { + STRICT_CHECK(ch != LF); + UPDATE_STATE(s_header_value_discard_lws); + break; + } + + case s_header_value_discard_lws: + { + if (ch == ' ' || ch == '\t') { + UPDATE_STATE(s_header_value_discard_ws); + break; + } else { + switch (parser->header_state) { + case h_connection_keep_alive: + parser->flags |= F_CONNECTION_KEEP_ALIVE; + break; + case h_connection_close: + parser->flags |= F_CONNECTION_CLOSE; + break; + case h_connection_upgrade: + parser->flags |= F_CONNECTION_UPGRADE; + break; + case h_transfer_encoding_chunked: + parser->flags |= F_CHUNKED; + break; + default: + break; + } + + /* header value was empty */ + MARK(header_value); + UPDATE_STATE(s_header_field_start); + CALLBACK_DATA_NOADVANCE(header_value); + REEXECUTE(); + } + } + + case s_headers_almost_done: + { + STRICT_CHECK(ch != LF); + + if (parser->flags & F_TRAILING) { + /* End of a chunked request */ + UPDATE_STATE(s_message_done); + CALLBACK_NOTIFY_NOADVANCE(chunk_complete); + REEXECUTE(); + } + + /* Cannot use chunked encoding and a content-length header together + per the HTTP specification. */ + if ((parser->flags & F_CHUNKED) && + (parser->flags & F_CONTENTLENGTH)) { + SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + + UPDATE_STATE(s_headers_done); + + /* Set this here so that on_headers_complete() callbacks can see it */ + parser->upgrade = + ((parser->flags & (F_UPGRADE | F_CONNECTION_UPGRADE)) == + (F_UPGRADE | F_CONNECTION_UPGRADE) || + parser->method == HTTP_CONNECT); + + /* Here we call the headers_complete callback. This is somewhat + * different than other callbacks because if the user returns 1, we + * will interpret that as saying that this message has no body. This + * is needed for the annoying case of receiving a response to a HEAD + * request. + * + * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so + * we have to simulate it by handling a change in errno below. + */ + if (settings->on_headers_complete) { + switch (settings->on_headers_complete(parser)) { + case 0: + break; + + case 2: + parser->upgrade = 1; + + case 1: + parser->flags |= F_SKIPBODY; + break; + + default: + SET_ERRNO(HPE_CB_headers_complete); + RETURN(p - data); /* Error */ + } + } + + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { + RETURN(p - data); + } + + REEXECUTE(); + } + + case s_headers_done: + { + int hasBody; + STRICT_CHECK(ch != LF); + + parser->nread = 0; + + hasBody = parser->flags & F_CHUNKED || + (parser->content_length > 0 && parser->content_length != ULLONG_MAX); + if (parser->upgrade && (parser->method == HTTP_CONNECT || + (parser->flags & F_SKIPBODY) || !hasBody)) { + /* Exit, the rest of the message is in a different protocol. */ + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + RETURN((p - data) + 1); + } + + if (parser->flags & F_SKIPBODY) { + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + } else if (parser->flags & F_CHUNKED) { + /* chunked encoding - ignore Content-Length header */ + UPDATE_STATE(s_chunk_size_start); + } else { + if (parser->content_length == 0) { + /* Content-Length header given but zero: Content-Length: 0\r\n */ + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + } else if (parser->content_length != ULLONG_MAX) { + /* Content-Length header given and non-zero */ + UPDATE_STATE(s_body_identity); + } else { + if (!http_message_needs_eof(parser)) { + /* Assume content-length 0 - read the next */ + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + } else { + /* Read body until EOF */ + UPDATE_STATE(s_body_identity_eof); + } + } + } + + break; + } + + case s_body_identity: + { + uint64_t to_read = MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->content_length != 0 + && parser->content_length != ULLONG_MAX); + + /* The difference between advancing content_length and p is because + * the latter will automatically advance on the next loop iteration. + * Further, if content_length ends up at 0, we want to see the last + * byte again for our message complete callback. + */ + MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + UPDATE_STATE(s_message_done); + + /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte. + * + * The alternative to doing this is to wait for the next byte to + * trigger the data callback, just as in every other case. The + * problem with this is that this makes it difficult for the test + * harness to distinguish between complete-on-EOF and + * complete-on-length. It's not clear that this distinction is + * important for applications, but let's keep it for now. + */ + CALLBACK_DATA_(body, p - body_mark + 1, p - data); + REEXECUTE(); + } + + break; + } + + /* read until EOF */ + case s_body_identity_eof: + MARK(body); + p = data + len - 1; + + break; + + case s_message_done: + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + if (parser->upgrade) { + /* Exit, the rest of the message is in a different protocol. */ + RETURN((p - data) + 1); + } + break; + + case s_chunk_size_start: + { + assert(parser->nread == 1); + assert(parser->flags & F_CHUNKED); + + unhex_val = unhex[(unsigned char)ch]; + if (UNLIKELY(unhex_val == -1)) { + SET_ERRNO(HPE_INVALID_CHUNK_SIZE); + goto error; + } + + parser->content_length = unhex_val; + UPDATE_STATE(s_chunk_size); + break; + } + + case s_chunk_size: + { + uint64_t t; + + assert(parser->flags & F_CHUNKED); + + if (ch == CR) { + UPDATE_STATE(s_chunk_size_almost_done); + break; + } + + unhex_val = unhex[(unsigned char)ch]; + + if (unhex_val == -1) { + if (ch == ';' || ch == ' ') { + UPDATE_STATE(s_chunk_parameters); + break; + } + + SET_ERRNO(HPE_INVALID_CHUNK_SIZE); + goto error; + } + + t = parser->content_length; + t *= 16; + t += unhex_val; + + /* Overflow? Test against a conservative limit for simplicity. */ + if (UNLIKELY((ULLONG_MAX - 16) / 16 < parser->content_length)) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + parser->content_length = t; + break; + } + + case s_chunk_parameters: + { + assert(parser->flags & F_CHUNKED); + /* just ignore this shit. TODO check for overflow */ + if (ch == CR) { + UPDATE_STATE(s_chunk_size_almost_done); + break; + } + break; + } + + case s_chunk_size_almost_done: + { + assert(parser->flags & F_CHUNKED); + STRICT_CHECK(ch != LF); + + parser->nread = 0; + + if (parser->content_length == 0) { + parser->flags |= F_TRAILING; + UPDATE_STATE(s_header_field_start); + } else { + UPDATE_STATE(s_chunk_data); + } + CALLBACK_NOTIFY(chunk_header); + break; + } + + case s_chunk_data: + { + uint64_t to_read = MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->flags & F_CHUNKED); + assert(parser->content_length != 0 + && parser->content_length != ULLONG_MAX); + + /* See the explanation in s_body_identity for why the content + * length and data pointers are managed this way. + */ + MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + UPDATE_STATE(s_chunk_data_almost_done); + } + + break; + } + + case s_chunk_data_almost_done: + assert(parser->flags & F_CHUNKED); + assert(parser->content_length == 0); + STRICT_CHECK(ch != CR); + UPDATE_STATE(s_chunk_data_done); + CALLBACK_DATA(body); + break; + + case s_chunk_data_done: + assert(parser->flags & F_CHUNKED); + STRICT_CHECK(ch != LF); + parser->nread = 0; + UPDATE_STATE(s_chunk_size_start); + CALLBACK_NOTIFY(chunk_complete); + break; + + default: + assert(0 && "unhandled state"); + SET_ERRNO(HPE_INVALID_INTERNAL_STATE); + goto error; + } + } + + /* Run callbacks for any marks that we have leftover after we ran our of + * bytes. There should be at most one of these set, so it's OK to invoke + * them in series (unset marks will not result in callbacks). + * + * We use the NOADVANCE() variety of callbacks here because 'p' has already + * overflowed 'data' and this allows us to correct for the off-by-one that + * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p' + * value that's in-bounds). + */ + + assert(((header_field_mark ? 1 : 0) + + (header_value_mark ? 1 : 0) + + (url_mark ? 1 : 0) + + (body_mark ? 1 : 0) + + (status_mark ? 1 : 0)) <= 1); + + CALLBACK_DATA_NOADVANCE(header_field); + CALLBACK_DATA_NOADVANCE(header_value); + CALLBACK_DATA_NOADVANCE(url); + CALLBACK_DATA_NOADVANCE(body); + CALLBACK_DATA_NOADVANCE(status); + + RETURN(len); + +error: + if (HTTP_PARSER_ERRNO(parser) == HPE_OK) { + SET_ERRNO(HPE_UNKNOWN); + } + + RETURN(p - data); +} + + +/* Does the parser need to see an EOF to find the end of the message? */ +int +http_message_needs_eof (const http_parser *parser) +{ + if (parser->type == HTTP_REQUEST) { + return 0; + } + + /* See RFC 2616 section 4.4 */ + if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ + parser->status_code == 204 || /* No Content */ + parser->status_code == 304 || /* Not Modified */ + parser->flags & F_SKIPBODY) { /* response to a HEAD request */ + return 0; + } + + if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) { + return 0; + } + + return 1; +} + + +int +http_should_keep_alive (const http_parser *parser) +{ + if (parser->http_major > 0 && parser->http_minor > 0) { + /* HTTP/1.1 */ + if (parser->flags & F_CONNECTION_CLOSE) { + return 0; + } + } else { + /* HTTP/1.0 or earlier */ + if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) { + return 0; + } + } + + return !http_message_needs_eof(parser); +} + + +const char * +http_method_str (enum http_method m) +{ + return ELEM_AT(method_strings, m, ""); +} + + +void +http_parser_init (http_parser *parser, enum http_parser_type t) +{ + void *data = parser->data; /* preserve application data */ + memset(parser, 0, sizeof(*parser)); + parser->data = data; + parser->type = t; + parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res)); + parser->http_errno = HPE_OK; +} + +void +http_parser_settings_init(http_parser_settings *settings) +{ + memset(settings, 0, sizeof(*settings)); +} + +const char * +http_errno_name(enum http_errno err) { + assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab)); + return http_strerror_tab[err].name; +} + +const char * +http_errno_description(enum http_errno err) { + assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab)); + return http_strerror_tab[err].description; +} + +static enum http_host_state +http_parse_host_char(enum http_host_state s, const char ch) { + switch(s) { + case s_http_userinfo: + case s_http_userinfo_start: + if (ch == '@') { + return s_http_host_start; + } + + if (IS_USERINFO_CHAR(ch)) { + return s_http_userinfo; + } + break; + + case s_http_host_start: + if (ch == '[') { + return s_http_host_v6_start; + } + + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + break; + + case s_http_host: + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + /* FALLTHROUGH */ + case s_http_host_v6_end: + if (ch == ':') { + return s_http_host_port_start; + } + + break; + + case s_http_host_v6: + if (ch == ']') { + return s_http_host_v6_end; + } + + /* FALLTHROUGH */ + case s_http_host_v6_start: + if (IS_HEX(ch) || ch == ':' || ch == '.') { + return s_http_host_v6; + } + + if (s == s_http_host_v6 && ch == '%') { + return s_http_host_v6_zone_start; + } + break; + + case s_http_host_v6_zone: + if (ch == ']') { + return s_http_host_v6_end; + } + + /* FALLTHROUGH */ + case s_http_host_v6_zone_start: + /* RFC 6874 Zone ID consists of 1*( unreserved / pct-encoded) */ + if (IS_ALPHANUM(ch) || ch == '%' || ch == '.' || ch == '-' || ch == '_' || + ch == '~') { + return s_http_host_v6_zone; + } + break; + + case s_http_host_port: + case s_http_host_port_start: + if (IS_NUM(ch)) { + return s_http_host_port; + } + + break; + + default: + break; + } + return s_http_host_dead; +} + +static int +http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { + enum http_host_state s; + + const char *p; + size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len; + + assert(u->field_set & (1 << UF_HOST)); + + u->field_data[UF_HOST].len = 0; + + s = found_at ? s_http_userinfo_start : s_http_host_start; + + for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) { + enum http_host_state new_s = http_parse_host_char(s, *p); + + if (new_s == s_http_host_dead) { + return 1; + } + + switch(new_s) { + case s_http_host: + if (s != s_http_host) { + u->field_data[UF_HOST].off = p - buf; + } + u->field_data[UF_HOST].len++; + break; + + case s_http_host_v6: + if (s != s_http_host_v6) { + u->field_data[UF_HOST].off = p - buf; + } + u->field_data[UF_HOST].len++; + break; + + case s_http_host_v6_zone_start: + case s_http_host_v6_zone: + u->field_data[UF_HOST].len++; + break; + + case s_http_host_port: + if (s != s_http_host_port) { + u->field_data[UF_PORT].off = p - buf; + u->field_data[UF_PORT].len = 0; + u->field_set |= (1 << UF_PORT); + } + u->field_data[UF_PORT].len++; + break; + + case s_http_userinfo: + if (s != s_http_userinfo) { + u->field_data[UF_USERINFO].off = p - buf ; + u->field_data[UF_USERINFO].len = 0; + u->field_set |= (1 << UF_USERINFO); + } + u->field_data[UF_USERINFO].len++; + break; + + default: + break; + } + s = new_s; + } + + /* Make sure we don't end somewhere unexpected */ + switch (s) { + case s_http_host_start: + case s_http_host_v6_start: + case s_http_host_v6: + case s_http_host_v6_zone_start: + case s_http_host_v6_zone: + case s_http_host_port_start: + case s_http_userinfo: + case s_http_userinfo_start: + return 1; + default: + break; + } + + return 0; +} + +void +http_parser_url_init(struct http_parser_url *u) { + memset(u, 0, sizeof(*u)); +} + +int +http_parser_parse_url(const char *buf, size_t buflen, int is_connect, + struct http_parser_url *u) +{ + enum state s; + const char *p; + enum http_parser_url_fields uf, old_uf; + int found_at = 0; + + u->port = u->field_set = 0; + s = is_connect ? s_req_server_start : s_req_spaces_before_url; + old_uf = UF_MAX; + + for (p = buf; p < buf + buflen; p++) { + s = parse_url_char(s, *p); + + /* Figure out the next field that we're operating on */ + switch (s) { + case s_dead: + return 1; + + /* Skip delimiters */ + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_query_string_start: + case s_req_fragment_start: + continue; + + case s_req_schema: + uf = UF_SCHEMA; + break; + + case s_req_server_with_at: + found_at = 1; + + /* FALLTROUGH */ + case s_req_server: + uf = UF_HOST; + break; + + case s_req_path: + uf = UF_PATH; + break; + + case s_req_query_string: + uf = UF_QUERY; + break; + + case s_req_fragment: + uf = UF_FRAGMENT; + break; + + default: + assert(!"Unexpected state"); + return 1; + } + + /* Nothing's changed; soldier on */ + if (uf == old_uf) { + u->field_data[uf].len++; + continue; + } + + u->field_data[uf].off = p - buf; + u->field_data[uf].len = 1; + + u->field_set |= (1 << uf); + old_uf = uf; + } + + /* host must be present if there is a schema */ + /* parsing http:///toto will fail */ + if ((u->field_set & (1 << UF_SCHEMA)) && + (u->field_set & (1 << UF_HOST)) == 0) { + return 1; + } + + if (u->field_set & (1 << UF_HOST)) { + if (http_parse_host(buf, u, found_at) != 0) { + return 1; + } + } + + /* CONNECT requests can only contain "hostname:port" */ + if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) { + return 1; + } + + if (u->field_set & (1 << UF_PORT)) { + /* Don't bother with endp; we've already validated the string */ + unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10); + + /* Ports have a max value of 2^16 */ + if (v > 0xffff) { + return 1; + } + + u->port = (uint16_t) v; + } + + return 0; +} + +void +http_parser_pause(http_parser *parser, int paused) { + /* Users should only be pausing/unpausing a parser that is not in an error + * state. In non-debug builds, there's not much that we can do about this + * other than ignore it. + */ + if (HTTP_PARSER_ERRNO(parser) == HPE_OK || + HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) { + SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK); + } else { + assert(0 && "Attempting to pause parser in error state"); + } +} + +int +http_body_is_final(const struct http_parser *parser) { + return parser->state == s_message_done; +} + +unsigned long +http_parser_version(void) { + return HTTP_PARSER_VERSION_MAJOR * 0x10000 | + HTTP_PARSER_VERSION_MINOR * 0x00100 | + HTTP_PARSER_VERSION_PATCH * 0x00001; +} diff --git a/userspace/libsinsp/http_parser.h b/userspace/libsinsp/http_parser.h new file mode 100644 index 0000000000..75a12fabb6 --- /dev/null +++ b/userspace/libsinsp/http_parser.h @@ -0,0 +1,362 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#ifndef http_parser_h +#define http_parser_h +#ifdef __cplusplus +extern "C" { +#endif + +/* Also update SONAME in the Makefile whenever you change these. */ +#define HTTP_PARSER_VERSION_MAJOR 2 +#define HTTP_PARSER_VERSION_MINOR 7 +#define HTTP_PARSER_VERSION_PATCH 1 + +#include +#if defined(_WIN32) && !defined(__MINGW32__) && \ + (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__) +#include +#include +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#else +#include +#endif + +/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run + * faster + */ +#ifndef HTTP_PARSER_STRICT +# define HTTP_PARSER_STRICT 1 +#endif + +/* Maximum header size allowed. If the macro is not defined + * before including this header then the default is used. To + * change the maximum header size, define the macro in the build + * environment (e.g. -DHTTP_MAX_HEADER_SIZE=). To remove + * the effective limit on the size of the header, define the macro + * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff) + */ +#ifndef HTTP_MAX_HEADER_SIZE +# define HTTP_MAX_HEADER_SIZE (80*1024) +#endif + +typedef struct http_parser http_parser; +typedef struct http_parser_settings http_parser_settings; + + +/* Callbacks should return non-zero to indicate an error. The parser will + * then halt execution. + * + * The one exception is on_headers_complete. In a HTTP_RESPONSE parser + * returning '1' from on_headers_complete will tell the parser that it + * should not expect a body. This is used when receiving a response to a + * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding: + * chunked' headers that indicate the presence of a body. + * + * Returning `2` from on_headers_complete will tell parser that it should not + * expect neither a body nor any future responses on this connection. This is + * useful for handling responses to a CONNECT request which may not contain + * `Upgrade` or `Connection: upgrade` headers. + * + * http_data_cb does not return data chunks. It will be called arbitrarily + * many times for each string. E.G. you might get 10 callbacks for "on_url" + * each providing just a few characters more data. + */ +typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); +typedef int (*http_cb) (http_parser*); + + +/* Request Methods */ +#define HTTP_METHOD_MAP(XX) \ + XX(0, DELETE, DELETE) \ + XX(1, GET, GET) \ + XX(2, HEAD, HEAD) \ + XX(3, POST, POST) \ + XX(4, PUT, PUT) \ + /* pathological */ \ + XX(5, CONNECT, CONNECT) \ + XX(6, OPTIONS, OPTIONS) \ + XX(7, TRACE, TRACE) \ + /* WebDAV */ \ + XX(8, COPY, COPY) \ + XX(9, LOCK, LOCK) \ + XX(10, MKCOL, MKCOL) \ + XX(11, MOVE, MOVE) \ + XX(12, PROPFIND, PROPFIND) \ + XX(13, PROPPATCH, PROPPATCH) \ + XX(14, SEARCH, SEARCH) \ + XX(15, UNLOCK, UNLOCK) \ + XX(16, BIND, BIND) \ + XX(17, REBIND, REBIND) \ + XX(18, UNBIND, UNBIND) \ + XX(19, ACL, ACL) \ + /* subversion */ \ + XX(20, REPORT, REPORT) \ + XX(21, MKACTIVITY, MKACTIVITY) \ + XX(22, CHECKOUT, CHECKOUT) \ + XX(23, MERGE, MERGE) \ + /* upnp */ \ + XX(24, MSEARCH, M-SEARCH) \ + XX(25, NOTIFY, NOTIFY) \ + XX(26, SUBSCRIBE, SUBSCRIBE) \ + XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \ + /* RFC-5789 */ \ + XX(28, PATCH, PATCH) \ + XX(29, PURGE, PURGE) \ + /* CalDAV */ \ + XX(30, MKCALENDAR, MKCALENDAR) \ + /* RFC-2068, section 19.6.1.2 */ \ + XX(31, LINK, LINK) \ + XX(32, UNLINK, UNLINK) \ + +enum http_method + { +#define XX(num, name, string) HTTP_##name = num, + HTTP_METHOD_MAP(XX) +#undef XX + }; + + +enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH }; + + +/* Flag values for http_parser.flags field */ +enum flags + { F_CHUNKED = 1 << 0 + , F_CONNECTION_KEEP_ALIVE = 1 << 1 + , F_CONNECTION_CLOSE = 1 << 2 + , F_CONNECTION_UPGRADE = 1 << 3 + , F_TRAILING = 1 << 4 + , F_UPGRADE = 1 << 5 + , F_SKIPBODY = 1 << 6 + , F_CONTENTLENGTH = 1 << 7 + }; + + +/* Map for errno-related constants + * + * The provided argument should be a macro that takes 2 arguments. + */ +#define HTTP_ERRNO_MAP(XX) \ + /* No error */ \ + XX(OK, "success") \ + \ + /* Callback-related errors */ \ + XX(CB_message_begin, "the on_message_begin callback failed") \ + XX(CB_url, "the on_url callback failed") \ + XX(CB_header_field, "the on_header_field callback failed") \ + XX(CB_header_value, "the on_header_value callback failed") \ + XX(CB_headers_complete, "the on_headers_complete callback failed") \ + XX(CB_body, "the on_body callback failed") \ + XX(CB_message_complete, "the on_message_complete callback failed") \ + XX(CB_status, "the on_status callback failed") \ + XX(CB_chunk_header, "the on_chunk_header callback failed") \ + XX(CB_chunk_complete, "the on_chunk_complete callback failed") \ + \ + /* Parsing-related errors */ \ + XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \ + XX(HEADER_OVERFLOW, \ + "too many header bytes seen; overflow detected") \ + XX(CLOSED_CONNECTION, \ + "data received after completed connection: close message") \ + XX(INVALID_VERSION, "invalid HTTP version") \ + XX(INVALID_STATUS, "invalid HTTP status code") \ + XX(INVALID_METHOD, "invalid HTTP method") \ + XX(INVALID_URL, "invalid URL") \ + XX(INVALID_HOST, "invalid host") \ + XX(INVALID_PORT, "invalid port") \ + XX(INVALID_PATH, "invalid path") \ + XX(INVALID_QUERY_STRING, "invalid query string") \ + XX(INVALID_FRAGMENT, "invalid fragment") \ + XX(LF_EXPECTED, "LF character expected") \ + XX(INVALID_HEADER_TOKEN, "invalid character in header") \ + XX(INVALID_CONTENT_LENGTH, \ + "invalid character in content-length header") \ + XX(UNEXPECTED_CONTENT_LENGTH, \ + "unexpected content-length header") \ + XX(INVALID_CHUNK_SIZE, \ + "invalid character in chunk size header") \ + XX(INVALID_CONSTANT, "invalid constant string") \ + XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\ + XX(STRICT, "strict mode assertion failed") \ + XX(PAUSED, "parser is paused") \ + XX(UNKNOWN, "an unknown error occurred") + + +/* Define HPE_* values for each errno value above */ +#define HTTP_ERRNO_GEN(n, s) HPE_##n, +enum http_errno { + HTTP_ERRNO_MAP(HTTP_ERRNO_GEN) +}; +#undef HTTP_ERRNO_GEN + + +/* Get an http_errno value from an http_parser */ +#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno) + + +struct http_parser { + /** PRIVATE **/ + unsigned int type : 2; /* enum http_parser_type */ + unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */ + unsigned int state : 7; /* enum state from http_parser.c */ + unsigned int header_state : 7; /* enum header_state from http_parser.c */ + unsigned int index : 7; /* index into current matcher */ + unsigned int lenient_http_headers : 1; + + uint32_t nread; /* # bytes read in various scenarios */ + uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */ + + /** READ-ONLY **/ + unsigned short http_major; + unsigned short http_minor; + unsigned int status_code : 16; /* responses only */ + unsigned int method : 8; /* requests only */ + unsigned int http_errno : 7; + + /* 1 = Upgrade header was present and the parser has exited because of that. + * 0 = No upgrade header present. + * Should be checked when http_parser_execute() returns in addition to + * error checking. + */ + unsigned int upgrade : 1; + + /** PUBLIC **/ + void *data; /* A pointer to get hook to the "connection" or "socket" object */ +}; + + +struct http_parser_settings { + http_cb on_message_begin; + http_data_cb on_url; + http_data_cb on_status; + http_data_cb on_header_field; + http_data_cb on_header_value; + http_cb on_headers_complete; + http_data_cb on_body; + http_cb on_message_complete; + /* When on_chunk_header is called, the current chunk length is stored + * in parser->content_length. + */ + http_cb on_chunk_header; + http_cb on_chunk_complete; +}; + + +enum http_parser_url_fields + { UF_SCHEMA = 0 + , UF_HOST = 1 + , UF_PORT = 2 + , UF_PATH = 3 + , UF_QUERY = 4 + , UF_FRAGMENT = 5 + , UF_USERINFO = 6 + , UF_MAX = 7 + }; + + +/* Result structure for http_parser_parse_url(). + * + * Callers should index into field_data[] with UF_* values iff field_set + * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and + * because we probably have padding left over), we convert any port to + * a uint16_t. + */ +struct http_parser_url { + uint16_t field_set; /* Bitmask of (1 << UF_*) values */ + uint16_t port; /* Converted UF_PORT string */ + + struct { + uint16_t off; /* Offset into buffer in which field starts */ + uint16_t len; /* Length of run in buffer */ + } field_data[UF_MAX]; +}; + + +/* Returns the library version. Bits 16-23 contain the major version number, + * bits 8-15 the minor version number and bits 0-7 the patch level. + * Usage example: + * + * unsigned long version = http_parser_version(); + * unsigned major = (version >> 16) & 255; + * unsigned minor = (version >> 8) & 255; + * unsigned patch = version & 255; + * printf("http_parser v%u.%u.%u\n", major, minor, patch); + */ +unsigned long http_parser_version(void); + +void http_parser_init(http_parser *parser, enum http_parser_type type); + + +/* Initialize http_parser_settings members to 0 + */ +void http_parser_settings_init(http_parser_settings *settings); + + +/* Executes the parser. Returns number of parsed bytes. Sets + * `parser->http_errno` on error. */ +size_t http_parser_execute(http_parser *parser, + const http_parser_settings *settings, + const char *data, + size_t len); + + +/* If http_should_keep_alive() in the on_headers_complete or + * on_message_complete callback returns 0, then this should be + * the last message on the connection. + * If you are the server, respond with the "Connection: close" header. + * If you are the client, close the connection. + */ +int http_should_keep_alive(const http_parser *parser); + +/* Returns a string version of the HTTP method. */ +const char *http_method_str(enum http_method m); + +/* Return a string name of the given error */ +const char *http_errno_name(enum http_errno err); + +/* Return a string description of the given error */ +const char *http_errno_description(enum http_errno err); + +/* Initialize all http_parser_url members to 0 */ +void http_parser_url_init(struct http_parser_url *u); + +/* Parse a URL; return nonzero on failure */ +int http_parser_parse_url(const char *buf, size_t buflen, + int is_connect, + struct http_parser_url *u); + +/* Pause or un-pause the parser; a nonzero value pauses */ +void http_parser_pause(http_parser *parser, int paused); + +/* Checks if this is the final chunk of the body. */ +int http_body_is_final(const http_parser *parser); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/userspace/libsinsp/http_reason.cpp b/userspace/libsinsp/http_reason.cpp new file mode 100644 index 0000000000..6287c73d56 --- /dev/null +++ b/userspace/libsinsp/http_reason.cpp @@ -0,0 +1,93 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "http_reason.h" + +const std::map http_reason::m_http_reason = + { { 100, "Continue" }, + { 101, "Switching Protocols" }, + { 102, "Processing" }, + { 200, "OK" }, + { 201, "Created" }, + { 202, "Accepted" }, + { 203, "Non-Authoritative Information" }, + { 204, "No Content" }, + { 205, "Reset Content" }, + { 206, "Partial Content" }, + { 207, "Multi Status" }, + { 208, "Already Reported" }, + { 226, "IM Used" }, + { 300, "Multiple Choices" }, + { 301, "Moved Permanently" }, + { 302, "Found" }, + { 303, "See Other" }, + { 304, "Not Modified" }, + { 305, "Use Proxy" }, + { 307, "Temporary Redirect" }, + { 308, "Permanent Redirect" }, + { 400, "Bad Request" }, + { 401, "Unauthorized" }, + { 402, "Payment Required" }, + { 403, "Forbidden" }, + { 404, "Not Found" }, + { 405, "Method Not Allowed" }, + { 406, "Not Acceptable" }, + { 407, "Proxy Authentication Required" }, + { 408, "Request Time-out" }, + { 409, "Conflict" }, + { 410, "Gone" }, + { 411, "Length Required" }, + { 412, "Precondition Failed" }, + { 413, "Request Entity Too Large" }, + { 414, "Request-URI Too Long" }, + { 415, "Unsupported Media Type" }, + { 416, "Requested Range Not Satisfiable" }, + { 417, "Expectation Failed" }, + { 418, "I'm a Teapot" }, + { 420, "Enhance Your Calm" }, + { 421, "Misdirected Request" }, + { 422, "Unprocessable Entity" }, + { 423, "Locked" }, + { 424, "Failed Dependency" }, + { 426, "Upgrade Required" }, + { 428, "Precondition Required" }, + { 429, "Too Many Requests" }, + { 431, "Request Header Fields Too Large" }, + { 451, "Unavailable For Legal Reasons" }, + { 500, "Internal Server Error" }, + { 501, "Not Implemented" }, + { 502, "Bad Gateway" }, + { 503, "Service Unavailable" }, + { 504, "Gateway Time-Out" }, + { 505, "HTTP Version Not Supported" }, + { 506, "Variant Also Negotiates" }, + { 507, "Insufficient Storage" }, + { 508, "Loop Detected" }, + { 510, "Not Extended" }, + { 511, "Network Authentication Required" } }; + +std::string http_reason::get(int status) +{ + auto it = m_http_reason.find(status); + if(it != m_http_reason.end()) + { + return it->second; + } + return ""; +} diff --git a/userspace/libsinsp/http_reason.h b/userspace/libsinsp/http_reason.h new file mode 100644 index 0000000000..a63d91938c --- /dev/null +++ b/userspace/libsinsp/http_reason.h @@ -0,0 +1,32 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#include + +class http_reason +{ +public: + static std::string get(int status); + +private: + static const std::map m_http_reason; +}; diff --git a/userspace/libsinsp/ifinfo.cpp b/userspace/libsinsp/ifinfo.cpp index 7d6fb4b9af..fa6132c96a 100644 --- a/userspace/libsinsp/ifinfo.cpp +++ b/userspace/libsinsp/ifinfo.cpp @@ -1,24 +1,34 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "sinsp.h" #include "sinsp_int.h" +sinsp_network_interfaces::sinsp_network_interfaces(sinsp* inspector) + : m_inspector(inspector) +{ + if(inet_pton(AF_INET6, "::1", m_ipv6_loopback_addr.m_b) != 1) + { + throw sinsp_exception("Could not convert ipv6 loopback address ::1 to ipv6addr struct"); + } +} + sinsp_ipv4_ifinfo::sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t bcast, const char* name) { m_addr = addr; @@ -30,15 +40,22 @@ sinsp_ipv4_ifinfo::sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t b void sinsp_ipv4_ifinfo::convert_to_string(char * dest, const uint32_t addr) { sprintf( - dest, - "%d.%d.%d.%d", + dest, + "%d.%d.%d.%d", (addr & 0xFF), ((addr & 0xFF00) >> 8), ((addr & 0xFF0000) >> 16), ((addr & 0xFF000000) >> 24)); } -string sinsp_ipv4_ifinfo::to_string() +string sinsp_ipv4_ifinfo::address() const +{ + char str_addr[16]; + convert_to_string(str_addr, m_addr); + return string(str_addr); +} + +string sinsp_ipv4_ifinfo::to_string() const { char s[100]; char str_addr[16]; @@ -48,7 +65,7 @@ string sinsp_ipv4_ifinfo::to_string() convert_to_string(str_addr, m_addr); convert_to_string(s_netmask, m_netmask); convert_to_string(s_bcast, m_bcast); - sprintf(s, "%s inet %s netmask %s broadcast %s", m_name.c_str(), str_addr, s_netmask, s_bcast); + snprintf(s, sizeof(s), "%s inet %s netmask %s broadcast %s", m_name.c_str(), str_addr, s_netmask, s_bcast); return string(s); } @@ -88,26 +105,93 @@ uint32_t sinsp_network_interfaces::infer_ipv4_address(uint32_t destination_addre void sinsp_network_interfaces::update_fd(sinsp_fdinfo_t *fd) { ipv4tuple *pipv4info = &(fd->m_sockinfo.m_ipv4info); + ipv6tuple *pipv6info = &(fd->m_sockinfo.m_ipv6info); // - // only handle ipv4 udp sockets + // only handle ipv4/ipv6 udp sockets // - if(fd->m_type != SCAP_FD_IPV4_SOCK) + if(fd->m_type != SCAP_FD_IPV4_SOCK && + fd->m_type != SCAP_FD_IPV6_SOCK) { return; } - if(0 != pipv4info->m_fields.m_sip && 0 != pipv4info->m_fields.m_dip) + if(fd->m_type == SCAP_FD_IPV4_SOCK) { - return; - } - if(0 == pipv4info->m_fields.m_sip) - { - pipv4info->m_fields.m_sip = infer_ipv4_address(pipv4info->m_fields.m_dip); + + if(0 != pipv4info->m_fields.m_sip && 0 != pipv4info->m_fields.m_dip) + { + return; + } + if(0 == pipv4info->m_fields.m_sip) + { + uint32_t newaddr; + newaddr = infer_ipv4_address(pipv4info->m_fields.m_dip); + + if(newaddr == pipv4info->m_fields.m_dip) + { + if(pipv4info->m_fields.m_sport == pipv4info->m_fields.m_dport) + { + return; + } + } + + pipv4info->m_fields.m_sip = newaddr; + } + else + { + uint32_t newaddr; + newaddr = infer_ipv4_address(pipv4info->m_fields.m_sip); + + if(newaddr == pipv4info->m_fields.m_sip) + { + if(pipv4info->m_fields.m_sport == pipv4info->m_fields.m_dport) + { + return; + } + } + + pipv4info->m_fields.m_dip = newaddr; + } } - else + else if(fd->m_type == SCAP_FD_IPV6_SOCK) { - pipv4info->m_fields.m_dip = infer_ipv4_address(pipv4info->m_fields.m_sip); + + if(ipv6addr::empty_address != pipv6info->m_fields.m_sip && + ipv6addr::empty_address != pipv6info->m_fields.m_dip) + { + return; + } + if(ipv6addr::empty_address == pipv6info->m_fields.m_sip) + { + ipv6addr newaddr; + newaddr = infer_ipv6_address(pipv6info->m_fields.m_dip); + + if(newaddr == pipv6info->m_fields.m_dip) + { + if(pipv6info->m_fields.m_sport == pipv6info->m_fields.m_dport) + { + return; + } + } + + pipv6info->m_fields.m_sip = newaddr; + } + else + { + ipv6addr newaddr; + newaddr = infer_ipv6_address(pipv6info->m_fields.m_sip); + + if(newaddr == pipv6info->m_fields.m_sip) + { + if(pipv6info->m_fields.m_sport == pipv6info->m_fields.m_dport) + { + return; + } + } + + pipv6info->m_fields.m_dip = newaddr; + } } } @@ -137,11 +221,69 @@ bool sinsp_network_interfaces::is_ipv4addr_in_subnet(uint32_t addr) return false; } -bool sinsp_network_interfaces::is_ipv4addr_in_local_machine(uint32_t addr) +bool sinsp_network_interfaces::is_ipv4addr_in_local_machine(uint32_t addr, sinsp_threadinfo* tinfo) { + if(!tinfo->m_container_id.empty()) + { + const sinsp_container_info::ptr_t container_info = + m_inspector->m_container_manager.get_container(tinfo->m_container_id); + + // + // Note: if we don't have container info, any pick we make is arbitrary. + // To at least achieve consistency across client and server, we just match the host interface addresses. + // + if(container_info) + { + if(container_info->m_container_ip != 0) + { + // + // We have a container info with a valid container IP. Let's use it. + // + if(addr == htonl(container_info->m_container_ip)) + { + return true; + } + } + else + { + // + // Container info is valid, but the IP address is zero. + // Scan the list of the containers looking for matches. + // If no match is found, we just jump to checking the + // host interfaces. + // + + if(!container_info->is_successful()) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "Checking IP address of container %s with incomplete metadata (state=%d)", + tinfo->m_container_id.c_str(), container_info->m_lookup_state); + } + + const sinsp_container_manager::map_ptr_t clist = m_inspector->m_container_manager.get_containers(); + + for(const auto& it : *clist) + { + if(!it.second->is_successful()) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "Checking IP address of container %s with incomplete metadata (in context of %s; state=%d)", + it.second->m_id.c_str(), tinfo->m_container_id.c_str(), + it.second->m_lookup_state); + } + + if(htonl(it.second->m_container_ip) == addr) + { + return true; + } + } + } + } + } + vector::iterator it; - // try to find an interface for the same subnet + // try to find an interface that has the given IP as address for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++) { if(it->m_addr == addr) @@ -153,11 +295,6 @@ bool sinsp_network_interfaces::is_ipv4addr_in_local_machine(uint32_t addr) return false; } -void sinsp_network_interfaces::import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo) -{ - m_ipv4_interfaces.push_back(ifinfo); -} - void sinsp_network_interfaces::import_ipv4_ifaddr_list(uint32_t count, scap_ifinfo_ipv4* plist) { if (count == 0) @@ -176,6 +313,62 @@ void sinsp_network_interfaces::import_ipv4_ifaddr_list(uint32_t count, scap_ifin } } +ipv6addr sinsp_network_interfaces::infer_ipv6_address(ipv6addr &destination_address) +{ + vector::iterator it; + + // first try to find exact match + for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++) + { + if(destination_address == it->m_net) + { + return it->m_net; + } + } + + // try to find an interface for the same subnet + for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++) + { + if(it->m_net.in_subnet(destination_address)) + { + return it->m_net; + } + } + + // otherwise take the first non loopback interface + for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++) + { + if(it->m_net != m_ipv6_loopback_addr) + { + return it->m_net; + } + } + + return ipv6addr::empty_address; +} + +bool sinsp_network_interfaces::is_ipv6addr_in_local_machine(ipv6addr &addr, sinsp_threadinfo* tinfo) +{ + if(!tinfo->m_container_id.empty()) + { + // For now, not supporting ipv6 networking for containers. So always return false; + return false; + } + + vector::iterator it; + + // try to find an interface that has the given IP as address + for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++) + { + if(addr.in_subnet(it->m_net)) + { + return true; + } + } + + return false; +} + void sinsp_network_interfaces::import_ipv6_ifaddr_list(uint32_t count, scap_ifinfo_ipv6* plist) { if (count == 0) @@ -185,9 +378,12 @@ void sinsp_network_interfaces::import_ipv6_ifaddr_list(uint32_t count, scap_ifin for(uint32_t j = 0; j < count; j++) { sinsp_ipv6_ifinfo info; - memcpy(info.m_addr, plist->addr, SCAP_IPV6_ADDR_LEN); - memcpy(info.m_netmask, plist->netmask, SCAP_IPV6_ADDR_LEN); - memcpy(info.m_bcast, plist->bcast, SCAP_IPV6_ADDR_LEN); + + // Only saving the address portion. (Assumes + // convention of first 48 bits for network, next 16 + // bits for subnet). + memcpy(info.m_net.m_b, plist->addr, SCAP_IPV6_ADDR_LEN); + info.m_name = plist->ifname; m_ipv6_interfaces.push_back(info); plist++; @@ -198,13 +394,22 @@ void sinsp_network_interfaces::import_interfaces(scap_addrlist* paddrlist) { if(NULL != paddrlist) { - m_ipv4_interfaces.clear(); - m_ipv6_interfaces.clear(); + clear(); import_ipv4_ifaddr_list(paddrlist->n_v4_addrs, paddrlist->v4list); import_ipv6_ifaddr_list(paddrlist->n_v6_addrs, paddrlist->v6list); } } +void sinsp_network_interfaces::import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo) +{ + m_ipv4_interfaces.push_back(ifinfo); +} + +void sinsp_network_interfaces::import_ipv6_interface(const sinsp_ipv6_ifinfo& ifinfo) +{ + m_ipv6_interfaces.push_back(ifinfo); +} + vector* sinsp_network_interfaces::get_ipv4_list() { return &m_ipv4_interfaces; diff --git a/userspace/libsinsp/ifinfo.h b/userspace/libsinsp/ifinfo.h index e00ebfcd48..1f77e5ae9d 100644 --- a/userspace/libsinsp/ifinfo.h +++ b/userspace/libsinsp/ifinfo.h @@ -1,23 +1,26 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once +#include "tuples.h" + #define LOOPBACK_ADDR 0x0100007f #ifndef VISIBILITY_PRIVATE @@ -34,16 +37,15 @@ class SINSP_PUBLIC sinsp_ipv4_ifinfo sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t bcast, const char* name); - string to_string(); + string to_string() const; + string address() const; uint32_t m_addr; uint32_t m_netmask; uint32_t m_bcast; - - string m_name; private: - void convert_to_string(char * dest, const uint32_t addr); + static void convert_to_string(char * dest, const uint32_t addr); }; // @@ -54,9 +56,7 @@ class SINSP_PUBLIC sinsp_ipv6_ifinfo public: sinsp_ipv6_ifinfo() {}; - char m_addr[SCAP_IPV6_ADDR_LEN]; - char m_netmask[SCAP_IPV6_ADDR_LEN]; - char m_bcast[SCAP_IPV6_ADDR_LEN]; + ipv6addr m_net; string m_name; }; @@ -64,19 +64,33 @@ class SINSP_PUBLIC sinsp_ipv6_ifinfo class SINSP_PUBLIC sinsp_network_interfaces { public: + sinsp_network_interfaces(sinsp* inspector); + void import_interfaces(scap_addrlist* paddrlist); void import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo); void update_fd(sinsp_fdinfo_t *fd); bool is_ipv4addr_in_subnet(uint32_t addr); - bool is_ipv4addr_in_local_machine(uint32_t addr); + bool is_ipv4addr_in_local_machine(uint32_t addr, sinsp_threadinfo* tinfo); + void import_ipv6_interface(const sinsp_ipv6_ifinfo& ifinfo); + bool is_ipv6addr_in_local_machine(ipv6addr &addr, sinsp_threadinfo* tinfo); vector* get_ipv4_list(); vector* get_ipv6_list(); + inline void clear(); + + ipv6addr m_ipv6_loopback_addr; VISIBILITY_PRIVATE uint32_t infer_ipv4_address(uint32_t destination_address); void import_ipv4_ifaddr_list(uint32_t count, scap_ifinfo_ipv4* plist); + ipv6addr infer_ipv6_address(ipv6addr &destination_address); void import_ipv6_ifaddr_list(uint32_t count, scap_ifinfo_ipv6* plist); vector m_ipv4_interfaces; vector m_ipv6_interfaces; + sinsp* m_inspector; +}; -}; \ No newline at end of file +void sinsp_network_interfaces::clear() +{ + m_ipv4_interfaces.clear(); + m_ipv6_interfaces.clear(); +} \ No newline at end of file diff --git a/userspace/libsinsp/ifinfo_test.cpp b/userspace/libsinsp/ifinfo_test.cpp index 26da5d43e2..c80218d30b 100644 --- a/userspace/libsinsp/ifinfo_test.cpp +++ b/userspace/libsinsp/ifinfo_test.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include diff --git a/userspace/libsinsp/include/sinsp_external_processor.h b/userspace/libsinsp/include/sinsp_external_processor.h new file mode 100644 index 0000000000..b3d90d94d7 --- /dev/null +++ b/userspace/libsinsp/include/sinsp_external_processor.h @@ -0,0 +1,56 @@ +#pragma once + +/** + * This api defines a relationship between libsinsp and an external event processor. + * Such external processors should derive from event_processor and register themselves with + * sinsp in order to start receiving appropriate callbacks. + */ + +// Required until the chisel_api dependency on the analyzer can be removed +class statsd_metric; +class sinsp; +class threadinfo; + +namespace libsinsp +{ +enum event_return +{ + EVENT_RETURN_TIMEOUT, + EVENT_RETURN_EOF, + EVENT_RETURN_NONE +}; + +class event_processor +{ +public: + virtual ~event_processor() = default; + + /** + * Called when the inspector begins capturing. + * + * All event_processors should register with the sinsp before staring the capture. + * If that is not done, this function may never be called on the processor. + */ + virtual void on_capture_start() = 0; + + /** + * Called on every event after sinsp has performed its processing. + */ + virtual void process_event(sinsp_evt* evt, event_return rc) = 0; + + /** + * Required until the chisel_api dependency on the analyzer can be removed + */ + virtual void add_chisel_metric(statsd_metric* metric) = 0; + + /** + * Some event processors allocate different thread types with extra data. + * + * This allows the processor to override the thread builder. Note that + * If this is overridden by the event processor, the processor MUST be registered + * before the sinsp object is init-ed + */ + virtual sinsp_threadinfo* build_threadinfo(sinsp* inspector); +}; + +} // namespace libsinsp diff --git a/userspace/libsinsp/internal_metrics.cpp b/userspace/libsinsp/internal_metrics.cpp index 89d30d74e5..02b7de2ca9 100644 --- a/userspace/libsinsp/internal_metrics.cpp +++ b/userspace/libsinsp/internal_metrics.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "sinsp.h" diff --git a/userspace/libsinsp/internal_metrics.h b/userspace/libsinsp/internal_metrics.h index faf4214042..3149f960c1 100644 --- a/userspace/libsinsp/internal_metrics.h +++ b/userspace/libsinsp/internal_metrics.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once diff --git a/userspace/libsinsp/json_error_log.cpp b/userspace/libsinsp/json_error_log.cpp new file mode 100644 index 0000000000..edd333f6f1 --- /dev/null +++ b/userspace/libsinsp/json_error_log.cpp @@ -0,0 +1,119 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 + +#include +#include +#include + +#include "user_event_logger.h" +#include "json_error_log.h" + +json_error_log g_json_error_log; + +json_error_log::json_error_log() + : m_events_rate(.00333), // one event per 5 minutes + m_events_max_burst(10) +{ +} + +json_error_log::~json_error_log() +{ +} + +void json_error_log::set_json_parse_errors_file(const std::string& filename) +{ + m_json_parse_errors_file = filename; +} + +// Note: not changing the rate/burst of any already-created bucket +void json_error_log::set_events_rate(double events_rate, uint32_t max_burst) +{ + m_events_rate = events_rate; + m_events_max_burst = max_burst; +} + +void json_error_log::log(const std::string &json, const std::string &errstr, + uint64_t ts_ns, const std::string &uri) +{ + time_t now = ts_ns / ONE_SECOND_IN_NS; + + if(m_json_parse_errors_file != "") + { + std::ofstream errs(m_json_parse_errors_file, std::ofstream::out | std::ofstream::app); + char buf[sizeof("YYYY-MM-DDTHH:MM:SSZ")]; + strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&now)); + + errs << "*******************************" << std::endl; + errs << "URI: " << uri << std::endl; + errs << "Time (UTC): " << buf << std::endl; + errs << "Error: " << errstr << std::endl; + errs << "Json: " << json << std::endl; + errs << "*******************************" << std::endl; + + errs.close(); + } + + token_bucket &bucket = get_bucket(uri); + + if(bucket.claim(1, ts_ns)) + { + sinsp_user_event::tag_map_t tags; + tags["source"] = "json_parser"; + tags["uri"] = uri; + tags["json_prefix"] = json.substr(0, 100); + std::string event_name = "json_parse_error"; + std::string desc = errstr; + + event_scope scope; + if(m_machine_id.length()) + { + scope.add("host.mac", m_machine_id); + } + + // Also emit a custom event noting the json parse failure. + auto evt = sinsp_user_event(now, + std::move(event_name), + std::move(desc), + std::move(scope.get_ref()), + std::move(tags), + user_event_logger::SEV_EVT_WARNING); + + g_logger.log("Logging user event: " + evt.to_string(), sinsp_logger::SEV_DEBUG); + + user_event_logger::log(evt, user_event_logger::SEV_EVT_WARNING); + } +} + +token_bucket &json_error_log::get_bucket(const std::string &uri) +{ + auto it = m_buckets.lower_bound(uri); + + if(it == m_buckets.end() || + it->first != uri) + { + it = m_buckets.emplace_hint(it, + std::make_pair(uri, token_bucket())); + + it->second.init(m_events_rate, m_events_max_burst); + } + + return it->second; +} + diff --git a/userspace/libsinsp/json_error_log.h b/userspace/libsinsp/json_error_log.h new file mode 100644 index 0000000000..3b130fdd10 --- /dev/null +++ b/userspace/libsinsp/json_error_log.h @@ -0,0 +1,67 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include +#include + +#include "token_bucket.h" + +class json_error_log +{ +public: + + json_error_log(); + virtual ~json_error_log(); + + // Upon any json parsing error, write the error and json + // document that had the error to this file. Not enabled by + // default. + void set_json_parse_errors_file(const std::string& filename); + + void set_events_rate(double events_rate, uint32_t max_burst); + + // Possibly log a json parsing error, depending on whether or + // not the above filename was set. + void log(const std::string &json, const std::string &errstr, uint64_t ts_ns, const std::string &uri); + + void set_machine_id(const std::string& machine_id); + +private: + + // Return a token bucket limiting errors related to the + // configured uri, creating it if necessary. + token_bucket &get_bucket(const std::string &uri); + + std::string m_json_parse_errors_file; + std::string m_machine_id; + double m_events_rate; + uint32_t m_events_max_burst; + + // Rate-limit json parse error events by uri. + std::map m_buckets; +}; + +inline void json_error_log::set_machine_id(const std::string& machine_id) +{ + m_machine_id = machine_id; +} + +extern json_error_log g_json_error_log; + diff --git a/userspace/libsinsp/json_query.cpp b/userspace/libsinsp/json_query.cpp new file mode 100644 index 0000000000..9d2dd4c582 --- /dev/null +++ b/userspace/libsinsp/json_query.cpp @@ -0,0 +1,135 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// json_parser.h +// +// jq wrapper +// + +#ifdef __linux__ + +#include "json_query.h" +#include "sinsp.h" + +json_query::json_query(const std::string& json, const std::string& filter, bool dbg) : + m_jq(jq_init()), m_input{0}, m_result{0}, m_processed(false) +{ + if(!m_jq) { cleanup(); } + process(json, filter, dbg); +} + +json_query::~json_query() +{ + cleanup(); +} + +bool json_query::process(const std::string& json, const std::string& filter, bool dbg) +{ + cleanup(m_input); + cleanup(m_result); + clear(); + + if(!m_jq) { cleanup(); } + if(!jq_compile(m_jq, filter.c_str())) + { + m_error = "Filter parsing failed."; + return false; + } + + m_input = jv_parse/*_sized*/(json.c_str()/*, json.length()*/); + if (!jv_is_valid(m_input)) + { + cleanup(m_input, "JSON parse error."); + return false; + } + + jq_start(m_jq, m_input, dbg ? JQ_DEBUG_TRACE : 0); + m_input = jv_null(); // jq_start() freed it + m_result = jq_next(m_jq); + if (!jv_is_valid(m_result)) + { + cleanup(m_result, "json_query filtering result invalid."); + return false; + } + m_json = json; + m_filter = filter; + return m_processed = true; +} + +const std::string& json_query::result(int flags) +{ + if(m_processed) + { + static const std::string ret; + if(!m_error.empty()) { return ret; } + char* buf; + size_t len; + FILE* f = open_memstream(&buf, &len); + if(f == NULL) + { + m_error = "Can't open memory stream for writing."; + return ret; + } + jv_dumpf(m_result, f, flags); + m_result = jv_null(); + clear(); + fclose (f); + m_filtered_json.assign(buf, len); + free (buf); + m_processed = false; + } + return m_filtered_json; +} + +void json_query::clear() +{ + m_result = jv_null(); + m_input = jv_null(); + m_filtered_json.clear(); + m_error.clear(); + m_processed = false; +} + +void json_query::cleanup() +{ + if(m_jq) + { + cleanup(m_input); + cleanup(m_result); + clear(); + jq_teardown(&m_jq); + m_jq = 0; + } + else + { + throw std::runtime_error("json_query handle is null."); + } +} + +void json_query::cleanup(jv& j, const std::string& msg) +{ + if(j.u.ptr) + { + jv_free(j); + j = jv_null(); + } + m_error = msg; +} + +#endif // __linux__ diff --git a/userspace/libsinsp/json_query.h b/userspace/libsinsp/json_query.h new file mode 100644 index 0000000000..8d169921cc --- /dev/null +++ b/userspace/libsinsp/json_query.h @@ -0,0 +1,108 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +// json_parser.h +// +// jq wrapper +// + +#ifdef __linux__ + +#pragma once +// +// Avoid some annoying "function not used" from jq headers; +// this pragma affects this header file only and has no effect +// on source file including it; +// see https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html +// +#ifdef __GNUC__ +# pragma GCC system_header +#endif + +#include +#include +#include + +// jq is not C++-friendly +extern "C" +{ + #include "jv.h" + #include "jq.h" +} + +#include + +class json_query +{ +public: + json_query(const std::string& json = "", const std::string& filter = "", bool dbg = false); + ~json_query(); + + void set_json(const std::string& json); + const std::string& get_json() const; + + void set_filter(const std::string& filter); + const std::string& get_filter() const; + + bool process(const std::string& json, const std::string& filter, bool dbg = false); + const std::string& result(int flags = 0); + + const std::string& get_error() const; + +private: + void clear(); + void cleanup(); + void cleanup(jv& j, const std::string& msg = ""); + + jq_state* m_jq; + std::string m_json; + std::string m_filter; + std::string m_filtered_json; + jv m_input; + jv m_result; + bool m_processed; + mutable std::string m_error; +}; + +inline void json_query::set_json(const std::string& json) +{ + m_json = json; +} + +inline const std::string& json_query::get_json() const +{ + return m_json; +} + +inline void json_query::set_filter(const std::string& filter) +{ + m_filter = filter; +} + +inline const std::string& json_query::get_filter() const +{ + return m_filter; +} + +inline const std::string& json_query::get_error() const +{ + return m_error; +} + +#endif // __linux__ diff --git a/userspace/libsinsp/k8s.cpp b/userspace/libsinsp/k8s.cpp new file mode 100644 index 0000000000..fe8e778ade --- /dev/null +++ b/userspace/libsinsp/k8s.cpp @@ -0,0 +1,324 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s.h" +#include "k8s_component.h" +#include "k8s_dispatcher.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include +#include +#include +#include + +k8s_component::type_map k8s::m_components; + +k8s::k8s(const std::string& uri, bool is_captured, +#ifdef HAS_CAPTURE + ssl_ptr_t ssl, + bt_ptr_t bt, + bool block, +#endif // HAS_CAPTURE + filter_ptr_t event_filter, + ext_list_ptr_t extensions, + bool events_only) : + m_state(is_captured), + m_event_filter(event_filter) +#ifdef HAS_CAPTURE + ,m_net(uri.empty() ? + nullptr : new k8s_net(*this, m_state, uri, ssl, bt, event_filter, block)) +#endif +{ + g_logger.log(std::string("Creating K8s object for [" + + (uri.empty() ? std::string("capture replay") : uri) + ']'), + sinsp_logger::SEV_DEBUG); + if(m_components.empty()) + { + if(events_only) + { + m_components.insert({ k8s_component::K8S_EVENTS, "events"}); + return; + } + m_components.insert({ k8s_component::K8S_NODES, "nodes" }); + m_components.insert({ k8s_component::K8S_NAMESPACES, "namespaces" }); + m_components.insert({ k8s_component::K8S_PODS, "pods" }); + m_components.insert({ k8s_component::K8S_REPLICATIONCONTROLLERS, "replicationcontrollers" }); + m_components.insert({ k8s_component::K8S_SERVICES, "services" }); + if(event_filter) + { + m_components.insert({ k8s_component::K8S_EVENTS, "events"}); + } + if(extensions) + { + for(const auto& ext : *extensions) + { + if(ext == "daemonsets") + { + m_components.insert({ k8s_component::K8S_DAEMONSETS, "daemonsets" }); + } + else if(ext == "deployments") + { + m_components.insert({ k8s_component::K8S_DEPLOYMENTS, "deployments" }); + } + else if(ext == "replicasets") + { + m_components.insert({ k8s_component::K8S_REPLICASETS, "replicasets" }); + } + } + } + } +} + +k8s::~k8s() +{ + stop_watch(); + cleanup(); +} + +void k8s::stop_watch() +{ +#ifdef HAS_CAPTURE + if(m_net) + { + m_net->stop_watching(); + } +#endif +} + +void k8s::cleanup() +{ +#ifdef HAS_CAPTURE + delete m_net; + m_net = nullptr; +#endif +} + +void k8s::check_components() +{ +#ifdef HAS_CAPTURE + if(m_net) + { + for (auto it = m_components.cbegin(); it != m_components.cend();) + { + if(m_net->has_handler(*it)) + { + k8s_net::handler_ptr_t handler = k8s_net::get_handler(m_net->handlers(), *it); + if(handler) + { + k8s_handler::api_error_ptr handler_error = handler->error(); + // HTTP error > 400 means non-existing, forbidden, etc. + if(handler_error && handler_error->code() >= 400) + { + std::string handler_name = handler->name(); + if(!k8s_component::is_critical(handler_name)) + { + g_logger.log("K8s: removing " + handler_name + " due to HTTP error " + + std::to_string(handler_error->code()) + + ", reason: " + handler_error->reason() + + ", message: " + handler_error->message(), + sinsp_logger::SEV_WARNING); + m_components.erase(it++); + continue; + } + else + { + throw sinsp_exception(handler_error->to_string()); + } + } + } + } + else + { + if(it->first != k8s_component::K8S_EVENTS) + { + m_net->add_handler(*it); + } + else if(m_event_filter) // events only if filter is enabled + { + m_net->add_handler(*it); + } + } + ++it; + } + } + else + { + throw sinsp_exception("K8s net object is null."); + } +#endif +} + +const k8s_state_t& k8s::get_state() +{ + return m_state; +} + +void k8s::watch() +{ +#ifdef HAS_CAPTURE + if(m_net) + { + check_components(); + m_net->watch(); + } +#endif +} + +void k8s::simulate_watch_event(const std::string& json, int version) +{ + Json::Value root; + Json::Reader reader; + k8s_component::type component_type = k8s_component::K8S_COMPONENT_COUNT; + if(reader.parse(json, root, false)) + { + const Json::Value& kind = root["kind"]; + if(!kind.isNull() && kind.isString()) + { + std::string type = kind.asString(); + if(type == "Namespace") { component_type = k8s_component::K8S_NAMESPACES; } + else if(type == "Node") { component_type = k8s_component::K8S_NODES; } + else if(type == "Pod") { component_type = k8s_component::K8S_PODS; } + else if(type == "ReplicationController") { component_type = k8s_component::K8S_REPLICATIONCONTROLLERS; } + else if(type == "ReplicaSet") { component_type = k8s_component::K8S_REPLICASETS; } + else if(type == "Service") { component_type = k8s_component::K8S_SERVICES; } + else if(type == "DaemonSet") { component_type = k8s_component::K8S_DAEMONSETS; } + else if(type == "Deployment") { component_type = k8s_component::K8S_DEPLOYMENTS; } + else if(type == "EventList") { component_type = k8s_component::K8S_EVENTS; } + else + { + g_logger.log("Unrecognized component type: " + type, sinsp_logger::SEV_ERROR); + return; + } + } + else + { + g_logger.log("Component type not found in JSON", sinsp_logger::SEV_ERROR); + return; + } + } + else + { + g_logger.log("Error parsing JSON", sinsp_logger::SEV_ERROR); + return; + } + + if(m_state.get_capture_version() == k8s_state_t::CAPTURE_VERSION_NONE) + { + m_state.set_capture_version(version); + } + static bool version_logged = false; + if(!version_logged) + { + g_logger.log("K8s capture version: " + std::to_string(version), sinsp_logger::SEV_DEBUG); + version_logged = true; + } + switch(version) + { + case k8s_state_t::CAPTURE_VERSION_1: // old capture format + if(component_type < k8s_component::K8S_COMPONENT_COUNT) + { + if(m_dispatch_map.find(component_type) == m_dispatch_map.end()) + { + m_dispatch_map[component_type] = + std::unique_ptr(new k8s_dispatcher(component_type, m_state)); + } + m_dispatch_map[component_type]->extract_data(root, false); + } + else + { + throw sinsp_exception(std::string("K8s capture: unknown component type (") + + std::to_string(component_type) + ")"); + } + break; + case k8s_state_t::CAPTURE_VERSION_2: + if(component_type < k8s_component::K8S_COMPONENT_COUNT) + { + if(m_handler_map.find(component_type) == m_handler_map.end()) + { + m_handler_map[component_type] = k8s_net::make_handler(m_state, component_type, false); + } + if(m_handler_map[component_type]) + { + m_handler_map[component_type]->handle_json(std::move(root)); + } + else + { + throw sinsp_exception(std::string("K8s capture replay: error creating ") + + k8s_component::get_name(component_type) + + " handler"); + } + } + else + { + throw sinsp_exception(std::string("K8s capture: unknown component type (") + + std::to_string(component_type) + ")"); + } + break; + default: + throw sinsp_exception(std::string("K8s capture: invalid capture version (") + + std::to_string(version) + ")"); + } +} + +std::size_t k8s::count(k8s_component::type component) const +{ + switch (component) + { + case k8s_component::K8S_NODES: + return m_state.get_nodes().size(); + + case k8s_component::K8S_NAMESPACES: + return m_state.get_namespaces().size(); + + case k8s_component::K8S_PODS: + return m_state.get_pods().size(); + + case k8s_component::K8S_REPLICATIONCONTROLLERS: + return m_state.get_rcs().size(); + + case k8s_component::K8S_REPLICASETS: + return m_state.get_rss().size(); + + case k8s_component::K8S_SERVICES: + return m_state.get_services().size(); + + case k8s_component::K8S_DAEMONSETS: + return m_state.get_daemonsets().size(); + + case k8s_component::K8S_DEPLOYMENTS: + return m_state.get_deployments().size(); + + case k8s_component::K8S_EVENTS: + return m_state.get_events().size(); + + case k8s_component::K8S_COMPONENT_COUNT: + default: + break; + } + + std::ostringstream os; + os << "Unknown component " << static_cast(component); + throw sinsp_exception(os.str()); +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s.h b/userspace/libsinsp/k8s.h new file mode 100644 index 0000000000..7aa53ed953 --- /dev/null +++ b/userspace/libsinsp/k8s.h @@ -0,0 +1,150 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s.h +// +// extracts needed data from the k8s REST API interface +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "k8s_component.h" +#include "k8s_state.h" +#include "k8s_event_data.h" +#include "k8s_net.h" +#include "sinsp_auth.h" +#include +#include + +class k8s_dispatcher; + +class k8s +{ +public: +#ifdef HAS_CAPTURE + typedef sinsp_ssl::ptr_t ssl_ptr_t; + typedef sinsp_bearer_token::ptr_t bt_ptr_t; +#endif // HAS_CAPTURE + + typedef k8s_component::ext_list_ptr_t ext_list_ptr_t; + typedef user_event_filter_t::ptr_t filter_ptr_t; + + k8s(const std::string& uri = "http://localhost:80", + bool is_captured = false, +#ifdef HAS_CAPTURE + ssl_ptr_t ssl = 0, + bt_ptr_t bt = 0, + bool block = false, +#endif // HAS_CAPTURE + filter_ptr_t event_filter = nullptr, + ext_list_ptr_t extensions = nullptr, + bool events_only = false); + + ~k8s(); + + std::size_t count(k8s_component::type component) const; + + void check_components(); + + const k8s_state_t& get_state(); + void clear_events(); + void set_machine_id(const std::string& machine_id); + std::string get_machine_id() const; + + void watch(); + void stop_watching(); + + bool is_alive() const; + +#ifdef HAS_CAPTURE + typedef k8s_state_t::event_list_t event_list_t; + const event_list_t& get_capture_events() const { return m_state.get_capture_events(); } + std::string dequeue_capture_event() { return m_state.dequeue_capture_event(); } +#endif // HAS_CAPTURE + + // version: + // - 1 to support k8s events captured in old format (before refactoring) + // - 2 to support k8s events captured in new format (after refactoring) + void simulate_watch_event(const std::string& json, int version = 2); + +private: + void stop_watch(); + + void cleanup(); + + k8s_state_t m_state; + filter_ptr_t m_event_filter; + + typedef std::map> dispatch_map_t; + typedef std::map> handler_map_t; + // dispatch map is deprecated and serves only for backward compatibility with captures with old sysdig + dispatch_map_t m_dispatch_map; + handler_map_t m_handler_map; + +#ifdef HAS_CAPTURE + k8s_net* m_net = nullptr; +#endif + + // a utility member containing pairs of enumerated values and component names + static k8s_component::type_map m_components; + friend class k8s_test; +}; + +inline bool k8s::is_alive() const +{ +#ifdef HAS_CAPTURE + ASSERT(m_net); + return m_net->is_healthy(); +#endif + return true; +} + +inline void k8s::clear_events() +{ + m_state.clear_events(); +} + +inline std::string k8s::get_machine_id() const +{ +#ifdef HAS_CAPTURE + if(m_net) + { + return m_net->get_machine_id(); + } +#endif // HAS_CAPTURE + return ""; +} + +inline void k8s::set_machine_id(const std::string& machine_id) +{ +#ifdef HAS_CAPTURE + if(m_net) + { + m_net->set_machine_id(machine_id); + } + else + { + g_logger.log("K8s machine ID (MAC) setting attempted on null net object; " + "scope may not be available for events.", + sinsp_logger::SEV_WARNING); + } +#endif // HAS_CAPTURE +} +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_api_error.cpp b/userspace/libsinsp/k8s_api_error.cpp new file mode 100644 index 0000000000..4095eadf54 --- /dev/null +++ b/userspace/libsinsp/k8s_api_error.cpp @@ -0,0 +1,59 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_api_error.cpp +// + +#include "k8s_api_error.h" + +k8s_api_error::k8s_api_error(const msg_data& data, const Json::Value& err): + m_data(data), + m_status(get_string(err, "status")), + m_message(get_string(err, "message")), + m_reason(get_string(err, "reason")), + m_details(get_string(err, "details")), + m_code(get_int(err, "code")) +{ +} + +k8s_api_error::~k8s_api_error() +{ +} + +std::string k8s_api_error::get_string(const Json::Value& obj, const std::string& name) +{ + std::string value; + const Json::Value& val = obj[name]; + if(!val.isNull() && val.isConvertibleTo(Json::stringValue)) + { + value = val.asString(); + } + return value; +} + +int k8s_api_error::get_int(const Json::Value& obj, const std::string& name) +{ + int value = 0; + const Json::Value& val = obj[name]; + if(!val.isNull() && val.isConvertibleTo(Json::intValue)) + { + value = val.asInt(); + } + return value; +} diff --git a/userspace/libsinsp/k8s_api_error.h b/userspace/libsinsp/k8s_api_error.h new file mode 100644 index 0000000000..2933f4e541 --- /dev/null +++ b/userspace/libsinsp/k8s_api_error.h @@ -0,0 +1,125 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_api_error.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "k8s_component.h" + +class k8s_api_error +{ +public: + typedef k8s_component::msg_reason msg_reason; + typedef k8s_component::msg_data msg_data; + + k8s_api_error(const msg_data& data, const Json::Value& err); + + ~k8s_api_error(); + + const std::string& component_name() const; + const std::string& component_id() const; + const std::string& component_namespace() const; + const std::string& component_kind() const; + + const std::string& metadata() const; + const std::string& status() const; + const std::string& message() const; + const std::string& reason() const; + const std::string& details() const; + int code() const; + + std::string to_string() const; + +private: + static std::string get_string(const Json::Value& obj, const std::string& name); + static int get_int(const Json::Value& obj, const std::string& name); + + msg_data m_data; + std::string m_meta; + std::string m_status; + std::string m_message; + std::string m_reason; + std::string m_details; + int m_code; +}; + +inline const std::string& k8s_api_error::component_name() const +{ + return m_data.m_name; +} + +inline const std::string& k8s_api_error::component_id() const +{ + return m_data.m_uid; +} + +inline const std::string& k8s_api_error::component_namespace() const +{ + return m_data.m_namespace; +} + +inline const std::string& k8s_api_error::component_kind() const +{ + return m_data.m_kind; +} + +inline const std::string& k8s_api_error::metadata() const +{ + return m_meta; +} + +inline const std::string& k8s_api_error::status() const +{ + return m_status; +} + +inline const std::string& k8s_api_error::message() const +{ + return m_message; +} + +inline const std::string& k8s_api_error::reason() const +{ + return m_reason; +} + +inline const std::string& k8s_api_error::details() const +{ + return m_details; +} + +inline int k8s_api_error::code() const +{ + return m_code; +} + +inline std::string k8s_api_error::to_string() const +{ + std::ostringstream os; + os << "K8s API error; Status: " << m_status << ", " + "Message: " << m_message << ", " + "Reason: " << m_reason << ", " + "Details: " << m_details << ", " + "Code: " << m_message; + return os.str(); +} +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_api_handler.cpp b/userspace/libsinsp/k8s_api_handler.cpp new file mode 100644 index 0000000000..a43bec7264 --- /dev/null +++ b/userspace/libsinsp/k8s_api_handler.cpp @@ -0,0 +1,126 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_api_handler.cpp +// +#ifndef CYGWING_AGENT + +#ifdef HAS_CAPTURE + +#include "k8s_api_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +k8s_api_handler::k8s_api_handler(collector_ptr_t collector, + const std::string& url, + const std::string& path, + const std::string& filter, + const std::string& http_version +#ifdef HAS_CAPTURE + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool blocking_socket +#endif // HAS_CAPTURE +): + k8s_handler("k8s_api_handler", false, +#ifdef HAS_CAPTURE + url, path, filter, ".", "", collector, http_version, 1000L, ssl, bt, + false, true, std::make_shared(), blocking_socket, +#endif // HAS_CAPTURE + ~0, nullptr) +{ +} + +k8s_api_handler::~k8s_api_handler() +{ +} + +bool k8s_api_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + m_error = false; + if(!json.isNull()) + { + if(json.isArray()) + { + for(const auto& version : json) + { + if(version.isConvertibleTo(Json::stringValue)) + { + m_extensions.push_back(version.asString()); + } + else + { + g_logger.log("K8s API handler error: could not extract API versions or extensions from JSON.", + sinsp_logger::SEV_ERROR); + m_error = true; + return false; + } + } + } + else if(json.isConvertibleTo(Json::stringValue)) + { + m_extensions.push_back(json.asString()); + } + else + { + g_logger.log("K8s API handler error: could not extract API versions or extensions from JSON.", + sinsp_logger::SEV_ERROR); + m_error = true; + return false; + } + m_data_received = true; + } + else + { + g_logger.log("K8s API handler error: json is null.", sinsp_logger::SEV_ERROR); + m_error = true; + return false; + } + return true; +} + +void k8s_api_handler::handle_json(Json::Value&& root) +{ + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("K8S API handler [" + json_as_string(root) + "] reply:\n", + sinsp_logger::SEV_TRACE); + } + + handle_component(root); +} + +bool k8s_api_handler::has(const std::string& version) const +{ + for(const auto& ver : m_extensions) + { + if(ver == version) + { + return true; + } + } + return false; +} + +#endif // HAS_CAPTURE +#endif // CYGWING_AGENT + diff --git a/userspace/libsinsp/k8s_api_handler.h b/userspace/libsinsp/k8s_api_handler.h new file mode 100644 index 0000000000..f52b8d1738 --- /dev/null +++ b/userspace/libsinsp/k8s_api_handler.h @@ -0,0 +1,69 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_api_handler.h +// + +#ifdef HAS_CAPTURE + +#pragma once + +#include "json/json.h" +#include "k8s_handler.h" + +class k8s_api_handler : public k8s_handler +{ +public: + typedef std::vector api_list_t; + + k8s_api_handler(collector_ptr_t collector, + const std::string& url, + const std::string& path, + const std::string& filter, + const std::string& http_version = "1.1", + ssl_ptr_t ssl = 0, + bt_ptr_t bt = 0, + bool blocking_socket = false); + + ~k8s_api_handler(); + + bool error() const; + const api_list_t& extensions() const; + bool has(const std::string& version) const; + +private: + void handle_json(Json::Value&& root); + bool handle_component(const Json::Value& json, const msg_data* data = 0); + + + api_list_t m_extensions; + bool m_error = false; +}; + +inline bool k8s_api_handler::error() const +{ + return m_error; +} + +inline const k8s_api_handler::api_list_t& k8s_api_handler::extensions() const +{ + return m_extensions; +} + +#endif // HAS_CAPTURE diff --git a/userspace/libsinsp/k8s_component.cpp b/userspace/libsinsp/k8s_component.cpp new file mode 100644 index 0000000000..9f48ca4007 --- /dev/null +++ b/userspace/libsinsp/k8s_component.cpp @@ -0,0 +1,983 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_component.cpp +// + +#include "k8s_component.h" +#include "k8s_state.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "user_event.h" +#include +#include + +// +// container +// + +k8s_container::k8s_container() +{ +} + +k8s_container::k8s_container(const std::string& name, const port_list& ports): m_name(name), + m_ports(ports) +{ +} + +k8s_container::k8s_container(const k8s_container& other): m_name(other.m_name), + m_ports(other.m_ports) +{ +} + +k8s_container::k8s_container(k8s_container&& other): m_name(std::move(other.m_name)), + m_ports(std::move(other.m_ports)) +{ +} + +k8s_container& k8s_container::operator=(const k8s_container& other) +{ + m_name = other.m_name; + m_ports = other.m_ports; + return *this; +} + +bool k8s_container::has_port(const std::string& port_name) const +{ + for(const auto& port : m_ports) + { + if (port.get_name() == port_name) + { + return true; + } + } + return false; +} + +const k8s_container::port* k8s_container::get_port(const std::string& port_name) const +{ + for(const auto& port : m_ports) + { + if (port.get_name() == port_name) + { + return &port; + } + } + return 0; +} + +// +// component +// + +const k8s_component::type_map k8s_component::list = +{ + { k8s_component::K8S_NODES, "nodes" }, + { k8s_component::K8S_NAMESPACES, "namespaces" }, + { k8s_component::K8S_PODS, "pods" }, + { k8s_component::K8S_REPLICATIONCONTROLLERS, "replicationcontrollers" }, + { k8s_component::K8S_REPLICASETS, "replicasets" }, + { k8s_component::K8S_SERVICES, "services" }, + { k8s_component::K8S_DAEMONSETS, "daemonsets" }, + { k8s_component::K8S_DEPLOYMENTS, "deployments" }, + { k8s_component::K8S_EVENTS, "events" } +}; + +k8s_component::k8s_component(type comp_type, const std::string& name, const std::string& uid, const std::string& ns) : + m_type(comp_type), m_name(name), m_uid(uid), m_ns(ns) +{ +} + +k8s_component::~k8s_component() +{ +} + +k8s_pair_list k8s_component::extract_object(const Json::Value& object, const std::string& name) +{ + k8s_pair_list entry_list; + if(!object.isNull()) + { + const Json::Value& entries = object[name]; + if(!entries.isNull()) + { + Json::Value::Members members = entries.getMemberNames(); + for (auto& member : members) + { + const Json::Value& val = entries[member]; + if(!val.isNull() && val.isString()) + { + entry_list.emplace_back(k8s_pair_t(member, val.asString())); + } + } + } + } + return entry_list; +} + +std::string k8s_component::get_name_u(type t) +{ + switch (t) + { + case K8S_NAMESPACES: + return "NAMESPACE"; + case K8S_NODES: + return "NODE"; + case K8S_PODS: + return "POD"; + case K8S_REPLICATIONCONTROLLERS: + return "REPLICATIONCONTROLLER"; + case K8S_REPLICASETS: + return "REPLICASET"; + case K8S_SERVICES: + return "SERVICE"; + case K8S_DAEMONSETS: + return "DAEMONSET"; + case K8S_DEPLOYMENTS: + return "DEPLOYMENT"; + case K8S_EVENTS: + return "EVENT"; + case K8S_COMPONENT_COUNT: + default: + break; + } + + std::ostringstream os; + os << "Unknown component type " << static_cast(t); + throw sinsp_exception(os.str().c_str()); +} + +std::string k8s_component::get_name(type t) +{ + switch (t) + { + case K8S_NAMESPACES: + return "namespaces"; + case K8S_NODES: + return "nodes"; + case K8S_PODS: + return "pods"; + case K8S_REPLICATIONCONTROLLERS: + return "replicationcontrollers"; + case K8S_REPLICASETS: + return "replicasets"; + case K8S_SERVICES: + return "services"; + case K8S_DAEMONSETS: + return "daemonsets"; + case K8S_DEPLOYMENTS: + return "deployments"; + case K8S_EVENTS: + return "events"; + case K8S_COMPONENT_COUNT: + default: + break; + } + + std::ostringstream os; + os << "Unknown component type " << static_cast(t); + throw sinsp_exception(os.str().c_str()); +} + +k8s_component::type k8s_component::get_type(const std::string& name) +{ + if(name == "namespaces") + { + return K8S_NAMESPACES; + } + else if(name == "nodes") + { + return K8S_NODES; + } + else if(name == "pods") + { + return K8S_PODS; + } + else if(name == "replicationcontrollers") + { + return K8S_REPLICATIONCONTROLLERS; + } + else if(name == "replicasets") + { + return K8S_REPLICASETS; + } + else if(name == "services") + { + return K8S_SERVICES; + } + else if(name == "daemonsets") + { + return K8S_DAEMONSETS; + } + else if(name == "deployments") + { + return K8S_DEPLOYMENTS; + } + else if(name == "events") + { + return K8S_EVENTS; + } + + std::ostringstream os; + os << "K8s: Unknown component name " << name; + throw sinsp_exception(os.str().c_str()); +} + +std::string k8s_component::get_selector(type t) +{ + switch (t) + { + case K8S_PODS: + return "?fieldSelector=status.phase%3DRunning"; + default: + break; + } + return ""; +} + +std::string k8s_component::get_selector(const component_pair& p) +{ + return get_selector(p.first); +} + +std::string k8s_component::get_selector(const std::string& name) +{ + return get_selector(get_type(name)); +} + +bool k8s_component::is_critical(type t) +{ + switch (t) + { + case K8S_NODES: + case K8S_NAMESPACES: + case K8S_PODS: + case K8S_REPLICATIONCONTROLLERS: + case K8S_SERVICES: + return true; + case K8S_EVENTS: + case K8S_REPLICASETS: + case K8S_DAEMONSETS: + case K8S_DEPLOYMENTS: + default: + break; + } + return false; +} + +bool k8s_component::is_critical(const component_pair& p) +{ + return is_critical(p.first); +} + +bool k8s_component::is_critical(const std::string& name) +{ + return is_critical(get_type(name)); +} + +std::string k8s_component::get_api(type t, ext_list_ptr_t extensions) +{ + switch (t) + { + case K8S_NAMESPACES: + case K8S_NODES: + case K8S_PODS: + case K8S_REPLICATIONCONTROLLERS: + case K8S_SERVICES: + case K8S_EVENTS: + return "/api/v1/"; + case K8S_REPLICASETS: + case K8S_DAEMONSETS: + case K8S_DEPLOYMENTS: + if(extensions && extensions->size()) + { + return "/apis/extensions/v1beta1/"; + } + else + { + return ""; + } + case K8S_COMPONENT_COUNT: + default: + break; + } + + std::ostringstream os; + os << "K8s: Unknown component type " << static_cast(t); + throw sinsp_exception(os.str().c_str()); +} + +std::string k8s_component::get_api(const component_pair& p, ext_list_ptr_t extensions) +{ + return get_api(p.first, extensions); +} + +std::string k8s_component::get_api(const std::string& name, ext_list_ptr_t extensions) +{ + return get_api(get_type(name), extensions); +} + +k8s_pair_t* k8s_component::get_label(const k8s_pair_t& label) +{ + for (auto& lbl : m_labels) + { + if((lbl.first == label.first) && (lbl.second == label.second)) + { + return &lbl; + } + } + return 0; +} + +void k8s_component::add_labels(k8s_pair_list&& labels) +{ + for (auto& label : labels) + { + if(!get_label(label)) + { + emplace_label(std::move(label)); + } + } +} + +k8s_pair_t* k8s_component::get_selector(const k8s_pair_t& selector) +{ + for (auto& sel : m_selectors) + { + if((sel.first == selector.first) && (sel.second == selector.second)) + { + return &sel; + } + } + return 0; +} + +void k8s_component::add_selectors(k8s_pair_list&& selectors) +{ + for (auto& selector : selectors) + { + if(!get_selector(selector)) + { + emplace_selector(std::move(selector)); + } + } +} + +// TODO: proper selection process is more complicated, see “Labels and Selectors” at +// http://kubernetes.io/v1.0/docs/user-guide/labels.html +bool k8s_component::selector_in_labels(const k8s_pair_t& selector, const k8s_pair_list& labels) const +{ + if(!labels.size()) { return false; } + for(const auto& label : labels) + { + if(label.first == selector.first && label.second == selector.second) + { + return true; + } + } + return false; +} + +bool k8s_component::selectors_in_labels(const k8s_pair_list& labels) const +{ + const k8s_pair_list& selectors = get_selectors(); + if(!labels.size() || !selectors.size()) { return false; } + for(const auto& selector : selectors) + { + if(!selector_in_labels(selector, labels)) + { + return false; + } + } + return true; +} + +// +// namespace +// +k8s_ns_t::k8s_ns_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_component(COMPONENT_TYPE, name, uid, ns) +{ +} + + +// +// node +// + +k8s_node_t::k8s_node_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_component(COMPONENT_TYPE, name, uid, ns) +{ +} + +k8s_node_t::host_ip_list k8s_node_t::extract_addresses(const Json::Value& status) +{ + host_ip_list address_list; + if(!status.isNull()) + { + const Json::Value& addresses = status["addresses"]; + if(!addresses.isNull() && addresses.isArray()) + { + for (auto& address : addresses) + { + if(address.isObject()) + { + Json::Value::Members addr_names_list = address.getMemberNames(); + for (auto& entry : addr_names_list) + { + if(entry == "address") + { + const Json::Value& ip = address[entry]; + if(!ip.isNull()) + { + address_list.emplace(ip.asString()); + } + } + } + } + } + } + } + return address_list; +} + + +// +// pod +// + +k8s_pod_t::k8s_pod_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_component(COMPONENT_TYPE, name, uid, ns) +{ +} + +// +// replicas +// + +k8s_replicas_t::k8s_replicas_t(int spec_replicas, int stat_replicas): + m_spec_replicas(spec_replicas), + m_stat_replicas(stat_replicas) +{ +} + +int k8s_replicas_t::get_count(const Json::Value& item, const std::string& replica_name) +{ + if(!item.isNull()) + { + const Json::Value& replicas = item[replica_name]; + if(!replicas.isNull() && replicas.isConvertibleTo(Json::intValue)) + { + return replicas.asInt(); + } + } + + if(g_logger.get_severity() >= sinsp_logger::SEV_DEBUG) + { + g_logger.log("K8s: Can not find " + replica_name + " in \n" + Json::FastWriter().write(item), + sinsp_logger::SEV_DEBUG); + + std::string name; + const Json::Value& tpl = item["template"]; + if(!tpl.isNull()) + { + const Json::Value& md = tpl["metadata"]; + if(!md.isNull()) + { + const Json::Value& lbl = md["labels"]; + if(!lbl.isNull()) + { + const Json::Value& n = lbl["name"]; + if(!n.isNull() && n.isString()) + { + name = n.asString(); + } + else + { + const Json::Value& n = lbl["app"]; + if(!n.isNull() && n.isString()) + { + name = n.asString(); + } + } + } + } + } + + g_logger.log("K8s: Can not determine number of replicas" + + (name.empty() ? std::string() : std::string(" for ").append(name)), + sinsp_logger::SEV_DEBUG); + } + + return k8s_replicas_t::UNKNOWN_REPLICAS; +} + +void k8s_replicas_t::set_replicas(k8s_replicas_t& replicas, const Json::Value& item) +{ + int replica_count = k8s_replicas_t::get_count(item["spec"], "replicas"); + if(replica_count != k8s_replicas_t::UNKNOWN_REPLICAS) + { + replicas.set_spec_replicas(replica_count); + } + replica_count = k8s_replicas_t::get_count(item["status"], "replicas"); + if(replica_count != k8s_replicas_t::UNKNOWN_REPLICAS) + { + replicas.set_stat_replicas(replica_count); + } + else + { + int unavailable_replicas = k8s_replicas_t::get_count(item["status"], "unavailableReplicas"); + int spec_replicas = replicas.get_spec_replicas(); + if(spec_replicas != k8s_replicas_t::UNKNOWN_REPLICAS && unavailable_replicas < spec_replicas) + { + replicas.set_stat_replicas(spec_replicas - unavailable_replicas); + } + } +} + +// +// replication controller +// + +k8s_rc_t::k8s_rc_t(const std::string& name, const std::string& uid, const std::string& ns, k8s_component::type type) : + k8s_component(type, name, uid, ns) +{ +} + +std::vector k8s_rc_t::get_selected_pods(const std::vector& pods) const +{ + std::vector pod_vec; + for(const auto& pod : pods) + { + if(selectors_in_labels(pod.get_labels()) && get_namespace() == pod.get_namespace()) + { + pod_vec.push_back(&pod); + } + } + return pod_vec; +} + +void k8s_rc_t::set_replicas(int spec, int stat) +{ + set_spec_replicas(spec); + set_stat_replicas(stat); +} + +// +// replica set +// +k8s_rs_t::k8s_rs_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_rc_t(name, uid, ns, COMPONENT_TYPE) +{ +} + + +// +// service +// + +k8s_service_t::k8s_service_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_component(COMPONENT_TYPE, name, uid, ns) +{ +} + +std::vector k8s_service_t::get_selected_pods(const std::vector& pods) const +{ + std::vector pod_vec; + for(const auto& pod : pods) + { + if (selectors_in_labels(pod.get_labels()) && get_namespace() == pod.get_namespace()) + { + pod_vec.push_back(&pod); + } + } + return pod_vec; +} + + +// +// daemon set +// + +k8s_daemonset_t::k8s_daemonset_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_component(COMPONENT_TYPE, name, uid, ns) +{ +} + + +// +// deployment +// + +k8s_deployment_t::k8s_deployment_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_component(COMPONENT_TYPE, name, uid, ns) +{ +} + +std::vector k8s_deployment_t::get_selected_pods(const std::vector& pods) const +{ + std::vector pod_vec; + for(const auto& pod : pods) + { + if(selectors_in_labels(pod.get_labels()) && get_namespace() == pod.get_namespace()) + { + pod_vec.push_back(&pod); + } + } + return pod_vec; +} + +// +// event +// + +k8s_event_t::k8s_event_t(const std::string& name, const std::string& uid, const std::string& ns) : + k8s_component(COMPONENT_TYPE, name, uid, ns), + m_name_translation + { + // + // Event translations, based on: + // https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/container/event.go + // https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/controller_utils.go + // https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/node/nodecontroller.go + // https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/kubelet.go + // https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/daemon/controller.go + // https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/deployment/deployment_controller.go + // https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/deployment/util/deployment_util.go + // + + // + // Node + // + + // Node Controller + { "TerminatedAllPods", "Terminated All Pods"}, + { "RegisteredNode", "Node Registered"}, + { "RemovingNode", "Removing Node"}, + { "DeletingNode", "Deleting Node"}, + { "DeletingAllPods", "Deleting All Pods"}, + { "TerminatingEvictedPod", "Terminating Evicted Pod" }, + + // Kubelet + { "NodeReady", "Node Ready" }, + { "NodeNotReady", "Node not Ready" }, + { "NodeSchedulable", "Node is Schedulable" }, + { "NodeNotSchedulable", "Node is not Schedulable" }, + { "CIDRNotAvailable", "CIDR not Available" }, + { "CIDRAssignmentFailed", "CIDR Assignment Failed" }, + { "Starting", "Starting Kubelet" }, + { "KubeletSetupFailed", "Kubelet Setup Failed" }, + { "FailedMount", "Volume Mount Failed" }, + { "NodeSelectorMismatching", "Node Selector Mismatch" }, + { "InsufficientFreeCPU", "Insufficient Free CPU" }, + { "InsufficientFreeMemory", "Insufficient Free Memory" }, + { "OutOfDisk", "Out of Disk" }, + { "HostNetworkNotSupported", "Host Network not Supported" }, + { "NilShaper", "Undefined Shaper" }, + { "Rebooted", "Node Rebooted" }, + { "NodeHasSufficientDisk", "Node Has Sufficient Disk" }, + { "NodeOutOfDisk", "Node Out of Disk Space" }, + + // Image manager + { "InvalidDiskCapacity", "Invalid Disk Capacity" }, + { "FreeDiskSpaceFailed", "Free Disk Space Failed" }, + + // + // Pod + // + + // Image + { "Pulling", "Pulling Image" }, + { "Pulled", "Image Pulled" }, + { "Failed", "Container Image Pull, Create or Start Failed" }, + { "InspectFailed", "Image Inspect Failed" }, + { "ErrImageNeverPull", "Image NeverPull Policy Error" }, + { "BackOff", "Back Off Container Start or Image Pull" }, + + //{ "OutOfDisk" ,"Out of Disk" }, duplicate + + // Container + { "Created", "Container Created" }, + { "Started", "Container Started" }, + //{ "Failed", "Container Create or Start Failed" }, duplicate + { "Killing", "Killing Container" }, + + //{ "BackOff", "Backoff Start Container" }, duplicate + + // Probe + { "Unhealthy", "Container Unhealthy" }, + + // Pod worker + { "FailedSync", "Pod Sync Failed" }, + + // Config + { "FailedValidation", "Failed Configuration Validation" }, + { "HostPortConflict", "Host/Port Conflict" }, + + // + // Replication Controller + // + { "SuccessfulCreate", "Pod Created" }, + { "FailedCreate", "Pod Create Failed"}, + { "SuccessfulDelete", "Pod Deleted" }, + { "FailedDelete", "Pod Delete Failed"}, + + // + // Replica Set + // + // { "SuccessfulCreate", "Pod Created" }, duplicate + // { "FailedCreate", "Pod Create Failed"}, duplicate + // { "SuccessfulDelete", "Pod Deleted" }, duplicate + // { "FailedDelete", "Pod Delete Failed"} duplicate + + // + // Deployment + // + { "SelectingAll", "Selecting All Pods" }, + { "ScalingReplicaSet", "Scaling Replica Set" }, + { "DeploymentRollbackRevisionNotFound", "No revision to roll back" }, + { "DeploymentRollbackTemplateUnchanged", "Skipping Rollback" }, + { "DeploymentRollback", "Rollback Done" } + + // + // Daemon Set + // + // { "SelectingAll", "Selecting All Pods" } duplicate + } +{ +} + +void k8s_event_t::post_process(k8s_state_t& state) +{ + for(auto it = m_postponed_events.cbegin(); it != m_postponed_events.end();) + { + g_logger.log("K8s event: " + std::to_string(m_postponed_events.size()) + " postponed events. " + "post-processing event [" + it->first + "] ...", sinsp_logger::SEV_TRACE); + m_force_delete = false; + bool updated = update(it->second, state); + if(updated || m_force_delete) + { + g_logger.log("K8s event: event [" + it->first + + "] post-processed.", sinsp_logger::SEV_TRACE); + m_postponed_events.erase(it++); + } + else + { + g_logger.log("K8s event: event [" + it->first + "] not post-processed. There's " + + std::to_string(m_postponed_events.size()) + + " postponed events pending.", sinsp_logger::SEV_TRACE); + ++it; + } + } +} + +bool k8s_event_t::update(const Json::Value& item, k8s_state_t& state) +{ +#ifndef _WIN32 + time_t epoch_time_evt_s = 0; + time_t epoch_time_now_s = get_epoch_utc_seconds_now(); + std::string event_name; + std::string description; + severity_t severity = user_event_logger::SEV_EVT_INFORMATION; + event_scope scope; + tag_map_t tags; + + const Json::Value& obj = item["involvedObject"]; + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("K8s EVENT: \n" + json_as_string(item), sinsp_logger::SEV_TRACE); + } + if(!obj.isNull()) + { + std::string sev = get_json_string(item, "type"); + // currently, only "Normal" and "Warning" + severity = user_event_logger::SEV_EVT_INFORMATION; + if(sev == "Warning") { severity = user_event_logger::SEV_EVT_WARNING; } + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("K8s EVENT:" + "\nnamespace = " + get_json_string(obj, "namespace") + + "\nname = " + get_json_string(obj, "name") + + "\nuid = " + get_json_string(obj, "uid") + + "\ntype = " + get_json_string(obj, "kind") + + "\nseverity = " + get_json_string(item, "type") + " (" + std::to_string(severity) + ')', sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("K8s event: cannot get involved object (null)", sinsp_logger::SEV_ERROR); + m_force_delete = true; + return false; + } + + std::string ts = get_json_string(item , "lastTimestamp"); + if(!ts.empty()) + { + if((epoch_time_evt_s = get_epoch_utc_seconds(ts)) == (time_t) -1) + { + g_logger.log("K8s event: cannot convert [" + ts + "] to epoch timestamp", sinsp_logger::SEV_ERROR); + } + g_logger.log("K8s EVENT update: time:" + std::to_string(epoch_time_evt_s), sinsp_logger::SEV_DEBUG); + } + else + { + g_logger.log("K8s event: cannot convert time (null, empty or not string)", sinsp_logger::SEV_ERROR); + } + event_name = get_json_string(item , "reason"); + const auto& translation = m_name_translation.find(event_name); + if(translation != m_name_translation.end()) + { + event_name = translation->second; + } + description = get_json_string(item, "message"); + g_logger.log("K8s EVENT message:" + description, sinsp_logger::SEV_DEBUG); + + // Although it's easier and more efficient to obtain the involved object data from + // the event itself, there is a downside - event may not carry the data in the + // same format as reported in metadata protobuf (generated from k8s state); + // an example is IP address vs. DNS name for node, there may be other cases. + // For that reason, we try to obtain info about involved object from state; if object is + // not found in state (due to undefined arrival order of event and metadata messages), + // we get scope data from the event itself. + std::string component_uid = get_json_string(obj, "uid"); + g_logger.log("K8s event UID:" + component_uid, sinsp_logger::SEV_TRACE); + if(!component_uid.empty()) + { + g_logger.log("K8s event: seconds since event occurred:" + std::to_string(epoch_time_now_s - epoch_time_evt_s), + sinsp_logger::SEV_TRACE); + std::string t; + const k8s_component* comp = state.get_component(component_uid, &t); + if(comp && !t.empty()) + { + const std::string& node_name = comp->get_node_name(); + if(!node_name.empty()) + { + scope.add("kubernetes.node.name", node_name); + } + const std::string& ns = comp->get_namespace(); + if(!ns.empty()) + { + scope.add("kubernetes.namespace.name", ns); + } + const std::string& comp_name = comp->get_name(); + if(!comp_name.empty()) + { + scope.add(std::string("kubernetes.").append(t).append(".name"), comp_name); + } + /* no labels for now + for(const auto& label : comp->get_labels()) + { + tags[label.first] = label.second; + //g_logger.log("EVENT label: [" + label.first + ':' + label.second + ']', sinsp_logger::SEV_DEBUG); + if(event_scope::check(label.second)) + { + scope.append(" and kubernetes.").append(t).append(".label.").append(label.first).append(1, '=').append(label.second); + } + else + { + g_logger.log("K8s invalid scope entry: [" + label.second + ']', sinsp_logger::SEV_WARNING); + } + }*/ + } + else + { + g_logger.log("K8s event: cannot obtain component (component with UID [" + component_uid + + "] not found), trying to build scope directly from event ...", sinsp_logger::SEV_TRACE); + make_scope(obj, scope); + } + } + else + { + g_logger.log("K8s event: cannot obtain component UID, trying to build scope directly from event ...", + sinsp_logger::SEV_TRACE); + make_scope(obj, scope); + } + + tags["source"] = "kubernetes"; + + auto evt = sinsp_user_event(epoch_time_evt_s, + std::move(event_name), + std::move(description), + std::move(scope.get_ref()), + std::move(tags), + severity); + + user_event_logger::log(evt, severity); + + // TODO: sysdig capture? +#endif // _WIN32 + + return true; +} + +void k8s_event_t::make_scope_impl(const Json::Value& obj, std::string comp, event_scope& scope, bool ns) +{ + if(ns) + { + const std::string& ns_name = get_json_string(obj, "namespace"); + if(!ns_name.empty()) + { + scope.add("kubernetes.namespace.name", ns_name); + } + } + if(comp.length() && ci_compare::is_equal(get_json_string(obj, "kind"), comp)) + { + const std::string& comp_name = get_json_string(obj, "name"); + if(!comp_name.empty()) + { + comp[0] = tolower(comp[0]); + scope.add(std::string("kubernetes.").append(comp).append(".name"), comp_name); + } + if(comp_name.empty()) + { + g_logger.log("K8s " + comp + " event detected but " + comp + " name could not be determined. Scope will be empty.", sinsp_logger::SEV_WARNING); + } + } + else + { + g_logger.log("K8s event detected but component name was empty. Scope will be empty.", sinsp_logger::SEV_WARNING); + } +} + +void k8s_event_t::make_scope(const Json::Value& obj, event_scope& scope) +{ + if(ci_compare::is_equal(get_json_string(obj, "kind"), "Pod")) + { + make_scope_impl(obj, "Pod", scope); + } + else if(ci_compare::is_equal(get_json_string(obj, "kind"), "ReplicationController")) + { + make_scope_impl(obj, "ReplicationController", scope); + } + else if(ci_compare::is_equal(get_json_string(obj, "kind"), "Node")) + { + make_scope_impl(obj, "Node", scope, false); + } + else if(ci_compare::is_equal(get_json_string(obj, "kind"), "ReplicaSet")) + { + make_scope_impl(obj, "ReplicaSet", scope); + } + else if(ci_compare::is_equal(get_json_string(obj, "kind"), "Deployment")) + { + make_scope_impl(obj, "Deployment", scope); + } + else if(ci_compare::is_equal(get_json_string(obj, "kind"), "DaemonSet")) + { + make_scope_impl(obj, "DaemonSet", scope); + } +} diff --git a/userspace/libsinsp/k8s_component.h b/userspace/libsinsp/k8s_component.h new file mode 100644 index 0000000000..4cb7503f5d --- /dev/null +++ b/userspace/libsinsp/k8s_component.h @@ -0,0 +1,1069 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_component.h +// +// kubernetes components (nodes, namespaces, pods, replication controllers, services) +// abstraction +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "logger.h" +#include "user_event.h" +#include "user_event_logger.h" +#include +#include + +typedef std::pair k8s_pair_t; +typedef std::vector k8s_pair_list; + +class k8s_pod_t; +class k8s_service_t; + +class k8s_container +{ +public: + typedef std::vector list; + + class port + { + public: + void set_name(const std::string& name); + + const std::string& get_name() const; + + void set_port(uint32_t port); + + uint32_t get_port() const; + + void set_protocol(const std::string& protocol); + + const std::string& get_protocol() const; + + bool operator==(const port& other) const; + + bool operator!=(const port& other) const; + + private: + std::string m_name; + uint32_t m_port = 0; + std::string m_protocol; + }; + + typedef std::vector port_list; + + k8s_container(); + + k8s_container(const std::string& name, const port_list& ports); + + k8s_container(const k8s_container& other); + + k8s_container(k8s_container&& other); + + k8s_container& operator=(const k8s_container& other); + + bool has_port(const std::string& port_name) const; + + const port* get_port(const std::string& port_name) const; + + void set_name(const std::string& name); + + const std::string& get_name() const; + + bool operator==(const k8s_container& other) const; + + bool operator!=(const k8s_container& other) const; + +private: + std::string m_name; + port_list m_ports; +}; + +// +// component +// + +class k8s_component +{ +public: + enum type + { + K8S_NODES, + K8S_NAMESPACES, + K8S_PODS, + K8S_REPLICATIONCONTROLLERS, + K8S_SERVICES, + K8S_EVENTS, + K8S_REPLICASETS, + K8S_DAEMONSETS, + K8S_DEPLOYMENTS, + K8S_COMPONENT_COUNT + }; + + typedef std::set ext_list_t; + typedef std::shared_ptr ext_list_ptr_t; + typedef std::pair component_pair; + typedef std::map type_map; + static const type_map list; + enum msg_reason + { + COMPONENT_ADDED, + COMPONENT_MODIFIED, + COMPONENT_DELETED, + COMPONENT_ERROR, + COMPONENT_NONEXISTENT, + COMPONENT_UNKNOWN // only to mark bad event messages + }; + + struct msg_data + { + msg_reason m_reason = COMPONENT_UNKNOWN; + std::string m_name; + std::string m_uid; + std::string m_namespace; + std::string m_kind; + + bool is_valid() const + { + return m_reason != COMPONENT_UNKNOWN; + } + + std::string get_reason_desc() const + { + switch(m_reason) + { + case COMPONENT_ADDED: return "ADDED"; + case COMPONENT_MODIFIED: return "MODIFIED"; + case COMPONENT_DELETED: return "DELETED"; + case COMPONENT_ERROR: return "ERROR"; + case COMPONENT_NONEXISTENT: return "NONEXISTENT"; + case COMPONENT_UNKNOWN: + default: return "UNKNOWN"; + } + return "UNKNOWN"; + } + }; + + k8s_component() = delete; + + k8s_component(type comp_type, const std::string& name, const std::string& uid, const std::string& ns = ""); + + virtual ~k8s_component(); + + const std::string& get_name() const; + + void set_name(const std::string& name); + + const std::string& get_uid() const; + + void set_uid(const std::string& uid); + + const std::string& get_namespace() const; + + void set_namespace(const std::string& ns); + + k8s_pair_t* get_label(const k8s_pair_t& label); + + const k8s_pair_list& get_labels() const; + + void set_labels(k8s_pair_list&& labels); + + void add_labels(k8s_pair_list&& labels); + + void swap_labels(k8s_pair_list& new_labels); + + void push_label(const k8s_pair_t& label); + + void emplace_label(const k8s_pair_t& label); + + k8s_pair_t* get_selector(const k8s_pair_t& selector); + + const k8s_pair_list& get_selectors() const; + + void set_selectors(k8s_pair_list&& selectors); + + void add_selectors(k8s_pair_list&& selectors); + + void swap_selectors(k8s_pair_list& new_selectors); + + void push_selector(const k8s_pair_t& selector); + + void emplace_selector(k8s_pair_t&& selector); + + virtual std::string get_node_name() const; + + template + static void extract_string_array(const Json::Value& arr, C& list) + { + if(!arr.isNull() && arr.isArray()) + { + for (auto& item : arr) + { + if(item.isConvertibleTo(Json::stringValue)) + { + list.emplace(item.asString()); + } + } + } + } + + static k8s_pair_list extract_object(const Json::Value& object, const std::string& name); + + static const std::string& get_name(const component_pair& p); + + static std::string get_name(type t); + static std::string get_name_u(type t); + + static type get_type(const component_pair& p); + + static type get_type(const std::string& name); + + static std::string get_api(type t, ext_list_ptr_t extensions = nullptr); + static std::string get_api(const component_pair& p, ext_list_ptr_t extensions = nullptr); + static std::string get_api(const std::string& name, ext_list_ptr_t extensions = nullptr); + + static std::string get_selector(type t); + static std::string get_selector(const component_pair& p); + static std::string get_selector(const std::string& name); + + static bool is_critical(type t); + static bool is_critical(const component_pair& p); + static bool is_critical(const std::string& name); + + bool selector_in_labels(const k8s_pair_t& selector, const k8s_pair_list& labels) const; + + bool selectors_in_labels(const k8s_pair_list& labels) const; + +private: + + type m_type; + std::string m_name; + std::string m_uid; + std::string m_ns; + k8s_pair_list m_labels; + k8s_pair_list m_selectors; + + friend class k8s_state_t; + friend class k8s_dispatcher; +}; + + +// +// namespace +// + +class k8s_ns_t : public k8s_component +{ +public: + static const k8s_component::type COMPONENT_TYPE = K8S_NAMESPACES; + k8s_ns_t(const std::string& name, const std::string& uid, const std::string& ns = ""); +}; + + +// +// node +// + +class k8s_node_t : public k8s_component +{ +public: + typedef std::unordered_set host_ip_list; + + static const k8s_component::type COMPONENT_TYPE = K8S_NODES; + + k8s_node_t(const std::string& name, const std::string& uid, const std::string& ns = ""); + + const host_ip_list& get_host_ips() const; + + void set_host_ips(host_ip_list&& host_ips); + + void add_host_ips(host_ip_list&& host_ips); + + void emplace_host_ip(std::string&& host_ip); + + virtual std::string get_node_name() const; + + static host_ip_list extract_addresses(const Json::Value& status); + +private: + host_ip_list m_host_ips; +}; + + +// +// pod +// + +class k8s_pod_t : public k8s_component +{ +public: + typedef std::vector container_id_list; + typedef k8s_container::list container_list; + + static const k8s_component::type COMPONENT_TYPE = K8S_PODS; + + k8s_pod_t(const std::string& name, const std::string& uid, const std::string& ns = ""); + + // container IDs + const container_id_list& get_container_ids() const; + void set_container_ids(container_id_list&& container_ids); + void add_container_ids(container_id_list&& container_ids); + void push_container_id(const std::string& container_id); + void emplace_container_id(std::string&& container_id); + + // restart count + size_t get_restart_count() const; + void set_restart_count(int rc); + + // containers + const container_list& get_containers() const; + void set_containers(container_list&& containers); + void add_containers(container_list&& containers); + void push_container(const k8s_container& container); + void emplace_container(k8s_container&& container); + + // node name, host IP and internal IP + virtual std::string get_node_name() const; + void set_node_name(const std::string& name); + const std::string& get_host_ip() const; + void set_host_ip(const std::string& host_ip); + const std::string& get_internal_ip() const; + void set_internal_ip(const std::string& internal_ip); + + // comparison + bool operator==(const k8s_pod_t& other) const; + bool operator!=(const k8s_pod_t& other) const; + +private: + container_id_list m_container_ids; + container_list m_containers; + std::string m_node_name; + std::string m_host_ip; + std::string m_internal_ip; + int m_restart_count_tot = -1; + mutable int m_restart_count_diff = 0; +}; + + +// +// replicas +// + +class k8s_replicas_t +{ +public: + static const int UNKNOWN_REPLICAS = -1; + + k8s_replicas_t(int spec_replicas = UNKNOWN_REPLICAS, int stat_replicas = UNKNOWN_REPLICAS); + + void set_spec_replicas(int replicas); + int get_spec_replicas() const; + void set_stat_replicas(int replicas); + int get_stat_replicas() const; + + static int get_count(const Json::Value& item, const std::string& replica_name = "replicas"); + static void set_replicas(k8s_replicas_t& replicas, const Json::Value& item); + +protected: + int m_spec_replicas = UNKNOWN_REPLICAS; + int m_stat_replicas = UNKNOWN_REPLICAS; +}; + + +// +// replication controller +// + +class k8s_rc_t : public k8s_component +{ +public: + static const k8s_component::type COMPONENT_TYPE = K8S_REPLICATIONCONTROLLERS; + + k8s_rc_t(const std::string& name, + const std::string& uid, + const std::string& ns = "", + k8s_component::type type = K8S_REPLICATIONCONTROLLERS); + + std::vector get_selected_pods(const std::vector& pods) const; + + void set_spec_replicas(int replicas); + int get_spec_replicas() const; + void set_stat_replicas(int replicas); + int get_stat_replicas() const; + void set_replicas(const Json::Value& item, const std::string& replica_name = "replicas"); + void set_replicas(int spec, int stat); + +protected: + k8s_replicas_t m_replicas; +}; + + +// +// replica set +// + +class k8s_rs_t : public k8s_rc_t +{ +public: + static const k8s_component::type COMPONENT_TYPE = K8S_REPLICASETS; + + k8s_rs_t(const std::string& name, const std::string& uid, const std::string& ns = ""); + +private: +}; + + +// +// service +// + +class k8s_service_t : public k8s_component +{ +public: + struct net_port + { + uint32_t m_port = 0; + std::string m_protocol; + uint32_t m_target_port = 0; + uint32_t m_node_port = 0; + }; + + typedef std::vector port_list; + + static const k8s_component::type COMPONENT_TYPE = K8S_SERVICES; + + k8s_service_t(const std::string& name, const std::string& uid, const std::string& ns = ""); + + const std::string& get_cluster_ip() const; + + void set_cluster_ip(const std::string& cluster_ip); + + const port_list& get_port_list() const; + + void set_port_list(port_list&& ports); + + std::vector get_selected_pods(const std::vector& pods) const; + +private: + std::string m_cluster_ip; + port_list m_ports; +}; + + +// +// daemon set +// + +class k8s_daemonset_t : public k8s_component +{ +public: + static const k8s_component::type COMPONENT_TYPE = K8S_DAEMONSETS; + + k8s_daemonset_t(const std::string& name, const std::string& uid, const std::string& ns = ""); + + void set_desired_scheduled(int replicas); + int get_desired_scheduled() const; + void set_current_scheduled(int replicas); + int get_current_scheduled() const; + void set_scheduled(const Json::Value& item); + void set_scheduled(int desired, int current); + +private: + k8s_replicas_t m_replicas; +}; + + +// +// deployment +// + +class k8s_deployment_t : public k8s_component +{ +public: + static const k8s_component::type COMPONENT_TYPE = K8S_DEPLOYMENTS; + + k8s_deployment_t(const std::string& name, const std::string& uid, const std::string& ns = ""); + + void set_spec_replicas(int replicas); + int get_spec_replicas() const; + void set_stat_replicas(int replicas); + int get_stat_replicas() const; + void set_replicas(const Json::Value& item); + void set_replicas(int desired, int current); + + std::vector get_selected_pods(const std::vector& pods) const; + +private: + k8s_replicas_t m_replicas; +}; + + +// +// event +// + +class k8s_state_t; +class event_scope; + +class k8s_event_t : public k8s_component +{ +public: + static const k8s_component::type COMPONENT_TYPE = K8S_EVENTS; + + k8s_event_t(const std::string& name, const std::string& uid, const std::string& ns); + + bool update(const Json::Value& item, k8s_state_t& state); + void post_process(k8s_state_t& state); + bool has_pending_events() const; + +private: + typedef sinsp_user_event::tag_map_t tag_map_t; + typedef user_event_logger::severity severity_t; + typedef std::unordered_map name_translation_map_t; + + void make_scope(const Json::Value& obj, event_scope& scope); + void make_scope_impl(const Json::Value& obj, std::string comp, event_scope& scope, bool ns = true); + + name_translation_map_t m_name_translation; + std::map m_postponed_events; + bool m_force_delete = false; +}; + +typedef std::vector k8s_namespaces; +typedef std::vector k8s_nodes; +typedef std::vector k8s_pods; +typedef std::vector k8s_controllers; +typedef std::vector k8s_replicasets; +typedef std::vector k8s_services; +typedef std::vector k8s_daemonsets; +typedef std::vector k8s_deployments; +typedef std::vector k8s_events; + +// +// container +// + +inline const std::string& k8s_container::get_name() const +{ + return m_name; +} + +inline void k8s_container::set_name(const std::string& name) +{ + m_name = name; +} + +inline bool k8s_container::operator==(const k8s_container& other) const +{ + if(&other == this) { return true; } + return (other.m_name == m_name) && (other.m_ports == m_ports); +} + +inline bool k8s_container::operator!=(const k8s_container& other) const +{ + if(&other == this) { return false; } + return !(other == *this); +} + +// +// container::port +// + +inline void k8s_container::port::set_name(const std::string& name) +{ + m_name = name; +} + +inline const std::string& k8s_container::port::get_name() const +{ + return m_name; +} + +inline void k8s_container::port::set_port(uint32_t port) +{ + m_port = port; +} + +inline uint32_t k8s_container::port::get_port() const +{ + return m_port; +} + +inline void k8s_container::port::set_protocol(const std::string& protocol) +{ + m_protocol = protocol; +} + +inline const std::string& k8s_container::port::get_protocol() const +{ + return m_protocol; +} + +inline bool k8s_container::port::operator==(const port& other) const +{ + if(&other == this) { return true; } + return other.m_name == m_name && + other.m_port == m_port && + other.m_protocol == m_protocol; +} + +inline bool k8s_container::port::operator!=(const port& other) const +{ + if(&other == this) { return false; } + return !(other == *this); +} + + +// +// component +// + +inline const std::string& k8s_component::get_name() const +{ + return m_name; +} + +inline void k8s_component::set_name(const std::string& name) +{ + m_name = name; +} + +inline const std::string& k8s_component::get_uid() const{ + + return m_uid; +} + +inline void k8s_component::set_uid(const std::string& uid) +{ + m_uid = uid; +} + +inline const std::string& k8s_component::get_namespace() const +{ + return m_ns; +} + +inline void k8s_component::set_namespace(const std::string& ns) +{ + m_ns = ns; +} + +inline const k8s_pair_list& k8s_component::get_labels() const +{ + return m_labels; +} + +inline void k8s_component::set_labels(k8s_pair_list&& labels) +{ + m_labels = std::move(labels); +} + +inline void k8s_component::swap_labels(k8s_pair_list& new_labels) +{ + m_labels.swap(new_labels); +} + +inline void k8s_component::push_label(const k8s_pair_t& label) +{ + m_labels.push_back(label); +} + +inline void k8s_component::emplace_label(const k8s_pair_t& label) +{ + m_labels.emplace_back(label); +} + +inline const k8s_pair_list& k8s_component::get_selectors() const +{ + return m_selectors; +} + +inline void k8s_component::set_selectors(k8s_pair_list&& selectors) +{ + m_selectors = std::move(selectors); +} + +inline void k8s_component::swap_selectors(k8s_pair_list& new_selectors) +{ + m_selectors.swap(new_selectors); +} + +inline void k8s_component::push_selector(const k8s_pair_t& selector) +{ + m_selectors.push_back(selector); +} + +inline void k8s_component::emplace_selector(k8s_pair_t&& selector) +{ + m_selectors.emplace_back(std::move(selector)); +} + +inline const std::string& k8s_component::get_name(const component_pair& p) +{ + return p.second; +} + +inline k8s_component::type k8s_component::get_type(const component_pair& p) +{ + return p.first; +} + +inline std::string k8s_component::get_node_name() const +{ + return ""; +} + +// +// node +// + +inline const k8s_node_t::host_ip_list& k8s_node_t::get_host_ips() const +{ + return m_host_ips; +} + +inline void k8s_node_t::set_host_ips(host_ip_list&& host_ips) +{ + m_host_ips = std::move(host_ips); +} + +inline void k8s_node_t::add_host_ips(host_ip_list&& host_ips) +{ + m_host_ips.insert(host_ips.begin(), host_ips.end()); +} + +inline void k8s_node_t::emplace_host_ip(std::string&& host_ip) +{ + m_host_ips.emplace(std::move(host_ip)); +} + +inline std::string k8s_node_t::get_node_name() const +{ + return get_name(); +} + +// +// pod +// + +// container IDs + +inline const k8s_pod_t::container_id_list& k8s_pod_t::get_container_ids() const +{ + return m_container_ids; +} + +inline void k8s_pod_t::set_container_ids(container_id_list&& container_ids) +{ + m_container_ids = std::move(container_ids); +} + +inline void k8s_pod_t::add_container_ids(container_id_list&& container_ids) +{ + m_container_ids.insert(m_container_ids.end(), container_ids.begin(), container_ids.end()); +} + +inline void k8s_pod_t::push_container_id(const std::string& container_id) +{ + m_container_ids.push_back(container_id); +} + +inline void k8s_pod_t::emplace_container_id(std::string&& container_id) +{ + m_container_ids.emplace_back(std::move(container_id)); +} + +// restart count + +inline size_t k8s_pod_t::get_restart_count() const +{ + int restart_count_diff = m_restart_count_diff; + m_restart_count_diff = 0; + return restart_count_diff; +} + +inline void k8s_pod_t::set_restart_count(int rc) +{ + if(rc < 0) + { + g_logger.log("Unexpected K8S pod restart count received: " + std::to_string(rc), + sinsp_logger::SEV_WARNING); + return; + } + + // only record current total on first call + if(m_restart_count_tot == -1) + { + m_restart_count_tot = rc; + return; + } + + if(rc >= m_restart_count_tot) + { + m_restart_count_diff = rc - m_restart_count_tot; + } + else + { + g_logger.log("Unexpected K8S pod restart count received (" + std::to_string(rc) + + ", last recorded value " + std::to_string(m_restart_count_tot) + "), resetting diff to zero.", + sinsp_logger::SEV_WARNING); + m_restart_count_diff = 0; + } + m_restart_count_tot = rc; +} + +// comparison + +inline bool k8s_pod_t::operator==(const k8s_pod_t& other) const +{ + if(&other == this) { return true; } + return other.m_container_ids == m_container_ids && + other.m_containers == m_containers && + other.m_host_ip == m_host_ip && + other.m_internal_ip == m_internal_ip; +} + +inline bool k8s_pod_t::operator!=(const k8s_pod_t& other) const +{ + if(&other == this) { return false; } + return !(other == *this); +} + +// containers + +inline const k8s_pod_t::container_list& k8s_pod_t::get_containers() const +{ + return m_containers; +} + +inline void k8s_pod_t::set_containers(container_list&& containers) +{ + m_containers = std::move(containers); +} + +inline void k8s_pod_t::add_containers(container_list&& containers) +{ + m_containers.insert(m_containers.end(), containers.begin(), containers.end()); +} + +inline void k8s_pod_t::push_container(const k8s_container& container) +{ + m_containers.push_back(container); +} + +inline void k8s_pod_t::emplace_container(k8s_container&& container) +{ + m_containers.emplace_back(std::move(container)); +} + +// getters/setters +inline std::string k8s_pod_t::get_node_name() const +{ + return m_node_name; +} + +inline void k8s_pod_t::set_node_name(const std::string& name) +{ + m_node_name = name; +} + +inline const std::string& k8s_pod_t::get_host_ip() const +{ + return m_host_ip; +} + +inline void k8s_pod_t::set_host_ip(const std::string& host_ip) +{ + m_host_ip = host_ip; +} + +inline const std::string& k8s_pod_t::get_internal_ip() const +{ + return m_internal_ip; +} + +inline void k8s_pod_t::set_internal_ip(const std::string& internal_ip) +{ + m_internal_ip = internal_ip; +} + + +// +// replicas +// + +inline void k8s_replicas_t::set_spec_replicas(int replicas) +{ + m_spec_replicas = replicas; +} + +inline int k8s_replicas_t::get_spec_replicas() const +{ + return m_spec_replicas; +} + +inline void k8s_replicas_t::set_stat_replicas(int replicas) +{ + m_stat_replicas = replicas; +} + +inline int k8s_replicas_t::get_stat_replicas() const +{ + return m_stat_replicas; +} + + +// +// replication controller +// + +inline void k8s_rc_t::set_spec_replicas(int replicas) +{ + m_replicas.set_spec_replicas(replicas); +} + +inline int k8s_rc_t::get_spec_replicas() const +{ + return m_replicas.get_spec_replicas(); +} + +inline void k8s_rc_t::set_stat_replicas(int replicas) +{ + m_replicas.set_stat_replicas(replicas); +} + +inline int k8s_rc_t::get_stat_replicas() const +{ + return m_replicas.get_stat_replicas(); +} + +inline void k8s_rc_t::set_replicas(const Json::Value& item, const std::string& replica_name) +{ + k8s_replicas_t::set_replicas(m_replicas, item); +} + +// +// service +// + +inline const std::string& k8s_service_t::get_cluster_ip() const +{ + return m_cluster_ip; +} + +inline void k8s_service_t::set_cluster_ip(const std::string& cluster_ip) +{ + m_cluster_ip = cluster_ip; +} + +inline const k8s_service_t::port_list& k8s_service_t::get_port_list() const +{ + return m_ports; +} + +inline void k8s_service_t::set_port_list(port_list&& ports) +{ + m_ports = std::move(ports); +} + +// +// deployment +// + +inline void k8s_deployment_t::set_spec_replicas(int replicas) +{ + m_replicas.set_spec_replicas(replicas); +} + +inline int k8s_deployment_t::get_spec_replicas() const +{ + return m_replicas.get_spec_replicas(); +} + +inline void k8s_deployment_t::set_stat_replicas(int replicas) +{ + m_replicas.set_stat_replicas(replicas); +} + +inline int k8s_deployment_t::get_stat_replicas() const +{ + return m_replicas.get_stat_replicas(); +} + +inline void k8s_deployment_t::set_replicas(const Json::Value& item) +{ + k8s_replicas_t::set_replicas(m_replicas, item); +} + +inline void k8s_deployment_t::set_replicas(int desired, int current) +{ + m_replicas.set_spec_replicas(desired); + m_replicas.set_stat_replicas(current); +} + + +// +// daemon set +// + +inline void k8s_daemonset_t::set_desired_scheduled(int scheduled) +{ + m_replicas.set_spec_replicas(scheduled); +} + +inline int k8s_daemonset_t::get_desired_scheduled() const +{ + return m_replicas.get_spec_replicas(); +} + +inline void k8s_daemonset_t::set_current_scheduled(int scheduled) +{ + m_replicas.set_stat_replicas(scheduled); +} + +inline int k8s_daemonset_t::get_current_scheduled() const +{ + return m_replicas.get_stat_replicas(); +} + +inline void k8s_daemonset_t::set_scheduled(const Json::Value& item) +{ + m_replicas.set_spec_replicas(k8s_replicas_t::get_count(item["status"], "desiredNumberScheduled")); + m_replicas.set_stat_replicas(k8s_replicas_t::get_count(item["status"], "currentNumberScheduled")); +} + +inline void k8s_daemonset_t::set_scheduled(int desired, int current) +{ + m_replicas.set_spec_replicas(desired); + m_replicas.set_stat_replicas(current); +} + +// +// event +// + +inline bool k8s_event_t::has_pending_events() const +{ + return m_postponed_events.size() != 0; +} +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_daemonset_handler.cpp b/userspace/libsinsp/k8s_daemonset_handler.cpp new file mode 100644 index 0000000000..c467ee1d6c --- /dev/null +++ b/userspace/libsinsp/k8s_daemonset_handler.cpp @@ -0,0 +1,162 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_daemonset_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_daemonset_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_daemonset_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " desiredScheduled: .status.desiredNumberScheduled," + " currentScheduled: .status.currentNumberScheduled," + " selector: .spec.selector.matchLabels," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_daemonset_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"DaemonSet\", " + " items:" + " [" + " .items[] | " + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " desiredScheduled: .status.desiredNumberScheduled," + " currentScheduled: .status.currentNumberScheduled," + " selector: .spec.selector.matchLabels," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_daemonset_handler::NULL_FILTER = + "{" + " type: \"NONEXISTENT\"," + " apiVersion: .apiVersion," + " kind: \"DaemonSet\", " + " items: [ null ]" + "}"; + +k8s_daemonset_handler::k8s_daemonset_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_daemonset_handler", true, +#ifdef HAS_CAPTURE + url, + "/apis/apps/v1/daemonsets", + STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + 100, // max msgs + &state) +{ +} + +k8s_daemonset_handler::~k8s_daemonset_handler() +{ +} + +bool k8s_daemonset_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_daemonset_t& ds = + m_state->get_component(m_state->get_daemonsets(), + data->m_name, data->m_uid, data->m_namespace); + k8s_pair_list entries = extract_object(json["labels"]); + if(entries.size() > 0) + { + ds.set_labels(std::move(entries)); + } + handle_selectors(ds, json["selector"]); + const Json::Value& desired = json["desiredScheduled"]; + const Json::Value& current = json["currentScheduled"]; + if(!desired.isNull() && desired.isConvertibleTo(Json::intValue) && + !current.isNull() && current.isConvertibleTo(Json::intValue)) + { + ds.set_scheduled(desired.asInt(), current.asInt()); + } + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_daemonsets(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: state is null."); + } + } + else + { + throw sinsp_exception("K8s node handler: data is null."); + } + return true; +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_daemonset_handler.h b/userspace/libsinsp/k8s_daemonset_handler.h new file mode 100644 index 0000000000..492cdf1ebc --- /dev/null +++ b/userspace/libsinsp/k8s_daemonset_handler.h @@ -0,0 +1,58 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_daemonset_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_state.h" + +class sinsp; + +class k8s_daemonset_handler : public k8s_handler +{ +public: + k8s_daemonset_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_daemonset_handler(); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + static std::string NULL_FILTER; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_deployment_handler.cpp b/userspace/libsinsp/k8s_deployment_handler.cpp new file mode 100644 index 0000000000..377958dc0b --- /dev/null +++ b/userspace/libsinsp/k8s_deployment_handler.cpp @@ -0,0 +1,161 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_deployment_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_deployment_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_deployment_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " specReplicas: .spec.replicas," + " statReplicas: .status.replicas," + " selector: .spec.selector.matchLabels," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_deployment_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"Deployment\", " + " items:" + " [" + " .items[] | " + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " specReplicas: .spec.replicas," + " statReplicas: .status.replicas," + " selector: .spec.selector.matchLabels," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_deployment_handler::NULL_FILTER = + "{" + " type: \"NONEXISTENT\"," + " apiVersion: .apiVersion," + " kind: \"Deployment\", " + " items: [ null ]" + "}"; + +k8s_deployment_handler::k8s_deployment_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_deployment_handler", true, +#ifdef HAS_CAPTURE + url, "/apis/apps/v1/deployments", + STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + 100, // max msgs + &state) +{ +} + +k8s_deployment_handler::~k8s_deployment_handler() +{ +} + +bool k8s_deployment_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_deployment_t& deployment = + m_state->get_component(m_state->get_deployments(), + data->m_name, data->m_uid, data->m_namespace); + k8s_pair_list entries = k8s_component::extract_object(json, "labels"); + if(entries.size() > 0) + { + deployment.set_labels(std::move(entries)); + } + handle_selectors(deployment, json["selector"]); + const Json::Value& spec = json["specReplicas"]; + const Json::Value& stat = json["statReplicas"]; + if(!spec.isNull() && spec.isConvertibleTo(Json::intValue) && + !stat.isNull() && stat.isConvertibleTo(Json::intValue)) + { + deployment.set_replicas(spec.asInt(), stat.asInt()); + } + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_deployments(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: state is null."); + } + } + else + { + throw sinsp_exception("K8s node handler: data is null."); + } + return true; +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_deployment_handler.h b/userspace/libsinsp/k8s_deployment_handler.h new file mode 100644 index 0000000000..206631c678 --- /dev/null +++ b/userspace/libsinsp/k8s_deployment_handler.h @@ -0,0 +1,58 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_deployment_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_state.h" + +class sinsp; + +class k8s_deployment_handler : public k8s_handler +{ +public: + k8s_deployment_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_deployment_handler(); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + static std::string NULL_FILTER; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_dispatcher.cpp b/userspace/libsinsp/k8s_dispatcher.cpp new file mode 100644 index 0000000000..2da8f5c8a9 --- /dev/null +++ b/userspace/libsinsp/k8s_dispatcher.cpp @@ -0,0 +1,787 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_dispatcher.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_dispatcher.h" +#include "k8s_service_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include +#include +#include +#include + + +k8s_dispatcher::k8s_dispatcher(k8s_component::type t, k8s_state_t& state, + filter_ptr_t event_filter): + m_type(t), m_state(state), m_event_filter(event_filter) +{ +} + +void k8s_dispatcher::enqueue(k8s_event_data&& event_data) +{ + assert(event_data.component() == m_type); + + std::string data = event_data.data(); + + if(m_messages.size() == 0) + { + m_messages.push_back(""); + } + + std::string* msg = &m_messages.back(); + std::string::size_type pos = msg->find_first_of('\n'); + + // previous msg full, this is a beginning of new message + if(pos != std::string::npos && pos == (msg->size() - 1)) + { + m_messages.push_back(""); + msg = &m_messages.back(); + } + + while ((pos = data.find_first_of('\n')) != std::string::npos) + { + msg->append(data.substr(0, pos + 1)); + if(data.length() > pos + 1) + { + data = data.substr(pos + 1); + m_messages.push_back(""); + msg = &m_messages.back(); + } + else + { + break; + } + }; + + if(data.size() > 0) + { + msg->append(data); + } + + dispatch(); // candidate for separate thread +} + +bool k8s_dispatcher::is_valid(const std::string& msg) +{ + // zero-length message is valid because that's how it starts its life. + // so, here we only check for messages that are single newline only + // or those that are longer than one character and contain multiple newlines. + + if((msg.size() == 1 && msg[0] == '\n') || + std::count(msg.begin(), msg.end(), '\n') > 1) + { + return false; + } + return true; +} + +bool k8s_dispatcher::is_ready(const std::string& msg) +{ + // absurd minimum ( "{}\n" ) but it's hard to tell + // what minimal size is, so there ... + if(msg.size() < 3) + { + return false; + } + return msg[msg.size() - 1] == '\n'; +} + +k8s_dispatcher::msg_data k8s_dispatcher::get_msg_data(Json::Value& root) +{ + msg_data data; + Json::Value evtype = root["type"]; + if(!evtype.isNull() && evtype.isString()) + { + const std::string& et = evtype.asString(); + if(!et.empty()) + { + if(et[0] == 'A') { data.m_reason = COMPONENT_ADDED; } + else if(et[0] == 'M') { data.m_reason = COMPONENT_MODIFIED; } + else if(et[0] == 'D') { data.m_reason = COMPONENT_DELETED; } + else if(et[0] == 'E') { data.m_reason = COMPONENT_ERROR; } + } + else + { + return msg_data(); + } + } + Json::Value object = root["object"]; + + // +++ for capture + Json::Value kind = object["kind"]; + if(!kind.isNull() && kind.isString() && root["kind"].isNull()) + { + root["kind"] = kind.asString(); + } + Json::Value api_version = object["apiVersion"]; + if(!api_version.isNull() && api_version.isString() && root["apiVersion"].isNull()) + { + root["apiVersion"] = api_version.asString(); + } + // --- for capture + + if(!object.isNull() && object.isObject()) + { + Json::Value meta = object["metadata"]; + if(!meta.isNull() && meta.isObject()) + { + Json::Value name = meta["name"]; + if(!name.isNull()) + { + data.m_name = name.asString(); + } + Json::Value uid = meta["uid"]; + if(!uid.isNull()) + { + data.m_uid = uid.asString(); + } + Json::Value nspace = meta["namespace"]; + if(!nspace.isNull()) + { + data.m_namespace = nspace.asString(); + } + } + } + return data; +} + +void k8s_dispatcher::log_error(const Json::Value& root, const std::string& comp) +{ + std::string unk_err = "Unknown."; + std::ostringstream os; + os << "K8S server reported " << comp << " error: "; + if(!root.isNull()) + { + Json::Value object = root["object"]; + if(!object.isNull()) + { + os << object.toStyledString(); + unk_err.clear(); + } + } + os << unk_err; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); +} + +void k8s_dispatcher::handle_node(const Json::Value& root, const msg_data& data) +{ + if(data.m_reason == COMPONENT_ADDED) + { + if(m_state.has(m_state.get_nodes(), data.m_uid)) + { + std::ostringstream os; + os << "ADDED message received for existing node [" << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + k8s_node_t& node = m_state.get_component(m_state.get_nodes(), data.m_name, data.m_uid); + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + const Json::Value& status = object["status"]; + if(!status.isNull()) + { + k8s_node_t::host_ip_list addresses = k8s_node_t::extract_addresses(status); + if(addresses.size() > 0) + { + node.set_host_ips(std::move(addresses)); + } + } + Json::Value metadata = object["metadata"]; + if(!metadata.isNull()) + { + k8s_pair_list entries = k8s_component::extract_object(metadata, "labels"); + if(entries.size() > 0) + { + node.set_labels(std::move(entries)); + } + } + } + } + else if(data.m_reason == COMPONENT_MODIFIED) + { + if(!m_state.has(m_state.get_nodes(), data.m_uid)) + { + std::ostringstream os; + os << "MODIFIED message received for non-existing node [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + return; + } + k8s_node_t& node = m_state.get_component(m_state.get_nodes(), data.m_name, data.m_uid); + Json::Value object = root["object"]; + if(!object.isNull()) + { + const Json::Value& status = object["status"]; + if(!status.isNull()) + { + k8s_node_t::host_ip_list addresses = k8s_node_t::extract_addresses(status); + if(addresses.size() > 0) + { + node.set_host_ips(std::move(addresses)); + } + } + const Json::Value& metadata = object["metadata"]; + if(!metadata.isNull()) + { + k8s_pair_list entries = k8s_component::extract_object(metadata, "labels"); + if(entries.size() > 0) + { + node.add_labels(std::move(entries)); + } + } + } + } + else if(data.m_reason == COMPONENT_DELETED) + { + if(!m_state.delete_component(m_state.get_nodes(), data.m_uid)) + { + g_logger.log(std::string("NODE not found: ") + data.m_name, sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_ERROR) + { + log_error(root, "NODE"); + } + else + { + g_logger.log(std::string("Unsupported K8S NODE event reason: ") + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + } +} + +void k8s_dispatcher::handle_namespace(const Json::Value& root, const msg_data& data) +{ + if(data.m_reason == COMPONENT_ADDED) + { + if(m_state.has(m_state.get_namespaces(), data.m_uid)) + { + std::ostringstream os; + os << "ADDED message received for existing namespace [" << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + k8s_ns_t& ns = m_state.get_component(m_state.get_namespaces(), data.m_name, data.m_uid); + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + const Json::Value& metadata = object["metadata"]; + if(!metadata.isNull()) + { + k8s_pair_list entries = k8s_component::extract_object(metadata, "labels"); + if(entries.size() > 0) + { + ns.set_labels(std::move(entries)); + } + } + } + } + else if(data.m_reason == COMPONENT_MODIFIED) + { + if(!m_state.has(m_state.get_namespaces(), data.m_uid)) + { + std::ostringstream os; + os << "MODIFIED message received for non-existing namespace [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + return; + } + k8s_ns_t& ns = m_state.get_component(m_state.get_namespaces(), data.m_name, data.m_uid); + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + const Json::Value& metadata = object["metadata"]; + if(!metadata.isNull()) + { + k8s_pair_list entries = k8s_component::extract_object(metadata, "labels"); + if(entries.size() > 0) + { + ns.add_labels(std::move(entries)); + } + } + } + } + else if(data.m_reason == COMPONENT_DELETED) + { + if(!m_state.delete_component(m_state.get_namespaces(), data.m_uid)) + { + g_logger.log(std::string("NAMESPACE not found: ") + data.m_name, sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_ERROR) + { + log_error(root, "NAMESPACE"); + } + else + { + g_logger.log(std::string("Unsupported K8S NAMESPACE event reason: ") + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + } +} + +bool k8s_dispatcher::handle_pod(const Json::Value& root, const msg_data& data) +{ + if(data.m_reason == COMPONENT_ADDED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(m_state.has(m_state.get_pods(), data.m_uid)) + { + std::ostringstream os; + os << "ADDED message received for existing pod [" << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + k8s_pod_t& pod = m_state.get_component(m_state.get_pods(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(pod, object["metadata"], "labels"); + m_state.update_pod(pod, object); + } + } + else if(data.m_reason == COMPONENT_MODIFIED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(!m_state.has(m_state.get_pods(), data.m_uid)) + { + std::ostringstream os; + os << "MODIFIED message received for non-existing pod [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + return false; + } + k8s_pod_t& pod = m_state.get_component(m_state.get_pods(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(pod, object["metadata"], "labels"); + m_state.update_pod(pod, object); + } + } + else if(data.m_reason == COMPONENT_DELETED) + { + k8s_pod_t* pod = m_state.get_component(m_state.get_pods(), data.m_uid); + if(pod) + { + if(!m_state.delete_component(m_state.get_pods(), data.m_uid)) + { + g_logger.log(std::string("Error deleting POD: ") + data.m_name, sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + g_logger.log(std::string("POD not found: ") + data.m_name, sinsp_logger::SEV_WARNING); + return false; + } + } + else if(data.m_reason == COMPONENT_ERROR) + { + log_error(root, "POD"); + } + else + { + g_logger.log(std::string("Unsupported K8S POD event reason: ") + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + return false; + } + return true; +} + +void k8s_dispatcher::handle_service(const Json::Value& root, const msg_data& data) +{ + if(data.m_reason == COMPONENT_ADDED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(m_state.has(m_state.get_services(), data.m_uid)) + { + std::ostringstream os; + os << "ADDED message received for existing service [" << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + k8s_service_t& service = m_state.get_component(m_state.get_services(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(service, object["metadata"], "labels"); + handle_selectors(service, object["spec"]); + k8s_service_handler::extract_services_data(object, service, m_state.get_pods()); + } + else + { + g_logger.log("K8s: object is null for service " + data.m_name + '[' + data.m_uid + ']', sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_MODIFIED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(!m_state.has(m_state.get_services(), data.m_uid)) + { + std::ostringstream os; + os << "MODIFIED message received for non-existing service [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + return; + } + k8s_service_t& service = m_state.get_component(m_state.get_services(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(service, object["metadata"], "labels"); + handle_selectors(service, object["spec"]); + k8s_service_handler::extract_services_data(object, service, m_state.get_pods()); + } + else + { + g_logger.log("K8s: object is null for service " + data.m_name + '[' + data.m_uid + ']', sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_DELETED) + { + if(!m_state.delete_component(m_state.get_services(), data.m_uid)) + { + g_logger.log(std::string("SERVICE not found: ") + data.m_name, sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_ERROR) + { + log_error(root, "SERVICE"); + } + else + { + g_logger.log(std::string("Unsupported K8S SERVICE event reason: ") + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + } +} + +void k8s_dispatcher::handle_deployment(const Json::Value& root, const msg_data& data) +{ + if(data.m_reason == COMPONENT_ADDED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(m_state.has(m_state.get_deployments(), data.m_uid)) + { + std::ostringstream os; + os << "ADDED message received for existing deployment [" << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + k8s_deployment_t& deployment = m_state.get_component(m_state.get_deployments(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(deployment, object["metadata"], "labels"); + handle_selectors(deployment, object["spec"]); + deployment.set_replicas(object); + } + else + { + g_logger.log("K8s: object is null for deployment "+ data.m_name + '[' + data.m_uid + ']', sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_MODIFIED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(!m_state.has(m_state.get_deployments(), data.m_uid)) + { + std::ostringstream os; + os << "MODIFIED message received for non-existing deployment [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + return; + } + k8s_deployment_t& deployment = + m_state.get_component(m_state.get_deployments(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(deployment, object["metadata"], "labels"); + handle_selectors(deployment, object["spec"]); + deployment.set_replicas(object); + } + else + { + g_logger.log("K8s: object is null for deployment " + data.m_name + '[' + data.m_uid + ']', sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_DELETED) + { + if(!m_state.delete_component(m_state.get_deployments(), data.m_uid)) + { + g_logger.log(std::string("DEPLOYMENT not found: ") + data.m_name, sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_ERROR) + { + log_error(root, "DEPLOYMENT"); + } + else + { + g_logger.log(std::string("Unsupported K8S DEPLOYMENT event reason: ") + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + } +} + +void k8s_dispatcher::handle_daemonset(const Json::Value& root, const msg_data& data) +{ + if(data.m_reason == COMPONENT_ADDED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(m_state.has(m_state.get_daemonsets(), data.m_uid)) + { + std::ostringstream os; + os << "ADDED message received for existing daemonset [" << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + k8s_daemonset_t& daemonset = m_state.get_component(m_state.get_daemonsets(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(daemonset, object["metadata"], "labels"); + handle_selectors(daemonset, object["spec"]); + daemonset.set_scheduled(object); + } + } + else if(data.m_reason == COMPONENT_MODIFIED) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + if(!m_state.has(m_state.get_daemonsets(), data.m_uid)) + { + std::ostringstream os; + os << "MODIFIED message received for non-existing daemonset [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + return; + } + k8s_daemonset_t& daemonset = m_state.get_component(m_state.get_daemonsets(), data.m_name, data.m_uid, data.m_namespace); + handle_labels(daemonset, object["metadata"], "labels"); + handle_selectors(daemonset, object["spec"]); + daemonset.set_scheduled(object); + } + } + else if(data.m_reason == COMPONENT_DELETED) + { + if(!m_state.delete_component(m_state.get_daemonsets(), data.m_uid)) + { + g_logger.log(std::string("DAEMONSET not found: ") + data.m_name, sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_ERROR) + { + log_error(root, "DAEMONSET"); + } + else + { + g_logger.log(std::string("Unsupported K8S DAEMONSET event reason: ") + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + } +} + +void k8s_dispatcher::handle_event(const Json::Value& root, const msg_data& data) +{ + if(m_event_filter) + { + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + g_logger.log("K8s EVENT: object found.", sinsp_logger::SEV_TRACE); + const Json::Value& involved_object = object["involvedObject"]; + if(!involved_object.isNull()) + { + bool is_aggregate = (get_json_string(object , "message").find("events with common reason combined") != std::string::npos); + time_t last_ts = get_epoch_utc_seconds(get_json_string(object , "lastTimestamp")); + time_t now_ts = get_epoch_utc_seconds_now(); + g_logger.log("K8s EVENT: lastTimestamp=" + std::to_string(last_ts) + ", now_ts=" + std::to_string(now_ts), sinsp_logger::SEV_TRACE); + if(((last_ts > 0) && (now_ts > 0)) && // we got good timestamps + !is_aggregate && // not an aggregated cached event + ((now_ts - last_ts) < 10)) // event not older than 10 seconds + { + const Json::Value& kind = involved_object["kind"]; + const Json::Value& event_reason = object["reason"]; + g_logger.log("K8s EVENT: involved object and event reason found:" + kind.asString() + '/' + event_reason.asString(), sinsp_logger::SEV_TRACE); + if(!kind.isNull() && kind.isConvertibleTo(Json::stringValue) && + !event_reason.isNull() && event_reason.isConvertibleTo(Json::stringValue)) + { + bool is_allowed = m_event_filter->allows_all(); + std::string type = kind.asString(); + if(!is_allowed && !type.empty()) + { + std::string reason = event_reason.asString(); + is_allowed = m_event_filter->allows_all(type); + if(!is_allowed && !reason.empty()) + { + is_allowed = m_event_filter->has(type, reason); + } + } + if(is_allowed) + { + g_logger.log("K8s EVENT: adding event.", sinsp_logger::SEV_TRACE); + k8s_event_t& evt = m_state.add_component(m_state.get_events(), + data.m_name, data.m_uid, data.m_namespace); + m_state.update_event(evt, object); + } + else + { + g_logger.log("K8s EVENT: filter does not allow {\"" + type + "\", \"{" + event_reason.asString() + "\"} }", sinsp_logger::SEV_TRACE); + g_logger.log(m_event_filter->to_string(), sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("K8s EVENT: event type or involvedObject kind not found.", sinsp_logger::SEV_ERROR); + g_logger.log(Json::FastWriter().write(root), sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("K8s EVENT: old event, ignoring: " + ", lastTimestamp=" + std::to_string(last_ts) + ", now_ts=" + std::to_string(now_ts), + sinsp_logger::SEV_DEBUG); + } + } + else + { + g_logger.log("K8s EVENT: involvedObject not found.", sinsp_logger::SEV_ERROR); + g_logger.log(Json::FastWriter().write(root), sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("K8s EVENT: object not found.", sinsp_logger::SEV_ERROR); + g_logger.log(Json::FastWriter().write(root), sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("K8s EVENT: filter NOT found.", sinsp_logger::SEV_DEBUG); + } +} + +void k8s_dispatcher::extract_data(Json::Value& root, bool enqueue) +{ + std::ostringstream os; + msg_data data = get_msg_data(root); + if(data.is_valid()) + { + std::ostringstream os; + os << '[' << to_reason_desc(data.m_reason) << ','; + switch (m_type) + { + case k8s_component::K8S_NODES: + os << "NODE,"; + handle_node(root, data); + break; + case k8s_component::K8S_NAMESPACES: + os << "NAMESPACE,"; + handle_namespace(root, data); + break; + case k8s_component::K8S_PODS: + os << "POD,"; + if(handle_pod(root, data)) { break; } + else { return; } + case k8s_component::K8S_REPLICATIONCONTROLLERS: + os << "REPLICATION_CONTROLLER,"; + handle_rc(root, data, m_state.get_rcs(), "replication controller"); + break; + case k8s_component::K8S_REPLICASETS: + os << "REPLICA_SET,"; + handle_rc(root, data, m_state.get_rss(), "replica set"); + break; + case k8s_component::K8S_SERVICES: + os << "SERVICE,"; + handle_service(root, data); + break; + case k8s_component::K8S_DAEMONSETS: + os << "DAEMON_SET,"; + handle_daemonset(root, data); + break; + case k8s_component::K8S_DEPLOYMENTS: + os << "DEPLOYMENT,"; + handle_deployment(root, data); + break; + case k8s_component::K8S_EVENTS: + os << "EVENT,"; + if(m_event_filter) + { + handle_event(root, data); + } + break; + default: + { + std::ostringstream eos; + eos << "Unknown component: " << static_cast(m_type); + throw sinsp_exception(os.str()); + } + } + os << data.m_name << ',' << data.m_uid << ',' << data.m_namespace << ']'; + g_logger.log(os.str(), sinsp_logger::SEV_INFO); + //g_logger.log(root.toStyledString(), sinsp_logger::SEV_DEBUG); + m_state.update_cache(m_type); +#ifdef HAS_CAPTURE + if(enqueue) + { + m_state.enqueue_capture_event(root); + } +#endif + } +} + +void k8s_dispatcher::extract_data(const std::string& json, bool enqueue) +{ + Json::Value root; + Json::Reader reader; + if(reader.parse(json, root, false)) + { + extract_data(root, enqueue); + } + else + { + g_logger.log("Bad JSON message received :[" + json + ']', sinsp_logger::SEV_ERROR); + } +} + +void k8s_dispatcher::dispatch() +{ + for (list::iterator it = m_messages.begin(); it != m_messages.end();) + { + if(is_ready(*it)) + { + extract_data(*it, true); + it = m_messages.erase(it); + } + else + { + ++it; + } + } +} + +std::string k8s_dispatcher::to_reason_desc(msg_reason reason) +{ + switch (reason) + { + case COMPONENT_ADDED: + return "ADDED"; + case COMPONENT_MODIFIED: + return "MODIFIED"; + case COMPONENT_DELETED: + return "DELETED"; + case COMPONENT_ERROR: + return "ERROR"; + case COMPONENT_UNKNOWN: + return "UNKNOWN"; + default: + return ""; + } +} + +k8s_dispatcher::msg_reason k8s_dispatcher::to_reason(const std::string& desc) +{ + if(desc == "ADDED") { return COMPONENT_ADDED; } + else if(desc == "MODIFIED") { return COMPONENT_MODIFIED; } + else if(desc == "DELETED") { return COMPONENT_DELETED; } + else if(desc == "ERROR") { return COMPONENT_ERROR; } + else if(desc == "UNKNOWN") { return COMPONENT_UNKNOWN; } + throw sinsp_exception(desc); +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_dispatcher.h b/userspace/libsinsp/k8s_dispatcher.h new file mode 100644 index 0000000000..ccaa28c2f8 --- /dev/null +++ b/userspace/libsinsp/k8s_dispatcher.h @@ -0,0 +1,231 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_dispatcher.h +// +// kubernetes REST API notification abstraction +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "k8s.h" +#include "k8s_component.h" +#include "k8s_state.h" +#include "k8s_event_data.h" +#include "json/json.h" +#include +#include + +class k8s_dispatcher +{ +public: + typedef user_event_filter_t::ptr_t filter_ptr_t; + + enum msg_reason + { + COMPONENT_ADDED, + COMPONENT_MODIFIED, + COMPONENT_DELETED, + COMPONENT_ERROR, + COMPONENT_UNKNOWN // only to mark bad event messages + }; + + struct msg_data + { + msg_reason m_reason = COMPONENT_UNKNOWN; + std::string m_name; + std::string m_uid; + std::string m_namespace; + + bool is_valid() const + { + return m_reason != COMPONENT_UNKNOWN; + } + }; + + k8s_dispatcher() = delete; + + k8s_dispatcher(k8s_component::type t, + k8s_state_t& state, + filter_ptr_t event_filter = nullptr); + + void enqueue(k8s_event_data&& data); + + void extract_data(const std::string& json, bool enqueue = false); + void extract_data(Json::Value& root, bool enqueue = false); + + // clears the content of labels and fills it with new values, if any + template + static void handle_labels(T& component, const Json::Value& metadata, const std::string& name) + { + if(!metadata.isNull()) + { + k8s_pair_list entries = k8s_component::extract_object(metadata, name); + component.set_labels(std::move(entries)); + } + else + { + g_logger.log("Null metadata object received", sinsp_logger::SEV_ERROR); + } + } + + // clears the content of selectors and fills it with new values, if any; + // the selector location depth in JSON tree is detected and handled accordingly + template + static void handle_selectors(T& component, const Json::Value& spec) + { + if(!spec.isNull()) + { + const Json::Value& selector = spec["selector"]; + if(!selector.isNull()) + { + const Json::Value& match_labels = selector["matchLabels"]; + k8s_pair_list selectors = match_labels.isNull() ? + k8s_component::extract_object(spec, "selector") : + k8s_component::extract_object(selector, "matchLabels"); + component.set_selectors(std::move(selectors)); + } + else + { + g_logger.log("K8s: Null selector object.", sinsp_logger::SEV_ERROR); + } + } + else + { + g_logger.log("K8s: Null spec object.", sinsp_logger::SEV_ERROR); + } + } + +private: + const std::string& next_msg(); + + msg_data get_msg_data(Json::Value& root); + + bool is_valid(const std::string& msg); + + bool is_ready(const std::string& msg); + + void remove(); + + void dispatch(); + + void handle_node(const Json::Value& root, const msg_data& data); + void handle_namespace(const Json::Value& root, const msg_data& data); + bool handle_pod(const Json::Value& root, const msg_data& data); + void handle_service(const Json::Value& root, const msg_data& data); + void handle_deployment(const Json::Value& root, const msg_data& data); + void handle_daemonset(const Json::Value& root, const msg_data& data); + void handle_event(const Json::Value& root, const msg_data& data); + + // handler for replication controllers and replica sets + template + void handle_rc(const Json::Value& root, const msg_data& data, T& cont, const std::string& comp_name) + { + typedef typename T::value_type comp_t; + + if(data.m_reason == COMPONENT_ADDED) + { + if(m_state.has(cont, data.m_uid)) + { + std::ostringstream os; + os << "ADDED message received for existing " << comp_name << '[' << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + comp_t& rc = m_state.get_component(cont, data.m_name, data.m_uid, data.m_namespace); + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + handle_labels(rc, object["metadata"], "labels"); + const Json::Value& spec = object["spec"]; + handle_selectors(rc, spec); + rc.set_replicas(object); + } + else + { + g_logger.log("K8s: object is null for " + comp_name + ' ' + data.m_name + '[' + data.m_uid + ']', + sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_MODIFIED) + { + if(!m_state.has(cont, data.m_uid)) + { + std::ostringstream os; + os << "MODIFIED message received for non-existing " << comp_name << '[' << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + return; + } + comp_t& rc = m_state.get_component(cont, data.m_name, data.m_uid, data.m_namespace); + const Json::Value& object = root["object"]; + if(!object.isNull()) + { + handle_labels(rc, object["metadata"], "labels"); + handle_selectors(rc, object["spec"]); + rc.set_replicas(object); + } + else + { + g_logger.log("K8s: object is null for " + comp_name + ' ' + data.m_name + '[' + data.m_uid + ']', + sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_DELETED) + { + if(!m_state.delete_component(cont, data.m_uid)) + { + g_logger.log("K8s: " + comp_name + " not found: " + data.m_name, sinsp_logger::SEV_ERROR); + } + } + else if(data.m_reason == COMPONENT_ERROR) + { + log_error(root, comp_name); + } + else + { + g_logger.log(std::string("Unsupported K8S " + comp_name + " event reason: ") + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + } + } + + void log_error(const Json::Value& root, const std::string& comp); + + static std::string to_reason_desc(msg_reason reason); + + static msg_reason to_reason(const std::string& desc); + + typedef std::deque list; + + k8s_component::type m_type; + list m_messages; + k8s_state_t& m_state; + filter_ptr_t m_event_filter; + std::string m_machine_id; +}; + + +inline const std::string& k8s_dispatcher::next_msg() +{ + return m_messages.front(); +} + +inline void k8s_dispatcher::remove() +{ + m_messages.pop_front(); +} + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_event_data.cpp b/userspace/libsinsp/k8s_event_data.cpp new file mode 100644 index 0000000000..93ba18fbc9 --- /dev/null +++ b/userspace/libsinsp/k8s_event_data.cpp @@ -0,0 +1,52 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_net.cpp +// + + +#include "k8s_event_data.h" + +k8s_event_data::k8s_event_data(k8s_component::type component, const char* data, int len): + m_component(component), + m_data(data, len) +{ +} + +k8s_event_data::k8s_event_data(const k8s_event_data& other): + m_component(other.m_component), + m_data(other.m_data) +{ +} + +k8s_event_data::k8s_event_data(k8s_event_data&& other): + m_component(std::move(other.m_component)), + m_data(std::move(other.m_data)) +{ +} + +k8s_event_data& k8s_event_data::operator=(k8s_event_data&& other) +{ + if(this != &other) + { + m_component = other.m_component; + m_data = other.m_data; + } + return *this; +} diff --git a/userspace/libsinsp/k8s_event_data.h b/userspace/libsinsp/k8s_event_data.h new file mode 100644 index 0000000000..8e4796c4e8 --- /dev/null +++ b/userspace/libsinsp/k8s_event_data.h @@ -0,0 +1,61 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_event_data.h +// +// connects and gets the data from k8s_net REST API interface +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "k8s_component.h" + + +class k8s_event_data +{ +public: + k8s_event_data() = delete; + + k8s_event_data(k8s_component::type component, const char* data, int len); + + k8s_event_data(const k8s_event_data& other); + + k8s_event_data(k8s_event_data&& other); + + k8s_event_data& operator=(k8s_event_data&& other); + + k8s_component::type component() const; + + std::string data() const; + +private: + k8s_component::type m_component; + std::string m_data; +}; + +inline k8s_component::type k8s_event_data::component() const +{ + return m_component; +} + +inline std::string k8s_event_data::data() const +{ + return m_data; +} +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_event_handler.cpp b/userspace/libsinsp/k8s_event_handler.cpp new file mode 100644 index 0000000000..d352b1276f --- /dev/null +++ b/userspace/libsinsp/k8s_event_handler.cpp @@ -0,0 +1,327 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_event_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_event_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_event_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " lastTimestamp: .lastTimestamp," + " eventTime: .eventTime," + " reason: .reason," + " message: .message," + " involvedObject: .involvedObject" + " }" + " ]" + "}"; + +std::string k8s_event_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"Event\"," + " items:" + " [" + " .items[] |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " lastTimestamp: .lastTimestamp," + " eventTime: .eventTime," + " reason: .reason," + " message: .message," + " involvedObject: .involvedObject" + " }" + " ]" + "}"; + +k8s_event_handler::k8s_event_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ,filter_ptr_t event_filter): + k8s_handler("k8s_event_handler", true, +#ifdef HAS_CAPTURE + url, "/api/v1/events", + STATE_FILTER, EVENT_FILTER, "", collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + ~0, &state), + m_event_filter(event_filter) +{ +} + +k8s_event_handler::~k8s_event_handler() +{ +} + +bool k8s_event_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(m_event_filter) + { + if(m_state) + { + if(data) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + g_logger.log("K8s EVENT: handling event.", sinsp_logger::SEV_TRACE); + const Json::Value& involved_object = json["involvedObject"]; + if(!involved_object.isNull()) + { + bool is_aggregate = (get_json_string(json , "message").find("events with common reason combined") != std::string::npos); + time_t last_ts = 0; + time_t now_ts = get_epoch_utc_seconds_now(); + // So we first are going to check for "eventTime" + // If that is empty, we will check for "lastTimestamp" + // If that is also empty, use current timestamp and log it. + // This change is necessitated because in v1beta1/events, "EventTime" is + // the main field that holds timestamp and `lastTimestamp` is deprecated. + // This change is addressed towards that. + std::string evtTime = get_json_string(json, "eventTime"); + std::string ts = get_json_string(json , "lastTimestamp"); + if(!evtTime.empty()) + { + last_ts = get_epoch_utc_seconds(evtTime); + } + else if(!ts.empty()) + { + last_ts = get_epoch_utc_seconds(ts); + } + else + { + // Ideally we should NEVER hit this case. But log it if we do, so we know. + g_logger.log("K8s EVENT: both eventTime and lastTimestamp are null, using current timestamp. Event Json : " + Json::FastWriter().write(json) , sinsp_logger::SEV_INFO); + last_ts = now_ts; + } + g_logger.log("K8s EVENT: lastTimestamp=" + std::to_string(last_ts) + ", now_ts=" + std::to_string(now_ts), + sinsp_logger::SEV_TRACE); + if(((last_ts > 0) && (now_ts > 0)) && // we got good timestamps + !is_aggregate && // not an aggregated cached event + ((now_ts - last_ts) < 10)) // event not older than 10 seconds + { + const Json::Value& kind = involved_object["kind"]; + const Json::Value& event_reason = json["reason"]; + g_logger.log("K8s EVENT: involved object and event reason found:" + kind.asString() + '/' + event_reason.asString(), + sinsp_logger::SEV_TRACE); + if(!kind.isNull() && kind.isConvertibleTo(Json::stringValue) && + !event_reason.isNull() && event_reason.isConvertibleTo(Json::stringValue)) + { + bool is_allowed = m_event_filter->allows_all(); + std::string type = kind.asString(); + if(!is_allowed && !type.empty()) + { + std::string reason = event_reason.asString(); + is_allowed = m_event_filter->allows_all(type); + if(!is_allowed && !reason.empty()) + { + is_allowed = m_event_filter->has(type, reason); + } + } + if(is_allowed) + { + k8s_events& evts = m_state->get_events(); + if(evts.size() < sinsp_user_event::max_events_per_cycle()) + { + k8s_event_t& evt = m_state->add_component(evts, + data->m_name, data->m_uid, data->m_namespace); + m_state->update_event(evt, json); + m_event_limit_exceeded = false; + if(g_logger.get_severity() >= sinsp_logger::SEV_DEBUG) + { + g_logger.log("K8s EVENT: added event [" + data->m_name + "]. " + "Queued events count=" + std::to_string(evts.size()), sinsp_logger::SEV_DEBUG); + } + } + else if(!m_event_limit_exceeded) // only get in here once per cycle, to send event overflow warning + { + sinsp_user_event::emit_event_overflow("Kubernetes", get_machine_id()); + m_event_limit_exceeded = true; + return false; + } + else // event limit exceeded and overflow logged, nothing to do + { + return false; + } + } + else // event not allowed by filter, ignore + { + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("K8s EVENT: filter does not allow {\"" + type + "\", \"{" + event_reason.asString() + "\"} }", + sinsp_logger::SEV_TRACE); + g_logger.log(m_event_filter->to_string(), sinsp_logger::SEV_TRACE); + } + m_event_ignored = true; + return false; + } + } + else + { + g_logger.log("K8s EVENT: event type or involvedObject kind not found.", sinsp_logger::SEV_ERROR); + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log(Json::FastWriter().write(json), sinsp_logger::SEV_TRACE); + } + return false; + } + } + else // old event, ignore + { + g_logger.log("K8s EVENT: old event, ignoring: " + ", lastTimestamp=" + std::to_string(last_ts) + ", now_ts=" + std::to_string(now_ts), + sinsp_logger::SEV_DEBUG); + m_event_ignored = true; + return false; + } + } + else + { + g_logger.log("K8s EVENT: involvedObject not found.", sinsp_logger::SEV_ERROR); + g_logger.log(Json::FastWriter().write(json), sinsp_logger::SEV_TRACE); + return false; + } + } + else // not ADDED or MODIFIED event, ignore + { + m_event_ignored = true; + return false; + } + } + else + { + g_logger.log("K8s EVENT: msg data is null.", sinsp_logger::SEV_ERROR); + g_logger.log(Json::FastWriter().write(json), sinsp_logger::SEV_TRACE); + return false; + } + } + else + { + g_logger.log("K8s EVENT: state is null.", sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + g_logger.log("K8s EVENT: no filter, K8s events disabled.", sinsp_logger::SEV_TRACE); + return false; + } + return true; +} + +void k8s_event_handler::handle_json(Json::Value&& root) +{ + /*if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log(json_as_string(root), sinsp_logger::SEV_TRACE); + }*/ + + if(!m_state) + { + throw sinsp_exception("k8s_handler (" + get_id() + "), state is null for " + get_url() + ")."); + } + const Json::Value& type = root["type"]; + if(!type.isNull()) + { + if(type.isConvertibleTo(Json::stringValue)) + { + const Json::Value& kind = root["kind"]; + if(!kind.isNull()) + { + if(kind.isConvertibleTo(Json::stringValue)) + { + std::string t = type.asString(); + std::string k = kind.asString(); + for(const Json::Value& item : root["items"]) + { + msg_data data = get_msg_data(t, k, item); + std::string reason_type = data.get_reason_desc(); + if(data.m_reason != k8s_component::COMPONENT_ADDED && + data.m_reason != k8s_component::COMPONENT_MODIFIED && + data.m_reason != k8s_component::COMPONENT_DELETED && + data.m_reason != k8s_component::COMPONENT_NONEXISTENT && + data.m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + continue; + } + /*if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("K8s handling event:\n" + json_as_string(item), sinsp_logger::SEV_TRACE); + }*/ + if(handle_component(item, &data)) + { + std::ostringstream os; + os << "K8s [" + reason_type + ", " << data.m_kind << + ", " << data.m_name << ", " << data.m_uid << "]"; + g_logger.log(os.str(), sinsp_logger::SEV_INFO); + } + else if(!m_event_ignored) + { + g_logger.log("K8s: error occurred while handling " + reason_type + + " event for " + data.m_kind + ' ' + data.m_name + " [" + + data.m_uid + ']', sinsp_logger::SEV_ERROR); + } + m_event_ignored = false; + } // end for items + } + } + } + else + { + g_logger.log(std::string("K8S event type is not string."), sinsp_logger::SEV_ERROR); + } + } + else + { + g_logger.log(std::string("K8S event type is null."), sinsp_logger::SEV_ERROR); + } +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_event_handler.h b/userspace/libsinsp/k8s_event_handler.h new file mode 100644 index 0000000000..81b60d8bff --- /dev/null +++ b/userspace/libsinsp/k8s_event_handler.h @@ -0,0 +1,64 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_event_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_event_data.h" + +class sinsp; + +class k8s_event_handler : public k8s_handler +{ +public: + typedef user_event_filter_t::ptr_t filter_ptr_t; + + k8s_event_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ,filter_ptr_t event_filter = 0); + + ~k8s_event_handler(); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + + bool handle_component(const Json::Value& json, const msg_data* data = 0); + void handle_json(Json::Value&& root); + + filter_ptr_t m_event_filter; + bool m_event_ignored = false; + bool m_event_limit_exceeded = false; +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_handler.cpp b/userspace/libsinsp/k8s_handler.cpp new file mode 100644 index 0000000000..7a7f93142b --- /dev/null +++ b/userspace/libsinsp/k8s_handler.cpp @@ -0,0 +1,807 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// to match regular K8s API message format, +// error is wrapped into a single-entry array +std::string k8s_handler::ERROR_FILTER = + "{" + " type: \"ERROR\"," + " apiVersion: .apiVersion," + " kind: .kind," + " items:" + " [" + " . |" + " {" + " metadata: .metadata," + " status: .status," + " message: .message," + " reason: .reason," + " details: .details," + " code: .code" + " }" + " ]" + "}"; + +k8s_handler::k8s_handler(const std::string& id, + bool is_captured, +#ifdef HAS_CAPTURE + std::string url, + const std::string& path, + const std::string& state_filter, + const std::string& event_filter, + const std::string& null_filter, + collector_ptr_t collector, + const std::string& http_version, + int timeout_ms, + ssl_ptr_t ssl, + bt_ptr_t bt, + bool watch, + bool connect, + ptr_t dependency_handler, + bool blocking_socket, +#endif // HAS_CAPTURE + unsigned max_messages, + k8s_state_t* state): m_state(state), + m_id(id + "_state"), +#ifdef HAS_CAPTURE + m_collector(collector), + m_path(path + ((path.find('?') == std::string::npos) ? "?pretty=false" : "&pretty=false")), + m_state_filter(state_filter), + m_event_filter(event_filter), + m_null_filter(null_filter), + m_filter(&m_state_filter), + m_timeout_ms(timeout_ms), + m_url(url), + m_http_version(http_version), + m_ssl(ssl), + m_bt(bt), + m_watch(watch), + m_connect(connect), + m_dependency_handler(dependency_handler), + m_blocking_socket(blocking_socket), +#endif // HAS_CAPTURE + m_max_messages(max_messages), + m_is_captured(is_captured) +{ +#ifdef HAS_CAPTURE + g_logger.log("Creating K8s " + name() + " (" + m_id + ") " + "handler object for [" + uri(m_url).to_string(false) + m_path + ']', + sinsp_logger::SEV_DEBUG); + if(m_connect) + { + g_logger.log(std::string("K8s (" + m_id + ") creating handler for " + + uri(m_url).to_string(false) + m_path), sinsp_logger::SEV_DEBUG); + m_handler = std::make_shared(*this, m_id, m_url, m_path, m_http_version, + m_timeout_ms, m_ssl, m_bt, !m_blocking_socket, m_blocking_socket); + m_handler->set_json_callback(&k8s_handler::set_event_json); + + // filter order is important; there are four kinds of filters: + // 1.a state filter (filters init state JSONs) + // 1.b event filter (filters watch JSONs) + // 2. null filter (filters init state JSONs when there are no K8s entities) + // 3. error filter (filters errors) + // and they must come in the following order: + // 1.a OR 1.b, then 2. (for state only), then 3. (always) + // mixing the order will produce erroneous results because + // null and error filters will successfully parse regular messages + // and prevent proper processing + m_handler->add_json_filter(*m_filter); + if(!m_null_filter.empty()) + { + m_handler->add_json_filter(m_null_filter); + } + m_handler->add_json_filter(ERROR_FILTER); + + m_handler->close_on_chunked_end(false); + this->connect(); + } +#endif // HAS_CAPTURE +} + +k8s_handler::~k8s_handler() +{ +} + +void k8s_handler::make_http() +{ +#ifdef HAS_CAPTURE + if(m_connect && m_collector) + { + if(!m_handler) + { + g_logger.log("K8s (" + m_id + ") creating handler for " + + uri(m_url).to_string(false) + m_path, sinsp_logger::SEV_INFO); + m_handler = std::make_shared(*this, m_id, m_url, m_path, m_http_version, + m_timeout_ms, m_ssl, m_bt, true, m_blocking_socket); + m_handler->set_json_callback(&k8s_handler::set_event_json); + } + else if(m_collector->has(m_handler)) + { + m_collector->remove(m_handler); + } + + // adjust filters for event handler + // (see comment in ctor for explanation) + // - null filter is only needed for init state + m_handler->remove_json_filter(m_null_filter); + // - state filter will be replaced with the event filter below + m_handler->remove_json_filter(m_state_filter); + m_filter = &m_event_filter; + // - error filter should be there, but just in case let's double-check + if(!m_handler->has_json_filter(ERROR_FILTER)) + { + g_logger.log("K8s (" + m_id + ") error filter was not present in state handler, adding it for events.", + sinsp_logger::SEV_WARNING); + m_handler->add_json_filter(ERROR_FILTER); + } + // - good event filter must always be before error event filter + m_handler->add_json_filter(*m_filter, ERROR_FILTER); + // end filter adjustment + + m_handler->set_path(m_path); + m_handler->set_id(m_id); + m_collector->set_steady_state(true); + m_watching = true; + m_blocking_socket = false; + m_handler->close_on_chunked_end(false); + + m_req_sent = false; + m_resp_recvd = false; + connect(); + m_handler->set_socket_option(SOCK_NONBLOCK); + } +#endif // HAS_CAPTURE +} + +void k8s_handler::check_enabled() +{ +#ifdef HAS_CAPTURE + if(!m_handler->is_enabled()) + { + g_logger.log("k8s_handler (" + m_id + + ") check_enabled() enabling socket in collector", sinsp_logger::SEV_TRACE); + m_handler->enable(); + } + else + { + g_logger.log("k8s_handler (" + m_id + + ") check_enabled() socket in collector is enabled, " + "checking collector status.", sinsp_logger::SEV_TRACE); + check_collector_status(); + } +#endif // HAS_CAPTURE +} + +bool k8s_handler::connect() +{ +#ifdef HAS_CAPTURE + if(m_collector && m_handler) + { + if(!m_collector->has(m_handler)) + { + g_logger.log(std::string("k8s_handler (" + m_id + + ") k8s_handler::connect() adding handler to collector"), sinsp_logger::SEV_TRACE); + m_collector->add(m_handler); + return false; + } + if(m_handler->is_connecting()) + { + g_logger.log(std::string("k8s_handler (" + m_id + + "), k8s_handler::connect() connecting to " + m_handler->get_url().to_string(false)), sinsp_logger::SEV_TRACE); + return false; + } + if(m_handler->is_connected()) + { + g_logger.log("k8s_handler (" + m_id + + ") k8s_handler::connect() socket is connected.", sinsp_logger::SEV_TRACE); + check_enabled(); + return true; + } + } + else if (m_collector && !m_url.empty()) + { + g_logger.log(std::string("k8s_handler (" + m_id + + ") k8s_handler::connect(), http is null, (re)creating ... "), + sinsp_logger::SEV_WARNING); + make_http(); + } +#endif // HAS_CAPTURE + return false; +} + +void k8s_handler::send_data_request() +{ +#ifdef HAS_CAPTURE + if(m_handler) + { + if(!m_req_sent) + { + if(m_handler->is_connected()) + { + g_logger.log("k8s_handler (" + m_id + ") sending request to " + + m_handler->get_url().to_string(false) + m_path, + sinsp_logger::SEV_DEBUG); + m_handler->send_request(); + m_req_sent = true; + } + else if(m_handler->is_connecting()) + { + g_logger.log("k8s_handler (" + m_id + ") is connecting to " + + m_handler->get_url().to_string(false), + sinsp_logger::SEV_DEBUG); + } + } + } + else + { + throw sinsp_exception("k8s_handler (" + m_id + ") HTTP client (" + uri(m_url).to_string(false) + ") is null."); + } +#endif // HAS_CAPTURE +} + +void k8s_handler::receive_response() +{ +#ifdef HAS_CAPTURE + if(m_handler) + { + if(m_req_sent) + { + if(!m_watching) + { + if(m_handler->get_all_data()) + { + m_data_received = true; + } + else + { + throw sinsp_exception("K8s k8s_handler::receive_response(): no data received."); + } + } + else + { + throw sinsp_exception("K8s k8s_handler::receive_response(): invalid call (in watch mode)."); + } + } + else + { + throw sinsp_exception("K8s k8s_handler::receive_response(): invalid call (request not sent)."); + } + } + else + { + throw sinsp_exception("K8s k8s_handler::receive_response(): handler is null."); + } +#endif // HAS_CAPTURE +} + +bool k8s_handler::is_alive() const +{ +#ifdef HAS_CAPTURE + if(m_handler && !m_handler->is_connecting() && !m_handler->is_connected()) + { + g_logger.log("k8s_handler (" + m_id + ") connection (" + m_handler->get_url().to_string(false) + ") loss.", + sinsp_logger::SEV_WARNING); + return false; + } +#endif // HAS_CAPTURE + return true; +} + +void k8s_handler::check_collector_status() +{ +#ifdef HAS_CAPTURE + if(m_collector) + { + if(!m_collector->has(m_handler)) + { + m_handler.reset(); + make_http(); + } + } + else + { + throw sinsp_exception("k8s_handler (" + m_id + ") collector is null."); + } +#endif // HAS_CAPTURE +} + +void k8s_handler::check_state() +{ +#ifdef HAS_CAPTURE + if(m_collector && m_handler) + { + if(m_resp_recvd && m_watch && !m_watching) + { + g_logger.log("k8s_handler (" + m_id + ") switching to watch connection for " + + uri(m_url).to_string(false) + m_path, + sinsp_logger::SEV_DEBUG); + std::string::size_type pos = m_id.find("_state"); + if(pos != std::string::npos) + { + m_id = m_id.substr(0, pos).append("_event"); + } + pos = m_path.find("/watch"); + if(pos == std::string::npos) + { + pos = m_path.rfind('/'); + if(pos != std::string::npos) + { + m_path.insert(pos, "/watch"); + } + else + { + throw sinsp_exception("k8s_handler (" + m_id + "), invalid URL path: " + m_path); + } + } + m_handler->set_socket_option(SOCK_NONBLOCK); + make_http(); + } + if(m_watching && m_id.find("_state") == std::string::npos && m_handler->wants_send()) + { + m_req_sent = false; + m_resp_recvd = false; + } + } +#endif // HAS_CAPTURE +} + +bool k8s_handler::connection_error() const +{ +#ifdef HAS_CAPTURE + if(m_handler) + { + return m_handler->connection_error(); + } +#endif // HAS_CAPTURE + return false; +} + +void k8s_handler::collect_data() +{ +#ifdef HAS_CAPTURE + if(m_collector && m_handler) + { + process_events(); // there may be leftovers from state connection closed by collector + check_state(); // switch to events, if needed + g_logger.log("k8s_handler (" + m_id + ")::collect_data(), checking connection to " + uri(m_url).to_string(false), sinsp_logger::SEV_DEBUG); + if(m_handler->is_connecting()) + { + g_logger.log("k8s_handler (" + m_id + ")::collect_data(), connecting to " + uri(m_url).to_string(false), sinsp_logger::SEV_DEBUG); + return; + } + else if(m_handler->is_connected()) + { + if(!m_connect_logged) + { + g_logger.log("k8s_handler (" + m_id + ")::collect_data(), connected to " + uri(m_url).to_string(false) + m_path, sinsp_logger::SEV_DEBUG); + m_connect_logged = true; + } + check_enabled(); + if(!m_req_sent) + { + g_logger.log("k8s_handler (" + m_id + ")::collect_data() [" + uri(m_url).to_string(false) + "], requesting data " + "from " + m_path + "... m_blocking_socket=" + std::to_string(m_blocking_socket) + ", m_watching=" + std::to_string(m_watching), + sinsp_logger::SEV_DEBUG); + send_data_request(); + if(m_blocking_socket && !m_watching) + { + receive_response(); + process_events(); + return; + } + } + if(m_collector->subscription_count()) + { + g_logger.log("k8s_handler (" + m_id + ")::collect_data() [" + uri(m_url).to_string(false) + "], getting data " + "from " + m_path + "...", sinsp_logger::SEV_DEBUG); + m_collector->get_data(); + g_logger.log("k8s_handler (" + m_id + ")::collect_data(), " + std::to_string(m_events.size()) + + " events from " + uri(m_url).to_string(false) + m_path, sinsp_logger::SEV_DEBUG); + if(m_events.size()) + { + g_logger.log("k8s_handler (" + m_id + ")::collect_data(), data from " + uri(m_url).to_string(false) + m_path + + ", event count=" + std::to_string(m_events.size()), sinsp_logger::SEV_DEBUG); + process_events(); + check_state(); + } + else + { + g_logger.log("k8s_handler (" + m_id + ") collect_data(), no data from " + uri(m_url).to_string(false) + m_path, + sinsp_logger::SEV_DEBUG); + } + } + else + { + g_logger.log("k8s_handler (" + m_id + ") collect_data(), no subscriptions to " + uri(m_url).to_string(false) + m_path, + sinsp_logger::SEV_DEBUG); + } + return; + } + else + { + connect(); + } + m_req_sent = false; + } + else + { + g_logger.log("k8s_handler (" + m_id + "), http interface not (yet?) created for " + uri(m_url).to_string(false) + ").", + sinsp_logger::SEV_TRACE); + } +#endif // HAS_CAPTURE +} + +k8s_handler::msg_data k8s_handler::get_msg_data(const std::string& type, const std::string& kind, const Json::Value& json) +{ + msg_data data; + if(!type.empty()) + { + if(type[0] == 'A') { data.m_reason = k8s_component::COMPONENT_ADDED; } + else if(type[0] == 'M') { data.m_reason = k8s_component::COMPONENT_MODIFIED; } + else if(type[0] == 'D') { data.m_reason = k8s_component::COMPONENT_DELETED; } + else if(type[0] == 'N') { data.m_reason = k8s_component::COMPONENT_NONEXISTENT; } + else if(type[0] == 'E') { data.m_reason = k8s_component::COMPONENT_ERROR; } + } + else + { + return data; + } + + data.m_kind = kind; + + Json::Value name = json["name"]; + if(!name.isNull()) + { + data.m_name = name.asString(); + } + Json::Value uid = json["uid"]; + if(!uid.isNull()) + { + data.m_uid = uid.asString(); + } + Json::Value nspace = json["namespace"]; + if(!nspace.isNull()) + { + data.m_namespace = nspace.asString(); + } + + return data; +} + +void k8s_handler::handle_json(Json::Value&& root) +{ + /*if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log(json_as_string(root), sinsp_logger::SEV_TRACE); + }*/ + + if(!m_state) + { +#ifdef HAS_CAPTURE + throw sinsp_exception("k8s_handler (" + m_id + "), state is null for " + uri(m_url).to_string(false) + ")."); +#else + throw sinsp_exception("k8s_handler (" + m_id + "), state is null."); +#endif // HAS_CAPTURE + } + const Json::Value& type = root["type"]; + if(!type.isNull()) + { + if(type.isConvertibleTo(Json::stringValue)) + { + const Json::Value& kind = root["kind"]; + if(!kind.isNull()) + { + if(kind.isConvertibleTo(Json::stringValue)) + { + std::string t = type.asString(); + std::string k = kind.asString(); + for(const Json::Value& item : root["items"]) + { + msg_data data = get_msg_data(t, k, item); + /* + uncomment to test proper error handling + + //if(name() == "replicasets") // vary name to verify (non)critical component error handling + if(name() == "pods") + { + std::string j = "{" + " \"metadata\": \"{}\"," + " \"status\": \"Failure\"," + " \"message\": \"the server could not find the requested resource\"," + " \"reason\": \"NotFound\"," + " \"details\": \"{}\"," + " \"code\": 404" + "}"; + Json::Value i; + Json::Reader().parse(j, i); + data.m_reason = k8s_component::COMPONENT_ERROR; + handle_error(data, i); + continue; + } + */ + std::string reason_type = data.get_reason_desc(); + if(data.m_reason == k8s_component::COMPONENT_ADDED) + { + if(m_state->has(data.m_uid)) + { + std::ostringstream os; + os << "K8s " + reason_type << " message received by " << m_id << +#ifdef HAS_CAPTURE + " [" << uri(m_url).to_string(false) << "]" +#endif // HAS_CAPTURE + "for existing " << data.m_kind << " [" << data.m_uid << "], updating only."; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + } + else if(data.m_reason == k8s_component::COMPONENT_MODIFIED) + { + if(!m_state->has(data.m_uid)) + { + std::ostringstream os; + os << "K8s " << reason_type << " message received by " << m_id << +#ifdef HAS_CAPTURE + " [" << uri(m_url).to_string(false) << "]" +#endif // HAS_CAPTURE + " for non-existing " << data.m_kind << " [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_WARNING); + continue; + } + } + else if(data.m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->has(data.m_uid)) + { + std::ostringstream os; + os << "K8s " + reason_type + " message received by " << m_id << +#ifdef HAS_CAPTURE + " [" << uri(m_url).to_string(false) << "]" +#endif // HAS_CAPTURE + " for non-existing " << data.m_kind << " [" << data.m_uid << "], giving up."; + g_logger.log(os.str(), sinsp_logger::SEV_WARNING); + continue; + } + } + else if(data.m_reason == k8s_component::COMPONENT_ERROR) + { + handle_error(data, item); + continue; + } + else + { + if(data.m_reason == k8s_component::COMPONENT_NONEXISTENT) + { + g_logger.log(std::string("Non-existent K8S component (" + name() + "), reason: ") + + std::to_string(data.m_reason), sinsp_logger::SEV_DEBUG); + } + else + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data.m_reason), sinsp_logger::SEV_ERROR); + } + continue; + } + /*if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("K8s handling item:\n" + json_as_string(item), sinsp_logger::SEV_TRACE); + }*/ + if(handle_component(item, &data)) + { + std::ostringstream os; + os << "K8s [" + reason_type + ", " << data.m_kind << + ", " << data.m_name << ", " << data.m_uid << "]"; + g_logger.log(os.str(), sinsp_logger::SEV_INFO); + m_state->update_cache(k8s_component::get_type(name())); + } + else + { + g_logger.log("K8s: error occurred while handling " + reason_type + + " event for " + data.m_kind + ' ' + data.m_name + " [" + + data.m_uid + ']', sinsp_logger::SEV_ERROR); + } + } // end for items + } + } + } + else + { + g_logger.log(std::string("K8S event type is not string."), sinsp_logger::SEV_ERROR); + } + } + else + { + g_logger.log(std::string("K8S event type is null."), sinsp_logger::SEV_ERROR); + } +} + +#ifdef HAS_CAPTURE + +bool k8s_handler::is_ip_address(const std::string& addr) +{ + struct sockaddr_in serv_addr = {0}; + return inet_aton(addr.c_str(), &serv_addr.sin_addr); +} + +k8s_handler::ip_addr_list_t k8s_handler::hostname_to_ip(const std::string& hostname) +{ + ip_addr_list_t ip_addrs; + struct addrinfo *servinfo = 0; + + struct addrinfo hints = {0}; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + if((getaddrinfo(hostname.c_str(), NULL, &hints, &servinfo)) != 0) + { + g_logger.log("Can't determine IP address for hostname: " + hostname, sinsp_logger::SEV_WARNING); + return ip_addrs; + } + + for(struct addrinfo* p = servinfo; p != NULL; p = p->ai_next) + { + struct sockaddr_in* h = (struct sockaddr_in*)p->ai_addr; + ip_addrs.emplace(inet_ntoa(h->sin_addr)); + } + + freeaddrinfo(servinfo); + return ip_addrs; +} + +#endif // HAS_CAPTURE + +bool k8s_handler::dependency_ready() const +{ +#ifdef HAS_CAPTURE + g_logger.log("k8s_handler (" + m_id + ") dependency " + "(" + m_dependency_handler->get_id() + ") ready: " + + std::to_string(m_dependency_handler->is_state_built()), + sinsp_logger::SEV_TRACE); + return m_dependency_handler->is_state_built(); +#else + return true; +#endif // HAS_CAPTURE +} + +void k8s_handler::process_events() +{ + if(dependency_ready()) + { + unsigned counter = 0; + for(auto evt = m_events.begin(); evt != m_events.end();) + { + m_state_processing_started = true; + if(++counter >= get_max_messages()) { break; } + if(*evt && !(*evt)->isNull()) + { + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("k8s_handler (" + m_id + ") processing event data:\n" + json_as_string(*(*evt)), + sinsp_logger::SEV_TRACE); + } +#ifdef HAS_CAPTURE + if(m_is_captured) + { + m_state->enqueue_capture_event(**evt); + } +#endif // HAS_CAPTURE + handle_json(std::move(**evt)); + } + else + { + g_logger.log("k8s_handler (" + m_id + ") error " + +#ifdef HAS_CAPTURE + "(" + uri(m_url).to_string(false) + ") " + +#endif // HAS_CAPTURE + (!(*evt) ? "data is null." : ((*evt)->isNull() ? "JSON is null." : "Unknown")), + sinsp_logger::SEV_ERROR); + } + evt = m_events.erase(evt); + } + if(!m_state_built && m_state_processing_started && !m_events.size()) { m_state_built = true; } + } +} + +void k8s_handler::set_event_json(json_ptr_t json, const std::string&) +{ + g_logger.log("k8s_handler adding event, (" + m_id + ") has " + std::to_string(m_events.size()) +#ifdef HAS_CAPTURE + + " events from " + uri(m_url).to_string(false) +#endif // HAS_CAPTURE + , sinsp_logger::SEV_TRACE); + // empty JSON is fine here; if there are no entities, state and first watch will pass nothing in here + // null is checked when processing + m_events.emplace_back(json); + g_logger.log("k8s_handler added event, (" + m_id + ") has " + std::to_string(m_events.size()) +#ifdef HAS_CAPTURE + + " events from " + uri(m_url).to_string(false) +#endif // HAS_CAPTURE + , sinsp_logger::SEV_TRACE); +#ifdef HAS_CAPTURE + if(!m_resp_recvd) { m_resp_recvd = true; } +#endif // HAS_CAPTURE +} + +k8s_pair_list k8s_handler::extract_object(const Json::Value& object) +{ + k8s_pair_list entry_list; + if(!object.isNull() && object.isObject()) + { + Json::Value::Members members = object.getMemberNames(); + for (auto& member : members) + { + const Json::Value& val = object[member]; + if(!val.isNull() && val.isString()) + { + entry_list.emplace_back(k8s_pair_t(member, val.asString())); + } + } + } + return entry_list; +} + +std::string k8s_handler::name() const +{ + std::string n; +#ifdef HAS_CAPTURE + std::string::size_type slash_pos = m_path.rfind('/'); + std::string::size_type qm_pos = m_path.rfind('?'); + std::string::size_type length = + (qm_pos == std::string::npos) ? + std::string::npos : qm_pos - slash_pos - 1; + + if((slash_pos != std::string::npos) && (++slash_pos < m_path.size())) + { + n = m_path.substr(slash_pos, length); + } +#endif // HAS_CAPTURE + return n; +} + +void k8s_handler::handle_error(const msg_data& data, const Json::Value& root, bool log) +{ + if(log) + { + log_error(data, root); + } +} + +void k8s_handler::log_error(const msg_data& data, const Json::Value& json) +{ +#ifdef HAS_CAPTURE + std::string unk_err = "Unknown."; + std::ostringstream os;; + os << "K8S server reported " << name() << " error for [" + uri(m_url).to_string(false) + m_path + "]: "; + if(!json.isNull()) + { + os << std::endl << json.toStyledString(); + unk_err.clear(); + m_error.reset(new k8s_api_error(data, json)); + } + os << unk_err; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); +#endif // HAS_CAPTURE +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_handler.h b/userspace/libsinsp/k8s_handler.h new file mode 100644 index 0000000000..7d8c17bcc2 --- /dev/null +++ b/userspace/libsinsp/k8s_handler.h @@ -0,0 +1,317 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "socket_collector.h" +#include "k8s_state.h" +#include "k8s_api_error.h" +#include + +class sinsp; + +class k8s_handler +{ +public: + typedef k8s_component::msg_reason msg_reason; + typedef k8s_component::msg_data msg_data; + + typedef std::shared_ptr ptr_t; + typedef std::vector uri_list_t; + typedef std::shared_ptr json_ptr_t; + typedef std::shared_ptr api_error_ptr; +#ifdef HAS_CAPTURE + typedef sinsp_ssl::ptr_t ssl_ptr_t; + typedef sinsp_bearer_token::ptr_t bt_ptr_t; + typedef socket_data_handler handler_t; + typedef handler_t::ptr_t handler_ptr_t; + typedef socket_collector collector_t; + typedef std::shared_ptr> collector_ptr_t; +#endif // HAS_CAPTURE + + static const int default_timeout_ms = 1000L; + + k8s_handler(const std::string& id, + bool is_captured, +#ifdef HAS_CAPTURE + std::string url, + const std::string& path, + const std::string& state_filter, + const std::string& event_filter, + const std::string& null_filter, + collector_ptr_t collector = nullptr, + const std::string& http_version = "1.1", + int timeout_ms = default_timeout_ms, + ssl_ptr_t ssl = nullptr, + bt_ptr_t bt = nullptr, + bool watch = true, + bool connect = true, + ptr_t dependency_handler = nullptr, + bool blocking_socket = false, +#endif // HAS_CAPTURE + unsigned max_messages = ~0, + k8s_state_t* state = nullptr); + + virtual ~k8s_handler(); + + bool connection_error() const; + bool is_alive() const; + bool ready() const; + void set_event_json(json_ptr_t json, const std::string&); + const std::string& get_id() const; +#ifdef HAS_CAPTURE + handler_ptr_t handler(); +#endif // HAS_CAPTURE + std::string get_url() const; + + void collect_data(); + void set_machine_id(const std::string& machine_id); + const std::string& get_machine_id() const; + + bool is_state_built() const; + std::string name() const; + api_error_ptr error() const; + virtual void handle_json(Json::Value&& root); + + unsigned get_max_messages() const; + void set_max_messages(unsigned max_msgs); + +protected: + typedef std::unordered_set ip_addr_list_t; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0) = 0; + msg_data get_msg_data(const std::string& evt, const std::string& type, const Json::Value& root); +#ifdef HAS_CAPTURE + static bool is_ip_address(const std::string& addr); +#endif // HAS_CAPTURE + + k8s_pair_list extract_object(const Json::Value& object); + + template + void handle_selectors(T& component, const Json::Value& selector) + { + if(!selector.isNull()) + { + component.set_selectors(extract_object(selector)); + } + else + { + g_logger.log("K8s Replication Controller: Null selector object.", sinsp_logger::SEV_ERROR); + } + } + + void log_event(const msg_data& data); + void handle_error(const msg_data& data, const Json::Value& root, bool log = true); + void log_error(const msg_data& data, const Json::Value& root); + void log_not_found(const msg_data& data) const; + + k8s_state_t* m_state = nullptr; + bool m_state_built = false; + bool m_data_received = false; + static std::string ERROR_FILTER; + +private: + typedef void (k8s_handler::*callback_func_t)(json_ptr_t, const std::string&); + + typedef std::vector event_list_t; + +#ifdef HAS_CAPTURE + static ip_addr_list_t hostname_to_ip(const std::string& hostname); +#endif // HAS_CAPTURE + + bool connect(); + void make_http(); + void send_data_request(); + void receive_response(); + void check_enabled(); + void check_state(); + void check_collector_status(); + void process_events(); + + const std::string& translate_name(const std::string& event_name); + bool dependency_ready() const; + + std::string m_id; + std::string m_machine_id; +#ifdef HAS_CAPTURE + collector_ptr_t m_collector; + handler_ptr_t m_handler; + std::string m_path; + std::string m_state_filter; + std::string m_event_filter; + std::string m_null_filter; + std::string* m_filter; + long m_timeout_ms; + std::string m_url; + bool m_req_sent = false; + bool m_resp_recvd = false; + json_query m_jq; + std::string m_http_version; + ssl_ptr_t m_ssl; + bt_ptr_t m_bt; + + // some handlers only fetch state and die by design (eg. api or extensions handlers + // have no need to continuously watch for updates) + // this flag indicates whether handler should continue to watch after receiving + // the initial state + bool m_watch; + bool m_watching = false; // indication of being in watch mode + + // flag indicating whether to connect to K8s API server (no connection needed when + // replaying capture) + bool m_connect; + + // k8s_handler on which this handler depends; the dependency handler must not be null and + // it must have its state fully built before this handler can begin building its own state + ptr_t m_dependency_handler; + + bool m_blocking_socket = false; + +#endif // HAS_CAPTURE + + // limits the number of messages handled in single cycle + unsigned m_max_messages = ~0; + bool m_state_processing_started = false; + + event_list_t m_events; + + // error indicating something went wrong with the K8s component handled by this handler + // this error is later examined by k8s::check_components() and if it is + // HTTP status > 400, one of the following actions is taken: + // - if component is critical for consistent k8s state (eg. namespace, node, pod), + // exception is thrown and, consequently, the whole k8s framework will be destroyed + // - if component is not critical (eg. extensions like daemonset or deployment), + // error is logged and handler is destroyed, but the k8s framework continues to + // exist without it, only receiving data for existing components + api_error_ptr m_error; + + // this capture flag does not indicate whether we are in global capture mode, + // it is only an indication of whether this handler data should be captured + // at all (eg. there is no need to capture api or extensions detection data) + // + // global capture flag is checked in the k8s state call + bool m_is_captured = false; + + bool m_connect_logged = false; +}; + +inline unsigned k8s_handler::get_max_messages() const +{ + return m_max_messages; +} + +inline void k8s_handler::set_max_messages(unsigned max_msgs) +{ + m_max_messages = max_msgs; +} + +#ifdef HAS_CAPTURE +inline k8s_handler::handler_ptr_t k8s_handler::handler() +{ + return m_handler; +} +#endif // HAS_CAPTURE + +inline std::string k8s_handler::get_url() const +{ +#ifdef HAS_CAPTURE + return m_url; +#else + return ""; +#endif +} + +inline const std::string& k8s_handler::get_id() const +{ + return m_id; +} + +inline void k8s_handler::set_machine_id(const std::string& machine_id) +{ + m_machine_id = machine_id; +} + +inline const std::string& k8s_handler::get_machine_id() const +{ + return m_machine_id; +} + +inline bool k8s_handler::ready() const +{ + return m_data_received; +} + +inline bool k8s_handler::is_state_built() const +{ + return m_state_built; +} + +inline void k8s_handler::log_event(const msg_data& data) +{ + g_logger.log("K8s " + data.get_reason_desc() + ' ' + + data.m_kind + ' ' + + data.m_name + " [" + data.m_uid + "]", + sinsp_logger::SEV_DEBUG); +} + +inline void k8s_handler::log_not_found(const msg_data& data) const +{ + g_logger.log("K8s " + name() + " not found [" + data.m_uid + "]: " + data.m_name, + sinsp_logger::SEV_ERROR); +} + +inline k8s_handler::api_error_ptr k8s_handler::error() const +{ + return m_error; +} + +// This dummy class serves only as a dependency stand-in for handlers +// which have no dependencies (eg. nodes handler, which is first populated +// into the state and has no dependency; or special-purpose handlers, +// such as delegator, api handler etc), but the logic requires a non-null +// pointer to handler to determine whether dependency is ready; to avoid +// special-casing eg. nodes handler all over the place, we use this dummy +// liar, always returning true for its state being built, as the dependency +class k8s_dummy_handler : public k8s_handler +{ +public: + k8s_dummy_handler(): k8s_handler("k8s_dummy_handler", false, +#ifdef HAS_CAPTURE + "", "", "", "", "", nullptr, + "", 0, nullptr, nullptr, + false, false, nullptr, false, +#endif // HAS_CAPTURE + ~0, nullptr) + { + m_state_built = true; + } + +private: + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0) + { + return false; + }; +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_namespace_handler.cpp b/userspace/libsinsp/k8s_namespace_handler.cpp new file mode 100644 index 0000000000..4b7282796f --- /dev/null +++ b/userspace/libsinsp/k8s_namespace_handler.cpp @@ -0,0 +1,135 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_namespace_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_namespace_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_namespace_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [ .object |" + " {" + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_namespace_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"Namespace\"," + " items:" + " [" + " .items[] |" + " {" + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +k8s_namespace_handler::k8s_namespace_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_namespace_handler", true, +#ifdef HAS_CAPTURE + url, "/api/v1/namespaces", + STATE_FILTER, EVENT_FILTER, "", collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + ~0, &state) +{ +} + +k8s_namespace_handler::~k8s_namespace_handler() +{ +} + +bool k8s_namespace_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_ns_t& ns = + m_state->get_component(m_state->get_namespaces(), + data->m_name, data->m_uid); + k8s_pair_list entries = k8s_component::extract_object(json, "labels"); + if(entries.size() > 0) + { + ns.set_labels(std::move(entries)); + } + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_namespaces(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: state is null."); + } + } + else + { + throw sinsp_exception("K8s namespace handler: data is null."); + } + return true; +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_namespace_handler.h b/userspace/libsinsp/k8s_namespace_handler.h new file mode 100644 index 0000000000..1e977f5c3f --- /dev/null +++ b/userspace/libsinsp/k8s_namespace_handler.h @@ -0,0 +1,56 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_namespace_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" + +class sinsp; + +class k8s_namespace_handler : public k8s_handler +{ +public: + k8s_namespace_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_namespace_handler(); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + + bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_net.cpp b/userspace/libsinsp/k8s_net.cpp new file mode 100644 index 0000000000..622ac09a23 --- /dev/null +++ b/userspace/libsinsp/k8s_net.cpp @@ -0,0 +1,281 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_net.cpp +// +#ifndef CYGWING_AGENT + +#ifdef HAS_CAPTURE + +#include "k8s_net.h" +#include "k8s_component.h" +#include "k8s_node_handler.h" +#include "k8s_namespace_handler.h" +#include "k8s_pod_handler.h" +#include "k8s_replicationcontroller_handler.h" +#include "k8s_replicaset_handler.h" +#include "k8s_service_handler.h" +#include "k8s_daemonset_handler.h" +#include "k8s_deployment_handler.h" +#include "k8s_event_handler.h" +#include "k8s.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include +#include + + +k8s_net::k8s_net(k8s& kube, k8s_state_t& state, const std::string& uri, + ssl_ptr_t ssl, + bt_ptr_t bt, + filter_ptr_t event_filter, + bool blocking_sockets) : m_state(state), + m_collector(std::make_shared()), + m_uri(uri), + m_ssl(ssl), + m_bt(bt), + m_stopped(true), + m_blocking_sockets(blocking_sockets), + m_event_filter(event_filter) +{ +} + +k8s_net::~k8s_net() +{ + cleanup(); +} + +void k8s_net::cleanup() +{ + stop_watching(); + m_handlers.clear(); +} + +void k8s_net::watch() +{ + for(auto it = m_handlers.cbegin(); it != m_handlers.cend();) + { + k8s_component::type comp_type = it->first; + if(it->second) + { + if(it->second->connection_error()) + { + if(k8s_component::is_critical(comp_type)) + { + throw sinsp_exception("K8s: " + k8s_component::get_name(comp_type) + " connection error."); + } + else + { + g_logger.log("K8s: " + k8s_component::get_name(comp_type) + " connection error, removing component.", + sinsp_logger::SEV_WARNING); + if(m_collector->has(it->second->handler())) + { + m_collector->remove(it->second->handler()); + } + m_handlers.erase(it++); + g_logger.log("K8s: " + k8s_component::get_name(comp_type) + " removed from watched endpoints.", + sinsp_logger::SEV_INFO); + } + } + else + { + it->second->collect_data(); + ++it; + } + } + else + { + g_logger.log("K8s: " + k8s_component::get_name(comp_type) + " handler is null.", + sinsp_logger::SEV_WARNING); + ++it; + } + } +} + +void k8s_net::stop_watching() +{ + if(!m_stopped) + { + m_stopped = true; + m_collector->remove_all(); + } +} + +k8s_net::handler_ptr_t k8s_net::get_dependency_handler(const handler_map_t& handlers, const k8s_component::type& component) +{ + switch(component) + { + case k8s_component::K8S_NODES: + return std::make_shared(); + case k8s_component::K8S_NAMESPACES: + return get_handler(handlers, k8s_component::K8S_NODES); + case k8s_component::K8S_PODS: + return get_handler(handlers, k8s_component::K8S_NAMESPACES); + case k8s_component::K8S_REPLICATIONCONTROLLERS: + return get_handler(handlers, k8s_component::K8S_PODS); + case k8s_component::K8S_SERVICES: + return get_handler(handlers, k8s_component::K8S_PODS); + case k8s_component::K8S_REPLICASETS: + return get_handler(handlers, k8s_component::K8S_PODS); + case k8s_component::K8S_DAEMONSETS: + return get_handler(handlers, k8s_component::K8S_PODS); + case k8s_component::K8S_DEPLOYMENTS: + return get_handler(handlers, k8s_component::K8S_PODS); + case k8s_component::K8S_EVENTS: + return std::make_shared(); + case k8s_component::K8S_COMPONENT_COUNT: + default: break; + } + throw sinsp_exception(std::string("Invalid K8s component type:") + std::to_string(component)); +} + +k8s_net::handler_ptr_t k8s_net::get_dependency_handler(const handler_map_t& handlers, const k8s_component::type_map::value_type& component) +{ + return get_dependency_handler(handlers, component.first); +} + +bool k8s_net::has_dependency(const k8s_component::type_map::value_type& component) +{ + auto it = get_dependency_handler(m_handlers, component); + return (it && it->is_state_built()); +} + +k8s_net::handler_ptr_t k8s_net::make_handler(k8s_state_t& state, const k8s_component::type component, bool connect, + handler_ptr_t dep, collector_ptr_t collector, const std::string& urlstr, + ssl_ptr_t ssl, bt_ptr_t bt, bool blocking, filter_ptr_t event_filter) +{ + switch(component) + { + case k8s_component::K8S_NODES: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_NAMESPACES: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_PODS: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_REPLICATIONCONTROLLERS: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_REPLICASETS: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_SERVICES: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_DAEMONSETS: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_DEPLOYMENTS: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking); + case k8s_component::K8S_EVENTS: + return std::make_shared(state, dep, collector, urlstr, "1.1", ssl, bt, connect, blocking, event_filter); + case k8s_component::K8S_COMPONENT_COUNT: + default: + return nullptr; + } + + return nullptr; +} + +void k8s_net::add_handler(const k8s_component::type_map::value_type& component) +{ + if(!has_handler(component)) + { + handler_ptr_t handler = + make_handler(m_state, component.first, true, get_dependency_handler(m_handlers, component), + m_collector, m_uri.to_string(), m_ssl, m_bt, m_blocking_sockets, m_event_filter); + if(handler) + { + if(!m_machine_id.empty()) + { + handler->set_machine_id(m_machine_id); + } + else if(handler->name() == "events") + { + g_logger.log("K8s machine ID (MAC) is empty - scope may not be available for " + handler->name(), + sinsp_logger::SEV_WARNING); + } + m_handlers[component.first] = handler; + } + else + { + std::ostringstream os; + os << "K8s: invalid component type encountered while creating handler: " << + component.second << " (" << + std::to_string(component.first) << ')'; + if(k8s_component::is_critical(component)) + { + throw sinsp_exception(os.str()); + } + else + { + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + } + } + g_logger.log("K8s: created " + k8s_component::get_name(component) + " handler.", sinsp_logger::SEV_INFO); + } + else + { + g_logger.log("K8s: component " + k8s_component::get_name(component) + " already exists.", + sinsp_logger::SEV_TRACE); + } +} +#else // !HAS_CAPTURE + +#include "k8s_component.h" +#include "k8s_node_handler.h" +#include "k8s_namespace_handler.h" +#include "k8s_pod_handler.h" +#include "k8s_replicationcontroller_handler.h" +#include "k8s_replicaset_handler.h" +#include "k8s_service_handler.h" +#include "k8s_daemonset_handler.h" +#include "k8s_deployment_handler.h" +#include "k8s_event_handler.h" + +namespace k8s_net +{ + k8s_handler::ptr_t make_handler(k8s_state_t& state, const k8s_component::type component, bool /*connect*/) + { + switch(component) + { + case k8s_component::K8S_NODES: + return std::make_shared(state); + case k8s_component::K8S_NAMESPACES: + return std::make_shared(state); + case k8s_component::K8S_PODS: + return std::make_shared(state); + case k8s_component::K8S_REPLICATIONCONTROLLERS: + return std::make_shared(state); + case k8s_component::K8S_REPLICASETS: + return std::make_shared(state); + case k8s_component::K8S_SERVICES: + return std::make_shared(state); + case k8s_component::K8S_DAEMONSETS: + return std::make_shared(state); + case k8s_component::K8S_DEPLOYMENTS: + return std::make_shared(state); + case k8s_component::K8S_EVENTS: + return std::make_shared(state); + case k8s_component::K8S_COMPONENT_COUNT: + default: + return nullptr; + } + return nullptr; + } +} + +#endif // HAS_CAPTURE +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_net.h b/userspace/libsinsp/k8s_net.h new file mode 100644 index 0000000000..ef5f677638 --- /dev/null +++ b/userspace/libsinsp/k8s_net.h @@ -0,0 +1,185 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_net.h +// +// connects and gets the data from k8s_net REST API interface +// +#ifndef MINIMAL_BUILD +#pragma once + +#ifdef HAS_CAPTURE + +#include "k8s_component.h" +#include "k8s_event_data.h" +#include "k8s_handler.h" +#include "k8s_event_data.h" +#include "uri.h" +#include "sinsp_curl.h" +#include +#include + +class k8s; + +class k8s_net +{ +public: + typedef sinsp_ssl::ptr_t ssl_ptr_t; + typedef sinsp_bearer_token::ptr_t bt_ptr_t; + typedef k8s_component::ext_list_ptr_t ext_list_ptr_t; + typedef user_event_filter_t::ptr_t filter_ptr_t; + typedef k8s_handler::ptr_t handler_ptr_t; + typedef k8s_handler::collector_t collector_t; + typedef k8s_handler::collector_ptr_t collector_ptr_t; + + k8s_net(k8s& kube, k8s_state_t& state, const std::string& uri = "http://localhost:80", + ssl_ptr_t ssl = nullptr, + bt_ptr_t bt = nullptr, + filter_ptr_t event_filter = nullptr, + bool blocking_sockets = false); + + ~k8s_net(); + + static handler_ptr_t make_handler(k8s_state_t& state, const k8s_component::type component, bool connect = true, + handler_ptr_t dep = std::make_shared(), + collector_ptr_t collector = nullptr, const std::string& urlstr = "", + ssl_ptr_t ssl = nullptr, bt_ptr_t bt = nullptr, bool blocking = false, + filter_ptr_t event_filter = nullptr); + void add_handler(const k8s_component::type_map::value_type& component); + bool has_handler(const k8s_component::type_map::value_type& component); + bool has_dependency(const k8s_component::type_map::value_type& component); + + bool is_state_built(const k8s_component::type_map::value_type& component); + + void watch(); + void stop_watching(); + bool is_healthy() const; + + void set_machine_id(const std::string& machine_id); + const std::string& get_machine_id() const; + + typedef k8s_handler::handler_t handler_t; + typedef std::map handler_map_t; + + const handler_map_t& handlers() const; + static handler_ptr_t get_handler(const handler_map_t& handlers, k8s_component::type component); + static handler_ptr_t get_handler(const handler_map_t& handlers, const k8s_component::type_map::value_type& component); + static handler_ptr_t get_dependency_handler(const handler_map_t& handlers, const k8s_component::type_map::value_type& component); + static handler_ptr_t get_dependency_handler(const handler_map_t& handlers, const k8s_component::type& component); + +private: + void init(); + bool is_secure(); + void cleanup(); + + k8s_state_t& m_state; + collector_ptr_t m_collector; + uri m_uri; + ssl_ptr_t m_ssl; + bt_ptr_t m_bt; + bool m_stopped; + handler_map_t m_handlers; + bool m_blocking_sockets = false; + filter_ptr_t m_event_filter; + std::string m_machine_id; +}; + +inline bool k8s_net::is_secure() +{ + return m_uri.get_scheme() == "https"; +} + +inline bool k8s_net::is_healthy() const +{ + if(m_collector) + { + if (m_collector->get_steady_state()) + { + return m_collector->subscription_count() == static_cast(m_handlers.size()); + } + else + { + return true; + } + } + else + { + return false; + } +} + +inline bool k8s_net::has_handler(const k8s_component::type_map::value_type& component) +{ + auto it = m_handlers.find(component.first); + return (it != m_handlers.end()) && it->second; +} + +inline k8s_net::handler_ptr_t k8s_net::get_handler(const handler_map_t& handlers, k8s_component::type component) +{ + auto it = handlers.find(component); + if(it != handlers.end()) + { + return it->second; + } + return nullptr; +} + +inline k8s_net::handler_ptr_t k8s_net::get_handler(const handler_map_t& handlers, const k8s_component::type_map::value_type& component) +{ + return get_handler(handlers, component.first); +} + +inline bool k8s_net::is_state_built(const k8s_component::type_map::value_type& component) +{ + const auto& it = m_handlers.find(component.first); + if(it != m_handlers.end()) + { + return it->second && it->second->is_state_built(); + } + return false; +} + +inline void k8s_net::set_machine_id(const std::string& machine_id) +{ + m_machine_id = machine_id; +} + +inline const std::string& k8s_net::get_machine_id() const +{ + return m_machine_id; +} + +inline const k8s_net::handler_map_t& k8s_net::handlers() const +{ + return m_handlers; +} + +#else // !HAS_CAPTURE + +#include "k8s_component.h" +#include "k8s_handler.h" + +namespace k8s_net +{ + k8s_handler::ptr_t make_handler(k8s_state_t& state, const k8s_component::type component, bool /*connect*/); +} + +#endif // HAS_CAPTURE + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_node_handler.cpp b/userspace/libsinsp/k8s_node_handler.cpp new file mode 100644 index 0000000000..1785937259 --- /dev/null +++ b/userspace/libsinsp/k8s_node_handler.cpp @@ -0,0 +1,149 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_node_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_node_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_node_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " labels: .metadata.labels," + " addresses: [.status.addresses[].address] | unique" + " }" + " ]" + "}"; + +std::string k8s_node_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"Node\", " + " items:" + " [" + " .items[] | " + " {" + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " labels: .metadata.labels," + " addresses: [.status.addresses[].address] | unique" + " }" + " ]" + "}"; + +k8s_node_handler::k8s_node_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_node_handler", true, +#ifdef HAS_CAPTURE + url, "/api/v1/nodes", + STATE_FILTER, EVENT_FILTER, "", collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + ~0, &state) +{ +} + +k8s_node_handler::~k8s_node_handler() +{ +} + +bool k8s_node_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_node_t& node = + m_state->get_component(m_state->get_nodes(), + data->m_name, data->m_uid); + k8s_node_t::host_ip_list addresses; + k8s_component::extract_string_array(json["addresses"], addresses); + if(addresses.size() > 0) + { + node.set_host_ips(std::move(addresses)); + } + else + { + g_logger.log("K8s Node handler: Can not obtain IP address(es) for node" + data->m_name + + '[' + data->m_uid + ']', sinsp_logger::SEV_ERROR); + } + k8s_pair_list entries = k8s_component::extract_object(json, "labels"); + if(entries.size() > 0) + { + node.set_labels(std::move(entries)); + } + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_nodes(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: state is null."); + } + } + else + { + throw sinsp_exception("K8s node handler: data is null."); + } + return true; +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_node_handler.h b/userspace/libsinsp/k8s_node_handler.h new file mode 100644 index 0000000000..35c4e380a6 --- /dev/null +++ b/userspace/libsinsp/k8s_node_handler.h @@ -0,0 +1,57 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_node_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_state.h" + +class sinsp; + +class k8s_node_handler : public k8s_handler +{ +public: + k8s_node_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_node_handler(); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_pod_handler.cpp b/userspace/libsinsp/k8s_pod_handler.cpp new file mode 100644 index 0000000000..1161950bdd --- /dev/null +++ b/userspace/libsinsp/k8s_pod_handler.cpp @@ -0,0 +1,289 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_pod_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_pod_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_pod_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " nodeName: .spec.nodeName," + " hostIP: .status.hostIP," + " podIP: .status.podIP," + " phase: .status.phase," + " containers: .spec.containers," + " containerStatuses: .status.containerStatuses," + " initContainerStatuses: .status.initContainerStatuses," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_pod_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"Pod\", " + " items:" + " [" + " .items[] | " + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " nodeName: .spec.nodeName," + " hostIP: .status.hostIP," + " podIP: .status.podIP," + " phase: .status.phase," + " containers: .spec.containers," + " containerStatuses: .status.containerStatuses," + " initContainerStatuses: .status.initContainerStatuses," + " labels: .metadata.labels," + " }" + " ]" + "}"; + +k8s_pod_handler::k8s_pod_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_pod_handler", true, +#ifdef HAS_CAPTURE + url, "/api/v1/pods?fieldSelector=status.phase!=Failed,status.phase!=Unknown,status.phase!=Succeeded", + STATE_FILTER, EVENT_FILTER, "", collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + ~0, &state) +{ +} + +k8s_pod_handler::~k8s_pod_handler() +{ +} + +std::vector k8s_pod_handler::extract_pod_container_ids(const Json::Value& item) +{ + std::vector container_list; + + const Json::Value& containers = item["containerStatuses"]; + if(!containers.isNull()) + { + for (auto& container : containers) + { + const Json::Value& container_id = container["containerID"]; + if(!container_id.isNull()) + { + container_list.emplace_back(container_id.asString()); + } + } + } + + const Json::Value& initContainers = item["initContainerStatuses"]; + if(!initContainers.isNull()) + { + for (auto& container : initContainers) + { + const Json::Value& container_id = container["containerID"]; + if(!container_id.isNull()) + { + container_list.emplace_back(container_id.asString()); + } + } + } + + return container_list; +} + +k8s_container::list k8s_pod_handler::extract_pod_containers(const Json::Value& item) +{ + k8s_container::list ext_containers; + // Not looking for init containers here because this appears + // to only be used by the k8s_service_handler for named port + // resolution. Init containers can't have service ports. + const Json::Value& containers = item["containers"]; + if(!containers.isNull()) + { + for (auto& container : containers) + { + std::string cont_name; + const Json::Value& name = container["name"]; + if(!name.isNull()) { cont_name = name.asString(); } + else { return ext_containers; } + k8s_container::port_list cont_ports; + const Json::Value& ports = container["ports"]; + for(const auto& port : ports) + { + k8s_container::port cont_port; + const Json::Value& name = port["name"]; + if(!name.isNull()) + { + cont_port.set_name(name.asString()); + } + const Json::Value& cport = port["containerPort"]; + if(!cport.isNull()) + { + cont_port.set_port(cport.asUInt()); + } + else + { + g_logger.log("Port not found, setting value to 0", sinsp_logger::SEV_WARNING); + cont_port.set_port(0); + } + const Json::Value& protocol = port["protocol"]; + if(!protocol.isNull()) + { + cont_port.set_protocol(protocol.asString()); + } + else + { + std::string port_name = name.isNull() ? "[NO NAME]" : name.asString(); + g_logger.log("Protocol not found for port: " + port_name, sinsp_logger::SEV_WARNING); + } + cont_ports.push_back(cont_port); + } + ext_containers.emplace_back(k8s_container(cont_name, cont_ports)); + } + } + return ext_containers; +} + +void k8s_pod_handler::extract_pod_data(const Json::Value& item, k8s_pod_t& pod) +{ + const Json::Value& node_name = item["nodeName"]; + if(!node_name.isNull()) + { + std::string nn = node_name.asString(); + if(!nn.empty()) + { + pod.set_node_name(nn); + } + } + const Json::Value& host_ip = item["hostIP"]; + if(!host_ip.isNull()) + { + std::string hip = host_ip.asString(); + if(!hip.empty()) + { + pod.set_host_ip(hip); + } + } + const Json::Value& pod_ip = item["podIP"]; + if(!pod_ip.isNull()) + { + std::string pip = pod_ip.asString(); + if(!pip.empty()) + { + pod.set_internal_ip(pip); + } + } +} + +size_t k8s_pod_handler::extract_pod_restart_count(const Json::Value& item) +{ + size_t restart_count = 0; + const Json::Value& container_statuses = item["containerStatuses"]; + if(!container_statuses.isNull()) + { + for (auto& status : container_statuses) + { + const Json::Value& rc = status["restartCount"]; + if(!rc.isNull() && rc.isInt()) + { + restart_count += rc.asInt(); + } + } + } + return restart_count; +} + +bool k8s_pod_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_pod_t& pod = + m_state->get_component(m_state->get_pods(), + data->m_name, data->m_uid, data->m_namespace); + k8s_pair_list entries = k8s_component::extract_object(json, "labels"); + if(entries.size() > 0) + { + pod.set_labels(std::move(entries)); + } + k8s_pod_t::container_id_list container_ids = extract_pod_container_ids(json); + k8s_container::list containers = extract_pod_containers(json); + extract_pod_data(json, pod); + pod.set_restart_count(extract_pod_restart_count(json)); + pod.set_container_ids(std::move(container_ids)); + pod.set_containers(std::move(containers)); + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_pods(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: data is null."); + } + return true; +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_pod_handler.h b/userspace/libsinsp/k8s_pod_handler.h new file mode 100644 index 0000000000..58a11b5b60 --- /dev/null +++ b/userspace/libsinsp/k8s_pod_handler.h @@ -0,0 +1,62 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_pod_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_component.h" + +class sinsp; + +class k8s_pod_handler : public k8s_handler +{ +public: + k8s_pod_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_pod_handler(); + + static std::vector extract_pod_container_ids(const Json::Value& item); + static k8s_container::list extract_pod_containers(const Json::Value& item); + static void extract_pod_data(const Json::Value& item, k8s_pod_t& pod); + static size_t extract_pod_restart_count(const Json::Value& item); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_replicaset_handler.cpp b/userspace/libsinsp/k8s_replicaset_handler.cpp new file mode 100644 index 0000000000..218f8a3c67 --- /dev/null +++ b/userspace/libsinsp/k8s_replicaset_handler.cpp @@ -0,0 +1,161 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_replicaset_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_replicaset_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_replicaset_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " specReplicas: .spec.replicas," + " statReplicas: .status.replicas," + " selector: .spec.selector.matchLabels," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_replicaset_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"ReplicaSet\", " + " items:" + " [" + " .items[] | " + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " specReplicas: .spec.replicas," + " statReplicas: .status.replicas," + " selector: .spec.selector.matchLabels," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_replicaset_handler::NULL_FILTER = + "{" + " type: \"NONEXISTENT\"," + " apiVersion: .apiVersion," + " kind: \"ReplicaSet\", " + " items: [ null ]" + "}"; + +k8s_replicaset_handler::k8s_replicaset_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_replicaset_handler", true, +#ifdef HAS_CAPTURE + url, "/apis/apps/v1/replicasets", + STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + 100, // max msgs + &state) +{ +} + +k8s_replicaset_handler::~k8s_replicaset_handler() +{ +} + +bool k8s_replicaset_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_rs_t& rs = + m_state->get_component(m_state->get_rss(), + data->m_name, data->m_uid, data->m_namespace); + k8s_pair_list entries = k8s_component::extract_object(json, "labels"); + if(entries.size() > 0) + { + rs.set_labels(std::move(entries)); + } + handle_selectors(rs, json["selector"]); + const Json::Value& spec = json["specReplicas"]; + const Json::Value& stat = json["statReplicas"]; + if(!spec.isNull() && spec.isConvertibleTo(Json::intValue) && + !stat.isNull() && stat.isConvertibleTo(Json::intValue)) + { + rs.set_replicas(spec.asInt(), stat.asInt()); + } + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_rss(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: state is null."); + } + } + else + { + throw sinsp_exception("K8s node handler: data is null."); + } + return true; +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_replicaset_handler.h b/userspace/libsinsp/k8s_replicaset_handler.h new file mode 100644 index 0000000000..7612bd11ba --- /dev/null +++ b/userspace/libsinsp/k8s_replicaset_handler.h @@ -0,0 +1,58 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_replicaset_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_state.h" + +class sinsp; + +class k8s_replicaset_handler : public k8s_handler +{ +public: + k8s_replicaset_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_replicaset_handler(); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + static std::string NULL_FILTER; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_replicationcontroller_handler.cpp b/userspace/libsinsp/k8s_replicationcontroller_handler.cpp new file mode 100644 index 0000000000..b1067fd3a4 --- /dev/null +++ b/userspace/libsinsp/k8s_replicationcontroller_handler.cpp @@ -0,0 +1,161 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_replicationcontroller_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_replicationcontroller_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_replicationcontroller_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " specReplicas: .spec.replicas," + " statReplicas: .status.replicas," + " selector: .spec.selector," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_replicationcontroller_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"ReplicationController\", " + " items:" + " [" + " .items[] | " + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " specReplicas: .spec.replicas," + " statReplicas: .status.replicas," + " selector: .spec.selector," + " labels: .metadata.labels" + " }" + " ]" + "}"; + +std::string k8s_replicationcontroller_handler::NULL_FILTER = + "{" + " type: \"NONEXISTENT\"," + " apiVersion: .apiVersion," + " kind: \"ReplicationController\", " + " items: [ null ]" + "}"; + +k8s_replicationcontroller_handler::k8s_replicationcontroller_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_replicationcontroller_handler", true, +#ifdef HAS_CAPTURE + url, "/api/v1/replicationcontrollers", + STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + 100, // max msgs + &state) +{ +} + +k8s_replicationcontroller_handler::~k8s_replicationcontroller_handler() +{ +} + +bool k8s_replicationcontroller_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_rc_t& rc = + m_state->get_component(m_state->get_rcs(), + data->m_name, data->m_uid, data->m_namespace); + k8s_pair_list entries = extract_object(json["labels"]); + if(entries.size() > 0) + { + rc.set_labels(std::move(entries)); + } + handle_selectors(rc, json["selector"]); + const Json::Value& spec = json["specReplicas"]; + const Json::Value& stat = json["statReplicas"]; + if(!spec.isNull() && spec.isConvertibleTo(Json::intValue) && + !stat.isNull() && stat.isConvertibleTo(Json::intValue)) + { + rc.set_replicas(spec.asInt(), stat.asInt()); + } + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_rcs(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: state is null."); + } + } + else + { + throw sinsp_exception("K8s node handler: data is null."); + } + return true; +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/k8s_replicationcontroller_handler.h b/userspace/libsinsp/k8s_replicationcontroller_handler.h new file mode 100644 index 0000000000..7ffb9a316a --- /dev/null +++ b/userspace/libsinsp/k8s_replicationcontroller_handler.h @@ -0,0 +1,58 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_replicationcontroller_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_state.h" + +class sinsp; + +class k8s_replicationcontroller_handler : public k8s_handler +{ +public: + k8s_replicationcontroller_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_replicationcontroller_handler(); + +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + static std::string NULL_FILTER; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_service_handler.cpp b/userspace/libsinsp/k8s_service_handler.cpp new file mode 100644 index 0000000000..09a2e89a04 --- /dev/null +++ b/userspace/libsinsp/k8s_service_handler.cpp @@ -0,0 +1,255 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_service_handler.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_service_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" + +// filters normalize state and event JSONs, so they can be processed generically: +// event is turned into a single-entry array, state is turned into an array of ADDED events + +std::string k8s_service_handler::EVENT_FILTER = + "{" + " type: .type," + " apiVersion: .object.apiVersion," + " kind: .object.kind," + " items:" + " [" + " .object |" + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " clusterIP: .spec.clusterIP," + " ports: .spec.ports," + " labels: .metadata.labels," + " selector: .spec.selector" + " }" + " ]" + "}"; + +std::string k8s_service_handler::STATE_FILTER = + "{" + " type: \"ADDED\"," + " apiVersion: .apiVersion," + " kind: \"Service\", " + " items:" + " [" + " .items[] | " + " {" + " namespace: .metadata.namespace," + " name: .metadata.name," + " uid: .metadata.uid," + " timestamp: .metadata.creationTimestamp," + " clusterIP: .spec.clusterIP," + " ports: .spec.ports," + " labels: .metadata.labels," + " selector: .spec.selector" + " }" + " ]" + "}"; + +std::string k8s_service_handler::NULL_FILTER = + "{" + " type: \"NONEXISTENT\"," + " apiVersion: .apiVersion," + " kind: \"Service\", " + " items: [ null ]" + "}"; + +k8s_service_handler::k8s_service_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector + ,std::string url + ,const std::string& http_version + ,ssl_ptr_t ssl + ,bt_ptr_t bt + ,bool connect + ,bool blocking_socket +#endif // HAS_CAPTURE + ): + k8s_handler("k8s_service_handler", true, +#ifdef HAS_CAPTURE + url, "/api/v1/services", + STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector, + http_version, 1000L, ssl, bt, true, + connect, dependency_handler, blocking_socket, +#endif // HAS_CAPTURE + 100, // max msgs + &state) +{ +} + +k8s_service_handler::~k8s_service_handler() +{ +} + +void k8s_service_handler::extract_services_data(const Json::Value& json, k8s_service_t& service, const k8s_pods& pods) +{ + if(!json.isNull()) + { + const Json::Value& cluster_ip = json["clusterIP"]; + if(!cluster_ip.isNull()) + { + service.set_cluster_ip(cluster_ip.asString()); + } + + k8s_service_t::port_list pl; + const Json::Value& ports = json["ports"]; + if(!ports.isNull() && ports.isArray()) + { + for (auto& port : ports) + { + k8s_service_t::net_port p; + const Json::Value& json_port = port["port"]; + if(!json_port.isNull()) + { + p.m_port = json_port.asUInt(); + } + + const Json::Value& json_protocol = port["protocol"]; + if(!json_protocol.isNull()) + { + p.m_protocol = json_protocol.asString(); + } + + const Json::Value& json_target_port = port["targetPort"]; + if(!json_target_port.isNull()) + { + if(json_target_port.isIntegral()) + { + p.m_target_port = json_target_port.asUInt(); + } + else if(json_target_port.isString()) + { + std::string port_name = json_target_port.asString(); + std::vector pod_subset = service.get_selected_pods(pods); + p.m_target_port = 0; + for(const auto& pod : pod_subset) + { + const k8s_container::list& containers = pod->get_containers(); + for(const auto& container : containers) + { + const k8s_container::port* container_port = container.get_port(port_name); + if(container_port) + { + g_logger.log("K8s: found port for service [" + service.get_name() + "], " + "container [" + container.get_name() + ']', + sinsp_logger::SEV_DEBUG); + p.m_target_port = container_port->get_port(); + break; + } + else + { + g_logger.log("K8s: error while trying to determine port for service [" + service.get_name() + "]: " + "no ports found for container [" + container.get_name() + "]", sinsp_logger::SEV_ERROR); + p.m_target_port = 0; + } + } + } + } + else + { + g_logger.log("Port of unknown or unsupported type.", sinsp_logger::SEV_ERROR); + p.m_target_port = 0; + } + } + + const Json::Value& json_node_port = port["nodePort"]; + if(!json_node_port.isNull()) + { + p.m_node_port = json_node_port.asUInt(); + } + + if(p.m_port && p.m_target_port) + { + pl.push_back(p); + } + } + } + + if(pl.size()) + { + service.set_port_list(std::move(pl)); + } + } + else + { + g_logger.log("Error while extracting data for service [" + service.get_name() + "]: " + " JSON is null.", sinsp_logger::SEV_ERROR); + } +} + +bool k8s_service_handler::handle_component(const Json::Value& json, const msg_data* data) +{ + if(data) + { + if(m_state) + { + if((data->m_reason == k8s_component::COMPONENT_ADDED) || + (data->m_reason == k8s_component::COMPONENT_MODIFIED)) + { + k8s_service_t& service = + m_state->get_component(m_state->get_services(), + data->m_name, data->m_uid, data->m_namespace); + k8s_pair_list entries = k8s_component::extract_object(json, "labels"); + if(entries.size() > 0) + { + service.set_labels(std::move(entries)); + } + entries = k8s_component::extract_object(json, "selector"); + if(entries.size() > 0) + { + service.set_selectors(std::move(entries)); + } + extract_services_data(json, service, m_state->get_pods()); + } + else if(data->m_reason == k8s_component::COMPONENT_DELETED) + { + if(!m_state->delete_component(m_state->get_services(), data->m_uid)) + { + log_not_found(*data); + return false; + } + } + else if(data->m_reason != k8s_component::COMPONENT_ERROR) + { + g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") + + std::to_string(data->m_reason), sinsp_logger::SEV_ERROR); + return false; + } + } + else + { + throw sinsp_exception("K8s node handler: state is null."); + } + } + else + { + throw sinsp_exception("K8s node handler: data is null."); + } + return true; +} +#endif \ No newline at end of file diff --git a/userspace/libsinsp/k8s_service_handler.h b/userspace/libsinsp/k8s_service_handler.h new file mode 100644 index 0000000000..0e8bc60cbd --- /dev/null +++ b/userspace/libsinsp/k8s_service_handler.h @@ -0,0 +1,59 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_service_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp_auth.h" +#include "k8s_handler.h" +#include "k8s_state.h" + +class sinsp; + +class k8s_service_handler : public k8s_handler +{ +public: + k8s_service_handler(k8s_state_t& state +#ifdef HAS_CAPTURE + ,ptr_t dependency_handler + ,collector_ptr_t collector = nullptr + ,std::string url = "" + ,const std::string& http_version = "1.1" + ,ssl_ptr_t ssl = 0 + ,bt_ptr_t bt = 0 + ,bool connect = true + ,bool blocking_socket = false +#endif // HAS_CAPTURE + ); + + ~k8s_service_handler(); + + static void extract_services_data(const Json::Value& spec, k8s_service_t& service, const k8s_pods& pods); +private: + static std::string EVENT_FILTER; + static std::string STATE_FILTER; + static std::string NULL_FILTER; + + virtual bool handle_component(const Json::Value& json, const msg_data* data = 0); +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/k8s_state.cpp b/userspace/libsinsp/k8s_state.cpp new file mode 100644 index 0000000000..1863b47b6d --- /dev/null +++ b/userspace/libsinsp/k8s_state.cpp @@ -0,0 +1,825 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_state.cpp +// +#ifndef CYGWING_AGENT + +#include "k8s_state.h" +#include "k8s_pod_handler.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include + +// +// state +// + +const std::string k8s_state_t::m_docker_prefix = "docker://"; +const std::string k8s_state_t::m_rkt_prefix = "rkt://"; +const std::string k8s_state_t::m_containerd_prefix = "containerd://"; +const std::string k8s_state_t::m_crio_prefix = "cri-o://"; +const unsigned k8s_state_t::m_id_length = 12u; + +k8s_state_t::k8s_state_t(bool is_captured, int capture_version): + m_is_captured(is_captured), + m_capture_version(capture_version) +{ +} + +// state/pods + +void k8s_state_t::update_pod(k8s_pod_t& pod, const Json::Value& item) +{ + k8s_pod_t::container_id_list container_ids = k8s_pod_handler::extract_pod_container_ids(item); + k8s_container::list containers = k8s_pod_handler::extract_pod_containers(item); + k8s_pod_handler::extract_pod_data(item, pod); + pod.set_restart_count(k8s_pod_handler::extract_pod_restart_count(item)); + pod.set_container_ids(std::move(container_ids)); + pod.set_containers(std::move(containers)); +} + +void k8s_state_t::cache_pod(container_pod_map& map, const std::string& id, const k8s_pod_t* pod) +{ + ASSERT(pod); + ASSERT(!pod->get_name().empty()); + std::string::size_type pos = id.find(m_docker_prefix); + if(pos == 0) + { + map[id.substr(m_docker_prefix.size(), m_id_length)] = pod; + return; + } + pos = id.find(m_rkt_prefix); + if(pos == 0) + { + map[id.substr(m_rkt_prefix.size())] = pod; + return; + } + pos = id.find(m_containerd_prefix); + if(pos == 0) + { + map[id.substr(m_containerd_prefix.size(), m_id_length)] = pod; + return; + } + pos = id.find(m_crio_prefix); + if(pos == 0) + { + map[id.substr(m_crio_prefix.size(), m_id_length)] = pod; + return; + } + throw sinsp_exception("Invalid container ID (expected one of: '" + m_docker_prefix + + "{ID}', '" + m_rkt_prefix + "{ID}', '" + m_containerd_prefix + + "{ID}', '" + m_crio_prefix + "{ID}'): " + id); +} + +bool k8s_state_t::has_pod(k8s_pod_t& pod) +{ + for(const auto& p : m_pods) + { + if(p == pod) { return true; } + } + return false; +} + +// state/events + +void k8s_state_t::update_event(k8s_event_t& evt, const Json::Value& item) +{ + if(!item.isNull()) + { + evt.update(item, *this); + } + else + { + g_logger.log("NULL K8s event received.", sinsp_logger::SEV_WARNING); + } +} + +// state/general + +void k8s_state_t::replace_items(k8s_component::type t, const std::string& name, const std::vector&& items) +{ + switch (t) + { + case k8s_component::K8S_NODES: + if(name == "labels") + { + m_nodes.back().m_labels = std::move(items); + return; + } + break; + + case k8s_component::K8S_NAMESPACES: + if(name == "labels") + { + m_namespaces.back().m_labels = std::move(items); + return; + } + break; + + case k8s_component::K8S_PODS: + if(name == "labels") + { + m_pods.back().m_labels = std::move(items); + return; + } + break; + + case k8s_component::K8S_REPLICATIONCONTROLLERS: + if(name == "labels") + { + m_controllers.back().m_labels = std::move(items); + return; + } + else if(name == "selector") + { + m_controllers.back().m_selectors = std::move(items); + return; + } + break; + + case k8s_component::K8S_REPLICASETS: + if(name == "labels") + { + m_replicasets.back().m_labels = std::move(items); + return; + } + else if(name == "selector") + { + m_replicasets.back().m_selectors = std::move(items); + return; + } + break; + + case k8s_component::K8S_SERVICES: + if(name == "labels") + { + m_services.back().m_labels = std::move(items); + return; + } + else if(name == "selector") + { + m_services.back().m_selectors = std::move(items); + return; + } + break; + + case k8s_component::K8S_DEPLOYMENTS: + if(name == "labels") + { + m_deployments.back().m_labels = std::move(items); + return; + } + else if(name == "selector") + { + m_deployments.back().m_selectors = std::move(items); + return; + } + break; + + case k8s_component::K8S_DAEMONSETS: + if(name == "labels") + { + m_daemonsets.back().m_labels = std::move(items); + return; + } + else if(name == "selector") + { + m_daemonsets.back().m_selectors = std::move(items); + return; + } + break; + + case k8s_component::K8S_EVENTS: + return; + + case k8s_component::K8S_COMPONENT_COUNT: + default: + break; + } + + std::ostringstream os; + os << "Unknown component type " << static_cast(t) << + " or object name " << name; + throw sinsp_exception(os.str().c_str()); +} + +k8s_component& k8s_state_t::add_common_single_value(k8s_component::type component, const std::string& name, const std::string& uid, const std::string& ns) +{ + switch (component) + { + case k8s_component::K8S_NODES: + return get_component(m_nodes, name, uid, ns); + + case k8s_component::K8S_NAMESPACES: + return get_component(m_namespaces, name, uid, ns); + + case k8s_component::K8S_PODS: + return get_component(m_pods, name, uid, ns); + + case k8s_component::K8S_REPLICATIONCONTROLLERS: + return get_component(m_controllers, name, uid, ns); + + case k8s_component::K8S_REPLICASETS: + return get_component(m_replicasets, name, uid, ns); + + case k8s_component::K8S_SERVICES: + return get_component(m_services, name, uid, ns); + + case k8s_component::K8S_DAEMONSETS: + return get_component(m_daemonsets, name, uid, ns); + + case k8s_component::K8S_DEPLOYMENTS: + return get_component(m_deployments, name, uid, ns); + + case k8s_component::K8S_EVENTS: + return get_component(m_events, name, uid, ns); + + case k8s_component::K8S_COMPONENT_COUNT: + default: + break; + } + + std::ostringstream os; + os << "Unknown component: " << component; + throw sinsp_exception(os.str()); +} + +k8s_node_t* k8s_state_t::get_node(const std::string& uid) +{ + for (auto& node : m_nodes) + { + if(node.get_uid() == uid) + { + return &node; + } + } + + return nullptr; +} + +void k8s_state_t::clear(k8s_component::type type) +{ + if(type == k8s_component::K8S_COMPONENT_COUNT) + { + m_namespaces.clear(); + m_nodes.clear(); + m_pods.clear(); + m_controllers.clear(); + m_services.clear(); + } + else + { + switch (type) + { + case k8s_component::K8S_NODES: + m_nodes.clear(); + break; + case k8s_component::K8S_NAMESPACES: + m_namespaces.clear(); + break; + case k8s_component::K8S_PODS: + m_pods.clear(); + break; + case k8s_component::K8S_REPLICATIONCONTROLLERS: + m_controllers.clear(); + break; + case k8s_component::K8S_REPLICASETS: + m_replicasets.clear(); + break; + case k8s_component::K8S_SERVICES: + m_services.clear(); + break; + case k8s_component::K8S_DAEMONSETS: + m_daemonsets.clear(); + break; + case k8s_component::K8S_DEPLOYMENTS: + m_deployments.clear(); + break; + case k8s_component::K8S_EVENTS: + m_events.clear(); + break; + case k8s_component::K8S_COMPONENT_COUNT: + default: + break; + } + } +} + +// state/caching + +void k8s_state_t::update_cache(const k8s_component::type_map::key_type& component) +{ +#ifndef HAS_ANALYZER + switch (component) + { + case k8s_component::K8S_NAMESPACES: + { + const k8s_namespaces& nspaces = get_namespaces(); + k8s_state_t::namespace_map& ns_map = get_namespace_map(); + ns_map.clear(); + for(const auto& ns : nspaces) + { + std::string ns_name = ns.get_name(); + if(!is_component_cached(ns_map, ns_name, &ns)) + { + cache_component(ns_map, ns_name, &ns); + } + else + { + g_logger.log("Attempt to cache already cached NAMESPACE: " + ns_name, sinsp_logger::SEV_ERROR); + } + } + } + break; + + case k8s_component::K8S_PODS: + { + const k8s_pods& pods = get_pods(); + k8s_state_t::container_pod_map& container_pod_map = get_container_pod_map(); + container_pod_map.clear(); + for(const auto& pod : pods) + { + const k8s_pod_t::container_id_list& c_ids = pod.get_container_ids(); + for(const auto& c_id : c_ids) + { + if(!is_component_cached(container_pod_map, c_id, &pod)) + { + cache_pod(container_pod_map, c_id, &pod); + } + else + { + g_logger.log("Attempt to cache already cached POD: " + c_id, sinsp_logger::SEV_ERROR); + } + } + } + } + break; + + case k8s_component::K8S_REPLICATIONCONTROLLERS: + { + const k8s_controllers& rcs = get_rcs(); + const k8s_pods& pods = get_pods(); + k8s_state_t::pod_rc_map& pod_ctrl_map = get_pod_rc_map(); + pod_ctrl_map.clear(); + for(const auto& rc : rcs) + { + std::vector pod_subset = rc.get_selected_pods(pods); + for(auto& pod : pod_subset) + { + const std::string& pod_uid = pod->get_uid(); + if(!is_component_cached(pod_ctrl_map, pod_uid, &rc)) + { + cache_component(pod_ctrl_map, pod_uid, &rc); + } + else + { + g_logger.log("Attempt to cache already cached REPLICATION CONTROLLER: " + pod_uid, sinsp_logger::SEV_ERROR); + } + } + } + } + break; + + case k8s_component::K8S_REPLICASETS: + { + const k8s_replicasets& rss = get_rss(); + const k8s_pods& pods = get_pods(); + k8s_state_t::pod_rs_map& pod_rset_map = get_pod_rs_map(); + pod_rset_map.clear(); + for(const auto& rs : rss) + { + std::vector pod_subset = rs.get_selected_pods(pods); + for(auto& pod : pod_subset) + { + const std::string& pod_uid = pod->get_uid(); + if(!is_component_cached(pod_rset_map, pod_uid, &rs)) + { + cache_component(pod_rset_map, pod_uid, &rs); + } + else + { + g_logger.log("Attempt to cache already cached REPLICA SET: " + pod_uid, sinsp_logger::SEV_ERROR); + } + } + } + } + break; + + case k8s_component::K8S_SERVICES: + { + const k8s_services& services = get_services(); + const k8s_pods& pods = get_pods(); + k8s_state_t::pod_service_map& pod_svc_map = get_pod_service_map(); + pod_svc_map.clear(); + for(const auto& service : services) + { + std::vector pod_subset = service.get_selected_pods(pods); + for(auto& pod : pod_subset) + { + const std::string& pod_uid = pod->get_uid(); + if(!is_component_cached(pod_svc_map, pod_uid, &service)) + { + cache_component(pod_svc_map, pod_uid, &service); + } + else + { + g_logger.log("Attempt to cache already cached SERVICE: " + pod_uid, sinsp_logger::SEV_ERROR); + } + } + } + } + break; + + case k8s_component::K8S_DAEMONSETS: + { + // TODO + /*const k8s_daemonsets& daemonsets = get_daemonsets(); + const k8s_pods& pods = get_pods(); + k8s_state_t::pod_daemonset_map& pod_svc_map = get_pod_daemonset_map(); + pod_svc_map.clear(); + for(const auto& daemonset : daemonsets) + { + std::vector pod_subset = daemonset.get_selected_pods(pods); + for(auto& pod : pod_subset) + { + const std::string& pod_uid = pod->get_uid(); + if(!is_component_cached(pod_svc_map, pod_uid, &daemonset)) + { + cache_component(pod_svc_map, pod_uid, &daemonset); + } + else + { + g_logger.log("Attempt to cache already cached SERVICE: " + pod_uid, sinsp_logger::SEV_ERROR); + } + } + }*/ + } + break; + + case k8s_component::K8S_DEPLOYMENTS: + { + const k8s_deployments& deployments = get_deployments(); + const k8s_pods& pods = get_pods(); + k8s_state_t::pod_deployment_map& pod_deployment_map = get_pod_deployment_map(); + pod_deployment_map.clear(); + for(const auto& deployment : deployments) + { + std::vector pod_subset = deployment.get_selected_pods(pods); + for(auto& pod : pod_subset) + { + const std::string& pod_uid = pod->get_uid(); + if(!is_component_cached(pod_deployment_map, pod_uid, &deployment)) + { + cache_component(pod_deployment_map, pod_uid, &deployment); + } + else + { + g_logger.log("Attempt to cache already cached Deployment: " + pod_uid, sinsp_logger::SEV_ERROR); + } + } + } + } + break; + + default: return; + } +#endif // HAS_ANALYZER +} + +k8s_component::type k8s_state_t::component_from_json(const Json::Value& item) +{ + const Json::Value& kind = item["kind"]; + if(kind.isNull() || !kind.isString()) + { + throw sinsp_exception("Component kind not found in JSON."); + } + std::string comp = kind.asString(); + if(comp == "Node") + { + return k8s_component::K8S_NODES; + } + else if(comp == "Namespace") + { + return k8s_component::K8S_NAMESPACES; + } + else if(comp == "Pod") + { + return k8s_component::K8S_PODS; + } + else if(comp == "ReplicationController") + { + return k8s_component::K8S_REPLICATIONCONTROLLERS; + } + else if(comp == "ReplicaSet") + { + return k8s_component::K8S_REPLICASETS; + } + else if(comp == "Service") + { + return k8s_component::K8S_SERVICES; + } + else if(comp == "DaemonSet") + { + return k8s_component::K8S_DAEMONSETS; + } + else if(comp == "Deployment") + { + return k8s_component::K8S_DEPLOYMENTS; + } + else if(comp == "Event") + { + return k8s_component::K8S_EVENTS; + } + + throw sinsp_exception("Unknown component kind:" + comp); +} + +const k8s_component* k8s_state_t::get_component(const std::string& uid, std::string* t) const +{ + component_map_t::const_iterator it = m_component_map.find(uid); + if(it != m_component_map.end()) + { + switch(it->second) + { + case k8s_component::K8S_NODES: + if(t) { *t = "node"; } + return get_component(m_nodes, uid); + break; + case k8s_component::K8S_NAMESPACES: + if(t) { *t = "namespace"; } + return get_component(m_namespaces, uid); + break; + case k8s_component::K8S_PODS: + if(t) { *t = "pod"; } + return get_component(m_pods, uid); + break; + case k8s_component::K8S_REPLICATIONCONTROLLERS: + if(t) { *t = "replicationController"; } + return get_component(m_controllers, uid); + break; + case k8s_component::K8S_REPLICASETS: + if(t) { *t = "replicaSet"; } + return get_component(m_replicasets, uid); + break; + case k8s_component::K8S_SERVICES: + if(t) { *t = "service"; } + return get_component(m_services, uid); + break; + case k8s_component::K8S_DAEMONSETS: + if(t) { *t = "daemonSet"; } + return get_component(m_daemonsets, uid); + break; + case k8s_component::K8S_DEPLOYMENTS: + if(t) { *t = "deployment"; } + return get_component(m_deployments, uid); + break; + case k8s_component::K8S_EVENTS: + if(t) { *t = "event"; } + return get_component(m_events, uid); + break; + default: + if(t) { t->clear(); } + return nullptr; + } + } + return nullptr; +} + +#ifdef HAS_CAPTURE +void k8s_state_t::enqueue_capture_event(const Json::Value& item) +{ + if(m_is_captured) + { + std::string json; + if(m_capture_version == k8s_state_t::CAPTURE_VERSION_1) + { + json = Json::FastWriter().write(extract_capture_data(item)); + } + else if(m_capture_version == k8s_state_t::CAPTURE_VERSION_2) + { + json = Json::FastWriter().write(item); + } + else + { + throw sinsp_exception(std::string("K8s : invalid capture version (") + + std::to_string(m_capture_version) + ')'); + } + m_capture_events.emplace_back(json); + } +} + +std::string k8s_state_t::dequeue_capture_event() +{ + if(!m_capture_events.size()) + { + throw sinsp_exception("Invalid event dequeue request."); + } + std::string ev = m_capture_events.front(); + m_capture_events.pop_front(); + return ev; +} +#endif // HAS_CAPTURE + +Json::Value k8s_state_t::extract_capture_data(const Json::Value& item) +{ + Json::Value cap_item; + +#ifdef HAS_CAPTURE + k8s_component::type component = component_from_json(item); + + Json::Value ver = item["apiVersion"]; + if(!ver.isNull() && ver.isString()) + { + cap_item["apiVersion"] = ver.asString(); + } + else + { + throw sinsp_exception("K8S capture: API version not provided."); + } + + Json::Value type = item["type"]; + if(!type.isNull() && type.isString()) + { + cap_item["type"] = type.asString(); + } + else + { + throw sinsp_exception("K8S capture: event type not provided."); + } + + Json::Value kind = item["kind"]; + if(!kind.isNull() && kind.isString()) + { + cap_item["kind"] = kind.asString(); + } + else + { + throw sinsp_exception("K8S capture: component kind not provided."); + } + const Json::Value& object = item["object"]; + if(object.isNull()) + { + throw sinsp_exception("K8S capture: object not found."); + } + + cap_item["object"] = Json::Value(); + Json::Value& cap_object = cap_item["object"]; + cap_object["metadata"] = Json::Value(); + Json::Value& cap_metadata = cap_object["metadata"]; + + const Json::Value& metadata = object["metadata"]; + if(metadata.isNull()) + { + throw sinsp_exception("K8S capture: object metadata not found."); + } + else + { + Json::Value ns = metadata["namespace"]; + if(!ns.isNull()) + { + cap_metadata["namespace"] = ns.asString(); + } + cap_metadata["name"] = metadata["name"].asString(); + cap_metadata["uid"] = metadata["uid"].asString(); + + Json::Value labels = metadata["labels"]; + if(!labels.isNull()) + { + cap_metadata["labels"] = labels; + } + } + + Json::Value spec = object["spec"]; + if(spec.isNull()) + { + throw sinsp_exception("K8S capture: object spec not found."); + } + else + { + Json::Value selector = spec["selector"]; + if(!selector.isNull()) + { + cap_object["spec"] = Json::Value(); + Json::Value& cap_spec = cap_object["spec"]; + cap_spec["selector"] = std::move(selector); + } + } + + Json::Value status = object["status"]; + if(status.isNull()) + { + throw sinsp_exception("K8S capture: object status not found."); + } + + switch(component) + { + case k8s_component::K8S_NAMESPACES: + break; + + case k8s_component::K8S_NODES: + { + cap_object["status"] = Json::Value(); + Json::Value& cap_status = cap_object["status"]; + cap_object["status"] = Json::Value(); + cap_status["addresses"] = status["addresses"]; + } + break; + + case k8s_component::K8S_PODS: + { + cap_object["spec"] = Json::Value(); + Json::Value& cap_spec = cap_object["spec"]; + const Json::Value& node_name = spec["nodeName"]; + if(!node_name.isNull()) + { + cap_spec["nodeName"] = node_name.asString(); + } + + cap_object["status"] = Json::Value(); + Json::Value& cap_status = cap_object["status"]; + const Json::Value& host_ip = status["hostIP"]; + if(!host_ip.isNull()) + { + cap_status["hostIP"] = host_ip.asString(); + } + const Json::Value& pod_ip = status["podIP"]; + if(!pod_ip.isNull()) + { + cap_status["podIP"] = pod_ip.asString(); + } + + if(status.isMember("containerStatuses") && status["containerStatuses"].isArray()) + { + for(const auto& c_status : status["containerStatuses"]) + { + Json::Value new_status; + new_status["containerID"] = c_status["containerID"]; + new_status["restartCount"] = c_status["restartCount"]; + cap_status["containerStatuses"].append(new_status); + } + } + } + break; + + case k8s_component::K8S_SERVICES: + { + cap_object["spec"] = Json::Value(); + Json::Value& cap_spec = cap_object["spec"]; + cap_spec["clusterIP"] = spec["clusterIP"].asString(); + cap_spec["ports"] = spec["ports"]; + } + break; + + case k8s_component::K8S_REPLICATIONCONTROLLERS: + break; + + case k8s_component::K8S_REPLICASETS: + break; + + case k8s_component::K8S_DAEMONSETS: + break; + + case k8s_component::K8S_DEPLOYMENTS: + break; + + case k8s_component::K8S_EVENTS: + break; + + default: break; + } + std::ostringstream os; + std::string nspace; + if(cap_metadata.isMember("namespace")) + { + nspace = cap_metadata["namespace"].asString(); + } + os << "Capture: [" << cap_item["type"].asString() << ',' << cap_item["kind"].asString() << ',' << + cap_metadata["name"].asString() << ',' << cap_metadata["uid"].asString() << ',' << nspace << ']'; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + //g_logger.log(item.toStyledString(), sinsp_logger::SEV_DEBUG); + //g_logger.log(cap_item.toStyledString(), sinsp_logger::SEV_DEBUG); + +#endif // HAS_CAPTURE + + return cap_item; +} +#endif // CYGWING_AGENT + diff --git a/userspace/libsinsp/k8s_state.h b/userspace/libsinsp/k8s_state.h new file mode 100644 index 0000000000..7784d6c474 --- /dev/null +++ b/userspace/libsinsp/k8s_state.h @@ -0,0 +1,665 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_state.h +// +// kubernetes state abstraction +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "k8s_component.h" +#include "json/json.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include +#include + +// +// state +// + +class k8s_state_t +{ +public: + typedef std::unordered_map namespace_map; + typedef std::unordered_map container_pod_map; + typedef std::unordered_multimap pod_service_map; + typedef std::unordered_map pod_rc_map; + typedef std::unordered_map pod_rs_map; + typedef std::unordered_multimap pod_deployment_map; + + static const int CAPTURE_VERSION_NONE = -1; + static const int CAPTURE_VERSION_1 = 1; + static const int CAPTURE_VERSION_2 = 2; + + k8s_state_t(bool is_captured = false, int capture_version = CAPTURE_VERSION_2); + + // + // namespaces + // + + const k8s_namespaces& get_namespaces() const; + k8s_namespaces& get_namespaces(); + void push_namespace(const k8s_ns_t& ns); + void emplace_namespace(k8s_ns_t&& ns); + + // + // nodes + // + + const k8s_nodes& get_nodes() const; + k8s_nodes& get_nodes(); + k8s_node_t* get_node(const std::string& uid); + void push_node(const k8s_node_t& node); + void emplace_node(k8s_node_t&& node); + + // + // pods + // + + const k8s_pods& get_pods() const; + k8s_pods& get_pods(); + void push_pod(const k8s_pod_t& pod); + void emplace_pod(k8s_pod_t&& pod); + void update_pod(k8s_pod_t& pod, const Json::Value& item); + bool has_pod(k8s_pod_t& pod); + const k8s_pod_t::container_id_list& get_pod_container_ids(k8s_pod_t& pod); + + // + // replication controllers + // + + const k8s_controllers& get_rcs() const; + k8s_controllers& get_rcs(); + void push_rc(const k8s_rc_t& rc); + void emplace_rc(k8s_rc_t&& rc); + + // + // replica sets + // + + const k8s_replicasets& get_rss() const; + k8s_replicasets& get_rss(); + void push_rs(const k8s_rs_t& rs); + void emplace_rs(k8s_rs_t&& rs); + + // + // services + // + + const k8s_services& get_services() const; + k8s_services& get_services(); + void push_service(const k8s_service_t& service); + void emplace_service(k8s_service_t&& service); + + // + // daemonsets + // + + const k8s_daemonsets& get_daemonsets() const; + k8s_daemonsets& get_daemonsets(); + void push_daemonset(const k8s_daemonset_t& daemonset); + void emplace_daemonset(k8s_daemonset_t&& daemonset); + + // + // deployments + // + + const k8s_deployments& get_deployments() const; + k8s_deployments& get_deployments(); + void push_deployment(const k8s_deployment_t& deployment); + void emplace_deployment(k8s_deployment_t&& deployment); + + // + // events + // + + const k8s_events& get_events() const; + k8s_events& get_events(); + void clear_events(); + void push_event(const k8s_event_t& evt); + void emplace_event(k8s_event_t&& evt); + void update_event(k8s_event_t& evt, const Json::Value& item); + + // + // general + // + + void replace_items(k8s_component::type t, const std::string& name, const std::vector&& items); + k8s_component& add_common_single_value(k8s_component::type component, const std::string& name, const std::string& uid, const std::string& ns); + void set_last_pod_node_name(const std::string& name); + void set_last_pod_host_ip(const std::string& host_ip); + void set_last_pod_internal_ip(const std::string& internal_ip); + void add_last_node_ip(std::string&& ip); + void add_last_pod_container_id(std::string&& container_id); + + // Returns true if component exists, false otherwise. + template + bool has(const C& components, const std::string& uid) const + { + for (auto& comp : components) + { + if(uid == comp.get_uid()) + { + return true; + } + } + return false; + } + + bool has(const std::string& uid) const + { + return get_component(uid) != nullptr; + } + + // Returns a pointer to existing component, if it exists. + // If component does not exist, it returns null pointer. + template + T* get_component(C& components, const std::string& uid) + { + for (auto& comp : components) + { + if(comp.get_uid() == uid) + { + return ∁ + } + } + return 0; + } + + template + const T* get_component(const C& components, const std::string& uid) const + { + for (const auto& comp : components) + { + if(comp.get_uid() == uid) + { + return ∁ + } + } + return 0; + } + + template + T& add_component(C& container, const std::string& name, const std::string& uid, const std::string& ns = "") + { + m_component_map[uid] = T::COMPONENT_TYPE; + container.emplace_back(std::move(T(name, uid, ns))); + return container.back(); + } + + // Returns the reference to existing component, if it exists. + // If component does not exist, it emplaces it to the back of the + // container and returns the reference of the added component. + template + T& get_component(C& container, const std::string& name, const std::string& uid, const std::string& ns = "") + { + for (auto& comp : container) + { + if(comp.get_uid() == uid) + { + return comp; + } + } + return add_component(container, name, uid, ns); + } + + template + bool delete_component(C& components, const std::string& uid) + { + for (typename C::iterator component = components.begin(), + end = components.end(); + component != end; + ++component) + { + if(component->get_uid() == uid) + { + components.erase(component); + m_component_map.erase(uid); + return true; + } + } + + return false; + } + + void clear(k8s_component::type type = k8s_component::K8S_COMPONENT_COUNT); + + // + // cached lookup support + // + + // any component by uid + const k8s_component* get_component(const std::string& uid, std::string* t = 0) const; + +#ifndef HAS_ANALYZER + + // pod by container; + const k8s_pod_t* get_pod(const std::string& container) const + { + container_pod_map::const_iterator it = m_container_pods.find(container); + if(it != m_container_pods.end()) + { + return it->second; + } + return 0; + } + + const namespace_map& get_namespace_map() const { return m_namespace_map; } + const container_pod_map& get_container_pod_map() const { return m_container_pods; } + const pod_service_map& get_pod_service_map() const { return m_pod_services; } + const pod_rc_map& get_pod_rc_map() const { return m_pod_rcs; } + const pod_rs_map& get_pod_rs_map() const { return m_pod_rss; } + const pod_deployment_map& get_pod_deployment_map() const { return m_pod_deployments; } + +#endif // HAS_ANALYZER + + void set_capture_version(int version); + int get_capture_version() const; + +#ifdef HAS_CAPTURE + typedef std::deque event_list_t; + const event_list_t& get_capture_events() const { return m_capture_events; } + void enqueue_capture_event(const Json::Value& item); + std::string dequeue_capture_event(); +#endif // HAS_CAPTURE + +private: + + void update_cache(const k8s_component::type_map::key_type& component); + static k8s_component::type component_from_json(const Json::Value& item); + static Json::Value extract_capture_data(const Json::Value& item); + + template + const typename C::mapped_type* get_component(const C& map, const std::string& key) + { + typename C::const_iterator it = map.find(key); + if(it != map.end()) + { + return it->second; + } + return 0; + } + + template + bool is_component_cached(const C& map, const std::string& key) const + { + return (map.find(key) != map.end()); + } + + template + bool is_component_cached(const C& map, const std::string& key, const typename C::mapped_type value) const + { + auto range = map.equal_range(key); + for (auto& it = range.first; it != range.second; ++it) + { + if(it->first == key && it->second == value) + { + return true; + } + } + return false; + } + + void cache_pod(container_pod_map& map, const std::string& id, const k8s_pod_t* pod); + + template + void cache_component(C& map, const std::string& key, typename C::mapped_type component) + { + ASSERT(component); + ASSERT(!component->get_name().empty()); + map.insert(typename C::value_type(key, component)); + return; + } + + template + void uncache_component(C& map, const std::string& key) + { + typename C::iterator it = map.find(key); + if(it != map.end()) + { + map.erase(it); + } + } + +#ifndef HAS_ANALYZER + + namespace_map& get_namespace_map() { return m_namespace_map; } + container_pod_map& get_container_pod_map() { return m_container_pods; } + pod_service_map& get_pod_service_map() { return m_pod_services; } + pod_rc_map& get_pod_rc_map() { return m_pod_rcs; } + pod_rs_map& get_pod_rs_map() { return m_pod_rss; } + pod_deployment_map& get_pod_deployment_map() { return m_pod_deployments; } + +#endif // HAS_ANALYZER + + static const std::string m_docker_prefix; // "docker://" + static const std::string m_rkt_prefix; // "rkt://" + static const std::string m_containerd_prefix; // "containerd://" + static const std::string m_crio_prefix; // "cri-o://" + static const unsigned m_id_length; // portion of the ID to be cached (=12) + +#ifndef HAS_ANALYZER + + namespace_map m_namespace_map; + container_pod_map m_container_pods; + pod_service_map m_pod_services; + pod_rc_map m_pod_rcs; + pod_rs_map m_pod_rss; + pod_deployment_map m_pod_deployments; + +#endif // HAS_ANALYZER + +#ifdef HAS_CAPTURE + event_list_t m_capture_events; +#endif // HAS_CAPTURE + + typedef std::unordered_map component_map_t; + + k8s_namespaces m_namespaces; + k8s_nodes m_nodes; + k8s_pods m_pods; + k8s_controllers m_controllers; + k8s_replicasets m_replicasets; + k8s_services m_services; + k8s_daemonsets m_daemonsets; + k8s_deployments m_deployments; + k8s_events m_events; + // map for uid/type cache for all components + // used by to quickly lookup any component by uid + component_map_t m_component_map; + bool m_is_captured; + int m_capture_version = -1; + + friend class k8s_dispatcher; + friend class k8s_handler; + friend class k8s; +}; + +// namespaces +inline const k8s_namespaces& k8s_state_t::get_namespaces() const +{ + return m_namespaces; +} + +inline k8s_namespaces& k8s_state_t::get_namespaces() +{ + return m_namespaces; +} + +inline void k8s_state_t::push_namespace(const k8s_ns_t& ns) +{ + m_namespaces.push_back(ns); +} + +inline void k8s_state_t::emplace_namespace(k8s_ns_t&& ns) +{ + m_namespaces.emplace_back(std::move(ns)); +} + +// nodes +inline const k8s_nodes& k8s_state_t::get_nodes() const +{ + return m_nodes; +} + +inline k8s_nodes& k8s_state_t::get_nodes() +{ + return m_nodes; +} + +inline void k8s_state_t::push_node(const k8s_node_t& node) +{ + m_nodes.push_back(node); +} + +inline void k8s_state_t::emplace_node(k8s_node_t&& node) +{ + m_nodes.emplace_back(std::move(node)); +} + +// pods +inline const k8s_pods& k8s_state_t::get_pods() const +{ + return m_pods; +} + +inline k8s_pods& k8s_state_t::get_pods() +{ + return m_pods; +} + +inline void k8s_state_t::push_pod(const k8s_pod_t& pod) +{ + m_pods.push_back(pod); +} + +inline void k8s_state_t::emplace_pod(k8s_pod_t&& pod) +{ + m_pods.emplace_back(std::move(pod)); +} + +inline const k8s_pod_t::container_id_list& k8s_state_t::get_pod_container_ids(k8s_pod_t& pod) +{ + return pod.get_container_ids(); +} + +// replication controllers +inline const k8s_controllers& k8s_state_t::get_rcs() const +{ + return m_controllers; +} + +inline k8s_controllers& k8s_state_t::get_rcs() +{ + return m_controllers; +} + +inline void k8s_state_t::push_rc(const k8s_rc_t& rc) +{ + m_controllers.push_back(rc); +} + +inline void k8s_state_t::emplace_rc(k8s_rc_t&& rc) +{ + m_controllers.emplace_back(std::move(rc)); +} + +// replica sets +inline const k8s_replicasets& k8s_state_t::get_rss() const +{ + return m_replicasets; +} + +inline k8s_replicasets& k8s_state_t::get_rss() +{ + return m_replicasets; +} + +inline void k8s_state_t::push_rs(const k8s_rs_t& rs) +{ + m_replicasets.push_back(rs); +} + +inline void k8s_state_t::emplace_rs(k8s_rs_t&& rs) +{ + m_replicasets.emplace_back(std::move(rs)); +} + +// services +inline const k8s_services& k8s_state_t::get_services() const +{ + return m_services; +} + +inline k8s_services& k8s_state_t::get_services() +{ + return m_services; +} + +inline void k8s_state_t::push_service(const k8s_service_t& service) +{ + m_services.push_back(service); +} + +inline void k8s_state_t::emplace_service(k8s_service_t&& service) +{ + m_services.emplace_back(std::move(service)); +} + +// daemonsets +inline const k8s_daemonsets& k8s_state_t::get_daemonsets() const +{ + return m_daemonsets; +} + +inline k8s_daemonsets& k8s_state_t::get_daemonsets() +{ + return m_daemonsets; +} + +inline void k8s_state_t::push_daemonset(const k8s_daemonset_t& daemonset) +{ + m_daemonsets.push_back(daemonset); +} + +inline void k8s_state_t::emplace_daemonset(k8s_daemonset_t&& daemonset) +{ + m_daemonsets.emplace_back(std::move(daemonset)); +} + +// deployments +inline const k8s_deployments& k8s_state_t::get_deployments() const +{ + return m_deployments; +} + +inline k8s_deployments& k8s_state_t::get_deployments() +{ + return m_deployments; +} + +inline void k8s_state_t::push_deployment(const k8s_deployment_t& deployment) +{ + m_deployments.push_back(deployment); +} + +inline void k8s_state_t::emplace_deployment(k8s_deployment_t&& deployment) +{ + m_deployments.emplace_back(std::move(deployment)); +} + +// events +inline const k8s_events& k8s_state_t::get_events() const +{ + return m_events; +} + +inline k8s_events& k8s_state_t::get_events() +{ + return m_events; +} + +inline void k8s_state_t::clear_events() +{ + for(auto it = m_events.begin(); it != m_events.end();) + { + it->post_process((*this)); + if(!it->has_pending_events()) + { + it = m_events.erase(it); + } + else + { + ++it; + } + } +} + +inline void k8s_state_t::push_event(const k8s_event_t& evt) +{ + m_events.push_back(evt); +} + +inline void k8s_state_t::emplace_event(k8s_event_t&& evt) +{ + m_events.emplace_back(std::move(evt)); +} + +// general +inline void k8s_state_t::set_last_pod_node_name(const std::string& name) +{ + if(m_pods.size()) + { + m_pods.back().set_node_name(name); + } +} + +inline void k8s_state_t::set_last_pod_host_ip(const std::string& host_ip) +{ + if(m_pods.size()) + { + m_pods.back().set_host_ip(host_ip); + } +} + +inline void k8s_state_t::set_last_pod_internal_ip(const std::string& internal_ip) +{ + if(m_pods.size()) + { + m_pods.back().set_internal_ip(internal_ip); + } +} + +inline void k8s_state_t::add_last_node_ip(std::string&& ip) +{ + if(m_nodes.size()) + { + m_nodes.back().emplace_host_ip(std::move(ip)); + } +} + +inline void k8s_state_t::add_last_pod_container_id(std::string&& container_id) +{ + if(m_pods.size()) + { + m_pods.back().emplace_container_id(std::move(container_id)); + } +} + +inline void k8s_state_t::set_capture_version(int version) +{ + if(version != CAPTURE_VERSION_NONE && + version != CAPTURE_VERSION_1 && + version != CAPTURE_VERSION_2) + { + throw sinsp_exception(std::string("K8s invalid capture version (") + + std::to_string(version) + ')'); + } + m_capture_version = version; +} + +inline int k8s_state_t::get_capture_version() const +{ + return m_capture_version; +} + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/logger.cpp b/userspace/libsinsp/logger.cpp index 2e88812f59..6fa1c33314 100644 --- a/userspace/libsinsp/logger.cpp +++ b/userspace/libsinsp/logger.cpp @@ -1,20 +1,24 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2019 Sysdig, Inc. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#include "logger.h" +#include "sinsp.h" +#include "sinsp_int.h" #ifndef _WIN32 #include @@ -22,19 +26,30 @@ along with sysdig. If not, see . #include #endif #include -#include "sinsp.h" -#include "sinsp_int.h" -/////////////////////////////////////////////////////////////////////////////// -// sinsp_logger implementation -/////////////////////////////////////////////////////////////////////////////// -sinsp_logger::sinsp_logger() +namespace { - m_file = NULL; - m_flags = OT_NONE; - m_sev = SEV_INFO; - m_callback = NULL; -} + +thread_local char s_tbuf[16384]; + +const size_t ENCODE_LEN = sizeof(uint64_t); + +} // end namespace + +const uint32_t sinsp_logger::OT_NONE = 0; +const uint32_t sinsp_logger::OT_STDOUT = 1; +const uint32_t sinsp_logger::OT_STDERR = (OT_STDOUT << 1); +const uint32_t sinsp_logger::OT_FILE = (OT_STDERR << 1); +const uint32_t sinsp_logger::OT_CALLBACK = (OT_FILE << 1); +const uint32_t sinsp_logger::OT_NOTS = (OT_CALLBACK << 1); +const uint32_t sinsp_logger::OT_ENCODE_SEV = (OT_NOTS << 1); + +sinsp_logger::sinsp_logger(): + m_file(nullptr), + m_callback(nullptr), + m_flags(OT_NONE), + m_sev(SEV_INFO) +{ } sinsp_logger::~sinsp_logger() { @@ -45,130 +60,249 @@ sinsp_logger::~sinsp_logger() } } -void sinsp_logger::set_log_output_type(sinsp_logger::output_type log_output_type) +bool sinsp_logger::is_callback() const { - if(log_output_type & (sinsp_logger::OT_STDOUT | sinsp_logger::OT_STDERR)) - { - m_flags = log_output_type; - } - else if(log_output_type == sinsp_logger::OT_STDERR) - { - add_file_log("sisnsp.log"); - } - else if(log_output_type == sinsp_logger::OT_NONE) - { - return; - } - else - { - ASSERT(false); - throw sinsp_exception("invalid log output type"); - } + return (m_flags & sinsp_logger::OT_CALLBACK) != 0; } -void sinsp_logger::add_stdout_log() +uint32_t sinsp_logger::get_log_output_type() const { - ASSERT((m_flags & sinsp_logger::OT_STDERR) == 0); + return m_flags; +} +void sinsp_logger::add_stdout_log() +{ m_flags |= sinsp_logger::OT_STDOUT; } void sinsp_logger::add_stderr_log() { - ASSERT((m_flags & sinsp_logger::OT_STDOUT) == 0); - m_flags |= sinsp_logger::OT_STDERR; } -void sinsp_logger::add_file_log(string filename) +void sinsp_logger::add_file_log(const std::string& filename) { - ASSERT(m_file == NULL); + ASSERT(m_file == nullptr); m_file = fopen(filename.c_str(), "w"); if(!m_file) { - throw sinsp_exception("unable to open file " + filename + " for wrirting"); + throw sinsp_exception("Unable to open file " + filename + " for writing"); } m_flags |= sinsp_logger::OT_FILE; } -void sinsp_logger::add_callback_log(sinsp_logger_callback callback) +void sinsp_logger::disable_timestamps() +{ + m_flags |= sinsp_logger::OT_NOTS; +} + +void sinsp_logger::add_encoded_severity() { - ASSERT(m_callback == NULL); - m_callback = callback; + m_flags |= sinsp_logger::OT_ENCODE_SEV; +} + +void sinsp_logger::add_callback_log(const sinsp_logger_callback callback) +{ + const sinsp_logger_callback old_cb = m_callback.exchange(callback); + + ASSERT(old_cb == nullptr); + + // For release builds, the compiler doesn't see that old_cb is used, + // so do something that will satisfy the compiler + static_cast(old_cb); m_flags |= sinsp_logger::OT_CALLBACK; } -void sinsp_logger::set_severity(severity sev) +void sinsp_logger::remove_callback_log() { - if(m_sev > SEV_MAX) + m_callback = nullptr; + m_flags &= ~sinsp_logger::OT_CALLBACK; +} + +void sinsp_logger::set_severity(const severity sev) +{ + if(m_sev < SEV_MIN || m_sev > SEV_MAX) { - throw sinsp_exception("invalid log severity"); + throw sinsp_exception("Invalid log severity"); } m_sev = sev; } -void sinsp_logger::log(string msg, severity sev) +sinsp_logger::severity sinsp_logger::get_severity() const +{ + return m_sev; +} + +void sinsp_logger::log(std::string msg, const severity sev) { - struct timeval ts; + sinsp_logger_callback cb = nullptr; - if(sev < m_sev) + if(sev > m_sev) { return; } if((m_flags & sinsp_logger::OT_NOTS) == 0) { - gettimeofday(&ts, NULL); - time_t rawtime = (time_t)ts.tv_sec; - struct tm* time_info = gmtime(&rawtime); - snprintf(m_tbuf, sizeof(m_tbuf), "%.2d-%.2d %.2d:%.2d:%.2d.%.6d %s", - time_info->tm_mon + 1, - time_info->tm_mday, - time_info->tm_hour, - time_info->tm_min, - time_info->tm_sec, - (int)ts.tv_usec, - msg.c_str()); + struct timeval ts = {}; + + if(gettimeofday(&ts, nullptr) == 0) + { + const std::string::size_type ts_length = sizeof("31-12 23:59:59.999999 "); + char ts_buf[ts_length]; + struct tm* ti; + struct tm time_info = {}; + +#ifdef _WIN32 + ti = _gmtime32((__time32_t*)&ts.tv_sec); +#else + gmtime_r(&ts.tv_sec, &time_info); + ti = &time_info; +#endif + + snprintf(ts_buf, + sizeof(ts_buf), + "%.2d-%.2d %.2d:%.2d:%.2d.%.6d ", + ti->tm_mon + 1, + ti->tm_mday, + ti->tm_hour, + ti->tm_min, + ti->tm_sec, + (int)ts.tv_usec); + + ts_buf[sizeof(ts_buf) - 1] = '\0'; + msg.insert(0, ts_buf); + } + } + + if(m_flags & sinsp_logger::OT_ENCODE_SEV) + { + char sev_buf[ENCODE_LEN + 1]; + strncpy(sev_buf, encode_severity(sev), sizeof(sev_buf)); + sev_buf[sizeof(sev_buf) - 1] = 0; + msg.insert(0, sev_buf); } - else + + if(is_callback()) { - snprintf(m_tbuf, sizeof(m_tbuf), "%s", msg.c_str()); + cb = m_callback; } - if(m_flags & sinsp_logger::OT_CALLBACK) + if(cb != nullptr) { - (*m_callback)(m_tbuf, (uint32_t)sev); + cb(std::move(msg), sev); } - else if(m_flags & sinsp_logger::OT_FILE) + else if((m_flags & sinsp_logger::OT_FILE) && m_file) { - fprintf(m_file, "%s\n", m_tbuf); + fprintf(m_file, "%s\n", msg.c_str()); fflush(m_file); } else if(m_flags & sinsp_logger::OT_STDOUT) { - fprintf(stdout, "%s\n", m_tbuf); + fprintf(stdout, "%s\n", msg.c_str()); fflush(stdout); } else if(m_flags & sinsp_logger::OT_STDERR) { - fprintf(stderr, "%s\n", m_tbuf); + fprintf(stderr, "%s\n", msg.c_str()); fflush(stderr); } } -char* sinsp_logger::format(severity sev, const char* fmt, ...) +void sinsp_logger::format(const severity sev, const char* const fmt, ...) +{ + if(sev > m_sev) + { + return; + } + + va_list ap; + + va_start(ap, fmt); + vsnprintf(s_tbuf, sizeof s_tbuf, fmt, ap); + va_end(ap); + + log(s_tbuf, sev); +} + +void sinsp_logger::format(const char* const fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vsnprintf(s_tbuf, sizeof s_tbuf, fmt, ap); + va_end(ap); + + log(s_tbuf, SEV_INFO); +} + +const char* sinsp_logger::format_and_return(const severity sev, const char* const fmt, ...) { va_list ap; va_start(ap, fmt); - vsnprintf(m_tbuf, sizeof(m_tbuf), fmt, ap); + vsnprintf(s_tbuf, sizeof s_tbuf, fmt, ap); va_end(ap); - log(m_tbuf, sev); + log(s_tbuf, SEV_INFO); + + return s_tbuf; +} + +namespace { +// All severity strings should be ENCODE_LEN chars long +const char* SEV_LEVELS[] = { + "SEV_DEF ", + "SEV_FAT ", + "SEV_CRI ", + "SEV_ERR ", + "SEV_WAR ", + "SEV_NOT ", + "SEV_INF ", + "SEV_DEB ", + "SEV_TRA " +}; + +static_assert(sizeof(SEV_LEVELS) == sizeof(*SEV_LEVELS) * ((size_t)(sinsp_logger::SEV_MAX) + 1), + "severity array must have SEV_MAX+1 elements"); +} + +const char* sinsp_logger::encode_severity(const sinsp_logger::severity sev) +{ + const char* ret; + auto sev_int = (size_t)sev; + if (sev_int > SEV_MAX) + { + sev_int = 0; + } + + ret = SEV_LEVELS[sev_int]; + assert(strlen(ret) == ENCODE_LEN); + return ret; +} + +size_t sinsp_logger::decode_severity(const std::string &str, severity& sev) +{ + if(str.length() < ENCODE_LEN) + { + return 0; + } + + const char* msg = str.c_str(); + + // we don't really expect "SEV_DEF " messages so skip severity 0 + for(size_t i = SEV_MIN; i <= SEV_MAX; ++i) + { + if(!strncmp(msg, SEV_LEVELS[i], ENCODE_LEN)) + { + sev = static_cast(i); + return ENCODE_LEN; + } + } - return m_tbuf; + return 0; } diff --git a/userspace/libsinsp/logger.h b/userspace/libsinsp/logger.h index 029b8cc1bf..32d3a5fad8 100644 --- a/userspace/libsinsp/logger.h +++ b/userspace/libsinsp/logger.h @@ -1,71 +1,248 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2019 Sysdig, Inc. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once -/////////////////////////////////////////////////////////////////////////////// -// The logger class -/////////////////////////////////////////////////////////////////////////////// -typedef void (*sinsp_logger_callback)(char* str, uint32_t sev); +#include "sinsp_public.h" + +#include +#include +/** + * Sysdig component logging API. This API exposes the ability to log to a + * variety of log sinks. sinsp_logger will use only one enabled log* sink; + * if multiple are enabled, then it will use the first available one it + * finds. The order in which log sinks is considered is: (1) a registered + * callback function, (2) a registered file, (3) standard output, and + * (4) standard error. + */ class SINSP_PUBLIC sinsp_logger { public: enum severity { - SEV_DEBUG = 0, - SEV_INFO = 1, - SEV_WARNING = 2, + SEV_FATAL = 1, + SEV_CRITICAL = 2, SEV_ERROR = 3, - SEV_CRITICAL = 4, - SEV_MAX = SEV_CRITICAL, + SEV_WARNING = 4, + SEV_NOTICE = 5, + SEV_INFO = 6, + SEV_DEBUG = 7, + SEV_TRACE = 8, }; + const static severity SEV_MIN = SEV_FATAL; + const static severity SEV_MAX = SEV_TRACE; - enum output_type - { - OT_NONE = 0, - OT_STDOUT = 1, - OT_STDERR = 2, - OT_FILE = 4, - OT_CALLBACK = 8, - OT_NOTS = 256, - }; + using callback_t = void (*)(std::string&& str, severity sev); + const static uint32_t OT_NONE; + const static uint32_t OT_STDOUT; + const static uint32_t OT_STDERR; + const static uint32_t OT_FILE; + const static uint32_t OT_CALLBACK; + const static uint32_t OT_NOTS; + const static uint32_t OT_ENCODE_SEV; + + /** + * Initialize this sinsp_logger with no output sinks enabled. + */ sinsp_logger(); ~sinsp_logger(); - void set_log_output_type(sinsp_logger::output_type log_output_type); + /** + * Get the currently configured output type, which includes the + * configured output sinks as well as whether timestamps are enabled + * or not. + */ + uint32_t get_log_output_type() const; + + /** Enable the standard output log sink. */ void add_stdout_log(); + + /** Enable the standard error log sink. */ void add_stderr_log(); - void add_file_log(string filename); - void add_file_log(FILE* f); - void add_callback_log(sinsp_logger_callback callback); + /** + * Enable the file log sink. + * + * @param[in] filename The filename to which sinsp_logger should write + * logs. + */ + void add_file_log(const std::string& filename); + + /** Disables tagging logs with the current timestamp. */ + void disable_timestamps(); + + /** Adds encoded severity to log messages */ + void add_encoded_severity(); + + /** + * Registered the given callback as the logging callback. + * + * Note: the given callback must be thread-safe. + */ + void add_callback_log(callback_t callback); + + /** Deregister any registered logging callbacks. */ + void remove_callback_log(); + + /** + * Set the minimum severity of logs that this sinsp_logger will emit. + */ void set_severity(severity sev); - void log(string msg, severity sev=SEV_INFO); - // Log function that accepts printf syntax and returns the formatted buffer. - char* format(severity sev, const char* fmt, ...); + /** + * Returns the minimum severity of logs that this sinsp_logger + * will emit. + */ + severity get_severity() const; + + /** + * Returns true if logs generated at the given severity will be written + * to the logging sink, false otherwise. + * + * Note that this is intentionally inline. + */ + bool is_enabled(const severity sev) const { return (sev <= m_sev); } + + /** + * Emit the given msg to the configured log sink if the given sev + * is greater than or equal to the minimum configured logging severity. + */ + void log(std::string msg, severity sev = SEV_INFO); + + /** + * Write the given printf-style log message of the given severity + * with the given format to the configured log sink. + */ + void format(severity sev, const char* fmt, ...); + + /** + * Write the given printf-style log message of the given severity + * with the given format to the configured log sink. + * + * @returns a pointer to static thread-local storage containing the + * formatted log message. + */ + const char* format_and_return(severity sev, const char* fmt, ...); + + /** + * Write the given printf-style log message of SEV_INFO severity + * with the given format to the configured log sink. + */ + void format(const char* fmt, ...); + + /** Sets `sev` to the decoded severity or SEV_MAX+1 for errors. + * Returns the length of the severity string on success + * and 0 in case of errors + */ + static size_t decode_severity(const std::string &s, severity& sev); private: - FILE* m_file; - sinsp_logger_callback m_callback; - uint32_t m_flags; - severity m_sev; - char m_tbuf[512]; + /** Returns true if the callback log sync is enabled, false otherwise. */ + bool is_callback() const; + + + /** Returns a string containing encoded severity, for OT_ENCODE_SEV. */ + static const char* encode_severity(severity sev); + std::atomic m_file; + std::atomic m_callback; + std::atomic m_flags; + std::atomic m_sev; }; + +using sinsp_logger_callback = sinsp_logger::callback_t; + +extern sinsp_logger g_logger; + +#define SINSP_LOG_(severity, fmt, ...) \ + do \ + { \ + if(g_logger.is_enabled(severity)) \ + { \ + g_logger.format((severity), ("" fmt), ##__VA_ARGS__); \ + } \ + } \ + while(false) + +#define SINSP_LOG_STR_(severity, msg) \ + do \ + { \ + if(g_logger.is_enabled(severity)) \ + { \ + g_logger.log((msg), (severity)); \ + } \ + } \ + while(false) + +#define SINSP_FATAL(...) SINSP_LOG_(sinsp_logger::SEV_FATAL, ##__VA_ARGS__) +#define SINSP_CRITICAL(...) SINSP_LOG_(sinsp_logger::SEV_CRITICAL, ##__VA_ARGS__) +#define SINSP_ERROR(...) SINSP_LOG_(sinsp_logger::SEV_ERROR, ##__VA_ARGS__) +#define SINSP_WARNING(...) SINSP_LOG_(sinsp_logger::SEV_WARNING, ##__VA_ARGS__) +#define SINSP_NOTICE(...) SINSP_LOG_(sinsp_logger::SEV_NOTICE, ##__VA_ARGS__) +#define SINSP_INFO(...) SINSP_LOG_(sinsp_logger::SEV_INFO, ##__VA_ARGS__) +#define SINSP_DEBUG(...) SINSP_LOG_(sinsp_logger::SEV_DEBUG, ##__VA_ARGS__) +#define SINSP_TRACE(...) SINSP_LOG_(sinsp_logger::SEV_TRACE, ##__VA_ARGS__) + +#define SINSP_STR_FATAL(str) SINSP_LOG_STR_(sinsp_logger::SEV_FATAL, (str)) +#define SINSP_STR_CRITICAL(str) SINSP_LOG_STR_(sinsp_logger::SEV_CRITICAL,(str)) +#define SINSP_STR_ERROR(str) SINSP_LOG_STR_(sinsp_logger::SEV_ERROR, (str)) +#define SINSP_STR_WARNING(str) SINSP_LOG_STR_(sinsp_logger::SEV_WARNING, (str)) +#define SINSP_STR_NOTICE(str) SINSP_LOG_STR_(sinsp_logger::SEV_NOTICE, (str)) +#define SINSP_STR_INFO(str) SINSP_LOG_STR_(sinsp_logger::SEV_INFO, (str)) +#define SINSP_STR_DEBUG(str) SINSP_LOG_STR_(sinsp_logger::SEV_DEBUG, (str)) +#define SINSP_STR_TRACE(str) SINSP_LOG_STR_(sinsp_logger::SEV_TRACE, (str)) + +#if _DEBUG +# define DBG_SINSP_FATAL(...) SINSP_FATAL( __VA_ARGS__) +# define DBG_SINSP_CRITICAL(...) SINSP_CRITICAL(__VA_ARGS__) +# define DBG_SINSP_ERROR(...) SINSP_ERROR( __VA_ARGS__) +# define DBG_SINSP_WARNING(...) SINSP_WARNING( __VA_ARGS__) +# define DBG_SINSP_NOTICE(...) SINSP_NOTICE( __VA_ARGS__) +# define DBG_SINSP_INFO(...) SINSP_INFO( __VA_ARGS__) +# define DBG_SINSP_DEBUG(...) SINSP_DEBUG( __VA_ARGS__) +# define DBG_SINSP_TRACE(...) SINSP_TRACE( __VA_ARGS__) + +# define DBG_SINSP_STR_FATAL(str) SINSP_STR_FATAL(str) +# define DBG_SINSP_STR_CRITICAL(str) SINSP_STR_CRITICAL(str) +# define DBG_SINSP_STR_ERROR(str) SINSP_STR_ERROR(str) +# define DBG_SINSP_STR_WARNING(str) SINSP_STR_WARNING(str) +# define DBG_SINSP_STR_NOTICE(str) SINSP_STR_NOTICE(str) +# define DBG_SINSP_STR_INFO(str) SINSP_STR_INFO(str) +# define DBG_SINSP_STR_DEBUG(str) SINSP_STR_DEBUG(str) +# define DBG_SINSP_STR_TRACE(str) SINSP_STR_TRACE(str) +#else +# define DBG_SINSP_FATAL(fmt, ...) +# define DBG_SINSP_CRITICAL(fmt, ...) +# define DBG_SINSP_ERROR(fmt, ...) +# define DBG_SINSP_WARNING(fmt, ...) +# define DBG_SINSP_NOTICE(fmt, ...) +# define DBG_SINSP_INFO(fmt, ...) +# define DBG_SINSP_DEBUG(fmt, ...) +# define DBG_SINSP_TRACE(fmt, ...) + +# define DBG_SINSP_STR_FATAL(str) +# define DBG_SINSP_STR_CRITICAL(str) +# define DBG_SINSP_STR_ERROR(str) +# define DBG_SINSP_STR_WARNING(str) +# define DBG_SINSP_STR_NOTICE(str) +# define DBG_SINSP_STR_INFO(str) +# define DBG_SINSP_STR_DEBUG(str) +# define DBG_SINSP_STR_TRACE(str) +#endif diff --git a/userspace/libsinsp/lua_parser.cpp b/userspace/libsinsp/lua_parser.cpp new file mode 100644 index 0000000000..0e26617d86 --- /dev/null +++ b/userspace/libsinsp/lua_parser.cpp @@ -0,0 +1,90 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include +#include "sinsp.h" +#include "filter.h" +#include "sinsp_int.h" + +#include "lua_parser.h" +#include "lua_parser_api.h" + + +extern "C" { +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +} + +const static struct luaL_reg ll_filter [] = +{ + {"rel_expr", &lua_parser_cbacks::rel_expr}, + {"bool_op", &lua_parser_cbacks::bool_op}, + {"nest", &lua_parser_cbacks::nest}, + {"unnest", &lua_parser_cbacks::unnest}, + {NULL,NULL} +}; + +lua_parser::lua_parser(gen_event_filter_factory &factory, lua_State *ls, const char *lua_library_name) + : m_factory(factory) +{ + m_filter = NULL; + + m_ls = ls; + reset(); + + // Register our c++ defined functions + luaL_openlib(m_ls, lua_library_name, ll_filter, 0); +} + +void lua_parser::reset() +{ + m_have_rel_expr = false; + m_last_boolop = BO_NONE; + m_nest_level = 0; + + m_filter = m_factory.new_filter(); +} + +gen_event_filter* lua_parser::get_filter(bool reset_filter) +{ + if (m_nest_level != 0) + { + throw sinsp_exception("Error in configured filter: unbalanced nesting"); + } + + gen_event_filter *ret = m_filter; + + if (reset_filter) + { + reset(); + } + + return ret; +} +lua_parser::~lua_parser() +{ + // The lua state is not considered owned by this object, so + // not freeing it. + + delete m_filter; + m_filter = NULL; +} + + diff --git a/userspace/libsinsp/lua_parser.h b/userspace/libsinsp/lua_parser.h new file mode 100644 index 0000000000..9123cf1b84 --- /dev/null +++ b/userspace/libsinsp/lua_parser.h @@ -0,0 +1,48 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include "lua_parser_api.h" +#include "gen_filter.h" + +typedef struct lua_State lua_State; + +class lua_parser +{ +public: + lua_parser(gen_event_filter_factory &factory, lua_State *ls, const char *lua_library_name); + virtual ~lua_parser(); + gen_event_filter* get_filter(bool reset_filter = false); + + private: + + void reset(); + gen_event_filter_factory &m_factory; + + gen_event_filter* m_filter; + + boolop m_last_boolop; + bool m_have_rel_expr; + int32_t m_nest_level; + + lua_State* m_ls; + + friend class lua_parser_cbacks; +}; + diff --git a/userspace/libsinsp/lua_parser_api.cpp b/userspace/libsinsp/lua_parser_api.cpp new file mode 100644 index 0000000000..c89e912641 --- /dev/null +++ b/userspace/libsinsp/lua_parser_api.cpp @@ -0,0 +1,313 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "sinsp.h" +#include "sinsp_int.h" + +#include "filterchecks.h" +#include "lua_parser_api.h" +#include "lua_parser.h" + +extern "C" { +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +} + +extern sinsp_filter_check_list g_filterlist; + +// It would be nice to expose this up to Lua so that comparison operator +// parsing/encoding can be done there. +static cmpop string_to_cmpop(const char* str) +{ + if(strcmp(str, "=") == 0) + { + return CO_EQ; + } + else if(strcmp(str, "!=") == 0) + { + return CO_NE; + } + else if(strcmp(str, "<=") == 0) + { + return CO_LE; + } + else if(strcmp(str, "<") == 0) + { + return CO_LT; + } + else if(strcmp(str, ">=") == 0) + { + return CO_GE; + } + else if(strcmp(str, ">") == 0) + { + return CO_GT; + } + else if(strcmp(str, "contains") == 0) + { + return CO_CONTAINS; + } + else if(strcmp(str, "icontains") == 0) + { + return CO_ICONTAINS; + } + else if(strcmp(str, "startswith") == 0) + { + return CO_STARTSWITH; + } + else if(strcmp(str, "endswith") == 0) + { + return CO_ENDSWITH; + } + else if(strcmp(str, "in") == 0) + { + return CO_IN; + } + else if(strcmp(str, "intersects") == 0) + { + return CO_INTERSECTS; + } + else if(strcmp(str, "pmatch") == 0) + { + return CO_PMATCH; + } + else if(strcmp(str, "exists") == 0) + { + return CO_EXISTS; + } + else if(strcmp(str, "glob") == 0) + { + return CO_GLOB; + } + else + { + throw sinsp_exception("filter error: invalid comparison operator: " + string(str)); + } +} + +boolop string_to_boolop(const char* str) +{ + if(strcmp(str, "or") == 0) + { + return BO_OR; + } + else if(strcmp(str, "and") == 0) + { + return BO_AND; + } + else if(strcmp(str, "not") == 0) + { + return BO_NOT; + } + else + { + throw sinsp_exception("filter error: invalid boolean operator: " + string(str)); + } +} + +int lua_parser_cbacks::nest(lua_State *ls) +{ + lua_parser* parser = (lua_parser*)lua_topointer(ls, -1); + + try { + + if (parser->m_have_rel_expr && parser->m_last_boolop == BO_NONE) + { + string err = "filter.nest() called without a preceding call to filter.bool_op()"; + throw sinsp_exception(err); + } + + gen_event_filter* filter = parser->m_filter; + + filter->push_expression(parser->m_last_boolop); + parser->m_nest_level++; + + parser->m_last_boolop = BO_NONE; + parser->m_have_rel_expr = false; + } + catch (const std::exception& e) + { + lua_pushstring(ls, e.what()); + lua_error(ls); + } + + return 0; +} + +int lua_parser_cbacks::unnest(lua_State *ls) +{ + lua_parser* parser = (lua_parser*)lua_topointer(ls, -1); + + try { + + if (parser->m_nest_level < 1) + { + string err = "filter.unnest() called without being nested"; + throw sinsp_exception(err); + } + + gen_event_filter* filter = parser->m_filter; + + filter->pop_expression(); + parser->m_nest_level--; + } + catch (const std::exception& e) + { + lua_pushstring(ls, e.what()); + lua_error(ls); + } + + return 0; +} + +int lua_parser_cbacks::bool_op(lua_State *ls) +{ + lua_parser* parser = (lua_parser*)lua_topointer(ls, -2); + + try { + + const char* opstr = luaL_checkstring(ls, -1); + boolop op = string_to_boolop(opstr); + + if (!parser->m_have_rel_expr) + { + if (op == BO_NOT) { + op = (boolop)((uint32_t)parser->m_last_boolop | op); + } + else + { + string err = "filter.bool_op() called without having called rel_expr() "; + throw sinsp_exception(err); + } + } + + if (parser->m_last_boolop != BO_NONE) + { + if (op == BO_NOT) { + op = (boolop)((uint32_t)parser->m_last_boolop | op); + } + else + { + string err = "filter.bool_op() called twice in a row"; + throw sinsp_exception(err); + } + } + parser->m_last_boolop = op; + + } + catch (const std::exception& e) + { + lua_pushstring(ls, e.what()); + lua_error(ls); + } + return 0; + +} + +int lua_parser_cbacks::rel_expr(lua_State *ls) +{ + lua_parser* parser = (lua_parser*)lua_topointer(ls, 1); + + try { + + if (parser->m_have_rel_expr && parser->m_last_boolop == BO_NONE) + { + string err = "filter.rel_expr() called twice in a row"; + throw sinsp_exception(err); + } + + parser->m_have_rel_expr = true; + gen_event_filter* filter = parser->m_filter; + + const char* fld = luaL_checkstring(ls, 2); + gen_event_filter_check *chk = parser->m_factory.new_filtercheck(fld); + if(chk == NULL) + { + string err = "filter_check called with nonexistent field " + string(fld); + throw sinsp_exception("parser API error"); + } + + int i; + int rule_index = 0; + + chk->m_boolop = parser->m_last_boolop; + parser->m_last_boolop = BO_NONE; + + chk->parse_field_name(fld, true, true); + + const char* cmpop = luaL_checkstring(ls, 3); + chk->m_cmpop = string_to_cmpop(cmpop); + + // "exists" is the only unary comparison op + if(strcmp(cmpop, "exists")) + { + if (strcmp(cmpop, "in") == 0 || + strcmp(cmpop, "intersects") == 0 || + strcmp(cmpop, "pmatch") == 0) + { + if (!lua_istable(ls, 4)) + { + string err = "Got non-table as in-expression operand\n"; + throw sinsp_exception("parser API error"); + } + int n = luaL_getn(ls, 4); /* get size of table */ + for (i=1; i<=n; i++) + { + lua_rawgeti(ls, 4, i); + const char* value = luaL_checkstring(ls, 6); + chk->add_filter_value(value, strlen(value), i - 1); + lua_pop(ls, 1); + } + } + else + { + const char* value = luaL_checkstring(ls, 4); + chk->add_filter_value(value, strlen(value)); + } + + if (lua_isnumber(ls, 5)) + { + rule_index = (int) luaL_checkinteger(ls, 5); + } + } + else + { + if (lua_isnumber(ls, 4)) + { + rule_index = (int) luaL_checkinteger(ls, 4); + } + } + + if (rule_index) + { + chk->set_check_id(rule_index); + } + + filter->add_check(chk); + + } + catch (const std::exception& e) + { + lua_pushstring(ls, e.what()); + lua_error(ls); + } + + return 0; +} + diff --git a/userspace/libsinsp/lua_parser_api.h b/userspace/libsinsp/lua_parser_api.h new file mode 100644 index 0000000000..e799789d1b --- /dev/null +++ b/userspace/libsinsp/lua_parser_api.h @@ -0,0 +1,85 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +typedef struct lua_State lua_State; + +class lua_parser_filtercheck +{ +public: + + lua_parser_filtercheck() {}; + virtual ~lua_parser_filtercheck() {}; + + boolop m_boolop; + cmpop m_cmpop; + + virtual int32_t parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering) = 0; + + virtual void add_filter_value(const char* str, uint32_t len, uint32_t i = 0 ) = 0; + + virtual void set_check_id(int32_t id) = 0; +}; + +class lua_parser_filter +{ +public: + lua_parser_filter() {}; + virtual ~lua_parser_filter() {}; + + virtual void push_expression(boolop op) = 0; + virtual void pop_expression() = 0; + virtual void add_check(lua_parser_filtercheck* chk) = 0; +}; + +class lua_filter_factory +{ +public: + + lua_filter_factory() {}; + virtual ~lua_filter_factory() {}; + + // Create a new filter + virtual lua_parser_filter *new_filter() = 0; + + // Create a new filterchekc + virtual lua_parser_filtercheck *new_filtercheck(const char *fldname) = 0; +}; + +class lua_parser_cbacks +{ +public: + // filter.rel_expr(field_name, cmpop, value, index) + // field_name and cmpop are mandatory + // value is mandatory unless cmpop=="exists" + // index is an optional index (integer) that will be written + // into events matching this expression (internal use). + static int rel_expr(lua_State *ls); + + // filter.bool_op(op) + static int bool_op(lua_State *ls); + + // filter.nest() + static int nest(lua_State *ls); + + // filter.unnest() + static int unnest(lua_State *ls); +}; + diff --git a/userspace/libsinsp/marathon_component.cpp b/userspace/libsinsp/marathon_component.cpp new file mode 100644 index 0000000000..751b96a44f --- /dev/null +++ b/userspace/libsinsp/marathon_component.cpp @@ -0,0 +1,385 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// marathon_component.cpp +// + +#include "marathon_component.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include + +// +// component +// + +marathon_component::task_app_map_t marathon_component::m_task_app_cache; + +const marathon_component::component_map marathon_component::list = +{ + { marathon_component::MARATHON_GROUP, "group" }, + { marathon_component::MARATHON_APP, "app" } +}; + +marathon_component::marathon_component(type t, const std::string& id) : + m_type(t), + m_id(id) +{ + component_map::const_iterator it = list.find(t); + if(it == list.end()) + { + throw sinsp_exception("Invalid Marathon component type: " + std::to_string(t)); + } + + if(m_id.empty()) + { + throw sinsp_exception("Marathon " + it->second + " ID cannot be empty"); + } +} + +marathon_component::marathon_component(const marathon_component& other): m_type(other.m_type), + m_id(other.m_id) +{ +} + +marathon_component::marathon_component(marathon_component&& other): m_type(other.m_type), + m_id(std::move(other.m_id)) +{ +} + +marathon_component& marathon_component::operator=(const marathon_component& other) +{ + m_type = other.m_type; + m_id = other.m_id; + return *this; +} + +marathon_component& marathon_component::operator=(const marathon_component&& other) +{ + m_type = other.m_type; + m_id = std::move(other.m_id); + return *this; +} + +std::string marathon_component::get_type_name(type t) +{ + component_map::const_iterator it = list.find(t); + if(it != list.end()) + { + return it->second; + } + + std::ostringstream os; + os << "Unknown component type " << static_cast(t); + throw sinsp_exception(os.str().c_str()); +} + +marathon_component::type marathon_component::get_type(const std::string& name) +{ + if(name == "group") + { + return MARATHON_GROUP; + } + else if(name == "app") + { + return MARATHON_APP; + } + + std::ostringstream os; + os << "Unknown component name " << name; + throw sinsp_exception(os.str().c_str()); +} + +void marathon_component::cache_task_app(const std::string& task_id, app_ptr_t app) +{ + m_task_app_cache[task_id] = app; +} + +void marathon_component::uncache_task_app(const std::string& task_id) +{ + m_task_app_cache.erase(task_id); +} + +marathon_component::app_ptr_t marathon_component::get_cached_app(const std::string& task_id) +{ + task_app_map_t::iterator it = m_task_app_cache.find(task_id); + if(it != m_task_app_cache.end()) + { + return it->second; + } + return 0; +} + +// +// app +// + +marathon_app::marathon_app(const std::string& id) : + marathon_component(marathon_component::MARATHON_APP, id) +{ +} + +marathon_app::~marathon_app() +{ +} + +void marathon_app::add_task(mesos_framework::task_ptr_t ptask) +{ + if(ptask) + { + const std::string& task_id = ptask->get_uid(); + ptask->set_marathon_app_id(get_id()); + for(auto& task : m_tasks) + { + if(task == task_id) { return; } + } + m_tasks.push_back(task_id); + cache_task_app(task_id, shared_from_this()); + } + else + { + g_logger.log("Attempt to add null task to app [" + get_id() + ']', sinsp_logger::SEV_WARNING); + } +} + +bool marathon_app::remove_task(const std::string& task_id) +{ + for(auto it = m_tasks.begin(); it != m_tasks.end(); ++it) + { + if(task_id == *it) + { + m_tasks.erase(it); + uncache_task_app(task_id); + return true; + } + } + g_logger.log("Task [" + task_id + "] not found in app [" + get_id() + ']', sinsp_logger::SEV_WARNING); + return false; +} + +bool marathon_app::has_task(const std::string& task_id) +{ + for(auto it = m_tasks.begin(); it != m_tasks.end(); ++it) + { + if(task_id == *it) + { + return true; + } + } + return false; +} + +std::string marathon_app::get_group_id() const +{ + return get_group_id(get_id()); +} + +std::string marathon_app::get_group_id(const std::string& app_id) +{ + std::string group_id; + std::string::size_type pos = app_id.rfind('/'); + if(pos != std::string::npos && app_id.length() > pos) + { + pos += (pos == 0) ? 1 : 0; + group_id = app_id.substr(0, pos); + } + return group_id; +} + +void marathon_app::set_labels(const Json::Value& labels) +{ + m_labels.clear(); + Json::Value::Members members = labels.getMemberNames(); + for(const auto& member : members) + { + if(labels[member].isConvertibleTo(Json::ValueType::stringValue)) + { + m_labels.push_back({member, labels[member].asString()}); + } + } +} + +mesos_pair_t marathon_app::get_label(const std::string& key) const +{ + for(const auto& label : m_labels) + { + if(label.first == key) + { + return label; + } + } + return mesos_pair_t(); +} + +// +// group +// + +marathon_group::marathon_group(const std::string& id, const std::string& framework_id) : + marathon_component(marathon_component::MARATHON_GROUP, id), + m_framework_id(framework_id) +{ +} + +marathon_group::app_ptr_t marathon_group::get_app(const std::string& id) +{ + for(const auto& app : m_apps) + { + if(app.second && app.second->get_id() == id) + { + return app.second; + } + } + return 0; +} + +marathon_group::ptr_t marathon_group::get_group(const std::string& group_id) +{ + if(group_id == get_id()) + { + return shared_from_this(); + } + + marathon_groups::iterator it = m_groups.find(group_id); + if(it != m_groups.end()) + { + return it->second; + } + else + { + for(auto group : m_groups) + { + if(ptr_t p_group = group.second->get_group(group_id)) + { + return p_group; + } + } + } + return 0; +} + +bool marathon_group::remove(const std::string& id) +{ + if(id == get_id()) + { + throw sinsp_exception("Invalid access - group can not remove itself."); + } + + if(ptr_t group = get_parent(id)) + { + return group->remove_group(id); + } + + return false; +} + +marathon_group::ptr_t marathon_group::get_parent(const std::string& id) +{ + marathon_groups::iterator it = m_groups.find(id); + if(it != m_groups.end()) + { + return shared_from_this(); + } + else + { + for(auto group : m_groups) + { + if(group.second->get_group(id)) + { + return group.second; + } + } + } + return 0; +} + +bool marathon_group::remove_group(const std::string& id) +{ + marathon_groups::iterator it = m_groups.find(id); + if(it != m_groups.end()) + { + m_groups.erase(it); + return true; + } + return false; +} + +bool marathon_group::remove_app(const std::string& id) +{ + auto it = m_apps.find(id); + if(it != m_apps.end()) + { + m_apps.erase(it); + return true; + } + return false; +} + +bool marathon_group::remove_task(const std::string& id) +{ + for(auto& app : m_apps) + { + if(app.second && app.second->remove_task(id)) + { + return true; + } + } + return false; +} + +marathon_group::app_ptr_t marathon_group::get_app(mesos_task::ptr_t task) const +{ + return get_cached_app(task->get_uid()); +} + +marathon_group::ptr_t marathon_group::get_group(mesos_task::ptr_t task) +{ + for(const auto& app : m_apps) + { + if(app.second && app.second->has_task(task->get_uid())) + { + return shared_from_this(); + } + } + for(const auto& group : m_groups) + { + app_ptr_t app = group.second->get_app(task); + if(app) + { + return group.second; + } + } + return 0; +} + +void marathon_group::print(int indent) const +{ + for(int j = 0; j < indent; ++j) + { + std::cout << " "; + } + std::cout << get_id() << std::endl; + for(auto& group : m_groups) + { + group.second->print(indent+2); + } +} + + diff --git a/userspace/libsinsp/marathon_component.h b/userspace/libsinsp/marathon_component.h new file mode 100644 index 0000000000..1cc81dc7f3 --- /dev/null +++ b/userspace/libsinsp/marathon_component.h @@ -0,0 +1,261 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// marathon_component.h +// +// marathon components (groups, apps, tasks) +// abstraction +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "mesos_component.h" +#include +#include +#include +#include +#include + +typedef std::pair marathon_pair_t; +typedef std::vector marathon_pair_list; + +class marathon_app; + +// +// component +// + +class marathon_component +{ +public: + enum type + { + MARATHON_GROUP, + MARATHON_APP + }; + + typedef std::pair component_pair; + typedef std::map component_map; + static const component_map list; + + marathon_component() = delete; + + marathon_component(type t, const std::string& id); + + marathon_component(const marathon_component& other); + + marathon_component(marathon_component&& other); + + marathon_component& operator=(const marathon_component& other); + + marathon_component& operator=(const marathon_component&& other); + + const std::string& get_id() const; + + void set_id(const std::string& id); + + const std::string& get_name() const; + + void set_name(const std::string& name); + + static std::string get_type_name(type t); + + static type get_type(const std::string& name); + +protected: + typedef std::shared_ptr app_ptr_t; + typedef std::map task_app_map_t; + static void cache_task_app(const std::string& task_id, app_ptr_t app); + static void uncache_task_app(const std::string& task_id); + static app_ptr_t get_cached_app(const std::string& task_id); + +private: + type m_type; + std::string m_id; + + static task_app_map_t m_task_app_cache; +}; + +// +// group +// + +class marathon_group : public marathon_component, public std::enable_shared_from_this +{ +public: + typedef std::shared_ptr ptr_t; + typedef std::shared_ptr app_ptr_t; + + typedef std::unordered_map> app_map_t; + typedef std::vector> app_list_t; + typedef std::map> group_map_t; + + marathon_group(const std::string& id, const std::string& framework_id); + + app_ptr_t get_app(const std::string& id); + app_ptr_t get_app(mesos_task::ptr_t task) const; + + void add_or_replace_app(std::shared_ptr); + bool remove_app(const std::string& id); + bool remove_task(const std::string& id); + + void add_or_replace_group(std::shared_ptr); + + const app_map_t& get_apps() const; + const group_map_t& get_groups() const; + ptr_t get_group(const std::string& group_id); + ptr_t get_group(mesos_task::ptr_t task); + + bool remove(const std::string& id); + + void print(int indent = 0) const; + + const std::string& get_framework_id() const; + void set_framework_id(const std::string& id); + +private: + template + static void add_or_replace_component(M& component_map, P comp) + { + typename M::value_type val = {comp->get_id(), comp}; + std::pair ret = component_map.insert(val); + if (!ret.second) ret.first->second = comp; + } + + bool remove_group(const std::string& id); + + ptr_t get_parent(const std::string& id); + + app_map_t m_apps; + group_map_t m_groups; + std::string m_framework_id; +}; + +// +// app +// + +class marathon_app : public marathon_component, public std::enable_shared_from_this +{ +public: + typedef std::shared_ptr ptr_t; + typedef std::vector task_list_t; + + marathon_app(const std::string& uid); + ~marathon_app(); + + void add_task(mesos_framework::task_ptr_t ptask); + bool remove_task(const std::string& task_id); + const task_list_t& get_tasks() const; + bool has_task(const std::string& task_id); + + std::string get_group_id() const; + static std::string get_group_id(const std::string& app_id); + + void set_labels(const Json::Value& labels); + const mesos_pair_list& get_labels() const; + mesos_pair_t get_label(const std::string& key) const; + +private: + task_list_t m_tasks; + mesos_pair_list m_labels; + friend class mesos; +}; + + +typedef marathon_group::app_map_t marathon_apps; +typedef marathon_group::group_map_t marathon_groups; + +// +// component +// + +inline const std::string& marathon_component::get_id() const +{ + return m_id; +} + +inline void marathon_component::set_id(const std::string& id) +{ + m_id = id; +} + +inline const std::string& marathon_component::get_name() const +{ + return m_id; +} + +inline void marathon_component::set_name(const std::string& name) +{ + m_id = name; +} + + +// +// group +// + +inline const marathon_group::app_map_t& marathon_group::get_apps() const +{ + return m_apps; +} + +inline const marathon_group::group_map_t& marathon_group::get_groups() const +{ + return m_groups; +} + +inline void marathon_group::add_or_replace_group(std::shared_ptr group) +{ + add_or_replace_component(m_groups, group); +} + +inline void marathon_group::add_or_replace_app(std::shared_ptr app) +{ + add_or_replace_component(m_apps, app); +} + +inline const std::string& marathon_group::get_framework_id() const +{ + return m_framework_id; +} + +inline void marathon_group::set_framework_id(const std::string& id) +{ + m_framework_id = id; +} + +// +// app +// + +inline const marathon_app::task_list_t& marathon_app::get_tasks() const +{ + return m_tasks; +} + +inline const mesos_pair_list& marathon_app::get_labels() const +{ + return m_labels; +} + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/marathon_http.cpp b/userspace/libsinsp/marathon_http.cpp new file mode 100644 index 0000000000..0ae7d762f5 --- /dev/null +++ b/userspace/libsinsp/marathon_http.cpp @@ -0,0 +1,125 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// marathon_http.cpp +// +#ifndef CYGWING_AGENT + +#ifdef HAS_CAPTURE + +#include "marathon_http.h" +#include "curl/curl.h" +#include "curl/easy.h" +#define BUFFERSIZE 512 // b64 needs this macro +#include "b64/encode.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "json_error_log.h" +#include "mesos.h" +#include +#include +#include +#include + +marathon_http::marathon_http(mesos& m, const uri& url, bool discover_marathon, int timeout_ms, const string& token): + mesos_http(m, url, false, discover_marathon, timeout_ms, token) +{ + g_logger.log("Creating Marathon HTTP object for [" + url.to_string(false) + "] ...", sinsp_logger::SEV_DEBUG); + if(refresh_data()) + { + g_logger.log("Created Marathon HTTP connection (" + url.to_string(false) + ") for framework [" + + get_framework_name() + "] (" + get_framework_id() + "), version: " + get_framework_version(), + sinsp_logger::SEV_INFO); + } + else + { + throw sinsp_exception("Could not obtain Mesos Marathon framework information."); + } + + g_logger.log("Marathon request [" + get_request() + ']', sinsp_logger::SEV_DEBUG); +} + +marathon_http::~marathon_http() +{ +} + +bool marathon_http::refresh_data() +{ + std::ostringstream os; + std::string uri = make_uri("/v2/info"); + CURLcode res = get_data(uri, os); + + if(res != CURLE_OK) + { + std::string errstr = std::string("Problem accessing /v2/info: ") + curl_easy_strerror(res); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri); + return false; + } + + try + { + Json::Value root; + Json::Reader reader; + if(reader.parse(os.str(), root, false)) + { + set_framework_id(get_json_string(root, "frameworkId")); + set_framework_name(get_json_string(root, "name")); + set_framework_version(get_json_string(root, "version")); + g_logger.log("Found Marathon framework: " + get_framework_name() + " (" + get_framework_id() + "), version: " + get_framework_version(), sinsp_logger::SEV_DEBUG); + } + else + { + std::string errstr; + errstr = reader.getFormattedErrorMessages(); + g_logger.log("Error parsing framework info (" + errstr + ").\nJSON:\n---\n" + os.str() + "\n---", sinsp_logger::SEV_ERROR); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri); + return false; + } + } + catch(const std::exception& ex) + { + std::string errstr = std::string("Error parsing framework info:") + ex.what(); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri); + return false; + } + + return true; +} + +std::string marathon_http::get_groups(const std::string& group_id) +{ + std::ostringstream os; + std::string uri = make_uri("/v2/groups" + group_id); + CURLcode res = get_data(uri, os); + + if(res != CURLE_OK) + { + std::string errstr = std::string("Problem accessing /v2/groups: ") + curl_easy_strerror(res); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri); + return ""; + } + + return os.str(); +} + +#endif // HAS_CAPTURE +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/marathon_http.h b/userspace/libsinsp/marathon_http.h new file mode 100644 index 0000000000..ec10bb9e8f --- /dev/null +++ b/userspace/libsinsp/marathon_http.h @@ -0,0 +1,50 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// marathon_http.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#ifdef HAS_CAPTURE + +#include "curl/curl.h" +#include "uri.h" +#include "mesos_http.h" +#include + +class marathon_http : public mesos_http +{ +public: + typedef std::shared_ptr ptr_t; + + marathon_http(mesos& m, const uri& url, bool discover_marathon, int timeout_ms = 5000L, const string& token = ""); + + ~marathon_http(); + + bool refresh_data(); + + std::string get_groups(const std::string& group_id); + +private: + std::string m_data; +}; + +#endif // HAS_CAPTURE +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/memmem.cpp b/userspace/libsinsp/memmem.cpp new file mode 100644 index 0000000000..f9ec85c67e --- /dev/null +++ b/userspace/libsinsp/memmem.cpp @@ -0,0 +1,50 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#ifndef _GNU_SOURCE +#include + +void *memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen) +{ + const unsigned char *ptr; + const unsigned char *end; + + if(needlelen == 0) + { + return (void *)haystack; + } + + if(haystacklen < needlelen) + { + return NULL; + } + + end = (const unsigned char *)haystack + haystacklen - needlelen; + for(ptr = (const unsigned char *)haystack; ptr <= end; ptr++) + { + if(!memcmp(ptr, needle, needlelen)) + { + return (void *)ptr; + } + } + + return NULL; +} +#endif diff --git a/userspace/libsinsp/mesos.cpp b/userspace/libsinsp/mesos.cpp new file mode 100644 index 0000000000..ac08eee420 --- /dev/null +++ b/userspace/libsinsp/mesos.cpp @@ -0,0 +1,1072 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos.cpp +// +#ifndef CYGWING_AGENT + +#include "mesos.h" +#include "mesos_component.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "json_error_log.h" + +const mesos_component::component_map mesos::m_components = +{ + { mesos_component::MESOS_FRAMEWORK, "framework" }, + { mesos_component::MESOS_TASK, "task" }, + { mesos_component::MESOS_SLAVE, "slave" } +}; + +const std::string mesos::default_state_uri = "http://localhost:5050"; +const std::string mesos::default_state_api = "/master/state.json"; +const std::string mesos::default_marathon_uri = "http://localhost:8080"; +const std::string mesos::default_groups_api = "/v2/groups"; +const std::string mesos::default_apps_api = "/v2/apps?embed=apps.tasks"; +const std::string mesos::default_watch_api = "/v2/events"; +const std::string mesos::default_version_api = "/version"; +const int mesos::default_timeout_ms = 5000; + +mesos::mesos(const std::string& mesos_state_json, + const std::string& marathon_groups_json, + const std::string& marathon_apps_json): + m_discover_mesos_leader(false), + m_discover_marathon_uris(false), + m_testing(true) +{ + if(mesos_state_json.empty() || + (marathon_groups_json.empty() && !marathon_apps_json.empty()) || + (!marathon_groups_json.empty() && marathon_apps_json.empty())) + { + throw sinsp_exception("Mesos state AND (both OR none [marathon apps and groups]) are needed"); + } + mesos_http::json_ptr_t state_json = mesos_http::try_parse(mesos_state_json, "fixed-mesos-state"); + if(state_json) + { + set_state_json(state_json); + if(!marathon_groups_json.empty()) + { + const Json::Value& frameworks = (*state_json)["frameworks"]; + if(frameworks.isNull() || !frameworks.isArray()) + { + throw sinsp_exception("Unexpected condition while detecting Mesos master: frameworks entry not found."); + } + g_logger.log("Found " + std::to_string(frameworks.size()) + " Mesos frameworks", sinsp_logger::SEV_DEBUG); + std::string framework_id; + for(auto framework : frameworks) + { + const Json::Value& name = framework["name"]; + if(!name.isNull() && name.isConvertibleTo(Json::stringValue) && mesos_framework::is_root_marathon(name.asString())) + { + const Json::Value& id = framework["id"]; + if(!id.isNull() && id.isConvertibleTo(Json::stringValue)) + { + framework_id = id.asString(); + } + } + } + mesos_http::json_ptr_t dummy_group; + set_marathon_groups_json(mesos_http::try_parse(marathon_groups_json, "fixed-marathon-state"), framework_id); + set_marathon_apps_json(dummy_group/*mesos_http::try_parse(marathon_apps_json, "fixed-groups-state")*/, framework_id); + } + collect_data(); + } + else + { + throw sinsp_exception("Could not create Mesos state JSON."); + } +} + +mesos::mesos(const std::string& state_uri, + const uri_list_t& marathon_uris, + bool discover_mesos_leader, + bool discover_marathon_leader, + const credentials_t& mesos_credentials, + const credentials_t& marathon_credentials, + int timeout_ms, + bool is_captured, + bool verbose): +#ifdef HAS_CAPTURE + m_collector(false), + m_mesos_uri(state_uri), + m_marathon_uris(marathon_uris), +#endif // HAS_CAPTURE + m_state(is_captured, verbose), + m_discover_mesos_leader(discover_mesos_leader), + m_discover_marathon_uris(discover_marathon_leader || marathon_uris.empty()), + m_timeout_ms(timeout_ms), + m_verbose(verbose), + m_testing(false) +{ +#ifdef HAS_CAPTURE + g_logger.log(std::string("Creating Mesos object for [" + + (m_mesos_uri.empty() ? std::string("capture replay") : m_mesos_uri) + + "], failover autodiscovery set to ") + + (m_discover_mesos_leader ? "true" : "false"), + sinsp_logger::SEV_DEBUG); + + if(m_marathon_uris.size() > 1) + { + std::string marathon_uri = m_marathon_uris[0]; + m_marathon_uris.clear(); + m_marathon_uris.emplace_back(marathon_uri); + g_logger.log("Multiple root marathon URIs configured; only the first one (" + marathon_uri + ") will have effect;" + " others will be treated as generic frameworks (user Marathon frameworks will be discovered).", sinsp_logger::SEV_WARNING); + } + uri mesos_state_uri(state_uri); + mesos_state_uri.get_credentials(m_mesos_credentials); + if(m_marathon_uris.size()) + { + uri marathon_uri(m_marathon_uris[0]); + marathon_uri.get_credentials(m_marathon_credentials); + } + + // explicitly specified credentials trump the ones in URI + if(!mesos_credentials.first.empty()) + { + m_mesos_credentials.first = mesos_credentials.first; + m_mesos_credentials.second = mesos_credentials.second; + } + if(!marathon_credentials.first.empty()) + { + m_marathon_credentials.first = marathon_credentials.first; + m_marathon_credentials.second = marathon_credentials.second; + } + +#endif + init(); +} + +mesos::mesos(const std::string& state_uri, + const uri_list_t& marathon_uris, + bool discover_mesos_leader, + bool discover_marathon_leader, + const credentials_t& dcos_enterprise_credentials, + int timeout_ms, + bool is_captured, + bool verbose): + mesos_auth(dcos_enterprise_credentials), +#ifdef HAS_CAPTURE + m_collector(false), + m_mesos_uri(state_uri), + m_marathon_uris(marathon_uris), +#endif // HAS_CAPTURE + m_state(is_captured, verbose), + m_discover_mesos_leader(discover_mesos_leader), + m_discover_marathon_uris(discover_marathon_leader || marathon_uris.empty()), + m_timeout_ms(timeout_ms), + m_verbose(verbose), + m_testing(false) +{ +#ifdef HAS_CAPTURE + g_logger.log(std::string("Creating Mesos object for [" + + (m_mesos_uri.empty() ? std::string("capture replay") : m_mesos_uri) + + "], failover autodiscovery set to ") + + (m_discover_mesos_leader ? "true" : "false"), + sinsp_logger::SEV_DEBUG); + + if(m_marathon_uris.size() > 1) + { + std::string marathon_uri = m_marathon_uris[0]; + m_marathon_uris.clear(); + m_marathon_uris.emplace_back(marathon_uri); + g_logger.log("Multiple root marathon URIs configured; only the first one (" + marathon_uri + ") will have effect;" + " others will be treated as generic frameworks (user Marathon frameworks will be discovered).", sinsp_logger::SEV_WARNING); + } + +#endif + init(); +} + +mesos::~mesos() +{ +} + +void mesos::init() +{ +#ifdef HAS_CAPTURE + if(!m_mesos_uri.empty()) + { + m_collector.remove_all(); + if((m_state_http) && (!m_state_http.unique())) + { + throw sinsp_exception("Invalid access to Mesos initializer: mesos state http client for [" + + m_mesos_uri + "] not unique."); + } + m_state_http = std::make_shared(*this, m_mesos_uri + default_state_api, m_discover_mesos_leader, m_marathon_uris.empty(), m_timeout_ms, m_token); + rebuild_mesos_state(true); + if(!has_marathon()) + { + init_marathon(); + } + } +#endif // HAS_CAPTURE +} + +void mesos::init_marathon() +{ +#ifdef HAS_CAPTURE + if(!m_mesos_uri.empty()) + { + m_marathon_groups_http.clear(); + m_marathon_apps_http.clear(); + + const uri_list_t& marathons = m_discover_marathon_uris ? m_state_http->get_marathon_uris() : m_marathon_uris; + if(marathons.size()) + { + g_logger.log("Found " + std::to_string(marathons.size()) + " Marathon URIs", sinsp_logger::SEV_DEBUG); + for(const auto& muri : marathons) + { + g_logger.log("Creating Marathon http objects: " + uri(muri).to_string(false), sinsp_logger::SEV_DEBUG); + m_marathon_groups_http[muri] = std::make_shared(*this, muri + default_groups_api, m_discover_marathon_uris, m_timeout_ms, m_token); + m_marathon_apps_http[muri] = std::make_shared(*this, muri + default_apps_api, m_discover_marathon_uris, m_timeout_ms, m_token); + } + + if(has_marathon()) + { + rebuild_marathon_state(true); + } + } + } +#endif // HAS_CAPTURE +} + +void mesos::refresh_token() +{ +#ifdef HAS_CAPTURE + mesos_auth::refresh_token(); + m_state_http->set_token(m_token); + if(has_marathon()) + { + for(auto& group_http : m_marathon_groups_http) + { + if(group_http.second) + { + group_http.second->set_token(m_token); + } + else + { + throw sinsp_exception("Marathon groups HTTP client is null."); + } + } + for(auto& app_http : m_marathon_apps_http) + { + if(app_http.second) + { + app_http.second->set_token(m_token); + } + else + { + throw sinsp_exception("Marathon apps HTTP client is null."); + } + } + } +#endif // HAS_CAPTURE +} + +#ifdef HAS_CAPTURE +const mesos::uri_list_t &mesos::marathon_uris() +{ + return (m_discover_marathon_uris ? m_state_http->get_marathon_uris() : m_marathon_uris); +} +#endif + +void mesos::refresh() +{ + rebuild_mesos_state(); + if(has_marathon()) + { + rebuild_marathon_state(); + } +} + +void mesos::rebuild_mesos_state(bool full) +{ +#ifdef HAS_CAPTURE + if(!m_mesos_uri.empty()) + { + if(full) + { + clear_mesos(); + if(m_state_http) + { + m_state_http->get_all_data(&mesos::parse_state); + } + else + { + throw sinsp_exception("Mesos state HTTP client is null."); + } + } + else + { + connect_mesos(); + send_mesos_data_request(); + collect_data(); + } + } +#endif // HAS_CAPTURE +} + +void mesos::rebuild_marathon_state(bool full) +{ +#ifdef HAS_CAPTURE + if(has_marathon()) + { + if(full) + { + clear_marathon(); + + for(auto& group_http : m_marathon_groups_http) + { + if(group_http.second) + { + group_http.second->get_all_data(&mesos::parse_groups); + } + else + { + throw sinsp_exception("Marathon groups HTTP client is null."); + } + } + + for(auto& app_http : m_marathon_apps_http) + { + if(app_http.second) + { + app_http.second->get_all_data(&mesos::parse_apps); + } + else + { + throw sinsp_exception("Marathon apps HTTP client is null."); + } + } + } + else + { + connect_marathon(); + send_marathon_data_request(); + collect_data(); + } + if(m_state_http) + { + const mesos_http::marathon_uri_t& marathon_uris = m_marathon_uris.empty() ? + m_state_http->get_marathon_uris() : + m_marathon_uris; + if(marathon_uris.size()) + { + m_state.set_marathon_uri(marathon_uris[0]); + } + else + { + throw sinsp_exception("Marathon detected but Marathon URI not found."); + } + } + else + { + throw sinsp_exception("Mesos state HTTP client is null."); + } + } +#endif // HAS_CAPTURE +} + +#ifdef HAS_CAPTURE +void mesos::send_marathon_data_request() +{ + if(has_marathon()) + { + for(auto& group_http : m_marathon_groups_http) + { + if(group_http.second) + { + group_http.second->send_request(); + g_logger.log("Marathon groups request sent.", sinsp_logger::SEV_DEBUG); + } + else + { + throw sinsp_exception("Marathon groups HTTP client is null."); + } + } + for(auto& app_http : m_marathon_apps_http) + { + if(app_http.second) + { + app_http.second->send_request(); + g_logger.log("Marathon apps request sent.", sinsp_logger::SEV_DEBUG); + } + else + { + throw sinsp_exception("Marathon apps HTTP client is null."); + } + } + } +} + +void mesos::connect_marathon() +{ + if(has_marathon()) + { + for(auto& group_http : m_marathon_groups_http) + { + if(!connect(group_http.second, &mesos::set_marathon_groups_json, 2)) + { + throw sinsp_exception("Connection to Marathon group API failed."); + } + } + for(auto& app_http : m_marathon_apps_http) + { + if(!connect(app_http.second, &mesos::set_marathon_apps_json, 3)) + { + throw sinsp_exception("Connection to Marathon app API failed."); + } + } + } +} + +void mesos::send_mesos_data_request() +{ + if(m_state_http) + { + m_state_http->send_request(); + } + else + { + throw sinsp_exception("Mesos state HTTP client is null."); + } +} + +void mesos::connect_mesos() +{ + if(!connect(m_state_http, &mesos::set_state_json, 1)) + { + throw sinsp_exception("Connection to Mesos API failed."); + } +} +#endif // HAS_CAPTURE + +bool mesos::is_alive() const +{ +#ifdef HAS_CAPTURE + if(m_state_http && !m_state_http->is_connected()) + { + g_logger.log("Mesos state connection loss.", sinsp_logger::SEV_WARNING); + return false; + } + + for(const auto& group : m_marathon_groups_http) + { + if(group.second && !group.second->is_connected()) + { + g_logger.log("Marathon groups connection loss.", sinsp_logger::SEV_WARNING); + return false; + } + } + + for(const auto& app : m_marathon_apps_http) + { + if(app.second && !app.second->is_connected()) + { + g_logger.log("Marathon apps connection loss.", sinsp_logger::SEV_WARNING); + return false; + } + } +#endif // HAS_CAPTURE + return true; +} + +#ifdef HAS_CAPTURE + +void mesos::check_collector_status(int expected) +{ + if(!m_collector.is_healthy(expected)) + { + throw sinsp_exception("Mesos collector not healthy (has " + std::to_string(m_collector.subscription_count()) + + " connections, expected " + std::to_string(expected) + "); giving up on data collection in this cycle ..."); + } +} + +void mesos::send_data_request(bool collect) +{ + if(m_mesos_state_json && !m_mesos_state_json->isNull()) { return; } + connect_mesos(); + send_mesos_data_request(); + g_logger.log("Mesos request sent.", sinsp_logger::SEV_DEBUG); + + if(has_marathon()) + { + for(auto& group : m_marathon_groups_json) + { + if(group.second && !group.second->isNull()) { return; } + } + for(auto& app : m_marathon_apps_json) + { + if(app.second && !app.second->isNull()) { return; } + } + connect_marathon(); + send_marathon_data_request(); + } + + if(collect) { collect_data(); } +} + +void mesos::capture_frameworks(const Json::Value& root, Json::Value& capture) +{ + const Json::Value& frameworks = root["frameworks"]; + if(!frameworks.isNull()) + { + if(frameworks.isArray()) + { + if(frameworks.size()) + { + capture["frameworks"] = Json::arrayValue; + for(const auto& framework : frameworks) + { + Json::Value c_framework; + c_framework["active"] = framework["active"]; + c_framework["id"] = framework["id"]; + c_framework["name"] = framework["name"]; + c_framework["hostname"] = framework["hostname"]; + c_framework["webui_url"] = framework["webui_url"]; + c_framework["tasks"] = Json::arrayValue; + Json::Value& c_tasks = c_framework["tasks"]; + for(const auto& task : framework["tasks"]) + { + Json::Value& c_task = c_tasks.append(Json::Value()); + c_task["id"] = task["id"]; + c_task["name"] = task["name"]; + c_task["framework_id"] = task["framework_id"]; + c_task["executor_id"] = task["executor_id"]; + c_task["slave_id"] = task["slave_id"]; + c_task["state"] = task["state"]; + //? TODO: statuses + c_task["labels"] = task["labels"]; + } + capture["frameworks"].append(c_framework); + } + } + } + } +} + +void mesos::capture_slaves(const Json::Value& root, Json::Value& capture) +{ + const Json::Value& slaves = root["slaves"]; + if(!slaves.isNull()) + { + capture["slaves"] = Json::arrayValue; + for(const auto& slave : slaves) + { + Json::Value c_slave; + c_slave["hostname"] = slave["hostname"]; + c_slave["id"] = slave["id"]; + capture["slaves"].append(c_slave); + } + } +} +#endif // HAS_CAPTURE + +bool mesos::collect_data() +{ +#ifdef HAS_CAPTURE + const int tout_s = 30; + + //TODO: see if we can do better here - instead of timing out, depending on + // mesos_collector socket drop detection when remote end closes connection + time_t now; time(&now); + if(m_last_mesos_refresh && difftime(now, m_last_mesos_refresh) > tout_s) + { + throw sinsp_exception("Detected stalled Mesos connection (" + + std::to_string(difftime(now, m_last_mesos_refresh)) + "s)." + " Reconnect attempt in next cycle ..."); + } + if(m_last_marathon_refresh && difftime(now, m_last_marathon_refresh) > tout_s) + { + throw sinsp_exception("Detected stalled Marathon connection(" + + std::to_string(difftime(now, m_last_marathon_refresh)) + "s)." + " Reconnect attempt in next cycle ..."); + } + if(m_json_error) + { + throw sinsp_exception("Detected JSON parsing error. Reconnect attempt in next cycle ..."); + } + + bool ret = false; + if(m_collector.subscription_count()) + { + m_collector.get_data(); + } + + if(m_mesos_state_json && !m_mesos_state_json->isNull()) + { + g_logger.log("Mesos state data detected.", sinsp_logger::SEV_DEBUG); + if(has_marathon()) + { + g_logger.log("Marathon connection detected.", sinsp_logger::SEV_DEBUG); + if(!m_marathon_apps_json.empty() && !m_marathon_groups_json.empty()) + { + g_logger.log("Marathon data detected.", sinsp_logger::SEV_DEBUG); + for(auto& group : m_marathon_groups_json) + { + if(group.second && !group.second->isNull()) + { + json_map_type_t::iterator app_it = m_marathon_apps_json.find(group.first); + if(app_it != m_marathon_apps_json.end()) + { + if(app_it->second && !app_it->second->isNull()) + { + parse_state(std::move(*m_mesos_state_json)); + m_mesos_state_json.reset(); + m_last_mesos_refresh = now; + if(m_inactive_frameworks.size()) + { + g_logger.log("Collection detected " + std::to_string(m_inactive_frameworks.size()) + " inactive frameworks", + sinsp_logger::SEV_DEBUG); + } + if(m_inactive_frameworks.find(group.first) == m_inactive_frameworks.end()) + { + g_logger.log("Detected active Marathon framework " + group.first, sinsp_logger::SEV_DEBUG); + // +++ order is important - apps belong to groups and must be processed after + parse_groups(std::move(*group.second), group.first); + parse_apps(std::move(*app_it->second), app_it->first); + m_last_marathon_refresh = now; + // --- + } + else // framework was shut down, clear groups/apps + { + g_logger.log("Detected inactive Marathon framework " + group.first, sinsp_logger::SEV_DEBUG); + m_state.erase_groups(group.first); // apps will go away with groups + m_inactive_frameworks.insert(group.first); + } + group.second.reset(); + app_it->second.reset(); + m_json_error = false; + ret = true; + } + else if((difftime(now, m_last_marathon_refresh) > tout_s) || m_json_error) + { + std::string errstr = "Detected null Marathon app (" + app_it->first + "), resetting current state."; + g_logger.log(errstr, sinsp_logger::SEV_WARNING); + g_json_error_log.log(app_it->first, errstr, sinsp_utils::get_current_time_ns(), "marathon-apps-state"); + m_mesos_state_json.reset(); + group.second.reset(); + app_it->second.reset(); + m_json_error = false; + } + } + else + { + // must never happen + throw sinsp_exception("A discrepancy found between groups and apps " + "(app json for framework [" + group.first + "] not found in json map)."); + } + } + else if((difftime(now, m_last_marathon_refresh) > tout_s) || m_json_error) + { + g_logger.log("Detected null Marathon group (" + group.first + "), resetting current state.", sinsp_logger::SEV_WARNING); + m_mesos_state_json.reset(); + json_map_type_t::iterator app_it = m_marathon_apps_json.find(group.first); + if(app_it != m_marathon_apps_json.end()) + { + app_it->second.reset(); + } + m_json_error = false; + } + } + } + } + else + { + parse_state(std::move(*m_mesos_state_json)); + m_mesos_state_json.reset(); + m_marathon_groups_json.clear(); + m_marathon_apps_json.clear(); + if(m_state_http->get_marathon_uris().size()) + { + rebuild_marathon_state(true); + } + m_json_error = false; + ret = true; + } + } + + return ret; +#else + return true; +#endif // HAS_CAPTURE +} + +void mesos::handle_frameworks(const Json::Value& root) +{ + const Json::Value& frameworks = root["frameworks"]; + if(!frameworks.isNull()) + { + if(frameworks.isArray()) + { + if(frameworks.size()) + { + for(const auto& framework : frameworks) + { + const Json::Value& uid = framework["id"]; + if(!uid.isNull() && uid.isString()) + { + const Json::Value& fw_name = framework["name"]; + std::string name; + if(!fw_name.isNull() && fw_name.isString()) + { + name = framework["name"].asString(); + } + if(!mesos_framework::is_framework_active(framework)) + { + framework_list_t::iterator it = m_inactive_frameworks.find(uid.asString()); + if(it == m_inactive_frameworks.end()) + { + std::string fid = uid.asString(); + m_inactive_frameworks.insert(fid); + m_activated_frameworks.erase(fid); + g_logger.log("Mesos framework deactivated: " + name + '[' + fid + ']', sinsp_logger::SEV_INFO); + remove_framework(framework); +#ifdef HAS_CAPTURE + remove_framework_http(m_marathon_groups_http, fid); + remove_framework_http(m_marathon_apps_http, fid); +#endif // HAS_CAPTURE + } + } + else // active framework detected + { + add_framework(framework); + if((m_inactive_frameworks.erase(uid.asString())) || + (m_activated_frameworks.find(uid.asString()) == m_activated_frameworks.end())) + { + g_logger.log("New or activated Mesos framework detected: " + name + " [" + uid.asString() + ']', sinsp_logger::SEV_INFO); + m_activated_frameworks.insert(uid.asString()); +#ifdef HAS_CAPTURE + if(mesos_framework::is_root_marathon(name) && + find_if(m_marathon_groups_http.begin(), m_marathon_groups_http.end(), [uid](const decltype(m_marathon_groups_http)::value_type& item) + { + return item.second->get_framework_id() == uid.asString(); + }) == m_marathon_groups_http.end()) + { + init_marathon(); + } +#endif + } + } + } + } + } + else + { + throw sinsp_exception("No Mesos frameworks found (possibly Mesos master HA migration, will retry)."); + } + } + else + { + throw sinsp_exception("Mesos frameworks entry found but not a JSON array."); + } + } + else + { + throw sinsp_exception("No Mesos frameworks entry found in state."); + } +} + +void mesos::handle_slaves(const Json::Value& root) +{ + const Json::Value& slaves = root["slaves"]; + if(!slaves.isNull()) + { + for(const auto& slave : slaves) + { + add_slave(slave); + } + } + else + { + g_logger.log("No frameworks found.", sinsp_logger::SEV_WARNING); + } +} + +void mesos::add_framework(const Json::Value& framework) +{ + std::string name, uid; + const Json::Value& fname = framework["name"]; + const Json::Value& fid = framework["id"]; + if(!fname.isNull()) + { + name = fname.asString(); + } + if(!fid.isNull()) + { + uid = fid.asString(); + } + if(!m_creation_logged) + { + std::ostringstream os; + os << "Adding Mesos framework: [" << name << ',' << uid << ']'; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + m_state.emplace_framework(mesos_framework(name, uid)); + add_tasks(m_state.get_frameworks().back(), framework); +} + +void mesos::remove_framework(const Json::Value& framework) +{ + m_state.remove_framework(framework); +} + +#ifdef HAS_CAPTURE +void mesos::remove_framework_http(marathon_http_map& http_map, const std::string& framework_id) +{ + for(marathon_http_map::iterator it = http_map.begin(), end = http_map.end(); it != end; ++it) + { + if(it->second && it->second->get_framework_id() == framework_id) + { + http_map.erase(it); + g_logger.log("Removed http for deactivated Marathon framework [" + framework_id + ']', + sinsp_logger::SEV_DEBUG); + return; + } + } +} +#endif // HAS_CAPTURE + +void mesos::add_slave(const Json::Value& slave) +{ + std::string name, uid; + const Json::Value& sname = slave["hostname"]; + const Json::Value& sid = slave["id"]; + if(!sname.isNull()) + { + name = sname.asString(); + } + if(!sid.isNull()) + { + uid = sid.asString(); + } + if(!m_creation_logged) + { + std::ostringstream os; + os << "Adding Mesos slave: [" << name << ',' << uid << ']'; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + } + m_state.emplace_slave(mesos_slave(name, uid)); +} + +void mesos::add_tasks_impl(mesos_framework& framework, const Json::Value& tasks) +{ + if(!tasks.isNull()) + { + for(const auto& task : tasks) + { + if(mesos_task::is_task_running(task)) + { + mesos_task::ptr_t t = mesos_task::make_task(task); + std::ostringstream os; + if(t) + { + os << "Adding Mesos task: [" << framework.get_name() << ':' << t->get_name() << ',' << t->get_uid() << ']'; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + m_state.add_or_replace_task(framework, t); + } + else + { + std::string name, uid, sid; + Json::Value fname = task["name"]; + if(!fname.isNull()) { name = fname.asString(); } + Json::Value fid = task["id"]; + if(!fid.isNull()) { uid = fid.asString(); } + Json::Value fsid = task["slave_id"]; + if(!fsid.isNull()) { sid = fsid.asString(); } + os << "Failed to add Mesos task: [" << framework.get_name() << ':' << name << ',' << uid << "], running on slave " << sid; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + g_json_error_log.log(framework.get_name(), os.str(), sinsp_utils::get_current_time_ns(), "add_tasks_impl"); + } + } + } + } + else + { + std::string errstr = "Tasks is null"; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(framework.get_name(), errstr, sinsp_utils::get_current_time_ns(), "add_tasks_impl for framework"); + } +} + +void mesos::add_tasks(mesos_framework& framework, const Json::Value& f_val) +{ + const Json::Value& tasks = f_val["tasks"]; + add_tasks_impl(framework, tasks); +} + +void mesos::check_frameworks(const json_ptr_t& json) +{ + if(has_marathon() && json && !json->isNull()) + { + const Json::Value& frameworks = (*json)["frameworks"]; + if(frameworks.isNull()) + { + throw sinsp_exception("No Mesos frameworks entry found."); + } + else + { + if(frameworks.isArray()) + { + if(!frameworks.size()) + { + throw sinsp_exception("No Mesos frameworks found (possibly Mesos master HA migration)."); + } + } + else + { + throw sinsp_exception("Unexpected Mesos frameworks entry found (not an array)."); + } + } + } +} + +void mesos::set_state_json(json_ptr_t json, const std::string&) +{ + bool json_error = !json || (json && json->isNull()); + m_mesos_state_json = json; + if(!json_error) + { + g_logger.log("Received state JSON", sinsp_logger::SEV_DEBUG); + check_frameworks(m_mesos_state_json); + } + else + { + g_logger.log("Received invalid state JSON", sinsp_logger::SEV_WARNING); + } + m_json_error = m_json_error || json_error; +} + +void mesos::parse_state(Json::Value&& root) +{ + clear_mesos(); + handle_frameworks(root); + handle_slaves(root); +#ifdef HAS_CAPTURE + if(m_state.is_captured()) + { + Json::Value capt; + capture_frameworks(root, capt); + capture_slaves(root, capt); + m_state.enqueue_capture_event(mesos_state_t::capture::MESOS_STATE, Json::FastWriter().write(capt)); + } +#endif // HAS_CAPTURE + if(m_verbose) + { + std::cout << Json::FastWriter().write(root) << std::endl; + } + if(!m_creation_logged) + { + m_creation_logged = true; + } +} + +void mesos::parse_state(json_ptr_t json, const std::string&) +{ + if(json && !json->isNull()) + { + parse_state(std::move(*json)); + } + else + { + throw sinsp_exception("Invalid JSON (parsing Mesos state failed)."); + } +} + +void mesos::set_marathon_groups_json(json_ptr_t json, const std::string& framework_id) +{ + bool json_error = !json || (json && json->isNull()); + m_marathon_groups_json[framework_id] = json; + if(!json_error) + { + g_logger.log("Received groups JSON for Marathon framework [" + framework_id + ']', sinsp_logger::SEV_DEBUG); + } + else + { + g_logger.log("Received invalid Marathon groups JSON", sinsp_logger::SEV_WARNING); + } + m_json_error = m_json_error || json_error; +} + +void mesos::set_marathon_apps_json(json_ptr_t json, const std::string& framework_id) +{ + bool json_error = !json || (json && json->isNull()); + m_marathon_apps_json[framework_id] = json; + if(!json_error) + { + g_logger.log("Received apps JSON for Marathon framework [" + framework_id + ']', sinsp_logger::SEV_DEBUG); + } + else + { + std::string errstr = "Received invalid Marathon apps JSON"; + g_logger.log(errstr, sinsp_logger::SEV_WARNING); + g_json_error_log.log("(null)", errstr, sinsp_utils::get_current_time_ns(), "set-marathon-apps-json"); + } + m_json_error = m_json_error || json_error; +} + +void mesos::simulate_event(const std::string& json) +{ + Json::Value root; + Json::Reader reader; + if(reader.parse(json, root)) + { + Json::Value::Members members = root.getMemberNames(); + for (auto& member : members) + { + if(member == "mesos_state") + { + m_discover_marathon_uris = false; + parse_state(std::move(root[member])); + } + else if(member == "marathon_groups") + { + const Json::Value& frameworkId = root[member]["frameworkId"]; + if(!frameworkId.isNull() && frameworkId.isString()) + { + m_state.parse_groups(std::move(root[member]), frameworkId.asString()); + } + else + { + throw sinsp_exception("Framework ID for Marathon groups not found during event simulation."); + } + } + else if(member == "marathon_apps") + { + const Json::Value& frameworkId = root[member]["frameworkId"]; + if(!frameworkId.isNull() && frameworkId.isString()) + { + m_state.parse_apps(std::move(root[member]), frameworkId.asString()); + } + else + { + throw sinsp_exception("Framework ID for Marathon groups not found during event simulation."); + } + } + } + } + else + { + std::string errstr = "Could not parse json (" + reader.getFormattedErrorMessages() + ")"; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(json, errstr, sinsp_utils::get_current_time_ns(), "parse-mesos-evt"); + } +} +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/mesos.h b/userspace/libsinsp/mesos.h new file mode 100644 index 0000000000..9b00cd33ab --- /dev/null +++ b/userspace/libsinsp/mesos.h @@ -0,0 +1,268 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "mesos_auth.h" +#include "mesos_common.h" +#include "mesos_component.h" +#include "mesos_http.h" +#include "marathon_http.h" +#include "mesos_state.h" +#include "mesos_collector.h" +#include "uri.h" +#include +#include +#include + +class mesos : public mesos_auth +{ +public: + +#ifdef HAS_CAPTURE + typedef mesos_http::marathon_uri_t uri_list_t; +#else + typedef std::vector uri_list_t; +#endif // HAS_CAPTURE + typedef std::shared_ptr json_ptr_t; + typedef uri::credentials_t credentials_t; + typedef std::shared_ptr uri_creds_ptr_t; + + static const std::string default_state_uri; + static const std::string default_state_api; + static const std::string default_marathon_uri; + static const std::string default_groups_api; + static const std::string default_apps_api; + static const std::string default_watch_api; + static const std::string default_version_api; + static const int default_timeout_ms; + + // constructor for testing only, not to be used in production + mesos(const std::string& mesos_state_json, + const std::string& marathon_groups_json, + const std::string& marathon_apps_json); + + mesos(const std::string& state_uri, + const uri_list_t& marathon_uris = uri_list_t(), + bool discover_mesos_leader = false, + bool discover_marathon_leader = false, + const credentials_t& mesos_credentials = credentials_t(), + const credentials_t& marathon_credentials = credentials_t(), + int timeout_ms = default_timeout_ms, + bool is_captured = false, + bool verbose = false); + + mesos(const std::string& state_uri, + const uri_list_t& marathon_uris = uri_list_t(), + bool discover_mesos_leader = false, + bool discover_marathon_leader = false, + const credentials_t& dcos_enterprise_credentials = credentials_t(), + int timeout_ms = default_timeout_ms, + bool is_captured = false, + bool verbose = false); + + virtual ~mesos(); + + const mesos_state_t& get_state() const; + bool is_alive() const; + void refresh(); + void clear_mesos(); + + bool has_marathon() const; + void clear_marathon(); + + void simulate_event(const std::string& json); + bool collect_data(); + virtual void refresh_token(); + +#ifdef HAS_CAPTURE + const uri_list_t &marathon_uris(); + + void send_data_request(bool collect = true); + + const mesos_state_t::capture_list& get_capture_events() const; + std::string dequeue_capture_event(); + +private: + void send_mesos_data_request(); + void connect_mesos(); + void check_collector_status(int expected); + void send_marathon_data_request(); + void connect_marathon(); + + template + bool connect(T http, typename T::element_type::callback_func_t func, int expected_connections) + { + if(http) + { + if(m_collector.has(http)) + { + if(!http->is_connected()) + { + m_collector.remove(http); + } + } + if(!m_collector.has(http)) + { + http->set_parse_func(func); + m_collector.add(http); + } + check_collector_status(expected_connections); + return m_collector.has(http); + } + return false; + } + void capture_frameworks(const Json::Value& root, Json::Value& capture); + void capture_slaves(const Json::Value& root, Json::Value& capture); + + typedef std::unordered_map marathon_http_map; + + void remove_framework_http(marathon_http_map& http_map, const std::string& framework_id); + + mesos_http::ptr_t m_state_http; + marathon_http_map m_marathon_groups_http; + marathon_http_map m_marathon_apps_http; + mesos_collector m_collector; + std::string m_mesos_uri; + uri_list_t m_marathon_uris; +#endif // HAS_CAPTURE + +private: + void init(); + void init_marathon(); + void rebuild_mesos_state(bool full = false); + void rebuild_marathon_state(bool full = false); + + void handle_frameworks(const Json::Value& root); + void add_framework(const Json::Value& framework); + void add_tasks(mesos_framework& framework, const Json::Value& f_val); + void add_tasks_impl(mesos_framework& framework, const Json::Value& tasks); + void handle_slaves(const Json::Value& root); + void add_slave(const Json::Value& framework); + + void check_frameworks(const json_ptr_t& json); + void set_state_json(json_ptr_t json, const std::string& dummy = ""); + void parse_state(Json::Value&& root); + void parse_state(json_ptr_t json, const std::string&); + void set_marathon_groups_json(json_ptr_t json, const std::string& framework_id); + void parse_groups(json_ptr_t json, const std::string& framework_id); + void parse_groups(Json::Value&& json, const std::string& framework_id); + void set_marathon_apps_json(json_ptr_t json, const std::string& framework_id); + void parse_apps(json_ptr_t json, const std::string& framework_id); + void parse_apps(Json::Value&& json, const std::string& framework_id); + void remove_framework(const Json::Value& framework); + + mesos_state_t m_state; + bool m_creation_logged = false; + bool m_discover_mesos_leader; + bool m_discover_marathon_uris; + long m_timeout_ms; + bool m_verbose = false; + + typedef std::map json_map_type_t; + json_ptr_t m_mesos_state_json; + json_map_type_t m_marathon_groups_json; + json_map_type_t m_marathon_apps_json; + time_t m_last_mesos_refresh = 0; + time_t m_last_marathon_refresh = 0; + bool m_json_error = false; + bool m_testing = false; + uri::credentials_t m_mesos_credentials; + uri::credentials_t m_marathon_credentials; + + typedef std::unordered_set framework_list_t; + framework_list_t m_inactive_frameworks; + framework_list_t m_activated_frameworks; + + static const mesos_component::component_map m_components; + + friend class mesos_http; + friend class marathon_http; +}; + +inline const mesos_state_t& mesos::get_state() const +{ + return m_state; +} + +inline bool mesos::has_marathon() const +{ +#ifdef HAS_CAPTURE + if(m_testing) + { + return true; + } + else + { + return m_marathon_groups_http.size() || m_marathon_apps_http.size(); + } +#else + return false; +#endif +} + +#ifdef HAS_CAPTURE + +inline const mesos_state_t::capture_list& mesos::get_capture_events() const +{ + return m_state.get_capture_events(); +} + +inline std::string mesos::dequeue_capture_event() +{ + return m_state.dequeue_capture_event(); +} + +#endif // HAS_CAPTURE + +inline void mesos::clear_mesos() +{ + m_state.clear_mesos(); +} + +inline void mesos::clear_marathon() +{ + m_state.clear_marathon(); +} + +inline void mesos::parse_apps(json_ptr_t json, const std::string& framework_id) +{ + m_state.parse_apps(std::move(json), framework_id); +} + +inline void mesos::parse_groups(json_ptr_t json, const std::string& framework_id) +{ + m_state.parse_groups(std::move(json), framework_id); +} + +inline void mesos::parse_apps(Json::Value&& json, const std::string& framework_id) +{ + m_state.parse_apps(std::move(json), framework_id); +} + +inline void mesos::parse_groups(Json::Value&& json, const std::string& framework_id) +{ + m_state.parse_groups(std::move(json), framework_id); +} + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/mesos_auth.cpp b/userspace/libsinsp/mesos_auth.cpp new file mode 100644 index 0000000000..822930906f --- /dev/null +++ b/userspace/libsinsp/mesos_auth.cpp @@ -0,0 +1,130 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +// +// mesos_auth.cpp +// +#ifndef CYGWING_AGENT + +#include + +#include "mesos_auth.h" +#include "json_error_log.h" + +using namespace std; + +mesos_auth::mesos_auth(const uri::credentials_t& dcos_enterprise_credentials, + string auth_hostname, + int token_refresh_interval) + : m_dcos_enterprise_credentials(dcos_enterprise_credentials), + m_auth_uri(string("https://") + auth_hostname + string("/acs/api/v1/auth/login")), + m_token_refresh_interval(token_refresh_interval), + m_last_token_refresh_s(0) + +{ + if(!m_dcos_enterprise_credentials.first.empty()) + { + authenticate(); + } +} + +mesos_auth::~mesos_auth() +{ +} + +string mesos_auth::get_token() +{ + refresh_token(); + return m_token; +} + +void mesos_auth::authenticate() +{ +#ifdef HAS_CAPTURE + + try + { + sinsp_curl auth_request(m_auth_uri, "", ""); + Json::FastWriter json_writer; + Json::Value auth_obj; + auth_obj["uid"] = m_dcos_enterprise_credentials.first; + auth_obj["password"] = m_dcos_enterprise_credentials.second; + auth_request.add_header("Content-Type: application/json"); + auth_request.setopt(CURLOPT_POST, 1); + auth_request.set_body(json_writer.write(auth_obj)); + //auth_request.enable_debug(); + auto response = auth_request.get_data(); + + if(auth_request.get_response_code() == 200) + { + Json::Reader json_reader; + Json::Value response_obj; + auto parse_ok = json_reader.parse(response, response_obj, false); + if(parse_ok && response_obj.isMember("token")) + { + m_token = response_obj["token"].asString(); + g_logger.format(sinsp_logger::SEV_DEBUG, "Mesos authenticated with token=%s", m_token.c_str()); + } + else if (!parse_ok) + { + std::string errstr; + errstr = json_reader.getFormattedErrorMessages(); + g_json_error_log.log(response, errstr, sinsp_utils::get_current_time_ns(), m_auth_uri.to_string()); + throw sinsp_exception(string("Cannot parse json (" + errstr + ")")); + } + else + { + throw sinsp_exception(string("Cannot authenticate on Mesos master, response=") + response); + } + } else + { + throw sinsp_exception(string("Cannot authenticate on Mesos master, response_code=") + to_string(auth_request.get_response_code())); + } + time(&m_last_token_refresh_s); + } + catch(const std::exception& e) + { + std::string errstr = "Could not fetch authentication token via " + + m_auth_uri.to_string() + ": " + + e.what(); + + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), m_auth_uri.to_string()); + } +#endif // HAS_CAPTURE +} + +void mesos_auth::refresh_token() +{ +#ifdef HAS_CAPTURE + if(!m_dcos_enterprise_credentials.first.empty()) + { + time_t now; time(&now); + + if(difftime(now, m_last_token_refresh_s) > m_token_refresh_interval) + { + g_logger.format(sinsp_logger::SEV_DEBUG, "Regenerating Mesos Auth token"); + authenticate(); + } + } +#endif // HAS_CAPTURE +} +#endif // CYGWING_AGENT + diff --git a/userspace/libsinsp/mesos_auth.h b/userspace/libsinsp/mesos_auth.h new file mode 100644 index 0000000000..8b1fa41439 --- /dev/null +++ b/userspace/libsinsp/mesos_auth.h @@ -0,0 +1,60 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +// +// mesos_auth.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "mesos_http.h" +#include "uri.h" + +static const uint64_t DCOS_ENTERPRISE_TOKEN_REFRESH_S = 60*60*24; // 1 day + +class mesos_auth +{ +public: + mesos_auth(const uri::credentials_t& dcos_enterprise_credentials = uri::credentials_t(), + std::string auth_hostname = "localhost", + int token_refresh_interval = DCOS_ENTERPRISE_TOKEN_REFRESH_S); + virtual ~mesos_auth(); + + virtual void refresh_token(); + + // Return the current token. The token may expire at any time + // after the token has been returned, so it's best to call + // get_token periodically, which will internally refresh the + // token if necessary. + std::string get_token(); + +protected: + std::string m_token; + +private: + void authenticate(); + + const uri::credentials_t m_dcos_enterprise_credentials; + uri m_auth_uri; + int m_token_refresh_interval; + time_t m_last_token_refresh_s; +}; + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/mesos_collector.cpp b/userspace/libsinsp/mesos_collector.cpp new file mode 100644 index 0000000000..d594d78162 --- /dev/null +++ b/userspace/libsinsp/mesos_collector.cpp @@ -0,0 +1,250 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_collector.cpp +// +#ifndef CYGWING_AGENT +#ifdef HAS_CAPTURE + +#include "sinsp.h" +#include "sinsp_int.h" +#include "mesos_collector.h" +#include "mesos_http.h" +#include +#include +#include + + +mesos_collector::mesos_collector(bool do_loop, long timeout_ms): + m_nfds(0), + m_loop(do_loop), + m_timeout_ms(timeout_ms), + m_stopped(false) +{ + clear(); +} + +mesos_collector::~mesos_collector() +{ +} + +void mesos_collector::clear() +{ + FD_ZERO(&m_errfd); + FD_ZERO(&m_infd); +} + +void mesos_collector::add(std::shared_ptr handler) +{ + int sockfd = handler->get_socket(m_timeout_ms); + + FD_SET(sockfd, &m_errfd); + FD_SET(sockfd, &m_infd); + if(sockfd > m_nfds) + { + m_nfds = sockfd; + } + m_sockets[sockfd] = handler; +} + +bool mesos_collector::has(std::shared_ptr handler) +{ + for(const auto& http : m_sockets) + { + if(http.second == handler) + { + return true; + } + } + return false; +} + +bool mesos_collector::remove(std::shared_ptr handler) +{ + for(socket_map_t::iterator it = m_sockets.begin(); it != m_sockets.end(); ++it) + { + if(it->second == handler) + { + remove(it); + return true; + } + } + return false; +} + +mesos_collector::socket_map_t::iterator& mesos_collector::remove(socket_map_t::iterator& it) +{ + if(it != m_sockets.end()) + { + m_sockets.erase(it++); + } + m_nfds = 0; + for(const auto& sock : m_sockets) + { + if(sock.first > m_nfds) + { + m_nfds = sock.first; + } + } + return it; +} + +void mesos_collector::remove_all() +{ + clear(); + m_sockets.clear(); + m_nfds = 0; +} + +bool mesos_collector::is_active() const +{ + return subscription_count() > 0; +} + +bool mesos_collector::is_healthy(int expected_count) const +{ + return subscription_count() >= expected_count; +} + +int mesos_collector::subscription_count() const +{ + return m_sockets.size(); +} + +void mesos_collector::get_data() +{ + try + { + struct timeval tv; + int res; + m_stopped = false; + while(!m_stopped) + { + tv.tv_sec = m_loop ? m_timeout_ms / 1000 : 0; + tv.tv_usec = m_loop ? (m_timeout_ms % 1000) * 1000 : 0; + { + if(m_sockets.size()) + { + g_logger.log("Mesos collector number of sockets: " + std::to_string(m_sockets.size()), sinsp_logger::SEV_DEBUG); + res = select(m_nfds + 1, &m_infd, NULL, &m_errfd, &tv); + if(res < 0) // error + { + std::string err = std::string("Mesos collector select error, removing all sockets (") + strerror(errno) + ")"; + g_logger.log(err, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", err, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + remove_all(); + } + else // data or idle + { + for(socket_map_t::iterator it = m_sockets.begin(); it != m_sockets.end();) + { + if(FD_ISSET(it->first, &m_infd)) + { + if(it->second && !it->second->on_data()) + { + if(errno != EAGAIN) + { + std::string fid = it->second->get_framework_id(); + if(!fid.empty()) + { + std::string errstr = "Mesos collector data handling error, removing Marathon socket for framework [" + fid + ']'; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + } + else + { + std::string errstr = "Mesos collector data handling error, removing Mesos state socket."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + } + remove(it); + continue; + } + } + } + else + { + FD_SET(it->first, &m_infd); + } + + if(FD_ISSET(it->first, &m_errfd)) + { + if(errno != EAGAIN) + { + std::string errstr = std::string("Mesos collector select errfd: ") + strerror(errno); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + std::string fid; + if(it->second) + { + it->second->on_error(errstr, true); + fid = it->second->get_framework_id(); + } + if(!fid.empty()) + { + std::string errstr = "Mesos collector socket error, removing Marathon socket for framework [" + fid + ']'; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + } + else + { + std::string errstr = "Mesos collector socket error, removing Mesos state socket."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + } + remove(it); + continue; + } + } + else + { + FD_SET(it->first, &m_errfd); + } + ++it; + } + } + } + else + { + std::string errstr = "Mesos collector is empty. Stopping."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + m_stopped = true; + return; + } + } + if(!m_loop) + { + break; + } + } + } + catch(const std::exception& ex) + { + std::string errstr = std::string("Mesos collector error: ") + ex.what(); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), "mesos-collector-get-data"); + remove_all(); + m_stopped = true; + } +} + + +#endif // HAS_CAPTURE +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/mesos_collector.h b/userspace/libsinsp/mesos_collector.h new file mode 100644 index 0000000000..ee5b44330b --- /dev/null +++ b/userspace/libsinsp/mesos_collector.h @@ -0,0 +1,77 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_collector.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#ifdef HAS_CAPTURE + +#include "mesos_common.h" +#include +#include + +class mesos_http; + +class mesos_collector +{ +public: + typedef std::map> socket_map_t; + + mesos_collector(bool do_loop = true, long timeout_ms = 1000L); + + ~mesos_collector(); + + void add(std::shared_ptr handler); + + void remove_all(); + + int subscription_count() const; + + void get_data(); + + void stop(); + + bool is_active() const; + bool is_healthy(int expected_count) const; + + bool has(std::shared_ptr handler); + bool remove(std::shared_ptr handler); + +private: + void clear(); + socket_map_t::iterator& remove(socket_map_t::iterator& it); + + socket_map_t m_sockets; + fd_set m_infd; + fd_set m_errfd; + int m_nfds; + bool m_loop; + long m_timeout_ms; + bool m_stopped; +}; + +inline void mesos_collector::stop() +{ + m_stopped = true; +} + +#endif // HAS_CAPTURE +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/mesos_common.h b/userspace/libsinsp/mesos_common.h new file mode 100644 index 0000000000..589594b088 --- /dev/null +++ b/userspace/libsinsp/mesos_common.h @@ -0,0 +1,25 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_common.h +// + +#pragma once + + diff --git a/userspace/libsinsp/mesos_component.cpp b/userspace/libsinsp/mesos_component.cpp new file mode 100644 index 0000000000..2d85727700 --- /dev/null +++ b/userspace/libsinsp/mesos_component.cpp @@ -0,0 +1,338 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_component.cpp +// +#ifndef CYGWING_AGENT + +#include "mesos_component.h" +#include "marathon_component.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "json_error_log.h" +#include +#include + +// +// component +// + +const mesos_component::component_map mesos_component::list = +{ + { mesos_component::MESOS_FRAMEWORK, "framework" }, + { mesos_component::MESOS_TASK, "task" }, + { mesos_component::MESOS_SLAVE, "slave" } +}; + +mesos_component::mesos_component(type t, const std::string& name, const std::string& uid) : + m_type(t), + m_name(name), m_uid(uid) +{ + component_map::const_iterator it = list.find(t); + if(it == list.end()) + { + throw sinsp_exception("Invalid Mesos component type: " + std::to_string(t)); + } + + if(m_name.empty()) + { + throw sinsp_exception("Mesos " + it->second + " name cannot be empty"); + } + + if(m_uid.empty()) + { + throw sinsp_exception("Mesos " + it->second + " uid cannot be empty"); + } +} + +mesos_component::mesos_component(const mesos_component& other): m_type(other.m_type), + m_name(other.m_name), + m_uid(other.m_uid), + m_labels(other.m_labels) +{ +} + +mesos_component::mesos_component(mesos_component&& other): m_type(other.m_type), + m_name(std::move(other.m_name)), + m_uid(std::move(other.m_uid)), + m_labels(std::move(other.m_labels)) +{ +} + +mesos_component& mesos_component::operator=(const mesos_component& other) +{ + m_type = other.m_type; + m_name = other.m_name; + m_uid = other.m_uid; + m_labels = other.m_labels; + return *this; +} + +mesos_component& mesos_component::operator=(const mesos_component&& other) +{ + m_type = other.m_type; + m_name = std::move(other.m_name); + m_uid = std::move(other.m_uid); + m_labels = other.m_labels; + return *this; +} + +std::string mesos_component::get_name(type t) +{ + component_map::const_iterator it = list.find(t); + if(it != list.end()) + { + return it->second; + } + + std::ostringstream os; + os << "Unknown component type " << static_cast(t); + throw sinsp_exception(os.str().c_str()); +} + +mesos_component::type mesos_component::get_type(const std::string& name) +{ + if(name == "framework") + { + return MESOS_FRAMEWORK; + } + else if(name == "task") + { + return MESOS_TASK; + } + else if(name == "slave") + { + return MESOS_SLAVE; + } + + std::ostringstream os; + os << "Unknown component name " << name; + throw sinsp_exception(os.str().c_str()); +} + +mesos_pair_t* mesos_component::get_label(const mesos_pair_t& label) +{ + for (auto& lbl : m_labels) + { + if((lbl.first == label.first) && (lbl.second == label.second)) + { + return &lbl; + } + } + return 0; +} + +void mesos_component::add_labels(mesos_pair_list&& labels) +{ + for (auto& label : labels) + { + if(!get_label(label)) + { + emplace_label(std::move(label)); + } + } +} + + +// +// framework +// + +const std::string mesos_framework::MARATHON_ROOT_NAME = "marathon"; + +mesos_framework::mesos_framework(const std::string& name, const std::string& uid) : + mesos_component(mesos_component::MESOS_FRAMEWORK, name, uid) +{ +} + +mesos_framework::~mesos_framework() +{ +} + +bool mesos_framework::is_framework_active(const Json::Value& framework) +{ + const Json::Value& active = framework["active"]; + if(!active.isNull() && active.isBool() && active.asBool()) + { + return true; + } + return false; +} + +void mesos_framework::add_or_replace_task(std::shared_ptr task) +{ + m_tasks.insert({task->get_uid(), task}); +} + +void mesos_framework::remove_task(const std::string& uid) +{ + task_map::iterator it = m_tasks.find(uid); + if(it != m_tasks.end()) + { + m_tasks.erase(it); + return; + } + g_logger.log("Removal of non-existing task (possible deployment failure): " + uid, + sinsp_logger::SEV_WARNING); +} + +const mesos_framework::task_map& mesos_framework::get_tasks() const +{ + return m_tasks; +} + +mesos_framework::task_map& mesos_framework::get_tasks() +{ + return m_tasks; +} + +// +// task +// + +mesos_task::mesos_task(const std::string& name, const std::string& uid) : + mesos_component(mesos_component::MESOS_TASK, name, uid) +{ +} + +mesos_task::~mesos_task() +{ +} + +mesos_task::mesos_task(const mesos_task& other): mesos_component(other), + m_marathon_app_id(other.m_marathon_app_id), + m_slave_id(other.m_slave_id) +{ +} + +mesos_task::mesos_task(mesos_task&& other): mesos_component(std::move(other)), + m_marathon_app_id(std::move(other.m_marathon_app_id)), + m_slave_id(std::move(other.m_slave_id)) +{ +} + +mesos_task& mesos_task::operator=(const mesos_task& other) +{ + mesos_component::operator =(other); + return *this; +} + +mesos_task& mesos_task::operator=(const mesos_task&& other) +{ + mesos_component::operator =(std::move(other)); + return *this; +} + +bool mesos_task::is_task_running(const Json::Value& task) +{ + const Json::Value& task_state = task["state"]; + if(!task_state.isNull() && task_state.isString()) + { + return task_state.asString() == "TASK_RUNNING"; + } + return false; +} + +mesos_task::ptr_t mesos_task::make_task(const Json::Value& task) +{ + //g_logger.log(task.toStyledString(), sinsp_logger::SEV_DEBUG); + std::string name, uid, sid; + Json::Value fid = task["id"]; + if(!fid.isNull()) { uid = fid.asString(); } + else + { + fid = task["taskId"]; + if(!fid.isNull()) { uid = fid.asString(); } + } + Json::Value fname = task["name"]; + if(!fname.isNull()) { name = fname.asString(); } + else + { + std::string::size_type pos = uid.rfind('.'); + if(pos != std::string::npos) + { + name = uid.substr(0, pos); + } + } + + std::shared_ptr t(new mesos_task(name, uid)); + + Json::Value fsid = task["slave_id"]; + if(!fsid.isNull()) { sid = fsid.asString(); } + else + { + Json::Value fsid = task["slaveId"]; + if(!fsid.isNull()) { sid = fsid.asString(); } + } + + if(!sid.empty()) + { + t->set_slave_id(sid); + } + add_labels(t, task); + + return t; +} + +void mesos_task::add_labels(mesos_task::ptr_t task, const Json::Value& t_val) +{ + std::ostringstream os; + if(task) + { + Json::Value labels = t_val["labels"]; + if(!labels.isNull()) + { + for(const auto& label : labels) + { + std::string key, val; + Json::Value lkey = label["key"]; + Json::Value lval = label["value"]; + if(!lkey.isNull()) + { + key = lkey.asString(); + } + if(!lval.isNull()) + { + val = lval.asString(); + } + os << "Adding Mesos task label: [" << key << ':' << val << ']'; + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + os.str(""); + task->emplace_label(mesos_pair_t(key, val)); + } + } + } + else + { + os << "Attempt to add Mesos task labels to null task."; + g_logger.log(os.str(), sinsp_logger::SEV_ERROR); + g_json_error_log.log("", os.str(), sinsp_utils::get_current_time_ns(), "mesos-task-add-labels"); + } +} + +// +// slave +// + +mesos_slave::mesos_slave(const std::string& name, const std::string& uid) : + mesos_component(mesos_component::MESOS_SLAVE, name, uid) +{ +} +#endif // CYGWING_AGENT + diff --git a/userspace/libsinsp/mesos_component.h b/userspace/libsinsp/mesos_component.h new file mode 100644 index 0000000000..93f9088217 --- /dev/null +++ b/userspace/libsinsp/mesos_component.h @@ -0,0 +1,297 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_component.h +// +// mesos components (frameworks, tasks, slaves) +// abstraction +// +#ifndef MINIMAL_BUILD +#pragma once + +#include "json/json.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include +#include +#include + +typedef std::pair mesos_pair_t; +typedef std::vector mesos_pair_list; + +// +// component +// + +class mesos_component +{ +public: + enum type + { + MESOS_FRAMEWORK, + MESOS_TASK, + MESOS_SLAVE + }; + + typedef std::pair component_pair; + typedef std::map component_map; + static const component_map list; + + mesos_component() = delete; + + mesos_component(type t, const std::string& name, const std::string& uid); + + mesos_component(const mesos_component& other); + + mesos_component(mesos_component&& other); + + mesos_component& operator=(const mesos_component& other); + + mesos_component& operator=(const mesos_component&& other); + + const std::string& get_name() const; + + void set_name(const std::string& name); + + const std::string& get_uid() const; + + void set_uid(const std::string& uid); + + mesos_pair_t* get_label(const mesos_pair_t& label); + + const mesos_pair_list& get_labels() const; + + void set_labels(mesos_pair_list&& labels); + + void add_labels(mesos_pair_list&& labels); + + void swap_labels(mesos_pair_list& new_labels); + + void push_label(const mesos_pair_t& label); + + void emplace_label(mesos_pair_t&& label); + + static const std::string& get_name(const component_pair& p); + + static std::string get_name(type t); + + static type get_type(const component_pair& p); + + static type get_type(const std::string& name); + +private: + type m_type; + std::string m_name; + std::string m_uid; + mesos_pair_list m_labels; +}; + + +class mesos_framework; + +// +// task +// + +class mesos_task : public mesos_component +{ +public: + typedef std::shared_ptr ptr_t; + mesos_task(const std::string& name, const std::string& uid); + + mesos_task(const mesos_task& other); + + ~mesos_task(); + + mesos_task(mesos_task&& other); + + mesos_task& operator=(const mesos_task& other); + + mesos_task& operator=(const mesos_task&& other); + + void set_marathon_app_id(const std::string& app_id) + { + m_marathon_app_id = app_id; + } + + const std::string& get_marathon_app_id() const + { + return m_marathon_app_id; + } + + void set_slave_id(const std::string& slave_id) + { + m_slave_id = slave_id; + } + + const std::string& get_slave_id() const + { + return m_slave_id; + } + + static bool is_task_running(const Json::Value& task); + static ptr_t make_task(const Json::Value& task); + static void add_labels(ptr_t task, const Json::Value& t_val); + +private: + std::string m_marathon_app_id; + std::string m_slave_id; +}; + +// +// framework +// + +class mesos_framework : public mesos_component +{ +public: + static const std::string MARATHON_ROOT_NAME; + + typedef std::shared_ptr ptr_t; + typedef mesos_task::ptr_t task_ptr_t; + typedef std::unordered_map task_map; + + mesos_framework(const std::string& name, const std::string& uid); + + ~mesos_framework(); + + bool has_task(const std::string& uid) const; + + task_ptr_t get_task(const std::string& id); + + void add_or_replace_task(std::shared_ptr task); + + void remove_task(const std::string& uid); + + const task_map& get_tasks() const; + + task_map& get_tasks(); + + static bool is_framework_active(const Json::Value& framework); + static bool is_root_marathon(const std::string& name); + +private: + task_map m_tasks; +}; + + +// +// slave +// + +class mesos_slave : public mesos_component +{ +public: + mesos_slave(const std::string& name, const std::string& uid); + +private: +}; + +typedef std::vector mesos_frameworks; +typedef std::vector mesos_slaves; + +// +// component +// + +inline const std::string& mesos_component::get_name() const +{ + return m_name; +} + +inline void mesos_component::set_name(const std::string& name) +{ + m_name = name; +} + +inline const std::string& mesos_component::get_uid() const{ + + return m_uid; +} + +inline void mesos_component::set_uid(const std::string& uid) +{ + m_uid = uid; +} + +inline const mesos_pair_list& mesos_component::get_labels() const +{ + return m_labels; +} + +inline void mesos_component::set_labels(mesos_pair_list&& labels) +{ + m_labels = std::move(labels); +} + +inline void mesos_component::swap_labels(mesos_pair_list& new_labels) +{ + m_labels.swap(new_labels); +} + +inline void mesos_component::push_label(const mesos_pair_t& label) +{ + m_labels.push_back(label); +} + +inline void mesos_component::emplace_label(mesos_pair_t&& label) +{ + m_labels.emplace_back(label); +} + +inline const std::string& mesos_component::get_name(const component_pair& p) +{ + return p.second; +} + +inline mesos_component::type mesos_component::get_type(const component_pair& p) +{ + return p.first; +} + +// +// framework +// + +inline bool mesos_framework::has_task(const std::string& uid) const +{ + return m_tasks.find(uid) != m_tasks.end(); +} + +inline mesos_framework::task_ptr_t mesos_framework::get_task(const std::string& id) +{ + task_map::iterator it = m_tasks.find(id); + if(it != m_tasks.end()) + { + return it->second; + } + return 0; +} + +inline bool mesos_framework::is_root_marathon(const std::string& name) +{ + return name == MARATHON_ROOT_NAME; +} + +// +// task +// + +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/mesos_http.cpp b/userspace/libsinsp/mesos_http.cpp new file mode 100644 index 0000000000..36dd2c1e11 --- /dev/null +++ b/userspace/libsinsp/mesos_http.cpp @@ -0,0 +1,872 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_http.cpp +// +#ifndef CYGWING_AGENT + +#ifdef HAS_CAPTURE + +#include "mesos_http.h" +#include "curl/curl.h" +#include "curl/easy.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "sinsp_curl.h" +#include "json_error_log.h" +#include "mesos.h" +#define BUFFERSIZE 512 // b64 needs this macro +#include "b64/encode.h" +#include +#include +#include +#include +#include + +mesos_http::mesos_http(mesos& m, const uri& url, + bool discover_mesos_lead_master, + bool discover_marathon, + int timeout_ms, const string& token): + m_sync_curl(curl_easy_init()), + m_select_curl(curl_easy_init()), + m_mesos(m), + m_url(url), + m_connected(true), + m_watch_socket(-1), + m_timeout_ms(timeout_ms), + m_callback_func(0), + m_curl_version(curl_version_info(CURLVERSION_NOW)), + m_is_mesos_state(url.to_string().find(mesos::default_state_api) != std::string::npos), + m_discover_lead_master(discover_mesos_lead_master), + m_discover_marathon(discover_marathon), + m_token(token) +{ + if(!m_sync_curl || !m_select_curl) + { + throw sinsp_exception("mesos_http: CURL initialization failed."); + } + + ASSERT(m_curl_version); + + m_request = make_request(url, m_curl_version); + if(!m_token.empty()) + { + m_sync_curl_headers.add(string("Authorization: token=") + m_token); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_HTTPHEADER, m_sync_curl_headers.ptr())); + } + if(m_url.is_secure()) + { + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_SSL_VERIFYPEER, 0)); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_SSL_VERIFYHOST, 0)); + check_error(curl_easy_setopt(m_select_curl, CURLOPT_SSL_VERIFYPEER, 0)); + check_error(curl_easy_setopt(m_select_curl, CURLOPT_SSL_VERIFYHOST, 0)); + } + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_FORBID_REUSE, 1L)); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_CONNECTTIMEOUT_MS, m_timeout_ms)); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_TIMEOUT_MS, m_timeout_ms)); + + check_error(curl_easy_setopt(m_select_curl, CURLOPT_CONNECTTIMEOUT_MS, m_timeout_ms)); + discover_mesos_leader(); +} + +mesos_http::~mesos_http() +{ + cleanup(); +} + +void mesos_http::cleanup() +{ + cleanup(&m_sync_curl); + cleanup(&m_select_curl); +} + +void mesos_http::cleanup(CURL** curl) +{ + if(curl && *curl) + { + curl_easy_cleanup(*curl); + *curl = 0; + } + m_connected = false; +} + +void mesos_http::set_token(const string& token) +{ + m_token = token; + m_request = make_request(m_url, m_curl_version); +} + +Json::Value mesos_http::get_state_frameworks() +{ + Json::Value frameworks; + std::ostringstream os; + CURLcode res = get_data(m_url.to_string(), os); + if(res == CURLE_OK) + { + Json::Value root; + Json::Reader reader; + if(reader.parse(os.str(), root)) + { + frameworks = root["frameworks"]; + if(frameworks.isNull() || !frameworks.isArray()) + { + throw sinsp_exception("mesos_http: Unexpected condition while detecting Mesos master: frameworks entry not found."); + } + } + else + { + std::string errstr; + errstr = reader.getFormattedErrorMessages(); + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + throw sinsp_exception("mesos_http: Mesos master leader detection failed in get_state_frameworks(): Invalid JSON (" + errstr + ")"); + } + } + else + { + throw sinsp_exception(std::string("mesos_http: Mesos master leader [") + m_url.to_string(false) + + "] detection failed: " + curl_easy_strerror(res)); + } + return frameworks; +} + +void mesos_http::discover_mesos_leader() +{ + if(m_is_mesos_state) + { + g_logger.log("mesos_http: Inspecting Mesos leader [" + m_url.to_string(false) + ']', sinsp_logger::SEV_DEBUG); + std::ostringstream os; + CURLcode res = get_data(m_url.to_string(), os); + if(res == CURLE_OK) + { + long http_response_code = 0; + check_error(curl_easy_getinfo(m_sync_curl, CURLINFO_RESPONSE_CODE, &http_response_code)); + if(sinsp_curl::is_redirect(http_response_code)) + { + uri newurl(m_redirect); + m_url.set_host(newurl.get_host()); + g_logger.log("mesos_http: Detected Mesos master leader HTTP redirect: [" + m_url.to_string(false) + ']', sinsp_logger::SEV_INFO); + discover_mesos_leader(); + return; + } + Json::Value root; + Json::Reader reader; + if(reader.parse(os.str(), root)) + { + const Json::Value& frameworks = root["frameworks"]; + if(frameworks.isNull() || !frameworks.isArray()) + { + throw sinsp_exception("mesos_http: Unexpected condition while detecting Mesos master: frameworks entry not found."); + } + g_logger.log("Found " + std::to_string(frameworks.size()) + " Mesos frameworks", sinsp_logger::SEV_DEBUG); + if(frameworks.size()) // this is master leader + { + discover_framework_uris(frameworks); + g_logger.log("mesos_http: Found Mesos master leader [" + m_url.to_string(false) + ']', sinsp_logger::SEV_INFO); + return; + } + else if(!m_discover_lead_master) // this is standby server and autodiscovery is disabled + { + throw sinsp_exception("mesos_http: Detected standby Mesos master: autodiscovery not enabled. Giving up (will retry)."); + } + else // autodiscovery is enabled, find where is the master + { + const Json::Value& leader = root["leader"]; + if(!leader.isNull() && leader.isString()) + { + std::string leader_address = leader.asString(); + std::string::size_type pos = leader_address.find('@'); + if(pos != std::string::npos && (pos + 1) < leader_address.size()) + { + std::string address = m_url.get_scheme() + "://"; + if(!m_mesos.m_mesos_credentials.first.empty()) + { + address.append(m_mesos.m_mesos_credentials.first).append(1, ':').append(m_mesos.m_mesos_credentials.second).append(1, '@'); + } + address.append(leader_address.substr(pos + 1)).append(mesos::default_state_api); + if(address != m_url.to_string(true)) + { + g_logger.log("mesos_http: Detected Mesos master leader redirect: [" + uri(address).to_string(false) + ']', sinsp_logger::SEV_INFO); + m_url = address; + discover_mesos_leader(); + } + else + { + throw sinsp_exception("mesos_http: Mesos master leader not discovered at [" + uri(address).to_string(false) + "] . " + "Giving up temporarily ..."); + } + } + else + { + throw sinsp_exception("mesos_http: Unexpected leader entry format while detecting Mesos master: " + leader_address); + } + } + else + { + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + throw sinsp_exception("mesos_http: Unexpected condition while detecting Mesos master leader [" + m_url.to_string(false) + + "]: leader entry not found."); + } + } + } + else + { + std::string errstr; + errstr = reader.getFormattedErrorMessages(); + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + throw sinsp_exception("mesos_http: Mesos master leader [" + m_url.to_string(false) + "] detection failed: Invalid JSON (" + errstr + ")"); + } + } + else + { + throw sinsp_exception("mesos_http: Mesos master leader [" + m_url.to_string(false) + "] detection failed: " + + curl_easy_strerror(res)); + } + } +} + +std::string mesos_http::get_framework_url(const Json::Value& framework) +{ + const Json::Value& fw_name = framework["name"]; + bool is_marathon = false; + if(!fw_name.isNull() && fw_name.isConvertibleTo(Json::stringValue)) + { + is_marathon = mesos_framework::is_root_marathon(fw_name.asString()); + } + bool has_creds = !m_mesos.m_marathon_credentials.first.empty(); + Json::Value fw_url = framework["webui_url"]; + if(!fw_url.isNull() && fw_url.isString() && !fw_url.asString().empty()) + { + uri url(fw_url.asString()); + if(is_marathon && has_creds) + { + url.set_credentials(m_mesos.m_marathon_credentials); + } + return url.to_string(true); + } + else + { + fw_url = framework["hostname"]; + if(!fw_url.isNull() && fw_url.isString() && !fw_url.asString().empty()) + { + uri url(std::string("https://").append(fw_url.asString()).append(":8080")); + if(is_marathon && has_creds) + { + url.set_credentials(m_mesos.m_marathon_credentials); + } + return url.to_string(true); + } + } + return ""; +} + +bool mesos_http::is_framework_active(const Json::Value& framework) +{ + Json::Value active = framework["active"]; + if(!active.isNull() && active.isBool() && active.asBool()) + { + return true; + } + return false; +} + +void mesos_http::discover_framework_uris(const Json::Value& frameworks) +{ + m_marathon_uris.clear(); + if(frameworks.isNull()) + { + throw sinsp_exception("mesos_http: Unexpected condition while inspecting Marathon framework: frameworks entry not found."); + } + if(frameworks.isArray()) + { + g_logger.log("Discovered " + std::to_string(frameworks.size()) + " frameworks.", sinsp_logger::SEV_DEBUG); + for(const auto& framework : frameworks) + { + const Json::Value& id = framework["id"]; + if(id.isNull() || !id.isString()) + { + throw sinsp_exception("mesos_http: Unexpected condition while detecting Marathon framework: ID entry not found."); + } + else + { + const Json::Value& active = framework["active"]; + const Json::Value& fw_name = framework["name"]; + std::string name; + if(!fw_name.isNull() && fw_name.isString()) + { + name = fw_name.asString(); + } + g_logger.log("Examining " + name + " [" + id.asString() + "] framework.", sinsp_logger::SEV_DEBUG); + if(!active.isNull() && active.isBool() && active.asBool()) + { + std::string framework_url = get_framework_url(framework); + if(!framework_url.empty()) + { + if(m_discover_marathon) + { + if(mesos_framework::is_root_marathon(name)) + { + g_logger.log(std::string("mesos_http: Found Marathon framework ").append(name).append(" (").append(id.asString()).append(") at [").append(framework_url).append(1, ']'), + sinsp_logger::SEV_INFO); + if(!m_marathon_uris.size()) + { + m_marathon_uris.emplace_back(get_framework_url(framework)); + } + else + { + g_logger.log("mesos_http: Multiple marathon URIs discovered; only the first one (" + m_marathon_uris[0] + ") will have effect;" + " others will be treated as generic frameworks.", sinsp_logger::SEV_WARNING); + } + } + else + { + g_logger.log(std::string("mesos_http: Skipping non-Marathon framework URL detection ").append(name).append(" (").append(id.asString()).append(1, ')'), sinsp_logger::SEV_DEBUG); + } + } + else + { + g_logger.log(std::string("mesos_http: Marathon detection not enabled."), sinsp_logger::SEV_DEBUG); + } + } + else + { + if(m_discover_marathon && mesos_framework::is_root_marathon(name)) + { + std::string errstr = "mesos_http: Can not obtain URL for Marathon framework."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + + } + } + } + else // framework exists, but is not active - remove it if we were watching it so far + { + g_logger.log(std::string("mesos_http: Mesos framework ").append(name).append(" (").append(id.asString()).append(") deactivated."), sinsp_logger::SEV_INFO); + std::string framework_url = get_framework_url(framework); + for(marathon_uri_t::iterator it = m_marathon_uris.begin(); it != m_marathon_uris.end();) + { + if(framework_url == *it) + { + it = m_marathon_uris.erase(it); + } + else { ++it; } + } + } + } + } + } + else + { + throw sinsp_exception("mesos_http: Mesos master leader detection failed: " + m_url.to_string(false)); + } +} + +std::string mesos_http::make_request(uri url, curl_version_info_data* curl_version) +{ + std::ostringstream request; + std::string host_and_port = url.get_host(); + int port = url.get_port(); + if(port) + { + host_and_port.append(1, ':').append(std::to_string(port)); + } + request << "GET " << url.get_path(); + std::string query = url.get_query(); + if(!query.empty()) + { + request << '?' << query; + } + request << " HTTP/1.1\r\nConnection: Keep-Alive\r\nUser-Agent: sysdig"; + if(curl_version && curl_version->version) + { + request << " (curl " << curl_version->version << ')'; + } + request << "\r\nHost: " << host_and_port << "\r\nAccept: */*\r\n"; + std::string creds = url.get_credentials(); + if(!creds.empty()) + { + std::istringstream is(creds); + std::ostringstream os; + base64::encoder().encode(is, os); + request << "Authorization: Basic " << os.str() << "\r\n"; + } + if(!m_token.empty()) + { + request << "Authorization: token=" << m_token << "\r\n"; + } + request << "\r\n"; + + return request.str(); +} + +CURLcode mesos_http::get_data(const std::string& url, std::ostream& os) +{ + g_logger.log(std::string("mesos_http: Retrieving data from ") + uri(url).to_string(false), sinsp_logger::SEV_DEBUG); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_URL, url.c_str())); + + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_HEADERDATA, m_redirect)); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_HEADERFUNCTION, sinsp_curl::header_callback)); + + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_NOSIGNAL, 1)); //Prevent "longjmp causes uninitialized stack frame" bug + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_ACCEPT_ENCODING, "deflate")); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_TIMEOUT_MS, m_timeout_ms)); + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_WRITEFUNCTION, sinsp_curl::write_data)); + + check_error(curl_easy_setopt(m_sync_curl, CURLOPT_WRITEDATA, &os)); + return curl_easy_perform(m_sync_curl); +} + +bool mesos_http::get_all_data(callback_func_t parse) +{ + std::ostringstream os; + CURLcode res = get_data(m_url.to_string(), os); + if(res != CURLE_OK) + { + std::string errstr = std::string("Could not fetch url:") + curl_easy_strerror(res); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + m_connected = false; + } + else + { + // HTTP errors are not returned by curl API + // error will be in the response stream + long http_code = 0; + curl_easy_getinfo(m_sync_curl, CURLINFO_RESPONSE_CODE, &http_code); + if(http_code >= 400) + { + m_connected = false; + return false; + } + else if(sinsp_curl::is_redirect(http_code)) + { + g_logger.log("mesos_http: HTTP redirect (" + std::to_string(http_code) + ')', sinsp_logger::SEV_DEBUG); + if(sinsp_curl::handle_redirect(m_url, std::string(m_redirect), os)) + { + os.str(""); + return get_all_data(parse); + } + } + Json::Reader reader; + json_ptr_t root(new Json::Value()); + if(reader.parse(os.str(), *root)) + { + (m_mesos.*parse)(root, m_framework_id); + } + else + { + std::string errstr; + errstr = reader.getFormattedErrorMessages(); + g_logger.log("mesos_http: Mesos or Marathon Invalid JSON received from [" + m_url.to_string(false) + "]: " + errstr, sinsp_logger::SEV_WARNING); + g_logger.log("JSON: <" + os.str() + '>', sinsp_logger::SEV_DEBUG); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + } + m_connected = true; + } + return res == CURLE_OK; +} + +int mesos_http::wait(int for_recv) +{ + struct timeval tv; + fd_set infd, outfd, errfd; + int res; + + tv.tv_sec = m_timeout_ms / 1000; + tv.tv_usec = (m_timeout_ms % 1000) * 1000; + + FD_ZERO(&infd); + FD_ZERO(&outfd); + FD_ZERO(&errfd); + FD_SET(m_watch_socket, &errfd); + + if(for_recv) + { + FD_SET(m_watch_socket, &infd); + } + else + { + FD_SET(m_watch_socket, &outfd); + } + + res = select(m_watch_socket + 1, &infd, &outfd, &errfd, &tv); + return res; +} + +int mesos_http::get_socket(long timeout_ms) +{ + if(m_request.empty()) + { + throw sinsp_exception("mesos_http: Cannot create watch socket (request empty)."); + } + + if(timeout_ms != -1) + { + m_timeout_ms = timeout_ms; + } + + if(m_watch_socket < 0 || !m_connected) + { + long sockextr; + std::string url = get_url().to_string(); + + check_error(curl_easy_setopt(m_select_curl, CURLOPT_URL, url.c_str())); + check_error(curl_easy_setopt(m_select_curl, CURLOPT_CONNECT_ONLY, 1L)); + +#if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 25 + // enable TCP keep-alive for this transfer + check_error(curl_easy_setopt(m_select_curl, CURLOPT_TCP_KEEPALIVE, 1L)); + // keep-alive idle time + check_error(curl_easy_setopt(m_select_curl, CURLOPT_TCP_KEEPIDLE, 300L)); + // interval time between keep-alive probes + check_error(curl_easy_setopt(m_select_curl, CURLOPT_TCP_KEEPINTVL, 10L)); +#endif // LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 25 + + check_error(curl_easy_perform(m_select_curl)); + + check_error(curl_easy_getinfo(m_select_curl, CURLINFO_LASTSOCKET, &sockextr)); + m_watch_socket = sockextr; + + if(!wait(0)) + { + throw sinsp_exception("mesos_http: Error obtaining socket: timeout."); + } + + g_logger.log(std::string("mesos_http: Connected; collecting data from ") + uri(url).to_string(false), sinsp_logger::SEV_DEBUG); + } + + if(m_watch_socket <= 0) + { + throw sinsp_exception("mesos_http: Error obtaining socket: " + std::to_string(m_watch_socket)); + } + + m_connected = true; + return m_watch_socket; +} + +void mesos_http::send_request() +{ + if(m_request.empty()) + { + throw sinsp_exception("mesos_http: Mesos send request (empty)."); + } + + if(m_watch_socket < 0) + { + throw sinsp_exception("mesos_http: Mesos send invalid socket."); + } + + //size_t iolen = send(m_watch_socket, m_request.c_str(), m_request.size(), 0); + size_t iolen; + check_error(curl_easy_send(m_select_curl, m_request.c_str(), m_request.size(), &iolen)); + if((iolen <= 0) || (m_request.size() != iolen)) + { + throw sinsp_exception("mesos_http: Mesos send socket connection error."); + } + else if(!wait(0)) + { + throw sinsp_exception("mesos_http: Mesos send timeout."); + } + g_logger.log(m_request, sinsp_logger::SEV_DEBUG); +} + +bool purge_chunked_markers(std::string& data) +{ + std::string::size_type pos = data.find("}\r\n0"); + if(pos != std::string::npos) + { + data = data.substr(0, pos); + } + + const std::string nl = "\r\n"; + std::string::size_type begin, end; + while((begin = data.find(nl)) != std::string::npos) + { + end = data.find(nl, begin + 2); + if(end != std::string::npos) + { + data.erase(begin, end + 2 - begin); + } + else // newlines must come in pairs + { + return false; + } + } + return true; +} + +void mesos_http::handle_json(std::string::size_type end_pos, bool chunked) +{ + if(end_pos != std::string::npos) + { + if(m_data_buf.length() >= end_pos + 1) + { + m_data_buf = m_data_buf.substr(0, end_pos + 1); + if(chunked && !purge_chunked_markers(m_data_buf)) + { + std::string errstr = "mesos_http: Invalid Mesos or Marathon JSON data detected (chunked transfer)."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(m_data_buf, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + (m_mesos.*m_callback_func)(nullptr, m_framework_id); + } + else + { + (m_mesos.*m_callback_func)(try_parse(m_data_buf, m_url.to_string()), m_framework_id); + } + m_data_buf.clear(); + m_content_length = std::string::npos; + } + } +} + +bool mesos_http::detect_chunked_transfer(const std::string& data) +{ + if(m_content_length == std::string::npos) + { + std::string::size_type cl_pos = data.find("Content-Length:"); + if(cl_pos != std::string::npos) + { + std::string::size_type nl_pos = data.find("\r\n", cl_pos); + if(nl_pos != std::string::npos) + { + cl_pos += std::string("Content-Length:").length(); + std::string cl = data.substr(cl_pos, nl_pos - cl_pos); + long len = strtol(cl.c_str(), NULL, 10); + if(len == 0L || len == LONG_MAX || len == LONG_MIN || errno == ERANGE) + { + std::string errstr = "Invalid HTTP content length from [: " + m_url.to_string(false) + ']' + + std::to_string(len); + (m_mesos.*m_callback_func)(nullptr, m_framework_id); + m_data_buf.clear(); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(data, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + return false; + } + else + { + m_content_length = static_cast(len); + } + } + } + } + return true; +} + +void mesos_http::extract_data(std::string& data) +{ + if(!detect_chunked_transfer(data)) + { + string errstr = "mesos_http: An error occurred while detecting chunked transfer."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(data, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + return; + } + + if(m_data_buf.empty()) + { + m_data_buf = data; + std::string::size_type pos = m_data_buf.find("\r\n{"); + if(pos != std::string::npos) // JSON begin + { + m_data_buf = m_data_buf.substr(pos + 2); + } + } + else + { + m_data_buf.append(data); + } + bool chunked = (m_content_length == std::string::npos); + if(chunked) + { + handle_json(m_data_buf.find("}\r\n0"), true); + } + else if (m_data_buf.length() >= m_content_length) + { + handle_json(m_data_buf.length() - 1, false); + } + return; +} + +bool mesos_http::on_data() +{ + if(!m_callback_func) + { + throw sinsp_exception("mesos_http: Cannot parse data (parse function null)."); + } + + size_t iolen = 0; + char buf[1024]; + buf[0] = '\0'; + std::string data; + CURLcode ret; + std::string errstr; + try + { + do + { + check_error(ret = curl_easy_recv(m_select_curl, buf, sizeof(buf), &iolen)); + if(iolen > 0) + { + data.append(buf, iolen); + } + else if(ret != CURLE_AGAIN) { goto connection_closed; } + } while(iolen && ret != CURLE_AGAIN); + if(data.size()) + { + extract_data(data); + } + } + catch(const sinsp_exception& ex) + { + errstr = std::string("mesos_http: Data receive error [" + m_url.to_string() + "]: ").append(ex.what()); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(buf, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + return false; + } + return true; + +connection_closed: + errstr = "mesos_http: Mesos or Marathon API connection [" + m_url.to_string() + "] closed."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(buf, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string()); + m_connected = false; + return false; +} + +void mesos_http::on_error(const std::string& /*err*/, bool /*disconnect*/) +{ + m_connected = false; +} + +void mesos_http::check_error(CURLcode res) +{ + if(CURLE_OK != res && CURLE_AGAIN != res) + { + m_connected = false; + std::ostringstream os; + os << "Error: " << curl_easy_strerror(res); + throw sinsp_exception(os.str()); + } +} + +std::string mesos_http::make_uri(const std::string& path) +{ + uri url = get_url(); + std::string target_uri = url.get_scheme(); + target_uri.append("://"); + std::string user = url.get_user(); + if(!user.empty()) + { + target_uri.append(user).append(1, ':').append(url.get_password()).append(1, '@'); + } + target_uri.append(url.get_host()); + int port = url.get_port(); + if(port) + { + target_uri.append(1, ':').append(std::to_string(port)); + } + target_uri.append(path); + return target_uri; +} + +Json::Value mesos_http::get_task_labels(const std::string& task_id) +{ + std::ostringstream os; + std::string uri = make_uri("/master/tasks"); + CURLcode res = get_data(uri, os); + + Json::Value labels; + if(res != CURLE_OK) + { + std::string errstr = curl_easy_strerror(res); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(task_id, errstr, sinsp_utils::get_current_time_ns(), uri); + return labels; + } + + try + { + Json::Value root; + Json::Reader reader; + if(reader.parse(os.str(), root, false)) + { + Json::Value tasks = root["tasks"]; + if(!tasks.isNull()) + { + for(const auto& task : tasks) + { + Json::Value id = task["id"]; + if(!id.isNull() && id.isString() && id.asString() == task_id) + { + Json::Value statuses = task["statuses"]; + if(!statuses.isNull()) + { + double tstamp = 0.0; + for(const auto& status : statuses) + { + // only task with most recent status + // "TASK_RUNNING" considered + Json::Value ts = status["timestamp"]; + if(!ts.isNull() && ts.isNumeric() && ts.asDouble() > tstamp) + { + Json::Value st = status["state"]; + if(!st.isNull() && st.isString()) + { + if(st.asString() == "TASK_RUNNING") + { + labels = task["labels"]; + tstamp = ts.asDouble(); + } + else + { + labels.clear(); + } + } + } + } + if(!labels.empty()) // currently running task found + { + return labels; + } + } + } + } + } + } + else + { + std::string errstr; + errstr = "mesos_http: Error parsing tasks (" + reader.getFormattedErrorMessages() + ")."; + g_logger.log(errstr + "\nJSON:\n---\n" + os.str() + "\n---", sinsp_logger::SEV_ERROR); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri); + } + } + catch(const std::exception& ex) + { + std::string errstr = std::string("mesos_http: Error parsing tasks:") + ex.what(); + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri); + } + + return labels; +} + +#endif // HAS_CAPTURE +#endif // CYGWING_AGENT diff --git a/userspace/libsinsp/mesos_http.h b/userspace/libsinsp/mesos_http.h new file mode 100644 index 0000000000..69aac8e19e --- /dev/null +++ b/userspace/libsinsp/mesos_http.h @@ -0,0 +1,266 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_http.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#ifdef HAS_CAPTURE + +#include "curl/curl.h" +#include "uri.h" +#include "json/json.h" +#include +#include +#include +#include +#include "sinsp_curl.h" +#include "json_error_log.h" + +class mesos; + +class mesos_http +{ +public: + typedef std::shared_ptr ptr_t; + typedef std::shared_ptr json_ptr_t; + typedef void (mesos::*callback_func_t)(json_ptr_t, const std::string&); + typedef std::vector marathon_uri_t; + + mesos_http(mesos& m, const uri& url, + bool discover_mesos_lead_master = false, + bool discover_marathon = false, + int timeout_ms = 5000L, + const string& token = ""); + + virtual ~mesos_http(); + + bool get_all_data(callback_func_t); + + virtual int get_socket(long timeout_ms = -1); + + virtual bool is_connected() const; + + virtual bool on_data(); + + virtual void on_error(const std::string& err, bool disconnect); + + const uri& get_url() const; + const std::string& get_request() const; + + std::string make_uri(const std::string& path); + + Json::Value get_task_labels(const std::string& task_id); + + void set_parse_func(callback_func_t parse); + + const std::string& get_framework_id() const; + void set_framework_id(const std::string& id); + const std::string& get_framework_name() const; + void set_framework_name(const std::string& id); + const std::string& get_framework_version() const; + void set_framework_version(const std::string& id); + + const marathon_uri_t& get_marathon_uris() const; + void set_token(const string& token); + +protected: + CURL* get_sync_curl(); + CURL* get_select_curl(); + mesos& get_mesos(); + CURLcode get_data(const std::string& url, std::ostream& os); + void check_error(CURLcode res); + void cleanup(); + void cleanup(CURL**); + int wait(int for_recv); + + callback_func_t get_parse_func(); + std::string make_request(uri url, curl_version_info_data* m_curl_version = 0); + static json_ptr_t try_parse(const std::string& json, const std::string &uri); + static bool is_framework_active(const Json::Value& framework); + std::string get_framework_url(const Json::Value& framework); + +private: + void discover_mesos_leader(); + Json::Value get_state_frameworks(); + void discover_framework_uris(const Json::Value& frameworks); + + void send_request(); + + CURL* m_sync_curl; + CURL* m_select_curl; + mesos& m_mesos; + std::string m_protocol; + uri m_url; + bool m_connected; + curl_socket_t m_watch_socket; + long m_timeout_ms; + callback_func_t m_callback_func; + std::string m_data_buf; + std::string m_framework_id; + std::string m_framework_name; + std::string m_framework_version; + curl_version_info_data* m_curl_version; + std::string m_request; + bool m_is_mesos_state; + marathon_uri_t m_marathon_uris; + bool m_discover_lead_master; + bool m_discover_marathon; + //bool m_redirect = false; + std::string::size_type m_content_length = std::string::npos; + char m_redirect[CURL_MAX_HTTP_HEADER] = {0}; + string m_token; + sinsp_curl_http_headers m_sync_curl_headers; + + friend class mesos; + + void extract_data(std::string& data); + void handle_data(); + bool detect_chunked_transfer(const std::string& data); + void handle_json(std::string::size_type end_pos, bool chunked); +}; + +inline bool mesos_http::is_connected() const +{ + return m_connected; +} + +inline const uri& mesos_http::get_url() const +{ + return m_url; +} + +inline CURL* mesos_http::get_sync_curl() +{ + return m_sync_curl; +} + +inline CURL* mesos_http::get_select_curl() +{ + return m_select_curl; +} + +inline mesos& mesos_http::get_mesos() +{ + return m_mesos; +} + +inline const std::string& mesos_http::get_request() const +{ + return m_request; +} + +inline void mesos_http::set_parse_func(callback_func_t parse) +{ + m_callback_func = parse; +} + +inline mesos_http::callback_func_t mesos_http::get_parse_func() +{ + return m_callback_func; +} + +inline mesos_http::json_ptr_t mesos_http::try_parse(const std::string& json, const std::string &uri) +{ + json_ptr_t root(new Json::Value()); + try + { + if(Json::Reader().parse(json, *root)) + { + return root; + } + else + { + std::string errstr; + errstr = Json::Reader().getFormattedErrorMessages(); + g_logger.log("mesos_http::try_parse could not parse json (" + errstr + ")", sinsp_logger::SEV_WARNING); + g_json_error_log.log(json, errstr, sinsp_utils::get_current_time_ns(), uri); + } + } + catch(const Json::Exception &e) + { + g_logger.log("Could not parse JSON document: " + string(e.what()), sinsp_logger::SEV_WARNING); + g_json_error_log.log(json, e.what(), sinsp_utils::get_current_time_ns(), uri); + } + catch(...) { } + return nullptr; +} + +inline const std::string& mesos_http::get_framework_id() const +{ + return m_framework_id; +} + +inline void mesos_http::set_framework_id(const std::string& id) +{ + m_framework_id = id; +} + +inline const std::string& mesos_http::get_framework_name() const +{ + return m_framework_name; +} + +inline void mesos_http::set_framework_name(const std::string& name) +{ + m_framework_name = name; +} + +inline const std::string& mesos_http::get_framework_version() const +{ + return m_framework_version; +} + +inline void mesos_http::set_framework_version(const std::string& version) +{ + m_framework_version = version; +} + +inline const mesos_http::marathon_uri_t& mesos_http::get_marathon_uris() const +{ + return m_marathon_uris; +} + +#else // !HAS_CAPTURE + +#include "json/json.h" +#include + +class mesos_http +{ +public: + typedef std::shared_ptr json_ptr_t; + static json_ptr_t try_parse(const std::string& json, const std::string &uri) + { + json_ptr_t root(new Json::Value()); + try + { + if(Json::Reader().parse(json, *root)) + { + return root; + } + } + catch(...) { } + return nullptr; + } +}; + +#endif // HAS_CAPTURE +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/mesos_state.cpp b/userspace/libsinsp/mesos_state.cpp new file mode 100644 index 0000000000..f4f4a521f2 --- /dev/null +++ b/userspace/libsinsp/mesos_state.cpp @@ -0,0 +1,585 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// k8s_state.cpp +// +#ifndef CYGWING_AGENT + +#include "mesos_state.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include +#include +#include + +// +// state +// + +mesos_state_t::mesos_state_t(bool is_captured, bool verbose) : + m_verbose(verbose) +#ifdef HAS_CAPTURE + , m_is_captured(is_captured) +#endif // HAS_CAPTURE +{ +} + +mesos_framework::task_ptr_t mesos_state_t::get_task(const std::string& uid) const +{ + for(auto& framework : get_frameworks()) + { + for(auto& task : framework.get_tasks()) + { + if(task.first == uid) + { + return task.second; + } + } + } + g_logger.log("Task not found: " + uid, sinsp_logger::SEV_WARNING); + return 0; +} + +std::unordered_set mesos_state_t::get_all_task_ids() const +{ + std::unordered_set tasks; + for(const auto& framework : m_frameworks) + { + for(const auto& task : framework.get_tasks()) + { + tasks.insert(task.first); + } + } + return tasks; +} + +const mesos_framework::task_map& mesos_state_t::get_tasks(const std::string& framework_uid) const +{ + for(const auto& framework : m_frameworks) + { + if(framework.get_uid() == framework_uid) + { + return framework.get_tasks(); + } + } + throw sinsp_exception("Framework not found: " + framework_uid); +} + +mesos_framework::task_map& mesos_state_t::get_tasks(const std::string& framework_uid) +{ + for(auto& framework : m_frameworks) + { + if(framework.get_uid() == framework_uid) + { + return framework.get_tasks(); + } + } + throw sinsp_exception("Framework not found: " + framework_uid); +} + +marathon_app::ptr_t mesos_state_t::get_app(const std::string& app_id) +{ + marathon_group::ptr_t group = get_app_group(app_id); + if(group) + { + g_logger.log("Found group for app [" + app_id + "]: " + group->get_id(), sinsp_logger::SEV_DEBUG); + return group->get_app(app_id); + } + return 0; +} + +marathon_app::ptr_t mesos_state_t::get_app(mesos_task::ptr_t task) const +{ + for(const auto& group : m_groups) + { + marathon_app::ptr_t app = group.second->get_app(task); + if(app) + { + return app; + } + } + return 0; +} + +marathon_group::ptr_t mesos_state_t::get_group(mesos_task::ptr_t task) const +{ + for(const auto& group : m_groups) + { + marathon_group::ptr_t grp = group.second->get_group(task); + if(grp) + { + return grp; + } + } + return 0; +} + +marathon_group::app_ptr_t mesos_state_t::add_or_replace_app(const std::string& app_id, + const std::string& group_id, + const std::string& task_id) +{ + marathon_group::app_ptr_t app = get_app(app_id); + if(!app) + { + app = std::make_shared(app_id); + g_logger.log("Created app [" + app_id + ']', sinsp_logger::SEV_DEBUG); + } + else + { + g_logger.log("Found app [" + app_id + ']', sinsp_logger::SEV_DEBUG); + } + if(!app) + { + std::string errstr = "Could not find or create app [" + app_id + ']'; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(app_id, errstr, sinsp_utils::get_current_time_ns(), "add-replace-app"); + return 0; + } + + if(!task_id.empty()) + { + g_logger.log("Adding task [" + task_id + "] to app [" + app_id + ']', sinsp_logger::SEV_DEBUG); + add_task_to_app(app, task_id); + } + + marathon_group::ptr_t group = get_group(group_id); + if(group) + { + g_logger.log("Adding app [" + app_id + "] to group [" + group_id + ']', sinsp_logger::SEV_DEBUG); + group->add_or_replace_app(app); + } + + return app; +} + +void mesos_state_t::add_task_to_app(marathon_group::app_ptr_t app, const std::string& task_id) +{ + if(app) + { + mesos_framework::task_ptr_t pt = get_task(task_id); + if(pt) + { + app->add_task(pt); + } + else + { + std::string errstr = "Task [" + task_id + "] can not be obtained (null). Task not added to app [" + app->get_id() + ']'; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(task_id, errstr, sinsp_utils::get_current_time_ns(), "add-task-to-app"); + } + } + else + { + std::string errstr = "Attempt to add task [" + task_id + "] to non-existing (null) app."; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(task_id, errstr, sinsp_utils::get_current_time_ns(), "add-task-to-app"); + } +} + +marathon_group::ptr_t mesos_state_t::get_app_group(const std::string& app_id) +{ + std::string group_id = marathon_app::get_group_id(app_id); + if(!group_id.empty()) + { + return get_group(group_id); + } + return 0; +} + +bool mesos_state_t::remove_app(const std::string& app_id) +{ + marathon_group::ptr_t group = get_group(app_id); + if(group) + { + return group->remove_app(app_id); + } + return false; +} + +marathon_group::ptr_t mesos_state_t::get_group(const std::string& group_id) +{ + marathon_groups::iterator it = m_groups.find(group_id); + if(it != m_groups.end()) + { + return it->second; + } + else + { + for(auto group : m_groups) + { + if(marathon_group::ptr_t p_group = group.second->get_group(group_id)) + { + return p_group; + } + } + } + return 0; +} + +marathon_group::ptr_t mesos_state_t::add_or_replace_group(marathon_group::ptr_t group, marathon_group::ptr_t to_group) +{ + std::string id = group->get_id(); + if(!to_group) // top level + { + marathon_groups::iterator it = m_groups.find(id); + if(it != m_groups.end()) + { + m_groups.erase(it); + } + m_groups.insert({id, group}); + } + else + { + to_group->add_or_replace_group(group); + } + return group; +} + +bool mesos_state_t::handle_groups(const Json::Value& root, marathon_group::ptr_t to_group, const std::string& framework_id) +{ + Json::Value groups = root["groups"]; + if(!groups.isNull() && groups.isArray()) + { + for(const auto& group : groups) + { + add_group(group, to_group, framework_id); + } + } + else + { + g_logger.log("No groups found.", sinsp_logger::SEV_WARNING); + return false; + } + return true; +} + +#ifdef HAS_CAPTURE +void mesos_state_t::capture_groups(const Json::Value& root, const std::string& framework_id, Json::Value& capt, bool capture_fw) +{ + if(!m_is_captured) { return; } + + capt["id"] = root["id"]; + const Json::Value& apps = root["apps"]; + if(!apps.isNull()) + { + if(capture_fw) + { + capt["frameworkId"] = framework_id; + } + capt["apps"] = Json::arrayValue; + for(const auto& app : apps) + { + Json::Value& c_app = capt["apps"].append(Json::Value()); + c_app["id"] = app["id"]; + + // labels + const Json::Value& labels = app["labels"]; + if(!labels.isNull()) + { + c_app["labels"] = Json::objectValue; + Json::Value::Members members = labels.getMemberNames(); + for (auto& member : members) + { + c_app["labels"][member] = labels[member]; + } + } + } + } + + const Json::Value& groups = root["groups"]; + if(!groups.isNull()) + { + capt["groups"] = Json::arrayValue; + for(const auto& group : groups) + { + Json::Value& c_group = capt["groups"].append(Json::objectValue); + capture_groups(group, framework_id, c_group); + } + } +} + +void mesos_state_t::capture_apps(const Json::Value& root, const std::string& framework_id) +{ + if(!m_is_captured) { return; } + + Json::Value capt; + const Json::Value& apps = root["apps"]; + if(!apps.isNull()) + { + capt["frameworkId"] = framework_id; + capt["apps"] = Json::arrayValue; + for(const auto& app : apps) + { + Json::Value& c_app = capt["apps"].append(Json::Value()); + c_app["id"] = app["id"]; + + // labels + const Json::Value& labels = app["labels"]; + if(!labels.isNull()) + { + c_app["labels"] = Json::objectValue; + Json::Value::Members members = labels.getMemberNames(); + for (auto& member : members) + { + c_app["labels"][member] = labels[member]; + } + } + + // tasks + const Json::Value& tasks = app["tasks"]; + if(!tasks.isNull()) + { + c_app["tasks"] = Json::arrayValue; + for(const auto& task : tasks) + { + Json::Value& c_task = c_app["tasks"].append(Json::objectValue); + c_task["id"] = task["id"]; + c_task["host"] = task["host"]; + c_task["slaveId"] = task["slaveId"]; + c_task["appId"] = task["appId"]; + } + } + } + } + enqueue_capture_event(capture::MARATHON_APPS, Json::FastWriter().write(capt)); +} +#endif // HAS_CAPTURE + +bool mesos_state_t::parse_groups(Json::Value&& root, const std::string& framework_id) +{ + add_group(root, 0, framework_id); +#ifdef HAS_CAPTURE + if(m_is_captured) + { + Json::Value capt; + capture_groups(root, framework_id, capt, true); + enqueue_capture_event(capture::MARATHON_GROUPS, Json::FastWriter().write(capt)); + } +#endif // HAS_CAPTURE + if(m_verbose) + { + std::cout << Json::FastWriter().write(root) << std::endl; + } + return true; +} + +bool mesos_state_t::parse_groups(json_ptr_t json, const std::string& framework_id) +{ + if(json && !json->isNull() && !(*json)["id"].isNull()) + { + return parse_groups(std::move(*json), framework_id); + } + else + { + throw sinsp_exception("Marathon groups parsing failed (Invalid JSON)."); + } +} + +void mesos_state_t::erase_groups(const std::string& framework_id) +{ + for(marathon_groups::iterator it = m_groups.begin(); it != m_groups.end();) + { + if(it->second->get_framework_id() == framework_id) + { + m_groups.erase(it++); + } + else { ++it; } + } +} + +void mesos_state_t::print_groups() const +{ + for(auto& group : m_groups) + { + group.second->print(); + } +} + +marathon_group::ptr_t mesos_state_t::add_group(const Json::Value& group, marathon_group::ptr_t to_group, const std::string& framework_id) +{ + const Json::Value& group_id = group["id"]; + if(!group_id.isNull()) + { + std::string id = group_id.asString(); + std::ostringstream os; + os << "Adding Marathon group [" << id << ']'; + if(to_group) + { + os << " to group [" << to_group->get_id() << ']'; + } + g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); + + marathon_group::ptr_t pg(new marathon_group(id, framework_id)); + add_or_replace_group(pg, to_group); + + const Json::Value& apps = group["apps"]; + if(!apps.isNull()) + { + for(const auto& app : apps) + { + const Json::Value& app_id = app["id"]; + if(!app_id.isNull()) + { + const Json::Value& instances = app["instances"]; + if(!instances.isNull() && instances.isInt() && instances.asInt() > 0) + { + marathon_app::ptr_t p_app = get_app(app_id.asString()); + if(!p_app) + { + p_app = add_app(app, framework_id); + } + if(p_app) + { + pg->add_or_replace_app(p_app); + if(!framework_id.empty()) + { + for(const auto& task : get_tasks(framework_id)) + { + if(task.second->get_marathon_app_id() == app_id.asString()) + { + add_task_to_app(p_app, task.first); + } + } + } + } + else + { + std::string errstr = "An error occurred adding app [" + app_id.asString() + + "] to group [" + id + ']'; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(app_id.asString(), errstr, sinsp_utils::get_current_time_ns(), "add-group"); + } + } + } + } + } + + Json::Value groups = group["groups"]; + if(!groups.isNull() && groups.isArray()) + { + handle_groups(group, pg, framework_id); + } + return pg; + } + return 0; +} + +void mesos_state_t::parse_apps(Json::Value&& root, const std::string& framework_id) +{ + const Json::Value& apps = root["apps"]; + if(!apps.isNull()) + { + for(const auto& app : apps) + { + add_app(app, framework_id); + } +#ifdef HAS_CAPTURE + if(m_is_captured) + { + capture_apps(root, framework_id); + } +#endif // HAS_CAPTURE + if(m_verbose) + { + std::cout << Json::FastWriter().write(root) << std::endl; + } + } + else + { + g_logger.log("No apps found.", sinsp_logger::SEV_WARNING); + } +} + +void mesos_state_t::parse_apps(json_ptr_t json, const std::string& framework_id) +{ + if(json && !json->isNull()) + { + parse_apps(std::move(*json), framework_id); + } + else + { + throw sinsp_exception("Invalid JSON (Marathon apps parsing failed)."); + } +} + +marathon_app::ptr_t mesos_state_t::add_app(const Json::Value& app, const std::string& /*framework_id*/) +{ + marathon_app::ptr_t p_app = 0; + const Json::Value& app_id = app["id"]; + if(!app_id.isNull()) + { + std::string id = app_id.asString(); + g_logger.log("Adding Marathon app: " + id, sinsp_logger::SEV_DEBUG); + std::string group_id = marathon_app::get_group_id(id); + if(!group_id.empty()) + { + p_app = add_or_replace_app(id, group_id); + if(p_app) + { + const Json::Value& labels = app["labels"]; + if(!labels.isNull()) + { + p_app->set_labels(labels); + } + g_logger.log("Added app [" + id + "] to Marathon group: [" + group_id + ']', sinsp_logger::SEV_DEBUG); + const Json::Value& tasks = app["tasks"]; + if(tasks.size()) + { + g_logger.log("App [" + id + "] has " + std::to_string(tasks.size()) + " tasks.", sinsp_logger::SEV_DEBUG); + for(const auto& task : tasks) + { + Json::Value task_id = task["id"]; + if(!task_id.isNull()) + { + std::string tid = task_id.asString(); + g_logger.log("Adding Mesos task ID to app [" + id + "]: " + tid, sinsp_logger::SEV_DEBUG); + mesos_framework::task_ptr_t pt = get_task(task_id.asString()); + if(pt) + { + pt->set_marathon_app_id(id); + add_task_to_app(p_app, tid); + } + else + { + std::string errstr = "Marathon task not found in mesos state: " + tid; + g_logger.log(errstr, sinsp_logger::SEV_WARNING); + g_json_error_log.log(tid, errstr, sinsp_utils::get_current_time_ns(), "add-app"); + } + } + } + } + } + else + { + std::string errstr = "NOT added app [" + id + "] to Marathon group: [" + group_id + ']'; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(id, errstr, sinsp_utils::get_current_time_ns(), "add-app"); + } + } + else + { + std::string errstr = "Could not determine group ID for app: " + id; + g_logger.log(errstr, sinsp_logger::SEV_ERROR); + g_json_error_log.log(id, errstr, sinsp_utils::get_current_time_ns(), "add-app"); + } + } + return p_app; +} +#endif // CYGWING_AGENT + diff --git a/userspace/libsinsp/mesos_state.h b/userspace/libsinsp/mesos_state.h new file mode 100644 index 0000000000..a1de232368 --- /dev/null +++ b/userspace/libsinsp/mesos_state.h @@ -0,0 +1,480 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// mesos_state_t.h +// +// mesos state abstraction +// + +#pragma once + +#include "mesos_component.h" +#include "marathon_component.h" +#include "json/json.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "json_error_log.h" +#include +#include +#include +#include + +// +// state +// + +class mesos_state_t +{ +public: + typedef std::shared_ptr json_ptr_t; + +#ifdef HAS_CAPTURE + struct capture + { + enum type_t + { + MESOS_STATE = 0, + MARATHON_GROUPS = 1, + MARATHON_APPS = 2 + }; + + capture(type_t type, std::string&& data): + m_type(type), + m_data(std::move(data)) + { + } + + std::string to_string() + { + m_data.erase(std::remove_if(m_data.begin(), m_data.end(), [](char c) { return c == '\r' || c == '\n'; })); + std::ostringstream os; + switch(m_type) + { + case MESOS_STATE: + os << "{\"mesos_state\":" << m_data << '}' << std::flush; + break; + case MARATHON_GROUPS: + os << "{\"marathon_groups\":" << m_data << '}' << std::flush; + break; + case MARATHON_APPS: + os << "{\"marathon_apps\":" << m_data << '}' << std::flush; + break; + } + return os.str(); + } + + type_t m_type; + std::string m_data; + }; + typedef std::deque capture_list; + + const capture_list& get_capture_events() const + { + return m_capture; + } + + std::string dequeue_capture_event() + { + std::string ret; + if(m_capture.size()) + { + ret = m_capture.front().to_string(); + m_capture.pop_front(); + } + return ret; + } + + void enqueue_capture_event(capture::type_t type, std::string&& data) + { + if(m_is_captured) + { + m_capture.emplace_back(capture(type, std::move(data))); + } + } + + bool is_captured() const + { + return m_is_captured; + } + + void capture_groups(const Json::Value& root, const std::string& framework_id, Json::Value& capt, bool capture_fw = false); + void capture_apps(const Json::Value& root, const std::string& framework_id); +#endif // HAS_CAPTURE + + mesos_state_t(bool is_captured = false, bool verbose = false); + + // + // frameworks + // + const mesos_frameworks& get_frameworks() const; + mesos_frameworks& get_frameworks(); + const mesos_framework& get_framework(const std::string& framework_uid) const; + mesos_framework& get_framework(const std::string& framework_uid); + void push_framework(const mesos_framework& framework); + void emplace_framework(mesos_framework&& framework); + void remove_framework(const std::string& framework_uid); + void remove_framework(const Json::Value& framework); + const mesos_framework* get_framework_for_task(const std::string& task_id) const; + + // + // tasks + // + std::unordered_set get_all_task_ids() const; + const mesos_framework::task_map& get_tasks(const std::string& framework_uid) const; + mesos_framework::task_map& get_tasks(const std::string& framework_uid); + mesos_framework::task_ptr_t get_task(const std::string& uid) const; + void add_or_replace_task(mesos_framework& framework, mesos_task::ptr_t task); + void remove_task(mesos_framework& framework, const std::string& uid); + + // + // slaves + // + const mesos_slaves& get_slaves() const; + mesos_slaves& get_slaves(); + const mesos_slave& get_slave(const std::string& slave_uid) const; + mesos_slave& get_slave(const std::string& slave_uid); + void push_slave(const mesos_slave& slave); + void emplace_slave(mesos_slave&& slave); + + // + // Marathon + // + + void set_marathon_uri(const std::string& uri); + const std::string& get_marathon_uri() const; + + // + // Marathon apps + // + void parse_apps(Json::Value&& root, const std::string& framework_id); + void parse_apps(json_ptr_t json, const std::string& framework_id); + marathon_app::ptr_t get_app(const std::string& app_id); + marathon_group::app_ptr_t add_or_replace_app(const std::string& id, + const std::string& group, + const std::string& task = ""); + bool remove_app(const std::string& id); + void add_task_to_app(marathon_group::app_ptr_t app, const std::string& task_id); + marathon_app::ptr_t get_app(mesos_task::ptr_t task) const; + + // + // Marathon groups + // + bool parse_groups(Json::Value&& root, const std::string& framework_id); + bool parse_groups(json_ptr_t json, const std::string& framework_id); + const marathon_groups& get_groups() const; + marathon_groups& get_groups(); + marathon_group::ptr_t get_group(const std::string& group_id); + marathon_group::ptr_t get_group(mesos_task::ptr_t task) const; + marathon_group::ptr_t add_or_replace_group(marathon_group::ptr_t group, marathon_group::ptr_t to_group = 0); + marathon_group::ptr_t get_app_group(const std::string& app_id); + void erase_groups(const std::string& framework_id); + void print_groups() const; + + // + // state + // + void clear_mesos(); + void clear_marathon(); + bool has_data() const; + +private: + marathon_group::ptr_t add_group(const Json::Value& group, marathon_group::ptr_t to_group, const std::string& framework_id); + bool handle_groups(const Json::Value& groups, marathon_group::ptr_t p_groups, const std::string& framework_id); + marathon_app::ptr_t add_app(const Json::Value& app, const std::string& framework_id); + + mesos_frameworks m_frameworks; + std::string m_marathon_uri; + mesos_slaves m_slaves; + marathon_groups m_groups; + bool m_verbose; +#ifdef HAS_CAPTURE + bool m_is_captured; + capture_list m_capture; +#endif // HAS_CAPTURE + + typedef std::unordered_map task_framework_map_t; + task_framework_map_t m_task_framework_cache; +}; + +// +// frameworks +// + +inline const mesos_frameworks& mesos_state_t::get_frameworks() const +{ + return m_frameworks; +} + +inline mesos_frameworks& mesos_state_t::get_frameworks() +{ + return m_frameworks; +} + +inline const mesos_framework& mesos_state_t::get_framework(const std::string& framework_uid) const +{ + for(const auto& framework : m_frameworks) + { + if(framework.get_uid() == framework_uid) + { + return framework; + } + } + throw sinsp_exception("Framework not found: " + framework_uid); +} + +inline mesos_framework& mesos_state_t::get_framework(const std::string& framework_uid) +{ + for(auto& framework : m_frameworks) + { + if(framework.get_uid() == framework_uid) + { + return framework; + } + } + throw sinsp_exception("Framework not found: " + framework_uid); +} + +inline void mesos_state_t::push_framework(const mesos_framework& framework) +{ + for(mesos_frameworks::iterator it = m_frameworks.begin(); it != m_frameworks.end();) + { + if(it->get_uid() == framework.get_uid()) + { + it = m_frameworks.erase(it); + } + else { ++it; } + } + m_frameworks.push_back(framework); +} + +inline void mesos_state_t::emplace_framework(mesos_framework&& framework) +{ + for(mesos_frameworks::iterator it = m_frameworks.begin(); it != m_frameworks.end();) + { + if(it->get_uid() == framework.get_uid()) + { + it = m_frameworks.erase(it); + } + else { ++it; } + } + m_frameworks.emplace_back(std::move(framework)); +} + +inline void mesos_state_t::remove_framework(const Json::Value& framework) +{ + const Json::Value& id = framework["id"]; + if(!id.isNull() && id.isString()) + { + remove_framework(id.asString()); + } +} + +inline const mesos_framework* mesos_state_t::get_framework_for_task(const std::string& task_id) const +{ + task_framework_map_t::const_iterator it = m_task_framework_cache.find(task_id); + if(it != m_task_framework_cache.end()) + { + return it->second; + } + return 0; +} + +inline void mesos_state_t::remove_framework(const std::string& framework_uid) +{ + for(mesos_frameworks::iterator it = m_frameworks.begin(); it != m_frameworks.end(); ++it) + { + if(it->get_uid() == framework_uid) + { + for(auto& task : it->get_tasks()) + { + m_task_framework_cache.erase(task.first); + } + m_frameworks.erase(it); + return; + } + } +} + +// +// tasks +// +inline void mesos_state_t::add_or_replace_task(mesos_framework& framework, mesos_task::ptr_t task) +{ + if(task) + { + framework.add_or_replace_task(task); + m_task_framework_cache[task->get_uid()] = &framework; + } +} + +inline void mesos_state_t::remove_task(mesos_framework& framework, const std::string& uid) +{ + mesos_task::ptr_t task = framework.get_task(uid); + if(task) + { + std::string app_id = task->get_marathon_app_id(); + if(!app_id.empty()) + { + marathon_group::ptr_t group = get_app_group(app_id); + if(group) + { + if(!group->remove_task(uid)) + { + std::string errstr = "Task [" + uid + "] not found in Marathon app [" + app_id + ']'; + g_logger.log(errstr, + sinsp_logger::SEV_ERROR); + g_json_error_log.log(uid, errstr, sinsp_utils::get_current_time_ns(), "remove-task"); + } + } + else + { + std::string errstr = "Group not found for Marathon app [" + app_id + "] while trying to remove task [" + uid + ']'; + g_logger.log(errstr, + sinsp_logger::SEV_ERROR); + g_json_error_log.log(app_id, errstr, sinsp_utils::get_current_time_ns(), "remove-task"); + } + } + else + { + g_logger.log("Task [" + uid + "] has no Marathon app ID.", sinsp_logger::SEV_WARNING); + } + } + else + { + g_logger.log("Task [" + uid + "] not found in framework [" + framework.get_uid() + ']', sinsp_logger::SEV_WARNING); + } + framework.remove_task(uid); + m_task_framework_cache.erase(uid); +} + +// +// slaves +// + +inline const mesos_slaves& mesos_state_t::get_slaves() const +{ + return m_slaves; +} + +inline mesos_slaves& mesos_state_t::get_slaves() +{ + return m_slaves; +} + +inline const mesos_slave& mesos_state_t::get_slave(const std::string& slave_uid) const +{ + for(const auto& slave : m_slaves) + { + if(slave.get_uid() == slave_uid) + { + return slave; + } + } + throw sinsp_exception("Slave not found: " + slave_uid); +} + +inline mesos_slave& mesos_state_t::get_slave(const std::string& slave_uid) +{ + for(auto& slave : m_slaves) + { + if(slave.get_uid() == slave_uid) + { + return slave; + } + } + throw sinsp_exception("Slave not found: " + slave_uid); +} + +inline void mesos_state_t::push_slave(const mesos_slave& slave) +{ + for(mesos_slaves::iterator it = m_slaves.begin(); it != m_slaves.end();) + { + if(it->get_uid() == slave.get_uid()) + { + it = m_slaves.erase(it); + } + else { ++it; } + } + m_slaves.push_back(slave); +} + +inline void mesos_state_t::emplace_slave(mesos_slave&& slave) +{ + for(mesos_slaves::iterator it = m_slaves.begin(); it != m_slaves.end();) + { + if(it->get_uid() == slave.get_uid()) + { + it = m_slaves.erase(it); + } + else { ++it; } + } + m_slaves.emplace_back(std::move(slave)); +} + +// +// Marathon +// + +inline void mesos_state_t::set_marathon_uri(const std::string& uri) +{ + m_marathon_uri = uri; +} + +inline const std::string& mesos_state_t::get_marathon_uri() const +{ + return m_marathon_uri; +} + +// +// apps +// + +// +// groups +// + +inline const marathon_groups& mesos_state_t::get_groups() const +{ + return m_groups; +} + +inline marathon_groups& mesos_state_t::get_groups() +{ + return m_groups; +} + +// +// state +// + +inline void mesos_state_t::clear_mesos() +{ + m_frameworks.clear(); + m_slaves.clear(); +} + +inline void mesos_state_t::clear_marathon() +{ + m_groups.clear(); +} + +inline bool mesos_state_t::has_data() const +{ + return m_frameworks.size() > 0 && m_slaves.size() > 0; +} diff --git a/userspace/libsinsp/mutex.h b/userspace/libsinsp/mutex.h new file mode 100644 index 0000000000..a083d1551e --- /dev/null +++ b/userspace/libsinsp/mutex.h @@ -0,0 +1,182 @@ +/* +Copyright (C) 2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include +#include + +namespace libsinsp { +template +class ConstMutexGuard; + +/** + * \brief A wrapper to allow synchronized access to a value owned by a Mutex + * + * @tparam T type of the value protected by the mutex + * + * It works by simply holding a `std::unique_lock` object that keeps the mutex + * locked while it exists and unlocks it upon destruction + */ +template +class MutexGuard { +public: + MutexGuard(std::unique_lock lock, T *inner) : m_lock(std::move(lock)), m_inner(inner) {} + + // we cannot copy a MutexGuard, only move + MutexGuard(MutexGuard &rhs) = delete; + MutexGuard& operator=(MutexGuard &rhs) = delete; + MutexGuard(MutexGuard &&rhs) noexcept : m_lock(std::move(rhs.m_lock)), + m_inner(rhs.m_inner) {} + + T *operator->() + { + return m_inner; + } + + T &operator*() + { + return *m_inner; + } + + /** + * Validate that the guarded object exists. + */ + bool valid() + { + return m_inner != nullptr; + } + +private: + std::unique_lock m_lock; + T *m_inner; + + friend class ConstMutexGuard; +}; + +/** + * \brief A wrapper to allow synchronized const access to a value owned by a Mutex + * + * @tparam T type of the value protected by the mutex + * + * It works by simply holding a `std::unique_lock` object that keeps the mutex + * locked while it exists and unlocks it upon destruction + */ +template +class ConstMutexGuard { +public: + ConstMutexGuard(std::unique_lock lock, const T *inner) : m_lock(std::move(lock)), + m_inner(inner) { + } + + // we cannot copy a ConstMutexGuard, only move + ConstMutexGuard(ConstMutexGuard &rhs) = delete; + ConstMutexGuard& operator=(ConstMutexGuard &rhs) = delete; + ConstMutexGuard(ConstMutexGuard &&rhs) noexcept : m_lock(std::move(rhs.m_lock)), + m_inner(rhs.m_inner) {} + + // a writable guard can be demoted to a read-only one, but *not* the other way around + ConstMutexGuard(MutexGuard &&rhs) noexcept : m_lock(std::move(rhs.m_lock)), + m_inner(rhs.m_inner) // NOLINT(google-explicit-constructor) + {} + + const T *operator->() const + { + return m_inner; + } + + const T &operator*() const + { + return *m_inner; + } + + /** + * Validate that the guarded object exists. + */ + bool valid() + { + return m_inner != nullptr; + } + +private: + std::unique_lock m_lock; + const T *m_inner; +}; + +/** + * \brief Wrap a value of type T, enforcing synchronized access + * + * @tparam T type of the wrapped value + * + * The class owns a value of type T and a mutex. The only way to access the T inside + * is via the lock() method, which returns a guard object that unlocks the mutex + * once it falls out of scope + * + * To protect an object with a mutex, declare a variable of type `Mutex`, e.g. + * + * Mutex> m_locked_vector; + * + * Then, to access the variable, call .lock() on the Mutex object: + * + * MutexGuard> locked = m_locked_vector.lock(); + * + * Now you can call the inner object's methods directly on the guard object, + * which behaves like a smart pointer to the inner object: + * + * size_t num_elts = locked->size(); + * + */ +template +class Mutex { +public: + Mutex() = default; + + Mutex(T inner) : m_inner(std::move(inner)) {} + + /** + * \brief Lock the mutex, allowing access to the stored object + * + * The returned guard object allows access to the protected data + * via operator * or -> and ensures the lock is held as long as + * the guard object exists + */ + MutexGuard lock() + { + return MutexGuard(std::unique_lock(m_lock), &m_inner); + } + + /** + * \brief Lock the mutex, allowing access to the stored object + * + * The returned guard object allows access to the protected data + * via operator * or -> and ensures the lock is held as long as + * the guard object exists + * + * `const Mutex` only allows read-only access to the protected object + */ + ConstMutexGuard lock() const + { + return ConstMutexGuard(std::unique_lock(m_lock), &m_inner); + } + +private: + mutable std::mutex m_lock; + T m_inner; +}; +} \ No newline at end of file diff --git a/userspace/libsinsp/parsers.cpp b/userspace/libsinsp/parsers.cpp index d31a13787f..e2e07764db 100644 --- a/userspace/libsinsp/parsers.cpp +++ b/userspace/libsinsp/parsers.cpp @@ -1,58 +1,114 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ -#include -#include -#include #ifdef _WIN32 +#define NOMINMAX #include #else #include #include -#ifdef _DEBUG -#endif // _DEBUG #include #endif // _WIN32 +#include +#include +#include +#include + +#include "container_engine/mesos.h" #include "sinsp.h" #include "sinsp_int.h" #include "../../driver/ppm_ringbuffer.h" +#include "tracers.h" #include "parsers.h" #include "sinsp_errno.h" #include "filter.h" #include "filterchecks.h" -#ifdef HAS_ANALYZER -#include "analyzer_int.h" -#include "analyzer_thread.h" -#endif +#include "protodecoder.h" #ifdef SIMULATE_DROP_MODE bool should_drop(sinsp_evt *evt); #endif +#include "container_engine/docker.h" + +extern sinsp_protodecoder_list g_decoderlist; +extern sinsp_evttables g_infotables; + sinsp_parser::sinsp_parser(sinsp *inspector) : m_inspector(inspector), m_tmp_evt(m_inspector), m_fd_listener(NULL) { + m_fake_userevt = (scap_evt*)m_fake_userevt_storage; + + // + // Note: allocated here instead of in the sinsp constructor because sinsp_partial_tracer + // is not defined in sinsp.cpp + // + m_inspector->m_partial_tracers_pool = new simple_lifo_queue(128); + + init_metaevt(m_k8s_metaevents_state, PPME_K8S_E, SP_EVT_BUF_SIZE); + init_metaevt(m_mesos_metaevents_state, PPME_MESOS_E, SP_EVT_BUF_SIZE); + m_drop_event_flags = EF_NONE; } sinsp_parser::~sinsp_parser() { + for(uint32_t j = 0; j < m_protodecoders.size(); j++) + { + delete m_protodecoders[j]; + } + + while(!m_tmp_events_buffer.empty()) + { + auto ptr = m_tmp_events_buffer.top(); + free(ptr); + m_tmp_events_buffer.pop(); + } + m_protodecoders.clear(); + + free(m_k8s_metaevents_state.m_piscapevt); + free(m_mesos_metaevents_state.m_piscapevt); + + if(m_inspector->m_partial_tracers_pool != NULL) + { + delete m_inspector->m_partial_tracers_pool; + } +} + +void sinsp_parser::init_scapevt(metaevents_state& evt_state, uint16_t evt_type, uint16_t buf_size) +{ + evt_state.m_piscapevt = (scap_evt*) realloc(evt_state.m_piscapevt, buf_size); + evt_state.m_scap_buf_size = buf_size; + evt_state.m_piscapevt->type = evt_type; + evt_state.m_metaevt.m_pevt = evt_state.m_piscapevt; +} + +void sinsp_parser::init_metaevt(metaevents_state& evt_state, uint16_t evt_type, uint16_t buf_size) +{ + evt_state.m_piscapevt = 0; + init_scapevt(evt_state, evt_type, buf_size); + evt_state.m_metaevt.m_inspector = m_inspector; + evt_state.m_metaevt.m_info = &(g_infotables.m_event_info[PPME_SYSDIGEVENT_X]); + evt_state.m_metaevt.m_cpuid = 0; + evt_state.m_metaevt.m_evtnum = 0; + evt_state.m_metaevt.m_fdinfo = NULL; } /////////////////////////////////////////////////////////////////////////////// @@ -60,22 +116,97 @@ sinsp_parser::~sinsp_parser() /////////////////////////////////////////////////////////////////////////////// void sinsp_parser::process_event(sinsp_evt *evt) { - uint16_t etype = evt->get_type(); + uint16_t etype = evt->m_pevt->type; + bool is_live = m_inspector->is_live(); // // Cleanup the event-related state // reset(evt); + // + // When debug mode is not enabled, filter out events about sysdig itself + // +#if defined(HAS_CAPTURE) + if(is_live && !m_inspector->is_debug_enabled()) + { + if(evt->get_tid() == m_inspector->m_sysdig_pid && + etype != PPME_SCHEDSWITCH_1_E && + etype != PPME_SCHEDSWITCH_6_E && + etype != PPME_DROP_E && + etype != PPME_DROP_X && + etype != PPME_SYSDIGEVENT_E && + etype != PPME_PROCINFO_E && + etype != PPME_CPU_HOTPLUG_E && + m_inspector->m_sysdig_pid) + { + evt->m_filtered_out = true; + return; + } + } +#endif + + if(m_drop_event_flags) + { + enum ppm_event_flags flags; + uint16_t etype = evt->m_pevt->type; + if(etype == PPME_GENERIC_E || etype == PPME_GENERIC_X) + { + sinsp_evt_param *parinfo = evt->get_param(0); + uint16_t evid = *(uint16_t *)parinfo->m_val; + flags = g_infotables.m_syscall_info_table[evid].flags; + } + else + { + flags = evt->get_info_flags(); + } + + if (flags & m_drop_event_flags) + { + evt->m_filtered_out = true; + return; + } + } + // // Filtering // #if defined(HAS_FILTERING) && defined(HAS_CAPTURE_FILTERING) bool do_filter_later = false; - if(m_inspector->m_filter) + if(m_inspector->m_filter || m_inspector->m_evttype_filter) { - ppm_event_flags eflags = evt->get_flags(); + ppm_event_flags eflags = evt->get_info_flags(); + + if(etype == PPME_SYSCALL_WRITE_X) + { + // + // Check if this is a tracer + // + sinsp_fdinfo_t* fdinfo = evt->m_fdinfo; + + if(fdinfo == NULL && evt->m_tinfo != nullptr) + { + fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd); + evt->m_fdinfo = fdinfo; + } + + if(fdinfo && (fdinfo->m_flags & (sinsp_fdinfo_t::FLAGS_IS_TRACER_FD | sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE))) + { + eflags = (ppm_event_flags)(((uint64_t)eflags) | EF_MODIFIES_STATE); + } + else + { + if(!m_inspector->is_live()) + { + if((evt->get_dump_flags() & SCAP_DF_TRACER) != 0) + { + evt->m_fdinfo = NULL; + eflags = (ppm_event_flags)(((uint64_t)eflags) | EF_MODIFIES_STATE); + } + } + } + } if(eflags & EF_MODIFIES_STATE) { @@ -83,11 +214,14 @@ void sinsp_parser::process_event(sinsp_evt *evt) } else { - if(m_inspector->m_filter->run(evt) == false) + if(m_inspector->run_filters_on_evt(evt) == false) { if(evt->m_tinfo != NULL) { - evt->m_tinfo->m_lastevent_type = PPM_SC_MAX; + if(!(eflags & EF_SKIPPARSERESET || etype == PPME_SCHEDSWITCH_6_E)) + { + evt->m_tinfo->m_lastevent_type = PPM_EVENT_MAX; + } } evt->m_filtered_out = true; @@ -104,6 +238,13 @@ void sinsp_parser::process_event(sinsp_evt *evt) // switch(etype) { + case PPME_SOCKET_SENDTO_E: + if((evt->m_fdinfo == nullptr) && (evt->m_tinfo != nullptr)) + { + infer_sendto_fdinfo(evt); + } + + // FALLTHRU case PPME_SYSCALL_OPEN_E: case PPME_SOCKET_SOCKET_E: case PPME_SYSCALL_EVENTFD_E: @@ -115,10 +256,31 @@ void sinsp_parser::process_event(sinsp_evt *evt) case PPME_SYSCALL_GETRLIMIT_E: case PPME_SYSCALL_SETRLIMIT_E: case PPME_SYSCALL_PRLIMIT_E: - case PPME_SOCKET_SENDTO_E: case PPME_SOCKET_SENDMSG_E: + case PPME_SYSCALL_SENDFILE_E: + case PPME_SYSCALL_SETRESUID_E: + case PPME_SYSCALL_SETRESGID_E: + case PPME_SYSCALL_SETUID_E: + case PPME_SYSCALL_SETGID_E: + case PPME_SYSCALL_EXECVE_18_E: + case PPME_SYSCALL_EXECVE_19_E: + case PPME_SYSCALL_SETPGID_E: store_event(evt); break; + case PPME_SYSCALL_WRITE_E: + if(!m_inspector->m_is_dumping && evt->m_tinfo != nullptr) + { + evt->m_fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd); + if(evt->m_fdinfo) + { + if(evt->m_fdinfo->m_flags & sinsp_fdinfo_t::FLAGS_IS_TRACER_FD) + { + evt->m_filtered_out = true; + return; + } + } + } + break; case PPME_SYSCALL_READ_X: case PPME_SYSCALL_WRITE_X: case PPME_SOCKET_RECV_X: @@ -135,39 +297,67 @@ void sinsp_parser::process_event(sinsp_evt *evt) case PPME_SYSCALL_PWRITEV_X: parse_rw_exit(evt); break; + case PPME_SYSCALL_SENDFILE_X: + parse_sendfile_exit(evt); + break; case PPME_SYSCALL_OPEN_X: case PPME_SYSCALL_CREAT_X: case PPME_SYSCALL_OPENAT_X: - parse_open_openat_creat_exit(evt); + case PPME_SYSCALL_OPENAT_2_X: + parse_open_openat_creat_exit(evt); break; case PPME_SYSCALL_SELECT_E: case PPME_SYSCALL_POLL_E: + case PPME_SYSCALL_PPOLL_E: case PPME_SYSCALL_EPOLLWAIT_E: - parse_select_poll_epollwait_enter(evt); - break; - case PPME_CLONE_X: + parse_select_poll_epollwait_enter(evt); + break; + case PPME_SYSCALL_CLONE_11_X: + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_X: + case PPME_SYSCALL_VFORK_17_X: + case PPME_SYSCALL_VFORK_20_X: parse_clone_exit(evt); break; - case PPME_SYSCALL_EXECVE_X: + case PPME_SYSCALL_EXECVE_8_X: + case PPME_SYSCALL_EXECVE_13_X: + case PPME_SYSCALL_EXECVE_14_X: + case PPME_SYSCALL_EXECVE_15_X: + case PPME_SYSCALL_EXECVE_16_X: + case PPME_SYSCALL_EXECVE_17_X: + case PPME_SYSCALL_EXECVE_18_X: + case PPME_SYSCALL_EXECVE_19_X: parse_execve_exit(evt); break; case PPME_PROCEXIT_E: + case PPME_PROCEXIT_1_E: parse_thread_exit(evt); break; case PPME_SYSCALL_PIPE_X: - parse_pipe_exit(evt); + parse_pipe_exit(evt); break; + case PPME_SOCKET_SOCKET_X: parse_socket_exit(evt); break; case PPME_SOCKET_BIND_X: parse_bind_exit(evt); break; + case PPME_SOCKET_CONNECT_E: + parse_connect_enter(evt); + break; case PPME_SOCKET_CONNECT_X: parse_connect_exit(evt); break; case PPME_SOCKET_ACCEPT_X: + case PPME_SOCKET_ACCEPT_5_X: case PPME_SOCKET_ACCEPT4_X: + case PPME_SOCKET_ACCEPT4_5_X: parse_accept_exit(evt); break; case PPME_SYSCALL_CLOSE_E: @@ -219,28 +409,113 @@ void sinsp_parser::process_event(sinsp_evt *evt) case PPME_SOCKET_SOCKETPAIR_X: parse_socketpair_exit(evt); break; + case PPME_SCHEDSWITCH_1_E: + case PPME_SCHEDSWITCH_6_E: + parse_context_switch(evt); + break; + case PPME_SYSCALL_BRK_4_X: + case PPME_SYSCALL_MMAP_X: + case PPME_SYSCALL_MMAP2_X: + case PPME_SYSCALL_MUNMAP_X: + parse_brk_munmap_mmap_exit(evt); + break; + case PPME_SYSCALL_SETRESUID_X: + parse_setresuid_exit(evt); + break; + case PPME_SYSCALL_SETRESGID_X: + parse_setresgid_exit(evt); + break; + case PPME_SYSCALL_SETUID_X: + parse_setuid_exit(evt); + break; + case PPME_SYSCALL_SETGID_X: + parse_setgid_exit(evt); + break; + case PPME_CONTAINER_E: + parse_container_evt(evt); // deprecated, only here for backwards compatibility + break; + case PPME_CONTAINER_JSON_E: + parse_container_json_evt(evt); + break; + case PPME_CPU_HOTPLUG_E: + parse_cpu_hotplug_enter(evt); + break; +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + case PPME_K8S_E: + if(!m_inspector->is_live()) + { + parse_k8s_evt(evt); + } + break; + case PPME_MESOS_E: + if(!m_inspector->is_live()) + { + parse_mesos_evt(evt); + } + break; +#endif // #if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + case PPME_SYSCALL_CHROOT_X: + parse_chroot_exit(evt); + break; + case PPME_SYSCALL_SETSID_X: + parse_setsid_exit(evt); + break; + case PPME_SOCKET_GETSOCKOPT_X: + if(evt->get_num_params() > 0) + { + parse_getsockopt_exit(evt); + } + break; default: break; } // - // With some state-changing events like clone, execve and open, we do the + // With some state-changing events like clone, execve and open, we do the // filtering after having updated the state // #if defined(HAS_FILTERING) && defined(HAS_CAPTURE_FILTERING) if(do_filter_later) { - if(m_inspector->m_filter) + if(m_inspector->run_filters_on_evt(evt) == false) { - if(m_inspector->m_filter->run(evt) == false) - { - evt->m_filtered_out = true; - return; - } + evt->m_filtered_out = true; + return; } evt->m_filtered_out = false; } #endif + // + // Offline captures can produce events with the SCAP_DF_STATE_ONLY. They are + // supposed to go through the engine, but they must be filtered out before + // reaching the user. + // + if(!is_live) + { + if(evt->get_dump_flags() & SCAP_DF_STATE_ONLY) + { + evt->m_filtered_out = true; + } + } + + // Check to see if the name changed as a side-effect of + // parsing this event. Try to avoid the overhead of a string + // compare for every event. + if(evt->m_fdinfo) + { + evt->set_fdinfo_name_changed(evt->m_fdinfo->m_name != evt->m_fdinfo->m_oldname); + } +} + +void sinsp_parser::event_cleanup(sinsp_evt *evt) +{ + if(evt->get_direction() == SCAP_ED_OUT && + evt->m_tinfo && evt->m_tinfo->m_lastevent_data) + { + free_event_buffer(evt->m_tinfo->m_lastevent_data); + evt->m_tinfo->m_lastevent_data = NULL; + evt->m_tinfo->set_lastevent_data_validity(false); + } } /////////////////////////////////////////////////////////////////////////////// @@ -253,13 +528,35 @@ void sinsp_parser::process_event(sinsp_evt *evt) // bool sinsp_parser::reset(sinsp_evt *evt) { + uint16_t etype = evt->get_type(); // - // Before anything can happen, the event needs to be initialized + // Before anything can happen, the event needs to be + // initialized. // - evt->init(); + // For events created by the container resolvers and pushed + // onto the inspector's pending_container_events queue, the + // event has a threadinfo pointer that points to the process + // that created the container. + // + // However, for container events that are at the beginning of + // trace files and only describe the set of current + // containers, the threadinfo pointer is junk and must be + // cleared in init(). So only keep the threadinfo for "live" + // containers. + // + if (m_inspector->is_live() && etype == PPME_CONTAINER_JSON_E && evt->m_tinfo_ref != nullptr) + { + // this is a synthetic event generated by the container manager + // the threadinfo should already be set properly + evt->init_keep_threadinfo(); + return true; + } + else + { + evt->init(); + } - ppm_event_flags eflags = evt->get_flags(); - uint16_t etype = evt->get_type(); + ppm_event_flags eflags = evt->get_info_flags(); evt->m_fdinfo = NULL; evt->m_errorcode = 0; @@ -267,8 +564,17 @@ bool sinsp_parser::reset(sinsp_evt *evt) // // Ignore scheduler events // - if(etype >= PPME_SCHEDSWITCH_E && etype <= PPME_DROP_X) + if(eflags & EF_SKIPPARSERESET) { + if(etype == PPME_PROCINFO_E) + { + evt->m_tinfo = m_inspector->get_thread(evt->m_pevt->tid, false, false); + } + else + { + evt->m_tinfo = NULL; + } + return false; } @@ -277,10 +583,21 @@ bool sinsp_parser::reset(sinsp_evt *evt) // // - // If we're exiting a clone, we don't look for /proc + // If we're exiting a clone or if we have a scheduler event + // (many kernel thread), we don't look for /proc // bool query_os; - if(etype == PPME_CLONE_X) + if(etype == PPME_SYSCALL_CLONE_11_X || + etype == PPME_SYSCALL_CLONE_16_X || + etype == PPME_SYSCALL_CLONE_17_X || + etype == PPME_SYSCALL_CLONE_20_X || + etype == PPME_SYSCALL_FORK_X || + etype == PPME_SYSCALL_FORK_17_X || + etype == PPME_SYSCALL_FORK_20_X || + etype == PPME_SYSCALL_VFORK_X || + etype == PPME_SYSCALL_VFORK_17_X || + etype == PPME_SYSCALL_VFORK_20_X || + etype == PPME_SCHEDSWITCH_6_E) { query_os = false; } @@ -289,10 +606,33 @@ bool sinsp_parser::reset(sinsp_evt *evt) query_os = true; } - evt->m_tinfo = evt->get_thread_info(query_os); + if(etype == PPME_CONTAINER_JSON_E) + { + evt->m_tinfo = nullptr; + return true; + } + else + { + evt->m_tinfo = m_inspector->get_thread(evt->m_pevt->tid, query_os, false); + } + + if(etype == PPME_SCHEDSWITCH_6_E) + { + return false; + } + if(!evt->m_tinfo) { - if(etype == PPME_CLONE_X) + if(etype == PPME_SYSCALL_CLONE_11_X || + etype == PPME_SYSCALL_CLONE_16_X || + etype == PPME_SYSCALL_CLONE_17_X || + etype == PPME_SYSCALL_CLONE_20_X || + etype == PPME_SYSCALL_FORK_X || + etype == PPME_SYSCALL_FORK_17_X || + etype == PPME_SYSCALL_FORK_20_X || + etype == PPME_SYSCALL_VFORK_X || + etype == PPME_SYSCALL_VFORK_17_X || + etype == PPME_SYSCALL_VFORK_20_X) { #ifdef GATHER_INTERNAL_STATS m_inspector->m_thread_manager->m_failed_lookups->decrement(); @@ -306,6 +646,11 @@ bool sinsp_parser::reset(sinsp_evt *evt) return false; } + if(query_os) + { + evt->m_tinfo->m_flags |= PPM_CL_ACTIVE; + } + if(PPME_IS_ENTER(etype)) { evt->m_tinfo->m_lastevent_fd = -1; @@ -324,6 +669,7 @@ bool sinsp_parser::reset(sinsp_evt *evt) ASSERT(evt->get_param_info(0)->type == PT_FD); evt->m_tinfo->m_lastevent_fd = *(int64_t *)parinfo->m_val; + evt->m_fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd); } evt->m_tinfo->m_latency = 0; @@ -331,31 +677,42 @@ bool sinsp_parser::reset(sinsp_evt *evt) } else { + sinsp_threadinfo* tinfo = evt->m_tinfo; + // // event latency // - if(evt->m_tinfo->m_last_latency_entertime != 0) + if(tinfo->m_last_latency_entertime != 0) { - evt->m_tinfo->m_latency = evt->get_ts() - evt->m_tinfo->m_last_latency_entertime; - ASSERT((int64_t)evt->m_tinfo->m_latency >= 0); + tinfo->m_latency = evt->get_ts() - tinfo->m_last_latency_entertime; + ASSERT((int64_t)tinfo->m_latency >= 0); } - if(etype == evt->m_tinfo->m_lastevent_type + 1) + if(etype == tinfo->m_lastevent_type + 1) { - evt->m_tinfo->set_lastevent_data_validity(true); + tinfo->set_lastevent_data_validity(true); } else { - evt->m_tinfo->set_lastevent_data_validity(false); - return false; + tinfo->set_lastevent_data_validity(false); + + if(tinfo->m_lastevent_type != PPME_TRACER_E) + { + return false; + } } // // Error detection logic // - if(evt->m_info->nparams != 0 && - ((evt->m_info->params[0].name[0] == 'r' && evt->m_info->params[0].name[1] == 'e' && evt->m_info->params[0].name[2] == 's') || - (evt->m_info->params[0].name[0] == 'f' && evt->m_info->params[0].name[1] == 'd'))) + if(evt->get_num_params() != 0 && + ((evt->m_info->params[0].name[0] == 'r' && + evt->m_info->params[0].name[1] == 'e' && + evt->m_info->params[0].name[2] == 's' && + evt->m_info->params[0].name[3] == '\0') || + (evt->m_info->params[0].name[0] == 'f' && + evt->m_info->params[0].name[1] == 'd' && + evt->m_info->params[0].name[2] == '\0'))) { sinsp_evt_param *parinfo; @@ -374,16 +731,22 @@ bool sinsp_parser::reset(sinsp_evt *evt) // if(eflags & EF_USES_FD) { - evt->m_fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd); + evt->m_fdinfo = tinfo->get_fd(tinfo->m_lastevent_fd); if(evt->m_fdinfo == NULL) { return false; } - else if(evt->m_fdinfo->m_flags & sinsp_fdinfo_t::FLAGS_CLOSE_CANCELED) + + if(evt->m_errorcode != 0 && m_fd_listener) + { + m_fd_listener->on_error(evt); + } + + if(evt->m_fdinfo->m_flags & sinsp_fdinfo_t::FLAGS_CLOSE_CANCELED) { // - // A close gets canceled when the same fd is created succesfully between + // A close gets canceled when the same fd is created successfully between // close enter and close exit. // If that happens // @@ -391,49 +754,18 @@ bool sinsp_parser::reset(sinsp_evt *evt) evt->m_fdinfo->m_flags &= ~sinsp_fdinfo_t::FLAGS_CLOSE_CANCELED; eparams.m_fd = CANCELED_FD_NUMBER; - eparams.m_fdinfo = evt->m_tinfo->get_fd(CANCELED_FD_NUMBER); + eparams.m_fdinfo = tinfo->get_fd(CANCELED_FD_NUMBER); // // Remove the fd from the different tables // eparams.m_remove_from_table = true; - eparams.m_inspector = m_inspector; - eparams.m_tinfo = evt->m_tinfo; + eparams.m_tinfo = tinfo; eparams.m_ts = evt->get_ts(); erase_fd(&eparams); } } - - if(eflags & EF_CREATES_FD) - { - // - // Calculate (and if necessary update) the fd usage ratio - // - sinsp_evt_param *parinfo; - int64_t fd; - - // - // In case of pipe or socketpair, just the first FD is good enough - // - uint32_t parnum = (etype == PPME_SYSCALL_PIPE_X || etype == PPME_SOCKET_SOCKETPAIR_X)? 1 : 0; - - parinfo = evt->get_param(parnum); - ASSERT(parinfo->m_len == sizeof(int64_t)); - ASSERT(evt->get_param_info(parnum)->type == PT_FD); - fd = *(int64_t *)parinfo->m_val; - - if(fd > 0 && evt->m_tinfo->m_fdlimit != -1) - { - int64_t m_fd_usage_pct = fd * 100 / evt->m_tinfo->m_fdlimit; - ASSERT(m_fd_usage_pct <= 100); - - if(m_fd_usage_pct > evt->m_tinfo->m_fd_usage_pct) - { - evt->m_tinfo->m_fd_usage_pct = (uint32_t)m_fd_usage_pct; - } - } - } } return true; @@ -445,7 +777,7 @@ void sinsp_parser::store_event(sinsp_evt *evt) { // // No thread in the table. We won't store this event, which mean that - // we won't be able to parse the correspoding exit event and we'll have + // we won't be able to parse the corresponding exit event and we'll have // to drop the information it carries. // #ifdef GATHER_INTERNAL_STATS @@ -454,7 +786,29 @@ void sinsp_parser::store_event(sinsp_evt *evt) return; } - evt->m_tinfo->store_event(evt); + uint32_t elen; + + // + // Make sure the event data is going to fit + // + elen = scap_event_getlen(evt->m_pevt); + + if(elen > SP_EVT_BUF_SIZE) + { + ASSERT(false); + return; + } + + // + // Copy the data + // + auto tinfo = evt->m_tinfo; + if(tinfo->m_lastevent_data == NULL) + { + tinfo->m_lastevent_data = reserve_event_buffer(); + } + memcpy(tinfo->m_lastevent_data, evt->m_pevt, elen); + tinfo->m_lastevent_cpuid = evt->get_cpuid(); #ifdef GATHER_INTERNAL_STATS m_inspector->m_stats.m_n_stored_evts++; @@ -474,7 +828,7 @@ bool sinsp_parser::retrieve_enter_event(sinsp_evt *enter_evt, sinsp_evt *exit_ev // // Retrieve the copy of the enter event and initialize it // - if(!exit_evt->m_tinfo->is_lastevent_data_valid()) + if(!(exit_evt->m_tinfo->is_lastevent_data_valid() && exit_evt->m_tinfo->m_lastevent_data)) { // // This happen especially at the beginning of trace files, where events @@ -508,16 +862,63 @@ bool sinsp_parser::retrieve_enter_event(sinsp_evt *enter_evt, sinsp_evt *exit_ev return true; } +sinsp_protodecoder* sinsp_parser::add_protodecoder(string decoder_name) +{ + // + // Make sure this decoder is not present yet + // + vector::iterator it; + for(it = m_protodecoders.begin(); it != m_protodecoders.end(); ++it) + { + if((*it)->get_name() == decoder_name) + { + return (*it); + } + } + + sinsp_protodecoder* nd = g_decoderlist.new_protodecoder_from_name(decoder_name, + m_inspector); + + nd->init(); + + m_protodecoders.push_back(nd); + + return nd; +} + +void sinsp_parser::register_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec) +{ + switch(etype) + { + case CT_OPEN: + m_open_callbacks.push_back(dec); + break; + case CT_CONNECT: + m_connect_callbacks.push_back(dec); + break; + default: + ASSERT(false); + break; + } + + return; +} + /////////////////////////////////////////////////////////////////////////////// // PARSERS /////////////////////////////////////////////////////////////////////////////// void sinsp_parser::parse_clone_exit(sinsp_evt *evt) { - sinsp_evt_param *parinfo; + sinsp_evt_param* parinfo; int64_t tid = evt->get_tid(); int64_t childtid; - unordered_map::iterator it; bool is_inverted_clone = false; // true if clone() in the child returns before the one in the parent + bool tid_collision = false; + bool valid_parent = true; + bool in_container = false; + int64_t vtid = tid; + int64_t vpid = -1; + uint16_t etype = evt->get_type(); // // Validate the return value and get the child tid @@ -526,6 +927,32 @@ void sinsp_parser::parse_clone_exit(sinsp_evt *evt) ASSERT(parinfo->m_len == sizeof(int64_t)); childtid = *(int64_t *)parinfo->m_val; + switch(evt->get_type()) + { + case PPME_SYSCALL_CLONE_11_X: + parinfo = evt->get_param(8); + break; + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_VFORK_X: + parinfo = evt->get_param(13); + break; + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_VFORK_17_X: + parinfo = evt->get_param(14); + break; + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_20_X: + parinfo = evt->get_param(15); + break; + default: + ASSERT(false); + } + ASSERT(parinfo->m_len == sizeof(int32_t)); + uint32_t flags = *(int32_t *)parinfo->m_val; + if(childtid < 0) { // @@ -533,58 +960,138 @@ void sinsp_parser::parse_clone_exit(sinsp_evt *evt) // return; } - else if(childtid == 0) - { - // - // clone() returns 0 in the child. - // Validate that the child thread info has actually been created. - // - if(!evt->m_tinfo) - { - // - // No thread yet. - // This happens if - // - clone() returns in the child before than in the parent. - // - we dropped the clone exit event in the parent. - // In both cases, we create the thread entry here - is_inverted_clone = true; + // + // Get the vtid to check if the clone is within a container + // + switch(etype) + { + case PPME_SYSCALL_CLONE_11_X: + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_VFORK_X: + case PPME_SYSCALL_VFORK_17_X: + break; + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_20_X: + parinfo = evt->get_param(18); + ASSERT(parinfo->m_len == sizeof(int64_t)); + vtid = *(int64_t *)parinfo->m_val; + + parinfo = evt->get_param(19); + ASSERT(parinfo->m_len == sizeof(int64_t)); + vpid = *(int64_t *)parinfo->m_val; + break; + default: + ASSERT(false); + } + + // the flag check should always suffice but leave the tid/vtid check for older driver versions + if(flags & PPM_CL_CHILD_IN_PIDNS || tid != vtid) + { + in_container = true; + } + + if(childtid == 0) + { + // + // clone() returns 0 in the child. + // + int64_t parenttid; + + // + // Before embarking in parsing the event, check if there's already + // an entry in the thread table for this process. If there is one, make sure + // it was created recently. Otherwise, assume it's an old thread for which + // we lost the exit event and remove it from the table. + // + if(evt->m_tinfo && evt->m_tinfo->m_clone_ts != 0) + { + if(evt->get_ts() - evt->m_tinfo->m_clone_ts > CLONE_STALE_TIME_NS) + { + m_inspector->remove_thread(tid, true); + evt->m_tinfo = NULL; + } + } + + // + // Check if this is a process or a new thread + // + if(flags & PPM_CL_CLONE_THREAD) + { // - // The tid to add is the one that generated this event - // - childtid = tid; + // This is a thread, the parent tid is the pid + // + parinfo = evt->get_param(4); + ASSERT(parinfo->m_len == sizeof(int64_t)); + parenttid = *(int64_t *)parinfo->m_val; + } + else + { + // + // This is not a thread, the parent tid is ptid + // + parinfo = evt->get_param(5); + ASSERT(parinfo->m_len == sizeof(int64_t)); + parenttid = *(int64_t *)parinfo->m_val; + } + // Validate that the child thread info has actually been created. + // + if(!evt->m_tinfo) + { + // + // No thread yet. + // This happens if + // - clone() returns in the child before than in the parent. + // - we dropped the clone exit event in the parent. + // - clone was executed in a container + // In both cases, we create the thread entry here // - // Get the flags, and check if this is a process or a new thread + // XXX: inverted_clone flag should be useless for containers + // since just the child's clone is allowed to create a thread // - parinfo = evt->get_param(8); - ASSERT(parinfo->m_len == sizeof(int32_t)); - uint32_t flags = *(int32_t *)parinfo->m_val; + is_inverted_clone = true; - if(flags & PPM_CL_CLONE_THREAD) - { - // - // This is a thread, the parent tid is the pid - // - parinfo = evt->get_param(4); - ASSERT(parinfo->m_len == sizeof(int64_t)); - tid = *(int64_t *)parinfo->m_val; - } - else - { - // - // This is not a thread, the parent tid is ptid - // - parinfo = evt->get_param(5); - ASSERT(parinfo->m_len == sizeof(int64_t)); - tid = *(int64_t *)parinfo->m_val; - } + // + // The tid to add is the one that generated this event + // + childtid = tid; + + tid = parenttid; // // Keep going and add the event with the standard code below // } else + { + // + // We are in the child's clone. If we are in a container, make + // sure the vtid/vpid are reflected because the father was maybe + // running outside the container so created the child thread without + // knowing the internal vtid/vpid + // + if(in_container) + { + evt->m_tinfo->m_vtid = vtid; + evt->m_tinfo->m_vpid = vpid; + } + + return; + } + } + else + { + // + // We are in the father. If the father is running in a container, + // don't create the child process but wait until we see child, because + // the father just sees the internal tid of the child + // + if(in_container) { return; } @@ -593,7 +1100,7 @@ void sinsp_parser::parse_clone_exit(sinsp_evt *evt) // // Lookup the thread that called clone() so we can copy its information // - sinsp_threadinfo* ptinfo = m_inspector->get_thread(tid, true); + sinsp_threadinfo* ptinfo = m_inspector->get_thread(tid, true, true); if(NULL == ptinfo) { // @@ -604,10 +1111,15 @@ void sinsp_parser::parse_clone_exit(sinsp_evt *evt) return; } + if(ptinfo->m_comm == "" && ptinfo->m_uid == 0xffffffff) + { + valid_parent = false; + } + // // See if the child is already there // - sinsp_threadinfo* child = m_inspector->get_thread(childtid, false); + sinsp_threadinfo* child = m_inspector->get_thread(childtid, false, true); if(NULL != child) { // @@ -622,8 +1134,8 @@ void sinsp_parser::parse_clone_exit(sinsp_evt *evt) } else { - ASSERT(false); - m_inspector->remove_thread(childtid); + m_inspector->remove_thread(childtid, true); + tid_collision = true; } } @@ -632,98 +1144,388 @@ void sinsp_parser::parse_clone_exit(sinsp_evt *evt) // XXX this should absolutely not do a malloc, but get the item from a // preallocated list // - sinsp_threadinfo tinfo(m_inspector); + sinsp_threadinfo* tinfo = m_inspector->build_threadinfo(); // // Set the tid and parent tid // - tinfo.m_tid = childtid; - tinfo.m_ptid = tid; + tinfo->m_tid = childtid; + tinfo->m_ptid = tid; + + if(valid_parent) + { + // Copy the command name from the parent + tinfo->m_comm = ptinfo->m_comm; + + // Copy the full executable name from the parent + tinfo->m_exe = ptinfo->m_exe; + + // Copy the full executable path from the parent + tinfo->m_exepath = ptinfo->m_exepath; + + // Copy the command arguments from the parent + tinfo->m_args = ptinfo->m_args; + + // Copy the root from the parent + tinfo->m_root = ptinfo->m_root; + + // Copy the session id from the parent + tinfo->m_sid = ptinfo->m_sid; + + // Copy the process group id from the parent + tinfo->m_vpgid = ptinfo->m_vpgid; + + tinfo->m_tty = ptinfo->m_tty; - // Copy the command name from the parent - tinfo.m_comm = ptinfo->m_comm; + tinfo->m_loginuid = ptinfo->m_loginuid; + + if(!(flags & PPM_CL_CLONE_THREAD)) + { + tinfo->m_env = ptinfo->m_env; + } + } + else + { + // + // Parent is an invalid thread, which is strange since it's performing + // a clone. We try to remove and look it up in proc. + // + m_inspector->remove_thread(tid, true); + tid_collision = true; + + ptinfo = m_inspector->get_thread(tid, + true, true); + + if(ptinfo == NULL) + { + // + // This can happen if the thread table has reached max capacity + // + ASSERT(false); + return; + } + + if(ptinfo->m_comm != "" && ptinfo->m_uid != 0xffffffff) + { + // + // Parent found in proc, use its data + // + tinfo->m_comm = ptinfo->m_comm; + tinfo->m_exe = ptinfo->m_exe; + tinfo->m_exepath = ptinfo->m_exepath; + tinfo->m_args = ptinfo->m_args; + tinfo->m_root = ptinfo->m_root; + tinfo->m_sid = ptinfo->m_sid; + tinfo->m_vpgid = ptinfo->m_vpgid; + tinfo->m_tty = ptinfo->m_tty; + tinfo->m_loginuid = ptinfo->m_loginuid; + if(!(flags & PPM_CL_CLONE_THREAD)) + { + tinfo->m_env = ptinfo->m_env; + } + } + else + { + // + // Parent not found in proc, use the event data. + // (The session id will remain unset) + // + parinfo = evt->get_param(1); + tinfo->m_exe = (char*)parinfo->m_val; + + switch(etype) + { + case PPME_SYSCALL_CLONE_11_X: + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_VFORK_X: + tinfo->m_comm = tinfo->m_exe; + break; + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_17_X: + case PPME_SYSCALL_VFORK_20_X: + parinfo = evt->get_param(13); + tinfo->m_comm = parinfo->m_val; + break; + default: + ASSERT(false); + } - // Copy the full executable name from the parent - tinfo.m_exe = ptinfo->m_exe; + parinfo = evt->get_param(2); + tinfo->set_args(parinfo->m_val, parinfo->m_len); - // Copy the command arguments from the parent - tinfo.m_args = ptinfo->m_args; + // + // Also, propagate the same values to the parent + // + ptinfo->m_comm = tinfo->m_comm; + ptinfo->m_exe = tinfo->m_exe; + ptinfo->m_exepath = tinfo->m_exepath; + ptinfo->set_args(parinfo->m_val, parinfo->m_len); + } + } // Copy the pid parinfo = evt->get_param(4); ASSERT(parinfo->m_len == sizeof(int64_t)); - tinfo.m_pid = *(int64_t *)parinfo->m_val; + tinfo->m_pid = *(int64_t *)parinfo->m_val; // Get the flags, and check if this is a thread or a new thread - parinfo = evt->get_param(8); - ASSERT(parinfo->m_len == sizeof(int32_t)); - tinfo.m_flags = *(int32_t *)parinfo->m_val; + tinfo->m_flags = flags; // // If clone()'s PPM_CL_CLONE_THREAD is not set it means that a new // thread was created. In that case, we set the pid to the one of the CHILD thread that // is going to be created. // - if(!(tinfo.m_flags & PPM_CL_CLONE_THREAD)) + if(!(tinfo->m_flags & PPM_CL_CLONE_THREAD)) { - tinfo.m_pid = childtid; + tinfo->m_pid = childtid; } - // - // Copy the fd list - // XXX this is a gross oversimplification that will need to be fixed. - // What we do is: if the child is NOT a thread, we copy all the parent fds. - // The right thing to do is looking at PPM_CL_CLONE_FILES, but there are - // syscalls like open and pipe2 that can override PPM_CL_CLONE_FILES with the O_CLOEXEC flag - // - if(!(tinfo.m_flags & PPM_CL_CLONE_THREAD)) + if(!(tinfo->m_flags & PPM_CL_CLONE_THREAD)) { - tinfo.m_fdtable = *(ptinfo->get_fd_table()); + // + // Copy the fd list + // XXX this is a gross oversimplification that will need to be fixed. + // What we do is: if the child is NOT a thread, we copy all the parent fds. + // The right thing to do is looking at PPM_CL_CLONE_FILES, but there are + // syscalls like open and pipe2 that can override PPM_CL_CLONE_FILES with the O_CLOEXEC flag + // + tinfo->m_fdtable = *(ptinfo->get_fd_table()); + + // + // Track down that those are cloned fds + // + for(auto fdit = tinfo->m_fdtable.m_table.begin(); fdit != tinfo->m_fdtable.m_table.end(); ++fdit) + { + fdit->second.set_is_cloned(); + } // // It's important to reset the cache of the child thread, to prevent it from // referring to an element in the parent's table. // - tinfo.m_fdtable.reset_cache(); + tinfo->m_fdtable.reset_cache(); + + // + // Not a thread, copy cwd + // + tinfo->m_cwd = ptinfo->get_cwd(); } - //if((tinfo.m_flags & (PPM_CL_CLONE_FILES))) + //if((tinfo->m_flags & (PPM_CL_CLONE_FILES))) //{ - // tinfo.m_fdtable = ptinfo.m_fdtable; + // tinfo->m_fdtable = ptinfo.m_fdtable; //} if(is_inverted_clone) { - tinfo.m_flags |= PPM_CL_CLONE_INVERTED; + tinfo->m_flags |= PPM_CL_CLONE_INVERTED; + } + + // Copy the command name + parinfo = evt->get_param(1); + tinfo->m_exe = (char*)parinfo->m_val; + + switch(etype) + { + case PPME_SYSCALL_CLONE_11_X: + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_VFORK_X: + tinfo->m_comm = tinfo->m_exe; + break; + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_17_X: + case PPME_SYSCALL_VFORK_20_X: + parinfo = evt->get_param(13); + tinfo->m_comm = parinfo->m_val; + break; + default: + ASSERT(false); } - // Copy the working directory - parinfo = evt->get_param(6); - tinfo.set_cwd(parinfo->m_val, parinfo->m_len); + // Get the command arguments + parinfo = evt->get_param(2); + tinfo->set_args(parinfo->m_val, parinfo->m_len); // Copy the fdlimit parinfo = evt->get_param(7); ASSERT(parinfo->m_len == sizeof(int64_t)); - tinfo.m_fdlimit = *(int64_t *)parinfo->m_val; + tinfo->m_fdlimit = *(int64_t *)parinfo->m_val; + + switch(etype) + { + case PPME_SYSCALL_CLONE_11_X: + break; + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_X: + case PPME_SYSCALL_VFORK_17_X: + case PPME_SYSCALL_VFORK_20_X: + // Get the pgflt_maj + parinfo = evt->get_param(8); + ASSERT(parinfo->m_len == sizeof(uint64_t)); + tinfo->m_pfmajor = *(uint64_t *)parinfo->m_val; + + // Get the pgflt_min + parinfo = evt->get_param(9); + ASSERT(parinfo->m_len == sizeof(uint64_t)); + tinfo->m_pfminor = *(uint64_t *)parinfo->m_val; + + // Get the vm_size + parinfo = evt->get_param(10); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + tinfo->m_vmsize_kb = *(uint32_t *)parinfo->m_val; + + // Get the vm_rss + parinfo = evt->get_param(11); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + tinfo->m_vmrss_kb = *(uint32_t *)parinfo->m_val; + + // Get the vm_swap + parinfo = evt->get_param(12); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + tinfo->m_vmswap_kb = *(uint32_t *)parinfo->m_val; + break; + default: + ASSERT(false); + } // Copy the uid - parinfo = evt->get_param(9); + switch(etype) + { + case PPME_SYSCALL_CLONE_11_X: + parinfo = evt->get_param(9); + break; + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_VFORK_X: + parinfo = evt->get_param(14); + break; + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_VFORK_17_X: + parinfo = evt->get_param(15); + break; + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_20_X: + parinfo = evt->get_param(16); + break; + default: + ASSERT(false); + } ASSERT(parinfo->m_len == sizeof(int32_t)); - tinfo.m_uid = *(int32_t *)parinfo->m_val; + tinfo->m_uid = *(int32_t *)parinfo->m_val; - // Copy the uid - parinfo = evt->get_param(10); + // Copy the gid + switch(etype) + { + case PPME_SYSCALL_CLONE_11_X: + parinfo = evt->get_param(10); + break; + case PPME_SYSCALL_CLONE_16_X: + case PPME_SYSCALL_FORK_X: + case PPME_SYSCALL_VFORK_X: + parinfo = evt->get_param(15); + break; + case PPME_SYSCALL_CLONE_17_X: + case PPME_SYSCALL_FORK_17_X: + case PPME_SYSCALL_VFORK_17_X: + parinfo = evt->get_param(16); + break; + case PPME_SYSCALL_CLONE_20_X: + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_20_X: + parinfo = evt->get_param(17); + break; + default: + ASSERT(false); + } ASSERT(parinfo->m_len == sizeof(int32_t)); - tinfo.m_gid = *(int32_t *)parinfo->m_val; + tinfo->m_gid = *(int32_t *)parinfo->m_val; + + // + // If we're in a container, vtid and vpid are + // initialized to the values coming from the event, + // otherwise they are just set to tid and pid. We can't + // use the event in that case because in a non-container + // case also the clone exit from the father can create a + // child process, and it doesn't have the right vtid and vpid + // values + // + if(in_container) + { + tinfo->m_vtid = vtid; + tinfo->m_vpid = vpid; + } + else + { + tinfo->m_vtid = tinfo->m_tid; + tinfo->m_vpid = tinfo->m_pid; + } + + // + // Set cgroups and heuristically detect container id + // + switch(etype) + { + case PPME_SYSCALL_FORK_20_X: + case PPME_SYSCALL_VFORK_20_X: + case PPME_SYSCALL_CLONE_20_X: + parinfo = evt->get_param(14); + tinfo->set_cgroups(parinfo->m_val, parinfo->m_len); + m_inspector->m_container_manager.resolve_container(tinfo, m_inspector->is_live()); + break; + } // - // Initilaize the thread clone time + // Initialize the thread clone time // - tinfo.m_clone_ts = evt->get_ts(); + tinfo->m_clone_ts = evt->get_ts(); // // Add the new thread to the table // - m_inspector->add_thread(tinfo); + bool thread_added = m_inspector->add_thread(tinfo); + + // + // If there's a listener, invoke it + // + if(m_fd_listener) + { + m_fd_listener->on_clone(evt, tinfo); + } + + // + // If we had to erase a previous entry for this tid and rebalance the table, + // make sure we reinitialize the tinfo pointer for this event, as the thread + // generating it might have gone away. + // + if(tid_collision) + { + reset(evt); +#ifdef HAS_ANALYZER + m_inspector->m_tid_collisions.push_back(tinfo->m_tid); +#endif + DBG_SINSP_INFO("tid collision for %" PRIu64 "(%s)", + tinfo->m_tid, + tinfo->m_comm.c_str()); + } + + if (!thread_added) { + delete tinfo; + } return; } @@ -732,6 +1534,8 @@ void sinsp_parser::parse_execve_exit(sinsp_evt *evt) { sinsp_evt_param *parinfo; int64_t retval; + uint16_t etype = evt->get_type(); + sinsp_evt *enter_evt = &m_tmp_evt; // Validate the return value parinfo = evt->get_param(0); @@ -758,21 +1562,31 @@ void sinsp_parser::parse_execve_exit(sinsp_evt *evt) return; } - string prev_comm(evt->m_tinfo->m_comm); - string prev_exe(evt->m_tinfo->m_exe); - - // Get the command name + // Get the exe parinfo = evt->get_param(1); - string tmps = parinfo->m_val; - tmps = tmps.substr(tmps.rfind("/") + 1); - evt->m_tinfo->m_comm = tmps; - - // - // XXX We should retrieve the full executable name from the arguments that execve receives in the kernel, - // but for the moment we don't do it, so we just copy the command name into the exe string - // evt->m_tinfo->m_exe = parinfo->m_val; + switch(etype) + { + case PPME_SYSCALL_EXECVE_8_X: + case PPME_SYSCALL_EXECVE_13_X: + case PPME_SYSCALL_EXECVE_14_X: + // Old trace files didn't have comm, so just set it to exe + evt->m_tinfo->m_comm = evt->m_tinfo->m_exe; + break; + case PPME_SYSCALL_EXECVE_15_X: + case PPME_SYSCALL_EXECVE_16_X: + case PPME_SYSCALL_EXECVE_17_X: + case PPME_SYSCALL_EXECVE_18_X: + case PPME_SYSCALL_EXECVE_19_X: + // Get the comm + parinfo = evt->get_param(13); + evt->m_tinfo->m_comm = parinfo->m_val; + break; + default: + ASSERT(false); + } + // Get the command arguments parinfo = evt->get_param(2); evt->m_tinfo->set_args(parinfo->m_val, parinfo->m_len); @@ -782,57 +1596,400 @@ void sinsp_parser::parse_execve_exit(sinsp_evt *evt) ASSERT(parinfo->m_len == sizeof(uint64_t)); evt->m_tinfo->m_pid = *(uint64_t *)parinfo->m_val; - // Get the working directory - parinfo = evt->get_param(6); - evt->m_tinfo->set_cwd(parinfo->m_val, parinfo->m_len); + // + // In case this thread is a fake entry, + // try to at least patch the parent, since + // we have it from the execve event + // + if(evt->m_tinfo->m_ptid == -1) + { + parinfo = evt->get_param(5); + ASSERT(parinfo->m_len == sizeof(uint64_t)); + evt->m_tinfo->m_ptid = *(uint64_t *)parinfo->m_val; + } // Get the fdlimit parinfo = evt->get_param(7); ASSERT(parinfo->m_len == sizeof(int64_t)); evt->m_tinfo->m_fdlimit = *(int64_t *)parinfo->m_val; - // - // execve starts with a clean fd list, so we get rid of the fd list that clone - // copied from the parent - // XXX validate this - // - // scap_fd_free_table(handle, tinfo); - - // - // Clear the flags for this thread, making sure to propagate the inverted flag - // - bool inverted = ((evt->m_tinfo->m_flags & PPM_CL_CLONE_INVERTED) != 0); - evt->m_tinfo->m_flags = 0; - if(inverted) + switch(etype) { - evt->m_tinfo->m_flags |= PPM_CL_CLONE_INVERTED; - } + case PPME_SYSCALL_EXECVE_8_X: + break; + case PPME_SYSCALL_EXECVE_13_X: + case PPME_SYSCALL_EXECVE_14_X: + case PPME_SYSCALL_EXECVE_15_X: + case PPME_SYSCALL_EXECVE_16_X: + case PPME_SYSCALL_EXECVE_17_X: + case PPME_SYSCALL_EXECVE_18_X: + case PPME_SYSCALL_EXECVE_19_X: + // Get the pgflt_maj + parinfo = evt->get_param(8); + ASSERT(parinfo->m_len == sizeof(uint64_t)); + evt->m_tinfo->m_pfmajor = *(uint64_t *)parinfo->m_val; + + // Get the pgflt_min + parinfo = evt->get_param(9); + ASSERT(parinfo->m_len == sizeof(uint64_t)); + evt->m_tinfo->m_pfminor = *(uint64_t *)parinfo->m_val; + + // Get the vm_size + parinfo = evt->get_param(10); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + evt->m_tinfo->m_vmsize_kb = *(uint32_t *)parinfo->m_val; - // - // This process' name changed, so we need to include it in the protocol again - // - evt->m_tinfo->m_flags |= PPM_CL_NAME_CHANGED; + // Get the vm_rss + parinfo = evt->get_param(11); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + evt->m_tinfo->m_vmrss_kb = *(uint32_t *)parinfo->m_val; + + // Get the vm_swap + parinfo = evt->get_param(12); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + evt->m_tinfo->m_vmswap_kb = *(uint32_t *)parinfo->m_val; + break; + default: + ASSERT(false); + } + + switch(etype) + { + case PPME_SYSCALL_EXECVE_8_X: + case PPME_SYSCALL_EXECVE_13_X: + break; + case PPME_SYSCALL_EXECVE_14_X: + // Get the environment + parinfo = evt->get_param(13); + evt->m_tinfo->set_env(parinfo->m_val, parinfo->m_len); + break; + case PPME_SYSCALL_EXECVE_15_X: + // Get the environment + parinfo = evt->get_param(14); + evt->m_tinfo->set_env(parinfo->m_val, parinfo->m_len); + break; + case PPME_SYSCALL_EXECVE_16_X: + case PPME_SYSCALL_EXECVE_17_X: + case PPME_SYSCALL_EXECVE_18_X: + case PPME_SYSCALL_EXECVE_19_X: + // Get the environment + parinfo = evt->get_param(15); + evt->m_tinfo->set_env(parinfo->m_val, parinfo->m_len); + + // + // Set cgroups and heuristically detect container id + // + parinfo = evt->get_param(14); + evt->m_tinfo->set_cgroups(parinfo->m_val, parinfo->m_len); + + // + // Resync container status after an execve, we need to do it + // because at container startup docker spawn a process with vpid=1 + // outside of container cgroup and correct cgroups are + // assigned just before doing execve: + // + // 1. docker-runc calls fork() and created process with vpid=1 + // 2. docker-runc changes cgroup hierarchy of it + // 3. vpid=1 execve to the real process the user wants to run inside the container + // + m_inspector->m_container_manager.resolve_container(evt->m_tinfo, m_inspector->is_live()); + break; + default: + ASSERT(false); + } + + switch(etype) + { + case PPME_SYSCALL_EXECVE_8_X: + case PPME_SYSCALL_EXECVE_13_X: + case PPME_SYSCALL_EXECVE_14_X: + case PPME_SYSCALL_EXECVE_15_X: + case PPME_SYSCALL_EXECVE_16_X: + break; + case PPME_SYSCALL_EXECVE_17_X: + case PPME_SYSCALL_EXECVE_18_X: + case PPME_SYSCALL_EXECVE_19_X: + // Get the tty + parinfo = evt->get_param(16); + ASSERT(parinfo->m_len == sizeof(int32_t)); + evt->m_tinfo->m_tty = *(int32_t *) parinfo->m_val; + break; + default: + ASSERT(false); + } + + switch(etype) + { + case PPME_SYSCALL_EXECVE_8_X: + case PPME_SYSCALL_EXECVE_13_X: + case PPME_SYSCALL_EXECVE_14_X: + case PPME_SYSCALL_EXECVE_15_X: + case PPME_SYSCALL_EXECVE_16_X: + case PPME_SYSCALL_EXECVE_17_X: + break; + case PPME_SYSCALL_EXECVE_18_X: + case PPME_SYSCALL_EXECVE_19_X: + // Get exepath + if (retrieve_enter_event(enter_evt, evt)) + { + char fullpath[SCAP_MAX_PATH_SIZE]; + parinfo = enter_evt->get_param(0); + if (strncmp(parinfo->m_val, "", 4) == 0) + { + evt->m_tinfo->m_exepath = ""; + } + else + { + sinsp_utils::concatenate_paths(fullpath, SCAP_MAX_PATH_SIZE, + evt->m_tinfo->m_cwd.c_str(), (uint32_t)evt->m_tinfo->m_cwd.size(), + parinfo->m_val, (uint32_t)parinfo->m_len); + evt->m_tinfo->m_exepath = fullpath; + } + } + break; + default: + ASSERT(false); + } + + switch(etype) + { + case PPME_SYSCALL_EXECVE_8_X: + case PPME_SYSCALL_EXECVE_13_X: + case PPME_SYSCALL_EXECVE_14_X: + case PPME_SYSCALL_EXECVE_15_X: + case PPME_SYSCALL_EXECVE_16_X: + case PPME_SYSCALL_EXECVE_17_X: + case PPME_SYSCALL_EXECVE_18_X: + break; + case PPME_SYSCALL_EXECVE_19_X: + // Get the vpgid + parinfo = evt->get_param(17); + ASSERT(parinfo->m_len == sizeof(int64_t)); + evt->m_tinfo->m_vpgid = *(int64_t *) parinfo->m_val; + break; + default: + ASSERT(false); + } + + // From scap version 1.2, event types of existent + // events are no longer changed. + // sinsp_evt::get_num_params() can instead be used + // to identify the version of the event. + // For example: + // + // if(evt->get_num_params() > 18) + // { + // ... + // } + + // Get the loginuid + if(evt->get_num_params() > 18) + { + parinfo = evt->get_param(18); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + evt->m_tinfo->m_loginuid = *(uint32_t *) parinfo->m_val; + } + + // + // execve starts with a clean fd list, so we get rid of the fd list that clone + // copied from the parent + // XXX validate this + // + // scap_fd_free_table(handle, tinfo); + + // + // Clear the flags for this thread, making sure to propagate the inverted + // and shell pipe flags + // + + auto spf = evt->m_tinfo->m_flags & (PPM_CL_PIPE_SRC | PPM_CL_PIPE_DST); + bool inverted = ((evt->m_tinfo->m_flags & PPM_CL_CLONE_INVERTED) != 0); + + evt->m_tinfo->m_flags = PPM_CL_ACTIVE; + + evt->m_tinfo->m_flags |= spf; + if(inverted) + { + evt->m_tinfo->m_flags |= PPM_CL_CLONE_INVERTED; + } // - // execve potentially breaks the program chain, and so we need to reflect it in our parents program count. + // This process' name changed, so we need to include it in the protocol again + // + evt->m_tinfo->m_flags |= PPM_CL_NAME_CHANGED; + + // + // Recompute the program hash + // + evt->m_tinfo->compute_program_hash(); + + // + // If there's a listener, invoke it // - if((prev_comm != evt->m_tinfo->m_comm) || (prev_exe != evt->m_tinfo->m_exe)) + if(m_fd_listener) + { + m_fd_listener->on_execve(evt); + } + + return; +} + +void sinsp_parser::parse_openat_dir(sinsp_evt *evt, char* name, int64_t dirfd, OUT string* sdir) +{ + bool is_absolute = (name[0] == '/'); + string tdirstr; + + if(is_absolute) + { + // + // The path is absolute. + // Some processes (e.g. irqbalance) actually do this: they pass an invalid fd and + // and absolute path, and openat succeeds. + // + *sdir = "."; + } + else if(dirfd == PPM_AT_FDCWD) + { + *sdir = evt->m_tinfo->get_cwd(); + } + else { - if(evt->m_tinfo->m_progid != -1LL) + evt->m_fdinfo = evt->m_tinfo->get_fd(dirfd); + + if(evt->m_fdinfo == NULL) { - m_inspector->m_thread_manager->decrement_program_childcount(evt->m_tinfo); + ASSERT(false); + *sdir = ""; } else { - m_inspector->m_thread_manager->increment_program_childcount(evt->m_tinfo); + if(evt->m_fdinfo->m_name[evt->m_fdinfo->m_name.length()] == '/') + { + *sdir = evt->m_fdinfo->m_name; + } + else + { + tdirstr = evt->m_fdinfo->m_name + '/'; + *sdir = tdirstr; + } } } +} -#ifdef HAS_ANALYZER - evt->m_tinfo->m_ainfo->clear_role_flags(); -#endif - return; +template +void schedule_more_evts(sinsp* inspector, void* data, T* client, ppm_event_type evt_type) +{ +#ifdef HAS_CAPTURE + ASSERT(data); + bool good_event = false; + metaevents_state* state = (metaevents_state*)data; + + if(state->m_new_group == true) + { + state->m_new_group = false; + inspector->add_meta_event(&state->m_metaevt); + return; + } + + ASSERT(client); + if(!client->get_capture_events().size()) + { + SINSP_STR_ERROR( + std::string("An event scheduled but no events available." + "All pending event requests for " + "[") + typeid(T).name() + "] are cancelled."); + state->m_new_group = false; + state->m_n_additional_events_to_add = 0; + inspector->remove_meta_event_callback(); + return; + } + string payload = client->dequeue_capture_event(); + std::size_t tot_len = sizeof(scap_evt) + sizeof(uint16_t) + payload.size() + 1; + + if(tot_len > state->m_scap_buf_size) + { + sinsp_parser::init_scapevt(*state, evt_type, tot_len); + } + + state->m_piscapevt->len = tot_len; + state->m_piscapevt->nparams = 1; + uint16_t* plen = (uint16_t*)((char *)state->m_piscapevt + sizeof(struct ppm_evt_hdr)); + plen[0] = (uint16_t)payload.size() + 1; + uint8_t* edata = (uint8_t*)plen + sizeof(uint16_t); + memcpy(edata, payload.c_str(), plen[0]); + good_event = true; + + state->m_n_additional_events_to_add--; + if(state->m_n_additional_events_to_add == 0) + { + inspector->remove_meta_event_callback(); + } + else if(good_event) + { + inspector->add_meta_event(&state->m_metaevt); + } +#endif // HAS_CAPTURE +} + +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) +void schedule_more_k8s_evts(sinsp* inspector, void* data) +{ + schedule_more_evts(inspector, data, inspector->get_k8s_client(), PPME_K8S_E); +} + +void sinsp_parser::schedule_k8s_events() +{ +#ifdef HAS_CAPTURE + // + // schedule k8s events, if any available + // + k8s* k8s_client = 0; + if(m_inspector && (k8s_client = m_inspector->m_k8s_client)) + { + int event_count = k8s_client->get_capture_events().size(); + if(event_count) + { + m_k8s_metaevents_state.m_piscapevt->tid = 0; + m_k8s_metaevents_state.m_piscapevt->ts = m_inspector->m_lastevent_ts; + m_k8s_metaevents_state.m_new_group = true; + m_k8s_metaevents_state.m_n_additional_events_to_add = event_count; + m_inspector->add_meta_event_callback(&schedule_more_k8s_evts, &m_k8s_metaevents_state); + + schedule_more_k8s_evts(m_inspector, &m_k8s_metaevents_state); + } + } +#endif // HAS_CAPTURE +} + +void schedule_more_mesos_evts(sinsp* inspector, void* data) +{ + schedule_more_evts(inspector, data, inspector->get_mesos_client(), PPME_MESOS_E); +} + +void sinsp_parser::schedule_mesos_events() +{ +#ifdef HAS_CAPTURE + // + // schedule mesos events, if any available + // + mesos* mesos_client = 0; + if(m_inspector && (mesos_client = m_inspector->m_mesos_client)) + { + int event_count = mesos_client->get_capture_events().size(); + if(event_count) + { + m_mesos_metaevents_state.m_piscapevt->tid = 0; + m_mesos_metaevents_state.m_piscapevt->ts = m_inspector->m_lastevent_ts; + m_mesos_metaevents_state.m_new_group = true; + m_mesos_metaevents_state.m_n_additional_events_to_add = event_count; + m_inspector->add_meta_event_callback(&schedule_more_mesos_evts, &m_mesos_metaevents_state); + + schedule_more_mesos_evts(m_inspector, &m_mesos_metaevents_state); + } + } +#endif // HAS_CAPTURE } +#endif // #if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) void sinsp_parser::parse_open_openat_creat_exit(sinsp_evt *evt) { @@ -841,22 +1998,30 @@ void sinsp_parser::parse_open_openat_creat_exit(sinsp_evt *evt) char *name; uint32_t namelen; uint32_t flags; - // uint32_t mode; sinsp_fdinfo_t fdi; sinsp_evt *enter_evt = &m_tmp_evt; string sdir; - string tdirstr; + uint16_t etype = evt->get_type(); + uint32_t dev = 0; ASSERT(evt->m_tinfo); - - // - // Load the enter event so we can access its arguments - // - if(!retrieve_enter_event(enter_evt, evt)) + if(evt->m_tinfo == nullptr) { return; } + + if(etype != PPME_SYSCALL_OPENAT_2_X) + { + // + // Load the enter event so we can access its arguments + // + if(!retrieve_enter_event(enter_evt, evt)) + { + return; + } + } + // // Check the return value // @@ -864,18 +2029,10 @@ void sinsp_parser::parse_open_openat_creat_exit(sinsp_evt *evt) ASSERT(parinfo->m_len == sizeof(int64_t)); fd = *(int64_t *)parinfo->m_val; - if(fd < 0) - { - // - // The syscall failed. Nothing to add to the table. - // - return; - } - // // Parse the parameters, based on the event type // - if(evt->get_type() == PPME_SYSCALL_OPEN_X) + if(etype == PPME_SYSCALL_OPEN_X) { parinfo = evt->get_param(1); name = parinfo->m_val; @@ -885,9 +2042,16 @@ void sinsp_parser::parse_open_openat_creat_exit(sinsp_evt *evt) ASSERT(parinfo->m_len == sizeof(uint32_t)); flags = *(uint32_t *)parinfo->m_val; + if(evt->get_num_params() > 4) + { + parinfo = evt->get_param(4); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + dev = *(uint32_t *)parinfo->m_val; + } + sdir = evt->m_tinfo->get_cwd(); } - else if(evt->get_type() == PPME_SYSCALL_CREAT_X) + else if(etype == PPME_SYSCALL_CREAT_X) { parinfo = evt->get_param(1); name = parinfo->m_val; @@ -895,9 +2059,16 @@ void sinsp_parser::parse_open_openat_creat_exit(sinsp_evt *evt) flags = 0; + if(evt->get_num_params() > 3) + { + parinfo = evt->get_param(3); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + dev = *(uint32_t *)parinfo->m_val; + } + sdir = evt->m_tinfo->get_cwd(); } - else if(evt->get_type() == PPME_SYSCALL_OPENAT_X) + else if(etype == PPME_SYSCALL_OPENAT_X) { parinfo = enter_evt->get_param(1); name = parinfo->m_val; @@ -911,43 +2082,30 @@ void sinsp_parser::parse_open_openat_creat_exit(sinsp_evt *evt) ASSERT(parinfo->m_len == sizeof(int64_t)); int64_t dirfd = *(int64_t *)parinfo->m_val; - bool is_absolute = (name[0] == '/'); + parse_openat_dir(evt, name, dirfd, &sdir); + } + else if(etype == PPME_SYSCALL_OPENAT_2_X) + { + parinfo = evt->get_param(2); + name = parinfo->m_val; + namelen = parinfo->m_len; - if(is_absolute) - { - // - // The path is absoulte. - // Some processes (e.g. irqbalance) actually do this: they pass an invalid fd and - // and bsolute path, and openat succeeds. - // - sdir = "."; - } - else if(dirfd == PPM_AT_FDCWD) + parinfo = evt->get_param(3); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + flags = *(uint32_t *)parinfo->m_val; + + parinfo = evt->get_param(1); + ASSERT(parinfo->m_len == sizeof(int64_t)); + int64_t dirfd = *(int64_t *)parinfo->m_val; + + if(evt->get_num_params() > 5) { - sdir = evt->m_tinfo->get_cwd(); + parinfo = evt->get_param(5); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + dev = *(uint32_t *)parinfo->m_val; } - else - { - evt->m_fdinfo = evt->m_tinfo->get_fd(dirfd); - if(evt->m_fdinfo == NULL) - { - ASSERT(false); - sdir = ""; - } - else - { - if(evt->m_fdinfo->m_name[evt->m_fdinfo->m_name.length()] == '/') - { - sdir = evt->m_fdinfo->m_name; - } - else - { - tdirstr = evt->m_fdinfo->m_name + '/'; - sdir = tdirstr; - } - } - } + parse_openat_dir(evt, name, dirfd, &sdir); } else { @@ -960,51 +2118,91 @@ void sinsp_parser::parse_open_openat_creat_exit(sinsp_evt *evt) //ASSERT(parinfo->m_len == sizeof(uint32_t)); //mode = *(uint32_t*)parinfo->m_val; - // - // Populate the new fdi - // - fdi.m_type = SCAP_FD_FILE; - fdi.m_openflags = flags; - fdi.add_filename(sdir.c_str(), - sdir.length(), - name, - namelen); - - // - // Add the fd to the table. - // - evt->m_fdinfo = evt->m_tinfo->add_fd(fd, &fdi); -} - -// -// Helper function to allocate a socket fd, initialize it by parsing its parameters and add it to the fd table of the given thread. -// -inline void sinsp_parser::add_socket(sinsp_evt *evt, int64_t fd, uint32_t domain, uint32_t type, uint32_t protocol) -{ - sinsp_fdinfo_t fdi; - - // - // Populate the new fdi - // - memset(&(fdi.m_sockinfo.m_ipv4info), 0, sizeof(fdi.m_sockinfo.m_ipv4info)); - fdi.m_type = SCAP_FD_UNKNOWN; - fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_UNKNOWN; + char fullpath[SCAP_MAX_PATH_SIZE]; + sinsp_utils::concatenate_paths(fullpath, SCAP_MAX_PATH_SIZE, sdir.c_str(), (uint32_t)sdir.length(), name, namelen); - if(domain == PPM_AF_UNIX) - { - fdi.m_type = SCAP_FD_UNIX_SOCK; - } - else if(domain == PPM_AF_INET || domain == PPM_AF_INET6) + if(fd >= 0) { - fdi.m_type = (domain == PPM_AF_INET)? SCAP_FD_IPV4_SOCK : SCAP_FD_IPV6_SOCK; - - if(protocol == IPPROTO_TCP) + // + // Populate the new fdi + // + if(flags & PPM_O_DIRECTORY) { - fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_TCP; + fdi.m_type = SCAP_FD_DIRECTORY; + } + else + { + fdi.m_type = SCAP_FD_FILE_V2; + } + + fdi.m_openflags = flags; + fdi.m_mount_id = 0; + fdi.m_dev = dev; + fdi.add_filename(fullpath); + + // + // If this is a user event fd, mark it with the proper flag + // + if(fdi.m_name == USER_EVT_DEVICE_NAME) + { + fdi.m_flags |= sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE; + } + else + { + fdi.m_flags |= sinsp_fdinfo_t::FLAGS_IS_NOT_TRACER_FD; + } + + // + // Add the fd to the table. + // + evt->m_fdinfo = evt->m_tinfo->add_fd(fd, &fdi); + + // + // Call the protocol decoder callbacks associated to this event + // + vector::iterator it; + for(it = m_open_callbacks.begin(); it != m_open_callbacks.end(); ++it) + { + (*it)->on_event(evt, CT_OPEN); + } + } + + if(m_fd_listener && !(flags & PPM_O_DIRECTORY)) + { + m_fd_listener->on_file_open(evt, fullpath, flags); + } +} + +// +// Helper function to allocate a socket fd, initialize it by parsing its parameters and add it to the fd table of the given thread. +// +inline void sinsp_parser::add_socket(sinsp_evt *evt, int64_t fd, uint32_t domain, uint32_t type, uint32_t protocol) +{ + sinsp_fdinfo_t fdi; + + // + // Populate the new fdi + // + memset(&(fdi.m_sockinfo.m_ipv4info), 0, sizeof(fdi.m_sockinfo.m_ipv4info)); + fdi.m_type = SCAP_FD_UNKNOWN; + fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_UNKNOWN; + + if(domain == PPM_AF_UNIX) + { + fdi.m_type = SCAP_FD_UNIX_SOCK; + } + else if(domain == PPM_AF_INET || domain == PPM_AF_INET6) + { + fdi.m_type = (domain == PPM_AF_INET)? SCAP_FD_IPV4_SOCK : SCAP_FD_IPV6_SOCK; + + uint8_t l4proto = SCAP_L4_UNKNOWN; + if(protocol == IPPROTO_TCP) + { + l4proto = (type == SOCK_RAW)? SCAP_L4_RAW : SCAP_L4_TCP; } else if(protocol == IPPROTO_UDP) { - fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_UDP; + l4proto = (type == SOCK_RAW)? SCAP_L4_RAW : SCAP_L4_UDP; } else if(protocol == IPPROTO_IP) { @@ -1015,11 +2213,11 @@ inline void sinsp_parser::add_socket(sinsp_evt *evt, int64_t fd, uint32_t domain // if((type & 0xff) == SOCK_STREAM) { - fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_TCP; + l4proto = SCAP_L4_TCP; } else if((type & 0xff) == SOCK_DGRAM) { - fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_UDP; + l4proto = SCAP_L4_UDP; } else { @@ -1028,13 +2226,30 @@ inline void sinsp_parser::add_socket(sinsp_evt *evt, int64_t fd, uint32_t domain } else if(protocol == IPPROTO_ICMP) { - fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_ICMP; + l4proto = (type == SOCK_RAW)? SCAP_L4_RAW : SCAP_L4_ICMP; + } + else if(protocol == IPPROTO_RAW) + { + l4proto = SCAP_L4_RAW; + } + + if(domain == PPM_AF_INET) + { + fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = l4proto; + } + else + { + memset(&(fdi.m_sockinfo.m_ipv6info), 0, sizeof(fdi.m_sockinfo.m_ipv6info)); + fdi.m_sockinfo.m_ipv6info.m_fields.m_l4proto = l4proto; } } + else if(domain == PPM_AF_NETLINK) + { + fdi.m_type = SCAP_FD_NETLINK; + } else { - if(domain != 16 && // AF_NETLINK, used by processes to talk to the kernel - domain != 10 && // IPv6 + if( domain != 10 && // IPv6 domain != 17) // AF_PACKET, used for packet capture { // @@ -1044,6 +2259,16 @@ inline void sinsp_parser::add_socket(sinsp_evt *evt, int64_t fd, uint32_t domain } } + if(fdi.m_type == SCAP_FD_UNKNOWN) + { + SINSP_STR_DEBUG("Unknown fd fd=" + to_string(fd) + + " domain=" + to_string(domain) + + " type=" + to_string(type) + + " protocol=" + to_string(protocol) + + " pid=" + to_string(evt->m_tinfo->m_pid) + + " comm=" + evt->m_tinfo->m_comm); + } + #ifndef INCLUDE_UNKNOWN_SOCKET_FDS if(fdi.m_type == SCAP_FD_UNKNOWN) { @@ -1057,6 +2282,68 @@ inline void sinsp_parser::add_socket(sinsp_evt *evt, int64_t fd, uint32_t domain evt->m_fdinfo = evt->m_tinfo->add_fd(fd, &fdi); } +/** + * If we receive a call to 'sendto()' and the event's m_fdinfo is nullptr, + * then we likely missed the call to 'socket()' that created the file + * descriptor. In that case, we'll guess that it's a SOCK_DGRAM/UDP socket + * and create the fdinfo based on that. + * + * Preconditions: evt->m_fdinfo == nullptr and + * evt->m_tinfo != nullptr + * + */ +inline void sinsp_parser::infer_sendto_fdinfo(sinsp_evt* const evt) +{ + if((evt->m_fdinfo != nullptr) || (evt->m_tinfo == nullptr)) + { + ASSERT(evt->m_fdinfo == nullptr); + ASSERT(evt->m_tinfo != nullptr); + return; + } + + const uint32_t FILE_DESCRIPTOR_PARAM = 0; + const uint32_t SOCKET_TUPLE_PARAM = 2; + + sinsp_evt_param* parinfo = nullptr; + + parinfo = evt->get_param(FILE_DESCRIPTOR_PARAM); + ASSERT(parinfo->m_len == sizeof(int64_t)); + ASSERT(evt->get_param_info(FILE_DESCRIPTOR_PARAM)->type == PT_FD); + const int64_t fd = *((int64_t*) parinfo->m_val); + + if(fd < 0) + { + // Call to sendto() with an invalid file descriptor + return; + } + + parinfo = evt->get_param(SOCKET_TUPLE_PARAM); + const char addr_family = *((char*) parinfo->m_val); + + if((addr_family == AF_INET) || (addr_family == AF_INET6)) + { + const uint32_t domain = (addr_family == AF_INET) + ? PPM_AF_INET + : PPM_AF_INET6; + +#ifndef _WIN32 + SINSP_DEBUG("Call to sendto() with fd=%d; missing socket() " + "data. Adding socket %s/SOCK_DGRAM/IPPROTO_UDP " + "for command '%s', pid %d", + fd, + (domain == PPM_AF_INET) ? "PPM_AF_INET" + : "PPM_AF_INET6", + evt->m_tinfo->get_comm().c_str(), + evt->m_tinfo->m_pid); +#endif + + // Here we're assuming sendto() means SOCK_DGRAM/UDP, but it + // can be used with TCP. We have no way to know for sure at + // this point. + add_socket(evt, fd, domain, SOCK_DGRAM, IPPROTO_UDP); + } +} + void sinsp_parser::parse_socket_exit(sinsp_evt *evt) { sinsp_evt_param *parinfo; @@ -1068,7 +2355,7 @@ void sinsp_parser::parse_socket_exit(sinsp_evt *evt) // // NOTE: we don't check the return value of get_param() because we know the arguments we need are there. - // XXX this extraction would be much faster if we parsed the event mnaually to extract the + // XXX this extraction would be much faster if we parsed the event manually to extract the // parameters in one scan. We don't care too much because we assume that we get here // seldom enough that saving few tens of CPU cycles is not important. // @@ -1084,6 +2371,11 @@ void sinsp_parser::parse_socket_exit(sinsp_evt *evt) return; } + if(evt->m_tinfo == nullptr) + { + return; + } + // // Load the enter event so we can access its arguments // @@ -1114,28 +2406,12 @@ void sinsp_parser::parse_socket_exit(sinsp_evt *evt) } void sinsp_parser::parse_bind_exit(sinsp_evt *evt) -{ - const char *parstr; - - if(evt->m_fdinfo == NULL) - { - return; - } - - // - // Update the name of this socket - // - evt->m_fdinfo->m_name = evt->get_param_as_str(1, &parstr, sinsp_evt::PF_SIMPLE); -} - -void sinsp_parser::parse_connect_exit(sinsp_evt *evt) { sinsp_evt_param *parinfo; + int64_t retval; + const char *parstr; uint8_t *packed_data; uint8_t family; - unordered_map::iterator fdit; - const char *parstr; - int64_t retval; if(evt->m_fdinfo == NULL) { @@ -1148,13 +2424,7 @@ void sinsp_parser::parse_connect_exit(sinsp_evt *evt) if(retval < 0) { - // - // connections that return with a SE_EINPROGRESS are totally legit. - // - if(retval != -SE_EINPROGRESS) - { - return; - } + return; } parinfo = evt->get_param(1); @@ -1171,88 +2441,271 @@ void sinsp_parser::parse_connect_exit(sinsp_evt *evt) packed_data = (uint8_t*)parinfo->m_val; - // - // Validate the family - // family = *packed_data; // - // Fill the fd with the socket info + // Update the FD info with this tuple, assume that if port > 0, means that + // the socket is used for listening // - if(family == PPM_AF_INET || family == PPM_AF_INET6) + if(family == PPM_AF_INET) { - if(family == PPM_AF_INET6) + uint32_t ip = *(uint32_t *)(packed_data + 1); + uint16_t port = *(uint16_t *)(packed_data + 5); + if(port > 0) { - // - // For the moment, we only support IPv4-mapped IPv6 addresses - // (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses) - // - uint8_t* sip = packed_data + 1; - uint8_t* dip = packed_data + 19; - - if(!(sinsp_utils::is_ipv4_mapped_ipv6(sip) && sinsp_utils::is_ipv4_mapped_ipv6(dip))) + evt->m_fdinfo->m_type = SCAP_FD_IPV4_SERVSOCK; + evt->m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip = ip; + evt->m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port = port; + evt->m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_l4proto = + evt->m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_l4proto; + evt->m_fdinfo->set_role_server(); + } + } + else if (family == PPM_AF_INET6) + { + uint8_t* ip = packed_data + 1; + uint16_t port = *(uint16_t *)(packed_data + 17); + if(port > 0) + { + if(sinsp_utils::is_ipv4_mapped_ipv6(ip)) { - return; + evt->m_fdinfo->m_type = SCAP_FD_IPV4_SERVSOCK; + evt->m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_l4proto = + evt->m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_l4proto; + evt->m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_ip = *(uint32_t *)(packed_data + 13); + evt->m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_port = port; } - - evt->m_fdinfo->m_type = SCAP_FD_IPV4_SOCK; + else + { + evt->m_fdinfo->m_type = SCAP_FD_IPV6_SERVSOCK; + evt->m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_port = port; + memcpy(evt->m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_ip.m_b, ip, sizeof(ipv6addr)); + evt->m_fdinfo->m_sockinfo.m_ipv6serverinfo.m_l4proto = + evt->m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_l4proto; + } + evt->m_fdinfo->set_role_server(); } + } + // + // Update the name of this socket + // + evt->m_fdinfo->m_name = evt->get_param_as_str(1, &parstr, sinsp_evt::PF_SIMPLE); - // - // This should happen only in case of a bug in our code, because I'm assuming that the OS - // causes a connect with the wrong socket type to fail. - // Assert in debug mode and just keep going in release mode. - // - ASSERT(evt->m_fdinfo->m_type == SCAP_FD_IPV4_SOCK); + // + // If there's a listener callback, invoke it + // + if(m_fd_listener) + { + m_fd_listener->on_bind(evt); + } +} + +/** + * Register a socket in pending state + */ +void sinsp_parser::parse_connect_enter(sinsp_evt *evt){ + if(!m_track_connection_status){ + return; + } + + sinsp_evt_param *parinfo; + uint8_t *packed_data; + + if(evt->m_fdinfo == NULL) + { + return; + } + + evt->m_fdinfo->set_socket_pending(); + parinfo = evt->get_param(1); + if(parinfo->m_len == 0) + { + // + // No address, there's nothing we can really do with this. + // This happens for socket types that we don't support, so we have the assertion + // to make sure that this is not a type of socket that we support. + // + ASSERT(!(evt->m_fdinfo->is_unix_socket() || evt->m_fdinfo->is_ipv4_socket())); + return; + } + + packed_data = (uint8_t*)parinfo->m_val; + + fill_client_socket_info(evt, packed_data); + + // + // If there's a listener callback, invoke it + // + if(m_fd_listener) + { + m_fd_listener->on_connect(evt, packed_data); + } +} + +inline void sinsp_parser::fill_client_socket_info(sinsp_evt *evt, uint8_t *packed_data){ + uint8_t family; + const char *parstr; + bool changed; + + // + // Validate the family + // + family = *packed_data; + + // + // Fill the fd with the socket info + // + if(family == PPM_AF_INET || family == PPM_AF_INET6) + { + if(family == PPM_AF_INET6) + { + // + // Check to see if it's an IPv4-mapped IPv6 address + // (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses) + // + uint8_t* sip = packed_data + 1; + uint8_t* dip = packed_data + 19; + + if(!(sinsp_utils::is_ipv4_mapped_ipv6(sip) && sinsp_utils::is_ipv4_mapped_ipv6(dip))) + { + evt->m_fdinfo->m_type = SCAP_FD_IPV6_SOCK; + changed = m_inspector->m_parser->set_ipv6_addresses_and_ports(evt->m_fdinfo, packed_data); + } + else + { + evt->m_fdinfo->m_type = SCAP_FD_IPV4_SOCK; + changed = m_inspector->m_parser->set_ipv4_mapped_ipv6_addresses_and_ports(evt->m_fdinfo, packed_data); + } + } + else + { + evt->m_fdinfo->m_type = SCAP_FD_IPV4_SOCK; + + // + // Update the FD info with this tuple + // + changed = m_inspector->m_parser->set_ipv4_addresses_and_ports(evt->m_fdinfo, packed_data); + } + + if(changed && evt->m_fdinfo->is_role_server() && evt->m_fdinfo->is_udp_socket()) + { + // connect done by a udp server, swap the addresses + swap_addresses(evt->m_fdinfo); + } + + // + // Add the friendly name to the fd info + // + if(evt->m_fdinfo->is_role_server() && evt->m_fdinfo->is_udp_socket()) + { + sinsp_utils::sockinfo_to_str(&evt->m_fdinfo->m_sockinfo, + evt->m_fdinfo->m_type, &evt->m_paramstr_storage[0], + (uint32_t)evt->m_paramstr_storage.size(), + m_inspector->m_hostname_and_port_resolution_enabled); + + evt->m_fdinfo->m_name = &evt->m_paramstr_storage[0]; + } + else + { + evt->m_fdinfo->m_name = evt->get_param_as_str(1, &parstr, sinsp_evt::PF_SIMPLE); + } + } + else + { + if(!evt->m_fdinfo->is_unix_socket()) + { + // + // This should happen only in case of a bug in our code, because I'm assuming that the OS + // causes a connect with the wrong socket type to fail. + // Assert in debug mode and just keep going in release mode. + // + ASSERT(false); + } + + // + // Add the friendly name to the fd info + // + evt->m_fdinfo->m_name = evt->get_param_as_str(1, &parstr, sinsp_evt::PF_SIMPLE); #ifndef HAS_ANALYZER - // - // Update the FD info with this tuple - // - if(family == PPM_AF_INET) + // + // Update the FD with this tuple + // + m_inspector->m_parser->set_unix_info(evt->m_fdinfo, packed_data); +#endif + } + + if(evt->m_fdinfo->is_role_none()) + { + // + // Mark this fd as a client + // + evt->m_fdinfo->set_role_client(); + } +} + +void sinsp_parser::parse_connect_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + uint8_t *packed_data; + unordered_map::iterator fdit; + int64_t retval; + + if(evt->m_fdinfo == NULL) + { + return; + } + + parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint64_t)); + retval = *(int64_t*)parinfo->m_val; + + if (m_track_connection_status) + { + if (retval == -SE_EINPROGRESS) { + evt->m_fdinfo->set_socket_pending(); + } else if(retval < 0) { + evt->m_fdinfo->set_socket_failed(); + } else { + evt->m_fdinfo->set_socket_connected(); + } + } + else + { + if (retval < 0 && retval != -SE_EINPROGRESS) { - m_inspector->m_parser->set_ipv4_addresses_and_ports(evt->m_fdinfo, packed_data); + return; } else { - m_inspector->m_parser->set_ipv4_mapped_ipv6_addresses_and_ports(evt->m_fdinfo, packed_data); + evt->m_fdinfo->set_socket_connected(); } -#endif + } + parinfo = evt->get_param(1); + if(parinfo->m_len == 0) + { // - // Add the friendly name to the fd info + // No address, there's nothing we can really do with this. + // This happens for socket types that we don't support, so we have the assertion + // to make sure that this is not a type of socket that we support. // - evt->m_fdinfo->m_name = evt->get_param_as_str(1, &parstr, sinsp_evt::PF_SIMPLE); + ASSERT(!(evt->m_fdinfo->is_unix_socket() || evt->m_fdinfo->is_ipv4_socket())); + return; } - else - { - if(!evt->m_fdinfo->is_unix_socket()) - { - // - // This should happen only in case of a bug in our code, because I'm assuming that the OS - // causes a connect with the wrong socket type to fail. - // Assert in debug mode and just keep going in release mode. - // - ASSERT(false); - } - // - // Add the friendly name to the fd info - // - evt->m_fdinfo->m_name = evt->get_param_as_str(1, &parstr, sinsp_evt::PF_SIMPLE); + packed_data = (uint8_t*)parinfo->m_val; -#ifndef HAS_ANALYZER - // - // Update the FD with this tuple - // - m_inspector->m_parser->set_unix_info(evt->m_fdinfo, packed_data); -#endif - } + fill_client_socket_info(evt, packed_data); // - // Mark this fd as a client + // Call the protocol decoder callbacks associated to this event // - evt->m_fdinfo->set_role_client(); + vector::iterator it; + for(it = m_connect_callbacks.begin(); it != m_connect_callbacks.end(); ++it) + { + (*it)->on_event(evt, CT_CONNECT); + } // // If there's a listener callback, invoke it @@ -1330,8 +2783,8 @@ void sinsp_parser::parse_accept_exit(sinsp_evt *evt) else if(*packed_data == PPM_AF_INET6) { // - // We only support IPv4-mapped IPv6 addresses (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses) - // for the moment + // Check to see if it's an IPv4-mapped IPv6 address + // (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses) // uint8_t* sip = packed_data + 1; uint8_t* dip = packed_data + 19; @@ -1342,6 +2795,12 @@ void sinsp_parser::parse_accept_exit(sinsp_evt *evt) fdi.m_type = SCAP_FD_IPV4_SOCK; fdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_TCP; } + else + { + set_ipv6_addresses_and_ports(&fdi, packed_data); + fdi.m_type = SCAP_FD_IPV6_SOCK; + fdi.m_sockinfo.m_ipv6info.m_fields.m_l4proto = SCAP_L4_TCP; + } } else if(*packed_data == PPM_AF_UNIX) { @@ -1364,17 +2823,20 @@ void sinsp_parser::parse_accept_exit(sinsp_evt *evt) m_fd_listener->on_accept(evt, fd, packed_data, &fdi); } - // // Mark this fd as a server // fdi.set_role_server(); + // + // Mark this fd as a connected socket + // + fdi.set_socket_connected(); + // // Add the entry to the table // evt->m_fdinfo = evt->m_tinfo->add_fd(fd, &fdi); - ASSERT(evt->m_fdinfo != NULL); } void sinsp_parser::parse_close_enter(sinsp_evt *evt) @@ -1394,18 +2856,18 @@ void sinsp_parser::parse_close_enter(sinsp_evt *evt) } // -// This function takes care of cleanung up the FD and removing it from all the tables +// This function takes care of cleaning up the FD and removing it from all the tables // (process FD table, connection table...). -// It's invoked when a close() or a threadexit happens. +// It's invoked when a close() or a thread exit happens. // void sinsp_parser::erase_fd(erase_fd_params* params) { if(params->m_fdinfo == NULL) { // - // This happens when more than one close has been canceled at the same time for + // This happens when more than one close has been canceled at the same time for // this thread. Since we currently handle just one canceling at at time (we - // don't have a list of canceled closes, just a single entry), the second one + // don't have a list of canceled closes, just a single entry), the second one // will generate a failed FD lookup. We do nothing. // NOTE: I do realize that this can cause a connection leak, I just assume that it's // rare enough that the delayed connection cleanup (when the timestamp expires) @@ -1420,8 +2882,8 @@ void sinsp_parser::erase_fd(erase_fd_params* params) // if(params->m_remove_from_table) { - params->m_inspector->m_tid_of_fd_to_remove = params->m_tinfo->m_tid; - params->m_inspector->m_fds_to_remove->push_back(params->m_fd); + m_inspector->m_tid_of_fd_to_remove = params->m_tinfo->m_tid; + m_inspector->m_fds_to_remove->push_back(params->m_fd); } if(m_fd_listener) @@ -1447,13 +2909,13 @@ void sinsp_parser::parse_close_exit(sinsp_evt *evt) // if(retval >= 0) { - if(evt->m_fdinfo == NULL) + if(evt->m_fdinfo == NULL || evt->m_tinfo == nullptr) { return; } // - // a close gets canceled when the same fd is created succesfully between + // a close gets canceled when the same fd is created successfully between // close enter and close exit. // erase_fd_params eparams; @@ -1470,13 +2932,10 @@ void sinsp_parser::parse_close_exit(sinsp_evt *evt) eparams.m_fdinfo = evt->m_fdinfo; } - //m_inspector->push_fdop(tid, evt->m_fdinfo, sinsp_fdop(fd, evt->get_type())); - // // Remove the fd from the different tables // eparams.m_remove_from_table = true; - eparams.m_inspector = m_inspector; eparams.m_tinfo = evt->m_tinfo; eparams.m_ts = evt->get_ts(); @@ -1550,6 +3009,12 @@ void sinsp_parser::parse_socketpair_exit(sinsp_evt *evt) return; } + if(evt->m_tinfo == nullptr) + { + // There is nothing we can do here if tinfo is missing + return; + } + parinfo = evt->get_param(1); ASSERT(parinfo->m_len == sizeof(int64_t)); fd1 = *(int64_t *)parinfo->m_val; @@ -1690,6 +3155,41 @@ bool sinsp_parser::set_ipv4_mapped_ipv6_addresses_and_ports(sinsp_fdinfo_t* fdin return true; } +bool sinsp_parser::set_ipv6_addresses_and_ports(sinsp_fdinfo_t* fdinfo, uint8_t* packed_data) +{ + ipv6addr tsip, tdip; + uint16_t tsport, tdport; + + memcpy((uint8_t *) tsip.m_b, packed_data + 1, sizeof(tsip.m_b)); + tsport = *(uint16_t *)(packed_data + 17); + + memcpy((uint8_t *) tdip.m_b, packed_data + 19, sizeof(tdip.m_b)); + tdport = *(uint16_t *)(packed_data + 35); + + if(fdinfo->m_type == SCAP_FD_IPV6_SOCK) + { + if((tsip == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip && + tsport == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport && + tdip == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip && + tdport == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport) || + (tdip == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip && + tdport == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport && + tsip == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip && + tsport == fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport) + ) + { + return false; + } + } + + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip = tsip; + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport = tsport; + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip = tdip; + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport = tdport; + + return true; +} + bool sinsp_parser::set_unix_info(sinsp_fdinfo_t* fdinfo, uint8_t* packed_data) { fdinfo->m_sockinfo.m_unixinfo.m_fields.m_source = *(uint64_t *)(packed_data + 1); @@ -1699,7 +3199,7 @@ bool sinsp_parser::set_unix_info(sinsp_fdinfo_t* fdinfo, uint8_t* packed_data) } -// Return false if the update didn't happen (for example because the tuple is NULL +// Return false if the update didn't happen (for example because the tuple is NULL) bool sinsp_parser::update_fd(sinsp_evt *evt, sinsp_evt_param *parinfo) { uint8_t* packed_data = (uint8_t*)parinfo->m_val; @@ -1717,7 +3217,7 @@ bool sinsp_parser::update_fd(sinsp_evt *evt, sinsp_evt_param *parinfo) // // If this was previously a server socket, propagate the L4 protocol // - evt->m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_l4proto = + evt->m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_l4proto = evt->m_fdinfo->m_sockinfo.m_ipv4serverinfo.m_l4proto; } @@ -1730,33 +3230,71 @@ bool sinsp_parser::update_fd(sinsp_evt *evt, sinsp_evt_param *parinfo) else if(family == PPM_AF_INET6) { // - // For the moment, we only support IPv4-mapped IPv6 addresses + // Check to see if it's an IPv4-mapped IPv6 address // (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses) // uint8_t* sip = packed_data + 1; uint8_t* dip = packed_data + 19; - if(!(sinsp_utils::is_ipv4_mapped_ipv6(sip) && sinsp_utils::is_ipv4_mapped_ipv6(dip))) + if(sinsp_utils::is_ipv4_mapped_ipv6(sip) && sinsp_utils::is_ipv4_mapped_ipv6(dip)) + { + evt->m_fdinfo->m_type = SCAP_FD_IPV4_SOCK; + + if(set_ipv4_mapped_ipv6_addresses_and_ports(evt->m_fdinfo, packed_data) == false) + { + return false; + } + } + else + { + // It's not an ipv4-mapped ipv6 address. Extract it as a normal address. + if(set_ipv6_addresses_and_ports(evt->m_fdinfo, packed_data) == false) + { + return false; + } + } + } + else if(family == PPM_AF_UNIX) + { + evt->m_fdinfo->m_type = SCAP_FD_UNIX_SOCK; + if(set_unix_info(evt->m_fdinfo, packed_data) == false) { return false; } - evt->m_fdinfo->m_type = SCAP_FD_IPV4_SOCK; + evt->m_fdinfo->m_name = ((char*)packed_data) + 17; - if(set_ipv4_mapped_ipv6_addresses_and_ports(evt->m_fdinfo, packed_data) == false) + // + // Call the protocol decoder callbacks to notify the decoders that this FD + // changed. + // + vector::iterator it; + for(it = m_connect_callbacks.begin(); it != m_connect_callbacks.end(); ++it) { - return false; + (*it)->on_event(evt, CT_TUPLE_CHANGE); } + + return true; } // - // If we reach this point and the protocol is not set yet, we assume this - // connection is UDP, because TCP would fail if the address is changed in + // If we reach this point and the protocol is not set yet, we assume this + // connection is UDP, because TCP would fail if the address is changed in // the middle of a connection. // - if(evt->m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_l4proto == SCAP_L4_UNKNOWN) + if(evt->m_fdinfo->m_type == SCAP_FD_IPV4_SOCK) + { + if(evt->m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_l4proto == SCAP_L4_UNKNOWN) + { + evt->m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_UDP; + } + } + else if(evt->m_fdinfo->m_type == SCAP_FD_IPV6_SOCK) { - evt->m_fdinfo->m_sockinfo.m_ipv4info.m_fields.m_l4proto = SCAP_L4_UDP; + if(evt->m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_l4proto == SCAP_L4_UNKNOWN) + { + evt->m_fdinfo->m_sockinfo.m_ipv6info.m_fields.m_l4proto = SCAP_L4_UDP; + } } // @@ -1764,20 +3302,216 @@ bool sinsp_parser::update_fd(sinsp_evt *evt, sinsp_evt_param *parinfo) // m_inspector->m_network_interfaces->update_fd(evt->m_fdinfo); + // + // Call the protocol decoder callbacks to notify the decoders that this FD + // changed. + // + vector::iterator it; + for(it = m_connect_callbacks.begin(); it != m_connect_callbacks.end(); ++it) + { + (*it)->on_event(evt, CT_TUPLE_CHANGE); + } + return true; } -void sinsp_parser::swap_ipv4_addresses(sinsp_fdinfo_t* fdinfo) +void sinsp_parser::swap_addresses(sinsp_fdinfo_t* fdinfo) +{ + if(fdinfo->m_type == SCAP_FD_IPV4_SOCK) + { + uint32_t tip; + uint16_t tport; + + tip = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip; + tport = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport; + fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip; + fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport; + fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip = tip; + fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport = tport; + } + else + { + ipv6addr tip; + uint16_t tport; + + tip = fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip; + tport = fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport; + + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sip = fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip;; + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_sport = fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport; + + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dip = tip; + fdinfo->m_sockinfo.m_ipv6info.m_fields.m_dport = tport; + } +} + +uint32_t sinsp_parser::parse_tracer(sinsp_evt *evt, int64_t retval) +{ + sinsp_threadinfo* tinfo = evt->m_tinfo; + ASSERT(tinfo); + + // + // Extract the data buffer + // + sinsp_evt_param *parinfo = evt->get_param(1); + char* data = parinfo->m_val; + uint32_t datalen = parinfo->m_len; + sinsp_tracerparser* p = tinfo->m_tracer_parser; + + if(p == NULL) + { + p = tinfo->m_tracer_parser = new sinsp_tracerparser(m_inspector); + } + + p->m_tinfo = tinfo; + + p->process_event_data(data, datalen, evt->get_ts()); + + if(p->m_res == sinsp_tracerparser::RES_TRUNCATED) + { + if(!m_inspector->m_is_dumping) + { + evt->m_filtered_out = true; + } + + return p->m_res; + } + + p->m_args.first = &p->m_argnames; + p->m_args.second = &p->m_argvals; + + // + // Populate the user event that we will send up the stack instead of the write + // + uint8_t* fakeevt_storage = (uint8_t*)m_fake_userevt; + m_fake_userevt->ts = evt->m_pevt->ts; + m_fake_userevt->tid = evt->m_pevt->tid; + m_fake_userevt->nparams = 3; + + if(p->m_res == sinsp_tracerparser::RES_OK) + { + if(p->m_type_str[0] == '>') + { + m_fake_userevt->type = PPME_TRACER_E; + } + else + { + m_fake_userevt->type = PPME_TRACER_X; + } + + uint16_t *lens = (uint16_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr)); + lens[0] = 8; + lens[1] = 8; + lens[2] = 8; + + *(uint64_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr) + 6) = p->m_id; + *(uint64_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr) + 14) = (uint64_t)&p->m_tags; + *(uint64_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr) + 22) = (uint64_t)&p->m_args; + } + else + { + uint32_t flags = evt->m_fdinfo->m_flags; + + if(!(flags & sinsp_fdinfo_t::FLAGS_IS_TRACER_FD)) + { + return p->m_res; + } + + // + // Parsing error. + // We don't know the direction, so we use enter. + // + p->m_argnames.clear(); + p->m_argvals.clear(); + + m_fake_userevt->type = PPME_TRACER_E; + + uint16_t *lens = (uint16_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr)); + lens[0] = 8; + lens[1] = 8; + lens[2] = 8; + + p->m_tags.clear(); + m_tracer_error_string = "invalid tracer " + string(data, datalen) + ", len" + to_string(datalen); + p->m_tags.push_back((char*)m_tracer_error_string.c_str()); + *(uint64_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr) + 6) = 0; + *(uint64_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr) + 14) = (uint64_t)&p->m_tags; + *(uint64_t *)(fakeevt_storage + sizeof(struct ppm_evt_hdr) + 22) = (uint64_t)&p->m_args; + } + + scap_evt* tevt = evt->m_pevt; + evt->m_pevt = m_fake_userevt; + evt->init(); + evt->m_poriginal_evt = tevt; + evt->m_flags |= (uint32_t)sinsp_evt::SINSP_EF_IS_TRACER; + + // + // Update some thread information + // + tinfo->m_lastevent_fd = -1; + tinfo->m_lastevent_type = PPME_TRACER_E; + tinfo->m_latency = 0; + tinfo->m_last_latency_entertime = 0; + + return p->m_res; +} + +bool sinsp_parser::detect_and_process_tracer_write(sinsp_evt *evt, + int64_t retval, + ppm_event_flags eflags) { - uint32_t tip; - uint16_t tport; + // + // Tracers get into the engine as normal writes, but the FD has a flag to + // quickly recognize them. + // + uint32_t flags = evt->m_fdinfo->m_flags; + + if(!(flags & sinsp_fdinfo_t::FLAGS_IS_NOT_TRACER_FD)) + { + sinsp_fdinfo_t* orifdinfo = evt->m_fdinfo; + if(orifdinfo->m_flags & sinsp_fdinfo_t::FLAGS_IS_TRACER_FD) + { + parse_tracer(evt, retval); + return true; + } + else + { + if(orifdinfo->m_flags & sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE) + { + if(eflags & EF_WRITES_TO_FD) + { + // + // We have not determined if this FD is a tracer FD or not. + // We're going to try to parse it. + // If the parsing succeeds, we mark it as a tracer FD. If it + // fails we mark it an NOT a tracer FD. Otherwise, we wait + // for the next buffer and we'll try again. + // + sinsp_tracerparser::parse_result pres = + (sinsp_tracerparser::parse_result)parse_tracer(evt, retval); + + if(pres == sinsp_tracerparser::RES_OK) + { + // + // This FD has been recognized to be a tracer one. + // We do two things: mark it for future reference, and tell + // the driver to enable tracers capture (if we haven't done + // it yet). + // + orifdinfo->m_flags |= sinsp_fdinfo_t::FLAGS_IS_TRACER_FD; + m_inspector->enable_tracers_capture(); + return true; + } + else if (pres == sinsp_tracerparser::RES_FAILED) + { + orifdinfo->m_flags |= sinsp_fdinfo_t::FLAGS_IS_NOT_TRACER_FD; + } + } + } + } + } - tip = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip; - tport = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport; - fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sip = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip; - fdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport = fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport; - fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dip = tip; - fdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport = tport; + return false; } void sinsp_parser::parse_rw_exit(sinsp_evt *evt) @@ -1786,12 +3520,7 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) int64_t retval; int64_t tid = evt->get_tid(); sinsp_evt *enter_evt = &m_tmp_evt; - ppm_event_flags eflags = evt->get_flags(); - - if(!evt->m_fdinfo) - { - return; - } + ppm_event_flags eflags = evt->get_info_flags(); // // Extract the return value @@ -1800,6 +3529,28 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) ASSERT(parinfo->m_len == sizeof(int64_t)); retval = *(int64_t *)parinfo->m_val; + if(evt->m_fdinfo == NULL) + { + if(!m_inspector->is_live()) + { + if((evt->get_dump_flags() & SCAP_DF_TRACER) != 0) + { + parse_tracer(evt, retval); + return; + } + } + + return; + } + + // + // Check if this is a tracer write on /dev/null, treat it in a special way + // + if(detect_and_process_tracer_write(evt, retval, eflags)) + { + return; + } + // // If the operation was successful, validate that the fd exists // @@ -1807,6 +3558,11 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) { uint16_t etype = evt->get_type(); + if (evt->m_fdinfo->m_type == SCAP_FD_IPV4_SOCK || + evt->m_fdinfo->m_type == SCAP_FD_IPV6_SOCK) { + evt->m_fdinfo->set_socket_connected(); + } + if(eflags & EF_READS_FROM_FD) { char *data; @@ -1836,7 +3592,8 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) scap_fd_type fdtype = evt->m_fdinfo->m_type; - if(fdtype == SCAP_FD_IPV4_SOCK) + if(fdtype == SCAP_FD_IPV4_SOCK || + fdtype == SCAP_FD_IPV6_SOCK) { if(evt->m_fdinfo->is_role_none()) { @@ -1848,12 +3605,13 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) if(evt->m_fdinfo->is_role_client()) { - swap_ipv4_addresses(evt->m_fdinfo); + swap_addresses(evt->m_fdinfo); } - sinsp_utils::sockinfo_to_str(&evt->m_fdinfo->m_sockinfo, - fdtype, &evt->m_paramstr_storage[0], - evt->m_paramstr_storage.size()); + sinsp_utils::sockinfo_to_str(&evt->m_fdinfo->m_sockinfo, + fdtype, &evt->m_paramstr_storage[0], + (uint32_t)evt->m_paramstr_storage.size(), + m_inspector->m_hostname_and_port_resolution_enabled); evt->m_fdinfo->m_name = &evt->m_paramstr_storage[0]; } @@ -1879,9 +3637,26 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) datalen = parinfo->m_len; data = parinfo->m_val; + // + // If there's an fd listener, call it now + // if(m_fd_listener) { - m_fd_listener->on_read(evt, tid, evt->m_tinfo->m_lastevent_fd, data, (uint32_t)retval, datalen); + m_fd_listener->on_read(evt, tid, evt->m_tinfo->m_lastevent_fd, evt->m_fdinfo, + data, (uint32_t)retval, datalen); + } + + // + // Call the protocol decoder callbacks associated to this event + // + if(evt->m_fdinfo->m_callbacks) + { + vector* cbacks = &(evt->m_fdinfo->m_callbacks->m_read_callbacks); + + for(auto it = cbacks->begin(); it != cbacks->end(); ++it) + { + (*it)->on_read(evt, data, datalen); + } } } else @@ -1895,7 +3670,7 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) tupleparam = 2; } - if(tupleparam != -1 && (evt->m_fdinfo->m_name.length() == 0 || evt->m_fdinfo->is_udp_socket())) + if(tupleparam != -1 && (evt->m_fdinfo->m_name.length() == 0 || !evt->m_fdinfo->is_tcp_socket())) { // // sendto contains tuple info in the enter event. @@ -1913,7 +3688,8 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) scap_fd_type fdtype = evt->m_fdinfo->m_type; - if(fdtype == SCAP_FD_IPV4_SOCK) + if(fdtype == SCAP_FD_IPV4_SOCK || + fdtype == SCAP_FD_IPV6_SOCK) { if(evt->m_fdinfo->is_role_none()) { @@ -1925,12 +3701,13 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) if(evt->m_fdinfo->is_role_server()) { - swap_ipv4_addresses(evt->m_fdinfo); + swap_addresses(evt->m_fdinfo); } - sinsp_utils::sockinfo_to_str(&evt->m_fdinfo->m_sockinfo, - fdtype, &evt->m_paramstr_storage[0], - evt->m_paramstr_storage.size()); + sinsp_utils::sockinfo_to_str(&evt->m_fdinfo->m_sockinfo, + fdtype, &evt->m_paramstr_storage[0], + (uint32_t)evt->m_paramstr_storage.size(), + m_inspector->m_hostname_and_port_resolution_enabled); evt->m_fdinfo->m_name = &evt->m_paramstr_storage[0]; } @@ -1948,14 +3725,87 @@ void sinsp_parser::parse_rw_exit(sinsp_evt *evt) datalen = parinfo->m_len; data = parinfo->m_val; + // + // If there's an fd listener, call it now + // if(m_fd_listener) { - m_fd_listener->on_write(evt, tid, evt->m_tinfo->m_lastevent_fd, data, (uint32_t)retval, datalen); + m_fd_listener->on_write(evt, tid, evt->m_tinfo->m_lastevent_fd, evt->m_fdinfo, + data, (uint32_t)retval, datalen); + } + + // + // Call the protocol decoder callbacks associated to this event + // + if(evt->m_fdinfo->m_callbacks) + { + vector* cbacks = &(evt->m_fdinfo->m_callbacks->m_write_callbacks); + + for(auto it = cbacks->begin(); it != cbacks->end(); ++it) + { + (*it)->on_write(evt, data, datalen); + } + } + } + } else if (m_track_connection_status) { + if (evt->m_fdinfo->m_type == SCAP_FD_IPV4_SOCK || + evt->m_fdinfo->m_type == SCAP_FD_IPV6_SOCK) { + evt->m_fdinfo->set_socket_failed(); + if (m_fd_listener) + { + m_fd_listener->on_socket_status_changed(evt); } } } } +void sinsp_parser::parse_sendfile_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + int64_t retval; + + if(!evt->m_fdinfo) + { + return; + } + + // + // Extract the return value + // + parinfo = evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(int64_t)); + retval = *(int64_t *)parinfo->m_val; + + // + // If the operation was successful, validate that the fd exists + // + if(retval >= 0) + { + sinsp_evt *enter_evt = &m_tmp_evt; + int64_t fdin; + + if(!retrieve_enter_event(enter_evt, evt)) + { + return; + } + + // + // Extract the in FD + // + parinfo = enter_evt->get_param(1); + ASSERT(parinfo->m_len == sizeof(int64_t)); + fdin = *(int64_t *)parinfo->m_val; + + // + // If there's an fd listener, call it now + // + if(m_fd_listener) + { + m_fd_listener->on_sendfile(evt, fdin, (uint32_t)retval); + } + } +} + void sinsp_parser::parse_eventfd_exit(sinsp_evt *evt) { sinsp_evt_param *parinfo; @@ -2000,6 +3850,11 @@ void sinsp_parser::parse_chdir_exit(sinsp_evt *evt) sinsp_evt_param *parinfo; int64_t retval; + if(!evt->m_tinfo) + { + return; + } + // // Extract the return value // @@ -2040,14 +3895,14 @@ void sinsp_parser::parse_fchdir_exit(sinsp_evt *evt) // // Find the fd name // - if(evt->m_fdinfo == NULL) + if(evt->m_fdinfo == NULL || evt->m_tinfo == nullptr) { return; } // Update the thread working directory evt->m_tinfo->set_cwd((char *)evt->m_fdinfo->m_name.c_str(), - evt->m_fdinfo->m_name.size()); + (uint32_t)evt->m_fdinfo->m_name.size()); } } @@ -2072,7 +3927,7 @@ void sinsp_parser::parse_getcwd_exit(sinsp_evt *evt) { // // No thread in the table. We won't store this event, which mean that - // we won't be able to parse the correspoding exit event and we'll have + // we won't be able to parse the corresponding exit event and we'll have // to drop the information it carries. // ASSERT(false); @@ -2093,12 +3948,12 @@ void sinsp_parser::parse_getcwd_exit(sinsp_evt *evt) // following chdir(). If it does, it's almost sure there was an event drop. // In that case, we use this value to update the thread cwd. // -#if !defined(_WIN32) && !defined(__APPLE__) +#if defined(HAS_CAPTURE) #ifdef _DEBUG int target_res; char target_name[1024]; - target_res = readlink((chkstr + "/").c_str(), - target_name, + target_res = readlink((chkstr + "/").c_str(), + target_name, sizeof(target_name) - 1); if(target_res > 0) @@ -2154,6 +4009,11 @@ void sinsp_parser::parse_dup_exit(sinsp_evt *evt) sinsp_evt_param *parinfo; int64_t retval; + if(evt->m_tinfo == nullptr) + { + return; + } + // // Extract the return value // @@ -2166,15 +4026,43 @@ void sinsp_parser::parse_dup_exit(sinsp_evt *evt) // if(retval >= 0) { + // + // Heuristic to determine if a thread is part of a shell pipe + // + if(retval == 0) + { + evt->m_tinfo->m_flags |= PPM_CL_PIPE_DST; + } + if(retval == 1) + { + evt->m_tinfo->m_flags |= PPM_CL_PIPE_SRC; + } + if(evt->m_fdinfo == NULL) { return; } + // + // If the old FD is in the table, remove it properly + // + sinsp_fdinfo_t* oldfdinfo = evt->m_tinfo->get_fd(retval); + + if(oldfdinfo != NULL) + { + erase_fd_params eparams; + + eparams.m_fd = retval; + eparams.m_fdinfo = oldfdinfo; + eparams.m_remove_from_table = false; + eparams.m_tinfo = evt->m_tinfo; + eparams.m_ts = evt->get_ts(); + + erase_fd(&eparams); + } + // // Add the new fd to the table. - // NOTE: dup2 and dup3 accept an existing FD and in that case they close it. - // For us it's ok to just overwrite it. // evt->m_fdinfo = evt->m_tinfo->add_fd(retval, evt->m_fdinfo); } @@ -2192,6 +4080,11 @@ void sinsp_parser::parse_signalfd_exit(sinsp_evt *evt) retval = *(int64_t *)parinfo->m_val; ASSERT(parinfo->m_len == sizeof(int64_t)); + if(evt->m_tinfo == nullptr) + { + return; + } + // // Check if the syscall was successful // @@ -2224,6 +4117,11 @@ void sinsp_parser::parse_timerfd_create_exit(sinsp_evt *evt) retval = *(int64_t *)parinfo->m_val; ASSERT(parinfo->m_len == sizeof(int64_t)); + if(evt->m_tinfo == nullptr) + { + return; + } + // // Check if the syscall was successful // @@ -2256,6 +4154,11 @@ void sinsp_parser::parse_inotify_init_exit(sinsp_evt *evt) retval = *(int64_t *)parinfo->m_val; ASSERT(parinfo->m_len == sizeof(int64_t)); + if(evt->m_tinfo == nullptr) + { + return; + } + // // Check if the syscall was successful // @@ -2284,6 +4187,11 @@ void sinsp_parser::parse_getrlimit_setrlimit_exit(sinsp_evt *evt) uint8_t resource; int64_t curval; + if(evt->m_tinfo == nullptr) + { + return; + } + // // Extract the return value // @@ -2323,16 +4231,16 @@ void sinsp_parser::parse_getrlimit_setrlimit_exit(sinsp_evt *evt) #ifdef _DEBUG if(evt->get_type() == PPME_SYSCALL_GETRLIMIT_X) { - if(evt->m_tinfo->m_fdlimit != -1) + if(evt->m_tinfo->get_main_thread()->m_fdlimit != -1) { - ASSERT(curval == evt->m_tinfo->m_fdlimit); +// ASSERT(curval == evt->m_tinfo->get_main_thread()->m_fdlimit); } } #endif if(curval != -1) { - evt->m_tinfo->m_fdlimit = curval; + evt->m_tinfo->get_main_thread()->m_fdlimit = curval; } else { @@ -2396,7 +4304,12 @@ void sinsp_parser::parse_prlimit_exit(sinsp_evt *evt) tid = *(int64_t *)parinfo->m_val; ASSERT(parinfo->m_len == sizeof(int64_t)); - sinsp_threadinfo* ptinfo = m_inspector->get_thread(tid, true); + if(tid == 0) + { + tid = evt->get_tid(); + } + + sinsp_threadinfo* ptinfo = m_inspector->get_thread(tid, true, true); if(ptinfo == NULL) { ASSERT(false); @@ -2406,7 +4319,7 @@ void sinsp_parser::parse_prlimit_exit(sinsp_evt *evt) // // update the process fdlimit // - ptinfo->m_fdlimit = newcur; + ptinfo->get_main_thread()->m_fdlimit = newcur; } } } @@ -2420,9 +4333,12 @@ void sinsp_parser::parse_select_poll_epollwait_enter(sinsp_evt *evt) return; } + if(evt->m_tinfo->m_lastevent_data == NULL) + { + evt->m_tinfo->m_lastevent_data = reserve_event_buffer(); + } *(uint64_t*)evt->m_tinfo->m_lastevent_data = evt->get_ts(); } - void sinsp_parser::parse_fcntl_enter(sinsp_evt *evt) { if(!evt->m_tinfo) @@ -2479,3 +4395,714 @@ void sinsp_parser::parse_fcntl_exit(sinsp_evt *evt) evt->m_fdinfo = evt->m_tinfo->add_fd(retval, evt->m_fdinfo); } } + +void sinsp_parser::parse_context_switch(sinsp_evt* evt) +{ + if(evt->m_tinfo) + { + sinsp_evt_param *parinfo; + parinfo = evt->get_param(1); + evt->m_tinfo->m_pfmajor = *(uint64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint64_t)); + + parinfo = evt->get_param(2); + evt->m_tinfo->m_pfminor = *(uint64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint64_t)); + + auto main_tinfo = evt->m_tinfo->get_main_thread(); + if(main_tinfo) + { + parinfo = evt->get_param(3); + main_tinfo->m_vmsize_kb = *(uint32_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint32_t)); + + parinfo = evt->get_param(4); + main_tinfo->m_vmrss_kb = *(uint32_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint32_t)); + + parinfo = evt->get_param(5); + main_tinfo->m_vmswap_kb = *(uint32_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint32_t)); + } + } +} + +void sinsp_parser::parse_brk_munmap_mmap_exit(sinsp_evt* evt) +{ + ASSERT(evt->m_tinfo); + if(evt->m_tinfo) + { + sinsp_evt_param *parinfo; + + parinfo = evt->get_param(1); + evt->m_tinfo->m_vmsize_kb = *(uint32_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint32_t)); + + parinfo = evt->get_param(2); + evt->m_tinfo->m_vmrss_kb = *(uint32_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint32_t)); + + parinfo = evt->get_param(3); + evt->m_tinfo->m_vmswap_kb = *(uint32_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(uint32_t)); + } +} + +void sinsp_parser::parse_setresuid_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + int64_t retval; + sinsp_evt *enter_evt = &m_tmp_evt; + + // + // Extract the return value + // + parinfo = evt->get_param(0); + retval = *(int64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int64_t)); + + if(retval >= 0 && retrieve_enter_event(enter_evt, evt)) + { + parinfo = enter_evt->get_param(1); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + uint32_t new_euid = *(uint32_t *)parinfo->m_val; + + if(new_euid < std::numeric_limits::max()) + { + if (evt->get_thread_info()) { + evt->get_thread_info()->m_uid = new_euid; + } + } + } +} + +void sinsp_parser::parse_setresgid_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + int64_t retval; + sinsp_evt *enter_evt = &m_tmp_evt; + + // + // Extract the return value + // + parinfo = evt->get_param(0); + retval = *(int64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int64_t)); + + if(retval >= 0 && retrieve_enter_event(enter_evt, evt)) + { + parinfo = enter_evt->get_param(1); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + uint32_t new_egid = *(uint32_t *)parinfo->m_val; + + if(new_egid < std::numeric_limits::max()) + { + if (evt->get_thread_info()) { + evt->get_thread_info()->m_gid = new_egid; + } + } + } +} + +void sinsp_parser::parse_setuid_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + int64_t retval; + sinsp_evt *enter_evt = &m_tmp_evt; + + // + // Extract the return value + // + parinfo = evt->get_param(0); + retval = *(int64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int64_t)); + + if(retval >= 0 && retrieve_enter_event(enter_evt, evt)) + { + parinfo = enter_evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + uint32_t new_euid = *(uint32_t *)parinfo->m_val; + if (evt->get_thread_info()) { + evt->get_thread_info()->m_uid = new_euid; + } + } +} + +void sinsp_parser::parse_setgid_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + int64_t retval; + sinsp_evt *enter_evt = &m_tmp_evt; + + // + // Extract the return value + // + parinfo = evt->get_param(0); + retval = *(int64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int64_t)); + + if(retval >= 0 && retrieve_enter_event(enter_evt, evt)) + { + parinfo = enter_evt->get_param(0); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + uint32_t new_egid = *(uint32_t *)parinfo->m_val; + if (evt->get_thread_info()) { + evt->get_thread_info()->m_gid = new_egid; + } + } +} + +namespace +{ + std::string generate_error_message(const Json::Value& value, const char* field) { + std::string val_as_string = value.isConvertibleTo(Json::stringValue) ? value.asString().c_str() : "value not convertible to string"; + std::string err_msg = "Unable to convert json value '" + val_as_string + "' for the field: '" + field +"'"; + + return std::move(err_msg); + } + + bool check_int64_json_is_convertible(const Json::Value& value, const char* field) { + if(!value.isNull()) + { + // isConvertibleTo doesn't seem to work on large 64 bit numbers + if(value.isInt64()) { + return true; + } else { + std::string err_msg = generate_error_message(value, field); + SINSP_DEBUG("%s",err_msg.c_str()); + } + } + return false; + } + + bool check_json_val_is_convertible(const Json::Value& value, Json::ValueType other, const char* field, bool log_message=false) + { + if(value.isNull()) { + return false; + } + + if(!value.isConvertibleTo(other)) { + std::string err_msg; + + if(log_message) { + err_msg = generate_error_message(value, field); + SINSP_WARNING("%s",err_msg.c_str()); + } else { + if(g_logger.get_severity() >= sinsp_logger::SEV_DEBUG) { + err_msg = generate_error_message(value, field); + SINSP_DEBUG("%s",err_msg.c_str()); + } + } + return false; + } + return true; + } +} + +void sinsp_parser::parse_container_json_evt(sinsp_evt *evt) +{ + ASSERT(m_inspector); + + if(evt->m_tinfo_ref != nullptr) + { + const auto& container_id = evt->m_tinfo_ref->m_container_id; + const auto container = m_inspector->m_container_manager.get_container(container_id); + if(container != nullptr && container->is_successful()) + { + SINSP_DEBUG("Ignoring container event for already successful lookup of %s", container_id.c_str()); + evt->m_filtered_out = true; + return; + } + } + + sinsp_evt_param *parinfo = evt->get_param(0); + ASSERT(parinfo); + ASSERT(parinfo->m_len > 0); + std::string json(parinfo->m_val, parinfo->m_len); + SINSP_DEBUG("Parsing Container JSON=%s", json.c_str()); + Json::Value root; + if(Json::Reader().parse(json, root)) + { + auto container_info = std::make_shared(); + const Json::Value& container = root["container"]; + const Json::Value& id = container["id"]; + if(check_json_val_is_convertible(id, Json::stringValue, "id")) + { + container_info->m_id = id.asString(); + } + const Json::Value& full_id = container["full_id"]; + if(check_json_val_is_convertible(full_id, Json::stringValue, "full_id")) + { + container_info->m_full_id = full_id.asString(); + } + const Json::Value& type = container["type"]; + if(check_json_val_is_convertible(type, Json::uintValue, "type")) + { + container_info->m_type = static_cast(type.asUInt()); + } + const Json::Value& name = container["name"]; + if(check_json_val_is_convertible(name, Json::stringValue, "name")) + { + container_info->m_name = name.asString(); + } + + const Json::Value& is_pod_sandbox = container["is_pod_sandbox"]; + if(check_json_val_is_convertible(is_pod_sandbox, Json::booleanValue, "is_pod_sandbox")) + { + container_info->m_is_pod_sandbox = is_pod_sandbox.asBool(); + } + + const Json::Value& image = container["image"]; + if(check_json_val_is_convertible(image, Json::stringValue, "image")) + { + container_info->m_image = image.asString(); + } + const Json::Value& imageid = container["imageid"]; + if(check_json_val_is_convertible(imageid, Json::stringValue, "imageid")) + { + container_info->m_imageid = imageid.asString(); + } + const Json::Value& imagerepo = container["imagerepo"]; + if(check_json_val_is_convertible(imagerepo, Json::stringValue, "imagerepo")) + { + container_info->m_imagerepo = imagerepo.asString(); + } + const Json::Value& imagetag = container["imagetag"]; + if(check_json_val_is_convertible(imagetag, Json::stringValue, "imagetag")) + { + container_info->m_imagetag = imagetag.asString(); + } + const Json::Value& imagedigest = container["imagedigest"]; + if(check_json_val_is_convertible(imagedigest, Json::stringValue, "imagedigest")) + { + container_info->m_imagedigest = imagedigest.asString(); + } + const Json::Value& privileged = container["privileged"]; + if(check_json_val_is_convertible(privileged, Json::booleanValue, "privileged")) + { + container_info->m_privileged = privileged.asBool(); + } + const Json::Value& lookup_state = container["lookup_state"]; + if(check_json_val_is_convertible(lookup_state, Json::uintValue, "lookup_state")) + { + container_info->m_lookup_state = static_cast(lookup_state.asUInt()); + switch(container_info->m_lookup_state) + { + case sinsp_container_lookup_state::STARTED: + case sinsp_container_lookup_state::SUCCESSFUL: + case sinsp_container_lookup_state::FAILED: + break; + default: + container_info->m_lookup_state = sinsp_container_lookup_state::SUCCESSFUL; + } + + // state == STARTED doesn't make sense in a scap file + // as there's no actual lookup that would ever finish + if(!evt->m_tinfo_ref && container_info->m_lookup_state == sinsp_container_lookup_state::STARTED) + { + SINSP_DEBUG("Rewriting lookup_state = STARTED from scap file to FAILED for container %s", + container_info->m_id.c_str()); + container_info->m_lookup_state = sinsp_container_lookup_state::FAILED; + } + } + + const Json::Value& created_time = container["created_time"]; + if(check_int64_json_is_convertible(created_time, "created_time")) + { + container_info->m_created_time = created_time.asInt64(); + } + +#if !defined(MINIMAL_BUILD) && !defined(_WIN32) + libsinsp::container_engine::docker::parse_json_mounts(container["Mounts"], container_info->m_mounts); +#endif + + sinsp_container_info::container_health_probe::parse_health_probes(container, container_info->m_health_probes); + + const Json::Value& contip = container["ip"]; + if(check_json_val_is_convertible(contip, Json::stringValue, "ip")) + { + uint32_t ip; + + if(inet_pton(AF_INET, contip.asString().c_str(), &ip) == -1) + { + throw sinsp_exception("Invalid 'ip' field while parsing container info: " + json); + } + + container_info->m_container_ip = ntohl(ip); + } + + const Json::Value &port_mappings = container["port_mappings"]; + + if(check_json_val_is_convertible(port_mappings, Json::arrayValue, "port_mappings")) + { + for (Json::Value::ArrayIndex i = 0; i != port_mappings.size(); i++) + { + sinsp_container_info::container_port_mapping map; + const Json::Value &host_ip = port_mappings[i]["HostIp"]; + // We log message for HostIp conversion failure at Warning level + if(check_json_val_is_convertible(host_ip, Json::intValue, "HostIp", true)) { + map.m_host_ip = host_ip.asInt(); + } + const Json::Value& host_port = port_mappings[i]["HostPort"]; + // We log message for HostPort conversion failure at Warning level + if(check_json_val_is_convertible(host_port, Json::intValue, "HostPort", true)) { + map.m_host_port = (uint16_t) host_port.asInt(); + } + const Json::Value& container_port = port_mappings[i]["ContainerPort"]; + // We log message for ContainerPort conversion failure at Warning level + if(check_json_val_is_convertible(container_port, Json::intValue, "ContainerPort", true)) { + map.m_container_port = (uint16_t) container_port.asInt(); + } + container_info->m_port_mappings.push_back(map); + } + } + + vector labels = container["labels"].getMemberNames(); + for(vector::const_iterator it = labels.begin(); it != labels.end(); ++it) + { + string val = container["labels"][*it].asString(); + container_info->m_labels[*it] = val; + } + + const Json::Value& env_vars = container["env"]; + + for(const auto& env_var : env_vars) + { + if(env_var.isString()) + { + container_info->m_env.emplace_back(env_var.asString()); + } + } + + const Json::Value& memory_limit = container["memory_limit"]; + if(check_int64_json_is_convertible(memory_limit, "memory_limit")) + { + container_info->m_memory_limit = memory_limit.asInt64(); + } + + const Json::Value& swap_limit = container["swap_limit"]; + if(check_int64_json_is_convertible(swap_limit, "swap_limit")) + { + container_info->m_swap_limit = swap_limit.asInt64(); + } + + const Json::Value& cpu_shares = container["cpu_shares"]; + if(check_int64_json_is_convertible(cpu_shares, "cpu_shares")) + { + container_info->m_cpu_shares = cpu_shares.asInt64(); + } + + const Json::Value& cpu_quota = container["cpu_quota"]; + if(check_int64_json_is_convertible(cpu_quota, "cpu_quota")) + { + container_info->m_cpu_quota = cpu_quota.asInt64(); + } + + const Json::Value& cpu_period = container["cpu_period"]; + if(check_int64_json_is_convertible(cpu_period, "cpu_period")) + { + container_info->m_cpu_period = cpu_period.asInt64(); + } + + const Json::Value& cpuset_cpu_count = container["cpuset_cpu_count"]; + if(check_json_val_is_convertible(cpuset_cpu_count, Json::intValue, "cpuset_cpu_count")) + { + container_info->m_cpuset_cpu_count = cpuset_cpu_count.asInt(); + } + + const Json::Value& mesos_task_id = container["mesos_task_id"]; + if(check_json_val_is_convertible(mesos_task_id, Json::stringValue, "mesos_task_id")) + { + container_info->m_mesos_task_id = mesos_task_id.asString(); + } + + const Json::Value& metadata_deadline = container["metadata_deadline"]; + if(!metadata_deadline.isNull()) + { + // isConvertibleTo doesn't seem to work on large 64 bit numbers + if(metadata_deadline.isUInt64()) { + container_info->m_metadata_deadline = metadata_deadline.asUInt64(); + } else { + SINSP_DEBUG("Unable to convert json value for field: %s", "metadata_deadline"); + } + } + + if(!container_info->is_successful()) + { + SINSP_DEBUG("Filtering container event for failed lookup of %s (but calling callbacks anyway)", container_info->m_id.c_str()); + evt->m_filtered_out = true; + } + evt->m_tinfo_ref = container_info->get_tinfo(m_inspector); + evt->m_tinfo = evt->m_tinfo_ref.get(); + m_inspector->m_container_manager.add_container(container_info, evt->get_thread_info(true)); + /* + SINSP_STR_DEBUG("Container\n-------\nID:" + container_info.m_id + + "\nType: " + std::to_string(container_info.m_type) + + "\nName: " + container_info.m_name + + "\nImage: " + container_info.m_image + + "\nMesos Task ID: " + container_info.m_mesos_task_id); + */ + } + else + { + std::string errstr; + errstr = Json::Reader().getFormattedErrorMessages(); + throw sinsp_exception("Invalid JSON encountered while parsing container info: " + json + "error=" + errstr); + } +} + +void sinsp_parser::parse_container_evt(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + auto container = std::make_shared(); + + parinfo = evt->get_param(0); + container->m_id = parinfo->m_val; + + parinfo = evt->get_param(1); + ASSERT(parinfo->m_len == sizeof(uint32_t)); + container->m_type = (sinsp_container_type) *(uint32_t *)parinfo->m_val; + + parinfo = evt->get_param(2); + container->m_name = parinfo->m_val; + + parinfo = evt->get_param(3); + container->m_image = parinfo->m_val; + + m_inspector->m_container_manager.add_container(container, evt->get_thread_info(true)); +} + +void sinsp_parser::parse_cpu_hotplug_enter(sinsp_evt *evt) +{ + if(m_inspector->is_live()) + { + throw sinsp_exception("CPU " + evt->get_param_value_str("cpu") + + " configuration change detected. Aborting."); + } +} + +uint8_t* sinsp_parser::reserve_event_buffer() +{ + if(m_tmp_events_buffer.empty()) + { + return (uint8_t*)malloc(sizeof(uint8_t)*SP_EVT_BUF_SIZE); + } + else + { + auto ptr = m_tmp_events_buffer.top(); + m_tmp_events_buffer.pop(); + return ptr; + } +} + +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) +int sinsp_parser::get_k8s_version(const std::string& json) +{ + if(m_k8s_capture_version == k8s_state_t::CAPTURE_VERSION_NONE) + { + SINSP_STR_DEBUG(json); + Json::Value root; + if(Json::Reader().parse(json, root)) + { + const Json::Value& items = root["items"]; // new + if(!items.isNull()) + { + SINSP_STR_DEBUG("K8s capture version " + + std::to_string(k8s_state_t::CAPTURE_VERSION_2) + + " detected."); + m_k8s_capture_version = k8s_state_t::CAPTURE_VERSION_2; + return m_k8s_capture_version; + } + + const Json::Value& object = root["object"]; // old + if(!object.isNull()) + { + SINSP_STR_DEBUG("K8s capture version " + + std::to_string(k8s_state_t::CAPTURE_VERSION_2) + + " detected."); + m_k8s_capture_version = k8s_state_t::CAPTURE_VERSION_1; + return m_k8s_capture_version; + } + throw sinsp_exception("Unrecognized K8s capture format."); + } + else + { + std::string errstr; + errstr = Json::Reader().getFormattedErrorMessages(); + throw sinsp_exception("Invalid K8s capture JSON encountered (" + errstr + ")"); + } + } + + return m_k8s_capture_version; +} + +void sinsp_parser::parse_k8s_evt(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo = evt->get_param(0); + ASSERT(parinfo); + ASSERT(parinfo->m_len > 0); + std::string json(parinfo->m_val, parinfo->m_len); + //SINSP_STR_DEBUG(json); + ASSERT(m_inspector); + if(!m_inspector) + { + throw sinsp_exception("Inspector is null, K8s client can not be created."); + } + if(!m_inspector->m_k8s_client) + { + m_inspector->make_k8s_client(); + } + if(m_inspector->m_k8s_client) + { + m_inspector->m_k8s_client->simulate_watch_event(std::move(json), get_k8s_version(json)); + } + else + { + throw sinsp_exception("K8s client can not be created."); + } +} + +void sinsp_parser::parse_mesos_evt(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo = evt->get_param(0); + ASSERT(parinfo); + ASSERT(parinfo->m_len > 0); + std::string json(parinfo->m_val, parinfo->m_len); + //SINSP_STR_DEBUG(json); + ASSERT(m_inspector); + ASSERT(m_inspector->m_mesos_client); + m_inspector->m_mesos_client->simulate_event(json); +} +#endif // #if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + +void sinsp_parser::parse_chroot_exit(sinsp_evt *evt) +{ + auto parinfo = evt->get_param(0); + auto retval = *(int64_t *)parinfo->m_val; + if(retval == 0 && evt->m_tinfo != nullptr) + { + const char* resolved_path; + auto path = evt->get_param_as_str(1, &resolved_path); + if(resolved_path[0] == 0) + { + evt->m_tinfo->m_root = path; + } + else + { + evt->m_tinfo->m_root = resolved_path; + } + // Root change, let's detect if we are on a container + ASSERT(m_inspector); + m_inspector->m_container_manager.resolve_container(evt->m_tinfo, m_inspector->is_live()); + } +} + +void sinsp_parser::parse_setsid_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + int64_t retval; + + // + // Extract the return value + // + parinfo = evt->get_param(0); + retval = *(int64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int64_t)); + + if(retval >= 0) + { + if (evt->get_thread_info()) { + evt->get_thread_info()->m_sid = retval; + } + } +} + +void sinsp_parser::parse_getsockopt_exit(sinsp_evt *evt) +{ + sinsp_evt_param *parinfo; + int64_t retval; + int64_t err; + int64_t fd; + int8_t level, optname; + + if(!evt->m_tinfo) + { + return; + } + + parinfo = evt->get_param(1); + fd = *(int64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int64_t)); + + evt->m_fdinfo = evt->m_tinfo->get_main_thread()->get_fd(fd); + evt->m_tinfo->m_lastevent_fd = fd; + + // right now we only parse getsockopt() for SO_ERROR options + // if that ever changes, move this check inside + // the `if (level == PPM_SOCKOPT_LEVEL_SOL_SOCKET ...)` block + if (!m_track_connection_status) + { + return; + } + + //evt->m_fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd); + + // + // Extract the return value + // + parinfo = evt->get_param(0); + retval = *(int64_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int64_t)); + + if(retval < 0) + { + return; + } + + parinfo = evt->get_param(2); + level = *(int8_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int8_t)); + + parinfo = evt->get_param(3); + optname = *(int8_t *)parinfo->m_val; + ASSERT(parinfo->m_len == sizeof(int8_t)); + + if(level == PPM_SOCKOPT_LEVEL_SOL_SOCKET && optname == PPM_SOCKOPT_SO_ERROR) + { + if (!evt->m_fdinfo) + { + return; + } + + parinfo = evt->get_param(4); + ASSERT(*parinfo->m_val == PPM_SOCKOPT_IDX_ERRNO); + ASSERT(parinfo->m_len == sizeof(int64_t) + 1); + err = *(int64_t *)(parinfo->m_val + 1); // add 1 byte to skip over PT_DYN param index + + evt->m_errorcode = (int32_t)err; + if (err < 0) + { + evt->m_fdinfo->set_socket_failed(); + } + else + { + evt->m_fdinfo->set_socket_connected(); + } + if (m_fd_listener) + { + m_fd_listener->on_socket_status_changed(evt); + } + } +} + +void sinsp_parser::free_event_buffer(uint8_t *ptr) +{ + if(m_tmp_events_buffer.size() < m_inspector->m_thread_manager->m_threadtable.size()) + { + m_tmp_events_buffer.push(ptr); + } + else + { + free(ptr); + } +} diff --git a/userspace/libsinsp/parsers.h b/userspace/libsinsp/parsers.h index 346ceb13d1..49ad84e15f 100644 --- a/userspace/libsinsp/parsers.h +++ b/userspace/libsinsp/parsers.h @@ -1,28 +1,40 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ //////////////////////////////////////////////////////////////////////////// // Public definitions for the scap library //////////////////////////////////////////////////////////////////////////// #pragma once +#include "sinsp.h" class sinsp_fd_listener; +class metaevents_state +{ +public: + bool m_new_group; + uint32_t m_n_additional_events_to_add; + sinsp_evt m_metaevt; + scap_evt* m_piscapevt; + uint32_t m_scap_buf_size; +}; + class sinsp_parser { public: @@ -33,33 +45,73 @@ class sinsp_parser // Processing entry point // void process_event(sinsp_evt* evt); + void event_cleanup(sinsp_evt* evt); + void erase_fd(erase_fd_params* params); + // + // Get the enter event matching the last received event + // + bool retrieve_enter_event(sinsp_evt* enter_evt, sinsp_evt* exit_evt); + + // + // Combine the openat arguments into a full file name + // + static void parse_openat_dir(sinsp_evt *evt, char* name, int64_t dirfd, OUT string* sdir); + + // + // Protocol decoder infrastructure methods + // + sinsp_protodecoder* add_protodecoder(string decoder_name); + void register_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec); + + void schedule_k8s_events(); + void schedule_mesos_events(); + + // + // Protocol decoders callback lists + // + vector m_open_callbacks; + vector m_connect_callbacks; + + ppm_event_flags m_drop_event_flags; + + // + // Initializers + // + static void init_scapevt(metaevents_state& evt_state, uint16_t evt_type, uint16_t buf_size); + private: + // + // Initializers + // + inline void init_metaevt(metaevents_state& evt_state, uint16_t evt_type, uint16_t buf_size); + // // Helpers // bool reset(sinsp_evt *evt); - void store_event(sinsp_evt* evt); - bool retrieve_enter_event(sinsp_evt* enter_evt, sinsp_evt* exit_evt); + inline void store_event(sinsp_evt* evt); // // Parsers // - void parse_clone_exit(sinsp_evt* evt); - void parse_execve_exit(sinsp_evt* evt); + void parse_clone_exit(sinsp_evt* evt); + void parse_execve_exit(sinsp_evt* evt); void proc_schedule_removal(sinsp_evt* evt); - void parse_open_openat_creat_exit(sinsp_evt* evt); + void parse_open_openat_creat_exit(sinsp_evt* evt); void parse_pipe_exit(sinsp_evt* evt); void parse_socketpair_exit(sinsp_evt* evt); - void parse_socket_exit(sinsp_evt* evt); + void parse_socket_exit(sinsp_evt* evt); + void parse_connect_enter(sinsp_evt* evt); void parse_connect_exit(sinsp_evt* evt); void parse_accept_exit(sinsp_evt* evt); void parse_close_enter(sinsp_evt* evt); void parse_close_exit(sinsp_evt* evt); void parse_thread_exit(sinsp_evt* evt); - void parse_rw_enter(sinsp_evt* evt); - void parse_rw_exit(sinsp_evt* evt); + inline bool detect_and_process_tracer_write(sinsp_evt *evt, int64_t retval, ppm_event_flags eflags); + inline void parse_rw_exit(sinsp_evt* evt); + void parse_sendfile_exit(sinsp_evt* evt); void parse_eventfd_exit(sinsp_evt* evt); void parse_bind_exit(sinsp_evt* evt); void parse_chdir_exit(sinsp_evt* evt); @@ -75,31 +127,73 @@ class sinsp_parser void parse_select_poll_epollwait_enter(sinsp_evt *evt); void parse_fcntl_enter(sinsp_evt* evt); void parse_fcntl_exit(sinsp_evt* evt); + void parse_context_switch(sinsp_evt* evt); + void parse_brk_munmap_mmap_exit(sinsp_evt* evt); + void parse_setresuid_exit(sinsp_evt* evt); + void parse_setresgid_exit(sinsp_evt* evt); + void parse_setuid_exit(sinsp_evt* evt); + void parse_setgid_exit(sinsp_evt* evt); + void parse_container_evt(sinsp_evt* evt); // deprecated, only for backward-compatibility + void parse_container_json_evt(sinsp_evt *evt); + inline uint32_t parse_tracer(sinsp_evt *evt, int64_t retval); + void parse_cpu_hotplug_enter(sinsp_evt* evt); + int get_k8s_version(const std::string& json); +#ifndef CYGWING_AGENT + void parse_k8s_evt(sinsp_evt *evt); + void parse_mesos_evt(sinsp_evt *evt); +#endif + void parse_chroot_exit(sinsp_evt *evt); + void parse_setsid_exit(sinsp_evt *evt); + void parse_getsockopt_exit(sinsp_evt *evt); + inline void fill_client_socket_info(sinsp_evt* evt, uint8_t* packed_data); inline void add_socket(sinsp_evt* evt, int64_t fd, uint32_t domain, uint32_t type, uint32_t protocol); + inline void infer_sendto_fdinfo(sinsp_evt *evt); inline void add_pipe(sinsp_evt *evt, int64_t tid, int64_t fd, uint64_t ino); // Return false if the update didn't happen (for example because the tuple is NULL) bool update_fd(sinsp_evt *evt, sinsp_evt_param* parinfo); - // Return false if the update didn't happen because the tuple is identical to the given address + + // Next 4 return false if the update didn't happen because the tuple is identical to the given address bool set_ipv4_addresses_and_ports(sinsp_fdinfo_t* fdinfo, uint8_t* packed_data); - // Return false if the update didn't happen because the tuple is identical to the given address bool set_ipv4_mapped_ipv6_addresses_and_ports(sinsp_fdinfo_t* fdinfo, uint8_t* packed_data); - // Return false if the update didn't happen because the tuple is identical to the given address + bool set_ipv6_addresses_and_ports(sinsp_fdinfo_t* fdinfo, uint8_t* packed_data); bool set_unix_info(sinsp_fdinfo_t* fdinfo, uint8_t* packed_data); - void swap_ipv4_addresses(sinsp_fdinfo_t* fdinfo); + + void swap_addresses(sinsp_fdinfo_t* fdinfo); + uint8_t* reserve_event_buffer(); + void free_event_buffer(uint8_t*); // // Pointers to inspector context // sinsp* m_inspector; + // // Temporary storage to avoid memory allocation + // sinsp_evt m_tmp_evt; - // The transaction table. Key pair is . -// unordered_map, sinsp_transactinfo> m_transactable; + uint8_t m_fake_userevt_storage[4096]; + scap_evt* m_fake_userevt; + string m_tracer_error_string; + + bool m_track_connection_status = false; + // FD listener callback sinsp_fd_listener* m_fd_listener; + // + // The protocol decoders allocated by this parser + // + vector m_protodecoders; + + metaevents_state m_k8s_metaevents_state; + int m_k8s_capture_version = -1; + metaevents_state m_mesos_metaevents_state; + + stack m_tmp_events_buffer; friend class sinsp_analyzer; friend class sinsp_analyzer_fd_listener; + friend class sinsp_protodecoder; + friend class sinsp_baseliner; + friend class sinsp_container_manager; }; diff --git a/userspace/libsinsp/prefix_search.cpp b/userspace/libsinsp/prefix_search.cpp new file mode 100644 index 0000000000..c65f29de3d --- /dev/null +++ b/userspace/libsinsp/prefix_search.cpp @@ -0,0 +1,95 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 + +#include "prefix_search.h" + +using namespace std; + +path_prefix_search::path_prefix_search() +{ +} + +path_prefix_search::~path_prefix_search() +{ +} + +void path_prefix_search::add_search_path(const char *path) +{ + bool dummy = true; + return path_prefix_map::add_search_path(path, dummy); +} + +void path_prefix_search::add_search_path(const filter_value_t &path) +{ + bool dummy = true; + return path_prefix_map::add_search_path(path, dummy); +} + +void path_prefix_search::add_search_path(const std::string &str) +{ + bool dummy = true; + return path_prefix_map::add_search_path(str, dummy); +} + +bool path_prefix_search::match(const char *path) +{ + const bool *val = path_prefix_map::match(path); + return (val != NULL); +} + +bool path_prefix_search::match(const filter_value_t &path) +{ + const bool *val = path_prefix_map::match(path); + return (val != NULL); +} + +std::string path_prefix_search::as_string() +{ + return path_prefix_map::as_string(false); +} + +void path_prefix_map_ut::split_path(const filter_value_t &path, filter_components_t &components) +{ + components.clear(); + + uint8_t *pos = path.first; + + while (pos < path.first + path.second) + { + uint8_t *sep = (uint8_t *) memchr((char *) pos, '/', path.second - (pos - path.first)); + + if (sep) + { + if (sep-pos > 0) + { + components.emplace_back(pos, sep-pos); + } + pos = sep + 1; + } + else + { + components.emplace_back(pos, path.second - (pos - path.first)); + pos = path.first + path.second + 1; + } + } +} + + diff --git a/userspace/libsinsp/prefix_search.h b/userspace/libsinsp/prefix_search.h new file mode 100644 index 0000000000..236b1daaca --- /dev/null +++ b/userspace/libsinsp/prefix_search.h @@ -0,0 +1,344 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include + +#include +#include +#include +#include + +#include "filter_value.h" + +namespace path_prefix_map_ut +{ + typedef std::list filter_components_t; + + // Split path /var/log/messages into a list of components (var, log, messages). Empty components are skipped. + void split_path(const filter_value_t &path, filter_components_t &components); +}; + +// +// A data structure that allows testing a path P against a set of +// search paths S. The search succeeds if any of the search paths Si +// is a prefix of the path P. +// +// Here are some examples: +// - search(/var/run/docker, [/var/run, /etc, /lib, /usr/lib]) +// succeeds because /var/run is a prefix of /var/run/docker. +// - search(/boot, [/var/run, /etc, /lib, /usr/lib]) +// does not succeed because no path is a prefix of /boot. +// - search(/var/lib/messages, [/var/run, /etc, /lib, /usr/lib]) +// does not succeed because no path is a prefix of /var/lib/messages. +// /var is a partial match but not /var/run. +// - search(/var, [/var/run, /etc, /lib, /usr/lib]) +// does not succeed because no path is a prefix of /var +// /var is a partial match but the search path is /var/run, not /var. + +template +class path_prefix_map +{ +public: + path_prefix_map(); + virtual ~path_prefix_map(); + + void add_search_path(const char *path, Value &v); + void add_search_path(const filter_value_t &path, Value &v); + + // With the above methods the pointers in the char */uint8_t * + // above point to memory not owned by this object. The below + // method passes a string which is copied into the object, + // holding its own memory. + void add_search_path(const std::string &str, Value &v); + + // Similar to add_search_path, but takes a path already split + // into a list of components. This allows for custom splitting + // of paths other than on '/' boundaries. + void add_search_path_components(const path_prefix_map_ut::filter_components_t &components, Value &v); + + // If non-NULL, Value is not allocated. It points to memory + // held within this path_prefix_map() and is only valid as + // long as the map exists. + Value * match(const char *path); + Value * match(const filter_value_t &path); + + Value *match_components(const path_prefix_map_ut::filter_components_t &components); + + std::string as_string(bool include_vals); + +private: + + std::string as_string(const std::string &prefix, bool include_vals); + + void add_search_path_components(const path_prefix_map_ut::filter_components_t &components, path_prefix_map_ut::filter_components_t::const_iterator comp, Value &v); + + Value *match_components(const path_prefix_map_ut::filter_components_t &components, path_prefix_map_ut::filter_components_t::const_iterator comp); + + // Maps from the path component at the current level to a + // prefix search for the sub-path below the current level. + // For example, if the set of search paths is (/var/run, /etc, + // /lib, /usr, /usr/lib, /var/lib, /var/run), m_dirs contains: + // - (var, path_prefix_map(/run) + // - (etc, NULL) + // - (lib, NULL) + // - (usr, NULL) + // - (var, path_prefix_map(/lib, /run) + // Note that because usr is a prefix of /usr/lib, the /usr/lib + // path is dropped and only /usr is kept. Also note that + // terminator paths have a NULL path_prefix_map object. + std::unordered_map, + g_hash_membuf, + g_equal_to_membuf> m_dirs; + + std::list m_strvals; +}; + +template +path_prefix_map::path_prefix_map() +{ +} + +template +path_prefix_map::~path_prefix_map() +{ + for (auto &ent : m_dirs) + { + delete(ent.second.first); + delete(ent.second.second); + } +} + +// NOTE: this does not copy, so it is only valid as long as path is valid. +template +void path_prefix_map::add_search_path(const char *path, Value &v) +{ + filter_value_t mem((uint8_t *) path, (uint32_t) strlen(path)); + return add_search_path(mem, v); +} + +template +void path_prefix_map::add_search_path(const std::string &str, Value &v) +{ + m_strvals.push_back(str); + + return add_search_path(m_strvals.back().c_str(), v); +} + +template +void path_prefix_map::add_search_path(const filter_value_t &path, Value &v) +{ + path_prefix_map_ut::filter_components_t components; + + path_prefix_map_ut::split_path(path, components); + + // Add an initial "root" to the set of components. That + // ensures that a top-level path of '/' still results in a + // non-empty components list. For all other paths, there will + // be a dummy 'root' prefix at the top of every path. + components.emplace_front((uint8_t *) "root", 4); + + return add_search_path_components(components, v); +} + +template +void path_prefix_map::add_search_path_components(const path_prefix_map_ut::filter_components_t &components, Value &v) +{ + add_search_path_components(components, components.begin(), v); +} + +template +void path_prefix_map::add_search_path_components(const path_prefix_map_ut::filter_components_t &components, + path_prefix_map_ut::filter_components_t::const_iterator comp, + Value &v) +{ + path_prefix_map *subtree = NULL; + auto it = m_dirs.find(*comp); + auto cur = comp; + comp++; + + if(it == m_dirs.end()) + { + // This path component doesn't match any existing + // dirent. We need to add one and its subtree. + if(comp != components.end()) + { + subtree = new path_prefix_map(); + subtree->add_search_path_components(components, comp, v); + } + + // If the path doesn't have anything remaining, we + // also add the value here. + m_dirs[*cur] = std::pair(subtree, (comp == components.end() ? new Value(v) : NULL)); + } + else + { + // An entry for this dirent already exists. We will + // either add a new entry to the subtree, do nothing, + // or get rid of the existing subtree. + if(comp == components.end()) + { + // This path is a prefix of the current path and we + // can drop the existing subtree. For example, we can + // drop /usr/lib when adding /usr. + delete(it->second.first); + delete(it->second.second); + m_dirs.erase(*cur); + m_dirs[*cur] = std::pair(NULL, new Value(v)); + } + else if(it->second.first == NULL) + { + // The existing path is shorter than the + // current path, in which case we don't have + // to do anything. For example, no need to add + // /usr/lib when /usr exists. + } + else + { + // We need to add the remainder to the + // sub-tree's search path. + it->second.first->add_search_path_components(components, comp, v); + } + } +} + +// NOTE: this does not copy, so it is only valid as long as path is valid. +template +Value *path_prefix_map::match(const char *path) +{ + filter_value_t mem((uint8_t *) path, (uint32_t) strlen(path)); + return match(mem); +} + +template +Value *path_prefix_map::match(const filter_value_t &path) +{ + path_prefix_map_ut::filter_components_t components; + + path_prefix_map_ut::split_path(path, components); + + // Add an initial "root" to the set of components. That + // ensures that a top-level path of '/' still results in a + // non-empty components list. For all other paths, there will + // be a dummy 'root' prefix at the top of every path. + components.emplace_front((uint8_t *) "root", 4); + + return match_components(components); +} + +template +Value *path_prefix_map::match_components(const path_prefix_map_ut::filter_components_t &components) +{ + return match_components(components, components.begin()); +} + +template +Value *path_prefix_map::match_components(const path_prefix_map_ut::filter_components_t &components, path_prefix_map_ut::filter_components_t::const_iterator comp) +{ + auto it = m_dirs.find(*comp); + comp++; + + if(it == m_dirs.end()) + { + return NULL; + } + else + { + // If there is nothing left in the match path, the + // subtree must be null. This ensures that /var + // matches only /var and not /var/lib + if(comp == components.end()) + { + if(it->second.first == NULL) + { + return it->second.second; + } + else + { + return NULL; + } + } + else if(it->second.first == NULL) + { + // /foo/bar matched a prefix /foo, so we're + // done. + return it->second.second; + } + else + { + return it->second.first->match_components(components, comp); + } + } +} + +template +std::string path_prefix_map::as_string(bool include_vals) +{ + return as_string(std::string(""), include_vals); +} + +// Unlike all the other methods, this does perform copies. +template +std::string path_prefix_map::as_string(const std::string &prefix, bool include_vals) +{ + std::ostringstream os; + + for (auto &it : m_dirs) + { + std::string dirent((const char *) it.first.first, it.first.second); + + os << prefix << dirent << " -> "; + if (include_vals && it.second.first == NULL) + { + os << "v=" << (*it.second.second); + } + + os << std::endl; + + if(it.second.first) + { + std::string indent = prefix; + indent += " "; + os << it.second.first->as_string(indent, include_vals); + } + } + + return os.str(); +} + +class path_prefix_search : public path_prefix_map +{ +public: + path_prefix_search(); + ~path_prefix_search(); + + void add_search_path(const char *path); + void add_search_path(const filter_value_t &path); + void add_search_path(const std::string &str); + + // If non-NULL, Value is not allocated. It points to memory + // held within this path_prefix_map() and is only valid as + // long as the map exists. + bool match(const char *path); + bool match(const filter_value_t &path); + + std::string as_string(); +}; diff --git a/userspace/libsinsp/procinfo_test.cpp b/userspace/libsinsp/procinfo_test.cpp index c45a3ef1ba..366e42be53 100644 --- a/userspace/libsinsp/procinfo_test.cpp +++ b/userspace/libsinsp/procinfo_test.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include "sinsp.h" @@ -159,4 +160,4 @@ TEST(procinfo,get_root_process_child_clone) inspector.add_process(child); EXPECT_EQ(inspector.get_process(0), inspector.get_process(1)->get_root_process()); -} \ No newline at end of file +} diff --git a/userspace/libsinsp/protodecoder.cpp b/userspace/libsinsp/protodecoder.cpp new file mode 100644 index 0000000000..faf985ff56 --- /dev/null +++ b/userspace/libsinsp/protodecoder.cpp @@ -0,0 +1,331 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#ifndef _WIN32 +#include +#endif +#include "sinsp.h" +#include "sinsp_int.h" +#include "protodecoder.h" + +extern sinsp_protodecoder_list g_decoderlist; + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_protodecoder implementation +/////////////////////////////////////////////////////////////////////////////// +sinsp_protodecoder::sinsp_protodecoder() +{ +} + +void sinsp_protodecoder::set_inspector(sinsp* inspector) +{ + m_inspector = inspector; +} + +void sinsp_protodecoder::on_read(sinsp_evt* evt, char *data, uint32_t len) +{ + ASSERT(false); +} + +void sinsp_protodecoder::on_write(sinsp_evt* evt, char *data, uint32_t len) +{ + ASSERT(false); +} + +void sinsp_protodecoder::on_reset(sinsp_evt* evt) +{ + ASSERT(false); +} + +void sinsp_protodecoder::register_event_callback(sinsp_pd_callback_type etype) +{ + ASSERT(m_inspector != NULL); + + m_inspector->m_parser->register_event_callback(etype, this); +} + +void sinsp_protodecoder::register_read_callback(sinsp_fdinfo_t* fdinfo) +{ + ASSERT(m_inspector != NULL); + + fdinfo->register_event_callback(CT_READ, this); +} + +void sinsp_protodecoder::register_write_callback(sinsp_fdinfo_t* fdinfo) +{ + ASSERT(m_inspector != NULL); + + fdinfo->register_event_callback(CT_WRITE, this); +} + +void sinsp_protodecoder::unregister_read_callback(sinsp_fdinfo_t* fdinfo) +{ + ASSERT(m_inspector != NULL); + + fdinfo->unregister_event_callback(CT_READ, this); +} + +void sinsp_protodecoder::unregister_write_callback(sinsp_fdinfo_t* fdinfo) +{ + ASSERT(m_inspector != NULL); + + fdinfo->unregister_event_callback(CT_WRITE, this); +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_protodecoder_list implementation +/////////////////////////////////////////////////////////////////////////////// +sinsp_protodecoder_list::sinsp_protodecoder_list() +{ + ////////////////////////////////////////////////////////////////////////////// + // ADD NEW DECODER CLASSES HERE + ////////////////////////////////////////////////////////////////////////////// + add_protodecoder(new sinsp_decoder_syslog()); +} + +sinsp_protodecoder_list::~sinsp_protodecoder_list() +{ + uint32_t j; + + for(j = 0; j < m_decoders_list.size(); j++) + { + delete m_decoders_list[j]; + } +} + +void sinsp_protodecoder_list::add_protodecoder(sinsp_protodecoder* protodecoder) +{ + m_decoders_list.push_back(protodecoder); +} + +sinsp_protodecoder* sinsp_protodecoder_list::new_protodecoder_from_name(const string& name, + sinsp* inspector) +{ + uint32_t j; + + for(j = 0; j < m_decoders_list.size(); j++) + { + m_decoders_list[j]->m_inspector = inspector; + + if(m_decoders_list[j]->m_name == name) + { + sinsp_protodecoder* newchk = m_decoders_list[j]->allocate_new(); + newchk->set_inspector(inspector); + return newchk; + } + } + + throw sinsp_exception("unknown protocol decoder " + name); +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_decoder_syslog implementation +/////////////////////////////////////////////////////////////////////////////// +const char* syslog_severity_strings[] = +{ + "emerg", "alert", "crit", "err", "warn", "notice", "info", "debug" +}; + +const char* syslog_facility_strings[] = +{ + "kern", + "user", + "mail", + "daemon", + "auth", + "syslog", + "lpr", + "news", + "uucp", + "clock", + "authpriv", + "ftp", + "ntp", + "logaudit", + "logalert", + "cron", + "local0", + "local1", + "local2", + "local3", + "local4", + "local5", + "local6", + "local7" +}; + +sinsp_decoder_syslog::sinsp_decoder_syslog() +{ + m_name = "syslog"; + m_priority = -1; +} + +sinsp_protodecoder* sinsp_decoder_syslog::allocate_new() +{ + return (sinsp_protodecoder*) new sinsp_decoder_syslog(); +} + +void sinsp_decoder_syslog::init() +{ + register_event_callback(CT_OPEN); + register_event_callback(CT_CONNECT); +} + +void sinsp_decoder_syslog::on_fd_from_proc(sinsp_fdinfo_t* fdinfo) +{ + if(fdinfo == NULL) + { + ASSERT(false); + return ; + } + + if(fdinfo->m_name.find("/dev/log") != string::npos) + { + register_write_callback(fdinfo); + } +} + +void sinsp_decoder_syslog::on_event(sinsp_evt* evt, sinsp_pd_callback_type etype) +{ + if(etype == CT_OPEN || + etype == CT_CONNECT) + { + sinsp_fdinfo_t* fdinfo = evt->get_fd_info(); + if(fdinfo == NULL) + { + return ; + } + + if(fdinfo->m_name.find("/dev/log") != string::npos) + { + register_write_callback(fdinfo); + } + } + else if(etype == CT_TUPLE_CHANGE) + { + sinsp_fdinfo_t* fdinfo = evt->get_fd_info(); + if(fdinfo == NULL) + { + return ; + } + + if(fdinfo->m_name.find("/dev/log") != string::npos) + { + register_write_callback(fdinfo); + } + else + { + if(fdinfo->has_decoder_callbacks()) + { + unregister_write_callback(fdinfo); + } + } + } + else + { + ASSERT(false); + } +} + +#define PRI_BUF_SIZE 16 + +void sinsp_decoder_syslog::on_write(sinsp_evt* evt, char *data, uint32_t len) +{ + char pri[PRI_BUF_SIZE]; + char* tc = data + 1; + char* te = data + len; + uint32_t j = 0; + + while(tc < te && *tc != '>' && *tc != '\0' && j < PRI_BUF_SIZE - 1) + { + pri[j++] = *tc; + tc++; + } + + pri[j] = 0; + + decode_message(data, len, pri, j); +} + +void sinsp_decoder_syslog::on_reset(sinsp_evt* evt) +{ + m_priority = -1; +} + +bool sinsp_decoder_syslog::is_data_valid() +{ + return (m_priority != -1); +} + +const char* sinsp_decoder_syslog::get_severity_str() +{ + if(m_severity >= sizeof(syslog_severity_strings) / sizeof(syslog_severity_strings[0])) + { + return ""; + } + else + { + return syslog_severity_strings[m_severity]; + } +} + +const char* sinsp_decoder_syslog::get_facility_str() +{ + if(m_facility >= sizeof(syslog_facility_strings) / sizeof(syslog_facility_strings[0])) + { + return ""; + } + else + { + return syslog_facility_strings[m_facility]; + } +} + +void sinsp_decoder_syslog::decode_message(char *data, uint32_t len, char* pristr, uint32_t pristrlen) +{ + if(len < pristrlen + 2 || pristrlen == 0) + { + m_priority = -1; + return; + } + + bool res = sinsp_numparser::tryparsed32_fast(pristr, pristrlen, &m_priority); + + if(!res) + { + m_priority = -1; + return; + } + + m_severity = m_priority & 0x07; + m_facility = m_priority >> 3; + + m_msg.assign(data + pristrlen + 2, len - pristrlen - 2); + + m_inspector->protodecoder_register_reset(this); +} + +bool sinsp_decoder_syslog::get_info_line(char** res) +{ + m_infostr = string("syslog sev=") + get_severity_str() + " msg=" + m_msg; + + *res = (char*)m_infostr.c_str(); + return (m_priority != -1); +} diff --git a/userspace/libsinsp/protodecoder.h b/userspace/libsinsp/protodecoder.h new file mode 100644 index 0000000000..559fb3f031 --- /dev/null +++ b/userspace/libsinsp/protodecoder.h @@ -0,0 +1,144 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +/////////////////////////////////////////////////////////////////////////////// +// The protocol decoder interface +/////////////////////////////////////////////////////////////////////////////// +class sinsp_protodecoder +{ +public: + sinsp_protodecoder(); + + virtual ~sinsp_protodecoder() + { + } + + // + // Allocate a new decoder of the same type. + // Every protodecoder plugin must implement this. + // + virtual sinsp_protodecoder* allocate_new() = 0; + + // + // Allocate a new decoder of the same type. + // Every protodecoder plugin must implement this. + // + virtual void init() = 0; + + // + // Return the protocol decoder name + // + const string& get_name() + { + return m_name; + } + + // + // Called by the engine for each of the FDs that are added from proc + // (or from the file) at the beginning of a capture. + // + virtual void on_fd_from_proc(sinsp_fdinfo_t* fdinfo) = 0; + + // + // Called by the engine after an event has been received and parsed. + // + virtual void on_event(sinsp_evt* evt, sinsp_pd_callback_type etype) = 0; + + // + // These are not part of on_event for performance reasons + // + virtual void on_read(sinsp_evt* evt, char *data, uint32_t len); + virtual void on_write(sinsp_evt* evt, char *data, uint32_t len); + virtual void on_reset(sinsp_evt* evt); + + // + // Used by the engine to retrieve the info line for the last event. + // Must return true if the line is valid. + // + virtual bool get_info_line(char** res) = 0; + +protected: + // + // Interface for the plugins + // + void register_event_callback(sinsp_pd_callback_type etype); + void register_read_callback(sinsp_fdinfo_t* fdinfo); + void register_write_callback(sinsp_fdinfo_t* fdinfo); + + void unregister_read_callback(sinsp_fdinfo_t* fdinfo); + void unregister_write_callback(sinsp_fdinfo_t* fdinfo); + + string m_name; + sinsp* m_inspector; + +private: + void set_inspector(sinsp* inspector); + +friend class sinsp_protodecoder_list; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Global class that stores the list of protocol decoders and offers +// functions to work with it. +/////////////////////////////////////////////////////////////////////////////// +class sinsp_protodecoder_list +{ +public: + sinsp_protodecoder_list(); + ~sinsp_protodecoder_list(); + void add_protodecoder(sinsp_protodecoder* protodecoder); + sinsp_protodecoder* new_protodecoder_from_name(const string& name, sinsp* inspector); + +private: + vector m_decoders_list; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Decoder classes +// NOTE: these should be moved to a separate file but, since we have only one +// for the moment, we keep it here +/////////////////////////////////////////////////////////////////////////////// +class sinsp_decoder_syslog : public sinsp_protodecoder +{ +public: + sinsp_decoder_syslog(); + sinsp_protodecoder* allocate_new(); + void init(); + void on_fd_from_proc(sinsp_fdinfo_t* fdinfo); + void on_event(sinsp_evt* evt, sinsp_pd_callback_type etype); + void on_write(sinsp_evt* evt, char *data, uint32_t len); + void on_reset(sinsp_evt* evt); + bool get_info_line(char** res); + + bool is_data_valid(); + + const char* get_severity_str(); + const char* get_facility_str(); + + int32_t m_priority; + uint32_t m_facility; + uint32_t m_severity; + string m_msg; + +private: + void decode_message(char *data, uint32_t len, char* pristr, uint32_t pristrlen); + string m_infostr; +}; diff --git a/userspace/libsinsp/runc.cpp b/userspace/libsinsp/runc.cpp new file mode 100644 index 0000000000..53480375f0 --- /dev/null +++ b/userspace/libsinsp/runc.cpp @@ -0,0 +1,99 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "runc.h" + +#include + +#include "sinsp.h" +#include "sinsp_int.h" + +namespace { + +const size_t CONTAINER_ID_LENGTH = 64; +const size_t REPORTED_CONTAINER_ID_LENGTH = 12; +const char* CONTAINER_ID_VALID_CHARACTERS = "0123456789abcdefABCDEF"; + +static_assert(REPORTED_CONTAINER_ID_LENGTH <= CONTAINER_ID_LENGTH, "Reported container ID length cannot be longer than actual length"); + +// check if cgroup ends with +// If true, set to a truncated version of the id and return true. +// Otherwise return false and leave container_id unchanged +bool match_one_container_id(const std::string &cgroup, const std::string &prefix, const std::string &suffix, std::string &container_id) +{ + size_t start_pos = cgroup.rfind(prefix); + if (start_pos == std::string::npos) + { + return false; + } + start_pos += prefix.size(); + + size_t end_pos = cgroup.rfind(suffix); + if (end_pos == std::string::npos) + { + return false; + } + + if (end_pos - start_pos != CONTAINER_ID_LENGTH) + { + return false; + } + + size_t invalid_ch_pos = cgroup.find_first_not_of(CONTAINER_ID_VALID_CHARACTERS, start_pos); + if (invalid_ch_pos < CONTAINER_ID_LENGTH) + { + return false; + } + + container_id = cgroup.substr(start_pos, REPORTED_CONTAINER_ID_LENGTH); + return true; +} + +bool match_container_id(const std::string &cgroup, const libsinsp::runc::cgroup_layout *layout, + std::string &container_id) +{ + for(size_t i = 0; layout[i].prefix && layout[i].suffix; ++i) + { + if(match_one_container_id(cgroup, layout[i].prefix, layout[i].suffix, container_id)) + { + return true; + } + } + + return false; +} +} + +namespace libsinsp { +namespace runc { + +bool matches_runc_cgroups(const sinsp_threadinfo *tinfo, const cgroup_layout *layout, std::string &container_id) +{ + for(const auto &it : tinfo->m_cgroups) + { + if(match_container_id(it.second, layout, container_id)) + { + return true; + } + } + + return false; +} +} +} \ No newline at end of file diff --git a/userspace/libsinsp/runc.h b/userspace/libsinsp/runc.h new file mode 100644 index 0000000000..2f85837d8d --- /dev/null +++ b/userspace/libsinsp/runc.h @@ -0,0 +1,47 @@ +/* +Copyright (C) 2013-2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include + +class sinsp_threadinfo; + +namespace libsinsp { +namespace runc { + +/// runc-based runtimes (Docker, containerd, CRI-O, probably others) use the same two cgroup layouts +/// with slight variations: +/// - non-systemd layout uses cgroups ending with .../ +/// - systemd layout uses .../.scope +/// where is always 64 hex digits (we report the first 12 as the container id). +/// For non-systemd only CRI-O seems to use /crio-, while for systemd layout +/// while all known container engines use a prefix like "docker-", "crio-" or "containerd-cri-". +/// We can encode all these variants with a simple list of (prefix, suffix) pairs +/// (the last one must be a pair of null pointers to mark the end of the array) +struct cgroup_layout { + const char* prefix; + const char* suffix; +}; + +/// If any of the cgroups of the thread in `tinfo` matches the `layout`, set `container_id` to the found id +/// and return true. Otherwise, return false and leave `container_id` unchanged +bool matches_runc_cgroups(const sinsp_threadinfo *tinfo, const cgroup_layout *layout, std::string &container_id); +} +} diff --git a/userspace/libsinsp/scap_open_exception.h b/userspace/libsinsp/scap_open_exception.h new file mode 100644 index 0000000000..f38a82807c --- /dev/null +++ b/userspace/libsinsp/scap_open_exception.h @@ -0,0 +1,47 @@ +/* +Copyright (C) 2013-2019 Sysdig Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include "sinsp_exception.h" + +/*! + \brief Instances of this exception are thrown when calls to scap_open() + fail. The given scap_rc is the error value returned from scap_open(). +*/ +class scap_open_exception : public sinsp_exception +{ +public: + scap_open_exception(const std::string& error_str, const int32_t scap_rc): + sinsp_exception(error_str), + m_scap_rc(scap_rc) + { } + + scap_open_exception(const char* const error_str, const int32_t scap_rc): + sinsp_exception(error_str), + m_scap_rc(scap_rc) + { } + + int32_t scap_rc() const + { + return m_scap_rc; + } + +private: + int32_t m_scap_rc; +}; diff --git a/userspace/libsinsp/settings.h b/userspace/libsinsp/settings.h index bd9a29642e..70be108581 100644 --- a/userspace/libsinsp/settings.h +++ b/userspace/libsinsp/settings.h @@ -1,21 +1,23 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ +#pragma once // // This flag can be used to include unsupported or unrecognized sockets // in the fd tables. It's useful to debug close() leaks @@ -24,7 +26,7 @@ along with sysdig. If not, see . // // Memory storage size for an entry in the event storage LIFO. -// Events bigger than SP_STORAGE_EVT_BUF_SIZE won't be be stored in the LIFO. +// Events bigger than SP_EVT_BUF_SIZE won't be be stored in the LIFO. // #define SP_EVT_BUF_SIZE 4096 @@ -53,7 +55,13 @@ along with sysdig. If not, see . // // Max size that the thread table can reach // -#define MAX_THREAD_TABLE_SIZE 65536 +#define MAX_THREAD_TABLE_SIZE 131072 +#define DEFAULT_THREAD_TABLE_SIZE 65536 + +// +// Max size that the FD table of a process can reach +// +#define MAX_FD_TABLE_SIZE 4096 // // The time after an inactive thread is removed. @@ -61,12 +69,17 @@ along with sysdig. If not, see . #define DEFAULT_THREAD_TIMEOUT_S 1800 // -// How often the thread table is sacnned for inactive threads +// How often the thread table is scanned for inactive threads +// +#define DEFAULT_INACTIVE_THREAD_SCAN_TIME_S 1200 + +// +// How often the container table is scanned for inactive containers // -#define DEFAULT_INACTIVE_THREAD_SCAN_TIME_S 600 +#define DEFAULT_INACTIVE_CONTAINER_SCAN_TIME_S 30 // -// Enables LUA chisel scripts support +// Enables Lua chisel scripts support // #define HAS_CHISELS @@ -80,6 +93,40 @@ along with sysdig. If not, see . // #define DEFAULT_SNAPLEN 80 +// +// Maximum user event buffer size +// +#define MAX_USER_EVT_BUFFER 65536 + +// +// Size the user event buffer is brought back once in a while +// +#define MIN_USER_EVT_BUFFER 256 + +// +// Is csysdig functionality included? +// +#define CSYSDIG + +#ifdef _WIN32 +#define NOCURSESUI +#endif + +// +// Name of the device used for tracer injection +// +#define USER_EVT_DEVICE_NAME "/dev/null" + +// +// The time after which a clone should be considered stale +// +#define CLONE_STALE_TIME_NS 2000000000 + +// +// Port range to enable larger snaplen on +// +#define DEFAULT_INCREASE_SNAPLEN_PORT_RANGE {0, 0} + // // FD class customized with the storage we need // @@ -89,3 +136,9 @@ along with sysdig. If not, see . template class sinsp_fdinfo; typedef sinsp_fdinfo sinsp_fdinfo_t; #endif // HAS_ANALYZER + +// Max JSON we can parse from docker API or others +// Added because older docker versions have a bug that causes +// very big JSONs returned by container inspect call +static const unsigned MAX_JSON_SIZE_B = 500 * 1024; // 500 kiB + diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index 6daaeee1fc..c9503640f4 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -1,21 +1,21 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + http://www.apache.org/licenses/LICENSE-2.0 -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . -*/ +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 #include @@ -25,54 +25,154 @@ along with sysdig. If not, see . #include #include #include +#include #endif // _WIN32 +#include "scap_open_exception.h" #include "sinsp.h" #include "sinsp_int.h" +#include "sinsp_auth.h" #include "filter.h" #include "filterchecks.h" +#include "chisel.h" +#include "cyclewriter.h" +#include "protodecoder.h" +#include "dns_manager.h" + +#ifndef CYGWING_AGENT +#ifndef MINIMAL_BUILD +#include "k8s_api_handler.h" +#endif // MINIMAL_BUILD +#ifdef HAS_CAPTURE +#ifndef MINIMAL_BUILD +#include +#endif // MINIMAL_BUILD +#include +#endif +#endif + #ifdef HAS_ANALYZER #include "analyzer_int.h" #include "analyzer.h" +#include "tracer_emitter.h" #endif -//#include "drfilterParser.h" -extern sinsp_evttables g_infotables; +#ifdef HAS_CHISELS extern vector* g_chisel_dirs; +#endif + +void on_new_entry_from_proc(void* context, scap_t* handle, int64_t tid, scap_threadinfo* tinfo, + scap_fdinfo* fdinfo); /////////////////////////////////////////////////////////////////////////////// // sinsp implementation /////////////////////////////////////////////////////////////////////////////// sinsp::sinsp() : - m_evt(this) + m_external_event_processor(), + m_evt(this), + m_lastevent_ts(0), + m_container_manager(this), + m_suppressed_comms() { +#if !defined(MINIMAL_BUILD) && !defined(CYGWING_AGENT) && defined(HAS_CAPTURE) + // used by mesos and container_manager + curl_global_init(CURL_GLOBAL_DEFAULT); +#endif m_h = NULL; m_parser = NULL; m_dumper = NULL; + m_is_dumping = false; + m_metaevt = NULL; + m_meinfo.m_piscapevt = NULL; m_network_interfaces = NULL; m_parser = new sinsp_parser(this); m_thread_manager = new sinsp_thread_manager(this); - m_max_thread_table_size = MAX_THREAD_TABLE_SIZE; + m_max_thread_table_size = DEFAULT_THREAD_TABLE_SIZE; + m_max_fdtable_size = MAX_FD_TABLE_SIZE; m_thread_timeout_ns = DEFAULT_THREAD_TIMEOUT_S * ONE_SECOND_IN_NS; m_inactive_thread_scan_time_ns = DEFAULT_INACTIVE_THREAD_SCAN_TIME_S * ONE_SECOND_IN_NS; - -#ifdef HAS_ANALYZER - m_analyzer = NULL; -#endif + m_inactive_container_scan_time_ns = DEFAULT_INACTIVE_CONTAINER_SCAN_TIME_S * ONE_SECOND_IN_NS; + m_cycle_writer = NULL; + m_write_cycling = false; #ifdef HAS_FILTERING m_filter = NULL; - m_firstevent_ts = 0; + m_evttype_filter = NULL; #endif m_fds_to_remove = new vector; m_machine_info = NULL; +#ifdef SIMULATE_DROP_MODE m_isdropping = false; +#endif m_n_proc_lookups = 0; - m_max_n_proc_lookups = 0; - m_max_n_proc_socket_lookups = 0; + m_n_proc_lookups_duration_ns = 0; + m_n_main_thread_lookups = 0; m_snaplen = DEFAULT_SNAPLEN; m_buffer_format = sinsp_evt::PF_NORMAL; + m_input_fd = 0; + m_bpf = false; + m_udig = false; + m_isdebug_enabled = false; + m_isfatfile_enabled = false; + m_isinternal_events_enabled = false; + m_hostname_and_port_resolution_enabled = false; + m_output_time_flag = 'h'; + m_max_evt_output_len = 0; + m_filesize = -1; + m_track_tracers_state = false; + m_import_users = true; + m_next_flush_time_ns = 0; + m_last_procrequest_tod = 0; + m_get_procs_cpu_from_driver = false; + m_is_tracers_capture_enabled = false; + m_file_start_offset = 0; + m_flush_memory_dump = false; + m_next_stats_print_time_ns = 0; + m_large_envs_enabled = false; + m_increased_snaplen_port_range = DEFAULT_INCREASE_SNAPLEN_PORT_RANGE; + m_statsd_port = -1; + + // Unless the cmd line arg "-pc" or "-pcontainer" is supplied this is false + m_print_container_data = false; + +#if defined(HAS_CAPTURE) + m_sysdig_pid = getpid(); +#endif + + uint32_t evlen = sizeof(scap_evt) + 2 * sizeof(uint16_t) + 2 * sizeof(uint64_t); + m_meinfo.m_piscapevt = (scap_evt*)new char[evlen]; + m_meinfo.m_piscapevt->type = PPME_PROCINFO_E; + m_meinfo.m_piscapevt->len = evlen; + m_meinfo.m_piscapevt->nparams = 2; + uint16_t* lens = (uint16_t*)((char *)m_meinfo.m_piscapevt + sizeof(struct ppm_evt_hdr)); + lens[0] = 8; + lens[1] = 8; + m_meinfo.m_piscapevt_vals = (uint64_t*)(lens + 2); + + m_meinfo.m_pievt.m_inspector = this; + m_meinfo.m_pievt.m_info = &(g_infotables.m_event_info[PPME_SYSDIGEVENT_X]); + m_meinfo.m_pievt.m_pevt = NULL; + m_meinfo.m_pievt.m_cpuid = 0; + m_meinfo.m_pievt.m_evtnum = 0; + m_meinfo.m_pievt.m_pevt = m_meinfo.m_piscapevt; + m_meinfo.m_pievt.m_fdinfo = NULL; + m_meinfo.m_n_procinfo_evts = 0; + m_meta_event_callback = NULL; + m_meta_event_callback_data = NULL; +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + m_k8s_client = NULL; + m_k8s_last_watch_time_ns = 0; + + m_k8s_client = NULL; + m_k8s_api_server = NULL; + m_k8s_api_cert = NULL; + + m_mesos_client = NULL; + m_mesos_last_watch_time_ns = 0; +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + + m_filter_proc_table_when_saving = false; } sinsp::~sinsp() @@ -95,172 +195,77 @@ sinsp::~sinsp() delete m_thread_manager; m_thread_manager = NULL; } -} - -void sinsp::open(uint32_t timeout_ms) -{ - char error[SCAP_LASTERR_SIZE]; - - g_logger.log("starting live capture"); - - m_islive = true; - m_h = scap_open_live(error); - - if(m_h == NULL) - { - throw sinsp_exception(error); - } - - init(); -} - -void sinsp::open(string filename) -{ - char error[SCAP_LASTERR_SIZE]; - - m_islive = false; - - if(filename == "") - { - open(); - return; - } - g_logger.log("starting offline capture"); - - m_h = scap_open_offline((char *)filename.c_str(), error); - - if(m_h == NULL) + if(m_cycle_writer) { - throw sinsp_exception(error); + delete m_cycle_writer; + m_cycle_writer = NULL; } - m_filename = filename; - - init(); -} - -void sinsp::close() -{ - if(m_h) + if(m_meinfo.m_piscapevt) { - scap_close(m_h); - m_h = NULL; + delete[] m_meinfo.m_piscapevt; } - if(NULL != m_dumper) - { - scap_dump_close(m_dumper); - m_dumper = NULL; - } + m_container_manager.cleanup(); - if(NULL != m_network_interfaces) - { - delete m_network_interfaces; - m_network_interfaces = NULL; - } +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + delete m_k8s_client; + delete m_k8s_api_server; + delete m_k8s_api_cert; -#ifdef HAS_FILTERING - if(m_filter != NULL) - { - delete m_filter; - } + delete m_mesos_client; +#ifdef HAS_CAPTURE + curl_global_cleanup(); + sinsp_dns_manager::get().cleanup(); +#endif #endif } -void sinsp::autodump_start(const string dump_filename) +void sinsp::add_protodecoders() { - if(NULL == m_h) - { - throw sinsp_exception("inspector not opened yet"); - } - - m_dumper = scap_dump_open(m_h, dump_filename.c_str()); - if(NULL == m_dumper) - { - throw sinsp_exception(scap_getlasterr(m_h)); - } + m_parser->add_protodecoder("syslog"); } -void sinsp::autodump_stop() +void sinsp::filter_proc_table_when_saving(bool filter) { - if(NULL == m_h) - { - throw sinsp_exception("inspector not opened yet"); - } + m_filter_proc_table_when_saving = filter; - if(m_dumper != NULL) + if(m_h != NULL) { - scap_dump_close(m_dumper); - m_dumper = NULL; + scap_set_refresh_proc_table_when_saving(m_h, !filter); } } -void sinsp::import_thread_table() +void sinsp::enable_tracers_capture() { - scap_threadinfo *pi; - scap_threadinfo *tpi; - sinsp_threadinfo newti(this); - - scap_threadinfo *table = scap_get_proc_table(m_h); - - // - // Scan the scap table and add the threads to our list - // - HASH_ITER(hh, table, pi, tpi) +#if defined(HAS_CAPTURE) && ! defined(CYGWING_AGENT) + if(!m_is_tracers_capture_enabled) { - newti.init(pi); - m_thread_manager->add_thread(newti, true); - } + if(is_live() && m_h != NULL) + { + if(scap_enable_tracers_capture(m_h) != SCAP_SUCCESS) + { + throw sinsp_exception("error enabling tracers capture"); + } + } - // - // Scan the list to create the proper parent/child dependencies - // - threadinfo_map_iterator_t it; - for(it = m_thread_manager->m_threadtable.begin(); - it != m_thread_manager->m_threadtable.end(); ++it) - { - m_thread_manager->increment_mainthread_childcount(&it->second); - m_thread_manager->increment_program_childcount(&it->second); + m_is_tracers_capture_enabled = true; } - - // - // Scan the list to fix the direction of the sockets - // - m_thread_manager->fix_sockets_coming_from_proc(); -} - -void sinsp::import_ifaddr_list() -{ - m_network_interfaces = new sinsp_network_interfaces; - m_network_interfaces->import_interfaces(scap_get_ifaddr_list(m_h)); -} - -sinsp_network_interfaces* sinsp::get_ifaddr_list() -{ - return m_network_interfaces; +#endif } -void sinsp::import_user_list() +void sinsp::enable_page_faults() { - uint32_t j; - scap_userlist* ul = scap_get_user_list(m_h); - - for(j = 0; j < ul->nusers; j++) - { - m_userlist[ul->users[j].uid] = &(ul->users[j]); - } - - for(j = 0; j < ul->ngroups; j++) +#if defined(HAS_CAPTURE) && ! defined(CYGWING_AGENT) + if(is_live() && m_h != NULL) { - m_grouplist[ul->groups[j].gid] = &(ul->groups[j]); + if(scap_enable_page_faults(m_h) != SCAP_SUCCESS) + { + throw sinsp_exception("error enabling page_faults"); + } } -} - -void sinsp::import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo) -{ - ASSERT(m_network_interfaces); - m_network_interfaces->import_ipv4_interface(ifinfo); +#endif } void sinsp::init() @@ -280,9 +285,21 @@ void sinsp::init() } // - // Reset the thread manager + // Attach the protocol decoders // - m_thread_manager->clear(); +#ifndef HAS_ANALYZER + add_protodecoders(); +#endif + // + // Allocate the cycle writer + // + if(m_cycle_writer) + { + delete m_cycle_writer; + m_cycle_writer = NULL; + } + + m_cycle_writer = new cycle_writer(is_live()); // // Basic inits @@ -291,486 +308,2300 @@ void sinsp::init() m_stats.clear(); #endif + m_nevts = 0; m_tid_to_remove = -1; m_lastevent_ts = 0; - - import_ifaddr_list(); - import_thread_table(); - import_user_list(); - -#ifdef HAS_ANALYZER - // - // Notify the analyzer that we're starting - // - if(m_analyzer) - { - m_analyzer->on_capture_start(); - } +#ifdef HAS_FILTERING + m_firstevent_ts = 0; #endif + m_fds_to_remove->clear(); + m_n_proc_lookups = 0; + m_n_proc_lookups_duration_ns = 0; + m_n_main_thread_lookups = 0; // - // If m_snaplen was modified, we set snaplen now + // Return the tracers to the pool and clear the tracers list // - if (m_snaplen != DEFAULT_SNAPLEN) + for(auto it = m_partial_tracers_list.begin(); it != m_partial_tracers_list.end(); ++it) { - set_snaplen(m_snaplen); + m_partial_tracers_pool->push(*it); } -} + m_partial_tracers_list.clear(); -bool should_drop(sinsp_evt *evt, bool* stopped, bool* switched); - -int32_t sinsp::next(OUT sinsp_evt **evt) -{ // - // Get the event from libscap + // If we're reading from file, we try to pre-parse the container events before + // importing the thread table, so that thread table filtering will work with + // container filters // - int32_t res = scap_next(m_h, &(m_evt.m_pevt), &(m_evt.m_cpuid)); - if(res != SCAP_SUCCESS) + if(is_capture()) { - if(res == SCAP_TIMEOUT) - { - *evt = NULL; - return res; - } - else if(res == SCAP_EOF) + uint64_t off = scap_ftell(m_h); + scap_evt* pevent; + uint16_t pcpuid; + uint32_t ncnt = 0; + + // + // Count how many container events we have + // + while(true) { -#ifdef HAS_ANALYZER - if(m_analyzer) + int32_t res = scap_next(m_h, &pevent, &pcpuid); + + if(res == SCAP_SUCCESS) { - m_analyzer->process_event(NULL, sinsp_analyzer::DF_NONE); + if((pevent->type != PPME_CONTAINER_E) && (pevent->type != PPME_CONTAINER_JSON_E)) + { + break; + } + else + { + ncnt++; + continue; + } + } + else + { + break; } -#endif } - else + + if (m_external_event_processor) { - throw sinsp_exception(scap_getlasterr(m_h)); + m_external_event_processor->on_capture_start(); + } + + // + // Rewind, reset the event count, and consume the exact number of events + // + scap_fseek(m_h, off); + scap_event_reset_count(m_h); + for(uint32_t j = 0; j < ncnt; j++) + { + sinsp_evt* tevt; + next(&tevt); } + } - return res; + if(is_capture() || m_filter_proc_table_when_saving == true) + { + import_thread_table(); } + import_ifaddr_list(); + + import_user_list(); + // - // Store a couple of values that we'll need later inside the event. + // Scan the list to create the proper parent/child dependencies // - m_evt.m_evtnum = get_num_events(); - m_lastevent_ts = m_evt.get_ts(); -#ifdef HAS_FILTERING - if(m_firstevent_ts == 0) + m_thread_manager->create_child_dependencies(); + + // + // Scan the list to fix the direction of the sockets + // + m_thread_manager->fix_sockets_coming_from_proc(); + + if (m_external_event_processor) { - m_firstevent_ts = m_lastevent_ts; + m_external_event_processor->on_capture_start(); } -#endif - -#ifndef HAS_ANALYZER // - // Deleayed removal of threads from the thread table, so that - // things like exit() or close() can be parsed. - // We only do this if the analyzer is not enabled, because the analyzer - // needs the process at the end of the sample and will take care of deleting - // it. + // If m_snaplen was modified, we set snaplen now // - if(m_tid_to_remove != -1) + if(m_snaplen != DEFAULT_SNAPLEN) { - remove_thread(m_tid_to_remove); - m_tid_to_remove = -1; + set_snaplen(m_snaplen); } // - // Run the periodic connection and thread table cleanup + // If the port range for increased snaplen was modified, set it now // - m_thread_manager->remove_inactive_threads(); -#endif // HAS_ANALYZER + if(increased_snaplen_port_range_set()) + { + set_fullcapture_port_range(m_increased_snaplen_port_range.range_start, + m_increased_snaplen_port_range.range_end); + } // - // Deleayed removal of the fd, so that - // things like exit() or close() can be parsed. + // If the statsd port was modified, push it to the kernel now. // - uint32_t nfdr = m_fds_to_remove->size(); + if(m_statsd_port != -1) + { + set_statsd_port(m_statsd_port); + } - if(nfdr != 0) +#if defined(HAS_CAPTURE) + if(m_mode == SCAP_MODE_LIVE) { - sinsp_threadinfo* ptinfo = get_thread(m_tid_of_fd_to_remove, true); - if(!ptinfo) + if(scap_getpid_global(m_h, &m_sysdig_pid) != SCAP_SUCCESS) { ASSERT(false); - return res; - } - - for(uint32_t j = 0; j < nfdr; j++) - { - ptinfo->remove_fd(m_fds_to_remove->at(j)); } - - m_fds_to_remove->clear(); } +#endif +} -#ifdef SIMULATE_DROP_MODE - bool sd = false; - bool sw = false; +void sinsp::set_import_users(bool import_users) +{ + m_import_users = import_users; +} - if(m_analyzer) - { - m_analyzer->m_configuration->set_analyzer_sample_len_ns(500000000); - } +void sinsp::open_live_common(uint32_t timeout_ms, scap_mode_t mode) +{ + char error[SCAP_LASTERR_SIZE]; - sd = should_drop(&m_evt, &m_isdropping, &sw); -#endif + g_logger.log("starting live capture"); // - // Run the state engine + // Reset the thread manager // -#ifdef SIMULATE_DROP_MODE - if(!sd || m_isdropping) + m_thread_manager->clear(); + + // + // Start the capture + // + m_mode = mode; + scap_open_args oargs; + oargs.mode = mode; + oargs.fname = NULL; + oargs.proc_callback = NULL; + oargs.proc_callback_context = NULL; + oargs.udig = m_udig; + + if(!m_filter_proc_table_when_saving) { - m_parser->process_event(&m_evt); + oargs.proc_callback = ::on_new_entry_from_proc; + oargs.proc_callback_context = this; } + oargs.import_users = m_import_users; - if(sd && !m_isdropping) + add_suppressed_comms(oargs); + + if(m_bpf) { - *evt = NULL; - return SCAP_TIMEOUT; + oargs.bpf_probe = m_bpf_probe.c_str(); + } + else + { + oargs.bpf_probe = NULL; } -#else - m_parser->process_event(&m_evt); -#endif -#if defined(HAS_FILTERING) && defined(HAS_CAPTURE_FILTERING) - if(m_evt.m_filtered_out) + add_suppressed_comms(oargs); + + int32_t scap_rc; + m_h = scap_open(oargs, error, &scap_rc); + + if(m_h == NULL) { - *evt = &m_evt; - return SCAP_TIMEOUT; + throw scap_open_exception(error, scap_rc); } -#endif + + scap_set_refresh_proc_table_when_saving(m_h, !m_filter_proc_table_when_saving); + + init(); +} + +void sinsp::open(uint32_t timeout_ms) +{ + open_live_common(timeout_ms, SCAP_MODE_LIVE); +} + +void sinsp::open_udig(uint32_t timeout_ms) +{ + m_udig = true; + open_live_common(timeout_ms, SCAP_MODE_LIVE); +} + +void sinsp::open_nodriver() +{ + char error[SCAP_LASTERR_SIZE]; + + g_logger.log("starting optimized sinsp"); // - // If needed, dump the event to file + // Reset the thread manager // - if(NULL != m_dumper) + m_thread_manager->clear(); + + // + // Start the capture + // + m_mode = SCAP_MODE_NODRIVER; + scap_open_args oargs; + oargs.mode = SCAP_MODE_NODRIVER; + oargs.fname = NULL; + oargs.proc_callback = NULL; + oargs.proc_callback_context = NULL; + if(!m_filter_proc_table_when_saving) { - res = scap_dump(m_h, m_dumper, m_evt.m_pevt, m_evt.m_cpuid); - if(SCAP_SUCCESS != res) + oargs.proc_callback = ::on_new_entry_from_proc; + oargs.proc_callback_context = this; + } + oargs.import_users = m_import_users; + + int32_t scap_rc; + m_h = scap_open(oargs, error, &scap_rc); + + if(m_h == NULL) + { + throw scap_open_exception(error, scap_rc); + } + + scap_set_refresh_proc_table_when_saving(m_h, !m_filter_proc_table_when_saving); + + init(); +} + +int64_t sinsp::get_file_size(const std::string& fname, char *error) +{ + static string err_str = "Could not determine capture file size: "; + std::string errdesc; +#ifdef _WIN32 + LARGE_INTEGER li = { 0 }; + HANDLE fh = CreateFile(fname.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); + if (fh != INVALID_HANDLE_VALUE) + { + if (0 != GetFileSizeEx(fh, &li)) { - throw sinsp_exception(scap_getlasterr(m_h)); + CloseHandle(fh); + return li.QuadPart; } + errdesc = get_error_desc(err_str); + CloseHandle(fh); + } +#else + struct stat st; + if (0 == stat(fname.c_str(), &st)) + { + return st.st_size; } +#endif + if(errdesc.empty()) errdesc = get_error_desc(err_str); + strncpy(error, errdesc.c_str(), errdesc.size() > SCAP_LASTERR_SIZE ? SCAP_LASTERR_SIZE : errdesc.size()); + return -1; +} - // - // Run the analysis engine - // -#ifdef HAS_ANALYZER - if(m_analyzer) +void sinsp::set_simpledriver_mode() +{ + if(scap_enable_simpledriver_mode(m_h) != SCAP_SUCCESS) { -#ifdef SIMULATE_DROP_MODE - if(!sd || m_isdropping || sw) + throw sinsp_exception(scap_getlasterr(m_h)); + } +} + +unsigned sinsp::m_num_possible_cpus = 0; + +unsigned sinsp::num_possible_cpus() +{ + if(m_num_possible_cpus == 0) + { + m_num_possible_cpus = read_num_possible_cpus(); + if(m_num_possible_cpus == 0) { - if(m_isdropping) - { - m_analyzer->process_event(&m_evt, sinsp_analyzer::DF_FORCE_FLUSH); - } - else if(sw) - { - m_analyzer->process_event(&m_evt, sinsp_analyzer::DF_FORCE_FLUSH_BUT_DONT_EMIT); - } - else - { - m_analyzer->process_event(&m_evt, sinsp_analyzer::DF_FORCE_NOFLUSH); - } + g_logger.log("Unable to read num_possible_cpus, falling back to 128", sinsp_logger::SEV_WARNING); + m_num_possible_cpus = 128; } -#else // SIMULATE_DROP_MODE - m_analyzer->process_event(&m_evt, sinsp_analyzer::DF_NONE); -#endif // SIMULATE_DROP_MODE + } + return m_num_possible_cpus; +} + +vector sinsp::get_n_tracepoint_hit() +{ + vector ret(num_possible_cpus(), 0); + if(scap_get_n_tracepoint_hit(m_h, ret.data()) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } + return ret; +} + +std::string sinsp::get_error_desc(const std::string& msg) +{ +#ifdef _WIN32 + DWORD err_no = GetLastError(); // first, so error is not wiped out by intermediate calls + std::string errstr = msg; + DWORD flg = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; + LPTSTR msg_buf = 0; + if(FormatMessageA(flg, 0, err_no, 0, (LPTSTR)&msg_buf, 0, NULL)) + if(msg_buf) + { + errstr.append(msg_buf, strlen(msg_buf)); + LocalFree(msg_buf); + } +#else + char* msg_buf = strerror(errno); // first, so error is not wiped out by intermediate calls + std::string errstr = msg; + if(msg_buf) + { + errstr.append(msg_buf, strlen(msg_buf)); } #endif + return errstr; +} + +void sinsp::open_int() +{ + char error[SCAP_LASTERR_SIZE] = {0}; // - // Update the last event time for this thread + // Reset the thread manager // - if(m_evt.m_tinfo) - { - m_evt.m_tinfo->m_prevevent_ts = m_evt.m_tinfo->m_lastevent_ts; - m_evt.m_tinfo->m_lastevent_ts = m_lastevent_ts; - } + m_thread_manager->clear(); // - // Done + // Start the capture // - *evt = &m_evt; - return res; + m_mode = SCAP_MODE_CAPTURE; + scap_open_args oargs; + oargs.mode = SCAP_MODE_CAPTURE; + if(m_input_fd != 0) + { + oargs.fd = m_input_fd; + } + else + { + oargs.fd = 0; + oargs.fname = m_input_filename.c_str(); + } + oargs.proc_callback = NULL; + oargs.proc_callback_context = NULL; + oargs.import_users = m_import_users; + if(m_file_start_offset != 0) + { + oargs.start_offset = m_file_start_offset; + } + else + { + oargs.start_offset = 0; + } + + add_suppressed_comms(oargs); + + int32_t scap_rc; + m_h = scap_open(oargs, error, &scap_rc); + + if(m_h == NULL) + { + throw scap_open_exception(error, scap_rc); + } + + if(m_input_fd != 0) + { + // We can't get a reliable filesize + m_filesize = 0; + } + else + { + m_filesize = get_file_size(m_input_filename, error); + + if(m_filesize < 0) + { + throw sinsp_exception(error); + } + } + + init(); } -uint64_t sinsp::get_num_events() +void sinsp::open(const std::string &filename) +{ + if(filename.empty()) + { + open(); + return; + } + + m_input_filename = filename; + + g_logger.log("starting offline capture"); + + open_int(); +} + +void sinsp::fdopen(int fd) { - return scap_event_get_num(m_h); + m_input_fd = fd; + + g_logger.log("starting offline capture"); + + open_int(); } -sinsp_threadinfo* sinsp::get_thread(int64_t tid, bool query_os_if_not_found) +void sinsp::close() { - sinsp_threadinfo* sinsp_proc = m_thread_manager->get_thread(tid); + if(m_h) + { + scap_close(m_h); + m_h = NULL; + } - if(sinsp_proc == NULL && query_os_if_not_found) + if(NULL != m_dumper) { - sinsp_threadinfo newti(this); - scap_threadinfo* scap_proc = NULL; - m_n_proc_lookups++; + scap_dump_close(m_dumper); + m_dumper = NULL; + } + + m_is_dumping = false; + + if(NULL != m_network_interfaces) + { + delete m_network_interfaces; + m_network_interfaces = NULL; + } + +#ifdef HAS_FILTERING + if(m_filter != NULL) + { + delete m_filter; + m_filter = NULL; + } + + if(m_evttype_filter != NULL) + { + delete m_evttype_filter; + m_evttype_filter = NULL; + } +#endif +} + +void sinsp::autodump_start(const string& dump_filename, bool compress) +{ + if(NULL == m_h) + { + throw sinsp_exception("inspector not opened yet"); + } + + if(compress) + { + m_dumper = scap_dump_open(m_h, dump_filename.c_str(), SCAP_COMPRESSION_GZIP, false); + } + else + { + m_dumper = scap_dump_open(m_h, dump_filename.c_str(), SCAP_COMPRESSION_NONE, false); + } + + m_is_dumping = true; + + if(NULL == m_dumper) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } + + m_container_manager.dump_containers(m_dumper); +} + +void sinsp::autodump_next_file() +{ + autodump_stop(); + autodump_start(m_cycle_writer->get_current_file_name(), m_compress); +} + +void sinsp::autodump_stop() +{ + if(NULL == m_h) + { + throw sinsp_exception("inspector not opened yet"); + } + + if(m_dumper != NULL) + { + scap_dump_close(m_dumper); + m_dumper = NULL; + } + + m_is_dumping = false; +} + +void sinsp::on_new_entry_from_proc(void* context, + scap_t* handle, + int64_t tid, + scap_threadinfo* tinfo, + scap_fdinfo* fdinfo) +{ + ASSERT(tinfo != NULL); + + m_h = handle; - if(m_n_proc_lookups == m_max_n_proc_socket_lookups) + // + // Retrieve machine information if we don't have it yet + // + { + m_machine_info = scap_get_machine_info(handle); + if(m_machine_info != NULL) { - g_logger.format(sinsp_logger::SEV_INFO, "Reached max socket lookup number"); + m_num_cpus = m_machine_info->num_cpus; } - - if(m_n_proc_lookups == m_max_n_proc_lookups) + else { - g_logger.format(sinsp_logger::SEV_INFO, "Reached max processs lookup number"); + ASSERT(false); + m_num_cpus = 0; } + } - if(m_max_n_proc_lookups == 0 || (m_max_n_proc_lookups != 0 && - (m_n_proc_lookups <= m_max_n_proc_lookups))) + // + // Add the thread or FD + // + if(fdinfo == NULL) + { + bool thread_added = false; + sinsp_threadinfo* newti = build_threadinfo(); + newti->init(tinfo); + if(is_nodriver()) { - bool scan_sockets = true; - - if(m_max_n_proc_socket_lookups == 0 || (m_max_n_proc_socket_lookups != 0 && - (m_n_proc_lookups <= m_max_n_proc_socket_lookups))) + auto sinsp_tinfo = find_thread(tid, true); + if(sinsp_tinfo == nullptr || newti->m_clone_ts > sinsp_tinfo->m_clone_ts) { - scan_sockets = false; + thread_added = m_thread_manager->add_thread(newti, true); } - - scap_proc = scap_proc_get(m_h, tid, scan_sockets); } - - if(scap_proc) + else { - newti.init(scap_proc); - scap_proc_free(m_h, scap_proc); + thread_added = m_thread_manager->add_thread(newti, true); } - else + if (!thread_added) { + delete newti; + } + } + else + { + auto sinsp_tinfo = find_thread(tid, true); + + if(!sinsp_tinfo) { - // - // Add a fake entry to avoid a continuous lookup - // - newti.m_tid = tid; - newti.m_pid = tid; - newti.m_ptid = -1; - newti.m_comm = ""; - newti.m_exe = ""; - newti.m_uid = 0xffffffff; - newti.m_gid = 0xffffffff; + sinsp_threadinfo* newti = build_threadinfo(); + newti->init(tinfo); + + if (!m_thread_manager->add_thread(newti, true)) { + ASSERT(false); + delete newti; + return; + } + + sinsp_tinfo = find_thread(tid, true); + if (!sinsp_tinfo) { + ASSERT(false); + return; + } } - m_thread_manager->add_thread(newti); - sinsp_proc = m_thread_manager->get_thread(tid); - } + sinsp_fdinfo_t sinsp_fdinfo; + sinsp_tinfo->add_fd_from_scap(fdinfo, &sinsp_fdinfo); + } +} + +void on_new_entry_from_proc(void* context, + scap_t* handle, + int64_t tid, + scap_threadinfo* tinfo, + scap_fdinfo* fdinfo) +{ + sinsp* _this = (sinsp*)context; + _this->on_new_entry_from_proc(context, handle, tid, tinfo, fdinfo); +} + +void sinsp::import_thread_table() +{ + scap_threadinfo *pi; + scap_threadinfo *tpi; + + scap_threadinfo *table = scap_get_proc_table(m_h); + + // + // Scan the scap table and add the threads to our list + // + HASH_ITER(hh, table, pi, tpi) + { + sinsp_threadinfo* newti = build_threadinfo(); + newti->init(pi); + m_thread_manager->add_thread(newti, true); + } +} + +void sinsp::import_ifaddr_list() +{ + m_network_interfaces = new sinsp_network_interfaces(this); + m_network_interfaces->import_interfaces(scap_get_ifaddr_list(m_h)); +} + +sinsp_network_interfaces* sinsp::get_ifaddr_list() +{ + return m_network_interfaces; +} + +void sinsp::import_user_list() +{ + uint32_t j; + scap_userlist* ul = scap_get_user_list(m_h); + + if(ul) + { + for(j = 0; j < ul->nusers; j++) + { + m_userlist[ul->users[j].uid] = &(ul->users[j]); + } + + for(j = 0; j < ul->ngroups; j++) + { + m_grouplist[ul->groups[j].gid] = &(ul->groups[j]); + } + } +} + +void sinsp::import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo) +{ + ASSERT(m_network_interfaces); + m_network_interfaces->import_ipv4_interface(ifinfo); +} + +void sinsp::refresh_ifaddr_list() +{ +#ifdef HAS_CAPTURE + if(!is_capture()) + { + ASSERT(m_network_interfaces); + scap_refresh_iflist(m_h); + m_network_interfaces->clear(); + m_network_interfaces->import_interfaces(scap_get_ifaddr_list(m_h)); + } +#endif +} + +bool should_drop(sinsp_evt *evt, bool* stopped, bool* switched); + +void sinsp::add_meta_event(sinsp_evt *metaevt) +{ + m_metaevt = metaevt; +} + +void sinsp::add_meta_event_callback(meta_event_callback cback, void* data) +{ + m_meta_event_callback = cback; + m_meta_event_callback_data = data; +} + +void sinsp::remove_meta_event_callback() +{ + m_meta_event_callback = NULL; +} + +void schedule_next_threadinfo_evt(sinsp* _this, void* data) +{ + sinsp_proc_metainfo* mei = (sinsp_proc_metainfo*)data; + ASSERT(mei->m_pli != NULL); + + while(true) + { + ASSERT(mei->m_cur_procinfo_evt <= (int32_t)mei->m_n_procinfo_evts); + ppm_proc_info* pi = &(mei->m_pli->entries[mei->m_cur_procinfo_evt]); + + if(mei->m_cur_procinfo_evt >= 0) + { + mei->m_piscapevt->tid = pi->pid; + mei->m_piscapevt_vals[0] = pi->utime; + mei->m_piscapevt_vals[1] = pi->stime; + } + + mei->m_cur_procinfo_evt++; + + if(mei->m_cur_procinfo_evt < (int32_t)mei->m_n_procinfo_evts) + { + if(pi->utime == 0 && pi->stime == 0) + { + continue; + } + + _this->add_meta_event(&mei->m_pievt); + } + + break; + } +} + +void sinsp::restart_capture_at_filepos(uint64_t filepos) +{ + // + // Backup a couple of settings + // + uint64_t evtnum = m_nevts; + string filterstring = m_filterstring; + + // + // Close and reopen the capture + // + m_file_start_offset = filepos; + close(); + open_int(); + + // + // Set again the backuped settings + // + m_evt.m_evtnum = evtnum; + m_nevts = evtnum; + if(filterstring != "") + { + set_filter(filterstring); + } +} + +uint64_t sinsp::max_buf_used() +{ + if(m_h) + { + return scap_max_buf_used(m_h); + } + else + { + return 0; + } +} + +int32_t sinsp::next(OUT sinsp_evt **puevt) +{ + sinsp_evt* evt; + int32_t res; + + // + // Check if there are fake cpu events to events + // + if(m_metaevt != NULL) + { + res = SCAP_SUCCESS; + evt = m_metaevt; + m_metaevt = NULL; + + if(m_meta_event_callback != NULL) + { + m_meta_event_callback(this, m_meta_event_callback_data); + } + } +#ifndef _WIN32 + else if (m_pending_container_evts.try_pop(m_container_evt)) + { + res = SCAP_SUCCESS; + evt = m_container_evt.get(); + } +#endif + else + { + evt = &m_evt; + + // + // Reset previous event's decoders if required + // + if(m_decoders_reset_list.size() != 0) + { + vector::iterator it; + for(it = m_decoders_reset_list.begin(); it != m_decoders_reset_list.end(); ++it) + { + (*it)->on_reset(evt); + } + + m_decoders_reset_list.clear(); + } + + // + // Get the event from libscap + // + res = scap_next(m_h, &(evt->m_pevt), &(evt->m_cpuid)); + + if(res != SCAP_SUCCESS) + { + if(res == SCAP_TIMEOUT) + { + if (m_external_event_processor) + { + m_external_event_processor->process_event(NULL, libsinsp::EVENT_RETURN_TIMEOUT); + } + *puevt = NULL; + return res; + } + else if(res == SCAP_EOF) + { + if (m_external_event_processor) + { + m_external_event_processor->process_event(NULL, libsinsp::EVENT_RETURN_EOF); + } + } + else if(res == SCAP_UNEXPECTED_BLOCK) + { + uint64_t filepos = scap_ftell(m_h) - scap_get_unexpected_block_readsize(m_h); + restart_capture_at_filepos(filepos); + return SCAP_TIMEOUT; + + } + else + { + m_lasterr = scap_getlasterr(m_h); + } + + return res; + } + } + + uint64_t ts = evt->get_ts(); + + if(m_firstevent_ts == 0 && evt->m_pevt->type != PPME_CONTAINER_JSON_E) + { + m_firstevent_ts = ts; + } + + // + // If required, retrieve the processes cpu from the kernel + // + if(m_get_procs_cpu_from_driver && is_live() && !m_udig) + { + if(ts > m_next_flush_time_ns) + { + if(m_next_flush_time_ns != 0) + { + struct timeval tod; + gettimeofday(&tod, NULL); + + uint64_t procrequest_tod = (uint64_t)tod.tv_sec * 1000000000 + tod.tv_usec * 1000; + + if(procrequest_tod - m_last_procrequest_tod > ONE_SECOND_IN_NS / 2) + { + m_last_procrequest_tod = procrequest_tod; + m_next_flush_time_ns = ts - (ts % ONE_SECOND_IN_NS) + ONE_SECOND_IN_NS; + + m_meinfo.m_pli = scap_get_threadlist(m_h); + if(m_meinfo.m_pli == NULL) + { + throw sinsp_exception(string("scap error: ") + scap_getlasterr(m_h)); + } + + m_meinfo.m_n_procinfo_evts = m_meinfo.m_pli->n_entries; + + if(m_meinfo.m_n_procinfo_evts > 0) + { + m_meinfo.m_cur_procinfo_evt = -1; + + m_meinfo.m_piscapevt->ts = m_next_flush_time_ns - (ONE_SECOND_IN_NS + 1); + add_meta_event_callback(&schedule_next_threadinfo_evt, &m_meinfo); + schedule_next_threadinfo_evt(this, &m_meinfo); + } + + return SCAP_TIMEOUT; + } + } + + m_next_flush_time_ns = ts - (ts % ONE_SECOND_IN_NS) + ONE_SECOND_IN_NS; + } + } + + // + // Store a couple of values that we'll need later inside the event. + // + m_nevts++; + evt->m_evtnum = m_nevts; + m_lastevent_ts = ts; + +#ifndef HAS_ANALYZER + // + // Delayed removal of threads from the thread table, so that + // things like exit() or close() can be parsed. + // We only do this if the analyzer is not enabled, because the analyzer + // needs the process at the end of the sample and will take care of deleting + // it. + // + if(m_tid_to_remove != -1) + { + remove_thread(m_tid_to_remove, false); + m_tid_to_remove = -1; + } + + if(is_debug_enabled() && is_live()) + { + if(ts > m_next_stats_print_time_ns) + { + if(m_next_stats_print_time_ns) + { + scap_stats stats; + get_capture_stats(&stats); + + g_logger.format(sinsp_logger::SEV_DEBUG, + "n_evts:%" PRIu64 + " n_drops:%" PRIu64 + " n_drops_buffer:%" PRIu64 + " n_drops_pf:%" PRIu64 + " n_drops_bug:%" PRIu64, + stats.n_evts, + stats.n_drops, + stats.n_drops_buffer, + stats.n_drops_pf, + stats.n_drops_bug); + } + + m_next_stats_print_time_ns = ts - (ts % ONE_SECOND_IN_NS) + ONE_SECOND_IN_NS; + } + } + + // + // Run the periodic connection and thread table cleanup + // + if(!is_capture()) + { + m_thread_manager->remove_inactive_threads(); + m_container_manager.remove_inactive_containers(); + +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + update_k8s_state(); + + if(m_mesos_client) + { + update_mesos_state(); + } +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + } +#endif // HAS_ANALYZER + + // + // Delayed removal of the fd, so that + // things like exit() or close() can be parsed. + // + uint32_t nfdr = (uint32_t)m_fds_to_remove->size(); + + if(nfdr != 0) + { + sinsp_threadinfo* ptinfo = get_thread(m_tid_of_fd_to_remove, true, true); + if(!ptinfo) + { + ASSERT(false); + return res; + } + + for(uint32_t j = 0; j < nfdr; j++) + { + ptinfo->remove_fd(m_fds_to_remove->at(j)); + } + + m_fds_to_remove->clear(); + } + +#ifdef SIMULATE_DROP_MODE + bool sd = false; + bool sw = false; + + if(m_analyzer) + { + m_analyzer->m_configuration->set_analyzer_sample_len_ns(500000000); + } + + sd = should_drop(evt, &m_isdropping, &sw); +#endif + + // + // Run the state engine + // +#ifdef SIMULATE_DROP_MODE + if(!sd || m_isdropping) + { + m_parser->process_event(evt); + } + + if(sd && !m_isdropping) + { + *evt = NULL; + return SCAP_TIMEOUT; + } +#else + m_parser->process_event(evt); +#endif + + // + // If needed, dump the event to file + // + if(NULL != m_dumper) + { + +#if defined(HAS_FILTERING) && defined(HAS_CAPTURE_FILTERING) + scap_dump_flags dflags; + + bool do_drop; + dflags = evt->get_dump_flags(&do_drop); + if(do_drop) + { + *puevt = evt; + return SCAP_TIMEOUT; + } +#endif + + if(m_write_cycling) + { + switch(m_cycle_writer->consider(evt)) + { + case cycle_writer::NEWFILE: + autodump_next_file(); + break; + + case cycle_writer::DOQUIT: + stop_capture(); + return SCAP_EOF; + break; + + case cycle_writer::SAMEFILE: + // do nothing. + break; + } + } + + scap_evt* pdevt = (evt->m_poriginal_evt)? evt->m_poriginal_evt : evt->m_pevt; + + res = scap_dump(m_h, m_dumper, pdevt, evt->m_cpuid, dflags); + + if(SCAP_SUCCESS != res) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } + } + +#if defined(HAS_FILTERING) && defined(HAS_CAPTURE_FILTERING) + if(evt->m_filtered_out) + { + ppm_event_category cat = evt->get_info_category(); + + // Skip the event, unless we're in internal events + // mode and the category of this event is internal. + if(!(m_isinternal_events_enabled && (cat & EC_INTERNAL))) + { + *puevt = evt; + return SCAP_TIMEOUT; + } + } +#endif + + // + // Run the analysis engine + // + if (m_external_event_processor) + { + m_external_event_processor->process_event(evt, libsinsp::EVENT_RETURN_NONE); + } + + // Clean parse related event data after analyzer did its parsing too + m_parser->event_cleanup(evt); + + // + // Update the last event time for this thread + // + if(evt->m_tinfo && + evt->get_type() != PPME_SCHEDSWITCH_1_E && + evt->get_type() != PPME_SCHEDSWITCH_6_E) + { + evt->m_tinfo->m_prevevent_ts = evt->m_tinfo->m_lastevent_ts; + evt->m_tinfo->m_lastevent_ts = m_lastevent_ts; + } + + // + // Done + // + *puevt = evt; + return res; +} + +uint64_t sinsp::get_num_events() +{ + if(m_h) + { + return scap_event_get_num(m_h); + } + else + { + return 0; + } +} + +sinsp_threadinfo* sinsp::find_thread_test(int64_t tid, bool lookup_only) +{ + // TODO: we pay the refcount manipulation price here + return &*find_thread(tid, lookup_only); +} + +sinsp_threadinfo* sinsp::get_thread(int64_t tid, bool query_os_if_not_found, bool lookup_only) +{ + // TODO: we pay the refcount manipulation price here + return &*get_thread_ref(tid, query_os_if_not_found, lookup_only); +} + +threadinfo_map_t::ptr_t sinsp::get_thread_ref(int64_t tid, bool query_os_if_not_found, bool lookup_only, bool main_thread) +{ + auto sinsp_proc = find_thread(tid, lookup_only); + + if(!sinsp_proc && query_os_if_not_found && + (m_thread_manager->m_threadtable.size() < m_max_thread_table_size +#if defined(HAS_CAPTURE) + || tid == m_sysdig_pid +#endif + )) + { + // Certain code paths can lead to this point from scap_open() (incomplete example: + // scap_proc_scan_proc_dir() -> resolve_container() -> get_env()). Adding a + // defensive check here to protect both, callers of get_env and get_thread. + if (!m_h) + { + g_logger.format(sinsp_logger::SEV_INFO, "%s: Unable to complete for tid=%" + PRIu64 ": sinsp::scap_t* is uninitialized", __func__, tid); + return NULL; + } + + scap_threadinfo* scap_proc = NULL; + sinsp_threadinfo* newti = build_threadinfo(); + + m_n_proc_lookups++; + + if(main_thread) + { + m_n_main_thread_lookups++; + } + + if(m_n_proc_lookups == m_max_n_proc_lookups) + { + g_logger.format(sinsp_logger::SEV_INFO, "Reached max process lookup number, duration=%" PRIu64 "ms", + m_n_proc_lookups_duration_ns / 1000000); + } + + if(m_max_n_proc_lookups < 0 || + m_n_proc_lookups <= m_max_n_proc_lookups) + { +#ifdef HAS_ANALYZER + tracer_emitter("sinsp_proc_lookup"); +#endif + + bool scan_sockets = false; + + if(m_max_n_proc_socket_lookups < 0 || + m_n_proc_lookups <= m_max_n_proc_socket_lookups) + { + scan_sockets = true; + if(m_n_proc_lookups == m_max_n_proc_socket_lookups) + { + g_logger.format(sinsp_logger::SEV_INFO, "Reached max socket lookup number, tid=%" PRIu64 ", duration=%" PRIu64 "ms", + tid, m_n_proc_lookups_duration_ns / 1000000); + } + } + +#ifdef HAS_ANALYZER + uint64_t ts = sinsp_utils::get_current_time_ns(); +#endif + scap_proc = scap_proc_get(m_h, tid, scan_sockets); +#ifdef HAS_ANALYZER + m_n_proc_lookups_duration_ns += sinsp_utils::get_current_time_ns() - ts; +#endif + } + + if(scap_proc) + { + newti->init(scap_proc); + scap_proc_free(m_h, scap_proc); + } + else + { + // + // Add a fake entry to avoid a continuous lookup + // + newti->m_tid = tid; + newti->m_pid = tid; + newti->m_ptid = -1; + newti->m_comm = ""; + newti->m_exe = ""; + newti->m_uid = 0xffffffff; + newti->m_gid = 0xffffffff; + newti->m_nchilds = 0; + newti->m_loginuid = 0xffffffff; + } + + // + // Since this thread is created out of thin air, we need to + // properly set its reference count, by scanning the table + // + m_thread_manager->m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + if(tinfo.m_pid == tid) + { + newti->m_nchilds++; + } + return true; + }); + + // + // Done. Add the new thread to the list. + // + m_thread_manager->add_thread(newti, false); + sinsp_proc = find_thread(tid, lookup_only); + } + + return sinsp_proc; +} + +sinsp_threadinfo* sinsp::get_thread(int64_t tid) +{ + return get_thread(tid, false, true); +} + +bool sinsp::add_thread(const sinsp_threadinfo *ptinfo) +{ + return m_thread_manager->add_thread((sinsp_threadinfo*)ptinfo, false); +} + +void sinsp::remove_thread(int64_t tid, bool force) +{ + m_thread_manager->remove_thread(tid, force); +} + +bool sinsp::suppress_events_comm(const std::string &comm) +{ + if(m_suppressed_comms.size() >= SCAP_MAX_SUPPRESSED_COMMS) + { + return false; + } + + m_suppressed_comms.insert(comm); + + if(m_h) + { + if (scap_suppress_events_comm(m_h, comm.c_str()) != SCAP_SUCCESS) + { + return false; + } + } + + return true; +} + +bool sinsp::check_suppressed(int64_t tid) +{ + return scap_check_suppressed_tid(m_h, tid); +} + +void sinsp::add_suppressed_comms(scap_open_args &oargs) +{ + uint32_t i = 0; + + // Note--using direct pointers to values in + // m_suppressed_comms. This is ok given that a scap_open() + // will immediately follow after which the args won't be used. + for(auto &comm : m_suppressed_comms) + { + oargs.suppressed_comms[i++] = comm.c_str(); + } + + oargs.suppressed_comms[i++] = NULL; +} + +void sinsp::set_docker_socket_path(std::string socket_path) +{ + m_container_manager.set_docker_socket_path(std::move(socket_path)); +} + +void sinsp::set_query_docker_image_info(bool query_image_info) +{ + m_container_manager.set_query_docker_image_info(query_image_info); +} + +void sinsp::set_cri_extra_queries(bool extra_queries) +{ + m_container_manager.set_cri_extra_queries(extra_queries); +} + +void sinsp::set_cri_socket_path(const std::string& path) +{ + m_container_manager.set_cri_socket_path(path); +} + +void sinsp::set_cri_timeout(int64_t timeout_ms) +{ + m_container_manager.set_cri_timeout(timeout_ms); +} + +void sinsp::set_cri_async(bool async) +{ + m_container_manager.set_cri_async(async); +} + +void sinsp::set_cri_delay(uint64_t delay_ms) +{ + m_container_manager.set_cri_delay(delay_ms); +} + +void sinsp::set_snaplen(uint32_t snaplen) +{ + // + // If set_snaplen is called before opening of the inspector, + // we register the value to be set after its initialization. + // + if(m_h == NULL) + { + m_snaplen = snaplen; + return; + } + + if(is_live() && scap_set_snaplen(m_h, snaplen) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } +} + +void sinsp::set_fullcapture_port_range(uint16_t range_start, uint16_t range_end) +{ + // + // If set_fullcapture_port_range is called before opening of the inspector, + // we register the value to be set after its initialization. + // + if(m_h == NULL) + { + m_increased_snaplen_port_range = {range_start, range_end}; + return; + } + + if(!is_live()) + { + throw sinsp_exception("set_fullcapture_port_range called on a trace file"); + } + + if(scap_set_fullcapture_port_range(m_h, range_start, range_end) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } +} + +void sinsp::set_statsd_port(const uint16_t port) +{ + // + // If this method is called before opening of the inspector, + // we register the value to be set after its initialization. + // + if(m_h == NULL) + { + m_statsd_port = port; + return; + } + + if(!is_live()) + { + throw sinsp_exception("set_statsd_port called on a trace file"); + } + + if(scap_set_statsd_port(m_h, port) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } +} + +void sinsp::stop_capture() +{ + if(scap_stop_capture(m_h) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } +} + +void sinsp::start_capture() +{ + if(scap_start_capture(m_h) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } +} + +void sinsp::stop_dropping_mode() +{ + if(m_mode == SCAP_MODE_LIVE) + { + g_logger.format(sinsp_logger::SEV_INFO, "stopping drop mode"); + + if(scap_stop_dropping_mode(m_h) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } + } +} + +void sinsp::start_dropping_mode(uint32_t sampling_ratio) +{ + if(m_mode == SCAP_MODE_LIVE) + { + g_logger.format(sinsp_logger::SEV_INFO, "setting drop mode to %" PRIu32, sampling_ratio); + + if(scap_start_dropping_mode(m_h, sampling_ratio) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } + } +} + +#ifdef HAS_FILTERING +void sinsp::set_filter(sinsp_filter* filter) +{ + if(m_filter != NULL) + { + ASSERT(false); + throw sinsp_exception("filter can only be set once"); + } + + m_filter = filter; +} + +void sinsp::set_filter(const string& filter) +{ + if(m_filter != NULL) + { + ASSERT(false); + throw sinsp_exception("filter can only be set once"); + } + + sinsp_filter_compiler compiler(this, filter); + m_filter = compiler.compile(); + m_filterstring = filter; +} + +const string sinsp::get_filter() +{ + return m_filterstring; +} + +void sinsp::add_evttype_filter(string &name, + set &evttypes, + set &syscalls, + set &tags, + sinsp_filter *filter) +{ + // Create the evttype filter if it doesn't exist. + if(m_evttype_filter == NULL) + { + m_evttype_filter = new sinsp_evttype_filter(); + } + + m_evttype_filter->add(name, evttypes, syscalls, tags, filter); +} + +bool sinsp::run_filters_on_evt(sinsp_evt *evt) +{ + // + // First run the global filter, if there is one. + // + if(m_filter && m_filter->run(evt) == true) + { + return true; + } + + // + // Then run the evttype filter, if there is one. + if(m_evttype_filter && m_evttype_filter->run(evt) == true) + { + return true; + } + + return false; +} +#endif + +const scap_machine_info* sinsp::get_machine_info() +{ + return m_machine_info; +} + +const unordered_map* sinsp::get_userlist() +{ + return &m_userlist; +} + +scap_userinfo* sinsp::get_user(uint32_t uid) +{ + unordered_map::const_iterator it; + if(uid == 0xffffffff) + { + return NULL; + } + + it = m_userlist.find(uid); + if(it == m_userlist.end()) + { + return NULL; + } + + return it->second; +} + +const unordered_map* sinsp::get_grouplist() +{ + return &m_grouplist; +} + +#ifdef HAS_FILTERING +void sinsp::get_filtercheck_fields_info(OUT vector* list) +{ + sinsp_utils::get_filtercheck_fields_info(list); +} +#else +void sinsp::get_filtercheck_fields_info(OUT vector* list) +{ +} +#endif + +uint32_t sinsp::reserve_thread_memory(uint32_t size) +{ + if(m_h != NULL) + { + throw sinsp_exception("reserve_thread_memory can't be called after capture starts"); + } + + return m_thread_privatestate_manager.reserve(size); +} + +void sinsp::get_capture_stats(scap_stats* stats) const +{ + if(scap_get_stats(m_h, stats) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } +} + +void sinsp::set_max_thread_table_size(uint32_t value) +{ + uint32_t max_size = uint32_t(MAX_THREAD_TABLE_SIZE); + m_max_thread_table_size = (value < max_size ? value : max_size); +} + +#ifdef GATHER_INTERNAL_STATS +sinsp_stats sinsp::get_stats() +{ + scap_stats stats; + + // + // Get capture stats from scap + // + if(m_h) + { + scap_get_stats(m_h, &stats); + + m_stats.m_n_seen_evts = stats.n_evts; + m_stats.m_n_drops = stats.n_drops; + m_stats.m_n_preemptions = stats.n_preemptions; + } + else + { + m_stats.m_n_seen_evts = 0; + m_stats.m_n_drops = 0; + m_stats.m_n_preemptions = 0; + } + + // + // Count the number of threads and fds by scanning the tables, + // and update the thread-related stats. + // + if(m_thread_manager) + { + m_thread_manager->update_statistics(); + } + + // + // Return the result + // + + return m_stats; +} +#endif // GATHER_INTERNAL_STATS + +void sinsp::set_log_callback(sinsp_logger_callback cb) +{ + if(cb) + { + g_logger.add_callback_log(cb); + } + else + { + g_logger.remove_callback_log(); + } +} + +void sinsp::set_log_file(string filename) +{ + g_logger.add_file_log(filename); +} + +void sinsp::set_log_stderr() +{ + g_logger.add_stderr_log(); +} + +void sinsp::set_min_log_severity(sinsp_logger::severity sev) +{ + g_logger.set_severity(sev); +} + +sinsp_evttables* sinsp::get_event_info_tables() +{ + return &g_infotables; +} + +void sinsp::add_chisel_dir(string dirname, bool front_add) +{ +#ifdef HAS_CHISELS + trim(dirname); + + if(dirname[dirname.size() -1] != '/') + { + dirname += "/"; + } + + chiseldir_info ncdi; + + ncdi.m_dir = std::move(dirname); + ncdi.m_need_to_resolve = false; + + if(front_add) + { + g_chisel_dirs->insert(g_chisel_dirs->begin(), ncdi); + } + else + { + g_chisel_dirs->push_back(ncdi); + } +#endif +} + +void sinsp::set_buffer_format(sinsp_evt::param_fmt format) +{ + m_buffer_format = format; +} + +void sinsp::set_drop_event_flags(ppm_event_flags flags) +{ + m_parser->m_drop_event_flags = flags; +} + +sinsp_evt::param_fmt sinsp::get_buffer_format() +{ + return m_buffer_format; +} - return sinsp_proc; +void sinsp::set_large_envs(bool enable) +{ + m_large_envs_enabled = enable; } -sinsp_threadinfo* sinsp::get_thread(int64_t tid) +void sinsp::set_debug_mode(bool enable_debug) { - return get_thread(tid, false); + m_isdebug_enabled = enable_debug; } -void sinsp::add_thread(const sinsp_threadinfo& ptinfo) +void sinsp::set_print_container_data(bool print_container_data) { - m_thread_manager->add_thread((sinsp_threadinfo&)ptinfo); + m_print_container_data = print_container_data; } -void sinsp::remove_thread(int64_t tid) +void sinsp::set_fatfile_dump_mode(bool enable_fatfile) { - m_thread_manager->remove_thread(tid); + m_isfatfile_enabled = enable_fatfile; } -void sinsp::set_snaplen(uint32_t snaplen) +void sinsp::set_internal_events_mode(bool enable_internal_events) { - // - // If set_snaplen is called before opening of the inspector, - // we register the value to be set after its initialization. - // - if (m_h == NULL) - { - m_snaplen = snaplen; - return; - } + m_isinternal_events_enabled = enable_internal_events; +} - if(scap_set_snaplen(m_h, snaplen) != SCAP_SUCCESS) - { - // - // We know that setting the snaplen on a file doesn't do anything and - // we're ok with it. - // - if(m_islive) - { - throw sinsp_exception(scap_getlasterr(m_h)); - } - } +void sinsp::set_hostname_and_port_resolution_mode(bool enable) +{ + m_hostname_and_port_resolution_enabled = enable; } -void sinsp::stop_capture() +void sinsp::set_max_evt_output_len(uint32_t len) { - if(scap_stop_capture(m_h) != SCAP_SUCCESS) - { - throw sinsp_exception(scap_getlasterr(m_h)); - } + m_max_evt_output_len = len; } -void sinsp::start_capture() +sinsp_protodecoder* sinsp::require_protodecoder(string decoder_name) { - if(scap_start_capture(m_h) != SCAP_SUCCESS) + return m_parser->add_protodecoder(decoder_name); +} + +void sinsp::set_eventmask(uint32_t event_types) +{ + if (scap_set_eventmask(m_h, event_types) != SCAP_SUCCESS) { throw sinsp_exception(scap_getlasterr(m_h)); } } -void sinsp::stop_dropping_mode() +void sinsp::unset_eventmask(uint32_t event_id) { - if(scap_stop_dropping_mode(m_h) != SCAP_SUCCESS) + if (scap_unset_eventmask(m_h, event_id) != SCAP_SUCCESS) { throw sinsp_exception(scap_getlasterr(m_h)); } } -void sinsp::start_dropping_mode(uint32_t sampling_ratio) +void sinsp::protodecoder_register_reset(sinsp_protodecoder* dec) +{ + m_decoders_reset_list.push_back(dec); +} + +sinsp_parser* sinsp::get_parser() +{ + return m_parser; +} + +bool sinsp::setup_cycle_writer(string base_file_name, int rollover_mb, int duration_seconds, int file_limit, unsigned long event_limit, bool compress) { - if(m_islive) + m_compress = compress; + + if(rollover_mb != 0 || duration_seconds != 0 || file_limit != 0 || event_limit != 0) { - if(scap_start_dropping_mode(m_h, sampling_ratio) != SCAP_SUCCESS) - { - throw sinsp_exception(scap_getlasterr(m_h)); - } + m_write_cycling = true; } + + return m_cycle_writer->setup(base_file_name, rollover_mb, duration_seconds, file_limit, event_limit, &m_dumper); } -#ifdef HAS_FILTERING -void sinsp::set_filter(string filter) +double sinsp::get_read_progress() { - if(m_filter != NULL) + if(m_input_fd != 0) { - ASSERT(false); - throw sinsp_exception("filter can only be set once"); + // We can't get a reliable file size, so we can't get + // any reliable progress + return 0; + } + + if(m_filesize == -1) + { + throw sinsp_exception(scap_getlasterr(m_h)); + } + + ASSERT(m_filesize != 0); + + int64_t fpos = scap_get_readfile_offset(m_h); + + if(fpos == -1) + { + throw sinsp_exception(scap_getlasterr(m_h)); } - m_filter = new sinsp_filter(this, filter); + return (double)fpos * 100 / m_filesize; } -#endif -const scap_machine_info* sinsp::get_machine_info() +bool sinsp::remove_inactive_threads() { - return m_machine_info; + return m_thread_manager->remove_inactive_threads(); } -const unordered_map* sinsp::get_userlist() +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) +void sinsp::init_mesos_client(string* api_server, bool verbose) { - return &m_userlist; + m_verbose_json = verbose; + if(m_mesos_client == NULL) + { + if(api_server) + { + // -m + std::string::size_type pos = api_server->find(','); + if(pos != std::string::npos) + { + m_marathon_api_server.clear(); + m_marathon_api_server.push_back(api_server->substr(pos + 1)); + } + m_mesos_api_server = api_server->substr(0, pos); + } + + bool is_live = !m_mesos_api_server.empty(); + m_mesos_client = new mesos(m_mesos_api_server, + m_marathon_api_server, + true, // mesos leader auto-follow + m_marathon_api_server.empty(), // marathon leader auto-follow if no uri + mesos::credentials_t(), // mesos creds, the only way to provide creds in sysdig is embedded in URI + mesos::credentials_t(), // marathon creds + mesos::default_timeout_ms, + is_live, + m_verbose_json); + } } -const unordered_map* sinsp::get_grouplist() +void sinsp::init_k8s_ssl(const string *ssl_cert) { - return &m_grouplist; +#ifdef HAS_CAPTURE + if(ssl_cert != nullptr && !ssl_cert->empty() + && (!m_k8s_ssl || ! m_k8s_bt)) + { + std::string cert; + std::string key; + std::string key_pwd; + std::string ca_cert; + + // -K | :[:] + std::string::size_type pos = ssl_cert->find(':'); + if(pos == std::string::npos) // ca_cert-only is obsoleted, single entry is now bearer token + { + m_k8s_bt = std::make_shared(*ssl_cert); + } + else + { + cert = ssl_cert->substr(0, pos); + if(cert.empty()) + { + throw sinsp_exception(string("Invalid K8S SSL entry: ") + *ssl_cert); + } + + // pos < ssl_cert->length() so it's safe to take + // substr() from head, but it may be empty + std::string::size_type head = pos + 1; + pos = ssl_cert->find(':', head); + if (pos == std::string::npos) + { + key = ssl_cert->substr(head); + } + else + { + key = ssl_cert->substr(head, pos - head); + ca_cert = ssl_cert->substr(pos + 1); + } + if(key.empty()) + { + throw sinsp_exception(string("Invalid K8S SSL entry: ") + *ssl_cert); + } + + // Parse the password if it exists + pos = key.find('#'); + if(pos != std::string::npos) + { + key_pwd = key.substr(pos + 1); + key = key.substr(0, pos); + } + } + g_logger.format(sinsp_logger::SEV_TRACE, + "Creating sinsp_ssl with cert %s, key %s, key_pwd %s, ca_cert %s", + cert.c_str(), key.c_str(), key_pwd.c_str(), ca_cert.c_str()); + m_k8s_ssl = std::make_shared(cert, key, key_pwd, + ca_cert, ca_cert.empty() ? false : true, "PEM"); + } +#endif // HAS_CAPTURE } -#ifdef HAS_FILTERING -void sinsp::get_filtercheck_fields_info(OUT vector* list) +void sinsp::make_k8s_client() { - sinsp_utils::get_filtercheck_fields_info(list); -} + bool is_live = m_k8s_api_server && !m_k8s_api_server->empty(); + m_k8s_client = new k8s(m_k8s_api_server ? *m_k8s_api_server : std::string() + ,is_live // capture +#ifdef HAS_CAPTURE + ,m_k8s_ssl + ,m_k8s_bt + ,true // blocking +#endif // HAS_CAPTURE + ,nullptr +#ifdef HAS_CAPTURE + ,m_ext_list_ptr #else -void sinsp::get_filtercheck_fields_info(OUT vector* list) -{ + ,nullptr +#endif // HAS_CAPTURE + ); } -#endif -uint32_t sinsp::reserve_thread_memory(uint32_t size) +void sinsp::init_k8s_client(string* api_server, string* ssl_cert, bool verbose) { - if(m_h != NULL) + ASSERT(api_server); + m_verbose_json = verbose; + m_k8s_api_server = api_server; + m_k8s_api_cert = ssl_cert; + + +#ifdef HAS_CAPTURE + if(m_k8s_api_detected && m_k8s_ext_detect_done) +#endif // HAS_CAPTURE { - throw sinsp_exception("reserve_thread_memory can't be called after capture starts"); + if(m_k8s_client) + { + delete m_k8s_client; + m_k8s_client = nullptr; + } + init_k8s_ssl(ssl_cert); + make_k8s_client(); } +} - return m_thread_privatestate_manager.reserve(size); +void sinsp::collect_k8s() +{ + if(m_parser) + { + if(m_k8s_api_server) + { + if(!m_k8s_client) + { + init_k8s_client(m_k8s_api_server, m_k8s_api_cert, m_verbose_json); + if(m_k8s_client) + { + g_logger.log("K8s client created.", sinsp_logger::SEV_DEBUG); + } + else + { + g_logger.log("K8s client NOT created.", sinsp_logger::SEV_DEBUG); + } + } + if(m_k8s_client) + { + if(m_lastevent_ts > m_k8s_last_watch_time_ns + ONE_SECOND_IN_NS) + { + m_k8s_last_watch_time_ns = m_lastevent_ts; + g_logger.log("K8s updating state ...", sinsp_logger::SEV_DEBUG); + uint64_t delta = sinsp_utils::get_current_time_ns(); + m_k8s_client->watch(); + m_parser->schedule_k8s_events(); + delta = sinsp_utils::get_current_time_ns() - delta; + g_logger.format(sinsp_logger::SEV_DEBUG, "Updating Kubernetes state took %" PRIu64 " ms", delta / 1000000LL); + } + } + } + } } -void sinsp::get_capture_stats(scap_stats* stats) +void sinsp::k8s_discover_ext() { - if(scap_get_stats(m_h, stats) != SCAP_SUCCESS) +#ifdef HAS_CAPTURE + try { - throw sinsp_exception(scap_getlasterr(m_h)); + if(m_k8s_api_server && !m_k8s_api_server->empty() && !m_k8s_ext_detect_done) + { + g_logger.log("K8s API extensions handler: detecting extensions.", sinsp_logger::SEV_TRACE); + if(!m_k8s_ext_handler) + { + if(!m_k8s_collector) + { + m_k8s_collector = std::make_shared(); + } + if(uri(*m_k8s_api_server).is_secure()) { init_k8s_ssl(m_k8s_api_cert); } + m_k8s_ext_handler.reset(new k8s_api_handler(m_k8s_collector, *m_k8s_api_server, + "/apis/extensions/v1beta1", "[.resources[].name]", + "1.1", m_k8s_ssl, m_k8s_bt, true)); + g_logger.log("K8s API extensions handler: collector created.", sinsp_logger::SEV_TRACE); + } + else + { + g_logger.log("K8s API extensions handler: collecting data.", sinsp_logger::SEV_TRACE); + m_k8s_ext_handler->collect_data(); + if(m_k8s_ext_handler->ready()) + { + g_logger.log("K8s API extensions handler: data received.", sinsp_logger::SEV_TRACE); + if(m_k8s_ext_handler->error()) + { + g_logger.log("K8s API extensions handler: data error occurred while detecting API extensions.", + sinsp_logger::SEV_WARNING); + m_ext_list_ptr.reset(); + } + else + { + const k8s_api_handler::api_list_t& exts = m_k8s_ext_handler->extensions(); + std::ostringstream ostr; + k8s_ext_list_t ext_list; + for(const auto& ext : exts) + { + ext_list.insert(ext); + ostr << std::endl << ext; + } + g_logger.log("K8s API extensions handler extensions found: " + ostr.str(), + sinsp_logger::SEV_DEBUG); + m_ext_list_ptr.reset(new k8s_ext_list_t(ext_list)); + } + m_k8s_ext_detect_done = true; + m_k8s_collector.reset(); + m_k8s_ext_handler.reset(); + } + else + { + g_logger.log("K8s API extensions handler: not ready.", sinsp_logger::SEV_TRACE); + } + } + } + } + catch(const std::exception& ex) + { + g_logger.log(std::string("K8s API extensions handler error: ").append(ex.what()), + sinsp_logger::SEV_ERROR); + m_k8s_ext_detect_done = false; + m_k8s_collector.reset(); + m_k8s_ext_handler.reset(); } + g_logger.log("K8s API extensions handler: detection done.", sinsp_logger::SEV_TRACE); +#endif // HAS_CAPTURE } -#ifdef GATHER_INTERNAL_STATS -sinsp_stats sinsp::get_stats() +void sinsp::update_k8s_state() { - scap_stats stats; +#ifdef HAS_CAPTURE + try + { + if(m_k8s_api_server && !m_k8s_api_server->empty()) + { + if(!m_k8s_api_detected) + { + if(!m_k8s_api_handler) + { + if(!m_k8s_collector) + { + m_k8s_collector = std::make_shared(); + } + if(uri(*m_k8s_api_server).is_secure() && (!m_k8s_ssl || ! m_k8s_bt)) + { + init_k8s_ssl(m_k8s_api_cert); + } + m_k8s_api_handler.reset(new k8s_api_handler(m_k8s_collector, *m_k8s_api_server, + "/api", ".versions", "1.1", + m_k8s_ssl, m_k8s_bt, true)); + } + else + { + m_k8s_api_handler->collect_data(); + if(m_k8s_api_handler->ready()) + { + g_logger.log("K8s API handler data received.", sinsp_logger::SEV_DEBUG); + if(m_k8s_api_handler->error()) + { + g_logger.log("K8s API handler data error occurred while detecting API versions.", + sinsp_logger::SEV_ERROR); + } + else + { + m_k8s_api_detected = m_k8s_api_handler->has("v1"); + if(m_k8s_api_detected) + { + g_logger.log("K8s API server v1 detected.", sinsp_logger::SEV_DEBUG); + } + } + m_k8s_collector.reset(); + m_k8s_api_handler.reset(); + } + else + { + g_logger.log("K8s API handler not ready yet.", sinsp_logger::SEV_DEBUG); + } + } + } + if(m_k8s_api_detected && !m_k8s_ext_detect_done) + { + k8s_discover_ext(); + } + if(m_k8s_api_detected && m_k8s_ext_detect_done) + { + collect_k8s(); + } + } + } + catch(const std::exception& e) + { + g_logger.log(std::string("Error fetching K8s data: ").append(e.what()), sinsp_logger::SEV_ERROR); + throw; + } +#endif // HAS_CAPTURE +} - // - // Get capture stats from scap - // - if(m_h) +bool sinsp::get_mesos_data() +{ + bool ret = false; +#ifdef HAS_CAPTURE + try { - scap_get_stats(m_h, &stats); + static time_t last_mesos_refresh = 0; + ASSERT(m_mesos_client); + ASSERT(m_mesos_client->is_alive()); - m_stats.m_n_seen_evts = stats.n_evts; - m_stats.m_n_drops = stats.n_drops; - m_stats.m_n_preemptions = stats.n_preemptions; + time_t now; time(&now); + if(last_mesos_refresh) + { + g_logger.log("Collecting Mesos data ...", sinsp_logger::SEV_DEBUG); + ret = m_mesos_client->collect_data(); + } + if(difftime(now, last_mesos_refresh) > 10) + { + g_logger.log("Requesting Mesos data ...", sinsp_logger::SEV_DEBUG); + m_mesos_client->send_data_request(false); + last_mesos_refresh = now; + } } - else + catch(const std::exception& ex) { - m_stats.m_n_seen_evts = 0; - m_stats.m_n_drops = 0; - m_stats.m_n_preemptions = 0; + g_logger.log(std::string("Mesos exception: ") + ex.what(), sinsp_logger::SEV_ERROR); + delete m_mesos_client; + m_mesos_client = NULL; + init_mesos_client(0, m_verbose_json); } +#endif // HAS_CAPTURE + return ret; +} - // - // Count the number of threads and fds by scanning the tables, - // and update the thread-related stats. - // - if(m_thread_manager) +void sinsp::update_mesos_state() +{ + ASSERT(m_mesos_client); + if(m_lastevent_ts > m_mesos_last_watch_time_ns + ONE_SECOND_IN_NS) { - m_thread_manager->update_statistics(); + m_mesos_last_watch_time_ns = m_lastevent_ts; + if(m_mesos_client->is_alive()) + { + uint64_t delta = sinsp_utils::get_current_time_ns(); + if(m_parser && get_mesos_data()) + { + m_parser->schedule_mesos_events(); + delta = sinsp_utils::get_current_time_ns() - delta; + g_logger.format(sinsp_logger::SEV_DEBUG, "Updating Mesos state took %" PRIu64 " ms", delta / 1000000LL); + } + } + else + { + g_logger.format(sinsp_logger::SEV_ERROR, "Mesos connection not active anymore, retrying ..."); + delete m_mesos_client; + m_mesos_client = NULL; + init_mesos_client(0, m_verbose_json); + } } - - // - // Return the result - // - - return m_stats; } -#endif // GATHER_INTERNAL_STATS +#endif // CYGWING_AGENT -void sinsp::set_log_callback(sinsp_logger_callback cb) +void sinsp::set_bpf_probe(const string& bpf_probe) { - g_logger.add_callback_log(cb); + m_bpf = true; + m_bpf_probe = bpf_probe; } -sinsp_evttables* sinsp::get_event_info_tables() +bool sinsp::is_bpf_enabled() { - return &g_infotables; + // At the inspector level, bpf can be explicitly enabled via + // sinsp::set_bpf_probe, but what's most important is whether + // it's enabled at the libscap level, which can also be done + // via the environment. + if(m_h) + { + return scap_get_bpf_enabled(m_h); + } + + return false; } -void sinsp::add_chisel_dir(string dirname) +/////////////////////////////////////////////////////////////////////////////// +// Note: this is defined here so we can inline it in sinso::next +/////////////////////////////////////////////////////////////////////////////// +bool sinsp_thread_manager::remove_inactive_threads() { - if(dirname[dirname.size() -1] != '/') + bool res = false; + + if(m_last_flush_time_ns == 0) { - dirname += "/"; + // + // Set the first table scan for 30 seconds in, so that we can spot bugs in the logic without having + // to wait for tens of minutes + // + if(m_inspector->m_inactive_thread_scan_time_ns > 30 * ONE_SECOND_IN_NS) + { + m_last_flush_time_ns = + (m_inspector->m_lastevent_ts - m_inspector->m_inactive_thread_scan_time_ns + 30 * ONE_SECOND_IN_NS); + } + else + { + m_last_flush_time_ns = + (m_inspector->m_lastevent_ts - m_inspector->m_inactive_thread_scan_time_ns); + } } - chiseldir_info ncdi; + if(m_inspector->m_lastevent_ts > + m_last_flush_time_ns + m_inspector->m_inactive_thread_scan_time_ns) + { + std::unordered_map to_delete; - strcpy(ncdi.m_dir, dirname.c_str()); - ncdi.m_need_to_resolve = false; + res = true; - g_chisel_dirs->push_back(ncdi); -} + m_last_flush_time_ns = m_inspector->m_lastevent_ts; -void sinsp::set_buffer_format(sinsp_evt::param_fmt format) -{ - m_buffer_format = format; + g_logger.format(sinsp_logger::SEV_INFO, "Flushing thread table"); + + // + // Go through the table and remove dead entries. + // + m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + bool closed = (tinfo.m_flags & PPM_CL_CLOSED) != 0; + + if(closed || + ((m_inspector->m_lastevent_ts > tinfo.m_lastaccess_ts + m_inspector->m_thread_timeout_ns) && + !scap_is_thread_alive(m_inspector->m_h, tinfo.m_pid, tinfo.m_tid, tinfo.m_comm.c_str())) + ) + { + // + // Reset the cache + // + m_last_tid = 0; + m_last_tinfo.reset(); + +#ifdef GATHER_INTERNAL_STATS + m_removed_threads->increment(); +#endif + to_delete[tinfo.m_tid] = closed; + } + return true; + }); + + for (auto& it : to_delete) + { + remove_thread(it.first, it.second); + } + + // + // Rebalance the thread table dependency tree, so we free up threads that + // exited but that are stuck because of reference counting. + // + recreate_child_dependencies(); + } + + return res; } -sinsp_evt::param_fmt sinsp::get_buffer_format() +#ifdef HAS_CAPTURE +std::shared_ptr sinsp::lookup_cgroup_dir(const string& subsys) { - return m_buffer_format; + shared_ptr cgroup_dir; + static std::unordered_map> cgroup_dir_cache; + + const auto& it = cgroup_dir_cache.find(subsys); + if(it != cgroup_dir_cache.end()) + { + return it->second; + } + + // Look for mount point of cgroup filesystem + // It should be already mounted on the host or by + // our docker-entrypoint.sh script + if(strcmp(scap_get_host_root(), "") != 0) + { + // We are inside our container, so we should use the directory + // mounted by it + auto cgroup = std::string(scap_get_host_root()) + "/cgroup/" + subsys; + cgroup_dir = std::make_shared(cgroup); + } + else + { + struct mntent mntent_buf = {}; + char mntent_string_buf[4096]; + FILE* fp = setmntent("/proc/mounts", "r"); + struct mntent* entry = getmntent_r(fp, &mntent_buf, + mntent_string_buf, sizeof(mntent_string_buf)); + while(entry != nullptr) + { + if(strcmp(entry->mnt_type, "cgroup") == 0 && + hasmntopt(entry, subsys.c_str()) != NULL) + { + cgroup_dir = std::make_shared(entry->mnt_dir); + break; + } + entry = getmntent(fp); + } + endmntent(fp); + } + if(!cgroup_dir) + { + return std::make_shared(); + } + else + { + cgroup_dir_cache[subsys] = cgroup_dir; + return cgroup_dir; + } } +#endif -bool sinsp::is_live() +sinsp_threadinfo* +libsinsp::event_processor::build_threadinfo(sinsp* inspector) { - return m_islive; -} \ No newline at end of file + return new sinsp_threadinfo(inspector); +} diff --git a/userspace/libsinsp/sinsp.h b/userspace/libsinsp/sinsp.h index fee5d1e896..f365f6d0e6 100644 --- a/userspace/libsinsp/sinsp.h +++ b/userspace/libsinsp/sinsp.h @@ -1,28 +1,29 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2019 Sysdig Inc. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ /*! \mainpage libsinsp documentation - + \section Introduction libsinsp is a system inspection library written in C++ and implementing high level - functionlity like: + functionality like: - live capture control (start/stop/pause...) - event capture from file or the live OS - OS state reconstruction. By parsing /proc and inspecting the live event stream, @@ -31,7 +32,7 @@ along with sysdig. If not, see . can be treated like programs, files, connections and users. - parsing of OS events and conversion of events into human-readable strings - event filtering - + This manual includes the following sections: - \ref inspector - \ref event @@ -41,18 +42,20 @@ along with sysdig. If not, see . */ #pragma once -#ifdef _WIN32 -#pragma warning(disable: 4251) -#endif + +#include "capture_stats_source.h" +#include "container_engine/wmi_handle_source.h" #ifdef _WIN32 -#define SINSP_PUBLIC __declspec(dllexport) -#include +#pragma warning(disable: 4251 4200 4221 4190) #else -#define SINSP_PUBLIC -#include +#include "tbb/concurrent_queue.h" #endif +#include "sinsp_inet.h" +#include "sinsp_public.h" +#include "sinsp_exception.h" + #define __STDC_FORMAT_MACROS #include @@ -61,6 +64,8 @@ along with sysdig. If not, see . #include #include #include +#include +#include using namespace std; @@ -72,10 +77,16 @@ using namespace std; #include "dumper.h" #include "stats.h" #include "ifinfo.h" -#include "chisel.h" +#include "container.h" +#include "viewinfo.h" +#include "utils.h" #ifndef VISIBILITY_PRIVATE +// Some code defines VISIBILITY_PRIVATE to nothing to get private access to sinsp #define VISIBILITY_PRIVATE private: +#define VISIBILITY_PROTECTED protected: +#else +#define VISIBILITY_PROTECTED #endif #define ONE_SECOND_IN_NS 1000000000LL @@ -85,54 +96,85 @@ using namespace std; #include "threadinfo.h" #include "ifinfo.h" #include "eventformatter.h" +#include "sinsp_pd_callback_type.h" +#include "include/sinsp_external_processor.h" class sinsp_partial_transaction; class sinsp_parser; class sinsp_analyzer; class sinsp_filter; +class cycle_writer; +class sinsp_protodecoder; +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) +class k8s; +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) +class sinsp_partial_tracer; +class mesos; + +#ifdef HAS_CAPTURE +class sinsp_ssl; +class sinsp_bearer_token; +template class socket_data_handler; +template class socket_collector; +class k8s_handler; +class k8s_api_handler; +#endif // HAS_CAPTURE + +std::vector sinsp_split(const std::string &s, char delim); /*! - \brief Information about a group of filter/formatting fields. + \brief Information about a chisel */ -class filter_check_info +class sinsp_chisel_details { public: - string m_name; ///< Field class name. - int32_t m_nfiedls; ///< Number of fields in this field group. - const filtercheck_field_info* m_fields; ///< Array containing m_nfiedls field descriptions. + std::string m_name; + std::vector> m_args; }; /*! - \brief sinsp library exception. + \brief Information about a group of filter/formatting fields. */ -struct sinsp_exception : std::exception +class filter_check_info { - sinsp_exception() - { - } - - ~sinsp_exception() throw() +public: + enum flags { - } + FL_NONE = 0, + FL_WORKS_ON_THREAD_TABLE = (1 << 0), ///< This filter check class supports filtering incomplete events that contain only valid thread info and FD info. + FL_HIDDEN = (1 << 1), ///< This filter check class won't be shown by stuff like the -l sysdig command line switch. + }; - sinsp_exception(string error_str) + filter_check_info() { - m_error_str = error_str; + m_flags = 0; } - char const* what() const throw() - { - return m_error_str.c_str(); - } - - string m_error_str; + string m_name; ///< Field class name. + int32_t m_nfields; ///< Number of fields in this field group. + const filtercheck_field_info* m_fields; ///< Array containing m_nfields field descriptions. + uint32_t m_flags; }; /*! - \brief The deafult way an event is converted to string by the library + \brief The default way an event is converted to string by the library */ -//#define DEFAULT_OUTPUT_STR "*%evt.num %evt.time %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.args" -#define DEFAULT_OUTPUT_STR "*%evt.time %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.args" +#define DEFAULT_OUTPUT_STR "*%evt.num %evt.time %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.args" + +// +// Internal stuff for meta event management +// +typedef void (*meta_event_callback)(sinsp*, void* data); +class sinsp_proc_metainfo +{ +public: + sinsp_evt m_pievt; + scap_evt* m_piscapevt; + uint64_t* m_piscapevt_vals; + uint64_t m_n_procinfo_evts; + int64_t m_cur_procinfo_evt; + ppm_proclist_info* m_pli; +}; /** @defgroup inspector Main library @{ @@ -146,32 +188,49 @@ struct sinsp_exception : std::exception - event retrieval - setting capture filters */ -class SINSP_PUBLIC sinsp +class SINSP_PUBLIC sinsp : public capture_stats_source, public wmi_handle_source { public: + typedef std::shared_ptr ptr; + typedef std::set k8s_ext_list_t; + typedef std::shared_ptr k8s_ext_list_ptr_t; + sinsp(); - ~sinsp(); + virtual ~sinsp(); /*! \brief Start a live event capture. - \param timeout_ms the optional read timeout, i.e. the time after which a + \param timeout_ms the optional read timeout, i.e. the time after which a call to \ref next() returns even if no events are available. - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case of failure. */ - void open(uint32_t timeout_ms = SCAP_TIMEOUT_MS); + virtual void open(uint32_t timeout_ms = SCAP_TIMEOUT_MS); /*! \brief Start an event capture from a trace file. \param filename the trace file name. - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case + of failure. + */ + void open(const std::string &filename); + + /*! + \brief Start an event capture from a file descriptor. + + \param fd the file descriptor + + @throws a sinsp_exception containing the error string is thrown in case of failure. */ - void open(string filename); + void fdopen(int fd); + + void open_udig(uint32_t timeout_ms = SCAP_TIMEOUT_MS); + void open_nodriver(); /*! \brief Ends a capture and release all resources. @@ -181,47 +240,70 @@ class SINSP_PUBLIC sinsp /*! \brief Get the next event from the open capture source - \param evt a \ref sinsp_evt pointer that will be initialized to point to + \param evt a \ref sinsp_evt pointer that will be initialized to point to the next available event. - \return SCAP_SUCCESS if the call is succesful and pevent and pcpuid contain - valid data. SCAP_TIMEOUT in case the read timeout expired and no event is + \return SCAP_SUCCESS if the call is successful and pevent and pcpuid contain + valid data. SCAP_TIMEOUT in case the read timeout expired and no event is available. SCAP_EOF when the end of an offline capture is reached. - On Failure, SCAP_FAILURE is returned and getlasterr() can be used to - obtain the cause of the error. + On Failure, SCAP_FAILURE is returned and getlasterr() can be used to + obtain the cause of the error. \note: the returned event can be considered valid only until the next - call to \ref next() + call to \ref) */ - int32_t next(OUT sinsp_evt** evt); + virtual int32_t next(OUT sinsp_evt **evt); + + /*! + \brief Get the maximum number of bytes currently in use by any CPU buffer + */ + uint64_t max_buf_used(); /*! \brief Get the number of events that have been captured and processed since the call to \ref open() - \return the number of captured events. + \return the number of captured events. */ uint64_t get_num_events(); /*! - \brief Set the capture snaplen, i.e. the maximum size an event + \brief Set the capture snaplen, i.e. the maximum size an event parameter can reach before the driver starts truncating it. \param snaplen the snaplen for this capture instance, in bytes. \note This function can only be called for live captures. - \note By default, the driver captures the first 80 bytes of the - buffers coming from events like read, write, send, recv, etc. - If you're not interested in payloads, smaller values will save + \note By default, the driver captures the first 80 bytes of the + buffers coming from events like read, write, send, recv, etc. + If you're not interested in payloads, smaller values will save capture buffer space and make capture files smaller. - Conversely, big values should be used with care because they can + Conversely, big values should be used with care because they can easily generate huge capture files. - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case of failure. */ void set_snaplen(uint32_t snaplen); + /*! + \brief Determine if this inspector is going to load user tables on + startup. + + \param import_users if true, no user tables will be created for + this capture. This also means that no user or group info will be + written to the trace file by the -w flag. The user/group tables are + necessary to use filter fields like user.name or group.name. However, + creating them can increase sysdig's startup time. Moreover, they contain + information that could be privacy sensitive. + + \note default behavior is import_users=true. + + @throws a sinsp_exception containing the error string is thrown in case + of failure. + */ + void set_import_users(bool import_users); + /*! \brief temporarily pauses event capture. @@ -230,13 +312,13 @@ class SINSP_PUBLIC sinsp void stop_capture(); /*! - \brief Restarts an event capture that had been paused with + \brief Restarts an event capture that had been paused with \ref stop_capture(). \note This function can only be called for live captures. */ void start_capture(); - + #ifdef HAS_FILTERING /*! \brief Compiles and installs the given capture filter. @@ -245,10 +327,33 @@ class SINSP_PUBLIC sinsp section in the sysdig website for information about the filtering syntax. - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case the filter is invalid. */ - void set_filter(string filter); + void set_filter(const string& filter); + + /*! + \brief Installs the given capture runtime filter object. + + \param filter the runtime filter object + */ + void set_filter(sinsp_filter* filter); + + /*! + \brief Return the filter set for this capture. + + \return the filter previously set with \ref set_filter(), or an empty + string if no filter has been set yet. + */ + const string get_filter(); + + void add_evttype_filter(std::string &name, + std::set &evttypes, + std::set &syscalls, + std::set &tags, + sinsp_filter* filter); + + bool run_filters_on_evt(sinsp_evt *evt); #endif /*! @@ -259,11 +364,29 @@ class SINSP_PUBLIC sinsp */ void set_log_callback(sinsp_logger_callback cb); + /*! + \brief Instruct sinsp to write its log messages to the given file. + */ + void set_log_file(string filename); + + /*! + \brief Instruct sinsp to write its log messages to stderr. + */ + void set_log_stderr(); + + /*! + \brief Specify the minimum severity of the messages that go into the logs + emitted by the library. + */ + void set_min_log_severity(sinsp_logger::severity sev); + /*! \brief Start writing the captured events to file. \param dump_filename the destination trace file. + \param compress true to save the trace file in a compressed format. + \note only the events that pass the capture filter set with \ref set_filter() will be saved to disk. \note this simplified dump interface allows only one dump per capture. @@ -271,15 +394,20 @@ class SINSP_PUBLIC sinsp also be combined with \ref sinsp_filter to filter what will go into the file. - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case of failure. */ - void autodump_start(const string dump_filename); + void autodump_start(const string& dump_filename, bool compress); + + /*! + \brief Cycles the file pointer to a new capture file + */ + void autodump_next_file(); /*! \brief Stops an event dump that was started with \ref autodump_start(). - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case of failure. */ void autodump_stop(); @@ -288,7 +416,7 @@ class SINSP_PUBLIC sinsp \brief Populate the given vector with the full list of filter check fields that this version of the library supports. */ - static void get_filtercheck_fields_info(vector* list); + static void get_filtercheck_fields_info(std::vector* list); bool has_metrics(); @@ -296,7 +424,7 @@ class SINSP_PUBLIC sinsp \brief Return information about the machine generating the events. \note this call works with file captures as well, because the machine - info is stored in the trace files. In that case, the returned + info is stored in the trace files. In that case, the returned machine info is the one of the machine where the capture happened. */ const scap_machine_info* get_machine_info(); @@ -310,10 +438,10 @@ class SINSP_PUBLIC sinsp \return the \ref sinsp_threadinfo object containing full thread information and state. - \note if you are interested in a process' information, just give this + \note if you are interested in a process' information, just give this function with the PID of the process. - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case of failure. */ sinsp_threadinfo* get_thread(int64_t tid); @@ -331,13 +459,14 @@ class SINSP_PUBLIC sinsp \return the \ref sinsp_threadinfo object containing full thread information and state. - \note if you are interested in a process' information, just give this + \note if you are interested in a process' information, just give this function with the PID of the process. - @throws a sinsp_exception containing the error string is thrown in case + @throws a sinsp_exception containing the error string is thrown in case of failure. */ - sinsp_threadinfo* get_thread(int64_t tid, bool query_os_if_not_found); + sinsp_threadinfo* get_thread(int64_t tid, bool query_os_if_not_found, bool lookup_only); + threadinfo_map_t::ptr_t get_thread_ref(int64_t tid, bool query_os_if_not_found, bool lookup_only, bool main_thread=false); /*! \brief Return the table with all the machine users. @@ -346,11 +475,23 @@ class SINSP_PUBLIC sinsp information as the data. \note this call works with file captures as well, because the user - table is stored in the trace files. In that case, the returned + table is stored in the trace files. In that case, the returned user list is the one of the machine where the capture happened. */ const unordered_map* get_userlist(); + /*! + \brief Lookup for user in the user table. + + \return the \ref scap_userinfo object containing full user information, + if user not found, returns NULL. + + \note this call works with file captures as well, because the user + table is stored in the trace files. In that case, the returned + user list is the one of the machine where the capture happened. + */ + scap_userinfo* get_user(uint32_t uid); + /*! \brief Return the table with all the machine user groups. @@ -358,7 +499,7 @@ class SINSP_PUBLIC sinsp information as the data. \note this call works with file captures as well, because the group - table is stored in the trace files. In that case, the returned + table is stored in the trace files. In that case, the returned user table is the one of the machine where the capture happened. */ const unordered_map* get_grouplist(); @@ -369,16 +510,36 @@ class SINSP_PUBLIC sinsp \note this call won't work on file captures. */ - void get_capture_stats(scap_stats* stats); + void get_capture_stats(scap_stats* stats) const override; + void set_max_thread_table_size(uint32_t value); #ifdef GATHER_INTERNAL_STATS sinsp_stats get_stats(); #endif -#ifdef HAS_ANALYZER - sinsp_analyzer* m_analyzer; -#endif + libsinsp::event_processor* m_external_event_processor; + + sinsp_threadinfo* build_threadinfo() + { + return m_external_event_processor ? m_external_event_processor->build_threadinfo(this) + : new sinsp_threadinfo(this); + } + + /*! + \brief registers external event processor. + After this, callbacks on libsinsp::event_processor will happen at + the appropriate times. This registration must happen before calling open. + */ + void register_external_event_processor(libsinsp::event_processor& processor) + { + m_external_event_processor = &processor; + } + + libsinsp::event_processor* get_external_event_processor() const + { + return m_external_event_processor; + } /*! \brief Return the event and system call information tables. @@ -399,40 +560,263 @@ class SINSP_PUBLIC sinsp /*! \brief Add a new directory containing chisels. + \parame front_add if true, the chisel directory is added at the front of + the search list and therefore gets priority. + \note This function is not reentrant. */ - void add_chisel_dir(string dirname); + void add_chisel_dir(string dirname, bool front_add); /*! \brief Get the list of machine network interfaces. - \return Pointer to the iterface list manager. + \return Pointer to the interface list manager. */ sinsp_network_interfaces* get_ifaddr_list(); /*! - \brief Set the format used to render event data + \brief Set the format used to render event data buffer arguments. */ void set_buffer_format(sinsp_evt::param_fmt format); /*! - \brief Get the format used to render event data + \brief Get the format used to render event data buffer arguments. */ sinsp_evt::param_fmt get_buffer_format(); /*! - \brief Returns true if the current capture is live. + \brief Set event flags for which matching events should be dropped pre-filtering + */ + void set_drop_event_flags(ppm_event_flags flags); + + /*! + \brief Returns true if the current capture is offline + */ + inline bool is_capture() + { + return m_mode == SCAP_MODE_CAPTURE; + } + + /*! + \brief Returns true if the current capture is live + */ + inline bool is_live() + { + return m_mode == SCAP_MODE_LIVE; + } + + /*! + \brief Returns true if the sysdig module is not loaded + */ + inline bool is_nodriver() + { + return m_mode == SCAP_MODE_NODRIVER; + } + + /*! + \brief Returns true if truncated environments should be loaded from /proc + */ + inline bool large_envs_enabled() + { + return is_live() && m_large_envs_enabled; + } + + /*! + \brief Enable/disable large environment support + + \param enable when it is true and the current capture is live + environments larger than SCAP_MAX_ENV_SIZE will be loaded + from /proc//environ (if possible) + */ + void set_large_envs(bool enable); + + /*! + \brief Set the debugging mode of the inspector. + + \param enable_debug when it is true and the current capture is live + the inspector filters out events about sysdig itself. + */ + void set_debug_mode(bool enable_debug); + + /*! + \brief Set the fatfile mode when writing events to file. + + \note fatfile mode involves saving "hidden" events in the trace file + that make it possible to preserve full state even when filters that + would drop state packets are used during the capture. + */ + void set_fatfile_dump_mode(bool enable_fatfile); + + /*! + \brief Set internal events mode. + + \note By default, internal events, such as events that note + when new containers or orchestration entities have + been created, are not returned in sinsp::next(). (They + are always written to capture files, to ensure that + the full state can be reconstructed when capture files + are read). Enabling internal events mode will result + in these events being returned. + */ + void set_internal_events_mode(bool enable_internal_events); + + /*! + \brief Set whether Sysdig should resolve hostnames and port protocols or not. + + \note Sysdig can use the system library functions getservbyport and so to + resolve protocol names and domain names. + + \param enable If set to false it will enable this function and use plain + numerical values. + */ + void set_hostname_and_port_resolution_mode(bool enable); + + /*! + \brief Set the runtime flag for resolving the timespan in a human + readable mode. + + \note Moved to the inspector due to sysdig#426 issue + + \param flag Can be 'h', 'a', 'r', 'd', 'D' as documented in the manual. + */ + inline void set_time_output_mode(char flag) + { + m_output_time_flag = flag; + } + + /*! + \brief Sets the max length of event argument strings. + + \param len Max length after which an event argument string is truncated. + 0 means no limit. Use this to reduce verbosity when printing event info + on screen. + */ + void set_max_evt_output_len(uint32_t len); + + /*! + \brief Returns true if the debug mode is enabled. + */ + inline bool is_debug_enabled() + { + return m_isdebug_enabled; + } + + /*! + \brief Set a flag indicating if the command line requested to show container information. + + \param set true if the command line argument is set to show container information + */ + void set_print_container_data(bool print_container_data); + + + /*! + \brief Returns true if the command line argument is set to show container information. + */ + inline bool is_print_container_data() + { + return m_print_container_data; + } + + /*! + \brief Lets a filter plugin request a protocol decoder. + + \param the name of the required decoder + */ + sinsp_protodecoder* require_protodecoder(std::string decoder_name); + + /*! + \brief Lets a filter plugin request a protocol decoder. + + \param the name of the required decoder + */ + void protodecoder_register_reset(sinsp_protodecoder* dec); + + /*! + \brief If this is an offline capture, return the name of the file that is + being read, otherwise return an empty string. + */ + std::string get_input_filename() + { + return m_input_filename; + } + + /*! + \brief If this is an online capture, set event_id. + \param event type to set + \return SCAP_SUCCESS if the call is successful + On Failure, SCAP_FAILURE is returned and getlasterr() can be used to + obtain the cause of the error. + + \note For a list of event types, refer to \ref etypes. + */ + void set_eventmask(uint32_t event_types); + + /*! + \brief If this is an online capture, unset event_id. + \param event type to unset + \return SCAP_SUCCESS if the call is successful + On Failure, SCAP_FAILURE is returned and getlasterr() can be used to + obtain the cause of the error. + + \note For a list of event types, refer to \ref etypes. + */ + void unset_eventmask(uint32_t event_id); + + /*! + \brief When reading events from a trace file, this function returns the + read progress as a number between 0 and 100. */ - bool is_live(); + double get_read_progress(); + + /*! + \brief Make the amount of data gathered for a syscall to be + determined by the number of parameters. + */ + virtual int /*SCAP_X*/ dynamic_snaplen(bool enable) + { + if(enable) + { + return scap_enable_dynamic_snaplen(m_h); + } + else + { + return scap_disable_dynamic_snaplen(m_h); + } + } + +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + void init_k8s_ssl(const std::string *ssl_cert); + void init_k8s_client(std::string* api_server, std::string* ssl_cert, bool verbose = false); + void make_k8s_client(); + k8s* get_k8s_client() const { return m_k8s_client; } + + void init_mesos_client(std::string* api_server, bool verbose = false); + mesos* get_mesos_client() const { return m_mesos_client; } +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) // - // Misc internal stuff + // Misc internal stuff // void stop_dropping_mode(); void start_dropping_mode(uint32_t sampling_ratio); - void import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo); + void on_new_entry_from_proc(void* context, scap_t* handle, int64_t tid, scap_threadinfo* tinfo, + scap_fdinfo* fdinfo); + void set_get_procs_cpu_from_driver(bool get_procs_cpu_from_driver) + { + m_get_procs_cpu_from_driver = get_procs_cpu_from_driver; + } + + // + // Used by filters to enable app event state tracking, which is disabled + // by default for performance reasons + // + void request_tracer_state_tracking() + { + m_track_tracers_state = true; + } + // // Allocates private state in the thread info class. // Returns the ID to use when retrieving the memory area. @@ -440,45 +824,279 @@ class SINSP_PUBLIC sinsp // uint32_t reserve_thread_memory(uint32_t size); + sinsp_parser* get_parser(); + + bool setup_cycle_writer(std::string base_file_name, int rollover_mb, int duration_seconds, int file_limit, unsigned long event_limit, bool compress); + void import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo); + void add_meta_event(sinsp_evt *metaevt); + void add_meta_event_callback(meta_event_callback cback, void* data); + void remove_meta_event_callback(); + void filter_proc_table_when_saving(bool filter); + void enable_tracers_capture(); + void enable_page_faults(); + uint64_t get_bytes_read() + { + return scap_ftell(m_h); + } + void refresh_ifaddr_list(); + void refresh_proc_list() { + scap_refresh_proc_table(m_h); + } + void set_simpledriver_mode(); + std::vector get_n_tracepoint_hit(); + void set_bpf_probe(const std::string& bpf_probe); + + bool is_bpf_enabled(); + + static unsigned num_possible_cpus(); + +#ifdef HAS_CAPTURE + static std::shared_ptr lookup_cgroup_dir(const std::string& subsys); +#endif +#ifdef CYGWING_AGENT + wh_t* get_wmi_handle() override + { + return scap_get_wmi_handle(m_h); + } +#endif + + static inline bool simple_consumer_consider_evtnum(uint16_t etype) + { + enum ppm_event_flags flags = g_infotables.m_event_info[etype].flags; + + return ! (flags & sinsp::simple_consumer_skip_flags()); + } + + static inline bool simple_consumer_consider_syscallid(uint16_t scid) + { + enum ppm_event_flags flags = g_infotables.m_syscall_info_table[scid].flags; + + return ! (flags & sinsp::simple_consumer_skip_flags()); + } + + // Add comm to the list of comms for which the inspector + // should not return events. + bool suppress_events_comm(const std::string &comm); + + bool check_suppressed(int64_t tid); + + void set_docker_socket_path(std::string socket_path); + void set_query_docker_image_info(bool query_image_info); + + void set_cri_extra_queries(bool extra_queries); + + void set_fullcapture_port_range(uint16_t range_start, uint16_t range_end); + + void set_statsd_port(uint16_t port); + + void set_cri_socket_path(const std::string& path); + void set_cri_timeout(int64_t timeout_ms); + void set_cri_async(bool async); + void set_cri_delay(uint64_t delay_ms); + +VISIBILITY_PROTECTED + bool add_thread(const sinsp_threadinfo *ptinfo); + void set_mode(scap_mode_t value) + { + m_mode = value; + } + VISIBILITY_PRIVATE + static inline ppm_event_flags simple_consumer_skip_flags() + { + return (ppm_event_flags) (EF_SKIPPARSERESET | EF_UNUSED | EF_DROP_SIMPLE_CONS); + } // Doxygen doesn't understand VISIBILITY_PRIVATE #ifdef _DOXYGEN private: #endif + void open_int(); + void open_live_common(uint32_t timeout_ms, scap_mode_t mode); void init(); void import_thread_table(); void import_ifaddr_list(); void import_user_list(); + void add_protodecoders(); + + void remove_thread(int64_t tid, bool force); + + // + // Note: lookup_only should be used when the query for the thread is made + // not as a consequence of an event for that thread arriving, but + // just for lookup reason. In that case, m_lastaccess_ts is not updated + // and m_last_tinfo is not set. + // + inline threadinfo_map_t::ptr_t find_thread(int64_t tid, bool lookup_only) + { + threadinfo_map_t::ptr_t thr; + // + // Try looking up in our simple cache + // + if(tid == m_thread_manager->m_last_tid) + { + thr = m_thread_manager->m_last_tinfo.lock(); + if (thr) + { + #ifdef GATHER_INTERNAL_STATS + m_thread_manager->m_cached_lookups->increment(); + #endif + thr->m_lastaccess_ts = m_lastevent_ts; + return thr; + } + } + + // + // Caching failed, do a real lookup + // + thr = m_thread_manager->m_threadtable.get_ref(tid); + + if(thr) + { + #ifdef GATHER_INTERNAL_STATS + m_thread_manager->m_non_cached_lookups->increment(); + #endif + if(!lookup_only) + { + m_thread_manager->m_last_tid = tid; + m_thread_manager->m_last_tinfo = thr; + thr->m_lastaccess_ts = m_lastevent_ts; + } + return thr; + } + else + { + #ifdef GATHER_INTERNAL_STATS + m_thread_manager->m_failed_lookups->increment(); + #endif + return NULL; + } + } + // this is here for testing purposes only + sinsp_threadinfo* find_thread_test(int64_t tid, bool lookup_only); + bool remove_inactive_threads(); + +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + void k8s_discover_ext(); + void collect_k8s(); + void update_k8s_state(); + void update_mesos_state(); + bool get_mesos_data(); +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + + static int64_t get_file_size(const std::string& fname, char *error); + static std::string get_error_desc(const std::string& msg = ""); + + void restart_capture_at_filepos(uint64_t filepos); + + void fseek(uint64_t filepos) + { + scap_fseek(m_h, filepos); + } + + void add_suppressed_comms(scap_open_args &oargs); - void add_thread(const sinsp_threadinfo& ptinfo); - void remove_thread(int64_t tid); + bool increased_snaplen_port_range_set() const + { + return m_increased_snaplen_port_range.range_start > 0 && + m_increased_snaplen_port_range.range_end > 0; + } scap_t* m_h; - bool m_islive; - string m_filename; + uint32_t m_nevts; + int64_t m_filesize; + + scap_mode_t m_mode = SCAP_MODE_NONE; + + // If non-zero, reading from this fd and m_input_filename contains "fd + // ". Otherwise, reading from m_input_filename. + int m_input_fd; + std::string m_input_filename; + bool m_bpf; + bool m_udig; + std::string m_bpf_probe; + bool m_isdebug_enabled; + bool m_isfatfile_enabled; + bool m_isinternal_events_enabled; + bool m_hostname_and_port_resolution_enabled; + char m_output_time_flag; + uint32_t m_max_evt_output_len; + bool m_compress; sinsp_evt m_evt; - string m_lasterr; + std::string m_lasterr; int64_t m_tid_to_remove; int64_t m_tid_of_fd_to_remove; - vector* m_fds_to_remove; + std::vector* m_fds_to_remove; uint64_t m_lastevent_ts; // the parsing engine sinsp_parser* m_parser; // the statistics analysis engine scap_dumper_t* m_dumper; + bool m_is_dumping; + bool m_filter_proc_table_when_saving; const scap_machine_info* m_machine_info; uint32_t m_num_cpus; sinsp_thread_privatestate_manager m_thread_privatestate_manager; + bool m_is_tracers_capture_enabled; + // This is used to support reading merged files, where the capture needs to + // restart in the middle of the file. + uint64_t m_file_start_offset; + bool m_flush_memory_dump; + bool m_large_envs_enabled; sinsp_network_interfaces* m_network_interfaces; - +public: sinsp_thread_manager* m_thread_manager; + sinsp_container_manager m_container_manager; + + // + // Kubernetes + // +#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + std::string* m_k8s_api_server; + std::string* m_k8s_api_cert; +#ifdef HAS_CAPTURE + std::shared_ptr m_k8s_ssl; + std::shared_ptr m_k8s_bt; + unique_ptr m_k8s_api_handler; + shared_ptr>> m_k8s_collector; + bool m_k8s_api_detected = false; + unique_ptr m_k8s_ext_handler; + k8s_ext_list_ptr_t m_ext_list_ptr; + bool m_k8s_ext_detect_done = false; +#endif // HAS_CAPTURE + k8s* m_k8s_client; + uint64_t m_k8s_last_watch_time_ns; +#endif // !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD) + + // + // Mesos/Marathon + // + std::string m_mesos_api_server; + std::vector m_marathon_api_server; + mesos* m_mesos_client; + uint64_t m_mesos_last_watch_time_ns; + + // + // True if sysdig is ran with -v. + // Used by mesos and k8s objects. + // + bool m_verbose_json = false; + + // + // True if the command line argument is set to show container information + // The default is false set within the constructor + // + bool m_print_container_data; + #ifdef HAS_FILTERING uint64_t m_firstevent_ts; sinsp_filter* m_filter; + sinsp_evttype_filter *m_evttype_filter; + std::string m_filterstring; + #endif // @@ -487,22 +1105,44 @@ VISIBILITY_PRIVATE #ifdef GATHER_INTERNAL_STATS sinsp_stats m_stats; #endif - uint32_t m_n_proc_lookups; - uint32_t m_max_n_proc_lookups; - uint32_t m_max_n_proc_socket_lookups; + int32_t m_n_proc_lookups; + uint64_t m_n_proc_lookups_duration_ns; + int32_t m_n_main_thread_lookups; + int32_t m_max_n_proc_lookups = -1; + int32_t m_max_n_proc_socket_lookups = -1; +#ifdef HAS_ANALYZER + std::vector m_tid_collisions; +#endif // // Saved snaplen // uint32_t m_snaplen; + // + // Saved increased capture range + // + struct + { + uint16_t range_start; + uint16_t range_end; + } m_increased_snaplen_port_range; + + int32_t m_statsd_port; + // // Some thread table limits // uint32_t m_max_thread_table_size; + uint32_t m_max_fdtable_size; uint64_t m_thread_timeout_ns; uint64_t m_inactive_thread_scan_time_ns; + // + // Container limits + // + uint64_t m_inactive_container_scan_time_ns; + // // How to render the data buffers // @@ -511,13 +1151,69 @@ VISIBILITY_PRIVATE // // User and group tables // + bool m_import_users; unordered_map m_userlist; unordered_map m_grouplist; + // + // The cycle-writer for files + // + cycle_writer* m_cycle_writer; + bool m_write_cycling; + +#ifdef SIMULATE_DROP_MODE // // Some dropping infrastructure // bool m_isdropping; +#endif + + // + // App events + // + bool m_track_tracers_state; + list m_partial_tracers_list; + simple_lifo_queue* m_partial_tracers_pool; + + // + // Protocol decoding state + // + std::vector m_decoders_reset_list; + + // + // meta event management for other sources like k8s, mesos. + // + sinsp_evt* m_metaevt; + meta_event_callback m_meta_event_callback; + void* m_meta_event_callback_data; + + // A queue of pending container events. Written from async + // callbacks that occur after looking up container + // information, read from sinsp::next(). +#ifndef _WIN32 + tbb::concurrent_queue> m_pending_container_evts; +#endif + + // Holds an event dequeued from the above queue + std::shared_ptr m_container_evt; + + // + // End of second housekeeping + // + bool m_get_procs_cpu_from_driver; + uint64_t m_next_flush_time_ns; + uint64_t m_last_procrequest_tod; + sinsp_proc_metainfo m_meinfo; + uint64_t m_next_stats_print_time_ns; + + static unsigned int m_num_possible_cpus; +#if defined(HAS_CAPTURE) + int64_t m_sysdig_pid; +#endif + + // Any thread with a comm in this set will not have its events + // returned in sinsp::next() + std::set m_suppressed_comms; friend class sinsp_parser; friend class sinsp_analyzer; @@ -526,11 +1222,40 @@ VISIBILITY_PRIVATE friend class sinsp_threadinfo; friend class sinsp_fdtable; friend class sinsp_thread_manager; + friend class sinsp_container_manager; friend class sinsp_dumper; friend class sinsp_analyzer_fd_listener; friend class sinsp_chisel; + friend class sinsp_tracerparser; + friend class sinsp_filter_check_event; + friend class sinsp_protodecoder; + friend class lua_cbacks; + friend class sinsp_filter_check_container; + friend class sinsp_worker; + friend class sinsp_table; + friend class curses_textbox; + friend class sinsp_filter_check_fd; + friend class sinsp_filter_check_k8s; + friend class sinsp_filter_check_mesos; + friend class sinsp_filter_check_evtin; + friend class sinsp_baseliner; + friend class sinsp_memory_dumper; + friend class sinsp_network_interfaces; + friend class test_helper; template friend class sinsp_connection_manager; + +#ifdef SYSDIG_TEST +protected: + void inject_machine_info(const scap_machine_info *value) + { + m_machine_info = value; + } + void inject_network_interfaces(sinsp_network_interfaces *value) + { + m_network_interfaces = value; + } +#endif // SYSDIG_TEST }; /*@}*/ diff --git a/userspace/libsinsp/sinsp.vcxproj b/userspace/libsinsp/sinsp.vcxproj index 02302708b1..cb5a95be71 100644 --- a/userspace/libsinsp/sinsp.vcxproj +++ b/userspace/libsinsp/sinsp.vcxproj @@ -49,7 +49,7 @@ - WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_WINDOWS;_USRDLL;SINSP_EXPORTS;_NO_DEBUG_HEAP;%(PreprocessorDefinitions) + WIN32;_CRT_SECURE_NO_WARNINGS;PLATFORM_NAME="Windows";_DEBUG;_WINDOWS;_USRDLL;SINSP_EXPORTS;_NO_DEBUG_HEAP;%(PreprocessorDefinitions) MultiThreadedDebugDLL Level3 ProgramDatabase @@ -69,7 +69,7 @@ - WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_WINDOWS;_USRDLL;SINSP_EXPORTS;%(PreprocessorDefinitions) + WIN32;_CRT_SECURE_NO_WARNINGS;PLATFORM_NAME="Windows";NDEBUG;_WINDOWS;_USRDLL;SINSP_EXPORTS;%(PreprocessorDefinitions) MultiThreadedDLL Level3 ProgramDatabase @@ -99,6 +99,7 @@ + diff --git a/userspace/libsinsp/sinsp.vcxproj.filters b/userspace/libsinsp/sinsp.vcxproj.filters index d9c3c9fe92..fc952644bc 100644 --- a/userspace/libsinsp/sinsp.vcxproj.filters +++ b/userspace/libsinsp/sinsp.vcxproj.filters @@ -49,6 +49,9 @@ Source Files + + Source Files + diff --git a/userspace/libsinsp/sinsp_auth.cpp b/userspace/libsinsp/sinsp_auth.cpp new file mode 100644 index 0000000000..a60ff7216e --- /dev/null +++ b/userspace/libsinsp/sinsp_auth.cpp @@ -0,0 +1,156 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// sinsp_auth.cpp +// +// Authentication/verification utilities +// + +#if defined(__linux__) + +#include "sinsp_auth.h" +#include +#include +#include +#include +#include +#include + +// +// sinsp_ssl +// + +sinsp_ssl::sinsp_ssl(const std::string& cert, const std::string& key, const std::string& key_passphrase, + const std::string& ca_cert, bool verify_peer, const std::string& cert_type): + m_cert_type(cert_type), m_cert(cert), m_key(key), m_key_passphrase(key_passphrase), + m_ca_cert(ca_cert), m_verify_peer(verify_peer) +{ +} + +sinsp_ssl::~sinsp_ssl() +{ +} + +std::string sinsp_ssl::memorize_file(const std::string& disk_file) +{ + std::string mem_file; + if(disk_file.empty()) + { + return mem_file; + } + std::string::size_type pos = disk_file.rfind('/'); + if(pos == std::string::npos) + { + mem_file.append(1, '/').append(disk_file); + } + else + { + mem_file.append(disk_file.substr(pos, disk_file.size() - pos)); + } + mem_file.append(1, '~'); + int fd = shm_open(mem_file.c_str(), O_CREAT|O_RDWR, S_IRUSR|S_IWUSR); + if(fd != -1) + { + char buf[FILENAME_MAX] = { 0 }; + std::ifstream ifs(disk_file); + std::string fd_path = "/proc/self/fd/" + std::to_string(fd); + ssize_t sz = readlink(fd_path.c_str(), buf, sizeof(buf)); + if(sz != -1 && sz <= static_cast(sizeof(buf))) + { + mem_file.assign(buf, sz); + std::string str; + std::ofstream ofs(mem_file, std::ofstream::out); + while(std::getline(ifs, str)) + { + ofs << str << '\n'; + } + } + else + { + std::ostringstream os; + os << "Error occurred while trying to determine the real path of memory file [" << fd_path << "]: " + << strerror(errno) << " (disk file [" << disk_file << "] will be used)."; + g_logger.log(os.str(), sinsp_logger::SEV_WARNING); + return disk_file; + } + } + else + { + std::ostringstream os; + os << "Memory file creation error: " << strerror(errno) << " (disk file [" << disk_file << "] will be used)."; + g_logger.log(os.str(), sinsp_logger::SEV_WARNING); + return disk_file; + } + return mem_file; +} + +void sinsp_ssl::unmemorize_file(const std::string& mem_file) +{ + if(shm_unlink(mem_file.c_str()) == 0) + { + std::ostringstream os; + os << "Memory file [" << mem_file << "] unlink error: " << strerror(errno); + g_logger.log(os.str(), sinsp_logger::SEV_WARNING); + } +} + +// +// bearer_token +// + +sinsp_bearer_token::sinsp_bearer_token(const std::string& bearer_token_file, bool curl_support): + m_bearer_token(stringize_file(bearer_token_file)), m_bt_auth_header(nullptr) +{ + if(curl_support) + { + std::size_t len = m_bearer_token.length(); // curl does not tolerate newlines in headers + while(len && (m_bearer_token[len-1] == '\r' || m_bearer_token[len-1] == '\n')) + { + m_bearer_token.erase(len-1); + len = m_bearer_token.length(); + } + if(len) + { + std::string hdr = "Authorization: Bearer "; + hdr.append(m_bearer_token); + m_bt_auth_header = curl_slist_append(m_bt_auth_header, hdr.c_str()); + } + } +} + +sinsp_bearer_token::~sinsp_bearer_token() +{ + if(m_bt_auth_header) + { + curl_slist_free_all(m_bt_auth_header); + } +} + +std::string sinsp_bearer_token::stringize_file(const std::string& disk_file) +{ + std::string tmp, content; + std::ifstream ifs(disk_file); + while(std::getline(ifs, tmp)) + { + content.append(tmp).append(1, '\n'); + } + return content; +} + +#endif // __linux__ diff --git a/userspace/libsinsp/sinsp_auth.h b/userspace/libsinsp/sinsp_auth.h new file mode 100644 index 0000000000..cad98da906 --- /dev/null +++ b/userspace/libsinsp/sinsp_auth.h @@ -0,0 +1,132 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// sinsp_auth.h +// +// Authentication/verification utilities +// + +#if defined(__linux__) && !defined(MINIMAL_BUILD) + +#pragma once + +#include "sinsp.h" +#include "sinsp_int.h" +#include "uri.h" +#include "curl/curl.h" +#include +#include + +class sinsp_ssl +{ +public: + typedef std::shared_ptr ptr_t; + + sinsp_ssl(const std::string& cert, const std::string& key, const std::string& key_passphrase = "", + const std::string& ca_cert = "", bool verify_peer = false, const std::string& cert_type = "PEM"); + ~sinsp_ssl(); + + const std::string& cert_type() const; + const std::string& cert() const; + const std::string& key() const; + const std::string& key_passphrase() const; + const std::string& ca_cert() const; + bool verify_peer() const; + +private: + static std::string memorize_file(const std::string& disk_file); + static void unmemorize_file(const std::string& mem_file); + + std::string m_cert_type; + std::string m_cert; + std::string m_key; + std::string m_key_passphrase; + std::string m_ca_cert; + bool m_verify_peer = false; +}; + +class sinsp_bearer_token +{ +public: + typedef std::shared_ptr ptr_t; + + sinsp_bearer_token(const std::string& bearer_token_file, bool curl_support = true); + ~sinsp_bearer_token(); + + const std::string& get_token() const; + struct curl_slist* bt_auth_header(); + +private: + static std::string stringize_file(const std::string& disk_file); + + std::string m_bearer_token; + struct curl_slist* m_bt_auth_header; +}; + + +// +// ssl +// + +inline const std::string& sinsp_ssl::cert_type() const +{ + return m_cert_type; +} + +inline const std::string& sinsp_ssl::cert() const +{ + return m_cert; +} + +inline const std::string& sinsp_ssl::key() const +{ + return m_key; +} + +inline const std::string& sinsp_ssl::key_passphrase() const +{ + return m_key_passphrase; +} + +inline const std::string& sinsp_ssl::ca_cert() const +{ + return m_ca_cert; +} + +inline bool sinsp_ssl::verify_peer() const +{ + return m_verify_peer; +} + + +// +// bearer_token +// + +inline const std::string& sinsp_bearer_token::get_token() const +{ + return m_bearer_token; +} + +inline struct curl_slist* sinsp_bearer_token::bt_auth_header() +{ + return m_bt_auth_header; +} + +#endif // __linux__ diff --git a/userspace/libsinsp/sinsp_capture_interrupt_exception.h b/userspace/libsinsp/sinsp_capture_interrupt_exception.h new file mode 100644 index 0000000000..c1992614b2 --- /dev/null +++ b/userspace/libsinsp/sinsp_capture_interrupt_exception.h @@ -0,0 +1,32 @@ +/* +Copyright (C) 2013-2019 Sysdig Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include "sinsp_exception.h" + +/*! + \brief sinsp library exception. +*/ +class sinsp_capture_interrupt_exception : public sinsp_exception +{ +public: + sinsp_capture_interrupt_exception(): + sinsp_exception("capture interrupted") + { } +}; diff --git a/userspace/libsinsp/sinsp_curl.cpp b/userspace/libsinsp/sinsp_curl.cpp new file mode 100644 index 0000000000..19655faef3 --- /dev/null +++ b/userspace/libsinsp/sinsp_curl.cpp @@ -0,0 +1,480 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// sinsp_curl.cpp +// +// Curl utility +// + +#if defined(__linux__) + +#include "sinsp_curl.h" +#include "http_reason.h" +#include +#include +#include +#include +#include +#include +#include + +sinsp_curl_http_headers::sinsp_curl_http_headers(): + m_curl_header_list(NULL) +{ + +} + +sinsp_curl_http_headers::~sinsp_curl_http_headers() +{ + if(m_curl_header_list) + { + curl_slist_free_all(m_curl_header_list); + } +} + +void sinsp_curl_http_headers::add(const string& header) +{ + m_curl_header_list = curl_slist_append(m_curl_header_list, header.c_str()); +} + +sinsp_curl::data sinsp_curl::m_config; + +sinsp_curl::sinsp_curl(const uri& url, long timeout_ms, bool debug): + m_curl(curl_easy_init()), m_uri(url), m_timeout_ms(timeout_ms), m_debug(debug) + +{ + init(); +} + +sinsp_curl::sinsp_curl(const uri& url, const std::string& bearer_token_file, long timeout_ms, bool debug): + m_curl(curl_easy_init()), m_uri(url), m_timeout_ms(timeout_ms), m_bt(new bearer_token(bearer_token_file)), + m_debug(debug) +{ + init(); +} + +sinsp_curl::sinsp_curl(const uri& url, + const std::string& cert, const std::string& key, const std::string& key_passphrase, + const std::string& ca_cert, bool verify_peer, const std::string& cert_type, + const std::string& bearer_token_file, + long timeout_ms, bool debug): + m_curl(curl_easy_init()), m_uri(url), m_timeout_ms(timeout_ms), + m_ssl(new ssl(cert, key, key_passphrase, ca_cert, verify_peer, cert_type)), + m_bt(new bearer_token(bearer_token_file)), + m_debug(debug) +{ + init(); +} + +sinsp_curl::sinsp_curl(const uri& url, ssl::ptr_t p_ssl, bearer_token::ptr_t p_bt, long timeout_ms, bool debug): + m_curl(curl_easy_init()), m_uri(url), m_timeout_ms(timeout_ms), + m_ssl(p_ssl), + m_bt(p_bt), + m_debug(debug) +{ + init(); +} + +void sinsp_curl::init() +{ + if(!m_curl) + { + throw sinsp_exception("Cannot initialize CURL."); + } + + check_error(curl_easy_setopt(m_curl, CURLOPT_FORBID_REUSE, 1L)); + + if(m_ssl) + { + init_ssl(m_curl, m_ssl); + } + + if(m_bt) + { + init_bt(m_curl, m_bt); + } + + enable_debug(m_curl, m_debug); + m_response_code = -1; +} + +sinsp_curl::~sinsp_curl() +{ + curl_easy_cleanup(m_curl); +} + +void sinsp_curl::init_bt(CURL* curl, bearer_token::ptr_t bt) +{ + if(bt && bt->bt_auth_header()) + { + check_error(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, bt->bt_auth_header())); + } +} + +void sinsp_curl::enable_debug(CURL* curl, bool enable) +{ + if(curl) + { + curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, &sinsp_curl::trace); + curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &m_config); + long en = 0L; + if(enable) { en = 1L; } + m_config.trace_ascii = en; + curl_easy_setopt(curl, CURLOPT_VERBOSE, en); + } +} + +void sinsp_curl::init_ssl(CURL* curl, ssl::ptr_t ssl_data) +{ + if(curl && ssl_data) + { + if(!ssl_data->cert().empty()) + { + if(!ssl_data->cert_type().empty()) + { + check_error(curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, ssl_data->cert_type().c_str())); + } + check_error(curl_easy_setopt(curl, CURLOPT_SSLCERT, ssl_data->cert().c_str())); + g_logger.log("CURL SSL certificate: " + ssl_data->cert(), sinsp_logger::SEV_DEBUG); + } + + if(!ssl_data->key_passphrase().empty()) + { + check_error(curl_easy_setopt(curl, CURLOPT_KEYPASSWD, ssl_data->key_passphrase().c_str())); + g_logger.log("CURL SSL key password SET. ", sinsp_logger::SEV_DEBUG); + } + + if(!ssl_data->key().empty()) + { + if(!ssl_data->cert_type().empty()) + { + check_error(curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, ssl_data->cert_type().c_str())); + } + check_error(curl_easy_setopt(curl, CURLOPT_SSLKEY, ssl_data->key().c_str())); + g_logger.log("CURL SSL key: " + ssl_data->key(), sinsp_logger::SEV_DEBUG); + } + + if(ssl_data->verify_peer()) + { + check_error(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L)); + check_error(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L)); + g_logger.log("CURL SSL peer and host verification ENABLED.", sinsp_logger::SEV_DEBUG); + } + else + { + check_error(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0)); + check_error(curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0)); + g_logger.log("CURL SSL peer and host verification DISABLED.", sinsp_logger::SEV_DEBUG); + } + + if(!ssl_data->ca_cert().empty()) + { + check_error(curl_easy_setopt(curl, CURLOPT_CAINFO, ssl_data->ca_cert().c_str())); + g_logger.log("CURL SSL CA cert set to: " + ssl_data->ca_cert(), sinsp_logger::SEV_DEBUG); + } + } +} + +string sinsp_curl::get_data(bool do_log) +{ + std::ostringstream os; + if(get_data(os)) + { + return os.str(); + } + if(do_log) + { + g_logger.log("CURL error while connecting to " + m_uri.to_string(false) + ", " + "response: [" + os.str() + ']', sinsp_logger::SEV_ERROR); + } + return ""; +} + +size_t sinsp_curl::header_callback(char *buffer, size_t size, size_t nitems, void *userdata) +{ + size_t sz = nitems * size; + std::string buf(buffer, sz); + + const std::string loc = "Location:"; + const std::string nl = "\r\n"; + std::string::size_type loc_pos = buf.find(loc); + std::string::size_type nl_pos = buf.find(nl); + if((loc_pos != std::string::npos) && (nl_pos != std::string::npos) && + (nl_pos - loc.length() > (loc + nl).length())) + { + std::string::size_type url_pos = buf.find("https://", loc_pos); + if(url_pos == std::string::npos) + { + url_pos = buf.find("//", loc_pos); + if(url_pos != std::string::npos) // still absolute + { + buf = buf.substr(url_pos, nl_pos-url_pos); + buf.insert(0, "http:"); + } + else // relative + { + buf = buf.substr(loc.length(), nl_pos-loc.length()); + } + } + else + { + buf = buf.substr(url_pos, nl_pos-url_pos); + } + trim(buf); + sz = buf.length(); + if(sz < CURL_MAX_HTTP_HEADER) + { + g_logger.log("HTTP redirect Location: (" + buf + ')', sinsp_logger::SEV_TRACE); + std::strncpy((char*) userdata, buf.data(), sz); + ((char*) userdata)[sz] = 0; + } + } + return nitems * size; +} + +bool sinsp_curl::is_redirect(long http_code) +{ + return ((http_code >= 301 && http_code <= 303) || + (http_code >= 307 && http_code <= 308)); +} + +bool sinsp_curl::handle_redirect(uri& url, std::string&& loc, std::ostream& os) +{ + if(!loc.empty()) + { + g_logger.log("HTTP redirect received from [" + url.to_string(false) + ']', + sinsp_logger::SEV_INFO); + std::string::size_type url_pos = loc.find("//"); + if(url_pos != std::string::npos) + { + uri::credentials_t creds; + url.get_credentials(creds); + url = trim(loc); + if(!creds.first.empty()) + { + url.set_credentials(creds); + } + } + else // location relative, take just path + { + url.set_path(trim(loc)); + } + g_logger.log("HTTP redirecting to [" + url.to_string(false) + "].", + sinsp_logger::SEV_INFO); + return true; + } + else + { + g_logger.log("CURL redirect received from [" + url.to_string(false) + "], " + "but location not found.", sinsp_logger::SEV_ERROR); + return false; + } + return false; +} + +size_t read_data(void* buffer, size_t size, size_t nmemb, void* instream) +{ + auto body = (stringstream*) instream; + body->read((char*) buffer, size*nmemb); + return body->gcount(); +} + +bool sinsp_curl::get_data(std::ostream& os) +{ + CURLcode res = CURLE_OK; + check_error(curl_easy_setopt(m_curl, CURLOPT_URL, m_uri.to_string().c_str())); + check_error(curl_easy_setopt(m_curl, CURLOPT_HEADERDATA, m_redirect)); + check_error(curl_easy_setopt(m_curl, CURLOPT_HEADERFUNCTION, header_callback)); + check_error(curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, static_cast(m_timeout_ms / 1000))); + check_error(curl_easy_setopt(m_curl, CURLOPT_TIMEOUT_MS, m_timeout_ms)); + check_error(curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1)); //Prevent "longjmp causes uninitialized stack frame" bug + check_error(curl_easy_setopt(m_curl, CURLOPT_ACCEPT_ENCODING, "deflate")); + check_error(curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, &sinsp_curl::write_data)); + check_error(curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &os)); + check_error(curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, &read_data)); + check_error(curl_easy_setopt(m_curl, CURLOPT_READDATA, &m_body)); + if(m_headers.ptr() != NULL) + { + setopt(CURLOPT_HTTPHEADER, m_headers.ptr()); + } + res = curl_easy_perform(m_curl); + if(res != CURLE_OK) + { + os << curl_easy_strerror(res) << std::flush; + } + else + { + // HTTP errors are not returned by curl API + // error will be in the response stream + check_error(curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &m_response_code)); + if(m_response_code >= 400) + { + g_logger.log("CURL HTTP error while accessing [" + m_uri.to_string(false) + "]: " + + std::to_string(m_response_code) + " (" + http_reason::get(m_response_code) + ')', sinsp_logger::SEV_ERROR); + return false; + } + else if(is_redirect(m_response_code)) + { + g_logger.log("HTTP redirect (" + std::to_string(m_response_code) + ')', sinsp_logger::SEV_DEBUG); + if(handle_redirect(m_uri, std::string(m_redirect), os)) + { + std::ostringstream* pos = dynamic_cast(&os); + if(pos) + { + pos->str(""); + return get_data(*pos); + } + else + { + g_logger.log("HTTP redirect received from [" + m_uri.to_string(false) + "] but " + "output stream can not be obtained (dynamic cast failed).", + sinsp_logger::SEV_ERROR); + return false; + } + } + } + } + + return res == CURLE_OK; +} + +size_t sinsp_curl::write_data(void *ptr, size_t size, size_t nmemb, void *cb) +{ + std::string data(reinterpret_cast(ptr), static_cast(size * nmemb)); + *reinterpret_cast(cb) << data << std::flush; + return size * nmemb; +} + +bool sinsp_curl::check_error(unsigned ret, bool exc) +{ + if(ret >= CURL_LAST && exc) + { + throw sinsp_exception("Invalid CURL return value:" + std::to_string(ret)); + } + else { return false; } + + CURLcode res = (CURLcode)ret; + if(CURLE_OK != res && CURLE_AGAIN != res && exc) + { + std::ostringstream os; + os << "Error: " << curl_easy_strerror(res); + throw sinsp_exception(os.str()); + } + else { return false; } + return true; +} + +void sinsp_curl::dump(const char *text, unsigned char *ptr, size_t size, char nohex) +{ + const std::size_t DBG_BUF_SIZE = 1024; + char stream[DBG_BUF_SIZE] = { 0 }; + std::ostringstream os; + size_t i; + size_t c; + unsigned int width = 0x10; + if(nohex) + { + width = 0x40; + } + snprintf(stream, DBG_BUF_SIZE, "%s, %10.10ld bytes (0x%8.8lx)\n", text, (long)size, (long)size); + os << stream; + for(i=0; i=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.'); + os << stream; + if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) + { + i+=(c+3-width); + break; + } + } + snprintf(stream, DBG_BUF_SIZE, "%c", '\n'); + os << stream; + } + g_logger.log("CURL: " + os .str(), sinsp_logger::SEV_DEBUG); +} + +int sinsp_curl::trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp) +{ + struct data *config = (struct data *)userp; + const char *text; + (void)handle; // prevent compiler warning + switch (type) + { + case CURLINFO_TEXT: + fprintf(stderr, "== Info: %s", data); + default: // in case a new one is introduced to shock us + return 0; + case CURLINFO_HEADER_OUT: + text = "=> Send header"; + break; + case CURLINFO_DATA_OUT: + text = "=> Send data"; + break; + case CURLINFO_SSL_DATA_OUT: + text = "=> Send SSL data"; + break; + case CURLINFO_HEADER_IN: + text = "<= Recv header"; + break; + case CURLINFO_DATA_IN: + text = "<= Recv data"; + break; + case CURLINFO_SSL_DATA_IN: + text = "<= Recv SSL data"; + break; + } + dump(text, (unsigned char *)data, size, config->trace_ascii); + return 0; +} + +void sinsp_curl::set_body(const string& data) +{ + m_body.clear(); + m_body << data; + add_header(string("Content-Length: ") + to_string(data.size())); +} + +#endif // __linux__ + diff --git a/userspace/libsinsp/sinsp_curl.h b/userspace/libsinsp/sinsp_curl.h new file mode 100644 index 0000000000..a2b805458c --- /dev/null +++ b/userspace/libsinsp/sinsp_curl.h @@ -0,0 +1,185 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// sinsp_curl.h +// +// Curl utility +// + +#if defined(__linux__) && !defined(MINIMAL_BUILD) + +#pragma once + +#include "sinsp.h" +#include "sinsp_int.h" +#include "sinsp_auth.h" +#include "uri.h" +#include "curl/curl.h" +#include +#include + +class sinsp_curl_http_headers +{ +public: + sinsp_curl_http_headers(); + ~sinsp_curl_http_headers(); + + void add(const string& header); + + struct curl_slist* ptr() + { + return m_curl_header_list; + } + +private: + struct curl_slist* m_curl_header_list; +}; + +class sinsp_curl +{ +public: + typedef sinsp_ssl ssl; + typedef sinsp_bearer_token bearer_token; + + static const long DEFAULT_TIMEOUT_MS = 5000L; + + sinsp_curl(const uri& url, long timeout_ms = DEFAULT_TIMEOUT_MS, bool debug = false); + sinsp_curl(const uri& url, const std::string& bearer_token_file, long timeout_ms = DEFAULT_TIMEOUT_MS, bool debug = false); + sinsp_curl(const uri& url, + const std::string& cert, const std::string& key, const std::string& key_passphrase = "", + const std::string& ca_cert = "", bool verify_peer = false, const std::string& cert_type = "PEM", + const std::string& bearer_token_file = "", + long timeout_ms = DEFAULT_TIMEOUT_MS, + bool debug = false); + sinsp_curl(const uri& url, ssl::ptr_t p_ssl = 0, bearer_token::ptr_t p_bt = 0, + long timeout_ms = DEFAULT_TIMEOUT_MS, bool debug = false); + + ~sinsp_curl(); + + bool get_data(std::ostream& os); + std::string get_data(bool do_log = true); + + void set_timeout(long seconds); + long get_timeout() const; + + void set_url(const std::string& url); + std::string get_url(bool show_creds = true) const; + void set_body(const string& data); + + bool is_secure() const; + ssl::ptr_t get_ssl(); + + template + void setopt(Opt opt, Arg arg) + { + check_error(curl_easy_setopt(m_curl, opt, arg)); + } + + void enable_debug() + { + sinsp_curl::enable_debug(m_curl); + } + + template + void add_header(T body) + { + m_headers.add(forward(body)); + } + + static void init_ssl(CURL* curl, ssl::ptr_t ssl_data); + bearer_token::ptr_t get_bt(); + static void init_bt(CURL* curl, bearer_token::ptr_t bt); + + static void enable_debug(CURL* curl, bool enable = true); + static bool check_error(unsigned ret, bool exc = true); + static size_t header_callback(char *buffer, size_t size, size_t nitems, void *userdata); + static bool is_redirect(long http_code); + static bool handle_redirect(uri& url, std::string&& loc, std::ostream& os); + static size_t write_data(void *ptr, size_t size, size_t nmemb, void *cb); + + const vector& response_headers() + { + return m_response_headers; + } + + long get_response_code() const + { + return m_response_code; + } + +private: + struct data + { + char trace_ascii; // 1 or 0 + }; + static data m_config; + static void dump(const char *text, unsigned char *ptr, size_t size, char nohex); + static int trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp); + + void init(); + + CURL* m_curl; + uri m_uri; + long m_timeout_ms; + ssl::ptr_t m_ssl; + bearer_token::ptr_t m_bt; + bool m_debug; + char m_redirect[CURL_MAX_HTTP_HEADER] = {0}; + stringstream m_body; + sinsp_curl_http_headers m_headers; + vector m_response_headers; + long m_response_code; +}; + +inline void sinsp_curl::set_timeout(long milliseconds) +{ + m_timeout_ms = milliseconds; +} + +inline long sinsp_curl::get_timeout() const +{ + return m_timeout_ms; +} + +inline void sinsp_curl::set_url(const std::string& url) +{ + m_uri = url; +} + +inline std::string sinsp_curl::get_url(bool show_creds) const +{ + return m_uri.to_string(show_creds); +} + +inline bool sinsp_curl::is_secure() const +{ + return m_uri.is_secure(); +} + +inline sinsp_curl::ssl::ptr_t sinsp_curl::get_ssl() +{ + return m_ssl; +} + +inline sinsp_curl::bearer_token::ptr_t sinsp_curl::get_bt() +{ + return m_bt; +} + +#endif // __linux__ diff --git a/userspace/libsinsp/sinsp_errno.h b/userspace/libsinsp/sinsp_errno.h index cbeba05c70..46b0489056 100644 --- a/userspace/libsinsp/sinsp_errno.h +++ b/userspace/libsinsp/sinsp_errno.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #define SE_EPERM 1 /* Operation not permitted */ @@ -140,6 +141,7 @@ along with sysdig. If not, see . #define SE_EDQUOT 122 /* Quota exceeded */ #define SE_ENOMEDIUM 123 /* No medium found */ #define SE_EMEDIUMTYPE 124 /* Wrong medium type */ +#define SE_ECANCELED 125 #define SE_ERESTARTSYS 512 /* Interrupted system call */ #define SE_ERESTARTNOINTR 513 #define SE_ERESTARTNOHAND 514 /* restart if no handler.. */ @@ -155,4 +157,7 @@ along with sysdig. If not, see . #define SE_EBADTYPE 527 /* Type not supported by server */ #define SE_EJUKEBOX 528 /* Request initiated, but will not complete before timeout */ #define SE_EIOCBQUEUED 529 /* iocb queued, will get completion event */ -#define SE_EIOCBRETRY 530 /* iocb queued, will trigger a retry */ \ No newline at end of file +#define SE_EIOCBRETRY 530 /* iocb queued, will trigger a retry */ + +// note: any new error here will need to have a mapping +// in utils.cpp under sinsp_utils::errno_to_str diff --git a/userspace/libsinsp/sinsp_exception.h b/userspace/libsinsp/sinsp_exception.h new file mode 100644 index 0000000000..6d6791135a --- /dev/null +++ b/userspace/libsinsp/sinsp_exception.h @@ -0,0 +1,37 @@ +/* +Copyright (C) 2013-2019 Sysdig Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include +#include + +/*! + \brief sinsp library exception. +*/ +class sinsp_exception : public std::runtime_error +{ +public: + sinsp_exception(const std::string& error_str): + std::runtime_error(error_str) + { } + + sinsp_exception(const char* const error_str): + std::runtime_error(error_str) + { } +}; diff --git a/userspace/libsinsp/sinsp_inet.h b/userspace/libsinsp/sinsp_inet.h new file mode 100644 index 0000000000..a6f450e3bd --- /dev/null +++ b/userspace/libsinsp/sinsp_inet.h @@ -0,0 +1,25 @@ +/* +Copyright (C) 2019 Sysdig, Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#if defined(_WIN32) +# include +#else +# include +#endif diff --git a/userspace/libsinsp/sinsp_int.h b/userspace/libsinsp/sinsp_int.h index d38f9b88b4..81eb78d78a 100644 --- a/userspace/libsinsp/sinsp_int.h +++ b/userspace/libsinsp/sinsp_int.h @@ -1,130 +1,124 @@ -/* -Copyright (C) 2013-2014 Draios inc. - -This file is part of sysdig. - -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. - -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . -*/ - -//////////////////////////////////////////////////////////////////////////// -// Public definitions for the scap library -//////////////////////////////////////////////////////////////////////////// -#pragma once - -#ifdef _WIN32 -#include -#endif -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -#include "../libscap/scap.h" -#include "settings.h" -#include "utils.h" -#include "../libscap/scap.h" -#include "parsers.h" -#include "ifinfo.h" -#include "internal_metrics.h" - -#ifndef MIN -#define MIN(X,Y) ((X) < (Y)? (X):(Y)) -#define MAX(X,Y) ((X) > (Y)? (X):(Y)) -#endif - -// -// ASSERT implementation -// -#ifdef _DEBUG -#ifdef ASSERT_TO_LOG -#define ASSERT(X) \ - if(!(X)) \ - { \ - g_logger.format(sinsp_logger::SEV_ERROR, "ASSERTION %s at %s:%d", #X , __FILE__, __LINE__); \ - assert(X); \ - } -#else -#define ASSERT(X) assert(X) -#endif // ASSERT_TO_LOG -#else // _DEBUG -#define ASSERT(X) -#endif // _DEBUG - -// -// Public export macro -// -#ifdef _WIN32 -#define SINSP_PUBLIC __declspec(dllexport) -#define BRK(X) {if(evt->get_num() == X)__debugbreak();} -#else -#define SINSP_PUBLIC -#define BRK(X) -#endif - -// -// Path separator -// -#ifdef _WIN32 -#define DIR_PATH_SEPARATOR '\\' -#else -#define DIR_PATH_SEPARATOR '/' -#endif - -// -// The logger -// -extern sinsp_logger g_logger; - -// -// Prototype of the callback invoked by the thread table when a thread is -// created or destroyed -// -class sinsp_threadtable_listener -{ -public: - virtual ~sinsp_threadtable_listener() - { - } - virtual void on_thread_created(sinsp_threadinfo* tinfo) = 0; - virtual void on_thread_destroyed(sinsp_threadinfo* tinfo) = 0; -}; - -// -// Prototype of the callback invoked by the thread table when a thread is -// created or destroyed -// -class sinsp_fd_listener -{ -public: - virtual ~sinsp_fd_listener() - { - } - virtual void on_read(sinsp_evt *evt, int64_t tid, int64_t fd, char *data, uint32_t original_len, uint32_t len) = 0; - virtual void on_write(sinsp_evt *evt, int64_t tid, int64_t fd, char *data, uint32_t original_len, uint32_t len) = 0; - virtual void on_connect(sinsp_evt *evt, uint8_t* packed_data) = 0; - virtual void on_accept(sinsp_evt *evt, int64_t newfd, uint8_t* packed_data, sinsp_fdinfo_t* new_fdinfo) = 0; - virtual void on_erase_fd(erase_fd_params* params) = 0; - virtual void on_socket_shutdown(sinsp_evt *evt) = 0; -}; +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + + +//////////////////////////////////////////////////////////////////////////// +// Public definitions for the scap library +//////////////////////////////////////////////////////////////////////////// +#pragma once + +#ifdef _WIN32 +#include +#else +#include +#endif +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#include "../libscap/scap.h" +#include "settings.h" +#include "utils.h" +#include "../libscap/scap.h" +#include "parsers.h" +#include "ifinfo.h" +#include "internal_metrics.h" +#include "sinsp_public.h" + +#ifndef MIN +#define MIN(X,Y) ((X) < (Y)? (X):(Y)) +#define MAX(X,Y) ((X) > (Y)? (X):(Y)) +#endif + +// +// Public export macro +// +#ifdef _WIN32 +#define BRK(X) {if(evt != NULL && evt->get_num() == X)__debugbreak();} +#else +#define BRK(X) +#endif + +// +// Path separator +// +#ifdef _WIN32 +#define DIR_PATH_SEPARATOR '\\' +#else +#define DIR_PATH_SEPARATOR '/' +#endif + +// +// The logger +// +extern sinsp_logger g_logger; +#define glogf g_logger.format + +// +// Prototype of the callback invoked by the thread table when a thread is +// created or destroyed +// +class sinsp_threadtable_listener +{ +public: + virtual ~sinsp_threadtable_listener() + { + } + virtual void on_thread_created(sinsp_threadinfo* tinfo) = 0; + virtual void on_thread_destroyed(sinsp_threadinfo* tinfo) = 0; +}; + +// +// Prototype of the callback invoked by the thread table when a thread is +// created or destroyed +// +class sinsp_fd_listener +{ +public: + virtual ~sinsp_fd_listener() + { + } + virtual void on_read(sinsp_evt* evt, int64_t tid, int64_t fd, sinsp_fdinfo_t* fdinfo, char *data, uint32_t original_len, uint32_t len) = 0; + virtual void on_write(sinsp_evt* evt, int64_t tid, int64_t fd, sinsp_fdinfo_t* fdinfo, char *data, uint32_t original_len, uint32_t len) = 0; + virtual void on_sendfile(sinsp_evt* evt, int64_t fdin, uint32_t len) = 0; + virtual void on_connect(sinsp_evt* evt, uint8_t* packed_data) = 0; + virtual void on_accept(sinsp_evt* evt, int64_t newfd, uint8_t* packed_data, sinsp_fdinfo_t* new_fdinfo) = 0; + virtual void on_file_open(sinsp_evt* evt, const string& fullpath, uint32_t flags) = 0; + virtual void on_error(sinsp_evt* evt) = 0; + virtual void on_erase_fd(erase_fd_params* params) = 0; + virtual void on_socket_shutdown(sinsp_evt *evt) = 0; + virtual void on_execve(sinsp_evt* evt) = 0; + virtual void on_clone(sinsp_evt* evt, sinsp_threadinfo* newtinfo) = 0; + virtual void on_bind(sinsp_evt* evt) = 0; + virtual bool on_resolve_container(sinsp_container_manager* manager, sinsp_threadinfo* tinfo, bool query_os_for_missing_info) = 0; + virtual void on_socket_status_changed(sinsp_evt *evt) = 0; +}; diff --git a/userspace/libsinsp/sinsp_pd_callback_type.h b/userspace/libsinsp/sinsp_pd_callback_type.h new file mode 100644 index 0000000000..06c17afc0d --- /dev/null +++ b/userspace/libsinsp/sinsp_pd_callback_type.h @@ -0,0 +1,32 @@ +/* +Copyright (C) 2019 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +// +// Protocol decoder callback type +// +typedef enum sinsp_pd_callback_type +{ + CT_OPEN, + CT_CONNECT, + CT_READ, + CT_WRITE, + CT_TUPLE_CHANGE, +}sinsp_pd_callback_type; diff --git a/userspace/libsinsp/sinsp_public.h b/userspace/libsinsp/sinsp_public.h new file mode 100644 index 0000000000..fdd9896397 --- /dev/null +++ b/userspace/libsinsp/sinsp_public.h @@ -0,0 +1,43 @@ +/* +Copyright (C) 2018-2019 Sysdig, Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#ifdef _WIN32 +#define SINSP_PUBLIC __declspec(dllexport) +#else +#define SINSP_PUBLIC +#endif + +#ifndef ASSERT +#ifdef _DEBUG + +#ifdef ASSERT_TO_LOG +#define ASSERT(X) \ + if(!(X)) \ + { \ + g_logger.format(sinsp_logger::SEV_ERROR, "ASSERTION %s at %s:%d", #X , __FILE__, __LINE__); \ + assert(X); \ + } +#else +#define ASSERT(X) assert(X); +#endif // ASSERT_TO_LOG +#else // _DEBUG +#define ASSERT(X) +#endif // _DEBUG +#endif // ASSERT diff --git a/userspace/libsinsp/sinsp_signal.h b/userspace/libsinsp/sinsp_signal.h index 70ad66782f..ea0468865a 100644 --- a/userspace/libsinsp/sinsp_signal.h +++ b/userspace/libsinsp/sinsp_signal.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #define SE_NSIG 64 diff --git a/userspace/libsinsp/sinsp_test.cpp b/userspace/libsinsp/sinsp_test.cpp index 2fbe1255c4..b5c327489e 100644 --- a/userspace/libsinsp/sinsp_test.cpp +++ b/userspace/libsinsp/sinsp_test.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #include @@ -35,4 +36,4 @@ TEST(inspector,get_proc_by_valid_tid) inspector.m_proctable[-100] = newpi; EXPECT_TRUE(NULL != inspector.get_process(-100)); -} \ No newline at end of file +} diff --git a/userspace/libsinsp/socket_collector.h b/userspace/libsinsp/socket_collector.h new file mode 100644 index 0000000000..720bb96261 --- /dev/null +++ b/userspace/libsinsp/socket_collector.h @@ -0,0 +1,391 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// socket_collector.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#ifdef HAS_CAPTURE + +#include "socket_handler.h" + +template +class socket_collector +{ +public: + typedef std::map> socket_map_t; + + socket_collector(bool do_loop = false, long timeout_ms = 1000L): + m_nfds(0), + m_loop(do_loop), + m_timeout_ms(timeout_ms), + m_stopped(false) + { + clear_fds(); + } + + ~socket_collector() + { + } + + void add(std::shared_ptr handler) + { + if(handler) + { + int sockfd = handler->get_socket(m_timeout_ms); + m_sockets[sockfd] = handler; + g_logger.log("Socket collector: handler [" + handler->get_id() + + "] added socket (" + std::to_string(sockfd) + ')', + sinsp_logger::SEV_TRACE); + } + else + { + g_logger.log("Socket collector: attempt to add null handler.", + sinsp_logger::SEV_ERROR); + } + } + + bool is_enabled(std::shared_ptr handler) const + { + if(handler) + { + return handler->is_enabled(); + } + return false; + } + + void enable(std::shared_ptr handler) + { + if(handler) + { + handler->enable(); + return; + } + g_logger.log("Socket collector: attempt to enable non-existing handler.", + sinsp_logger::SEV_ERROR); + } + + int get_socket(std::shared_ptr handler) const + { + for(const auto& http : m_sockets) + { + if(http.second == handler) + { + return http.first; + } + } + return -1; + } + + bool has(std::shared_ptr handler) + { + for(const auto& http : m_sockets) + { + if(http.second == handler) + { + return true; + } + } + return false; + } + + bool remove(std::shared_ptr handler) + { + for(typename socket_map_t::iterator it = m_sockets.begin(); it != m_sockets.end(); ++it) + { + if(it->second == handler) + { + remove(it); + return true; + } + } + return false; + } + + void remove_all() + { + clear_fds(); + m_sockets.clear(); + m_nfds = 0; + } + + int subscription_count() const + { + return m_sockets.size(); + } + + int signaled_sockets_count() + { + int count = 0; + for(typename socket_map_t::iterator it = m_sockets.begin(); it != m_sockets.end(); ++it) + { + if(FD_ISSET(it->first, &m_infd)) + { + ++count; + } + } + return count; + } + + void trace_sockets() + { + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + for(typename socket_map_t::iterator it = m_sockets.begin(); it != m_sockets.end(); ++it) + { + if(it->second) + { + if(it->second->is_enabled()) + { + g_logger.log("Socket collector: examining socket " + std::to_string(it->first) + + " (" + it->second->get_id() + ')', sinsp_logger::SEV_TRACE); + if(FD_ISSET(it->first, &m_infd)) + { + g_logger.log("Socket collector: activity on socket " + std::to_string(it->first) + + " (" + it->second->get_id() + ')', sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("Socket collector: socket " + std::to_string(it->first) + + " handler (" + it->second->get_id() + ") is not enabled.", + sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("Socket collector: socket " + std::to_string(it->first) + + " handler (" + it->second->get_id() + ") is null.", + sinsp_logger::SEV_TRACE); + } + } + } + } + + bool is_fd_valid(int sockfd) + { + struct timeval tv = {0}; + fd_set infd, outfd, errfd; + FD_ZERO(&infd); + FD_ZERO(&outfd); + FD_ZERO(&errfd); + FD_SET(sockfd, &infd); + FD_SET(sockfd, &outfd); + FD_SET(sockfd, &errfd); + return 0 <= select(sockfd + 1, &infd, &outfd, &errfd, &tv); + } + + void enable_sockets() + { + clear_fds(); + for(typename socket_map_t::iterator it = m_sockets.begin(); it != m_sockets.end();) + { + int sockfd = -1; + if(it->second) + { + if(it->second->is_enabled()) + { + if(!is_fd_valid(it->first)) + { + remove(it); + continue; + } + else + { + sockfd = it->first; + FD_SET(sockfd, &m_infd); + FD_SET(sockfd, &m_errfd); + if(sockfd > m_nfds) + { + m_nfds = sockfd; + } + } + } + } + ++it; + } + } + + void get_data() + { + try + { + struct timeval tv; + int res; + m_stopped = false; + while(!m_stopped) + { + tv.tv_sec = m_loop ? m_timeout_ms / 1000 : 0; + tv.tv_usec = m_loop ? (m_timeout_ms % 1000) * 1000 : 0; + { + if(m_sockets.size()) + { + enable_sockets(); // flag all enabled handler sockets + g_logger.log("Socket collector: total sockets=" + std::to_string(m_sockets.size()) + + ", select-enabled sockets= " + std::to_string(signaled_sockets_count()), + sinsp_logger::SEV_TRACE); + res = select(m_nfds + 1, &m_infd, NULL, &m_errfd, &tv); + g_logger.log("Socket collector: total sockets=" + std::to_string(m_sockets.size()) + + ", signaled sockets= " + std::to_string(signaled_sockets_count()), + sinsp_logger::SEV_TRACE); + if(res == 0) // all quiet + { + g_logger.log("Socket collector: " + std::to_string(m_sockets.size()) + " sockets total, no activity.", + sinsp_logger::SEV_DEBUG); + } + else if(res < 0) // select error + { + // socket sets are undefined after error, nothing to do here ... + throw sinsp_exception(std::string("Socket collector: select error (").append(strerror(errno)).append(1, ')')); + } + else // data available or socket error + { + trace_sockets(); + for(typename socket_map_t::iterator it = m_sockets.begin(); it != m_sockets.end();) + { + std::string id = it->second->get_id(); + int err = 0; + if(FD_ISSET(it->first, &m_infd)) + { + if(it->second && it->second->is_enabled() && (err = it->second->on_data())) + { + if((err != EAGAIN) && (err != EINPROGRESS)) + { + if(err != it->second->CONNECTION_CLOSED) + { + g_logger.log("Socket collector: data handling error " + std::to_string(errno) + ", (" + + strerror(errno) + "), removing handler [" + id + ']', sinsp_logger::SEV_ERROR); + } + else + { + g_logger.log("Socket collector: connection close detected while handling data" + ", removing handler [" + id + ']', sinsp_logger::SEV_DEBUG); + } + remove(it); + continue; + } + } + } + + if(FD_ISSET(it->first, &m_errfd)) + { + if(it->second && (err = it->second->get_socket_error())) + { + g_logger.log("Socket collector: socket error " + std::to_string(err) + ", (" + + strerror(err) + "), removing handler [" + id + ']', sinsp_logger::SEV_ERROR); + } + else + { + g_logger.log("Socket collector: handler [" + id + "] unknown socket error, closing connection.", + sinsp_logger::SEV_ERROR); + } + remove(it); + continue; + } + ++it; + } + } + } + else + { + g_logger.log("Socket collector is empty.", sinsp_logger::SEV_DEBUG); + m_stopped = true; + return; + } + } + if(!m_loop) { break; } + } + } + catch(const std::exception& ex) + { + g_logger.log(std::string("Socket collector error: ") + ex.what(), sinsp_logger::SEV_ERROR); + remove_all(); + m_stopped = true; + } + } + + void stop() + { + m_stopped = true; + } + + bool is_active() const + { + return subscription_count() > 0; + } + + bool is_healthy(std::shared_ptr handler) const + { + if(m_steady_state) + { + return get_socket(handler) != -1; + } + return true; + } + + // flag indicating collector passed through the + // transitional state (if any), where sockets and + // handlers may come and go + void set_steady_state(bool state = true) + { + m_steady_state = true; + } + + bool get_steady_state() const + { + return m_steady_state; + } + +private: + void clear_fds() + { + FD_ZERO(&m_errfd); + FD_ZERO(&m_infd); + } + + typename socket_map_t::iterator& remove(typename socket_map_t::iterator& it) + { + if(it != m_sockets.end()) + { + m_sockets.erase(it++); + } + m_nfds = 0; + for(const auto& sock : m_sockets) + { + if(sock.first > m_nfds) + { + m_nfds = sock.first; + } + } + return it; + } + + socket_map_t m_sockets; + fd_set m_infd; + fd_set m_errfd; + int m_nfds = 0; + bool m_loop = false; + long m_timeout_ms; + bool m_stopped = false; + bool m_steady_state = false; +}; + +#endif // HAS_CAPTURE +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/socket_handler.h b/userspace/libsinsp/socket_handler.h new file mode 100644 index 0000000000..b797069236 --- /dev/null +++ b/userspace/libsinsp/socket_handler.h @@ -0,0 +1,1641 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +// +// socket_handler.h +// +#ifndef MINIMAL_BUILD +#pragma once + +#ifdef HAS_CAPTURE + +#include "ares.h" +#include "addrinfo.h" +#include "http_parser.h" +#include "uri.h" +#include "json/json.h" +#define BUFFERSIZE 512 // b64 needs this macro +#include "b64/encode.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "sinsp_auth.h" +#include "http_reason.h" +#include "json_query.h" +#include +#include +#include +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef SOCK_NONBLOCK +#define SOCK_NONBLOCK 0 +#endif + +template +class socket_data_handler +{ +public: + typedef std::shared_ptr ptr_t; + typedef std::shared_ptr json_ptr_t; + typedef sinsp_ssl::ptr_t ssl_ptr_t; + typedef sinsp_bearer_token::ptr_t bt_ptr_t; + typedef void (T::*json_callback_func_t)(json_ptr_t, const std::string&); + + static const std::string HTTP_VERSION_10; + static const std::string HTTP_VERSION_11; + static const int CONNECTION_CLOSED = ~0; + + socket_data_handler(T& obj, + const std::string& id, + const std::string& url, + const std::string& path = "", + const std::string& http_version = HTTP_VERSION_11, + int timeout_ms = 1000, + ssl_ptr_t ssl = nullptr, + bt_ptr_t bt = nullptr, + bool keep_alive = true, + bool blocking = false, + unsigned data_limit = 524288u, + bool fetching_state = true): m_obj(obj), + m_id(id), + m_url(url), + m_keep_alive(keep_alive ? std::string("Connection: keep-alive\r\n") : std::string()), + m_path(path.empty() ? m_url.get_path() : path), + m_blocking(blocking), + m_ssl(ssl), + m_bt(bt), + m_timeout_ms(timeout_ms), + m_request(make_request(url, http_version)), + m_http_version(http_version), + m_data_limit(data_limit), + m_fetching_state(fetching_state) + + { + g_logger.log(std::string("Creating Socket handler object for (" + id + ") " + "[" + uri(url).to_string(false) + ']'), sinsp_logger::SEV_DEBUG); + m_buf.resize(1024); + init_http_parser(); + } + + virtual ~socket_data_handler() + { + cleanup(); + } + + virtual int get_socket(long timeout_ms = -1) + { + if(timeout_ms != -1) + { + m_timeout_ms = timeout_ms; + } + + if(m_socket < 0 || !m_connected) + { + connect_socket(); + } + + return m_socket; + } + + virtual bool is_connected() const + { + return m_connected; + } + + bool is_connecting() + { + if(m_address.empty()) + { + try_resolve(); + } + if(!m_connected && m_sa && m_sa_len) + { + try_connect(); + } + return m_connecting; + } + + void close_on_chunked_end(bool close = true) + { + m_close_on_chunked_end = close; + } + + const uri& get_url() const + { + return m_url; + } + + void set_path(const std::string& path) + { + m_path = path; + m_request = make_request(m_url, m_http_version); + } + + std::string make_request(uri url, const std::string& http_version) + { + std::ostringstream request; + std::string host_and_port = url.get_host(); + if(!host_and_port.empty()) + { + int port = url.get_port(); + if(port) + { + host_and_port.append(1, ':').append(std::to_string(port)); + } + } + request << "GET " << m_path; + std::string query = url.get_query(); + if(!query.empty()) + { + request << '?' << query; + } + request << " HTTP/" << http_version << "\r\n" << m_keep_alive << "User-Agent: sysdig\r\n"; + if(!host_and_port.empty()) + { + request << "Host: " << host_and_port << "\r\n"; + } + request << "Accept: */*\r\n"; + std::string creds = url.get_credentials(); + if(!creds.empty()) + { + uri::decode(creds); + std::istringstream is(creds); + std::ostringstream os; + base64::encoder().encode(is, os); + std::string bauth = os.str(); + request << "Authorization: Basic " << trim(bauth) << "\r\n"; + } + if(m_bt && !m_bt->get_token().empty()) + { + request << "Authorization: Bearer " << m_bt->get_token() << "\r\n"; + } + request << "\r\n"; + + return request.str(); + } + + void set_id(const std::string& id) + { + m_id = id; + } + + const std::string& get_id() const + { + return m_id; + } + + void set_json_callback(json_callback_func_t f) + { + m_json_callback = f; + } + + SSL* ssl_connection() + { + return m_ssl_connection; + } + + bool wants_send() const + { + return m_wants_send; + } + + void send_request() + { + m_wants_send = false; // no matter what happens, this is a one-shot + if(m_request.empty()) + { + throw sinsp_exception("Socket handler (" + m_id + ") send: request (empty)."); + } + + if(m_socket <= 0) + { + throw sinsp_exception("Socket handler (" + m_id + ") send: invalid socket."); + } + + int iolen = 0; + if(m_request.size()) + { + g_logger.log("Socket handler (" + m_id + ") socket=" + std::to_string(m_socket) + + ", m_ssl_connection=" + std::to_string((int64_t)m_ssl_connection), sinsp_logger::SEV_TRACE); + std::string req = m_request; + time_t then; time(&then); + while(req.size()) + { + if(m_url.is_secure()) + { + if(!m_ssl_connection) + { + throw sinsp_exception("Socket handler (" + m_id + ") send: SSL connection is null."); + } + iolen = SSL_write(m_ssl_connection, m_request.c_str(), m_request.size()); + } + else + { + iolen = send(m_socket, m_request.c_str(), m_request.size(), 0); + } + if(iolen == static_cast(req.size())) { break; } + else if(iolen == 0 || errno == ENOTCONN || errno == EPIPE) + { + goto connection_closed; + } + else if(iolen < 0) + { + if(errno == ENOTCONN || errno == EPIPE) + { + goto connection_closed; + } + else if(errno != EAGAIN && errno != EWOULDBLOCK) + { + goto connection_error; + } + if(m_url.is_secure()) + { + int err = SSL_get_error(m_ssl_connection, iolen); + if(err != SSL_ERROR_WANT_WRITE && err != SSL_ERROR_WANT_READ) + { + goto connection_error; + } + } + } + else { req.erase(0, iolen); } + time_t now; time(&now); + if(difftime(now, then) > m_timeout_ms * 1000) + { + throw sinsp_exception("Socket handler (" + m_id + "): send timeout."); + } + } + } + else + { + throw sinsp_exception("Socket handler (" + m_id + ") request is empty."); + } + g_logger.log(m_request, sinsp_logger::SEV_TRACE); + return; + + connection_error: + { + std::string err = strerror(errno); + std::ostringstream os; + os << "Socket handler (" << m_id << ") send_request(), connection [" << m_url.to_string(false) << "] error: " << err; + if(m_url.is_secure()) + { + std::string ssl_err = ssl_errors(); + if(!ssl_err.empty()) + { + os << std::endl << "SSL error: " << ssl_err; + } + } + throw sinsp_exception(os.str()); + } + + connection_closed: + { + std::ostringstream os; + os << "Socket handler (" << m_id << ") send_request(), connection [" << m_url.to_string(false) << "] closed."; + if(m_url.is_secure()) + { + std::string ssl_err = ssl_errors(); + if(!ssl_err.empty()) + { + os << std::endl << "SSL error: " << ssl_err; + } + } + m_connecting = false; + m_connected = false; + throw sinsp_exception(os.str()); + } + } + + void set_socket_option(int opt) + { + int flags = fcntl(m_socket, F_GETFL, 0); + if(flags != -1) + { + fcntl(m_socket, F_SETFL, flags | opt); + } + else + { + throw sinsp_exception("Socket handler (" + m_id + + ") error while setting socket option (" + + std::to_string(opt) + "): " + strerror(errno)); + } + } + + int get_all_data() + { + g_logger.log("Socket handler (" + m_id + ") Retrieving all data in blocking mode ...", + sinsp_logger::SEV_TRACE); + ssize_t rec = 0; + std::vector buf(1024, 0); + int counter = 0; + int processed = 0; + init_http_parser(); + do + { + int count = 0; + int ioret = ioctl(m_socket, FIONREAD, &count); + if(ioret >= 0 && count > 0) + { + buf.resize(count); + if(m_url.is_secure()) + { + rec = SSL_read(m_ssl_connection, &buf[0], buf.size()); + } + else + { + rec = recv(m_socket, &buf[0], buf.size(), 0); + } + if(rec > 0) + { + process(&buf[0], rec, false); + processed += rec; + } + else if(rec == 0) + { + throw sinsp_exception("Socket handler (" + m_id + "): Connection closed."); + } + else if(rec < 0) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + strerror(errno)); + } + //g_logger.log("Socket handler (" + m_id + ") received=" + std::to_string(rec) + + // "\n\n" + data + "\n\n", sinsp_logger::SEV_TRACE); + } + + // To prevent reads from entirely stalling (like in gigantic k8s + // environments), give up after reading 30mb. + ++counter; + if(processed > 30 * 1024 * 1024) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + "read more than 30MB of data from " + m_url.to_string(false) + m_path + + " (" + std::to_string(processed) + " bytes, " + std::to_string(counter) + " reads). Giving up"); + } + else { usleep(10000); } + } while(!m_msg_completed); + init_http_parser(); + return processed; + } + + void data_handling_error(const std::string& data, size_t nparsed) + { + std::ostringstream os; + os << "Socket handler (" << m_id + ") an error occurred during http parsing. " + "processed=" << nparsed << ", expected=" << data.size() << ", status_code=" << + std::to_string(m_http_parser->status_code) << ", http_errno=" << + std::to_string(m_http_parser->http_errno) << "data:" << std::endl << data; + throw sinsp_exception(os.str()); + } + + void parse_http(char* data, size_t len) + { + size_t nparsed = http_parser_execute(m_http_parser, &m_http_parser_settings, data, len); + if(nparsed != len) { data_handling_error(std::string(data, len), nparsed); } + } + + void process_json() + { + if(m_json_filters.empty()) { add_json_filter("."); } + bool handled = false; + for(auto js = m_json.begin(); js != m_json.end();) + { + handled = false; + for(auto it = m_json_filters.cbegin(); it != m_json_filters.cend(); ++it) + { + json_ptr_t pjson = try_parse(m_jq, *js, *it, m_id, m_url.to_string(false)); + if(pjson) + { + (m_obj.*m_json_callback)(pjson, m_id); + handled = true; + break; + } + } + if(!handled) + { + g_logger.log("Socket handler: (" + m_id + ") JSON not handled, " + "discarding:\n" + *js, sinsp_logger::SEV_ERROR); + } + js = m_json.erase(js); + } + } + + int process(char* data, size_t len, bool reinit = true) + { + if(len) + { + parse_http(data, len); + unsigned parser_errno = HTTP_PARSER_ERRNO(m_http_parser); + if(parser_errno != HPE_OK) + { + if(parser_errno <= HPE_UNKNOWN) + { + g_logger.log("Socket handler (" + m_id + ") http parser error " + std::to_string(parser_errno) + " ([" + + http_errno_name((http_errno) parser_errno) + "]: " + + http_errno_description((http_errno) parser_errno) + ')', + sinsp_logger::SEV_ERROR); + } + else + { + g_logger.log("Socket handler (" + m_id + ") http parser error " + std::to_string(parser_errno) + ')', + sinsp_logger::SEV_ERROR); + } + return CONNECTION_CLOSED; + } + if(m_json.size()) { process_json(); } + if(m_http_response >= 400) + { + g_logger.log("Socket handler (" + m_id + ") response " + std::to_string(m_http_response) + + " (" + get_http_reason(m_http_response) + ") received, disconnecting ... ", + sinsp_logger::SEV_ERROR); + return CONNECTION_CLOSED; + } + if(m_msg_completed) + { + if(m_data_buf.size()) // should never happen + { + g_logger.log("Socket handler (" + m_id + ") response ended with unprocessed data, " + "clearing and sending new request ... ", sinsp_logger::SEV_WARNING); + ASSERT(!m_data_buf.size()); + m_data_buf.clear(); + } + // In HTTP 1.1 connections with chunked transfer, this socket may never be closed by server, + // (K8s API server is an example of such behavior), in which case the chunked data will just + // stop flowing. We can keep the good socket and resend the request instead of severing the + // connection. The m_wants_send flag has to be checked by the caller and request re-sent, otherwise + // this pipeline will remain idle. To force client-initiated socket close on chunked transfer end, + // set the m_close_on_chunked_end flag to true (default). + if(m_close_on_chunked_end) + { + g_logger.log("Socket handler (" + m_id + ") chunked response ended", + sinsp_logger::SEV_DEBUG); + return CONNECTION_CLOSED; + } + m_wants_send = true; + if(reinit) { init_http_parser(); } + } + } + return 0; + } + + int on_data() + { + bool is_error = false; + + if(!m_json_callback) + { + throw sinsp_exception("Socket handler (" + m_id + "): cannot parse data (callback is null)."); + } + + ssize_t iolen = 0; + size_t len_read = 0, len_to_read = m_buf.size(); + try + { + do + { + if(len_read >= m_data_limit) { break; } + else if((len_read + m_buf.size()) > m_data_limit) + { + len_to_read = m_data_limit - len_read; + } + errno = 0; + if(m_url.is_secure()) + { + iolen = static_cast(SSL_read(m_ssl_connection, &m_buf[0], len_to_read)); + } + else + { + iolen = recv(m_socket, &m_buf[0], len_to_read, 0); + } + if(iolen > 0) { len_read += iolen; } + m_sock_err = errno; + sinsp_logger::severity sev = (iolen < 0 && m_sock_err != EAGAIN) ? + sinsp_logger::SEV_DEBUG : sinsp_logger::SEV_TRACE; + g_logger.log("Socket handler (" + m_id + ") " + m_url.to_string(false) + ", iolen=" + + std::to_string(iolen) + ", data=" + std::to_string(len_read) + " bytes, " + "errno=" + std::to_string(m_sock_err) + " (" + strerror(m_sock_err) + ')', + sev); + /* uncomment to see raw HTTP stream data in trace logs + if((iolen > 0) && g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("Socket handler (" + m_id + "), data --->" + std::string(&m_buf[0], iolen) + "<--- data", + sinsp_logger::SEV_TRACE); + } + */ + if(iolen > 0) + { + size_t len = (iolen <= static_cast(m_buf.size())) ? static_cast(iolen) : m_buf.size(); + if(CONNECTION_CLOSED == process(&m_buf[0], len)) + { + return CONNECTION_CLOSED; + } + } + else if(iolen == 0 || m_sock_err == ENOTCONN || m_sock_err == EPIPE) + { + if(m_url.is_secure()) + { + if(m_ssl_connection) + { + int err = SSL_get_error(m_ssl_connection, iolen); + if (err != SSL_ERROR_ZERO_RETURN) + { + g_logger.log("Socket handler(" + m_id + "): SSL conn closed with code " + + std::to_string(err), + sinsp_logger::SEV_DEBUG); + } + + int sd = SSL_get_shutdown(m_ssl_connection); + if(sd == 0) + { + g_logger.log("Socket handler (" + m_id + "): SSL zero bytes received, " + "but no shutdown state set for [" + m_url.to_string(false) + "]: ", + sinsp_logger::SEV_WARNING); + } + if(sd & SSL_RECEIVED_SHUTDOWN) + { + g_logger.log("Socket handler(" + m_id + "): SSL shutdown from [" + + m_url.to_string(false) + "]: ", sinsp_logger::SEV_TRACE); + } + if(sd & SSL_SENT_SHUTDOWN) + { + g_logger.log("Socket handler(" + m_id + "): SSL shutdown sent to [" + + m_url.to_string(false) + "]: ", sinsp_logger::SEV_TRACE); + } + } + else + { + g_logger.log("Socket handler(" + m_id + "): SSL connection is null", + sinsp_logger::SEV_WARNING); + } + } + goto connection_closed; + } + else if(iolen < 0) + { + if(m_sock_err == ENOTCONN || m_sock_err == EPIPE) + { + goto connection_closed; + } + else if(m_sock_err != EAGAIN && m_sock_err != EWOULDBLOCK) + { + goto connection_error; + } + if(m_url.is_secure()) + { + int err = SSL_get_error(m_ssl_connection, iolen); + if(err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) + { + g_logger.log("Socket handler(" + m_id + "): received SSL error" + + std::to_string(err), + sinsp_logger::SEV_ERROR); + goto connection_error; + } + } + } + } while(iolen && (m_sock_err != EAGAIN) && (len_read < m_data_limit)); + g_logger.log("Socket handler (" + m_id + ") " + + std::to_string(len_read) + " bytes of data received", + sinsp_logger::SEV_TRACE); + } + catch(const sinsp_exception& ex) + { + g_logger.log(std::string("Socket handler (" + m_id + ") data receive error [" + + m_url.to_string(false) + "]: ").append(ex.what()), + sinsp_logger::SEV_ERROR); + return m_sock_err; + } + return 0; + + connection_error: + is_error = true; + + connection_closed: + if(m_url.is_secure()) + { + std::string ssl_err = ssl_errors(); + if(!ssl_err.empty()) + { + g_logger.log(ssl_err, sinsp_logger::SEV_ERROR); + } + } + return is_error ? m_sock_err : CONNECTION_CLOSED; + } + + void on_error(const std::string& /*err*/, bool /*disconnect*/) + { + } + + bool has_json_filter(const std::string& filter) + { + for(auto flt : m_json_filters) + { + if(flt == filter) + { + return true; + } + } + return false; + } + + void add_json_filter(const std::string& filter, const std::string& before_filter = "") + { + if(filter.empty()) + { + throw sinsp_exception(std::string("Socket handler (") + m_id + "), " + "[" + m_url.to_string(false) + "] " + "attempt to add empty filter"); + } + remove_json_filter(filter); + if(before_filter.empty()) + { + m_json_filters.push_back(filter); + return; + } + else + { + auto it = m_json_filters.begin(); + for(; it != m_json_filters.end(); ++it) + { + if(*it == before_filter) { break; } + } + if(it == m_json_filters.end()) + { + g_logger.log("Socket handler (" + m_id + "), [" + m_url.to_string(false) + "] " + "attempt to insert filter before a non-existing filter. " + "Filter will be added to the end of filter list.", sinsp_logger::SEV_WARNING); + } + m_json_filters.insert(it, filter); + } + } + + void remove_json_filter(const std::string& filter) + { + for(auto it = m_json_filters.begin(); it != m_json_filters.end(); ++it) + { + if(*it == filter) + { + m_json_filters.erase(it); + return; + } + } + } + + void replace_json_filter(const std::string& from, const std::string& to) + { + for(auto it = m_json_filters.begin(); it != m_json_filters.end(); ++it) + { + if(*it == from) + { + *it = to; + return; + } + } + throw sinsp_exception(std::string("Socket handler (") + m_id + "), " + "[" + m_url.to_string(false) + "] " + "attempt to replace non-existing filter"); + } + + void print_filters(sinsp_logger::severity sev = sinsp_logger::SEV_DEBUG) + { + std::ostringstream filters; + filters << std::endl << "Filters:" << std::endl; + for(auto filter : m_json_filters) + { + filters << filter << std::endl; + } + g_logger.log("Socket handler (" + m_id + "), [" + m_url.to_string(false) + "]" + filters.str(), sev); + } + + static json_ptr_t try_parse(json_query& jq, const std::string& json, const std::string& filter, + const std::string& id, const std::string& url) + { + std::string filtered_json(json); + if(!filter.empty()) + { + // failure to parse is ok, it will fail over to the next filter + // and log error if all filters fail + if(jq.process(json, filter)) + { + filtered_json = jq.result(); + if (filtered_json.empty() && !jq.get_error().empty()) + { + g_logger.log("Socket handler (" + id + "), [" + + url + "] filter result is empty \"" + + jq.get_error() + "\"; JSON: <" + + json + ">, jq filter: <" + filter + '>', + sinsp_logger::SEV_DEBUG); + } + } + else + { + g_logger.log("Socket handler (" + id + "), [" + + url + "] filter processing error \"" + + jq.get_error() + "\"; JSON: <" + + json + ">, jq filter: <" + filter + '>', + sinsp_logger::SEV_DEBUG); + return nullptr; + } + } + json_ptr_t root(new Json::Value()); + try + { + if(Json::Reader().parse(filtered_json, *root)) + { + /* + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("Socket handler (" + id + "), [" + url + "] " + "filtered JSON: " + json_as_string(*root), + sinsp_logger::SEV_TRACE); + } + */ + return root; + } + } + catch(...) { } + g_logger.log("Socket handler (" + id + "), [" + url + "] parsing error; JSON: <" + + json + ">, jq filter: <" + filter + '>', sinsp_logger::SEV_ERROR); + return nullptr; + } + + // when connection is non-blocking and a socket + // should not be polled until it is connected + // this flag indicates readiness to be polled + bool is_enabled() const + { + return m_enabled; + } + + void enable(bool e = true) + { + m_enabled = e; + } + + bool connection_error() const + { + return m_connection_error; + } + + int get_socket_error() + { + socklen_t optlen = sizeof(m_sock_err); + int ret = getsockopt(m_socket, SOL_SOCKET, SO_ERROR, &m_sock_err, &optlen); + g_logger.log("Socket handler (" + m_id + ") getsockopt() ret=" + + std::to_string(ret) + ", m_sock_err=" + std::to_string(m_sock_err) + + " (" + strerror(m_sock_err) + ')', + sinsp_logger::SEV_TRACE); + if(!ret) { return m_sock_err; } + throw sinsp_exception("Socket handler (" + m_id + ") an error occurred " + "trying to obtain socket status while connecting to " + + m_url.to_string(false) + ": " + strerror(ret)); + } + +private: + + typedef std::vector password_vec_t; + + int wait(bool for_recv, long tout = 1000L) + { + struct timeval tv; + tv.tv_sec = m_timeout_ms / 1000; + tv.tv_usec = (m_timeout_ms % 1000) * 1000; + + fd_set infd, outfd, errfd; + FD_ZERO(&infd); + FD_ZERO(&outfd); + FD_ZERO(&errfd); + FD_SET(m_socket, &errfd); + if(for_recv) + { + FD_SET(m_socket, &infd); + } + else + { + FD_SET(m_socket, &outfd); + } + + return select(m_socket + 1, &infd, &outfd, &errfd, &tv); + } + + bool send_ready() + { + struct timeval tv = {0}; + fd_set outfd; + FD_ZERO(&outfd); + FD_SET(m_socket, &outfd); + int sel_ret = select(m_socket + 1, 0, &outfd, 0, &tv); + if(sel_ret != 1) { return false; } + int sock_ret = get_socket_error(); + if(!sock_ret) { return true; } + return false; + } + + bool recv_ready() + { + struct timeval tv = {0}; + fd_set infd; + FD_ZERO(&infd); + FD_SET(m_socket, &infd); + return select(m_socket + 1, &infd, 0, 0, &tv) == 1; + } + + bool socket_error() + { + struct timeval tv = {0}; + fd_set errfd; + FD_ZERO(&errfd); + FD_SET(m_socket, &errfd); + return select(m_socket + 1, 0, 0, &errfd, &tv) == 1; + } + + static int ssl_verify_callback(int preverify_ok, X509_STORE_CTX* ctx) + { + SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); + if(ssl) + { + char buf[256] = {0}; + X509* err_cert = X509_STORE_CTX_get_current_cert(ctx); + X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256); + + if(preverify_ok && SSL_get_verify_result(ssl) == X509_V_OK) + { + g_logger.log("Socket handler SSL CA verified: " + std::string(buf), + sinsp_logger::SEV_DEBUG); + return 1; + } + else + { + int err = X509_STORE_CTX_get_error(ctx); + int depth = X509_STORE_CTX_get_error_depth(ctx); + g_logger.log("Socket handler SSL CA verify error: num=" + std::to_string(err) + + ':' + X509_verify_cert_error_string(err) + + ":depth=" + std::to_string(depth) + + ':' + std::string(buf), sinsp_logger::SEV_ERROR); + return 0; + } + } + return 0; + } + + static int ssl_no_verify_callback(int, X509_STORE_CTX* ctx) + { + g_logger.log("Socket handler SSL CA verification disabled, certificate accepted.", + sinsp_logger::SEV_DEBUG); + return 1; + } + + static int ssl_key_password_cb(char *buf, int size, int, void* pass) + { + if(pass) + { + std::memset(buf, 0, size); + int pass_len = static_cast(strlen((char*)pass)); + if(size < (pass_len) + 1) { return 0; } + strncpy(buf, (const char*)pass, pass_len); + return pass_len; + } + return 0; + } + + std::string ssl_errors() + { + std::ostringstream os; + if(m_url.is_secure()) + { + char errbuf[256] = {0}; + unsigned long err; + while((err = ERR_get_error()) != 0) + { + if(os.str().empty()) + { + os << "Socket handler (" << m_id << ", " + "socket=" << std::to_string(m_socket) << ") SSL errors:\n"; + } + os << ERR_error_string(err, errbuf) << std::endl; + } + } + return os.str(); + } + + void init_ssl_context(void) + { + if(!m_ssl_context) + { + SSL_library_init(); + SSL_load_error_strings(); + OpenSSL_add_all_algorithms(); + const SSL_METHOD* method = +#if (OPENSSL_VERSION_NUMBER < 0x10100000L) + TLSv1_2_client_method(); +#else + TLS_client_method(); +#endif + if(!method) + { + g_logger.log("Socket handler (" + m_id + "): Can't initialize SSL method\n" + ssl_errors(), + sinsp_logger::SEV_ERROR); + } + m_ssl_context = SSL_CTX_new(method); + if(!m_ssl_context) + { + g_logger.log("Socket handler (" + m_id + "): Can't initialize SSL context\n" + ssl_errors(), + sinsp_logger::SEV_ERROR); + return; + } + + if(m_ssl) + { + if(m_ssl->verify_peer()) + { + const std::string ca_cert = m_ssl->ca_cert(); + if(!ca_cert.empty() && !SSL_CTX_load_verify_locations(m_ssl_context, ca_cert.c_str(), 0)) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + "Can't load SSL CA certificate (" + ca_cert + ").\n" + + ssl_errors()); + } + else if(ca_cert.empty()) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + "Invalid SSL CA certificate configuration " + "(Verify Peer enabled but no CA certificate specified)."); + } + SSL_CTX_set_verify(m_ssl_context, SSL_VERIFY_PEER, ssl_verify_callback); + g_logger.log("Socket handler (" + m_id + "): CA verify set to PEER", sinsp_logger::SEV_TRACE); + } + else + { + SSL_CTX_set_verify(m_ssl_context, SSL_VERIFY_NONE, ssl_no_verify_callback); + g_logger.log("Socket handler (" + m_id + "): CA verify set to NONE", sinsp_logger::SEV_TRACE); + } + + const std::string& cert = m_ssl->cert(); + if(!cert.empty()) + { + if(SSL_CTX_use_certificate_file(m_ssl_context, cert.c_str(), SSL_FILETYPE_PEM) <= 0) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + "Can't load SSL certificate from " + cert + ".\n" + + ssl_errors()); + } + else + { + g_logger.log("Socket handler (" + m_id + "): using SSL certificate from " + cert, + sinsp_logger::SEV_TRACE); + } + const std::string& key = m_ssl->key(); + if(!key.empty()) + { + const std::string& pass = m_ssl->key_passphrase(); + if(!pass.empty()) + { + m_ssl_key_pass.assign(pass.begin(), pass.end()); + m_ssl_key_pass.push_back('\0'); + SSL_CTX_set_default_passwd_cb_userdata(m_ssl_context, (void*)&m_ssl_key_pass[0]); + SSL_CTX_set_default_passwd_cb(m_ssl_context, ssl_key_password_cb); + } + + if(SSL_CTX_use_PrivateKey_file(m_ssl_context, key.c_str(), SSL_FILETYPE_PEM) <= 0) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + "Can't load SSL private key from " + key + ".\n" + + ssl_errors()); + } + else + { + g_logger.log("Socket handler (" + m_id + "): using SSL private key from " + key, sinsp_logger::SEV_TRACE); + } + + if(!SSL_CTX_check_private_key(m_ssl_context)) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + "SSL private key (" + key + ") does not match public certificate (" + cert + ").\n" + + ssl_errors()); + } + else + { + g_logger.log("Socket handler (" + m_id + "): SSL private key " + key + " matches public certificate " + cert, + sinsp_logger::SEV_TRACE); + } + } + else + { + throw sinsp_exception("Socket handler (" + m_id + "): " + "Invalid SSL configuration: public certificate specified without private key."); + } + } + else + { + g_logger.log("Socket handler (" + m_id + "): SSL public certificate not provided.", sinsp_logger::SEV_TRACE); + } + } + } + } + + void init_ssl_socket() + { + if(m_socket != -1 && !m_ssl_init_complete) + { + if(m_url.is_secure()) + { + if(!m_ssl_context) { init_ssl_context(); } + if(m_ssl_context) + { + m_ssl_connection = SSL_new(m_ssl_context); + if(m_ssl_connection) + { + if(1 == SSL_set_fd(m_ssl_connection, m_socket)) + { + m_ssl_init_complete = true; + } + else + { + throw sinsp_exception("Socket handler " + m_id + " (" + m_url.to_string(false) + ") " + "error assigning socket to SSL connection: " + ssl_errors()); + } + } + else + { + throw sinsp_exception("Socket handler " + m_id + " (" + m_url.to_string(false) + ") " + "error obtaining socket: " + ssl_errors()); + } + } + else + { + throw sinsp_exception("Socket handler " + m_id + " (" + m_url.to_string(false) + ") " + "SSL context error : " + ssl_errors()); + } + } + } + } + + void create_socket() + { + int sock_type = SOCK_STREAM; + if(!m_blocking) + { + sock_type |= SOCK_NONBLOCK; + } + if(m_socket < 0) + { + if(m_url.is_file()) + { + m_socket = socket(PF_UNIX, sock_type, 0); + } + else + { + m_socket = socket(PF_INET, sock_type, 0); + } + if(m_socket < 0) + { + throw sinsp_exception("Socket handler " + m_id + " (" + m_url.to_string(false) + ") " + "error obtaining socket: " + strerror(errno)); + } + } + } + + bool check_connected() + { + if(!send_ready()) + { + if(m_sock_err && m_sock_err != EINPROGRESS) + { + m_connection_error = true; + throw sinsp_exception("Socket handler (" + m_id + ") an error occurred " + "while connecting to " + m_url.to_string(false) + ": " + strerror(m_sock_err)); + } + else if(m_sock_err == EINPROGRESS) + { + m_connection_error = false; + m_connecting = true; + m_connected = false; + } + return false; + } + return true; + } + + bool try_connect() + { + g_logger.log("Socket handler (" + m_id + ") try_connect() entry, m_connecting=" + std::to_string(m_connecting) + + ", m_connected=" + std::to_string(m_connected), sinsp_logger::SEV_TRACE); + if(m_connected) { return true; } + if(m_socket == -1) + { + create_socket(); + } + + if(!is_resolved()) + { + if(!try_resolve()) + { + return false; + } + } + + int ret = -1; + if(m_connection_error) + { + return false; + } + else + { + if(!check_connected()) + { + return false; + } + } + + g_logger.log("Socket handler (" + m_id + ") try_connect() middle, m_connecting=" + std::to_string(m_connecting) + + ", m_connected=" + std::to_string(m_connected), sinsp_logger::SEV_TRACE); + if(!m_connected) + { + g_logger.log("Socket handler (" + m_id + ") connecting to " + m_url.to_string(false) + + " (socket=" + std::to_string(m_socket) + ')', sinsp_logger::SEV_DEBUG); + if(!m_sa || !m_sa_len) + { + std::ostringstream os; + os << m_sa; + throw sinsp_exception("Socket handler (" + m_id + ") invalid state connecting to " + + m_url.to_string(false) + " (socket=" + std::to_string(m_socket) + "), " + "sa=" + os.str() + ", sa_len=" + std::to_string(m_sa_len)); + } + if(!m_connect_called) + { + ret = connect(m_socket, m_sa, m_sa_len); + m_connect_called = true; + if(ret < 0 && errno != EINPROGRESS) + { + throw sinsp_exception("Error during connection attempt to " + m_url.to_string(false) + + " (socket=" + std::to_string(m_socket) + + ", error=" + std::to_string(errno) + "): " + strerror(errno)); + } + else if(errno == EINPROGRESS) + { + m_connecting = true; + m_connected = false; + return false; + } + } + else + { + if(get_socket_error() == EINPROGRESS) + { + m_connecting = true; + m_connected = false; + return false; + } + } + if(m_url.is_secure()) + { + if(!m_ssl_init_complete) + { + init_ssl_socket(); + } + if(m_ssl_connection) + { + ret = SSL_connect(m_ssl_connection); + if(ret == 1) + { + m_connecting = false; + m_connected = true; + g_logger.log("Socket handler (" + m_id + "): " + "SSL connected to " + m_url.get_host(), + sinsp_logger::SEV_INFO); + g_logger.log("Socket handler (" + m_id + "): " + "SSL socket=" + std::to_string(m_socket) + ", " + "local port=" + std::to_string(get_local_port()), + sinsp_logger::SEV_DEBUG); + } + else + { + int err = SSL_get_error(m_ssl_connection, ret); + switch(err) + { + case SSL_ERROR_NONE: // 0 + break; + case SSL_ERROR_SSL: // 1 + throw sinsp_exception(ssl_errors()); + case SSL_ERROR_WANT_READ: // 2 + case SSL_ERROR_WANT_WRITE: // 3 + return false; + case SSL_ERROR_WANT_X509_LOOKUP: // 4 + break; + case SSL_ERROR_SYSCALL: // 5 + throw sinsp_exception("Socket handler (" + m_id + "), error " + std::to_string(err) + + " (" + strerror(errno) + ") while connecting to " + + m_url.get_host() + ':' + std::to_string(m_url.get_port())); + case SSL_ERROR_ZERO_RETURN: // 6 + cleanup(); + throw sinsp_exception("Socket handler (" + m_id + "), " + "error (connection closed) while connecting to " + + m_url.get_host() + ':' + std::to_string(m_url.get_port())); + case SSL_ERROR_WANT_CONNECT: // 7 + throw sinsp_exception("Socket handler (" + m_id + "), " + "error (the operation failed while attempting to connect " + "the transport) while connecting to " + + m_url.get_host() + ':' + std::to_string(m_url.get_port())); + case SSL_ERROR_WANT_ACCEPT: // 8 + throw sinsp_exception("Socket handler (" + m_id + "), " + "error (the operation failed while attempting to accept a" + " connection from the transport) while connecting to " + + m_url.get_host() + ':' + std::to_string(m_url.get_port())); + } + } + } + else + { + throw sinsp_exception("Socket handler (" + m_id + "): " + m_url.to_string(false) + + " SSL connection is null (" + strerror(errno) + ')'); + } + } + + g_logger.log("Socket handler (" + m_id + "): Connected: socket=" + std::to_string(m_socket) + + ", collecting data from " + m_url.to_string(false) + m_path, sinsp_logger::SEV_DEBUG); + + if(m_url.is_secure() && m_ssl && m_ssl->verify_peer()) + { + if(SSL_get_peer_certificate(m_ssl_connection)) + { + long err = SSL_get_verify_result(m_ssl_connection); + if(err != X509_V_OK && + err != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT && + err != X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) + { + throw sinsp_exception("Socket handler (" + m_id + "): " + m_url.to_string(false) + + " server certificate verification failed."); + } + } + } + m_connection_error = false; + m_connecting = false; + m_connected = true; + } + return true; + } + + bool is_resolved() const + { + return (!m_address.empty() && m_sa && m_sa_len) || m_url.is_file(); + } + + bool try_resolve() + { + if (is_resolved()) + { + return true; + } + else + { + if (inet_aton(m_url.get_host().c_str(), &m_serv_addr.sin_addr)) // IP address provided + { + m_address = m_url.get_host(); + } + else // name provided, resolve to IP address + { + m_serv_addr = {0}; + + if (!m_ares_cb_res.call) // first call, call async resolver + { + g_logger.log("Socket handler (" + m_id + ") resolving " + m_url.get_host(), + sinsp_logger::SEV_TRACE); + + ares_init_options(&m_ares_channel, &m_ares_opts, 0); + ares_gethostbyname(m_ares_channel, m_url.get_host().c_str(), AF_INET, ares_cb, &m_ares_cb_res); + m_ares_cb_res.call = true; + + return false; + } + else if (!m_ares_cb_res.done) + { + int nfds; + fd_set read_fds, write_fds; + nfds = ares_fds(m_ares_channel, &read_fds, &write_fds); + if (nfds == 0) + { + return false; + } + ares_process(m_ares_channel, &read_fds, &write_fds); + return false; + } + else // rest of the calls, check if address was resolved + { + if (m_ares_cb_res.address.empty()) + { + g_logger.log("Socket handler (" + m_id + "): " + m_url.get_host() + + " address not resolved yet.", + sinsp_logger::SEV_TRACE); + return false; + } + m_address = m_ares_cb_res.address; + m_serv_addr.sin_addr = m_ares_cb_res.addr; + } + } + } + m_serv_addr.sin_family = AF_INET; + m_serv_addr.sin_port = htons(m_url.get_port()); + m_sa = (sockaddr *)&m_serv_addr; + m_sa_len = sizeof(struct sockaddr_in); + return true; + } + + void connect_socket() + { + if(!m_sa || !m_sa_len) + { + if(m_url.is_file()) + { + if(m_url.get_path().length() > sizeof(m_file_addr.sun_path) - 1) + { + throw sinsp_exception("Invalid address (too long): [" + m_url.get_path() + ']'); + } + m_file_addr.sun_family = AF_UNIX; + strncpy(m_file_addr.sun_path, m_url.get_path().c_str(), m_url.get_path().length()); + m_file_addr.sun_path[sizeof(m_file_addr.sun_path) - 1]= '\0'; + m_sa = (sockaddr*)&m_file_addr; + m_sa_len = sizeof(struct sockaddr_un); + } + else if(m_url.is("https") || m_url.is("http")) + { + try_resolve(); + } + else + { + throw sinsp_exception("Socket handler (" + m_id + "): " + + m_url.get_scheme() + " protocol not supported."); + } + } + try_connect(); + } + + std::string get_local_address() + { + struct sockaddr_in local_address; + socklen_t address_length = sizeof(local_address); + getsockname(m_socket, (struct sockaddr*)&local_address, &address_length); + return std::string(inet_ntoa(local_address.sin_addr)); + } + + int get_local_port() + { + struct sockaddr_in local_address; + socklen_t address_length = sizeof(local_address); + getsockname(m_socket, (struct sockaddr*)&local_address, &address_length); + return (int) ntohs(local_address.sin_port); + } + + void close_socket() + { + if(m_socket != -1) + { + g_logger.log("Socket handler (" + m_id + ") closing connection to " + + m_url.to_string(false) + m_path, sinsp_logger::SEV_DEBUG); + int ret = close(m_socket); + if(ret < 0) + { + g_logger.log("Socket handler (" + m_id + ") connection [" + + m_url.to_string(false) + m_path + "] error closing socket: " + + strerror(errno), sinsp_logger::SEV_ERROR); + } + m_socket = -1; + } + m_enabled = false; + m_connected = false; + m_connecting = false; + m_connect_called = true; + } + + void ssl_cleanup() + { + SSL_free(m_ssl_connection); + m_ssl_connection = 0; + SSL_CTX_free(m_ssl_context); + m_ssl_context = 0; + } + + void cleanup() + { + ares_destroy(m_ares_channel); + free(m_http_parser); + m_http_parser = nullptr; + close_socket(); + ssl_cleanup(); + } + + struct http_parser_data + { + std::string* m_data_buf = nullptr; + std::vector* m_json = nullptr; + int* m_http_response = nullptr; + bool* m_msg_completed = nullptr; + bool* m_fetching_state = nullptr; + }; + + static int http_body_callback(http_parser* parser, const char* data, size_t len) + { + if(parser) + { + if(parser->data) + { + if(data && len) + { + http_parser_data* parser_data = (http_parser_data*) parser->data; + if(parser_data->m_data_buf && parser_data->m_json) + { + parser_data->m_data_buf->append(data, len); + // only try to parse this JSON if we are certain it is not pretty-printed + // since this logic relies on JSONs in the stream being delimited by newlines + // (and having no newlines themselves), pretty-printed JSONs can not be + // handled here, but must be handled in the http_msg_completed_callback() + if(parser_data->m_fetching_state) + { + if(!*(parser_data->m_fetching_state)) + { + std::string::size_type pos = parser_data->m_data_buf->find('\n'); + while(pos != std::string::npos) + { + parser_data->m_json->push_back(parser_data->m_data_buf->substr(0, pos)); + parser_data->m_data_buf->erase(0, pos + 1); + pos = parser_data->m_data_buf->find('\n'); + } + } + /*else + { + if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE) + { + g_logger.log("Socket handler (http_body_callback) data received, will be parsed on response end:" + + *parser_data->m_data_buf, sinsp_logger::SEV_TRACE); + } + }*/ + } + } + else { throw sinsp_exception("Socket handler (http_body_callback): http or json buffer is null."); } + } + } + else { throw sinsp_exception("Socket handler (http_body_callback) parser data is null."); } + } + else { throw sinsp_exception("Socket handler (http_body_callback): parser is null."); } + return 0; + } + + static int http_msg_completed_callback(http_parser* parser) + { + if(parser && parser->data) + { + http_parser_data* parser_data = (http_parser_data*) parser->data; + if(parser_data->m_fetching_state) + { + if(*(parser_data->m_fetching_state)) + { + std::string* buf = parser_data->m_data_buf; + if(buf) + { + std::string::size_type pos = buf->rfind('\n'); + if(pos != std::string::npos) + { + buf->erase(std::remove_if(buf->begin(), buf->end(), [](char c){return c == '\n' || c == '\r';}), buf->end()); + parser_data->m_json->emplace_back(std::move(*buf)); + buf->clear(); + } + else + { + g_logger.log("Initial state fetch completed, but no newline found!", sinsp_logger::SEV_ERROR); + } + *(parser_data->m_fetching_state) = false; + } + else { throw sinsp_exception("Socket handler (http_msg_completed_callback): parser data m_data_buf is null."); } + } + } + else { throw sinsp_exception("Socket handler (http_msg_completed_callback): parser data m_data_buf is null."); } + if(parser_data->m_msg_completed) + { + *(parser_data->m_msg_completed) = true; + } + else { throw sinsp_exception("Socket handler (http_msg_completed_callback): parser data m_msg_completed is null."); } + if(parser_data->m_http_response) + { + *(parser_data->m_http_response) = parser->status_code; + } + else { throw sinsp_exception("Socket handler (http_msg_completed_callback): parser data m_http_response is null."); } + } + else { throw sinsp_exception("Socket handler (http_msg_completed_callback): parser or data null."); } + return 0; + } + + void init_http_parser() + { + m_msg_completed = false; + m_http_response = -1; + http_parser_settings_init(&m_http_parser_settings); + m_http_parser_settings.on_body = http_body_callback; + m_http_parser_settings.on_message_complete = http_msg_completed_callback; + if(!m_http_parser) + { + m_http_parser = (http_parser *)std::malloc(sizeof(http_parser)); + } + m_http_parser_data.m_data_buf = &m_data_buf; + m_http_parser_data.m_json = &m_json; + m_http_parser_data.m_http_response = &m_http_response; + m_http_parser_data.m_msg_completed = &m_msg_completed; + m_http_parser_data.m_fetching_state = &m_fetching_state; + http_parser_init(m_http_parser, HTTP_RESPONSE); + m_http_parser->data = &m_http_parser_data; + } + + static std::string get_http_reason(int status) + { + return http_reason::get(status); + } + + T& m_obj; + std::string m_id; + uri m_url; + std::string m_keep_alive; + std::string m_path; + std::string m_address; + bool m_connecting = false; + bool m_connected = false; + bool m_connect_called = false; + bool m_connection_error = false; + bool m_enabled = false; + int m_socket = -1; + bool m_blocking = false; + std::vector m_buf; + int m_sock_err = 0; + ssl_ptr_t m_ssl; + bt_ptr_t m_bt; + long m_timeout_ms; + json_callback_func_t m_json_callback = nullptr; + std::string m_data_buf; + std::string m_request; + std::string m_http_version; + std::vector m_json_filters; + std::vector m_json; + json_query m_jq; + bool m_ssl_init_complete = false; + SSL_CTX* m_ssl_context = nullptr; + SSL* m_ssl_connection = nullptr; + password_vec_t m_ssl_key_pass; + struct sockaddr_un m_file_addr = {0}; + struct sockaddr_in m_serv_addr = {0}; + struct sockaddr* m_sa = 0; + socklen_t m_sa_len = 0; + bool m_close_on_chunked_end = true; + bool m_wants_send = false; + int m_http_response = -1; + bool m_msg_completed = false; + http_parser_settings m_http_parser_settings; + http_parser* m_http_parser = nullptr; + http_parser_data m_http_parser_data; + unsigned m_data_limit = 524288; // bytes + + ares_channel m_ares_channel = nullptr; + ares_options m_ares_opts; + ares_cb_result m_ares_cb_res; + + // older versions of kubernetes send pretty-printed JSON by default; + // that creates a problem with JSON-newline-delimit-based detection logic, + // which relies on JSON itself having no newlines; while there is a way to + // prevent this for entities (eg. nodes, pods) URIs by specifying '?pretty=false', + // some cluster-level URIs (eg. /api) do not honor this parameter; + // + // this flag is true by default and it remains true until the first state http + // request for this handler is completed, at which point all newlines are purged + // from the string and the purged buffer is posted for further processing + bool m_fetching_state = true; +}; + +template +const std::string socket_data_handler::HTTP_VERSION_10 = "1.0"; +template +const std::string socket_data_handler::HTTP_VERSION_11 = "1.1"; + +#endif // HAS_CAPTURE +#endif // MINIMAL_BUILD \ No newline at end of file diff --git a/userspace/libsinsp/stats.cpp b/userspace/libsinsp/stats.cpp index b9b37ed0bc..df309a8d57 100644 --- a/userspace/libsinsp/stats.cpp +++ b/userspace/libsinsp/stats.cpp @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ //////////////////////////////////////////////////////////////////////////// diff --git a/userspace/libsinsp/stats.h b/userspace/libsinsp/stats.h index 872bef6376..59da9d5381 100644 --- a/userspace/libsinsp/stats.h +++ b/userspace/libsinsp/stats.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once diff --git a/userspace/libsinsp/stopwatch.cpp b/userspace/libsinsp/stopwatch.cpp new file mode 100644 index 0000000000..096df2fefb --- /dev/null +++ b/userspace/libsinsp/stopwatch.cpp @@ -0,0 +1,30 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// stopwatch.cpp +// +// stopwatch utility +// + +#include "stopwatch.h" + +sinsp_stopwatch::sinsp_stopwatch() +{ + start(); +} diff --git a/userspace/libsinsp/stopwatch.h b/userspace/libsinsp/stopwatch.h new file mode 100644 index 0000000000..1f4f19546e --- /dev/null +++ b/userspace/libsinsp/stopwatch.h @@ -0,0 +1,70 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// stopwatch.h +// +// stopwatch utility +// + +#pragma once + +#include + +class sinsp_stopwatch +{ +public: + sinsp_stopwatch(); + + void stop(); + void start(); + void reset(); + + template + typename T::rep elapsed() const + { + return std::chrono::duration_cast(m_stop - m_start).count(); + } + +private: + void record(std::chrono::high_resolution_clock::time_point& tp); + + std::chrono::high_resolution_clock::time_point m_start; + std::chrono::high_resolution_clock::time_point m_stop; +}; + +inline void sinsp_stopwatch::sinsp_stopwatch::reset() +{ + m_start = std::chrono::high_resolution_clock::time_point::min(); + m_stop = m_start; +} + +inline void sinsp_stopwatch::sinsp_stopwatch::start() +{ + record(m_start); +} + +inline void sinsp_stopwatch::sinsp_stopwatch::stop() +{ + record(m_stop); +} + +inline void sinsp_stopwatch::record(std::chrono::high_resolution_clock::time_point& tp) +{ + tp = std::chrono::high_resolution_clock::now(); +} diff --git a/userspace/libsinsp/table.cpp b/userspace/libsinsp/table.cpp new file mode 100644 index 0000000000..1562c70441 --- /dev/null +++ b/userspace/libsinsp/table.cpp @@ -0,0 +1,1609 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#ifndef _WIN32 +#include +#endif + +#include "sinsp.h" +#include "sinsp_int.h" +#include "../../driver/ppm_ringbuffer.h" +#include "filter.h" +#include "filterchecks.h" +#include "table.h" + +extern sinsp_filter_check_list g_filterlist; +extern sinsp_evttables g_infotables; + +// +// +// Table sorter functor +typedef struct table_row_cmp +{ + bool operator()(const sinsp_sample_row& src, const sinsp_sample_row& dst) + { + cmpop op; + + if(m_ascending) + { + op = CO_LT; + } + else + { + op = CO_GT; + } + + if(src.m_values[m_colid].m_cnt > 1 || + dst.m_values[m_colid].m_cnt > 1) + { + return flt_compare_avg(op, m_type, + src.m_values[m_colid].m_val, + dst.m_values[m_colid].m_val, + src.m_values[m_colid].m_len, + dst.m_values[m_colid].m_len, + src.m_values[m_colid].m_cnt, + dst.m_values[m_colid].m_cnt); + } + else + { + return flt_compare(op, m_type, + src.m_values[m_colid].m_val, + dst.m_values[m_colid].m_val, + src.m_values[m_colid].m_len, + dst.m_values[m_colid].m_len); + } + } + + uint32_t m_colid; + ppm_param_type m_type; + bool m_ascending; +}table_row_cmp; + +sinsp_table::sinsp_table(sinsp* inspector, tabletype type, uint64_t refresh_interval_ns, + sinsp_table::output_type output_type, uint32_t json_first_row, uint32_t json_last_row) +{ + m_inspector = inspector; + m_type = type; + m_is_key_present = false; + m_is_groupby_key_present = false; + m_fld_pointers = NULL; + m_premerge_fld_pointers = NULL; + m_postmerge_fld_pointers = NULL; + m_n_fields = 0; + m_n_premerge_fields = 0; + m_n_postmerge_fields = 0; + m_refresh_interval_ns = refresh_interval_ns; + m_output_type = output_type; + m_next_flush_time_ns = 0; + m_prev_flush_time_ns = 0; + m_printer = new sinsp_filter_check_reference(); + m_buffer = &m_buffer1; + m_is_sorting_ascending = false; + m_sorting_col = -1; + m_just_sorted = true; + m_do_merging = true; + m_types = &m_premerge_types; + m_table = &m_premerge_table; + m_extractors = &m_premerge_extractors; + m_filter = NULL; + m_use_defaults = false; + m_zero_u64 = 0; + m_zero_double = 0; + m_paused = false; + m_sample_data = NULL; + m_json_first_row = json_first_row; + m_json_last_row = json_last_row; +} + +sinsp_table::~sinsp_table() +{ + uint32_t j; + + for(j = 0; j < m_chks_to_free.size(); j++) + { + delete m_chks_to_free[j]; + } + + if(m_premerge_fld_pointers != NULL) + { + delete[] m_premerge_fld_pointers; + } + + if(m_postmerge_fld_pointers != NULL) + { + delete[] m_postmerge_fld_pointers; + } + + if(m_filter != NULL) + { + delete m_filter; + } + + delete m_printer; +} + +void sinsp_table::configure(vector* entries, const string& filter, + bool use_defaults, uint32_t view_depth) +{ + m_use_defaults = use_defaults; + m_view_depth = view_depth; + + // + // If this is a list table, increase the refresh time to improve realtimyiness + // + if(m_type == sinsp_table::TT_LIST) + { + set_refresh_interval(200000000); + } + + ////////////////////////////////////////////////////////////////////////////////////// + // If a filter has been specified, compile it + ////////////////////////////////////////////////////////////////////////////////////// + if(filter != "") + { + sinsp_filter_compiler compiler(m_inspector, filter); + m_filter = compiler.compile(); + } + + ////////////////////////////////////////////////////////////////////////////////////// + // Extract the tokens + ////////////////////////////////////////////////////////////////////////////////////// + m_premerge_extractors.clear(); + + for(auto vit : *entries) + { + sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(vit.get_field(m_view_depth), + m_inspector, + false); + + if(chk == NULL) + { + throw sinsp_exception("invalid field name " + vit.get_field(m_view_depth)); + } + + chk->m_aggregation = (sinsp_field_aggregation)vit.m_aggregation; + m_chks_to_free.push_back(chk); + + chk->parse_field_name(vit.get_field(m_view_depth).c_str(), true, false); + + if((vit.m_flags & TEF_IS_KEY) != 0) + { + if(m_is_key_present) + { + throw sinsp_exception("invalid table configuration: multiple keys specified"); + } + + m_premerge_extractors.insert(m_premerge_extractors.begin(), chk); + m_is_key_present = true; + } + else + { + m_premerge_extractors.push_back(chk); + } + } + + if(m_type == sinsp_table::TT_TABLE) + { + // + // Make sure this is a valid table + // + if(!m_is_key_present) + { + throw sinsp_exception("table is missing the key"); + } + } + else + { + sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname("util.cnt", + m_inspector, + false); + + if(chk == NULL) + { + throw sinsp_exception("internal table error"); + } + + chk->m_aggregation = A_NONE; + m_chks_to_free.push_back(chk); + + chk->parse_field_name("util.cnt", true, false); + + if(m_is_key_present) + { + throw sinsp_exception("list table can't have a key"); + } + + m_premerge_extractors.insert(m_premerge_extractors.begin(), chk); + m_is_key_present = true; + } + + m_premerge_fld_pointers = new sinsp_table_field[m_premerge_extractors.size()]; + m_fld_pointers = m_premerge_fld_pointers; + m_n_premerge_fields = (uint32_t)m_premerge_extractors.size(); + m_n_fields = m_n_premerge_fields; + + if(m_n_fields < 2) + { + throw sinsp_exception("table has no values"); + } + + for(auto it = m_premerge_extractors.begin(); it != m_premerge_extractors.end(); ++it) + { + m_premerge_types.push_back((*it)->get_field_info()->m_type); + m_premerge_legend.push_back(*(*it)->get_field_info()); + } + + m_premerge_vals_array_sz = (m_n_fields - 1) * sizeof(sinsp_table_field); + m_vals_array_sz = m_premerge_vals_array_sz; + + ////////////////////////////////////////////////////////////////////////////////////// + // If a merge has been specified, configure it + ////////////////////////////////////////////////////////////////////////////////////// + uint32_t n_gby_keys = 0; + + for(auto vit : *entries) + { + if((vit.m_flags & TEF_IS_GROUPBY_KEY) != 0) + { + n_gby_keys++; + } + } + + if(n_gby_keys == 0) + { + // + // No merge string. We can stop here + // + m_do_merging = false; + return; + } + else if(n_gby_keys > 1) + { + throw sinsp_exception("invalid table definition: multiple groupby keys"); + } + + // + // Merging not supported for lists + // + if(m_type != sinsp_table::TT_TABLE) + { + throw sinsp_exception("group by not supported for list tables"); + } + + m_do_merging = true; + + for(uint32_t j = 0; j < entries->size(); j++) + { + auto vit = entries->at(j); + + // + // Skip original key when grouping + // + if((vit.m_flags & TEF_IS_KEY) != 0) + { + continue; + } + + + sinsp_filter_check* chk = m_premerge_extractors[j]; + + chk->m_merge_aggregation = (sinsp_field_aggregation)vit.m_groupby_aggregation; + + if((vit.m_flags & TEF_IS_GROUPBY_KEY) != 0) + { + if(m_is_groupby_key_present) + { + throw sinsp_exception("invalid table configuration: more than one groupby key specified"); + } + + m_is_groupby_key_present = true; + m_postmerge_extractors.insert(m_postmerge_extractors.begin(), chk); + m_groupby_columns.insert(m_groupby_columns.begin(), j); + } + else + { + m_postmerge_extractors.push_back(chk); + m_groupby_columns.push_back(j); + } + } + + m_postmerge_fld_pointers = new sinsp_table_field[m_postmerge_extractors.size()]; + m_n_postmerge_fields = (uint32_t)m_postmerge_extractors.size(); + + if(!m_is_groupby_key_present) + { + throw sinsp_exception("table is missing the groupby key"); + } + + if(m_groupby_columns.size() < 2) + { + throw sinsp_exception("groupby table has no values"); + } + + for(auto it = m_postmerge_extractors.begin(); it != m_postmerge_extractors.end(); ++it) + { + m_postmerge_types.push_back((*it)->get_field_info()->m_type); + m_postmerge_legend.push_back(*(*it)->get_field_info()); + } + + m_postmerge_vals_array_sz = (m_n_postmerge_fields - 1) * sizeof(sinsp_table_field); +} + +void sinsp_table::add_row(bool merging) +{ + uint32_t j; + + sinsp_table_field key(m_fld_pointers[0].m_val, + m_fld_pointers[0].m_len, + m_fld_pointers[0].m_cnt); + + if(m_type == sinsp_table::TT_TABLE) + { + // + // This is a table. Do a proper key lookup and update the entry + // + auto it = m_table->find(key); + + if(it == m_table->end()) + { + // + // New entry + // + key.m_val = key.m_val; + key.m_cnt = 1; + m_vals = (sinsp_table_field*)m_buffer->reserve(m_vals_array_sz); + + for(j = 1; j < m_n_fields; j++) + { + uint32_t vlen = get_field_len(j); + m_vals[j - 1].m_val = m_fld_pointers[j].m_val; + m_vals[j - 1].m_len = vlen; + m_vals[j - 1].m_cnt = m_fld_pointers[j].m_cnt; + } + + (*m_table)[key] = m_vals; + } + else + { + // + // Existing entry + // + m_vals = it->second; + + for(j = 1; j < m_n_fields; j++) + { + if(merging) + { + add_fields(j, &m_fld_pointers[j], m_postmerge_extractors[j]->m_merge_aggregation); + } + else + { + add_fields(j, &m_fld_pointers[j], m_premerge_extractors[j]->m_aggregation); + } + } + } + } + else + { + // + // We are in list mode. Just append the row to the end of the sample + // + if(m_paused) + { + return; + } + + sinsp_sample_row row; + + // + // This is a list. Create the new entry and push it back. + // + key.m_val = key.m_val; + key.m_cnt = 1; + row.m_key = key; + + m_vals = (sinsp_table_field*)m_buffer->reserve(m_vals_array_sz); + + for(j = 1; j < m_n_fields; j++) + { + uint32_t vlen = get_field_len(j); + m_vals[j - 1].m_val = m_fld_pointers[j].m_val; + m_vals[j - 1].m_len = vlen; + m_vals[j - 1].m_cnt = 1; + row.m_values.push_back(m_vals[j - 1]); + } + + m_full_sample_data.push_back(row); + } +} + +void sinsp_table::process_event(sinsp_evt* evt) +{ + uint32_t j; + + // + // Apply the filter + // + if(m_filter) + { + if(!m_filter->run(evt)) + { + return; + } + } + + // + // Extract the values and create the row to add + // + for(j = 0; j < m_n_premerge_fields; j++) + { + uint32_t len; + uint8_t* val = m_premerge_extractors[j]->extract(evt, &len); + + sinsp_table_field* pfld = &(m_premerge_fld_pointers[j]); + + // + // XXX For the moment, we only support defaults for numeric fields. + // At a certain point we will want to introduce the concept of zero + // for other fields too. + // + if(val == NULL) + { + if(m_use_defaults) + { + pfld->m_val = get_default_val(&m_premerge_legend[j]); + if(pfld->m_val == NULL) + { + return; + } + + pfld->m_len = get_field_len(j); + pfld->m_val = m_buffer->copy(pfld->m_val, pfld->m_len); + pfld->m_cnt = 0; + } + else + { + return; + } + } + else + { + pfld->m_val = val; + pfld->m_len = get_field_len(j); + pfld->m_val = m_buffer->copy(val, pfld->m_len); + pfld->m_cnt = 1; + } + } + + // + // Add the row + // + add_row(false); + + return; +} + +void sinsp_table::process_proctable(sinsp_evt* evt) +{ + sinsp_evt tevt; + scap_evt tscapevt; + + threadinfo_map_t* threadtable = m_inspector->m_thread_manager->get_threads(); + ASSERT(threadtable != NULL); + + uint64_t ts = evt->get_ts(); + uint64_t ts_s = ts - (ts % ONE_SECOND_IN_NS); + tscapevt.ts = ts_s - 1; + + // + // Note: as the event type for this fake event, we pick one of the unused + // numbers, so we guarantee that filter checks will not wrongly pick it up + // + tscapevt.type = PPME_SYSDIGEVENT_X; + tscapevt.len = 0; + tscapevt.nparams = 0; + + tevt.m_inspector = m_inspector; + tevt.m_info = &(g_infotables.m_event_info[PPME_SYSDIGEVENT_X]); + tevt.m_pevt = NULL; + tevt.m_cpuid = 0; + tevt.m_evtnum = 0; + tevt.m_pevt = &tscapevt; + tevt.m_fdinfo = NULL; + + threadtable->loop([&] (sinsp_threadinfo& tinfo) { + tevt.m_tinfo = &tinfo; + tscapevt.tid = tevt.m_tinfo->m_tid; + + if(m_filter) + { + if(!m_filter->run(&tevt)) + { + return true; + } + } + + process_event(&tevt); + return true; + }); +} + +void sinsp_table::flush(sinsp_evt* evt) +{ + if(!m_paused) + { + if(m_next_flush_time_ns != 0) + { + // + // Time to emit the sample! + // Add the proctable as a sample at the end of the second + // + process_proctable(evt); + + // + // If there is a merging step, switch the types to point to the merging ones. + // + if(m_do_merging) + { + m_types = &m_postmerge_types; + m_table = &m_merge_table; + m_n_fields = m_n_postmerge_fields; + m_vals_array_sz = m_postmerge_vals_array_sz; + m_fld_pointers = m_postmerge_fld_pointers; + m_extractors = &m_postmerge_extractors; + } + + // + // Emit the sample + // + create_sample(); + + if(m_type == sinsp_table::TT_TABLE) + { + // + // Switch the data storage so that the current one is still usable by the + // consumers of the table. + // + switch_buffers(); + + // + // Clear the current data storage + // + m_buffer->clear(); + } + + // + // Reinitialize the tables + // + m_premerge_table.clear(); + m_merge_table.clear(); + } + } + + uint64_t ts = evt->get_ts(); + + m_prev_flush_time_ns = m_next_flush_time_ns; + m_next_flush_time_ns = ts - (ts % m_refresh_interval_ns) + m_refresh_interval_ns; + + return; +} + +void sinsp_table::print_raw(vector* sample_data, uint64_t time_delta) +{ + vector* legend = get_legend(); + + for(auto it = sample_data->begin(); it != sample_data->end(); ++it) + { + for(uint32_t j = 0; j < m_n_fields - 1; j++) + { + sinsp_filter_check* extractor = m_extractors->at(j + 1); + uint64_t td = 0; + + if(extractor->m_aggregation == A_TIME_AVG || + extractor->m_merge_aggregation == A_TIME_AVG) + { + td = time_delta; + } + + m_printer->set_val(m_types->at(j + 1), + it->m_values[j].m_val, + it->m_values[j].m_len, + it->m_values[j].m_cnt, + legend->at(j + 1).m_print_format); + char* prstr = m_printer->tostring_nice(NULL, 10, td); + printf("%s ", prstr); + //printf("%s ", m_printer->tostring(NULL)); + } + + printf("\n"); + } + + printf("----------------------\n"); +} + +void sinsp_table::print_json(vector* sample_data, uint64_t time_delta) +{ + Json::FastWriter writer; + vector* legend = get_legend(); + string res; + uint32_t j = 0; + uint32_t k = 0; + m_json_output_lines_count = 0; + + if(sample_data->size() == 0) + { + return; + } + + if(m_json_first_row >= sample_data->size()) + { + return; + } + + if(m_json_last_row == 0 || m_json_last_row >= sample_data->size() - 1) + { + m_json_last_row = sample_data->size() - 1; + } + + printf("\"data\": [\n"); + + for(k = m_json_first_row; k <= m_json_last_row; k++) + { + Json::Value root; + Json::Value jd; + auto row = sample_data->at(k); + + for(uint32_t j = 0; j < m_n_fields - 1; j++) + { + sinsp_filter_check* extractor = m_extractors->at(j + 1); + uint64_t td = 0; + + if(extractor->m_aggregation == A_TIME_AVG || + extractor->m_merge_aggregation == A_TIME_AVG) + { + td = time_delta; + } + + m_printer->set_val(m_types->at(j + 1), + row.m_values[j].m_val, + row.m_values[j].m_len, + row.m_values[j].m_cnt, + legend->at(j + 1).m_print_format); + + jd.append(m_printer->tojson(NULL, 10, td)); + } + + + auto key = get_row_key_name_and_val(k, false); + + root["k"] = key.second; + root["d"] = jd; + + res = writer.write(root); + printf("%s", res.substr(0, res.size() - 1).c_str()); + + m_json_output_lines_count++; + + if(k >= m_json_last_row) + { + break; + } + + if(j < sample_data->size() - 1) + { + printf(","); + } + printf("\n"); + + j++; + } + + printf("],\n"); +} + +void sinsp_table::filter_sample() +{ + vector* legend = get_legend(); + + m_filtered_sample_data.clear(); + + for(auto it : m_full_sample_data) + { + for(uint32_t j = 0; j < it.m_values.size(); j++) + { + ppm_param_type type; + + if(m_do_merging) + { + type = m_postmerge_types[j + 1]; + } + else + { + type = m_premerge_types[j + 1]; + } + + if(type == PT_CHARBUF || type == PT_BYTEBUF || type == PT_SYSCALLID || + type == PT_PORT || type == PT_L4PROTO || type == PT_SOCKFAMILY || type == PT_IPV4ADDR || + type == PT_IPV6ADDR || + type == PT_UID || type == PT_GID) + { + m_printer->set_val(type, + it.m_values[j].m_val, + it.m_values[j].m_len, + it.m_values[j].m_cnt, + legend->at(j + 1).m_print_format); + + string strval = m_printer->tostring_nice(NULL, 0, 0); + + if(strval.find(m_freetext_filter) != string::npos) + { + m_filtered_sample_data.push_back(it); + break; + } + } + } + } +} + +// +// Returns the key of the first match, or NULL if no match +// +sinsp_table_field* sinsp_table::search_in_sample(string text) +{ + vector* legend = get_legend(); + + for(auto it = m_full_sample_data.begin(); it != m_full_sample_data.end(); ++it) + { + for(uint32_t j = 0; j < it->m_values.size(); j++) + { + ppm_param_type type; + + if(m_do_merging) + { + ASSERT(m_types->size() == it->m_values.size() + 2); + type = m_types->at(j + 2); + } + else + { + ASSERT(m_types->size() == it->m_values.size() + 1); + type = m_types->at(j + 1); + } + + if(type == PT_CHARBUF || type == PT_BYTEBUF || type == PT_SYSCALLID || + type == PT_PORT || type == PT_L4PROTO || type == PT_SOCKFAMILY || type == PT_IPV4ADDR || + type == PT_IPV6ADDR || + type == PT_UID || type == PT_GID) + { + m_printer->set_val(type, + it->m_values[j].m_val, + it->m_values[j].m_len, + it->m_values[j].m_cnt, + legend->at(j + 1).m_print_format); + + string strval = m_printer->tostring_nice(NULL, 0, 0); + + if(strval.find(text) != string::npos) + { + return &(it->m_key); + } + } + } + } + + return NULL; +} + +void sinsp_table::sort_sample() +{ + if(m_type == sinsp_table::TT_LIST) + { + if(m_sorting_col == -1 || !m_just_sorted) + { + return; + } + + m_just_sorted = false; + } + + if(m_sample_data->size() != 0) + { + if(m_sorting_col >= (int32_t)m_sample_data->at(0).m_values.size()) + { + throw sinsp_exception("invalid table sorting column"); + } + + table_row_cmp cc; + cc.m_colid = m_sorting_col; + cc.m_ascending = m_is_sorting_ascending; + uint32_t tyid = m_do_merging? m_sorting_col + 2 : m_sorting_col + 1; + cc.m_type = m_premerge_types[tyid]; + + sort(m_sample_data->begin(), + m_sample_data->end(), + cc); + } +} + +vector* sinsp_table::get_sample(uint64_t time_delta) +{ + // + // No sample generation happens when the table is paused + // + if(!m_paused) + { + // + // If we have a freetext filter, we start by filtering the sample + // + if(m_freetext_filter != "") + { + filter_sample(); + m_sample_data = &m_filtered_sample_data; + } + else + { + m_sample_data = &m_full_sample_data; + } + + // + // Sort the sample + // + sort_sample(); + } + + // + // If required, emit the sample to stdout + // +#ifndef _WIN32 + if(m_output_type != sinsp_table::OT_CURSES) + { +#endif + if(m_output_type == sinsp_table::OT_RAW) + { + print_raw(m_sample_data, time_delta); + } + else if(m_output_type == sinsp_table::OT_JSON) + { + print_json(m_sample_data, time_delta); + } + else + { + ASSERT(false); + } +#ifndef _WIN32 + } +#endif + + // + // Restore the lists used for event processing + // + m_types = &m_premerge_types; + m_table = &m_premerge_table; + m_n_fields = m_n_premerge_fields; + m_vals_array_sz = m_premerge_vals_array_sz; + m_fld_pointers = m_premerge_fld_pointers; + m_extractors = &m_premerge_extractors; + + return m_sample_data; +} + +void sinsp_table::set_sorting_col(uint32_t col) +{ + uint32_t n_fields; + vector* types; + + if(m_do_merging) + { + n_fields = m_n_postmerge_fields; + types = &m_postmerge_types; + } + else + { + n_fields = m_n_premerge_fields; + types = &m_premerge_types; + } + + if(col == 0) + { + if(m_type == sinsp_table::TT_TABLE) + { + throw sinsp_exception("cannot sort by key"); + } + else + { + m_sorting_col = -1; + return; + } + } + + if(col >= n_fields) + { + throw sinsp_exception("invalid table sorting column"); + } + + if(col == (uint32_t)(m_sorting_col + 1)) + { + m_is_sorting_ascending = !m_is_sorting_ascending; + } + else + { + switch(types->at(col)) + { + case PT_INT8: + case PT_INT16: + case PT_INT32: + case PT_INT64: + case PT_UINT8: + case PT_UINT16: + case PT_UINT32: + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + case PT_DOUBLE: + case PT_BOOL: + m_is_sorting_ascending = false; + break; + default: + m_is_sorting_ascending = true; + break; + } + } + + m_just_sorted = true; + m_sorting_col = col - 1; +} + +uint32_t sinsp_table::get_sorting_col() +{ + return (uint32_t)m_sorting_col + 1; +} + +void sinsp_table::create_sample() +{ + if(m_type == sinsp_table::TT_TABLE) + { + uint32_t j; + m_full_sample_data.clear(); + sinsp_sample_row row; + + // + // If merging is on, perform the merge and switch to the merged table + // + if(m_do_merging) + { + m_table = &m_merge_table; + m_merge_table.clear(); + + for(auto it = m_premerge_table.begin(); it != m_premerge_table.end(); ++it) + { + for(j = 0; j < m_n_postmerge_fields; j++) + { + sinsp_table_field* pfld = &(m_postmerge_fld_pointers[j]); + + uint32_t col = m_groupby_columns[j]; + if(col == 0) + { + pfld->m_val = it->first.m_val; + pfld->m_len = it->first.m_len; + pfld->m_cnt = it->first.m_cnt; + } + else + { + pfld->m_val = it->second[col - 1].m_val; + pfld->m_len = it->second[col - 1].m_len; + pfld->m_cnt = it->second[col - 1].m_cnt; + } + } + + add_row(true); + } + } + else + { + m_table = &m_premerge_table; + } + + // + // Emit the table + // + for(auto it = m_table->begin(); it != m_table->end(); ++it) + { + row.m_key = it->first; + + row.m_values.clear(); + + sinsp_table_field* fields = it->second; + for(j = 0; j < m_n_fields - 1; j++) + { + row.m_values.push_back(fields[j]); + } + + m_full_sample_data.push_back(row); + } + } + else + { + // + // If this is a list, there's nothing to be done, since m_full_sample_data + // is already prepared and doesn't need to be cleaned. + // + return; + } +} + +void sinsp_table::add_fields_sum(ppm_param_type type, sinsp_table_field *dst, sinsp_table_field *src) +{ + uint8_t* operand1 = dst->m_val; + uint8_t* operand2 = src->m_val; + + switch(type) + { + case PT_INT8: + *(int8_t*)operand1 += *(int8_t*)operand2; + return; + case PT_INT16: + *(int16_t*)operand1 += *(int16_t*)operand2; + return; + case PT_INT32: + *(int32_t*)operand1 += *(int32_t*)operand2; + return; + case PT_INT64: + *(int64_t*)operand1 += *(int64_t*)operand2; + return; + case PT_UINT8: + *(uint8_t*)operand1 += *(uint8_t*)operand2; + return; + case PT_UINT16: + *(uint16_t*)operand1 += *(uint16_t*)operand2; + return; + case PT_UINT32: + case PT_BOOL: + *(uint32_t*)operand1 += *(uint32_t*)operand2; + return; + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + *(uint64_t*)operand1 += *(uint64_t*)operand2; + return; + case PT_DOUBLE: + *(double*)operand1 += *(double*)operand2; + return; + default: + return; + } +} + +void sinsp_table::add_fields_sum_of_avg(ppm_param_type type, sinsp_table_field *dst, sinsp_table_field *src) +{ + uint8_t* operand1 = dst->m_val; + uint8_t* operand2 = src->m_val; + uint32_t cnt1 = dst->m_cnt; + uint32_t cnt2 = src->m_cnt; + + switch(type) + { + case PT_INT8: + if(cnt1 > 1) + { + *(int8_t*)operand1 = *(int8_t*)operand1 / cnt1; + } + + *(int8_t*)operand1 += (*(int8_t*)operand2) / cnt2; + break; + case PT_INT16: + if(cnt1 > 1) + { + *(int16_t*)operand1 = *(int16_t*)operand1 / cnt1; + } + + *(int16_t*)operand1 += (*(int16_t*)operand2) / cnt2; + break; + case PT_INT32: + if(cnt1 > 1) + { + *(int32_t*)operand1 = *(int32_t*)operand1 / cnt1; + } + + *(int32_t*)operand1 += (*(int32_t*)operand2) / cnt2; + break; + case PT_INT64: + if(cnt1 > 1) + { + *(int64_t*)operand1 = *(int64_t*)operand1 / cnt1; + } + + *(int64_t*)operand1 += (*(int64_t*)operand2) / cnt2; + break; + case PT_UINT8: + if(cnt1 > 1) + { + *(uint8_t*)operand1 = *(uint8_t*)operand1 / cnt1; + } + + *(uint8_t*)operand1 += (*(uint8_t*)operand2) / cnt2; + break; + case PT_UINT16: + if(cnt1 > 1) + { + *(uint16_t*)operand1 = *(uint16_t*)operand1 / cnt1; + } + + *(uint16_t*)operand1 += (*(uint16_t*)operand2) / cnt2; + break; + case PT_UINT32: + case PT_BOOL: + if(cnt1 > 1) + { + *(uint32_t*)operand1 = *(uint32_t*)operand1 / cnt1; + } + + *(uint32_t*)operand1 += (*(uint32_t*)operand2) / cnt2; + break; + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + if(cnt1 > 1) + { + *(uint64_t*)operand1 = *(uint64_t*)operand1 / cnt1; + } + + *(uint64_t*)operand1 += (*(uint64_t*)operand2) / cnt2; + break; + case PT_DOUBLE: + if(cnt1 > 1) + { + *(double*)operand1 = *(double*)operand1 / cnt1; + } + + *(double*)operand1 += (*(double*)operand2) / cnt2; + break; + default: + break; + } + + src->m_cnt = 1; + dst->m_cnt = 1; +} + +void sinsp_table::add_fields_max(ppm_param_type type, sinsp_table_field *dst, sinsp_table_field *src) +{ + uint8_t* operand1 = dst->m_val; + uint8_t* operand2 = src->m_val; + + switch(type) + { + case PT_INT8: + if(*(int8_t*)operand1 < *(int8_t*)operand2) + { + *(int8_t*)operand1 = *(int8_t*)operand2; + } + return; + case PT_INT16: + if(*(int16_t*)operand1 < *(int16_t*)operand2) + { + *(int16_t*)operand1 = *(int16_t*)operand2; + } + return; + case PT_INT32: + if(*(int32_t*)operand1 < *(int32_t*)operand2) + { + *(int32_t*)operand1 = *(int32_t*)operand2; + } + return; + case PT_INT64: + if(*(int64_t*)operand1 < *(int64_t*)operand2) + { + *(int64_t*)operand1 = *(int64_t*)operand2; + } + return; + case PT_UINT8: + if(*(uint8_t*)operand1 < *(uint8_t*)operand2) + { + *(uint8_t*)operand1 = *(uint8_t*)operand2; + } + return; + case PT_UINT16: + if(*(uint16_t*)operand1 < *(uint16_t*)operand2) + { + *(uint16_t*)operand1 = *(uint16_t*)operand2; + } + return; + case PT_UINT32: + case PT_BOOL: + if(*(uint32_t*)operand1 < *(uint32_t*)operand2) + { + *(uint32_t*)operand1 = *(uint32_t*)operand2; + } + return; + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + if(*(uint64_t*)operand1 < *(uint64_t*)operand2) + { + *(uint64_t*)operand1 = *(uint64_t*)operand2; + } + return; + case PT_DOUBLE: + if(*(double*)operand1 < *(double*)operand2) + { + *(double*)operand1 = *(double*)operand2; + } + return; + case PT_CHARBUF: + case PT_BYTEBUF: + if(dst->m_len >= src->m_len) + { + memcpy(dst->m_val, src->m_val, src->m_len); + } + else + { + dst->m_val = m_buffer->copy(src->m_val, src->m_len); + } + + dst->m_len = src->m_len; + default: + return; + } +} + +void sinsp_table::add_fields_min(ppm_param_type type, sinsp_table_field *dst, sinsp_table_field *src) +{ + uint8_t* operand1 = dst->m_val; + uint8_t* operand2 = src->m_val; + + switch(type) + { + case PT_INT8: + if(*(int8_t*)operand1 > *(int8_t*)operand2) + { + *(int8_t*)operand1 = *(int8_t*)operand2; + } + return; + case PT_INT16: + if(*(int16_t*)operand1 > *(int16_t*)operand2) + { + *(int16_t*)operand1 = *(int16_t*)operand2; + } + return; + case PT_INT32: + if(*(int32_t*)operand1 > *(int32_t*)operand2) + { + *(int32_t*)operand1 = *(int32_t*)operand2; + } + return; + case PT_INT64: + if(*(int64_t*)operand1 > *(int64_t*)operand2) + { + *(int64_t*)operand1 = *(int64_t*)operand2; + } + return; + case PT_UINT8: + if(*(uint8_t*)operand1 > *(uint8_t*)operand2) + { + *(uint8_t*)operand1 = *(uint8_t*)operand2; + } + return; + case PT_UINT16: + if(*(uint16_t*)operand1 > *(uint16_t*)operand2) + { + *(uint16_t*)operand1 = *(uint16_t*)operand2; + } + return; + case PT_UINT32: + case PT_BOOL: + if(*(uint32_t*)operand1 > *(uint32_t*)operand2) + { + *(uint32_t*)operand1 = *(uint32_t*)operand2; + } + return; + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + if(*(uint64_t*)operand1 > *(uint64_t*)operand2) + { + *(uint64_t*)operand1 = *(uint64_t*)operand2; + } + return; + case PT_DOUBLE: + if(*(double*)operand1 > *(double*)operand2) + { + *(double*)operand1 = *(double*)operand2; + } + return; + case PT_CHARBUF: + case PT_BYTEBUF: + ASSERT(false); // Not supposed to use this + if(dst->m_len >= src->m_len) + { + memcpy(dst->m_val, src->m_val, src->m_len); + } + else + { + dst->m_val = m_buffer->copy(src->m_val, src->m_len); + } + + dst->m_len = src->m_len; + default: + return; + } +} + +void sinsp_table::add_fields(uint32_t dst_id, sinsp_table_field* src, uint32_t aggr) +{ + ppm_param_type type = (*m_types)[dst_id]; + sinsp_table_field* dst = &(m_vals[dst_id - 1]); + + switch(aggr) + { + case A_NONE: + return; + case A_SUM: + case A_TIME_AVG: + if(src->m_cnt < 2) + { + add_fields_sum(type, dst, src); + } + else + { + add_fields_sum_of_avg(type, dst, src); + } + + return; + case A_AVG: + dst->m_cnt += src->m_cnt; + add_fields_sum(type, dst, src); + return; + case A_MAX: + add_fields_max(type, dst, src); + return; + case A_MIN: + if(src->m_cnt != 0) + { + if(dst->m_cnt == 0) + { + add_fields_sum(type, dst, src); + dst->m_cnt++; + } + else + { + add_fields_min(type, dst, src); + } + } + return; + default: + ASSERT(false); + return; + } +} + +uint32_t sinsp_table::get_field_len(uint32_t id) +{ + ppm_param_type type; + sinsp_table_field *fld; + + type = (*m_types)[id]; + fld = &(m_fld_pointers[id]); + + switch(type) + { + case PT_INT8: + return 1; + case PT_INT16: + return 2; + case PT_INT32: + return 4; + case PT_INT64: + case PT_FD: + case PT_PID: + case PT_ERRNO: + return 8; + case PT_FLAGS8: + case PT_UINT8: + case PT_SIGTYPE: + return 1; + case PT_FLAGS16: + case PT_UINT16: + case PT_PORT: + case PT_SYSCALLID: + return 2; + case PT_UINT32: + case PT_FLAGS32: + case PT_MODE: + case PT_BOOL: + case PT_IPV4ADDR: + case PT_SIGSET: + return 4; + case PT_UINT64: + case PT_RELTIME: + case PT_ABSTIME: + return 8; + case PT_CHARBUF: + return (uint32_t)(strlen((char*)fld->m_val) + 1); + case PT_BYTEBUF: + return fld->m_len; + case PT_DOUBLE: + return sizeof(double); + case PT_IPV6ADDR: + return sizeof(ipv6addr); + case PT_IPADDR: + case PT_IPNET: + if(fld->m_len == sizeof(struct in_addr)) + { + return 4; + } + else + { + return sizeof(ipv6addr); + } + case PT_SOCKADDR: + case PT_SOCKTUPLE: + case PT_FDLIST: + case PT_FSPATH: + case PT_FSRELPATH: + default: + ASSERT(false); + return false; + } +} + +uint8_t* sinsp_table::get_default_val(filtercheck_field_info* fld) +{ + switch(fld->m_type) + { + case PT_INT8: + case PT_INT16: + case PT_INT32: + case PT_INT64: + case PT_UINT8: + case PT_UINT16: + case PT_UINT32: + case PT_UINT64: + case PT_BOOL: + case PT_RELTIME: + case PT_ABSTIME: + if(fld->m_print_format == PF_DEC) + { + return (uint8_t*)&m_zero_u64; + } + else + { + return NULL; + } + case PT_DOUBLE: + return (uint8_t*)&m_zero_double; + case PT_CHARBUF: + return (uint8_t*)&m_zero_u64; + case PT_PORT: + case PT_IPV4ADDR: + case PT_IPV6ADDR: + return NULL; + default: + ASSERT(false); + return NULL; + } +} + +void sinsp_table::switch_buffers() +{ + if(m_buffer == &m_buffer1) + { + m_buffer = &m_buffer2; + } + else + { + m_buffer = &m_buffer1; + } +} + +pair sinsp_table::get_row_key_name_and_val(uint32_t rownum, bool force) +{ + pair res; + vector* extractors; + vector* types; + + if(m_do_merging) + { + extractors = &m_postmerge_extractors; + types = &m_postmerge_types; + } + else + { + extractors = &m_premerge_extractors; + types = &m_premerge_types; + } + + if(m_sample_data == NULL || rownum >= m_sample_data->size()) + { + ASSERT(m_sample_data == NULL || m_sample_data->size() == 0); + if(force) + { + res.first = (filtercheck_field_info*)((*extractors)[0])->get_field_info(); + ASSERT(res.first != NULL); + } + else + { + res.first = NULL; + } + res.second = ""; + } + else + { + vector* legend = get_legend(); + res.first = (filtercheck_field_info*)((*extractors)[0])->get_field_info(); + ASSERT(res.first != NULL); + + m_printer->set_val(types->at(0), + m_sample_data->at(rownum).m_key.m_val, + m_sample_data->at(rownum).m_key.m_len, + m_sample_data->at(rownum).m_key.m_cnt, + legend->at(0).m_print_format); + + res.second = m_printer->tostring(NULL); + } + + return res; +} + +sinsp_table_field* sinsp_table::get_row_key(uint32_t rownum) +{ + if(rownum >= m_sample_data->size()) + { + return NULL; + } + + return &m_sample_data->at(rownum).m_key; +} + +int32_t sinsp_table::get_row_from_key(sinsp_table_field* key) +{ + uint32_t j; + + for(j = 0; j < m_sample_data->size(); j++) + { + sinsp_table_field* rowkey = &(m_sample_data->at(j).m_key); + + if(rowkey->m_len == key->m_len) + { + if(memcmp(rowkey->m_val, key->m_val, key->m_len) == 0) + { + return j; + } + } + } + + return -1; +} + +void sinsp_table::set_paused(bool paused) +{ + m_paused = paused; +} + +void sinsp_table::clear() +{ + if(m_type == sinsp_table::TT_LIST) + { + m_full_sample_data.clear(); + m_buffer->clear(); + } + else + { + ASSERT(false); + } +} diff --git a/userspace/libsinsp/table.h b/userspace/libsinsp/table.h new file mode 100644 index 0000000000..9ac7ede48d --- /dev/null +++ b/userspace/libsinsp/table.h @@ -0,0 +1,367 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#define SINSP_TABLE_DEFAULT_REFRESH_INTERVAL_NS 1000000000 +#define SINSP_TABLE_BUFFER_ENTRY_SIZE 16384 + +class sinsp_filter_check_reference; + +typedef enum sysdig_table_action +{ + STA_NONE, + STA_PARENT_HANDLE, + STA_QUIT, + STA_SWITCH_VIEW, + STA_SWITCH_SPY, + STA_DRILLDOWN, + STA_DRILLDOWN_TEMPLATE, + STA_DRILLUP, + STA_SPY, + STA_DIG, + STA_SPECTRO, + STA_SPECTRO_FILE, + STA_DESTROY_CHILD, +}sysdig_table_action; + +class sinsp_table_field +{ +public: + sinsp_table_field() + { + m_val = NULL; + } + + sinsp_table_field(uint8_t* val, uint32_t len, uint32_t cnt) + { + m_len = len; + m_val = val; + m_cnt = cnt; + } + + bool operator==(const sinsp_table_field &other) const + { + if(m_len!= other.m_len) + { + return false; + } + + if(memcmp(m_val, other.m_val, m_len) == 0) + { + return true; + } + else + { + return false; + } + } + + uint32_t m_len; + uint32_t m_cnt; // For averages, this stores the entry count + uint8_t* m_val; + + friend class curses_table; +}; + +#define STF_STORAGE_BUFSIZE 512 + +class sinsp_table_field_storage : public sinsp_table_field +{ +public: + sinsp_table_field_storage() + { + m_storage_len = STF_STORAGE_BUFSIZE; + m_val = new uint8_t[m_storage_len]; + m_isvalid = false; + } + + ~sinsp_table_field_storage() + { + if(m_val != NULL) + { + delete[] m_val; + } + } + + void copy(sinsp_table_field* other) + { + if(other->m_len > m_storage_len) + { + resize(other->m_len); + } + + m_len = other->m_len; + + memcpy(m_val, other->m_val, m_len); + } + + bool m_isvalid; + +private: + void resize(uint32_t newlen) + { + delete[] m_val; + m_val = NULL; + m_storage_len = newlen; + m_val = new uint8_t[m_storage_len]; + } + + uint32_t m_storage_len; +}; + +struct sinsp_table_field_hasher +{ + size_t operator()(const sinsp_table_field& k) const + { + size_t h = 0; + uint8_t* s = k.m_val; + uint32_t len = k.m_len; + + while(--len) + { + h = h * 101 + (unsigned) *s++; + } + + return h; + } +}; + +class sinsp_table_buffer +{ +public: + sinsp_table_buffer() + { + push_buffer(); + } + + ~sinsp_table_buffer() + { + for(auto it = m_bufs.begin(); it != m_bufs.end(); ++it) + { + delete[] *it; + } + } + + void push_buffer() + { + m_curbuf = new uint8_t[SINSP_TABLE_BUFFER_ENTRY_SIZE]; + m_bufs.push_back(m_curbuf); + m_pos = 0; + } + + uint8_t* copy(uint8_t* src, uint32_t len) + { + if(m_pos + len >= SINSP_TABLE_BUFFER_ENTRY_SIZE) + { + push_buffer(); + } + + uint8_t* dest = m_curbuf + m_pos; + memcpy(dest, src, len); + m_pos += len; + return dest; + } + + uint8_t* reserve(uint32_t len) + { + if(len >= SINSP_TABLE_BUFFER_ENTRY_SIZE) + { + ASSERT(false); + throw sinsp_exception("field value too long"); + } + + if(m_pos + len >= SINSP_TABLE_BUFFER_ENTRY_SIZE) + { + push_buffer(); + } + + uint8_t* dest = m_curbuf + m_pos; + m_pos += len; + return dest; + } + + void clear() + { + for(auto it = m_bufs.begin(); it != m_bufs.end(); ++it) + { + delete[] *it; + } + + m_bufs.clear(); + push_buffer(); + m_pos = 0; + } + + vector m_bufs; + uint8_t* m_curbuf; + uint32_t m_pos; +}; + +class sinsp_sample_row +{ +public: + sinsp_table_field m_key; + vector m_values; +}; + +class sinsp_table +{ +public: + enum tabletype + { + TT_NONE = 0, + TT_TABLE, + TT_LIST, + }; + + enum output_type + { + OT_CURSES, + OT_RAW, + OT_JSON, + }; + + sinsp_table(sinsp* inspector, tabletype type, + uint64_t refresh_interval_ns, sinsp_table::output_type output_type, + uint32_t json_first_row, uint32_t json_last_row); + ~sinsp_table(); + void configure(vector* entries, const string& filter, bool use_defaults, uint32_t view_depth); + void process_event(sinsp_evt* evt); + void flush(sinsp_evt* evt); + void filter_sample(); + // + // Returns the key of the first match, or NULL if no match + // + sinsp_table_field* search_in_sample(string text); + void sort_sample(); + vector* get_sample(uint64_t time_delta); + vector* get_legend() + { + if(m_do_merging) + { + return &m_postmerge_legend; + } + else + { + return &m_premerge_legend; + } + } + void set_sorting_col(uint32_t col); + uint32_t get_sorting_col(); + pair get_row_key_name_and_val(uint32_t rownum, bool force); + sinsp_table_field* get_row_key(uint32_t rownum); + int32_t get_row_from_key(sinsp_table_field* key); + void set_paused(bool paused); + void set_freetext_filter(string filter) + { + m_freetext_filter = filter; + } + tabletype get_type() + { + return m_type; + } + void set_refresh_interval(uint64_t newinterval_ns) + { + m_refresh_interval_ns = newinterval_ns; + } + void clear(); + bool is_merging() + { + return m_do_merging; + } + bool is_sorting_ascending() + { + return m_is_sorting_ascending; + } + void set_is_sorting_ascending(bool is_sorting_ascending) + { + m_is_sorting_ascending = is_sorting_ascending; + } + + uint64_t m_next_flush_time_ns; + uint64_t m_prev_flush_time_ns; + uint64_t m_refresh_interval_ns; + vector* m_types; + uint64_t m_json_output_lines_count; + +private: + inline void add_row(bool merging); + inline void add_fields_sum(ppm_param_type type, sinsp_table_field* dst, sinsp_table_field* src); + inline void add_fields_sum_of_avg(ppm_param_type type, sinsp_table_field* dst, sinsp_table_field* src); + inline void add_fields_max(ppm_param_type type, sinsp_table_field* dst, sinsp_table_field* src); + inline void add_fields_min(ppm_param_type type, sinsp_table_field* dst, sinsp_table_field* src); + inline void add_fields(uint32_t dst_id, sinsp_table_field* src, uint32_t aggr); + void process_proctable(sinsp_evt* evt); + inline uint32_t get_field_len(uint32_t id); + inline uint8_t* get_default_val(filtercheck_field_info* fld); + void create_sample(); + void switch_buffers(); + void print_raw(vector* sample_data, uint64_t time_delta); + void print_json(vector* sample_data, uint64_t time_delta); + + sinsp* m_inspector; + unordered_map* m_table; + unordered_map m_premerge_table; + unordered_map m_merge_table; + vector m_premerge_legend; + vector m_premerge_extractors; + vector m_postmerge_extractors; + vector* m_extractors; + vector m_chks_to_free; + vector m_premerge_types; + vector m_postmerge_types; + bool m_is_key_present; + bool m_is_groupby_key_present; + vector m_groupby_columns; + vector m_postmerge_legend; + sinsp_table_field* m_fld_pointers; + sinsp_table_field* m_premerge_fld_pointers; + sinsp_table_field* m_postmerge_fld_pointers; + uint32_t m_n_fields; + uint32_t m_n_premerge_fields; + uint32_t m_n_postmerge_fields; + sinsp_table_buffer* m_buffer; + sinsp_table_buffer m_buffer1; + sinsp_table_buffer m_buffer2; + uint32_t m_vals_array_sz; + uint32_t m_premerge_vals_array_sz; + uint32_t m_postmerge_vals_array_sz; + sinsp_filter_check_reference* m_printer; + vector m_full_sample_data; + vector m_filtered_sample_data; + vector* m_sample_data; + sinsp_table_field* m_vals; + int32_t m_sorting_col; + bool m_just_sorted; + bool m_is_sorting_ascending; + bool m_do_merging; + sinsp_filter* m_filter; + bool m_use_defaults; + uint64_t m_zero_u64; + uint64_t m_zero_double; + bool m_paused; + string m_freetext_filter; + tabletype m_type; + output_type m_output_type; + uint32_t m_view_depth; + uint32_t m_json_first_row; + uint32_t m_json_last_row; + + friend class curses_table; + friend class sinsp_cursesui; +}; diff --git a/userspace/libsinsp/test/CMakeLists.txt b/userspace/libsinsp/test/CMakeLists.txt new file mode 100644 index 0000000000..71f9c8d19b --- /dev/null +++ b/userspace/libsinsp/test/CMakeLists.txt @@ -0,0 +1,43 @@ +# +# Copyright (C) 2019 Sysdig Inc. +# +# This file is part of sysdig . +# +# 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_directories("${GTEST_INCLUDE_DIR}") +include_directories("${JSONCPP_INCLUDE}") +include_directories("${TBB_INCLUDE_DIR}") +if(NOT MINIMAL_BUILD) + include_directories("${CURL_INCLUDE_DIR}") +endif() # MINIMAL_BUILD + +include_directories("..") +include_directories("../../libscap") + +add_executable(unit-test-libsinsp + cgroup_list_counter.ut.cpp + sinsp.ut.cpp +) + +target_link_libraries(unit-test-libsinsp + "${GTEST_LIB}" + "${GTEST_MAIN_LIB}" + sinsp +) + +add_custom_target(run-unit-test-libsinsp + DEPENDS unit-test-libsinsp + COMMAND unit-test-libsinsp +) diff --git a/userspace/libsinsp/test/cgroup_list_counter.ut.cpp b/userspace/libsinsp/test/cgroup_list_counter.ut.cpp new file mode 100644 index 0000000000..bd6322d7c5 --- /dev/null +++ b/userspace/libsinsp/test/cgroup_list_counter.ut.cpp @@ -0,0 +1,65 @@ +/* +Copyright (C) 2019 Sysdig Inc. + +This file is part of sysdig. + +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 +#include + +TEST(cgroup_list_counter_test, basic) +{ + libsinsp::cgroup_list_counter counter; + ASSERT_EQ(8, counter("0-5,8,14")); + ASSERT_EQ(1, counter("5")); + ASSERT_EQ(6, counter("9-14")); +} + +TEST(cgroup_list_counter_test, invalid_value) +{ + libsinsp::cgroup_list_counter counter; + ASSERT_EQ(-1, counter("")); + ASSERT_EQ(-1, counter(",1")); +} + +TEST(cgroup_list_counter_test, invalid_range_missing_number) +{ + libsinsp::cgroup_list_counter counter; + ASSERT_EQ(-1, counter("-5,8,14")); + ASSERT_EQ(-1, counter("1,-5,8,14")); + ASSERT_EQ(-1, counter("1,4-,14")); + ASSERT_EQ(-1, counter("1,4-")); +} + +TEST(cgroup_list_counter_test, invalid_range_double_dash) +{ + libsinsp::cgroup_list_counter counter; + ASSERT_EQ(-1, counter("1,4-5-6,14")); +} + +TEST(cgroup_list_counter_test, invalid_range_wrong_order) +{ + libsinsp::cgroup_list_counter counter; + ASSERT_EQ(-1, counter("1,6-5,14")); +} + +TEST(cgroup_list_counter_test, not_a_number) +{ + libsinsp::cgroup_list_counter counter; + ASSERT_EQ(-1, counter("1,5-a,14")); +} + + diff --git a/userspace/libsinsp/test/sinsp.ut.cpp b/userspace/libsinsp/test/sinsp.ut.cpp new file mode 100644 index 0000000000..82757a2b17 --- /dev/null +++ b/userspace/libsinsp/test/sinsp.ut.cpp @@ -0,0 +1,40 @@ +/* +Copyright (C) 2019 Sysdig Inc. + +This file is part of sysdig. + +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 "sinsp.h" +#include + +using namespace libsinsp; + +class sinsp_external_processor_dummy : public event_processor +{ + void on_capture_start() override {} + void process_event(sinsp_evt* evt, event_return rc) override {} + void add_chisel_metric(statsd_metric* metric) override {} +}; + +TEST(sinsp, external_event_processor_initialization) +{ + sinsp my_sinsp; + EXPECT_EQ(my_sinsp.get_external_event_processor(), nullptr); + sinsp_external_processor_dummy processor; + my_sinsp.register_external_event_processor(processor); + EXPECT_EQ(my_sinsp.get_external_event_processor(), &processor); +} + diff --git a/userspace/libsinsp/third-party/jsoncpp/json/json-forwards.h b/userspace/libsinsp/third-party/jsoncpp/json/json-forwards.h index 28a6ad7d66..ccbdb2b13c 100644 --- a/userspace/libsinsp/third-party/jsoncpp/json/json-forwards.h +++ b/userspace/libsinsp/third-party/jsoncpp/json/json-forwards.h @@ -1,5 +1,5 @@ /// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/). -/// It is intented to be used with #include +/// It is intended to be used with #include "json/json-forwards.h" /// This header provides forward declaration for all JsonCpp types. // ////////////////////////////////////////////////////////////////////// @@ -77,7 +77,7 @@ license you like. # define JSON_FORWARD_AMALGATED_H_INCLUDED /// If defined, indicates that the source file is amalgated /// to prevent private header inclusion. -#define JSON_IS_AMALGATED +#define JSON_IS_AMALGAMATION // ////////////////////////////////////////////////////////////////////// // Beginning of content of file: include/json/config.h @@ -89,95 +89,108 @@ license you like. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef JSON_CONFIG_H_INCLUDED -# define JSON_CONFIG_H_INCLUDED +#define JSON_CONFIG_H_INCLUDED /// If defined, indicates that json library is embedded in CppTL library. //# define JSON_IN_CPPTL 1 /// If defined, indicates that json may leverage CppTL library //# define JSON_USE_CPPTL 1 -/// If defined, indicates that cpptl vector based map should be used instead of std::map +/// If defined, indicates that cpptl vector based map should be used instead of +/// std::map /// as Value container. //# define JSON_USE_CPPTL_SMALLMAP 1 -/// If defined, indicates that Json specific container should be used -/// (hash table & simple deque container with customizable allocator). -/// THIS FEATURE IS STILL EXPERIMENTAL! There is know bugs: See #3177332 -//# define JSON_VALUE_USE_INTERNAL_MAP 1 -/// Force usage of standard new/malloc based allocator instead of memory pool based allocator. -/// The memory pools allocator used optimization (initializing Value and ValueInternalLink -/// as if it was a POD) that may cause some validation tool to report errors. -/// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined. -//# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1 - -/// If defined, indicates that Json use exception to report invalid type manipulation -/// instead of C assert macro. -# define JSON_USE_EXCEPTION 1 + +// If non-zero, the library uses exceptions to report bad input instead of C +// assertion macros. The default is to use exceptions. +#ifndef JSON_USE_EXCEPTION +#define JSON_USE_EXCEPTION 1 +#endif /// If defined, indicates that the source file is amalgated /// to prevent private header inclusion. /// Remarks: it is automatically defined in the generated amalgated header. // #define JSON_IS_AMALGAMATION +#ifdef JSON_IN_CPPTL +#include +#ifndef JSON_USE_CPPTL +#define JSON_USE_CPPTL 1 +#endif +#endif + +#ifdef JSON_IN_CPPTL +#define JSON_API CPPTL_API +#elif defined(JSON_DLL_BUILD) +#if defined(_MSC_VER) +#define JSON_API __declspec(dllexport) +#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING +#endif // if defined(_MSC_VER) +#elif defined(JSON_DLL) +#if defined(_MSC_VER) +#define JSON_API __declspec(dllimport) +#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING +#endif // if defined(_MSC_VER) +#endif // ifdef JSON_IN_CPPTL +#if !defined(JSON_API) +#define JSON_API +#endif -# ifdef JSON_IN_CPPTL -# include -# ifndef JSON_USE_CPPTL -# define JSON_USE_CPPTL 1 -# endif -# endif - -# ifdef JSON_IN_CPPTL -# define JSON_API CPPTL_API -# elif defined(JSON_DLL_BUILD) -# define JSON_API __declspec(dllexport) -# elif defined(JSON_DLL) -# define JSON_API __declspec(dllimport) -# else -# define JSON_API -# endif - -// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for integer +// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for +// integer // Storages, and 64 bits integer support is disabled. // #define JSON_NO_INT64 1 -#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6 +#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6 // Microsoft Visual Studio 6 only support conversion from __int64 to double // (no conversion from unsigned __int64). #define JSON_USE_INT64_DOUBLE_CONVERSION 1 +// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255' +// characters in the debug information) +// All projects I've ever seen with VS6 were using this globally (not bothering +// with pragma push/pop). +#pragma warning(disable : 4786) #endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6 -#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008 +#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008 /// Indicates that the following function is deprecated. -# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) +#define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) +#elif defined(__clang__) && defined(__has_feature) +#if __has_feature(attribute_deprecated_with_message) +#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message))) +#endif +#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) +#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message))) +#elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) +#define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__)) #endif #if !defined(JSONCPP_DEPRECATED) -# define JSONCPP_DEPRECATED(message) +#define JSONCPP_DEPRECATED(message) #endif // if !defined(JSONCPP_DEPRECATED) namespace Json { - typedef int Int; - typedef unsigned int UInt; -# if defined(JSON_NO_INT64) - typedef int LargestInt; - typedef unsigned int LargestUInt; -# undef JSON_HAS_INT64 -# else // if defined(JSON_NO_INT64) - // For Microsoft Visual use specific types as long long is not supported -# if defined(_MSC_VER) // Microsoft Visual Studio - typedef __int64 Int64; - typedef unsigned __int64 UInt64; -# else // if defined(_MSC_VER) // Other platforms, use long long - typedef long long int Int64; - typedef unsigned long long int UInt64; -# endif // if defined(_MSC_VER) - typedef Int64 LargestInt; - typedef UInt64 LargestUInt; -# define JSON_HAS_INT64 -# endif // if defined(JSON_NO_INT64) +typedef int Int; +typedef unsigned int UInt; +#if defined(JSON_NO_INT64) +typedef int LargestInt; +typedef unsigned int LargestUInt; +#undef JSON_HAS_INT64 +#else // if defined(JSON_NO_INT64) +// For Microsoft Visual use specific types as long long is not supported +#if defined(_MSC_VER) // Microsoft Visual Studio +typedef __int64 Int64; +typedef unsigned __int64 UInt64; +#else // if defined(_MSC_VER) // Other platforms, use long long +typedef long long int Int64; +typedef unsigned long long int UInt64; +#endif // if defined(_MSC_VER) +typedef Int64 LargestInt; +typedef UInt64 LargestUInt; +#define JSON_HAS_INT64 +#endif // if defined(JSON_NO_INT64) } // end namespace Json - #endif // JSON_CONFIG_H_INCLUDED // ////////////////////////////////////////////////////////////////////// @@ -199,42 +212,35 @@ namespace Json { // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef JSON_FORWARDS_H_INCLUDED -# define JSON_FORWARDS_H_INCLUDED +#define JSON_FORWARDS_H_INCLUDED #if !defined(JSON_IS_AMALGAMATION) -# include "config.h" +#include "config.h" #endif // if !defined(JSON_IS_AMALGAMATION) namespace Json { - // writer.h - class FastWriter; - class StyledWriter; - - // reader.h - class Reader; - - // features.h - class Features; - - // value.h - typedef unsigned int ArrayIndex; - class StaticString; - class Path; - class PathArgument; - class Value; - class ValueIteratorBase; - class ValueIterator; - class ValueConstIterator; -#ifdef JSON_VALUE_USE_INTERNAL_MAP - class ValueMapAllocator; - class ValueInternalLink; - class ValueInternalArray; - class ValueInternalMap; -#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP +// writer.h +class FastWriter; +class StyledWriter; -} // namespace Json +// reader.h +class Reader; +// features.h +class Features; + +// value.h +typedef unsigned int ArrayIndex; +class StaticString; +class Path; +class PathArgument; +class Value; +class ValueIteratorBase; +class ValueIterator; +class ValueConstIterator; + +} // namespace Json #endif // JSON_FORWARDS_H_INCLUDED diff --git a/userspace/libsinsp/third-party/jsoncpp/json/json.h b/userspace/libsinsp/third-party/jsoncpp/json/json.h index 79fec96147..6859137eff 100644 --- a/userspace/libsinsp/third-party/jsoncpp/json/json.h +++ b/userspace/libsinsp/third-party/jsoncpp/json/json.h @@ -1,5 +1,5 @@ /// Json-cpp amalgated header (http://jsoncpp.sourceforge.net/). -/// It is intented to be used with #include +/// It is intended to be used with #include "json/json.h" // ////////////////////////////////////////////////////////////////////// // Beginning of content of file: LICENSE @@ -70,14 +70,40 @@ license you like. -#define JSON_IS_AMALGAMATION #ifndef JSON_AMALGATED_H_INCLUDED # define JSON_AMALGATED_H_INCLUDED /// If defined, indicates that the source file is amalgated /// to prevent private header inclusion. -#define JSON_IS_AMALGATED +#define JSON_IS_AMALGAMATION + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/version.h +// ////////////////////////////////////////////////////////////////////// + +// DO NOT EDIT. This file (and "version") is generated by CMake. +// Run CMake configure step to update it. +#ifndef JSON_VERSION_H_INCLUDED +# define JSON_VERSION_H_INCLUDED + +# define JSONCPP_VERSION_STRING "0.10.6" +# define JSONCPP_VERSION_MAJOR 0 +# define JSONCPP_VERSION_MINOR 10 +# define JSONCPP_VERSION_PATCH 6 +# define JSONCPP_VERSION_QUALIFIER +# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8)) + +#endif // JSON_VERSION_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/version.h +// ////////////////////////////////////////////////////////////////////// + + + + + // ////////////////////////////////////////////////////////////////////// // Beginning of content of file: include/json/config.h @@ -89,95 +115,108 @@ license you like. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef JSON_CONFIG_H_INCLUDED -# define JSON_CONFIG_H_INCLUDED +#define JSON_CONFIG_H_INCLUDED /// If defined, indicates that json library is embedded in CppTL library. //# define JSON_IN_CPPTL 1 /// If defined, indicates that json may leverage CppTL library //# define JSON_USE_CPPTL 1 -/// If defined, indicates that cpptl vector based map should be used instead of std::map +/// If defined, indicates that cpptl vector based map should be used instead of +/// std::map /// as Value container. //# define JSON_USE_CPPTL_SMALLMAP 1 -/// If defined, indicates that Json specific container should be used -/// (hash table & simple deque container with customizable allocator). -/// THIS FEATURE IS STILL EXPERIMENTAL! There is know bugs: See #3177332 -//# define JSON_VALUE_USE_INTERNAL_MAP 1 -/// Force usage of standard new/malloc based allocator instead of memory pool based allocator. -/// The memory pools allocator used optimization (initializing Value and ValueInternalLink -/// as if it was a POD) that may cause some validation tool to report errors. -/// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined. -//# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1 - -/// If defined, indicates that Json use exception to report invalid type manipulation -/// instead of C assert macro. -# define JSON_USE_EXCEPTION 1 + +// If non-zero, the library uses exceptions to report bad input instead of C +// assertion macros. The default is to use exceptions. +#ifndef JSON_USE_EXCEPTION +#define JSON_USE_EXCEPTION 1 +#endif /// If defined, indicates that the source file is amalgated /// to prevent private header inclusion. /// Remarks: it is automatically defined in the generated amalgated header. // #define JSON_IS_AMALGAMATION +#ifdef JSON_IN_CPPTL +#include +#ifndef JSON_USE_CPPTL +#define JSON_USE_CPPTL 1 +#endif +#endif + +#ifdef JSON_IN_CPPTL +#define JSON_API CPPTL_API +#elif defined(JSON_DLL_BUILD) +#if defined(_MSC_VER) +#define JSON_API __declspec(dllexport) +#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING +#endif // if defined(_MSC_VER) +#elif defined(JSON_DLL) +#if defined(_MSC_VER) +#define JSON_API __declspec(dllimport) +#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING +#endif // if defined(_MSC_VER) +#endif // ifdef JSON_IN_CPPTL +#if !defined(JSON_API) +#define JSON_API +#endif -# ifdef JSON_IN_CPPTL -# include -# ifndef JSON_USE_CPPTL -# define JSON_USE_CPPTL 1 -# endif -# endif - -# ifdef JSON_IN_CPPTL -# define JSON_API CPPTL_API -# elif defined(JSON_DLL_BUILD) -# define JSON_API __declspec(dllexport) -# elif defined(JSON_DLL) -# define JSON_API __declspec(dllimport) -# else -# define JSON_API -# endif - -// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for integer +// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for +// integer // Storages, and 64 bits integer support is disabled. // #define JSON_NO_INT64 1 -#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6 +#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6 // Microsoft Visual Studio 6 only support conversion from __int64 to double // (no conversion from unsigned __int64). #define JSON_USE_INT64_DOUBLE_CONVERSION 1 +// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255' +// characters in the debug information) +// All projects I've ever seen with VS6 were using this globally (not bothering +// with pragma push/pop). +#pragma warning(disable : 4786) #endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6 -#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008 +#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008 /// Indicates that the following function is deprecated. -# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) +#define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) +#elif defined(__clang__) && defined(__has_feature) +#if __has_feature(attribute_deprecated_with_message) +#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message))) +#endif +#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) +#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message))) +#elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) +#define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__)) #endif #if !defined(JSONCPP_DEPRECATED) -# define JSONCPP_DEPRECATED(message) +#define JSONCPP_DEPRECATED(message) #endif // if !defined(JSONCPP_DEPRECATED) namespace Json { - typedef int Int; - typedef unsigned int UInt; -# if defined(JSON_NO_INT64) - typedef int LargestInt; - typedef unsigned int LargestUInt; -# undef JSON_HAS_INT64 -# else // if defined(JSON_NO_INT64) - // For Microsoft Visual use specific types as long long is not supported -# if defined(_MSC_VER) // Microsoft Visual Studio - typedef __int64 Int64; - typedef unsigned __int64 UInt64; -# else // if defined(_MSC_VER) // Other platforms, use long long - typedef long long int Int64; - typedef unsigned long long int UInt64; -# endif // if defined(_MSC_VER) - typedef Int64 LargestInt; - typedef UInt64 LargestUInt; -# define JSON_HAS_INT64 -# endif // if defined(JSON_NO_INT64) +typedef int Int; +typedef unsigned int UInt; +#if defined(JSON_NO_INT64) +typedef int LargestInt; +typedef unsigned int LargestUInt; +#undef JSON_HAS_INT64 +#else // if defined(JSON_NO_INT64) +// For Microsoft Visual use specific types as long long is not supported +#if defined(_MSC_VER) // Microsoft Visual Studio +typedef __int64 Int64; +typedef unsigned __int64 UInt64; +#else // if defined(_MSC_VER) // Other platforms, use long long +typedef long long int Int64; +typedef unsigned long long int UInt64; +#endif // if defined(_MSC_VER) +typedef Int64 LargestInt; +typedef UInt64 LargestUInt; +#define JSON_HAS_INT64 +#endif // if defined(JSON_NO_INT64) } // end namespace Json - #endif // JSON_CONFIG_H_INCLUDED // ////////////////////////////////////////////////////////////////////// @@ -199,42 +238,35 @@ namespace Json { // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef JSON_FORWARDS_H_INCLUDED -# define JSON_FORWARDS_H_INCLUDED +#define JSON_FORWARDS_H_INCLUDED #if !defined(JSON_IS_AMALGAMATION) -# include "config.h" +#include "config.h" #endif // if !defined(JSON_IS_AMALGAMATION) namespace Json { - // writer.h - class FastWriter; - class StyledWriter; - - // reader.h - class Reader; - - // features.h - class Features; - - // value.h - typedef unsigned int ArrayIndex; - class StaticString; - class Path; - class PathArgument; - class Value; - class ValueIteratorBase; - class ValueIterator; - class ValueConstIterator; -#ifdef JSON_VALUE_USE_INTERNAL_MAP - class ValueMapAllocator; - class ValueInternalLink; - class ValueInternalArray; - class ValueInternalMap; -#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP +// writer.h +class FastWriter; +class StyledWriter; -} // namespace Json +// reader.h +class Reader; + +// features.h +class Features; +// value.h +typedef unsigned int ArrayIndex; +class StaticString; +class Path; +class PathArgument; +class Value; +class ValueIteratorBase; +class ValueIterator; +class ValueConstIterator; + +} // namespace Json #endif // JSON_FORWARDS_H_INCLUDED @@ -257,45 +289,47 @@ namespace Json { // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef CPPTL_JSON_FEATURES_H_INCLUDED -# define CPPTL_JSON_FEATURES_H_INCLUDED +#define CPPTL_JSON_FEATURES_H_INCLUDED #if !defined(JSON_IS_AMALGAMATION) -# include "forwards.h" +#include "forwards.h" #endif // if !defined(JSON_IS_AMALGAMATION) namespace Json { - /** \brief Configuration passed to reader and writer. - * This configuration object can be used to force the Reader or Writer - * to behave in a standard conforming way. - */ - class JSON_API Features - { - public: - /** \brief A configuration that allows all features and assumes all strings are UTF-8. - * - C & C++ comments are allowed - * - Root object can be any JSON value - * - Assumes Value strings are encoded in UTF-8 - */ - static Features all(); - - /** \brief A configuration that is strictly compatible with the JSON specification. - * - Comments are forbidden. - * - Root object must be either an array or an object value. - * - Assumes Value strings are encoded in UTF-8 - */ - static Features strictMode(); - - /** \brief Initialize the configuration like JsonConfig::allFeatures; - */ - Features(); - - /// \c true if comments are allowed. Default: \c true. - bool allowComments_; - - /// \c true if root must be either an array or an object value. Default: \c false. - bool strictRoot_; - }; +/** \brief Configuration passed to reader and writer. + * This configuration object can be used to force the Reader or Writer + * to behave in a standard conforming way. + */ +class JSON_API Features { +public: + /** \brief A configuration that allows all features and assumes all strings + * are UTF-8. + * - C & C++ comments are allowed + * - Root object can be any JSON value + * - Assumes Value strings are encoded in UTF-8 + */ + static Features all(); + + /** \brief A configuration that is strictly compatible with the JSON + * specification. + * - Comments are forbidden. + * - Root object must be either an array or an object value. + * - Assumes Value strings are encoded in UTF-8 + */ + static Features strictMode(); + + /** \brief Initialize the configuration like JsonConfig::allFeatures; + */ + Features(); + + /// \c true if comments are allowed. Default: \c true. + bool allowComments_; + + /// \c true if root must be either an array or an object value. Default: \c + /// false. + bool strictRoot_; +}; } // namespace Json @@ -320,1102 +354,844 @@ namespace Json { // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef CPPTL_JSON_H_INCLUDED -# define CPPTL_JSON_H_INCLUDED +#define CPPTL_JSON_H_INCLUDED #if !defined(JSON_IS_AMALGAMATION) -# include "forwards.h" +#include "forwards.h" #endif // if !defined(JSON_IS_AMALGAMATION) -# include -# include - -# ifndef JSON_USE_CPPTL_SMALLMAP -# include -# else -# include -# endif -# ifdef JSON_USE_CPPTL -# include -# endif +#include +#include +#include + +#ifndef JSON_USE_CPPTL_SMALLMAP +#include +#else +#include +#endif +#ifdef JSON_USE_CPPTL +#include +#endif + +// Disable warning C4251: : needs to have dll-interface to +// be used by... +#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +#pragma warning(push) +#pragma warning(disable : 4251) +#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) + +//Conditional NORETURN attribute on the throw functions would: +// a) suppress false positives from static code analysis +// b) possibly improve optimization opportunities. +#if !defined(JSONCPP_NORETURN) +# if defined(_MSC_VER) +# define JSONCPP_NORETURN __declspec(noreturn) +# elif defined(__GNUC__) +# define JSONCPP_NORETURN __attribute__ ((__noreturn__)) +# else +# define JSONCPP_NORETURN +# endif +#endif /** \brief JSON (JavaScript Object Notation). */ namespace Json { - /** \brief Type of the value held by a Value object. - */ - enum ValueType - { - nullValue = 0, ///< 'null' value - intValue, ///< signed integer value - uintValue, ///< unsigned integer value - realValue, ///< double value - stringValue, ///< UTF-8 string value - booleanValue, ///< bool value - arrayValue, ///< array value (ordered list) - objectValue ///< object value (collection of name/value pairs). - }; - - enum CommentPlacement - { - commentBefore = 0, ///< a comment placed on the line before a value - commentAfterOnSameLine, ///< a comment just after a value on the same line - commentAfter, ///< a comment on the line after a value (only make sense for root value) - numberOfCommentPlacement - }; +/** Base class for all exceptions we throw. + * + * We use nothing but these internally. Of course, STL can throw others. + */ +class JSON_API Exception : public std::exception { +public: + Exception(std::string const& msg); + virtual ~Exception() throw(); + virtual char const* what() const throw(); +protected: + std::string const msg_; +}; + +/** Exceptions which the user cannot easily avoid. + * + * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input + * + * \remark derived from Json::Exception + */ +class JSON_API RuntimeError : public Exception { +public: + RuntimeError(std::string const& msg); +}; + +/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros. + * + * These are precondition-violations (user bugs) and internal errors (our bugs). + * + * \remark derived from Json::Exception + */ +class JSON_API LogicError : public Exception { +public: + LogicError(std::string const& msg); +}; + +/// used internally +JSONCPP_NORETURN void throwRuntimeError(std::string const& msg); +/// used internally +JSONCPP_NORETURN void throwLogicError(std::string const& msg); + +/** \brief Type of the value held by a Value object. + */ +enum ValueType { + nullValue = 0, ///< 'null' value + intValue, ///< signed integer value + uintValue, ///< unsigned integer value + realValue, ///< double value + stringValue, ///< UTF-8 string value + booleanValue, ///< bool value + arrayValue, ///< array value (ordered list) + objectValue ///< object value (collection of name/value pairs). +}; + +enum CommentPlacement { + commentBefore = 0, ///< a comment placed on the line before a value + commentAfterOnSameLine, ///< a comment just after a value on the same line + commentAfter, ///< a comment on the line after a value (only make sense for + /// root value) + numberOfCommentPlacement +}; //# ifdef JSON_USE_CPPTL // typedef CppTL::AnyEnumerator EnumMemberNames; // typedef CppTL::AnyEnumerator EnumValues; //# endif - /** \brief Lightweight wrapper to tag static string. - * - * Value constructor and objectValue member assignement takes advantage of the - * StaticString and avoid the cost of string duplication when storing the - * string or the member name. - * - * Example of usage: - * \code - * Json::Value aValue( StaticString("some text") ); - * Json::Value object; - * static const StaticString code("code"); - * object[code] = 1234; - * \endcode - */ - class JSON_API StaticString - { - public: - explicit StaticString( const char *czstring ) - : str_( czstring ) - { - } - - operator const char *() const - { - return str_; - } - - const char *c_str() const - { - return str_; - } - - private: - const char *str_; - }; - - /** \brief Represents a JSON value. - * - * This class is a discriminated union wrapper that can represents a: - * - signed integer [range: Value::minInt - Value::maxInt] - * - unsigned integer (range: 0 - Value::maxUInt) - * - double - * - UTF-8 string - * - boolean - * - 'null' - * - an ordered list of Value - * - collection of name/value pairs (javascript object) - * - * The type of the held value is represented by a #ValueType and - * can be obtained using type(). - * - * values of an #objectValue or #arrayValue can be accessed using operator[]() methods. - * Non const methods will automatically create the a #nullValue element - * if it does not exist. - * The sequence of an #arrayValue will be automatically resize and initialized - * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. - * - * The get() methods can be used to obtanis default value in the case the required element - * does not exist. - * - * It is possible to iterate over the list of a #objectValue values using - * the getMemberNames() method. - */ - class JSON_API Value - { - friend class ValueIteratorBase; -# ifdef JSON_VALUE_USE_INTERNAL_MAP - friend class ValueInternalLink; - friend class ValueInternalMap; -# endif - public: - typedef std::vector Members; - typedef ValueIterator iterator; - typedef ValueConstIterator const_iterator; - typedef Json::UInt UInt; - typedef Json::Int Int; -# if defined(JSON_HAS_INT64) - typedef Json::UInt64 UInt64; - typedef Json::Int64 Int64; +/** \brief Lightweight wrapper to tag static string. + * + * Value constructor and objectValue member assignement takes advantage of the + * StaticString and avoid the cost of string duplication when storing the + * string or the member name. + * + * Example of usage: + * \code + * Json::Value aValue( StaticString("some text") ); + * Json::Value object; + * static const StaticString code("code"); + * object[code] = 1234; + * \endcode + */ +class JSON_API StaticString { +public: + explicit StaticString(const char* czstring) : c_str_(czstring) {} + + operator const char*() const { return c_str_; } + + const char* c_str() const { return c_str_; } + +private: + const char* c_str_; +}; + +/** \brief Represents a JSON value. + * + * This class is a discriminated union wrapper that can represents a: + * - signed integer [range: Value::minInt - Value::maxInt] + * - unsigned integer (range: 0 - Value::maxUInt) + * - double + * - UTF-8 string + * - boolean + * - 'null' + * - an ordered list of Value + * - collection of name/value pairs (javascript object) + * + * The type of the held value is represented by a #ValueType and + * can be obtained using type(). + * + * Values of an #objectValue or #arrayValue can be accessed using operator[]() + * methods. + * Non-const methods will automatically create the a #nullValue element + * if it does not exist. + * The sequence of an #arrayValue will be automatically resized and initialized + * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. + * + * The get() methods can be used to obtain default value in the case the + * required element does not exist. + * + * It is possible to iterate over the list of a #objectValue values using + * the getMemberNames() method. + * + * \note #Value string-length fit in size_t, but keys must be < 2^30. + * (The reason is an implementation detail.) A #CharReader will raise an + * exception if a bound is exceeded to avoid security holes in your app, + * but the Value API does *not* check bounds. That is the responsibility + * of the caller. + */ +class JSON_API Value { + friend class ValueIteratorBase; +public: + typedef std::vector Members; + typedef ValueIterator iterator; + typedef ValueConstIterator const_iterator; + typedef Json::UInt UInt; + typedef Json::Int Int; +#if defined(JSON_HAS_INT64) + typedef Json::UInt64 UInt64; + typedef Json::Int64 Int64; #endif // defined(JSON_HAS_INT64) - typedef Json::LargestInt LargestInt; - typedef Json::LargestUInt LargestUInt; - typedef Json::ArrayIndex ArrayIndex; - - static const Value null; - /// Minimum signed integer value that can be stored in a Json::Value. - static const LargestInt minLargestInt; - /// Maximum signed integer value that can be stored in a Json::Value. - static const LargestInt maxLargestInt; - /// Maximum unsigned integer value that can be stored in a Json::Value. - static const LargestUInt maxLargestUInt; - - /// Minimum signed int value that can be stored in a Json::Value. - static const Int minInt; - /// Maximum signed int value that can be stored in a Json::Value. - static const Int maxInt; - /// Maximum unsigned int value that can be stored in a Json::Value. - static const UInt maxUInt; - - /// Minimum signed 64 bits int value that can be stored in a Json::Value. - static const Int64 minInt64; - /// Maximum signed 64 bits int value that can be stored in a Json::Value. - static const Int64 maxInt64; - /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. - static const UInt64 maxUInt64; - - private: -#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION -# ifndef JSON_VALUE_USE_INTERNAL_MAP - class CZString - { - public: - enum DuplicationPolicy - { - noDuplication = 0, - duplicate, - duplicateOnCopy - }; - CZString( ArrayIndex index ); - CZString( const char *cstr, DuplicationPolicy allocate ); - CZString( const CZString &other ); - ~CZString(); - CZString &operator =( const CZString &other ); - bool operator<( const CZString &other ) const; - bool operator==( const CZString &other ) const; - ArrayIndex index() const; - const char *c_str() const; - bool isStaticString() const; - private: - void swap( CZString &other ); - const char *cstr_; - ArrayIndex index_; - }; - - public: -# ifndef JSON_USE_CPPTL_SMALLMAP - typedef std::map ObjectValues; -# else - typedef CppTL::SmallMap ObjectValues; -# endif // ifndef JSON_USE_CPPTL_SMALLMAP -# endif // ifndef JSON_VALUE_USE_INTERNAL_MAP -#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + typedef Json::LargestInt LargestInt; + typedef Json::LargestUInt LargestUInt; + typedef Json::ArrayIndex ArrayIndex; + + static const Value& nullRef; +#if !defined(__ARMEL__) + /// \deprecated This exists for binary compatibility only. Use nullRef. + static const Value null; +#endif + /// Minimum signed integer value that can be stored in a Json::Value. + static const LargestInt minLargestInt; + /// Maximum signed integer value that can be stored in a Json::Value. + static const LargestInt maxLargestInt; + /// Maximum unsigned integer value that can be stored in a Json::Value. + static const LargestUInt maxLargestUInt; + + /// Minimum signed int value that can be stored in a Json::Value. + static const Int minInt; + /// Maximum signed int value that can be stored in a Json::Value. + static const Int maxInt; + /// Maximum unsigned int value that can be stored in a Json::Value. + static const UInt maxUInt; - public: - /** \brief Create a default Value of the given type. - - This is a very useful constructor. - To create an empty array, pass arrayValue. - To create an empty object, pass objectValue. - Another Value can then be set to this one by assignment. - This is useful since clear() and resize() will not alter types. - - Examples: - \code - Json::Value null_value; // null - Json::Value arr_value(Json::arrayValue); // [] - Json::Value obj_value(Json::objectValue); // {} - \endcode - */ - Value( ValueType type = nullValue ); - Value( Int value ); - Value( UInt value ); #if defined(JSON_HAS_INT64) - Value( Int64 value ); - Value( UInt64 value ); -#endif // if defined(JSON_HAS_INT64) - Value( double value ); - Value( const char *value ); - Value( const char *beginValue, const char *endValue ); - /** \brief Constructs a value from a static string. - - * Like other value string constructor but do not duplicate the string for - * internal storage. The given string must remain alive after the call to this - * constructor. - * Example of usage: - * \code - * Json::Value aValue( StaticString("some text") ); - * \endcode - */ - Value( const StaticString &value ); - Value( const std::string &value ); -# ifdef JSON_USE_CPPTL - Value( const CppTL::ConstString &value ); -# endif - Value( bool value ); - Value( const Value &other ); - ~Value(); - - Value &operator=( const Value &other ); - /// Swap values. - /// \note Currently, comments are intentionally not swapped, for - /// both logic and efficiency. - void swap( Value &other ); - - ValueType type() const; - - bool operator <( const Value &other ) const; - bool operator <=( const Value &other ) const; - bool operator >=( const Value &other ) const; - bool operator >( const Value &other ) const; - - bool operator ==( const Value &other ) const; - bool operator !=( const Value &other ) const; - - int compare( const Value &other ) const; - - const char *asCString() const; - std::string asString() const; -# ifdef JSON_USE_CPPTL - CppTL::ConstString asConstString() const; -# endif - Int asInt() const; - UInt asUInt() const; - Int64 asInt64() const; - UInt64 asUInt64() const; - LargestInt asLargestInt() const; - LargestUInt asLargestUInt() const; - float asFloat() const; - double asDouble() const; - bool asBool() const; - - bool isNull() const; - bool isBool() const; - bool isInt() const; - bool isUInt() const; - bool isIntegral() const; - bool isDouble() const; - bool isNumeric() const; - bool isString() const; - bool isArray() const; - bool isObject() const; - - bool isConvertibleTo( ValueType other ) const; - - /// Number of values in array or object - ArrayIndex size() const; - - /// \brief Return true if empty array, empty object, or null; - /// otherwise, false. - bool empty() const; - - /// Return isNull() - bool operator!() const; - - /// Remove all object members and array elements. - /// \pre type() is arrayValue, objectValue, or nullValue - /// \post type() is unchanged - void clear(); - - /// Resize the array to size elements. - /// New elements are initialized to null. - /// May only be called on nullValue or arrayValue. - /// \pre type() is arrayValue or nullValue - /// \post type() is arrayValue - void resize( ArrayIndex size ); - - /// Access an array element (zero based index ). - /// If the array contains less than index element, then null value are inserted - /// in the array so that its size is index+1. - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - Value &operator[]( ArrayIndex index ); - - /// Access an array element (zero based index ). - /// If the array contains less than index element, then null value are inserted - /// in the array so that its size is index+1. - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - Value &operator[]( int index ); - - /// Access an array element (zero based index ) - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - const Value &operator[]( ArrayIndex index ) const; - - /// Access an array element (zero based index ) - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - const Value &operator[]( int index ) const; - - /// If the array contains at least index+1 elements, returns the element value, - /// otherwise returns defaultValue. - Value get( ArrayIndex index, - const Value &defaultValue ) const; - /// Return true if index < size(). - bool isValidIndex( ArrayIndex index ) const; - /// \brief Append value to array at the end. - /// - /// Equivalent to jsonvalue[jsonvalue.size()] = value; - Value &append( const Value &value ); - - /// Access an object value by name, create a null member if it does not exist. - Value &operator[]( const char *key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value &operator[]( const char *key ) const; - /// Access an object value by name, create a null member if it does not exist. - Value &operator[]( const std::string &key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value &operator[]( const std::string &key ) const; - /** \brief Access an object value by name, create a null member if it does not exist. - - * If the object as no entry for that name, then the member name used to store - * the new entry is not duplicated. - * Example of use: - * \code - * Json::Value object; - * static const StaticString code("code"); - * object[code] = 1234; - * \endcode - */ - Value &operator[]( const StaticString &key ); -# ifdef JSON_USE_CPPTL - /// Access an object value by name, create a null member if it does not exist. - Value &operator[]( const CppTL::ConstString &key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value &operator[]( const CppTL::ConstString &key ) const; -# endif - /// Return the member named key if it exist, defaultValue otherwise. - Value get( const char *key, - const Value &defaultValue ) const; - /// Return the member named key if it exist, defaultValue otherwise. - Value get( const std::string &key, - const Value &defaultValue ) const; -# ifdef JSON_USE_CPPTL - /// Return the member named key if it exist, defaultValue otherwise. - Value get( const CppTL::ConstString &key, - const Value &defaultValue ) const; -# endif - /// \brief Remove and return the named member. - /// - /// Do nothing if it did not exist. - /// \return the removed Value, or null. - /// \pre type() is objectValue or nullValue - /// \post type() is unchanged - Value removeMember( const char* key ); - /// Same as removeMember(const char*) - Value removeMember( const std::string &key ); - - /// Return true if the object has a member named key. - bool isMember( const char *key ) const; - /// Return true if the object has a member named key. - bool isMember( const std::string &key ) const; -# ifdef JSON_USE_CPPTL - /// Return true if the object has a member named key. - bool isMember( const CppTL::ConstString &key ) const; -# endif - - /// \brief Return a list of the member names. - /// - /// If null, return an empty list. - /// \pre type() is objectValue or nullValue - /// \post if type() was nullValue, it remains nullValue - Members getMemberNames() const; + /// Minimum signed 64 bits int value that can be stored in a Json::Value. + static const Int64 minInt64; + /// Maximum signed 64 bits int value that can be stored in a Json::Value. + static const Int64 maxInt64; + /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. + static const UInt64 maxUInt64; +#endif // defined(JSON_HAS_INT64) -//# ifdef JSON_USE_CPPTL -// EnumMemberNames enumMemberNames() const; -// EnumValues enumValues() const; -//# endif +private: +#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + class CZString { + public: + enum DuplicationPolicy { + noDuplication = 0, + duplicate, + duplicateOnCopy + }; + CZString(ArrayIndex index); + CZString(char const* str, unsigned length, DuplicationPolicy allocate); + CZString(CZString const& other); + ~CZString(); + CZString& operator=(CZString other); + bool operator<(CZString const& other) const; + bool operator==(CZString const& other) const; + ArrayIndex index() const; + //const char* c_str() const; ///< \deprecated + char const* data() const; + unsigned length() const; + bool isStaticString() const; + + private: + void swap(CZString& other); + + struct StringStorage { + unsigned policy_: 2; + unsigned length_: 30; // 1GB max + }; + + char const* cstr_; // actually, a prefixed string, unless policy is noDup + union { + ArrayIndex index_; + StringStorage storage_; + }; + }; - /// Comments must be //... or /* ... */ - void setComment( const char *comment, - CommentPlacement placement ); - /// Comments must be //... or /* ... */ - void setComment( const std::string &comment, - CommentPlacement placement ); - bool hasComment( CommentPlacement placement ) const; - /// Include delimiters and embedded newlines. - std::string getComment( CommentPlacement placement ) const; - - std::string toStyledString() const; - - const_iterator begin() const; - const_iterator end() const; - - iterator begin(); - iterator end(); - - private: - Value &resolveReference( const char *key, - bool isStatic ); - -# ifdef JSON_VALUE_USE_INTERNAL_MAP - inline bool isItemAvailable() const - { - return itemIsUsed_ == 0; - } - - inline void setItemUsed( bool isUsed = true ) - { - itemIsUsed_ = isUsed ? 1 : 0; - } - - inline bool isMemberNameStatic() const - { - return memberNameIsStatic_ == 0; - } - - inline void setMemberNameIsStatic( bool isStatic ) - { - memberNameIsStatic_ = isStatic ? 1 : 0; - } -# endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP - - private: - struct CommentInfo - { - CommentInfo(); - ~CommentInfo(); - - void setComment( const char *text ); - - char *comment_; - }; - - //struct MemberNamesTransform - //{ - // typedef const char *result_type; - // const char *operator()( const CZString &name ) const - // { - // return name.c_str(); - // } - //}; - - union ValueHolder - { - LargestInt int_; - LargestUInt uint_; - double real_; - bool bool_; - char *string_; -# ifdef JSON_VALUE_USE_INTERNAL_MAP - ValueInternalArray *array_; - ValueInternalMap *map_; +public: +#ifndef JSON_USE_CPPTL_SMALLMAP + typedef std::map ObjectValues; #else - ObjectValues *map_; -# endif - } value_; - ValueType type_ : 8; - int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. -# ifdef JSON_VALUE_USE_INTERNAL_MAP - unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container. - int memberNameIsStatic_ : 1; // used by the ValueInternalMap container. -# endif - CommentInfo *comments_; - }; - - - /** \brief Experimental and untested: represents an element of the "path" to access a node. - */ - class PathArgument - { - public: - friend class Path; - - PathArgument(); - PathArgument( ArrayIndex index ); - PathArgument( const char *key ); - PathArgument( const std::string &key ); - - private: - enum Kind - { - kindNone = 0, - kindIndex, - kindKey - }; - std::string key_; - ArrayIndex index_; - Kind kind_; - }; - - /** \brief Experimental and untested: represents a "path" to access a node. - * - * Syntax: - * - "." => root node - * - ".[n]" => elements at index 'n' of root node (an array value) - * - ".name" => member named 'name' of root node (an object value) - * - ".name1.name2.name3" - * - ".[0][1][2].name1[3]" - * - ".%" => member name is provided as parameter - * - ".[%]" => index is provied as parameter - */ - class Path - { - public: - Path( const std::string &path, - const PathArgument &a1 = PathArgument(), - const PathArgument &a2 = PathArgument(), - const PathArgument &a3 = PathArgument(), - const PathArgument &a4 = PathArgument(), - const PathArgument &a5 = PathArgument() ); - - const Value &resolve( const Value &root ) const; - Value resolve( const Value &root, - const Value &defaultValue ) const; - /// Creates the "path" to access the specified node and returns a reference on the node. - Value &make( Value &root ) const; - - private: - typedef std::vector InArgs; - typedef std::vector Args; - - void makePath( const std::string &path, - const InArgs &in ); - void addPathInArg( const std::string &path, - const InArgs &in, - InArgs::const_iterator &itInArg, - PathArgument::Kind kind ); - void invalidPath( const std::string &path, - int location ); - - Args args_; - }; - - - -#ifdef JSON_VALUE_USE_INTERNAL_MAP - /** \brief Allocator to customize Value internal map. - * Below is an example of a simple implementation (default implementation actually - * use memory pool for speed). - * \code - class DefaultValueMapAllocator : public ValueMapAllocator - { - public: // overridden from ValueMapAllocator - virtual ValueInternalMap *newMap() - { - return new ValueInternalMap(); - } - - virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) - { - return new ValueInternalMap( other ); - } - - virtual void destructMap( ValueInternalMap *map ) - { - delete map; - } - - virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) - { - return new ValueInternalLink[size]; - } - - virtual void releaseMapBuckets( ValueInternalLink *links ) - { - delete [] links; - } - - virtual ValueInternalLink *allocateMapLink() - { - return new ValueInternalLink(); - } - - virtual void releaseMapLink( ValueInternalLink *link ) - { - delete link; - } - }; - * \endcode - */ - class JSON_API ValueMapAllocator - { - public: - virtual ~ValueMapAllocator(); - virtual ValueInternalMap *newMap() = 0; - virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0; - virtual void destructMap( ValueInternalMap *map ) = 0; - virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0; - virtual void releaseMapBuckets( ValueInternalLink *links ) = 0; - virtual ValueInternalLink *allocateMapLink() = 0; - virtual void releaseMapLink( ValueInternalLink *link ) = 0; - }; - - /** \brief ValueInternalMap hash-map bucket chain link (for internal use only). - * \internal previous_ & next_ allows for bidirectional traversal. - */ - class JSON_API ValueInternalLink - { - public: - enum { itemPerLink = 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture. - enum InternalFlags { - flagAvailable = 0, - flagUsed = 1 - }; - - ValueInternalLink(); - - ~ValueInternalLink(); - - Value items_[itemPerLink]; - char *keys_[itemPerLink]; - ValueInternalLink *previous_; - ValueInternalLink *next_; - }; - - - /** \brief A linked page based hash-table implementation used internally by Value. - * \internal ValueInternalMap is a tradional bucket based hash-table, with a linked - * list in each bucket to handle collision. There is an addional twist in that - * each node of the collision linked list is a page containing a fixed amount of - * value. This provides a better compromise between memory usage and speed. - * - * Each bucket is made up of a chained list of ValueInternalLink. The last - * link of a given bucket can be found in the 'previous_' field of the following bucket. - * The last link of the last bucket is stored in tailLink_ as it has no following bucket. - * Only the last link of a bucket may contains 'available' item. The last link always - * contains at least one element unless is it the bucket one very first link. - */ - class JSON_API ValueInternalMap - { - friend class ValueIteratorBase; - friend class Value; - public: - typedef unsigned int HashKey; - typedef unsigned int BucketIndex; - -# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - struct IteratorState - { - IteratorState() - : map_(0) - , link_(0) - , itemIndex_(0) - , bucketIndex_(0) - { - } - ValueInternalMap *map_; - ValueInternalLink *link_; - BucketIndex itemIndex_; - BucketIndex bucketIndex_; - }; -# endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - - ValueInternalMap(); - ValueInternalMap( const ValueInternalMap &other ); - ValueInternalMap &operator =( const ValueInternalMap &other ); - ~ValueInternalMap(); - - void swap( ValueInternalMap &other ); - - BucketIndex size() const; - - void clear(); - - bool reserveDelta( BucketIndex growth ); - - bool reserve( BucketIndex newItemCount ); - - const Value *find( const char *key ) const; - - Value *find( const char *key ); - - Value &resolveReference( const char *key, - bool isStatic ); - - void remove( const char *key ); - - void doActualRemove( ValueInternalLink *link, - BucketIndex index, - BucketIndex bucketIndex ); - - ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex ); - - Value &setNewItem( const char *key, - bool isStatic, - ValueInternalLink *link, - BucketIndex index ); - - Value &unsafeAdd( const char *key, - bool isStatic, - HashKey hashedKey ); - - HashKey hash( const char *key ) const; - - int compare( const ValueInternalMap &other ) const; - - private: - void makeBeginIterator( IteratorState &it ) const; - void makeEndIterator( IteratorState &it ) const; - static bool equals( const IteratorState &x, const IteratorState &other ); - static void increment( IteratorState &iterator ); - static void incrementBucket( IteratorState &iterator ); - static void decrement( IteratorState &iterator ); - static const char *key( const IteratorState &iterator ); - static const char *key( const IteratorState &iterator, bool &isStatic ); - static Value &value( const IteratorState &iterator ); - static int distance( const IteratorState &x, const IteratorState &y ); - - private: - ValueInternalLink *buckets_; - ValueInternalLink *tailLink_; - BucketIndex bucketsSize_; - BucketIndex itemCount_; - }; - - /** \brief A simplified deque implementation used internally by Value. - * \internal - * It is based on a list of fixed "page", each page contains a fixed number of items. - * Instead of using a linked-list, a array of pointer is used for fast item look-up. - * Look-up for an element is as follow: - * - compute page index: pageIndex = itemIndex / itemsPerPage - * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage] + typedef CppTL::SmallMap ObjectValues; +#endif // ifndef JSON_USE_CPPTL_SMALLMAP +#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + +public: + /** \brief Create a default Value of the given type. + + This is a very useful constructor. + To create an empty array, pass arrayValue. + To create an empty object, pass objectValue. + Another Value can then be set to this one by assignment. +This is useful since clear() and resize() will not alter types. + + Examples: +\code +Json::Value null_value; // null +Json::Value arr_value(Json::arrayValue); // [] +Json::Value obj_value(Json::objectValue); // {} +\endcode + */ + Value(ValueType type = nullValue); + Value(Int value); + Value(UInt value); +#if defined(JSON_HAS_INT64) + Value(Int64 value); + Value(UInt64 value); +#endif // if defined(JSON_HAS_INT64) + Value(double value); + Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.) + Value(const char* begin, const char* end); ///< Copy all, incl zeroes. + /** \brief Constructs a value from a static string. + + * Like other value string constructor but do not duplicate the string for + * internal storage. The given string must remain alive after the call to this + * constructor. + * \note This works only for null-terminated strings. (We cannot change the + * size of this class, so we have nowhere to store the length, + * which might be computed later for various operations.) * - * Insertion is amortized constant time (only the array containing the index of pointers - * need to be reallocated when items are appended). + * Example of usage: + * \code + * static StaticString foo("some text"); + * Json::Value aValue(foo); + * \endcode */ - class JSON_API ValueInternalArray - { - friend class Value; - friend class ValueIteratorBase; - public: - enum { itemsPerPage = 8 }; // should be a power of 2 for fast divide and modulo. - typedef Value::ArrayIndex ArrayIndex; - typedef unsigned int PageIndex; - -# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - struct IteratorState // Must be a POD - { - IteratorState() - : array_(0) - , currentPageIndex_(0) - , currentItemIndex_(0) - { - } - ValueInternalArray *array_; - Value **currentPageIndex_; - unsigned int currentItemIndex_; - }; -# endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - - ValueInternalArray(); - ValueInternalArray( const ValueInternalArray &other ); - ValueInternalArray &operator =( const ValueInternalArray &other ); - ~ValueInternalArray(); - void swap( ValueInternalArray &other ); - - void clear(); - void resize( ArrayIndex newSize ); - - Value &resolveReference( ArrayIndex index ); - - Value *find( ArrayIndex index ) const; - - ArrayIndex size() const; - - int compare( const ValueInternalArray &other ) const; - - private: - static bool equals( const IteratorState &x, const IteratorState &other ); - static void increment( IteratorState &iterator ); - static void decrement( IteratorState &iterator ); - static Value &dereference( const IteratorState &iterator ); - static Value &unsafeDereference( const IteratorState &iterator ); - static int distance( const IteratorState &x, const IteratorState &y ); - static ArrayIndex indexOf( const IteratorState &iterator ); - void makeBeginIterator( IteratorState &it ) const; - void makeEndIterator( IteratorState &it ) const; - void makeIterator( IteratorState &it, ArrayIndex index ) const; - - void makeIndexValid( ArrayIndex index ); - - Value **pages_; - ArrayIndex size_; - PageIndex pageCount_; - }; - - /** \brief Experimental: do not use. Allocator to customize Value internal array. - * Below is an example of a simple implementation (actual implementation use - * memory pool). - \code -class DefaultValueArrayAllocator : public ValueArrayAllocator -{ -public: // overridden from ValueArrayAllocator - virtual ~DefaultValueArrayAllocator() - { - } - - virtual ValueInternalArray *newArray() - { - return new ValueInternalArray(); - } - - virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) - { - return new ValueInternalArray( other ); - } - - virtual void destruct( ValueInternalArray *array ) - { - delete array; - } - - virtual void reallocateArrayPageIndex( Value **&indexes, - ValueInternalArray::PageIndex &indexCount, - ValueInternalArray::PageIndex minNewIndexCount ) - { - ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1; - if ( minNewIndexCount > newIndexCount ) - newIndexCount = minNewIndexCount; - void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount ); - if ( !newIndexes ) - throw std::bad_alloc(); - indexCount = newIndexCount; - indexes = static_cast( newIndexes ); - } - virtual void releaseArrayPageIndex( Value **indexes, - ValueInternalArray::PageIndex indexCount ) - { - if ( indexes ) - free( indexes ); - } - - virtual Value *allocateArrayPage() - { - return static_cast( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) ); - } - - virtual void releaseArrayPage( Value *value ) - { - if ( value ) - free( value ); - } -}; - \endcode - */ - class JSON_API ValueArrayAllocator - { - public: - virtual ~ValueArrayAllocator(); - virtual ValueInternalArray *newArray() = 0; - virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0; - virtual void destructArray( ValueInternalArray *array ) = 0; - /** \brief Reallocate array page index. - * Reallocates an array of pointer on each page. - * \param indexes [input] pointer on the current index. May be \c NULL. - * [output] pointer on the new index of at least - * \a minNewIndexCount pages. - * \param indexCount [input] current number of pages in the index. - * [output] number of page the reallocated index can handle. - * \b MUST be >= \a minNewIndexCount. - * \param minNewIndexCount Minimum number of page the new index must be able to - * handle. - */ - virtual void reallocateArrayPageIndex( Value **&indexes, - ValueInternalArray::PageIndex &indexCount, - ValueInternalArray::PageIndex minNewIndexCount ) = 0; - virtual void releaseArrayPageIndex( Value **indexes, - ValueInternalArray::PageIndex indexCount ) = 0; - virtual Value *allocateArrayPage() = 0; - virtual void releaseArrayPage( Value *value ) = 0; - }; -#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP - - - /** \brief base class for Value iterators. - * - */ - class ValueIteratorBase - { - public: - typedef unsigned int size_t; - typedef int difference_type; - typedef ValueIteratorBase SelfType; - - ValueIteratorBase(); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - explicit ValueIteratorBase( const Value::ObjectValues::iterator ¤t ); -#else - ValueIteratorBase( const ValueInternalArray::IteratorState &state ); - ValueIteratorBase( const ValueInternalMap::IteratorState &state ); + Value(const StaticString& value); + Value(const std::string& value); ///< Copy data() til size(). Embedded zeroes too. +#ifdef JSON_USE_CPPTL + Value(const CppTL::ConstString& value); #endif + Value(bool value); + /// Deep copy. + Value(const Value& other); + ~Value(); + + /// Deep copy, then swap(other). + /// \note Over-write existing comments. To preserve comments, use #swapPayload(). + Value &operator=(const Value &other); + /// Swap everything. + void swap(Value& other); + /// Swap values but leave comments and source offsets in place. + void swapPayload(Value& other); + + ValueType type() const; + + /// Compare payload only, not comments etc. + bool operator<(const Value& other) const; + bool operator<=(const Value& other) const; + bool operator>=(const Value& other) const; + bool operator>(const Value& other) const; + bool operator==(const Value& other) const; + bool operator!=(const Value& other) const; + int compare(const Value& other) const; + + const char* asCString() const; ///< Embedded zeroes could cause you trouble! + std::string asString() const; ///< Embedded zeroes are possible. + /** Get raw char* of string-value. + * \return false if !string. (Seg-fault if str or end are NULL.) + */ + bool getString( + char const** begin, char const** end) const; +#ifdef JSON_USE_CPPTL + CppTL::ConstString asConstString() const; +#endif + Int asInt() const; + UInt asUInt() const; +#if defined(JSON_HAS_INT64) + Int64 asInt64() const; + UInt64 asUInt64() const; +#endif // if defined(JSON_HAS_INT64) + LargestInt asLargestInt() const; + LargestUInt asLargestUInt() const; + float asFloat() const; + double asDouble() const; + bool asBool() const; + + bool isNull() const; + bool isBool() const; + bool isInt() const; + bool isInt64() const; + bool isUInt() const; + bool isUInt64() const; + bool isIntegral() const; + bool isDouble() const; + bool isNumeric() const; + bool isString() const; + bool isArray() const; + bool isObject() const; + + bool isConvertibleTo(ValueType other) const; + + /// Number of values in array or object + ArrayIndex size() const; + + /// \brief Return true if empty array, empty object, or null; + /// otherwise, false. + bool empty() const; + + /// Return isNull() + bool operator!() const; + + /// Remove all object members and array elements. + /// \pre type() is arrayValue, objectValue, or nullValue + /// \post type() is unchanged + void clear(); + + /// Resize the array to size elements. + /// New elements are initialized to null. + /// May only be called on nullValue or arrayValue. + /// \pre type() is arrayValue or nullValue + /// \post type() is arrayValue + void resize(ArrayIndex size); + + /// Access an array element (zero based index ). + /// If the array contains less than index element, then null value are + /// inserted + /// in the array so that its size is index+1. + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + Value& operator[](ArrayIndex index); + + /// Access an array element (zero based index ). + /// If the array contains less than index element, then null value are + /// inserted + /// in the array so that its size is index+1. + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + Value& operator[](int index); + + /// Access an array element (zero based index ) + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + const Value& operator[](ArrayIndex index) const; + + /// Access an array element (zero based index ) + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + const Value& operator[](int index) const; + + /// If the array contains at least index+1 elements, returns the element + /// value, + /// otherwise returns defaultValue. + Value get(ArrayIndex index, const Value& defaultValue) const; + /// Return true if index < size(). + bool isValidIndex(ArrayIndex index) const; + /// \brief Append value to array at the end. + /// + /// Equivalent to jsonvalue[jsonvalue.size()] = value; + Value& append(const Value& value); + + /// Access an object value by name, create a null member if it does not exist. + /// \note Because of our implementation, keys are limited to 2^30 -1 chars. + /// Exceeding that will cause an exception. + Value& operator[](const char* key); + /// Access an object value by name, returns null if there is no member with + /// that name. + const Value& operator[](const char* key) const; + /// Access an object value by name, create a null member if it does not exist. + /// \param key may contain embedded nulls. + Value& operator[](const std::string& key); + /// Access an object value by name, returns null if there is no member with + /// that name. + /// \param key may contain embedded nulls. + const Value& operator[](const std::string& key) const; + /** \brief Access an object value by name, create a null member if it does not + exist. + + * If the object has no entry for that name, then the member name used to store + * the new entry is not duplicated. + * Example of use: + * \code + * Json::Value object; + * static const StaticString code("code"); + * object[code] = 1234; + * \endcode + */ + Value& operator[](const StaticString& key); +#ifdef JSON_USE_CPPTL + /// Access an object value by name, create a null member if it does not exist. + Value& operator[](const CppTL::ConstString& key); + /// Access an object value by name, returns null if there is no member with + /// that name. + const Value& operator[](const CppTL::ConstString& key) const; +#endif + /// Return the member named key if it exist, defaultValue otherwise. + /// \note deep copy + Value get(const char* key, const Value& defaultValue) const; + /// Return the member named key if it exist, defaultValue otherwise. + /// \note deep copy + /// \note key may contain embedded nulls. + Value get(const char* begin, const char* end, const Value& defaultValue) const; + /// Return the member named key if it exist, defaultValue otherwise. + /// \note deep copy + /// \param key may contain embedded nulls. + Value get(const std::string& key, const Value& defaultValue) const; +#ifdef JSON_USE_CPPTL + /// Return the member named key if it exist, defaultValue otherwise. + /// \note deep copy + Value get(const CppTL::ConstString& key, const Value& defaultValue) const; +#endif + /// Most general and efficient version of isMember()const, get()const, + /// and operator[]const + /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30 + Value const* find(char const* begin, char const* end) const; + /// Most general and efficient version of object-mutators. + /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30 + /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue. + Value const* demand(char const* begin, char const* end); + /// \brief Remove and return the named member. + /// + /// Do nothing if it did not exist. + /// \return the removed Value, or null. + /// \pre type() is objectValue or nullValue + /// \post type() is unchanged + /// \deprecated + Value removeMember(const char* key); + /// Same as removeMember(const char*) + /// \param key may contain embedded nulls. + /// \deprecated + Value removeMember(const std::string& key); + /// Same as removeMember(const char* begin, const char* end, Value* removed), + /// but 'key' is null-terminated. + bool removeMember(const char* key, Value* removed); + /** \brief Remove the named map member. + + Update 'removed' iff removed. + \param key may contain embedded nulls. + \return true iff removed (no exceptions) + */ + bool removeMember(std::string const& key, Value* removed); + /// Same as removeMember(std::string const& key, Value* removed) + bool removeMember(const char* begin, const char* end, Value* removed); + /** \brief Remove the indexed array element. + + O(n) expensive operations. + Update 'removed' iff removed. + \return true iff removed (no exceptions) + */ + bool removeIndex(ArrayIndex i, Value* removed); + + /// Return true if the object has a member named key. + /// \note 'key' must be null-terminated. + bool isMember(const char* key) const; + /// Return true if the object has a member named key. + /// \param key may contain embedded nulls. + bool isMember(const std::string& key) const; + /// Same as isMember(std::string const& key)const + bool isMember(const char* begin, const char* end) const; +#ifdef JSON_USE_CPPTL + /// Return true if the object has a member named key. + bool isMember(const CppTL::ConstString& key) const; +#endif + + /// \brief Return a list of the member names. + /// + /// If null, return an empty list. + /// \pre type() is objectValue or nullValue + /// \post if type() was nullValue, it remains nullValue + Members getMemberNames() const; + + //# ifdef JSON_USE_CPPTL + // EnumMemberNames enumMemberNames() const; + // EnumValues enumValues() const; + //# endif + + /// \deprecated Always pass len. + JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.") + void setComment(const char* comment, CommentPlacement placement); + /// Comments must be //... or /* ... */ + void setComment(const char* comment, size_t len, CommentPlacement placement); + /// Comments must be //... or /* ... */ + void setComment(const std::string& comment, CommentPlacement placement); + bool hasComment(CommentPlacement placement) const; + /// Include delimiters and embedded newlines. + std::string getComment(CommentPlacement placement) const; + + std::string toStyledString() const; + + const_iterator begin() const; + const_iterator end() const; + + iterator begin(); + iterator end(); + +private: + void initBasic(ValueType type, bool allocated = false); + + Value& resolveReference(const char* key); + Value& resolveReference(const char* key, const char* end); + + struct CommentInfo { + CommentInfo(); + ~CommentInfo(); + + void setComment(const char* text, size_t len); + + char* comment_; + }; + + // struct MemberNamesTransform + //{ + // typedef const char *result_type; + // const char *operator()( const CZString &name ) const + // { + // return name.c_str(); + // } + //}; + + union ValueHolder { + LargestInt int_; + LargestUInt uint_; + double real_; + bool bool_; + char* string_; // actually ptr to unsigned, followed by str, unless !allocated_ + ObjectValues* map_; + } value_; + ValueType type_ : 8; + unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. + // If not allocated_, string_ must be null-terminated. + CommentInfo* comments_; +}; + +/** \brief Experimental and untested: represents an element of the "path" to + * access a node. + */ +class JSON_API PathArgument { +public: + friend class Path; + + PathArgument(); + PathArgument(ArrayIndex index); + PathArgument(const char* key); + PathArgument(const std::string& key); + +private: + enum Kind { + kindNone = 0, + kindIndex, + kindKey + }; + std::string key_; + ArrayIndex index_; + Kind kind_; +}; - bool operator ==( const SelfType &other ) const - { - return isEqual( other ); - } +/** \brief Experimental and untested: represents a "path" to access a node. + * + * Syntax: + * - "." => root node + * - ".[n]" => elements at index 'n' of root node (an array value) + * - ".name" => member named 'name' of root node (an object value) + * - ".name1.name2.name3" + * - ".[0][1][2].name1[3]" + * - ".%" => member name is provided as parameter + * - ".[%]" => index is provied as parameter + */ +class JSON_API Path { +public: + Path(const std::string& path, + const PathArgument& a1 = PathArgument(), + const PathArgument& a2 = PathArgument(), + const PathArgument& a3 = PathArgument(), + const PathArgument& a4 = PathArgument(), + const PathArgument& a5 = PathArgument()); + + const Value& resolve(const Value& root) const; + Value resolve(const Value& root, const Value& defaultValue) const; + /// Creates the "path" to access the specified node and returns a reference on + /// the node. + Value& make(Value& root) const; + +private: + typedef std::vector InArgs; + typedef std::vector Args; + + void makePath(const std::string& path, const InArgs& in); + void addPathInArg(const std::string& path, + const InArgs& in, + InArgs::const_iterator& itInArg, + PathArgument::Kind kind); + void invalidPath(const std::string& path, int location); + + Args args_; +}; - bool operator !=( const SelfType &other ) const - { - return !isEqual( other ); - } +/** \brief base class for Value iterators. + * + */ +class JSON_API ValueIteratorBase { +public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef unsigned int size_t; + typedef int difference_type; + typedef ValueIteratorBase SelfType; - difference_type operator -( const SelfType &other ) const - { - return computeDistance( other ); - } + bool operator==(const SelfType& other) const { return isEqual(other); } - /// Return either the index or the member name of the referenced value as a Value. - Value key() const; + bool operator!=(const SelfType& other) const { return !isEqual(other); } - /// Return the index of the referenced Value. -1 if it is not an arrayValue. - UInt index() const; + difference_type operator-(const SelfType& other) const { + return other.computeDistance(*this); + } - /// Return the member name of the referenced Value. "" if it is not an objectValue. - const char *memberName() const; + /// Return either the index or the member name of the referenced value as a + /// Value. + Value key() const; - protected: - Value &deref() const; + /// Return the index of the referenced Value, or -1 if it is not an arrayValue. + UInt index() const; - void increment(); + /// Return the member name of the referenced Value, or "" if it is not an + /// objectValue. + /// \note Avoid `c_str()` on result, as embedded zeroes are possible. + std::string name() const; - void decrement(); + /// Return the member name of the referenced Value. "" if it is not an + /// objectValue. + /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls. + JSONCPP_DEPRECATED("Use `key = name();` instead.") + char const* memberName() const; + /// Return the member name of the referenced Value, or NULL if it is not an + /// objectValue. + /// \note Better version than memberName(). Allows embedded nulls. + char const* memberName(char const** end) const; - difference_type computeDistance( const SelfType &other ) const; +protected: + Value& deref() const; - bool isEqual( const SelfType &other ) const; + void increment(); - void copy( const SelfType &other ); + void decrement(); - private: -#ifndef JSON_VALUE_USE_INTERNAL_MAP - Value::ObjectValues::iterator current_; - // Indicates that iterator is for a null value. - bool isNull_; -#else - union - { - ValueInternalArray::IteratorState array_; - ValueInternalMap::IteratorState map_; - } iterator_; - bool isArray_; -#endif - }; + difference_type computeDistance(const SelfType& other) const; - /** \brief const iterator for object and array value. - * - */ - class ValueConstIterator : public ValueIteratorBase - { - friend class Value; - public: - typedef unsigned int size_t; - typedef int difference_type; - typedef const Value &reference; - typedef const Value *pointer; - typedef ValueConstIterator SelfType; - - ValueConstIterator(); - private: - /*! \internal Use by Value to create an iterator. - */ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - explicit ValueConstIterator( const Value::ObjectValues::iterator ¤t ); -#else - ValueConstIterator( const ValueInternalArray::IteratorState &state ); - ValueConstIterator( const ValueInternalMap::IteratorState &state ); -#endif - public: - SelfType &operator =( const ValueIteratorBase &other ); - - SelfType operator++( int ) - { - SelfType temp( *this ); - ++*this; - return temp; - } - - SelfType operator--( int ) - { - SelfType temp( *this ); - --*this; - return temp; - } - - SelfType &operator--() - { - decrement(); - return *this; - } - - SelfType &operator++() - { - increment(); - return *this; - } - - reference operator *() const - { - return deref(); - } - }; - - - /** \brief Iterator for object and array value. - */ - class ValueIterator : public ValueIteratorBase - { - friend class Value; - public: - typedef unsigned int size_t; - typedef int difference_type; - typedef Value &reference; - typedef Value *pointer; - typedef ValueIterator SelfType; - - ValueIterator(); - ValueIterator( const ValueConstIterator &other ); - ValueIterator( const ValueIterator &other ); - private: - /*! \internal Use by Value to create an iterator. - */ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - explicit ValueIterator( const Value::ObjectValues::iterator ¤t ); -#else - ValueIterator( const ValueInternalArray::IteratorState &state ); - ValueIterator( const ValueInternalMap::IteratorState &state ); -#endif - public: - - SelfType &operator =( const SelfType &other ); - - SelfType operator++( int ) - { - SelfType temp( *this ); - ++*this; - return temp; - } - - SelfType operator--( int ) - { - SelfType temp( *this ); - --*this; - return temp; - } - - SelfType &operator--() - { - decrement(); - return *this; - } - - SelfType &operator++() - { - increment(); - return *this; - } - - reference operator *() const - { - return deref(); - } - }; + bool isEqual(const SelfType& other) const; + + void copy(const SelfType& other); +private: + Value::ObjectValues::iterator current_; + // Indicates that iterator is for a null value. + bool isNull_; + +public: + // For some reason, BORLAND needs these at the end, rather + // than earlier. No idea why. + ValueIteratorBase(); + explicit ValueIteratorBase(const Value::ObjectValues::iterator& current); +}; + +/** \brief const iterator for object and array value. + * + */ +class JSON_API ValueConstIterator : public ValueIteratorBase { + friend class Value; + +public: + typedef const Value value_type; + //typedef unsigned int size_t; + //typedef int difference_type; + typedef const Value& reference; + typedef const Value* pointer; + typedef ValueConstIterator SelfType; + + ValueConstIterator(); + +private: +/*! \internal Use by Value to create an iterator. + */ + explicit ValueConstIterator(const Value::ObjectValues::iterator& current); +public: + SelfType& operator=(const ValueIteratorBase& other); + + SelfType operator++(int) { + SelfType temp(*this); + ++*this; + return temp; + } + + SelfType operator--(int) { + SelfType temp(*this); + --*this; + return temp; + } + + SelfType& operator--() { + decrement(); + return *this; + } + + SelfType& operator++() { + increment(); + return *this; + } + + reference operator*() const { return deref(); } + + pointer operator->() const { return &deref(); } +}; + +/** \brief Iterator for object and array value. + */ +class JSON_API ValueIterator : public ValueIteratorBase { + friend class Value; + +public: + typedef Value value_type; + typedef unsigned int size_t; + typedef int difference_type; + typedef Value& reference; + typedef Value* pointer; + typedef ValueIterator SelfType; + + ValueIterator(); + ValueIterator(const ValueConstIterator& other); + ValueIterator(const ValueIterator& other); + +private: +/*! \internal Use by Value to create an iterator. + */ + explicit ValueIterator(const Value::ObjectValues::iterator& current); +public: + SelfType& operator=(const SelfType& other); + + SelfType operator++(int) { + SelfType temp(*this); + ++*this; + return temp; + } + + SelfType operator--(int) { + SelfType temp(*this); + --*this; + return temp; + } + + SelfType& operator--() { + decrement(); + return *this; + } + + SelfType& operator++() { + increment(); + return *this; + } + + reference operator*() const { return deref(); } + + pointer operator->() const { return &deref(); } +}; } // namespace Json +namespace std { +/// Specialize std::swap() for Json::Value. +template<> +inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); } +} + + +#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +#pragma warning(pop) +#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) + #endif // CPPTL_JSON_H_INCLUDED // ////////////////////////////////////////////////////////////////////// @@ -1437,213 +1213,359 @@ class DefaultValueArrayAllocator : public ValueArrayAllocator // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef CPPTL_JSON_READER_H_INCLUDED -# define CPPTL_JSON_READER_H_INCLUDED +#define CPPTL_JSON_READER_H_INCLUDED #if !defined(JSON_IS_AMALGAMATION) -# include "features.h" -# include "value.h" +#include "features.h" +#include "value.h" #endif // if !defined(JSON_IS_AMALGAMATION) -# include -# include -# include -# include +#include +#include +#include +#include +#include + +// Disable warning C4251: : needs to have dll-interface to +// be used by... +#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +#pragma warning(push) +#pragma warning(disable : 4251) +#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) namespace Json { - /** \brief Unserialize a JSON document into a Value. - * +/** \brief Unserialize a JSON document into a + *Value. + * + * \deprecated Use CharReader and CharReaderBuilder. + */ +class JSON_API Reader { +public: + typedef char Char; + typedef const Char* Location; + + /** \brief Constructs a Reader allowing all features + * for parsing. + */ + Reader(); + + /** \brief Constructs a Reader allowing the specified feature set + * for parsing. + */ + Reader(const Features& features); + + /** \brief Read a Value from a JSON + * document. + * \param document UTF-8 encoded string containing the document to read. + * \param root [out] Contains the root value of the document if it was + * successfully parsed. + * \param collectComments \c true to collect comment and allow writing them + * back during + * serialization, \c false to discard comments. + * This parameter is ignored if + * Features::allowComments_ + * is \c false. + * \return \c true if the document was successfully parsed, \c false if an + * error occurred. + */ + bool + parse(const std::string& document, Value& root, bool collectComments = true); + + /** \brief Read a Value from a JSON + document. + * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the + document to read. + * \param endDoc Pointer on the end of the UTF-8 encoded string of the + document to read. + * Must be >= beginDoc. + * \param root [out] Contains the root value of the document if it was + * successfully parsed. + * \param collectComments \c true to collect comment and allow writing them + back during + * serialization, \c false to discard comments. + * This parameter is ignored if + Features::allowComments_ + * is \c false. + * \return \c true if the document was successfully parsed, \c false if an + error occurred. + */ + bool parse(const char* beginDoc, + const char* endDoc, + Value& root, + bool collectComments = true); + + /// \brief Parse from input stream. + /// \see Json::operator>>(std::istream&, Json::Value&). + bool parse(std::istream& is, Value& root, bool collectComments = true); + + /** \brief Returns a user friendly string that list errors in the parsed + * document. + * \return Formatted error message with the list of errors with their location + * in + * the parsed document. An empty string is returned if no error + * occurred + * during parsing. + * \deprecated Use getFormattedErrorMessages() instead (typo fix). + */ + JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.") + std::string getFormatedErrorMessages() const; + + /** \brief Returns a user friendly string that list errors in the parsed + * document. + * \return Formatted error message with the list of errors with their location + * in + * the parsed document. An empty string is returned if no error + * occurred + * during parsing. + */ + std::string getFormattedErrorMessages() const; + +private: + enum TokenType { + tokenEndOfStream = 0, + tokenObjectBegin, + tokenObjectEnd, + tokenArrayBegin, + tokenArrayEnd, + tokenString, + tokenNumber, + tokenTrue, + tokenFalse, + tokenNull, + tokenArraySeparator, + tokenMemberSeparator, + tokenComment, + tokenError + }; + + class Token { + public: + TokenType type_; + Location start_; + Location end_; + }; + + class ErrorInfo { + public: + Token token_; + std::string message_; + Location extra_; + }; + + typedef std::deque Errors; + + bool readToken(Token& token); + void skipSpaces(); + bool match(Location pattern, int patternLength); + bool readComment(); + bool readCStyleComment(); + bool readCppStyleComment(); + bool readString(); + void readNumber(); + bool readValue(); + bool readObject(Token& token); + bool readArray(Token& token); + bool decodeNumber(Token& token); + bool decodeNumber(Token& token, Value& decoded); + bool decodeString(Token& token); + bool decodeString(Token& token, std::string& decoded); + bool decodeDouble(Token& token); + bool decodeDouble(Token& token, Value& decoded); + bool decodeUnicodeCodePoint(Token& token, + Location& current, + Location end, + unsigned int& unicode); + bool decodeUnicodeEscapeSequence(Token& token, + Location& current, + Location end, + unsigned int& unicode); + bool addError(const std::string& message, Token& token, Location extra = 0); + bool recoverFromError(TokenType skipUntilToken); + bool addErrorAndRecover(const std::string& message, + Token& token, + TokenType skipUntilToken); + void skipUntilSpace(); + Value& currentValue(); + Char getNextChar(); + void + getLocationLineAndColumn(Location location, int& line, int& column) const; + std::string getLocationLineAndColumn(Location location) const; + void addComment(Location begin, Location end, CommentPlacement placement); + void skipCommentTokens(Token& token); + + typedef std::stack Nodes; + Nodes nodes_; + Errors errors_; + std::string document_; + Location begin_; + Location end_; + Location current_; + Location lastValueEnd_; + Value* lastValue_; + std::string commentsBefore_; + Features features_; + bool collectComments_; +}; // Reader + +/** Interface for reading JSON from a char array. + */ +class JSON_API CharReader { +public: + virtual ~CharReader() {} + /** \brief Read a Value from a JSON + document. + * The document must be a UTF-8 encoded string containing the document to read. + * + * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the + document to read. + * \param endDoc Pointer on the end of the UTF-8 encoded string of the + document to read. + * Must be >= beginDoc. + * \param root [out] Contains the root value of the document if it was + * successfully parsed. + * \param errs [out] Formatted error messages (if not NULL) + * a user friendly string that lists errors in the parsed + * document. + * \return \c true if the document was successfully parsed, \c false if an + error occurred. + */ + virtual bool parse( + char const* beginDoc, char const* endDoc, + Value* root, std::string* errs) = 0; + + class Factory { + public: + virtual ~Factory() {} + /** \brief Allocate a CharReader via operator new(). + * \throw std::exception if something goes wrong (e.g. invalid settings) + */ + virtual CharReader* newCharReader() const = 0; + }; // Factory +}; // CharReader + +/** \brief Build a CharReader implementation. + +Usage: +\code + using namespace Json; + CharReaderBuilder builder; + builder["collectComments"] = false; + Value value; + std::string errs; + bool ok = parseFromStream(builder, std::cin, &value, &errs); +\endcode +*/ +class JSON_API CharReaderBuilder : public CharReader::Factory { +public: + // Note: We use a Json::Value so that we can add data-members to this class + // without a major version bump. + /** Configuration of this builder. + These are case-sensitive. + Available settings (case-sensitive): + - `"collectComments": false or true` + - true to collect comment and allow writing them + back during serialization, false to discard comments. + This parameter is ignored if allowComments is false. + - `"allowComments": false or true` + - true if comments are allowed. + - `"strictRoot": false or true` + - true if root must be either an array or an object value + - `"allowDroppedNullPlaceholders": false or true` + - true if dropped null placeholders are allowed. (See StreamWriterBuilder.) + - `"allowNumericKeys": false or true` + - true if numeric object keys are allowed. + - `"allowSingleQuotes": false or true` + - true if '' are allowed for strings (both keys and values) + - `"stackLimit": integer` + - Exceeding stackLimit (recursive depth of `readValue()`) will + cause an exception. + - This is a security issue (seg-faults caused by deeply nested JSON), + so the default is low. + - `"failIfExtra": false or true` + - If true, `parse()` returns false when extra non-whitespace trails + the JSON value in the input string. + - `"rejectDupKeys": false or true` + - If true, `parse()` returns false when a key is duplicated within an object. + - `"allowSpecialFloats": false or true` + - If true, special float values (NaNs and infinities) are allowed + and their values are lossfree restorable. + + You can examine 'settings_` yourself + to see the defaults. You can also write and read them just like any + JSON Value. + \sa setDefaults() */ - class JSON_API Reader - { - public: - typedef char Char; - typedef const Char *Location; - - /** \brief Constructs a Reader allowing all features - * for parsing. - */ - Reader(); - - /** \brief Constructs a Reader allowing the specified feature set - * for parsing. - */ - Reader( const Features &features ); - - /** \brief Read a Value from a JSON document. - * \param document UTF-8 encoded string containing the document to read. - * \param root [out] Contains the root value of the document if it was - * successfully parsed. - * \param collectComments \c true to collect comment and allow writing them back during - * serialization, \c false to discard comments. - * This parameter is ignored if Features::allowComments_ - * is \c false. - * \return \c true if the document was successfully parsed, \c false if an error occurred. - */ - bool parse( const std::string &document, - Value &root, - bool collectComments = true ); - - /** \brief Read a Value from a JSON document. - * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the document to read. - * \param endDoc Pointer on the end of the UTF-8 encoded string of the document to read. - \ Must be >= beginDoc. - * \param root [out] Contains the root value of the document if it was - * successfully parsed. - * \param collectComments \c true to collect comment and allow writing them back during - * serialization, \c false to discard comments. - * This parameter is ignored if Features::allowComments_ - * is \c false. - * \return \c true if the document was successfully parsed, \c false if an error occurred. - */ - bool parse( const char *beginDoc, const char *endDoc, - Value &root, - bool collectComments = true ); - - /// \brief Parse from input stream. - /// \see Json::operator>>(std::istream&, Json::Value&). - bool parse( std::istream &is, - Value &root, - bool collectComments = true ); - - /** \brief Returns a user friendly string that list errors in the parsed document. - * \return Formatted error message with the list of errors with their location in - * the parsed document. An empty string is returned if no error occurred - * during parsing. - * \deprecated Use getFormattedErrorMessages() instead (typo fix). - */ - JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") - std::string getFormatedErrorMessages() const; - - /** \brief Returns a user friendly string that list errors in the parsed document. - * \return Formatted error message with the list of errors with their location in - * the parsed document. An empty string is returned if no error occurred - * during parsing. - */ - std::string getFormattedErrorMessages() const; - - private: - enum TokenType - { - tokenEndOfStream = 0, - tokenObjectBegin, - tokenObjectEnd, - tokenArrayBegin, - tokenArrayEnd, - tokenString, - tokenNumber, - tokenTrue, - tokenFalse, - tokenNull, - tokenArraySeparator, - tokenMemberSeparator, - tokenComment, - tokenError - }; - - class Token - { - public: - TokenType type_; - Location start_; - Location end_; - }; - - class ErrorInfo - { - public: - Token token_; - std::string message_; - Location extra_; - }; - - typedef std::deque Errors; - - bool expectToken( TokenType type, Token &token, const char *message ); - bool readToken( Token &token ); - void skipSpaces(); - bool match( Location pattern, - int patternLength ); - bool readComment(); - bool readCStyleComment(); - bool readCppStyleComment(); - bool readString(); - void readNumber(); - bool readValue(); - bool readObject( Token &token ); - bool readArray( Token &token ); - bool decodeNumber( Token &token ); - bool decodeString( Token &token ); - bool decodeString( Token &token, std::string &decoded ); - bool decodeDouble( Token &token ); - bool decodeUnicodeCodePoint( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ); - bool decodeUnicodeEscapeSequence( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ); - bool addError( const std::string &message, - Token &token, - Location extra = 0 ); - bool recoverFromError( TokenType skipUntilToken ); - bool addErrorAndRecover( const std::string &message, - Token &token, - TokenType skipUntilToken ); - void skipUntilSpace(); - Value ¤tValue(); - Char getNextChar(); - void getLocationLineAndColumn( Location location, - int &line, - int &column ) const; - std::string getLocationLineAndColumn( Location location ) const; - void addComment( Location begin, - Location end, - CommentPlacement placement ); - void skipCommentTokens( Token &token ); - - typedef std::stack Nodes; - Nodes nodes_; - Errors errors_; - std::string document_; - Location begin_; - Location end_; - Location current_; - Location lastValueEnd_; - Value *lastValue_; - std::string commentsBefore_; - Features features_; - bool collectComments_; - }; - - /** \brief Read from 'sin' into 'root'. - - Always keep comments from the input JSON. - - This can be used to read a file into a particular sub-object. - For example: - \code - Json::Value root; - cin >> root["dir"]["file"]; - cout << root; - \endcode - Result: - \verbatim - { - "dir": { - "file": { - // The input stream JSON would be nested here. - } - } - } - \endverbatim - \throw std::exception on parse error. - \see Json::operator<<() + Json::Value settings_; + + CharReaderBuilder(); + virtual ~CharReaderBuilder(); + + virtual CharReader* newCharReader() const; + + /** \return true if 'settings' are legal and consistent; + * otherwise, indicate bad settings via 'invalid'. + */ + bool validate(Json::Value* invalid) const; + + /** A simple way to update a specific setting. */ - std::istream& operator>>( std::istream&, Value& ); + Value& operator[](std::string key); + + /** Called by ctor, but you can use this to reset settings_. + * \pre 'settings' != NULL (but Json::null is fine) + * \remark Defaults: + * \snippet src/lib_json/json_reader.cpp CharReaderBuilderDefaults + */ + static void setDefaults(Json::Value* settings); + /** Same as old Features::strictMode(). + * \pre 'settings' != NULL (but Json::null is fine) + * \remark Defaults: + * \snippet src/lib_json/json_reader.cpp CharReaderBuilderStrictMode + */ + static void strictMode(Json::Value* settings); +}; + +/** Consume entire stream and use its begin/end. + * Someday we might have a real StreamReader, but for now this + * is convenient. + */ +bool JSON_API parseFromStream( + CharReader::Factory const&, + std::istream&, + Value* root, std::string* errs); + +/** \brief Read from 'sin' into 'root'. + + Always keep comments from the input JSON. + + This can be used to read a file into a particular sub-object. + For example: + \code + Json::Value root; + cin >> root["dir"]["file"]; + cout << root; + \endcode + Result: + \verbatim + { + "dir": { + "file": { + // The input stream JSON would be nested here. + } + } + } + \endverbatim + \throw std::exception on parse error. + \see Json::operator<<() +*/ +JSON_API std::istream& operator>>(std::istream&, Value&); } // namespace Json +#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +#pragma warning(pop) +#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) + #endif // CPPTL_JSON_READER_H_INCLUDED // ////////////////////////////////////////////////////////////////////// @@ -1665,183 +1587,318 @@ namespace Json { // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef JSON_WRITER_H_INCLUDED -# define JSON_WRITER_H_INCLUDED +#define JSON_WRITER_H_INCLUDED #if !defined(JSON_IS_AMALGAMATION) -# include "value.h" +#include "value.h" #endif // if !defined(JSON_IS_AMALGAMATION) -# include -# include -# include +#include +#include +#include + +// Disable warning C4251: : needs to have dll-interface to +// be used by... +#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +#pragma warning(push) +#pragma warning(disable : 4251) +#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) namespace Json { - class Value; +class Value; - /** \brief Abstract class for writers. - */ - class JSON_API Writer - { - public: - virtual ~Writer(); - - virtual std::string write( const Value &root ) = 0; - }; - - /** \brief Outputs a Value in JSON format without formatting (not human friendly). - * - * The JSON document is written in a single line. It is not intended for 'human' consumption, - * but may be usefull to support feature such as RPC where bandwith is limited. - * \sa Reader, Value - */ - class JSON_API FastWriter : public Writer - { - public: - FastWriter(); - virtual ~FastWriter(){} - - void enableYAMLCompatibility(); - - public: // overridden from Writer - virtual std::string write( const Value &root ); - - private: - void writeValue( const Value &value ); - - std::string document_; - bool yamlCompatiblityEnabled_; - }; - - /** \brief Writes a Value in JSON format in a human friendly way. - * - * The rules for line break and indent are as follow: - * - Object value: - * - if empty then print {} without indent and line break - * - if not empty the print '{', line break & indent, print one value per line - * and then unindent and line break and print '}'. - * - Array value: - * - if empty then print [] without indent and line break - * - if the array contains no object value, empty array or some other value types, - * and all the values fit on one lines, then print the array on a single line. - * - otherwise, it the values do not fit on one line, or the array contains - * object or non empty array, then print one value per line. - * - * If the Value have comments then they are outputed according to their #CommentPlacement. - * - * \sa Reader, Value, Value::setComment() - */ - class JSON_API StyledWriter: public Writer - { - public: - StyledWriter(); - virtual ~StyledWriter(){} - - public: // overridden from Writer - /** \brief Serialize a Value in JSON format. - * \param root Value to serialize. - * \return String containing the JSON document that represents the root value. - */ - virtual std::string write( const Value &root ); - - private: - void writeValue( const Value &value ); - void writeArrayValue( const Value &value ); - bool isMultineArray( const Value &value ); - void pushValue( const std::string &value ); - void writeIndent(); - void writeWithIndent( const std::string &value ); - void indent(); - void unindent(); - void writeCommentBeforeValue( const Value &root ); - void writeCommentAfterValueOnSameLine( const Value &root ); - bool hasCommentForValue( const Value &value ); - static std::string normalizeEOL( const std::string &text ); - - typedef std::vector ChildValues; - - ChildValues childValues_; - std::string document_; - std::string indentString_; - int rightMargin_; - int indentSize_; - bool addChildValues_; - }; - - /** \brief Writes a Value in JSON format in a human friendly way, - to a stream rather than to a string. - * - * The rules for line break and indent are as follow: - * - Object value: - * - if empty then print {} without indent and line break - * - if not empty the print '{', line break & indent, print one value per line - * and then unindent and line break and print '}'. - * - Array value: - * - if empty then print [] without indent and line break - * - if the array contains no object value, empty array or some other value types, - * and all the values fit on one lines, then print the array on a single line. - * - otherwise, it the values do not fit on one line, or the array contains - * object or non empty array, then print one value per line. - * - * If the Value have comments then they are outputed according to their #CommentPlacement. - * - * \param indentation Each level will be indented by this amount extra. - * \sa Reader, Value, Value::setComment() +/** + +Usage: +\code + using namespace Json; + void writeToStdout(StreamWriter::Factory const& factory, Value const& value) { + std::unique_ptr const writer( + factory.newStreamWriter()); + writer->write(value, &std::cout); + std::cout << std::endl; // add lf and flush + } +\endcode +*/ +class JSON_API StreamWriter { +protected: + std::ostream* sout_; // not owned; will not delete +public: + StreamWriter(); + virtual ~StreamWriter(); + /** Write Value into document as configured in sub-class. + Do not take ownership of sout, but maintain a reference during function. + \pre sout != NULL + \return zero on success (For now, we always return zero, so check the stream instead.) + \throw std::exception possibly, depending on configuration + */ + virtual int write(Value const& root, std::ostream* sout) = 0; + + /** \brief A simple abstract factory. + */ + class JSON_API Factory { + public: + virtual ~Factory(); + /** \brief Allocate a CharReader via operator new(). + * \throw std::exception if something goes wrong (e.g. invalid settings) + */ + virtual StreamWriter* newStreamWriter() const = 0; + }; // Factory +}; // StreamWriter + +/** \brief Write into stringstream, then return string, for convenience. + * A StreamWriter will be created from the factory, used, and then deleted. + */ +std::string JSON_API writeString(StreamWriter::Factory const& factory, Value const& root); + + +/** \brief Build a StreamWriter implementation. + +Usage: +\code + using namespace Json; + Value value = ...; + StreamWriterBuilder builder; + builder["commentStyle"] = "None"; + builder["indentation"] = " "; // or whatever you like + std::unique_ptr writer( + builder.newStreamWriter()); + writer->write(value, &std::cout); + std::cout << std::endl; // add lf and flush +\endcode +*/ +class JSON_API StreamWriterBuilder : public StreamWriter::Factory { +public: + // Note: We use a Json::Value so that we can add data-members to this class + // without a major version bump. + /** Configuration of this builder. + Available settings (case-sensitive): + - "commentStyle": "None" or "All" + - "indentation": "" + - "enableYAMLCompatibility": false or true + - slightly change the whitespace around colons + - "dropNullPlaceholders": false or true + - Drop the "null" string from the writer's output for nullValues. + Strictly speaking, this is not valid JSON. But when the output is being + fed to a browser's Javascript, it makes for smaller output and the + browser can handle the output just fine. + - "useSpecialFloats": false or true + - If true, outputs non-finite floating point values in the following way: + NaN values as "NaN", positive infinity as "Infinity", and negative infinity + as "-Infinity". + + You can examine 'settings_` yourself + to see the defaults. You can also write and read them just like any + JSON Value. + \sa setDefaults() */ - class JSON_API StyledStreamWriter - { - public: - StyledStreamWriter( std::string indentation="\t" ); - ~StyledStreamWriter(){} - - public: - /** \brief Serialize a Value in JSON format. - * \param out Stream to write to. (Can be ostringstream, e.g.) - * \param root Value to serialize. - * \note There is no point in deriving from Writer, since write() should not return a value. - */ - void write( std::ostream &out, const Value &root ); - - private: - void writeValue( const Value &value ); - void writeArrayValue( const Value &value ); - bool isMultineArray( const Value &value ); - void pushValue( const std::string &value ); - void writeIndent(); - void writeWithIndent( const std::string &value ); - void indent(); - void unindent(); - void writeCommentBeforeValue( const Value &root ); - void writeCommentAfterValueOnSameLine( const Value &root ); - bool hasCommentForValue( const Value &value ); - static std::string normalizeEOL( const std::string &text ); - - typedef std::vector ChildValues; - - ChildValues childValues_; - std::ostream* document_; - std::string indentString_; - int rightMargin_; - std::string indentation_; - bool addChildValues_; - }; - -# if defined(JSON_HAS_INT64) - std::string JSON_API valueToString( Int value ); - std::string JSON_API valueToString( UInt value ); -# endif // if defined(JSON_HAS_INT64) - std::string JSON_API valueToString( LargestInt value ); - std::string JSON_API valueToString( LargestUInt value ); - std::string JSON_API valueToString( double value ); - std::string JSON_API valueToString( bool value ); - std::string JSON_API valueToQuotedString( const char *value ); - - /// \brief Output using the StyledStreamWriter. - /// \see Json::operator>>() - std::ostream& operator<<( std::ostream&, const Value &root ); + Json::Value settings_; -} // namespace Json + StreamWriterBuilder(); + virtual ~StreamWriterBuilder(); + /** + * \throw std::exception if something goes wrong (e.g. invalid settings) + */ + virtual StreamWriter* newStreamWriter() const; + /** \return true if 'settings' are legal and consistent; + * otherwise, indicate bad settings via 'invalid'. + */ + bool validate(Json::Value* invalid) const; + /** A simple way to update a specific setting. + */ + Value& operator[](std::string key); + + /** Called by ctor, but you can use this to reset settings_. + * \pre 'settings' != NULL (but Json::null is fine) + * \remark Defaults: + * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults + */ + static void setDefaults(Json::Value* settings); +}; + +/** \brief Abstract class for writers. + * \deprecated Use StreamWriter. (And really, this is an implementation detail.) + */ +class JSON_API Writer { +public: + virtual ~Writer(); + + virtual std::string write(const Value& root) = 0; +}; + +/** \brief Outputs a Value in JSON format + *without formatting (not human friendly). + * + * The JSON document is written in a single line. It is not intended for 'human' + *consumption, + * but may be usefull to support feature such as RPC where bandwith is limited. + * \sa Reader, Value + * \deprecated Use StreamWriterBuilder. + */ +class JSON_API FastWriter : public Writer { + +public: + FastWriter(); + virtual ~FastWriter() {} + + void enableYAMLCompatibility(); + +public: // overridden from Writer + virtual std::string write(const Value& root); + +private: + void writeValue(const Value& value); + + std::string document_; + bool yamlCompatiblityEnabled_; +}; + +/** \brief Writes a Value in JSON format in a + *human friendly way. + * + * The rules for line break and indent are as follow: + * - Object value: + * - if empty then print {} without indent and line break + * - if not empty the print '{', line break & indent, print one value per + *line + * and then unindent and line break and print '}'. + * - Array value: + * - if empty then print [] without indent and line break + * - if the array contains no object value, empty array or some other value + *types, + * and all the values fit on one lines, then print the array on a single + *line. + * - otherwise, it the values do not fit on one line, or the array contains + * object or non empty array, then print one value per line. + * + * If the Value have comments then they are outputed according to their + *#CommentPlacement. + * + * \sa Reader, Value, Value::setComment() + * \deprecated Use StreamWriterBuilder. + */ +class JSON_API StyledWriter : public Writer { +public: + StyledWriter(); + virtual ~StyledWriter() {} + +public: // overridden from Writer + /** \brief Serialize a Value in JSON format. + * \param root Value to serialize. + * \return String containing the JSON document that represents the root value. + */ + virtual std::string write(const Value& root); + +private: + void writeValue(const Value& value); + void writeArrayValue(const Value& value); + bool isMultineArray(const Value& value); + void pushValue(const std::string& value); + void writeIndent(); + void writeWithIndent(const std::string& value); + void indent(); + void unindent(); + void writeCommentBeforeValue(const Value& root); + void writeCommentAfterValueOnSameLine(const Value& root); + bool hasCommentForValue(const Value& value); + static std::string normalizeEOL(const std::string& text); + + typedef std::vector ChildValues; + + ChildValues childValues_; + std::string document_; + std::string indentString_; + int rightMargin_; + int indentSize_; + bool addChildValues_; +}; + +/** \brief Writes a Value in JSON format in a + human friendly way, + to a stream rather than to a string. + * + * The rules for line break and indent are as follow: + * - Object value: + * - if empty then print {} without indent and line break + * - if not empty the print '{', line break & indent, print one value per + line + * and then unindent and line break and print '}'. + * - Array value: + * - if empty then print [] without indent and line break + * - if the array contains no object value, empty array or some other value + types, + * and all the values fit on one lines, then print the array on a single + line. + * - otherwise, it the values do not fit on one line, or the array contains + * object or non empty array, then print one value per line. + * + * If the Value have comments then they are outputed according to their + #CommentPlacement. + * + * \param indentation Each level will be indented by this amount extra. + * \sa Reader, Value, Value::setComment() + * \deprecated Use StreamWriterBuilder. + */ +class JSON_API StyledStreamWriter { +public: + StyledStreamWriter(std::string indentation = "\t"); + ~StyledStreamWriter() {} + +public: + /** \brief Serialize a Value in JSON format. + * \param out Stream to write to. (Can be ostringstream, e.g.) + * \param root Value to serialize. + * \note There is no point in deriving from Writer, since write() should not + * return a value. + */ + void write(std::ostream& out, const Value& root); + +private: + void writeValue(const Value& value); + void writeArrayValue(const Value& value); + bool isMultineArray(const Value& value); + void pushValue(const std::string& value); + void writeIndent(); + void writeWithIndent(const std::string& value); + void indent(); + void unindent(); + void writeCommentBeforeValue(const Value& root); + void writeCommentAfterValueOnSameLine(const Value& root); + bool hasCommentForValue(const Value& value); + static std::string normalizeEOL(const std::string& text); + + typedef std::vector ChildValues; + + ChildValues childValues_; + std::ostream* document_; + std::string indentString_; + int rightMargin_; + std::string indentation_; + bool addChildValues_ : 1; + bool indented_ : 1; +}; + +#if defined(JSON_HAS_INT64) +std::string JSON_API valueToString(Int value); +std::string JSON_API valueToString(UInt value); +#endif // if defined(JSON_HAS_INT64) +std::string JSON_API valueToString(LargestInt value); +std::string JSON_API valueToString(LargestUInt value); +std::string JSON_API valueToString(double value); +std::string JSON_API valueToString(bool value); +std::string JSON_API valueToQuotedString(const char* value); + +/// \brief Output using the StyledStreamWriter. +/// \see Json::operator>>() +JSON_API std::ostream& operator<<(std::ostream&, const Value& root); + +} // namespace Json + +#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +#pragma warning(pop) +#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) #endif // JSON_WRITER_H_INCLUDED @@ -1853,4 +1910,72 @@ namespace Json { + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/assertions.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef CPPTL_JSON_ASSERTIONS_H_INCLUDED +#define CPPTL_JSON_ASSERTIONS_H_INCLUDED + +#include +#include + +#if !defined(JSON_IS_AMALGAMATION) +#include "config.h" +#endif // if !defined(JSON_IS_AMALGAMATION) + +/** It should not be possible for a maliciously designed file to + * cause an abort() or seg-fault, so these macros are used only + * for pre-condition violations and internal logic errors. + */ +#if JSON_USE_EXCEPTION + +// @todo <= add detail about condition in exception +# define JSON_ASSERT(condition) \ + {if (!(condition)) {Json::throwLogicError( "assert json failed" );}} + +# define JSON_FAIL_MESSAGE(message) \ + { \ + std::ostringstream oss; oss << message; \ + Json::throwLogicError(oss.str()); \ + abort(); \ + } + +#else // JSON_USE_EXCEPTION + +# define JSON_ASSERT(condition) assert(condition) + +// The call to assert() will show the failure message in debug builds. In +// release builds we abort, for a core-dump or debugger. +# define JSON_FAIL_MESSAGE(message) \ + { \ + std::ostringstream oss; oss << message; \ + assert(false && oss.str().c_str()); \ + abort(); \ + } + + +#endif + +#define JSON_ASSERT_MESSAGE(condition, message) \ + if (!(condition)) { \ + JSON_FAIL_MESSAGE(message); \ + } + +#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/assertions.h +// ////////////////////////////////////////////////////////////////////// + + + + + #endif //ifndef JSON_AMALGATED_H_INCLUDED diff --git a/userspace/libsinsp/third-party/jsoncpp/jsoncpp.cpp b/userspace/libsinsp/third-party/jsoncpp/jsoncpp.cpp index 74fb78aa15..9fb5503d7e 100644 --- a/userspace/libsinsp/third-party/jsoncpp/jsoncpp.cpp +++ b/userspace/libsinsp/third-party/jsoncpp/jsoncpp.cpp @@ -1,5 +1,5 @@ /// Json-cpp amalgated source (http://jsoncpp.sourceforge.net/). -/// It is intented to be used with #include +/// It is intended to be used with #include "json/json.h" // ////////////////////////////////////////////////////////////////////// // Beginning of content of file: LICENSE @@ -72,7 +72,12 @@ license you like. -#include + +#include "json/json.h" + +#ifndef JSON_IS_AMALGAMATION +#error "Compile with -I PATH_TO_JSON_DIRECTORY" +#endif // ////////////////////////////////////////////////////////////////////// @@ -85,7 +90,7 @@ license you like. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED -# define LIB_JSONCPP_JSON_TOOL_H_INCLUDED +#define LIB_JSONCPP_JSON_TOOL_H_INCLUDED /* This header provides common string manipulation support, such as UTF-8, * portable conversion from/to string... @@ -96,77 +101,71 @@ license you like. namespace Json { /// Converts a unicode code-point to UTF-8. -static inline std::string -codePointToUTF8(unsigned int cp) -{ - std::string result; - - // based on description from http://en.wikipedia.org/wiki/UTF-8 - - if (cp <= 0x7f) - { - result.resize(1); - result[0] = static_cast(cp); - } - else if (cp <= 0x7FF) - { - result.resize(2); - result[1] = static_cast(0x80 | (0x3f & cp)); - result[0] = static_cast(0xC0 | (0x1f & (cp >> 6))); - } - else if (cp <= 0xFFFF) - { - result.resize(3); - result[2] = static_cast(0x80 | (0x3f & cp)); - result[1] = 0x80 | static_cast((0x3f & (cp >> 6))); - result[0] = 0xE0 | static_cast((0xf & (cp >> 12))); - } - else if (cp <= 0x10FFFF) - { - result.resize(4); - result[3] = static_cast(0x80 | (0x3f & cp)); - result[2] = static_cast(0x80 | (0x3f & (cp >> 6))); - result[1] = static_cast(0x80 | (0x3f & (cp >> 12))); - result[0] = static_cast(0xF0 | (0x7 & (cp >> 18))); - } - - return result; -} - - -/// Returns true if ch is a control character (in range [0,32[). -static inline bool -isControlCharacter(char ch) -{ - return ch > 0 && ch <= 0x1F; -} - - -enum { - /// Constant that specify the size of the buffer that must be passed to uintToString. - uintToStringBufferSize = 3*sizeof(LargestUInt)+1 +static inline std::string codePointToUTF8(unsigned int cp) { + std::string result; + + // based on description from http://en.wikipedia.org/wiki/UTF-8 + + if (cp <= 0x7f) { + result.resize(1); + result[0] = static_cast(cp); + } else if (cp <= 0x7FF) { + result.resize(2); + result[1] = static_cast(0x80 | (0x3f & cp)); + result[0] = static_cast(0xC0 | (0x1f & (cp >> 6))); + } else if (cp <= 0xFFFF) { + result.resize(3); + result[2] = static_cast(0x80 | (0x3f & cp)); + result[1] = static_cast(0x80 | (0x3f & (cp >> 6))); + result[0] = static_cast(0xE0 | (0xf & (cp >> 12))); + } else if (cp <= 0x10FFFF) { + result.resize(4); + result[3] = static_cast(0x80 | (0x3f & cp)); + result[2] = static_cast(0x80 | (0x3f & (cp >> 6))); + result[1] = static_cast(0x80 | (0x3f & (cp >> 12))); + result[0] = static_cast(0xF0 | (0x7 & (cp >> 18))); + } + + return result; +} + +/// Returns true if ch is a control character (in range [1,31]). +static inline bool isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F; } + +enum { + /// Constant that specify the size of the buffer that must be passed to + /// uintToString. + uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1 }; // Defines a char buffer for use with uintToString(). typedef char UIntToStringBuffer[uintToStringBufferSize]; - /** Converts an unsigned integer to string. * @param value Unsigned interger to convert to string - * @param current Input/Output string buffer. + * @param current Input/Output string buffer. * Must have at least uintToStringBufferSize chars free. */ -static inline void -uintToString( LargestUInt value, - char *¤t ) -{ - *--current = 0; - do - { - *--current = char(value % 10) + '0'; - value /= 10; - } - while ( value != 0 ); +static inline void uintToString(LargestUInt value, char*& current) { + *--current = 0; + do { + *--current = static_cast(value % 10U + static_cast('0')); + value /= 10; + } while (value != 0); +} + +/** Change ',' to '.' everywhere in buffer. + * + * We had a sophisticated way, but it did not work in WinCE. + * @see https://github.com/open-source-parsers/jsoncpp/pull/9 + */ +static inline void fixNumericLocale(char* begin, char* end) { + while (begin < end) { + if (*begin == ',') { + *begin = '.'; + } + ++begin; + } } } // namespace Json { @@ -186,889 +185,2023 @@ uintToString( LargestUInt value, // Beginning of content of file: src/lib_json/json_reader.cpp // ////////////////////////////////////////////////////////////////////// -// Copyright 2007-2010 Baptiste Lepilleur +// Copyright 2007-2011 Baptiste Lepilleur // Distributed under MIT license, or public domain if desired and // recognized in your jurisdiction. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #if !defined(JSON_IS_AMALGAMATION) -# include -# include -# include "json_tool.h" +#include +#include +#include +#include "json_tool.h" #endif // if !defined(JSON_IS_AMALGAMATION) #include #include #include #include -#include -#include +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) +#if !defined(WINCE) && defined(__STDC_SECURE_LIB__) && _MSC_VER >= 1500 // VC++ 9.0 and above +#define snprintf sprintf_s +#elif _MSC_VER >= 1900 // VC++ 14.0 and above +#define snprintf std::snprintf +#else +#define snprintf _snprintf +#endif +#elif defined(__ANDROID__) +#define snprintf snprintf +#elif __cplusplus >= 201103L +#define snprintf std::snprintf +#endif -#if _MSC_VER >= 1400 // VC++ 8.0 -#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. +#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0 +// Disable warning about strdup being deprecated. +#pragma warning(disable : 4996) #endif +static int const stackLimit_g = 1000; +static int stackDepth_g = 0; // see readValue() + namespace Json { +#if __cplusplus >= 201103L +typedef std::unique_ptr const CharReaderPtr; +#else +typedef std::auto_ptr CharReaderPtr; +#endif + // Implementation of class Features // //////////////////////////////// Features::Features() - : allowComments_( true ) - , strictRoot_( false ) -{ -} - - -Features -Features::all() -{ - return Features(); -} + : allowComments_(true), strictRoot_(false) +{} +Features Features::all() { return Features(); } - -Features -Features::strictMode() -{ - Features features; - features.allowComments_ = false; - features.strictRoot_ = true; - return features; +Features Features::strictMode() { + Features features; + features.allowComments_ = false; + features.strictRoot_ = true; + return features; } // Implementation of class Reader // //////////////////////////////// - -static inline bool -in( Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4 ) -{ - return c == c1 || c == c2 || c == c3 || c == c4; -} - -static inline bool -in( Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4, Reader::Char c5 ) -{ - return c == c1 || c == c2 || c == c3 || c == c4 || c == c5; -} - - -static bool -containsNewLine( Reader::Location begin, - Reader::Location end ) -{ - for ( ;begin < end; ++begin ) - if ( *begin == '\n' || *begin == '\r' ) - return true; - return false; +static bool containsNewLine(Reader::Location begin, Reader::Location end) { + for (; begin < end; ++begin) + if (*begin == '\n' || *begin == '\r') + return true; + return false; } - // Class Reader // ////////////////////////////////////////////////////////////////// Reader::Reader() - : features_( Features::all() ) -{ -} - + : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(), + lastValue_(), commentsBefore_(), features_(Features::all()), + collectComments_() {} -Reader::Reader( const Features &features ) - : features_( features ) -{ +Reader::Reader(const Features& features) + : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(), + lastValue_(), commentsBefore_(), features_(features), collectComments_() { } - bool -Reader::parse( const std::string &document, - Value &root, - bool collectComments ) -{ - document_ = document; - const char *begin = document_.c_str(); - const char *end = begin + document_.length(); - return parse( begin, end, root, collectComments ); +Reader::parse(const std::string& document, Value& root, bool collectComments) { + document_ = document; + const char* begin = document_.c_str(); + const char* end = begin + document_.length(); + return parse(begin, end, root, collectComments); +} + +bool Reader::parse(std::istream& sin, Value& root, bool collectComments) { + // std::istream_iterator begin(sin); + // std::istream_iterator end; + // Those would allow streamed input from a file, if parse() were a + // template function. + + // Since std::string is reference-counted, this at least does not + // create an extra copy. + std::string doc; + std::getline(sin, doc, (char)EOF); + return parse(doc, root, collectComments); +} + +bool Reader::parse(const char* beginDoc, + const char* endDoc, + Value& root, + bool collectComments) { + if (!features_.allowComments_) { + collectComments = false; + } + + begin_ = beginDoc; + end_ = endDoc; + collectComments_ = collectComments; + current_ = begin_; + lastValueEnd_ = 0; + lastValue_ = 0; + commentsBefore_ = ""; + errors_.clear(); + while (!nodes_.empty()) + nodes_.pop(); + nodes_.push(&root); + + stackDepth_g = 0; // Yes, this is bad coding, but options are limited. + bool successful = readValue(); + Token token; + skipCommentTokens(token); + if (collectComments_ && !commentsBefore_.empty()) + root.setComment(commentsBefore_, commentAfter); + if (features_.strictRoot_) { + if (!root.isArray() && !root.isObject()) { + // Set error location to start of doc, ideally should be first token found + // in doc + token.type_ = tokenError; + token.start_ = beginDoc; + token.end_ = endDoc; + addError( + "A valid JSON document must be either an array or an object value.", + token); + return false; + } + } + return successful; +} + +bool Reader::readValue() { + // This is a non-reentrant way to support a stackLimit. Terrible! + // But this deprecated class has a security problem: Bad input can + // cause a seg-fault. This seems like a fair, binary-compatible way + // to prevent the problem. + if (stackDepth_g >= stackLimit_g) throwRuntimeError("Exceeded stackLimit in readValue()."); + ++stackDepth_g; + + Token token; + skipCommentTokens(token); + bool successful = true; + + if (collectComments_ && !commentsBefore_.empty()) { + currentValue().setComment(commentsBefore_, commentBefore); + commentsBefore_ = ""; + } + + switch (token.type_) { + case tokenObjectBegin: + successful = readObject(token); + break; + case tokenArrayBegin: + successful = readArray(token); + break; + case tokenNumber: + successful = decodeNumber(token); + break; + case tokenString: + successful = decodeString(token); + break; + case tokenTrue: + { + Value v(true); + currentValue().swapPayload(v); + } + break; + case tokenFalse: + { + Value v(false); + currentValue().swapPayload(v); + } + break; + case tokenNull: + { + Value v; + currentValue().swapPayload(v); + } + break; + // Else, fall through... + default: + return addError("Syntax error: value, object or array expected.", token); + } + + if (collectComments_) { + lastValueEnd_ = current_; + lastValue_ = ¤tValue(); + } + + --stackDepth_g; + return successful; +} + +void Reader::skipCommentTokens(Token& token) { + if (features_.allowComments_) { + do { + readToken(token); + } while (token.type_ == tokenComment); + } else { + readToken(token); + } +} + +bool Reader::readToken(Token& token) { + skipSpaces(); + token.start_ = current_; + Char c = getNextChar(); + bool ok = true; + switch (c) { + case '{': + token.type_ = tokenObjectBegin; + break; + case '}': + token.type_ = tokenObjectEnd; + break; + case '[': + token.type_ = tokenArrayBegin; + break; + case ']': + token.type_ = tokenArrayEnd; + break; + case '"': + token.type_ = tokenString; + ok = readString(); + break; + case '/': + token.type_ = tokenComment; + ok = readComment(); + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '-': + token.type_ = tokenNumber; + readNumber(); + break; + case 't': + token.type_ = tokenTrue; + ok = match("rue", 3); + break; + case 'f': + token.type_ = tokenFalse; + ok = match("alse", 4); + break; + case 'n': + token.type_ = tokenNull; + ok = match("ull", 3); + break; + case ',': + token.type_ = tokenArraySeparator; + break; + case ':': + token.type_ = tokenMemberSeparator; + break; + case 0: + token.type_ = tokenEndOfStream; + break; + default: + ok = false; + break; + } + if (!ok) + token.type_ = tokenError; + token.end_ = current_; + return true; +} + +void Reader::skipSpaces() { + while (current_ != end_) { + Char c = *current_; + if (c == ' ' || c == '\t' || c == '\r' || c == '\n') + ++current_; + else + break; + } } - -bool -Reader::parse( std::istream& sin, - Value &root, - bool collectComments ) -{ - //std::istream_iterator begin(sin); - //std::istream_iterator end; - // Those would allow streamed input from a file, if parse() were a - // template function. - - // Since std::string is reference-counted, this at least does not - // create an extra copy. - std::string doc; - std::getline(sin, doc, (char)EOF); - return parse( doc, root, collectComments ); -} - -bool -Reader::parse( const char *beginDoc, const char *endDoc, - Value &root, - bool collectComments ) -{ - if ( !features_.allowComments_ ) - { - collectComments = false; - } - - begin_ = beginDoc; - end_ = endDoc; - collectComments_ = collectComments; - current_ = begin_; - lastValueEnd_ = 0; - lastValue_ = 0; - commentsBefore_ = ""; - errors_.clear(); - while ( !nodes_.empty() ) - nodes_.pop(); - nodes_.push( &root ); - - bool successful = readValue(); - Token token; - skipCommentTokens( token ); - if ( collectComments_ && !commentsBefore_.empty() ) - root.setComment( commentsBefore_, commentAfter ); - if ( features_.strictRoot_ ) - { - if ( !root.isArray() && !root.isObject() ) - { - // Set error location to start of doc, ideally should be first token found in doc - token.type_ = tokenError; - token.start_ = beginDoc; - token.end_ = endDoc; - addError( "A valid JSON document must be either an array or an object value.", - token ); - return false; - } - } - return successful; +bool Reader::match(Location pattern, int patternLength) { + if (end_ - current_ < patternLength) + return false; + int index = patternLength; + while (index--) + if (current_[index] != pattern[index]) + return false; + current_ += patternLength; + return true; +} + +bool Reader::readComment() { + Location commentBegin = current_ - 1; + Char c = getNextChar(); + bool successful = false; + if (c == '*') + successful = readCStyleComment(); + else if (c == '/') + successful = readCppStyleComment(); + if (!successful) + return false; + + if (collectComments_) { + CommentPlacement placement = commentBefore; + if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) { + if (c != '*' || !containsNewLine(commentBegin, current_)) + placement = commentAfterOnSameLine; + } + + addComment(commentBegin, current_, placement); + } + return true; +} + +static std::string normalizeEOL(Reader::Location begin, Reader::Location end) { + std::string normalized; + normalized.reserve(end - begin); + Reader::Location current = begin; + while (current != end) { + char c = *current++; + if (c == '\r') { + if (current != end && *current == '\n') + // convert dos EOL + ++current; + // convert Mac EOL + normalized += '\n'; + } else { + normalized += c; + } + } + return normalized; } +void +Reader::addComment(Location begin, Location end, CommentPlacement placement) { + assert(collectComments_); + const std::string& normalized = normalizeEOL(begin, end); + if (placement == commentAfterOnSameLine) { + assert(lastValue_ != 0); + lastValue_->setComment(normalized, placement); + } else { + commentsBefore_ += normalized; + } +} + +bool Reader::readCStyleComment() { + while (current_ != end_) { + Char c = getNextChar(); + if (c == '*' && *current_ == '/') + break; + } + return getNextChar() == '/'; +} -bool -Reader::readValue() -{ - Token token; - skipCommentTokens( token ); - bool successful = true; - - if ( collectComments_ && !commentsBefore_.empty() ) - { - currentValue().setComment( commentsBefore_, commentBefore ); - commentsBefore_ = ""; - } - - - switch ( token.type_ ) - { - case tokenObjectBegin: - successful = readObject( token ); +bool Reader::readCppStyleComment() { + while (current_ != end_) { + Char c = getNextChar(); + if (c == '\n') break; - case tokenArrayBegin: - successful = readArray( token ); + if (c == '\r') { + // Consume DOS EOL. It will be normalized in addComment. + if (current_ != end_ && *current_ == '\n') + getNextChar(); + // Break on Moc OS 9 EOL. break; - case tokenNumber: - successful = decodeNumber( token ); + } + } + return true; +} + +void Reader::readNumber() { + const char *p = current_; + char c = '0'; // stopgap for already consumed character + // integral part + while (c >= '0' && c <= '9') + c = (current_ = p) < end_ ? *p++ : 0; + // fractional part + if (c == '.') { + c = (current_ = p) < end_ ? *p++ : 0; + while (c >= '0' && c <= '9') + c = (current_ = p) < end_ ? *p++ : 0; + } + // exponential part + if (c == 'e' || c == 'E') { + c = (current_ = p) < end_ ? *p++ : 0; + if (c == '+' || c == '-') + c = (current_ = p) < end_ ? *p++ : 0; + while (c >= '0' && c <= '9') + c = (current_ = p) < end_ ? *p++ : 0; + } +} + +bool Reader::readString() { + Char c = 0; + while (current_ != end_) { + c = getNextChar(); + if (c == '\\') + getNextChar(); + else if (c == '"') break; - case tokenString: - successful = decodeString( token ); + } + return c == '"'; +} + +bool Reader::readObject(Token& /*tokenStart*/) { + Token tokenName; + std::string name; + Value init(objectValue); + currentValue().swapPayload(init); + while (readToken(tokenName)) { + bool initialTokenOk = true; + while (tokenName.type_ == tokenComment && initialTokenOk) + initialTokenOk = readToken(tokenName); + if (!initialTokenOk) break; - case tokenTrue: - currentValue() = true; + if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object + return true; + name = ""; + if (tokenName.type_ == tokenString) { + if (!decodeString(tokenName, name)) + return recoverFromError(tokenObjectEnd); + } else { break; - case tokenFalse: - currentValue() = false; + } + + Token colon; + if (!readToken(colon) || colon.type_ != tokenMemberSeparator) { + return addErrorAndRecover( + "Missing ':' after object member name", colon, tokenObjectEnd); + } + Value& value = currentValue()[name]; + nodes_.push(&value); + bool ok = readValue(); + nodes_.pop(); + if (!ok) // error already set + return recoverFromError(tokenObjectEnd); + + Token comma; + if (!readToken(comma) || + (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator && + comma.type_ != tokenComment)) { + return addErrorAndRecover( + "Missing ',' or '}' in object declaration", comma, tokenObjectEnd); + } + bool finalizeTokenOk = true; + while (comma.type_ == tokenComment && finalizeTokenOk) + finalizeTokenOk = readToken(comma); + if (comma.type_ == tokenObjectEnd) + return true; + } + return addErrorAndRecover( + "Missing '}' or object member name", tokenName, tokenObjectEnd); +} + +bool Reader::readArray(Token& /*tokenStart*/) { + Value init(arrayValue); + currentValue().swapPayload(init); + skipSpaces(); + if (*current_ == ']') // empty array + { + Token endArray; + readToken(endArray); + return true; + } + int index = 0; + for (;;) { + Value& value = currentValue()[index++]; + nodes_.push(&value); + bool ok = readValue(); + nodes_.pop(); + if (!ok) // error already set + return recoverFromError(tokenArrayEnd); + + Token token; + // Accept Comment after last item in the array. + ok = readToken(token); + while (token.type_ == tokenComment && ok) { + ok = readToken(token); + } + bool badTokenType = + (token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd); + if (!ok || badTokenType) { + return addErrorAndRecover( + "Missing ',' or ']' in array declaration", token, tokenArrayEnd); + } + if (token.type_ == tokenArrayEnd) break; - case tokenNull: - currentValue() = Value(); + } + return true; +} + +bool Reader::decodeNumber(Token& token) { + Value decoded; + if (!decodeNumber(token, decoded)) + return false; + currentValue().swapPayload(decoded); + return true; +} + +bool Reader::decodeNumber(Token& token, Value& decoded) { + // Attempts to parse the number as an integer. If the number is + // larger than the maximum supported value of an integer then + // we decode the number as a double. + Location current = token.start_; + bool isNegative = *current == '-'; + if (isNegative) + ++current; + // TODO: Help the compiler do the div and mod at compile time or get rid of them. + Value::LargestUInt maxIntegerValue = + isNegative ? Value::LargestUInt(Value::maxLargestInt) + 1 + : Value::maxLargestUInt; + Value::LargestUInt threshold = maxIntegerValue / 10; + Value::LargestUInt value = 0; + while (current < token.end_) { + Char c = *current++; + if (c < '0' || c > '9') + return decodeDouble(token, decoded); + Value::UInt digit(c - '0'); + if (value >= threshold) { + // We've hit or exceeded the max value divided by 10 (rounded down). If + // a) we've only just touched the limit, b) this is the last digit, and + // c) it's small enough to fit in that rounding delta, we're okay. + // Otherwise treat this number as a double to avoid overflow. + if (value > threshold || current != token.end_ || + digit > maxIntegerValue % 10) { + return decodeDouble(token, decoded); + } + } + value = value * 10 + digit; + } + if (isNegative && value == maxIntegerValue) + decoded = Value::minLargestInt; + else if (isNegative) + decoded = -Value::LargestInt(value); + else if (value <= Value::LargestUInt(Value::maxInt)) + decoded = Value::LargestInt(value); + else + decoded = value; + return true; +} + +bool Reader::decodeDouble(Token& token) { + Value decoded; + if (!decodeDouble(token, decoded)) + return false; + currentValue().swapPayload(decoded); + return true; +} + +bool Reader::decodeDouble(Token& token, Value& decoded) { + double value = 0; + std::string buffer(token.start_, token.end_); + std::istringstream is(buffer); + if (!(is >> value)) + return addError("'" + std::string(token.start_, token.end_) + + "' is not a number.", + token); + decoded = value; + return true; +} + +bool Reader::decodeString(Token& token) { + std::string decoded_string; + if (!decodeString(token, decoded_string)) + return false; + Value decoded(decoded_string); + currentValue().swapPayload(decoded); + return true; +} + +bool Reader::decodeString(Token& token, std::string& decoded) { + decoded.reserve(token.end_ - token.start_ - 2); + Location current = token.start_ + 1; // skip '"' + Location end = token.end_ - 1; // do not include '"' + while (current != end) { + Char c = *current++; + if (c == '"') break; - default: - return addError( "Syntax error: value, object or array expected.", token ); - } - - if ( collectComments_ ) - { - lastValueEnd_ = current_; - lastValue_ = ¤tValue(); - } + else if (c == '\\') { + if (current == end) + return addError("Empty escape sequence in string", token, current); + Char escape = *current++; + switch (escape) { + case '"': + decoded += '"'; + break; + case '/': + decoded += '/'; + break; + case '\\': + decoded += '\\'; + break; + case 'b': + decoded += '\b'; + break; + case 'f': + decoded += '\f'; + break; + case 'n': + decoded += '\n'; + break; + case 'r': + decoded += '\r'; + break; + case 't': + decoded += '\t'; + break; + case 'u': { + unsigned int unicode; + if (!decodeUnicodeCodePoint(token, current, end, unicode)) + return false; + decoded += codePointToUTF8(unicode); + } break; + default: + return addError("Bad escape sequence in string", token, current); + } + } else { + decoded += c; + } + } + return true; +} + +bool Reader::decodeUnicodeCodePoint(Token& token, + Location& current, + Location end, + unsigned int& unicode) { + + if (!decodeUnicodeEscapeSequence(token, current, end, unicode)) + return false; + if (unicode >= 0xD800 && unicode <= 0xDBFF) { + // surrogate pairs + if (end - current < 6) + return addError( + "additional six characters expected to parse unicode surrogate pair.", + token, + current); + unsigned int surrogatePair; + if (*(current++) == '\\' && *(current++) == 'u') { + if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) { + unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); + } else + return false; + } else + return addError("expecting another \\u token to begin the second half of " + "a unicode surrogate pair", + token, + current); + } + return true; +} + +bool Reader::decodeUnicodeEscapeSequence(Token& token, + Location& current, + Location end, + unsigned int& unicode) { + if (end - current < 4) + return addError( + "Bad unicode escape sequence in string: four digits expected.", + token, + current); + unicode = 0; + for (int index = 0; index < 4; ++index) { + Char c = *current++; + unicode *= 16; + if (c >= '0' && c <= '9') + unicode += c - '0'; + else if (c >= 'a' && c <= 'f') + unicode += c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + unicode += c - 'A' + 10; + else + return addError( + "Bad unicode escape sequence in string: hexadecimal digit expected.", + token, + current); + } + return true; +} - return successful; +bool +Reader::addError(const std::string& message, Token& token, Location extra) { + ErrorInfo info; + info.token_ = token; + info.message_ = message; + info.extra_ = extra; + errors_.push_back(info); + return false; +} + +bool Reader::recoverFromError(TokenType skipUntilToken) { + int errorCount = int(errors_.size()); + Token skip; + for (;;) { + if (!readToken(skip)) + errors_.resize(errorCount); // discard errors caused by recovery + if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream) + break; + } + errors_.resize(errorCount); + return false; +} + +bool Reader::addErrorAndRecover(const std::string& message, + Token& token, + TokenType skipUntilToken) { + addError(message, token); + return recoverFromError(skipUntilToken); +} + +Value& Reader::currentValue() { return *(nodes_.top()); } + +Reader::Char Reader::getNextChar() { + if (current_ == end_) + return 0; + return *current_++; +} + +void Reader::getLocationLineAndColumn(Location location, + int& line, + int& column) const { + Location current = begin_; + Location lastLineStart = current; + line = 0; + while (current < location && current != end_) { + Char c = *current++; + if (c == '\r') { + if (*current == '\n') + ++current; + lastLineStart = current; + ++line; + } else if (c == '\n') { + lastLineStart = current; + ++line; + } + } + // column & line start at 1 + column = int(location - lastLineStart) + 1; + ++line; +} + +std::string Reader::getLocationLineAndColumn(Location location) const { + int line, column; + getLocationLineAndColumn(location, line, column); + char buffer[18 + 16 + 16 + 1]; + snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column); + return buffer; } +// Deprecated. Preserved for backward compatibility +std::string Reader::getFormatedErrorMessages() const { + return getFormattedErrorMessages(); +} + +std::string Reader::getFormattedErrorMessages() const { + std::string formattedMessage; + for (Errors::const_iterator itError = errors_.begin(); + itError != errors_.end(); + ++itError) { + const ErrorInfo& error = *itError; + formattedMessage += + "* " + getLocationLineAndColumn(error.token_.start_) + "\n"; + formattedMessage += " " + error.message_ + "\n"; + if (error.extra_) + formattedMessage += + "See " + getLocationLineAndColumn(error.extra_) + " for detail.\n"; + } + return formattedMessage; +} + +// Reader +///////////////////////// + +// exact copy of Features +class OurFeatures { +public: + static OurFeatures all(); + OurFeatures(); + bool allowComments_; + bool strictRoot_; + bool allowDroppedNullPlaceholders_; + bool allowNumericKeys_; + bool allowSingleQuotes_; + bool failIfExtra_; + bool rejectDupKeys_; + bool allowSpecialFloats_; + int stackLimit_; +}; // OurFeatures + +// exact copy of Implementation of class Features +// //////////////////////////////// -void -Reader::skipCommentTokens( Token &token ) +OurFeatures::OurFeatures() + : allowComments_(true), strictRoot_(false) + , allowDroppedNullPlaceholders_(false), allowNumericKeys_(false) + , allowSingleQuotes_(false) + , failIfExtra_(false) + , allowSpecialFloats_(false) { - if ( features_.allowComments_ ) - { - do - { - readToken( token ); - } - while ( token.type_ == tokenComment ); - } - else - { - readToken( token ); - } } +OurFeatures OurFeatures::all() { return OurFeatures(); } -bool -Reader::expectToken( TokenType type, Token &token, const char *message ) -{ - readToken( token ); - if ( token.type_ != type ) - return addError( message, token ); - return true; -} +// Implementation of class Reader +// //////////////////////////////// +// exact copy of Reader, renamed to OurReader +class OurReader { +public: + typedef char Char; + typedef const Char* Location; + struct StructuredError { + size_t offset_start; + size_t offset_limit; + std::string message; + }; + + OurReader(OurFeatures const& features); + bool parse(const char* beginDoc, + const char* endDoc, + Value& root, + bool collectComments = true); + std::string getFormattedErrorMessages() const; -bool -Reader::readToken( Token &token ) -{ - skipSpaces(); - token.start_ = current_; - Char c = getNextChar(); - bool ok = true; - switch ( c ) - { - case '{': - token.type_ = tokenObjectBegin; - break; - case '}': - token.type_ = tokenObjectEnd; - break; - case '[': - token.type_ = tokenArrayBegin; - break; - case ']': - token.type_ = tokenArrayEnd; +private: + OurReader(OurReader const&); // no impl + void operator=(OurReader const&); // no impl + + enum TokenType { + tokenEndOfStream = 0, + tokenObjectBegin, + tokenObjectEnd, + tokenArrayBegin, + tokenArrayEnd, + tokenString, + tokenNumber, + tokenTrue, + tokenFalse, + tokenNull, + tokenNaN, + tokenPosInf, + tokenNegInf, + tokenArraySeparator, + tokenMemberSeparator, + tokenComment, + tokenError + }; + + class Token { + public: + TokenType type_; + Location start_; + Location end_; + }; + + class ErrorInfo { + public: + Token token_; + std::string message_; + Location extra_; + }; + + typedef std::deque Errors; + + bool readToken(Token& token); + void skipSpaces(); + bool match(Location pattern, int patternLength); + bool readComment(); + bool readCStyleComment(); + bool readCppStyleComment(); + bool readString(); + bool readStringSingleQuote(); + bool readNumber(bool checkInf); + bool readValue(); + bool readObject(Token& token); + bool readArray(Token& token); + bool decodeNumber(Token& token); + bool decodeNumber(Token& token, Value& decoded); + bool decodeString(Token& token); + bool decodeString(Token& token, std::string& decoded); + bool decodeDouble(Token& token); + bool decodeDouble(Token& token, Value& decoded); + bool decodeUnicodeCodePoint(Token& token, + Location& current, + Location end, + unsigned int& unicode); + bool decodeUnicodeEscapeSequence(Token& token, + Location& current, + Location end, + unsigned int& unicode); + bool addError(const std::string& message, Token& token, Location extra = 0); + bool recoverFromError(TokenType skipUntilToken); + bool addErrorAndRecover(const std::string& message, + Token& token, + TokenType skipUntilToken); + void skipUntilSpace(); + Value& currentValue(); + Char getNextChar(); + void + getLocationLineAndColumn(Location location, int& line, int& column) const; + std::string getLocationLineAndColumn(Location location) const; + void addComment(Location begin, Location end, CommentPlacement placement); + void skipCommentTokens(Token& token); + + typedef std::stack Nodes; + Nodes nodes_; + Errors errors_; + std::string document_; + Location begin_; + Location end_; + Location current_; + Location lastValueEnd_; + Value* lastValue_; + std::string commentsBefore_; + int stackDepth_; + + OurFeatures const features_; + bool collectComments_; +}; // OurReader + +// complete copy of Read impl, for OurReader + +OurReader::OurReader(OurFeatures const& features) + : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(), + lastValue_(), commentsBefore_(), features_(features), collectComments_() { +} + +bool OurReader::parse(const char* beginDoc, + const char* endDoc, + Value& root, + bool collectComments) { + if (!features_.allowComments_) { + collectComments = false; + } + + begin_ = beginDoc; + end_ = endDoc; + collectComments_ = collectComments; + current_ = begin_; + lastValueEnd_ = 0; + lastValue_ = 0; + commentsBefore_ = ""; + errors_.clear(); + while (!nodes_.empty()) + nodes_.pop(); + nodes_.push(&root); + + stackDepth_ = 0; + bool successful = readValue(); + Token token; + skipCommentTokens(token); + if (features_.failIfExtra_) { + if (token.type_ != tokenError && token.type_ != tokenEndOfStream) { + addError("Extra non-whitespace after JSON value.", token); + return false; + } + } + if (collectComments_ && !commentsBefore_.empty()) + root.setComment(commentsBefore_, commentAfter); + if (features_.strictRoot_) { + if (!root.isArray() && !root.isObject()) { + // Set error location to start of doc, ideally should be first token found + // in doc + token.type_ = tokenError; + token.start_ = beginDoc; + token.end_ = endDoc; + addError( + "A valid JSON document must be either an array or an object value.", + token); + return false; + } + } + return successful; +} + +bool OurReader::readValue() { + if (stackDepth_ >= features_.stackLimit_) throwRuntimeError("Exceeded stackLimit in readValue()."); + ++stackDepth_; + Token token; + skipCommentTokens(token); + bool successful = true; + + if (collectComments_ && !commentsBefore_.empty()) { + currentValue().setComment(commentsBefore_, commentBefore); + commentsBefore_ = ""; + } + + switch (token.type_) { + case tokenObjectBegin: + successful = readObject(token); + break; + case tokenArrayBegin: + successful = readArray(token); + break; + case tokenNumber: + successful = decodeNumber(token); + break; + case tokenString: + successful = decodeString(token); + break; + case tokenTrue: + { + Value v(true); + currentValue().swapPayload(v); + } + break; + case tokenFalse: + { + Value v(false); + currentValue().swapPayload(v); + } + break; + case tokenNull: + { + Value v; + currentValue().swapPayload(v); + } + break; + case tokenNaN: + { + Value v(std::numeric_limits::quiet_NaN()); + currentValue().swapPayload(v); + } + break; + case tokenPosInf: + { + Value v(std::numeric_limits::infinity()); + currentValue().swapPayload(v); + } + break; + case tokenNegInf: + { + Value v(-std::numeric_limits::infinity()); + currentValue().swapPayload(v); + } + break; + case tokenArraySeparator: + case tokenObjectEnd: + case tokenArrayEnd: + if (features_.allowDroppedNullPlaceholders_) { + // "Un-read" the current token and mark the current value as a null + // token. + current_--; + Value v; + currentValue().swapPayload(v); break; - case '"': - token.type_ = tokenString; - ok = readString(); + } // else, fall through ... + default: + return addError("Syntax error: value, object or array expected.", token); + } + + if (collectComments_) { + lastValueEnd_ = current_; + lastValue_ = ¤tValue(); + } + + --stackDepth_; + return successful; +} + +void OurReader::skipCommentTokens(Token& token) { + if (features_.allowComments_) { + do { + readToken(token); + } while (token.type_ == tokenComment); + } else { + readToken(token); + } +} + +bool OurReader::readToken(Token& token) { + skipSpaces(); + token.start_ = current_; + Char c = getNextChar(); + bool ok = true; + switch (c) { + case '{': + token.type_ = tokenObjectBegin; + break; + case '}': + token.type_ = tokenObjectEnd; + break; + case '[': + token.type_ = tokenArrayBegin; + break; + case ']': + token.type_ = tokenArrayEnd; + break; + case '"': + token.type_ = tokenString; + ok = readString(); + break; + case '\'': + if (features_.allowSingleQuotes_) { + token.type_ = tokenString; + ok = readStringSingleQuote(); + break; + } // else continue + case '/': + token.type_ = tokenComment; + ok = readComment(); + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + token.type_ = tokenNumber; + readNumber(false); + break; + case '-': + if (readNumber(true)) { + token.type_ = tokenNumber; + } else { + token.type_ = tokenNegInf; + ok = features_.allowSpecialFloats_ && match("nfinity", 7); + } + break; + case 't': + token.type_ = tokenTrue; + ok = match("rue", 3); + break; + case 'f': + token.type_ = tokenFalse; + ok = match("alse", 4); + break; + case 'n': + token.type_ = tokenNull; + ok = match("ull", 3); + break; + case 'N': + if (features_.allowSpecialFloats_) { + token.type_ = tokenNaN; + ok = match("aN", 2); + } else { + ok = false; + } + break; + case 'I': + if (features_.allowSpecialFloats_) { + token.type_ = tokenPosInf; + ok = match("nfinity", 7); + } else { + ok = false; + } + break; + case ',': + token.type_ = tokenArraySeparator; + break; + case ':': + token.type_ = tokenMemberSeparator; + break; + case 0: + token.type_ = tokenEndOfStream; + break; + default: + ok = false; + break; + } + if (!ok) + token.type_ = tokenError; + token.end_ = current_; + return true; +} + +void OurReader::skipSpaces() { + while (current_ != end_) { + Char c = *current_; + if (c == ' ' || c == '\t' || c == '\r' || c == '\n') + ++current_; + else break; - case '/': - token.type_ = tokenComment; - ok = readComment(); + } +} + +bool OurReader::match(Location pattern, int patternLength) { + if (end_ - current_ < patternLength) + return false; + int index = patternLength; + while (index--) + if (current_[index] != pattern[index]) + return false; + current_ += patternLength; + return true; +} + +bool OurReader::readComment() { + Location commentBegin = current_ - 1; + Char c = getNextChar(); + bool successful = false; + if (c == '*') + successful = readCStyleComment(); + else if (c == '/') + successful = readCppStyleComment(); + if (!successful) + return false; + + if (collectComments_) { + CommentPlacement placement = commentBefore; + if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) { + if (c != '*' || !containsNewLine(commentBegin, current_)) + placement = commentAfterOnSameLine; + } + + addComment(commentBegin, current_, placement); + } + return true; +} + +void +OurReader::addComment(Location begin, Location end, CommentPlacement placement) { + assert(collectComments_); + const std::string& normalized = normalizeEOL(begin, end); + if (placement == commentAfterOnSameLine) { + assert(lastValue_ != 0); + lastValue_->setComment(normalized, placement); + } else { + commentsBefore_ += normalized; + } +} + +bool OurReader::readCStyleComment() { + while (current_ != end_) { + Char c = getNextChar(); + if (c == '*' && *current_ == '/') break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '-': - token.type_ = tokenNumber; - readNumber(); + } + return getNextChar() == '/'; +} + +bool OurReader::readCppStyleComment() { + while (current_ != end_) { + Char c = getNextChar(); + if (c == '\n') break; - case 't': - token.type_ = tokenTrue; - ok = match( "rue", 3 ); + if (c == '\r') { + // Consume DOS EOL. It will be normalized in addComment. + if (current_ != end_ && *current_ == '\n') + getNextChar(); + // Break on Moc OS 9 EOL. break; - case 'f': - token.type_ = tokenFalse; - ok = match( "alse", 4 ); + } + } + return true; +} + +bool OurReader::readNumber(bool checkInf) { + const char *p = current_; + if (checkInf && p != end_ && *p == 'I') { + current_ = ++p; + return false; + } + char c = '0'; // stopgap for already consumed character + // integral part + while (c >= '0' && c <= '9') + c = (current_ = p) < end_ ? *p++ : 0; + // fractional part + if (c == '.') { + c = (current_ = p) < end_ ? *p++ : 0; + while (c >= '0' && c <= '9') + c = (current_ = p) < end_ ? *p++ : 0; + } + // exponential part + if (c == 'e' || c == 'E') { + c = (current_ = p) < end_ ? *p++ : 0; + if (c == '+' || c == '-') + c = (current_ = p) < end_ ? *p++ : 0; + while (c >= '0' && c <= '9') + c = (current_ = p) < end_ ? *p++ : 0; + } + return true; +} +bool OurReader::readString() { + Char c = 0; + while (current_ != end_) { + c = getNextChar(); + if (c == '\\') + getNextChar(); + else if (c == '"') break; - case 'n': - token.type_ = tokenNull; - ok = match( "ull", 3 ); + } + return c == '"'; +} + + +bool OurReader::readStringSingleQuote() { + Char c = 0; + while (current_ != end_) { + c = getNextChar(); + if (c == '\\') + getNextChar(); + else if (c == '\'') break; - case ',': - token.type_ = tokenArraySeparator; + } + return c == '\''; +} + +bool OurReader::readObject(Token& /*tokenStart*/) { + Token tokenName; + std::string name; + Value init(objectValue); + currentValue().swapPayload(init); + while (readToken(tokenName)) { + bool initialTokenOk = true; + while (tokenName.type_ == tokenComment && initialTokenOk) + initialTokenOk = readToken(tokenName); + if (!initialTokenOk) break; - case ':': - token.type_ = tokenMemberSeparator; + if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object + return true; + name = ""; + if (tokenName.type_ == tokenString) { + if (!decodeString(tokenName, name)) + return recoverFromError(tokenObjectEnd); + } else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) { + Value numberName; + if (!decodeNumber(tokenName, numberName)) + return recoverFromError(tokenObjectEnd); + name = numberName.asString(); + } else { break; - case 0: - token.type_ = tokenEndOfStream; + } + + Token colon; + if (!readToken(colon) || colon.type_ != tokenMemberSeparator) { + return addErrorAndRecover( + "Missing ':' after object member name", colon, tokenObjectEnd); + } + if (name.length() >= (1U<<30)) throwRuntimeError("keylength >= 2^30"); + if (features_.rejectDupKeys_ && currentValue().isMember(name)) { + std::string msg = "Duplicate key: '" + name + "'"; + return addErrorAndRecover( + msg, tokenName, tokenObjectEnd); + } + Value& value = currentValue()[name]; + nodes_.push(&value); + bool ok = readValue(); + nodes_.pop(); + if (!ok) // error already set + return recoverFromError(tokenObjectEnd); + + Token comma; + if (!readToken(comma) || + (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator && + comma.type_ != tokenComment)) { + return addErrorAndRecover( + "Missing ',' or '}' in object declaration", comma, tokenObjectEnd); + } + bool finalizeTokenOk = true; + while (comma.type_ == tokenComment && finalizeTokenOk) + finalizeTokenOk = readToken(comma); + if (comma.type_ == tokenObjectEnd) + return true; + } + return addErrorAndRecover( + "Missing '}' or object member name", tokenName, tokenObjectEnd); +} + +bool OurReader::readArray(Token& /*tokenStart*/) { + Value init(arrayValue); + currentValue().swapPayload(init); + skipSpaces(); + if (*current_ == ']') // empty array + { + Token endArray; + readToken(endArray); + return true; + } + int index = 0; + for (;;) { + Value& value = currentValue()[index++]; + nodes_.push(&value); + bool ok = readValue(); + nodes_.pop(); + if (!ok) // error already set + return recoverFromError(tokenArrayEnd); + + Token token; + // Accept Comment after last item in the array. + ok = readToken(token); + while (token.type_ == tokenComment && ok) { + ok = readToken(token); + } + bool badTokenType = + (token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd); + if (!ok || badTokenType) { + return addErrorAndRecover( + "Missing ',' or ']' in array declaration", token, tokenArrayEnd); + } + if (token.type_ == tokenArrayEnd) break; - default: - ok = false; + } + return true; +} + +bool OurReader::decodeNumber(Token& token) { + Value decoded; + if (!decodeNumber(token, decoded)) + return false; + currentValue().swapPayload(decoded); + return true; +} + +bool OurReader::decodeNumber(Token& token, Value& decoded) { + // Attempts to parse the number as an integer. If the number is + // larger than the maximum supported value of an integer then + // we decode the number as a double. + Location current = token.start_; + bool isNegative = *current == '-'; + if (isNegative) + ++current; + // TODO: Help the compiler do the div and mod at compile time or get rid of them. + Value::LargestUInt maxIntegerValue = + isNegative ? Value::LargestUInt(-Value::minLargestInt) + : Value::maxLargestUInt; + Value::LargestUInt threshold = maxIntegerValue / 10; + Value::LargestUInt value = 0; + while (current < token.end_) { + Char c = *current++; + if (c < '0' || c > '9') + return decodeDouble(token, decoded); + Value::UInt digit(c - '0'); + if (value >= threshold) { + // We've hit or exceeded the max value divided by 10 (rounded down). If + // a) we've only just touched the limit, b) this is the last digit, and + // c) it's small enough to fit in that rounding delta, we're okay. + // Otherwise treat this number as a double to avoid overflow. + if (value > threshold || current != token.end_ || + digit > maxIntegerValue % 10) { + return decodeDouble(token, decoded); + } + } + value = value * 10 + digit; + } + if (isNegative) + decoded = -Value::LargestInt(value); + else if (value <= Value::LargestUInt(Value::maxInt)) + decoded = Value::LargestInt(value); + else + decoded = value; + return true; +} + +bool OurReader::decodeDouble(Token& token) { + Value decoded; + if (!decodeDouble(token, decoded)) + return false; + currentValue().swapPayload(decoded); + return true; +} + +bool OurReader::decodeDouble(Token& token, Value& decoded) { + double value = 0; + std::string buffer( token.start_, token.end_ ); + std::istringstream is(buffer); + if (!(is >> value)) + return addError("'" + std::string(token.start_, token.end_) + + "' is not a number.", + token); + decoded = value; + return true; +} + +bool OurReader::decodeString(Token& token) { + std::string decoded_string; + if (!decodeString(token, decoded_string)) + return false; + Value decoded(decoded_string); + currentValue().swapPayload(decoded); + return true; +} + +bool OurReader::decodeString(Token& token, std::string& decoded) { + decoded.reserve(token.end_ - token.start_ - 2); + Location current = token.start_ + 1; // skip '"' + Location end = token.end_ - 1; // do not include '"' + while (current != end) { + Char c = *current++; + if (c == '"') break; - } - if ( !ok ) - token.type_ = tokenError; - token.end_ = current_; - return true; + else if (c == '\\') { + if (current == end) + return addError("Empty escape sequence in string", token, current); + Char escape = *current++; + switch (escape) { + case '"': + decoded += '"'; + break; + case '/': + decoded += '/'; + break; + case '\\': + decoded += '\\'; + break; + case 'b': + decoded += '\b'; + break; + case 'f': + decoded += '\f'; + break; + case 'n': + decoded += '\n'; + break; + case 'r': + decoded += '\r'; + break; + case 't': + decoded += '\t'; + break; + case 'u': { + unsigned int unicode; + if (!decodeUnicodeCodePoint(token, current, end, unicode)) + return false; + decoded += codePointToUTF8(unicode); + } break; + default: + return addError("Bad escape sequence in string", token, current); + } + } else { + decoded += c; + } + } + return true; +} + +bool OurReader::decodeUnicodeCodePoint(Token& token, + Location& current, + Location end, + unsigned int& unicode) { + + if (!decodeUnicodeEscapeSequence(token, current, end, unicode)) + return false; + if (unicode >= 0xD800 && unicode <= 0xDBFF) { + // surrogate pairs + if (end - current < 6) + return addError( + "additional six characters expected to parse unicode surrogate pair.", + token, + current); + unsigned int surrogatePair; + if (*(current++) == '\\' && *(current++) == 'u') { + if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) { + unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); + } else + return false; + } else + return addError("expecting another \\u token to begin the second half of " + "a unicode surrogate pair", + token, + current); + } + return true; +} + +bool OurReader::decodeUnicodeEscapeSequence(Token& token, + Location& current, + Location end, + unsigned int& unicode) { + if (end - current < 4) + return addError( + "Bad unicode escape sequence in string: four digits expected.", + token, + current); + unicode = 0; + for (int index = 0; index < 4; ++index) { + Char c = *current++; + unicode *= 16; + if (c >= '0' && c <= '9') + unicode += c - '0'; + else if (c >= 'a' && c <= 'f') + unicode += c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + unicode += c - 'A' + 10; + else + return addError( + "Bad unicode escape sequence in string: hexadecimal digit expected.", + token, + current); + } + return true; } +bool +OurReader::addError(const std::string& message, Token& token, Location extra) { + ErrorInfo info; + info.token_ = token; + info.message_ = message; + info.extra_ = extra; + errors_.push_back(info); + return false; +} + +bool OurReader::recoverFromError(TokenType skipUntilToken) { + int errorCount = int(errors_.size()); + Token skip; + for (;;) { + if (!readToken(skip)) + errors_.resize(errorCount); // discard errors caused by recovery + if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream) + break; + } + errors_.resize(errorCount); + return false; +} + +bool OurReader::addErrorAndRecover(const std::string& message, + Token& token, + TokenType skipUntilToken) { + addError(message, token); + return recoverFromError(skipUntilToken); +} + +Value& OurReader::currentValue() { return *(nodes_.top()); } + +OurReader::Char OurReader::getNextChar() { + if (current_ == end_) + return 0; + return *current_++; +} + +void OurReader::getLocationLineAndColumn(Location location, + int& line, + int& column) const { + Location current = begin_; + Location lastLineStart = current; + line = 0; + while (current < location && current != end_) { + Char c = *current++; + if (c == '\r') { + if (*current == '\n') + ++current; + lastLineStart = current; + ++line; + } else if (c == '\n') { + lastLineStart = current; + ++line; + } + } + // column & line start at 1 + column = int(location - lastLineStart) + 1; + ++line; +} + +std::string OurReader::getLocationLineAndColumn(Location location) const { + int line, column; + getLocationLineAndColumn(location, line, column); + char buffer[18 + 16 + 16 + 1]; + snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column); + return buffer; +} + +std::string OurReader::getFormattedErrorMessages() const { + std::string formattedMessage; + for (Errors::const_iterator itError = errors_.begin(); + itError != errors_.end(); + ++itError) { + const ErrorInfo& error = *itError; + formattedMessage += + "* " + getLocationLineAndColumn(error.token_.start_) + "\n"; + formattedMessage += " " + error.message_ + "\n"; + if (error.extra_) + formattedMessage += + "See " + getLocationLineAndColumn(error.extra_) + " for detail.\n"; + } + return formattedMessage; +} + + +class OurCharReader : public CharReader { + bool const collectComments_; + OurReader reader_; +public: + OurCharReader( + bool collectComments, + OurFeatures const& features) + : collectComments_(collectComments) + , reader_(features) + {} + virtual bool parse( + char const* beginDoc, char const* endDoc, + Value* root, std::string* errs) { + bool ok = reader_.parse(beginDoc, endDoc, *root, collectComments_); + if (errs) { + *errs = reader_.getFormattedErrorMessages(); + } + return ok; + } +}; -void -Reader::skipSpaces() -{ - while ( current_ != end_ ) - { - Char c = *current_; - if ( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) - ++current_; - else - break; - } +CharReaderBuilder::CharReaderBuilder() +{ + setDefaults(&settings_); +} +CharReaderBuilder::~CharReaderBuilder() +{} +CharReader* CharReaderBuilder::newCharReader() const +{ + bool collectComments = settings_["collectComments"].asBool(); + OurFeatures features = OurFeatures::all(); + features.allowComments_ = settings_["allowComments"].asBool(); + features.strictRoot_ = settings_["strictRoot"].asBool(); + features.allowDroppedNullPlaceholders_ = settings_["allowDroppedNullPlaceholders"].asBool(); + features.allowNumericKeys_ = settings_["allowNumericKeys"].asBool(); + features.allowSingleQuotes_ = settings_["allowSingleQuotes"].asBool(); + features.stackLimit_ = settings_["stackLimit"].asInt(); + features.failIfExtra_ = settings_["failIfExtra"].asBool(); + features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool(); + features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool(); + return new OurCharReader(collectComments, features); +} +static void getValidReaderKeys(std::set* valid_keys) +{ + valid_keys->clear(); + valid_keys->insert("collectComments"); + valid_keys->insert("allowComments"); + valid_keys->insert("strictRoot"); + valid_keys->insert("allowDroppedNullPlaceholders"); + valid_keys->insert("allowNumericKeys"); + valid_keys->insert("allowSingleQuotes"); + valid_keys->insert("stackLimit"); + valid_keys->insert("failIfExtra"); + valid_keys->insert("rejectDupKeys"); + valid_keys->insert("allowSpecialFloats"); +} +bool CharReaderBuilder::validate(Json::Value* invalid) const +{ + Json::Value my_invalid; + if (!invalid) invalid = &my_invalid; // so we do not need to test for NULL + Json::Value& inv = *invalid; + std::set valid_keys; + getValidReaderKeys(&valid_keys); + Value::Members keys = settings_.getMemberNames(); + size_t n = keys.size(); + for (size_t i = 0; i < n; ++i) { + std::string const& key = keys[i]; + if (valid_keys.find(key) == valid_keys.end()) { + inv[key] = settings_[key]; + } + } + return 0u == inv.size(); +} +Value& CharReaderBuilder::operator[](std::string key) +{ + return settings_[key]; +} +// static +void CharReaderBuilder::strictMode(Json::Value* settings) +{ +//! [CharReaderBuilderStrictMode] + (*settings)["allowComments"] = false; + (*settings)["strictRoot"] = true; + (*settings)["allowDroppedNullPlaceholders"] = false; + (*settings)["allowNumericKeys"] = false; + (*settings)["allowSingleQuotes"] = false; + (*settings)["failIfExtra"] = true; + (*settings)["rejectDupKeys"] = true; + (*settings)["allowSpecialFloats"] = false; +//! [CharReaderBuilderStrictMode] +} +// static +void CharReaderBuilder::setDefaults(Json::Value* settings) +{ +//! [CharReaderBuilderDefaults] + (*settings)["collectComments"] = true; + (*settings)["allowComments"] = true; + (*settings)["strictRoot"] = false; + (*settings)["allowDroppedNullPlaceholders"] = false; + (*settings)["allowNumericKeys"] = false; + (*settings)["allowSingleQuotes"] = false; + (*settings)["stackLimit"] = 1000; + (*settings)["failIfExtra"] = false; + (*settings)["rejectDupKeys"] = false; + (*settings)["allowSpecialFloats"] = false; +//! [CharReaderBuilderDefaults] +} + +////////////////////////////////// +// global functions + +bool parseFromStream( + CharReader::Factory const& fact, std::istream& sin, + Value* root, std::string* errs) +{ + std::ostringstream ssin; + ssin << sin.rdbuf(); + std::string doc = ssin.str(); + char const* begin = doc.data(); + char const* end = begin + doc.size(); + // Note that we do not actually need a null-terminator. + CharReaderPtr const reader(fact.newCharReader()); + return reader->parse(begin, end, root, errs); +} + +std::istream& operator>>(std::istream& sin, Value& root) { + CharReaderBuilder b; + std::string errs; + bool ok = parseFromStream(b, sin, &root, &errs); + if (!ok) { + fprintf(stderr, + "Error from reader: %s", + errs.c_str()); + + throwRuntimeError("reader error"); + } + return sin; } +} // namespace Json -bool -Reader::match( Location pattern, - int patternLength ) -{ - if ( end_ - current_ < patternLength ) - return false; - int index = patternLength; - while ( index-- ) - if ( current_[index] != pattern[index] ) - return false; - current_ += patternLength; - return true; -} +// ////////////////////////////////////////////////////////////////////// +// End of content of file: src/lib_json/json_reader.cpp +// ////////////////////////////////////////////////////////////////////// -bool -Reader::readComment() -{ - Location commentBegin = current_ - 1; - Char c = getNextChar(); - bool successful = false; - if ( c == '*' ) - successful = readCStyleComment(); - else if ( c == '/' ) - successful = readCppStyleComment(); - if ( !successful ) - return false; - if ( collectComments_ ) - { - CommentPlacement placement = commentBefore; - if ( lastValueEnd_ && !containsNewLine( lastValueEnd_, commentBegin ) ) - { - if ( c != '*' || !containsNewLine( commentBegin, current_ ) ) - placement = commentAfterOnSameLine; - } - addComment( commentBegin, current_, placement ); - } - return true; -} -void -Reader::addComment( Location begin, - Location end, - CommentPlacement placement ) -{ - assert( collectComments_ ); - if ( placement == commentAfterOnSameLine ) - { - assert( lastValue_ != 0 ); - lastValue_->setComment( std::string( begin, end ), placement ); - } - else - { - if ( !commentsBefore_.empty() ) - commentsBefore_ += "\n"; - commentsBefore_ += std::string( begin, end ); - } -} - - -bool -Reader::readCStyleComment() -{ - while ( current_ != end_ ) - { - Char c = getNextChar(); - if ( c == '*' && *current_ == '/' ) - break; - } - return getNextChar() == '/'; -} +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: src/lib_json/json_valueiterator.inl +// ////////////////////////////////////////////////////////////////////// +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE -bool -Reader::readCppStyleComment() -{ - while ( current_ != end_ ) - { - Char c = getNextChar(); - if ( c == '\r' || c == '\n' ) - break; - } - return true; -} +// included by json_value.cpp +namespace Json { -void -Reader::readNumber() -{ - while ( current_ != end_ ) - { - if ( !(*current_ >= '0' && *current_ <= '9') && - !in( *current_, '.', 'e', 'E', '+', '-' ) ) - break; - ++current_; - } -} +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class ValueIteratorBase +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// -bool -Reader::readString() -{ - Char c = 0; - while ( current_ != end_ ) - { - c = getNextChar(); - if ( c == '\\' ) - getNextChar(); - else if ( c == '"' ) - break; - } - return c == '"'; +ValueIteratorBase::ValueIteratorBase() + : current_(), isNull_(true) { } +ValueIteratorBase::ValueIteratorBase( + const Value::ObjectValues::iterator& current) + : current_(current), isNull_(false) {} -bool -Reader::readObject( Token &/*tokenStart*/ ) -{ - Token tokenName; - std::string name; - currentValue() = Value( objectValue ); - while ( readToken( tokenName ) ) - { - bool initialTokenOk = true; - while ( tokenName.type_ == tokenComment && initialTokenOk ) - initialTokenOk = readToken( tokenName ); - if ( !initialTokenOk ) - break; - if ( tokenName.type_ == tokenObjectEnd && name.empty() ) // empty object - return true; - if ( tokenName.type_ != tokenString ) - break; - - name = ""; - if ( !decodeString( tokenName, name ) ) - return recoverFromError( tokenObjectEnd ); - - Token colon; - if ( !readToken( colon ) || colon.type_ != tokenMemberSeparator ) - { - return addErrorAndRecover( "Missing ':' after object member name", - colon, - tokenObjectEnd ); - } - Value &value = currentValue()[ name ]; - nodes_.push( &value ); - bool ok = readValue(); - nodes_.pop(); - if ( !ok ) // error already set - return recoverFromError( tokenObjectEnd ); - - Token comma; - if ( !readToken( comma ) - || ( comma.type_ != tokenObjectEnd && - comma.type_ != tokenArraySeparator && - comma.type_ != tokenComment ) ) - { - return addErrorAndRecover( "Missing ',' or '}' in object declaration", - comma, - tokenObjectEnd ); - } - bool finalizeTokenOk = true; - while ( comma.type_ == tokenComment && - finalizeTokenOk ) - finalizeTokenOk = readToken( comma ); - if ( comma.type_ == tokenObjectEnd ) - return true; - } - return addErrorAndRecover( "Missing '}' or object member name", - tokenName, - tokenObjectEnd ); +Value& ValueIteratorBase::deref() const { + return current_->second; } - -bool -Reader::readArray( Token &/*tokenStart*/ ) -{ - currentValue() = Value( arrayValue ); - skipSpaces(); - if ( *current_ == ']' ) // empty array - { - Token endArray; - readToken( endArray ); - return true; - } - int index = 0; - for (;;) - { - Value &value = currentValue()[ index++ ]; - nodes_.push( &value ); - bool ok = readValue(); - nodes_.pop(); - if ( !ok ) // error already set - return recoverFromError( tokenArrayEnd ); - - Token token; - // Accept Comment after last item in the array. - ok = readToken( token ); - while ( token.type_ == tokenComment && ok ) - { - ok = readToken( token ); - } - bool badTokenType = ( token.type_ != tokenArraySeparator && - token.type_ != tokenArrayEnd ); - if ( !ok || badTokenType ) - { - return addErrorAndRecover( "Missing ',' or ']' in array declaration", - token, - tokenArrayEnd ); - } - if ( token.type_ == tokenArrayEnd ) - break; - } - return true; +void ValueIteratorBase::increment() { + ++current_; } - -bool -Reader::decodeNumber( Token &token ) -{ - bool isDouble = false; - for ( Location inspect = token.start_; inspect != token.end_; ++inspect ) - { - isDouble = isDouble - || in( *inspect, '.', 'e', 'E', '+' ) - || ( *inspect == '-' && inspect != token.start_ ); - } - if ( isDouble ) - return decodeDouble( token ); - // Attempts to parse the number as an integer. If the number is - // larger than the maximum supported value of an integer then - // we decode the number as a double. - Location current = token.start_; - bool isNegative = *current == '-'; - if ( isNegative ) - ++current; - Value::LargestUInt maxIntegerValue = isNegative ? Value::LargestUInt(-Value::minLargestInt) - : Value::maxLargestUInt; - Value::LargestUInt threshold = maxIntegerValue / 10; - Value::UInt lastDigitThreshold = Value::UInt( maxIntegerValue % 10 ); - assert( lastDigitThreshold >=0 && lastDigitThreshold <= 9 ); - Value::LargestUInt value = 0; - while ( current < token.end_ ) - { - Char c = *current++; - if ( c < '0' || c > '9' ) - return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token ); - Value::UInt digit(c - '0'); - if ( value >= threshold ) - { - // If the current digit is not the last one, or if it is - // greater than the last digit of the maximum integer value, - // the parse the number as a double. - if ( current != token.end_ || digit > lastDigitThreshold ) - { - return decodeDouble( token ); - } - } - value = value * 10 + digit; - } - if ( isNegative ) - currentValue() = -Value::LargestInt( value ); - else if ( value <= Value::LargestUInt(Value::maxInt) ) - currentValue() = Value::LargestInt( value ); - else - currentValue() = value; - return true; +void ValueIteratorBase::decrement() { + --current_; } - -bool -Reader::decodeDouble( Token &token ) -{ - double value = 0; - const int bufferSize = 32; - int count; - int length = int(token.end_ - token.start_); - if ( length <= bufferSize ) - { - Char buffer[bufferSize+1]; - memcpy( buffer, token.start_, length ); - buffer[length] = 0; - count = sscanf( buffer, "%lf", &value ); - } - else - { - std::string buffer( token.start_, token.end_ ); - count = sscanf( buffer.c_str(), "%lf", &value ); - } - - if ( count != 1 ) - return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token ); - currentValue() = value; - return true; -} - - -bool -Reader::decodeString( Token &token ) -{ - std::string decoded; - if ( !decodeString( token, decoded ) ) - return false; - currentValue() = decoded; - return true; +ValueIteratorBase::difference_type +ValueIteratorBase::computeDistance(const SelfType& other) const { +#ifdef JSON_USE_CPPTL_SMALLMAP + return other.current_ - current_; +#else + // Iterator for null value are initialized using the default + // constructor, which initialize current_ to the default + // std::map::iterator. As begin() and end() are two instance + // of the default std::map::iterator, they can not be compared. + // To allow this, we handle this comparison specifically. + if (isNull_ && other.isNull_) { + return 0; + } + + // Usage of std::distance is not portable (does not compile with Sun Studio 12 + // RogueWave STL, + // which is the one used by default). + // Using a portable hand-made version for non random iterator instead: + // return difference_type( std::distance( current_, other.current_ ) ); + difference_type myDistance = 0; + for (Value::ObjectValues::iterator it = current_; it != other.current_; + ++it) { + ++myDistance; + } + return myDistance; +#endif } - -bool -Reader::decodeString( Token &token, std::string &decoded ) -{ - decoded.reserve( token.end_ - token.start_ - 2 ); - Location current = token.start_ + 1; // skip '"' - Location end = token.end_ - 1; // do not include '"' - while ( current != end ) - { - Char c = *current++; - if ( c == '"' ) - break; - else if ( c == '\\' ) - { - if ( current == end ) - return addError( "Empty escape sequence in string", token, current ); - Char escape = *current++; - switch ( escape ) - { - case '"': decoded += '"'; break; - case '/': decoded += '/'; break; - case '\\': decoded += '\\'; break; - case 'b': decoded += '\b'; break; - case 'f': decoded += '\f'; break; - case 'n': decoded += '\n'; break; - case 'r': decoded += '\r'; break; - case 't': decoded += '\t'; break; - case 'u': - { - unsigned int unicode; - if ( !decodeUnicodeCodePoint( token, current, end, unicode ) ) - return false; - decoded += codePointToUTF8(unicode); - } - break; - default: - return addError( "Bad escape sequence in string", token, current ); - } - } - else - { - decoded += c; - } - } - return true; +bool ValueIteratorBase::isEqual(const SelfType& other) const { + if (isNull_) { + return other.isNull_; + } + return current_ == other.current_; } -bool -Reader::decodeUnicodeCodePoint( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ) -{ +void ValueIteratorBase::copy(const SelfType& other) { + current_ = other.current_; + isNull_ = other.isNull_; +} - if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) ) - return false; - if (unicode >= 0xD800 && unicode <= 0xDBFF) - { - // surrogate pairs - if (end - current < 6) - return addError( "additional six characters expected to parse unicode surrogate pair.", token, current ); - unsigned int surrogatePair; - if (*(current++) == '\\' && *(current++)== 'u') - { - if (decodeUnicodeEscapeSequence( token, current, end, surrogatePair )) - { - unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); - } - else - return false; - } - else - return addError( "expecting another \\u token to begin the second half of a unicode surrogate pair", token, current ); - } - return true; -} - -bool -Reader::decodeUnicodeEscapeSequence( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ) -{ - if ( end - current < 4 ) - return addError( "Bad unicode escape sequence in string: four digits expected.", token, current ); - unicode = 0; - for ( int index =0; index < 4; ++index ) - { - Char c = *current++; - unicode *= 16; - if ( c >= '0' && c <= '9' ) - unicode += c - '0'; - else if ( c >= 'a' && c <= 'f' ) - unicode += c - 'a' + 10; - else if ( c >= 'A' && c <= 'F' ) - unicode += c - 'A' + 10; - else - return addError( "Bad unicode escape sequence in string: hexadecimal digit expected.", token, current ); - } - return true; -} - - -bool -Reader::addError( const std::string &message, - Token &token, - Location extra ) -{ - ErrorInfo info; - info.token_ = token; - info.message_ = message; - info.extra_ = extra; - errors_.push_back( info ); - return false; +Value ValueIteratorBase::key() const { + const Value::CZString czstring = (*current_).first; + if (czstring.data()) { + if (czstring.isStaticString()) + return Value(StaticString(czstring.data())); + return Value(czstring.data(), czstring.data() + czstring.length()); + } + return Value(czstring.index()); } +UInt ValueIteratorBase::index() const { + const Value::CZString czstring = (*current_).first; + if (!czstring.data()) + return czstring.index(); + return Value::UInt(-1); +} -bool -Reader::recoverFromError( TokenType skipUntilToken ) -{ - int errorCount = int(errors_.size()); - Token skip; - for (;;) - { - if ( !readToken(skip) ) - errors_.resize( errorCount ); // discard errors caused by recovery - if ( skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream ) - break; - } - errors_.resize( errorCount ); - return false; -} - - -bool -Reader::addErrorAndRecover( const std::string &message, - Token &token, - TokenType skipUntilToken ) -{ - addError( message, token ); - return recoverFromError( skipUntilToken ); +std::string ValueIteratorBase::name() const { + char const* keey; + char const* end; + keey = memberName(&end); + if (!keey) return std::string(); + return std::string(keey, end); } +char const* ValueIteratorBase::memberName() const { + const char* cname = (*current_).first.data(); + return cname ? cname : ""; +} -Value & -Reader::currentValue() -{ - return *(nodes_.top()); +char const* ValueIteratorBase::memberName(char const** end) const { + const char* cname = (*current_).first.data(); + if (!cname) { + *end = NULL; + return NULL; + } + *end = cname + (*current_).first.length(); + return cname; } +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class ValueConstIterator +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// -Reader::Char -Reader::getNextChar() -{ - if ( current_ == end_ ) - return 0; - return *current_++; -} +ValueConstIterator::ValueConstIterator() {} +ValueConstIterator::ValueConstIterator( + const Value::ObjectValues::iterator& current) + : ValueIteratorBase(current) {} -void -Reader::getLocationLineAndColumn( Location location, - int &line, - int &column ) const -{ - Location current = begin_; - Location lastLineStart = current; - line = 0; - while ( current < location && current != end_ ) - { - Char c = *current++; - if ( c == '\r' ) - { - if ( *current == '\n' ) - ++current; - lastLineStart = current; - ++line; - } - else if ( c == '\n' ) - { - lastLineStart = current; - ++line; - } - } - // column & line start at 1 - column = int(location - lastLineStart) + 1; - ++line; +ValueConstIterator& ValueConstIterator:: +operator=(const ValueIteratorBase& other) { + copy(other); + return *this; } +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class ValueIterator +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// -std::string -Reader::getLocationLineAndColumn( Location location ) const -{ - int line, column; - getLocationLineAndColumn( location, line, column ); - char buffer[18+16+16+1]; - sprintf( buffer, "Line %d, Column %d", line, column ); - return buffer; -} +ValueIterator::ValueIterator() {} +ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current) + : ValueIteratorBase(current) {} -// Deprecated. Preserved for backward compatibility -std::string -Reader::getFormatedErrorMessages() const -{ - return getFormattedErrorMessages(); -} +ValueIterator::ValueIterator(const ValueConstIterator& other) + : ValueIteratorBase(other) {} +ValueIterator::ValueIterator(const ValueIterator& other) + : ValueIteratorBase(other) {} -std::string -Reader::getFormattedErrorMessages() const -{ - std::string formattedMessage; - for ( Errors::const_iterator itError = errors_.begin(); - itError != errors_.end(); - ++itError ) - { - const ErrorInfo &error = *itError; - formattedMessage += "* " + getLocationLineAndColumn( error.token_.start_ ) + "\n"; - formattedMessage += " " + error.message_ + "\n"; - if ( error.extra_ ) - formattedMessage += "See " + getLocationLineAndColumn( error.extra_ ) + " for detail.\n"; - } - return formattedMessage; -} - - -std::istream& operator>>( std::istream &sin, Value &root ) -{ - Json::Reader reader; - bool ok = reader.parse(sin, root, true); - //JSON_ASSERT( ok ); - if (!ok) throw std::runtime_error(reader.getFormattedErrorMessages()); - return sin; +ValueIterator& ValueIterator::operator=(const SelfType& other) { + copy(other); + return *this; } - } // namespace Json // ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_reader.cpp +// End of content of file: src/lib_json/json_valueiterator.inl // ////////////////////////////////////////////////////////////////////// @@ -1077,510 +2210,83 @@ std::istream& operator>>( std::istream &sin, Value &root ) // ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_batchallocator.h +// Beginning of content of file: src/lib_json/json_value.cpp // ////////////////////////////////////////////////////////////////////// -// Copyright 2007-2010 Baptiste Lepilleur +// Copyright 2011 Baptiste Lepilleur // Distributed under MIT license, or public domain if desired and // recognized in your jurisdiction. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE -#ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED -# define JSONCPP_BATCHALLOCATOR_H_INCLUDED - -# include -# include +#if !defined(JSON_IS_AMALGAMATION) +#include +#include +#include +#endif // if !defined(JSON_IS_AMALGAMATION) +#include +#include +#include +#include +#include +#ifdef JSON_USE_CPPTL +#include +#endif +#include // size_t +#include // min() -# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION +#define JSON_ASSERT_UNREACHABLE assert(false) namespace Json { -/* Fast memory allocator. - * - * This memory allocator allocates memory for a batch of object (specified by - * the page size, the number of object in each page). - * - * It does not allow the destruction of a single object. All the allocated objects - * can be destroyed at once. The memory can be either released or reused for future - * allocation. - * - * The in-place new operator must be used to construct the object using the pointer - * returned by allocate. - */ -template -class BatchAllocator -{ -public: - typedef AllocatedType Type; - - BatchAllocator( unsigned int objectsPerPage = 255 ) - : freeHead_( 0 ) - , objectsPerPage_( objectsPerPage ) - { -// printf( "Size: %d => %s\n", sizeof(AllocatedType), typeid(AllocatedType).name() ); - assert( sizeof(AllocatedType) * objectPerAllocation >= sizeof(AllocatedType *) ); // We must be able to store a slist in the object free space. - assert( objectsPerPage >= 16 ); - batches_ = allocateBatch( 0 ); // allocated a dummy page - currentBatch_ = batches_; - } - - ~BatchAllocator() - { - for ( BatchInfo *batch = batches_; batch; ) - { - BatchInfo *nextBatch = batch->next_; - free( batch ); - batch = nextBatch; - } - } - - /// allocate space for an array of objectPerAllocation object. - /// @warning it is the responsability of the caller to call objects constructors. - AllocatedType *allocate() - { - if ( freeHead_ ) // returns node from free list. - { - AllocatedType *object = freeHead_; - freeHead_ = *(AllocatedType **)object; - return object; - } - if ( currentBatch_->used_ == currentBatch_->end_ ) - { - currentBatch_ = currentBatch_->next_; - while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ ) - currentBatch_ = currentBatch_->next_; - - if ( !currentBatch_ ) // no free batch found, allocate a new one - { - currentBatch_ = allocateBatch( objectsPerPage_ ); - currentBatch_->next_ = batches_; // insert at the head of the list - batches_ = currentBatch_; - } - } - AllocatedType *allocated = currentBatch_->used_; - currentBatch_->used_ += objectPerAllocation; - return allocated; - } - - /// Release the object. - /// @warning it is the responsability of the caller to actually destruct the object. - void release( AllocatedType *object ) - { - assert( object != 0 ); - *(AllocatedType **)object = freeHead_; - freeHead_ = object; - } - -private: - struct BatchInfo - { - BatchInfo *next_; - AllocatedType *used_; - AllocatedType *end_; - AllocatedType buffer_[objectPerAllocation]; - }; - - // disabled copy constructor and assignement operator. - BatchAllocator( const BatchAllocator & ); - void operator =( const BatchAllocator &); - - static BatchInfo *allocateBatch( unsigned int objectsPerPage ) - { - const unsigned int mallocSize = sizeof(BatchInfo) - sizeof(AllocatedType)* objectPerAllocation - + sizeof(AllocatedType) * objectPerAllocation * objectsPerPage; - BatchInfo *batch = static_cast( malloc( mallocSize ) ); - batch->next_ = 0; - batch->used_ = batch->buffer_; - batch->end_ = batch->buffer_ + objectsPerPage; - return batch; - } - - BatchInfo *batches_; - BatchInfo *currentBatch_; - /// Head of a single linked list within the allocated space of freeed object - AllocatedType *freeHead_; - unsigned int objectsPerPage_; -}; +// This is a walkaround to avoid the static initialization of Value::null. +// kNull must be word-aligned to avoid crashing on ARM. We use an alignment of +// 8 (instead of 4) as a bit of future-proofing. +#if defined(__ARMEL__) +#define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) +#else +// This exists for binary compatibility only. Use nullRef. +const Value Value::null; +#define ALIGNAS(byte_alignment) +#endif +static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 }; +const unsigned char& kNullRef = kNull[0]; +const Value& Value::nullRef = reinterpret_cast(kNullRef); +const Int Value::minInt = Int(~(UInt(-1) / 2)); +const Int Value::maxInt = Int(UInt(-1) / 2); +const UInt Value::maxUInt = UInt(-1); +#if defined(JSON_HAS_INT64) +const Int64 Value::minInt64 = Int64(~(UInt64(-1) / 2)); +const Int64 Value::maxInt64 = Int64(UInt64(-1) / 2); +const UInt64 Value::maxUInt64 = UInt64(-1); +// The constant is hard-coded because some compiler have trouble +// converting Value::maxUInt64 to a double correctly (AIX/xlC). +// Assumes that UInt64 is a 64 bits integer. +static const double maxUInt64AsDouble = 18446744073709551615.0; +#endif // defined(JSON_HAS_INT64) +const LargestInt Value::minLargestInt = LargestInt(~(LargestUInt(-1) / 2)); +const LargestInt Value::maxLargestInt = LargestInt(LargestUInt(-1) / 2); +const LargestUInt Value::maxLargestUInt = LargestUInt(-1); -} // namespace Json - -# endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION - -#endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED - - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_batchallocator.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_valueiterator.inl -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -// included by json_value.cpp - -namespace Json { - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class ValueIteratorBase -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - -ValueIteratorBase::ValueIteratorBase() -#ifndef JSON_VALUE_USE_INTERNAL_MAP - : current_() - , isNull_( true ) -{ -} -#else - : isArray_( true ) - , isNull_( true ) -{ - iterator_.array_ = ValueInternalArray::IteratorState(); -} -#endif - - -#ifndef JSON_VALUE_USE_INTERNAL_MAP -ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator ¤t ) - : current_( current ) - , isNull_( false ) -{ -} -#else -ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state ) - : isArray_( true ) -{ - iterator_.array_ = state; -} - - -ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state ) - : isArray_( false ) -{ - iterator_.map_ = state; -} -#endif - -Value & -ValueIteratorBase::deref() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - return current_->second; -#else - if ( isArray_ ) - return ValueInternalArray::dereference( iterator_.array_ ); - return ValueInternalMap::value( iterator_.map_ ); -#endif -} - - -void -ValueIteratorBase::increment() -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - ++current_; -#else - if ( isArray_ ) - ValueInternalArray::increment( iterator_.array_ ); - ValueInternalMap::increment( iterator_.map_ ); -#endif -} - - -void -ValueIteratorBase::decrement() -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - --current_; -#else - if ( isArray_ ) - ValueInternalArray::decrement( iterator_.array_ ); - ValueInternalMap::decrement( iterator_.map_ ); -#endif -} - - -ValueIteratorBase::difference_type -ValueIteratorBase::computeDistance( const SelfType &other ) const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP -# ifdef JSON_USE_CPPTL_SMALLMAP - return current_ - other.current_; -# else - // Iterator for null value are initialized using the default - // constructor, which initialize current_ to the default - // std::map::iterator. As begin() and end() are two instance - // of the default std::map::iterator, they can not be compared. - // To allow this, we handle this comparison specifically. - if ( isNull_ && other.isNull_ ) - { - return 0; - } - - - // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL, - // which is the one used by default). - // Using a portable hand-made version for non random iterator instead: - // return difference_type( std::distance( current_, other.current_ ) ); - difference_type myDistance = 0; - for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it ) - { - ++myDistance; - } - return myDistance; -# endif -#else - if ( isArray_ ) - return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ ); - return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ ); -#endif -} - - -bool -ValueIteratorBase::isEqual( const SelfType &other ) const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - if ( isNull_ ) - { - return other.isNull_; - } - return current_ == other.current_; -#else - if ( isArray_ ) - return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ ); - return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ ); -#endif -} - - -void -ValueIteratorBase::copy( const SelfType &other ) -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - current_ = other.current_; -#else - if ( isArray_ ) - iterator_.array_ = other.iterator_.array_; - iterator_.map_ = other.iterator_.map_; -#endif -} - - -Value -ValueIteratorBase::key() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - const Value::CZString czstring = (*current_).first; - if ( czstring.c_str() ) - { - if ( czstring.isStaticString() ) - return Value( StaticString( czstring.c_str() ) ); - return Value( czstring.c_str() ); - } - return Value( czstring.index() ); -#else - if ( isArray_ ) - return Value( ValueInternalArray::indexOf( iterator_.array_ ) ); - bool isStatic; - const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic ); - if ( isStatic ) - return Value( StaticString( memberName ) ); - return Value( memberName ); -#endif -} - - -UInt -ValueIteratorBase::index() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - const Value::CZString czstring = (*current_).first; - if ( !czstring.c_str() ) - return czstring.index(); - return Value::UInt( -1 ); -#else - if ( isArray_ ) - return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) ); - return Value::UInt( -1 ); -#endif -} - - -const char * -ValueIteratorBase::memberName() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - const char *name = (*current_).first.c_str(); - return name ? name : ""; -#else - if ( !isArray_ ) - return ValueInternalMap::key( iterator_.map_ ); - return ""; -#endif -} - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class ValueConstIterator -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - -ValueConstIterator::ValueConstIterator() -{ -} - - -#ifndef JSON_VALUE_USE_INTERNAL_MAP -ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator ¤t ) - : ValueIteratorBase( current ) -{ -} -#else -ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state ) - : ValueIteratorBase( state ) -{ -} - -ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state ) - : ValueIteratorBase( state ) -{ -} -#endif - -ValueConstIterator & -ValueConstIterator::operator =( const ValueIteratorBase &other ) -{ - copy( other ); - return *this; -} - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class ValueIterator -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - -ValueIterator::ValueIterator() -{ -} - - -#ifndef JSON_VALUE_USE_INTERNAL_MAP -ValueIterator::ValueIterator( const Value::ObjectValues::iterator ¤t ) - : ValueIteratorBase( current ) -{ +#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) +template +static inline bool InRange(double d, T min, U max) { + return d >= min && d <= max; } -#else -ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state ) - : ValueIteratorBase( state ) -{ +#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) +static inline double integerToDouble(Json::UInt64 value) { + return static_cast(Int64(value / 2)) * 2.0 + Int64(value & 1); } -ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state ) - : ValueIteratorBase( state ) -{ +template static inline double integerToDouble(T value) { + return static_cast(value); } -#endif -ValueIterator::ValueIterator( const ValueConstIterator &other ) - : ValueIteratorBase( other ) -{ -} - -ValueIterator::ValueIterator( const ValueIterator &other ) - : ValueIteratorBase( other ) -{ +template +static inline bool InRange(double d, T min, U max) { + return d >= integerToDouble(min) && d <= integerToDouble(max); } - -ValueIterator & -ValueIterator::operator =( const SelfType &other ) -{ - copy( other ); - return *this; -} - -} // namespace Json - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_valueiterator.inl -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_value.cpp -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#if !defined(JSON_IS_AMALGAMATION) -# include -# include -# ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR -# include "json_batchallocator.h" -# endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR -#endif // if !defined(JSON_IS_AMALGAMATION) -#include -#include -#include -#include -#include -#ifdef JSON_USE_CPPTL -# include -#endif -#include // size_t - -#define JSON_ASSERT_UNREACHABLE assert( false ) -#define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw -#define JSON_FAIL_MESSAGE( message ) throw std::runtime_error( message ); -#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) JSON_FAIL_MESSAGE( message ) - -namespace Json { - -const Value Value::null; -const Int Value::minInt = Int( ~(UInt(-1)/2) ); -const Int Value::maxInt = Int( UInt(-1)/2 ); -const UInt Value::maxUInt = UInt(-1); -const Int64 Value::minInt64 = Int64( ~(UInt64(-1)/2) ); -const Int64 Value::maxInt64 = Int64( UInt64(-1)/2 ); -const UInt64 Value::maxUInt64 = UInt64(-1); -const LargestInt Value::minLargestInt = LargestInt( ~(LargestUInt(-1)/2) ); -const LargestInt Value::maxLargestInt = LargestInt( LargestUInt(-1)/2 ); -const LargestUInt Value::maxLargestUInt = LargestUInt(-1); - - -/// Unknown size marker -static const unsigned int unknown = (unsigned)-1; - +#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) /** Duplicates the specified string value. * @param value Pointer to the string to duplicate. Must be zero-terminated if @@ -1589,32 +2295,65 @@ static const unsigned int unknown = (unsigned)-1; * computed using strlen(value). * @return Pointer on the duplicate instance of string. */ -static inline char * -duplicateStringValue( const char *value, - unsigned int length = unknown ) -{ - if ( length == unknown ) - length = (unsigned int)strlen(value); - char *newString = static_cast( malloc( length + 1 ) ); - JSON_ASSERT_MESSAGE( newString != 0, "Failed to allocate string value buffer" ); - memcpy( newString, value, length ); - newString[length] = 0; - return newString; -} - - -/** Free the string duplicated by duplicateStringValue(). +static inline char* duplicateStringValue(const char* value, + size_t length) { + // Avoid an integer overflow in the call to malloc below by limiting length + // to a sane value. + if (length >= (size_t)Value::maxInt) + length = Value::maxInt - 1; + + char* newString = static_cast(malloc(length + 1)); + if (newString == NULL) { + throwRuntimeError( + "in Json::Value::duplicateStringValue(): " + "Failed to allocate string value buffer"); + } + memcpy(newString, value, length); + newString[length] = 0; + return newString; +} + +/* Record the length as a prefix. */ -static inline void -releaseStringValue( char *value ) -{ - if ( value ) - free( value ); -} +static inline char* duplicateAndPrefixStringValue( + const char* value, + unsigned int length) +{ + // Avoid an integer overflow in the call to malloc below by limiting length + // to a sane value. + JSON_ASSERT_MESSAGE(length <= (unsigned)Value::maxInt - sizeof(unsigned) - 1U, + "in Json::Value::duplicateAndPrefixStringValue(): " + "length too big for prefixing"); + unsigned actualLength = length + static_cast(sizeof(unsigned)) + 1U; + char* newString = static_cast(malloc(actualLength)); + if (newString == 0) { + throwRuntimeError( + "in Json::Value::duplicateAndPrefixStringValue(): " + "Failed to allocate string value buffer"); + } + *reinterpret_cast(newString) = length; + memcpy(newString + sizeof(unsigned), value, length); + newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later + return newString; +} +inline static void decodePrefixedString( + bool isPrefixed, char const* prefixed, + unsigned* length, char const** value) +{ + if (!isPrefixed) { + *length = static_cast(strlen(prefixed)); + *value = prefixed; + } else { + *length = *reinterpret_cast(prefixed); + *value = prefixed + sizeof(unsigned); + } +} +/** Free the string duplicated by duplicateStringValue()/duplicateAndPrefixStringValue(). + */ +static inline void releaseStringValue(char* value) { free(value); } } // namespace Json - // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// @@ -1623,1261 +2362,1027 @@ releaseStringValue( char *value ) // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// #if !defined(JSON_IS_AMALGAMATION) -# ifdef JSON_VALUE_USE_INTERNAL_MAP -# include "json_internalarray.inl" -# include "json_internalmap.inl" -# endif // JSON_VALUE_USE_INTERNAL_MAP -# include "json_valueiterator.inl" +#include "json_valueiterator.inl" #endif // if !defined(JSON_IS_AMALGAMATION) namespace Json { -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class Value::CommentInfo -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - - -Value::CommentInfo::CommentInfo() - : comment_( 0 ) -{ -} - -Value::CommentInfo::~CommentInfo() -{ - if ( comment_ ) - releaseStringValue( comment_ ); -} - - -void -Value::CommentInfo::setComment( const char *text ) -{ - if ( comment_ ) - releaseStringValue( comment_ ); - JSON_ASSERT( text != 0 ); - JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "Comments must start with /"); - // It seems that /**/ style comments are acceptable as well. - comment_ = duplicateStringValue( text ); -} - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class Value::CZString -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -# ifndef JSON_VALUE_USE_INTERNAL_MAP - -// Notes: index_ indicates if the string was allocated when -// a string is stored. - -Value::CZString::CZString( ArrayIndex index ) - : cstr_( 0 ) - , index_( index ) -{ -} - -Value::CZString::CZString( const char *cstr, DuplicationPolicy allocate ) - : cstr_( allocate == duplicate ? duplicateStringValue(cstr) - : cstr ) - , index_( allocate ) -{ -} - -Value::CZString::CZString( const CZString &other ) -: cstr_( other.index_ != noDuplication && other.cstr_ != 0 - ? duplicateStringValue( other.cstr_ ) - : other.cstr_ ) - , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate) - : other.index_ ) +Exception::Exception(std::string const& msg) + : msg_(msg) +{} +Exception::~Exception() throw() +{} +char const* Exception::what() const throw() { + return msg_.c_str(); } - -Value::CZString::~CZString() -{ - if ( cstr_ && index_ == duplicate ) - releaseStringValue( const_cast( cstr_ ) ); -} - -void -Value::CZString::swap( CZString &other ) -{ - std::swap( cstr_, other.cstr_ ); - std::swap( index_, other.index_ ); -} - -Value::CZString & -Value::CZString::operator =( const CZString &other ) +RuntimeError::RuntimeError(std::string const& msg) + : Exception(msg) +{} +LogicError::LogicError(std::string const& msg) + : Exception(msg) +{} +JSONCPP_NORETURN void throwRuntimeError(std::string const& msg) { - CZString temp( other ); - swap( temp ); - return *this; + throw RuntimeError(msg); } - -bool -Value::CZString::operator<( const CZString &other ) const +JSONCPP_NORETURN void throwLogicError(std::string const& msg) { - if ( cstr_ ) - return strcmp( cstr_, other.cstr_ ) < 0; - return index_ < other.index_; + throw LogicError(msg); } -bool -Value::CZString::operator==( const CZString &other ) const -{ - if ( cstr_ ) - return strcmp( cstr_, other.cstr_ ) == 0; - return index_ == other.index_; -} - - -ArrayIndex -Value::CZString::index() const -{ - return index_; -} - - -const char * -Value::CZString::c_str() const -{ - return cstr_; -} - -bool -Value::CZString::isStaticString() const -{ - return index_ == noDuplication; -} - -#endif // ifndef JSON_VALUE_USE_INTERNAL_MAP - - // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// -// class Value::Value +// class Value::CommentInfo // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// -/*! \internal Default constructor initialization must be equivalent to: - * memset( this, 0, sizeof(Value) ) - * This optimization is used in ValueInternalMap fast allocator. - */ -Value::Value( ValueType type ) - : type_( type ) - , allocated_( 0 ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - switch ( type ) - { - case nullValue: - break; - case intValue: - case uintValue: - value_.int_ = 0; - break; - case realValue: - value_.real_ = 0.0; - break; - case stringValue: - value_.string_ = 0; - break; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - value_.map_ = new ObjectValues(); - break; -#else - case arrayValue: - value_.array_ = arrayAllocator()->newArray(); - break; - case objectValue: - value_.map_ = mapAllocator()->newMap(); - break; -#endif - case booleanValue: - value_.bool_ = false; - break; - default: - JSON_ASSERT_UNREACHABLE; - } -} - - -#if defined(JSON_HAS_INT64) -Value::Value( UInt value ) - : type_( uintValue ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.uint_ = value; -} - -Value::Value( Int value ) - : type_( intValue ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.int_ = value; -} +Value::CommentInfo::CommentInfo() : comment_(0) {} -#endif // if defined(JSON_HAS_INT64) - - -Value::Value( Int64 value ) - : type_( intValue ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.int_ = value; -} - - -Value::Value( UInt64 value ) - : type_( uintValue ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.uint_ = value; -} - -Value::Value( double value ) - : type_( realValue ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.real_ = value; +Value::CommentInfo::~CommentInfo() { + if (comment_) + releaseStringValue(comment_); } -Value::Value( const char *value ) - : type_( stringValue ) - , allocated_( true ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.string_ = duplicateStringValue( value ); +void Value::CommentInfo::setComment(const char* text, size_t len) { + if (comment_) { + releaseStringValue(comment_); + comment_ = 0; + } + JSON_ASSERT(text != 0); + JSON_ASSERT_MESSAGE( + text[0] == '\0' || text[0] == '/', + "in Json::Value::setComment(): Comments must start with /"); + // It seems that /**/ style comments are acceptable as well. + comment_ = duplicateStringValue(text, len); } +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class Value::CZString +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// -Value::Value( const char *beginValue, - const char *endValue ) - : type_( stringValue ) - , allocated_( true ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.string_ = duplicateStringValue( beginValue, - (unsigned int)(endValue - beginValue) ); -} +// Notes: policy_ indicates if the string was allocated when +// a string is stored. +Value::CZString::CZString(ArrayIndex aindex) : cstr_(0), index_(aindex) {} -Value::Value( const std::string &value ) - : type_( stringValue ) - , allocated_( true ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif +Value::CZString::CZString(char const* str, unsigned ulength, DuplicationPolicy allocate) + : cstr_(str) { - value_.string_ = duplicateStringValue( value.c_str(), - (unsigned int)value.length() ); - + // allocate != duplicate + storage_.policy_ = allocate & 0x3; + storage_.length_ = ulength & 0x3FFFFFFF; } -Value::Value( const StaticString &value ) - : type_( stringValue ) - , allocated_( false ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif +Value::CZString::CZString(const CZString& other) + : cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0 + ? duplicateStringValue(other.cstr_, other.storage_.length_) + : other.cstr_) { - value_.string_ = const_cast( value.c_str() ); + storage_.policy_ = (other.cstr_ + ? (static_cast(other.storage_.policy_) == noDuplication + ? noDuplication : duplicate) + : static_cast(other.storage_.policy_)); + storage_.length_ = other.storage_.length_; } +Value::CZString::~CZString() { + if (cstr_ && storage_.policy_ == duplicate) + releaseStringValue(const_cast(cstr_)); +} -# ifdef JSON_USE_CPPTL -Value::Value( const CppTL::ConstString &value ) - : type_( stringValue ) - , allocated_( true ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.string_ = duplicateStringValue( value, value.length() ); +void Value::CZString::swap(CZString& other) { + std::swap(cstr_, other.cstr_); + std::swap(index_, other.index_); } -# endif -Value::Value( bool value ) - : type_( booleanValue ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - value_.bool_ = value; +Value::CZString& Value::CZString::operator=(CZString other) { + swap(other); + return *this; } +bool Value::CZString::operator<(const CZString& other) const { + if (!cstr_) return index_ < other.index_; + //return strcmp(cstr_, other.cstr_) < 0; + // Assume both are strings. + unsigned this_len = this->storage_.length_; + unsigned other_len = other.storage_.length_; + unsigned min_len = std::min(this_len, other_len); + int comp = memcmp(this->cstr_, other.cstr_, min_len); + if (comp < 0) return true; + if (comp > 0) return false; + return (this_len < other_len); +} -Value::Value( const Value &other ) - : type_( other.type_ ) - , comments_( 0 ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif -{ - switch ( type_ ) - { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - value_ = other.value_; - break; - case stringValue: - if ( other.value_.string_ ) - { - value_.string_ = duplicateStringValue( other.value_.string_ ); - allocated_ = true; - } - else - value_.string_ = 0; - break; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - value_.map_ = new ObjectValues( *other.value_.map_ ); - break; -#else - case arrayValue: - value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ ); - break; - case objectValue: - value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ ); - break; -#endif - default: - JSON_ASSERT_UNREACHABLE; - } - if ( other.comments_ ) - { - comments_ = new CommentInfo[numberOfCommentPlacement]; - for ( int comment =0; comment < numberOfCommentPlacement; ++comment ) - { - const CommentInfo &otherComment = other.comments_[comment]; - if ( otherComment.comment_ ) - comments_[comment].setComment( otherComment.comment_ ); - } - } +bool Value::CZString::operator==(const CZString& other) const { + if (!cstr_) return index_ == other.index_; + //return strcmp(cstr_, other.cstr_) == 0; + // Assume both are strings. + unsigned this_len = this->storage_.length_; + unsigned other_len = other.storage_.length_; + if (this_len != other_len) return false; + int comp = memcmp(this->cstr_, other.cstr_, this_len); + return comp == 0; } +ArrayIndex Value::CZString::index() const { return index_; } -Value::~Value() -{ - switch ( type_ ) - { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - break; - case stringValue: - if ( allocated_ ) - releaseStringValue( value_.string_ ); - break; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - delete value_.map_; - break; -#else - case arrayValue: - arrayAllocator()->destructArray( value_.array_ ); - break; - case objectValue: - mapAllocator()->destructMap( value_.map_ ); - break; -#endif - default: - JSON_ASSERT_UNREACHABLE; - } +//const char* Value::CZString::c_str() const { return cstr_; } +const char* Value::CZString::data() const { return cstr_; } +unsigned Value::CZString::length() const { return storage_.length_; } +bool Value::CZString::isStaticString() const { return storage_.policy_ == noDuplication; } - if ( comments_ ) - delete[] comments_; -} +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class Value::Value +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// -Value & -Value::operator=( const Value &other ) -{ - Value temp( other ); - swap( temp ); - return *this; +/*! \internal Default constructor initialization must be equivalent to: + * memset( this, 0, sizeof(Value) ) + * This optimization is used in ValueInternalMap fast allocator. + */ +Value::Value(ValueType vtype) { + initBasic(vtype); + switch (vtype) { + case nullValue: + break; + case intValue: + case uintValue: + value_.int_ = 0; + break; + case realValue: + value_.real_ = 0.0; + break; + case stringValue: + value_.string_ = 0; + break; + case arrayValue: + case objectValue: + value_.map_ = new ObjectValues(); + break; + case booleanValue: + value_.bool_ = false; + break; + default: + JSON_ASSERT_UNREACHABLE; + } +} + +Value::Value(Int value) { + initBasic(intValue); + value_.int_ = value; +} + +Value::Value(UInt value) { + initBasic(uintValue); + value_.uint_ = value; } - -void -Value::swap( Value &other ) -{ - ValueType temp = type_; - type_ = other.type_; - other.type_ = temp; - std::swap( value_, other.value_ ); - int temp2 = allocated_; - allocated_ = other.allocated_; - other.allocated_ = temp2; +#if defined(JSON_HAS_INT64) +Value::Value(Int64 value) { + initBasic(intValue); + value_.int_ = value; } - -ValueType -Value::type() const -{ - return type_; +Value::Value(UInt64 value) { + initBasic(uintValue); + value_.uint_ = value; } +#endif // defined(JSON_HAS_INT64) - -int -Value::compare( const Value &other ) const -{ - if ( *this < other ) - return -1; - if ( *this > other ) - return 1; - return 0; +Value::Value(double value) { + initBasic(realValue); + value_.real_ = value; } - -bool -Value::operator <( const Value &other ) const -{ - int typeDelta = type_ - other.type_; - if ( typeDelta ) - return typeDelta < 0 ? true : false; - switch ( type_ ) - { - case nullValue: - return false; - case intValue: - return value_.int_ < other.value_.int_; - case uintValue: - return value_.uint_ < other.value_.uint_; - case realValue: - return value_.real_ < other.value_.real_; - case booleanValue: - return value_.bool_ < other.value_.bool_; - case stringValue: - return ( value_.string_ == 0 && other.value_.string_ ) - || ( other.value_.string_ - && value_.string_ - && strcmp( value_.string_, other.value_.string_ ) < 0 ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - { - int delta = int( value_.map_->size() - other.value_.map_->size() ); - if ( delta ) - return delta < 0; - return (*value_.map_) < (*other.value_.map_); - } -#else - case arrayValue: - return value_.array_->compare( *(other.value_.array_) ) < 0; - case objectValue: - return value_.map_->compare( *(other.value_.map_) ) < 0; -#endif - default: - JSON_ASSERT_UNREACHABLE; - } - return false; // unreachable +Value::Value(const char* value) { + initBasic(stringValue, true); + value_.string_ = duplicateAndPrefixStringValue(value, static_cast(strlen(value))); } -bool -Value::operator <=( const Value &other ) const -{ - return !(other < *this); +Value::Value(const char* beginValue, const char* endValue) { + initBasic(stringValue, true); + value_.string_ = + duplicateAndPrefixStringValue(beginValue, static_cast(endValue - beginValue)); } -bool -Value::operator >=( const Value &other ) const -{ - return !(*this < other); +Value::Value(const std::string& value) { + initBasic(stringValue, true); + value_.string_ = + duplicateAndPrefixStringValue(value.data(), static_cast(value.length())); } -bool -Value::operator >( const Value &other ) const -{ - return other < *this; +Value::Value(const StaticString& value) { + initBasic(stringValue); + value_.string_ = const_cast(value.c_str()); } -bool -Value::operator ==( const Value &other ) const -{ - //if ( type_ != other.type_ ) - // GCC 2.95.3 says: - // attempt to take address of bit-field structure member `Json::Value::type_' - // Beats me, but a temp solves the problem. - int temp = other.type_; - if ( type_ != temp ) - return false; - switch ( type_ ) - { - case nullValue: - return true; - case intValue: - return value_.int_ == other.value_.int_; - case uintValue: - return value_.uint_ == other.value_.uint_; - case realValue: - return value_.real_ == other.value_.real_; - case booleanValue: - return value_.bool_ == other.value_.bool_; - case stringValue: - return ( value_.string_ == other.value_.string_ ) - || ( other.value_.string_ - && value_.string_ - && strcmp( value_.string_, other.value_.string_ ) == 0 ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - return value_.map_->size() == other.value_.map_->size() - && (*value_.map_) == (*other.value_.map_); -#else - case arrayValue: - return value_.array_->compare( *(other.value_.array_) ) == 0; - case objectValue: - return value_.map_->compare( *(other.value_.map_) ) == 0; +#ifdef JSON_USE_CPPTL +Value::Value(const CppTL::ConstString& value) { + initBasic(stringValue, true); + value_.string_ = duplicateAndPrefixStringValue(value, static_cast(value.length())); +} #endif - default: - JSON_ASSERT_UNREACHABLE; - } - return false; // unreachable + +Value::Value(bool value) { + initBasic(booleanValue); + value_.bool_ = value; +} + +Value::Value(Value const& other) + : type_(other.type_), allocated_(false) + , + comments_(0) +{ + switch (type_) { + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + value_ = other.value_; + break; + case stringValue: + if (other.value_.string_ && other.allocated_) { + unsigned len; + char const* str; + decodePrefixedString(other.allocated_, other.value_.string_, + &len, &str); + value_.string_ = duplicateAndPrefixStringValue(str, len); + allocated_ = true; + } else { + value_.string_ = other.value_.string_; + allocated_ = false; + } + break; + case arrayValue: + case objectValue: + value_.map_ = new ObjectValues(*other.value_.map_); + break; + default: + JSON_ASSERT_UNREACHABLE; + } + if (other.comments_) { + comments_ = new CommentInfo[numberOfCommentPlacement]; + for (int comment = 0; comment < numberOfCommentPlacement; ++comment) { + const CommentInfo& otherComment = other.comments_[comment]; + if (otherComment.comment_) + comments_[comment].setComment( + otherComment.comment_, strlen(otherComment.comment_)); + } + } +} + +Value::~Value() { + switch (type_) { + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + break; + case stringValue: + if (allocated_) + releaseStringValue(value_.string_); + break; + case arrayValue: + case objectValue: + delete value_.map_; + break; + default: + JSON_ASSERT_UNREACHABLE; + } + + if (comments_) + delete[] comments_; +} + +Value &Value::operator=(const Value &other) { + Value temp(other); + swap(temp); + return *this; +} + +void Value::swapPayload(Value& other) { + ValueType temp = type_; + type_ = other.type_; + other.type_ = temp; + std::swap(value_, other.value_); + int temp2 = allocated_; + allocated_ = other.allocated_; + other.allocated_ = temp2 & 0x1; +} + +void Value::swap(Value& other) { + swapPayload(other); + std::swap(comments_, other.comments_); +} + +ValueType Value::type() const { return type_; } + +int Value::compare(const Value& other) const { + if (*this < other) + return -1; + if (*this > other) + return 1; + return 0; +} + +bool Value::operator<(const Value& other) const { + int typeDelta = type_ - other.type_; + if (typeDelta) + return typeDelta < 0 ? true : false; + switch (type_) { + case nullValue: + return false; + case intValue: + return value_.int_ < other.value_.int_; + case uintValue: + return value_.uint_ < other.value_.uint_; + case realValue: + return value_.real_ < other.value_.real_; + case booleanValue: + return value_.bool_ < other.value_.bool_; + case stringValue: + { + if ((value_.string_ == 0) || (other.value_.string_ == 0)) { + if (other.value_.string_) return true; + else return false; + } + unsigned this_len; + unsigned other_len; + char const* this_str; + char const* other_str; + decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str); + decodePrefixedString(other.allocated_, other.value_.string_, &other_len, &other_str); + unsigned min_len = std::min(this_len, other_len); + int comp = memcmp(this_str, other_str, min_len); + if (comp < 0) return true; + if (comp > 0) return false; + return (this_len < other_len); + } + case arrayValue: + case objectValue: { + int delta = int(value_.map_->size() - other.value_.map_->size()); + if (delta) + return delta < 0; + return (*value_.map_) < (*other.value_.map_); + } + default: + JSON_ASSERT_UNREACHABLE; + } + return false; // unreachable +} + +bool Value::operator<=(const Value& other) const { return !(other < *this); } + +bool Value::operator>=(const Value& other) const { return !(*this < other); } + +bool Value::operator>(const Value& other) const { return other < *this; } + +bool Value::operator==(const Value& other) const { + // if ( type_ != other.type_ ) + // GCC 2.95.3 says: + // attempt to take address of bit-field structure member `Json::Value::type_' + // Beats me, but a temp solves the problem. + int temp = other.type_; + if (type_ != temp) + return false; + switch (type_) { + case nullValue: + return true; + case intValue: + return value_.int_ == other.value_.int_; + case uintValue: + return value_.uint_ == other.value_.uint_; + case realValue: + return value_.real_ == other.value_.real_; + case booleanValue: + return value_.bool_ == other.value_.bool_; + case stringValue: + { + if ((value_.string_ == 0) || (other.value_.string_ == 0)) { + return (value_.string_ == other.value_.string_); + } + unsigned this_len; + unsigned other_len; + char const* this_str; + char const* other_str; + decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str); + decodePrefixedString(other.allocated_, other.value_.string_, &other_len, &other_str); + if (this_len != other_len) return false; + int comp = memcmp(this_str, other_str, this_len); + return comp == 0; + } + case arrayValue: + case objectValue: + return value_.map_->size() == other.value_.map_->size() && + (*value_.map_) == (*other.value_.map_); + default: + JSON_ASSERT_UNREACHABLE; + } + return false; // unreachable +} + +bool Value::operator!=(const Value& other) const { return !(*this == other); } + +const char* Value::asCString() const { + JSON_ASSERT_MESSAGE(type_ == stringValue, + "in Json::Value::asCString(): requires stringValue"); + if (value_.string_ == 0) return 0; + unsigned this_len; + char const* this_str; + decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str); + return this_str; +} + +bool Value::getString(char const** str, char const** cend) const { + if (type_ != stringValue) return false; + if (value_.string_ == 0) return false; + unsigned length; + decodePrefixedString(this->allocated_, this->value_.string_, &length, str); + *cend = *str + length; + return true; +} + +std::string Value::asString() const { + switch (type_) { + case nullValue: + return ""; + case stringValue: + { + if (value_.string_ == 0) return ""; + unsigned this_len; + char const* this_str; + decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str); + return std::string(this_str, this_len); + } + case booleanValue: + return value_.bool_ ? "true" : "false"; + case intValue: + return valueToString(value_.int_); + case uintValue: + return valueToString(value_.uint_); + case realValue: + return valueToString(value_.real_); + default: + JSON_FAIL_MESSAGE("Type is not convertible to string"); + } } -bool -Value::operator !=( const Value &other ) const -{ - return !( *this == other ); +#ifdef JSON_USE_CPPTL +CppTL::ConstString Value::asConstString() const { + unsigned len; + char const* str; + decodePrefixedString(allocated_, value_.string_, + &len, &str); + return CppTL::ConstString(str, len); } +#endif -const char * -Value::asCString() const -{ - JSON_ASSERT( type_ == stringValue ); - return value_.string_; +Value::Int Value::asInt() const { + switch (type_) { + case intValue: + JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range"); + return Int(value_.int_); + case uintValue: + JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range"); + return Int(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt), + "double out of Int range"); + return Int(value_.real_); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to Int."); +} + +Value::UInt Value::asUInt() const { + switch (type_) { + case intValue: + JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range"); + return UInt(value_.int_); + case uintValue: + JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range"); + return UInt(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt), + "double out of UInt range"); + return UInt(value_.real_); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to UInt."); } +#if defined(JSON_HAS_INT64) -std::string -Value::asString() const -{ - switch ( type_ ) - { - case nullValue: - return ""; - case stringValue: - return value_.string_ ? value_.string_ : ""; - case booleanValue: - return value_.bool_ ? "true" : "false"; - case intValue: - case uintValue: - case realValue: - case arrayValue: - case objectValue: - JSON_FAIL_MESSAGE( "Type is not convertible to string" ); - default: - JSON_ASSERT_UNREACHABLE; - } - return ""; // unreachable -} - -# ifdef JSON_USE_CPPTL -CppTL::ConstString -Value::asConstString() const -{ - return CppTL::ConstString( asString().c_str() ); +Value::Int64 Value::asInt64() const { + switch (type_) { + case intValue: + return Int64(value_.int_); + case uintValue: + JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range"); + return Int64(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt64, maxInt64), + "double out of Int64 range"); + return Int64(value_.real_); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to Int64."); +} + +Value::UInt64 Value::asUInt64() const { + switch (type_) { + case intValue: + JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range"); + return UInt64(value_.int_); + case uintValue: + return UInt64(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64), + "double out of UInt64 range"); + return UInt64(value_.real_); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to UInt64."); } -# endif - +#endif // if defined(JSON_HAS_INT64) -Value::Int -Value::asInt() const -{ - switch ( type_ ) - { - case nullValue: - return 0; - case intValue: - JSON_ASSERT_MESSAGE( value_.int_ >= minInt && value_.int_ <= maxInt, "unsigned integer out of signed int range" ); - return Int(value_.int_); - case uintValue: - JSON_ASSERT_MESSAGE( value_.uint_ <= UInt(maxInt), "unsigned integer out of signed int range" ); - return Int(value_.uint_); - case realValue: - JSON_ASSERT_MESSAGE( value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range" ); - return Int( value_.real_ ); - case booleanValue: - return value_.bool_ ? 1 : 0; - case stringValue: - case arrayValue: - case objectValue: - JSON_FAIL_MESSAGE( "Type is not convertible to int" ); - default: - JSON_ASSERT_UNREACHABLE; - } - return 0; // unreachable; -} - - -Value::UInt -Value::asUInt() const -{ - switch ( type_ ) - { - case nullValue: - return 0; - case intValue: - JSON_ASSERT_MESSAGE( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" ); - JSON_ASSERT_MESSAGE( value_.int_ <= maxUInt, "signed integer out of UInt range" ); - return UInt(value_.int_); - case uintValue: - JSON_ASSERT_MESSAGE( value_.uint_ <= maxUInt, "unsigned integer out of UInt range" ); - return UInt(value_.uint_); - case realValue: - JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt, "Real out of unsigned integer range" ); - return UInt( value_.real_ ); - case booleanValue: - return value_.bool_ ? 1 : 0; - case stringValue: - case arrayValue: - case objectValue: - JSON_FAIL_MESSAGE( "Type is not convertible to uint" ); - default: - JSON_ASSERT_UNREACHABLE; - } - return 0; // unreachable; -} - - -# if defined(JSON_HAS_INT64) - -Value::Int64 -Value::asInt64() const -{ - switch ( type_ ) - { - case nullValue: - return 0; - case intValue: - return value_.int_; - case uintValue: - JSON_ASSERT_MESSAGE( value_.uint_ <= UInt64(maxInt64), "unsigned integer out of Int64 range" ); - return value_.uint_; - case realValue: - JSON_ASSERT_MESSAGE( value_.real_ >= minInt64 && value_.real_ <= maxInt64, "Real out of Int64 range" ); - return Int( value_.real_ ); - case booleanValue: - return value_.bool_ ? 1 : 0; - case stringValue: - case arrayValue: - case objectValue: - JSON_FAIL_MESSAGE( "Type is not convertible to Int64" ); - default: - JSON_ASSERT_UNREACHABLE; - } - return 0; // unreachable; -} - - -Value::UInt64 -Value::asUInt64() const -{ - switch ( type_ ) - { - case nullValue: - return 0; - case intValue: - JSON_ASSERT_MESSAGE( value_.int_ >= 0, "Negative integer can not be converted to UInt64" ); - return value_.int_; - case uintValue: - return value_.uint_; - case realValue: - JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt64, "Real out of UInt64 range" ); - return UInt( value_.real_ ); - case booleanValue: - return value_.bool_ ? 1 : 0; - case stringValue: - case arrayValue: - case objectValue: - JSON_FAIL_MESSAGE( "Type is not convertible to UInt64" ); - default: - JSON_ASSERT_UNREACHABLE; - } - return 0; // unreachable; -} -# endif // if defined(JSON_HAS_INT64) - - -LargestInt -Value::asLargestInt() const -{ +LargestInt Value::asLargestInt() const { #if defined(JSON_NO_INT64) - return asInt(); + return asInt(); #else - return asInt64(); + return asInt64(); #endif } - -LargestUInt -Value::asLargestUInt() const -{ +LargestUInt Value::asLargestUInt() const { #if defined(JSON_NO_INT64) - return asUInt(); + return asUInt(); #else - return asUInt64(); + return asUInt64(); #endif } - -double -Value::asDouble() const -{ - switch ( type_ ) - { - case nullValue: - return 0.0; - case intValue: - return static_cast( value_.int_ ); - case uintValue: +double Value::asDouble() const { + switch (type_) { + case intValue: + return static_cast(value_.int_); + case uintValue: #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return static_cast( value_.uint_ ); -#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return static_cast( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1); + return static_cast(value_.uint_); +#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return integerToDouble(value_.uint_); #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - case realValue: - return value_.real_; - case booleanValue: - return value_.bool_ ? 1.0 : 0.0; - case stringValue: - case arrayValue: - case objectValue: - JSON_FAIL_MESSAGE( "Type is not convertible to double" ); - default: - JSON_ASSERT_UNREACHABLE; - } - return 0; // unreachable; -} - -float -Value::asFloat() const -{ - switch ( type_ ) - { - case nullValue: - return 0.0f; - case intValue: - return static_cast( value_.int_ ); - case uintValue: + case realValue: + return value_.real_; + case nullValue: + return 0.0; + case booleanValue: + return value_.bool_ ? 1.0 : 0.0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to double."); +} + +float Value::asFloat() const { + switch (type_) { + case intValue: + return static_cast(value_.int_); + case uintValue: #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return static_cast( value_.uint_ ); -#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return static_cast( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1); + return static_cast(value_.uint_); +#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return integerToDouble(value_.uint_); #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - case realValue: - return static_cast( value_.real_ ); - case booleanValue: - return value_.bool_ ? 1.0f : 0.0f; - case stringValue: - case arrayValue: - case objectValue: - JSON_FAIL_MESSAGE( "Type is not convertible to float" ); - default: - JSON_ASSERT_UNREACHABLE; - } - return 0.0f; // unreachable; -} - -bool -Value::asBool() const -{ - switch ( type_ ) - { - case nullValue: - return false; - case intValue: - case uintValue: - return value_.int_ != 0; - case realValue: - return value_.real_ != 0.0; - case booleanValue: - return value_.bool_; - case stringValue: - return value_.string_ && value_.string_[0] != 0; - case arrayValue: - case objectValue: - return value_.map_->size() != 0; - default: - JSON_ASSERT_UNREACHABLE; - } - return false; // unreachable; -} - - -bool -Value::isConvertibleTo( ValueType other ) const -{ - switch ( type_ ) - { - case nullValue: - return true; - case intValue: - return ( other == nullValue && value_.int_ == 0 ) - || other == intValue - || ( other == uintValue && value_.int_ >= 0 ) - || other == realValue - || other == stringValue - || other == booleanValue; - case uintValue: - return ( other == nullValue && value_.uint_ == 0 ) - || ( other == intValue && value_.uint_ <= (unsigned)maxInt ) - || other == uintValue - || other == realValue - || other == stringValue - || other == booleanValue; - case realValue: - return ( other == nullValue && value_.real_ == 0.0 ) - || ( other == intValue && value_.real_ >= minInt && value_.real_ <= maxInt ) - || ( other == uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt ) - || other == realValue - || other == stringValue - || other == booleanValue; - case booleanValue: - return ( other == nullValue && value_.bool_ == false ) - || other == intValue - || other == uintValue - || other == realValue - || other == stringValue - || other == booleanValue; - case stringValue: - return other == stringValue - || ( other == nullValue && (!value_.string_ || value_.string_[0] == 0) ); - case arrayValue: - return other == arrayValue - || ( other == nullValue && value_.map_->size() == 0 ); - case objectValue: - return other == objectValue - || ( other == nullValue && value_.map_->size() == 0 ); - default: - JSON_ASSERT_UNREACHABLE; - } - return false; // unreachable; + case realValue: + return static_cast(value_.real_); + case nullValue: + return 0.0; + case booleanValue: + return value_.bool_ ? 1.0f : 0.0f; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to float."); +} + +bool Value::asBool() const { + switch (type_) { + case booleanValue: + return value_.bool_; + case nullValue: + return false; + case intValue: + return value_.int_ ? true : false; + case uintValue: + return value_.uint_ ? true : false; + case realValue: + // This is kind of strange. Not recommended. + return (value_.real_ != 0.0) ? true : false; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to bool."); +} + +bool Value::isConvertibleTo(ValueType other) const { + switch (other) { + case nullValue: + return (isNumeric() && asDouble() == 0.0) || + (type_ == booleanValue && value_.bool_ == false) || + (type_ == stringValue && asString() == "") || + (type_ == arrayValue && value_.map_->size() == 0) || + (type_ == objectValue && value_.map_->size() == 0) || + type_ == nullValue; + case intValue: + return isInt() || + (type_ == realValue && InRange(value_.real_, minInt, maxInt)) || + type_ == booleanValue || type_ == nullValue; + case uintValue: + return isUInt() || + (type_ == realValue && InRange(value_.real_, 0, maxUInt)) || + type_ == booleanValue || type_ == nullValue; + case realValue: + return isNumeric() || type_ == booleanValue || type_ == nullValue; + case booleanValue: + return isNumeric() || type_ == booleanValue || type_ == nullValue; + case stringValue: + return isNumeric() || type_ == booleanValue || type_ == stringValue || + type_ == nullValue; + case arrayValue: + return type_ == arrayValue || type_ == nullValue; + case objectValue: + return type_ == objectValue || type_ == nullValue; + } + JSON_ASSERT_UNREACHABLE; + return false; } - /// Number of values in array or object -ArrayIndex -Value::size() const -{ - switch ( type_ ) - { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - case stringValue: - return 0; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: // size of the array is highest index + 1 - if ( !value_.map_->empty() ) - { - ObjectValues::const_iterator itLast = value_.map_->end(); - --itLast; - return (*itLast).first.index()+1; - } - return 0; - case objectValue: - return ArrayIndex( value_.map_->size() ); -#else - case arrayValue: - return Int( value_.array_->size() ); - case objectValue: - return Int( value_.map_->size() ); -#endif - default: - JSON_ASSERT_UNREACHABLE; - } - return 0; // unreachable; +ArrayIndex Value::size() const { + switch (type_) { + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + case stringValue: + return 0; + case arrayValue: // size of the array is highest index + 1 + if (!value_.map_->empty()) { + ObjectValues::const_iterator itLast = value_.map_->end(); + --itLast; + return (*itLast).first.index() + 1; + } + return 0; + case objectValue: + return ArrayIndex(value_.map_->size()); + } + JSON_ASSERT_UNREACHABLE; + return 0; // unreachable; +} + +bool Value::empty() const { + if (isNull() || isArray() || isObject()) + return size() == 0u; + else + return false; +} + +bool Value::operator!() const { return isNull(); } + +void Value::clear() { + JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue || + type_ == objectValue, + "in Json::Value::clear(): requires complex value"); + switch (type_) { + case arrayValue: + case objectValue: + value_.map_->clear(); + break; + default: + break; + } +} + +void Value::resize(ArrayIndex newSize) { + JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue, + "in Json::Value::resize(): requires arrayValue"); + if (type_ == nullValue) + *this = Value(arrayValue); + ArrayIndex oldSize = size(); + if (newSize == 0) + clear(); + else if (newSize > oldSize) + (*this)[newSize - 1]; + else { + for (ArrayIndex index = newSize; index < oldSize; ++index) { + value_.map_->erase(index); + } + assert(size() == newSize); + } +} + +Value& Value::operator[](ArrayIndex index) { + JSON_ASSERT_MESSAGE( + type_ == nullValue || type_ == arrayValue, + "in Json::Value::operator[](ArrayIndex): requires arrayValue"); + if (type_ == nullValue) + *this = Value(arrayValue); + CZString key(index); + ObjectValues::iterator it = value_.map_->lower_bound(key); + if (it != value_.map_->end() && (*it).first == key) + return (*it).second; + + ObjectValues::value_type defaultValue(key, nullRef); + it = value_.map_->insert(it, defaultValue); + return (*it).second; +} + +Value& Value::operator[](int index) { + JSON_ASSERT_MESSAGE( + index >= 0, + "in Json::Value::operator[](int index): index cannot be negative"); + return (*this)[ArrayIndex(index)]; +} + +const Value& Value::operator[](ArrayIndex index) const { + JSON_ASSERT_MESSAGE( + type_ == nullValue || type_ == arrayValue, + "in Json::Value::operator[](ArrayIndex)const: requires arrayValue"); + if (type_ == nullValue) + return nullRef; + CZString key(index); + ObjectValues::const_iterator it = value_.map_->find(key); + if (it == value_.map_->end()) + return nullRef; + return (*it).second; +} + +const Value& Value::operator[](int index) const { + JSON_ASSERT_MESSAGE( + index >= 0, + "in Json::Value::operator[](int index) const: index cannot be negative"); + return (*this)[ArrayIndex(index)]; +} + +void Value::initBasic(ValueType vtype, bool allocated) { + type_ = vtype; + allocated_ = allocated; + comments_ = 0; +} + +// Access an object value by name, create a null member if it does not exist. +// @pre Type of '*this' is object or null. +// @param key is null-terminated. +Value& Value::resolveReference(const char* key) { + JSON_ASSERT_MESSAGE( + type_ == nullValue || type_ == objectValue, + "in Json::Value::resolveReference(): requires objectValue"); + if (type_ == nullValue) + *this = Value(objectValue); + CZString actualKey( + key, static_cast(strlen(key)), CZString::noDuplication); // NOTE! + ObjectValues::iterator it = value_.map_->lower_bound(actualKey); + if (it != value_.map_->end() && (*it).first == actualKey) + return (*it).second; + + ObjectValues::value_type defaultValue(actualKey, nullRef); + it = value_.map_->insert(it, defaultValue); + Value& value = (*it).second; + return value; +} + +// @param key is not null-terminated. +Value& Value::resolveReference(char const* key, char const* cend) +{ + JSON_ASSERT_MESSAGE( + type_ == nullValue || type_ == objectValue, + "in Json::Value::resolveReference(key, end): requires objectValue"); + if (type_ == nullValue) + *this = Value(objectValue); + CZString actualKey( + key, static_cast(cend-key), CZString::duplicateOnCopy); + ObjectValues::iterator it = value_.map_->lower_bound(actualKey); + if (it != value_.map_->end() && (*it).first == actualKey) + return (*it).second; + + ObjectValues::value_type defaultValue(actualKey, nullRef); + it = value_.map_->insert(it, defaultValue); + Value& value = (*it).second; + return value; } - -bool -Value::empty() const -{ - if ( isNull() || isArray() || isObject() ) - return size() == 0u; - else - return false; +Value Value::get(ArrayIndex index, const Value& defaultValue) const { + const Value* value = &((*this)[index]); + return value == &nullRef ? defaultValue : *value; } +bool Value::isValidIndex(ArrayIndex index) const { return index < size(); } -bool -Value::operator!() const +Value const* Value::find(char const* key, char const* cend) const { - return isNull(); + JSON_ASSERT_MESSAGE( + type_ == nullValue || type_ == objectValue, + "in Json::Value::find(key, end, found): requires objectValue or nullValue"); + if (type_ == nullValue) return NULL; + CZString actualKey(key, static_cast(cend-key), CZString::noDuplication); + ObjectValues::const_iterator it = value_.map_->find(actualKey); + if (it == value_.map_->end()) return NULL; + return &(*it).second; } - - -void -Value::clear() +const Value& Value::operator[](const char* key) const { - JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue ); - - switch ( type_ ) - { -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - value_.map_->clear(); - break; -#else - case arrayValue: - value_.array_->clear(); - break; - case objectValue: - value_.map_->clear(); - break; -#endif - default: - break; - } + Value const* found = find(key, key + strlen(key)); + if (!found) return nullRef; + return *found; } - -void -Value::resize( ArrayIndex newSize ) +Value const& Value::operator[](std::string const& key) const { - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); - if ( type_ == nullValue ) - *this = Value( arrayValue ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - ArrayIndex oldSize = size(); - if ( newSize == 0 ) - clear(); - else if ( newSize > oldSize ) - (*this)[ newSize - 1 ]; - else - { - for ( ArrayIndex index = newSize; index < oldSize; ++index ) - { - value_.map_->erase( index ); - } - assert( size() == newSize ); - } -#else - value_.array_->resize( newSize ); -#endif + Value const* found = find(key.data(), key.data() + key.length()); + if (!found) return nullRef; + return *found; } - -Value & -Value::operator[]( ArrayIndex index ) -{ - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); - if ( type_ == nullValue ) - *this = Value( arrayValue ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString key( index ); - ObjectValues::iterator it = value_.map_->lower_bound( key ); - if ( it != value_.map_->end() && (*it).first == key ) - return (*it).second; - - ObjectValues::value_type defaultValue( key, null ); - it = value_.map_->insert( it, defaultValue ); - return (*it).second; -#else - return value_.array_->resolveReference( index ); -#endif +Value& Value::operator[](const char* key) { + return resolveReference(key, key + strlen(key)); } - -Value & -Value::operator[]( int index ) -{ - JSON_ASSERT( index >= 0 ); - return (*this)[ ArrayIndex(index) ]; +Value& Value::operator[](const std::string& key) { + return resolveReference(key.data(), key.data() + key.length()); } - -const Value & -Value::operator[]( ArrayIndex index ) const -{ - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); - if ( type_ == nullValue ) - return null; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString key( index ); - ObjectValues::const_iterator it = value_.map_->find( key ); - if ( it == value_.map_->end() ) - return null; - return (*it).second; -#else - Value *value = value_.array_->find( index ); - return value ? *value : null; -#endif +Value& Value::operator[](const StaticString& key) { + return resolveReference(key.c_str()); } - -const Value & -Value::operator[]( int index ) const -{ - JSON_ASSERT( index >= 0 ); - return (*this)[ ArrayIndex(index) ]; +#ifdef JSON_USE_CPPTL +Value& Value::operator[](const CppTL::ConstString& key) { + return resolveReference(key.c_str(), key.end_c_str()); } - - -Value & -Value::operator[]( const char *key ) +Value const& Value::operator[](CppTL::ConstString const& key) const { - return resolveReference( key, false ); + Value const* found = find(key.c_str(), key.end_c_str()); + if (!found) return nullRef; + return *found; } - - -Value & -Value::resolveReference( const char *key, - bool isStatic ) -{ - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - *this = Value( objectValue ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString actualKey( key, isStatic ? CZString::noDuplication - : CZString::duplicateOnCopy ); - ObjectValues::iterator it = value_.map_->lower_bound( actualKey ); - if ( it != value_.map_->end() && (*it).first == actualKey ) - return (*it).second; - - ObjectValues::value_type defaultValue( actualKey, null ); - it = value_.map_->insert( it, defaultValue ); - Value &value = (*it).second; - return value; -#else - return value_.map_->resolveReference( key, isStatic ); #endif -} +Value& Value::append(const Value& value) { return (*this)[size()] = value; } -Value -Value::get( ArrayIndex index, - const Value &defaultValue ) const +Value Value::get(char const* key, char const* cend, Value const& defaultValue) const { - const Value *value = &((*this)[index]); - return value == &null ? defaultValue : *value; + Value const* found = find(key, cend); + return !found ? defaultValue : *found; } - - -bool -Value::isValidIndex( ArrayIndex index ) const +Value Value::get(char const* key, Value const& defaultValue) const { - return index < size(); + return get(key, key + strlen(key), defaultValue); } - - - -const Value & -Value::operator[]( const char *key ) const +Value Value::get(std::string const& key, Value const& defaultValue) const { - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - return null; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString actualKey( key, CZString::noDuplication ); - ObjectValues::const_iterator it = value_.map_->find( actualKey ); - if ( it == value_.map_->end() ) - return null; - return (*it).second; -#else - const Value *value = value_.map_->find( key ); - return value ? *value : null; -#endif + return get(key.data(), key.data() + key.length(), defaultValue); } -Value & -Value::operator[]( const std::string &key ) +bool Value::removeMember(const char* key, const char* cend, Value* removed) { - return (*this)[ key.c_str() ]; + if (type_ != objectValue) { + return false; + } + CZString actualKey(key, static_cast(cend-key), CZString::noDuplication); + ObjectValues::iterator it = value_.map_->find(actualKey); + if (it == value_.map_->end()) + return false; + *removed = it->second; + value_.map_->erase(it); + return true; } - - -const Value & -Value::operator[]( const std::string &key ) const +bool Value::removeMember(const char* key, Value* removed) { - return (*this)[ key.c_str() ]; + return removeMember(key, key + strlen(key), removed); } - -Value & -Value::operator[]( const StaticString &key ) +bool Value::removeMember(std::string const& key, Value* removed) { - return resolveReference( key, true ); + return removeMember(key.data(), key.data() + key.length(), removed); } - - -# ifdef JSON_USE_CPPTL -Value & -Value::operator[]( const CppTL::ConstString &key ) +Value Value::removeMember(const char* key) { - return (*this)[ key.c_str() ]; -} + JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == objectValue, + "in Json::Value::removeMember(): requires objectValue"); + if (type_ == nullValue) + return nullRef; - -const Value & -Value::operator[]( const CppTL::ConstString &key ) const -{ - return (*this)[ key.c_str() ]; + Value removed; // null + removeMember(key, key + strlen(key), &removed); + return removed; // still null if removeMember() did nothing } -# endif - - -Value & -Value::append( const Value &value ) +Value Value::removeMember(const std::string& key) { - return (*this)[size()] = value; + return removeMember(key.c_str()); } - -Value -Value::get( const char *key, - const Value &defaultValue ) const -{ - const Value *value = &((*this)[key]); - return value == &null ? defaultValue : *value; +bool Value::removeIndex(ArrayIndex index, Value* removed) { + if (type_ != arrayValue) { + return false; + } + CZString key(index); + ObjectValues::iterator it = value_.map_->find(key); + if (it == value_.map_->end()) { + return false; + } + *removed = it->second; + ArrayIndex oldSize = size(); + // shift left all items left, into the place of the "removed" + for (ArrayIndex i = index; i < (oldSize - 1); ++i){ + CZString keey(i); + (*value_.map_)[keey] = (*this)[i + 1]; + } + // erase the last one ("leftover") + CZString keyLast(oldSize - 1); + ObjectValues::iterator itLast = value_.map_->find(keyLast); + value_.map_->erase(itLast); + return true; } - -Value -Value::get( const std::string &key, - const Value &defaultValue ) const -{ - return get( key.c_str(), defaultValue ); +#ifdef JSON_USE_CPPTL +Value Value::get(const CppTL::ConstString& key, + const Value& defaultValue) const { + return get(key.c_str(), key.end_c_str(), defaultValue); } - -Value -Value::removeMember( const char* key ) -{ - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - return null; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString actualKey( key, CZString::noDuplication ); - ObjectValues::iterator it = value_.map_->find( actualKey ); - if ( it == value_.map_->end() ) - return null; - Value old(it->second); - value_.map_->erase(it); - return old; -#else - Value *value = value_.map_->find( key ); - if (value){ - Value old(*value); - value_.map_.remove( key ); - return old; - } else { - return null; - } #endif -} - -Value -Value::removeMember( const std::string &key ) -{ - return removeMember( key.c_str() ); -} -# ifdef JSON_USE_CPPTL -Value -Value::get( const CppTL::ConstString &key, - const Value &defaultValue ) const +bool Value::isMember(char const* key, char const* cend) const { - return get( key.c_str(), defaultValue ); + Value const* value = find(key, cend); + return NULL != value; } -# endif - -bool -Value::isMember( const char *key ) const +bool Value::isMember(char const* key) const { - const Value *value = &((*this)[key]); - return value != &null; + return isMember(key, key + strlen(key)); } - - -bool -Value::isMember( const std::string &key ) const +bool Value::isMember(std::string const& key) const { - return isMember( key.c_str() ); + return isMember(key.data(), key.data() + key.length()); } - -# ifdef JSON_USE_CPPTL -bool -Value::isMember( const CppTL::ConstString &key ) const -{ - return isMember( key.c_str() ); +#ifdef JSON_USE_CPPTL +bool Value::isMember(const CppTL::ConstString& key) const { + return isMember(key.c_str(), key.end_c_str()); } #endif -Value::Members -Value::getMemberNames() const -{ - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - return Value::Members(); - Members members; - members.reserve( value_.map_->size() ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - ObjectValues::const_iterator it = value_.map_->begin(); - ObjectValues::const_iterator itEnd = value_.map_->end(); - for ( ; it != itEnd; ++it ) - members.push_back( std::string( (*it).first.c_str() ) ); -#else - ValueInternalMap::IteratorState it; - ValueInternalMap::IteratorState itEnd; - value_.map_->makeBeginIterator( it ); - value_.map_->makeEndIterator( itEnd ); - for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) ) - members.push_back( std::string( ValueInternalMap::key( it ) ) ); -#endif - return members; +Value::Members Value::getMemberNames() const { + JSON_ASSERT_MESSAGE( + type_ == nullValue || type_ == objectValue, + "in Json::Value::getMemberNames(), value must be objectValue"); + if (type_ == nullValue) + return Value::Members(); + Members members; + members.reserve(value_.map_->size()); + ObjectValues::const_iterator it = value_.map_->begin(); + ObjectValues::const_iterator itEnd = value_.map_->end(); + for (; it != itEnd; ++it) { + members.push_back(std::string((*it).first.data(), + (*it).first.length())); + } + return members; } // //# ifdef JSON_USE_CPPTL -//EnumMemberNames -//Value::enumMemberNames() const +// EnumMemberNames +// Value::enumMemberNames() const //{ // if ( type_ == objectValue ) // { @@ -2889,482 +3394,340 @@ Value::getMemberNames() const //} // // -//EnumValues -//Value::enumValues() const +// EnumValues +// Value::enumValues() const //{ // if ( type_ == objectValue || type_ == arrayValue ) -// return CppTL::Enum::anyValues( *(value_.map_), -// CppTL::Type() ); -// return EnumValues(); -//} -// -//# endif - - -bool -Value::isNull() const -{ - return type_ == nullValue; -} - - -bool -Value::isBool() const -{ - return type_ == booleanValue; -} - - -bool -Value::isInt() const -{ - return type_ == intValue; -} - - -bool -Value::isUInt() const -{ - return type_ == uintValue; -} - - -bool -Value::isIntegral() const -{ - return type_ == intValue - || type_ == uintValue - || type_ == booleanValue; -} - - -bool -Value::isDouble() const -{ - return type_ == realValue; -} - - -bool -Value::isNumeric() const -{ - return isIntegral() || isDouble(); -} - - -bool -Value::isString() const -{ - return type_ == stringValue; -} - - -bool -Value::isArray() const -{ - return type_ == nullValue || type_ == arrayValue; -} - - -bool -Value::isObject() const -{ - return type_ == nullValue || type_ == objectValue; -} - - -void -Value::setComment( const char *comment, - CommentPlacement placement ) -{ - if ( !comments_ ) - comments_ = new CommentInfo[numberOfCommentPlacement]; - comments_[placement].setComment( comment ); -} - - -void -Value::setComment( const std::string &comment, - CommentPlacement placement ) -{ - setComment( comment.c_str(), placement ); -} - - -bool -Value::hasComment( CommentPlacement placement ) const -{ - return comments_ != 0 && comments_[placement].comment_ != 0; -} - -std::string -Value::getComment( CommentPlacement placement ) const -{ - if ( hasComment(placement) ) - return comments_[placement].comment_; - return ""; -} - - -std::string -Value::toStyledString() const -{ - StyledWriter writer; - return writer.write( *this ); -} - - -Value::const_iterator -Value::begin() const -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeBeginIterator( it ); - return const_iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeBeginIterator( it ); - return const_iterator( it ); - } - break; -#else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return const_iterator( value_.map_->begin() ); - break; -#endif - default: - break; - } - return const_iterator(); -} +// return CppTL::Enum::anyValues( *(value_.map_), +// CppTL::Type() ); +// return EnumValues(); +//} +// +//# endif -Value::const_iterator -Value::end() const -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeEndIterator( it ); - return const_iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeEndIterator( it ); - return const_iterator( it ); - } - break; +static bool IsIntegral(double d) { + double integral_part; + return modf(d, &integral_part) == 0.0; +} + +bool Value::isNull() const { return type_ == nullValue; } + +bool Value::isBool() const { return type_ == booleanValue; } + +bool Value::isInt() const { + switch (type_) { + case intValue: + return value_.int_ >= minInt && value_.int_ <= maxInt; + case uintValue: + return value_.uint_ <= UInt(maxInt); + case realValue: + return value_.real_ >= minInt && value_.real_ <= maxInt && + IsIntegral(value_.real_); + default: + break; + } + return false; +} + +bool Value::isUInt() const { + switch (type_) { + case intValue: + return value_.int_ >= 0 && LargestUInt(value_.int_) <= LargestUInt(maxUInt); + case uintValue: + return value_.uint_ <= maxUInt; + case realValue: + return value_.real_ >= 0 && value_.real_ <= maxUInt && + IsIntegral(value_.real_); + default: + break; + } + return false; +} + +bool Value::isInt64() const { +#if defined(JSON_HAS_INT64) + switch (type_) { + case intValue: + return true; + case uintValue: + return value_.uint_ <= UInt64(maxInt64); + case realValue: + // Note that maxInt64 (= 2^63 - 1) is not exactly representable as a + // double, so double(maxInt64) will be rounded up to 2^63. Therefore we + // require the value to be strictly less than the limit. + return value_.real_ >= double(minInt64) && + value_.real_ < double(maxInt64) && IsIntegral(value_.real_); + default: + break; + } +#endif // JSON_HAS_INT64 + return false; +} + +bool Value::isUInt64() const { +#if defined(JSON_HAS_INT64) + switch (type_) { + case intValue: + return value_.int_ >= 0; + case uintValue: + return true; + case realValue: + // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a + // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we + // require the value to be strictly less than the limit. + return value_.real_ >= 0 && value_.real_ < maxUInt64AsDouble && + IsIntegral(value_.real_); + default: + break; + } +#endif // JSON_HAS_INT64 + return false; +} + +bool Value::isIntegral() const { +#if defined(JSON_HAS_INT64) + return isInt64() || isUInt64(); #else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return const_iterator( value_.map_->end() ); - break; + return isInt() || isUInt(); #endif - default: - break; - } - return const_iterator(); } +bool Value::isDouble() const { return type_ == realValue || isIntegral(); } -Value::iterator -Value::begin() -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeBeginIterator( it ); - return iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeBeginIterator( it ); - return iterator( it ); - } - break; -#else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return iterator( value_.map_->begin() ); - break; -#endif - default: - break; - } - return iterator(); -} +bool Value::isNumeric() const { return isIntegral() || isDouble(); } -Value::iterator -Value::end() -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeEndIterator( it ); - return iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeEndIterator( it ); - return iterator( it ); - } - break; -#else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return iterator( value_.map_->end() ); - break; -#endif - default: - break; - } - return iterator(); -} +bool Value::isString() const { return type_ == stringValue; } +bool Value::isArray() const { return type_ == arrayValue; } -// class PathArgument -// ////////////////////////////////////////////////////////////////// +bool Value::isObject() const { return type_ == objectValue; } -PathArgument::PathArgument() - : kind_( kindNone ) -{ +void Value::setComment(const char* comment, size_t len, CommentPlacement placement) { + if (!comments_) + comments_ = new CommentInfo[numberOfCommentPlacement]; + if ((len > 0) && (comment[len-1] == '\n')) { + // Always discard trailing newline, to aid indentation. + len -= 1; + } + comments_[placement].setComment(comment, len); } - -PathArgument::PathArgument( ArrayIndex index ) - : index_( index ) - , kind_( kindIndex ) -{ +void Value::setComment(const char* comment, CommentPlacement placement) { + setComment(comment, strlen(comment), placement); } +void Value::setComment(const std::string& comment, CommentPlacement placement) { + setComment(comment.c_str(), comment.length(), placement); +} -PathArgument::PathArgument( const char *key ) - : key_( key ) - , kind_( kindKey ) -{ +bool Value::hasComment(CommentPlacement placement) const { + return comments_ != 0 && comments_[placement].comment_ != 0; } +std::string Value::getComment(CommentPlacement placement) const { + if (hasComment(placement)) + return comments_[placement].comment_; + return ""; +} -PathArgument::PathArgument( const std::string &key ) - : key_( key.c_str() ) - , kind_( kindKey ) -{ +std::string Value::toStyledString() const { + StyledWriter writer; + return writer.write(*this); } -// class Path -// ////////////////////////////////////////////////////////////////// +Value::const_iterator Value::begin() const { + switch (type_) { + case arrayValue: + case objectValue: + if (value_.map_) + return const_iterator(value_.map_->begin()); + break; + default: + break; + } + return const_iterator(); +} -Path::Path( const std::string &path, - const PathArgument &a1, - const PathArgument &a2, - const PathArgument &a3, - const PathArgument &a4, - const PathArgument &a5 ) -{ - InArgs in; - in.push_back( &a1 ); - in.push_back( &a2 ); - in.push_back( &a3 ); - in.push_back( &a4 ); - in.push_back( &a5 ); - makePath( path, in ); +Value::const_iterator Value::end() const { + switch (type_) { + case arrayValue: + case objectValue: + if (value_.map_) + return const_iterator(value_.map_->end()); + break; + default: + break; + } + return const_iterator(); } +Value::iterator Value::begin() { + switch (type_) { + case arrayValue: + case objectValue: + if (value_.map_) + return iterator(value_.map_->begin()); + break; + default: + break; + } + return iterator(); +} -void -Path::makePath( const std::string &path, - const InArgs &in ) -{ - const char *current = path.c_str(); - const char *end = current + path.length(); - InArgs::const_iterator itInArg = in.begin(); - while ( current != end ) - { - if ( *current == '[' ) - { - ++current; - if ( *current == '%' ) - addPathInArg( path, in, itInArg, PathArgument::kindIndex ); - else - { - ArrayIndex index = 0; - for ( ; current != end && *current >= '0' && *current <= '9'; ++current ) - index = index * 10 + ArrayIndex(*current - '0'); - args_.push_back( index ); - } - if ( current == end || *current++ != ']' ) - invalidPath( path, int(current - path.c_str()) ); - } - else if ( *current == '%' ) - { - addPathInArg( path, in, itInArg, PathArgument::kindKey ); - ++current; - } - else if ( *current == '.' ) - { - ++current; - } - else - { - const char *beginName = current; - while ( current != end && !strchr( "[.", *current ) ) - ++current; - args_.push_back( std::string( beginName, current ) ); - } - } +Value::iterator Value::end() { + switch (type_) { + case arrayValue: + case objectValue: + if (value_.map_) + return iterator(value_.map_->end()); + break; + default: + break; + } + return iterator(); } +// class PathArgument +// ////////////////////////////////////////////////////////////////// -void -Path::addPathInArg( const std::string &path, - const InArgs &in, - InArgs::const_iterator &itInArg, - PathArgument::Kind kind ) -{ - if ( itInArg == in.end() ) - { - // Error: missing argument %d - } - else if ( (*itInArg)->kind_ != kind ) - { - // Error: bad argument type - } - else - { - args_.push_back( **itInArg ); - } -} - - -void -Path::invalidPath( const std::string &path, - int location ) -{ - // Error: invalid path. -} +PathArgument::PathArgument() : key_(), index_(), kind_(kindNone) {} +PathArgument::PathArgument(ArrayIndex index) + : key_(), index_(index), kind_(kindIndex) {} -const Value & -Path::resolve( const Value &root ) const -{ - const Value *node = &root; - for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) - { - const PathArgument &arg = *it; - if ( arg.kind_ == PathArgument::kindIndex ) - { - if ( !node->isArray() || node->isValidIndex( arg.index_ ) ) - { - // Error: unable to resolve path (array value expected at position... - } - node = &((*node)[arg.index_]); - } - else if ( arg.kind_ == PathArgument::kindKey ) - { - if ( !node->isObject() ) - { - // Error: unable to resolve path (object value expected at position...) - } - node = &((*node)[arg.key_]); - if ( node == &Value::null ) - { - // Error: unable to resolve path (object has no member named '' at position...) - } - } - } - return *node; -} +PathArgument::PathArgument(const char* key) + : key_(key), index_(), kind_(kindKey) {} +PathArgument::PathArgument(const std::string& key) + : key_(key.c_str()), index_(), kind_(kindKey) {} -Value -Path::resolve( const Value &root, - const Value &defaultValue ) const -{ - const Value *node = &root; - for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) - { - const PathArgument &arg = *it; - if ( arg.kind_ == PathArgument::kindIndex ) - { - if ( !node->isArray() || node->isValidIndex( arg.index_ ) ) - return defaultValue; - node = &((*node)[arg.index_]); +// class Path +// ////////////////////////////////////////////////////////////////// + +Path::Path(const std::string& path, + const PathArgument& a1, + const PathArgument& a2, + const PathArgument& a3, + const PathArgument& a4, + const PathArgument& a5) { + InArgs in; + in.push_back(&a1); + in.push_back(&a2); + in.push_back(&a3); + in.push_back(&a4); + in.push_back(&a5); + makePath(path, in); +} + +void Path::makePath(const std::string& path, const InArgs& in) { + const char* current = path.c_str(); + const char* end = current + path.length(); + InArgs::const_iterator itInArg = in.begin(); + while (current != end) { + if (*current == '[') { + ++current; + if (*current == '%') + addPathInArg(path, in, itInArg, PathArgument::kindIndex); + else { + ArrayIndex index = 0; + for (; current != end && *current >= '0' && *current <= '9'; ++current) + index = index * 10 + ArrayIndex(*current - '0'); + args_.push_back(index); + } + if (current == end || *current++ != ']') + invalidPath(path, int(current - path.c_str())); + } else if (*current == '%') { + addPathInArg(path, in, itInArg, PathArgument::kindKey); + ++current; + } else if (*current == '.') { + ++current; + } else { + const char* beginName = current; + while (current != end && !strchr("[.", *current)) + ++current; + args_.push_back(std::string(beginName, current)); + } + } +} + +void Path::addPathInArg(const std::string& /*path*/, + const InArgs& in, + InArgs::const_iterator& itInArg, + PathArgument::Kind kind) { + if (itInArg == in.end()) { + // Error: missing argument %d + } else if ((*itInArg)->kind_ != kind) { + // Error: bad argument type + } else { + args_.push_back(**itInArg); + } +} + +void Path::invalidPath(const std::string& /*path*/, int /*location*/) { + // Error: invalid path. +} + +const Value& Path::resolve(const Value& root) const { + const Value* node = &root; + for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) { + const PathArgument& arg = *it; + if (arg.kind_ == PathArgument::kindIndex) { + if (!node->isArray() || !node->isValidIndex(arg.index_)) { + // Error: unable to resolve path (array value expected at position... } - else if ( arg.kind_ == PathArgument::kindKey ) - { - if ( !node->isObject() ) - return defaultValue; - node = &((*node)[arg.key_]); - if ( node == &Value::null ) - return defaultValue; + node = &((*node)[arg.index_]); + } else if (arg.kind_ == PathArgument::kindKey) { + if (!node->isObject()) { + // Error: unable to resolve path (object value expected at position...) } - } - return *node; -} - - -Value & -Path::make( Value &root ) const -{ - Value *node = &root; - for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) - { - const PathArgument &arg = *it; - if ( arg.kind_ == PathArgument::kindIndex ) - { - if ( !node->isArray() ) - { - // Error: node is not an array at position ... - } - node = &((*node)[arg.index_]); + node = &((*node)[arg.key_]); + if (node == &Value::nullRef) { + // Error: unable to resolve path (object has no member named '' at + // position...) } - else if ( arg.kind_ == PathArgument::kindKey ) - { - if ( !node->isObject() ) - { - // Error: node is not an object at position... - } - node = &((*node)[arg.key_]); + } + } + return *node; +} + +Value Path::resolve(const Value& root, const Value& defaultValue) const { + const Value* node = &root; + for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) { + const PathArgument& arg = *it; + if (arg.kind_ == PathArgument::kindIndex) { + if (!node->isArray() || !node->isValidIndex(arg.index_)) + return defaultValue; + node = &((*node)[arg.index_]); + } else if (arg.kind_ == PathArgument::kindKey) { + if (!node->isObject()) + return defaultValue; + node = &((*node)[arg.key_]); + if (node == &Value::nullRef) + return defaultValue; + } + } + return *node; +} + +Value& Path::make(Value& root) const { + Value* node = &root; + for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) { + const PathArgument& arg = *it; + if (arg.kind_ == PathArgument::kindIndex) { + if (!node->isArray()) { + // Error: node is not an array at position ... } - } - return *node; + node = &((*node)[arg.index_]); + } else if (arg.kind_ == PathArgument::kindKey) { + if (!node->isObject()) { + // Error: node is not an object at position... + } + node = &((*node)[arg.key_]); + } + } + return *node; } - } // namespace Json // ////////////////////////////////////////////////////////////////////// @@ -3380,842 +3743,1195 @@ Path::make( Value &root ) const // Beginning of content of file: src/lib_json/json_writer.cpp // ////////////////////////////////////////////////////////////////////// -// Copyright 2007-2010 Baptiste Lepilleur +// Copyright 2011 Baptiste Lepilleur // Distributed under MIT license, or public domain if desired and // recognized in your jurisdiction. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE #if !defined(JSON_IS_AMALGAMATION) -# include -# include "json_tool.h" +#include +#include "json_tool.h" #endif // if !defined(JSON_IS_AMALGAMATION) -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include -#if _MSC_VER >= 1400 // VC++ 8.0 -#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. +#if defined(_MSC_VER) && _MSC_VER >= 1200 && _MSC_VER < 1800 // Between VC++ 6.0 and VC++ 11.0 +#include +#define isfinite _finite +#elif defined(__sun) && defined(__SVR4) //Solaris +#include +#define isfinite finite +#else +#include +#define isfinite std::isfinite #endif -namespace Json { - -static bool containsControlCharacter( const char* str ) -{ - while ( *str ) - { - if ( isControlCharacter( *(str++) ) ) - return true; - } - return false; -} - - -std::string valueToString( LargestInt value ) -{ - UIntToStringBuffer buffer; - char *current = buffer + sizeof(buffer); - bool isNegative = value < 0; - if ( isNegative ) - value = -value; - uintToString( LargestUInt(value), current ); - if ( isNegative ) - *--current = '-'; - assert( current >= buffer ); - return current; -} - - -std::string valueToString( LargestUInt value ) -{ - UIntToStringBuffer buffer; - char *current = buffer + sizeof(buffer); - uintToString( value, current ); - assert( current >= buffer ); - return current; -} - -#if defined(JSON_HAS_INT64) - -std::string valueToString( Int value ) -{ - return valueToString( LargestInt(value) ); -} - +#if defined(_MSC_VER) +#if !defined(WINCE) && defined(__STDC_SECURE_LIB__) && _MSC_VER >= 1500 // VC++ 9.0 and above +#define snprintf sprintf_s +#elif _MSC_VER >= 1900 // VC++ 14.0 and above +#define snprintf std::snprintf +#else +#define snprintf _snprintf +#endif +#elif defined(__ANDROID__) +#define snprintf snprintf +#elif __cplusplus >= 201103L +#define snprintf std::snprintf +#endif -std::string valueToString( UInt value ) -{ - return valueToString( LargestUInt(value) ); -} +#if defined(__BORLANDC__) +#include +#define isfinite _finite +#define snprintf _snprintf +#endif -#endif // # if defined(JSON_HAS_INT64) +#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0 +// Disable warning about strdup being deprecated. +#pragma warning(disable : 4996) +#endif +namespace Json { -std::string valueToString( double value ) -{ - char buffer[32]; -#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 to avoid warning. - sprintf_s(buffer, sizeof(buffer), "%#.16g", value); -#else - sprintf(buffer, "%#.16g", value); +#if __cplusplus >= 201103L +typedef std::unique_ptr const StreamWriterPtr; +#else +typedef std::auto_ptr StreamWriterPtr; #endif - char* ch = buffer + strlen(buffer) - 1; - if (*ch != '0') return buffer; // nothing to truncate, so save time - while(ch > buffer && *ch == '0'){ - --ch; - } - char* last_nonzero = ch; - while(ch >= buffer){ - switch(*ch){ - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - --ch; - continue; - case '.': - // Truncate zeroes to save bytes in output, but keep one. - *(last_nonzero+2) = '\0'; - return buffer; - default: - return buffer; - } - } - return buffer; -} - - -std::string valueToString( bool value ) -{ - return value ? "true" : "false"; -} -std::string valueToQuotedString( const char *value ) -{ - // Not sure how to handle unicode... - if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL && !containsControlCharacter( value )) - return std::string("\"") + value + "\""; - // We have to walk value and escape any special characters. - // Appending to std::string is not efficient, but this should be rare. - // (Note: forward slashes are *not* rare, but I am not escaping them.) - std::string::size_type maxsize = strlen(value)*2 + 3; // allescaped+quotes+NULL - std::string result; - result.reserve(maxsize); // to avoid lots of mallocs - result += "\""; - for (const char* c=value; *c != 0; ++c) - { - switch(*c) - { - case '\"': - result += "\\\""; - break; - case '\\': - result += "\\\\"; - break; - case '\b': - result += "\\b"; - break; - case '\f': - result += "\\f"; - break; - case '\n': - result += "\\n"; - break; - case '\r': - result += "\\r"; - break; - case '\t': - result += "\\t"; - break; - //case '/': - // Even though \/ is considered a legal escape in JSON, a bare - // slash is also legal, so I see no reason to escape it. - // (I hope I am not misunderstanding something. - // blep notes: actually escaping \/ may be useful in javascript to avoid (*c); - result += oss.str(); - } - else - { - result += *c; - } - break; - } - } - result += "\""; - return result; -} - -// Class Writer -// ////////////////////////////////////////////////////////////////// -Writer::~Writer() -{ +static bool containsControlCharacter(const char* str) { + while (*str) { + if (isControlCharacter(*(str++))) + return true; + } + return false; } - -// Class FastWriter -// ////////////////////////////////////////////////////////////////// - -FastWriter::FastWriter() - : yamlCompatiblityEnabled_( false ) -{ +static bool containsControlCharacter0(const char* str, unsigned len) { + char const* end = str + len; + while (end != str) { + if (isControlCharacter(*str) || 0==*str) + return true; + ++str; + } + return false; +} + +std::string valueToString(LargestInt value) { + UIntToStringBuffer buffer; + char* current = buffer + sizeof(buffer); + if (value == Value::minLargestInt) { + uintToString(LargestUInt(Value::maxLargestInt) + 1, current); + *--current = '-'; + } else if (value < 0) { + uintToString(LargestUInt(-value), current); + *--current = '-'; + } else { + uintToString(LargestUInt(value), current); + } + assert(current >= buffer); + return current; +} + +std::string valueToString(LargestUInt value) { + UIntToStringBuffer buffer; + char* current = buffer + sizeof(buffer); + uintToString(value, current); + assert(current >= buffer); + return current; } +#if defined(JSON_HAS_INT64) -void -FastWriter::enableYAMLCompatibility() -{ - yamlCompatiblityEnabled_ = true; +std::string valueToString(Int value) { + return valueToString(LargestInt(value)); } - -std::string -FastWriter::write( const Value &root ) -{ - document_ = ""; - writeValue( root ); - document_ += "\n"; - return document_; +std::string valueToString(UInt value) { + return valueToString(LargestUInt(value)); } +#endif // # if defined(JSON_HAS_INT64) -void -FastWriter::writeValue( const Value &value ) -{ - switch ( value.type() ) - { - case nullValue: - document_ += "null"; +std::string valueToString(double value, bool useSpecialFloats, unsigned int precision) { + // Allocate a buffer that is more than large enough to store the 16 digits of + // precision requested below. + char buffer[32]; + int len = -1; + + char formatString[6]; + sprintf(formatString, "%%.%dg", precision); + + // Print into the buffer. We need not request the alternative representation + // that always has a decimal point because JSON doesn't distingish the + // concepts of reals and integers. + if (isfinite(value)) { + len = snprintf(buffer, sizeof(buffer), formatString, value); + } else { + // IEEE standard states that NaN values will not compare to themselves + if (value != value) { + len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "NaN" : "null"); + } else if (value < 0) { + len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "-Infinity" : "-1e+9999"); + } else { + len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "Infinity" : "1e+9999"); + } + // For those, we do not need to call fixNumLoc, but it is fast. + } + assert(len >= 0); + fixNumericLocale(buffer, buffer + len); + return buffer; +} + +std::string valueToString(double value) { return valueToString(value, false, 17); } + +std::string valueToString(bool value) { return value ? "true" : "false"; } + +std::string valueToQuotedString(const char* value) { + if (value == NULL) + return ""; + // Not sure how to handle unicode... + if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL && + !containsControlCharacter(value)) + return std::string("\"") + value + "\""; + // We have to walk value and escape any special characters. + // Appending to std::string is not efficient, but this should be rare. + // (Note: forward slashes are *not* rare, but I am not escaping them.) + std::string::size_type maxsize = + strlen(value) * 2 + 3; // allescaped+quotes+NULL + std::string result; + result.reserve(maxsize); // to avoid lots of mallocs + result += "\""; + for (const char* c = value; *c != 0; ++c) { + switch (*c) { + case '\"': + result += "\\\""; break; - case intValue: - document_ += valueToString( value.asLargestInt() ); + case '\\': + result += "\\\\"; break; - case uintValue: - document_ += valueToString( value.asLargestUInt() ); + case '\b': + result += "\\b"; break; - case realValue: - document_ += valueToString( value.asDouble() ); + case '\f': + result += "\\f"; break; - case stringValue: - document_ += valueToQuotedString( value.asCString() ); + case '\n': + result += "\\n"; break; - case booleanValue: - document_ += valueToString( value.asBool() ); + case '\r': + result += "\\r"; break; - case arrayValue: - { - document_ += "["; - int size = value.size(); - for ( int index =0; index < size; ++index ) - { - if ( index > 0 ) - document_ += ","; - writeValue( value[index] ); - } - document_ += "]"; - } + case '\t': + result += "\\t"; break; - case objectValue: - { - Value::Members members( value.getMemberNames() ); - document_ += "{"; - for ( Value::Members::iterator it = members.begin(); - it != members.end(); - ++it ) - { - const std::string &name = *it; - if ( it != members.begin() ) - document_ += ","; - document_ += valueToQuotedString( name.c_str() ); - document_ += yamlCompatiblityEnabled_ ? ": " - : ":"; - writeValue( value[name] ); - } - document_ += "}"; + // case '/': + // Even though \/ is considered a legal escape in JSON, a bare + // slash is also legal, so I see no reason to escape it. + // (I hope I am not misunderstanding something. + // blep notes: actually escaping \/ may be useful in javascript to avoid (*c); + result += oss.str(); + } else { + result += *c; } break; - } -} - - -// Class StyledWriter -// ////////////////////////////////////////////////////////////////// - -StyledWriter::StyledWriter() - : rightMargin_( 74 ) - , indentSize_( 3 ) -{ -} - - -std::string -StyledWriter::write( const Value &root ) -{ - document_ = ""; - addChildValues_ = false; - indentString_ = ""; - writeCommentBeforeValue( root ); - writeValue( root ); - writeCommentAfterValueOnSameLine( root ); - document_ += "\n"; - return document_; -} - - -void -StyledWriter::writeValue( const Value &value ) -{ - switch ( value.type() ) - { - case nullValue: - pushValue( "null" ); + } + } + result += "\""; + return result; +} + +// https://github.com/upcaste/upcaste/blob/master/src/upcore/src/cstring/strnpbrk.cpp +static char const* strnpbrk(char const* s, char const* accept, size_t n) { + assert((s || !n) && accept); + + char const* const end = s + n; + for (char const* cur = s; cur < end; ++cur) { + int const c = *cur; + for (char const* a = accept; *a; ++a) { + if (*a == c) { + return cur; + } + } + } + return NULL; +} +static std::string valueToQuotedStringN(const char* value, unsigned length) { + if (value == NULL) + return ""; + // Not sure how to handle unicode... + if (strnpbrk(value, "\"\\\b\f\n\r\t", length) == NULL && + !containsControlCharacter0(value, length)) + return std::string("\"") + value + "\""; + // We have to walk value and escape any special characters. + // Appending to std::string is not efficient, but this should be rare. + // (Note: forward slashes are *not* rare, but I am not escaping them.) + std::string::size_type maxsize = + length * 2 + 3; // allescaped+quotes+NULL + std::string result; + result.reserve(maxsize); // to avoid lots of mallocs + result += "\""; + char const* end = value + length; + for (const char* c = value; c != end; ++c) { + switch (*c) { + case '\"': + result += "\\\""; break; - case intValue: - pushValue( valueToString( value.asLargestInt() ) ); + case '\\': + result += "\\\\"; break; - case uintValue: - pushValue( valueToString( value.asLargestUInt() ) ); + case '\b': + result += "\\b"; break; - case realValue: - pushValue( valueToString( value.asDouble() ) ); + case '\f': + result += "\\f"; break; - case stringValue: - pushValue( valueToQuotedString( value.asCString() ) ); + case '\n': + result += "\\n"; break; - case booleanValue: - pushValue( valueToString( value.asBool() ) ); + case '\r': + result += "\\r"; break; - case arrayValue: - writeArrayValue( value); + case '\t': + result += "\\t"; break; - case objectValue: - { - Value::Members members( value.getMemberNames() ); - if ( members.empty() ) - pushValue( "{}" ); - else - { - writeWithIndent( "{" ); - indent(); - Value::Members::iterator it = members.begin(); - for (;;) - { - const std::string &name = *it; - const Value &childValue = value[name]; - writeCommentBeforeValue( childValue ); - writeWithIndent( valueToQuotedString( name.c_str() ) ); - document_ += " : "; - writeValue( childValue ); - if ( ++it == members.end() ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - document_ += ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "}" ); - } + // case '/': + // Even though \/ is considered a legal escape in JSON, a bare + // slash is also legal, so I see no reason to escape it. + // (I hope I am not misunderstanding something.) + // blep notes: actually escaping \/ may be useful in javascript to avoid (*c); + result += oss.str(); + } else { + result += *c; } break; - } -} - - -void -StyledWriter::writeArrayValue( const Value &value ) -{ - unsigned size = value.size(); - if ( size == 0 ) - pushValue( "[]" ); - else - { - bool isArrayMultiLine = isMultineArray( value ); - if ( isArrayMultiLine ) - { - writeWithIndent( "[" ); - indent(); - bool hasChildValue = !childValues_.empty(); - unsigned index =0; - for (;;) - { - const Value &childValue = value[index]; - writeCommentBeforeValue( childValue ); - if ( hasChildValue ) - writeWithIndent( childValues_[index] ); - else - { - writeIndent(); - writeValue( childValue ); - } - if ( ++index == size ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - document_ += ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "]" ); - } - else // output on a single line - { - assert( childValues_.size() == size ); - document_ += "[ "; - for ( unsigned index =0; index < size; ++index ) - { - if ( index > 0 ) - document_ += ", "; - document_ += childValues_[index]; - } - document_ += " ]"; - } - } -} - - -bool -StyledWriter::isMultineArray( const Value &value ) -{ - int size = value.size(); - bool isMultiLine = size*3 >= rightMargin_ ; - childValues_.clear(); - for ( int index =0; index < size && !isMultiLine; ++index ) - { - const Value &childValue = value[index]; - isMultiLine = isMultiLine || - ( (childValue.isArray() || childValue.isObject()) && - childValue.size() > 0 ); - } - if ( !isMultiLine ) // check if line length > max line length - { - childValues_.reserve( size ); - addChildValues_ = true; - int lineLength = 4 + (size-1)*2; // '[ ' + ', '*n + ' ]' - for ( int index =0; index < size && !isMultiLine; ++index ) - { - writeValue( value[index] ); - lineLength += int( childValues_[index].length() ); - isMultiLine = isMultiLine && hasCommentForValue( value[index] ); - } - addChildValues_ = false; - isMultiLine = isMultiLine || lineLength >= rightMargin_; - } - return isMultiLine; -} - - -void -StyledWriter::pushValue( const std::string &value ) -{ - if ( addChildValues_ ) - childValues_.push_back( value ); - else - document_ += value; + } + } + result += "\""; + return result; } +// Class Writer +// ////////////////////////////////////////////////////////////////// +Writer::~Writer() {} -void -StyledWriter::writeIndent() -{ - if ( !document_.empty() ) - { - char last = document_[document_.length()-1]; - if ( last == ' ' ) // already indented - return; - if ( last != '\n' ) // Comments may add new-line - document_ += '\n'; - } - document_ += indentString_; -} - +// Class FastWriter +// ////////////////////////////////////////////////////////////////// -void -StyledWriter::writeWithIndent( const std::string &value ) -{ - writeIndent(); - document_ += value; +FastWriter::FastWriter() + : yamlCompatiblityEnabled_(false) {} + +void FastWriter::enableYAMLCompatibility() { yamlCompatiblityEnabled_ = true; } + +std::string FastWriter::write(const Value& root) { + document_ = ""; + writeValue(root); + document_ += "\n"; + return document_; +} + +void FastWriter::writeValue(const Value& value) { + switch (value.type()) { + case nullValue: + document_ += "null"; + break; + case intValue: + document_ += valueToString(value.asLargestInt()); + break; + case uintValue: + document_ += valueToString(value.asLargestUInt()); + break; + case realValue: + document_ += valueToString(value.asDouble()); + break; + case stringValue: + { + // Is NULL possible for value.string_? + char const* str; + char const* end; + bool ok = value.getString(&str, &end); + if (ok) document_ += valueToQuotedStringN(str, static_cast(end-str)); + break; + } + case booleanValue: + document_ += valueToString(value.asBool()); + break; + case arrayValue: { + document_ += '['; + int size = value.size(); + for (int index = 0; index < size; ++index) { + if (index > 0) + document_ += ','; + writeValue(value[index]); + } + document_ += ']'; + } break; + case objectValue: { + Value::Members members(value.getMemberNames()); + document_ += '{'; + for (Value::Members::iterator it = members.begin(); it != members.end(); + ++it) { + const std::string& name = *it; + if (it != members.begin()) + document_ += ','; + document_ += valueToQuotedStringN(name.data(), static_cast(name.length())); + document_ += yamlCompatiblityEnabled_ ? ": " : ":"; + writeValue(value[name]); + } + document_ += '}'; + } break; + } } +// Class StyledWriter +// ////////////////////////////////////////////////////////////////// -void -StyledWriter::indent() -{ - indentString_ += std::string( indentSize_, ' ' ); +StyledWriter::StyledWriter() + : rightMargin_(74), indentSize_(3), addChildValues_() {} + +std::string StyledWriter::write(const Value& root) { + document_ = ""; + addChildValues_ = false; + indentString_ = ""; + writeCommentBeforeValue(root); + writeValue(root); + writeCommentAfterValueOnSameLine(root); + document_ += "\n"; + return document_; +} + +void StyledWriter::writeValue(const Value& value) { + switch (value.type()) { + case nullValue: + pushValue("null"); + break; + case intValue: + pushValue(valueToString(value.asLargestInt())); + break; + case uintValue: + pushValue(valueToString(value.asLargestUInt())); + break; + case realValue: + pushValue(valueToString(value.asDouble())); + break; + case stringValue: + { + // Is NULL possible for value.string_? + char const* str; + char const* end; + bool ok = value.getString(&str, &end); + if (ok) pushValue(valueToQuotedStringN(str, static_cast(end-str))); + else pushValue(""); + break; + } + case booleanValue: + pushValue(valueToString(value.asBool())); + break; + case arrayValue: + writeArrayValue(value); + break; + case objectValue: { + Value::Members members(value.getMemberNames()); + if (members.empty()) + pushValue("{}"); + else { + writeWithIndent("{"); + indent(); + Value::Members::iterator it = members.begin(); + for (;;) { + const std::string& name = *it; + const Value& childValue = value[name]; + writeCommentBeforeValue(childValue); + writeWithIndent(valueToQuotedString(name.c_str())); + document_ += " : "; + writeValue(childValue); + if (++it == members.end()) { + writeCommentAfterValueOnSameLine(childValue); + break; + } + document_ += ','; + writeCommentAfterValueOnSameLine(childValue); + } + unindent(); + writeWithIndent("}"); + } + } break; + } +} + +void StyledWriter::writeArrayValue(const Value& value) { + unsigned size = value.size(); + if (size == 0) + pushValue("[]"); + else { + bool isArrayMultiLine = isMultineArray(value); + if (isArrayMultiLine) { + writeWithIndent("["); + indent(); + bool hasChildValue = !childValues_.empty(); + unsigned index = 0; + for (;;) { + const Value& childValue = value[index]; + writeCommentBeforeValue(childValue); + if (hasChildValue) + writeWithIndent(childValues_[index]); + else { + writeIndent(); + writeValue(childValue); + } + if (++index == size) { + writeCommentAfterValueOnSameLine(childValue); + break; + } + document_ += ','; + writeCommentAfterValueOnSameLine(childValue); + } + unindent(); + writeWithIndent("]"); + } else // output on a single line + { + assert(childValues_.size() == size); + document_ += "[ "; + for (unsigned index = 0; index < size; ++index) { + if (index > 0) + document_ += ", "; + document_ += childValues_[index]; + } + document_ += " ]"; + } + } +} + +bool StyledWriter::isMultineArray(const Value& value) { + int size = value.size(); + bool isMultiLine = size * 3 >= rightMargin_; + childValues_.clear(); + for (int index = 0; index < size && !isMultiLine; ++index) { + const Value& childValue = value[index]; + isMultiLine = + isMultiLine || ((childValue.isArray() || childValue.isObject()) && + childValue.size() > 0); + } + if (!isMultiLine) // check if line length > max line length + { + childValues_.reserve(size); + addChildValues_ = true; + int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]' + for (int index = 0; index < size; ++index) { + if (hasCommentForValue(value[index])) { + isMultiLine = true; + } + writeValue(value[index]); + lineLength += int(childValues_[index].length()); + } + addChildValues_ = false; + isMultiLine = isMultiLine || lineLength >= rightMargin_; + } + return isMultiLine; +} + +void StyledWriter::pushValue(const std::string& value) { + if (addChildValues_) + childValues_.push_back(value); + else + document_ += value; +} + +void StyledWriter::writeIndent() { + if (!document_.empty()) { + char last = document_[document_.length() - 1]; + if (last == ' ') // already indented + return; + if (last != '\n') // Comments may add new-line + document_ += '\n'; + } + document_ += indentString_; } - -void -StyledWriter::unindent() -{ - assert( int(indentString_.size()) >= indentSize_ ); - indentString_.resize( indentString_.size() - indentSize_ ); +void StyledWriter::writeWithIndent(const std::string& value) { + writeIndent(); + document_ += value; } +void StyledWriter::indent() { indentString_ += std::string(indentSize_, ' '); } -void -StyledWriter::writeCommentBeforeValue( const Value &root ) -{ - if ( !root.hasComment( commentBefore ) ) - return; - document_ += normalizeEOL( root.getComment( commentBefore ) ); - document_ += "\n"; +void StyledWriter::unindent() { + assert(int(indentString_.size()) >= indentSize_); + indentString_.resize(indentString_.size() - indentSize_); } +void StyledWriter::writeCommentBeforeValue(const Value& root) { + if (!root.hasComment(commentBefore)) + return; -void -StyledWriter::writeCommentAfterValueOnSameLine( const Value &root ) -{ - if ( root.hasComment( commentAfterOnSameLine ) ) - document_ += " " + normalizeEOL( root.getComment( commentAfterOnSameLine ) ); + document_ += "\n"; + writeIndent(); + const std::string& comment = root.getComment(commentBefore); + std::string::const_iterator iter = comment.begin(); + while (iter != comment.end()) { + document_ += *iter; + if (*iter == '\n' && + (iter != comment.end() && *(iter + 1) == '/')) + writeIndent(); + ++iter; + } - if ( root.hasComment( commentAfter ) ) - { - document_ += "\n"; - document_ += normalizeEOL( root.getComment( commentAfter ) ); - document_ += "\n"; - } + // Comments are stripped of trailing newlines, so add one here + document_ += "\n"; } +void StyledWriter::writeCommentAfterValueOnSameLine(const Value& root) { + if (root.hasComment(commentAfterOnSameLine)) + document_ += " " + root.getComment(commentAfterOnSameLine); -bool -StyledWriter::hasCommentForValue( const Value &value ) -{ - return value.hasComment( commentBefore ) - || value.hasComment( commentAfterOnSameLine ) - || value.hasComment( commentAfter ); + if (root.hasComment(commentAfter)) { + document_ += "\n"; + document_ += root.getComment(commentAfter); + document_ += "\n"; + } } - -std::string -StyledWriter::normalizeEOL( const std::string &text ) -{ - std::string normalized; - normalized.reserve( text.length() ); - const char *begin = text.c_str(); - const char *end = begin + text.length(); - const char *current = begin; - while ( current != end ) - { - char c = *current++; - if ( c == '\r' ) // mac or dos EOL - { - if ( *current == '\n' ) // convert dos EOL - ++current; - normalized += '\n'; - } - else // handle unix EOL & other char - normalized += c; - } - return normalized; +bool StyledWriter::hasCommentForValue(const Value& value) { + return value.hasComment(commentBefore) || + value.hasComment(commentAfterOnSameLine) || + value.hasComment(commentAfter); } - // Class StyledStreamWriter // ////////////////////////////////////////////////////////////////// -StyledStreamWriter::StyledStreamWriter( std::string indentation ) - : document_(NULL) - , rightMargin_( 74 ) - , indentation_( indentation ) -{ -} - - -void -StyledStreamWriter::write( std::ostream &out, const Value &root ) -{ - document_ = &out; - addChildValues_ = false; - indentString_ = ""; - writeCommentBeforeValue( root ); - writeValue( root ); - writeCommentAfterValueOnSameLine( root ); - *document_ << "\n"; - document_ = NULL; // Forget the stream, for safety. -} - - -void -StyledStreamWriter::writeValue( const Value &value ) -{ - switch ( value.type() ) - { - case nullValue: - pushValue( "null" ); - break; - case intValue: - pushValue( valueToString( value.asLargestInt() ) ); - break; - case uintValue: - pushValue( valueToString( value.asLargestUInt() ) ); - break; - case realValue: - pushValue( valueToString( value.asDouble() ) ); - break; - case stringValue: - pushValue( valueToQuotedString( value.asCString() ) ); - break; - case booleanValue: - pushValue( valueToString( value.asBool() ) ); - break; - case arrayValue: - writeArrayValue( value); - break; - case objectValue: - { - Value::Members members( value.getMemberNames() ); - if ( members.empty() ) - pushValue( "{}" ); - else - { - writeWithIndent( "{" ); - indent(); - Value::Members::iterator it = members.begin(); - for (;;) - { - const std::string &name = *it; - const Value &childValue = value[name]; - writeCommentBeforeValue( childValue ); - writeWithIndent( valueToQuotedString( name.c_str() ) ); - *document_ << " : "; - writeValue( childValue ); - if ( ++it == members.end() ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - *document_ << ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "}" ); - } +StyledStreamWriter::StyledStreamWriter(std::string indentation) + : document_(NULL), rightMargin_(74), indentation_(indentation), + addChildValues_() {} + +void StyledStreamWriter::write(std::ostream& out, const Value& root) { + document_ = &out; + addChildValues_ = false; + indentString_ = ""; + indented_ = true; + writeCommentBeforeValue(root); + if (!indented_) writeIndent(); + indented_ = true; + writeValue(root); + writeCommentAfterValueOnSameLine(root); + *document_ << "\n"; + document_ = NULL; // Forget the stream, for safety. +} + +void StyledStreamWriter::writeValue(const Value& value) { + switch (value.type()) { + case nullValue: + pushValue("null"); + break; + case intValue: + pushValue(valueToString(value.asLargestInt())); + break; + case uintValue: + pushValue(valueToString(value.asLargestUInt())); + break; + case realValue: + pushValue(valueToString(value.asDouble())); + break; + case stringValue: + { + // Is NULL possible for value.string_? + char const* str; + char const* end; + bool ok = value.getString(&str, &end); + if (ok) pushValue(valueToQuotedStringN(str, static_cast(end-str))); + else pushValue(""); + break; + } + case booleanValue: + pushValue(valueToString(value.asBool())); + break; + case arrayValue: + writeArrayValue(value); + break; + case objectValue: { + Value::Members members(value.getMemberNames()); + if (members.empty()) + pushValue("{}"); + else { + writeWithIndent("{"); + indent(); + Value::Members::iterator it = members.begin(); + for (;;) { + const std::string& name = *it; + const Value& childValue = value[name]; + writeCommentBeforeValue(childValue); + writeWithIndent(valueToQuotedString(name.c_str())); + *document_ << " : "; + writeValue(childValue); + if (++it == members.end()) { + writeCommentAfterValueOnSameLine(childValue); + break; + } + *document_ << ","; + writeCommentAfterValueOnSameLine(childValue); } - break; - } -} - - -void -StyledStreamWriter::writeArrayValue( const Value &value ) -{ - unsigned size = value.size(); - if ( size == 0 ) - pushValue( "[]" ); - else - { - bool isArrayMultiLine = isMultineArray( value ); - if ( isArrayMultiLine ) - { - writeWithIndent( "[" ); - indent(); - bool hasChildValue = !childValues_.empty(); - unsigned index =0; - for (;;) - { - const Value &childValue = value[index]; - writeCommentBeforeValue( childValue ); - if ( hasChildValue ) - writeWithIndent( childValues_[index] ); - else - { - writeIndent(); - writeValue( childValue ); - } - if ( ++index == size ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - *document_ << ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "]" ); + unindent(); + writeWithIndent("}"); + } + } break; + } +} + +void StyledStreamWriter::writeArrayValue(const Value& value) { + unsigned size = value.size(); + if (size == 0) + pushValue("[]"); + else { + bool isArrayMultiLine = isMultineArray(value); + if (isArrayMultiLine) { + writeWithIndent("["); + indent(); + bool hasChildValue = !childValues_.empty(); + unsigned index = 0; + for (;;) { + const Value& childValue = value[index]; + writeCommentBeforeValue(childValue); + if (hasChildValue) + writeWithIndent(childValues_[index]); + else { + if (!indented_) writeIndent(); + indented_ = true; + writeValue(childValue); + indented_ = false; + } + if (++index == size) { + writeCommentAfterValueOnSameLine(childValue); + break; + } + *document_ << ","; + writeCommentAfterValueOnSameLine(childValue); } - else // output on a single line - { - assert( childValues_.size() == size ); - *document_ << "[ "; - for ( unsigned index =0; index < size; ++index ) - { - if ( index > 0 ) - *document_ << ", "; - *document_ << childValues_[index]; - } - *document_ << " ]"; + unindent(); + writeWithIndent("]"); + } else // output on a single line + { + assert(childValues_.size() == size); + *document_ << "[ "; + for (unsigned index = 0; index < size; ++index) { + if (index > 0) + *document_ << ", "; + *document_ << childValues_[index]; } - } -} - - -bool -StyledStreamWriter::isMultineArray( const Value &value ) -{ - int size = value.size(); - bool isMultiLine = size*3 >= rightMargin_ ; - childValues_.clear(); - for ( int index =0; index < size && !isMultiLine; ++index ) - { - const Value &childValue = value[index]; - isMultiLine = isMultiLine || - ( (childValue.isArray() || childValue.isObject()) && - childValue.size() > 0 ); - } - if ( !isMultiLine ) // check if line length > max line length - { - childValues_.reserve( size ); - addChildValues_ = true; - int lineLength = 4 + (size-1)*2; // '[ ' + ', '*n + ' ]' - for ( int index =0; index < size && !isMultiLine; ++index ) - { - writeValue( value[index] ); - lineLength += int( childValues_[index].length() ); - isMultiLine = isMultiLine && hasCommentForValue( value[index] ); + *document_ << " ]"; + } + } +} + +bool StyledStreamWriter::isMultineArray(const Value& value) { + int size = value.size(); + bool isMultiLine = size * 3 >= rightMargin_; + childValues_.clear(); + for (int index = 0; index < size && !isMultiLine; ++index) { + const Value& childValue = value[index]; + isMultiLine = + isMultiLine || ((childValue.isArray() || childValue.isObject()) && + childValue.size() > 0); + } + if (!isMultiLine) // check if line length > max line length + { + childValues_.reserve(size); + addChildValues_ = true; + int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]' + for (int index = 0; index < size; ++index) { + if (hasCommentForValue(value[index])) { + isMultiLine = true; } - addChildValues_ = false; - isMultiLine = isMultiLine || lineLength >= rightMargin_; - } - return isMultiLine; + writeValue(value[index]); + lineLength += int(childValues_[index].length()); + } + addChildValues_ = false; + isMultiLine = isMultiLine || lineLength >= rightMargin_; + } + return isMultiLine; } - -void -StyledStreamWriter::pushValue( const std::string &value ) -{ - if ( addChildValues_ ) - childValues_.push_back( value ); - else - *document_ << value; +void StyledStreamWriter::pushValue(const std::string& value) { + if (addChildValues_) + childValues_.push_back(value); + else + *document_ << value; } - -void -StyledStreamWriter::writeIndent() -{ - /* - Some comments in this method would have been nice. ;-) - - if ( !document_.empty() ) - { - char last = document_[document_.length()-1]; - if ( last == ' ' ) // already indented - return; - if ( last != '\n' ) // Comments may add new-line - *document_ << '\n'; - } - */ - *document_ << '\n' << indentString_; +void StyledStreamWriter::writeIndent() { + // blep intended this to look at the so-far-written string + // to determine whether we are already indented, but + // with a stream we cannot do that. So we rely on some saved state. + // The caller checks indented_. + *document_ << '\n' << indentString_; } - -void -StyledStreamWriter::writeWithIndent( const std::string &value ) -{ - writeIndent(); - *document_ << value; +void StyledStreamWriter::writeWithIndent(const std::string& value) { + if (!indented_) writeIndent(); + *document_ << value; + indented_ = false; } +void StyledStreamWriter::indent() { indentString_ += indentation_; } -void -StyledStreamWriter::indent() -{ - indentString_ += indentation_; +void StyledStreamWriter::unindent() { + assert(indentString_.size() >= indentation_.size()); + indentString_.resize(indentString_.size() - indentation_.size()); } +void StyledStreamWriter::writeCommentBeforeValue(const Value& root) { + if (!root.hasComment(commentBefore)) + return; -void -StyledStreamWriter::unindent() -{ - assert( indentString_.size() >= indentation_.size() ); - indentString_.resize( indentString_.size() - indentation_.size() ); + if (!indented_) writeIndent(); + const std::string& comment = root.getComment(commentBefore); + std::string::const_iterator iter = comment.begin(); + while (iter != comment.end()) { + *document_ << *iter; + if (*iter == '\n' && + (iter != comment.end() && *(iter + 1) == '/')) + // writeIndent(); // would include newline + *document_ << indentString_; + ++iter; + } + indented_ = false; } +void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) { + if (root.hasComment(commentAfterOnSameLine)) + *document_ << ' ' << root.getComment(commentAfterOnSameLine); -void -StyledStreamWriter::writeCommentBeforeValue( const Value &root ) -{ - if ( !root.hasComment( commentBefore ) ) - return; - *document_ << normalizeEOL( root.getComment( commentBefore ) ); - *document_ << "\n"; + if (root.hasComment(commentAfter)) { + writeIndent(); + *document_ << root.getComment(commentAfter); + } + indented_ = false; } - -void -StyledStreamWriter::writeCommentAfterValueOnSameLine( const Value &root ) -{ - if ( root.hasComment( commentAfterOnSameLine ) ) - *document_ << " " + normalizeEOL( root.getComment( commentAfterOnSameLine ) ); - - if ( root.hasComment( commentAfter ) ) - { - *document_ << "\n"; - *document_ << normalizeEOL( root.getComment( commentAfter ) ); - *document_ << "\n"; - } +bool StyledStreamWriter::hasCommentForValue(const Value& value) { + return value.hasComment(commentBefore) || + value.hasComment(commentAfterOnSameLine) || + value.hasComment(commentAfter); } +////////////////////////// +// BuiltStyledStreamWriter -bool -StyledStreamWriter::hasCommentForValue( const Value &value ) -{ - return value.hasComment( commentBefore ) - || value.hasComment( commentAfterOnSameLine ) - || value.hasComment( commentAfter ); -} - +/// Scoped enums are not available until C++11. +struct CommentStyle { + /// Decide whether to write comments. + enum Enum { + None, ///< Drop all comments. + Most, ///< Recover odd behavior of previous versions (not implemented yet). + All ///< Keep all comments. + }; +}; -std::string -StyledStreamWriter::normalizeEOL( const std::string &text ) -{ - std::string normalized; - normalized.reserve( text.length() ); - const char *begin = text.c_str(); - const char *end = begin + text.length(); - const char *current = begin; - while ( current != end ) - { - char c = *current++; - if ( c == '\r' ) // mac or dos EOL - { - if ( *current == '\n' ) // convert dos EOL - ++current; - normalized += '\n'; +struct BuiltStyledStreamWriter : public StreamWriter +{ + BuiltStyledStreamWriter( + std::string const& indentation, + CommentStyle::Enum cs, + std::string const& colonSymbol, + std::string const& nullSymbol, + std::string const& endingLineFeedSymbol, + bool useSpecialFloats, + unsigned int precision); + virtual int write(Value const& root, std::ostream* sout); +private: + void writeValue(Value const& value); + void writeArrayValue(Value const& value); + bool isMultineArray(Value const& value); + void pushValue(std::string const& value); + void writeIndent(); + void writeWithIndent(std::string const& value); + void indent(); + void unindent(); + void writeCommentBeforeValue(Value const& root); + void writeCommentAfterValueOnSameLine(Value const& root); + static bool hasCommentForValue(const Value& value); + + typedef std::vector ChildValues; + + ChildValues childValues_; + std::string indentString_; + int rightMargin_; + std::string indentation_; + CommentStyle::Enum cs_; + std::string colonSymbol_; + std::string nullSymbol_; + std::string endingLineFeedSymbol_; + bool addChildValues_ : 1; + bool indented_ : 1; + bool useSpecialFloats_ : 1; + unsigned int precision_; +}; +BuiltStyledStreamWriter::BuiltStyledStreamWriter( + std::string const& indentation, + CommentStyle::Enum cs, + std::string const& colonSymbol, + std::string const& nullSymbol, + std::string const& endingLineFeedSymbol, + bool useSpecialFloats, + unsigned int precision) + : rightMargin_(74) + , indentation_(indentation) + , cs_(cs) + , colonSymbol_(colonSymbol) + , nullSymbol_(nullSymbol) + , endingLineFeedSymbol_(endingLineFeedSymbol) + , addChildValues_(false) + , indented_(false) + , useSpecialFloats_(useSpecialFloats) + , precision_(precision) +{ +} +int BuiltStyledStreamWriter::write(Value const& root, std::ostream* sout) +{ + sout_ = sout; + addChildValues_ = false; + indented_ = true; + indentString_ = ""; + writeCommentBeforeValue(root); + if (!indented_) writeIndent(); + indented_ = true; + writeValue(root); + writeCommentAfterValueOnSameLine(root); + *sout_ << endingLineFeedSymbol_; + sout_ = NULL; + return 0; +} +void BuiltStyledStreamWriter::writeValue(Value const& value) { + switch (value.type()) { + case nullValue: + pushValue(nullSymbol_); + break; + case intValue: + pushValue(valueToString(value.asLargestInt())); + break; + case uintValue: + pushValue(valueToString(value.asLargestUInt())); + break; + case realValue: + pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_)); + break; + case stringValue: + { + // Is NULL is possible for value.string_? + char const* str; + char const* end; + bool ok = value.getString(&str, &end); + if (ok) pushValue(valueToQuotedStringN(str, static_cast(end-str))); + else pushValue(""); + break; + } + case booleanValue: + pushValue(valueToString(value.asBool())); + break; + case arrayValue: + writeArrayValue(value); + break; + case objectValue: { + Value::Members members(value.getMemberNames()); + if (members.empty()) + pushValue("{}"); + else { + writeWithIndent("{"); + indent(); + Value::Members::iterator it = members.begin(); + for (;;) { + std::string const& name = *it; + Value const& childValue = value[name]; + writeCommentBeforeValue(childValue); + writeWithIndent(valueToQuotedStringN(name.data(), static_cast(name.length()))); + *sout_ << colonSymbol_; + writeValue(childValue); + if (++it == members.end()) { + writeCommentAfterValueOnSameLine(childValue); + break; + } + *sout_ << ","; + writeCommentAfterValueOnSameLine(childValue); + } + unindent(); + writeWithIndent("}"); + } + } break; + } +} + +void BuiltStyledStreamWriter::writeArrayValue(Value const& value) { + unsigned size = value.size(); + if (size == 0) + pushValue("[]"); + else { + bool isMultiLine = (cs_ == CommentStyle::All) || isMultineArray(value); + if (isMultiLine) { + writeWithIndent("["); + indent(); + bool hasChildValue = !childValues_.empty(); + unsigned index = 0; + for (;;) { + Value const& childValue = value[index]; + writeCommentBeforeValue(childValue); + if (hasChildValue) + writeWithIndent(childValues_[index]); + else { + if (!indented_) writeIndent(); + indented_ = true; + writeValue(childValue); + indented_ = false; + } + if (++index == size) { + writeCommentAfterValueOnSameLine(childValue); + break; + } + *sout_ << ","; + writeCommentAfterValueOnSameLine(childValue); + } + unindent(); + writeWithIndent("]"); + } else // output on a single line + { + assert(childValues_.size() == size); + *sout_ << "["; + if (!indentation_.empty()) *sout_ << " "; + for (unsigned index = 0; index < size; ++index) { + if (index > 0) + *sout_ << ", "; + *sout_ << childValues_[index]; + } + if (!indentation_.empty()) *sout_ << " "; + *sout_ << "]"; + } + } +} + +bool BuiltStyledStreamWriter::isMultineArray(Value const& value) { + int size = value.size(); + bool isMultiLine = size * 3 >= rightMargin_; + childValues_.clear(); + for (int index = 0; index < size && !isMultiLine; ++index) { + Value const& childValue = value[index]; + isMultiLine = + isMultiLine || ((childValue.isArray() || childValue.isObject()) && + childValue.size() > 0); + } + if (!isMultiLine) // check if line length > max line length + { + childValues_.reserve(size); + addChildValues_ = true; + int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]' + for (int index = 0; index < size; ++index) { + if (hasCommentForValue(value[index])) { + isMultiLine = true; } - else // handle unix EOL & other char - normalized += c; - } - return normalized; + writeValue(value[index]); + lineLength += int(childValues_[index].length()); + } + addChildValues_ = false; + isMultiLine = isMultiLine || lineLength >= rightMargin_; + } + return isMultiLine; } - -std::ostream& operator<<( std::ostream &sout, const Value &root ) -{ - Json::StyledStreamWriter writer; - writer.write(sout, root); - return sout; +void BuiltStyledStreamWriter::pushValue(std::string const& value) { + if (addChildValues_) + childValues_.push_back(value); + else + *sout_ << value; +} + +void BuiltStyledStreamWriter::writeIndent() { + // blep intended this to look at the so-far-written string + // to determine whether we are already indented, but + // with a stream we cannot do that. So we rely on some saved state. + // The caller checks indented_. + + if (!indentation_.empty()) { + // In this case, drop newlines too. + *sout_ << '\n' << indentString_; + } +} + +void BuiltStyledStreamWriter::writeWithIndent(std::string const& value) { + if (!indented_) writeIndent(); + *sout_ << value; + indented_ = false; +} + +void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; } + +void BuiltStyledStreamWriter::unindent() { + assert(indentString_.size() >= indentation_.size()); + indentString_.resize(indentString_.size() - indentation_.size()); +} + +void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) { + if (cs_ == CommentStyle::None) return; + if (!root.hasComment(commentBefore)) + return; + + if (!indented_) writeIndent(); + const std::string& comment = root.getComment(commentBefore); + std::string::const_iterator iter = comment.begin(); + while (iter != comment.end()) { + *sout_ << *iter; + if (*iter == '\n' && + (iter != comment.end() && *(iter + 1) == '/')) + // writeIndent(); // would write extra newline + *sout_ << indentString_; + ++iter; + } + indented_ = false; +} + +void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) { + if (cs_ == CommentStyle::None) return; + if (root.hasComment(commentAfterOnSameLine)) + *sout_ << " " + root.getComment(commentAfterOnSameLine); + + if (root.hasComment(commentAfter)) { + writeIndent(); + *sout_ << root.getComment(commentAfter); + } +} + +// static +bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) { + return value.hasComment(commentBefore) || + value.hasComment(commentAfterOnSameLine) || + value.hasComment(commentAfter); +} + +/////////////// +// StreamWriter + +StreamWriter::StreamWriter() + : sout_(NULL) +{ +} +StreamWriter::~StreamWriter() +{ +} +StreamWriter::Factory::~Factory() +{} +StreamWriterBuilder::StreamWriterBuilder() +{ + setDefaults(&settings_); +} +StreamWriterBuilder::~StreamWriterBuilder() +{} +StreamWriter* StreamWriterBuilder::newStreamWriter() const +{ + std::string indentation = settings_["indentation"].asString(); + std::string cs_str = settings_["commentStyle"].asString(); + bool eyc = settings_["enableYAMLCompatibility"].asBool(); + bool dnp = settings_["dropNullPlaceholders"].asBool(); + bool usf = settings_["useSpecialFloats"].asBool(); + unsigned int pre = settings_["precision"].asUInt(); + CommentStyle::Enum cs = CommentStyle::All; + if (cs_str == "All") { + cs = CommentStyle::All; + } else if (cs_str == "None") { + cs = CommentStyle::None; + } else { + throwRuntimeError("commentStyle must be 'All' or 'None'"); + } + std::string colonSymbol = " : "; + if (eyc) { + colonSymbol = ": "; + } else if (indentation.empty()) { + colonSymbol = ":"; + } + std::string nullSymbol = "null"; + if (dnp) { + nullSymbol = ""; + } + if (pre > 17) pre = 17; + std::string endingLineFeedSymbol = ""; + return new BuiltStyledStreamWriter( + indentation, cs, + colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre); +} +static void getValidWriterKeys(std::set* valid_keys) +{ + valid_keys->clear(); + valid_keys->insert("indentation"); + valid_keys->insert("commentStyle"); + valid_keys->insert("enableYAMLCompatibility"); + valid_keys->insert("dropNullPlaceholders"); + valid_keys->insert("useSpecialFloats"); + valid_keys->insert("precision"); +} +bool StreamWriterBuilder::validate(Json::Value* invalid) const +{ + Json::Value my_invalid; + if (!invalid) invalid = &my_invalid; // so we do not need to test for NULL + Json::Value& inv = *invalid; + std::set valid_keys; + getValidWriterKeys(&valid_keys); + Value::Members keys = settings_.getMemberNames(); + size_t n = keys.size(); + for (size_t i = 0; i < n; ++i) { + std::string const& key = keys[i]; + if (valid_keys.find(key) == valid_keys.end()) { + inv[key] = settings_[key]; + } + } + return 0u == inv.size(); +} +Value& StreamWriterBuilder::operator[](std::string key) +{ + return settings_[key]; +} +// static +void StreamWriterBuilder::setDefaults(Json::Value* settings) +{ + //! [StreamWriterBuilderDefaults] + (*settings)["commentStyle"] = "All"; + (*settings)["indentation"] = "\t"; + (*settings)["enableYAMLCompatibility"] = false; + (*settings)["dropNullPlaceholders"] = false; + (*settings)["useSpecialFloats"] = false; + (*settings)["precision"] = 17; + //! [StreamWriterBuilderDefaults] +} + +std::string writeString(StreamWriter::Factory const& builder, Value const& root) { + std::ostringstream sout; + StreamWriterPtr const writer(builder.newStreamWriter()); + writer->write(root, &sout); + return sout.str(); +} + +std::ostream& operator<<(std::ostream& sout, Value const& root) { + StreamWriterBuilder builder; + StreamWriterPtr const writer(builder.newStreamWriter()); + writer->write(root, &sout); + return sout; } - } // namespace Json diff --git a/userspace/libsinsp/threadinfo.cpp b/userspace/libsinsp/threadinfo.cpp index 20d2f2c56b..c4c9260666 100644 --- a/userspace/libsinsp/threadinfo.cpp +++ b/userspace/libsinsp/threadinfo.cpp @@ -1,28 +1,35 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #ifndef _WIN32 #define __STDC_FORMAT_MACROS #include #endif +#include +#include #include #include "sinsp.h" #include "sinsp_int.h" +#include "protodecoder.h" +#include "tracers.h" + +extern sinsp_evttables g_infotables; static void copy_ipv6_address(uint32_t* dest, uint32_t* src) { @@ -35,24 +42,19 @@ static void copy_ipv6_address(uint32_t* dest, uint32_t* src) /////////////////////////////////////////////////////////////////////////////// // sinsp_threadinfo implementation /////////////////////////////////////////////////////////////////////////////// -sinsp_threadinfo::sinsp_threadinfo() : - m_fdtable(NULL) -{ - m_inspector = NULL; - init(); -} - -sinsp_threadinfo::sinsp_threadinfo(sinsp *inspector) : +sinsp_threadinfo::sinsp_threadinfo(sinsp* inspector) : + m_tracer_parser(NULL), + m_inspector(inspector), m_fdtable(inspector) { - m_inspector = inspector; init(); } void sinsp_threadinfo::init() { m_pid = (uint64_t) - 1LL; - m_progid = -1LL; + m_sid = (uint64_t) - 1LL; + m_vpgid = (uint64_t) - 1LL; set_lastevent_data_validity(false); m_lastevent_type = -1; m_lastevent_ts = 0; @@ -63,22 +65,34 @@ void sinsp_threadinfo::init() m_flags = PPM_CL_NAME_CHANGED; m_nchilds = 0; m_fdlimit = -1; - m_fd_usage_pct = 0; - m_main_thread = NULL; - m_main_program_thread = NULL; + m_vmsize_kb = 0; + m_vmrss_kb = 0; + m_vmswap_kb = 0; + m_pfmajor = 0; + m_pfminor = 0; + m_vtid = -1; + m_vpid = -1; + m_main_thread.reset(); m_lastevent_fd = 0; #ifdef HAS_FILTERING m_last_latency_entertime = 0; m_latency = 0; #endif - m_ainfo = NULL; + m_program_hash = 0; + m_program_hash_scripts = 0; + m_lastevent_data = NULL; + m_parent_loop_detected = false; + m_tty = 0; + m_category = CAT_NONE; + m_blprogram = NULL; + m_loginuid = 0; } sinsp_threadinfo::~sinsp_threadinfo() { uint32_t j; - if((m_inspector != NULL) && + if((m_inspector != NULL) && (m_inspector->m_thread_manager != NULL) && (m_inspector->m_thread_manager->m_listener != NULL)) { @@ -91,6 +105,15 @@ sinsp_threadinfo::~sinsp_threadinfo() } m_private_state.clear(); + if(m_lastevent_data) + { + free(m_lastevent_data); + } + + if(m_tracer_parser) + { + delete m_tracer_parser; + } } void sinsp_threadinfo::fix_sockets_coming_from_proc() @@ -115,7 +138,7 @@ void sinsp_threadinfo::fix_sockets_coming_from_proc() it->second.m_sockinfo.m_ipv4info.m_fields.m_sport = it->second.m_sockinfo.m_ipv4info.m_fields.m_dport; it->second.m_sockinfo.m_ipv4info.m_fields.m_dport = tport; - it->second.m_name = ipv4tuple_to_string(&it->second.m_sockinfo.m_ipv4info); + it->second.m_name = ipv4tuple_to_string(&it->second.m_sockinfo.m_ipv4info, m_inspector->m_hostname_and_port_resolution_enabled); it->second.set_role_server(); } @@ -127,163 +150,359 @@ void sinsp_threadinfo::fix_sockets_coming_from_proc() } } -void sinsp_threadinfo::init(const scap_threadinfo* pi) +#define STR_AS_NUM_JAVA 0x6176616a +#define STR_AS_NUM_RUBY 0x79627572 +#define STR_AS_NUM_PERL 0x6c726570 +#define STR_AS_NUM_NODE 0x65646f6e + +#define MAX_PROG_HASH_LEN 1024 + +void sinsp_threadinfo::compute_program_hash() { - scap_fdinfo *fdi; - scap_fdinfo *tfdi; - sinsp_fdinfo_t newfdi; - string tcomm(pi->comm); + auto curr_hash = std::hash()(m_exe); + hash_combine(curr_hash, m_container_id); + auto rem_len = MAX_PROG_HASH_LEN - (m_exe.size() + m_container_id.size()); - init(); + // + // By default, the scripts hash is just exe+container + // + m_program_hash_scripts = curr_hash; - m_tid = pi->tid; - m_pid = pi->pid; - m_ptid = pi->ptid; + // + // The program hash includes the arguments as well + // + for (auto arg = m_args.begin(); arg != m_args.end() && rem_len > 0; ++arg) + { + if (arg->size() >= rem_len) + { + auto partial_str = arg->substr(0, rem_len); + hash_combine(curr_hash, partial_str); + break; + } - m_comm = pi->comm; + hash_combine(curr_hash, *arg); + rem_len -= arg->size(); + } + m_program_hash = curr_hash; + + // + // For some specific processes (essentially the scripting languages) + // we include the arguments in the scripts hash as well + // + if(m_comm.size() == 4) + { + uint32_t ncomm = *(uint32_t*)m_comm.c_str(); + + if(ncomm == STR_AS_NUM_JAVA || ncomm == STR_AS_NUM_RUBY || + ncomm == STR_AS_NUM_PERL || ncomm == STR_AS_NUM_NODE) + { + m_program_hash_scripts = m_program_hash; + } + } + else if(m_comm.size() >= 6) + { + if(m_comm.substr(0, 6) == "python") + { + m_program_hash_scripts = m_program_hash; + } + } +} + +void sinsp_threadinfo::add_fd_from_scap(scap_fdinfo *fdi, OUT sinsp_fdinfo_t *res) +{ + sinsp_fdinfo_t* newfdi = res; + newfdi->reset(); + bool do_add = true; + + newfdi->m_type = fdi->type; + newfdi->m_openflags = 0; + newfdi->m_type = fdi->type; + newfdi->m_flags = sinsp_fdinfo_t::FLAGS_FROM_PROC; + newfdi->m_ino = fdi->ino; - if(tcomm == "" || tcomm[tcomm.length() - 1] == '/') + switch(newfdi->m_type) { - string ts(pi->exe); + case SCAP_FD_IPV4_SOCK: + newfdi->m_sockinfo.m_ipv4info.m_fields.m_sip = fdi->info.ipv4info.sip; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_dip = fdi->info.ipv4info.dip; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_sport = fdi->info.ipv4info.sport; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_dport = fdi->info.ipv4info.dport; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_l4proto = fdi->info.ipv4info.l4proto; + if(fdi->info.ipv4info.l4proto == SCAP_L4_TCP) + { + newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_SOCKET_CONNECTED; + } + if(m_inspector->m_network_interfaces) + { + m_inspector->m_network_interfaces->update_fd(newfdi); + } + newfdi->m_name = ipv4tuple_to_string(&newfdi->m_sockinfo.m_ipv4info, m_inspector->m_hostname_and_port_resolution_enabled); + break; + case SCAP_FD_IPV4_SERVSOCK: + newfdi->m_sockinfo.m_ipv4serverinfo.m_ip = fdi->info.ipv4serverinfo.ip; + newfdi->m_sockinfo.m_ipv4serverinfo.m_port = fdi->info.ipv4serverinfo.port; + newfdi->m_sockinfo.m_ipv4serverinfo.m_l4proto = fdi->info.ipv4serverinfo.l4proto; + newfdi->m_name = ipv4serveraddr_to_string(&newfdi->m_sockinfo.m_ipv4serverinfo, m_inspector->m_hostname_and_port_resolution_enabled); + + // + // We keep note of all the host bound server ports. + // We'll need them later when patching connections direction. + // + m_inspector->m_thread_manager->m_server_ports.insert(newfdi->m_sockinfo.m_ipv4serverinfo.m_port); + + break; + case SCAP_FD_IPV6_SOCK: + if(sinsp_utils::is_ipv4_mapped_ipv6((uint8_t*)&fdi->info.ipv6info.sip) && + sinsp_utils::is_ipv4_mapped_ipv6((uint8_t*)&fdi->info.ipv6info.dip)) + { + // + // This is an IPv4-mapped IPv6 addresses (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses). + // Convert it into the IPv4 representation. + // + newfdi->m_type = SCAP_FD_IPV4_SOCK; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_sip = fdi->info.ipv6info.sip[3]; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_dip = fdi->info.ipv6info.dip[3]; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_sport = fdi->info.ipv6info.sport; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_dport = fdi->info.ipv6info.dport; + newfdi->m_sockinfo.m_ipv4info.m_fields.m_l4proto = fdi->info.ipv6info.l4proto; + if(fdi->info.ipv6info.l4proto == SCAP_L4_TCP) + { + newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_SOCKET_CONNECTED; + } + if(m_inspector->m_network_interfaces) + { + m_inspector->m_network_interfaces->update_fd(newfdi); + } + newfdi->m_name = ipv4tuple_to_string(&newfdi->m_sockinfo.m_ipv4info, m_inspector->m_hostname_and_port_resolution_enabled); + } + else + { + copy_ipv6_address(newfdi->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b, fdi->info.ipv6info.sip); + copy_ipv6_address(newfdi->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b, fdi->info.ipv6info.dip); + newfdi->m_sockinfo.m_ipv6info.m_fields.m_sport = fdi->info.ipv6info.sport; + newfdi->m_sockinfo.m_ipv6info.m_fields.m_dport = fdi->info.ipv6info.dport; + newfdi->m_sockinfo.m_ipv6info.m_fields.m_l4proto = fdi->info.ipv6info.l4proto; + if(fdi->info.ipv6info.l4proto == SCAP_L4_TCP) + { + newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_SOCKET_CONNECTED; + } + newfdi->m_name = ipv6tuple_to_string(&newfdi->m_sockinfo.m_ipv6info, m_inspector->m_hostname_and_port_resolution_enabled); + } + break; + case SCAP_FD_IPV6_SERVSOCK: + copy_ipv6_address(newfdi->m_sockinfo.m_ipv6serverinfo.m_ip.m_b, fdi->info.ipv6serverinfo.ip); + newfdi->m_sockinfo.m_ipv6serverinfo.m_port = fdi->info.ipv6serverinfo.port; + newfdi->m_sockinfo.m_ipv6serverinfo.m_l4proto = fdi->info.ipv6serverinfo.l4proto; + newfdi->m_name = ipv6serveraddr_to_string(&newfdi->m_sockinfo.m_ipv6serverinfo, m_inspector->m_hostname_and_port_resolution_enabled); - size_t commbegin = ts.rfind('/'); + // + // We keep note of all the host bound server ports. + // We'll need them later when patching connections direction. + // + m_inspector->m_thread_manager->m_server_ports.insert(newfdi->m_sockinfo.m_ipv6serverinfo.m_port); + + break; + case SCAP_FD_UNIX_SOCK: + newfdi->m_sockinfo.m_unixinfo.m_fields.m_source = fdi->info.unix_socket_info.source; + newfdi->m_sockinfo.m_unixinfo.m_fields.m_dest = fdi->info.unix_socket_info.destination; + newfdi->m_name = fdi->info.unix_socket_info.fname; + if(newfdi->m_name.empty()) + { + newfdi->set_role_client(); + } + else + { + newfdi->set_role_server(); + } + break; + case SCAP_FD_FILE_V2: + newfdi->m_openflags = fdi->info.regularinfo.open_flags; + newfdi->m_name = fdi->info.regularinfo.fname; + newfdi->m_dev = fdi->info.regularinfo.dev; + newfdi->m_mount_id = fdi->info.regularinfo.mount_id; + + if(newfdi->m_name == USER_EVT_DEVICE_NAME) + { + newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE; + } + else + { + newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_NOT_TRACER_FD; + } - if(commbegin != string::npos) + break; + case SCAP_FD_FIFO: + case SCAP_FD_FILE: + case SCAP_FD_DIRECTORY: + case SCAP_FD_UNSUPPORTED: + case SCAP_FD_SIGNALFD: + case SCAP_FD_EVENTPOLL: + case SCAP_FD_EVENT: + case SCAP_FD_INOTIFY: + case SCAP_FD_TIMERFD: + case SCAP_FD_NETLINK: + newfdi->m_name = fdi->info.fname; + + if(newfdi->m_name == USER_EVT_DEVICE_NAME) + { + newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE; + } + else { - m_comm = ts.substr(commbegin + 1); + newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_NOT_TRACER_FD; } + + break; + default: + ASSERT(false); + do_add = false; + break; + } + + // + // Call the protocol decoder callbacks associated to notify them about this FD + // + ASSERT(m_inspector != NULL); + vector::iterator it; + + for(it = m_inspector->m_parser->m_open_callbacks.begin(); + it != m_inspector->m_parser->m_open_callbacks.end(); ++it) + { + (*it)->on_fd_from_proc(newfdi); + } + + // + // Add the FD to the table + // + if(do_add) + { + m_fdtable.add(fdi->fd, newfdi); } +} +void sinsp_threadinfo::init(scap_threadinfo* pi) +{ + scap_fdinfo *fdi; + scap_fdinfo *tfdi; + + init(); + + m_tid = pi->tid; + m_pid = pi->pid; + m_ptid = pi->ptid; + m_sid = pi->sid; + m_vpgid = pi->vpgid; + + m_comm = pi->comm; m_exe = pi->exe; + m_exepath = pi->exepath; set_args(pi->args, pi->args_len); - set_cwd(pi->cwd, strlen(pi->cwd)); + if(is_main_thread()) + { + set_env(pi->env, pi->env_len); + set_cwd(pi->cwd, (uint32_t)strlen(pi->cwd)); + } m_flags |= pi->flags; + m_flags |= PPM_CL_ACTIVE; // Assume that all the threads coming from /proc are real, active threads m_fdtable.clear(); + m_fdtable.m_tid = m_tid; m_fdlimit = pi->fdlimit; m_uid = pi->uid; m_gid = pi->gid; + m_vmsize_kb = pi->vmsize_kb; + m_vmrss_kb = pi->vmrss_kb; + m_vmswap_kb = pi->vmswap_kb; + m_pfmajor = pi->pfmajor; + m_pfminor = pi->pfminor; + m_nchilds = 0; + m_vtid = pi->vtid; + m_vpid = pi->vpid; + m_clone_ts = pi->clone_ts; + m_tty = pi->tty; + m_loginuid = pi->loginuid; + m_category = CAT_NONE; + + set_cgroups(pi->cgroups, pi->cgroups_len); + m_root = pi->root; + ASSERT(m_inspector); + m_inspector->m_container_manager.resolve_container(this, !m_inspector->is_capture()); + // + // Prepare for filtering + // + sinsp_fdinfo_t tfdinfo; + sinsp_evt tevt; + scap_evt tscapevt; + + // + // Initialize the fake events for filtering + // + tscapevt.ts = 0; + tscapevt.type = PPME_SYSCALL_READ_X; + tscapevt.len = 0; + tscapevt.nparams = 0; + + tevt.m_inspector = m_inspector; + tevt.m_info = &(g_infotables.m_event_info[PPME_SYSCALL_READ_X]); + tevt.m_pevt = NULL; + tevt.m_cpuid = 0; + tevt.m_evtnum = 0; + tevt.m_pevt = &tscapevt; + bool match = false; HASH_ITER(hh, pi->fdlist, fdi, tfdi) { - bool do_add = true; - - newfdi.m_type = fdi->type; - newfdi.m_openflags = 0; - newfdi.m_type = fdi->type; - newfdi.m_flags = sinsp_fdinfo_t::FLAGS_FROM_PROC; - newfdi.m_ino = fdi->ino; + add_fd_from_scap(fdi, &tfdinfo); - switch(newfdi.m_type) + if(m_inspector->m_filter != NULL && m_inspector->m_filter_proc_table_when_saving) { - case SCAP_FD_IPV4_SOCK: - newfdi.m_sockinfo.m_ipv4info.m_fields.m_sip = fdi->info.ipv4info.sip; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_dip = fdi->info.ipv4info.dip; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_sport = fdi->info.ipv4info.sport; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_dport = fdi->info.ipv4info.dport; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = fdi->info.ipv4info.l4proto; - m_inspector->m_network_interfaces->update_fd(&newfdi); - newfdi.m_name = ipv4tuple_to_string(&newfdi.m_sockinfo.m_ipv4info); - break; - case SCAP_FD_IPV4_SERVSOCK: - newfdi.m_sockinfo.m_ipv4serverinfo.m_ip = fdi->info.ipv4serverinfo.ip; - newfdi.m_sockinfo.m_ipv4serverinfo.m_port = fdi->info.ipv4serverinfo.port; - newfdi.m_sockinfo.m_ipv4serverinfo.m_l4proto = fdi->info.ipv4serverinfo.l4proto; - newfdi.m_name = ipv4serveraddr_to_string(&newfdi.m_sockinfo.m_ipv4serverinfo); - - // - // We keep note of all the host bound server ports. - // We'll need them later when patching connections direction. - // - m_inspector->m_thread_manager->m_server_ports.insert(newfdi.m_sockinfo.m_ipv4serverinfo.m_port); + tevt.m_tinfo = this; + tevt.m_fdinfo = &tfdinfo; + tscapevt.tid = m_tid; + int64_t tlefd = tevt.m_tinfo->m_lastevent_fd; + tevt.m_tinfo->m_lastevent_fd = fdi->fd; - break; - case SCAP_FD_IPV6_SOCK: - if(sinsp_utils::is_ipv4_mapped_ipv6((uint8_t*)&fdi->info.ipv6info.sip) && - sinsp_utils::is_ipv4_mapped_ipv6((uint8_t*)&fdi->info.ipv6info.dip)) + if(m_inspector->m_filter->run(&tevt)) { - // - // This is an IPv4-mapped IPv6 addresses (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses). - // Convert it into the IPv4 representation. - // - newfdi.m_type = SCAP_FD_IPV4_SOCK; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_sip = fdi->info.ipv6info.sip[3]; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_dip = fdi->info.ipv6info.dip[3]; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_sport = fdi->info.ipv6info.sport; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_dport = fdi->info.ipv6info.dport; - newfdi.m_sockinfo.m_ipv4info.m_fields.m_l4proto = fdi->info.ipv6info.l4proto; - m_inspector->m_network_interfaces->update_fd(&newfdi); - newfdi.m_name = ipv4tuple_to_string(&newfdi.m_sockinfo.m_ipv4info); + match = true; } else { - copy_ipv6_address(newfdi.m_sockinfo.m_ipv6info.m_fields.m_sip, fdi->info.ipv6info.sip); - copy_ipv6_address(newfdi.m_sockinfo.m_ipv6info.m_fields.m_dip, fdi->info.ipv6info.dip); - newfdi.m_sockinfo.m_ipv6info.m_fields.m_sport = fdi->info.ipv6info.sport; - newfdi.m_sockinfo.m_ipv6info.m_fields.m_dport = fdi->info.ipv6info.dport; - newfdi.m_sockinfo.m_ipv6info.m_fields.m_l4proto = fdi->info.ipv6info.l4proto; - newfdi.m_name = ipv6tuple_to_string(&newfdi.m_sockinfo.m_ipv6info); + // + // This tells scap not to include this FD in the write file + // + fdi->type = SCAP_FD_UNINITIALIZED; } - break; - case SCAP_FD_IPV6_SERVSOCK: - copy_ipv6_address(newfdi.m_sockinfo.m_ipv6serverinfo.m_ip, fdi->info.ipv6serverinfo.ip); - newfdi.m_sockinfo.m_ipv6serverinfo.m_port = fdi->info.ipv6serverinfo.port; - newfdi.m_sockinfo.m_ipv6serverinfo.m_l4proto = fdi->info.ipv6serverinfo.l4proto; - newfdi.m_name = ipv6serveraddr_to_string(&newfdi.m_sockinfo.m_ipv6serverinfo); - - // - // We keep note of all the host bound server ports. - // We'll need them later when patching connections direction. - // - m_inspector->m_thread_manager->m_server_ports.insert(newfdi.m_sockinfo.m_ipv6serverinfo.m_port); - break; - case SCAP_FD_UNIX_SOCK: - newfdi.m_sockinfo.m_unixinfo.m_fields.m_source = fdi->info.unix_socket_info.source; - newfdi.m_sockinfo.m_unixinfo.m_fields.m_dest = fdi->info.unix_socket_info.destination; - newfdi.m_name = fdi->info.unix_socket_info.fname; - if(newfdi.m_name.empty()) - { - newfdi.set_role_client(); - } - else - { - newfdi.set_role_server(); - } - break; - case SCAP_FD_FIFO: - case SCAP_FD_FILE: - case SCAP_FD_DIRECTORY: - case SCAP_FD_UNSUPPORTED: - case SCAP_FD_SIGNALFD: - case SCAP_FD_EVENTPOLL: - case SCAP_FD_EVENT: - case SCAP_FD_INOTIFY: - case SCAP_FD_TIMERFD: - newfdi.m_name = fdi->info.fname; - break; - default: - ASSERT(false); - do_add = false; - break; + tevt.m_tinfo->m_lastevent_fd = tlefd; } + } + + m_lastevent_data = NULL; - if(do_add) + if(m_inspector->m_filter != NULL && m_inspector->m_filter_proc_table_when_saving) + { + if(!match) { - m_fdtable.add(fdi->fd, &newfdi); + pi->filtered_out = 1; } } } -string sinsp_threadinfo::get_comm() +std::string sinsp_threadinfo::get_comm() const { return m_comm; } -string sinsp_threadinfo::get_exe() +std::string sinsp_threadinfo::get_exe() const { return m_exe; } +std::string sinsp_threadinfo::get_exepath() const +{ + return m_exepath; +} + void sinsp_threadinfo::set_args(const char* args, size_t len) { m_args.clear(); @@ -296,72 +515,162 @@ void sinsp_threadinfo::set_args(const char* args, size_t len) } } -bool sinsp_threadinfo::is_main_thread() +void sinsp_threadinfo::set_env(const char* env, size_t len) { - return m_tid == m_pid; -} - -sinsp_threadinfo* sinsp_threadinfo::get_main_thread() -{ - if(m_main_thread == NULL) + if (len == SCAP_MAX_ENV_SIZE && m_inspector->large_envs_enabled()) { - // - // Is this a child thread? - // - if(m_pid == m_tid) + // the environment is possibly truncated, try to read from /proc + // this may fail for short-lived processes + if (set_env_from_proc()) { - // - // No, this is either a single thread process or the root thread of a - // multithread process. - // Note: we don't set m_main_thread because there are cases in which this is - // invoked for a threadinfo that is in the stack. Caching the this pointer - // would cause future mess. - // - return this; + g_logger.format(sinsp_logger::SEV_DEBUG, "Large environment for process %lu [%s], loaded from /proc", m_pid, m_comm.c_str()); + return; + } else { + g_logger.format(sinsp_logger::SEV_INFO, "Failed to load environment for process %lu [%s] from /proc, using first %d bytes", m_pid, m_comm.c_str(), SCAP_MAX_ENV_SIZE); } - else + } + + m_env.clear(); + size_t offset = 0; + while(offset < len) + { + const char* left = env + offset; + // environment string may actually be shorter than indicated by len + // if the rest is empty, we bail out early + if(!strlen(left)) { - // - // Yes, this is a child thread. Find the process root thread. - // - sinsp_threadinfo *ptinfo = m_inspector->get_thread(m_pid, true); - if(NULL == ptinfo) + size_t sz = len - offset; + void* zero = calloc(sz, sizeof(char)); + if(!memcmp(left, zero, sz)) { - ASSERT(false); - return NULL; + free(zero); + return; } + free(zero); + } + m_env.push_back(left); + + offset += m_env.back().length() + 1; + } +} + +bool sinsp_threadinfo::set_env_from_proc() { + string environ_path = string(scap_get_host_root()) + "/proc/" + to_string(m_pid) + "/environ"; + + ifstream environment(environ_path); + if (!environment) + { + // failed to read the environment from /proc, work with what we have + return false; + } - m_main_thread = ptinfo; + m_env.clear(); + while (environment) { + string env; + getline(environment, env, '\0'); + if (!env.empty()) + { + m_env.emplace_back(env); } } - return m_main_thread; + return true; } -sinsp_threadinfo* sinsp_threadinfo::get_parent_thread() +const vector& sinsp_threadinfo::get_env() { - return m_inspector->get_thread(m_ptid, false); + if(is_main_thread()) + { + return m_env; + } + else + { + auto mtinfo = get_main_thread(); + if(mtinfo != nullptr) + { + return mtinfo->get_env(); + } + else + { + // it should never happen but provide a safe fallback just in case + // except during sinsp::scap_open() (see sinsp::get_thread()). + ASSERT(false); + return m_env; + } + } } -sinsp_fdtable* sinsp_threadinfo::get_fd_table() +// Return value string for the exact environment variable name given +string sinsp_threadinfo::get_env(const string& name) { - sinsp_threadinfo* root; - - if(!(m_flags & PPM_CL_CLONE_FILES)) + size_t nlen = name.length(); + for(const auto& env_var : get_env()) { - root = this; + if((env_var.length() > (nlen + 1)) && (env_var[nlen] == '=') && + !env_var.compare(0, nlen, name)) + { + // Stripping spaces, not sure if we really should or need to + size_t first = env_var.find_first_not_of(' ', nlen + 1); + if (first == string::npos) + return ""; + size_t last = env_var.find_last_not_of(' '); + + return env_var.substr(first, last - first + 1); + } } - else + + return ""; +} + +void sinsp_threadinfo::set_cgroups(const char* cgroups, size_t len) +{ + m_cgroups.clear(); + + size_t offset = 0; + while(offset < len) { - root = get_main_thread(); - if(NULL == root) + const char* str = cgroups + offset; + const char* sep = strrchr(str, '='); + if(sep == NULL) { ASSERT(false); - return NULL; + return; + } + + string subsys(str, sep - str); + string cgroup(sep + 1); + + size_t subsys_length = subsys.length(); + size_t pos = subsys.find("_cgroup"); + if(pos != string::npos) + { + subsys.erase(pos, sizeof("_cgroup") - 1); + } + + if(subsys == "perf") + { + subsys = "perf_event"; } + else if(subsys == "mem") + { + subsys = "memory"; + } + else if(subsys == "io") + { + // blkio has been renamed just `io` + // in kernel space: + // https://github.com/torvalds/linux/commit/c165b3e3c7bb68c2ed55a5ac2623f030d01d9567 + subsys = "blkio"; + } + + m_cgroups.push_back(std::make_pair(subsys, cgroup)); + offset += subsys_length + 1 + cgroup.length() + 1; } +} - return &(root->m_fdtable); +sinsp_threadinfo* sinsp_threadinfo::get_parent_thread() +{ + return m_inspector->get_thread(m_ptid, false, true); } sinsp_fdinfo_t* sinsp_threadinfo::add_fd(int64_t fd, sinsp_fdinfo_t *fdinfo) @@ -381,35 +690,13 @@ void sinsp_threadinfo::remove_fd(int64_t fd) get_fd_table()->erase(fd); } -sinsp_fdinfo_t* sinsp_threadinfo::get_fd(int64_t fd) -{ - if(fd < 0) - { - return NULL; - } - - sinsp_fdtable* fdt = get_fd_table(); - - if(fdt) - { - return fdt->find(fd); - } - else - { - ASSERT(false); - } - - return NULL; -} - bool sinsp_threadinfo::is_bound_to_port(uint16_t number) { unordered_map::iterator it; sinsp_fdtable* fdt = get_fd_table(); - for(it = fdt->m_table.begin(); - it != fdt->m_table.end(); ++it) + for(it = fdt->m_table.begin(); it != fdt->m_table.end(); ++it) { if(it->second.m_type == SCAP_FD_IPV4_SOCK) { @@ -436,7 +723,7 @@ bool sinsp_threadinfo::uses_client_port(uint16_t number) sinsp_fdtable* fdt = get_fd_table(); - for(it = fdt->m_table.begin(); + for(it = fdt->m_table.begin(); it != fdt->m_table.end(); ++it) { if(it->second.m_type == SCAP_FD_IPV4_SOCK) @@ -451,45 +738,11 @@ bool sinsp_threadinfo::uses_client_port(uint16_t number) return false; } -void sinsp_threadinfo::store_event(sinsp_evt *evt) -{ - uint32_t elen; - - // - // Make sure the event data is going to fit - // - elen = scap_event_getlen(evt->m_pevt); - - if(elen > SP_EVT_BUF_SIZE) - { - ASSERT(false); - return; - } - - // - // Copy the data - // - memcpy(m_lastevent_data, evt->m_pevt, elen); - m_lastevent_cpuid = evt->get_cpuid(); -} - bool sinsp_threadinfo::is_lastevent_data_valid() { return (m_lastevent_cpuid != (uint16_t) - 1); } -void sinsp_threadinfo::set_lastevent_data_validity(bool isvalid) -{ - if(isvalid) - { - m_lastevent_cpuid = (uint16_t)1; - } - else - { - m_lastevent_cpuid = (uint16_t) - 1; - } -} - sinsp_threadinfo* sinsp_threadinfo::get_cwd_root() { if(!(m_flags & PPM_CL_CLONE_FS)) @@ -504,7 +757,12 @@ sinsp_threadinfo* sinsp_threadinfo::get_cwd_root() string sinsp_threadinfo::get_cwd() { - sinsp_threadinfo* tinfo = get_cwd_root(); + // Ideally we should use get_cwd_root() + // but scap does not read CLONE_FS from /proc + // Also glibc and muslc use always + // CLONE_THREAD|CLONE_FS so let's use + // get_main_thread() for now + sinsp_threadinfo* tinfo = get_main_thread(); if(tinfo) { @@ -520,20 +778,22 @@ string sinsp_threadinfo::get_cwd() void sinsp_threadinfo::set_cwd(const char* cwd, uint32_t cwdlen) { char tpath[SCAP_MAX_PATH_SIZE]; - sinsp_threadinfo* tinfo = get_cwd_root(); + sinsp_threadinfo* tinfo = get_main_thread(); if(tinfo) { - sinsp_utils::concatenate_paths(tpath, - SCAP_MAX_PATH_SIZE, - (char*)tinfo->m_cwd.c_str(), - tinfo->m_cwd.size(), - cwd, + sinsp_utils::concatenate_paths(tpath, + SCAP_MAX_PATH_SIZE, + (char*)tinfo->m_cwd.c_str(), + (uint32_t)tinfo->m_cwd.size(), + cwd, cwdlen); tinfo->m_cwd = tpath; - if(tinfo->m_cwd[tinfo->m_cwd.size() - 1] != '/') + uint32_t size = tinfo->m_cwd.size(); + + if(size == 0 || (tinfo->m_cwd[size - 1] != '/')) { tinfo->m_cwd += '/'; } @@ -553,7 +813,7 @@ void sinsp_threadinfo::allocate_private_state() m_private_state.clear(); vector* sizes = &m_inspector->m_thread_privatestate_manager.m_memory_sizes; - + for(j = 0; j < sizes->size(); j++) { void* newbuf = malloc(sizes->at(j)); @@ -574,183 +834,480 @@ void* sinsp_threadinfo::get_private_state(uint32_t id) return m_private_state[id]; } - -/////////////////////////////////////////////////////////////////////////////// -// sinsp_thread_manager implementation -/////////////////////////////////////////////////////////////////////////////// -sinsp_thread_manager::sinsp_thread_manager(sinsp* inspector) +uint64_t sinsp_threadinfo::get_fd_usage_pct() { - m_inspector = inspector; - m_listener = NULL; - clear(); + int64_t fdlimit = get_fd_limit(); + if(fdlimit > 0) + { + uint64_t fd_opencount = get_fd_opencount(); + ASSERT(fd_opencount <= (uint64_t) fdlimit); + if(fd_opencount <= (uint64_t) fdlimit) + { + return (fd_opencount * 100) / fdlimit; + } + else + { + return 100; + } + } + else + { + return 0; + } } -void sinsp_thread_manager::clear() +double sinsp_threadinfo::get_fd_usage_pct_d() { - m_threadtable.clear(); - m_last_tid = 0; - m_last_tinfo = NULL; - m_last_flush_time_ns = 0; - m_n_drops = 0; + int64_t fdlimit = get_fd_limit(); + if(fdlimit > 0) + { + uint64_t fd_opencount = get_fd_opencount(); + ASSERT(fd_opencount <= (uint64_t) fdlimit); + if(fd_opencount <= (uint64_t) fdlimit) + { + return ((double)fd_opencount * 100) / fdlimit; + } + else + { + return 100; + } + } + else + { + return 0; + } +} -#ifdef GATHER_INTERNAL_STATS - m_failed_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_failed_lookups","Failed thread lookups")); - m_cached_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_cached_lookups","Cached thread lookups")); - m_non_cached_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_non_cached_lookups","Non cached thread lookups")); - m_added_threads = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_added","Number of added threads")); - m_removed_threads = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_removed","Removed threads")); -#endif +uint64_t sinsp_threadinfo::get_fd_opencount() const +{ + return get_main_thread()->m_fdtable.size(); } -void sinsp_thread_manager::set_listener(sinsp_threadtable_listener* listener) +uint64_t sinsp_threadinfo::get_fd_limit() { - m_listener = listener; + return get_main_thread()->m_fdlimit; } -sinsp_threadinfo* sinsp_thread_manager::get_thread(int64_t tid) +const std::string& sinsp_threadinfo::get_cgroup(const std::string& subsys) const { - threadinfo_map_iterator_t it; + static const std::string notfound = "/"; - // - // Try looking up in our simple cache - // - if(m_last_tinfo && tid == m_last_tid) + for(const auto& it : m_cgroups) { -#ifdef GATHER_INTERNAL_STATS - m_cached_lookups->increment(); -#endif - m_last_tinfo->m_lastaccess_ts = m_inspector->m_lastevent_ts; - return m_last_tinfo; + if(it.first == subsys) + { + return it.second; + } } - // - // Caching failed, do a real lookup - // - it = m_threadtable.find(tid); - - if(it != m_threadtable.end()) + return notfound; +} + +void sinsp_threadinfo::traverse_parent_state(visitor_func_t &visitor) +{ + // Use two pointers starting at this, traversing the parent + // state, at different rates. If they ever equal each other + // before slow is NULL there's a loop. + + sinsp_threadinfo *slow=this->get_parent_thread(), *fast=slow; + + // Move fast to its parent + fast = (fast ? fast->get_parent_thread() : fast); + + // The slow pointer must be valid and not have a tid of -1. + while(slow && slow->m_tid != -1) { -#ifdef GATHER_INTERNAL_STATS - m_non_cached_lookups->increment(); -#endif - m_last_tid = tid; - m_last_tinfo = &(it->second); - m_last_tinfo->m_lastaccess_ts = m_inspector->m_lastevent_ts; - return &(it->second); + if(!visitor(slow)) + { + break; + } + + // Advance slow one step and advance fast two steps + slow = slow->get_parent_thread(); + + // advance fast 2 steps, checking to see if we meet + // slow after each step. + for (uint32_t i = 0; i < 2; i++) { + fast = (fast ? fast->get_parent_thread() : fast); + + // If not at the end but fast == slow or if + // slow points to itself, there's a loop in + // the thread state. + if(slow && (slow == fast || + slow->m_tid == slow->m_ptid)) + { + // Note we only log a loop once for a given main thread, to avoid flooding logs. + if(!m_parent_loop_detected) + { + g_logger.log(string("Loop in parent thread state detected for pid ") + + std::to_string(m_pid) + + ". stopped at tid= " + std::to_string(slow->m_tid) + + " ptid=" + std::to_string(slow->m_ptid), + sinsp_logger::SEV_WARNING); + m_parent_loop_detected = true; + } + return; + } + } } - else +} + +void sinsp_threadinfo::populate_cmdline(string &cmdline, sinsp_threadinfo *tinfo) +{ + cmdline = tinfo->get_comm(); + + uint32_t j; + uint32_t nargs = (uint32_t)tinfo->m_args.size(); + + for(j = 0; j < nargs; j++) { -#ifdef GATHER_INTERNAL_STATS - m_failed_lookups->increment(); + cmdline += " " + tinfo->m_args[j]; + } +} + +bool sinsp_threadinfo::is_health_probe() +{ + return (m_category == sinsp_threadinfo::CAT_HEALTHCHECK || + m_category == sinsp_threadinfo::CAT_LIVENESS_PROBE || + m_category == sinsp_threadinfo::CAT_READINESS_PROBE); +} + +string sinsp_threadinfo::get_path_for_dir_fd(int64_t dir_fd) +{ + sinsp_fdinfo_t* dir_fdinfo = get_fd(dir_fd); + if (!dir_fdinfo || dir_fdinfo->m_name.empty()) + { +#ifdef HAS_CAPTURE + // Sad day; we don't have the directory in the tinfo's fd cache. + // Must manually look it up so we can resolve filenames correctly. + char proc_path[PATH_MAX]; + char dirfd_path[PATH_MAX]; + int ret; + snprintf(proc_path, + sizeof(proc_path), + "%s/proc/%lld/fd/%lld", + scap_get_host_root(), + (long long)m_pid, + (long long)dir_fd); + + ret = readlink(proc_path, dirfd_path, sizeof(dirfd_path) - 1); + if (ret < 0) + { + g_logger.log("Unable to determine path for file descriptor.", + sinsp_logger::SEV_INFO); + return ""; + } + dirfd_path[ret] = '\0'; + std::string rel_path_base = dirfd_path; + sanitize_string(rel_path_base); + rel_path_base.append("/"); + g_logger.log(std::string("Translating to ") + rel_path_base); + return rel_path_base; +#else + g_logger.log("Can't translate working directory outside of live capture.", + sinsp_logger::SEV_INFO); + return ""; #endif - return NULL; } + return dir_fdinfo->m_name; } -void sinsp_thread_manager::increment_mainthread_childcount(sinsp_threadinfo* threadinfo) +shared_ptr sinsp_threadinfo::lookup_thread() const { - if(threadinfo->m_flags & PPM_CL_CLONE_THREAD) + return m_inspector->get_thread_ref(m_pid, true, true, true); +} + +size_t sinsp_threadinfo::args_len() const +{ + return strvec_len(m_args); +} + +size_t sinsp_threadinfo::env_len() const +{ + return strvec_len(m_env); +} + +size_t sinsp_threadinfo::cgroups_len() const +{ + size_t totlen = 0; + + for(auto &cgroup : m_cgroups) { - // - // Increment the refcount of the main thread so it won't - // be deleted (if it calls pthread_exit()) until we are done - // - ASSERT(threadinfo->m_pid != threadinfo->m_tid); - sinsp_threadinfo* main_thread = m_inspector->get_thread(threadinfo->m_pid, false); - if(main_thread) + totlen += cgroup.first.size() + 1 + cgroup.second.size(); + totlen++; // Trailing NULL + } + + return totlen; +} + +void sinsp_threadinfo::args_to_iovec(struct iovec **iov, int *iovcnt, + std::string &rem) const +{ + return strvec_to_iovec(m_args, + iov, iovcnt, + rem); +} + +void sinsp_threadinfo::env_to_iovec(struct iovec **iov, int *iovcnt, + std::string &rem) const +{ + return strvec_to_iovec(m_env, + iov, iovcnt, + rem); +} + +// Set the provided iovec to the string in str, if it will fit. If it +// won't, copy the portion that will fit to rem and set the iovec to +// rem. Updates alen with the new total length and possibly sets rem +// to any truncated string. +void sinsp_threadinfo::add_to_iovec(const string &str, + const bool include_trailing_null, + struct iovec &iov, + uint32_t &alen, + std::string &rem) const +{ + uint32_t len = str.size() + (include_trailing_null ? 1 : 0); + const char *buf = str.c_str(); + + if(len > alen) + { + // The entire string won't fit. Use rem to hold a + // truncated copy + rem = str.substr(0, alen-1); + buf = rem.c_str(); + len = alen; + } + + iov.iov_base = (void *) buf; + iov.iov_len = len; + + alen -= len; +} + +// iov will be allocated and must be freed. rem is used to hold a +// possibly truncated final argument. +void sinsp_threadinfo::cgroups_to_iovec(struct iovec **iov, int *iovcnt, + std::string &rem) const +{ + uint32_t alen = SCAP_MAX_ARGS_SIZE; + static const string eq = "="; + + // We allocate an iovec big enough to hold all the cgroups and + // intermediate '=' signs. Based on alen, we might not use all + // of the iovec. + *iov = (struct iovec *) malloc((3 * m_cgroups.size()) * sizeof(struct iovec)); + + *iovcnt = 0; + + for(auto it = m_cgroups.begin(); it != m_cgroups.end() && alen > 0; ++it) + { + add_to_iovec(it->first, false, (*iov)[(*iovcnt)++], alen, rem); + if(alen > 0) { - ++main_thread->m_nchilds; + add_to_iovec(eq, false, (*iov)[(*iovcnt)++], alen, rem); } - else + + if(alen > 0) { - ASSERT(false); + add_to_iovec(it->second, true, (*iov)[(*iovcnt)++], alen, rem); } } } -void sinsp_thread_manager::increment_program_childcount(sinsp_threadinfo* threadinfo) +size_t sinsp_threadinfo::strvec_len(const vector &strs) const { - if(threadinfo->is_main_thread()) + size_t totlen = 0; + + for(auto &str : strs) { - sinsp_threadinfo* parent_thread = m_inspector->get_thread(threadinfo->m_ptid, false); + totlen += str.size(); + totlen++; // Trailing NULL + } - if(parent_thread) - { - if((parent_thread->m_comm == threadinfo->m_comm) && - (parent_thread->m_exe == threadinfo->m_exe)) - { - threadinfo->m_progid = parent_thread->m_tid; - ++parent_thread->m_nchilds; - increment_program_childcount(parent_thread); - } - } + return totlen; +} + +// iov will be allocated and must be freed. rem is used to hold a +// possibly truncated final argument. +void sinsp_threadinfo::strvec_to_iovec(const vector &strs, + struct iovec **iov, int *iovcnt, + std::string &rem) const +{ + uint32_t alen = SCAP_MAX_ARGS_SIZE; + + // We allocate an iovec big enough to hold all the entries in + // strs. Based on alen, we might not use all of the iovec. + *iov = (struct iovec *) malloc(strs.size() * sizeof(struct iovec)); + + *iovcnt = 0; + + for(auto it = strs.begin(); it != strs.end() && alen > 0; ++it) + { + add_to_iovec(*it, true, (*iov)[(*iovcnt)++], alen, rem); } } -// Don't set level, it's for internal use -void sinsp_thread_manager::decrement_program_childcount(sinsp_threadinfo* threadinfo, uint32_t level) + +void sinsp_threadinfo::fd_to_scap(scap_fdinfo *dst, sinsp_fdinfo_t* src) { - if(threadinfo->is_main_thread()) + dst->type = src->m_type; + dst->ino = src->m_ino; + + switch(dst->type) { - ASSERT(threadinfo->m_pid != threadinfo->m_progid); + case SCAP_FD_IPV4_SOCK: + dst->info.ipv4info.sip = src->m_sockinfo.m_ipv4info.m_fields.m_sip; + dst->info.ipv4info.dip = src->m_sockinfo.m_ipv4info.m_fields.m_dip; + dst->info.ipv4info.sport = src->m_sockinfo.m_ipv4info.m_fields.m_sport; + dst->info.ipv4info.dport = src->m_sockinfo.m_ipv4info.m_fields.m_dport; + dst->info.ipv4info.l4proto = src->m_sockinfo.m_ipv4info.m_fields.m_l4proto; + break; + case SCAP_FD_IPV4_SERVSOCK: + dst->info.ipv4serverinfo.ip = src->m_sockinfo.m_ipv4serverinfo.m_ip; + dst->info.ipv4serverinfo.port = src->m_sockinfo.m_ipv4serverinfo.m_port; + dst->info.ipv4serverinfo.l4proto = src->m_sockinfo.m_ipv4serverinfo.m_l4proto; + break; + case SCAP_FD_IPV6_SOCK: + copy_ipv6_address(dst->info.ipv6info.sip, src->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b); + copy_ipv6_address(dst->info.ipv6info.dip, src->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b); + dst->info.ipv6info.sport = src->m_sockinfo.m_ipv6info.m_fields.m_sport; + dst->info.ipv6info.dport = src->m_sockinfo.m_ipv6info.m_fields.m_dport; + dst->info.ipv6info.l4proto = src->m_sockinfo.m_ipv6info.m_fields.m_l4proto; + break; + case SCAP_FD_IPV6_SERVSOCK: + copy_ipv6_address(dst->info.ipv6serverinfo.ip, src->m_sockinfo.m_ipv6serverinfo.m_ip.m_b); + dst->info.ipv6serverinfo.port = src->m_sockinfo.m_ipv6serverinfo.m_port; + dst->info.ipv6serverinfo.l4proto = src->m_sockinfo.m_ipv6serverinfo.m_l4proto; + break; + case SCAP_FD_UNIX_SOCK: + dst->info.unix_socket_info.source = src->m_sockinfo.m_unixinfo.m_fields.m_source; + dst->info.unix_socket_info.destination = src->m_sockinfo.m_unixinfo.m_fields.m_dest; + strncpy(dst->info.unix_socket_info.fname, src->m_name.c_str(), SCAP_MAX_PATH_SIZE); + break; + case SCAP_FD_FILE_V2: + dst->info.regularinfo.open_flags = src->m_openflags; + strncpy(dst->info.regularinfo.fname, src->m_name.c_str(), SCAP_MAX_PATH_SIZE); + dst->info.regularinfo.dev = src->m_dev; + dst->info.regularinfo.mount_id = src->m_mount_id; + break; + case SCAP_FD_FIFO: + case SCAP_FD_FILE: + case SCAP_FD_DIRECTORY: + case SCAP_FD_UNSUPPORTED: + case SCAP_FD_SIGNALFD: + case SCAP_FD_EVENTPOLL: + case SCAP_FD_EVENT: + case SCAP_FD_INOTIFY: + case SCAP_FD_TIMERFD: + case SCAP_FD_NETLINK: + strncpy(dst->info.fname, src->m_name.c_str(), SCAP_MAX_PATH_SIZE); + break; + default: + ASSERT(false); + break; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_thread_manager implementation +/////////////////////////////////////////////////////////////////////////////// +sinsp_thread_manager::sinsp_thread_manager(sinsp* inspector) +{ + m_inspector = inspector; + m_listener = NULL; + clear(); +} + +void sinsp_thread_manager::clear() +{ + m_threadtable.clear(); + m_last_tid = 0; + m_last_tinfo.reset(); + m_last_flush_time_ns = 0; + m_n_drops = 0; + +#ifdef GATHER_INTERNAL_STATS + m_failed_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_failed_lookups","Failed thread lookups")); + m_cached_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_cached_lookups","Cached thread lookups")); + m_non_cached_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_non_cached_lookups","Non cached thread lookups")); + m_added_threads = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_added","Number of added threads")); + m_removed_threads = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_removed","Removed threads")); +#endif +} + +void sinsp_thread_manager::set_listener(sinsp_threadtable_listener* listener) +{ + m_listener = listener; +} - sinsp_threadinfo* prog_thread = m_inspector->get_thread(threadinfo->m_progid, false); +void sinsp_thread_manager::increment_mainthread_childcount(sinsp_threadinfo* threadinfo) +{ + if(threadinfo->m_flags & PPM_CL_CLONE_THREAD) + { + // + // Increment the refcount of the main thread so it won't + // be deleted (if it calls pthread_exit()) until we are done + // + ASSERT(threadinfo->m_pid != threadinfo->m_tid); - if(prog_thread) + sinsp_threadinfo* main_thread = m_inspector->get_thread(threadinfo->m_pid, true, true); + if(main_thread) { - if(prog_thread->m_nchilds > 0) - { - --prog_thread->m_nchilds; - decrement_program_childcount(prog_thread, level + 1); - } - else - { - ASSERT(false); - } + ++main_thread->m_nchilds; } - - if(level == 0) + else { - threadinfo->m_progid = -1LL; - threadinfo->m_main_program_thread = NULL; + ASSERT(false); } } } -void sinsp_thread_manager::add_thread(sinsp_threadinfo& threadinfo, bool from_scap_proctable) +bool sinsp_thread_manager::add_thread(sinsp_threadinfo *threadinfo, bool from_scap_proctable) { #ifdef GATHER_INTERNAL_STATS m_added_threads->increment(); #endif - if(m_threadtable.size() >= m_inspector->m_max_thread_table_size) + m_last_tinfo.reset(); + + if (m_threadtable.size() >= m_inspector->m_max_thread_table_size +#if defined(HAS_CAPTURE) + && threadinfo->m_pid != m_inspector->m_sysdig_pid +#endif + ) { + // rate limit messages to avoid spamming the logs + if (m_n_drops % m_inspector->m_max_thread_table_size == 0) + { + g_logger.format(sinsp_logger::SEV_INFO, "Thread table full, dropping tid %lu (pid %lu, comm \"%s\")", + threadinfo->m_tid, threadinfo->m_pid, threadinfo->m_comm.c_str()); + } m_n_drops++; - return; + return false; } if(!from_scap_proctable) { - increment_mainthread_childcount(&threadinfo); - increment_program_childcount(&threadinfo); + increment_mainthread_childcount(threadinfo); } - sinsp_threadinfo& newentry = (m_threadtable[threadinfo.m_tid] = threadinfo); - newentry.allocate_private_state(); + threadinfo->compute_program_hash(); + threadinfo->allocate_private_state(); + m_threadtable.put(threadinfo); + if(m_listener) { - m_listener->on_thread_created(&newentry); + m_listener->on_thread_created(threadinfo); } + return true; } -void sinsp_thread_manager::remove_thread(int64_t tid) +void sinsp_thread_manager::remove_thread(int64_t tid, bool force) { - remove_thread(m_threadtable.find(tid)); -} + uint64_t nchilds; + sinsp_threadinfo* tinfo = m_threadtable.get(tid); -void sinsp_thread_manager::remove_thread(threadinfo_map_iterator_t it) -{ - if(it == m_threadtable.end()) + if(tinfo == nullptr) { // // Looks like there's no thread to remove. @@ -763,46 +1320,47 @@ void sinsp_thread_manager::remove_thread(threadinfo_map_iterator_t it) #endif return; } - else if(it->second.m_nchilds == 0) + else if((nchilds = tinfo->m_nchilds) == 0 || force) { // // Decrement the refcount of the main thread/program because // this reference is gone // - if(it->second.m_flags & PPM_CL_CLONE_THREAD) + if(tinfo->m_flags & PPM_CL_CLONE_THREAD) { - ASSERT(it->second.m_pid != it->second.m_tid); - sinsp_threadinfo* main_thread = m_inspector->get_thread(it->second.m_pid, false); + ASSERT(tinfo->m_pid != tinfo->m_tid); + sinsp_threadinfo* main_thread = m_inspector->get_thread(tinfo->m_pid, false, true); if(main_thread) { - ASSERT(main_thread->m_nchilds); - --main_thread->m_nchilds; + if(main_thread->m_nchilds > 0) + { + --main_thread->m_nchilds; + } + else + { + ASSERT(false); + } } else { ASSERT(false); } } - else if(it->second.m_progid != -1LL) - { - decrement_program_childcount(&it->second); - } // // If this is the main thread of a process, erase all the FDs that the process owns // - if(it->second.m_pid == it->second.m_tid) + if(tinfo->m_pid == tinfo->m_tid) { - unordered_map fdtable = it->second.get_fd_table()->m_table; + unordered_map* fdtable = &(tinfo->get_fd_table()->m_table); unordered_map::iterator fdit; erase_fd_params eparams; eparams.m_remove_from_table = false; - eparams.m_inspector = m_inspector; - eparams.m_tinfo = &(it->second); + eparams.m_tinfo = tinfo; eparams.m_ts = m_inspector->m_lastevent_ts; - for(fdit = fdtable.begin(); fdit != fdtable.end(); ++fdit) + for(fdit = fdtable->begin(); fdit != fdtable->end(); ++fdit) { eparams.m_fd = fdit->first; @@ -821,63 +1379,81 @@ void sinsp_thread_manager::remove_thread(threadinfo_map_iterator_t it) // Reset the cache // m_last_tid = 0; - m_last_tinfo = NULL; + m_last_tinfo.reset(); #ifdef GATHER_INTERNAL_STATS m_removed_threads->increment(); #endif - m_threadtable.erase(it); + m_threadtable.erase(tid); + + // + // If the thread has a nonzero refcount, it means that we are forcing the removal + // of a main process or program that some child refer to. + // We need to recalculate the child relationships, or the table will become + // corrupted. + // + if(nchilds != 0) + { + recreate_child_dependencies(); + } } } -void sinsp_thread_manager::remove_inactive_threads() +void sinsp_thread_manager::fix_sockets_coming_from_proc() +{ + m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + tinfo.fix_sockets_coming_from_proc(); + return true; + }); +} + +void sinsp_thread_manager::clear_thread_pointers(sinsp_threadinfo& tinfo) { - if(m_last_flush_time_ns == 0) + tinfo.m_main_thread.reset(); + + sinsp_fdtable* fdt = tinfo.get_fd_table(); + if(fdt != NULL) { - m_last_flush_time_ns = m_inspector->m_lastevent_ts; + fdt->reset_cache(); } +} - if(m_inspector->m_lastevent_ts > - m_last_flush_time_ns + m_inspector->m_inactive_thread_scan_time_ns) - { - m_last_flush_time_ns = m_inspector->m_lastevent_ts; +/* +void sinsp_thread_manager::clear_thread_pointers(threadinfo_map_iterator_t it) +{ + it->second.m_main_program_thread = NULL; + it->second.m_main_thread = NULL; + it->second.m_progid = -1LL; + it->second.m_fdtable.reset_cache(); +} +*/ - for(threadinfo_map_iterator_t it = m_threadtable.begin(); it != m_threadtable.end();) - { - if(it->second.m_nchilds == 0 && - m_inspector->m_lastevent_ts > - it->second.m_lastaccess_ts + m_inspector->m_thread_timeout_ns) - { - // - // Reset the cache - // - m_last_tid = 0; - m_last_tinfo = NULL; +void sinsp_thread_manager::reset_child_dependencies() +{ + m_last_tinfo.reset(); + m_last_tid = 0; -#ifdef GATHER_INTERNAL_STATS - m_removed_threads->increment(); -#endif - m_threadtable.erase(it++); - } - else - { - ++it; - } - } - } + m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + tinfo.m_nchilds = 0; + clear_thread_pointers(tinfo); + return true; + }); } -void sinsp_thread_manager::fix_sockets_coming_from_proc() +void sinsp_thread_manager::create_child_dependencies() { - threadinfo_map_iterator_t it; - for(it = m_threadtable.begin(); - it != m_threadtable.end(); ++it) - { - it->second.fix_sockets_coming_from_proc(); - } + m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + increment_mainthread_childcount(&tinfo); + return true; + }); } +void sinsp_thread_manager::recreate_child_dependencies() +{ + reset_child_dependencies(); + create_child_dependencies(); +} void sinsp_thread_manager::update_statistics() { @@ -891,3 +1467,207 @@ void sinsp_thread_manager::update_statistics() } #endif } + +void sinsp_thread_manager::free_dump_fdinfos(vector* fdinfos_to_free) +{ + for(uint32_t j = 0; j < fdinfos_to_free->size(); j++) + { + free(fdinfos_to_free->at(j)); + } + + fdinfos_to_free->clear(); +} + +// NOTE: This does *not* populate any array-based fields (comm, exe, +// exepath, args, env, cwd, cgroups, root) +void sinsp_thread_manager::thread_to_scap(sinsp_threadinfo& tinfo, scap_threadinfo* sctinfo) +{ + // + // Fill in the thread data + // + + // NOTE: This is doing a shallow copy of the strings from + // tinfo, and is valid only as long as tinfo is valid. + + sctinfo->tid = tinfo.m_tid; + sctinfo->pid = tinfo.m_pid; + sctinfo->ptid = tinfo.m_ptid; + sctinfo->sid = tinfo.m_sid; + sctinfo->vpgid = tinfo.m_vpgid; + + sctinfo->flags = tinfo.m_flags ; + sctinfo->fdlimit = tinfo.m_fdlimit; + sctinfo->uid = tinfo.m_uid; + sctinfo->gid = tinfo.m_gid; + sctinfo->vmsize_kb = tinfo.m_vmsize_kb; + sctinfo->vmrss_kb = tinfo.m_vmrss_kb; + sctinfo->vmswap_kb = tinfo.m_vmswap_kb; + sctinfo->pfmajor = tinfo.m_pfmajor; + sctinfo->pfminor = tinfo.m_pfminor; + sctinfo->vtid = tinfo.m_vtid; + sctinfo->vpid = tinfo.m_vpid; + sctinfo->fdlist = NULL; + sctinfo->loginuid = tinfo.m_loginuid; + sctinfo->filtered_out = false; +} + +void sinsp_thread_manager::dump_threads_to_file(scap_dumper_t* dumper) +{ + // + // First pass of the table to calculate the lengths + // + uint32_t totlen = 0; + + vector lengths; + + m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + uint32_t il = (uint32_t) + (sizeof(uint32_t) + // len + sizeof(uint64_t) + // tid + sizeof(uint64_t) + // pid + sizeof(uint64_t) + // ptid + sizeof(uint64_t) + // sid + sizeof(uint64_t) + // pgid + 2 + MIN(tinfo.m_comm.size(), SCAP_MAX_PATH_SIZE) + + 2 + MIN(tinfo.m_exe.size(), SCAP_MAX_PATH_SIZE) + + 2 + MIN(tinfo.m_exepath.size(), SCAP_MAX_PATH_SIZE) + + 2 + MIN(tinfo.args_len(), SCAP_MAX_ARGS_SIZE) + + // 1 is sizeof("/") + 2 + MIN((tinfo.m_cwd == "")? 1 : tinfo.m_cwd.size(), SCAP_MAX_PATH_SIZE) + + sizeof(uint64_t) + // fdlimit + sizeof(uint32_t) + // flags + sizeof(uint32_t) + // uid + sizeof(uint32_t) + // gid + sizeof(uint32_t) + // vmsize_kb + sizeof(uint32_t) + // vmrss_kb + sizeof(uint32_t) + // vmswap_kb + sizeof(uint64_t) + // pfmajor + sizeof(uint64_t) + // pfminor + 2 + MIN(tinfo.env_len(), SCAP_MAX_ENV_SIZE) + + sizeof(int64_t) + // vtid + sizeof(int64_t) + // vpid + 2 + MIN(tinfo.cgroups_len(), SCAP_MAX_CGROUPS_SIZE) + + 2 + MIN(tinfo.m_root.size(), SCAP_MAX_PATH_SIZE)) + + sizeof(uint32_t); // loginuid + + lengths.push_back(il); + totlen += il; + return true; + }); + + // + // Second pass of the table to dump the Threads + // + if(scap_write_proclist_header(m_inspector->m_h, dumper, totlen) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); + } + + uint32_t idx = 0; + m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + scap_threadinfo *sctinfo; + struct iovec *args_iov, *envs_iov, *cgroups_iov; + int argscnt, envscnt, cgroupscnt; + string argsrem, envsrem, cgroupsrem; + + if((sctinfo = scap_proc_alloc(m_inspector->m_h)) == NULL) + { + throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); + } + + thread_to_scap(tinfo, sctinfo); + tinfo.args_to_iovec(&args_iov, &argscnt, argsrem); + tinfo.env_to_iovec(&envs_iov, &envscnt, envsrem); + tinfo.cgroups_to_iovec(&cgroups_iov, &cgroupscnt, cgroupsrem); + + if(scap_write_proclist_entry_bufs(m_inspector->m_h, dumper, sctinfo, lengths[idx++], + tinfo.m_comm.c_str(), + tinfo.m_exe.c_str(), + tinfo.m_exepath.c_str(), + args_iov, argscnt, + envs_iov, envscnt, + (tinfo.m_cwd == "" ? "/" : tinfo.m_cwd.c_str()), + cgroups_iov, cgroupscnt, + tinfo.m_root.c_str()) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); + } + + free(args_iov); + free(envs_iov); + free(cgroups_iov); + + scap_proc_free(m_inspector->m_h, sctinfo); + return true; + }); + + if(scap_write_proclist_trailer(m_inspector->m_h, dumper, totlen) != SCAP_SUCCESS) + { + throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); + } + + // + // Third pass of the table to dump the FDs + // + + m_threadtable.loop([&] (sinsp_threadinfo& tinfo) { + scap_threadinfo *sctinfo; + + if((sctinfo = scap_proc_alloc(m_inspector->m_h)) == NULL) + { + throw sinsp_exception(scap_getlasterr(m_inspector->m_h)); + } + + // Note: as scap_fd_add/scap_write_proc_fds do not use + // any of the array-based fields like comm, etc. a + // shallow copy is safe + thread_to_scap(tinfo, sctinfo); + + if(tinfo.is_main_thread()) + { + // + // Add the FDs + // + unordered_map& fdtable = tinfo.get_fd_table()->m_table; + for(auto it = fdtable.begin(); it != fdtable.end(); ++it) + { + // + // Allocate the scap fd info + // + scap_fdinfo* scfdinfo = (scap_fdinfo*)malloc(sizeof(scap_fdinfo)); + if(scfdinfo == NULL) + { + scap_proc_free(m_inspector->m_h, sctinfo); + throw sinsp_exception("thread memory allocation error in sinsp_thread_manager::to_scap"); + } + + // + // Populate the fd info + // + scfdinfo->fd = it->first; + tinfo.fd_to_scap(scfdinfo, &it->second); + + // + // Add the new fd to the scap table. + // + if(scap_fd_add(m_inspector->m_h, sctinfo, it->first, scfdinfo) != SCAP_SUCCESS) + { + scap_proc_free(m_inspector->m_h, sctinfo); + throw sinsp_exception("error calling scap_fd_add in sinsp_thread_manager::to_scap (" + string(scap_getlasterr(m_inspector->m_h)) + ")"); + } + } + } + + // + // Dump the thread to disk + // + if(scap_write_proc_fds(m_inspector->m_h, sctinfo, dumper) != SCAP_SUCCESS) + { + scap_proc_free(m_inspector->m_h, sctinfo); + throw sinsp_exception("error calling scap_proc_add in sinsp_thread_manager::to_scap (" + string(scap_getlasterr(m_inspector->m_h)) + ")"); + } + + scap_proc_free(m_inspector->m_h, sctinfo); + return true; + }); +} diff --git a/userspace/libsinsp/threadinfo.h b/userspace/libsinsp/threadinfo.h index 045b2c22e3..2403b4241a 100644 --- a/userspace/libsinsp/threadinfo.h +++ b/userspace/libsinsp/threadinfo.h @@ -1,19 +1,20 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once @@ -22,21 +23,36 @@ along with sysdig. If not, see . #define VISIBILITY_PRIVATE private: #endif +#ifdef _WIN32 +struct iovec { + void *iov_base; /* Starting address */ + size_t iov_len; /* Number of bytes to transfer */ +}; +#else +#include +#endif + +#include +#include +#include +#include "fdinfo.h" +#include "internal_metrics.h" + class sinsp_delays_info; class sinsp_threadtable_listener; -class thread_analyzer_info; +class sinsp_tracerparser; +class blprogram; typedef struct erase_fd_params { bool m_remove_from_table; - sinsp* m_inspector; int64_t m_fd; sinsp_threadinfo* m_tinfo; sinsp_fdinfo_t* m_fdinfo; uint64_t m_ts; }erase_fd_params; -/** @defgroup state State management +/** @defgroup state State management * @{ */ @@ -46,7 +62,7 @@ typedef struct erase_fd_params manipulate threads and retrieve thread information. \note As a library user, you won't need to construct thread objects. Rather, - you get them by calling \ref sinsp_evt::get_thread_info or + you get them by calling \ref sinsp_evt::get_thread_info or \ref sinsp::get_thread. \note sinsp_threadinfo is also used to keep process state. For the sinsp library, a process is just a thread with TID=PID. @@ -54,34 +70,88 @@ typedef struct erase_fd_params class SINSP_PUBLIC sinsp_threadinfo { public: - sinsp_threadinfo(); - sinsp_threadinfo(sinsp *inspector); - ~sinsp_threadinfo(); + sinsp_threadinfo(sinsp *inspector = nullptr); + virtual ~sinsp_threadinfo(); /*! \brief Return the name of the process containing this thread, e.g. "top". */ - string get_comm(); + std::string get_comm() const; /*! - \brief Return the full name of the process containing this thread, e.g. "/bin/top". + \brief Return the name of the process containing this thread from argv[0], e.g. "/bin/top". */ - string get_exe(); + std::string get_exe() const; + + /*! + \brief Return the full executable path of the process containing this thread, e.g. "/bin/top". + */ + std::string get_exepath() const; /*! \brief Return the working directory of the process containing this thread. */ - string get_cwd(); + std::string get_cwd(); + + /*! + \brief Return the values of all environment variables for the process + containing this thread. + */ + const std::vector& get_env(); + + /*! + \brief Return the value of the specified environment variable for the process + containing this thread. Returns empty string if variable is not found. + */ + std::string get_env(const std::string& name); /*! \brief Return true if this is a process' main thread. */ - bool is_main_thread(); + inline bool is_main_thread() const + { + return m_tid == m_pid; + } /*! \brief Get the main thread of the process containing this thread. */ - sinsp_threadinfo* get_main_thread(); + inline sinsp_threadinfo* get_main_thread() const + { + auto main_thread = m_main_thread.lock(); + if(!main_thread) + { + // + // Is this a child thread? + // + if(m_pid == m_tid) + { + // + // No, this is either a single thread process or the root thread of a + // multithread process. + // Note: we don't set m_main_thread because there are cases in which this is + // invoked for a threadinfo that is in the stack. Caching the this pointer + // would cause future mess. + // + return const_cast(this); + } + else + { + // + // Yes, this is a child thread. Find the process root thread. + // + auto ptinfo = lookup_thread(); + if (!ptinfo) + { + return NULL; + } + m_main_thread = ptinfo; + return &*ptinfo; + } + } + + return &*main_thread; + } /*! \brief Get the process that launched this thread's process. @@ -89,14 +159,37 @@ class SINSP_PUBLIC sinsp_threadinfo sinsp_threadinfo* get_parent_thread(); /*! - \brief Retrive information about one of this thread/process FDs. + \brief Retrieve information about one of this thread/process FDs. \param fd The file descriptor number, e.g. 0 for stdin. \return Pointer to the FD information, or NULL if the given FD doesn't exist */ - sinsp_fdinfo_t* get_fd(int64_t fd); + inline sinsp_fdinfo_t* get_fd(int64_t fd) + { + if(fd < 0) + { + return NULL; + } + + sinsp_fdtable* fdt = get_fd_table(); + + if(fdt) + { + sinsp_fdinfo_t *fdinfo = fdt->find(fd); + if(fdinfo) + { + // Its current name is now its old + // name. The name might change as a + // result of parsing. + fdinfo->m_oldname = fdinfo->m_name; + return fdinfo; + } + } + + return NULL; + } /*! \brief Return true if this thread is bound to the given server port. @@ -110,33 +203,125 @@ class SINSP_PUBLIC sinsp_threadinfo void* get_private_state(uint32_t id); + /*! + \brief Return the ratio between open FDs and maximum available FDs for this thread. + */ + uint64_t get_fd_usage_pct(); + double get_fd_usage_pct_d(); + + /*! + \brief Return the number of open FDs for this thread. + */ + uint64_t get_fd_opencount() const; + + /*! + \brief Return the maximum number of FDs this thread can open. + */ + uint64_t get_fd_limit(); + + /*! + \brief Return the cgroup name for a specific subsystem + */ + const std::string& get_cgroup(const std::string& subsys) const; + + // + // Walk up the parent process hierarchy, calling the provided + // function for each node. If the function returns false, the + // traversal stops. + // + typedef std::function visitor_func_t; + void traverse_parent_state(visitor_func_t &visitor); + + static void populate_cmdline(std::string &cmdline, sinsp_threadinfo *tinfo); + + // Return true if this thread is a part of a healthcheck, + // readiness probe, or liveness probe. + bool is_health_probe(); + + /*! + \brief Translate a directory's file descriptor into its path + \param dir_fd A file descriptor for a directory + \return A path (or "" if failure) + */ + std::string get_path_for_dir_fd(int64_t dir_fd); + // // Core state // int64_t m_tid; ///< The id of this thread int64_t m_pid; ///< The id of the process containing this thread. In single thread threads, this is equal to tid. int64_t m_ptid; ///< The id of the process that started this thread. - int64_t m_progid; ///< Main program id. If this process is part of a logical group of processes (e.g. it's one of the apache processes), the tid of the process that is the head of this group. - string m_comm; ///< Command name (e.g. "top") - string m_exe; ///< Full command name (e.g. "/bin/top") - vector m_args; ///< Command line arguments (e.g. "-d1") + int64_t m_sid; ///< The session id of the process containing this thread. + std::string m_comm; ///< Command name (e.g. "top") + std::string m_exe; ///< argv[0] (e.g. "sshd: user@pts/4") + std::string m_exepath; ///< full executable path + std::vector m_args; ///< Command line arguments (e.g. "-d1") + std::vector m_env; ///< Environment variables + std::vector> m_cgroups; ///< subsystem-cgroup pairs + std::string m_container_id; ///< heuristic-based container id uint32_t m_flags; ///< The thread flags. See the PPM_CL_* declarations in ppm_events_public.h. int64_t m_fdlimit; ///< The maximum number of FDs this thread can open - uint32_t m_fd_usage_pct; ///< The ratio between open FDs and maximum available FDs for this thread uint32_t m_uid; ///< user id uint32_t m_gid; ///< group id uint64_t m_nchilds; ///< When this is 0 the process can be deleted + uint32_t m_vmsize_kb; ///< total virtual memory (as kb). + uint32_t m_vmrss_kb; ///< resident non-swapped memory (as kb). + uint32_t m_vmswap_kb; ///< swapped memory (as kb). + uint64_t m_pfmajor; ///< number of major page faults since start. + uint64_t m_pfminor; ///< number of minor page faults since start. + int64_t m_vtid; ///< The virtual id of this thread. + int64_t m_vpid; ///< The virtual id of the process containing this thread. In single thread threads, this is equal to vtid. + int64_t m_vpgid; // The virtual process group id, as seen from its pid namespace + std::string m_root; + size_t m_program_hash; ///< Unique hash of the current program + size_t m_program_hash_scripts; ///< Unique hash of the current program, including arguments for scripting programs (like python or ruby) + int32_t m_tty; + int32_t m_loginuid; ///< loginuid (auid) + + // In some cases, a threadinfo has a category that identifies + // why it was run. Descriptions: + // CAT_NONE: no specific category + // CAT_CONTAINER: a process run in a container and *not* any + // of the following more specific categories. + // CAT_HEALTHCHECK: part of a container healthcheck + // CAT_LIVENESS_PROBE: part of a k8s liveness probe + // CAT_READINESS_PROBE: part of a k8s readiness probe + enum command_category { + CAT_NONE = 0, + CAT_CONTAINER, + CAT_HEALTHCHECK, + CAT_LIVENESS_PROBE, + CAT_READINESS_PROBE + }; + + command_category m_category; // // State for multi-event processing // - int64_t m_lastevent_fd; ///< The FD os the last event generated by this thread. + int64_t m_lastevent_fd; ///< The FD os the last event used by this thread. uint64_t m_lastevent_ts; ///< timestamp of the last event for this thread. uint64_t m_prevevent_ts; ///< timestamp of the event before the last for this thread. - uint64_t m_lastaccess_ts; ///< The last time this thread was looked up. Used when cleaning up the table. - uint64_t m_clone_ts; ///< When the clone that started this process happened. + uint64_t m_lastaccess_ts; ///< The last time this thread was looked up. Used when cleaning up the table. + uint64_t m_clone_ts; ///< When the clone that started this process happened. + + // + // Parser for the user events. Public so that filter fields can access it + // + sinsp_tracerparser* m_tracer_parser; + + size_t args_len() const; + size_t env_len() const; + size_t cgroups_len() const; + + void args_to_iovec(struct iovec **iov, int *iovcnt, + std::string &rem) const; - thread_analyzer_info* m_ainfo; + void env_to_iovec(struct iovec **iov, int *iovcnt, + std::string &rem) const; + + void cgroups_to_iovec(struct iovec **iov, int *iovcnt, + std::string &rem) const; #ifdef HAS_FILTERING // @@ -151,20 +336,93 @@ class SINSP_PUBLIC sinsp_threadinfo // sinsp *m_inspector; +public: // types required for use in sets + struct hasher { + size_t operator()(sinsp_threadinfo* tinfo) const + { + return tinfo->get_main_thread()->m_program_hash; + } + }; + + struct comparer { + size_t operator()(sinsp_threadinfo* lhs, sinsp_threadinfo* rhs) const + { + return lhs->get_main_thread()->m_program_hash == rhs->get_main_thread()->m_program_hash; + } + }; + +protected: + inline sinsp_fdtable* get_fd_table() + { + if(!(m_flags & PPM_CL_CLONE_FILES)) + { + return &m_fdtable;; + } + else + { + sinsp_threadinfo* root = get_main_thread(); + return (root == nullptr) ? nullptr : &(root->m_fdtable); + } + } + +#ifndef _WIN32 + inline const sinsp_fdtable* get_fd_table() const + { + if(!(m_flags & PPM_CL_CLONE_FILES)) + { + return &m_fdtable;; + } + else + { + sinsp_threadinfo* root = get_main_thread(); + return (root == nullptr) ? nullptr : &(root->m_fdtable); + } + } +#endif + +public: VISIBILITY_PRIVATE void init(); - void init(const scap_threadinfo* pi); + // return true if, based on the current inspector filter, this thread should be kept + void init(scap_threadinfo* pi); void fix_sockets_coming_from_proc(); sinsp_fdinfo_t* add_fd(int64_t fd, sinsp_fdinfo_t *fdinfo); + void add_fd_from_scap(scap_fdinfo *fdinfo, OUT sinsp_fdinfo_t *res); void remove_fd(int64_t fd); - sinsp_fdtable* get_fd_table(); void set_cwd(const char *cwd, uint32_t cwdlen); sinsp_threadinfo* get_cwd_root(); void set_args(const char* args, size_t len); - void store_event(sinsp_evt *evt); + void set_env(const char* env, size_t len); + bool set_env_from_proc(); + void set_cgroups(const char* cgroups, size_t len); bool is_lastevent_data_valid(); - void set_lastevent_data_validity(bool isvalid); + inline void set_lastevent_data_validity(bool isvalid) + { + if(isvalid) + { + m_lastevent_cpuid = (uint16_t)1; + } + else + { + m_lastevent_cpuid = (uint16_t) - 1; + } + } void allocate_private_state(); + void compute_program_hash(); + std::shared_ptr lookup_thread() const; + + size_t strvec_len(const std::vector &strs) const; + void strvec_to_iovec(const std::vector &strs, + struct iovec **iov, int *iovcnt, + std::string &rem) const; + + void add_to_iovec(const std::string &str, + const bool include_trailing_null, + struct iovec &iov, + uint32_t &alen, + std::string &rem) const; + + void fd_to_scap(scap_fdinfo *dst, sinsp_fdinfo_t* src); // void push_fdop(sinsp_fdop* op); // the queue of recent fd operations @@ -175,15 +433,16 @@ VISIBILITY_PRIVATE // parent thread info // sinsp_fdtable m_fdtable; // The fd table of this thread - string m_cwd; // current working directory - sinsp_threadinfo* m_main_thread; - sinsp_threadinfo* m_main_program_thread; - uint8_t m_lastevent_data[SP_EVT_BUF_SIZE]; // Used by some event parsers to store the last enter event - vector m_private_state; + std::string m_cwd; // current working directory + mutable std::weak_ptr m_main_thread; + uint8_t* m_lastevent_data; // Used by some event parsers to store the last enter event + std::vector m_private_state; uint16_t m_lastevent_type; uint16_t m_lastevent_cpuid; sinsp_evt::category m_lastevent_category; + bool m_parent_loop_detected; + blprogram* m_blprogram; friend class sinsp; friend class sinsp_parser; @@ -192,13 +451,87 @@ VISIBILITY_PRIVATE friend class sinsp_evt; friend class sinsp_thread_manager; friend class sinsp_transaction_table; - friend class thread_analyzer_info; + friend class sinsp_tracerparser; + friend class lua_cbacks; + friend class sinsp_baseliner; }; /*@}*/ -typedef unordered_map threadinfo_map_t; -typedef threadinfo_map_t::iterator threadinfo_map_iterator_t; +class threadinfo_map_t +{ +public: + typedef std::function const_visitor_t; + typedef std::function visitor_t; + typedef std::shared_ptr ptr_t; + + inline void put(sinsp_threadinfo* tinfo) + { + m_threads[tinfo->m_tid] = ptr_t(tinfo); + } + + inline sinsp_threadinfo* get(uint64_t tid) + { + auto it = m_threads.find(tid); + if (it == m_threads.end()) + { + return nullptr; + } + return it->second.get(); + } + + inline ptr_t get_ref(uint64_t tid) + { + auto it = m_threads.find(tid); + if (it == m_threads.end()) + { + return nullptr; + } + return it->second; + } + + inline void erase(uint64_t tid) + { + m_threads.erase(tid); + } + + inline void clear() + { + m_threads.clear(); + } + + bool const_loop(const_visitor_t callback) const + { + for (const auto& it : m_threads) + { + if (!callback(*it.second.get())) + { + return false; + } + } + return true; + } + + bool loop(visitor_t callback) + { + for (auto& it : m_threads) + { + if (!callback(*it.second.get())) + { + return false; + } + } + return true; + } + + inline size_t size() const + { + return m_threads.size(); + } + +protected: + std::unordered_map m_threads; +}; /////////////////////////////////////////////////////////////////////////////// @@ -213,16 +546,16 @@ class sinsp_thread_privatestate_manager uint32_t reserve(uint32_t size) { m_memory_sizes.push_back(size); - return m_memory_sizes.size() - 1; + return (uint32_t)m_memory_sizes.size() - 1; } uint32_t get_size() { - return m_memory_sizes.size(); + return (uint32_t)m_memory_sizes.size(); } private: - vector m_memory_sizes; + std::vector m_memory_sizes; friend class sinsp_threadinfo; }; @@ -237,16 +570,21 @@ class SINSP_PUBLIC sinsp_thread_manager void clear(); void set_listener(sinsp_threadtable_listener* listener); - sinsp_threadinfo* get_thread(int64_t tid); - void add_thread(sinsp_threadinfo& threadinfo, bool from_scap_proctable=false); - void remove_thread(int64_t tid); - void remove_thread(threadinfo_map_iterator_t it); - void remove_inactive_threads(); + bool add_thread(sinsp_threadinfo *threadinfo, bool from_scap_proctable); + void remove_thread(int64_t tid, bool force); + // Returns true if the table is actually scanned + // NOTE: this is implemented in sinsp.cpp so we can inline it from there + inline bool remove_inactive_threads(); void fix_sockets_coming_from_proc(); + void reset_child_dependencies(); + void create_child_dependencies(); + void recreate_child_dependencies(); + + void dump_threads_to_file(scap_dumper_t* dumper); uint32_t get_thread_count() { - return m_threadtable.size(); + return (uint32_t)m_threadtable.size(); } void update_statistics(); @@ -256,21 +594,20 @@ class SINSP_PUBLIC sinsp_thread_manager return &m_threadtable; } - set m_server_ports; + std::set m_server_ports; private: void increment_mainthread_childcount(sinsp_threadinfo* threadinfo); - void increment_program_childcount(sinsp_threadinfo* threadinfo); - // Don't set level, it's for internal use - void decrement_program_childcount(sinsp_threadinfo* threadinfo, uint32_t level = 0); + inline void clear_thread_pointers(sinsp_threadinfo& threadinfo); + void free_dump_fdinfos(std::vector* fdinfos_to_free); + void thread_to_scap(sinsp_threadinfo& tinfo, scap_threadinfo* sctinfo); sinsp* m_inspector; threadinfo_map_t m_threadtable; int64_t m_last_tid; - sinsp_threadinfo* m_last_tinfo; + std::weak_ptr m_last_tinfo; uint64_t m_last_flush_time_ns; uint32_t m_n_drops; - uint32_t m_n_proc_lookups; sinsp_threadtable_listener* m_listener; @@ -284,4 +621,5 @@ class SINSP_PUBLIC sinsp_thread_manager friend class sinsp_analyzer; friend class sinsp; friend class sinsp_threadinfo; + friend class sinsp_baseliner; }; diff --git a/userspace/libsinsp/token_bucket.cpp b/userspace/libsinsp/token_bucket.cpp new file mode 100644 index 0000000000..420b0971a9 --- /dev/null +++ b/userspace/libsinsp/token_bucket.cpp @@ -0,0 +1,92 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 + +#include "sinsp.h" +#include "utils.h" +#include "token_bucket.h" + +token_bucket::token_bucket() +{ + init(1, 1); +} + +token_bucket::~token_bucket() +{ +} + +void token_bucket::init(double rate, double max_tokens, uint64_t now) +{ + m_rate = rate; + m_max_tokens = max_tokens; + m_tokens = max_tokens; + + if(now == 0) + { + now = sinsp_utils::get_current_time_ns(); + } + + m_last_seen = now; +} + +bool token_bucket::claim() +{ + uint64_t now = sinsp_utils::get_current_time_ns(); + + return claim(1, now); +} + +bool token_bucket::claim(double tokens, uint64_t now) +{ + double tokens_gained = m_rate * ((now - m_last_seen) / (1000000000.0)); + m_last_seen = now; + + m_tokens += tokens_gained; + + // + // Cap at max_tokens + // + if(m_tokens > m_max_tokens) + { + m_tokens = m_max_tokens; + } + + // + // If m_tokens is < tokens, can't claim. + // + if(m_tokens < tokens) + { + return false; + } + + m_tokens -= tokens; + + return true; +} + +double token_bucket::get_tokens() +{ + return m_tokens; +} + +uint64_t token_bucket::get_last_seen() +{ + return m_last_seen; +} diff --git a/userspace/libsinsp/token_bucket.h b/userspace/libsinsp/token_bucket.h new file mode 100644 index 0000000000..43f8eb7f5e --- /dev/null +++ b/userspace/libsinsp/token_bucket.h @@ -0,0 +1,78 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include + +// A simple token bucket that accumulates tokens at a fixed rate and allows +// for limited bursting in the form of "banked" tokens. + +class token_bucket +{ +public: + token_bucket(); + virtual ~token_bucket(); + + // + // Initialize the token bucket and start accumulating tokens + // + void init(double rate, double max_tokens, uint64_t now = 0); + + // + // Try to claim tokens tokens from the token bucket, using a + // timestamp of now. Returns true if the tokens could be + // claimed. Also updates internal metrics. + // + bool claim(double tokens, uint64_t now); + + // Simpler version of claim that claims a single token and + // uses the current time for now + bool claim(); + + // Return the current number of tokens available + double get_tokens(); + + // Return the last time someone tried to claim a token. + uint64_t get_last_seen(); + +private: + + // + // The number of tokens generated per second. + // + double m_rate; + + // + // The maximum number of tokens that can be banked for future + // claim()s. + // + double m_max_tokens; + + // + // The current number of tokens + // + double m_tokens; + + // + // The last time claim() was called (or the object was created). + // Nanoseconds since the epoch. + // + uint64_t m_last_seen; +}; diff --git a/userspace/libsinsp/tracer_emitter.cpp b/userspace/libsinsp/tracer_emitter.cpp new file mode 100644 index 0000000000..f3236e869b --- /dev/null +++ b/userspace/libsinsp/tracer_emitter.cpp @@ -0,0 +1,212 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "tracer_emitter.h" +#include "sinsp.h" +#include "sinsp_int.h" +#include "analyzer_utils.h" +#include +#include +#include + +thread_local int tls_fd = -1; + +// Helper class to allow multiple tracer_emitter instances to +// share a single connection to /dev/null. Multiple threads can +// write safely and without locking by storing the /dev/null +// fd in thread local storage. Locking is only required when +// the fd is created or destroyed, and that should only happen +// at startup. +class tracer_writer +{ +public: + tracer_writer() {} + ~tracer_writer() { close_fd(); } + + void write(const std::string &trc); + +private: + int get_fd(); + void close_fd(); + + int m_fd = -1; + run_on_interval m_open_interval = 60 * ONE_SECOND_IN_NS; + std::mutex m_fd_lock; + constexpr static const char *m_file = "/dev/null"; +}; + +void tracer_writer::write(const std::string &trc) +{ + if (tls_fd < 0) + { + tls_fd = get_fd(); + if (tls_fd < 0) + { + // Something is wrong with /dev/null, + // so all writes are going to drop + return; + } + } + ASSERT(tls_fd >= 0); + + // Writes to /dev/null should always succeed. + // Still, error check because if m_fd changes + // for some reason, tls_fd needs to get + // cleared so we pick up the new m_fd next time. + auto ret = ::write(tls_fd, trc.c_str(), trc.length()); + if (ret < 0 && errno == EINTR) + { + // Try once more before giving up + ret = ::write(tls_fd, trc.c_str(), trc.length()); + } + + if (ret < 0 && errno != EINTR) + { + g_logger.format(sinsp_logger::SEV_ERROR, + "Unable to write tracer (%s) to %s: %s", + trc.c_str(), m_file, strerror(errno)); + close_fd(); + } + // We know ret >= 0 so size_t cast is safe + else if ((size_t)ret != trc.length()) + { + ASSERT(false); + g_logger.format(sinsp_logger::SEV_ERROR, + "Incomplete write of tracer (%s) to %s", + trc.c_str(), m_file); + close_fd(); + } + return; +} + +int tracer_writer::get_fd() +{ + std::lock_guard lock(m_fd_lock); + + if (m_fd >= 0) + { + return m_fd; + } + + m_open_interval.run( + [this]() + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "Opening %s for writing tracers", m_file); + m_fd = ::open(m_file, O_WRONLY|O_NONBLOCK|O_CLOEXEC); + if (m_fd < 0) + { + g_logger.format(sinsp_logger::SEV_ERROR, + "Unable to open %s for writing tracers: %s", + m_file, strerror(errno)); + } + }, sinsp_utils::get_current_time_ns()); + + return m_fd; +} + +void tracer_writer::close_fd() +{ + std::lock_guard lock(m_fd_lock); + + if (m_fd > -1) + { + g_logger.format(sinsp_logger::SEV_DEBUG, + "Closing %s (fd %d) for writing tracers", + m_file, m_fd); + ::close(m_fd); + m_fd = -1; + } +} + +tracer_emitter::tracer_emitter(std::string tag, uint64_t timeout_ns) + : m_tag(std::move(tag)) + , m_start_ns(sinsp_utils::get_current_time_ns()) + , m_timeout_ns(timeout_ns) +{ + start(); +} + +bool tracer_emitter::m_enabled = false; + +// XXX find/write a constexpr-compatible string class +// for compile time concatenation +tracer_emitter::tracer_emitter(std::string tag, const tracer_emitter &parent, uint64_t timeout_ns) + : tracer_emitter::tracer_emitter( + parent.tag() + '.' + std::move(tag), + std::min(timeout_ns, parent.m_timeout_ns)) +{ +} + +tracer_emitter::~tracer_emitter() +{ + if (!m_exit_written) + { + write_tracer(false); + elapsed_time(); // just for the side effect of logging if needed + } +} + +void tracer_emitter::start() +{ + write_tracer(true); +} + +uint64_t tracer_emitter::stop() +{ + ASSERT(!m_exit_written); + if (!m_exit_written) + { + write_tracer(false); + } + return elapsed_time(); +} + +void tracer_emitter::write_tracer(const bool enter) +{ + if (!m_enabled) + { + return; + } + + static tracer_writer trc_writer; + + // XXX can we constexpr this part too? + std::string trc_str(enter ? ">" : "<"); + // 't' == use thread id + trc_str.append(":t:"); + trc_str.append(m_tag); + trc_str.append("::"); + + trc_writer.write(trc_str); + + if (!enter) + { + m_exit_written = true; + } +} + +uint64_t tracer_emitter::elapsed_time() const +{ + auto elapsed = sinsp_utils::get_current_time_ns() - m_start_ns; + if (elapsed > m_timeout_ns) + { + g_logger.format(sinsp_logger::SEV_INFO, "Tracer %s elapsed time %llu ns", m_tag.c_str(), elapsed); + } + return elapsed; +} diff --git a/userspace/libsinsp/tracer_emitter.h b/userspace/libsinsp/tracer_emitter.h new file mode 100644 index 0000000000..bf189d006d --- /dev/null +++ b/userspace/libsinsp/tracer_emitter.h @@ -0,0 +1,54 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#pragma once +#include + +// This class allows the caller to output sysdig tracers +// to /dev/null. +class tracer_emitter +{ +public: + static const uint64_t no_timeout = ~0ULL; + + tracer_emitter(std::string tag, uint64_t timeout_ns=no_timeout); + tracer_emitter(std::string tag, const tracer_emitter &parent, uint64_t timeout_ns=no_timeout); + ~tracer_emitter(); + + tracer_emitter() = delete; + tracer_emitter(const tracer_emitter&) = delete; + tracer_emitter& operator=(const tracer_emitter&) = delete; + + // Stop is only needed if you want the exit + // event before the instance gets destructed, + // i.e. goes out of scope + uint64_t stop(); + static void set_enabled(bool enabled) { m_enabled = enabled; } + +private: + void start(); + void write_tracer(const bool enter); + const std::string& tag() const { return m_tag; } + uint64_t elapsed_time() const; + + const std::string m_tag; + const uint64_t m_start_ns = 0; + const uint64_t m_timeout_ns = 0; + bool m_exit_written = false; + static bool m_enabled; +}; diff --git a/userspace/libsinsp/tracers.cpp b/userspace/libsinsp/tracers.cpp new file mode 100644 index 0000000000..d81045b206 --- /dev/null +++ b/userspace/libsinsp/tracers.cpp @@ -0,0 +1,1340 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include "sinsp.h" +#include "sinsp_int.h" +#include "tracers.h" + +sinsp_tracerparser::sinsp_tracerparser(sinsp *inspector) +{ + m_inspector = inspector; + m_storage_size = 0; + m_storage = NULL; + m_res = sinsp_tracerparser::RES_OK; + m_fragment_size = 0; + m_enter_pae = NULL; +} + +sinsp_tracerparser::~sinsp_tracerparser() +{ + if(m_storage) + { + free(m_storage); + } +} + +void sinsp_tracerparser::set_storage_size(uint32_t newsize) +{ + m_storage = (char*)realloc(m_storage, newsize); + if(m_storage == NULL) + { + throw sinsp_exception("memory allocation error in sinsp_tracerparser::process_event_data."); + } + + m_storage_size = newsize; +} + +sinsp_tracerparser::parse_result sinsp_tracerparser::process_event_data(char *data, uint32_t datalen, uint64_t ts) +{ + ASSERT(data != NULL); + m_storlen = m_fragment_size + datalen; + + // + // Make sure we have enough space in the buffer and copy the data into it + // + if(m_storage_size < m_storlen + 1) + { + set_storage_size(m_storlen + 1); + } + + memcpy(m_storage + m_fragment_size, data, datalen); + m_storage[m_storlen] = 0; + + if(m_fragment_size != 0) + { + m_fullfragment_storage_str = m_storage; + } + + // + // Do the parsing + // + if(m_storlen > 0) + { + // + // Reset the content + // + m_res = sinsp_tracerparser::RES_OK; + m_tags.clear(); + m_argnames.clear(); + m_argvals.clear(); + m_taglens.clear(); + m_argnamelens.clear(); + m_argvallens.clear(); + m_tot_taglens = 0; + m_tot_argnamelens = 0; + m_tot_argvallens = 0; + + if(m_storage[0] == '>' || m_storage[0] == '<') + { + parse_simple(m_storage); + } + else + { + parse_json(m_storage); + } + } + else + { + m_res = sinsp_tracerparser::RES_FAILED; + } + + if(m_res == sinsp_tracerparser::RES_FAILED) + { + // + // Invalid syntax + // + m_fragment_size = 0; + m_fullfragment_storage_str.clear(); + return m_res; + } + else if(m_res == sinsp_tracerparser::RES_TRUNCATED) + { + // + // Valid syntax, but the message is incomplete. Buffer it and wait for + // more fragments. + // + if(m_fragment_size > MAX_USER_EVT_BUFFER) + { + // + // Maximum buffering size reached, drop the event + // + m_fragment_size = 0; + return m_res; + } + + if(m_fullfragment_storage_str.length() == 0) + { + memcpy(m_storage, + data, + datalen); + + m_storage[datalen] = 0; + m_fragment_size += datalen; + } + else + { + uint32_t tlen = (uint32_t)m_fullfragment_storage_str.length(); + + memcpy(m_storage, + m_fullfragment_storage_str.c_str(), + tlen); + + m_fragment_size = tlen; + } + + return m_res; + } + + m_fragment_size = 0; + m_fullfragment_storage_str.clear(); + + // + // Parser tests stop here + // + if(m_inspector == NULL) + { + return sinsp_tracerparser::RES_OK; + } + + // + // Event decoding done. We do state tracking only if explicitly requested + // by one or more filters. + // + if(m_inspector->m_track_tracers_state == false) + { + return sinsp_tracerparser::RES_OK; + } + + // + // If this is an enter event, allocate a sinsp_partial_tracer object and + // push it to the list + // + if(m_type_str[0] == '>') + { + sinsp_partial_tracer* pae = m_inspector->m_partial_tracers_pool->pop(); + if(pae == NULL) + { + // + // The list is completely used. This likely means that there have been drops and + // the entries will be stuck there forever. Better clean the list, miss the 128 + // events it contains, and start fresh. + // + list* partial_tracers_list = &m_inspector->m_partial_tracers_list; + list::iterator it; + + for(it = partial_tracers_list->begin(); it != partial_tracers_list->end(); ++it) + { + m_inspector->m_partial_tracers_pool->push(*it); + } + + partial_tracers_list->clear(); + + return sinsp_tracerparser::RES_OK; + } + + init_partial_tracer(pae); + pae->m_time = ts; + m_inspector->m_partial_tracers_list.push_front(pae); + m_enter_pae = pae; + } + else + { + list* partial_tracers_list = &m_inspector->m_partial_tracers_list; + list::iterator it; + + init_partial_tracer(&m_exit_pae); + + for(it = partial_tracers_list->begin(); it != partial_tracers_list->end(); ++it) + { + if(m_exit_pae.compare(*it) == true) + { + m_exit_pae.m_time = ts; + + // + // This is a bit tricky and deserves some explanation: + // despite removing the pae and returning it to the available pool, + // we link to it so that the filters will use it. We do that as an + // optimization (it avoids making a copy or implementing logic for + // delayed list removal), and we base it on the assumption that, + // since the processing is strictly sequential and single thread, + // nobody will modify the pae until the event is fully processed. + // + m_enter_pae = *it; + + m_inspector->m_partial_tracers_pool->push(*it); + partial_tracers_list->erase(it); + return sinsp_tracerparser::RES_OK; + } + } + + m_enter_pae = NULL; + } + + return sinsp_tracerparser::RES_OK; +} + +sinsp_partial_tracer* sinsp_tracerparser::find_parent_enter_pae() +{ + list* partial_tracers_list = &m_inspector->m_partial_tracers_list; + list::iterator it; + + char* tse = m_enter_pae->m_tags_storage + m_tot_taglens; + if(*tse == 0 && tse > m_enter_pae->m_tags_storage) + { + --tse; + } + + uint32_t len = 0; + while(tse != m_enter_pae->m_tags_storage) + { + if(*tse == 0) + { + len = tse - m_enter_pae->m_tags_storage + 1; // 1 is for the trailing zero + break; + } + + --tse; + } + + for(it = partial_tracers_list->begin(); it != partial_tracers_list->end(); ++it) + { + if(m_enter_pae->compare(*it, len) == true) + { + return *it; + } + } + + return NULL; +} + +inline void sinsp_tracerparser::parse_json(char* evtstr) +{ + char* p = m_storage; + uint32_t delta; + char* tstr; + + // + // Skip the initial bracket + // + m_res = skip_spaces(p, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + if(*(p++) != '[') + { + m_res = sinsp_tracerparser::RES_FAILED; + return; + } + + // + // type + // + m_res = parsestr(p, &m_type_str, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + // + // ID + // + m_res = skip_spaces_and_commas(p, &delta, 1); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + if(*p == '"') + { + switch(*(++p)) + { + case 't': + m_id = m_tinfo->m_tid; + delta = 2; + break; + case 'p': + m_id = m_tinfo->m_pid; + if(*(p + 1) == 'p') + { + m_id = m_tinfo->m_ptid; + p++; + } + + delta = 2; + break; + case ':': + m_id = 0; + delta = 1; + break; + case 'g': + m_id = 0; + delta = 2; + break; + default: + m_res = sinsp_tracerparser::RES_FAILED; + break; + } + } + else + { + m_res = parsenumber(p, &m_id, &delta); + if(m_res > sinsp_tracerparser::RES_COMMA) + { + return; + } + } + + p += delta; + + if(m_res == sinsp_tracerparser::RES_COMMA) + { + m_res = skip_spaces(p, &delta); + } + else + { + m_res = skip_spaces_and_commas(p, &delta, 1); + } + + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + // + // First tag + // + m_res = skip_spaces_and_char(p, &delta, '['); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + m_res = parsestr_not_enforce(p, &tstr, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + if(tstr != NULL) + { + m_tags.push_back(tstr); + m_taglens.push_back(delta - 2); + m_tot_taglens += delta - 2; + + // + // Remaining tags + // + while(true) + { + m_res = skip_spaces_and_commas(p, &delta, 0); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + if(*p == ']') + { + break; + } + + m_res = parsestr(p, &tstr, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + m_tags.push_back(tstr); + m_taglens.push_back(delta - 2); + m_tot_taglens += delta - 2; + } + } + + // + // First argument + // + m_res = skip_spaces_and_commas_and_all_brackets(p, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + m_res = parsestr_not_enforce(p, &tstr, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + if(tstr != NULL) + { + m_argnames.push_back(tstr); + m_argnamelens.push_back(delta - 2); + m_tot_argnamelens += delta - 2; + + m_res = skip_spaces_and_char(p, &delta, ':'); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + m_res = parsestr(p, &tstr, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + m_argvals.push_back(tstr); + m_argvallens.push_back(delta - 2); + m_tot_argvallens += delta - 2; + + // + // Remaining arguments + // + while(true) + { + m_res = skip_spaces_and_commas_and_cr_brackets(p, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + if(*p == ']') + { + p++; + break; + } + + m_res = parsestr(p, &tstr, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + m_argnames.push_back(tstr); + m_argnamelens.push_back(delta - 2); + m_tot_argnamelens += delta - 2; + + m_res = skip_spaces_and_char(p, &delta, ':'); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + m_res = parsestr(p, &tstr, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + m_argvals.push_back(tstr); + m_argvallens.push_back(delta - 2); + m_tot_argvallens += delta - 2; + } + } + + // + // Terminating ] + // + m_res = skip_spaces(p, &delta); + if(m_res != sinsp_tracerparser::RES_OK) + { + return; + } + p += delta; + + if(*p != ']') + { + if(*p == 0) + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + } + else + { + m_res = sinsp_tracerparser::RES_FAILED; + } + return; + } + + m_res = sinsp_tracerparser::RES_OK; + return; +} + +inline void sinsp_tracerparser::delete_char(char* p) +{ + while(*p != 0) + { + *p = *(p + 1); + p++; + } +} + +inline void sinsp_tracerparser::parse_simple(char* evtstr) +{ + char* p = evtstr; + uint32_t delta; + + // + // Extract the type + // + m_type_str = p++; + + // + // Skip to the scope/id + // + if(*p != ':') + { + if(*p == 0) + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + } + else + { + m_res = sinsp_tracerparser::RES_FAILED; + } + return; + } + + *p = 0; + p++; + + // + // Extract the scope + // + if(*p == '0') + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + return; + } + + switch(*p) + { + case 't': + m_id = m_tinfo->m_tid; + delta = 2; + break; + case 'p': + m_id = m_tinfo->m_pid; + if(*(p + 1) == 'p') + { + m_id = m_tinfo->m_ptid; + p++; + } + + delta = 2; + break; + case ':': + m_id = 0; + delta = 1; + break; + case 'g': + m_id = 0; + delta = 2; + break; + default: + m_res = parsenumber_colend(p, &m_id, &delta); + if(m_res > sinsp_tracerparser::RES_COMMA) + { + return; + } + break; + } + + p += delta; + + // + // Extract the tags + // + if(*p == '0') + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + return; + } + + if(*p != ':') + { + bool dont_interpret_next_char = false; + + while(true) + { + char* start = p; + + m_tags.push_back(p); + + while(*p != 0) + { + if(dont_interpret_next_char) + { + dont_interpret_next_char = false; + ++p; + continue; + } + + if(*p == '\\') + { + ASSERT(dont_interpret_next_char == false); + dont_interpret_next_char = true; + delete_char(p); + continue; + } + + if(*p == '.' || *p == ':') + { + break; + } + + if(*p == '>' || *p == '<' || *p == '=' || *p == '\n') + { + m_res = sinsp_tracerparser::RES_FAILED; + return; + } + + ++p; + } + + m_taglens.push_back((uint32_t)(p - start)); + m_tot_taglens += (uint32_t)(p - start); + + if(*p == ':') + { + *p = 0; + break; + } + else if(*p == 0) + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + return; + } + else + { + *p = 0; + ++p; + } + } + } + + ++p; + + // + // Extract the arguments + // + if(*p == 0) + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + return; + } + + if(*p != ':') + { + bool dont_interpret_next_char = false; + + while(true) + { + char* start = p; + + // + // Arg name + // + m_argnames.push_back(p); + + while(*p != 0) + { + if(dont_interpret_next_char) + { + dont_interpret_next_char = false; + ++p; + continue; + } + + if(*p == '\\') + { + ASSERT(dont_interpret_next_char == false); + dont_interpret_next_char = true; + delete_char(p); + continue; + } + + if(*p == '=') + { + break; + } + + if(*p == '>' || *p == '<' || *p == '\n') + { + m_res = sinsp_tracerparser::RES_FAILED; + return; + } + + ++p; + } + + m_argnamelens.push_back((uint32_t)(p - start)); + m_tot_argnamelens += (uint32_t)(p - start); + + if(*p == 0) + { + if(*(p - 1) == ':') + { + // + // This means there was an argument without value, + // which we don't support + // + m_res = sinsp_tracerparser::RES_FAILED; + } + else + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + } + break; + } + else + { + *p = 0; + ++p; + } + + // + // Arg vals + // + start = p; + m_argvals.push_back(p); + + dont_interpret_next_char = false; + + while(*p != 0) + { + if(dont_interpret_next_char) + { + dont_interpret_next_char = false; + ++p; + continue; + } + + if(*p == '\\') + { + ASSERT(dont_interpret_next_char == false); + dont_interpret_next_char = true; + delete_char(p); + continue; + } + + if(*p == ',' || *p == ':' || *p == '=') + { + break; + } + + ++p; + } + + m_argvallens.push_back((uint32_t)(p - start)); + m_tot_argvallens += (uint32_t)(p - start); + + if(*p == ':') + { + *p = 0; + m_res = sinsp_tracerparser::RES_OK; + break; + } + else if(*p == 0) + { + m_res = sinsp_tracerparser::RES_TRUNCATED; + break; + } + else + { + *p = 0; + ++p; + } + } + } + + // + // All done + // + return; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::skip_spaces(char* p, uint32_t* delta) +{ + char* start = p; + + while(*p == ' ') + { + if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + + p++; + } + + *delta = (uint32_t)(p - start); + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::skip_spaces_and_commas(char* p, uint32_t* delta, uint32_t n_expected_commas) +{ + char* start = p; + uint32_t nc = 0; + + while(true) + { + if(*p == ' ') + { + p++; + continue; + } + else if(*p == ',') + { + nc++; + } + else if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + else + { + break; + } + + p++; + } + + if(nc < n_expected_commas) + { + return sinsp_tracerparser::RES_FAILED; + } + + *delta = (uint32_t)(p - start); + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::skip_spaces_and_char(char* p, uint32_t* delta, char char_to_skip) +{ + char* start = p; + uint32_t nc = 0; + + while(*p == ' ' || *p == char_to_skip || *p == 0) + { + if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + else if(*p == char_to_skip) + { + nc++; + } + + p++; + } + + if(nc != 1) + { + return sinsp_tracerparser::RES_FAILED; + } + + *delta = (uint32_t)(p - start); + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::skip_spaces_and_commas_and_sq_brackets(char* p, uint32_t* delta) +{ + char* start = p; + uint32_t nc = 0; + uint32_t nosb = 0; + + while(*p == ' ' || *p == ',' || *p == '[' || *p == ']' || *p == 0) + { + if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + else if(*p == ',') + { + nc++; + } + else if(*p == '[') + { + nosb++; + } + else if(*p == ']') + { + if(nosb != 0) + { + break; + } + } + + p++; + } + + if(nc != 1 || nosb != 1) + { + return sinsp_tracerparser::RES_FAILED; + } + + *delta = (uint32_t)(p - start); + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::skip_spaces_and_commas_and_cr_brackets(char* p, uint32_t* delta) +{ + char* start = p; + uint32_t nc = 0; + uint32_t nocb = 0; + uint32_t nccb = 0; + + while(*p == ' ' || *p == ',' || *p == '{' || *p == '}' || *p == 0) + { + if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + else if(*p == ',') + { + nc++; + } + else if(*p == '{') + { + nocb++; + } + else if(*p == '}') + { + nccb++; + } + + p++; + } + + if(!((nc == 1 && nocb == 1) || (nc == 1 && nccb == 1) || (nccb == 1 && *p == ']'))) + { + return sinsp_tracerparser::RES_FAILED; + } + + *delta = (uint32_t)(p - start); + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::skip_spaces_and_commas_and_all_brackets(char* p, uint32_t* delta) +{ + char* start = p; + uint32_t nc = 0; + uint32_t nosb = 0; + uint32_t nocb = 0; + + while(*p == ' ' || *p == ',' || *p == '[' || *p == ']' || *p == '{' || *p == '}' || (*p == 0)) + { + if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + else if(*p == ',') + { + nc++; + } + else if(*p == '[') + { + nosb++; + } + else if(*p == ']') + { + if(nosb != 0) + { + break; + } + } + else if(*p == '{') + { + nocb++; + } + + p++; + } + + if(nc != 1 || nosb != 1) + { + return sinsp_tracerparser::RES_FAILED; + } + else if(nocb != 1) + { + if(*p != ']') + { + return sinsp_tracerparser::RES_FAILED; + } + } + + *delta = (uint32_t)(p - start); + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::parsestr(char* p, char** res, uint32_t* delta) +{ + char* initial = p; + *res = NULL; + + // + // Make sure that we start with a \" + // + if(*p != '"') + { + *delta = (uint32_t)(p - initial + 1); + if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + else + { + return sinsp_tracerparser::RES_FAILED; + } + } + + *res = p + 1; + p++; + + // + // Navigate to the end of the string + // + while(!(*p == '\"' && *(p - 1) != '\\')) + { + if(*p == 0) + { + *delta = (uint32_t)(p - initial + 1); + return sinsp_tracerparser::RES_TRUNCATED; + } + + p++; + } + + *p = 0; + + *delta = (uint32_t)(p - initial + 1); + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::parsestr_not_enforce(char* p, char** res, uint32_t* delta) +{ + sinsp_tracerparser::parse_result psres = parsestr(p, res, delta); + + if(psres == sinsp_tracerparser::RES_FAILED) + { + if(*(p + *delta) == ']') + { + *res = NULL; + return sinsp_tracerparser::RES_OK; + } + } + else if(psres == sinsp_tracerparser::RES_TRUNCATED) + { + return psres; + } + + return sinsp_tracerparser::RES_OK; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::parsenumber(char* p, int64_t* res, uint32_t* delta) +{ + char* start = p; + sinsp_tracerparser::parse_result retval = sinsp_tracerparser::RES_OK; + int64_t val = 0; + + bool negative = false; + + if(*p == '-') + { + negative = true; + p++; + } + + while(*p >= '0' && *p <= '9') + { + val = val * 10 + (*p - '0'); + p++; + } + + if(*p == ',') + { + retval = sinsp_tracerparser::RES_COMMA; + } + else if(*p != 0 && *p != ' ') + { + return sinsp_tracerparser::RES_FAILED; + } + else if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + + + *p = 0; + + if(negative) + { + *res = -val; + } + else + { + *res = val; + } + + *delta = (uint32_t)(p - start + 1); + return retval; +} + +inline sinsp_tracerparser::parse_result sinsp_tracerparser::parsenumber_colend(char* p, int64_t* res, uint32_t* delta) +{ + char* start = p; + int64_t val = 0; + bool negative = false; + + if(*p == '-') + { + negative = true; + p++; + } + + while(*p >= '0' && *p <= '9') + { + val = val * 10 + (*p - '0'); + p++; + } + + if(*p != ':') + { + if(*p == 0) + { + return sinsp_tracerparser::RES_TRUNCATED; + } + else + { + return sinsp_tracerparser::RES_FAILED; + } + } + else + { + *delta = (uint32_t)(p - start + 1); + if(negative) + { + *res = -val; + } + else + { + *res = val; + } + + return sinsp_tracerparser::RES_OK; + } +} + +inline void sinsp_tracerparser::init_partial_tracer(sinsp_partial_tracer* pae) +{ + vector::iterator it; + vector::iterator sit; + + ASSERT(m_tinfo != NULL); + pae->m_tid = m_tinfo->m_tid; + + // + // Store the ID + // + pae->m_id = m_id; + + ASSERT(m_tags.size() == m_taglens.size()); + ASSERT(m_argnames.size() == m_argnamelens.size()); + ASSERT(m_argvals.size() == m_argvallens.size()); + + // + // Pack the tags + // + pae->m_tags.clear(); + pae->m_taglens.clear(); + pae->m_ntags = (uint32_t)m_tags.size(); + uint32_t encoded_tags_len = m_tot_taglens + pae->m_ntags + 1; + + if(pae->m_tags_storage_size < encoded_tags_len) + { + pae->m_tags_storage = (char*)realloc(pae->m_tags_storage, encoded_tags_len); + pae->m_tags_storage_size = encoded_tags_len; + } + + char* p = pae->m_tags_storage; + for(it = m_tags.begin(), sit = m_taglens.begin(); + it != m_tags.end(); ++it, ++sit) + { + memcpy(p, *it, (*sit) + 1); + pae->m_tags.push_back(p); + pae->m_taglens.push_back(*sit); + p += (*sit) + 1; + } + + *p++ = 0; + pae->m_tags_len = (uint32_t)(p - pae->m_tags_storage); + + // + // Pack the argnames + // + pae->m_argnames.clear(); + pae->m_argnamelens.clear(); + pae->m_nargs = (uint32_t)m_argnames.size(); + uint32_t encoded_argnames_len = m_tot_argnamelens + pae->m_nargs + 1; + + if(pae->m_argnames_storage_size < encoded_argnames_len) + { + pae->m_argnames_storage = (char*)realloc(pae->m_argnames_storage, encoded_argnames_len); + pae->m_argnames_storage_size = encoded_argnames_len; + } + + p = pae->m_argnames_storage; + for(it = m_argnames.begin(), sit = m_argnamelens.begin(); + it != m_argnames.end(); ++it, ++sit) + { + memcpy(p, *it, (*sit) + 1); + pae->m_argnames.push_back(p); + pae->m_argnamelens.push_back(*sit); + p += (*sit) + 1; + } + + *p++ = 0; + pae->m_argnames_len = (uint32_t)(p - pae->m_argnames_storage); + + // + // Pack the argvals + // + pae->m_argvals.clear(); + pae->m_argvallens.clear(); + uint32_t encoded_argvals_len = m_tot_argvallens + pae->m_nargs + 1; + + if(pae->m_argvals_storage_size < encoded_argvals_len) + { + pae->m_argvals_storage = (char*)realloc(pae->m_argvals_storage, encoded_argvals_len); + pae->m_argvals_storage_size = encoded_argvals_len; + } + + p = pae->m_argvals_storage; + for(it = m_argvals.begin(), sit = m_argvallens.begin(); + it != m_argvals.end(); ++it, ++sit) + { + memcpy(p, *it, (*sit) + 1); + pae->m_argvals.push_back(p); + pae->m_argvallens.push_back(*sit); + p += (*sit) + 1; + } + + *p++ = 0; + pae->m_argvals_len = (uint32_t)(p - pae->m_argvals_storage); +} + +void sinsp_tracerparser::test() +{ + char doc1[] = "[\">\", 12345, [\"mysql\", \"query\", \"init\"], [{\"argname1\":\"argval1\"}, {\"argname2\":\"argval2\"}, {\"argname3\":\"argval3\"}]]"; + + m_tinfo = new sinsp_threadinfo(nullptr); + m_tinfo->m_ptid = 11; + m_tinfo->m_pid = 22; + m_tinfo->m_tid = 33; + + printf("1\n"); + + float cpu_time = ((float)clock ()) / CLOCKS_PER_SEC; + + for(uint64_t j = 0; j < 30000000; j++) + { + process_event_data(doc1, sizeof(doc1) - 1, 10); + + if(m_res != sinsp_tracerparser::RES_OK) + { + printf("ERROR\n"); + } + + process_event_data(doc1, sizeof(doc1) - 1, 20); + + if(m_res != sinsp_tracerparser::RES_OK) + { + printf("ERROR\n"); + } + } + + cpu_time = ((float)clock()/ CLOCKS_PER_SEC) - cpu_time; + printf ("time: %5.2f\n", cpu_time); + + delete m_tinfo; + m_tinfo = nullptr; +} diff --git a/userspace/libsinsp/tracers.h b/userspace/libsinsp/tracers.h new file mode 100644 index 0000000000..6bc5deb123 --- /dev/null +++ b/userspace/libsinsp/tracers.h @@ -0,0 +1,190 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#define UESTORAGE_INITIAL_BUFSIZE 256 + +/////////////////////////////////////////////////////////////////////////////// +// A partial tracer +/////////////////////////////////////////////////////////////////////////////// +class sinsp_partial_tracer +{ +public: + sinsp_partial_tracer() + { + m_tags_storage = (char*)malloc(UESTORAGE_INITIAL_BUFSIZE); + m_argnames_storage = (char*)malloc(UESTORAGE_INITIAL_BUFSIZE); + m_argvals_storage = (char*)malloc(UESTORAGE_INITIAL_BUFSIZE); + m_tags_storage_size = UESTORAGE_INITIAL_BUFSIZE; + m_argnames_storage_size = UESTORAGE_INITIAL_BUFSIZE; + m_argvals_storage_size = UESTORAGE_INITIAL_BUFSIZE; + } + + ~sinsp_partial_tracer() + { + if(m_tags_storage) + { + free(m_tags_storage); + } + + if(m_argnames_storage) + { + free(m_argnames_storage); + } + + if(m_argvals_storage) + { + free(m_argvals_storage); + } + } + + inline bool compare(sinsp_partial_tracer* other) + { + if(m_id != other->m_id) + { + return false; + } + + if(m_tags_len != other->m_tags_len) + { + return false; + } + + if(memcmp(m_tags_storage, + other->m_tags_storage, + m_tags_len) == 0) + { + return true; + } + + return false; + } + + inline bool compare(sinsp_partial_tracer* other, uint32_t len) + { + if(m_id != other->m_id) + { + return false; + } + + if(len != other->m_tags_len - 1) + { + return false; + } + + if(memcmp(m_tags_storage, + other->m_tags_storage, + len) == 0) + { + return true; + } + + return false; + } + + char* m_tags_storage; + char* m_argnames_storage; + char* m_argvals_storage; + uint32_t m_tags_len; + uint32_t m_argnames_len; + uint32_t m_argvals_len; + uint32_t m_tags_storage_size; + uint32_t m_argnames_storage_size; + uint32_t m_argvals_storage_size; + uint64_t m_id; + vector m_tags; + vector m_argnames; + vector m_argvals; + vector m_taglens; + vector m_argnamelens; + vector m_argvallens; + uint32_t m_ntags; + uint32_t m_nargs; + + uint64_t m_time; + uint64_t m_tid; +}; + +/////////////////////////////////////////////////////////////////////////////// +// tracer parser +/////////////////////////////////////////////////////////////////////////////// +class sinsp_tracerparser +{ +public: + enum parse_result + { + RES_OK = 0, + RES_COMMA = 1, + RES_FAILED = 2, + RES_TRUNCATED = 3, + }; + + sinsp_tracerparser(sinsp *inspector); + ~sinsp_tracerparser(); + uint32_t get_storage_size() + { + return m_storage_size; + } + void set_storage_size(uint32_t newsize); + parse_result process_event_data(char *data, uint32_t datalen, uint64_t ts); + inline void parse_json(char* evtstr); + inline void parse_simple(char* evtstr); + sinsp_partial_tracer* find_parent_enter_pae(); + void test(); + + char* m_type_str; + int64_t m_id; + vector m_tags; + vector m_argnames; + vector m_argvals; + vector m_taglens; + vector m_argnamelens; + vector m_argvallens; + pair*, vector*> m_args; + uint32_t m_tot_taglens; + uint32_t m_tot_argnamelens; + uint32_t m_tot_argvallens; + sinsp_partial_tracer* m_enter_pae; + sinsp_partial_tracer m_exit_pae; + sinsp_threadinfo* m_tinfo; + +VISIBILITY_PRIVATE + inline parse_result skip_spaces(char* p, uint32_t* delta); + inline parse_result skip_spaces_and_commas(char* p, uint32_t* delta, uint32_t n_expected_commas); + inline parse_result skip_spaces_and_char(char* p, uint32_t* delta, char char_to_skip); + inline parse_result skip_spaces_and_commas_and_sq_brackets(char* p, uint32_t* delta); + inline parse_result skip_spaces_and_commas_and_cr_brackets(char* p, uint32_t* delta); + inline parse_result skip_spaces_and_commas_and_all_brackets(char* p, uint32_t* delta); + inline parse_result parsestr(char* p, char** res, uint32_t* delta); + inline parse_result parsestr_not_enforce(char* p, char** res, uint32_t* delta); + inline parse_result parsenumber(char* p, int64_t* res, uint32_t* delta); + inline parse_result parsenumber_colend(char* p, int64_t* res, uint32_t* delta); + inline void init_partial_tracer(sinsp_partial_tracer* pae); + inline void delete_char(char* p); + + string m_fullfragment_storage_str; + sinsp *m_inspector; + char* m_storage; + uint32_t m_storage_size; + uint32_t m_fragment_size; + sinsp_tracerparser::parse_result m_res; + uint32_t m_storlen; + + + friend class sinsp_parser; +}; diff --git a/userspace/libsinsp/tuples.cpp b/userspace/libsinsp/tuples.cpp new file mode 100644 index 0000000000..fe2d910491 --- /dev/null +++ b/userspace/libsinsp/tuples.cpp @@ -0,0 +1,54 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 + +ipv6addr ipv6addr::empty_address = {0x00000000, 0x00000000, 0x00000000, 0x00000000}; + +bool ipv6addr::operator==(const ipv6addr &other) const +{ + return (m_b[0] == other.m_b[0] && + m_b[1] == other.m_b[1] && + m_b[2] == other.m_b[2] && + m_b[3] == other.m_b[3]); +} + +bool ipv6addr::operator!=(const ipv6addr &other) const +{ + return !operator==(other); +} + +bool ipv6addr::operator<(const ipv6addr &other) const +{ + for(int i = 0; i < 4; i++) + { + if(m_b[i] < other.m_b[i]) return true; + else if(other.m_b[i] < m_b[i]) return false; + } + return false; +} + +bool ipv6addr::in_subnet(const ipv6addr &other) const +{ + // They're in the same subnet if the first 64 bits match + // (Assumes convention of first 48 bits for network, next 16 + // bits for subnet). + return (m_b[0] == other.m_b[0] && + m_b[1] == other.m_b[1]); +} diff --git a/userspace/libsinsp/tuples.h b/userspace/libsinsp/tuples.h index 0c20059f6e..f23f3ccb95 100644 --- a/userspace/libsinsp/tuples.h +++ b/userspace/libsinsp/tuples.h @@ -1,24 +1,27 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #pragma once -/** @defgroup state State management +#include + +/** @defgroup state State management * @{ */ @@ -35,23 +38,45 @@ typedef union _ipv4tuple uint16_t m_dport; ///< Destination (i.e. server) port. uint8_t m_l4proto; ///< Layer 4 protocol (e.g. TCP, UDP...). }m_fields; - uint8_t m_all[13]; ///< The fields as a raw array ob bytes. Used for hasing. + uint8_t m_all[13]; ///< The fields as a raw array ob bytes. Used for hashing. }ipv4tuple; +/*! + \brief An IPv4 network. +*/ +typedef struct ipv4net +{ + uint32_t m_ip; ///< IP addr + uint32_t m_netmask; ///< Subnet mask +}ipv4net; + +typedef struct _ipv6addr +{ + uint32_t m_b[4]; + + bool operator==(const _ipv6addr &other) const; + bool operator!=(const _ipv6addr &other) const; + bool operator<(const _ipv6addr &other) const; + bool in_subnet(const _ipv6addr &other) const; + + static struct _ipv6addr empty_address; +}ipv6addr; + + /*! \brief An IPv6 tuple. */ typedef union _ipv6tuple { - struct - { - uint32_t m_sip[4]; ///< source (i.e. client) address. - uint32_t m_dip[4]; ///< destination (i.e. server) address. + struct { + + ipv6addr m_sip; ///< source (i.e. client) address. + ipv6addr m_dip; ///< destination (i.e. server) address. uint16_t m_sport; ///< source (i.e. client) port. uint16_t m_dport; ///< destination (i.e. server) port. uint8_t m_l4proto; ///< Layer 4 protocol (e.g. TCP, UDP...) } m_fields; - uint8_t m_all[37]; ///< The fields as a raw array ob bytes. Used for hasing. + uint8_t m_all[37]; ///< The fields as a raw array ob bytes. Used for hashing. } ipv6tuple; /*! @@ -69,7 +94,7 @@ typedef struct ipv4serverinfo */ typedef struct ipv6serverinfo { - uint32_t m_ip[4]; ///< address + ipv6addr m_ip; ///< address uint16_t m_port; ///< port uint8_t m_l4proto; ///< IP protocol } ipv6serverinfo; @@ -84,7 +109,7 @@ typedef union _unix_tuple uint64_t m_source; ///< source OS pointer. uint64_t m_dest; ///< destination OS pointer. } m_fields; - uint8_t m_all[16]; ///< The fields as a raw array ob bytes. Used for hasing. + uint8_t m_all[16]; ///< The fields as a raw array ob bytes. Used for hashing. } unix_tuple; /*@}*/ diff --git a/userspace/libsinsp/uri.cpp b/userspace/libsinsp/uri.cpp new file mode 100644 index 0000000000..c80d5d9af6 --- /dev/null +++ b/userspace/libsinsp/uri.cpp @@ -0,0 +1,269 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// uri.cpp +// +// URI utility +// + +#include "uri.h" +#include "sinsp.h" +#include +#include + +const std::string uri::SPECIAL_CHARS = "!#$&'()*+,/:;=?@[]"; +const std::string uri::AMBIGUOUS_CHARS = " \"%-.<>\\^_`{|}~"; + +uri::uri(std::string str) +{ + trim(str); + // parser does not handle missing host properly + bool no_host = false; + if(ci_find_substr(str, std::string("file:///")) == 0) + { + str.insert(7, "localhost"); + no_host = true; + } + parsed_uri p_uri = parse_uri(str.c_str()); + if(p_uri.error) + { + str = std::string("Invalid URI: [").append(str).append(1, ']'); + throw sinsp_exception(str); + } + m_scheme = str.substr(p_uri.scheme_start, p_uri.scheme_end - p_uri.scheme_start); + std::transform(m_scheme.begin(), m_scheme.end(), m_scheme.begin(), ::tolower); + if(!no_host) + { + m_host = str.substr(p_uri.host_start, p_uri.host_end - p_uri.host_start); + std::transform(m_host.begin(), m_host.end(), m_host.begin(), ::tolower); + } + m_port = p_uri.port; + if(m_port == 0) + { + m_has_port = false; + m_port = get_well_known_port(); + } + m_path = str.substr(p_uri.path_start, p_uri.path_end - p_uri.path_start); + m_query = str.substr(p_uri.query_start, p_uri.query_end - p_uri.query_start); + if(p_uri.user_info_end != p_uri.user_info_start) + { + std::string auth = str.substr(p_uri.user_info_start, p_uri.user_info_end - p_uri.user_info_start); + std::string::size_type pos = auth.find(':'); + if(pos == std::string::npos) + { + throw sinsp_exception("Invalid credentials format."); + } + m_user = auth.substr(0, pos); + m_password = auth.substr(pos + 1); + } +} + +void uri::check(std::string str) +{ + trim(str); + // parser does not handle missing host properly + if(ci_find_substr(str, std::string("file:///")) == 0) + { + str.insert(7, "localhost"); + } + parsed_uri p_uri = parse_uri(str.c_str()); + if(p_uri.error) + { + str = std::string("Invalid URI: [").append(str).append(1, ']'); + throw sinsp_exception(str); + } + + if(p_uri.user_info_end != p_uri.user_info_start) + { + std::string auth = str.substr(p_uri.user_info_start, p_uri.user_info_end - p_uri.user_info_start); + std::string::size_type pos = auth.find(':'); + if(pos == std::string::npos) + { + throw sinsp_exception("Invalid credentials format."); + } + } +} + +int uri::get_well_known_port() const +{ + if (!m_scheme.empty()) + { + if(m_scheme == "http") { return 80; } + else if(m_scheme == "file") { return 0; } + else if(m_scheme == "https") { return 443; } + else if(m_scheme == "ftp") { return 21; } + else if(m_scheme == "ssh") { return 22; } + else if(m_scheme == "telnet") { return 23; } + else if(m_scheme == "nntp") { return 119; } + else if(m_scheme == "ldap") { return 389; } + else if(m_scheme == "rtsp") { return 554; } + else if(m_scheme == "sip") { return 5060; } + else if(m_scheme == "sips") { return 5061; } + else if(m_scheme == "xmpp") { return 5222; } + } + return 0; +} + +void uri::set_path(const std::string& path) +{ + uri u(*this); + u.m_path = path; + parsed_uri p_uri = parse_uri(u.to_string().c_str()); + if(p_uri.error) + { + throw sinsp_exception(std::string("Invalid URI Path: [").append(path).append(1, ']')); + } + m_path = path; +} + +std::string uri::to_string(bool show_creds) const +{ + std::ostringstream ostr; + ostr << m_scheme << "://"; + if(!m_user.empty()) + { + if(show_creds) + { + ostr << m_user << ':' << m_password << '@'; + } + else + { + ostr << "***:***@"; + } + } + ostr << m_host; + if(m_port && m_has_port) + { + ostr << ':' << m_port; + } + ostr << m_path; + if(!m_query.empty()) + { + ostr << '?' << m_query; + } + return ostr.str(); +} + +std::string uri::encode(const std::string& str, const std::string& reserved) +{ + std::string encoded_str; + for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) + { + char c = *it; + if((c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9')) + { + encoded_str += c; + } + else if (c <= 0x20 || c >= 0x7F || + SPECIAL_CHARS.find(c) != std::string::npos || + AMBIGUOUS_CHARS.find(c) != std::string::npos || + reserved.find(c) != std::string::npos) + { + std::ostringstream ostr; + ostr << "%" << std::setfill('0') << std::setw(2) << std::uppercase << std::hex << ((unsigned) (unsigned char) c); + encoded_str.append(ostr.str()); + } + else + { + encoded_str += c; + } + } + return encoded_str; +} + +// URI-decodes the given string by replacing percent-encoded +// characters with the actual character. Returns the decoded string. +// +// When plus_as_space is true, non-encoded plus signs in the query are decoded as spaces. +// (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1) +std::string uri::decode(const std::string& str, bool plus_as_space) +{ + std::string decoded_str; + bool in_query = false; + std::string::const_iterator it = str.begin(); + std::string::const_iterator end = str.end(); + while(it != end) + { + char c = *it++; + if(c == '?') + { + in_query = true; + } + // spaces may be encoded as plus signs in the query + if(in_query && plus_as_space && c == '+') + { + c = ' '; + } + else if(c == '%') + { + if (it == end) + { + throw sinsp_exception("URI encoding: no hex digit following percent sign in " + str); + } + char hi = *it++; + if (it == end) + { + throw sinsp_exception("URI encoding: two hex digits must follow percent sign in " + str); + } + char lo = *it++; + if (hi >= '0' && hi <= '9') + { + c = hi - '0'; + } + else if (hi >= 'A' && hi <= 'F') + { + c = hi - 'A' + 10; + } + else if (hi >= 'a' && hi <= 'f') + { + c = hi - 'a' + 10; + } + else + { + throw sinsp_exception("URI encoding: not a hex digit found in " + str); + } + c *= 16; + if (lo >= '0' && lo <= '9') + { + c += lo - '0'; + } + else if (lo >= 'A' && lo <= 'F') + { + c += lo - 'A' + 10; + } + else if (lo >= 'a' && lo <= 'f') + { + c += lo - 'a' + 10; + } + else + { + throw sinsp_exception("URI encoding: not a hex digit"); + } + } + decoded_str += c; + } + return decoded_str; +} + +bool uri::is(const std::string& proto) +{ + return ci_compare::is_equal(m_scheme, proto); +} diff --git a/userspace/libsinsp/uri.h b/userspace/libsinsp/uri.h new file mode 100644 index 0000000000..dc7e5f8e85 --- /dev/null +++ b/userspace/libsinsp/uri.h @@ -0,0 +1,170 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +// +// uri.h +// +// URI utility +// + +#pragma once + +#ifdef _WIN32 +#pragma warning(disable: 4190) +#endif + +#include "uri_parser.h" +#include + +// TODO: support fragments +class uri +{ +public: + typedef std::pair credentials_t; + + static const std::string SPECIAL_CHARS; + static const std::string AMBIGUOUS_CHARS; + + uri() = delete; + + uri(std::string str); + + const std::string& get_scheme() const; + const std::string& get_user() const; + const std::string& get_password() const; + const std::string& get_host() const; + const std::string& get_path() const; + void set_path(const std::string& path); + const std::string& get_query() const; + int get_port() const; + + void set_scheme(std::string scheme); + void set_host(std::string host); + + bool is(const std::string& proto); + bool is_file() const; + bool is_secure() const; + std::string get_credentials() const; + credentials_t& get_credentials(credentials_t& creds) const; + void set_credentials(const credentials_t& cred); + + std::string to_string(bool show_creds = true) const; + bool is_local() const; + + // URI-encodes the given string by escaping reserved, ambiguous and non-ASCII + // characters. Returns the encoded string with uppercase hex letters (eg. %5B, not %5b). + static std::string encode(const std::string& str, const std::string& reserved = ""); + + // URI-decodes the given string by replacing percent-encoded + // characters with the actual character. Returns the decoded string. + // + // When plus_as_space is true, non-encoded plus signs in the query are decoded as spaces. + // (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1) + static std::string decode(const std::string& str, bool plus_as_space = false); + static void check(std::string str); + +private: + int get_well_known_port() const; + + std::string m_scheme, m_user, m_password, m_host, m_path, m_query; + int m_port; + bool m_has_port = true; +}; + +inline const std::string& uri::get_scheme() const +{ + return m_scheme; +} + +inline void uri::set_scheme(std::string scheme) +{ + m_scheme = move(scheme); +} + +inline const std::string& uri::get_user() const +{ + return m_user; +} + +inline const std::string& uri::get_password() const +{ + return m_password; +} + +inline const std::string& uri::get_host() const +{ + return m_host; +} + +inline void uri::set_host(std::string host) +{ + m_host = move(host); +} + +inline const std::string& uri::get_path() const +{ + return m_path; +} + +inline const std::string& uri::get_query() const +{ + return m_query; +} + +inline int uri::get_port() const +{ + return m_port; +} + +inline bool uri::is_file() const +{ + return m_scheme == "file"; +} + +inline bool uri::is_secure() const +{ + return m_scheme == "https"; +} + +inline void uri::set_credentials(const credentials_t& cred) +{ + m_user = cred.first; + m_password = cred.second; +} + +inline std::string uri::get_credentials() const +{ + std::string creds; + if(!m_user.empty()) + { + creds.append(m_user).append(1, ':').append(m_password); + } + return creds; +} + +inline uri::credentials_t& uri::get_credentials(credentials_t& creds) const +{ + creds.first = m_user; + creds.second = m_password; + return creds; +} + +inline bool uri::is_local() const +{ + return m_host == "localhost" || m_host == "127.0.0.1" || m_scheme == "file"; +} diff --git a/userspace/libsinsp/uri_parser.c b/userspace/libsinsp/uri_parser.c new file mode 100644 index 0000000000..c8e6a4d3a7 --- /dev/null +++ b/userspace/libsinsp/uri_parser.c @@ -0,0 +1,614 @@ +/* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev + * + * Additional changes are licensed under the same terms as NGINX and + * copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include "uri_parser.h" +#include +#include +#include +#include + +#ifndef BIT_AT +# define BIT_AT(a, i) \ + (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \ + (1 << ((unsigned int) (i) & 7)))) +#endif + +#if HTTP_PARSER_STRICT +# define T(v) 0 +#else +# define T(v) v +#endif + +static const uint8_t normal_url_char[32] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128, +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, }; + +#undef T + +enum state + { s_dead = 1 /* important that this is > 0 */ + , s_req_spaces_before_url + , s_req_schema + , s_req_schema_slash + , s_req_schema_slash_slash + , s_req_server_start + , s_req_server + , s_req_server_with_at + , s_req_path + , s_req_query_string_start + , s_req_query_string + , s_req_fragment_start + , s_req_fragment + }; + +enum http_host_state + { + s_http_host_dead = 1 + , s_http_userinfo_start + , s_http_userinfo + , s_http_host_start + , s_http_host_v6_start + , s_http_host + , s_http_host_v6 + , s_http_host_v6_end + , s_http_host_v6_zone_start + , s_http_host_v6_zone + , s_http_host_port_start + , s_http_host_port +}; + +/* Macros for character classes; depends on strict-mode */ +#define CR '\r' +#define LF '\n' +#define LOWER(c) (unsigned char)(c | 0x20) +#define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z') +#define IS_NUM(c) ((c) >= '0' && (c) <= '9') +#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) +#define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f')) +#define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \ + (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \ + (c) == ')') +#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \ + (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \ + (c) == '$' || (c) == ',') + +#define STRICT_TOKEN(c) (tokens[(unsigned char)c]) + +#if HTTP_PARSER_STRICT +#define TOKEN(c) (tokens[(unsigned char)c]) +#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c)) +#define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-') +#else +#define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c]) +#define IS_URL_CHAR(c) \ + (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80)) +#define IS_HOST_CHAR(c) \ + (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_') +#endif + +#if HTTP_PARSER_STRICT +# define STRICT_CHECK(cond) \ +do { \ + if (cond) { \ + SET_ERRNO(HPE_STRICT); \ + goto error; \ + } \ +} while (0) +# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead) +#else +# define STRICT_CHECK(cond) +# define NEW_MESSAGE() start_state +#endif + +/* Our URL parser. + * + * This is designed to be shared by http_parser_execute() for URL validation, + * hence it has a state transition + byte-for-byte interface. In addition, it + * is meant to be embedded in http_parser_parse_uri(), which does the dirty + * work of turning state transitions URL components for its API. + * + * This function should only be invoked with non-space characters. It is + * assumed that the caller cares about (and can detect) the transition between + * URL and non-URL states by looking for these. + */ +static enum state +parse_url_char(enum state s, const char ch) +{ + if (ch == ' ' || ch == '\r' || ch == '\n') { + return s_dead; + } + +#if HTTP_PARSER_STRICT + if (ch == '\t' || ch == '\f') { + return s_dead; + } +#endif + + switch (s) { + case s_req_spaces_before_url: + /* Proxied requests are followed by scheme of an absolute URI (alpha). + * All methods except CONNECT are followed by '/' or '*'. + */ + + if (ch == '/' || ch == '*') { + return s_req_path; + } + + if (IS_ALPHA(ch)) { + return s_req_schema; + } + + break; + + case s_req_schema: + if (IS_ALPHA(ch)) { + return s; + } + + if (ch == ':') { + return s_req_schema_slash; + } + + break; + + case s_req_schema_slash: + if (ch == '/') { + return s_req_schema_slash_slash; + } + + break; + + case s_req_schema_slash_slash: + if (ch == '/') { + return s_req_server_start; + } + + break; + + case s_req_server_with_at: + if (ch == '@') { + return s_dead; + } + + /* FALLTHROUGH */ + case s_req_server_start: + case s_req_server: + if (ch == '/') { + return s_req_path; + } + + if (ch == '?') { + return s_req_query_string_start; + } + + if (ch == '@') { + return s_req_server_with_at; + } + + if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') { + return s_req_server; + } + + break; + + case s_req_path: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + return s_req_query_string_start; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_query_string_start: + case s_req_query_string: + if (IS_URL_CHAR(ch)) { + return s_req_query_string; + } + + switch (ch) { + case '?': + /* allow extra '?' in query string */ + return s_req_query_string; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_fragment_start: + if (IS_URL_CHAR(ch)) { + return s_req_fragment; + } + + switch (ch) { + case '?': + return s_req_fragment; + + case '#': + return s; + } + + break; + + case s_req_fragment: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + case '#': + return s; + } + + break; + + default: + break; + } + + /* We should never fall out of the switch above unless there's an error */ + return s_dead; +} + +static enum http_host_state +http_parse_host_char(enum http_host_state s, const char ch) { + switch(s) { + case s_http_userinfo: + case s_http_userinfo_start: + if (ch == '@') { + return s_http_host_start; + } + + if (IS_USERINFO_CHAR(ch)) { + return s_http_userinfo; + } + break; + + case s_http_host_start: + if (ch == '[') { + return s_http_host_v6_start; + } + + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + break; + + case s_http_host: + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + /* FALLTHROUGH */ + case s_http_host_v6_end: + if (ch == ':') { + return s_http_host_port_start; + } + + break; + + case s_http_host_v6: + if (ch == ']') { + return s_http_host_v6_end; + } + + /* FALLTHROUGH */ + case s_http_host_v6_start: + if (IS_HEX(ch) || ch == ':' || ch == '.') { + return s_http_host_v6; + } + + if (s == s_http_host_v6 && ch == '%') { + return s_http_host_v6_zone_start; + } + break; + + case s_http_host_v6_zone: + if (ch == ']') { + return s_http_host_v6_end; + } + + /* FALLTHROUGH */ + case s_http_host_v6_zone_start: + /* RFC 6874 Zone ID consists of 1*( unreserved / pct-encoded) */ + if (IS_ALPHANUM(ch) || ch == '%' || ch == '.' || ch == '-' || ch == '_' || + ch == '~') { + return s_http_host_v6_zone; + } + break; + + case s_http_host_port: + case s_http_host_port_start: + if (IS_NUM(ch)) { + return s_http_host_port; + } + + break; + + default: + break; + } + return s_http_host_dead; +} + +static int +http_parse_host(const char * buf, struct http_parser_uri *u, int found_at) { + assert(u->field_set & (1 << URI_FLD_HOST)); + enum http_host_state s; + + const char *p; + size_t buflen = u->field_data[URI_FLD_HOST].off + u->field_data[URI_FLD_HOST].len; + + u->field_data[URI_FLD_HOST].len = 0; + + s = found_at ? s_http_userinfo_start : s_http_host_start; + + for (p = buf + u->field_data[URI_FLD_HOST].off; p < buf + buflen; p++) { + enum http_host_state new_s = http_parse_host_char(s, *p); + + if (new_s == s_http_host_dead) { + return 1; + } + + switch(new_s) { + case s_http_host: + if (s != s_http_host) { + u->field_data[URI_FLD_HOST].off = p - buf; + } + u->field_data[URI_FLD_HOST].len++; + break; + + case s_http_host_v6: + if (s != s_http_host_v6) { + u->field_data[URI_FLD_HOST].off = p - buf; + } + u->field_data[URI_FLD_HOST].len++; + break; + + case s_http_host_v6_zone_start: + case s_http_host_v6_zone: + u->field_data[URI_FLD_HOST].len++; + break; + + case s_http_host_port: + if (s != s_http_host_port) { + u->field_data[URI_FLD_PORT].off = p - buf; + u->field_data[URI_FLD_PORT].len = 0; + u->field_set |= (1 << URI_FLD_PORT); + } + u->field_data[URI_FLD_PORT].len++; + break; + + case s_http_userinfo: + if (s != s_http_userinfo) { + u->field_data[URI_FLD_USERINFO].off = p - buf ; + u->field_data[URI_FLD_USERINFO].len = 0; + u->field_set |= (1 << URI_FLD_USERINFO); + } + u->field_data[URI_FLD_USERINFO].len++; + break; + + default: + break; + } + s = new_s; + } + + /* Make sure we don't end somewhere unexpected */ + switch (s) { + case s_http_host_start: + case s_http_host_v6_start: + case s_http_host_v6: + case s_http_host_v6_zone_start: + case s_http_host_v6_zone: + case s_http_host_port_start: + case s_http_userinfo: + case s_http_userinfo_start: + return 1; + default: + break; + } + + return 0; +} + +void +http_parser_uri_init(struct http_parser_uri *u) { + memset(u, 0, sizeof(*u)); +} + +int +http_parser_parse_uri(const char *buf, size_t buflen, int is_connect, + struct http_parser_uri *u) +{ + enum state s; + const char *p; + enum http_parser_uri_fields uf, old_uf; + int found_at = 0; + + u->port = u->field_set = 0; + s = is_connect ? s_req_server_start : s_req_spaces_before_url; + old_uf = URI_FLD_MAX; + + for (p = buf; p < buf + buflen; p++) { + s = parse_url_char(s, *p); + + /* Figure out the next field that we're operating on */ + switch (s) { + case s_dead: + return 1; + + /* Skip delimiters */ + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_query_string_start: + case s_req_fragment_start: + continue; + + case s_req_schema: + uf = URI_FLD_SCHEMA; + break; + + case s_req_server_with_at: + found_at = 1; + + /* FALLTROUGH */ + case s_req_server: + uf = URI_FLD_HOST; + break; + + case s_req_path: + uf = URI_FLD_PATH; + break; + + case s_req_query_string: + uf = URI_FLD_QUERY; + break; + + case s_req_fragment: + uf = URI_FLD_FRAGMENT; + break; + + default: + assert(!"Unexpected state"); + return 1; + } + + /* Nothing's changed; soldier on */ + if (uf == old_uf) { + u->field_data[uf].len++; + continue; + } + + u->field_data[uf].off = p - buf; + u->field_data[uf].len = 1; + + u->field_set |= (1 << uf); + old_uf = uf; + } + + /* host must be present if there is a schema */ + /* parsing http:///toto will fail */ + if ((u->field_set & (1 << URI_FLD_SCHEMA)) && + (u->field_set & (1 << URI_FLD_HOST)) == 0) { + return 1; + } + + if (u->field_set & (1 << URI_FLD_HOST)) { + if (http_parse_host(buf, u, found_at) != 0) { + return 1; + } + } + + /* CONNECT requests can only contain "hostname:port" */ + if (is_connect && u->field_set != ((1 << URI_FLD_HOST)|(1 << URI_FLD_PORT))) { + return 1; + } + + if (u->field_set & (1 << URI_FLD_PORT)) { + /* Don't bother with endp; we've already validated the string */ + unsigned long v = strtoul(buf + u->field_data[URI_FLD_PORT].off, NULL, 10); + + /* Ports have a max value of 2^16 */ + if (v > 0xffff) { + return 1; + } + + u->port = (uint16_t) v; + } + + return 0; +} + +struct parsed_uri parse_uri(const char *uri_string) { + struct http_parser_uri u; + http_parser_uri_init(&u); + + int rc = http_parser_parse_uri(uri_string, strlen(uri_string), 0, &u); + + if (rc) { + struct parsed_uri uri = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; + return uri; + } + + struct parsed_uri uri = { + 0, + u.field_set, + u.field_data[0].off, + u.field_data[0].off + u.field_data[0].len, + u.field_data[6].off, + u.field_data[6].off + u.field_data[6].len, + u.field_data[1].off, + u.field_data[1].off + u.field_data[1].len, + u.port, + u.field_data[3].off, + u.field_data[3].off + u.field_data[3].len, + u.field_data[4].off, + u.field_data[4].off + u.field_data[4].len, + u.field_data[5].off, + u.field_data[5].off + u.field_data[5].len, + }; + + return uri; +} diff --git a/userspace/libsinsp/uri_parser.h b/userspace/libsinsp/uri_parser.h new file mode 100644 index 0000000000..502c1fc6d8 --- /dev/null +++ b/userspace/libsinsp/uri_parser.h @@ -0,0 +1,115 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#ifndef uri_parser_h +#define uri_parser_h +#ifdef __cplusplus +extern "C" { +#endif + +#include +#if defined(_WIN32) && !defined(__MINGW32__) && \ + (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__) +#include +#include +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#else +#include +#endif + +/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run + * faster + */ +#ifndef HTTP_PARSER_STRICT +# define HTTP_PARSER_STRICT 1 +#endif + +/* Get an http_errno value from an http_parser */ +#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno) + +enum http_parser_uri_fields + { URI_FLD_SCHEMA = 0 + , URI_FLD_HOST = 1 + , URI_FLD_PORT = 2 + , URI_FLD_PATH = 3 + , URI_FLD_QUERY = 4 + , URI_FLD_FRAGMENT = 5 + , URI_FLD_USERINFO = 6 + , URI_FLD_MAX = 7 + }; + + +/* Result structure for http_parser_parse_uri(). + * + * Callers should index into field_data[] with UF_* values iff field_set + * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and + * because we probably have padding left over), we convert any port to + * a uint16_t. + */ +struct http_parser_uri { + uint16_t field_set; /* Bitmask of (1 << UF_*) values */ + uint16_t port; /* Converted URI_FLD_PORT string */ + + struct { + uint16_t off; /* Offset into buffer in which field starts */ + uint16_t len; /* Length of run in buffer */ + } field_data[URI_FLD_MAX]; +}; + + +/* Initialize all http_parser_uri members to 0 */ +void http_parser_uri_init(struct http_parser_uri *u); + +/* Parse a URL; return nonzero on failure */ +int http_parser_parse_uri(const char *buf, size_t buflen, + int is_connect, + struct http_parser_uri *u); + +struct parsed_uri { + const uint8_t error; + const uint16_t field_set; + const uint16_t scheme_start; + const uint16_t scheme_end; + const uint16_t user_info_start; + const uint16_t user_info_end; + const uint16_t host_start; + const uint16_t host_end; + const unsigned short port; + const uint16_t path_start; + const uint16_t path_end; + const uint16_t query_start; + const uint16_t query_end; + const uint16_t fragment_start; + const uint16_t fragment_end; +}; + +struct parsed_uri parse_uri(const char *uri_string); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/userspace/libsinsp/user_event.cpp b/userspace/libsinsp/user_event.cpp new file mode 100644 index 0000000000..2fa6fbe81c --- /dev/null +++ b/userspace/libsinsp/user_event.cpp @@ -0,0 +1,445 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "sinsp.h" +#include "sinsp_int.h" +#include "user_event.h" +#include "user_event_logger.h" + +// +// event_scope +// + +const std::string event_scope::SCOPE_OP_AND = "and"; + +// these string lists contain reserved strings; some of the reserved +// strings are escaped and mandatory to be first in RESERVED_STRINGS +// and have their escaped counterparts in the REPLACEMENT_STRINGS, +// in the same order as they appear in RESERVED_STRINGS +const event_scope::string_list_t event_scope::RESERVED_STRINGS = + {"'"}; +const event_scope::string_list_t event_scope::REPLACEMENT_STRINGS = + {"\\'"}; + +// scope key name format regex +#ifndef _WIN32 +const std::string event_scope::KEY_FORMAT = "[a-zA-Z0-9_/\\.-]*"; +#else +const std::string event_scope::KEY_FORMAT = "^[a-zA-Z0-9_/\\.-]*$"; +#endif // _WIN32 + +event_scope::event_scope(const std::string& key, const std::string& value) +{ + if(!key.empty() && !value.empty()) + { + add(key, value, ""); + } +} + +bool event_scope::add(const std::string& key, const std::string& value, const std::string& op) +{ + if(check_key_format(key)) + { + std::string k(key); + std::string o(!m_scope.empty() ? op : ""); + std::string v(value); + replace(v); + if(!v.empty()) + { + if(!o.empty()) + { + m_scope.append(1, ' ').append(trim(o)).append(1, ' '); + } + m_scope.append(trim(k)).append("='").append(trim(v)).append(1, 0x27); + return true; + } + } + else + { + g_logger.log("Scope key is invalid: [" + key + "], entry will not be added to scope.", + sinsp_logger::SEV_WARNING); + } + return false; +} + +string& event_scope::replace(std::string& value) +{ + ASSERT(RESERVED_STRINGS.size() == REPLACEMENT_STRINGS.size()); + + string_list_t::const_iterator res_it = RESERVED_STRINGS.cbegin(); + string_list_t::const_iterator res_end = RESERVED_STRINGS.cend(); + string_list_t::const_iterator rep_it = REPLACEMENT_STRINGS.cbegin(); + string_list_t::const_iterator rep_end = REPLACEMENT_STRINGS.cend(); + for(; res_it != res_end && rep_it != rep_end; ++res_it, ++rep_it) + { + replace_in_place(value, *res_it, *rep_it); + } + + return value; +} + +#ifndef _WIN32 +void event_scope::regex_error(const std::string& call, size_t ret, regex_t* preg, const std::string& str) +{ + if(!preg) { return; } + char errbuf[256] = {0}; + if(regerror(ret, preg, errbuf, 256)) + { + g_logger.log(call + "() error: " + errbuf, sinsp_logger::SEV_WARNING); + } + else + { + g_logger.log("Can't obtain " + call + "() [" + str + "] error.", sinsp_logger::SEV_WARNING); + } +} + +bool event_scope::check_key_format(const std::string& key) +{ + + if(key.empty()) { return false; } + bool result = false; + std::string exp(KEY_FORMAT); + regex_t reg = {0}; + size_t ret = regcomp(®, exp.c_str(), REG_EXTENDED); + if(0 == ret) + { + regmatch_t rm = {0}; + ret = regexec(®, key.c_str(), 1, &rm, 0); + if(0 == ret) + { + if((rm.rm_eo - rm.rm_so) == static_cast(key.length())) + { + result = true; + } + } + else { regex_error("regexec", ret, ®, key); } + } + else { regex_error("regcomp", ret, ®, exp); } + regfree(®); + return result; +} + +#else + +bool event_scope::check_key_format(const std::string& key) +{ + static const std::regex r(KEY_FORMAT); + if (std::regex_match(key, r)) { return true; } + return false; +} + +#endif // _WIN32 + +// +// user_event_meta_t +// +const std::string user_event_meta_t::PERMIT_ALL = "all"; + +user_event_meta_t::user_event_meta_t(const std::string& kind, const type_list_t& types): + m_kind(kind), m_types(types) +{ +} + +user_event_meta_t::user_event_meta_t(std::string&& kind, type_list_t&& types): + m_kind(std::move(kind)), m_types(std::move(types)) +{ +} + +user_event_meta_t::user_event_meta_t(const user_event_meta_t& other): + m_kind(other.m_kind), m_types(other.m_types) +{ +} + +user_event_meta_t::user_event_meta_t(user_event_meta_t&& other): + m_kind(std::move(other.m_kind)), m_types(std::move(other.m_types)) +{ + other.m_kind.clear(); + other.m_types.clear(); +} + +user_event_meta_t& user_event_meta_t::operator = (const user_event_meta_t& other) +{ + if(&other != this) + { + m_kind = other.m_kind; + m_types = other.m_types; + } + return *this; +} + +user_event_meta_t& user_event_meta_t::operator = (user_event_meta_t&& other) +{ + if(&other != this) + { + m_kind = std::move(other.m_kind); + m_types = std::move(other.m_types); + other.m_kind.clear(); + other.m_types.clear(); + } + return *this; +} + +// +// user_event_filter_t +// +user_event_filter_t::user_event_filter_t() +{ +} + +user_event_filter_t::user_event_filter_t(const list_t& list): m_list(list) +{ +} + +user_event_filter_t::user_event_filter_t(list_t&& list): m_list(std::move(list)) +{ +} + +void user_event_filter_t::add(const user_event_meta_t& evt) +{ + if(handle_all(user_event_meta_t(evt))) + { + return; + } + if(get_meta(evt.kind()) == m_list.end()) + { + m_list.insert(evt); + } +} + +void user_event_filter_t::add(user_event_meta_t&& evt) +{ + if(handle_all(user_event_meta_t(evt))) + { + return; + } + if(get_meta(evt.kind()) == m_list.end()) + { + m_list.emplace(std::move(evt)); + } +} + +bool user_event_filter_t::handle_all(user_event_meta_t&& evt) +{ + if(ci_compare_str(evt.kind(), user_event_meta_t::PERMIT_ALL)) + { + m_list.clear(); + m_list.emplace(std::move(evt)); + return true; + } + user_event_meta_t loc_evt(evt); + list_t::iterator it = m_list.find(loc_evt); + if(it != m_list.end()) + { + if(it->types().find(user_event_meta_t::PERMIT_ALL) != it->types().end()) + { + m_list.erase(it); + m_list.insert(loc_evt); + return true; + } + } + return false; +} + +void user_event_filter_t::remove(const user_event_meta_t& evt) +{ + m_list.erase(evt); +} + +void user_event_filter_t::remove(const std::string& kind) +{ + user_event_meta_t::type_list_t types; + user_event_meta_t evt(kind, types); + remove(evt); +} + +user_event_filter_t::list_t::const_iterator user_event_filter_t::get(const std::string& evt_kind) const +{ + list_t::const_iterator it = m_list.begin(), end = m_list.end(); + for(;it != end; ++it) + { + if(ci_compare_str(it->kind(), evt_kind)) { return it; } + } + return end; +} + +bool user_event_filter_t::has(const std::string& evt_kind, const std::string& evt_type) const +{ + list_t::const_iterator it = get(evt_kind); + if(it != m_list.end()) + { + for(const auto& t : it->types()) + { + if(ci_compare_str(t, evt_type)) + { + return true; + } + } + } + return false; +} + +bool user_event_filter_t::allows(const user_event_meta_t& evt) const +{ + list_t::const_iterator it = get_meta(evt.kind()); + if(it != m_list.end()) // this event kind is allowed + { + // check for "any event" type being allowed by this filter + if(it->types().find(user_event_meta_t::PERMIT_ALL) != it->types().end()) + { + return true; + } + // check if event has more types than this filter + if(evt.types().size() > it->types().size()) + { + return false; + } + // if all event types are present in this filter, event is allowed + for(auto const& type : evt.types()) + { + if(it->types().find(type) == it->types().end()) + { + return false; + } + } + return true; + } + return false; +} + +user_event_filter_t::list_t::const_iterator user_event_filter_t::get_meta(const std::string& evt_kind) const +{ + list_t::const_iterator it = m_list.begin(), end = m_list.end(); + for(; it != end; ++it) + { + if(ci_compare_str(it->kind(), user_event_meta_t::PERMIT_ALL) || ci_compare_str(it->kind(),evt_kind)) + { + return it; + } + } + return end; +} + +std::string user_event_filter_t::to_string() const +{ + std::string ret; + for(const auto& evt : m_list) + { + if(evt.types().size()) + { + ret.append(1, '\n').append(evt.kind()).append(1, ':'); + for(const auto& type : evt.types()) + { + ret.append(type).append(", "); + } + } + } + return ret; +} + +// +// sinsp_user_event +// + +sinsp_user_event::sinsp_user_event() : m_epoch_time_s(0), m_severity(~0) +{ +} + +sinsp_user_event::sinsp_user_event(uint64_t epoch_time_s, string&& name, string&& desc, + string&& scope, tag_map_t&& tags, uint32_t sev): + m_epoch_time_s(epoch_time_s), m_name(std::move(name)), m_description(std::move(desc)), + m_severity(sev), m_scope(std::move(scope)), m_tags(std::move(tags)) +{ +} + +sinsp_user_event::sinsp_user_event(sinsp_user_event&& other): + m_epoch_time_s(other.m_epoch_time_s), + m_name(std::move(other.m_name)), + m_description(std::move(other.m_description)), + m_severity(other.m_severity), + m_scope(std::move(other.m_scope)), + m_tags(std::move(other.m_tags)) +{ +} + +sinsp_user_event& sinsp_user_event::operator=(sinsp_user_event&& other) +{ + if(this != &other) + { + m_epoch_time_s = other.m_epoch_time_s; + m_name = std::move(other.m_name); + m_description = std::move(other.m_description); + m_severity = other.m_severity; + m_scope = std::move(other.m_scope); + m_tags = std::move(other.m_tags); + } + + return *this; +} + +std::string sinsp_user_event::to_string() +{ + std::ostringstream ostr; + ostr << "timestamp: " << m_epoch_time_s << '\n' << + "name: " << m_name << "\n" + "description: " << m_description << "\"\n" + "scope: " << m_scope << "\"\n"; + + if(m_severity != UNKNOWN_SEVERITY) + { + ostr << "priority: " << m_severity << '\n'; + } + + if(m_tags.size()) + { + ostr << "tags:"; + for(auto& tag : m_tags) + { + ostr << "\n " << tag.first << ": " << tag.second; + } + } + ostr << std::flush; + return ostr.str(); +} + +void sinsp_user_event::emit_event_overflow(const std::string& component, + const std::string& machine_id, + const std::string& source) +{ + std::string event_name = component; + event_name.append(" Event Limit Exceeded"); + std::ostringstream description; + description << component << " event limit (" << max_events_per_cycle() << + " per second) exceeded. Excess events were discarded."; + std::string scope; + if(machine_id.length()) + { + scope.append("host.mac='").append(machine_id).append("'"); + } + tag_map_t tags{{"source", source}}; + + auto evt = sinsp_user_event( + get_epoch_utc_seconds_now(), + std::move(event_name), + description.str(), + std::move(scope), + std::move(tags), + user_event_logger::SEV_EVT_WARNING); + + user_event_logger::log(evt, user_event_logger::SEV_EVT_WARNING); +} diff --git a/userspace/libsinsp/user_event.h b/userspace/libsinsp/user_event.h new file mode 100644 index 0000000000..210920703a --- /dev/null +++ b/userspace/libsinsp/user_event.h @@ -0,0 +1,382 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#include "sinsp_int.h" + +#include +#include +#include +#include +#include +#include +// c++ std regex is buggy on g++ 4.8 +// so we use POSIX regex on non-windows +#ifndef _WIN32 +#include +#else +#include +#endif + +#include "sinsp_int.h" + +// +// scope utilities +// +class event_scope +{ +public: + typedef std::vector string_list_t; + + static const std::string SCOPE_OP_AND; + static const string_list_t RESERVED_STRINGS; + static const string_list_t REPLACEMENT_STRINGS; + static const std::string KEY_FORMAT; + + event_scope(const std::string& key = "", const std::string& value = ""); + + bool add(const std::string& key, const std::string& value, const std::string& op = SCOPE_OP_AND); + + const std::string& get() const; + std::string& get_ref(); + + void clear(); + + // utility function to check that a scope entry key is valid; + // valid entries match KEY_FORMAT regular expression + static bool check_key_format(const std::string& key); + +private: + + // utility function to replace RESERVED_STRINGS with their + // counterparts in REPLACEMENT_STRINGS + static string& replace(std::string& scope); +#ifndef _WIN32 + static void regex_error(const std::string& call, size_t ret, regex_t* preg, const std::string& str); +#endif + std::string m_scope; +}; + +inline const std::string& event_scope::get() const +{ + if(m_scope.empty()) + { + g_logger.log("Scope is empty--at least one key/value pair should be present", + sinsp_logger::SEV_WARNING); + } + return m_scope; +} + +inline std::string& event_scope::get_ref() +{ + if(m_scope.empty()) + { + g_logger.log("Scope is empty--at least one key/value pair should be present", + sinsp_logger::SEV_WARNING); + } + + return m_scope; +} + +inline void event_scope::clear() +{ + m_scope.clear(); +} + + +// +// user-configured event meta +// +class user_event_meta_t +{ +public: + typedef std::set type_list_t; + + static const std::string PERMIT_ALL; + + user_event_meta_t() {}; + ~user_event_meta_t() {}; + + user_event_meta_t(const std::string& kind, const type_list_t& types); + user_event_meta_t(std::string&& kind, type_list_t&& types); + user_event_meta_t(const user_event_meta_t& other); + user_event_meta_t(user_event_meta_t&& other); + + user_event_meta_t& operator = (const user_event_meta_t& other); + user_event_meta_t& operator = (user_event_meta_t&& other); + bool operator < (const user_event_meta_t& other) const; + + const std::string& kind() const; + const type_list_t& types() const; + bool has_type(const std::string& type) const; + bool any_type() const; + bool is_kind(const std::string& kind) const; + bool any_kind() const; + +private: + std::string m_kind; + type_list_t m_types; +}; + +inline bool user_event_meta_t::operator < (const user_event_meta_t& other) const +{ +#ifndef _WIN32 + return strcasecmp(m_kind.c_str(), other.m_kind.c_str()) < 0; +#else + return lstrcmpiA(m_kind.c_str(), other.m_kind.c_str()) < 0; +#endif // _WIN32 +} + +inline const std::string& user_event_meta_t::kind() const +{ + return m_kind; +} + +inline const user_event_meta_t::type_list_t& user_event_meta_t::types() const +{ + return m_types; +} + +inline bool user_event_meta_t::has_type(const std::string& type) const +{ + return m_types.find(type) != m_types.end() || any_type(); +} + +inline bool user_event_meta_t::any_type() const +{ + return m_types.find(PERMIT_ALL) != m_types.end(); +} + +inline bool user_event_meta_t::is_kind(const std::string& kind) const +{ +#ifndef _WIN32 + return (strcasecmp(m_kind.c_str(), kind.c_str()) == 0) || any_kind(); +#else + return lstrcmpiA(m_kind.c_str(), kind.c_str()) < 0; +#endif // _WIN32 +} + +inline bool user_event_meta_t::any_kind() const +{ +#ifndef _WIN32 + return strcasecmp(m_kind.c_str(), PERMIT_ALL.c_str()) == 0; +#else + return lstrcmpiA(m_kind.c_str(), PERMIT_ALL.c_str()) < 0; +#endif // _WIN32 +} + + +// +// user-configured-event filter +// +class user_event_filter_t +{ +public: + typedef std::set list_t; + typedef std::shared_ptr ptr_t; + + user_event_filter_t(); + user_event_filter_t(const list_t& list); + user_event_filter_t(list_t&& list); + + void add(const user_event_meta_t& evt); + void add(user_event_meta_t&& evt); + bool has(const std::string& evt_kind) const; + bool has(const std::string& evt_kind, const std::string& evt_type) const; + list_t::const_iterator get(const std::string& evt_kind) const; + void remove(const user_event_meta_t& evt); + void remove(const std::string& kind); + void clear(); + bool allows(const user_event_meta_t& evt) const; + bool allows_all(const std::string& kind) const; + bool allows_all() const; + + std::string to_string() const; + +private: + static bool ci_compare_str(const std::string& a, const std::string& b); + + // if the filter entry of the requested kind is found, returns const iterator pointing to it; + // if "any event" entry is found, iterator pointing to it is returned; otherwise, the iterator + // pointing to the end of the filter list is returned (indicating the event kind was not found) + list_t::const_iterator get_meta(const std::string& evt_kind) const; + + bool handle_all(user_event_meta_t&& evt); + + list_t m_list; +}; + +inline void user_event_filter_t::clear() +{ + m_list.clear(); +} + +inline bool user_event_filter_t::ci_compare_str(const std::string& a, const std::string& b) +{ +#ifndef _WIN32 + return strcasecmp(a.c_str(), b.c_str()) == 0; +#else + return lstrcmpiA(a.c_str(), b.c_str()) == 0; +#endif // _WIN32 +} + +inline bool user_event_filter_t::has(const std::string& evt_kind) const +{ + return get(evt_kind) != m_list.end(); +} + +inline bool user_event_filter_t:: allows_all(const std::string& kind) const +{ + return allows(user_event_meta_t(kind, {user_event_meta_t::PERMIT_ALL})); +} + +inline bool user_event_filter_t::allows_all() const +{ + return get(user_event_meta_t::PERMIT_ALL) != m_list.end(); +} + +// +// Wrapper class for user-configured events +// +class sinsp_user_event +{ +public: + typedef std::unordered_map tag_map_t; + static const uint32_t UNKNOWN_SEVERITY = static_cast(~0); + + sinsp_user_event(const sinsp_user_event&) = delete; + sinsp_user_event& operator=(const sinsp_user_event& other) = delete; + + sinsp_user_event(); + + sinsp_user_event(uint64_t epoch_time_s, std::string&& name, std::string&& desc, + std::string&& scope, tag_map_t&& tags, uint32_t sev); + + sinsp_user_event(sinsp_user_event&& other); + + sinsp_user_event& operator=(sinsp_user_event&& other); + + uint64_t epoch_time_s() const; + const std::string& name() const; + const std::string& description() const; + uint32_t severity() const; + const std::string& scope() const; + const tag_map_t& tags() const; + + /** + * \brief Format the event as a YAML-like human readable string + * @return the formatted string + * + * Note: While the format looks superficially similar to YAML, it's not. + * This method does not generate valid YAML, especially when characters + * like quotes, backslashes or newlines are found in any of the fields + */ + std::string to_string(); + + static void emit_event_overflow(const std::string& component, + const std::string& machine_id, + const std::string& source = "sysdig-agent"); + static size_t max_events_per_cycle(); + +private: + uint64_t m_epoch_time_s; + std::string m_name; + std::string m_description; + uint32_t m_severity; + std::string m_scope; + tag_map_t m_tags; +}; + +inline uint64_t sinsp_user_event::epoch_time_s() const +{ + return m_epoch_time_s; +} + +inline const std::string& sinsp_user_event::name() const +{ + return m_name; +} + +inline const std::string& sinsp_user_event::description() const +{ + return m_description; +} + +inline uint32_t sinsp_user_event::severity() const +{ + return m_severity; +} + +inline const std::string& sinsp_user_event::scope() const +{ + return m_scope; +} + +inline const sinsp_user_event::tag_map_t& sinsp_user_event::tags() const +{ + return m_tags; +} + +inline size_t sinsp_user_event::max_events_per_cycle() +{ + return 100u; // TODO: move this value to config? +} + +// +// User-configured events queue +// +class user_event_queue +{ +public: + typedef std::shared_ptr ptr_t; + typedef std::deque type_t; + + void add(sinsp_user_event&& evt); + bool get(sinsp_user_event& evt); + type_t::size_type count() const; + +private: + type_t m_queue; + mutable std::mutex m_mutex; +}; + +inline void user_event_queue::add(sinsp_user_event&& evt) +{ + std::lock_guard lock(m_mutex); + m_queue.emplace_back(std::move(evt)); +} + +inline bool user_event_queue::get(sinsp_user_event& evt) +{ + std::lock_guard lock(m_mutex); + if(!m_queue.size()) + { + return false; + } + evt = std::move(m_queue.front()); + m_queue.pop_front(); + return true; +} + +inline user_event_queue::type_t::size_type user_event_queue::count() const +{ + std::lock_guard lock(m_mutex); + return m_queue.size(); +} diff --git a/userspace/libsinsp/user_event_logger.cpp b/userspace/libsinsp/user_event_logger.cpp new file mode 100644 index 0000000000..3795adc13a --- /dev/null +++ b/userspace/libsinsp/user_event_logger.cpp @@ -0,0 +1,66 @@ +/* +Copyright (C) 2019 Sysdig Inc. + +This file is part of sysdig. + +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 "sinsp.h" +#include "user_event_logger.h" +#include +#include + +namespace +{ + +/** + * Do-nothing realization of the user_event_logger::callback interface. + */ +class null_callback : public user_event_logger::callback +{ +public: + void log(const sinsp_user_event& evt, + const user_event_logger::severity severity) override + { } + + bool is_null() const override { return true; } +}; + +/** The current callback handler. */ +user_event_logger::callback::ptr_t s_callback = std::make_shared(); + +} // end namespace + +void user_event_logger::log(const sinsp_user_event& evt, + const user_event_logger::severity severity) +{ + s_callback->log(evt, severity); +} + +void user_event_logger::register_callback(callback::ptr_t callback) +{ + if(callback) + { + s_callback = callback; + } + else + { + s_callback = std::make_shared(); + } +} + +const user_event_logger::callback& user_event_logger::get_callback() +{ + return *s_callback; +} diff --git a/userspace/libsinsp/user_event_logger.h b/userspace/libsinsp/user_event_logger.h new file mode 100644 index 0000000000..ff37a7b34a --- /dev/null +++ b/userspace/libsinsp/user_event_logger.h @@ -0,0 +1,89 @@ +/* +Copyright (C) 2019 Sysdig Inc. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#include +#include "user_event.h" + +/** + * This namespace exposes an API for logging user events. + */ +namespace user_event_logger +{ + +/** + * The severities at which user events may be logged. + */ +enum severity +{ + SEV_EVT_FATAL, + SEV_EVT_CRITICAL, + SEV_EVT_ERROR, + SEV_EVT_WARNING, + SEV_EVT_NOTICE, + SEV_EVT_INFORMATION, + SEV_EVT_DEBUG, +}; + +/** + * Interface to an object that will receive callbacks whenever user event + * logs are generated. + */ +class callback +{ +public: + using ptr_t = std::shared_ptr; + + virtual ~callback() = default; + + /** + * Write the given log str with the given severity. + */ + virtual void log(const sinsp_user_event& evt, user_event_logger::severity sev) = 0; + + /** + * We use the "Null Object Pattern" with this interface; this will + * return true for do-nothing implementations, false otherwise. + */ + virtual bool is_null() const { return false; } +}; + +/** + * Write the given user event log message with the given severity to the + * registered callback. + */ +void log(const sinsp_user_event& evt, user_event_logger::severity sev); + +/** + * Register the given callback. If a callback is already registered, it will + * be replaced with the given callback. The given callback may be nullptr, + * in which case the registered callback will be replaced with a null + * callback handler. + */ +void register_callback(user_event_logger::callback::ptr_t callback); + +/** + * Returns a reference to the current callback handler. Use the is_null() + * method on the returned object to determine if the handler is expected to + * perform useful logging. + */ +const callback& get_callback(); + +} // end namespace user_event_logger + diff --git a/userspace/libsinsp/utils.cpp b/userspace/libsinsp/utils.cpp index f1a24057ca..aa15f100a7 100644 --- a/userspace/libsinsp/utils.cpp +++ b/userspace/libsinsp/utils.cpp @@ -1,26 +1,46 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #ifndef _WIN32 #include #include #include +#include +#ifdef __GLIBC__ +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#else +#pragma comment(lib, "Ws2_32.lib") +#include +#include "Shlwapi.h" +#pragma comment(lib,"shlwapi.lib") #endif +#include +#include +#include #include "sinsp.h" #include "sinsp_int.h" @@ -28,32 +48,53 @@ along with sysdig. If not, see . #include "sinsp_signal.h" #include "filter.h" #include "filterchecks.h" +#include "chisel.h" +#include "protodecoder.h" +#include "uri.h" +#if !defined(_WIN32) && !defined(MINIMAL_BUILD) +#include "curl/curl.h" +#endif + +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif +#ifdef HAS_CHISELS const chiseldir_info g_chisel_dirs_array[] = { {false, ""}, // file as is #ifdef _WIN32 {false, "c:/sysdig/chisels/"}, #endif - {false, "./"}, {false, "./chisels/"}, - {true, "~/chisels/"}, + {true, "~/.chisels/"}, }; +#endif #ifndef _WIN32 -char* realpath_ex(const char *path, char *buff) +static std::string realpath_ex(const std::string& path) { - char *home; - - if(*path=='~' && (home = getenv("HOME"))) - { - char s[PATH_MAX]; - return realpath(strcat(strcpy(s, home), path+1), buff); - } - else - { - return realpath(path, buff); - } + char *home; + char* resolved; + + if(!path.empty() && path[0]=='~' && (home = getenv("HOME"))) + { + std::string expanded_home = home; + expanded_home += path.c_str()+1; + resolved = realpath(expanded_home.c_str(), nullptr); + } + else + { + resolved = realpath(path.c_str(), nullptr); + } + + if (!resolved) + { + return ""; + } + std::string ret = resolved; + free(resolved); + return resolved; } #endif @@ -70,7 +111,10 @@ sinsp_initializer g_initializer; #ifdef HAS_FILTERING sinsp_filter_check_list g_filterlist; #endif +sinsp_protodecoder_list g_decoderlist; +#ifdef HAS_CHISELS vector* g_chisel_dirs = NULL; +#endif // // loading time initializations @@ -86,8 +130,9 @@ sinsp_initializer::sinsp_initializer() // // Init the logger // - g_logger.set_severity(sinsp_logger::SEV_DEBUG); + g_logger.set_severity(sinsp_logger::SEV_TRACE); +#ifdef HAS_CHISELS // // Init the chisel directory list // @@ -99,20 +144,17 @@ sinsp_initializer::sinsp_initializer() if(g_chisel_dirs_array[j].m_need_to_resolve) { #ifndef _WIN32 - char resolved_path[PATH_MAX]; - - if(realpath_ex(g_chisel_dirs_array[j].m_dir, resolved_path) != NULL) + std::string resolved_path = realpath_ex(g_chisel_dirs_array[j].m_dir); + if(!resolved_path.empty()) { - string resolved_path_str(resolved_path); - - if(resolved_path_str[resolved_path_str.size() -1] != '/') + if(resolved_path[resolved_path.size() - 1] != '/') { - resolved_path_str += "/"; + resolved_path += '/'; } chiseldir_info cdi; cdi.m_need_to_resolve = false; - sprintf(cdi.m_dir, "%s", resolved_path_str.c_str()); + cdi.m_dir = std::move(resolved_path); g_chisel_dirs->push_back(cdi); } #else @@ -124,6 +166,7 @@ sinsp_initializer::sinsp_initializer() g_chisel_dirs->push_back(g_chisel_dirs_array[j]); } } +#endif // HAS_CHISELS // // Sockets initialization on windows @@ -137,10 +180,12 @@ sinsp_initializer::sinsp_initializer() sinsp_initializer::~sinsp_initializer() { +#ifdef HAS_CHISELS if(g_chisel_dirs) { delete g_chisel_dirs; } +#endif } /////////////////////////////////////////////////////////////////////////////// @@ -149,7 +194,6 @@ sinsp_initializer::~sinsp_initializer() // // errno to string conversion. -// Only the first 40 error codes are currently implemented // const char* sinsp_utils::errno_to_str(int32_t code) { @@ -235,40 +279,202 @@ const char* sinsp_utils::errno_to_str(int32_t code) return "ENOTEMPTY"; case SE_ELOOP: return "ELOOP"; - case SE_ERESTARTSYS: - return "ERESTARTSYS"; + case SE_ENOMSG: + return "ENOMSG"; + case SE_EIDRM: + return "EIDRM"; + case SE_ECHRNG: + return "ECHRNG"; + case SE_EL2NSYNC: + return "EL2NSYNC"; + case SE_EL3HLT: + return "EL3HLT"; + case SE_EL3RST: + return "EL3RST"; + case SE_ELNRNG: + return "ELNRNG"; + case SE_EUNATCH: + return "EUNATCH"; + case SE_ENOCSI: + return "ENOCSI"; + case SE_EL2HLT: + return "EL2HLT"; + case SE_EBADE: + return "EBADE"; + case SE_EBADR: + return "EBADR"; + case SE_EXFULL: + return "EXFULL"; + case SE_ENOANO: + return "ENOANO"; + case SE_EBADRQC: + return "EBADRQC"; + case SE_EBADSLT: + return "EBADSLT"; + case SE_EBFONT: + return "EBFONT"; + case SE_ENOSTR: + return "ENOSTR"; + case SE_ENODATA: + return "ENODATA"; + case SE_ETIME: + return "ETIME"; + case SE_ENOSR: + return "ENOSR"; + case SE_ENONET: + return "ENONET"; + case SE_ENOPKG: + return "ENOPKG"; + case SE_EREMOTE: + return "EREMOTE"; + case SE_ENOLINK: + return "ENOLINK"; + case SE_EADV: + return "EADV"; + case SE_ESRMNT: + return "ESRMNT"; + case SE_ECOMM: + return "ECOMM"; + case SE_EPROTO: + return "EPROTO"; + case SE_EMULTIHOP: + return "EMULTIHOP"; + case SE_EDOTDOT: + return "EDOTDOT"; + case SE_EBADMSG: + return "EBADMSG"; + case SE_EOVERFLOW: + return "EOVERFLOW"; + case SE_ENOTUNIQ: + return "ENOTUNIQ"; + case SE_EBADFD: + return "EBADFD"; + case SE_EREMCHG: + return "EREMCHG"; + case SE_ELIBACC: + return "ELIBACC"; + case SE_ELIBBAD: + return "ELIBBAD"; + case SE_ELIBSCN: + return "ELIBSCN"; + case SE_ELIBMAX: + return "ELIBMAX"; + case SE_ELIBEXEC: + return "ELIBEXEC"; + case SE_EILSEQ: + return "EILSEQ"; + case SE_ERESTART: + return "ERESTART"; + case SE_ESTRPIPE: + return "ESTRPIPE"; + case SE_EUSERS: + return "EUSERS"; + case SE_ENOTSOCK: + return "ENOTSOCK"; + case SE_EDESTADDRREQ: + return "EDESTADDRREQ"; + case SE_EMSGSIZE: + return "EMSGSIZE"; + case SE_EPROTOTYPE: + return "EPROTOTYPE"; + case SE_ENOPROTOOPT: + return "ENOPROTOOPT"; + case SE_EPROTONOSUPPORT: + return "EPROTONOSUPPORT"; + case SE_ESOCKTNOSUPPORT: + return "ESOCKTNOSUPPORT"; + case SE_EOPNOTSUPP: + return "EOPNOTSUPP"; + case SE_EPFNOSUPPORT: + return "EPFNOSUPPORT"; + case SE_EAFNOSUPPORT: + return "EAFNOSUPPORT"; + case SE_EADDRINUSE: + return "EADDRINUSE"; + case SE_EADDRNOTAVAIL: + return "EADDRNOTAVAIL"; + case SE_ENETDOWN: + return "ENETDOWN"; case SE_ENETUNREACH: return "ENETUNREACH"; - case SE_EINPROGRESS: - return "EINPROGRESS"; - case SE_ETIMEDOUT: - return "ETIMEDOUT"; + case SE_ENETRESET: + return "ENETRESET"; + case SE_ECONNABORTED: + return "ECONNABORTED"; case SE_ECONNRESET: return "ECONNRESET"; - case SE_ECONNREFUSED: - return "ECONNREFUSED"; - case SE_ERESTARTNOHAND: - return "ERESTARTNOHAND"; - case SE_EADDRNOTAVAIL: - return "EADDRNOTAVAIL"; + case SE_ENOBUFS: + return "ENOBUFS"; + case SE_EISCONN: + return "EISCONN"; case SE_ENOTCONN: return "ENOTCONN"; - case SE_ENETDOWN: - return "ENETDOWN"; - case SE_EOPNOTSUPP: - return "EOPNOTSUPP"; - case SE_ENOTSOCK: - return "ENOTSOCK"; - case SE_ERESTART_RESTARTBLOCK: - return "ERESTART_RESTARTBLOCK"; - case SE_EADDRINUSE: - return "EADDRINUSE"; - case SE_EPROTOTYPE: - return "EPROTOTYPE"; + case SE_ESHUTDOWN: + return "ESHUTDOWN"; + case SE_ETOOMANYREFS: + return "ETOOMANYREFS"; + case SE_ETIMEDOUT: + return "ETIMEDOUT"; + case SE_ECONNREFUSED: + return "ECONNREFUSED"; + case SE_EHOSTDOWN: + return "EHOSTDOWN"; + case SE_EHOSTUNREACH: + return "EHOSTUNREACH"; case SE_EALREADY: return "EALREADY"; + case SE_EINPROGRESS: + return "EINPROGRESS"; + case SE_ESTALE: + return "ESTALE"; + case SE_EUCLEAN: + return "EUCLEAN"; + case SE_ENOTNAM: + return "ENOTNAM"; + case SE_ENAVAIL: + return "ENAVAIL"; + case SE_EISNAM: + return "EISNAM"; + case SE_EREMOTEIO: + return "EREMOTEIO"; + case SE_EDQUOT: + return "EDQUOT"; case SE_ENOMEDIUM: return "ENOMEDIUM"; + case SE_EMEDIUMTYPE: + return "EMEDIUMTYPE"; + case SE_ECANCELED: + return "ECANCELED"; + case SE_ERESTARTSYS: + return "ERESTARTSYS"; + case SE_ERESTARTNOINTR: + return "ERESTARTNOINTR"; + case SE_ERESTARTNOHAND: + return "ERESTARTNOHAND"; + case SE_ENOIOCTLCMD: + return "ENOIOCTLCMD"; + case SE_ERESTART_RESTARTBLOCK: + return "ERESTART_RESTARTBLOCK"; + case SE_EBADHANDLE: + return "EBADHANDLE"; + case SE_ENOTSYNC: + return "ENOTSYNC"; + case SE_EBADCOOKIE: + return "EBADCOOKIE"; + case SE_ENOTSUPP: + return "ENOTSUPP"; + case SE_ETOOSMALL: + return "ETOOSMALL"; + case SE_ESERVERFAULT: + return "ESERVERFAULT"; + case SE_EBADTYPE: + return "EBADTYPE"; + case SE_EJUKEBOX: + return "EJUKEBOX"; + case SE_EIOCBQUEUED: + return "EIOCBQUEUED"; + case SE_EIOCBRETRY: + return "EIOCBRETRY"; default: ASSERT(false); return ""; @@ -276,8 +482,8 @@ const char* sinsp_utils::errno_to_str(int32_t code) } // -// errno to string conversion. -// Only the first 40 error codes are currently implemented +// signal to string conversion. +// Only non-extremely-obscure signals are implemented // const char* sinsp_utils::signal_to_str(uint8_t code) { @@ -350,7 +556,7 @@ const char* sinsp_utils::signal_to_str(uint8_t code) } } -bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, char* targetbuf, uint32_t targetbuf_size) +bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, char* targetbuf, uint32_t targetbuf_size, bool resolve) { if(stype == SCAP_FD_IPV4_SOCK) { @@ -360,19 +566,17 @@ bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, cha if(sinfo->m_ipv4info.m_fields.m_l4proto == SCAP_L4_TCP || sinfo->m_ipv4info.m_fields.m_l4proto == SCAP_L4_UDP) { + ipv4tuple addr; + addr.m_fields.m_sip = *(uint32_t*)sb; + addr.m_fields.m_sport = sinfo->m_ipv4info.m_fields.m_sport; + addr.m_fields.m_dip = *(uint32_t*)db; + addr.m_fields.m_dport = sinfo->m_ipv4info.m_fields.m_dport; + addr.m_fields.m_l4proto = sinfo->m_ipv4info.m_fields.m_l4proto; + string straddr = ipv4tuple_to_string(&addr, resolve); snprintf(targetbuf, - targetbuf_size, - "%u.%u.%u.%u:%u->%u.%u.%u.%u:%u", - (unsigned int)(uint8_t)sb[0], - (unsigned int)(uint8_t)sb[1], - (unsigned int)(uint8_t)sb[2], - (unsigned int)(uint8_t)sb[3], - (unsigned int)sinfo->m_ipv4info.m_fields.m_sport, - (unsigned int)(uint8_t)db[0], - (unsigned int)(uint8_t)db[1], - (unsigned int)(uint8_t)db[2], - (unsigned int)(uint8_t)db[3], - (unsigned int)sinfo->m_ipv4info.m_fields.m_dport); + targetbuf_size, + "%s", + straddr.c_str()); } else if(sinfo->m_ipv4info.m_fields.m_l4proto == SCAP_L4_ICMP || sinfo->m_ipv4info.m_fields.m_l4proto == SCAP_L4_RAW) @@ -398,45 +602,43 @@ bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, cha } else if(stype == SCAP_FD_IPV6_SOCK) { - uint8_t* sip6 = (uint8_t*)sinfo->m_ipv6info.m_fields.m_sip; - uint8_t* dip6 = (uint8_t*)sinfo->m_ipv6info.m_fields.m_dip; - uint8_t* sip = ((uint8_t*)(sinfo->m_ipv6info.m_fields.m_sip)) + 12; - uint8_t* dip = ((uint8_t*)(sinfo->m_ipv6info.m_fields.m_dip)) + 12; + uint8_t* sip6 = (uint8_t*)sinfo->m_ipv6info.m_fields.m_sip.m_b; + uint8_t* dip6 = (uint8_t*)sinfo->m_ipv6info.m_fields.m_dip.m_b; + uint8_t* sip = ((uint8_t*)(sinfo->m_ipv6info.m_fields.m_sip.m_b)) + 12; + uint8_t* dip = ((uint8_t*)(sinfo->m_ipv6info.m_fields.m_dip.m_b)) + 12; if(sinfo->m_ipv6info.m_fields.m_l4proto == SCAP_L4_TCP || sinfo->m_ipv6info.m_fields.m_l4proto == SCAP_L4_UDP) { if(sinsp_utils::is_ipv4_mapped_ipv6(sip6) && sinsp_utils::is_ipv4_mapped_ipv6(dip6)) { + ipv4tuple addr; + addr.m_fields.m_sip = *(uint32_t*)sip; + addr.m_fields.m_sport = sinfo->m_ipv4info.m_fields.m_sport; + addr.m_fields.m_dip = *(uint32_t*)dip; + addr.m_fields.m_dport = sinfo->m_ipv4info.m_fields.m_dport; + addr.m_fields.m_l4proto = sinfo->m_ipv4info.m_fields.m_l4proto; + string straddr = ipv4tuple_to_string(&addr, resolve); snprintf(targetbuf, - targetbuf_size, - "%u.%u.%u.%u:%u->%u.%u.%u.%u:%u", - (unsigned int)sip[0], - (unsigned int)sip[1], - (unsigned int)sip[2], - (unsigned int)sip[3], - (unsigned int)sinfo->m_ipv4info.m_fields.m_sport, - (unsigned int)dip[0], - (unsigned int)dip[1], - (unsigned int)dip[2], - (unsigned int)dip[3], - (unsigned int)sinfo->m_ipv4info.m_fields.m_dport); + targetbuf_size, + "%s", + straddr.c_str()); return true; } else { char srcstr[INET6_ADDRSTRLEN]; char dststr[INET6_ADDRSTRLEN]; - if(inet_ntop(AF_INET6, sip6, srcstr, sizeof(srcstr)) && + if(inet_ntop(AF_INET6, sip6, srcstr, sizeof(srcstr)) && inet_ntop(AF_INET6, sip6, dststr, sizeof(dststr))) { snprintf(targetbuf, targetbuf_size, - "%s:%u->%s:%u", + "%s:%s->%s:%s", srcstr, - (unsigned int)sinfo->m_ipv4info.m_fields.m_sport, + port_to_string(sinfo->m_ipv6info.m_fields.m_sport, sinfo->m_ipv6info.m_fields.m_l4proto, resolve).c_str(), dststr, - (unsigned int)sinfo->m_ipv4info.m_fields.m_dport); + port_to_string(sinfo->m_ipv6info.m_fields.m_dport, sinfo->m_ipv6info.m_fields.m_l4proto, resolve).c_str()); return true; } } @@ -463,7 +665,7 @@ bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, cha { char srcstr[INET6_ADDRSTRLEN]; char dststr[INET6_ADDRSTRLEN]; - if(inet_ntop(AF_INET6, sip6, srcstr, sizeof(srcstr)) && + if(inet_ntop(AF_INET6, sip6, srcstr, sizeof(srcstr)) && inet_ntop(AF_INET6, sip6, dststr, sizeof(dststr))) { snprintf(targetbuf, @@ -510,10 +712,10 @@ void rewind_to_parent_path(char* targetbase, char** tc, const char** pc, uint32_ // // Args: -// - target: the string where we are supposed to start copying -// - targetbase: the base of the path, i.e. the furthest we can go back when -// following parent directories -// - path: the path to copy +// - target: the string where we are supposed to start copying +// - targetbase: the base of the path, i.e. the furthest we can go back when +// following parent directories +// - path: the path to copy // void copy_and_sanitize_path(char* target, char* targetbase, const char* path) { @@ -633,17 +835,17 @@ void copy_and_sanitize_path(char* target, char* targetbase, const char* path) // // Return false if path2 is an absolute path // -bool sinsp_utils::concatenate_paths(char* target, - uint32_t targetlen, - const char* path1, - uint32_t len1, - const char* path2, +bool sinsp_utils::concatenate_paths(char* target, + uint32_t targetlen, + const char* path1, + uint32_t len1, + const char* path2, uint32_t len2) { if(targetlen < (len1 + len2 + 1)) { - ASSERT(false); strcpy(target, "/PATH_TOO_LONG"); + return false; } if(len2 != 0 && path2[0] != '/') @@ -662,9 +864,13 @@ bool sinsp_utils::concatenate_paths(char* target, bool sinsp_utils::is_ipv4_mapped_ipv6(uint8_t* paddr) { - if(paddr[0] == 0 && paddr[1] == 0 && paddr[2] == 0 && paddr[3] == 0 && paddr[4] == 0 && - paddr[5] == 0 && paddr[6] == 0 && paddr[7] == 0 && paddr[8] == 0 && paddr[9] == 0 && - paddr[10] == 0xff && paddr[11] == 0xff) + if(paddr[0] == 0 && paddr[1] == 0 && paddr[2] == 0 && paddr[3] == 0 && paddr[4] == 0 && + paddr[5] == 0 && paddr[6] == 0 && paddr[7] == 0 && paddr[8] == 0 && paddr[9] == 0 && + ( + ( paddr[10] == 0xff && paddr[11] == 0xff) || // A real IPv4 address + (paddr[10] == 0 && paddr[11] == 0 && paddr[12] == 0 && paddr[13] == 0 && paddr[14] == 0 && paddr[15] == 0) // all zero address, assume IPv4 as well + ) + ) { return true; } @@ -687,9 +893,9 @@ const struct ppm_param_info* sinsp_utils::find_longest_matching_evt_param(string { const struct ppm_param_info* pi = &ei->params[k]; const char* an = pi->name; - uint32_t alen = strlen(an); + uint32_t alen = (uint32_t)strlen(an); string subs = string(name, 0, alen); - + if(subs == an) { if(alen > maxlen) @@ -711,13 +917,297 @@ void sinsp_utils::get_filtercheck_fields_info(OUT vector &env, const vector &keys) +{ + for (const string key : keys) + { + for(const auto& env_var : env) + { + if((env_var.size() > key.size()) && !env_var.compare(0, key.size(), key) && (env_var[key.size()] == '=')) + { + out = env_var.substr(key.size()+1); + return true; + } + } + } + return false; +} + +bool sinsp_utils::find_env(std::string &out, const vector &env, const std::string &key) +{ + const vector keys = { key }; + return find_first_env(out, env, keys); +} + +void sinsp_utils::split_container_image(const std::string &image, + std::string &hostname, + std::string &port, + std::string &name, + std::string &tag, + std::string &digest, + bool split_repo) +{ + auto split = [](const std::string &src, std::string &part1, std::string &part2, const std::string sep) + { + size_t pos = src.find(sep); + if(pos != std::string::npos) + { + part1 = src.substr(0, pos); + part2 = src.substr(pos+1); + return true; + } + return false; + }; + + std::string hostport, rem, rem2, repo; + + hostname = port = name = tag = digest = ""; + + if(split(image, hostport, rem, "/")) + { + repo = hostport + "/"; + if(!split(hostport, hostname, port, ":")) + { + hostname = hostport; + port = ""; + } + } + else + { + hostname = ""; + port = ""; + rem = image; + } + + if(split(rem, rem2, digest, "@")) + { + if(!split(rem2, name, tag, ":")) + { + name = rem2; + tag = ""; + } + } + else + { + digest = ""; + if(!split(rem, name, tag, ":")) + { + name = rem; + tag = ""; + } + } + + if(!split_repo) + { + name = repo + name; + } +} + +void sinsp_utils::parse_suppressed_types(const std::vector &supp_strs, + std::vector *supp_ids) +{ + for (auto ii = 0; ii < PPM_EVENT_MAX; ii++) + { + auto iter = std::find(supp_strs.begin(), supp_strs.end(), + event_name_by_id(ii)); + if (iter != supp_strs.end()) + { + supp_ids->push_back(ii); + } + } +} + +const char* sinsp_utils::event_name_by_id(uint16_t id) +{ + if (id >= PPM_EVENT_MAX) + { + ASSERT(false); + return "NA"; + } + return g_infotables.m_event_info[id].name; +} + +void sinsp_utils::ts_to_string(uint64_t ts, OUT string* res, bool date, bool ns) +{ + struct tm *tm; + time_t Time; + uint64_t sec = ts / ONE_SECOND_IN_NS; + uint64_t nsec = ts % ONE_SECOND_IN_NS; + int32_t thiszone = gmt2local(0); + int32_t s = (sec + thiszone) % 86400; + int32_t bufsize = 0; + char buf[256]; + + if(date) + { + Time = (sec + thiszone) - s; + tm = gmtime (&Time); + if(!tm) + { + bufsize = sprintf(buf, " "); + } + else + { + bufsize = sprintf(buf, "%04d-%02d-%02d ", + tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); + } + } + + if(ns) + { + sprintf(buf + bufsize, "%02d:%02d:%02d.%09u", + s / 3600, (s % 3600) / 60, s % 60, (unsigned)nsec); + } + else + { + sprintf(buf + bufsize, "%02d:%02d:%02d", + s / 3600, (s % 3600) / 60, s % 60); + } + + *res = buf; +} + +#define TS_STR_FMT "YYYY-MM-DDTHH:MM:SS-0000" +void sinsp_utils::ts_to_iso_8601(uint64_t ts, OUT string* res) +{ + static const char *fmt = TS_STR_FMT; + char buf[sizeof(TS_STR_FMT)]; + uint64_t ns = ts % ONE_SECOND_IN_NS; + time_t sec = ts / ONE_SECOND_IN_NS; + + if(strftime(buf, sizeof(buf), "%FT%T", gmtime(&sec)) == 0) + { + *res = fmt; + return; + } + + *res = buf; + if(sprintf(buf, ".%09u", (unsigned) ns) < 0) + { + *res = fmt; + return; + } + *res += buf; + if(strftime(buf, sizeof(buf), "%z", gmtime(&sec)) == 0) + { + *res = fmt; + return; + } + *res += buf; +} + /////////////////////////////////////////////////////////////////////////////// -// gettimeofday() windows implementation +// Time utility functions. /////////////////////////////////////////////////////////////////////////////// + +bool sinsp_utils::parse_iso_8601_utc_string(const std::string& time_str, uint64_t &ns) +{ +#ifndef _WIN32 + char *rem; + + struct tm tm_time = {0}; + rem = strptime(time_str.c_str(), "%Y-%m-%dT%H:%M:", &tm_time); + if(rem == NULL || *rem == '\0') + { + return false; + } + tm_time.tm_isdst = -1; // strptime does not set this, signal timegm to determine DST + ns = timegm(&tm_time) * ONE_SECOND_IN_NS; + + // Handle the possibly fractional seconds now. Also verify + // that the string ends with Z. + double fractional_secs; + if(sscanf(rem, "%lfZ", &fractional_secs) != 1) + { + return false; + } + + ns += (fractional_secs * ONE_SECOND_IN_NS); + + return true; +#else + throw sinsp_exception("parse_iso_8601_utc_string() not implemented on Windows"); +#endif +} + +time_t get_epoch_utc_seconds(const std::string& time_str, const std::string& fmt) +{ +#ifndef _WIN32 + if(time_str.empty() || fmt.empty()) + { + throw sinsp_exception("get_epoch_utc_seconds(): empty time or format string."); + } + struct tm tm_time = {0}; + strptime(time_str.c_str(), fmt.c_str(), &tm_time); + tm_time.tm_isdst = -1; // strptime does not set this, signal timegm to determine DST + return timegm(&tm_time); +#else + throw sinsp_exception("get_epoch_utc_seconds() not implemented on Windows"); +#endif // _WIN32 +} + +time_t get_epoch_utc_seconds_now() +{ +#ifndef _WIN32 + time_t rawtime; + time(&rawtime); + return timegm(gmtime(&rawtime)); +#else + throw sinsp_exception("get_now_seconds() not implemented on Windows"); +#endif // _WIN32 +} + +// gettimeofday() windows implementation #ifdef _WIN32 #include -#include +#include const __int64 DELTA_EPOCH_IN_MICROSECS = 11644473600000000; @@ -741,7 +1231,7 @@ int gettimeofday(struct timeval *tv, struct timezone2 *tz) // converting file time to unix epoch // tmpres /= 10; // convert into microseconds - tmpres -= DELTA_EPOCH_IN_MICROSECS; + tmpres -= DELTA_EPOCH_IN_MICROSECS; tv->tv_sec = (__int32)(tmpres*0.000001); tv->tv_usec =(tmpres%1000000); @@ -781,149 +1271,373 @@ string sinsp_gethostname() /////////////////////////////////////////////////////////////////////////////// // tuples to string /////////////////////////////////////////////////////////////////////////////// -string ipv4tuple_to_string(ipv4tuple* tuple) +string port_to_string(uint16_t port, uint8_t l4proto, bool resolve) +{ + string ret = ""; + if(resolve) + { + string proto = ""; + if(l4proto == SCAP_L4_TCP) + { + proto = "tcp"; + } + else if(l4proto == SCAP_L4_UDP) + { + proto = "udp"; + } + + // `port` is saved with network byte order + struct servent * res; + res = getservbyport(ntohs(port), (proto != "") ? proto.c_str() : NULL); // best effort! + if (res) + { + ret = res->s_name; + } + else + { + ret = to_string(port); + } + } + else + { + ret = to_string(port); + } + + return ret; +} + +string ipv4serveraddr_to_string(ipv4serverinfo* addr, bool resolve) { char buf[50]; - sprintf(buf, - "%d.%d.%d.%d:%d->%d.%d.%d.%d:%d", - (tuple->m_fields.m_sip & 0xFF), - ((tuple->m_fields.m_sip & 0xFF00) >> 8), - ((tuple->m_fields.m_sip & 0xFF0000) >> 16), - ((tuple->m_fields.m_sip & 0xFF000000) >> 24), - tuple->m_fields.m_sport, - (tuple->m_fields.m_dip & 0xFF), - ((tuple->m_fields.m_dip & 0xFF00) >> 8), - ((tuple->m_fields.m_dip & 0xFF0000) >> 16), - ((tuple->m_fields.m_dip & 0xFF000000) >> 24), - tuple->m_fields.m_dport); + uint8_t *ip = (uint8_t *)&addr->m_ip; + + // IP address is in network byte order regardless of host endianness + snprintf(buf, + sizeof(buf), + "%d.%d.%d.%d:%s", ip[0], ip[1], ip[2], ip[3], + port_to_string(addr->m_port, addr->m_l4proto, resolve).c_str()); + + return string(buf); +} + +string ipv4tuple_to_string(ipv4tuple* tuple, bool resolve) +{ + char buf[100]; + + ipv4serverinfo info; + + info.m_ip = tuple->m_fields.m_sip; + info.m_port = tuple->m_fields.m_sport; + info.m_l4proto = tuple->m_fields.m_l4proto; + string source = ipv4serveraddr_to_string(&info, resolve); + + info.m_ip = tuple->m_fields.m_dip; + info.m_port = tuple->m_fields.m_dport; + info.m_l4proto = tuple->m_fields.m_l4proto; + string dest = ipv4serveraddr_to_string(&info, resolve); + + snprintf(buf, sizeof(buf), "%s->%s", source.c_str(), dest.c_str()); + return string(buf); } -string ipv6tuple_to_string(_ipv6tuple* tuple) +string ipv6serveraddr_to_string(ipv6serverinfo* addr, bool resolve) +{ + char address[100]; + char buf[200]; + + if(NULL == inet_ntop(AF_INET6, addr->m_ip.m_b, address, 100)) + { + return string(); + } + + snprintf(buf,200,"%s:%s", + address, + port_to_string(addr->m_port, addr->m_l4proto, resolve).c_str()); + + return string(buf); +} + +string ipv6tuple_to_string(_ipv6tuple* tuple, bool resolve) { char source_address[100]; char destination_address[100]; char buf[200]; - if(NULL == inet_ntop(AF_INET6, tuple->m_fields.m_sip, source_address, 100)) + if(NULL == inet_ntop(AF_INET6, tuple->m_fields.m_sip.m_b, source_address, 100)) { return string(); } - if(NULL == inet_ntop(AF_INET6, tuple->m_fields.m_dip, destination_address, 100)) + if(NULL == inet_ntop(AF_INET6, tuple->m_fields.m_dip.m_b, destination_address, 100)) { return string(); } - - snprintf(buf,200,"%s:%u->%s:%u", + + snprintf(buf,200,"%s:%s->%s:%s", source_address, - tuple->m_fields.m_sport, + port_to_string(tuple->m_fields.m_sport, tuple->m_fields.m_l4proto, resolve).c_str(), destination_address, - tuple->m_fields.m_dport); - + port_to_string(tuple->m_fields.m_dport, tuple->m_fields.m_l4proto, resolve).c_str()); + return string(buf); } -string ipv4serveraddr_to_string(ipv4serverinfo* addr) +const char* param_type_to_string(ppm_param_type pt) { - char buf[50]; - sprintf(buf, - "%d.%d.%d.%d:%d", - (addr->m_ip & 0xFF), - ((addr->m_ip & 0xFF00) >> 8), - ((addr->m_ip & 0xFF0000) >> 16), - ((addr->m_ip & 0xFF000000) >> 24), - addr->m_port); - return string(buf); + switch(pt) + { + case PT_NONE: + return "NONE"; + case PT_INT8: + return "INT8"; + case PT_INT16: + return "INT16"; + case PT_INT32: + return "INT32"; + case PT_INT64: + return "INT64"; + case PT_UINT8: + return "UINT8"; + case PT_UINT16: + return "UINT16"; + case PT_UINT32: + return "UINT32"; + case PT_UINT64: + return "UINT64"; + case PT_CHARBUF: + return "CHARBUF"; + case PT_BYTEBUF: + return "BYTEBUF"; + case PT_ERRNO: + return "ERRNO"; + case PT_SOCKADDR: + return "SOCKADDR"; + case PT_SOCKTUPLE: + return "SOCKTUPLE"; + case PT_FD: + return "FD"; + case PT_PID: + return "PID"; + case PT_FDLIST: + return "FDLIST"; + case PT_FSPATH: + return "FSPATH"; + case PT_SYSCALLID: + return "SYSCALLID"; + case PT_SIGTYPE: + return "SIGTYPE"; + case PT_RELTIME: + return "RELTIME"; + case PT_ABSTIME: + return "ABSTIME"; + case PT_PORT: + return "PORT"; + case PT_L4PROTO: + return "L4PROTO"; + case PT_SOCKFAMILY: + return "SOCKFAMILY"; + case PT_BOOL: + return "BOOL"; + case PT_IPV4ADDR: + return "IPV4ADDR"; + case PT_DYN: + return "DYNAMIC"; + case PT_FLAGS8: + return "FLAGS8"; + case PT_FLAGS16: + return "FLAGS16"; + case PT_FLAGS32: + return "FLAGS32"; + case PT_MODE: + return "MODE"; + case PT_UID: + return "UID"; + case PT_GID: + return "GID"; + case PT_SIGSET: + return "SIGSET"; + case PT_IPV4NET: + return "IPV4NET"; + case PT_DOUBLE: + return "DOUBLE"; + case PT_CHARBUFARRAY: + return "CHARBUFARRAY"; + case PT_CHARBUF_PAIR_ARRAY: + return "CHARBUF_PAIR_ARRAY"; + case PT_FSRELPATH: + return "FSRELPATH"; + default: + ASSERT(false); + return ""; + } } -string ipv6serveraddr_to_string(ipv6serverinfo* addr) +const char* print_format_to_string(ppm_print_format fmt) { - char address[100]; - char buf[200]; - - if(NULL == inet_ntop(AF_INET6, addr->m_ip, address, 100)) + switch(fmt) { - return string(); + case PF_DEC: + return "DEC"; + case PF_HEX: + return "HEX"; + case PF_10_PADDED_DEC: + return "10_PADDED_DEC"; + case PF_ID: + return "ID"; + case PF_DIR: + return "DIR"; + case PF_OCT: + return "OCT"; + case PF_NA: + return "NA"; + default: + ASSERT(false); + return "NA"; } - - snprintf(buf,200,"%s:%u", - address, - addr->m_port); - - return string(buf); } /////////////////////////////////////////////////////////////////////////////// -// String split +// String helpers /////////////////////////////////////////////////////////////////////////////// +// +// String split +// vector sinsp_split(const string &s, char delim) { - vector res; - istringstream f(s); - string ts; + vector res; + istringstream f(s); + string ts; - while(getline(f, ts, delim)) + while(getline(f, ts, delim)) { - res.push_back(ts); - } + res.push_back(ts); + } return res; } +// +// trim from start +// +string& ltrim(string &s) +{ + s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun(isspace)))); + return s; +} + +// +// trim from end +// +string& rtrim(string &s) +{ + s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), s.end()); + return s; +} + +// +// trim from both ends +// +string& trim(string &s) +{ + return ltrim(rtrim(s)); +} + +string& replace_in_place(string& str, const string& search, const string& replacement) +{ + string::size_type ssz = search.length(); + string::size_type rsz = replacement.length(); + string::size_type pos = 0; + while((pos = str.find(search, pos)) != string::npos) + { + str.replace(pos, ssz, replacement); + pos += rsz; + ASSERT(pos <= str.length()); + } + return str; +} + +string replace(const string& str, const string& search, const string& replacement) +{ + string s(str); + replace_in_place(s, search, replacement); + return s; +} + + +bool sinsp_utils::endswith(const string& str, const string& ending) +{ + if (ending.size() <= str.size()) + { + return (0 == str.compare(str.length() - ending.length(), ending.length(), ending)); + } + return false; +} + + +bool sinsp_utils::endswith(const char *str, const char *ending, uint32_t lstr, uint32_t lend) +{ + if (lstr >= lend) + { + return (0 == memcmp(ending, str + (lstr - lend), lend)); + } + return 0; +} + + /////////////////////////////////////////////////////////////////////////////// // sinsp_numparser implementation /////////////////////////////////////////////////////////////////////////////// -uint32_t sinsp_numparser::parseu8(const string& str) +uint8_t sinsp_numparser::parseu8(const string& str) { uint32_t res; char temp; - if(std::sscanf(str.c_str(), "%" PRIu8 "%c", &res, &temp) != 1) + if(std::sscanf(str.c_str(), "%" PRIu32 "%c", &res, &temp) != 1) { throw sinsp_exception(str + " is not a valid number"); } - return res; + return (uint8_t)res; } -int32_t sinsp_numparser::parsed8(const string& str) +int8_t sinsp_numparser::parsed8(const string& str) { int32_t res; char temp; - if(std::sscanf(str.c_str(), "%" PRId8 "%c", &res, &temp) != 1) + if(std::sscanf(str.c_str(), "%" PRId32 "%c", &res, &temp) != 1) { throw sinsp_exception(str + " is not a valid number"); } - return res; + return (int8_t)res; } -uint32_t sinsp_numparser::parseu16(const string& str) +uint16_t sinsp_numparser::parseu16(const string& str) { uint32_t res; char temp; - if(std::sscanf(str.c_str(), "%" PRIu16 "%c", &res, &temp) != 1) + if(std::sscanf(str.c_str(), "%" PRIu32 "%c", &res, &temp) != 1) { throw sinsp_exception(str + " is not a valid number"); } - return res; + return (uint16_t)res; } -int32_t sinsp_numparser::parsed16(const string& str) +int16_t sinsp_numparser::parsed16(const string& str) { int32_t res; char temp; - if(std::sscanf(str.c_str(), "%" PRId16 "%c", &res, &temp) != 1) + if(std::sscanf(str.c_str(), "%" PRId32 "%c", &res, &temp) != 1) { throw sinsp_exception(str + " is not a valid number"); } - return res; + return (int16_t)res; } uint32_t sinsp_numparser::parseu32(const string& str) @@ -1025,3 +1739,109 @@ bool sinsp_numparser::tryparsed64(const string& str, int64_t* res) return true; } + +bool sinsp_numparser::tryparseu32_fast(const char* str, uint32_t strlen, uint32_t* res) +{ + const char* p = str; + const char* end = str + strlen; + + *res = 0; + + while(p < end) + { + if(*p >= '0' && *p <= '9') + { + *res = (*res) * 10 + (*p - '0'); + } + else + { + return false; + } + + p++; + } + + return true; +} + +bool sinsp_numparser::tryparsed32_fast(const char* str, uint32_t strlen, int32_t* res) +{ + const char* p = str; + const char* end = str + strlen; + + *res = 0; + + while(p < end) + { + if(*p >= '0' && *p <= '9') + { + *res = (*res) * 10 + (*p - '0'); + } + else + { + return false; + } + + p++; + } + + return true; +} + +/////////////////////////////////////////////////////////////////////////////// +// JSON helpers +/////////////////////////////////////////////////////////////////////////////// + +std::string get_json_string(const Json::Value& obj, const std::string& name) +{ + std::string ret; + const Json::Value& json_val = obj[name]; + if(!json_val.isNull() && json_val.isConvertibleTo(Json::stringValue)) + { + ret = json_val.asString(); + } + return ret; +} + +/////////////////////////////////////////////////////////////////////////////// +// socket helpers +/////////////////////////////////////////////////////////////////////////////// + +bool set_socket_blocking(int sock, bool block) +{ +#ifndef _WIN32 + int arg = block ? 0 : 1; + if(ioctl(sock, FIONBIO, &arg) == -1) +#else + u_long arg = block ? 0 : 1; + if(ioctlsocket(sock, FIONBIO, &arg) == -1) +#endif // _WIN32 + { + return false; + } + return true; +} + +unsigned int read_num_possible_cpus(void) +{ + static const char *fcpu = "/sys/devices/system/cpu/possible"; + unsigned int start, end, possible_cpus = 0; + char buff[128]; + FILE *fp; + + fp = fopen(fcpu, "r"); + if (!fp) { + return possible_cpus; + } + + while (fgets(buff, sizeof(buff), fp)) { + if (sscanf(buff, "%u-%u", &start, &end) == 2) { + possible_cpus = start == 0 ? end + 1 : 0; + break; + } + } + + fclose(fp); + + return possible_cpus; +} diff --git a/userspace/libsinsp/utils.h b/userspace/libsinsp/utils.h index 16efa19dfa..930a81d3c5 100644 --- a/userspace/libsinsp/utils.h +++ b/userspace/libsinsp/utils.h @@ -1,24 +1,41 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ -#pragma once +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "json/json.h" class sinsp_evttables; +typedef union _sinsp_sockinfo sinsp_sockinfo; +class filter_check_info; + +extern sinsp_evttables g_infotables; /////////////////////////////////////////////////////////////////////////////// // Initializer class. @@ -52,15 +69,21 @@ class sinsp_utils // // // - static bool sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, char* targetbuf, uint32_t targetbuf_size); + static bool sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, char* targetbuf, uint32_t targetbuf_size, bool resolve = false); + + // + // Check if string ends with another + // + static bool endswith(const std::string& str, const std::string& ending); + static bool endswith(const char *str, const char *ending, uint32_t lstr, uint32_t lend); // // Concatenate two paths and puts the result in "target". // If path2 is relative, the concatenation happens and the result is true. // If path2 is absolute, the concatenation does not happen, target contains path2 and the result is false. - // Assumes that path1 is well formed. + // Assumes that path1 is well formed. // - static bool concatenate_paths(char* target, uint32_t targetlen, const char* path1, uint32_t len1, const char* path2, uint32_t len2); + static bool concatenate_paths(char* target, uint32_t targetlen, const char* path1, uint32_t len1, const char* path2, uint32_t len2); // // Determines if an IPv6 address is IPv4-mapped @@ -70,35 +93,89 @@ class sinsp_utils // // Given a string, scan the event list and find the longest argument that the input string contains // - static const struct ppm_param_info* find_longest_matching_evt_param(string name); + static const struct ppm_param_info* find_longest_matching_evt_param(std::string name); // // Get the list of filtercheck fields // - static void get_filtercheck_fields_info(vector* list); + static void get_filtercheck_fields_info(std::vector* list); + + static uint64_t get_current_time_ns(); + + static bool glob_match(const char *pattern, const char *string); + +#ifndef _WIN32 + // + // Print the call stack + // + static void bt(void); +#endif // _WIN32 + + static bool find_first_env(std::string &out, const std::vector &env, const std::vector &keys); + static bool find_env(std::string &out, const std::vector &env, const std::string &key); + + static void split_container_image(const std::string &image, + std::string &hostname, + std::string &port, + std::string &name, + std::string &tag, + std::string &digest, + bool split_repo = true); + + static void parse_suppressed_types(const std::vector &supp_strs, + std::vector *supp_ids); + + static const char* event_name_by_id(uint16_t id); + + static void ts_to_string(uint64_t ts, OUT std::string* res, bool date, bool ns); + + static void ts_to_iso_8601(uint64_t ts, OUT std::string* res); + + // Limited version of iso 8601 time string parsing, that assumes a + // timezone of Z for UTC, but does support parsing fractional seconds, + // unlike get_epoch_utc_seconds_* below. + static bool parse_iso_8601_utc_string(const std::string& time_str, uint64_t &ns); }; /////////////////////////////////////////////////////////////////////////////// // little STL thing to sanitize strings /////////////////////////////////////////////////////////////////////////////// + struct g_invalidchar { - bool operator()(char c) const - { - if(c < -1) - { - return true; - } + bool operator()(char c) const + { + if(c < -1) + { + return true; + } - return !isprint((unsigned)c); + return !isprint((unsigned)c); } }; +inline void sanitize_string(std::string &str) +{ + // It turns out with -O3 (release flags) using erase and + // remove_if is slightly faster than the inline version that + // was here. It's not faster for -O2, and is actually much + // slower without optimization. + // + // Optimize for the release case, then. + str.erase(remove_if(str.begin(), str.end(), g_invalidchar()), str.end()); +} + /////////////////////////////////////////////////////////////////////////////// -// Time functions for Windows +// Time utility functions. /////////////////////////////////////////////////////////////////////////////// + +time_t get_epoch_utc_seconds(const std::string& time_str, const std::string& fmt = "%Y-%m-%dT%H:%M:%SZ"); +time_t get_epoch_utc_seconds_now(); + +// Time functions for Windows + #ifdef _WIN32 -struct timezone2 +struct timezone2 { int32_t tz_minuteswest; bool tz_dsttime; @@ -110,20 +187,56 @@ SINSP_PUBLIC int gettimeofday(struct timeval *tv, struct timezone2 *tz); /////////////////////////////////////////////////////////////////////////////// // gethostname wrapper /////////////////////////////////////////////////////////////////////////////// -string sinsp_gethostname(); +std::string sinsp_gethostname(); /////////////////////////////////////////////////////////////////////////////// // tuples to string /////////////////////////////////////////////////////////////////////////////// -string ipv4tuple_to_string(ipv4tuple* tuple); -string ipv6tuple_to_string(_ipv6tuple* tuple); -string ipv4serveraddr_to_string(ipv4serverinfo* addr); -string ipv6serveraddr_to_string(ipv6serverinfo* addr); + +// each of these functions uses values in network byte order + +std::string ipv4tuple_to_string(ipv4tuple* tuple, bool resolve); +std::string ipv6tuple_to_string(ipv6tuple* tuple, bool resolve); +std::string ipv4serveraddr_to_string(ipv4serverinfo* addr, bool resolve); +std::string ipv6serveraddr_to_string(ipv6serverinfo* addr, bool resolve); + +// `l4proto` should be of type scap_l4_proto, but since it's an enum sometimes +// is used as int and we would have to cast +// `port` must be saved with network byte order +// `l4proto` could be neither TCP nor UDP, in this case any protocol will be +// matched +std::string port_to_string(uint16_t port, uint8_t l4proto, bool resolve); + +const char* param_type_to_string(ppm_param_type pt); +const char* print_format_to_string(ppm_print_format fmt); /////////////////////////////////////////////////////////////////////////////// -// String split +// String helpers /////////////////////////////////////////////////////////////////////////////// -vector sinsp_split(const string &s, char delim); +std::vector sinsp_split(const std::string& s, char delim); + +template +std::string sinsp_join(It begin, It end, char delim) +{ + if(begin == end) + { + return ""; + } + std::stringstream ss; + ss << *begin; + ++begin; + for(auto it = begin; it != end; ++it) + { + ss << delim << *it; + } + return ss.str(); +} + +std::string& ltrim(std::string& s); +std::string& rtrim(std::string& s); +std::string& trim(std::string& s); +std::string& replace_in_place(std::string& s, const std::string& search, const std::string& replacement); +std::string replace(const std::string& str, const std::string& search, const std::string& replacement); /////////////////////////////////////////////////////////////////////////////// // number parser @@ -131,17 +244,153 @@ vector sinsp_split(const string &s, char delim); class sinsp_numparser { public: - static uint32_t parseu8(const string& str); - static int32_t parsed8(const string& str); - static uint32_t parseu16(const string& str); - static int32_t parsed16(const string& str); - static uint32_t parseu32(const string& str); - static int32_t parsed32(const string& str); - static uint64_t parseu64(const string& str); - static int64_t parsed64(const string& str); - - static bool tryparseu32(const string& str, uint32_t* res); - static bool tryparsed32(const string& str, int32_t* res); - static bool tryparseu64(const string& str, uint64_t* res); - static bool tryparsed64(const string& str, int64_t* res); + static uint8_t parseu8(const std::string& str); + static int8_t parsed8(const std::string& str); + static uint16_t parseu16(const std::string& str); + static int16_t parsed16(const std::string& str); + static uint32_t parseu32(const std::string& str); + static int32_t parsed32(const std::string& str); + static uint64_t parseu64(const std::string& str); + static int64_t parsed64(const std::string& str); + + static bool tryparseu32(const std::string& str, uint32_t* res); + static bool tryparsed32(const std::string& str, int32_t* res); + static bool tryparseu64(const std::string& str, uint64_t* res); + static bool tryparsed64(const std::string& str, int64_t* res); + + static bool tryparseu32_fast(const char* str, uint32_t strlen, uint32_t* res); + static bool tryparsed32_fast(const char* str, uint32_t strlen, int32_t* res); +}; + +/////////////////////////////////////////////////////////////////////////////// +// JSON helpers +/////////////////////////////////////////////////////////////////////////////// +namespace Json +{ + class Value; +} + +std::string get_json_string(const Json::Value& obj, const std::string& name); +inline std::string json_as_string(const Json::Value& json) +{ + return Json::FastWriter().write(json); +} + +/////////////////////////////////////////////////////////////////////////////// +// A simple class to manage pre-allocated objects in a LIFO +// fashion and make sure all of them are deleted upon destruction. +/////////////////////////////////////////////////////////////////////////////// +template +class simple_lifo_queue +{ +public: + simple_lifo_queue(uint32_t size) + { + uint32_t j; + for(j = 0; j < size; j++) + { + OBJ* newentry = new OBJ; + m_full_list.push_back(newentry); + m_avail_list.push_back(newentry); + } + } + ~simple_lifo_queue() + { + while(!m_avail_list.empty()) + { + OBJ* head = m_avail_list.front(); + delete head; + m_avail_list.pop_front(); + } + } + void push(OBJ* newentry) + + { + m_avail_list.push_front(newentry); + } + + OBJ* pop() + { + if(m_avail_list.empty()) + { + return NULL; + } + OBJ* head = m_avail_list.front(); + m_avail_list.pop_front(); + return head; + } + + bool empty() + { + return m_avail_list.empty(); + } + +private: + std::list m_avail_list; + std::list m_full_list; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Case-insensitive string find. +/////////////////////////////////////////////////////////////////////////////// +template +struct ci_equal +{ + ci_equal(const std::locale& loc) : m_loc(loc) {} + bool operator()(charT ch1, charT ch2) + { + return std::toupper(ch1, m_loc) == std::toupper(ch2, m_loc); + } +private: + const std::locale& m_loc; +}; + +template +int ci_find_substr(const T& str1, const T& str2, const std::locale& loc = std::locale()) +{ + typename T::const_iterator it = std::search(str1.begin(), str1.end(), + str2.begin(), str2.end(), ci_equal(loc) ); + if(it != str1.end()) { return it - str1.begin(); } + return -1; +} + +struct ci_compare +{ + // less-than, for use in STL containers + bool operator() (const std::string& a, const std::string& b) const + { +#ifndef _WIN32 + return strcasecmp(a.c_str(), b.c_str()) < 0; +#else + return lstrcmpiA(a.c_str(), b.c_str()) < 0; +#endif // _WIN32 + } + + static bool is_equal(const std::string& a, const std::string& b) + { +#ifndef _WIN32 + return strcasecmp(a.c_str(), b.c_str()) == 0; +#else + return lstrcmpiA(a.c_str(), b.c_str()) == 0; +#endif // _WIN32 + } }; + +/////////////////////////////////////////////////////////////////////////////// +// socket helpers +/////////////////////////////////////////////////////////////////////////////// + +bool set_socket_blocking(int sock, bool block); + +unsigned int read_num_possible_cpus(void); + +/////////////////////////////////////////////////////////////////////////////// +// hashing helpers +/////////////////////////////////////////////////////////////////////////////// + +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3876.pdf +template +inline void hash_combine(std::size_t &seed, const T& val) +{ + seed ^= std::hash()(val) + 0x9e3779b9 + (seed<<6) + (seed>>2); +} diff --git a/userspace/libsinsp/value_parser.cpp b/userspace/libsinsp/value_parser.cpp new file mode 100644 index 0000000000..8edd31c80a --- /dev/null +++ b/userspace/libsinsp/value_parser.cpp @@ -0,0 +1,230 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 "sinsp.h" +#include "sinsp_int.h" +#include "value_parser.h" + +#ifdef _WIN32 +#pragma comment(lib, "Ws2_32.lib") +#include +#else +#include +#endif + +size_t sinsp_filter_value_parser::string_to_rawval(const char* str, uint32_t len, uint8_t *storage, string::size_type max_len, ppm_param_type ptype) +{ + size_t parsed_len; + + switch(ptype) + { + case PT_INT8: + *(int8_t*)storage = sinsp_numparser::parsed8(str); + parsed_len = sizeof(int8_t); + break; + case PT_INT16: + *(int16_t*)storage = sinsp_numparser::parsed16(str); + parsed_len = sizeof(int16_t); + break; + case PT_INT32: + *(int32_t*)storage = sinsp_numparser::parsed32(str); + parsed_len = sizeof(int32_t); + break; + case PT_INT64: + case PT_FD: + case PT_ERRNO: + *(int64_t*)storage = sinsp_numparser::parsed64(str); + parsed_len = sizeof(int64_t); + break; + case PT_L4PROTO: // This can be resolved in the future + case PT_FLAGS8: + case PT_UINT8: + *(uint8_t*)storage = sinsp_numparser::parseu8(str); + parsed_len = sizeof(int8_t); + break; + case PT_PORT: + { + string in(str); + + if(in.empty()) + { + *(uint16_t*)storage = 0; + } + else + { + // if the string is made only of numbers + if(strspn(in.c_str(), "0123456789") == in.size()) + { + *(uint16_t*)storage = stoi(in); + } + else + { + struct servent* se = getservbyname(in.c_str(), NULL); + + if(se == NULL) + { + throw sinsp_exception("unrecognized protocol " + in); + } + else + { + *(uint16_t*)storage = ntohs(getservbyname(in.c_str(), NULL)->s_port); + } + } + } + + parsed_len = sizeof(int16_t); + break; + } + case PT_FLAGS16: + case PT_UINT16: + *(uint16_t*)storage = sinsp_numparser::parseu16(str); + parsed_len = sizeof(uint16_t); + break; + case PT_FLAGS32: + case PT_UINT32: + case PT_MODE: + *(uint32_t*)storage = sinsp_numparser::parseu32(str); + parsed_len = sizeof(uint32_t); + break; + case PT_UINT64: + *(uint64_t*)storage = sinsp_numparser::parseu64(str); + parsed_len = sizeof(uint64_t); + break; + case PT_RELTIME: + case PT_ABSTIME: + *(uint64_t*)storage = sinsp_numparser::parseu64(str); + parsed_len = sizeof(uint64_t); + break; + case PT_CHARBUF: + case PT_SOCKADDR: + case PT_SOCKFAMILY: + { + len = (uint32_t)strlen(str); + if(len >= max_len) + { + throw sinsp_exception("filter parameter too long:" + string(str)); + } + + memcpy(storage, str, len); + *(uint8_t*)(&storage[len]) = 0; + parsed_len = len; + } + break; + case PT_BOOL: + parsed_len = sizeof(uint32_t); + if(string(str) == "true") + { + *(uint32_t*)storage = 1; + } + else if(string(str) == "false") + { + *(uint32_t*)storage = 0; + } + else + { + throw sinsp_exception("filter error: unrecognized boolean value " + string(str)); + } + + break; + case PT_IPADDR: + if(memchr(str, '.', len) != NULL) + { + return string_to_rawval(str, len, storage, max_len, PT_IPV4ADDR); + } + else + { + return string_to_rawval(str, len, storage, max_len, PT_IPV6ADDR); + } + + break; + case PT_IPV4ADDR: + if(inet_pton(AF_INET, str, storage) != 1) + { + throw sinsp_exception("unrecognized IPv4 address " + string(str)); + } + parsed_len = sizeof(struct in_addr); + break; + case PT_IPV6ADDR: + case PT_IPV6NET: + { + ipv6addr *addr = (ipv6addr*) storage; + if(inet_pton(AF_INET6, str, addr->m_b) != 1) + { + throw sinsp_exception("unrecognized IPv6 address " + string(str)); + } + parsed_len = sizeof(ipv6addr); + break; + } + case PT_IPNET: + if(memchr(str, '.', len) != NULL) + { + return string_to_rawval(str, len, storage, max_len, PT_IPV4NET); + } + else + { + return string_to_rawval(str, len, storage, max_len, PT_IPV6NET); + } + + break; + case PT_IPV4NET: + { + stringstream ss(str); + string ip, mask; + ipv4net* net = (ipv4net*)storage; + + if (strchr(str, '/') == NULL) + { + throw sinsp_exception("unrecognized IP network " + string(str)); + } + + getline(ss, ip, '/'); + getline(ss, mask); + + if(inet_pton(AF_INET, ip.c_str(), &net->m_ip) != 1) + { + throw sinsp_exception("unrecognized IP address " + string(str)); + } + + uint32_t cidrlen = sinsp_numparser::parseu8(mask); + + if (cidrlen > 32) + { + throw sinsp_exception("invalid netmask " + mask); + } + + uint32_t j; + net->m_netmask = 0; + + for(j = 0; j < cidrlen; j++) + { + net->m_netmask |= 1<<(31-j); + } + + net->m_netmask = htonl(net->m_netmask); + + parsed_len = sizeof(ipv4net); + break; + } + default: + ASSERT(false); + throw sinsp_exception("wrong parameter type " + to_string((long long) ptype)); + } + + return parsed_len; +} + diff --git a/userspace/libsinsp/value_parser.h b/userspace/libsinsp/value_parser.h new file mode 100644 index 0000000000..1edb209207 --- /dev/null +++ b/userspace/libsinsp/value_parser.h @@ -0,0 +1,30 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once +// +// If this check is used by a filter, extract the constant to compare it to +// Doesn't return the field length because the filtering engine can calculate it. +// + +class sinsp_filter_value_parser +{ + public: + static size_t string_to_rawval(const char* str, uint32_t len, uint8_t *storage, string::size_type max_len, ppm_param_type ptype); +}; diff --git a/userspace/libsinsp/viewinfo.cpp b/userspace/libsinsp/viewinfo.cpp new file mode 100644 index 0000000000..e2072e05fa --- /dev/null +++ b/userspace/libsinsp/viewinfo.cpp @@ -0,0 +1,406 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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 +#include "sinsp.h" +#include "sinsp_int.h" +#include "sinsp_errno.h" +#include "sinsp_signal.h" +#include "filter.h" +#include "filterchecks.h" +#include "chisel.h" +#include "protodecoder.h" + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_view_column_info implementation +/////////////////////////////////////////////////////////////////////////////// +string sinsp_view_column_info::get_field(uint32_t depth) +{ + // Trim the string + replace_in_place(m_field, " ", ""); + replace_in_place(m_field, "\t", ""); + + if(m_field.find("%depth-1") != string::npos) + { + string res = m_field; + replace_in_place(res, "%depth-1", to_string(depth - 1)); + return res; + } + else if(m_field.find("%depth+1") != string::npos) + { + string res = m_field; + replace_in_place(res, "%depth+1", to_string(depth - 1)); + return res; + } + else if(m_field.find("%depth") != string::npos) + { + string res = m_field; + replace_in_place(res, "%depth", to_string(depth)); + return res; + } + else + { + return m_field; + } +} + +string sinsp_view_column_info::get_filter_field(uint32_t depth) +{ + // + // If m_filterfield, return it as an override to m_field + // + if(m_filterfield != "") + { + return m_filterfield; + } + else + { + return get_field(depth); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_view_info implementation +/////////////////////////////////////////////////////////////////////////////// +sinsp_view_info::sinsp_view_info() +{ + m_valid = false; +} + +sinsp_view_info::sinsp_view_info(viewtype type, + string id, + string name, + string description, + vector tags, + vector tips, + vector columns, + vector applies_to, + string filter, + string drilldown_target, + bool use_defaults, + bool is_root, + vector actions, + bool drilldown_increase_depth, + string spectro_type, + bool propagate_filter) +{ + m_id = id; + m_name = name; + m_description = description; + m_does_groupby = false; + m_type = type; + m_tags = tags; + m_tips = tips; + m_columns = columns; + m_drilldown_target = drilldown_target; + m_is_root = is_root; + m_applies_to = applies_to; + m_drilldown_increase_depth = drilldown_increase_depth; + m_spectro_type = spectro_type; + m_propagate_filter = propagate_filter; + + m_use_defaults = use_defaults; + + // + // Make sure the keys go at the beginning + // + move_key_to_front(TEF_IS_GROUPBY_KEY); + move_key_to_front(TEF_IS_KEY); + + // + // Determine the sorting and grouping columns + // + set_sorting_col(); + + m_filter = filter; + m_valid = true; + m_actions = actions; + // init the array for hotkeys for sorting columns + set_col_sorting_hotkeys(); +} + +void sinsp_view_info::set_col_sorting_hotkeys() +{ + const char shift_number_keys [] = {'!', '@', '#', '$', '%', '^', '&', '*', '('}; + uint32_t size = sizeof(shift_number_keys) / sizeof(shift_number_keys[0]); + for(uint32_t i=0; i 1) + { + throw sinsp_exception("view format error: more than one sorting column"); + } + + if((int64_t)m_sortingcol < 0) + { + ASSERT(false); + throw sinsp_exception("view sorting column configuration error"); + } +} + +void sinsp_view_info::apply_tag(string tag) +{ + for(auto it = m_columns.begin(); it != m_columns.end();) + { + bool found = false; + + if(it->m_tags.size() != 0) + { + for(string t : it->m_tags) + { + if(t == tag) + { + found = true; + break; + } + } + + if(!found) + { + it = m_columns.erase(it); + continue; + } + } + + ++it; + } + + // + // Make sure to recalculate the sorting and grouping columns, which could change + // if we remove columns. + // + set_sorting_col(); +} + +void sinsp_view_info::get_col_names_and_sizes(OUT vector* colnames, OUT vector* colsizes) +{ + if(m_type == viewtype::T_LIST) + { + colsizes->push_back(-1); + colnames->push_back(""); + } + + for(auto fit : m_columns) + { + if(m_does_groupby) + { + if((fit.m_flags & TEF_IS_KEY) != 0) + { + continue; + } + + if((fit.m_flags & TEF_IS_GROUPBY_KEY) != 0) + { + colsizes->insert(colsizes->begin(), fit.m_colsize); + colnames->insert(colnames->begin(), fit.m_name); + continue; + } + } + + colsizes->push_back(fit.m_colsize); + colnames->push_back(fit.m_name); + } +} + +void sinsp_view_info::move_key_to_front(uint32_t keyflag) +{ + for(uint32_t j = 0; j < m_columns.size(); j++) + { + if((m_columns[j].m_flags & keyflag) != 0) + { + sinsp_view_column_info ci = m_columns[j]; + + m_columns.erase(m_columns.begin() +j); + m_columns.insert(m_columns.begin(), ci); + return; + } + } +} + +sinsp_view_column_info* sinsp_view_info::get_key() +{ + for(uint32_t j = 0; j < m_columns.size(); j++) + { + if((m_columns[j].m_flags & TEF_IS_GROUPBY_KEY) != 0) + { + return &m_columns[j]; + } + } + + for(uint32_t j = 0; j < m_columns.size(); j++) + { + if((m_columns[j].m_flags & TEF_IS_KEY) != 0) + { + return &m_columns[j]; + } + } + + // The *must* be a key + return NULL; +} + +string sinsp_view_info::get_filter(uint32_t depth) +{ + if(m_filter.find("%depth+1") != string::npos) + { + string res = m_filter; + replace_in_place(res, "%depth+1", to_string(depth + 1)); + replace_in_place(res, "%depth + 1", to_string(depth + 1)); + return res; + } + else if(m_filter.find("%depth-1") != string::npos) + { + string res = m_filter; + replace_in_place(res, "%depth-1", to_string(depth - 1)); + replace_in_place(res, "%depth - 1", to_string(depth - 1)); + return res; + } + else if(m_filter.find("%depth") != string::npos) + { + string res = m_filter; + replace_in_place(res, "%depth", to_string(depth)); + return res; + } + else + { + return m_filter; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// sinsp_view_manager implementation +/////////////////////////////////////////////////////////////////////////////// +void sinsp_view_manager::add(sinsp_view_info* vinfo) +{ + m_views.push_back(*vinfo); +} + +typedef struct view_cmp +{ + bool operator()(const sinsp_view_info& src, const sinsp_view_info& dst) + { + return src.m_name < dst.m_name; + } +}table_row_cmp; + +void sinsp_view_manager::sort_views() +{ + view_cmp cc; + + // + // Sort the list alphabetically + // + sort(m_views.begin(), + m_views.end(), + cc); + + // + // Print the view list for debugging purposes + // + //for(uint32_t j = 0; j < m_views.size(); j++) + //{ + // g_logger.format("> %d) %s", j, m_views[j].m_name.c_str()); + //} +} + +vector* sinsp_view_manager::get_views() +{ + sort_views(); + return &m_views; +} + +uint32_t sinsp_view_manager::get_selected_view() +{ + sort_views(); + + if(m_selected_view_id != "") + { + for(uint32_t j = 0; j < m_views.size(); j++) + { + if(m_views[j].m_id == m_selected_view_id) + { + return j; + } + } + + if(m_selected_view_id == "echo") + { + return VIEW_ID_SPY; + } + else if(m_selected_view_id == "dig") + { + return VIEW_ID_DIG; + } + } + else + { + for(uint32_t j = 0; j < m_views.size(); j++) + { + if(m_views[j].m_is_root) + { + return j; + } + } + } + + throw sinsp_exception("view " + m_selected_view_id + " not found"); + return 0; +} + +void sinsp_view_manager::set_selected_view(string viewid) +{ + m_selected_view_id = viewid; +} diff --git a/userspace/libsinsp/viewinfo.h b/userspace/libsinsp/viewinfo.h new file mode 100644 index 0000000000..027168c3fa --- /dev/null +++ b/userspace/libsinsp/viewinfo.h @@ -0,0 +1,224 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#pragma once + +#define VIEW_ID_SPY -1 +#define VIEW_ID_DIG -2 +#define VIEW_ID_INFO -3 + +// +// Aggregation type for table fields +// +typedef enum sinsp_field_aggregation +{ + A_NONE, + A_SUM, + A_AVG, + A_TIME_AVG, + A_MIN, + A_MAX, +}sinsp_field_aggregation; + +// +// sinsp_view_column_info flags +// +#define TEF_NONE 0 +#define TEF_IS_KEY 1 +#define TEF_IS_SORT_COLUMN (1 << 1) +#define TEF_IS_GROUPBY_KEY (1 << 2) +#define TEF_FILTER_IN_CHILD_ONLY (1 << 3) + +/////////////////////////////////////////////////////////////////////////////// +// Column information +/////////////////////////////////////////////////////////////////////////////// +class sinsp_view_column_info +{ +public: + sinsp_view_column_info() + { + } + + sinsp_view_column_info(string field, + string name, + string description, + uint32_t colsize, + uint32_t flags, + sinsp_field_aggregation aggregation, + sinsp_field_aggregation groupby_aggregation, + vector tags, + string filterfield) + { + m_field = field; + m_name = name; + m_description = description; + m_colsize = colsize; + m_aggregation = aggregation; + m_groupby_aggregation = groupby_aggregation; + m_flags = flags; + m_tags = tags; + m_filterfield = filterfield; + } + + string get_field(uint32_t depth); + string get_filter_field(uint32_t depth); + + string m_field; + string m_name; + string m_description; + uint32_t m_colsize; + sinsp_field_aggregation m_aggregation; + sinsp_field_aggregation m_groupby_aggregation; + uint32_t m_flags; + vector m_tags; + string m_filterfield; +}; + +/////////////////////////////////////////////////////////////////////////////// +// action information +/////////////////////////////////////////////////////////////////////////////// +class sinsp_view_action_info +{ +public: + sinsp_view_action_info(char hotkey, + string command, + string description, + bool ask_confirmation, + bool waitfinish) + { + m_hotkey = hotkey; + m_command = command; + m_description = description; + m_ask_confirmation = ask_confirmation; + m_waitfinish = waitfinish; + } + + char m_hotkey; + string m_command; + string m_description; + bool m_ask_confirmation; + bool m_waitfinish; +}; + +/////////////////////////////////////////////////////////////////////////////// +// View information +/////////////////////////////////////////////////////////////////////////////// +class sinsp_view_info +{ +public: + enum viewtype + { + T_NONE = 0, + T_TABLE, + T_LIST, + T_TEXT, + T_SPECTRO, + }; + + sinsp_view_info(); + sinsp_view_info(viewtype type, + string id, + string name, + string description, + vector tags, + vector tips, + vector columns, + vector applies_to, + string filter, + string drilldown_target, + bool use_defaults, + bool is_root, + vector actions, + bool drilldown_increase_depth, + string spectro_type, + bool propagate_filter); + + void get_col_names_and_sizes(OUT vector* colnames, OUT vector* colsizes); + sinsp_view_column_info* get_key(); + string get_filter(uint32_t depth); + viewtype get_type() + { + return m_type; + } + + bool does_groupby() + { + return m_does_groupby; + } + + void apply_tag(string tag); + + void run_action(sinsp_view_action_info* action); + string m_id; + string m_name; + string m_description; + vector m_tags; + vector m_tips; + uint32_t m_sortingcol; + vector m_applies_to; + vector m_columns; + bool m_use_defaults; + bool m_does_groupby; + viewtype m_type; + bool m_valid; + string m_drilldown_target; + bool m_is_root; + vector m_actions; + vector m_col_sort_hotkeys; + uint32_t max_col_sort_hotkeys; + bool m_drilldown_increase_depth; + bool m_propagate_filter; + string m_spectro_type; + string m_filter; + +private: + void set_sorting_col(); + void move_key_to_front(uint32_t keyflag); + void set_col_sorting_hotkeys(); + + uint32_t m_n_sorting_cols; +}; + + +/////////////////////////////////////////////////////////////////////////////// +// View manager +/////////////////////////////////////////////////////////////////////////////// +class sinsp_view_manager +{ +public: + void add(sinsp_view_info* vinfo); + vector* get_views(); + uint32_t get_selected_view(); + void set_selected_view(string viewid); + size_t size() + { + return m_views.size(); + } + sinsp_view_info* at(uint32_t viewnum) + { + return &m_views[viewnum]; + } + +private: + void sort_views(); + + vector m_views; + + string m_selected_view_id; +}; diff --git a/userspace/sysdig.project b/userspace/sysdig.project new file mode 100644 index 0000000000..76a18f0a8d --- /dev/null +++ b/userspace/sysdig.project @@ -0,0 +1,3331 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make -C ../build/debug all + make -C ../build/debug clean + make -C ../build/debug install + + + + None + $(WorkspacePath) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make -C ../build/release all + make -C ../build/release clean + make -C ../build/release install + + + + None + $(WorkspacePath) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/userspace/sysdig/CMakeLists.txt b/userspace/sysdig/CMakeLists.txt index a5ce09fdca..f8514e80f5 100644 --- a/userspace/sysdig/CMakeLists.txt +++ b/userspace/sysdig/CMakeLists.txt @@ -1,58 +1,123 @@ -include_directories(${PROJECT_SOURCE_DIR}/common) -include_directories(${PROJECT_SOURCE_DIR}/userspace/libscap) -include_directories(${PROJECT_SOURCE_DIR}/userspace/libsinsp) -include_directories(${PROJECT_BINARY_DIR}/userspace/sysdig) +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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_directories("${JSONCPP_INCLUDE}") + +if(NOT WIN32) + include_directories("${TBB_INCLUDE_DIR}") + if(NOT MINIMAL_BUILD) + include_directories("${CURL_INCLUDE_DIR}") + endif() # MINIMAL_BUILD + include_directories("${CURSES_INCLUDE_DIR}") +endif() # NOT WIN32 + +include_directories("${PROJECT_SOURCE_DIR}/common") +include_directories("${PROJECT_SOURCE_DIR}/userspace/libscap") +include_directories("${PROJECT_SOURCE_DIR}/userspace/libsinsp") +include_directories("${PROJECT_BINARY_DIR}/userspace/sysdig") include_directories(.) if(NOT WIN32) set(SOURCE_FILES fields_info.cpp sysdig.cpp) + + set(SOURCE_FILES_CSYSDIG + fields_info.cpp + csysdig.cpp) else() set(SOURCE_FILES fields_info.cpp sysdig.cpp win32/getopt.c) + + set(SOURCE_FILES_CSYSDIG + fields_info.cpp + csysdig.cpp + win32/getopt.c) endif() add_executable(sysdig ${SOURCE_FILES}) - -target_link_libraries(sysdig - sinsp) +add_executable(csysdig ${SOURCE_FILES_CSYSDIG}) if(NOT WIN32) + include_directories(${PROJECT_BINARY_DIR}/driver/src) + target_link_libraries(sysdig + sinsp) + + if(USE_BUNDLED_NCURSES) + add_dependencies(csysdig ncurses) + endif() + + target_link_libraries(csysdig + sinsp + "${CURSES_LIBRARIES}") + + add_subdirectory(man) install(TARGETS sysdig DESTINATION bin) - install(FILES man/sysdig.8 - DESTINATION share/man/man8) + install(TARGETS csysdig + DESTINATION bin) install(DIRECTORY chisels DESTINATION share/sysdig) file(COPY chisels - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") else() + target_link_libraries(sysdig + sinsp) + + target_link_libraries(csysdig + sinsp) + target_link_libraries(sysdig odbc32.lib odbccp32.lib) + target_link_libraries(csysdig odbc32.lib odbccp32.lib) + add_custom_command(TARGET sysdig POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${PROJECT_SOURCE_DIR}/third-party/LuaJIT-2.0.2/src/lua51.dll" + COMMAND "${CMAKE_COMMAND}" -E copy_if_different + "${LUAJIT_SRC}/lua51.dll" "${PROJECT_BINARY_DIR}/$(Configuration)/lua51.dll") add_custom_command(TARGET sysdig POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory + COMMAND "${CMAKE_COMMAND}" -E copy_if_different + "${ZLIB_INCLUDE}/zlib1.dll" + "${PROJECT_BINARY_DIR}/$(Configuration)/") + + add_custom_command(TARGET sysdig POST_BUILD + COMMAND "${CMAKE_COMMAND}" -E copy_directory "${PROJECT_SOURCE_DIR}/userspace/sysdig/chisels" "${PROJECT_BINARY_DIR}/$(Configuration)/chisels") add_custom_command(TARGET sysdig POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different + COMMAND "${CMAKE_COMMAND}" -E copy_if_different $ "${PROJECT_BINARY_DIR}/$(Configuration)/sysdig.exe") + add_custom_command(TARGET csysdig POST_BUILD + COMMAND "${CMAKE_COMMAND}" -E copy_if_different + $ + "${PROJECT_BINARY_DIR}/$(Configuration)/csysdig.exe") + endif() -configure_file(config.h.in config.h) +configure_file(config_sysdig.h.in config_sysdig.h) diff --git a/userspace/sysdig/chisels/COPYING b/userspace/sysdig/chisels/COPYING index d159169d10..9f7ded3381 100644 --- a/userspace/sysdig/chisels/COPYING +++ b/userspace/sysdig/chisels/COPYING @@ -1,339 +1,203 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. +The contents of the driver/ subdirectory are licensed separately--see COPYING.driver. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/userspace/sysdig/chisels/ansiterminal.lua b/userspace/sysdig/chisels/ansiterminal.lua index 2657156b61..26877436c6 100644 --- a/userspace/sysdig/chisels/ansiterminal.lua +++ b/userspace/sysdig/chisels/ansiterminal.lua @@ -1,18 +1,20 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] local pairs = pairs @@ -58,18 +60,30 @@ local function makecolor(name, value) ansiterminal[name] = schar(27) .. '[' .. tostring(value) .. 'm' end -for c, v in pairs(colors) do - makecolor(c, v) +function ansiterminal.enable_color(enable_colors) + if enable_colors == true then + for c, v in pairs(colors) do + makecolor(c, v) + end + else + for name, v in pairs(colors) do + ansiterminal[name] = "" + end + end end function ansiterminal.clearscreen() io.write(schar(27) .. '[' .. "2J") end -function ansiterminal.goto(x, y) +function ansiterminal.moveto(x, y) io.write(schar(27) .. '[' .. tostring(x) .. ";" .. tostring(y) .. 'H') end +function ansiterminal.moveup(n) + io.write(schar(27) .. '[' .. tostring(n) .. 'F') +end + function ansiterminal.clearline() io.write(schar(27) .. '[' .. "2K") end @@ -82,4 +96,12 @@ function ansiterminal.showcursor() io.write(schar(27) .. '[' .. "?25h") end +function ansiterminal.setfgcol(color) + io.write(schar(27) .. '[' .. "38;5;" .. color .. "m") +end + +function ansiterminal.setbgcol(color) + io.write(schar(27) .. '[' .. "48;5;" .. color .. "m") +end + return ansiterminal diff --git a/userspace/sysdig/chisels/around.lua b/userspace/sysdig/chisels/around.lua new file mode 100644 index 0000000000..2197d65242 --- /dev/null +++ b/userspace/sysdig/chisels/around.lua @@ -0,0 +1,134 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Given a filter on the command line, this chisel saves the events that are in a time range around filter matches, and that are on the SAME process/thread. The time range can be adjusted with the dump_range_ms argument. For example, 'sysdig -c around evt.type=open and evt.failed=true' will save two seconds of activity around every failed open."; +short_description = "Export to file the events around the time range where the given filter matches."; +category = "Misc"; + +-- Argument list +args = +{ + { + name = "dump_file_name", + description = "The name of the file where the chisel will write the events related to each syslog entry.", + argtype = "string", + optional = false + }, + { + name = "dump_range_ms", + description = "The time interval to capture *before* and *after* each event, in milliseconds. For example, 500 means that 1 second around each displayed event (.5s before and .5s after) will be saved to . The default value for dump_range_ms is 1000.", + argtype = "int", + optional = true + }, +} +-- Imports and globals +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) +local dump_file_name = nil +local dump_range_ms = "1000" +local entrylist = {} +local capturing = false + +-- Argument notification callback +function on_set_arg(name, val) + if name == "dump_file_name" then + dump_file_name = val + return true + elseif name == "dump_range_ms" then + dump_range_ms = val + return true + end + + return false +end + +-- Initialization callback +function on_init() + -- Request the fields that we need + fpname = chisel.request_field("proc.name") + ftid = chisel.request_field("thread.tid") + fetime = chisel.request_field("evt.time") + + is_tty = sysdig.is_tty() + + if sysdig.get_filter() == "" then + print("no filter specified") + return false + end + + return true +end + +-- Final chisel initialization +function on_capture_start() + if sysdig.is_live() then + print("live capture not supported") + return false + end + + capturing = true + + return true +end + +-- Event parsing callback +function on_event() + -- Extract the event details + local pname = evt.field(fpname) + local tid = evt.field(ftid) + local etime = evt.field(fetime) + + if pname == nil then + pname = "" + end + + print(etime .. " " .. pname .. "(" .. tid .. ")") + + local hi, low = evt.get_ts() + local tid = evt.field(ftid) + table.insert(entrylist, {hi, low, tid}) + + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + if is_tty then + print(terminal.reset) + end + + if capturing then + local sn = sysdig.get_evtsource_name() + + local args = "-F -r" .. sn .. " -w" .. dump_file_name .. " " + + for i, v in ipairs(entrylist) do + if i ~= 1 then + args = args .. " or " + end + + args = args .. "(evt.around[" .. ts_to_str(v[1], v[2]) .. "]=" .. dump_range_ms .. " and thread.tid=" .. v[3] .. ")" + end + + print("\nSaving events around " .. #entrylist .. " syslog entries to " .. dump_file_name) + sysdig.run_sysdig(args) + end +end diff --git a/userspace/sysdig/chisels/bottlenecks.lua b/userspace/sysdig/chisels/bottlenecks.lua index 15c27a301b..de1e96bb8d 100644 --- a/userspace/sysdig/chisels/bottlenecks.lua +++ b/userspace/sysdig/chisels/bottlenecks.lua @@ -1,18 +1,20 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- The number of items to show @@ -58,15 +60,15 @@ function on_event() if prname == nil then prname = "" end - line = string.format("%d) 0.%.9d %s (%d) > %s %s", evt.field(fevnum), - 0, - prname, - evt.field(ftid), - evtype, + line = string.format("%d) 0.%.9d %s (%d) > %s %s", evt.field(fevnum), + 0, + prname, + evt.field(ftid), + evtype, evt.field(fevtargs)) last_lines[tid] = line - else + elseif latency ~= nil then for j = 1, HOW_MANY do if slow_calls[j] == nil or latency > slow_calls[j][1] then prname = evt.field(fprname) @@ -74,11 +76,11 @@ function on_event() prname = "" end - line = string.format("%d) %d.%.9d %s (%d) < %s %s", evt.field(fevnum), - latency / 1000000000, - latency % 1000000000, - prname, evt.field(ftid), - evtype, + line = string.format("%d) %d.%.9d %s (%d) < %s %s", evt.field(fevnum), + latency / 1000000000, + latency % 1000000000, + prname, evt.field(ftid), + evtype, evt.field(fevtargs)) table.insert(slow_calls, j, {latency, last_lines[tid], line}) @@ -86,7 +88,7 @@ function on_event() end end - if table.getn(slow_calls) > HOW_MANY then + if #slow_calls > HOW_MANY then table.remove(slow_calls) end end @@ -94,9 +96,9 @@ function on_event() return true end --- Interval callback, emits the ourput +-- Called by the engine at the end of the capture (Ctrl-C) function on_capture_end() - for j = 1, table.getn(slow_calls) do + for j = 1, #slow_calls do print(slow_calls[j][2]) print(slow_calls[j][3]) end diff --git a/userspace/sysdig/chisels/common.lua b/userspace/sysdig/chisels/common.lua index d7d5d4735d..3d0d664829 100644 --- a/userspace/sysdig/chisels/common.lua +++ b/userspace/sysdig/chisels/common.lua @@ -1,42 +1,107 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] ---[[ +--[[ This file contains a bunch of functions that are helpful in multiple scripts ]]-- ---[[ +--[[ +Serialize the content of a table into a string +]]-- +function st(val, name, skipnewlines, depth) + skipnewlines = skipnewlines or false + depth = depth or 0 + + local tmp = string.rep(" ", depth) + + if name then tmp = tmp .. name .. " = " end + + if type(val) == "table" then + tmp = tmp .. "{" .. (not skipnewlines and "\n" or "") + + for k, v in pairs(val) do + tmp = tmp .. st(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "") + end + + tmp = tmp .. string.rep(" ", depth) .. "}" + elseif type(val) == "number" then + tmp = tmp .. tostring(val) + elseif type(val) == "string" then + tmp = tmp .. string.format("%q", val) + elseif type(val) == "boolean" then + tmp = tmp .. (val and "true" or "false") + else + tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\"" + end + + return tmp +end + +--[[ Extends a string to newlen with spaces ]]-- function extend_string(s, newlen) - ccs = " " - s = s .. string.sub(ccs, 0, newlen - string.len(s)) - return s + if #s < newlen then + local ccs = " " + s = s .. string.sub(ccs, 0, newlen - #s) + return s + else + if newlen > 0 then + return (string.sub(s, 0, newlen - 1) .. " ") + else + return "" + end + end end ---[[ +--[[ +Basic string split. +]]-- +function split(s, delimiter) + local result = {} + + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match) + end + return result +end + +--[[ +Substring matching. +]]-- +function starts_with(str, prefix) + return prefix == "" or str:sub(1, #prefix) == prefix +end + +function ends_with(str, suffix) + return suffix == "" or str:sub(-#suffix) == suffix +end + +--[[ convert a number into a byte representation. E.g. 1230 becomes 1.23K ]]-- function format_bytes(val) - if val > (1024 * 1024 * 1024) then - return string.format("%.2fP", val / (1024 * 1024 * 1024)) - elseif val > (1024 * 1024 * 1024) then - return string.format("%.2fT", val / (1024 * 1024 * 1024)) + if val > (1024 * 1024 * 1024 * 1024 * 1024) then + return string.format("%.2fP", val / (1024 * 1024 * 1024 * 1024 * 1024)) + elseif val > (1024 * 1024 * 1024 * 1024) then + return string.format("%.2fT", val / (1024 * 1024 * 1024 * 1024)) elseif val > (1024 * 1024 * 1024) then return string.format("%.2fG", val / (1024 * 1024 * 1024)) elseif val > (1024 * 1024) then @@ -48,7 +113,7 @@ function format_bytes(val) end end ---[[ +--[[ convert a nanosecond time interval into a s.ns representation. E.g. 1100000000 becomes 1.1s ]]-- @@ -58,25 +123,26 @@ ONE_US_IN_NS=1000 function format_time_interval(val) if val >= (ONE_S_IN_NS) then - return string.format("%u.%02us", val / ONE_S_IN_NS, (val % ONE_S_IN_NS) / 10000000) + return string.format("%u.%02us", math.floor(val / ONE_S_IN_NS), (val % ONE_S_IN_NS) / 10000000) elseif val >= (ONE_S_IN_NS / 100) then - return string.format("%ums", val / (ONE_S_IN_NS / 1000)) + return string.format("%ums", math.floor(val / (ONE_S_IN_NS / 1000))) elseif val >= (ONE_S_IN_NS / 1000) then - return string.format("%u.%02ums", val / (ONE_S_IN_NS / 1000), (val % ONE_MS_IN_NS) / 10000) + return string.format("%u.%02ums", math.floor(val / (ONE_S_IN_NS / 1000)), (val % ONE_MS_IN_NS) / 10000) elseif val >= (ONE_S_IN_NS / 100000) then - return string.format("%uus", val / (ONE_S_IN_NS / 1000000)) + return string.format("%uus", math.floor(val / (ONE_S_IN_NS / 1000000))) elseif val >= (ONE_S_IN_NS / 1000000) then - return string.format("%u.%02uus", val / (ONE_S_IN_NS / 1000000), (val % ONE_US_IN_NS) / 10) + return string.format("%u.%02uus", math.floor(val / (ONE_S_IN_NS / 1000000)), (val % ONE_US_IN_NS) / 10) else return string.format("%uns", val) end end ---[[ +--[[ extract the top num entries from the table t, after sorting them based on the entry value using the function order() ]]-- function pairs_top_by_val(t, num, order) local keys = {} + for k in pairs(t) do keys[#keys+1] = k end table.sort(keys, function(a,b) return order(t, a, b) end) @@ -90,30 +156,143 @@ function pairs_top_by_val(t, num, order) end end ---[[ +--[[ +Timestamp <-> string conversion +]]-- +function ts_to_str(tshi, tslo) + return string.format("%u%.9u", tshi, tslo) +end + +--[[ Pick a key-value table and render it to the console in sorted top format ]]-- -function print_sorted_table(stable, timedelta, result_rendering) - sorted_grtable = pairs_top_by_val(stable, top_number, function(t,a,b) return t[b] < t[a] end) +json = require ("dkjson") - print(extend_string(value_desc, 10) .. key_desc) - print("------------------------------") - - for k,v in sorted_grtable do - if result_rendering == "none" then - print(extend_string(v, 10) .. k) - elseif result_rendering == "bytes" then - print(extend_string(format_bytes(v), 10) .. k) - elseif result_rendering == "time" then - print(extend_string(format_time_interval(v), 10) .. k) - elseif result_rendering == "timepct" then - if timedelta ~= 0 then - pctstr = string.format("%.2f%%", v / timedelta * 100) - else - pctstr = "0.00%" +function print_sorted_table(stable, ts_s, ts_ns, timedelta, viz_info) + local sorted_grtable = pairs_top_by_val(stable, viz_info.top_number, function(t,a,b) return t[b] < t[a] end) + + if viz_info.output_format == "json" then + local jdata = {} + local j = 1 + + for k,v in sorted_grtable do + local vals = split(k, "\001\001") + vals[#vals + 1] = v + jdata[j] = vals + j = j + 1 + end + + local jinfo = {} + + for i, keyname in ipairs(viz_info.key_fld) do + jinfo[i] = {name = keyname, desc = viz_info.key_desc[i], is_key = true} + end + jinfo[3] = {name = viz_info.value_fld, desc = viz_info.value_desc, is_key = false} + + local res = {ts = sysdig.make_ts(ts_s, ts_ns), data = jdata, info = jinfo} + + local str = json.encode(res, { indent = true }) + print(str) + else + -- Same size to extend each string + local EXTEND_STRING_SIZE = 20 + local header = extend_string(viz_info.value_desc, EXTEND_STRING_SIZE) + + for i, fldname in ipairs(viz_info.key_desc) do + header = header .. extend_string(fldname, EXTEND_STRING_SIZE) + end + + print(header) + print("--------------------------------------------------------------------------------") + + for k,v in sorted_grtable do + local keystr = "" + + local singlekeys = split(k, "\001\001") + + for i, singlekey in ipairs(singlekeys) do + if i < #singlekeys then + keystr = keystr .. extend_string(string.sub(singlekey, 0, EXTEND_STRING_SIZE), EXTEND_STRING_SIZE) + else + keystr = keystr .. singlekey + end end - print(extend_string(pctstr, 10) .. k) + if viz_info.value_units == "none" then + print(extend_string(tostring(v), EXTEND_STRING_SIZE) .. keystr) + elseif viz_info.value_units == "bytes" then + print(extend_string(format_bytes(v), EXTEND_STRING_SIZE) .. keystr) + elseif viz_info.value_units == "time" then + print(extend_string(format_time_interval(v), EXTEND_STRING_SIZE) .. keystr) + elseif viz_info.value_units == "timepct" then + if timedelta > 0 then + pctstr = string.format("%.2f%%", v / timedelta * 100) + else + pctstr = "0.00%" + end + + print(extend_string(pctstr, EXTEND_STRING_SIZE) .. keystr) + end end end end + +--[[ +Try to convert user input to a number using tonumber(). If tonumber() returns +'nil', print an error message to the user and exit, otherwise return +tonumber(value). +]]-- +function parse_numeric_input(value, name) + val = tonumber(value) + if val == nil then + print(string.format("Input %s must be a number.", name)) + require ("os") + os.exit() + end + return val +end + +--[[ +Perform a deep copy of a table. +]]-- +function copytable(orig) + local orig_type = type(orig) + local copy + if orig_type == 'table' then + copy = {} + for orig_key, orig_value in next, orig, nil do + copy[copytable(orig_key)] = copytable(orig_value) + end + setmetatable(copy, copytable(getmetatable(orig))) + else -- number, string, boolean, etc + copy = orig + end + return copy +end + +--[[ +Add the content of a table at the end of another one. +]]-- +function concattable(dst, src) + for i=1,#src do + dst[#dst + 1] = src[i] + end + + return dst +end + +--[[ +return the type of a variable. +]]-- +function typeof(var) + local _type = type(var); + if(_type ~= "table" and _type ~= "userdata") then + return _type; + end + local _meta = getmetatable(var); + if(_meta ~= nil and _meta._NAME ~= nil) then + return _meta._NAME; + else + return _type; + end +end diff --git a/userspace/sysdig/chisels/dkjson.lua b/userspace/sysdig/chisels/dkjson.lua new file mode 100644 index 0000000000..6d1a263a8f --- /dev/null +++ b/userspace/sysdig/chisels/dkjson.lua @@ -0,0 +1,843 @@ + -- Module options: + local always_try_using_lpeg = true + local register_global_module_table = false + local global_module_name = 'json' + + --[==[ + +David Kolf's JSON module for Lua 5.1/5.2 +======================================== + +*Version 2.4* + +In the default configuration this module writes no global values, not even +the module table. Import it using + + json = require ("dkjson") + +In environments where `require` or a similar function are not available +and you cannot receive the return value of the module, you can set the +option `register_global_module_table` to `true`. The module table will +then be saved in the global variable with the name given by the option +`global_module_name`. + +Exported functions and values: + +`json.encode (object [, state])` +-------------------------------- + +Create a string representing the object. `Object` can be a table, +a string, a number, a boolean, `nil`, `json.null` or any object with +a function `__tojson` in its metatable. A table can only use strings +and numbers as keys and its values have to be valid objects as +well. It raises an error for any invalid data types or reference +cycles. + +`state` is an optional table with the following fields: + + - `indent` + When `indent` (a boolean) is set, the created string will contain + newlines and indentations. Otherwise it will be one long line. + - `keyorder` + `keyorder` is an array to specify the ordering of keys in the + encoded output. If an object has keys which are not in this array + they are written after the sorted keys. + - `level` + This is the initial level of indentation used when `indent` is + set. For each level two spaces are added. When absent it is set + to 0. + - `buffer` + `buffer` is an array to store the strings for the result so they + can be concatenated at once. When it isn't given, the encode + function will create it temporary and will return the + concatenated result. + - `bufferlen` + When `bufferlen` is set, it has to be the index of the last + element of `buffer`. + - `tables` + `tables` is a set to detect reference cycles. It is created + temporary when absent. Every table that is currently processed + is used as key, the value is `true`. + +When `state.buffer` was set, the return value will be `true` on +success. Without `state.buffer` the return value will be a string. + +`json.decode (string [, position [, null]])` +-------------------------------------------- + +Decode `string` starting at `position` or at 1 if `position` was +omitted. + +`null` is an optional value to be returned for null values. The +default is `nil`, but you could set it to `json.null` or any other +value. + +The return values are the object or `nil`, the position of the next +character that doesn't belong to the object, and in case of errors +an error message. + +Two metatables are created. Every array or object that is decoded gets +a metatable with the `__jsontype` field set to either `array` or +`object`. If you want to provide your own metatables use the syntax + + json.decode (string, position, null, objectmeta, arraymeta) + +To prevent the assigning of metatables pass `nil`: + + json.decode (string, position, null, nil) + +`.__jsonorder` +------------------------- + +`__jsonorder` can overwrite the `keyorder` for a specific table. + +`.__jsontype` +------------------------ + +`__jsontype` can be either `"array"` or `"object"`. This value is only +checked for empty tables. (The default for empty tables is `"array"`). + +`.__tojson (self, state)` +------------------------------------ + +You can provide your own `__tojson` function in a metatable. In this +function you can either add directly to the buffer and return true, +or you can return a string. On errors nil and a message should be +returned. + +`json.null` +----------- + +You can use this value for setting explicit `null` values. + +`json.version` +-------------- + +Set to `"dkjson 2.4"`. + +`json.quotestring (string)` +--------------------------- + +Quote a UTF-8 string and escape critical characters using JSON +escape sequences. This function is only necessary when you build +your own `__tojson` functions. + +`json.addnewline (state)` +------------------------- + +When `state.indent` is set, add a newline to `state.buffer` and spaces +according to `state.level`. + +LPeg support +------------ + +When the local configuration variable `always_try_using_lpeg` is set, +this module tries to load LPeg to replace the `decode` function. The +speed increase is significant. You can get the LPeg module at + . +When LPeg couldn't be loaded, the pure Lua functions stay active. + +In case you don't want this module to require LPeg on its own, +disable the option `always_try_using_lpeg` in the options section at +the top of the module. + +In this case you can later load LPeg support using + +### `json.use_lpeg ()` + +Require the LPeg module and replace the functions `quotestring` and +and `decode` with functions that use LPeg patterns. +This function returns the module table, so you can load the module +using: + + json = require "dkjson".use_lpeg() + +Alternatively you can use `pcall` so the JSON module still works when +LPeg isn't found. + + json = require "dkjson" + pcall (json.use_lpeg) + +### `json.using_lpeg` + +This variable is set to `true` when LPeg was loaded successfully. + +--------------------------------------------------------------------- + +Contact +------- + +You can contact the author by sending an e-mail to 'david' at the +domain 'dkolf.de'. + +--------------------------------------------------------------------- + +*Copyright (C) 2010-2013 David Heiko Kolf* + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + + + diff --git a/userspace/sysdig/chisels/echo_fds.lua b/userspace/sysdig/chisels/echo_fds.lua index 5c32d1722b..0c014ae433 100644 --- a/userspace/sysdig/chisels/echo_fds.lua +++ b/userspace/sysdig/chisels/echo_fds.lua @@ -1,43 +1,69 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "print the data read and written for any FD. Combine this script with a filter to restrict what it shows."; +description = "Print the data read and written for any FD. Combine this script with a filter to restrict what it shows. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. (Blue represents [Write], and Red represents [Read] for all data except when the -pc or -pcontainer argument is used. If used the container.name and container.id will be represented as: Green [host], and Cyan [container]) Container information will contain '[]' around container.name and container.id."; short_description = "Print the data read and written by processes."; category = "I/O"; -args = {} +args = +{ + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} require "common" terminal = require "ansiterminal" +terminal.enable_color(true) + +-- Argument notification callback +function on_set_arg(name, val) + if name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + end + + return true +end -- Initialization callback function on_init() - -- Request the fileds that we need + -- Request the fields that we need fbuf = chisel.request_field("evt.rawarg.data") fisread = chisel.request_field("evt.is_io_read") fres = chisel.request_field("evt.rawarg.res") fname = chisel.request_field("fd.name") + fpname = chisel.request_field("proc.name") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() - -- increase the snaplen so we capture more of the conversation + -- increase the snaplen so we capture more of the conversation sysdig.set_snaplen(2000) -- set the filter - chisel.set_filter("evt.is_io=true and evt.dir=<") + chisel.set_filter("evt.is_io=true and evt.dir=< and evt.rawres>0") chisel.set_event_formatter("%evt.arg.data") return true @@ -45,10 +71,14 @@ end -- Event parsing callback function on_event() - buf = evt.field(fbuf) - isread = evt.field(fisread) - res = evt.field(fres) - name = evt.field(fname) + local buf = evt.field(fbuf) + local isread = evt.field(fisread) + local res = evt.field(fres) + local name = evt.field(fname) + local pname = evt.field(fpname) + local containername = evt.field(fcontainername) + local containerid = evt.field(fcontainerid) + if name == nil then name = "" @@ -57,18 +87,37 @@ function on_event() if res <= 0 then return true end - + + local container = "" + if print_container then + if containername == "host" then + -- Make host green + container = string.format("%s [%s] [%s]", terminal.green, containername, containerid ); + else + -- Make container cyan + container = string.format("%s [%s] [%s]", terminal.cyan, containername, containerid ); + end + end + if isread then - infostr = string.format("%s------ Read %s from %s", terminal.red, format_bytes(res), name) + -- Because container info might be colored make the end of the line the same color as read (red) + name_pname = string.format("%s %s (%s)", terminal.red, name, pname ); + -- When a read occurs show it in red + infostr = string.format("%s------ Read %s from %s %s", terminal.red, format_bytes(res), container, name_pname) else - infostr = string.format("%s------ Write %s to %s", terminal.blue, format_bytes(res), name) + -- Because container info might be colored make the end of the line the same color as write (blue) + name_pname = string.format("%s %s (%s)", terminal.blue, name, pname ); + -- When a write occurs show it in blue + infostr = string.format("%s------ Write %s to %s %s", terminal.blue, format_bytes(res), container, name_pname) end - + + -- Print out the line (if -pc or -pcontainer sandwich container color between either red or blue) print(infostr) return true end +-- Called by the engine at the end of the capture (Ctrl-C) function on_capture_end() print(terminal.reset) end diff --git a/userspace/sysdig/chisels/fdbytes_by.lua b/userspace/sysdig/chisels/fdbytes_by.lua index d1b201c729..8bd429883a 100644 --- a/userspace/sysdig/chisels/fdbytes_by.lua +++ b/userspace/sysdig/chisels/fdbytes_by.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Gropus FD activity based on the given filter field, and returns the key that generated the most input+output bytes. For example, this script can be used to list the processes or TCP ports that generated most traffic." +description = "Groups FD activity based on the given filter field, and returns the key that generated the most input+output bytes. For example, this script can be used to list the processes or TCP ports that generated most traffic." short_description = "I/O bytes, aggregated by an arbitrary filter field" category = "I/O" @@ -25,13 +27,13 @@ args = { { name = "key", - description = "the filter field used for grouping", + description = "The filter field used for grouping", argtype = "string" }, } -- The number of items to show -TOP_NUMBER = 0 +TOP_NUMBER = 30 key_fld = "" -- Argument notification callback @@ -51,7 +53,7 @@ function on_init() key_fld, "evt.rawarg.res", "Bytes", - "evt.is_io=true", + "evt.is_io=true and evt.failed=false", "" .. TOP_NUMBER, "bytes") return true diff --git a/userspace/sysdig/chisels/fdcount_by.lua b/userspace/sysdig/chisels/fdcount_by.lua index 8c6e2b62e5..5df6c70953 100644 --- a/userspace/sysdig/chisels/fdcount_by.lua +++ b/userspace/sysdig/chisels/fdcount_by.lua @@ -1,31 +1,33 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Gropus all the active FDs based on the given filter field, and returns the fd count for each key. For example, it can be used to list the number of connections per process or per IP endpoint." +description = "Groups all the active FDs based on the given filter field, and returns the fd count for each key. For example, it can be used to list the number of connections per process or per IP endpoint." short_description = "FD count, aggregated by an arbitrary filter field" category = "I/O" -- Chisel argument list -args = +args = { { - name = "key", - description = "the filter field used for grouping", + name = "key", + description = "The filter field used for grouping", argtype = "string" }, } @@ -85,7 +87,7 @@ function on_event() return true end --- Interval callback, emits the ourput +-- Called by the engine at the end of the capture (Ctrl-C) function on_capture_end() sorted_grtable = pairs_top_by_val(grtable, TOP_NUMBER, function(t,a,b) return t[b]["c"] < t[a]["c"] end) diff --git a/userspace/sysdig/chisels/fdtime_by.lua b/userspace/sysdig/chisels/fdtime_by.lua index da391a1f6d..72e81f8c12 100644 --- a/userspace/sysdig/chisels/fdtime_by.lua +++ b/userspace/sysdig/chisels/fdtime_by.lua @@ -1,31 +1,33 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Gropus FD activity based on the given filter field, and returns the keys where most time was spent. For example, this script can be used to list the processes or files that caused the biggest I/O latency." +description = "Groups FD activity based on the given filter field, and returns the keys where most time was spent. For example, this script can be used to list the processes or files that caused the biggest I/O latency." short_description = "FD time group by" -category = "IO" +category = "I/O" -- Chisel argument list args = { { name = "key", - description = "the filter field used for grouping", + description = "The filter field used for grouping", argtype = "string" }, } @@ -52,7 +54,7 @@ function on_init() "evt.latency", "Time", "evt.is_io=true", - "time") "" .. TOP_NUMBER, + "time") return true end diff --git a/userspace/sysdig/chisels/fileslower.lua b/userspace/sysdig/chisels/fileslower.lua new file mode 100644 index 0000000000..a67d4b0e1e --- /dev/null +++ b/userspace/sysdig/chisels/fileslower.lua @@ -0,0 +1,173 @@ +--[[ +Copyright (C) 2014 Brendan Gregg. + +This file is part of sysdig. + +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. + +--]] + +--[[ +fileslower.lua - trace file I/O slower than a given threshold. + +USAGE: sysdig -c fileslower min_ms + eg, + + sysdig -c fileslower 10 # show file I/O slower than 10 ms + sysdig -c fileslower 0 # show all file I/O + sysdig -c fileslower "1 disable_colors" # show file I/O slower than 1 ms. w/ no colors + sysdig -pc -c fileslower 0 # show all file I/O and container output + +By default this skips file I/O to /dev. Modify the skip_dev variable in this +chisel to change this behavior. + +Note: The file I/O traced is those matched by the sysdig filter: +"evt.is_io=true and fd.type=file". + +--]] + +-- Chisel description +description = "Trace file I/O slower than a threshold, or all file I/O. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. (Blue represents a process running within a container, and Green represents a host process)"; +short_description = "Trace slow file I/O"; +category = "Performance"; + +skip_dev = true -- skip /dev/... files + +-- Chisel argument list +args = +{ + { + name = "min_ms", + description = "Minimum millisecond threshold for showing file I/O", + argtype = "int", + optional = false + }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + +-- Argument notification callback +function on_set_arg(name, val) + + if name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + elseif name == "min_ms" then + min_ms = parse_numeric_input(val, name) + end + + return true +end + +-- Initialization callback +function on_init() + -- set the following fields on_event() + etype = chisel.request_field("evt.type") + dir = chisel.request_field("evt.dir") + datetime = chisel.request_field("evt.datetime") + fname = chisel.request_field("fd.name") + pname = chisel.request_field("proc.name") + latency = chisel.request_field("evt.latency") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + -- filter for file I/O + chisel.set_filter("evt.is_io=true and fd.type=file") + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(string.format("%-23.23s %-20.20s %-20.20s %-12.12s %-8s %-12s %s", + "evt.datetime", + "container.id", + "container.name", + "proc.name", + "evt.type", + "LATENCY(ms)", + "fd.name")) + print(string.format("%-23.23s %-20.20s %-20.20s %-12.12s %-8s %-12s %s", + "-----------------------", + "------------------------------", + "------------------------------", + "------------", + "--------", + "------------", + "-----------------------------------------")) + else + print(string.format("%-23.23s %-12.12s %-8s %-12s %s", + "evt.datetime", + "proc.name", + "evt.type", + "LATENCY(ms)", + "fd.name")) + print(string.format("%-23.23s %-12.12s %-8s %-12s %s", + "-----------------------", + "------------", + "--------", + "------------", + "-----------------------------------------")) + end + + return true +end + +-- Event callback +function on_event() + + local color = terminal.green + + lat = evt.field(latency) / 1000000 + fn = evt.field(fname) + + if evt.field(dir) == "<" and lat > min_ms then + + -- filter /dev files if needed + if skip_dev == false or string.sub(fn, 0, 5) ~= "/dev/" then + + -- If this is a container modify the output color + if evt.field(fcontainername) ~= "host" then + color = terminal.blue + end + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(color .. string.format("%-23.23s %-20.20s %-20.20s %-12.12s %-8s %12d %s", + evt.field(datetime), + evt.field(fcontainerid), + evt.field(fcontainername), + evt.field(pname), + evt.field(etype), + lat, + fn )) + else + print(color .. string.format("%-23.23s %-12.12s %-8s %12d %s", + evt.field(datetime), + evt.field(pname), + evt.field(etype), + lat, + fn )) + end + end + end + + return true +end diff --git a/userspace/sysdig/chisels/flame.lua b/userspace/sysdig/chisels/flame.lua new file mode 100644 index 0000000000..4e087e4b75 --- /dev/null +++ b/userspace/sysdig/chisels/flame.lua @@ -0,0 +1,525 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + + +-- Chisel description +disabled_description = "Flame graph generator"; +short_description = "Sysdig trace flame graph builder"; +category = "Performance"; + +-- Chisel argument list +args = +{ +} + +require "common" +json = require ("dkjson") + +local CAPTURE_LOGS = true + +local spans = {} +local fid +local flatency +local fcontname +local fexe +local fbuf +local fdir +local ftime +local MAX_DEPTH = 256 +local avg_tree = {} +local full_tree = {} +local max_tree = {} +local min_tree = {} +local logs_tree = {} +local next = next -- make next faster +local PAGE_HEADER = [[ + + + + + Flame UI + + + + + + + + + + + + + + + + + + + + + + + +]] + +-- Argument notification callback +function on_set_arg(name, val) + return true +end + +-- Initialization callback +function on_init() + -- Request the fields needed for this chisel + for j = 0, MAX_DEPTH do + local fname = "span.tag[" .. j .. "]" + local minfo = chisel.request_field(fname) + spans[j] = minfo + end + + fid = chisel.request_field("span.id") + flatency = chisel.request_field("span.duration") + fcontname = chisel.request_field("container.name") + fexe = chisel.request_field("proc.exeline") + fbuf = chisel.request_field("evt.buffer") + fdir = chisel.request_field("evt.dir") + ftid = chisel.request_field("thread.tid") + ftime = chisel.request_field("evt.time") + + -- set the filter + if CAPTURE_LOGS then + chisel.set_filter("(evt.type=tracer) or (evt.is_io_write=true and evt.dir=< and (fd.num=1 or fd.num=2 or fd.name contains log))") + else + chisel.set_filter("evt.type=tracer and evt.dir=<") + end + + return true +end + +-- Add a log entry into the proper place(s) in the log table +function collect_log(tid_tree) + for k,entry in pairs(tid_tree) do + while true do + local lastv = v + k,v = next(entry) + if v == nil then + if lastv.l == nil then + lastv.l = {} + end + + local etime = evt.field(ftime) + local buf = evt.field(fbuf) + local tid = evt.field(ftid) + local hi, low = evt.get_ts() + + local linedata = {t=etime, th=hi, tl=low, tid=tid, b=buf} + + table.insert(lastv.l, linedata) +--print("*** " .. evt.get_num() .. " " .. linedata) +--print(st(logs_tree)) +--print("***************************") + return + end + + entry = v.ch + end + end +end + +-- Parse a tracer enter event and update the logs_tree table +function parse_tracer_enter(logtable_cur, hr) + for j = 1, #hr do + local mv = hr[j] + + if mv == nil then + break + end + + if logtable_cur[mv] == nil then + logtable_cur[mv] = {ch={}} + end + + if j == #hr then + logtable_cur[mv].r=true + end + + logtable_cur = logtable_cur[mv].ch + end +end + +-- Parse a tracer exit event and update the given transaction entry +function parse_tracer_exit(mrk_cur, logtable_cur, hr, latency, contname, exe, id) + local res = false + local parent_has_logs = false; + + for j = 1, #hr do + local mv = hr[j] + if mv == nil or mrk_cur == nil then + break + end + + local has_logtable_entry = (logtable_cur ~= nil and logtable_cur[mv] ~= nil) + +--print("! " .. evt.get_num() .. " " .. j) +--print(parent_has_logs) +--print(logtable_cur[mv].r) + if j == #hr then + local llogs + + if has_logtable_entry and logtable_cur[mv].l ~= nil then + llogs = logtable_cur[mv].l + else + llogs = nil + end + +--print("################ " .. evt.get_num() .. " " .. st(logs_tree)) + if mrk_cur[mv] == nil then + mrk_cur[mv] = {t=latency, tt=latency, cont=contname, exe=exe, c=1, logs=llogs} + if j == 1 then + mrk_cur[mv].n = 0 + end + else + mrk_cur[mv]["tt"] = mrk_cur[mv]["tt"] + latency + mrk_cur[mv]["cont"] = contname + mrk_cur[mv]["exe"] = exe + mrk_cur[mv]["c"] = 1 + mrk_cur[mv]["logs"] = llogs + end + +--print("################ " .. evt.get_num()) +--print(st(logs_tree)) +--print("## " .. evt.get_num()) +--print(st(logtable_cur[mv].r)) + + if has_logtable_entry and parent_has_logs == false then + res = true + else + logtable_cur[mv] = nil + has_logtable_entry = false + logtable_cur = nil + end + elseif j == (#hr - 1) then + if mrk_cur[mv] == nil then + mrk_cur[mv] = {tt=0} + if j == 1 then + mrk_cur[mv].n = 0 + end + end + else + if mrk_cur[mv] == nil then + mrk_cur[mv] = {tt=0} + if j == 1 then + mrk_cur[mv].n = 0 + mrk_cur[mv]["id"] = id + end + end + end + + if mrk_cur[mv]["ch"] == nil then + mrk_cur[mv]["ch"] = {} + end + + if #hr == 1 then + mrk_cur[mv].n = mrk_cur[mv].n + 1 + end + + -- end of node parsing, update pointers to movo to the child + if has_logtable_entry then + parent_has_logs = (logtable_cur[mv].r ~= nil) + end + + mrk_cur = mrk_cur[mv].ch + + if logtable_cur ~= nil then + logtable_cur = logtable_cur[mv].ch + end + end + + return res +end + +-- Event parsing callback +function on_event() + local etype = evt.get_type() + + if etype ~= "tracer" then + local tid = evt.field(ftid) + + if logs_tree[tid] == nil then + return + else + collect_log(logs_tree[tid]) + end + + return + end + + local latency = evt.field(flatency) + local contname = evt.field(fcontname) + local id = evt.field(fid) + local exe = evt.field(fexe) + local hr = {} + local full_trs = nil + local dir = evt.field(fdir) + local tid = evt.field(ftid) + + for j = 0, MAX_DEPTH do + hr[j + 1] = evt.field(spans[j]) + end + + if dir == ">" then + if logs_tree[tid] == nil then + logs_tree[tid] = {} + end + + local idt = logs_tree[tid][id] + + if idt == nil then + logs_tree[tid][id] = {} + idt = logs_tree[tid][id] + end + + parse_tracer_enter(idt, hr) + return true + else + if latency == nil then + return true + end + + if full_tree[id] == nil then + full_tree[id] = {} + end + + -- find the logs for this transaction span + local logs + + if logs_tree[tid] == nil then + logs = nil + else + if logs_tree[tid][id] == nil then + logs = nil + else + logs = logs_tree[tid][id] + end + end + + if parse_tracer_exit(full_tree[id], logs, hr, latency, contname, exe, id) then +--print(st(logs_tree)) +--print("------------ " .. evt.get_num()) +--print(st(full_tree)) +--print("---------------------------------------------------") + + logs_tree[tid][id] = nil + + if next(logs_tree[tid]) == nil then + logs_tree[tid] = nil + end + + end + + return true + end +end + +function calculate_t_in_node(node) + local totchtime = 0 + local maxchtime = 0 + local nconc = 0 + local ch_to_keep + + if node.ch then + for k,d in pairs(node.ch) do + local nv = calculate_t_in_node(d) + + totchtime = totchtime + nv + + if nv > maxchtime then + maxchtime = nv + ch_to_keep = d + end + + nconc = nconc + 1 + end + end + + if node.tt >= totchtime then + node.t = node.tt - totchtime + else + node.t = node.tt - maxchtime + node.nconc = nconc + + for k,d in pairs(node.ch) do + if d ~= ch_to_keep then + node.ch[k] = nil + end + end + + end + + return node.tt +end + +function normalize(node, factor) + node.t = node.t / factor + node.tt = node.tt / factor + if node.ch then + for k,d in pairs(node.ch) do + normalize(d, factor) + end + end +end + +function is_transaction_complete(node) + if node.c ~= 1 then + return false + end + + if node.ch then + for k,d in pairs(node.ch) do + if is_transaction_complete(d) == false then + return false + end + end + end + + return true +end + +function update_avg_tree(dsttree, key, val) + if dsttree[key] == nil then + dsttree[key] = copytable(val) + return + else + dsttree[key].tt = dsttree[key].tt + val.tt + + if dsttree[key].n then + dsttree[key].n = dsttree[key].n + 1 + end + + if val.logs then + if dsttree[key].logs == nil then + dsttree[key].logs = {} + end + + concattable(dsttree[key].logs, val.logs) + end + end + + if val.ch then + if dsttree[key].ch == nil then + dsttree[key].ch = {} + end + + for k,d in pairs(val.ch) do + update_avg_tree(dsttree[key].ch, k, d) + end + end +end + +function update_max_tree(dsttree, key, val) + if dsttree[key] == nil then + dsttree[key] = val + return + else + if val.tt > dsttree[key].tt then + dsttree[key] = val + end + end +end + +function update_min_tree(dsttree, key, val) + if dsttree[key] == nil then + dsttree[key] = val + return + else + if val.tt < dsttree[key].tt then + dsttree[key] = val + end + end +end + +-- This processes the transaction list to extract and aggregate the transactions to emit +function collapse_tree() + -- scan the transaction list + for i,v in pairs(full_tree) do + local ttt = 0 + for key,val in pairs(v) do + ttt = ttt + val.tt + if is_transaction_complete(val) then + update_avg_tree(avg_tree, key, val) + update_max_tree(max_tree, key, val) + update_min_tree(min_tree, key, val) + end + end + end +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() +--print(st(full_tree)) + -- Process the list and create the required transactions + collapse_tree() + + -- calculate the unique time spent in each node + for i,v in pairs(avg_tree) do + calculate_t_in_node(v) + end + + -- normalize each root span tree + for i,v in pairs(avg_tree) do + normalize(v, v.n) + end + + print(PAGE_HEADER) + + -- emit the average transaction + local AvgData = {} + AvgData[""] = {ch=avg_tree, t=0, tt=0} + local str = json.encode(AvgData, { indent = true }) + print('"avg": ' .. str .. ",") + + -- normalize the best transaction + for i,v in pairs(min_tree) do + calculate_t_in_node(v) + end + + -- emit the best transaction + local tdata = {} + tdata[""] = {ch=min_tree, t=0, tt=0} + local str = json.encode(tdata, { indent = true }) + print('"min": ' .. str .. ",") + + -- normalize the worst transaction + for i,v in pairs(max_tree) do + calculate_t_in_node(v) + end + + -- emit the worst transaction + local tdata = {} + tdata[""] = {ch=max_tree, t=0, tt=0} + local str = json.encode(tdata, { indent = true }) + print('"max": ' .. str .. ",") + + print(PAGE_TRAILER) +end diff --git a/userspace/sysdig/chisels/http.lua b/userspace/sysdig/chisels/http.lua new file mode 100644 index 0000000000..2e43543f91 --- /dev/null +++ b/userspace/sysdig/chisels/http.lua @@ -0,0 +1,105 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Common function used by http parsing chisels + +partial_transactions = {} + +function http_init() + chisel.set_filter("evt.is_io = true and evt.buflen > 0 and (fd.sockfamily = ip or fd.sockfamily = unix)") + buffer_field = chisel.request_field("evt.buffer") + fd_field = chisel.request_field("fd.num") + pid_field = chisel.request_field("proc.pid") + rawtime_field = chisel.request_field("evt.rawtime") + datetime_field = chisel.request_field("evt.datetime") + dir_field = chisel.request_field("evt.io_dir") + + container_field = chisel.request_field("container.name") + + sysdig.set_snaplen(1024) +end + +function parse_request(req_buffer) + method, url = string.match(req_buffer, "^(%u+) (%g+)") + if method and url then + host = string.match(req_buffer, "Host: (%g+)%.%.") + if host then + url = host .. url + end + return { + method=method, + url=url + } + end + + return nil +end + +function parse_response(resp_buffer) + resp_code = string.match(resp_buffer, "HTTP/[%g]+ (%d+)") + if resp_code then + content_length = string.match(resp_buffer, "Content%-Length: (%d+)%.%.") + if not content_length then + content_length = 0 + end + return { + code = tonumber(resp_code), + length = tonumber(content_length) + } + else + return nil + end +end + +function run_http_parser(evt, on_transaction) + buf = evt.field(buffer_field) + fd = evt.field(fd_field) + pid = evt.field(pid_field) + evt_dir = evt.field(dir_field) + key = string.format("%d\001\001%d", pid, fd) + + timestamp = evt.field(rawtime_field) + + transaction = partial_transactions[key] + if not transaction then + request = parse_request(buf) + if request then + transaction_dir = "" + if evt_dir == "read" then + transaction_dir = "<" + elseif evt_dir == "write" then + transaction_dir = ">" + end + request["ts"] = timestamp + partial_transactions[key] = { + request= request, + dir=transaction_dir, + container=evt.field(container_field) + } + end + else + response = parse_response(buf) + if response then + transaction["response"] = response + transaction["response"]["ts"] = timestamp + on_transaction(transaction) + partial_transactions[key] = nil + end + end +end diff --git a/userspace/sysdig/chisels/httplog.lua b/userspace/sysdig/chisels/httplog.lua new file mode 100644 index 0000000000..0d6028a438 --- /dev/null +++ b/userspace/sysdig/chisels/httplog.lua @@ -0,0 +1,57 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Show a log of all HTTP requests"; +short_description = "HTTP requests log"; +category = "Application"; +args = {} + +require "http" + +-- Initialization callback +function on_init() + http_init() + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + return true +end + +function on_transaction(transaction) + if print_container then + container = " " .. transaction["container"] .. " " + else + container = " " + end + print(string.format("%s%s%s method=%s url=%s response_code=%d latency=%dms size=%dB", + evt.field(datetime_field), + container, + transaction["dir"], + transaction["request"]["method"], + transaction["request"]["url"], + transaction["response"]["code"], + (transaction["response"]["ts"] - transaction["request"]["ts"])/1000000, + transaction["response"]["length"] + )) +end + +function on_event() + run_http_parser(evt, on_transaction) +end diff --git a/userspace/sysdig/chisels/httptop.lua b/userspace/sysdig/chisels/httptop.lua new file mode 100644 index 0000000000..ff12d56fe4 --- /dev/null +++ b/userspace/sysdig/chisels/httptop.lua @@ -0,0 +1,180 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Show top HTTP requests by: ncalls, time or bytes"; +short_description = "Top HTTP requests"; +category = "Application"; + +-- Chisel argument list +args = { + { + name = "by", + description = "Show top HTTP transactions by: ncalls, time or bytes, default is ncalls", + argtype = "string", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +require "http" + +vizinfo = +{ + key_fld = {"method", "url"}, + key_desc = {"method", "url"}, + value_fld = "ncalls", + value_desc = "ncalls", + value_units = "none", + top_number = 30, + output_format = "normal" +} + +by_field = "ncalls" + +-- Argument notification callback +function on_set_arg(name, val) + if name == "by" then + if val == "time" then + vizinfo["value_fld"] = "time" + vizinfo["value_desc"] = "time" + vizinfo["value_units"] = "time" + elseif val == "ncalls" then + vizinfo["value_fld"] = "ncalls" + vizinfo["value_desc"] = "ncalls" + vizinfo["value_units"] = "none" + elseif val == "bytes" then + vizinfo["value_fld"] = "bytes" + vizinfo["value_desc"] = "bytes" + vizinfo["value_units"] = "bytes" + else + print("Invalid argument! Valid options: ncalls, bytes, time") + return false + end + by_field = val + return true + end +end + + +-- Initialization callback +function on_init() + http_init() + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + table.insert(vizinfo["key_fld"], 1, "container") + table.insert(vizinfo["key_desc"], 1, "container") + end + + return true +end + +islive = false +grtable = {} +partial_transactions = {} + +function build_grtable_key(transaction) + request = transaction["request"] + ret = "" + if print_container then + ret = transaction["container"] .. "\001\001" + end + ret = ret .. string.format("%s\001\001%s", request["method"], request["url"]) + return ret +end + +function on_transaction(transaction) + grtable_key = build_grtable_key(transaction) + if not grtable[grtable_key] then + grtable[grtable_key] = {} + end + table.insert(grtable[grtable_key], transaction) +end + +function on_event() + run_http_parser(evt, on_transaction) +end + +-- Final chisel initialization +function on_capture_start() + islive = sysdig.is_live() + vizinfo.output_format = sysdig.get_output_format() + + if islive then + chisel.set_interval_s(1) + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.hidecursor() + end + end + + return true +end + +function aggregate_grtable() + for key, transactions in pairs(grtable) do + if by_field == "ncalls" then + grtable[key] = #transactions + elseif by_field == "bytes" then + total_bytes = 0 + for _, tr in ipairs(transactions) do + total_bytes = total_bytes + tr["response"]["length"] + end + grtable[key] = total_bytes + elseif by_field == "time" then + total_time = 0 + for _, tr in ipairs(transactions) do + total_time = total_time + tr["response"]["ts"] - tr["request"]["ts"] + end + grtable[key] = total_time / #transactions + end + end +end + +function on_interval(ts_s, ts_ns, delta) + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.moveto(0, 0) + end + + aggregate_grtable() + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) + + -- Clear the table + grtable = {} + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end(ts_s, ts_ns, delta) + if islive and vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.moveto(0 ,0) + terminal.showcursor() + return true + end + + aggregate_grtable() + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) + + return true +end diff --git a/userspace/sysdig/chisels/iobytes.lua b/userspace/sysdig/chisels/iobytes.lua index 03fa71094c..72277b396a 100644 --- a/userspace/sysdig/chisels/iobytes.lua +++ b/userspace/sysdig/chisels/iobytes.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "counts the total bytes read from and written to any type of FD (disk, socket, pipe...) and prints the result every second."; +description = "Counts the total bytes read from and written to any type of FD (disk, socket, pipe...) and prints the result every second."; short_description = "Sum of I/O bytes on any type of FD"; category = "I/O"; diff --git a/userspace/sysdig/chisels/iobytes_file.lua b/userspace/sysdig/chisels/iobytes_file.lua index 43f4a3f988..326025dd5c 100644 --- a/userspace/sysdig/chisels/iobytes_file.lua +++ b/userspace/sysdig/chisels/iobytes_file.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "counts the total bytes read from and written to files."; +description = "Counts the total bytes read from and written to files."; short_description = "Sum of file I/O bytes"; category = "I/O"; diff --git a/userspace/sysdig/chisels/iobytes_net.lua b/userspace/sysdig/chisels/iobytes_net.lua index 1689f11d85..6817afb67d 100644 --- a/userspace/sysdig/chisels/iobytes_net.lua +++ b/userspace/sysdig/chisels/iobytes_net.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "counts the total bytes read from and written to the network, and prints the result every second"; +description = "Counts the total bytes read from and written to the network, and prints the result every second"; short_description = "Show total network I/O bytes"; category = "Net"; @@ -62,6 +64,7 @@ end function on_interval(delta) etime = evt.field(ftime) + print(etime .. " in:" .. totin .. " out:" .. totout .. " tot:" .. tot) tot = 0 totin = 0 diff --git a/userspace/sysdig/chisels/list_login_shells.lua b/userspace/sysdig/chisels/list_login_shells.lua new file mode 100644 index 0000000000..40c8f171b0 --- /dev/null +++ b/userspace/sysdig/chisels/list_login_shells.lua @@ -0,0 +1,122 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "List the IDs of the login sessions. Optionally, the list can be filtered to include only the sessions that contain a specific command. The session IDs listed by this chisel can be used as filters for the spy_users chisel. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown."; +short_description = "List the login shell IDs"; +category = "Security"; + +-- Chisel argument list +args = +{ + { + name = "command", + description = "If this parameter is specified, only the login shells that contain commands including the given string in their name will be listed. * will match any command name.", + argtype = "string", + optional = true + }, + { + name = "arguments", + description = "If this parameter is specified, only the login shells that contain commands including the given string in their arguments will be listed", + argtype = "string", + optional = true + }, +} + +require "common" + +sids = +{ + fsid = nil, + containername = nil, + containerid = nil +} + +-- Argument notification callback +function on_set_arg(name, val) + if name == "command" then + if val ~= "*" then + matching_comm_str = val + end + elseif name == "arguments" then + matching_arg_str = val + end + return true +end + +-- Initialization callback +function on_init() + fsid = chisel.request_field("proc.loginshellid") + fexe = chisel.request_field("evt.arg.exe") + fargs = chisel.request_field("evt.arg.args") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + chisel.set_filter("evt.type=execve") + + return true +end + +-- Event parsing callback +function on_event() + + sid = evt.field(fsid) + exe = evt.field(fexe) + args = evt.field(fargs) + containername = evt.field(fcontainername) + containerid = evt.field(fcontainerid) + + if sid and exe then + if matching_comm_str and string.find(exe, matching_comm_str) == nil then + return true + end + + if matching_arg_str and args and string.find(args, matching_arg_str) == nil then + return true + end + + sids.fsid = sid + sids.containername = containername + sids.containerid = containerid + end + + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + if matching_comm_str then + print("Shells containing " .. matching_comm_str .. ":") + else + print("All shells:") + end + + for k, v in pairs(sids) do + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(sids.fsid .. " " .. sids.containername .. " " .. sids.containerid) + else + print(sids.fsid) + end + end +end diff --git a/userspace/sysdig/chisels/lscontainers.lua b/userspace/sysdig/chisels/lscontainers.lua new file mode 100644 index 0000000000..e7ec83f493 --- /dev/null +++ b/userspace/sysdig/chisels/lscontainers.lua @@ -0,0 +1,108 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "List the running containers and the metadata"; +short_description = "List the running containers"; +category = "System State"; + +-- Argument list +args = +{ + { + name = "desc", + description = "Prints the result set as a data structure", + argtype = "string", + optional = true + } +} + +-- Imports and globals +require "common" +local dctable = {} +local capturing = false +local filter = nil +local desc = false + +-- Argument initialization Callback +function on_set_arg(name, val) + if name == "desc" and val == "desc" then + desc = true + return true + end + + return false +end + +-- Initialization callback +function on_init() + return true +end + +-- Event parsing callback +function on_event() + return true +end + +-- Final chisel initialization +function on_capture_start() + capturing = true + return true +end + +-- Event parsing callback +function on_event() + sysdig.end_capture() + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end(ts_s, ts_ns, delta) + if not capturing then + return + end + + local ttable = sysdig.get_container_table(filter) + + -- Print out the result set as a data structure + if ( desc ) then + print(st(ttable)) + else + -- Print out the information in a tabular format + + local sorted_ttable = pairs_top_by_val(ttable, 0, function(t,a,b) return a < b end) + + print( extend_string("container.type", 15) .. + extend_string("container.image", 16) .. + extend_string("container.name", 20 ) .. + extend_string("container.id", 13) ) + print( extend_string("---------------", 15) .. + extend_string("----------------", 16) .. + extend_string("--------------------", 20 ) .. + extend_string("-------------", 13) ) + + for key, val in sorted_ttable do + print(extend_string(tostring(val.type), 15) .. + extend_string(tostring(val.image), 16) .. + extend_string(tostring(val.name), 20) .. + extend_string(tostring(val.id), 13) + ) + end + end +end diff --git a/userspace/sysdig/chisels/lsof.lua b/userspace/sysdig/chisels/lsof.lua new file mode 100644 index 0000000000..f3ae155299 --- /dev/null +++ b/userspace/sysdig/chisels/lsof.lua @@ -0,0 +1,117 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "This chisel prints the open file descriptors for every process in the system, with an output that is similar to the one of lsof. Output is at a point in time; adjust this in the filter. It defaults to time of evt.num=0"; +short_description = "List (and optionally filter) the open file descriptors."; +category = "System State"; + +-- Argument list +args = +{ + { + name = "filter", + description = "A sysdig-like filter expression that allows restricting the FD list. E.g. 'proc.name=foo and fd.name contains /etc'.", + argtype = "filter", + optional = true + } +} + +-- Argument initialization Callback +function on_set_arg(name, val) + if name == "filter" then + filter = val + return true + end + + return false +end + +-- Imports and globals +require "common" +local dctable = {} +local capturing = false +local filter = nil +local match = false + +-- Argument notification callback +function on_set_arg(name, val) + if name == "filter" then + filter = val + return true + end + + return false +end + +-- Initialization callback +function on_init() + return true +end + +-- Final chisel initialization +function on_capture_start() + capturing = true + return true +end + +-- Event parsing callback +function on_event() + sysdig.end_capture() + match = true + return false +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + if not capturing then + return + end + + if match == false then + print("empty capture or no event matching the filter") + return + end + + local ttable = sysdig.get_thread_table(filter) + + local sorted_ttable = pairs_top_by_val(ttable, 0, function(t,a,b) return a < b end) + + print(extend_string("COMMAND", 20) .. + extend_string("PID", 8) .. + extend_string("TID", 8) .. + extend_string("USER", 8) .. + extend_string("FD", 8) .. + extend_string("TYPE", 12) .. + "NAME") + + for tid, proc in sorted_ttable do + local fdtable = proc.fdtable + + for fd, fdinfo in pairs(fdtable) do + print(extend_string(proc.comm, 20) .. + extend_string(tostring(proc.pid), 8) .. + extend_string(tostring(tid), 8) .. + extend_string(proc.username, 8) .. + extend_string(tostring(fd), 8) .. + extend_string(tostring(fdinfo.type), 12) .. + tostring(fdinfo.name)) + end + end +end diff --git a/userspace/sysdig/chisels/memcachelog.lua b/userspace/sysdig/chisels/memcachelog.lua new file mode 100644 index 0000000000..a8d6f8e186 --- /dev/null +++ b/userspace/sysdig/chisels/memcachelog.lua @@ -0,0 +1,111 @@ +--[[ + +Copyright (C) 2015 Donatas Abraitis. + +This file is part of sysdig. + +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. + +--]] + +--[[ +memcachelog.lua - show most used memcached keys. + +USAGE: sysdig -c memcachelog + eg, + + sysdig -c memcachelog # show memcached get/set utilization + sysdig -c memcachelog get # show memcached only get utilization + sysdig -c memcachelog set # show memcached only set utilization + sysdig -c memcachelog 'set 1000' # show memcached set utilization which object's size is higher than 1000 + +By default it will print both methods. +--]] + +-- Chisel description +description = "Show a log of memcached commands (get/set)" +short_description = "memcached requests log" +category = "Application" + +-- Chisel argument list +args = +{ + { + name = "method", + description = "get/set", + optional = true + }, + { + name = "size", + description = "object size", + optional = true + } +} + +-- Helpers -- +function split(s, delimiter) + result = {}; + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match); + end + return result; +end + +-- Argument notification callback +function on_set_arg(name, val) + if name == "method" then + opt_method = val + return true + elseif name == "size" then + opt_size = tonumber(val) + return true + end + return false +end + +-- Initialization callback +function on_init() + util = {} + start_time = os.time() + sysdig.set_filter("(fd.sport=11211 or proc.name=memcached) and evt.is_io=true") + sysdig.set_snaplen(4096) + data = chisel.request_field("evt.arg[1]") + datetime = chisel.request_field("evt.datetime") + return true +end + +-- Event callback +function on_event() + local data = evt.field(data) + local line = split(data, " ") + if string.match(line[1], '^[gs]et') ~= nil then + local method = line[1] + local key = line[2] + local size = tonumber(line[5]) or 0 + if key ~= nil then + if opt_method ~= nil and opt_method ~= method then + return true + end + if opt_method == 'set' and size < opt_size then + return true + end + print(string.format("%s method=%s size=%dB key=%s", + evt.field(datetime), + method, + size, + key + )) + end + end + return true +end diff --git a/userspace/sysdig/chisels/netlower.lua b/userspace/sysdig/chisels/netlower.lua new file mode 100644 index 0000000000..76267b5bd7 --- /dev/null +++ b/userspace/sysdig/chisels/netlower.lua @@ -0,0 +1,160 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +--[[ +netlower.lua - trace network I/O slower than a given threshold. + +USAGE: sysdig -c netlower min_ms + eg, + + sysdig -c netlower 1000 # show network I/O slower than 1000 ms. + sysdig -c netlower "1 disable_colors" # show network I/O slower than 1 ms. w/ no colors + sysdig -c netlower 1000 # show network I/O slower than 1000 ms and container output + +--]] + +-- Chisel description +description = "Trace network I/O slower than a threshold, or all network I/O. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. (Blue represents a process running within a container, and Green represents a host process)" +short_description = "Trace slow network I/0" +category = "Performance" + +-- Chisel argument list +args = +{ + { + name = "min_ms", + description = "Minimum millisecond threshold for showing network I/O", + argtype = "int", + optional = false + }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + +-- Argument notification callback +function on_set_arg(name, val) + + if name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + elseif name == "min_ms" then + min_ms = parse_numeric_input(val, name) + end + + return true +end + +-- Initialization callback +function on_init() + -- set the following fields on_event() + etype = chisel.request_field("evt.type") + dir = chisel.request_field("evt.dir") + datetime = chisel.request_field("evt.datetime") + fname = chisel.request_field("fd.name") + pname = chisel.request_field("proc.name") + latency = chisel.request_field("evt.latency") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + -- filter for network I/O + chisel.set_filter("evt.is_io=true and (fd.type=ipv4 or fd.type=ipv6)") + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(string.format("%-23.23s %-20.20s %-20.20s %-12.12s %-8s %-12s %s", + "evt.datetime", + "container.id", + "container.name", + "proc.name", + "evt.type", + "LATENCY(ms)", + "fd.name")) + print(string.format("%-23.23s %-20.20s %-20.20s %-12.12s %-8s %-12s %s", + "-----------------------", + "------------------------------", + "------------------------------", + "------------", + "--------", + "------------", + "-----------------------------------------")) + else + print(string.format("%-23.23s %-12.12s %-8s %-12s %s", + "evt.datetime", + "proc.name", + "evt.type", + "LATENCY(ms)", + "fd.name")) + print(string.format("%-23.23s %-12.12s %-8s %-12s %s", + "-----------------------", + "------------", + "--------", + "------------", + "-----------------------------------------")) + end + + return true +end + +-- Event callback +function on_event() + + local color = terminal.green + + lat = evt.field(latency) / 1000000 + fn = evt.field(fname) + + if evt.field(dir) == "<" and lat > min_ms then + + -- If this is a container modify the output color + if evt.field(fcontainername) ~= "host" then + color = terminal.blue + end + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(color .. string.format("%-23.23s %-20.20s %-20.20s %-12.12s %-8s %12d %s", + evt.field(datetime), + evt.field(fcontainerid), + evt.field(fcontainername), + evt.field(pname), + evt.field(etype), + lat, + fn )) + else + print(color .. string.format("%-23.23s %-12.12s %-8s %12d %s", + evt.field(datetime), + evt.field(pname), + evt.field(etype), + lat, + fn )) + end + end + + return true +end diff --git a/userspace/sysdig/chisels/netstat.lua b/userspace/sysdig/chisels/netstat.lua new file mode 100644 index 0000000000..b03822aae9 --- /dev/null +++ b/userspace/sysdig/chisels/netstat.lua @@ -0,0 +1,122 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Print the system network connections, with an output that is similar to the one of netstat. Output is at a point in time; adjust this in the filter. It defaults to time of evt.num=0"; +short_description = "List (and optionally filter) network connections."; +category = "System State"; + +-- Argument list +args = +{ + { + name = "filter", + description = "A sysdig-like filter expression that allows restricting the FD list. E.g. 'proc.name=foo and fd.port=80'.", + argtype = "filter", + optional = true + } +} + +-- Argument initialization Callback +function on_set_arg(name, val) + if name == "filter" then + filter = val + return true + end + + return false +end + +-- Imports and globals +require "common" +local dctable = {} +local capturing = false +local filter = "(fd.type=ipv4 or fd.type=ipv6)" +local match = false + +-- Argument notification callback +function on_set_arg(name, val) + if name == "filter" then + filter = filter .. "and (" .. val .. ")" + return true + end + + return false +end + +-- Initialization callback +function on_init() + return true +end + +-- Final chisel initialization +function on_capture_start() + capturing = true + return true +end + +-- Event parsing callback +function on_event() + sysdig.end_capture() + match = true + return false +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + if not capturing then + return + end + + if match == false then + print("empty capture or no event matching the filter") + return + end + + local ttable = sysdig.get_thread_table(filter) + + print(extend_string("Proto", 6) .. + extend_string("Server Address", 25) .. + extend_string("Client Address", 25) .. + extend_string("State", 15) .. + "TID/PID/Program Name") + + for tid, proc in pairs(ttable) do + local fdtable = proc.fdtable + + for fd, fdinfo in pairs(fdtable) do + local cip = fdinfo.cip + local cport = fdinfo.cport + local state = "ESTABLISHED" + + if cip == nil then + cip = "0.0.0.0" + cport = "*" + state = "LISTEN" + end + + print(extend_string(fdinfo.l4proto, 6) .. + extend_string(fdinfo.sip .. ":" .. fdinfo.sport, 25) .. + extend_string(cip .. ":" .. cport, 25) .. + extend_string(state, 15) .. + tid .. "/" .. proc.pid .. "/" .. proc.comm + ) + end + end +end diff --git a/userspace/sysdig/chisels/proc_exec_time.lua b/userspace/sysdig/chisels/proc_exec_time.lua new file mode 100644 index 0000000000..cd98a9bda1 --- /dev/null +++ b/userspace/sysdig/chisels/proc_exec_time.lua @@ -0,0 +1,144 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +--[[ +USAGE: sysdig -c proc_exec_time + eg, + + sysdig -c proc_exec_time # show processes that have finished + sysdig -c proc_exec_time disable_colors" # show processes that have finished w/ no colors + sysdig -pc -c proc_exec_time # show processes that have finished and container output +--]] + +-- Chisel description +description = "List the processes that have finished running, along with their execution time, and color every line based on the total process run time (Green|Blue below thresholds, Yellow at 3 sec, and Red at 10 sec). This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. (Blue represents a process running within a container, and Green represents a host process)"; +short_description = "Show process execution time"; +category = "Performance"; + +-- Chisel argument list +args = +{ + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + +local THRESHOLD_YELLOW_NS = 3000000000 +local THRESHOLD_RED_NS = 10000000000 + +-- Argument notification callback +function on_set_arg(name, val) + if val == "disable_colors" then + terminal.enable_color(false) + end + return true +end + +-- Initialization callback +function on_init() + -- Request the fields that we need + fetype = chisel.request_field("evt.type") + fexe = chisel.request_field("proc.name") + fargs = chisel.request_field("proc.args") + fdtime = chisel.request_field("evt.time.s") + fduration = chisel.request_field("proc.duration") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + -- set the filter + chisel.set_filter("evt.type=procexit") + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(string.format("%-13.13s %-20.20s %-20.20s %-12.12s %s", + "proc.duration", + "container.id", + "container.name", + "proc.name", + "proc.args")) + print(string.format("%-13.13s %-20.20s %-20.20s %-12.12s %s", + "-------------", + "--------------------", + "--------------------", + "------------", + "--------------------")) + else + print(string.format("%-13.13s %-12.12s %s", + "proc.duration", + "proc.name", + "proc.args")) + print(string.format("%-13.13s %-12.12s %s", + "-------------", + "------------", + "--------------------")) + end + + return true +end + +-- Event parsing callback +function on_event() + local dtime = evt.field(fdtime) + local duration = evt.field(fduration) + + if duration ~= nil then + local color = terminal.green + + if duration > THRESHOLD_RED_NS then + color = terminal.red + elseif duration > THRESHOLD_YELLOW_NS then + color = terminal.yellow + elseif evt.field(fcontainername) ~= "host" then + -- if the data is associated with a container change the color to blue unless a threshold is met + color = terminal.blue + end + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(color .. string.format("%-13.13s %-20.20s %-20.20s %-12.12s %s", + format_time_interval(duration), + evt.field(fcontainerid), + evt.field(fcontainername), + evt.field(fexe), + evt.field(fargs))) + else + print(color .. string.format("%-13.13s %-12.12s %s", + format_time_interval(duration), + evt.field(fexe), + evt.field(fargs))) + end + end + + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + print(terminal.reset) +end diff --git a/userspace/sysdig/chisels/ps.lua b/userspace/sysdig/chisels/ps.lua new file mode 100644 index 0000000000..6d98fb78b5 --- /dev/null +++ b/userspace/sysdig/chisels/ps.lua @@ -0,0 +1,113 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "List the running processes, with an output that is similar to the one of ps. Output is at a point in time; adjust this in the filter. It defaults to time of evt.num=0"; +short_description = "List (and optionally filter) the machine processes."; +category = "System State"; + +-- Argument list +args = +{ + { + name = "filter", + description = "A sysdig-like filter expression that allows restricting the FD list. For example 'fd.name contains /etc' shows all the processes that have files open under /etc.", + argtype = "filter", + optional = true + } +} + +-- Argument initialization Callback +function on_set_arg(name, val) + if name == "filter" then + filter = val + return true + end + + return false +end + +-- Imports and globals +require "common" +local dctable = {} +local capturing = false +local filter = nil +local match = false + +-- Argument notification callback +function on_set_arg(name, val) + if name == "filter" then + filter = val + return true + end + + return false +end + +-- Initialization callback +function on_init() + return true +end + +function on_capture_start() + capturing = true + return true +end + +-- Event parsing callback +function on_event() + sysdig.end_capture() + match = true + return false +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end(ts_s, ts_ns, delta) + if not capturing then + return + end + + if match == false then + print("empty capture or no event matching the filter") + return + end + + local ttable = sysdig.get_thread_table(filter) + + local sorted_ttable = pairs_top_by_val(ttable, 0, function(t,a,b) return a < b end) + + print(extend_string("TID", 8) .. + extend_string("PID", 8) .. + extend_string("USER", 12) .. + extend_string("VIRT", 11) .. + extend_string("RES", 11) .. + extend_string("FDLIMIT", 10) .. + extend_string("CMD", 20)) + + for tid, proc in sorted_ttable do + print(extend_string(tostring(tid), 8) .. + extend_string(tostring(proc.pid), 8) .. + extend_string(proc.username, 12) .. + extend_string(format_bytes(proc.vmsize_kb * 1024), 11) .. + extend_string(format_bytes(proc.vmrss_kb * 1024), 11) .. + extend_string(tostring(proc.fdlimit), 10) .. + proc.comm + ) + end +end diff --git a/userspace/sysdig/chisels/scallslower.lua b/userspace/sysdig/chisels/scallslower.lua new file mode 100644 index 0000000000..ccce25d89a --- /dev/null +++ b/userspace/sysdig/chisels/scallslower.lua @@ -0,0 +1,154 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +--[[ +scallslower.lua - trace the syscalls slower than a given threshold. + +USAGE: sysdig -c scallslower min_ms + eg, + + sysdig -c scallslower 1000 # show syscalls slower than 1000 ms. + sysdig -c scallslower "1 disable_colors" # show syscalls slower than 1 ms. w/ no colors + sysdig -pc -c scallslower 1000 # show syscalls slower than 1000 ms and container output + +--]] + +-- Chisel description +description = "Trace syscalls slower than a threshold milliseconds. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. (Blue represents a process running within a container, and Green represents a host process)"; +short_description = "Trace slow syscalls"; +category = "Performance"; + +-- Chisel argument list +args = +{ + { + name = "min_ms", + description = "Minimum milliseconds before which a syscall should complete", + argtype = "int", + optional = false + }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + + +-- Argument notification callback +function on_set_arg(name, val) + + if name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + elseif name == "min_ms" then + min_ms = parse_numeric_input(val, name) + end + + return true +end + +-- Initialization callback +function on_init() + -- set the following fields on_event() + etype = chisel.request_field("evt.type") + dir = chisel.request_field("evt.dir") + datetime = chisel.request_field("evt.datetime") + pname = chisel.request_field("proc.name") + latency = chisel.request_field("evt.latency") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(string.format("%-23.23s %-20.20s %-20.20s %-23.23s %-20s %s", + "evt.datetime", + "container.id", + "container.name", + "proc.name", + "LATENCY(ms)", + "evt.type")) + print(string.format("%-23.23s %-20.20s %-20.20s %-23.23s %-20s %s", + "-----------------------", + "--------------------", + "--------------------", + "-----------------------", + "--------------------", + "--------------------")) + else + print(string.format("%-23.23s %-23.23s %-20s %s", + "evt.datetime", + "proc.name", + "LATENCY(ms)", + "evt.type")) + print(string.format("%-23.23s %-23.23s %-20s %s", + "-----------------------", + "-----------------------", + "--------------------", + "--------------------")) + end + + return true +end + +-- Event callback +function on_event() + + local color = terminal.green + + lat = evt.field(latency) + + if lat == nil then + return + end + + lat = lat / 1000000 + + if lat > min_ms then + + if evt.field(fcontainername) ~= "host" then + color = terminal.blue + end + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + print(color .. string.format("%-23.23s %-20.20s %-20.20s %-23.23s %-20s %s", + evt.field(datetime), + evt.field(fcontainerid), + evt.field(fcontainername), + evt.field(pname), + lat, + evt.field(etype))) + else + print(color .. string.format("%-23.23s %-23.23s %-20s %s", + evt.field(datetime), + evt.field(pname), + lat, + evt.field(etype))) + end + + end +end diff --git a/userspace/sysdig/chisels/shellshock_detect.lua b/userspace/sysdig/chisels/shellshock_detect.lua new file mode 100644 index 0000000000..8e4a301f82 --- /dev/null +++ b/userspace/sysdig/chisels/shellshock_detect.lua @@ -0,0 +1,76 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Reports every attempt to execute bash in a way that exploits the shellshock vulnerability (http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-6271). For every attempt, the chisel reports the time, the name of the process trying to run bash, and its PID."; +short_description = "print shellshock attacks"; +category = "Security"; + +args = {} + +require "common" + +-- Initialization callback +function on_init() + -- Request the fields that we need + fpname = chisel.request_field("proc.pname") + fppid = chisel.request_field("proc.ppid") + fpid = chisel.request_field("proc.pid") + fenv = chisel.request_field("evt.arg.environment") + fetime = chisel.request_field("evt.time") + + -- set the filter + chisel.set_filter("proc.name=bash or proc.name=sh and evt.type=execve") + + print(extend_string("TIME", 22) .. + extend_string("PROCNAME", 22) .. + extend_string("PID", 8) .. + "FUNCTION") + + return true +end + +-- Event parsing callback +function on_event() + local env = evt.field(fenv) + local pname = evt.field(fpname) + local etime = evt.field(fetime) + local ppid = evt.field(fppid) + + if env ~= nil then + if string.find(env, "%(%) ?{.+") then + local pid = evt.field(fpid) + local env_list = sysdig.get_thread_table(filter)[pid].env + + for i, v in ipairs(env_list) do + if string.find(v, "%(%) ?{.+") then + local command = string.match(v, "%(%).+") + + print(extend_string(etime, 22) .. + extend_string(pname, 22) .. + extend_string(tostring(ppid), 8) .. + command) + break + end + end + end + end + + return true +end diff --git a/userspace/sysdig/chisels/spectrogram.lua b/userspace/sysdig/chisels/spectrogram.lua new file mode 100644 index 0000000000..58c967fb35 --- /dev/null +++ b/userspace/sysdig/chisels/spectrogram.lua @@ -0,0 +1,194 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "This console visualization shows the frequency of system call latencies. The Y axis unit is time. By default, a new line is created twice a second, but that can be changed by specifying a different refresh time argument. The X axis shows a range of latencies. Each latency value has a color that can be black (no calls), green (tens of calls/s), yellow (hundreds of calls/s) or red (Thousands of calls/s). In other words, red areas mean that there are many system calls taking the specified time to return. Use this chisel in conjunction with filters to visualize latencies for certain processes, types of I/O activity, file systems, etc." +short_description = "Visualize OS latency in real time." +category = "CPU Usage" + +-- Chisel argument list +args = { + { + name = "refresh_time", + description = "Chart refresh time in milliseconds", + argtype = "int", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + +refresh_time = 500000000 +refresh_per_sec = 1000000000 / refresh_time +frequencies = {} +colpalette = {22, 28, 64, 34, 2, 76, 46, 118, 154, 191, 227, 226, 11, 220, 209, 208, 202, 197, 9, 1} +charpalette = {" ", "░", "▒", "░"} + +-- Argument initialization Callback +function on_set_arg(name, val) + if name == "refresh_time" then + refresh_time = parse_numeric_input(val, name) * 1000000 + refresh_per_sec = 1000000000 / refresh_time + return true + end + + return false +end + +-- Initialization callback +function on_init() + is_tty = sysdig.is_tty() + + if not is_tty then + print("This chisel only works on ANSI terminals. Aborting.") + return false + end + + tinfo = sysdig.get_terminal_info() + w = tinfo.width + h = tinfo.height + + chisel.set_filter("evt.dir=<") + + flatency = chisel.request_field("evt.latency") + + terminal.hidecursor() + + return true +end + +-- Final chisel initialization +function on_capture_start() + chisel.set_interval_ns(refresh_time) + return true +end + +-- Event parsing callback +function on_event() + local latency = evt.field(flatency) + + if latency == 0 then + return true + end + + local llatency = math.log10(latency) + + if(llatency > 11) then + llatency = 11 + end + + local norm_llatency = math.floor(llatency * w / 11) + 1 + + if frequencies[norm_llatency] == nil then + frequencies[norm_llatency] = 1 + else + frequencies[norm_llatency] = frequencies[norm_llatency] + 1 + end + + return true +end + +-- Calculate colors and character to be used +function mkcol(n) + local col = math.log10(n * refresh_per_sec + 1) / math.log10(1.6) + + if col < 1 then + col = 1 + elseif col > #colpalette then + col = #colpalette + end + + local low_col = math.floor(col) + local high_col = math.ceil(col) + local delta = col - low_col + local ch = charpalette[math.floor(1 + delta * #charpalette)] + + -- If delta is > 75% we use 25% fill and flip fg and bg to fake a 75% filled block + if delta > .75 then + return colpalette[high_col], colpalette[low_col], ch + else + return colpalette[low_col], colpalette[high_col], ch + end +end + +-- Periodic timeout callback +function on_interval(ts_s, ts_ns, delta) + terminal.moveup(1) + + for x = 1, w do + local fr = frequencies[x] + local fg, bg, ch + + if fr == nil or fr == 0 then + terminal.setfgcol(0) + terminal.setbgcol(0) + ch = " " + else + fg, bg, ch = mkcol(fr) + terminal.setfgcol(fg) + terminal.setbgcol(bg) + end + + io.write(ch) + end + + io.write(terminal.reset .. "\n") + + local x = 0 + while true do + if x >= w then + break + end + + local curtime = math.floor(x * 11 / w) + local prevtime = math.floor((x - 1) * 11 / w) + + if curtime ~= prevtime then + io.write("|") + local tstr = format_time_interval(math.pow(10, curtime)) + io.write(tstr) + x = x + #tstr + 1 + else + io.write(" ") + x = x + 1 + end + end + + io.write("\n") + + frequencies = {} + + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end(ts_s, ts_ns, delta) + if is_tty then + -- Include the last sample + on_interval(ts_s, ts_ns, 0) + + -- reset the terminal + print(terminal.reset) + terminal.showcursor() + end + + return true +end diff --git a/userspace/sysdig/chisels/spy_file.lua b/userspace/sysdig/chisels/spy_file.lua new file mode 100644 index 0000000000..9b69ee1ea0 --- /dev/null +++ b/userspace/sysdig/chisels/spy_file.lua @@ -0,0 +1,131 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "This chisel intercepts all reads and writes to all files. Instead of all files, you can limit interception to one file." +short_description = "Echo any read/write made by any process to all files. Optionally, you can provide the name of one file to only intercept reads/writes to that file."; +category = "I/O"; + +-- Argument list +args = +{ + { + name = "read_or_write", + description = "Specify 'R' to capture only read events; 'W' to capture only write events; 'RW' to capture read and write events. By default both read and write events are captured.", + argtype = "string", + optional = true + }, + { + name = "spy_on_file_name", + description = "The name of the file which the chisel should spy on for all read and write activity.", + argtype = "string", + optional = true + } +} + +-- Imports and globals +require "common" +local spy_file_name = nil +local read_or_write = nil +local verbose = false + +-- Argument notification callback +function on_set_arg(name, val) + if name == "read_or_write" then + read_or_write = val + return true + elseif name == "spy_on_file_name" then + spy_file_name = val + return true + end + + return false +end + +-- Initialization callback +function on_init() + local filter + + -- Request the fields that we need + fbuf = chisel.request_field("evt.buffer") + fdata = chisel.request_field("evt.arg.data") + ffdname = chisel.request_field("fd.name") + fisw = chisel.request_field("evt.is_io_write") + fpid = chisel.request_field("proc.pid") + fpname = chisel.request_field("proc.name") + fres = chisel.request_field("evt.rawarg.res") + ftid = chisel.request_field("thread.tid") + fts = chisel.request_field("evt.time") + + -- increase the snaplen so we capture more of the conversation + sysdig.set_snaplen(2000) + + -- set the output format to ascii + sysdig.set_output_format("ascii") + + -- set the filter + if spy_file_name ~= nil and spy_file_name ~= "" then + filter = string.format("(fd.name=%s) and ", spy_file_name) + else + -- watching terminals risks looping in a live capture + filter = "(not fd.name contains /dev/pt and not fd.name contains /dev/tty) and " + end + + if read_or_write == "R" or read_or_write == "r" then + filter = string.format("%s%s", filter, "evt.is_io_read=true and ") + elseif read_or_write == "W" or read_or_write == "w" then + filter = string.format("%s%s", filter, "evt.is_io_write=true and ") + else + filter = string.format("%s%s", filter, "evt.is_io=true and ") + end + + filter = string.format("%s%s", filter, "fd.type=file and evt.dir=< and evt.failed=false") + + if verbose then + print("filter=" .. filter) + end + + chisel.set_filter(filter) + + return true +end + +-- Event parsing callback +function on_event() + -- Extract the event details + local data = evt.field(fdata) + local fdname = evt.field(ffdname) + local is_write = evt.field(fisw) + local pid = evt.field(fpid) + local pname = evt.field(fpname) + local res = evt.field(fres) + local ts = evt.field(fts) + local read_write + + -- Render the message to screen + if is_write == true then + read_write = "W" + else + read_write = "R" + end + + print(string.format("%s %s(%s) %s %s %s %s", ts, pname, pid, read_write, format_bytes(res), fdname, data)) + + return true +end diff --git a/userspace/sysdig/chisels/spy_ip.lua b/userspace/sysdig/chisels/spy_ip.lua index 329175b295..5ff67a7a60 100644 --- a/userspace/sysdig/chisels/spy_ip.lua +++ b/userspace/sysdig/chisels/spy_ip.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "shows the network payloads exchanged with an IP endpoint. You can combine this chisel with the -x, -X or -T sysdig command line switches to customize the screen output"; +description = "Shows the network payloads exchanged with an IP end-point. You can combine this chisel with the -x, -X or -A sysdig command line switches to customize the screen output"; short_description = "Show the data exchanged with the given IP address"; category = "Net"; @@ -25,24 +27,39 @@ args = { { name = "host_ip", - description = "the remote host IP address", + description = "The remote host IP address", argtype = "ipv4" }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, } require "common" terminal = require "ansiterminal" +terminal.enable_color(true) -- Argument notification callback function on_set_arg(name, val) - addr = val - - return true + if name == "host_ip" then + addr = val + return true + elseif name == "disable_color" then + if val == "disable_colors" then + terminal.enable_color(false) + end + return true + end + + return false end -- Initialization callback function on_init() - -- Request the fileds that we need + -- Request the fields that we need fdata = chisel.request_field("evt.arg.data") fisread = chisel.request_field("evt.is_io_read") fres = chisel.request_field("evt.rawarg.res") @@ -51,8 +68,8 @@ function on_init() sysdig.set_snaplen(1000) -- set the filter - chisel.set_filter("evt.is_io=true and fd.type=ipv4 and fd.ip=" .. addr) - + chisel.set_filter("evt.is_io=true and (fd.type=ipv4 or fd.type=ipv6) and fd.ip=" .. addr) + return true end diff --git a/userspace/sysdig/chisels/spy_logs.lua b/userspace/sysdig/chisels/spy_logs.lua new file mode 100644 index 0000000000..a441db359b --- /dev/null +++ b/userspace/sysdig/chisels/spy_logs.lua @@ -0,0 +1,225 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Edit this to change which files are watched by this chisel +FILE_FILTER = "(fd.name contains .log or fd.name contains _log or fd.name contains /var/log) and not (fd.name contains .gz or fd.name contains .tgz)" + +-- Chisel description +description = "This chisel intercepts all the writes to files containing '.log' or '_log' in their name, and pretty prints them. You can combine this chisel with filters like 'proc.name=foo' (to restrict the output to a specific process), or 'evt.buffer contains foo' (to show only messages including a specific string). You can also write the events generated around each log entry to file by using the dump_file_name and dump_range_ms arguments. If running from a terminal the line will be colored Green = OK; Yellow = Warn; and Red = Error. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. ( A Blue colored container.name represents a process running within a container)"; +short_description = "Echo any write made by any process to a log file. Optionally, export the events around each log message to file."; +category = "Logs"; + +-- Argument list +args = +{ + { + name = "dump_file_name", + description = "The name of the file where the chisel will write the events related to each syslog entry.", + argtype = "string", + optional = true + }, + { + name = "dump_range_ms", + description = "The time interval to capture *before* and *after* each event, in milliseconds. For example, 500 means that 1 second around each displayed event (.5s before and .5s after) will be saved to . The default value for dump_range_ms is 1000.", + argtype = "int", + optional = true + }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +-- Imports and globals +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) +local do_dump = false +local dump_file_name = nil +local dump_range_ms = "1000" +local entrylist = {} +local capturing = false +local lastfd = "" +local verbose = true + +-- Argument notification callback +function on_set_arg(name, val) + if name == "dump_file_name" then + do_dump = true + dump_file_name = val + return true + elseif name == "dump_range_ms" then + dump_range_ms = val + return true + elseif name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + return true + end + + return false +end + +-- Initialization callback +function on_init() + -- Request the fields that we need + fbuf = chisel.request_field("evt.buffer") + ftid = chisel.request_field("thread.tid") + fpname = chisel.request_field("proc.name") + ffdname = chisel.request_field("fd.name") + fcontainername = chisel.request_field("container.name") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + -- increase the snaplen so we capture more of the conversation + sysdig.set_snaplen(2000) + + -- set the output format to ascii + sysdig.set_output_format("ascii") + + -- set the filter + chisel.set_filter("(" .. FILE_FILTER .. ") and evt.is_io_write=true and evt.dir=< and evt.failed=false") + + -- determine if we're printing our output in a terminal + is_tty = sysdig.is_tty() + + return true +end + +-- Final chisel initialization +function on_capture_start() + if do_dump then + if sysdig.is_live() then + print("events export not supported on live captures") + return false + end + end + + capturing = true + + return true +end + +-- Event parsing callback +function on_event() + -- Extract the event details + local buf = evt.field(fbuf) + local fdname + local pname + + local containername = evt.field(fcontainername) + + if verbose then + fdname = evt.field(ffdname) + pname = evt.field(fpname) + end + + local msgs = split(buf, "\n") + + -- Render the message to screen + for i, msg in ipairs(msgs) do + if #msg ~= 0 then + local infostr + + if verbose then + infostr = pname .. " " .. fdname .. " " + else + infostr = "" + end + + if is_tty then + local color = terminal.green + local ls = string.lower(msg) + + if ls.find(ls, "warn") ~= nil then + color = terminal.yellow + elseif ls.find(msg, "err") then + color = terminal.red + end + + -- Setup the colors for the container option -pc + local container = "" + if print_container then + if containername ~= "host" then + -- Make container blue + container = string.format("%s %s", terminal.blue, containername ); + else + -- Make container color (Green, Red, or Yellow) + container = string.format("%s %s", color, containername ); + end + end + + -- Always make sure anything after container is the color (Green, Red, or Yellow) + local infostr_msg = string.format("%s %s%s", color, infostr, msg ); + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + infostr = string.format("%s %s %s", color, container, infostr_msg) + else + infostr = string.format("%s %s%s", color, infostr, msg) + end + else + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + infostr = string.format("%s %s%s", containername, infostr, msg) + else + infostr = string.format("%s%s", infostr, msg) + end + end + + print(infostr) + end + end + + if do_dump then + local hi, low = evt.get_ts() + local tid = evt.field(ftid) + table.insert(entrylist, {hi, low, tid}) + end + + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + if is_tty then + print(terminal.reset) + end + + if do_dump then + if capturing then + local sn = sysdig.get_evtsource_name() + + local args = "-F -r" .. sn .. " -w" .. dump_file_name .. " " + + for i, v in ipairs(entrylist) do + if i ~= 1 then + args = args .. " or " + end + + args = args .. "(evt.around[" .. ts_to_str(v[1], v[2]) .. "]=" .. dump_range_ms .. " and thread.tid=" .. v[3] .. ")" + end + + print("Writing events for " .. #entrylist .. " log entries") + sysdig.run_sysdig(args) + end + end +end diff --git a/userspace/sysdig/chisels/spy_port.lua b/userspace/sysdig/chisels/spy_port.lua new file mode 100644 index 0000000000..ca61c30489 --- /dev/null +++ b/userspace/sysdig/chisels/spy_port.lua @@ -0,0 +1,108 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Shows the network payloads exchanged using a given IP port number. You can combine this chisel with the -x, -X or -A sysdig command line switches to customize the screen output"; +short_description = "Show the data exchanged using the given IP port number"; +category = "Net"; + +-- Chisel argument list +args = +{ + { + name = "host_port", + description = "The remote host IP port number", + argtype = "int" + }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + +-- Argument notification callback +function on_set_arg(name, val) + if name == "host_port" then + port = val + return true + elseif name == "disable_color" then + if val == "disable_colors" then + terminal.enable_color(false) + end + return true + end + return false +end + +-- Initialization callback +function on_init() + -- Request the fields that we need + fdata = chisel.request_field("evt.arg.data") + fisread = chisel.request_field("evt.is_io_read") + fres = chisel.request_field("evt.rawarg.res") + + -- increase the snaplen so we capture more of the conversation + sysdig.set_snaplen(1000) + + -- set the filter + chisel.set_filter("evt.is_io=true and (fd.type=ipv4 or fd.type=ipv6) and fd.port=" .. port ) + return true +end + +DIR_READ = 1 +DIR_WRITE = 2 + +direction = nil + +-- Event parsing callback +function on_event() + res = evt.field(fres) + data = evt.field(fdata) + + if res == nil or res <= 0 then + return true + end + + if data ~= nil then + isread = evt.field(fisread) + + if isread and direction ~= DIR_READ then + infostr = string.format("%s------ Read %s", terminal.red, format_bytes(res)) + direction = DIR_READ + elseif not isread and direction ~= DIR_WRITE then + infostr = string.format("%s------ Write %s", terminal.blue, format_bytes(res)) + direction = DIR_WRITE + end + + print(infostr) + print(data) + end + + return true +end + +function on_capture_end() + print(terminal.reset) +end diff --git a/userspace/sysdig/chisels/spy_syslog.lua b/userspace/sysdig/chisels/spy_syslog.lua new file mode 100644 index 0000000000..8f02b49c2e --- /dev/null +++ b/userspace/sysdig/chisels/spy_syslog.lua @@ -0,0 +1,218 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Print every message written to syslog by any process. You can combine this chisel with filters like 'proc.name=foo' (to restrict the output to a specific process), or 'syslog.message contains foo' (to show only messages including a specific string). You can also write the events generated around each log entry to file by using the dump_file_name and dump_range_ms arguments."; +short_description = "Print every message written to syslog. Optionally, export the events around each syslog message to file."; +category = "Logs"; + +-- Argument list +args = +{ + { + name = "dump_file_name", + description = "The name of the file where the chisel will write the events related to each syslog entry.", + argtype = "string", + optional = true + }, + { + name = "dump_range_ms", + description = "The time interval to capture *before* and *after* each event, in milliseconds. For example, 500 means that 1 second around each displayed event (.5s before and .5s after) will be saved to . The default value for dump_range_ms is 1000.", + argtype = "int", + optional = true + }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} +-- Imports and globals +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) +local do_dump = false +local dump_file_name = nil +local dump_range_ms = "1000" +local entrylist = {} +local capturing = false + +-- Argument notification callback +function on_set_arg(name, val) + if name == "dump_file_name" then + do_dump = true + dump_file_name = val + return true + elseif name == "dump_range_ms" then + dump_range_ms = val + return true + elseif name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + return true + end + + return false +end + +-- Initialization callback +function on_init() + -- Request the fields that we need + ffac = chisel.request_field("syslog.facility.str") + fsev = chisel.request_field("syslog.severity.str") + fsevcode = chisel.request_field("syslog.severity") + fmsg = chisel.request_field("syslog.message") + ftid = chisel.request_field("thread.tid") + fpname = chisel.request_field("proc.name") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + -- increase the snaplen so we capture more of the conversation + sysdig.set_snaplen(1000) + + -- set the filter + chisel.set_filter("fd.name contains /dev/log and evt.is_io_write=true and evt.dir=< and evt.failed=false") + + is_tty = sysdig.is_tty() + + return true +end + +-- Final chisel initialization +function on_capture_start() + if do_dump then + if sysdig.is_live() then + print("events export not supported on live captures") + return false + end + end + + capturing = true + + return true +end + +-- Event parsing callback +function on_event() + + local color = "" + + -- Extract the event details + local fac = evt.field(ffac) + local sev = evt.field(fsev) + local msg = evt.field(fmsg) + local sevcode = evt.field(fsevcode) + local tid = evt.field(ftid) + local pname = evt.field(fpname) + local containername = evt.field(fcontainername) + local containerid = evt.field(fcontainerid) + + -- Render the message to screen + if is_tty then + local color = terminal.green + + if sevcode == 4 then + color = terminal.yellow + elseif sevcode < 4 then + color = terminal.red + elseif containername ~= "host" then + -- If -pc or -pcontainer option change default to blue + color = terminal.blue + else + color = terminal.green + end + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + infostr = string.format("%s%-20s %-20s %s.%s %s[%u] %s", + color, + containerid, + containername, + fac, + sev, + pname, + tid, + msg) + else + infostr = string.format("%s%s.%s %s[%u] %s", + color, + fac, + sev, + pname, + tid, + msg) + end + else + if print_container then + infostr = string.format("%-20s %-20s %s.%s %s[%u] %s", + fac, + containerid, + containername, + sev, + pname, + tid, + msg) + else + infostr = string.format("%s.%s %s[%u] %s", + fac, + sev, + pname, + tid, + msg) + end + end + + print(infostr) + + if do_dump then + local hi, low = evt.get_ts() + table.insert(entrylist, {hi, low, tid}) + end + + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + if is_tty then + print(terminal.reset) + end + + if do_dump then + if capturing then + local sn = sysdig.get_evtsource_name() + + local args = "-F -r" .. sn .. " -w" .. dump_file_name .. " " + + for i, v in ipairs(entrylist) do + if i ~= 1 then + args = args .. " or " + end + + args = args .. "(evt.around[" .. ts_to_str(v[1], v[2]) .. "]=" .. dump_range_ms .. " and thread.tid=" .. v[3] .. ")" + end + + print("Writing events for " .. #entrylist .. " log entries") + sysdig.run_sysdig(args) + end + end +end diff --git a/userspace/sysdig/chisels/spy_users.lua b/userspace/sysdig/chisels/spy_users.lua index ab32bf4e1c..4a7b4e126c 100644 --- a/userspace/sysdig/chisels/spy_users.lua +++ b/userspace/sysdig/chisels/spy_users.lua @@ -1,50 +1,241 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "lists every command that users launch interactively (e.g. from bash) and every directory users visit"; +description = "Lists every command that users launch interactively (e.g. from bash) and every directory users visit. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. (Blue represents a process running within a container, and Green represents a host process)"; short_description = "Display interactive user activity"; category = "Security"; -- Chisel argument list -args = {} +args = +{ + { + name = "max_depth", + description = "the maximum depth to show in the hierarchy of processes", + argtype = "int", + optional = true + }, + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + +MAX_ANCESTOR_NAVIGATION = 16 +max_depth = -1 + +-- Argument notification callback +function on_set_arg(name, val) + if name == "max_depth" then + max_depth = parse_numeric_input(val, name) + elseif name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + end + + return true +end -- Initialization callback function on_init() - -- Request the fileds that we need + -- Request the fields needed for this chisel fetype = chisel.request_field("evt.type") fexe = chisel.request_field("proc.exe") fargs = chisel.request_field("proc.args") fdir = chisel.request_field("evt.arg.path") fuser = chisel.request_field("user.name") + fdtime = chisel.request_field("evt.time.s") + fpid = chisel.request_field("proc.pid") + fppid = chisel.request_field("proc.ppid") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + fanames = {} + fapids = {} + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() -- set the filter - chisel.set_filter("(evt.type=execve and proc.name!=bash and proc.parentname contains sh) or (evt.type=chdir and evt.dir=< and proc.name contains sh)") - + chisel.set_filter("((evt.type=execve and evt.dir=<) or (evt.type=chdir and evt.dir=< and proc.name contains sh and not proc.name contains sshd)) and evt.failed=false") + + for j = 0, MAX_ANCESTOR_NAVIGATION do + fanames[j] = chisel.request_field("proc.aname[" .. j .. "]") + fapids[j] = chisel.request_field("proc.apid[" .. j .. "]") + end + return true end +process_tree = {} + -- Event parsing callback function on_event() - if evt.field(fetype) == "chdir" then - print(evt.field(fuser) .. ")" .. "cd " .. evt.field(fdir)) + + local color = "" + + -- If -pc or -pcontainer option change default to green + if print_container then + color = terminal.green + end + + + local user = evt.field(fuser) + local dtime = evt.field(fdtime) + local pid = evt.field(fpid) + local ppid = evt.field(fppid) + local ischdir = evt.field(fetype) == "chdir" + local containername = evt.field(fcontainername) + local containerid = evt.field(fcontainerid) + local aname + local icorr = 1 + + if ischdir then + ppid = pid + table.insert(fanames, 0, 0) + table.insert(fapids, 0, 0) + icorr = 0 + end + + if user == nil then + user = "" + end + + if not process_tree[ppid] then + -- No parent pid in the table yet. + -- Add one and make sure that there's a shell among the ancestors + process_tree[ppid] = {-1} + + for j = 1, MAX_ANCESTOR_NAVIGATION do + aname = evt.field(fanames[j]) + + if aname == nil then + if evt.field(fapids[j]) == nil then + -- no shell in the ancestor list, hide this command + break + end + elseif string.len(aname) >= 2 and aname:sub(-2) == "sh" then + apid = evt.field(fapids[j]) + if process_tree[apid] then + process_tree[ppid] = {j - 1, apid} + else + process_tree[ppid] = {0, apid} + end + end + end + end + + if process_tree[ppid][1] == -1 then + -- the parent process has already been detected as NOT having a shell ancestor + return true + end + + if not process_tree[pid] then + process_tree[pid] = {1 + process_tree[ppid][1], process_tree[ppid][2]} + end + + if ischdir then + + if max_depth ~= -1 then + if process_tree[pid][1] - icorr > max_depth then + return true + end + end + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + + -- Container will print out as blue + if containername ~= "host" then + color = terminal.blue + end + + print(color .. + extend_string("", 4 * (process_tree[pid][1] - icorr)) .. process_tree[pid][2] .. " " .. + dtime .. " " .. + user .. "@" .. + containername ..") cd " .. + evt.field(fdir)) + + else + + print(color .. + extend_string("", 4 * (process_tree[pid][1] - icorr)) .. process_tree[pid][2] .. " " .. + dtime .. " " .. + user .. ") cd " .. + evt.field(fdir)) + + end else - print(evt.field(fuser) .. ")" .. evt.field(fexe) .. " " .. evt.field(fargs)) + if max_depth ~= -1 then + if process_tree[pid][1] - 1 > max_depth then + return true + end + end + + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + + -- Container will print out as blue + if containername ~= "host" then + color = terminal.blue + end + + print(color .. + extend_string("", 4 * (process_tree[pid][1] - 1)) .. process_tree[pid][2] .. " " .. + dtime .. " " .. + user .. "@" .. + containername ..") " .. + evt.field(fexe) .. " " .. + evt.field(fargs)) + else + + print(color .. + extend_string("", 3 * (process_tree[pid][1] - 1)) .. process_tree[pid][2] .. " " .. + dtime .. " " .. + user ..") " .. + evt.field(fexe) .. " " .. + evt.field(fargs)) + + end + +-- Tabular format, a future option with potentially a chisel cmd line argument? +-- print(color .. string.format("%10.10s %-10.10s %-8.8s %-20.20s %-20.20s %-15.15s %s", +-- (process_tree[pid][1] - 1), +-- process_tree[pid][2], +-- dtime, +-- containerid, +-- containername, +-- user, +-- evt.field(fexe) .. " " .. evt.field(fargs))) + + end return true end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end() + print(terminal.reset) +end diff --git a/userspace/sysdig/chisels/statsd.lua b/userspace/sysdig/chisels/statsd.lua new file mode 100644 index 0000000000..0abfbf08df --- /dev/null +++ b/userspace/sysdig/chisels/statsd.lua @@ -0,0 +1,168 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Statsd client +-- +-- For statsd protocol info: https://github.com/b/statsd_spec + +local math = require "math" +local os = require "os" + +math.randomseed(os.time()) + +local function send_to_socket(self, string) + return sysdig.udp_send(string) +end + +local function make_statsd_message(self, stat, delta, kind, sample_rate) + -- Build prefix + local prefix = "" + + if self.namespace ~= nil then prefix = self.namespace.."." end + + -- Escape the stat name + stat = stat:gsub("[:|@]", "_") + + -- Append the sample rate + local rate = "" + + if sample_rate ~= 1 then rate = "|@"..sample_rate end + + return prefix..stat..":"..delta.."|"..kind..rate +end + +local function send(self, stat, delta, kind, sample_rate, neg) + local packet_size = self.packet_size + + local msg + local stat_type = type(stat) + if stat_type == 'table' then sample_rate = delta end + sample_rate = sample_rate or 1 + if not (sample_rate == 1 or math.random() <= sample_rate) then + return + end + + if stat_type == 'table' then + local t, size = {}, 0 + for s, v in pairs(stat) do + if kind == 'c' then + if type(s) == 'number' then + -- this is array of keys ( increment{'register', 'register_accept'}) + s, v = v, 1 + end + v = neg and -v or v + end + msg = make_statsd_message(self, s, v, kind, sample_rate) + size = size + #msg + + if t[1] and (size > packet_size) then + local msg = table.concat(t, "\n") + local ok, err = self:send_to_socket(msg) + if not ok then return nil, err end + t, size = {}, 0 + end + + t[#t + 1] = msg + end + msg = table.concat(t, "\n") + else + msg = make_statsd_message(self, stat, delta, kind, sample_rate) + end + + return self:send_to_socket(msg) +end + +-- Record an instantaneous measurement. It's different from a counter in that +-- the value is calculated by the client rather than the server. +local function gauge(self, stat, value, sample_rate) + return self:send(stat, value, "g", sample_rate) +end + +local function counter_(self, stat, value, sample_rate, ...) + return self:send(stat, value, "c", sample_rate, ...) +end + +-- A counter is a gauge whose value is calculated by the statsd server. The +-- client merely gives a delta value by which to change the gauge value. +local function counter(self, stat, value, sample_rate) + return counter_(self, stat, value, sample_rate) +end + +-- Increment a counter by `value`. +local function increment(self, stat, value, sample_rate) + return counter_(self, stat, value or 1, sample_rate, false) +end + +-- Decrement a counter by `value`. +local function decrement(self, stat, value, sample_rate) + value = value or 1 + if type(stat) == 'string' then value = -value end + return counter_(self, stat, value, sample_rate, true) +end + +-- A timer is a measure of the number of milliseconds elapsed between a start +-- and end time, for example the time to complete rendering of a web page for +-- a user. +local function timer(self, stat, ms) + return self:send(stat, ms, "ms") +end + +-- A histogram is a measure of the distribution of timer values over time, +-- calculated by the statsd server. Not supported by all statsd implementations. +local function histogram(self, stat, value) + return self:send(stat, value, "h") +end + +-- A meter measures the rate of events over time, calculated by the Statsd +-- server. Not supported by all statsd implementations. +local function meter(self, stat, value) + return self:send(stat, value, "m") +end + +-- A set counts unique occurrences of events between flushes. Not supported by +-- all statsd implementations. +local function set(self, stat, value) + return self:send(stat, value, "s") +end + +return function(options) + options = options or {} + + local host = options.host or "127.0.0.1" + local port = options.port or 8125 + local namespace = options.namespace or nil + local packet_size = options.packet_size or 508 -- RFC791 + + sysdig.udp_setpeername(host, port) + + return { + namespace = namespace, + udp = udp, + packet_size = packet_size, + gauge = gauge, + counter = counter, + increment = increment, + decrement = decrement, + timer = timer, + histogram = histogram, + meter = meter, + send = send, + send_to_socket = send_to_socket + } +end diff --git a/userspace/sysdig/chisels/stderr.lua b/userspace/sysdig/chisels/stderr.lua index b2195f176d..9cd1c70e93 100644 --- a/userspace/sysdig/chisels/stderr.lua +++ b/userspace/sysdig/chisels/stderr.lua @@ -1,33 +1,62 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "print the standard error of any process on screen. Combine this script with a filter to limit the output to a specific process or pid."; +description = "Print the standard error of any process on screen. Combine this script with a filter to limit the output to a specific process or pid. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown. (Blue represents a process running within a container, and Green represents a host process)"; short_description = "Print stderr of processes"; category = "I/O"; -args = {} +-- Chisel argument list +args = +{ + { + name = "disable_color", + description = "Set to 'disable_colors' if you want to disable color output", + argtype = "string", + optional = true + }, +} + +terminal = require "ansiterminal" +terminal.enable_color(true) + +-- Argument notification callback +function on_set_arg(name, val) + if name == "disable_color" and val == "disable_color" then + terminal.enable_color(false) + return true + end + + return false +end -- Initialization callback function on_init() - -- Request the fileds that we need + -- Request the fields that we need fbuf = chisel.request_field("evt.rawarg.data") + fcontainername = chisel.request_field("container.name") + fcontainerid = chisel.request_field("container.id") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() - -- increase the snaplen so we capture more of the conversation + -- increase the snaplen so we capture more of the conversation sysdig.set_snaplen(2000) -- set the filter @@ -38,10 +67,35 @@ end -- Event parsing callback function on_event() - buf = evt.field(fbuf) + + local color = "" + + -- If -pc or -pcontainer option change default to green + if print_container then + color = terminal.green + end + + local buf = evt.field(fbuf) + local containername = evt.field(fcontainername) + local containerid = evt.field(fcontainerid) if buf ~= nil then - print(buf) + -- The -pc or -pcontainer options was supplied on the cmd line + if print_container then + + -- Container will print out as blue + if containername ~= "host" then + color = terminal.blue + end + + print(color .. string.format("%-20.20s %-20.20s %s", + containerid, + containername, + buf )) + + else + print(buf) + end end return true diff --git a/userspace/sysdig/chisels/stdin.lua b/userspace/sysdig/chisels/stdin.lua index 85a1bd31b3..408f8323c5 100644 --- a/userspace/sysdig/chisels/stdin.lua +++ b/userspace/sysdig/chisels/stdin.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "print the standard input of any process on screen. Combine this script with a filter to limit the output to a specific process or pid."; +description = "Print the standard input of any process on screen. Combine this script with a filter to limit the output to a specific process or pid."; short_description = "Print stdin of processes"; category = "I/O"; @@ -24,7 +26,7 @@ args = {} -- Initialization callback function on_init() - -- Request the fileds that we need + -- Request the fields that we need fbuf = chisel.request_field("evt.rawarg.data") -- increase the snaplen so we capture more of the conversation diff --git a/userspace/sysdig/chisels/stdout.lua b/userspace/sysdig/chisels/stdout.lua index fe3ca819c2..52331b1e2e 100644 --- a/userspace/sysdig/chisels/stdout.lua +++ b/userspace/sysdig/chisels/stdout.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "print the standard output of any process on screen. Combine this script with a filter to limit the output to a specific process or pid."; +description = "Print the standard output of any process on screen. Combine this script with a filter to limit the output to a specific process or pid."; short_description = "Print stdout of processes"; category = "I/O"; @@ -24,7 +26,7 @@ args = {} -- Initialization callback function on_init() - -- Request the fileds that we need + -- Request the fields that we need fbuf = chisel.request_field("evt.rawarg.data") -- increase the snaplen so we capture more of the conversation diff --git a/userspace/sysdig/chisels/subsecoffset.lua b/userspace/sysdig/chisels/subsecoffset.lua new file mode 100644 index 0000000000..2d623d77c3 --- /dev/null +++ b/userspace/sysdig/chisels/subsecoffset.lua @@ -0,0 +1,191 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +Copyright (C) 2015 Brendan Gregg. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "This visualizes the subsecond offset time of system call execution. This allows repetitive patterns to be identified, which are lost when averaging at a one second granularity. The Y axis (vertical) shows the passage of time. The X axis (horizontal) shows the passage of time within whole or fractions of a second. By default, the X axis range is 1000 milliseconds; this can be specified as an argument (try 100). Each bucket of the heat map, or spectrogram, is shown as a colored rectangle. The color shows how many syscalls fell into that time and subsecond offset range. It can be black (no calls), green (tens of calls/s), yellow (hundreds of calls/s) or red (Thousands of calls/s). Use this chisel in conjunction with filters to visualize latencies for certain processes, types of I/O activity, file systems, etc." +short_description = "Visualize subsecond offset execution time." +category = "CPU Usage" + +-- Chisel argument list +args = { + { + name = "refresh_time", + description = "chart refresh time in milliseconds", + argtype = "int", + optional = true + }, +} + +require "common" +terminal = require "ansiterminal" +terminal.enable_color(true) + +refresh_time = 1000 * 1000 * 1000 +refresh_per_sec = 1 * 1000 * 1000 * 1000 / refresh_time +max_label_len = 0 +frequencies = {} +colpalette = {22, 28, 64, 34, 2, 76, 46, 118, 154, 191, 227, 226, 11, 220, 209, 208, 202, 197, 9, 1} +charpalette = {" ", "░", "▒", "░"} + +-- Argument initialization +function on_set_arg(name, val) + if name == "refresh_time" then + refresh_time = parse_numeric_input(val, name) * 1 * 1000 * 1000 + refresh_per_sec = 1 * 1000 * 1000 * 1000 / refresh_time + return true + end + + return false +end + +-- Initialization callback +function on_init() + is_tty = sysdig.is_tty() + + if not is_tty then + print("This chisel only works on ANSI terminals. Aborting.") + return false + end + + tinfo = sysdig.get_terminal_info() + w = tinfo.width + h = tinfo.height + max_label_len = string.len("|" .. (0.9 * refresh_time / 10000000) .. "ms") + + -- trace syscall entry + chisel.set_filter("evt.dir=>") + + rawtime = chisel.request_field("evt.rawtime") + + terminal.hidecursor() + + print("Tracing syscalls... red (hot) == high frequency <-> green == low frequency.\n") + + return true +end + +-- Final chisel initialization +function on_capture_start() + chisel.set_interval_ns(refresh_time) + return true +end + +-- Event parsing callback +function on_event() + local subsec = evt.field(rawtime) + + -- subsec is normalized to terminal column location + subsec = math.floor((((subsec * 1000 / refresh_time) % 1000) / 1000) * w) + + if frequencies[subsec] == nil then + frequencies[subsec] = 1 + else + frequencies[subsec] = frequencies[subsec] + 1 + end + + return true +end + +-- Calculate colors and character to be used +function mkcol(n) + local col = math.log10(n * refresh_per_sec + 1) / math.log10(1.6) + + if col < 1 then + col = 1 + elseif col > #colpalette then + col = #colpalette + end + + local low_col = math.floor(col) + local high_col = math.ceil(col) + local delta = col - low_col + local ch = charpalette[math.floor(1 + delta * #charpalette)] + + -- If delta is > 75% we use 25% fill and flip fg and bg to fake a 75% filled block + if delta > .75 then + return colpalette[high_col], colpalette[low_col], ch + else + return colpalette[low_col], colpalette[high_col], ch + end +end + +-- Periodic timeout callback +function on_interval(ts_s, ts_ns, delta) + terminal.moveup(1) + + for x = 1, w do + local fr = frequencies[x] + local fg, bg, ch + + if fr == nil or fr == 0 then + terminal.setbgcol(0) + terminal.setbgcol(0) + ch = " " + else + fg, bg, ch = mkcol(fr) + terminal.setfgcol(fg) + terminal.setbgcol(bg) + end + + io.write(ch) + end + + io.write(terminal.reset .. "\n") + + local x = 0 + while true do + if x >= w then + break + end + + local curtime = math.floor(x * 10 / w) + local prevtime = math.floor((x - 1) * 10 / w) + + if curtime ~= prevtime then + if (x <= w - max_label_len) then + local tstr = "|" .. (math.floor(10 * x / w) * refresh_time / 10000000) .. "ms" + io.write(tstr) + x = x + string.len(tstr) + else + io.write(" ") + x = x + 1 + end + else + io.write(" ") + x = x + 1 + end + end + + io.write("\n") + + frequencies = {} + + return true +end + +function on_capture_end(ts_s, ts_ns, delta) + if is_tty then + print(terminal.reset) + terminal.showcursor() + end + + return true +end + diff --git a/userspace/sysdig/chisels/table_generator.lua b/userspace/sysdig/chisels/table_generator.lua index 2f409c4cc3..8e9de48317 100644 --- a/userspace/sysdig/chisels/table_generator.lua +++ b/userspace/sysdig/chisels/table_generator.lua @@ -1,62 +1,64 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Given two filter fields, a key and a value, this chisel creates and renders to the screen a table." -short_description = "FD bytes group by" -category = "IO" +description = "Given two filter fields, a key and a value, this chisel creates and renders a table to the screen." +short_description = "Filter on a key and value" +category = "Filter" hidden = true -- Chisel argument list -args = +args = { { - name = "key", - description = "the filter field used for grouping", + name = "keys", + description = "comma-separated list of filter fields to use for grouping", argtype = "string" }, { - name = "keydesc", - description = "human readable description for the key", + name = "keydescs", + description = "comma separated list of human readable descriptions for the key", argtype = "string" }, { - name = "value", - description = "the value to count for every key", + name = "value", + description = "the value to count for every key", argtype = "string" }, { - name = "valuedesc", - description = "human readable description for the value", + name = "valuedesc", + description = "human readable description for the value", argtype = "string" }, { - name = "filter", - description = "the filter to apply", + name = "filter", + description = "the filter to apply", argtype = "string" }, { - name = "top_number", - description = "maximum number of elements to display", + name = "top_number", + description = "maximum number of elements to display", argtype = "string" }, { - name = "result_rendering", - description = "how to render the values in the result. Can be 'bytes', 'time', 'timepct', or 'none'.", + name = "value_units", + description = "how to render the values in the result. Can be 'bytes', 'time', 'timepct', or 'none'.", argtype = "string" }, } @@ -64,48 +66,63 @@ args = require "common" terminal = require "ansiterminal" -top_number = 0 grtable = {} -key_fld = "" -key_desc = "" -value_fld = "" -value_desc = "" filter = "" -result_rendering = "none" islive = false +fkeys = {} + +vizinfo = +{ + key_fld = {}, + key_desc = {}, + value_fld = "", + value_desc = "", + value_units = "none", + top_number = 0, + output_format = "normal" +} -- Argument notification callback function on_set_arg(name, val) - if name == "key" then - key_fld = val + if name == "keys" then + vizinfo.key_fld = split(val, ",") return true - elseif name == "keydesc" then - key_desc = val + elseif name == "keydescs" then + vizinfo.key_desc = split(val, ",") return true elseif name == "value" then - value_fld = val + vizinfo.value_fld = val return true elseif name == "valuedesc" then - value_desc = val + vizinfo.value_desc = val return true elseif name == "filter" then filter = val return true elseif name == "top_number" then - top_number = tonumber(val) + vizinfo.top_number = tonumber(val) return true - elseif name == "result_rendering" then - result_rendering = val + elseif name == "value_units" then + vizinfo.value_units = val return true end return false end +-- Initialization callback function on_init() + if #vizinfo.key_fld ~= #vizinfo.key_desc then + print("error: number of entries in keys different from number entries in keydescs") + return false + end + -- Request the fields we need - fkey = chisel.request_field(key_fld) - fvalue = chisel.request_field(value_fld) + for i, name in ipairs(vizinfo.key_fld) do + fkeys[i] = chisel.request_field(name) + end + + fvalue = chisel.request_field(vizinfo.value_fld) -- set the filter if filter ~= "" then @@ -115,23 +132,43 @@ function on_init() return true end +-- Final chisel initialization function on_capture_start() islive = sysdig.is_live() + vizinfo.output_format = sysdig.get_output_format() if islive then chisel.set_interval_s(1) - terminal.clearscreen() - terminal.hidecursor() + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.hidecursor() + end end - + return true end +-- Event parsing callback function on_event() - key = evt.field(fkey) + local key = nil + local kv = nil + + for i, fld in ipairs(fkeys) do + kv = evt.field(fld) + if kv == nil then + return + end + + if key == nil then + key = kv + else + key = key .. "\001\001" .. evt.field(fld) + end + end + value = evt.field(fvalue) - if key ~= nil and value ~= nil and value > 0 then + if value ~= nil and value > 0 then entryval = grtable[key] if entryval == nil then @@ -144,11 +181,14 @@ function on_event() return true end -function on_interval(ts_s, ts_ns, delta) - terminal.clearscreen() - terminal.goto(0, 0) +-- Periodic timeout callback +function on_interval(ts_s, ts_ns, delta) + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.moveto(0, 0) + end - print_sorted_table(grtable, delta, result_rendering) + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) -- Clear the table grtable = {} @@ -156,15 +196,16 @@ function on_interval(ts_s, ts_ns, delta) return true end +-- Called by the engine at the end of the capture (Ctrl-C) function on_capture_end(ts_s, ts_ns, delta) - if islive then + if islive and vizinfo.output_format ~= "json" then terminal.clearscreen() - terminal.goto(0 ,0) + terminal.moveto(0 ,0) terminal.showcursor() return true end - print_sorted_table(grtable, delta, result_rendering) + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) return true end diff --git a/userspace/sysdig/chisels/topconns.lua b/userspace/sysdig/chisels/topconns.lua index 825dc426f2..1d956bb78d 100644 --- a/userspace/sysdig/chisels/topconns.lua +++ b/userspace/sysdig/chisels/topconns.lua @@ -1,30 +1,32 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Shows the top network connections in terms of total (in+out) bandwidth."; -short_description = "top network connections by total bytes"; +description = "Shows the top network connections in terms of total (in+out) bandwidth. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown."; +short_description = "Top network connections by total bytes"; category = "Net"; -- Chisel argument list args = {} -- The number of items to show -TOP_NUMBER = 10 +TOP_NUMBER = 30 -- Argument notification callback function on_set_arg(name, val) @@ -33,13 +35,29 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "fd.name", - "Tuple", - "evt.rawarg.res", - "Bytes", - "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", - "" .. TOP_NUMBER, - "bytes") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "container.name,fd.l4proto,fd.name", + "container.name,Proto,Conn", + "evt.rawarg.res", + "Bytes", + "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + else + chisel.exec("table_generator", + "fd.l4proto,fd.name", + "Proto,Conn", + "evt.rawarg.res", + "Bytes", + "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + end + return true end diff --git a/userspace/sysdig/chisels/topcontainers_cpu.lua b/userspace/sysdig/chisels/topcontainers_cpu.lua new file mode 100644 index 0000000000..394e27b052 --- /dev/null +++ b/userspace/sysdig/chisels/topcontainers_cpu.lua @@ -0,0 +1,134 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Show the top containers defined by the highest CPU utilization." +short_description = "Top containers by CPU usage" +category = "CPU Usage" + +-- Chisel argument list +args = {} + +require "common" +terminal = require "ansiterminal" + +grtable = {} +islive = false +fkeys = {} + +vizinfo = +{ + key_fld = {"container.name"}, + key_desc = {"container.name"}, + value_fld = "thread.exectime", + value_desc = "CPU%", + value_units = "timepct", + top_number = 10, + output_format = "normal" +} + +-- Initialization callback +function on_init() + -- Request the fields we need + for i, name in ipairs(vizinfo.key_fld) do + fkeys[i] = chisel.request_field(name) + end + + -- Request the fields we need + fvalue = chisel.request_field(vizinfo.value_fld) + fcpu = chisel.request_field("thread.cpu") + + chisel.set_filter("evt.type=procinfo") + + return true +end + +-- Final chisel initialization +function on_capture_start() + islive = sysdig.is_live() + vizinfo.output_format = sysdig.get_output_format() + + if islive then + chisel.set_interval_s(1) + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.hidecursor() + end + end + + return true +end + +-- Event parsing callback +function on_event() + local key = nil + local kv = nil + + for i, fld in ipairs(fkeys) do + kv = evt.field(fld) + if kv == nil then + return + end + + if key == nil then + key = kv + else + key = key .. "\001\001" .. evt.field(fld) + end + end + + local cpu = evt.field(fcpu) + + if grtable[key] == nil then + grtable[key] = cpu * 10000000 + else + grtable[key] = grtable[key] + (cpu * 10000000) + end + + return true +end + +-- Periodic timeout callback +function on_interval(ts_s, ts_ns, delta) + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.moveto(0, 0) + end + + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) + + -- Clear the table + grtable = {} + + return true +end + +-- Called by the engine at the end of the capture (Ctrl-C) +function on_capture_end(ts_s, ts_ns, delta) + if islive and vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.moveto(0 ,0) + terminal.showcursor() + return true + end + + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) + + return true +end diff --git a/userspace/sysdig/chisels/topcontainers_error.lua b/userspace/sysdig/chisels/topcontainers_error.lua new file mode 100644 index 0000000000..ecac279c40 --- /dev/null +++ b/userspace/sysdig/chisels/topcontainers_error.lua @@ -0,0 +1,44 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Shows the top container in terms of system call errors." +short_description = "Top containers by number of errors" +category = "Errors" + +-- Chisel argument list +args = {} + +-- Argument notification callback +function on_set_arg(name, val) + return false +end + +-- Initialization callback +function on_init() + chisel.exec("table_generator", + "container.name", + "container.name", + "evt.count", + "#Errors", + "evt.failed=true and container.name!=host", + "100", + "none") + return true +end diff --git a/userspace/sysdig/chisels/topcontainers_file.lua b/userspace/sysdig/chisels/topcontainers_file.lua new file mode 100644 index 0000000000..e03aa59db1 --- /dev/null +++ b/userspace/sysdig/chisels/topcontainers_file.lua @@ -0,0 +1,47 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Shows the top containers in terms of total (in+out) bytes to disk." +short_description = "Top containers by R+W disk bytes" +category = "I/O" + +-- Chisel argument list +args = {} + +-- The number of items to show +TOP_NUMBER = 10 + +-- Argument notification callback +function on_set_arg(name, val) + return false +end + +-- Initialization callback +function on_init() + chisel.exec("table_generator", + "container.name", + "container.name", + "evt.rawarg.res", + "Bytes", + "fd.type=file and evt.is_io=true and container.name!=host", + "" .. TOP_NUMBER, + "bytes") + return true +end diff --git a/userspace/sysdig/chisels/topcontainers_net.lua b/userspace/sysdig/chisels/topcontainers_net.lua new file mode 100644 index 0000000000..2fbae48df0 --- /dev/null +++ b/userspace/sysdig/chisels/topcontainers_net.lua @@ -0,0 +1,40 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Sorted list of containers that use the most network bandwidth." +short_description = "Top containers by network I/O" +category = "Net" + +-- Chisel argument list +args = {} + +-- Initialization callback +function on_init() + chisel.exec("table_generator", + "container.name", + "container.name", + "evt.rawarg.res", + "Bytes", + "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true and container.name!=host", + "100", + "bytes") + + return true +end diff --git a/userspace/sysdig/chisels/topfiles_bytes.lua b/userspace/sysdig/chisels/topfiles_bytes.lua index 2d60a4ecb1..eab2388ed9 100644 --- a/userspace/sysdig/chisels/topfiles_bytes.lua +++ b/userspace/sysdig/chisels/topfiles_bytes.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Shows the top files in terms of disk usage." +description = "Shows the top files in terms of disk usage. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "Top files by R+W bytes" category = "I/O" @@ -33,13 +35,29 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "fd.name", - "Filename", - "evt.rawarg.res", - "Bytes", - "fd.type=file and evt.is_io=true", - "" .. TOP_NUMBER, - "bytes") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "container.name,fd.name", + "container.name,Filename", + "evt.rawarg.res", + "Bytes", + "fd.type=file and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + else + chisel.exec("table_generator", + "fd.name", + "Filename", + "evt.rawarg.res", + "Bytes", + "fd.type=file and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + end + return true end diff --git a/userspace/sysdig/chisels/topfiles_errors.lua b/userspace/sysdig/chisels/topfiles_errors.lua index ef9e48cafe..5def227fe9 100644 --- a/userspace/sysdig/chisels/topfiles_errors.lua +++ b/userspace/sysdig/chisels/topfiles_errors.lua @@ -1,24 +1,26 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Shows the top files in terms of I/O errros." -short_description = "top files by number of errors" -category = "errors" +description = "Shows the top files in terms of I/O errors. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." +short_description = "Top files by number of errors" +category = "Errors" -- Chisel argument list args = {} @@ -33,13 +35,28 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "fd.name", - "Filename", - "evt.count", - "#Errors", - "fd.type=file and evt.failed=true", - "100", - "none") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "fd.name,container.name", + "Filename,container.name", + "evt.count", + "#Errors", + "fd.type=file and evt.failed=true", + "100", + "none") + else + chisel.exec("table_generator", + "fd.name", + "Filename", + "evt.count", + "#Errors", + "fd.type=file and evt.failed=true", + "100", + "none") + end return true end diff --git a/userspace/sysdig/chisels/topfiles_time.lua b/userspace/sysdig/chisels/topfiles_time.lua index 33bcff0a21..03862256a4 100644 --- a/userspace/sysdig/chisels/topfiles_time.lua +++ b/userspace/sysdig/chisels/topfiles_time.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Shows the top files in terms of disk usage." +description = "Shows the top files in terms of disk usage. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "Top files by time" category = "I/O" @@ -33,13 +35,29 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "fd.name", - "Tilename", - "evt.latency", - "Time", - "fd.type=file and evt.is_io=true", - "" .. TOP_NUMBER, - "time") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "fd.name,container.name", + "Filename,container.name", + "evt.latency", + "Time", + "fd.type=file and evt.is_io=true", + "" .. TOP_NUMBER, + "time") + else + chisel.exec("table_generator", + "fd.name", + "Filename", + "evt.latency", + "Time", + "fd.type=file and evt.is_io=true", + "" .. TOP_NUMBER, + "time") + end + return true end diff --git a/userspace/sysdig/chisels/topports_server.lua b/userspace/sysdig/chisels/topports_server.lua index 0cbfa307f0..cd79ba9516 100644 --- a/userspace/sysdig/chisels/topports_server.lua +++ b/userspace/sysdig/chisels/topports_server.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Shows the top TCP/UDP server ports in terms of total (in+out) bandwidth."; +description = "Shows the top TCP/UDP server ports in terms of total (in+out) bandwidth. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown."; short_description = "Top TCP/UDP server ports by R+W bytes"; category = "Net"; @@ -33,13 +35,28 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "fd.sport", - "Server Port", - "evt.rawarg.res", - "Bytes", - "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", - "" .. TOP_NUMBER, - "bytes") + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "fd.sport,container.name", + "Srv Port,container.name", + "evt.rawarg.res", + "Bytes", + "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + else + chisel.exec("table_generator", + "fd.sport", + "Srv Port", + "evt.rawarg.res", + "Bytes", + "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + end + return true end diff --git a/userspace/sysdig/chisels/topprocs_cpu.lua b/userspace/sysdig/chisels/topprocs_cpu.lua index cb0da435ee..54deea2427 100644 --- a/userspace/sysdig/chisels/topprocs_cpu.lua +++ b/userspace/sysdig/chisels/topprocs_cpu.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Given two filter fields, a key and a value, this chisel creates and renders to the screen a table." +description = "Show the top process defined by the highest CPU utilization. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "Top processes by CPU usage" category = "CPU Usage" @@ -26,98 +28,101 @@ args = {} require "common" terminal = require "ansiterminal" -top_number = 10 grtable = {} -key_fld = "proc.name" -key_desc = "Process" -value_fld = "thread.exectime" -value_desc = "CPU%" -result_rendering = "timepct" islive = false -cpustates = {} - +fkeys = {} +local print_container = false + +vizinfo = +{ + key_fld = {"proc.name","proc.pid"}, + key_desc = {"Process", "PID"}, + value_fld = "thread.exectime", + value_desc = "CPU%", + value_units = "timepct", + top_number = 10, + output_format = "normal" +} + +-- Initialization callback function on_init() + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + -- Print container info as well + if print_container then + -- Modify host pid column name and add container information + vizinfo.key_fld = {"proc.name", "proc.pid", "proc.vpid", "container.name"} + vizinfo.key_desc = {"Process", "Host_pid", "Container_pid", "container.name"} + end + -- Request the fields we need - fkey = chisel.request_field(key_fld) - fvalue = chisel.request_field(value_fld) - fnext = chisel.request_field("evt.arg.next") - fnextraw = chisel.request_field("evt.rawarg.next") - - chisel.set_filter("evt.type=switch") + for i, name in ipairs(vizinfo.key_fld) do + fkeys[i] = chisel.request_field(name) + end + + -- Request the fields we need + fvalue = chisel.request_field(vizinfo.value_fld) + fcpu = chisel.request_field("thread.cpu") + chisel.set_filter("evt.type=procinfo") + return true end +-- Final chisel initialization function on_capture_start() islive = sysdig.is_live() + vizinfo.output_format = sysdig.get_output_format() if islive then chisel.set_interval_s(1) - terminal.clearscreen() - terminal.hidecursor() - end - - ncpus = sysdig.get_machine_info().num_cpus - - for j = 1, ncpus do - cpustates[j] = {0, 0, 0, ""} + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.hidecursor() + end end return true end +-- Event parsing callback function on_event() - key = evt.field(fkey) - value = evt.field(fvalue) - cpuid = evt.get_cpuid() + 1 - - if key ~= nil and value ~= nil and value > 0 then - thissec = value - cpustates[cpuid][3] - if thissec < 0 then - thissec = 0 + local key = nil + local kv = nil + + for i, fld in ipairs(fkeys) do + kv = evt.field(fld) + if kv == nil then + return end - if grtable[key] == nil then - grtable[key] = thissec + if key == nil then + key = kv else - grtable[key] = grtable[key] + thissec + key = key .. "\001\001" .. evt.field(fld) end - - cpustates[cpuid][1], cpustates[cpuid][2] = evt.get_ts() end - if evt.field(fnext) ~= "" .. evt.field(fnextraw) then - cpustates[cpuid][4] = evt.field(fnext) + local cpu = evt.field(fcpu) + + if grtable[key] == nil then + grtable[key] = cpu * 10000000 else - cpustates[cpuid][4] = nil + grtable[key] = grtable[key] + (cpu * 10000000) end - cpustates[cpuid][3] = 0 - return true end +-- Periodic timeout callback function on_interval(ts_s, ts_ns, delta) - terminal.clearscreen() - terminal.goto(0, 0) - - for cpuid = 1, ncpus do - if cpustates[cpuid][1] ~= 0 then - cpustates[cpuid][3] = 1000000000 - cpustates[cpuid][2] - - key = cpustates[cpuid][4] - - if key ~= nil and value ~= nil and value > 0 then - if grtable[key] == nil then - grtable[key] = cpustates[cpuid][3] - else - grtable[key] = grtable[key] + cpustates[cpuid][3] - end - end - end + if vizinfo.output_format ~= "json" then + terminal.clearscreen() + terminal.moveto(0, 0) end - print_sorted_table(grtable, 1000000000, result_rendering) + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) -- Clear the table grtable = {} @@ -125,16 +130,16 @@ function on_interval(ts_s, ts_ns, delta) return true end +-- Called by the engine at the end of the capture (Ctrl-C) function on_capture_end(ts_s, ts_ns, delta) - if islive then + if islive and vizinfo.output_format ~= "json" then terminal.clearscreen() - terminal.goto(0 ,0) + terminal.moveto(0 ,0) terminal.showcursor() return true end - print_sorted_table(grtable, delta, result_rendering) + print_sorted_table(grtable, ts_s, 0, delta, vizinfo) return true end - diff --git a/userspace/sysdig/chisels/topprocs_errors.lua b/userspace/sysdig/chisels/topprocs_errors.lua index c932d7812e..cca4ac9cbf 100644 --- a/userspace/sysdig/chisels/topprocs_errors.lua +++ b/userspace/sysdig/chisels/topprocs_errors.lua @@ -1,24 +1,26 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Shows the top processes in terms of system call errors." +description = "Shows the top processes in terms of system call errors. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "top processes by number of errors" -category = "errors" +category = "Errors" -- Chisel argument list args = {} @@ -30,13 +32,29 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "proc.name", - "Process", - "evt.count", - "#Errors", - "evt.failed=true", - "100", - "none") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "proc.name,proc.pid,thread.vtid,container.name", + "Process,Host_pid,Container_pid,container.name", + "evt.count", + "#Errors", + "evt.failed=true", + "100", + "none") + else + chisel.exec("table_generator", + "proc.name,proc.pid", + "Process,PID", + "evt.count", + "#Errors", + "evt.failed=true", + "100", + "none") + end + return true end diff --git a/userspace/sysdig/chisels/topprocs_file.lua b/userspace/sysdig/chisels/topprocs_file.lua index f1258e77e1..db0dcca393 100644 --- a/userspace/sysdig/chisels/topprocs_file.lua +++ b/userspace/sysdig/chisels/topprocs_file.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Shows the top processes in terms of total (in+out) bytes to disk." +description = "Shows the top processes in terms of total (in+out) bytes to disk. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "Top processes by R+W disk bytes" category = "I/O" @@ -33,13 +35,29 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "proc.name", - "Process", - "evt.rawarg.res", - "Bytes", - "fd.type=file and evt.is_io=true", - "" .. TOP_NUMBER, - "bytes") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "proc.name,proc.pid,thread.vtid,container.name", + "Process,Host_pid,Container_pid,container.name", + "evt.rawarg.res", + "Bytes", + "fd.type=file and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + else + chisel.exec("table_generator", + "proc.name,proc.pid", + "Process,PID", + "evt.rawarg.res", + "Bytes", + "fd.type=file and evt.is_io=true", + "" .. TOP_NUMBER, + "bytes") + end + return true end diff --git a/userspace/sysdig/chisels/topprocs_net.lua b/userspace/sysdig/chisels/topprocs_net.lua index 31129a6b7f..6c83fde5a3 100644 --- a/userspace/sysdig/chisels/topprocs_net.lua +++ b/userspace/sysdig/chisels/topprocs_net.lua @@ -1,22 +1,24 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- Chisel description -description = "Sorthed list of the processes that use the most network bandwidth." +description = "Sort the list of the processes that use the most network bandwidth. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "Top processes by network I/O" category = "Net" @@ -25,14 +27,31 @@ args = {} -- Initialization callback function on_init() - chisel.exec("table_generator", - "proc.name", - "Process", - "evt.rawarg.res", - "Bytes", - "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", - "100", - "bytes") + + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "proc.name,proc.pid,thread.vtid,container.name", + "Process,Host_pid,Container_pid,container.name", + "evt.rawarg.res", + "Bytes", + "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", + "100", + "bytes") + else + chisel.exec("table_generator", + "proc.name,proc.pid", + "Process,PID", + "evt.rawarg.res", + "Bytes", + "(fd.type=ipv4 or fd.type=ipv6) and evt.is_io=true", + "100", + "bytes") + end + return true end diff --git a/userspace/sysdig/chisels/topscalls.lua b/userspace/sysdig/chisels/topscalls.lua index 767fddb421..d71874cc19 100644 --- a/userspace/sysdig/chisels/topscalls.lua +++ b/userspace/sysdig/chisels/topscalls.lua @@ -1,25 +1,27 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- The number of items to show TOP_NUMBER = 30 -- Chisel description -description = "Show the top " .. TOP_NUMBER .. " system calls in terms of number of calls. You can use filters to restrict this to a specific process, thread or file." +description = "Show the top " .. TOP_NUMBER .. " system calls in terms of number of calls. You can use filters to restrict this to a specific process, thread or file. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "Top system calls by number of calls" category = "Performance" @@ -33,13 +35,29 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "evt.type", - "System Call", - "evt.count", - "# Calls", - "evt.type!=switch", - "" .. TOP_NUMBER, - "none") + + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "evt.type,container.name", + "Syscall,container.name", + "evt.count", + "# Calls", + "evt.type!=switch", + "" .. TOP_NUMBER, + "none") + else + chisel.exec("table_generator", + "evt.type", + "Syscall", + "evt.count", + "# Calls", + "evt.type!=switch", + "" .. TOP_NUMBER, + "none") + end + return true end diff --git a/userspace/sysdig/chisels/topscalls_time.lua b/userspace/sysdig/chisels/topscalls_time.lua index 2c15eab079..0c6f85debd 100644 --- a/userspace/sysdig/chisels/topscalls_time.lua +++ b/userspace/sysdig/chisels/topscalls_time.lua @@ -1,25 +1,27 @@ --[[ -Copyright (C) 2013-2014 Draios inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. +This file is part of sysdig. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +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. -You should have received a copy of the GNU General Public License -along with this program. If not, see . --]] -- The number of items to show TOP_NUMBER = 30 -- Chisel description -description = "Show the top " .. TOP_NUMBER .. " system calls in terms of time spent in each call. You can use filters to restrict this to a specific process, thread or file." +description = "Show the top " .. TOP_NUMBER .. " system calls in terms of time spent in each call. You can use filters to restrict this to a specific process, thread or file. This chisel is compatible with containers using the sysdig -pc or -pcontainer argument, otherwise no container information will be shown." short_description = "Top system calls by time" category = "Performance" @@ -33,13 +35,28 @@ end -- Initialization callback function on_init() - chisel.exec("table_generator", - "evt.type", - "System Call", - "evt.latency", - "Time", - "", - "" .. TOP_NUMBER, - "time") + -- The -pc or -pcontainer options was supplied on the cmd line + print_container = sysdig.is_print_container_data() + + if print_container then + chisel.exec("table_generator", + "evt.type,container.name", + "Syscall,container.name", + "evt.latency", + "Time", + "", + "" .. TOP_NUMBER, + "time") + else + chisel.exec("table_generator", + "evt.type", + "Syscall", + "evt.latency", + "Time", + "", + "" .. TOP_NUMBER, + "time") + end + return true end diff --git a/userspace/sysdig/chisels/tracers_2_statsd.lua b/userspace/sysdig/chisels/tracers_2_statsd.lua new file mode 100644 index 0000000000..c1a840eb42 --- /dev/null +++ b/userspace/sysdig/chisels/tracers_2_statsd.lua @@ -0,0 +1,82 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "Converts sysdig span duration data into statsd metrics and pipes them to the given statsd server. See https://github.com/draios/sysdig/wiki/Tracers for more information."; +short_description = "Export spans duration as statds metrics."; +category = "Tracers"; + +args = +{ + { + name = "server_addr", + description = "The address of the statsd server to send data to", + argtype = "string", + optional = true + }, + { + name = "server_port", + description = "The UDP port to use", + argtype = "string", + optional = true + }, +} + +local lstatsd = require "statsd" +local host = "127.0.0.1" +local port = 8125 + +-- Argument notification callback +function on_set_arg(name, val) + if name == "server_addr" then + host = val + return true + elseif name == "server_port" then + port = tonumber(val) + return true + end + + return false +end + +-- Initialization callback +function on_init() + -- Initialize statsd + statsd = lstatsd({host = "127.0.0.1"}) + + -- Request the fields that we need + ftags = chisel.request_field("span.tags") + flatency = chisel.request_field("span.duration") + + -- set the filter + chisel.set_filter("evt.type=tracer and evt.dir=<") + return true +end + +-- Event parsing callback +function on_event() + local tags = evt.field(ftags) + local latency = evt.field(flatency) + + if latency then + statsd:timer(tags, tonumber(latency) / 1000000) + end + + return true +end diff --git a/userspace/sysdig/chisels/udp_extract.lua b/userspace/sysdig/chisels/udp_extract.lua new file mode 100644 index 0000000000..47cdcfba23 --- /dev/null +++ b/userspace/sysdig/chisels/udp_extract.lua @@ -0,0 +1,72 @@ +--[[ +Copyright (C) 2018 Draios inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License version 2 as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +--]] +local OUTPUT_DIR_NAME = "./udp_dump_files" + +description = "This chisel parses a trace file, identifies file descriptors carrying UDP network traffic (DNS excluded) and dumps the content of each FD into a different file in the " .. OUTPUT_DIR_NAME .. " directory. Files are named after the UDP tuple they contain."; +short_description = "extract data from UDP streams to files."; +category = "I/O"; + +args = {} + +files = {} + +function mkdir(dirname) + os.execute('mkdir ' .. dirname .. " 2> /dev/null") + os.execute('md ' .. dirname .. " 2> nul") +end + +function on_init() + fbuf = chisel.request_field("evt.rawarg.data") + fres = chisel.request_field("evt.rawarg.res") + ffdname = chisel.request_field("fd.name") + ffdtype = chisel.request_field("fd.type") + + mkdir(OUTPUT_DIR_NAME) + + sysdig.set_snaplen(16384) + chisel.set_filter("evt.dir=< and evt.rawres>=0 and fd.l4proto=udp and evt.is_io=true") + return true +end + +function on_capture_start() + if sysdig.is_live() then + print("live capture not supported") + return false + end + return true +end + +function on_event() + local buf = evt.field(fbuf) + local etype = evt.get_type() + local res = evt.field(fres) + local fdname = evt.field(ffdname) + local fdtype = evt.field(fdtype) + local containername = evt.field(fcontainername) + local is_io_read = evt.field(fis_io_read) + local is_io_write = evt.field(fis_io_write) + + if not files[fdname] then + file_name = OUTPUT_DIR_NAME .. "/" .. fdname + file_name = string.gsub(file_name, ":", "_") + file_name = string.gsub(file_name, ">", "-") + files[fdname] = io.open(file_name, "w") + end + + files[fdname]:write(buf) + + return true +end diff --git a/userspace/sysdig/chisels/v_backlog.lua b/userspace/sysdig/chisels/v_backlog.lua new file mode 100644 index 0000000000..f1606dcb75 --- /dev/null +++ b/userspace/sysdig/chisels/v_backlog.lua @@ -0,0 +1,80 @@ +--[[ + +Copyright (C) 2015 Donatas Abraitis. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "backlog", + name = "Socket Queues", + description = "This will show queues (backlog) utilization per process. This is useful if you have no clue what's going to with your system with heavy workload on sockets. It would help you to troubleshoot current listen() backlog, maximum backlog, which is configured by application", + tags = {"Default"}, + view_type = "table", + applies_to = {"", "proc.pid", "proc.name", "fd.sport", "fd.sproto"}, + filter = "evt.type=accept", + is_root = false, + use_defaults = true, + drilldown_target = "connections", + columns = + { + { + name = "NA", + field = "fd.sport", + is_key = true + }, + { + name = "PID", + field = "proc.pid", + description = "Process ID", + colsize = 15 + }, + { + name = "PORT", + field = "fd.sport", + description = "Server port", + colsize = 15 + }, + { + name = "BACKLOG", + field = "evt.arg[3]", + description = "Current backlog size", + colsize = 15, + is_sorting = true, + aggregation = "AVG" + }, + { + name = "BACKLOG_PCT", + field = "evt.arg[2]", + description = "Current backlog size in percentage", + colsize = 15, + aggregation = "AVG" + }, + { + name = "BACKLOG_MAX", + field = "evt.arg[4]", + description = "Max backlog size", + colsize = 15 + }, + { + name = "PROC", + field = "proc.name", + description = "Process name", + colsize = 50 + }, + } +} diff --git a/userspace/sysdig/chisels/v_connections.lua b/userspace/sysdig/chisels/v_connections.lua new file mode 100644 index 0000000000..6de7a249f4 --- /dev/null +++ b/userspace/sysdig/chisels/v_connections.lua @@ -0,0 +1,147 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "connections", + name = "Connections", + description = "Top network connections. This view lists all of the network connections that were active during the last sampling interval, with details for each of them.", + tips = {"This view can be applied not only to the whole machine, but also to single processes, containers, threads and so on. Use it after a drill down for more fine grained investigation."}, + tags = {"Default", "wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "fd.dport", "fd.dproto", "fd.port", "fd.proto", "fd.lport", "fd.rport", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "fd.type=ipv4 or fd.type=ipv6 and fd.name!=''", + use_defaults = true, + drilldown_target = "incoming_connections", + columns = + { + { + tags = {"default"}, + name = "NA", + field = "fd.name", + is_key = true + }, + { + tags = {"containers"}, + name = "NA", + field = "fd.containername", + is_key = true + }, + { + name = "L4PROTO", + description = "The connection transport protocol (TCP, UDP, etc.).", + field = "fd.l4proto", + colsize = 8, + }, + { + name = "LIP", + description = "Local IP Address.", + field = "fd.lip", + colsize = 17, + }, + { + name = "LPORT", + description = "Local Port.", + field = "fd.lport", + colsize = 8, + }, + { + name = "RIP", + description = "Remote IP Address.", + field = "fd.rip", + colsize = 17, + }, + { + name = "RPORT", + description = "Remote Port.", + field = "fd.rport", + colsize = 8, + }, + { + is_sorting = true, + name = "BPS IN", + field = "evt.buflen.net.in", + description = "This connection's input bandwidth in bytes per second.", + colsize = 12, + aggregation = "TIME_AVG" + }, + { + name = "BPS OUT", + field = "evt.buflen.net.out", + description = "This connection's output bandwidth in bytes per second.", + colsize = 12, + aggregation = "TIME_AVG" + }, + { + name = "IOPS", + field = "evt.count", + description = "Total (read+write) number of calls per second made on this connection by the owning process.", + colsize = 12, + aggregation = "TIME_AVG" + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + description = "The full command line of the process owning the connection's socket.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + }, + actions = + { + { + hotkey = "c", + command = "tcpdump -niany host %fd.lip and host %fd.rip and port %fd.lport and port %fd.rport", + description = "tcpdump connection", + }, + { + hotkey = "l", + command = "tcpdump -niany host %fd.lip", + description = "tcpdump local IP", + }, + { + hotkey = "n", + command = "nslookup %fd.rip", + description = "nslookup remote IP", + }, + { + hotkey = "P", + command = "ping %fd.rip", + description = "ping remote IP", + wait_finish = false + }, + { + hotkey = "r", + command = "tcpdump -niany host %fd.rip", + description = "tcpdump remote IP", + }, + { + hotkey = "t", + command = "traceroute %fd.rip", + description = "traceroute remote IP", + }, + }, +} diff --git a/userspace/sysdig/chisels/v_containers.lua b/userspace/sysdig/chisels/v_containers.lua new file mode 100644 index 0000000000..eba61293d1 --- /dev/null +++ b/userspace/sysdig/chisels/v_containers.lua @@ -0,0 +1,186 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "containers", + name = "Containers", + description = "List all the containers running on this machine, and the resources that each of them uses.", + tips = {"Select a container and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected container."}, + tags = {"wsysdig"}, + view_type = "table", + applies_to = {"", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "container.name != host", + use_defaults = true, + drilldown_target = "procs", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the container.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "PROCS", + field = "evt.count.procinfo", + description = "Number of processes currently running inside the container.", + aggregation = "AVG", + groupby_aggregation = "SUM", + colsize = 8, + }, + { + name = "THREADS", + field = "evt.count.threadinfo", + description = "Number of threads currently running inside the container.", + aggregation = "AVG", + groupby_aggregation = "SUM", + colsize = 8 + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the process.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the process.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the container, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the container, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "container.id", + is_groupby_key = true + }, + { + name = "ENGINE", + field = "container.type", + description = "Container type.", + colsize = 8 + }, + { + name = "IMAGE", + field = "container.image", + description = "Container image name.", + colsize = 30 + }, + { + name = "ID", + field = "container.id", + description = "Container ID. The format of this column depends on the containerization technology. For example, Docker ID are 12 characters hexadecimal digit strings.", + colsize = 13 + }, + { + name = "NAME", + field = "container.name", + description = "Name of the container.", + colsize = 0 + }, + }, + actions = + { + { + hotkey = "a", + command = "docker attach %container.id", + description = "docker attach" + }, + { + hotkey = "b", + command = "docker exec -i -t %container.id /bin/bash", + description = "bash shell", + wait_finish = false + }, + { + hotkey = "f", + command = "docker logs -f %container.id", + description = "follow logs" + }, + { + hotkey = "h", + command = "docker history %container.image", + description = "image history" + }, + { + hotkey = "i", + command = "docker inspect %container.id", + description = "docker inspect" + }, + { + hotkey = "k", + command = "docker kill %container.id", + description = "docker kill", + ask_confirmation = true + }, + { + hotkey = "l", + command = "docker logs %container.id", + description = "docker logs" + }, + { + hotkey = "s", + command = "docker stop %container.id", + description = "docker stop" + }, + { + hotkey = "z", + command = "docker pause %container.id", + description = "docker pause" + }, + { + hotkey = "u", + command = "docker unpause %container.id", + description = "docker unpause" + }, + { + hotkey = "w", + command = "docker wait %container.id", + description = "docker wait" + }, + }, +} diff --git a/userspace/sysdig/chisels/v_containers_errors.lua b/userspace/sysdig/chisels/v_containers_errors.lua new file mode 100644 index 0000000000..53107a0cab --- /dev/null +++ b/userspace/sysdig/chisels/v_containers_errors.lua @@ -0,0 +1,158 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "containers_errors", + name = "Containers Errors", + description = "This view shows system error counters for each container running on the machine. Errors are grouped into 4 categories: file I/O, network I/O, memory allocation and 'other'.", + tips = { + "If you click 'enter' on a selection in this chart, you will be able to see the specific errors that the container is generating.", + "Digging into a container by clicking on F6 will let you explore the system calls for that specific container and see the full details about what's causing the errors." + }, + tags = {"Default"}, + view_type = "table", + applies_to = {"", "container.id", "fd.name", "fd.containername", "fd.sport", "fd.sproto", "evt.type", "fd.directory", "fd.containerdirectory", "fd.containerdirectory", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + drilldown_target = "errors", + filter = "container.name != host", + use_defaults = true, + columns = + { + { + name = "NA", + field = "proc.pid", + is_key = true + }, + { + name = "ID", + field = "container.id", + is_groupby_key = true + }, + { + name = "FILE", + field = "evt.count.error.file", + description = "Number of file I/O errors generated in the container during the sample interval. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.count.error.net", + description = "Number of network I/O errors generated in the container during the sample interval. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM", + groupby_aggregation = "SUM" + }, + { + name = "MEMORY", + field = "evt.count.error.memory", + description = "Number of memory allocation/release related errors generated in the container during the sample interval. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM", + groupby_aggregation = "SUM" + }, + { + name = "OTHER", + field = "evt.count.error.other", + description = "Number of errors generated in the container that don't fall in any of the previous categories. E.g. signal or event related errors. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM", + groupby_aggregation = "SUM" + }, + { + name = "ENGINE", + field = "container.type", + description = "Container type.", + colsize = 8 + }, + { + name = "ID", + field = "container.id", + description = "Container ID. The format of this column depends on the containerization technology. For example, Docker ID are 12 characters hexadecimal digit strings.", + colsize = 13 + }, + { + name = "CONTAINER", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 0 + } + }, + actions = + { + { + hotkey = "a", + command = "docker attach %container.id", + description = "docker attach" + }, + { + hotkey = "b", + command = "docker exec -i -t %container.id /bin/bash", + description = "bash shell", + wait_finish = false + }, + { + hotkey = "f", + command = "docker logs -f %container.id", + description = "follow logs" + }, + { + hotkey = "h", + command = "docker history %container.image", + description = "image history" + }, + { + hotkey = "i", + command = "docker inspect %container.id", + description = "docker inspect" + }, + { + hotkey = "k", + command = "docker kill %container.id", + description = "docker kill", + ask_confirmation = true + }, + { + hotkey = "l", + command = "docker logs %container.id", + description = "docker logs" + }, + { + hotkey = "s", + command = "docker stats %container.id", + description = "docker stop" + }, + { + hotkey = "z", + command = "docker pause %container.id", + description = "docker pause" + }, + { + hotkey = "u", + command = "docker unpause %container.id", + description = "docker unpause" + }, + { + hotkey = "w", + command = "docker wait %container.id", + description = "docker wait" + }, + }, +} diff --git a/userspace/sysdig/chisels/v_cpus.lua b/userspace/sysdig/chisels/v_cpus.lua new file mode 100644 index 0000000000..a791598d18 --- /dev/null +++ b/userspace/sysdig/chisels/v_cpus.lua @@ -0,0 +1,67 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_infoz = +{ + id = "cores", + name = "CPUs", + description = "This is the typical top/htop process list, showing usage of resources like CPU, memory, disk and network on a by process basis.", + tags = {"Default"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + is_root = true, + use_defaults = true, + drilldown_target = "procs", + columns = + { + { + name = "NA", + field = "proc.pid", + is_key = true + }, + { + name = "NA", + field = "evt.cpu", + is_groupby_key = true + }, + { + name = "CORE", + description = "CPU or Core ID.", + field = "evt.cpu", + colsize = 8, + }, + { + name = "CPU", + field = "proc.cpu", + description = "CPU usage.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "TH", + field = "proc.nthreads", + description = "Number of threads that the process contains.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 5 + } + } +} diff --git a/userspace/sysdig/chisels/v_directories.lua b/userspace/sysdig/chisels/v_directories.lua new file mode 100644 index 0000000000..89993cadbb --- /dev/null +++ b/userspace/sysdig/chisels/v_directories.lua @@ -0,0 +1,103 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "directories", + name = "Directories", + description = "This view lists the directories that were accessed on the file system. The list can be sorted by metrics like the input/output bytes and the IOPS", + tips = {"This view can be applied not only to the whole machine, but also to single processes, containers, threads and so on. Use it after a drill down for more fine grained investigation."}, + tags = {"Default", "wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "fd.type=file or fd.type=directory and fd.name!=''", + use_defaults = true, + drilldown_target = "files", + columns = + { + { + tags = {"default"}, + name = "NA", + field = "fd.directory", + is_key = true + }, + { + tags = {"containers"}, + name = "NA", + field = "fd.containerdirectory", + is_key = true + }, + { + name = "BYTES IN", + field = "evt.buflen.file.in", + description = "Amount of bytes read from the file. For live captures, this is the amount during the last sampling interval. For trace files, this is the total amount for the full file.", + colsize = 12, + aggregation = "SUM" + }, + { + name = "BYTES OUT", + field = "evt.buflen.file.out", + description = "amount of bytes written to the file. For live captures, this is the amount during the last sampling interval. For trace files, this is the total amount for the full file.", + colsize = 12, + aggregation = "SUM" + }, + { + is_sorting = true, + name = "OPS", + field = "evt.count.exit", + description = "Number of I/O operations on the file. This counts all the operations on the file, including, open, close, read, write, stat, and so on. As a consequence, this value can be nonzero even if I/O bytes for the file are zero.", + colsize = 9, + aggregation = "SUM" + }, + { + name = "OPENS", + field = "evt.type.is.3", + description = "Number times the file has been opened.", + colsize = 9, + aggregation = "SUM" + }, + { + name = "ERRORS", + field = "evt.count.error", + description = "Number I/O errors that happened on this file.", + colsize = 9, + aggregation = "SUM" + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + colsize = 20 + }, + { + name = "DIRNAME", + description = "The full directory path name.", + field = "fd.directory", + colsize = 0 + } + }; + actions = + { + { + hotkey = "l", + command = "ls -al %fd.directory", + description = "ls directory" + }, + }, +} diff --git a/userspace/sysdig/chisels/v_docker_events.lua b/userspace/sysdig/chisels/v_docker_events.lua new file mode 100644 index 0000000000..06131831c7 --- /dev/null +++ b/userspace/sysdig/chisels/v_docker_events.lua @@ -0,0 +1,63 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "docker_events", + name = "Docker Events", + description = "Lists all the events generated by docker activity, for example container start, stop, kill, etc.", + tags = {"Default", "nocsysdig"}, + view_type = "list", + applies_to = {""}, + filter = "evt.type=infra", + use_defaults = true, + columns = + { + { + name = "TIME", + field = "evt.time", + description = "Time when the event happened.", + colsize = 18, + }, + { + name = "EVENT TYPE", + field = "evt.arg.name", + description = "Type of docker event.", + colsize = 20, + }, + { + name = "CONTAINER ID", + field = "evt.infra.docker.container.id", + description = "ID of the container that generated this event.", + colsize = 20, + }, + { + name = "CONTAINER IMAGE", + field = "evt.infra.docker.container.image", + description = "Image name of the container that generated this event.", + colsize = 20, + }, + { + name = "CONTAINER NAME", + field = "evt.infra.docker.container.name", + description = "Name of the container that generated this event.", + colsize = 0, + }, + } +} diff --git a/userspace/sysdig/chisels/v_errors.lua b/userspace/sysdig/chisels/v_errors.lua new file mode 100644 index 0000000000..0184f7845f --- /dev/null +++ b/userspace/sysdig/chisels/v_errors.lua @@ -0,0 +1,57 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "errors", + name = "Errors", + description = "This view shows the top system call errors, sorted by number of occurrences. Errors are shows as errno codes. Do a 'man errno' to find the meaning of the most important codes.", + tips = { + "This view can be applied not only to the whole machine, but also to single processes, containers, threads and so on. Use it after a drill down for more fine grained investigation.", + "Drill down on an error by clicking enter to see which processes are generating it." + }, + tags = {"Default", "wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "fd.directory", "fd.containerdirectory", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "evt.res != SUCCESS", + use_defaults = true, + drilldown_target = "procs_errors", + columns = + { + { + name = "NA", + field = "evt.res", + is_key = true + }, + { + name = "COUNT", + field = "evt.count", + description = "The number of times the error happened during the sample interval. On trace files, this is the total for the whole file.", + colsize = 12, + aggregation = "SUM", + is_sorting = true, + }, + { + name = "ERROR", + description = "The error 'errno' code. Do a 'man errno' to find the meaning of the most important codes.", + field = "evt.res", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_file_opens.lua b/userspace/sysdig/chisels/v_file_opens.lua new file mode 100644 index 0000000000..0f57ff49a5 --- /dev/null +++ b/userspace/sysdig/chisels/v_file_opens.lua @@ -0,0 +1,74 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "file_opens", + name = "File Opens List", + description = "List file name and process for of every single file open.", + tips = {"The RES column is very useful to identify failed opens. Successful opens will show 'SUCCESS' in this column, while failed opens will show an errno code. Do a 'man errno' to find the meaning of the most important codes. And remember that you can sort the opens based on this code, or filter for specific codes using the F4 key."}, + tags = {"Default"}, + view_type = "list", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "fd.name", "fd.containername", "fd.directory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "evt.type=open and evt.dir=<", + columns = + { + { + name = "TIME", + field = "evt.time", + description = "The timestamp of the file open.", + colsize = 19, + }, + { + name = "RES", + field = "evt.res", + description = "The result of the open call. This can be either 'SUCCESS', or an errno code.", + colsize = 8, + }, + { + name = "FILE", + field = "fd.name", + description = "The file name.", + colsize = 40, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + field = "proc.exeline", + aggregation = "MAX", + description = "The program that opened the file, including its arguments.", + colsize = 0, + } + }, + actions = + { + { + hotkey = "l", + command = "less %fd.name", + description = "less file", + wait_finish = false + }, + }, +} diff --git a/userspace/sysdig/chisels/v_files.lua b/userspace/sysdig/chisels/v_files.lua new file mode 100644 index 0000000000..d56f45d878 --- /dev/null +++ b/userspace/sysdig/chisels/v_files.lua @@ -0,0 +1,104 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "files", + name = "Files", + description = "This view lists the files that were accessed on the file system. The list can be sorted by metrics like the input/output bytes and the IOPS", + tips = {"This view can be applied not only to the whole machine, but also to single processes, containers, threads and so on. Use it after a drill down for more fine grained investigation."}, + tags = {"Default", "wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "fd.directory", "fd.containerdirectory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "fd.type=file or fd.type=directory and fd.name!=''", + use_defaults = true, + drilldown_target = "procs", + columns = + { + { + tags = {"default"}, + name = "NA", + field = "fd.name", + is_key = true + }, + { + tags = {"containers"}, + name = "NA", + field = "fd.containername", + is_key = true + }, + { + name = "BYTES IN", + field = "evt.buflen.file.in", + description = "Amount of bytes read from the file. For live captures, this is the amount during the last sampling interval. For trace files, this is the total amount for the full file.", + colsize = 12, + aggregation = "SUM" + }, + { + name = "BYTES OUT", + field = "evt.buflen.file.out", + description = "amount of bytes written to the file. For live captures, this is the amount during the last sampling interval. For trace files, this is the total amount for the full file.", + colsize = 12, + aggregation = "SUM" + }, + { + is_sorting = true, + name = "OPS", + field = "evt.count.exit", + description = "Number of I/O operations on the file. This counts all the operations on the file, including, open, close, read, write, stat, and so on. As a consequence, this value can be nonzero even if I/O bytes for the file are zero.", + colsize = 9, + aggregation = "SUM" + }, + { + name = "OPENS", + field = "evt.type.is.3", + description = "Number times the file has been opened during the sample interval. On trace files, this is the total for the whole file.", + colsize = 9, + aggregation = "SUM" + }, + { + name = "ERRORS", + field = "evt.count.error", + description = "Number I/O errors that happened on this file during the sample interval. On trace files, this is the total for the whole file.", + colsize = 9, + aggregation = "SUM" + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + colsize = 20 + }, + { + name = "FILENAME", + description = "The file name including its full path.", + field = "fd.name", + colsize = 0 + } + }, + actions = + { + { + hotkey = "l", + command = "less %fd.name", + description = "less file", + wait_finish = false + }, + }, +} diff --git a/userspace/sysdig/chisels/v_incoming_connections.lua b/userspace/sysdig/chisels/v_incoming_connections.lua new file mode 100644 index 0000000000..cdd12e09dc --- /dev/null +++ b/userspace/sysdig/chisels/v_incoming_connections.lua @@ -0,0 +1,58 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "incoming_connections", + name = "New Connections", + description = "List every newly established network connection.", + tags = {"Default"}, + view_type = "list", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "thread.tid", "proc.name", "fd.name", "fd.containername", "fd.sport", "fd.sproto", "fd.dport", "fd.port", "fd.lport", "fd.rport", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "evt.type=accept and evt.dir=< and evt.failed=false", + columns = + { + { + name = "TIME", + field = "evt.time", + description = "Time when the connection was received by this machine.", + colsize = 19, + }, + { + name = "Connection", + field = "fd.name", + description = "Connection tuple details.", + colsize = 40, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. This field depends on the containerization technology. For docker this is the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + field = "proc.exeline", + aggregation = "MAX", + description = "Name and arguments of the process that received the connection.", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_io_by_type.lua b/userspace/sysdig/chisels/v_io_by_type.lua new file mode 100644 index 0000000000..9ee5b8951f --- /dev/null +++ b/userspace/sysdig/chisels/v_io_by_type.lua @@ -0,0 +1,75 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "io_by_type", + name = "I/O by Type", + description = "Show an overview of the I/O volume based on I/O type. Possible I/O types are: file, directory, ipv4 or ipv6 network traffic, pipe, unix socket, signal fd, event fd, inotify fd.", + tips = {"This view is a good starting point to understand what a machine is doing besides CPU computation. Remember that you can apply it to a process or to a container as well, to get an overview of what they are doing."}, + tags = {"Default", "wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + use_defaults = true, + drilldown_target = "procs", + columns = + { + { + name = "NA", + field = "fd.type", + is_key = true + }, + { + name = "BPS IN", + field = "evt.buflen.in", + description = "Bytes per second read from the FDs of the specific type.", + colsize = 12, + aggregation = "TIME_AVG" + }, + { + name = "BPS OUT", + field = "evt.buflen.out", + description = "Bytes per second written to the FDs of the specific type.", + colsize = 12, + aggregation = "TIME_AVG" + }, + { + is_sorting = true, + name = "IOPS", + field = "evt.count", + description = "Number of I/O operations for the specified I/O category. This counts all the operations on the file, including, open, close, read, write, stat, and so on. As a consequence, this value can be nonzero even if I/O bytes for the file are zero.", + colsize = 9, + aggregation = "TIME_AVG" + }, + { + name = "TIME", + field = "evt.latency", + description = "Time spent by processes doing any I/O operation (including wait) of this type.", + colsize = 9, + aggregation = "SUM" + }, + { + name = "I/O Type", + field = "fd.type", + description = "Type of I/O. Can be one of: file, directory, ipv4, ipv6, pipe, unix, signal, event, inotify", + aggregation = "SUM", + colsize = 0 + }, + } +} diff --git a/userspace/sysdig/chisels/v_kubernetes_controllers.lua b/userspace/sysdig/chisels/v_kubernetes_controllers.lua new file mode 100644 index 0000000000..d140a7b0be --- /dev/null +++ b/userspace/sysdig/chisels/v_kubernetes_controllers.lua @@ -0,0 +1,122 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "kubernetes_controllers", + name = "K8s Controllers", + description = "List all Kubernetes controllers running on this machine, and the resources that each of them uses.", + tips = {"Select a controller and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected controller."}, + view_type = "table", + applies_to = {"", "evt.res", "k8s.pod.id", "k8s.svc.id", "k8s.ns.id"}, + filter = "k8s.rc.id != ''", + use_defaults = true, + drilldown_target = "kubernetes_pods", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the controller.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the controller.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the controller.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the controller, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the controller, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "k8s.rc.id", + is_groupby_key = true + }, + { + name = "ID", + field = "k8s.rc.id", + description = "Controller id.", + colsize = 38 + }, + { + name = "NAME", + field = "k8s.rc.name", + description = "Controller name.", + colsize = 25 + }, + { + name = "NAMESPACE", + field = "k8s.ns.name", + description = "Controller namespace.", + colsize = 20 + }, + { + name = "LABELS", + field = "k8s.rc.labels", + description = "Controller labels.", + colsize = 0 + }, + }, + actions = + { + { + hotkey = "d", + command = "kubectl --namespace=%k8s.ns.name describe rc %k8s.rc.name", + description = "describe" + }, + { + hotkey = "x", + command = "kubectl --namespace=%k8s.ns.name delete rc %k8s.rc.name", + description = "delete" + } + } +} diff --git a/userspace/sysdig/chisels/v_kubernetes_deployments.lua b/userspace/sysdig/chisels/v_kubernetes_deployments.lua new file mode 100644 index 0000000000..a37204402c --- /dev/null +++ b/userspace/sysdig/chisels/v_kubernetes_deployments.lua @@ -0,0 +1,122 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "kubernetes_deployments", + name = "K8s Deployments", + description = "List all Kubernetes deployments running on this machine, and the resources that each of them uses.", + tips = {"Select a deployment and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected deployment."}, + view_type = "table", + applies_to = {"", "evt.res", "k8s.pod.id", "k8s.svc.id", "k8s.ns.id"}, + filter = "k8s.deployment.id != ''", + use_defaults = true, + drilldown_target = "kubernetes_pods", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the deployment.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the deployment.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the deployment.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the deployment, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the deployment, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "k8s.deployment.id", + is_groupby_key = true + }, + { + name = "ID", + field = "k8s.deployment.id", + description = "Deployment id.", + colsize = 38 + }, + { + name = "NAME", + field = "k8s.deployment.name", + description = "Deployment name.", + colsize = 25 + }, + { + name = "NAMESPACE", + field = "k8s.ns.name", + description = "Deployment namespace.", + colsize = 20 + }, + { + name = "LABELS", + field = "k8s.deployment.labels", + description = "Deployment labels.", + colsize = 0 + }, + }, + actions = + { + { + hotkey = "d", + command = "kubectl --namespace=%k8s.ns.name describe deployment %k8s.deployment.name", + description = "describe" + }, + { + hotkey = "x", + command = "kubectl --namespace=%k8s.ns.name delete deployment %k8s.deployment.name", + description = "delete" + } + } +} diff --git a/userspace/sysdig/chisels/v_kubernetes_namespaces.lua b/userspace/sysdig/chisels/v_kubernetes_namespaces.lua new file mode 100644 index 0000000000..0fae3ef0cb --- /dev/null +++ b/userspace/sysdig/chisels/v_kubernetes_namespaces.lua @@ -0,0 +1,116 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "kubernetes_namespaces", + name = "K8s Namespaces", + description = "List all Kubernetes namespaces running on this machine, and the resources that each of them uses.", + tips = {"Select a namespace and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected namespace."}, + view_type = "table", + applies_to = {"", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id"}, + filter = "k8s.ns.id != ''", + use_defaults = true, + drilldown_target = "kubernetes_pods", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the namespace.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the namespace.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the namespace.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the namespace, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the namespace, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "k8s.ns.id", + is_groupby_key = true + }, + { + name = "ID", + field = "k8s.ns.id", + description = "Namespace id.", + colsize = 38 + }, + { + name = "NAME", + field = "k8s.ns.name", + description = "Namespace name.", + colsize = 25 + }, + { + name = "LABELS", + field = "k8s.ns.labels", + description = "Namespace labels.", + colsize = 0 + }, + }, + actions = + { + { + hotkey = "d", + command = "kubectl --namespace=%k8s.ns.name describe namespaces %k8s.ns.name", + description = "describe" + }, + { + hotkey = "x", + command = "kubectl --namespace=%k8s.ns.name delete namespaces %k8s.ns.name", + description = "delete" + } + } +} diff --git a/userspace/sysdig/chisels/v_kubernetes_pods.lua b/userspace/sysdig/chisels/v_kubernetes_pods.lua new file mode 100644 index 0000000000..aa8d58340f --- /dev/null +++ b/userspace/sysdig/chisels/v_kubernetes_pods.lua @@ -0,0 +1,142 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "kubernetes_pods", + name = "K8s Pods", + description = "List all Kubernetes pods running on this machine, and the resources that each of them uses.", + tips = {"Select a pod and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected pod."}, + view_type = "table", + applies_to = {"", "evt.res", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id"}, + filter = "k8s.pod.id != ''", + use_defaults = true, + drilldown_target = "containers", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the pod.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the pod.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the pod.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the pod, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the pod, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "k8s.pod.id", + is_groupby_key = true + }, + { + name = "ID", + field = "k8s.pod.id", + description = "Pod name.", + colsize = 38 + }, + { + name = "NAME", + field = "k8s.pod.name", + description = "Pod name.", + colsize = 25 + }, + { + name = "NAMESPACE", + field = "k8s.ns.name", + description = "Pod namespace.", + colsize = 20 + }, + { + name = "LABELS", + field = "k8s.pod.labels", + description = "Pod labels.", + colsize = 0 + }, + }, + actions = + { + { + hotkey = "d", + command = "kubectl --namespace=%k8s.ns.name describe pods %k8s.pod.name", + description = "describe" + }, + { + hotkey = "x", + command = "kubectl --namespace=%k8s.ns.name delete pods %k8s.pod.name", + description = "delete" + }, + { + hotkey = "b", + command = "kubectl --namespace=%k8s.ns.name exec %k8s.pod.name -i -t -- /bin/bash", + description = "bash shell" + }, + { + hotkey = "l", + command = "kubectl --namespace=%k8s.ns.name logs %k8s.pod.name", + description = "log" + }, + { + hotkey = "f", + command = "kubectl --namespace=%k8s.ns.name logs -f %k8s.pod.name", + description = "follow log" + }, + { + hotkey = "o", + command = "kubectl --namespace=%k8s.ns.name logs -p %k8s.pod.name", + description = "previous log" + } + } +} diff --git a/userspace/sysdig/chisels/v_kubernetes_replicasets.lua b/userspace/sysdig/chisels/v_kubernetes_replicasets.lua new file mode 100644 index 0000000000..41eb42df25 --- /dev/null +++ b/userspace/sysdig/chisels/v_kubernetes_replicasets.lua @@ -0,0 +1,122 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "kubernetes_replicasets", + name = "K8s ReplicaSets", + description = "List all Kubernetes replica sets running on this machine, and the resources that each of them uses.", + tips = {"Select a replica set and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected controller."}, + view_type = "table", + applies_to = {"", "evt.res", "k8s.pod.id", "k8s.svc.id", "k8s.ns.id"}, + filter = "k8s.rs.id != ''", + use_defaults = true, + drilldown_target = "kubernetes_pods", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the replica set.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the replica set.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the replica set.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the replica set, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the replica set, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "k8s.rs.id", + is_groupby_key = true + }, + { + name = "ID", + field = "k8s.rs.id", + description = "Controller id.", + colsize = 38 + }, + { + name = "NAME", + field = "k8s.rs.name", + description = "Controller name.", + colsize = 25 + }, + { + name = "NAMESPACE", + field = "k8s.ns.name", + description = "Controller namespace.", + colsize = 20 + }, + { + name = "LABELS", + field = "k8s.rs.labels", + description = "Controller labels.", + colsize = 0 + }, + }, + actions = + { + { + hotkey = "d", + command = "kubectl --namespace=%k8s.ns.name describe rs %k8s.rs.name", + description = "describe" + }, + { + hotkey = "x", + command = "kubectl --namespace=%k8s.ns.name delete rs %k8s.rs.name", + description = "delete" + } + } +} diff --git a/userspace/sysdig/chisels/v_kubernetes_services.lua b/userspace/sysdig/chisels/v_kubernetes_services.lua new file mode 100644 index 0000000000..568e94be3c --- /dev/null +++ b/userspace/sysdig/chisels/v_kubernetes_services.lua @@ -0,0 +1,122 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "kubernetes_services", + name = "K8s Services", + description = "List all Kubernetes services running on this machine, and the resources that each of them uses.", + tips = {"Select a service and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected service."}, + view_type = "table", + applies_to = {"", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.ns.id"}, + filter = "k8s.svc.id != ''", + use_defaults = true, + drilldown_target = "kubernetes_pods", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the service.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the service.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the service.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the service, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the service, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "k8s.svc.id", + is_groupby_key = true + }, + { + name = "ID", + field = "k8s.svc.id", + description = "Service id.", + colsize = 38 + }, + { + name = "NAME", + field = "k8s.svc.name", + description = "Service name.", + colsize = 25 + }, + { + name = "NAMESPACE", + field = "k8s.ns.name", + description = "Service namespace.", + colsize = 20 + }, + { + name = "LABELS", + field = "k8s.svc.labels", + description = "Service labels.", + colsize = 0 + }, + }, + actions = + { + { + hotkey = "d", + command = "kubectl --namespace=%k8s.ns.name describe services/%k8s.svc.name", + description = "describe" + }, + { + hotkey = "x", + command = "kubectl --namespace=%k8s.ns.name delete services %k8s.svc.name", + description = "delete" + } + } +} diff --git a/userspace/sysdig/chisels/v_marathon_apps.lua b/userspace/sysdig/chisels/v_marathon_apps.lua new file mode 100644 index 0000000000..05c9914998 --- /dev/null +++ b/userspace/sysdig/chisels/v_marathon_apps.lua @@ -0,0 +1,97 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "marathon_apps", + name = "Marathon Apps", + description = "List all Marathon apps running on this machine, and the resources that each of them uses.", + tips = {"Select an app and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected app."}, + view_type = "table", + applies_to = {"", "evt.res", "marathon.group.id"}, + filter = "marathon.app.id != ''", + use_defaults = true, + drilldown_target = "mesos_tasks", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the app.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the app.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the app.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the app, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the app, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "marathon.app.id", + is_groupby_key = true + }, + { + name = "ID", + field = "marathon.app.id", + description = "App name.", + colsize = 38 + }, + { + name = "LABELS", + field = "marathon.app.labels", + description = "App labels.", + colsize = 0 + }, + } +} diff --git a/userspace/sysdig/chisels/v_marathon_groups.lua b/userspace/sysdig/chisels/v_marathon_groups.lua new file mode 100644 index 0000000000..43cee5e64a --- /dev/null +++ b/userspace/sysdig/chisels/v_marathon_groups.lua @@ -0,0 +1,91 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "marathon_groups", + name = "Marathon Groups", + description = "List all Marathon Groups running on this machine, and the resources that each of them uses.", + tips = {"Select a group and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected group."}, + view_type = "table", + applies_to = {"", "evt.res", "marathon.app.id"}, + filter = "marathon.group.id != ''", + use_defaults = true, + drilldown_target = "marathon_apps", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the framework.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the framework.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the framework.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the framework, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the framework, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "marathon.group.name", + is_groupby_key = true + }, + { + name = "ID", + field = "marathon.group.id", + description = "Group id.", + colsize = 38 + } + } +} diff --git a/userspace/sysdig/chisels/v_mesos_frameworks.lua b/userspace/sysdig/chisels/v_mesos_frameworks.lua new file mode 100644 index 0000000000..6326f8af90 --- /dev/null +++ b/userspace/sysdig/chisels/v_mesos_frameworks.lua @@ -0,0 +1,97 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "mesos_frameworks", + name = "Mesos Frameworks", + description = "List all Mesos frameworks running on this machine, and the resources that each of them uses.", + tips = {"Select a framework and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected framework."}, + view_type = "table", + applies_to = {"", "evt.res", "mesos.task.id"}, + filter = "mesos.framework.id != ''", + use_defaults = true, + drilldown_target = "mesos_tasks", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the framework.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the framework.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the framework.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the framework, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the framework, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "mesos.framework.name", + is_groupby_key = true + }, + { + name = "ID", + field = "mesos.framework.id", + description = "Framework ID.", + colsize = 38 + }, + { + name = "NAME", + field = "mesos.framework.name", + description = "Framework name.", + colsize = 25 + } + } +} diff --git a/userspace/sysdig/chisels/v_mesos_tasks.lua b/userspace/sysdig/chisels/v_mesos_tasks.lua new file mode 100644 index 0000000000..f6d6a94a95 --- /dev/null +++ b/userspace/sysdig/chisels/v_mesos_tasks.lua @@ -0,0 +1,103 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "mesos_tasks", + name = "Mesos Tasks", + description = "List all Mesos tasks running on this machine, and the resources that each of them uses.", + tips = {"Select a task and click enter to drill down into it. At that point, you will be able to access several views that will show you the details of the selected task."}, + view_type = "table", + applies_to = {"", "evt.res", "mesos.framework.id"}, + filter = "mesos.task.id != ''", + use_defaults = true, + drilldown_target = "containers", + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the task.", + colsize = 8, + aggregation = "AVG", + groupby_aggregation = "SUM", + is_sorting = true + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the task.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the task.", + aggregation = "MAX", + groupby_aggregation = "SUM", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the task, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network bandwidth generated by the task, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG", + groupby_aggregation = "SUM" + }, + { + name = "NA", + field = "mesos.task.id", + is_groupby_key = true + }, + { + name = "ID", + field = "mesos.task.id", + description = "Task name.", + colsize = 38 + }, + { + name = "NAME", + field = "mesos.task.name", + description = "Task name.", + colsize = 25 + }, + { + name = "LABELS", + field = "mesos.task.labels", + description = "Task labels.", + colsize = 0 + }, + } +} diff --git a/userspace/sysdig/chisels/v_notifications.lua b/userspace/sysdig/chisels/v_notifications.lua new file mode 100644 index 0000000000..8c110913cc --- /dev/null +++ b/userspace/sysdig/chisels/v_notifications.lua @@ -0,0 +1,57 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "notifications", + name = "Notifications", + description = "Lists the notification events that indicate the specific point in time when sysdig secure policies have been violated.", + tags = {"nocsysdig"}, + view_type = "list", + applies_to = {""}, + filter = "evt.type=notification", + use_defaults = true, + columns = + { + { + name = "TIME", + field = "evt.time", + description = "Time when the command was executed.", + colsize = 12, + }, + { + name = "ID", + field = "evt.arg.id", + description = "Notification ID. This can be used to locate the notification in the sysdig secure user interface.", + colsize = 24, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + colsize = 20 + }, + { + name = "DESCRIPTION", + field = "evt.arg.desc", + description = "The description of the policy that generated this notification.", + colsize = 0, + } + } +} diff --git a/userspace/sysdig/chisels/v_page_faults.lua b/userspace/sysdig/chisels/v_page_faults.lua new file mode 100644 index 0000000000..3b3168b648 --- /dev/null +++ b/userspace/sysdig/chisels/v_page_faults.lua @@ -0,0 +1,85 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "page_faults", + name = "Page Faults", + description = "This view shows page fault counters for processes. Both minor and major page faults are reported for each process. The counters report the number of page faults since process start.", + tips = { + "Major page faults are typically the ones you really want to keep an eye on. They are the ones causing pages swapping to disk, thus dramatically slowing down process execution.", + "When applying this view on a live system, if the system is well tuned for performance you should see no changes in the first column." + }, + tags = {"Default", "wsysdig"}, + view_type = "table", + filter = "evt.type!=switch", + applies_to = {"", "container.id", "fd.name", "fd.containername", "fd.sport", "fd.sproto", "evt.type", "fd.directory", "fd.containerdirectory", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + drilldown_target = "errors", + use_defaults = true, + columns = + { + { + name = "NA", + field = "proc.pid", + is_key = true + }, + { + name = "MAJOR", + field = "thread.pfmajor", + description = "Number of major page faults that the process generated since its start.", + colsize = 9, + aggregation = "MAX", + is_sorting = true + }, + { + name = "MINOR", + field = "thread.pfminor", + description = "Number of minor page faults that the process generated since its start.", + colsize = 9, + aggregation = "MAX" + }, + { + name = "PID", + description = "Process PID.", + field = "proc.pid", + colsize = 8, + }, + { + tags = {"containers"}, + name = "VPID", + field = "proc.vpid", + description = "PID that the process has inside the container.", + colsize = 8, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + description = "Full command line of the process.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_port_bindings.lua b/userspace/sysdig/chisels/v_port_bindings.lua new file mode 100644 index 0000000000..c8bf76b6d4 --- /dev/null +++ b/userspace/sysdig/chisels/v_port_bindings.lua @@ -0,0 +1,58 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "port_bindings", + name = "Port bindings", + description = "Lists the creation (bind) and removal (close) of listening ports on the system.", + tags = {"default", "wsysdig", "nocsysdig"}, + view_type = "list", + applies_to = {""}, + filter = "(evt.type=bind and evt.dir=< and (fd.type=ipv4 or fd.type=ipv6)) or (evt.type=close and evt.dir=> and fd.typechar=2 and fd.type=ipv4)", + use_defaults = true, + columns = + { + { + name = "TIME", + field = "evt.time", + description = "Time when the action happened.", + colsize = 12, + }, + { + name = "OPERATION", + field = "evt.type", + description = "Action type. Can Be 'bind' (when a new listening port is added) or 'close' (when a listening port is removed).", + colsize = 24, + }, + { + name = "PORT", + field = "fd.sport", + description = "The number of the created/removed port.", + colsize = 24, + }, + { + name = "Command", + description = "The full command line of the process adding/removing the port.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_procs.lua b/userspace/sysdig/chisels/v_procs.lua new file mode 100644 index 0000000000..8e221de42d --- /dev/null +++ b/userspace/sysdig/chisels/v_procs.lua @@ -0,0 +1,187 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "procs", + name = "Processes", + description = "This is the typical top/htop process list, showing usage of resources like CPU, memory, disk and network on a by process basis.", + tips = {"This is a perfect view to start a drill down session. Click enter or double click on a process to dive into it and explore its behavior."}, + tags = {"Default", "wsysdig"}, + view_type = "table", + filter = "evt.type!=switch", + applies_to = {"", "container.id", "fd.name", "fd.containername", "fd.sport", "fd.sproto", "evt.type", "fd.directory", "fd.containerdirectory", "fd.type", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + is_root = true, + drilldown_target = "threads", + use_defaults = true, + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "NA", + field = "proc.pid", + is_groupby_key = true + }, + { + name = "PID", + description = "Process PID.", + field = "proc.pid", + colsize = 7, + }, + { + name = "PPID", + description = "Process Parent PID.", + field = "proc.ppid", + colsize = 7, + }, + { + tags = {"containers"}, + name = "VPID", + field = "proc.vpid", + description = "PID that the process has inside the container.", + colsize = 8, + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the process.", + aggregation = "AVG", + groupby_aggregation = "SUM", + colsize = 8, + is_sorting = true + }, + { + name = "USER", + field = "user.name", + colsize = 12 + }, + { + name = "TH", + field = "proc.nthreads", + description = "Number of threads that the process contains.", + aggregation = "MAX", + groupby_aggregation = "MAX", + colsize = 5 + }, + { + name = "VIRT", + field = "thread.vmsize.b", + description = "Total virtual memory for the process.", + aggregation = "MAX", + groupby_aggregation = "MAX", + colsize = 9 + }, + { + name = "RES", + field = "thread.vmrss.b", + description = "Resident non-swapped memory for the process.", + aggregation = "MAX", + groupby_aggregation = "MAX", + colsize = 9 + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the process, in bytes per second.", + aggregation = "TIME_AVG", + groupby_aggregation = "SUM", + colsize = 8 + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network I/O bandwidth generated by the process, in bytes per second.", + aggregation = "TIME_AVG", + groupby_aggregation = "SUM", + colsize = 8 + }, + { + tags = {"containers"}, + name = "CONTAINER", + field = "container.name", + colsize = 20 + }, + { + name = "Command", + description = "The full command line of the process.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + }, + actions = + { + { + hotkey = "9", + command = "kill -9 %proc.pid", + description = "kill -9", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "c", + command = "gcore %proc.pid", + description = "generate core", + }, + { + hotkey = "g", + command = "gdb -p %proc.pid", + description = "gdb attach", + wait_finish = false + }, + { + hotkey = "k", + command = "kill %proc.pid", + description = "kill", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "l", + command = "ltrace -p %proc.pid", + description = "ltrace", + }, + { + hotkey = "s", + command = "gdb -p %proc.pid --batch --quiet -ex \"thread apply all bt full\" -ex \"quit\"", + description = "print stack", + }, + { + hotkey = "f", + command = "lsof -p %proc.pid", + description = "one-time lsof", + }, + { + hotkey = "[", + command = "renice $(expr $(ps -h -p %proc.pid -o nice) + 1) -p %proc.pid", + description = "increment nice by 1", + wait_finish = false, + }, + { + hotkey = "]", + command = "renice $(expr $(ps -h -p %proc.pid -o nice) - 1) -p %proc.pid", + description = "decrement nice by 1", + wait_finish = false, + }, + }, +} diff --git a/userspace/sysdig/chisels/v_procs_cpu.lua b/userspace/sysdig/chisels/v_procs_cpu.lua new file mode 100644 index 0000000000..0707fbfa60 --- /dev/null +++ b/userspace/sysdig/chisels/v_procs_cpu.lua @@ -0,0 +1,146 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "procs_cpu", + name = "Processes CPU", + description = "Show total versus user versus system CPU usage for every process.", + tips = { + "A high value for both SYS and SYSCALLS likely means that the process is I/O bound. A high value for SYS and a moderate value for SYSCALLS might on the other side indicate a kernel bottleneck. In both cases, drilling down with the 'System Calls' view can help understand what's happening." + }, + tags = {"Default", "wsysdig"}, + view_type = "table", + filter = "evt.type!=switch", + applies_to = {"", "container.id", "fd.name", "fd.containername", "fd.sport", "fd.sproto", "evt.type", "fd.directory", "fd.containerdirectory", "fd.type", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + is_root = true, + drilldown_target = "threads", + use_defaults = true, + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "NA", + field = "proc.pid", + is_groupby_key = true + }, + { + name = "PID", + description = "Process PID.", + field = "proc.pid", + colsize = 8, + }, + { + tags = {"containers"}, + name = "VPID", + field = "proc.vpid", + description = "PID that the process has inside the container.", + colsize = 8, + }, + { + name = "TOT", + field = "thread.cpu", + description = "Total amount of CPU used by the process (user + system).", + aggregation = "AVG", + groupby_aggregation = "SUM", + colsize = 8, + is_sorting = true + }, + { + name = "USER", + field = "thread.cpu.user", + description = "Amount of user CPU used by the process.", + aggregation = "AVG", + groupby_aggregation = "SUM", + colsize = 8, + }, + { + name = "SYS", + field = "thread.cpu.system", + description = "Amount of system CPU used by the process.", + aggregation = "AVG", + groupby_aggregation = "SUM", + colsize = 8, + }, + { + name = "SYSCALLS", + field = "evt.count", + description = "Number of system calls per second made by the process.", + aggregation = "TIME_AVG", + groupby_aggregation = "SUM", + colsize = 9, + }, + { + tags = {"containers"}, + name = "CONTAINER", + description = "The container this process belongs to.", + field = "container.name", + colsize = 20 + }, + { + name = "Command", + description = "The full command line of the process.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + }, + actions = + { + { + hotkey = "9", + command = "kill -9 %proc.pid", + description = "kill -9", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "c", + command = "gcore %proc.pid", + description = "generate core", + }, + { + hotkey = "g", + command = "gdb -p %proc.pid", + description = "gdb attach", + wait_finish = false + }, + { + hotkey = "k", + command = "kill %proc.pid", + description = "kill", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "l", + command = "ltrace -p %proc.pid", + description = "ltrace", + }, + { + hotkey = "s", + command = "gdb -p %proc.pid --batch --quiet -ex \"thread apply all bt full\" -ex \"quit\"", + description = "print stack", + }, + }, +} diff --git a/userspace/sysdig/chisels/v_procs_errors.lua b/userspace/sysdig/chisels/v_procs_errors.lua new file mode 100644 index 0000000000..3db4ba3bad --- /dev/null +++ b/userspace/sysdig/chisels/v_procs_errors.lua @@ -0,0 +1,129 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "procs_errors", + name = "Processes Errors", + description = "This view shows system error counters for processes. Errors are grouped into 4 categories: file I/O, network I/O, memory allocation and 'other'.", + tips = { + "If you click 'enter' on a selection in this chart, you will be able to see the specific errors that the process is generating.", + "Digging into a process by clicking on F6 will let you explore the system calls for that specific process and see the full details about what's causing the errors." + }, + tags = {"Default", "wsysdig"}, + filter = "evt.type!=switch", + view_type = "table", + applies_to = {"", "container.id", "fd.name", "fd.containername", "fd.sport", "fd.sproto", "evt.type", "fd.directory", "fd.containerdirectory", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + drilldown_target = "errors", + use_defaults = true, + columns = + { + { + name = "NA", + field = "proc.pid", + is_key = true + }, + { + name = "FILE", + field = "evt.count.error.file", + description = "Number of file I/O errors generated by the process during the sample interval. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM" + }, + { + name = "NET", + field = "evt.count.error.net", + description = "Number of network I/O errors generated by the process during the sample interval. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM" + }, + { + name = "MEMORY", + field = "evt.count.error.memory", + description = "Number of memory allocation/release related errors generated by the process during the sample interval. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM" + }, + { + name = "OTHER", + field = "evt.count.error.other", + description = "Number of errors generated by the process that don't fall in any of the previous categories. E.g. signal or event related errors. On trace files, this is the total for the whole file.", + colsize = 8, + aggregation = "SUM" + }, + { + name = "PID", + description = "Process PID.", + field = "proc.pid", + colsize = 8, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + description = "Full command line of the process.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + }, + actions = + { + { + hotkey = "9", + command = "kill -9 %proc.pid", + description = "kill -9", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "c", + command = "gcore %proc.pid", + description = "generate core", + }, + { + hotkey = "g", + command = "gdb -p %proc.pid", + description = "gdb attach", + wait_finish = false + }, + { + hotkey = "k", + command = "kill %proc.pid", + description = "kill", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "l", + command = "ltrace -p %proc.pid", + description = "ltrace", + }, + { + hotkey = "s", + command = "gdb -p %proc.pid --batch --quiet -ex \"thread apply all bt full\" -ex \"quit\"", + description = "print stack", + }, + }, +} diff --git a/userspace/sysdig/chisels/v_procs_fd_usage.lua b/userspace/sysdig/chisels/v_procs_fd_usage.lua new file mode 100644 index 0000000000..d11ff53029 --- /dev/null +++ b/userspace/sysdig/chisels/v_procs_fd_usage.lua @@ -0,0 +1,129 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "procs_fd_usage", + name = "Processes FD Usage", + description = "This view summarizes file descriptor usage for the processes in the system.", + tips = { + "A process that reaches its FD limit will very likely be killed by the OS. As a consequence, processes for which the OPEN column value is close to the MAX column value (or which, alternatively, have a PCT value close to 100) deserve particular attention.", + "Clicking enter on a selection will show the activity I/O activity done by the process on different families of FDs."}, + tags = {"Default"}, + view_type = "table", + filter = "evt.type!=switch", + applies_to = {"", "container.id", "fd.name", "fd.containername", "fd.sport", "fd.sproto", "evt.type", "fd.directory", "fd.containerdirectory", "fd.type", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + is_root = true, + drilldown_target = "io_by_type", + use_defaults = true, + columns = + { + { + name = "NA", + field = "proc.pid", + is_key = true + }, + { + name = "PID", + description = "Process PID.", + field = "proc.pid", + colsize = 8, + }, + { + tags = {"containers"}, + name = "VPID", + field = "proc.vpid", + description = "PID that the process has inside the container.", + colsize = 8, + }, + { + name = "OPEN", + field = "proc.fdopencount", + description = "Number of open FDs that the process currently has. On a trace file, this is the maximum value reached by the process over the whole file.", + aggregation = "MAX", + colsize = 8, + is_sorting = true, + }, + { + name = "MAX", + field = "proc.fdlimit", + description = "Maximum number of FDs that this process can open.", + aggregation = "MAX", + colsize = 8, + }, + { + name = "PCT", + field = "proc.fdusage", + description = "Percentage of currently open FDs versus the maximum allows for this process. In other words, this equals to OPEN * 100 / MAX, and can be used to quickly identify processes that are getting close to their limit.", + aggregation = "MAX", + colsize = 8, + }, + { + tags = {"containers"}, + name = "The container this process belongs to.", + field = "container.name", + colsize = 20 + }, + { + name = "Command", + description = "The full command line of the process.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + }, + actions = + { + { + hotkey = "9", + command = "kill -9 %proc.pid", + description = "kill -9", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "c", + command = "gcore %proc.pid", + description = "generate core", + }, + { + hotkey = "g", + command = "gdb -p %proc.pid", + description = "gdb attach", + wait_finish = false + }, + { + hotkey = "k", + command = "kill %proc.pid", + description = "kill", + ask_confirmation = true, + wait_finish = false + }, + { + hotkey = "l", + command = "ltrace -p %proc.pid", + description = "ltrace", + }, + { + hotkey = "s", + command = "gdb -p %proc.pid --batch --quiet -ex \"thread apply all bt full\" -ex \"quit\"", + description = "print stack", + }, + }, +} diff --git a/userspace/sysdig/chisels/v_slow_io.lua b/userspace/sysdig/chisels/v_slow_io.lua new file mode 100644 index 0000000000..128e00dd5c --- /dev/null +++ b/userspace/sysdig/chisels/v_slow_io.lua @@ -0,0 +1,77 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "slow_io", + name = "Slow File I/O", + description = "Lists all of the file read and write calls that took more than 1ms to complete, sorted based on completion time.", + tags = {"Default", "wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "thread.tid", "proc.name", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "evt.is_io=true and fd.type=file and (not fd.name contains '/dev/') and evt.latency>1000000", + use_defaults = true, + columns = + { + { + name = "NA", + field = "evt.rawtime", + is_key = true + }, + { + name = "TIME", + field = "evt.time", + description = "Time when the command was executed.", + colsize = 12, + }, + { + name = "LATENCY", + field = "evt.latency", + description = "The slow file name.", + is_sorting = true, + colsize = 8, + }, + { + name = "FILENAME", + field = "fd.name", + description = "The slow file name.", + colsize = 32, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "PID", + field = "proc.pid", + description = "PID of the process performing the I/O call.", + colsize = 12, + }, + { + name = "Command", + field = "proc.exeline", + aggregation = "MAX", + description = "The command accessing the slow file, including arguments.", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_spans_list.lua b/userspace/sysdig/chisels/v_spans_list.lua new file mode 100644 index 0000000000..288279e550 --- /dev/null +++ b/userspace/sysdig/chisels/v_spans_list.lua @@ -0,0 +1,79 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "spans_list", + name = "Spans List", + description = "Show the detailed list of a tracer selection's child spans. For each span type, the view reports information like its arguments and how long it took to complete.", + tips = { + "Only the spans spans that are direct childs of the selection (i.e. the spans with one more tag than the selection) are shown. Drilling down allows you to explore the further levels.", + }, + tags = {"Default"}, + view_type = "table", + applies_to = {"span.tag", "span.id", "span.time", "span.parenttime", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.directory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + use_defaults = true, + filter = "span.ntags>=%depth+1", + drilldown_target = "spans_list", + drilldown_increase_depth = true, + columns = + { + { + name = "NA", + field = "span.rawtime", + filterfield = "span.rawparenttime", + is_key = true, + filter_in_child_only = true, + }, + { + name = "ID", + field = "span.id", + description = "the unique numeric ID of the span.", + colsize = 10, + }, + { + name = "TIME", + field = "span.time", + description = "the time of the span enter tracer.", + colsize = 19, + }, + { + name = "DURATION", + field = "span.duration.fortag[%depth]", + description = "the time this span call took to complete", + colsize = 10, + aggregation = "AVG", + is_sorting = true, + }, + { + name = "TAG", + field = "span.tag[%depth]", + description = "span tag.", + colsize = 32, + aggregation = "SUM" + }, + { + name = "ARGS", + field = "span.enterargs", + description = "span enter arguments.", + colsize = 256, + aggregation = "SUM" + }, + } +} diff --git a/userspace/sysdig/chisels/v_spans_summary.lua b/userspace/sysdig/chisels/v_spans_summary.lua new file mode 100644 index 0000000000..b39690ddbf --- /dev/null +++ b/userspace/sysdig/chisels/v_spans_summary.lua @@ -0,0 +1,88 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "spans_summary", + name = "Spans Summary", + description = "Show a summary of a tracer selection's child spans. For each span type, the view reports information like how many times it's been called and how long it took to complete.", + tips = { + "Only the spans spans that are direct childs of the selection (i.e. the spans with one more tag than the selection) are shown. Drilling down allows you to explore the further levels.", + "This view collapses multiple spans with the same tag into a single entry. If you instead want to see each span as a separate entry, use the 'Spans List' view.", + }, + tags = {"Default"}, + view_type = "table", + applies_to = {"span.tag", "span.id", "span.time", "span.parenttime", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.directory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + use_defaults = true, + filter = "span.ntags>=%depth+1", + drilldown_target = "spans_summary", + spectro_type = "tracers", + drilldown_increase_depth = true, + columns = + { + { + name = "NA", + field = "span.tag[%depth]", + is_key = true + }, + { + is_sorting = true, + name = "HITS", + field = "span.count.fortag[%depth]", + description = "number of times the span with the given tag has been hit.", + colsize = 10, + aggregation = "SUM" + }, + { + name = "AVG TIME", + field = "span.duration.fortag[%depth]", + description = "the average time this span took to complete.", + colsize = 10, + aggregation = "AVG" + }, + { + name = "MIN TIME", + field = "span.duration.fortag[%depth]", + description = "the minimum time this span took to complete.", + colsize = 10, + aggregation = "MIN" + }, + { + name = "MAX TIME", + field = "span.duration.fortag[%depth]", + description = "the maximum time this span took to complete.", + colsize = 10, + aggregation = "MAX" + }, + { + name = "CHD HITS", + field = "span.childcount.fortag[%depth]", + description = "number of times any child of the span with the given tag has been hit. This is useful to determine if the span is a leaf or if it has childs nested in it.", + colsize = 10, + aggregation = "SUM" + }, + { + name = "TAG", + field = "span.tag[%depth]", + description = "span tag.", + colsize = 256, + aggregation = "SUM" + }, + } +} diff --git a/userspace/sysdig/chisels/v_spectro_all.lua b/userspace/sysdig/chisels/v_spectro_all.lua new file mode 100644 index 0000000000..7ce4239b88 --- /dev/null +++ b/userspace/sysdig/chisels/v_spectro_all.lua @@ -0,0 +1,48 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] +view_info = +{ + id = "spectro_all", + name = "Spectrogram-All", + description = "System call latency spectrogram.", + view_type = "spectrogram", + applies_to = {"evt.type"}, + filter = "evt.dir=<", + use_defaults = false, + columns = + { + { + name = "NA", + field = "evt.latency.quantized", + is_key = true + }, + { + name = "LATENCY", + description = "system call latency.", + field = "evt.latency.quantized", + }, + { + name = "COUNT", + description = "XXX.", + field = "evt.count", + aggregation = "SUM", + colsize = 8, + } + } +} diff --git a/userspace/sysdig/chisels/v_spectro_file.lua b/userspace/sysdig/chisels/v_spectro_file.lua new file mode 100644 index 0000000000..7efba64342 --- /dev/null +++ b/userspace/sysdig/chisels/v_spectro_file.lua @@ -0,0 +1,48 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] +view_info = +{ + id = "spectro_file", + name = "Spectrogram-File", + description = "File I/O latency spectrogram.", + view_type = "spectrogram", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "thread.tid", "proc.name", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name", "fd.name", "fd.containername", "fd.directory", "fd.containerdirectory", "fd.containerdirectory"}, + filter = "evt.dir=< and fd.type=file", + use_defaults = false, + columns = + { + { + name = "NA", + field = "evt.latency.quantized", + is_key = true + }, + { + name = "LATENCY", + description = "file latency.", + field = "evt.latency.quantized", + }, + { + name = "COUNT", + description = "XXX.", + field = "evt.count", + aggregation = "SUM", + colsize = 8, + } + } +} diff --git a/userspace/sysdig/chisels/v_spectro_traces.lua b/userspace/sysdig/chisels/v_spectro_traces.lua new file mode 100644 index 0000000000..4483bcd0be --- /dev/null +++ b/userspace/sysdig/chisels/v_spectro_traces.lua @@ -0,0 +1,54 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] +view_info = +{ + id = "spectro_traces", + name = "Traces Spectrogram", + description = "Traces duration spectrogram.", + tips = { + "This view offers a spectrogram-based representation of root trace spans durations.", + "When appled to a selection in a view like 'Trace Summary' or 'Trace List', this view will only show the latency of the selected spans, while their parent and child spans won't be shown. When applied to the whole machine, this view will show the latency of the traces, i.e. the root spans that have just one tag.", + "If you are in a trace view like 'Traces Summary' or 'Traces List', you can quickly show this spectrogram for a selection by clicking on F12.", + }, + view_type = "spectrogram", + applies_to = {"", "span.tag", "span.id", "container.id", "proc.pid", "thread.nametid", "thread.tid", "proc.name", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name", "fd.name", "fd.containername", "fd.directory", "fd.containerdirectory", "fd.containerdirectory"}, + filter = "span.ntags=%depth+1", + use_defaults = false, + drilldown_target = "spans_list", + propagate_filter = false, + columns = + { + { + name = "NA", + field = "span.duration.quantized", + is_key = true + }, + { + name = "LATENCY", + description = "span latency. This determines the horizontal position of a dot in the chart.", + field = "span.duration.quantized", + }, + { + name = "COUNT", + description = "number of times a span falls in a certain latency bucket. This determines the color of a dot in the chart.", + field = "evt.count", + aggregation = "SUM", + } + } +} diff --git a/userspace/sysdig/chisels/v_sports.lua b/userspace/sysdig/chisels/v_sports.lua new file mode 100644 index 0000000000..95502ba64b --- /dev/null +++ b/userspace/sysdig/chisels/v_sports.lua @@ -0,0 +1,78 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "sports", + name = "Server Ports", + description = "This view lists all of the server ports in terms of network bandwidth usage.", + tips = {"Want to restrict this visualization to a single process or container? Just drill down into them before applying it.", + "Select a port and drill down with the 'Top Processes' view to see which processes are generating traffic on a port."}, + tags = {"wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "thread.tid", "proc.name", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "(fd.type=ipv4 or fd.type=ipv6) and fd.name!=''", + use_defaults = true, + drilldown_target = "connections", + columns = + { + { + name = "NA", + field = "fd.sport", + is_key = true + }, + { + name = "SPORT", + description = "Server Port.", + field = "fd.sport", + colsize = 8, + }, + { + name = "BPS IN", + field = "evt.buflen.net.in", + description = "This port's input bandwidth in bytes per second.", + is_sorting = true, + colsize = 12, + aggregation = "TIME_AVG" + }, + { + name = "BPS OUT", + field = "evt.buflen.net.out", + description = "This port's output bandwidth in bytes per second.", + colsize = 12, + aggregation = "TIME_AVG" + }, + { + name = "IO CALLS", + field = "evt.count", + description = "Total (read+write) number of input/output calls made by the process on the connection.", + colsize = 12, + aggregation = "SUM" + } + }, + actions = + { + { + hotkey = "t", + command = "tcpdump -niany port %fd.sport", + description = "tcpdump port", + wait_finish = false + }, + }, +} diff --git a/userspace/sysdig/chisels/v_spy_syslog.lua b/userspace/sysdig/chisels/v_spy_syslog.lua new file mode 100644 index 0000000000..c2ff86303e --- /dev/null +++ b/userspace/sysdig/chisels/v_spy_syslog.lua @@ -0,0 +1,69 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "spy_syslog", + name = "Spy Syslog", + description = "Show the entries written to syslog.", + tips = {"This view can be applied to the whole system, to watch overall syslog activity, but is also useful when applied to a container or a process. In that case, the view will only show the syslog writes generated by the selected entity."}, + tags = {"Default"}, + view_type = "list", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "fd.name contains /dev/log and evt.is_io_write=true and evt.dir=< and evt.failed=false", + columns = + { + { + name = "PID", + field = "proc.pid", + description = "PID of the process generating the message.", + colsize = 8, + }, + { + name = "PROC", + field = "proc.name", + description = "Name of the process generating the message.", + colsize = 8, + }, + { + name = "FAC", + field = "syslog.facility.str", + description = "syslog facility of the message.", + colsize = 8, + }, + { + name = "SEV", + field = "syslog.severity.str", + description = "syslog severity of the message.", + colsize = 8, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + colsize = 20 + }, + { + name = "MESSAGE", + field = "syslog.message", + description = "Message sent to syslog", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_spy_users.lua b/userspace/sysdig/chisels/v_spy_users.lua new file mode 100644 index 0000000000..19a8591184 --- /dev/null +++ b/userspace/sysdig/chisels/v_spy_users.lua @@ -0,0 +1,65 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "spy_users", + name = "Spy Users", + description = "Lists all the commands that are run interactively, i.e. that have a shell as the parent process. The result is the display of all the user activity, sorted by time.", + tags = {"Default"}, + view_type = "list", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "thread.tid", "proc.name", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "((evt.type=execve and evt.dir=<) or (evt.type=chdir and evt.dir=< and proc.name contains sh and not proc.name contains sshd)) and evt.failed=false", + use_defaults = true, + columns = + { + { + name = "TIME", + field = "evt.time", + description = "Time when the command was executed.", + colsize = 12, + }, + { + name = "USER", + field = "user.name", + description = "Name of the user running the command.", + colsize = 12, + }, + { + name = "SHELL", + field = "proc.ppid", + description = "Pid of the shell where this command was executed. This, essentially, corresponds to a 'session ID'. You can filer or sort by this column to isolate a specific interactive user session.", + colsize = 8, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + field = "proc.exeline", + aggregation = "MAX", + description = "The executed command, including arguments.", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_spy_users_wsysdig.lua b/userspace/sysdig/chisels/v_spy_users_wsysdig.lua new file mode 100644 index 0000000000..aaf2a3a22b --- /dev/null +++ b/userspace/sysdig/chisels/v_spy_users_wsysdig.lua @@ -0,0 +1,73 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "spy_users_wsysdig", + name = "Spy Users", + description = "Lists all the commands that are run interactively, i.e. that have a shell as the parent process. The result is the display of all the user activity, sorted by time.", + tags = {"Default", "wsysdig", "nocsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "thread.tid", "proc.name", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + filter = "((evt.type=execve and evt.dir=<) or (evt.type=chdir and evt.dir=< and proc.name contains sh and not proc.name contains sshd)) and evt.failed=false", + drilldown_target = "threads", + use_defaults = true, + propagate_filter = false, + columns = + { + { + name = "NA", + field = "thread.nametid", + is_key = true + }, + { + name = "TIME", + field = "evt.time", + description = "Time when the command was executed.", + colsize = 12, + is_sorting = true + }, + { + name = "USER", + field = "user.name", + description = "Name of the user running the command.", + colsize = 12, + }, + { + name = "SHELL", + field = "proc.ppid", + description = "Pid of the shell where this command was executed. This, essentially, corresponds to a 'session ID'. You can filer or sort by this column to isolate a specific interactive user session.", + colsize = 8, + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + field = "proc.exeline", + aggregation = "MAX", + description = "The executed command, including arguments.", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_syscall_procs.lua b/userspace/sysdig/chisels/v_syscall_procs.lua new file mode 100644 index 0000000000..e686bb885c --- /dev/null +++ b/userspace/sysdig/chisels/v_syscall_procs.lua @@ -0,0 +1,67 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "syscall_procs", + name = "Syscall Callers", + description = "Show the top processes based on number of system call invocations and time spent calling them.", + tags = {"Default"}, + view_type = "table", + applies_to = {"evt.type"}, + use_defaults = true, + filter = "syscall.type exists", + columns = + { + { + name = "NA", + field = "proc.pid", + is_key = true + }, + { + is_sorting = true, + name = "CALLS/S", + field = "evt.count", + description = "Number of system calls per second that this process has invoked.", + colsize = 10, + aggregation = "TIME_AVG" + }, + { + name = "TIME", + field = "evt.latency", + description = "Total time spent on system calls by the process during the sample interval. On trace files, this is the total for the whole file.", + colsize = 10, + aggregation = "SUM" + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + description = "The full command line of the process.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_syscalls.lua b/userspace/sysdig/chisels/v_syscalls.lua new file mode 100644 index 0000000000..5a6a7622b9 --- /dev/null +++ b/userspace/sysdig/chisels/v_syscalls.lua @@ -0,0 +1,72 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "syscalls", + name = "System Calls", + description = "Show the top system calls in the system based on number of invocations and time spent calling them.", + tips = { + "This view is useful to spot not only system activity saturation, but also things like high wait time.", + "Drill down by clicking enter on a system call to see which processes are using it.", + "The AVG TIME column is useful to identify system operations that tend to be consistently slow and can be the cause of bottlenecks."}, + tags = {"Default", "wsysdig"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.sport", "fd.sproto", "fd.name", "fd.containername", "fd.directory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + use_defaults = true, + filter = "syscall.type exists", + drilldown_target = "syscall_procs", + columns = + { + { + name = "NA", + field = "evt.type", + is_key = true + }, + { + is_sorting = true, + name = "CALLS/S", + field = "evt.count", + description = "Number of calls per second for this system call.", + colsize = 10, + aggregation = "TIME_AVG" + }, + { + name = "TOT TIME", + field = "evt.latency", + description = "Total time spent waiting for the given system call to return.", + colsize = 10, + aggregation = "SUM" + }, + { + name = "AVG TIME", + field = "evt.latency", + description = "Average time spent in the given system call. This is calculated dividing the value under TOT TIME by the value under COUNT.", + colsize = 10, + aggregation = "AVG" + }, + { + name = "SYSCALL", + field = "evt.type", + description = "System call name.", + colsize = 32, + aggregation = "SUM" + }, + } +} diff --git a/userspace/sysdig/chisels/v_threads.lua b/userspace/sysdig/chisels/v_threads.lua new file mode 100644 index 0000000000..59f0618237 --- /dev/null +++ b/userspace/sysdig/chisels/v_threads.lua @@ -0,0 +1,103 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "threads", + name = "Threads", + description = "This view lists all the threads running in the system or in the current selection, showing usage of resources like CPU, memory, disk and network for each thread.", + tips = {"Apply this view to a process to get the list of threads for that process only. Similarly, apply it to a container to see the threads running inside it."}, + tags = {"Default", "wsysdig"}, + view_type = "table", + filter = "evt.type!=switch", + applies_to = {"", "proc.pid", "thread.nametid", "proc.name", "container.id", "fd.sport", "fd.sproto", "fd.name", "fd.containername", "fd.directory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + is_root = true, + drilldown_target = "files", + use_defaults = true, + columns = + { + { + name = "NA", + field = "thread.tid", + is_key = true + }, + { + name = "PID", + description = "PID of the process this thread belongs to.", + field = "proc.pid", + colsize = 8, + }, + { + name = "TID", + description = "Thread-specific ID. Main threads have TID=PID.", + field = "thread.tid", + colsize = 8, + }, + { + tags = {"containers"}, + name = "VPID", + field = "proc.vpid", + description = "PID that the process has inside the container.", + colsize = 8, + }, + { + tags = {"containers"}, + name = "VTID", + field = "thread.vtid", + description = "TID that the tread has inside the container.", + colsize = 8, + }, + { + name = "CPU", + field = "thread.cpu", + description = "Amount of CPU used by the process.", + colsize = 8, + aggregation = "AVG", + is_sorting = true + }, + { + name = "FILE", + field = "evt.buflen.file", + description = "Total (input+output) file I/O bandwidth generated by the thread, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG" + }, + { + name = "NET", + field = "evt.buflen.net", + description = "Total (input+output) network I/O bandwidth generated by the thread, in bytes per second.", + colsize = 8, + aggregation = "TIME_AVG" + }, + { + tags = {"containers"}, + name = "Container", + field = "container.name", + description = "Name of the container. What this field contains depends on the containerization technology. For example, for docker this is the content of the 'NAMES' column in 'docker ps'", + colsize = 20 + }, + { + name = "Command", + description = "The full command line of the process.", + field = "proc.exeline", + aggregation = "MAX", + colsize = 0 + } + } +} diff --git a/userspace/sysdig/chisels/v_traces_list.lua b/userspace/sysdig/chisels/v_traces_list.lua new file mode 100644 index 0000000000..e0fcf0890f --- /dev/null +++ b/userspace/sysdig/chisels/v_traces_list.lua @@ -0,0 +1,80 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "traces_list", + name = "Traces List", + description = "Show the detailed list of the traces executing in the system. For each single executed trace, the view reports information like the timestamp, the duration and the arguments.", + tips = { + "Traces are sysdig's super easy way to delimit portions of your code so that sysdig can measure how long they take and tell you what's happening inside them. You can learn about tracers at https://github.com/draios/sysdig/wiki/Tracers.", + "Only the root trace spans (i.e. the spans with only one tag) are shown when this view is applied to the whole machine. Drilling down allows you to explore the child spans.", + }, + tags = {"Default"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.directory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + use_defaults = false, + filter = "span.ntags>=%depth+1", + drilldown_target = "spans_list", + drilldown_increase_depth = true, + columns = + { + { + name = "NA", + field = "span.rawtime", + filterfield = "span.rawparenttime", + is_key = true, + filter_in_child_only = true, + }, + { + name = "ID", + field = "span.id", + description = "the unique numeric ID of the span.", + colsize = 10, + }, + { + name = "TIME", + field = "span.time", + description = "the time of the span enter tracer.", + colsize = 19, + }, + { + name = "DURATION", + field = "span.duration.fortag[%depth]", + description = "the time this span call took to complete", + colsize = 10, + aggregation = "AVG", + is_sorting = true, + }, + { + name = "TAG", + field = "span.tag[%depth]", + description = "trace tag.", + colsize = 32, + aggregation = "SUM" + }, + { + name = "ARGS", + field = "span.enterargs", + description = "trace enter arguments.", + colsize = 256, + aggregation = "SUM" + }, + } +} diff --git a/userspace/sysdig/chisels/v_traces_summary.lua b/userspace/sysdig/chisels/v_traces_summary.lua new file mode 100644 index 0000000000..f6308d7a06 --- /dev/null +++ b/userspace/sysdig/chisels/v_traces_summary.lua @@ -0,0 +1,89 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +view_info = +{ + id = "traces_summary", + name = "Traces Summary", + description = "Show a summary of the traces executing in the system. For each trace tag, the view reports information like how many spans with that tag have executed and what's the average duration.", + tips = { + "Traces are sysdig's super easy way to delimit portions of your code so that sysdig can measure how long they take and tell you what's happening inside them. You can learn about tracers at https://github.com/draios/sysdig/wiki/Tracers.", + "Only the root trace spans (i.e. the spans with only one tag) are shown when this view is applied to the whole machine. Drilling down allows you to explore the child spans.", + "This view collapses multiple spans with the same tag into a single entry, offering a compact summary of trace activity. If you instead want to see each span as a separate entry, use the 'Trace List' view.", + }, + tags = {"Default"}, + view_type = "table", + applies_to = {"", "container.id", "proc.pid", "thread.nametid", "proc.name", "thread.tid", "fd.directory", "fd.containerdirectory", "evt.res", "k8s.pod.id", "k8s.rc.id", "k8s.rs.id", "k8s.svc.id", "k8s.ns.id", "marathon.app.id", "marathon.group.name", "mesos.task.id", "mesos.framework.name"}, + use_defaults = true, + filter = "span.ntags>=%depth+1", + drilldown_target = "spans_summary", + spectro_type = "tracers", + drilldown_increase_depth = true, + columns = + { + { + name = "NA", + field = "span.tag[%depth]", + is_key = true + }, + { + is_sorting = true, + name = "HITS", + field = "span.count.fortag[%depth]", + description = "number of times the trace with the given tag has been hit.", + colsize = 10, + aggregation = "SUM" + }, + { + name = "AVG TIME", + field = "span.duration.fortag[%depth]", + description = "the average time this trace took to complete.", + colsize = 10, + aggregation = "AVG" + }, + { + name = "MIN TIME", + field = "span.duration.fortag[%depth]", + description = "the minimum time this trace took to complete.", + colsize = 10, + aggregation = "MIN" + }, + { + name = "MAX TIME", + field = "span.duration.fortag[%depth]", + description = "the maximum time this trace took to complete.", + colsize = 10, + aggregation = "MAX" + }, + { + name = "CHD HITS", + field = "span.childcount.fortag[%depth]", + description = "number of times any child of the trace with the given tag has been hit. This is useful to determine if the span is a leaf or if it has childs nested in it.", + colsize = 10, + aggregation = "SUM" + }, + { + name = "TAG", + field = "span.tag[%depth]", + description = "span tag.", + colsize = 256, + aggregation = "SUM" + }, + } +} diff --git a/userspace/sysdig/chisels/wsysdig_summary.lua b/userspace/sysdig/chisels/wsysdig_summary.lua new file mode 100644 index 0000000000..52d671a744 --- /dev/null +++ b/userspace/sysdig/chisels/wsysdig_summary.lua @@ -0,0 +1,1503 @@ +--[[ +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +--]] + +-- Chisel description +description = "internal chisel, creates the json for the wsysdig summary page." +short_description = "wsysdig summary generator" +category = "NA" +hidden = true + +-- Imports and globals +require "common" + +-- Chisel argument list +args = +{ + { + name = "composite_args", + description = "The number of events in the file. If this argument is not specified, the chisel will just scan the file, compute the number of events and then relaunch itself with the number as argument.", + argtype = "int", + optional = true + } +} + +local disable_index = true -- change this if you are working on this script and don't want to be bothered by indexing +local n_samples = 400 +local sampling_period = 0 +local arg_n_timeline_samples = n_samples +local json = require ("dkjson") +local gsummary = {} -- The global summary +local ssummary = {} -- Last sample's summary +local nintervals = 0 +local file_cache_exists = false +local arg_file_duration = nil +local evtcnt = 0 +local index_format_version = 1 -- Increase this if the content or the format of the output changes. + -- An increase in this number will cause existing indexes to be discharged. + +-- Argument notification callback +function on_set_arg(name, val) + if name == "composite_args" then + vals = split(val, ",") + + local val1n = tonumber(vals[1]) + + if val1n ~= 0 and val1n < n_samples then + arg_n_timeline_samples = val1n + end + + if vals[2] ~= nil then + arg_file_duration = vals[2] + end + + return true + end + + return false +end +------------------------------------------------------------------------------- +-- Summary handling helpers +------------------------------------------------------------------------------- +local services = +{ + [80] = 'HTTP', + [8080] = 'HTTP', + [443] = 'HTTPs', + [22] = 'SSH', + [53] = 'DNS', + [6666] = 'Sysdig Agent', + [6667] = 'Sysdig Agent', + [6443] = 'Sysdig Agent', + [2379] = 'etcd', + [22379] = 'etcd', + [3306] = 'mysql', + [5432] = 'postgres', + [6379] = 'redis', + [5984] = 'couchdb', + [9880] = 'fluentd', + [8125] = 'statsd', + [4730] = 'gearman', + [50070] = 'hadoop', + [8020] = 'hadoop', + [9000] = 'hadoop', + [60000] = 'hbase', + [60010] = 'hbase', + [60020] = 'hbase', + [60030] = 'hbase', + [2181] = 'kafka', + [1978] = 'Kyoto Tycoon', + [11211] = 'memcached', + [27017] = 'mongodb', + [27018] = 'mongodb', + [27019] = 'mongodb', + [28017] = 'mongodb', + [5672] = 'rabbitmq', + [8087] = 'riak', + [8098] = 'riak', + [8983] = 'solr', + [5555] = 'voltdb' +} + +-- +-- Populate the protocol table for a crappy application that use a ton of ports +-- +for j=9200,9400,1 do services[j] = 'elasticsearch' end + +-- +-- Create the protocols table by inverting the services table +-- +local protocols = {} +for i, v in pairs(services) do + if protocols[v] == nil then + protocols[v] = {i} + else + protocols[v][#protocols[v] + 1] = i + end +end + +function create_category_basic(excludable, noteworthy, aggregation) + aggregation = aggregation or 'sum' + + return { + tot=0, + max=0, + timeLine={}, + excludable=excludable, + noteworthy=noteworthy, + aggregation=aggregation + } +end + +function create_category_table(excludable, noteworthy, aggregation) + aggregation = aggregation or 'sum' + + return { + tot=0, + max=0, + timeLine={}, + table={}, + excludable=excludable, + noteworthy=noteworthy, + aggregation=aggregation + } +end + +function reset_summary(s) + s.procCount = create_category_table(false, false, 'avg') + s.containerCount = create_category_table(false, false, 'avg') + s.executedCommands = create_category_basic(false, true) + s.executedInteractiveCommands = create_category_basic(true, true) + s.syscallCount = create_category_basic(false, false) + s.fileCount = create_category_table(true, false) + s.fileBytes = create_category_basic(false, false) + s.fileBytesR = create_category_basic(false, false) + s.fileBytesW = create_category_basic(false, false) + s.fileCountW = create_category_table(true, false) + s.sysFileCountW = create_category_table(true, true) + s.connectionCount = create_category_table(true, false) + s.netBytes = create_category_basic(false, false) + s.netBytesR = create_category_basic(false, false) + s.netBytesW = create_category_basic(false, false) + s.notifications = create_category_basic(true, true) + if s.listeningPortCount == nil then + s.listeningPortCount = create_category_table(true, false, 'avg') + end + s.newConnectionsO = create_category_basic(true, false) + s.newConnectionsI = create_category_basic(true, false) + s.newConnectionsSsh = create_category_basic(true, true) + s.newListeningPorts = create_category_basic(true, true) + s.fileDeletionsCount = create_category_basic(true, true) + s.newSymLinksCount = create_category_basic(true, true) + s.forkCount = create_category_basic(true, false) + s.openErrorCount = create_category_basic(true, false) + s.connectErrorCount = create_category_basic(true, true) + s.sudoInvocations = create_category_basic(true, true) + s.setnsInvocations = create_category_basic(true, true) + s.signalCount = create_category_basic(true, false) + s.segfaultCount = create_category_basic(true, true) + s.over1msFileIoCount = create_category_basic(true, false) + s.over10msFileIoCount = create_category_basic(true, false) + s.over100msFileIoCount = create_category_basic(true, true) + s.appLogCount = create_category_basic(true, false) + s.appLogCountW = create_category_basic(true, false) + s.appLogCountE = create_category_basic(true, true) + s.sysLogCount = create_category_basic(true, false) + s.sysLogCountW = create_category_basic(true, false) + s.sysLogCountE = create_category_basic(true, true) + s.dockerEvtsCount = create_category_basic(true, true) + -- reset dynamic dockerEvtsCount* categories + for ccat in pairs(s) do + prefix = 'dockerEvtsCount' + if starts_with(ccat, prefix) and ccat ~= prefix then + s[ccat] = create_category_basic(true, true) + end + end + s.sysReqCountHttp = create_category_basic(true, true) + s.sysErrCountHttp = create_category_basic(true, true) + + -- creating the protocol categories involves two passes of the services table + for i, v in pairs(protocols) do + local ccat = 'protoBytes_' .. i + s[ccat] = create_category_basic(true, false) + end +end + +function add_summaries(ts_s, ts_ns, dst, src) + local time = sysdig.make_ts(ts_s, ts_ns) + + for k, v in pairs(src) do + if dst[k] == nil then + -- add missing category dynamically + -- dynamic categories are dockerEvtsCount* + prefix = 'dockerEvtsCount' + if starts_with(k, prefix) and k ~= prefix then + dst[k] = create_category_basic(true, true) + end + end + + dst[k].tot = dst[k].tot + v.tot + if v.tot > dst[k].max then + dst[k].max = v.tot + end + local tl = dst[k].timeLine + tl[#tl+1] = {t=time, v=v.tot} + + if v.table ~= nil then + local dt = dst[k].table + for tk, tv in pairs(v.table) do + dt[tk] = tv + end + end + end +end + +function generate_subsampled_timeline(src, nsamples, op) + local res = {} + local ratio = math.ceil(#src / nsamples) + local k = 0 + local accumulator = 0 + local etime = src[1].t + local max = 0 + local tot = 0 + + for j = 1,#src,1 do + k = k + 1 + accumulator = accumulator + src[j].v + + if k >= ratio then + if op == 'avg' then + accumulator = accumulator / k + end + + res[#res+1] = {t=etime, v=accumulator} + + tot = tot + accumulator + if accumulator > max then + max = accumulator + end + k = 0 + accumulator = 0 + if src[j + 1] ~= nil then + etime = src[j + 1].t + end + end + end + + return{timeLine=res, tot=tot, max=max} +end + +function subsample_timelines(jtable) + if arg_n_timeline_samples ~= 0 and arg_n_timeline_samples ~= n_samples then + for k, v in pairs(jtable.metrics) do + local data = v.data + st = generate_subsampled_timeline(data.timeLine, + arg_n_timeline_samples, + data.aggregation) + + v.data.timeLine = st.timeLine + v.data.max = st.max + end + end +end + +------------------------------------------------------------------------------- +-- Helpers to dig into the data coming from sysdig +------------------------------------------------------------------------------- +function string.starts(big_str, small_str) + return string.sub(big_str, 1, string.len(small_str)) == small_str +end + +function is_system_dir(filename) + if string.starts(filename, '/bin/') or + string.starts(filename, '/sbin/') or + string.starts(filename, '/boot/') or + string.starts(filename, '/etc/') or + string.starts(filename, '/lib') or + string.starts(filename, '/usr/bin/') or + string.starts(filename, '/usr/sbin/') or + string.starts(filename, '/usr/share/') or + string.starts(filename, '/usr/lib') + then + return true + end + + return false +end + +function is_log_file(filename) + if(string.find(filename, '%.log') or + string.find(filename, '_log') or + string.find(filename, '/var/log')) and + not (string.find(filename, '%.gz') or string.find(filename, '%.tgz')) + then + return true + end + + return false +end + +function generate_io_stats(fdname, cnt_cat) + if fdname == nil then + return + end + + if cnt_cat.table[fdname] == nil then + cnt_cat.table[fdname] = 1 + cnt_cat.tot = cnt_cat.tot + 1 + end +end + +function generate_proto_stats(sport, buflen) + local proto = services[sport] + if proto ~= nil then + local catname = 'protoBytes_' .. proto + ssummary[catname].tot = ssummary[catname].tot + buflen + end +end + +function parse_thread_table_startup() + local data = {} + local cnt = 0 + + local ttable = sysdig.get_thread_table_barebone(sysdig.get_filter()) + + for k, v in pairs(ttable) do + for kf, vf in pairs(v.fdtable) do + if vf.is_server then + data[vf.sport] = 1 + end + end + end + + ssummary.listeningPortCount.tot = 0 + for k, v in pairs(data) do + ssummary.listeningPortCount.tot = ssummary.listeningPortCount.tot + 1 + end +--print(ssummary.listeningPortCount.tot) + ssummary.listeningPortCount.table = data +end + +function parse_thread_table_interval() + local data = {} + local cnt = 0 + local ttable = sysdig.get_thread_table_barebone_nofds(sysdig.get_filter()) + + for k, v in pairs(ttable) do + if v.tid == v.pid then + data[v.pid] = 1 + cnt = cnt + 1 + end + end + + ssummary.procCount.tot = cnt + ssummary.procCount.table = data +end + +function parse_container_table() + local data = {} + local cnt = 0 + + local ctable = sysdig.get_container_table() + + for k, v in pairs(ctable) do + data[v.id] = v.name + cnt = cnt + 1 + end + + ssummary.containerCount.tot = cnt + ssummary.containerCount.table = data +end + +function update_docker_cats(evt_type) + local cat = 'dockerEvtsCount' .. evt_type + + if (ssummary[cat] == nil) then + ssummary[cat] = create_category_basic(true, true) + end + + ssummary[cat].tot = ssummary[cat].tot + 1 +end + +------------------------------------------------------------------------------- +-- Initialization callbacks +------------------------------------------------------------------------------- +function on_init() + if arg_file_duration == nil then + return true + end + + if(sysdig.get_filter() ~= nil and sysdig.get_filter() ~= '') then + disable_index = true + end + + sampling_period = arg_file_duration / (n_samples - 1) + chisel.set_precise_interval_ns(sampling_period) + percent_update_sample_period = math.floor(n_samples / 100 * 3) + if percent_update_sample_period < 2 then + percent_update_sample_period = 1 + end + + reset_summary(gsummary) + reset_summary(ssummary) + + -- set the following fields on_event() + fetype = chisel.request_field("evt.type") + fdir = chisel.request_field("evt.dir") + frawres = chisel.request_field("evt.rawres") + ffdcontname = chisel.request_field("fd.containername") + ffdname = chisel.request_field("fd.name") + ffdtype = chisel.request_field("fd.type") + fiswrite = chisel.request_field("evt.is_io_write") + fisread = chisel.request_field("evt.is_io_read") + fbuffer = chisel.request_field("evt.buffer") + fbuflen = chisel.request_field("evt.buflen") + fsport = chisel.request_field("fd.sport") + flport = chisel.request_field("fd.lport") + ftypechar = chisel.request_field("fd.typechar") + fexe = chisel.request_field("evt.arg.exe") + fsignal = chisel.request_field("evt.arg.sig") + flatency = chisel.request_field("evt.latency") + fsyslogsev = chisel.request_field("syslog.severity") + finfrasource = chisel.request_field("evt.arg.source") + finfraname = chisel.request_field("evt.arg.name") + fpname = chisel.request_field("proc.pname") + + -- kick off GC + collectgarbage() + + print('{"slices": [') + return true +end + +function on_capture_start() + if arg_file_duration == nil then + return true + end + + if not disable_index then + local dirname = sysdig.get_evtsource_name() .. '_wd_index' + local f = io.open(dirname .. '/VERSION', "r") + if f ~= nil then + local version = tonumber(f:read "*all") + f:close() + + if version == index_format_version then + file_cache_exists = true + sysdig.end_capture() + end + end + end + parse_thread_table_startup() + return true +end + +------------------------------------------------------------------------------- +-- Event callback +------------------------------------------------------------------------------- +function on_event() + if arg_file_duration == nil then + evtcnt = evtcnt + 1 + return true + end + + ssummary.syscallCount.tot = ssummary.syscallCount.tot + 1 + + local dir = evt.field(fdir) + + if dir ~= nil then + if dir == '<' then + local rawres = evt.field(frawres) + local etype = evt.field(fetype) + + if rawres ~= nil and rawres >= 0 then + local fdcontname = evt.field(ffdcontname) + local fdname = evt.field(ffdname) + local fdtype = evt.field(ffdtype) + local iswrite = evt.field(fiswrite) + local isread = evt.field(fisread) + + if iswrite or isread then + if fdtype == 'file' then + local buflen = evt.field(fbuflen) + if buflen == nil then + buflen = 0 + end + + generate_io_stats(fdcontname, ssummary.fileCount) + + if iswrite then + generate_io_stats(fdcontname, ssummary.fileCountW) + ssummary.fileBytes.tot = ssummary.fileBytes.tot + buflen + ssummary.fileBytesW.tot = ssummary.fileBytesW.tot + buflen + + if is_system_dir(fdname) then + generate_io_stats(fdname, ssummary.sysFileCountW) + end + + -- log metrics support + + local syslogsev = evt.field(fsyslogsev) + if syslogsev ~= nil then + ssummary.sysLogCount.tot = ssummary.sysLogCount.tot + 1 + if syslogsev == 4 then + ssummary.sysLogCountW.tot = ssummary.sysLogCountW.tot + 1 + elseif syslogsev < 4 then + ssummary.sysLogCountE.tot = ssummary.sysLogCountE.tot + 1 + end + elseif is_log_file(fdname) then + local buf = evt.field(fbuffer) + local msgs = split(buf, "\n") + + for i, msg in ipairs(msgs) do + if #msg ~= 0 then + ssummary.appLogCount.tot = ssummary.appLogCount.tot + 1 + + local ls = string.lower(msg) + + if string.find(ls, "warn") then + ssummary.appLogCountW.tot = ssummary.appLogCountW.tot + 1 + elseif string.find(ls, "error") or + string.find(ls, "critic") or + string.find(ls, "emergency") or + string.find(ls, "alert") then + ssummary.appLogCountE.tot = ssummary.appLogCountE.tot + 1 + end + end + end + end + elseif isread then + ssummary.fileBytes.tot = ssummary.fileBytes.tot + buflen + ssummary.fileBytesR.tot = ssummary.fileBytesR.tot + buflen + end + + local latency = evt.field(flatency) + if latency ~= nil and not string.starts(fdname, '/dev/') then + if latency > 100000000 then + ssummary.over100msFileIoCount.tot = ssummary.over100msFileIoCount.tot + 1 + end + if latency > 10000000 then + ssummary.over10msFileIoCount.tot = ssummary.over10msFileIoCount.tot + 1 + end + if latency > 1000000 then + ssummary.over1msFileIoCount.tot = ssummary.over1msFileIoCount.tot + 1 + end + end + elseif fdtype == 'ipv4' or fdtype == 'ipv6' then + local buflen = evt.field(fbuflen) + if buflen == nil then + buflen = 0 + end + + generate_io_stats(fdcontname, ssummary.connectionCount) + + if iswrite then + ssummary.netBytes.tot = ssummary.netBytes.tot + buflen + ssummary.netBytesW.tot = ssummary.netBytesW.tot + buflen + elseif isread then + ssummary.netBytes.tot = ssummary.netBytes.tot + buflen + ssummary.netBytesR.tot = ssummary.netBytesR.tot + buflen + end + + local sport = evt.field(fsport) + if sport ~= nil then + generate_proto_stats(sport, buflen) + end + + local buf = evt.field(fbuffer) + if string.starts(buf, 'HTTP/') then + ssummary.sysReqCountHttp.tot = ssummary.sysReqCountHttp.tot + 1 + + local parts = split(buf, ' ') + if tonumber(parts[2]) ~= 200 then + ssummary.sysErrCountHttp.tot = ssummary.sysErrCountHttp.tot + 1 + end + end + + elseif fdtype == 'unix' then + if iswrite then + -- apps can write to syslog using unix pipes + local syslogsev = evt.field(fsyslogsev) + if syslogsev ~= nil then + ssummary.sysLogCount.tot = ssummary.sysLogCount.tot + 1 + if syslogsev == 4 then + ssummary.sysLogCountW.tot = ssummary.sysLogCountW.tot + 1 + elseif syslogsev < 4 then + ssummary.sysLogCountE.tot = ssummary.sysLogCountE.tot + 1 + end + end + end + end + elseif etype == 'execve' then + ssummary.executedCommands.tot = ssummary.executedCommands.tot + 1 + local pname = evt.field(fpname) + if pname ~= nil then + if string.find(pname, 'bash') then + ssummary.executedInteractiveCommands.tot = ssummary.executedInteractiveCommands.tot + 1 + end + end + + local exe = evt.field(fexe) + if exe == 'sudo' then + ssummary.sudoInvocations.tot = ssummary.sudoInvocations.tot + 1 + end + elseif etype == 'bind' then + local sport = evt.field(fsport) + if sport ~= nil then + generate_io_stats(sport, ssummary.listeningPortCount) + ssummary.newListeningPorts.tot = ssummary.newListeningPorts.tot + 1 + end + elseif etype == 'connect' then + local sport = evt.field(fsport) + if sport ~= nil then + ssummary.newConnectionsO.tot = ssummary.newConnectionsO.tot + 1 + if sport == 22 then + ssummary.newConnectionsSsh.tot = ssummary.newConnectionsSsh.tot + 1 + end + end + elseif etype == 'accept' then + local sport = evt.field(fsport) + if sport ~= nil then + ssummary.newConnectionsI.tot = ssummary.newConnectionsI.tot + 1 + if sport == 22 then + ssummary.newConnectionsSsh.tot = ssummary.newConnectionsSsh.tot + 1 + end + end + elseif etype == 'unlink' or etype == 'unlinkat' then + ssummary.fileDeletionsCount.tot = ssummary.fileDeletionsCount.tot + 1 + elseif etype == 'symlink' or etype == 'symlinkat' then + ssummary.newSymLinksCount.tot = ssummary.newSymLinksCount.tot + 1 + elseif etype == 'clone' or etype == 'fork' then + if rawres > 0 then + ssummary.forkCount.tot = ssummary.forkCount.tot + 1 + end + elseif etype == 'setns' then + ssummary.setnsInvocations.tot = ssummary.setnsInvocations.tot + 1 + end + elseif etype == 'connect' then + local sport = evt.field(fsport) + if sport ~= nil then + ssummary.newConnectionsO.tot = ssummary.newConnectionsO.tot + 1 + if sport == 22 then + ssummary.newConnectionsSsh.tot = ssummary.newConnectionsSsh.tot + 1 + end + end + + if rawres ~= -115 then + local fdtype = evt.field(ffdtype) + if fdtype == 'ipv4' or fdtype == 'ipv6' then + ssummary.connectErrorCount.tot = ssummary.connectErrorCount.tot + 1 + end + end + elseif etype == 'accept' then + local sport = evt.field(fsport) + if sport ~= nil then + ssummary.newConnectionsI.tot = ssummary.newConnectionsI.tot + 1 + if sport == 22 then + ssummary.newConnectionsSsh.tot = ssummary.newConnectionsSsh.tot + 1 + end + end + elseif etype == 'open' then + ssummary.openErrorCount.tot = ssummary.openErrorCount.tot + 1 + end + else + local etype = evt.field(fetype) + if etype == 'close' then + local sport = evt.field(fsport) + if sport ~= nil then + local typechar = evt.field(ftypechar) + if typechar == '2' then + if ssummary.listeningPortCount.table[sport] ~= nil then + ssummary.listeningPortCount.table[sport] = nil + ssummary.listeningPortCount.tot = ssummary.listeningPortCount.tot - 1 + end + end + end + elseif etype == 'signaldeliver' then + ssummary.signalCount.tot = ssummary.signalCount.tot + 1 + local signal = evt.field(fsignal) + if signal == 'SIGSEGV' then + ssummary.segfaultCount.tot = ssummary.segfaultCount.tot + 1 + end + elseif etype == 'notification' then + ssummary.notifications.tot = ssummary.notifications.tot + 1 + elseif etype == 'infra' then + local infrasource = evt.field(finfrasource) + if infrasource == 'docker' then + ssummary.dockerEvtsCount.tot = ssummary.dockerEvtsCount.tot + 1 + + local infraname = evt.field(finfraname) + update_docker_cats(infraname) + end + end + end + end + + return true +end + +------------------------------------------------------------------------------- +-- Periodic timeout callback +------------------------------------------------------------------------------- +function on_interval(ts_s, ts_ns, delta) + parse_thread_table_interval() + parse_container_table() + + if nintervals == 0 then + -- clean up events + collectgarbage() + end +--print(json.encode(ssummary.connectionCount, { indent = true })) + add_summaries(ts_s, ts_ns, gsummary, ssummary) + reset_summary(ssummary) + + if nintervals % percent_update_sample_period == 0 then + local progress = sysdig.get_read_progress() + if progress == 100 then + progress = 99.99 + end + print('{"progress": ' .. progress .. ' },') + io.flush(stdout) + end + + nintervals = nintervals + 1 + + return true +end + +------------------------------------------------------------------------------- +-- End of capture output generation +------------------------------------------------------------------------------- +function update_table_count(cat) + if cat.table ~= nil then + local cnt = 0 + for tk, tv in pairs(cat.table) do + cnt = cnt + 1 + end + + cat.tot = cnt + cat.table = nil + end +end + +function update_table_counts() + for k, v in pairs(gsummary) do + update_table_count(v) + end +end + +function should_include(category) + if category.excludable then + if category.tot ~= 0 then + return true + else + return false + end + else + return true + end +end + +function get_category_table(include_network_apps, include_security, include_performance, include_logs, include_infrastructure) + local res = { + {id='general', name='General'}, + {id='file', name='File'}, + {id='network', name='Network'}, + } + + if include_network_apps then + res[#res+1] = {id='napps', name='Network Apps'} + end + + if include_security then + res[#res+1] = {id='security', name='Security'} + end + + if include_performance then + res[#res+1] = {id='performance', name='Performance'} + end + + if include_logs then + res[#res+1] = {id='logs', name='Logs'} + end + + if include_infrastructure then + res[#res+1] = {id='infrastructure', name='Infrastructure'} + end + + return res +end + +function build_output(captureDuration) + local ctable = copytable(gsummary.containerCount.table) + local res = {} + local has_cat_logs = false; + local has_cat_infrastructure = false; + local has_cat_netapps = false; + + local jtable = { + info={ + IndexFormatVersion=index_format_version, + containers=ctable, + durationNs=captureDuration, + startTs = sysdig.get_firstevent_ts(), + endTs = sysdig.get_lastevent_ts() + }, + metrics=res} + local filter = sysdig.get_filter() + + update_table_counts() + + if should_include(gsummary.notifications) then + res[#res+1] = { + name = 'Sysdig Secure Notifications', + desc = 'Sysdig Secure notifications. Sysdig secure inserts a "notification" event in the capture stream each time a policy triggers. This metric counts the notifications. Chart it over time to compare the other metrics with the point in time where policies were triggered.', + category = 'general', + targetView = 'notifications', + drillDownKey = 'NONE', + data = gsummary.notifications + } + end + + if should_include(gsummary.procCount) then + res[#res+1] = { + name = 'Running Processes', + desc = 'Total number of processes that were running during the capture', + category = 'general', + targetView = 'procs', + drillDownKey = '', + data = gsummary.procCount + } + end + + if(not string.find(filter, 'container') and should_include(gsummary.containerCount)) then + res[#res+1] = { + name = 'Running Containers', + desc = 'Total number of containers that were running during the capture', + category = 'general', + targetView = 'containers', + drillDownKey = '', + data = gsummary.containerCount + } + end + + if(should_include(gsummary.syscallCount)) then + res[#res+1] = { + name = 'System Calls', + desc = 'Number of system calls performed by any process/container in the system', + category = 'general', + targetView = 'syscalls', + drillDownKey = '', + data = gsummary.syscallCount + } + end + + if should_include(gsummary.fileBytes) then + res[#res+1] = { + name = 'File Bytes In+Out', + desc = 'Amount of bytes read from or written to the file system', + category = 'file', + targetView = 'files', + drillDownKey = 'fd.directory', + targetViewSortingCol = 2, + data = gsummary.fileBytes + } + end + + if should_include(gsummary.fileBytesR) then + res[#res+1] = { + name = 'File Bytes In', + desc = 'Amount of bytes read from the file system', + category = 'file', + targetView = 'files', + drillDownKey = 'fd.directory', + targetViewSortingCol = 0, + data = gsummary.fileBytesR + } + end + + if should_include(gsummary.fileBytesW) then + res[#res+1] = { + name = 'File Bytes Out', + desc = 'Amount of bytes written to the file system', + category = 'file', + targetView = 'files', + drillDownKey = 'fd.directory', + targetViewSortingCol = 1, + data = gsummary.fileBytesW + } + end + + if should_include(gsummary.fileCount) then + res[#res+1] = { + name = 'Accessed Files', + desc = 'Number of files that have been accessed during the capture', + category = 'file', + targetView = 'files', + targetViewFilter = 'evt.is_io_read=true', + drillDownKey = 'fd.directory', + targetViewSortingCol = 2, + data = gsummary.fileCount + } + end + + if should_include(gsummary.fileCountW) then + res[#res+1] = { + name = 'Modified Files', + desc = 'Number of files that have been received writes during the capture', + category = 'file', + targetView = 'files', + drillDownKey = 'fd.directory', + targetViewSortingCol = 1, + targetViewFilter = 'evt.is_io_write=true', + data = gsummary.fileCountW + } + end + + if should_include(gsummary.netBytes) then + res[#res+1] = { + name = 'Net Bytes In+Out', + desc = 'Amount of bytes read from or written to the network', + category = 'network', + targetView = 'sports', + drillDownKey = 'fd.directory', + targetViewSortingCol = 4, + data = gsummary.netBytes + } + end + + if should_include(gsummary.netBytesR) then + res[#res+1] = { + name = 'Net Bytes In', + desc = 'Amount of bytes read from the network', + category = 'network', + targetView = 'sports', + drillDownKey = 'fd.sport', + targetViewSortingCol = 2, + data = gsummary.netBytesR + } + end + + if should_include(gsummary.netBytesW) then + res[#res+1] = { + name = 'Net Bytes Out', + desc = 'Amount of bytes written to the network', + category = 'network', + targetView = 'sports', + drillDownKey = 'fd.sport', + targetViewSortingCol = 3, + data = gsummary.netBytesW + } + end + + if should_include(gsummary.connectionCount) then + res[#res+1] = { + name = 'Active Network Connections', + desc = 'Number of network connections that have been accessed during the capture', + category = 'network', + targetView = 'connections', + targetViewFilter = 'evt.is_io=true', + drillDownKey = 'fd.sport', + targetViewSortingCol = 8, + data = gsummary.connectionCount + } + end + + if should_include(gsummary.listeningPortCount) then + res[#res+1] = { + name = 'Listening Ports', + desc = 'Number of open ports on this system', + category = 'network', + targetView = 'port_bindings', + drillDownKey = 'fd.sport', + data = gsummary.listeningPortCount + } + end + + if should_include(gsummary.newListeningPorts) then + res[#res+1] = { + name = 'New Listening Ports', + desc = 'Number of open ports that have been added during the observation interval', + category = 'network', + targetView = 'port_bindings', + drillDownKey = 'fd.sport', + data = gsummary.newListeningPorts + } + end + + if should_include(gsummary.newConnectionsO) then + res[#res+1] = { + name = 'New Outbound Connections', + desc = 'New client network connections', + category = 'network', + targetView = 'dig', + targetViewTitle = 'Connect events', + targetViewFilter = 'evt.type=connect and evt.dir=< and fd.sport exists', + drillDownKey = 'NONE', + data = gsummary.newConnectionsO + } + end + + if should_include(gsummary.newConnectionsI) then + res[#res+1] = { + name = 'New Inbound Connections', + desc = 'New server network connections', + category = 'network', + targetView = 'dig', + targetViewTitle = 'Connect events', + targetViewFilter = 'evt.type=accept and evt.dir=< and fd.sport exists', + drillDownKey = '', + data = gsummary.newConnectionsI + } + end + + if should_include(gsummary.executedCommands) then + res[#res+1] = { + name = 'Executed Commands', + desc = 'Number of new programs that have been executed during the observed interval', + category = 'security', + targetView = 'spy_users_wsysdig', + drillDownKey = '', + data = gsummary.executedCommands + } + end + + if should_include(gsummary.executedInteractiveCommands) then + res[#res+1] = { + name = 'Executed Interactive Commands', + desc = 'Number of new programs that have been executed from a shell during the observed interval', + category = 'security', + targetView = 'spy_users_wsysdig', + targetViewFilter = 'proc.pname=bash', + drillDownKey = 'NONE', + data = gsummary.executedInteractiveCommands + } + end + + if should_include(gsummary.newSymLinksCount) then + res[#res+1] = { + name = 'New Symlinks', + desc = 'Number of new symbolic links that were created', + category = 'security', + targetView = 'dig', + targetViewTitle = 'Symlink creations', + targetViewFilter = '(evt.type=symlink or evt.type=symlinkat) and evt.dir=< and evt.failed = false', + drillDownKey = 'NONE', + data = gsummary.newSymLinksCount + } + end + + if should_include(gsummary.sysFileCountW) then + res[#res+1] = { + name = 'Modified System Files', + desc = 'Number of files that have been accessed during the capture', + category = 'security', + targetViewSortingCol = 1, + targetView = 'files', + targetViewFilter = 'evt.is_io_write=true', + drillDownKey = 'NONE', + data = gsummary.sysFileCountW + } + end + + if should_include(gsummary.sudoInvocations) then + res[#res+1] = { + name = 'Sudo Invocations', + desc = 'Number of times the sudo program has been called', + category = 'security', + targetView = 'dig', + targetViewTitle = 'Sudo executions', + targetViewFilter = 'evt.type=execve and evt.arg.exe=sudo', + drillDownKey = 'NONE', + data = gsummary.sudoInvocations + } + end + + if should_include(gsummary.setnsInvocations) then + res[#res+1] = { + name = 'Setns Invocations', + desc = 'Number of times the setns system call has been called. Setns is typically used to "enter" in another container', + category = 'security', + targetView = 'dig', + targetViewTitle = 'Setns executions', + targetViewFilter = 'evt.type=setns', + drillDownKey = 'NONE', + data = gsummary.setnsInvocations + } + end + + if should_include(gsummary.newConnectionsSsh) then + res[#res+1] = { + name = 'New SSH Connections', + desc = 'Client or server connections', + category = 'security', + targetView = 'dig', + targetViewTitle = 'Connect events', + targetViewFilter = '(evt.type=accept or evt.type=connect) and evt.dir=< and fd.sport=22', + drillDownKey = '', + data = gsummary.newConnectionsSsh + } + end + + if should_include(gsummary.fileDeletionsCount) then + res[#res+1] = { + name = 'Deleted Files', + desc = 'Number of files that were deleted', + category = 'security', + targetView = 'dig', + targetViewTitle = 'File deletions', + targetViewFilter = 'evt.type=unlink or evt.type=unlinkat', + drillDownKey = 'NONE', + data = gsummary.fileDeletionsCount + } + end + + if should_include(gsummary.sysReqCountHttp) then + res[#res+1] = { + name = 'HTTP Requests', + desc = 'Number of HTTP requests', + category = 'performance', + targetView = 'echo', + targetViewTitle = 'HTTP responses', + targetViewFilter = '(fd.type=ipv4 or fd.type=ipv6) and evt.buffer contains "HTTP/"', + drillDownKey = 'fd.directory', + data = gsummary.sysReqCountHttp + } + end + + if should_include(gsummary.sysErrCountHttp) then + res[#res+1] = { + name = 'HTTP Errors', + desc = 'Number of HTTP responses with code different from 400', + category = 'performance', + targetView = 'echo', + targetViewTitle = 'HTTP responses', + targetViewFilter = '(fd.type=ipv4 or fd.type=ipv6) and evt.arg.data startswith "HTTP/" and not evt.arg.data contains "200"', + drillDownKey = 'fd.directory', + data = gsummary.sysErrCountHttp + } + end + + if should_include(gsummary.openErrorCount) then + res[#res+1] = { + name = 'File Open Errors', + desc = 'Count of failed file opens', + category = 'performance', + targetView = 'dig', + targetViewTitle = 'Failed open() calls', + targetViewFilter = 'evt.type=open and evt.rawres<0', + drillDownKey = 'fd.directory', + data = gsummary.openErrorCount + } + end + + if should_include(gsummary.forkCount) then + res[#res+1] = { + name = 'Fork Count', + desc = 'Count of processes and threads that have been created', + category = 'performance', + targetView = 'dig', + targetViewTitle = 'Clone executions', + targetViewFilter = 'evt.type=clone and evt.rawres=0', + drillDownKey = 'NONE', + data = gsummary.forkCount + } + end + + if should_include(gsummary.connectErrorCount) then + res[#res+1] = { + name = 'Failed Connection Attempts', + desc = 'Count of failed network connect calls', + category = 'performance', + targetView = 'dig', + targetViewTitle = 'Failed connect() calls', + targetViewFilter = 'evt.type=connect and (fd.type=ipv4 or fd.type=ipv6) and evt.rawres<0 and evt.res!=EINPROGRESS', + drillDownKey = 'NONE', + data = gsummary.connectErrorCount + } + end + + if should_include(gsummary.signalCount) then + res[#res+1] = { + name = 'Received Signals', + desc = 'Number of unix signals that have been received by the processes on the system', + category = 'performance', + targetView = 'dig', + targetViewTitle = 'Received signals', + targetViewFilter = 'evt.type=signaldeliver', + drillDownKey = 'NONE', + data = gsummary.signalCount + } + end + + if should_include(gsummary.segfaultCount) then + res[#res+1] = { + name = 'Segmentation Faults', + desc = 'Number of process segfaults', + category = 'performance', + targetView = 'dig', + targetViewTitle = 'List of segfault events', + targetViewFilter = 'evt.type=signaldeliver and evt.arg.sig=SIGSEV', + drillDownKey = 'NONE', + data = gsummary.segfaultCount + } + end + + if should_include(gsummary.over1msFileIoCount) then + res[#res+1] = { + name = 'Slow File I/O calls (1ms+)', + desc = 'Number of file read or write calls that took more than 1ms to return', + category = 'performance', + targetView = 'slow_io', + targetViewSortingCol = 1, + drillDownKey = 'NONE', + data = gsummary.over1msFileIoCount + } + end + + if should_include(gsummary.over10msFileIoCount) then + res[#res+1] = { + name = 'Slow File I/O calls (10ms+)', + desc = 'Number of file read or write calls that took more than 10ms to return', + category = 'performance', + targetView = 'slow_io', + targetViewSortingCol = 1, + drillDownKey = 'NONE', + data = gsummary.over10msFileIoCount + } + end + + if should_include(gsummary.over100msFileIoCount) then + res[#res+1] = { + name = 'Slow File I/O calls (100ms+)', + desc = 'Number of file read or write calls that took more than 100ms to return', + category = 'performance', + targetView = 'slow_io', + targetViewSortingCol = 1, + drillDownKey = 'NONE', + data = gsummary.over100msFileIoCount + } + end + + if should_include(gsummary.appLogCount) then + res[#res+1] = { + name = 'App Log Messages', + desc = 'Number of writes to application log files', + category = 'logs', + targetView = 'echo', + targetViewTitle = 'Application Log Messages', + targetViewFilter = '((fd.name contains .log or fd.name contains _log or fd.name contains /var/log) and not (fd.name contains .gz or fd.name contains .tgz)) and evt.is_io_write=true', + drillDownKey = 'NONE', + data = gsummary.appLogCount + } + has_cat_logs = true + end + + if should_include(gsummary.appLogCountW) then + res[#res+1] = { + name = 'App Log Warning Messages', + desc = 'Number of writes to application log files containing the word "warning"', + category = 'logs', + targetView = 'echo', + targetViewTitle = 'Warning Application Log Messages', + targetViewFilter = '((fd.name contains .log or fd.name contains _log or fd.name contains /var/log) and not (fd.name contains .gz or fd.name icontains .tgz)) and evt.is_io_write=true and evt.arg.data icontains warn', + drillDownKey = 'NONE', + data = gsummary.appLogCountW + } + has_cat_logs = true + end + + if should_include(gsummary.appLogCountE) then + res[#res+1] = { + name = 'App Log Error Messages', + desc = 'Number of writes to application log files containing the word "error"', + category = 'logs', + targetView = 'echo', + targetViewTitle = 'Error Application Log Messages', + targetViewFilter = '((fd.name contains .log or fd.name contains _log or fd.name contains /var/log) and not (fd.name contains .gz or fd.name contains .tgz)) and evt.is_io_write=true and (evt.arg.data icontains error or evt.arg.data icontains critic or evt.arg.data icontains emerg or evt.arg.data icontains alert)', + drillDownKey = 'NONE', + data = gsummary.appLogCountE + } + has_cat_logs = true + end + + if should_include(gsummary.sysLogCount) then + res[#res+1] = { + name = 'Syslog Messages', + desc = 'Number of entries written to syslog', + category = 'logs', + targetView = 'spy_syslog', + targetViewTitle = 'Syslog Messages', + drillDownKey = 'NONE', + data = gsummary.sysLogCount + } + has_cat_logs = true + end + + if should_include(gsummary.sysLogCountW) then + res[#res+1] = { + name = 'Syslog Warning Messages', + desc = 'Number of entries with severity WARNING written to syslog', + category = 'logs', + targetView = 'spy_syslog', + targetViewTitle = 'Syslog Messages', + targetViewFilter = 'syslog.severity=4', + drillDownKey = 'NONE', + data = gsummary.sysLogCountW + } + has_cat_logs = true + end + + if should_include(gsummary.sysLogCountE) then + res[#res+1] = { + name = 'Syslog Error Messages', + desc = 'Number of entries with severity ERROR or lower written to syslog', + category = 'logs', + targetView = 'spy_syslog', + targetViewTitle = 'Syslog Messages', + targetViewFilter = 'syslog.severity<4', + drillDownKey = 'NONE', + data = gsummary.sysLogCountE + } + has_cat_logs = true + end + + if should_include(gsummary.dockerEvtsCount) then + res[#res+1] = { + name = 'Docker Events', + desc = 'Total number of events generated by docker activity', + category = 'infrastructure', + targetView = 'docker_events', + drillDownKey = 'NONE', + data = gsummary.dockerEvtsCount + } + has_cat_infrastructure = true + end + + -- evaluate dynamic dockerEvtsCount* categories + prefix = 'dockerEvtsCount' + dockerEvtsCountEvents = {} + for ccat in pairs(gsummary) do + if starts_with(ccat, prefix) and ccat ~= prefix then + if should_include(gsummary[ccat]) then + ccat_name = ccat:sub(#prefix + 1) + dockerEvtsCountEvents[ccat] = { + name = ccat_name .. ' Events', + desc = 'Total number of docker events of type ' .. ccat_name, + category = 'infrastructure', + targetView = 'docker_events', + targetViewFilter = 'evt.arg.name="' .. ccat_name .. '"' , + drillDownKey = 'NONE', + data = gsummary[ccat] + } + has_cat_infrastructure = true + end + end + end + -- sort categories to make sure the final list is "stable" + table.sort(dockerEvtsCountEvents, function (a, b) return a.name - b.name end) + for i, v in pairs(dockerEvtsCountEvents) do + res[#res+1] = v + end + + for i, v in pairs(protocols) do + local ccat = 'protoBytes_' .. i + if should_include(gsummary[ccat]) then + local flt = '' + for ii, vv in pairs(v) do + flt = flt .. ('fd.sport=' .. vv .. ' or ') + end + flt = string.sub(flt, 0, #flt - 4) + + res[#res+1] = { + name = i .. ' Bytes', + desc = 'Total number of network bytes generated by the ' .. i .. ' protocol', + category = 'napps', + targetView = 'connections', + targetViewFilter = flt, + drillDownKey = 'NONE', + data = gsummary[ccat] + } + + has_cat_netapps = true + end + end + + jtable.info.categories = get_category_table(has_cat_netapps, true, true, has_cat_logs, has_cat_infrastructure) + + return jtable +end + +function load_index(dirname) + local f = io.open(dirname .. '/summary.json', "r") + if f == nil then + return nil + end + + local res = f:read("*all") + f:close() + + return res +end + +-- Callback called by the engine at the end of the capture +function on_capture_end(ts_s, ts_ns, delta) + if arg_file_duration == nil then + sysdig.run_sysdig('-r "' .. sysdig.get_evtsource_name() .. + '" -c wsysdig_summary ' .. arg_n_timeline_samples .. ',' .. delta .. ' ' .. + sysdig.get_filter()) + return true + end + + local sstr = '' + local dirname = sysdig.get_evtsource_name() .. '_wd_index' + + if file_cache_exists and not disable_index then + sstr = load_index(dirname) + if sstr == nil then + print('{"progress": 100, "error": "can\'t read the trace file index" }') + print(']}') + return false + end + + jtable = json.decode(sstr) + subsample_timelines(jtable) + sstr = json.encode(jtable, { indent = true }) + else + add_summaries(ts_s, ts_ns, gsummary, ssummary) + jtable = build_output(delta) + sstr = json.encode(jtable, { indent = true }) + + if not disable_index then + os.execute('rm -fr ' .. dirname .. " 2> /dev/null") + os.execute('rmdir ' .. dirname .. " 2> nul") + os.execute('mkdir ' .. dirname .. " 2> /dev/null") + os.execute('md ' .. dirname .. " 2> nul") + + -- Save the data + local f = io.open(dirname .. '/summary.json', "w") + if f == nil then + print('{"progress": 100, "error": "can\'t create the trace file index" }') + print(']}') + return false + end + + f:write(sstr) + f:close() + + -- Save the index version + local fv = io.open(dirname .. '/VERSION', "w") + if fv == nil then + print('{"progress": 100, "error": "can\'t create the trace file index" }') + print(']}') + return false + end + + fv:write(index_format_version) + fv:close() + end + + subsample_timelines(jtable) + sstr = json.encode(jtable, { indent = true }) + end + + print('{"progress": 100, "data": '.. sstr ..'}') + print(']}') + + return true +end diff --git a/userspace/sysdig/config.h.in b/userspace/sysdig/config.h.in deleted file mode 100644 index 56898088ee..0000000000 --- a/userspace/sysdig/config.h.in +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#define SYSDIG_VERSION "${SYSDIG_VERSION}" - -#define SYSDIG_INSTALLATION_DIR "${CMAKE_INSTALL_PREFIX}" diff --git a/userspace/sysdig/config_sysdig.h.in b/userspace/sysdig/config_sysdig.h.in new file mode 100644 index 0000000000..ed9022956b --- /dev/null +++ b/userspace/sysdig/config_sysdig.h.in @@ -0,0 +1,23 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ +#pragma once + +#define SYSDIG_VERSION "${SYSDIG_VERSION}" + +#define SYSDIG_INSTALLATION_DIR "${CMAKE_INSTALL_PREFIX}" diff --git a/userspace/sysdig/csysdig.cpp b/userspace/sysdig/csysdig.cpp new file mode 100644 index 0000000000..42af462b99 --- /dev/null +++ b/userspace/sysdig/csysdig.cpp @@ -0,0 +1,1051 @@ +/* +Copyright (C) 2013-2018 Draios Inc dba Sysdig. + +This file is part of sysdig. + +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. + +*/ + +#define __STDC_FORMAT_MACROS + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "chisel.h" +#ifdef HAS_CAPTURE +#include "driver_config.h" +#endif // HAS_CAPTURE +#include "sysdig.h" +#include "table.h" +#include "utils.h" + +#ifdef _WIN32 +#include "win32/getopt.h" +#include +#else +#include +#include +#include +#endif + +#include "cursescomponents.h" +#include "cursestable.h" +#include "cursesui.h" +#include "scap_open_exception.h" +#include "sinsp_capture_interrupt_exception.h" + +#define MOUSE_CAPABLE_TERM "xterm-1003" +#define MOUSE_CAPABLE_TERM_COMPAT "xterm-1002" + +static bool g_terminate = false; +static void usage(); + +// +// Helper functions +// +static void signal_callback(int signal) +{ + g_terminate = true; +} + +// +// Program help +// +static void usage() +{ + printf( +"csysdig version " SYSDIG_VERSION "\n" +"Usage: csysdig [options] [filter]\n\n" +"Options:\n" +" -A, --print-ascii When emitting JSON, only print the text portion of data buffers, and echo\n" +" end-of-lines. This is useful to only display human-readable\n" +" data.\n" +" -B, --bpf=\n" +" Enable live capture using the specified BPF probe instead of the kernel module.\n" +" The BPF probe can also be specified via the environment variable\n" +" SYSDIG_BPF_PROBE. If is left empty, sysdig will\n" +" try to load one from the sysdig-probe-loader script.\n" +#ifdef HAS_CAPTURE +" --cri Path to CRI socket for container metadata\n" +" Use the specified socket to fetch data from a CRI-compatible runtime\n" +"\n" +" --cri-timeout \n" +" Wait at most milliseconds for response from CRI\n" +#endif +" -d , --delay=\n" +" Set the delay between updates, in milliseconds. This works\n" +" similarly to the -d option in top.\n" +" -E, --exclude-users\n" +" Don't create the user/group tables by querying the OS when\n" +" sysdig starts. This also means that no user or group info\n" +" will be written to the tracefile by the -w flag.\n" +" The user/group tables are necessary to use filter fields\n" +" like user.name or group.name. However, creating them can\n" +" increase sysdig's startup time. Moreover, they contain\n" +" information that could be privacy sensitive.\n" +" --force-term-compat\n" +" Try to configure simple terminal settings (xterm-1002) that work\n" +" better with terminals like putty. Try to use this flag if you experience\n" +" terminal issues like the mouse not working.\n" +" -h, --help Print this page\n" +" -k , --k8s-api=\n" +" Enable Kubernetes support by connecting to the API server\n" +" specified as argument. E.g. \"http://admin:password@127.0.0.1:8080\".\n" +" The API server can also be specified via the environment variable\n" +" SYSDIG_K8S_API.\n" +" -K | :[:], --k8s-api-cert= | :[:]\n" +" Use the provided files names to authenticate user and (optionally) verify the K8S API\n" +" server identity.\n" +" Each entry must specify full (absolute, or relative to the current directory) path\n" +" to the respective file.\n" +" Private key password is optional (needed only if key is password protected).\n" +" CA certificate is optional. For all files, only PEM file format is supported. \n" +" Specifying CA certificate only is obsoleted - when single entry is provided \n" +" for this option, it will be interpreted as the name of a file containing bearer token.\n" +" Note that the format of this command-line option prohibits use of files whose names contain\n" +" ':' or '#' characters in the file name.\n" +" Option can also be provided via the environment variable SYSDIG_K8S_API_CERT.\n" +" -l, --list List all the fields that can be used in views.\n" +" --large-environment\n" +" Support environments larger than 4KiB\n" +" When the environment is larger than 4KiB, load the whole\n" +" environment from /proc instead of truncating to the first 4KiB\n" +" This may fail for short-lived processes and in that case\n" +" the truncated environment is used instead.\n" +" --logfile=\n" +" Print program logs into the given file.\n" +" -n , --numevents=\n" +" Stop capturing after events\n" +" --page-faults Capture user/kernel major/minor page faults\n" +" -pc, -pcontainer\n" +" Instruct csysdig to use a container-friendly format in its\n" +" views.\n" +" This will cause several of the views to contain additional\n" +" container-related columns.\n" +" -R Resolve port numbers to names.\n" +" -r , --read=\n" +" Read the events from .\n" +" --raw Print raw output on a regular terminal instead of enabling\n" +" ncurses-based ANSI output.\n" +" -s , --snaplen=\n" +" Capture the first bytes of each I/O buffer.\n" +" By default, the first 80 bytes are captured. Use this\n" +" option with caution, it can generate huge trace files.\n" +" -T, --force-tracers-capture\n" +" Tell the driver to make sure full buffers are captured from\n" +" /dev/null, to make sure that tracers are completely\n" +" captured. Note that sysdig will enable extended /dev/null\n" +" capture by itself after detecting that tracers are written\n" +" there, but that could result in the truncation of some\n" +" tracers at the beginning of the capture. This option allows\n" +" preventing that.\n" +" -v , --view=\n" +" Run the view with the given ID when csysdig starts.\n" +" View IDs can be found in the view documentation pages in\n" +" csysdig. Combine this option with a command line filter for\n" +" complete output customization.\n" +" --version Print version number.\n" +" -X, --print-hex-ascii\n" +" When emitting JSON, print data buffers in hex and ASCII.\n" +"\n" +"How to use csysdig:\n" +"1. you can either see real time data, or analyze a trace file by using the -r\n" +" command line flag.\n" +"2. you can switch to a different view by using the F2 key.\n" +"3. You can drill down into a selection by typing enter.\n" +" You can navigate back by typing backspace.\n" +"4. you can observe reads and writes (F5) or see sysdig events (F6) for any\n" +" selection.\n" +"\nAdditional help can be obtained by clicking F1 while the program is running,\n" +"and in the man page.\n\n" + ); +} + +#ifdef HAS_CHISELS +static void add_chisel_dirs(sinsp* inspector) +{ + // + // Add the default chisel directory statically configured by the build system + // + inspector->add_chisel_dir(SYSDIG_INSTALLATION_DIR CHISELS_INSTALLATION_DIR, false); + + // + // Add the directories configured in the SYSDIG_CHISEL_DIR environment variable + // + char* s_user_cdirs = getenv("SYSDIG_CHISEL_DIR"); + + if(s_user_cdirs != NULL) + { + vector user_cdirs = sinsp_split(s_user_cdirs, ';'); + + for(uint32_t j = 0; j < user_cdirs.size(); j++) + { + inspector->add_chisel_dir(user_cdirs[j], true); + } + } +} + +static void print_views(sinsp_view_manager* view_manager) +{ + Json::FastWriter writer; + Json::Value root; + + vector* vlist = view_manager->get_views(); + + for(auto it = vlist->begin(); it != vlist->end(); ++it) + { + Json::Value jv; + sinsp_view_info& vinfo = *it; + + jv["id"] = vinfo.m_id; + jv["name"] = vinfo.m_name; + jv["description"] = vinfo.m_description; + jv["isRoot"] = vinfo.m_is_root; + jv["drilldownTarget"] = vinfo.m_drilldown_target; + jv["filter"] = vinfo.m_filter; + jv["canDrillDown"] = (vinfo.m_type == sinsp_view_info::T_TABLE); + + for(auto it = vinfo.m_applies_to.begin(); it != vinfo.m_applies_to.end(); ++it) + { + jv["appliesTo"].append(*it); + } + for(auto it = vinfo.m_tags.begin(); it != vinfo.m_tags.end(); ++it) + { + jv["tags"].append(*it); + } + for(auto it = vinfo.m_tips.begin(); it != vinfo.m_tips.end(); ++it) + { + jv["tips"].append(*it); + } + + root.append(jv); + } + + string output = writer.write(root); + printf("%s", output.substr(0, output.size() - 1).c_str()); +} +#endif + +captureinfo do_inspect(sinsp* inspector, + uint64_t cnt, + sinsp_cursesui* ui) +{ + captureinfo retval; + int32_t res; + sinsp_evt* ev; + + // + // Loop through the events + // + while(1) + { + if(retval.m_nevts == cnt || g_terminate) + { + // + // End of capture, either because the user stopped it, or because + // we reached the event count specified with -n. + // + break; + } + + res = inspector->next(&ev); + + if(res == SCAP_TIMEOUT) + { + continue; + } + else if(res != SCAP_EOF && res != SCAP_SUCCESS) + { + // + // Event read error. + // Notify the chisels that we're exiting, and then die with an error. + // + if(inspector->is_live()) + { + throw sinsp_exception(inspector->getlasterr()); + } + else + { + ui->set_truncated_input(true); + res = SCAP_EOF; + } + } + + if(ui->process_event(ev, res) == true) + { + return retval; + } + + retval.m_nevts++; + } + + return retval; +} + +string g_version_string = SYSDIG_VERSION; + +sysdig_init_res csysdig_init(int argc, char **argv) +{ + sysdig_init_res res; + sinsp* inspector = NULL; + vector infiles; + int op; + uint64_t cnt = -1; + uint32_t snaplen = 0; + int long_index = 0; + int32_t n_filterargs = 0; + captureinfo cinfo; + string errorstr; + string display_view; + bool print_containers = false; + uint64_t refresh_interval_ns = 2000000000; + bool list_flds = false; + bool is_interactive = false; + int32_t json_first_row = 0; + int32_t json_last_row = 0; + int32_t sorting_col = -1; + bool list_views = false; + bool bpf = false; + string bpf_probe; +#ifdef HAS_CAPTURE + string cri_socket_path; +#endif + +#ifndef _WIN32 + sinsp_table::output_type output_type = sinsp_table::OT_CURSES; +#else + sinsp_table::output_type output_type = sinsp_table::OT_JSON; +#endif + string* k8s_api = 0; + string* k8s_api_cert = 0; + string* mesos_api = 0; + bool terminal_with_mouse = false; + bool force_tracers_capture = false; + bool force_term_compat = false; + sinsp_evt::param_fmt event_buffer_format = sinsp_evt::PF_NORMAL; + bool page_faults = false; + + static struct option long_options[] = + { + {"print-ascii", no_argument, 0, 'A' }, + {"bpf", optional_argument, 0, 'B' }, +#ifdef HAS_CAPTURE + {"cri", required_argument, 0, 0 }, + {"cri-timeout", required_argument, 0, 0 }, +#endif + {"delay", required_argument, 0, 'd' }, + {"exclude-users", no_argument, 0, 'E' }, + {"from", required_argument, 0, 0 }, + {"help", no_argument, 0, 'h' }, + {"k8s-api", required_argument, 0, 'k'}, + {"k8s-api-cert", required_argument, 0, 'K' }, + {"json", no_argument, 0, 'j' }, + {"interactive", optional_argument, 0, 0 }, + {"large-environment", no_argument, 0, 0 }, + {"list", optional_argument, 0, 'l' }, + {"list-views", no_argument, 0, 0}, + {"mesos-api", required_argument, 0, 'm'}, + {"numevents", required_argument, 0, 'n' }, + {"page-faults", no_argument, 0, 0 }, + {"print", required_argument, 0, 'p' }, + {"resolve-ports", no_argument, 0, 'R'}, + {"readfile", required_argument, 0, 'r' }, + {"raw", no_argument, 0, 0 }, + {"snaplen", required_argument, 0, 's' }, + {"logfile", required_argument, 0, 0 }, + {"force-tracers-capture", required_argument, 0, 'T'}, + {"force-term-compat", no_argument, 0, 0}, + {"sortingcol", required_argument, 0, 0 }, + {"to", required_argument, 0, 0 }, + {"view", required_argument, 0, 'v' }, + {"version", no_argument, 0, 0 }, + {"print-hex-ascii", no_argument, 0, 'X'}, + {0, 0, 0, 0} + }; + + // + // Parse the arguments + // + try + { + inspector = new sinsp(); + +#ifdef HAS_CHISELS + add_chisel_dirs(inspector); +#endif + + // + // Parse the args + // + while((op = getopt_long(argc, argv, + "AB::d:Ehk:K:jlm:n:p:Rr:s:Tv:X", long_options, &long_index)) != -1) + { + switch(op) + { + case '?': + // + // Command line error + // + throw sinsp_exception("command line error"); + break; + case 'A': + if(event_buffer_format != sinsp_evt::PF_NORMAL) + { + fprintf(stderr, "you cannot specify more than one output format\n"); + delete inspector; + return sysdig_init_res(EXIT_SUCCESS); + } + + event_buffer_format = sinsp_evt::PF_EOLS_COMPACT; + break; + case 'B': + { + bpf = true; + if(optarg) + { + bpf_probe = optarg; + } + break; + } + case 'd': + try + { + refresh_interval_ns = sinsp_numparser::parseu64(optarg) * 1000000; + } + catch(...) + { + throw sinsp_exception("can't parse the -d argument, make sure it's a number"); + } + + if(refresh_interval_ns < 100000000) + { + throw sinsp_exception("Period must be bigger then 100ms"); + } + + break; + case 'E': + inspector->set_import_users(false); + break; + case 'h': + usage(); + delete inspector; + return sysdig_init_res(EXIT_SUCCESS); + case 'k': + k8s_api = new string(optarg); + break; + case 'K': + k8s_api_cert = new string(optarg); + break; + case 'j': + output_type = sinsp_table::OT_JSON; + break; + case 'l': + list_flds = true; + break; + case 'm': + mesos_api = new string(optarg); + break; + case 'n': + try + { + cnt = sinsp_numparser::parseu64(optarg); + } + catch(...) + { + throw sinsp_exception("can't parse the -n argument, make sure it's a number"); + } + + if(cnt <= 0) + { + throw sinsp_exception(string("invalid event count ") + optarg); + res.m_res = EXIT_FAILURE; + goto exit; + } + break; + case 'p': + if(string(optarg) == "c" || string(optarg) == "container") + { + inspector->set_print_container_data(true); + print_containers = true; + } + + break; + case 'R': + inspector->set_hostname_and_port_resolution_mode(true); + break; + case 'r': + infiles.push_back(optarg); + k8s_api = new string(); + mesos_api = new string(); + break; + case 's': + snaplen = atoi(optarg); + break; + case 'T': + force_tracers_capture = true; + break; + case 'v': + display_view = optarg; + break; + case 'X': + if(event_buffer_format != sinsp_evt::PF_NORMAL) + { + fprintf(stderr, "you cannot specify more than one output format\n"); + delete inspector; + return sysdig_init_res(EXIT_FAILURE); + } + + event_buffer_format = sinsp_evt::PF_HEXASCII; + break; + case 0: + { + if(long_options[long_index].flag != 0) + { + break; + } + + string optname = string(long_options[long_index].name); + if(optname == "version") + { + printf("sysdig version %s\n", SYSDIG_VERSION); + delete inspector; + return sysdig_init_res(EXIT_SUCCESS); + } + else if(optname == "interactive") + { + is_interactive = true; + output_type = sinsp_table::OT_JSON; + } + else if(optname == "large-environment") + { + inspector->set_large_envs(true); + } +#ifdef HAS_CAPTURE + else if(optname == "cri") + { + cri_socket_path = optarg; + } + else if(optname == "cri-timeout") + { + inspector->set_cri_timeout(sinsp_numparser::parsed64(optarg)); + } +#endif + else if(optname == "logfile") + { + inspector->set_log_file(optarg); + } + else if(optname == "raw") + { + output_type = sinsp_table::OT_RAW; + } + else if(optname == "force-term-compat") + { + force_term_compat = true; + } + else if(optname == "from") + { + json_first_row = sinsp_numparser::parsed32(optarg); + } + else if(optname == "to") + { + json_last_row = sinsp_numparser::parsed32(optarg); + } + else if(optname == "sortingcol") + { + sorting_col = sinsp_numparser::parsed32(optarg); + } + else if(optname == "list-views") + { + list_views = true; + } + else if(optname == "page-faults") + { + page_faults = true; + } + } + break; + default: + break; + } + } + +#ifdef HAS_CAPTURE + if(!cri_socket_path.empty()) + { + inspector->set_cri_socket_path(cri_socket_path); + } +#endif + + string filter; + + // + // If -l was specified, print the fields and exit + // + if(list_flds) + { + list_fields(false, false); + + res.m_res = EXIT_SUCCESS; + goto exit; + } + + // + // the filter is at the end of the command line + // + if(optind + n_filterargs < argc) + { +#ifdef HAS_FILTERING + for(int32_t j = optind + n_filterargs; j < argc; j++) + { + filter += argv[j]; + if(j < argc) + { + filter += " "; + } + } +#else + fprintf(stderr, "filtering not compiled.\n"); + res.m_res = EXIT_FAILURE; + goto exit; +#endif + } + + if(!bpf) + { + const char *probe = scap_get_bpf_probe_from_env(); + if(probe) + { + bpf = true; + bpf_probe = probe; + } + } + + if(bpf) + { + inspector->set_bpf_probe(bpf_probe); + } + + if(signal(SIGINT, signal_callback) == SIG_ERR) + { + fprintf(stderr, "An error occurred while setting SIGINT signal handler.\n"); + res.m_res = EXIT_FAILURE; + goto exit; + } + + if(signal(SIGTERM, signal_callback) == SIG_ERR) + { + fprintf(stderr, "An error occurred while setting SIGTERM signal handler.\n"); + res.m_res = EXIT_FAILURE; + goto exit; + } + + if(json_last_row < json_first_row) + { + fprintf(stderr, "'to' argument cannot be smaller than the 'from' one.\n"); + res.m_res = EXIT_FAILURE; + goto exit; + } + + // + // Initialize ncurses + // +#ifndef NOCURSESUI + if(output_type == sinsp_table::OT_CURSES) + { + // + // Check if terminal has mouse support + // + const char* mct = force_term_compat? MOUSE_CAPABLE_TERM_COMPAT : MOUSE_CAPABLE_TERM; + terminal_with_mouse = (tgetent(NULL, mct) != 0); + + if(terminal_with_mouse) + { + // + // Enable fine-grained mouse activity capture by setting xterm-1002 + // + setenv("TERM", mct, 1); + } + + (void) initscr(); // initialize the curses library + (void) nonl(); // tell curses not to do NL->CR/NL on output + intrflush(stdscr, false); + keypad(stdscr, true); + curs_set(0); + if(has_colors()) + { + start_color(); + } + use_default_colors(); + mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL); + noecho(); + + timeout(0); + + // If this is uncommented, it's possible to natively handle stuff like CTRL+c + //raw(); + } +#endif + + // + // Create the list of views + // + sinsp_view_manager view_manager; + + // + // Scan the chisel list to load the Lua views, and add them to the list + // + vector chlist; + sinsp_chisel::get_chisel_list(&chlist); + + for(auto it : chlist) + { + if(it.m_viewinfo.m_valid) + { + if(print_containers) + { + it.m_viewinfo.apply_tag("containers"); + } + else + { + it.m_viewinfo.apply_tag("default"); + } + + if(it.m_viewinfo.m_tags.size() != 0) + { + if(it.m_viewinfo.m_tags[0] == "Containers") + { + continue; + } + } + + if(output_type != sinsp_table::OT_JSON) + { + if(std::find(it.m_viewinfo.m_tags.begin(), + it.m_viewinfo.m_tags.end(), + "nocsysdig") != it.m_viewinfo.m_tags.end()) + { + continue; + } + } + + view_manager.add(&it.m_viewinfo); + } + } + + // + // Set the initial display view + // + view_manager.set_selected_view(display_view); + + if(list_views) + { + print_views(&view_manager); + goto exit; + } + + // + // Go through the input sources and apply the processing to all of them + // + for(uint32_t j = 0; j < infiles.size() || infiles.size() == 0; j++) + { + // + // Initialize the UI + // + sinsp_cursesui ui(inspector, + (infiles.size() != 0)? infiles[0] : "", + (filter.size() != 0)? filter : "", + refresh_interval_ns, + print_containers, + output_type, + terminal_with_mouse, + json_first_row, + json_last_row, + sorting_col, + event_buffer_format); + + ui.configure(&view_manager); + + if(display_view == "dig" || display_view == "echo") + { + ui.start(false, true); + } + else + { + ui.start(false, false); + } + + if(is_interactive) + { + printf("ready\n"); + + // + // In interactive mode, make sure stderr is flushed at every printf + // + setbuf(stderr, NULL); + + // + // Set the UI in interactive mode and start listening to user + // input. + // + ui.set_interactive(true); + } + + // + // Launch the capture + // + if(infiles.size() != 0) + { + // + // We have a file to open + // + inspector->open(infiles[j]); + } + else + { + if(j > 0) + { + break; + } + + // + // No file to open, this is a live capture + // +#if defined(HAS_CAPTURE) + bool open_success = true; + + try + { + inspector->open(""); + } + catch(const sinsp_exception& e) + { + open_success = false; + } + + // + // Starting the live capture failed, try to load the driver with + // modprobe. + // + if(!open_success) + { + open_success = true; + + if(bpf) + { + if(bpf_probe.empty()) + { + if(system("sysdig-probe-loader bpf")) + { + fprintf(stderr, "Unable to load the BPF probe\n"); + } + } + } + else + { + if(system("modprobe " PROBE_NAME " > /dev/null 2> /dev/null")) + { + fprintf(stderr, "Unable to load the driver\n"); + } + } + + inspector->open(""); + } +#else + // + // Starting live capture + // If this fails on Windows and OSX, don't try with any driver + // + inspector->open(""); +#endif + + // + // Enable gathering the CPU from the kernel module + // + inspector->set_get_procs_cpu_from_driver(true); + } + + // + // If required, set the snaplen + // + if(snaplen != 0) + { + inspector->set_snaplen(snaplen); + } + + // + // If required, tell the driver to enable tracers capture + // + if(force_tracers_capture) + { + inspector->enable_tracers_capture(); + } + + if(page_faults) + { + inspector->enable_page_faults(); + } + + // + // run k8s, if required + // + if(k8s_api) + { + if(!k8s_api_cert) + { + if(char* k8s_cert_env = getenv("SYSDIG_K8S_API_CERT")) + { + k8s_api_cert = new string(k8s_cert_env); + } + } + inspector->init_k8s_client(k8s_api, k8s_api_cert); + k8s_api = 0; + k8s_api_cert = 0; + } + else if(char* k8s_api_env = getenv("SYSDIG_K8S_API")) + { + if(k8s_api_env != NULL) + { + if(!k8s_api_cert) + { + if(char* k8s_cert_env = getenv("SYSDIG_K8S_API_CERT")) + { + k8s_api_cert = new string(k8s_cert_env); + } + } + k8s_api = new string(k8s_api_env); + inspector->init_k8s_client(k8s_api, k8s_api_cert); + } + else + { + delete k8s_api; + delete k8s_api_cert; + } + k8s_api = 0; + k8s_api_cert = 0; + } + + // + // run mesos, if required + // + if(mesos_api) + { + inspector->init_mesos_client(mesos_api); + } + else if(char* mesos_api_env = getenv("SYSDIG_MESOS_API")) + { + if(mesos_api_env != NULL) + { + mesos_api = new string(mesos_api_env); + inspector->init_mesos_client(mesos_api); + } + } + delete mesos_api; + mesos_api = 0; + + if(output_type == sinsp_table::OT_JSON) + { + printf("{\"slices\": [\n"); + if(display_view != "dig" && display_view != "echo") + { + printf("{\"progress\": 0},\n"); + } + } + + // + // Start the capture loop + // + cinfo = do_inspect(inspector, + cnt, + &ui); + + if(output_type == sinsp_table::OT_JSON) + { + printf("]}\n"); + //printf("%c", EOF); + } + + // + // Done. Close the capture. + // + inspector->close(); + } + } + catch(const sinsp_capture_interrupt_exception&) + { + } + catch(const scap_open_exception& e) + { + errorstr = e.what(); + res.m_res = e.scap_rc(); + } + catch(const std::exception& e) + { + errorstr = e.what(); + res.m_res = EXIT_FAILURE; + } + catch(...) + { + errorstr = "uncaught exception"; + res.m_res = EXIT_FAILURE; + } + +exit: + if(inspector) + { + delete inspector; + } + + // + // Restore the original screen + // +#ifndef NOCURSESUI + if(output_type == sinsp_table::OT_CURSES) + { + endwin(); + } +#endif + + if(errorstr != "") + { + cerr << errorstr << endl; + } + + return res; +} + +// +// MAIN +// +int main(int argc, char **argv) +{ + sysdig_init_res res; + + // + // Run csysdig + // + res = csysdig_init(argc, argv); + +#ifdef _WIN32 + _CrtDumpMemoryLeaks(); +#endif + + return res.m_res; +} diff --git a/userspace/sysdig/fields_info.cpp b/userspace/sysdig/fields_info.cpp index b315b265dc..8dd6d193b5 100644 --- a/userspace/sysdig/fields_info.cpp +++ b/userspace/sysdig/fields_info.cpp @@ -1,23 +1,24 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ // -// Variuos helper functions to render stuff on the screen +// Various helper functions to render stuff on the screen // #define __STDC_FORMAT_MACROS #include @@ -26,19 +27,27 @@ along with sysdig. If not, see . #include #include -#include "sysdig.h" #include "chisel.h" +#include "sysdig.h" + +// Must match the value in the zsh tab completion +#define DESCRIPTION_TEXT_START 16 + -#define DESCRIPTION_TEXT_START 18 #define CONSOLE_LINE_LEN 79 #define PRINTF_WRAP_CPROC(x) #x #define PRINTF_WRAP(x) PRINTF_WRAP_CPROC(x) -void list_fields(bool verbose) +void list_fields(bool verbose, bool markdown, bool names_only) { uint32_t j, l, m; int32_t k; + if(markdown && !names_only) + { + printf("# Sysdig Filter Fields List\n\n"); + } + vector fc_plugins; sinsp::get_filtercheck_fields_info(&fc_plugins); @@ -46,159 +55,94 @@ void list_fields(bool verbose) { const filter_check_info* fci = fc_plugins[j]; - printf("\n----------------------\n"); - printf("Field Class: %s\n\n", fci->m_name.c_str()); - - for(k = 0; k < fci->m_nfiedls; k++) + if(fci->m_flags & filter_check_info::FL_HIDDEN) { - const filtercheck_field_info* fld = &fci->m_fields[k]; + continue; + } - printf("%s", fld->m_name); - uint32_t namelen = strlen(fld->m_name); + if(!names_only) + { + if(markdown) + { + printf("## Filter Class: %s\n\n", fci->m_name.c_str()); + } + else + { + printf("\n----------------------\n"); + printf("Field Class: %s\n\n", fci->m_name.c_str()); + } + } - ASSERT(namelen < DESCRIPTION_TEXT_START); + for(k = 0; k < fci->m_nfields; k++) + { + const filtercheck_field_info* fld = &fci->m_fields[k]; - for(l = 0; l < DESCRIPTION_TEXT_START - namelen; l++) + if(fld->m_flags & EPF_TABLE_ONLY) { - printf(" "); + continue; } - - string desc; - if(verbose) + if(names_only) { - desc = string(fld->m_description) + " Type:" + param_type_to_string(fld->m_type) + "."; + printf("%s\n", fld->m_name); } - else + else if(markdown) { - desc = string(fld->m_description); + printf("**Name**: %s \n", fld->m_name); + printf("**Description**: %s \n", fld->m_description); + printf("**Type**: %s \n\n", param_type_to_string(fld->m_type)); } - - size_t desclen = desc.size(); - - for(l = 0; l < desclen; l++) + else { - if(l % (CONSOLE_LINE_LEN - DESCRIPTION_TEXT_START) == 0 && l != 0) + printf("%s", fld->m_name); + uint32_t namelen = (uint32_t)strlen(fld->m_name); + + if(namelen >= DESCRIPTION_TEXT_START) { printf("\n"); + namelen = 0; + } + + for(l = 0; l < DESCRIPTION_TEXT_START - namelen; l++) + { + printf(" "); + } + + string desc(fld->m_description); + + if(fld->m_flags & EPF_FILTER_ONLY) + { + desc = "(FILTER ONLY) " + desc; + } - for(m = 0; m < DESCRIPTION_TEXT_START; m++) + if(verbose) + { + desc += string(" Type:") + param_type_to_string(fld->m_type) + "."; + } + + size_t desclen = desc.size(); + + for(l = 0; l < desclen; l++) + { + if(l % (CONSOLE_LINE_LEN - DESCRIPTION_TEXT_START) == 0 && l != 0) { - printf(" "); + printf("\n"); + + for(m = 0; m < DESCRIPTION_TEXT_START; m++) + { + printf(" "); + } } + + printf("%c", desc[l]); } - printf("%c", desc[l]); + printf("\n"); } - - printf("\n"); } } } -const char* param_type_to_string(ppm_param_type pt) -{ - switch(pt) - { - case PT_NONE: - return "NONE"; - break; - case PT_INT8: - return "INT8"; - break; - case PT_INT16: - return "INT16"; - break; - case PT_INT32: - return "INT32"; - break; - case PT_INT64: - return "INT64"; - break; - case PT_UINT8: - return "UINT8"; - break; - case PT_UINT16: - return "UINT16"; - break; - case PT_UINT32: - return "UINT32"; - break; - case PT_UINT64: - return "UINT64"; - break; - case PT_CHARBUF: - return "CHARBUF"; - break; - case PT_BYTEBUF: - return "BYTEBUF"; - break; - case PT_ERRNO: - return "ERRNO"; - break; - case PT_SOCKADDR: - return "SOCKADDR"; - break; - case PT_SOCKTUPLE: - return "SOCKTUPLE"; - break; - case PT_FD: - return "FD"; - break; - case PT_PID: - return "PID"; - break; - case PT_FDLIST: - return "FDLIST"; - break; - case PT_FSPATH: - return "FSPATH"; - break; - case PT_SYSCALLID: - return "SYSCALLID"; - break; - case PT_SIGTYPE: - return "SIGTYPE"; - break; - case PT_RELTIME: - return "RELTIME"; - break; - case PT_ABSTIME: - return "ABSTIME"; - break; - case PT_PORT: - return "PORT"; - break; - case PT_L4PROTO: - return "L4PROTO"; - break; - case PT_SOCKFAMILY: - return "SOCKFAMILY"; - break; - case PT_BOOL: - return "BOOL"; - break; - case PT_IPV4ADDR: - return "IPV4ADDR"; - break; - case PT_DYN: - return "DYNAMIC"; - break; - case PT_FLAGS8: - return "FLAGS8"; - break; - case PT_FLAGS16: - return "FLAGS16"; - break; - case PT_FLAGS32: - return "FLAGS32"; - break; - default: - ASSERT(false); - return ""; - } -} - void list_events(sinsp* inspector) { uint32_t j, k; @@ -212,7 +156,7 @@ void list_events(sinsp* inspector) const struct ppm_event_info ei = etable[j]; char dir = (PPME_IS_ENTER(j))? '>' : '<'; - if(ei.flags & EF_UNUSED) + if((ei.flags & EF_UNUSED) || (ei.flags & EF_OLD_VERSION) || (ei.category & EC_INTERNAL)) { continue; } @@ -234,6 +178,7 @@ void list_events(sinsp* inspector) } } +#ifdef HAS_CHISELS struct summary_chisel_comparer { bool operator() (const chisel_desc& first, const chisel_desc& second) const @@ -252,7 +197,7 @@ void print_chisel_info(chisel_desc* cd) std::vector chlist; chlist.push_back(cd[0]); - list_chisels(&chlist); + list_chisels(&chlist, false); // Now we have to do the real work printf("\n"); @@ -265,7 +210,7 @@ void print_chisel_info(chisel_desc* cd) for(l = 0; l < desclen; l++) { - if(l % (CONSOLE_LINE_LEN - DESCRIPTION_TEXT_START) == 0 && l != 0) + if(l % CONSOLE_LINE_LEN == 0 && l != 0) { printf("\n"); } @@ -275,7 +220,7 @@ void print_chisel_info(chisel_desc* cd) printf("\n"); - astr += "\nArgs:\n"; + astr += "Args:\n"; if(cd->m_args.size() != 0) { @@ -293,10 +238,13 @@ void print_chisel_info(chisel_desc* cd) } size_t astrlen = astr.size(); + int linepos = 0; - for(l = 0; l < astrlen; l++) + for(l = 0; l < astrlen; l++, linepos++) { - if(l % (CONSOLE_LINE_LEN - DESCRIPTION_TEXT_START) == 0 && l != 0) + if(astr[l] == '\n') + linepos = -1; + else if(linepos % (CONSOLE_LINE_LEN - DESCRIPTION_TEXT_START) == 0 && linepos != 0) { printf("\n%" PRINTF_WRAP(DESCRIPTION_TEXT_START) "s", ""); } @@ -304,11 +252,11 @@ void print_chisel_info(chisel_desc* cd) printf("%c", astr[l]); } - // just for good meaure + // just for good measure printf("\n"); } -void list_chisels(vector* chlist) +void list_chisels(vector* chlist, bool verbose) { uint32_t j, l; @@ -325,6 +273,11 @@ void list_chisels(vector* chlist) { chisel_desc* cd = &(chlist->at(j)); + if(cd->m_viewinfo.m_valid) + { + continue; + } + string category = cd->m_category; if(category != last_category) @@ -342,11 +295,15 @@ void list_chisels(vector* chlist) } printf("%s", cd->m_name.c_str()); - uint32_t namelen = cd->m_name.size(); + uint32_t namelen = (uint32_t)cd->m_name.size(); - ASSERT(namelen < (DESCRIPTION_TEXT_START - 2)); + if(namelen >= DESCRIPTION_TEXT_START) + { + printf("\n"); + namelen = 0; + } - for(l = 0; l < (DESCRIPTION_TEXT_START - namelen - 2); l++) + for(l = 0; l < (DESCRIPTION_TEXT_START - namelen); l++) { printf(" "); } @@ -367,5 +324,9 @@ void list_chisels(vector* chlist) printf("\n"); } - printf("\nUse the -i flag to get detailed information about a specific chisel\n"); + if(verbose) + { + printf("\nUse the -i flag to get detailed information about a specific chisel\n"); + } } +#endif // HAS_CHISELS diff --git a/userspace/sysdig/man/CMakeLists.txt b/userspace/sysdig/man/CMakeLists.txt new file mode 100644 index 0000000000..fc59c6521d --- /dev/null +++ b/userspace/sysdig/man/CMakeLists.txt @@ -0,0 +1,40 @@ +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +find_program(PANDOC pandoc) + +if (PANDOC) + add_custom_target(man_sysdig ALL + COMMAND ${PANDOC} -s -f markdown_github -t man sysdig.md -o ${CMAKE_CURRENT_BINARY_DIR}/sysdig.8 + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + VERBATIM) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/sysdig.8 + DESTINATION share/man/man8) + + add_custom_target(man_csysdig ALL + COMMAND ${PANDOC} -s -f markdown_github -t man csysdig.md -o ${CMAKE_CURRENT_BINARY_DIR}/csysdig.8 + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + VERBATIM) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/csysdig.8 + DESTINATION share/man/man8) +else() + install(FILES sysdig.8 + DESTINATION share/man/man8) + + install(FILES csysdig.8 + DESTINATION share/man/man8) +endif() diff --git a/userspace/sysdig/man/build.sh b/userspace/sysdig/man/build.sh old mode 100644 new mode 100755 index bd3ba766ab..735d244746 --- a/userspace/sysdig/man/build.sh +++ b/userspace/sysdig/man/build.sh @@ -1 +1,19 @@ -pandoc -s -f markdown_github -t man sysdig.md -o sysdig.8 \ No newline at end of file +# +# Copyright (C) 2013-2018 Draios Inc dba Sysdig. +# +# This file is part of sysdig . +# +# 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. +# +pandoc -s -f markdown_github -t man sysdig.md -o sysdig.8 +pandoc -s -f markdown_github -t man csysdig.md -o csysdig.8 diff --git a/userspace/sysdig/man/csysdig.8 b/userspace/sysdig/man/csysdig.8 new file mode 100644 index 0000000000..a3b4c35abb --- /dev/null +++ b/userspace/sysdig/man/csysdig.8 @@ -0,0 +1,501 @@ +.\" Automatically generated by Pandoc 1.19.2.1 +.\" +.TH "" "" "" "" "" +.hy +.SS NAME +.PP +csysdig \- the ncurses user interface for sysdig +.SS SYNOPSIS +.PP +\f[B]csysdig\f[] [\f[I]option\f[]]... +[\f[I]filter\f[]] +.SS DESCRIPTION +.PP +csysdig exports sysdig\[aq]s functionality through an intuitive and +powerful ncurses\-based user interface. +.PP +csysdig has been designed to mimic tools like \f[B]top\f[] and +\f[B]htop\f[], but it offers richer functionality, including: +.IP \[bu] 2 +Support for both live analysis and sysdig trace files. +Trace files can come from the same machine or from another machine. +.IP \[bu] 2 +Visibility into a broad range of metrics, including CPU, memory, disk +I/O, network I/O. +.IP \[bu] 2 +Ability to observe input/output activity for processes, files, network +connections and more. +.IP \[bu] 2 +Ability to drill down into processes, files, network connections and +more to further explore their behavior. +.IP \[bu] 2 +Full customization support. +.IP \[bu] 2 +Support for sysdig\[aq]s filtering language. +.IP \[bu] 2 +Container support by design. +.PP +csysdig works on any terminal, and has support for colors and mouse +input. +.PP +\f[B]Views\f[] +.PP +csysdig is based on the concept of \[aq]views\[aq], little Lua scripts +that determine how metrics are collected, processed and represented on +screen. +Including a new visualization to csysdig doesn\[aq]t require to update +the program, and is simply a matter of adding a new view. +Views rely on the sysdig processing engine, and this means that they can +include any sysdig filter field. +Views are located in the sysdig chisel directory path, usually +\f[I]/usr/share/sysdig/chisels\f[] and \f[I]~/.chisels\f[]. +.SS BASIC USAGE +.PP +Here are some basic tips to get you started with sysdig: +.IP "1." 3 +If you run csysdig without arguments, it will display live system data, +updating every 2 seconds. +To analyze a trace file, use the \-r command line flag. +.IP "2." 3 +You can switch to a different view by using the \f[I]F2\f[] key. +.IP "3." 3 +You can drill down into a selection by clicking \f[I]enter\f[]. +You can navigate back by typing \f[I]backspace\f[]. +.IP "4." 3 +You can observe input/output for the currently selected entity by typing +\f[I]F5\f[] +.IP "5." 3 +You can see sysdig events for the currently selected entity by typing +\f[I]F6\f[] +.SS DRILLING DOWN +.PP +You drill down by selecting an element in a view and then clicking +\f[I]enter\f[]. +Once inside a selection, you can switch to a different view, and the new +view will be applied in the context of the selection. +For example, if you drill down into a process called foo and then switch +to the \f[I]Connections\f[] view, the output will include only the +connections made or received by \f[I]foo\f[]. +.PP +To drill down multiple times, keep clicking \f[I]enter\f[]. +For example, you can click on a container in the \f[I]Containers\f[] +view to get the processes running inside it, and then click on one of +the processes to see its threads. +.SS ACTIONS AND HOTKEYS +.PP +Each view has a list of command lines that can be executed in the +context of the current selection by pressing \[aq]hotkeys\[aq]. +For example, pressing \[aq]k\[aq] in the Processes view kills the +selected process, pressing \[aq]b\[aq] in the Containers view opens a +bash shell in the selected container. +.PP +Each view supports different actions. +You can see which actions a view supports by pressing F8. +You can customize the view\[aq]s actions by editing the view\[aq]s Lua +file. +.SS CONTAINERS SUPPORT +.PP +Starting csysdig with the \-pc command line switch will cause many of +the views to include additional container information. +For example, the \f[I]Processes\f[] will include a column showing the +container the process belongs to. +Similarly, the \f[I]Connections\f[] view will show which container each +connection belongs to. +.SS INTERACTIVE COMMANDS +.SS Views Window +.PP +\f[B]Arrows, PgUP, PgDn, Home, End\f[] +.PD 0 +.P +.PD +Change the selection and scroll view content, both vertically and +horizontally. +.PP +\f[B]Enter\f[] +.PD 0 +.P +.PD +Drill down into the currently highlighted entry. +.PP +\f[B]Backspace\f[] +.PD 0 +.P +.PD +Navigate back to the previous view. +.PP +\f[B]F2\f[] +.PD 0 +.P +.PD +Show the view picker. +This will let you switch to another view. +.PP +\f[B]CTRL+F /\f[] +.PD 0 +.P +.PD +Incremental search in the list of view entries. +.PP +\f[B]F4\f[] +.PD 0 +.P +.PD +Incremental filtering of the view entries. +.PP +\f[B]F5, e\f[] +.PD 0 +.P +.PD +\[aq]echo FDs\[aq] for the selection, i.e. +view FD input/output for the currently highlighted entry. +.PP +\f[B]F6, d\f[] +.PD 0 +.P +.PD +\[aq]dig\[aq] into the selection, i.e. +view sysdig events for the currently highlighted entry. +Refer to the sysdig man page to learn about interpreting the content of +this window. +.PP +\f[B]F7\f[] +.PD 0 +.P +.PD +Show the help page for the currently displayed view. +.PP +\f[B]F8\f[] +.PD 0 +.P +.PD +Open the view\[aq]s actions panel. +.PP +\f[B]F9, >\f[] +.PD 0 +.P +.PD +Open the column sort panel. +.PP +\f[B]F10, q\f[] +.PD 0 +.P +.PD +Quit. +.PP +\f[B]DEL, c\f[] +.PD 0 +.P +.PD +For views that are listing elements without aggregating them by key +(identifiable by yellow column headers), this command clears the view +content. +.PP +\f[B]p\f[] +.PD 0 +.P +.PD +Pause screen updates. +.PP +\f[B]\f[C]\ <1\-9>\f[]\f[] +.PD 0 +.P +.PD +sort column \f[C]\f[] +.PP +\f[B]F1, h, ?\f[] +.PD 0 +.P +.PD +Show the help screen. +.SS Echo and sysdig Windows +.PP +\f[B]Arrows, PgUP, PgDn, Home, End\f[] +.PD 0 +.P +.PD +Scroll the page content. +.PP +\f[B]Backspace\f[] +.PD 0 +.P +.PD +Navigate back to the previous view. +.PP +\f[B]CTRL+F /\f[] +.PD 0 +.P +.PD +Search inside the window content. +.PP +\f[B]F3\f[] +.PD 0 +.P +.PD +Find Next. +.PP +\f[B]F2\f[] +.PD 0 +.P +.PD +Chose the output rendering format. +Options are \[aq]Dotted ASCII\[aq] (non\-printable binary bytes are +rendered as dots), \[aq]Printable ASCII\[aq] (non\-printable binary +bytes are not included and line endings are rendered accurately) and +\[aq]Hex\[aq] (dotted ASCII representation is included together with the +Hexadecimal rendering of the buffers). +.PP +\f[B]DEL, c\f[] +.PD 0 +.P +.PD +Clear the screen content. +.PP +\f[B]p\f[] +.PD 0 +.P +.PD +Pause screen updates. +.PP +\f[B]CTRL+G\f[] +.PD 0 +.P +.PD +Go to line. +.SS Spectrogram Window +.PP +\f[B]F2\f[] +.PD 0 +.P +.PD +Show the view picker. +This will let you switch to another view. +.PP +\f[B]p\f[] +.PD 0 +.P +.PD +Pause/Resume the visualization. +.PP +\f[B]Backspace\f[] +.PD 0 +.P +.PD +Navigate back to the previous view. +.SS MOUSE USAGE +.IP \[bu] 2 +Clicking on column headers lets you sort the table. +.IP \[bu] 2 +Double clicking on row entries performs a drill down. +.IP \[bu] 2 +Clicking on the filter string at the top of the screen (the text after +\[aq]Filter:\[aq]) lets you change the sysdig filter and customize the +view content. +.IP \[bu] 2 +You can use the mouse on the entries in the menu at the bottom of the +screen to perform their respective actions. +.SS COMMAND LINE OPTIONS +.PP +\f[B]\-d\f[] \f[I]period\f[], \f[B]\-\-delay\f[]=\f[I]period\f[] +.PD 0 +.P +.PD +Set the delay between updates, in milliseconds (by default = 2000). +This works similarly to the \-d option in top. +.PP +\f[B]\-E\f[], \f[B]\-\-exclude\-users\f[] +.PD 0 +.P +.PD +Don\[aq]t create the user/group tables by querying the OS when sysdig +starts. +This also means that no user or group info will be written to the +tracefile by the \-w flag. +The user/group tables are necessary to use filter fields like user.name +or group.name. +However, creating them can increase sysdig\[aq]s startup time. +.PP +\f[B]\-\-force\-term\-compat\f[] +.PD 0 +.P +.PD +Try to configure simple terminal settings (xterm\-1002) that work better +with terminals like putty. +Try to use this flag if you experience terminal issues like the mouse +not working. +.PP +\f[B]\-h\f[], \f[B]\-\-help\f[] +.PD 0 +.P +.PD +Print this page +.PP +\f[B]\-k\f[], \f[B]\-\-k8s\-api\f[] +.PD 0 +.P +.PD +Enable Kubernetes support by connecting to the API server specified as +argument. +E.g. +"". +The API server can also be specified via the environment variable +SYSDIG_K8S_API. +.PP +\f[B]\-K\f[] \f[I]btfile | certfile:keyfile[#password][:cacertfile]\f[], +\f[B]\-\-k8s\-api\-cert=\f[]\f[I]btfile | +certfile:keyfile[#password][:cacertfile]\f[] +.PD 0 +.P +.PD +Use the provided files names to authenticate user and (optionally) +verify the K8S API server identity. +Each entry must specify full (absolute, or relative to the current +directory) path to the respective file. +Private key password is optional (needed only if key is password +protected). +CA certificate is optional. +For all files, only PEM file format is supported. +Specifying CA certificate only is obsoleted \- when single entry is +provided for this option, it will be interpreted as the name of a file +containing bearer token. +Note that the format of this command\-line option prohibits use of files +whose names contain \[aq]:\[aq] or \[aq]#\[aq] characters in the file +name. +Option can also be provided via the environment variable +SYSDIG_K8S_API_CERT. +.PP +\f[B]\-l\f[], \f[B]\-\-list\f[] +.PD 0 +.P +.PD +List all the fields that can be used in views. +.PP +\f[B]\-\-logfile\f[] \f[I]file\f[] +.PD 0 +.P +.PD +Print program logs into the given file. +.PP +\f[B]\-m\f[] \f[I]url[,marathon\-url]\f[], +\f[B]\-\-mesos\-api=\f[]\f[I]url[,marathon\-url]\f[] +.PD 0 +.P +.PD +Enable Mesos support by connecting to the API server specified as +argument (e.g. +). +Mesos url is required. +Marathon url is optional, defaulting to auto\-follow \- if Marathon API +server is not provided, csysdig will attempt to retrieve (and +subsequently follow, if it migrates) the location of Marathon API server +from the Mesos master. +Note that, with auto\-follow, csysdig will likely receive a cluster +internal IP address for Marathon API server, so running csysdig with +Marathon auto\-follow from a node that is not part of Mesos cluster may +not work. +Additionally, running csysdig with Mesos support on a node that has no +containers managed by Mesos is of limited use because, although cluster +metadata will be collected, there will be no Mesos/Marathon filtering +capability. +The API servers can also be specified via the environment variable +SYSDIG_MESOS_API. +.PP +\f[B]\-n\f[] \f[I]num\f[], \f[B]\-\-numevents\f[]=\f[I]num\f[] +.PD 0 +.P +.PD +Stop capturing after \f[I]num\f[] events +.PP +\f[B]\-\-page\-faults\f[] +.PD 0 +.P +.PD +Capture user/kernel major/minor page faults +.PP +\f[B]\-pc\f[], \f[B]\-pcontainers\f[]_ +.PD 0 +.P +.PD +Instruct csysdig to use a container\-friendly format in its views. +This will cause several of the views to contain additional +container\-related columns. +.PP +\f[B]\-R\f[], \f[B]\-\-resolve\-ports\f[] +.PD 0 +.P +.PD +Resolve port numbers to names. +.PP +\f[B]\-r\f[] \f[I]readfile\f[], \f[B]\-\-read\f[]=\f[I]readfile\f[] +.PD 0 +.P +.PD +Read the events from \f[I]readfile\f[]. +.PP +\f[B]\-s\f[] \f[I]len\f[], \f[B]\-\-snaplen\f[]=\f[I]len\f[] +.PD 0 +.P +.PD +Capture the first \f[I]len\f[] bytes of each I/O buffer. +By default, the first 80 bytes are captured. +Use this option with caution, it can generate huge trace files. +.PP +\f[B]\-T\f[], \f[B]\-\-force\-tracers\-capture\f[] +.PD 0 +.P +.PD +Tell the driver to make sure full buffers are captured from /dev/null, +to make sure that tracers are completely captured. +Note that sysdig will enable extended /dev/null capture by itself after +detecting that tracers are written there, but that could result in the +truncation of some tracers at the beginning of the capture. +This option allows preventing that. +.PP +\f[B]\-v\f[] \f[I]view_id\f[], \f[B]\-\-views\f[]=\f[I]view_id\f[] +.PD 0 +.P +.PD +Run the view with the given ID when csysdig starts. +View IDs can be found in the view documentation pages in csysdig. +Combine this option with a command line filter for complete output +customization. +.PP +\f[B]\-\-version\f[] +.PD 0 +.P +.PD +Print version number. +.SS FILTERING +.PP +Similarly to what you do with sysdig, you can specify a filter on the +command line to restrict the events that csysdig processes. +To modify the filter while the program is running, or to add a filter at +runtime, click on the filter text in the UI with the mouse. +.SS CUSTOMIZING CSYSDIG +.PP +csysdig is completely customizable. +This means that you can modify any of the csysdig views, and even create +your own views. +Like sysdig chisels, csysdig views are Lua scripts. +Full information can be found at the following github wiki page: +. +.SS FILES +.PP +\f[I]/usr/share/sysdig/chisels\f[] +.PD 0 +.P +.PD +The global views directory. +.PP +\f[I]~/.chisels\f[] +.PD 0 +.P +.PD +The personal views directory. +.SS AUTHOR +.PP +Draios Inc. +(dba Sysdig) +.SS SEE ALSO +.PP +\f[B]sysdig\f[](8), \f[B]strace\f[](8), \f[B]tcpdump\f[](8), +\f[B]lsof\f[](8) diff --git a/userspace/sysdig/man/csysdig.md b/userspace/sysdig/man/csysdig.md new file mode 100644 index 0000000000..987b6ed1b8 --- /dev/null +++ b/userspace/sysdig/man/csysdig.md @@ -0,0 +1,239 @@ +NAME +---- + +csysdig - the ncurses user interface for sysdig + +SYNOPSIS +-------- + +**csysdig** [*option*]... [*filter*] + +DESCRIPTION +----------- + +csysdig exports sysdig's functionality through an intuitive and powerful ncurses-based user interface. + +csysdig has been designed to mimic tools like **top** and **htop**, but it offers richer functionality, including: + +- Support for both live analysis and sysdig trace files. Trace files can come from the same machine or from another machine. +- Visibility into a broad range of metrics, including CPU, memory, disk I/O, network I/O. +- Ability to observe input/output activity for processes, files, network connections and more. +- Ability to drill down into processes, files, network connections and more to further explore their behavior. +- Full customization support. +- Support for sysdig's filtering language. +- Container support by design. + +csysdig works on any terminal, and has support for colors and mouse input. + +**Views** + +csysdig is based on the concept of 'views', little Lua scripts that determine how metrics are collected, processed and represented on screen. Including a new visualization to csysdig doesn't require to update the program, and is simply a matter of adding a new view. Views rely on the sysdig processing engine, and this means that they can include any sysdig filter field. Views are located in the sysdig chisel directory path, usually */usr/share/sysdig/chisels* and *~/.chisels*. + + +BASIC USAGE +----------- + +Here are some basic tips to get you started with sysdig: + +1. If you run csysdig without arguments, it will display live system data, updating every 2 seconds. To analyze a trace file, use the -r command line flag. +2. You can switch to a different view by using the _F2_ key. +3. You can drill down into a selection by clicking _enter_. You can navigate back by typing _backspace_. +4. You can observe input/output for the currently selected entity by typing _F5_ +5. You can see sysdig events for the currently selected entity by typing _F6_ + +DRILLING DOWN +------------- +You drill down by selecting an element in a view and then clicking _enter_. Once inside a selection, you can switch to a different view, and the new view will be applied in the context of the selection. For example, if you drill down into a process called foo and then switch to the _Connections_ view, the output will include only the connections made or received by _foo_. + +To drill down multiple times, keep clicking _enter_. For example, you can click on a container in the _Containers_ view to get the processes running inside it, and then click on one of the processes to see its threads. + +ACTIONS AND HOTKEYS +------------------- +Each view has a list of command lines that can be executed in the context of the current selection by pressing 'hotkeys'. For example, pressing 'k' in the Processes view kills the selected process, pressing 'b' in the Containers view opens a bash shell in the selected container. + +Each view supports different actions. You can see which actions a view supports by pressing F8. You can customize the view's actions by editing the view's Lua file. + +CONTAINERS SUPPORT +------------------ +Starting csysdig with the -pc command line switch will cause many of the views to include additional container information. For example, the _Processes_ will include a column showing the container the process belongs to. Similarly, the _Connections_ view will show which container each connection belongs to. + +INTERACTIVE COMMANDS +-------------------- + +## Views Window ## + +**Arrows, PgUP, PgDn, Home, End** + Change the selection and scroll view content, both vertically and horizontally. + +**Enter** + Drill down into the currently highlighted entry. + +**Backspace** + Navigate back to the previous view. + +**F2** + Show the view picker. This will let you switch to another view. + +**CTRL+F /** + Incremental search in the list of view entries. + +**F4** + Incremental filtering of the view entries. + +**F5, e** + 'echo FDs' for the selection, i.e. view FD input/output for the currently highlighted entry. + +**F6, d** + 'dig' into the selection, i.e. view sysdig events for the currently highlighted entry. Refer to the sysdig man page to learn about interpreting the content of this window. + +**F7** + Show the help page for the currently displayed view. + +**F8** + Open the view's actions panel. + +**F9, >** + Open the column sort panel. + +**F10, q** + Quit. + +**DEL, c** + For views that are listing elements without aggregating them by key (identifiable by yellow column headers), this command clears the view content. + +**p** + Pause screen updates. + +**` <1-9>`** + sort column `` + +**F1, h, ?** + Show the help screen. + +## Echo and sysdig Windows ## + +**Arrows, PgUP, PgDn, Home, End** + Scroll the page content. + +**Backspace** + Navigate back to the previous view. + +**CTRL+F /** + Search inside the window content. + +**F3** + Find Next. + +**F2** + Chose the output rendering format. Options are 'Dotted ASCII' (non-printable binary bytes are rendered as dots), 'Printable ASCII' (non-printable binary bytes are not included and line endings are rendered accurately) and 'Hex' (dotted ASCII representation is included together with the Hexadecimal rendering of the buffers). + +**DEL, c** + Clear the screen content. + +**p** + Pause screen updates. + +**CTRL+G** + Go to line. + +## Spectrogram Window ## + +**F2** + Show the view picker. This will let you switch to another view. + +**p** + Pause/Resume the visualization. + +**Backspace** + Navigate back to the previous view. + +MOUSE USAGE +----------- +- Clicking on column headers lets you sort the table. +- Double clicking on row entries performs a drill down. +- Clicking on the filter string at the top of the screen (the text after 'Filter:') lets you change the sysdig filter and customize the view content. +- You can use the mouse on the entries in the menu at the bottom of the screen to perform their respective actions. + +COMMAND LINE OPTIONS +-------------------- + +**-d** _period_, **--delay**=_period_ + Set the delay between updates, in milliseconds (by default = 2000). This works similarly to the -d option in top. + +**-E**, **--exclude-users** + Don't create the user/group tables by querying the OS when sysdig starts. This also means that no user or group info will be written to the tracefile by the -w flag. The user/group tables are necessary to use filter fields like user.name or group.name. However, creating them can increase sysdig's startup time. + +**--force-term-compat** + Try to configure simple terminal settings (xterm-1002) that work better with terminals like putty. Try to use this flag if you experience terminal issues like the mouse not working. + +**-h**, **--help** + Print this page + +**-k**, **--k8s-api** + Enable Kubernetes support by connecting to the API server specified as argument. E.g. "http://admin:password@127.0.0.1:8080". The API server can also be specified via the environment variable SYSDIG_K8S_API. + +**-K** _btfile | certfile:keyfile[#password][:cacertfile]_, **--k8s-api-cert=**_btfile | certfile:keyfile[#password][:cacertfile]_ + Use the provided files names to authenticate user and (optionally) verify the K8S API server identity. Each entry must specify full (absolute, or relative to the current directory) path to the respective file. Private key password is optional (needed only if key is password protected). CA certificate is optional. For all files, only PEM file format is supported. Specifying CA certificate only is obsoleted - when single entry is provided for this option, it will be interpreted as the name of a file containing bearer token. Note that the format of this command-line option prohibits use of files whose names contain ':' or '#' characters in the file name. Option can also be provided via the environment variable SYSDIG_K8S_API_CERT. + +**-l**, **--list** + List all the fields that can be used in views. + +**--logfile** _file_ + Print program logs into the given file. + +**-m** _url[,marathon-url]_, **--mesos-api=**_url[,marathon-url]_ + Enable Mesos support by connecting to the API server specified as argument (e.g. http://admin:password@127.0.0.1:5050). Mesos url is required. Marathon url is optional, defaulting to auto-follow - if Marathon API server is not provided, csysdig will attempt to retrieve (and subsequently follow, if it migrates) the location of Marathon API server from the Mesos master. Note that, with auto-follow, csysdig will likely receive a cluster internal IP address for Marathon API server, so running csysdig with Marathon auto-follow from a node that is not part of Mesos cluster may not work. Additionally, running csysdig with Mesos support on a node that has no containers managed by Mesos is of limited use because, although cluster metadata will be collected, there will be no Mesos/Marathon filtering capability. The API servers can also be specified via the environment variable SYSDIG_MESOS_API. + +**-n** _num_, **--numevents**=_num_ + Stop capturing after _num_ events + +**--page-faults** + Capture user/kernel major/minor page faults + +**-pc**, **-pcontainers**_ + Instruct csysdig to use a container-friendly format in its views. This will cause several of the views to contain additional container-related columns. + +**-R**, **--resolve-ports** + Resolve port numbers to names. + +**-r** _readfile_, **--read**=_readfile_ + Read the events from _readfile_. + +**-s** _len_, **--snaplen**=_len_ + Capture the first _len_ bytes of each I/O buffer. By default, the first 80 bytes are captured. Use this option with caution, it can generate huge trace files. + +**-T**, **--force-tracers-capture** + Tell the driver to make sure full buffers are captured from /dev/null, to make sure that tracers are completely captured. Note that sysdig will enable extended /dev/null capture by itself after detecting that tracers are written there, but that could result in the truncation of some tracers at the beginning of the capture. This option allows preventing that. + +**-v** _view_id_, **--views**=_view_id_ + Run the view with the given ID when csysdig starts. View IDs can be found in the view documentation pages in csysdig. Combine this option with a command line filter for complete output customization. + +**--version** + Print version number. + +FILTERING +--------- +Similarly to what you do with sysdig, you can specify a filter on the command line to restrict the events that csysdig processes. To modify the filter while the program is running, or to add a filter at runtime, click on the filter text in the UI with the mouse. + +CUSTOMIZING CSYSDIG +------------------- +csysdig is completely customizable. This means that you can modify any of the csysdig views, and even create your own views. Like sysdig chisels, csysdig views are Lua scripts. Full information can be found at the following github wiki page: https://github.com/draios/sysdig/wiki/csysdig-View-Format-Reference. + +FILES +----- + +*/usr/share/sysdig/chisels* + The global views directory. + +*~/.chisels* + The personal views directory. + +AUTHOR +------ + +Draios Inc. (dba Sysdig) + +SEE ALSO +-------- + +**sysdig**(8), **strace**(8), **tcpdump**(8), **lsof**(8) diff --git a/userspace/sysdig/man/sysdig.8 b/userspace/sysdig/man/sysdig.8 index d968a053b3..0b7bf70a65 100644 --- a/userspace/sysdig/man/sysdig.8 +++ b/userspace/sysdig/man/sysdig.8 @@ -1,4 +1,7 @@ -.TH SYSDIG 8 +.\" Automatically generated by Pandoc 1.19.2.1 +.\" +.TH "" "" "" "" "" +.hy .SS NAME .PP sysdig \- the definitive system and process troubleshooting tool @@ -8,6 +11,9 @@ sysdig \- the definitive system and process troubleshooting tool [\f[I]filter\f[]] .SS DESCRIPTION .PP +\f[B]Note: if you are interested in an easier to use interface for the +sysdig functionality, use the csysdig command line utility.\f[] +.PP sysdig is a tool for system troubleshooting, analysis and exploration. It can be used to capture, filter and decode system calls and other OS events. @@ -17,7 +23,7 @@ events. sysdig can be both used to inspect live systems, or to generate trace files that can be analyzed at a later stage. .PP -Sysdig includes a powerul filtering language, has customizable output, +sysdig includes a powerful filtering language, has customizable output, and can be extended through Lua scripts, called chisels. .PP \f[B]Output format\f[] @@ -25,52 +31,36 @@ and can be extended through Lua scripts, called chisels. By default, sysdig prints the information for each captured event on a single line, with the following format: .PP -\f[C]\ \ \ \ \ \ \ \f[] +\f[C]*%evt.num\ %evt.time\ %evt.cpu\ %proc.name\ (%thread.tid)\ %evt.dir\ %evt.type\ %evt.info\f[] .PP where: .IP \[bu] 2 evt.num is the incremental event number -.PD 0 -.P -.PD .IP \[bu] 2 evt.time is the event timestamp -.PD 0 -.P -.PD .IP \[bu] 2 evt.cpu is the CPU number where the event was captured -.PD 0 -.P -.PD .IP \[bu] 2 proc.name is the name of the process that generated the event -.PD 0 -.P -.PD .IP \[bu] 2 thread.tid id the TID that generated the event, which corresponds to the PID for single thread processes -.PD 0 -.P -.PD .IP \[bu] 2 evt.dir is the event direction, > for enter events and < for exit events -.PD 0 -.P -.PD .IP \[bu] 2 evt.type is the name of the event, e.g. \[aq]open\[aq] or \[aq]read\[aq] -.PD 0 -.P -.PD .IP \[bu] 2 evt.args is the list of event arguments. .PP The output format can be customized with the \-p switch, using any of the fields listed by \[aq]sysdig \-l\[aq]. .PP +Using \-pc or \-pcontainer, the default format will be changed to a +container\-friendly one: +.PP +\f[C]*%evt.num\ %evt.time\ %evt.cpu\ %container.name\ (%container.id)\ %proc.name\ (%thread.tid:%thread.vtid)\ %evt.dir\ %evt.type\ %evt.info\f[] +.PP \f[B]Trace Files\f[] .PP A trace file can be created using the \-w switch: @@ -112,14 +102,23 @@ The list of available fields can be obtained with \[aq]sysdig \-l\[aq]. .PD Filter expressions can use one of these comparison operators: \f[I]=\f[], \f[I]!=\f[], \f[I]<\f[], \f[I]<=\f[], \f[I]>\f[], -\f[I]>=\f[] and \f[I]contains\f[]. +\f[I]>=\f[], \f[I]contains\f[], \f[I]icontains\f[], \f[I]in\f[] and +\f[I]exists\f[]. e.g. .RS .PP $ sysdig fd.name contains /etc +.PD 0 +.P +.PD +$ sysdig "evt.type in ( \[aq]select\[aq], \[aq]poll\[aq] )" +.PD 0 +.P +.PD +$ sysdig proc.name exists .RE .PP -Multiple checks can be combined through brakets and the following +Multiple checks can be combined through brackets and the following boolean operators: \f[I]and\f[], \f[I]or\f[], \f[I]not\f[]. e.g. .RS @@ -128,10 +127,8 @@ $ sysdig "not (fd.name contains /proc or fd.name contains /dev)" .RE .PP \f[B]Chisels\f[] -.PD 0 -.P -.PD -Sysdig\[aq]s chisels are little scripts that analyze the sysdig event +.PP +sysdig\[aq]s chisels are little scripts that analyze the sysdig event stream to perform useful actions. .PD 0 .P @@ -145,7 +142,7 @@ $ sysdig \-cl To get details about a specific chisel, type .RS .PP -$ sysdig \-ispy_ip +$ sysdig \-i spy_ip .RE .PP To run one of the chisels, you use the \-c flag, e.g. @@ -160,7 +157,14 @@ If a chisel needs arguments, you specify them after the chisel name: $ sysdig \-c spy_ip 192.168.1.157 .RE .PP -Chiesls can be combined with filters: +If a chisel has more than one argument, specify them after the chisel +name, enclosed in quotes: +.RS +.PP +$ sysdig \-c chisel_name "arg1 arg2 arg3" +.RE +.PP +Chisels can be combined with filters: .RS .PP $ sysdig \-c topfiles_bytes "not fd.name contains /dev" @@ -174,11 +178,13 @@ $ sysdig \-c topfiles_bytes "not fd.name contains /dev" Only print the text portion of data buffers, and echo end\-of\-lines. This is useful to only display human\-readable data. .PP -\f[B]\-a\f[], \f[B]\-\-abstime\f[] +\f[B]\-b\f[], \f[B]\-\-print\-base64\f[] .PD 0 .P .PD -Show absolute event timestamps +Print data buffers in base64. +This is useful for encoding binary data that needs to be used over media +designed to handle textual data (i.e., terminal or json). .PP \f[B]\-c\f[] \f[I]chiselname\f[] \f[I]chiselargs\f[], \f[B]\-\-chisel\f[]=\f[I]chiselname\f[] \f[I]chiselargs\f[] @@ -189,23 +195,113 @@ run the specified chisel. If the chisel require arguments, they must be specified in the command line after the name. .PP +\f[B]\-C\f[] \f[I]filesize\f[] +.PD 0 +.P +.PD +Break a capture into separate files, and limit the size of each file +based on the specified number of megabytes. +The units of \f[I]filesize\f[] are millions of bytes (10^6, not 2^20). +Use in conjunction with \f[B]\-W\f[] to enable automatic file rotation. +Otherwise, new files will continue to be created until the capture is +manually stopped. +.PP +Files will have the name specified by \f[B]\-w\f[] with a counter added +starting at 0. +.PP \f[B]\-cl\f[], \f[B]\-\-list\-chisels\f[] .PD 0 .P .PD lists the available chisels. -Looks for chisels in ., ./chisels, ~/chisels and +Looks for chisels in ./chisels, ~/.chisels and /usr/share/sysdig/chisels. .PP \f[B]\-d\f[], \f[B]\-\-displayflt\f[] .PD 0 .P .PD -Make the given filter a display one Setting this option causes the -events to be filtered after being parsed by the state system. +Make the given filter a display one. +Setting this option causes the events to be filtered after being parsed +by the state system. Events are normally filtered before being analyzed, which is more efficient, but can cause state (e.g. -FD names) to be lost +FD names) to be lost. +.PP +\f[B]\-D\f[], \f[B]\-\-debug\f[] +.PD 0 +.P +.PD +Capture events about sysdig itself and print additional logging on +standard error. +.PP +\f[B]\-E\f[], \f[B]\-\-exclude\-users\f[] +.PD 0 +.P +.PD +Don\[aq]t create the user/group tables by querying the OS when sysdig +starts. +This also means that no user or group info will be written to the +tracefile by the \f[B]\-w\f[] flag. +The user/group tables are necessary to use filter fields like user.name +or group.name. +However, creating them can increase sysdig\[aq]s startup time. +Moreover, they contain information that could be privacy sensitive. +.PP +\f[B]\-e\f[] \f[I]numevents\f[] +.PD 0 +.P +.PD +Break a capture into separate files, and limit the size of each file +based on the specified number of events. +Use in conjunction with \f[B]\-W\f[] to enable automatic file rotation. +Otherwise, new files will continue to be created until the capture is +manually stopped. +.PP +Files will have the name specified by \f[B]\-w\f[] with a counter added +starting at 0. +.PP +\f[B]\-F\f[], \f[B]\-\-fatfile\f[] +.PD 0 +.P +.PD +Enable fatfile mode. +When writing in fatfile mode, the output file will contain events that +will be invisible when reading the file, but that are necessary to fully +reconstruct the state. +Fatfile mode is useful when saving events to disk with an aggressive +filter. +The filter could drop events that would the state to be updated (e.g. +clone() or open()). +With fatfile mode, those events are still saved to file, but +\[aq]hidden\[aq] so that they won\[aq]t appear when reading the file. +Be aware that using this flag might generate substantially bigger traces +files. +.PP +\f[B]\-\-filter\-proclist\f[] +.PD 0 +.P +.PD +apply the filter to the process table. +A full dump of /proc is typically included in any trace file to make +sure all the state required to decode events is in the file. +This could cause the file to contain unwanted or sensitive information. +Using this flag causes the command line filter to be applied to the +/proc dump as well. +.PP +\f[B]\-G\f[] \f[I]numseconds\f[] +.PD 0 +.P +.PD +Break a capture into separate files, and limit the size of each file +based on the specified number of seconds. +Use in conjunction with \f[B]\-W\f[] to enable automatic file rotation. +Otherwise, new files will continue to be created until the capture is +manually stopped. +.PP +Files will have the name specified by \f[B]\-w\f[] which should include +a time format as defined by strftime(3). +If no time format is specified, a counter will be used. .PP \f[B]\-h\f[], \f[B]\-\-help\f[] .PD 0 @@ -213,19 +309,60 @@ FD names) to be lost .PD Print this page .PP +\f[B]\-i \f[I]chiselname\f[]\f[], +\f[B]\-\-chisel\-info=\f[]\f[I]chiselname\f[] +.PD 0 +.P +.PD +Get a longer description and the arguments associated with a chisel +found in the \-cl option list. +.PP \f[B]\-j\f[], \f[B]\-\-json\f[] .PD 0 .P .PD -Emit output as json +Emit output as json, data buffer encoding will depend from the print +format selected. .PP -\f[B]\-i \f[I]chiselname\f[]\f[], \f[B]\-\-chisel\-info -\f[I]chiselname\f[]\f[] +\f[B]\-k\f[], \f[B]\-\-k8s\-api\f[] .PD 0 .P .PD -Get a longer description and the arguments associated with a chisel -found in the \-cl option list. +Enable Kubernetes support by connecting to the API server specified as +argument. +E.g. +"". +The API server can also be specified via the environment variable +SYSDIG_K8S_API. +.PP +\f[B]\-K\f[] \f[I]btfile | certfile:keyfile[#password][:cacertfile]\f[], +\f[B]\-\-k8s\-api\-cert=\f[]\f[I]btfile | +certfile:keyfile[#password][:cacertfile]\f[] +.PD 0 +.P +.PD +Use the provided files names to authenticate user and (optionally) +verify the K8S API server identity. +Each entry must specify full (absolute, or relative to the current +directory) path to the respective file. +Private key password is optional (needed only if key is password +protected). +CA certificate is optional. +For all files, only PEM file format is supported. +Specifying CA certificate only is obsoleted \- when single entry is +provided for this option, it will be interpreted as the name of a file +containing bearer token. +Note that the format of this command\-line option prohibits use of files +whose names contain \[aq]:\[aq] or \[aq]#\[aq] characters in the file +name. +Option can also be provided via the environment variable +SYSDIG_K8S_API_CERT. +.PP +\f[B]\-L\f[], \f[B]\-\-list\-events\f[] +.PD 0 +.P +.PD +List the events that the engine supports .PP \f[B]\-l\f[], \f[B]\-\-list\f[] .PD 0 @@ -234,25 +371,65 @@ found in the \-cl option list. List the fields that can be used for filtering and output formatting. Use \-lv to get additional information for each field. .PP -\f[B]\-L\f[], \f[B]\-\-list\-events\f[] +\f[B]\-m\f[] \f[I]url[,marathon\-url]\f[], +\f[B]\-\-mesos\-api=\f[]\f[I]url[,marathon\-url]\f[] .PD 0 .P .PD -List the events that the engine supports +Enable Mesos support by connecting to the API server specified as +argument (e.g. +). +Mesos url is required. +Marathon url is optional, defaulting to auto\-follow \- if Marathon API +server is not provided, sysdig will attempt to retrieve (and +subsequently follow, if it migrates) the location of Marathon API server +from the Mesos master. +Note that, with auto\-follow, sysdig will likely receive a cluster +internal IP address for Marathon API server, so running sysdig with +Marathon auto\-follow from a node that is not part of Mesos cluster may +not work. +Additionally, running sysdig with Mesos support on a node that has no +containers managed by Mesos is of limited use because, although cluster +metadata will be collected, there will be no Mesos/Marathon filtering +capability. +The API servers can also be specified via the environment variable +SYSDIG_MESOS_API. +.PP +\f[B]\-M\f[] \f[I]num_seconds\f[] +.PD 0 +.P +.PD +Stop collecting after reaching .PP \f[B]\-n\f[] \f[I]num\f[], \f[B]\-\-numevents\f[]=\f[I]num\f[] .PD 0 .P .PD -Stop capturing after events +Stop capturing after \f[I]num\f[] events .PP -\f[B]\-p\f[] \f[I]output\f[]format_, -\f[B]\-\-print\f[]=\f[I]output\f[]format_ +\f[B]\-\-page\-faults\f[] +.PD 0 +.P +.PD +Capture user/kernel major/minor page faults +.PP +\f[B]\-P\f[], \f[B]\-\-progress\f[] +.PD 0 +.P +.PD +Print progress on stderr while processing trace files. +.PP +\f[B]\-p\f[] \f[I]outputformat\f[], +\f[B]\-\-print\f[]=\f[I]outputformat\f[] .PD 0 .P .PD Specify the format to be used when printing the events. -See the examples section below for more info. +With \-pc or \-pcontainer will use a container\-friendly format. +With \-pk or \-pkubernetes will use a kubernetes\-friendly format. +With \-pm or \-pmesos will use a mesos\-friendly format. +Specifying \f[B]\-pp\f[] on the command line will cause sysdig to print +the default command line format and exit. .PP \f[B]\-q\f[], \f[B]\-\-quiet\f[] .PD 0 @@ -265,7 +442,13 @@ Useful when dumping to disk. .PD 0 .P .PD -Read the events from . +Read the events from \f[I]readfile\f[]. +.PP +\f[B]\-R\f[], \f[B]\-\-resolve\-ports\f[] +.PD 0 +.P +.PD +Resolve port numbers to names. .PP \f[B]\-S\f[], \f[B]\-\-summary\f[] .PD 0 @@ -278,7 +461,7 @@ the list of the top events) when the capture ends. .PD 0 .P .PD -Capture the first bytes of each I/O buffer. +Capture the first \f[I]len\f[] bytes of each I/O buffer. By default, the first 80 bytes are captured. Use this option with caution, it can generate huge trace files. .PP @@ -286,17 +469,48 @@ Use this option with caution, it can generate huge trace files. .PD 0 .P .PD -Change the way event time is diplayed. +Change the way event time is displayed. Accepted values are \f[B]h\f[] for human\-readable string, \f[B]a\f[] -for abosulte timestamp from epoch, \f[B]r\f[] for relative time from the -beginning of the capture, and \f[B]d\f[] for delta between event enter -and exit. +for absolute timestamp from epoch, \f[B]r\f[] for relative time from the +first displayed event, \f[B]d\f[] for delta between event enter and +exit, and \f[B]D\f[] for delta from the previous event. +.PP +\f[B]\-T\f[], \f[B]\-\-force\-tracers\-capture\f[] +.PD 0 +.P +.PD +Tell the driver to make sure full buffers are captured from /dev/null, +to make sure that tracers are completely captured. +Note that sysdig will enable extended /dev/null capture by itself after +detecting that tracers are written there, but that could result in the +truncation of some tracers at the beginning of the capture. +This option allows preventing that. +.PP +\f[B]\-\-unbuffered\f[] +.PD 0 +.P +.PD +Turn off output buffering. +This causes every single line emitted by sysdig to be flushed, which +generates higher CPU usage but is useful when piping sysdig\[aq]s output +into another process or into a script. .PP \f[B]\-v\f[], \f[B]\-\-verbose\f[] .PD 0 .P .PD Verbose output. +This flag will cause the full content of text and binary buffers to be +printed on screen, instead of being truncated to 40 characters. +Note that data buffers length is still limited by the snaplen (refer to +the \-s flag documentation) \-v will also make sysdig print some summary +information at the end of the capture. +.PP +\f[B]\-\-version\f[] +.PD 0 +.P +.PD +Print version number. .PP \f[B]\-w\f[] \f[I]writefile\f[], \f[B]\-\-write\f[]=\f[I]writefile\f[] .PD 0 @@ -304,6 +518,17 @@ Verbose output. .PD Write the captured events to \f[I]writefile\f[]. .PP +\f[B]\-W\f[] \f[I]num\f[] +.PD 0 +.P +.PD +Turn on file rotation for continuous capture, and limit the number of +files created to the specified number. +Once the cap is reached, older files will be overwritten (ring buffer). +Use in conjunction with the \f[B]\-C\f[] / \f[B]\-G\f[] / \f[B]\-e\f[] +options to limit the size of each file based on number of megabytes, +seconds, and/or events (respectively). +.PP \f[B]\-x\f[], \f[B]\-\-print\-hex\f[] .PD 0 .P @@ -315,6 +540,12 @@ Print data buffers in hex. .P .PD Print data buffers in hex and ASCII. +.PP +\f[B]\-z\f[], \f[B]\-\-compress\f[] +.PD 0 +.P +.PD +Used with \f[B]\-w\f[], enables compression for tracefiles. .SS EXAMPLES .PP Capture all the events from the live system and print them to screen @@ -329,12 +560,26 @@ Capture all the events from the live system and save them to disk $ sysdig \-w dumpfile.scap .RE .PP +Capture all the events in the latest 24 hours and save them to disk +organized in files containing 1 hour of system activity each +.RS +.PP +$ sysdig \-G 3600 \-W 24 \-w dumpfile.scap +.RE +.PP Read events from a file and print them to screen .RS .PP $ sysdig \-r dumpfile.scap .RE .PP +Prepare a sanitized version of a system capture +.RS +.PP +$ sysdig \-r dumpfile.scap \[aq]not evt.buffer contains foo\[aq] \-w +cleandump.scap +.RE +.PP Print all the open system calls invoked by cat .RS .PP @@ -344,13 +589,13 @@ $ sysdig proc.name=cat and evt.type=open Print the name of the files opened by cat .RS .PP -$ ./sysdig \-p"%evt.arg.name" proc.name=cat and evt.type=open +$ sysdig \-p"%evt.arg.name" proc.name=cat and evt.type=open .RE .PP List the available chisels .RS .PP -$ ./sysdig \-cl +$ sysdig \-cl .RE .PP Use the spy_ip chisel to look at the data exchanged with 192.168.1.157: @@ -372,12 +617,16 @@ The global chisels directory. .PD The personal chisels directory. .SS BUGS -.PP -Bugs? +.IP \[bu] 2 +sysdig and its chisels are designed to be used with LuaJIT in Lua 5.1 +mode. +While it is possible to use sysdig with LuaJIT in Lua 5.2 mode or +regular Lua, some chisels may not work as expected. .SS AUTHOR .PP -Draios inc. - +Draios Inc. +aka sysdig .SS SEE ALSO .PP -\f[B]strace\f[](8), \f[B]tcpdump\f[](8), \f[B]lsof\f[](8) +\f[B]csysdig\f[](8), \f[B]strace\f[](8), \f[B]tcpdump\f[](8), +\f[B]lsof\f[](8) diff --git a/userspace/sysdig/man/sysdig.md b/userspace/sysdig/man/sysdig.md index d0cc7a7de3..c583b72c70 100644 --- a/userspace/sysdig/man/sysdig.md +++ b/userspace/sysdig/man/sysdig.md @@ -11,16 +11,18 @@ SYNOPSIS DESCRIPTION ----------- +**Note: if you are interested in an easier to use interface for the sysdig functionality, use the csysdig command line utility.** + sysdig is a tool for system troubleshooting, analysis and exploration. It can be used to capture, filter and decode system calls and other OS events. sysdig can be both used to inspect live systems, or to generate trace files that can be analyzed at a later stage. -Sysdig includes a powerul filtering language, has customizable output, and can be extended through Lua scripts, called chisels. +sysdig includes a powerful filtering language, has customizable output, and can be extended through Lua scripts, called chisels. **Output format** By default, sysdig prints the information for each captured event on a single line, with the following format: -``` ``` +```*%evt.num %evt.time %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.info``` where: * evt.num is the incremental event number @@ -34,6 +36,10 @@ where: The output format can be customized with the -p switch, using any of the fields listed by 'sysdig -l'. +Using -pc or -pcontainer, the default format will be changed to a container-friendly one: + +```*%evt.num %evt.time %evt.cpu %container.name (%container.id) %proc.name (%thread.tid:%thread.vtid) %evt.dir %evt.type %evt.info``` + **Trace Files** A trace file can be created using the -w switch: @@ -46,26 +52,28 @@ used to save only certain events to disk: Trace files can be read this using the -r switch: > $ sysdig -r trace.scap - **Filtering** sysdig filters are specified at the end of the command line. The simplest filter is a basic field-value check: > $ sysdig proc.name=cat The list of available fields can be obtained with 'sysdig -l'. -Filter expressions can use one of these comparison operators: _=_, _!=_, _<_, _<=_, _>_, _>=_ and _contains_. e.g. +Filter expressions can use one of these comparison operators: _=_, _!=_, _<_, _<=_, _>_, _>=_, _contains_, _icontains_, _in_ and _exists_. e.g. > $ sysdig fd.name contains /etc +> $ sysdig "evt.type in ( 'select', 'poll' )" +> $ sysdig proc.name exists -Multiple checks can be combined through brakets and the following boolean operators: _and_, _or_, _not_. e.g. +Multiple checks can be combined through brackets and the following boolean operators: _and_, _or_, _not_. e.g. > $ sysdig "not (fd.name contains /proc or fd.name contains /dev)" **Chisels** -Sysdig's chisels are little scripts that analyze the sysdig event stream to perform useful actions. + +sysdig's chisels are little scripts that analyze the sysdig event stream to perform useful actions. To get the list of available chisels, type > $ sysdig -cl To get details about a specific chisel, type -> $ sysdig -ispy_ip +> $ sysdig -i spy_ip To run one of the chisels, you use the -c flag, e.g. > $ sysdig -c topfiles_bytes @@ -73,94 +81,170 @@ To run one of the chisels, you use the -c flag, e.g. If a chisel needs arguments, you specify them after the chisel name: > $ sysdig -c spy_ip 192.168.1.157 -Chiesls can be combined with filters: +If a chisel has more than one argument, specify them after the chisel name, enclosed in quotes: +> $ sysdig -c chisel_name "arg1 arg2 arg3" + +Chisels can be combined with filters: > $ sysdig -c topfiles_bytes "not fd.name contains /dev" OPTIONS ------- -**-A**, **--print-ascii** +**-A**, **--print-ascii** Only print the text portion of data buffers, and echo end-of-lines. This is useful to only display human-readable data. - -**-a**, **--abstime** - Show absolute event timestamps - -**-c** _chiselname_ _chiselargs_, **--chisel**=_chiselname_ _chiselargs_ + +**-b**, **--print-base64** + Print data buffers in base64. This is useful for encoding binary data that needs to be used over media designed to handle textual data (i.e., terminal or json). + +**-c** _chiselname_ _chiselargs_, **--chisel**=_chiselname_ _chiselargs_ run the specified chisel. If the chisel require arguments, they must be specified in the command line after the name. + +**-C** _filesize_ + Break a capture into separate files, and limit the size of each file based on the specified number of megabytes. The units of _filesize_ are millions of bytes (10^6, not 2^20). Use in conjunction with **-W** to enable automatic file rotation. Otherwise, new files will continue to be created until the capture is manually stopped. -**-cl**, **--list-chisels** - lists the available chisels. Looks for chisels in ., ./chisels, ~/chisels and /usr/share/sysdig/chisels. + Files will have the name specified by **-w** with a counter added starting at 0. -**-d**, **--displayflt** - Make the given filter a display one Setting this option causes the events to be filtered after being parsed by the state system. Events are normally filtered before being analyzed, which is more efficient, but can cause state (e.g. FD names) to be lost +**-cl**, **--list-chisels** + lists the available chisels. Looks for chisels in ./chisels, ~/.chisels and /usr/share/sysdig/chisels. -**-h**, **--help** - Print this page +**-d**, **--displayflt** + Make the given filter a display one. Setting this option causes the events to be filtered after being parsed by the state system. Events are normally filtered before being analyzed, which is more efficient, but can cause state (e.g. FD names) to be lost. -**-j**, **--json** - Emit output as json +**-D**, **--debug** + Capture events about sysdig itself, display internal events in addition to system events, and print additional logging on standard error. + +**-E**, **--exclude-users** + Don't create the user/group tables by querying the OS when sysdig starts. This also means that no user or group info will be written to the tracefile by the **-w** flag. The user/group tables are necessary to use filter fields like user.name or group.name. However, creating them can increase sysdig's startup time. Moreover, they contain information that could be privacy sensitive. + +**-e** _numevents_ + Break a capture into separate files, and limit the size of each file based on the specified number of events. Use in conjunction with **-W** to enable automatic file rotation. Otherwise, new files will continue to be created until the capture is manually stopped. -**-i _chiselname_**, **--chisel-info _chiselname_** - Get a longer description and the arguments associated with a chisel found in the -cl option list. + Files will have the name specified by **-w** with a counter added starting at 0. -**-l**, **--list** - List the fields that can be used for filtering and output formatting. Use -lv to get additional information for each field. +**-F**, **--fatfile** + Enable fatfile mode. When writing in fatfile mode, the output file will contain events that will be invisible when reading the file, but that are necessary to fully reconstruct the state. Fatfile mode is useful when saving events to disk with an aggressive filter. The filter could drop events that would cause the state to be updated (e.g. clone() or open()). With fatfile mode, those events are still saved to file, but 'hidden' so that they won't appear when reading the file. Be aware that using this flag might generate substantially bigger traces files. + +**--filter-proclist** + apply the filter to the process table. A full dump of /proc is typically included in any trace file to make sure all the state required to decode events is in the file. This could cause the file to contain unwanted or sensitive information. Using this flag causes the command line filter to be applied to the /proc dump as well. + +**-G** _numseconds_ + Break a capture into separate files, and limit the size of each file based on the specified number of seconds. Use in conjunction with **-W** to enable automatic file rotation. Otherwise, new files will continue to be created until the capture is manually stopped. + + Files will have the name specified by **-w** which should include a time format as defined by strftime(3). If no time format is specified, a counter will be used. + +**-h**, **--help** + Print this page + +**-i _chiselname_**, **--chisel-info=**_chiselname_ + Get a longer description and the arguments associated with a chisel found in the -cl option list. -**-L**, **--list-events** +**-j**, **--json** + Emit output as json, data buffer encoding will depend from the print format selected. + +**-k**, **--k8s-api** + Enable Kubernetes support by connecting to the API server specified as argument. E.g. "http://admin:password@127.0.0.1:8080". The API server can also be specified via the environment variable SYSDIG_K8S_API. + +**-K** _btfile | certfile:keyfile[#password][:cacertfile]_, **--k8s-api-cert=**_btfile | certfile:keyfile[#password][:cacertfile]_ + Use the provided files names to authenticate user and (optionally) verify the K8S API server identity. Each entry must specify full (absolute, or relative to the current directory) path to the respective file. Private key password is optional (needed only if key is password protected). CA certificate is optional. For all files, only PEM file format is supported. Specifying CA certificate only is obsoleted - when single entry is provided for this option, it will be interpreted as the name of a file containing bearer token. Note that the format of this command-line option prohibits use of files whose names contain ':' or '#' characters in the file name. Option can also be provided via the environment variable SYSDIG_K8S_API_CERT. + +**-L**, **--list-events** List the events that the engine supports +**-l**, **--list** + List the fields that can be used for filtering and output formatting. Use -lv to get additional information for each field. + +**--list-markdown** + Like -l, but produces markdown output + +**-m** _url[,marathon-url]_, **--mesos-api=**_url[,marathon-url]_ + Enable Mesos support by connecting to the API server specified as argument (e.g. http://admin:password@127.0.0.1:5050). Mesos url is required. Marathon url is optional, defaulting to auto-follow - if Marathon API server is not provided, sysdig will attempt to retrieve (and subsequently follow, if it migrates) the location of Marathon API server from the Mesos master. Note that, with auto-follow, sysdig will likely receive a cluster internal IP address for Marathon API server, so running sysdig with Marathon auto-follow from a node that is not part of Mesos cluster may not work. Additionally, running sysdig with Mesos support on a node that has no containers managed by Mesos is of limited use because, although cluster metadata will be collected, there will be no Mesos/Marathon filtering capability. The API servers can also be specified via the environment variable SYSDIG_MESOS_API. + +**-M** _num_seconds_ + Stop collecting after reaching + **-n** _num_, **--numevents**=_num_ - Stop capturing after events + Stop capturing after _num_ events + +**--page-faults** + Capture user/kernel major/minor page faults + +**-P**, **--progress** + Print progress on stderr while processing trace files. -**-p** _output_format_, **--print**=_output_format_ - Specify the format to be used when printing the events. See the examples section below for more info. +**-p** _outputformat_, **--print**=_outputformat_ + Specify the format to be used when printing the events. With -pc or -pcontainer will use a container-friendly format. With -pk or -pkubernetes will use a kubernetes-friendly format. With -pm or -pmesos will use a mesos-friendly format. Specifying **-pp** on the command line will cause sysdig to print the default command line format and exit. **-q**, **--quiet** Don't print events on the screen. Useful when dumping to disk. **-r** _readfile_, **--read**=_readfile_ - Read the events from . - + Read the events from _readfile_. + +**-R**, **--resolve-ports** + Resolve port numbers to names. + **-S**, **--summary** print the event summary (i.e. the list of the top events) when the capture ends. **-s** _len_, **--snaplen**=_len_ - Capture the first bytes of each I/O buffer. By default, the first 80 bytes are captured. Use this option with caution, it can generate huge trace files. + Capture the first _len_ bytes of each I/O buffer. By default, the first 80 bytes are captured. Use this option with caution, it can generate huge trace files. **-t** _timetype_, **--timetype**=_timetype_ - Change the way event time is diplayed. Accepted values are **h** for human-readable string, **a** for abosulte timestamp from epoch, **r** for relative time from the beginning of the capture, and **d** for delta between event enter and exit. - + Change the way event time is displayed. Accepted values are **h** for human-readable string, **a** for absolute timestamp from epoch, **r** for relative time from the first displayed event, **d** for delta between event enter and exit, and **D** for delta from the previous event. + +**-T**, **--force-tracers-capture** + Tell the driver to make sure full buffers are captured from /dev/null, to make sure that tracers are completely captured. Note that sysdig will enable extended /dev/null capture by itself after detecting that tracers are written there, but that could result in the truncation of some tracers at the beginning of the capture. This option allows preventing that. + +**--unbuffered** + Turn off output buffering. This causes every single line emitted by sysdig to be flushed, which generates higher CPU usage but is useful when piping sysdig's output into another process or into a script. + **-v**, **--verbose** - Verbose output. + Verbose output. This flag will cause the full content of text and binary buffers to be printed on screen, instead of being truncated to 40 characters. Note that data buffers length is still limited by the snaplen (refer to the -s flag documentation) -v will also make sysdig print some summary information at the end of the capture. + +**--version** + Print version number. **-w** _writefile_, **--write**=_writefile_ Write the captured events to _writefile_. +**-W** _num_ + Turn on file rotation for continuous capture, and limit the number of files created to the specified number. Once the cap is reached, older files will be overwritten (ring buffer). Use in conjunction with the **-C** / **-G** / **-e** options to limit the size of each file based on number of megabytes, seconds, and/or events (respectively). + **-x**, **--print-hex** Print data buffers in hex. **-X**, **--print-hex-ascii** Print data buffers in hex and ASCII. + +**-z**, **--compress** + Used with **-w**, enables compression for tracefiles. EXAMPLES -------- + Capture all the events from the live system and print them to screen > $ sysdig Capture all the events from the live system and save them to disk > $ sysdig -w dumpfile.scap +Capture all the events in the latest 24 hours and save them to disk organized in files containing 1 hour of system activity each +> $ sysdig -G 3600 -W 24 -w dumpfile.scap + Read events from a file and print them to screen > $ sysdig -r dumpfile.scap +Prepare a sanitized version of a system capture +> $ sysdig -r dumpfile.scap 'not evt.buffer contains foo' -w cleandump.scap + Print all the open system calls invoked by cat > $ sysdig proc.name=cat and evt.type=open Print the name of the files opened by cat -> $ ./sysdig -p"%evt.arg.name" proc.name=cat and evt.type=open +> $ sysdig -p"%evt.arg.name" proc.name=cat and evt.type=open List the available chisels -> $ ./sysdig -cl +> $ sysdig -cl Use the spy_ip chisel to look at the data exchanged with 192.168.1.157: > $ sysdig -c spy_ip 192.168.1.157 @@ -177,14 +261,14 @@ FILES BUGS ---- -Bugs? +* sysdig and its chisels are designed to be used with LuaJIT in Lua 5.1 mode. While it is possible to use sysdig with LuaJIT in Lua 5.2 mode or regular Lua, some chisels may not work as expected. AUTHOR ------ -Draios inc. +Draios Inc. aka sysdig SEE ALSO -------- -**strace**(8), **tcpdump**(8), **lsof**(8) +**csysdig**(8), **strace**(8), **tcpdump**(8), **lsof**(8) diff --git a/userspace/sysdig/sysdig.cpp b/userspace/sysdig/sysdig.cpp index 065065d768..eeb9d6d07d 100644 --- a/userspace/sysdig/sysdig.cpp +++ b/userspace/sysdig/sysdig.cpp @@ -1,22 +1,24 @@ /* -Copyright (C) 2013-2014 Draios inc. +Copyright (C) 2013-2018 Draios Inc dba Sysdig. This file is part of sysdig. -sysdig is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +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 -sysdig is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + 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. -You should have received a copy of the GNU General Public License -along with sysdig. If not, see . */ #define __STDC_FORMAT_MACROS + #include #include #include @@ -27,8 +29,14 @@ along with sysdig. If not, see . #include #include -#include "sysdig.h" #include "chisel.h" +#include "scap_open_exception.h" +#include "sinsp_capture_interrupt_exception.h" +#ifdef HAS_CAPTURE +#include "driver_config.h" +#endif // HAS_CAPTURE +#include "sysdig.h" +#include "utils.h" #ifdef _WIN32 #include "win32/getopt.h" @@ -38,7 +46,10 @@ along with sysdig. If not, see . #include #endif -bool ctrl_c_pressed = false; +static bool g_terminate = false; +#ifdef HAS_CHISELS +vector g_chisels; +#endif static void usage(); @@ -47,23 +58,7 @@ static void usage(); // static void signal_callback(int signal) { - ctrl_c_pressed = true; -} - -void replace_in_place(string& str, string substr_to_replace, string new_substr) -{ - size_t index = 0; - uint32_t nsize = substr_to_replace.size(); - - while (true) - { - index = str.find(substr_to_replace, index); - if (index == string::npos) break; - - str.replace(index, nsize, new_substr); - - index += nsize; - } + g_terminate = true; } // @@ -75,37 +70,145 @@ static void usage() "sysdig version " SYSDIG_VERSION "\n" "Usage: sysdig [options] [-p ] [filter]\n\n" "Options:\n" -" -A, --print-ascii Only print the text portion of data buffers, and echo\n" +" -A, --print-ascii Only print the text portion of data buffers, and echo\n" " end-of-lines. This is useful to only display human-readable\n" " data.\n" -" -a, --abstime Show absolute event timestamps\n" -" -c , --chisel \n" +" -b, --print-base64 Print data buffers in base64. This is useful for encoding\n" +" binary data that needs to be used over media designed to\n" +" handle textual data (i.e., terminal or json).\n" +" -B, --bpf=\n" +" Enable live capture using the specified BPF probe instead of the kernel module.\n" +" The BPF probe can also be specified via the environment variable\n" +" SYSDIG_BPF_PROBE. If is left empty, sysdig will\n" +" try to load one from the sysdig-probe-loader script.\n" +#ifdef HAS_CHISELS +" -c , --chisel \n" " run the specified chisel. If the chisel require arguments,\n" " they must be specified in the command line after the name.\n" " -cl, --list-chisels\n" -" lists the available chisels. Looks for chisels in .,\n" -" ./chisels, ~/chisels and /usr/share/sysdig/chisels.\n" +" lists the available chisels. Looks for chisels in\n" +" ./chisels, ~/.chisels and /usr/share/sysdig/chisels.\n" +#endif +" -C , --file-size=\n" +" Before writing an event, check whether the file is\n" +" currently larger than file_size and, if so, close the\n" +" current file and open a new one. Saved files will have the\n" +" name specified with the -w flag, with a number after it,\n" +" starting at 0 and continuing upward. The units of file_size\n" +" are millions of bytes (10^6, not 2^20). Use the -W flag to\n" +" determine how many files will be saved to disk.\n" +#ifdef HAS_CAPTURE +#ifndef MINIMAL_BUILD +" --cri Path to CRI socket for container metadata\n" +" Use the specified socket to fetch data from a CRI-compatible runtime\n" +"\n" +" --cri-timeout \n" +" Wait at most milliseconds for response from CRI\n" +#endif // MINIMAL_BUILD +#endif // HAS_CAPTURE " -d, --displayflt Make the given filter a display one\n" -" Setting this option causes the events to be filtered\n" +" Setting this option causes the events to be filtered\n" " after being parsed by the state system. Events are\n" " normally filtered before being analyzed, which is more\n" -" efficient, but can cause state (e.g. FD names) to be lost\n" +" efficient, but can cause state (e.g. FD names) to be lost.\n" +" -D, --debug Capture events about sysdig itself, display internal events\n" +" in addition to system events, and print additional\n" +" logging on standard error.\n" +" -E, --exclude-users\n" +" Don't create the user/group tables by querying the OS when\n" +" sysdig starts. This also means that no user or group info\n" +" will be written to the trace file by the -w flag.\n" +" The user/group tables are necessary to use filter fields\n" +" like user.name or group.name. However, creating them can\n" +" increase sysdig's startup time. Moreover, they contain\n" +" information that could be privacy sensitive.\n" +" -e If used together with -w option, creates a series of dump files\n" +" containing only a specified number of events given in num_events\n" +" parameter each.\n" +" Used alongside -W flags creates a ring buffer of file containing\n" +" num_events each.\n" +" -F, --fatfile Enable fatfile mode\n" +" when writing in fatfile mode, the output file will contain\n" +" events that will be invisible when reading the file, but\n" +" that are necessary to fully reconstruct the state.\n" +" Fatfile mode is useful when saving events to disk with an\n" +" aggressive filter. The filter could drop events that would\n" +" the state to be updated (e.g. clone() or open()). With\n" +" fatfile mode, those events are still saved to file, but\n" +" 'hidden' so that they won't appear when reading the file.\n" +" Be aware that using this flag might generate substantially\n" +" bigger traces files.\n" +" --filter-proclist apply the filter to the process table\n" +" a full dump of /proc is typically included in any trace file\n" +" to make sure all the state required to decode events is in the\n" +" file. This could cause the file to contain unwanted or sensitive\n" +" information. Using this flag causes the command line filter to\n" +" be applied to the /proc dump as well.\n" +" -G , --seconds=\n" +" Rotates the dump file specified with the -w option every\n" +" num_seconds seconds. Saved files will have the name specified\n" +" by -w which should include a time format as defined by strftime(3).\n" +" If no time format is specified, a counter will be used.\n" +" If no data format is specified, this can be used with -W flag to\n" +" create a ring buffer of events.\n" " -h, --help Print this page\n" +#ifdef HAS_CHISELS " -i , --chisel-info \n" " Get a longer description and the arguments associated with\n" " a chisel found in the -cl option list.\n" -" -j, --json Emit output as json\n" +#endif +" -j, --json Emit output as json, data buffer encoding will depend from the\n" +" print format selected.\n" +#ifndef MINIMAL_BUILD +" -k , --k8s-api=\n" +" Enable Kubernetes support by connecting to the API server\n" +" specified as argument. E.g. \"http://admin:password@127.0.0.1:8080\".\n" +" The API server can also be specified via the environment variable\n" +" SYSDIG_K8S_API.\n" +" -K | :[:], --k8s-api-cert= | :[:]\n" +" Use the provided files names to authenticate user and (optionally) verify the K8S API\n" +" server identity.\n" +" Each entry must specify full (absolute, or relative to the current directory) path\n" +" to the respective file.\n" +" Private key password is optional (needed only if key is password protected).\n" +" CA certificate is optional. For all files, only PEM file format is supported. \n" +" Specifying CA certificate only is obsoleted - when single entry is provided \n" +" for this option, it will be interpreted as the name of a file containing bearer token.\n" +" Note that the format of this command-line option prohibits use of files whose names contain\n" +" ':' or '#' characters in the file name.\n" +" Option can also be provided via the environment variable SYSDIG_K8S_API_CERT.\n" +#endif // MINIMAL_BUILD +" -L, --list-events List the events that the engine supports\n" " -l, --list List the fields that can be used for filtering and output\n" " formatting. Use -lv to get additional information for each\n" " field.\n" -" -L, --list-events List the events that the engine supports\n" +" --large-environment\n" +" Support environments larger than 4KiB\n" +" When the environment is larger than 4KiB, load the whole\n" +" environment from /proc instead of truncating to the first 4KiB\n" +" This may fail for short-lived processes and in that case\n" +" the truncated environment is used instead.\n" +" --list-markdown like -l, but produces markdown output\n" +" -m , --mesos-api=\n" +" Enable Mesos support by connecting to the API server\n" +" specified as argument. E.g. \"http://admin:password@127.0.0.1:5050\".\n" +" Marathon url is optional and defaults to Mesos address, port 8080.\n" +" The API servers can also be specified via the environment variable\n" +" SYSDIG_MESOS_API.\n" +" -M Stop collecting after reached.\n" " -n , --numevents=\n" " Stop capturing after events\n" +" --page-faults Capture user/kernel major/minor page faults\n" +" -P, --progress Print progress on stderr while processing trace files\n" " -p , --print=\n" " Specify the format to be used when printing the events.\n" +" With -pc or -pcontainer will use a container-friendly format.\n" +" With -pk or -pkubernetes will use a kubernetes-friendly format.\n" +" With -pm or -pmesos will use a mesos-friendly format.\n" " See the examples section below for more info.\n" -" -q, --quiet Don't print events on the screen.\n" +" -q, --quiet Don't print events on the screen\n" " Useful when dumping to disk.\n" +" -R Resolve port numbers to names.\n" " -r , --read=\n" " Read the events from .\n" " -S, --summary print the event summary (i.e. the list of the top events)\n" @@ -115,32 +218,71 @@ static void usage() " By default, the first 80 bytes are captured. Use this\n" " option with caution, it can generate huge trace files.\n" " -t , --timetype=\n" -" Change the way event time is diplayed. Accepted values are\n" -" h for human-readable string, a for abosulte timestamp from\n" -" epoch, r for relative time from the beginning of the\n" -" capture, and d for delta between event enter and exit.\n" +" Change the way event time is displayed. Accepted values are\n" +" h for human-readable string, a for absolute timestamp from\n" +" epoch, r for relative time from the beginning of the\n" +" capture, d for delta between event enter and exit, and\n" +" D for delta from the previous event.\n" +" -T, --force-tracers-capture\n" +" Tell the driver to make sure full buffers are captured from\n" +" /dev/null, to make sure that tracers are completely\n" +" captured. Note that sysdig will enable extended /dev/null\n" +" capture by itself after detecting that tracers are written\n" +" there, but that could result in the truncation of some\n" +" tracers at the beginning of the capture. This option allows\n" +" preventing that.\n" +" --unbuffered Turn off output buffering. This causes every single line\n" +" emitted by sysdig to be flushed, which generates higher CPU\n" +" usage but is useful when piping sysdig's output into another\n" +" process or into a script.\n" +" -U, --suppress-comm\n" +" Ignore all events from processes having the provided comm.\n" " -v, --verbose Verbose output.\n" +" This flag will cause the full content of text and binary\n" +" buffers to be printed on screen, instead of being truncated\n" +" to 40 characters. Note that data buffers length is still\n" +" limited by the snaplen (refer to the -s flag documentation)\n" +" -v will also make sysdig print some summary information at\n" +" the end of the capture.\n" +" --version Print version number.\n" " -w , --write=\n" " Write the captured events to .\n" -" -x, --print-hex Print data buffers in hex.\n" +" -W , --limit \n" +" Used in conjunction with the -C option, this will limit the number\n" +" of files created to the specified number, and begin overwriting files\n" +" from the beginning, thus creating a 'rotating' buffer.\n" +"\n" +" Used in conjunction with the -G option, this will limit the number\n" +" of rotated dump files that get created, exiting with status 0 when\n" +" reaching the limit. If used with -C as well, the behavior will result\n" +" in cyclical files per timeslice.\n" +" -x, --print-hex Print data buffers in hex.\n" " -X, --print-hex-ascii\n" " Print data buffers in hex and ASCII.\n" +" -z, --compress Used with -w, enables compression for trace files.\n" "\n" "Output format:\n\n" "By default, sysdig prints the information for each captured event on a single\n" " line with the following format:\n\n" -" %%evt.cpu %%proc.name (%%thread.tid) %%evt.dir %%evt.type %%evt.args\n\n" +" %%evt.num %%evt.outputtime %%evt.cpu %%proc.name (%%thread.tid) %%evt.dir %%evt.type %%evt.info\n\n" "where:\n" +" evt.num is the incremental event number\n" " evt.time is the event timestamp\n" " evt.cpu is the CPU number where the event was captured\n" " proc.name is the name of the process that generated the event\n" -" thread.tid id the TID that generated the event, which corresponds to the\n" -" PID for single thread processes\n" -" evt.dir is the event direction, > for enter events and < for exit events\n" +" thread.tid id the TID that generated the event, which corresponds to the\n" +" PID for single thread processes\n" +" evt.dir is the event direction, > for enter events and < for exit events\n" " evt.type is the name of the event, e.g. 'open' or 'read'\n" -" evt.args is the list of event arguments.\n\n" +" evt.info is the list of event arguments.\n\n" "The output format can be customized with the -p switch, using any of the\n" "fields listed by 'sysdig -l'.\n\n" +"Using -pc or -pcontainer, the default format will be changed to a container-friendly one:\n\n" +"%%evt.num %%evt.outputtime %%evt.cpu %%container.name (%%container.id) %%proc.name (%%thread.tid:%%thread.vtid) %%evt.dir %%evt.type %%evt.info\n\n" +"Using -pk or -pkubernetes, the default format will be changed to a kubernetes-friendly one:\n\n" +"%%evt.num %%evt.outputtime %%evt.cpu %%k8s.pod.name (%%container.id) %%proc.name (%%thread.tid:%%thread.vtid) %%evt.dir %%evt.type %%evt.info\n\n" +"Using -pm or -pmesos, the default format will be changed to a mesos-friendly one:\n\n" +"%%evt.num %%evt.outputtime %%evt.cpu %%mesos.task.name (%%container.id) %%proc.name (%%thread.tid:%%thread.vtid) %%evt.dir %%evt.type %%evt.info\n\n" "Examples:\n\n" " Capture all the events from the live system and print them to screen\n" " $ sysdig\n\n" @@ -151,12 +293,12 @@ static void usage() " Print all the open system calls invoked by cat\n" " $ sysdig proc.name=cat and evt.type=open\n\n" " Print the name of the files opened by cat\n" -" $ ./sysdig -p\"%%evt.arg.name\" proc.name=cat and evt.type=open\n\n" +" $ sysdig -p\"%%evt.arg.name\" proc.name=cat and evt.type=open\n\n" ); } -void print_summary_table(sinsp* inspector, - vector* summary_table, +void print_summary_table(sinsp* inspector, + vector &summary_table, uint32_t nentries) { sinsp_evttables* einfo = inspector->get_event_info_tables(); @@ -168,82 +310,304 @@ void print_summary_table(sinsp* inspector, cout << tstr; cout << "----------------------\n"; - sort(summary_table->begin(), summary_table->end(), + sort(summary_table.begin(), summary_table.end(), summary_table_entry_rsort_comparer()); for(uint32_t j = 0; j < nentries; j++) { - summary_table_entry* e = &summary_table->at(j); + const summary_table_entry &e = summary_table.at(j); - if(e->m_ncalls == 0) + if(e.m_ncalls == 0) { break; } - if(e->m_is_unsupported_syscall) + if(e.m_is_unsupported_syscall) { - tstr = einfo->m_syscall_info_table[e->m_id / 2].name; + tstr = einfo->m_syscall_info_table[e.m_id / 2].name; tstr.resize(16, ' '); - printf("%s%s%" PRIu64 "\n", - (PPME_IS_ENTER(e->m_id))? "> ": "< ", - tstr.c_str(), - e->m_ncalls); + printf("%s%s%" PRIu64 "\n", + (PPME_IS_ENTER(e.m_id))? "> ": "< ", + tstr.c_str(), + e.m_ncalls); } else { - tstr = einfo->m_event_info[e->m_id].name; + tstr = einfo->m_event_info[e.m_id].name; tstr.resize(16, ' '); - printf("%s%s%" PRIu64 "\n", - (PPME_IS_ENTER(e->m_id))? "> ": "< ", - tstr.c_str(), - e->m_ncalls); + printf("%s%s%" PRIu64 "\n", + (PPME_IS_ENTER(e.m_id))? "> ": "< ", + tstr.c_str(), + e.m_ncalls); + } + } +} + +#ifdef HAS_CHISELS +static void add_chisel_dirs(sinsp* inspector) +{ + // + // Add the default chisel directory statically configured by the build system + // + inspector->add_chisel_dir(SYSDIG_INSTALLATION_DIR CHISELS_INSTALLATION_DIR, false); + + // + // Add the directories configured in the SYSDIG_CHISEL_DIR environment variable + // + char* s_user_cdirs = getenv("SYSDIG_CHISEL_DIR"); + + if(s_user_cdirs != NULL) + { + vector user_cdirs = sinsp_split(s_user_cdirs, ';'); + + for(uint32_t j = 0; j < user_cdirs.size(); j++) + { + inspector->add_chisel_dir(user_cdirs[j], true); + } + } +} +#endif + +static void initialize_chisels() +{ +#ifdef HAS_CHISELS + for(uint32_t j = 0; j < g_chisels.size(); j++) + { + g_chisels[j]->on_init(); + } +#endif +} + +// +// Parse the command line following a chisel to consume the chisel command line. +// We use the following strategy: +// - if the chisel has no arguments, we don't consume anything +// - if the chisel has at least one required argument, we consume the next command line token +// - if the chisel has only optional arguments, we consume the next token, unless +// - there is no next token +// - the next token starts with a '-' +// - the rest of the command line contains a valid filter +// +static void parse_chisel_args(sinsp_chisel* ch, sinsp* inspector, int optind, int argc, char **argv, int32_t* n_filterargs) +{ + uint32_t nargs = ch->get_n_args(); + uint32_t nreqargs = ch->get_n_required_args(); + string args; + + if(nargs != 0) + { + if(optind > (int32_t)argc) + { + throw sinsp_exception("invalid number of arguments for chisel " + string(optarg) + ", " + to_string((long long int)nargs) + " expected."); + } + else if(optind < (int32_t)argc) + { + args = argv[optind]; + + if(nreqargs != 0) + { + ch->set_args(args); + (*n_filterargs)++; + } + else + { + if(args[0] != '-') + { + string testflt; + + for(int32_t j = optind; j < argc; j++) + { + testflt += argv[j]; + if(j < argc - 1) + { + testflt += " "; + } + } + + if(nargs == 1 && ch->get_lua_script_info()->m_args[0].m_type == "filter") + { + ch->set_args(args); + (*n_filterargs)++; + } + else + { + try + { + sinsp_filter_compiler compiler(inspector, testflt); + sinsp_filter* s = compiler.compile(); + delete s; + } + catch(...) + { + ch->set_args(args); + (*n_filterargs)++; + } + } + } + } + } + else + { + if(nreqargs != 0) + { + throw sinsp_exception("missing arguments for chisel " + string(optarg)); + } + } + } +} + +static void free_chisels() +{ +#ifdef HAS_CHISELS + for(vector::iterator it = g_chisels.begin(); + it != g_chisels.end(); ++it) + { + delete *it; + } + + g_chisels.clear(); +#endif +} + +static void chisels_on_capture_start() +{ +#ifdef HAS_CHISELS + for(uint32_t j = 0; j < g_chisels.size(); j++) + { + g_chisels[j]->on_capture_start(); + } +#endif +} + +static void chisels_on_capture_end() +{ +#ifdef HAS_CHISELS + for(vector::iterator it = g_chisels.begin(); + it != g_chisels.end(); ++it) + { + (*it)->on_capture_end(); + } +#endif +} + +static void chisels_do_timeout(sinsp_evt* ev) +{ +#ifdef HAS_CHISELS + for(vector::iterator it = g_chisels.begin(); + it != g_chisels.end(); ++it) + { + (*it)->do_timeout(ev); + } +#endif +} + +void handle_end_of_file(bool print_progress, sinsp_evt_formatter* formatter = NULL) +{ + string line; + + // Notify the formatter that we are at the + // end of the capture in case it needs to + // write any terminating characters + if(formatter != NULL && formatter->on_capture_end(&line)) + { + cout << line << endl; + } + + // + // Reached the end of a trace file. + // If we are reporting progress, this is 100% + // + if(print_progress) + { + fprintf(stderr, "100.00\n"); + fflush(stderr); + } + + // + // Notify the chisels that we're exiting. + // + try + { + chisels_on_capture_end(); + } + catch(...) + { + } +} + +vector split_nextrun_args(string na) +{ + vector res; + uint32_t laststart = 0; + uint32_t j; + bool inquote = false; + + for(j = 0; j < na.size(); j++) + { + if(na[j] == '"') + { + inquote = !inquote; + } + else if(na[j] == ' ') + { + if(!inquote) + { + string arg = na.substr(laststart, j - laststart); + replace_in_place(arg, "\"", ""); + res.push_back(arg); + laststart = j + 1; + } } } + + res.push_back(na.substr(laststart, j - laststart)); + laststart = j + 1; + + return res; } // // Event processing loop // -captureinfo do_inspect(sinsp* inspector, - uint64_t cnt, - bool quiet, - bool absolute_times, - sinsp_filter* display_filter, - vector* chisels, - vector* summary_table, - sinsp_evt_formatter* formatter) +captureinfo do_inspect(sinsp* inspector, + uint64_t cnt, + uint64_t duration_to_tot_ns, + bool quiet, + bool json, + bool do_flush, + bool print_progress, + sinsp_filter* display_filter, + vector &summary_table, + sinsp_evt_formatter* formatter) { captureinfo retval; int32_t res; sinsp_evt* ev; - uint64_t ts; - uint64_t deltats = 0; - uint64_t firstts = 0; string line; + double last_printed_progress_pct = 0; + uint64_t duration_start = 0; + + if(json) + { + do_flush = true; + } // // Loop through the events // while(1) { - if(retval.m_nevts == cnt || ctrl_c_pressed) + if(retval.m_nevts == cnt || g_terminate) { // // End of capture, either because the user stopped it, or because // we reached the event count specified with -n. - // Notify the chisels that we're exiting. // - for(vector::iterator it = chisels->begin(); - it != chisels->end(); ++it) - { - (*it)->on_capture_end(); - } - + handle_end_of_file(print_progress, formatter); break; } - res = inspector->next(&ev); if(res == SCAP_TIMEOUT) @@ -254,24 +618,14 @@ captureinfo do_inspect(sinsp* inspector, // The event has been dropped by the filtering system. // Give the chisels a chance to run their timeout logic. // - for(vector::iterator it = chisels->begin(); it != chisels->end(); ++it) - { - (*it)->do_timeout(ev); - } + chisels_do_timeout(ev); } + continue; } else if(res == SCAP_EOF) { - // - // Reached the end of a trace file. - // Notify the chisels that we're exiting. - // - for(vector::iterator it = chisels->begin(); it != chisels->end(); ++it) - { - (*it)->on_capture_end(); - } - + handle_end_of_file(print_progress, formatter); break; } else if(res != SCAP_SUCCESS) @@ -280,30 +634,46 @@ captureinfo do_inspect(sinsp* inspector, // Event read error. // Notify the chisels that we're exiting, and then die with an error. // - for(vector::iterator it = chisels->begin(); it != chisels->end(); ++it) - { - (*it)->on_capture_end(); - } - + handle_end_of_file(print_progress, formatter); cerr << "res = " << res << endl; throw sinsp_exception(inspector->getlasterr().c_str()); } + if (duration_start == 0) + { + duration_start = ev->get_ts(); + } else if(duration_to_tot_ns > 0) + { + if(ev->get_ts() - duration_start >= duration_to_tot_ns) + { + handle_end_of_file(print_progress, formatter); + break; + } + } retval.m_nevts++; - ts = ev->get_ts(); - if(firstts == 0) + if(print_progress) { - firstts = ts; + if(ev->get_num() % 10000 == 0) + { + double progress_pct = inspector->get_read_progress(); + + if(progress_pct - last_printed_progress_pct > 0.1) + { + fprintf(stderr, "%.2lf\n", progress_pct); + fflush(stderr); + last_printed_progress_pct = progress_pct; + } + } } - deltats = ts - firstts; // // If there are chisels to run, run them // - if(!chisels->empty()) +#ifdef HAS_CHISELS + if(!g_chisels.empty()) { - for(vector::iterator it = chisels->begin(); it != chisels->end(); ++it) + for(vector::iterator it = g_chisels.begin(); it != g_chisels.end(); ++it) { if((*it)->run(ev) == false) { @@ -312,29 +682,30 @@ captureinfo do_inspect(sinsp* inspector, } } else - { +#endif + { // // If we're supposed to summarize, increase the count for this event // - if(summary_table != NULL) + if(!summary_table.empty()) { uint16_t etype = ev->get_type(); - + if(etype == PPME_GENERIC_E) { sinsp_evt_param *parinfo = ev->get_param(0); uint16_t id = *(int16_t *)parinfo->m_val; - ((*summary_table)[PPM_EVENT_MAX + id * 2]).m_ncalls++; + summary_table[PPM_EVENT_MAX + id * 2].m_ncalls++; } else if(etype == PPME_GENERIC_X) { sinsp_evt_param *parinfo = ev->get_param(0); uint16_t id = *(int16_t *)parinfo->m_val; - ((*summary_table)[PPM_EVENT_MAX + id * 2 + 1]).m_ncalls++; + summary_table[PPM_EVENT_MAX + id * 2 + 1].m_ncalls++; } else { - ((*summary_table)[etype]).m_ncalls++; + summary_table[etype].m_ncalls++; } } @@ -347,98 +718,170 @@ captureinfo do_inspect(sinsp* inspector, continue; } - // - // Output the line - // - if(display_filter) + if(!inspector->is_debug_enabled() && + ev->get_category() & EC_INTERNAL) { - if(!display_filter->run(ev)) - { - continue; - } + continue; } if(formatter->tostring(ev, &line)) { + // + // Output the line + // + if(display_filter) + { + if(!display_filter->run(ev)) + { + continue; + } + } + cout << line << endl; } } + + if(do_flush) + { + cout << flush; + } } - retval.m_time = deltats; return retval; } // -// MAIN +// ARGUMENT PARSING AND PROGRAM SETUP // -int main(int argc, char **argv) +sysdig_init_res sysdig_init(int argc, char **argv) { - int res = EXIT_SUCCESS; + sysdig_init_res res; sinsp* inspector = NULL; - string infile; + vector infiles; string outfile; int op; uint64_t cnt = -1; - bool emitjson = false; bool quiet = false; - bool absolute_times = false; bool is_filter_display = false; bool verbose = false; bool list_flds = false; + bool list_flds_markdown = false; + bool print_progress = false; + bool compress = false; sinsp_evt::param_fmt event_buffer_format = sinsp_evt::PF_NORMAL; sinsp_filter* display_filter = NULL; double duration = 1; + int duration_to_tot = 0; captureinfo cinfo; string output_format; uint32_t snaplen = 0; int long_index = 0; - vector chisels; int32_t n_filterargs = 0; - int cflag = 0; + bool jflag = false; + bool unbuf_flag = false; + bool filter_proclist_flag = false; string cname; - vector* summary_table = NULL; - string timefmt = "%evt.time"; + vector summary_table; +#ifndef MINIMAL_BUILD + string* k8s_api = 0; + string* k8s_api_cert = 0; + string* mesos_api = 0; +#endif // MINIMAL_BUILD + bool force_tracers_capture = false; + bool page_faults = false; + bool bpf = false; + string bpf_probe; + std::set suppress_comms; +#ifdef HAS_CAPTURE + string cri_socket_path; +#endif + bool udig = false; + + // These variables are for the cycle_writer engine + int duration_seconds = 0; + int rollover_mb = 0; + int file_limit = 0; + unsigned long event_limit = 0L; - static struct option long_options[] = + static struct option long_options[] = { {"print-ascii", no_argument, 0, 'A' }, - {"abstimes", no_argument, 0, 'a' }, + {"print-base64", no_argument, 0, 'b' }, + {"bpf", optional_argument, 0, 'B' }, +#ifdef HAS_CHISELS {"chisel", required_argument, 0, 'c' }, - {"list-chisels", no_argument, &cflag, 1 }, + {"list-chisels", no_argument, 0, 0 }, +#endif +#ifdef HAS_CAPTURE + {"cri", required_argument, 0, 0 }, + {"cri-timeout", required_argument, 0, 0 }, +#endif {"displayflt", no_argument, 0, 'd' }, + {"debug", no_argument, 0, 'D'}, + {"exclude-users", no_argument, 0, 'E' }, + {"event-limit", required_argument, 0, 'e'}, + {"fatfile", no_argument, 0, 'F'}, + {"filter-proclist", no_argument, 0, 0 }, + {"seconds", required_argument, 0, 'G' }, {"help", no_argument, 0, 'h' }, +#ifdef HAS_CHISELS {"chisel-info", required_argument, 0, 'i' }, +#endif + {"file-size", required_argument, 0, 'C' }, {"json", no_argument, 0, 'j' }, + {"k8s-api", required_argument, 0, 'k'}, + {"k8s-api-cert", required_argument, 0, 'K' }, + {"large-environment", no_argument, 0, 0 }, {"list", no_argument, 0, 'l' }, {"list-events", no_argument, 0, 'L' }, + {"list-markdown", no_argument, 0, 0 }, + {"mesos-api", required_argument, 0, 'm'}, {"numevents", required_argument, 0, 'n' }, + {"page-faults", no_argument, 0, 0 }, + {"progress", required_argument, 0, 'P' }, {"print", required_argument, 0, 'p' }, {"quiet", no_argument, 0, 'q' }, + {"resolve-ports", no_argument, 0, 'R'}, {"readfile", required_argument, 0, 'r' }, {"snaplen", required_argument, 0, 's' }, {"summary", no_argument, 0, 'S' }, + {"suppress-comm", required_argument, 0, 'U' }, + {"udig", required_argument, 0, 'u' }, {"timetype", required_argument, 0, 't' }, + {"force-tracers-capture", required_argument, 0, 'T'}, + {"unbuffered", no_argument, 0, 0 }, {"verbose", no_argument, 0, 'v' }, + {"version", no_argument, 0, 0 }, {"writefile", required_argument, 0, 'w' }, + {"limit", required_argument, 0, 'W' }, {"print-hex", no_argument, 0, 'x'}, {"print-hex-ascii", no_argument, 0, 'X'}, + {"compress", no_argument, 0, 'z' }, {0, 0, 0, 0} }; - output_format = "*%evt.num